FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2026-07-16 17:05:34
Exec Total Coverage
Lines: 665 880 75.6%
Functions: 37 41 90.2%
Branches: 564 827 68.2%

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 16561 AVFilterGraph *avfilter_graph_alloc(void)
86 {
87 16561 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
88 AVFilterGraph *ret;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16561 times.
16561 if (!graph)
91 return NULL;
92
93 16561 ret = &graph->p;
94 16561 ret->av_class = &filtergraph_class;
95 16561 av_opt_set_defaults(ret);
96 16561 ff_framequeue_global_init(&graph->frame_queues);
97
98 16561 return ret;
99 }
100
101 64357 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
102 {
103 int i, j;
104
1/2
✓ Branch 0 taken 64357 times.
✗ Branch 1 not taken.
64357 for (i = 0; i < graph->nb_filters; i++) {
105
1/2
✓ Branch 0 taken 64357 times.
✗ Branch 1 not taken.
64357 if (graph->filters[i] == filter) {
106 64357 FFSWAP(AVFilterContext*, graph->filters[i],
107 graph->filters[graph->nb_filters - 1]);
108 64357 graph->nb_filters--;
109 64357 filter->graph = NULL;
110
2/2
✓ Branch 0 taken 56115 times.
✓ Branch 1 taken 64357 times.
120472 for (j = 0; j<filter->nb_outputs; j++)
111
2/2
✓ Branch 0 taken 21250 times.
✓ Branch 1 taken 34865 times.
56115 if (filter->outputs[j])
112 21250 ff_filter_link(filter->outputs[j])->graph = NULL;
113
114 64357 return;
115 }
116 }
117 }
118
119 24751 void avfilter_graph_free(AVFilterGraph **graphp)
120 {
121 24751 AVFilterGraph *graph = *graphp;
122 24751 FFFilterGraph *graphi = fffiltergraph(graph);
123
124
2/2
✓ Branch 0 taken 8190 times.
✓ Branch 1 taken 16561 times.
24751 if (!graph)
125 8190 return;
126
127
2/2
✓ Branch 0 taken 64291 times.
✓ Branch 1 taken 16561 times.
80852 while (graph->nb_filters)
128 64291 avfilter_free(graph->filters[0]);
129
130 16561 ff_graph_thread_free(graphi);
131
132 16561 av_freep(&graphi->sink_links);
133
134 16561 av_opt_free(graph);
135
136 16561 av_freep(&graph->filters);
137 16561 av_freep(graphp);
138 }
139
140 19685 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 19685 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19685 times.
19685 if (!*filt_ctx)
148 return AVERROR(ENOMEM);
149
150 19685 ret = avfilter_init_str(*filt_ctx, args);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19685 times.
19685 if (ret < 0)
152 goto fail;
153
154 19685 return 0;
155
156 fail:
157 avfilter_free(*filt_ctx);
158 *filt_ctx = NULL;
159 return ret;
160 }
161
162 6375 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
163 {
164 6375 fffiltergraph(graph)->disable_auto_convert = flags;
165 6375 }
166
167 64357 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
168 const AVFilter *filter,
169 const char *name)
170 {
171 AVFilterContext **filters, *s;
172 64357 FFFilterGraph *graphi = fffiltergraph(graph);
173
174
4/4
✓ Branch 0 taken 34117 times.
✓ Branch 1 taken 30240 times.
✓ Branch 2 taken 16561 times.
✓ Branch 3 taken 17556 times.
64357 if (graph->thread_type && !graphi->thread_execute) {
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16561 times.
16561 if (graph->execute) {
176 graphi->thread_execute = graph->execute;
177 } else {
178 16561 int ret = ff_graph_thread_init(graphi);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16561 times.
16561 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 64357 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64357 times.
64357 if (!filters)
188 return NULL;
189 64357 graph->filters = filters;
190
191 64357 s = ff_filter_alloc(filter, name);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64357 times.
64357 if (!s)
193 return NULL;
194
195 64357 graph->filters[graph->nb_filters++] = s;
196
197 64357 s->graph = graph;
198
199 64357 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 8303 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 44389 times.
✓ Branch 1 taken 8303 times.
52692 for (i = 0; i < graph->nb_filters; i++) {
216 const AVFilterPad *pad;
217 44389 filt = graph->filters[i];
218
219
2/2
✓ Branch 0 taken 36048 times.
✓ Branch 1 taken 44389 times.
80437 for (j = 0; j < filt->nb_inputs; j++) {
220
2/4
✓ Branch 0 taken 36048 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36048 times.
36048 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 36048 times.
✓ Branch 1 taken 44389 times.
80437 for (j = 0; j < filt->nb_outputs; j++) {
230
2/4
✓ Branch 0 taken 36048 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36048 times.
36048 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 8303 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 8303 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 45692 times.
✓ Branch 1 taken 8302 times.
53994 for (i = 0; i < graph->nb_filters; i++) {
254 45692 filt = graph->filters[i];
255
256
2/2
✓ Branch 0 taken 8442 times.
✓ Branch 1 taken 37250 times.
45692 if (!filt->nb_outputs) {
257
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8441 times.
8442 if ((ret = ff_filter_config_links(filt)))
258 1 return ret;
259 }
260 }
261
262 8302 return 0;
263 }
264
265 8302 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 45687 times.
✓ Branch 1 taken 8302 times.
53989 for (i = 0; i < graph->nb_filters; i++) {
273 45687 f = graph->filters[i];
274
2/2
✓ Branch 0 taken 37347 times.
✓ Branch 1 taken 45687 times.
83034 for (j = 0; j < f->nb_outputs; j++) {
275 37347 l = f->outputs[j];
276
2/2
✓ Branch 0 taken 31109 times.
✓ Branch 1 taken 6238 times.
37347 if (l->type == AVMEDIA_TYPE_VIDEO) {
277 31109 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 31109 times.
31109 if (ret < 0)
279 return ret;
280 }
281 }
282 }
283 8302 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 60822 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
298 {
299 int ret;
300
301
2/3
✓ Branch 0 taken 50983 times.
✓ Branch 1 taken 9839 times.
✗ Branch 2 not taken.
60822 switch (link->type) {
302
303 50983 case AVMEDIA_TYPE_VIDEO:
304
2/4
✓ Branch 1 taken 50983 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 50983 times.
✗ Branch 4 not taken.
101966 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
305
1/2
✓ Branch 1 taken 50983 times.
✗ Branch 2 not taken.
101966 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 50983 times.
101966 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0 ||
307 50983 (ret = ff_formats_check_alpha_modes(log, cfg->alpha_modes)) < 0)
308 return ret;
309 50983 break;
310
311 9839 case AVMEDIA_TYPE_AUDIO:
312
2/4
✓ Branch 1 taken 9839 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9839 times.
✗ Branch 4 not taken.
19678 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9839 times.
19678 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
314 9839 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
315 return ret;
316 9839 break;
317
318 default:
319 av_assert0(!"reached");
320 }
321 60822 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 38204 static int filter_check_formats(AVFilterContext *ctx)
330 {
331 unsigned i;
332 int ret;
333
334
2/2
✓ Branch 0 taken 30986 times.
✓ Branch 1 taken 38204 times.
69190 for (i = 0; i < ctx->nb_inputs; i++) {
335 30986 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30986 times.
30986 if (ret < 0)
337 return ret;
338 }
339
2/2
✓ Branch 0 taken 29836 times.
✓ Branch 1 taken 38204 times.
68040 for (i = 0; i < ctx->nb_outputs; i++) {
340 29836 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29836 times.
29836 if (ret < 0)
342 return ret;
343 }
344 38204 return 0;
345 }
346
347 45709 static int filter_query_formats(AVFilterContext *ctx)
348 {
349 45709 const FFFilter *const filter = fffilter(ctx->filter);
350 int ret;
351
352
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 45675 times.
45709 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
353
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 17 times.
34 if ((ret = filter->formats.query_func(ctx)) < 0) {
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 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 17 return ret;
358 }
359
2/2
✓ Branch 0 taken 38187 times.
✓ Branch 1 taken 7488 times.
45675 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
360 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
361 38187 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 38186 times.
38187 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 30836 times.
✓ Branch 1 taken 7350 times.
38186 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
371
372
2/2
✓ Branch 0 taken 30963 times.
✓ Branch 1 taken 38187 times.
69150 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
373 30963 AVFilterLink *l = ctx->inputs[i];
374 30963 cfg_in[i] = &l->outcfg;
375 }
376
377
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 38186 times.
38187 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 29745 times.
✓ Branch 1 taken 8441 times.
38186 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
386
387
2/2
✓ Branch 0 taken 29819 times.
✓ Branch 1 taken 38187 times.
68006 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
388 29819 AVFilterLink *l = ctx->outputs[i];
389 29819 cfg_out[i] = &l->incfg;
390 }
391
392 38187 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
393 38187 av_freep(&cfg_in_dyn);
394 38187 av_freep(&cfg_out_dyn);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38187 times.
38187 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 45675 times.
✓ Branch 1 taken 17 times.
45692 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
404
2/2
✓ Branch 0 taken 38187 times.
✓ Branch 1 taken 7488 times.
45675 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
405 38204 ret = filter_check_formats(ctx);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38204 times.
38204 if (ret < 0)
407 return ret;
408 }
409
410 45692 return ff_default_query_formats(ctx);
411 }
412
413 44463 static int formats_declared(AVFilterContext *f)
414 {
415 int i;
416
417
2/2
✓ Branch 0 taken 35949 times.
✓ Branch 1 taken 8554 times.
44503 for (i = 0; i < f->nb_inputs; i++) {
418
2/2
✓ Branch 0 taken 35907 times.
✓ Branch 1 taken 42 times.
35949 if (!f->inputs[i]->outcfg.formats)
419 35907 return 0;
420
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 9 times.
42 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 9 times.
✓ Branch 1 taken 31 times.
40 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
426
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 !(f->inputs[i]->outcfg.samplerates &&
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 f->inputs[i]->outcfg.channel_layouts))
428 return 0;
429 }
430
2/2
✓ Branch 0 taken 8543 times.
✓ Branch 1 taken 57 times.
8600 for (i = 0; i < f->nb_outputs; i++) {
431
2/2
✓ Branch 0 taken 8497 times.
✓ Branch 1 taken 46 times.
8543 if (!f->outputs[i]->incfg.formats)
432 8497 return 0;
433
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 15 times.
46 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 15 times.
✓ Branch 1 taken 31 times.
46 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
439
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 !(f->outputs[i]->incfg.samplerates &&
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 f->outputs[i]->incfg.channel_layouts))
441 return 0;
442 }
443 57 return 1;
444 }
445
446 static void print_link_formats(void *log_ctx, int level, const AVFilterLink *l,
447 const AVFilterFormatsMerger *mergers[],
448 int nb_mergers)
449 {
450 if (av_log_get_level() < level)
451 return;
452
453 AVBPrint bp;
454 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
455
456 av_log(log_ctx, level, "Link '%s.%s' -> '%s.%s':\n",
457 l->src->name, l->srcpad->name, l->dst->name, l->dstpad->name);
458
459 for (unsigned i = 0; i < nb_mergers; i++) {
460 const AVFilterFormatsMerger *m = mergers[i];
461 av_log(log_ctx, level, " %s:\n", m->name);
462 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, l->incfg));
463 if (av_bprint_is_complete(&bp))
464 av_log(log_ctx, level, " src: %s\n", bp.str);
465 av_bprint_clear(&bp);
466
467 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, l->outcfg));
468 if (av_bprint_is_complete(&bp))
469 av_log(log_ctx, level, " dst: %s\n", bp.str);
470 av_bprint_clear(&bp);
471 }
472
473 av_bprint_finalize(&bp, NULL);
474 }
475
476 44389 static void print_filter_formats(void *log_ctx, int level, const AVFilterContext *f)
477 {
478
1/2
✓ Branch 1 taken 44389 times.
✗ Branch 2 not taken.
44389 if (av_log_get_level() < level)
479 44389 return;
480
481 AVBPrint bp;
482 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
483
484 av_log(log_ctx, level, "Filter '%s' formats:\n", f->name);
485 for (int i = 0; i < f->nb_inputs; i++) {
486 const AVFilterLink *in = f->inputs[i];
487 const AVFilterNegotiation *neg = ff_filter_get_negotiation(in);
488 av_log(log_ctx, level, " in[%d] '%s':\n", i, f->input_pads[i].name);
489
490 for (unsigned i = 0; i < neg->nb_mergers; i++) {
491 const AVFilterFormatsMerger *m = &neg->mergers[i];
492 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, in->outcfg));
493 if (av_bprint_is_complete(&bp))
494 av_log(log_ctx, level, " %s: %s\n", m->name, bp.str);
495 av_bprint_clear(&bp);
496 }
497 }
498
499 for (int i = 0; i < f->nb_outputs; i++) {
500 const AVFilterLink *out = f->outputs[i];
501 const AVFilterNegotiation *neg = ff_filter_get_negotiation(out);
502 av_log(log_ctx, level, " out[%d] '%s':\n", i, f->output_pads[i].name);
503
504 for (unsigned i = 0; i < neg->nb_mergers; i++) {
505 const AVFilterFormatsMerger *m = &neg->mergers[i];
506 m->print_list(&bp, FF_FIELD_AT(void *, m->offset, out->incfg));
507 if (av_bprint_is_complete(&bp))
508 av_log(log_ctx, level, " %s: %s\n", m->name, bp.str);
509 av_bprint_clear(&bp);
510 }
511 }
512
513 av_bprint_finalize(&bp, NULL);
514 }
515
516 /**
517 * Perform one round of query_formats() and merging formats lists on the
518 * filter graph.
519 * @return >=0 if all links formats lists could be queried and merged;
520 * AVERROR(EAGAIN) some progress was made in the queries or merging
521 * and a later call may succeed;
522 * AVERROR(EIO) (may be changed) plus a log message if no progress
523 * was made and the negotiation is stuck;
524 * a negative error code if some other error happened
525 */
526 8318 static int query_formats(AVFilterGraph *graph, void *log_ctx)
527 {
528 int i, j, k, ret;
529 8318 int converter_count = 0;
530 8318 int count_queried = 0; /* successful calls to query_formats() */
531 8318 int count_merged = 0; /* successful merge of formats lists */
532 8318 int count_already_merged = 0; /* lists already merged */
533 8318 int count_delayed = 0; /* lists that need to be merged later */
534
535
2/2
✓ Branch 0 taken 44463 times.
✓ Branch 1 taken 8318 times.
52781 for (i = 0; i < graph->nb_filters; i++) {
536 44463 AVFilterContext *f = graph->filters[i];
537
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 44406 times.
44463 if (formats_declared(f))
538 57 continue;
539 44406 ret = filter_query_formats(f);
540
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 44389 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
44406 if (ret < 0 && ret != AVERROR(EAGAIN))
541 return ret;
542 /* note: EAGAIN could indicate a partial success, not counted yet */
543
2/2
✓ Branch 0 taken 44389 times.
✓ Branch 1 taken 17 times.
44406 if (ret >= 0) {
544 44389 print_filter_formats(log_ctx, AV_LOG_DEBUG, f);
545 44389 count_queried++;
546 }
547 }
548
549 /* go through and merge as many format lists as possible */
550 8318 retry:
551
2/2
✓ Branch 0 taken 64889 times.
✓ Branch 1 taken 8318 times.
73207 for (i = 0; i < graph->nb_filters; i++) {
552 64889 AVFilterContext *filter = graph->filters[i];
553
554
2/2
✓ Branch 0 taken 52915 times.
✓ Branch 1 taken 63586 times.
116501 for (j = 0; j < filter->nb_inputs; j++) {
555 52915 AVFilterLink *link = filter->inputs[j];
556 const AVFilterNegotiation *neg;
557 AVFilterContext *conv[4];
558 const AVFilterFormatsMerger *mergers[4]; /* triggered mergers */
559 52915 const char *conv_filters[4], *conv_opts[4] = {0};
560 52915 unsigned neg_step, num_conv = 0, num_mergers = 0;
561
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52915 times.
52915 if (!link)
563 continue;
564
565 52915 neg = ff_filter_get_negotiation(link);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52915 times.
52915 av_assert0(neg);
567
2/2
✓ Branch 0 taken 191826 times.
✓ Branch 1 taken 52915 times.
244741 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
568 191826 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
569 191826 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
570 191826 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
571
8/8
✓ Branch 0 taken 191762 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 191688 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 139852 times.
✓ Branch 5 taken 51836 times.
✓ Branch 7 taken 1381 times.
✓ Branch 8 taken 138471 times.
191826 if (a && b && a != b && !m->can_merge(a, b)) {
572
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1303 times.
1381 for (k = 0; k < num_conv; k++) {
573
1/2
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
78 if (!strcmp(conv_filters[k], m->conversion_filter))
574 78 break;
575 }
576
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 78 times.
1381 if (k == num_conv) {
577 av_assert1(num_conv < FF_ARRAY_ELEMS(conv_filters));
578 1303 conv_filters[num_conv] = m->conversion_filter;
579
2/2
✓ Branch 0 taken 1302 times.
✓ Branch 1 taken 1 times.
1303 if (m->conversion_opts_offset)
580 1302 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
581 1303 num_conv++;
582 }
583 av_assert1(num_mergers < FF_ARRAY_ELEMS(mergers));
584 1381 mergers[num_mergers++] = m;
585 }
586 }
587
2/2
✓ Branch 0 taken 191826 times.
✓ Branch 1 taken 52915 times.
244741 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
588 191826 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
589 191826 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
590 191826 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
591
4/4
✓ Branch 0 taken 191762 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 191688 times.
191826 if (!(a && b)) {
592 138 count_delayed++;
593
2/2
✓ Branch 0 taken 57261 times.
✓ Branch 1 taken 134427 times.
191688 } else if (a == b) {
594 57261 count_already_merged++;
595
2/2
✓ Branch 0 taken 130504 times.
✓ Branch 1 taken 3923 times.
134427 } else if (!num_conv) {
596 130504 count_merged++;
597 130504 ret = m->merge(a, b);
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130504 times.
130504 if (ret < 0)
599 return ret;
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130504 times.
130504 if (!ret) {
601 mergers[num_mergers++] = m;
602 conv_filters[num_conv] = m->conversion_filter;
603 if (m->conversion_opts_offset)
604 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
605 num_conv++;
606 }
607 }
608 }
609
610 /**
611 * Couldn't merge format lists; auto-insert conversion filters
612 * in reverse order to keep the order consistent with the list
613 * of mergers, since they are prepended onto the existing link
614 */
615
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 52915 times.
54218 for (k = num_conv - 1; k >= 0; k--) {
616 const AVFilter *filter;
617 char inst_name[30];
618
619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1303 times.
1303 if (fffiltergraph(graph)->disable_auto_convert) {
620 av_log(log_ctx, AV_LOG_ERROR,
621 "The filters '%s' and '%s' do not have a common format "
622 "and automatic conversion is disabled.\n",
623 link->src->name, link->dst->name);
624 print_link_formats(log_ctx, AV_LOG_ERROR, link, mergers, num_mergers);
625 return AVERROR(EINVAL);
626 }
627
628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1303 times.
1303 if (!(filter = avfilter_get_by_name(conv_filters[k]))) {
629 av_log(log_ctx, AV_LOG_ERROR,
630 "'%s' filter not present, cannot convert formats.\n",
631 conv_filters[k]);
632 print_link_formats(log_ctx, AV_LOG_ERROR, link, mergers, num_mergers);
633 return AVERROR(EINVAL);
634 }
635 1303 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
636 conv_filters[k], converter_count++);
637 1303 ret = avfilter_graph_create_filter(&conv[k], filter, inst_name,
638 conv_opts[k], NULL, graph);
639
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
1303 if (ret < 0)
640 return ret;
641
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1303 times.
1303 if ((ret = avfilter_insert_filter(link, conv[k], 0, 0)) < 0)
642 return ret;
643
644
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1303 times.
1303 if ((ret = filter_query_formats(conv[k])) < 0)
645 return ret;
646 }
647
648 /* preemptively settle formats of auto filters */
649
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 52915 times.
54218 for (k = 0; k < num_conv; k++) {
650 1303 AVFilterLink *inlink = conv[k]->inputs[0];
651 1303 AVFilterLink *outlink = conv[k]->outputs[0];
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
1303 av_assert0( inlink->incfg.formats->refcount > 0);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
1303 av_assert0( inlink->outcfg.formats->refcount > 0);
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
1303 av_assert0(outlink->incfg.formats->refcount > 0);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1303 times.
1303 av_assert0(outlink->outcfg.formats->refcount > 0);
656
2/2
✓ Branch 0 taken 634 times.
✓ Branch 1 taken 669 times.
1303 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
659
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
660
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
663
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0( inlink-> incfg.alpha_modes->refcount > 0);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0( inlink->outcfg.alpha_modes->refcount > 0);
667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0(outlink-> incfg.alpha_modes->refcount > 0);
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 634 times.
634 av_assert0(outlink->outcfg.alpha_modes->refcount > 0);
669
1/2
✓ Branch 0 taken 669 times.
✗ Branch 1 not taken.
669 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0( inlink-> incfg.samplerates->refcount > 0);
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0( inlink->outcfg.samplerates->refcount > 0);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0(outlink-> incfg.samplerates->refcount > 0);
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0(outlink->outcfg.samplerates->refcount > 0);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 669 times.
669 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
678 }
679
680 #define MERGE(merger, link) \
681 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
682 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
683
684
2/2
✓ Branch 0 taken 4543 times.
✓ Branch 1 taken 1303 times.
5846 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
685 4543 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
686
2/2
✓ Branch 0 taken 636 times.
✓ Branch 1 taken 3907 times.
4543 if (strcmp(m->conversion_filter, conv_filters[k]))
687 636 continue;
688
2/4
✓ Branch 1 taken 3907 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3907 times.
7814 if ((ret = MERGE(m, inlink)) <= 0 ||
689 3907 (ret = MERGE(m, outlink)) <= 0) {
690 if (ret < 0)
691 return ret;
692 av_log(log_ctx, AV_LOG_ERROR,
693 "Impossible to convert between the formats supported by the filter "
694 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
695 print_link_formats(log_ctx, AV_LOG_ERROR, inlink, &m, 1);
696 print_link_formats(log_ctx, AV_LOG_ERROR, outlink, &m, 1);
697 return AVERROR(ENOSYS);
698 } else {
699 3907 count_merged += 2;
700 }
701 }
702 }
703
704 /* if there is an auto filter, we may need another round to fully
705 * settle formats due to possible cross-incompatibilities between
706 * the auto filters themselves, or between the auto filters and
707 * a different attribute of the filter they are modifying */
708
2/2
✓ Branch 0 taken 1303 times.
✓ Branch 1 taken 51612 times.
52915 if (num_conv)
709 1303 goto retry;
710 }
711 }
712
713 8318 av_log(graph, AV_LOG_DEBUG, "query_formats: "
714 "%d queried, %d merged, %d already done, %d delayed\n",
715 count_queried, count_merged, count_already_merged, count_delayed);
716
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 8303 times.
8318 if (count_delayed) {
717 AVBPrint bp;
718
719 /* if count_queried > 0, one filter at least did set its formats,
720 that will give additional information to its neighbour;
721 if count_merged > 0, one pair of formats lists at least was merged,
722 that will give additional information to all connected filters;
723 in both cases, progress was made and a new round must be done */
724
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)
725 15 return AVERROR(EAGAIN);
726 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
727 for (i = 0; i < graph->nb_filters; i++)
728 if (!formats_declared(graph->filters[i]))
729 av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
730 graph->filters[i]->name);
731 av_log(graph, AV_LOG_ERROR,
732 "The following filters could not choose their formats: %s\n"
733 "Consider inserting the (a)format filter near their input or "
734 "output.\n", bp.str);
735 return AVERROR(EIO);
736 }
737 8303 return 0;
738 }
739
740 1218 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
741 {
742 1218 int score = 0;
743
744
2/2
✓ Branch 2 taken 1119 times.
✓ Branch 3 taken 99 times.
1218 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
745 1119 score ++;
746
747
2/2
✓ Branch 2 taken 278 times.
✓ Branch 3 taken 940 times.
1218 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
748 278 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
749 }else
750 940 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
751
752
4/4
✓ Branch 1 taken 151 times.
✓ Branch 2 taken 1067 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 149 times.
1369 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
753 151 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
754 2 score += 20;
755
756
3/4
✓ Branch 1 taken 101 times.
✓ Branch 2 taken 1117 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
1319 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
757 101 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
758 score += 2;
759
760 1218 return score;
761 }
762
763 609 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
764 enum AVSampleFormat src_fmt)
765 {
766 int score1, score2;
767
768 609 score1 = get_fmt_score(dst_fmt1, src_fmt);
769 609 score2 = get_fmt_score(dst_fmt2, src_fmt);
770
771
2/2
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 151 times.
609 return score1 < score2 ? dst_fmt1 : dst_fmt2;
772 }
773
774 36907 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
775 {
776 36907 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36907 times.
36907 if (!desc)
778 return 0;
779
2/2
✓ Branch 0 taken 1806 times.
✓ Branch 1 taken 35101 times.
36907 if (desc->nb_components < 3)
780 1806 return 0; /* Grayscale is explicitly full-range in swscale */
781 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
782 35101 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
783 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
784 }
785
786
787 26314 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
788 {
789
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 25560 times.
26314 switch (fmt) {
790 754 case AV_PIX_FMT_YUVJ420P:
791 case AV_PIX_FMT_YUVJ422P:
792 case AV_PIX_FMT_YUVJ444P:
793 case AV_PIX_FMT_YUVJ440P:
794 case AV_PIX_FMT_YUVJ411P:
795 754 return 1;
796 25560 default:
797 25560 return 0;
798 }
799 }
800
801 111989 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
802 {
803
3/4
✓ Branch 0 taken 111989 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 74638 times.
✓ Branch 3 taken 37351 times.
111989 if (!link || !link->incfg.formats)
804 74638 return 0;
805
806
2/2
✓ Branch 0 taken 31113 times.
✓ Branch 1 taken 6238 times.
37351 if (link->type == AVMEDIA_TYPE_VIDEO) {
807
3/4
✓ Branch 0 taken 114 times.
✓ Branch 1 taken 30999 times.
✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
31113 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
808 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
809 114 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
810 114 enum AVPixelFormat best= AV_PIX_FMT_NONE;
811 int i;
812
2/2
✓ Branch 0 taken 1117 times.
✓ Branch 1 taken 114 times.
1231 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
813 1117 enum AVPixelFormat p = link->incfg.formats->formats[i];
814 1117 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
815 }
816 114 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
817 114 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
818 114 av_get_pix_fmt_name(ref->format), has_alpha);
819 114 link->incfg.formats->formats[0] = best;
820 }
821
1/2
✓ Branch 0 taken 6238 times.
✗ Branch 1 not taken.
6238 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
822
3/4
✓ Branch 0 taken 151 times.
✓ Branch 1 taken 6087 times.
✓ Branch 2 taken 151 times.
✗ Branch 3 not taken.
6238 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
823 151 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
824 int i;
825
2/2
✓ Branch 0 taken 609 times.
✓ Branch 1 taken 151 times.
760 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
826 609 enum AVSampleFormat p = link->incfg.formats->formats[i];
827 609 best = find_best_sample_fmt_of_2(best, p, ref->format);
828 }
829 151 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
830 151 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
831 151 av_get_sample_fmt_name(ref->format));
832 151 link->incfg.formats->formats[0] = best;
833 }
834 }
835
836 37351 link->incfg.formats->nb_formats = 1;
837 37351 link->format = link->incfg.formats->formats[0];
838
839
2/2
✓ Branch 0 taken 31113 times.
✓ Branch 1 taken 6238 times.
37351 if (link->type == AVMEDIA_TYPE_VIDEO) {
840 31113 enum AVPixelFormat swfmt = link->format;
841
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31113 times.
31113 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
842 // FIXME: this is a hack - we'd like to use the sw_format of
843 // link->hw_frames_ctx here, but it is not yet available.
844 // To make this work properly we will need to either reorder
845 // things so that it is available here or somehow negotiate
846 // sw_format separately.
847 swfmt = AV_PIX_FMT_YUV420P;
848 }
849
850 31113 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
851
2/2
✓ Branch 1 taken 9877 times.
✓ Branch 2 taken 21236 times.
31113 if (!ff_fmt_is_regular_yuv(swfmt)) {
852 /* These fields are explicitly documented as affecting YUV only,
853 * so set them to sane values for other formats. */
854
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 9306 times.
9877 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
855 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
856 else
857 9306 link->color_range = AVCOL_RANGE_JPEG;
858
2/2
✓ Branch 0 taken 8270 times.
✓ Branch 1 taken 1607 times.
9877 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
859 8270 link->colorspace = AVCOL_SPC_RGB;
860 } else {
861 1607 link->colorspace = AVCOL_SPC_UNSPECIFIED;
862 }
863 } else {
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21236 times.
21236 if (!link->incfg.color_spaces->nb_formats) {
865 av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
866 " the link between filters %s and %s.\n", link->src->name,
867 link->dst->name);
868 return AVERROR(EINVAL);
869 }
870 21236 link->incfg.color_spaces->nb_formats = 1;
871 21236 link->colorspace = link->incfg.color_spaces->formats[0];
872
873
2/2
✓ Branch 1 taken 679 times.
✓ Branch 2 taken 20557 times.
21236 if (ff_fmt_is_forced_full_range(swfmt)) {
874 679 link->color_range = AVCOL_RANGE_JPEG;
875 } else {
876
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20557 times.
20557 if (!link->incfg.color_ranges->nb_formats) {
877 av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
878 " the link between filters %s and %s.\n", link->src->name,
879 link->dst->name);
880 return AVERROR(EINVAL);
881 }
882 20557 link->incfg.color_ranges->nb_formats = 1;
883 20557 link->color_range = link->incfg.color_ranges->formats[0];
884 }
885 }
886
887
2/2
✓ Branch 0 taken 3781 times.
✓ Branch 1 taken 27332 times.
31113 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA) {
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3781 times.
3781 if (!link->incfg.alpha_modes->nb_formats) {
889 av_log(link->src, AV_LOG_ERROR, "Cannot select alpha mode for"
890 " the link between filters %s and %s.\n", link->src->name,
891 link->dst->name);
892 return AVERROR(EINVAL);
893 }
894 3781 link->incfg.alpha_modes->nb_formats = 1;
895 3781 link->alpha_mode = link->incfg.alpha_modes->formats[0];
896 } else {
897 27332 link->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
898 }
899
1/2
✓ Branch 0 taken 6238 times.
✗ Branch 1 not taken.
6238 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
900 int ret;
901
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6238 times.
6238 if (!link->incfg.samplerates->nb_formats) {
903 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
904 " the link between filters %s and %s.\n", link->src->name,
905 link->dst->name);
906 return AVERROR(EINVAL);
907 }
908 6238 link->incfg.samplerates->nb_formats = 1;
909 6238 link->sample_rate = link->incfg.samplerates->formats[0];
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6238 times.
6238 if (link->incfg.channel_layouts->all_layouts) {
912 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
913 " the link between filters %s and %s.\n", link->src->name,
914 link->dst->name);
915 if (!link->incfg.channel_layouts->all_counts)
916 av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
917 "supported, try specifying a channel layout using "
918 "'aformat=channel_layouts=something'.\n");
919 return AVERROR(EINVAL);
920 }
921 6238 link->incfg.channel_layouts->nb_channel_layouts = 1;
922 6238 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
923
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6238 times.
6238 if (ret < 0)
924 return ret;
925 }
926
927 37351 ff_formats_unref(&link->incfg.formats);
928 37351 ff_formats_unref(&link->outcfg.formats);
929 37351 ff_formats_unref(&link->incfg.samplerates);
930 37351 ff_formats_unref(&link->outcfg.samplerates);
931 37351 ff_channel_layouts_unref(&link->incfg.channel_layouts);
932 37351 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
933 37351 ff_formats_unref(&link->incfg.color_spaces);
934 37351 ff_formats_unref(&link->outcfg.color_spaces);
935 37351 ff_formats_unref(&link->incfg.color_ranges);
936 37351 ff_formats_unref(&link->outcfg.color_ranges);
937 37351 ff_formats_unref(&link->incfg.alpha_modes);
938 37351 ff_formats_unref(&link->outcfg.alpha_modes);
939
940 37351 return 0;
941 }
942
943 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
944 do { \
945 for (i = 0; i < filter->nb_inputs; i++) { \
946 AVFilterLink *link = filter->inputs[i]; \
947 fmt_type fmt; \
948 \
949 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
950 continue; \
951 fmt = link->outcfg.list->var[0]; \
952 \
953 for (j = 0; j < filter->nb_outputs; j++) { \
954 AVFilterLink *out_link = filter->outputs[j]; \
955 list_type *fmts; \
956 \
957 if (link->type != out_link->type || \
958 out_link->incfg.list->nb == 1) \
959 continue; \
960 fmts = out_link->incfg.list; \
961 \
962 if (!out_link->incfg.list->nb) { \
963 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
964 return ret; \
965 ret = 1; \
966 break; \
967 } \
968 \
969 for (k = 0; k < out_link->incfg.list->nb; k++) \
970 if (fmts->var[k] == fmt) { \
971 fmts->var[0] = fmt; \
972 fmts->nb = 1; \
973 ret = 1; \
974 break; \
975 } \
976 } \
977 } \
978 } while (0)
979
980 72899 static int reduce_formats_on_filter(AVFilterContext *filter)
981 {
982 72899 int i, j, k, ret = 0;
983
984
16/20
✓ Branch 0 taken 59874 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1489 times.
✓ Branch 3 taken 58385 times.
✓ Branch 4 taken 45641 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 45044 times.
✓ Branch 7 taken 597 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 597 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 55 times.
✓ Branch 14 taken 4900 times.
✓ Branch 15 taken 4955 times.
✓ Branch 16 taken 542 times.
✓ Branch 17 taken 45653 times.
✓ Branch 18 taken 58385 times.
✓ Branch 19 taken 59874 times.
✓ Branch 20 taken 72899 times.
183326 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
985 nb_formats, ff_add_format);
986
18/20
✓ Branch 0 taken 11203 times.
✓ Branch 1 taken 48671 times.
✓ Branch 2 taken 1245 times.
✓ Branch 3 taken 9958 times.
✓ Branch 4 taken 8057 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 7246 times.
✓ Branch 7 taken 811 times.
✓ Branch 8 taken 772 times.
✓ Branch 9 taken 39 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 772 times.
✓ Branch 13 taken 39 times.
✓ Branch 14 taken 69 times.
✓ Branch 15 taken 108 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 8063 times.
✓ Branch 18 taken 9186 times.
✓ Branch 19 taken 59874 times.
✓ Branch 20 taken 72899 times.
140133 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
987 nb_formats, ff_add_format);
988
15/20
✓ Branch 0 taken 48671 times.
✓ Branch 1 taken 11203 times.
✓ Branch 2 taken 11422 times.
✓ Branch 3 taken 37249 times.
✓ Branch 4 taken 29196 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25254 times.
✓ Branch 7 taken 3942 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3942 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3942 times.
✓ Branch 14 taken 499 times.
✓ Branch 15 taken 4441 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 29196 times.
✓ Branch 18 taken 37249 times.
✓ Branch 19 taken 59874 times.
✓ Branch 20 taken 72899 times.
162468 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
989 nb_formats, ff_add_format);
990
15/20
✓ Branch 0 taken 48671 times.
✓ Branch 1 taken 11203 times.
✓ Branch 2 taken 8776 times.
✓ Branch 3 taken 39895 times.
✓ Branch 4 taken 30551 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27226 times.
✓ Branch 7 taken 3325 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3325 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3325 times.
✓ Branch 14 taken 3307 times.
✓ Branch 15 taken 6632 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 30551 times.
✓ Branch 18 taken 39895 times.
✓ Branch 19 taken 59874 times.
✓ Branch 20 taken 72899 times.
166631 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
991 nb_formats, ff_add_format);
992
10/20
✓ Branch 0 taken 48671 times.
✓ Branch 1 taken 11203 times.
✓ Branch 2 taken 48061 times.
✓ Branch 3 taken 610 times.
✓ Branch 4 taken 466 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 466 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 466 times.
✓ Branch 18 taken 610 times.
✓ Branch 19 taken 59874 times.
✓ Branch 20 taken 72899 times.
133239 REDUCE_FORMATS(int, AVFilterFormats, alpha_modes, formats,
993 nb_formats, ff_add_format);
994
995 /* reduce channel layouts */
996
2/2
✓ Branch 0 taken 59874 times.
✓ Branch 1 taken 72899 times.
132773 for (i = 0; i < filter->nb_inputs; i++) {
997 59874 AVFilterLink *inlink = filter->inputs[i];
998 const AVChannelLayout *fmt;
999
1000
2/2
✓ Branch 0 taken 11203 times.
✓ Branch 1 taken 48671 times.
59874 if (!inlink->outcfg.channel_layouts ||
1001
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 11120 times.
11203 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
1002 48754 continue;
1003 11120 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
1004
1005
2/2
✓ Branch 0 taken 8812 times.
✓ Branch 1 taken 10273 times.
19085 for (j = 0; j < filter->nb_outputs; j++) {
1006 8812 AVFilterLink *outlink = filter->outputs[j];
1007 AVFilterChannelLayouts *fmts;
1008
1009 8812 fmts = outlink->incfg.channel_layouts;
1010
4/4
✓ Branch 0 taken 8806 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7914 times.
✓ Branch 3 taken 892 times.
8812 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
1011 7920 continue;
1012
1013
2/2
✓ Branch 0 taken 847 times.
✓ Branch 1 taken 45 times.
892 if (fmts->all_layouts &&
1014
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 840 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
847 (KNOWN(fmt) || fmts->all_counts)) {
1015 /* Turn the infinite list into a singleton */
1016 847 fmts->all_layouts = fmts->all_counts = 0;
1017 847 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 847 times.
847 if (ret < 0)
1019 return ret;
1020 847 ret = 1;
1021 847 break;
1022 }
1023
1024
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 6 times.
148 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
1025
2/2
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 103 times.
142 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
1026 39 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
39 if (ret < 0)
1028 return ret;
1029 39 fmts->nb_channel_layouts = 1;
1030 39 ret = 1;
1031 39 break;
1032 }
1033 }
1034 }
1035 }
1036
1037 72899 return ret;
1038 }
1039
1040 8303 static int reduce_formats(AVFilterGraph *graph)
1041 {
1042 int i, reduced, ret;
1043
1044 do {
1045 12951 reduced = 0;
1046
1047
2/2
✓ Branch 0 taken 72899 times.
✓ Branch 1 taken 12951 times.
85850 for (i = 0; i < graph->nb_filters; i++) {
1048
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72899 times.
72899 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
1049 return ret;
1050 72899 reduced |= ret;
1051 }
1052
2/2
✓ Branch 0 taken 4648 times.
✓ Branch 1 taken 8303 times.
12951 } while (reduced);
1053
1054 8303 return 0;
1055 }
1056
1057 45692 static void swap_samplerates_on_filter(AVFilterContext *filter)
1058 {
1059 45692 AVFilterLink *link = NULL;
1060 int sample_rate;
1061 int i, j;
1062
1063
2/2
✓ Branch 0 taken 37262 times.
✓ Branch 1 taken 39539 times.
76801 for (i = 0; i < filter->nb_inputs; i++) {
1064 37262 link = filter->inputs[i];
1065
1066
2/2
✓ Branch 0 taken 6153 times.
✓ Branch 1 taken 31109 times.
37262 if (link->type == AVMEDIA_TYPE_AUDIO &&
1067
1/2
✓ Branch 0 taken 6153 times.
✗ Branch 1 not taken.
6153 link->outcfg.samplerates->nb_formats== 1)
1068 6153 break;
1069 }
1070
2/2
✓ Branch 0 taken 39539 times.
✓ Branch 1 taken 6153 times.
45692 if (i == filter->nb_inputs)
1071 39539 return;
1072
1073 6153 sample_rate = link->outcfg.samplerates->formats[0];
1074
1075
2/2
✓ Branch 0 taken 4784 times.
✓ Branch 1 taken 6153 times.
10937 for (i = 0; i < filter->nb_outputs; i++) {
1076 4784 AVFilterLink *outlink = filter->outputs[i];
1077 4784 int best_idx, best_diff = INT_MAX;
1078
1079
2/2
✓ Branch 0 taken 4782 times.
✓ Branch 1 taken 2 times.
4784 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1080
1/2
✓ Branch 0 taken 4782 times.
✗ Branch 1 not taken.
4782 outlink->incfg.samplerates->nb_formats < 2)
1081 4784 continue;
1082
1083 for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
1084 int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
1085
1086 av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
1087
1088 if (diff < best_diff) {
1089 best_diff = diff;
1090 best_idx = j;
1091 }
1092 }
1093 FFSWAP(int, outlink->incfg.samplerates->formats[0],
1094 outlink->incfg.samplerates->formats[best_idx]);
1095 }
1096 }
1097
1098 8303 static void swap_samplerates(AVFilterGraph *graph)
1099 {
1100 int i;
1101
1102
2/2
✓ Branch 0 taken 45692 times.
✓ Branch 1 taken 8303 times.
53995 for (i = 0; i < graph->nb_filters; i++)
1103 45692 swap_samplerates_on_filter(graph->filters[i]);
1104 8303 }
1105
1106 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
1107 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
1108 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
1109 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
1110 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
1111 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
1112 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
1113
1114 /* allowable substitutions for channel pairs when comparing layouts,
1115 * ordered by priority for both values */
1116 static const uint64_t ch_subst[][2] = {
1117 { CH_FRONT_PAIR, CH_CENTER_PAIR },
1118 { CH_FRONT_PAIR, CH_WIDE_PAIR },
1119 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
1120 { CH_CENTER_PAIR, CH_FRONT_PAIR },
1121 { CH_CENTER_PAIR, CH_WIDE_PAIR },
1122 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
1123 { CH_WIDE_PAIR, CH_FRONT_PAIR },
1124 { CH_WIDE_PAIR, CH_CENTER_PAIR },
1125 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
1126 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
1127 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
1128 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
1129 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
1130 { CH_SIDE_PAIR, CH_BACK_PAIR },
1131 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
1132 { CH_BACK_PAIR, CH_DIRECT_PAIR },
1133 { CH_BACK_PAIR, CH_SIDE_PAIR },
1134 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
1135 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
1136 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
1137 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
1138 };
1139
1140 45692 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1141 {
1142 45692 AVFilterLink *link = NULL;
1143 int i, j, k;
1144
1145
2/2
✓ Branch 0 taken 37262 times.
✓ Branch 1 taken 39545 times.
76807 for (i = 0; i < filter->nb_inputs; i++) {
1146 37262 link = filter->inputs[i];
1147
1148
2/2
✓ Branch 0 taken 6153 times.
✓ Branch 1 taken 31109 times.
37262 if (link->type == AVMEDIA_TYPE_AUDIO &&
1149
2/2
✓ Branch 0 taken 6147 times.
✓ Branch 1 taken 6 times.
6153 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1150 6147 break;
1151 }
1152
2/2
✓ Branch 0 taken 39545 times.
✓ Branch 1 taken 6147 times.
45692 if (i == filter->nb_inputs)
1153 39545 return;
1154
1155
2/2
✓ Branch 0 taken 4781 times.
✓ Branch 1 taken 6147 times.
10928 for (i = 0; i < filter->nb_outputs; i++) {
1156 4781 AVFilterLink *outlink = filter->outputs[i];
1157 4781 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1158
1159
2/2
✓ Branch 0 taken 4779 times.
✓ Branch 1 taken 2 times.
4781 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1160
2/2
✓ Branch 0 taken 4776 times.
✓ Branch 1 taken 3 times.
4779 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1161 4778 continue;
1162
1163
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++) {
1164 34 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1165 int in_channels;
1166 int out_channels;
1167 int count_diff;
1168 int matched_channels, extra_channels;
1169 34 int score = 100000;
1170
1171 34 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1172 34 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1173 34 in_channels = in_chlayout.nb_channels;
1174 34 out_channels = out_chlayout.nb_channels;
1175 34 count_diff = out_channels - in_channels;
1176
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)) {
1177 /* Compute score in case the input or output layout encodes
1178 a channel count; in this case the score is not altered by
1179 the computation afterwards, as in_chlayout and
1180 out_chlayout have both been set to 0 */
1181
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))
1182
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1183
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1184 out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1185 32 score -= 10000 + FFABS(out_channels - in_channels) +
1186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1187 16 av_channel_layout_uninit(&in_chlayout);
1188 16 av_channel_layout_uninit(&out_chlayout);
1189 /* Let the remaining computation run, even if the score
1190 value is not altered */
1191 }
1192
1193 /* channel substitution */
1194
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 34 times.
748 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1195 714 uint64_t cmp0 = ch_subst[k][0];
1196 714 uint64_t cmp1 = ch_subst[k][1];
1197
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) &&
1198
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 100 times.
336 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1199
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
140 av_channel_layout_subset(&out_chlayout, cmp1) &&
1200 20 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1201 4 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1202 4 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1203 /* add score for channel match, minus a deduction for
1204 having to do the substitution */
1205 4 score += 10 * av_popcount64(cmp1) - 2;
1206 }
1207 }
1208
1209 /* no penalty for LFE channel mismatch */
1210
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 &&
1211 18 av_channel_layout_index_from_channel(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1212 7 score += 10;
1213 34 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1214 34 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1215
1216 34 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1217 34 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1218 34 score += 10 * matched_channels - 5 * extra_channels;
1219
1220
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 ||
1221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (count_diff < best_count_diff && score == best_score)) {
1222 10 best_score = score;
1223 10 best_idx = j;
1224 10 best_count_diff = count_diff;
1225 }
1226 }
1227
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(best_idx >= 0);
1228 3 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1229 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1230 }
1231
1232 }
1233
1234 8303 static void swap_channel_layouts(AVFilterGraph *graph)
1235 {
1236 int i;
1237
1238
2/2
✓ Branch 0 taken 45692 times.
✓ Branch 1 taken 8303 times.
53995 for (i = 0; i < graph->nb_filters; i++)
1239 45692 swap_channel_layouts_on_filter(graph->filters[i]);
1240 8303 }
1241
1242 45692 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1243 {
1244 45692 AVFilterLink *link = NULL;
1245 int format, bps;
1246 int i, j;
1247
1248
2/2
✓ Branch 0 taken 37331 times.
✓ Branch 1 taken 39955 times.
77286 for (i = 0; i < filter->nb_inputs; i++) {
1249 37331 link = filter->inputs[i];
1250
1251
2/2
✓ Branch 0 taken 6222 times.
✓ Branch 1 taken 31109 times.
37331 if (link->type == AVMEDIA_TYPE_AUDIO &&
1252
2/2
✓ Branch 0 taken 5737 times.
✓ Branch 1 taken 485 times.
6222 link->outcfg.formats->nb_formats == 1)
1253 5737 break;
1254 }
1255
2/2
✓ Branch 0 taken 39955 times.
✓ Branch 1 taken 5737 times.
45692 if (i == filter->nb_inputs)
1256 39955 return;
1257
1258 5737 format = link->outcfg.formats->formats[0];
1259 5737 bps = av_get_bytes_per_sample(format);
1260
1261
2/2
✓ Branch 0 taken 4385 times.
✓ Branch 1 taken 5737 times.
10122 for (i = 0; i < filter->nb_outputs; i++) {
1262 4385 AVFilterLink *outlink = filter->outputs[i];
1263 4385 int best_idx = -1, best_score = INT_MIN;
1264
1265
2/2
✓ Branch 0 taken 4383 times.
✓ Branch 1 taken 2 times.
4385 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1266
2/2
✓ Branch 0 taken 4218 times.
✓ Branch 1 taken 165 times.
4383 outlink->incfg.formats->nb_formats < 2)
1267 4220 continue;
1268
1269
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 8 times.
321 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1270 313 int out_format = outlink->incfg.formats->formats[j];
1271 313 int out_bps = av_get_bytes_per_sample(out_format);
1272 int score;
1273
1274
4/4
✓ Branch 1 taken 172 times.
✓ Branch 2 taken 141 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 156 times.
485 if (av_get_packed_sample_fmt(out_format) == format ||
1275 172 av_get_planar_sample_fmt(out_format) == format) {
1276 157 best_idx = j;
1277 157 break;
1278 }
1279
1280 /* for s32 and float prefer double to prevent loss of information */
1281
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 150 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
156 if (bps == 4 && out_bps == 8) {
1282 best_idx = j;
1283 break;
1284 }
1285
1286 /* prefer closest higher or equal bps */
1287 156 score = -abs(out_bps - bps);
1288
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 127 times.
156 if (out_bps >= bps)
1289 29 score += INT_MAX/2;
1290
1291
2/2
✓ Branch 0 taken 121 times.
✓ Branch 1 taken 35 times.
156 if (score > best_score) {
1292 121 best_score = score;
1293 121 best_idx = j;
1294 }
1295 }
1296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 av_assert0(best_idx >= 0);
1297 165 FFSWAP(int, outlink->incfg.formats->formats[0],
1298 outlink->incfg.formats->formats[best_idx]);
1299 }
1300 }
1301
1302 8303 static void swap_sample_fmts(AVFilterGraph *graph)
1303 {
1304 int i;
1305
1306
2/2
✓ Branch 0 taken 45692 times.
✓ Branch 1 taken 8303 times.
53995 for (i = 0; i < graph->nb_filters; i++)
1307 45692 swap_sample_fmts_on_filter(graph->filters[i]);
1308
1309 8303 }
1310
1311 8303 static int pick_formats(AVFilterGraph *graph)
1312 {
1313 int i, j, ret;
1314 int change;
1315
1316 do{
1317 16640 change = 0;
1318
2/2
✓ Branch 0 taken 92347 times.
✓ Branch 1 taken 16640 times.
108987 for (i = 0; i < graph->nb_filters; i++) {
1319 92347 AVFilterContext *filter = graph->filters[i];
1320
2/2
✓ Branch 0 taken 75204 times.
✓ Branch 1 taken 17143 times.
92347 if (filter->nb_inputs){
1321
2/2
✓ Branch 0 taken 75591 times.
✓ Branch 1 taken 75204 times.
150795 for (j = 0; j < filter->nb_inputs; j++){
1322
4/4
✓ Branch 0 taken 15963 times.
✓ Branch 1 taken 59628 times.
✓ Branch 2 taken 15433 times.
✓ Branch 3 taken 530 times.
75591 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1323
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15433 times.
15433 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1324 return ret;
1325 15433 change = 1;
1326 }
1327 }
1328 }
1329
2/2
✓ Branch 0 taken 75319 times.
✓ Branch 1 taken 17028 times.
92347 if (filter->nb_outputs){
1330
2/2
✓ Branch 0 taken 75591 times.
✓ Branch 1 taken 75319 times.
150910 for (j = 0; j < filter->nb_outputs; j++){
1331
4/4
✓ Branch 0 taken 22242 times.
✓ Branch 1 taken 53349 times.
✓ Branch 2 taken 21589 times.
✓ Branch 3 taken 653 times.
75591 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21589 times.
21589 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1333 return ret;
1334 21589 change = 1;
1335 }
1336 }
1337 }
1338
6/6
✓ Branch 0 taken 75204 times.
✓ Branch 1 taken 17143 times.
✓ Branch 2 taken 58176 times.
✓ Branch 3 taken 17028 times.
✓ Branch 4 taken 57890 times.
✓ Branch 5 taken 286 times.
92347 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1339
2/2
✓ Branch 0 taken 58094 times.
✓ Branch 1 taken 57890 times.
115984 for (j = 0; j < filter->nb_outputs; j++) {
1340
2/2
✓ Branch 0 taken 265 times.
✓ Branch 1 taken 57829 times.
58094 if (filter->outputs[j]->format<0) {
1341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 265 times.
265 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1342 return ret;
1343 265 change = 1;
1344 }
1345 }
1346 }
1347 }
1348
2/2
✓ Branch 0 taken 8337 times.
✓ Branch 1 taken 8303 times.
16640 }while(change);
1349
1350
2/2
✓ Branch 0 taken 45692 times.
✓ Branch 1 taken 8303 times.
53995 for (i = 0; i < graph->nb_filters; i++) {
1351 45692 AVFilterContext *filter = graph->filters[i];
1352
1353
2/2
✓ Branch 0 taken 37351 times.
✓ Branch 1 taken 45692 times.
83043 for (j = 0; j < filter->nb_inputs; j++)
1354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37351 times.
37351 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1355 return ret;
1356
2/2
✓ Branch 0 taken 37351 times.
✓ Branch 1 taken 45692 times.
83043 for (j = 0; j < filter->nb_outputs; j++)
1357
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37351 times.
37351 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1358 return ret;
1359 }
1360 8303 return 0;
1361 }
1362
1363 /**
1364 * Configure the formats of all the links in the graph.
1365 */
1366 8303 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1367 {
1368 int ret;
1369
1370 /* find supported formats from sub-filters, and merge along links */
1371
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 8303 times.
8318 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1372 15 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8303 times.
8303 if (ret < 0)
1374 return ret;
1375
1376 /* Once everything is merged, it's possible that we'll still have
1377 * multiple valid media format choices. We try to minimize the amount
1378 * of format conversion inside filters */
1379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8303 times.
8303 if ((ret = reduce_formats(graph)) < 0)
1380 return ret;
1381
1382 /* for audio filters, ensure the best format, sample rate and channel layout
1383 * is selected */
1384 8303 swap_sample_fmts(graph);
1385 8303 swap_samplerates(graph);
1386 8303 swap_channel_layouts(graph);
1387
1388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8303 times.
8303 if ((ret = pick_formats(graph)) < 0)
1389 return ret;
1390
1391 8303 return 0;
1392 }
1393
1394 8302 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1395 {
1396 unsigned i, j;
1397 8302 int sink_links_count = 0, n = 0;
1398 AVFilterContext *f;
1399 FilterLinkInternal **sinks;
1400
1401
2/2
✓ Branch 0 taken 45687 times.
✓ Branch 1 taken 8302 times.
53989 for (i = 0; i < graph->nb_filters; i++) {
1402 45687 f = graph->filters[i];
1403
2/2
✓ Branch 0 taken 37347 times.
✓ Branch 1 taken 45687 times.
83034 for (j = 0; j < f->nb_inputs; j++) {
1404 37347 ff_link_internal(f->inputs[j])->age_index = -1;
1405 }
1406
2/2
✓ Branch 0 taken 37347 times.
✓ Branch 1 taken 45687 times.
83034 for (j = 0; j < f->nb_outputs; j++) {
1407 37347 ff_link_internal(f->outputs[j])->age_index = -1;
1408 }
1409
2/2
✓ Branch 0 taken 8441 times.
✓ Branch 1 taken 37246 times.
45687 if (!f->nb_outputs) {
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8441 times.
8441 if (f->nb_inputs > INT_MAX - sink_links_count)
1411 return AVERROR(EINVAL);
1412 8441 sink_links_count += f->nb_inputs;
1413 }
1414 }
1415 8302 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8302 times.
8302 if (!sinks)
1417 return AVERROR(ENOMEM);
1418
2/2
✓ Branch 0 taken 45687 times.
✓ Branch 1 taken 8302 times.
53989 for (i = 0; i < graph->nb_filters; i++) {
1419 45687 f = graph->filters[i];
1420
2/2
✓ Branch 0 taken 8441 times.
✓ Branch 1 taken 37246 times.
45687 if (!f->nb_outputs) {
1421
2/2
✓ Branch 0 taken 8441 times.
✓ Branch 1 taken 8441 times.
16882 for (j = 0; j < f->nb_inputs; j++) {
1422 8441 sinks[n] = ff_link_internal(f->inputs[j]);
1423 8441 sinks[n]->age_index = n;
1424 8441 n++;
1425 }
1426 }
1427 }
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8302 times.
8302 av_assert0(n == sink_links_count);
1429 8302 fffiltergraph(graph)->sink_links = sinks;
1430 8302 fffiltergraph(graph)->sink_links_count = sink_links_count;
1431 8302 return 0;
1432 }
1433
1434 8303 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1435 {
1436 int ret;
1437
1438
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8302 times.
8303 if (graphctx->max_buffered_frames)
1439 1 fffiltergraph(graphctx)->frame_queues.max_queued = graphctx->max_buffered_frames;
1440
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8303 times.
8303 if ((ret = graph_check_validity(graphctx, log_ctx)))
1441 return ret;
1442
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8303 times.
8303 if ((ret = graph_config_formats(graphctx, log_ctx)))
1443 return ret;
1444
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8302 times.
8303 if ((ret = graph_config_links(graphctx, log_ctx)))
1445 1 return ret;
1446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8302 times.
8302 if ((ret = graph_check_links(graphctx, log_ctx)))
1447 return ret;
1448
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8302 times.
8302 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1449 return ret;
1450
1451 8302 return 0;
1452 }
1453
1454 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1455 {
1456 int i, r = AVERROR(ENOSYS);
1457
1458 if (!graph)
1459 return r;
1460
1461 if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1462 r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1463 if (r != AVERROR(ENOSYS))
1464 return r;
1465 }
1466
1467 if (res_len && res)
1468 res[0] = 0;
1469
1470 for (i = 0; i < graph->nb_filters; i++) {
1471 AVFilterContext *filter = graph->filters[i];
1472 if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1473 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1474 if (r != AVERROR(ENOSYS)) {
1475 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1476 return r;
1477 }
1478 }
1479 }
1480
1481 return r;
1482 }
1483
1484 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1485 {
1486 int i;
1487
1488 if(!graph)
1489 return 0;
1490
1491 for (i = 0; i < graph->nb_filters; i++) {
1492 AVFilterContext *filter = graph->filters[i];
1493 FFFilterContext *ctxi = fffilterctx(filter);
1494 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1495 AVFilterCommand **queue = &ctxi->command_queue, *next;
1496 while (*queue && (*queue)->time <= ts)
1497 queue = &(*queue)->next;
1498 next = *queue;
1499 *queue = av_mallocz(sizeof(AVFilterCommand));
1500 if (!*queue)
1501 return AVERROR(ENOMEM);
1502
1503 (*queue)->command = av_strdup(command);
1504 (*queue)->arg = av_strdup(arg);
1505 (*queue)->time = ts;
1506 (*queue)->flags = flags;
1507 (*queue)->next = next;
1508 if(flags & AVFILTER_CMD_FLAG_ONE)
1509 return 0;
1510 }
1511 }
1512
1513 return 0;
1514 }
1515
1516 467329 static void heap_bubble_up(FFFilterGraph *graph,
1517 FilterLinkInternal *li, int index)
1518 {
1519 467329 FilterLinkInternal **links = graph->sink_links;
1520
1521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 467329 times.
467329 av_assert0(index >= 0);
1522
1523
2/2
✓ Branch 0 taken 1896 times.
✓ Branch 1 taken 467329 times.
469225 while (index) {
1524 1896 int parent = (index - 1) >> 1;
1525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1896 times.
1896 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1526 break;
1527 1896 links[index] = links[parent];
1528 1896 links[index]->age_index = index;
1529 1896 index = parent;
1530 }
1531 467329 links[index] = li;
1532 467329 li->age_index = index;
1533 467329 }
1534
1535 467456 static void heap_bubble_down(FFFilterGraph *graph,
1536 FilterLinkInternal *li, int index)
1537 {
1538 467456 FilterLinkInternal **links = graph->sink_links;
1539
1540
1/2
✓ Branch 0 taken 467456 times.
✗ Branch 1 not taken.
467456 av_assert0(index >= 0);
1541
1542 5262 while (1) {
1543 472718 int child = 2 * index + 1;
1544
2/2
✓ Branch 0 taken 467095 times.
✓ Branch 1 taken 5623 times.
472718 if (child >= graph->sink_links_count)
1545 467095 break;
1546
2/2
✓ Branch 0 taken 4193 times.
✓ Branch 1 taken 1430 times.
5623 if (child + 1 < graph->sink_links_count &&
1547
2/2
✓ Branch 0 taken 1289 times.
✓ Branch 1 taken 2904 times.
4193 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1548 1289 child++;
1549
2/2
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 5262 times.
5623 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1550 361 break;
1551 5262 links[index] = links[child];
1552 5262 links[index]->age_index = index;
1553 5262 index = child;
1554 }
1555 467456 links[index] = li;
1556 467456 li->age_index = index;
1557 467456 }
1558
1559 467329 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1560 {
1561 467329 FFFilterGraph *graphi = fffiltergraph(graph);
1562
1563 467329 heap_bubble_up (graphi, li, li->age_index);
1564 467329 heap_bubble_down(graphi, li, li->age_index);
1565 467329 }
1566
1567 455672 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1568 {
1569 455672 FFFilterGraph *graphi = fffiltergraph(graph);
1570 455672 FilterLinkInternal *oldesti = graphi->sink_links[0];
1571 455672 AVFilterLink *oldest = &oldesti->l.pub;
1572 int64_t frame_count;
1573 int r;
1574
1575
2/2
✓ Branch 0 taken 455805 times.
✓ Branch 1 taken 5108 times.
460913 while (graphi->sink_links_count) {
1576 455805 oldesti = graphi->sink_links[0];
1577 455805 oldest = &oldesti->l.pub;
1578
2/2
✓ Branch 1 taken 455783 times.
✓ Branch 2 taken 22 times.
455805 if (fffilter(oldest->dst->filter)->activate) {
1579 455783 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1580 AV_BUFFERSINK_FLAG_PEEK);
1581
2/2
✓ Branch 0 taken 450543 times.
✓ Branch 1 taken 5240 times.
455783 if (r != AVERROR_EOF)
1582 450543 return r;
1583 } else {
1584 22 r = ff_request_frame(oldest);
1585 }
1586
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 5241 times.
5262 if (r != AVERROR_EOF)
1587 21 break;
1588 5241 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1589 5241 oldest->dst->name,
1590 5241 oldest->dstpad->name);
1591 /* EOF: remove the link from the heap */
1592
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 5114 times.
5241 if (oldesti->age_index < --graphi->sink_links_count)
1593 127 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1594 oldesti->age_index);
1595 5241 oldesti->age_index = -1;
1596 }
1597
2/2
✓ Branch 0 taken 5108 times.
✓ Branch 1 taken 21 times.
5129 if (!graphi->sink_links_count)
1598 5108 return AVERROR_EOF;
1599 av_assert1(!fffilter(oldest->dst->filter)->activate);
1600 av_assert1(oldesti->age_index >= 0);
1601 21 frame_count = oldesti->l.frame_count_out;
1602
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
84 while (frame_count == oldesti->l.frame_count_out) {
1603 84 r = ff_filter_graph_run_once(graph);
1604
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 63 times.
84 if (r == FFERROR_BUFFERSRC_EMPTY)
1605 21 r = 0;
1606
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 63 times.
84 if (r == AVERROR(EAGAIN) &&
1607
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
21 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1608 !oldesti->status_in)
1609 (void)ff_request_frame(oldest);
1610
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 63 times.
84 else if (r < 0)
1611 21 return r;
1612 }
1613 return 0;
1614 }
1615
1616 5130170 int ff_filter_graph_run_once(AVFilterGraph *graph)
1617 {
1618 FFFilterContext *ctxi;
1619 unsigned i;
1620
1621
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5130170 times.
5130170 av_assert0(graph->nb_filters);
1622 5130170 ctxi = fffilterctx(graph->filters[0]);
1623
2/2
✓ Branch 0 taken 21510495 times.
✓ Branch 1 taken 5130170 times.
26640665 for (i = 1; i < graph->nb_filters; i++) {
1624 21510495 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1625
1626
2/2
✓ Branch 0 taken 3066304 times.
✓ Branch 1 taken 18444191 times.
21510495 if (ctxi_other->ready > ctxi->ready)
1627 3066304 ctxi = ctxi_other;
1628 }
1629
1630
2/2
✓ Branch 0 taken 849359 times.
✓ Branch 1 taken 4280811 times.
5130170 if (!ctxi->ready)
1631 849359 return AVERROR(EAGAIN);
1632 4280811 return ff_filter_activate(&ctxi->p);
1633 }
1634