FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2026-10-28 19:13:44
Exec Total Coverage
Lines: 679 898 75.6%
Functions: 37 41 90.2%
Branches: 597 867 68.9%

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