bool BufferSinkFilterContext::getSamples(AVFrame *frame, int nbSamples, OptionalErrorCode ec) { clear_if(ec); if (!m_sink) { throws_if(ec, Errors::Unallocated); return false; } if (m_req == ReqGetFrame) { throws_if(ec, Errors::MixBufferSinkAccess); return false; } m_req = ReqGetSamples; int sts = av_buffersink_get_samples(m_sink.raw(), frame, nbSamples); if (sts < 0) { if (sts == AVERROR_EOF || sts == AVERROR(EAGAIN)) { if (ec) { *ec = make_ffmpeg_error(sts); } } else { throws_if(ec, sts, ffmpeg_category()); } return false; } return true; }
void BufferSinkFilterContext::assign(const FilterContext &ctx, OptionalErrorCode ec) { clear_if(ec); m_type = checkFilter(ctx.filter()); if (m_type == FilterMediaType::Unknown) { throws_if(ec, Errors::IncorrectBufferSinkFilter); return; } m_sink = ctx; }
FilterContext FilterGraph::filter(const string &name, error_code &ec) { clear_if(ec); if (!m_raw) { throws_if(ec, Errors::Unallocated); return FilterContext(); } AVFilterContext *ctx = avfilter_graph_get_filter(m_raw, name.c_str()); if (!ctx) return FilterContext(); return FilterContext(ctx); }
void BufferSinkFilterContext::setFrameSize(unsigned size, OptionalErrorCode ec) { clear_if(ec); static_assert(LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(3,17,100), "BufferSink set frame size functionality does not present on FFmpeg prior 1.0"); if (!m_sink) { throws_if(ec, Errors::Unallocated); return; } if (m_type != FilterMediaType::Audio) { throws_if(ec, Errors::IncorrectBufferSinkMediaType); return; } av_buffersink_set_frame_size(m_sink.raw(), size); }
FilterContext FilterGraph::allocFilter(const Filter &filter, const std::string &name, error_code &ec) { clear_if(ec); if (!m_raw) { throws_if(ec, Errors::Unallocated); return FilterContext(); } AVFilterContext *ctx = avfilter_graph_alloc_filter(m_raw, filter.raw(), name.c_str()); if (!ctx) { throws_if(ec, ENOMEM, system_category()); return FilterContext(); } return FilterContext(ctx); }
void FilterGraph::config(error_code &ec) { clear_if(ec); if (!m_raw) { throws_if(ec, Errors::Unallocated); return; } int sts = avfilter_graph_config(m_raw, nullptr); if (sts < 0) { throws_if(ec, sts, ffmpeg_category()); return; } m_configured = true; }
Rational BufferSinkFilterContext::frameRate(OptionalErrorCode ec) { clear_if(ec); static_assert(LIBAVFILTER_VERSION_INT >= AV_VERSION_INT(3,17,100), "BufferSink get frame rate functionality does not present on FFmpeg prior 1.0"); if (!m_sink) { throws_if(ec, Errors::Unallocated); return Rational(); } if (m_type != FilterMediaType::Video) { throws_if(ec, Errors::IncorrectBufferSinkMediaType); return Rational(); } return av_buffersink_get_frame_rate(m_sink.raw()); }
FilterContext FilterGraph::filter(unsigned idx, error_code &ec) { clear_if(ec); if (!m_raw) { throws_if(ec, Errors::Unallocated); return FilterContext(); } if (idx < m_raw->nb_filters) { throws_if(ec, Errors::OutOfRange); return FilterContext(); } AVFilterContext *ctx = m_raw->filters[idx]; if (!ctx) { throws_if(ec, Errors::InvalidArgument); return FilterContext(); } return FilterContext(ctx); }
FilterContext FilterGraph::createFilter(const Filter &filter, const string &filterName, const string &filterArgs, error_code &ec) { clear_if(ec); if (!m_raw || filter.isNull()) { throws_if(ec, Errors::Unallocated); return FilterContext(); } AVFilterContext *ctx = nullptr; int stat = avfilter_graph_create_filter(&ctx, filter.raw(), filterName.c_str(), filterArgs.empty() ? nullptr : filterArgs.c_str(), this, m_raw); if (stat < 0) { throws_if(ec, stat, ffmpeg_category()); return FilterContext(); } return FilterContext(ctx); }
void FilterGraph::parse(const string &graphDescription, FilterContext &srcFilterCtx, FilterContext &sinkFilterCtx, error_code &ec) { clear_if(ec); if (!m_raw || !srcFilterCtx || !sinkFilterCtx) { throws_if(ec, Errors::Unallocated); return; } auto srcf = srcFilterCtx.raw(); auto sinkf = sinkFilterCtx.raw(); bool srcfFound = false; bool sinkfFound = false; for (size_t i = 0; i < (size_t)filtersCount(); ++i) { if (m_raw->filters[i] == srcf) srcfFound = true; if (m_raw->filters[i] == sinkf) sinkfFound = true; if (srcfFound && sinkfFound) // search completed break; } if (!srcfFound) { fflog(AV_LOG_ERROR, "Source filter does not present in filter graph"); throws_if(ec, Errors::FilterNotInFilterGraph); return; } if (!sinkfFound) { fflog(AV_LOG_ERROR, "Sink filter does not present in filter graph"); throws_if(ec, Errors::FilterNotInFilterGraph); return; } struct FilterInOutDeleter { void operator()(AVFilterInOut *&ptr) { avfilter_inout_free(&ptr); } }; using FilterInOutPtr = std::unique_ptr<AVFilterInOut, FilterInOutDeleter>; FilterInOutPtr inputs; FilterInOutPtr outputs; if (graphDescription.empty()) { fflog(AV_LOG_ERROR, "Empty graph description"); throws_if(ec, Errors::FilterGraphDescriptionEmpty); return; } else { outputs.reset(avfilter_inout_alloc()); inputs.reset(avfilter_inout_alloc()); if (!outputs || !inputs) { throws_if(ec, errc::not_enough_memory); return; } outputs->name = av_strdup("in"); outputs->filter_ctx = srcFilterCtx.raw(); outputs->pad_idx = 0; outputs->next = 0; inputs->name = av_strdup("out"); inputs->filter_ctx = sinkFilterCtx.raw(); inputs->pad_idx = 0; inputs->next = 0; int sts = avfilter_graph_parse(m_raw, graphDescription.c_str(), inputs.get(), outputs.get(), nullptr); if (sts < 0) { throws_if(ec, sts, ffmpeg_category()); return; } } }