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