Browse code

Merge commit '3b266da3d35f3f7a61258b78384dfe920d875d29'

* commit '3b266da3d35f3f7a61258b78384dfe920d875d29':
avconv: add support for complex filtergraphs.
avconv: make filtergraphs global.
avconv: move filtered_frame from InputStream to OutputStream.
avconv: don't set output width/height directly from input value.
avconv: move resample_{width,height,pix_fmt} to InputStream.
avconv: remove a useless variable from OutputStream.
avconv: get output pixel format from lavfi.
graphparser: fix the order in which unlabeled input links are returned.
avconv: change {input,output}_{streams,files} into arrays of pointers.
avconv: don't pass input/output streams to some functions.

Conflicts:
cmdutils.c
cmdutils.h
doc/ffmpeg.texi
ffmpeg.c
ffplay.c

Merged-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2012/04/17 11:01:17
Showing 11 changed files
... ...
@@ -370,6 +370,7 @@ int cmdutils_read_file(const char *filename, char **bufptr, size_t *size);
370 370
 FILE *get_preset_file(char *filename, size_t filename_size,
371 371
                       const char *preset_name, int is_path, const char *codec_name);
372 372
 
373
+
373 374
 /**
374 375
  * Do all the necessary cleanup and abort.
375 376
  * This function is implemented in the avtools, not cmdutils.
... ...
@@ -224,6 +224,9 @@ codec-dependent.
224 224
 @var{filter_graph} is a description of the filter graph to apply to
225 225
 the stream. Use @code{-filters} to show all the available filters
226 226
 (including also sources and sinks).
227
+
228
+See also the @option{-filter_complex} option if you want to create filter graphs
229
+with multiple inputs and/or outputs.
227 230
 @item -pre[:@var{stream_specifier}] @var{preset_name} (@emph{output,per-stream})
228 231
 Specify the preset for matching stream(s).
229 232
 
... ...
@@ -522,7 +525,7 @@ Synchronize read on input.
522 522
 @section Advanced options
523 523
 
524 524
 @table @option
525
-@item -map [-]@var{input_file_id}[:@var{stream_specifier}][,@var{sync_file_id}[:@var{stream_specifier}]] (@emph{output})
525
+@item -map [-]@var{input_file_id}[:@var{stream_specifier}][,@var{sync_file_id}[:@var{stream_specifier}]] | @var{[linklabel]} (@emph{output})
526 526
 
527 527
 Designate one or more input streams as a source for the output file. Each input
528 528
 stream is identified by the input file index @var{input_file_id} and
... ...
@@ -538,6 +541,10 @@ the source for output stream 1, etc.
538 538
 A @code{-} character before the stream identifier creates a "negative" mapping.
539 539
 It disables matching streams from already created mappings.
540 540
 
541
+An alternative @var{[linklabel]} form will map outputs from complex filter
542
+graphs (see the @option{-filter_complex} option) to the output file.
543
+@var{linklabel} must correspond to a defined output link label in the graph.
544
+
541 545
 For example, to map ALL streams from the first input file to output
542 546
 @example
543 547
 ffmpeg -i INPUT -map 0 output
... ...
@@ -832,6 +839,44 @@ Specify Timecode for writing. @var{SEP} is ':' for non drop timecode and ';'
832 832
 @example
833 833
 ffmpeg -i input.mpg -timecode 01:02:03.04 -r 30000/1001 -s ntsc output.mpg
834 834
 @end example
835
+
836
+@item -filter_complex @var{filtergraph} (@emph{global})
837
+Define a complex filter graph, i.e. one with arbitrary number of inputs and/or
838
+outputs. For simple graphs -- those with one input and one output of the same
839
+type -- see the @option{-filter} options. @var{filtergraph} is a description of
840
+the filter graph, as described in @ref{Filtergraph syntax}.
841
+
842
+Input link labels must refer to input streams using the
843
+@code{[file_index:stream_specifier]} syntax (i.e. the same as @option{-map}
844
+uses). If @var{stream_specifier} matches multiple streams, the first one will be
845
+used. An unlabeled input will be connected to the first unused input stream of
846
+the matching type.
847
+
848
+Output link labels are referred to with @option{-map}. Unlabeled outputs are
849
+added to the first output file.
850
+
851
+For example, to overlay an image over video
852
+@example
853
+ffmpeg -i video.mkv -i image.png -filter_complex '[0:v][1:v]overlay[out]' -map
854
+'[out]' out.mkv
855
+@end example
856
+Here @code{[0:v]} refers to the first video stream in the first input file,
857
+which is linked to the first (main) input of the overlay filter. Similarly the
858
+first video stream in the second input is linked to the second (overlay) input
859
+of overlay.
860
+
861
+Assuming there is only one video stream in each input file, we can omit input
862
+labels, so the above is equivalent to
863
+@example
864
+ffmpeg -i video.mkv -i image.png -filter_complex 'overlay[out]' -map
865
+'[out]' out.mkv
866
+@end example
867
+
868
+Furthermore we can omit the output label and the single output from the filter
869
+graph will be added to the output file automatically, so we can simply write
870
+@example
871
+ffmpeg -i video.mkv -i image.png -filter_complex 'overlay' out.mkv
872
+@end example
835 873
 @end table
836 874
 
837 875
 @section Preset files
... ...
@@ -14,6 +14,7 @@ number of input and output pads of the filter.
14 14
 A filter with no input pads is called a "source", a filter with no
15 15
 output pads is called a "sink".
16 16
 
17
+@anchor{Filtergraph syntax}
17 18
 @section Filtergraph syntax
18 19
 
19 20
 A filtergraph can be represented using a textual representation, which
... ...
@@ -108,6 +108,7 @@ typedef struct StreamMap {
108 108
     int stream_index;
109 109
     int sync_file_index;
110 110
     int sync_stream_index;
111
+    char *linklabel;       /** name of an output link, for mapping lavfi outputs */
111 112
 } StreamMap;
112 113
 
113 114
 typedef struct {
... ...
@@ -175,6 +176,33 @@ static unsigned int allocated_async_buf_size;
175 175
 
176 176
 #define DEFAULT_PASS_LOGFILENAME_PREFIX "ffmpeg2pass"
177 177
 
178
+typedef struct InputFilter {
179
+    AVFilterContext    *filter;
180
+    struct InputStream *ist;
181
+    struct FilterGraph *graph;
182
+} InputFilter;
183
+
184
+typedef struct OutputFilter {
185
+    AVFilterContext     *filter;
186
+    struct OutputStream *ost;
187
+    struct FilterGraph  *graph;
188
+
189
+    /* temporary storage until stream maps are processed */
190
+    AVFilterInOut       *out_tmp;
191
+} OutputFilter;
192
+
193
+typedef struct FilterGraph {
194
+    int            index;
195
+    const char    *graph_desc;
196
+
197
+    AVFilterGraph *graph;
198
+
199
+    InputFilter   **inputs;
200
+    int          nb_inputs;
201
+    OutputFilter **outputs;
202
+    int         nb_outputs;
203
+} FilterGraph;
204
+
178 205
 typedef struct FrameBuffer {
179 206
     uint8_t *base[4];
180 207
     uint8_t *data[4];
... ...
@@ -195,7 +223,6 @@ typedef struct InputStream {
195 195
     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
196 196
     AVCodec *dec;
197 197
     AVFrame *decoded_frame;
198
-    AVFrame *filtered_frame;
199 198
 
200 199
     int64_t       start;     /* time when read started */
201 200
     /* predicted dts of the next packet read for this stream or (when there are
... ...
@@ -211,9 +238,18 @@ typedef struct InputStream {
211 211
     int showed_multi_packet_warning;
212 212
     AVDictionary *opts;
213 213
 
214
+    int resample_height;
215
+    int resample_width;
216
+    int resample_pix_fmt;
217
+
214 218
     /* a pool of free buffers for decoded data */
215 219
     FrameBuffer *buffer_pool;
216 220
     int dr1;
221
+
222
+    /* decoded data from this stream goes into all those filters
223
+     * currently video only */
224
+    InputFilter **filters;
225
+    int        nb_filters;
217 226
 } InputStream;
218 227
 
219 228
 typedef struct InputFile {
... ...
@@ -242,17 +278,15 @@ typedef struct OutputStream {
242 242
     AVCodec *enc;
243 243
     int64_t max_frames;
244 244
     AVFrame *output_frame;
245
+    AVFrame *filtered_frame;
245 246
 
246 247
     /* video only */
247
-    int video_resample;
248
-    int resample_height;
249
-    int resample_width;
250
-    int resample_pix_fmt;
251 248
     AVRational frame_rate;
252 249
     int force_fps;
253 250
     int top_field_first;
254 251
 
255 252
     float frame_aspect_ratio;
253
+    float last_quality;
256 254
 
257 255
     /* forced key frames */
258 256
     int64_t *forced_kf_pts;
... ...
@@ -272,10 +306,8 @@ typedef struct OutputStream {
272 272
 
273 273
     SwrContext *swr;
274 274
 
275
-    AVFilterContext *output_video_filter;
276
-    AVFilterContext *input_video_filter;
275
+    OutputFilter *filter;
277 276
     char *avfilter;
278
-    AVFilterGraph *graph;
279 277
 
280 278
     int64_t sws_flags;
281 279
     int64_t swr_dither_method;
... ...
@@ -285,6 +317,8 @@ typedef struct OutputStream {
285 285
     int stream_copy;
286 286
     const char *attachment_filename;
287 287
     int copy_initial_nonkeyframes;
288
+
289
+    enum PixelFormat pix_fmts[2];
288 290
 } OutputStream;
289 291
 
290 292
 
... ...
@@ -304,15 +338,18 @@ typedef struct OutputFile {
304 304
     uint64_t limit_filesize; /* filesize limit expressed in bytes */
305 305
 } OutputFile;
306 306
 
307
-static InputStream *input_streams   = NULL;
308
-static int         nb_input_streams = 0;
309
-static InputFile   *input_files     = NULL;
310
-static int         nb_input_files   = 0;
307
+static InputStream **input_streams = NULL;
308
+static int        nb_input_streams = 0;
309
+static InputFile   **input_files   = NULL;
310
+static int        nb_input_files   = 0;
311 311
 
312
-static OutputStream *output_streams = NULL;
313
-static int        nb_output_streams = 0;
314
-static OutputFile   *output_files   = NULL;
315
-static int        nb_output_files   = 0;
312
+static OutputStream **output_streams = NULL;
313
+static int         nb_output_streams = 0;
314
+static OutputFile   **output_files   = NULL;
315
+static int         nb_output_files   = 0;
316
+
317
+static FilterGraph **filtergraphs;
318
+int               nb_filtergraphs;
316 319
 
317 320
 typedef struct OptionsContext {
318 321
     /* input/output options */
... ...
@@ -457,6 +494,7 @@ static void reset_options(OptionsContext *o, int is_input)
457 457
 {
458 458
     const OptionDef *po = options;
459 459
     OptionsContext bak= *o;
460
+    int i;
460 461
 
461 462
     /* all OPT_SPEC and OPT_STRING can be freed in generic way */
462 463
     while (po->name) {
... ...
@@ -477,6 +515,8 @@ static void reset_options(OptionsContext *o, int is_input)
477 477
         po++;
478 478
     }
479 479
 
480
+    for (i = 0; i < o->nb_stream_maps; i++)
481
+        av_freep(&o->stream_maps[i].linklabel);
480 482
     av_freep(&o->stream_maps);
481 483
     av_freep(&o->audio_channel_maps);
482 484
     av_freep(&o->streamid_map);
... ...
@@ -627,20 +667,64 @@ static void filter_release_buffer(AVFilterBuffer *fb)
627 627
     unref_buffer(buf->ist, buf);
628 628
 }
629 629
 
630
-static int configure_video_filters(InputStream *ist, OutputStream *ost)
630
+static void choose_pixel_fmt(AVStream *st, AVCodec *codec)
631 631
 {
632
+    if (codec && codec->pix_fmts) {
633
+        const enum PixelFormat *p = codec->pix_fmts;
634
+        int has_alpha= av_pix_fmt_descriptors[st->codec->pix_fmt].nb_components % 2 == 0;
635
+        enum PixelFormat best= PIX_FMT_NONE;
636
+        if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
637
+            if (st->codec->codec_id == CODEC_ID_MJPEG) {
638
+                p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE };
639
+            } else if (st->codec->codec_id == CODEC_ID_LJPEG) {
640
+                p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P,
641
+                                                 PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE };
642
+            }
643
+        }
644
+        for (; *p != PIX_FMT_NONE; p++) {
645
+            best= avcodec_find_best_pix_fmt2(best, *p, st->codec->pix_fmt, has_alpha, NULL);
646
+            if (*p == st->codec->pix_fmt)
647
+                break;
648
+        }
649
+        if (*p == PIX_FMT_NONE) {
650
+            if (st->codec->pix_fmt != PIX_FMT_NONE)
651
+                av_log(NULL, AV_LOG_WARNING,
652
+                       "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
653
+                       av_pix_fmt_descriptors[st->codec->pix_fmt].name,
654
+                       codec->name,
655
+                       av_pix_fmt_descriptors[best].name);
656
+            st->codec->pix_fmt = best;
657
+        }
658
+    }
659
+}
660
+
661
+static const enum PixelFormat *choose_pixel_fmts(OutputStream *ost)
662
+{
663
+    if (ost->st->codec->pix_fmt != PIX_FMT_NONE) {
664
+        ost->pix_fmts[0] = ost->st->codec->pix_fmt;
665
+        return ost->pix_fmts;
666
+    } else if (ost->enc->pix_fmts)
667
+        return ost->enc->pix_fmts;
668
+    else
669
+        return NULL;
670
+}
671
+
672
+static int configure_video_filters(FilterGraph *fg)
673
+{
674
+    InputStream  *ist = fg->inputs[0]->ist;
675
+    OutputStream *ost = fg->outputs[0]->ost;
632 676
     AVFilterContext *last_filter, *filter;
633 677
     /** filter graph containing all filters including input & output */
634 678
     AVCodecContext *codec = ost->st->codec;
635
-    AVCodecContext *icodec = ist->st->codec;
636
-    enum PixelFormat pix_fmts[] = { codec->pix_fmt, PIX_FMT_NONE };
679
+    enum PixelFormat *pix_fmts = choose_pixel_fmts(ost);
637 680
     AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
638 681
     AVRational sample_aspect_ratio;
639 682
     char args[255];
640 683
     int ret;
641 684
 
642
-    ost->graph = avfilter_graph_alloc();
643
-    if (!ost->graph)
685
+    avfilter_graph_free(&fg->graph);
686
+    fg->graph = avfilter_graph_alloc();
687
+    if (!fg->graph)
644 688
         return AVERROR(ENOMEM);
645 689
 
646 690
     if (ist->st->sample_aspect_ratio.num) {
... ...
@@ -650,34 +734,35 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost)
650 650
 
651 651
     snprintf(args, 255, "%d:%d:%d:%d:%d:%d:%d:flags=%d", ist->st->codec->width,
652 652
              ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE,
653
-             sample_aspect_ratio.num, sample_aspect_ratio.den, SWS_BILINEAR + ((icodec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
653
+             sample_aspect_ratio.num, sample_aspect_ratio.den, SWS_BILINEAR + ((ist->st->codec->flags&CODEC_FLAG_BITEXACT) ? SWS_BITEXACT:0));
654 654
 
655
-    ret = avfilter_graph_create_filter(&ost->input_video_filter, avfilter_get_by_name("buffer"),
656
-                                       "src", args, NULL, ost->graph);
655
+    ret = avfilter_graph_create_filter(&fg->inputs[0]->filter,
656
+                                       avfilter_get_by_name("buffer"),
657
+                                       "src", args, NULL, fg->graph);
657 658
     if (ret < 0)
658 659
         return ret;
659 660
 
660 661
 #if FF_API_OLD_VSINK_API
661
-    ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
662
-                                       "out", NULL, pix_fmts, ost->graph);
662
+    ret = avfilter_graph_create_filter(&fg->outputs[0]->filter, avfilter_get_by_name("buffersink"),
663
+                                       "out", NULL, pix_fmts, fg->graph);
663 664
 #else
664 665
     buffersink_params->pixel_fmts = pix_fmts;
665
-    ret = avfilter_graph_create_filter(&ost->output_video_filter, avfilter_get_by_name("buffersink"),
666
-                                       "out", NULL, buffersink_params, ost->graph);
666
+    ret = avfilter_graph_create_filter(&fg->outputs[0]->filter, avfilter_get_by_name("buffersink"),
667
+                                       "out", NULL, buffersink_params, fg->graph);
667 668
 #endif
668 669
     av_freep(&buffersink_params);
669 670
 
670 671
     if (ret < 0)
671 672
         return ret;
672
-    last_filter = ost->input_video_filter;
673
+    last_filter = fg->inputs[0]->filter;
673 674
 
674
-    if (codec->width != icodec->width || codec->height != icodec->height) {
675
+    if (codec->width || codec->height) {
675 676
         snprintf(args, 255, "%d:%d:flags=0x%X",
676 677
                  codec->width,
677 678
                  codec->height,
678 679
                  (unsigned)ost->sws_flags);
679 680
         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
680
-                                                NULL, args, NULL, ost->graph)) < 0)
681
+                                                NULL, args, NULL, fg->graph)) < 0)
681 682
             return ret;
682 683
         if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
683 684
             return ret;
... ...
@@ -685,7 +770,7 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost)
685 685
     }
686 686
 
687 687
     snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
688
-    ost->graph->scale_sws_opts = av_strdup(args);
688
+    fg->graph->scale_sws_opts = av_strdup(args);
689 689
 
690 690
     if (ost->avfilter) {
691 691
         AVFilterInOut *outputs = avfilter_inout_alloc();
... ...
@@ -697,31 +782,258 @@ static int configure_video_filters(InputStream *ist, OutputStream *ost)
697 697
         outputs->next    = NULL;
698 698
 
699 699
         inputs->name    = av_strdup("out");
700
-        inputs->filter_ctx = ost->output_video_filter;
700
+        inputs->filter_ctx = fg->outputs[0]->filter;
701 701
         inputs->pad_idx = 0;
702 702
         inputs->next    = NULL;
703 703
 
704
-        if ((ret = avfilter_graph_parse(ost->graph, ost->avfilter, &inputs, &outputs, NULL)) < 0)
704
+        if ((ret = avfilter_graph_parse(fg->graph, ost->avfilter, &inputs, &outputs, NULL)) < 0)
705 705
             return ret;
706 706
         av_freep(&ost->avfilter);
707 707
     } else {
708
-        if ((ret = avfilter_link(last_filter, 0, ost->output_video_filter, 0)) < 0)
708
+        if ((ret = avfilter_link(last_filter, 0, fg->outputs[0]->filter, 0)) < 0)
709 709
             return ret;
710 710
     }
711 711
 
712
-    if ((ret = avfilter_graph_config(ost->graph, NULL)) < 0)
712
+    if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
713 713
         return ret;
714 714
 
715
-    codec->width  = ost->output_video_filter->inputs[0]->w;
716
-    codec->height = ost->output_video_filter->inputs[0]->h;
717
-    codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
718
-        ost->frame_aspect_ratio ? // overridden by the -aspect cli option
719
-        av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
720
-        ost->output_video_filter->inputs[0]->sample_aspect_ratio;
715
+    ost->filter = fg->outputs[0];
721 716
 
722 717
     return 0;
723 718
 }
724 719
 
720
+static FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
721
+{
722
+    FilterGraph *fg = av_mallocz(sizeof(*fg));
723
+
724
+    if (!fg)
725
+        exit_program(1);
726
+    fg->index = nb_filtergraphs;
727
+
728
+    fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs,
729
+                             fg->nb_outputs + 1);
730
+    if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
731
+        exit_program(1);
732
+    fg->outputs[0]->ost   = ost;
733
+    fg->outputs[0]->graph = fg;
734
+
735
+    fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs), &fg->nb_inputs,
736
+                            fg->nb_inputs + 1);
737
+    if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
738
+        exit_program(1);
739
+    fg->inputs[0]->ist   = ist;
740
+    fg->inputs[0]->graph = fg;
741
+
742
+    ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
743
+                              &ist->nb_filters, ist->nb_filters + 1);
744
+    ist->filters[ist->nb_filters - 1] = fg->inputs[0];
745
+
746
+    filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
747
+                              &nb_filtergraphs, nb_filtergraphs + 1);
748
+    filtergraphs[nb_filtergraphs - 1] = fg;
749
+
750
+    return fg;
751
+}
752
+
753
+static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
754
+{
755
+    InputStream *ist;
756
+    enum AVMediaType type = in->filter_ctx->input_pads[in->pad_idx].type;
757
+    int i;
758
+
759
+    // TODO: support other filter types
760
+    if (type != AVMEDIA_TYPE_VIDEO) {
761
+        av_log(NULL, AV_LOG_FATAL, "Only video filters supported currently.\n");
762
+        exit_program(1);
763
+    }
764
+
765
+    if (in->name) {
766
+        AVFormatContext *s;
767
+        AVStream       *st = NULL;
768
+        char *p;
769
+        int file_idx = strtol(in->name, &p, 0);
770
+
771
+        if (file_idx < 0 || file_idx > nb_input_files) {
772
+            av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
773
+                   file_idx, fg->graph_desc);
774
+            exit_program(1);
775
+        }
776
+        s = input_files[file_idx]->ctx;
777
+
778
+        for (i = 0; i < s->nb_streams; i++) {
779
+            if (s->streams[i]->codec->codec_type != type)
780
+                continue;
781
+            if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
782
+                st = s->streams[i];
783
+                break;
784
+            }
785
+        }
786
+        if (!st) {
787
+            av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
788
+                   "matches no streams.\n", p, fg->graph_desc);
789
+            exit_program(1);
790
+        }
791
+        ist = input_streams[input_files[file_idx]->ist_index + st->index];
792
+    } else {
793
+        /* find the first unused stream of corresponding type */
794
+        for (i = 0; i < nb_input_streams; i++) {
795
+            ist = input_streams[i];
796
+            if (ist->st->codec->codec_type == type && ist->discard)
797
+                break;
798
+        }
799
+        if (i == nb_input_streams) {
800
+            av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
801
+                   "unlabeled input pad %d on filter %s", in->pad_idx,
802
+                   in->filter_ctx->name);
803
+            exit_program(1);
804
+        }
805
+    }
806
+    ist->discard         = 0;
807
+    ist->decoding_needed = 1;
808
+    ist->st->discard = AVDISCARD_NONE;
809
+
810
+    fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs),
811
+                            &fg->nb_inputs, fg->nb_inputs + 1);
812
+    if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
813
+        exit_program(1);
814
+    fg->inputs[fg->nb_inputs - 1]->ist   = ist;
815
+    fg->inputs[fg->nb_inputs - 1]->graph = fg;
816
+
817
+    ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
818
+                              &ist->nb_filters, ist->nb_filters + 1);
819
+    ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
820
+}
821
+
822
+static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
823
+{
824
+    AVCodecContext *codec = ofilter->ost->st->codec;
825
+    AVFilterContext *last_filter = out->filter_ctx;
826
+    int pad_idx = out->pad_idx;
827
+    int ret;
828
+    enum PixelFormat *pix_fmts = choose_pixel_fmts(ofilter->ost);
829
+    AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
830
+
831
+#if FF_API_OLD_VSINK_API
832
+    ret = avfilter_graph_create_filter(&ofilter->filter, avfilter_get_by_name("buffersink"),
833
+                                       "out", NULL, pix_fmts, fg->graph);
834
+#else
835
+    buffersink_params->pixel_fmts = pix_fmts;
836
+    ret = avfilter_graph_create_filter(&ofilter->filter, avfilter_get_by_name("buffersink"),
837
+                                       "out", NULL, buffersink_params, fg->graph);
838
+#endif
839
+    av_freep(&buffersink_params);
840
+
841
+    if (ret < 0)
842
+        return ret;
843
+
844
+    if (codec->width || codec->height) {
845
+        char args[255];
846
+        snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
847
+                 codec->width,
848
+                 codec->height,
849
+                 (unsigned)ofilter->ost->sws_flags);
850
+        if ((ret = avfilter_graph_create_filter(&last_filter, avfilter_get_by_name("scale"),
851
+                                                NULL, args, NULL, fg->graph)) < 0)
852
+            return ret;
853
+        if ((ret = avfilter_link(out->filter_ctx, out->pad_idx, last_filter, 0)) < 0)
854
+            return ret;
855
+        pad_idx = 0;
856
+    }
857
+
858
+    if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
859
+        return ret;
860
+
861
+    return 0;
862
+}
863
+
864
+static int configure_complex_filter(FilterGraph *fg)
865
+{
866
+    AVFilterInOut *inputs, *outputs, *cur;
867
+    int ret, i, init = !fg->graph;
868
+
869
+    avfilter_graph_free(&fg->graph);
870
+    if (!(fg->graph = avfilter_graph_alloc()))
871
+        return AVERROR(ENOMEM);
872
+
873
+    if ((ret = avfilter_graph_parse2(fg->graph, fg->graph_desc, &inputs, &outputs)) < 0)
874
+        return ret;
875
+
876
+    for (cur = inputs; init && cur; cur = cur->next)
877
+        init_input_filter(fg, cur);
878
+
879
+    for (cur = inputs, i = 0; cur; cur = cur->next, i++) {
880
+        InputFilter *ifilter = fg->inputs[i];
881
+        InputStream     *ist = ifilter->ist;
882
+        AVRational       sar;
883
+        char            args[255];
884
+
885
+        sar = ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
886
+                                                 ist->st->codec->sample_aspect_ratio;
887
+        snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
888
+                 ist->st->codec->height, ist->st->codec->pix_fmt, 1, AV_TIME_BASE,
889
+                 sar.num, sar.den);
890
+
891
+        if ((ret = avfilter_graph_create_filter(&ifilter->filter,
892
+                                                avfilter_get_by_name("buffer"), cur->name,
893
+                                                args, NULL, fg->graph)) < 0)
894
+            return ret;
895
+        if ((ret = avfilter_link(ifilter->filter, 0,
896
+                                 cur->filter_ctx, cur->pad_idx)) < 0)
897
+            return ret;
898
+    }
899
+    avfilter_inout_free(&inputs);
900
+
901
+    if (!init) {
902
+        /* we already know the mappings between lavfi outputs and output streams,
903
+         * so we can finish the setup */
904
+        for (cur = outputs, i = 0; cur; cur = cur->next, i++)
905
+            configure_output_filter(fg, fg->outputs[i], cur);
906
+        avfilter_inout_free(&outputs);
907
+
908
+        if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
909
+            return ret;
910
+    } else {
911
+        /* wait until output mappings are processed */
912
+        for (cur = outputs; cur;) {
913
+            fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs),
914
+                                     &fg->nb_outputs, fg->nb_outputs + 1);
915
+            if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
916
+                exit_program(1);
917
+            fg->outputs[fg->nb_outputs - 1]->graph   = fg;
918
+            fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
919
+            cur = cur->next;
920
+            fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
921
+        }
922
+    }
923
+
924
+    return 0;
925
+}
926
+
927
+static int configure_complex_filters(void)
928
+{
929
+    int i, ret = 0;
930
+
931
+    for (i = 0; i < nb_filtergraphs; i++)
932
+        if (!filtergraphs[i]->graph &&
933
+            (ret = configure_complex_filter(filtergraphs[i])) < 0)
934
+            return ret;
935
+    return 0;
936
+}
937
+
938
+static int configure_filtergraph(FilterGraph *fg)
939
+{
940
+    return fg->graph_desc ? configure_complex_filter(fg) : configure_video_filters(fg);
941
+}
942
+
943
+static int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
944
+{
945
+    int i;
946
+    for (i = 0; i < fg->nb_inputs; i++)
947
+        if (fg->inputs[i]->ist == ist)
948
+            return 1;
949
+    return 0;
950
+}
951
+
725 952
 static void term_exit(void)
726 953
 {
727 954
     av_log(NULL, AV_LOG_QUIET, "%s", "");
... ...
@@ -839,40 +1151,57 @@ static const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
839 839
 
840 840
 void av_noreturn exit_program(int ret)
841 841
 {
842
-    int i;
842
+    int i, j;
843
+
844
+    for (i = 0; i < nb_filtergraphs; i++) {
845
+        avfilter_graph_free(&filtergraphs[i]->graph);
846
+        for (j = 0; j < filtergraphs[i]->nb_inputs; j++)
847
+            av_freep(&filtergraphs[i]->inputs[j]);
848
+        av_freep(&filtergraphs[i]->inputs);
849
+        for (j = 0; j < filtergraphs[i]->nb_outputs; j++)
850
+            av_freep(&filtergraphs[i]->outputs[j]);
851
+        av_freep(&filtergraphs[i]->outputs);
852
+        av_freep(&filtergraphs[i]);
853
+    }
854
+    av_freep(&filtergraphs);
843 855
 
844 856
     /* close files */
845 857
     for (i = 0; i < nb_output_files; i++) {
846
-        AVFormatContext *s = output_files[i].ctx;
858
+        AVFormatContext *s = output_files[i]->ctx;
847 859
         if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
848 860
             avio_close(s->pb);
849 861
         avformat_free_context(s);
850
-        av_dict_free(&output_files[i].opts);
862
+        av_dict_free(&output_files[i]->opts);
863
+        av_freep(&output_files[i]);
851 864
     }
852 865
     for (i = 0; i < nb_output_streams; i++) {
853
-        AVBitStreamFilterContext *bsfc = output_streams[i].bitstream_filters;
866
+        AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
854 867
         while (bsfc) {
855 868
             AVBitStreamFilterContext *next = bsfc->next;
856 869
             av_bitstream_filter_close(bsfc);
857 870
             bsfc = next;
858 871
         }
859
-        output_streams[i].bitstream_filters = NULL;
872
+        output_streams[i]->bitstream_filters = NULL;
860 873
 
861
-        if (output_streams[i].output_frame) {
862
-            AVFrame *frame = output_streams[i].output_frame;
874
+        if (output_streams[i]->output_frame) {
875
+            AVFrame *frame = output_streams[i]->output_frame;
863 876
             if (frame->extended_data != frame->data)
864 877
                 av_freep(&frame->extended_data);
865 878
             av_freep(&frame);
866 879
         }
880
+        av_freep(&output_streams[i]->filtered_frame);
881
+        av_freep(&output_streams[i]);
867 882
     }
868 883
     for (i = 0; i < nb_input_files; i++) {
869
-        avformat_close_input(&input_files[i].ctx);
884
+        avformat_close_input(&input_files[i]->ctx);
885
+        av_freep(&input_files[i]);
870 886
     }
871 887
     for (i = 0; i < nb_input_streams; i++) {
872
-        av_freep(&input_streams[i].decoded_frame);
873
-        av_freep(&input_streams[i].filtered_frame);
874
-        av_dict_free(&input_streams[i].opts);
875
-        free_buffer_pool(&input_streams[i]);
888
+        av_freep(&input_streams[i]->decoded_frame);
889
+        av_dict_free(&input_streams[i]->opts);
890
+        free_buffer_pool(input_streams[i]);
891
+        av_freep(&input_streams[i]->filters);
892
+        av_freep(&input_streams[i]);
876 893
     }
877 894
 
878 895
     if (vstats_file)
... ...
@@ -981,41 +1310,10 @@ static void choose_sample_rate(AVStream *st, AVCodec *codec)
981 981
     }
982 982
 }
983 983
 
984
-static void choose_pixel_fmt(AVStream *st, AVCodec *codec)
985
-{
986
-    if (codec && codec->pix_fmts) {
987
-        const enum PixelFormat *p = codec->pix_fmts;
988
-        int has_alpha= av_pix_fmt_descriptors[st->codec->pix_fmt].nb_components % 2 == 0;
989
-        enum PixelFormat best= PIX_FMT_NONE;
990
-        if (st->codec->strict_std_compliance <= FF_COMPLIANCE_UNOFFICIAL) {
991
-            if (st->codec->codec_id == CODEC_ID_MJPEG) {
992
-                p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_NONE };
993
-            } else if (st->codec->codec_id == CODEC_ID_LJPEG) {
994
-                p = (const enum PixelFormat[]) { PIX_FMT_YUVJ420P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ444P, PIX_FMT_YUV420P,
995
-                                                 PIX_FMT_YUV422P, PIX_FMT_YUV444P, PIX_FMT_BGRA, PIX_FMT_NONE };
996
-            }
997
-        }
998
-        for (; *p != PIX_FMT_NONE; p++) {
999
-            best= avcodec_find_best_pix_fmt2(best, *p, st->codec->pix_fmt, has_alpha, NULL);
1000
-            if (*p == st->codec->pix_fmt)
1001
-                break;
1002
-        }
1003
-        if (*p == PIX_FMT_NONE) {
1004
-            if (st->codec->pix_fmt != PIX_FMT_NONE)
1005
-                av_log(NULL, AV_LOG_WARNING,
1006
-                       "Incompatible pixel format '%s' for codec '%s', auto-selecting format '%s'\n",
1007
-                       av_pix_fmt_descriptors[st->codec->pix_fmt].name,
1008
-                       codec->name,
1009
-                       av_pix_fmt_descriptors[best].name);
1010
-            st->codec->pix_fmt = best;
1011
-        }
1012
-    }
1013
-}
1014
-
1015 984
 static double
1016 985
 get_sync_ipts(const OutputStream *ost, int64_t pts)
1017 986
 {
1018
-    OutputFile *of = &output_files[ost->file_index];
987
+    OutputFile *of = output_files[ost->file_index];
1019 988
     return (double)(pts - of->start_time) / AV_TIME_BASE;
1020 989
 }
1021 990
 
... ...
@@ -1518,19 +1816,21 @@ static void do_video_stats(AVFormatContext *os, OutputStream *ost,
1518 1518
 
1519 1519
 
1520 1520
 static void do_video_out(AVFormatContext *s, OutputStream *ost,
1521
-                         InputStream *ist, AVFrame *in_picture)
1521
+                         AVFrame *in_picture, float quality)
1522 1522
 {
1523 1523
     int nb_frames, i, ret, format_video_sync;
1524 1524
     AVCodecContext *enc;
1525 1525
     double sync_ipts, delta;
1526 1526
     double duration = 0;
1527 1527
     int frame_size = 0;
1528
-    float quality = same_quant ? in_picture->quality
1529
-                               : ost->st->codec->global_quality;
1528
+    InputStream *ist = NULL;
1529
+
1530
+    if (ost->source_index >= 0)
1531
+        ist = input_streams[ost->source_index];
1530 1532
 
1531 1533
     enc = ost->st->codec;
1532 1534
 
1533
-    if (ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE) {
1535
+    if (ist && ist->st->start_time != AV_NOPTS_VALUE && ist->st->first_dts != AV_NOPTS_VALUE) {
1534 1536
         duration = FFMAX(av_q2d(ist->st->time_base), av_q2d(ist->st->codec->time_base));
1535 1537
         if(ist->st->r_frame_rate.num)
1536 1538
             duration= FFMAX(duration, 1/av_q2d(ist->st->r_frame_rate));
... ...
@@ -1672,12 +1972,66 @@ static void do_video_out(AVFormatContext *s, OutputStream *ost,
1672 1672
         ost->frame_number++;
1673 1673
     }
1674 1674
     if (vstats_filename && frame_size)
1675
-        do_video_stats(output_files[ost->file_index].ctx, ost, frame_size);
1675
+        do_video_stats(output_files[ost->file_index]->ctx, ost, frame_size);
1676 1676
 }
1677 1677
 
1678
-static void print_report(OutputFile *output_files,
1679
-                         OutputStream *ost_table, int nb_ostreams,
1680
-                         int is_last_report, int64_t timer_start, int64_t cur_time)
1678
+/* check for new output on any of the filtergraphs */
1679
+static int poll_filters(void)
1680
+{
1681
+    AVFilterBufferRef *picref;
1682
+    AVFrame *filtered_frame = NULL;
1683
+    int i, ret;
1684
+
1685
+    for (i = 0; i < nb_output_streams; i++) {
1686
+        OutputStream *ost = output_streams[i];
1687
+        OutputFile    *of = output_files[ost->file_index];
1688
+
1689
+        if (!ost->filter || ost->is_past_recording_time)
1690
+            continue;
1691
+
1692
+        if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
1693
+            return AVERROR(ENOMEM);
1694
+        } else
1695
+            avcodec_get_frame_defaults(ost->filtered_frame);
1696
+        filtered_frame = ost->filtered_frame;
1697
+
1698
+        while (avfilter_poll_frame(ost->filter->filter->inputs[0])) {
1699
+            AVRational ist_pts_tb = ost->filter->filter->inputs[0]->time_base;
1700
+            if ((ret = av_buffersink_get_buffer_ref(ost->filter->filter,
1701
+                                                            &picref,
1702
+                                                            0)) < 0) {
1703
+                av_log(NULL, AV_LOG_WARNING, "AV Filter told us it has a frame available but failed to output one\n");
1704
+                return ret;
1705
+            }
1706
+            filtered_frame->pts = av_rescale_q(picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
1707
+//             if (ost->source_index >= 0)
1708
+//                 *filtered_frame= *input_streams[ost->source_index]->decoded_frame; //for me_threshold
1709
+
1710
+            if (of->start_time && filtered_frame->pts < of->start_time)
1711
+                return 0;
1712
+
1713
+            switch (ost->filter->filter->inputs[0]->type) {
1714
+            case AVMEDIA_TYPE_VIDEO:
1715
+                avfilter_fill_frame_from_video_buffer_ref(filtered_frame, picref);
1716
+                if (!ost->frame_aspect_ratio)
1717
+                    ost->st->codec->sample_aspect_ratio = picref->video->sample_aspect_ratio;
1718
+
1719
+                do_video_out(of->ctx, ost, filtered_frame,
1720
+                             same_quant ? ost->last_quality :
1721
+                                          ost->st->codec->global_quality);
1722
+                break;
1723
+            default:
1724
+                // TODO support audio/subtitle filters
1725
+                av_assert0(0);
1726
+            }
1727
+
1728
+            avfilter_unref_buffer(picref);
1729
+        }
1730
+    }
1731
+    return 0;
1732
+}
1733
+
1734
+static void print_report(int is_last_report, int64_t timer_start, int64_t cur_time)
1681 1735
 {
1682 1736
     char buf[1024];
1683 1737
     OutputStream *ost;
... ...
@@ -1705,7 +2059,7 @@ static void print_report(OutputFile *output_files,
1705 1705
     }
1706 1706
 
1707 1707
 
1708
-    oc = output_files[0].ctx;
1708
+    oc = output_files[0]->ctx;
1709 1709
 
1710 1710
     total_size = avio_size(oc->pb);
1711 1711
     if (total_size < 0) { // FIXME improve avio_size() so it works with non seekable output too
... ...
@@ -1716,9 +2070,9 @@ static void print_report(OutputFile *output_files,
1716 1716
 
1717 1717
     buf[0] = '\0';
1718 1718
     vid = 0;
1719
-    for (i = 0; i < nb_ostreams; i++) {
1719
+    for (i = 0; i < nb_output_streams; i++) {
1720 1720
         float q = -1;
1721
-        ost = &ost_table[i];
1721
+        ost = output_streams[i];
1722 1722
         enc = ost->st->codec;
1723 1723
         if (!ost->stream_copy && enc->coded_frame)
1724 1724
             q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
... ...
@@ -1811,14 +2165,14 @@ static void print_report(OutputFile *output_files,
1811 1811
     }
1812 1812
 }
1813 1813
 
1814
-static void flush_encoders(OutputStream *ost_table, int nb_ostreams)
1814
+static void flush_encoders(void)
1815 1815
 {
1816 1816
     int i, ret;
1817 1817
 
1818
-    for (i = 0; i < nb_ostreams; i++) {
1819
-        OutputStream   *ost = &ost_table[i];
1818
+    for (i = 0; i < nb_output_streams; i++) {
1819
+        OutputStream   *ost = output_streams[i];
1820 1820
         AVCodecContext *enc = ost->st->codec;
1821
-        AVFormatContext *os = output_files[ost->file_index].ctx;
1821
+        AVFormatContext *os = output_files[ost->file_index]->ctx;
1822 1822
         int stop_encoding = 0;
1823 1823
 
1824 1824
         if (!ost->encoding_needed)
... ...
@@ -1899,8 +2253,8 @@ static void flush_encoders(OutputStream *ost_table, int nb_ostreams)
1899 1899
  */
1900 1900
 static int check_output_constraints(InputStream *ist, OutputStream *ost)
1901 1901
 {
1902
-    OutputFile *of = &output_files[ost->file_index];
1903
-    int ist_index  = ist - input_streams;
1902
+    OutputFile *of = output_files[ost->file_index];
1903
+    int ist_index  = input_files[ist->file_index]->ist_index + ist->st->index;
1904 1904
 
1905 1905
     if (ost->source_index != ist_index)
1906 1906
         return 0;
... ...
@@ -1920,7 +2274,7 @@ static int check_output_constraints(InputStream *ist, OutputStream *ost)
1920 1920
 
1921 1921
 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
1922 1922
 {
1923
-    OutputFile *of = &output_files[ost->file_index];
1923
+    OutputFile *of = output_files[ost->file_index];
1924 1924
     int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
1925 1925
     AVPicture pict;
1926 1926
     AVPacket opkt;
... ...
@@ -1980,7 +2334,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p
1980 1980
 
1981 1981
 static void rate_emu_sleep(InputStream *ist)
1982 1982
 {
1983
-    if (input_files[ist->file_index].rate_emu) {
1983
+    if (input_files[ist->file_index]->rate_emu) {
1984 1984
         int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
1985 1985
         int64_t now = av_gettime() - ist->start;
1986 1986
         if (pts > now)
... ...
@@ -2091,11 +2445,11 @@ static int transcode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
2091 2091
     rate_emu_sleep(ist);
2092 2092
 
2093 2093
     for (i = 0; i < nb_output_streams; i++) {
2094
-        OutputStream *ost = &output_streams[i];
2094
+        OutputStream *ost = output_streams[i];
2095 2095
 
2096 2096
         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
2097 2097
             continue;
2098
-        do_audio_out(output_files[ost->file_index].ctx, ost, ist, decoded_frame);
2098
+        do_audio_out(output_files[ost->file_index]->ctx, ost, ist, decoded_frame);
2099 2099
     }
2100 2100
 
2101 2101
     return ret;
... ...
@@ -2105,9 +2459,10 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int
2105 2105
 {
2106 2106
     AVFrame *decoded_frame;
2107 2107
     void *buffer_to_free = NULL;
2108
-    int i, ret = 0;
2108
+    int i, ret = 0, resample_changed;
2109 2109
     int64_t *best_effort_timestamp;
2110 2110
     AVRational *frame_sample_aspect;
2111
+    float quality;
2111 2112
 
2112 2113
     if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
2113 2114
         return AVERROR(ENOMEM);
... ...
@@ -2125,6 +2480,7 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int
2125 2125
     if (ret < 0)
2126 2126
         return ret;
2127 2127
 
2128
+    quality = same_quant ? decoded_frame->quality : 0;
2128 2129
     if (!*got_output) {
2129 2130
         /* no picture yet */
2130 2131
         return ret;
... ...
@@ -2138,75 +2494,66 @@ static int transcode_video(InputStream *ist, AVPacket *pkt, int *got_output, int
2138 2138
 
2139 2139
     pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
2140 2140
 
2141
-    frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
2142
-    for(i=0;i<nb_output_streams;i++) {
2143
-        OutputStream *ost = ost = &output_streams[i];
2144
-        if(check_output_constraints(ist, ost) && ost->encoding_needed){
2145
-            int changed =    ist->st->codec->width   != ost->input_video_filter->outputs[0]->w
2146
-                          || ist->st->codec->height  != ost->input_video_filter->outputs[0]->h
2147
-                          || ist->st->codec->pix_fmt != ost->input_video_filter->outputs[0]->format;
2148
-            if (!frame_sample_aspect->num)
2149
-                *frame_sample_aspect = ist->st->sample_aspect_ratio;
2150
-            decoded_frame->pts = ist->pts;
2151
-            if (ist->dr1 && decoded_frame->type==FF_BUFFER_TYPE_USER && !changed) {
2152
-                FrameBuffer      *buf = decoded_frame->opaque;
2153
-                AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
2154
-                                            decoded_frame->data, decoded_frame->linesize,
2155
-                                            AV_PERM_READ | AV_PERM_PRESERVE,
2156
-                                            ist->st->codec->width, ist->st->codec->height,
2157
-                                            ist->st->codec->pix_fmt);
2158
-
2159
-                avfilter_copy_frame_props(fb, decoded_frame);
2160
-                fb->buf->priv           = buf;
2161
-                fb->buf->free           = filter_release_buffer;
2162
-
2163
-                buf->refcount++;
2164
-                av_buffersrc_buffer(ost->input_video_filter, fb);
2165
-            } else
2166
-            if((av_vsrc_buffer_add_frame(ost->input_video_filter, decoded_frame, AV_VSRC_BUF_FLAG_OVERWRITE)) < 0){
2167
-                av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
2168
-                exit_program(1);
2169
-            }
2170
-        }
2171
-    }
2172
-
2173 2141
     rate_emu_sleep(ist);
2174 2142
 
2175 2143
     if (ist->st->sample_aspect_ratio.num)
2176 2144
         decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
2177 2145
 
2178
-    for (i = 0; i < nb_output_streams; i++) {
2179
-        OutputStream *ost = &output_streams[i];
2180
-
2181
-        if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
2182
-            continue;
2183
-
2184
-        while (av_buffersink_poll_frame(ost->output_video_filter)) {
2185
-            AVRational ist_pts_tb = ost->output_video_filter->inputs[0]->time_base;
2186
-            AVFrame *filtered_frame;
2187
-            AVFilterBufferRef *picref;
2188
-
2189
-            if (av_buffersink_get_buffer_ref(ost->output_video_filter, &picref, 0) < 0){
2190
-                av_log(NULL, AV_LOG_WARNING, "AV Filter told us it has a frame available but failed to output one\n");
2191
-                goto cont;
2192
-            }
2193
-            if (!ist->filtered_frame && !(ist->filtered_frame = avcodec_alloc_frame())) {
2194
-                ret = AVERROR(ENOMEM);
2195
-                goto fail;
2146
+    resample_changed = ist->resample_width   != decoded_frame->width  ||
2147
+                       ist->resample_height  != decoded_frame->height ||
2148
+                       ist->resample_pix_fmt != decoded_frame->format;
2149
+    if (resample_changed) {
2150
+        av_log(NULL, AV_LOG_INFO,
2151
+               "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
2152
+               ist->file_index, ist->st->index,
2153
+               ist->resample_width,  ist->resample_height,  av_get_pix_fmt_name(ist->resample_pix_fmt),
2154
+               decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format));
2155
+
2156
+        ist->resample_width   = decoded_frame->width;
2157
+        ist->resample_height  = decoded_frame->height;
2158
+        ist->resample_pix_fmt = decoded_frame->format;
2159
+
2160
+        for (i = 0; i < nb_filtergraphs; i++)
2161
+            if (ist_in_filtergraph(filtergraphs[i], ist) &&
2162
+                configure_filtergraph(filtergraphs[i]) < 0) {
2163
+                av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
2164
+                exit_program(1);
2196 2165
             }
2197
-            filtered_frame = ist->filtered_frame;
2198
-            *filtered_frame= *decoded_frame; //for me_threshold
2199
-            avfilter_fill_frame_from_video_buffer_ref(filtered_frame, picref);
2200
-            filtered_frame->pts = av_rescale_q(picref->pts, ist_pts_tb, AV_TIME_BASE_Q);
2201
-            if (!ost->frame_aspect_ratio)
2202
-                ost->st->codec->sample_aspect_ratio = picref->video->sample_aspect_ratio;
2203
-            do_video_out(output_files[ost->file_index].ctx, ost, ist, filtered_frame);
2204
-            cont:
2205
-            avfilter_unref_buffer(picref);
2166
+    }
2167
+
2168
+    frame_sample_aspect= av_opt_ptr(avcodec_get_frame_class(), decoded_frame, "sample_aspect_ratio");
2169
+    for (i = 0; i < ist->nb_filters; i++) {
2170
+        int changed =      ist->st->codec->width   != ist->filters[i]->filter->outputs[0]->w
2171
+                        || ist->st->codec->height  != ist->filters[i]->filter->outputs[0]->h
2172
+                        || ist->st->codec->pix_fmt != ist->filters[i]->filter->outputs[0]->format;
2173
+        // XXX what an ugly hack
2174
+        if (ist->filters[i]->graph->nb_outputs == 1)
2175
+            ist->filters[i]->graph->outputs[0]->ost->last_quality = quality;
2176
+
2177
+        if (!frame_sample_aspect->num)
2178
+            *frame_sample_aspect = ist->st->sample_aspect_ratio;
2179
+        if (ist->dr1 && decoded_frame->type==FF_BUFFER_TYPE_USER && !changed) {
2180
+            FrameBuffer      *buf = decoded_frame->opaque;
2181
+            AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
2182
+                                        decoded_frame->data, decoded_frame->linesize,
2183
+                                        AV_PERM_READ | AV_PERM_PRESERVE,
2184
+                                        ist->st->codec->width, ist->st->codec->height,
2185
+                                        ist->st->codec->pix_fmt);
2186
+
2187
+            avfilter_copy_frame_props(fb, decoded_frame);
2188
+            fb->buf->priv           = buf;
2189
+            fb->buf->free           = filter_release_buffer;
2190
+
2191
+            buf->refcount++;
2192
+            av_buffersrc_buffer(ist->filters[i]->filter, fb);
2193
+        } else
2194
+        if(av_vsrc_buffer_add_frame(ist->filters[i]->filter, decoded_frame,AV_VSRC_BUF_FLAG_OVERWRITE)<0) {
2195
+            av_log(NULL, AV_LOG_FATAL, "Failed to inject frame into filter network\n");
2196
+            exit_program(1);
2206 2197
         }
2198
+
2207 2199
     }
2208 2200
 
2209
-fail:
2210 2201
     av_free(buffer_to_free);
2211 2202
     return ret;
2212 2203
 }
... ...
@@ -2224,12 +2571,12 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
2224 2224
     rate_emu_sleep(ist);
2225 2225
 
2226 2226
     for (i = 0; i < nb_output_streams; i++) {
2227
-        OutputStream *ost = &output_streams[i];
2227
+        OutputStream *ost = output_streams[i];
2228 2228
 
2229 2229
         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
2230 2230
             continue;
2231 2231
 
2232
-        do_subtitle_out(output_files[ost->file_index].ctx, ost, ist, &subtitle, pkt->pts);
2232
+        do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pkt->pts);
2233 2233
     }
2234 2234
 
2235 2235
     avsubtitle_free(&subtitle);
... ...
@@ -2237,9 +2584,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
2237 2237
 }
2238 2238
 
2239 2239
 /* pkt = NULL means EOF (needed to flush decoder buffers) */
2240
-static int output_packet(InputStream *ist,
2241
-                         OutputStream *ost_table, int nb_ostreams,
2242
-                         const AVPacket *pkt)
2240
+static int output_packet(InputStream *ist, const AVPacket *pkt)
2243 2241
 {
2244 2242
     int ret = 0, i;
2245 2243
     int got_output;
... ...
@@ -2356,8 +2701,8 @@ static int output_packet(InputStream *ist,
2356 2356
         ist->pts = ist->dts;
2357 2357
         ist->next_pts = ist->next_dts;
2358 2358
     }
2359
-    for (i = 0; pkt && i < nb_ostreams; i++) {
2360
-        OutputStream *ost = &ost_table[i];
2359
+    for (i = 0; pkt && i < nb_output_streams; i++) {
2360
+        OutputStream *ost = output_streams[i];
2361 2361
 
2362 2362
         if (!check_output_constraints(ist, ost) || ost->encoding_needed)
2363 2363
             continue;
... ...
@@ -2368,18 +2713,18 @@ static int output_packet(InputStream *ist,
2368 2368
     return 0;
2369 2369
 }
2370 2370
 
2371
-static void print_sdp(OutputFile *output_files, int n)
2371
+static void print_sdp(void)
2372 2372
 {
2373 2373
     char sdp[2048];
2374 2374
     int i;
2375
-    AVFormatContext **avc = av_malloc(sizeof(*avc) * n);
2375
+    AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files);
2376 2376
 
2377 2377
     if (!avc)
2378 2378
         exit_program(1);
2379
-    for (i = 0; i < n; i++)
2380
-        avc[i] = output_files[i].ctx;
2379
+    for (i = 0; i < nb_output_files; i++)
2380
+        avc[i] = output_files[i]->ctx;
2381 2381
 
2382
-    av_sdp_create(avc, n, sdp, sizeof(sdp));
2382
+    av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
2383 2383
     printf("SDP:\n%s\n", sdp);
2384 2384
     fflush(stdout);
2385 2385
     av_freep(&avc);
... ...
@@ -2430,11 +2775,10 @@ static void get_default_channel_layouts(OutputStream *ost, InputStream *ist)
2430 2430
 }
2431 2431
 
2432 2432
 
2433
-static int init_input_stream(int ist_index, OutputStream *output_streams, int nb_output_streams,
2434
-                             char *error, int error_len)
2433
+static int init_input_stream(int ist_index, char *error, int error_len)
2435 2434
 {
2436
-    InputStream *ist = &input_streams[ist_index];
2437 2435
     int i;
2436
+    InputStream *ist = input_streams[ist_index];
2438 2437
 
2439 2438
     if (ist->decoding_needed) {
2440 2439
         AVCodec *codec = ist->dec;
... ...
@@ -2463,7 +2807,7 @@ static int init_input_stream(int ist_index, OutputStream *output_streams, int nb
2463 2463
 
2464 2464
         if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2465 2465
             for (i = 0; i < nb_output_streams; i++) {
2466
-                OutputStream *ost = &output_streams[i];
2466
+                OutputStream *ost = output_streams[i];
2467 2467
                 if (ost->source_index == ist_index) {
2468 2468
                     if (!ist->st->codec->channel_layout || !ost->st->codec->channel_layout)
2469 2469
                         get_default_channel_layouts(ost, ist);
... ...
@@ -2481,8 +2825,24 @@ static int init_input_stream(int ist_index, OutputStream *output_streams, int nb
2481 2481
     return 0;
2482 2482
 }
2483 2483
 
2484
-static int transcode_init(OutputFile *output_files, int nb_output_files,
2485
-                          InputFile  *input_files,  int nb_input_files)
2484
+static InputStream *get_input_stream(OutputStream *ost)
2485
+{
2486
+    if (ost->source_index >= 0)
2487
+        return input_streams[ost->source_index];
2488
+
2489
+    if (ost->filter) {
2490
+        FilterGraph *fg = ost->filter->graph;
2491
+        int i;
2492
+
2493
+        for (i = 0; i < fg->nb_inputs; i++)
2494
+            if (fg->inputs[i]->ist->st->codec->codec_type == ost->st->codec->codec_type)
2495
+                return fg->inputs[i]->ist;
2496
+    }
2497
+
2498
+    return NULL;
2499
+}
2500
+
2501
+static int transcode_init(void)
2486 2502
 {
2487 2503
     int ret = 0, i, j, k;
2488 2504
     AVFormatContext *oc;
... ...
@@ -2494,15 +2854,15 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2494 2494
 
2495 2495
     /* init framerate emulation */
2496 2496
     for (i = 0; i < nb_input_files; i++) {
2497
-        InputFile *ifile = &input_files[i];
2497
+        InputFile *ifile = input_files[i];
2498 2498
         if (ifile->rate_emu)
2499 2499
             for (j = 0; j < ifile->nb_streams; j++)
2500
-                input_streams[j + ifile->ist_index].start = av_gettime();
2500
+                input_streams[j + ifile->ist_index]->start = av_gettime();
2501 2501
     }
2502 2502
 
2503 2503
     /* output stream init */
2504 2504
     for (i = 0; i < nb_output_files; i++) {
2505
-        oc = output_files[i].ctx;
2505
+        oc = output_files[i]->ctx;
2506 2506
         if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2507 2507
             av_dump_format(oc, i, oc->filename, 1);
2508 2508
             av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
... ...
@@ -2510,21 +2870,29 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2510 2510
         }
2511 2511
     }
2512 2512
 
2513
+    /* init complex filtergraphs */
2514
+    for (i = 0; i < nb_filtergraphs; i++)
2515
+        if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)
2516
+            return ret;
2517
+
2513 2518
     /* for each output stream, we compute the right encoding parameters */
2514 2519
     for (i = 0; i < nb_output_streams; i++) {
2515
-        ost = &output_streams[i];
2516
-        oc  = output_files[ost->file_index].ctx;
2517
-        ist = &input_streams[ost->source_index];
2520
+        ost = output_streams[i];
2521
+        oc  = output_files[ost->file_index]->ctx;
2522
+        ist = get_input_stream(ost);
2518 2523
 
2519 2524
         if (ost->attachment_filename)
2520 2525
             continue;
2521 2526
 
2522 2527
         codec  = ost->st->codec;
2523
-        icodec = ist->st->codec;
2524 2528
 
2525
-        ost->st->disposition          = ist->st->disposition;
2526
-        codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;
2527
-        codec->chroma_sample_location = icodec->chroma_sample_location;
2529
+        if (ist) {
2530
+            icodec = ist->st->codec;
2531
+
2532
+            ost->st->disposition          = ist->st->disposition;
2533
+            codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;
2534
+            codec->chroma_sample_location = icodec->chroma_sample_location;
2535
+        }
2528 2536
 
2529 2537
         if (ost->stream_copy) {
2530 2538
             uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
... ...
@@ -2625,7 +2993,8 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2625 2625
             if (!ost->enc)
2626 2626
                 ost->enc = avcodec_find_encoder(codec->codec_id);
2627 2627
 
2628
-            ist->decoding_needed = 1;
2628
+            if (ist)
2629
+                ist->decoding_needed = 1;
2629 2630
             ost->encoding_needed = 1;
2630 2631
 
2631 2632
             switch (codec->codec_type) {
... ...
@@ -2675,32 +3044,16 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2675 2675
                 ost->resample_channels    = icodec->channels;
2676 2676
                 break;
2677 2677
             case AVMEDIA_TYPE_VIDEO:
2678
-                if (codec->pix_fmt == PIX_FMT_NONE)
2679
-                    codec->pix_fmt = icodec->pix_fmt;
2680
-                choose_pixel_fmt(ost->st, ost->enc);
2681
-
2682
-                if (ost->st->codec->pix_fmt == PIX_FMT_NONE) {
2683
-                    av_log(NULL, AV_LOG_FATAL, "Video pixel format is unknown, stream cannot be encoded\n");
2684
-                    exit_program(1);
2685
-                }
2686
-
2687
-                if (!codec->width || !codec->height) {
2688
-                    codec->width  = icodec->width;
2689
-                    codec->height = icodec->height;
2690
-                }
2691
-
2692
-                ost->video_resample = codec->width   != icodec->width  ||
2693
-                                      codec->height  != icodec->height ||
2694
-                                      codec->pix_fmt != icodec->pix_fmt;
2695
-                if (ost->video_resample) {
2696
-                    codec->bits_per_raw_sample = frame_bits_per_raw_sample;
2678
+                if (!ost->filter) {
2679
+                    FilterGraph *fg;
2680
+                    fg = init_simple_filtergraph(ist, ost);
2681
+                    if (configure_video_filters(fg)) {
2682
+                        av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
2683
+                        exit(1);
2684
+                    }
2697 2685
                 }
2698 2686
 
2699
-                ost->resample_height  = icodec->height;
2700
-                ost->resample_width   = icodec->width;
2701
-                ost->resample_pix_fmt = icodec->pix_fmt;
2702
-
2703
-                if (!ost->frame_rate.num)
2687
+                if (ist && !ost->frame_rate.num)
2704 2688
                     ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational) { 25, 1 };
2705 2689
                 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
2706 2690
                     int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
... ...
@@ -2717,10 +3070,20 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2717 2717
                                                          AV_TIME_BASE_Q,
2718 2718
                                                          codec->time_base);
2719 2719
 
2720
-                if (configure_video_filters(ist, ost)) {
2721
-                    av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
2722
-                    exit_program(1);
2720
+                codec->width  = ost->filter->filter->inputs[0]->w;
2721
+                codec->height = ost->filter->filter->inputs[0]->h;
2722
+                codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
2723
+                    ost->frame_aspect_ratio ? // overridden by the -aspect cli option
2724
+                    av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
2725
+                    ost->filter->filter->inputs[0]->sample_aspect_ratio;
2726
+                codec->pix_fmt = ost->filter->filter->inputs[0]->format;
2727
+
2728
+                if (codec->width   != icodec->width  ||
2729
+                    codec->height  != icodec->height ||
2730
+                    codec->pix_fmt != icodec->pix_fmt) {
2731
+                    codec->bits_per_raw_sample = frame_bits_per_raw_sample;
2723 2732
                 }
2733
+
2724 2734
                 break;
2725 2735
             case AVMEDIA_TYPE_SUBTITLE:
2726 2736
                 codec->time_base = (AVRational){1, 1000};
... ...
@@ -2766,17 +3129,20 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2766 2766
 
2767 2767
     /* open each encoder */
2768 2768
     for (i = 0; i < nb_output_streams; i++) {
2769
-        ost = &output_streams[i];
2769
+        ost = output_streams[i];
2770 2770
         if (ost->encoding_needed) {
2771 2771
             AVCodec      *codec = ost->enc;
2772
-            AVCodecContext *dec = input_streams[ost->source_index].st->codec;
2772
+            AVCodecContext *dec = NULL;
2773 2773
             if (!codec) {
2774 2774
                 snprintf(error, sizeof(error), "Encoder (codec %s) not found for output stream #%d:%d",
2775 2775
                          avcodec_get_name(ost->st->codec->codec_id), ost->file_index, ost->index);
2776 2776
                 ret = AVERROR(EINVAL);
2777 2777
                 goto dump_format;
2778 2778
             }
2779
-            if (dec->subtitle_header) {
2779
+
2780
+            if ((ist = get_input_stream(ost)))
2781
+                dec = ist->st->codec;
2782
+            if (dec && dec->subtitle_header) {
2780 2783
                 ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
2781 2784
                 if (!ost->st->codec->subtitle_header) {
2782 2785
                     ret = AVERROR(ENOMEM);
... ...
@@ -2801,24 +3167,24 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2801 2801
             extra_size += ost->st->codec->extradata_size;
2802 2802
 
2803 2803
             if (ost->st->codec->me_threshold)
2804
-                input_streams[ost->source_index].st->codec->debug |= FF_DEBUG_MV;
2804
+                input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV;
2805 2805
         }
2806 2806
     }
2807 2807
 
2808 2808
     /* init input streams */
2809 2809
     for (i = 0; i < nb_input_streams; i++)
2810
-        if ((ret = init_input_stream(i, output_streams, nb_output_streams, error, sizeof(error))) < 0)
2810
+        if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
2811 2811
             goto dump_format;
2812 2812
 
2813 2813
     /* discard unused programs */
2814 2814
     for (i = 0; i < nb_input_files; i++) {
2815
-        InputFile *ifile = &input_files[i];
2815
+        InputFile *ifile = input_files[i];
2816 2816
         for (j = 0; j < ifile->ctx->nb_programs; j++) {
2817 2817
             AVProgram *p = ifile->ctx->programs[j];
2818 2818
             int discard  = AVDISCARD_ALL;
2819 2819
 
2820 2820
             for (k = 0; k < p->nb_stream_indexes; k++)
2821
-                if (!input_streams[ifile->ist_index + p->stream_index[k]].discard) {
2821
+                if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
2822 2822
                     discard = AVDISCARD_DEFAULT;
2823 2823
                     break;
2824 2824
                 }
... ...
@@ -2828,14 +3194,14 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2828 2828
 
2829 2829
     /* open files and write file headers */
2830 2830
     for (i = 0; i < nb_output_files; i++) {
2831
-        oc = output_files[i].ctx;
2831
+        oc = output_files[i]->ctx;
2832 2832
         oc->interrupt_callback = int_cb;
2833
-        if (avformat_write_header(oc, &output_files[i].opts) < 0) {
2833
+        if (avformat_write_header(oc, &output_files[i]->opts) < 0) {
2834 2834
             snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?)", i);
2835 2835
             ret = AVERROR(EINVAL);
2836 2836
             goto dump_format;
2837 2837
         }
2838
-//        assert_avoptions(output_files[i].opts);
2838
+//         assert_avoptions(output_files[i]->opts);
2839 2839
         if (strcmp(oc->oformat->name, "rtp")) {
2840 2840
             want_sdp = 0;
2841 2841
         }
... ...
@@ -2845,13 +3211,31 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2845 2845
     /* dump the file output parameters - cannot be done before in case
2846 2846
        of stream copy */
2847 2847
     for (i = 0; i < nb_output_files; i++) {
2848
-        av_dump_format(output_files[i].ctx, i, output_files[i].ctx->filename, 1);
2848
+        av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2849 2849
     }
2850 2850
 
2851 2851
     /* dump the stream mapping */
2852 2852
     av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
2853
+    for (i = 0; i < nb_input_streams; i++) {
2854
+        ist = input_streams[i];
2855
+
2856
+        for (j = 0; j < ist->nb_filters; j++) {
2857
+            AVFilterLink *link = ist->filters[j]->filter->outputs[0];
2858
+            if (ist->filters[j]->graph->graph_desc) {
2859
+                av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d (%s) -> %s",
2860
+                       ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
2861
+                       link->dst->filter->name);
2862
+                if (link->dst->input_count > 1)
2863
+                    av_log(NULL, AV_LOG_INFO, ":%s", link->dstpad->name);
2864
+                if (nb_filtergraphs > 1)
2865
+                    av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
2866
+                av_log(NULL, AV_LOG_INFO, "\n");
2867
+            }
2868
+        }
2869
+    }
2870
+
2853 2871
     for (i = 0; i < nb_output_streams; i++) {
2854
-        ost = &output_streams[i];
2872
+        ost = output_streams[i];
2855 2873
 
2856 2874
         if (ost->attachment_filename) {
2857 2875
             /* an attached file */
... ...
@@ -2859,9 +3243,24 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2859 2859
                    ost->attachment_filename, ost->file_index, ost->index);
2860 2860
             continue;
2861 2861
         }
2862
+
2863
+        if (ost->filter && ost->filter->graph->graph_desc) {
2864
+            /* output from a complex graph */
2865
+            AVFilterLink *link = ost->filter->filter->inputs[0];
2866
+            av_log(NULL, AV_LOG_INFO, "  %s", link->src->filter->name);
2867
+            if (link->src->output_count > 1)
2868
+                av_log(NULL, AV_LOG_INFO, ":%s", link->srcpad->name);
2869
+            if (nb_filtergraphs > 1)
2870
+                av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
2871
+
2872
+            av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
2873
+                   ost->index, ost->enc ? ost->enc->name : "?");
2874
+            continue;
2875
+        }
2876
+
2862 2877
         av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
2863
-               input_streams[ost->source_index].file_index,
2864
-               input_streams[ost->source_index].st->index,
2878
+               input_streams[ost->source_index]->file_index,
2879
+               input_streams[ost->source_index]->st->index,
2865 2880
                ost->file_index,
2866 2881
                ost->index);
2867 2882
         if (ost->audio_channels_mapped) {
... ...
@@ -2873,15 +3272,15 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2873 2873
                     av_log(NULL, AV_LOG_INFO, " %d", ost->audio_channels_map[j]);
2874 2874
             av_log(NULL, AV_LOG_INFO, "]");
2875 2875
         }
2876
-        if (ost->sync_ist != &input_streams[ost->source_index])
2876
+        if (ost->sync_ist != input_streams[ost->source_index])
2877 2877
             av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
2878 2878
                    ost->sync_ist->file_index,
2879 2879
                    ost->sync_ist->st->index);
2880 2880
         if (ost->stream_copy)
2881 2881
             av_log(NULL, AV_LOG_INFO, " (copy)");
2882 2882
         else
2883
-            av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index].dec ?
2884
-                   input_streams[ost->source_index].dec->name : "?",
2883
+            av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?
2884
+                   input_streams[ost->source_index]->dec->name : "?",
2885 2885
                    ost->enc ? ost->enc->name : "?");
2886 2886
         av_log(NULL, AV_LOG_INFO, "\n");
2887 2887
     }
... ...
@@ -2892,7 +3291,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2892 2892
     }
2893 2893
 
2894 2894
     if (want_sdp) {
2895
-        print_sdp(output_files, nb_output_files);
2895
+        print_sdp();
2896 2896
     }
2897 2897
 
2898 2898
     return 0;
... ...
@@ -2901,8 +3300,7 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
2901 2901
 /*
2902 2902
  * The following code is the main loop of the file converter
2903 2903
  */
2904
-static int transcode(OutputFile *output_files, int nb_output_files,
2905
-                     InputFile  *input_files,  int nb_input_files)
2904
+static int transcode(void)
2906 2905
 {
2907 2906
     int ret, i;
2908 2907
     AVFormatContext *is, *os;
... ...
@@ -2916,7 +3314,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
2916 2916
     if (!(no_packet = av_mallocz(nb_input_files)))
2917 2917
         exit_program(1);
2918 2918
 
2919
-    ret = transcode_init(output_files, nb_output_files, input_files, nb_input_files);
2919
+    ret = transcode_init();
2920 2920
     if (ret < 0)
2921 2921
         goto fail;
2922 2922
 
... ...
@@ -2927,14 +3325,12 @@ static int transcode(OutputFile *output_files, int nb_output_files,
2927 2927
     timer_start = av_gettime();
2928 2928
 
2929 2929
     for (; received_sigterm == 0;) {
2930
-        int file_index, ist_index;
2930
+        int file_index, ist_index, past_recording_time = 1;
2931 2931
         AVPacket pkt;
2932 2932
         int64_t ipts_min;
2933
-        double opts_min;
2934 2933
         int64_t cur_time= av_gettime();
2935 2934
 
2936 2935
         ipts_min = INT64_MAX;
2937
-        opts_min = 1e100;
2938 2936
         /* if 'q' pressed, exits */
2939 2937
         if (!using_stdin) {
2940 2938
             static int64_t last_time;
... ...
@@ -2974,15 +3370,15 @@ static int transcode(OutputFile *output_files, int nb_output_files,
2974 2974
                     (n = sscanf(buf, "%63[^ ] %lf %255[^ ] %255[^\n]", target, &time, command, arg)) >= 3) {
2975 2975
                     av_log(NULL, AV_LOG_DEBUG, "Processing command target:%s time:%f command:%s arg:%s",
2976 2976
                            target, time, command, arg);
2977
-                    for (i = 0; i < nb_output_streams; i++) {
2978
-                        ost = &output_streams[i];
2979
-                        if (ost->graph) {
2977
+                    for (i = 0; i < nb_filtergraphs; i++) {
2978
+                        FilterGraph *fg = filtergraphs[i];
2979
+                        if (fg->graph) {
2980 2980
                             if (time < 0) {
2981
-                                ret = avfilter_graph_send_command(ost->graph, target, command, arg, buf, sizeof(buf),
2981
+                                ret = avfilter_graph_send_command(fg->graph, target, command, arg, buf, sizeof(buf),
2982 2982
                                                                   key == 'c' ? AVFILTER_CMD_FLAG_ONE : 0);
2983 2983
                                 fprintf(stderr, "Command reply for stream %d: ret:%d res:%s\n", i, ret, buf);
2984 2984
                             } else {
2985
-                                ret = avfilter_graph_queue_command(ost->graph, target, command, arg, 0, time);
2985
+                                ret = avfilter_graph_queue_command(fg->graph, target, command, arg, 0, time);
2986 2986
                             }
2987 2987
                         }
2988 2988
                     }
... ...
@@ -2995,7 +3391,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
2995 2995
             if (key == 'd' || key == 'D'){
2996 2996
                 int debug=0;
2997 2997
                 if(key == 'D') {
2998
-                    debug = input_streams[0].st->codec->debug<<1;
2998
+                    debug = input_streams[0]->st->codec->debug<<1;
2999 2999
                     if(!debug) debug = 1;
3000 3000
                     while(debug & (FF_DEBUG_DCT_COEFF|FF_DEBUG_VIS_QP|FF_DEBUG_VIS_MB_TYPE)) //unsupported, would just crash
3001 3001
                         debug += debug;
... ...
@@ -3003,10 +3399,10 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3003 3003
                     if(scanf("%d", &debug)!=1)
3004 3004
                         fprintf(stderr,"error parsing debug value\n");
3005 3005
                 for(i=0;i<nb_input_streams;i++) {
3006
-                    input_streams[i].st->codec->debug = debug;
3006
+                    input_streams[i]->st->codec->debug = debug;
3007 3007
                 }
3008 3008
                 for(i=0;i<nb_output_streams;i++) {
3009
-                    ost = &output_streams[i];
3009
+                    ost = output_streams[i];
3010 3010
                     ost->st->codec->debug = debug;
3011 3011
                 }
3012 3012
                 if(debug) av_log_set_level(AV_LOG_DEBUG);
... ...
@@ -3026,38 +3422,40 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3026 3026
             }
3027 3027
         }
3028 3028
 
3029
-        /* select the stream that we must read now by looking at the
3030
-           smallest output pts */
3031
-        file_index = -1;
3029
+        /* check if there's any stream where output is still needed */
3032 3030
         for (i = 0; i < nb_output_streams; i++) {
3033 3031
             OutputFile *of;
3034
-            int64_t ipts;
3035
-            double  opts;
3036
-            ost = &output_streams[i];
3037
-            of = &output_files[ost->file_index];
3038
-            os = output_files[ost->file_index].ctx;
3039
-            ist = &input_streams[ost->source_index];
3040
-            if (ost->is_past_recording_time || no_packet[ist->file_index] ||
3032
+            ost = output_streams[i];
3033
+            of  = output_files[ost->file_index];
3034
+            os  = output_files[ost->file_index]->ctx;
3035
+            if (ost->is_past_recording_time ||
3041 3036
                 (os->pb && avio_tell(os->pb) >= of->limit_filesize))
3042 3037
                 continue;
3043
-            opts = ost->st->pts.val * av_q2d(ost->st->time_base);
3038
+            if (ost->frame_number > ost->max_frames) {
3039
+                int j;
3040
+                for (j = 0; j < of->ctx->nb_streams; j++)
3041
+                    output_streams[of->ost_index + j]->is_past_recording_time = 1;
3042
+                continue;
3043
+            }
3044
+            past_recording_time = 0;
3045
+        }
3046
+        if (past_recording_time)
3047
+            break;
3048
+
3049
+        /* select the stream that we must read now by looking at the
3050
+           smallest output pts */
3051
+        file_index = -1;
3052
+        for (i = 0; i < nb_input_streams; i++) {
3053
+            int64_t ipts;
3054
+            ist = input_streams[i];
3044 3055
             ipts = ist->pts;
3045
-            if (!input_files[ist->file_index].eof_reached) {
3056
+            if (ist->discard || no_packet[ist->file_index])
3057
+                continue;
3058
+            if (!input_files[ist->file_index]->eof_reached) {
3046 3059
                 if (ipts < ipts_min) {
3047 3060
                     ipts_min = ipts;
3048
-                    if (input_sync)
3049
-                        file_index = ist->file_index;
3061
+                    file_index = ist->file_index;
3050 3062
                 }
3051
-                if (opts < opts_min) {
3052
-                    opts_min = opts;
3053
-                    if (!input_sync) file_index = ist->file_index;
3054
-                }
3055
-            }
3056
-            if (ost->frame_number >= ost->max_frames) {
3057
-                int j;
3058
-                for (j = 0; j < of->ctx->nb_streams; j++)
3059
-                    output_streams[of->ost_index + j].is_past_recording_time = 1;
3060
-                continue;
3061 3063
             }
3062 3064
         }
3063 3065
         /* if none, if is finished */
... ...
@@ -3072,7 +3470,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3072 3072
         }
3073 3073
 
3074 3074
         /* read a frame from it and output it in the fifo */
3075
-        is  = input_files[file_index].ctx;
3075
+        is  = input_files[file_index]->ctx;
3076 3076
         ret = av_read_frame(is, &pkt);
3077 3077
         if (ret == AVERROR(EAGAIN)) {
3078 3078
             no_packet[file_index] = 1;
... ...
@@ -3080,7 +3478,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3080 3080
             continue;
3081 3081
         }
3082 3082
         if (ret < 0) {
3083
-            input_files[file_index].eof_reached = 1;
3083
+            input_files[file_index]->eof_reached = 1;
3084 3084
             if (opt_shortest)
3085 3085
                 break;
3086 3086
             else
... ...
@@ -3096,17 +3494,17 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3096 3096
         }
3097 3097
         /* the following test is needed in case new streams appear
3098 3098
            dynamically in stream : we ignore them */
3099
-        if (pkt.stream_index >= input_files[file_index].nb_streams)
3099
+        if (pkt.stream_index >= input_files[file_index]->nb_streams)
3100 3100
             goto discard_packet;
3101
-        ist_index = input_files[file_index].ist_index + pkt.stream_index;
3102
-        ist = &input_streams[ist_index];
3101
+        ist_index = input_files[file_index]->ist_index + pkt.stream_index;
3102
+        ist = input_streams[ist_index];
3103 3103
         if (ist->discard)
3104 3104
             goto discard_packet;
3105 3105
 
3106 3106
         if (pkt.dts != AV_NOPTS_VALUE)
3107
-            pkt.dts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3107
+            pkt.dts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3108 3108
         if (pkt.pts != AV_NOPTS_VALUE)
3109
-            pkt.pts += av_rescale_q(input_files[ist->file_index].ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3109
+            pkt.pts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3110 3110
 
3111 3111
         if (pkt.pts != AV_NOPTS_VALUE)
3112 3112
             pkt.pts *= ist->ts_scale;
... ...
@@ -3120,7 +3518,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3120 3120
                     av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &ist->st->time_base),
3121 3121
                     av_ts2str(pkt.pts), av_ts2timestr(pkt.pts, &ist->st->time_base),
3122 3122
                     av_ts2str(pkt.dts), av_ts2timestr(pkt.dts, &ist->st->time_base),
3123
-                    input_files[ist->file_index].ts_offset);
3123
+                    input_files[ist->file_index]->ts_offset);
3124 3124
         }
3125 3125
 
3126 3126
         if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE && !copy_ts) {
... ...
@@ -3131,10 +3529,10 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3131 3131
                 (delta > 1LL*dts_delta_threshold*AV_TIME_BASE &&
3132 3132
                  ist->st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) ||
3133 3133
                 pkt_dts+1<ist->pts){
3134
-                input_files[ist->file_index].ts_offset -= delta;
3134
+                input_files[ist->file_index]->ts_offset -= delta;
3135 3135
                 av_log(NULL, AV_LOG_DEBUG,
3136 3136
                        "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
3137
-                       delta, input_files[ist->file_index].ts_offset);
3137
+                       delta, input_files[ist->file_index]->ts_offset);
3138 3138
                 pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3139 3139
                 if (pkt.pts != AV_NOPTS_VALUE)
3140 3140
                     pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
... ...
@@ -3160,8 +3558,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3160 3160
         }
3161 3161
 
3162 3162
         // fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size);
3163
-        if (output_packet(ist, output_streams, nb_output_streams, &pkt) < 0) {
3164
-
3163
+        if (output_packet(ist, &pkt) < 0 || poll_filters() < 0) {
3165 3164
             av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
3166 3165
                    ist->file_index, ist->st->index);
3167 3166
             if (exit_on_error)
... ...
@@ -3174,42 +3571,42 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3174 3174
         av_free_packet(&pkt);
3175 3175
 
3176 3176
         /* dump report by using the output first video and audio streams */
3177
-        print_report(output_files, output_streams, nb_output_streams, 0, timer_start, cur_time);
3177
+        print_report(0, timer_start, cur_time);
3178 3178
     }
3179 3179
 
3180 3180
     /* at the end of stream, we must flush the decoder buffers */
3181 3181
     for (i = 0; i < nb_input_streams; i++) {
3182
-        ist = &input_streams[i];
3182
+        ist = input_streams[i];
3183 3183
         if (ist->decoding_needed) {
3184
-            output_packet(ist, output_streams, nb_output_streams, NULL);
3184
+            output_packet(ist, NULL);
3185 3185
         }
3186 3186
     }
3187
-    flush_encoders(output_streams, nb_output_streams);
3187
+    poll_filters();
3188
+    flush_encoders();
3188 3189
 
3189 3190
     term_exit();
3190 3191
 
3191 3192
     /* write the trailer if needed and close file */
3192 3193
     for (i = 0; i < nb_output_files; i++) {
3193
-        os = output_files[i].ctx;
3194
+        os = output_files[i]->ctx;
3194 3195
         av_write_trailer(os);
3195 3196
     }
3196 3197
 
3197 3198
     /* dump report by using the first video and audio streams */
3198
-    print_report(output_files, output_streams, nb_output_streams, 1, timer_start, av_gettime());
3199
+    print_report(1, timer_start, av_gettime());
3199 3200
 
3200 3201
     /* close each encoder */
3201 3202
     for (i = 0; i < nb_output_streams; i++) {
3202
-        ost = &output_streams[i];
3203
+        ost = output_streams[i];
3203 3204
         if (ost->encoding_needed) {
3204 3205
             av_freep(&ost->st->codec->stats_in);
3205 3206
             avcodec_close(ost->st->codec);
3206 3207
         }
3207
-        avfilter_graph_free(&ost->graph);
3208 3208
     }
3209 3209
 
3210 3210
     /* close each decoder */
3211 3211
     for (i = 0; i < nb_input_streams; i++) {
3212
-        ist = &input_streams[i];
3212
+        ist = input_streams[i];
3213 3213
         if (ist->decoding_needed) {
3214 3214
             avcodec_close(ist->st->codec);
3215 3215
         }
... ...
@@ -3223,7 +3620,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
3223 3223
 
3224 3224
     if (output_streams) {
3225 3225
         for (i = 0; i < nb_output_streams; i++) {
3226
-            ost = &output_streams[i];
3226
+            ost = output_streams[i];
3227 3227
             if (ost) {
3228 3228
                 if (ost->stream_copy)
3229 3229
                     av_freep(&ost->st->codec->extradata);
... ...
@@ -3314,13 +3711,13 @@ static int opt_map(OptionsContext *o, const char *opt, const char *arg)
3314 3314
         }
3315 3315
         if (*sync)
3316 3316
             sync++;
3317
-        for (i = 0; i < input_files[sync_file_idx].nb_streams; i++)
3318
-            if (check_stream_specifier(input_files[sync_file_idx].ctx,
3319
-                                       input_files[sync_file_idx].ctx->streams[i], sync) == 1) {
3317
+        for (i = 0; i < input_files[sync_file_idx]->nb_streams; i++)
3318
+            if (check_stream_specifier(input_files[sync_file_idx]->ctx,
3319
+                                       input_files[sync_file_idx]->ctx->streams[i], sync) == 1) {
3320 3320
                 sync_stream_idx = i;
3321 3321
                 break;
3322 3322
             }
3323
-        if (i == input_files[sync_file_idx].nb_streams) {
3323
+        if (i == input_files[sync_file_idx]->nb_streams) {
3324 3324
             av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
3325 3325
                                        "match any streams.\n", arg);
3326 3326
             exit_program(1);
... ...
@@ -3328,6 +3725,18 @@ static int opt_map(OptionsContext *o, const char *opt, const char *arg)
3328 3328
     }
3329 3329
 
3330 3330
 
3331
+    if (map[0] == '[') {
3332
+        /* this mapping refers to lavfi output */
3333
+        const char *c = map + 1;
3334
+        o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
3335
+                                    &o->nb_stream_maps, o->nb_stream_maps + 1);
3336
+        m = &o->stream_maps[o->nb_stream_maps - 1];
3337
+        m->linklabel = av_get_token(&c, "]");
3338
+        if (!m->linklabel) {
3339
+            av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
3340
+            exit_program(1);
3341
+        }
3342
+    } else {
3331 3343
     file_idx = strtol(map, &p, 0);
3332 3344
     if (file_idx >= nb_input_files || file_idx < 0) {
3333 3345
         av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
... ...
@@ -3338,14 +3747,14 @@ static int opt_map(OptionsContext *o, const char *opt, const char *arg)
3338 3338
         for (i = 0; i < o->nb_stream_maps; i++) {
3339 3339
             m = &o->stream_maps[i];
3340 3340
             if (file_idx == m->file_index &&
3341
-                check_stream_specifier(input_files[m->file_index].ctx,
3342
-                                       input_files[m->file_index].ctx->streams[m->stream_index],
3341
+                check_stream_specifier(input_files[m->file_index]->ctx,
3342
+                                       input_files[m->file_index]->ctx->streams[m->stream_index],
3343 3343
                                        *p == ':' ? p + 1 : p) > 0)
3344 3344
                 m->disabled = 1;
3345 3345
         }
3346 3346
     else
3347
-        for (i = 0; i < input_files[file_idx].nb_streams; i++) {
3348
-            if (check_stream_specifier(input_files[file_idx].ctx, input_files[file_idx].ctx->streams[i],
3347
+        for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
3348
+            if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
3349 3349
                         *p == ':' ? p + 1 : p) <= 0)
3350 3350
                 continue;
3351 3351
             o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
... ...
@@ -3363,6 +3772,7 @@ static int opt_map(OptionsContext *o, const char *opt, const char *arg)
3363 3363
                 m->sync_stream_index = i;
3364 3364
             }
3365 3365
         }
3366
+    }
3366 3367
 
3367 3368
     if (!m) {
3368 3369
         av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
... ...
@@ -3422,12 +3832,12 @@ static int opt_map_channel(OptionsContext *o, const char *opt, const char *arg)
3422 3422
         exit_program(1);
3423 3423
     }
3424 3424
     if (m->stream_idx < 0 ||
3425
-        m->stream_idx >= input_files[m->file_idx].nb_streams) {
3425
+        m->stream_idx >= input_files[m->file_idx]->nb_streams) {
3426 3426
         av_log(NULL, AV_LOG_FATAL, "mapchan: invalid input file stream index #%d.%d\n",
3427 3427
                m->file_idx, m->stream_idx);
3428 3428
         exit_program(1);
3429 3429
     }
3430
-    st = input_files[m->file_idx].ctx->streams[m->stream_idx];
3430
+    st = input_files[m->file_idx]->ctx->streams[m->stream_idx];
3431 3431
     if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
3432 3432
         av_log(NULL, AV_LOG_FATAL, "mapchan: stream #%d.%d is not an audio stream.\n",
3433 3433
                m->file_idx, m->stream_idx);
... ...
@@ -3614,10 +4024,14 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
3614 3614
     for (i = 0; i < ic->nb_streams; i++) {
3615 3615
         AVStream *st = ic->streams[i];
3616 3616
         AVCodecContext *dec = st->codec;
3617
-        InputStream *ist;
3617
+        InputStream *ist = av_mallocz(sizeof(*ist));
3618
+
3619
+        if (!ist)
3620
+            exit_program(1);
3618 3621
 
3619 3622
         input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
3620
-        ist = &input_streams[nb_input_streams - 1];
3623
+        input_streams[nb_input_streams - 1] = ist;
3624
+
3621 3625
         ist->st = st;
3622 3626
         ist->file_index = nb_input_files;
3623 3627
         ist->discard = 1;
... ...
@@ -3645,6 +4059,10 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
3645 3645
                 dec->flags |= CODEC_FLAG_EMU_EDGE;
3646 3646
             }
3647 3647
 
3648
+            ist->resample_height  = dec->height;
3649
+            ist->resample_width   = dec->width;
3650
+            ist->resample_pix_fmt = dec->pix_fmt;
3651
+
3648 3652
             break;
3649 3653
         case AVMEDIA_TYPE_AUDIO:
3650 3654
         case AVMEDIA_TYPE_DATA:
... ...
@@ -3819,11 +4237,14 @@ static int opt_input_file(OptionsContext *o, const char *opt, const char *filena
3819 3819
     av_dump_format(ic, nb_input_files, filename, 0);
3820 3820
 
3821 3821
     input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1);
3822
-    input_files[nb_input_files - 1].ctx        = ic;
3823
-    input_files[nb_input_files - 1].ist_index  = nb_input_streams - ic->nb_streams;
3824
-    input_files[nb_input_files - 1].ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
3825
-    input_files[nb_input_files - 1].nb_streams = ic->nb_streams;
3826
-    input_files[nb_input_files - 1].rate_emu   = o->rate_emu;
3822
+    if (!(input_files[nb_input_files - 1] = av_mallocz(sizeof(*input_files[0]))))
3823
+        exit_program(1);
3824
+
3825
+    input_files[nb_input_files - 1]->ctx        = ic;
3826
+    input_files[nb_input_files - 1]->ist_index  = nb_input_streams - ic->nb_streams;
3827
+    input_files[nb_input_files - 1]->ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
3828
+    input_files[nb_input_files - 1]->nb_streams = ic->nb_streams;
3829
+    input_files[nb_input_files - 1]->rate_emu   = o->rate_emu;
3827 3830
 
3828 3831
     for (i = 0; i < o->nb_dump_attachment; i++) {
3829 3832
         int j;
... ...
@@ -3947,7 +4368,10 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
3947 3947
 
3948 3948
     output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams,
3949 3949
                                 nb_output_streams + 1);
3950
-    ost = &output_streams[nb_output_streams - 1];
3950
+    if (!(ost = av_mallocz(sizeof(*ost))))
3951
+        exit_program(1);
3952
+    output_streams[nb_output_streams - 1] = ost;
3953
+
3951 3954
     ost->file_index = nb_output_files;
3952 3955
     ost->index      = idx;
3953 3956
     ost->st         = st;
... ...
@@ -4028,11 +4452,13 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
4028 4028
 
4029 4029
     ost->source_index = source_index;
4030 4030
     if (source_index >= 0) {
4031
-        ost->sync_ist = &input_streams[source_index];
4032
-        input_streams[source_index].discard = 0;
4033
-        input_streams[source_index].st->discard = AVDISCARD_NONE;
4031
+        ost->sync_ist = input_streams[source_index];
4032
+        input_streams[source_index]->discard = 0;
4033
+        input_streams[source_index]->st->discard = AVDISCARD_NONE;
4034 4034
     }
4035 4035
 
4036
+    ost->pix_fmts[0] = ost->pix_fmts[1] = PIX_FMT_NONE;
4037
+
4036 4038
     return ost;
4037 4039
 }
4038 4040
 
... ...
@@ -4216,7 +4642,7 @@ static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc, in
4216 4216
     /* check for channel mapping for this audio stream */
4217 4217
     for (n = 0; n < o->nb_audio_channel_maps; n++) {
4218 4218
         AudioChannelMap *map = &o->audio_channel_maps[n];
4219
-        InputStream *ist = &input_streams[ost->source_index];
4219
+        InputStream *ist = input_streams[ost->source_index];
4220 4220
         if ((map->channel_idx == -1 || (ist->file_index == map->file_idx && ist->st->index == map->stream_idx)) &&
4221 4221
             (map->ofile_idx   == -1 || ost->file_index == map->ofile_idx) &&
4222 4222
             (map->ostream_idx == -1 || ost->st->index  == map->ostream_idx)) {
... ...
@@ -4370,15 +4796,43 @@ static int read_ffserver_streams(OptionsContext *o, AVFormatContext *s, const ch
4370 4370
     return 0;
4371 4371
 }
4372 4372
 
4373
+static void init_output_filter(OutputFilter *ofilter, OptionsContext *o,
4374
+                               AVFormatContext *oc)
4375
+{
4376
+    OutputStream *ost;
4377
+
4378
+    if (ofilter->out_tmp->filter_ctx->output_pads[ofilter->out_tmp->pad_idx].type != AVMEDIA_TYPE_VIDEO) {
4379
+        av_log(NULL, AV_LOG_FATAL, "Only video filters are supported currently.\n");
4380
+        exit_program(1);
4381
+    }
4382
+
4383
+    ost               = new_video_stream(o, oc, -1);
4384
+    ost->source_index = -1;
4385
+    ost->filter       = ofilter;
4386
+
4387
+    ofilter->ost      = ost;
4388
+
4389
+    if (configure_output_filter(ofilter->graph, ofilter, ofilter->out_tmp) < 0) {
4390
+        av_log(NULL, AV_LOG_FATAL, "Error configuring filter.\n");
4391
+        exit_program(1);
4392
+    }
4393
+    avfilter_inout_free(&ofilter->out_tmp);
4394
+}
4395
+
4373 4396
 static void opt_output_file(void *optctx, const char *filename)
4374 4397
 {
4375 4398
     OptionsContext *o = optctx;
4376 4399
     AVFormatContext *oc;
4377
-    int i, err;
4400
+    int i, j, err;
4378 4401
     AVOutputFormat *file_oformat;
4379 4402
     OutputStream *ost;
4380 4403
     InputStream  *ist;
4381 4404
 
4405
+    if (configure_complex_filters() < 0) {
4406
+        av_log(NULL, AV_LOG_FATAL, "Error configuring filters.\n");
4407
+        exit_program(1);
4408
+    }
4409
+
4382 4410
     if (!strcmp(filename, "-"))
4383 4411
         filename = "pipe:";
4384 4412
 
... ...
@@ -4390,6 +4844,24 @@ static void opt_output_file(void *optctx, const char *filename)
4390 4390
     file_oformat= oc->oformat;
4391 4391
     oc->interrupt_callback = int_cb;
4392 4392
 
4393
+    /* create streams for all unlabeled output pads */
4394
+    for (i = 0; i < nb_filtergraphs; i++) {
4395
+        FilterGraph *fg = filtergraphs[i];
4396
+        for (j = 0; j < fg->nb_outputs; j++) {
4397
+            OutputFilter *ofilter = fg->outputs[j];
4398
+
4399
+            if (!ofilter->out_tmp || ofilter->out_tmp->name)
4400
+                continue;
4401
+
4402
+            switch (ofilter->out_tmp->filter_ctx->output_pads[ofilter->out_tmp->pad_idx].type) {
4403
+            case AVMEDIA_TYPE_VIDEO:    o->video_disable    = 1; break;
4404
+            case AVMEDIA_TYPE_AUDIO:    o->audio_disable    = 1; break;
4405
+            case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
4406
+            }
4407
+            init_output_filter(ofilter, o, oc);
4408
+        }
4409
+    }
4410
+
4393 4411
     if (!strcmp(file_oformat->name, "ffm") &&
4394 4412
         av_strstart(filename, "http:", NULL)) {
4395 4413
         int j;
... ...
@@ -4401,9 +4873,9 @@ static void opt_output_file(void *optctx, const char *filename)
4401 4401
             exit_program(1);
4402 4402
         }
4403 4403
         for(j = nb_output_streams - oc->nb_streams; j < nb_output_streams; j++) {
4404
-            ost = &output_streams[j];
4404
+            ost = output_streams[j];
4405 4405
             for (i = 0; i < nb_input_streams; i++) {
4406
-                ist = &input_streams[i];
4406
+                ist = input_streams[i];
4407 4407
                 if(ist->st->codec->codec_type == ost->st->codec->codec_type){
4408 4408
                     ost->sync_ist= ist;
4409 4409
                     ost->source_index= i;
... ...
@@ -4424,7 +4896,7 @@ static void opt_output_file(void *optctx, const char *filename)
4424 4424
         if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) {
4425 4425
             int area = 0, idx = -1;
4426 4426
             for (i = 0; i < nb_input_streams; i++) {
4427
-                ist = &input_streams[i];
4427
+                ist = input_streams[i];
4428 4428
                 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
4429 4429
                     ist->st->codec->width * ist->st->codec->height > area) {
4430 4430
                     area = ist->st->codec->width * ist->st->codec->height;
... ...
@@ -4439,7 +4911,7 @@ static void opt_output_file(void *optctx, const char *filename)
4439 4439
         if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) {
4440 4440
             int channels = 0, idx = -1;
4441 4441
             for (i = 0; i < nb_input_streams; i++) {
4442
-                ist = &input_streams[i];
4442
+                ist = input_streams[i];
4443 4443
                 if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
4444 4444
                     ist->st->codec->channels > channels) {
4445 4445
                     channels = ist->st->codec->channels;
... ...
@@ -4453,7 +4925,7 @@ static void opt_output_file(void *optctx, const char *filename)
4453 4453
         /* subtitles: pick first */
4454 4454
         if (!o->subtitle_disable && (oc->oformat->subtitle_codec != CODEC_ID_NONE || subtitle_codec_name)) {
4455 4455
             for (i = 0; i < nb_input_streams; i++)
4456
-                if (input_streams[i].st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4456
+                if (input_streams[i]->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4457 4457
                     new_subtitle_stream(o, oc, i);
4458 4458
                     break;
4459 4459
                 }
... ...
@@ -4462,12 +4934,35 @@ static void opt_output_file(void *optctx, const char *filename)
4462 4462
     } else {
4463 4463
         for (i = 0; i < o->nb_stream_maps; i++) {
4464 4464
             StreamMap *map = &o->stream_maps[i];
4465
-            int src_idx = input_files[map->file_index].ist_index + map->stream_index;
4465
+            int src_idx = input_files[map->file_index]->ist_index + map->stream_index;
4466 4466
 
4467 4467
             if (map->disabled)
4468 4468
                 continue;
4469 4469
 
4470
-            ist = &input_streams[input_files[map->file_index].ist_index + map->stream_index];
4470
+            if (map->linklabel) {
4471
+                FilterGraph *fg;
4472
+                OutputFilter *ofilter = NULL;
4473
+                int j, k;
4474
+
4475
+                for (j = 0; j < nb_filtergraphs; j++) {
4476
+                    fg = filtergraphs[j];
4477
+                    for (k = 0; k < fg->nb_outputs; k++) {
4478
+                        AVFilterInOut *out = fg->outputs[k]->out_tmp;
4479
+                        if (out && !strcmp(out->name, map->linklabel)) {
4480
+                            ofilter = fg->outputs[k];
4481
+                            goto loop_end;
4482
+                        }
4483
+                    }
4484
+                }
4485
+loop_end:
4486
+                if (!ofilter) {
4487
+                    av_log(NULL, AV_LOG_FATAL, "Output with label '%s' does not exist "
4488
+                           "in any defined filter graph.\n", map->linklabel);
4489
+                    exit_program(1);
4490
+                }
4491
+                init_output_filter(ofilter, o, oc);
4492
+            } else {
4493
+            ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
4471 4494
             if(o->subtitle_disable && ist->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
4472 4495
                 continue;
4473 4496
             if(o->   audio_disable && ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
... ...
@@ -4488,13 +4983,15 @@ static void opt_output_file(void *optctx, const char *filename)
4488 4488
                        map->file_index, map->stream_index);
4489 4489
                 exit_program(1);
4490 4490
             }
4491
+
4492
+            }
4491 4493
         }
4492 4494
     }
4493 4495
 
4494 4496
 
4495 4497
     for (i = nb_output_streams - oc->nb_streams; i < nb_output_streams; i++) { //for all streams of this output file
4496 4498
         AVDictionaryEntry *e;
4497
-        ost = &output_streams[i];
4499
+        ost = output_streams[i];
4498 4500
 
4499 4501
         if (   ost->stream_copy
4500 4502
             && (e = av_dict_get(codec_opts, "flags", NULL, AV_DICT_IGNORE_SUFFIX))
... ...
@@ -4539,14 +5036,17 @@ static void opt_output_file(void *optctx, const char *filename)
4539 4539
     }
4540 4540
 
4541 4541
     output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1);
4542
-    output_files[nb_output_files - 1].ctx       = oc;
4543
-    output_files[nb_output_files - 1].ost_index = nb_output_streams - oc->nb_streams;
4544
-    output_files[nb_output_files - 1].recording_time = o->recording_time;
4542
+    if (!(output_files[nb_output_files - 1] = av_mallocz(sizeof(*output_files[0]))))
4543
+        exit_program(1);
4544
+
4545
+    output_files[nb_output_files - 1]->ctx            = oc;
4546
+    output_files[nb_output_files - 1]->ost_index      = nb_output_streams - oc->nb_streams;
4547
+    output_files[nb_output_files - 1]->recording_time = o->recording_time;
4545 4548
     if (o->recording_time != INT64_MAX)
4546 4549
         oc->duration = o->recording_time;
4547
-    output_files[nb_output_files - 1].start_time     = o->start_time;
4548
-    output_files[nb_output_files - 1].limit_filesize = o->limit_filesize;
4549
-    av_dict_copy(&output_files[nb_output_files - 1].opts, format_opts, 0);
4550
+    output_files[nb_output_files - 1]->start_time     = o->start_time;
4551
+    output_files[nb_output_files - 1]->limit_filesize = o->limit_filesize;
4552
+    av_dict_copy(&output_files[nb_output_files - 1]->opts, format_opts, 0);
4550 4553
 
4551 4554
     /* check filename in case of an image number is expected */
4552 4555
     if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
... ...
@@ -4563,7 +5063,7 @@ static void opt_output_file(void *optctx, const char *filename)
4563 4563
         /* open the file */
4564 4564
         if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
4565 4565
                               &oc->interrupt_callback,
4566
-                              &output_files[nb_output_files - 1].opts)) < 0) {
4566
+                              &output_files[nb_output_files - 1]->opts)) < 0) {
4567 4567
             print_error(filename, err);
4568 4568
             exit_program(1);
4569 4569
         }
... ...
@@ -4572,7 +5072,7 @@ static void opt_output_file(void *optctx, const char *filename)
4572 4572
     if (o->mux_preload) {
4573 4573
         uint8_t buf[64];
4574 4574
         snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
4575
-        av_dict_set(&output_files[nb_output_files - 1].opts, "preload", buf, 0);
4575
+        av_dict_set(&output_files[nb_output_files - 1]->opts, "preload", buf, 0);
4576 4576
     }
4577 4577
     oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
4578 4578
 
... ...
@@ -4585,7 +5085,7 @@ static void opt_output_file(void *optctx, const char *filename)
4585 4585
             av_log(NULL, AV_LOG_FATAL, "Invalid input file index %d while processing metadata maps\n", in_file_index);
4586 4586
             exit_program(1);
4587 4587
         }
4588
-        copy_metadata(o->metadata_map[i].specifier, *p ? p + 1 : p, oc, in_file_index >= 0 ? input_files[in_file_index].ctx : NULL, o);
4588
+        copy_metadata(o->metadata_map[i].specifier, *p ? p + 1 : p, oc, in_file_index >= 0 ? input_files[in_file_index]->ctx : NULL, o);
4589 4589
     }
4590 4590
 
4591 4591
     /* copy chapters */
... ...
@@ -4594,7 +5094,7 @@ static void opt_output_file(void *optctx, const char *filename)
4594 4594
             /* copy chapters from the first input file that has them*/
4595 4595
             o->chapters_input_file = -1;
4596 4596
             for (i = 0; i < nb_input_files; i++)
4597
-                if (input_files[i].ctx->nb_chapters) {
4597
+                if (input_files[i]->ctx->nb_chapters) {
4598 4598
                     o->chapters_input_file = i;
4599 4599
                     break;
4600 4600
                 }
... ...
@@ -4605,23 +5105,23 @@ static void opt_output_file(void *optctx, const char *filename)
4605 4605
         }
4606 4606
     }
4607 4607
     if (o->chapters_input_file >= 0)
4608
-        copy_chapters(&input_files[o->chapters_input_file], &output_files[nb_output_files - 1],
4608
+        copy_chapters(input_files[o->chapters_input_file], output_files[nb_output_files - 1],
4609 4609
                       !o->metadata_chapters_manual);
4610 4610
 
4611 4611
     /* copy global metadata by default */
4612 4612
     if (!o->metadata_global_manual && nb_input_files){
4613
-        av_dict_copy(&oc->metadata, input_files[0].ctx->metadata,
4613
+        av_dict_copy(&oc->metadata, input_files[0]->ctx->metadata,
4614 4614
                      AV_DICT_DONT_OVERWRITE);
4615 4615
         if(o->recording_time != INT64_MAX)
4616 4616
             av_dict_set(&oc->metadata, "duration", NULL, 0);
4617 4617
     }
4618 4618
     if (!o->metadata_streams_manual)
4619
-        for (i = output_files[nb_output_files - 1].ost_index; i < nb_output_streams; i++) {
4619
+        for (i = output_files[nb_output_files - 1]->ost_index; i < nb_output_streams; i++) {
4620 4620
             InputStream *ist;
4621
-            if (output_streams[i].source_index < 0)         /* this is true e.g. for attached files */
4621
+            if (output_streams[i]->source_index < 0)         /* this is true e.g. for attached files */
4622 4622
                 continue;
4623
-            ist = &input_streams[output_streams[i].source_index];
4624
-            av_dict_copy(&output_streams[i].st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
4623
+            ist = input_streams[output_streams[i]->source_index];
4624
+            av_dict_copy(&output_streams[i]->st->metadata, ist->st->metadata, AV_DICT_DONT_OVERWRITE);
4625 4625
         }
4626 4626
 
4627 4627
     /* process manually set metadata */
... ...
@@ -4764,8 +5264,8 @@ static int opt_target(OptionsContext *o, const char *opt, const char *arg)
4764 4764
         if (nb_input_files) {
4765 4765
             int i, j, fr;
4766 4766
             for (j = 0; j < nb_input_files; j++) {
4767
-                for (i = 0; i < input_files[j].nb_streams; i++) {
4768
-                    AVCodecContext *c = input_files[j].ctx->streams[i]->codec;
4767
+                for (i = 0; i < input_files[j]->nb_streams; i++) {
4768
+                    AVCodecContext *c = input_files[j]->ctx->streams[i]->codec;
4769 4769
                     if (c->codec_type != AVMEDIA_TYPE_VIDEO)
4770 4770
                         continue;
4771 4771
                     fr = c->time_base.den * 1000 / c->time_base.num;
... ...
@@ -5080,6 +5580,17 @@ static int opt_channel_layout(OptionsContext *o, const char *opt, const char *ar
5080 5080
     return ret;
5081 5081
 }
5082 5082
 
5083
+static int opt_filter_complex(const char *opt, const char *arg)
5084
+{
5085
+    filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
5086
+                              &nb_filtergraphs, nb_filtergraphs + 1);
5087
+    if (!(filtergraphs[nb_filtergraphs - 1] = av_mallocz(sizeof(*filtergraphs[0]))))
5088
+        return AVERROR(ENOMEM);
5089
+    filtergraphs[nb_filtergraphs - 1]->index       = nb_filtergraphs - 1;
5090
+    filtergraphs[nb_filtergraphs - 1]->graph_desc = arg;
5091
+    return 0;
5092
+}
5093
+
5083 5094
 #define OFFSET(x) offsetof(OptionsContext, x)
5084 5095
 static const OptionDef options[] = {
5085 5096
     /* main options */
... ...
@@ -5131,6 +5642,7 @@ static const OptionDef options[] = {
5131 5131
     { "qscale", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_qscale}, "use fixed quality scale (VBR)", "q" },
5132 5132
     { "profile", HAS_ARG | OPT_EXPERT | OPT_FUNC2, {(void*)opt_profile}, "set profile", "profile" },
5133 5133
     { "filter", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(filters)}, "set stream filterchain", "filter_list" },
5134
+    { "filter_complex", HAS_ARG | OPT_EXPERT, {(void*)opt_filter_complex}, "create a complex filtergraph", "graph_description" },
5134 5135
     { "stats", OPT_BOOL, {&print_stats}, "print progress report during encoding", },
5135 5136
     { "attach", HAS_ARG | OPT_FUNC2, {(void*)opt_attach}, "add an attachment to the output file", "filename" },
5136 5137
     { "dump_attachment", HAS_ARG | OPT_STRING | OPT_SPEC, {.off = OFFSET(dump_attachment)}, "extract an attachment into a file", "filename" },
... ...
@@ -5274,7 +5786,7 @@ int main(int argc, char **argv)
5274 5274
     }
5275 5275
 
5276 5276
     current_time = ti = getutime();
5277
-    if (transcode(output_files, nb_output_files, input_files, nb_input_files) < 0)
5277
+    if (transcode() < 0)
5278 5278
         exit_program(1);
5279 5279
     ti = getutime() - ti;
5280 5280
     if (do_benchmark) {
... ...
@@ -1740,9 +1740,9 @@ static AVFilter input_filter =
1740 1740
 
1741 1741
 static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters)
1742 1742
 {
1743
+    static const enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
1743 1744
     char sws_flags_str[128];
1744 1745
     int ret;
1745
-    enum PixelFormat pix_fmts[] = { PIX_FMT_YUV420P, PIX_FMT_NONE };
1746 1746
     AVBufferSinkParams *buffersink_params = av_buffersink_params_alloc();
1747 1747
     AVFilterContext *filt_src = NULL, *filt_out = NULL;
1748 1748
     snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
... ...
@@ -242,7 +242,7 @@ static int link_filter_inouts(AVFilterContext *filt_ctx,
242 242
         } else {
243 243
             p->filter_ctx = filt_ctx;
244 244
             p->pad_idx = pad;
245
-            insert_inout(open_inputs, p);
245
+            append_inout(open_inputs, &p);
246 246
         }
247 247
     }
248 248
 
... ...
@@ -165,9 +165,9 @@ static av_cold int vsink_init(AVFilterContext *ctx, const char *args, void *opaq
165 165
     av_unused AVBufferSinkParams *params;
166 166
 
167 167
     if (!opaque) {
168
-        av_log(ctx, AV_LOG_ERROR,
168
+        av_log(ctx, AV_LOG_WARNING,
169 169
                "No opaque field provided\n");
170
-        return AVERROR(EINVAL);
170
+        buf->pixel_fmts = NULL;
171 171
     } else {
172 172
 #if FF_API_OLD_VSINK_API
173 173
         const int *pixel_fmts = (const enum PixelFormat *)opaque;
... ...
@@ -194,7 +194,11 @@ static int vsink_query_formats(AVFilterContext *ctx)
194 194
 {
195 195
     BufferSinkContext *buf = ctx->priv;
196 196
 
197
-    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
197
+    if (buf->pixel_fmts)
198
+        avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(buf->pixel_fmts));
199
+    else
200
+        avfilter_default_query_formats(ctx);
201
+
198 202
     return 0;
199 203
 }
200 204
 
... ...
@@ -73,9 +73,9 @@ do_video_encoding mpeg2threadivlc.mpg "-qscale 10 -vcodec mpeg2video -f mpeg1vid
73 73
 do_video_decoding
74 74
 
75 75
 # mpeg2 encoding interlaced
76
-file=${outfile}mpeg2reuse.mpg
77
-do_avconv $file $DEC_OPTS -me_threshold 256 -i ${target_path}/${outfile}mpeg2thread.mpg $ENC_OPTS -same_quant -me_threshold 256 -mb_threshold 1024 -vcodec mpeg2video -f mpeg1video -bf 2 -flags +ildct+ilme -threads 4
78
-do_video_decoding
76
+#file=${outfile}mpeg2reuse.mpg
77
+#do_avconv $file $DEC_OPTS -me_threshold 256 -i ${target_path}/${outfile}mpeg2thread.mpg $ENC_OPTS -same_quant -me_threshold 256 -mb_threshold 1024 -vcodec mpeg2video -f mpeg1video -bf 2 -flags +ildct+ilme -threads 4
78
+#do_video_decoding
79 79
 fi
80 80
 
81 81
 if [ -n "$do_msmpeg4v2" ] ; then
82 82
deleted file mode 100644
... ...
@@ -1,46 +0,0 @@
1
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:      0 size: 20829
2
-ret: 0         st:-1 flags:0  ts:-1.000000
3
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:      0 size: 20829
4
-ret: 0         st:-1 flags:1  ts: 1.894167
5
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 337078 size: 26840
6
-ret: 0         st: 0 flags:0  ts: 0.788334
7
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 141401 size: 23537
8
-ret:-1         st: 0 flags:1  ts:-0.317499
9
-ret:-1         st:-1 flags:0  ts: 2.576668
10
-ret: 0         st:-1 flags:1  ts: 1.470835
11
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 232037 size: 26192
12
-ret: 0         st: 0 flags:0  ts: 0.365002
13
-ret: 0         st: 0 flags:1 dts: 0.400000 pts: NOPTS    pos:  63793 size: 21295
14
-ret:-1         st: 0 flags:1  ts:-0.740831
15
-ret:-1         st:-1 flags:0  ts: 2.153336
16
-ret: 0         st:-1 flags:1  ts: 1.047503
17
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 141401 size: 23537
18
-ret: 0         st: 0 flags:0  ts:-0.058330
19
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:      0 size: 20829
20
-ret: 0         st: 0 flags:1  ts: 2.835837
21
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 337078 size: 26840
22
-ret: 0         st:-1 flags:0  ts: 1.730004
23
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 337078 size: 26840
24
-ret: 0         st:-1 flags:1  ts: 0.624171
25
-ret: 0         st: 0 flags:1 dts: 0.400000 pts: NOPTS    pos:  63793 size: 21295
26
-ret: 0         st: 0 flags:0  ts:-0.481662
27
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:      0 size: 20829
28
-ret: 0         st: 0 flags:1  ts: 2.412505
29
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 337078 size: 26840
30
-ret: 0         st:-1 flags:0  ts: 1.306672
31
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 232037 size: 26192
32
-ret: 0         st:-1 flags:1  ts: 0.200839
33
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:      0 size: 20829
34
-ret: 0         st: 0 flags:0  ts:-0.904994
35
-ret: 0         st: 0 flags:1 dts: 0.000000 pts: NOPTS    pos:      0 size: 20829
36
-ret: 0         st: 0 flags:1  ts: 1.989173
37
-ret: 0         st: 0 flags:1 dts: 1.840000 pts: NOPTS    pos: 337078 size: 26840
38
-ret: 0         st:-1 flags:0  ts: 0.883340
39
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 232037 size: 26192
40
-ret:-1         st:-1 flags:1  ts:-0.222493
41
-ret:-1         st: 0 flags:0  ts: 2.671674
42
-ret: 0         st: 0 flags:1  ts: 1.565841
43
-ret: 0         st: 0 flags:1 dts: 1.360000 pts: NOPTS    pos: 232037 size: 26192
44
-ret: 0         st:-1 flags:0  ts: 0.460008
45
-ret: 0         st: 0 flags:1 dts: 0.880000 pts: NOPTS    pos: 141401 size: 23537
46
-ret:-1         st:-1 flags:1  ts:-0.645825
... ...
@@ -2,7 +2,3 @@
2 2
 791773 ./tests/data/vsynth1/mpeg2threadivlc.mpg
3 3
 d1658911ca83f5616c1d32abc40750de *./tests/data/mpeg2thread_ilace.vsynth1.out.yuv
4 4
 stddev:    7.63 PSNR: 30.48 MAXDIFF:  110 bytes:  7603200/  7603200
5
-d119fe917dd81d1ff758b4ce684a8d9d *./tests/data/vsynth1/mpeg2reuse.mpg
6
-2074636 ./tests/data/vsynth1/mpeg2reuse.mpg
7
-92ced6afe8c02304943c400cce51a5f4 *./tests/data/mpeg2thread_ilace.vsynth1.out.yuv
8
-stddev:    7.66 PSNR: 30.44 MAXDIFF:  111 bytes:  7603200/  7603200
... ...
@@ -2,7 +2,3 @@
2 2
 178801 ./tests/data/vsynth2/mpeg2threadivlc.mpg
3 3
 8c6a7ed2eb73bd18fd2bb9829464100d *./tests/data/mpeg2thread_ilace.vsynth2.out.yuv
4 4
 stddev:    4.72 PSNR: 34.65 MAXDIFF:   72 bytes:  7603200/  7603200
5
-864d6bf2982a61e510003a518be65a2d *./tests/data/vsynth2/mpeg2reuse.mpg
6
-383419 ./tests/data/vsynth2/mpeg2reuse.mpg
7
-bb20fa080cfd2b0a687ea7376ff4f902 *./tests/data/mpeg2thread_ilace.vsynth2.out.yuv
8
-stddev:    4.73 PSNR: 34.63 MAXDIFF:   72 bytes:  7603200/  7603200