avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
Filters

A filter graph sits between a decoder and an encoder: frames enter through a source filter, pass through any number of filters described by a string, and leave through a sink filter.

Building the graph

// Setup filter
Filter filter_buffer_src{"buffer"};
Filter filter_buffer_sink{"buffersink"};
FilterGraph filter_graph;
// Input and ouput contexts
std::stringstream src_args;
src_args << "video_size=" << vdec.width() << "x" << vdec.height() << ":pix_fmt=" << vdec.pixelFormat()
<< ":time_base=" << vdec.timeBase() << ":pixel_aspect=" << vdec.sampleAspectRatio();
clog << "Input arguments: " << src_args.str() << endl;
FilterContext src_ctx = filter_graph.createFilter(filter_buffer_src, "in", src_args.str(), ec);
if (ec)
{
clog << "Error creating filter source context: " << ec << ", " << ec.message() << endl;
return 1;
}
FilterContext sink_ctx = filter_graph.createFilter(filter_buffer_sink, "out", "", ec);
if (ec)
{
clog << "Error creating filter sink context: " << ec << ", " << ec.message() << endl;
return 1;
}
// Setup the filter chain
string filterChain;
if (argc > 3) {
filterChain = argv[3];
} else {
filterChain = "scale=320x200:-1:flags=lanczos,split [s0][s1]; "
"[s0] palettegen [p]; [s1][p] paletteuse[f]; ";
}
cout << "Filter: " << filterChain << endl;
filter_graph.parse(filterChain, src_ctx, sink_ctx, ec);
if (ec) {
clog << "Error parsing filter chain: " << ec << ", " << ec.message() << endl;
return 1;
}
filter_graph.config(ec);
if (ec)
{
clog << "Error configuring filter chain: " << ec << ", " << ec.message() << endl;
return 1;
}
// Setup the entry/exit points
BufferSrcFilterContext buffer_src{src_ctx};
BufferSinkFilterContext buffer_sink{sink_ctx};

Four steps:

  1. Name the endpoints. Filter{"buffer"} and Filter{"buffersink"} are the standard entry and exit filters of every graph. They are not part of the chain string.
  2. Describe the input. The source arguments must match the frames that will be pushed: size, pixel format, time base and pixel aspect ratio. A mismatch fails at config() time or, worse, produces a silently wrong graph.
  3. Parse the chain. parse(chain, source, sink) resolves the filter names and wires the graph. The chain syntax is FFmpeg's own: scale=320x200:-1:flags=lanczos,split [s0][s1]; ....
  4. Configure. config() negotiates formats and allocates the filters. Nothing can be pushed before it.

BufferSrcFilterContext and BufferSinkFilterContext are typed handles over the two endpoints. Build them after config(), from the same contexts.

Pushing and pulling

// Push into the filter
cout << "Pushing frame into filter: " << frame.width() << "x" << frame.height() << ", size=" << frame.size()
<< ", ts=" << frame.pts() << ", tb: " << frame.timeBase() << endl;
buffer_src.addVideoFrame(frame, AV_BUFFERSRC_FLAG_KEEP_REF, ec);
if (ec)
{
clog << "Filter push error: " << ec << ", " << ec.message() << endl;
return 1;
}
// Pull until there are no more incoming frames
VideoFrame filtered;
while (buffer_sink.getVideoFrame(filtered, ec))
{
filtered.setTimeBase(encoder.timeBase());
auto ts = filtered.pts();
clog << " filtered: " << filtered.width() << "x" << filtered.height() << ", size=" << filtered.size()
<< ", ts=" << ts << ", tm: " << ts.seconds() << ", tb: " << filtered.timeBase()
<< ", ref=" << filtered.isReferenced() << ":" << filtered.refCount() << endl;
// Encode
Packet opkt = filtered ? encoder.encode(filtered, ec) : encoder.encode(ec);
if (ec) {
cerr << "Encoding error: " << ec << endl;
return 1;
}
if (opkt) {
// Only one output stream
opkt.setStreamIndex(0);
clog << "Write packet: pts=" << opkt.pts() << ", dts=" << opkt.dts() << " / " << opkt.pts().seconds() << " / " << opkt.timeBase() << " / st: " << opkt.streamIndex() << endl;
octx.writePacket(opkt, ec);
if (ec) {
cerr << "Error write packet: " << ec << ", " << ec.message() << endl;
return 1;
}
}
}

One frame in does not mean one frame out: split, fps and timeline filters change the ratio, and a sink can return several frames after one push. The inner loop keeps calling getVideoFrame() until it returns false, and that false is the normal "no more output right now" case.

AV_BUFFERSRC_FLAG_KEEP_REF keeps the pushed frame valid after the call, which is what the example needs because it reuses the decoder frame afterwards. Drop it when the frame is not used again.

An EAGAIN from the sink is not an error: the example checks ec.value() != -EAGAIN explicitly, because the sink reports it as a value while the graph is starving for input.

Timestamps

Filtered frames carry their own timestamps, derived from the input time base. When the encoder uses a different time base, set it on each filtered frame before encoding — exactly as with plain decoded frames.

Text and audio

The same graph works for audio with buffer/abuffersink, addAudioSamples() and getAudioSamples(). Text filters (for example subtitles=) are configured through the chain string and do not need a frame loop.

Next