FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2025-01-20 09:27:23
Exec Total Coverage
Lines: 612 791 77.4%
Functions: 36 39 92.3%
Branches: 509 745 68.3%

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 { NULL },
60 };
61
62 static const AVClass filtergraph_class = {
63 .class_name = "AVFilterGraph",
64 .item_name = av_default_item_name,
65 .version = LIBAVUTIL_VERSION_INT,
66 .option = filtergraph_options,
67 .category = AV_CLASS_CATEGORY_FILTER,
68 };
69
70 #if !HAVE_THREADS
71 void ff_graph_thread_free(FFFilterGraph *graph)
72 {
73 }
74
75 int ff_graph_thread_init(FFFilterGraph *graph)
76 {
77 graph->p.thread_type = 0;
78 graph->p.nb_threads = 1;
79 return 0;
80 }
81 #endif
82
83 15237 AVFilterGraph *avfilter_graph_alloc(void)
84 {
85 15237 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
86 AVFilterGraph *ret;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15237 times.
15237 if (!graph)
89 return NULL;
90
91 15237 ret = &graph->p;
92 15237 ret->av_class = &filtergraph_class;
93 15237 av_opt_set_defaults(ret);
94 15237 ff_framequeue_global_init(&graph->frame_queues);
95
96 15237 return ret;
97 }
98
99 58008 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
100 {
101 int i, j;
102
1/2
✓ Branch 0 taken 58008 times.
✗ Branch 1 not taken.
58008 for (i = 0; i < graph->nb_filters; i++) {
103
1/2
✓ Branch 0 taken 58008 times.
✗ Branch 1 not taken.
58008 if (graph->filters[i] == filter) {
104 58008 FFSWAP(AVFilterContext*, graph->filters[i],
105 graph->filters[graph->nb_filters - 1]);
106 58008 graph->nb_filters--;
107 58008 filter->graph = NULL;
108
2/2
✓ Branch 0 taken 50436 times.
✓ Branch 1 taken 58008 times.
108444 for (j = 0; j<filter->nb_outputs; j++)
109
2/2
✓ Branch 0 taken 19414 times.
✓ Branch 1 taken 31022 times.
50436 if (filter->outputs[j])
110 19414 ff_filter_link(filter->outputs[j])->graph = NULL;
111
112 58008 return;
113 }
114 }
115 }
116
117 22778 void avfilter_graph_free(AVFilterGraph **graphp)
118 {
119 22778 AVFilterGraph *graph = *graphp;
120 22778 FFFilterGraph *graphi = fffiltergraph(graph);
121
122
2/2
✓ Branch 0 taken 7541 times.
✓ Branch 1 taken 15237 times.
22778 if (!graph)
123 7541 return;
124
125
2/2
✓ Branch 0 taken 57942 times.
✓ Branch 1 taken 15237 times.
73179 while (graph->nb_filters)
126 57942 avfilter_free(graph->filters[0]);
127
128 15237 ff_graph_thread_free(graphi);
129
130 15237 av_freep(&graphi->sink_links);
131
132 15237 av_opt_free(graph);
133
134 15237 av_freep(&graph->filters);
135 15237 av_freep(graphp);
136 }
137
138 17894 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
139 const char *name, const char *args, void *opaque,
140 AVFilterGraph *graph_ctx)
141 {
142 int ret;
143
144 17894 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17894 times.
17894 if (!*filt_ctx)
146 return AVERROR(ENOMEM);
147
148 17894 ret = avfilter_init_str(*filt_ctx, args);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17894 times.
17894 if (ret < 0)
150 goto fail;
151
152 17894 return 0;
153
154 fail:
155 avfilter_free(*filt_ctx);
156 *filt_ctx = NULL;
157 return ret;
158 }
159
160 5813 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
161 {
162 5813 fffiltergraph(graph)->disable_auto_convert = flags;
163 5813 }
164
165 58008 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
166 const AVFilter *filter,
167 const char *name)
168 {
169 AVFilterContext **filters, *s;
170 58008 FFFilterGraph *graphi = fffiltergraph(graph);
171
172
4/4
✓ Branch 0 taken 29997 times.
✓ Branch 1 taken 28011 times.
✓ Branch 2 taken 15237 times.
✓ Branch 3 taken 14760 times.
58008 if (graph->thread_type && !graphi->thread_execute) {
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15237 times.
15237 if (graph->execute) {
174 graphi->thread_execute = graph->execute;
175 } else {
176 15237 int ret = ff_graph_thread_init(graphi);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15237 times.
15237 if (ret < 0) {
178 av_log(graph, AV_LOG_ERROR, "Error initializing threading: %s.\n", av_err2str(ret));
179 return NULL;
180 }
181 }
182 }
183
184 58008 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58008 times.
58008 if (!filters)
186 return NULL;
187 58008 graph->filters = filters;
188
189 58008 s = ff_filter_alloc(filter, name);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58008 times.
58008 if (!s)
191 return NULL;
192
193 58008 graph->filters[graph->nb_filters++] = s;
194
195 58008 s->graph = graph;
196
197 58008 return s;
198 }
199
200 /**
201 * Check for the validity of graph.
202 *
203 * A graph is considered valid if all its input and output pads are
204 * connected.
205 *
206 * @return >= 0 in case of success, a negative value otherwise
207 */
208 7630 static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
209 {
210 AVFilterContext *filt;
211 int i, j;
212
213
2/2
✓ Branch 0 taken 40056 times.
✓ Branch 1 taken 7630 times.
47686 for (i = 0; i < graph->nb_filters; i++) {
214 const AVFilterPad *pad;
215 40056 filt = graph->filters[i];
216
217
2/2
✓ Branch 0 taken 32389 times.
✓ Branch 1 taken 40056 times.
72445 for (j = 0; j < filt->nb_inputs; j++) {
218
2/4
✓ Branch 0 taken 32389 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32389 times.
32389 if (!filt->inputs[j] || !filt->inputs[j]->src) {
219 pad = &filt->input_pads[j];
220 av_log(log_ctx, AV_LOG_ERROR,
221 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
222 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
223 return AVERROR(EINVAL);
224 }
225 }
226
227
2/2
✓ Branch 0 taken 32389 times.
✓ Branch 1 taken 40056 times.
72445 for (j = 0; j < filt->nb_outputs; j++) {
228
2/4
✓ Branch 0 taken 32389 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32389 times.
32389 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
229 pad = &filt->output_pads[j];
230 av_log(log_ctx, AV_LOG_ERROR,
231 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
232 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
233 return AVERROR(EINVAL);
234 }
235 }
236 }
237
238 7630 return 0;
239 }
240
241 /**
242 * Configure all the links of graphctx.
243 *
244 * @return >= 0 in case of success, a negative value otherwise
245 */
246 7630 static int graph_config_links(AVFilterGraph *graph, void *log_ctx)
247 {
248 AVFilterContext *filt;
249 int i, ret;
250
251
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++) {
252 41304 filt = graph->filters[i];
253
254
2/2
✓ Branch 0 taken 7762 times.
✓ Branch 1 taken 33542 times.
41304 if (!filt->nb_outputs) {
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7762 times.
7762 if ((ret = ff_filter_config_links(filt)))
256 return ret;
257 }
258 }
259
260 7630 return 0;
261 }
262
263 7630 static int graph_check_links(AVFilterGraph *graph, void *log_ctx)
264 {
265 AVFilterContext *f;
266 AVFilterLink *l;
267 unsigned i, j;
268 int ret;
269
270
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++) {
271 41304 f = graph->filters[i];
272
2/2
✓ Branch 0 taken 33637 times.
✓ Branch 1 taken 41304 times.
74941 for (j = 0; j < f->nb_outputs; j++) {
273 33637 l = f->outputs[j];
274
2/2
✓ Branch 0 taken 27740 times.
✓ Branch 1 taken 5897 times.
33637 if (l->type == AVMEDIA_TYPE_VIDEO) {
275 27740 ret = av_image_check_size2(l->w, l->h, INT64_MAX, l->format, 0, f);
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27740 times.
27740 if (ret < 0)
277 return ret;
278 }
279 }
280 }
281 7630 return 0;
282 }
283
284 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
285 {
286 int i;
287
288 for (i = 0; i < graph->nb_filters; i++)
289 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
290 return graph->filters[i];
291
292 return NULL;
293 }
294
295 54861 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
296 {
297 int ret;
298
299
2/3
✓ Branch 0 taken 45511 times.
✓ Branch 1 taken 9350 times.
✗ Branch 2 not taken.
54861 switch (link->type) {
300
301 45511 case AVMEDIA_TYPE_VIDEO:
302
2/4
✓ Branch 1 taken 45511 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45511 times.
✗ Branch 4 not taken.
91022 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45511 times.
91022 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
304 45511 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0)
305 return ret;
306 45511 break;
307
308 9350 case AVMEDIA_TYPE_AUDIO:
309
2/4
✓ Branch 1 taken 9350 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9350 times.
✗ Branch 4 not taken.
18700 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9350 times.
18700 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
311 9350 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
312 return ret;
313 9350 break;
314
315 default:
316 av_assert0(!"reached");
317 }
318 54861 return 0;
319 }
320
321 /**
322 * Check the validity of the formats / etc. lists set by query_formats().
323 *
324 * In particular, check they do not contain any redundant element.
325 */
326 34722 static int filter_check_formats(AVFilterContext *ctx)
327 {
328 unsigned i;
329 int ret;
330
331
2/2
✓ Branch 0 taken 27833 times.
✓ Branch 1 taken 34722 times.
62555 for (i = 0; i < ctx->nb_inputs; i++) {
332 27833 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27833 times.
27833 if (ret < 0)
334 return ret;
335 }
336
2/2
✓ Branch 0 taken 27028 times.
✓ Branch 1 taken 34722 times.
61750 for (i = 0; i < ctx->nb_outputs; i++) {
337 27028 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27028 times.
27028 if (ret < 0)
339 return ret;
340 }
341 34722 return 0;
342 }
343
344 41318 static int filter_query_formats(AVFilterContext *ctx)
345 {
346 41318 const FFFilter *const filter = fffilter(ctx->filter);
347 int ret;
348
349
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 41290 times.
41318 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
350
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
28 if ((ret = filter->formats.query_func(ctx)) < 0) {
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret != AVERROR(EAGAIN))
352 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
353 ctx->name, av_err2str(ret));
354 14 return ret;
355 }
356
2/2
✓ Branch 0 taken 34708 times.
✓ Branch 1 taken 6582 times.
41290 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
357 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
358 34708 AVFilterFormatsConfig **cfg_in_dyn = NULL, **cfg_out_dyn = NULL;
359 AVFilterFormatsConfig **cfg_in, **cfg_out;
360
361
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34707 times.
34708 if (ctx->nb_inputs > FF_ARRAY_ELEMS(cfg_in_stack)) {
362 1 cfg_in_dyn = av_malloc_array(ctx->nb_inputs, sizeof(*cfg_in_dyn));
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_in_dyn)
364 return AVERROR(ENOMEM);
365 1 cfg_in = cfg_in_dyn;
366 } else
367
2/2
✓ Branch 0 taken 27704 times.
✓ Branch 1 taken 7003 times.
34707 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
368
369
2/2
✓ Branch 0 taken 27818 times.
✓ Branch 1 taken 34708 times.
62526 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
370 27818 AVFilterLink *l = ctx->inputs[i];
371 27818 cfg_in[i] = &l->outcfg;
372 }
373
374
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34707 times.
34708 if (ctx->nb_outputs > FF_ARRAY_ELEMS(cfg_out_stack)) {
375 1 cfg_out_dyn = av_malloc_array(ctx->nb_outputs, sizeof(*cfg_out_dyn));
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_out_dyn) {
377 av_freep(&cfg_in_dyn);
378 return AVERROR(ENOMEM);
379 }
380 1 cfg_out = cfg_out_dyn;
381 } else
382
2/2
✓ Branch 0 taken 26945 times.
✓ Branch 1 taken 7762 times.
34707 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
383
384
2/2
✓ Branch 0 taken 27014 times.
✓ Branch 1 taken 34708 times.
61722 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
385 27014 AVFilterLink *l = ctx->outputs[i];
386 27014 cfg_out[i] = &l->incfg;
387 }
388
389 34708 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
390 34708 av_freep(&cfg_in_dyn);
391 34708 av_freep(&cfg_out_dyn);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34708 times.
34708 if (ret < 0) {
393 if (ret != AVERROR(EAGAIN))
394 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
395 ctx->name, av_err2str(ret));
396 return ret;
397 }
398 }
399
400
2/2
✓ Branch 0 taken 41290 times.
✓ Branch 1 taken 14 times.
41304 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
401
2/2
✓ Branch 0 taken 34708 times.
✓ Branch 1 taken 6582 times.
41290 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
402 34722 ret = filter_check_formats(ctx);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34722 times.
34722 if (ret < 0)
404 return ret;
405 }
406
407 41304 return ff_default_query_formats(ctx);
408 }
409
410 40114 static int formats_declared(AVFilterContext *f)
411 {
412 int i;
413
414
2/2
✓ Branch 0 taken 32299 times.
✓ Branch 1 taken 7848 times.
40147 for (i = 0; i < f->nb_inputs; i++) {
415
2/2
✓ Branch 0 taken 32264 times.
✓ Branch 1 taken 35 times.
32299 if (!f->inputs[i]->outcfg.formats)
416 32264 return 0;
417
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
35 if (f->inputs[i]->type == AVMEDIA_TYPE_VIDEO &&
418
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 !(f->inputs[i]->outcfg.color_ranges &&
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->inputs[i]->outcfg.color_spaces))
420 2 return 0;
421
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
422
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(f->inputs[i]->outcfg.samplerates &&
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 f->inputs[i]->outcfg.channel_layouts))
424 return 0;
425 }
426
2/2
✓ Branch 0 taken 7838 times.
✓ Branch 1 taken 44 times.
7882 for (i = 0; i < f->nb_outputs; i++) {
427
2/2
✓ Branch 0 taken 7804 times.
✓ Branch 1 taken 34 times.
7838 if (!f->outputs[i]->incfg.formats)
428 7804 return 0;
429
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 3 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_VIDEO &&
430
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !(f->outputs[i]->incfg.color_ranges &&
431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->outputs[i]->incfg.color_spaces))
432 return 0;
433
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
434
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !(f->outputs[i]->incfg.samplerates &&
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 f->outputs[i]->incfg.channel_layouts))
436 return 0;
437 }
438 44 return 1;
439 }
440
441 /**
442 * Perform one round of query_formats() and merging formats lists on the
443 * filter graph.
444 * @return >=0 if all links formats lists could be queried and merged;
445 * AVERROR(EAGAIN) some progress was made in the queries or merging
446 * and a later call may succeed;
447 * AVERROR(EIO) (may be changed) plus a log message if no progress
448 * was made and the negotiation is stuck;
449 * a negative error code if some other error happened
450 */
451 7644 static int query_formats(AVFilterGraph *graph, void *log_ctx)
452 {
453 int i, j, ret;
454 7644 int converter_count = 0;
455 7644 int count_queried = 0; /* successful calls to query_formats() */
456 7644 int count_merged = 0; /* successful merge of formats lists */
457 7644 int count_already_merged = 0; /* lists already merged */
458 7644 int count_delayed = 0; /* lists that need to be merged later */
459
460
2/2
✓ Branch 0 taken 40114 times.
✓ Branch 1 taken 7644 times.
47758 for (i = 0; i < graph->nb_filters; i++) {
461 40114 AVFilterContext *f = graph->filters[i];
462
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 40070 times.
40114 if (formats_declared(f))
463 44 continue;
464 40070 ret = filter_query_formats(f);
465
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 40056 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
40070 if (ret < 0 && ret != AVERROR(EAGAIN))
466 return ret;
467 /* note: EAGAIN could indicate a partial success, not counted yet */
468 40070 count_queried += ret >= 0;
469 }
470
471 /* go through and merge as many format lists as possible */
472
2/2
✓ Branch 0 taken 41362 times.
✓ Branch 1 taken 7644 times.
49006 for (i = 0; i < graph->nb_filters; i++) {
473 41362 AVFilterContext *filter = graph->filters[i];
474
475
2/2
✓ Branch 0 taken 33685 times.
✓ Branch 1 taken 41362 times.
75047 for (j = 0; j < filter->nb_inputs; j++) {
476 33685 AVFilterLink *link = filter->inputs[j];
477 const AVFilterNegotiation *neg;
478 unsigned neg_step;
479 33685 int convert_needed = 0;
480
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33685 times.
33685 if (!link)
482 continue;
483
484 33685 neg = ff_filter_get_negotiation(link);
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33685 times.
33685 av_assert0(neg);
486
2/2
✓ Branch 0 taken 99807 times.
✓ Branch 1 taken 32437 times.
132244 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
487 99807 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
488 99807 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
489 99807 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
490
8/8
✓ Branch 0 taken 99765 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 99722 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 95906 times.
✓ Branch 5 taken 3816 times.
✓ Branch 7 taken 1248 times.
✓ Branch 8 taken 94658 times.
99807 if (a && b && a != b && !m->can_merge(a, b)) {
491 1248 convert_needed = 1;
492 1248 break;
493 }
494 }
495
2/2
✓ Branch 0 taken 101055 times.
✓ Branch 1 taken 33685 times.
134740 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
496 101055 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
497 101055 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
498 101055 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
499
4/4
✓ Branch 0 taken 101013 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 100970 times.
101055 if (!(a && b)) {
500 85 count_delayed++;
501
2/2
✓ Branch 0 taken 8968 times.
✓ Branch 1 taken 92002 times.
100970 } else if (a == b) {
502 8968 count_already_merged++;
503
2/2
✓ Branch 0 taken 88841 times.
✓ Branch 1 taken 3161 times.
92002 } else if (!convert_needed) {
504 88841 count_merged++;
505 88841 ret = m->merge(a, b);
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88841 times.
88841 if (ret < 0)
507 return ret;
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88841 times.
88841 if (!ret)
509 convert_needed = 1;
510 }
511 }
512
513
2/2
✓ Branch 0 taken 1248 times.
✓ Branch 1 taken 32437 times.
33685 if (convert_needed) {
514 AVFilterContext *convert;
515 const AVFilter *filter;
516 AVFilterLink *inlink, *outlink;
517 char inst_name[30];
518 const char *opts;
519
520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1248 times.
1248 if (fffiltergraph(graph)->disable_auto_convert) {
521 av_log(log_ctx, AV_LOG_ERROR,
522 "The filters '%s' and '%s' do not have a common format "
523 "and automatic conversion is disabled.\n",
524 link->src->name, link->dst->name);
525 return AVERROR(EINVAL);
526 }
527
528 /* couldn't merge format lists. auto-insert conversion filter */
529
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1248 times.
1248 if (!(filter = avfilter_get_by_name(neg->conversion_filter))) {
530 av_log(log_ctx, AV_LOG_ERROR,
531 "'%s' filter not present, cannot convert formats.\n",
532 neg->conversion_filter);
533 return AVERROR(EINVAL);
534 }
535 1248 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
536 1248 neg->conversion_filter, converter_count++);
537 1248 opts = FF_FIELD_AT(char *, neg->conversion_opts_offset, *graph);
538 1248 ret = avfilter_graph_create_filter(&convert, filter, inst_name, opts, NULL, graph);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1248 times.
1248 if (ret < 0)
540 return ret;
541
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1248 times.
1248 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
542 return ret;
543
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1248 times.
1248 if ((ret = filter_query_formats(convert)) < 0)
545 return ret;
546
547 1248 inlink = convert->inputs[0];
548 1248 outlink = convert->outputs[0];
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1248 times.
1248 av_assert0( inlink->incfg.formats->refcount > 0);
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1248 times.
1248 av_assert0( inlink->outcfg.formats->refcount > 0);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1248 times.
1248 av_assert0(outlink->incfg.formats->refcount > 0);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1248 times.
1248 av_assert0(outlink->outcfg.formats->refcount > 0);
553
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 632 times.
1248 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
562
1/2
✓ Branch 0 taken 632 times.
✗ Branch 1 not taken.
632 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink-> incfg.samplerates->refcount > 0);
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink->outcfg.samplerates->refcount > 0);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink-> incfg.samplerates->refcount > 0);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink->outcfg.samplerates->refcount > 0);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 632 times.
632 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
571 }
572 #define MERGE(merger, link) \
573 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
574 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
575
2/2
✓ Branch 0 taken 3744 times.
✓ Branch 1 taken 1248 times.
4992 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
576 3744 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
577
2/4
✓ Branch 1 taken 3744 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3744 times.
7488 if ((ret = MERGE(m, inlink)) <= 0 ||
578 3744 (ret = MERGE(m, outlink)) <= 0) {
579 if (ret < 0)
580 return ret;
581 av_log(log_ctx, AV_LOG_ERROR,
582 "Impossible to convert between the formats supported by the filter "
583 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
584 return AVERROR(ENOSYS);
585 }
586 }
587 }
588 }
589 }
590
591 7644 av_log(graph, AV_LOG_DEBUG, "query_formats: "
592 "%d queried, %d merged, %d already done, %d delayed\n",
593 count_queried, count_merged, count_already_merged, count_delayed);
594
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7630 times.
7644 if (count_delayed) {
595 AVBPrint bp;
596
597 /* if count_queried > 0, one filter at least did set its formats,
598 that will give additional information to its neighbour;
599 if count_merged > 0, one pair of formats lists at least was merged,
600 that will give additional information to all connected filters;
601 in both cases, progress was made and a new round must be done */
602
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14 if (count_queried || count_merged)
603 14 return AVERROR(EAGAIN);
604 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
605 for (i = 0; i < graph->nb_filters; i++)
606 if (!formats_declared(graph->filters[i]))
607 av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
608 graph->filters[i]->name);
609 av_log(graph, AV_LOG_ERROR,
610 "The following filters could not choose their formats: %s\n"
611 "Consider inserting the (a)format filter near their input or "
612 "output.\n", bp.str);
613 return AVERROR(EIO);
614 }
615 7630 return 0;
616 }
617
618 1118 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
619 {
620 1118 int score = 0;
621
622
2/2
✓ Branch 2 taken 1024 times.
✓ Branch 3 taken 94 times.
1118 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
623 1024 score ++;
624
625
2/2
✓ Branch 2 taken 252 times.
✓ Branch 3 taken 866 times.
1118 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
626 252 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
627 }else
628 866 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
629
630
3/4
✓ Branch 1 taken 138 times.
✓ Branch 2 taken 980 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 138 times.
1256 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
631 138 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
632 score += 20;
633
634
3/4
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 1024 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 94 times.
1212 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
635 94 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
636 score += 2;
637
638 1118 return score;
639 }
640
641 559 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
642 enum AVSampleFormat src_fmt)
643 {
644 int score1, score2;
645
646 559 score1 = get_fmt_score(dst_fmt1, src_fmt);
647 559 score2 = get_fmt_score(dst_fmt2, src_fmt);
648
649
2/2
✓ Branch 0 taken 420 times.
✓ Branch 1 taken 139 times.
559 return score1 < score2 ? dst_fmt1 : dst_fmt2;
650 }
651
652 33309 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
653 {
654 33309 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33309 times.
33309 if (!desc)
656 return 0;
657
2/2
✓ Branch 0 taken 1701 times.
✓ Branch 1 taken 31608 times.
33309 if (desc->nb_components < 3)
658 1701 return 0; /* Grayscale is explicitly full-range in swscale */
659 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
660 31608 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
661 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
662 }
663
664
665 23289 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
666 {
667
2/2
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 22574 times.
23289 switch (fmt) {
668 715 case AV_PIX_FMT_YUVJ420P:
669 case AV_PIX_FMT_YUVJ422P:
670 case AV_PIX_FMT_YUVJ444P:
671 case AV_PIX_FMT_YUVJ440P:
672 case AV_PIX_FMT_YUVJ411P:
673 715 return 1;
674 22574 default:
675 22574 return 0;
676 }
677 }
678
679 100863 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
680 {
681
3/4
✓ Branch 0 taken 100863 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67226 times.
✓ Branch 3 taken 33637 times.
100863 if (!link || !link->incfg.formats)
682 67226 return 0;
683
684
2/2
✓ Branch 0 taken 27740 times.
✓ Branch 1 taken 5897 times.
33637 if (link->type == AVMEDIA_TYPE_VIDEO) {
685
3/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 27643 times.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
27740 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
686 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
687 97 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
688 97 enum AVPixelFormat best= AV_PIX_FMT_NONE;
689 int i;
690
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 97 times.
1116 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
691 1019 enum AVPixelFormat p = link->incfg.formats->formats[i];
692 1019 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
693 }
694 97 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
695 97 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
696 97 av_get_pix_fmt_name(ref->format), has_alpha);
697 97 link->incfg.formats->formats[0] = best;
698 }
699
1/2
✓ Branch 0 taken 5897 times.
✗ Branch 1 not taken.
5897 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
700
3/4
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 5758 times.
✓ Branch 2 taken 139 times.
✗ Branch 3 not taken.
5897 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
701 139 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
702 int i;
703
2/2
✓ Branch 0 taken 559 times.
✓ Branch 1 taken 139 times.
698 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
704 559 enum AVSampleFormat p = link->incfg.formats->formats[i];
705 559 best = find_best_sample_fmt_of_2(best, p, ref->format);
706 }
707 139 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
708 139 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
709 139 av_get_sample_fmt_name(ref->format));
710 139 link->incfg.formats->formats[0] = best;
711 }
712 }
713
714 33637 link->incfg.formats->nb_formats = 1;
715 33637 link->format = link->incfg.formats->formats[0];
716
717
2/2
✓ Branch 0 taken 27740 times.
✓ Branch 1 taken 5897 times.
33637 if (link->type == AVMEDIA_TYPE_VIDEO) {
718 27740 enum AVPixelFormat swfmt = link->format;
719
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27740 times.
27740 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
720 // FIXME: this is a hack - we'd like to use the sw_format of
721 // link->hw_frames_ctx here, but it is not yet available.
722 // To make this work properly we will need to either reorder
723 // things so that it is available here or somehow negotiate
724 // sw_format separately.
725 swfmt = AV_PIX_FMT_YUV420P;
726 }
727
728
2/2
✓ Branch 1 taken 9320 times.
✓ Branch 2 taken 18420 times.
27740 if (!ff_fmt_is_regular_yuv(swfmt)) {
729 9320 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
730 /* These fields are explicitly documented as affecting YUV only,
731 * so set them to sane values for other formats. */
732
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 8749 times.
9320 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
733 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
734 else
735 8749 link->color_range = AVCOL_RANGE_JPEG;
736
2/2
✓ Branch 0 taken 7810 times.
✓ Branch 1 taken 1510 times.
9320 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
737 7810 link->colorspace = AVCOL_SPC_RGB;
738 } else {
739 1510 link->colorspace = AVCOL_SPC_UNSPECIFIED;
740 }
741 } else {
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18420 times.
18420 if (!link->incfg.color_spaces->nb_formats) {
743 av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
744 " the link between filters %s and %s.\n", link->src->name,
745 link->dst->name);
746 return AVERROR(EINVAL);
747 }
748 18420 link->incfg.color_spaces->nb_formats = 1;
749 18420 link->colorspace = link->incfg.color_spaces->formats[0];
750
751
2/2
✓ Branch 1 taken 646 times.
✓ Branch 2 taken 17774 times.
18420 if (ff_fmt_is_forced_full_range(swfmt)) {
752 646 link->color_range = AVCOL_RANGE_JPEG;
753 } else {
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17774 times.
17774 if (!link->incfg.color_ranges->nb_formats) {
755 av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
756 " the link between filters %s and %s.\n", link->src->name,
757 link->dst->name);
758 return AVERROR(EINVAL);
759 }
760 17774 link->incfg.color_ranges->nb_formats = 1;
761 17774 link->color_range = link->incfg.color_ranges->formats[0];
762 }
763 }
764
1/2
✓ Branch 0 taken 5897 times.
✗ Branch 1 not taken.
5897 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
765 int ret;
766
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5897 times.
5897 if (!link->incfg.samplerates->nb_formats) {
768 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
769 " the link between filters %s and %s.\n", link->src->name,
770 link->dst->name);
771 return AVERROR(EINVAL);
772 }
773 5897 link->incfg.samplerates->nb_formats = 1;
774 5897 link->sample_rate = link->incfg.samplerates->formats[0];
775
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5897 times.
5897 if (link->incfg.channel_layouts->all_layouts) {
777 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
778 " the link between filters %s and %s.\n", link->src->name,
779 link->dst->name);
780 if (!link->incfg.channel_layouts->all_counts)
781 av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
782 "supported, try specifying a channel layout using "
783 "'aformat=channel_layouts=something'.\n");
784 return AVERROR(EINVAL);
785 }
786 5897 link->incfg.channel_layouts->nb_channel_layouts = 1;
787 5897 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5897 times.
5897 if (ret < 0)
789 return ret;
790 }
791
792 33637 ff_formats_unref(&link->incfg.formats);
793 33637 ff_formats_unref(&link->outcfg.formats);
794 33637 ff_formats_unref(&link->incfg.samplerates);
795 33637 ff_formats_unref(&link->outcfg.samplerates);
796 33637 ff_channel_layouts_unref(&link->incfg.channel_layouts);
797 33637 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
798 33637 ff_formats_unref(&link->incfg.color_spaces);
799 33637 ff_formats_unref(&link->outcfg.color_spaces);
800 33637 ff_formats_unref(&link->incfg.color_ranges);
801 33637 ff_formats_unref(&link->outcfg.color_ranges);
802
803 33637 return 0;
804 }
805
806 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
807 do { \
808 for (i = 0; i < filter->nb_inputs; i++) { \
809 AVFilterLink *link = filter->inputs[i]; \
810 fmt_type fmt; \
811 \
812 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
813 continue; \
814 fmt = link->outcfg.list->var[0]; \
815 \
816 for (j = 0; j < filter->nb_outputs; j++) { \
817 AVFilterLink *out_link = filter->outputs[j]; \
818 list_type *fmts; \
819 \
820 if (link->type != out_link->type || \
821 out_link->incfg.list->nb == 1) \
822 continue; \
823 fmts = out_link->incfg.list; \
824 \
825 if (!out_link->incfg.list->nb) { \
826 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
827 return ret; \
828 ret = 1; \
829 break; \
830 } \
831 \
832 for (k = 0; k < out_link->incfg.list->nb; k++) \
833 if (fmts->var[k] == fmt) { \
834 fmts->var[0] = fmt; \
835 fmts->nb = 1; \
836 ret = 1; \
837 break; \
838 } \
839 } \
840 } \
841 } while (0)
842
843 67363 static int reduce_formats_on_filter(AVFilterContext *filter)
844 {
845 67363 int i, j, k, ret = 0;
846
847
16/20
✓ Branch 0 taken 55217 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1355 times.
✓ Branch 3 taken 53862 times.
✓ Branch 4 taken 41937 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 41391 times.
✓ Branch 7 taken 546 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 546 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 51 times.
✓ Branch 14 taken 4628 times.
✓ Branch 15 taken 4679 times.
✓ Branch 16 taken 495 times.
✓ Branch 17 taken 41949 times.
✓ Branch 18 taken 53862 times.
✓ Branch 19 taken 55217 times.
✓ Branch 20 taken 67363 times.
169157 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
848 nb_formats, ff_add_format);
849
18/20
✓ Branch 0 taken 10687 times.
✓ Branch 1 taken 44530 times.
✓ Branch 2 taken 1177 times.
✓ Branch 3 taken 9510 times.
✓ Branch 4 taken 7710 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6946 times.
✓ Branch 7 taken 764 times.
✓ Branch 8 taken 728 times.
✓ Branch 9 taken 36 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 728 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 71 times.
✓ Branch 15 taken 107 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 7716 times.
✓ Branch 18 taken 8782 times.
✓ Branch 19 taken 55217 times.
✓ Branch 20 taken 67363 times.
129639 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
850 nb_formats, ff_add_format);
851
15/20
✓ Branch 0 taken 44530 times.
✓ Branch 1 taken 10687 times.
✓ Branch 2 taken 8917 times.
✓ Branch 3 taken 35613 times.
✓ Branch 4 taken 27912 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24144 times.
✓ Branch 7 taken 3768 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3768 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3768 times.
✓ Branch 14 taken 404 times.
✓ Branch 15 taken 4172 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 27912 times.
✓ Branch 18 taken 35613 times.
✓ Branch 19 taken 55217 times.
✓ Branch 20 taken 67363 times.
150896 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
852 nb_formats, ff_add_format);
853
15/20
✓ Branch 0 taken 44530 times.
✓ Branch 1 taken 10687 times.
✓ Branch 2 taken 7261 times.
✓ Branch 3 taken 37269 times.
✓ Branch 4 taken 28609 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25438 times.
✓ Branch 7 taken 3171 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3171 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3171 times.
✓ Branch 14 taken 3151 times.
✓ Branch 15 taken 6322 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 28609 times.
✓ Branch 18 taken 37269 times.
✓ Branch 19 taken 55217 times.
✓ Branch 20 taken 67363 times.
154340 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
854 nb_formats, ff_add_format);
855
856 /* reduce channel layouts */
857
2/2
✓ Branch 0 taken 55217 times.
✓ Branch 1 taken 67363 times.
122580 for (i = 0; i < filter->nb_inputs; i++) {
858 55217 AVFilterLink *inlink = filter->inputs[i];
859 const AVChannelLayout *fmt;
860
861
2/2
✓ Branch 0 taken 10687 times.
✓ Branch 1 taken 44530 times.
55217 if (!inlink->outcfg.channel_layouts ||
862
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 10614 times.
10687 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
863 44603 continue;
864 10614 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
865
866
2/2
✓ Branch 0 taken 8433 times.
✓ Branch 1 taken 9775 times.
18208 for (j = 0; j < filter->nb_outputs; j++) {
867 8433 AVFilterLink *outlink = filter->outputs[j];
868 AVFilterChannelLayouts *fmts;
869
870 8433 fmts = outlink->incfg.channel_layouts;
871
4/4
✓ Branch 0 taken 8427 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7549 times.
✓ Branch 3 taken 878 times.
8433 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
872 7555 continue;
873
874
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 39 times.
878 if (fmts->all_layouts &&
875
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 832 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
839 (KNOWN(fmt) || fmts->all_counts)) {
876 /* Turn the infinite list into a singleton */
877 839 fmts->all_layouts = fmts->all_counts = 0;
878 839 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 839 times.
839 if (ret < 0)
880 return ret;
881 839 ret = 1;
882 839 break;
883 }
884
885
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 4 times.
94 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
886
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 55 times.
90 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
887 35 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0)
889 return ret;
890 35 fmts->nb_channel_layouts = 1;
891 35 ret = 1;
892 35 break;
893 }
894 }
895 }
896 }
897
898 67363 return ret;
899 }
900
901 7630 static int reduce_formats(AVFilterGraph *graph)
902 {
903 int i, reduced, ret;
904
905 do {
906 12076 reduced = 0;
907
908
2/2
✓ Branch 0 taken 67363 times.
✓ Branch 1 taken 12076 times.
79439 for (i = 0; i < graph->nb_filters; i++) {
909
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 67363 times.
67363 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
910 return ret;
911 67363 reduced |= ret;
912 }
913
2/2
✓ Branch 0 taken 4446 times.
✓ Branch 1 taken 7630 times.
12076 } while (reduced);
914
915 7630 return 0;
916 }
917
918 41304 static void swap_samplerates_on_filter(AVFilterContext *filter)
919 {
920 41304 AVFilterLink *link = NULL;
921 int sample_rate;
922 int i, j;
923
924
2/2
✓ Branch 0 taken 33554 times.
✓ Branch 1 taken 35486 times.
69040 for (i = 0; i < filter->nb_inputs; i++) {
925 33554 link = filter->inputs[i];
926
927
2/2
✓ Branch 0 taken 5818 times.
✓ Branch 1 taken 27736 times.
33554 if (link->type == AVMEDIA_TYPE_AUDIO &&
928
1/2
✓ Branch 0 taken 5818 times.
✗ Branch 1 not taken.
5818 link->outcfg.samplerates->nb_formats== 1)
929 5818 break;
930 }
931
2/2
✓ Branch 0 taken 35486 times.
✓ Branch 1 taken 5818 times.
41304 if (i == filter->nb_inputs)
932 35486 return;
933
934 5818 sample_rate = link->outcfg.samplerates->formats[0];
935
936
2/2
✓ Branch 0 taken 4538 times.
✓ Branch 1 taken 5818 times.
10356 for (i = 0; i < filter->nb_outputs; i++) {
937 4538 AVFilterLink *outlink = filter->outputs[i];
938 4538 int best_idx, best_diff = INT_MAX;
939
940
2/2
✓ Branch 0 taken 4536 times.
✓ Branch 1 taken 2 times.
4538 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
941
1/2
✓ Branch 0 taken 4536 times.
✗ Branch 1 not taken.
4536 outlink->incfg.samplerates->nb_formats < 2)
942 4538 continue;
943
944 for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
945 int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
946
947 av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
948
949 if (diff < best_diff) {
950 best_diff = diff;
951 best_idx = j;
952 }
953 }
954 FFSWAP(int, outlink->incfg.samplerates->formats[0],
955 outlink->incfg.samplerates->formats[best_idx]);
956 }
957 }
958
959 7630 static void swap_samplerates(AVFilterGraph *graph)
960 {
961 int i;
962
963
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++)
964 41304 swap_samplerates_on_filter(graph->filters[i]);
965 7630 }
966
967 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
968 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
969 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
970 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
971 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
972 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
973 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
974
975 /* allowable substitutions for channel pairs when comparing layouts,
976 * ordered by priority for both values */
977 static const uint64_t ch_subst[][2] = {
978 { CH_FRONT_PAIR, CH_CENTER_PAIR },
979 { CH_FRONT_PAIR, CH_WIDE_PAIR },
980 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
981 { CH_CENTER_PAIR, CH_FRONT_PAIR },
982 { CH_CENTER_PAIR, CH_WIDE_PAIR },
983 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
984 { CH_WIDE_PAIR, CH_FRONT_PAIR },
985 { CH_WIDE_PAIR, CH_CENTER_PAIR },
986 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
987 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
988 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
989 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
990 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
991 { CH_SIDE_PAIR, CH_BACK_PAIR },
992 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
993 { CH_BACK_PAIR, CH_DIRECT_PAIR },
994 { CH_BACK_PAIR, CH_SIDE_PAIR },
995 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
996 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
997 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
998 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
999 };
1000
1001 41304 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1002 {
1003 41304 AVFilterLink *link = NULL;
1004 int i, j, k;
1005
1006
2/2
✓ Branch 0 taken 33554 times.
✓ Branch 1 taken 35490 times.
69044 for (i = 0; i < filter->nb_inputs; i++) {
1007 33554 link = filter->inputs[i];
1008
1009
2/2
✓ Branch 0 taken 5818 times.
✓ Branch 1 taken 27736 times.
33554 if (link->type == AVMEDIA_TYPE_AUDIO &&
1010
2/2
✓ Branch 0 taken 5814 times.
✓ Branch 1 taken 4 times.
5818 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1011 5814 break;
1012 }
1013
2/2
✓ Branch 0 taken 35490 times.
✓ Branch 1 taken 5814 times.
41304 if (i == filter->nb_inputs)
1014 35490 return;
1015
1016
2/2
✓ Branch 0 taken 4536 times.
✓ Branch 1 taken 5814 times.
10350 for (i = 0; i < filter->nb_outputs; i++) {
1017 4536 AVFilterLink *outlink = filter->outputs[i];
1018 4536 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1019
1020
2/2
✓ Branch 0 taken 4534 times.
✓ Branch 1 taken 2 times.
4536 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1021
2/2
✓ Branch 0 taken 4532 times.
✓ Branch 1 taken 2 times.
4534 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1022 4534 continue;
1023
1024
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (j = 0; j < outlink->incfg.channel_layouts->nb_channel_layouts; j++) {
1025 18 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1026 int in_channels;
1027 int out_channels;
1028 int count_diff;
1029 int matched_channels, extra_channels;
1030 18 int score = 100000;
1031
1032 18 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1033 18 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1034 18 in_channels = in_chlayout.nb_channels;
1035 18 out_channels = out_chlayout.nb_channels;
1036 18 count_diff = out_channels - in_channels;
1037
4/8
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
18 if (!KNOWN(&in_chlayout) || !KNOWN(&out_chlayout)) {
1038 /* Compute score in case the input or output layout encodes
1039 a channel count; in this case the score is not altered by
1040 the computation afterwards, as in_chlayout and
1041 out_chlayout have both been set to 0 */
1042
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))
1043
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1044
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1045 out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1046 32 score -= 10000 + FFABS(out_channels - in_channels) +
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1048 16 av_channel_layout_uninit(&in_chlayout);
1049 16 av_channel_layout_uninit(&out_chlayout);
1050 /* Let the remaining computation run, even if the score
1051 value is not altered */
1052 }
1053
1054 /* channel substitution */
1055
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 18 times.
396 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1056 378 uint64_t cmp0 = ch_subst[k][0];
1057 378 uint64_t cmp1 = ch_subst[k][1];
1058
4/4
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 354 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 6 times.
402 if ( av_channel_layout_subset(& in_chlayout, cmp0) &&
1059
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 16 times.
42 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1060
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
20 av_channel_layout_subset(&out_chlayout, cmp1) &&
1061 2 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1062 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1063 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1064 /* add score for channel match, minus a deduction for
1065 having to do the substitution */
1066 score += 10 * av_popcount64(cmp1) - 2;
1067 }
1068 }
1069
1070 /* no penalty for LFE channel mismatch */
1071
3/4
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
20 if (av_channel_layout_channel_from_index(&in_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0 &&
1072 2 av_channel_layout_channel_from_index(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1073 score += 10;
1074 18 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1075 18 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1076
1077 18 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1078 18 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1079 18 score += 10 * matched_channels - 5 * extra_channels;
1080
1081
3/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 15 times.
18 if (score > best_score ||
1082 (count_diff < best_count_diff && score == best_score)) {
1083 3 best_score = score;
1084 3 best_idx = j;
1085 3 best_count_diff = count_diff;
1086 }
1087 }
1088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(best_idx >= 0);
1089 2 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1090 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1091 }
1092
1093 }
1094
1095 7630 static void swap_channel_layouts(AVFilterGraph *graph)
1096 {
1097 int i;
1098
1099
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++)
1100 41304 swap_channel_layouts_on_filter(graph->filters[i]);
1101 7630 }
1102
1103 41304 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1104 {
1105 41304 AVFilterLink *link = NULL;
1106 int format, bps;
1107 int i, j;
1108
1109
2/2
✓ Branch 0 taken 33623 times.
✓ Branch 1 taken 35875 times.
69498 for (i = 0; i < filter->nb_inputs; i++) {
1110 33623 link = filter->inputs[i];
1111
1112
2/2
✓ Branch 0 taken 5887 times.
✓ Branch 1 taken 27736 times.
33623 if (link->type == AVMEDIA_TYPE_AUDIO &&
1113
2/2
✓ Branch 0 taken 5429 times.
✓ Branch 1 taken 458 times.
5887 link->outcfg.formats->nb_formats == 1)
1114 5429 break;
1115 }
1116
2/2
✓ Branch 0 taken 35875 times.
✓ Branch 1 taken 5429 times.
41304 if (i == filter->nb_inputs)
1117 35875 return;
1118
1119 5429 format = link->outcfg.formats->formats[0];
1120 5429 bps = av_get_bytes_per_sample(format);
1121
1122
2/2
✓ Branch 0 taken 4159 times.
✓ Branch 1 taken 5429 times.
9588 for (i = 0; i < filter->nb_outputs; i++) {
1123 4159 AVFilterLink *outlink = filter->outputs[i];
1124 4159 int best_idx = -1, best_score = INT_MIN;
1125
1126
2/2
✓ Branch 0 taken 4157 times.
✓ Branch 1 taken 2 times.
4159 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1127
2/2
✓ Branch 0 taken 4000 times.
✓ Branch 1 taken 157 times.
4157 outlink->incfg.formats->nb_formats < 2)
1128 4002 continue;
1129
1130
2/2
✓ Branch 0 taken 291 times.
✓ Branch 1 taken 7 times.
298 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1131 291 int out_format = outlink->incfg.formats->formats[j];
1132 291 int out_bps = av_get_bytes_per_sample(out_format);
1133 int score;
1134
1135
4/4
✓ Branch 1 taken 155 times.
✓ Branch 2 taken 136 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 141 times.
446 if (av_get_packed_sample_fmt(out_format) == format ||
1136 155 av_get_planar_sample_fmt(out_format) == format) {
1137 150 best_idx = j;
1138 150 break;
1139 }
1140
1141 /* for s32 and float prefer double to prevent loss of information */
1142
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 137 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
141 if (bps == 4 && out_bps == 8) {
1143 best_idx = j;
1144 break;
1145 }
1146
1147 /* prefer closest higher or equal bps */
1148 141 score = -abs(out_bps - bps);
1149
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 113 times.
141 if (out_bps >= bps)
1150 28 score += INT_MAX/2;
1151
1152
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 33 times.
141 if (score > best_score) {
1153 108 best_score = score;
1154 108 best_idx = j;
1155 }
1156 }
1157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 av_assert0(best_idx >= 0);
1158 157 FFSWAP(int, outlink->incfg.formats->formats[0],
1159 outlink->incfg.formats->formats[best_idx]);
1160 }
1161 }
1162
1163 7630 static void swap_sample_fmts(AVFilterGraph *graph)
1164 {
1165 int i;
1166
1167
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++)
1168 41304 swap_sample_fmts_on_filter(graph->filters[i]);
1169
1170 7630 }
1171
1172 7630 static int pick_formats(AVFilterGraph *graph)
1173 {
1174 int i, j, ret;
1175 int change;
1176
1177 do{
1178 15304 change = 0;
1179
2/2
✓ Branch 0 taken 83556 times.
✓ Branch 1 taken 15304 times.
98860 for (i = 0; i < graph->nb_filters; i++) {
1180 83556 AVFilterContext *filter = graph->filters[i];
1181
2/2
✓ Branch 0 taken 67792 times.
✓ Branch 1 taken 15764 times.
83556 if (filter->nb_inputs){
1182
2/2
✓ Branch 0 taken 68141 times.
✓ Branch 1 taken 67792 times.
135933 for (j = 0; j < filter->nb_inputs; j++){
1183
4/4
✓ Branch 0 taken 14948 times.
✓ Branch 1 taken 53193 times.
✓ Branch 2 taken 14458 times.
✓ Branch 3 taken 490 times.
68141 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14458 times.
14458 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1185 return ret;
1186 14458 change = 1;
1187 }
1188 }
1189 }
1190
2/2
✓ Branch 0 taken 67880 times.
✓ Branch 1 taken 15676 times.
83556 if (filter->nb_outputs){
1191
2/2
✓ Branch 0 taken 68141 times.
✓ Branch 1 taken 67880 times.
136021 for (j = 0; j < filter->nb_outputs; j++){
1192
4/4
✓ Branch 0 taken 19488 times.
✓ Branch 1 taken 48653 times.
✓ Branch 2 taken 18895 times.
✓ Branch 3 taken 593 times.
68141 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18895 times.
18895 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1194 return ret;
1195 18895 change = 1;
1196 }
1197 }
1198 }
1199
6/6
✓ Branch 0 taken 67792 times.
✓ Branch 1 taken 15764 times.
✓ Branch 2 taken 52116 times.
✓ Branch 3 taken 15676 times.
✓ Branch 4 taken 51847 times.
✓ Branch 5 taken 269 times.
83556 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1200
2/2
✓ Branch 0 taken 52041 times.
✓ Branch 1 taken 51847 times.
103888 for (j = 0; j < filter->nb_outputs; j++) {
1201
2/2
✓ Branch 0 taken 236 times.
✓ Branch 1 taken 51805 times.
52041 if (filter->outputs[j]->format<0) {
1202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 236 times.
236 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1203 return ret;
1204 236 change = 1;
1205 }
1206 }
1207 }
1208 }
1209
2/2
✓ Branch 0 taken 7674 times.
✓ Branch 1 taken 7630 times.
15304 }while(change);
1210
1211
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++) {
1212 41304 AVFilterContext *filter = graph->filters[i];
1213
1214
2/2
✓ Branch 0 taken 33637 times.
✓ Branch 1 taken 41304 times.
74941 for (j = 0; j < filter->nb_inputs; j++)
1215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33637 times.
33637 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1216 return ret;
1217
2/2
✓ Branch 0 taken 33637 times.
✓ Branch 1 taken 41304 times.
74941 for (j = 0; j < filter->nb_outputs; j++)
1218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33637 times.
33637 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1219 return ret;
1220 }
1221 7630 return 0;
1222 }
1223
1224 /**
1225 * Configure the formats of all the links in the graph.
1226 */
1227 7630 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1228 {
1229 int ret;
1230
1231 /* find supported formats from sub-filters, and merge along links */
1232
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 7630 times.
7644 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1233 14 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7630 times.
7630 if (ret < 0)
1235 return ret;
1236
1237 /* Once everything is merged, it's possible that we'll still have
1238 * multiple valid media format choices. We try to minimize the amount
1239 * of format conversion inside filters */
1240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = reduce_formats(graph)) < 0)
1241 return ret;
1242
1243 /* for audio filters, ensure the best format, sample rate and channel layout
1244 * is selected */
1245 7630 swap_sample_fmts(graph);
1246 7630 swap_samplerates(graph);
1247 7630 swap_channel_layouts(graph);
1248
1249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = pick_formats(graph)) < 0)
1250 return ret;
1251
1252 7630 return 0;
1253 }
1254
1255 7630 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1256 {
1257 unsigned i, j;
1258 7630 int sink_links_count = 0, n = 0;
1259 AVFilterContext *f;
1260 FilterLinkInternal **sinks;
1261
1262
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++) {
1263 41304 f = graph->filters[i];
1264
2/2
✓ Branch 0 taken 33637 times.
✓ Branch 1 taken 41304 times.
74941 for (j = 0; j < f->nb_inputs; j++) {
1265 33637 ff_link_internal(f->inputs[j])->age_index = -1;
1266 }
1267
2/2
✓ Branch 0 taken 33637 times.
✓ Branch 1 taken 41304 times.
74941 for (j = 0; j < f->nb_outputs; j++) {
1268 33637 ff_link_internal(f->outputs[j])->age_index = -1;
1269 }
1270
2/2
✓ Branch 0 taken 7762 times.
✓ Branch 1 taken 33542 times.
41304 if (!f->nb_outputs) {
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7762 times.
7762 if (f->nb_inputs > INT_MAX - sink_links_count)
1272 return AVERROR(EINVAL);
1273 7762 sink_links_count += f->nb_inputs;
1274 }
1275 }
1276 7630 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7630 times.
7630 if (!sinks)
1278 return AVERROR(ENOMEM);
1279
2/2
✓ Branch 0 taken 41304 times.
✓ Branch 1 taken 7630 times.
48934 for (i = 0; i < graph->nb_filters; i++) {
1280 41304 f = graph->filters[i];
1281
2/2
✓ Branch 0 taken 7762 times.
✓ Branch 1 taken 33542 times.
41304 if (!f->nb_outputs) {
1282
2/2
✓ Branch 0 taken 7762 times.
✓ Branch 1 taken 7762 times.
15524 for (j = 0; j < f->nb_inputs; j++) {
1283 7762 sinks[n] = ff_link_internal(f->inputs[j]);
1284 7762 sinks[n]->age_index = n;
1285 7762 n++;
1286 }
1287 }
1288 }
1289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7630 times.
7630 av_assert0(n == sink_links_count);
1290 7630 fffiltergraph(graph)->sink_links = sinks;
1291 7630 fffiltergraph(graph)->sink_links_count = sink_links_count;
1292 7630 return 0;
1293 }
1294
1295 7630 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1296 {
1297 int ret;
1298
1299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = graph_check_validity(graphctx, log_ctx)))
1300 return ret;
1301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = graph_config_formats(graphctx, log_ctx)))
1302 return ret;
1303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = graph_config_links(graphctx, log_ctx)))
1304 return ret;
1305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = graph_check_links(graphctx, log_ctx)))
1306 return ret;
1307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7630 times.
7630 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1308 return ret;
1309
1310 7630 return 0;
1311 }
1312
1313 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1314 {
1315 int i, r = AVERROR(ENOSYS);
1316
1317 if (!graph)
1318 return r;
1319
1320 if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1321 r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1322 if (r != AVERROR(ENOSYS))
1323 return r;
1324 }
1325
1326 if (res_len && res)
1327 res[0] = 0;
1328
1329 for (i = 0; i < graph->nb_filters; i++) {
1330 AVFilterContext *filter = graph->filters[i];
1331 if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1332 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1333 if (r != AVERROR(ENOSYS)) {
1334 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1335 return r;
1336 }
1337 }
1338 }
1339
1340 return r;
1341 }
1342
1343 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1344 {
1345 int i;
1346
1347 if(!graph)
1348 return 0;
1349
1350 for (i = 0; i < graph->nb_filters; i++) {
1351 AVFilterContext *filter = graph->filters[i];
1352 FFFilterContext *ctxi = fffilterctx(filter);
1353 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1354 AVFilterCommand **queue = &ctxi->command_queue, *next;
1355 while (*queue && (*queue)->time <= ts)
1356 queue = &(*queue)->next;
1357 next = *queue;
1358 *queue = av_mallocz(sizeof(AVFilterCommand));
1359 if (!*queue)
1360 return AVERROR(ENOMEM);
1361
1362 (*queue)->command = av_strdup(command);
1363 (*queue)->arg = av_strdup(arg);
1364 (*queue)->time = ts;
1365 (*queue)->flags = flags;
1366 (*queue)->next = next;
1367 if(flags & AVFILTER_CMD_FLAG_ONE)
1368 return 0;
1369 }
1370 }
1371
1372 return 0;
1373 }
1374
1375 399972 static void heap_bubble_up(FFFilterGraph *graph,
1376 FilterLinkInternal *li, int index)
1377 {
1378 399972 FilterLinkInternal **links = graph->sink_links;
1379
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 399972 times.
399972 av_assert0(index >= 0);
1381
1382
2/2
✓ Branch 0 taken 946 times.
✓ Branch 1 taken 399932 times.
400878 while (index) {
1383 946 int parent = (index - 1) >> 1;
1384
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 906 times.
946 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1385 40 break;
1386 906 links[index] = links[parent];
1387 906 links[index]->age_index = index;
1388 906 index = parent;
1389 }
1390 399972 links[index] = li;
1391 399972 li->age_index = index;
1392 399972 }
1393
1394 400075 static void heap_bubble_down(FFFilterGraph *graph,
1395 FilterLinkInternal *li, int index)
1396 {
1397 400075 FilterLinkInternal **links = graph->sink_links;
1398
1399
1/2
✓ Branch 0 taken 400075 times.
✗ Branch 1 not taken.
400075 av_assert0(index >= 0);
1400
1401 4549 while (1) {
1402 404624 int child = 2 * index + 1;
1403
2/2
✓ Branch 0 taken 399768 times.
✓ Branch 1 taken 4856 times.
404624 if (child >= graph->sink_links_count)
1404 399768 break;
1405
2/2
✓ Branch 0 taken 3510 times.
✓ Branch 1 taken 1346 times.
4856 if (child + 1 < graph->sink_links_count &&
1406
2/2
✓ Branch 0 taken 973 times.
✓ Branch 1 taken 2537 times.
3510 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1407 973 child++;
1408
2/2
✓ Branch 0 taken 307 times.
✓ Branch 1 taken 4549 times.
4856 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1409 307 break;
1410 4549 links[index] = links[child];
1411 4549 links[index]->age_index = index;
1412 4549 index = child;
1413 }
1414 400075 links[index] = li;
1415 400075 li->age_index = index;
1416 400075 }
1417
1418 399972 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1419 {
1420 399972 FFFilterGraph *graphi = fffiltergraph(graph);
1421
1422 399972 heap_bubble_up (graphi, li, li->age_index);
1423 399972 heap_bubble_down(graphi, li, li->age_index);
1424 399972 }
1425
1426 768762 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1427 {
1428 768762 FFFilterGraph *graphi = fffiltergraph(graph);
1429 768762 FilterLinkInternal *oldesti = graphi->sink_links[0];
1430 768762 AVFilterLink *oldest = &oldesti->l.pub;
1431 int64_t frame_count;
1432 int r;
1433
1434
2/2
✓ Branch 0 taken 768889 times.
✓ Branch 1 taken 4594 times.
773483 while (graphi->sink_links_count) {
1435 768889 oldesti = graphi->sink_links[0];
1436 768889 oldest = &oldesti->l.pub;
1437
1/2
✓ Branch 1 taken 768889 times.
✗ Branch 2 not taken.
768889 if (fffilter(oldest->dst->filter)->activate) {
1438 768889 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1439 AV_BUFFERSINK_FLAG_PEEK);
1440
2/2
✓ Branch 0 taken 764168 times.
✓ Branch 1 taken 4721 times.
768889 if (r != AVERROR_EOF)
1441 764168 return r;
1442 } else {
1443 r = ff_request_frame(oldest);
1444 }
1445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4721 times.
4721 if (r != AVERROR_EOF)
1446 break;
1447 4721 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1448 4721 oldest->dst->name,
1449 4721 oldest->dstpad->name);
1450 /* EOF: remove the link from the heap */
1451
2/2
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 4618 times.
4721 if (oldesti->age_index < --graphi->sink_links_count)
1452 103 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1453 oldesti->age_index);
1454 4721 oldesti->age_index = -1;
1455 }
1456
1/2
✓ Branch 0 taken 4594 times.
✗ Branch 1 not taken.
4594 if (!graphi->sink_links_count)
1457 4594 return AVERROR_EOF;
1458 av_assert1(!fffilter(oldest->dst->filter)->activate);
1459 av_assert1(oldesti->age_index >= 0);
1460 frame_count = oldesti->l.frame_count_out;
1461 while (frame_count == oldesti->l.frame_count_out) {
1462 r = ff_filter_graph_run_once(graph);
1463 if (r == AVERROR(EAGAIN) &&
1464 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1465 !oldesti->status_in)
1466 (void)ff_request_frame(oldest);
1467 else if (r < 0)
1468 return r;
1469 }
1470 return 0;
1471 }
1472
1473 4256129 int ff_filter_graph_run_once(AVFilterGraph *graph)
1474 {
1475 FFFilterContext *ctxi;
1476 unsigned i;
1477
1478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4256129 times.
4256129 av_assert0(graph->nb_filters);
1479 4256129 ctxi = fffilterctx(graph->filters[0]);
1480
2/2
✓ Branch 0 taken 17367420 times.
✓ Branch 1 taken 4256129 times.
21623549 for (i = 1; i < graph->nb_filters; i++) {
1481 17367420 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1482
1483
2/2
✓ Branch 0 taken 2498199 times.
✓ Branch 1 taken 14869221 times.
17367420 if (ctxi_other->ready > ctxi->ready)
1484 2498199 ctxi = ctxi_other;
1485 }
1486
1487
2/2
✓ Branch 0 taken 727562 times.
✓ Branch 1 taken 3528567 times.
4256129 if (!ctxi->ready)
1488 727562 return AVERROR(EAGAIN);
1489 3528567 return ff_filter_activate(&ctxi->p);
1490 }
1491