FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfilter.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 653 837 78.0%
Functions: 58 67 86.6%
Branches: 347 496 70.0%

Line Branch Exec Source
1 /*
2 * filter layer
3 * Copyright (c) 2007 Bobby Bingham
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/avassert.h"
23 #include "libavutil/avstring.h"
24 #include "libavutil/bprint.h"
25 #include "libavutil/buffer.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/common.h"
28 #include "libavutil/eval.h"
29 #include "libavutil/frame.h"
30 #include "libavutil/hwcontext.h"
31 #include "libavutil/internal.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/pixdesc.h"
35 #include "libavutil/rational.h"
36 #include "libavutil/samplefmt.h"
37
38 #include "audio.h"
39 #include "avfilter.h"
40 #include "avfilter_internal.h"
41 #include "filters.h"
42 #include "formats.h"
43 #include "framequeue.h"
44 #include "framepool.h"
45 #include "video.h"
46
47 1422399 static void tlog_ref(void *ctx, AVFrame *ref, int end)
48 {
49 #ifdef TRACE
50 ff_tlog(ctx,
51 "ref[%p buf:%p data:%p linesize[%d, %d, %d, %d] pts:%"PRId64,
52 ref, ref->buf, ref->data[0],
53 ref->linesize[0], ref->linesize[1], ref->linesize[2], ref->linesize[3],
54 ref->pts);
55
56 if (ref->width) {
57 ff_tlog(ctx, " a:%d/%d s:%dx%d i:%c iskey:%d type:%c",
58 ref->sample_aspect_ratio.num, ref->sample_aspect_ratio.den,
59 ref->width, ref->height,
60 !(ref->flags & AV_FRAME_FLAG_INTERLACED) ? 'P' : /* Progressive */
61 (ref->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) ? 'T' : 'B', /* Top / Bottom */
62 !!(ref->flags & AV_FRAME_FLAG_KEY),
63 av_get_picture_type_char(ref->pict_type));
64 }
65 if (ref->nb_samples) {
66 AVBPrint bprint;
67
68 av_bprint_init(&bprint, 1, AV_BPRINT_SIZE_UNLIMITED);
69 av_channel_layout_describe_bprint(&ref->ch_layout, &bprint);
70 ff_tlog(ctx, " cl:%s n:%d r:%d",
71 bprint.str,
72 ref->nb_samples,
73 ref->sample_rate);
74 av_bprint_finalize(&bprint, NULL);
75 }
76
77 ff_tlog(ctx, "]%s", end ? "\n" : "");
78 #endif
79 1422399 }
80
81 static void command_queue_pop(AVFilterContext *filter)
82 {
83 FFFilterContext *ctxi = fffilterctx(filter);
84 AVFilterCommand *c = ctxi->command_queue;
85 av_freep(&c->arg);
86 av_freep(&c->command);
87 ctxi->command_queue = c->next;
88 av_free(c);
89 }
90
91 /**
92 * Append a new pad.
93 *
94 * @param count Pointer to the number of pads in the list
95 * @param pads Pointer to the pointer to the beginning of the list of pads
96 * @param links Pointer to the pointer to the beginning of the list of links
97 * @param newpad The new pad to add. A copy is made when adding.
98 * @return >= 0 in case of success, a negative AVERROR code on error
99 */
100 501 static int append_pad(unsigned *count, AVFilterPad **pads,
101 AVFilterLink ***links, AVFilterPad *newpad)
102 {
103 AVFilterLink **newlinks;
104 AVFilterPad *newpads;
105 501 unsigned idx = *count;
106
107 501 newpads = av_realloc_array(*pads, idx + 1, sizeof(*newpads));
108 501 newlinks = av_realloc_array(*links, idx + 1, sizeof(*newlinks));
109
1/2
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
501 if (newpads)
110 501 *pads = newpads;
111
1/2
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
501 if (newlinks)
112 501 *links = newlinks;
113
2/4
✓ Branch 0 taken 501 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 501 times.
501 if (!newpads || !newlinks) {
114 if (newpad->flags & AVFILTERPAD_FLAG_FREE_NAME)
115 av_freep(&newpad->name);
116 return AVERROR(ENOMEM);
117 }
118
119 501 memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
120 501 (*links)[idx] = NULL;
121
122 501 (*count)++;
123
124 501 return 0;
125 }
126
127 229 int ff_append_inpad(AVFilterContext *f, AVFilterPad *p)
128 {
129 229 return append_pad(&f->nb_inputs, &f->input_pads, &f->inputs, p);
130 }
131
132 210 int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
133 {
134 210 p->flags |= AVFILTERPAD_FLAG_FREE_NAME;
135 210 return ff_append_inpad(f, p);
136 }
137
138 272 int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
139 {
140 272 return append_pad(&f->nb_outputs, &f->output_pads, &f->outputs, p);
141 }
142
143 131 int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
144 {
145 131 p->flags |= AVFILTERPAD_FLAG_FREE_NAME;
146 131 return ff_append_outpad(f, p);
147 }
148
149 42702 int avfilter_link(AVFilterContext *src, unsigned srcpad,
150 AVFilterContext *dst, unsigned dstpad)
151 {
152 FilterLinkInternal *li;
153 AVFilterLink *link;
154
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42702 times.
42702 av_assert0(src->graph);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42702 times.
42702 av_assert0(dst->graph);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42702 times.
42702 av_assert0(src->graph == dst->graph);
158
159
2/4
✓ Branch 0 taken 42702 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42702 times.
✗ Branch 3 not taken.
42702 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
160
2/4
✓ Branch 0 taken 42702 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42702 times.
42702 src->outputs[srcpad] || dst->inputs[dstpad])
161 return AVERROR(EINVAL);
162
163
1/2
✓ Branch 1 taken 42702 times.
✗ Branch 2 not taken.
42702 if (!(fffilterctx(src)->state_flags & AV_CLASS_STATE_INITIALIZED) ||
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 42702 times.
42702 !(fffilterctx(dst)->state_flags & AV_CLASS_STATE_INITIALIZED)) {
165 av_log(src, AV_LOG_ERROR, "Filters must be initialized before linking.\n");
166 return AVERROR(EINVAL);
167 }
168
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42702 times.
42702 if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
170 av_log(src, AV_LOG_ERROR,
171 "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
172 src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
173 dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
174 return AVERROR(EINVAL);
175 }
176
177 42702 li = av_mallocz(sizeof(*li));
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42702 times.
42702 if (!li)
179 return AVERROR(ENOMEM);
180 42702 link = &li->l.pub;
181
182 42702 src->outputs[srcpad] = dst->inputs[dstpad] = link;
183
184 42702 link->src = src;
185 42702 link->dst = dst;
186 42702 link->srcpad = &src->output_pads[srcpad];
187 42702 link->dstpad = &dst->input_pads[dstpad];
188 42702 link->type = src->output_pads[srcpad].type;
189 42702 li->l.graph = src->graph;
190 av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
191 42702 link->format = -1;
192 42702 link->colorspace = AVCOL_SPC_UNSPECIFIED;
193 42702 ff_framequeue_init(&li->fifo, &fffiltergraph(src->graph)->frame_queues);
194
195 42702 return 0;
196 }
197
198 42834 static void link_free(AVFilterLink **link)
199 {
200 FilterLinkInternal *li;
201
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42834 times.
42834 if (!*link)
203 return;
204 42834 li = ff_link_internal(*link);
205
206 42834 ff_framequeue_free(&li->fifo);
207 42834 ff_frame_pool_uninit(&li->frame_pool);
208 42834 av_channel_layout_uninit(&(*link)->ch_layout);
209 42834 av_frame_side_data_free(&(*link)->side_data, &(*link)->nb_side_data);
210
211 42834 av_buffer_unref(&li->l.hw_frames_ctx);
212
213 42834 av_freep(link);
214 }
215
216 #if FF_API_LINK_PUBLIC
217 void avfilter_link_free(AVFilterLink **link)
218 {
219 link_free(link);
220 }
221 int avfilter_config_links(AVFilterContext *filter)
222 {
223 return ff_filter_config_links(filter);
224 }
225 #endif
226
227 1436162 static void update_link_current_pts(FilterLinkInternal *li, int64_t pts)
228 {
229 1436162 AVFilterLink *const link = &li->l.pub;
230
231
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1436157 times.
1436162 if (pts == AV_NOPTS_VALUE)
232 5 return;
233 1436157 li->l.current_pts = pts;
234 1436157 li->l.current_pts_us = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
235 /* TODO use duration */
236
3/4
✓ Branch 0 taken 1436157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 399594 times.
✓ Branch 3 taken 1036563 times.
1436157 if (li->l.graph && li->age_index >= 0)
237 399594 ff_avfilter_graph_update_heap(li->l.graph, li);
238 }
239
240 3536883 void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
241 {
242 3536883 FFFilterContext *ctxi = fffilterctx(filter);
243 3536883 ctxi->ready = FFMAX(ctxi->ready, priority);
244 3536883 }
245
246 /**
247 * Clear frame_blocked_in on all outputs.
248 * This is necessary whenever something changes on input.
249 */
250 2120441 static void filter_unblock(AVFilterContext *filter)
251 {
252 unsigned i;
253
254
2/2
✓ Branch 0 taken 1722731 times.
✓ Branch 1 taken 2120441 times.
3843172 for (i = 0; i < filter->nb_outputs; i++) {
255 1722731 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
256 1722731 li->frame_blocked_in = 0;
257 }
258 2120441 }
259
260
261 17129 void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
262 {
263 17129 FilterLinkInternal * const li = ff_link_internal(link);
264
265
2/2
✓ Branch 0 taken 3809 times.
✓ Branch 1 taken 13320 times.
17129 if (li->status_in == status)
266 3809 return;
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13320 times.
13320 av_assert0(!li->status_in);
268 13320 li->status_in = status;
269 13320 li->status_in_pts = pts;
270 13320 li->frame_wanted_out = 0;
271 13320 li->frame_blocked_in = 0;
272 13320 filter_unblock(link->dst);
273 13320 ff_filter_set_ready(link->dst, 200);
274 }
275
276 /**
277 * Set the status field of a link from the destination filter.
278 * The pts should probably be left unset (AV_NOPTS_VALUE).
279 */
280 7986 static void link_set_out_status(AVFilterLink *link, int status, int64_t pts)
281 {
282 7986 FilterLinkInternal * const li = ff_link_internal(link);
283
284
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7986 times.
7986 av_assert0(!li->frame_wanted_out);
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7986 times.
7986 av_assert0(!li->status_out);
286 7986 li->status_out = status;
287
2/2
✓ Branch 0 taken 5883 times.
✓ Branch 1 taken 2103 times.
7986 if (pts != AV_NOPTS_VALUE)
288 5883 update_link_current_pts(li, pts);
289 7986 filter_unblock(link->dst);
290 7986 ff_filter_set_ready(link->src, 200);
291 7986 }
292
293 1246 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
294 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
295 {
296 int ret;
297 1246 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
298
299 1246 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
300 "between the filter '%s' and the filter '%s'\n",
301 1246 filt->name, link->src->name, link->dst->name);
302
303 1246 link->dst->inputs[dstpad_idx] = NULL;
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1246 times.
1246 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
305 /* failed to link output filter to new filter */
306 link->dst->inputs[dstpad_idx] = link;
307 return ret;
308 }
309
310 /* re-hookup the link to the new destination filter we inserted */
311 1246 link->dst = filt;
312 1246 link->dstpad = &filt->input_pads[filt_srcpad_idx];
313 1246 filt->inputs[filt_srcpad_idx] = link;
314
315 /* if any information on supported media formats already exists on the
316 * link, we need to preserve that */
317
1/2
✓ Branch 0 taken 1246 times.
✗ Branch 1 not taken.
1246 if (link->outcfg.formats)
318 1246 ff_formats_changeref(&link->outcfg.formats,
319 1246 &filt->outputs[filt_dstpad_idx]->outcfg.formats);
320
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 630 times.
1246 if (link->outcfg.color_spaces)
321 616 ff_formats_changeref(&link->outcfg.color_spaces,
322 616 &filt->outputs[filt_dstpad_idx]->outcfg.color_spaces);
323
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 630 times.
1246 if (link->outcfg.color_ranges)
324 616 ff_formats_changeref(&link->outcfg.color_ranges,
325 616 &filt->outputs[filt_dstpad_idx]->outcfg.color_ranges);
326
2/2
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 616 times.
1246 if (link->outcfg.samplerates)
327 630 ff_formats_changeref(&link->outcfg.samplerates,
328 630 &filt->outputs[filt_dstpad_idx]->outcfg.samplerates);
329
2/2
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 616 times.
1246 if (link->outcfg.channel_layouts)
330 630 ff_channel_layouts_changeref(&link->outcfg.channel_layouts,
331 630 &filt->outputs[filt_dstpad_idx]->outcfg.channel_layouts);
332
333 1246 return 0;
334 }
335
336 41399 int ff_filter_config_links(AVFilterContext *filter)
337 {
338 int (*config_link)(AVFilterLink *);
339 unsigned i;
340 int ret;
341
342
2/2
✓ Branch 0 taken 33742 times.
✓ Branch 1 taken 41399 times.
75141 for (i = 0; i < filter->nb_inputs; i ++) {
343 33742 AVFilterLink *link = filter->inputs[i];
344 AVFilterLink *inlink;
345 33742 FilterLinkInternal *li = ff_link_internal(link);
346 FilterLinkInternal *li_in;
347
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33742 times.
33742 if (!link) continue;
349
2/4
✓ Branch 0 taken 33742 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 33742 times.
33742 if (!link->src || !link->dst) {
350 av_log(filter, AV_LOG_ERROR,
351 "Not all input and output are properly linked (%d).\n", i);
352 return AVERROR(EINVAL);
353 }
354
355
2/2
✓ Branch 0 taken 25909 times.
✓ Branch 1 taken 7833 times.
33742 inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
356
2/2
✓ Branch 0 taken 25909 times.
✓ Branch 1 taken 7833 times.
33742 li_in = inlink ? ff_link_internal(inlink) : NULL;
357 33742 li->l.current_pts =
358 33742 li->l.current_pts_us = AV_NOPTS_VALUE;
359
360
2/4
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33637 times.
✗ Branch 3 not taken.
33742 switch (li->init_state) {
361 105 case AVLINK_INIT:
362 105 continue;
363 case AVLINK_STARTINIT:
364 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
365 return 0;
366 33637 case AVLINK_UNINIT:
367 33637 li->init_state = AVLINK_STARTINIT;
368
369
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33637 times.
33637 if ((ret = ff_filter_config_links(link->src)) < 0)
370 return ret;
371
372
2/2
✓ Branch 0 taken 17526 times.
✓ Branch 1 taken 16111 times.
33637 if (!(config_link = link->srcpad->config_props)) {
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17526 times.
17526 if (link->src->nb_inputs != 1) {
374 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
375 "with more than one input "
376 "must set config_props() "
377 "callbacks on all outputs\n");
378 return AVERROR(EINVAL);
379 }
380 }
381
382 /* Copy side data before link->srcpad->config_props() is called, so the filter
383 * may remove it for the next filter in the chain */
384
5/6
✓ Branch 0 taken 25833 times.
✓ Branch 1 taken 7804 times.
✓ Branch 2 taken 239 times.
✓ Branch 3 taken 25594 times.
✓ Branch 4 taken 239 times.
✗ Branch 5 not taken.
33637 if (inlink && inlink->nb_side_data && !link->nb_side_data) {
385
2/2
✓ Branch 0 taken 246 times.
✓ Branch 1 taken 239 times.
485 for (int j = 0; j < inlink->nb_side_data; j++) {
386 246 ret = av_frame_side_data_clone(&link->side_data, &link->nb_side_data,
387 246 inlink->side_data[j], 0);
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 246 times.
246 if (ret < 0) {
389 av_frame_side_data_free(&link->side_data, &link->nb_side_data);
390 return ret;
391 }
392 }
393 }
394
395
3/4
✓ Branch 0 taken 16111 times.
✓ Branch 1 taken 17526 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16111 times.
33637 if (config_link && (ret = config_link(link)) < 0) {
396 av_log(link->src, AV_LOG_ERROR,
397 "Failed to configure output pad on %s\n",
398 link->src->name);
399 return ret;
400 }
401
402
2/3
✓ Branch 0 taken 27742 times.
✓ Branch 1 taken 5895 times.
✗ Branch 2 not taken.
33637 switch (link->type) {
403 27742 case AVMEDIA_TYPE_VIDEO:
404
3/4
✓ Branch 0 taken 21045 times.
✓ Branch 1 taken 6697 times.
✓ Branch 2 taken 21045 times.
✗ Branch 3 not taken.
27742 if (!link->time_base.num && !link->time_base.den)
405
1/2
✓ Branch 0 taken 21045 times.
✗ Branch 1 not taken.
21045 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
406
407
4/4
✓ Branch 0 taken 24350 times.
✓ Branch 1 taken 3392 times.
✓ Branch 2 taken 15055 times.
✓ Branch 3 taken 9295 times.
27742 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
408 15055 link->sample_aspect_ratio = inlink ?
409
2/2
✓ Branch 0 taken 15041 times.
✓ Branch 1 taken 14 times.
15055 inlink->sample_aspect_ratio : (AVRational){1,1};
410
411
2/2
✓ Branch 0 taken 21298 times.
✓ Branch 1 taken 6444 times.
27742 if (inlink) {
412
3/4
✓ Branch 0 taken 21088 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 21088 times.
✗ Branch 3 not taken.
21298 if (!li->l.frame_rate.num && !li->l.frame_rate.den)
413 21088 li->l.frame_rate = li_in->l.frame_rate;
414
2/2
✓ Branch 0 taken 14574 times.
✓ Branch 1 taken 6724 times.
21298 if (!link->w)
415 14574 link->w = inlink->w;
416
2/2
✓ Branch 0 taken 14574 times.
✓ Branch 1 taken 6724 times.
21298 if (!link->h)
417 14574 link->h = inlink->h;
418
2/4
✓ Branch 0 taken 6444 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6444 times.
6444 } else if (!link->w || !link->h) {
419 av_log(link->src, AV_LOG_ERROR,
420 "Video source filters must set their output link's "
421 "width and height\n");
422 return AVERROR(EINVAL);
423 }
424 27742 break;
425
426 5895 case AVMEDIA_TYPE_AUDIO:
427
2/2
✓ Branch 0 taken 4535 times.
✓ Branch 1 taken 1360 times.
5895 if (inlink) {
428
3/4
✓ Branch 0 taken 3040 times.
✓ Branch 1 taken 1495 times.
✓ Branch 2 taken 3040 times.
✗ Branch 3 not taken.
4535 if (!link->time_base.num && !link->time_base.den)
429 3040 link->time_base = inlink->time_base;
430 }
431
432
3/4
✓ Branch 0 taken 81 times.
✓ Branch 1 taken 5814 times.
✓ Branch 2 taken 81 times.
✗ Branch 3 not taken.
5895 if (!link->time_base.num && !link->time_base.den)
433 81 link->time_base = (AVRational) {1, link->sample_rate};
434 }
435
436
2/2
✓ Branch 0 taken 25833 times.
✓ Branch 1 taken 7804 times.
33637 if (link->src->nb_inputs &&
437
1/2
✓ Branch 1 taken 25833 times.
✗ Branch 2 not taken.
25833 !(fffilter(link->src->filter)->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
438 25833 FilterLink *l0 = ff_filter_link(link->src->inputs[0]);
439
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25833 times.
25833 av_assert0(!li->l.hw_frames_ctx &&
441 "should not be set by non-hwframe-aware filter");
442
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25833 times.
25833 if (l0->hw_frames_ctx) {
444 li->l.hw_frames_ctx = av_buffer_ref(l0->hw_frames_ctx);
445 if (!li->l.hw_frames_ctx)
446 return AVERROR(ENOMEM);
447 }
448 }
449
450
2/2
✓ Branch 0 taken 4462 times.
✓ Branch 1 taken 29175 times.
33637 if ((config_link = link->dstpad->config_props))
451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4462 times.
4462 if ((ret = config_link(link)) < 0) {
452 av_log(link->dst, AV_LOG_ERROR,
453 "Failed to configure input pad on %s\n",
454 link->dst->name);
455 return ret;
456 }
457
458 33637 li->init_state = AVLINK_INIT;
459 }
460 }
461
462 41399 return 0;
463 }
464
465 #ifdef TRACE
466 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
467 {
468 if (link->type == AVMEDIA_TYPE_VIDEO) {
469 ff_tlog(ctx,
470 "link[%p s:%dx%d fmt:%s %s->%s]%s",
471 link, link->w, link->h,
472 av_get_pix_fmt_name(link->format),
473 link->src ? link->src->filter->name : "",
474 link->dst ? link->dst->filter->name : "",
475 end ? "\n" : "");
476 } else {
477 char buf[128];
478 av_channel_layout_describe(&link->ch_layout, buf, sizeof(buf));
479
480 ff_tlog(ctx,
481 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
482 link, (int)link->sample_rate, buf,
483 av_get_sample_fmt_name(link->format),
484 link->src ? link->src->filter->name : "",
485 link->dst ? link->dst->filter->name : "",
486 end ? "\n" : "");
487 }
488 }
489 #endif
490
491 672417 int ff_request_frame(AVFilterLink *link)
492 {
493 672417 FilterLinkInternal * const li = ff_link_internal(link);
494
495 FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
496
497 av_assert1(!fffilter(link->dst->filter)->activate);
498
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 672408 times.
672417 if (li->status_out)
499 9 return li->status_out;
500
2/2
✓ Branch 0 taken 5884 times.
✓ Branch 1 taken 666524 times.
672408 if (li->status_in) {
501
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5884 times.
5884 if (ff_framequeue_queued_frames(&li->fifo)) {
502 av_assert1(!li->frame_wanted_out);
503 av_assert1(fffilterctx(link->dst)->ready >= 300);
504 return 0;
505 } else {
506 /* Acknowledge status change. Filters using ff_request_frame() will
507 handle the change automatically. Filters can also check the
508 status directly but none do yet. */
509 5884 link_set_out_status(link, li->status_in, li->status_in_pts);
510 5884 return li->status_out;
511 }
512 }
513 666524 li->frame_wanted_out = 1;
514 666524 ff_filter_set_ready(link->src, 100);
515 666524 return 0;
516 }
517
518 5885 static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base)
519 {
520 unsigned i;
521 5885 int64_t r = INT64_MAX;
522
523
2/2
✓ Branch 0 taken 5884 times.
✓ Branch 1 taken 5885 times.
11769 for (i = 0; i < ctx->nb_inputs; i++) {
524 5884 FilterLinkInternal * const li = ff_link_internal(ctx->inputs[i]);
525
1/2
✓ Branch 0 taken 5884 times.
✗ Branch 1 not taken.
5884 if (li->status_out == status)
526 5884 r = FFMIN(r, av_rescale_q(li->l.current_pts, ctx->inputs[i]->time_base, link_time_base));
527 }
528
2/2
✓ Branch 0 taken 5884 times.
✓ Branch 1 taken 1 times.
5885 if (r < INT64_MAX)
529 5884 return r;
530 1 av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (i = 0; i < ctx->nb_inputs; i++) {
532 FilterLinkInternal * const li = ff_link_internal(ctx->inputs[i]);
533 r = FFMIN(r, av_rescale_q(li->status_in_pts, ctx->inputs[i]->time_base, link_time_base));
534 }
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (r < INT64_MAX)
536 return r;
537 1 return AV_NOPTS_VALUE;
538 }
539
540 672760 static int request_frame_to_filter(AVFilterLink *link)
541 {
542 672760 FilterLinkInternal * const li = ff_link_internal(link);
543 672760 int ret = -1;
544
545 FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);
546 /* Assume the filter is blocked, let the method clear it if not */
547 672760 li->frame_blocked_in = 1;
548
2/2
✓ Branch 0 taken 2864 times.
✓ Branch 1 taken 669896 times.
672760 if (link->srcpad->request_frame)
549 2864 ret = link->srcpad->request_frame(link);
550
1/2
✓ Branch 0 taken 669896 times.
✗ Branch 1 not taken.
669896 else if (link->src->inputs[0])
551 669896 ret = ff_request_frame(link->src->inputs[0]);
552
2/2
✓ Branch 0 taken 5885 times.
✓ Branch 1 taken 666875 times.
672760 if (ret < 0) {
553
2/4
✓ Branch 0 taken 5885 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5885 times.
✗ Branch 3 not taken.
5885 if (ret != AVERROR(EAGAIN) && ret != li->status_in)
554 5885 ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));
555
1/2
✓ Branch 0 taken 5885 times.
✗ Branch 1 not taken.
5885 if (ret == AVERROR_EOF)
556 5885 ret = 0;
557 }
558 672760 return ret;
559 }
560
561 static const char *const var_names[] = {
562 "t",
563 "n",
564 #if FF_API_FRAME_PKT
565 "pos",
566 #endif
567 "w",
568 "h",
569 NULL
570 };
571
572 enum {
573 VAR_T,
574 VAR_N,
575 #if FF_API_FRAME_PKT
576 VAR_POS,
577 #endif
578 VAR_W,
579 VAR_H,
580 VAR_VARS_NB
581 };
582
583 6 static int set_enable_expr(FFFilterContext *ctxi, const char *expr)
584 {
585 6 AVFilterContext *ctx = &ctxi->p;
586 int ret;
587 char *expr_dup;
588 6 AVExpr *old = ctxi->enable;
589
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
591 av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
592 "with filter '%s'\n", ctx->filter->name);
593 return AVERROR_PATCHWELCOME;
594 }
595
596 6 expr_dup = av_strdup(expr);
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!expr_dup)
598 return AVERROR(ENOMEM);
599
600
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!ctxi->var_values) {
601 6 ctxi->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctxi->var_values));
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ctxi->var_values) {
603 av_free(expr_dup);
604 return AVERROR(ENOMEM);
605 }
606 }
607
608 6 ret = av_expr_parse(&ctxi->enable, expr_dup, var_names,
609 NULL, NULL, NULL, NULL, 0, ctx->priv);
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
611 av_log(ctx->priv, AV_LOG_ERROR,
612 "Error when evaluating the expression '%s' for enable\n",
613 expr_dup);
614 av_free(expr_dup);
615 return ret;
616 }
617
618 6 av_expr_free(old);
619 6 av_free(ctx->enable_str);
620 6 ctx->enable_str = expr_dup;
621 6 return 0;
622 }
623
624 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
625 {
626 if(!strcmp(cmd, "ping")){
627 char local_res[256] = {0};
628
629 if (!res) {
630 res = local_res;
631 res_len = sizeof(local_res);
632 }
633 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
634 if (res == local_res)
635 av_log(filter, AV_LOG_INFO, "%s", res);
636 return 0;
637 }else if(!strcmp(cmd, "enable")) {
638 return set_enable_expr(fffilterctx(filter), arg);
639 }else if (fffilter(filter->filter)->process_command) {
640 return fffilter(filter->filter)->process_command(filter, cmd, arg, res, res_len, flags);
641 }
642 return AVERROR(ENOSYS);
643 }
644
645 14139 unsigned avfilter_filter_pad_count(const AVFilter *filter, int is_output)
646 {
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14139 times.
14139 return is_output ? fffilter(filter)->nb_outputs : fffilter(filter)->nb_inputs;
648 }
649
650 555 static const char *default_filter_name(void *filter_ctx)
651 {
652 555 AVFilterContext *ctx = filter_ctx;
653
1/2
✓ Branch 0 taken 555 times.
✗ Branch 1 not taken.
555 return ctx->name ? ctx->name : ctx->filter->name;
654 }
655
656 50292 static void *filter_child_next(void *obj, void *prev)
657 {
658 50292 AVFilterContext *ctx = obj;
659
5/8
✓ Branch 0 taken 50286 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 50286 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50286 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 50286 times.
✗ Branch 7 not taken.
50292 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
660 50286 return ctx->priv;
661 6 return NULL;
662 }
663
664 static const AVClass *filter_child_class_iterate(void **iter)
665 {
666 const AVFilter *f;
667
668 while ((f = av_filter_iterate(iter)))
669 if (f->priv_class)
670 return f->priv_class;
671
672 return NULL;
673 }
674
675 #define OFFSET(x) offsetof(AVFilterContext, x)
676 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
677 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
678 static const AVOption avfilter_options[] = {
679 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
680 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, .unit = "thread_type" },
681 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
682 { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = TFLAGS },
683 { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
684 { .i64 = 0 }, 0, INT_MAX, FLAGS, .unit = "threads" },
685 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = FLAGS, .unit = "threads"},
686 { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
687 OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
688 { NULL },
689 };
690
691 static const AVClass avfilter_class = {
692 .class_name = "AVFilter",
693 .item_name = default_filter_name,
694 .version = LIBAVUTIL_VERSION_INT,
695 .category = AV_CLASS_CATEGORY_FILTER,
696 .child_next = filter_child_next,
697 .child_class_iterate = filter_child_class_iterate,
698 .option = avfilter_options,
699 .state_flags_offset = offsetof(FFFilterContext, state_flags),
700 };
701
702 728 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
703 int *ret, int nb_jobs)
704 {
705 int i;
706
707
2/2
✓ Branch 0 taken 728 times.
✓ Branch 1 taken 728 times.
1456 for (i = 0; i < nb_jobs; i++) {
708 728 int r = func(ctx, arg, i, nb_jobs);
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
728 if (ret)
710 ret[i] = r;
711 }
712 728 return 0;
713 }
714
715 58011 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
716 {
717 FFFilterContext *ctx;
718 AVFilterContext *ret;
719 58011 const FFFilter *const fi = fffilter(filter);
720 58011 int preinited = 0;
721
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 if (!filter)
723 return NULL;
724
725 58011 ctx = av_mallocz(sizeof(*ctx));
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 if (!ctx)
727 return NULL;
728 58011 ret = &ctx->p;
729
730 58011 ret->av_class = &avfilter_class;
731 58011 ret->filter = filter;
732
1/2
✓ Branch 0 taken 58011 times.
✗ Branch 1 not taken.
58011 ret->name = inst_name ? av_strdup(inst_name) : NULL;
733
2/2
✓ Branch 0 taken 51356 times.
✓ Branch 1 taken 6655 times.
58011 if (fi->priv_size) {
734 51356 ret->priv = av_mallocz(fi->priv_size);
735
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51356 times.
51356 if (!ret->priv)
736 goto err;
737 }
738
2/2
✓ Branch 0 taken 12320 times.
✓ Branch 1 taken 45691 times.
58011 if (fi->preinit) {
739
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12320 times.
12320 if (fi->preinit(ret) < 0)
740 goto err;
741 12320 preinited = 1;
742 }
743
744 58011 av_opt_set_defaults(ret);
745
2/2
✓ Branch 0 taken 50159 times.
✓ Branch 1 taken 7852 times.
58011 if (filter->priv_class) {
746 50159 *(const AVClass**)ret->priv = filter->priv_class;
747 50159 av_opt_set_defaults(ret->priv);
748 }
749
750 58011 ctx->execute = default_execute;
751
752 58011 ret->nb_inputs = fi->nb_inputs;
753
2/2
✓ Branch 0 taken 49231 times.
✓ Branch 1 taken 8780 times.
58011 if (ret->nb_inputs ) {
754 49231 ret->input_pads = av_memdup(filter->inputs, ret->nb_inputs * sizeof(*filter->inputs));
755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49231 times.
49231 if (!ret->input_pads)
756 goto err;
757 49231 ret->inputs = av_calloc(ret->nb_inputs, sizeof(*ret->inputs));
758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49231 times.
49231 if (!ret->inputs)
759 goto err;
760 }
761
762 58011 ret->nb_outputs = fi->nb_outputs;
763
2/2
✓ Branch 0 taken 50167 times.
✓ Branch 1 taken 7844 times.
58011 if (ret->nb_outputs) {
764 50167 ret->output_pads = av_memdup(filter->outputs, ret->nb_outputs * sizeof(*filter->outputs));
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50167 times.
50167 if (!ret->output_pads)
766 goto err;
767 50167 ret->outputs = av_calloc(ret->nb_outputs, sizeof(*ret->outputs));
768
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50167 times.
50167 if (!ret->outputs)
769 goto err;
770 }
771
772 58011 return ret;
773
774 err:
775 if (preinited)
776 fi->uninit(ret);
777 av_freep(&ret->inputs);
778 av_freep(&ret->input_pads);
779 ret->nb_inputs = 0;
780 av_freep(&ret->outputs);
781 av_freep(&ret->output_pads);
782 ret->nb_outputs = 0;
783 av_freep(&ret->priv);
784 av_free(ret);
785 return NULL;
786 }
787
788 99993 static void free_link(AVFilterLink *link)
789 {
790
2/2
✓ Branch 0 taken 57159 times.
✓ Branch 1 taken 42834 times.
99993 if (!link)
791 57159 return;
792
793
2/2
✓ Branch 0 taken 42702 times.
✓ Branch 1 taken 132 times.
42834 if (link->src)
794 42702 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
795
2/2
✓ Branch 0 taken 42702 times.
✓ Branch 1 taken 132 times.
42834 if (link->dst)
796 42702 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
797
798 42834 ff_formats_unref(&link->incfg.formats);
799 42834 ff_formats_unref(&link->outcfg.formats);
800 42834 ff_formats_unref(&link->incfg.color_spaces);
801 42834 ff_formats_unref(&link->outcfg.color_spaces);
802 42834 ff_formats_unref(&link->incfg.color_ranges);
803 42834 ff_formats_unref(&link->outcfg.color_ranges);
804 42834 ff_formats_unref(&link->incfg.samplerates);
805 42834 ff_formats_unref(&link->outcfg.samplerates);
806 42834 ff_channel_layouts_unref(&link->incfg.channel_layouts);
807 42834 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
808 42834 link_free(&link);
809 }
810
811 58011 void avfilter_free(AVFilterContext *filter)
812 {
813 FFFilterContext *ctxi;
814 int i;
815
816
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 if (!filter)
817 return;
818 58011 ctxi = fffilterctx(filter);
819
820
1/2
✓ Branch 0 taken 58011 times.
✗ Branch 1 not taken.
58011 if (filter->graph)
821 58011 ff_filter_graph_remove_filter(filter->graph, filter);
822
823
2/2
✓ Branch 1 taken 44573 times.
✓ Branch 2 taken 13438 times.
58011 if (fffilter(filter->filter)->uninit)
824 44573 fffilter(filter->filter)->uninit(filter);
825
826
2/2
✓ Branch 0 taken 49554 times.
✓ Branch 1 taken 58011 times.
107565 for (i = 0; i < filter->nb_inputs; i++) {
827 49554 free_link(filter->inputs[i]);
828
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 49344 times.
49554 if (filter->input_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
829 210 av_freep(&filter->input_pads[i].name);
830 }
831
2/2
✓ Branch 0 taken 50439 times.
✓ Branch 1 taken 58011 times.
108450 for (i = 0; i < filter->nb_outputs; i++) {
832 50439 free_link(filter->outputs[i]);
833
2/2
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 50172 times.
50439 if (filter->output_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
834 267 av_freep(&filter->output_pads[i].name);
835 }
836
837
2/2
✓ Branch 0 taken 50159 times.
✓ Branch 1 taken 7852 times.
58011 if (filter->filter->priv_class)
838 50159 av_opt_free(filter->priv);
839
840 58011 av_buffer_unref(&filter->hw_device_ctx);
841
842 58011 av_freep(&filter->name);
843 58011 av_freep(&filter->input_pads);
844 58011 av_freep(&filter->output_pads);
845 58011 av_freep(&filter->inputs);
846 58011 av_freep(&filter->outputs);
847 58011 av_freep(&filter->priv);
848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 while (ctxi->command_queue)
849 command_queue_pop(filter);
850 58011 av_opt_free(filter);
851 58011 av_expr_free(ctxi->enable);
852 58011 ctxi->enable = NULL;
853 58011 av_freep(&ctxi->var_values);
854 58011 av_free(filter);
855 }
856
857 16347 int ff_filter_get_nb_threads(AVFilterContext *ctx)
858 {
859
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16347 times.
16347 if (ctx->nb_threads > 0)
860 return FFMIN(ctx->nb_threads, ctx->graph->nb_threads);
861 16347 return ctx->graph->nb_threads;
862 }
863
864 29974 int ff_filter_opt_parse(void *logctx, const AVClass *priv_class,
865 AVDictionary **options, const char *args)
866 {
867 29974 const AVOption *o = NULL;
868 int ret;
869 29974 int offset= -1;
870
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29974 times.
29974 if (!args)
872 return 0;
873
874
2/2
✓ Branch 0 taken 51797 times.
✓ Branch 1 taken 29974 times.
81771 while (*args) {
875 char *parsed_key, *value;
876 const char *key;
877 51797 const char *shorthand = NULL;
878 51797 int additional_flags = 0;
879
880
4/4
✓ Branch 0 taken 40047 times.
✓ Branch 1 taken 11750 times.
✓ Branch 3 taken 38765 times.
✓ Branch 4 taken 1282 times.
51797 if (priv_class && (o = av_opt_next(&priv_class, o))) {
881
4/4
✓ Branch 0 taken 36995 times.
✓ Branch 1 taken 1770 times.
✓ Branch 2 taken 3802 times.
✓ Branch 3 taken 33193 times.
38765 if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
882 5572 continue;
883 33193 offset = o->offset;
884 33193 shorthand = o->name;
885 }
886
887 46225 ret = av_opt_get_key_value(&args, "=", ":",
888 shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
889 &parsed_key, &value);
890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46225 times.
46225 if (ret < 0) {
891 if (ret == AVERROR(EINVAL))
892 av_log(logctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
893 else
894 av_log(logctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
895 av_err2str(ret));
896 return ret;
897 }
898
2/2
✓ Branch 0 taken 24582 times.
✓ Branch 1 taken 21643 times.
46225 if (*args)
899 24582 args++;
900
2/2
✓ Branch 0 taken 27757 times.
✓ Branch 1 taken 18468 times.
46225 if (parsed_key) {
901 27757 key = parsed_key;
902 27757 additional_flags = AV_DICT_DONT_STRDUP_KEY;
903 27757 priv_class = NULL; /* reject all remaining shorthand */
904 } else {
905 18468 key = shorthand;
906 }
907
908 46225 av_log(logctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
909
910 46225 av_dict_set(options, key, value,
911 additional_flags | AV_DICT_DONT_STRDUP_VAL | AV_DICT_MULTIKEY);
912 }
913
914 29974 return 0;
915 }
916
917 int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
918 const char *arg, char *res, int res_len, int flags)
919 {
920 const AVOption *o;
921
922 if (!ctx->filter->priv_class)
923 return 0;
924 o = av_opt_find2(ctx->priv, cmd, NULL, AV_OPT_FLAG_RUNTIME_PARAM | AV_OPT_FLAG_FILTERING_PARAM, AV_OPT_SEARCH_CHILDREN, NULL);
925 if (!o)
926 return AVERROR(ENOSYS);
927 return av_opt_set(ctx->priv, cmd, arg, 0);
928 }
929
930 58011 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
931 {
932 58011 FFFilterContext *ctxi = fffilterctx(ctx);
933 58011 int ret = 0;
934
935
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 if (ctxi->state_flags & AV_CLASS_STATE_INITIALIZED) {
936 av_log(ctx, AV_LOG_ERROR, "Filter already initialized\n");
937 return AVERROR(EINVAL);
938 }
939
940 58011 ret = av_opt_set_dict2(ctx, options, AV_OPT_SEARCH_CHILDREN);
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 if (ret < 0) {
942 av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
943 return ret;
944 }
945
946
2/2
✓ Branch 0 taken 1190 times.
✓ Branch 1 taken 56821 times.
58011 if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
947
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 1043 times.
1190 ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
948
1/2
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
147 fffiltergraph(ctx->graph)->thread_execute) {
949 147 ctx->thread_type = AVFILTER_THREAD_SLICE;
950 147 ctxi->execute = fffiltergraph(ctx->graph)->thread_execute;
951 } else {
952 57864 ctx->thread_type = 0;
953 }
954
955
2/2
✓ Branch 1 taken 45032 times.
✓ Branch 2 taken 12979 times.
58011 if (fffilter(ctx->filter)->init)
956 45032 ret = fffilter(ctx->filter)->init(ctx);
957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58011 times.
58011 if (ret < 0)
958 return ret;
959
960
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 58005 times.
58011 if (ctx->enable_str) {
961 6 ret = set_enable_expr(ctxi, ctx->enable_str);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
963 return ret;
964 }
965
966 58011 ctxi->state_flags |= AV_CLASS_STATE_INITIALIZED;
967
968 58011 return 0;
969 }
970
971 18983 int avfilter_init_str(AVFilterContext *filter, const char *args)
972 {
973 18983 AVDictionary *options = NULL;
974 const AVDictionaryEntry *e;
975 18983 int ret = 0;
976
977
3/4
✓ Branch 0 taken 9487 times.
✓ Branch 1 taken 9496 times.
✓ Branch 2 taken 9487 times.
✗ Branch 3 not taken.
18983 if (args && *args) {
978 9487 ret = ff_filter_opt_parse(filter, filter->filter->priv_class, &options, args);
979
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9487 times.
9487 if (ret < 0)
980 goto fail;
981 }
982
983 18983 ret = avfilter_init_dict(filter, &options);
984
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18983 times.
18983 if (ret < 0)
985 goto fail;
986
987
1/2
✓ Branch 1 taken 18983 times.
✗ Branch 2 not taken.
18983 if ((e = av_dict_iterate(options, NULL))) {
988 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
989 ret = AVERROR_OPTION_NOT_FOUND;
990 goto fail;
991 }
992
993 18983 fail:
994 18983 av_dict_free(&options);
995
996 18983 return ret;
997 }
998
999 14455 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
1000 {
1001 14455 return pads[pad_idx].name;
1002 }
1003
1004 29183 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
1005 {
1006 29183 return pads[pad_idx].type;
1007 }
1008
1009 658655 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
1010 {
1011 658655 return ff_filter_frame(link->dst->outputs[0], frame);
1012 }
1013
1014 /**
1015 * Evaluate the timeline expression of the link for the time and properties
1016 * of the frame.
1017 * @return >0 if enabled, 0 if disabled
1018 * @note It does not update link->dst->is_disabled.
1019 */
1020 2097207 static int evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
1021 {
1022 2097207 FilterLink *l = ff_filter_link(link);
1023 2097207 AVFilterContext *dstctx = link->dst;
1024 2097207 FFFilterContext *dsti = fffilterctx(dstctx);
1025 2097207 int64_t pts = frame->pts;
1026 #if FF_API_FRAME_PKT
1027 FF_DISABLE_DEPRECATION_WARNINGS
1028 2097207 int64_t pos = frame->pkt_pos;
1029 FF_ENABLE_DEPRECATION_WARNINGS
1030 #endif
1031
1032
2/2
✓ Branch 0 taken 2097011 times.
✓ Branch 1 taken 196 times.
2097207 if (!dstctx->enable_str)
1033 2097011 return 1;
1034
1035 196 dsti->var_values[VAR_N] = l->frame_count_out;
1036
1/2
✓ Branch 0 taken 196 times.
✗ Branch 1 not taken.
196 dsti->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
1037 196 dsti->var_values[VAR_W] = link->w;
1038 196 dsti->var_values[VAR_H] = link->h;
1039 #if FF_API_FRAME_PKT
1040
1/2
✓ Branch 0 taken 196 times.
✗ Branch 1 not taken.
196 dsti->var_values[VAR_POS] = pos == -1 ? NAN : pos;
1041 #endif
1042
1043 196 return fabs(av_expr_eval(dsti->enable, dsti->var_values, NULL)) >= 0.5;
1044 }
1045
1046 676736 static int filter_frame_framed(AVFilterLink *link, AVFrame *frame)
1047 {
1048 676736 FilterLink *l = ff_filter_link(link);
1049 int (*filter_frame)(AVFilterLink *, AVFrame *);
1050 676736 AVFilterContext *dstctx = link->dst;
1051 676736 AVFilterPad *dst = link->dstpad;
1052 int ret;
1053
1054
2/2
✓ Branch 0 taken 658646 times.
✓ Branch 1 taken 18090 times.
676736 if (!(filter_frame = dst->filter_frame))
1055 658646 filter_frame = default_filter_frame;
1056
1057
2/2
✓ Branch 0 taken 2551 times.
✓ Branch 1 taken 674185 times.
676736 if (dst->flags & AVFILTERPAD_FLAG_NEEDS_WRITABLE) {
1058 2551 ret = ff_inlink_make_frame_writable(link, &frame);
1059
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
2551 if (ret < 0)
1060 goto fail;
1061 }
1062
1063 676736 ff_inlink_process_commands(link, frame);
1064 676736 dstctx->is_disabled = !evaluate_timeline_at_frame(link, frame);
1065
1066
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 676717 times.
676736 if (dstctx->is_disabled &&
1067
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
1068 9 filter_frame = default_filter_frame;
1069 676736 ret = filter_frame(link, frame);
1070 676736 l->frame_count_out++;
1071 676736 return ret;
1072
1073 fail:
1074 av_frame_free(&frame);
1075 return ret;
1076 }
1077
1078 1422399 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
1079 {
1080 1422399 FilterLinkInternal * const li = ff_link_internal(link);
1081 int ret;
1082 1422399 FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); tlog_ref(NULL, frame, 1);
1083
1084 /* Consistency checks */
1085
2/2
✓ Branch 0 taken 494718 times.
✓ Branch 1 taken 927681 times.
1422399 if (link->type == AVMEDIA_TYPE_VIDEO) {
1086
2/2
✓ Branch 0 taken 361863 times.
✓ Branch 1 taken 132855 times.
494718 if (strcmp(link->dst->filter->name, "buffersink") &&
1087
2/2
✓ Branch 0 taken 221765 times.
✓ Branch 1 taken 140098 times.
361863 strcmp(link->dst->filter->name, "format") &&
1088
2/2
✓ Branch 0 taken 221739 times.
✓ Branch 1 taken 26 times.
221765 strcmp(link->dst->filter->name, "idet") &&
1089
2/2
✓ Branch 0 taken 132924 times.
✓ Branch 1 taken 88815 times.
221739 strcmp(link->dst->filter->name, "null") &&
1090 132924 strcmp(link->dst->filter->name, "scale")) {
1091 av_assert1(frame->format == link->format);
1092 av_assert1(frame->width == link->w);
1093 av_assert1(frame->height == link->h);
1094 }
1095
1096 494718 frame->sample_aspect_ratio = link->sample_aspect_ratio;
1097 } else {
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 927681 times.
927681 if (frame->format != link->format) {
1099 av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
1100 goto error;
1101 }
1102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 927681 times.
927681 if (av_channel_layout_compare(&frame->ch_layout, &link->ch_layout)) {
1103 av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
1104 goto error;
1105 }
1106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 927681 times.
927681 if (frame->sample_rate != link->sample_rate) {
1107 av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
1108 goto error;
1109 }
1110
1111 927681 frame->duration = av_rescale_q(frame->nb_samples, (AVRational){ 1, frame->sample_rate },
1112 link->time_base);
1113 }
1114
1115 1422399 li->frame_blocked_in = li->frame_wanted_out = 0;
1116 1422399 li->l.frame_count_in++;
1117 1422399 li->l.sample_count_in += frame->nb_samples;
1118 1422399 filter_unblock(link->dst);
1119 1422399 ret = ff_framequeue_add(&li->fifo, frame);
1120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1422399 times.
1422399 if (ret < 0) {
1121 av_frame_free(&frame);
1122 return ret;
1123 }
1124 1422399 ff_filter_set_ready(link->dst, 300);
1125 1422399 return 0;
1126
1127 error:
1128 av_frame_free(&frame);
1129 return AVERROR_PATCHWELCOME;
1130 }
1131
1132 2021119 static int samples_ready(FilterLinkInternal *link, unsigned min)
1133 {
1134
2/2
✓ Branch 1 taken 676839 times.
✓ Branch 2 taken 1344280 times.
2697958 return ff_framequeue_queued_frames(&link->fifo) &&
1135
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 676735 times.
676839 (ff_framequeue_queued_samples(&link->fifo) >= min ||
1136
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 103 times.
104 link->status_in);
1137 }
1138
1139 2482 static int take_samples(FilterLinkInternal *li, unsigned min, unsigned max,
1140 AVFrame **rframe)
1141 {
1142 2482 FilterLink *l = &li->l;
1143 2482 AVFilterLink *link = &l->pub;
1144 AVFrame *frame0, *frame, *buf;
1145 unsigned nb_samples, nb_frames, i, p;
1146 int ret;
1147
1148 /* Note: this function relies on no format changes and must only be
1149 called with enough samples. */
1150 av_assert1(samples_ready(li, l->min_samples));
1151 2482 frame0 = frame = ff_framequeue_peek(&li->fifo, 0);
1152
6/6
✓ Branch 0 taken 926 times.
✓ Branch 1 taken 1556 times.
✓ Branch 2 taken 813 times.
✓ Branch 3 taken 113 times.
✓ Branch 4 taken 541 times.
✓ Branch 5 taken 272 times.
2482 if (!li->fifo.samples_skipped && frame->nb_samples >= min && frame->nb_samples <= max) {
1153 541 *rframe = ff_framequeue_take(&li->fifo);
1154 541 return 0;
1155 }
1156 1941 nb_frames = 0;
1157 1941 nb_samples = 0;
1158 while (1) {
1159
2/2
✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 1465 times.
3023 if (nb_samples + frame->nb_samples > max) {
1160
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 2 times.
1558 if (nb_samples < min)
1161 1556 nb_samples = max;
1162 1558 break;
1163 }
1164 1465 nb_samples += frame->nb_samples;
1165 1465 nb_frames++;
1166
2/2
✓ Branch 1 taken 383 times.
✓ Branch 2 taken 1082 times.
1465 if (nb_frames == ff_framequeue_queued_frames(&li->fifo))
1167 383 break;
1168 1082 frame = ff_framequeue_peek(&li->fifo, nb_frames);
1169 }
1170
1171 1941 buf = ff_get_audio_buffer(link, nb_samples);
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1941 times.
1941 if (!buf)
1173 return AVERROR(ENOMEM);
1174 1941 ret = av_frame_copy_props(buf, frame0);
1175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1941 times.
1941 if (ret < 0) {
1176 av_frame_free(&buf);
1177 return ret;
1178 }
1179
1180 1941 p = 0;
1181
2/2
✓ Branch 0 taken 1465 times.
✓ Branch 1 taken 1941 times.
3406 for (i = 0; i < nb_frames; i++) {
1182 1465 frame = ff_framequeue_take(&li->fifo);
1183 1465 av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
1184 1465 frame->nb_samples, link->ch_layout.nb_channels, link->format);
1185 1465 p += frame->nb_samples;
1186 1465 av_frame_free(&frame);
1187 }
1188
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 385 times.
1941 if (p < nb_samples) {
1189 1556 unsigned n = nb_samples - p;
1190 1556 frame = ff_framequeue_peek(&li->fifo, 0);
1191 1556 av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
1192 1556 link->ch_layout.nb_channels, link->format);
1193 1556 ff_framequeue_skip_samples(&li->fifo, n, link->time_base);
1194 }
1195
1196 1941 *rframe = buf;
1197 1941 return 0;
1198 }
1199
1200 676736 static int filter_frame_to_filter(AVFilterLink *link)
1201 {
1202 676736 FilterLinkInternal * const li = ff_link_internal(link);
1203 676736 AVFrame *frame = NULL;
1204 676736 AVFilterContext *dst = link->dst;
1205 int ret;
1206
1207 av_assert1(ff_framequeue_queued_frames(&li->fifo));
1208 1353472 ret = li->l.min_samples ?
1209
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 676700 times.
676736 ff_inlink_consume_samples(link, li->l.min_samples, li->l.max_samples, &frame) :
1210 676700 ff_inlink_consume_frame(link, &frame);
1211 av_assert1(ret);
1212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 676736 times.
676736 if (ret < 0) {
1213 av_assert1(!frame);
1214 return ret;
1215 }
1216 /* The filter will soon have received a new frame, that may allow it to
1217 produce one or more: unblock its outputs. */
1218 676736 filter_unblock(dst);
1219 /* AVFilterPad.filter_frame() expect frame_count_out to have the value
1220 before the frame; filter_frame_framed() will re-increment it. */
1221 676736 li->l.frame_count_out--;
1222 676736 ret = filter_frame_framed(link, frame);
1223
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 676736 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
676736 if (ret < 0 && ret != li->status_out) {
1224 link_set_out_status(link, ret, AV_NOPTS_VALUE);
1225 } else {
1226 /* Run once again, to see if several frames were available, or if
1227 the input status has also changed, or any other reason. */
1228 676736 ff_filter_set_ready(dst, 300);
1229 }
1230 676736 return ret;
1231 }
1232
1233 5884 static int forward_status_change(AVFilterContext *filter, FilterLinkInternal *li_in)
1234 {
1235 5884 AVFilterLink *in = &li_in->l.pub;
1236 5884 unsigned out = 0, progress = 0;
1237 int ret;
1238
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5884 times.
5884 av_assert0(!li_in->status_out);
1240
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5884 times.
5884 if (!filter->nb_outputs) {
1241 /* not necessary with the current API and sinks */
1242 return 0;
1243 }
1244
2/2
✓ Branch 0 taken 5884 times.
✓ Branch 1 taken 5884 times.
11768 while (!li_in->status_out) {
1245 5884 FilterLinkInternal *li_out = ff_link_internal(filter->outputs[out]);
1246
1247
1/2
✓ Branch 0 taken 5884 times.
✗ Branch 1 not taken.
5884 if (!li_out->status_in) {
1248 5884 progress++;
1249 5884 ret = request_frame_to_filter(filter->outputs[out]);
1250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5884 times.
5884 if (ret < 0)
1251 return ret;
1252 }
1253
1/2
✓ Branch 0 taken 5884 times.
✗ Branch 1 not taken.
5884 if (++out == filter->nb_outputs) {
1254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5884 times.
5884 if (!progress) {
1255 /* Every output already closed: input no longer interesting
1256 (example: overlay in shortest mode, other input closed). */
1257 link_set_out_status(in, li_in->status_in, li_in->status_in_pts);
1258 return 0;
1259 }
1260 5884 progress = 0;
1261 5884 out = 0;
1262 }
1263 }
1264 5884 ff_filter_set_ready(filter, 200);
1265 5884 return 0;
1266 }
1267
1268 2028105 static int filter_activate_default(AVFilterContext *filter)
1269 {
1270 unsigned i;
1271
1272
2/2
✓ Branch 0 taken 2028105 times.
✓ Branch 1 taken 2021400 times.
4049505 for (i = 0; i < filter->nb_outputs; i++) {
1273 2028105 FilterLinkInternal *li = ff_link_internal(filter->outputs[i]);
1274 2028105 int ret = li->status_in;
1275
1276
2/2
✓ Branch 0 taken 6705 times.
✓ Branch 1 taken 2021400 times.
2028105 if (ret) {
1277
2/2
✓ Branch 0 taken 6705 times.
✓ Branch 1 taken 6705 times.
13410 for (int j = 0; j < filter->nb_inputs; j++)
1278 6705 ff_inlink_set_status(filter->inputs[j], ret);
1279 6705 return 0;
1280 }
1281 }
1282
1283
2/2
✓ Branch 0 taken 2021119 times.
✓ Branch 1 taken 1344664 times.
3365783 for (i = 0; i < filter->nb_inputs; i++) {
1284 2021119 FilterLinkInternal *li = ff_link_internal(filter->inputs[i]);
1285
2/2
✓ Branch 1 taken 676736 times.
✓ Branch 2 taken 1344383 times.
2021119 if (samples_ready(li, li->l.min_samples)) {
1286 676736 return filter_frame_to_filter(filter->inputs[i]);
1287 }
1288 }
1289
2/2
✓ Branch 0 taken 1344383 times.
✓ Branch 1 taken 1338780 times.
2683163 for (i = 0; i < filter->nb_inputs; i++) {
1290 1344383 FilterLinkInternal * const li = ff_link_internal(filter->inputs[i]);
1291
4/4
✓ Branch 0 taken 5908 times.
✓ Branch 1 taken 1338475 times.
✓ Branch 2 taken 5884 times.
✓ Branch 3 taken 24 times.
1344383 if (li->status_in && !li->status_out) {
1292 av_assert1(!ff_framequeue_queued_frames(&li->fifo));
1293 5884 return forward_status_change(filter, li);
1294 }
1295 }
1296
2/2
✓ Branch 0 taken 1338780 times.
✓ Branch 1 taken 671904 times.
2010684 for (i = 0; i < filter->nb_outputs; i++) {
1297 1338780 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
1298
2/2
✓ Branch 0 taken 667026 times.
✓ Branch 1 taken 671754 times.
1338780 if (li->frame_wanted_out &&
1299
2/2
✓ Branch 0 taken 666876 times.
✓ Branch 1 taken 150 times.
667026 !li->frame_blocked_in) {
1300 666876 return request_frame_to_filter(filter->outputs[i]);
1301 }
1302 }
1303 671904 return FFERROR_NOT_READY;
1304 }
1305
1306 /*
1307 Filter scheduling and activation
1308
1309 When a filter is activated, it must:
1310 - if possible, output a frame;
1311 - else, if relevant, forward the input status change;
1312 - else, check outputs for wanted frames and forward the requests.
1313
1314 The following AVFilterLink fields are used for activation:
1315
1316 - frame_wanted_out:
1317
1318 This field indicates if a frame is needed on this input of the
1319 destination filter. A positive value indicates that a frame is needed
1320 to process queued frames or internal data or to satisfy the
1321 application; a zero value indicates that a frame is not especially
1322 needed but could be processed anyway; a negative value indicates that a
1323 frame would just be queued.
1324
1325 It is set by filters using ff_request_frame() or ff_request_no_frame(),
1326 when requested by the application through a specific API or when it is
1327 set on one of the outputs.
1328
1329 It is cleared when a frame is sent from the source using
1330 ff_filter_frame().
1331
1332 It is also cleared when a status change is sent from the source using
1333 ff_avfilter_link_set_in_status().
1334
1335 - frame_blocked_in:
1336
1337 This field means that the source filter can not generate a frame as is.
1338 Its goal is to avoid repeatedly calling the request_frame() method on
1339 the same link.
1340
1341 It is set by the framework on all outputs of a filter before activating it.
1342
1343 It is automatically cleared by ff_filter_frame().
1344
1345 It is also automatically cleared by ff_avfilter_link_set_in_status().
1346
1347 It is also cleared on all outputs (using filter_unblock()) when
1348 something happens on an input: processing a frame or changing the
1349 status.
1350
1351 - fifo:
1352
1353 Contains the frames queued on a filter input. If it contains frames and
1354 frame_wanted_out is not set, then the filter can be activated. If that
1355 result in the filter not able to use these frames, the filter must set
1356 frame_wanted_out to ask for more frames.
1357
1358 - status_in and status_in_pts:
1359
1360 Status (EOF or error code) of the link and timestamp of the status
1361 change (in link time base, same as frames) as seen from the input of
1362 the link. The status change is considered happening after the frames
1363 queued in fifo.
1364
1365 It is set by the source filter using ff_avfilter_link_set_in_status().
1366
1367 - status_out:
1368
1369 Status of the link as seen from the output of the link. The status
1370 change is considered having already happened.
1371
1372 It is set by the destination filter using
1373 link_set_out_status().
1374
1375 Filters are activated according to the ready field, set using the
1376 ff_filter_set_ready(). Eventually, a priority queue will be used.
1377 ff_filter_set_ready() is called whenever anything could cause progress to
1378 be possible. Marking a filter ready when it is not is not a problem,
1379 except for the small overhead it causes.
1380
1381 Conditions that cause a filter to be marked ready are:
1382
1383 - frames added on an input link;
1384
1385 - changes in the input or output status of an input link;
1386
1387 - requests for a frame on an output link;
1388
1389 - after any actual processing using the legacy methods (filter_frame(),
1390 and request_frame() to acknowledge status changes), to run once more
1391 and check if enough input was present for several frames.
1392
1393 Examples of scenarios to consider:
1394
1395 - buffersrc: activate if frame_wanted_out to notify the application;
1396 activate when the application adds a frame to push it immediately.
1397
1398 - testsrc: activate only if frame_wanted_out to produce and push a frame.
1399
1400 - concat (not at stitch points): can process a frame on any output.
1401 Activate if frame_wanted_out on output to forward on the corresponding
1402 input. Activate when a frame is present on input to process it
1403 immediately.
1404
1405 - framesync: needs at least one frame on each input; extra frames on the
1406 wrong input will accumulate. When a frame is first added on one input,
1407 set frame_wanted_out<0 on it to avoid getting more (would trigger
1408 testsrc) and frame_wanted_out>0 on the other to allow processing it.
1409
1410 Activation of old filters:
1411
1412 In order to activate a filter implementing the legacy filter_frame() and
1413 request_frame() methods, perform the first possible of the following
1414 actions:
1415
1416 - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
1417 frame and call filter_frame().
1418
1419 Rationale: filter frames as soon as possible instead of leaving them
1420 queued; frame_wanted_out < 0 is not possible since the old API does not
1421 set it nor provides any similar feedback; frame_wanted_out > 0 happens
1422 when min_samples > 0 and there are not enough samples queued.
1423
1424 - If an input has status_in set but not status_out, try to call
1425 request_frame() on one of the outputs in the hope that it will trigger
1426 request_frame() on the input with status_in and acknowledge it. This is
1427 awkward and fragile, filters with several inputs or outputs should be
1428 updated to direct activation as soon as possible.
1429
1430 - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
1431 request_frame().
1432
1433 Rationale: checking frame_blocked_in is necessary to avoid requesting
1434 repeatedly on a blocked input if another is not blocked (example:
1435 [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
1436 */
1437
1438 3524908 int ff_filter_activate(AVFilterContext *filter)
1439 {
1440 3524908 FFFilterContext *ctxi = fffilterctx(filter);
1441 3524908 const FFFilter *const fi = fffilter(filter->filter);
1442 int ret;
1443
1444 /* Generic timeline support is not yet implemented but should be easy */
1445 av_assert1(!(fi->p.flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
1446 fi->activate));
1447 3524908 ctxi->ready = 0;
1448
2/2
✓ Branch 0 taken 1496803 times.
✓ Branch 1 taken 2028105 times.
3524908 ret = fi->activate ? fi->activate(filter) : filter_activate_default(filter);
1449
2/2
✓ Branch 0 taken 1032608 times.
✓ Branch 1 taken 2492300 times.
3524908 if (ret == FFERROR_NOT_READY)
1450 1032608 ret = 0;
1451 3524908 return ret;
1452 }
1453
1454 3141491 int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
1455 {
1456 3141491 FilterLinkInternal * const li = ff_link_internal(link);
1457 3141491 *rpts = li->l.current_pts;
1458
2/2
✓ Branch 1 taken 1966 times.
✓ Branch 2 taken 3139525 times.
3141491 if (ff_framequeue_queued_frames(&li->fifo))
1459 1966 return *rstatus = 0;
1460
2/2
✓ Branch 0 taken 1883 times.
✓ Branch 1 taken 3137642 times.
3139525 if (li->status_out)
1461 1883 return *rstatus = li->status_out;
1462
2/2
✓ Branch 0 taken 3130207 times.
✓ Branch 1 taken 7435 times.
3137642 if (!li->status_in)
1463 3130207 return *rstatus = 0;
1464 7435 *rstatus = li->status_out = li->status_in;
1465 7435 update_link_current_pts(li, li->status_in_pts);
1466 7435 *rpts = li->l.current_pts;
1467 7435 return 1;
1468 }
1469
1470 496727 size_t ff_inlink_queued_frames(AVFilterLink *link)
1471 {
1472 496727 FilterLinkInternal * const li = ff_link_internal(link);
1473 496727 return ff_framequeue_queued_frames(&li->fifo);
1474 }
1475
1476 4308206 int ff_inlink_check_available_frame(AVFilterLink *link)
1477 {
1478 4308206 FilterLinkInternal * const li = ff_link_internal(link);
1479 4308206 return ff_framequeue_queued_frames(&li->fifo) > 0;
1480 }
1481
1482 2019 int ff_inlink_queued_samples(AVFilterLink *link)
1483 {
1484 2019 FilterLinkInternal * const li = ff_link_internal(link);
1485 2019 return ff_framequeue_queued_samples(&li->fifo);
1486 }
1487
1488 4353 int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
1489 {
1490 4353 FilterLinkInternal * const li = ff_link_internal(link);
1491 4353 uint64_t samples = ff_framequeue_queued_samples(&li->fifo);
1492 av_assert1(min);
1493
6/6
✓ Branch 0 taken 1877 times.
✓ Branch 1 taken 2476 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 1811 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 59 times.
4353 return samples >= min || (li->status_in && samples);
1494 }
1495
1496 1422844 static void consume_update(FilterLinkInternal *li, const AVFrame *frame)
1497 {
1498 1422844 AVFilterLink *const link = &li->l.pub;
1499 1422844 update_link_current_pts(li, frame->pts);
1500 1422844 ff_inlink_process_commands(link, frame);
1501
2/2
✓ Branch 0 taken 1420471 times.
✓ Branch 1 taken 2373 times.
1422844 if (link == link->dst->inputs[0])
1502 1420471 link->dst->is_disabled = !evaluate_timeline_at_frame(link, frame);
1503 1422844 li->l.frame_count_out++;
1504 1422844 li->l.sample_count_out += frame->nb_samples;
1505 1422844 }
1506
1507 4306321 int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
1508 {
1509 4306321 FilterLinkInternal * const li = ff_link_internal(link);
1510 AVFrame *frame;
1511
1512 4306321 *rframe = NULL;
1513
2/2
✓ Branch 1 taken 2885956 times.
✓ Branch 2 taken 1420365 times.
4306321 if (!ff_inlink_check_available_frame(link))
1514 2885956 return 0;
1515
1516
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1420362 times.
1420365 if (li->fifo.samples_skipped) {
1517 3 frame = ff_framequeue_peek(&li->fifo, 0);
1518 3 return ff_inlink_consume_samples(link, frame->nb_samples, frame->nb_samples, rframe);
1519 }
1520
1521 1420362 frame = ff_framequeue_take(&li->fifo);
1522 1420362 consume_update(li, frame);
1523 1420362 *rframe = frame;
1524 1420362 return 1;
1525 }
1526
1527 4217 int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
1528 AVFrame **rframe)
1529 {
1530 4217 FilterLinkInternal * const li = ff_link_internal(link);
1531 AVFrame *frame;
1532 int ret;
1533
1534 av_assert1(min);
1535 4217 *rframe = NULL;
1536
2/2
✓ Branch 1 taken 1735 times.
✓ Branch 2 taken 2482 times.
4217 if (!ff_inlink_check_available_samples(link, min))
1537 1735 return 0;
1538
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2472 times.
2482 if (li->status_in)
1539
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 4 times.
10 min = FFMIN(min, ff_framequeue_queued_samples(&li->fifo));
1540 2482 ret = take_samples(li, min, max, &frame);
1541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2482 times.
2482 if (ret < 0)
1542 return ret;
1543 2482 consume_update(li, frame);
1544 2482 *rframe = frame;
1545 2482 return 1;
1546 }
1547
1548 AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
1549 {
1550 FilterLinkInternal * const li = ff_link_internal(link);
1551 return ff_framequeue_peek(&li->fifo, idx);
1552 }
1553
1554 4687 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
1555 {
1556 4687 AVFrame *frame = *rframe;
1557 AVFrame *out;
1558 int ret;
1559
1560
2/2
✓ Branch 1 taken 2188 times.
✓ Branch 2 taken 2499 times.
4687 if (av_frame_is_writable(frame))
1561 2188 return 0;
1562 2499 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
1563
1564
1/3
✓ Branch 0 taken 2499 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2499 switch (link->type) {
1565 2499 case AVMEDIA_TYPE_VIDEO:
1566 2499 out = ff_get_video_buffer(link, link->w, link->h);
1567 2499 break;
1568 case AVMEDIA_TYPE_AUDIO:
1569 out = ff_get_audio_buffer(link, frame->nb_samples);
1570 break;
1571 default:
1572 return AVERROR(EINVAL);
1573 }
1574
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2499 times.
2499 if (!out)
1575 return AVERROR(ENOMEM);
1576
1577 2499 ret = av_frame_copy_props(out, frame);
1578
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2499 times.
2499 if (ret < 0) {
1579 av_frame_free(&out);
1580 return ret;
1581 }
1582
1583 2499 ret = av_frame_copy(out, frame);
1584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2499 times.
2499 if (ret < 0) {
1585 av_frame_free(&out);
1586 return ret;
1587 }
1588
1589 2499 av_frame_free(&frame);
1590 2499 *rframe = out;
1591 2499 return 0;
1592 }
1593
1594 2099580 int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
1595 {
1596 2099580 FFFilterContext *ctxi = fffilterctx(link->dst);
1597 2099580 AVFilterCommand *cmd = ctxi->command_queue;
1598
1599
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2099580 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2099580 while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
1600 av_log(link->dst, AV_LOG_DEBUG,
1601 "Processing command time:%f command:%s arg:%s\n",
1602 cmd->time, cmd->command, cmd->arg);
1603 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
1604 command_queue_pop(link->dst);
1605 cmd = ctxi->command_queue;
1606 }
1607 2099580 return 0;
1608 }
1609
1610 737924 void ff_inlink_request_frame(AVFilterLink *link)
1611 {
1612 737924 av_unused FilterLinkInternal *li = ff_link_internal(link);
1613 av_assert1(!li->status_in);
1614 av_assert1(!li->status_out);
1615 737924 li->frame_wanted_out = 1;
1616 737924 ff_filter_set_ready(link->src, 100);
1617 737924 }
1618
1619 10385 void ff_inlink_set_status(AVFilterLink *link, int status)
1620 {
1621 10385 FilterLinkInternal * const li = ff_link_internal(link);
1622
2/2
✓ Branch 0 taken 8283 times.
✓ Branch 1 taken 2102 times.
10385 if (li->status_out)
1623 8283 return;
1624 2102 li->frame_wanted_out = 0;
1625 2102 li->frame_blocked_in = 0;
1626 2102 link_set_out_status(link, status, AV_NOPTS_VALUE);
1627
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2102 times.
2102 while (ff_framequeue_queued_frames(&li->fifo)) {
1628 AVFrame *frame = ff_framequeue_take(&li->fifo);
1629 av_frame_free(&frame);
1630 }
1631
2/2
✓ Branch 0 taken 2101 times.
✓ Branch 1 taken 1 times.
2102 if (!li->status_in)
1632 2101 li->status_in = status;
1633 }
1634
1635 1068765 int ff_outlink_get_status(AVFilterLink *link)
1636 {
1637 1068765 FilterLinkInternal * const li = ff_link_internal(link);
1638 1068765 return li->status_in;
1639 }
1640
1641 1478 int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink)
1642 {
1643 1478 FilterLinkInternal * const li_in = ff_link_internal(inlink);
1644
1/2
✓ Branch 1 taken 887 times.
✗ Branch 2 not taken.
2365 return ff_outlink_frame_wanted(outlink) ||
1645
2/2
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 591 times.
2365 ff_inlink_check_available_frame(inlink) ||
1646
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 860 times.
887 li_in->status_out;
1647 }
1648
1649
1650 const AVClass *avfilter_get_class(void)
1651 {
1652 return &avfilter_class;
1653 }
1654
1655 int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
1656 int default_pool_size)
1657 {
1658 FilterLink *l = ff_filter_link(link);
1659 AVHWFramesContext *frames;
1660
1661 // Must already be set by caller.
1662 av_assert0(l->hw_frames_ctx);
1663
1664 frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
1665
1666 if (frames->initial_pool_size == 0) {
1667 // Dynamic allocation is necessarily supported.
1668 } else if (avctx->extra_hw_frames >= 0) {
1669 frames->initial_pool_size += avctx->extra_hw_frames;
1670 } else {
1671 frames->initial_pool_size = default_pool_size;
1672 }
1673
1674 return 0;
1675 }
1676
1677 386936 int ff_outlink_frame_wanted(AVFilterLink *link)
1678 {
1679 386936 FilterLinkInternal * const li = ff_link_internal(link);
1680 386936 return li->frame_wanted_out;
1681 }
1682
1683 4180 int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func,
1684 void *arg, int *ret, int nb_jobs)
1685 {
1686 4180 return fffilterctx(ctx)->execute(ctx, func, arg, ret, nb_jobs);
1687 }
1688