FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2026-10-04 16:07:23
Exec Total Coverage
Lines: 680 899 75.6%
Functions: 37 41 90.2%
Branches: 597 867 68.9%

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 16796 AVFilterGraph *avfilter_graph_alloc(void)
86 {
87 16796 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
88 AVFilterGraph *ret;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16796 times.
16796 if (!graph)
91 ✗ return NULL;
92
93 16796 ret = &graph->p;
94 16796 ret->av_class = &filtergraph_class;
95 16796 av_opt_set_defaults(ret);
96 16796 ff_framequeue_global_init(&graph->frame_queues);
97
98 16796 return ret;
99 }
100
101 65663 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
102 {
103 int i, j;
104
1/2
✓ Branch 0 taken 65663 times.
✗ Branch 1 not taken.
65663 for (i = 0; i < graph->nb_filters; i++) {
105
1/2
✓ Branch 0 taken 65663 times.
✗ Branch 1 not taken.
65663 if (graph->filters[i] == filter) {
106 65663 FFSWAP(AVFilterContext*, graph->filters[i],
107 graph->filters[graph->nb_filters - 1]);
108 65663 graph->nb_filters--;
109 65663 filter->graph = NULL;
110
2/2
✓ Branch 0 taken 57411 times.
✓ Branch 1 taken 65663 times.
123074 for (j = 0; j<filter->nb_outputs; j++)
111
2/2
✓ Branch 0 taken 21861 times.
✓ Branch 1 taken 35550 times.
57411 if (filter->outputs[j])
112 21861 ff_filter_link(filter->outputs[j])->graph = NULL;
113
114 65663 return;
115 }
116 }
117 }
118
119 25097 void avfilter_graph_free(AVFilterGraph **graphp)
120 {
121 25097 AVFilterGraph *graph = *graphp;
122 25097 FFFilterGraph *graphi = fffiltergraph(graph);
123
124
2/2
✓ Branch 0 taken 8301 times.
✓ Branch 1 taken 16796 times.
25097 if (!graph)
125 8301 return;
126
127
2/2
✓ Branch 0 taken 65597 times.
✓ Branch 1 taken 16796 times.
82393 while (graph->nb_filters)
128 65597 avfilter_free(graph->filters[0]);
129
130 16796 ff_graph_thread_free(graphi);
131
132 16796 av_freep(&graphi->sink_links);
133
134 16796 av_opt_free(graph);
135
136 16796 av_freep(&graph->filters);
137 16796 av_freep(graphp);
138 }
139
140 20390 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 20390 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20390 times.
20390 if (!*filt_ctx)
148 ✗ return AVERROR(ENOMEM);
149
150 20390 ret = avfilter_init_str(*filt_ctx, args);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20390 times.
20390 if (ret < 0)
152 ✗ goto fail;
153
154 20390 return 0;
155
156 ✗ fail:
157 ✗ avfilter_free(*filt_ctx);
158 ✗ *filt_ctx = NULL;
159 ✗ return ret;
160 }
161
162 6438 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
163 {
164 6438 fffiltergraph(graph)->disable_auto_convert = flags;
165 6438 }
166
167 65663 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
168 const AVFilter *filter,
169 const char *name)
170 {
171 AVFilterContext **filters, *s;
172 65663 FFFilterGraph *graphi = fffiltergraph(graph);
173
174
4/4
✓ Branch 0 taken 35113 times.
✓ Branch 1 taken 30550 times.
✓ Branch 2 taken 16796 times.
✓ Branch 3 taken 18317 times.
65663 if (graph->thread_type && !graphi->thread_execute) {
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16796 times.
16796 if (graph->execute) {
176 ✗ graphi->thread_execute = graph->execute;
177 } else {
178 16796 int ret = ff_graph_thread_init(graphi);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16796 times.
16796 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 65663 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65663 times.
65663 if (!filters)
188 ✗ return NULL;
189 65663 graph->filters = filters;
190
191 65663 s = ff_filter_alloc(filter, name);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65663 times.
65663 if (!s)
193 ✗ return NULL;
194
195 65663 graph->filters[graph->nb_filters++] = s;
196
197 65663 s->graph = graph;
198
199 65663 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 8425 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 45144 times.
✓ Branch 1 taken 8425 times.
53569 for (i = 0; i < graph->nb_filters; i++) {
216 const AVFilterPad *pad;
217 45144 filt = graph->filters[i];
218
219
2/2
✓ Branch 0 taken 36687 times.
✓ Branch 1 taken 45144 times.
81831 for (j = 0; j < filt->nb_inputs; j++) {
220
2/4
✓ Branch 0 taken 36687 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36687 times.
36687 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 36687 times.
✓ Branch 1 taken 45144 times.
81831 for (j = 0; j < filt->nb_outputs; j++) {
230
2/4
✓ Branch 0 taken 36687 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36687 times.
36687 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 8425 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 8425 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 46719 times.
✓ Branch 1 taken 8424 times.
55143 for (i = 0; i < graph->nb_filters; i++) {
254 46719 filt = graph->filters[i];
255
256
2/2
✓ Branch 0 taken 8664 times.
✓ Branch 1 taken 38055 times.
46719 if (!filt->nb_outputs) {
257
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8663 times.
8664 if ((ret = ff_filter_config_links(filt)))
258 1 return ret;
259 }
260 }
261
262 8424 return 0;
263 }
264
265 8424 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 46714 times.
✓ Branch 1 taken 8424 times.
55138 for (i = 0; i < graph->nb_filters; i++) {
273 46714 f = graph->filters[i];
274
2/2
✓ Branch 0 taken 38258 times.
✓ Branch 1 taken 46714 times.
84972 for (j = 0; j < f->nb_outputs; j++) {
275 38258 l = f->outputs[j];
276
2/2
✓ Branch 0 taken 31518 times.
✓ Branch 1 taken 6740 times.
38258 if (l->type == AVMEDIA_TYPE_VIDEO) {
277 31518 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 31518 times.
31518 if (ret < 0)
279 ✗ return ret;
280 }
281 }
282 }
283 8424 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 62340 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
298 {
299 int ret;
300
301
2/3
✓ Branch 0 taken 51684 times.
✓ Branch 1 taken 10656 times.
✗ Branch 2 not taken.
62340 switch (link->type) {
302
303 51684 case AVMEDIA_TYPE_VIDEO:
304
2/4
✓ Branch 1 taken 51684 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 51684 times.
✗ Branch 4 not taken.
103368 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
305
1/2
✓ Branch 1 taken 51684 times.
✗ Branch 2 not taken.
103368 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
306
1/2
✓ Branch 1 taken 51684 times.
✗ Branch 2 not taken.
103368 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0 ||
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51684 times.
103368 (ret = ff_formats_check_alpha_modes(log, cfg->alpha_modes)) < 0 ||
308 51684 (ret = ff_formats_check_chroma_locations(log, cfg->chroma_locations)) < 0)
309 ✗ return ret;
310 51684 break;
311
312 10656 case AVMEDIA_TYPE_AUDIO:
313
2/4
✓ Branch 1 taken 10656 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10656 times.
✗ Branch 4 not taken.
21312 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10656 times.
21312 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
315 10656 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
316 ✗ return ret;
317 10656 break;
318
319 ✗ default:
320 ✗ av_assert0(!"reached");
321 }
322 62340 return 0;
323 }
324
325 /**
326 * Check the validity of the formats / etc. lists set by query_formats().
327 *
328 * In particular, check they do not contain any redundant element.
329 */
330 39135 static int filter_check_formats(AVFilterContext *ctx)
331 {
332 unsigned i;
333 int ret;
334
335
2/2
✓ Branch 0 taken 31795 times.
✓ Branch 1 taken 39135 times.
70930 for (i = 0; i < ctx->nb_inputs; i++) {
336 31795 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31795 times.
31795 if (ret < 0)
338 ✗ return ret;
339 }
340
2/2
✓ Branch 0 taken 30545 times.
✓ Branch 1 taken 39135 times.
69680 for (i = 0; i < ctx->nb_outputs; i++) {
341 30545 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30545 times.
30545 if (ret < 0)
343 ✗ return ret;
344 }
345 39135 return 0;
346 }
347
348 46736 static int filter_query_formats(AVFilterContext *ctx)
349 {
350 46736 const FFFilter *const filter = fffilter(ctx->filter);
351 int ret;
352
353
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 46701 times.
46736 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
354
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 18 times.
35 if ((ret = filter->formats.query_func(ctx)) < 0) {
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret != AVERROR(EAGAIN))
356 ✗ av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
357 ✗ ctx->name, av_err2str(ret));
358 17 return ret;
359 }
360
2/2
✓ Branch 0 taken 39117 times.
✓ Branch 1 taken 7584 times.
46701 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
361 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
362 39117 AVFilterFormatsConfig **cfg_in_dyn = NULL, **cfg_out_dyn = NULL;
363 AVFilterFormatsConfig **cfg_in, **cfg_out;
364
365
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 39116 times.
39117 if (ctx->nb_inputs > FF_ARRAY_ELEMS(cfg_in_stack)) {
366 1 cfg_in_dyn = av_malloc_array(ctx->nb_inputs, sizeof(*cfg_in_dyn));
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_in_dyn)
368 ✗ return AVERROR(ENOMEM);
369 1 cfg_in = cfg_in_dyn;
370 } else
371
2/2
✓ Branch 0 taken 31637 times.
✓ Branch 1 taken 7479 times.
39116 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
372
373
2/2
✓ Branch 0 taken 31771 times.
✓ Branch 1 taken 39117 times.
70888 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
374 31771 AVFilterLink *l = ctx->inputs[i];
375 31771 cfg_in[i] = &l->outcfg;
376 }
377
378
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 39116 times.
39117 if (ctx->nb_outputs > FF_ARRAY_ELEMS(cfg_out_stack)) {
379 1 cfg_out_dyn = av_malloc_array(ctx->nb_outputs, sizeof(*cfg_out_dyn));
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_out_dyn) {
381 ✗ av_freep(&cfg_in_dyn);
382 ✗ return AVERROR(ENOMEM);
383 }
384 1 cfg_out = cfg_out_dyn;
385 } else
386
2/2
✓ Branch 0 taken 30453 times.
✓ Branch 1 taken 8663 times.
39116 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
387
388
2/2
✓ Branch 0 taken 30527 times.
✓ Branch 1 taken 39117 times.
69644 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
389 30527 AVFilterLink *l = ctx->outputs[i];
390 30527 cfg_out[i] = &l->incfg;
391 }
392
393 39117 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
394 39117 av_freep(&cfg_in_dyn);
395 39117 av_freep(&cfg_out_dyn);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39117 times.
39117 if (ret < 0) {
397 ✗ if (ret != AVERROR(EAGAIN))
398 ✗ av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
399 ✗ ctx->name, av_err2str(ret));
400 ✗ return ret;
401 }
402 }
403
404
2/2
✓ Branch 0 taken 46701 times.
✓ Branch 1 taken 18 times.
46719 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
405
2/2
✓ Branch 0 taken 39117 times.
✓ Branch 1 taken 7584 times.
46701 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
406 39135 ret = filter_check_formats(ctx);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39135 times.
39135 if (ret < 0)
408 ✗ return ret;
409 }
410
411 46719 return ff_default_query_formats(ctx);
412 }
413
414 45218 static int formats_declared(AVFilterContext *f)
415 {
416 int i;
417
418
2/2
✓ Branch 0 taken 36572 times.
✓ Branch 1 taken 8686 times.
45258 for (i = 0; i < f->nb_inputs; i++) {
419
2/2
✓ Branch 0 taken 36530 times.
✓ Branch 1 taken 42 times.
36572 if (!f->inputs[i]->outcfg.formats)
420 36530 return 0;
421
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9 times.
42 if (f->inputs[i]->type == AVMEDIA_TYPE_VIDEO &&
422
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 !(f->inputs[i]->outcfg.color_ranges &&
423
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->inputs[i]->outcfg.color_spaces &&
424
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->inputs[i]->outcfg.alpha_modes &&
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->inputs[i]->outcfg.chroma_locations))
426 2 return 0;
427
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 31 times.
40 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
428
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 !(f->inputs[i]->outcfg.samplerates &&
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 f->inputs[i]->outcfg.channel_layouts))
430 ✗ return 0;
431 }
432
2/2
✓ Branch 0 taken 8675 times.
✓ Branch 1 taken 57 times.
8732 for (i = 0; i < f->nb_outputs; i++) {
433
2/2
✓ Branch 0 taken 8629 times.
✓ Branch 1 taken 46 times.
8675 if (!f->outputs[i]->incfg.formats)
434 8629 return 0;
435
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 15 times.
46 if (f->outputs[i]->type == AVMEDIA_TYPE_VIDEO &&
436
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !(f->outputs[i]->incfg.color_ranges &&
437
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->outputs[i]->incfg.color_spaces &&
438
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->outputs[i]->incfg.alpha_modes &&
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->outputs[i]->incfg.chroma_locations))
440 ✗ return 0;
441
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 31 times.
46 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
442
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 !(f->outputs[i]->incfg.samplerates &&
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 f->outputs[i]->incfg.channel_layouts))
444 ✗ return 0;
445 }
446 57 return 1;
447 }
448
449 ✗ static void print_link_formats(void *log_ctx, int level, const AVFilterLink *l,
450 const AVFilterFormatsMerger *mergers[],
451 int nb_mergers)
452 {
453 ✗ if (av_log_get_level() < level)
454 ✗ return;
455
456 AVBPrint bp;
457 ✗ av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
458
459 ✗ av_log(log_ctx, level, "Link '%s.%s' -> '%s.%s':\n",
460 ✗ l->src->name, l->srcpad->name, l->dst->name, l->dstpad->name);
461
462 ✗ for (unsigned i = 0; i < nb_mergers; i++) {
463 ✗ const AVFilterFormatsMerger *m = mergers[i];
464 ✗ av_log(log_ctx, level, " %s:\n", m->name);
465 ✗ m->print_list(&bp, FF_FIELD_AT(void *, m->offset, l->incfg));
466 ✗ if (av_bprint_is_complete(&bp))
467 ✗ av_log(log_ctx, level, " src: %s\n", bp.str);
468 ✗ av_bprint_clear(&bp);
469
470 ✗ m->print_list(&bp, FF_FIELD_AT(void *, m->offset, l->outcfg));
471 ✗ if (av_bprint_is_complete(&bp))
472 ✗ av_log(log_ctx, level, " dst: %s\n", bp.str);
473 ✗ av_bprint_clear(&bp);
474 }
475
476 ✗ av_bprint_finalize(&bp, NULL);
477 }
478
479 45144 static void print_filter_formats(void *log_ctx, int level, const AVFilterContext *f)
480 {
481
1/2
✓ Branch 1 taken 45144 times.
✗ Branch 2 not taken.
45144 if (av_log_get_level() < level)
482 45144 return;
483
484 AVBPrint bp;
485 ✗ av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
486
487 ✗ av_log(log_ctx, level, "Filter '%s' formats:\n", f->name);
488 ✗ for (int i = 0; i < f->nb_inputs; i++) {
489 ✗ const AVFilterLink *in = f->inputs[i];
490 ✗ const AVFilterNegotiation *neg = ff_filter_get_negotiation(in);
491 ✗ av_log(log_ctx, level, " in[%d] '%s':\n", i, f->input_pads[i].name);
492
493 ✗ for (unsigned i = 0; i < neg->nb_mergers; i++) {
494 ✗ const AVFilterFormatsMerger *m = &neg->mergers[i];
495 ✗ m->print_list(&bp, FF_FIELD_AT(void *, m->offset, in->outcfg));
496 ✗ if (av_bprint_is_complete(&bp))
497 ✗ av_log(log_ctx, level, " %s: %s\n", m->name, bp.str);
498 ✗ av_bprint_clear(&bp);
499 }
500 }
501
502 ✗ for (int i = 0; i < f->nb_outputs; i++) {
503 ✗ const AVFilterLink *out = f->outputs[i];
504 ✗ const AVFilterNegotiation *neg = ff_filter_get_negotiation(out);
505 ✗ av_log(log_ctx, level, " out[%d] '%s':\n", i, f->output_pads[i].name);
506
507 ✗ for (unsigned i = 0; i < neg->nb_mergers; i++) {
508 ✗ const AVFilterFormatsMerger *m = &neg->mergers[i];
509 ✗ m->print_list(&bp, FF_FIELD_AT(void *, m->offset, out->incfg));
510 ✗ if (av_bprint_is_complete(&bp))
511 ✗ av_log(log_ctx, level, " %s: %s\n", m->name, bp.str);
512 ✗ av_bprint_clear(&bp);
513 }
514 }
515
516 ✗ av_bprint_finalize(&bp, NULL);
517 }
518
519 /**
520 * Perform one round of query_formats() and merging formats lists on the
521 * filter graph.
522 * @return >=0 if all links formats lists could be queried and merged;
523 * AVERROR(EAGAIN) some progress was made in the queries or merging
524 * and a later call may succeed;
525 * AVERROR(EIO) (may be changed) plus a log message if no progress
526 * was made and the negotiation is stuck;
527 * a negative error code if some other error happened
528 */
529 8440 static int query_formats(AVFilterGraph *graph, void *log_ctx)
530 {
531 int i, j, k, ret;
532 8440 int converter_count = 0;
533 8440 int count_queried = 0; /* successful calls to query_formats() */
534 8440 int count_merged = 0; /* successful merge of formats lists */
535 8440 int count_already_merged = 0; /* lists already merged */
536 8440 int count_delayed = 0; /* lists that need to be merged later */
537
538
2/2
✓ Branch 0 taken 45218 times.
✓ Branch 1 taken 8440 times.
53658 for (i = 0; i < graph->nb_filters; i++) {
539 45218 AVFilterContext *f = graph->filters[i];
540
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 45161 times.
45218 if (formats_declared(f))
541 57 continue;
542 45161 ret = filter_query_formats(f);
543
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 45144 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
45161 if (ret < 0 && ret != AVERROR(EAGAIN))
544 ✗ return ret;
545 /* note: EAGAIN could indicate a partial success, not counted yet */
546
2/2
✓ Branch 0 taken 45144 times.
✓ Branch 1 taken 17 times.
45161 if (ret >= 0) {
547 45144 print_filter_formats(log_ctx, AV_LOG_DEBUG, f);
548 45144 count_queried++;
549 }
550 }
551
552 /* go through and merge as many format lists as possible */
553 8440 retry:
554
2/2
✓ Branch 0 taken 76898 times.
✓ Branch 1 taken 8440 times.
85338 for (i = 0; i < graph->nb_filters; i++) {
555 76898 AVFilterContext *filter = graph->filters[i];
556
557
2/2
✓ Branch 0 taken 64539 times.
✓ Branch 1 taken 75323 times.
139862 for (j = 0; j < filter->nb_inputs; j++) {
558 64539 AVFilterLink *link = filter->inputs[j];
559 const AVFilterNegotiation *neg;
560 AVFilterContext *conv[4];
561 const AVFilterFormatsMerger *mergers[4]; /* triggered mergers */
562 64539 const char *conv_filters[4], *conv_opts[4] = {0};
563 64539 unsigned neg_step, num_conv = 0, num_mergers = 0;
564
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64539 times.
64539 if (!link)
566 ✗ continue;
567
568 64539 neg = ff_filter_get_negotiation(link);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64539 times.
64539 av_assert0(neg);
570
2/2
✓ Branch 0 taken 261385 times.
✓ Branch 1 taken 64539 times.
325924 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
571 261385 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
572 261385 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
573 261385 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
574
8/8
✓ Branch 0 taken 261308 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 261221 times.
✓ Branch 3 taken 87 times.
✓ Branch 4 taken 173030 times.
✓ Branch 5 taken 88191 times.
✓ Branch 7 taken 1662 times.
✓ Branch 8 taken 171368 times.
261385 if (a && b && a != b && !m->can_merge(a, b)) {
575
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 1575 times.
1662 for (k = 0; k < num_conv; k++) {
576
1/2
✓ Branch 0 taken 87 times.
✗ Branch 1 not taken.
87 if (!strcmp(conv_filters[k], m->conversion_filter))
577 87 break;
578 }
579
2/2
✓ Branch 0 taken 1575 times.
✓ Branch 1 taken 87 times.
1662 if (k == num_conv) {
580 av_assert1(num_conv < FF_ARRAY_ELEMS(conv_filters));
581 1575 conv_filters[num_conv] = m->conversion_filter;
582
2/2
✓ Branch 0 taken 1574 times.
✓ Branch 1 taken 1 times.
1575 if (m->conversion_opts_offset)
583 1574 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
584 1575 num_conv++;
585 }
586 av_assert1(num_mergers < FF_ARRAY_ELEMS(mergers));
587 1662 mergers[num_mergers++] = m;
588 }
589 }
590
2/2
✓ Branch 0 taken 261385 times.
✓ Branch 1 taken 64539 times.
325924 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
591 261385 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
592 261385 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
593 261385 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
594
4/4
✓ Branch 0 taken 261308 times.
✓ Branch 1 taken 77 times.
✓ Branch 2 taken 87 times.
✓ Branch 3 taken 261221 times.
261385 if (!(a && b)) {
595 164 count_delayed++;
596
2/2
✓ Branch 0 taken 93869 times.
✓ Branch 1 taken 167352 times.
261221 } else if (a == b) {
597 93869 count_already_merged++;
598
2/2
✓ Branch 0 taken 161751 times.
✓ Branch 1 taken 5601 times.
167352 } else if (!num_conv) {
599 161751 count_merged++;
600 161751 ret = m->merge(a, b);
601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161751 times.
161751 if (ret < 0)
602 ✗ return ret;
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 161751 times.
161751 if (!ret) {
604 ✗ mergers[num_mergers++] = m;
605 ✗ conv_filters[num_conv] = m->conversion_filter;
606 ✗ if (m->conversion_opts_offset)
607 ✗ conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
608 ✗ num_conv++;
609 }
610 }
611 }
612
613 /**
614 * Couldn't merge format lists; auto-insert conversion filters
615 * in reverse order to keep the order consistent with the list
616 * of mergers, since they are prepended onto the existing link
617 */
618
2/2
✓ Branch 0 taken 1575 times.
✓ Branch 1 taken 64539 times.
66114 for (k = num_conv - 1; k >= 0; k--) {
619 const AVFilter *filter;
620 char inst_name[30];
621
622
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1575 times.
1575 if (fffiltergraph(graph)->disable_auto_convert) {
623 ✗ av_log(log_ctx, AV_LOG_ERROR,
624 "The filters '%s' and '%s' do not have a common format "
625 "and automatic conversion is disabled.\n",
626 ✗ link->src->name, link->dst->name);
627 ✗ print_link_formats(log_ctx, AV_LOG_ERROR, link, mergers, num_mergers);
628 ✗ return AVERROR(EINVAL);
629 }
630
631
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1575 times.
1575 if (!(filter = avfilter_get_by_name(conv_filters[k]))) {
632 ✗ av_log(log_ctx, AV_LOG_ERROR,
633 "'%s' filter not present, cannot convert formats.\n",
634 conv_filters[k]);
635 ✗ print_link_formats(log_ctx, AV_LOG_ERROR, link, mergers, num_mergers);
636 ✗ return AVERROR(EINVAL);
637 }
638 1575 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
639 conv_filters[k], converter_count++);
640 1575 ret = avfilter_graph_create_filter(&conv[k], filter, inst_name,
641 conv_opts[k], NULL, graph);
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1575 times.
1575 if (ret < 0)
643 ✗ return ret;
644
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1575 times.
1575 if ((ret = avfilter_insert_filter(link, conv[k], 0, 0)) < 0)
645 ✗ return ret;
646
647
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1575 times.
1575 if ((ret = filter_query_formats(conv[k])) < 0)
648 ✗ return ret;
649 }
650
651 /* preemptively settle formats of auto filters */
652
2/2
✓ Branch 0 taken 1575 times.
✓ Branch 1 taken 64539 times.
66114 for (k = 0; k < num_conv; k++) {
653 1575 AVFilterLink *inlink = conv[k]->inputs[0];
654 1575 AVFilterLink *outlink = conv[k]->outputs[0];
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1575 times.
1575 av_assert0( inlink->incfg.formats->refcount > 0);
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1575 times.
1575 av_assert0( inlink->outcfg.formats->refcount > 0);
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1575 times.
1575 av_assert0(outlink->incfg.formats->refcount > 0);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1575 times.
1575 av_assert0(outlink->outcfg.formats->refcount > 0);
659
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 810 times.
1575 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink-> incfg.alpha_modes->refcount > 0);
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink->outcfg.alpha_modes->refcount > 0);
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink-> incfg.alpha_modes->refcount > 0);
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink->outcfg.alpha_modes->refcount > 0);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink-> incfg.chroma_locations->refcount > 0);
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0( inlink->outcfg.chroma_locations->refcount > 0);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink-> incfg.chroma_locations->refcount > 0);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 765 times.
765 av_assert0(outlink->outcfg.chroma_locations->refcount > 0);
676
1/2
✓ Branch 0 taken 810 times.
✗ Branch 1 not taken.
810 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0( inlink-> incfg.samplerates->refcount > 0);
678
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0( inlink->outcfg.samplerates->refcount > 0);
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0(outlink-> incfg.samplerates->refcount > 0);
680
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0(outlink->outcfg.samplerates->refcount > 0);
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
684
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 810 times.
810 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
685 }
686
687 #define MERGE(merger, link) \
688 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
689 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
690
691
2/2
✓ Branch 0 taken 6255 times.
✓ Branch 1 taken 1575 times.
7830 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
692 6255 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
693
2/2
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 5487 times.
6255 if (strcmp(m->conversion_filter, conv_filters[k]))
694 768 continue;
695
2/4
✓ Branch 1 taken 5487 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 5487 times.
10974 if ((ret = MERGE(m, inlink)) <= 0 ||
696 5487 (ret = MERGE(m, outlink)) <= 0) {
697 ✗ if (ret < 0)
698 ✗ return ret;
699 ✗ av_log(log_ctx, AV_LOG_ERROR,
700 "Impossible to convert between the formats supported by the filter "
701 ✗ "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
702 ✗ print_link_formats(log_ctx, AV_LOG_ERROR, inlink, &m, 1);
703 ✗ print_link_formats(log_ctx, AV_LOG_ERROR, outlink, &m, 1);
704 ✗ return AVERROR(ENOSYS);
705 } else {
706 5487 count_merged += 2;
707 }
708 }
709 }
710
711 /* if there is an auto filter, we may need another round to fully
712 * settle formats due to possible cross-incompatibilities between
713 * the auto filters themselves, or between the auto filters and
714 * a different attribute of the filter they are modifying */
715
2/2
✓ Branch 0 taken 1575 times.
✓ Branch 1 taken 62964 times.
64539 if (num_conv)
716 1575 goto retry;
717 }
718 }
719
720 8440 av_log(graph, AV_LOG_DEBUG, "query_formats: "
721 "%d queried, %d merged, %d already done, %d delayed\n",
722 count_queried, count_merged, count_already_merged, count_delayed);
723
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 8425 times.
8440 if (count_delayed) {
724 AVBPrint bp;
725
726 /* if count_queried > 0, one filter at least did set its formats,
727 that will give additional information to its neighbour;
728 if count_merged > 0, one pair of formats lists at least was merged,
729 that will give additional information to all connected filters;
730 in both cases, progress was made and a new round must be done */
731
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)
732 15 return AVERROR(EAGAIN);
733 ✗ av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
734 ✗ for (i = 0; i < graph->nb_filters; i++)
735 ✗ if (!formats_declared(graph->filters[i]))
736 ✗ av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
737 ✗ graph->filters[i]->name);
738 ✗ av_log(graph, AV_LOG_ERROR,
739 "The following filters could not choose their formats: %s\n"
740 "Consider inserting the (a)format filter near their input or "
741 "output.\n", bp.str);
742 ✗ return AVERROR(EIO);
743 }
744 8425 return 0;
745 }
746
747 1242 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
748 {
749 1242 int score = 0;
750
751
2/2
✓ Branch 2 taken 1142 times.
✓ Branch 3 taken 100 times.
1242 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
752 1142 score ++;
753
754
2/2
✓ Branch 2 taken 289 times.
✓ Branch 3 taken 953 times.
1242 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
755 289 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
756 }else
757 953 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
758
759
4/4
✓ Branch 1 taken 153 times.
✓ Branch 2 taken 1089 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 151 times.
1395 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
760 153 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
761 2 score += 20;
762
763
3/4
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 1137 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 105 times.
1347 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
764 105 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
765 ✗ score += 2;
766
767 1242 return score;
768 }
769
770 621 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
771 enum AVSampleFormat src_fmt)
772 {
773 int score1, score2;
774
775 621 score1 = get_fmt_score(dst_fmt1, src_fmt);
776 621 score2 = get_fmt_score(dst_fmt2, src_fmt);
777
778
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 154 times.
621 return score1 < score2 ? dst_fmt1 : dst_fmt2;
779 }
780
781 37336 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
782 {
783 37336 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
784
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37336 times.
37336 if (!desc)
785 ✗ return 0;
786
2/2
✓ Branch 0 taken 1825 times.
✓ Branch 1 taken 35511 times.
37336 if (desc->nb_components < 3)
787 1825 return 0; /* Grayscale is explicitly full-range in swscale */
788 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
789 35511 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
790 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
791 }
792
793
794 26681 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
795 {
796
2/2
✓ Branch 0 taken 758 times.
✓ Branch 1 taken 25923 times.
26681 switch (fmt) {
797 758 case AV_PIX_FMT_YUVJ420P:
798 case AV_PIX_FMT_YUVJ422P:
799 case AV_PIX_FMT_YUVJ444P:
800 case AV_PIX_FMT_YUVJ440P:
801 case AV_PIX_FMT_YUVJ411P:
802 758 return 1;
803 25923 default:
804 25923 return 0;
805 }
806 }
807
808 114691 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
809 {
810
3/4
✓ Branch 0 taken 114691 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76429 times.
✓ Branch 3 taken 38262 times.
114691 if (!link || !link->incfg.formats)
811 76429 return 0;
812
813
2/2
✓ Branch 0 taken 31522 times.
✓ Branch 1 taken 6740 times.
38262 if (link->type == AVMEDIA_TYPE_VIDEO) {
814
3/4
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 31407 times.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
31522 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
815 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
816 115 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
817 115 enum AVPixelFormat best= AV_PIX_FMT_NONE;
818 int i;
819
2/2
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 115 times.
1234 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
820 1119 enum AVPixelFormat p = link->incfg.formats->formats[i];
821 1119 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
822 }
823 115 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
824 115 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
825 115 av_get_pix_fmt_name(ref->format), has_alpha);
826 115 link->incfg.formats->formats[0] = best;
827 }
828
1/2
✓ Branch 0 taken 6740 times.
✗ Branch 1 not taken.
6740 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
829
3/4
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 6586 times.
✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
6740 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
830 154 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
831 int i;
832
2/2
✓ Branch 0 taken 621 times.
✓ Branch 1 taken 154 times.
775 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
833 621 enum AVSampleFormat p = link->incfg.formats->formats[i];
834 621 best = find_best_sample_fmt_of_2(best, p, ref->format);
835 }
836 154 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
837 154 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
838 154 av_get_sample_fmt_name(ref->format));
839 154 link->incfg.formats->formats[0] = best;
840 }
841 }
842
843 38262 link->incfg.formats->nb_formats = 1;
844 38262 link->format = link->incfg.formats->formats[0];
845
846
2/2
✓ Branch 0 taken 31522 times.
✓ Branch 1 taken 6740 times.
38262 if (link->type == AVMEDIA_TYPE_VIDEO) {
847 31522 enum AVPixelFormat swfmt = link->format;
848
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31522 times.
31522 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
849 // FIXME: this is a hack - we'd like to use the sw_format of
850 // link->hw_frames_ctx here, but it is not yet available.
851 // To make this work properly we will need to either reorder
852 // things so that it is available here or somehow negotiate
853 // sw_format separately.
854 ✗ swfmt = AV_PIX_FMT_YUV420P;
855 }
856
857 31522 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
858
2/2
✓ Branch 1 taken 9937 times.
✓ Branch 2 taken 21585 times.
31522 if (!ff_fmt_is_regular_yuv(swfmt)) {
859 /* These fields are explicitly documented as affecting YUV only,
860 * so set them to sane values for other formats. */
861
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 9331 times.
9937 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
862 606 link->color_range = AVCOL_RANGE_UNSPECIFIED;
863 else
864 9331 link->color_range = AVCOL_RANGE_JPEG;
865
2/2
✓ Branch 0 taken 8311 times.
✓ Branch 1 taken 1626 times.
9937 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
866 8311 link->colorspace = AVCOL_SPC_RGB;
867 } else {
868 1626 link->colorspace = AVCOL_SPC_UNSPECIFIED;
869 }
870 } else {
871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21585 times.
21585 if (!link->incfg.color_spaces->nb_formats) {
872 ✗ av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
873 ✗ " the link between filters %s and %s.\n", link->src->name,
874 ✗ link->dst->name);
875 ✗ return AVERROR(EINVAL);
876 }
877 21585 link->incfg.color_spaces->nb_formats = 1;
878 21585 link->colorspace = link->incfg.color_spaces->formats[0];
879
880
2/2
✓ Branch 1 taken 683 times.
✓ Branch 2 taken 20902 times.
21585 if (ff_fmt_is_forced_full_range(swfmt)) {
881 683 link->color_range = AVCOL_RANGE_JPEG;
882 } else {
883
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20902 times.
20902 if (!link->incfg.color_ranges->nb_formats) {
884 ✗ av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
885 ✗ " the link between filters %s and %s.\n", link->src->name,
886 ✗ link->dst->name);
887 ✗ return AVERROR(EINVAL);
888 }
889 20902 link->incfg.color_ranges->nb_formats = 1;
890 20902 link->color_range = link->incfg.color_ranges->formats[0];
891 }
892 }
893
894
2/2
✓ Branch 0 taken 3785 times.
✓ Branch 1 taken 27737 times.
31522 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) {
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3785 times.
3785 if (!link->incfg.alpha_modes->nb_formats) {
896 ✗ av_log(link->src, AV_LOG_ERROR, "Cannot select alpha mode for"
897 ✗ " the link between filters %s and %s.\n", link->src->name,
898 ✗ link->dst->name);
899 ✗ return AVERROR(EINVAL);
900 }
901 3785 link->incfg.alpha_modes->nb_formats = 1;
902 3785 link->alpha_mode = link->incfg.alpha_modes->formats[0];
903 } else {
904 27737 link->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
905 }
906
907
4/4
✓ Branch 0 taken 17248 times.
✓ Branch 1 taken 14274 times.
✓ Branch 2 taken 512 times.
✓ Branch 3 taken 16736 times.
31522 if (desc->log2_chroma_w || desc->log2_chroma_h) {
908
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14786 times.
14786 if (!link->incfg.chroma_locations->nb_formats) {
909 ✗ av_log(link->src, AV_LOG_ERROR, "Cannot select chroma location"
910 ✗ " for the link between filters %s and %s.\n", link->src->name,
911 ✗ link->dst->name);
912 ✗ return AVERROR(EINVAL);
913 }
914 14786 link->incfg.chroma_locations->nb_formats = 1;
915 14786 link->chroma_location = link->incfg.chroma_locations->formats[0];
916 } else {
917 /* Chroma location is meaningless without subsampled chroma. */
918 16736 link->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
919 }
920
1/2
✓ Branch 0 taken 6740 times.
✗ Branch 1 not taken.
6740 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
921 int ret;
922
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6740 times.
6740 if (!link->incfg.samplerates->nb_formats) {
924 ✗ av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
925 ✗ " the link between filters %s and %s.\n", link->src->name,
926 ✗ link->dst->name);
927 ✗ return AVERROR(EINVAL);
928 }
929 6740 link->incfg.samplerates->nb_formats = 1;
930 6740 link->sample_rate = link->incfg.samplerates->formats[0];
931
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6740 times.
6740 if (link->incfg.channel_layouts->all_layouts) {
933 ✗ av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
934 ✗ " the link between filters %s and %s.\n", link->src->name,
935 ✗ link->dst->name);
936 ✗ if (!link->incfg.channel_layouts->all_counts)
937 ✗ av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
938 "supported, try specifying a channel layout using "
939 "'aformat=channel_layouts=something'.\n");
940 ✗ return AVERROR(EINVAL);
941 }
942 6740 link->incfg.channel_layouts->nb_channel_layouts = 1;
943 6740 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6740 times.
6740 if (ret < 0)
945 ✗ return ret;
946 }
947
948 38262 ff_formats_unref(&link->incfg.formats);
949 38262 ff_formats_unref(&link->outcfg.formats);
950 38262 ff_formats_unref(&link->incfg.samplerates);
951 38262 ff_formats_unref(&link->outcfg.samplerates);
952 38262 ff_channel_layouts_unref(&link->incfg.channel_layouts);
953 38262 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
954 38262 ff_formats_unref(&link->incfg.color_spaces);
955 38262 ff_formats_unref(&link->outcfg.color_spaces);
956 38262 ff_formats_unref(&link->incfg.color_ranges);
957 38262 ff_formats_unref(&link->outcfg.color_ranges);
958 38262 ff_formats_unref(&link->incfg.alpha_modes);
959 38262 ff_formats_unref(&link->outcfg.alpha_modes);
960 38262 ff_formats_unref(&link->incfg.chroma_locations);
961 38262 ff_formats_unref(&link->outcfg.chroma_locations);
962
963 38262 return 0;
964 }
965
966 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
967 do { \
968 for (i = 0; i < filter->nb_inputs; i++) { \
969 AVFilterLink *link = filter->inputs[i]; \
970 fmt_type fmt; \
971 \
972 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
973 continue; \
974 fmt = link->outcfg.list->var[0]; \
975 \
976 for (j = 0; j < filter->nb_outputs; j++) { \
977 AVFilterLink *out_link = filter->outputs[j]; \
978 list_type *fmts; \
979 \
980 if (link->type != out_link->type || \
981 out_link->incfg.list->nb == 1) \
982 continue; \
983 fmts = out_link->incfg.list; \
984 \
985 if (!out_link->incfg.list->nb) { \
986 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
987 return ret; \
988 ret = 1; \
989 break; \
990 } \
991 \
992 for (k = 0; k < out_link->incfg.list->nb; k++) \
993 if (fmts->var[k] == fmt) { \
994 fmts->var[0] = fmt; \
995 fmts->nb = 1; \
996 ret = 1; \
997 break; \
998 } \
999 } \
1000 } \
1001 } while (0)
1002
1003 75169 static int reduce_formats_on_filter(AVFilterContext *filter)
1004 {
1005 75169 int i, j, k, ret = 0;
1006
1007
16/20
✓ Branch 0 taken 61839 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1581 times.
✓ Branch 3 taken 60258 times.
✓ Branch 4 taken 47250 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 46612 times.
✓ Branch 7 taken 638 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 638 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 87 times.
✓ Branch 14 taken 4942 times.
✓ Branch 15 taken 5029 times.
✓ Branch 16 taken 551 times.
✓ Branch 17 taken 47262 times.
✓ Branch 18 taken 60258 times.
✓ Branch 19 taken 61839 times.
✓ Branch 20 taken 75169 times.
189212 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
1008 nb_formats, ff_add_format);
1009
18/20
✓ Branch 0 taken 12188 times.
✓ Branch 1 taken 49651 times.
✓ Branch 2 taken 1522 times.
✓ Branch 3 taken 10666 times.
✓ Branch 4 taken 8800 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 7842 times.
✓ Branch 7 taken 958 times.
✓ Branch 8 taken 802 times.
✓ Branch 9 taken 156 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 802 times.
✓ Branch 13 taken 156 times.
✓ Branch 14 taken 615 times.
✓ Branch 15 taken 771 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 8806 times.
✓ Branch 18 taken 9864 times.
✓ Branch 19 taken 61839 times.
✓ Branch 20 taken 75169 times.
145627 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
1010 nb_formats, ff_add_format);
1011
15/20
✓ Branch 0 taken 49651 times.
✓ Branch 1 taken 12188 times.
✓ Branch 2 taken 11957 times.
✓ Branch 3 taken 37694 times.
✓ Branch 4 taken 29620 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25545 times.
✓ Branch 7 taken 4075 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4075 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 4075 times.
✓ Branch 14 taken 499 times.
✓ Branch 15 taken 4574 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 29620 times.
✓ Branch 18 taken 37694 times.
✓ Branch 19 taken 61839 times.
✓ Branch 20 taken 75169 times.
167127 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
1012 nb_formats, ff_add_format);
1013
15/20
✓ Branch 0 taken 49651 times.
✓ Branch 1 taken 12188 times.
✓ Branch 2 taken 9218 times.
✓ Branch 3 taken 40433 times.
✓ Branch 4 taken 30939 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27614 times.
✓ Branch 7 taken 3325 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3325 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3325 times.
✓ Branch 14 taken 3307 times.
✓ Branch 15 taken 6632 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 30939 times.
✓ Branch 18 taken 40433 times.
✓ Branch 19 taken 61839 times.
✓ Branch 20 taken 75169 times.
171254 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
1014 nb_formats, ff_add_format);
1015
15/20
✓ Branch 0 taken 49651 times.
✓ Branch 1 taken 12188 times.
✓ Branch 2 taken 49025 times.
✓ Branch 3 taken 626 times.
✓ Branch 4 taken 478 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 476 times.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 4 times.
✓ Branch 15 taken 6 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 478 times.
✓ Branch 18 taken 626 times.
✓ Branch 19 taken 61839 times.
✓ Branch 20 taken 75169 times.
137490 REDUCE_FORMATS(int, AVFilterFormats, alpha_modes, formats,
1016 nb_formats, ff_add_format);
1017
16/20
✓ Branch 0 taken 49651 times.
✓ Branch 1 taken 12188 times.
✓ Branch 2 taken 42627 times.
✓ Branch 3 taken 7024 times.
✓ Branch 4 taken 5019 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4624 times.
✓ Branch 7 taken 395 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 395 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 393 times.
✓ Branch 14 taken 753 times.
✓ Branch 15 taken 1146 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 5019 times.
✓ Branch 18 taken 7024 times.
✓ Branch 19 taken 61839 times.
✓ Branch 20 taken 75169 times.
142780 REDUCE_FORMATS(int, AVFilterFormats, chroma_locations, formats,
1018 nb_formats, ff_add_format);
1019
1020 /* reduce channel layouts */
1021
2/2
✓ Branch 0 taken 61839 times.
✓ Branch 1 taken 75169 times.
137008 for (i = 0; i < filter->nb_inputs; i++) {
1022 61839 AVFilterLink *inlink = filter->inputs[i];
1023 const AVChannelLayout *fmt;
1024
1025
2/2
✓ Branch 0 taken 12188 times.
✓ Branch 1 taken 49651 times.
61839 if (!inlink->outcfg.channel_layouts ||
1026
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 11890 times.
12188 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
1027 49949 continue;
1028 11890 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
1029
1030
2/2
✓ Branch 0 taken 9586 times.
✓ Branch 1 taken 11037 times.
20623 for (j = 0; j < filter->nb_outputs; j++) {
1031 9586 AVFilterLink *outlink = filter->outputs[j];
1032 AVFilterChannelLayouts *fmts;
1033
1034 9586 fmts = outlink->incfg.channel_layouts;
1035
4/4
✓ Branch 0 taken 9580 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8575 times.
✓ Branch 3 taken 1005 times.
9586 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
1036 8581 continue;
1037
1038
2/2
✓ Branch 0 taken 853 times.
✓ Branch 1 taken 152 times.
1005 if (fmts->all_layouts &&
1039
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 846 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
853 (KNOWN(fmt) || fmts->all_counts)) {
1040 /* Turn the infinite list into a singleton */
1041 853 fmts->all_layouts = fmts->all_counts = 0;
1042 853 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
1043
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 853 times.
853 if (ret < 0)
1044 ✗ return ret;
1045 853 ret = 1;
1046 853 break;
1047 }
1048
1049
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 6 times.
260 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
1050
2/2
✓ Branch 1 taken 146 times.
✓ Branch 2 taken 108 times.
254 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
1051 146 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
1052
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 146 times.
146 if (ret < 0)
1053 ✗ return ret;
1054 146 fmts->nb_channel_layouts = 1;
1055 146 ret = 1;
1056 146 break;
1057 }
1058 }
1059 }
1060 }
1061
1062 75169 return ret;
1063 }
1064
1065 8425 static int reduce_formats(AVFilterGraph *graph)
1066 {
1067 int i, reduced, ret;
1068
1069 do {
1070 13262 reduced = 0;
1071
1072
2/2
✓ Branch 0 taken 75169 times.
✓ Branch 1 taken 13262 times.
88431 for (i = 0; i < graph->nb_filters; i++) {
1073
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 75169 times.
75169 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
1074 ✗ return ret;
1075 75169 reduced |= ret;
1076 }
1077
2/2
✓ Branch 0 taken 4837 times.
✓ Branch 1 taken 8425 times.
13262 } while (reduced);
1078
1079 8425 return 0;
1080 }
1081
1082 46719 static void swap_samplerates_on_filter(AVFilterContext *filter)
1083 {
1084 46719 AVFilterLink *link = NULL;
1085 int sample_rate;
1086 int i, j;
1087
1088
2/2
✓ Branch 0 taken 38172 times.
✓ Branch 1 taken 40065 times.
78237 for (i = 0; i < filter->nb_inputs; i++) {
1089 38172 link = filter->inputs[i];
1090
1091
2/2
✓ Branch 0 taken 6654 times.
✓ Branch 1 taken 31518 times.
38172 if (link->type == AVMEDIA_TYPE_AUDIO &&
1092
1/2
✓ Branch 0 taken 6654 times.
✗ Branch 1 not taken.
6654 link->outcfg.samplerates->nb_formats== 1)
1093 6654 break;
1094 }
1095
2/2
✓ Branch 0 taken 40065 times.
✓ Branch 1 taken 6654 times.
46719 if (i == filter->nb_inputs)
1096 40065 return;
1097
1098 6654 sample_rate = link->outcfg.samplerates->formats[0];
1099
1100
2/2
✓ Branch 0 taken 5229 times.
✓ Branch 1 taken 6654 times.
11883 for (i = 0; i < filter->nb_outputs; i++) {
1101 5229 AVFilterLink *outlink = filter->outputs[i];
1102 5229 int best_idx, best_diff = INT_MAX;
1103
1104
2/2
✓ Branch 0 taken 5227 times.
✓ Branch 1 taken 2 times.
5229 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1105
1/2
✓ Branch 0 taken 5227 times.
✗ Branch 1 not taken.
5227 outlink->incfg.samplerates->nb_formats < 2)
1106 5229 continue;
1107
1108 ✗ for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
1109 ✗ int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
1110
1111 ✗ av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
1112
1113 ✗ if (diff < best_diff) {
1114 ✗ best_diff = diff;
1115 ✗ best_idx = j;
1116 }
1117 }
1118 ✗ FFSWAP(int, outlink->incfg.samplerates->formats[0],
1119 outlink->incfg.samplerates->formats[best_idx]);
1120 }
1121 }
1122
1123 8425 static void swap_samplerates(AVFilterGraph *graph)
1124 {
1125 int i;
1126
1127
2/2
✓ Branch 0 taken 46719 times.
✓ Branch 1 taken 8425 times.
55144 for (i = 0; i < graph->nb_filters; i++)
1128 46719 swap_samplerates_on_filter(graph->filters[i]);
1129 8425 }
1130
1131 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
1132 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
1133 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
1134 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
1135 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
1136 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
1137 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
1138
1139 /* allowable substitutions for channel pairs when comparing layouts,
1140 * ordered by priority for both values */
1141 static const uint64_t ch_subst[][2] = {
1142 { CH_FRONT_PAIR, CH_CENTER_PAIR },
1143 { CH_FRONT_PAIR, CH_WIDE_PAIR },
1144 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
1145 { CH_CENTER_PAIR, CH_FRONT_PAIR },
1146 { CH_CENTER_PAIR, CH_WIDE_PAIR },
1147 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
1148 { CH_WIDE_PAIR, CH_FRONT_PAIR },
1149 { CH_WIDE_PAIR, CH_CENTER_PAIR },
1150 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
1151 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
1152 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
1153 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
1154 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
1155 { CH_SIDE_PAIR, CH_BACK_PAIR },
1156 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
1157 { CH_BACK_PAIR, CH_DIRECT_PAIR },
1158 { CH_BACK_PAIR, CH_SIDE_PAIR },
1159 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
1160 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
1161 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
1162 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
1163 };
1164
1165 46719 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1166 {
1167 46719 AVFilterLink *link = NULL;
1168 int i, j, k;
1169
1170
2/2
✓ Branch 0 taken 38172 times.
✓ Branch 1 taken 40071 times.
78243 for (i = 0; i < filter->nb_inputs; i++) {
1171 38172 link = filter->inputs[i];
1172
1173
2/2
✓ Branch 0 taken 6654 times.
✓ Branch 1 taken 31518 times.
38172 if (link->type == AVMEDIA_TYPE_AUDIO &&
1174
2/2
✓ Branch 0 taken 6648 times.
✓ Branch 1 taken 6 times.
6654 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1175 6648 break;
1176 }
1177
2/2
✓ Branch 0 taken 40071 times.
✓ Branch 1 taken 6648 times.
46719 if (i == filter->nb_inputs)
1178 40071 return;
1179
1180
2/2
✓ Branch 0 taken 5226 times.
✓ Branch 1 taken 6648 times.
11874 for (i = 0; i < filter->nb_outputs; i++) {
1181 5226 AVFilterLink *outlink = filter->outputs[i];
1182 5226 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1183
1184
2/2
✓ Branch 0 taken 5224 times.
✓ Branch 1 taken 2 times.
5226 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1185
2/2
✓ Branch 0 taken 5221 times.
✓ Branch 1 taken 3 times.
5224 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1186 5223 continue;
1187
1188
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++) {
1189 34 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1190 int in_channels;
1191 int out_channels;
1192 int count_diff;
1193 int matched_channels, extra_channels;
1194 34 int score = 100000;
1195
1196 34 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1197 34 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1198 34 in_channels = in_chlayout.nb_channels;
1199 34 out_channels = out_chlayout.nb_channels;
1200 34 count_diff = out_channels - in_channels;
1201
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)) {
1202 /* Compute score in case the input or output layout encodes
1203 a channel count; in this case the score is not altered by
1204 the computation afterwards, as in_chlayout and
1205 out_chlayout have both been set to 0 */
1206
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))
1207
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1208
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1209 ✗ out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1210 32 score -= 10000 + FFABS(out_channels - in_channels) +
1211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1212 16 av_channel_layout_uninit(&in_chlayout);
1213 16 av_channel_layout_uninit(&out_chlayout);
1214 /* Let the remaining computation run, even if the score
1215 value is not altered */
1216 }
1217
1218 /* channel substitution */
1219
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 34 times.
748 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1220 714 uint64_t cmp0 = ch_subst[k][0];
1221 714 uint64_t cmp1 = ch_subst[k][1];
1222
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) &&
1223
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 100 times.
336 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1224
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
140 av_channel_layout_subset(&out_chlayout, cmp1) &&
1225 20 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1226 4 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1227 4 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1228 /* add score for channel match, minus a deduction for
1229 having to do the substitution */
1230 4 score += 10 * av_popcount64(cmp1) - 2;
1231 }
1232 }
1233
1234 /* no penalty for LFE channel mismatch */
1235
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 &&
1236 18 av_channel_layout_index_from_channel(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1237 7 score += 10;
1238 34 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1239 34 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1240
1241 34 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1242 34 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1243 34 score += 10 * matched_channels - 5 * extra_channels;
1244
1245
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 ||
1246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (count_diff < best_count_diff && score == best_score)) {
1247 10 best_score = score;
1248 10 best_idx = j;
1249 10 best_count_diff = count_diff;
1250 }
1251 }
1252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(best_idx >= 0);
1253 3 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1254 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1255 }
1256
1257 }
1258
1259 8425 static void swap_channel_layouts(AVFilterGraph *graph)
1260 {
1261 int i;
1262
1263
2/2
✓ Branch 0 taken 46719 times.
✓ Branch 1 taken 8425 times.
55144 for (i = 0; i < graph->nb_filters; i++)
1264 46719 swap_channel_layouts_on_filter(graph->filters[i]);
1265 8425 }
1266
1267 46719 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1268 {
1269 46719 AVFilterLink *link = NULL;
1270 int format, bps;
1271 int i, j;
1272
1273
2/2
✓ Branch 0 taken 38242 times.
✓ Branch 1 taken 40489 times.
78731 for (i = 0; i < filter->nb_inputs; i++) {
1274 38242 link = filter->inputs[i];
1275
1276
2/2
✓ Branch 0 taken 6724 times.
✓ Branch 1 taken 31518 times.
38242 if (link->type == AVMEDIA_TYPE_AUDIO &&
1277
2/2
✓ Branch 0 taken 6230 times.
✓ Branch 1 taken 494 times.
6724 link->outcfg.formats->nb_formats == 1)
1278 6230 break;
1279 }
1280
2/2
✓ Branch 0 taken 40489 times.
✓ Branch 1 taken 6230 times.
46719 if (i == filter->nb_inputs)
1281 40489 return;
1282
1283 6230 format = link->outcfg.formats->formats[0];
1284 6230 bps = av_get_bytes_per_sample(format);
1285
1286
2/2
✓ Branch 0 taken 4828 times.
✓ Branch 1 taken 6230 times.
11058 for (i = 0; i < filter->nb_outputs; i++) {
1287 4828 AVFilterLink *outlink = filter->outputs[i];
1288 4828 int best_idx = -1, best_score = INT_MIN;
1289
1290
2/2
✓ Branch 0 taken 4826 times.
✓ Branch 1 taken 2 times.
4828 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1291
2/2
✓ Branch 0 taken 4657 times.
✓ Branch 1 taken 169 times.
4826 outlink->incfg.formats->nb_formats < 2)
1292 4659 continue;
1293
1294
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 10 times.
337 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1295 327 int out_format = outlink->incfg.formats->formats[j];
1296 327 int out_bps = av_get_bytes_per_sample(out_format);
1297 int score;
1298
1299
4/4
✓ Branch 1 taken 186 times.
✓ Branch 2 taken 141 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 168 times.
513 if (av_get_packed_sample_fmt(out_format) == format ||
1300 186 av_get_planar_sample_fmt(out_format) == format) {
1301 159 best_idx = j;
1302 159 break;
1303 }
1304
1305 /* for s32 and float prefer double to prevent loss of information */
1306
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
168 if (bps == 4 && out_bps == 8) {
1307 ✗ best_idx = j;
1308 ✗ break;
1309 }
1310
1311 /* prefer closest higher or equal bps */
1312 168 score = -abs(out_bps - bps);
1313
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 135 times.
168 if (out_bps >= bps)
1314 33 score += INT_MAX/2;
1315
1316
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 39 times.
168 if (score > best_score) {
1317 129 best_score = score;
1318 129 best_idx = j;
1319 }
1320 }
1321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 169 times.
169 av_assert0(best_idx >= 0);
1322 169 FFSWAP(int, outlink->incfg.formats->formats[0],
1323 outlink->incfg.formats->formats[best_idx]);
1324 }
1325 }
1326
1327 8425 static void swap_sample_fmts(AVFilterGraph *graph)
1328 {
1329 int i;
1330
1331
2/2
✓ Branch 0 taken 46719 times.
✓ Branch 1 taken 8425 times.
55144 for (i = 0; i < graph->nb_filters; i++)
1332 46719 swap_sample_fmts_on_filter(graph->filters[i]);
1333
1334 8425 }
1335
1336 8425 static int pick_formats(AVFilterGraph *graph)
1337 {
1338 int i, j, ret;
1339 int change;
1340
1341 do{
1342 16873 change = 0;
1343
2/2
✓ Branch 0 taken 94369 times.
✓ Branch 1 taken 16873 times.
111242 for (i = 0; i < graph->nb_filters; i++) {
1344 94369 AVFilterContext *filter = graph->filters[i];
1345
2/2
✓ Branch 0 taken 76973 times.
✓ Branch 1 taken 17396 times.
94369 if (filter->nb_inputs){
1346
2/2
✓ Branch 0 taken 77392 times.
✓ Branch 1 taken 76973 times.
154365 for (j = 0; j < filter->nb_inputs; j++){
1347
4/4
✓ Branch 0 taken 16514 times.
✓ Branch 1 taken 60878 times.
✓ Branch 2 taken 15939 times.
✓ Branch 3 taken 575 times.
77392 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1348
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15939 times.
15939 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1349 ✗ return ret;
1350 15939 change = 1;
1351 }
1352 }
1353 }
1354
2/2
✓ Branch 0 taken 76908 times.
✓ Branch 1 taken 17461 times.
94369 if (filter->nb_outputs){
1355
2/2
✓ Branch 0 taken 77392 times.
✓ Branch 1 taken 76908 times.
154300 for (j = 0; j < filter->nb_outputs; j++){
1356
4/4
✓ Branch 0 taken 22658 times.
✓ Branch 1 taken 54734 times.
✓ Branch 2 taken 21959 times.
✓ Branch 3 taken 699 times.
77392 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1357
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21959 times.
21959 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1358 ✗ return ret;
1359 21959 change = 1;
1360 }
1361 }
1362 }
1363
6/6
✓ Branch 0 taken 76973 times.
✓ Branch 1 taken 17396 times.
✓ Branch 2 taken 59512 times.
✓ Branch 3 taken 17461 times.
✓ Branch 4 taken 59195 times.
✓ Branch 5 taken 317 times.
94369 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1364
2/2
✓ Branch 0 taken 59611 times.
✓ Branch 1 taken 59195 times.
118806 for (j = 0; j < filter->nb_outputs; j++) {
1365
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 59342 times.
59611 if (filter->outputs[j]->format<0) {
1366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 269 times.
269 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1367 ✗ return ret;
1368 269 change = 1;
1369 }
1370 }
1371 }
1372 }
1373
2/2
✓ Branch 0 taken 8448 times.
✓ Branch 1 taken 8425 times.
16873 }while(change);
1374
1375
2/2
✓ Branch 0 taken 46719 times.
✓ Branch 1 taken 8425 times.
55144 for (i = 0; i < graph->nb_filters; i++) {
1376 46719 AVFilterContext *filter = graph->filters[i];
1377
1378
2/2
✓ Branch 0 taken 38262 times.
✓ Branch 1 taken 46719 times.
84981 for (j = 0; j < filter->nb_inputs; j++)
1379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38262 times.
38262 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1380 ✗ return ret;
1381
2/2
✓ Branch 0 taken 38262 times.
✓ Branch 1 taken 46719 times.
84981 for (j = 0; j < filter->nb_outputs; j++)
1382
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38262 times.
38262 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1383 ✗ return ret;
1384 }
1385 8425 return 0;
1386 }
1387
1388 /**
1389 * Configure the formats of all the links in the graph.
1390 */
1391 8425 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1392 {
1393 int ret;
1394
1395 /* find supported formats from sub-filters, and merge along links */
1396
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 8425 times.
8440 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1397 15 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1398
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8425 times.
8425 if (ret < 0)
1399 ✗ return ret;
1400
1401 /* Once everything is merged, it's possible that we'll still have
1402 * multiple valid media format choices. We try to minimize the amount
1403 * of format conversion inside filters */
1404
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8425 times.
8425 if ((ret = reduce_formats(graph)) < 0)
1405 ✗ return ret;
1406
1407 /* for audio filters, ensure the best format, sample rate and channel layout
1408 * is selected */
1409 8425 swap_sample_fmts(graph);
1410 8425 swap_samplerates(graph);
1411 8425 swap_channel_layouts(graph);
1412
1413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8425 times.
8425 if ((ret = pick_formats(graph)) < 0)
1414 ✗ return ret;
1415
1416 8425 return 0;
1417 }
1418
1419 8424 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1420 {
1421 unsigned i, j;
1422 8424 int sink_links_count = 0, n = 0;
1423 AVFilterContext *f;
1424 FilterLinkInternal **sinks;
1425
1426
2/2
✓ Branch 0 taken 46714 times.
✓ Branch 1 taken 8424 times.
55138 for (i = 0; i < graph->nb_filters; i++) {
1427 46714 f = graph->filters[i];
1428
2/2
✓ Branch 0 taken 38258 times.
✓ Branch 1 taken 46714 times.
84972 for (j = 0; j < f->nb_inputs; j++) {
1429 38258 ff_link_internal(f->inputs[j])->age_index = -1;
1430 }
1431
2/2
✓ Branch 0 taken 38258 times.
✓ Branch 1 taken 46714 times.
84972 for (j = 0; j < f->nb_outputs; j++) {
1432 38258 ff_link_internal(f->outputs[j])->age_index = -1;
1433 }
1434
2/2
✓ Branch 0 taken 8663 times.
✓ Branch 1 taken 38051 times.
46714 if (!f->nb_outputs) {
1435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8663 times.
8663 if (f->nb_inputs > INT_MAX - sink_links_count)
1436 ✗ return AVERROR(EINVAL);
1437 8663 sink_links_count += f->nb_inputs;
1438 }
1439 }
1440 8424 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8424 times.
8424 if (!sinks)
1442 ✗ return AVERROR(ENOMEM);
1443
2/2
✓ Branch 0 taken 46714 times.
✓ Branch 1 taken 8424 times.
55138 for (i = 0; i < graph->nb_filters; i++) {
1444 46714 f = graph->filters[i];
1445
2/2
✓ Branch 0 taken 8663 times.
✓ Branch 1 taken 38051 times.
46714 if (!f->nb_outputs) {
1446
2/2
✓ Branch 0 taken 8663 times.
✓ Branch 1 taken 8663 times.
17326 for (j = 0; j < f->nb_inputs; j++) {
1447 8663 sinks[n] = ff_link_internal(f->inputs[j]);
1448 8663 sinks[n]->age_index = n;
1449 8663 n++;
1450 }
1451 }
1452 }
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8424 times.
8424 av_assert0(n == sink_links_count);
1454 8424 fffiltergraph(graph)->sink_links = sinks;
1455 8424 fffiltergraph(graph)->sink_links_count = sink_links_count;
1456 8424 return 0;
1457 }
1458
1459 8425 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1460 {
1461 int ret;
1462
1463
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8424 times.
8425 if (graphctx->max_buffered_frames)
1464 1 fffiltergraph(graphctx)->frame_queues.max_queued = graphctx->max_buffered_frames;
1465
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8425 times.
8425 if ((ret = graph_check_validity(graphctx, log_ctx)))
1466 ✗ return ret;
1467
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8425 times.
8425 if ((ret = graph_config_formats(graphctx, log_ctx)))
1468 ✗ return ret;
1469
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8424 times.
8425 if ((ret = graph_config_links(graphctx, log_ctx)))
1470 1 return ret;
1471
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8424 times.
8424 if ((ret = graph_check_links(graphctx, log_ctx)))
1472 ✗ return ret;
1473
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8424 times.
8424 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1474 ✗ return ret;
1475
1476 8424 return 0;
1477 }
1478
1479 ✗ int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1480 {
1481 ✗ int i, r = AVERROR(ENOSYS);
1482
1483 ✗ if (!graph)
1484 ✗ return r;
1485
1486 ✗ if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1487 ✗ r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1488 ✗ if (r != AVERROR(ENOSYS))
1489 ✗ return r;
1490 }
1491
1492 ✗ if (res_len && res)
1493 ✗ res[0] = 0;
1494
1495 ✗ for (i = 0; i < graph->nb_filters; i++) {
1496 ✗ AVFilterContext *filter = graph->filters[i];
1497 ✗ if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1498 ✗ r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1499 ✗ if (r != AVERROR(ENOSYS)) {
1500 ✗ if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1501 ✗ return r;
1502 }
1503 }
1504 }
1505
1506 ✗ return r;
1507 }
1508
1509 ✗ int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1510 {
1511 int i;
1512
1513 ✗ if(!graph)
1514 ✗ return 0;
1515
1516 ✗ for (i = 0; i < graph->nb_filters; i++) {
1517 ✗ AVFilterContext *filter = graph->filters[i];
1518 ✗ FFFilterContext *ctxi = fffilterctx(filter);
1519 ✗ if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1520 ✗ AVFilterCommand **queue = &ctxi->command_queue, *next;
1521 ✗ while (*queue && (*queue)->time <= ts)
1522 ✗ queue = &(*queue)->next;
1523 ✗ next = *queue;
1524 ✗ *queue = av_mallocz(sizeof(AVFilterCommand));
1525 ✗ if (!*queue)
1526 ✗ return AVERROR(ENOMEM);
1527
1528 ✗ (*queue)->command = av_strdup(command);
1529 ✗ (*queue)->arg = av_strdup(arg);
1530 ✗ (*queue)->time = ts;
1531 ✗ (*queue)->flags = flags;
1532 ✗ (*queue)->next = next;
1533 ✗ if(flags & AVFILTER_CMD_FLAG_ONE)
1534 ✗ return 0;
1535 }
1536 }
1537
1538 ✗ return 0;
1539 }
1540
1541 475911 static void heap_bubble_up(FFFilterGraph *graph,
1542 FilterLinkInternal *li, int index)
1543 {
1544 475911 FilterLinkInternal **links = graph->sink_links;
1545
1546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 475911 times.
475911 av_assert0(index >= 0);
1547
1548
2/2
✓ Branch 0 taken 14028 times.
✓ Branch 1 taken 475911 times.
489939 while (index) {
1549 14028 int parent = (index - 1) >> 1;
1550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14028 times.
14028 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1551 ✗ break;
1552 14028 links[index] = links[parent];
1553 14028 links[index]->age_index = index;
1554 14028 index = parent;
1555 }
1556 475911 links[index] = li;
1557 475911 li->age_index = index;
1558 475911 }
1559
1560 476131 static void heap_bubble_down(FFFilterGraph *graph,
1561 FilterLinkInternal *li, int index)
1562 {
1563 476131 FilterLinkInternal **links = graph->sink_links;
1564
1565
1/2
✓ Branch 0 taken 476131 times.
✗ Branch 1 not taken.
476131 av_assert0(index >= 0);
1566
1567 24207 while (1) {
1568 500338 int child = 2 * index + 1;
1569
2/2
✓ Branch 0 taken 475769 times.
✓ Branch 1 taken 24569 times.
500338 if (child >= graph->sink_links_count)
1570 475769 break;
1571
2/2
✓ Branch 0 taken 23033 times.
✓ Branch 1 taken 1536 times.
24569 if (child + 1 < graph->sink_links_count &&
1572
2/2
✓ Branch 0 taken 8296 times.
✓ Branch 1 taken 14737 times.
23033 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1573 8296 child++;
1574
2/2
✓ Branch 0 taken 362 times.
✓ Branch 1 taken 24207 times.
24569 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1575 362 break;
1576 24207 links[index] = links[child];
1577 24207 links[index]->age_index = index;
1578 24207 index = child;
1579 }
1580 476131 links[index] = li;
1581 476131 li->age_index = index;
1582 476131 }
1583
1584 475911 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1585 {
1586 475911 FFFilterGraph *graphi = fffiltergraph(graph);
1587
1588 475911 heap_bubble_up (graphi, li, li->age_index);
1589 475911 heap_bubble_down(graphi, li, li->age_index);
1590 475911 }
1591
1592 460930 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1593 {
1594 460930 FFFilterGraph *graphi = fffiltergraph(graph);
1595 460930 FilterLinkInternal *oldesti = graphi->sink_links[0];
1596 460930 AVFilterLink *oldest = &oldesti->l.pub;
1597 int64_t frame_count;
1598 int r;
1599
1600
2/2
✓ Branch 0 taken 461163 times.
✓ Branch 1 taken 5195 times.
466358 while (graphi->sink_links_count) {
1601 461163 oldesti = graphi->sink_links[0];
1602 461163 oldest = &oldesti->l.pub;
1603
2/2
✓ Branch 1 taken 461158 times.
✓ Branch 2 taken 5 times.
461163 if (fffilter(oldest->dst->filter)->activate) {
1604 461158 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1605 AV_BUFFERSINK_FLAG_PEEK);
1606
2/2
✓ Branch 0 taken 455731 times.
✓ Branch 1 taken 5427 times.
461158 if (r != AVERROR_EOF)
1607 455731 return r;
1608 } else {
1609 5 r = ff_request_frame(oldest);
1610 }
1611
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 5428 times.
5432 if (r != AVERROR_EOF)
1612 4 break;
1613 5428 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1614 5428 oldest->dst->name,
1615 5428 oldest->dstpad->name);
1616 /* EOF: remove the link from the heap */
1617
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 5208 times.
5428 if (oldesti->age_index < --graphi->sink_links_count)
1618 220 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1619 oldesti->age_index);
1620 5428 oldesti->age_index = -1;
1621 }
1622
2/2
✓ Branch 0 taken 5195 times.
✓ Branch 1 taken 4 times.
5199 if (!graphi->sink_links_count)
1623 5195 return AVERROR_EOF;
1624 av_assert1(!fffilter(oldest->dst->filter)->activate);
1625 av_assert1(oldesti->age_index >= 0);
1626 4 frame_count = oldesti->l.frame_count_out;
1627
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 while (frame_count == oldesti->l.frame_count_out) {
1628 16 r = ff_filter_graph_run_once(graph);
1629
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (r == FFERROR_BUFFERSRC_EMPTY)
1630 4 r = 0;
1631
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (r == AVERROR(EAGAIN) &&
1632
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1633 ✗ !oldesti->status_in)
1634 ✗ (void)ff_request_frame(oldest);
1635
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 else if (r < 0)
1636 4 return r;
1637 }
1638 ✗ return 0;
1639 }
1640
1641 5215807 int ff_filter_graph_run_once(AVFilterGraph *graph)
1642 {
1643 FFFilterContext *ctxi;
1644 unsigned i;
1645
1646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5215807 times.
5215807 av_assert0(graph->nb_filters);
1647 5215807 ctxi = fffilterctx(graph->filters[0]);
1648
2/2
✓ Branch 0 taken 25921373 times.
✓ Branch 1 taken 5215807 times.
31137180 for (i = 1; i < graph->nb_filters; i++) {
1649 25921373 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1650
1651
2/2
✓ Branch 0 taken 3132501 times.
✓ Branch 1 taken 22788872 times.
25921373 if (ctxi_other->ready > ctxi->ready)
1652 3132501 ctxi = ctxi_other;
1653 }
1654
1655
2/2
✓ Branch 0 taken 858819 times.
✓ Branch 1 taken 4356988 times.
5215807 if (!ctxi->ready)
1656 858819 return AVERROR(EAGAIN);
1657 4356988 return ff_filter_activate(&ctxi->p);
1658 }
1659