FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 660 870 75.9%
Functions: 37 42 88.1%
Branches: 559 826 67.7%

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 16159 AVFilterGraph *avfilter_graph_alloc(void)
86 {
87 16159 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
88 AVFilterGraph *ret;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16159 times.
16159 if (!graph)
91 return NULL;
92
93 16159 ret = &graph->p;
94 16159 ret->av_class = &filtergraph_class;
95 16159 av_opt_set_defaults(ret);
96 16159 ff_framequeue_global_init(&graph->frame_queues);
97
98 16159 return ret;
99 }
100
101 62880 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
102 {
103 int i, j;
104
1/2
✓ Branch 0 taken 62880 times.
✗ Branch 1 not taken.
62880 for (i = 0; i < graph->nb_filters; i++) {
105
1/2
✓ Branch 0 taken 62880 times.
✗ Branch 1 not taken.
62880 if (graph->filters[i] == filter) {
106 62880 FFSWAP(AVFilterContext*, graph->filters[i],
107 graph->filters[graph->nb_filters - 1]);
108 62880 graph->nb_filters--;
109 62880 filter->graph = NULL;
110
2/2
✓ Branch 0 taken 54845 times.
✓ Branch 1 taken 62880 times.
117725 for (j = 0; j<filter->nb_outputs; j++)
111
2/2
✓ Branch 0 taken 20754 times.
✓ Branch 1 taken 34091 times.
54845 if (filter->outputs[j])
112 20754 ff_filter_link(filter->outputs[j])->graph = NULL;
113
114 62880 return;
115 }
116 }
117 }
118
119 24157 void avfilter_graph_free(AVFilterGraph **graphp)
120 {
121 24157 AVFilterGraph *graph = *graphp;
122 24157 FFFilterGraph *graphi = fffiltergraph(graph);
123
124
2/2
✓ Branch 0 taken 7998 times.
✓ Branch 1 taken 16159 times.
24157 if (!graph)
125 7998 return;
126
127
2/2
✓ Branch 0 taken 62814 times.
✓ Branch 1 taken 16159 times.
78973 while (graph->nb_filters)
128 62814 avfilter_free(graph->filters[0]);
129
130 16159 ff_graph_thread_free(graphi);
131
132 16159 av_freep(&graphi->sink_links);
133
134 16159 av_opt_free(graph);
135
136 16159 av_freep(&graph->filters);
137 16159 av_freep(graphp);
138 }
139
140 19120 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 19120 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19120 times.
19120 if (!*filt_ctx)
148 return AVERROR(ENOMEM);
149
150 19120 ret = avfilter_init_str(*filt_ctx, args);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19120 times.
19120 if (ret < 0)
152 goto fail;
153
154 19120 return 0;
155
156 fail:
157 avfilter_free(*filt_ctx);
158 *filt_ctx = NULL;
159 return ret;
160 }
161
162 6263 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
163 {
164 6263 fffiltergraph(graph)->disable_auto_convert = flags;
165 6263 }
166
167 62880 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
168 const AVFilter *filter,
169 const char *name)
170 {
171 AVFilterContext **filters, *s;
172 62880 FFFilterGraph *graphi = fffiltergraph(graph);
173
174
4/4
✓ Branch 0 taken 33085 times.
✓ Branch 1 taken 29795 times.
✓ Branch 2 taken 16159 times.
✓ Branch 3 taken 16926 times.
62880 if (graph->thread_type && !graphi->thread_execute) {
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16159 times.
16159 if (graph->execute) {
176 graphi->thread_execute = graph->execute;
177 } else {
178 16159 int ret = ff_graph_thread_init(graphi);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16159 times.
16159 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 62880 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62880 times.
62880 if (!filters)
188 return NULL;
189 62880 graph->filters = filters;
190
191 62880 s = ff_filter_alloc(filter, name);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62880 times.
62880 if (!s)
193 return NULL;
194
195 62880 graph->filters[graph->nb_filters++] = s;
196
197 62880 s->graph = graph;
198
199 62880 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 8095 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 43331 times.
✓ Branch 1 taken 8095 times.
51426 for (i = 0; i < graph->nb_filters; i++) {
216 const AVFilterPad *pad;
217 43331 filt = graph->filters[i];
218
219
2/2
✓ Branch 0 taken 35199 times.
✓ Branch 1 taken 43331 times.
78530 for (j = 0; j < filt->nb_inputs; j++) {
220
2/4
✓ Branch 0 taken 35199 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35199 times.
35199 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 35199 times.
✓ Branch 1 taken 43331 times.
78530 for (j = 0; j < filt->nb_outputs; j++) {
230
2/4
✓ Branch 0 taken 35199 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35199 times.
35199 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 8095 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 8095 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 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++) {
254 44584 filt = graph->filters[i];
255
256
2/2
✓ Branch 0 taken 8231 times.
✓ Branch 1 taken 36353 times.
44584 if (!filt->nb_outputs) {
257
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8231 times.
8231 if ((ret = ff_filter_config_links(filt)))
258 return ret;
259 }
260 }
261
262 8095 return 0;
263 }
264
265 8095 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 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++) {
273 44584 f = graph->filters[i];
274
2/2
✓ Branch 0 taken 36452 times.
✓ Branch 1 taken 44584 times.
81036 for (j = 0; j < f->nb_outputs; j++) {
275 36452 l = f->outputs[j];
276
2/2
✓ Branch 0 taken 30527 times.
✓ Branch 1 taken 5925 times.
36452 if (l->type == AVMEDIA_TYPE_VIDEO) {
277 30527 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 30527 times.
30527 if (ret < 0)
279 return ret;
280 }
281 }
282 }
283 8095 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 59467 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
298 {
299 int ret;
300
301
2/3
✓ Branch 0 taken 50070 times.
✓ Branch 1 taken 9397 times.
✗ Branch 2 not taken.
59467 switch (link->type) {
302
303 50070 case AVMEDIA_TYPE_VIDEO:
304
2/4
✓ Branch 1 taken 50070 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50070 times.
✗ Branch 4 not taken.
100140 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
305
1/2
✓ Branch 1 taken 50070 times.
✗ Branch 2 not taken.
100140 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50070 times.
100140 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0 ||
307 50070 (ret = ff_formats_check_alpha_modes(log, cfg->alpha_modes)) < 0)
308 return ret;
309 50070 break;
310
311 9397 case AVMEDIA_TYPE_AUDIO:
312
2/4
✓ Branch 1 taken 9397 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9397 times.
✗ Branch 4 not taken.
18794 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9397 times.
18794 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
314 9397 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
315 return ret;
316 9397 break;
317
318 default:
319 av_assert0(!"reached");
320 }
321 59467 return 0;
322 }
323
324 /**
325 * Check the validity of the formats / etc. lists set by query_formats().
326 *
327 * In particular, check they do not contain any redundant element.
328 */
329 37341 static int filter_check_formats(AVFilterContext *ctx)
330 {
331 unsigned i;
332 int ret;
333
334
2/2
✓ Branch 0 taken 30285 times.
✓ Branch 1 taken 37341 times.
67626 for (i = 0; i < ctx->nb_inputs; i++) {
335 30285 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30285 times.
30285 if (ret < 0)
337 return ret;
338 }
339
2/2
✓ Branch 0 taken 29182 times.
✓ Branch 1 taken 37341 times.
66523 for (i = 0; i < ctx->nb_outputs; i++) {
340 29182 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29182 times.
29182 if (ret < 0)
342 return ret;
343 }
344 37341 return 0;
345 }
346
347 44598 static int filter_query_formats(AVFilterContext *ctx)
348 {
349 44598 const FFFilter *const filter = fffilter(ctx->filter);
350 int ret;
351
352
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 44570 times.
44598 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
353
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
28 if ((ret = filter->formats.query_func(ctx)) < 0) {
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret != AVERROR(EAGAIN))
355 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
356 ctx->name, av_err2str(ret));
357 14 return ret;
358 }
359
2/2
✓ Branch 0 taken 37327 times.
✓ Branch 1 taken 7243 times.
44570 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
360 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
361 37327 AVFilterFormatsConfig **cfg_in_dyn = NULL, **cfg_out_dyn = NULL;
362 AVFilterFormatsConfig **cfg_in, **cfg_out;
363
364
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37326 times.
37327 if (ctx->nb_inputs > FF_ARRAY_ELEMS(cfg_in_stack)) {
365 1 cfg_in_dyn = av_malloc_array(ctx->nb_inputs, sizeof(*cfg_in_dyn));
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_in_dyn)
367 return AVERROR(ENOMEM);
368 1 cfg_in = cfg_in_dyn;
369 } else
370
2/2
✓ Branch 0 taken 30156 times.
✓ Branch 1 taken 7170 times.
37326 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
371
372
2/2
✓ Branch 0 taken 30270 times.
✓ Branch 1 taken 37327 times.
67597 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
373 30270 AVFilterLink *l = ctx->inputs[i];
374 30270 cfg_in[i] = &l->outcfg;
375 }
376
377
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37326 times.
37327 if (ctx->nb_outputs > FF_ARRAY_ELEMS(cfg_out_stack)) {
378 1 cfg_out_dyn = av_malloc_array(ctx->nb_outputs, sizeof(*cfg_out_dyn));
379
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_out_dyn) {
380 av_freep(&cfg_in_dyn);
381 return AVERROR(ENOMEM);
382 }
383 1 cfg_out = cfg_out_dyn;
384 } else
385
2/2
✓ Branch 0 taken 29096 times.
✓ Branch 1 taken 8230 times.
37326 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
386
387
2/2
✓ Branch 0 taken 29168 times.
✓ Branch 1 taken 37327 times.
66495 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
388 29168 AVFilterLink *l = ctx->outputs[i];
389 29168 cfg_out[i] = &l->incfg;
390 }
391
392 37327 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
393 37327 av_freep(&cfg_in_dyn);
394 37327 av_freep(&cfg_out_dyn);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37327 times.
37327 if (ret < 0) {
396 if (ret != AVERROR(EAGAIN))
397 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
398 ctx->name, av_err2str(ret));
399 return ret;
400 }
401 }
402
403
2/2
✓ Branch 0 taken 44570 times.
✓ Branch 1 taken 14 times.
44584 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
404
2/2
✓ Branch 0 taken 37327 times.
✓ Branch 1 taken 7243 times.
44570 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
405 37341 ret = filter_check_formats(ctx);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37341 times.
37341 if (ret < 0)
407 return ret;
408 }
409
410 44584 return ff_default_query_formats(ctx);
411 }
412
413 43389 static int formats_declared(AVFilterContext *f)
414 {
415 int i;
416
417
2/2
✓ Branch 0 taken 35108 times.
✓ Branch 1 taken 8314 times.
43422 for (i = 0; i < f->nb_inputs; i++) {
418
2/2
✓ Branch 0 taken 35073 times.
✓ Branch 1 taken 35 times.
35108 if (!f->inputs[i]->outcfg.formats)
419 35073 return 0;
420
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
35 if (f->inputs[i]->type == AVMEDIA_TYPE_VIDEO &&
421
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 !(f->inputs[i]->outcfg.color_ranges &&
422
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->inputs[i]->outcfg.color_spaces &&
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->inputs[i]->outcfg.alpha_modes))
424 2 return 0;
425
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
426
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(f->inputs[i]->outcfg.samplerates &&
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 f->inputs[i]->outcfg.channel_layouts))
428 return 0;
429 }
430
2/2
✓ Branch 0 taken 8304 times.
✓ Branch 1 taken 44 times.
8348 for (i = 0; i < f->nb_outputs; i++) {
431
2/2
✓ Branch 0 taken 8270 times.
✓ Branch 1 taken 34 times.
8304 if (!f->outputs[i]->incfg.formats)
432 8270 return 0;
433
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 3 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_VIDEO &&
434
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !(f->outputs[i]->incfg.color_ranges &&
435
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 f->outputs[i]->incfg.color_spaces &&
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->outputs[i]->incfg.alpha_modes))
437 return 0;
438
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
439
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !(f->outputs[i]->incfg.samplerates &&
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 f->outputs[i]->incfg.channel_layouts))
441 return 0;
442 }
443 44 return 1;
444 }
445
446 static void print_formats(void *log_ctx, int level, enum AVMediaType type,
447 const AVFilterFormats *formats)
448 {
449 AVBPrint bp;
450 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
451
452 switch (type) {
453 case AVMEDIA_TYPE_VIDEO:
454 for (unsigned i = 0; i < formats->nb_formats; i++)
455 av_bprintf(&bp, "%s%s", bp.len ? " " : "", av_get_pix_fmt_name(formats->formats[i]));
456 break;
457 case AVMEDIA_TYPE_AUDIO:
458 for (unsigned i = 0; i < formats->nb_formats; i++)
459 av_bprintf(&bp, "%s%s", bp.len ? " " : "", av_get_sample_fmt_name(formats->formats[i]));
460 break;
461 default:
462 av_bprintf(&bp, "(unknown)");
463 break;
464 }
465
466 if (av_bprint_is_complete(&bp)) {
467 av_log(log_ctx, level, "%s\n", bp.str);
468 } else {
469 av_log(log_ctx, level, "(out of memory)\n");
470 }
471 av_bprint_finalize(&bp, NULL);
472 }
473
474 static void print_link_formats(void *log_ctx, int level, const AVFilterLink *l)
475 {
476 if (av_log_get_level() < level)
477 return;
478
479 av_log(log_ctx, level, "Link '%s.%s' -> '%s.%s':\n"
480 " src: ", l->src->name, l->srcpad->name, l->dst->name, l->dstpad->name);
481 print_formats(log_ctx, level, l->type, l->incfg.formats);
482 av_log(log_ctx, level, " dst: ");
483 print_formats(log_ctx, level, l->type, l->outcfg.formats);
484 }
485
486 43331 static void print_filter_formats(void *log_ctx, int level, const AVFilterContext *f)
487 {
488
1/2
✓ Branch 1 taken 43331 times.
✗ Branch 2 not taken.
43331 if (av_log_get_level() < level)
489 43331 return;
490
491 av_log(log_ctx, level, "Filter '%s' formats:\n", f->name);
492 for (int i = 0; i < f->nb_inputs; i++) {
493 av_log(log_ctx, level, " in[%d] '%s': ", i, f->input_pads[i].name);
494 print_formats(log_ctx, level, f->inputs[i]->type, f->inputs[i]->outcfg.formats);
495 }
496 for (int i = 0; i < f->nb_outputs; i++) {
497 av_log(log_ctx, level, " out[%d] '%s': ", i, f->output_pads[i].name);
498 print_formats(log_ctx, level, f->outputs[i]->type, f->outputs[i]->incfg.formats);
499 }
500 }
501
502 /**
503 * Perform one round of query_formats() and merging formats lists on the
504 * filter graph.
505 * @return >=0 if all links formats lists could be queried and merged;
506 * AVERROR(EAGAIN) some progress was made in the queries or merging
507 * and a later call may succeed;
508 * AVERROR(EIO) (may be changed) plus a log message if no progress
509 * was made and the negotiation is stuck;
510 * a negative error code if some other error happened
511 */
512 8109 static int query_formats(AVFilterGraph *graph, void *log_ctx)
513 {
514 int i, j, k, ret;
515 8109 int converter_count = 0;
516 8109 int count_queried = 0; /* successful calls to query_formats() */
517 8109 int count_merged = 0; /* successful merge of formats lists */
518 8109 int count_already_merged = 0; /* lists already merged */
519 8109 int count_delayed = 0; /* lists that need to be merged later */
520
521
2/2
✓ Branch 0 taken 43389 times.
✓ Branch 1 taken 8109 times.
51498 for (i = 0; i < graph->nb_filters; i++) {
522 43389 AVFilterContext *f = graph->filters[i];
523
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 43345 times.
43389 if (formats_declared(f))
524 44 continue;
525 43345 ret = filter_query_formats(f);
526
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 43331 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
43345 if (ret < 0 && ret != AVERROR(EAGAIN))
527 return ret;
528 /* note: EAGAIN could indicate a partial success, not counted yet */
529
2/2
✓ Branch 0 taken 43331 times.
✓ Branch 1 taken 14 times.
43345 if (ret >= 0) {
530 43331 print_filter_formats(log_ctx, AV_LOG_DEBUG, f);
531 43331 count_queried++;
532 }
533 }
534
535 /* go through and merge as many format lists as possible */
536 8109 retry:
537
2/2
✓ Branch 0 taken 44642 times.
✓ Branch 1 taken 8109 times.
52751 for (i = 0; i < graph->nb_filters; i++) {
538 44642 AVFilterContext *filter = graph->filters[i];
539
540
2/2
✓ Branch 0 taken 36500 times.
✓ Branch 1 taken 44642 times.
81142 for (j = 0; j < filter->nb_inputs; j++) {
541 36500 AVFilterLink *link = filter->inputs[j];
542 const AVFilterNegotiation *neg;
543 AVFilterContext *conv[4];
544 36500 const char *conv_filters[4], *conv_opts[4] = {0};
545 36500 unsigned neg_step, num_conv = 0;
546
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36500 times.
36500 if (!link)
548 continue;
549
550 36500 neg = ff_filter_get_negotiation(link);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36500 times.
36500 av_assert0(neg);
552
2/2
✓ Branch 0 taken 140071 times.
✓ Branch 1 taken 36500 times.
176571 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
553 140071 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
554 140071 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
555 140071 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
556
8/8
✓ Branch 0 taken 140016 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 139960 times.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 136106 times.
✓ Branch 5 taken 3854 times.
✓ Branch 7 taken 1329 times.
✓ Branch 8 taken 134777 times.
140071 if (a && b && a != b && !m->can_merge(a, b)) {
557
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1253 times.
1329 for (k = 0; k < num_conv; k++) {
558
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (conv_filters[k] == m->conversion_filter)
559 76 break;
560 }
561
2/2
✓ Branch 0 taken 1253 times.
✓ Branch 1 taken 76 times.
1329 if (k == num_conv) {
562 av_assert1(num_conv < FF_ARRAY_ELEMS(conv_filters));
563 1253 conv_filters[num_conv] = m->conversion_filter;
564
1/2
✓ Branch 0 taken 1253 times.
✗ Branch 1 not taken.
1253 if (m->conversion_opts_offset)
565 1253 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
566 1253 num_conv++;
567 }
568 }
569 }
570
2/2
✓ Branch 0 taken 140071 times.
✓ Branch 1 taken 36500 times.
176571 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
571 140071 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
572 140071 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
573 140071 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
574
4/4
✓ Branch 0 taken 140016 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 139960 times.
140071 if (!(a && b)) {
575 111 count_delayed++;
576
2/2
✓ Branch 0 taken 9032 times.
✓ Branch 1 taken 130928 times.
139960 } else if (a == b) {
577 9032 count_already_merged++;
578
2/2
✓ Branch 0 taken 127130 times.
✓ Branch 1 taken 3798 times.
130928 } else if (!num_conv) {
579 127130 count_merged++;
580 127130 ret = m->merge(a, b);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127130 times.
127130 if (ret < 0)
582 return ret;
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127130 times.
127130 if (!ret) {
584 conv_filters[num_conv] = m->conversion_filter;
585 if (m->conversion_opts_offset)
586 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
587 num_conv++;
588 }
589 }
590 }
591
592 /**
593 * Couldn't merge format lists; auto-insert conversion filters
594 * in reverse order to keep the order consistent with the list
595 * of mergers, since they are prepended onto the existing link
596 */
597
2/2
✓ Branch 0 taken 1253 times.
✓ Branch 1 taken 36500 times.
37753 for (k = num_conv - 1; k >= 0; k--) {
598 const AVFilter *filter;
599 char inst_name[30];
600
601
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1253 times.
1253 if (fffiltergraph(graph)->disable_auto_convert) {
602 av_log(log_ctx, AV_LOG_ERROR,
603 "The filters '%s' and '%s' do not have a common format "
604 "and automatic conversion is disabled.\n",
605 link->src->name, link->dst->name);
606 print_link_formats(log_ctx, AV_LOG_ERROR, link);
607 return AVERROR(EINVAL);
608 }
609
610
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1253 times.
1253 if (!(filter = avfilter_get_by_name(conv_filters[k]))) {
611 av_log(log_ctx, AV_LOG_ERROR,
612 "'%s' filter not present, cannot convert formats.\n",
613 conv_filters[k]);
614 print_link_formats(log_ctx, AV_LOG_ERROR, link);
615 return AVERROR(EINVAL);
616 }
617 1253 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
618 conv_filters[k], converter_count++);
619 1253 ret = avfilter_graph_create_filter(&conv[k], filter, inst_name,
620 conv_opts[k], NULL, graph);
621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 if (ret < 0)
622 return ret;
623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1253 times.
1253 if ((ret = avfilter_insert_filter(link, conv[k], 0, 0)) < 0)
624 return ret;
625
626
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1253 times.
1253 if ((ret = filter_query_formats(conv[k])) < 0)
627 return ret;
628 }
629
630 /* preemptively settle formats of auto filters */
631
2/2
✓ Branch 0 taken 1253 times.
✓ Branch 1 taken 36500 times.
37753 for (k = 0; k < num_conv; k++) {
632 1253 AVFilterLink *inlink = conv[k]->inputs[0];
633 1253 AVFilterLink *outlink = conv[k]->outputs[0];
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 av_assert0( inlink->incfg.formats->refcount > 0);
635
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 av_assert0( inlink->outcfg.formats->refcount > 0);
636
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 av_assert0(outlink->incfg.formats->refcount > 0);
637
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1253 times.
1253 av_assert0(outlink->outcfg.formats->refcount > 0);
638
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 631 times.
1253 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
640
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
642
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
646
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
647
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink-> incfg.alpha_modes->refcount > 0);
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink->outcfg.alpha_modes->refcount > 0);
649
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink-> incfg.alpha_modes->refcount > 0);
650
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink->outcfg.alpha_modes->refcount > 0);
651
1/2
✓ Branch 0 taken 631 times.
✗ Branch 1 not taken.
631 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0( inlink-> incfg.samplerates->refcount > 0);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0( inlink->outcfg.samplerates->refcount > 0);
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0(outlink-> incfg.samplerates->refcount > 0);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0(outlink->outcfg.samplerates->refcount > 0);
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 631 times.
631 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
660 }
661
662 #define MERGE(merger, link) \
663 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
664 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
665
666
2/2
✓ Branch 0 taken 4381 times.
✓ Branch 1 taken 1253 times.
5634 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
667 4381 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
668
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 3759 times.
4381 if (m->conversion_filter != conv_filters[k])
669 622 continue;
670
2/4
✓ Branch 1 taken 3759 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3759 times.
7518 if ((ret = MERGE(m, inlink)) <= 0 ||
671 3759 (ret = MERGE(m, outlink)) <= 0) {
672 if (ret < 0)
673 return ret;
674 av_log(log_ctx, AV_LOG_ERROR,
675 "Impossible to convert between the formats supported by the filter "
676 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
677 print_link_formats(log_ctx, AV_LOG_ERROR, link);
678 return AVERROR(ENOSYS);
679 } else {
680 3759 count_merged += 2;
681 }
682 }
683 }
684
685 /* if there is more than one auto filter, we may need another round
686 * to fully settle formats due to possible cross-incompatibilities
687 * between the auto filters themselves */
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36500 times.
36500 if (num_conv > 1)
689 goto retry;
690 }
691 }
692
693 8109 av_log(graph, AV_LOG_DEBUG, "query_formats: "
694 "%d queried, %d merged, %d already done, %d delayed\n",
695 count_queried, count_merged, count_already_merged, count_delayed);
696
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8095 times.
8109 if (count_delayed) {
697 AVBPrint bp;
698
699 /* if count_queried > 0, one filter at least did set its formats,
700 that will give additional information to its neighbour;
701 if count_merged > 0, one pair of formats lists at least was merged,
702 that will give additional information to all connected filters;
703 in both cases, progress was made and a new round must be done */
704
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)
705 14 return AVERROR(EAGAIN);
706 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
707 for (i = 0; i < graph->nb_filters; i++)
708 if (!formats_declared(graph->filters[i]))
709 av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
710 graph->filters[i]->name);
711 av_log(graph, AV_LOG_ERROR,
712 "The following filters could not choose their formats: %s\n"
713 "Consider inserting the (a)format filter near their input or "
714 "output.\n", bp.str);
715 return AVERROR(EIO);
716 }
717 8095 return 0;
718 }
719
720 1130 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
721 {
722 1130 int score = 0;
723
724
2/2
✓ Branch 2 taken 1035 times.
✓ Branch 3 taken 95 times.
1130 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
725 1035 score ++;
726
727
2/2
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 876 times.
1130 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
728 254 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
729 }else
730 876 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
731
732
3/4
✓ Branch 1 taken 139 times.
✓ Branch 2 taken 991 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 139 times.
1269 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
733 139 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
734 score += 20;
735
736
3/4
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 1035 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 95 times.
1225 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
737 95 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
738 score += 2;
739
740 1130 return score;
741 }
742
743 565 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
744 enum AVSampleFormat src_fmt)
745 {
746 int score1, score2;
747
748 565 score1 = get_fmt_score(dst_fmt1, src_fmt);
749 565 score2 = get_fmt_score(dst_fmt2, src_fmt);
750
751
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 140 times.
565 return score1 < score2 ? dst_fmt1 : dst_fmt2;
752 }
753
754 36248 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
755 {
756 36248 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36248 times.
36248 if (!desc)
758 return 0;
759
2/2
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 34478 times.
36248 if (desc->nb_components < 3)
760 1770 return 0; /* Grayscale is explicitly full-range in swscale */
761 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
762 34478 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
763 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
764 }
765
766
767 25846 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
768 {
769
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 25094 times.
25846 switch (fmt) {
770 752 case AV_PIX_FMT_YUVJ420P:
771 case AV_PIX_FMT_YUVJ422P:
772 case AV_PIX_FMT_YUVJ444P:
773 case AV_PIX_FMT_YUVJ440P:
774 case AV_PIX_FMT_YUVJ411P:
775 752 return 1;
776 25094 default:
777 25094 return 0;
778 }
779 }
780
781 109304 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
782 {
783
3/4
✓ Branch 0 taken 109304 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72852 times.
✓ Branch 3 taken 36452 times.
109304 if (!link || !link->incfg.formats)
784 72852 return 0;
785
786
2/2
✓ Branch 0 taken 30527 times.
✓ Branch 1 taken 5925 times.
36452 if (link->type == AVMEDIA_TYPE_VIDEO) {
787
3/4
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 30427 times.
✓ Branch 2 taken 100 times.
✗ Branch 3 not taken.
30527 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
788 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
789 100 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
790 100 enum AVPixelFormat best= AV_PIX_FMT_NONE;
791 int i;
792
2/2
✓ Branch 0 taken 1073 times.
✓ Branch 1 taken 100 times.
1173 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
793 1073 enum AVPixelFormat p = link->incfg.formats->formats[i];
794 1073 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
795 }
796 100 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
797 100 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
798 100 av_get_pix_fmt_name(ref->format), has_alpha);
799 100 link->incfg.formats->formats[0] = best;
800 }
801
1/2
✓ Branch 0 taken 5925 times.
✗ Branch 1 not taken.
5925 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
802
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 5785 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
5925 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
803 140 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
804 int i;
805
2/2
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 140 times.
705 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
806 565 enum AVSampleFormat p = link->incfg.formats->formats[i];
807 565 best = find_best_sample_fmt_of_2(best, p, ref->format);
808 }
809 140 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
810 140 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
811 140 av_get_sample_fmt_name(ref->format));
812 140 link->incfg.formats->formats[0] = best;
813 }
814 }
815
816 36452 link->incfg.formats->nb_formats = 1;
817 36452 link->format = link->incfg.formats->formats[0];
818
819
2/2
✓ Branch 0 taken 30527 times.
✓ Branch 1 taken 5925 times.
36452 if (link->type == AVMEDIA_TYPE_VIDEO) {
820 30527 enum AVPixelFormat swfmt = link->format;
821
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30527 times.
30527 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
822 // FIXME: this is a hack - we'd like to use the sw_format of
823 // link->hw_frames_ctx here, but it is not yet available.
824 // To make this work properly we will need to either reorder
825 // things so that it is available here or somehow negotiate
826 // sw_format separately.
827 swfmt = AV_PIX_FMT_YUV420P;
828 }
829
830 30527 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
831
2/2
✓ Branch 1 taken 9697 times.
✓ Branch 2 taken 20830 times.
30527 if (!ff_fmt_is_regular_yuv(swfmt)) {
832 /* These fields are explicitly documented as affecting YUV only,
833 * so set them to sane values for other formats. */
834
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 9126 times.
9697 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
835 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
836 else
837 9126 link->color_range = AVCOL_RANGE_JPEG;
838
2/2
✓ Branch 0 taken 8121 times.
✓ Branch 1 taken 1576 times.
9697 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
839 8121 link->colorspace = AVCOL_SPC_RGB;
840 } else {
841 1576 link->colorspace = AVCOL_SPC_UNSPECIFIED;
842 }
843 } else {
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20830 times.
20830 if (!link->incfg.color_spaces->nb_formats) {
845 av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
846 " the link between filters %s and %s.\n", link->src->name,
847 link->dst->name);
848 return AVERROR(EINVAL);
849 }
850 20830 link->incfg.color_spaces->nb_formats = 1;
851 20830 link->colorspace = link->incfg.color_spaces->formats[0];
852
853
2/2
✓ Branch 1 taken 678 times.
✓ Branch 2 taken 20152 times.
20830 if (ff_fmt_is_forced_full_range(swfmt)) {
854 678 link->color_range = AVCOL_RANGE_JPEG;
855 } else {
856
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20152 times.
20152 if (!link->incfg.color_ranges->nb_formats) {
857 av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
858 " the link between filters %s and %s.\n", link->src->name,
859 link->dst->name);
860 return AVERROR(EINVAL);
861 }
862 20152 link->incfg.color_ranges->nb_formats = 1;
863 20152 link->color_range = link->incfg.color_ranges->formats[0];
864 }
865 }
866
867
2/2
✓ Branch 0 taken 3764 times.
✓ Branch 1 taken 26763 times.
30527 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) {
868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3764 times.
3764 if (!link->incfg.alpha_modes->nb_formats) {
869 av_log(link->src, AV_LOG_ERROR, "Cannot select alpha mode for"
870 " the link between filters %s and %s.\n", link->src->name,
871 link->dst->name);
872 return AVERROR(EINVAL);
873 }
874 3764 link->incfg.alpha_modes->nb_formats = 1;
875 3764 link->alpha_mode = link->incfg.alpha_modes->formats[0];
876 } else {
877 26763 link->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
878 }
879
1/2
✓ Branch 0 taken 5925 times.
✗ Branch 1 not taken.
5925 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
880 int ret;
881
882
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5925 times.
5925 if (!link->incfg.samplerates->nb_formats) {
883 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
884 " the link between filters %s and %s.\n", link->src->name,
885 link->dst->name);
886 return AVERROR(EINVAL);
887 }
888 5925 link->incfg.samplerates->nb_formats = 1;
889 5925 link->sample_rate = link->incfg.samplerates->formats[0];
890
891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5925 times.
5925 if (link->incfg.channel_layouts->all_layouts) {
892 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
893 " the link between filters %s and %s.\n", link->src->name,
894 link->dst->name);
895 if (!link->incfg.channel_layouts->all_counts)
896 av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
897 "supported, try specifying a channel layout using "
898 "'aformat=channel_layouts=something'.\n");
899 return AVERROR(EINVAL);
900 }
901 5925 link->incfg.channel_layouts->nb_channel_layouts = 1;
902 5925 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5925 times.
5925 if (ret < 0)
904 return ret;
905 }
906
907 36452 ff_formats_unref(&link->incfg.formats);
908 36452 ff_formats_unref(&link->outcfg.formats);
909 36452 ff_formats_unref(&link->incfg.samplerates);
910 36452 ff_formats_unref(&link->outcfg.samplerates);
911 36452 ff_channel_layouts_unref(&link->incfg.channel_layouts);
912 36452 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
913 36452 ff_formats_unref(&link->incfg.color_spaces);
914 36452 ff_formats_unref(&link->outcfg.color_spaces);
915 36452 ff_formats_unref(&link->incfg.color_ranges);
916 36452 ff_formats_unref(&link->outcfg.color_ranges);
917 36452 ff_formats_unref(&link->incfg.alpha_modes);
918 36452 ff_formats_unref(&link->outcfg.alpha_modes);
919
920 36452 return 0;
921 }
922
923 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
924 do { \
925 for (i = 0; i < filter->nb_inputs; i++) { \
926 AVFilterLink *link = filter->inputs[i]; \
927 fmt_type fmt; \
928 \
929 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
930 continue; \
931 fmt = link->outcfg.list->var[0]; \
932 \
933 for (j = 0; j < filter->nb_outputs; j++) { \
934 AVFilterLink *out_link = filter->outputs[j]; \
935 list_type *fmts; \
936 \
937 if (link->type != out_link->type || \
938 out_link->incfg.list->nb == 1) \
939 continue; \
940 fmts = out_link->incfg.list; \
941 \
942 if (!out_link->incfg.list->nb) { \
943 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
944 return ret; \
945 ret = 1; \
946 break; \
947 } \
948 \
949 for (k = 0; k < out_link->incfg.list->nb; k++) \
950 if (fmts->var[k] == fmt) { \
951 fmts->var[0] = fmt; \
952 fmts->nb = 1; \
953 ret = 1; \
954 break; \
955 } \
956 } \
957 } \
958 } while (0)
959
960 71463 static int reduce_formats_on_filter(AVFilterContext *filter)
961 {
962 71463 int i, j, k, ret = 0;
963
964
16/20
✓ Branch 0 taken 58711 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1375 times.
✓ Branch 3 taken 57336 times.
✓ Branch 4 taken 44813 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 44259 times.
✓ Branch 7 taken 554 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 554 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 51 times.
✓ Branch 14 taken 4754 times.
✓ Branch 15 taken 4805 times.
✓ Branch 16 taken 503 times.
✓ Branch 17 taken 44825 times.
✓ Branch 18 taken 57336 times.
✓ Branch 19 taken 58711 times.
✓ Branch 20 taken 71463 times.
179753 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
965 nb_formats, ff_add_format);
966
18/20
✓ Branch 0 taken 10735 times.
✓ Branch 1 taken 47976 times.
✓ Branch 2 taken 1175 times.
✓ Branch 3 taken 9560 times.
✓ Branch 4 taken 7746 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6977 times.
✓ Branch 7 taken 769 times.
✓ Branch 8 taken 733 times.
✓ Branch 9 taken 36 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 733 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 68 times.
✓ Branch 15 taken 104 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 7752 times.
✓ Branch 18 taken 8827 times.
✓ Branch 19 taken 58711 times.
✓ Branch 20 taken 71463 times.
137261 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
967 nb_formats, ff_add_format);
968
15/20
✓ Branch 0 taken 47976 times.
✓ Branch 1 taken 10735 times.
✓ Branch 2 taken 11013 times.
✓ Branch 3 taken 36963 times.
✓ Branch 4 taken 28978 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25064 times.
✓ Branch 7 taken 3914 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3914 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3914 times.
✓ Branch 14 taken 425 times.
✓ Branch 15 taken 4339 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 28978 times.
✓ Branch 18 taken 36963 times.
✓ Branch 19 taken 58711 times.
✓ Branch 20 taken 71463 times.
159577 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
969 nb_formats, ff_add_format);
970
15/20
✓ Branch 0 taken 47976 times.
✓ Branch 1 taken 10735 times.
✓ Branch 2 taken 8478 times.
✓ Branch 3 taken 39498 times.
✓ Branch 4 taken 30261 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26950 times.
✓ Branch 7 taken 3311 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3311 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3311 times.
✓ Branch 14 taken 3292 times.
✓ Branch 15 taken 6603 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 30261 times.
✓ Branch 18 taken 39498 times.
✓ Branch 19 taken 58711 times.
✓ Branch 20 taken 71463 times.
163727 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
971 nb_formats, ff_add_format);
972
10/20
✓ Branch 0 taken 47976 times.
✓ Branch 1 taken 10735 times.
✓ Branch 2 taken 47477 times.
✓ Branch 3 taken 499 times.
✓ Branch 4 taken 373 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 373 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 373 times.
✓ Branch 18 taken 499 times.
✓ Branch 19 taken 58711 times.
✓ Branch 20 taken 71463 times.
130547 REDUCE_FORMATS(int, AVFilterFormats, alpha_modes, formats,
973 nb_formats, ff_add_format);
974
975 /* reduce channel layouts */
976
2/2
✓ Branch 0 taken 58711 times.
✓ Branch 1 taken 71463 times.
130174 for (i = 0; i < filter->nb_inputs; i++) {
977 58711 AVFilterLink *inlink = filter->inputs[i];
978 const AVChannelLayout *fmt;
979
980
2/2
✓ Branch 0 taken 10735 times.
✓ Branch 1 taken 47976 times.
58711 if (!inlink->outcfg.channel_layouts ||
981
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 10658 times.
10735 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
982 48053 continue;
983 10658 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
984
985
2/2
✓ Branch 0 taken 8466 times.
✓ Branch 1 taken 9814 times.
18280 for (j = 0; j < filter->nb_outputs; j++) {
986 8466 AVFilterLink *outlink = filter->outputs[j];
987 AVFilterChannelLayouts *fmts;
988
989 8466 fmts = outlink->incfg.channel_layouts;
990
4/4
✓ Branch 0 taken 8460 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7575 times.
✓ Branch 3 taken 885 times.
8466 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
991 7581 continue;
992
993
2/2
✓ Branch 0 taken 844 times.
✓ Branch 1 taken 41 times.
885 if (fmts->all_layouts &&
994
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 837 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
844 (KNOWN(fmt) || fmts->all_counts)) {
995 /* Turn the infinite list into a singleton */
996 844 fmts->all_layouts = fmts->all_counts = 0;
997 844 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 844 times.
844 if (ret < 0)
999 return ret;
1000 844 ret = 1;
1001 844 break;
1002 }
1003
1004
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 6 times.
128 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
1005
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 87 times.
122 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
1006 35 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
1007
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0)
1008 return ret;
1009 35 fmts->nb_channel_layouts = 1;
1010 35 ret = 1;
1011 35 break;
1012 }
1013 }
1014 }
1015 }
1016
1017 71463 return ret;
1018 }
1019
1020 8095 static int reduce_formats(AVFilterGraph *graph)
1021 {
1022 int i, reduced, ret;
1023
1024 do {
1025 12682 reduced = 0;
1026
1027
2/2
✓ Branch 0 taken 71463 times.
✓ Branch 1 taken 12682 times.
84145 for (i = 0; i < graph->nb_filters; i++) {
1028
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 71463 times.
71463 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
1029 return ret;
1030 71463 reduced |= ret;
1031 }
1032
2/2
✓ Branch 0 taken 4587 times.
✓ Branch 1 taken 8095 times.
12682 } while (reduced);
1033
1034 8095 return 0;
1035 }
1036
1037 44584 static void swap_samplerates_on_filter(AVFilterContext *filter)
1038 {
1039 44584 AVFilterLink *link = NULL;
1040 int sample_rate;
1041 int i, j;
1042
1043
2/2
✓ Branch 0 taken 36368 times.
✓ Branch 1 taken 38739 times.
75107 for (i = 0; i < filter->nb_inputs; i++) {
1044 36368 link = filter->inputs[i];
1045
1046
2/2
✓ Branch 0 taken 5845 times.
✓ Branch 1 taken 30523 times.
36368 if (link->type == AVMEDIA_TYPE_AUDIO &&
1047
1/2
✓ Branch 0 taken 5845 times.
✗ Branch 1 not taken.
5845 link->outcfg.samplerates->nb_formats== 1)
1048 5845 break;
1049 }
1050
2/2
✓ Branch 0 taken 38739 times.
✓ Branch 1 taken 5845 times.
44584 if (i == filter->nb_inputs)
1051 38739 return;
1052
1053 5845 sample_rate = link->outcfg.samplerates->formats[0];
1054
1055
2/2
✓ Branch 0 taken 4557 times.
✓ Branch 1 taken 5845 times.
10402 for (i = 0; i < filter->nb_outputs; i++) {
1056 4557 AVFilterLink *outlink = filter->outputs[i];
1057 4557 int best_idx, best_diff = INT_MAX;
1058
1059
2/2
✓ Branch 0 taken 4555 times.
✓ Branch 1 taken 2 times.
4557 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1060
1/2
✓ Branch 0 taken 4555 times.
✗ Branch 1 not taken.
4555 outlink->incfg.samplerates->nb_formats < 2)
1061 4557 continue;
1062
1063 for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
1064 int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
1065
1066 av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
1067
1068 if (diff < best_diff) {
1069 best_diff = diff;
1070 best_idx = j;
1071 }
1072 }
1073 FFSWAP(int, outlink->incfg.samplerates->formats[0],
1074 outlink->incfg.samplerates->formats[best_idx]);
1075 }
1076 }
1077
1078 8095 static void swap_samplerates(AVFilterGraph *graph)
1079 {
1080 int i;
1081
1082
2/2
✓ Branch 0 taken 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++)
1083 44584 swap_samplerates_on_filter(graph->filters[i]);
1084 8095 }
1085
1086 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
1087 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
1088 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
1089 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
1090 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
1091 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
1092 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
1093
1094 /* allowable substitutions for channel pairs when comparing layouts,
1095 * ordered by priority for both values */
1096 static const uint64_t ch_subst[][2] = {
1097 { CH_FRONT_PAIR, CH_CENTER_PAIR },
1098 { CH_FRONT_PAIR, CH_WIDE_PAIR },
1099 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
1100 { CH_CENTER_PAIR, CH_FRONT_PAIR },
1101 { CH_CENTER_PAIR, CH_WIDE_PAIR },
1102 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
1103 { CH_WIDE_PAIR, CH_FRONT_PAIR },
1104 { CH_WIDE_PAIR, CH_CENTER_PAIR },
1105 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
1106 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
1107 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
1108 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
1109 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
1110 { CH_SIDE_PAIR, CH_BACK_PAIR },
1111 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
1112 { CH_BACK_PAIR, CH_DIRECT_PAIR },
1113 { CH_BACK_PAIR, CH_SIDE_PAIR },
1114 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
1115 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
1116 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
1117 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
1118 };
1119
1120 44584 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1121 {
1122 44584 AVFilterLink *link = NULL;
1123 int i, j, k;
1124
1125
2/2
✓ Branch 0 taken 36368 times.
✓ Branch 1 taken 38745 times.
75113 for (i = 0; i < filter->nb_inputs; i++) {
1126 36368 link = filter->inputs[i];
1127
1128
2/2
✓ Branch 0 taken 5845 times.
✓ Branch 1 taken 30523 times.
36368 if (link->type == AVMEDIA_TYPE_AUDIO &&
1129
2/2
✓ Branch 0 taken 5839 times.
✓ Branch 1 taken 6 times.
5845 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1130 5839 break;
1131 }
1132
2/2
✓ Branch 0 taken 38745 times.
✓ Branch 1 taken 5839 times.
44584 if (i == filter->nb_inputs)
1133 38745 return;
1134
1135
2/2
✓ Branch 0 taken 4554 times.
✓ Branch 1 taken 5839 times.
10393 for (i = 0; i < filter->nb_outputs; i++) {
1136 4554 AVFilterLink *outlink = filter->outputs[i];
1137 4554 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1138
1139
2/2
✓ Branch 0 taken 4552 times.
✓ Branch 1 taken 2 times.
4554 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1140
2/2
✓ Branch 0 taken 4549 times.
✓ Branch 1 taken 3 times.
4552 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1141 4551 continue;
1142
1143
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 3 times.
37 for (j = 0; j < outlink->incfg.channel_layouts->nb_channel_layouts; j++) {
1144 34 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1145 int in_channels;
1146 int out_channels;
1147 int count_diff;
1148 int matched_channels, extra_channels;
1149 34 int score = 100000;
1150
1151 34 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1152 34 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1153 34 in_channels = in_chlayout.nb_channels;
1154 34 out_channels = out_chlayout.nb_channels;
1155 34 count_diff = out_channels - in_channels;
1156
4/8
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
34 if (!KNOWN(&in_chlayout) || !KNOWN(&out_chlayout)) {
1157 /* Compute score in case the input or output layout encodes
1158 a channel count; in this case the score is not altered by
1159 the computation afterwards, as in_chlayout and
1160 out_chlayout have both been set to 0 */
1161
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))
1162
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1163
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1164 out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1165 32 score -= 10000 + FFABS(out_channels - in_channels) +
1166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1167 16 av_channel_layout_uninit(&in_chlayout);
1168 16 av_channel_layout_uninit(&out_chlayout);
1169 /* Let the remaining computation run, even if the score
1170 value is not altered */
1171 }
1172
1173 /* channel substitution */
1174
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 34 times.
748 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1175 714 uint64_t cmp0 = ch_subst[k][0];
1176 714 uint64_t cmp1 = ch_subst[k][1];
1177
4/4
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 498 times.
✓ Branch 3 taken 120 times.
✓ Branch 4 taken 96 times.
930 if ( av_channel_layout_subset(& in_chlayout, cmp0) &&
1178
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 100 times.
336 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1179
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
140 av_channel_layout_subset(&out_chlayout, cmp1) &&
1180 20 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1181 4 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1182 4 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1183 /* add score for channel match, minus a deduction for
1184 having to do the substitution */
1185 4 score += 10 * av_popcount64(cmp1) - 2;
1186 }
1187 }
1188
1189 /* no penalty for LFE channel mismatch */
1190
4/4
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 11 times.
52 if (av_channel_layout_index_from_channel(&in_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0 &&
1191 18 av_channel_layout_index_from_channel(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1192 7 score += 10;
1193 34 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1194 34 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1195
1196 34 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1197 34 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1198 34 score += 10 * matched_channels - 5 * extra_channels;
1199
1200
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 20 times.
34 if (score > best_score ||
1201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (count_diff < best_count_diff && score == best_score)) {
1202 10 best_score = score;
1203 10 best_idx = j;
1204 10 best_count_diff = count_diff;
1205 }
1206 }
1207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(best_idx >= 0);
1208 3 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1209 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1210 }
1211
1212 }
1213
1214 8095 static void swap_channel_layouts(AVFilterGraph *graph)
1215 {
1216 int i;
1217
1218
2/2
✓ Branch 0 taken 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++)
1219 44584 swap_channel_layouts_on_filter(graph->filters[i]);
1220 8095 }
1221
1222 44584 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1223 {
1224 44584 AVFilterLink *link = NULL;
1225 int format, bps;
1226 int i, j;
1227
1228
2/2
✓ Branch 0 taken 36437 times.
✓ Branch 1 taken 39130 times.
75567 for (i = 0; i < filter->nb_inputs; i++) {
1229 36437 link = filter->inputs[i];
1230
1231
2/2
✓ Branch 0 taken 5914 times.
✓ Branch 1 taken 30523 times.
36437 if (link->type == AVMEDIA_TYPE_AUDIO &&
1232
2/2
✓ Branch 0 taken 5454 times.
✓ Branch 1 taken 460 times.
5914 link->outcfg.formats->nb_formats == 1)
1233 5454 break;
1234 }
1235
2/2
✓ Branch 0 taken 39130 times.
✓ Branch 1 taken 5454 times.
44584 if (i == filter->nb_inputs)
1236 39130 return;
1237
1238 5454 format = link->outcfg.formats->formats[0];
1239 5454 bps = av_get_bytes_per_sample(format);
1240
1241
2/2
✓ Branch 0 taken 4176 times.
✓ Branch 1 taken 5454 times.
9630 for (i = 0; i < filter->nb_outputs; i++) {
1242 4176 AVFilterLink *outlink = filter->outputs[i];
1243 4176 int best_idx = -1, best_score = INT_MIN;
1244
1245
2/2
✓ Branch 0 taken 4174 times.
✓ Branch 1 taken 2 times.
4176 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1246
2/2
✓ Branch 0 taken 4016 times.
✓ Branch 1 taken 158 times.
4174 outlink->incfg.formats->nb_formats < 2)
1247 4018 continue;
1248
1249
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 7 times.
300 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1250 293 int out_format = outlink->incfg.formats->formats[j];
1251 293 int out_bps = av_get_bytes_per_sample(out_format);
1252 int score;
1253
1254
4/4
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 142 times.
449 if (av_get_packed_sample_fmt(out_format) == format ||
1255 156 av_get_planar_sample_fmt(out_format) == format) {
1256 151 best_idx = j;
1257 151 break;
1258 }
1259
1260 /* for s32 and float prefer double to prevent loss of information */
1261
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
142 if (bps == 4 && out_bps == 8) {
1262 best_idx = j;
1263 break;
1264 }
1265
1266 /* prefer closest higher or equal bps */
1267 142 score = -abs(out_bps - bps);
1268
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 114 times.
142 if (out_bps >= bps)
1269 28 score += INT_MAX/2;
1270
1271
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 33 times.
142 if (score > best_score) {
1272 109 best_score = score;
1273 109 best_idx = j;
1274 }
1275 }
1276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 av_assert0(best_idx >= 0);
1277 158 FFSWAP(int, outlink->incfg.formats->formats[0],
1278 outlink->incfg.formats->formats[best_idx]);
1279 }
1280 }
1281
1282 8095 static void swap_sample_fmts(AVFilterGraph *graph)
1283 {
1284 int i;
1285
1286
2/2
✓ Branch 0 taken 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++)
1287 44584 swap_sample_fmts_on_filter(graph->filters[i]);
1288
1289 8095 }
1290
1291 8095 static int pick_formats(AVFilterGraph *graph)
1292 {
1293 int i, j, ret;
1294 int change;
1295
1296 do{
1297 16232 change = 0;
1298
2/2
✓ Branch 0 taken 90110 times.
✓ Branch 1 taken 16232 times.
106342 for (i = 0; i < graph->nb_filters; i++) {
1299 90110 AVFilterContext *filter = graph->filters[i];
1300
2/2
✓ Branch 0 taken 73416 times.
✓ Branch 1 taken 16694 times.
90110 if (filter->nb_inputs){
1301
2/2
✓ Branch 0 taken 73767 times.
✓ Branch 1 taken 73416 times.
147183 for (j = 0; j < filter->nb_inputs; j++){
1302
4/4
✓ Branch 0 taken 15565 times.
✓ Branch 1 taken 58202 times.
✓ Branch 2 taken 15071 times.
✓ Branch 3 taken 494 times.
73767 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15071 times.
15071 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1304 return ret;
1305 15071 change = 1;
1306 }
1307 }
1308 }
1309
2/2
✓ Branch 0 taken 73499 times.
✓ Branch 1 taken 16611 times.
90110 if (filter->nb_outputs){
1310
2/2
✓ Branch 0 taken 73767 times.
✓ Branch 1 taken 73499 times.
147266 for (j = 0; j < filter->nb_outputs; j++){
1311
4/4
✓ Branch 0 taken 21690 times.
✓ Branch 1 taken 52077 times.
✓ Branch 2 taken 21089 times.
✓ Branch 3 taken 601 times.
73767 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21089 times.
21089 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1313 return ret;
1314 21089 change = 1;
1315 }
1316 }
1317 }
1318
6/6
✓ Branch 0 taken 73416 times.
✓ Branch 1 taken 16694 times.
✓ Branch 2 taken 56805 times.
✓ Branch 3 taken 16611 times.
✓ Branch 4 taken 56535 times.
✓ Branch 5 taken 270 times.
90110 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1319
2/2
✓ Branch 0 taken 56735 times.
✓ Branch 1 taken 56535 times.
113270 for (j = 0; j < filter->nb_outputs; j++) {
1320
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 56495 times.
56735 if (filter->outputs[j]->format<0) {
1321
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
240 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1322 return ret;
1323 240 change = 1;
1324 }
1325 }
1326 }
1327 }
1328
2/2
✓ Branch 0 taken 8137 times.
✓ Branch 1 taken 8095 times.
16232 }while(change);
1329
1330
2/2
✓ Branch 0 taken 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++) {
1331 44584 AVFilterContext *filter = graph->filters[i];
1332
1333
2/2
✓ Branch 0 taken 36452 times.
✓ Branch 1 taken 44584 times.
81036 for (j = 0; j < filter->nb_inputs; j++)
1334
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36452 times.
36452 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1335 return ret;
1336
2/2
✓ Branch 0 taken 36452 times.
✓ Branch 1 taken 44584 times.
81036 for (j = 0; j < filter->nb_outputs; j++)
1337
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36452 times.
36452 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1338 return ret;
1339 }
1340 8095 return 0;
1341 }
1342
1343 /**
1344 * Configure the formats of all the links in the graph.
1345 */
1346 8095 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1347 {
1348 int ret;
1349
1350 /* find supported formats from sub-filters, and merge along links */
1351
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 8095 times.
8109 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1352 14 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8095 times.
8095 if (ret < 0)
1354 return ret;
1355
1356 /* Once everything is merged, it's possible that we'll still have
1357 * multiple valid media format choices. We try to minimize the amount
1358 * of format conversion inside filters */
1359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = reduce_formats(graph)) < 0)
1360 return ret;
1361
1362 /* for audio filters, ensure the best format, sample rate and channel layout
1363 * is selected */
1364 8095 swap_sample_fmts(graph);
1365 8095 swap_samplerates(graph);
1366 8095 swap_channel_layouts(graph);
1367
1368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = pick_formats(graph)) < 0)
1369 return ret;
1370
1371 8095 return 0;
1372 }
1373
1374 8095 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1375 {
1376 unsigned i, j;
1377 8095 int sink_links_count = 0, n = 0;
1378 AVFilterContext *f;
1379 FilterLinkInternal **sinks;
1380
1381
2/2
✓ Branch 0 taken 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++) {
1382 44584 f = graph->filters[i];
1383
2/2
✓ Branch 0 taken 36452 times.
✓ Branch 1 taken 44584 times.
81036 for (j = 0; j < f->nb_inputs; j++) {
1384 36452 ff_link_internal(f->inputs[j])->age_index = -1;
1385 }
1386
2/2
✓ Branch 0 taken 36452 times.
✓ Branch 1 taken 44584 times.
81036 for (j = 0; j < f->nb_outputs; j++) {
1387 36452 ff_link_internal(f->outputs[j])->age_index = -1;
1388 }
1389
2/2
✓ Branch 0 taken 8231 times.
✓ Branch 1 taken 36353 times.
44584 if (!f->nb_outputs) {
1390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8231 times.
8231 if (f->nb_inputs > INT_MAX - sink_links_count)
1391 return AVERROR(EINVAL);
1392 8231 sink_links_count += f->nb_inputs;
1393 }
1394 }
1395 8095 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8095 times.
8095 if (!sinks)
1397 return AVERROR(ENOMEM);
1398
2/2
✓ Branch 0 taken 44584 times.
✓ Branch 1 taken 8095 times.
52679 for (i = 0; i < graph->nb_filters; i++) {
1399 44584 f = graph->filters[i];
1400
2/2
✓ Branch 0 taken 8231 times.
✓ Branch 1 taken 36353 times.
44584 if (!f->nb_outputs) {
1401
2/2
✓ Branch 0 taken 8231 times.
✓ Branch 1 taken 8231 times.
16462 for (j = 0; j < f->nb_inputs; j++) {
1402 8231 sinks[n] = ff_link_internal(f->inputs[j]);
1403 8231 sinks[n]->age_index = n;
1404 8231 n++;
1405 }
1406 }
1407 }
1408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8095 times.
8095 av_assert0(n == sink_links_count);
1409 8095 fffiltergraph(graph)->sink_links = sinks;
1410 8095 fffiltergraph(graph)->sink_links_count = sink_links_count;
1411 8095 return 0;
1412 }
1413
1414 8095 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1415 {
1416 int ret;
1417
1418
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8094 times.
8095 if (graphctx->max_buffered_frames)
1419 1 fffiltergraph(graphctx)->frame_queues.max_queued = graphctx->max_buffered_frames;
1420
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = graph_check_validity(graphctx, log_ctx)))
1421 return ret;
1422
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = graph_config_formats(graphctx, log_ctx)))
1423 return ret;
1424
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = graph_config_links(graphctx, log_ctx)))
1425 return ret;
1426
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = graph_check_links(graphctx, log_ctx)))
1427 return ret;
1428
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8095 times.
8095 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1429 return ret;
1430
1431 8095 return 0;
1432 }
1433
1434 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1435 {
1436 int i, r = AVERROR(ENOSYS);
1437
1438 if (!graph)
1439 return r;
1440
1441 if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1442 r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1443 if (r != AVERROR(ENOSYS))
1444 return r;
1445 }
1446
1447 if (res_len && res)
1448 res[0] = 0;
1449
1450 for (i = 0; i < graph->nb_filters; i++) {
1451 AVFilterContext *filter = graph->filters[i];
1452 if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1453 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1454 if (r != AVERROR(ENOSYS)) {
1455 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1456 return r;
1457 }
1458 }
1459 }
1460
1461 return r;
1462 }
1463
1464 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1465 {
1466 int i;
1467
1468 if(!graph)
1469 return 0;
1470
1471 for (i = 0; i < graph->nb_filters; i++) {
1472 AVFilterContext *filter = graph->filters[i];
1473 FFFilterContext *ctxi = fffilterctx(filter);
1474 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1475 AVFilterCommand **queue = &ctxi->command_queue, *next;
1476 while (*queue && (*queue)->time <= ts)
1477 queue = &(*queue)->next;
1478 next = *queue;
1479 *queue = av_mallocz(sizeof(AVFilterCommand));
1480 if (!*queue)
1481 return AVERROR(ENOMEM);
1482
1483 (*queue)->command = av_strdup(command);
1484 (*queue)->arg = av_strdup(arg);
1485 (*queue)->time = ts;
1486 (*queue)->flags = flags;
1487 (*queue)->next = next;
1488 if(flags & AVFILTER_CMD_FLAG_ONE)
1489 return 0;
1490 }
1491 }
1492
1493 return 0;
1494 }
1495
1496 420210 static void heap_bubble_up(FFFilterGraph *graph,
1497 FilterLinkInternal *li, int index)
1498 {
1499 420210 FilterLinkInternal **links = graph->sink_links;
1500
1501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 420210 times.
420210 av_assert0(index >= 0);
1502
1503
2/2
✓ Branch 0 taken 1828 times.
✓ Branch 1 taken 420210 times.
422038 while (index) {
1504 1828 int parent = (index - 1) >> 1;
1505
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1828 times.
1828 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1506 break;
1507 1828 links[index] = links[parent];
1508 1828 links[index]->age_index = index;
1509 1828 index = parent;
1510 }
1511 420210 links[index] = li;
1512 420210 li->age_index = index;
1513 420210 }
1514
1515 420334 static void heap_bubble_down(FFFilterGraph *graph,
1516 FilterLinkInternal *li, int index)
1517 {
1518 420334 FilterLinkInternal **links = graph->sink_links;
1519
1520
1/2
✓ Branch 0 taken 420334 times.
✗ Branch 1 not taken.
420334 av_assert0(index >= 0);
1521
1522 5144 while (1) {
1523 425478 int child = 2 * index + 1;
1524
2/2
✓ Branch 0 taken 419999 times.
✓ Branch 1 taken 5479 times.
425478 if (child >= graph->sink_links_count)
1525 419999 break;
1526
2/2
✓ Branch 0 taken 4082 times.
✓ Branch 1 taken 1397 times.
5479 if (child + 1 < graph->sink_links_count &&
1527
2/2
✓ Branch 0 taken 1261 times.
✓ Branch 1 taken 2821 times.
4082 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1528 1261 child++;
1529
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 5144 times.
5479 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1530 335 break;
1531 5144 links[index] = links[child];
1532 5144 links[index]->age_index = index;
1533 5144 index = child;
1534 }
1535 420334 links[index] = li;
1536 420334 li->age_index = index;
1537 420334 }
1538
1539 420210 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1540 {
1541 420210 FFFilterGraph *graphi = fffiltergraph(graph);
1542
1543 420210 heap_bubble_up (graphi, li, li->age_index);
1544 420210 heap_bubble_down(graphi, li, li->age_index);
1545 420210 }
1546
1547 409799 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1548 {
1549 409799 FFFilterGraph *graphi = fffiltergraph(graph);
1550 409799 FilterLinkInternal *oldesti = graphi->sink_links[0];
1551 409799 AVFilterLink *oldest = &oldesti->l.pub;
1552 int64_t frame_count;
1553 int r;
1554
1555
2/2
✓ Branch 0 taken 409929 times.
✓ Branch 1 taken 4935 times.
414864 while (graphi->sink_links_count) {
1556 409929 oldesti = graphi->sink_links[0];
1557 409929 oldest = &oldesti->l.pub;
1558
2/2
✓ Branch 1 taken 409902 times.
✓ Branch 2 taken 27 times.
409929 if (fffilter(oldest->dst->filter)->activate) {
1559 409902 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1560 AV_BUFFERSINK_FLAG_PEEK);
1561
2/2
✓ Branch 0 taken 404838 times.
✓ Branch 1 taken 5064 times.
409902 if (r != AVERROR_EOF)
1562 404838 return r;
1563 } else {
1564 27 r = ff_request_frame(oldest);
1565 }
1566
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 5065 times.
5091 if (r != AVERROR_EOF)
1567 26 break;
1568 5065 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1569 5065 oldest->dst->name,
1570 5065 oldest->dstpad->name);
1571 /* EOF: remove the link from the heap */
1572
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 4941 times.
5065 if (oldesti->age_index < --graphi->sink_links_count)
1573 124 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1574 oldesti->age_index);
1575 5065 oldesti->age_index = -1;
1576 }
1577
2/2
✓ Branch 0 taken 4935 times.
✓ Branch 1 taken 26 times.
4961 if (!graphi->sink_links_count)
1578 4935 return AVERROR_EOF;
1579 av_assert1(!fffilter(oldest->dst->filter)->activate);
1580 av_assert1(oldesti->age_index >= 0);
1581 26 frame_count = oldesti->l.frame_count_out;
1582
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 while (frame_count == oldesti->l.frame_count_out) {
1583 104 r = ff_filter_graph_run_once(graph);
1584
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 78 times.
104 if (r == FFERROR_BUFFERSRC_EMPTY)
1585 26 r = 0;
1586
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 78 times.
104 if (r == AVERROR(EAGAIN) &&
1587
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
26 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1588 !oldesti->status_in)
1589 (void)ff_request_frame(oldest);
1590
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 78 times.
104 else if (r < 0)
1591 26 return r;
1592 }
1593 return 0;
1594 }
1595
1596 4574420 int ff_filter_graph_run_once(AVFilterGraph *graph)
1597 {
1598 FFFilterContext *ctxi;
1599 unsigned i;
1600
1601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4574420 times.
4574420 av_assert0(graph->nb_filters);
1602 4574420 ctxi = fffilterctx(graph->filters[0]);
1603
2/2
✓ Branch 0 taken 19200560 times.
✓ Branch 1 taken 4574420 times.
23774980 for (i = 1; i < graph->nb_filters; i++) {
1604 19200560 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1605
1606
2/2
✓ Branch 0 taken 2734620 times.
✓ Branch 1 taken 16465940 times.
19200560 if (ctxi_other->ready > ctxi->ready)
1607 2734620 ctxi = ctxi_other;
1608 }
1609
1610
2/2
✓ Branch 0 taken 759484 times.
✓ Branch 1 taken 3814936 times.
4574420 if (!ctxi->ready)
1611 759484 return AVERROR(EAGAIN);
1612 3814936 return ff_filter_activate(&ctxi->p);
1613 }
1614