FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2026-05-02 03:33:10
Exec Total Coverage
Lines: 664 880 75.5%
Functions: 37 41 90.2%
Branches: 563 827 68.1%

Line Branch Exec Source
1 /*
2 * filter graphs
3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34
35
36 #include "avfilter.h"
37 #include "avfilter_internal.h"
38 #include "buffersink.h"
39 #include "filters.h"
40 #include "formats.h"
41 #include "framequeue.h"
42 #include "video.h"
43
44 #define OFFSET(x) offsetof(AVFilterGraph, x)
45 #define F AV_OPT_FLAG_FILTERING_PARAM
46 #define V AV_OPT_FLAG_VIDEO_PARAM
47 #define A AV_OPT_FLAG_AUDIO_PARAM
48 static const AVOption filtergraph_options[] = {
49 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
50 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, F|V|A, .unit = "thread_type" },
51 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = F|V|A, .unit = "thread_type" },
52 { "threads", "Maximum number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
53 { .i64 = 0 }, 0, INT_MAX, F|V|A, .unit = "threads"},
54 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = F|V|A, .unit = "threads"},
55 {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) ,
56 AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|V },
57 {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) ,
58 AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|A },
59 {"max_buffered_frames" , "maximum number of buffered frames allowed", OFFSET(max_buffered_frames),
60 AV_OPT_TYPE_UINT, {.i64 = 0}, 0, UINT_MAX, F|V|A },
61 { NULL },
62 };
63
64 static const AVClass filtergraph_class = {
65 .class_name = "AVFilterGraph",
66 .item_name = av_default_item_name,
67 .version = LIBAVUTIL_VERSION_INT,
68 .option = filtergraph_options,
69 .category = AV_CLASS_CATEGORY_FILTER,
70 };
71
72 #if !HAVE_THREADS
73 void ff_graph_thread_free(FFFilterGraph *graph)
74 {
75 }
76
77 int ff_graph_thread_init(FFFilterGraph *graph)
78 {
79 graph->p.thread_type = 0;
80 graph->p.nb_threads = 1;
81 return 0;
82 }
83 #endif
84
85 16405 AVFilterGraph *avfilter_graph_alloc(void)
86 {
87 16405 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
88 AVFilterGraph *ret;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16405 times.
16405 if (!graph)
91 return NULL;
92
93 16405 ret = &graph->p;
94 16405 ret->av_class = &filtergraph_class;
95 16405 av_opt_set_defaults(ret);
96 16405 ff_framequeue_global_init(&graph->frame_queues);
97
98 16405 return ret;
99 }
100
101 63893 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
102 {
103 int i, j;
104
1/2
✓ Branch 0 taken 63893 times.
✗ Branch 1 not taken.
63893 for (i = 0; i < graph->nb_filters; i++) {
105
1/2
✓ Branch 0 taken 63893 times.
✗ Branch 1 not taken.
63893 if (graph->filters[i] == filter) {
106 63893 FFSWAP(AVFilterContext*, graph->filters[i],
107 graph->filters[graph->nb_filters - 1]);
108 63893 graph->nb_filters--;
109 63893 filter->graph = NULL;
110
2/2
✓ Branch 0 taken 55733 times.
✓ Branch 1 taken 63893 times.
119626 for (j = 0; j<filter->nb_outputs; j++)
111
2/2
✓ Branch 0 taken 21070 times.
✓ Branch 1 taken 34663 times.
55733 if (filter->outputs[j])
112 21070 ff_filter_link(filter->outputs[j])->graph = NULL;
113
114 63893 return;
115 }
116 }
117 }
118
119 24521 void avfilter_graph_free(AVFilterGraph **graphp)
120 {
121 24521 AVFilterGraph *graph = *graphp;
122 24521 FFFilterGraph *graphi = fffiltergraph(graph);
123
124
2/2
✓ Branch 0 taken 8116 times.
✓ Branch 1 taken 16405 times.
24521 if (!graph)
125 8116 return;
126
127
2/2
✓ Branch 0 taken 63827 times.
✓ Branch 1 taken 16405 times.
80232 while (graph->nb_filters)
128 63827 avfilter_free(graph->filters[0]);
129
130 16405 ff_graph_thread_free(graphi);
131
132 16405 av_freep(&graphi->sink_links);
133
134 16405 av_opt_free(graph);
135
136 16405 av_freep(&graph->filters);
137 16405 av_freep(graphp);
138 }
139
140 19463 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
141 const char *name, const char *args, void *opaque,
142 AVFilterGraph *graph_ctx)
143 {
144 int ret;
145
146 19463 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19463 times.
19463 if (!*filt_ctx)
148 return AVERROR(ENOMEM);
149
150 19463 ret = avfilter_init_str(*filt_ctx, args);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19463 times.
19463 if (ret < 0)
152 goto fail;
153
154 19463 return 0;
155
156 fail:
157 avfilter_free(*filt_ctx);
158 *filt_ctx = NULL;
159 return ret;
160 }
161
162 6334 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
163 {
164 6334 fffiltergraph(graph)->disable_auto_convert = flags;
165 6334 }
166
167 63893 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
168 const AVFilter *filter,
169 const char *name)
170 {
171 AVFilterContext **filters, *s;
172 63893 FFFilterGraph *graphi = fffiltergraph(graph);
173
174
4/4
✓ Branch 0 taken 33766 times.
✓ Branch 1 taken 30127 times.
✓ Branch 2 taken 16405 times.
✓ Branch 3 taken 17361 times.
63893 if (graph->thread_type && !graphi->thread_execute) {
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16405 times.
16405 if (graph->execute) {
176 graphi->thread_execute = graph->execute;
177 } else {
178 16405 int ret = ff_graph_thread_init(graphi);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16405 times.
16405 if (ret < 0) {
180 av_log(graph, AV_LOG_ERROR, "Error initializing threading: %s.\n", av_err2str(ret));
181 return NULL;
182 }
183 }
184 }
185
186 63893 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63893 times.
63893 if (!filters)
188 return NULL;
189 63893 graph->filters = filters;
190
191 63893 s = ff_filter_alloc(filter, name);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63893 times.
63893 if (!s)
193 return NULL;
194
195 63893 graph->filters[graph->nb_filters++] = s;
196
197 63893 s->graph = graph;
198
199 63893 return s;
200 }
201
202 /**
203 * Check for the validity of graph.
204 *
205 * A graph is considered valid if all its input and output pads are
206 * connected.
207 *
208 * @return >= 0 in case of success, a negative value otherwise
209 */
210 8221 static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
211 {
212 AVFilterContext *filt;
213 int i, j;
214
215
2/2
✓ Branch 0 taken 44043 times.
✓ Branch 1 taken 8221 times.
52264 for (i = 0; i < graph->nb_filters; i++) {
216 const AVFilterPad *pad;
217 44043 filt = graph->filters[i];
218
219
2/2
✓ Branch 0 taken 35784 times.
✓ Branch 1 taken 44043 times.
79827 for (j = 0; j < filt->nb_inputs; j++) {
220
2/4
✓ Branch 0 taken 35784 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35784 times.
35784 if (!filt->inputs[j] || !filt->inputs[j]->src) {
221 pad = &filt->input_pads[j];
222 av_log(log_ctx, AV_LOG_ERROR,
223 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
224 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
225 return AVERROR(EINVAL);
226 }
227 }
228
229
2/2
✓ Branch 0 taken 35784 times.
✓ Branch 1 taken 44043 times.
79827 for (j = 0; j < filt->nb_outputs; j++) {
230
2/4
✓ Branch 0 taken 35784 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35784 times.
35784 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
231 pad = &filt->output_pads[j];
232 av_log(log_ctx, AV_LOG_ERROR,
233 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
234 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
235 return AVERROR(EINVAL);
236 }
237 }
238 }
239
240 8221 return 0;
241 }
242
243 /**
244 * Configure all the links of graphctx.
245 *
246 * @return >= 0 in case of success, a negative value otherwise
247 */
248 8221 static int graph_config_links(AVFilterGraph *graph, void *log_ctx)
249 {
250 AVFilterContext *filt;
251 int i, ret;
252
253
2/2
✓ Branch 0 taken 45323 times.
✓ Branch 1 taken 8220 times.
53543 for (i = 0; i < graph->nb_filters; i++) {
254 45323 filt = graph->filters[i];
255
256
2/2
✓ Branch 0 taken 8360 times.
✓ Branch 1 taken 36963 times.
45323 if (!filt->nb_outputs) {
257
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8359 times.
8360 if ((ret = ff_filter_config_links(filt)))
258 1 return ret;
259 }
260 }
261
262 8220 return 0;
263 }
264
265 8220 static int graph_check_links(AVFilterGraph *graph, void *log_ctx)
266 {
267 AVFilterContext *f;
268 AVFilterLink *l;
269 unsigned i, j;
270 int ret;
271
272
2/2
✓ Branch 0 taken 45318 times.
✓ Branch 1 taken 8220 times.
53538 for (i = 0; i < graph->nb_filters; i++) {
273 45318 f = graph->filters[i];
274
2/2
✓ Branch 0 taken 37060 times.
✓ Branch 1 taken 45318 times.
82378 for (j = 0; j < f->nb_outputs; j++) {
275 37060 l = f->outputs[j];
276
2/2
✓ Branch 0 taken 30991 times.
✓ Branch 1 taken 6069 times.
37060 if (l->type == AVMEDIA_TYPE_VIDEO) {
277 30991 ret = av_image_check_size2(l->w, l->h, INT64_MAX, l->format, 0, f);
278
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30991 times.
30991 if (ret < 0)
279 return ret;
280 }
281 }
282 }
283 8220 return 0;
284 }
285
286 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
287 {
288 int i;
289
290 for (i = 0; i < graph->nb_filters; i++)
291 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
292 return graph->filters[i];
293
294 return NULL;
295 }
296
297 60378 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
298 {
299 int ret;
300
301
2/3
✓ Branch 0 taken 50779 times.
✓ Branch 1 taken 9599 times.
✗ Branch 2 not taken.
60378 switch (link->type) {
302
303 50779 case AVMEDIA_TYPE_VIDEO:
304
2/4
✓ Branch 1 taken 50779 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50779 times.
✗ Branch 4 not taken.
101558 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
305
1/2
✓ Branch 1 taken 50779 times.
✗ Branch 2 not taken.
101558 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50779 times.
101558 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0 ||
307 50779 (ret = ff_formats_check_alpha_modes(log, cfg->alpha_modes)) < 0)
308 return ret;
309 50779 break;
310
311 9599 case AVMEDIA_TYPE_AUDIO:
312
2/4
✓ Branch 1 taken 9599 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9599 times.
✗ Branch 4 not taken.
19198 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9599 times.
19198 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
314 9599 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
315 return ret;
316 9599 break;
317
318 default:
319 av_assert0(!"reached");
320 }
321 60378 return 0;
322 }
323
324 /**
325 * Check the validity of the formats / etc. lists set by query_formats().
326 *
327 * In particular, check they do not contain any redundant element.
328 */
329 37901 static int filter_check_formats(AVFilterContext *ctx)
330 {
331 unsigned i;
332 int ret;
333
334
2/2
✓ Branch 0 taken 30763 times.
✓ Branch 1 taken 37901 times.
68664 for (i = 0; i < ctx->nb_inputs; i++) {
335 30763 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30763 times.
30763 if (ret < 0)
337 return ret;
338 }
339
2/2
✓ Branch 0 taken 29615 times.
✓ Branch 1 taken 37901 times.
67516 for (i = 0; i < ctx->nb_outputs; i++) {
340 29615 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29615 times.
29615 if (ret < 0)
342 return ret;
343 }
344 37901 return 0;
345 }
346
347 45340 static int filter_query_formats(AVFilterContext *ctx)
348 {
349 45340 const FFFilter *const filter = fffilter(ctx->filter);
350 int ret;
351
352
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 45306 times.
45340 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
353
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17 times.
34 if ((ret = filter->formats.query_func(ctx)) < 0) {
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret != AVERROR(EAGAIN))
355 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
356 ctx->name, av_err2str(ret));
357 17 return ret;
358 }
359
2/2
✓ Branch 0 taken 37884 times.
✓ Branch 1 taken 7422 times.
45306 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
360 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
361 37884 AVFilterFormatsConfig **cfg_in_dyn = NULL, **cfg_out_dyn = NULL;
362 AVFilterFormatsConfig **cfg_in, **cfg_out;
363
364
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37883 times.
37884 if (ctx->nb_inputs > FF_ARRAY_ELEMS(cfg_in_stack)) {
365 1 cfg_in_dyn = av_malloc_array(ctx->nb_inputs, sizeof(*cfg_in_dyn));
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_in_dyn)
367 return AVERROR(ENOMEM);
368 1 cfg_in = cfg_in_dyn;
369 } else
370
2/2
✓ Branch 0 taken 30615 times.
✓ Branch 1 taken 7268 times.
37883 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
371
372
2/2
✓ Branch 0 taken 30740 times.
✓ Branch 1 taken 37884 times.
68624 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
373 30740 AVFilterLink *l = ctx->inputs[i];
374 30740 cfg_in[i] = &l->outcfg;
375 }
376
377
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37883 times.
37884 if (ctx->nb_outputs > FF_ARRAY_ELEMS(cfg_out_stack)) {
378 1 cfg_out_dyn = av_malloc_array(ctx->nb_outputs, sizeof(*cfg_out_dyn));
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_out_dyn) {
380 av_freep(&cfg_in_dyn);
381 return AVERROR(ENOMEM);
382 }
383 1 cfg_out = cfg_out_dyn;
384 } else
385
2/2
✓ Branch 0 taken 29524 times.
✓ Branch 1 taken 8359 times.
37883 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
386
387
2/2
✓ Branch 0 taken 29598 times.
✓ Branch 1 taken 37884 times.
67482 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
388 29598 AVFilterLink *l = ctx->outputs[i];
389 29598 cfg_out[i] = &l->incfg;
390 }
391
392 37884 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
393 37884 av_freep(&cfg_in_dyn);
394 37884 av_freep(&cfg_out_dyn);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37884 times.
37884 if (ret < 0) {
396 if (ret != AVERROR(EAGAIN))
397 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
398 ctx->name, av_err2str(ret));
399 return ret;
400 }
401 }
402
403
2/2
✓ Branch 0 taken 45306 times.
✓ Branch 1 taken 17 times.
45323 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
404
2/2
✓ Branch 0 taken 37884 times.
✓ Branch 1 taken 7422 times.
45306 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
405 37901 ret = filter_check_formats(ctx);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37901 times.
37901 if (ret < 0)
407 return ret;
408 }
409
410 45323 return ff_default_query_formats(ctx);
411 }
412
413 44117 static int formats_declared(AVFilterContext *f)
414 {
415 int i;
416
417
2/2
✓ Branch 0 taken 35687 times.
✓ Branch 1 taken 8470 times.
44157 for (i = 0; i < f->nb_inputs; i++) {
418
2/2
✓ Branch 0 taken 35645 times.
✓ Branch 1 taken 42 times.
35687 if (!f->inputs[i]->outcfg.formats)
419 35645 return 0;
420
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9 times.
42 if (f->inputs[i]->type == AVMEDIA_TYPE_VIDEO &&
421
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 !(f->inputs[i]->outcfg.color_ranges &&
422
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->inputs[i]->outcfg.color_spaces &&
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->inputs[i]->outcfg.alpha_modes))
424 2 return 0;
425
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 31 times.
40 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
426
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 !(f->inputs[i]->outcfg.samplerates &&
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 f->inputs[i]->outcfg.channel_layouts))
428 return 0;
429 }
430
2/2
✓ Branch 0 taken 8459 times.
✓ Branch 1 taken 57 times.
8516 for (i = 0; i < f->nb_outputs; i++) {
431
2/2
✓ Branch 0 taken 8413 times.
✓ Branch 1 taken 46 times.
8459 if (!f->outputs[i]->incfg.formats)
432 8413 return 0;
433
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 15 times.
46 if (f->outputs[i]->type == AVMEDIA_TYPE_VIDEO &&
434
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !(f->outputs[i]->incfg.color_ranges &&
435
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->outputs[i]->incfg.color_spaces &&
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->outputs[i]->incfg.alpha_modes))
437 return 0;
438
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 31 times.
46 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
439
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 !(f->outputs[i]->incfg.samplerates &&
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 f->outputs[i]->incfg.channel_layouts))
441 return 0;
442 }
443 57 return 1;
444 }
445
446 static void print_link_formats(void *log_ctx, int level, const AVFilterLink *l,
447 const AVFilterFormatsMerger *mergers[],
448 int nb_mergers)
449 {
450 if (av_log_get_level() < level)
451 return;
452
453 AVBPrint bp;
454 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
455
456 av_log(log_ctx, level, "Link '%s.%s' -> '%s.%s':\n",
457 l->src->name, l->srcpad->name, l->dst->name, l->dstpad->name);
458
459 for (unsigned i = 0; i < nb_mergers; i++) {
460 const AVFilterFormatsMerger *m = mergers[i];
461 av_log(log_ctx, level, " %s:\n", m->name);
462 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, l->incfg));
463 if (av_bprint_is_complete(&bp))
464 av_log(log_ctx, level, " src: %s\n", bp.str);
465 av_bprint_clear(&bp);
466
467 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, l->outcfg));
468 if (av_bprint_is_complete(&bp))
469 av_log(log_ctx, level, " dst: %s\n", bp.str);
470 av_bprint_clear(&bp);
471 }
472
473 av_bprint_finalize(&bp, NULL);
474 }
475
476 44043 static void print_filter_formats(void *log_ctx, int level, const AVFilterContext *f)
477 {
478
1/2
✓ Branch 1 taken 44043 times.
✗ Branch 2 not taken.
44043 if (av_log_get_level() < level)
479 44043 return;
480
481 AVBPrint bp;
482 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
483
484 av_log(log_ctx, level, "Filter '%s' formats:\n", f->name);
485 for (int i = 0; i < f->nb_inputs; i++) {
486 const AVFilterLink *in = f->inputs[i];
487 const AVFilterNegotiation *neg = ff_filter_get_negotiation(in);
488 av_log(log_ctx, level, " in[%d] '%s':\n", i, f->input_pads[i].name);
489
490 for (unsigned i = 0; i < neg->nb_mergers; i++) {
491 const AVFilterFormatsMerger *m = &neg->mergers[i];
492 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, in->outcfg));
493 if (av_bprint_is_complete(&bp))
494 av_log(log_ctx, level, " %s: %s\n", m->name, bp.str);
495 av_bprint_clear(&bp);
496 }
497 }
498
499 for (int i = 0; i < f->nb_outputs; i++) {
500 const AVFilterLink *out = f->outputs[i];
501 const AVFilterNegotiation *neg = ff_filter_get_negotiation(out);
502 av_log(log_ctx, level, " out[%d] '%s':\n", i, f->output_pads[i].name);
503
504 for (unsigned i = 0; i < neg->nb_mergers; i++) {
505 const AVFilterFormatsMerger *m = &neg->mergers[i];
506 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, out->incfg));
507 if (av_bprint_is_complete(&bp))
508 av_log(log_ctx, level, " %s: %s\n", m->name, bp.str);
509 av_bprint_clear(&bp);
510 }
511 }
512
513 av_bprint_finalize(&bp, NULL);
514 }
515
516 /**
517 * Perform one round of query_formats() and merging formats lists on the
518 * filter graph.
519 * @return >=0 if all links formats lists could be queried and merged;
520 * AVERROR(EAGAIN) some progress was made in the queries or merging
521 * and a later call may succeed;
522 * AVERROR(EIO) (may be changed) plus a log message if no progress
523 * was made and the negotiation is stuck;
524 * a negative error code if some other error happened
525 */
526 8236 static int query_formats(AVFilterGraph *graph, void *log_ctx)
527 {
528 int i, j, k, ret;
529 8236 int converter_count = 0;
530 8236 int count_queried = 0; /* successful calls to query_formats() */
531 8236 int count_merged = 0; /* successful merge of formats lists */
532 8236 int count_already_merged = 0; /* lists already merged */
533 8236 int count_delayed = 0; /* lists that need to be merged later */
534
535
2/2
✓ Branch 0 taken 44117 times.
✓ Branch 1 taken 8236 times.
52353 for (i = 0; i < graph->nb_filters; i++) {
536 44117 AVFilterContext *f = graph->filters[i];
537
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 44060 times.
44117 if (formats_declared(f))
538 57 continue;
539 44060 ret = filter_query_formats(f);
540
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 44043 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
44060 if (ret < 0 && ret != AVERROR(EAGAIN))
541 return ret;
542 /* note: EAGAIN could indicate a partial success, not counted yet */
543
2/2
✓ Branch 0 taken 44043 times.
✓ Branch 1 taken 17 times.
44060 if (ret >= 0) {
544 44043 print_filter_formats(log_ctx, AV_LOG_DEBUG, f);
545 44043 count_queried++;
546 }
547 }
548
549 /* go through and merge as many format lists as possible */
550 8236 retry:
551
2/2
✓ Branch 0 taken 64434 times.
✓ Branch 1 taken 8236 times.
72670 for (i = 0; i < graph->nb_filters; i++) {
552 64434 AVFilterContext *filter = graph->filters[i];
553
554
2/2
✓ Branch 0 taken 52565 times.
✓ Branch 1 taken 63154 times.
115719 for (j = 0; j < filter->nb_inputs; j++) {
555 52565 AVFilterLink *link = filter->inputs[j];
556 const AVFilterNegotiation *neg;
557 AVFilterContext *conv[4];
558 const AVFilterFormatsMerger *mergers[4]; /* triggered mergers */
559 52565 const char *conv_filters[4], *conv_opts[4] = {0};
560 52565 unsigned neg_step, num_conv = 0, num_mergers = 0;
561
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52565 times.
52565 if (!link)
563 continue;
564
565 52565 neg = ff_filter_get_negotiation(link);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52565 times.
52565 av_assert0(neg);
567
2/2
✓ Branch 0 taken 190656 times.
✓ Branch 1 taken 52565 times.
243221 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
568 190656 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
569 190656 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
570 190656 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
571
8/8
✓ Branch 0 taken 190592 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 190518 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 138940 times.
✓ Branch 5 taken 51578 times.
✓ Branch 7 taken 1356 times.
✓ Branch 8 taken 137584 times.
190656 if (a && b && a != b && !m->can_merge(a, b)) {
572
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1280 times.
1356 for (k = 0; k < num_conv; k++) {
573
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (!strcmp(conv_filters[k], m->conversion_filter))
574 76 break;
575 }
576
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 76 times.
1356 if (k == num_conv) {
577 av_assert1(num_conv < FF_ARRAY_ELEMS(conv_filters));
578 1280 conv_filters[num_conv] = m->conversion_filter;
579
2/2
✓ Branch 0 taken 1279 times.
✓ Branch 1 taken 1 times.
1280 if (m->conversion_opts_offset)
580 1279 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
581 1280 num_conv++;
582 }
583 av_assert1(num_mergers < FF_ARRAY_ELEMS(mergers));
584 1356 mergers[num_mergers++] = m;
585 }
586 }
587
2/2
✓ Branch 0 taken 190656 times.
✓ Branch 1 taken 52565 times.
243221 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
588 190656 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
589 190656 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
590 190656 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
591
4/4
✓ Branch 0 taken 190592 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 190518 times.
190656 if (!(a && b)) {
592 138 count_delayed++;
593
2/2
✓ Branch 0 taken 56860 times.
✓ Branch 1 taken 133658 times.
190518 } else if (a == b) {
594 56860 count_already_merged++;
595
2/2
✓ Branch 0 taken 129787 times.
✓ Branch 1 taken 3871 times.
133658 } else if (!num_conv) {
596 129787 count_merged++;
597 129787 ret = m->merge(a, b);
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129787 times.
129787 if (ret < 0)
599 return ret;
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129787 times.
129787 if (!ret) {
601 mergers[num_mergers++] = m;
602 conv_filters[num_conv] = m->conversion_filter;
603 if (m->conversion_opts_offset)
604 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
605 num_conv++;
606 }
607 }
608 }
609
610 /**
611 * Couldn't merge format lists; auto-insert conversion filters
612 * in reverse order to keep the order consistent with the list
613 * of mergers, since they are prepended onto the existing link
614 */
615
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 52565 times.
53845 for (k = num_conv - 1; k >= 0; k--) {
616 const AVFilter *filter;
617 char inst_name[30];
618
619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1280 times.
1280 if (fffiltergraph(graph)->disable_auto_convert) {
620 av_log(log_ctx, AV_LOG_ERROR,
621 "The filters '%s' and '%s' do not have a common format "
622 "and automatic conversion is disabled.\n",
623 link->src->name, link->dst->name);
624 print_link_formats(log_ctx, AV_LOG_ERROR, link, mergers, num_mergers);
625 return AVERROR(EINVAL);
626 }
627
628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1280 times.
1280 if (!(filter = avfilter_get_by_name(conv_filters[k]))) {
629 av_log(log_ctx, AV_LOG_ERROR,
630 "'%s' filter not present, cannot convert formats.\n",
631 conv_filters[k]);
632 print_link_formats(log_ctx, AV_LOG_ERROR, link, mergers, num_mergers);
633 return AVERROR(EINVAL);
634 }
635 1280 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
636 conv_filters[k], converter_count++);
637 1280 ret = avfilter_graph_create_filter(&conv[k], filter, inst_name,
638 conv_opts[k], NULL, graph);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 if (ret < 0)
640 return ret;
641
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1280 times.
1280 if ((ret = avfilter_insert_filter(link, conv[k], 0, 0)) < 0)
642 return ret;
643
644
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1280 times.
1280 if ((ret = filter_query_formats(conv[k])) < 0)
645 return ret;
646 }
647
648 /* preemptively settle formats of auto filters */
649
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 52565 times.
53845 for (k = 0; k < num_conv; k++) {
650 1280 AVFilterLink *inlink = conv[k]->inputs[0];
651 1280 AVFilterLink *outlink = conv[k]->outputs[0];
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 av_assert0( inlink->incfg.formats->refcount > 0);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 av_assert0( inlink->outcfg.formats->refcount > 0);
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 av_assert0(outlink->incfg.formats->refcount > 0);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1280 times.
1280 av_assert0(outlink->outcfg.formats->refcount > 0);
656
2/2
✓ Branch 0 taken 632 times.
✓ Branch 1 taken 648 times.
1280 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink-> incfg.alpha_modes->refcount > 0);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink->outcfg.alpha_modes->refcount > 0);
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink-> incfg.alpha_modes->refcount > 0);
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink->outcfg.alpha_modes->refcount > 0);
669
1/2
✓ Branch 0 taken 648 times.
✗ Branch 1 not taken.
648 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0( inlink-> incfg.samplerates->refcount > 0);
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0( inlink->outcfg.samplerates->refcount > 0);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0(outlink-> incfg.samplerates->refcount > 0);
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0(outlink->outcfg.samplerates->refcount > 0);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 648 times.
648 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
678 }
679
680 #define MERGE(merger, link) \
681 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
682 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
683
684
2/2
✓ Branch 0 taken 4472 times.
✓ Branch 1 taken 1280 times.
5752 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
685 4472 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
686
2/2
✓ Branch 0 taken 634 times.
✓ Branch 1 taken 3838 times.
4472 if (strcmp(m->conversion_filter, conv_filters[k]))
687 634 continue;
688
2/4
✓ Branch 1 taken 3838 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3838 times.
7676 if ((ret = MERGE(m, inlink)) <= 0 ||
689 3838 (ret = MERGE(m, outlink)) <= 0) {
690 if (ret < 0)
691 return ret;
692 av_log(log_ctx, AV_LOG_ERROR,
693 "Impossible to convert between the formats supported by the filter "
694 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
695 print_link_formats(log_ctx, AV_LOG_ERROR, inlink, &m, 1);
696 print_link_formats(log_ctx, AV_LOG_ERROR, outlink, &m, 1);
697 return AVERROR(ENOSYS);
698 } else {
699 3838 count_merged += 2;
700 }
701 }
702 }
703
704 /* if there is an auto filter, we may need another round to fully
705 * settle formats due to possible cross-incompatibilities between
706 * the auto filters themselves, or between the auto filters and
707 * a different attribute of the filter they are modifying */
708
2/2
✓ Branch 0 taken 1280 times.
✓ Branch 1 taken 51285 times.
52565 if (num_conv)
709 1280 goto retry;
710 }
711 }
712
713 8236 av_log(graph, AV_LOG_DEBUG, "query_formats: "
714 "%d queried, %d merged, %d already done, %d delayed\n",
715 count_queried, count_merged, count_already_merged, count_delayed);
716
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 8221 times.
8236 if (count_delayed) {
717 AVBPrint bp;
718
719 /* if count_queried > 0, one filter at least did set its formats,
720 that will give additional information to its neighbour;
721 if count_merged > 0, one pair of formats lists at least was merged,
722 that will give additional information to all connected filters;
723 in both cases, progress was made and a new round must be done */
724
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15 if (count_queried || count_merged)
725 15 return AVERROR(EAGAIN);
726 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
727 for (i = 0; i < graph->nb_filters; i++)
728 if (!formats_declared(graph->filters[i]))
729 av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
730 graph->filters[i]->name);
731 av_log(graph, AV_LOG_ERROR,
732 "The following filters could not choose their formats: %s\n"
733 "Consider inserting the (a)format filter near their input or "
734 "output.\n", bp.str);
735 return AVERROR(EIO);
736 }
737 8221 return 0;
738 }
739
740 1204 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
741 {
742 1204 int score = 0;
743
744
2/2
✓ Branch 2 taken 1105 times.
✓ Branch 3 taken 99 times.
1204 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
745 1105 score ++;
746
747
2/2
✓ Branch 2 taken 271 times.
✓ Branch 3 taken 933 times.
1204 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
748 271 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
749 }else
750 933 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
751
752
3/4
✓ Branch 1 taken 148 times.
✓ Branch 2 taken 1056 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 148 times.
1352 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
753 148 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
754 score += 20;
755
756
3/4
✓ Branch 1 taken 100 times.
✓ Branch 2 taken 1104 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 100 times.
1304 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
757 100 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
758 score += 2;
759
760 1204 return score;
761 }
762
763 602 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
764 enum AVSampleFormat src_fmt)
765 {
766 int score1, score2;
767
768 602 score1 = get_fmt_score(dst_fmt1, src_fmt);
769 602 score2 = get_fmt_score(dst_fmt2, src_fmt);
770
771
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 149 times.
602 return score1 < score2 ? dst_fmt1 : dst_fmt2;
772 }
773
774 36762 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
775 {
776 36762 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36762 times.
36762 if (!desc)
778 return 0;
779
2/2
✓ Branch 0 taken 1794 times.
✓ Branch 1 taken 34968 times.
36762 if (desc->nb_components < 3)
780 1794 return 0; /* Grayscale is explicitly full-range in swscale */
781 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
782 34968 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
783 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
784 }
785
786
787 26215 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
788 {
789
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 25463 times.
26215 switch (fmt) {
790 752 case AV_PIX_FMT_YUVJ420P:
791 case AV_PIX_FMT_YUVJ422P:
792 case AV_PIX_FMT_YUVJ444P:
793 case AV_PIX_FMT_YUVJ440P:
794 case AV_PIX_FMT_YUVJ411P:
795 752 return 1;
796 25463 default:
797 25463 return 0;
798 }
799 }
800
801 111132 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
802 {
803
3/4
✓ Branch 0 taken 111132 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74068 times.
✓ Branch 3 taken 37064 times.
111132 if (!link || !link->incfg.formats)
804 74068 return 0;
805
806
2/2
✓ Branch 0 taken 30995 times.
✓ Branch 1 taken 6069 times.
37064 if (link->type == AVMEDIA_TYPE_VIDEO) {
807
3/4
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 30894 times.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
30995 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
808 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
809 101 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
810 101 enum AVPixelFormat best= AV_PIX_FMT_NONE;
811 int i;
812
2/2
✓ Branch 0 taken 1083 times.
✓ Branch 1 taken 101 times.
1184 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
813 1083 enum AVPixelFormat p = link->incfg.formats->formats[i];
814 1083 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
815 }
816 101 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
817 101 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
818 101 av_get_pix_fmt_name(ref->format), has_alpha);
819 101 link->incfg.formats->formats[0] = best;
820 }
821
1/2
✓ Branch 0 taken 6069 times.
✗ Branch 1 not taken.
6069 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
822
3/4
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 5920 times.
✓ Branch 2 taken 149 times.
✗ Branch 3 not taken.
6069 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
823 149 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
824 int i;
825
2/2
✓ Branch 0 taken 602 times.
✓ Branch 1 taken 149 times.
751 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
826 602 enum AVSampleFormat p = link->incfg.formats->formats[i];
827 602 best = find_best_sample_fmt_of_2(best, p, ref->format);
828 }
829 149 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
830 149 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
831 149 av_get_sample_fmt_name(ref->format));
832 149 link->incfg.formats->formats[0] = best;
833 }
834 }
835
836 37064 link->incfg.formats->nb_formats = 1;
837 37064 link->format = link->incfg.formats->formats[0];
838
839
2/2
✓ Branch 0 taken 30995 times.
✓ Branch 1 taken 6069 times.
37064 if (link->type == AVMEDIA_TYPE_VIDEO) {
840 30995 enum AVPixelFormat swfmt = link->format;
841
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30995 times.
30995 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
842 // FIXME: this is a hack - we'd like to use the sw_format of
843 // link->hw_frames_ctx here, but it is not yet available.
844 // To make this work properly we will need to either reorder
845 // things so that it is available here or somehow negotiate
846 // sw_format separately.
847 swfmt = AV_PIX_FMT_YUV420P;
848 }
849
850 30995 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
851
2/2
✓ Branch 1 taken 9834 times.
✓ Branch 2 taken 21161 times.
30995 if (!ff_fmt_is_regular_yuv(swfmt)) {
852 /* These fields are explicitly documented as affecting YUV only,
853 * so set them to sane values for other formats. */
854
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 9263 times.
9834 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
855 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
856 else
857 9263 link->color_range = AVCOL_RANGE_JPEG;
858
2/2
✓ Branch 0 taken 8238 times.
✓ Branch 1 taken 1596 times.
9834 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
859 8238 link->colorspace = AVCOL_SPC_RGB;
860 } else {
861 1596 link->colorspace = AVCOL_SPC_UNSPECIFIED;
862 }
863 } else {
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21161 times.
21161 if (!link->incfg.color_spaces->nb_formats) {
865 av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
866 " the link between filters %s and %s.\n", link->src->name,
867 link->dst->name);
868 return AVERROR(EINVAL);
869 }
870 21161 link->incfg.color_spaces->nb_formats = 1;
871 21161 link->colorspace = link->incfg.color_spaces->formats[0];
872
873
2/2
✓ Branch 1 taken 678 times.
✓ Branch 2 taken 20483 times.
21161 if (ff_fmt_is_forced_full_range(swfmt)) {
874 678 link->color_range = AVCOL_RANGE_JPEG;
875 } else {
876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20483 times.
20483 if (!link->incfg.color_ranges->nb_formats) {
877 av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
878 " the link between filters %s and %s.\n", link->src->name,
879 link->dst->name);
880 return AVERROR(EINVAL);
881 }
882 20483 link->incfg.color_ranges->nb_formats = 1;
883 20483 link->color_range = link->incfg.color_ranges->formats[0];
884 }
885 }
886
887
2/2
✓ Branch 0 taken 3769 times.
✓ Branch 1 taken 27226 times.
30995 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) {
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3769 times.
3769 if (!link->incfg.alpha_modes->nb_formats) {
889 av_log(link->src, AV_LOG_ERROR, "Cannot select alpha mode for"
890 " the link between filters %s and %s.\n", link->src->name,
891 link->dst->name);
892 return AVERROR(EINVAL);
893 }
894 3769 link->incfg.alpha_modes->nb_formats = 1;
895 3769 link->alpha_mode = link->incfg.alpha_modes->formats[0];
896 } else {
897 27226 link->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
898 }
899
1/2
✓ Branch 0 taken 6069 times.
✗ Branch 1 not taken.
6069 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
900 int ret;
901
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6069 times.
6069 if (!link->incfg.samplerates->nb_formats) {
903 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
904 " the link between filters %s and %s.\n", link->src->name,
905 link->dst->name);
906 return AVERROR(EINVAL);
907 }
908 6069 link->incfg.samplerates->nb_formats = 1;
909 6069 link->sample_rate = link->incfg.samplerates->formats[0];
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6069 times.
6069 if (link->incfg.channel_layouts->all_layouts) {
912 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
913 " the link between filters %s and %s.\n", link->src->name,
914 link->dst->name);
915 if (!link->incfg.channel_layouts->all_counts)
916 av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
917 "supported, try specifying a channel layout using "
918 "'aformat=channel_layouts=something'.\n");
919 return AVERROR(EINVAL);
920 }
921 6069 link->incfg.channel_layouts->nb_channel_layouts = 1;
922 6069 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6069 times.
6069 if (ret < 0)
924 return ret;
925 }
926
927 37064 ff_formats_unref(&link->incfg.formats);
928 37064 ff_formats_unref(&link->outcfg.formats);
929 37064 ff_formats_unref(&link->incfg.samplerates);
930 37064 ff_formats_unref(&link->outcfg.samplerates);
931 37064 ff_channel_layouts_unref(&link->incfg.channel_layouts);
932 37064 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
933 37064 ff_formats_unref(&link->incfg.color_spaces);
934 37064 ff_formats_unref(&link->outcfg.color_spaces);
935 37064 ff_formats_unref(&link->incfg.color_ranges);
936 37064 ff_formats_unref(&link->outcfg.color_ranges);
937 37064 ff_formats_unref(&link->incfg.alpha_modes);
938 37064 ff_formats_unref(&link->outcfg.alpha_modes);
939
940 37064 return 0;
941 }
942
943 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
944 do { \
945 for (i = 0; i < filter->nb_inputs; i++) { \
946 AVFilterLink *link = filter->inputs[i]; \
947 fmt_type fmt; \
948 \
949 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
950 continue; \
951 fmt = link->outcfg.list->var[0]; \
952 \
953 for (j = 0; j < filter->nb_outputs; j++) { \
954 AVFilterLink *out_link = filter->outputs[j]; \
955 list_type *fmts; \
956 \
957 if (link->type != out_link->type || \
958 out_link->incfg.list->nb == 1) \
959 continue; \
960 fmts = out_link->incfg.list; \
961 \
962 if (!out_link->incfg.list->nb) { \
963 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
964 return ret; \
965 ret = 1; \
966 break; \
967 } \
968 \
969 for (k = 0; k < out_link->incfg.list->nb; k++) \
970 if (fmts->var[k] == fmt) { \
971 fmts->var[0] = fmt; \
972 fmts->nb = 1; \
973 ret = 1; \
974 break; \
975 } \
976 } \
977 } \
978 } while (0)
979
980 72318 static int reduce_formats_on_filter(AVFilterContext *filter)
981 {
982 72318 int i, j, k, ret = 0;
983
984
16/20
✓ Branch 0 taken 59418 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1427 times.
✓ Branch 3 taken 57991 times.
✓ Branch 4 taken 45340 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 44771 times.
✓ Branch 7 taken 569 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 569 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 55 times.
✓ Branch 14 taken 4822 times.
✓ Branch 15 taken 4877 times.
✓ Branch 16 taken 514 times.
✓ Branch 17 taken 45352 times.
✓ Branch 18 taken 57991 times.
✓ Branch 19 taken 59418 times.
✓ Branch 20 taken 72318 times.
181910 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
985 nb_formats, ff_add_format);
986
18/20
✓ Branch 0 taken 10937 times.
✓ Branch 1 taken 48481 times.
✓ Branch 2 taken 1208 times.
✓ Branch 3 taken 9729 times.
✓ Branch 4 taken 7880 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 7094 times.
✓ Branch 7 taken 786 times.
✓ Branch 8 taken 749 times.
✓ Branch 9 taken 37 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 749 times.
✓ Branch 13 taken 37 times.
✓ Branch 14 taken 68 times.
✓ Branch 15 taken 105 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 7886 times.
✓ Branch 18 taken 8980 times.
✓ Branch 19 taken 59418 times.
✓ Branch 20 taken 72318 times.
138941 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
987 nb_formats, ff_add_format);
988
15/20
✓ Branch 0 taken 48481 times.
✓ Branch 1 taken 10937 times.
✓ Branch 2 taken 11391 times.
✓ Branch 3 taken 37090 times.
✓ Branch 4 taken 29078 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25154 times.
✓ Branch 7 taken 3924 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3924 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3924 times.
✓ Branch 14 taken 425 times.
✓ Branch 15 taken 4349 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 29078 times.
✓ Branch 18 taken 37090 times.
✓ Branch 19 taken 59418 times.
✓ Branch 20 taken 72318 times.
161239 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
989 nb_formats, ff_add_format);
990
15/20
✓ Branch 0 taken 48481 times.
✓ Branch 1 taken 10937 times.
✓ Branch 2 taken 8746 times.
✓ Branch 3 taken 39735 times.
✓ Branch 4 taken 30433 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27115 times.
✓ Branch 7 taken 3318 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3318 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3318 times.
✓ Branch 14 taken 3299 times.
✓ Branch 15 taken 6617 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 30433 times.
✓ Branch 18 taken 39735 times.
✓ Branch 19 taken 59418 times.
✓ Branch 20 taken 72318 times.
165468 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
991 nb_formats, ff_add_format);
992
10/20
✓ Branch 0 taken 48481 times.
✓ Branch 1 taken 10937 times.
✓ Branch 2 taken 47879 times.
✓ Branch 3 taken 602 times.
✓ Branch 4 taken 460 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 460 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 460 times.
✓ Branch 18 taken 602 times.
✓ Branch 19 taken 59418 times.
✓ Branch 20 taken 72318 times.
132196 REDUCE_FORMATS(int, AVFilterFormats, alpha_modes, formats,
993 nb_formats, ff_add_format);
994
995 /* reduce channel layouts */
996
2/2
✓ Branch 0 taken 59418 times.
✓ Branch 1 taken 72318 times.
131736 for (i = 0; i < filter->nb_inputs; i++) {
997 59418 AVFilterLink *inlink = filter->inputs[i];
998 const AVChannelLayout *fmt;
999
1000
2/2
✓ Branch 0 taken 10937 times.
✓ Branch 1 taken 48481 times.
59418 if (!inlink->outcfg.channel_layouts ||
1001
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 10858 times.
10937 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
1002 48560 continue;
1003 10858 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
1004
1005
2/2
✓ Branch 0 taken 8619 times.
✓ Branch 1 taken 10014 times.
18633 for (j = 0; j < filter->nb_outputs; j++) {
1006 8619 AVFilterLink *outlink = filter->outputs[j];
1007 AVFilterChannelLayouts *fmts;
1008
1009 8619 fmts = outlink->incfg.channel_layouts;
1010
4/4
✓ Branch 0 taken 8613 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7727 times.
✓ Branch 3 taken 886 times.
8619 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
1011 7733 continue;
1012
1013
2/2
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 42 times.
886 if (fmts->all_layouts &&
1014
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 837 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
844 (KNOWN(fmt) || fmts->all_counts)) {
1015 /* Turn the infinite list into a singleton */
1016 844 fmts->all_layouts = fmts->all_counts = 0;
1017 844 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
844 if (ret < 0)
1019 return ret;
1020 844 ret = 1;
1021 844 break;
1022 }
1023
1024
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 6 times.
129 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
1025
2/2
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 87 times.
123 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
1026 36 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
36 if (ret < 0)
1028 return ret;
1029 36 fmts->nb_channel_layouts = 1;
1030 36 ret = 1;
1031 36 break;
1032 }
1033 }
1034 }
1035 }
1036
1037 72318 return ret;
1038 }
1039
1040 8221 static int reduce_formats(AVFilterGraph *graph)
1041 {
1042 int i, reduced, ret;
1043
1044 do {
1045 12826 reduced = 0;
1046
1047
2/2
✓ Branch 0 taken 72318 times.
✓ Branch 1 taken 12826 times.
85144 for (i = 0; i < graph->nb_filters; i++) {
1048
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72318 times.
72318 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
1049 return ret;
1050 72318 reduced |= ret;
1051 }
1052
2/2
✓ Branch 0 taken 4605 times.
✓ Branch 1 taken 8221 times.
12826 } while (reduced);
1053
1054 8221 return 0;
1055 }
1056
1057 45323 static void swap_samplerates_on_filter(AVFilterContext *filter)
1058 {
1059 45323 AVFilterLink *link = NULL;
1060 int sample_rate;
1061 int i, j;
1062
1063
2/2
✓ Branch 0 taken 36975 times.
✓ Branch 1 taken 39339 times.
76314 for (i = 0; i < filter->nb_inputs; i++) {
1064 36975 link = filter->inputs[i];
1065
1066
2/2
✓ Branch 0 taken 5984 times.
✓ Branch 1 taken 30991 times.
36975 if (link->type == AVMEDIA_TYPE_AUDIO &&
1067
1/2
✓ Branch 0 taken 5984 times.
✗ Branch 1 not taken.
5984 link->outcfg.samplerates->nb_formats== 1)
1068 5984 break;
1069 }
1070
2/2
✓ Branch 0 taken 39339 times.
✓ Branch 1 taken 5984 times.
45323 if (i == filter->nb_inputs)
1071 39339 return;
1072
1073 5984 sample_rate = link->outcfg.samplerates->formats[0];
1074
1075
2/2
✓ Branch 0 taken 4661 times.
✓ Branch 1 taken 5984 times.
10645 for (i = 0; i < filter->nb_outputs; i++) {
1076 4661 AVFilterLink *outlink = filter->outputs[i];
1077 4661 int best_idx, best_diff = INT_MAX;
1078
1079
2/2
✓ Branch 0 taken 4659 times.
✓ Branch 1 taken 2 times.
4661 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1080
1/2
✓ Branch 0 taken 4659 times.
✗ Branch 1 not taken.
4659 outlink->incfg.samplerates->nb_formats < 2)
1081 4661 continue;
1082
1083 for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
1084 int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
1085
1086 av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
1087
1088 if (diff < best_diff) {
1089 best_diff = diff;
1090 best_idx = j;
1091 }
1092 }
1093 FFSWAP(int, outlink->incfg.samplerates->formats[0],
1094 outlink->incfg.samplerates->formats[best_idx]);
1095 }
1096 }
1097
1098 8221 static void swap_samplerates(AVFilterGraph *graph)
1099 {
1100 int i;
1101
1102
2/2
✓ Branch 0 taken 45323 times.
✓ Branch 1 taken 8221 times.
53544 for (i = 0; i < graph->nb_filters; i++)
1103 45323 swap_samplerates_on_filter(graph->filters[i]);
1104 8221 }
1105
1106 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
1107 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
1108 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
1109 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
1110 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
1111 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
1112 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
1113
1114 /* allowable substitutions for channel pairs when comparing layouts,
1115 * ordered by priority for both values */
1116 static const uint64_t ch_subst[][2] = {
1117 { CH_FRONT_PAIR, CH_CENTER_PAIR },
1118 { CH_FRONT_PAIR, CH_WIDE_PAIR },
1119 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
1120 { CH_CENTER_PAIR, CH_FRONT_PAIR },
1121 { CH_CENTER_PAIR, CH_WIDE_PAIR },
1122 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
1123 { CH_WIDE_PAIR, CH_FRONT_PAIR },
1124 { CH_WIDE_PAIR, CH_CENTER_PAIR },
1125 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
1126 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
1127 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
1128 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
1129 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
1130 { CH_SIDE_PAIR, CH_BACK_PAIR },
1131 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
1132 { CH_BACK_PAIR, CH_DIRECT_PAIR },
1133 { CH_BACK_PAIR, CH_SIDE_PAIR },
1134 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
1135 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
1136 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
1137 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
1138 };
1139
1140 45323 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1141 {
1142 45323 AVFilterLink *link = NULL;
1143 int i, j, k;
1144
1145
2/2
✓ Branch 0 taken 36975 times.
✓ Branch 1 taken 39345 times.
76320 for (i = 0; i < filter->nb_inputs; i++) {
1146 36975 link = filter->inputs[i];
1147
1148
2/2
✓ Branch 0 taken 5984 times.
✓ Branch 1 taken 30991 times.
36975 if (link->type == AVMEDIA_TYPE_AUDIO &&
1149
2/2
✓ Branch 0 taken 5978 times.
✓ Branch 1 taken 6 times.
5984 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1150 5978 break;
1151 }
1152
2/2
✓ Branch 0 taken 39345 times.
✓ Branch 1 taken 5978 times.
45323 if (i == filter->nb_inputs)
1153 39345 return;
1154
1155
2/2
✓ Branch 0 taken 4658 times.
✓ Branch 1 taken 5978 times.
10636 for (i = 0; i < filter->nb_outputs; i++) {
1156 4658 AVFilterLink *outlink = filter->outputs[i];
1157 4658 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1158
1159
2/2
✓ Branch 0 taken 4656 times.
✓ Branch 1 taken 2 times.
4658 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1160
2/2
✓ Branch 0 taken 4653 times.
✓ Branch 1 taken 3 times.
4656 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1161 4655 continue;
1162
1163
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 3 times.
37 for (j = 0; j < outlink->incfg.channel_layouts->nb_channel_layouts; j++) {
1164 34 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1165 int in_channels;
1166 int out_channels;
1167 int count_diff;
1168 int matched_channels, extra_channels;
1169 34 int score = 100000;
1170
1171 34 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1172 34 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1173 34 in_channels = in_chlayout.nb_channels;
1174 34 out_channels = out_chlayout.nb_channels;
1175 34 count_diff = out_channels - in_channels;
1176
4/8
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
34 if (!KNOWN(&in_chlayout) || !KNOWN(&out_chlayout)) {
1177 /* Compute score in case the input or output layout encodes
1178 a channel count; in this case the score is not altered by
1179 the computation afterwards, as in_chlayout and
1180 out_chlayout have both been set to 0 */
1181
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (!KNOWN(&in_chlayout))
1182
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1183
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1184 out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1185 32 score -= 10000 + FFABS(out_channels - in_channels) +
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1187 16 av_channel_layout_uninit(&in_chlayout);
1188 16 av_channel_layout_uninit(&out_chlayout);
1189 /* Let the remaining computation run, even if the score
1190 value is not altered */
1191 }
1192
1193 /* channel substitution */
1194
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 34 times.
748 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1195 714 uint64_t cmp0 = ch_subst[k][0];
1196 714 uint64_t cmp1 = ch_subst[k][1];
1197
4/4
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 498 times.
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 96 times.
930 if ( av_channel_layout_subset(& in_chlayout, cmp0) &&
1198
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 100 times.
336 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1199
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
140 av_channel_layout_subset(&out_chlayout, cmp1) &&
1200 20 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1201 4 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1202 4 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1203 /* add score for channel match, minus a deduction for
1204 having to do the substitution */
1205 4 score += 10 * av_popcount64(cmp1) - 2;
1206 }
1207 }
1208
1209 /* no penalty for LFE channel mismatch */
1210
4/4
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 11 times.
52 if (av_channel_layout_index_from_channel(&in_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0 &&
1211 18 av_channel_layout_index_from_channel(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1212 7 score += 10;
1213 34 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1214 34 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1215
1216 34 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1217 34 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1218 34 score += 10 * matched_channels - 5 * extra_channels;
1219
1220
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 20 times.
34 if (score > best_score ||
1221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (count_diff < best_count_diff && score == best_score)) {
1222 10 best_score = score;
1223 10 best_idx = j;
1224 10 best_count_diff = count_diff;
1225 }
1226 }
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(best_idx >= 0);
1228 3 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1229 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1230 }
1231
1232 }
1233
1234 8221 static void swap_channel_layouts(AVFilterGraph *graph)
1235 {
1236 int i;
1237
1238
2/2
✓ Branch 0 taken 45323 times.
✓ Branch 1 taken 8221 times.
53544 for (i = 0; i < graph->nb_filters; i++)
1239 45323 swap_channel_layouts_on_filter(graph->filters[i]);
1240 8221 }
1241
1242 45323 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1243 {
1244 45323 AVFilterLink *link = NULL;
1245 int format, bps;
1246 int i, j;
1247
1248
2/2
✓ Branch 0 taken 37044 times.
✓ Branch 1 taken 39751 times.
76795 for (i = 0; i < filter->nb_inputs; i++) {
1249 37044 link = filter->inputs[i];
1250
1251
2/2
✓ Branch 0 taken 6053 times.
✓ Branch 1 taken 30991 times.
37044 if (link->type == AVMEDIA_TYPE_AUDIO &&
1252
2/2
✓ Branch 0 taken 5572 times.
✓ Branch 1 taken 481 times.
6053 link->outcfg.formats->nb_formats == 1)
1253 5572 break;
1254 }
1255
2/2
✓ Branch 0 taken 39751 times.
✓ Branch 1 taken 5572 times.
45323 if (i == filter->nb_inputs)
1256 39751 return;
1257
1258 5572 format = link->outcfg.formats->formats[0];
1259 5572 bps = av_get_bytes_per_sample(format);
1260
1261
2/2
✓ Branch 0 taken 4264 times.
✓ Branch 1 taken 5572 times.
9836 for (i = 0; i < filter->nb_outputs; i++) {
1262 4264 AVFilterLink *outlink = filter->outputs[i];
1263 4264 int best_idx = -1, best_score = INT_MIN;
1264
1265
2/2
✓ Branch 0 taken 4262 times.
✓ Branch 1 taken 2 times.
4264 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1266
2/2
✓ Branch 0 taken 4099 times.
✓ Branch 1 taken 163 times.
4262 outlink->incfg.formats->nb_formats < 2)
1267 4101 continue;
1268
1269
2/2
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 7 times.
313 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1270 306 int out_format = outlink->incfg.formats->formats[j];
1271 306 int out_bps = av_get_bytes_per_sample(out_format);
1272 int score;
1273
1274
4/4
✓ Branch 1 taken 165 times.
✓ Branch 2 taken 141 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 150 times.
471 if (av_get_packed_sample_fmt(out_format) == format ||
1275 165 av_get_planar_sample_fmt(out_format) == format) {
1276 156 best_idx = j;
1277 156 break;
1278 }
1279
1280 /* for s32 and float prefer double to prevent loss of information */
1281
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 146 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
150 if (bps == 4 && out_bps == 8) {
1282 best_idx = j;
1283 break;
1284 }
1285
1286 /* prefer closest higher or equal bps */
1287 150 score = -abs(out_bps - bps);
1288
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 122 times.
150 if (out_bps >= bps)
1289 28 score += INT_MAX/2;
1290
1291
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 34 times.
150 if (score > best_score) {
1292 116 best_score = score;
1293 116 best_idx = j;
1294 }
1295 }
1296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 av_assert0(best_idx >= 0);
1297 163 FFSWAP(int, outlink->incfg.formats->formats[0],
1298 outlink->incfg.formats->formats[best_idx]);
1299 }
1300 }
1301
1302 8221 static void swap_sample_fmts(AVFilterGraph *graph)
1303 {
1304 int i;
1305
1306
2/2
✓ Branch 0 taken 45323 times.
✓ Branch 1 taken 8221 times.
53544 for (i = 0; i < graph->nb_filters; i++)
1307 45323 swap_sample_fmts_on_filter(graph->filters[i]);
1308
1309 8221 }
1310
1311 8221 static int pick_formats(AVFilterGraph *graph)
1312 {
1313 int i, j, ret;
1314 int change;
1315
1316 do{
1317 16478 change = 0;
1318
2/2
✓ Branch 0 taken 91609 times.
✓ Branch 1 taken 16478 times.
108087 for (i = 0; i < graph->nb_filters; i++) {
1319 91609 AVFilterContext *filter = graph->filters[i];
1320
2/2
✓ Branch 0 taken 74632 times.
✓ Branch 1 taken 16977 times.
91609 if (filter->nb_inputs){
1321
2/2
✓ Branch 0 taken 75015 times.
✓ Branch 1 taken 74632 times.
149647 for (j = 0; j < filter->nb_inputs; j++){
1322
4/4
✓ Branch 0 taken 15800 times.
✓ Branch 1 taken 59215 times.
✓ Branch 2 taken 15279 times.
✓ Branch 3 taken 521 times.
75015 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1323
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15279 times.
15279 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1324 return ret;
1325 15279 change = 1;
1326 }
1327 }
1328 }
1329
2/2
✓ Branch 0 taken 74743 times.
✓ Branch 1 taken 16866 times.
91609 if (filter->nb_outputs){
1330
2/2
✓ Branch 0 taken 75015 times.
✓ Branch 1 taken 74743 times.
149758 for (j = 0; j < filter->nb_outputs; j++){
1331
4/4
✓ Branch 0 taken 22107 times.
✓ Branch 1 taken 52908 times.
✓ Branch 2 taken 21475 times.
✓ Branch 3 taken 632 times.
75015 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21475 times.
21475 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1333 return ret;
1334 21475 change = 1;
1335 }
1336 }
1337 }
1338
6/6
✓ Branch 0 taken 74632 times.
✓ Branch 1 taken 16977 times.
✓ Branch 2 taken 57766 times.
✓ Branch 3 taken 16866 times.
✓ Branch 4 taken 57482 times.
✓ Branch 5 taken 284 times.
91609 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1339
2/2
✓ Branch 0 taken 57686 times.
✓ Branch 1 taken 57482 times.
115168 for (j = 0; j < filter->nb_outputs; j++) {
1340
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 57436 times.
57686 if (filter->outputs[j]->format<0) {
1341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1342 return ret;
1343 250 change = 1;
1344 }
1345 }
1346 }
1347 }
1348
2/2
✓ Branch 0 taken 8257 times.
✓ Branch 1 taken 8221 times.
16478 }while(change);
1349
1350
2/2
✓ Branch 0 taken 45323 times.
✓ Branch 1 taken 8221 times.
53544 for (i = 0; i < graph->nb_filters; i++) {
1351 45323 AVFilterContext *filter = graph->filters[i];
1352
1353
2/2
✓ Branch 0 taken 37064 times.
✓ Branch 1 taken 45323 times.
82387 for (j = 0; j < filter->nb_inputs; j++)
1354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37064 times.
37064 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1355 return ret;
1356
2/2
✓ Branch 0 taken 37064 times.
✓ Branch 1 taken 45323 times.
82387 for (j = 0; j < filter->nb_outputs; j++)
1357
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37064 times.
37064 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1358 return ret;
1359 }
1360 8221 return 0;
1361 }
1362
1363 /**
1364 * Configure the formats of all the links in the graph.
1365 */
1366 8221 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1367 {
1368 int ret;
1369
1370 /* find supported formats from sub-filters, and merge along links */
1371
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 8221 times.
8236 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1372 15 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8221 times.
8221 if (ret < 0)
1374 return ret;
1375
1376 /* Once everything is merged, it's possible that we'll still have
1377 * multiple valid media format choices. We try to minimize the amount
1378 * of format conversion inside filters */
1379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8221 times.
8221 if ((ret = reduce_formats(graph)) < 0)
1380 return ret;
1381
1382 /* for audio filters, ensure the best format, sample rate and channel layout
1383 * is selected */
1384 8221 swap_sample_fmts(graph);
1385 8221 swap_samplerates(graph);
1386 8221 swap_channel_layouts(graph);
1387
1388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8221 times.
8221 if ((ret = pick_formats(graph)) < 0)
1389 return ret;
1390
1391 8221 return 0;
1392 }
1393
1394 8220 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1395 {
1396 unsigned i, j;
1397 8220 int sink_links_count = 0, n = 0;
1398 AVFilterContext *f;
1399 FilterLinkInternal **sinks;
1400
1401
2/2
✓ Branch 0 taken 45318 times.
✓ Branch 1 taken 8220 times.
53538 for (i = 0; i < graph->nb_filters; i++) {
1402 45318 f = graph->filters[i];
1403
2/2
✓ Branch 0 taken 37060 times.
✓ Branch 1 taken 45318 times.
82378 for (j = 0; j < f->nb_inputs; j++) {
1404 37060 ff_link_internal(f->inputs[j])->age_index = -1;
1405 }
1406
2/2
✓ Branch 0 taken 37060 times.
✓ Branch 1 taken 45318 times.
82378 for (j = 0; j < f->nb_outputs; j++) {
1407 37060 ff_link_internal(f->outputs[j])->age_index = -1;
1408 }
1409
2/2
✓ Branch 0 taken 8359 times.
✓ Branch 1 taken 36959 times.
45318 if (!f->nb_outputs) {
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8359 times.
8359 if (f->nb_inputs > INT_MAX - sink_links_count)
1411 return AVERROR(EINVAL);
1412 8359 sink_links_count += f->nb_inputs;
1413 }
1414 }
1415 8220 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8220 times.
8220 if (!sinks)
1417 return AVERROR(ENOMEM);
1418
2/2
✓ Branch 0 taken 45318 times.
✓ Branch 1 taken 8220 times.
53538 for (i = 0; i < graph->nb_filters; i++) {
1419 45318 f = graph->filters[i];
1420
2/2
✓ Branch 0 taken 8359 times.
✓ Branch 1 taken 36959 times.
45318 if (!f->nb_outputs) {
1421
2/2
✓ Branch 0 taken 8359 times.
✓ Branch 1 taken 8359 times.
16718 for (j = 0; j < f->nb_inputs; j++) {
1422 8359 sinks[n] = ff_link_internal(f->inputs[j]);
1423 8359 sinks[n]->age_index = n;
1424 8359 n++;
1425 }
1426 }
1427 }
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8220 times.
8220 av_assert0(n == sink_links_count);
1429 8220 fffiltergraph(graph)->sink_links = sinks;
1430 8220 fffiltergraph(graph)->sink_links_count = sink_links_count;
1431 8220 return 0;
1432 }
1433
1434 8221 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1435 {
1436 int ret;
1437
1438
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8220 times.
8221 if (graphctx->max_buffered_frames)
1439 1 fffiltergraph(graphctx)->frame_queues.max_queued = graphctx->max_buffered_frames;
1440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8221 times.
8221 if ((ret = graph_check_validity(graphctx, log_ctx)))
1441 return ret;
1442
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8221 times.
8221 if ((ret = graph_config_formats(graphctx, log_ctx)))
1443 return ret;
1444
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8220 times.
8221 if ((ret = graph_config_links(graphctx, log_ctx)))
1445 1 return ret;
1446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8220 times.
8220 if ((ret = graph_check_links(graphctx, log_ctx)))
1447 return ret;
1448
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8220 times.
8220 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1449 return ret;
1450
1451 8220 return 0;
1452 }
1453
1454 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1455 {
1456 int i, r = AVERROR(ENOSYS);
1457
1458 if (!graph)
1459 return r;
1460
1461 if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1462 r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1463 if (r != AVERROR(ENOSYS))
1464 return r;
1465 }
1466
1467 if (res_len && res)
1468 res[0] = 0;
1469
1470 for (i = 0; i < graph->nb_filters; i++) {
1471 AVFilterContext *filter = graph->filters[i];
1472 if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1473 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1474 if (r != AVERROR(ENOSYS)) {
1475 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1476 return r;
1477 }
1478 }
1479 }
1480
1481 return r;
1482 }
1483
1484 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1485 {
1486 int i;
1487
1488 if(!graph)
1489 return 0;
1490
1491 for (i = 0; i < graph->nb_filters; i++) {
1492 AVFilterContext *filter = graph->filters[i];
1493 FFFilterContext *ctxi = fffilterctx(filter);
1494 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1495 AVFilterCommand **queue = &ctxi->command_queue, *next;
1496 while (*queue && (*queue)->time <= ts)
1497 queue = &(*queue)->next;
1498 next = *queue;
1499 *queue = av_mallocz(sizeof(AVFilterCommand));
1500 if (!*queue)
1501 return AVERROR(ENOMEM);
1502
1503 (*queue)->command = av_strdup(command);
1504 (*queue)->arg = av_strdup(arg);
1505 (*queue)->time = ts;
1506 (*queue)->flags = flags;
1507 (*queue)->next = next;
1508 if(flags & AVFILTER_CMD_FLAG_ONE)
1509 return 0;
1510 }
1511 }
1512
1513 return 0;
1514 }
1515
1516 458964 static void heap_bubble_up(FFFilterGraph *graph,
1517 FilterLinkInternal *li, int index)
1518 {
1519 458964 FilterLinkInternal **links = graph->sink_links;
1520
1521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 458964 times.
458964 av_assert0(index >= 0);
1522
1523
2/2
✓ Branch 0 taken 1858 times.
✓ Branch 1 taken 458964 times.
460822 while (index) {
1524 1858 int parent = (index - 1) >> 1;
1525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1858 times.
1858 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1526 break;
1527 1858 links[index] = links[parent];
1528 1858 links[index]->age_index = index;
1529 1858 index = parent;
1530 }
1531 458964 links[index] = li;
1532 458964 li->age_index = index;
1533 458964 }
1534
1535 459091 static void heap_bubble_down(FFFilterGraph *graph,
1536 FilterLinkInternal *li, int index)
1537 {
1538 459091 FilterLinkInternal **links = graph->sink_links;
1539
1540
1/2
✓ Branch 0 taken 459091 times.
✗ Branch 1 not taken.
459091 av_assert0(index >= 0);
1541
1542 5284 while (1) {
1543 464375 int child = 2 * index + 1;
1544
2/2
✓ Branch 0 taken 458756 times.
✓ Branch 1 taken 5619 times.
464375 if (child >= graph->sink_links_count)
1545 458756 break;
1546
2/2
✓ Branch 0 taken 4185 times.
✓ Branch 1 taken 1434 times.
5619 if (child + 1 < graph->sink_links_count &&
1547
2/2
✓ Branch 0 taken 1285 times.
✓ Branch 1 taken 2900 times.
4185 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1548 1285 child++;
1549
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 5284 times.
5619 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1550 335 break;
1551 5284 links[index] = links[child];
1552 5284 links[index]->age_index = index;
1553 5284 index = child;
1554 }
1555 459091 links[index] = li;
1556 459091 li->age_index = index;
1557 459091 }
1558
1559 458964 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1560 {
1561 458964 FFFilterGraph *graphi = fffiltergraph(graph);
1562
1563 458964 heap_bubble_up (graphi, li, li->age_index);
1564 458964 heap_bubble_down(graphi, li, li->age_index);
1565 458964 }
1566
1567 447980 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1568 {
1569 447980 FFFilterGraph *graphi = fffiltergraph(graph);
1570 447980 FilterLinkInternal *oldesti = graphi->sink_links[0];
1571 447980 AVFilterLink *oldest = &oldesti->l.pub;
1572 int64_t frame_count;
1573 int r;
1574
1575
2/2
✓ Branch 0 taken 448113 times.
✓ Branch 1 taken 5045 times.
453158 while (graphi->sink_links_count) {
1576 448113 oldesti = graphi->sink_links[0];
1577 448113 oldest = &oldesti->l.pub;
1578
2/2
✓ Branch 1 taken 448089 times.
✓ Branch 2 taken 24 times.
448113 if (fffilter(oldest->dst->filter)->activate) {
1579 448089 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1580 AV_BUFFERSINK_FLAG_PEEK);
1581
2/2
✓ Branch 0 taken 442912 times.
✓ Branch 1 taken 5177 times.
448089 if (r != AVERROR_EOF)
1582 442912 return r;
1583 } else {
1584 24 r = ff_request_frame(oldest);
1585 }
1586
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 5178 times.
5201 if (r != AVERROR_EOF)
1587 23 break;
1588 5178 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1589 5178 oldest->dst->name,
1590 5178 oldest->dstpad->name);
1591 /* EOF: remove the link from the heap */
1592
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 5051 times.
5178 if (oldesti->age_index < --graphi->sink_links_count)
1593 127 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1594 oldesti->age_index);
1595 5178 oldesti->age_index = -1;
1596 }
1597
2/2
✓ Branch 0 taken 5045 times.
✓ Branch 1 taken 23 times.
5068 if (!graphi->sink_links_count)
1598 5045 return AVERROR_EOF;
1599 av_assert1(!fffilter(oldest->dst->filter)->activate);
1600 av_assert1(oldesti->age_index >= 0);
1601 23 frame_count = oldesti->l.frame_count_out;
1602
1/2
✓ Branch 0 taken 92 times.
✗ Branch 1 not taken.
92 while (frame_count == oldesti->l.frame_count_out) {
1603 92 r = ff_filter_graph_run_once(graph);
1604
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 69 times.
92 if (r == FFERROR_BUFFERSRC_EMPTY)
1605 23 r = 0;
1606
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 69 times.
92 if (r == AVERROR(EAGAIN) &&
1607
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
23 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1608 !oldesti->status_in)
1609 (void)ff_request_frame(oldest);
1610
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 69 times.
92 else if (r < 0)
1611 23 return r;
1612 }
1613 return 0;
1614 }
1615
1616 5038995 int ff_filter_graph_run_once(AVFilterGraph *graph)
1617 {
1618 FFFilterContext *ctxi;
1619 unsigned i;
1620
1621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5038995 times.
5038995 av_assert0(graph->nb_filters);
1622 5038995 ctxi = fffilterctx(graph->filters[0]);
1623
2/2
✓ Branch 0 taken 21168347 times.
✓ Branch 1 taken 5038995 times.
26207342 for (i = 1; i < graph->nb_filters; i++) {
1624 21168347 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1625
1626
2/2
✓ Branch 0 taken 3013572 times.
✓ Branch 1 taken 18154775 times.
21168347 if (ctxi_other->ready > ctxi->ready)
1627 3013572 ctxi = ctxi_other;
1628 }
1629
1630
2/2
✓ Branch 0 taken 833658 times.
✓ Branch 1 taken 4205337 times.
5038995 if (!ctxi->ready)
1631 833658 return AVERROR(EAGAIN);
1632 4205337 return ff_filter_activate(&ctxi->p);
1633 }
1634