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