]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
fate: test both direct and indirect paths in hqdn3d and gradfun.
[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 FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <string.h>
24
25 #include "libavutil/avassert.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavcodec/avcodec.h" // avcodec_find_best_pix_fmt_of_2()
30 #include "avfilter.h"
31 #include "avfiltergraph.h"
32 #include "formats.h"
33 #include "internal.h"
34
35 #define OFFSET(x) offsetof(AVFilterGraph,x)
36
37 static const AVOption options[]={
38 {"scale_sws_opts"       , "default scale filter options"        , OFFSET(scale_sws_opts)        ,  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
39 {"aresample_swr_opts"   , "default aresample filter options"    , OFFSET(aresample_swr_opts)    ,  AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, 0 },
40 {0}
41 };
42
43
44 static const AVClass filtergraph_class = {
45     .class_name = "AVFilterGraph",
46     .item_name  = av_default_item_name,
47     .option     = options,
48     .version    = LIBAVUTIL_VERSION_INT,
49     .category   = AV_CLASS_CATEGORY_FILTER,
50 };
51
52 AVFilterGraph *avfilter_graph_alloc(void)
53 {
54     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
55     if (!ret)
56         return NULL;
57     ret->av_class = &filtergraph_class;
58     return ret;
59 }
60
61 void avfilter_graph_free(AVFilterGraph **graph)
62 {
63     if (!*graph)
64         return;
65     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
66         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
67     av_freep(&(*graph)->sink_links);
68     av_freep(&(*graph)->scale_sws_opts);
69     av_freep(&(*graph)->aresample_swr_opts);
70     av_freep(&(*graph)->resample_lavr_opts);
71     av_freep(&(*graph)->filters);
72     av_freep(graph);
73 }
74
75 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
76 {
77     AVFilterContext **filters = av_realloc(graph->filters,
78                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
79     if (!filters)
80         return AVERROR(ENOMEM);
81
82     graph->filters = filters;
83     graph->filters[graph->filter_count++] = filter;
84
85     return 0;
86 }
87
88 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
89                                  const char *name, const char *args, void *opaque,
90                                  AVFilterGraph *graph_ctx)
91 {
92     int ret;
93
94     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
95         goto fail;
96     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
97         goto fail;
98     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
99         goto fail;
100     return 0;
101
102 fail:
103     if (*filt_ctx)
104         avfilter_free(*filt_ctx);
105     *filt_ctx = NULL;
106     return ret;
107 }
108
109 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
110 {
111     graph->disable_auto_convert = flags;
112 }
113
114 /**
115  * Check for the validity of graph.
116  *
117  * A graph is considered valid if all its input and output pads are
118  * connected.
119  *
120  * @return 0 in case of success, a negative value otherwise
121  */
122 static int graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
123 {
124     AVFilterContext *filt;
125     int i, j;
126
127     for (i = 0; i < graph->filter_count; i++) {
128         const AVFilterPad *pad;
129         filt = graph->filters[i];
130
131         for (j = 0; j < filt->nb_inputs; j++) {
132             if (!filt->inputs[j] || !filt->inputs[j]->src) {
133                 pad = &filt->input_pads[j];
134                 av_log(log_ctx, AV_LOG_ERROR,
135                        "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
136                        pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
137                 return AVERROR(EINVAL);
138             }
139         }
140
141         for (j = 0; j < filt->nb_outputs; j++) {
142             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
143                 pad = &filt->output_pads[j];
144                 av_log(log_ctx, AV_LOG_ERROR,
145                        "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
146                        pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
147                 return AVERROR(EINVAL);
148             }
149         }
150     }
151
152     return 0;
153 }
154
155 /**
156  * Configure all the links of graphctx.
157  *
158  * @return 0 in case of success, a negative value otherwise
159  */
160 static int graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
161 {
162     AVFilterContext *filt;
163     int i, ret;
164
165     for (i=0; i < graph->filter_count; i++) {
166         filt = graph->filters[i];
167
168         if (!filt->nb_outputs) {
169             if ((ret = avfilter_config_links(filt)))
170                 return ret;
171         }
172     }
173
174     return 0;
175 }
176
177 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
178 {
179     int i;
180
181     for (i = 0; i < graph->filter_count; i++)
182         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
183             return graph->filters[i];
184
185     return NULL;
186 }
187
188 static void sanitize_channel_layouts(void *log, AVFilterChannelLayouts *l)
189 {
190     if (!l)
191         return;
192     if (l->nb_channel_layouts) {
193         if (l->all_layouts || l->all_counts)
194             av_log(log, AV_LOG_WARNING, "All layouts set on non-empty list\n");
195         l->all_layouts = l->all_counts = 0;
196     } else {
197         if (l->all_counts && !l->all_layouts)
198             av_log(log, AV_LOG_WARNING, "All counts without all layouts\n");
199         l->all_layouts = 1;
200     }
201 }
202
203 static int filter_query_formats(AVFilterContext *ctx)
204 {
205     int ret, i;
206     AVFilterFormats *formats;
207     AVFilterChannelLayouts *chlayouts;
208     AVFilterFormats *samplerates;
209     enum AVMediaType type = ctx->inputs  && ctx->inputs [0] ? ctx->inputs [0]->type :
210                             ctx->outputs && ctx->outputs[0] ? ctx->outputs[0]->type :
211                             AVMEDIA_TYPE_VIDEO;
212
213     if ((ret = ctx->filter->query_formats(ctx)) < 0) {
214         av_log(ctx, AV_LOG_ERROR, "Query format failed for '%s': %s\n",
215                ctx->name, av_err2str(ret));
216         return ret;
217     }
218
219     for (i = 0; i < ctx->nb_inputs; i++)
220         sanitize_channel_layouts(ctx, ctx->inputs[i]->out_channel_layouts);
221     for (i = 0; i < ctx->nb_outputs; i++)
222         sanitize_channel_layouts(ctx, ctx->outputs[i]->in_channel_layouts);
223
224     formats = ff_all_formats(type);
225     if (!formats)
226         return AVERROR(ENOMEM);
227     ff_set_common_formats(ctx, formats);
228     if (type == AVMEDIA_TYPE_AUDIO) {
229         samplerates = ff_all_samplerates();
230         if (!samplerates)
231             return AVERROR(ENOMEM);
232         ff_set_common_samplerates(ctx, samplerates);
233         chlayouts = ff_all_channel_layouts();
234         if (!chlayouts)
235             return AVERROR(ENOMEM);
236         ff_set_common_channel_layouts(ctx, chlayouts);
237     }
238     return 0;
239 }
240
241 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
242 {
243     int i, j, ret;
244     int scaler_count = 0, resampler_count = 0;
245
246     for (j = 0; j < 2; j++) {
247     /* ask all the sub-filters for their supported media formats */
248     for (i = 0; i < graph->filter_count; i++) {
249         /* Call query_formats on sources first.
250            This is a temporary workaround for amerge,
251            until format renegociation is implemented. */
252         if (!graph->filters[i]->nb_inputs == j)
253             continue;
254         if (graph->filters[i]->filter->query_formats)
255             ret = filter_query_formats(graph->filters[i]);
256         else
257             ret = ff_default_query_formats(graph->filters[i]);
258         if (ret < 0)
259             return ret;
260     }
261     }
262
263     /* go through and merge as many format lists as possible */
264     for (i = 0; i < graph->filter_count; i++) {
265         AVFilterContext *filter = graph->filters[i];
266
267         for (j = 0; j < filter->nb_inputs; j++) {
268             AVFilterLink *link = filter->inputs[j];
269             int convert_needed = 0;
270
271             if (!link)
272                 continue;
273
274             if (link->in_formats != link->out_formats &&
275                 !ff_merge_formats(link->in_formats, link->out_formats,
276                                   link->type))
277                 convert_needed = 1;
278             if (link->type == AVMEDIA_TYPE_AUDIO) {
279                 if (link->in_channel_layouts != link->out_channel_layouts &&
280                     !ff_merge_channel_layouts(link->in_channel_layouts,
281                                               link->out_channel_layouts))
282                     convert_needed = 1;
283                 if (link->in_samplerates != link->out_samplerates &&
284                     !ff_merge_samplerates(link->in_samplerates,
285                                           link->out_samplerates))
286                     convert_needed = 1;
287             }
288
289             if (convert_needed) {
290                 AVFilterContext *convert;
291                 AVFilter *filter;
292                 AVFilterLink *inlink, *outlink;
293                 char scale_args[256];
294                 char inst_name[30];
295
296                 /* couldn't merge format lists. auto-insert conversion filter */
297                 switch (link->type) {
298                 case AVMEDIA_TYPE_VIDEO:
299                     if (!(filter = avfilter_get_by_name("scale"))) {
300                         av_log(log_ctx, AV_LOG_ERROR, "'scale' filter "
301                                "not present, cannot convert pixel formats.\n");
302                         return AVERROR(EINVAL);
303                     }
304
305                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
306                              scaler_count++);
307                     if (graph->scale_sws_opts)
308                         snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
309                     else
310                         snprintf(scale_args, sizeof(scale_args), "0:0");
311
312                     if ((ret = avfilter_graph_create_filter(&convert, filter,
313                                                             inst_name, scale_args, NULL,
314                                                             graph)) < 0)
315                         return ret;
316                     break;
317                 case AVMEDIA_TYPE_AUDIO:
318                     if (!(filter = avfilter_get_by_name("aresample"))) {
319                         av_log(log_ctx, AV_LOG_ERROR, "'aresample' filter "
320                                "not present, cannot convert audio formats.\n");
321                         return AVERROR(EINVAL);
322                     }
323
324                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
325                              resampler_count++);
326                     scale_args[0] = '\0';
327                     if (graph->aresample_swr_opts)
328                         snprintf(scale_args, sizeof(scale_args), "%s",
329                                  graph->aresample_swr_opts);
330                     if ((ret = avfilter_graph_create_filter(&convert, filter,
331                                                             inst_name, graph->aresample_swr_opts,
332                                                             NULL, graph)) < 0)
333                         return ret;
334                     break;
335                 default:
336                     return AVERROR(EINVAL);
337                 }
338
339                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
340                     return ret;
341
342                 filter_query_formats(convert);
343                 inlink  = convert->inputs[0];
344                 outlink = convert->outputs[0];
345                 if (!ff_merge_formats( inlink->in_formats,  inlink->out_formats,  inlink->type) ||
346                     !ff_merge_formats(outlink->in_formats, outlink->out_formats, outlink->type))
347                     ret |= AVERROR(ENOSYS);
348                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
349                     (!ff_merge_samplerates(inlink->in_samplerates,
350                                            inlink->out_samplerates) ||
351                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
352                                                inlink->out_channel_layouts)))
353                     ret |= AVERROR(ENOSYS);
354                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
355                     (!ff_merge_samplerates(outlink->in_samplerates,
356                                            outlink->out_samplerates) ||
357                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
358                                                outlink->out_channel_layouts)))
359                     ret |= AVERROR(ENOSYS);
360
361                 if (ret < 0) {
362                     av_log(log_ctx, AV_LOG_ERROR,
363                            "Impossible to convert between the formats supported by the filter "
364                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
365                     return ret;
366                 }
367             }
368         }
369     }
370
371     return 0;
372 }
373
374 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
375 {
376     if (!link || !link->in_formats)
377         return 0;
378
379     if (link->type == AVMEDIA_TYPE_VIDEO) {
380         if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
381             int has_alpha= av_pix_fmt_desc_get(ref->format)->nb_components % 2 == 0;
382             enum AVPixelFormat best= AV_PIX_FMT_NONE;
383             int i;
384             for (i=0; i<link->in_formats->format_count; i++) {
385                 enum AVPixelFormat p = link->in_formats->formats[i];
386                 best= avcodec_find_best_pix_fmt_of_2(best, p, ref->format, has_alpha, NULL);
387             }
388             av_log(link->src,AV_LOG_DEBUG, "picking %s out of %d ref:%s alpha:%d\n",
389                    av_get_pix_fmt_name(best), link->in_formats->format_count,
390                    av_get_pix_fmt_name(ref->format), has_alpha);
391             link->in_formats->formats[0] = best;
392         }
393     }
394
395     link->in_formats->format_count = 1;
396     link->format = link->in_formats->formats[0];
397
398     if (link->type == AVMEDIA_TYPE_AUDIO) {
399         if (!link->in_samplerates->format_count) {
400             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
401                    " the link between filters %s and %s.\n", link->src->name,
402                    link->dst->name);
403             return AVERROR(EINVAL);
404         }
405         link->in_samplerates->format_count = 1;
406         link->sample_rate = link->in_samplerates->formats[0];
407
408         if (link->in_channel_layouts->all_layouts) {
409             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
410                    " the link between filters %s and %s.\n", link->src->name,
411                    link->dst->name);
412             return AVERROR(EINVAL);
413         }
414         link->in_channel_layouts->nb_channel_layouts = 1;
415         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
416         if ((link->channels = FF_LAYOUT2COUNT(link->channel_layout)))
417             link->channel_layout = 0;
418         else
419             link->channels = av_get_channel_layout_nb_channels(link->channel_layout);
420     }
421
422     ff_formats_unref(&link->in_formats);
423     ff_formats_unref(&link->out_formats);
424     ff_formats_unref(&link->in_samplerates);
425     ff_formats_unref(&link->out_samplerates);
426     ff_channel_layouts_unref(&link->in_channel_layouts);
427     ff_channel_layouts_unref(&link->out_channel_layouts);
428
429     return 0;
430 }
431
432 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
433 do {                                                                   \
434     for (i = 0; i < filter->nb_inputs; i++) {                          \
435         AVFilterLink *link = filter->inputs[i];                        \
436         fmt_type fmt;                                                  \
437                                                                        \
438         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
439             continue;                                                  \
440         fmt = link->out_ ## list->var[0];                              \
441                                                                        \
442         for (j = 0; j < filter->nb_outputs; j++) {                     \
443             AVFilterLink *out_link = filter->outputs[j];               \
444             list_type *fmts;                                           \
445                                                                        \
446             if (link->type != out_link->type ||                        \
447                 out_link->in_ ## list->nb == 1)                        \
448                 continue;                                              \
449             fmts = out_link->in_ ## list;                              \
450                                                                        \
451             if (!out_link->in_ ## list->nb) {                          \
452                 add_format(&out_link->in_ ##list, fmt);                \
453                 break;                                                 \
454             }                                                          \
455                                                                        \
456             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
457                 if (fmts->var[k] == fmt) {                             \
458                     fmts->var[0]  = fmt;                               \
459                     fmts->nb = 1;                                      \
460                     ret = 1;                                           \
461                     break;                                             \
462                 }                                                      \
463         }                                                              \
464     }                                                                  \
465 } while (0)
466
467 static int reduce_formats_on_filter(AVFilterContext *filter)
468 {
469     int i, j, k, ret = 0;
470
471     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
472                    format_count, ff_add_format);
473     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
474                    format_count, ff_add_format);
475
476     /* reduce channel layouts */
477     for (i = 0; i < filter->nb_inputs; i++) {
478         AVFilterLink *inlink = filter->inputs[i];
479         uint64_t fmt;
480
481         if (!inlink->out_channel_layouts ||
482             inlink->out_channel_layouts->nb_channel_layouts != 1)
483             continue;
484         fmt = inlink->out_channel_layouts->channel_layouts[0];
485
486         for (j = 0; j < filter->nb_outputs; j++) {
487             AVFilterLink *outlink = filter->outputs[j];
488             AVFilterChannelLayouts *fmts;
489
490             fmts = outlink->in_channel_layouts;
491             if (inlink->type != outlink->type || fmts->nb_channel_layouts == 1)
492                 continue;
493
494             if (fmts->all_layouts) {
495                 /* Turn the infinite list into a singleton */
496                 fmts->all_layouts = fmts->all_counts  = 0;
497                 ff_add_channel_layout(&outlink->in_channel_layouts, fmt);
498                 break;
499             }
500
501             for (k = 0; k < outlink->in_channel_layouts->nb_channel_layouts; k++) {
502                 if (fmts->channel_layouts[k] == fmt) {
503                     fmts->channel_layouts[0]  = fmt;
504                     fmts->nb_channel_layouts = 1;
505                     ret = 1;
506                     break;
507                 }
508             }
509         }
510     }
511
512     return ret;
513 }
514
515 static void reduce_formats(AVFilterGraph *graph)
516 {
517     int i, reduced;
518
519     do {
520         reduced = 0;
521
522         for (i = 0; i < graph->filter_count; i++)
523             reduced |= reduce_formats_on_filter(graph->filters[i]);
524     } while (reduced);
525 }
526
527 static void swap_samplerates_on_filter(AVFilterContext *filter)
528 {
529     AVFilterLink *link = NULL;
530     int sample_rate;
531     int i, j;
532
533     for (i = 0; i < filter->nb_inputs; i++) {
534         link = filter->inputs[i];
535
536         if (link->type == AVMEDIA_TYPE_AUDIO &&
537             link->out_samplerates->format_count == 1)
538             break;
539     }
540     if (i == filter->nb_inputs)
541         return;
542
543     sample_rate = link->out_samplerates->formats[0];
544
545     for (i = 0; i < filter->nb_outputs; i++) {
546         AVFilterLink *outlink = filter->outputs[i];
547         int best_idx, best_diff = INT_MAX;
548
549         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
550             outlink->in_samplerates->format_count < 2)
551             continue;
552
553         for (j = 0; j < outlink->in_samplerates->format_count; j++) {
554             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
555
556             if (diff < best_diff) {
557                 best_diff = diff;
558                 best_idx  = j;
559             }
560         }
561         FFSWAP(int, outlink->in_samplerates->formats[0],
562                outlink->in_samplerates->formats[best_idx]);
563     }
564 }
565
566 static void swap_samplerates(AVFilterGraph *graph)
567 {
568     int i;
569
570     for (i = 0; i < graph->filter_count; i++)
571         swap_samplerates_on_filter(graph->filters[i]);
572 }
573
574 #define CH_CENTER_PAIR (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)
575 #define CH_FRONT_PAIR  (AV_CH_FRONT_LEFT           | AV_CH_FRONT_RIGHT)
576 #define CH_STEREO_PAIR (AV_CH_STEREO_LEFT          | AV_CH_STEREO_RIGHT)
577 #define CH_WIDE_PAIR   (AV_CH_WIDE_LEFT            | AV_CH_WIDE_RIGHT)
578 #define CH_SIDE_PAIR   (AV_CH_SIDE_LEFT            | AV_CH_SIDE_RIGHT)
579 #define CH_DIRECT_PAIR (AV_CH_SURROUND_DIRECT_LEFT | AV_CH_SURROUND_DIRECT_RIGHT)
580 #define CH_BACK_PAIR   (AV_CH_BACK_LEFT            | AV_CH_BACK_RIGHT)
581
582 /* allowable substitutions for channel pairs when comparing layouts,
583  * ordered by priority for both values */
584 static const uint64_t ch_subst[][2] = {
585     { CH_FRONT_PAIR,      CH_CENTER_PAIR     },
586     { CH_FRONT_PAIR,      CH_WIDE_PAIR       },
587     { CH_FRONT_PAIR,      AV_CH_FRONT_CENTER },
588     { CH_CENTER_PAIR,     CH_FRONT_PAIR      },
589     { CH_CENTER_PAIR,     CH_WIDE_PAIR       },
590     { CH_CENTER_PAIR,     AV_CH_FRONT_CENTER },
591     { CH_WIDE_PAIR,       CH_FRONT_PAIR      },
592     { CH_WIDE_PAIR,       CH_CENTER_PAIR     },
593     { CH_WIDE_PAIR,       AV_CH_FRONT_CENTER },
594     { AV_CH_FRONT_CENTER, CH_FRONT_PAIR      },
595     { AV_CH_FRONT_CENTER, CH_CENTER_PAIR     },
596     { AV_CH_FRONT_CENTER, CH_WIDE_PAIR       },
597     { CH_SIDE_PAIR,       CH_DIRECT_PAIR     },
598     { CH_SIDE_PAIR,       CH_BACK_PAIR       },
599     { CH_SIDE_PAIR,       AV_CH_BACK_CENTER  },
600     { CH_BACK_PAIR,       CH_DIRECT_PAIR     },
601     { CH_BACK_PAIR,       CH_SIDE_PAIR       },
602     { CH_BACK_PAIR,       AV_CH_BACK_CENTER  },
603     { AV_CH_BACK_CENTER,  CH_BACK_PAIR       },
604     { AV_CH_BACK_CENTER,  CH_DIRECT_PAIR     },
605     { AV_CH_BACK_CENTER,  CH_SIDE_PAIR       },
606 };
607
608 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
609 {
610     AVFilterLink *link = NULL;
611     int i, j, k;
612
613     for (i = 0; i < filter->nb_inputs; i++) {
614         link = filter->inputs[i];
615
616         if (link->type == AVMEDIA_TYPE_AUDIO &&
617             link->out_channel_layouts->nb_channel_layouts == 1)
618             break;
619     }
620     if (i == filter->nb_inputs)
621         return;
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, best_count_diff = INT_MAX;
626
627         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
628             outlink->in_channel_layouts->nb_channel_layouts < 2)
629             continue;
630
631         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
632             uint64_t  in_chlayout = link->out_channel_layouts->channel_layouts[0];
633             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
634             int  in_channels      = av_get_channel_layout_nb_channels(in_chlayout);
635             int out_channels      = av_get_channel_layout_nb_channels(out_chlayout);
636             int count_diff        = out_channels - in_channels;
637             int matched_channels, extra_channels;
638             int score = 100000;
639
640             if (FF_LAYOUT2COUNT(in_chlayout) || FF_LAYOUT2COUNT(out_chlayout)) {
641                 /* Compute score in case the input or output layout encodes
642                    a channel count; in this case the score is not altered by
643                    the computation afterwards, as in_chlayout and
644                    out_chlayout have both been set to 0 */
645                 if (FF_LAYOUT2COUNT(in_chlayout))
646                     in_channels = FF_LAYOUT2COUNT(in_chlayout);
647                 if (FF_LAYOUT2COUNT(out_chlayout))
648                     out_channels = FF_LAYOUT2COUNT(out_chlayout);
649                 score -= 10000 + FFABS(out_channels - in_channels) +
650                          (in_channels > out_channels ? 10000 : 0);
651                 in_chlayout = out_chlayout = 0;
652                 /* Let the remaining computation run, even if the score
653                    value is not altered */
654             }
655
656             /* channel substitution */
657             for (k = 0; k < FF_ARRAY_ELEMS(ch_subst); k++) {
658                 uint64_t cmp0 = ch_subst[k][0];
659                 uint64_t cmp1 = ch_subst[k][1];
660                 if (( in_chlayout & cmp0) && (!(out_chlayout & cmp0)) &&
661                     (out_chlayout & cmp1) && (!( in_chlayout & cmp1))) {
662                     in_chlayout  &= ~cmp0;
663                     out_chlayout &= ~cmp1;
664                     /* add score for channel match, minus a deduction for
665                        having to do the substitution */
666                     score += 10 * av_get_channel_layout_nb_channels(cmp1) - 2;
667                 }
668             }
669
670             /* no penalty for LFE channel mismatch */
671             if ( (in_chlayout & AV_CH_LOW_FREQUENCY) &&
672                 (out_chlayout & AV_CH_LOW_FREQUENCY))
673                 score += 10;
674             in_chlayout  &= ~AV_CH_LOW_FREQUENCY;
675             out_chlayout &= ~AV_CH_LOW_FREQUENCY;
676
677             matched_channels = av_get_channel_layout_nb_channels(in_chlayout &
678                                                                  out_chlayout);
679             extra_channels   = av_get_channel_layout_nb_channels(out_chlayout &
680                                                                  (~in_chlayout));
681             score += 10 * matched_channels - 5 * extra_channels;
682
683             if (score > best_score ||
684                 (count_diff < best_count_diff && score == best_score)) {
685                 best_score = score;
686                 best_idx   = j;
687                 best_count_diff = count_diff;
688             }
689         }
690         av_assert0(best_idx >= 0);
691         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
692                outlink->in_channel_layouts->channel_layouts[best_idx]);
693     }
694
695 }
696
697 static void swap_channel_layouts(AVFilterGraph *graph)
698 {
699     int i;
700
701     for (i = 0; i < graph->filter_count; i++)
702         swap_channel_layouts_on_filter(graph->filters[i]);
703 }
704
705 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
706 {
707     AVFilterLink *link = NULL;
708     int format, bps;
709     int i, j;
710
711     for (i = 0; i < filter->nb_inputs; i++) {
712         link = filter->inputs[i];
713
714         if (link->type == AVMEDIA_TYPE_AUDIO &&
715             link->out_formats->format_count == 1)
716             break;
717     }
718     if (i == filter->nb_inputs)
719         return;
720
721     format = link->out_formats->formats[0];
722     bps    = av_get_bytes_per_sample(format);
723
724     for (i = 0; i < filter->nb_outputs; i++) {
725         AVFilterLink *outlink = filter->outputs[i];
726         int best_idx = -1, best_score = INT_MIN;
727
728         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
729             outlink->in_formats->format_count < 2)
730             continue;
731
732         for (j = 0; j < outlink->in_formats->format_count; j++) {
733             int out_format = outlink->in_formats->formats[j];
734             int out_bps    = av_get_bytes_per_sample(out_format);
735             int score;
736
737             if (av_get_packed_sample_fmt(out_format) == format ||
738                 av_get_planar_sample_fmt(out_format) == format) {
739                 best_idx   = j;
740                 break;
741             }
742
743             /* for s32 and float prefer double to prevent loss of information */
744             if (bps == 4 && out_bps == 8) {
745                 best_idx = j;
746                 break;
747             }
748
749             /* prefer closest higher or equal bps */
750             score = -abs(out_bps - bps);
751             if (out_bps >= bps)
752                 score += INT_MAX/2;
753
754             if (score > best_score) {
755                 best_score = score;
756                 best_idx   = j;
757             }
758         }
759         av_assert0(best_idx >= 0);
760         FFSWAP(int, outlink->in_formats->formats[0],
761                outlink->in_formats->formats[best_idx]);
762     }
763 }
764
765 static void swap_sample_fmts(AVFilterGraph *graph)
766 {
767     int i;
768
769     for (i = 0; i < graph->filter_count; i++)
770         swap_sample_fmts_on_filter(graph->filters[i]);
771
772 }
773
774 static int pick_formats(AVFilterGraph *graph)
775 {
776     int i, j, ret;
777     int change;
778
779     do{
780         change = 0;
781         for (i = 0; i < graph->filter_count; i++) {
782             AVFilterContext *filter = graph->filters[i];
783             if (filter->nb_inputs){
784                 for (j = 0; j < filter->nb_inputs; j++){
785                     if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
786                         if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
787                             return ret;
788                         change = 1;
789                     }
790                 }
791             }
792             if (filter->nb_outputs){
793                 for (j = 0; j < filter->nb_outputs; j++){
794                     if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
795                         if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
796                             return ret;
797                         change = 1;
798                     }
799                 }
800             }
801             if (filter->nb_inputs && filter->nb_outputs && filter->inputs[0]->format>=0) {
802                 for (j = 0; j < filter->nb_outputs; j++) {
803                     if(filter->outputs[j]->format<0) {
804                         if ((ret = pick_format(filter->outputs[j], filter->inputs[0])) < 0)
805                             return ret;
806                         change = 1;
807                     }
808                 }
809             }
810         }
811     }while(change);
812
813     for (i = 0; i < graph->filter_count; i++) {
814         AVFilterContext *filter = graph->filters[i];
815
816         for (j = 0; j < filter->nb_inputs; j++)
817             if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
818                 return ret;
819         for (j = 0; j < filter->nb_outputs; j++)
820             if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
821                 return ret;
822     }
823     return 0;
824 }
825
826 /**
827  * Configure the formats of all the links in the graph.
828  */
829 static int graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
830 {
831     int ret;
832
833     /* find supported formats from sub-filters, and merge along links */
834     if ((ret = query_formats(graph, log_ctx)) < 0)
835         return ret;
836
837     /* Once everything is merged, it's possible that we'll still have
838      * multiple valid media format choices. We try to minimize the amount
839      * of format conversion inside filters */
840     reduce_formats(graph);
841
842     /* for audio filters, ensure the best format, sample rate and channel layout
843      * is selected */
844     swap_sample_fmts(graph);
845     swap_samplerates(graph);
846     swap_channel_layouts(graph);
847
848     if ((ret = pick_formats(graph)) < 0)
849         return ret;
850
851     return 0;
852 }
853
854 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
855                                              AVClass *log_ctx)
856 {
857     unsigned i, j;
858     int sink_links_count = 0, n = 0;
859     AVFilterContext *f;
860     AVFilterLink **sinks;
861
862     for (i = 0; i < graph->filter_count; i++) {
863         f = graph->filters[i];
864         for (j = 0; j < f->nb_inputs; j++) {
865             f->inputs[j]->graph     = graph;
866             f->inputs[j]->age_index = -1;
867         }
868         for (j = 0; j < f->nb_outputs; j++) {
869             f->outputs[j]->graph    = graph;
870             f->outputs[j]->age_index= -1;
871         }
872         if (!f->nb_outputs) {
873             if (f->nb_inputs > INT_MAX - sink_links_count)
874                 return AVERROR(EINVAL);
875             sink_links_count += f->nb_inputs;
876         }
877     }
878     sinks = av_calloc(sink_links_count, sizeof(*sinks));
879     if (!sinks)
880         return AVERROR(ENOMEM);
881     for (i = 0; i < graph->filter_count; i++) {
882         f = graph->filters[i];
883         if (!f->nb_outputs) {
884             for (j = 0; j < f->nb_inputs; j++) {
885                 sinks[n] = f->inputs[j];
886                 f->inputs[j]->age_index = n++;
887             }
888         }
889     }
890     av_assert0(n == sink_links_count);
891     graph->sink_links       = sinks;
892     graph->sink_links_count = sink_links_count;
893     return 0;
894 }
895
896 static int graph_insert_fifos(AVFilterGraph *graph, AVClass *log_ctx)
897 {
898     AVFilterContext *f;
899     int i, j, ret;
900     int fifo_count = 0;
901
902     for (i = 0; i < graph->filter_count; i++) {
903         f = graph->filters[i];
904
905         for (j = 0; j < f->nb_inputs; j++) {
906             AVFilterLink *link = f->inputs[j];
907             AVFilterContext *fifo_ctx;
908             AVFilter *fifo;
909             char name[32];
910
911             if (!link->dstpad->needs_fifo)
912                 continue;
913
914             fifo = f->inputs[j]->type == AVMEDIA_TYPE_VIDEO ?
915                    avfilter_get_by_name("fifo") :
916                    avfilter_get_by_name("afifo");
917
918             snprintf(name, sizeof(name), "auto-inserted fifo %d", fifo_count++);
919
920             ret = avfilter_graph_create_filter(&fifo_ctx, fifo, name, NULL,
921                                                NULL, graph);
922             if (ret < 0)
923                 return ret;
924
925             ret = avfilter_insert_filter(link, fifo_ctx, 0, 0);
926             if (ret < 0)
927                 return ret;
928         }
929     }
930
931     return 0;
932 }
933
934 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
935 {
936     int ret;
937
938     if ((ret = graph_check_validity(graphctx, log_ctx)))
939         return ret;
940     if ((ret = graph_insert_fifos(graphctx, log_ctx)) < 0)
941         return ret;
942     if ((ret = graph_config_formats(graphctx, log_ctx)))
943         return ret;
944     if ((ret = graph_config_links(graphctx, log_ctx)))
945         return ret;
946     if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
947         return ret;
948
949     return 0;
950 }
951
952 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
953 {
954     int i, r = AVERROR(ENOSYS);
955
956     if(!graph)
957         return r;
958
959     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
960         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
961         if(r != AVERROR(ENOSYS))
962             return r;
963     }
964
965     if(res_len && res)
966         res[0]= 0;
967
968     for (i = 0; i < graph->filter_count; i++) {
969         AVFilterContext *filter = graph->filters[i];
970         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
971             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
972             if(r != AVERROR(ENOSYS)) {
973                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
974                     return r;
975             }
976         }
977     }
978
979     return r;
980 }
981
982 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
983 {
984     int i;
985
986     if(!graph)
987         return 0;
988
989     for (i = 0; i < graph->filter_count; i++) {
990         AVFilterContext *filter = graph->filters[i];
991         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
992             AVFilterCommand **queue = &filter->command_queue, *next;
993             while (*queue && (*queue)->time <= ts)
994                 queue = &(*queue)->next;
995             next = *queue;
996             *queue = av_mallocz(sizeof(AVFilterCommand));
997             (*queue)->command = av_strdup(command);
998             (*queue)->arg     = av_strdup(arg);
999             (*queue)->time    = ts;
1000             (*queue)->flags   = flags;
1001             (*queue)->next    = next;
1002             if(flags & AVFILTER_CMD_FLAG_ONE)
1003                 return 0;
1004         }
1005     }
1006
1007     return 0;
1008 }
1009
1010 static void heap_bubble_up(AVFilterGraph *graph,
1011                            AVFilterLink *link, int index)
1012 {
1013     AVFilterLink **links = graph->sink_links;
1014
1015     while (index) {
1016         int parent = (index - 1) >> 1;
1017         if (links[parent]->current_pts >= link->current_pts)
1018             break;
1019         links[index] = links[parent];
1020         links[index]->age_index = index;
1021         index = parent;
1022     }
1023     links[index] = link;
1024     link->age_index = index;
1025 }
1026
1027 static void heap_bubble_down(AVFilterGraph *graph,
1028                              AVFilterLink *link, int index)
1029 {
1030     AVFilterLink **links = graph->sink_links;
1031
1032     while (1) {
1033         int child = 2 * index + 1;
1034         if (child >= graph->sink_links_count)
1035             break;
1036         if (child + 1 < graph->sink_links_count &&
1037             links[child + 1]->current_pts < links[child]->current_pts)
1038             child++;
1039         if (link->current_pts < links[child]->current_pts)
1040             break;
1041         links[index] = links[child];
1042         links[index]->age_index = index;
1043         index = child;
1044     }
1045     links[index] = link;
1046     link->age_index = index;
1047 }
1048
1049 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
1050 {
1051     heap_bubble_up  (graph, link, link->age_index);
1052     heap_bubble_down(graph, link, link->age_index);
1053 }
1054
1055
1056 int avfilter_graph_request_oldest(AVFilterGraph *graph)
1057 {
1058     while (graph->sink_links_count) {
1059         AVFilterLink *oldest = graph->sink_links[0];
1060         int r = ff_request_frame(oldest);
1061         if (r != AVERROR_EOF)
1062             return r;
1063         av_log(oldest->dst, AV_LOG_DEBUG, "EOF on sink link %s:%s.\n",
1064                oldest->dst ? oldest->dst->name : "unknown",
1065                oldest->dstpad ? oldest->dstpad->name : "unknown");
1066         /* EOF: remove the link from the heap */
1067         if (oldest->age_index < --graph->sink_links_count)
1068             heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
1069                              oldest->age_index);
1070         oldest->age_index = -1;
1071     }
1072     return AVERROR_EOF;
1073 }