FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2025-07-12 02:25:37
Exec Total Coverage
Lines: 627 793 79.1%
Functions: 36 39 92.3%
Branches: 523 747 70.0%

Line Branch Exec Source
1 /*
2 * filter graphs
3 * Copyright (c) 2008 Vitor Sessak
4 * Copyright (c) 2007 Bobby Bingham
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <string.h>
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/bprint.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/imgutils.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34
35
36 #include "avfilter.h"
37 #include "avfilter_internal.h"
38 #include "buffersink.h"
39 #include "filters.h"
40 #include "formats.h"
41 #include "framequeue.h"
42 #include "video.h"
43
44 #define OFFSET(x) offsetof(AVFilterGraph, x)
45 #define F AV_OPT_FLAG_FILTERING_PARAM
46 #define V AV_OPT_FLAG_VIDEO_PARAM
47 #define A AV_OPT_FLAG_AUDIO_PARAM
48 static const AVOption filtergraph_options[] = {
49 { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
50 { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, F|V|A, .unit = "thread_type" },
51 { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = F|V|A, .unit = "thread_type" },
52 { "threads", "Maximum number of threads", OFFSET(nb_threads), AV_OPT_TYPE_INT,
53 { .i64 = 0 }, 0, INT_MAX, F|V|A, .unit = "threads"},
54 {"auto", "autodetect a suitable number of threads to use", 0, AV_OPT_TYPE_CONST, {.i64 = 0 }, .flags = F|V|A, .unit = "threads"},
55 {"scale_sws_opts" , "default scale filter options" , OFFSET(scale_sws_opts) ,
56 AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|V },
57 {"aresample_swr_opts" , "default aresample filter options" , OFFSET(aresample_swr_opts) ,
58 AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, F|A },
59 { NULL },
60 };
61
62 static const AVClass filtergraph_class = {
63 .class_name = "AVFilterGraph",
64 .item_name = av_default_item_name,
65 .version = LIBAVUTIL_VERSION_INT,
66 .option = filtergraph_options,
67 .category = AV_CLASS_CATEGORY_FILTER,
68 };
69
70 #if !HAVE_THREADS
71 void ff_graph_thread_free(FFFilterGraph *graph)
72 {
73 }
74
75 int ff_graph_thread_init(FFFilterGraph *graph)
76 {
77 graph->p.thread_type = 0;
78 graph->p.nb_threads = 1;
79 return 0;
80 }
81 #endif
82
83 16115 AVFilterGraph *avfilter_graph_alloc(void)
84 {
85 16115 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
86 AVFilterGraph *ret;
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16115 times.
16115 if (!graph)
89 return NULL;
90
91 16115 ret = &graph->p;
92 16115 ret->av_class = &filtergraph_class;
93 16115 av_opt_set_defaults(ret);
94 16115 ff_framequeue_global_init(&graph->frame_queues);
95
96 16115 return ret;
97 }
98
99 62773 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
100 {
101 int i, j;
102
1/2
✓ Branch 0 taken 62773 times.
✗ Branch 1 not taken.
62773 for (i = 0; i < graph->nb_filters; i++) {
103
1/2
✓ Branch 0 taken 62773 times.
✗ Branch 1 not taken.
62773 if (graph->filters[i] == filter) {
104 62773 FFSWAP(AVFilterContext*, graph->filters[i],
105 graph->filters[graph->nb_filters - 1]);
106 62773 graph->nb_filters--;
107 62773 filter->graph = NULL;
108
2/2
✓ Branch 0 taken 54760 times.
✓ Branch 1 taken 62773 times.
117533 for (j = 0; j<filter->nb_outputs; j++)
109
2/2
✓ Branch 0 taken 20715 times.
✓ Branch 1 taken 34045 times.
54760 if (filter->outputs[j])
110 20715 ff_filter_link(filter->outputs[j])->graph = NULL;
111
112 62773 return;
113 }
114 }
115 }
116
117 24093 void avfilter_graph_free(AVFilterGraph **graphp)
118 {
119 24093 AVFilterGraph *graph = *graphp;
120 24093 FFFilterGraph *graphi = fffiltergraph(graph);
121
122
2/2
✓ Branch 0 taken 7978 times.
✓ Branch 1 taken 16115 times.
24093 if (!graph)
123 7978 return;
124
125
2/2
✓ Branch 0 taken 62707 times.
✓ Branch 1 taken 16115 times.
78822 while (graph->nb_filters)
126 62707 avfilter_free(graph->filters[0]);
127
128 16115 ff_graph_thread_free(graphi);
129
130 16115 av_freep(&graphi->sink_links);
131
132 16115 av_opt_free(graph);
133
134 16115 av_freep(&graph->filters);
135 16115 av_freep(graphp);
136 }
137
138 19075 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt,
139 const char *name, const char *args, void *opaque,
140 AVFilterGraph *graph_ctx)
141 {
142 int ret;
143
144 19075 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19075 times.
19075 if (!*filt_ctx)
146 return AVERROR(ENOMEM);
147
148 19075 ret = avfilter_init_str(*filt_ctx, args);
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19075 times.
19075 if (ret < 0)
150 goto fail;
151
152 19075 return 0;
153
154 fail:
155 avfilter_free(*filt_ctx);
156 *filt_ctx = NULL;
157 return ret;
158 }
159
160 6244 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
161 {
162 6244 fffiltergraph(graph)->disable_auto_convert = flags;
163 6244 }
164
165 62773 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
166 const AVFilter *filter,
167 const char *name)
168 {
169 AVFilterContext **filters, *s;
170 62773 FFFilterGraph *graphi = fffiltergraph(graph);
171
172
4/4
✓ Branch 0 taken 33000 times.
✓ Branch 1 taken 29773 times.
✓ Branch 2 taken 16115 times.
✓ Branch 3 taken 16885 times.
62773 if (graph->thread_type && !graphi->thread_execute) {
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16115 times.
16115 if (graph->execute) {
174 graphi->thread_execute = graph->execute;
175 } else {
176 16115 int ret = ff_graph_thread_init(graphi);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16115 times.
16115 if (ret < 0) {
178 av_log(graph, AV_LOG_ERROR, "Error initializing threading: %s.\n", av_err2str(ret));
179 return NULL;
180 }
181 }
182 }
183
184 62773 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62773 times.
62773 if (!filters)
186 return NULL;
187 62773 graph->filters = filters;
188
189 62773 s = ff_filter_alloc(filter, name);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62773 times.
62773 if (!s)
191 return NULL;
192
193 62773 graph->filters[graph->nb_filters++] = s;
194
195 62773 s->graph = graph;
196
197 62773 return s;
198 }
199
200 /**
201 * Check for the validity of graph.
202 *
203 * A graph is considered valid if all its input and output pads are
204 * connected.
205 *
206 * @return >= 0 in case of success, a negative value otherwise
207 */
208 8071 static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
209 {
210 AVFilterContext *filt;
211 int i, j;
212
213
2/2
✓ Branch 0 taken 43245 times.
✓ Branch 1 taken 8071 times.
51316 for (i = 0; i < graph->nb_filters; i++) {
214 const AVFilterPad *pad;
215 43245 filt = graph->filters[i];
216
217
2/2
✓ Branch 0 taken 35137 times.
✓ Branch 1 taken 43245 times.
78382 for (j = 0; j < filt->nb_inputs; j++) {
218
2/4
✓ Branch 0 taken 35137 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35137 times.
35137 if (!filt->inputs[j] || !filt->inputs[j]->src) {
219 pad = &filt->input_pads[j];
220 av_log(log_ctx, AV_LOG_ERROR,
221 "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
222 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
223 return AVERROR(EINVAL);
224 }
225 }
226
227
2/2
✓ Branch 0 taken 35137 times.
✓ Branch 1 taken 43245 times.
78382 for (j = 0; j < filt->nb_outputs; j++) {
228
2/4
✓ Branch 0 taken 35137 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 35137 times.
35137 if (!filt->outputs[j] || !filt->outputs[j]->dst) {
229 pad = &filt->output_pads[j];
230 av_log(log_ctx, AV_LOG_ERROR,
231 "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
232 pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
233 return AVERROR(EINVAL);
234 }
235 }
236 }
237
238 8071 return 0;
239 }
240
241 /**
242 * Configure all the links of graphctx.
243 *
244 * @return >= 0 in case of success, a negative value otherwise
245 */
246 8071 static int graph_config_links(AVFilterGraph *graph, void *log_ctx)
247 {
248 AVFilterContext *filt;
249 int i, ret;
250
251
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++) {
252 44497 filt = graph->filters[i];
253
254
2/2
✓ Branch 0 taken 8205 times.
✓ Branch 1 taken 36292 times.
44497 if (!filt->nb_outputs) {
255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8205 times.
8205 if ((ret = ff_filter_config_links(filt)))
256 return ret;
257 }
258 }
259
260 8071 return 0;
261 }
262
263 8071 static int graph_check_links(AVFilterGraph *graph, void *log_ctx)
264 {
265 AVFilterContext *f;
266 AVFilterLink *l;
267 unsigned i, j;
268 int ret;
269
270
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++) {
271 44497 f = graph->filters[i];
272
2/2
✓ Branch 0 taken 36389 times.
✓ Branch 1 taken 44497 times.
80886 for (j = 0; j < f->nb_outputs; j++) {
273 36389 l = f->outputs[j];
274
2/2
✓ Branch 0 taken 30478 times.
✓ Branch 1 taken 5911 times.
36389 if (l->type == AVMEDIA_TYPE_VIDEO) {
275 30478 ret = av_image_check_size2(l->w, l->h, INT64_MAX, l->format, 0, f);
276
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30478 times.
30478 if (ret < 0)
277 return ret;
278 }
279 }
280 }
281 8071 return 0;
282 }
283
284 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, const char *name)
285 {
286 int i;
287
288 for (i = 0; i < graph->nb_filters; i++)
289 if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
290 return graph->filters[i];
291
292 return NULL;
293 }
294
295 59360 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
296 {
297 int ret;
298
299
2/3
✓ Branch 0 taken 49988 times.
✓ Branch 1 taken 9372 times.
✗ Branch 2 not taken.
59360 switch (link->type) {
300
301 49988 case AVMEDIA_TYPE_VIDEO:
302
2/4
✓ Branch 1 taken 49988 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 49988 times.
✗ Branch 4 not taken.
99976 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 49988 times.
99976 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
304 49988 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0)
305 return ret;
306 49988 break;
307
308 9372 case AVMEDIA_TYPE_AUDIO:
309
2/4
✓ Branch 1 taken 9372 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9372 times.
✗ Branch 4 not taken.
18744 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
310
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9372 times.
18744 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
311 9372 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
312 return ret;
313 9372 break;
314
315 default:
316 av_assert0(!"reached");
317 }
318 59360 return 0;
319 }
320
321 /**
322 * Check the validity of the formats / etc. lists set by query_formats().
323 *
324 * In particular, check they do not contain any redundant element.
325 */
326 37263 static int filter_check_formats(AVFilterContext *ctx)
327 {
328 unsigned i;
329 int ret;
330
331
2/2
✓ Branch 0 taken 30232 times.
✓ Branch 1 taken 37263 times.
67495 for (i = 0; i < ctx->nb_inputs; i++) {
332 30232 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30232 times.
30232 if (ret < 0)
334 return ret;
335 }
336
2/2
✓ Branch 0 taken 29128 times.
✓ Branch 1 taken 37263 times.
66391 for (i = 0; i < ctx->nb_outputs; i++) {
337 29128 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
338
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29128 times.
29128 if (ret < 0)
339 return ret;
340 }
341 37263 return 0;
342 }
343
344 44511 static int filter_query_formats(AVFilterContext *ctx)
345 {
346 44511 const FFFilter *const filter = fffilter(ctx->filter);
347 int ret;
348
349
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 44483 times.
44511 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
350
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 14 times.
28 if ((ret = filter->formats.query_func(ctx)) < 0) {
351
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret != AVERROR(EAGAIN))
352 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
353 ctx->name, av_err2str(ret));
354 14 return ret;
355 }
356
2/2
✓ Branch 0 taken 37249 times.
✓ Branch 1 taken 7234 times.
44483 } else if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
357 AVFilterFormatsConfig *cfg_in_stack[64], *cfg_out_stack[64];
358 37249 AVFilterFormatsConfig **cfg_in_dyn = NULL, **cfg_out_dyn = NULL;
359 AVFilterFormatsConfig **cfg_in, **cfg_out;
360
361
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37248 times.
37249 if (ctx->nb_inputs > FF_ARRAY_ELEMS(cfg_in_stack)) {
362 1 cfg_in_dyn = av_malloc_array(ctx->nb_inputs, sizeof(*cfg_in_dyn));
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_in_dyn)
364 return AVERROR(ENOMEM);
365 1 cfg_in = cfg_in_dyn;
366 } else
367
2/2
✓ Branch 0 taken 30103 times.
✓ Branch 1 taken 7145 times.
37248 cfg_in = ctx->nb_inputs ? cfg_in_stack : NULL;
368
369
2/2
✓ Branch 0 taken 30217 times.
✓ Branch 1 taken 37249 times.
67466 for (unsigned i = 0; i < ctx->nb_inputs; i++) {
370 30217 AVFilterLink *l = ctx->inputs[i];
371 30217 cfg_in[i] = &l->outcfg;
372 }
373
374
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37248 times.
37249 if (ctx->nb_outputs > FF_ARRAY_ELEMS(cfg_out_stack)) {
375 1 cfg_out_dyn = av_malloc_array(ctx->nb_outputs, sizeof(*cfg_out_dyn));
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!cfg_out_dyn) {
377 av_freep(&cfg_in_dyn);
378 return AVERROR(ENOMEM);
379 }
380 1 cfg_out = cfg_out_dyn;
381 } else
382
2/2
✓ Branch 0 taken 29044 times.
✓ Branch 1 taken 8204 times.
37248 cfg_out = ctx->nb_outputs ? cfg_out_stack : NULL;
383
384
2/2
✓ Branch 0 taken 29114 times.
✓ Branch 1 taken 37249 times.
66363 for (unsigned i = 0; i < ctx->nb_outputs; i++) {
385 29114 AVFilterLink *l = ctx->outputs[i];
386 29114 cfg_out[i] = &l->incfg;
387 }
388
389 37249 ret = filter->formats.query_func2(ctx, cfg_in, cfg_out);
390 37249 av_freep(&cfg_in_dyn);
391 37249 av_freep(&cfg_out_dyn);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37249 times.
37249 if (ret < 0) {
393 if (ret != AVERROR(EAGAIN))
394 av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
395 ctx->name, av_err2str(ret));
396 return ret;
397 }
398 }
399
400
2/2
✓ Branch 0 taken 44483 times.
✓ Branch 1 taken 14 times.
44497 if (filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC ||
401
2/2
✓ Branch 0 taken 37249 times.
✓ Branch 1 taken 7234 times.
44483 filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC2) {
402 37263 ret = filter_check_formats(ctx);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37263 times.
37263 if (ret < 0)
404 return ret;
405 }
406
407 44497 return ff_default_query_formats(ctx);
408 }
409
410 43303 static int formats_declared(AVFilterContext *f)
411 {
412 int i;
413
414
2/2
✓ Branch 0 taken 35047 times.
✓ Branch 1 taken 8289 times.
43336 for (i = 0; i < f->nb_inputs; i++) {
415
2/2
✓ Branch 0 taken 35012 times.
✓ Branch 1 taken 35 times.
35047 if (!f->inputs[i]->outcfg.formats)
416 35012 return 0;
417
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
35 if (f->inputs[i]->type == AVMEDIA_TYPE_VIDEO &&
418
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 !(f->inputs[i]->outcfg.color_ranges &&
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->inputs[i]->outcfg.color_spaces))
420 2 return 0;
421
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 31 times.
33 if (f->inputs[i]->type == AVMEDIA_TYPE_AUDIO &&
422
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 !(f->inputs[i]->outcfg.samplerates &&
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 f->inputs[i]->outcfg.channel_layouts))
424 return 0;
425 }
426
2/2
✓ Branch 0 taken 8279 times.
✓ Branch 1 taken 44 times.
8323 for (i = 0; i < f->nb_outputs; i++) {
427
2/2
✓ Branch 0 taken 8245 times.
✓ Branch 1 taken 34 times.
8279 if (!f->outputs[i]->incfg.formats)
428 8245 return 0;
429
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 3 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_VIDEO &&
430
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 !(f->outputs[i]->incfg.color_ranges &&
431
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 f->outputs[i]->incfg.color_spaces))
432 return 0;
433
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 31 times.
34 if (f->outputs[i]->type == AVMEDIA_TYPE_AUDIO &&
434
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !(f->outputs[i]->incfg.samplerates &&
435
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 f->outputs[i]->incfg.channel_layouts))
436 return 0;
437 }
438 44 return 1;
439 }
440
441 /**
442 * Perform one round of query_formats() and merging formats lists on the
443 * filter graph.
444 * @return >=0 if all links formats lists could be queried and merged;
445 * AVERROR(EAGAIN) some progress was made in the queries or merging
446 * and a later call may succeed;
447 * AVERROR(EIO) (may be changed) plus a log message if no progress
448 * was made and the negotiation is stuck;
449 * a negative error code if some other error happened
450 */
451 8085 static int query_formats(AVFilterGraph *graph, void *log_ctx)
452 {
453 int i, j, ret;
454 8085 int converter_count = 0;
455 8085 int count_queried = 0; /* successful calls to query_formats() */
456 8085 int count_merged = 0; /* successful merge of formats lists */
457 8085 int count_already_merged = 0; /* lists already merged */
458 8085 int count_delayed = 0; /* lists that need to be merged later */
459
460
2/2
✓ Branch 0 taken 43303 times.
✓ Branch 1 taken 8085 times.
51388 for (i = 0; i < graph->nb_filters; i++) {
461 43303 AVFilterContext *f = graph->filters[i];
462
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 43259 times.
43303 if (formats_declared(f))
463 44 continue;
464 43259 ret = filter_query_formats(f);
465
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 43245 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
43259 if (ret < 0 && ret != AVERROR(EAGAIN))
466 return ret;
467 /* note: EAGAIN could indicate a partial success, not counted yet */
468 43259 count_queried += ret >= 0;
469 }
470
471 /* go through and merge as many format lists as possible */
472
2/2
✓ Branch 0 taken 44555 times.
✓ Branch 1 taken 8085 times.
52640 for (i = 0; i < graph->nb_filters; i++) {
473 44555 AVFilterContext *filter = graph->filters[i];
474
475
2/2
✓ Branch 0 taken 36437 times.
✓ Branch 1 taken 44555 times.
80992 for (j = 0; j < filter->nb_inputs; j++) {
476 36437 AVFilterLink *link = filter->inputs[j];
477 const AVFilterNegotiation *neg;
478 unsigned neg_step;
479 36437 int convert_needed = 0;
480
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36437 times.
36437 if (!link)
482 continue;
483
484 36437 neg = ff_filter_get_negotiation(link);
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36437 times.
36437 av_assert0(neg);
486
2/2
✓ Branch 0 taken 108051 times.
✓ Branch 1 taken 35185 times.
143236 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
487 108051 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
488 108051 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
489 108051 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
490
8/8
✓ Branch 0 taken 108009 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 107966 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 104138 times.
✓ Branch 5 taken 3828 times.
✓ Branch 7 taken 1252 times.
✓ Branch 8 taken 102886 times.
108051 if (a && b && a != b && !m->can_merge(a, b)) {
491 1252 convert_needed = 1;
492 1252 break;
493 }
494 }
495
2/2
✓ Branch 0 taken 109311 times.
✓ Branch 1 taken 36437 times.
145748 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
496 109311 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
497 109311 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
498 109311 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
499
4/4
✓ Branch 0 taken 109269 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 109226 times.
109311 if (!(a && b)) {
500 85 count_delayed++;
501
2/2
✓ Branch 0 taken 8996 times.
✓ Branch 1 taken 100230 times.
109226 } else if (a == b) {
502 8996 count_already_merged++;
503
2/2
✓ Branch 0 taken 97055 times.
✓ Branch 1 taken 3175 times.
100230 } else if (!convert_needed) {
504 97055 count_merged++;
505 97055 ret = m->merge(a, b);
506
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97055 times.
97055 if (ret < 0)
507 return ret;
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 97055 times.
97055 if (!ret)
509 convert_needed = 1;
510 }
511 }
512
513
2/2
✓ Branch 0 taken 1252 times.
✓ Branch 1 taken 35185 times.
36437 if (convert_needed) {
514 AVFilterContext *convert;
515 const AVFilter *filter;
516 AVFilterLink *inlink, *outlink;
517 char inst_name[30];
518 const char *opts;
519
520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if (fffiltergraph(graph)->disable_auto_convert) {
521 av_log(log_ctx, AV_LOG_ERROR,
522 "The filters '%s' and '%s' do not have a common format "
523 "and automatic conversion is disabled.\n",
524 link->src->name, link->dst->name);
525 return AVERROR(EINVAL);
526 }
527
528 /* couldn't merge format lists. auto-insert conversion filter */
529
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if (!(filter = avfilter_get_by_name(neg->conversion_filter))) {
530 av_log(log_ctx, AV_LOG_ERROR,
531 "'%s' filter not present, cannot convert formats.\n",
532 neg->conversion_filter);
533 return AVERROR(EINVAL);
534 }
535 1252 snprintf(inst_name, sizeof(inst_name), "auto_%s_%d",
536 1252 neg->conversion_filter, converter_count++);
537 1252 opts = FF_FIELD_AT(char *, neg->conversion_opts_offset, *graph);
538 1252 ret = avfilter_graph_create_filter(&convert, filter, inst_name, opts, NULL, graph);
539
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 if (ret < 0)
540 return ret;
541
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
542 return ret;
543
544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1252 times.
1252 if ((ret = filter_query_formats(convert)) < 0)
545 return ret;
546
547 1252 inlink = convert->inputs[0];
548 1252 outlink = convert->outputs[0];
549
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 av_assert0( inlink->incfg.formats->refcount > 0);
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 av_assert0( inlink->outcfg.formats->refcount > 0);
551
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 av_assert0(outlink->incfg.formats->refcount > 0);
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1252 times.
1252 av_assert0(outlink->outcfg.formats->refcount > 0);
553
2/2
✓ Branch 0 taken 622 times.
✓ Branch 1 taken 630 times.
1252 if (outlink->type == AVMEDIA_TYPE_VIDEO) {
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink-> incfg.color_spaces->refcount > 0);
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink->outcfg.color_spaces->refcount > 0);
556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink-> incfg.color_spaces->refcount > 0);
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink->outcfg.color_spaces->refcount > 0);
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink-> incfg.color_ranges->refcount > 0);
559
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0( inlink->outcfg.color_ranges->refcount > 0);
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink-> incfg.color_ranges->refcount > 0);
561
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 622 times.
622 av_assert0(outlink->outcfg.color_ranges->refcount > 0);
562
1/2
✓ Branch 0 taken 630 times.
✗ Branch 1 not taken.
630 } else if (outlink->type == AVMEDIA_TYPE_AUDIO) {
563
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0( inlink-> incfg.samplerates->refcount > 0);
564
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0( inlink->outcfg.samplerates->refcount > 0);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0(outlink-> incfg.samplerates->refcount > 0);
566
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0(outlink->outcfg.samplerates->refcount > 0);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0( inlink-> incfg.channel_layouts->refcount > 0);
568
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0( inlink->outcfg.channel_layouts->refcount > 0);
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0(outlink-> incfg.channel_layouts->refcount > 0);
570
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 630 times.
630 av_assert0(outlink->outcfg.channel_layouts->refcount > 0);
571 }
572 #define MERGE(merger, link) \
573 ((merger)->merge(FF_FIELD_AT(void *, (merger)->offset, (link)->incfg), \
574 FF_FIELD_AT(void *, (merger)->offset, (link)->outcfg)))
575
2/2
✓ Branch 0 taken 3756 times.
✓ Branch 1 taken 1252 times.
5008 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
576 3756 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
577
2/4
✓ Branch 1 taken 3756 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3756 times.
7512 if ((ret = MERGE(m, inlink)) <= 0 ||
578 3756 (ret = MERGE(m, outlink)) <= 0) {
579 if (ret < 0)
580 return ret;
581 av_log(log_ctx, AV_LOG_ERROR,
582 "Impossible to convert between the formats supported by the filter "
583 "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
584 return AVERROR(ENOSYS);
585 }
586 }
587 }
588 }
589 }
590
591 8085 av_log(graph, AV_LOG_DEBUG, "query_formats: "
592 "%d queried, %d merged, %d already done, %d delayed\n",
593 count_queried, count_merged, count_already_merged, count_delayed);
594
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 8071 times.
8085 if (count_delayed) {
595 AVBPrint bp;
596
597 /* if count_queried > 0, one filter at least did set its formats,
598 that will give additional information to its neighbour;
599 if count_merged > 0, one pair of formats lists at least was merged,
600 that will give additional information to all connected filters;
601 in both cases, progress was made and a new round must be done */
602
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14 if (count_queried || count_merged)
603 14 return AVERROR(EAGAIN);
604 av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
605 for (i = 0; i < graph->nb_filters; i++)
606 if (!formats_declared(graph->filters[i]))
607 av_bprintf(&bp, "%s%s", bp.len ? ", " : "",
608 graph->filters[i]->name);
609 av_log(graph, AV_LOG_ERROR,
610 "The following filters could not choose their formats: %s\n"
611 "Consider inserting the (a)format filter near their input or "
612 "output.\n", bp.str);
613 return AVERROR(EIO);
614 }
615 8071 return 0;
616 }
617
618 1130 static int get_fmt_score(enum AVSampleFormat dst_fmt, enum AVSampleFormat src_fmt)
619 {
620 1130 int score = 0;
621
622
2/2
✓ Branch 2 taken 1035 times.
✓ Branch 3 taken 95 times.
1130 if (av_sample_fmt_is_planar(dst_fmt) != av_sample_fmt_is_planar(src_fmt))
623 1035 score ++;
624
625
2/2
✓ Branch 2 taken 254 times.
✓ Branch 3 taken 876 times.
1130 if (av_get_bytes_per_sample(dst_fmt) < av_get_bytes_per_sample(src_fmt)) {
626 254 score += 100 * (av_get_bytes_per_sample(src_fmt) - av_get_bytes_per_sample(dst_fmt));
627 }else
628 876 score += 10 * (av_get_bytes_per_sample(dst_fmt) - av_get_bytes_per_sample(src_fmt));
629
630
3/4
✓ Branch 1 taken 139 times.
✓ Branch 2 taken 991 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 139 times.
1269 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_S32 &&
631 139 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_FLT)
632 score += 20;
633
634
3/4
✓ Branch 1 taken 95 times.
✓ Branch 2 taken 1035 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 95 times.
1225 if (av_get_packed_sample_fmt(dst_fmt) == AV_SAMPLE_FMT_FLT &&
635 95 av_get_packed_sample_fmt(src_fmt) == AV_SAMPLE_FMT_S32)
636 score += 2;
637
638 1130 return score;
639 }
640
641 565 static enum AVSampleFormat find_best_sample_fmt_of_2(enum AVSampleFormat dst_fmt1, enum AVSampleFormat dst_fmt2,
642 enum AVSampleFormat src_fmt)
643 {
644 int score1, score2;
645
646 565 score1 = get_fmt_score(dst_fmt1, src_fmt);
647 565 score2 = get_fmt_score(dst_fmt2, src_fmt);
648
649
2/2
✓ Branch 0 taken 425 times.
✓ Branch 1 taken 140 times.
565 return score1 < score2 ? dst_fmt1 : dst_fmt2;
650 }
651
652 36183 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
653 {
654 36183 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
655
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36183 times.
36183 if (!desc)
656 return 0;
657
2/2
✓ Branch 0 taken 1770 times.
✓ Branch 1 taken 34413 times.
36183 if (desc->nb_components < 3)
658 1770 return 0; /* Grayscale is explicitly full-range in swscale */
659 av_assert1(!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL));
660 34413 return !(desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_PAL |
661 AV_PIX_FMT_FLAG_XYZ | AV_PIX_FMT_FLAG_FLOAT));
662 }
663
664
665 25786 int ff_fmt_is_forced_full_range(enum AVPixelFormat fmt)
666 {
667
2/2
✓ Branch 0 taken 745 times.
✓ Branch 1 taken 25041 times.
25786 switch (fmt) {
668 745 case AV_PIX_FMT_YUVJ420P:
669 case AV_PIX_FMT_YUVJ422P:
670 case AV_PIX_FMT_YUVJ444P:
671 case AV_PIX_FMT_YUVJ440P:
672 case AV_PIX_FMT_YUVJ411P:
673 745 return 1;
674 25041 default:
675 25041 return 0;
676 }
677 }
678
679 109116 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
680 {
681
3/4
✓ Branch 0 taken 109116 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 72727 times.
✓ Branch 3 taken 36389 times.
109116 if (!link || !link->incfg.formats)
682 72727 return 0;
683
684
2/2
✓ Branch 0 taken 30478 times.
✓ Branch 1 taken 5911 times.
36389 if (link->type == AVMEDIA_TYPE_VIDEO) {
685
3/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 30381 times.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
30478 if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
686 //FIXME: This should check for AV_PIX_FMT_FLAG_ALPHA after PAL8 pixel format without alpha is implemented
687 97 int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
688 97 enum AVPixelFormat best= AV_PIX_FMT_NONE;
689 int i;
690
2/2
✓ Branch 0 taken 1029 times.
✓ Branch 1 taken 97 times.
1126 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
691 1029 enum AVPixelFormat p = link->incfg.formats->formats[i];
692 1029 best= av_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
693 }
694 97 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
695 97 av_get_pix_fmt_name(best), link->incfg.formats->nb_formats,
696 97 av_get_pix_fmt_name(ref->format), has_alpha);
697 97 link->incfg.formats->formats[0] = best;
698 }
699
1/2
✓ Branch 0 taken 5911 times.
✗ Branch 1 not taken.
5911 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
700
3/4
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 5771 times.
✓ Branch 2 taken 140 times.
✗ Branch 3 not taken.
5911 if(ref && ref->type == AVMEDIA_TYPE_AUDIO){
701 140 enum AVSampleFormat best= AV_SAMPLE_FMT_NONE;
702 int i;
703
2/2
✓ Branch 0 taken 565 times.
✓ Branch 1 taken 140 times.
705 for (i = 0; i < link->incfg.formats->nb_formats; i++) {
704 565 enum AVSampleFormat p = link->incfg.formats->formats[i];
705 565 best = find_best_sample_fmt_of_2(best, p, ref->format);
706 }
707 140 av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s\n",
708 140 av_get_sample_fmt_name(best), link->incfg.formats->nb_formats,
709 140 av_get_sample_fmt_name(ref->format));
710 140 link->incfg.formats->formats[0] = best;
711 }
712 }
713
714 36389 link->incfg.formats->nb_formats = 1;
715 36389 link->format = link->incfg.formats->formats[0];
716
717
2/2
✓ Branch 0 taken 30478 times.
✓ Branch 1 taken 5911 times.
36389 if (link->type == AVMEDIA_TYPE_VIDEO) {
718 30478 enum AVPixelFormat swfmt = link->format;
719
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 30478 times.
30478 if (av_pix_fmt_desc_get(swfmt)->flags & AV_PIX_FMT_FLAG_HWACCEL) {
720 // FIXME: this is a hack - we'd like to use the sw_format of
721 // link->hw_frames_ctx here, but it is not yet available.
722 // To make this work properly we will need to either reorder
723 // things so that it is available here or somehow negotiate
724 // sw_format separately.
725 swfmt = AV_PIX_FMT_YUV420P;
726 }
727
728
2/2
✓ Branch 1 taken 9693 times.
✓ Branch 2 taken 20785 times.
30478 if (!ff_fmt_is_regular_yuv(swfmt)) {
729 9693 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(swfmt);
730 /* These fields are explicitly documented as affecting YUV only,
731 * so set them to sane values for other formats. */
732
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 9122 times.
9693 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
733 571 link->color_range = AVCOL_RANGE_UNSPECIFIED;
734 else
735 9122 link->color_range = AVCOL_RANGE_JPEG;
736
2/2
✓ Branch 0 taken 8117 times.
✓ Branch 1 taken 1576 times.
9693 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
737 8117 link->colorspace = AVCOL_SPC_RGB;
738 } else {
739 1576 link->colorspace = AVCOL_SPC_UNSPECIFIED;
740 }
741 } else {
742
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20785 times.
20785 if (!link->incfg.color_spaces->nb_formats) {
743 av_log(link->src, AV_LOG_ERROR, "Cannot select color space for"
744 " the link between filters %s and %s.\n", link->src->name,
745 link->dst->name);
746 return AVERROR(EINVAL);
747 }
748 20785 link->incfg.color_spaces->nb_formats = 1;
749 20785 link->colorspace = link->incfg.color_spaces->formats[0];
750
751
2/2
✓ Branch 1 taken 674 times.
✓ Branch 2 taken 20111 times.
20785 if (ff_fmt_is_forced_full_range(swfmt)) {
752 674 link->color_range = AVCOL_RANGE_JPEG;
753 } else {
754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20111 times.
20111 if (!link->incfg.color_ranges->nb_formats) {
755 av_log(link->src, AV_LOG_ERROR, "Cannot select color range for"
756 " the link between filters %s and %s.\n", link->src->name,
757 link->dst->name);
758 return AVERROR(EINVAL);
759 }
760 20111 link->incfg.color_ranges->nb_formats = 1;
761 20111 link->color_range = link->incfg.color_ranges->formats[0];
762 }
763 }
764
1/2
✓ Branch 0 taken 5911 times.
✗ Branch 1 not taken.
5911 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
765 int ret;
766
767
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5911 times.
5911 if (!link->incfg.samplerates->nb_formats) {
768 av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
769 " the link between filters %s and %s.\n", link->src->name,
770 link->dst->name);
771 return AVERROR(EINVAL);
772 }
773 5911 link->incfg.samplerates->nb_formats = 1;
774 5911 link->sample_rate = link->incfg.samplerates->formats[0];
775
776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5911 times.
5911 if (link->incfg.channel_layouts->all_layouts) {
777 av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
778 " the link between filters %s and %s.\n", link->src->name,
779 link->dst->name);
780 if (!link->incfg.channel_layouts->all_counts)
781 av_log(link->src, AV_LOG_ERROR, "Unknown channel layouts not "
782 "supported, try specifying a channel layout using "
783 "'aformat=channel_layouts=something'.\n");
784 return AVERROR(EINVAL);
785 }
786 5911 link->incfg.channel_layouts->nb_channel_layouts = 1;
787 5911 ret = av_channel_layout_copy(&link->ch_layout, &link->incfg.channel_layouts->channel_layouts[0]);
788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5911 times.
5911 if (ret < 0)
789 return ret;
790 }
791
792 36389 ff_formats_unref(&link->incfg.formats);
793 36389 ff_formats_unref(&link->outcfg.formats);
794 36389 ff_formats_unref(&link->incfg.samplerates);
795 36389 ff_formats_unref(&link->outcfg.samplerates);
796 36389 ff_channel_layouts_unref(&link->incfg.channel_layouts);
797 36389 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
798 36389 ff_formats_unref(&link->incfg.color_spaces);
799 36389 ff_formats_unref(&link->outcfg.color_spaces);
800 36389 ff_formats_unref(&link->incfg.color_ranges);
801 36389 ff_formats_unref(&link->outcfg.color_ranges);
802
803 36389 return 0;
804 }
805
806 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
807 do { \
808 for (i = 0; i < filter->nb_inputs; i++) { \
809 AVFilterLink *link = filter->inputs[i]; \
810 fmt_type fmt; \
811 \
812 if (!link->outcfg.list || link->outcfg.list->nb != 1) \
813 continue; \
814 fmt = link->outcfg.list->var[0]; \
815 \
816 for (j = 0; j < filter->nb_outputs; j++) { \
817 AVFilterLink *out_link = filter->outputs[j]; \
818 list_type *fmts; \
819 \
820 if (link->type != out_link->type || \
821 out_link->incfg.list->nb == 1) \
822 continue; \
823 fmts = out_link->incfg.list; \
824 \
825 if (!out_link->incfg.list->nb) { \
826 if ((ret = add_format(&out_link->incfg.list, fmt)) < 0)\
827 return ret; \
828 ret = 1; \
829 break; \
830 } \
831 \
832 for (k = 0; k < out_link->incfg.list->nb; k++) \
833 if (fmts->var[k] == fmt) { \
834 fmts->var[0] = fmt; \
835 fmts->nb = 1; \
836 ret = 1; \
837 break; \
838 } \
839 } \
840 } \
841 } while (0)
842
843 71334 static int reduce_formats_on_filter(AVFilterContext *filter)
844 {
845 71334 int i, j, k, ret = 0;
846
847
16/20
✓ Branch 0 taken 58616 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1362 times.
✓ Branch 3 taken 57254 times.
✓ Branch 4 taken 44758 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 44210 times.
✓ Branch 7 taken 548 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 548 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 51 times.
✓ Branch 14 taken 4666 times.
✓ Branch 15 taken 4717 times.
✓ Branch 16 taken 497 times.
✓ Branch 17 taken 44770 times.
✓ Branch 18 taken 57254 times.
✓ Branch 19 taken 58616 times.
✓ Branch 20 taken 71334 times.
179386 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
848 nb_formats, ff_add_format);
849
18/20
✓ Branch 0 taken 10708 times.
✓ Branch 1 taken 47908 times.
✓ Branch 2 taken 1173 times.
✓ Branch 3 taken 9535 times.
✓ Branch 4 taken 7728 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6963 times.
✓ Branch 7 taken 765 times.
✓ Branch 8 taken 729 times.
✓ Branch 9 taken 36 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 729 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 68 times.
✓ Branch 15 taken 104 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 7734 times.
✓ Branch 18 taken 8806 times.
✓ Branch 19 taken 58616 times.
✓ Branch 20 taken 71334 times.
137023 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
850 nb_formats, ff_add_format);
851
15/20
✓ Branch 0 taken 47908 times.
✓ Branch 1 taken 10708 times.
✓ Branch 2 taken 11011 times.
✓ Branch 3 taken 36897 times.
✓ Branch 4 taken 28937 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25029 times.
✓ Branch 7 taken 3908 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3908 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3908 times.
✓ Branch 14 taken 411 times.
✓ Branch 15 taken 4319 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 28937 times.
✓ Branch 18 taken 36897 times.
✓ Branch 19 taken 58616 times.
✓ Branch 20 taken 71334 times.
159298 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
852 nb_formats, ff_add_format);
853
15/20
✓ Branch 0 taken 47908 times.
✓ Branch 1 taken 10708 times.
✓ Branch 2 taken 8464 times.
✓ Branch 3 taken 39444 times.
✓ Branch 4 taken 30226 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 26919 times.
✓ Branch 7 taken 3307 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3307 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3307 times.
✓ Branch 14 taken 3287 times.
✓ Branch 15 taken 6594 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 30226 times.
✓ Branch 18 taken 39444 times.
✓ Branch 19 taken 58616 times.
✓ Branch 20 taken 71334 times.
163463 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
854 nb_formats, ff_add_format);
855
856 /* reduce channel layouts */
857
2/2
✓ Branch 0 taken 58616 times.
✓ Branch 1 taken 71334 times.
129950 for (i = 0; i < filter->nb_inputs; i++) {
858 58616 AVFilterLink *inlink = filter->inputs[i];
859 const AVChannelLayout *fmt;
860
861
2/2
✓ Branch 0 taken 10708 times.
✓ Branch 1 taken 47908 times.
58616 if (!inlink->outcfg.channel_layouts ||
862
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 10631 times.
10708 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
863 47985 continue;
864 10631 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
865
866
2/2
✓ Branch 0 taken 8447 times.
✓ Branch 1 taken 9790 times.
18237 for (j = 0; j < filter->nb_outputs; j++) {
867 8447 AVFilterLink *outlink = filter->outputs[j];
868 AVFilterChannelLayouts *fmts;
869
870 8447 fmts = outlink->incfg.channel_layouts;
871
4/4
✓ Branch 0 taken 8441 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 7559 times.
✓ Branch 3 taken 882 times.
8447 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
872 7565 continue;
873
874
2/2
✓ Branch 0 taken 841 times.
✓ Branch 1 taken 41 times.
882 if (fmts->all_layouts &&
875
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 834 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
841 (KNOWN(fmt) || fmts->all_counts)) {
876 /* Turn the infinite list into a singleton */
877 841 fmts->all_layouts = fmts->all_counts = 0;
878 841 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 841 times.
841 if (ret < 0)
880 return ret;
881 841 ret = 1;
882 841 break;
883 }
884
885
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 6 times.
128 for (k = 0; k < outlink->incfg.channel_layouts->nb_channel_layouts; k++) {
886
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 87 times.
122 if (!av_channel_layout_compare(&fmts->channel_layouts[k], fmt)) {
887 35 ret = av_channel_layout_copy(&fmts->channel_layouts[0], fmt);
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (ret < 0)
889 return ret;
890 35 fmts->nb_channel_layouts = 1;
891 35 ret = 1;
892 35 break;
893 }
894 }
895 }
896 }
897
898 71334 return ret;
899 }
900
901 8071 static int reduce_formats(AVFilterGraph *graph)
902 {
903 int i, reduced, ret;
904
905 do {
906 12648 reduced = 0;
907
908
2/2
✓ Branch 0 taken 71334 times.
✓ Branch 1 taken 12648 times.
83982 for (i = 0; i < graph->nb_filters; i++) {
909
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 71334 times.
71334 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
910 return ret;
911 71334 reduced |= ret;
912 }
913
2/2
✓ Branch 0 taken 4577 times.
✓ Branch 1 taken 8071 times.
12648 } while (reduced);
914
915 8071 return 0;
916 }
917
918 44497 static void swap_samplerates_on_filter(AVFilterContext *filter)
919 {
920 44497 AVFilterLink *link = NULL;
921 int sample_rate;
922 int i, j;
923
924
2/2
✓ Branch 0 taken 36306 times.
✓ Branch 1 taken 38665 times.
74971 for (i = 0; i < filter->nb_inputs; i++) {
925 36306 link = filter->inputs[i];
926
927
2/2
✓ Branch 0 taken 5832 times.
✓ Branch 1 taken 30474 times.
36306 if (link->type == AVMEDIA_TYPE_AUDIO &&
928
1/2
✓ Branch 0 taken 5832 times.
✗ Branch 1 not taken.
5832 link->outcfg.samplerates->nb_formats== 1)
929 5832 break;
930 }
931
2/2
✓ Branch 0 taken 38665 times.
✓ Branch 1 taken 5832 times.
44497 if (i == filter->nb_inputs)
932 38665 return;
933
934 5832 sample_rate = link->outcfg.samplerates->formats[0];
935
936
2/2
✓ Branch 0 taken 4548 times.
✓ Branch 1 taken 5832 times.
10380 for (i = 0; i < filter->nb_outputs; i++) {
937 4548 AVFilterLink *outlink = filter->outputs[i];
938 4548 int best_idx, best_diff = INT_MAX;
939
940
2/2
✓ Branch 0 taken 4546 times.
✓ Branch 1 taken 2 times.
4548 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
941
1/2
✓ Branch 0 taken 4546 times.
✗ Branch 1 not taken.
4546 outlink->incfg.samplerates->nb_formats < 2)
942 4548 continue;
943
944 for (j = 0; j < outlink->incfg.samplerates->nb_formats; j++) {
945 int diff = abs(sample_rate - outlink->incfg.samplerates->formats[j]);
946
947 av_assert0(diff < INT_MAX); // This would lead to the use of uninitialized best_diff but is only possible with invalid sample rates
948
949 if (diff < best_diff) {
950 best_diff = diff;
951 best_idx = j;
952 }
953 }
954 FFSWAP(int, outlink->incfg.samplerates->formats[0],
955 outlink->incfg.samplerates->formats[best_idx]);
956 }
957 }
958
959 8071 static void swap_samplerates(AVFilterGraph *graph)
960 {
961 int i;
962
963
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++)
964 44497 swap_samplerates_on_filter(graph->filters[i]);
965 8071 }
966
967 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
968 #define CH_FRONT_PAIR (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)
969 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT)
970 #define CH_WIDE_PAIR (AV_CH_WIDE_LEFT | AV_CH_WIDE_RIGHT)
971 #define CH_SIDE_PAIR (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)
972 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
973 #define CH_BACK_PAIR (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)
974
975 /* allowable substitutions for channel pairs when comparing layouts,
976 * ordered by priority for both values */
977 static const uint64_t ch_subst[][2] = {
978 { CH_FRONT_PAIR, CH_CENTER_PAIR },
979 { CH_FRONT_PAIR, CH_WIDE_PAIR },
980 { CH_FRONT_PAIR, AV_CH_FRONT_CENTER },
981 { CH_CENTER_PAIR, CH_FRONT_PAIR },
982 { CH_CENTER_PAIR, CH_WIDE_PAIR },
983 { CH_CENTER_PAIR, AV_CH_FRONT_CENTER },
984 { CH_WIDE_PAIR, CH_FRONT_PAIR },
985 { CH_WIDE_PAIR, CH_CENTER_PAIR },
986 { CH_WIDE_PAIR, AV_CH_FRONT_CENTER },
987 { AV_CH_FRONT_CENTER, CH_FRONT_PAIR },
988 { AV_CH_FRONT_CENTER, CH_CENTER_PAIR },
989 { AV_CH_FRONT_CENTER, CH_WIDE_PAIR },
990 { CH_SIDE_PAIR, CH_DIRECT_PAIR },
991 { CH_SIDE_PAIR, CH_BACK_PAIR },
992 { CH_SIDE_PAIR, AV_CH_BACK_CENTER },
993 { CH_BACK_PAIR, CH_DIRECT_PAIR },
994 { CH_BACK_PAIR, CH_SIDE_PAIR },
995 { CH_BACK_PAIR, AV_CH_BACK_CENTER },
996 { AV_CH_BACK_CENTER, CH_BACK_PAIR },
997 { AV_CH_BACK_CENTER, CH_DIRECT_PAIR },
998 { AV_CH_BACK_CENTER, CH_SIDE_PAIR },
999 };
1000
1001 44497 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
1002 {
1003 44497 AVFilterLink *link = NULL;
1004 int i, j, k;
1005
1006
2/2
✓ Branch 0 taken 36306 times.
✓ Branch 1 taken 38671 times.
74977 for (i = 0; i < filter->nb_inputs; i++) {
1007 36306 link = filter->inputs[i];
1008
1009
2/2
✓ Branch 0 taken 5832 times.
✓ Branch 1 taken 30474 times.
36306 if (link->type == AVMEDIA_TYPE_AUDIO &&
1010
2/2
✓ Branch 0 taken 5826 times.
✓ Branch 1 taken 6 times.
5832 link->outcfg.channel_layouts->nb_channel_layouts == 1)
1011 5826 break;
1012 }
1013
2/2
✓ Branch 0 taken 38671 times.
✓ Branch 1 taken 5826 times.
44497 if (i == filter->nb_inputs)
1014 38671 return;
1015
1016
2/2
✓ Branch 0 taken 4545 times.
✓ Branch 1 taken 5826 times.
10371 for (i = 0; i < filter->nb_outputs; i++) {
1017 4545 AVFilterLink *outlink = filter->outputs[i];
1018 4545 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
1019
1020
2/2
✓ Branch 0 taken 4543 times.
✓ Branch 1 taken 2 times.
4545 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1021
2/2
✓ Branch 0 taken 4540 times.
✓ Branch 1 taken 3 times.
4543 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
1022 4542 continue;
1023
1024
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++) {
1025 34 AVChannelLayout in_chlayout = { 0 }, out_chlayout = { 0 };
1026 int in_channels;
1027 int out_channels;
1028 int count_diff;
1029 int matched_channels, extra_channels;
1030 34 int score = 100000;
1031
1032 34 av_channel_layout_copy(&in_chlayout, &link->outcfg.channel_layouts->channel_layouts[0]);
1033 34 av_channel_layout_copy(&out_chlayout, &outlink->incfg.channel_layouts->channel_layouts[j]);
1034 34 in_channels = in_chlayout.nb_channels;
1035 34 out_channels = out_chlayout.nb_channels;
1036 34 count_diff = out_channels - in_channels;
1037
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)) {
1038 /* Compute score in case the input or output layout encodes
1039 a channel count; in this case the score is not altered by
1040 the computation afterwards, as in_chlayout and
1041 out_chlayout have both been set to 0 */
1042
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))
1043
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 in_channels = FF_LAYOUT2COUNT(&in_chlayout);
1044
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16 if (!KNOWN(&out_chlayout))
1045 out_channels = FF_LAYOUT2COUNT(&out_chlayout);
1046 32 score -= 10000 + FFABS(out_channels - in_channels) +
1047
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 (in_channels > out_channels ? 10000 : 0);
1048 16 av_channel_layout_uninit(&in_chlayout);
1049 16 av_channel_layout_uninit(&out_chlayout);
1050 /* Let the remaining computation run, even if the score
1051 value is not altered */
1052 }
1053
1054 /* channel substitution */
1055
2/2
✓ Branch 0 taken 714 times.
✓ Branch 1 taken 34 times.
748 for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
1056 714 uint64_t cmp0 = ch_subst[k][0];
1057 714 uint64_t cmp1 = ch_subst[k][1];
1058
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) &&
1059
2/2
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 100 times.
336 !av_channel_layout_subset(&out_chlayout, cmp0) &&
1060
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
140 av_channel_layout_subset(&out_chlayout, cmp1) &&
1061 20 !av_channel_layout_subset(& in_chlayout, cmp1)) {
1062 4 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(& in_chlayout, ~cmp0));
1063 4 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~cmp1));
1064 /* add score for channel match, minus a deduction for
1065 having to do the substitution */
1066 4 score += 10 * av_popcount64(cmp1) - 2;
1067 }
1068 }
1069
1070 /* no penalty for LFE channel mismatch */
1071
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 &&
1072 18 av_channel_layout_index_from_channel(&out_chlayout, AV_CHAN_LOW_FREQUENCY) >= 0)
1073 7 score += 10;
1074 34 av_channel_layout_from_mask(&in_chlayout, av_channel_layout_subset(&in_chlayout, ~AV_CH_LOW_FREQUENCY));
1075 34 av_channel_layout_from_mask(&out_chlayout, av_channel_layout_subset(&out_chlayout, ~AV_CH_LOW_FREQUENCY));
1076
1077 34 matched_channels = av_popcount64(in_chlayout.u.mask & out_chlayout.u.mask);
1078 34 extra_channels = av_popcount64(out_chlayout.u.mask & (~in_chlayout.u.mask));
1079 34 score += 10 * matched_channels - 5 * extra_channels;
1080
1081
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 ||
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 (count_diff < best_count_diff && score == best_score)) {
1083 10 best_score = score;
1084 10 best_idx = j;
1085 10 best_count_diff = count_diff;
1086 }
1087 }
1088
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 av_assert0(best_idx >= 0);
1089 3 FFSWAP(AVChannelLayout, outlink->incfg.channel_layouts->channel_layouts[0],
1090 outlink->incfg.channel_layouts->channel_layouts[best_idx]);
1091 }
1092
1093 }
1094
1095 8071 static void swap_channel_layouts(AVFilterGraph *graph)
1096 {
1097 int i;
1098
1099
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++)
1100 44497 swap_channel_layouts_on_filter(graph->filters[i]);
1101 8071 }
1102
1103 44497 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1104 {
1105 44497 AVFilterLink *link = NULL;
1106 int format, bps;
1107 int i, j;
1108
1109
2/2
✓ Branch 0 taken 36375 times.
✓ Branch 1 taken 39056 times.
75431 for (i = 0; i < filter->nb_inputs; i++) {
1110 36375 link = filter->inputs[i];
1111
1112
2/2
✓ Branch 0 taken 5901 times.
✓ Branch 1 taken 30474 times.
36375 if (link->type == AVMEDIA_TYPE_AUDIO &&
1113
2/2
✓ Branch 0 taken 5441 times.
✓ Branch 1 taken 460 times.
5901 link->outcfg.formats->nb_formats == 1)
1114 5441 break;
1115 }
1116
2/2
✓ Branch 0 taken 39056 times.
✓ Branch 1 taken 5441 times.
44497 if (i == filter->nb_inputs)
1117 39056 return;
1118
1119 5441 format = link->outcfg.formats->formats[0];
1120 5441 bps = av_get_bytes_per_sample(format);
1121
1122
2/2
✓ Branch 0 taken 4167 times.
✓ Branch 1 taken 5441 times.
9608 for (i = 0; i < filter->nb_outputs; i++) {
1123 4167 AVFilterLink *outlink = filter->outputs[i];
1124 4167 int best_idx = -1, best_score = INT_MIN;
1125
1126
2/2
✓ Branch 0 taken 4165 times.
✓ Branch 1 taken 2 times.
4167 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1127
2/2
✓ Branch 0 taken 4007 times.
✓ Branch 1 taken 158 times.
4165 outlink->incfg.formats->nb_formats < 2)
1128 4009 continue;
1129
1130
2/2
✓ Branch 0 taken 293 times.
✓ Branch 1 taken 7 times.
300 for (j = 0; j < outlink->incfg.formats->nb_formats; j++) {
1131 293 int out_format = outlink->incfg.formats->formats[j];
1132 293 int out_bps = av_get_bytes_per_sample(out_format);
1133 int score;
1134
1135
4/4
✓ Branch 1 taken 156 times.
✓ Branch 2 taken 137 times.
✓ Branch 3 taken 14 times.
✓ Branch 4 taken 142 times.
449 if (av_get_packed_sample_fmt(out_format) == format ||
1136 156 av_get_planar_sample_fmt(out_format) == format) {
1137 151 best_idx = j;
1138 151 break;
1139 }
1140
1141 /* for s32 and float prefer double to prevent loss of information */
1142
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 138 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
142 if (bps == 4 && out_bps == 8) {
1143 best_idx = j;
1144 break;
1145 }
1146
1147 /* prefer closest higher or equal bps */
1148 142 score = -abs(out_bps - bps);
1149
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 114 times.
142 if (out_bps >= bps)
1150 28 score += INT_MAX/2;
1151
1152
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 33 times.
142 if (score > best_score) {
1153 109 best_score = score;
1154 109 best_idx = j;
1155 }
1156 }
1157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158 times.
158 av_assert0(best_idx >= 0);
1158 158 FFSWAP(int, outlink->incfg.formats->formats[0],
1159 outlink->incfg.formats->formats[best_idx]);
1160 }
1161 }
1162
1163 8071 static void swap_sample_fmts(AVFilterGraph *graph)
1164 {
1165 int i;
1166
1167
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++)
1168 44497 swap_sample_fmts_on_filter(graph->filters[i]);
1169
1170 8071 }
1171
1172 8071 static int pick_formats(AVFilterGraph *graph)
1173 {
1174 int i, j, ret;
1175 int change;
1176
1177 do{
1178 16185 change = 0;
1179
2/2
✓ Branch 0 taken 89938 times.
✓ Branch 1 taken 16185 times.
106123 for (i = 0; i < graph->nb_filters; i++) {
1180 89938 AVFilterContext *filter = graph->filters[i];
1181
2/2
✓ Branch 0 taken 73293 times.
✓ Branch 1 taken 16645 times.
89938 if (filter->nb_inputs){
1182
2/2
✓ Branch 0 taken 73642 times.
✓ Branch 1 taken 73293 times.
146935 for (j = 0; j < filter->nb_inputs; j++){
1183
4/4
✓ Branch 0 taken 15530 times.
✓ Branch 1 taken 58112 times.
✓ Branch 2 taken 15037 times.
✓ Branch 3 taken 493 times.
73642 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15037 times.
15037 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1185 return ret;
1186 15037 change = 1;
1187 }
1188 }
1189 }
1190
2/2
✓ Branch 0 taken 73378 times.
✓ Branch 1 taken 16560 times.
89938 if (filter->nb_outputs){
1191
2/2
✓ Branch 0 taken 73642 times.
✓ Branch 1 taken 73378 times.
147020 for (j = 0; j < filter->nb_outputs; j++){
1192
4/4
✓ Branch 0 taken 21661 times.
✓ Branch 1 taken 51981 times.
✓ Branch 2 taken 21064 times.
✓ Branch 3 taken 597 times.
73642 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21064 times.
21064 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1194 return ret;
1195 21064 change = 1;
1196 }
1197 }
1198 }
1199
6/6
✓ Branch 0 taken 73293 times.
✓ Branch 1 taken 16645 times.
✓ Branch 2 taken 56733 times.
✓ Branch 3 taken 16560 times.
✓ Branch 4 taken 56463 times.
✓ Branch 5 taken 270 times.
89938 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1200
2/2
✓ Branch 0 taken 56659 times.
✓ Branch 1 taken 56463 times.
113122 for (j = 0; j < filter->nb_outputs; j++) {
1201
2/2
✓ Branch 0 taken 237 times.
✓ Branch 1 taken 56422 times.
56659 if (filter->outputs[j]->format<0) {
1202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 237 times.
237 if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
1203 return ret;
1204 237 change = 1;
1205 }
1206 }
1207 }
1208 }
1209
2/2
✓ Branch 0 taken 8114 times.
✓ Branch 1 taken 8071 times.
16185 }while(change);
1210
1211
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++) {
1212 44497 AVFilterContext *filter = graph->filters[i];
1213
1214
2/2
✓ Branch 0 taken 36389 times.
✓ Branch 1 taken 44497 times.
80886 for (j = 0; j < filter->nb_inputs; j++)
1215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36389 times.
36389 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1216 return ret;
1217
2/2
✓ Branch 0 taken 36389 times.
✓ Branch 1 taken 44497 times.
80886 for (j = 0; j < filter->nb_outputs; j++)
1218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36389 times.
36389 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1219 return ret;
1220 }
1221 8071 return 0;
1222 }
1223
1224 /**
1225 * Configure the formats of all the links in the graph.
1226 */
1227 8071 static int graph_config_formats(AVFilterGraph *graph, void *log_ctx)
1228 {
1229 int ret;
1230
1231 /* find supported formats from sub-filters, and merge along links */
1232
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 8071 times.
8085 while ((ret = query_formats(graph, log_ctx)) == AVERROR(EAGAIN))
1233 14 av_log(graph, AV_LOG_DEBUG, "query_formats not finished\n");
1234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8071 times.
8071 if (ret < 0)
1235 return ret;
1236
1237 /* Once everything is merged, it's possible that we'll still have
1238 * multiple valid media format choices. We try to minimize the amount
1239 * of format conversion inside filters */
1240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = reduce_formats(graph)) < 0)
1241 return ret;
1242
1243 /* for audio filters, ensure the best format, sample rate and channel layout
1244 * is selected */
1245 8071 swap_sample_fmts(graph);
1246 8071 swap_samplerates(graph);
1247 8071 swap_channel_layouts(graph);
1248
1249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = pick_formats(graph)) < 0)
1250 return ret;
1251
1252 8071 return 0;
1253 }
1254
1255 8071 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1256 {
1257 unsigned i, j;
1258 8071 int sink_links_count = 0, n = 0;
1259 AVFilterContext *f;
1260 FilterLinkInternal **sinks;
1261
1262
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++) {
1263 44497 f = graph->filters[i];
1264
2/2
✓ Branch 0 taken 36389 times.
✓ Branch 1 taken 44497 times.
80886 for (j = 0; j < f->nb_inputs; j++) {
1265 36389 ff_link_internal(f->inputs[j])->age_index = -1;
1266 }
1267
2/2
✓ Branch 0 taken 36389 times.
✓ Branch 1 taken 44497 times.
80886 for (j = 0; j < f->nb_outputs; j++) {
1268 36389 ff_link_internal(f->outputs[j])->age_index = -1;
1269 }
1270
2/2
✓ Branch 0 taken 8205 times.
✓ Branch 1 taken 36292 times.
44497 if (!f->nb_outputs) {
1271
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8205 times.
8205 if (f->nb_inputs > INT_MAX - sink_links_count)
1272 return AVERROR(EINVAL);
1273 8205 sink_links_count += f->nb_inputs;
1274 }
1275 }
1276 8071 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8071 times.
8071 if (!sinks)
1278 return AVERROR(ENOMEM);
1279
2/2
✓ Branch 0 taken 44497 times.
✓ Branch 1 taken 8071 times.
52568 for (i = 0; i < graph->nb_filters; i++) {
1280 44497 f = graph->filters[i];
1281
2/2
✓ Branch 0 taken 8205 times.
✓ Branch 1 taken 36292 times.
44497 if (!f->nb_outputs) {
1282
2/2
✓ Branch 0 taken 8205 times.
✓ Branch 1 taken 8205 times.
16410 for (j = 0; j < f->nb_inputs; j++) {
1283 8205 sinks[n] = ff_link_internal(f->inputs[j]);
1284 8205 sinks[n]->age_index = n;
1285 8205 n++;
1286 }
1287 }
1288 }
1289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8071 times.
8071 av_assert0(n == sink_links_count);
1290 8071 fffiltergraph(graph)->sink_links = sinks;
1291 8071 fffiltergraph(graph)->sink_links_count = sink_links_count;
1292 8071 return 0;
1293 }
1294
1295 8071 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1296 {
1297 int ret;
1298
1299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = graph_check_validity(graphctx, log_ctx)))
1300 return ret;
1301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = graph_config_formats(graphctx, log_ctx)))
1302 return ret;
1303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = graph_config_links(graphctx, log_ctx)))
1304 return ret;
1305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = graph_check_links(graphctx, log_ctx)))
1306 return ret;
1307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8071 times.
8071 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1308 return ret;
1309
1310 8071 return 0;
1311 }
1312
1313 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
1314 {
1315 int i, r = AVERROR(ENOSYS);
1316
1317 if (!graph)
1318 return r;
1319
1320 if ((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
1321 r = avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
1322 if (r != AVERROR(ENOSYS))
1323 return r;
1324 }
1325
1326 if (res_len && res)
1327 res[0] = 0;
1328
1329 for (i = 0; i < graph->nb_filters; i++) {
1330 AVFilterContext *filter = graph->filters[i];
1331 if (!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)) {
1332 r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
1333 if (r != AVERROR(ENOSYS)) {
1334 if ((flags & AVFILTER_CMD_FLAG_ONE) || r < 0)
1335 return r;
1336 }
1337 }
1338 }
1339
1340 return r;
1341 }
1342
1343 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
1344 {
1345 int i;
1346
1347 if(!graph)
1348 return 0;
1349
1350 for (i = 0; i < graph->nb_filters; i++) {
1351 AVFilterContext *filter = graph->filters[i];
1352 FFFilterContext *ctxi = fffilterctx(filter);
1353 if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
1354 AVFilterCommand **queue = &ctxi->command_queue, *next;
1355 while (*queue && (*queue)->time <= ts)
1356 queue = &(*queue)->next;
1357 next = *queue;
1358 *queue = av_mallocz(sizeof(AVFilterCommand));
1359 if (!*queue)
1360 return AVERROR(ENOMEM);
1361
1362 (*queue)->command = av_strdup(command);
1363 (*queue)->arg = av_strdup(arg);
1364 (*queue)->time = ts;
1365 (*queue)->flags = flags;
1366 (*queue)->next = next;
1367 if(flags & AVFILTER_CMD_FLAG_ONE)
1368 return 0;
1369 }
1370 }
1371
1372 return 0;
1373 }
1374
1375 408207 static void heap_bubble_up(FFFilterGraph *graph,
1376 FilterLinkInternal *li, int index)
1377 {
1378 408207 FilterLinkInternal **links = graph->sink_links;
1379
1380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 408207 times.
408207 av_assert0(index >= 0);
1381
1382
2/2
✓ Branch 0 taken 1306 times.
✓ Branch 1 taken 408207 times.
409513 while (index) {
1383 1306 int parent = (index - 1) >> 1;
1384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1306 times.
1306 if (links[parent]->l.current_pts_us >= li->l.current_pts_us)
1385 break;
1386 1306 links[index] = links[parent];
1387 1306 links[index]->age_index = index;
1388 1306 index = parent;
1389 }
1390 408207 links[index] = li;
1391 408207 li->age_index = index;
1392 408207 }
1393
1394 408329 static void heap_bubble_down(FFFilterGraph *graph,
1395 FilterLinkInternal *li, int index)
1396 {
1397 408329 FilterLinkInternal **links = graph->sink_links;
1398
1399
1/2
✓ Branch 0 taken 408329 times.
✗ Branch 1 not taken.
408329 av_assert0(index >= 0);
1400
1401 4615 while (1) {
1402 412944 int child = 2 * index + 1;
1403
2/2
✓ Branch 0 taken 407994 times.
✓ Branch 1 taken 4950 times.
412944 if (child >= graph->sink_links_count)
1404 407994 break;
1405
2/2
✓ Branch 0 taken 3554 times.
✓ Branch 1 taken 1396 times.
4950 if (child + 1 < graph->sink_links_count &&
1406
2/2
✓ Branch 0 taken 1010 times.
✓ Branch 1 taken 2544 times.
3554 links[child + 1]->l.current_pts_us < links[child]->l.current_pts_us)
1407 1010 child++;
1408
2/2
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 4615 times.
4950 if (li->l.current_pts_us < links[child]->l.current_pts_us)
1409 335 break;
1410 4615 links[index] = links[child];
1411 4615 links[index]->age_index = index;
1412 4615 index = child;
1413 }
1414 408329 links[index] = li;
1415 408329 li->age_index = index;
1416 408329 }
1417
1418 408207 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1419 {
1420 408207 FFFilterGraph *graphi = fffiltergraph(graph);
1421
1422 408207 heap_bubble_up (graphi, li, li->age_index);
1423 408207 heap_bubble_down(graphi, li, li->age_index);
1424 408207 }
1425
1426 399563 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1427 {
1428 399563 FFFilterGraph *graphi = fffiltergraph(graph);
1429 399563 FilterLinkInternal *oldesti = graphi->sink_links[0];
1430 399563 AVFilterLink *oldest = &oldesti->l.pub;
1431 int64_t frame_count;
1432 int r;
1433
1434
2/2
✓ Branch 0 taken 399691 times.
✓ Branch 1 taken 4914 times.
404605 while (graphi->sink_links_count) {
1435 399691 oldesti = graphi->sink_links[0];
1436 399691 oldest = &oldesti->l.pub;
1437
2/2
✓ Branch 1 taken 399665 times.
✓ Branch 2 taken 26 times.
399691 if (fffilter(oldest->dst->filter)->activate) {
1438 399665 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1439 AV_BUFFERSINK_FLAG_PEEK);
1440
2/2
✓ Branch 0 taken 394624 times.
✓ Branch 1 taken 5041 times.
399665 if (r != AVERROR_EOF)
1441 394624 return r;
1442 } else {
1443 26 r = ff_request_frame(oldest);
1444 }
1445
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5042 times.
5067 if (r != AVERROR_EOF)
1446 25 break;
1447 5042 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1448 5042 oldest->dst->name,
1449 5042 oldest->dstpad->name);
1450 /* EOF: remove the link from the heap */
1451
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 4920 times.
5042 if (oldesti->age_index < --graphi->sink_links_count)
1452 122 heap_bubble_down(graphi, graphi->sink_links[graphi->sink_links_count],
1453 oldesti->age_index);
1454 5042 oldesti->age_index = -1;
1455 }
1456
2/2
✓ Branch 0 taken 4914 times.
✓ Branch 1 taken 25 times.
4939 if (!graphi->sink_links_count)
1457 4914 return AVERROR_EOF;
1458 av_assert1(!fffilter(oldest->dst->filter)->activate);
1459 av_assert1(oldesti->age_index >= 0);
1460 25 frame_count = oldesti->l.frame_count_out;
1461
1/2
✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
100 while (frame_count == oldesti->l.frame_count_out) {
1462 100 r = ff_filter_graph_run_once(graph);
1463
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 75 times.
100 if (r == FFERROR_BUFFERSRC_EMPTY)
1464 25 r = 0;
1465
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 75 times.
100 if (r == AVERROR(EAGAIN) &&
1466
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
25 !oldesti->frame_wanted_out && !oldesti->frame_blocked_in &&
1467 !oldesti->status_in)
1468 (void)ff_request_frame(oldest);
1469
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 75 times.
100 else if (r < 0)
1470 25 return r;
1471 }
1472 return 0;
1473 }
1474
1475 4452028 int ff_filter_graph_run_once(AVFilterGraph *graph)
1476 {
1477 FFFilterContext *ctxi;
1478 unsigned i;
1479
1480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4452028 times.
4452028 av_assert0(graph->nb_filters);
1481 4452028 ctxi = fffilterctx(graph->filters[0]);
1482
2/2
✓ Branch 0 taken 18736670 times.
✓ Branch 1 taken 4452028 times.
23188698 for (i = 1; i < graph->nb_filters; i++) {
1483 18736670 FFFilterContext *ctxi_other = fffilterctx(graph->filters[i]);
1484
1485
2/2
✓ Branch 0 taken 2665044 times.
✓ Branch 1 taken 16071626 times.
18736670 if (ctxi_other->ready > ctxi->ready)
1486 2665044 ctxi = ctxi_other;
1487 }
1488
1489
2/2
✓ Branch 0 taken 738378 times.
✓ Branch 1 taken 3713650 times.
4452028 if (!ctxi->ready)
1490 738378 return AVERROR(EAGAIN);
1491 3713650 return ff_filter_activate(&ctxi->p);
1492 }
1493