FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2026-09-24 20:08:24
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 16693 AVFilterGraph *avfilter_graph_alloc(void)
86 {
87 16693 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
88 AVFilterGraph *ret;
89
90
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16693 times.
16693 if (!graph)
91 ✗ return NULL;
92
93 16693 ret = &graph->p;
94 16693 ret->av_class = &filtergraph_class;
95 16693 av_opt_set_defaults(ret);
96 16693 ff_framequeue_global_init(&graph->frame_queues);
97
98 16693 return ret;
99 }
100
101 64733 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
102 {
103 int i, j;
104
1/2
✓ Branch 0 taken 64733 times.
✗ Branch 1 not taken.
64733 for (i = 0; i < graph->nb_filters; i++) {
105
1/2
✓ Branch 0 taken 64733 times.
✗ Branch 1 not taken.
64733 if (graph->filters[i] == filter) {
106 64733 FFSWAP(AVFilterContext*, graph->filters[i],
107 graph->filters[graph->nb_filters - 1]);
108 64733 graph->nb_filters--;
109 64733 filter->graph = NULL;
110
2/2
✓ Branch 0 taken 56424 times.
✓ Branch 1 taken 64733 times.
121157 for (j = 0; j<filter->nb_outputs; j++)
111
2/2
✓ Branch 0 taken 21410 times.
✓ Branch 1 taken 35014 times.
56424 if (filter->outputs[j])
112 21410 ff_filter_link(filter->outputs[j])->graph = NULL;
113
114 64733 return;
115 }
116 }
117 }
118
119 24944 void avfilter_graph_free(AVFilterGraph **graphp)
120 {
121 24944 AVFilterGraph *graph = *graphp;
122 24944 FFFilterGraph *graphi = fffiltergraph(graph);
123
124
2/2
✓ Branch 0 taken 8251 times.
✓ Branch 1 taken 16693 times.
24944 if (!graph)
125 8251 return;
126
127
2/2
✓ Branch 0 taken 64667 times.
✓ Branch 1 taken 16693 times.
81360 while (graph->nb_filters)
128 64667 avfilter_free(graph->filters[0]);
129
130 16693 ff_graph_thread_free(graphi);
131
132 16693 av_freep(&graphi->sink_links);
133
134 16693 av_opt_free(graph);
135
136 16693 av_freep(&graph->filters);
137 16693 av_freep(graphp);
138 }
139
140 19865 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 19865 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19865 times.
19865 if (!*filt_ctx)
148 ✗ return AVERROR(ENOMEM);
149
150 19865 ret = avfilter_init_str(*filt_ctx, args);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19865 times.
19865 if (ret < 0)
152 ✗ goto fail;
153
154 19865 return 0;
155
156 ✗ fail:
157 ✗ avfilter_free(*filt_ctx);
158 ✗ *filt_ctx = NULL;
159 ✗ return ret;
160 }
161
162 6399 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
163 {
164 6399 fffiltergraph(graph)->disable_auto_convert = flags;
165 6399 }
166
167 64733 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
168 const AVFilter *filter,
169 const char *name)
170 {
171 AVFilterContext **filters, *s;
172 64733 FFFilterGraph *graphi = fffiltergraph(graph);
173
174
4/4
✓ Branch 0 taken 34453 times.
✓ Branch 1 taken 30280 times.
✓ Branch 2 taken 16693 times.
✓ Branch 3 taken 17760 times.
64733 if (graph->thread_type && !graphi->thread_execute) {
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16693 times.
16693 if (graph->execute) {
176 ✗ graphi->thread_execute = graph->execute;
177 } else {
178 16693 int ret = ff_graph_thread_init(graphi);
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16693 times.
16693 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 64733 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64733 times.
64733 if (!filters)
188 ✗ return NULL;
189 64733 graph->filters = filters;
190
191 64733 s = ff_filter_alloc(filter, name);
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64733 times.
64733 if (!s)
193 ✗ return NULL;
194
195 64733 graph->filters[graph->nb_filters++] = s;
196
197 64733 s->graph = graph;
198
199 64733 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 8372 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 44651 times.
✓ Branch 1 taken 8372 times.
53023 for (i = 0; i < graph->nb_filters; i++) {
216 const AVFilterPad *pad;
217 44651 filt = graph->filters[i];
218
219
2/2
✓ Branch 0 taken 36242 times.
✓ Branch 1 taken 44651 times.
80893 for (j = 0; j < filt->nb_inputs; j++) {
220
2/4
✓ Branch 0 taken 36242 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36242 times.
36242 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 36242 times.
✓ Branch 1 taken 44651 times.
80893 for (j = 0; j < filt->nb_outputs; j++) {
230
2/4
✓ Branch 0 taken 36242 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 36242 times.
36242 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 8372 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 8372 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 45987 times.
✓ Branch 1 taken 8371 times.
54358 for (i = 0; i < graph->nb_filters; i++) {
254 45987 filt = graph->filters[i];
255
256
2/2
✓ Branch 0 taken 8511 times.
✓ Branch 1 taken 37476 times.
45987 if (!filt->nb_outputs) {
257
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8510 times.
8511 if ((ret = ff_filter_config_links(filt)))
258 1 return ret;
259 }
260 }
261
262 8371 return 0;
263 }
264
265 8371 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 45982 times.
✓ Branch 1 taken 8371 times.
54353 for (i = 0; i < graph->nb_filters; i++) {
273 45982 f = graph->filters[i];
274
2/2
✓ Branch 0 taken 37574 times.
✓ Branch 1 taken 45982 times.
83556 for (j = 0; j < f->nb_outputs; j++) {
275 37574 l = f->outputs[j];
276
2/2
✓ Branch 0 taken 31186 times.
✓ Branch 1 taken 6388 times.
37574 if (l->type == AVMEDIA_TYPE_VIDEO) {
277 31186 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 31186 times.
31186 if (ret < 0)
279 ✗ return ret;
280 }
281 }
282 }
283 8371 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 61159 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
298 {
299 int ret;
300
301
2/3
✓ Branch 0 taken 51088 times.
✓ Branch 1 taken 10071 times.
✗ Branch 2 not taken.
61159 switch (link->type) {
302
303 51088 case AVMEDIA_TYPE_VIDEO:
304
2/4
✓ Branch 1 taken 51088 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 51088 times.
✗ Branch 4 not taken.
102176 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
305
1/2
✓ Branch 1 taken 51088 times.
✗ Branch 2 not taken.
102176 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 51088 times.
102176 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0 ||
307 51088 (ret = ff_formats_check_alpha_modes(log, cfg->alpha_modes)) < 0)
308 ✗ return ret;
309 51088 break;
310
311 10071 case AVMEDIA_TYPE_AUDIO:
312
2/4
✓ Branch 1 taken 10071 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10071 times.
✗ Branch 4 not taken.
20142 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
313
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10071 times.
20142 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
314 10071 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
315 ✗ return ret;
316 10071 break;
317
318 ✗ default:
319 ✗ av_assert0(!"reached");
320 }
321 61159 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 38441 static int filter_check_formats(AVFilterContext *ctx)
330 {
331 unsigned i;
332 int ret;
333
334
2/2
✓ Branch 0 taken 31155 times.
✓ Branch 1 taken 38441 times.
69596 for (i = 0; i < ctx->nb_inputs; i++) {
335 31155 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31155 times.
31155 if (ret < 0)
337 ✗ return ret;
338 }
339
2/2
✓ Branch 0 taken 30004 times.
✓ Branch 1 taken 38441 times.
68445 for (i = 0; i < ctx->nb_outputs; i++) {
340 30004 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30004 times.
30004 if (ret < 0)
342 ✗ return ret;
343 }
344 38441 return 0;
345 }
346
347 46004 static int filter_query_formats(AVFilterContext *ctx)
348 {
349 46004 const FFFilter *const filter = fffilter(ctx->filter);
350 int ret;
351
352
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 45969 times.
46004 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
353
2/2
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 18 times.
35 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 38423 times.
✓ Branch 1 taken 7546 times.
45969 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
360 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
361 38423 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 38422 times.
38423 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 31003 times.
✓ Branch 1 taken 7419 times.
38422 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
371
372
2/2
✓ Branch 0 taken 31131 times.
✓ Branch 1 taken 38423 times.
69554 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
373 31131 AVFilterLink *l = ctx->inputs[i];
374 31131 cfg_in[i] = &l->outcfg;
375 }
376
377
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 38422 times.
38423 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 29912 times.
✓ Branch 1 taken 8510 times.
38422 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
386
387
2/2
✓ Branch 0 taken 29986 times.
✓ Branch 1 taken 38423 times.
68409 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
388 29986 AVFilterLink *l = ctx->outputs[i];
389 29986 cfg_out[i] = &l->incfg;
390 }
391
392 38423 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
393 38423 av_freep(&cfg_in_dyn);
394 38423 av_freep(&cfg_out_dyn);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38423 times.
38423 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 45969 times.
✓ Branch 1 taken 18 times.
45987 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
404
2/2
✓ Branch 0 taken 38423 times.
✓ Branch 1 taken 7546 times.
45969 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
405 38441 ret = filter_check_formats(ctx);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38441 times.
38441 if (ret < 0)
407 ✗ return ret;
408 }
409
410 45987 return ff_default_query_formats(ctx);
411 }
412
413 44725 static int formats_declared(AVFilterContext *f)
414 {
415 int i;
416
417
2/2
✓ Branch 0 taken 36141 times.
✓ Branch 1 taken 8624 times.
44765 for (i = 0; i < f->nb_inputs; i++) {
418
2/2
✓ Branch 0 taken 36099 times.
✓ Branch 1 taken 42 times.
36141 if (!f->inputs[i]->outcfg.formats)
419 36099 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 8613 times.
✓ Branch 1 taken 57 times.
8670 for (i = 0; i < f->nb_outputs; i++) {
431
2/2
✓ Branch 0 taken 8567 times.
✓ Branch 1 taken 46 times.
8613 if (!f->outputs[i]->incfg.formats)
432 8567 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 44651 static void print_filter_formats(void *log_ctx, int level, const AVFilterContext *f)
477 {
478
1/2
✓ Branch 1 taken 44651 times.
✗ Branch 2 not taken.
44651 if (av_log_get_level() < level)
479 44651 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 8387 static int query_formats(AVFilterGraph *graph, void *log_ctx)
527 {
528 int i, j, k, ret;
529 8387 int converter_count = 0;
530 8387 int count_queried = 0; /* successful calls to query_formats() */
531 8387 int count_merged = 0; /* successful merge of formats lists */
532 8387 int count_already_merged = 0; /* lists already merged */
533 8387 int count_delayed = 0; /* lists that need to be merged later */
534
535
2/2
✓ Branch 0 taken 44725 times.
✓ Branch 1 taken 8387 times.
53112 for (i = 0; i < graph->nb_filters; i++) {
536 44725 AVFilterContext *f = graph->filters[i];
537
2/2
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 44668 times.
44725 if (formats_declared(f))
538 57 continue;
539 44668 ret = filter_query_formats(f);
540
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 44651 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
44668 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 44651 times.
✓ Branch 1 taken 17 times.
44668 if (ret >= 0) {
544 44651 print_filter_formats(log_ctx, AV_LOG_DEBUG, f);
545 44651 count_queried++;
546 }
547 }
548
549 /* go through and merge as many format lists as possible */
550 8387 retry:
551
2/2
✓ Branch 0 taken 65311 times.
✓ Branch 1 taken 8387 times.
73698 for (i = 0; i < graph->nb_filters; i++) {
552 65311 AVFilterContext *filter = graph->filters[i];
553
554
2/2
✓ Branch 0 taken 53239 times.
✓ Branch 1 taken 63975 times.
117214 for (j = 0; j < filter->nb_inputs; j++) {
555 53239 AVFilterLink *link = filter->inputs[j];
556 const AVFilterNegotiation *neg;
557 AVFilterContext *conv[4];
558 const AVFilterFormatsMerger *mergers[4]; /* triggered mergers */
559 53239 const char *conv_filters[4], *conv_opts[4] = {0};
560 53239 unsigned neg_step, num_conv = 0, num_mergers = 0;
561
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53239 times.
53239 if (!link)
563 ✗ continue;
564
565 53239 neg = ff_filter_get_negotiation(link);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53239 times.
53239 av_assert0(neg);
567
2/2
✓ Branch 0 taken 192875 times.
✓ Branch 1 taken 53239 times.
246114 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
568 192875 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
569 192875 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
570 192875 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
571
8/8
✓ Branch 0 taken 192811 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 192737 times.
✓ Branch 3 taken 74 times.
✓ Branch 4 taken 140507 times.
✓ Branch 5 taken 52230 times.
✓ Branch 7 taken 1415 times.
✓ Branch 8 taken 139092 times.
192875 if (a && b && a != b && !m->can_merge(a, b)) {
572
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 1336 times.
1415 for (k = 0; k < num_conv; k++) {
573
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 if (!strcmp(conv_filters[k], m->conversion_filter))
574 79 break;
575 }
576
2/2
✓ Branch 0 taken 1336 times.
✓ Branch 1 taken 79 times.
1415 if (k == num_conv) {
577 av_assert1(num_conv < FF_ARRAY_ELEMS(conv_filters));
578 1336 conv_filters[num_conv] = m->conversion_filter;
579
2/2
✓ Branch 0 taken 1335 times.
✓ Branch 1 taken 1 times.
1336 if (m->conversion_opts_offset)
580 1335 conv_opts[num_conv] = FF_FIELD_AT(char *, m->conversion_opts_offset, *graph);
581 1336 num_conv++;
582 }
583 av_assert1(num_mergers < FF_ARRAY_ELEMS(mergers));
584 1415 mergers[num_mergers++] = m;
585 }
586 }
587
2/2
✓ Branch 0 taken 192875 times.
✓ Branch 1 taken 53239 times.
246114 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
588 192875 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
589 192875 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
590 192875 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
591
4/4
✓ Branch 0 taken 192811 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 192737 times.
192875 if (!(a && b)) {
592 138 count_delayed++;
593
2/2
✓ Branch 0 taken 57765 times.
✓ Branch 1 taken 134972 times.
192737 } else if (a == b) {
594 57765 count_already_merged++;
595
2/2
✓ Branch 0 taken 130976 times.
✓ Branch 1 taken 3996 times.
134972 } else if (!num_conv) {
596 130976 count_merged++;
597 130976 ret = m->merge(a, b);
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130976 times.
130976 if (ret < 0)
599 ✗ return ret;
600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130976 times.
130976 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 1336 times.
✓ Branch 1 taken 53239 times.
54575 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 1336 times.
1336 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 1336 times.
1336 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 1336 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
636 conv_filters[k], converter_count++);
637 1336 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 1336 times.
1336 if (ret < 0)
640 ✗ return ret;
641
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1336 times.
1336 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 1336 times.
1336 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 1336 times.
✓ Branch 1 taken 53239 times.
54575 for (k = 0; k < num_conv; k++) {
650 1336 AVFilterLink *inlink = conv[k]->inputs[0];
651 1336 AVFilterLink *outlink = conv[k]->outputs[0];
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1336 times.
1336 av_assert0( inlink->incfg.formats->refcount > 0);
653
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1336 times.
1336 av_assert0( inlink->outcfg.formats->refcount > 0);
654
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1336 times.
1336 av_assert0(outlink->incfg.formats->refcount > 0);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1336 times.
1336 av_assert0(outlink->outcfg.formats->refcount > 0);
656
2/2
✓ Branch 0 taken 634 times.
✓ Branch 1 taken 702 times.
1336 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 702 times.
✗ Branch 1 not taken.
702 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0( inlink-> incfg.samplerates->refcount > 0);
671
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0( inlink->outcfg.samplerates->refcount > 0);
672
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0(outlink-> incfg.samplerates->refcount > 0);
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0(outlink->outcfg.samplerates->refcount > 0);
674
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
675
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
676
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 702 times.
702 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 4642 times.
✓ Branch 1 taken 1336 times.
5978 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
685 4642 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
686
2/2
✓ Branch 0 taken 636 times.
✓ Branch 1 taken 4006 times.
4642 if (strcmp(m->conversion_filter, conv_filters[k]))
687 636 continue;
688
2/4
✓ Branch 1 taken 4006 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4006 times.
8012 if ((ret = MERGE(m, inlink)) <= 0 ||
689 4006 (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 4006 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 1336 times.
✓ Branch 1 taken 51903 times.
53239 if (num_conv)
709 1336 goto retry;
710 }
711 }
712
713 8387 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 8372 times.
8387 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 8372 return 0;
738 }
739
740 1232 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
741 {
742 1232 int score = 0;
743
744
2/2
✓ Branch 2 taken 1132 times.
✓ Branch 3 taken 100 times.
1232 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
745 1132 score ++;
746
747
2/2
✓ Branch 2 taken 284 times.
✓ Branch 3 taken 948 times.
1232 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
748 284 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
749 }else
750 948 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
751
752
4/4
✓ Branch 1 taken 152 times.
✓ Branch 2 taken 1080 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 150 times.
1384 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
753 152 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
754 2 score += 20;
755
756
3/4
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 1128 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 104 times.
1336 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
757 104 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
758 ✗ score += 2;
759
760 1232 return score;
761 }
762
763 616 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 616 score1 = get_fmt_score(dst_fmt1, src_fmt);
769 616 score2 = get_fmt_score(dst_fmt2, src_fmt);
770
771
2/2
✓ Branch 0 taken 463 times.
✓ Branch 1 taken 153 times.
616 return score1 < score2 ? dst_fmt1 : dst_fmt2;
772 }
773
774 37003 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
775 {
776 37003 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37003 times.
37003 if (!desc)
778 ✗ return 0;
779
2/2
✓ Branch 0 taken 1813 times.
✓ Branch 1 taken 35190 times.
37003 if (desc->nb_components < 3)
780 1813 return 0; /* Grayscale is explicitly full-range in swscale */
781 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
782 35190 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 26396 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
788 {
789
2/2
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 25642 times.
26396 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 25642 default:
797 25642 return 0;
798 }
799 }
800
801 112664 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
802 {
803
3/4
✓ Branch 0 taken 112664 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 75086 times.
✓ Branch 3 taken 37578 times.
112664 if (!link || !link->incfg.formats)
804 75086 return 0;
805
806
2/2
✓ Branch 0 taken 31190 times.
✓ Branch 1 taken 6388 times.
37578 if (link->type == AVMEDIA_TYPE_VIDEO) {
807
3/4
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 31075 times.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
31190 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 115 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
810 115 enum AVPixelFormat best= AV_PIX_FMT_NONE;
811 int i;
812
2/2
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 115 times.
1234 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
813 1119 enum AVPixelFormat p = link->incfg.formats->formats[i];
814 1119 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
815 }
816 115 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
817 115 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
818 115 av_get_pix_fmt_name(ref->format), has_alpha);
819 115 link->incfg.formats->formats[0] = best;
820 }
821
1/2
✓ Branch 0 taken 6388 times.
✗ Branch 1 not taken.
6388 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
822
3/4
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 6235 times.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
6388 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
823 153 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
824 int i;
825
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 153 times.
769 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
826 616 enum AVSampleFormat p = link->incfg.formats->formats[i];
827 616 best = find_best_sample_fmt_of_2(best, p, ref->format);
828 }
829 153 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
830 153 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
831 153 av_get_sample_fmt_name(ref->format));
832 153 link->incfg.formats->formats[0] = best;
833 }
834 }
835
836 37578 link->incfg.formats->nb_formats = 1;
837 37578 link->format = link->incfg.formats->formats[0];
838
839
2/2
✓ Branch 0 taken 31190 times.
✓ Branch 1 taken 6388 times.
37578 if (link->type == AVMEDIA_TYPE_VIDEO) {
840 31190 enum AVPixelFormat swfmt = link->format;
841
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31190 times.
31190 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 31190 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
851
2/2
✓ Branch 1 taken 9889 times.
✓ Branch 2 taken 21301 times.
31190 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 9318 times.
9889 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
855 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
856 else
857 9318 link->color_range = AVCOL_RANGE_JPEG;
858
2/2
✓ Branch 0 taken 8275 times.
✓ Branch 1 taken 1614 times.
9889 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
859 8275 link->colorspace = AVCOL_SPC_RGB;
860 } else {
861 1614 link->colorspace = AVCOL_SPC_UNSPECIFIED;
862 }
863 } else {
864
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21301 times.
21301 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 21301 link->incfg.color_spaces->nb_formats = 1;
871 21301 link->colorspace = link->incfg.color_spaces->formats[0];
872
873
2/2
✓ Branch 1 taken 679 times.
✓ Branch 2 taken 20622 times.
21301 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 20622 times.
20622 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 20622 link->incfg.color_ranges->nb_formats = 1;
883 20622 link->color_range = link->incfg.color_ranges->formats[0];
884 }
885 }
886
887
2/2
✓ Branch 0 taken 3781 times.
✓ Branch 1 taken 27409 times.
31190 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 27409 link->alpha_mode = AVALPHA_MODE_UNSPECIFIED;
898 }
899
1/2
✓ Branch 0 taken 6388 times.
✗ Branch 1 not taken.
6388 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
900 int ret;
901
902
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6388 times.
6388 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 6388 link->incfg.samplerates->nb_formats = 1;
909 6388 link->sample_rate = link->incfg.samplerates->formats[0];
910
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6388 times.
6388 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 6388 link->incfg.channel_layouts->nb_channel_layouts = 1;
922 6388 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 6388 times.
6388 if (ret < 0)
924 ✗ return ret;
925 }
926
927 37578 ff_formats_unref(&link->incfg.formats);
928 37578 ff_formats_unref(&link->outcfg.formats);
929 37578 ff_formats_unref(&link->incfg.samplerates);
930 37578 ff_formats_unref(&link->outcfg.samplerates);
931 37578 ff_channel_layouts_unref(&link->incfg.channel_layouts);
932 37578 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
933 37578 ff_formats_unref(&link->incfg.color_spaces);
934 37578 ff_formats_unref(&link->outcfg.color_spaces);
935 37578 ff_formats_unref(&link->incfg.color_ranges);
936 37578 ff_formats_unref(&link->outcfg.color_ranges);
937 37578 ff_formats_unref(&link->incfg.alpha_modes);
938 37578 ff_formats_unref(&link->outcfg.alpha_modes);
939
940 37578 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 73380 static int reduce_formats_on_filter(AVFilterContext *filter)
981 {
982 73380 int i, j, k, ret = 0;
983
984
16/20
✓ Branch 0 taken 60249 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1504 times.
✓ Branch 3 taken 58745 times.
✓ Branch 4 taken 45904 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 45296 times.
✓ Branch 7 taken 608 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 608 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 59 times.
✓ Branch 14 taken 4925 times.
✓ Branch 15 taken 4984 times.
✓ Branch 16 taken 549 times.
✓ Branch 17 taken 45916 times.
✓ Branch 18 taken 58745 times.
✓ Branch 19 taken 60249 times.
✓ Branch 20 taken 73380 times.
184470 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
985 nb_formats, ff_add_format);
986
18/20
✓ Branch 0 taken 11490 times.
✓ Branch 1 taken 48759 times.
✓ Branch 2 taken 1307 times.
✓ Branch 3 taken 10183 times.
✓ Branch 4 taken 8237 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 7392 times.
✓ Branch 7 taken 845 times.
✓ Branch 8 taken 794 times.
✓ Branch 9 taken 51 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 794 times.
✓ Branch 13 taken 51 times.
✓ Branch 14 taken 99 times.
✓ Branch 15 taken 150 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 8243 times.
✓ Branch 18 taken 9389 times.
✓ Branch 19 taken 60249 times.
✓ Branch 20 taken 73380 times.
141177 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
987 nb_formats, ff_add_format);
988
15/20
✓ Branch 0 taken 48759 times.
✓ Branch 1 taken 11490 times.
✓ Branch 2 taken 11464 times.
✓ Branch 3 taken 37295 times.
✓ Branch 4 taken 29223 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25279 times.
✓ Branch 7 taken 3944 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3944 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3944 times.
✓ Branch 14 taken 499 times.
✓ Branch 15 taken 4443 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 29223 times.
✓ Branch 18 taken 37295 times.
✓ Branch 19 taken 60249 times.
✓ Branch 20 taken 73380 times.
163351 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
989 nb_formats, ff_add_format);
990
15/20
✓ Branch 0 taken 48759 times.
✓ Branch 1 taken 11490 times.
✓ Branch 2 taken 8826 times.
✓ Branch 3 taken 39933 times.
✓ Branch 4 taken 30575 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 27250 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 30575 times.
✓ Branch 18 taken 39933 times.
✓ Branch 19 taken 60249 times.
✓ Branch 20 taken 73380 times.
167511 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
991 nb_formats, ff_add_format);
992
10/20
✓ Branch 0 taken 48759 times.
✓ Branch 1 taken 11490 times.
✓ Branch 2 taken 48149 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 60249 times.
✓ Branch 20 taken 73380 times.
134095 REDUCE_FORMATS(int, AVFilterFormats, alpha_modes, formats,
993 nb_formats, ff_add_format);
994
995 /* reduce channel layouts */
996
2/2
✓ Branch 0 taken 60249 times.
✓ Branch 1 taken 73380 times.
133629 for (i = 0; i < filter->nb_inputs; i++) {
997 60249 AVFilterLink *inlink = filter->inputs[i];
998 const AVChannelLayout *fmt;
999
1000
2/2
✓ Branch 0 taken 11490 times.
✓ Branch 1 taken 48759 times.
60249 if (!inlink->outcfg.channel_layouts ||
1001
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 11394 times.
11490 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
1002 48855 continue;
1003 11394 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
1004
1005
2/2
✓ Branch 0 taken 9017 times.
✓ Branch 1 taken 10544 times.
19561 for (j = 0; j < filter->nb_outputs; j++) {
1006 9017 AVFilterLink *outlink = filter->outputs[j];
1007 AVFilterChannelLayouts *fmts;
1008
1009 9017 fmts = outlink->incfg.channel_layouts;
1010
4/4
✓ Branch 0 taken 9011 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8110 times.
✓ Branch 3 taken 901 times.
9017 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
1011 8116 continue;
1012
1013
2/2
✓ Branch 0 taken 850 times.
✓ Branch 1 taken 51 times.
901 if (fmts->all_layouts &&
1014
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 843 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
850 (KNOWN(fmt) || fmts->all_counts)) {
1015 /* Turn the infinite list into a singleton */
1016 850 fmts->all_layouts = fmts->all_counts = 0;
1017 850 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 850 times.
850 if (ret < 0)
1019 ✗ return ret;
1020 850 ret = 1;
1021 850 break;
1022 }
1023
1024
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 6 times.
159 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
1025
2/2
✓ Branch 1 taken 45 times.
✓ Branch 2 taken 108 times.
153 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
1026 45 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
1027
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45 times.
45 if (ret < 0)
1028 ✗ return ret;
1029 45 fmts->nb_channel_layouts = 1;
1030 45 ret = 1;
1031 45 break;
1032 }
1033 }
1034 }
1035 }
1036
1037 73380 return ret;
1038 }
1039
1040 8372 static int reduce_formats(AVFilterGraph *graph)
1041 {
1042 int i, reduced, ret;
1043
1044 do {
1045 13058 reduced = 0;
1046
1047
2/2
✓ Branch 0 taken 73380 times.
✓ Branch 1 taken 13058 times.
86438 for (i = 0; i < graph->nb_filters; i++) {
1048
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 73380 times.
73380 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
1049 ✗ return ret;
1050 73380 reduced |= ret;
1051 }
1052
2/2
✓ Branch 0 taken 4686 times.
✓ Branch 1 taken 8372 times.
13058 } while (reduced);
1053
1054 8372 return 0;
1055 }
1056
1057 45987 static void swap_samplerates_on_filter(AVFilterContext *filter)
1058 {
1059 45987 AVFilterLink *link = NULL;
1060 int sample_rate;
1061 int i, j;
1062
1063
2/2
✓ Branch 0 taken 37488 times.
✓ Branch 1 taken 39685 times.
77173 for (i = 0; i < filter->nb_inputs; i++) {
1064 37488 link = filter->inputs[i];
1065
1066
2/2
✓ Branch 0 taken 6302 times.
✓ Branch 1 taken 31186 times.
37488 if (link->type == AVMEDIA_TYPE_AUDIO &&
1067
1/2
✓ Branch 0 taken 6302 times.
✗ Branch 1 not taken.
6302 link->outcfg.samplerates->nb_formats== 1)
1068 6302 break;
1069 }
1070
2/2
✓ Branch 0 taken 39685 times.
✓ Branch 1 taken 6302 times.
45987 if (i == filter->nb_inputs)
1071 39685 return;
1072
1073 6302 sample_rate = link->outcfg.samplerates->formats[0];
1074
1075
2/2
✓ Branch 0 taken 4892 times.
✓ Branch 1 taken 6302 times.
11194 for (i = 0; i < filter->nb_outputs; i++) {
1076 4892 AVFilterLink *outlink = filter->outputs[i];
1077 4892 int best_idx, best_diff = INT_MAX;
1078
1079
2/2
✓ Branch 0 taken 4890 times.
✓ Branch 1 taken 2 times.
4892 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1080
1/2
✓ Branch 0 taken 4890 times.
✗ Branch 1 not taken.
4890 outlink->incfg.samplerates->nb_formats < 2)
1081 4892 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 8372 static void swap_samplerates(AVFilterGraph *graph)
1099 {
1100 int i;
1101
1102
2/2
✓ Branch 0 taken 45987 times.
✓ Branch 1 taken 8372 times.
54359 for (i = 0; i < graph->nb_filters; i++)
1103 45987 swap_samplerates_on_filter(graph->filters[i]);
1104 8372 }
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 45987 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1141 {
1142 45987 AVFilterLink *link = NULL;
1143 int i, j, k;
1144
1145
2/2
✓ Branch 0 taken 37488 times.
✓ Branch 1 taken 39691 times.
77179 for (i = 0; i < filter->nb_inputs; i++) {
1146 37488 link = filter->inputs[i];
1147
1148
2/2
✓ Branch 0 taken 6302 times.
✓ Branch 1 taken 31186 times.
37488 if (link->type == AVMEDIA_TYPE_AUDIO &&
1149
2/2
✓ Branch 0 taken 6296 times.
✓ Branch 1 taken 6 times.
6302 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1150 6296 break;
1151 }
1152
2/2
✓ Branch 0 taken 39691 times.
✓ Branch 1 taken 6296 times.
45987 if (i == filter->nb_inputs)
1153 39691 return;
1154
1155
2/2
✓ Branch 0 taken 4889 times.
✓ Branch 1 taken 6296 times.
11185 for (i = 0; i < filter->nb_outputs; i++) {
1156 4889 AVFilterLink *outlink = filter->outputs[i];
1157 4889 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1158
1159
2/2
✓ Branch 0 taken 4887 times.
✓ Branch 1 taken 2 times.
4889 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1160
2/2
✓ Branch 0 taken 4884 times.
✓ Branch 1 taken 3 times.
4887 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1161 4886 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 8372 static void swap_channel_layouts(AVFilterGraph *graph)
1235 {
1236 int i;
1237
1238
2/2
✓ Branch 0 taken 45987 times.
✓ Branch 1 taken 8372 times.
54359 for (i = 0; i < graph->nb_filters; i++)
1239 45987 swap_channel_layouts_on_filter(graph->filters[i]);
1240 8372 }
1241
1242 45987 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1243 {
1244 45987 AVFilterLink *link = NULL;
1245 int format, bps;
1246 int i, j;
1247
1248
2/2
✓ Branch 0 taken 37558 times.
✓ Branch 1 taken 40108 times.
77666 for (i = 0; i < filter->nb_inputs; i++) {
1249 37558 link = filter->inputs[i];
1250
1251
2/2
✓ Branch 0 taken 6372 times.
✓ Branch 1 taken 31186 times.
37558 if (link->type == AVMEDIA_TYPE_AUDIO &&
1252
2/2
✓ Branch 0 taken 5879 times.
✓ Branch 1 taken 493 times.
6372 link->outcfg.formats->nb_formats == 1)
1253 5879 break;
1254 }
1255
2/2
✓ Branch 0 taken 40108 times.
✓ Branch 1 taken 5879 times.
45987 if (i == filter->nb_inputs)
1256 40108 return;
1257
1258 5879 format = link->outcfg.formats->formats[0];
1259 5879 bps = av_get_bytes_per_sample(format);
1260
1261
2/2
✓ Branch 0 taken 4491 times.
✓ Branch 1 taken 5879 times.
10370 for (i = 0; i < filter->nb_outputs; i++) {
1262 4491 AVFilterLink *outlink = filter->outputs[i];
1263 4491 int best_idx = -1, best_score = INT_MIN;
1264
1265
2/2
✓ Branch 0 taken 4489 times.
✓ Branch 1 taken 2 times.
4491 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1266
2/2
✓ Branch 0 taken 4321 times.
✓ Branch 1 taken 168 times.
4489 outlink->incfg.formats->nb_formats < 2)
1267 4323 continue;
1268
1269
2/2
✓ Branch 0 taken 322 times.
✓ Branch 1 taken 10 times.
332 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1270 322 int out_format = outlink->incfg.formats->formats[j];
1271 322 int out_bps = av_get_bytes_per_sample(out_format);
1272 int score;
1273
1274
4/4
✓ Branch 1 taken 181 times.
✓ Branch 2 taken 141 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 164 times.
503 if (av_get_packed_sample_fmt(out_format) == format ||
1275 181 av_get_planar_sample_fmt(out_format) == format) {
1276 158 best_idx = j;
1277 158 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 158 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
164 if (bps == 4 && out_bps == 8) {
1282 ✗ best_idx = j;
1283 ✗ break;
1284 }
1285
1286 /* prefer closest higher or equal bps */
1287 164 score = -abs(out_bps - bps);
1288
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 131 times.
164 if (out_bps >= bps)
1289 33 score += INT_MAX/2;
1290
1291
2/2
✓ Branch 0 taken 126 times.
✓ Branch 1 taken 38 times.
164 if (score > best_score) {
1292 126 best_score = score;
1293 126 best_idx = j;
1294 }
1295 }
1296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 168 times.
168 av_assert0(best_idx >= 0);
1297 168 FFSWAP(int, outlink->incfg.formats->formats[0],
1298 outlink->incfg.formats->formats[best_idx]);
1299 }
1300 }
1301
1302 8372 static void swap_sample_fmts(AVFilterGraph *graph)
1303 {
1304 int i;
1305
1306
2/2
✓ Branch 0 taken 45987 times.
✓ Branch 1 taken 8372 times.
54359 for (i = 0; i < graph->nb_filters; i++)
1307 45987 swap_sample_fmts_on_filter(graph->filters[i]);
1308
1309 8372 }
1310
1311 8372 static int pick_formats(AVFilterGraph *graph)
1312 {
1313 int i, j, ret;
1314 int change;
1315
1316 do{
1317 16772 change = 0;
1318
2/2
✓ Branch 0 taken 92925 times.
✓ Branch 1 taken 16772 times.
109697 for (i = 0; i < graph->nb_filters; i++) {
1319 92925 AVFilterContext *filter = graph->filters[i];
1320
2/2
✓ Branch 0 taken 75648 times.
✓ Branch 1 taken 17277 times.
92925 if (filter->nb_inputs){
1321
2/2
✓ Branch 0 taken 76039 times.
✓ Branch 1 taken 75648 times.
151687 for (j = 0; j < filter->nb_inputs; j++){
1322
4/4
✓ Branch 0 taken 16105 times.
✓ Branch 1 taken 59934 times.
✓ Branch 2 taken 15566 times.
✓ Branch 3 taken 539 times.
76039 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1323
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15566 times.
15566 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1324 ✗ return ret;
1325 15566 change = 1;
1326 }
1327 }
1328 }
1329
2/2
✓ Branch 0 taken 75765 times.
✓ Branch 1 taken 17160 times.
92925 if (filter->nb_outputs){
1330
2/2
✓ Branch 0 taken 76039 times.
✓ Branch 1 taken 75765 times.
151804 for (j = 0; j < filter->nb_outputs; j++){
1331
4/4
✓ Branch 0 taken 22337 times.
✓ Branch 1 taken 53702 times.
✓ Branch 2 taken 21674 times.
✓ Branch 3 taken 663 times.
76039 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1332
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21674 times.
21674 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1333 ✗ return ret;
1334 21674 change = 1;
1335 }
1336 }
1337 }
1338
6/6
✓ Branch 0 taken 75648 times.
✓ Branch 1 taken 17277 times.
✓ Branch 2 taken 58488 times.
✓ Branch 3 taken 17160 times.
✓ Branch 4 taken 58201 times.
✓ Branch 5 taken 287 times.
92925 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1339
2/2
✓ Branch 0 taken 58407 times.
✓ Branch 1 taken 58201 times.
116608 for (j = 0; j < filter->nb_outputs; j++) {
1340
2/2
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 58139 times.
58407 if (filter->outputs[j]->format<0) {
1341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 268 times.
268 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1342 ✗ return ret;
1343 268 change = 1;
1344 }
1345 }
1346 }
1347 }
1348
2/2
✓ Branch 0 taken 8400 times.
✓ Branch 1 taken 8372 times.
16772 }while(change);
1349
1350
2/2
✓ Branch 0 taken 45987 times.
✓ Branch 1 taken 8372 times.
54359 for (i = 0; i < graph->nb_filters; i++) {
1351 45987 AVFilterContext *filter = graph->filters[i];
1352
1353
2/2
✓ Branch 0 taken 37578 times.
✓ Branch 1 taken 45987 times.
83565 for (j = 0; j < filter->nb_inputs; j++)
1354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37578 times.
37578 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1355 ✗ return ret;
1356
2/2
✓ Branch 0 taken 37578 times.
✓ Branch 1 taken 45987 times.
83565 for (j = 0; j < filter->nb_outputs; j++)
1357
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37578 times.
37578 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1358 ✗ return ret;
1359 }
1360 8372 return 0;
1361 }
1362
1363 /**
1364 * Configure the formats of all the links in the graph.
1365 */
1366 8372 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 8372 times.
8387 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 8372 times.
8372 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 8372 times.
8372 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 8372 swap_sample_fmts(graph);
1385 8372 swap_samplerates(graph);
1386 8372 swap_channel_layouts(graph);
1387
1388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8372 times.
8372 if ((ret = pick_formats(graph)) < 0)
1389 ✗ return ret;
1390
1391 8372 return 0;
1392 }
1393
1394 8371 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1395 {
1396 unsigned i, j;
1397 8371 int sink_links_count = 0, n = 0;
1398 AVFilterContext *f;
1399 FilterLinkInternal **sinks;
1400
1401
2/2
✓ Branch 0 taken 45982 times.
✓ Branch 1 taken 8371 times.
54353 for (i = 0; i < graph->nb_filters; i++) {
1402 45982 f = graph->filters[i];
1403
2/2
✓ Branch 0 taken 37574 times.
✓ Branch 1 taken 45982 times.
83556 for (j = 0; j < f->nb_inputs; j++) {
1404 37574 ff_link_internal(f->inputs[j])->age_index = -1;
1405 }
1406
2/2
✓ Branch 0 taken 37574 times.
✓ Branch 1 taken 45982 times.
83556 for (j = 0; j < f->nb_outputs; j++) {
1407 37574 ff_link_internal(f->outputs[j])->age_index = -1;
1408 }
1409
2/2
✓ Branch 0 taken 8510 times.
✓ Branch 1 taken 37472 times.
45982 if (!f->nb_outputs) {
1410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8510 times.
8510 if (f->nb_inputs > INT_MAX - sink_links_count)
1411 ✗ return AVERROR(EINVAL);
1412 8510 sink_links_count += f->nb_inputs;
1413 }
1414 }
1415 8371 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8371 times.
8371 if (!sinks)
1417 ✗ return AVERROR(ENOMEM);
1418
2/2
✓ Branch 0 taken 45982 times.
✓ Branch 1 taken 8371 times.
54353 for (i = 0; i < graph->nb_filters; i++) {
1419 45982 f = graph->filters[i];
1420
2/2
✓ Branch 0 taken 8510 times.
✓ Branch 1 taken 37472 times.
45982 if (!f->nb_outputs) {
1421
2/2
✓ Branch 0 taken 8510 times.
✓ Branch 1 taken 8510 times.
17020 for (j = 0; j < f->nb_inputs; j++) {
1422 8510 sinks[n] = ff_link_internal(f->inputs[j]);
1423 8510 sinks[n]->age_index = n;
1424 8510 n++;
1425 }
1426 }
1427 }
1428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8371 times.
8371 av_assert0(n == sink_links_count);
1429 8371 fffiltergraph(graph)->sink_links = sinks;
1430 8371 fffiltergraph(graph)->sink_links_count = sink_links_count;
1431 8371 return 0;
1432 }
1433
1434 8372 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 8371 times.
8372 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 8372 times.
8372 if ((ret = graph_check_validity(graphctx, log_ctx)))
1441 ✗ return ret;
1442
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8372 times.
8372 if ((ret = graph_config_formats(graphctx, log_ctx)))
1443 ✗ return ret;
1444
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 8371 times.
8372 if ((ret = graph_config_links(graphctx, log_ctx)))
1445 1 return ret;
1446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8371 times.
8371 if ((ret = graph_check_links(graphctx, log_ctx)))
1447 ✗ return ret;
1448
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8371 times.
8371 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1449 ✗ return ret;
1450
1451 8371 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 470414 static void heap_bubble_up(FFFilterGraph *graph,
1517 FilterLinkInternal *li, int index)
1518 {
1519 470414 FilterLinkInternal **links = graph->sink_links;
1520
1521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 470414 times.
470414 av_assert0(index >= 0);
1522
1523
2/2
✓ Branch 0 taken 1897 times.
✓ Branch 1 taken 470414 times.
472311 while (index) {
1524 1897 int parent = (index - 1) >> 1;
1525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1897 times.
1897 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1526 ✗ break;
1527 1897 links[index] = links[parent];
1528 1897 links[index]->age_index = index;
1529 1897 index = parent;
1530 }
1531 470414 links[index] = li;
1532 470414 li->age_index = index;
1533 470414 }
1534
1535 470541 static void heap_bubble_down(FFFilterGraph *graph,
1536 FilterLinkInternal *li, int index)
1537 {
1538 470541 FilterLinkInternal **links = graph->sink_links;
1539
1540
1/2
✓ Branch 0 taken 470541 times.
✗ Branch 1 not taken.
470541 av_assert0(index >= 0);
1541
1542 5261 while (1) {
1543 475802 int child = 2 * index + 1;
1544
2/2
✓ Branch 0 taken 470179 times.
✓ Branch 1 taken 5623 times.
475802 if (child >= graph->sink_links_count)
1545 470179 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 362 times.
✓ Branch 1 taken 5261 times.
5623 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1550 362 break;
1551 5261 links[index] = links[child];
1552 5261 links[index]->age_index = index;
1553 5261 index = child;
1554 }
1555 470541 links[index] = li;
1556 470541 li->age_index = index;
1557 470541 }
1558
1559 470414 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1560 {
1561 470414 FFFilterGraph *graphi = fffiltergraph(graph);
1562
1563 470414 heap_bubble_up (graphi, li, li->age_index);
1564 470414 heap_bubble_down(graphi, li, li->age_index);
1565 470414 }
1566
1567 458695 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1568 {
1569 458695 FFFilterGraph *graphi = fffiltergraph(graph);
1570 458695 FilterLinkInternal *oldesti = graphi->sink_links[0];
1571 458695 AVFilterLink *oldest = &oldesti->l.pub;
1572 int64_t frame_count;
1573 int r;
1574
1575
2/2
✓ Branch 0 taken 458828 times.
✓ Branch 1 taken 5151 times.
463979 while (graphi->sink_links_count) {
1576 458828 oldesti = graphi->sink_links[0];
1577 458828 oldest = &oldesti->l.pub;
1578
2/2
✓ Branch 1 taken 458820 times.
✓ Branch 2 taken 8 times.
458828 if (fffilter(oldest->dst->filter)->activate) {
1579 458820 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1580 AV_BUFFERSINK_FLAG_PEEK);
1581
2/2
✓ Branch 0 taken 453537 times.
✓ Branch 1 taken 5283 times.
458820 if (r != AVERROR_EOF)
1582 453537 return r;
1583 } else {
1584 8 r = ff_request_frame(oldest);
1585 }
1586
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5284 times.
5291 if (r != AVERROR_EOF)
1587 7 break;
1588 5284 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1589 5284 oldest->dst->name,
1590 5284 oldest->dstpad->name);
1591 /* EOF: remove the link from the heap */
1592
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 5157 times.
5284 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 5284 oldesti->age_index = -1;
1596 }
1597
2/2
✓ Branch 0 taken 5151 times.
✓ Branch 1 taken 7 times.
5158 if (!graphi->sink_links_count)
1598 5151 return AVERROR_EOF;
1599 av_assert1(!fffilter(oldest->dst->filter)->activate);
1600 av_assert1(oldesti->age_index >= 0);
1601 7 frame_count = oldesti->l.frame_count_out;
1602
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 while (frame_count == oldesti->l.frame_count_out) {
1603 28 r = ff_filter_graph_run_once(graph);
1604
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 if (r == FFERROR_BUFFERSRC_EMPTY)
1605 7 r = 0;
1606
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 if (r == AVERROR(EAGAIN) &&
1607
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1608 ✗ !oldesti->status_in)
1609 ✗ (void)ff_request_frame(oldest);
1610
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 else if (r < 0)
1611 7 return r;
1612 }
1613 ✗ return 0;
1614 }
1615
1616 5165807 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 5165807 times.
5165807 av_assert0(graph->nb_filters);
1622 5165807 ctxi = fffilterctx(graph->filters[0]);
1623
2/2
✓ Branch 0 taken 21659435 times.
✓ Branch 1 taken 5165807 times.
26825242 for (i = 1; i < graph->nb_filters; i++) {
1624 21659435 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1625
1626
2/2
✓ Branch 0 taken 3087061 times.
✓ Branch 1 taken 18572374 times.
21659435 if (ctxi_other->ready > ctxi->ready)
1627 3087061 ctxi = ctxi_other;
1628 }
1629
1630
2/2
✓ Branch 0 taken 855483 times.
✓ Branch 1 taken 4310324 times.
5165807 if (!ctxi->ready)
1631 855483 return AVERROR(EAGAIN);
1632 4310324 return ff_filter_activate(&ctxi->p);
1633 }
1634