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