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