<p>You can't just call a filter, You must instantiate a filter graph.</p>

<p>First you need to create a <code>buffer</code> and <code>buffersink</code> via:</p>

<pre><code>AVFilterContext* source = 0;
AVFilterContext* sink = 0;
AVFilterGraph *graph = avfilter_graph_alloc()
avfilter_graph_create_filter(&amp;source, avfilter_get_by_name("buffer"), nullptr, args, nullptr, graph);


AVBufferSinkParams* buffersink_params = av_buffersink_params_alloc();
buffersink_params-&gt;pixel_fmts = /*Your pixel format HERE*/;

avfilter_graph_create_filter(&amp;sink, avfilter_get_by_name("buffersink"), nullptr, nullptr, buffersink_params, graph);

</code></pre>

<p>You can then create your filter via something like this</p>

<pre><code>snprintf(args, sizeof(args), "n=-60dB:d=2");
avfilter_graph_create_filter(&amp;freezedetect, avfilter_get_by_name("freezedetect"), nullptr, args, nullptr, graph);
</code></pre>

<p>Then link it all together:</p>

<pre><code>avfilter_link(source, 0, freezedetect, 0);
avfilter_link(freezedetect, 0, sink, 0);
avfilter_graph_config(graph, 0);
</code></pre>

<p>Finally you can send frames to the filter via</p>

<pre><code>av_buffersrc_write_frame(source, frame);
</code></pre>

<p>And get the result with </p>

<pre><code> av_buffersink_get_frame(sink, frame);
</code></pre>

<p>Don't forget to free it all at the end</p>

CC BY-SA 4.0
2019-05-02T20:24:18.260Z
umm looks very promising! And at the same time not easy though. Thanks!
angel_30
CC BY-SA 4.0
2019-05-02T20:25:01.673Z
Yeah, Filters are pretty complicated.
SlimSCSI
CC BY-SA 4.0
2019-08-12T13:58:51.490Z
Is filter API sequential, or multi-threaded? I mean, when calling filters, do they run sequential, not on a separate thread?
angel_30
CC BY-SA 4.0
2019-05-02T20:18:44.203Z
2019-05-02T20:18:44.203Z
SlimSCSI
<p>I'm trying to find out how to call a <code>FFmpeg</code> filter (<code>freezedetect</code>) from within FFmpeg's source code. I see the filter's file is under <code>libavfilter/vf_freezedetect.c</code>, but I don't know how to use that filter inside the source code. How can I do that? And what I need to pass to the function?</p>

<p>My FFmpeg command's high-level API is this:</p>

<pre><code>ffmpeg -i freeze.mp4 -vf "freezedetect=n=-60dB:d=2" -map 0:v:0 -f null -
</code></pre>

<p>And what I would need to get the resulting outputs directly after calling a function given its input arguments:</p>

<pre><code>lavfi.freezedetect.freeze_start: 5.005
lavfi.freezedetect.freeze_duration: 2.03537
lavfi.freezedetect.freeze_end: 7.04037
lavfi.freezedetect.freeze_start: 13.0464
lavfi.freezedetect.freeze_duration: 3.03637
lavfi.freezedetect.freeze_end: 16.0827
</code></pre>

CC BY-SA 4.0
2019-05-02T16:11:06.340Z
Just for some context, are you already familiar with C programming and the ffmpeg APIs? If not I suggest you start with the dranger.com tutorial on reading and parsing first. If you are comfortable with that, you can ask this question on stackoverflow and I can help with creating pads and filters.
SlimSCSI
CC BY-SA 4.0
2019-05-02T20:04:55.113Z
I am familiar with C, but not FFmpeg C API. Knowing how to call this filter from C API would help.
angel_30
CC BY-SA 4.0
2019-05-03T18:09:42.157Z
Also see `doc/examples` in FFmpeg source code, such as `filtering_video.c`.
llogan
CC BY-SA 4.0
2019-05-03T20:35:53.263Z
Sure. I hope the answer below can be detailed enough to implement. I'm not super C guy.
angel_30
CC BY-SA 4.0
2019-08-12T13:58:34.337Z
@llogan Is filter API sequential, or multi-threaded? I mean, when calling filters, do they run sequential, not on a separate thread?
angel_30
CC BY-SA 4.0
2019-08-19T17:24:54.677Z
@TinaJ I'm not sure, but you can view the output of `ffmpeg -filters` to see which filters support slice threading.
llogan
<video><ffmpeg><encoding><video-capture><decoding>
Call FFmpeg filter from source code (C API)
angel_30
# Call FFmpeg filter from source code (C API)

(Asked by: username_0 on 2019-05-01)

I'm trying to find out how to call a FFmpeg filter (freezedetect) from within FFmpeg's source code. I see the filter's file is under libavfilter/vf_freezedetect.c, but I don't know how to use that filter inside the source code. How can I do that? And what I need to pass to the function?
My FFmpeg command's high-level API is this:
ffmpeg -i freeze.mp4 -vf "freezedetect=n=-60dB:d=2" -map 0:v:0 -f null -

And what I would need to get the resulting outputs directly after calling a function given its input arguments:
lavfi.freezedetect.freeze_start: 5.005
lavfi.freezedetect.freeze_duration: 2.03537
lavfi.freezedetect.freeze_end: 7.04037
lavfi.freezedetect.freeze_start: 13.0464
lavfi.freezedetect.freeze_duration: 3.03637
lavfi.freezedetect.freeze_end: 16.0827

**Tags:** video, ffmpeg, encoding, video-capture, decoding

> **Comments:**
> **username_1**: Just for some context, are you already familiar with C programming and the ffmpeg APIs? If not I suggest you start with the dranger.com tutorial on reading and parsing first. If you are comfortable with that, you can ask this question on stackoverflow and I can help with creating pads and filters.  
> **username_0**: I am familiar with C, but not FFmpeg C API. Knowing how to call this filter from C API would help.  
> **username_2**: Also see `doc/examples` in FFmpeg source code, such as `filtering_video.c`.  
> **username_0**: Sure. I hope the answer below can be detailed enough to implement. I'm not super C guy.  
> **username_0**: @username_2 Is filter API sequential, or multi-threaded? I mean, when calling filters, do they run sequential, not on a separate thread?  
> **username_2**: @TinaJ I'm not sure, but you can view the output of `ffmpeg -filters` to see which filters support slice threading.  

---

## 1 answers

### Answer by username_1 on 2019-05-02. Score: 5 | Accepted answer

You can't just call a filter, You must instantiate a filter graph.
First you need to create a buffer and buffersink via:
AVFilterContext* source = 0;
AVFilterContext* sink = 0;
AVFilterGraph *graph = avfilter_graph_alloc()
avfilter_graph_create_filter(&source, avfilter_get_by_name("buffer"), nullptr, args, nullptr, graph);


AVBufferSinkParams* buffersink_params = av_buffersink_params_alloc();
buffersink_params->pixel_fmts = /*Your pixel format HERE*/;

avfilter_graph_create_filter(&sink, avfilter_get_by_name("buffersink"), nullptr, nullptr, buffersink_params, graph);


You can then create your filter via something like this
snprintf(args, sizeof(args), "n=-60dB:d=2");
avfilter_graph_create_filter(&freezedetect, avfilter_get_by_name("freezedetect"), nullptr, args, nullptr, graph);

Then link it all together:
avfilter_link(source, 0, freezedetect, 0);
avfilter_link(freezedetect, 0, sink, 0);
avfilter_graph_config(graph, 0);

Finally you can send frames to the filter via
av_buffersrc_write_frame(source, frame);

And get the result with 
 av_buffersink_get_frame(sink, frame);

Don't forget to free it all at the end

> **Comments:**
> **username_0**: umm looks very promising! And at the same time not easy though. Thanks!  
> **username_1**: Yeah, Filters are pretty complicated.  
> **username_0**: Is filter API sequential, or multi-threaded? I mean, when calling filters, do they run sequential, not on a separate thread?  

---

