FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 611 790 77.3%
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 15229 AVFilterGraph *avfilter_graph_alloc(void)
84 {
85 15229 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
86 AVFilterGraph *ret;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15229 times.
15229 if (!graph)
89 return NULL;
90
91 15229 ret = &graph->p;
92 15229 ret->av_class = &filtergraph_class;
93 15229 av_opt_set_defaults(ret);
94 15229 ff_framequeue_global_init(&graph->frame_queues);
95
96 15229 return ret;
97 }
98
99 57912 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
100 {
101 int i, j;
102
1/2
✓ Branch 0 taken 57912 times.
✗ Branch 1 not taken.
57912 for (i = 0; i < graph->nb_filters; i++) {
103
1/2
✓ Branch 0 taken 57912 times.
✗ Branch 1 not taken.
57912 if (graph->filters[i] == filter) {
104 57912 FFSWAP(AVFilterContext*, graph->filters[i],
105 graph->filters[graph->nb_filters - 1]);
106 57912 graph->nb_filters--;
107 57912 filter->graph = NULL;
108
2/2
✓ Branch 0 taken 50353 times.
✓ Branch 1 taken 57912 times.
108265 for (j = 0; j<filter->nb_outputs; j++)
109
2/2
✓ Branch 0 taken 19379 times.
✓ Branch 1 taken 30974 times.
50353 if (filter->outputs[j])
110 19379 ff_filter_link(filter->outputs[j])->graph = NULL;
111
112 57912 return;
113 }
114 }
115 }
116
117 22766 void avfilter_graph_free(AVFilterGraph **graphp)
118 {
119 22766 AVFilterGraph *graph = *graphp;
120 22766 FFFilterGraph *graphi = fffiltergraph(graph);
121
122
2/2
✓ Branch 0 taken 7537 times.
✓ Branch 1 taken 15229 times.
22766 if (!graph)
123 7537 return;
124
125
2/2
✓ Branch 0 taken 57846 times.
✓ Branch 1 taken 15229 times.
73075 while (graph->nb_filters)
126 57846 avfilter_free(graph->filters[0]);
127
128 15229 ff_graph_thread_free(graphi);
129
130 15229 av_freep(&graphi->sink_links);
131
132 15229 av_opt_free(graph);
133
134 15229 av_freep(&graph->filters);
135 15229 av_freep(graphp);
136 }
137
138 17840 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 17840 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17840 times.
17840 if (!*filt_ctx)
146 return AVERROR(ENOMEM);
147
148 17840 ret = avfilter_init_str(*filt_ctx, args);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17840 times.
17840 if (ret < 0)
150 goto fail;
151
152 17840 return 0;
153
154 fail:
155 avfilter_free(*filt_ctx);
156 *filt_ctx = NULL;
157 return ret;
158 }
159
160 5810 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
161 {
162 5810 fffiltergraph(graph)->disable_auto_convert = flags;
163 5810 }
164
165 57912 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
166 const AVFilter *filter,
167 const char *name)
168 {
169 AVFilterContext **filters, *s;
170 57912 FFFilterGraph *graphi = fffiltergraph(graph);
171
172
4/4
✓ Branch 0 taken 29912 times.
✓ Branch 1 taken 28000 times.
✓ Branch 2 taken 15229 times.
✓ Branch 3 taken 14683 times.
57912 if (graph->thread_type && !graphi->thread_execute) {
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15229 times.
15229 if (graph->execute) {
174 graphi->thread_execute = graph->execute;
175 } else {
176 15229 int ret = ff_graph_thread_init(graphi);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15229 times.
15229 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 57912 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57912 times.
57912 if (!filters)
186 return NULL;
187 57912 graph->filters = filters;
188
189 57912 s = ff_filter_alloc(filter, name);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57912 times.
57912 if (!s)
191 return NULL;
192
193 57912 graph->filters[graph->nb_filters++] = s;
194
195 57912 s->graph = graph;
196
197 57912 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 7626 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 39995 times.
✓ Branch 1 taken 7626 times.
47621 for (i = 0; i < graph->nb_filters; i++) {
214 const AVFilterPad *pad;
215 39995 filt = graph->filters[i];
216
217
2/2
✓ Branch 0 taken 32341 times.
✓ Branch 1 taken 39995 times.
72336 for (j = 0; j < filt->nb_inputs; j++) {
218
2/4
✓ Branch 0 taken 32341 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32341 times.
32341 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 32341 times.
✓ Branch 1 taken 39995 times.
72336 for (j = 0; j < filt->nb_outputs; j++) {
228
2/4
✓ Branch 0 taken 32341 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32341 times.
32341 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 7626 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 7626 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 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++) {
252 41223 filt = graph->filters[i];
253
254
2/2
✓ Branch 0 taken 7749 times.
✓ Branch 1 taken 33474 times.
41223 if (!filt->nb_outputs) {
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7749 times.
7749 if ((ret = ff_filter_config_links(filt)))
256 return ret;
257 }
258 }
259
260 7626 return 0;
261 }
262
263 7626 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 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++) {
271 41223 f = graph->filters[i];
272
2/2
✓ Branch 0 taken 33569 times.
✓ Branch 1 taken 41223 times.
74792 for (j = 0; j < f->nb_outputs; j++) {
273 33569 l = f->outputs[j];
274
2/2
✓ Branch 0 taken 27736 times.
✓ Branch 1 taken 5833 times.
33569 if (l->type == AVMEDIA_TYPE_VIDEO) {
275 27736 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 27736 times.
27736 if (ret < 0)
277 return ret;
278 }
279 }
280 }
281 7626 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 54751 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
296 {
297 int ret;
298
299
2/3
✓ Branch 0 taken 45505 times.
✓ Branch 1 taken 9246 times.
✗ Branch 2 not taken.
54751 switch (link->type) {
300
301 45505 case AVMEDIA_TYPE_VIDEO:
302
2/4
✓ Branch 1 taken 45505 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 45505 times.
✗ Branch 4 not taken.
91010 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45505 times.
91010 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
304 45505 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0)
305 return ret;
306 45505 break;
307
308 9246 case AVMEDIA_TYPE_AUDIO:
309
2/4
✓ Branch 1 taken 9246 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9246 times.
✗ Branch 4 not taken.
18492 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9246 times.
18492 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
311 9246 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
312 return ret;
313 9246 break;
314
315 default:
316 av_assert0(!"reached");
317 }
318 54751 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 34654 static int filter_check_formats(AVFilterContext *ctx)
327 {
328 unsigned i;
329 int ret;
330
331
2/2
✓ Branch 0 taken 27778 times.
✓ Branch 1 taken 34654 times.
62432 for (i = 0; i < ctx->nb_inputs; i++) {
332 27778 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27778 times.
27778 if (ret < 0)
334 return ret;
335 }
336
2/2
✓ Branch 0 taken 26973 times.
✓ Branch 1 taken 34654 times.
61627 for (i = 0; i < ctx->nb_outputs; i++) {
337 26973 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26973 times.
26973 if (ret < 0)
339 return ret;
340 }
341 34654 return 0;
342 }
343
344 41237 static int filter_query_formats(AVFilterContext *ctx)
345 {
346 int ret;
347
348
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 41209 times.
41237 if (ctx->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
349
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
28 if ((ret = ctx->filter->formats.query_func(ctx)) < 0) {
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret != AVERROR(EAGAIN))
351 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
352 ctx->name, av_err2str(ret));
353 14 return ret;
354 }
355
2/2
✓ Branch 0 taken 34640 times.
✓ Branch 1 taken 6569 times.
41209 } else if (ctx->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
356 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
357 34640 AVFilterFormatsConfig **cfg_in_dyn = NULL, **cfg_out_dyn = NULL;
358 AVFilterFormatsConfig **cfg_in, **cfg_out;
359
360
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34639 times.
34640 if (ctx->nb_inputs > FF_ARRAY_ELEMS(cfg_in_stack)) {
361 1 cfg_in_dyn = av_malloc_array(ctx->nb_inputs, sizeof(*cfg_in_dyn));
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_in_dyn)
363 return AVERROR(ENOMEM);
364 1 cfg_in = cfg_in_dyn;
365 } else
366
2/2
✓ Branch 0 taken 27649 times.
✓ Branch 1 taken 6990 times.
34639 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
367
368
2/2
✓ Branch 0 taken 27763 times.
✓ Branch 1 taken 34640 times.
62403 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
369 27763 AVFilterLink *l = ctx->inputs[i];
370 27763 cfg_in[i] = &l->outcfg;
371 }
372
373
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 34639 times.
34640 if (ctx->nb_outputs > FF_ARRAY_ELEMS(cfg_out_stack)) {
374 1 cfg_out_dyn = av_malloc_array(ctx->nb_outputs, sizeof(*cfg_out_dyn));
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_out_dyn) {
376 av_freep(&cfg_in_dyn);
377 return AVERROR(ENOMEM);
378 }
379 1 cfg_out = cfg_out_dyn;
380 } else
381
2/2
✓ Branch 0 taken 26890 times.
✓ Branch 1 taken 7749 times.
34639 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
382
383
2/2
✓ Branch 0 taken 26959 times.
✓ Branch 1 taken 34640 times.
61599 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
384 26959 AVFilterLink *l = ctx->outputs[i];
385 26959 cfg_out[i] = &l->incfg;
386 }
387
388 34640 ret = ctx->filter->formats.query_func2(ctx, cfg_in, cfg_out);
389 34640 av_freep(&cfg_in_dyn);
390 34640 av_freep(&cfg_out_dyn);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34640 times.
34640 if (ret < 0) {
392 if (ret != AVERROR(EAGAIN))
393 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
394 ctx->name, av_err2str(ret));
395 return ret;
396 }
397 }
398
399
2/2
✓ Branch 0 taken 41209 times.
✓ Branch 1 taken 14 times.
41223 if (ctx->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
400
2/2
✓ Branch 0 taken 34640 times.
✓ Branch 1 taken 6569 times.
41209 ctx->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
401 34654 ret = filter_check_formats(ctx);
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34654 times.
34654 if (ret < 0)
403 return ret;
404 }
405
406 41223 return ff_default_query_formats(ctx);
407 }
408
409 40053 static int formats_declared(AVFilterContext *f)
410 {
411 int i;
412
413
2/2
✓ Branch 0 taken 32251 times.
✓ Branch 1 taken 7835 times.
40086 for (i = 0; i < f->nb_inputs; i++) {
414
2/2
✓ Branch 0 taken 32216 times.
✓ Branch 1 taken 35 times.
32251 if (!f->inputs[i]->outcfg.formats)
415 32216 return 0;
416
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
35 if (f->inputs[i]->type == AVMEDIA_TYPE_VIDEO &&
417
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 !(f->inputs[i]->outcfg.color_ranges &&
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->inputs[i]->outcfg.color_spaces))
419 2 return 0;
420
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
421
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(f->inputs[i]->outcfg.samplerates &&
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 f->inputs[i]->outcfg.channel_layouts))
423 return 0;
424 }
425
2/2
✓ Branch 0 taken 7825 times.
✓ Branch 1 taken 44 times.
7869 for (i = 0; i < f->nb_outputs; i++) {
426
2/2
✓ Branch 0 taken 7791 times.
✓ Branch 1 taken 34 times.
7825 if (!f->outputs[i]->incfg.formats)
427 7791 return 0;
428
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 3 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_VIDEO &&
429
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !(f->outputs[i]->incfg.color_ranges &&
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->outputs[i]->incfg.color_spaces))
431 return 0;
432
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
433
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !(f->outputs[i]->incfg.samplerates &&
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 f->outputs[i]->incfg.channel_layouts))
435 return 0;
436 }
437 44 return 1;
438 }
439
440 /**
441 * Perform one round of query_formats() and merging formats lists on the
442 * filter graph.
443 * @return >=0 if all links formats lists could be queried and merged;
444 * AVERROR(EAGAIN) some progress was made in the queries or merging
445 * and a later call may succeed;
446 * AVERROR(EIO) (may be changed) plus a log message if no progress
447 * was made and the negotiation is stuck;
448 * a negative error code if some other error happened
449 */
450 7640 static int query_formats(AVFilterGraph *graph, void *log_ctx)
451 {
452 int i, j, ret;
453 7640 int converter_count = 0;
454 7640 int count_queried = 0; /* successful calls to query_formats() */
455 7640 int count_merged = 0; /* successful merge of formats lists */
456 7640 int count_already_merged = 0; /* lists already merged */
457 7640 int count_delayed = 0; /* lists that need to be merged later */
458
459
2/2
✓ Branch 0 taken 40053 times.
✓ Branch 1 taken 7640 times.
47693 for (i = 0; i < graph->nb_filters; i++) {
460 40053 AVFilterContext *f = graph->filters[i];
461
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 40009 times.
40053 if (formats_declared(f))
462 44 continue;
463 40009 ret = filter_query_formats(f);
464
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 39995 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
40009 if (ret < 0 && ret != AVERROR(EAGAIN))
465 return ret;
466 /* note: EAGAIN could indicate a partial success, not counted yet */
467 40009 count_queried += ret >= 0;
468 }
469
470 /* go through and merge as many format lists as possible */
471
2/2
✓ Branch 0 taken 41281 times.
✓ Branch 1 taken 7640 times.
48921 for (i = 0; i < graph->nb_filters; i++) {
472 41281 AVFilterContext *filter = graph->filters[i];
473
474
2/2
✓ Branch 0 taken 33617 times.
✓ Branch 1 taken 41281 times.
74898 for (j = 0; j < filter->nb_inputs; j++) {
475 33617 AVFilterLink *link = filter->inputs[j];
476 const AVFilterNegotiation *neg;
477 unsigned neg_step;
478 33617 int convert_needed = 0;
479
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33617 times.
33617 if (!link)
481 continue;
482
483 33617 neg = ff_filter_get_negotiation(link);
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33617 times.
33617 av_assert0(neg);
485
2/2
✓ Branch 0 taken 99603 times.
✓ Branch 1 taken 32389 times.
131992 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
486 99603 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
487 99603 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
488 99603 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
489
8/8
✓ Branch 0 taken 99561 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 99518 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 95762 times.
✓ Branch 5 taken 3756 times.
✓ Branch 7 taken 1228 times.
✓ Branch 8 taken 94534 times.
99603 if (a && b && a != b && !m->can_merge(a, b)) {
490 1228 convert_needed = 1;
491 1228 break;
492 }
493 }
494
2/2
✓ Branch 0 taken 100851 times.
✓ Branch 1 taken 33617 times.
134468 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
495 100851 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
496 100851 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
497 100851 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
498
4/4
✓ Branch 0 taken 100809 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 100766 times.
100851 if (!(a && b)) {
499 85 count_delayed++;
500
2/2
✓ Branch 0 taken 8864 times.
✓ Branch 1 taken 91902 times.
100766 } else if (a == b) {
501 8864 count_already_merged++;
502
2/2
✓ Branch 0 taken 88781 times.
✓ Branch 1 taken 3121 times.
91902 } else if (!convert_needed) {
503 88781 count_merged++;
504 88781 ret = m->merge(a, b);
505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88781 times.
88781 if (ret < 0)
506 return ret;
507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88781 times.
88781 if (!ret)
508 convert_needed = 1;
509 }
510 }
511
512
2/2
✓ Branch 0 taken 1228 times.
✓ Branch 1 taken 32389 times.
33617 if (convert_needed) {
513 AVFilterContext *convert;
514 const AVFilter *filter;
515 AVFilterLink *inlink, *outlink;
516 char inst_name[30];
517 const char *opts;
518
519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1228 times.
1228 if (fffiltergraph(graph)->disable_auto_convert) {
520 av_log(log_ctx, AV_LOG_ERROR,
521 "The filters '%s' and '%s' do not have a common format "
522 "and automatic conversion is disabled.\n",
523 link->src->name, link->dst->name);
524 return AVERROR(EINVAL);
525 }
526
527 /* couldn't merge format lists. auto-insert conversion filter */
528
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1228 times.
1228 if (!(filter = avfilter_get_by_name(neg->conversion_filter))) {
529 av_log(log_ctx, AV_LOG_ERROR,
530 "'%s' filter not present, cannot convert formats.\n",
531 neg->conversion_filter);
532 return AVERROR(EINVAL);
533 }
534 1228 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
535 1228 neg->conversion_filter, converter_count++);
536 1228 opts = FF_FIELD_AT(char *, neg->conversion_opts_offset, *graph);
537 1228 ret = avfilter_graph_create_filter(&convert, filter, inst_name, opts, NULL, graph);
538
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 if (ret < 0)
539 return ret;
540
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1228 times.
1228 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
541 return ret;
542
543
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1228 times.
1228 if ((ret = filter_query_formats(convert)) < 0)
544 return ret;
545
546 1228 inlink = convert->inputs[0];
547 1228 outlink = convert->outputs[0];
548
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 av_assert0( inlink->incfg.formats->refcount > 0);
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 av_assert0( inlink->outcfg.formats->refcount > 0);
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 av_assert0(outlink->incfg.formats->refcount > 0);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1228 times.
1228 av_assert0(outlink->outcfg.formats->refcount > 0);
552
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 612 times.
1228 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
553
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 616 times.
616 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
561
1/2
✓ Branch 0 taken 612 times.
✗ Branch 1 not taken.
612 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0( inlink-> incfg.samplerates->refcount > 0);
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0( inlink->outcfg.samplerates->refcount > 0);
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0(outlink-> incfg.samplerates->refcount > 0);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0(outlink->outcfg.samplerates->refcount > 0);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 612 times.
612 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
570 }
571 #define MERGE(merger, link) \
572 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
573 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
574
2/2
✓ Branch 0 taken 3684 times.
✓ Branch 1 taken 1228 times.
4912 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
575 3684 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
576
2/4
✓ Branch 1 taken 3684 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3684 times.
7368 if ((ret = MERGE(m, inlink)) <= 0 ||
577 3684 (ret = MERGE(m, outlink)) <= 0) {
578 if (ret < 0)
579 return ret;
580 av_log(log_ctx, AV_LOG_ERROR,
581 "Impossible to convert between the formats supported by the filter "
582 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
583 return AVERROR(ENOSYS);
584 }
585 }
586 }
587 }
588 }
589
590 7640 av_log(graph, AV_LOG_DEBUG, "query_formats: "
591 "%d queried, %d merged, %d already done, %d delayed\n",
592 count_queried, count_merged, count_already_merged, count_delayed);
593
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7626 times.
7640 if (count_delayed) {
594 AVBPrint bp;
595
596 /* if count_queried > 0, one filter at least did set its formats,
597 that will give additional information to its neighbour;
598 if count_merged > 0, one pair of formats lists at least was merged,
599 that will give additional information to all connected filters;
600 in both cases, progress was made and a new round must be done */
601
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)
602 14 return AVERROR(EAGAIN);
603 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
604 for (i = 0; i < graph->nb_filters; i++)
605 if (!formats_declared(graph->filters[i]))
606 av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
607 graph->filters[i]->name);
608 av_log(graph, AV_LOG_ERROR,
609 "The following filters could not choose their formats: %s\n"
610 "Consider inserting the (a)format filter near their input or "
611 "output.\n", bp.str);
612 return AVERROR(EIO);
613 }
614 7626 return 0;
615 }
616
617 958 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
618 {
619 958 int score = 0;
620
621
2/2
✓ Branch 2 taken 874 times.
✓ Branch 3 taken 84 times.
958 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
622 874 score ++;
623
624
2/2
✓ Branch 2 taken 222 times.
✓ Branch 3 taken 736 times.
958 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
625 222 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
626 }else
627 736 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
628
629
3/4
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 840 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 118 times.
1076 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
630 118 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
631 score += 20;
632
633
3/4
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 874 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 84 times.
1042 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
634 84 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
635 score += 2;
636
637 958 return score;
638 }
639
640 479 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
641 enum AVSampleFormat src_fmt)
642 {
643 int score1, score2;
644
645 479 score1 = get_fmt_score(dst_fmt1, src_fmt);
646 479 score2 = get_fmt_score(dst_fmt2, src_fmt);
647
648
2/2
✓ Branch 0 taken 360 times.
✓ Branch 1 taken 119 times.
479 return score1 < score2 ? dst_fmt1 : dst_fmt2;
649 }
650
651 33303 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
652 {
653 33303 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33303 times.
33303 if (!desc)
655 return 0;
656
2/2
✓ Branch 0 taken 1701 times.
✓ Branch 1 taken 31602 times.
33303 if (desc->nb_components < 3)
657 1701 return 0; /* Grayscale is explicitly full-range in swscale */
658 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
659 31602 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
660 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
661 }
662
663
664 23283 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
665 {
666
2/2
✓ Branch 0 taken 715 times.
✓ Branch 1 taken 22568 times.
23283 switch (fmt) {
667 715 case AV_PIX_FMT_YUVJ420P:
668 case AV_PIX_FMT_YUVJ422P:
669 case AV_PIX_FMT_YUVJ444P:
670 case AV_PIX_FMT_YUVJ440P:
671 case AV_PIX_FMT_YUVJ411P:
672 715 return 1;
673 22568 default:
674 22568 return 0;
675 }
676 }
677
678 100659 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
679 {
680
3/4
✓ Branch 0 taken 100659 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 67090 times.
✓ Branch 3 taken 33569 times.
100659 if (!link || !link->incfg.formats)
681 67090 return 0;
682
683
2/2
✓ Branch 0 taken 27736 times.
✓ Branch 1 taken 5833 times.
33569 if (link->type == AVMEDIA_TYPE_VIDEO) {
684
3/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 27639 times.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
27736 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
685 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
686 97 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
687 97 enum AVPixelFormat best= AV_PIX_FMT_NONE;
688 int i;
689
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 97 times.
1116 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
690 1019 enum AVPixelFormat p = link->incfg.formats->formats[i];
691 1019 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
692 }
693 97 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
694 97 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
695 97 av_get_pix_fmt_name(ref->format), has_alpha);
696 97 link->incfg.formats->formats[0] = best;
697 }
698
1/2
✓ Branch 0 taken 5833 times.
✗ Branch 1 not taken.
5833 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
699
3/4
✓ Branch 0 taken 119 times.
✓ Branch 1 taken 5714 times.
✓ Branch 2 taken 119 times.
✗ Branch 3 not taken.
5833 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
700 119 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
701 int i;
702
2/2
✓ Branch 0 taken 479 times.
✓ Branch 1 taken 119 times.
598 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
703 479 enum AVSampleFormat p = link->incfg.formats->formats[i];
704 479 best = find_best_sample_fmt_of_2(best, p, ref->format);
705 }
706 119 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
707 119 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
708 119 av_get_sample_fmt_name(ref->format));
709 119 link->incfg.formats->formats[0] = best;
710 }
711 }
712
713 33569 link->incfg.formats->nb_formats = 1;
714 33569 link->format = link->incfg.formats->formats[0];
715
716
2/2
✓ Branch 0 taken 27736 times.
✓ Branch 1 taken 5833 times.
33569 if (link->type == AVMEDIA_TYPE_VIDEO) {
717 27736 enum AVPixelFormat swfmt = link->format;
718
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27736 times.
27736 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
719 // FIXME: this is a hack - we'd like to use the sw_format of
720 // link->hw_frames_ctx here, but it is not yet available.
721 // To make this work properly we will need to either reorder
722 // things so that it is available here or somehow negotiate
723 // sw_format separately.
724 swfmt = AV_PIX_FMT_YUV420P;
725 }
726
727
2/2
✓ Branch 1 taken 9320 times.
✓ Branch 2 taken 18416 times.
27736 if (!ff_fmt_is_regular_yuv(swfmt)) {
728 9320 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
729 /* These fields are explicitly documented as affecting YUV only,
730 * so set them to sane values for other formats. */
731
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 8749 times.
9320 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
732 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
733 else
734 8749 link->color_range = AVCOL_RANGE_JPEG;
735
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)) {
736 7810 link->colorspace = AVCOL_SPC_RGB;
737 } else {
738 1510 link->colorspace = AVCOL_SPC_UNSPECIFIED;
739 }
740 } else {
741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18416 times.
18416 if (!link->incfg.color_spaces->nb_formats) {
742 av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
743 " the link between filters %s and %s.\n", link->src->name,
744 link->dst->name);
745 return AVERROR(EINVAL);
746 }
747 18416 link->incfg.color_spaces->nb_formats = 1;
748 18416 link->colorspace = link->incfg.color_spaces->formats[0];
749
750
2/2
✓ Branch 1 taken 646 times.
✓ Branch 2 taken 17770 times.
18416 if (ff_fmt_is_forced_full_range(swfmt)) {
751 646 link->color_range = AVCOL_RANGE_JPEG;
752 } else {
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17770 times.
17770 if (!link->incfg.color_ranges->nb_formats) {
754 av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
755 " the link between filters %s and %s.\n", link->src->name,
756 link->dst->name);
757 return AVERROR(EINVAL);
758 }
759 17770 link->incfg.color_ranges->nb_formats = 1;
760 17770 link->color_range = link->incfg.color_ranges->formats[0];
761 }
762 }
763
1/2
✓ Branch 0 taken 5833 times.
✗ Branch 1 not taken.
5833 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
764 int ret;
765
766
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5833 times.
5833 if (!link->incfg.samplerates->nb_formats) {
767 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
768 " the link between filters %s and %s.\n", link->src->name,
769 link->dst->name);
770 return AVERROR(EINVAL);
771 }
772 5833 link->incfg.samplerates->nb_formats = 1;
773 5833 link->sample_rate = link->incfg.samplerates->formats[0];
774
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5833 times.
5833 if (link->incfg.channel_layouts->all_layouts) {
776 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
777 " the link between filters %s and %s.\n", link->src->name,
778 link->dst->name);
779 if (!link->incfg.channel_layouts->all_counts)
780 av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
781 "supported, try specifying a channel layout using "
782 "'aformat=channel_layouts=something'.\n");
783 return AVERROR(EINVAL);
784 }
785 5833 link->incfg.channel_layouts->nb_channel_layouts = 1;
786 5833 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5833 times.
5833 if (ret < 0)
788 return ret;
789 }
790
791 33569 ff_formats_unref(&link->incfg.formats);
792 33569 ff_formats_unref(&link->outcfg.formats);
793 33569 ff_formats_unref(&link->incfg.samplerates);
794 33569 ff_formats_unref(&link->outcfg.samplerates);
795 33569 ff_channel_layouts_unref(&link->incfg.channel_layouts);
796 33569 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
797 33569 ff_formats_unref(&link->incfg.color_spaces);
798 33569 ff_formats_unref(&link->outcfg.color_spaces);
799 33569 ff_formats_unref(&link->incfg.color_ranges);
800 33569 ff_formats_unref(&link->outcfg.color_ranges);
801
802 33569 return 0;
803 }
804
805 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
806 do { \
807 for (i = 0; i < filter->nb_inputs; i++) { \
808 AVFilterLink *link = filter->inputs[i]; \
809 fmt_type fmt; \
810 \
811 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
812 continue; \
813 fmt = link->outcfg.list->var[0]; \
814 \
815 for (j = 0; j < filter->nb_outputs; j++) { \
816 AVFilterLink *out_link = filter->outputs[j]; \
817 list_type *fmts; \
818 \
819 if (link->type != out_link->type || \
820 out_link->incfg.list->nb == 1) \
821 continue; \
822 fmts = out_link->incfg.list; \
823 \
824 if (!out_link->incfg.list->nb) { \
825 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
826 return ret; \
827 ret = 1; \
828 break; \
829 } \
830 \
831 for (k = 0; k < out_link->incfg.list->nb; k++) \
832 if (fmts->var[k] == fmt) { \
833 fmts->var[0] = fmt; \
834 fmts->nb = 1; \
835 ret = 1; \
836 break; \
837 } \
838 } \
839 } \
840 } while (0)
841
842 67209 static int reduce_formats_on_filter(AVFilterContext *filter)
843 {
844 67209 int i, j, k, ret = 0;
845
846
16/20
✓ Branch 0 taken 55087 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1257 times.
✓ Branch 3 taken 53830 times.
✓ Branch 4 taken 41909 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 41383 times.
✓ Branch 7 taken 526 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 526 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 51 times.
✓ Branch 14 taken 4508 times.
✓ Branch 15 taken 4559 times.
✓ Branch 16 taken 475 times.
✓ Branch 17 taken 41921 times.
✓ Branch 18 taken 53830 times.
✓ Branch 19 taken 55087 times.
✓ Branch 20 taken 67209 times.
168725 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
847 nb_formats, ff_add_format);
848
18/20
✓ Branch 0 taken 10563 times.
✓ Branch 1 taken 44524 times.
✓ Branch 2 taken 1137 times.
✓ Branch 3 taken 9426 times.
✓ Branch 4 taken 7637 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6893 times.
✓ Branch 7 taken 744 times.
✓ Branch 8 taken 708 times.
✓ Branch 9 taken 36 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 708 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 71 times.
✓ Branch 15 taken 107 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 7643 times.
✓ Branch 18 taken 8718 times.
✓ Branch 19 taken 55087 times.
✓ Branch 20 taken 67209 times.
129302 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
849 nb_formats, ff_add_format);
850
15/20
✓ Branch 0 taken 44524 times.
✓ Branch 1 taken 10563 times.
✓ Branch 2 taken 8918 times.
✓ Branch 3 taken 35606 times.
✓ Branch 4 taken 27908 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24141 times.
✓ Branch 7 taken 3767 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3767 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3767 times.
✓ Branch 14 taken 361 times.
✓ Branch 15 taken 4128 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 27908 times.
✓ Branch 18 taken 35606 times.
✓ Branch 19 taken 55087 times.
✓ Branch 20 taken 67209 times.
150565 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
851 nb_formats, ff_add_format);
852
15/20
✓ Branch 0 taken 44524 times.
✓ Branch 1 taken 10563 times.
✓ Branch 2 taken 7257 times.
✓ Branch 3 taken 37267 times.
✓ Branch 4 taken 28608 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25439 times.
✓ Branch 7 taken 3169 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3169 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3169 times.
✓ Branch 14 taken 3148 times.
✓ Branch 15 taken 6317 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 28608 times.
✓ Branch 18 taken 37267 times.
✓ Branch 19 taken 55087 times.
✓ Branch 20 taken 67209 times.
154052 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
853 nb_formats, ff_add_format);
854
855 /* reduce channel layouts */
856
2/2
✓ Branch 0 taken 55087 times.
✓ Branch 1 taken 67209 times.
122296 for (i = 0; i < filter->nb_inputs; i++) {
857 55087 AVFilterLink *inlink = filter->inputs[i];
858 const AVChannelLayout *fmt;
859
860
2/2
✓ Branch 0 taken 10563 times.
✓ Branch 1 taken 44524 times.
55087 if (!inlink->outcfg.channel_layouts ||
861
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 10490 times.
10563 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
862 44597 continue;
863 10490 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
864
865
2/2
✓ Branch 0 taken 8330 times.
✓ Branch 1 taken 9651 times.
17981 for (j = 0; j < filter->nb_outputs; j++) {
866 8330 AVFilterLink *outlink = filter->outputs[j];
867 AVFilterChannelLayouts *fmts;
868
869 8330 fmts = outlink->incfg.channel_layouts;
870
4/4
✓ Branch 0 taken 8324 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7446 times.
✓ Branch 3 taken 878 times.
8330 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
871 7452 continue;
872
873
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 39 times.
878 if (fmts->all_layouts &&
874
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)) {
875 /* Turn the infinite list into a singleton */
876 839 fmts->all_layouts = fmts->all_counts = 0;
877 839 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 839 times.
839 if (ret < 0)
879 return ret;
880 839 ret = 1;
881 839 break;
882 }
883
884
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++) {
885
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 55 times.
90 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
886 35 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0)
888 return ret;
889 35 fmts->nb_channel_layouts = 1;
890 35 ret = 1;
891 35 break;
892 }
893 }
894 }
895 }
896
897 67209 return ret;
898 }
899
900 7626 static int reduce_formats(AVFilterGraph *graph)
901 {
902 int i, reduced, ret;
903
904 do {
905 12070 reduced = 0;
906
907
2/2
✓ Branch 0 taken 67209 times.
✓ Branch 1 taken 12070 times.
79279 for (i = 0; i < graph->nb_filters; i++) {
908
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 67209 times.
67209 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
909 return ret;
910 67209 reduced |= ret;
911 }
912
2/2
✓ Branch 0 taken 4444 times.
✓ Branch 1 taken 7626 times.
12070 } while (reduced);
913
914 7626 return 0;
915 }
916
917 41223 static void swap_samplerates_on_filter(AVFilterContext *filter)
918 {
919 41223 AVFilterLink *link = NULL;
920 int sample_rate;
921 int i, j;
922
923
2/2
✓ Branch 0 taken 33486 times.
✓ Branch 1 taken 35469 times.
68955 for (i = 0; i < filter->nb_inputs; i++) {
924 33486 link = filter->inputs[i];
925
926
2/2
✓ Branch 0 taken 5754 times.
✓ Branch 1 taken 27732 times.
33486 if (link->type == AVMEDIA_TYPE_AUDIO &&
927
1/2
✓ Branch 0 taken 5754 times.
✗ Branch 1 not taken.
5754 link->outcfg.samplerates->nb_formats== 1)
928 5754 break;
929 }
930
2/2
✓ Branch 0 taken 35469 times.
✓ Branch 1 taken 5754 times.
41223 if (i == filter->nb_inputs)
931 35469 return;
932
933 5754 sample_rate = link->outcfg.samplerates->formats[0];
934
935
2/2
✓ Branch 0 taken 4485 times.
✓ Branch 1 taken 5754 times.
10239 for (i = 0; i < filter->nb_outputs; i++) {
936 4485 AVFilterLink *outlink = filter->outputs[i];
937 4485 int best_idx, best_diff = INT_MAX;
938
939
2/2
✓ Branch 0 taken 4483 times.
✓ Branch 1 taken 2 times.
4485 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
940
1/2
✓ Branch 0 taken 4483 times.
✗ Branch 1 not taken.
4483 outlink->incfg.samplerates->nb_formats < 2)
941 4485 continue;
942
943 for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
944 int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
945
946 av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
947
948 if (diff < best_diff) {
949 best_diff = diff;
950 best_idx = j;
951 }
952 }
953 FFSWAP(int, outlink->incfg.samplerates->formats[0],
954 outlink->incfg.samplerates->formats[best_idx]);
955 }
956 }
957
958 7626 static void swap_samplerates(AVFilterGraph *graph)
959 {
960 int i;
961
962
2/2
✓ Branch 0 taken 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++)
963 41223 swap_samplerates_on_filter(graph->filters[i]);
964 7626 }
965
966 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
967 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
968 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
969 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
970 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
971 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
972 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
973
974 /* allowable substitutions for channel pairs when comparing layouts,
975 * ordered by priority for both values */
976 static const uint64_t ch_subst[][2] = {
977 { CH_FRONT_PAIR, CH_CENTER_PAIR },
978 { CH_FRONT_PAIR, CH_WIDE_PAIR },
979 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
980 { CH_CENTER_PAIR, CH_FRONT_PAIR },
981 { CH_CENTER_PAIR, CH_WIDE_PAIR },
982 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
983 { CH_WIDE_PAIR, CH_FRONT_PAIR },
984 { CH_WIDE_PAIR, CH_CENTER_PAIR },
985 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
986 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
987 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
988 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
989 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
990 { CH_SIDE_PAIR, CH_BACK_PAIR },
991 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
992 { CH_BACK_PAIR, CH_DIRECT_PAIR },
993 { CH_BACK_PAIR, CH_SIDE_PAIR },
994 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
995 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
996 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
997 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
998 };
999
1000 41223 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1001 {
1002 41223 AVFilterLink *link = NULL;
1003 int i, j, k;
1004
1005
2/2
✓ Branch 0 taken 33486 times.
✓ Branch 1 taken 35473 times.
68959 for (i = 0; i < filter->nb_inputs; i++) {
1006 33486 link = filter->inputs[i];
1007
1008
2/2
✓ Branch 0 taken 5754 times.
✓ Branch 1 taken 27732 times.
33486 if (link->type == AVMEDIA_TYPE_AUDIO &&
1009
2/2
✓ Branch 0 taken 5750 times.
✓ Branch 1 taken 4 times.
5754 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1010 5750 break;
1011 }
1012
2/2
✓ Branch 0 taken 35473 times.
✓ Branch 1 taken 5750 times.
41223 if (i == filter->nb_inputs)
1013 35473 return;
1014
1015
2/2
✓ Branch 0 taken 4483 times.
✓ Branch 1 taken 5750 times.
10233 for (i = 0; i < filter->nb_outputs; i++) {
1016 4483 AVFilterLink *outlink = filter->outputs[i];
1017 4483 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1018
1019
2/2
✓ Branch 0 taken 4481 times.
✓ Branch 1 taken 2 times.
4483 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1020
2/2
✓ Branch 0 taken 4479 times.
✓ Branch 1 taken 2 times.
4481 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1021 4481 continue;
1022
1023
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++) {
1024 18 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1025 int in_channels;
1026 int out_channels;
1027 int count_diff;
1028 int matched_channels, extra_channels;
1029 18 int score = 100000;
1030
1031 18 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1032 18 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1033 18 in_channels = in_chlayout.nb_channels;
1034 18 out_channels = out_chlayout.nb_channels;
1035 18 count_diff = out_channels - in_channels;
1036
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)) {
1037 /* Compute score in case the input or output layout encodes
1038 a channel count; in this case the score is not altered by
1039 the computation afterwards, as in_chlayout and
1040 out_chlayout have both been set to 0 */
1041
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))
1042
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1043
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1044 out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1045 32 score -= 10000 + FFABS(out_channels - in_channels) +
1046
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1047 16 av_channel_layout_uninit(&in_chlayout);
1048 16 av_channel_layout_uninit(&out_chlayout);
1049 /* Let the remaining computation run, even if the score
1050 value is not altered */
1051 }
1052
1053 /* channel substitution */
1054
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 18 times.
396 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1055 378 uint64_t cmp0 = ch_subst[k][0];
1056 378 uint64_t cmp1 = ch_subst[k][1];
1057
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) &&
1058
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 16 times.
42 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1059
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
20 av_channel_layout_subset(&out_chlayout, cmp1) &&
1060 2 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1061 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1062 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1063 /* add score for channel match, minus a deduction for
1064 having to do the substitution */
1065 score += 10 * av_popcount64(cmp1) - 2;
1066 }
1067 }
1068
1069 /* no penalty for LFE channel mismatch */
1070
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 &&
1071 2 av_channel_layout_channel_from_index(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1072 score += 10;
1073 18 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1074 18 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1075
1076 18 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1077 18 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1078 18 score += 10 * matched_channels - 5 * extra_channels;
1079
1080
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 ||
1081 (count_diff < best_count_diff && score == best_score)) {
1082 3 best_score = score;
1083 3 best_idx = j;
1084 3 best_count_diff = count_diff;
1085 }
1086 }
1087
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(best_idx >= 0);
1088 2 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1089 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1090 }
1091
1092 }
1093
1094 7626 static void swap_channel_layouts(AVFilterGraph *graph)
1095 {
1096 int i;
1097
1098
2/2
✓ Branch 0 taken 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++)
1099 41223 swap_channel_layouts_on_filter(graph->filters[i]);
1100 7626 }
1101
1102 41223 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1103 {
1104 41223 AVFilterLink *link = NULL;
1105 int format, bps;
1106 int i, j;
1107
1108
2/2
✓ Branch 0 taken 33555 times.
✓ Branch 1 taken 35808 times.
69363 for (i = 0; i < filter->nb_inputs; i++) {
1109 33555 link = filter->inputs[i];
1110
1111
2/2
✓ Branch 0 taken 5823 times.
✓ Branch 1 taken 27732 times.
33555 if (link->type == AVMEDIA_TYPE_AUDIO &&
1112
2/2
✓ Branch 0 taken 5415 times.
✓ Branch 1 taken 408 times.
5823 link->outcfg.formats->nb_formats == 1)
1113 5415 break;
1114 }
1115
2/2
✓ Branch 0 taken 35808 times.
✓ Branch 1 taken 5415 times.
41223 if (i == filter->nb_inputs)
1116 35808 return;
1117
1118 5415 format = link->outcfg.formats->formats[0];
1119 5415 bps = av_get_bytes_per_sample(format);
1120
1121
2/2
✓ Branch 0 taken 4146 times.
✓ Branch 1 taken 5415 times.
9561 for (i = 0; i < filter->nb_outputs; i++) {
1122 4146 AVFilterLink *outlink = filter->outputs[i];
1123 4146 int best_idx = -1, best_score = INT_MIN;
1124
1125
2/2
✓ Branch 0 taken 4144 times.
✓ Branch 1 taken 2 times.
4146 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1126
2/2
✓ Branch 0 taken 3997 times.
✓ Branch 1 taken 147 times.
4144 outlink->incfg.formats->nb_formats < 2)
1127 3999 continue;
1128
1129
2/2
✓ Branch 0 taken 271 times.
✓ Branch 1 taken 7 times.
278 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1130 271 int out_format = outlink->incfg.formats->formats[j];
1131 271 int out_bps = av_get_bytes_per_sample(out_format);
1132 int score;
1133
1134
4/4
✓ Branch 1 taken 145 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 131 times.
416 if (av_get_packed_sample_fmt(out_format) == format ||
1135 145 av_get_planar_sample_fmt(out_format) == format) {
1136 140 best_idx = j;
1137 140 break;
1138 }
1139
1140 /* for s32 and float prefer double to prevent loss of information */
1141
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 127 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
131 if (bps == 4 && out_bps == 8) {
1142 best_idx = j;
1143 break;
1144 }
1145
1146 /* prefer closest higher or equal bps */
1147 131 score = -abs(out_bps - bps);
1148
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 103 times.
131 if (out_bps >= bps)
1149 28 score += INT_MAX/2;
1150
1151
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 33 times.
131 if (score > best_score) {
1152 98 best_score = score;
1153 98 best_idx = j;
1154 }
1155 }
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 147 times.
147 av_assert0(best_idx >= 0);
1157 147 FFSWAP(int, outlink->incfg.formats->formats[0],
1158 outlink->incfg.formats->formats[best_idx]);
1159 }
1160 }
1161
1162 7626 static void swap_sample_fmts(AVFilterGraph *graph)
1163 {
1164 int i;
1165
1166
2/2
✓ Branch 0 taken 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++)
1167 41223 swap_sample_fmts_on_filter(graph->filters[i]);
1168
1169 7626 }
1170
1171 7626 static int pick_formats(AVFilterGraph *graph)
1172 {
1173 int i, j, ret;
1174 int change;
1175
1176 do{
1177 15295 change = 0;
1178
2/2
✓ Branch 0 taken 83325 times.
✓ Branch 1 taken 15295 times.
98620 for (i = 0; i < graph->nb_filters; i++) {
1179 83325 AVFilterContext *filter = graph->filters[i];
1180
2/2
✓ Branch 0 taken 67597 times.
✓ Branch 1 taken 15728 times.
83325 if (filter->nb_inputs){
1181
2/2
✓ Branch 0 taken 67946 times.
✓ Branch 1 taken 67597 times.
135543 for (j = 0; j < filter->nb_inputs; j++){
1182
4/4
✓ Branch 0 taken 14886 times.
✓ Branch 1 taken 53060 times.
✓ Branch 2 taken 14435 times.
✓ Branch 3 taken 451 times.
67946 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14435 times.
14435 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1184 return ret;
1185 14435 change = 1;
1186 }
1187 }
1188 }
1189
2/2
✓ Branch 0 taken 67685 times.
✓ Branch 1 taken 15640 times.
83325 if (filter->nb_outputs){
1190
2/2
✓ Branch 0 taken 67946 times.
✓ Branch 1 taken 67685 times.
135631 for (j = 0; j < filter->nb_outputs; j++){
1191
4/4
✓ Branch 0 taken 19414 times.
✓ Branch 1 taken 48532 times.
✓ Branch 2 taken 18870 times.
✓ Branch 3 taken 544 times.
67946 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18870 times.
18870 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1193 return ret;
1194 18870 change = 1;
1195 }
1196 }
1197 }
1198
6/6
✓ Branch 0 taken 67597 times.
✓ Branch 1 taken 15728 times.
✓ Branch 2 taken 51957 times.
✓ Branch 3 taken 15640 times.
✓ Branch 4 taken 51717 times.
✓ Branch 5 taken 240 times.
83325 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1199
2/2
✓ Branch 0 taken 51911 times.
✓ Branch 1 taken 51717 times.
103628 for (j = 0; j < filter->nb_outputs; j++) {
1200
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 51695 times.
51911 if (filter->outputs[j]->format<0) {
1201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 216 times.
216 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1202 return ret;
1203 216 change = 1;
1204 }
1205 }
1206 }
1207 }
1208
2/2
✓ Branch 0 taken 7669 times.
✓ Branch 1 taken 7626 times.
15295 }while(change);
1209
1210
2/2
✓ Branch 0 taken 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++) {
1211 41223 AVFilterContext *filter = graph->filters[i];
1212
1213
2/2
✓ Branch 0 taken 33569 times.
✓ Branch 1 taken 41223 times.
74792 for (j = 0; j < filter->nb_inputs; j++)
1214
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33569 times.
33569 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1215 return ret;
1216
2/2
✓ Branch 0 taken 33569 times.
✓ Branch 1 taken 41223 times.
74792 for (j = 0; j < filter->nb_outputs; j++)
1217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33569 times.
33569 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1218 return ret;
1219 }
1220 7626 return 0;
1221 }
1222
1223 /**
1224 * Configure the formats of all the links in the graph.
1225 */
1226 7626 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1227 {
1228 int ret;
1229
1230 /* find supported formats from sub-filters, and merge along links */
1231
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 7626 times.
7640 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1232 14 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7626 times.
7626 if (ret < 0)
1234 return ret;
1235
1236 /* Once everything is merged, it's possible that we'll still have
1237 * multiple valid media format choices. We try to minimize the amount
1238 * of format conversion inside filters */
1239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = reduce_formats(graph)) < 0)
1240 return ret;
1241
1242 /* for audio filters, ensure the best format, sample rate and channel layout
1243 * is selected */
1244 7626 swap_sample_fmts(graph);
1245 7626 swap_samplerates(graph);
1246 7626 swap_channel_layouts(graph);
1247
1248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = pick_formats(graph)) < 0)
1249 return ret;
1250
1251 7626 return 0;
1252 }
1253
1254 7626 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1255 {
1256 unsigned i, j;
1257 7626 int sink_links_count = 0, n = 0;
1258 AVFilterContext *f;
1259 FilterLinkInternal **sinks;
1260
1261
2/2
✓ Branch 0 taken 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++) {
1262 41223 f = graph->filters[i];
1263
2/2
✓ Branch 0 taken 33569 times.
✓ Branch 1 taken 41223 times.
74792 for (j = 0; j < f->nb_inputs; j++) {
1264 33569 ff_link_internal(f->inputs[j])->age_index = -1;
1265 }
1266
2/2
✓ Branch 0 taken 33569 times.
✓ Branch 1 taken 41223 times.
74792 for (j = 0; j < f->nb_outputs; j++) {
1267 33569 ff_link_internal(f->outputs[j])->age_index = -1;
1268 }
1269
2/2
✓ Branch 0 taken 7749 times.
✓ Branch 1 taken 33474 times.
41223 if (!f->nb_outputs) {
1270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7749 times.
7749 if (f->nb_inputs > INT_MAX - sink_links_count)
1271 return AVERROR(EINVAL);
1272 7749 sink_links_count += f->nb_inputs;
1273 }
1274 }
1275 7626 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7626 times.
7626 if (!sinks)
1277 return AVERROR(ENOMEM);
1278
2/2
✓ Branch 0 taken 41223 times.
✓ Branch 1 taken 7626 times.
48849 for (i = 0; i < graph->nb_filters; i++) {
1279 41223 f = graph->filters[i];
1280
2/2
✓ Branch 0 taken 7749 times.
✓ Branch 1 taken 33474 times.
41223 if (!f->nb_outputs) {
1281
2/2
✓ Branch 0 taken 7749 times.
✓ Branch 1 taken 7749 times.
15498 for (j = 0; j < f->nb_inputs; j++) {
1282 7749 sinks[n] = ff_link_internal(f->inputs[j]);
1283 7749 sinks[n]->age_index = n;
1284 7749 n++;
1285 }
1286 }
1287 }
1288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7626 times.
7626 av_assert0(n == sink_links_count);
1289 7626 fffiltergraph(graph)->sink_links = sinks;
1290 7626 fffiltergraph(graph)->sink_links_count = sink_links_count;
1291 7626 return 0;
1292 }
1293
1294 7626 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1295 {
1296 int ret;
1297
1298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = graph_check_validity(graphctx, log_ctx)))
1299 return ret;
1300
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = graph_config_formats(graphctx, log_ctx)))
1301 return ret;
1302
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = graph_config_links(graphctx, log_ctx)))
1303 return ret;
1304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = graph_check_links(graphctx, log_ctx)))
1305 return ret;
1306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7626 times.
7626 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1307 return ret;
1308
1309 7626 return 0;
1310 }
1311
1312 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1313 {
1314 int i, r = AVERROR(ENOSYS);
1315
1316 if (!graph)
1317 return r;
1318
1319 if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1320 r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1321 if (r != AVERROR(ENOSYS))
1322 return r;
1323 }
1324
1325 if (res_len && res)
1326 res[0] = 0;
1327
1328 for (i = 0; i < graph->nb_filters; i++) {
1329 AVFilterContext *filter = graph->filters[i];
1330 if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1331 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1332 if (r != AVERROR(ENOSYS)) {
1333 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1334 return r;
1335 }
1336 }
1337 }
1338
1339 return r;
1340 }
1341
1342 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1343 {
1344 int i;
1345
1346 if(!graph)
1347 return 0;
1348
1349 for (i = 0; i < graph->nb_filters; i++) {
1350 AVFilterContext *filter = graph->filters[i];
1351 FFFilterContext *ctxi = fffilterctx(filter);
1352 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1353 AVFilterCommand **queue = &ctxi->command_queue, *next;
1354 while (*queue && (*queue)->time <= ts)
1355 queue = &(*queue)->next;
1356 next = *queue;
1357 *queue = av_mallocz(sizeof(AVFilterCommand));
1358 if (!*queue)
1359 return AVERROR(ENOMEM);
1360
1361 (*queue)->command = av_strdup(command);
1362 (*queue)->arg = av_strdup(arg);
1363 (*queue)->time = ts;
1364 (*queue)->flags = flags;
1365 (*queue)->next = next;
1366 if(flags & AVFILTER_CMD_FLAG_ONE)
1367 return 0;
1368 }
1369 }
1370
1371 return 0;
1372 }
1373
1374 399742 static void heap_bubble_up(FFFilterGraph *graph,
1375 FilterLinkInternal *li, int index)
1376 {
1377 399742 FilterLinkInternal **links = graph->sink_links;
1378
1379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 399742 times.
399742 av_assert0(index >= 0);
1380
1381
2/2
✓ Branch 0 taken 852 times.
✓ Branch 1 taken 399702 times.
400554 while (index) {
1382 852 int parent = (index - 1) >> 1;
1383
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 812 times.
852 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1384 40 break;
1385 812 links[index] = links[parent];
1386 812 links[index]->age_index = index;
1387 812 index = parent;
1388 }
1389 399742 links[index] = li;
1390 399742 li->age_index = index;
1391 399742 }
1392
1393 399839 static void heap_bubble_down(FFFilterGraph *graph,
1394 FilterLinkInternal *li, int index)
1395 {
1396 399839 FilterLinkInternal **links = graph->sink_links;
1397
1398
1/2
✓ Branch 0 taken 399839 times.
✗ Branch 1 not taken.
399839 av_assert0(index >= 0);
1399
1400 4233 while (1) {
1401 404072 int child = 2 * index + 1;
1402
2/2
✓ Branch 0 taken 399532 times.
✓ Branch 1 taken 4540 times.
404072 if (child >= graph->sink_links_count)
1403 399532 break;
1404
2/2
✓ Branch 0 taken 3219 times.
✓ Branch 1 taken 1321 times.
4540 if (child + 1 < graph->sink_links_count &&
1405
2/2
✓ Branch 0 taken 896 times.
✓ Branch 1 taken 2323 times.
3219 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1406 896 child++;
1407
2/2
✓ Branch 0 taken 307 times.
✓ Branch 1 taken 4233 times.
4540 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1408 307 break;
1409 4233 links[index] = links[child];
1410 4233 links[index]->age_index = index;
1411 4233 index = child;
1412 }
1413 399839 links[index] = li;
1414 399839 li->age_index = index;
1415 399839 }
1416
1417 399742 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1418 {
1419 399742 FFFilterGraph *graphi = fffiltergraph(graph);
1420
1421 399742 heap_bubble_up (graphi, li, li->age_index);
1422 399742 heap_bubble_down(graphi, li, li->age_index);
1423 399742 }
1424
1425 768461 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1426 {
1427 768461 FFFilterGraph *graphi = fffiltergraph(graph);
1428 768461 FilterLinkInternal *oldesti = graphi->sink_links[0];
1429 768461 AVFilterLink *oldest = &oldesti->l.pub;
1430 int64_t frame_count;
1431 int r;
1432
1433
2/2
✓ Branch 0 taken 768579 times.
✓ Branch 1 taken 4592 times.
773171 while (graphi->sink_links_count) {
1434 768579 oldesti = graphi->sink_links[0];
1435 768579 oldest = &oldesti->l.pub;
1436
1/2
✓ Branch 0 taken 768579 times.
✗ Branch 1 not taken.
768579 if (oldest->dst->filter->activate) {
1437 768579 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1438 AV_BUFFERSINK_FLAG_PEEK);
1439
2/2
✓ Branch 0 taken 763869 times.
✓ Branch 1 taken 4710 times.
768579 if (r != AVERROR_EOF)
1440 763869 return r;
1441 } else {
1442 r = ff_request_frame(oldest);
1443 }
1444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4710 times.
4710 if (r != AVERROR_EOF)
1445 break;
1446 4710 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1447 4710 oldest->dst->name,
1448 4710 oldest->dstpad->name);
1449 /* EOF: remove the link from the heap */
1450
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 4613 times.
4710 if (oldesti->age_index < --graphi->sink_links_count)
1451 97 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1452 oldesti->age_index);
1453 4710 oldesti->age_index = -1;
1454 }
1455
1/2
✓ Branch 0 taken 4592 times.
✗ Branch 1 not taken.
4592 if (!graphi->sink_links_count)
1456 4592 return AVERROR_EOF;
1457 av_assert1(!oldest->dst->filter->activate);
1458 av_assert1(oldesti->age_index >= 0);
1459 frame_count = oldesti->l.frame_count_out;
1460 while (frame_count == oldesti->l.frame_count_out) {
1461 r = ff_filter_graph_run_once(graph);
1462 if (r == AVERROR(EAGAIN) &&
1463 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1464 !oldesti->status_in)
1465 (void)ff_request_frame(oldest);
1466 else if (r < 0)
1467 return r;
1468 }
1469 return 0;
1470 }
1471
1472 4253130 int ff_filter_graph_run_once(AVFilterGraph *graph)
1473 {
1474 FFFilterContext *ctxi;
1475 unsigned i;
1476
1477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4253130 times.
4253130 av_assert0(graph->nb_filters);
1478 4253130 ctxi = fffilterctx(graph->filters[0]);
1479
2/2
✓ Branch 0 taken 17260824 times.
✓ Branch 1 taken 4253130 times.
21513954 for (i = 1; i < graph->nb_filters; i++) {
1480 17260824 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1481
1482
2/2
✓ Branch 0 taken 2495831 times.
✓ Branch 1 taken 14764993 times.
17260824 if (ctxi_other->ready > ctxi->ready)
1483 2495831 ctxi = ctxi_other;
1484 }
1485
1486
2/2
✓ Branch 0 taken 727320 times.
✓ Branch 1 taken 3525810 times.
4253130 if (!ctxi->ready)
1487 727320 return AVERROR(EAGAIN);
1488 3525810 return ff_filter_activate(&ctxi->p);
1489 }
1490