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