FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfilter.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 652 836 78.0%
Functions: 58 66 87.9%
Branches: 348 498 69.9%

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 1477657 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 1477657 }
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 45867 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 45867 times.
45867 av_assert0(src->graph);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45867 times.
45867 av_assert0(dst->graph);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45867 times.
45867 av_assert0(src->graph == dst->graph);
158
159
2/4
✓ Branch 0 taken 45867 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 45867 times.
✗ Branch 3 not taken.
45867 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
160
2/4
✓ Branch 0 taken 45867 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 45867 times.
45867 src->outputs[srcpad] || dst->inputs[dstpad])
161 return AVERROR(EINVAL);
162
163
1/2
✓ Branch 1 taken 45867 times.
✗ Branch 2 not taken.
45867 if (!(fffilterctx(src)->state_flags & AV_CLASS_STATE_INITIALIZED) ||
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45867 times.
45867 !(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 45867 times.
45867 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 45867 li = av_mallocz(sizeof(*li));
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45867 times.
45867 if (!li)
179 return AVERROR(ENOMEM);
180 45867 link = &li->l.pub;
181
182 45867 src->outputs[srcpad] = dst->inputs[dstpad] = link;
183
184 45867 link->src = src;
185 45867 link->dst = dst;
186 45867 link->srcpad = &src->output_pads[srcpad];
187 45867 link->dstpad = &dst->input_pads[dstpad];
188 45867 link->type = src->output_pads[srcpad].type;
189 45867 li->l.graph = src->graph;
190 av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
191 45867 link->format = -1;
192 45867 link->colorspace = AVCOL_SPC_UNSPECIFIED;
193 45867 ff_framequeue_init(&li->fifo, &fffiltergraph(src->graph)->frame_queues);
194
195 45867 return 0;
196 }
197
198 45999 static void link_free(AVFilterLink **link)
199 {
200 FilterLinkInternal *li;
201
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45999 times.
45999 if (!*link)
203 return;
204 45999 li = ff_link_internal(*link);
205
206 45999 ff_framequeue_free(&li->fifo);
207 45999 ff_frame_pool_uninit(&li->frame_pool);
208 45999 av_channel_layout_uninit(&(*link)->ch_layout);
209 45999 av_frame_side_data_free(&(*link)->side_data, &(*link)->nb_side_data);
210
211 45999 av_buffer_unref(&li->l.hw_frames_ctx);
212
213 45999 av_freep(link);
214 }
215
216 1491800 static void update_link_current_pts(FilterLinkInternal *li, int64_t pts)
217 {
218 1491800 AVFilterLink *const link = &li->l.pub;
219
220
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1491795 times.
1491800 if (pts == AV_NOPTS_VALUE)
221 5 return;
222 1491795 li->l.current_pts = pts;
223 1491795 li->l.current_pts_us = av_rescale_q(pts, link->time_base, AV_TIME_BASE_Q);
224 /* TODO use duration */
225
3/4
✓ Branch 0 taken 1491795 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 407841 times.
✓ Branch 3 taken 1083954 times.
1491795 if (li->l.graph && li->age_index >= 0)
226 407841 ff_avfilter_graph_update_heap(li->l.graph, li);
227 }
228
229 3672385 void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
230 {
231 3672385 FFFilterContext *ctxi = fffilterctx(filter);
232 3672385 ctxi->ready = FFMAX(ctxi->ready, priority);
233 3672385 }
234
235 /**
236 * Clear frame_blocked_in on all outputs.
237 * This is necessary whenever something changes on input.
238 */
239 2200442 static void filter_unblock(AVFilterContext *filter)
240 {
241 unsigned i;
242
243
2/2
✓ Branch 0 taken 1794485 times.
✓ Branch 1 taken 2200442 times.
3994927 for (i = 0; i < filter->nb_outputs; i++) {
244 1794485 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
245 1794485 li->frame_blocked_in = 0;
246 }
247 2200442 }
248
249
250 17527 void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
251 {
252 17527 FilterLinkInternal * const li = ff_link_internal(link);
253
254
2/2
✓ Branch 0 taken 3835 times.
✓ Branch 1 taken 13692 times.
17527 if (li->status_in == status)
255 3835 return;
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13692 times.
13692 av_assert0(!li->status_in);
257 13692 li->status_in = status;
258 13692 li->status_in_pts = pts;
259 13692 li->frame_wanted_out = 0;
260 13692 li->frame_blocked_in = 0;
261 13692 filter_unblock(link->dst);
262 13692 ff_filter_set_ready(link->dst, 200);
263 }
264
265 /**
266 * Set the status field of a link from the destination filter.
267 * The pts should probably be left unset (AV_NOPTS_VALUE).
268 */
269 8323 static void link_set_out_status(AVFilterLink *link, int status, int64_t pts)
270 {
271 8323 FilterLinkInternal * const li = ff_link_internal(link);
272
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8323 times.
8323 av_assert0(!li->frame_wanted_out);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8323 times.
8323 av_assert0(!li->status_out);
275 8323 li->status_out = status;
276
2/2
✓ Branch 0 taken 5925 times.
✓ Branch 1 taken 2398 times.
8323 if (pts != AV_NOPTS_VALUE)
277 5925 update_link_current_pts(li, pts);
278 8323 filter_unblock(link->dst);
279 8323 ff_filter_set_ready(link->src, 200);
280 8323 }
281
282 1252 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
283 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
284 {
285 int ret;
286 1252 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
287
288 1252 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
289 "between the filter '%s' and the filter '%s'\n",
290 1252 filt->name, link->src->name, link->dst->name);
291
292 1252 link->dst->inputs[dstpad_idx] = NULL;
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if ((ret = avfilter_link(filt, filt_dstpad_idx, link->dst, dstpad_idx)) < 0) {
294 /* failed to link output filter to new filter */
295 link->dst->inputs[dstpad_idx] = link;
296 return ret;
297 }
298
299 /* re-hookup the link to the new destination filter we inserted */
300 1252 link->dst = filt;
301 1252 link->dstpad = &filt->input_pads[filt_srcpad_idx];
302 1252 filt->inputs[filt_srcpad_idx] = link;
303
304 /* if any information on supported media formats already exists on the
305 * link, we need to preserve that */
306
1/2
✓ Branch 0 taken 1252 times.
✗ Branch 1 not taken.
1252 if (link->outcfg.formats)
307 1252 ff_formats_changeref(&link->outcfg.formats,
308 1252 &filt->outputs[filt_dstpad_idx]->outcfg.formats);
309
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 630 times.
1252 if (link->outcfg.color_spaces)
310 622 ff_formats_changeref(&link->outcfg.color_spaces,
311 622 &filt->outputs[filt_dstpad_idx]->outcfg.color_spaces);
312
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 630 times.
1252 if (link->outcfg.color_ranges)
313 622 ff_formats_changeref(&link->outcfg.color_ranges,
314 622 &filt->outputs[filt_dstpad_idx]->outcfg.color_ranges);
315
2/2
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 622 times.
1252 if (link->outcfg.samplerates)
316 630 ff_formats_changeref(&link->outcfg.samplerates,
317 630 &filt->outputs[filt_dstpad_idx]->outcfg.samplerates);
318
2/2
✓ Branch 0 taken 630 times.
✓ Branch 1 taken 622 times.
1252 if (link->outcfg.channel_layouts)
319 630 ff_channel_layouts_changeref(&link->outcfg.channel_layouts,
320 630 &filt->outputs[filt_dstpad_idx]->outcfg.channel_layouts);
321
322 1252 return 0;
323 }
324
325 43970 int ff_filter_config_links(AVFilterContext *filter)
326 {
327 int (*config_link)(AVFilterLink *);
328 unsigned i;
329 int ret;
330
331
2/2
✓ Branch 0 taken 35978 times.
✓ Branch 1 taken 43970 times.
79948 for (i = 0; i < filter->nb_inputs; i ++) {
332 35978 AVFilterLink *link = filter->inputs[i];
333 AVFilterLink *inlink;
334 35978 FilterLinkInternal *li = ff_link_internal(link);
335 FilterLinkInternal *li_in;
336
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35978 times.
35978 if (!link) continue;
338
2/4
✓ Branch 0 taken 35978 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35978 times.
35978 if (!link->src || !link->dst) {
339 av_log(filter, AV_LOG_ERROR,
340 "Not all input and output are properly linked (%d).\n", i);
341 return AVERROR(EINVAL);
342 }
343
344
2/2
✓ Branch 0 taken 27810 times.
✓ Branch 1 taken 8168 times.
35978 inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
345
2/2
✓ Branch 0 taken 27810 times.
✓ Branch 1 taken 8168 times.
35978 li_in = inlink ? ff_link_internal(inlink) : NULL;
346 35978 li->l.current_pts =
347 35978 li->l.current_pts_us = AV_NOPTS_VALUE;
348
349
2/4
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35873 times.
✗ Branch 3 not taken.
35978 switch (li->init_state) {
350 105 case AVLINK_INIT:
351 105 continue;
352 case AVLINK_STARTINIT:
353 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
354 return 0;
355 35873 case AVLINK_UNINIT:
356 35873 li->init_state = AVLINK_STARTINIT;
357
358
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35873 times.
35873 if ((ret = ff_filter_config_links(link->src)) < 0)
359 return ret;
360
361
2/2
✓ Branch 0 taken 18792 times.
✓ Branch 1 taken 17081 times.
35873 if (!(config_link = link->srcpad->config_props)) {
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18792 times.
18792 if (link->src->nb_inputs != 1) {
363 av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
364 "with more than one input "
365 "must set config_props() "
366 "callbacks on all outputs\n");
367 return AVERROR(EINVAL);
368 }
369 }
370
371 /* Copy side data before link->srcpad->config_props() is called, so the filter
372 * may remove it for the next filter in the chain */
373
5/6
✓ Branch 0 taken 27734 times.
✓ Branch 1 taken 8139 times.
✓ Branch 2 taken 248 times.
✓ Branch 3 taken 27486 times.
✓ Branch 4 taken 248 times.
✗ Branch 5 not taken.
35873 if (inlink && inlink->nb_side_data && !link->nb_side_data) {
374
2/2
✓ Branch 0 taken 255 times.
✓ Branch 1 taken 248 times.
503 for (int j = 0; j < inlink->nb_side_data; j++) {
375 255 ret = av_frame_side_data_clone(&link->side_data, &link->nb_side_data,
376 255 inlink->side_data[j], 0);
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 255 times.
255 if (ret < 0) {
378 av_frame_side_data_free(&link->side_data, &link->nb_side_data);
379 return ret;
380 }
381 }
382 }
383
384
3/4
✓ Branch 0 taken 17081 times.
✓ Branch 1 taken 18792 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17081 times.
35873 if (config_link && (ret = config_link(link)) < 0) {
385 av_log(link->src, AV_LOG_ERROR,
386 "Failed to configure output pad on %s\n",
387 link->src->name);
388 return ret;
389 }
390
391
2/3
✓ Branch 0 taken 29969 times.
✓ Branch 1 taken 5904 times.
✗ Branch 2 not taken.
35873 switch (link->type) {
392 29969 case AVMEDIA_TYPE_VIDEO:
393
3/4
✓ Branch 0 taken 22940 times.
✓ Branch 1 taken 7029 times.
✓ Branch 2 taken 22940 times.
✗ Branch 3 not taken.
29969 if (!link->time_base.num && !link->time_base.den)
394
1/2
✓ Branch 0 taken 22940 times.
✗ Branch 1 not taken.
22940 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
395
396
4/4
✓ Branch 0 taken 25684 times.
✓ Branch 1 taken 4285 times.
✓ Branch 2 taken 16319 times.
✓ Branch 3 taken 9365 times.
29969 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
397 16319 link->sample_aspect_ratio = inlink ?
398
2/2
✓ Branch 0 taken 16305 times.
✓ Branch 1 taken 14 times.
16319 inlink->sample_aspect_ratio : (AVRational){1,1};
399
400
2/2
✓ Branch 0 taken 23193 times.
✓ Branch 1 taken 6776 times.
29969 if (inlink) {
401
3/4
✓ Branch 0 taken 22983 times.
✓ Branch 1 taken 210 times.
✓ Branch 2 taken 22983 times.
✗ Branch 3 not taken.
23193 if (!li->l.frame_rate.num && !li->l.frame_rate.den)
402 22983 li->l.frame_rate = li_in->l.frame_rate;
403
2/2
✓ Branch 0 taken 15835 times.
✓ Branch 1 taken 7358 times.
23193 if (!link->w)
404 15835 link->w = inlink->w;
405
2/2
✓ Branch 0 taken 15835 times.
✓ Branch 1 taken 7358 times.
23193 if (!link->h)
406 15835 link->h = inlink->h;
407
2/4
✓ Branch 0 taken 6776 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6776 times.
6776 } else if (!link->w || !link->h) {
408 av_log(link->src, AV_LOG_ERROR,
409 "Video source filters must set their output link's "
410 "width and height\n");
411 return AVERROR(EINVAL);
412 }
413 29969 break;
414
415 5904 case AVMEDIA_TYPE_AUDIO:
416
2/2
✓ Branch 0 taken 4541 times.
✓ Branch 1 taken 1363 times.
5904 if (inlink) {
417
3/4
✓ Branch 0 taken 3045 times.
✓ Branch 1 taken 1496 times.
✓ Branch 2 taken 3045 times.
✗ Branch 3 not taken.
4541 if (!link->time_base.num && !link->time_base.den)
418 3045 link->time_base = inlink->time_base;
419 }
420
421
3/4
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 5822 times.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
5904 if (!link->time_base.num && !link->time_base.den)
422 82 link->time_base = (AVRational) {1, link->sample_rate};
423 }
424
425
2/2
✓ Branch 0 taken 27734 times.
✓ Branch 1 taken 8139 times.
35873 if (link->src->nb_inputs &&
426
1/2
✓ Branch 1 taken 27734 times.
✗ Branch 2 not taken.
27734 !(fffilter(link->src->filter)->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
427 27734 FilterLink *l0 = ff_filter_link(link->src->inputs[0]);
428
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27734 times.
27734 av_assert0(!li->l.hw_frames_ctx &&
430 "should not be set by non-hwframe-aware filter");
431
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27734 times.
27734 if (l0->hw_frames_ctx) {
433 li->l.hw_frames_ctx = av_buffer_ref(l0->hw_frames_ctx);
434 if (!li->l.hw_frames_ctx)
435 return AVERROR(ENOMEM);
436 }
437 }
438
439
2/2
✓ Branch 0 taken 4764 times.
✓ Branch 1 taken 31109 times.
35873 if ((config_link = link->dstpad->config_props))
440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4764 times.
4764 if ((ret = config_link(link)) < 0) {
441 av_log(link->dst, AV_LOG_ERROR,
442 "Failed to configure input pad on %s\n",
443 link->dst->name);
444 return ret;
445 }
446
447 35873 li->init_state = AVLINK_INIT;
448 }
449 }
450
451 43970 return 0;
452 }
453
454 #ifdef TRACE
455 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
456 {
457 if (link->type == AVMEDIA_TYPE_VIDEO) {
458 ff_tlog(ctx,
459 "link[%p s:%dx%d fmt:%s %s->%s]%s",
460 link, link->w, link->h,
461 av_get_pix_fmt_name(link->format),
462 link->src ? link->src->filter->name : "",
463 link->dst ? link->dst->filter->name : "",
464 end ? "\n" : "");
465 } else {
466 char buf[128];
467 av_channel_layout_describe(&link->ch_layout, buf, sizeof(buf));
468
469 ff_tlog(ctx,
470 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
471 link, (int)link->sample_rate, buf,
472 av_get_sample_fmt_name(link->format),
473 link->src ? link->src->filter->name : "",
474 link->dst ? link->dst->filter->name : "",
475 end ? "\n" : "");
476 }
477 }
478 #endif
479
480 696444 int ff_request_frame(AVFilterLink *link)
481 {
482 696444 FilterLinkInternal * const li = ff_link_internal(link);
483
484 FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
485
486 av_assert1(!fffilter(link->dst->filter)->activate);
487
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 696435 times.
696444 if (li->status_out)
488 9 return li->status_out;
489
2/2
✓ Branch 0 taken 5926 times.
✓ Branch 1 taken 690509 times.
696435 if (li->status_in) {
490
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5926 times.
5926 if (ff_framequeue_queued_frames(&li->fifo)) {
491 av_assert1(!li->frame_wanted_out);
492 av_assert1(fffilterctx(link->dst)->ready >= 300);
493 return 0;
494 } else {
495 /* Acknowledge status change. Filters using ff_request_frame() will
496 handle the change automatically. Filters can also check the
497 status directly but none do yet. */
498 5926 link_set_out_status(link, li->status_in, li->status_in_pts);
499 5926 return li->status_out;
500 }
501 }
502 690509 li->frame_wanted_out = 1;
503 690509 ff_filter_set_ready(link->src, 100);
504 690509 return 0;
505 }
506
507 5927 static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base)
508 {
509 unsigned i;
510 5927 int64_t r = INT64_MAX;
511
512
2/2
✓ Branch 0 taken 5926 times.
✓ Branch 1 taken 5927 times.
11853 for (i = 0; i < ctx->nb_inputs; i++) {
513 5926 FilterLinkInternal * const li = ff_link_internal(ctx->inputs[i]);
514
1/2
✓ Branch 0 taken 5926 times.
✗ Branch 1 not taken.
5926 if (li->status_out == status)
515 5926 r = FFMIN(r, av_rescale_q(li->l.current_pts, ctx->inputs[i]->time_base, link_time_base));
516 }
517
2/2
✓ Branch 0 taken 5926 times.
✓ Branch 1 taken 1 times.
5927 if (r < INT64_MAX)
518 5926 return r;
519 1 av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (i = 0; i < ctx->nb_inputs; i++) {
521 FilterLinkInternal * const li = ff_link_internal(ctx->inputs[i]);
522 r = FFMIN(r, av_rescale_q(li->status_in_pts, ctx->inputs[i]->time_base, link_time_base));
523 }
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (r < INT64_MAX)
525 return r;
526 1 return AV_NOPTS_VALUE;
527 }
528
529 696779 static int request_frame_to_filter(AVFilterLink *link)
530 {
531 696779 FilterLinkInternal * const li = ff_link_internal(link);
532 696779 int ret = -1;
533
534 FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);
535 /* Assume the filter is blocked, let the method clear it if not */
536 696779 li->frame_blocked_in = 1;
537
2/2
✓ Branch 0 taken 2856 times.
✓ Branch 1 taken 693923 times.
696779 if (link->srcpad->request_frame)
538 2856 ret = link->srcpad->request_frame(link);
539
1/2
✓ Branch 0 taken 693923 times.
✗ Branch 1 not taken.
693923 else if (link->src->inputs[0])
540 693923 ret = ff_request_frame(link->src->inputs[0]);
541
2/2
✓ Branch 0 taken 5927 times.
✓ Branch 1 taken 690852 times.
696779 if (ret < 0) {
542
2/4
✓ Branch 0 taken 5927 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5927 times.
✗ Branch 3 not taken.
5927 if (ret != AVERROR(EAGAIN) && ret != li->status_in)
543 5927 ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));
544
1/2
✓ Branch 0 taken 5927 times.
✗ Branch 1 not taken.
5927 if (ret == AVERROR_EOF)
545 5927 ret = 0;
546 }
547 696779 return ret;
548 }
549
550 static const char *const var_names[] = {
551 "t",
552 "n",
553 "w",
554 "h",
555 NULL
556 };
557
558 enum {
559 VAR_T,
560 VAR_N,
561 VAR_W,
562 VAR_H,
563 VAR_VARS_NB
564 };
565
566 6 static int set_enable_expr(FFFilterContext *ctxi, const char *expr)
567 {
568 6 AVFilterContext *ctx = &ctxi->p;
569 int ret;
570 char *expr_dup;
571 6 AVExpr *old = ctxi->enable;
572
573
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
574 av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
575 "with filter '%s'\n", ctx->filter->name);
576 return AVERROR_PATCHWELCOME;
577 }
578
579 6 expr_dup = av_strdup(expr);
580
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!expr_dup)
581 return AVERROR(ENOMEM);
582
583
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!ctxi->var_values) {
584 6 ctxi->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctxi->var_values));
585
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ctxi->var_values) {
586 av_free(expr_dup);
587 return AVERROR(ENOMEM);
588 }
589 }
590
591 6 ret = av_expr_parse(&ctxi->enable, expr_dup, var_names,
592 NULL, NULL, NULL, NULL, 0, ctx->priv);
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
594 av_log(ctx->priv, AV_LOG_ERROR,
595 "Error when evaluating the expression '%s' for enable\n",
596 expr_dup);
597 av_free(expr_dup);
598 return ret;
599 }
600
601 6 av_expr_free(old);
602 6 av_free(ctx->enable_str);
603 6 ctx->enable_str = expr_dup;
604 6 return 0;
605 }
606
607 int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
608 {
609 if(!strcmp(cmd, "ping")){
610 char local_res[256] = {0};
611
612 if (!res) {
613 res = local_res;
614 res_len = sizeof(local_res);
615 }
616 av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
617 if (res == local_res)
618 av_log(filter, AV_LOG_INFO, "%s", res);
619 return 0;
620 }else if(!strcmp(cmd, "enable")) {
621 return set_enable_expr(fffilterctx(filter), arg);
622 }else if (fffilter(filter->filter)->process_command) {
623 return fffilter(filter->filter)->process_command(filter, cmd, arg, res, res_len, flags);
624 }
625 return AVERROR(ENOSYS);
626 }
627
628 14516 unsigned avfilter_filter_pad_count(const AVFilter *filter, int is_output)
629 {
630
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14516 times.
14516 return is_output ? fffilter(filter)->nb_outputs : fffilter(filter)->nb_inputs;
631 }
632
633 557 static const char *default_filter_name(void *filter_ctx)
634 {
635 557 AVFilterContext *ctx = filter_ctx;
636
1/2
✓ Branch 0 taken 557 times.
✗ Branch 1 not taken.
557 return ctx->name ? ctx->name : ctx->filter->name;
637 }
638
639 55204 static void *filter_child_next(void *obj, void *prev)
640 {
641 55204 AVFilterContext *ctx = obj;
642
5/8
✓ Branch 0 taken 55198 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 55198 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55198 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 55198 times.
✗ Branch 7 not taken.
55204 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
643 55198 return ctx->priv;
644 6 return NULL;
645 }
646
647 static const AVClass *filter_child_class_iterate(void **iter)
648 {
649 const AVFilter *f;
650
651 while ((f = av_filter_iterate(iter)))
652 if (f->priv_class)
653 return f->priv_class;
654
655 return NULL;
656 }
657
658 #define OFFSET(x) offsetof(AVFilterContext, x)
659 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
660 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
661 static const AVOption avfilter_options[] = {
662 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
663 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, .unit = "thread_type" },
664 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
665 { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = TFLAGS },
666 { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
667 { .i64 = 0 }, 0, INT_MAX, FLAGS, .unit = "threads" },
668 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = FLAGS, .unit = "threads"},
669 { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
670 OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
671 { NULL },
672 };
673
674 static const AVClass avfilter_class = {
675 .class_name = "AVFilter",
676 .item_name = default_filter_name,
677 .version = LIBAVUTIL_VERSION_INT,
678 .category = AV_CLASS_CATEGORY_FILTER,
679 .child_next = filter_child_next,
680 .child_class_iterate = filter_child_class_iterate,
681 .option = avfilter_options,
682 .state_flags_offset = offsetof(FFFilterContext, state_flags),
683 };
684
685 733 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
686 int *ret, int nb_jobs)
687 {
688 int i;
689
690
2/2
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 733 times.
1466 for (i = 0; i < nb_jobs; i++) {
691 733 int r = func(ctx, arg, i, nb_jobs);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 733 times.
733 if (ret)
693 ret[i] = r;
694 }
695 733 return 0;
696 }
697
698 61844 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
699 {
700 FFFilterContext *ctx;
701 AVFilterContext *ret;
702 61844 const FFFilter *const fi = fffilter(filter);
703 61844 int preinited = 0;
704
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 if (!filter)
706 return NULL;
707
708 61844 ctx = av_mallocz(sizeof(*ctx));
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 if (!ctx)
710 return NULL;
711 61844 ret = &ctx->p;
712
713 61844 ret->av_class = &avfilter_class;
714 61844 ret->filter = filter;
715
1/2
✓ Branch 0 taken 61844 times.
✗ Branch 1 not taken.
61844 ret->name = inst_name ? av_strdup(inst_name) : NULL;
716
2/2
✓ Branch 0 taken 55153 times.
✓ Branch 1 taken 6691 times.
61844 if (fi->priv_size) {
717 55153 ret->priv = av_mallocz(fi->priv_size);
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55153 times.
55153 if (!ret->priv)
719 goto err;
720 }
721
2/2
✓ Branch 0 taken 13271 times.
✓ Branch 1 taken 48573 times.
61844 if (fi->preinit) {
722
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13271 times.
13271 if (fi->preinit(ret) < 0)
723 goto err;
724 13271 preinited = 1;
725 }
726
727 61844 av_opt_set_defaults(ret);
728
2/2
✓ Branch 0 taken 53944 times.
✓ Branch 1 taken 7900 times.
61844 if (filter->priv_class) {
729 53944 *(const AVClass**)ret->priv = filter->priv_class;
730 53944 av_opt_set_defaults(ret->priv);
731 }
732
733 61844 ctx->execute = default_execute;
734
735 61844 ret->nb_inputs = fi->nb_inputs;
736
2/2
✓ Branch 0 taken 52430 times.
✓ Branch 1 taken 9414 times.
61844 if (ret->nb_inputs ) {
737 52430 ret->input_pads = av_memdup(filter->inputs, ret->nb_inputs * sizeof(*filter->inputs));
738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52430 times.
52430 if (!ret->input_pads)
739 goto err;
740 52430 ret->inputs = av_calloc(ret->nb_inputs, sizeof(*ret->inputs));
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52430 times.
52430 if (!ret->inputs)
742 goto err;
743 }
744
745 61844 ret->nb_outputs = fi->nb_outputs;
746
2/2
✓ Branch 0 taken 53665 times.
✓ Branch 1 taken 8179 times.
61844 if (ret->nb_outputs) {
747 53665 ret->output_pads = av_memdup(filter->outputs, ret->nb_outputs * sizeof(*filter->outputs));
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53665 times.
53665 if (!ret->output_pads)
749 goto err;
750 53665 ret->outputs = av_calloc(ret->nb_outputs, sizeof(*ret->outputs));
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53665 times.
53665 if (!ret->outputs)
752 goto err;
753 }
754
755 61844 return ret;
756
757 err:
758 if (preinited)
759 fi->uninit(ret);
760 av_freep(&ret->inputs);
761 av_freep(&ret->input_pads);
762 ret->nb_inputs = 0;
763 av_freep(&ret->outputs);
764 av_freep(&ret->output_pads);
765 ret->nb_outputs = 0;
766 av_freep(&ret->priv);
767 av_free(ret);
768 return NULL;
769 }
770
771 106690 static void free_link(AVFilterLink *link)
772 {
773
2/2
✓ Branch 0 taken 60691 times.
✓ Branch 1 taken 45999 times.
106690 if (!link)
774 60691 return;
775
776
2/2
✓ Branch 0 taken 45867 times.
✓ Branch 1 taken 132 times.
45999 if (link->src)
777 45867 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
778
2/2
✓ Branch 0 taken 45867 times.
✓ Branch 1 taken 132 times.
45999 if (link->dst)
779 45867 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
780
781 45999 ff_formats_unref(&link->incfg.formats);
782 45999 ff_formats_unref(&link->outcfg.formats);
783 45999 ff_formats_unref(&link->incfg.color_spaces);
784 45999 ff_formats_unref(&link->outcfg.color_spaces);
785 45999 ff_formats_unref(&link->incfg.color_ranges);
786 45999 ff_formats_unref(&link->outcfg.color_ranges);
787 45999 ff_formats_unref(&link->incfg.samplerates);
788 45999 ff_formats_unref(&link->outcfg.samplerates);
789 45999 ff_channel_layouts_unref(&link->incfg.channel_layouts);
790 45999 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
791 45999 link_free(&link);
792 }
793
794 61844 void avfilter_free(AVFilterContext *filter)
795 {
796 FFFilterContext *ctxi;
797 int i;
798
799
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 if (!filter)
800 return;
801 61844 ctxi = fffilterctx(filter);
802
803
1/2
✓ Branch 0 taken 61844 times.
✗ Branch 1 not taken.
61844 if (filter->graph)
804 61844 ff_filter_graph_remove_filter(filter->graph, filter);
805
806
2/2
✓ Branch 1 taken 48048 times.
✓ Branch 2 taken 13796 times.
61844 if (fffilter(filter->filter)->uninit)
807 48048 fffilter(filter->filter)->uninit(filter);
808
809
2/2
✓ Branch 0 taken 52753 times.
✓ Branch 1 taken 61844 times.
114597 for (i = 0; i < filter->nb_inputs; i++) {
810 52753 free_link(filter->inputs[i]);
811
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 52543 times.
52753 if (filter->input_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
812 210 av_freep(&filter->input_pads[i].name);
813 }
814
2/2
✓ Branch 0 taken 53937 times.
✓ Branch 1 taken 61844 times.
115781 for (i = 0; i < filter->nb_outputs; i++) {
815 53937 free_link(filter->outputs[i]);
816
2/2
✓ Branch 0 taken 267 times.
✓ Branch 1 taken 53670 times.
53937 if (filter->output_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
817 267 av_freep(&filter->output_pads[i].name);
818 }
819
820
2/2
✓ Branch 0 taken 53944 times.
✓ Branch 1 taken 7900 times.
61844 if (filter->filter->priv_class)
821 53944 av_opt_free(filter->priv);
822
823 61844 av_buffer_unref(&filter->hw_device_ctx);
824
825 61844 av_freep(&filter->name);
826 61844 av_freep(&filter->input_pads);
827 61844 av_freep(&filter->output_pads);
828 61844 av_freep(&filter->inputs);
829 61844 av_freep(&filter->outputs);
830 61844 av_freep(&filter->priv);
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 while (ctxi->command_queue)
832 command_queue_pop(filter);
833 61844 av_opt_free(filter);
834 61844 av_expr_free(ctxi->enable);
835 61844 ctxi->enable = NULL;
836 61844 av_freep(&ctxi->var_values);
837 61844 av_free(filter);
838 }
839
840 17306 int ff_filter_get_nb_threads(AVFilterContext *ctx)
841 {
842
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17306 times.
17306 if (ctx->nb_threads > 0)
843 return FFMIN(ctx->nb_threads, ctx->graph->nb_threads);
844 17306 return ctx->graph->nb_threads;
845 }
846
847 33052 int ff_filter_opt_parse(void *logctx, const AVClass *priv_class,
848 AVDictionary **options, const char *args)
849 {
850 33052 const AVOption *o = NULL;
851 int ret;
852 33052 int offset= -1;
853
854
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33052 times.
33052 if (!args)
855 return 0;
856
857
2/2
✓ Branch 0 taken 56991 times.
✓ Branch 1 taken 33052 times.
90043 while (*args) {
858 char *parsed_key, *value;
859 const char *key;
860 56991 const char *shorthand = NULL;
861 56991 int additional_flags = 0;
862
863
4/4
✓ Branch 0 taken 44319 times.
✓ Branch 1 taken 12672 times.
✓ Branch 3 taken 43037 times.
✓ Branch 4 taken 1282 times.
56991 if (priv_class && (o = av_opt_next(&priv_class, o))) {
864
4/4
✓ Branch 0 taken 41267 times.
✓ Branch 1 taken 1770 times.
✓ Branch 2 taken 4404 times.
✓ Branch 3 taken 36863 times.
43037 if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
865 6174 continue;
866 36863 offset = o->offset;
867 36863 shorthand = o->name;
868 }
869
870 50817 ret = av_opt_get_key_value(&args, "=", ":",
871 shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
872 &parsed_key, &value);
873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50817 times.
50817 if (ret < 0) {
874 if (ret == AVERROR(EINVAL))
875 av_log(logctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
876 else
877 av_log(logctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
878 av_err2str(ret));
879 return ret;
880 }
881
2/2
✓ Branch 0 taken 26449 times.
✓ Branch 1 taken 24368 times.
50817 if (*args)
882 26449 args++;
883
2/2
✓ Branch 0 taken 30509 times.
✓ Branch 1 taken 20308 times.
50817 if (parsed_key) {
884 30509 key = parsed_key;
885 30509 additional_flags = AV_DICT_DONT_STRDUP_KEY;
886 30509 priv_class = NULL; /* reject all remaining shorthand */
887 } else {
888 20308 key = shorthand;
889 }
890
891 50817 av_log(logctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
892
893 50817 av_dict_set(options, key, value,
894 additional_flags | AV_DICT_DONT_STRDUP_VAL | AV_DICT_MULTIKEY);
895 }
896
897 33052 return 0;
898 }
899
900 int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
901 const char *arg, char *res, int res_len, int flags)
902 {
903 const AVOption *o;
904
905 if (!ctx->filter->priv_class)
906 return 0;
907 o = av_opt_find2(ctx->priv, cmd, NULL, AV_OPT_FLAG_RUNTIME_PARAM | AV_OPT_FLAG_FILTERING_PARAM, AV_OPT_SEARCH_CHILDREN, NULL);
908 if (!o)
909 return AVERROR(ENOSYS);
910 return av_opt_set(ctx->priv, cmd, arg, 0);
911 }
912
913 61844 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
914 {
915 61844 FFFilterContext *ctxi = fffilterctx(ctx);
916 61844 int ret = 0;
917
918
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 if (ctxi->state_flags & AV_CLASS_STATE_INITIALIZED) {
919 av_log(ctx, AV_LOG_ERROR, "Filter already initialized\n");
920 return AVERROR(EINVAL);
921 }
922
923 61844 ret = av_opt_set_dict2(ctx, options, AV_OPT_SEARCH_CHILDREN);
924
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 if (ret < 0) {
925 av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
926 return ret;
927 }
928
929
2/2
✓ Branch 0 taken 1197 times.
✓ Branch 1 taken 60647 times.
61844 if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
930
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 1050 times.
1197 ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
931
1/2
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
147 fffiltergraph(ctx->graph)->thread_execute) {
932 147 ctx->thread_type = AVFILTER_THREAD_SLICE;
933 147 ctxi->execute = fffiltergraph(ctx->graph)->thread_execute;
934 } else {
935 61697 ctx->thread_type = 0;
936 }
937
938
2/2
✓ Branch 1 taken 48797 times.
✓ Branch 2 taken 13047 times.
61844 if (fffilter(ctx->filter)->init)
939 48797 ret = fffilter(ctx->filter)->init(ctx);
940
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61844 times.
61844 if (ret < 0)
941 return ret;
942
943
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 61838 times.
61844 if (ctx->enable_str) {
944 6 ret = set_enable_expr(ctxi, ctx->enable_str);
945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
946 return ret;
947 }
948
949 61844 ctxi->state_flags |= AV_CLASS_STATE_INITIALIZED;
950
951 61844 return 0;
952 }
953
954 20253 int avfilter_init_str(AVFilterContext *filter, const char *args)
955 {
956 20253 AVDictionary *options = NULL;
957 const AVDictionaryEntry *e;
958 20253 int ret = 0;
959
960
3/4
✓ Branch 0 taken 10130 times.
✓ Branch 1 taken 10123 times.
✓ Branch 2 taken 10130 times.
✗ Branch 3 not taken.
20253 if (args && *args) {
961 10130 ret = ff_filter_opt_parse(filter, filter->filter->priv_class, &options, args);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10130 times.
10130 if (ret < 0)
963 goto fail;
964 }
965
966 20253 ret = avfilter_init_dict(filter, &options);
967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20253 times.
20253 if (ret < 0)
968 goto fail;
969
970
1/2
✓ Branch 1 taken 20253 times.
✗ Branch 2 not taken.
20253 if ((e = av_dict_iterate(options, NULL))) {
971 av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
972 ret = AVERROR_OPTION_NOT_FOUND;
973 goto fail;
974 }
975
976 20253 fail:
977 20253 av_dict_free(&options);
978
979 20253 return ret;
980 }
981
982 14822 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
983 {
984 14822 return pads[pad_idx].name;
985 }
986
987 29919 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
988 {
989 29919 return pads[pad_idx].type;
990 }
991
992 AVBufferRef *avfilter_link_get_hw_frames_ctx(AVFilterLink *link)
993 {
994 FilterLink *plink = ff_filter_link(link);
995 if (plink->hw_frames_ctx)
996 return av_buffer_ref(plink->hw_frames_ctx);
997
998 return NULL;
999 }
1000
1001 682712 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
1002 {
1003 682712 return ff_filter_frame(link->dst->outputs[0], frame);
1004 }
1005
1006 /**
1007 * Evaluate the timeline expression of the link for the time and properties
1008 * of the frame.
1009 * @return >0 if enabled, 0 if disabled
1010 * @note It does not update link->dst->is_disabled.
1011 */
1012 2176505 static int evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
1013 {
1014 2176505 FilterLink *l = ff_filter_link(link);
1015 2176505 AVFilterContext *dstctx = link->dst;
1016 2176505 FFFilterContext *dsti = fffilterctx(dstctx);
1017 2176505 int64_t pts = frame->pts;
1018
1019
2/2
✓ Branch 0 taken 2176309 times.
✓ Branch 1 taken 196 times.
2176505 if (!dstctx->enable_str)
1020 2176309 return 1;
1021
1022 196 dsti->var_values[VAR_N] = l->frame_count_out;
1023
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);
1024 196 dsti->var_values[VAR_W] = link->w;
1025 196 dsti->var_values[VAR_H] = link->h;
1026
1027 196 return fabs(av_expr_eval(dsti->enable, dsti->var_values, NULL)) >= 0.5;
1028 }
1029
1030 700770 static int filter_frame_framed(AVFilterLink *link, AVFrame *frame)
1031 {
1032 700770 FilterLink *l = ff_filter_link(link);
1033 int (*filter_frame)(AVFilterLink *, AVFrame *);
1034 700770 AVFilterContext *dstctx = link->dst;
1035 700770 AVFilterPad *dst = link->dstpad;
1036 int ret;
1037
1038
2/2
✓ Branch 0 taken 682703 times.
✓ Branch 1 taken 18067 times.
700770 if (!(filter_frame = dst->filter_frame))
1039 682703 filter_frame = default_filter_frame;
1040
1041
2/2
✓ Branch 0 taken 2551 times.
✓ Branch 1 taken 698219 times.
700770 if (dst->flags & AVFILTERPAD_FLAG_NEEDS_WRITABLE) {
1042 2551 ret = ff_inlink_make_frame_writable(link, &frame);
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2551 times.
2551 if (ret < 0)
1044 goto fail;
1045 }
1046
1047 700770 ff_inlink_process_commands(link, frame);
1048 700770 dstctx->is_disabled = !evaluate_timeline_at_frame(link, frame);
1049
1050
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 700751 times.
700770 if (dstctx->is_disabled &&
1051
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
1052 9 filter_frame = default_filter_frame;
1053 700770 ret = filter_frame(link, frame);
1054 700770 l->frame_count_out++;
1055 700770 return ret;
1056
1057 fail:
1058 av_frame_free(&frame);
1059 return ret;
1060 }
1061
1062 1477657 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
1063 {
1064 1477657 FilterLinkInternal * const li = ff_link_internal(link);
1065 int ret;
1066 1477657 FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); tlog_ref(NULL, frame, 1);
1067
1068 /* Consistency checks */
1069
2/2
✓ Branch 0 taken 549463 times.
✓ Branch 1 taken 928194 times.
1477657 if (link->type == AVMEDIA_TYPE_VIDEO) {
1070
2/2
✓ Branch 0 taken 408848 times.
✓ Branch 1 taken 140615 times.
549463 if (strcmp(link->dst->filter->name, "buffersink") &&
1071
2/2
✓ Branch 0 taken 245361 times.
✓ Branch 1 taken 163487 times.
408848 strcmp(link->dst->filter->name, "format") &&
1072
2/2
✓ Branch 0 taken 245335 times.
✓ Branch 1 taken 26 times.
245361 strcmp(link->dst->filter->name, "idet") &&
1073
2/2
✓ Branch 0 taken 156113 times.
✓ Branch 1 taken 89222 times.
245335 strcmp(link->dst->filter->name, "null") &&
1074
2/2
✓ Branch 0 taken 51523 times.
✓ Branch 1 taken 104590 times.
156113 strcmp(link->dst->filter->name, "scale") &&
1075 51523 strcmp(link->dst->filter->name, "libplacebo")) {
1076 av_assert1(frame->format == link->format);
1077 av_assert1(frame->width == link->w);
1078 av_assert1(frame->height == link->h);
1079 }
1080
1081 549463 frame->sample_aspect_ratio = link->sample_aspect_ratio;
1082 } else {
1083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 928194 times.
928194 if (frame->format != link->format) {
1084 av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
1085 goto error;
1086 }
1087
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 928194 times.
928194 if (av_channel_layout_compare(&frame->ch_layout, &link->ch_layout)) {
1088 av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
1089 goto error;
1090 }
1091
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 928194 times.
928194 if (frame->sample_rate != link->sample_rate) {
1092 av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
1093 goto error;
1094 }
1095
1096 928194 frame->duration = av_rescale_q(frame->nb_samples, (AVRational){ 1, frame->sample_rate },
1097 link->time_base);
1098 }
1099
1100 1477657 li->frame_blocked_in = li->frame_wanted_out = 0;
1101 1477657 li->l.frame_count_in++;
1102 1477657 li->l.sample_count_in += frame->nb_samples;
1103 1477657 filter_unblock(link->dst);
1104 1477657 ret = ff_framequeue_add(&li->fifo, frame);
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1477657 times.
1477657 if (ret < 0) {
1106 av_frame_free(&frame);
1107 return ret;
1108 }
1109 1477657 ff_filter_set_ready(link->dst, 300);
1110 1477657 return 0;
1111
1112 error:
1113 av_frame_free(&frame);
1114 return AVERROR_PATCHWELCOME;
1115 }
1116
1117 2093171 static int samples_ready(FilterLinkInternal *link, unsigned min)
1118 {
1119
2/2
✓ Branch 1 taken 700873 times.
✓ Branch 2 taken 1392298 times.
2794044 return ff_framequeue_queued_frames(&link->fifo) &&
1120
2/2
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 700769 times.
700873 (ff_framequeue_queued_samples(&link->fifo) >= min ||
1121
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 103 times.
104 link->status_in);
1122 }
1123
1124 2482 static int take_samples(FilterLinkInternal *li, unsigned min, unsigned max,
1125 AVFrame **rframe)
1126 {
1127 2482 FilterLink *l = &li->l;
1128 2482 AVFilterLink *link = &l->pub;
1129 AVFrame *frame0, *frame, *buf;
1130 unsigned nb_samples, nb_frames, i, p;
1131 int ret;
1132
1133 /* Note: this function relies on no format changes and must only be
1134 called with enough samples. */
1135 av_assert1(samples_ready(li, l->min_samples));
1136 2482 frame0 = frame = ff_framequeue_peek(&li->fifo, 0);
1137
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) {
1138 541 *rframe = ff_framequeue_take(&li->fifo);
1139 541 return 0;
1140 }
1141 1941 nb_frames = 0;
1142 1941 nb_samples = 0;
1143 while (1) {
1144
2/2
✓ Branch 0 taken 1558 times.
✓ Branch 1 taken 1465 times.
3023 if (nb_samples + frame->nb_samples > max) {
1145
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 2 times.
1558 if (nb_samples < min)
1146 1556 nb_samples = max;
1147 1558 break;
1148 }
1149 1465 nb_samples += frame->nb_samples;
1150 1465 nb_frames++;
1151
2/2
✓ Branch 1 taken 383 times.
✓ Branch 2 taken 1082 times.
1465 if (nb_frames == ff_framequeue_queued_frames(&li->fifo))
1152 383 break;
1153 1082 frame = ff_framequeue_peek(&li->fifo, nb_frames);
1154 }
1155
1156 1941 buf = ff_get_audio_buffer(link, nb_samples);
1157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1941 times.
1941 if (!buf)
1158 return AVERROR(ENOMEM);
1159 1941 ret = av_frame_copy_props(buf, frame0);
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1941 times.
1941 if (ret < 0) {
1161 av_frame_free(&buf);
1162 return ret;
1163 }
1164
1165 1941 p = 0;
1166
2/2
✓ Branch 0 taken 1465 times.
✓ Branch 1 taken 1941 times.
3406 for (i = 0; i < nb_frames; i++) {
1167 1465 frame = ff_framequeue_take(&li->fifo);
1168 1465 av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
1169 1465 frame->nb_samples, link->ch_layout.nb_channels, link->format);
1170 1465 p += frame->nb_samples;
1171 1465 av_frame_free(&frame);
1172 }
1173
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 385 times.
1941 if (p < nb_samples) {
1174 1556 unsigned n = nb_samples - p;
1175 1556 frame = ff_framequeue_peek(&li->fifo, 0);
1176 1556 av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
1177 1556 link->ch_layout.nb_channels, link->format);
1178 1556 ff_framequeue_skip_samples(&li->fifo, n, link->time_base);
1179 }
1180
1181 1941 *rframe = buf;
1182 1941 return 0;
1183 }
1184
1185 700770 static int filter_frame_to_filter(AVFilterLink *link)
1186 {
1187 700770 FilterLinkInternal * const li = ff_link_internal(link);
1188 700770 AVFrame *frame = NULL;
1189 700770 AVFilterContext *dst = link->dst;
1190 int ret;
1191
1192 av_assert1(ff_framequeue_queued_frames(&li->fifo));
1193 1401540 ret = li->l.min_samples ?
1194
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 700734 times.
700770 ff_inlink_consume_samples(link, li->l.min_samples, li->l.max_samples, &frame) :
1195 700734 ff_inlink_consume_frame(link, &frame);
1196 av_assert1(ret);
1197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 700770 times.
700770 if (ret < 0) {
1198 av_assert1(!frame);
1199 return ret;
1200 }
1201 /* The filter will soon have received a new frame, that may allow it to
1202 produce one or more: unblock its outputs. */
1203 700770 filter_unblock(dst);
1204 /* AVFilterPad.filter_frame() expect frame_count_out to have the value
1205 before the frame; filter_frame_framed() will re-increment it. */
1206 700770 li->l.frame_count_out--;
1207 700770 ret = filter_frame_framed(link, frame);
1208
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 700770 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
700770 if (ret < 0 && ret != li->status_out) {
1209 link_set_out_status(link, ret, AV_NOPTS_VALUE);
1210 } else {
1211 /* Run once again, to see if several frames were available, or if
1212 the input status has also changed, or any other reason. */
1213 700770 ff_filter_set_ready(dst, 300);
1214 }
1215 700770 return ret;
1216 }
1217
1218 5926 static int forward_status_change(AVFilterContext *filter, FilterLinkInternal *li_in)
1219 {
1220 5926 AVFilterLink *in = &li_in->l.pub;
1221 5926 unsigned out = 0, progress = 0;
1222 int ret;
1223
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5926 times.
5926 av_assert0(!li_in->status_out);
1225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5926 times.
5926 if (!filter->nb_outputs) {
1226 /* not necessary with the current API and sinks */
1227 return 0;
1228 }
1229
2/2
✓ Branch 0 taken 5926 times.
✓ Branch 1 taken 5926 times.
11852 while (!li_in->status_out) {
1230 5926 FilterLinkInternal *li_out = ff_link_internal(filter->outputs[out]);
1231
1232
1/2
✓ Branch 0 taken 5926 times.
✗ Branch 1 not taken.
5926 if (!li_out->status_in) {
1233 5926 progress++;
1234 5926 ret = request_frame_to_filter(filter->outputs[out]);
1235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5926 times.
5926 if (ret < 0)
1236 return ret;
1237 }
1238
1/2
✓ Branch 0 taken 5926 times.
✗ Branch 1 not taken.
5926 if (++out == filter->nb_outputs) {
1239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5926 times.
5926 if (!progress) {
1240 /* Every output already closed: input no longer interesting
1241 (example: overlay in shortest mode, other input closed). */
1242 link_set_out_status(in, li_in->status_in, li_in->status_in_pts);
1243 return 0;
1244 }
1245 5926 progress = 0;
1246 5926 out = 0;
1247 }
1248 }
1249 5926 ff_filter_set_ready(filter, 200);
1250 5926 return 0;
1251 }
1252
1253 2100192 static int filter_activate_default(AVFilterContext *filter)
1254 {
1255 unsigned i;
1256
1257
2/2
✓ Branch 0 taken 2100192 times.
✓ Branch 1 taken 2093444 times.
4193636 for (i = 0; i < filter->nb_outputs; i++) {
1258 2100192 FilterLinkInternal *li = ff_link_internal(filter->outputs[i]);
1259 2100192 int ret = li->status_in;
1260
1261
2/2
✓ Branch 0 taken 6748 times.
✓ Branch 1 taken 2093444 times.
2100192 if (ret) {
1262
2/2
✓ Branch 0 taken 6748 times.
✓ Branch 1 taken 6748 times.
13496 for (int j = 0; j < filter->nb_inputs; j++)
1263 6748 ff_inlink_set_status(filter->inputs[j], ret);
1264 6748 return 0;
1265 }
1266 }
1267
1268
2/2
✓ Branch 0 taken 2093171 times.
✓ Branch 1 taken 1392674 times.
3485845 for (i = 0; i < filter->nb_inputs; i++) {
1269 2093171 FilterLinkInternal *li = ff_link_internal(filter->inputs[i]);
1270
2/2
✓ Branch 1 taken 700770 times.
✓ Branch 2 taken 1392401 times.
2093171 if (samples_ready(li, li->l.min_samples)) {
1271 700770 return filter_frame_to_filter(filter->inputs[i]);
1272 }
1273 }
1274
2/2
✓ Branch 0 taken 1392401 times.
✓ Branch 1 taken 1386748 times.
2779149 for (i = 0; i < filter->nb_inputs; i++) {
1275 1392401 FilterLinkInternal * const li = ff_link_internal(filter->inputs[i]);
1276
4/4
✓ Branch 0 taken 5950 times.
✓ Branch 1 taken 1386451 times.
✓ Branch 2 taken 5926 times.
✓ Branch 3 taken 24 times.
1392401 if (li->status_in && !li->status_out) {
1277 av_assert1(!ff_framequeue_queued_frames(&li->fifo));
1278 5926 return forward_status_change(filter, li);
1279 }
1280 }
1281
2/2
✓ Branch 0 taken 1386748 times.
✓ Branch 1 taken 695895 times.
2082643 for (i = 0; i < filter->nb_outputs; i++) {
1282 1386748 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
1283
2/2
✓ Branch 0 taken 691004 times.
✓ Branch 1 taken 695744 times.
1386748 if (li->frame_wanted_out &&
1284
2/2
✓ Branch 0 taken 690853 times.
✓ Branch 1 taken 151 times.
691004 !li->frame_blocked_in) {
1285 690853 return request_frame_to_filter(filter->outputs[i]);
1286 }
1287 }
1288 695895 return FFERROR_NOT_READY;
1289 }
1290
1291 /*
1292 Filter scheduling and activation
1293
1294 When a filter is activated, it must:
1295 - if possible, output a frame;
1296 - else, if relevant, forward the input status change;
1297 - else, check outputs for wanted frames and forward the requests.
1298
1299 The following AVFilterLink fields are used for activation:
1300
1301 - frame_wanted_out:
1302
1303 This field indicates if a frame is needed on this input of the
1304 destination filter. A positive value indicates that a frame is needed
1305 to process queued frames or internal data or to satisfy the
1306 application; a zero value indicates that a frame is not especially
1307 needed but could be processed anyway; a negative value indicates that a
1308 frame would just be queued.
1309
1310 It is set by filters using ff_request_frame() or ff_request_no_frame(),
1311 when requested by the application through a specific API or when it is
1312 set on one of the outputs.
1313
1314 It is cleared when a frame is sent from the source using
1315 ff_filter_frame().
1316
1317 It is also cleared when a status change is sent from the source using
1318 ff_avfilter_link_set_in_status().
1319
1320 - frame_blocked_in:
1321
1322 This field means that the source filter can not generate a frame as is.
1323 Its goal is to avoid repeatedly calling the request_frame() method on
1324 the same link.
1325
1326 It is set by the framework on all outputs of a filter before activating it.
1327
1328 It is automatically cleared by ff_filter_frame().
1329
1330 It is also automatically cleared by ff_avfilter_link_set_in_status().
1331
1332 It is also cleared on all outputs (using filter_unblock()) when
1333 something happens on an input: processing a frame or changing the
1334 status.
1335
1336 - fifo:
1337
1338 Contains the frames queued on a filter input. If it contains frames and
1339 frame_wanted_out is not set, then the filter can be activated. If that
1340 result in the filter not able to use these frames, the filter must set
1341 frame_wanted_out to ask for more frames.
1342
1343 - status_in and status_in_pts:
1344
1345 Status (EOF or error code) of the link and timestamp of the status
1346 change (in link time base, same as frames) as seen from the input of
1347 the link. The status change is considered happening after the frames
1348 queued in fifo.
1349
1350 It is set by the source filter using ff_avfilter_link_set_in_status().
1351
1352 - status_out:
1353
1354 Status of the link as seen from the output of the link. The status
1355 change is considered having already happened.
1356
1357 It is set by the destination filter using
1358 link_set_out_status().
1359
1360 Filters are activated according to the ready field, set using the
1361 ff_filter_set_ready(). Eventually, a priority queue will be used.
1362 ff_filter_set_ready() is called whenever anything could cause progress to
1363 be possible. Marking a filter ready when it is not is not a problem,
1364 except for the small overhead it causes.
1365
1366 Conditions that cause a filter to be marked ready are:
1367
1368 - frames added on an input link;
1369
1370 - changes in the input or output status of an input link;
1371
1372 - requests for a frame on an output link;
1373
1374 - after any actual processing using the legacy methods (filter_frame(),
1375 and request_frame() to acknowledge status changes), to run once more
1376 and check if enough input was present for several frames.
1377
1378 Examples of scenarios to consider:
1379
1380 - buffersrc: activate if frame_wanted_out to notify the application;
1381 activate when the application adds a frame to push it immediately.
1382
1383 - testsrc: activate only if frame_wanted_out to produce and push a frame.
1384
1385 - concat (not at stitch points): can process a frame on any output.
1386 Activate if frame_wanted_out on output to forward on the corresponding
1387 input. Activate when a frame is present on input to process it
1388 immediately.
1389
1390 - framesync: needs at least one frame on each input; extra frames on the
1391 wrong input will accumulate. When a frame is first added on one input,
1392 set frame_wanted_out<0 on it to avoid getting more (would trigger
1393 testsrc) and frame_wanted_out>0 on the other to allow processing it.
1394
1395 Activation of old filters:
1396
1397 In order to activate a filter implementing the legacy filter_frame() and
1398 request_frame() methods, perform the first possible of the following
1399 actions:
1400
1401 - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
1402 frame and call filter_frame().
1403
1404 Rationale: filter frames as soon as possible instead of leaving them
1405 queued; frame_wanted_out < 0 is not possible since the old API does not
1406 set it nor provides any similar feedback; frame_wanted_out > 0 happens
1407 when min_samples > 0 and there are not enough samples queued.
1408
1409 - If an input has status_in set but not status_out, try to call
1410 request_frame() on one of the outputs in the hope that it will trigger
1411 request_frame() on the input with status_in and acknowledge it. This is
1412 awkward and fragile, filters with several inputs or outputs should be
1413 updated to direct activation as soon as possible.
1414
1415 - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
1416 request_frame().
1417
1418 Rationale: checking frame_blocked_in is necessary to avoid requesting
1419 repeatedly on a blocked input if another is not blocked (example:
1420 [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
1421 */
1422
1423 3659778 int ff_filter_activate(AVFilterContext *filter)
1424 {
1425 3659778 FFFilterContext *ctxi = fffilterctx(filter);
1426 3659778 const FFFilter *const fi = fffilter(filter->filter);
1427 int ret;
1428
1429 /* Generic timeline support is not yet implemented but should be easy */
1430 av_assert1(!(fi->p.flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
1431 fi->activate));
1432 3659778 ctxi->ready = 0;
1433
2/2
✓ Branch 0 taken 1559586 times.
✓ Branch 1 taken 2100192 times.
3659778 ret = fi->activate ? fi->activate(filter) : filter_activate_default(filter);
1434
2/2
✓ Branch 0 taken 1057101 times.
✓ Branch 1 taken 2602677 times.
3659778 if (ret == FFERROR_NOT_READY)
1435 1057101 ret = 0;
1436 3659778 return ret;
1437 }
1438
1439 3313471 int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
1440 {
1441 3313471 FilterLinkInternal * const li = ff_link_internal(link);
1442 3313471 *rpts = li->l.current_pts;
1443
2/2
✓ Branch 1 taken 1968 times.
✓ Branch 2 taken 3311503 times.
3313471 if (ff_framequeue_queued_frames(&li->fifo))
1444 1968 return *rstatus = 0;
1445
2/2
✓ Branch 0 taken 1818 times.
✓ Branch 1 taken 3309685 times.
3311503 if (li->status_out)
1446 1818 return *rstatus = li->status_out;
1447
2/2
✓ Branch 0 taken 3301920 times.
✓ Branch 1 taken 7765 times.
3309685 if (!li->status_in)
1448 3301920 return *rstatus = 0;
1449 7765 *rstatus = li->status_out = li->status_in;
1450 7765 update_link_current_pts(li, li->status_in_pts);
1451 7765 *rpts = li->l.current_pts;
1452 7765 return 1;
1453 }
1454
1455 512090 size_t ff_inlink_queued_frames(AVFilterLink *link)
1456 {
1457 512090 FilterLinkInternal * const li = ff_link_internal(link);
1458 512090 return ff_framequeue_queued_frames(&li->fifo);
1459 }
1460
1461 4527812 int ff_inlink_check_available_frame(AVFilterLink *link)
1462 {
1463 4527812 FilterLinkInternal * const li = ff_link_internal(link);
1464 4527812 return ff_framequeue_queued_frames(&li->fifo) > 0;
1465 }
1466
1467 2110 int ff_inlink_queued_samples(AVFilterLink *link)
1468 {
1469 2110 FilterLinkInternal * const li = ff_link_internal(link);
1470 2110 return ff_framequeue_queued_samples(&li->fifo);
1471 }
1472
1473 4356 int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
1474 {
1475 4356 FilterLinkInternal * const li = ff_link_internal(link);
1476 4356 uint64_t samples = ff_framequeue_queued_samples(&li->fifo);
1477 av_assert1(min);
1478
6/6
✓ Branch 0 taken 1880 times.
✓ Branch 1 taken 2476 times.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 1814 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 59 times.
4356 return samples >= min || (li->status_in && samples);
1479 }
1480
1481 1478110 static void consume_update(FilterLinkInternal *li, const AVFrame *frame)
1482 {
1483 1478110 AVFilterLink *const link = &li->l.pub;
1484 1478110 update_link_current_pts(li, frame->pts);
1485 1478110 ff_inlink_process_commands(link, frame);
1486
2/2
✓ Branch 0 taken 1475735 times.
✓ Branch 1 taken 2375 times.
1478110 if (link == link->dst->inputs[0])
1487 1475735 link->dst->is_disabled = !evaluate_timeline_at_frame(link, frame);
1488 1478110 li->l.frame_count_out++;
1489 1478110 li->l.sample_count_out += frame->nb_samples;
1490 1478110 }
1491
1492 4525927 int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
1493 {
1494 4525927 FilterLinkInternal * const li = ff_link_internal(link);
1495 AVFrame *frame;
1496
1497 4525927 *rframe = NULL;
1498
2/2
✓ Branch 1 taken 3050296 times.
✓ Branch 2 taken 1475631 times.
4525927 if (!ff_inlink_check_available_frame(link))
1499 3050296 return 0;
1500
1501
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1475628 times.
1475631 if (li->fifo.samples_skipped) {
1502 3 frame = ff_framequeue_peek(&li->fifo, 0);
1503 3 return ff_inlink_consume_samples(link, frame->nb_samples, frame->nb_samples, rframe);
1504 }
1505
1506 1475628 frame = ff_framequeue_take(&li->fifo);
1507 1475628 consume_update(li, frame);
1508 1475628 *rframe = frame;
1509 1475628 return 1;
1510 }
1511
1512 4205 int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
1513 AVFrame **rframe)
1514 {
1515 4205 FilterLinkInternal * const li = ff_link_internal(link);
1516 AVFrame *frame;
1517 int ret;
1518
1519 av_assert1(min);
1520 4205 *rframe = NULL;
1521
2/2
✓ Branch 1 taken 1723 times.
✓ Branch 2 taken 2482 times.
4205 if (!ff_inlink_check_available_samples(link, min))
1522 1723 return 0;
1523
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2462 times.
2482 if (li->status_in)
1524
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 14 times.
20 min = FFMIN(min, ff_framequeue_queued_samples(&li->fifo));
1525 2482 ret = take_samples(li, min, max, &frame);
1526
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2482 times.
2482 if (ret < 0)
1527 return ret;
1528 2482 consume_update(li, frame);
1529 2482 *rframe = frame;
1530 2482 return 1;
1531 }
1532
1533 AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
1534 {
1535 FilterLinkInternal * const li = ff_link_internal(link);
1536 return ff_framequeue_peek(&li->fifo, idx);
1537 }
1538
1539 4683 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
1540 {
1541 4683 AVFrame *frame = *rframe;
1542 AVFrame *out;
1543 int ret;
1544
1545
2/2
✓ Branch 1 taken 2203 times.
✓ Branch 2 taken 2480 times.
4683 if (av_frame_is_writable(frame))
1546 2203 return 0;
1547 2480 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
1548
1549
1/3
✓ Branch 0 taken 2480 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2480 switch (link->type) {
1550 2480 case AVMEDIA_TYPE_VIDEO:
1551 2480 out = ff_get_video_buffer(link, link->w, link->h);
1552 2480 break;
1553 case AVMEDIA_TYPE_AUDIO:
1554 out = ff_get_audio_buffer(link, frame->nb_samples);
1555 break;
1556 default:
1557 return AVERROR(EINVAL);
1558 }
1559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (!out)
1560 return AVERROR(ENOMEM);
1561
1562 2480 ret = av_frame_copy_props(out, frame);
1563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (ret < 0) {
1564 av_frame_free(&out);
1565 return ret;
1566 }
1567
1568 2480 ret = av_frame_copy(out, frame);
1569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2480 times.
2480 if (ret < 0) {
1570 av_frame_free(&out);
1571 return ret;
1572 }
1573
1574 2480 av_frame_free(&frame);
1575 2480 *rframe = out;
1576 2480 return 0;
1577 }
1578
1579 2178880 int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
1580 {
1581 2178880 FFFilterContext *ctxi = fffilterctx(link->dst);
1582 2178880 AVFilterCommand *cmd = ctxi->command_queue;
1583
1584
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2178880 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2178880 while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
1585 av_log(link->dst, AV_LOG_DEBUG,
1586 "Processing command time:%f command:%s arg:%s\n",
1587 cmd->time, cmd->command, cmd->arg);
1588 avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
1589 command_queue_pop(link->dst);
1590 cmd = ctxi->command_queue;
1591 }
1592 2178880 return 0;
1593 }
1594
1595 769398 void ff_inlink_request_frame(AVFilterLink *link)
1596 {
1597 769398 av_unused FilterLinkInternal *li = ff_link_internal(link);
1598 av_assert1(!li->status_in);
1599 av_assert1(!li->status_out);
1600 769398 li->frame_wanted_out = 1;
1601 769398 ff_filter_set_ready(link->src, 100);
1602 769398 }
1603
1604 10732 void ff_inlink_set_status(AVFilterLink *link, int status)
1605 {
1606 10732 FilterLinkInternal * const li = ff_link_internal(link);
1607
2/2
✓ Branch 0 taken 8335 times.
✓ Branch 1 taken 2397 times.
10732 if (li->status_out)
1608 8335 return;
1609 2397 li->frame_wanted_out = 0;
1610 2397 li->frame_blocked_in = 0;
1611 2397 link_set_out_status(link, status, AV_NOPTS_VALUE);
1612
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2397 times.
2397 while (ff_framequeue_queued_frames(&li->fifo)) {
1613 AVFrame *frame = ff_framequeue_take(&li->fifo);
1614 av_frame_free(&frame);
1615 }
1616
2/2
✓ Branch 0 taken 2396 times.
✓ Branch 1 taken 1 times.
2397 if (!li->status_in)
1617 2396 li->status_in = status;
1618 }
1619
1620 1115845 int ff_outlink_get_status(AVFilterLink *link)
1621 {
1622 1115845 FilterLinkInternal * const li = ff_link_internal(link);
1623 1115845 return li->status_in;
1624 }
1625
1626 1478 int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink)
1627 {
1628 1478 FilterLinkInternal * const li_in = ff_link_internal(inlink);
1629
1/2
✓ Branch 1 taken 887 times.
✗ Branch 2 not taken.
2365 return ff_outlink_frame_wanted(outlink) ||
1630
2/2
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 591 times.
2365 ff_inlink_check_available_frame(inlink) ||
1631
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 860 times.
887 li_in->status_out;
1632 }
1633
1634
1635 const AVClass *avfilter_get_class(void)
1636 {
1637 return &avfilter_class;
1638 }
1639
1640 int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
1641 int default_pool_size)
1642 {
1643 FilterLink *l = ff_filter_link(link);
1644 AVHWFramesContext *frames;
1645
1646 // Must already be set by caller.
1647 av_assert0(l->hw_frames_ctx);
1648
1649 frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
1650
1651 if (frames->initial_pool_size == 0) {
1652 // Dynamic allocation is necessarily supported.
1653 } else if (avctx->extra_hw_frames >= 0) {
1654 frames->initial_pool_size += avctx->extra_hw_frames;
1655 } else {
1656 frames->initial_pool_size = default_pool_size;
1657 }
1658
1659 return 0;
1660 }
1661
1662 417922 int ff_outlink_frame_wanted(AVFilterLink *link)
1663 {
1664 417922 FilterLinkInternal * const li = ff_link_internal(link);
1665 417922 return li->frame_wanted_out;
1666 }
1667
1668 4185 int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func,
1669 void *arg, int *ret, int nb_jobs)
1670 {
1671 4185 return fffilterctx(ctx)->execute(ctx, func, arg, ret, nb_jobs);
1672 }
1673