FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfilter.c
Date: 2026-10-27 14:35:28
Exec Total Coverage
Lines: 696 870 80.0%
Functions: 60 67 89.6%
Branches: 369 520 71.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/framepool.h"
31 #include "libavutil/hwcontext.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/rational.h"
37 #include "libavutil/samplefmt.h"
38
39 #include "audio.h"
40 #include "avfilter.h"
41 #include "avfilter_internal.h"
42 #include "filters.h"
43 #include "formats.h"
44 #include "framequeue.h"
45 #include "video.h"
46
47 1763508 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 1763508 }
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 846 static int append_pad(unsigned *count, AVFilterPad **pads,
101 AVFilterLink ***links, AVFilterPad *newpad)
102 {
103 AVFilterLink **newlinks;
104 AVFilterPad *newpads;
105 846 unsigned idx = *count;
106
107 846 newpads = av_realloc_array(*pads, idx + 1, sizeof(*newpads));
108 846 newlinks = av_realloc_array(*links, idx + 1, sizeof(*newlinks));
109
1/2
✓ Branch 0 taken 846 times.
✗ Branch 1 not taken.
846 if (newpads)
110 846 *pads = newpads;
111
1/2
✓ Branch 0 taken 846 times.
✗ Branch 1 not taken.
846 if (newlinks)
112 846 *links = newlinks;
113
2/4
✓ Branch 0 taken 846 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 846 times.
846 if (!newpads || !newlinks) {
114 ✗ if (newpad->flags & AVFILTERPAD_FLAG_FREE_NAME)
115 ✗ av_freep(&newpad->name);
116 ✗ return AVERROR(ENOMEM);
117 }
118
119 846 memcpy(*pads + idx, newpad, sizeof(AVFilterPad));
120 846 (*links)[idx] = NULL;
121
122 846 (*count)++;
123
124 846 return 0;
125 }
126
127 327 int ff_append_inpad(AVFilterContext *f, AVFilterPad *p)
128 {
129 327 return append_pad(&f->nb_inputs, &f->input_pads, &f->inputs, p);
130 }
131
132 304 int ff_append_inpad_free_name(AVFilterContext *f, AVFilterPad *p)
133 {
134 304 p->flags |= AVFILTERPAD_FLAG_FREE_NAME;
135 304 return ff_append_inpad(f, p);
136 }
137
138 519 int ff_append_outpad(AVFilterContext *f, AVFilterPad *p)
139 {
140 519 return append_pad(&f->nb_outputs, &f->output_pads, &f->outputs, p);
141 }
142
143 377 int ff_append_outpad_free_name(AVFilterContext *f, AVFilterPad *p)
144 {
145 377 p->flags |= AVFILTERPAD_FLAG_FREE_NAME;
146 377 return ff_append_outpad(f, p);
147 }
148
149 49212 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 49212 times.
49212 av_assert0(src->graph);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49212 times.
49212 av_assert0(dst->graph);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49212 times.
49212 av_assert0(src->graph == dst->graph);
158
159
2/4
✓ Branch 0 taken 49212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 49212 times.
✗ Branch 3 not taken.
49212 if (src->nb_outputs <= srcpad || dst->nb_inputs <= dstpad ||
160
2/4
✓ Branch 0 taken 49212 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49212 times.
49212 src->outputs[srcpad] || dst->inputs[dstpad])
161 ✗ return AVERROR(EINVAL);
162
163
1/2
✓ Branch 1 taken 49212 times.
✗ Branch 2 not taken.
49212 if (!(fffilterctx(src)->state_flags & AV_CLASS_STATE_INITIALIZED) ||
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49212 times.
49212 !(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 49212 times.
49212 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 49212 li = av_mallocz(sizeof(*li));
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49212 times.
49212 if (!li)
179 ✗ return AVERROR(ENOMEM);
180 49212 link = &li->l.pub;
181
182 49212 src->outputs[srcpad] = dst->inputs[dstpad] = link;
183
184 49212 link->src = src;
185 49212 link->dst = dst;
186 49212 link->srcpad = &src->output_pads[srcpad];
187 49212 link->dstpad = &dst->input_pads[dstpad];
188 49212 link->type = src->output_pads[srcpad].type;
189 49212 li->l.graph = src->graph;
190 av_assert0(AV_PIX_FMT_NONE == -1 && AV_SAMPLE_FMT_NONE == -1);
191 49212 link->format = -1;
192 49212 link->colorspace = AVCOL_SPC_UNSPECIFIED;
193 49212 ff_framequeue_init(&li->fifo, &fffiltergraph(src->graph)->frame_queues);
194
195 49212 return 0;
196 }
197
198 49344 static void link_free(AVFilterLink **link)
199 {
200 FilterLinkInternal *li;
201
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49344 times.
49344 if (!*link)
203 ✗ return;
204 49344 li = ff_link_internal(*link);
205
206 49344 ff_framequeue_free(&li->fifo);
207 49344 ff_frame_pool_uninit(&li->frame_pool);
208 49344 av_channel_layout_uninit(&(*link)->ch_layout);
209 49344 av_frame_side_data_free(&(*link)->side_data, &(*link)->nb_side_data);
210
211 49344 av_buffer_unref(&li->l.hw_frames_ctx);
212
213 49344 av_freep(link);
214 }
215
216 1776281 static void update_link_current_pts(FilterLinkInternal *li, int64_t pts)
217 {
218 1776281 AVFilterLink *const link = &li->l.pub;
219
220
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1776276 times.
1776281 if (pts == AV_NOPTS_VALUE)
221 5 return;
222 1776276 li->l.current_pts = pts;
223 1776276 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 1776276 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 477582 times.
✓ Branch 3 taken 1298694 times.
1776276 if (li->l.graph && li->age_index >= 0)
226 477582 ff_avfilter_graph_update_heap(li->l.graph, li);
227 }
228
229 4389666 void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
230 {
231 4389666 FFFilterContext *ctxi = fffilterctx(filter);
232 4389666 ctxi->ready = FFMAX(ctxi->ready, priority);
233 4389666 }
234
235 /**
236 * Clear frame_blocked_in on all outputs.
237 * This is necessary whenever something changes on input.
238 */
239 2623113 static void filter_unblock(AVFilterContext *filter)
240 {
241 unsigned i;
242
243
2/2
✓ Branch 0 taken 2148892 times.
✓ Branch 1 taken 2623113 times.
4772005 for (i = 0; i < filter->nb_outputs; i++) {
244 2148892 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
245 2148892 li->frame_blocked_in = 0;
246 }
247 2623113 }
248
249
250 21348 void ff_avfilter_link_set_in_status(AVFilterLink *link, int status, int64_t pts)
251 {
252 21348 FilterLinkInternal * const li = ff_link_internal(link);
253
254
2/2
✓ Branch 0 taken 5983 times.
✓ Branch 1 taken 15365 times.
21348 if (li->status_in == status)
255 5983 return;
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15365 times.
15365 av_assert0(!li->status_in);
257 15365 li->status_in = status;
258 15365 li->status_in_pts = pts;
259 15365 li->frame_wanted_out = 0;
260 15365 li->frame_blocked_in = 0;
261 15365 filter_unblock(link->dst);
262 15365 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 9177 static void link_set_out_status(AVFilterLink *link, int status, int64_t pts)
270 {
271 9177 FilterLinkInternal * const li = ff_link_internal(link);
272
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9177 times.
9177 av_assert0(!li->frame_wanted_out);
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9177 times.
9177 av_assert0(!li->status_out);
275 9177 li->status_out = status;
276
2/2
✓ Branch 0 taken 6562 times.
✓ Branch 1 taken 2615 times.
9177 if (pts != AV_NOPTS_VALUE)
277 6562 update_link_current_pts(li, pts);
278 9177 filter_unblock(link->dst);
279 9177 ff_filter_set_ready(link->src, 200);
280 9177 }
281
282 1590 int avfilter_insert_filter(AVFilterLink *link, AVFilterContext *filt,
283 unsigned filt_srcpad_idx, unsigned filt_dstpad_idx)
284 {
285 int ret;
286 1590 unsigned dstpad_idx = link->dstpad - link->dst->input_pads;
287
288 1590 av_log(link->dst, AV_LOG_VERBOSE, "auto-inserting filter '%s' "
289 "between the filter '%s' and the filter '%s'\n",
290 1590 filt->name, link->src->name, link->dst->name);
291
292 1590 link->dst->inputs[dstpad_idx] = NULL;
293
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1590 times.
1590 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 1590 link->dst = filt;
301 1590 link->dstpad = &filt->input_pads[filt_srcpad_idx];
302 1590 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 1590 times.
✗ Branch 1 not taken.
1590 if (link->outcfg.formats)
307 1590 ff_formats_changeref(&link->outcfg.formats,
308 1590 &filt->outputs[filt_dstpad_idx]->outcfg.formats);
309
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 816 times.
1590 if (link->outcfg.color_spaces)
310 774 ff_formats_changeref(&link->outcfg.color_spaces,
311 774 &filt->outputs[filt_dstpad_idx]->outcfg.color_spaces);
312
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 816 times.
1590 if (link->outcfg.color_ranges)
313 774 ff_formats_changeref(&link->outcfg.color_ranges,
314 774 &filt->outputs[filt_dstpad_idx]->outcfg.color_ranges);
315
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 816 times.
1590 if (link->outcfg.alpha_modes)
316 774 ff_formats_changeref(&link->outcfg.alpha_modes,
317 774 &filt->outputs[filt_dstpad_idx]->outcfg.alpha_modes);
318
2/2
✓ Branch 0 taken 774 times.
✓ Branch 1 taken 816 times.
1590 if (link->outcfg.chroma_locations)
319 774 ff_formats_changeref(&link->outcfg.chroma_locations,
320 774 &filt->outputs[filt_dstpad_idx]->outcfg.chroma_locations);
321
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 774 times.
1590 if (link->outcfg.samplerates)
322 816 ff_formats_changeref(&link->outcfg.samplerates,
323 816 &filt->outputs[filt_dstpad_idx]->outcfg.samplerates);
324
2/2
✓ Branch 0 taken 816 times.
✓ Branch 1 taken 774 times.
1590 if (link->outcfg.channel_layouts)
325 816 ff_channel_layouts_changeref(&link->outcfg.channel_layouts,
326 816 &filt->outputs[filt_dstpad_idx]->outcfg.channel_layouts);
327
328 1590 return 0;
329 }
330
331 47358 int ff_filter_config_links(AVFilterContext *filter)
332 {
333 int (*config_link)(AVFilterLink *);
334 unsigned i;
335 int ret;
336
337
2/2
✓ Branch 0 taken 38826 times.
✓ Branch 1 taken 47349 times.
86175 for (i = 0; i < filter->nb_inputs; i ++) {
338 38826 AVFilterLink *link = filter->inputs[i];
339 AVFilterLink *inlink;
340 38826 FilterLinkInternal *li = ff_link_internal(link);
341 FilterLinkInternal *li_in;
342
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38826 times.
38826 if (!link) continue;
344
2/4
✓ Branch 0 taken 38826 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 38826 times.
38826 if (!link->src || !link->dst) {
345 ✗ av_log(filter, AV_LOG_ERROR,
346 "Not all input and output are properly linked (%d).\n", i);
347 ✗ return AVERROR(EINVAL);
348 }
349
350
2/2
✓ Branch 0 taken 29975 times.
✓ Branch 1 taken 8851 times.
38826 inlink = link->src->nb_inputs ? link->src->inputs[0] : NULL;
351
2/2
✓ Branch 0 taken 29975 times.
✓ Branch 1 taken 8851 times.
38826 li_in = inlink ? ff_link_internal(inlink) : NULL;
352 38826 li->l.current_pts =
353 38826 li->l.current_pts_us = AV_NOPTS_VALUE;
354
355
3/4
✓ Branch 0 taken 217 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 38607 times.
✗ Branch 3 not taken.
38826 switch (li->init_state) {
356 217 case AVLINK_INIT:
357 217 continue;
358 2 case AVLINK_STARTINIT:
359 2 av_log(filter, AV_LOG_INFO, "circular filter chain detected\n");
360 2 return 0;
361 38607 case AVLINK_UNINIT:
362 38607 li->init_state = AVLINK_STARTINIT;
363
364
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 38602 times.
38607 if ((ret = ff_filter_config_links(link->src)) < 0)
365 5 return ret;
366
367
2/2
✓ Branch 0 taken 20136 times.
✓ Branch 1 taken 18466 times.
38602 if (!(config_link = link->srcpad->config_props)) {
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20136 times.
20136 if (link->src->nb_inputs != 1) {
369 ✗ av_log(link->src, AV_LOG_ERROR, "Source filters and filters "
370 "with more than one input "
371 "must set config_props() "
372 "callbacks on all outputs\n");
373 ✗ return AVERROR(EINVAL);
374 }
375 }
376
377 /* Copy side data before link->srcpad->config_props() is called, so the filter
378 * may remove it for the next filter in the chain */
379
5/6
✓ Branch 0 taken 29884 times.
✓ Branch 1 taken 8718 times.
✓ Branch 2 taken 334 times.
✓ Branch 3 taken 29550 times.
✓ Branch 4 taken 334 times.
✗ Branch 5 not taken.
38602 if (inlink && inlink->nb_side_data && !link->nb_side_data) {
380
2/2
✓ Branch 0 taken 379 times.
✓ Branch 1 taken 334 times.
713 for (int j = 0; j < inlink->nb_side_data; j++) {
381 379 ret = av_frame_side_data_clone(&link->side_data, &link->nb_side_data,
382 379 inlink->side_data[j], 0);
383
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 379 times.
379 if (ret < 0) {
384 ✗ av_frame_side_data_free(&link->side_data, &link->nb_side_data);
385 ✗ return ret;
386 }
387 }
388 }
389
390
4/4
✓ Branch 0 taken 18466 times.
✓ Branch 1 taken 20136 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 18465 times.
38602 if (config_link && (ret = config_link(link)) < 0) {
391 1 av_log(link->src, AV_LOG_ERROR,
392 "Failed to configure output pad on %s\n",
393 1 link->src->name);
394 1 return ret;
395 }
396
397
2/3
✓ Branch 0 taken 31604 times.
✓ Branch 1 taken 6997 times.
✗ Branch 2 not taken.
38601 switch (link->type) {
398 31604 case AVMEDIA_TYPE_VIDEO:
399
3/4
✓ Branch 0 taken 24177 times.
✓ Branch 1 taken 7427 times.
✓ Branch 2 taken 24177 times.
✗ Branch 3 not taken.
31604 if (!link->time_base.num && !link->time_base.den)
400
1/2
✓ Branch 0 taken 24177 times.
✗ Branch 1 not taken.
24177 link->time_base = inlink ? inlink->time_base : AV_TIME_BASE_Q;
401
402
4/4
✓ Branch 0 taken 26885 times.
✓ Branch 1 taken 4719 times.
✓ Branch 2 taken 17116 times.
✓ Branch 3 taken 9769 times.
31604 if (!link->sample_aspect_ratio.num && !link->sample_aspect_ratio.den)
403 17116 link->sample_aspect_ratio = inlink ?
404
2/2
✓ Branch 0 taken 17101 times.
✓ Branch 1 taken 15 times.
17116 inlink->sample_aspect_ratio : (AVRational){1,1};
405
406
2/2
✓ Branch 0 taken 24459 times.
✓ Branch 1 taken 7145 times.
31604 if (inlink) {
407
3/4
✓ Branch 0 taken 24220 times.
✓ Branch 1 taken 239 times.
✓ Branch 2 taken 24220 times.
✗ Branch 3 not taken.
24459 if (!li->l.frame_rate.num && !li->l.frame_rate.den)
408 24220 li->l.frame_rate = li_in->l.frame_rate;
409
2/2
✓ Branch 0 taken 16606 times.
✓ Branch 1 taken 7853 times.
24459 if (!link->w)
410 16606 link->w = inlink->w;
411
2/2
✓ Branch 0 taken 16606 times.
✓ Branch 1 taken 7853 times.
24459 if (!link->h)
412 16606 link->h = inlink->h;
413
2/4
✓ Branch 0 taken 7145 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7145 times.
7145 } else if (!link->w || !link->h) {
414 ✗ av_log(link->src, AV_LOG_ERROR,
415 "Video source filters must set their output link's "
416 "width and height\n");
417 ✗ return AVERROR(EINVAL);
418 }
419 31604 break;
420
421 6997 case AVMEDIA_TYPE_AUDIO:
422
2/2
✓ Branch 0 taken 5424 times.
✓ Branch 1 taken 1573 times.
6997 if (inlink) {
423
3/4
✓ Branch 0 taken 3677 times.
✓ Branch 1 taken 1747 times.
✓ Branch 2 taken 3677 times.
✗ Branch 3 not taken.
5424 if (!link->time_base.num && !link->time_base.den)
424 3677 link->time_base = inlink->time_base;
425 }
426
427
3/4
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 6904 times.
✓ Branch 2 taken 93 times.
✗ Branch 3 not taken.
6997 if (!link->time_base.num && !link->time_base.den)
428 93 link->time_base = (AVRational) {1, link->sample_rate};
429 }
430
431
2/2
✓ Branch 0 taken 29883 times.
✓ Branch 1 taken 8718 times.
38601 if (link->src->nb_inputs &&
432
1/2
✓ Branch 1 taken 29883 times.
✗ Branch 2 not taken.
29883 !(fffilter(link->src->filter)->flags_internal & FF_FILTER_FLAG_HWFRAME_AWARE)) {
433 29883 FilterLink *l0 = ff_filter_link(link->src->inputs[0]);
434
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29883 times.
29883 av_assert0(!li->l.hw_frames_ctx &&
436 "should not be set by non-hwframe-aware filter");
437
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29883 times.
29883 if (l0->hw_frames_ctx) {
439 ✗ li->l.hw_frames_ctx = av_buffer_ref(l0->hw_frames_ctx);
440 ✗ if (!li->l.hw_frames_ctx)
441 ✗ return AVERROR(ENOMEM);
442 }
443 }
444
445
2/2
✓ Branch 0 taken 5305 times.
✓ Branch 1 taken 33296 times.
38601 if ((config_link = link->dstpad->config_props))
446
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5304 times.
5305 if ((ret = config_link(link)) < 0) {
447 1 av_log(link->dst, AV_LOG_ERROR,
448 "Failed to configure input pad on %s\n",
449 1 link->dst->name);
450 1 return ret;
451 }
452
453 38600 li->cfg_format = link->format;
454 38600 li->cfg_w = link->w;
455 38600 li->cfg_h = link->h;
456 38600 li->init_state = AVLINK_INIT;
457 }
458 }
459
460 47349 return 0;
461 }
462
463 #ifdef TRACE
464 void ff_tlog_link(void *ctx, AVFilterLink *link, int end)
465 {
466 if (link->type == AVMEDIA_TYPE_VIDEO) {
467 ff_tlog(ctx,
468 "link[%p s:%dx%d fmt:%s %s->%s]%s",
469 link, link->w, link->h,
470 av_get_pix_fmt_name(link->format),
471 link->src ? link->src->filter->name : "",
472 link->dst ? link->dst->filter->name : "",
473 end ? "\n" : "");
474 } else {
475 char buf[128];
476 av_channel_layout_describe(&link->ch_layout, buf, sizeof(buf));
477
478 ff_tlog(ctx,
479 "link[%p r:%d cl:%s fmt:%s %s->%s]%s",
480 link, (int)link->sample_rate, buf,
481 av_get_sample_fmt_name(link->format),
482 link->src ? link->src->filter->name : "",
483 link->dst ? link->dst->filter->name : "",
484 end ? "\n" : "");
485 }
486 }
487 #endif
488
489 833317 int ff_request_frame(AVFilterLink *link)
490 {
491 833317 FilterLinkInternal * const li = ff_link_internal(link);
492
493 FF_TPRINTF_START(NULL, request_frame); ff_tlog_link(NULL, link, 1);
494
495 av_assert1(!fffilter(link->dst->filter)->activate);
496
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 833306 times.
833317 if (li->status_out)
497 11 return li->status_out;
498
2/2
✓ Branch 0 taken 6563 times.
✓ Branch 1 taken 826743 times.
833306 if (li->status_in) {
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6563 times.
6563 if (ff_framequeue_queued_frames(&li->fifo)) {
500 av_assert1(!li->frame_wanted_out);
501 av_assert1(fffilterctx(link->dst)->ready >= 300);
502 ✗ return 0;
503 } else {
504 /* Acknowledge status change. Filters using ff_request_frame() will
505 handle the change automatically. Filters can also check the
506 status directly but none do yet. */
507 6563 link_set_out_status(link, li->status_in, li->status_in_pts);
508 6563 return li->status_out;
509 }
510 }
511 826743 li->frame_wanted_out = 1;
512 826743 ff_filter_set_ready(link->src, 100);
513 826743 return 0;
514 }
515
516 6555 static int64_t guess_status_pts(AVFilterContext *ctx, int status, AVRational link_time_base)
517 {
518 unsigned i;
519 6555 int64_t r = INT64_MAX;
520
521
2/2
✓ Branch 0 taken 6554 times.
✓ Branch 1 taken 6555 times.
13109 for (i = 0; i < ctx->nb_inputs; i++) {
522 6554 FilterLinkInternal * const li = ff_link_internal(ctx->inputs[i]);
523
1/2
✓ Branch 0 taken 6554 times.
✗ Branch 1 not taken.
6554 if (li->status_out == status)
524 6554 r = FFMIN(r, av_rescale_q(li->l.current_pts, ctx->inputs[i]->time_base, link_time_base));
525 }
526
2/2
✓ Branch 0 taken 6554 times.
✓ Branch 1 taken 1 times.
6555 if (r < INT64_MAX)
527 6554 return r;
528 1 av_log(ctx, AV_LOG_WARNING, "EOF timestamp not reliable\n");
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 for (i = 0; i < ctx->nb_inputs; i++) {
530 ✗ FilterLinkInternal * const li = ff_link_internal(ctx->inputs[i]);
531 ✗ r = FFMIN(r, av_rescale_q(li->status_in_pts, ctx->inputs[i]->time_base, link_time_base));
532 }
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (r < INT64_MAX)
534 ✗ return r;
535 1 return AV_NOPTS_VALUE;
536 }
537
538 833629 static int request_frame_to_filter(AVFilterLink *link)
539 {
540 833629 FilterLinkInternal * const li = ff_link_internal(link);
541 833629 int ret = -1;
542
543 FF_TPRINTF_START(NULL, request_frame_to_filter); ff_tlog_link(NULL, link, 1);
544 /* Assume the filter is blocked, let the method clear it if not */
545 833629 li->frame_blocked_in = 1;
546
2/2
✓ Branch 0 taken 2045 times.
✓ Branch 1 taken 831584 times.
833629 if (link->srcpad->request_frame)
547 2045 ret = link->srcpad->request_frame(link);
548
1/2
✓ Branch 0 taken 831584 times.
✗ Branch 1 not taken.
831584 else if (link->src->inputs[0])
549 831584 ret = ff_request_frame(link->src->inputs[0]);
550
2/2
✓ Branch 0 taken 6555 times.
✓ Branch 1 taken 827074 times.
833629 if (ret < 0) {
551
2/4
✓ Branch 0 taken 6555 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6555 times.
✗ Branch 3 not taken.
6555 if (ret != AVERROR(EAGAIN) && ret != li->status_in)
552 6555 ff_avfilter_link_set_in_status(link, ret, guess_status_pts(link->src, ret, link->time_base));
553
1/2
✓ Branch 0 taken 6555 times.
✗ Branch 1 not taken.
6555 if (ret == AVERROR_EOF)
554 6555 ret = 0;
555 }
556 833629 return ret;
557 }
558
559 static const char *const var_names[] = {
560 "t",
561 "n",
562 "w",
563 "h",
564 NULL
565 };
566
567 enum {
568 VAR_T,
569 VAR_N,
570 VAR_W,
571 VAR_H,
572 VAR_VARS_NB
573 };
574
575 6 static int set_enable_expr(FFFilterContext *ctxi, const char *expr)
576 {
577 6 AVFilterContext *ctx = &ctxi->p;
578 int ret;
579 char *expr_dup;
580 6 AVExpr *old = ctxi->enable;
581
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!(ctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE)) {
583 ✗ av_log(ctx, AV_LOG_ERROR, "Timeline ('enable' option) not supported "
584 ✗ "with filter '%s'\n", ctx->filter->name);
585 ✗ return AVERROR_PATCHWELCOME;
586 }
587
588 6 expr_dup = av_strdup(expr);
589
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!expr_dup)
590 ✗ return AVERROR(ENOMEM);
591
592
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!ctxi->var_values) {
593 6 ctxi->var_values = av_calloc(VAR_VARS_NB, sizeof(*ctxi->var_values));
594
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ctxi->var_values) {
595 ✗ av_free(expr_dup);
596 ✗ return AVERROR(ENOMEM);
597 }
598 }
599
600 6 ret = av_expr_parse(&ctxi->enable, expr_dup, var_names,
601 NULL, NULL, NULL, NULL, 0, ctx->priv);
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
603 ✗ av_log(ctx->priv, AV_LOG_ERROR,
604 "Error when evaluating the expression '%s' for enable\n",
605 expr_dup);
606 ✗ av_free(expr_dup);
607 ✗ return ret;
608 }
609
610 6 av_expr_free(old);
611 6 av_free(ctx->enable_str);
612 6 ctx->enable_str = expr_dup;
613 6 return 0;
614 }
615
616 ✗ int avfilter_process_command(AVFilterContext *filter, const char *cmd, const char *arg, char *res, int res_len, int flags)
617 {
618 ✗ if(!strcmp(cmd, "ping")){
619 ✗ char local_res[256] = {0};
620
621 ✗ if (!res) {
622 ✗ res = local_res;
623 ✗ res_len = sizeof(local_res);
624 }
625 ✗ av_strlcatf(res, res_len, "pong from:%s %s\n", filter->filter->name, filter->name);
626 ✗ if (res == local_res)
627 ✗ av_log(filter, AV_LOG_INFO, "%s", res);
628 ✗ return 0;
629 ✗ }else if(!strcmp(cmd, "enable")) {
630 ✗ return set_enable_expr(fffilterctx(filter), arg);
631 ✗ }else if (fffilter(filter->filter)->process_command) {
632 ✗ return fffilter(filter->filter)->process_command(filter, cmd, arg, res, res_len, flags);
633 }
634 ✗ return AVERROR(ENOSYS);
635 }
636
637 ✗ unsigned avfilter_filter_pad_count(const AVFilter *filter, int is_output)
638 {
639 ✗ return is_output ? fffilter(filter)->nb_outputs : fffilter(filter)->nb_inputs;
640 }
641
642 1377 static const char *default_filter_name(void *filter_ctx)
643 {
644 1377 AVFilterContext *ctx = filter_ctx;
645
1/2
✓ Branch 0 taken 1377 times.
✗ Branch 1 not taken.
1377 return ctx->name ? ctx->name : ctx->filter->name;
646 }
647
648 60570 static void *filter_child_next(void *obj, void *prev)
649 {
650 60570 AVFilterContext *ctx = obj;
651
5/8
✓ Branch 0 taken 60564 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 60564 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60564 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 60564 times.
✗ Branch 7 not taken.
60570 if (!prev && ctx->filter && ctx->filter->priv_class && ctx->priv)
652 60564 return ctx->priv;
653 6 return NULL;
654 }
655
656 ✗ static const AVClass *filter_child_class_iterate(void **iter)
657 {
658 const AVFilter *f;
659
660 ✗ while ((f = av_filter_iterate(iter)))
661 ✗ if (f->priv_class)
662 ✗ return f->priv_class;
663
664 ✗ return NULL;
665 }
666
667 #define OFFSET(x) offsetof(AVFilterContext, x)
668 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
669 #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
670 static const AVOption avfilter_options[] = {
671 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
672 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, .unit = "thread_type" },
673 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
674 { "enable", "set enable expression", OFFSET(enable_str), AV_OPT_TYPE_STRING, {.str=NULL}, .flags = TFLAGS },
675 { "threads", "Allowed number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
676 { .i64 = 0 }, 0, INT_MAX, FLAGS, .unit = "threads" },
677 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = FLAGS, .unit = "threads"},
678 { "extra_hw_frames", "Number of extra hardware frames to allocate for the user",
679 OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS },
680 { NULL },
681 };
682
683 static const AVClass avfilter_class = {
684 .class_name = "AVFilter",
685 .item_name = default_filter_name,
686 .version = LIBAVUTIL_VERSION_INT,
687 .category = AV_CLASS_CATEGORY_FILTER,
688 .child_next = filter_child_next,
689 .child_class_iterate = filter_child_class_iterate,
690 .option = avfilter_options,
691 .state_flags_offset = offsetof(FFFilterContext, state_flags),
692 };
693
694 1374 static int default_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg,
695 int *ret, int nb_jobs)
696 {
697 int i;
698
699
2/2
✓ Branch 0 taken 1430 times.
✓ Branch 1 taken 1374 times.
2804 for (i = 0; i < nb_jobs; i++) {
700 1430 int r = func(ctx, arg, i, nb_jobs);
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1430 times.
1430 if (ret)
702 ✗ ret[i] = r;
703 }
704 1374 return 0;
705 }
706
707 66240 AVFilterContext *ff_filter_alloc(const AVFilter *filter, const char *inst_name)
708 {
709 FFFilterContext *ctx;
710 AVFilterContext *ret;
711 66240 const FFFilter *const fi = fffilter(filter);
712 66240 int preinited = 0;
713
714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 if (!filter)
715 ✗ return NULL;
716
717 66240 ctx = av_mallocz(sizeof(*ctx));
718
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 if (!ctx)
719 ✗ return NULL;
720 66240 ret = &ctx->p;
721
722 66240 ret->av_class = &avfilter_class;
723 66240 ret->filter = filter;
724
1/2
✓ Branch 0 taken 66240 times.
✗ Branch 1 not taken.
66240 ret->name = inst_name ? av_strdup(inst_name) : NULL;
725
2/2
✓ Branch 0 taken 59131 times.
✓ Branch 1 taken 7109 times.
66240 if (fi->priv_size) {
726 59131 ret->priv = av_mallocz(fi->priv_size);
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59131 times.
59131 if (!ret->priv)
728 ✗ goto err;
729 }
730
2/2
✓ Branch 0 taken 14239 times.
✓ Branch 1 taken 52001 times.
66240 if (fi->preinit) {
731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14239 times.
14239 if (fi->preinit(ret) < 0)
732 ✗ goto err;
733 14239 preinited = 1;
734 }
735
736 66240 av_opt_set_defaults(ret);
737
2/2
✓ Branch 0 taken 57872 times.
✓ Branch 1 taken 8368 times.
66240 if (filter->priv_class) {
738 57872 *(const AVClass**)ret->priv = filter->priv_class;
739 57872 av_opt_set_defaults(ret->priv);
740 }
741
742 66240 ctx->execute = default_execute;
743
744 66240 ret->nb_inputs = fi->nb_inputs;
745
2/2
✓ Branch 0 taken 56108 times.
✓ Branch 1 taken 10132 times.
66240 if (ret->nb_inputs ) {
746 56108 ret->input_pads = av_memdup(filter->inputs, ret->nb_inputs * sizeof(*filter->inputs));
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56108 times.
56108 if (!ret->input_pads)
748 ✗ goto err;
749 56108 ret->inputs = av_calloc(ret->nb_inputs, sizeof(*ret->inputs));
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56108 times.
56108 if (!ret->inputs)
751 ✗ goto err;
752 }
753
754 66240 ret->nb_outputs = fi->nb_outputs;
755
2/2
✓ Branch 0 taken 57378 times.
✓ Branch 1 taken 8862 times.
66240 if (ret->nb_outputs) {
756 57378 ret->output_pads = av_memdup(filter->outputs, ret->nb_outputs * sizeof(*filter->outputs));
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57378 times.
57378 if (!ret->output_pads)
758 ✗ goto err;
759 57378 ret->outputs = av_calloc(ret->nb_outputs, sizeof(*ret->outputs));
760
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57378 times.
57378 if (!ret->outputs)
761 ✗ goto err;
762 }
763
764 66240 return ret;
765
766 ✗ err:
767 ✗ if (preinited)
768 ✗ fi->uninit(ret);
769 ✗ av_freep(&ret->name);
770 ✗ av_freep(&ret->inputs);
771 ✗ av_freep(&ret->input_pads);
772 ✗ ret->nb_inputs = 0;
773 ✗ av_freep(&ret->outputs);
774 ✗ av_freep(&ret->output_pads);
775 ✗ ret->nb_outputs = 0;
776 ✗ av_freep(&ret->priv);
777 ✗ av_free(ret);
778 ✗ return NULL;
779 }
780
781 114463 static void free_link(AVFilterLink *link)
782 {
783
2/2
✓ Branch 0 taken 65119 times.
✓ Branch 1 taken 49344 times.
114463 if (!link)
784 65119 return;
785
786
2/2
✓ Branch 0 taken 49212 times.
✓ Branch 1 taken 132 times.
49344 if (link->src)
787 49212 link->src->outputs[link->srcpad - link->src->output_pads] = NULL;
788
2/2
✓ Branch 0 taken 49212 times.
✓ Branch 1 taken 132 times.
49344 if (link->dst)
789 49212 link->dst->inputs[link->dstpad - link->dst->input_pads] = NULL;
790
791 49344 ff_formats_unref(&link->incfg.formats);
792 49344 ff_formats_unref(&link->outcfg.formats);
793 49344 ff_formats_unref(&link->incfg.color_spaces);
794 49344 ff_formats_unref(&link->outcfg.color_spaces);
795 49344 ff_formats_unref(&link->incfg.color_ranges);
796 49344 ff_formats_unref(&link->outcfg.color_ranges);
797 49344 ff_formats_unref(&link->incfg.alpha_modes);
798 49344 ff_formats_unref(&link->outcfg.alpha_modes);
799 49344 ff_formats_unref(&link->incfg.chroma_locations);
800 49344 ff_formats_unref(&link->outcfg.chroma_locations);
801 49344 ff_formats_unref(&link->incfg.samplerates);
802 49344 ff_formats_unref(&link->outcfg.samplerates);
803 49344 ff_channel_layouts_unref(&link->incfg.channel_layouts);
804 49344 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
805 49344 link_free(&link);
806 }
807
808 66240 void avfilter_free(AVFilterContext *filter)
809 {
810 FFFilterContext *ctxi;
811 int i;
812
813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 if (!filter)
814 ✗ return;
815 66240 ctxi = fffilterctx(filter);
816
817
1/2
✓ Branch 0 taken 66240 times.
✗ Branch 1 not taken.
66240 if (filter->graph)
818 66240 ff_filter_graph_remove_filter(filter->graph, filter);
819
820
2/2
✓ Branch 1 taken 51388 times.
✓ Branch 2 taken 14852 times.
66240 if (fffilter(filter->filter)->uninit)
821 51388 fffilter(filter->filter)->uninit(filter);
822
823
2/2
✓ Branch 0 taken 56562 times.
✓ Branch 1 taken 66240 times.
122802 for (i = 0; i < filter->nb_inputs; i++) {
824 56562 free_link(filter->inputs[i]);
825
2/2
✓ Branch 0 taken 304 times.
✓ Branch 1 taken 56258 times.
56562 if (filter->input_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
826 304 av_freep(&filter->input_pads[i].name);
827 }
828
2/2
✓ Branch 0 taken 57901 times.
✓ Branch 1 taken 66240 times.
124141 for (i = 0; i < filter->nb_outputs; i++) {
829 57901 free_link(filter->outputs[i]);
830
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 57388 times.
57901 if (filter->output_pads[i].flags & AVFILTERPAD_FLAG_FREE_NAME)
831 513 av_freep(&filter->output_pads[i].name);
832 }
833
834
2/2
✓ Branch 0 taken 57872 times.
✓ Branch 1 taken 8368 times.
66240 if (filter->filter->priv_class)
835 57872 av_opt_free(filter->priv);
836
837 66240 av_buffer_unref(&filter->hw_device_ctx);
838
839 66240 av_freep(&filter->name);
840 66240 av_freep(&filter->input_pads);
841 66240 av_freep(&filter->output_pads);
842 66240 av_freep(&filter->inputs);
843 66240 av_freep(&filter->outputs);
844 66240 av_freep(&filter->priv);
845
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 while (ctxi->command_queue)
846 ✗ command_queue_pop(filter);
847 66240 av_opt_free(filter);
848 66240 av_expr_free(ctxi->enable);
849 66240 ctxi->enable = NULL;
850 66240 av_freep(&ctxi->var_values);
851 66240 av_free(filter);
852 }
853
854 20050 int ff_filter_get_nb_threads(AVFilterContext *ctx)
855 {
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20050 times.
20050 if (ctx->nb_threads > 0)
857 ✗ return FFMIN(ctx->nb_threads, ctx->graph->nb_threads);
858 20050 return ctx->graph->nb_threads;
859 }
860
861 35465 int ff_filter_opt_parse(void *logctx, const AVClass *priv_class,
862 AVDictionary **options, const char *args)
863 {
864 35465 const AVOption *o = NULL;
865 int ret;
866 35465 int offset= -1;
867
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35465 times.
35465 if (!args)
869 ✗ return 0;
870
871
2/2
✓ Branch 0 taken 62362 times.
✓ Branch 1 taken 35465 times.
97827 while (*args) {
872 char *parsed_key, *value;
873 const char *key;
874 62362 const char *shorthand = NULL;
875 62362 int additional_flags = 0;
876
877
4/4
✓ Branch 0 taken 47250 times.
✓ Branch 1 taken 15112 times.
✓ Branch 3 taken 45968 times.
✓ Branch 4 taken 1282 times.
62362 if (priv_class && (o = av_opt_next(&priv_class, o))) {
878
4/4
✓ Branch 0 taken 44198 times.
✓ Branch 1 taken 1770 times.
✓ Branch 2 taken 4664 times.
✓ Branch 3 taken 39534 times.
45968 if (o->type == AV_OPT_TYPE_CONST || o->offset == offset)
879 6434 continue;
880 39534 offset = o->offset;
881 39534 shorthand = o->name;
882 }
883
884 55928 ret = av_opt_get_key_value(&args, "=", ":",
885 shorthand ? AV_OPT_FLAG_IMPLICIT_KEY : 0,
886 &parsed_key, &value);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 55928 times.
55928 if (ret < 0) {
888 ✗ if (ret == AVERROR(EINVAL))
889 ✗ av_log(logctx, AV_LOG_ERROR, "No option name near '%s'\n", args);
890 else
891 ✗ av_log(logctx, AV_LOG_ERROR, "Unable to parse '%s': %s\n", args,
892 ✗ av_err2str(ret));
893 ✗ return ret;
894 }
895
2/2
✓ Branch 0 taken 29725 times.
✓ Branch 1 taken 26203 times.
55928 if (*args)
896 29725 args++;
897
2/2
✓ Branch 0 taken 34535 times.
✓ Branch 1 taken 21393 times.
55928 if (parsed_key) {
898 34535 key = parsed_key;
899 34535 additional_flags = AV_DICT_DONT_STRDUP_KEY;
900 34535 priv_class = NULL; /* reject all remaining shorthand */
901 } else {
902 21393 key = shorthand;
903 }
904
905 55928 av_log(logctx, AV_LOG_DEBUG, "Setting '%s' to value '%s'\n", key, value);
906
907 55928 av_dict_set(options, key, value,
908 additional_flags | AV_DICT_DONT_STRDUP_VAL | AV_DICT_MULTIKEY);
909 }
910
911 35465 return 0;
912 }
913
914 ✗ int ff_filter_process_command(AVFilterContext *ctx, const char *cmd,
915 const char *arg, char *res, int res_len, int flags)
916 {
917 const AVOption *o;
918
919 ✗ if (!ctx->filter->priv_class)
920 ✗ return 0;
921 ✗ o = av_opt_find2(ctx->priv, cmd, NULL, AV_OPT_FLAG_RUNTIME_PARAM | AV_OPT_FLAG_FILTERING_PARAM, AV_OPT_SEARCH_CHILDREN, NULL);
922 ✗ if (!o)
923 ✗ return AVERROR(ENOSYS);
924 ✗ return av_opt_set(ctx->priv, cmd, arg, 0);
925 }
926
927 66240 int avfilter_init_dict(AVFilterContext *ctx, AVDictionary **options)
928 {
929 66240 FFFilterContext *ctxi = fffilterctx(ctx);
930 66240 int ret = 0;
931
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 if (ctxi->state_flags & AV_CLASS_STATE_INITIALIZED) {
933 ✗ av_log(ctx, AV_LOG_ERROR, "Filter already initialized\n");
934 ✗ return AVERROR(EINVAL);
935 }
936
937 66240 ret = av_opt_set_dict2(ctx, options, AV_OPT_SEARCH_CHILDREN);
938
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66240 times.
66240 if (ret < 0) {
939 ✗ av_log(ctx, AV_LOG_ERROR, "Error applying generic filter options.\n");
940 ✗ return ret;
941 }
942
943
2/2
✓ Branch 0 taken 1317 times.
✓ Branch 1 taken 64923 times.
66240 if (ctx->filter->flags & AVFILTER_FLAG_SLICE_THREADS &&
944
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 1133 times.
1317 ctx->thread_type & ctx->graph->thread_type & AVFILTER_THREAD_SLICE &&
945
1/2
✓ Branch 1 taken 184 times.
✗ Branch 2 not taken.
184 fffiltergraph(ctx->graph)->thread_execute) {
946 184 ctx->thread_type = AVFILTER_THREAD_SLICE;
947 184 ctxi->execute = fffiltergraph(ctx->graph)->thread_execute;
948 } else {
949 66056 ctx->thread_type = 0;
950 }
951
952
2/2
✓ Branch 1 taken 52207 times.
✓ Branch 2 taken 14033 times.
66240 if (fffilter(ctx->filter)->init)
953 52207 ret = fffilter(ctx->filter)->init(ctx);
954
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 66239 times.
66240 if (ret < 0)
955 1 return ret;
956
957
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 66233 times.
66239 if (ctx->enable_str) {
958 6 ret = set_enable_expr(ctxi, ctx->enable_str);
959
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
960 ✗ return ret;
961 }
962
963 66239 ctxi->state_flags |= AV_CLASS_STATE_INITIALIZED;
964
965 66239 return 0;
966 }
967
968 22109 int avfilter_init_str(AVFilterContext *filter, const char *args)
969 {
970 22109 AVDictionary *options = NULL;
971 const AVDictionaryEntry *e;
972 22109 int ret = 0;
973
974
3/4
✓ Branch 0 taken 11041 times.
✓ Branch 1 taken 11068 times.
✓ Branch 2 taken 11041 times.
✗ Branch 3 not taken.
22109 if (args && *args) {
975 11041 ret = ff_filter_opt_parse(filter, filter->filter->priv_class, &options, args);
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11041 times.
11041 if (ret < 0)
977 ✗ goto fail;
978 }
979
980 22109 ret = avfilter_init_dict(filter, &options);
981
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22109 times.
22109 if (ret < 0)
982 ✗ goto fail;
983
984
1/2
✓ Branch 1 taken 22109 times.
✗ Branch 2 not taken.
22109 if ((e = av_dict_iterate(options, NULL))) {
985 ✗ av_log(filter, AV_LOG_ERROR, "No such option: %s.\n", e->key);
986 ✗ ret = AVERROR_OPTION_NOT_FOUND;
987 ✗ goto fail;
988 }
989
990 22109 fail:
991 22109 av_dict_free(&options);
992
993 22109 return ret;
994 }
995
996 15746 const char *avfilter_pad_get_name(const AVFilterPad *pads, int pad_idx)
997 {
998 15746 return pads[pad_idx].name;
999 }
1000
1001 32078 enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
1002 {
1003 32078 return pads[pad_idx].type;
1004 }
1005
1006 6 AVBufferRef *avfilter_link_get_hw_frames_ctx(AVFilterLink *link)
1007 {
1008 6 FilterLink *plink = ff_filter_link(link);
1009
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (plink->hw_frames_ctx)
1010 ✗ return av_buffer_ref(plink->hw_frames_ctx);
1011
1012 6 return NULL;
1013 }
1014
1015 813933 static int default_filter_frame(AVFilterLink *link, AVFrame *frame)
1016 {
1017 813933 return ff_filter_frame(link->dst->outputs[0], frame);
1018 }
1019
1020 /**
1021 * Evaluate the timeline expression of the link for the time and properties
1022 * of the frame.
1023 * @return >0 if enabled, 0 if disabled
1024 * @note It does not update link->dst->is_disabled.
1025 */
1026 2592965 static int evaluate_timeline_at_frame(AVFilterLink *link, const AVFrame *frame)
1027 {
1028 2592965 FilterLink *l = ff_filter_link(link);
1029 2592965 AVFilterContext *dstctx = link->dst;
1030 2592965 FFFilterContext *dsti = fffilterctx(dstctx);
1031 2592965 int64_t pts = frame->pts;
1032
1033
2/2
✓ Branch 0 taken 2592767 times.
✓ Branch 1 taken 198 times.
2592965 if (!dstctx->enable_str)
1034 2592767 return 1;
1035
1036 198 dsti->var_values[VAR_N] = l->frame_count_out;
1037
1/2
✓ Branch 0 taken 198 times.
✗ Branch 1 not taken.
198 dsti->var_values[VAR_T] = pts == AV_NOPTS_VALUE ? NAN : pts * av_q2d(link->time_base);
1038 198 dsti->var_values[VAR_W] = link->w;
1039 198 dsti->var_values[VAR_H] = link->h;
1040
1041 198 return fabs(av_expr_eval(dsti->enable, dsti->var_values, NULL)) >= 0.5;
1042 }
1043
1044 835063 static int filter_frame_framed(AVFilterLink *link, AVFrame *frame)
1045 {
1046 835063 FilterLink *l = ff_filter_link(link);
1047 int (*filter_frame)(AVFilterLink *, AVFrame *);
1048 835063 AVFilterContext *dstctx = link->dst;
1049 835063 AVFilterPad *dst = link->dstpad;
1050 int ret;
1051
1052
2/2
✓ Branch 0 taken 813924 times.
✓ Branch 1 taken 21139 times.
835063 if (!(filter_frame = dst->filter_frame))
1053 813924 filter_frame = default_filter_frame;
1054
1055
2/2
✓ Branch 0 taken 2993 times.
✓ Branch 1 taken 832070 times.
835063 if (dst->flags & AVFILTERPAD_FLAG_NEEDS_WRITABLE) {
1056 2993 ret = ff_inlink_make_frame_writable(link, &frame);
1057
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2993 times.
2993 if (ret < 0)
1058 ✗ goto fail;
1059 }
1060
1061 835063 ff_inlink_process_commands(link, frame);
1062 835063 dstctx->is_disabled = !evaluate_timeline_at_frame(link, frame);
1063
1064
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 835044 times.
835063 if (dstctx->is_disabled &&
1065
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 (dstctx->filter->flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC))
1066 9 filter_frame = default_filter_frame;
1067 835063 ret = filter_frame(link, frame);
1068 835063 l->frame_count_out++;
1069 835063 return ret;
1070
1071 ✗ fail:
1072 ✗ av_frame_free(&frame);
1073 ✗ return ret;
1074 }
1075
1076 1763508 int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
1077 {
1078 1763508 FilterLinkInternal * const li = ff_link_internal(link);
1079 int ret;
1080 1763508 FF_TPRINTF_START(NULL, filter_frame); ff_tlog_link(NULL, link, 1); ff_tlog(NULL, " "); tlog_ref(NULL, frame, 1);
1081
1082 /* Consistency checks */
1083
2/2
✓ Branch 0 taken 597122 times.
✓ Branch 1 taken 1166386 times.
1763508 if (link->type == AVMEDIA_TYPE_VIDEO) {
1084 597122 int dynamic_flags = fffilter(link->dst->filter)->flags_internal;
1085
1086
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 597122 times.
597122 if ((ret = ff_inlink_check_frame_params(link, frame, dynamic_flags)) < 0) {
1087 ✗ av_frame_free(&frame);
1088 ✗ return ret;
1089 }
1090
2/2
✓ Branch 0 taken 20529 times.
✓ Branch 1 taken 576593 times.
597122 if (!(dynamic_flags & FF_FILTER_FLAG_DYNAMIC_FRAME_FORMAT) &&
1091 20529 av_pix_fmt_desc_get(link->format)->flags & AV_PIX_FMT_FLAG_ALPHA)
1092 av_assert1(frame->alpha_mode == link->alpha_mode);
1093 } else {
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1166386 times.
1166386 if (frame->format != link->format) {
1095 ✗ av_log(link->dst, AV_LOG_ERROR, "Format change is not supported\n");
1096 ✗ goto error;
1097 }
1098
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1166386 times.
1166386 if (av_channel_layout_compare(&frame->ch_layout, &link->ch_layout)) {
1099 ✗ av_log(link->dst, AV_LOG_ERROR, "Channel layout change is not supported\n");
1100 ✗ goto error;
1101 }
1102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1166386 times.
1166386 if (frame->sample_rate != link->sample_rate) {
1103 ✗ av_log(link->dst, AV_LOG_ERROR, "Sample rate change is not supported\n");
1104 ✗ goto error;
1105 }
1106
1107 1166386 frame->duration = av_rescale_q(frame->nb_samples, (AVRational){ 1, frame->sample_rate },
1108 link->time_base);
1109 }
1110
1111 1763508 li->frame_blocked_in = li->frame_wanted_out = 0;
1112 1763508 li->l.frame_count_in++;
1113 1763508 li->l.sample_count_in += frame->nb_samples;
1114 1763508 filter_unblock(link->dst);
1115 1763508 ret = ff_framequeue_add(&li->fifo, frame);
1116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1763508 times.
1763508 if (ret < 0) {
1117 ✗ const FFFrameQueueGlobal *global = li->fifo.global;
1118 ✗ if (ret == AVERROR(ENOMEM) && global->queued >= global->max_queued)
1119 ✗ av_log(link->dst, AV_LOG_ERROR, "Exhausted frame queue capacity (%zu frames)\n", global->max_queued);
1120 ✗ av_frame_free(&frame);
1121 ✗ return ret;
1122 }
1123 1763508 ff_filter_set_ready(link->dst, 300);
1124 1763508 return 0;
1125
1126 ✗ error:
1127 ✗ av_frame_free(&frame);
1128 ✗ return AVERROR_PATCHWELCOME;
1129 }
1130
1131 2498228 static int samples_ready(FilterLinkInternal *link, unsigned min)
1132 {
1133
2/2
✓ Branch 1 taken 835197 times.
✓ Branch 2 taken 1663031 times.
3333425 return ff_framequeue_queued_frames(&link->fifo) &&
1134
2/2
✓ Branch 1 taken 135 times.
✓ Branch 2 taken 835062 times.
835197 (ff_framequeue_queued_samples(&link->fifo) >= min ||
1135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 134 times.
135 link->status_in);
1136 }
1137
1138 2937 static int take_samples(FilterLinkInternal *li, unsigned min, unsigned max,
1139 AVFrame **rframe)
1140 {
1141 2937 FilterLink *l = &li->l;
1142 2937 AVFilterLink *link = &l->pub;
1143 AVFrame *frame0, *frame, *buf;
1144 unsigned nb_samples, nb_frames, i, p;
1145 int ret;
1146
1147 /* Note: this function relies on no format changes and must only be
1148 called with enough samples. */
1149 av_assert1(samples_ready(li, l->min_samples));
1150 2937 frame0 = frame = ff_framequeue_peek(&li->fifo, 0);
1151
6/6
✓ Branch 0 taken 1419 times.
✓ Branch 1 taken 1518 times.
✓ Branch 2 taken 1303 times.
✓ Branch 3 taken 116 times.
✓ Branch 4 taken 1027 times.
✓ Branch 5 taken 276 times.
2937 if (!li->fifo.samples_skipped && frame->nb_samples >= min && frame->nb_samples <= max) {
1152 1027 *rframe = ff_framequeue_take(&li->fifo);
1153 1027 return 0;
1154 }
1155 1910 nb_frames = 0;
1156 1910 nb_samples = 0;
1157 while (1) {
1158
2/2
✓ Branch 0 taken 1646 times.
✓ Branch 1 taken 1482 times.
3128 if (nb_samples + frame->nb_samples > max) {
1159
2/2
✓ Branch 0 taken 1521 times.
✓ Branch 1 taken 125 times.
1646 if (nb_samples < min)
1160 1521 nb_samples = max;
1161 1646 break;
1162 }
1163 1482 nb_samples += frame->nb_samples;
1164 1482 nb_frames++;
1165
2/2
✓ Branch 1 taken 264 times.
✓ Branch 2 taken 1218 times.
1482 if (nb_frames == ff_framequeue_queued_frames(&li->fifo))
1166 264 break;
1167 1218 frame = ff_framequeue_peek(&li->fifo, nb_frames);
1168 }
1169
1170 1910 buf = ff_get_audio_buffer(link, nb_samples);
1171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1910 times.
1910 if (!buf)
1172 ✗ return AVERROR(ENOMEM);
1173 1910 ret = av_frame_copy_props(buf, frame0);
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1910 times.
1910 if (ret < 0) {
1175 ✗ av_frame_free(&buf);
1176 ✗ return ret;
1177 }
1178
1179 1910 p = 0;
1180
2/2
✓ Branch 0 taken 1482 times.
✓ Branch 1 taken 1910 times.
3392 for (i = 0; i < nb_frames; i++) {
1181 1482 frame = ff_framequeue_take(&li->fifo);
1182 1482 av_samples_copy(buf->extended_data, frame->extended_data, p, 0,
1183 1482 frame->nb_samples, link->ch_layout.nb_channels, link->format);
1184 1482 p += frame->nb_samples;
1185 1482 av_frame_free(&frame);
1186 }
1187
2/2
✓ Branch 0 taken 1521 times.
✓ Branch 1 taken 389 times.
1910 if (p < nb_samples) {
1188 1521 unsigned n = nb_samples - p;
1189 1521 frame = ff_framequeue_peek(&li->fifo, 0);
1190 1521 av_samples_copy(buf->extended_data, frame->extended_data, p, 0, n,
1191 1521 link->ch_layout.nb_channels, link->format);
1192 1521 ff_framequeue_skip_samples(&li->fifo, n, link->time_base);
1193 }
1194
1195 1910 *rframe = buf;
1196 1910 return 0;
1197 }
1198
1199 835063 static int filter_frame_to_filter(AVFilterLink *link)
1200 {
1201 835063 FilterLinkInternal * const li = ff_link_internal(link);
1202 835063 AVFrame *frame = NULL;
1203 835063 AVFilterContext *dst = link->dst;
1204 int ret;
1205
1206 av_assert1(ff_framequeue_queued_frames(&li->fifo));
1207 1670126 ret = li->l.min_samples ?
1208
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 835027 times.
835063 ff_inlink_consume_samples(link, li->l.min_samples, li->l.max_samples, &frame) :
1209 835027 ff_inlink_consume_frame(link, &frame);
1210 av_assert1(ret);
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 835063 times.
835063 if (ret < 0) {
1212 av_assert1(!frame);
1213 ✗ return ret;
1214 }
1215 /* The filter will soon have received a new frame, that may allow it to
1216 produce one or more: unblock its outputs. */
1217 835063 filter_unblock(dst);
1218 /* AVFilterPad.filter_frame() expect frame_count_out to have the value
1219 before the frame; filter_frame_framed() will re-increment it. */
1220 835063 li->l.frame_count_out--;
1221 835063 ret = filter_frame_framed(link, frame);
1222
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 835063 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
835063 if (ret < 0 && ret != li->status_out) {
1223 ✗ link_set_out_status(link, ret, AV_NOPTS_VALUE);
1224 } else {
1225 /* Run once again, to see if several frames were available, or if
1226 the input status has also changed, or any other reason. */
1227 835063 ff_filter_set_ready(dst, 300);
1228 }
1229 835063 return ret;
1230 }
1231
1232 6563 static int forward_status_change(AVFilterContext *filter, FilterLinkInternal *li_in)
1233 {
1234 6563 AVFilterLink *in = &li_in->l.pub;
1235 6563 unsigned out = 0, progress = 0;
1236 int ret;
1237
1238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6563 times.
6563 av_assert0(!li_in->status_out);
1239
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6562 times.
6563 if (!filter->nb_outputs) {
1240 /* not necessary with the current API and sinks */
1241 1 return 0;
1242 }
1243
2/2
✓ Branch 0 taken 6562 times.
✓ Branch 1 taken 6562 times.
13124 while (!li_in->status_out) {
1244 6562 FilterLinkInternal *li_out = ff_link_internal(filter->outputs[out]);
1245
1246
1/2
✓ Branch 0 taken 6562 times.
✗ Branch 1 not taken.
6562 if (!li_out->status_in) {
1247 6562 progress++;
1248 6562 ret = request_frame_to_filter(filter->outputs[out]);
1249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6562 times.
6562 if (ret < 0)
1250 ✗ return ret;
1251 }
1252
1/2
✓ Branch 0 taken 6562 times.
✗ Branch 1 not taken.
6562 if (++out == filter->nb_outputs) {
1253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6562 times.
6562 if (!progress) {
1254 /* Every output already closed: input no longer interesting
1255 (example: overlay in shortest mode, other input closed). */
1256 ✗ link_set_out_status(in, li_in->status_in, li_in->status_in_pts);
1257 ✗ return 0;
1258 }
1259 6562 progress = 0;
1260 6562 out = 0;
1261 }
1262 }
1263 6562 ff_filter_set_ready(filter, 200);
1264 6562 return 0;
1265 }
1266
1267 2505842 static int filter_activate_default(AVFilterContext *filter)
1268 {
1269 unsigned i;
1270 2505842 int nb_eofs = 0;
1271
1272
2/2
✓ Branch 0 taken 2505809 times.
✓ Branch 1 taken 2505842 times.
5011651 for (i = 0; i < filter->nb_outputs; i++)
1273 2505809 nb_eofs += ff_outlink_get_status(filter->outputs[i]) == AVERROR_EOF;
1274
4/4
✓ Branch 0 taken 2505809 times.
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 7350 times.
✓ Branch 3 taken 2498459 times.
2505842 if (filter->nb_outputs && nb_eofs == filter->nb_outputs) {
1275
2/2
✓ Branch 0 taken 7350 times.
✓ Branch 1 taken 7350 times.
14700 for (int j = 0; j < filter->nb_inputs; j++)
1276 7350 ff_inlink_set_status(filter->inputs[j], AVERROR_EOF);
1277 7350 return 0;
1278 }
1279
1280
2/2
✓ Branch 0 taken 2498228 times.
✓ Branch 1 taken 1663429 times.
4161657 for (i = 0; i < filter->nb_inputs; i++) {
1281 2498228 FilterLinkInternal *li = ff_link_internal(filter->inputs[i]);
1282
2/2
✓ Branch 1 taken 835063 times.
✓ Branch 2 taken 1663165 times.
2498228 if (samples_ready(li, li->l.min_samples)) {
1283 835063 return filter_frame_to_filter(filter->inputs[i]);
1284 }
1285 }
1286
2/2
✓ Branch 0 taken 1663165 times.
✓ Branch 1 taken 1656866 times.
3320031 for (i = 0; i < filter->nb_inputs; i++) {
1287 1663165 FilterLinkInternal * const li = ff_link_internal(filter->inputs[i]);
1288
4/4
✓ Branch 0 taken 6598 times.
✓ Branch 1 taken 1656567 times.
✓ Branch 2 taken 6563 times.
✓ Branch 3 taken 35 times.
1663165 if (li->status_in && !li->status_out) {
1289 av_assert1(!ff_framequeue_queued_frames(&li->fifo));
1290 6563 return forward_status_change(filter, li);
1291 }
1292 }
1293
2/2
✓ Branch 0 taken 1656850 times.
✓ Branch 1 taken 835361 times.
2492211 for (i = 0; i < filter->nb_outputs; i++) {
1294 1656850 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
1295
2/2
✓ Branch 0 taken 827067 times.
✓ Branch 1 taken 829783 times.
1656850 if (li->frame_wanted_out &&
1296
2/2
✓ Branch 0 taken 821505 times.
✓ Branch 1 taken 5562 times.
827067 !li->frame_blocked_in) {
1297 821505 return request_frame_to_filter(filter->outputs[i]);
1298 }
1299 }
1300
2/2
✓ Branch 0 taken 835345 times.
✓ Branch 1 taken 829799 times.
1665144 for (i = 0; i < filter->nb_outputs; i++) {
1301 835345 FilterLinkInternal * const li = ff_link_internal(filter->outputs[i]);
1302
2/2
✓ Branch 0 taken 5562 times.
✓ Branch 1 taken 829783 times.
835345 if (li->frame_wanted_out)
1303 5562 return request_frame_to_filter(filter->outputs[i]);
1304 }
1305
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 829783 times.
829799 if (!filter->nb_outputs) {
1306 16 ff_inlink_request_frame(filter->inputs[0]);
1307 16 return 0;
1308 }
1309 829783 return FFERROR_NOT_READY;
1310 }
1311
1312 /*
1313 Filter scheduling and activation
1314
1315 When a filter is activated, it must:
1316 - if possible, output a frame;
1317 - else, if relevant, forward the input status change;
1318 - else, check outputs for wanted frames and forward the requests.
1319
1320 The following AVFilterLink fields are used for activation:
1321
1322 - frame_wanted_out:
1323
1324 This field indicates if a frame is needed on this input of the
1325 destination filter. A positive value indicates that a frame is needed
1326 to process queued frames or internal data or to satisfy the
1327 application; a zero value indicates that a frame is not especially
1328 needed but could be processed anyway; a negative value indicates that a
1329 frame would just be queued.
1330
1331 It is set by filters using ff_request_frame() or ff_request_no_frame(),
1332 when requested by the application through a specific API or when it is
1333 set on one of the outputs.
1334
1335 It is cleared when a frame is sent from the source using
1336 ff_filter_frame().
1337
1338 It is also cleared when a status change is sent from the source using
1339 ff_avfilter_link_set_in_status().
1340
1341 - frame_blocked_in:
1342
1343 This field means that the source filter can not generate a frame as is.
1344 Its goal is to avoid repeatedly calling the request_frame() method on
1345 the same link.
1346
1347 It is set by the framework on all outputs of a filter before activating it.
1348
1349 It is automatically cleared by ff_filter_frame().
1350
1351 It is also automatically cleared by ff_avfilter_link_set_in_status().
1352
1353 It is also cleared on all outputs (using filter_unblock()) when
1354 something happens on an input: processing a frame or changing the
1355 status.
1356
1357 - fifo:
1358
1359 Contains the frames queued on a filter input. If it contains frames and
1360 frame_wanted_out is not set, then the filter can be activated. If that
1361 result in the filter not able to use these frames, the filter must set
1362 frame_wanted_out to ask for more frames.
1363
1364 - status_in and status_in_pts:
1365
1366 Status (EOF or error code) of the link and timestamp of the status
1367 change (in link time base, same as frames) as seen from the input of
1368 the link. The status change is considered happening after the frames
1369 queued in fifo.
1370
1371 It is set by the source filter using ff_avfilter_link_set_in_status().
1372
1373 - status_out:
1374
1375 Status of the link as seen from the output of the link. The status
1376 change is considered having already happened.
1377
1378 It is set by the destination filter using
1379 link_set_out_status().
1380
1381 Filters are activated according to the ready field, set using the
1382 ff_filter_set_ready(). Eventually, a priority queue will be used.
1383 ff_filter_set_ready() is called whenever anything could cause progress to
1384 be possible. Marking a filter ready when it is not is not a problem,
1385 except for the small overhead it causes.
1386
1387 Conditions that cause a filter to be marked ready are:
1388
1389 - frames added on an input link;
1390
1391 - changes in the input or output status of an input link;
1392
1393 - requests for a frame on an output link;
1394
1395 - after any actual processing using the legacy methods (filter_frame(),
1396 and request_frame() to acknowledge status changes), to run once more
1397 and check if enough input was present for several frames.
1398
1399 Examples of scenarios to consider:
1400
1401 - buffersrc: activate if frame_wanted_out to notify the application;
1402 activate when the application adds a frame to push it immediately.
1403
1404 - testsrc: activate only if frame_wanted_out to produce and push a frame.
1405
1406 - concat (not at stitch points): can process a frame on any output.
1407 Activate if frame_wanted_out on output to forward on the corresponding
1408 input. Activate when a frame is present on input to process it
1409 immediately.
1410
1411 - framesync: needs at least one frame on each input; extra frames on the
1412 wrong input will accumulate. When a frame is first added on one input,
1413 set frame_wanted_out<0 on it to avoid getting more (would trigger
1414 testsrc) and frame_wanted_out>0 on the other to allow processing it.
1415
1416 Activation of old filters:
1417
1418 In order to activate a filter implementing the legacy filter_frame() and
1419 request_frame() methods, perform the first possible of the following
1420 actions:
1421
1422 - If an input has frames in fifo and frame_wanted_out == 0, dequeue a
1423 frame and call filter_frame().
1424
1425 Rationale: filter frames as soon as possible instead of leaving them
1426 queued; frame_wanted_out < 0 is not possible since the old API does not
1427 set it nor provides any similar feedback; frame_wanted_out > 0 happens
1428 when min_samples > 0 and there are not enough samples queued.
1429
1430 - If an input has status_in set but not status_out, try to call
1431 request_frame() on one of the outputs in the hope that it will trigger
1432 request_frame() on the input with status_in and acknowledge it. This is
1433 awkward and fragile, filters with several inputs or outputs should be
1434 updated to direct activation as soon as possible.
1435
1436 - If an output has frame_wanted_out > 0 and not frame_blocked_in, call
1437 request_frame().
1438
1439 Rationale: checking frame_blocked_in is necessary to avoid requesting
1440 repeatedly on a blocked input if another is not blocked (example:
1441 [buffersrc1][testsrc1][buffersrc2][testsrc2]concat=v=2).
1442
1443 - If an output has frame_wanted_out > 0 call request_frame().
1444
1445 Rationale: even if all inputs are blocked an activate callback should
1446 request a frame on some if its inputs if a frame is requested on any of
1447 its output.
1448
1449 - Request a frame on the input for sinks.
1450
1451 Rationale: sinks using the old api have no way to request a frame on their
1452 input, so we need to do it for them.
1453 */
1454
1455 4374964 int ff_filter_activate(AVFilterContext *filter)
1456 {
1457 4374964 FFFilterContext *ctxi = fffilterctx(filter);
1458 4374964 const FFFilter *const fi = fffilter(filter->filter);
1459 int ret;
1460
1461 /* Generic timeline support is not yet implemented but should be easy */
1462 av_assert1(!(fi->p.flags & AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC &&
1463 fi->activate));
1464 4374964 ctxi->ready = 0;
1465
2/2
✓ Branch 0 taken 1869122 times.
✓ Branch 1 taken 2505842 times.
4374964 ret = fi->activate ? fi->activate(filter) : filter_activate_default(filter);
1466
2/2
✓ Branch 0 taken 830704 times.
✓ Branch 1 taken 3544260 times.
4374964 if (ret == FFERROR_NOT_READY)
1467 830704 ret = 0;
1468 4374964 return ret;
1469 }
1470
1471 3957442 int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
1472 {
1473 3957442 FilterLinkInternal * const li = ff_link_internal(link);
1474 3957442 *rpts = li->l.current_pts;
1475
2/2
✓ Branch 1 taken 2185 times.
✓ Branch 2 taken 3955257 times.
3957442 if (ff_framequeue_queued_frames(&li->fifo))
1476 2185 return *rstatus = 0;
1477
2/2
✓ Branch 0 taken 8193 times.
✓ Branch 1 taken 3947064 times.
3955257 if (li->status_out)
1478 8193 return *rstatus = li->status_out;
1479
2/2
✓ Branch 0 taken 3938278 times.
✓ Branch 1 taken 8786 times.
3947064 if (!li->status_in)
1480 3938278 return *rstatus = 0;
1481 8786 *rstatus = li->status_out = li->status_in;
1482 8786 update_link_current_pts(li, li->status_in_pts);
1483 8786 *rpts = li->l.current_pts;
1484 8786 return 1;
1485 }
1486
1487 1934 size_t ff_inlink_queued_frames(AVFilterLink *link)
1488 {
1489 1934 FilterLinkInternal * const li = ff_link_internal(link);
1490 1934 return ff_framequeue_queued_frames(&li->fifo);
1491 }
1492
1493 5708519 int ff_inlink_check_available_frame(AVFilterLink *link)
1494 {
1495 5708519 FilterLinkInternal * const li = ff_link_internal(link);
1496 5708519 return ff_framequeue_queued_frames(&li->fifo) > 0;
1497 }
1498
1499 8312 int ff_inlink_queued_samples(AVFilterLink *link)
1500 {
1501 8312 FilterLinkInternal * const li = ff_link_internal(link);
1502 8312 return ff_framequeue_queued_samples(&li->fifo);
1503 }
1504
1505 4875 int ff_inlink_check_available_samples(AVFilterLink *link, unsigned min)
1506 {
1507 4875 FilterLinkInternal * const li = ff_link_internal(link);
1508 4875 uint64_t samples = ff_framequeue_queued_samples(&li->fifo);
1509 av_assert1(min);
1510
6/6
✓ Branch 0 taken 1944 times.
✓ Branch 1 taken 2931 times.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 1877 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 60 times.
4875 return samples >= min || (li->status_in && samples);
1511 }
1512
1513 1760933 static void consume_update(FilterLinkInternal *li, const AVFrame *frame)
1514 {
1515 1760933 AVFilterLink *const link = &li->l.pub;
1516 1760933 update_link_current_pts(li, frame->pts);
1517 1760933 ff_inlink_process_commands(link, frame);
1518
2/2
✓ Branch 0 taken 1757902 times.
✓ Branch 1 taken 3031 times.
1760933 if (link == link->dst->inputs[0])
1519 1757902 link->dst->is_disabled = !evaluate_timeline_at_frame(link, frame);
1520 1760933 li->l.frame_count_out++;
1521 1760933 li->l.sample_count_out += frame->nb_samples;
1522 1760933 }
1523
1524 5705462 int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
1525 {
1526 5705462 FilterLinkInternal * const li = ff_link_internal(link);
1527 AVFrame *frame;
1528
1529 5705462 *rframe = NULL;
1530
2/2
✓ Branch 1 taken 3947462 times.
✓ Branch 2 taken 1758000 times.
5705462 if (!ff_inlink_check_available_frame(link))
1531 3947462 return 0;
1532
1533
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1757996 times.
1758000 if (li->fifo.samples_skipped) {
1534 4 frame = ff_framequeue_peek(&li->fifo, 0);
1535 4 return ff_inlink_consume_samples(link, frame->nb_samples, frame->nb_samples, rframe);
1536 }
1537
1538 1757996 frame = ff_framequeue_take(&li->fifo);
1539 1757996 consume_update(li, frame);
1540 1757996 *rframe = frame;
1541 1757996 return 1;
1542 }
1543
1544 4871 int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max,
1545 AVFrame **rframe)
1546 {
1547 4871 FilterLinkInternal * const li = ff_link_internal(link);
1548 AVFrame *frame;
1549 int ret;
1550
1551 av_assert1(min);
1552 4871 *rframe = NULL;
1553
2/2
✓ Branch 1 taken 1934 times.
✓ Branch 2 taken 2937 times.
4871 if (!ff_inlink_check_available_samples(link, min))
1554 1934 return 0;
1555
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 2910 times.
2937 if (li->status_in)
1556
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 21 times.
27 min = FFMIN(min, ff_framequeue_queued_samples(&li->fifo));
1557 2937 ret = take_samples(li, min, max, &frame);
1558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2937 times.
2937 if (ret < 0)
1559 ✗ return ret;
1560 2937 consume_update(li, frame);
1561 2937 *rframe = frame;
1562 2937 return 1;
1563 }
1564
1565 532 AVFrame *ff_inlink_peek_frame(AVFilterLink *link, size_t idx)
1566 {
1567 532 FilterLinkInternal * const li = ff_link_internal(link);
1568 532 return ff_framequeue_peek(&li->fifo, idx);
1569 }
1570
1571 599024 int ff_inlink_check_frame_params(AVFilterLink *link, const AVFrame *frame,
1572 int dynamic_flags)
1573 {
1574 599024 const FilterLinkInternal *li = ff_link_internal(link);
1575
1576
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 598866 times.
599024 if ((frame->format == li->cfg_format ||
1577
1/2
✓ Branch 0 taken 158 times.
✗ Branch 1 not taken.
158 dynamic_flags & FF_FILTER_FLAG_DYNAMIC_FRAME_FORMAT) &&
1578
3/4
✓ Branch 0 taken 599019 times.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 599019 times.
599024 (frame->width == li->cfg_w && frame->height == li->cfg_h ||
1579
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 dynamic_flags & FF_FILTER_FLAG_DYNAMIC_FRAME_SIZE))
1580 599024 return 0;
1581
1582 ✗ av_log(link->dst, AV_LOG_ERROR,
1583 "Frame size or format changed without filter graph reinitialization\n");
1584 ✗ return AVERROR(EINVAL);
1585 }
1586
1587 5189 int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
1588 {
1589 5189 AVFrame *frame = *rframe;
1590 AVFrame *out;
1591 int ret;
1592
1593
2/2
✓ Branch 1 taken 2286 times.
✓ Branch 2 taken 2903 times.
5189 if (av_frame_is_writable(frame))
1594 2286 return 0;
1595 2903 av_log(link->dst, AV_LOG_DEBUG, "Copying data in avfilter.\n");
1596
1597
1/3
✓ Branch 0 taken 2903 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2903 switch (link->type) {
1598 2903 case AVMEDIA_TYPE_VIDEO:
1599
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2903 times.
2903 if (frame->format != link->format)
1600 ✗ return av_frame_make_writable(frame);
1601 2903 out = ff_get_video_buffer(link, frame->width, frame->height);
1602 2903 break;
1603 ✗ case AVMEDIA_TYPE_AUDIO:
1604 ✗ out = ff_get_audio_buffer(link, frame->nb_samples);
1605 ✗ break;
1606 ✗ default:
1607 ✗ return AVERROR(EINVAL);
1608 }
1609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2903 times.
2903 if (!out)
1610 ✗ return AVERROR(ENOMEM);
1611
1612 2903 ret = av_frame_copy_props(out, frame);
1613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2903 times.
2903 if (ret < 0) {
1614 ✗ av_frame_free(&out);
1615 ✗ return ret;
1616 }
1617
1618 2903 ret = av_frame_copy(out, frame);
1619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2903 times.
2903 if (ret < 0) {
1620 ✗ av_frame_free(&out);
1621 ✗ return ret;
1622 }
1623
1624 2903 av_frame_free(&frame);
1625 2903 *rframe = out;
1626 2903 return 0;
1627 }
1628
1629 2595996 int ff_inlink_process_commands(AVFilterLink *link, const AVFrame *frame)
1630 {
1631 2595996 FFFilterContext *ctxi = fffilterctx(link->dst);
1632 2595996 AVFilterCommand *cmd = ctxi->command_queue;
1633
1634
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2595996 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2595996 while(cmd && cmd->time <= frame->pts * av_q2d(link->time_base)){
1635 ✗ av_log(link->dst, AV_LOG_DEBUG,
1636 "Processing command time:%f command:%s arg:%s\n",
1637 cmd->time, cmd->command, cmd->arg);
1638 ✗ avfilter_process_command(link->dst, cmd->command, cmd->arg, 0, 0, cmd->flags);
1639 ✗ command_queue_pop(link->dst);
1640 ✗ cmd = ctxi->command_queue;
1641 }
1642 2595996 return 0;
1643 }
1644
1645 927090 void ff_inlink_request_frame(AVFilterLink *link)
1646 {
1647 927090 av_unused FilterLinkInternal *li = ff_link_internal(link);
1648 av_assert1(!li->status_in);
1649 av_assert1(!li->status_out);
1650 927090 li->frame_wanted_out = 1;
1651 927090 ff_filter_set_ready(link->src, 100);
1652 927090 }
1653
1654 11944 void ff_inlink_set_status(AVFilterLink *link, int status)
1655 {
1656 11944 FilterLinkInternal * const li = ff_link_internal(link);
1657
2/2
✓ Branch 0 taken 9330 times.
✓ Branch 1 taken 2614 times.
11944 if (li->status_out)
1658 9330 return;
1659 2614 li->frame_wanted_out = 0;
1660 2614 li->frame_blocked_in = 0;
1661 2614 link_set_out_status(link, status, AV_NOPTS_VALUE);
1662
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2614 times.
2623 while (ff_framequeue_queued_frames(&li->fifo)) {
1663 9 AVFrame *frame = ff_framequeue_take(&li->fifo);
1664 9 av_frame_free(&frame);
1665 }
1666
2/2
✓ Branch 0 taken 2609 times.
✓ Branch 1 taken 5 times.
2614 if (!li->status_in)
1667 2609 li->status_in = status;
1668 }
1669
1670 4303413 int ff_outlink_get_status(AVFilterLink *link)
1671 {
1672 4303413 FilterLinkInternal * const li = ff_link_internal(link);
1673 4303413 return li->status_in;
1674 }
1675
1676 1784 int ff_inoutlink_check_flow(AVFilterLink *inlink, AVFilterLink *outlink)
1677 {
1678 1784 FilterLinkInternal * const li_in = ff_link_internal(inlink);
1679
2/2
✓ Branch 1 taken 1059 times.
✓ Branch 2 taken 70 times.
2913 return ff_outlink_frame_wanted(outlink) ||
1680
2/2
✓ Branch 0 taken 1129 times.
✓ Branch 1 taken 655 times.
2913 ff_inlink_check_available_frame(inlink) ||
1681
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 997 times.
1059 li_in->status_out;
1682 }
1683
1684
1685 ✗ const AVClass *avfilter_get_class(void)
1686 {
1687 ✗ return &avfilter_class;
1688 }
1689
1690 ✗ int ff_filter_init_hw_frames(AVFilterContext *avctx, AVFilterLink *link,
1691 int default_pool_size)
1692 {
1693 ✗ FilterLink *l = ff_filter_link(link);
1694 AVHWFramesContext *frames;
1695
1696 // Must already be set by caller.
1697 ✗ av_assert0(l->hw_frames_ctx);
1698
1699 ✗ frames = (AVHWFramesContext*)l->hw_frames_ctx->data;
1700
1701 ✗ if (frames->initial_pool_size == 0) {
1702 // Dynamic allocation is necessarily supported.
1703 ✗ } else if (avctx->extra_hw_frames >= 0) {
1704 ✗ frames->initial_pool_size += avctx->extra_hw_frames;
1705 } else {
1706 ✗ frames->initial_pool_size = default_pool_size;
1707 }
1708
1709 ✗ return 0;
1710 }
1711
1712 507931 int ff_outlink_frame_wanted(AVFilterLink *link)
1713 {
1714 507931 FilterLinkInternal * const li = ff_link_internal(link);
1715 507931 return li->frame_wanted_out;
1716 }
1717
1718 5381 int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func,
1719 void *arg, int *ret, int nb_jobs)
1720 {
1721 5381 return fffilterctx(ctx)->execute(ctx, func, arg, ret, nb_jobs);
1722 }
1723