FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2024-03-28 04:31:58
Exec Total Coverage
Lines: 593 757 78.3%
Functions: 35 38 92.1%
Branches: 494 719 68.7%

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