lavfi: add a slice threading infrastructure
[ffmpeg.git] / libavfilter / avfiltergraph.c
1 /*
2  * filter graphs
3  * Copyright (c) 2008 Vitor Sessak
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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/avstring.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/common.h"
31 #include "libavutil/log.h"
32 #include "libavutil/opt.h"
33
34 #include "avfilter.h"
35 #include "formats.h"
36 #include "internal.h"
37 #include "thread.h"
38
39 #define OFFSET(x) offsetof(AVFilterGraph, x)
40 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
41 static const AVOption filtergraph_options[] = {
42     { "thread_type", "Allowed thread types", OFFSET(thread_type), AV_OPT_TYPE_FLAGS,
43         { .i64 = AVFILTER_THREAD_SLICE }, 0, INT_MAX, FLAGS, "thread_type" },
44         { "slice", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AVFILTER_THREAD_SLICE }, .flags = FLAGS, .unit = "thread_type" },
45     { "threads",     "Maximum number of threads", OFFSET(nb_threads),
46         AV_OPT_TYPE_INT,   { .i64 = 0 }, 0, INT_MAX, FLAGS },
47     { NULL },
48 };
49
50 static const AVClass filtergraph_class = {
51     .class_name = "AVFilterGraph",
52     .item_name  = av_default_item_name,
53     .version    = LIBAVUTIL_VERSION_INT,
54     .option     = filtergraph_options,
55 };
56
57 #if !HAVE_THREADS
58 void ff_graph_thread_free(AVFilterGraph *graph)
59 {
60 }
61
62 int ff_graph_thread_init(AVFilterGraph *graph)
63 {
64     graph->thread_type = 0;
65     graph->nb_threads  = 1;
66     return 0;
67 }
68 #endif
69
70 AVFilterGraph *avfilter_graph_alloc(void)
71 {
72     AVFilterGraph *ret = av_mallocz(sizeof(*ret));
73     if (!ret)
74         return NULL;
75
76     ret->internal = av_mallocz(sizeof(*ret->internal));
77     if (!ret->internal) {
78         av_freep(&ret);
79         return NULL;
80     }
81
82     ret->av_class = &filtergraph_class;
83     av_opt_set_defaults(ret);
84
85     return ret;
86 }
87
88 void ff_filter_graph_remove_filter(AVFilterGraph *graph, AVFilterContext *filter)
89 {
90     int i;
91     for (i = 0; i < graph->nb_filters; i++) {
92         if (graph->filters[i] == filter) {
93             FFSWAP(AVFilterContext*, graph->filters[i],
94                    graph->filters[graph->nb_filters - 1]);
95             graph->nb_filters--;
96             return;
97         }
98     }
99 }
100
101 void avfilter_graph_free(AVFilterGraph **graph)
102 {
103     if (!*graph)
104         return;
105
106     while ((*graph)->nb_filters)
107         avfilter_free((*graph)->filters[0]);
108
109     ff_graph_thread_free(*graph);
110
111     av_freep(&(*graph)->scale_sws_opts);
112     av_freep(&(*graph)->resample_lavr_opts);
113     av_freep(&(*graph)->filters);
114     av_freep(&(*graph)->internal);
115     av_freep(graph);
116 }
117
118 #if FF_API_AVFILTER_OPEN
119 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
120 {
121     AVFilterContext **filters = av_realloc(graph->filters,
122                                            sizeof(*filters) * (graph->nb_filters + 1));
123     if (!filters)
124         return AVERROR(ENOMEM);
125
126     graph->filters = filters;
127     graph->filters[graph->nb_filters++] = filter;
128
129 #if FF_API_FOO_COUNT
130     graph->filter_count = graph->nb_filters;
131 #endif
132
133     filter->graph = graph;
134
135     return 0;
136 }
137 #endif
138
139 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
140                                  const char *name, const char *args, void *opaque,
141                                  AVFilterGraph *graph_ctx)
142 {
143     int ret;
144
145     *filt_ctx = avfilter_graph_alloc_filter(graph_ctx, filt, name);
146     if (!*filt_ctx)
147         return AVERROR(ENOMEM);
148
149     ret = avfilter_init_str(*filt_ctx, args);
150     if (ret < 0)
151         goto fail;
152
153     return 0;
154
155 fail:
156     if (*filt_ctx)
157         avfilter_free(*filt_ctx);
158     *filt_ctx = NULL;
159     return ret;
160 }
161
162 AVFilterContext *avfilter_graph_alloc_filter(AVFilterGraph *graph,
163                                              const AVFilter *filter,
164                                              const char *name)
165 {
166     AVFilterContext **filters, *s;
167
168     if (graph->thread_type && !graph->internal->thread) {
169         int ret = ff_graph_thread_init(graph);
170         if (ret < 0) {
171             av_log(graph, AV_LOG_ERROR, "Error initializing threading.\n");
172             return NULL;
173         }
174     }
175
176     s = ff_filter_alloc(filter, name);
177     if (!s)
178         return NULL;
179
180     filters = av_realloc(graph->filters, sizeof(*filters) * (graph->nb_filters + 1));
181     if (!filters) {
182         avfilter_free(s);
183         return NULL;
184     }
185
186     graph->filters = filters;
187     graph->filters[graph->nb_filters++] = s;
188
189 #if FF_API_FOO_COUNT
190     graph->filter_count = graph->nb_filters;
191 #endif
192
193     s->graph = graph;
194
195     return s;
196 }
197
198 /**
199  * Check for the validity of graph.
200  *
201  * A graph is considered valid if all its input and output pads are
202  * connected.
203  *
204  * @return 0 in case of success, a negative value otherwise
205  */
206 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
207 {
208     AVFilterContext *filt;
209     int i, j;
210
211     for (i = 0; i < graph->nb_filters; i++) {
212         filt = graph->filters[i];
213
214         for (j = 0; j < filt->nb_inputs; j++) {
215             if (!filt->inputs[j] || !filt->inputs[j]->src) {
216                 av_log(log_ctx, AV_LOG_ERROR,
217                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
218                        filt->input_pads[j].name, filt->name, filt->filter->name);
219                 return AVERROR(EINVAL);
220             }
221         }
222
223         for (j = 0; j < filt->nb_outputs; j++) {
224             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
225                 av_log(log_ctx, AV_LOG_ERROR,
226                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
227                        filt->output_pads[j].name, filt->name, filt->filter->name);
228                 return AVERROR(EINVAL);
229             }
230         }
231     }
232
233     return 0;
234 }
235
236 /**
237  * Configure all the links of graphctx.
238  *
239  * @return 0 in case of success, a negative value otherwise
240  */
241 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
242 {
243     AVFilterContext *filt;
244     int i, ret;
245
246     for (i = 0; i < graph->nb_filters; i++) {
247         filt = graph->filters[i];
248
249         if (!filt->nb_outputs) {
250             if ((ret = avfilter_config_links(filt)))
251                 return ret;
252         }
253     }
254
255     return 0;
256 }
257
258 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
259 {
260     int i;
261
262     for (i = 0; i < graph->nb_filters; i++)
263         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
264             return graph->filters[i];
265
266     return NULL;
267 }
268
269 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
270 {
271     int i, j, ret;
272     int scaler_count = 0, resampler_count = 0;
273
274     /* ask all the sub-filters for their supported media formats */
275     for (i = 0; i < graph->nb_filters; i++) {
276         if (graph->filters[i]->filter->query_formats)
277             graph->filters[i]->filter->query_formats(graph->filters[i]);
278         else
279             ff_default_query_formats(graph->filters[i]);
280     }
281
282     /* go through and merge as many format lists as possible */
283     for (i = 0; i < graph->nb_filters; i++) {
284         AVFilterContext *filter = graph->filters[i];
285
286         for (j = 0; j < filter->nb_inputs; j++) {
287             AVFilterLink *link = filter->inputs[j];
288             int convert_needed = 0;
289
290             if (!link)
291                 continue;
292
293             if (link->in_formats != link->out_formats &&
294                 !ff_merge_formats(link->in_formats,
295                                         link->out_formats))
296                 convert_needed = 1;
297             if (link->type == AVMEDIA_TYPE_AUDIO) {
298                 if (link->in_channel_layouts != link->out_channel_layouts &&
299                     !ff_merge_channel_layouts(link->in_channel_layouts,
300                                               link->out_channel_layouts))
301                     convert_needed = 1;
302                 if (link->in_samplerates != link->out_samplerates &&
303                     !ff_merge_samplerates(link->in_samplerates,
304                                           link->out_samplerates))
305                     convert_needed = 1;
306             }
307
308             if (convert_needed) {
309                 AVFilterContext *convert;
310                 AVFilter *filter;
311                 AVFilterLink *inlink, *outlink;
312                 char scale_args[256];
313                 char inst_name[30];
314
315                 /* couldn't merge format lists. auto-insert conversion filter */
316                 switch (link->type) {
317                 case AVMEDIA_TYPE_VIDEO:
318                     if (!(filter = avfilter_get_by_name("scale"))) {
319                         av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
320                                "not present, cannot convert pixel formats.\n");
321                         return AVERROR(EINVAL);
322                     }
323
324                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
325                              scaler_count++);
326
327                     if ((ret = avfilter_graph_create_filter(&convert, filter,
328                                                             inst_name, graph->scale_sws_opts, NULL,
329                                                             graph)) < 0)
330                         return ret;
331                     break;
332                 case AVMEDIA_TYPE_AUDIO:
333                     if (!(filter = avfilter_get_by_name("resample"))) {
334                         av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
335                                "not present, cannot convert audio formats.\n");
336                         return AVERROR(EINVAL);
337                     }
338
339                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
340                              resampler_count++);
341                     scale_args[0] = '\0';
342                     if (graph->resample_lavr_opts)
343                         snprintf(scale_args, sizeof(scale_args), "%s",
344                                  graph->resample_lavr_opts);
345                     if ((ret = avfilter_graph_create_filter(&convert, filter,
346                                                             inst_name, scale_args,
347                                                             NULL, graph)) < 0)
348                         return ret;
349                     break;
350                 default:
351                     return AVERROR(EINVAL);
352                 }
353
354                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
355                     return ret;
356
357                 convert->filter->query_formats(convert);
358                 inlink  = convert->inputs[0];
359                 outlink = convert->outputs[0];
360                 if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats) ||
361                     !ff_merge_formats(outlink->in_formats, outlink->out_formats))
362                     ret |= AVERROR(ENOSYS);
363                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
364                     (!ff_merge_samplerates(inlink->in_samplerates,
365                                            inlink->out_samplerates) ||
366                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
367                                                inlink->out_channel_layouts)))
368                     ret |= AVERROR(ENOSYS);
369                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
370                     (!ff_merge_samplerates(outlink->in_samplerates,
371                                            outlink->out_samplerates) ||
372                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
373                                                outlink->out_channel_layouts)))
374                     ret |= AVERROR(ENOSYS);
375
376                 if (ret < 0) {
377                     av_log(log_ctx, AV_LOG_ERROR,
378                            "Impossible to convert between the formats supported by the filter "
379                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
380                     return ret;
381                 }
382             }
383         }
384     }
385
386     return 0;
387 }
388
389 static int pick_format(AVFilterLink *link)
390 {
391     if (!link || !link->in_formats)
392         return 0;
393
394     link->in_formats->nb_formats = 1;
395     link->format = link->in_formats->formats[0];
396
397     if (link->type == AVMEDIA_TYPE_AUDIO) {
398         if (!link->in_samplerates->nb_formats) {
399             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
400                    " the link between filters %s and %s.\n", link->src->name,
401                    link->dst->name);
402             return AVERROR(EINVAL);
403         }
404         link->in_samplerates->nb_formats = 1;
405         link->sample_rate = link->in_samplerates->formats[0];
406
407         if (!link->in_channel_layouts->nb_channel_layouts) {
408             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
409                    "the link between filters %s and %s.\n", link->src->name,
410                    link->dst->name);
411             return AVERROR(EINVAL);
412         }
413         link->in_channel_layouts->nb_channel_layouts = 1;
414         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
415     }
416
417     ff_formats_unref(&link->in_formats);
418     ff_formats_unref(&link->out_formats);
419     ff_formats_unref(&link->in_samplerates);
420     ff_formats_unref(&link->out_samplerates);
421     ff_channel_layouts_unref(&link->in_channel_layouts);
422     ff_channel_layouts_unref(&link->out_channel_layouts);
423
424     return 0;
425 }
426
427 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
428 do {                                                                   \
429     for (i = 0; i < filter->nb_inputs; i++) {                          \
430         AVFilterLink *link = filter->inputs[i];                        \
431         fmt_type fmt;                                                  \
432                                                                        \
433         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
434             continue;                                                  \
435         fmt = link->out_ ## list->var[0];                              \
436                                                                        \
437         for (j = 0; j < filter->nb_outputs; j++) {                     \
438             AVFilterLink *out_link = filter->outputs[j];               \
439             list_type *fmts;                                           \
440                                                                        \
441             if (link->type != out_link->type ||                        \
442                 out_link->in_ ## list->nb == 1)                        \
443                 continue;                                              \
444             fmts = out_link->in_ ## list;                              \
445                                                                        \
446             if (!out_link->in_ ## list->nb) {                          \
447                 add_format(&out_link->in_ ##list, fmt);                \
448                 break;                                                 \
449             }                                                          \
450                                                                        \
451             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
452                 if (fmts->var[k] == fmt) {                             \
453                     fmts->var[0]  = fmt;                               \
454                     fmts->nb = 1;                                      \
455                     ret = 1;                                           \
456                     break;                                             \
457                 }                                                      \
458         }                                                              \
459     }                                                                  \
460 } while (0)
461
462 static int reduce_formats_on_filter(AVFilterContext *filter)
463 {
464     int i, j, k, ret = 0;
465
466     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
467                    nb_formats, ff_add_format);
468     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
469                    nb_formats, ff_add_format);
470     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
471                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
472
473     return ret;
474 }
475
476 static void reduce_formats(AVFilterGraph *graph)
477 {
478     int i, reduced;
479
480     do {
481         reduced = 0;
482
483         for (i = 0; i < graph->nb_filters; i++)
484             reduced |= reduce_formats_on_filter(graph->filters[i]);
485     } while (reduced);
486 }
487
488 static void swap_samplerates_on_filter(AVFilterContext *filter)
489 {
490     AVFilterLink *link = NULL;
491     int sample_rate;
492     int i, j;
493
494     for (i = 0; i < filter->nb_inputs; i++) {
495         link = filter->inputs[i];
496
497         if (link->type == AVMEDIA_TYPE_AUDIO &&
498             link->out_samplerates->nb_formats== 1)
499             break;
500     }
501     if (i == filter->nb_inputs)
502         return;
503
504     sample_rate = link->out_samplerates->formats[0];
505
506     for (i = 0; i < filter->nb_outputs; i++) {
507         AVFilterLink *outlink = filter->outputs[i];
508         int best_idx, best_diff = INT_MAX;
509
510         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
511             outlink->in_samplerates->nb_formats < 2)
512             continue;
513
514         for (j = 0; j < outlink->in_samplerates->nb_formats; j++) {
515             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
516
517             if (diff < best_diff) {
518                 best_diff = diff;
519                 best_idx  = j;
520             }
521         }
522         FFSWAP(int, outlink->in_samplerates->formats[0],
523                outlink->in_samplerates->formats[best_idx]);
524     }
525 }
526
527 static void swap_samplerates(AVFilterGraph *graph)
528 {
529     int i;
530
531     for (i = 0; i < graph->nb_filters; i++)
532         swap_samplerates_on_filter(graph->filters[i]);
533 }
534
535 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
536 #define CH_FRONT_PAIR  (AV_CH_FRONT_LEFT           | AV_CH_FRONT_RIGHT)
537 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT          | AV_CH_STEREO_RIGHT)
538 #define CH_WIDE_PAIR   (AV_CH_WIDE_LEFT            | AV_CH_WIDE_RIGHT)
539 #define CH_SIDE_PAIR   (AV_CH_SIDE_LEFT            | AV_CH_SIDE_RIGHT)
540 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
541 #define CH_BACK_PAIR   (AV_CH_BACK_LEFT            | AV_CH_BACK_RIGHT)
542
543 /* allowable substitutions for channel pairs when comparing layouts,
544  * ordered by priority for both values */
545 static const uint64_t ch_subst[][2] = {
546     { CH_FRONT_PAIR,      CH_CENTER_PAIR     },
547     { CH_FRONT_PAIR,      CH_WIDE_PAIR       },
548     { CH_FRONT_PAIR,      AV_CH_FRONT_CENTER },
549     { CH_CENTER_PAIR,     CH_FRONT_PAIR      },
550     { CH_CENTER_PAIR,     CH_WIDE_PAIR       },
551     { CH_CENTER_PAIR,     AV_CH_FRONT_CENTER },
552     { CH_WIDE_PAIR,       CH_FRONT_PAIR      },
553     { CH_WIDE_PAIR,       CH_CENTER_PAIR     },
554     { CH_WIDE_PAIR,       AV_CH_FRONT_CENTER },
555     { AV_CH_FRONT_CENTER, CH_FRONT_PAIR      },
556     { AV_CH_FRONT_CENTER, CH_CENTER_PAIR     },
557     { AV_CH_FRONT_CENTER, CH_WIDE_PAIR       },
558     { CH_SIDE_PAIR,       CH_DIRECT_PAIR     },
559     { CH_SIDE_PAIR,       CH_BACK_PAIR       },
560     { CH_SIDE_PAIR,       AV_CH_BACK_CENTER  },
561     { CH_BACK_PAIR,       CH_DIRECT_PAIR     },
562     { CH_BACK_PAIR,       CH_SIDE_PAIR       },
563     { CH_BACK_PAIR,       AV_CH_BACK_CENTER  },
564     { AV_CH_BACK_CENTER,  CH_BACK_PAIR       },
565     { AV_CH_BACK_CENTER,  CH_DIRECT_PAIR     },
566     { AV_CH_BACK_CENTER,  CH_SIDE_PAIR       },
567 };
568
569 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
570 {
571     AVFilterLink *link = NULL;
572     int i, j, k;
573
574     for (i = 0; i < filter->nb_inputs; i++) {
575         link = filter->inputs[i];
576
577         if (link->type == AVMEDIA_TYPE_AUDIO &&
578             link->out_channel_layouts->nb_channel_layouts == 1)
579             break;
580     }
581     if (i == filter->nb_inputs)
582         return;
583
584     for (i = 0; i < filter->nb_outputs; i++) {
585         AVFilterLink *outlink = filter->outputs[i];
586         int best_idx = -1, best_score = INT_MIN, best_count_diff = INT_MAX;
587
588         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
589             outlink->in_channel_layouts->nb_channel_layouts < 2)
590             continue;
591
592         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
593             uint64_t  in_chlayout = link->out_channel_layouts->channel_layouts[0];
594             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
595             int  in_channels      = av_get_channel_layout_nb_channels(in_chlayout);
596             int out_channels      = av_get_channel_layout_nb_channels(out_chlayout);
597             int count_diff        = out_channels - in_channels;
598             int matched_channels, extra_channels;
599             int score = 0;
600
601             /* channel substitution */
602             for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
603                 uint64_t cmp0 = ch_subst[k][0];
604                 uint64_t cmp1 = ch_subst[k][1];
605                 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
606                     (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
607                     in_chlayout  &= ~cmp0;
608                     out_chlayout &= ~cmp1;
609                     /* add score for channel match, minus a deduction for
610                        having to do the substitution */
611                     score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
612                 }
613             }
614
615             /* no penalty for LFE channel mismatch */
616             if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
617                 (out_chlayout & AV_CH_LOW_FREQUENCY))
618                 score += 10;
619             in_chlayout  &= ~AV_CH_LOW_FREQUENCY;
620             out_chlayout &= ~AV_CH_LOW_FREQUENCY;
621
622             matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
623                                                                  out_chlayout);
624             extra_channels   = av_get_channel_layout_nb_channels(out_chlayout &
625                                                                  (~in_chlayout));
626             score += 10 * matched_channels - 5 * extra_channels;
627
628             if (score > best_score ||
629                 (count_diff < best_count_diff && score == best_score)) {
630                 best_score = score;
631                 best_idx   = j;
632                 best_count_diff = count_diff;
633             }
634         }
635         av_assert0(best_idx >= 0);
636         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
637                outlink->in_channel_layouts->channel_layouts[best_idx]);
638     }
639
640 }
641
642 static void swap_channel_layouts(AVFilterGraph *graph)
643 {
644     int i;
645
646     for (i = 0; i < graph->nb_filters; i++)
647         swap_channel_layouts_on_filter(graph->filters[i]);
648 }
649
650 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
651 {
652     AVFilterLink *link = NULL;
653     int format, bps;
654     int i, j;
655
656     for (i = 0; i < filter->nb_inputs; i++) {
657         link = filter->inputs[i];
658
659         if (link->type == AVMEDIA_TYPE_AUDIO &&
660             link->out_formats->nb_formats == 1)
661             break;
662     }
663     if (i == filter->nb_inputs)
664         return;
665
666     format = link->out_formats->formats[0];
667     bps    = av_get_bytes_per_sample(format);
668
669     for (i = 0; i < filter->nb_outputs; i++) {
670         AVFilterLink *outlink = filter->outputs[i];
671         int best_idx = -1, best_score = INT_MIN;
672
673         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
674             outlink->in_formats->nb_formats < 2)
675             continue;
676
677         for (j = 0; j < outlink->in_formats->nb_formats; j++) {
678             int out_format = outlink->in_formats->formats[j];
679             int out_bps    = av_get_bytes_per_sample(out_format);
680             int score;
681
682             if (av_get_packed_sample_fmt(out_format) == format ||
683                 av_get_planar_sample_fmt(out_format) == format) {
684                 best_idx   = j;
685                 break;
686             }
687
688             /* for s32 and float prefer double to prevent loss of information */
689             if (bps == 4 && out_bps == 8) {
690                 best_idx = j;
691                 break;
692             }
693
694             /* prefer closest higher or equal bps */
695             score = -abs(out_bps - bps);
696             if (out_bps >= bps)
697                 score += INT_MAX/2;
698
699             if (score > best_score) {
700                 best_score = score;
701                 best_idx   = j;
702             }
703         }
704         av_assert0(best_idx >= 0);
705         FFSWAP(int, outlink->in_formats->formats[0],
706                outlink->in_formats->formats[best_idx]);
707     }
708 }
709
710 static void swap_sample_fmts(AVFilterGraph *graph)
711 {
712     int i;
713
714     for (i = 0; i < graph->nb_filters; i++)
715         swap_sample_fmts_on_filter(graph->filters[i]);
716
717 }
718
719 static int pick_formats(AVFilterGraph *graph)
720 {
721     int i, j, ret;
722
723     for (i = 0; i < graph->nb_filters; i++) {
724         AVFilterContext *filter = graph->filters[i];
725
726         for (j = 0; j < filter->nb_inputs; j++)
727             if ((ret = pick_format(filter->inputs[j])) < 0)
728                 return ret;
729         for (j = 0; j < filter->nb_outputs; j++)
730             if ((ret = pick_format(filter->outputs[j])) < 0)
731                 return ret;
732     }
733     return 0;
734 }
735
736 /**
737  * Configure the formats of all the links in the graph.
738  */
739 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
740 {
741     int ret;
742
743     /* find supported formats from sub-filters, and merge along links */
744     if ((ret = query_formats(graph, log_ctx)) < 0)
745         return ret;
746
747     /* Once everything is merged, it's possible that we'll still have
748      * multiple valid media format choices. We try to minimize the amount
749      * of format conversion inside filters */
750     reduce_formats(graph);
751
752     /* for audio filters, ensure the best format, sample rate and channel layout
753      * is selected */
754     swap_sample_fmts(graph);
755     swap_samplerates(graph);
756     swap_channel_layouts(graph);
757
758     if ((ret = pick_formats(graph)) < 0)
759         return ret;
760
761     return 0;
762 }
763
764 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
765 {
766     AVFilterContext *f;
767     int i, j, ret;
768     int fifo_count = 0;
769
770     for (i = 0; i < graph->nb_filters; i++) {
771         f = graph->filters[i];
772
773         for (j = 0; j < f->nb_inputs; j++) {
774             AVFilterLink *link = f->inputs[j];
775             AVFilterContext *fifo_ctx;
776             AVFilter *fifo;
777             char name[32];
778
779             if (!link->dstpad->needs_fifo)
780                 continue;
781
782             fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
783                    avfilter_get_by_name("fifo") :
784                    avfilter_get_by_name("afifo");
785
786             snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
787
788             ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
789                                                NULL, graph);
790             if (ret < 0)
791                 return ret;
792
793             ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
794             if (ret < 0)
795                 return ret;
796         }
797     }
798
799     return 0;
800 }
801
802 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
803 {
804     int ret;
805
806     if ((ret = graph_check_validity(graphctx, log_ctx)))
807         return ret;
808     if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
809         return ret;
810     if ((ret = graph_config_formats(graphctx, log_ctx)))
811         return ret;
812     if ((ret = graph_config_links(graphctx, log_ctx)))
813         return ret;
814
815     return 0;
816 }