]> git.sesse.net Git - ffmpeg/blob - libavfilter/avfiltergraph.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / avfiltergraph.c
1 /*
2  * filter graphs
3  * Copyright (c) 2008 Vitor Sessak
4  * Copyright (c) 2007 Bobby Bingham
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include <ctype.h>
24 #include <string.h>
25
26 #include "libavutil/audioconvert.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "avfiltergraph.h"
31 #include "formats.h"
32 #include "internal.h"
33
34 #include "libavutil/audioconvert.h"
35 #include "libavutil/log.h"
36
37 static const AVClass filtergraph_class = {
38     .class_name = "AVFilterGraph",
39     .item_name  = av_default_item_name,
40     .version    = LIBAVUTIL_VERSION_INT,
41 };
42
43 AVFilterGraph *avfilter_graph_alloc(void)
44 {
45     AVFilterGraph *ret = av_mallocz(sizeof(AVFilterGraph));
46     if (!ret)
47         return NULL;
48 #if FF_API_GRAPH_AVCLASS
49     ret->av_class = &filtergraph_class;
50 #endif
51     return ret;
52 }
53
54 void avfilter_graph_free(AVFilterGraph **graph)
55 {
56     if (!*graph)
57         return;
58     for (; (*graph)->filter_count > 0; (*graph)->filter_count--)
59         avfilter_free((*graph)->filters[(*graph)->filter_count - 1]);
60     av_freep(&(*graph)->sink_links);
61     av_freep(&(*graph)->scale_sws_opts);
62     av_freep(&(*graph)->filters);
63     av_freep(graph);
64 }
65
66 int avfilter_graph_add_filter(AVFilterGraph *graph, AVFilterContext *filter)
67 {
68     AVFilterContext **filters = av_realloc(graph->filters,
69                                            sizeof(AVFilterContext*) * (graph->filter_count+1));
70     if (!filters)
71         return AVERROR(ENOMEM);
72
73     graph->filters = filters;
74     graph->filters[graph->filter_count++] = filter;
75
76     return 0;
77 }
78
79 int avfilter_graph_create_filter(AVFilterContext **filt_ctx, AVFilter *filt,
80                                  const char *name, const char *args, void *opaque,
81                                  AVFilterGraph *graph_ctx)
82 {
83     int ret;
84
85     if ((ret = avfilter_open(filt_ctx, filt, name)) < 0)
86         goto fail;
87     if ((ret = avfilter_init_filter(*filt_ctx, args, opaque)) < 0)
88         goto fail;
89     if ((ret = avfilter_graph_add_filter(graph_ctx, *filt_ctx)) < 0)
90         goto fail;
91     return 0;
92
93 fail:
94     if (*filt_ctx)
95         avfilter_free(*filt_ctx);
96     *filt_ctx = NULL;
97     return ret;
98 }
99
100 void avfilter_graph_set_auto_convert(AVFilterGraph *graph, unsigned flags)
101 {
102     graph->disable_auto_convert = flags;
103 }
104
105 int ff_avfilter_graph_check_validity(AVFilterGraph *graph, AVClass *log_ctx)
106 {
107     AVFilterContext *filt;
108     int i, j;
109
110     for (i = 0; i < graph->filter_count; i++) {
111         filt = graph->filters[i];
112
113         for (j = 0; j < filt->input_count; j++) {
114             if (!filt->inputs[j] || !filt->inputs[j]->src) {
115                 av_log(log_ctx, AV_LOG_ERROR,
116                        "Input pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any source\n",
117                        filt->input_pads[j].name, filt->name, filt->filter->name);
118                 return AVERROR(EINVAL);
119             }
120         }
121
122         for (j = 0; j < filt->output_count; j++) {
123             if (!filt->outputs[j] || !filt->outputs[j]->dst) {
124                 av_log(log_ctx, AV_LOG_ERROR,
125                        "Output pad \"%s\" for the filter \"%s\" of type \"%s\" not connected to any destination\n",
126                        filt->output_pads[j].name, filt->name, filt->filter->name);
127                 return AVERROR(EINVAL);
128             }
129         }
130     }
131
132     return 0;
133 }
134
135 int ff_avfilter_graph_config_links(AVFilterGraph *graph, AVClass *log_ctx)
136 {
137     AVFilterContext *filt;
138     int i, ret;
139
140     for (i=0; i < graph->filter_count; i++) {
141         filt = graph->filters[i];
142
143         if (!filt->output_count) {
144             if ((ret = avfilter_config_links(filt)))
145                 return ret;
146         }
147     }
148
149     return 0;
150 }
151
152 AVFilterContext *avfilter_graph_get_filter(AVFilterGraph *graph, char *name)
153 {
154     int i;
155
156     for (i = 0; i < graph->filter_count; i++)
157         if (graph->filters[i]->name && !strcmp(name, graph->filters[i]->name))
158             return graph->filters[i];
159
160     return NULL;
161 }
162
163 static int insert_conv_filter(AVFilterGraph *graph, AVFilterLink *link,
164                               const char *filt_name, const char *filt_args)
165 {
166     static int auto_count = 0, ret;
167     char inst_name[32];
168     AVFilterContext *filt_ctx;
169
170     if (graph->disable_auto_convert) {
171         av_log(NULL, AV_LOG_ERROR,
172                "The filters '%s' and '%s' do not have a common format "
173                "and automatic conversion is disabled.\n",
174                link->src->name, link->dst->name);
175         return AVERROR(EINVAL);
176     }
177
178     snprintf(inst_name, sizeof(inst_name), "auto-inserted %s %d",
179             filt_name, auto_count++);
180
181     if ((ret = avfilter_graph_create_filter(&filt_ctx,
182                                             avfilter_get_by_name(filt_name),
183                                             inst_name, filt_args, NULL, graph)) < 0)
184         return ret;
185     if ((ret = avfilter_insert_filter(link, filt_ctx, 0, 0)) < 0)
186         return ret;
187
188     filt_ctx->filter->query_formats(filt_ctx);
189
190     if ( ((link = filt_ctx-> inputs[0]) &&
191            !avfilter_merge_formats(link->in_formats, link->out_formats)) ||
192          ((link = filt_ctx->outputs[0]) &&
193            !avfilter_merge_formats(link->in_formats, link->out_formats))
194        ) {
195         av_log(NULL, AV_LOG_ERROR,
196                "Impossible to convert between the formats supported by the filter "
197                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
198         return AVERROR(EINVAL);
199     }
200
201     if (link->type == AVMEDIA_TYPE_AUDIO &&
202          (((link = filt_ctx-> inputs[0]) &&
203            !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)) ||
204          ((link = filt_ctx->outputs[0]) &&
205            !ff_merge_channel_layouts(link->in_channel_layouts, link->out_channel_layouts)))
206        ) {
207         av_log(NULL, AV_LOG_ERROR,
208                "Impossible to convert between the channel layouts formats supported by the filter "
209                "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
210         return AVERROR(EINVAL);
211     }
212
213     return 0;
214 }
215
216 static int query_formats(AVFilterGraph *graph, AVClass *log_ctx)
217 {
218     int i, j, ret;
219     char filt_args[128];
220     AVFilterFormats *formats;
221     AVFilterChannelLayouts *chlayouts;
222     AVFilterFormats *samplerates;
223     int scaler_count = 0, resampler_count = 0;
224
225     /* ask all the sub-filters for their supported media formats */
226     for (i = 0; i < graph->filter_count; i++) {
227         if (graph->filters[i]->filter->query_formats)
228             graph->filters[i]->filter->query_formats(graph->filters[i]);
229         else
230             ff_default_query_formats(graph->filters[i]);
231     }
232
233     /* go through and merge as many format lists as possible */
234     for (i = 0; i < graph->filter_count; i++) {
235         AVFilterContext *filter = graph->filters[i];
236
237         for (j = 0; j < filter->input_count; j++) {
238             AVFilterLink *link = filter->inputs[j];
239 #if 0
240             if (!link) continue;
241
242             if (!link->in_formats || !link->out_formats)
243                 return AVERROR(EINVAL);
244
245             if (link->type == AVMEDIA_TYPE_VIDEO &&
246                 !avfilter_merge_formats(link->in_formats, link->out_formats)) {
247
248                 /* couldn't merge format lists, auto-insert scale filter */
249                 snprintf(filt_args, sizeof(filt_args), "0:0:%s",
250                          graph->scale_sws_opts);
251                 if (ret = insert_conv_filter(graph, link, "scale", filt_args))
252                     return ret;
253             }
254             else if (link->type == AVMEDIA_TYPE_AUDIO) {
255                 if (!link->in_channel_layouts || !link->out_channel_layouts)
256                     return AVERROR(EINVAL);
257
258                 /* Merge all three list before checking: that way, in all
259                  * three categories, aconvert will use a common format
260                  * whenever possible. */
261                 formats   = avfilter_merge_formats(link->in_formats,   link->out_formats);
262                 chlayouts   = ff_merge_channel_layouts(link->in_channel_layouts  , link->out_channel_layouts);
263                 samplerates = ff_merge_samplerates    (link->in_samplerates, link->out_samplerates);
264
265                 if (!formats || !chlayouts || !samplerates)
266                     if (ret = insert_conv_filter(graph, link, "aresample", NULL))
267                        return ret;
268 #else
269             int convert_needed = 0;
270
271             if (!link)
272                 continue;
273
274             if (link->in_formats != link->out_formats &&
275                 !avfilter_merge_formats(link->in_formats,
276                                         link->out_formats))
277                 convert_needed = 1;
278             if (link->type == AVMEDIA_TYPE_AUDIO) {
279                 if (link->in_channel_layouts != link->out_channel_layouts &&
280                     !ff_merge_channel_layouts(link->in_channel_layouts,
281                                               link->out_channel_layouts))
282                     convert_needed = 1;
283                 if (link->in_samplerates != link->out_samplerates &&
284                     !ff_merge_samplerates(link->in_samplerates,
285                                           link->out_samplerates))
286                     convert_needed = 1;
287             }
288
289             if (convert_needed) {
290                 AVFilterContext *convert;
291                 AVFilter *filter;
292                 AVFilterLink *inlink, *outlink;
293                 char scale_args[256];
294                 char inst_name[30];
295
296                 /* couldn't merge format lists. auto-insert conversion filter */
297                 switch (link->type) {
298                 case AVMEDIA_TYPE_VIDEO:
299                     snprintf(inst_name, sizeof(inst_name), "auto-inserted scaler %d",
300                              scaler_count++);
301                     snprintf(scale_args, sizeof(scale_args), "0:0:%s", graph->scale_sws_opts);
302                     if ((ret = avfilter_graph_create_filter(&convert,
303                                                             avfilter_get_by_name("scale"),
304                                                             inst_name, scale_args, NULL,
305                                                             graph)) < 0)
306                         return ret;
307                     break;
308                 case AVMEDIA_TYPE_AUDIO:
309                     if (!(filter = avfilter_get_by_name("aresample"))) {
310                         av_log(log_ctx, AV_LOG_ERROR, "'resample' filter "
311                                "not present, cannot convert audio formats.\n");
312                         return AVERROR(EINVAL);
313                     }
314
315                     snprintf(inst_name, sizeof(inst_name), "auto-inserted resampler %d",
316                              resampler_count++);
317                     if ((ret = avfilter_graph_create_filter(&convert,
318                                                             avfilter_get_by_name("aresample"),
319                                                             inst_name, NULL, NULL, graph)) < 0)
320                         return ret;
321                     break;
322                 default:
323                     return AVERROR(EINVAL);
324                 }
325
326                 if ((ret = avfilter_insert_filter(link, convert, 0, 0)) < 0)
327                     return ret;
328
329                 convert->filter->query_formats(convert);
330                 inlink  = convert->inputs[0];
331                 outlink = convert->outputs[0];
332                 if (!avfilter_merge_formats( inlink->in_formats,  inlink->out_formats) ||
333                     !avfilter_merge_formats(outlink->in_formats, outlink->out_formats))
334                     ret |= AVERROR(ENOSYS);
335                 if (inlink->type == AVMEDIA_TYPE_AUDIO &&
336                     (!ff_merge_samplerates(inlink->in_samplerates,
337                                            inlink->out_samplerates) ||
338                      !ff_merge_channel_layouts(inlink->in_channel_layouts,
339                                                inlink->out_channel_layouts)))
340                     ret |= AVERROR(ENOSYS);
341                 if (outlink->type == AVMEDIA_TYPE_AUDIO &&
342                     (!ff_merge_samplerates(outlink->in_samplerates,
343                                            outlink->out_samplerates) ||
344                      !ff_merge_channel_layouts(outlink->in_channel_layouts,
345                                                outlink->out_channel_layouts)))
346                     ret |= AVERROR(ENOSYS);
347
348                 if (ret < 0) {
349                     av_log(log_ctx, AV_LOG_ERROR,
350                            "Impossible to convert between the formats supported by the filter "
351                            "'%s' and the filter '%s'\n", link->src->name, link->dst->name);
352                     return ret;
353                 }
354 #endif
355             }
356         }
357     }
358
359     return 0;
360 }
361
362 static int pick_format(AVFilterLink *link, AVFilterLink *ref)
363 {
364     if (!link || !link->in_formats)
365         return 0;
366
367     if (link->type == AVMEDIA_TYPE_VIDEO) {
368         if(ref && ref->type == AVMEDIA_TYPE_VIDEO){
369             int has_alpha= av_pix_fmt_descriptors[ref->format].nb_components % 2 == 0;
370             enum PixelFormat best= PIX_FMT_NONE;
371             int i;
372             for (i=0; i<link->in_formats->format_count; i++) {
373                 enum PixelFormat p = link->in_formats->formats[i];
374                 best= avcodec_find_best_pix_fmt2(best, p, ref->format, has_alpha, NULL);
375             }
376             link->in_formats->formats[0] = best;
377         }
378     }
379
380     link->in_formats->format_count = 1;
381     link->format = link->in_formats->formats[0];
382
383     if (link->type == AVMEDIA_TYPE_AUDIO) {
384         if (!link->in_samplerates->format_count) {
385             av_log(link->src, AV_LOG_ERROR, "Cannot select sample rate for"
386                    " the link between filters %s and %s.\n", link->src->name,
387                    link->dst->name);
388             return AVERROR(EINVAL);
389         }
390         link->in_samplerates->format_count = 1;
391         link->sample_rate = link->in_samplerates->formats[0];
392
393         if (!link->in_channel_layouts->nb_channel_layouts) {
394             av_log(link->src, AV_LOG_ERROR, "Cannot select channel layout for"
395                    "the link between filters %s and %s.\n", link->src->name,
396                    link->dst->name);
397             return AVERROR(EINVAL);
398         }
399         link->in_channel_layouts->nb_channel_layouts = 1;
400         link->channel_layout = link->in_channel_layouts->channel_layouts[0];
401     }
402
403     avfilter_formats_unref(&link->in_formats);
404     avfilter_formats_unref(&link->out_formats);
405     avfilter_formats_unref(&link->in_samplerates);
406     avfilter_formats_unref(&link->out_samplerates);
407     ff_channel_layouts_unref(&link->in_channel_layouts);
408     ff_channel_layouts_unref(&link->out_channel_layouts);
409
410     return 0;
411 }
412
413 #define REDUCE_FORMATS(fmt_type, list_type, list, var, nb, add_format) \
414 do {                                                                   \
415     for (i = 0; i < filter->input_count; i++) {                        \
416         AVFilterLink *link = filter->inputs[i];                        \
417         fmt_type fmt;                                                  \
418                                                                        \
419         if (!link->out_ ## list || link->out_ ## list->nb != 1)        \
420             continue;                                                  \
421         fmt = link->out_ ## list->var[0];                              \
422                                                                        \
423         for (j = 0; j < filter->output_count; j++) {                   \
424             AVFilterLink *out_link = filter->outputs[j];               \
425             list_type *fmts;                                           \
426                                                                        \
427             if (link->type != out_link->type ||                        \
428                 out_link->in_ ## list->nb == 1)                        \
429                 continue;                                              \
430             fmts = out_link->in_ ## list;                              \
431                                                                        \
432             if (!out_link->in_ ## list->nb) {                          \
433                 add_format(&out_link->in_ ##list, fmt);                \
434                 break;                                                 \
435             }                                                          \
436                                                                        \
437             for (k = 0; k < out_link->in_ ## list->nb; k++)            \
438                 if (fmts->var[k] == fmt) {                             \
439                     fmts->var[0]  = fmt;                               \
440                     fmts->nb = 1;                                      \
441                     ret = 1;                                           \
442                     break;                                             \
443                 }                                                      \
444         }                                                              \
445     }                                                                  \
446 } while (0)
447
448 static int reduce_formats_on_filter(AVFilterContext *filter)
449 {
450     int i, j, k, ret = 0;
451
452     REDUCE_FORMATS(int,      AVFilterFormats,        formats,         formats,
453                    format_count, avfilter_add_format);
454     REDUCE_FORMATS(int,      AVFilterFormats,        samplerates,     formats,
455                    format_count, avfilter_add_format);
456     REDUCE_FORMATS(uint64_t, AVFilterChannelLayouts, channel_layouts,
457                    channel_layouts, nb_channel_layouts, ff_add_channel_layout);
458
459     return ret;
460 }
461
462 static void reduce_formats(AVFilterGraph *graph)
463 {
464     int i, reduced;
465
466     do {
467         reduced = 0;
468
469         for (i = 0; i < graph->filter_count; i++)
470             reduced |= reduce_formats_on_filter(graph->filters[i]);
471     } while (reduced);
472 }
473
474 static void swap_samplerates_on_filter(AVFilterContext *filter)
475 {
476     AVFilterLink *link = NULL;
477     int sample_rate;
478     int i, j;
479
480     for (i = 0; i < filter->input_count; i++) {
481         link = filter->inputs[i];
482
483         if (link->type == AVMEDIA_TYPE_AUDIO &&
484             link->out_samplerates->format_count == 1)
485             break;
486     }
487     if (i == filter->input_count)
488         return;
489
490     sample_rate = link->out_samplerates->formats[0];
491
492     for (i = 0; i < filter->output_count; i++) {
493         AVFilterLink *outlink = filter->outputs[i];
494         int best_idx, best_diff = INT_MAX;
495
496         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
497             outlink->in_samplerates->format_count < 2)
498             continue;
499
500         for (j = 0; j < outlink->in_samplerates->format_count; j++) {
501             int diff = abs(sample_rate - outlink->in_samplerates->formats[j]);
502
503             if (diff < best_diff) {
504                 best_diff = diff;
505                 best_idx  = j;
506             }
507         }
508         FFSWAP(int, outlink->in_samplerates->formats[0],
509                outlink->in_samplerates->formats[best_idx]);
510     }
511 }
512
513 static void swap_samplerates(AVFilterGraph *graph)
514 {
515     int i;
516
517     for (i = 0; i < graph->filter_count; i++)
518         swap_samplerates_on_filter(graph->filters[i]);
519 }
520
521 static void swap_channel_layouts_on_filter(AVFilterContext *filter)
522 {
523     AVFilterLink *link = NULL;
524     uint64_t chlayout;
525     int i, j;
526
527     for (i = 0; i < filter->input_count; i++) {
528         link = filter->inputs[i];
529
530         if (link->type == AVMEDIA_TYPE_AUDIO &&
531             link->out_channel_layouts->nb_channel_layouts == 1)
532             break;
533     }
534     if (i == filter->input_count)
535         return;
536
537     chlayout = link->out_channel_layouts->channel_layouts[0];
538
539     for (i = 0; i < filter->output_count; i++) {
540         AVFilterLink *outlink = filter->outputs[i];
541         int best_idx, best_score = INT_MIN;
542
543         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
544             outlink->in_channel_layouts->nb_channel_layouts < 2)
545             continue;
546
547         for (j = 0; j < outlink->in_channel_layouts->nb_channel_layouts; j++) {
548             uint64_t out_chlayout = outlink->in_channel_layouts->channel_layouts[j];
549             int matched_channels  = av_get_channel_layout_nb_channels(chlayout &
550                                                                       out_chlayout);
551             int extra_channels     = av_get_channel_layout_nb_channels(out_chlayout &
552                                                                        (~chlayout));
553             int score = matched_channels - extra_channels;
554
555             if (score > best_score) {
556                 best_score = score;
557                 best_idx   = j;
558             }
559         }
560         FFSWAP(uint64_t, outlink->in_channel_layouts->channel_layouts[0],
561                outlink->in_channel_layouts->channel_layouts[best_idx]);
562     }
563
564 }
565
566 static void swap_channel_layouts(AVFilterGraph *graph)
567 {
568     int i;
569
570     for (i = 0; i < graph->filter_count; i++)
571         swap_channel_layouts_on_filter(graph->filters[i]);
572 }
573
574 static void swap_sample_fmts_on_filter(AVFilterContext *filter)
575 {
576     AVFilterLink *link = NULL;
577     int format, bps;
578     int i, j;
579
580     for (i = 0; i < filter->input_count; i++) {
581         link = filter->inputs[i];
582
583         if (link->type == AVMEDIA_TYPE_AUDIO &&
584             link->out_formats->format_count == 1)
585             break;
586     }
587     if (i == filter->input_count)
588         return;
589
590     format = link->out_formats->formats[0];
591     bps    = av_get_bytes_per_sample(format);
592
593     for (i = 0; i < filter->output_count; i++) {
594         AVFilterLink *outlink = filter->outputs[i];
595         int best_idx, best_score = INT_MIN;
596
597         if (outlink->type != AVMEDIA_TYPE_AUDIO ||
598             outlink->in_formats->format_count < 2)
599             continue;
600
601         for (j = 0; j < outlink->in_formats->format_count; j++) {
602             int out_format = outlink->in_formats->formats[j];
603             int out_bps    = av_get_bytes_per_sample(out_format);
604             int score;
605
606             if (av_get_packed_sample_fmt(out_format) == format ||
607                 av_get_planar_sample_fmt(out_format) == format) {
608                 best_idx   = j;
609                 break;
610             }
611
612             /* for s32 and float prefer double to prevent loss of information */
613             if (bps == 4 && out_bps == 8) {
614                 best_idx = j;
615                 break;
616             }
617
618             /* prefer closest higher or equal bps */
619             score = -abs(out_bps - bps);
620             if (out_bps >= bps)
621                 score += INT_MAX/2;
622
623             if (score > best_score) {
624                 best_score = score;
625                 best_idx   = j;
626             }
627         }
628         FFSWAP(int, outlink->in_formats->formats[0],
629                outlink->in_formats->formats[best_idx]);
630     }
631 }
632
633 static void swap_sample_fmts(AVFilterGraph *graph)
634 {
635     int i;
636
637     for (i = 0; i < graph->filter_count; i++)
638         swap_sample_fmts_on_filter(graph->filters[i]);
639
640 }
641
642 static int pick_formats(AVFilterGraph *graph)
643 {
644     int i, j, ret;
645     int change;
646
647     do{
648         change = 0;
649         for (i = 0; i < graph->filter_count; i++) {
650             AVFilterContext *filter = graph->filters[i];
651             if (filter->input_count){
652                 for (j = 0; j < filter->input_count; j++){
653                     if(filter->inputs[j]->in_formats && filter->inputs[j]->in_formats->format_count == 1) {
654                         pick_format(filter->inputs[j], NULL);
655                         change = 1;
656                     }
657                 }
658             }
659             if (filter->output_count){
660                 for (j = 0; j < filter->output_count; j++){
661                     if(filter->outputs[j]->in_formats && filter->outputs[j]->in_formats->format_count == 1) {
662                         pick_format(filter->outputs[j], NULL);
663                         change = 1;
664                     }
665                 }
666             }
667             if (filter->input_count && filter->output_count && filter->inputs[0]->format>=0) {
668                 for (j = 0; j < filter->output_count; j++) {
669                     if(filter->outputs[j]->format<0) {
670                         pick_format(filter->outputs[j], filter->inputs[0]);
671                         change = 1;
672                     }
673                 }
674             }
675         }
676     }while(change);
677
678     for (i = 0; i < graph->filter_count; i++) {
679         AVFilterContext *filter = graph->filters[i];
680
681         for (j = 0; j < filter->input_count; j++)
682             if ((ret = pick_format(filter->inputs[j], NULL)) < 0)
683                 return ret;
684         for (j = 0; j < filter->output_count; j++)
685             if ((ret = pick_format(filter->outputs[j], NULL)) < 0)
686                 return ret;
687     }
688     return 0;
689 }
690
691 int ff_avfilter_graph_config_formats(AVFilterGraph *graph, AVClass *log_ctx)
692 {
693     int ret;
694
695     /* find supported formats from sub-filters, and merge along links */
696     if ((ret = query_formats(graph, log_ctx)) < 0)
697         return ret;
698
699     /* Once everything is merged, it's possible that we'll still have
700      * multiple valid media format choices. We try to minimize the amount
701      * of format conversion inside filters */
702     reduce_formats(graph);
703
704     /* for audio filters, ensure the best format, sample rate and channel layout
705      * is selected */
706     swap_sample_fmts(graph);
707     swap_samplerates(graph);
708     swap_channel_layouts(graph);
709
710     if ((ret = pick_formats(graph)) < 0)
711         return ret;
712
713     return 0;
714 }
715
716 static int ff_avfilter_graph_config_pointers(AVFilterGraph *graph,
717                                              AVClass *log_ctx)
718 {
719     unsigned i, j;
720     int sink_links_count = 0, n = 0;
721     AVFilterContext *f;
722     AVFilterLink **sinks;
723
724     for (i = 0; i < graph->filter_count; i++) {
725         f = graph->filters[i];
726         for (j = 0; j < f->input_count; j++) {
727             f->inputs[j]->graph     = graph;
728             f->inputs[j]->age_index = -1;
729         }
730         for (j = 0; j < f->output_count; j++) {
731             f->outputs[j]->graph    = graph;
732             f->outputs[j]->age_index= -1;
733         }
734         if (!f->output_count) {
735             if (f->input_count > INT_MAX - sink_links_count)
736                 return AVERROR(EINVAL);
737             sink_links_count += f->input_count;
738         }
739     }
740     sinks = av_calloc(sink_links_count, sizeof(*sinks));
741     if (!sinks)
742         return AVERROR(ENOMEM);
743     for (i = 0; i < graph->filter_count; i++) {
744         f = graph->filters[i];
745         if (!f->output_count) {
746             for (j = 0; j < f->input_count; j++) {
747                 sinks[n] = f->inputs[j];
748                 f->inputs[j]->age_index = n++;
749             }
750         }
751     }
752     av_assert0(n == sink_links_count);
753     graph->sink_links       = sinks;
754     graph->sink_links_count = sink_links_count;
755     return 0;
756 }
757
758 int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
759 {
760     int ret;
761
762     if ((ret = ff_avfilter_graph_check_validity(graphctx, log_ctx)))
763         return ret;
764     if ((ret = ff_avfilter_graph_config_formats(graphctx, log_ctx)))
765         return ret;
766     if ((ret = ff_avfilter_graph_config_links(graphctx, log_ctx)))
767         return ret;
768     if ((ret = ff_avfilter_graph_config_pointers(graphctx, log_ctx)))
769         return ret;
770
771     return 0;
772 }
773
774 int avfilter_graph_send_command(AVFilterGraph *graph, const char *target, const char *cmd, const char *arg, char *res, int res_len, int flags)
775 {
776     int i, r = AVERROR(ENOSYS);
777
778     if(!graph)
779         return r;
780
781     if((flags & AVFILTER_CMD_FLAG_ONE) && !(flags & AVFILTER_CMD_FLAG_FAST)) {
782         r=avfilter_graph_send_command(graph, target, cmd, arg, res, res_len, flags | AVFILTER_CMD_FLAG_FAST);
783         if(r != AVERROR(ENOSYS))
784             return r;
785     }
786
787     if(res_len && res)
788         res[0]= 0;
789
790     for (i = 0; i < graph->filter_count; i++) {
791         AVFilterContext *filter = graph->filters[i];
792         if(!strcmp(target, "all") || (filter->name && !strcmp(target, filter->name)) || !strcmp(target, filter->filter->name)){
793             r = avfilter_process_command(filter, cmd, arg, res, res_len, flags);
794             if(r != AVERROR(ENOSYS)) {
795                 if((flags & AVFILTER_CMD_FLAG_ONE) || r<0)
796                     return r;
797             }
798         }
799     }
800
801     return r;
802 }
803
804 int avfilter_graph_queue_command(AVFilterGraph *graph, const char *target, const char *command, const char *arg, int flags, double ts)
805 {
806     int i;
807
808     if(!graph)
809         return 0;
810
811     for (i = 0; i < graph->filter_count; i++) {
812         AVFilterContext *filter = graph->filters[i];
813         if(filter && (!strcmp(target, "all") || !strcmp(target, filter->name) || !strcmp(target, filter->filter->name))){
814             AVFilterCommand **que = &filter->command_queue, *next;
815             while(*que && (*que)->time <= ts)
816                 que = &(*que)->next;
817             next= *que;
818             *que= av_mallocz(sizeof(AVFilterCommand));
819             (*que)->command = av_strdup(command);
820             (*que)->arg     = av_strdup(arg);
821             (*que)->time    = ts;
822             (*que)->flags   = flags;
823             (*que)->next    = next;
824             if(flags & AVFILTER_CMD_FLAG_ONE)
825                 return 0;
826         }
827     }
828
829     return 0;
830 }
831
832 static void heap_bubble_up(AVFilterGraph *graph,
833                            AVFilterLink *link, int index)
834 {
835     AVFilterLink **links = graph->sink_links;
836
837     while (index) {
838         int parent = (index - 1) >> 1;
839         if (links[parent]->current_pts >= link->current_pts)
840             break;
841         links[index] = links[parent];
842         links[index]->age_index = index;
843         index = parent;
844     }
845     links[index] = link;
846     link->age_index = index;
847 }
848
849 static void heap_bubble_down(AVFilterGraph *graph,
850                              AVFilterLink *link, int index)
851 {
852     AVFilterLink **links = graph->sink_links;
853
854     while (1) {
855         int child = 2 * index + 1;
856         if (child >= graph->sink_links_count)
857             break;
858         if (child + 1 < graph->sink_links_count &&
859             links[child + 1]->current_pts < links[child]->current_pts)
860             child++;
861         if (link->current_pts < links[child]->current_pts)
862             break;
863         links[index] = links[child];
864         links[index]->age_index = index;
865         index = child;
866     }
867     links[index] = link;
868     link->age_index = index;
869 }
870
871 void ff_avfilter_graph_update_heap(AVFilterGraph *graph, AVFilterLink *link)
872 {
873     heap_bubble_up  (graph, link, link->age_index);
874     heap_bubble_down(graph, link, link->age_index);
875 }
876
877
878 int avfilter_graph_request_oldest(AVFilterGraph *graph)
879 {
880     while (graph->sink_links_count) {
881         AVFilterLink *oldest = graph->sink_links[0];
882         int r = avfilter_request_frame(oldest);
883         if (r != AVERROR_EOF)
884             return r;
885         /* EOF: remove the link from the heap */
886         if (oldest->age_index < --graph->sink_links_count)
887             heap_bubble_down(graph, graph->sink_links[graph->sink_links_count],
888                              oldest->age_index);
889         oldest->age_index = -1;
890     }
891     return AVERROR_EOF;
892 }