FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/avfiltergraph.c
Date: 2024-04-19 17:50:32
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/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 "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 12918 AVFilterGraph *avfilter_graph_alloc(void)
83 {
84 12918 FFFilterGraph *graph = av_mallocz(sizeof(*graph));
85 AVFilterGraph *ret;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12918 times.
12918 if (!graph)
88 return NULL;
89
90 12918 ret = &graph->p;
91 12918 ret->av_class = &filtergraph_class;
92 12918 av_opt_set_defaults(ret);
93 12918 ff_framequeue_global_init(&graph->frame_queues);
94
95 12918 return ret;
96 }
97
98 45431 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
99 {
100 int i, j;
101
1/2
✓ Branch 0 taken 45431 times.
✗ Branch 1 not taken.
45431 for (i = 0; i < graph->nb_filters; i++) {
102
1/2
✓ Branch 0 taken 45431 times.
✗ Branch 1 not taken.
45431 if (graph->filters[i] == filter) {
103 45431 FFSWAP(AVFilterContext*, graph->filters[i],
104 graph->filters[graph->nb_filters - 1]);
105 45431 graph->nb_filters--;
106 45431 filter->graph = NULL;
107
2/2
✓ Branch 0 taken 38967 times.
✓ Branch 1 taken 45431 times.
84398 for (j = 0; j<filter->nb_outputs; j++)
108
2/2
✓ Branch 0 taken 15772 times.
✓ Branch 1 taken 23195 times.
38967 if (filter->outputs[j])
109 15772 filter->outputs[j]->graph = NULL;
110
111 45431 return;
112 }
113 }
114 }
115
116 19302 void avfilter_graph_free(AVFilterGraph **graphp)
117 {
118 19302 AVFilterGraph *graph = *graphp;
119 19302 FFFilterGraph *graphi = fffiltergraph(graph);
120
121
2/2
✓ Branch 0 taken 6384 times.
✓ Branch 1 taken 12918 times.
19302 if (!graph)
122 6384 return;
123
124
2/2
✓ Branch 0 taken 45365 times.
✓ Branch 1 taken 12918 times.
58283 while (graph->nb_filters)
125 45365 avfilter_free(graph->filters[0]);
126
127 12918 ff_graph_thread_free(graphi);
128
129 12918 av_freep(&graphi->sink_links);
130
131 12918 av_opt_free(graph);
132
133 12918 av_freep(&graph->filters);
134 12918 av_freep(graphp);
135 }
136
137 19651 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 19651 *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19651 times.
19651 if (!*filt_ctx)
145 return AVERROR(ENOMEM);
146
147 19651 ret = avfilter_init_str(*filt_ctx, args);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19651 times.
19651 if (ret < 0)
149 goto fail;
150
151 19651 return 0;
152
153 fail:
154 avfilter_free(*filt_ctx);
155 *filt_ctx = NULL;
156 return ret;
157 }
158
159 4693 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
160 {
161 4693 fffiltergraph(graph)->disable_auto_convert = flags;
162 4693 }
163
164 45431 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
165 const AVFilter *filter,
166 const char *name)
167 {
168 AVFilterContext **filters, *s;
169 45431 FFFilterGraph *graphi = fffiltergraph(graph);
170
171
4/4
✓ Branch 0 taken 21699 times.
✓ Branch 1 taken 23732 times.
✓ Branch 2 taken 12918 times.
✓ Branch 3 taken 8781 times.
45431 if (graph->thread_type && !graphi->thread_execute) {
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12918 times.
12918 if (graph->execute) {
173 graphi->thread_execute = graph->execute;
174 } else {
175 12918 int ret = ff_graph_thread_init(graphi);
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12918 times.
12918 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 45431 filters = av_realloc_array(graph->filters, graph->nb_filters + 1, sizeof(*filters));
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45431 times.
45431 if (!filters)
185 return NULL;
186 45431 graph->filters = filters;
187
188 45431 s = ff_filter_alloc(filter, name);
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45431 times.
45431 if (!s)
190 return NULL;
191
192 45431 graph->filters[graph->nb_filters++] = s;
193
194 45431 s->graph = graph;
195
196 45431 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 6468 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 31639 times.
✓ Branch 1 taken 6468 times.
38107 for (i = 0; i < graph->nb_filters; i++) {
213 const AVFilterPad *pad;
214 31639 filt = graph->filters[i];
215
216
2/2
✓ Branch 0 taken 25147 times.
✓ Branch 1 taken 31639 times.
56786 for (j = 0; j < filt->nb_inputs; j++) {
217
2/4
✓ Branch 0 taken 25147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25147 times.
25147 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 25147 times.
✓ Branch 1 taken 31639 times.
56786 for (j = 0; j < filt->nb_outputs; j++) {
227
2/4
✓ Branch 0 taken 25147 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 25147 times.
25147 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 6468 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 6468 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 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++) {
251 32697 filt = graph->filters[i];
252
253
2/2
✓ Branch 0 taken 6521 times.
✓ Branch 1 taken 26176 times.
32697 if (!filt->nb_outputs) {
254
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6521 times.
6521 if ((ret = ff_filter_config_links(filt)))
255 return ret;
256 }
257 }
258
259 6468 return 0;
260 }
261
262 6468 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 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++) {
270 32697 f = graph->filters[i];
271
2/2
✓ Branch 0 taken 26205 times.
✓ Branch 1 taken 32697 times.
58902 for (j = 0; j < f->nb_outputs; j++) {
272 26205 l = f->outputs[j];
273
2/2
✓ Branch 0 taken 20765 times.
✓ Branch 1 taken 5440 times.
26205 if (l->type == AVMEDIA_TYPE_VIDEO) {
274 20765 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 20765 times.
20765 if (ret < 0)
276 return ret;
277 }
278 }
279 }
280 6468 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 42647 static int filter_link_check_formats(void *log, AVFilterLink *link, AVFilterFormatsConfig *cfg)
295 {
296 int ret;
297
298
2/3
✓ Branch 0 taken 34159 times.
✓ Branch 1 taken 8488 times.
✗ Branch 2 not taken.
42647 switch (link->type) {
299
300 34159 case AVMEDIA_TYPE_VIDEO:
301
2/4
✓ Branch 1 taken 34159 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34159 times.
✗ Branch 4 not taken.
68318 if ((ret = ff_formats_check_pixel_formats(log, cfg->formats)) < 0 ||
302
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 34159 times.
68318 (ret = ff_formats_check_color_spaces(log, cfg->color_spaces)) < 0 ||
303 34159 (ret = ff_formats_check_color_ranges(log, cfg->color_ranges)) < 0)
304 return ret;
305 34159 break;
306
307 8488 case AVMEDIA_TYPE_AUDIO:
308
2/4
✓ Branch 1 taken 8488 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8488 times.
✗ Branch 4 not taken.
16976 if ((ret = ff_formats_check_sample_formats(log, cfg->formats)) < 0 ||
309
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8488 times.
16976 (ret = ff_formats_check_sample_rates(log, cfg->samplerates)) < 0 ||
310 8488 (ret = ff_formats_check_channel_layouts(log, cfg->channel_layouts)) < 0)
311 return ret;
312 8488 break;
313
314 default:
315 av_assert0(!"reached");
316 }
317 42647 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 27828 static int filter_check_formats(AVFilterContext *ctx)
326 {
327 unsigned i;
328 int ret;
329
330
2/2
✓ Branch 0 taken 21335 times.
✓ Branch 1 taken 27828 times.
49163 for (i = 0; i < ctx->nb_inputs; i++) {
331 21335 ret = filter_link_check_formats(ctx, ctx->inputs[i], &ctx->inputs[i]->outcfg);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21335 times.
21335 if (ret < 0)
333 return ret;
334 }
335
2/2
✓ Branch 0 taken 21312 times.
✓ Branch 1 taken 27828 times.
49140 for (i = 0; i < ctx->nb_outputs; i++) {
336 21312 ret = filter_link_check_formats(ctx, ctx->outputs[i], &ctx->outputs[i]->incfg);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21312 times.
21312 if (ret < 0)
338 return ret;
339 }
340 27828 return 0;
341 }
342
343 32711 static int filter_query_formats(AVFilterContext *ctx)
344 {
345 int ret;
346
347
2/2
✓ Branch 0 taken 27842 times.
✓ Branch 1 taken 4869 times.
32711 if (ctx->filter->formats_state == FF_FILTER_FORMATS_QUERY_FUNC) {
348
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 27828 times.
27842 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 27828 ret = filter_check_formats(ctx);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27828 times.
27828 if (ret < 0)
357 return ret;
358 }
359
360 32697 return ff_default_query_formats(ctx);
361 }
362
363 31697 static int formats_declared(AVFilterContext *f)
364 {
365 int i;
366
367
2/2
✓ Branch 0 taken 25124 times.
✓ Branch 1 taken 6606 times.
31730 for (i = 0; i < f->nb_inputs; i++) {
368
2/2
✓ Branch 0 taken 25089 times.
✓ Branch 1 taken 35 times.
25124 if (!f->inputs[i]->outcfg.formats)
369 25089 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 6596 times.
✓ Branch 1 taken 44 times.
6640 for (i = 0; i < f->nb_outputs; i++) {
380
2/2
✓ Branch 0 taken 6562 times.
✓ Branch 1 taken 34 times.
6596 if (!f->outputs[i]->incfg.formats)
381 6562 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 6482 static int query_formats(AVFilterGraph *graph, void *log_ctx)
405 {
406 int i, j, ret;
407 6482 int converter_count = 0;
408 6482 int count_queried = 0; /* successful calls to query_formats() */
409 6482 int count_merged = 0; /* successful merge of formats lists */
410 6482 int count_already_merged = 0; /* lists already merged */
411 6482 int count_delayed = 0; /* lists that need to be merged later */
412
413
2/2
✓ Branch 0 taken 31697 times.
✓ Branch 1 taken 6482 times.
38179 for (i = 0; i < graph->nb_filters; i++) {
414 31697 AVFilterContext *f = graph->filters[i];
415
2/2
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 31653 times.
31697 if (formats_declared(f))
416 44 continue;
417 31653 ret = filter_query_formats(f);
418
3/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 31639 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
31653 if (ret < 0 && ret != AVERROR(EAGAIN))
419 return ret;
420 /* note: EAGAIN could indicate a partial success, not counted yet */
421 31653 count_queried += ret >= 0;
422 }
423
424 /* go through and merge as many format lists as possible */
425
2/2
✓ Branch 0 taken 32755 times.
✓ Branch 1 taken 6482 times.
39237 for (i = 0; i < graph->nb_filters; i++) {
426 32755 AVFilterContext *filter = graph->filters[i];
427
428
2/2
✓ Branch 0 taken 26253 times.
✓ Branch 1 taken 32755 times.
59008 for (j = 0; j < filter->nb_inputs; j++) {
429 26253 AVFilterLink *link = filter->inputs[j];
430 const AVFilterNegotiation *neg;
431 unsigned neg_step;
432 26253 int convert_needed = 0;
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26253 times.
26253 if (!link)
435 continue;
436
437 26253 neg = ff_filter_get_negotiation(link);
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26253 times.
26253 av_assert0(neg);
439
2/2
✓ Branch 0 taken 77553 times.
✓ Branch 1 taken 25195 times.
102748 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
440 77553 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
441 77553 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
442 77553 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
443
8/8
✓ Branch 0 taken 77511 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 77468 times.
✓ Branch 3 taken 43 times.
✓ Branch 4 taken 74225 times.
✓ Branch 5 taken 3243 times.
✓ Branch 7 taken 1058 times.
✓ Branch 8 taken 73167 times.
77553 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 78759 times.
✓ Branch 1 taken 26253 times.
105012 for (neg_step = 0; neg_step < neg->nb_mergers; neg_step++) {
449 78759 const AVFilterFormatsMerger *m = &neg->mergers[neg_step];
450 78759 void *a = FF_FIELD_AT(void *, m->offset, link->incfg);
451 78759 void *b = FF_FIELD_AT(void *, m->offset, link->outcfg);
452
4/4
✓ Branch 0 taken 78717 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 43 times.
✓ Branch 3 taken 78674 times.
78759 if (!(a && b)) {
453 85 count_delayed++;
454
2/2
✓ Branch 0 taken 8107 times.
✓ Branch 1 taken 70567 times.
78674 } else if (a == b) {
455 8107 count_already_merged++;
456
2/2
✓ Branch 0 taken 67808 times.
✓ Branch 1 taken 2759 times.
70567 } else if (!convert_needed) {
457 67808 count_merged++;
458 67808 ret = m->merge(a, b);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67808 times.
67808 if (ret < 0)
460 return ret;
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 67808 times.
67808 if (!ret)
462 convert_needed = 1;
463 }
464 }
465
466
2/2
✓ Branch 0 taken 1058 times.
✓ Branch 1 taken 25195 times.
26253 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 6482 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 6468 times.
6482 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 6468 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 25964 int ff_fmt_is_regular_yuv(enum AVPixelFormat fmt)
606 {
607 25964 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25964 times.
25964 if (!desc)
609 return 0;
610
2/2
✓ Branch 0 taken 1495 times.
✓ Branch 1 taken 24469 times.
25964 if (desc->nb_components < 3)
611 1495 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 5478 times.
✓ Branch 1 taken 18991 times.
24469 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 5478 return 0;
616
617
2/2
✓ Branch 0 taken 641 times.
✓ Branch 1 taken 18350 times.
18991 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 18350 default:
625 18350 return 1;
626 }
627 }
628
629 78567 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
630 {
631
3/4
✓ Branch 0 taken 78567 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52362 times.
✓ Branch 3 taken 26205 times.
78567 if (!link || !link->incfg.formats)
632 52362 return 0;
633
634
2/2
✓ Branch 0 taken 20765 times.
✓ Branch 1 taken 5440 times.
26205 if (link->type == AVMEDIA_TYPE_VIDEO) {
635
3/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 20668 times.
✓ Branch 2 taken 97 times.
✗ Branch 3 not taken.
20765 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 5440 times.
✗ Branch 1 not taken.
5440 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
650
3/4
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 5336 times.
✓ Branch 2 taken 104 times.
✗ Branch 3 not taken.
5440 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 26205 link->incfg.formats->nb_formats = 1;
665 26205 link->format = link->incfg.formats->formats[0];
666
667
2/2
✓ Branch 0 taken 20765 times.
✓ Branch 1 taken 5440 times.
26205 if (link->type == AVMEDIA_TYPE_VIDEO) {
668 20765 enum AVPixelFormat swfmt = link->format;
669
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20765 times.
20765 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 6847 times.
✓ Branch 2 taken 13918 times.
20765 if (!ff_fmt_is_regular_yuv(swfmt)) {
679 6847 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 6294 times.
6847 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT)
683 553 link->color_range = AVCOL_RANGE_UNSPECIFIED;
684 else
685 6294 link->color_range = AVCOL_RANGE_JPEG;
686
2/2
✓ Branch 0 taken 4971 times.
✓ Branch 1 taken 1876 times.
6847 if (desc->flags & (AV_PIX_FMT_FLAG_RGB | AV_PIX_FMT_FLAG_XYZ)) {
687 4971 link->colorspace = AVCOL_SPC_RGB;
688 } else {
689 1876 link->colorspace = AVCOL_SPC_UNSPECIFIED;
690 }
691 } else {
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13918 times.
13918 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 13918 link->incfg.color_spaces->nb_formats = 1;
699 13918 link->colorspace = link->incfg.color_spaces->formats[0];
700
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13918 times.
13918 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 13918 link->incfg.color_ranges->nb_formats = 1;
708 13918 link->color_range = link->incfg.color_ranges->formats[0];
709 }
710
1/2
✓ Branch 0 taken 5440 times.
✗ Branch 1 not taken.
5440 } else if (link->type == AVMEDIA_TYPE_AUDIO) {
711 int ret;
712
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5440 times.
5440 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 5440 link->incfg.samplerates->nb_formats = 1;
720 5440 link->sample_rate = link->incfg.samplerates->formats[0];
721
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5440 times.
5440 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 5440 link->incfg.channel_layouts->nb_channel_layouts = 1;
733 5440 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 5440 times.
5440 if (ret < 0)
735 return ret;
736 }
737
738 26205 ff_formats_unref(&link->incfg.formats);
739 26205 ff_formats_unref(&link->outcfg.formats);
740 26205 ff_formats_unref(&link->incfg.samplerates);
741 26205 ff_formats_unref(&link->outcfg.samplerates);
742 26205 ff_channel_layouts_unref(&link->incfg.channel_layouts);
743 26205 ff_channel_layouts_unref(&link->outcfg.channel_layouts);
744 26205 ff_formats_unref(&link->incfg.color_spaces);
745 26205 ff_formats_unref(&link->outcfg.color_spaces);
746 26205 ff_formats_unref(&link->incfg.color_ranges);
747 26205 ff_formats_unref(&link->outcfg.color_ranges);
748
749 26205 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 56327 static int reduce_formats_on_filter(AVFilterContext *filter)
790 {
791 56327 int i, j, k, ret = 0;
792
793
16/20
✓ Branch 0 taken 45747 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 890 times.
✓ Branch 3 taken 44857 times.
✓ Branch 4 taken 34587 times.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 34218 times.
✓ Branch 7 taken 369 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 369 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 49 times.
✓ Branch 14 taken 3335 times.
✓ Branch 15 taken 3384 times.
✓ Branch 16 taken 320 times.
✓ Branch 17 taken 34599 times.
✓ Branch 18 taken 44857 times.
✓ Branch 19 taken 45747 times.
✓ Branch 20 taken 56327 times.
140008 REDUCE_FORMATS(int, AVFilterFormats, formats, formats,
794 nb_formats, ff_add_format);
795
18/20
✓ Branch 0 taken 9787 times.
✓ Branch 1 taken 35960 times.
✓ Branch 2 taken 904 times.
✓ Branch 3 taken 8883 times.
✓ Branch 4 taken 7107 times.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6446 times.
✓ Branch 7 taken 661 times.
✓ Branch 8 taken 625 times.
✓ Branch 9 taken 36 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 625 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 71 times.
✓ Branch 15 taken 107 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 7113 times.
✓ Branch 18 taken 8258 times.
✓ Branch 19 taken 45747 times.
✓ Branch 20 taken 56327 times.
108633 REDUCE_FORMATS(int, AVFilterFormats, samplerates, formats,
796 nb_formats, ff_add_format);
797
15/20
✓ Branch 0 taken 35960 times.
✓ Branch 1 taken 9787 times.
✓ Branch 2 taken 3644 times.
✓ Branch 3 taken 32316 times.
✓ Branch 4 taken 25382 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 21996 times.
✓ Branch 7 taken 3386 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 3386 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 3386 times.
✓ Branch 14 taken 259 times.
✓ Branch 15 taken 3645 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 25382 times.
✓ Branch 18 taken 32316 times.
✓ Branch 19 taken 45747 times.
✓ Branch 20 taken 56327 times.
127715 REDUCE_FORMATS(int, AVFilterFormats, color_spaces, formats,
798 nb_formats, ff_add_format);
799
15/20
✓ Branch 0 taken 35960 times.
✓ Branch 1 taken 9787 times.
✓ Branch 2 taken 8619 times.
✓ Branch 3 taken 27341 times.
✓ Branch 4 taken 21602 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 18762 times.
✓ Branch 7 taken 2840 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 2840 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 2840 times.
✓ Branch 14 taken 2842 times.
✓ Branch 15 taken 5682 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 21602 times.
✓ Branch 18 taken 27341 times.
✓ Branch 19 taken 45747 times.
✓ Branch 20 taken 56327 times.
126518 REDUCE_FORMATS(int, AVFilterFormats, color_ranges, formats,
800 nb_formats, ff_add_format);
801
802 /* reduce channel layouts */
803
2/2
✓ Branch 0 taken 45747 times.
✓ Branch 1 taken 56327 times.
102074 for (i = 0; i < filter->nb_inputs; i++) {
804 45747 AVFilterLink *inlink = filter->inputs[i];
805 const AVChannelLayout *fmt;
806
807
2/2
✓ Branch 0 taken 9787 times.
✓ Branch 1 taken 35960 times.
45747 if (!inlink->outcfg.channel_layouts ||
808
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 9714 times.
9787 inlink->outcfg.channel_layouts->nb_channel_layouts != 1)
809 36033 continue;
810 9714 fmt = &inlink->outcfg.channel_layouts->channel_layouts[0];
811
812
2/2
✓ Branch 0 taken 7577 times.
✓ Branch 1 taken 8875 times.
16452 for (j = 0; j < filter->nb_outputs; j++) {
813 7577 AVFilterLink *outlink = filter->outputs[j];
814 AVFilterChannelLayouts *fmts;
815
816 7577 fmts = outlink->incfg.channel_layouts;
817
4/4
✓ Branch 0 taken 7571 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6693 times.
✓ Branch 3 taken 878 times.
7577 if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
818 6699 continue;
819
820
2/2
✓ Branch 0 taken 839 times.
✓ Branch 1 taken 39 times.
878 if (fmts->all_layouts &&
821
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 832 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
839 (KNOWN(fmt) || fmts->all_counts)) {
822 /* Turn the infinite list into a singleton */
823 839 fmts->all_layouts = fmts->all_counts = 0;
824 839 ret = ff_add_channel_layout(&outlink->incfg.channel_layouts, fmt);
825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 839 times.
839 if (ret < 0)
826 return ret;
827 839 ret = 1;
828 839 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 56327 return ret;
845 }
846
847 6468 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 56327 times.
✓ Branch 1 taken 10538 times.
66865 for (i = 0; i < graph->nb_filters; i++) {
855
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 56327 times.
56327 if ((ret = reduce_formats_on_filter(graph->filters[i])) < 0)
856 return ret;
857 56327 reduced |= ret;
858 }
859
2/2
✓ Branch 0 taken 4070 times.
✓ Branch 1 taken 6468 times.
10538 } while (reduced);
860
861 6468 return 0;
862 }
863
864 32697 static void swap_samplerates_on_filter(AVFilterContext *filter)
865 {
866 32697 AVFilterLink *link = NULL;
867 int sample_rate;
868 int i, j;
869
870
2/2
✓ Branch 0 taken 26187 times.
✓ Branch 1 taken 27271 times.
53458 for (i = 0; i < filter->nb_inputs; i++) {
871 26187 link = filter->inputs[i];
872
873
2/2
✓ Branch 0 taken 5426 times.
✓ Branch 1 taken 20761 times.
26187 if (link->type == AVMEDIA_TYPE_AUDIO &&
874
1/2
✓ Branch 0 taken 5426 times.
✗ Branch 1 not taken.
5426 link->outcfg.samplerates->nb_formats== 1)
875 5426 break;
876 }
877
2/2
✓ Branch 0 taken 27271 times.
✓ Branch 1 taken 5426 times.
32697 if (i == filter->nb_inputs)
878 27271 return;
879
880 5426 sample_rate = link->outcfg.samplerates->formats[0];
881
882
2/2
✓ Branch 0 taken 4170 times.
✓ Branch 1 taken 5426 times.
9596 for (i = 0; i < filter->nb_outputs; i++) {
883 4170 AVFilterLink *outlink = filter->outputs[i];
884 4170 int best_idx, best_diff = INT_MAX;
885
886
2/2
✓ Branch 0 taken 4168 times.
✓ Branch 1 taken 2 times.
4170 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
887
1/2
✓ Branch 0 taken 4168 times.
✗ Branch 1 not taken.
4168 outlink->incfg.samplerates->nb_formats < 2)
888 4170 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 6468 static void swap_samplerates(AVFilterGraph *graph)
906 {
907 int i;
908
909
2/2
✓ Branch 0 taken 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++)
910 32697 swap_samplerates_on_filter(graph->filters[i]);
911 6468 }
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 32697 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
948 {
949 32697 AVFilterLink *link = NULL;
950 int i, j, k;
951
952
2/2
✓ Branch 0 taken 26187 times.
✓ Branch 1 taken 27275 times.
53462 for (i = 0; i < filter->nb_inputs; i++) {
953 26187 link = filter->inputs[i];
954
955
2/2
✓ Branch 0 taken 5426 times.
✓ Branch 1 taken 20761 times.
26187 if (link->type == AVMEDIA_TYPE_AUDIO &&
956
2/2
✓ Branch 0 taken 5422 times.
✓ Branch 1 taken 4 times.
5426 link->outcfg.channel_layouts->nb_channel_layouts == 1)
957 5422 break;
958 }
959
2/2
✓ Branch 0 taken 27275 times.
✓ Branch 1 taken 5422 times.
32697 if (i == filter->nb_inputs)
960 27275 return;
961
962
2/2
✓ Branch 0 taken 4168 times.
✓ Branch 1 taken 5422 times.
9590 for (i = 0; i < filter->nb_outputs; i++) {
963 4168 AVFilterLink *outlink = filter->outputs[i];
964 4168 int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
965
966
2/2
✓ Branch 0 taken 4166 times.
✓ Branch 1 taken 2 times.
4168 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
967
2/2
✓ Branch 0 taken 4164 times.
✓ Branch 1 taken 2 times.
4166 outlink->incfg.channel_layouts->nb_channel_layouts < 2)
968 4166 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 6468 static void swap_channel_layouts(AVFilterGraph *graph)
1042 {
1043 int i;
1044
1045
2/2
✓ Branch 0 taken 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++)
1046 32697 swap_channel_layouts_on_filter(graph->filters[i]);
1047 6468 }
1048
1049 32697 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
1050 {
1051 32697 AVFilterLink *link = NULL;
1052 int format, bps;
1053 int i, j;
1054
1055
2/2
✓ Branch 0 taken 26191 times.
✓ Branch 1 taken 27505 times.
53696 for (i = 0; i < filter->nb_inputs; i++) {
1056 26191 link = filter->inputs[i];
1057
1058
2/2
✓ Branch 0 taken 5430 times.
✓ Branch 1 taken 20761 times.
26191 if (link->type == AVMEDIA_TYPE_AUDIO &&
1059
2/2
✓ Branch 0 taken 5192 times.
✓ Branch 1 taken 238 times.
5430 link->outcfg.formats->nb_formats == 1)
1060 5192 break;
1061 }
1062
2/2
✓ Branch 0 taken 27505 times.
✓ Branch 1 taken 5192 times.
32697 if (i == filter->nb_inputs)
1063 27505 return;
1064
1065 5192 format = link->outcfg.formats->formats[0];
1066 5192 bps = av_get_bytes_per_sample(format);
1067
1068
2/2
✓ Branch 0 taken 3994 times.
✓ Branch 1 taken 5192 times.
9186 for (i = 0; i < filter->nb_outputs; i++) {
1069 3994 AVFilterLink *outlink = filter->outputs[i];
1070 3994 int best_idx = -1, best_score = INT_MIN;
1071
1072
2/2
✓ Branch 0 taken 3992 times.
✓ Branch 1 taken 2 times.
3994 if (outlink->type != AVMEDIA_TYPE_AUDIO ||
1073
2/2
✓ Branch 0 taken 3918 times.
✓ Branch 1 taken 74 times.
3992 outlink->incfg.formats->nb_formats < 2)
1074 3920 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 6468 static void swap_sample_fmts(AVFilterGraph *graph)
1110 {
1111 int i;
1112
1113
2/2
✓ Branch 0 taken 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++)
1114 32697 swap_sample_fmts_on_filter(graph->filters[i]);
1115
1116 6468 }
1117
1118 6468 static int pick_formats(AVFilterGraph *graph)
1119 {
1120 int i, j, ret;
1121 int change;
1122
1123 do{
1124 12977 change = 0;
1125
2/2
✓ Branch 0 taken 65886 times.
✓ Branch 1 taken 12977 times.
78863 for (i = 0; i < graph->nb_filters; i++) {
1126 65886 AVFilterContext *filter = graph->filters[i];
1127
2/2
✓ Branch 0 taken 52689 times.
✓ Branch 1 taken 13197 times.
65886 if (filter->nb_inputs){
1128
2/2
✓ Branch 0 taken 52838 times.
✓ Branch 1 taken 52689 times.
105527 for (j = 0; j < filter->nb_inputs; j++){
1129
4/4
✓ Branch 0 taken 13041 times.
✓ Branch 1 taken 39797 times.
✓ Branch 2 taken 12690 times.
✓ Branch 3 taken 351 times.
52838 if (filter->inputs[j]->incfg.formats && filter->inputs[j]->incfg.formats->nb_formats == 1) {
1130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12690 times.
12690 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1131 return ret;
1132 12690 change = 1;
1133 }
1134 }
1135 }
1136
2/2
✓ Branch 0 taken 52775 times.
✓ Branch 1 taken 13111 times.
65886 if (filter->nb_outputs){
1137
2/2
✓ Branch 0 taken 52838 times.
✓ Branch 1 taken 52775 times.
105613 for (j = 0; j < filter->nb_outputs; j++){
1138
4/4
✓ Branch 0 taken 13703 times.
✓ Branch 1 taken 39135 times.
✓ Branch 2 taken 13266 times.
✓ Branch 3 taken 437 times.
52838 if (filter->outputs[j]->incfg.formats && filter->outputs[j]->incfg.formats->nb_formats == 1) {
1139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13266 times.
13266 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1140 return ret;
1141 13266 change = 1;
1142 }
1143 }
1144 }
1145
6/6
✓ Branch 0 taken 52689 times.
✓ Branch 1 taken 13197 times.
✓ Branch 2 taken 39578 times.
✓ Branch 3 taken 13111 times.
✓ Branch 4 taken 39365 times.
✓ Branch 5 taken 213 times.
65886 if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
1146
2/2
✓ Branch 0 taken 39426 times.
✓ Branch 1 taken 39365 times.
78791 for (j = 0; j < filter->nb_outputs; j++) {
1147
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 39225 times.
39426 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 6509 times.
✓ Branch 1 taken 6468 times.
12977 }while(change);
1156
1157
2/2
✓ Branch 0 taken 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++) {
1158 32697 AVFilterContext *filter = graph->filters[i];
1159
1160
2/2
✓ Branch 0 taken 26205 times.
✓ Branch 1 taken 32697 times.
58902 for (j = 0; j < filter->nb_inputs; j++)
1161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26205 times.
26205 if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
1162 return ret;
1163
2/2
✓ Branch 0 taken 26205 times.
✓ Branch 1 taken 32697 times.
58902 for (j = 0; j < filter->nb_outputs; j++)
1164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 26205 times.
26205 if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
1165 return ret;
1166 }
1167 6468 return 0;
1168 }
1169
1170 /**
1171 * Configure the formats of all the links in the graph.
1172 */
1173 6468 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 6468 times.
6482 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 6468 times.
6468 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 6468 times.
6468 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 6468 swap_sample_fmts(graph);
1192 6468 swap_samplerates(graph);
1193 6468 swap_channel_layouts(graph);
1194
1195
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6468 times.
6468 if ((ret = pick_formats(graph)) < 0)
1196 return ret;
1197
1198 6468 return 0;
1199 }
1200
1201 6468 static int graph_config_pointers(AVFilterGraph *graph, void *log_ctx)
1202 {
1203 unsigned i, j;
1204 6468 int sink_links_count = 0, n = 0;
1205 AVFilterContext *f;
1206 FilterLinkInternal **sinks;
1207
1208
2/2
✓ Branch 0 taken 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++) {
1209 32697 f = graph->filters[i];
1210
2/2
✓ Branch 0 taken 26205 times.
✓ Branch 1 taken 32697 times.
58902 for (j = 0; j < f->nb_inputs; j++) {
1211 26205 f->inputs[j]->graph = graph;
1212 26205 ff_link_internal(f->inputs[j])->age_index = -1;
1213 }
1214
2/2
✓ Branch 0 taken 26205 times.
✓ Branch 1 taken 32697 times.
58902 for (j = 0; j < f->nb_outputs; j++) {
1215 26205 f->outputs[j]->graph = graph;
1216 26205 ff_link_internal(f->outputs[j])->age_index = -1;
1217 }
1218
2/2
✓ Branch 0 taken 6521 times.
✓ Branch 1 taken 26176 times.
32697 if (!f->nb_outputs) {
1219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6521 times.
6521 if (f->nb_inputs > INT_MAX - sink_links_count)
1220 return AVERROR(EINVAL);
1221 6521 sink_links_count += f->nb_inputs;
1222 }
1223 }
1224 6468 sinks = av_calloc(sink_links_count, sizeof(*sinks));
1225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6468 times.
6468 if (!sinks)
1226 return AVERROR(ENOMEM);
1227
2/2
✓ Branch 0 taken 32697 times.
✓ Branch 1 taken 6468 times.
39165 for (i = 0; i < graph->nb_filters; i++) {
1228 32697 f = graph->filters[i];
1229
2/2
✓ Branch 0 taken 6521 times.
✓ Branch 1 taken 26176 times.
32697 if (!f->nb_outputs) {
1230
2/2
✓ Branch 0 taken 6521 times.
✓ Branch 1 taken 6521 times.
13042 for (j = 0; j < f->nb_inputs; j++) {
1231 6521 sinks[n] = ff_link_internal(f->inputs[j]);
1232 6521 sinks[n]->age_index = n;
1233 6521 n++;
1234 }
1235 }
1236 }
1237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6468 times.
6468 av_assert0(n == sink_links_count);
1238 6468 fffiltergraph(graph)->sink_links = sinks;
1239 6468 fffiltergraph(graph)->sink_links_count = sink_links_count;
1240 6468 return 0;
1241 }
1242
1243 6468 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
1244 {
1245 int ret;
1246
1247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6468 times.
6468 if ((ret = graph_check_validity(graphctx, log_ctx)))
1248 return ret;
1249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6468 times.
6468 if ((ret = graph_config_formats(graphctx, log_ctx)))
1250 return ret;
1251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6468 times.
6468 if ((ret = graph_config_links(graphctx, log_ctx)))
1252 return ret;
1253
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6468 times.
6468 if ((ret = graph_check_links(graphctx, log_ctx)))
1254 return ret;
1255
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6468 times.
6468 if ((ret = graph_config_pointers(graphctx, log_ctx)))
1256 return ret;
1257
1258 6468 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 371623 static void heap_bubble_up(FFFilterGraph *graph,
1323 FilterLinkInternal *li, int index)
1324 {
1325 371623 FilterLinkInternal **links = graph->sink_links;
1326
1327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 371623 times.
371623 av_assert0(index >= 0);
1328
1329
2/2
✓ Branch 0 taken 609 times.
✓ Branch 1 taken 371623 times.
372232 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 371623 links[index] = li;
1338 371623 li->age_index = index;
1339 371623 }
1340
1341 371653 static void heap_bubble_down(FFFilterGraph *graph,
1342 FilterLinkInternal *li, int index)
1343 {
1344 371653 FilterLinkInternal **links = graph->sink_links;
1345
1346
1/2
✓ Branch 0 taken 371653 times.
✗ Branch 1 not taken.
371653 av_assert0(index >= 0);
1347
1348 3410 while (1) {
1349 375063 int child = 2 * index + 1;
1350
2/2
✓ Branch 0 taken 371346 times.
✓ Branch 1 taken 3717 times.
375063 if (child >= graph->sink_links_count)
1351 371346 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 371653 links[index] = li;
1362 371653 li->age_index = index;
1363 371653 }
1364
1365 371623 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, FilterLinkInternal *li)
1366 {
1367 371623 FFFilterGraph *graphi = fffiltergraph(graph);
1368
1369 371623 heap_bubble_up (graphi, li, li->age_index);
1370 371623 heap_bubble_down(graphi, li, li->age_index);
1371 371623 }
1372
1373 714661 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1374 {
1375 714661 FFFilterGraph *graphi = fffiltergraph(graph);
1376 714661 FilterLinkInternal *oldesti = graphi->sink_links[0];
1377 714661 AVFilterLink *oldest = &oldesti->l;
1378 int64_t frame_count;
1379 int r;
1380
1381
2/2
✓ Branch 0 taken 714708 times.
✓ Branch 1 taken 3735 times.
718443 while (graphi->sink_links_count) {
1382 714708 oldesti = graphi->sink_links[0];
1383 714708 oldest = &oldesti->l;
1384
2/2
✓ Branch 0 taken 714702 times.
✓ Branch 1 taken 6 times.
714708 if (oldest->dst->filter->activate) {
1385 714702 r = av_buffersink_get_frame_flags(oldest->dst, NULL,
1386 AV_BUFFERSINK_FLAG_PEEK);
1387
2/2
✓ Branch 0 taken 710920 times.
✓ Branch 1 taken 3782 times.
714702 if (r != AVERROR_EOF)
1388 710920 return r;
1389 } else {
1390 6 r = ff_request_frame(oldest);
1391 }
1392
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3782 times.
3788 if (r != AVERROR_EOF)
1393 6 break;
1394 3782 av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1395 3782 oldest->dst->name,
1396 3782 oldest->dstpad->name);
1397 /* EOF: remove the link from the heap */
1398
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 3752 times.
3782 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 3782 oldesti->age_index = -1;
1402 }
1403
2/2
✓ Branch 0 taken 3735 times.
✓ Branch 1 taken 6 times.
3741 if (!graphi->sink_links_count)
1404 3735 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 3871518 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 3871518 times.
3871518 av_assert0(graph->nb_filters);
1426 3871518 filter = graph->filters[0];
1427
2/2
✓ Branch 0 taken 14270093 times.
✓ Branch 1 taken 3871518 times.
18141611 for (i = 1; i < graph->nb_filters; i++)
1428
2/2
✓ Branch 0 taken 2154948 times.
✓ Branch 1 taken 12115145 times.
14270093 if (graph->filters[i]->ready > filter->ready)
1429 2154948 filter = graph->filters[i];
1430
2/2
✓ Branch 0 taken 712001 times.
✓ Branch 1 taken 3159517 times.
3871518 if (!filter->ready)
1431 712001 return AVERROR(EAGAIN);
1432 3159517 return ff_filter_activate(filter);
1433 }
1434