Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-02T20:24:18.260Z
Answers.Text: umm looks very promising! And at the same time not easy though. Thanks!
Answers.UserDisplayName: angel_30
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-02T20:25:01.673Z
Answers.Text: Yeah, Filters are pretty complicated.
Answers.UserDisplayName: SlimSCSI
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-12T13:58:51.490Z
Answers.Text: Is filter API sequential, or multi-threaded? I mean, when calling filters, do they run sequential, not on a separate thread?
Answers.UserDisplayName: angel_30
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-02T20:18:44.203Z
Answers.LastActivityDate: 2019-05-02T20:18:44.203Z
Answers.UserDisplayName: SlimSCSI
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-02T16:11:06.340Z
Comments.Text: 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.
Comments.UserDisplayName: SlimSCSI
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-02T20:04:55.113Z
Comments.Text: I am familiar with C, but not FFmpeg C API. Knowing how to call this filter from C API would help.
Comments.UserDisplayName: angel_30
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-03T18:09:42.157Z
Comments.Text: Also see `doc/examples` in FFmpeg source code, such as `filtering_video.c`.
Comments.UserDisplayName: llogan
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-05-03T20:35:53.263Z
Comments.Text: Sure. I hope the answer below can be detailed enough to implement. I'm not super C guy.
Comments.UserDisplayName: angel_30
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-12T13:58:34.337Z
Comments.Text: @llogan Is filter API sequential, or multi-threaded? I mean, when calling filters, do they run sequential, not on a separate thread?
Comments.UserDisplayName: angel_30
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-19T17:24:54.677Z
Comments.Text: @TinaJ I'm not sure, but you can view the output of `ffmpeg -filters` to see which filters support slice threading.
Comments.UserDisplayName: llogan
Tags: <video><ffmpeg><encoding><video-capture><decoding>
Title: Call FFmpeg filter from source code (C API)
UserDisplayName: angel_30
ThreadText: # 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?  
  
  ---
  
  