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