27dabc01f851d43cba91bfec97d03f821594cbd1
[ffmpeg.git] / avconv.c
1 /*
2  * avconv main
3  * Copyright (c) 2000-2011 The libav developers.
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "config.h"
23 #include <ctype.h>
24 #include <string.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <signal.h>
29 #include <limits.h>
30 #include "libavformat/avformat.h"
31 #include "libavdevice/avdevice.h"
32 #include "libswscale/swscale.h"
33 #include "libavresample/avresample.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/audioconvert.h"
36 #include "libavutil/parseutils.h"
37 #include "libavutil/samplefmt.h"
38 #include "libavutil/colorspace.h"
39 #include "libavutil/fifo.h"
40 #include "libavutil/intreadwrite.h"
41 #include "libavutil/dict.h"
42 #include "libavutil/mathematics.h"
43 #include "libavutil/pixdesc.h"
44 #include "libavutil/avstring.h"
45 #include "libavutil/libm.h"
46 #include "libavutil/imgutils.h"
47 #include "libavutil/time.h"
48 #include "libavformat/os_support.h"
49
50 # include "libavfilter/avfilter.h"
51 # include "libavfilter/avfiltergraph.h"
52 # include "libavfilter/buffersrc.h"
53 # include "libavfilter/buffersink.h"
54 # include "libavfilter/vsrc_buffer.h"
55
56 #if HAVE_SYS_RESOURCE_H
57 #include <sys/types.h>
58 #include <sys/resource.h>
59 #elif HAVE_GETPROCESSTIMES
60 #include <windows.h>
61 #endif
62 #if HAVE_GETPROCESSMEMORYINFO
63 #include <windows.h>
64 #include <psapi.h>
65 #endif
66
67 #if HAVE_SYS_SELECT_H
68 #include <sys/select.h>
69 #endif
70
71 #if HAVE_PTHREADS
72 #include <pthread.h>
73 #endif
74
75 #include <time.h>
76
77 #include "cmdutils.h"
78
79 #include "libavutil/avassert.h"
80
81 #define VSYNC_AUTO       -1
82 #define VSYNC_PASSTHROUGH 0
83 #define VSYNC_CFR         1
84 #define VSYNC_VFR         2
85
86 const char program_name[] = "avconv";
87 const int program_birth_year = 2000;
88
89 /* select an input stream for an output stream */
90 typedef struct StreamMap {
91     int disabled;           /** 1 is this mapping is disabled by a negative map */
92     int file_index;
93     int stream_index;
94     int sync_file_index;
95     int sync_stream_index;
96     char *linklabel;       /** name of an output link, for mapping lavfi outputs */
97 } StreamMap;
98
99 /**
100  * select an input file for an output file
101  */
102 typedef struct MetadataMap {
103     int  file;      ///< file index
104     char type;      ///< type of metadata to copy -- (g)lobal, (s)tream, (c)hapter or (p)rogram
105     int  index;     ///< stream/chapter/program number
106 } MetadataMap;
107
108 static const OptionDef options[];
109
110 static int video_discard = 0;
111 static int same_quant = 0;
112 static int do_deinterlace = 0;
113 static int intra_dc_precision = 8;
114 static int qp_hist = 0;
115
116 static int file_overwrite = 0;
117 static int do_benchmark = 0;
118 static int do_hex_dump = 0;
119 static int do_pkt_dump = 0;
120 static int do_pass = 0;
121 static char *pass_logfilename_prefix = NULL;
122 static int video_sync_method = VSYNC_AUTO;
123 static int audio_sync_method = 0;
124 static float audio_drift_threshold = 0.1;
125 static int copy_ts = 0;
126 static int copy_tb = 1;
127 static int opt_shortest = 0;
128 static char *vstats_filename;
129 static FILE *vstats_file;
130
131 static int audio_volume = 256;
132
133 static int exit_on_error = 0;
134 static int using_stdin = 0;
135 static int64_t video_size = 0;
136 static int64_t audio_size = 0;
137 static int64_t extra_size = 0;
138 static int nb_frames_dup = 0;
139 static int nb_frames_drop = 0;
140 static int input_sync;
141
142 static float dts_delta_threshold = 10;
143
144 static int print_stats = 1;
145
146 #if HAVE_PTHREADS
147 /* signal to input threads that they should exit; set by the main thread */
148 static int transcoding_finished;
149 #endif
150
151 #define DEFAULT_PASS_LOGFILENAME_PREFIX "av2pass"
152
153 typedef struct InputFilter {
154     AVFilterContext    *filter;
155     struct InputStream *ist;
156     struct FilterGraph *graph;
157     uint8_t            *name;
158 } InputFilter;
159
160 typedef struct OutputFilter {
161     AVFilterContext     *filter;
162     struct OutputStream *ost;
163     struct FilterGraph  *graph;
164     uint8_t             *name;
165
166     /* temporary storage until stream maps are processed */
167     AVFilterInOut       *out_tmp;
168 } OutputFilter;
169
170 typedef struct FilterGraph {
171     int            index;
172     const char    *graph_desc;
173
174     AVFilterGraph *graph;
175
176     InputFilter   **inputs;
177     int          nb_inputs;
178     OutputFilter **outputs;
179     int         nb_outputs;
180 } FilterGraph;
181
182 typedef struct InputStream {
183     int file_index;
184     AVStream *st;
185     int discard;             /* true if stream data should be discarded */
186     int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
187     AVCodec *dec;
188     AVFrame *decoded_frame;
189
190     int64_t       start;     /* time when read started */
191     /* predicted dts of the next packet read for this stream or (when there are
192      * several frames in a packet) of the next frame in current packet */
193     int64_t       next_dts;
194     /* dts of the last packet read for this stream */
195     int64_t       last_dts;
196     PtsCorrectionContext pts_ctx;
197     double ts_scale;
198     int is_start;            /* is 1 at the start and after a discontinuity */
199     int showed_multi_packet_warning;
200     AVDictionary *opts;
201     AVRational framerate;               /* framerate forced with -r */
202
203     int resample_height;
204     int resample_width;
205     int resample_pix_fmt;
206
207     int      resample_sample_fmt;
208     int      resample_sample_rate;
209     int      resample_channels;
210     uint64_t resample_channel_layout;
211
212     /* a pool of free buffers for decoded data */
213     FrameBuffer *buffer_pool;
214
215     /* decoded data from this stream goes into all those filters
216      * currently video and audio only */
217     InputFilter **filters;
218     int        nb_filters;
219 } InputStream;
220
221 typedef struct InputFile {
222     AVFormatContext *ctx;
223     int eof_reached;      /* true if eof reached */
224     int ist_index;        /* index of first stream in ist_table */
225     int buffer_size;      /* current total buffer size */
226     int64_t ts_offset;
227     int nb_streams;       /* number of stream that avconv is aware of; may be different
228                              from ctx.nb_streams if new streams appear during av_read_frame() */
229     int rate_emu;
230
231 #if HAVE_PTHREADS
232     pthread_t thread;           /* thread reading from this file */
233     int finished;               /* the thread has exited */
234     int joined;                 /* the thread has been joined */
235     pthread_mutex_t fifo_lock;  /* lock for access to fifo */
236     pthread_cond_t  fifo_cond;  /* the main thread will signal on this cond after reading from fifo */
237     AVFifoBuffer *fifo;         /* demuxed packets are stored here; freed by the main thread */
238 #endif
239 } InputFile;
240
241 typedef struct OutputStream {
242     int file_index;          /* file index */
243     int index;               /* stream index in the output file */
244     int source_index;        /* InputStream index */
245     AVStream *st;            /* stream in the output file */
246     int encoding_needed;     /* true if encoding needed for this stream */
247     int frame_number;
248     /* input pts and corresponding output pts
249        for A/V sync */
250     // double sync_ipts;        /* dts from the AVPacket of the demuxer in second units */
251     struct InputStream *sync_ist; /* input stream to sync against */
252     int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ // FIXME look at frame_number
253     /* pts of the first frame encoded for this stream, used for limiting
254      * recording time */
255     int64_t first_pts;
256     AVBitStreamFilterContext *bitstream_filters;
257     AVCodec *enc;
258     int64_t max_frames;
259     AVFrame *filtered_frame;
260
261     /* video only */
262     AVRational frame_rate;
263     int force_fps;
264     int top_field_first;
265
266     float frame_aspect_ratio;
267     float last_quality;
268
269     /* forced key frames */
270     int64_t *forced_kf_pts;
271     int forced_kf_count;
272     int forced_kf_index;
273     char *forced_keyframes;
274
275     FILE *logfile;
276
277     OutputFilter *filter;
278     char *avfilter;
279
280     int64_t sws_flags;
281     AVDictionary *opts;
282     int is_past_recording_time;
283     int stream_copy;
284     const char *attachment_filename;
285     int copy_initial_nonkeyframes;
286
287     enum PixelFormat pix_fmts[2];
288 } OutputStream;
289
290
291 typedef struct OutputFile {
292     AVFormatContext *ctx;
293     AVDictionary *opts;
294     int ost_index;       /* index of the first stream in output_streams */
295     int64_t recording_time; /* desired length of the resulting file in microseconds */
296     int64_t start_time;     /* start time in microseconds */
297     uint64_t limit_filesize;
298 } OutputFile;
299
300 static InputStream **input_streams = NULL;
301 static int        nb_input_streams = 0;
302 static InputFile   **input_files   = NULL;
303 static int        nb_input_files   = 0;
304
305 static OutputStream **output_streams = NULL;
306 static int         nb_output_streams = 0;
307 static OutputFile   **output_files   = NULL;
308 static int         nb_output_files   = 0;
309
310 static FilterGraph **filtergraphs;
311 int               nb_filtergraphs;
312
313 typedef struct OptionsContext {
314     /* input/output options */
315     int64_t start_time;
316     const char *format;
317
318     SpecifierOpt *codec_names;
319     int        nb_codec_names;
320     SpecifierOpt *audio_channels;
321     int        nb_audio_channels;
322     SpecifierOpt *audio_sample_rate;
323     int        nb_audio_sample_rate;
324     SpecifierOpt *frame_rates;
325     int        nb_frame_rates;
326     SpecifierOpt *frame_sizes;
327     int        nb_frame_sizes;
328     SpecifierOpt *frame_pix_fmts;
329     int        nb_frame_pix_fmts;
330
331     /* input options */
332     int64_t input_ts_offset;
333     int rate_emu;
334
335     SpecifierOpt *ts_scale;
336     int        nb_ts_scale;
337     SpecifierOpt *dump_attachment;
338     int        nb_dump_attachment;
339
340     /* output options */
341     StreamMap *stream_maps;
342     int     nb_stream_maps;
343     /* first item specifies output metadata, second is input */
344     MetadataMap (*meta_data_maps)[2];
345     int nb_meta_data_maps;
346     int metadata_global_manual;
347     int metadata_streams_manual;
348     int metadata_chapters_manual;
349     const char **attachments;
350     int       nb_attachments;
351
352     int chapters_input_file;
353
354     int64_t recording_time;
355     uint64_t limit_filesize;
356     float mux_preload;
357     float mux_max_delay;
358
359     int video_disable;
360     int audio_disable;
361     int subtitle_disable;
362     int data_disable;
363
364     /* indexed by output file stream index */
365     int   *streamid_map;
366     int nb_streamid_map;
367
368     SpecifierOpt *metadata;
369     int        nb_metadata;
370     SpecifierOpt *max_frames;
371     int        nb_max_frames;
372     SpecifierOpt *bitstream_filters;
373     int        nb_bitstream_filters;
374     SpecifierOpt *codec_tags;
375     int        nb_codec_tags;
376     SpecifierOpt *sample_fmts;
377     int        nb_sample_fmts;
378     SpecifierOpt *qscale;
379     int        nb_qscale;
380     SpecifierOpt *forced_key_frames;
381     int        nb_forced_key_frames;
382     SpecifierOpt *force_fps;
383     int        nb_force_fps;
384     SpecifierOpt *frame_aspect_ratios;
385     int        nb_frame_aspect_ratios;
386     SpecifierOpt *rc_overrides;
387     int        nb_rc_overrides;
388     SpecifierOpt *intra_matrices;
389     int        nb_intra_matrices;
390     SpecifierOpt *inter_matrices;
391     int        nb_inter_matrices;
392     SpecifierOpt *top_field_first;
393     int        nb_top_field_first;
394     SpecifierOpt *metadata_map;
395     int        nb_metadata_map;
396     SpecifierOpt *presets;
397     int        nb_presets;
398     SpecifierOpt *copy_initial_nonkeyframes;
399     int        nb_copy_initial_nonkeyframes;
400     SpecifierOpt *filters;
401     int        nb_filters;
402 } OptionsContext;
403
404 #define MATCH_PER_STREAM_OPT(name, type, outvar, fmtctx, st)\
405 {\
406     int i, ret;\
407     for (i = 0; i < o->nb_ ## name; i++) {\
408         char *spec = o->name[i].specifier;\
409         if ((ret = check_stream_specifier(fmtctx, st, spec)) > 0)\
410             outvar = o->name[i].u.type;\
411         else if (ret < 0)\
412             exit_program(1);\
413     }\
414 }
415
416 static void reset_options(OptionsContext *o)
417 {
418     const OptionDef *po = options;
419     int i;
420
421     /* all OPT_SPEC and OPT_STRING can be freed in generic way */
422     while (po->name) {
423         void *dst = (uint8_t*)o + po->u.off;
424
425         if (po->flags & OPT_SPEC) {
426             SpecifierOpt **so = dst;
427             int i, *count = (int*)(so + 1);
428             for (i = 0; i < *count; i++) {
429                 av_freep(&(*so)[i].specifier);
430                 if (po->flags & OPT_STRING)
431                     av_freep(&(*so)[i].u.str);
432             }
433             av_freep(so);
434             *count = 0;
435         } else if (po->flags & OPT_OFFSET && po->flags & OPT_STRING)
436             av_freep(dst);
437         po++;
438     }
439
440     for (i = 0; i < o->nb_stream_maps; i++)
441         av_freep(&o->stream_maps[i].linklabel);
442     av_freep(&o->stream_maps);
443     av_freep(&o->meta_data_maps);
444     av_freep(&o->streamid_map);
445
446     memset(o, 0, sizeof(*o));
447
448     o->mux_max_delay  = 0.7;
449     o->recording_time = INT64_MAX;
450     o->limit_filesize = UINT64_MAX;
451     o->chapters_input_file = INT_MAX;
452
453     uninit_opts();
454     init_opts();
455 }
456
457 /**
458  * Define a function for building a string containing a list of
459  * allowed formats,
460  */
461 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name, separator) \
462 static char *choose_ ## var ## s(OutputStream *ost)                             \
463 {                                                                               \
464     if (ost->st->codec->var != none) {                                          \
465         get_name(ost->st->codec->var);                                          \
466         return av_strdup(name);                                                 \
467     } else if (ost->enc->supported_list) {                                      \
468         const type *p;                                                          \
469         AVIOContext *s = NULL;                                                  \
470         uint8_t *ret;                                                           \
471         int len;                                                                \
472                                                                                 \
473         if (avio_open_dyn_buf(&s) < 0)                                          \
474             exit_program(1);                                                    \
475                                                                                 \
476         for (p = ost->enc->supported_list; *p != none; p++) {                   \
477             get_name(*p);                                                       \
478             avio_printf(s, "%s" separator, name);                               \
479         }                                                                       \
480         len = avio_close_dyn_buf(s, &ret);                                      \
481         ret[len - 1] = 0;                                                       \
482         return ret;                                                             \
483     } else                                                                      \
484         return NULL;                                                            \
485 }
486
487 #define GET_PIX_FMT_NAME(pix_fmt)\
488     const char *name = av_get_pix_fmt_name(pix_fmt);
489
490 DEF_CHOOSE_FORMAT(enum PixelFormat, pix_fmt, pix_fmts, PIX_FMT_NONE,
491                   GET_PIX_FMT_NAME, ":")
492
493 #define GET_SAMPLE_FMT_NAME(sample_fmt)\
494     const char *name = av_get_sample_fmt_name(sample_fmt)
495
496 DEF_CHOOSE_FORMAT(enum AVSampleFormat, sample_fmt, sample_fmts,
497                   AV_SAMPLE_FMT_NONE, GET_SAMPLE_FMT_NAME, ",")
498
499 #define GET_SAMPLE_RATE_NAME(rate)\
500     char name[16];\
501     snprintf(name, sizeof(name), "%d", rate);
502
503 DEF_CHOOSE_FORMAT(int, sample_rate, supported_samplerates, 0,
504                   GET_SAMPLE_RATE_NAME, ",")
505
506 #define GET_CH_LAYOUT_NAME(ch_layout)\
507     char name[16];\
508     snprintf(name, sizeof(name), "0x%"PRIx64, ch_layout);
509
510 DEF_CHOOSE_FORMAT(uint64_t, channel_layout, channel_layouts, 0,
511                   GET_CH_LAYOUT_NAME, ",")
512
513 static FilterGraph *init_simple_filtergraph(InputStream *ist, OutputStream *ost)
514 {
515     FilterGraph *fg = av_mallocz(sizeof(*fg));
516
517     if (!fg)
518         exit_program(1);
519     fg->index = nb_filtergraphs;
520
521     fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs), &fg->nb_outputs,
522                              fg->nb_outputs + 1);
523     if (!(fg->outputs[0] = av_mallocz(sizeof(*fg->outputs[0]))))
524         exit_program(1);
525     fg->outputs[0]->ost   = ost;
526     fg->outputs[0]->graph = fg;
527
528     ost->filter = fg->outputs[0];
529
530     fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs), &fg->nb_inputs,
531                             fg->nb_inputs + 1);
532     if (!(fg->inputs[0] = av_mallocz(sizeof(*fg->inputs[0]))))
533         exit_program(1);
534     fg->inputs[0]->ist   = ist;
535     fg->inputs[0]->graph = fg;
536
537     ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
538                               &ist->nb_filters, ist->nb_filters + 1);
539     ist->filters[ist->nb_filters - 1] = fg->inputs[0];
540
541     filtergraphs = grow_array(filtergraphs, sizeof(*filtergraphs),
542                               &nb_filtergraphs, nb_filtergraphs + 1);
543     filtergraphs[nb_filtergraphs - 1] = fg;
544
545     return fg;
546 }
547
548 static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
549 {
550     InputStream *ist;
551     enum AVMediaType type = avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx);
552     int i;
553
554     // TODO: support other filter types
555     if (type != AVMEDIA_TYPE_VIDEO && type != AVMEDIA_TYPE_AUDIO) {
556         av_log(NULL, AV_LOG_FATAL, "Only video and audio filters supported "
557                "currently.\n");
558         exit_program(1);
559     }
560
561     if (in->name) {
562         AVFormatContext *s;
563         AVStream       *st = NULL;
564         char *p;
565         int file_idx = strtol(in->name, &p, 0);
566
567         if (file_idx < 0 || file_idx >= nb_input_files) {
568             av_log(NULL, AV_LOG_FATAL, "Invalid file index %d in filtegraph description %s.\n",
569                    file_idx, fg->graph_desc);
570             exit_program(1);
571         }
572         s = input_files[file_idx]->ctx;
573
574         for (i = 0; i < s->nb_streams; i++) {
575             if (s->streams[i]->codec->codec_type != type)
576                 continue;
577             if (check_stream_specifier(s, s->streams[i], *p == ':' ? p + 1 : p) == 1) {
578                 st = s->streams[i];
579                 break;
580             }
581         }
582         if (!st) {
583             av_log(NULL, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
584                    "matches no streams.\n", p, fg->graph_desc);
585             exit_program(1);
586         }
587         ist = input_streams[input_files[file_idx]->ist_index + st->index];
588     } else {
589         /* find the first unused stream of corresponding type */
590         for (i = 0; i < nb_input_streams; i++) {
591             ist = input_streams[i];
592             if (ist->st->codec->codec_type == type && ist->discard)
593                 break;
594         }
595         if (i == nb_input_streams) {
596             av_log(NULL, AV_LOG_FATAL, "Cannot find a matching stream for "
597                    "unlabeled input pad %d on filter %s", in->pad_idx,
598                    in->filter_ctx->name);
599             exit_program(1);
600         }
601     }
602     ist->discard         = 0;
603     ist->decoding_needed = 1;
604     ist->st->discard = AVDISCARD_NONE;
605
606     fg->inputs = grow_array(fg->inputs, sizeof(*fg->inputs),
607                             &fg->nb_inputs, fg->nb_inputs + 1);
608     if (!(fg->inputs[fg->nb_inputs - 1] = av_mallocz(sizeof(*fg->inputs[0]))))
609         exit_program(1);
610     fg->inputs[fg->nb_inputs - 1]->ist   = ist;
611     fg->inputs[fg->nb_inputs - 1]->graph = fg;
612
613     ist->filters = grow_array(ist->filters, sizeof(*ist->filters),
614                               &ist->nb_filters, ist->nb_filters + 1);
615     ist->filters[ist->nb_filters - 1] = fg->inputs[fg->nb_inputs - 1];
616 }
617
618 static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
619 {
620     char *pix_fmts;
621     OutputStream *ost = ofilter->ost;
622     AVCodecContext *codec = ost->st->codec;
623     AVFilterContext *last_filter = out->filter_ctx;
624     int pad_idx = out->pad_idx;
625     int ret;
626     char name[255];
627
628     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
629     ret = avfilter_graph_create_filter(&ofilter->filter,
630                                        avfilter_get_by_name("buffersink"),
631                                        name, NULL, pix_fmts, fg->graph);
632     if (ret < 0)
633         return ret;
634
635     if (codec->width || codec->height) {
636         char args[255];
637         AVFilterContext *filter;
638
639         snprintf(args, sizeof(args), "%d:%d:flags=0x%X",
640                  codec->width,
641                  codec->height,
642                  (unsigned)ost->sws_flags);
643         snprintf(name, sizeof(name), "scaler for output stream %d:%d",
644                  ost->file_index, ost->index);
645         if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
646                                                 name, args, NULL, fg->graph)) < 0)
647             return ret;
648         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
649             return ret;
650
651         last_filter = filter;
652         pad_idx = 0;
653     }
654
655     if ((pix_fmts = choose_pix_fmts(ost))) {
656         AVFilterContext *filter;
657         snprintf(name, sizeof(name), "pixel format for output stream %d:%d",
658                  ost->file_index, ost->index);
659         if ((ret = avfilter_graph_create_filter(&filter,
660                                                 avfilter_get_by_name("format"),
661                                                 "format", pix_fmts, NULL,
662                                                 fg->graph)) < 0)
663             return ret;
664         if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
665             return ret;
666
667         last_filter = filter;
668         pad_idx     = 0;
669         av_freep(&pix_fmts);
670     }
671
672     if (ost->frame_rate.num) {
673         AVFilterContext *fps;
674         char args[255];
675
676         snprintf(args, sizeof(args), "fps=%d/%d", ost->frame_rate.num,
677                  ost->frame_rate.den);
678         snprintf(name, sizeof(name), "fps for output stream %d:%d",
679                  ost->file_index, ost->index);
680         ret = avfilter_graph_create_filter(&fps, avfilter_get_by_name("fps"),
681                                            name, args, NULL, fg->graph);
682         if (ret < 0)
683             return ret;
684
685         ret = avfilter_link(last_filter, pad_idx, fps, 0);
686         if (ret < 0)
687             return ret;
688         last_filter = fps;
689         pad_idx = 0;
690     }
691
692     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
693         return ret;
694
695     return 0;
696 }
697
698 static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
699 {
700     OutputStream *ost = ofilter->ost;
701     AVCodecContext *codec  = ost->st->codec;
702     AVFilterContext *last_filter = out->filter_ctx;
703     int pad_idx = out->pad_idx;
704     char *sample_fmts, *sample_rates, *channel_layouts;
705     char name[255];
706     int ret;
707
708
709     snprintf(name, sizeof(name), "output stream %d:%d", ost->file_index, ost->index);
710     ret = avfilter_graph_create_filter(&ofilter->filter,
711                                        avfilter_get_by_name("abuffersink"),
712                                        name, NULL, NULL, fg->graph);
713     if (ret < 0)
714         return ret;
715
716     if (codec->channels && !codec->channel_layout)
717         codec->channel_layout = av_get_default_channel_layout(codec->channels);
718
719     sample_fmts     = choose_sample_fmts(ost);
720     sample_rates    = choose_sample_rates(ost);
721     channel_layouts = choose_channel_layouts(ost);
722     if (sample_fmts || sample_rates || channel_layouts) {
723         AVFilterContext *format;
724         char args[256];
725         int len = 0;
726
727         if (sample_fmts)
728             len += snprintf(args + len, sizeof(args) - len, "sample_fmts=%s:",
729                             sample_fmts);
730         if (sample_rates)
731             len += snprintf(args + len, sizeof(args) - len, "sample_rates=%s:",
732                             sample_rates);
733         if (channel_layouts)
734             len += snprintf(args + len, sizeof(args) - len, "channel_layouts=%s:",
735                             channel_layouts);
736         args[len - 1] = 0;
737
738         av_freep(&sample_fmts);
739         av_freep(&sample_rates);
740         av_freep(&channel_layouts);
741
742         snprintf(name, sizeof(name), "audio format for output stream %d:%d",
743                  ost->file_index, ost->index);
744         ret = avfilter_graph_create_filter(&format,
745                                            avfilter_get_by_name("aformat"),
746                                            name, args, NULL, fg->graph);
747         if (ret < 0)
748             return ret;
749
750         ret = avfilter_link(last_filter, pad_idx, format, 0);
751         if (ret < 0)
752             return ret;
753
754         last_filter = format;
755         pad_idx = 0;
756     }
757
758     if ((ret = avfilter_link(last_filter, pad_idx, ofilter->filter, 0)) < 0)
759         return ret;
760
761     return 0;
762 }
763
764 #define DESCRIBE_FILTER_LINK(f, inout, in)                         \
765 {                                                                  \
766     AVFilterContext *ctx = inout->filter_ctx;                      \
767     AVFilterPad *pads = in ? ctx->input_pads  : ctx->output_pads;  \
768     int       nb_pads = in ? ctx->input_count : ctx->output_count; \
769     AVIOContext *pb;                                               \
770                                                                    \
771     if (avio_open_dyn_buf(&pb) < 0)                                \
772         exit_program(1);                                           \
773                                                                    \
774     avio_printf(pb, "%s", ctx->filter->name);                      \
775     if (nb_pads > 1)                                               \
776         avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
777     avio_w8(pb, 0);                                                \
778     avio_close_dyn_buf(pb, &f->name);                              \
779 }
780
781 static int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
782 {
783     av_freep(&ofilter->name);
784     DESCRIBE_FILTER_LINK(ofilter, out, 0);
785
786     switch (avfilter_pad_get_type(out->filter_ctx->output_pads, out->pad_idx)) {
787     case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, ofilter, out);
788     case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, ofilter, out);
789     default: av_assert0(0);
790     }
791 }
792
793 static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter,
794                                         AVFilterInOut *in)
795 {
796     AVFilterContext *first_filter = in->filter_ctx;
797     AVFilter *filter = avfilter_get_by_name("buffer");
798     InputStream *ist = ifilter->ist;
799     AVRational tb = ist->framerate.num ? (AVRational){ist->framerate.den,
800                                                       ist->framerate.num} :
801                                          ist->st->time_base;
802     AVRational sar;
803     char args[255], name[255];
804     int pad_idx = in->pad_idx;
805     int ret;
806
807     sar = ist->st->sample_aspect_ratio.num ?
808           ist->st->sample_aspect_ratio :
809           ist->st->codec->sample_aspect_ratio;
810     snprintf(args, sizeof(args), "%d:%d:%d:%d:%d:%d:%d", ist->st->codec->width,
811              ist->st->codec->height, ist->st->codec->pix_fmt,
812              tb.num, tb.den, sar.num, sar.den);
813     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
814              ist->file_index, ist->st->index);
815
816     if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter, name,
817                                             args, NULL, fg->graph)) < 0)
818         return ret;
819
820     if (ist->framerate.num) {
821         AVFilterContext *setpts;
822
823         snprintf(name, sizeof(name), "force CFR for input from stream %d:%d",
824                  ist->file_index, ist->st->index);
825         if ((ret = avfilter_graph_create_filter(&setpts,
826                                                 avfilter_get_by_name("setpts"),
827                                                 name, "N", NULL,
828                                                 fg->graph)) < 0)
829             return ret;
830
831         if ((ret = avfilter_link(setpts, 0, first_filter, pad_idx)) < 0)
832             return ret;
833
834         first_filter = setpts;
835         pad_idx = 0;
836     }
837
838     if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
839         return ret;
840     return 0;
841 }
842
843 static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter,
844                                         AVFilterInOut *in)
845 {
846     AVFilterContext *first_filter = in->filter_ctx;
847     AVFilter *filter = avfilter_get_by_name("abuffer");
848     InputStream *ist = ifilter->ist;
849     int pad_idx = in->pad_idx;
850     char args[255], name[255];
851     int ret;
852
853     snprintf(args, sizeof(args), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
854              ":channel_layout=0x%"PRIx64,
855              ist->st->time_base.num, ist->st->time_base.den,
856              ist->st->codec->sample_rate,
857              av_get_sample_fmt_name(ist->st->codec->sample_fmt),
858              ist->st->codec->channel_layout);
859     snprintf(name, sizeof(name), "graph %d input from stream %d:%d", fg->index,
860              ist->file_index, ist->st->index);
861
862     if ((ret = avfilter_graph_create_filter(&ifilter->filter, filter,
863                                             name, args, NULL,
864                                             fg->graph)) < 0)
865         return ret;
866
867     if (audio_sync_method > 0) {
868         AVFilterContext *async;
869         char args[256];
870         int  len = 0;
871
872         av_log(NULL, AV_LOG_WARNING, "-async has been deprecated. Used the "
873                "asyncts audio filter instead.\n");
874
875         if (audio_sync_method > 1)
876             len += snprintf(args + len, sizeof(args) - len, "compensate=1:"
877                             "max_comp=%d:", audio_sync_method);
878         snprintf(args + len, sizeof(args) - len, "min_delta=%f",
879                  audio_drift_threshold);
880
881         snprintf(name, sizeof(name), "graph %d audio sync for input stream %d:%d",
882                  fg->index, ist->file_index, ist->st->index);
883         ret = avfilter_graph_create_filter(&async,
884                                            avfilter_get_by_name("asyncts"),
885                                            name, args, NULL, fg->graph);
886         if (ret < 0)
887             return ret;
888
889         ret = avfilter_link(async, 0, first_filter, pad_idx);
890         if (ret < 0)
891             return ret;
892
893         first_filter = async;
894         pad_idx = 0;
895     }
896     if ((ret = avfilter_link(ifilter->filter, 0, first_filter, pad_idx)) < 0)
897         return ret;
898
899     return 0;
900 }
901
902 static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter,
903                                   AVFilterInOut *in)
904 {
905     av_freep(&ifilter->name);
906     DESCRIBE_FILTER_LINK(ifilter, in, 1);
907
908     switch (avfilter_pad_get_type(in->filter_ctx->input_pads, in->pad_idx)) {
909     case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, ifilter, in);
910     case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, ifilter, in);
911     default: av_assert0(0);
912     }
913 }
914
915 static int configure_filtergraph(FilterGraph *fg)
916 {
917     AVFilterInOut *inputs, *outputs, *cur;
918     int ret, i, init = !fg->graph, simple = !fg->graph_desc;
919     const char *graph_desc = simple ? fg->outputs[0]->ost->avfilter :
920                                       fg->graph_desc;
921
922     avfilter_graph_free(&fg->graph);
923     if (!(fg->graph = avfilter_graph_alloc()))
924         return AVERROR(ENOMEM);
925
926     if (simple) {
927         OutputStream *ost = fg->outputs[0]->ost;
928         char args[255];
929         snprintf(args, sizeof(args), "flags=0x%X", (unsigned)ost->sws_flags);
930         fg->graph->scale_sws_opts = av_strdup(args);
931     }
932
933     if ((ret = avfilter_graph_parse2(fg->graph, graph_desc, &inputs, &outputs)) < 0)
934         return ret;
935
936     if (simple && (!inputs || inputs->next || !outputs || outputs->next)) {
937         av_log(NULL, AV_LOG_ERROR, "Simple filtergraph '%s' does not have "
938                "exactly one input and output.\n", graph_desc);
939         return AVERROR(EINVAL);
940     }
941
942     for (cur = inputs; !simple && init && cur; cur = cur->next)
943         init_input_filter(fg, cur);
944
945     for (cur = inputs, i = 0; cur; cur = cur->next, i++)
946         if ((ret = configure_input_filter(fg, fg->inputs[i], cur)) < 0)
947             return ret;
948     avfilter_inout_free(&inputs);
949
950     if (!init || simple) {
951         /* we already know the mappings between lavfi outputs and output streams,
952          * so we can finish the setup */
953         for (cur = outputs, i = 0; cur; cur = cur->next, i++)
954             configure_output_filter(fg, fg->outputs[i], cur);
955         avfilter_inout_free(&outputs);
956
957         if ((ret = avfilter_graph_config(fg->graph, NULL)) < 0)
958             return ret;
959     } else {
960         /* wait until output mappings are processed */
961         for (cur = outputs; cur;) {
962             fg->outputs = grow_array(fg->outputs, sizeof(*fg->outputs),
963                                      &fg->nb_outputs, fg->nb_outputs + 1);
964             if (!(fg->outputs[fg->nb_outputs - 1] = av_mallocz(sizeof(*fg->outputs[0]))))
965                 exit_program(1);
966             fg->outputs[fg->nb_outputs - 1]->graph   = fg;
967             fg->outputs[fg->nb_outputs - 1]->out_tmp = cur;
968             cur = cur->next;
969             fg->outputs[fg->nb_outputs - 1]->out_tmp->next = NULL;
970         }
971     }
972
973     return 0;
974 }
975
976 static int configure_complex_filters(void)
977 {
978     int i, ret = 0;
979
980     for (i = 0; i < nb_filtergraphs; i++)
981         if (!filtergraphs[i]->graph &&
982             (ret = configure_filtergraph(filtergraphs[i])) < 0)
983             return ret;
984     return 0;
985 }
986
987 static int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)
988 {
989     int i;
990     for (i = 0; i < fg->nb_inputs; i++)
991         if (fg->inputs[i]->ist == ist)
992             return 1;
993     return 0;
994 }
995
996 static void term_exit(void)
997 {
998     av_log(NULL, AV_LOG_QUIET, "");
999 }
1000
1001 static volatile int received_sigterm = 0;
1002 static volatile int received_nb_signals = 0;
1003
1004 static void
1005 sigterm_handler(int sig)
1006 {
1007     received_sigterm = sig;
1008     received_nb_signals++;
1009     term_exit();
1010 }
1011
1012 static void term_init(void)
1013 {
1014     signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).    */
1015     signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
1016 #ifdef SIGXCPU
1017     signal(SIGXCPU, sigterm_handler);
1018 #endif
1019 }
1020
1021 static int decode_interrupt_cb(void *ctx)
1022 {
1023     return received_nb_signals > 1;
1024 }
1025
1026 static const AVIOInterruptCB int_cb = { decode_interrupt_cb, NULL };
1027
1028 void exit_program(int ret)
1029 {
1030     int i, j;
1031
1032     for (i = 0; i < nb_filtergraphs; i++) {
1033         avfilter_graph_free(&filtergraphs[i]->graph);
1034         for (j = 0; j < filtergraphs[i]->nb_inputs; j++) {
1035             av_freep(&filtergraphs[i]->inputs[j]->name);
1036             av_freep(&filtergraphs[i]->inputs[j]);
1037         }
1038         av_freep(&filtergraphs[i]->inputs);
1039         for (j = 0; j < filtergraphs[i]->nb_outputs; j++) {
1040             av_freep(&filtergraphs[i]->outputs[j]->name);
1041             av_freep(&filtergraphs[i]->outputs[j]);
1042         }
1043         av_freep(&filtergraphs[i]->outputs);
1044         av_freep(&filtergraphs[i]);
1045     }
1046     av_freep(&filtergraphs);
1047
1048     /* close files */
1049     for (i = 0; i < nb_output_files; i++) {
1050         AVFormatContext *s = output_files[i]->ctx;
1051         if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
1052             avio_close(s->pb);
1053         avformat_free_context(s);
1054         av_dict_free(&output_files[i]->opts);
1055         av_freep(&output_files[i]);
1056     }
1057     for (i = 0; i < nb_output_streams; i++) {
1058         AVBitStreamFilterContext *bsfc = output_streams[i]->bitstream_filters;
1059         while (bsfc) {
1060             AVBitStreamFilterContext *next = bsfc->next;
1061             av_bitstream_filter_close(bsfc);
1062             bsfc = next;
1063         }
1064         output_streams[i]->bitstream_filters = NULL;
1065
1066         av_freep(&output_streams[i]->forced_keyframes);
1067         av_freep(&output_streams[i]->avfilter);
1068         av_freep(&output_streams[i]->filtered_frame);
1069         av_freep(&output_streams[i]);
1070     }
1071     for (i = 0; i < nb_input_files; i++) {
1072         avformat_close_input(&input_files[i]->ctx);
1073         av_freep(&input_files[i]);
1074     }
1075     for (i = 0; i < nb_input_streams; i++) {
1076         av_freep(&input_streams[i]->decoded_frame);
1077         av_dict_free(&input_streams[i]->opts);
1078         free_buffer_pool(&input_streams[i]->buffer_pool);
1079         av_freep(&input_streams[i]->filters);
1080         av_freep(&input_streams[i]);
1081     }
1082
1083     if (vstats_file)
1084         fclose(vstats_file);
1085     av_free(vstats_filename);
1086
1087     av_freep(&input_streams);
1088     av_freep(&input_files);
1089     av_freep(&output_streams);
1090     av_freep(&output_files);
1091
1092     uninit_opts();
1093
1094     avfilter_uninit();
1095     avformat_network_deinit();
1096
1097     if (received_sigterm) {
1098         av_log(NULL, AV_LOG_INFO, "Received signal %d: terminating.\n",
1099                (int) received_sigterm);
1100         exit (255);
1101     }
1102
1103     exit(ret);
1104 }
1105
1106 static void assert_avoptions(AVDictionary *m)
1107 {
1108     AVDictionaryEntry *t;
1109     if ((t = av_dict_get(m, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
1110         av_log(NULL, AV_LOG_FATAL, "Option %s not found.\n", t->key);
1111         exit_program(1);
1112     }
1113 }
1114
1115 static void assert_codec_experimental(AVCodecContext *c, int encoder)
1116 {
1117     const char *codec_string = encoder ? "encoder" : "decoder";
1118     AVCodec *codec;
1119     if (c->codec->capabilities & CODEC_CAP_EXPERIMENTAL &&
1120         c->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
1121         av_log(NULL, AV_LOG_FATAL, "%s '%s' is experimental and might produce bad "
1122                 "results.\nAdd '-strict experimental' if you want to use it.\n",
1123                 codec_string, c->codec->name);
1124         codec = encoder ? avcodec_find_encoder(c->codec->id) : avcodec_find_decoder(c->codec->id);
1125         if (!(codec->capabilities & CODEC_CAP_EXPERIMENTAL))
1126             av_log(NULL, AV_LOG_FATAL, "Or use the non experimental %s '%s'.\n",
1127                    codec_string, codec->name);
1128         exit_program(1);
1129     }
1130 }
1131
1132 /**
1133  * Update the requested input sample format based on the output sample format.
1134  * This is currently only used to request float output from decoders which
1135  * support multiple sample formats, one of which is AV_SAMPLE_FMT_FLT.
1136  * Ideally this will be removed in the future when decoders do not do format
1137  * conversion and only output in their native format.
1138  */
1139 static void update_sample_fmt(AVCodecContext *dec, AVCodec *dec_codec,
1140                               AVCodecContext *enc)
1141 {
1142     /* if sample formats match or a decoder sample format has already been
1143        requested, just return */
1144     if (enc->sample_fmt == dec->sample_fmt ||
1145         dec->request_sample_fmt > AV_SAMPLE_FMT_NONE)
1146         return;
1147
1148     /* if decoder supports more than one output format */
1149     if (dec_codec && dec_codec->sample_fmts &&
1150         dec_codec->sample_fmts[0] != AV_SAMPLE_FMT_NONE &&
1151         dec_codec->sample_fmts[1] != AV_SAMPLE_FMT_NONE) {
1152         const enum AVSampleFormat *p;
1153         int min_dec = -1, min_inc = -1;
1154
1155         /* find a matching sample format in the encoder */
1156         for (p = dec_codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++) {
1157             if (*p == enc->sample_fmt) {
1158                 dec->request_sample_fmt = *p;
1159                 return;
1160             } else if (*p > enc->sample_fmt) {
1161                 min_inc = FFMIN(min_inc, *p - enc->sample_fmt);
1162             } else
1163                 min_dec = FFMIN(min_dec, enc->sample_fmt - *p);
1164         }
1165
1166         /* if none match, provide the one that matches quality closest */
1167         dec->request_sample_fmt = min_inc > 0 ? enc->sample_fmt + min_inc :
1168                                   enc->sample_fmt - min_dec;
1169     }
1170 }
1171
1172 static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
1173 {
1174     AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
1175     AVCodecContext          *avctx = ost->st->codec;
1176     int ret;
1177
1178     /*
1179      * Audio encoders may split the packets --  #frames in != #packets out.
1180      * But there is no reordering, so we can limit the number of output packets
1181      * by simply dropping them here.
1182      * Counting encoded video frames needs to be done separately because of
1183      * reordering, see do_video_out()
1184      */
1185     if (!(avctx->codec_type == AVMEDIA_TYPE_VIDEO && avctx->codec)) {
1186         if (ost->frame_number >= ost->max_frames) {
1187             av_free_packet(pkt);
1188             return;
1189         }
1190         ost->frame_number++;
1191     }
1192
1193     while (bsfc) {
1194         AVPacket new_pkt = *pkt;
1195         int a = av_bitstream_filter_filter(bsfc, avctx, NULL,
1196                                            &new_pkt.data, &new_pkt.size,
1197                                            pkt->data, pkt->size,
1198                                            pkt->flags & AV_PKT_FLAG_KEY);
1199         if (a > 0) {
1200             av_free_packet(pkt);
1201             new_pkt.destruct = av_destruct_packet;
1202         } else if (a < 0) {
1203             av_log(NULL, AV_LOG_ERROR, "%s failed for stream %d, codec %s",
1204                    bsfc->filter->name, pkt->stream_index,
1205                    avctx->codec ? avctx->codec->name : "copy");
1206             print_error("", a);
1207             if (exit_on_error)
1208                 exit_program(1);
1209         }
1210         *pkt = new_pkt;
1211
1212         bsfc = bsfc->next;
1213     }
1214
1215     pkt->stream_index = ost->index;
1216     ret = av_interleaved_write_frame(s, pkt);
1217     if (ret < 0) {
1218         print_error("av_interleaved_write_frame()", ret);
1219         exit_program(1);
1220     }
1221 }
1222
1223 static int check_recording_time(OutputStream *ost)
1224 {
1225     OutputFile *of = output_files[ost->file_index];
1226
1227     if (of->recording_time != INT64_MAX &&
1228         av_compare_ts(ost->sync_opts - ost->first_pts, ost->st->codec->time_base, of->recording_time,
1229                       AV_TIME_BASE_Q) >= 0) {
1230         ost->is_past_recording_time = 1;
1231         return 0;
1232     }
1233     return 1;
1234 }
1235
1236 static void do_audio_out(AVFormatContext *s, OutputStream *ost,
1237                          AVFrame *frame)
1238 {
1239     AVCodecContext *enc = ost->st->codec;
1240     AVPacket pkt;
1241     int got_packet = 0;
1242
1243     av_init_packet(&pkt);
1244     pkt.data = NULL;
1245     pkt.size = 0;
1246
1247     if (!check_recording_time(ost))
1248         return;
1249
1250     if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
1251         frame->pts = ost->sync_opts;
1252     ost->sync_opts = frame->pts + frame->nb_samples;
1253
1254     if (avcodec_encode_audio2(enc, &pkt, frame, &got_packet) < 0) {
1255         av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
1256         exit_program(1);
1257     }
1258
1259     if (got_packet) {
1260         if (pkt.pts != AV_NOPTS_VALUE)
1261             pkt.pts      = av_rescale_q(pkt.pts,      enc->time_base, ost->st->time_base);
1262         if (pkt.dts != AV_NOPTS_VALUE)
1263             pkt.dts      = av_rescale_q(pkt.dts,      enc->time_base, ost->st->time_base);
1264         if (pkt.duration > 0)
1265             pkt.duration = av_rescale_q(pkt.duration, enc->time_base, ost->st->time_base);
1266
1267         write_frame(s, &pkt, ost);
1268
1269         audio_size += pkt.size;
1270     }
1271 }
1272
1273 static void pre_process_video_frame(InputStream *ist, AVPicture *picture, void **bufp)
1274 {
1275     AVCodecContext *dec;
1276     AVPicture *picture2;
1277     AVPicture picture_tmp;
1278     uint8_t *buf = 0;
1279
1280     dec = ist->st->codec;
1281
1282     /* deinterlace : must be done before any resize */
1283     if (do_deinterlace) {
1284         int size;
1285
1286         /* create temporary picture */
1287         size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
1288         buf  = av_malloc(size);
1289         if (!buf)
1290             return;
1291
1292         picture2 = &picture_tmp;
1293         avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
1294
1295         if (avpicture_deinterlace(picture2, picture,
1296                                  dec->pix_fmt, dec->width, dec->height) < 0) {
1297             /* if error, do not deinterlace */
1298             av_log(NULL, AV_LOG_WARNING, "Deinterlacing failed\n");
1299             av_free(buf);
1300             buf = NULL;
1301             picture2 = picture;
1302         }
1303     } else {
1304         picture2 = picture;
1305     }
1306
1307     if (picture != picture2)
1308         *picture = *picture2;
1309     *bufp = buf;
1310 }
1311
1312 static void do_subtitle_out(AVFormatContext *s,
1313                             OutputStream *ost,
1314                             InputStream *ist,
1315                             AVSubtitle *sub,
1316                             int64_t pts)
1317 {
1318     static uint8_t *subtitle_out = NULL;
1319     int subtitle_out_max_size = 1024 * 1024;
1320     int subtitle_out_size, nb, i;
1321     AVCodecContext *enc;
1322     AVPacket pkt;
1323
1324     if (pts == AV_NOPTS_VALUE) {
1325         av_log(NULL, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
1326         if (exit_on_error)
1327             exit_program(1);
1328         return;
1329     }
1330
1331     enc = ost->st->codec;
1332
1333     if (!subtitle_out) {
1334         subtitle_out = av_malloc(subtitle_out_max_size);
1335     }
1336
1337     /* Note: DVB subtitle need one packet to draw them and one other
1338        packet to clear them */
1339     /* XXX: signal it in the codec context ? */
1340     if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
1341         nb = 2;
1342     else
1343         nb = 1;
1344
1345     for (i = 0; i < nb; i++) {
1346         ost->sync_opts = av_rescale_q(pts, ist->st->time_base, enc->time_base);
1347         if (!check_recording_time(ost))
1348             return;
1349
1350         sub->pts = av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q);
1351         // start_display_time is required to be 0
1352         sub->pts               += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
1353         sub->end_display_time  -= sub->start_display_time;
1354         sub->start_display_time = 0;
1355         subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
1356                                                     subtitle_out_max_size, sub);
1357         if (subtitle_out_size < 0) {
1358             av_log(NULL, AV_LOG_FATAL, "Subtitle encoding failed\n");
1359             exit_program(1);
1360         }
1361
1362         av_init_packet(&pkt);
1363         pkt.data = subtitle_out;
1364         pkt.size = subtitle_out_size;
1365         pkt.pts  = av_rescale_q(sub->pts, AV_TIME_BASE_Q, ost->st->time_base);
1366         if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
1367             /* XXX: the pts correction is handled here. Maybe handling
1368                it in the codec would be better */
1369             if (i == 0)
1370                 pkt.pts += 90 * sub->start_display_time;
1371             else
1372                 pkt.pts += 90 * sub->end_display_time;
1373         }
1374         write_frame(s, &pkt, ost);
1375     }
1376 }
1377
1378 static void do_video_out(AVFormatContext *s,
1379                          OutputStream *ost,
1380                          AVFrame *in_picture,
1381                          int *frame_size, float quality)
1382 {
1383     int ret, format_video_sync;
1384     AVPacket pkt;
1385     AVCodecContext *enc = ost->st->codec;
1386
1387     *frame_size = 0;
1388
1389     format_video_sync = video_sync_method;
1390     if (format_video_sync == VSYNC_AUTO)
1391         format_video_sync = (s->oformat->flags & AVFMT_NOTIMESTAMPS) ? VSYNC_PASSTHROUGH :
1392                             (s->oformat->flags & AVFMT_VARIABLE_FPS) ? VSYNC_VFR : VSYNC_CFR;
1393     if (format_video_sync != VSYNC_PASSTHROUGH &&
1394         ost->frame_number &&
1395         in_picture->pts != AV_NOPTS_VALUE &&
1396         in_picture->pts < ost->sync_opts) {
1397         nb_frames_drop++;
1398         av_log(NULL, AV_LOG_VERBOSE, "*** drop!\n");
1399         return;
1400     }
1401
1402     if (in_picture->pts == AV_NOPTS_VALUE)
1403         in_picture->pts = ost->sync_opts;
1404     ost->sync_opts = in_picture->pts;
1405
1406
1407     if (!ost->frame_number)
1408         ost->first_pts = in_picture->pts;
1409
1410     av_init_packet(&pkt);
1411     pkt.data = NULL;
1412     pkt.size = 0;
1413
1414     if (!check_recording_time(ost) ||
1415         ost->frame_number >= ost->max_frames)
1416         return;
1417
1418     if (s->oformat->flags & AVFMT_RAWPICTURE &&
1419         enc->codec->id == CODEC_ID_RAWVIDEO) {
1420         /* raw pictures are written as AVPicture structure to
1421            avoid any copies. We support temporarily the older
1422            method. */
1423         enc->coded_frame->interlaced_frame = in_picture->interlaced_frame;
1424         enc->coded_frame->top_field_first  = in_picture->top_field_first;
1425         pkt.data   = (uint8_t *)in_picture;
1426         pkt.size   =  sizeof(AVPicture);
1427         pkt.pts    = av_rescale_q(in_picture->pts, enc->time_base, ost->st->time_base);
1428         pkt.flags |= AV_PKT_FLAG_KEY;
1429
1430         write_frame(s, &pkt, ost);
1431     } else {
1432         int got_packet;
1433         AVFrame big_picture;
1434
1435         big_picture = *in_picture;
1436         /* better than nothing: use input picture interlaced
1437            settings */
1438         big_picture.interlaced_frame = in_picture->interlaced_frame;
1439         if (ost->st->codec->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)) {
1440             if (ost->top_field_first == -1)
1441                 big_picture.top_field_first = in_picture->top_field_first;
1442             else
1443                 big_picture.top_field_first = !!ost->top_field_first;
1444         }
1445
1446         /* handles same_quant here. This is not correct because it may
1447            not be a global option */
1448         big_picture.quality = quality;
1449         if (!enc->me_threshold)
1450             big_picture.pict_type = 0;
1451         if (ost->forced_kf_index < ost->forced_kf_count &&
1452             big_picture.pts >= ost->forced_kf_pts[ost->forced_kf_index]) {
1453             big_picture.pict_type = AV_PICTURE_TYPE_I;
1454             ost->forced_kf_index++;
1455         }
1456         ret = avcodec_encode_video2(enc, &pkt, &big_picture, &got_packet);
1457         if (ret < 0) {
1458             av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
1459             exit_program(1);
1460         }
1461
1462         if (got_packet) {
1463             if (pkt.pts != AV_NOPTS_VALUE)
1464                 pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
1465             if (pkt.dts != AV_NOPTS_VALUE)
1466                 pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
1467
1468             write_frame(s, &pkt, ost);
1469             *frame_size = pkt.size;
1470             video_size += pkt.size;
1471
1472             /* if two pass, output log */
1473             if (ost->logfile && enc->stats_out) {
1474                 fprintf(ost->logfile, "%s", enc->stats_out);
1475             }
1476         }
1477     }
1478     ost->sync_opts++;
1479     /*
1480      * For video, number of frames in == number of packets out.
1481      * But there may be reordering, so we can't throw away frames on encoder
1482      * flush, we need to limit them here, before they go into encoder.
1483      */
1484     ost->frame_number++;
1485 }
1486
1487 static double psnr(double d)
1488 {
1489     return -10.0 * log(d) / log(10.0);
1490 }
1491
1492 static void do_video_stats(AVFormatContext *os, OutputStream *ost,
1493                            int frame_size)
1494 {
1495     AVCodecContext *enc;
1496     int frame_number;
1497     double ti1, bitrate, avg_bitrate;
1498
1499     /* this is executed just the first time do_video_stats is called */
1500     if (!vstats_file) {
1501         vstats_file = fopen(vstats_filename, "w");
1502         if (!vstats_file) {
1503             perror("fopen");
1504             exit_program(1);
1505         }
1506     }
1507
1508     enc = ost->st->codec;
1509     if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1510         frame_number = ost->frame_number;
1511         fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality / (float)FF_QP2LAMBDA);
1512         if (enc->flags&CODEC_FLAG_PSNR)
1513             fprintf(vstats_file, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
1514
1515         fprintf(vstats_file,"f_size= %6d ", frame_size);
1516         /* compute pts value */
1517         ti1 = ost->sync_opts * av_q2d(enc->time_base);
1518         if (ti1 < 0.01)
1519             ti1 = 0.01;
1520
1521         bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1522         avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
1523         fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1524                (double)video_size / 1024, ti1, bitrate, avg_bitrate);
1525         fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(enc->coded_frame->pict_type));
1526     }
1527 }
1528
1529 /* check for new output on any of the filtergraphs */
1530 static int poll_filters(void)
1531 {
1532     AVFilterBufferRef *picref;
1533     AVFrame *filtered_frame = NULL;
1534     int i, frame_size;
1535
1536     for (i = 0; i < nb_output_streams; i++) {
1537         OutputStream *ost = output_streams[i];
1538         OutputFile    *of = output_files[ost->file_index];
1539         int ret = 0;
1540
1541         if (!ost->filter)
1542             continue;
1543
1544         if (!ost->filtered_frame && !(ost->filtered_frame = avcodec_alloc_frame())) {
1545             return AVERROR(ENOMEM);
1546         } else
1547             avcodec_get_frame_defaults(ost->filtered_frame);
1548         filtered_frame = ost->filtered_frame;
1549
1550         while (ret >= 0 && !ost->is_past_recording_time) {
1551             if (ost->enc->type == AVMEDIA_TYPE_AUDIO &&
1552                 !(ost->enc->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE))
1553                 ret = av_buffersink_read_samples(ost->filter->filter, &picref,
1554                                                  ost->st->codec->frame_size);
1555             else
1556                 ret = av_buffersink_read(ost->filter->filter, &picref);
1557
1558             if (ret < 0)
1559                 break;
1560
1561             avfilter_copy_buf_props(filtered_frame, picref);
1562             if (picref->pts != AV_NOPTS_VALUE)
1563                 filtered_frame->pts = av_rescale_q(picref->pts,
1564                                                    ost->filter->filter->inputs[0]->time_base,
1565                                                    ost->st->codec->time_base) -
1566                                       av_rescale_q(of->start_time,
1567                                                    AV_TIME_BASE_Q,
1568                                                    ost->st->codec->time_base);
1569
1570             if (of->start_time && filtered_frame->pts < of->start_time) {
1571                 avfilter_unref_buffer(picref);
1572                 continue;
1573             }
1574
1575             switch (ost->filter->filter->inputs[0]->type) {
1576             case AVMEDIA_TYPE_VIDEO:
1577                 if (!ost->frame_aspect_ratio)
1578                     ost->st->codec->sample_aspect_ratio = picref->video->pixel_aspect;
1579
1580                 do_video_out(of->ctx, ost, filtered_frame, &frame_size,
1581                              same_quant ? ost->last_quality :
1582                                           ost->st->codec->global_quality);
1583                 if (vstats_filename && frame_size)
1584                     do_video_stats(of->ctx, ost, frame_size);
1585                 break;
1586             case AVMEDIA_TYPE_AUDIO:
1587                 do_audio_out(of->ctx, ost, filtered_frame);
1588                 break;
1589             default:
1590                 // TODO support subtitle filters
1591                 av_assert0(0);
1592             }
1593
1594             avfilter_unref_buffer(picref);
1595         }
1596     }
1597     return 0;
1598 }
1599
1600 static void print_report(int is_last_report, int64_t timer_start)
1601 {
1602     char buf[1024];
1603     OutputStream *ost;
1604     AVFormatContext *oc;
1605     int64_t total_size;
1606     AVCodecContext *enc;
1607     int frame_number, vid, i;
1608     double bitrate, ti1, pts;
1609     static int64_t last_time = -1;
1610     static int qp_histogram[52];
1611
1612     if (!print_stats && !is_last_report)
1613         return;
1614
1615     if (!is_last_report) {
1616         int64_t cur_time;
1617         /* display the report every 0.5 seconds */
1618         cur_time = av_gettime();
1619         if (last_time == -1) {
1620             last_time = cur_time;
1621             return;
1622         }
1623         if ((cur_time - last_time) < 500000)
1624             return;
1625         last_time = cur_time;
1626     }
1627
1628
1629     oc = output_files[0]->ctx;
1630
1631     total_size = avio_size(oc->pb);
1632     if (total_size < 0) // FIXME improve avio_size() so it works with non seekable output too
1633         total_size = avio_tell(oc->pb);
1634
1635     buf[0] = '\0';
1636     ti1 = 1e10;
1637     vid = 0;
1638     for (i = 0; i < nb_output_streams; i++) {
1639         float q = -1;
1640         ost = output_streams[i];
1641         enc = ost->st->codec;
1642         if (!ost->stream_copy && enc->coded_frame)
1643             q = enc->coded_frame->quality / (float)FF_QP2LAMBDA;
1644         if (vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1645             snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ", q);
1646         }
1647         if (!vid && enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1648             float t = (av_gettime() - timer_start) / 1000000.0;
1649
1650             frame_number = ost->frame_number;
1651             snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d fps=%3d q=%3.1f ",
1652                      frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 0, q);
1653             if (is_last_report)
1654                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
1655             if (qp_hist) {
1656                 int j;
1657                 int qp = lrintf(q);
1658                 if (qp >= 0 && qp < FF_ARRAY_ELEMS(qp_histogram))
1659                     qp_histogram[qp]++;
1660                 for (j = 0; j < 32; j++)
1661                     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j] + 1) / log(2)));
1662             }
1663             if (enc->flags&CODEC_FLAG_PSNR) {
1664                 int j;
1665                 double error, error_sum = 0;
1666                 double scale, scale_sum = 0;
1667                 char type[3] = { 'Y','U','V' };
1668                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
1669                 for (j = 0; j < 3; j++) {
1670                     if (is_last_report) {
1671                         error = enc->error[j];
1672                         scale = enc->width * enc->height * 255.0 * 255.0 * frame_number;
1673                     } else {
1674                         error = enc->coded_frame->error[j];
1675                         scale = enc->width * enc->height * 255.0 * 255.0;
1676                     }
1677                     if (j)
1678                         scale /= 4;
1679                     error_sum += error;
1680                     scale_sum += scale;
1681                     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error / scale));
1682                 }
1683                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum / scale_sum));
1684             }
1685             vid = 1;
1686         }
1687         /* compute min output value */
1688         pts = (double)ost->st->pts.val * av_q2d(ost->st->time_base);
1689         if ((pts < ti1) && (pts > 0))
1690             ti1 = pts;
1691     }
1692     if (ti1 < 0.01)
1693         ti1 = 0.01;
1694
1695     bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1696
1697     snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1698             "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
1699             (double)total_size / 1024, ti1, bitrate);
1700
1701     if (nb_frames_dup || nb_frames_drop)
1702         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
1703                 nb_frames_dup, nb_frames_drop);
1704
1705     av_log(NULL, AV_LOG_INFO, "%s    \r", buf);
1706
1707     fflush(stderr);
1708
1709     if (is_last_report) {
1710         int64_t raw= audio_size + video_size + extra_size;
1711         av_log(NULL, AV_LOG_INFO, "\n");
1712         av_log(NULL, AV_LOG_INFO, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
1713                video_size / 1024.0,
1714                audio_size / 1024.0,
1715                extra_size / 1024.0,
1716                100.0 * (total_size - raw) / raw
1717         );
1718     }
1719 }
1720
1721 static void flush_encoders(void)
1722 {
1723     int i, ret;
1724
1725     for (i = 0; i < nb_output_streams; i++) {
1726         OutputStream   *ost = output_streams[i];
1727         AVCodecContext *enc = ost->st->codec;
1728         AVFormatContext *os = output_files[ost->file_index]->ctx;
1729         int stop_encoding = 0;
1730
1731         if (!ost->encoding_needed)
1732             continue;
1733
1734         if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO && enc->frame_size <= 1)
1735             continue;
1736         if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE) && enc->codec->id == CODEC_ID_RAWVIDEO)
1737             continue;
1738
1739         for (;;) {
1740             int (*encode)(AVCodecContext*, AVPacket*, const AVFrame*, int*) = NULL;
1741             const char *desc;
1742             int64_t *size;
1743
1744             switch (ost->st->codec->codec_type) {
1745             case AVMEDIA_TYPE_AUDIO:
1746                 encode = avcodec_encode_audio2;
1747                 desc   = "Audio";
1748                 size   = &audio_size;
1749                 break;
1750             case AVMEDIA_TYPE_VIDEO:
1751                 encode = avcodec_encode_video2;
1752                 desc   = "Video";
1753                 size   = &video_size;
1754                 break;
1755             default:
1756                 stop_encoding = 1;
1757             }
1758
1759             if (encode) {
1760                 AVPacket pkt;
1761                 int got_packet;
1762                 av_init_packet(&pkt);
1763                 pkt.data = NULL;
1764                 pkt.size = 0;
1765
1766                 ret = encode(enc, &pkt, NULL, &got_packet);
1767                 if (ret < 0) {
1768                     av_log(NULL, AV_LOG_FATAL, "%s encoding failed\n", desc);
1769                     exit_program(1);
1770                 }
1771                 *size += ret;
1772                 if (ost->logfile && enc->stats_out) {
1773                     fprintf(ost->logfile, "%s", enc->stats_out);
1774                 }
1775                 if (!got_packet) {
1776                     stop_encoding = 1;
1777                     break;
1778                 }
1779                 if (pkt.pts != AV_NOPTS_VALUE)
1780                     pkt.pts = av_rescale_q(pkt.pts, enc->time_base, ost->st->time_base);
1781                 if (pkt.dts != AV_NOPTS_VALUE)
1782                     pkt.dts = av_rescale_q(pkt.dts, enc->time_base, ost->st->time_base);
1783                 write_frame(os, &pkt, ost);
1784             }
1785
1786             if (stop_encoding)
1787                 break;
1788         }
1789     }
1790 }
1791
1792 /*
1793  * Check whether a packet from ist should be written into ost at this time
1794  */
1795 static int check_output_constraints(InputStream *ist, OutputStream *ost)
1796 {
1797     OutputFile *of = output_files[ost->file_index];
1798     int ist_index  = input_files[ist->file_index]->ist_index + ist->st->index;
1799
1800     if (ost->source_index != ist_index)
1801         return 0;
1802
1803     if (of->start_time && ist->last_dts < of->start_time)
1804         return 0;
1805
1806     return 1;
1807 }
1808
1809 static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *pkt)
1810 {
1811     OutputFile *of = output_files[ost->file_index];
1812     int64_t ost_tb_start_time = av_rescale_q(of->start_time, AV_TIME_BASE_Q, ost->st->time_base);
1813     AVPacket opkt;
1814
1815     av_init_packet(&opkt);
1816
1817     if ((!ost->frame_number && !(pkt->flags & AV_PKT_FLAG_KEY)) &&
1818         !ost->copy_initial_nonkeyframes)
1819         return;
1820
1821     if (of->recording_time != INT64_MAX &&
1822         ist->last_dts >= of->recording_time + of->start_time) {
1823         ost->is_past_recording_time = 1;
1824         return;
1825     }
1826
1827     /* force the input stream PTS */
1828     if (ost->st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1829         audio_size += pkt->size;
1830     else if (ost->st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1831         video_size += pkt->size;
1832         ost->sync_opts++;
1833     }
1834
1835     if (pkt->pts != AV_NOPTS_VALUE)
1836         opkt.pts = av_rescale_q(pkt->pts, ist->st->time_base, ost->st->time_base) - ost_tb_start_time;
1837     else
1838         opkt.pts = AV_NOPTS_VALUE;
1839
1840     if (pkt->dts == AV_NOPTS_VALUE)
1841         opkt.dts = av_rescale_q(ist->last_dts, AV_TIME_BASE_Q, ost->st->time_base);
1842     else
1843         opkt.dts = av_rescale_q(pkt->dts, ist->st->time_base, ost->st->time_base);
1844     opkt.dts -= ost_tb_start_time;
1845
1846     opkt.duration = av_rescale_q(pkt->duration, ist->st->time_base, ost->st->time_base);
1847     opkt.flags    = pkt->flags;
1848
1849     // FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1850     if (  ost->st->codec->codec_id != CODEC_ID_H264
1851        && ost->st->codec->codec_id != CODEC_ID_MPEG1VIDEO
1852        && ost->st->codec->codec_id != CODEC_ID_MPEG2VIDEO
1853        && ost->st->codec->codec_id != CODEC_ID_VC1
1854        ) {
1855         if (av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, pkt->data, pkt->size, pkt->flags & AV_PKT_FLAG_KEY))
1856             opkt.destruct = av_destruct_packet;
1857     } else {
1858         opkt.data = pkt->data;
1859         opkt.size = pkt->size;
1860     }
1861
1862     write_frame(of->ctx, &opkt, ost);
1863     ost->st->codec->frame_number++;
1864     av_free_packet(&opkt);
1865 }
1866
1867 static void rate_emu_sleep(InputStream *ist)
1868 {
1869     if (input_files[ist->file_index]->rate_emu) {
1870         int64_t pts = av_rescale(ist->last_dts, 1000000, AV_TIME_BASE);
1871         int64_t now = av_gettime() - ist->start;
1872         if (pts > now)
1873             av_usleep(pts - now);
1874     }
1875 }
1876
1877 static int guess_input_channel_layout(InputStream *ist)
1878 {
1879     AVCodecContext *dec = ist->st->codec;
1880
1881     if (!dec->channel_layout) {
1882         char layout_name[256];
1883
1884         dec->channel_layout = av_get_default_channel_layout(dec->channels);
1885         if (!dec->channel_layout)
1886             return 0;
1887         av_get_channel_layout_string(layout_name, sizeof(layout_name),
1888                                      dec->channels, dec->channel_layout);
1889         av_log(NULL, AV_LOG_WARNING, "Guessed Channel Layout for  Input Stream "
1890                "#%d.%d : %s\n", ist->file_index, ist->st->index, layout_name);
1891     }
1892     return 1;
1893 }
1894
1895 static int decode_audio(InputStream *ist, AVPacket *pkt, int *got_output)
1896 {
1897     AVFrame *decoded_frame;
1898     AVCodecContext *avctx = ist->st->codec;
1899     int bps = av_get_bytes_per_sample(ist->st->codec->sample_fmt);
1900     int i, ret, resample_changed;
1901
1902     if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
1903         return AVERROR(ENOMEM);
1904     else
1905         avcodec_get_frame_defaults(ist->decoded_frame);
1906     decoded_frame = ist->decoded_frame;
1907
1908     ret = avcodec_decode_audio4(avctx, decoded_frame, got_output, pkt);
1909     if (ret < 0) {
1910         return ret;
1911     }
1912
1913     if (!*got_output) {
1914         /* no audio frame */
1915         if (!pkt->size)
1916             for (i = 0; i < ist->nb_filters; i++)
1917                 av_buffersrc_buffer(ist->filters[i]->filter, NULL);
1918         return ret;
1919     }
1920
1921     /* if the decoder provides a pts, use it instead of the last packet pts.
1922        the decoder could be delaying output by a packet or more. */
1923     if (decoded_frame->pts != AV_NOPTS_VALUE)
1924         ist->next_dts = decoded_frame->pts;
1925     else if (pkt->pts != AV_NOPTS_VALUE) {
1926         decoded_frame->pts = pkt->pts;
1927         pkt->pts           = AV_NOPTS_VALUE;
1928     }
1929
1930     // preprocess audio (volume)
1931     if (audio_volume != 256) {
1932         int decoded_data_size = decoded_frame->nb_samples * avctx->channels * bps;
1933         void *samples = decoded_frame->data[0];
1934         switch (avctx->sample_fmt) {
1935         case AV_SAMPLE_FMT_U8:
1936         {
1937             uint8_t *volp = samples;
1938             for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
1939                 int v = (((*volp - 128) * audio_volume + 128) >> 8) + 128;
1940                 *volp++ = av_clip_uint8(v);
1941             }
1942             break;
1943         }
1944         case AV_SAMPLE_FMT_S16:
1945         {
1946             int16_t *volp = samples;
1947             for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
1948                 int v = ((*volp) * audio_volume + 128) >> 8;
1949                 *volp++ = av_clip_int16(v);
1950             }
1951             break;
1952         }
1953         case AV_SAMPLE_FMT_S32:
1954         {
1955             int32_t *volp = samples;
1956             for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
1957                 int64_t v = (((int64_t)*volp * audio_volume + 128) >> 8);
1958                 *volp++ = av_clipl_int32(v);
1959             }
1960             break;
1961         }
1962         case AV_SAMPLE_FMT_FLT:
1963         {
1964             float *volp = samples;
1965             float scale = audio_volume / 256.f;
1966             for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
1967                 *volp++ *= scale;
1968             }
1969             break;
1970         }
1971         case AV_SAMPLE_FMT_DBL:
1972         {
1973             double *volp = samples;
1974             double scale = audio_volume / 256.;
1975             for (i = 0; i < (decoded_data_size / sizeof(*volp)); i++) {
1976                 *volp++ *= scale;
1977             }
1978             break;
1979         }
1980         default:
1981             av_log(NULL, AV_LOG_FATAL,
1982                    "Audio volume adjustment on sample format %s is not supported.\n",
1983                    av_get_sample_fmt_name(ist->st->codec->sample_fmt));
1984             exit_program(1);
1985         }
1986     }
1987
1988     rate_emu_sleep(ist);
1989
1990     resample_changed = ist->resample_sample_fmt     != decoded_frame->format         ||
1991                        ist->resample_channels       != avctx->channels               ||
1992                        ist->resample_channel_layout != decoded_frame->channel_layout ||
1993                        ist->resample_sample_rate    != decoded_frame->sample_rate;
1994     if (resample_changed) {
1995         char layout1[64], layout2[64];
1996
1997         if (!guess_input_channel_layout(ist)) {
1998             av_log(NULL, AV_LOG_FATAL, "Unable to find default channel "
1999                    "layout for Input Stream #%d.%d\n", ist->file_index,
2000                    ist->st->index);
2001             exit_program(1);
2002         }
2003         decoded_frame->channel_layout = avctx->channel_layout;
2004
2005         av_get_channel_layout_string(layout1, sizeof(layout1), ist->resample_channels,
2006                                      ist->resample_channel_layout);
2007         av_get_channel_layout_string(layout2, sizeof(layout2), avctx->channels,
2008                                      decoded_frame->channel_layout);
2009
2010         av_log(NULL, AV_LOG_INFO,
2011                "Input stream #%d:%d frame changed from rate:%d fmt:%s ch:%d chl:%s to rate:%d fmt:%s ch:%d chl:%s\n",
2012                ist->file_index, ist->st->index,
2013                ist->resample_sample_rate,  av_get_sample_fmt_name(ist->resample_sample_fmt),
2014                ist->resample_channels, layout1,
2015                decoded_frame->sample_rate, av_get_sample_fmt_name(decoded_frame->format),
2016                avctx->channels, layout2);
2017
2018         ist->resample_sample_fmt     = decoded_frame->format;
2019         ist->resample_sample_rate    = decoded_frame->sample_rate;
2020         ist->resample_channel_layout = decoded_frame->channel_layout;
2021         ist->resample_channels       = avctx->channels;
2022
2023         for (i = 0; i < nb_filtergraphs; i++)
2024             if (ist_in_filtergraph(filtergraphs[i], ist) &&
2025                 configure_filtergraph(filtergraphs[i]) < 0) {
2026                 av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
2027                 exit_program(1);
2028             }
2029     }
2030
2031     for (i = 0; i < ist->nb_filters; i++)
2032         av_buffersrc_write_frame(ist->filters[i]->filter, decoded_frame);
2033
2034     return ret;
2035 }
2036
2037 static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
2038 {
2039     AVFrame *decoded_frame;
2040     void *buffer_to_free = NULL;
2041     int i, ret = 0, resample_changed;
2042     float quality;
2043
2044     if (!ist->decoded_frame && !(ist->decoded_frame = avcodec_alloc_frame()))
2045         return AVERROR(ENOMEM);
2046     else
2047         avcodec_get_frame_defaults(ist->decoded_frame);
2048     decoded_frame = ist->decoded_frame;
2049
2050     ret = avcodec_decode_video2(ist->st->codec,
2051                                 decoded_frame, got_output, pkt);
2052     if (ret < 0)
2053         return ret;
2054
2055     quality = same_quant ? decoded_frame->quality : 0;
2056     if (!*got_output) {
2057         /* no picture yet */
2058         if (!pkt->size)
2059             for (i = 0; i < ist->nb_filters; i++)
2060                 av_buffersrc_buffer(ist->filters[i]->filter, NULL);
2061         return ret;
2062     }
2063     decoded_frame->pts = guess_correct_pts(&ist->pts_ctx, decoded_frame->pkt_pts,
2064                                            decoded_frame->pkt_dts);
2065     pkt->size = 0;
2066     pre_process_video_frame(ist, (AVPicture *)decoded_frame, &buffer_to_free);
2067
2068     rate_emu_sleep(ist);
2069
2070     if (ist->st->sample_aspect_ratio.num)
2071         decoded_frame->sample_aspect_ratio = ist->st->sample_aspect_ratio;
2072
2073     resample_changed = ist->resample_width   != decoded_frame->width  ||
2074                        ist->resample_height  != decoded_frame->height ||
2075                        ist->resample_pix_fmt != decoded_frame->format;
2076     if (resample_changed) {
2077         av_log(NULL, AV_LOG_INFO,
2078                "Input stream #%d:%d frame changed from size:%dx%d fmt:%s to size:%dx%d fmt:%s\n",
2079                ist->file_index, ist->st->index,
2080                ist->resample_width,  ist->resample_height,  av_get_pix_fmt_name(ist->resample_pix_fmt),
2081                decoded_frame->width, decoded_frame->height, av_get_pix_fmt_name(decoded_frame->format));
2082
2083         ist->resample_width   = decoded_frame->width;
2084         ist->resample_height  = decoded_frame->height;
2085         ist->resample_pix_fmt = decoded_frame->format;
2086
2087         for (i = 0; i < nb_filtergraphs; i++)
2088             if (ist_in_filtergraph(filtergraphs[i], ist) &&
2089                 configure_filtergraph(filtergraphs[i]) < 0) {
2090                 av_log(NULL, AV_LOG_FATAL, "Error reinitializing filters!\n");
2091                 exit_program(1);
2092             }
2093     }
2094
2095     for (i = 0; i < ist->nb_filters; i++) {
2096         // XXX what an ugly hack
2097         if (ist->filters[i]->graph->nb_outputs == 1)
2098             ist->filters[i]->graph->outputs[0]->ost->last_quality = quality;
2099
2100         if (ist->st->codec->codec->capabilities & CODEC_CAP_DR1) {
2101             FrameBuffer      *buf = decoded_frame->opaque;
2102             AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
2103                                         decoded_frame->data, decoded_frame->linesize,
2104                                         AV_PERM_READ | AV_PERM_PRESERVE,
2105                                         ist->st->codec->width, ist->st->codec->height,
2106                                         ist->st->codec->pix_fmt);
2107
2108             avfilter_copy_frame_props(fb, decoded_frame);
2109             fb->buf->priv           = buf;
2110             fb->buf->free           = filter_release_buffer;
2111
2112             buf->refcount++;
2113             av_buffersrc_buffer(ist->filters[i]->filter, fb);
2114         } else
2115             av_buffersrc_write_frame(ist->filters[i]->filter, decoded_frame);
2116     }
2117
2118     av_free(buffer_to_free);
2119     return ret;
2120 }
2121
2122 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
2123 {
2124     AVSubtitle subtitle;
2125     int i, ret = avcodec_decode_subtitle2(ist->st->codec,
2126                                           &subtitle, got_output, pkt);
2127     if (ret < 0)
2128         return ret;
2129     if (!*got_output)
2130         return ret;
2131
2132     rate_emu_sleep(ist);
2133
2134     for (i = 0; i < nb_output_streams; i++) {
2135         OutputStream *ost = output_streams[i];
2136
2137         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
2138             continue;
2139
2140         do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pkt->pts);
2141     }
2142
2143     avsubtitle_free(&subtitle);
2144     return ret;
2145 }
2146
2147 /* pkt = NULL means EOF (needed to flush decoder buffers) */
2148 static int output_packet(InputStream *ist, const AVPacket *pkt)
2149 {
2150     int i;
2151     int got_output;
2152     AVPacket avpkt;
2153
2154     if (ist->next_dts == AV_NOPTS_VALUE)
2155         ist->next_dts = ist->last_dts;
2156
2157     if (pkt == NULL) {
2158         /* EOF handling */
2159         av_init_packet(&avpkt);
2160         avpkt.data = NULL;
2161         avpkt.size = 0;
2162         goto handle_eof;
2163     } else {
2164         avpkt = *pkt;
2165     }
2166
2167     if (pkt->dts != AV_NOPTS_VALUE)
2168         ist->next_dts = ist->last_dts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
2169
2170     // while we have more to decode or while the decoder did output something on EOF
2171     while (ist->decoding_needed && (avpkt.size > 0 || (!pkt && got_output))) {
2172         int ret = 0;
2173     handle_eof:
2174
2175         ist->last_dts = ist->next_dts;
2176
2177         if (avpkt.size && avpkt.size != pkt->size) {
2178             av_log(NULL, ist->showed_multi_packet_warning ? AV_LOG_VERBOSE : AV_LOG_WARNING,
2179                    "Multiple frames in a packet from stream %d\n", pkt->stream_index);
2180             ist->showed_multi_packet_warning = 1;
2181         }
2182
2183         switch (ist->st->codec->codec_type) {
2184         case AVMEDIA_TYPE_AUDIO:
2185             ret = decode_audio    (ist, &avpkt, &got_output);
2186             break;
2187         case AVMEDIA_TYPE_VIDEO:
2188             ret = decode_video    (ist, &avpkt, &got_output);
2189             if (avpkt.duration)
2190                 ist->next_dts += av_rescale_q(avpkt.duration, ist->st->time_base, AV_TIME_BASE_Q);
2191             else if (ist->st->r_frame_rate.num)
2192                 ist->next_dts += av_rescale_q(1, (AVRational){ist->st->r_frame_rate.den,
2193                                                               ist->st->r_frame_rate.num},
2194                                               AV_TIME_BASE_Q);
2195             else if (ist->st->codec->time_base.num != 0) {
2196                 int ticks      = ist->st->parser ? ist->st->parser->repeat_pict + 1 :
2197                                                    ist->st->codec->ticks_per_frame;
2198                 ist->next_dts += av_rescale_q(ticks, ist->st->codec->time_base, AV_TIME_BASE_Q);
2199             }
2200             break;
2201         case AVMEDIA_TYPE_SUBTITLE:
2202             ret = transcode_subtitles(ist, &avpkt, &got_output);
2203             break;
2204         default:
2205             return -1;
2206         }
2207
2208         if (ret < 0)
2209             return ret;
2210         // touch data and size only if not EOF
2211         if (pkt) {
2212             avpkt.data += ret;
2213             avpkt.size -= ret;
2214         }
2215         if (!got_output) {
2216             continue;
2217         }
2218     }
2219
2220     /* handle stream copy */
2221     if (!ist->decoding_needed) {
2222         rate_emu_sleep(ist);
2223         ist->last_dts = ist->next_dts;
2224         switch (ist->st->codec->codec_type) {
2225         case AVMEDIA_TYPE_AUDIO:
2226             ist->next_dts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
2227                              ist->st->codec->sample_rate;
2228             break;
2229         case AVMEDIA_TYPE_VIDEO:
2230             if (ist->st->codec->time_base.num != 0) {
2231                 int ticks = ist->st->parser ? ist->st->parser->repeat_pict + 1 : ist->st->codec->ticks_per_frame;
2232                 ist->next_dts += ((int64_t)AV_TIME_BASE *
2233                                   ist->st->codec->time_base.num * ticks) /
2234                                   ist->st->codec->time_base.den;
2235             }
2236             break;
2237         }
2238     }
2239     for (i = 0; pkt && i < nb_output_streams; i++) {
2240         OutputStream *ost = output_streams[i];
2241
2242         if (!check_output_constraints(ist, ost) || ost->encoding_needed)
2243             continue;
2244
2245         do_streamcopy(ist, ost, pkt);
2246     }
2247
2248     return 0;
2249 }
2250
2251 static void print_sdp(void)
2252 {
2253     char sdp[2048];
2254     int i;
2255     AVFormatContext **avc = av_malloc(sizeof(*avc) * nb_output_files);
2256
2257     if (!avc)
2258         exit_program(1);
2259     for (i = 0; i < nb_output_files; i++)
2260         avc[i] = output_files[i]->ctx;
2261
2262     av_sdp_create(avc, nb_output_files, sdp, sizeof(sdp));
2263     printf("SDP:\n%s\n", sdp);
2264     fflush(stdout);
2265     av_freep(&avc);
2266 }
2267
2268 static int init_input_stream(int ist_index, char *error, int error_len)
2269 {
2270     int i;
2271     InputStream *ist = input_streams[ist_index];
2272     if (ist->decoding_needed) {
2273         AVCodec *codec = ist->dec;
2274         if (!codec) {
2275             snprintf(error, error_len, "Decoder (codec id %d) not found for input stream #%d:%d",
2276                     ist->st->codec->codec_id, ist->file_index, ist->st->index);
2277             return AVERROR(EINVAL);
2278         }
2279
2280         /* update requested sample format for the decoder based on the
2281            corresponding encoder sample format */
2282         for (i = 0; i < nb_output_streams; i++) {
2283             OutputStream *ost = output_streams[i];
2284             if (ost->source_index == ist_index) {
2285                 update_sample_fmt(ist->st->codec, codec, ost->st->codec);
2286                 break;
2287             }
2288         }
2289
2290         if (codec->type == AVMEDIA_TYPE_VIDEO && codec->capabilities & CODEC_CAP_DR1) {
2291             ist->st->codec->get_buffer     = codec_get_buffer;
2292             ist->st->codec->release_buffer = codec_release_buffer;
2293             ist->st->codec->opaque         = &ist->buffer_pool;
2294         }
2295
2296         if (!av_dict_get(ist->opts, "threads", NULL, 0))
2297             av_dict_set(&ist->opts, "threads", "auto", 0);
2298         if (avcodec_open2(ist->st->codec, codec, &ist->opts) < 0) {
2299             snprintf(error, error_len, "Error while opening decoder for input stream #%d:%d",
2300                     ist->file_index, ist->st->index);
2301             return AVERROR(EINVAL);
2302         }
2303         assert_codec_experimental(ist->st->codec, 0);
2304         assert_avoptions(ist->opts);
2305     }
2306
2307     ist->last_dts = ist->st->avg_frame_rate.num ? - ist->st->codec->has_b_frames * AV_TIME_BASE / av_q2d(ist->st->avg_frame_rate) : 0;
2308     ist->next_dts = AV_NOPTS_VALUE;
2309     init_pts_correction(&ist->pts_ctx);
2310     ist->is_start = 1;
2311
2312     return 0;
2313 }
2314
2315 static InputStream *get_input_stream(OutputStream *ost)
2316 {
2317     if (ost->source_index >= 0)
2318         return input_streams[ost->source_index];
2319
2320     if (ost->filter) {
2321         FilterGraph *fg = ost->filter->graph;
2322         int i;
2323
2324         for (i = 0; i < fg->nb_inputs; i++)
2325             if (fg->inputs[i]->ist->st->codec->codec_type == ost->st->codec->codec_type)
2326                 return fg->inputs[i]->ist;
2327     }
2328
2329     return NULL;
2330 }
2331
2332 static void parse_forced_key_frames(char *kf, OutputStream *ost,
2333                                     AVCodecContext *avctx)
2334 {
2335     char *p;
2336     int n = 1, i;
2337     int64_t t;
2338
2339     for (p = kf; *p; p++)
2340         if (*p == ',')
2341             n++;
2342     ost->forced_kf_count = n;
2343     ost->forced_kf_pts   = av_malloc(sizeof(*ost->forced_kf_pts) * n);
2344     if (!ost->forced_kf_pts) {
2345         av_log(NULL, AV_LOG_FATAL, "Could not allocate forced key frames array.\n");
2346         exit_program(1);
2347     }
2348     for (i = 0; i < n; i++) {
2349         p = i ? strchr(p, ',') + 1 : kf;
2350         t = parse_time_or_die("force_key_frames", p, 1);
2351         ost->forced_kf_pts[i] = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
2352     }
2353 }
2354
2355 static int transcode_init(void)
2356 {
2357     int ret = 0, i, j, k;
2358     AVFormatContext *oc;
2359     AVCodecContext *codec, *icodec;
2360     OutputStream *ost;
2361     InputStream *ist;
2362     char error[1024];
2363     int want_sdp = 1;
2364
2365     /* init framerate emulation */
2366     for (i = 0; i < nb_input_files; i++) {
2367         InputFile *ifile = input_files[i];
2368         if (ifile->rate_emu)
2369             for (j = 0; j < ifile->nb_streams; j++)
2370                 input_streams[j + ifile->ist_index]->start = av_gettime();
2371     }
2372
2373     /* output stream init */
2374     for (i = 0; i < nb_output_files; i++) {
2375         oc = output_files[i]->ctx;
2376         if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
2377             av_dump_format(oc, i, oc->filename, 1);
2378             av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
2379             return AVERROR(EINVAL);
2380         }
2381     }
2382
2383     /* init complex filtergraphs */
2384     for (i = 0; i < nb_filtergraphs; i++)
2385         if ((ret = avfilter_graph_config(filtergraphs[i]->graph, NULL)) < 0)
2386             return ret;
2387
2388     /* for each output stream, we compute the right encoding parameters */
2389     for (i = 0; i < nb_output_streams; i++) {
2390         ost = output_streams[i];
2391         oc  = output_files[ost->file_index]->ctx;
2392         ist = get_input_stream(ost);
2393
2394         if (ost->attachment_filename)
2395             continue;
2396
2397         codec  = ost->st->codec;
2398
2399         if (ist) {
2400             icodec = ist->st->codec;
2401
2402             ost->st->disposition          = ist->st->disposition;
2403             codec->bits_per_raw_sample    = icodec->bits_per_raw_sample;
2404             codec->chroma_sample_location = icodec->chroma_sample_location;
2405         }
2406
2407         if (ost->stream_copy) {
2408             uint64_t extra_size;
2409
2410             av_assert0(ist && !ost->filter);
2411
2412             extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
2413
2414             if (extra_size > INT_MAX) {
2415                 return AVERROR(EINVAL);
2416             }
2417
2418             /* if stream_copy is selected, no need to decode or encode */
2419             codec->codec_id   = icodec->codec_id;
2420             codec->codec_type = icodec->codec_type;
2421
2422             if (!codec->codec_tag) {
2423                 if (!oc->oformat->codec_tag ||
2424                      av_codec_get_id (oc->oformat->codec_tag, icodec->codec_tag) == codec->codec_id ||
2425                      av_codec_get_tag(oc->oformat->codec_tag, icodec->codec_id) <= 0)
2426                     codec->codec_tag = icodec->codec_tag;
2427             }
2428
2429             codec->bit_rate       = icodec->bit_rate;
2430             codec->rc_max_rate    = icodec->rc_max_rate;
2431             codec->rc_buffer_size = icodec->rc_buffer_size;
2432             codec->field_order    = icodec->field_order;
2433             codec->extradata      = av_mallocz(extra_size);
2434             if (!codec->extradata) {
2435                 return AVERROR(ENOMEM);
2436             }
2437             memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
2438             codec->extradata_size = icodec->extradata_size;
2439             if (!copy_tb) {
2440                 codec->time_base      = icodec->time_base;
2441                 codec->time_base.num *= icodec->ticks_per_frame;
2442                 av_reduce(&codec->time_base.num, &codec->time_base.den,
2443                           codec->time_base.num, codec->time_base.den, INT_MAX);
2444             } else
2445                 codec->time_base = ist->st->time_base;
2446
2447             switch (codec->codec_type) {
2448             case AVMEDIA_TYPE_AUDIO:
2449                 if (audio_volume != 256) {
2450                     av_log(NULL, AV_LOG_FATAL, "-acodec copy and -vol are incompatible (frames are not decoded)\n");
2451                     exit_program(1);
2452                 }
2453                 codec->channel_layout     = icodec->channel_layout;
2454                 codec->sample_rate        = icodec->sample_rate;
2455                 codec->channels           = icodec->channels;
2456                 codec->frame_size         = icodec->frame_size;
2457                 codec->audio_service_type = icodec->audio_service_type;
2458                 codec->block_align        = icodec->block_align;
2459                 break;
2460             case AVMEDIA_TYPE_VIDEO:
2461                 codec->pix_fmt            = icodec->pix_fmt;
2462                 codec->width              = icodec->width;
2463                 codec->height             = icodec->height;
2464                 codec->has_b_frames       = icodec->has_b_frames;
2465                 if (!codec->sample_aspect_ratio.num) {
2466                     codec->sample_aspect_ratio   =
2467                     ost->st->sample_aspect_ratio =
2468                         ist->st->sample_aspect_ratio.num ? ist->st->sample_aspect_ratio :
2469                         ist->st->codec->sample_aspect_ratio.num ?
2470                         ist->st->codec->sample_aspect_ratio : (AVRational){0, 1};
2471                 }
2472                 break;
2473             case AVMEDIA_TYPE_SUBTITLE:
2474                 codec->width  = icodec->width;
2475                 codec->height = icodec->height;
2476                 break;
2477             case AVMEDIA_TYPE_DATA:
2478             case AVMEDIA_TYPE_ATTACHMENT:
2479                 break;
2480             default:
2481                 abort();
2482             }
2483         } else {
2484             if (!ost->enc) {
2485                 /* should only happen when a default codec is not present. */
2486                 snprintf(error, sizeof(error), "Automatic encoder selection "
2487                          "failed for output stream #%d:%d. Default encoder for "
2488                          "format %s is probably disabled. Please choose an "
2489                          "encoder manually.\n", ost->file_index, ost->index,
2490                          oc->oformat->name);
2491                 ret = AVERROR(EINVAL);
2492                 goto dump_format;
2493             }
2494
2495             if (ist)
2496                 ist->decoding_needed = 1;
2497             ost->encoding_needed = 1;
2498
2499             /*
2500              * We want CFR output if and only if one of those is true:
2501              * 1) user specified output framerate with -r
2502              * 2) user specified -vsync cfr
2503              * 3) output format is CFR and the user didn't force vsync to
2504              *    something else than CFR
2505              *
2506              * in such a case, set ost->frame_rate
2507              */
2508             if (codec->codec_type == AVMEDIA_TYPE_VIDEO &&
2509                 !ost->frame_rate.num && ist &&
2510                 (video_sync_method ==  VSYNC_CFR ||
2511                  (video_sync_method ==  VSYNC_AUTO &&
2512                   !(oc->oformat->flags & (AVFMT_NOTIMESTAMPS | AVFMT_VARIABLE_FPS))))) {
2513                 ost->frame_rate = ist->st->r_frame_rate.num ? ist->st->r_frame_rate : (AVRational){25, 1};
2514                 if (ost->enc && ost->enc->supported_framerates && !ost->force_fps) {
2515                     int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates);
2516                     ost->frame_rate = ost->enc->supported_framerates[idx];
2517                 }
2518             }
2519
2520             if (!ost->filter &&
2521                 (codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2522                  codec->codec_type == AVMEDIA_TYPE_AUDIO)) {
2523                     FilterGraph *fg;
2524                     fg = init_simple_filtergraph(ist, ost);
2525                     if (configure_filtergraph(fg)) {
2526                         av_log(NULL, AV_LOG_FATAL, "Error opening filters!\n");
2527                         exit(1);
2528                     }
2529             }
2530
2531             switch (codec->codec_type) {
2532             case AVMEDIA_TYPE_AUDIO:
2533                 codec->sample_fmt     = ost->filter->filter->inputs[0]->format;
2534                 codec->sample_rate    = ost->filter->filter->inputs[0]->sample_rate;
2535                 codec->channel_layout = ost->filter->filter->inputs[0]->channel_layout;
2536                 codec->channels       = av_get_channel_layout_nb_channels(codec->channel_layout);
2537                 codec->time_base      = (AVRational){ 1, codec->sample_rate };
2538                 break;
2539             case AVMEDIA_TYPE_VIDEO:
2540                 codec->time_base = ost->filter->filter->inputs[0]->time_base;
2541
2542                 codec->width  = ost->filter->filter->inputs[0]->w;
2543                 codec->height = ost->filter->filter->inputs[0]->h;
2544                 codec->sample_aspect_ratio = ost->st->sample_aspect_ratio =
2545                     ost->frame_aspect_ratio ? // overridden by the -aspect cli option
2546                     av_d2q(ost->frame_aspect_ratio * codec->height/codec->width, 255) :
2547                     ost->filter->filter->inputs[0]->sample_aspect_ratio;
2548                 codec->pix_fmt = ost->filter->filter->inputs[0]->format;
2549
2550                 if (codec->width   != icodec->width  ||
2551                     codec->height  != icodec->height ||
2552                     codec->pix_fmt != icodec->pix_fmt) {
2553                     codec->bits_per_raw_sample = 0;
2554                 }
2555
2556                 if (ost->forced_keyframes)
2557                     parse_forced_key_frames(ost->forced_keyframes, ost,
2558                                             ost->st->codec);
2559                 break;
2560             case AVMEDIA_TYPE_SUBTITLE:
2561                 codec->time_base = (AVRational){1, 1000};
2562                 break;
2563             default:
2564                 abort();
2565                 break;
2566             }
2567             /* two pass mode */
2568             if ((codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
2569                 char logfilename[1024];
2570                 FILE *f;
2571
2572                 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
2573                          pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
2574                          i);
2575                 if (!strcmp(ost->enc->name, "libx264")) {
2576                     av_dict_set(&ost->opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE);
2577                 } else {
2578                     if (codec->flags & CODEC_FLAG_PASS1) {
2579                         f = fopen(logfilename, "wb");
2580                         if (!f) {
2581                             av_log(NULL, AV_LOG_FATAL, "Cannot write log file '%s' for pass-1 encoding: %s\n",
2582                                    logfilename, strerror(errno));
2583                             exit_program(1);
2584                         }
2585                         ost->logfile = f;
2586                     } else {
2587                         char  *logbuffer;
2588                         size_t logbuffer_size;
2589                         if (cmdutils_read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
2590                             av_log(NULL, AV_LOG_FATAL, "Error reading log file '%s' for pass-2 encoding\n",
2591                                    logfilename);
2592                             exit_program(1);
2593                         }
2594                         codec->stats_in = logbuffer;
2595                     }
2596                 }
2597             }
2598         }
2599     }
2600
2601     /* open each encoder */
2602     for (i = 0; i < nb_output_streams; i++) {
2603         ost = output_streams[i];
2604         if (ost->encoding_needed) {
2605             AVCodec      *codec = ost->enc;
2606             AVCodecContext *dec = NULL;
2607
2608             if ((ist = get_input_stream(ost)))
2609                 dec = ist->st->codec;
2610             if (dec && dec->subtitle_header) {
2611                 ost->st->codec->subtitle_header = av_malloc(dec->subtitle_header_size);
2612                 if (!ost->st->codec->subtitle_header) {
2613                     ret = AVERROR(ENOMEM);
2614                     goto dump_format;
2615                 }
2616                 memcpy(ost->st->codec->subtitle_header, dec->subtitle_header, dec->subtitle_header_size);
2617                 ost->st->codec->subtitle_header_size = dec->subtitle_header_size;
2618             }
2619             if (!av_dict_get(ost->opts, "threads", NULL, 0))
2620                 av_dict_set(&ost->opts, "threads", "auto", 0);
2621             if (avcodec_open2(ost->st->codec, codec, &ost->opts) < 0) {
2622                 snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d:%d - maybe incorrect parameters such as bit_rate, rate, width or height",
2623                         ost->file_index, ost->index);
2624                 ret = AVERROR(EINVAL);
2625                 goto dump_format;
2626             }
2627             assert_codec_experimental(ost->st->codec, 1);
2628             assert_avoptions(ost->opts);
2629             if (ost->st->codec->bit_rate && ost->st->codec->bit_rate < 1000)
2630                 av_log(NULL, AV_LOG_WARNING, "The bitrate parameter is set too low."
2631                                              "It takes bits/s as argument, not kbits/s\n");
2632             extra_size += ost->st->codec->extradata_size;
2633
2634             if (ost->st->codec->me_threshold)
2635                 input_streams[ost->source_index]->st->codec->debug |= FF_DEBUG_MV;
2636         }
2637     }
2638
2639     /* init input streams */
2640     for (i = 0; i < nb_input_streams; i++)
2641         if ((ret = init_input_stream(i, error, sizeof(error))) < 0)
2642             goto dump_format;
2643
2644     /* discard unused programs */
2645     for (i = 0; i < nb_input_files; i++) {
2646         InputFile *ifile = input_files[i];
2647         for (j = 0; j < ifile->ctx->nb_programs; j++) {
2648             AVProgram *p = ifile->ctx->programs[j];
2649             int discard  = AVDISCARD_ALL;
2650
2651             for (k = 0; k < p->nb_stream_indexes; k++)
2652                 if (!input_streams[ifile->ist_index + p->stream_index[k]]->discard) {
2653                     discard = AVDISCARD_DEFAULT;
2654                     break;
2655                 }
2656             p->discard = discard;
2657         }
2658     }
2659
2660     /* open files and write file headers */
2661     for (i = 0; i < nb_output_files; i++) {
2662         oc = output_files[i]->ctx;
2663         oc->interrupt_callback = int_cb;
2664         if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) {
2665             char errbuf[128];
2666             const char *errbuf_ptr = errbuf;
2667             if (av_strerror(ret, errbuf, sizeof(errbuf)) < 0)
2668                 errbuf_ptr = strerror(AVUNERROR(ret));
2669             snprintf(error, sizeof(error), "Could not write header for output file #%d (incorrect codec parameters ?): %s", i, errbuf_ptr);
2670             ret = AVERROR(EINVAL);
2671             goto dump_format;
2672         }
2673         assert_avoptions(output_files[i]->opts);
2674         if (strcmp(oc->oformat->name, "rtp")) {
2675             want_sdp = 0;
2676         }
2677     }
2678
2679  dump_format:
2680     /* dump the file output parameters - cannot be done before in case
2681        of stream copy */
2682     for (i = 0; i < nb_output_files; i++) {
2683         av_dump_format(output_files[i]->ctx, i, output_files[i]->ctx->filename, 1);
2684     }
2685
2686     /* dump the stream mapping */
2687     av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
2688     for (i = 0; i < nb_input_streams; i++) {
2689         ist = input_streams[i];
2690
2691         for (j = 0; j < ist->nb_filters; j++) {
2692             if (ist->filters[j]->graph->graph_desc) {
2693                 av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d (%s) -> %s",
2694                        ist->file_index, ist->st->index, ist->dec ? ist->dec->name : "?",
2695                        ist->filters[j]->name);
2696                 if (nb_filtergraphs > 1)
2697                     av_log(NULL, AV_LOG_INFO, " (graph %d)", ist->filters[j]->graph->index);
2698                 av_log(NULL, AV_LOG_INFO, "\n");
2699             }
2700         }
2701     }
2702
2703     for (i = 0; i < nb_output_streams; i++) {
2704         ost = output_streams[i];
2705
2706         if (ost->attachment_filename) {
2707             /* an attached file */
2708             av_log(NULL, AV_LOG_INFO, "  File %s -> Stream #%d:%d\n",
2709                    ost->attachment_filename, ost->file_index, ost->index);
2710             continue;
2711         }
2712
2713         if (ost->filter && ost->filter->graph->graph_desc) {
2714             /* output from a complex graph */
2715             av_log(NULL, AV_LOG_INFO, "  %s", ost->filter->name);
2716             if (nb_filtergraphs > 1)
2717                 av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index);
2718
2719             av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index,
2720                    ost->index, ost->enc ? ost->enc->name : "?");
2721             continue;
2722         }
2723
2724         av_log(NULL, AV_LOG_INFO, "  Stream #%d:%d -> #%d:%d",
2725                input_streams[ost->source_index]->file_index,
2726                input_streams[ost->source_index]->st->index,
2727                ost->file_index,
2728                ost->index);
2729         if (ost->sync_ist != input_streams[ost->source_index])
2730             av_log(NULL, AV_LOG_INFO, " [sync #%d:%d]",
2731                    ost->sync_ist->file_index,
2732                    ost->sync_ist->st->index);
2733         if (ost->stream_copy)
2734             av_log(NULL, AV_LOG_INFO, " (copy)");
2735         else
2736             av_log(NULL, AV_LOG_INFO, " (%s -> %s)", input_streams[ost->source_index]->dec ?
2737                    input_streams[ost->source_index]->dec->name : "?",
2738                    ost->enc ? ost->enc->name : "?");
2739         av_log(NULL, AV_LOG_INFO, "\n");
2740     }
2741
2742     if (ret) {
2743         av_log(NULL, AV_LOG_ERROR, "%s\n", error);
2744         return ret;
2745     }
2746
2747     if (want_sdp) {
2748         print_sdp();
2749     }
2750
2751     return 0;
2752 }
2753
2754 /**
2755  * @return 1 if there are still streams where more output is wanted,
2756  *         0 otherwise
2757  */
2758 static int need_output(void)
2759 {
2760     int i;
2761
2762     for (i = 0; i < nb_output_streams; i++) {
2763         OutputStream *ost    = output_streams[i];
2764         OutputFile *of       = output_files[ost->file_index];
2765         AVFormatContext *os  = output_files[ost->file_index]->ctx;
2766
2767         if (ost->is_past_recording_time ||
2768             (os->pb && avio_tell(os->pb) >= of->limit_filesize))
2769             continue;
2770         if (ost->frame_number >= ost->max_frames) {
2771             int j;
2772             for (j = 0; j < of->ctx->nb_streams; j++)
2773                 output_streams[of->ost_index + j]->is_past_recording_time = 1;
2774             continue;
2775         }
2776
2777         return 1;
2778     }
2779
2780     return 0;
2781 }
2782
2783 static int select_input_file(uint8_t *no_packet)
2784 {
2785     int64_t ipts_min = INT64_MAX;
2786     int i, file_index = -1;
2787
2788     for (i = 0; i < nb_input_streams; i++) {
2789         InputStream *ist = input_streams[i];
2790         int64_t ipts     = ist->last_dts;
2791
2792         if (ist->discard || no_packet[ist->file_index])
2793             continue;
2794         if (!input_files[ist->file_index]->eof_reached) {
2795             if (ipts < ipts_min) {
2796                 ipts_min = ipts;
2797                 file_index = ist->file_index;
2798             }
2799         }
2800     }
2801
2802     return file_index;
2803 }
2804
2805 #if HAVE_PTHREADS
2806 static void *input_thread(void *arg)
2807 {
2808     InputFile *f = arg;
2809     int ret = 0;
2810
2811     while (!transcoding_finished && ret >= 0) {
2812         AVPacket pkt;
2813         ret = av_read_frame(f->ctx, &pkt);
2814
2815         if (ret == AVERROR(EAGAIN)) {
2816             av_usleep(10000);
2817             ret = 0;
2818             continue;
2819         } else if (ret < 0)
2820             break;
2821
2822         pthread_mutex_lock(&f->fifo_lock);
2823         while (!av_fifo_space(f->fifo))
2824             pthread_cond_wait(&f->fifo_cond, &f->fifo_lock);
2825
2826         av_dup_packet(&pkt);
2827         av_fifo_generic_write(f->fifo, &pkt, sizeof(pkt), NULL);
2828
2829         pthread_mutex_unlock(&f->fifo_lock);
2830     }
2831
2832     f->finished = 1;
2833     return NULL;
2834 }
2835
2836 static void free_input_threads(void)
2837 {
2838     int i;
2839
2840     if (nb_input_files == 1)
2841         return;
2842
2843     transcoding_finished = 1;
2844
2845     for (i = 0; i < nb_input_files; i++) {
2846         InputFile *f = input_files[i];
2847         AVPacket pkt;
2848
2849         if (!f->fifo || f->joined)
2850             continue;
2851
2852         pthread_mutex_lock(&f->fifo_lock);
2853         while (av_fifo_size(f->fifo)) {
2854             av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
2855             av_free_packet(&pkt);
2856         }
2857         pthread_cond_signal(&f->fifo_cond);
2858         pthread_mutex_unlock(&f->fifo_lock);
2859
2860         pthread_join(f->thread, NULL);
2861         f->joined = 1;
2862
2863         while (av_fifo_size(f->fifo)) {
2864             av_fifo_generic_read(f->fifo, &pkt, sizeof(pkt), NULL);
2865             av_free_packet(&pkt);
2866         }
2867         av_fifo_free(f->fifo);
2868     }
2869 }
2870
2871 static int init_input_threads(void)
2872 {
2873     int i, ret;
2874
2875     if (nb_input_files == 1)
2876         return 0;
2877
2878     for (i = 0; i < nb_input_files; i++) {
2879         InputFile *f = input_files[i];
2880
2881         if (!(f->fifo = av_fifo_alloc(8*sizeof(AVPacket))))
2882             return AVERROR(ENOMEM);
2883
2884         pthread_mutex_init(&f->fifo_lock, NULL);
2885         pthread_cond_init (&f->fifo_cond, NULL);
2886
2887         if ((ret = pthread_create(&f->thread, NULL, input_thread, f)))
2888             return AVERROR(ret);
2889     }
2890     return 0;
2891 }
2892
2893 static int get_input_packet_mt(InputFile *f, AVPacket *pkt)
2894 {
2895     int ret = 0;
2896
2897     pthread_mutex_lock(&f->fifo_lock);
2898
2899     if (av_fifo_size(f->fifo)) {
2900         av_fifo_generic_read(f->fifo, pkt, sizeof(*pkt), NULL);
2901         pthread_cond_signal(&f->fifo_cond);
2902     } else {
2903         if (f->finished)
2904             ret = AVERROR_EOF;
2905         else
2906             ret = AVERROR(EAGAIN);
2907     }
2908
2909     pthread_mutex_unlock(&f->fifo_lock);
2910
2911     return ret;
2912 }
2913 #endif
2914
2915 static int get_input_packet(InputFile *f, AVPacket *pkt)
2916 {
2917 #if HAVE_PTHREADS
2918     if (nb_input_files > 1)
2919         return get_input_packet_mt(f, pkt);
2920 #endif
2921     return av_read_frame(f->ctx, pkt);
2922 }
2923
2924 /*
2925  * The following code is the main loop of the file converter
2926  */
2927 static int transcode(void)
2928 {
2929     int ret, i;
2930     AVFormatContext *is, *os;
2931     OutputStream *ost;
2932     InputStream *ist;
2933     uint8_t *no_packet;
2934     int no_packet_count = 0;
2935     int64_t timer_start;
2936
2937     if (!(no_packet = av_mallocz(nb_input_files)))
2938         exit_program(1);
2939
2940     ret = transcode_init();
2941     if (ret < 0)
2942         goto fail;
2943
2944     av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
2945     term_init();
2946
2947     timer_start = av_gettime();
2948
2949 #if HAVE_PTHREADS
2950     if ((ret = init_input_threads()) < 0)
2951         goto fail;
2952 #endif
2953
2954     for (; received_sigterm == 0;) {
2955         int file_index, ist_index;
2956         AVPacket pkt;
2957
2958         /* check if there's any stream where output is still needed */
2959         if (!need_output()) {
2960             av_log(NULL, AV_LOG_VERBOSE, "No more output streams to write to, finishing.\n");
2961             break;
2962         }
2963
2964         /* select the stream that we must read now */
2965         file_index = select_input_file(no_packet);
2966         /* if none, if is finished */
2967         if (file_index < 0) {
2968             if (no_packet_count) {
2969                 no_packet_count = 0;
2970                 memset(no_packet, 0, nb_input_files);
2971                 av_usleep(10000);
2972                 continue;
2973             }
2974             av_log(NULL, AV_LOG_VERBOSE, "No more inputs to read from, finishing.\n");
2975             break;
2976         }
2977
2978         is  = input_files[file_index]->ctx;
2979         ret = get_input_packet(input_files[file_index], &pkt);
2980
2981         if (ret == AVERROR(EAGAIN)) {
2982             no_packet[file_index] = 1;
2983             no_packet_count++;
2984             continue;
2985         }
2986         if (ret < 0) {
2987             input_files[file_index]->eof_reached = 1;
2988
2989             for (i = 0; i < input_files[file_index]->nb_streams; i++) {
2990                 ist = input_streams[input_files[file_index]->ist_index + i];
2991                 if (ist->decoding_needed)
2992                     output_packet(ist, NULL);
2993             }
2994
2995             if (opt_shortest)
2996                 break;
2997             else
2998                 continue;
2999         }
3000
3001         no_packet_count = 0;
3002         memset(no_packet, 0, nb_input_files);
3003
3004         if (do_pkt_dump) {
3005             av_pkt_dump_log2(NULL, AV_LOG_DEBUG, &pkt, do_hex_dump,
3006                              is->streams[pkt.stream_index]);
3007         }
3008         /* the following test is needed in case new streams appear
3009            dynamically in stream : we ignore them */
3010         if (pkt.stream_index >= input_files[file_index]->nb_streams)
3011             goto discard_packet;
3012         ist_index = input_files[file_index]->ist_index + pkt.stream_index;
3013         ist = input_streams[ist_index];
3014         if (ist->discard)
3015             goto discard_packet;
3016
3017         if (pkt.dts != AV_NOPTS_VALUE)
3018             pkt.dts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3019         if (pkt.pts != AV_NOPTS_VALUE)
3020             pkt.pts += av_rescale_q(input_files[ist->file_index]->ts_offset, AV_TIME_BASE_Q, ist->st->time_base);
3021
3022         if (pkt.pts != AV_NOPTS_VALUE)
3023             pkt.pts *= ist->ts_scale;
3024         if (pkt.dts != AV_NOPTS_VALUE)
3025             pkt.dts *= ist->ts_scale;
3026
3027         //fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n",
3028         //        ist->next_dts,
3029         //        pkt.dts, input_files[ist->file_index].ts_offset,
3030         //        ist->st->codec->codec_type);
3031         if (pkt.dts != AV_NOPTS_VALUE && ist->next_dts != AV_NOPTS_VALUE
3032             && (is->iformat->flags & AVFMT_TS_DISCONT)) {
3033             int64_t pkt_dts = av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q);
3034             int64_t delta   = pkt_dts - ist->next_dts;
3035             if ((FFABS(delta) > 1LL * dts_delta_threshold * AV_TIME_BASE || pkt_dts + 1 < ist->last_dts) && !copy_ts) {
3036                 input_files[ist->file_index]->ts_offset -= delta;
3037                 av_log(NULL, AV_LOG_DEBUG,
3038                        "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n",
3039                        delta, input_files[ist->file_index]->ts_offset);
3040                 pkt.dts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3041                 if (pkt.pts != AV_NOPTS_VALUE)
3042                     pkt.pts-= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
3043             }
3044         }
3045
3046         // fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->st->index, pkt.size);
3047         if (output_packet(ist, &pkt) < 0 || poll_filters() < 0) {
3048             av_log(NULL, AV_LOG_ERROR, "Error while decoding stream #%d:%d\n",
3049                    ist->file_index, ist->st->index);
3050             if (exit_on_error)
3051                 exit_program(1);
3052             av_free_packet(&pkt);
3053             continue;
3054         }
3055
3056     discard_packet:
3057         av_free_packet(&pkt);
3058
3059         /* dump report by using the output first video and audio streams */
3060         print_report(0, timer_start);
3061     }
3062 #if HAVE_PTHREADS
3063     free_input_threads();
3064 #endif
3065
3066     /* at the end of stream, we must flush the decoder buffers */
3067     for (i = 0; i < nb_input_streams; i++) {
3068         ist = input_streams[i];
3069         if (!input_files[ist->file_index]->eof_reached && ist->decoding_needed) {
3070             output_packet(ist, NULL);
3071         }
3072     }
3073     poll_filters();
3074     flush_encoders();
3075
3076     term_exit();
3077
3078     /* write the trailer if needed and close file */
3079     for (i = 0; i < nb_output_files; i++) {
3080         os = output_files[i]->ctx;
3081         av_write_trailer(os);
3082     }
3083
3084     /* dump report by using the first video and audio streams */
3085     print_report(1, timer_start);
3086
3087     /* close each encoder */
3088     for (i = 0; i < nb_output_streams; i++) {
3089         ost = output_streams[i];
3090         if (ost->encoding_needed) {
3091             av_freep(&ost->st->codec->stats_in);
3092             avcodec_close(ost->st->codec);
3093         }
3094     }
3095
3096     /* close each decoder */
3097     for (i = 0; i < nb_input_streams; i++) {
3098         ist = input_streams[i];
3099         if (ist->decoding_needed) {
3100             avcodec_close(ist->st->codec);
3101         }
3102     }
3103
3104     /* finished ! */
3105     ret = 0;
3106
3107  fail:
3108     av_freep(&no_packet);
3109 #if HAVE_PTHREADS
3110     free_input_threads();
3111 #endif
3112
3113     if (output_streams) {
3114         for (i = 0; i < nb_output_streams; i++) {
3115             ost = output_streams[i];
3116             if (ost) {
3117                 if (ost->stream_copy)
3118                     av_freep(&ost->st->codec->extradata);
3119                 if (ost->logfile) {
3120                     fclose(ost->logfile);
3121                     ost->logfile = NULL;
3122                 }
3123                 av_freep(&ost->st->codec->subtitle_header);
3124                 av_free(ost->forced_kf_pts);
3125                 av_dict_free(&ost->opts);
3126             }
3127         }
3128     }
3129     return ret;
3130 }
3131
3132 static double parse_frame_aspect_ratio(const char *arg)
3133 {
3134     int x = 0, y = 0;
3135     double ar = 0;
3136     const char *p;
3137     char *end;
3138
3139     p = strchr(arg, ':');
3140     if (p) {
3141         x = strtol(arg, &end, 10);
3142         if (end == p)
3143             y = strtol(end + 1, &end, 10);
3144         if (x > 0 && y > 0)
3145             ar = (double)x / (double)y;
3146     } else
3147         ar = strtod(arg, NULL);
3148
3149     if (!ar) {
3150         av_log(NULL, AV_LOG_FATAL, "Incorrect aspect ratio specification.\n");
3151         exit_program(1);
3152     }
3153     return ar;
3154 }
3155
3156 static int opt_audio_codec(OptionsContext *o, const char *opt, const char *arg)
3157 {
3158     return parse_option(o, "codec:a", arg, options);
3159 }
3160
3161 static int opt_video_codec(OptionsContext *o, const char *opt, const char *arg)
3162 {
3163     return parse_option(o, "codec:v", arg, options);
3164 }
3165
3166 static int opt_subtitle_codec(OptionsContext *o, const char *opt, const char *arg)
3167 {
3168     return parse_option(o, "codec:s", arg, options);
3169 }
3170
3171 static int opt_data_codec(OptionsContext *o, const char *opt, const char *arg)
3172 {
3173     return parse_option(o, "codec:d", arg, options);
3174 }
3175
3176 static int opt_map(OptionsContext *o, const char *opt, const char *arg)
3177 {
3178     StreamMap *m = NULL;
3179     int i, negative = 0, file_idx;
3180     int sync_file_idx = -1, sync_stream_idx;
3181     char *p, *sync;
3182     char *map;
3183
3184     if (*arg == '-') {
3185         negative = 1;
3186         arg++;
3187     }
3188     map = av_strdup(arg);
3189
3190     /* parse sync stream first, just pick first matching stream */
3191     if (sync = strchr(map, ',')) {
3192         *sync = 0;
3193         sync_file_idx = strtol(sync + 1, &sync, 0);
3194         if (sync_file_idx >= nb_input_files || sync_file_idx < 0) {
3195             av_log(NULL, AV_LOG_FATAL, "Invalid sync file index: %d.\n", sync_file_idx);
3196             exit_program(1);
3197         }
3198         if (*sync)
3199             sync++;
3200         for (i = 0; i < input_files[sync_file_idx]->nb_streams; i++)
3201             if (check_stream_specifier(input_files[sync_file_idx]->ctx,
3202                                        input_files[sync_file_idx]->ctx->streams[i], sync) == 1) {
3203                 sync_stream_idx = i;
3204                 break;
3205             }
3206         if (i == input_files[sync_file_idx]->nb_streams) {
3207             av_log(NULL, AV_LOG_FATAL, "Sync stream specification in map %s does not "
3208                                        "match any streams.\n", arg);
3209             exit_program(1);
3210         }
3211     }
3212
3213
3214     if (map[0] == '[') {
3215         /* this mapping refers to lavfi output */
3216         const char *c = map + 1;
3217         o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
3218                                     &o->nb_stream_maps, o->nb_stream_maps + 1);
3219         m = &o->stream_maps[o->nb_stream_maps - 1];
3220         m->linklabel = av_get_token(&c, "]");
3221         if (!m->linklabel) {
3222             av_log(NULL, AV_LOG_ERROR, "Invalid output link label: %s.\n", map);
3223             exit_program(1);
3224         }
3225     } else {
3226         file_idx = strtol(map, &p, 0);
3227         if (file_idx >= nb_input_files || file_idx < 0) {
3228             av_log(NULL, AV_LOG_FATAL, "Invalid input file index: %d.\n", file_idx);
3229             exit_program(1);
3230         }
3231         if (negative)
3232             /* disable some already defined maps */
3233             for (i = 0; i < o->nb_stream_maps; i++) {
3234                 m = &o->stream_maps[i];
3235                 if (file_idx == m->file_index &&
3236                     check_stream_specifier(input_files[m->file_index]->ctx,
3237                                            input_files[m->file_index]->ctx->streams[m->stream_index],
3238                                            *p == ':' ? p + 1 : p) > 0)
3239                     m->disabled = 1;
3240             }
3241         else
3242             for (i = 0; i < input_files[file_idx]->nb_streams; i++) {
3243                 if (check_stream_specifier(input_files[file_idx]->ctx, input_files[file_idx]->ctx->streams[i],
3244                             *p == ':' ? p + 1 : p) <= 0)
3245                     continue;
3246                 o->stream_maps = grow_array(o->stream_maps, sizeof(*o->stream_maps),
3247                                             &o->nb_stream_maps, o->nb_stream_maps + 1);
3248                 m = &o->stream_maps[o->nb_stream_maps - 1];
3249
3250                 m->file_index   = file_idx;
3251                 m->stream_index = i;
3252
3253                 if (sync_file_idx >= 0) {
3254                     m->sync_file_index   = sync_file_idx;
3255                     m->sync_stream_index = sync_stream_idx;
3256                 } else {
3257                     m->sync_file_index   = file_idx;
3258                     m->sync_stream_index = i;
3259                 }
3260             }
3261     }
3262
3263     if (!m) {
3264         av_log(NULL, AV_LOG_FATAL, "Stream map '%s' matches no streams.\n", arg);
3265         exit_program(1);
3266     }
3267
3268     av_freep(&map);
3269     return 0;
3270 }
3271
3272 static int opt_attach(OptionsContext *o, const char *opt, const char *arg)
3273 {
3274     o->attachments = grow_array(o->attachments, sizeof(*o->attachments),
3275                                 &o->nb_attachments, o->nb_attachments + 1);
3276     o->attachments[o->nb_attachments - 1] = arg;
3277     return 0;
3278 }
3279
3280 /**
3281  * Parse a metadata specifier in arg.
3282  * @param type metadata type is written here -- g(lobal)/s(tream)/c(hapter)/p(rogram)
3283  * @param index for type c/p, chapter/program index is written here
3284  * @param stream_spec for type s, the stream specifier is written here
3285  */
3286 static void parse_meta_type(char *arg, char *type, int *index, const char **stream_spec)
3287 {
3288     if (*arg) {
3289         *type = *arg;
3290         switch (*arg) {
3291         case 'g':
3292             break;
3293         case 's':
3294             if (*(++arg) && *arg != ':') {
3295                 av_log(NULL, AV_LOG_FATAL, "Invalid metadata specifier %s.\n", arg);
3296                 exit_program(1);
3297             }
3298             *stream_spec = *arg == ':' ? arg + 1 : "";
3299             break;
3300         case 'c':
3301         case 'p':
3302             if (*(++arg) == ':')
3303                 *index = strtol(++arg, NULL, 0);
3304             break;
3305         default:
3306             av_log(NULL, AV_LOG_FATAL, "Invalid metadata type %c.\n", *arg);
3307             exit_program(1);
3308         }
3309     } else
3310         *type = 'g';
3311 }
3312
3313 static int copy_metadata(char *outspec, char *inspec, AVFormatContext *oc, AVFormatContext *ic, OptionsContext *o)
3314 {
3315     AVDictionary **meta_in = NULL;
3316     AVDictionary **meta_out;
3317     int i, ret = 0;
3318     char type_in, type_out;
3319     const char *istream_spec = NULL, *ostream_spec = NULL;
3320     int idx_in = 0, idx_out = 0;
3321
3322     parse_meta_type(inspec,  &type_in,  &idx_in,  &istream_spec);
3323     parse_meta_type(outspec, &type_out, &idx_out, &ostream_spec);
3324
3325     if (type_in == 'g' || type_out == 'g')
3326         o->metadata_global_manual = 1;
3327     if (type_in == 's' || type_out == 's')
3328         o->metadata_streams_manual = 1;
3329     if (type_in == 'c' || type_out == 'c')
3330         o->metadata_chapters_manual = 1;
3331
3332 #define METADATA_CHECK_INDEX(index, nb_elems, desc)\
3333     if ((index) < 0 || (index) >= (nb_elems)) {\
3334         av_log(NULL, AV_LOG_FATAL, "Invalid %s index %d while processing metadata maps.\n",\
3335                 (desc), (index));\
3336         exit_program(1);\
3337     }
3338
3339 #define SET_DICT(type, meta, context, index)\
3340         switch (type) {\
3341         case 'g':\
3342             meta = &context->metadata;\
3343             break;\
3344         case 'c':\
3345             METADATA_CHECK_INDEX(index, context->nb_chapters, "chapter")\
3346             meta = &context->chapters[index]->metadata;\
3347             break;\
3348         case 'p':\
3349             METADATA_CHECK_INDEX(index, context->nb_programs, "program")\
3350             meta = &context->programs[index]->metadata;\
3351             break;\
3352         }\
3353
3354     SET_DICT(type_in, meta_in, ic, idx_in);
3355     SET_DICT(type_out, meta_out, oc, idx_out);
3356
3357     /* for input streams choose first matching stream */
3358     if (type_in == 's') {
3359         for (i = 0; i < ic->nb_streams; i++) {
3360             if ((ret = check_stream_specifier(ic, ic->streams[i], istream_spec)) > 0) {
3361                 meta_in = &ic->streams[i]->metadata;
3362                 break;
3363             } else if (ret < 0)
3364                 exit_program(1);
3365         }
3366         if (!meta_in) {
3367             av_log(NULL, AV_LOG_FATAL, "Stream specifier %s does not match  any streams.\n", istream_spec);
3368             exit_program(1);
3369         }
3370     }
3371
3372     if (type_out == 's') {
3373         for (i = 0; i < oc->nb_streams; i++) {
3374             if ((ret = check_stream_specifier(oc, oc->streams[i], ostream_spec)) > 0) {
3375                 meta_out = &oc->streams[i]->metadata;
3376                 av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
3377             } else if (ret < 0)
3378                 exit_program(1);
3379         }
3380     } else
3381         av_dict_copy(meta_out, *meta_in, AV_DICT_DONT_OVERWRITE);
3382
3383     return 0;
3384 }
3385
3386 static AVCodec *find_codec_or_die(const char *name, enum AVMediaType type, int encoder)
3387 {
3388     const char *codec_string = encoder ? "encoder" : "decoder";
3389     AVCodec *codec;
3390
3391     codec = encoder ?
3392         avcodec_find_encoder_by_name(name) :
3393         avcodec_find_decoder_by_name(name);
3394     if (!codec) {
3395         av_log(NULL, AV_LOG_FATAL, "Unknown %s '%s'\n", codec_string, name);
3396         exit_program(1);
3397     }
3398     if (codec->type != type) {
3399         av_log(NULL, AV_LOG_FATAL, "Invalid %s type '%s'\n", codec_string, name);
3400         exit_program(1);
3401     }
3402     return codec;
3403 }
3404
3405 static AVCodec *choose_decoder(OptionsContext *o, AVFormatContext *s, AVStream *st)
3406 {
3407     char *codec_name = NULL;
3408
3409     MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, st);
3410     if (codec_name) {
3411         AVCodec *codec = find_codec_or_die(codec_name, st->codec->codec_type, 0);
3412         st->codec->codec_id = codec->id;
3413         return codec;
3414     } else
3415         return avcodec_find_decoder(st->codec->codec_id);
3416 }
3417
3418 /**
3419  * Add all the streams from the given input file to the global
3420  * list of input streams.
3421  */
3422 static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
3423 {
3424     int i;
3425
3426     for (i = 0; i < ic->nb_streams; i++) {
3427         AVStream *st = ic->streams[i];
3428         AVCodecContext *dec = st->codec;
3429         InputStream *ist = av_mallocz(sizeof(*ist));
3430         char *framerate = NULL;
3431
3432         if (!ist)
3433             exit_program(1);
3434
3435         input_streams = grow_array(input_streams, sizeof(*input_streams), &nb_input_streams, nb_input_streams + 1);
3436         input_streams[nb_input_streams - 1] = ist;
3437
3438         ist->st = st;
3439         ist->file_index = nb_input_files;
3440         ist->discard = 1;
3441         st->discard  = AVDISCARD_ALL;
3442         ist->opts = filter_codec_opts(codec_opts, ist->st->codec->codec_id, ic, st);
3443
3444         ist->ts_scale = 1.0;
3445         MATCH_PER_STREAM_OPT(ts_scale, dbl, ist->ts_scale, ic, st);
3446
3447         ist->dec = choose_decoder(o, ic, st);
3448
3449         switch (dec->codec_type) {
3450         case AVMEDIA_TYPE_VIDEO:
3451             ist->resample_height  = dec->height;
3452             ist->resample_width   = dec->width;
3453             ist->resample_pix_fmt = dec->pix_fmt;
3454
3455             MATCH_PER_STREAM_OPT(frame_rates, str, framerate, ic, st);
3456             if (framerate && av_parse_video_rate(&ist->framerate,
3457                                                  framerate) < 0) {
3458                 av_log(NULL, AV_LOG_ERROR, "Error parsing framerate %s.\n",
3459                        framerate);
3460                 exit_program(1);
3461             }
3462
3463             break;
3464         case AVMEDIA_TYPE_AUDIO:
3465             guess_input_channel_layout(ist);
3466
3467             ist->resample_sample_fmt     = dec->sample_fmt;
3468             ist->resample_sample_rate    = dec->sample_rate;
3469             ist->resample_channels       = dec->channels;
3470             ist->resample_channel_layout = dec->channel_layout;
3471
3472             break;
3473         case AVMEDIA_TYPE_DATA:
3474         case AVMEDIA_TYPE_SUBTITLE:
3475         case AVMEDIA_TYPE_ATTACHMENT:
3476         case AVMEDIA_TYPE_UNKNOWN:
3477             break;
3478         default:
3479             abort();
3480         }
3481     }
3482 }
3483
3484 static void assert_file_overwrite(const char *filename)
3485 {
3486     if (!file_overwrite &&
3487         (strchr(filename, ':') == NULL || filename[1] == ':' ||
3488          av_strstart(filename, "file:", NULL))) {
3489         if (avio_check(filename, 0) == 0) {
3490             if (!using_stdin) {
3491                 fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
3492                 fflush(stderr);
3493                 if (!read_yesno()) {
3494                     fprintf(stderr, "Not overwriting - exiting\n");
3495                     exit_program(1);
3496                 }
3497             }
3498             else {
3499                 fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
3500                 exit_program(1);
3501             }
3502         }
3503     }
3504 }
3505
3506 static void dump_attachment(AVStream *st, const char *filename)
3507 {
3508     int ret;
3509     AVIOContext *out = NULL;
3510     AVDictionaryEntry *e;
3511
3512     if (!st->codec->extradata_size) {
3513         av_log(NULL, AV_LOG_WARNING, "No extradata to dump in stream #%d:%d.\n",
3514                nb_input_files - 1, st->index);
3515         return;
3516     }
3517     if (!*filename && (e = av_dict_get(st->metadata, "filename", NULL, 0)))
3518         filename = e->value;
3519     if (!*filename) {
3520         av_log(NULL, AV_LOG_FATAL, "No filename specified and no 'filename' tag"
3521                "in stream #%d:%d.\n", nb_input_files - 1, st->index);
3522         exit_program(1);
3523     }
3524
3525     assert_file_overwrite(filename);
3526
3527     if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &int_cb, NULL)) < 0) {
3528         av_log(NULL, AV_LOG_FATAL, "Could not open file %s for writing.\n",
3529                filename);
3530         exit_program(1);
3531     }
3532
3533     avio_write(out, st->codec->extradata, st->codec->extradata_size);
3534     avio_flush(out);
3535     avio_close(out);
3536 }
3537
3538 static int opt_input_file(OptionsContext *o, const char *opt, const char *filename)
3539 {
3540     AVFormatContext *ic;
3541     AVInputFormat *file_iformat = NULL;
3542     int err, i, ret;
3543     int64_t timestamp;
3544     uint8_t buf[128];
3545     AVDictionary **opts;
3546     int orig_nb_streams;                     // number of streams before avformat_find_stream_info
3547
3548     if (o->format) {
3549         if (!(file_iformat = av_find_input_format(o->format))) {
3550             av_log(NULL, AV_LOG_FATAL, "Unknown input format: '%s'\n", o->format);
3551             exit_program(1);
3552         }
3553     }
3554
3555     if (!strcmp(filename, "-"))
3556         filename = "pipe:";
3557
3558     using_stdin |= !strncmp(filename, "pipe:", 5) ||
3559                     !strcmp(filename, "/dev/stdin");
3560
3561     /* get default parameters from command line */
3562     ic = avformat_alloc_context();
3563     if (!ic) {
3564         print_error(filename, AVERROR(ENOMEM));
3565         exit_program(1);
3566     }
3567     if (o->nb_audio_sample_rate) {
3568         snprintf(buf, sizeof(buf), "%d", o->audio_sample_rate[o->nb_audio_sample_rate - 1].u.i);
3569         av_dict_set(&format_opts, "sample_rate", buf, 0);
3570     }
3571     if (o->nb_audio_channels) {
3572         /* because we set audio_channels based on both the "ac" and
3573          * "channel_layout" options, we need to check that the specified
3574          * demuxer actually has the "channels" option before setting it */
3575         if (file_iformat && file_iformat->priv_class &&
3576             av_opt_find(&file_iformat->priv_class, "channels", NULL, 0,
3577                         AV_OPT_SEARCH_FAKE_OBJ)) {
3578             snprintf(buf, sizeof(buf), "%d",
3579                      o->audio_channels[o->nb_audio_channels - 1].u.i);
3580             av_dict_set(&format_opts, "channels", buf, 0);
3581         }
3582     }
3583     if (o->nb_frame_rates) {
3584         /* set the format-level framerate option;
3585          * this is important for video grabbers, e.g. x11 */
3586         if (file_iformat && file_iformat->priv_class &&
3587             av_opt_find(&file_iformat->priv_class, "framerate", NULL, 0,
3588                         AV_OPT_SEARCH_FAKE_OBJ)) {
3589             av_dict_set(&format_opts, "framerate",
3590                         o->frame_rates[o->nb_frame_rates - 1].u.str, 0);
3591         }
3592     }
3593     if (o->nb_frame_sizes) {
3594         av_dict_set(&format_opts, "video_size", o->frame_sizes[o->nb_frame_sizes - 1].u.str, 0);
3595     }
3596     if (o->nb_frame_pix_fmts)
3597         av_dict_set(&format_opts, "pixel_format", o->frame_pix_fmts[o->nb_frame_pix_fmts - 1].u.str, 0);
3598
3599     ic->flags |= AVFMT_FLAG_NONBLOCK;
3600     ic->interrupt_callback = int_cb;
3601
3602     /* open the input file with generic libav function */
3603     err = avformat_open_input(&ic, filename, file_iformat, &format_opts);
3604     if (err < 0) {
3605         print_error(filename, err);
3606         exit_program(1);
3607     }
3608     assert_avoptions(format_opts);
3609
3610     /* apply forced codec ids */
3611     for (i = 0; i < ic->nb_streams; i++)
3612         choose_decoder(o, ic, ic->streams[i]);
3613
3614     /* Set AVCodecContext options for avformat_find_stream_info */
3615     opts = setup_find_stream_info_opts(ic, codec_opts);
3616     orig_nb_streams = ic->nb_streams;
3617
3618     /* If not enough info to get the stream parameters, we decode the
3619        first frames to get it. (used in mpeg case for example) */
3620     ret = avformat_find_stream_info(ic, opts);
3621     if (ret < 0) {
3622         av_log(NULL, AV_LOG_FATAL, "%s: could not find codec parameters\n", filename);
3623         avformat_close_input(&ic);
3624         exit_program(1);
3625     }
3626
3627     timestamp = o->start_time;
3628     /* add the stream start time */
3629     if (ic->start_time != AV_NOPTS_VALUE)
3630         timestamp += ic->start_time;
3631
3632     /* if seeking requested, we execute it */
3633     if (o->start_time != 0) {
3634         ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
3635         if (ret < 0) {
3636             av_log(NULL, AV_LOG_WARNING, "%s: could not seek to position %0.3f\n",
3637                    filename, (double)timestamp / AV_TIME_BASE);
3638         }
3639     }
3640
3641     /* update the current parameters so that they match the one of the input stream */
3642     add_input_streams(o, ic);
3643
3644     /* dump the file content */
3645     av_dump_format(ic, nb_input_files, filename, 0);
3646
3647     input_files = grow_array(input_files, sizeof(*input_files), &nb_input_files, nb_input_files + 1);
3648     if (!(input_files[nb_input_files - 1] = av_mallocz(sizeof(*input_files[0]))))
3649         exit_program(1);
3650
3651     input_files[nb_input_files - 1]->ctx        = ic;
3652     input_files[nb_input_files - 1]->ist_index  = nb_input_streams - ic->nb_streams;
3653     input_files[nb_input_files - 1]->ts_offset  = o->input_ts_offset - (copy_ts ? 0 : timestamp);
3654     input_files[nb_input_files - 1]->nb_streams = ic->nb_streams;
3655     input_files[nb_input_files - 1]->rate_emu   = o->rate_emu;
3656
3657     for (i = 0; i < o->nb_dump_attachment; i++) {
3658         int j;
3659
3660         for (j = 0; j < ic->nb_streams; j++) {
3661             AVStream *st = ic->streams[j];
3662
3663             if (check_stream_specifier(ic, st, o->dump_attachment[i].specifier) == 1)
3664                 dump_attachment(st, o->dump_attachment[i].u.str);
3665         }
3666     }
3667
3668     for (i = 0; i < orig_nb_streams; i++)
3669         av_dict_free(&opts[i]);
3670     av_freep(&opts);
3671
3672     reset_options(o);
3673     return 0;
3674 }
3675
3676 static uint8_t *get_line(AVIOContext *s)
3677 {
3678     AVIOContext *line;
3679     uint8_t *buf;
3680     char c;
3681
3682     if (avio_open_dyn_buf(&line) < 0) {
3683         av_log(NULL, AV_LOG_FATAL, "Could not alloc buffer for reading preset.\n");
3684         exit_program(1);
3685     }
3686
3687     while ((c = avio_r8(s)) && c != '\n')
3688         avio_w8(line, c);
3689     avio_w8(line, 0);
3690     avio_close_dyn_buf(line, &buf);
3691
3692     return buf;
3693 }
3694
3695 static int get_preset_file_2(const char *preset_name, const char *codec_name, AVIOContext **s)
3696 {
3697     int i, ret = 1;
3698     char filename[1000];
3699     const char *base[3] = { getenv("AVCONV_DATADIR"),
3700                             getenv("HOME"),
3701                             AVCONV_DATADIR,
3702                             };
3703
3704     for (i = 0; i < FF_ARRAY_ELEMS(base) && ret; i++) {
3705         if (!base[i])
3706             continue;
3707         if (codec_name) {
3708             snprintf(filename, sizeof(filename), "%s%s/%s-%s.avpreset", base[i],
3709                      i != 1 ? "" : "/.avconv", codec_name, preset_name);
3710             ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
3711         }
3712         if (ret) {
3713             snprintf(filename, sizeof(filename), "%s%s/%s.avpreset", base[i],
3714                      i != 1 ? "" : "/.avconv", preset_name);
3715             ret = avio_open2(s, filename, AVIO_FLAG_READ, &int_cb, NULL);
3716         }
3717     }
3718     return ret;
3719 }
3720
3721 static void choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost)
3722 {
3723     char *codec_name = NULL;
3724
3725     MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st);
3726     if (!codec_name) {
3727         ost->st->codec->codec_id = av_guess_codec(s->oformat, NULL, s->filename,
3728                                                   NULL, ost->st->codec->codec_type);
3729         ost->enc = avcodec_find_encoder(ost->st->codec->codec_id);
3730     } else if (!strcmp(codec_name, "copy"))
3731         ost->stream_copy = 1;
3732     else {
3733         ost->enc = find_codec_or_die(codec_name, ost->st->codec->codec_type, 1);
3734         ost->st->codec->codec_id = ost->enc->id;
3735     }
3736 }
3737
3738 static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type)
3739 {
3740     OutputStream *ost;
3741     AVStream *st = avformat_new_stream(oc, NULL);
3742     int idx      = oc->nb_streams - 1, ret = 0;
3743     char *bsf = NULL, *next, *codec_tag = NULL;
3744     AVBitStreamFilterContext *bsfc, *bsfc_prev = NULL;
3745     double qscale = -1;
3746     char *buf = NULL, *arg = NULL, *preset = NULL;
3747     AVIOContext *s = NULL;
3748
3749     if (!st) {
3750         av_log(NULL, AV_LOG_FATAL, "Could not alloc stream.\n");
3751         exit_program(1);
3752     }
3753
3754     if (oc->nb_streams - 1 < o->nb_streamid_map)
3755         st->id = o->streamid_map[oc->nb_streams - 1];
3756
3757     output_streams = grow_array(output_streams, sizeof(*output_streams), &nb_output_streams,
3758                                 nb_output_streams + 1);
3759     if (!(ost = av_mallocz(sizeof(*ost))))
3760         exit_program(1);
3761     output_streams[nb_output_streams - 1] = ost;
3762
3763     ost->file_index = nb_output_files;
3764     ost->index      = idx;
3765     ost->st         = st;
3766     st->codec->codec_type = type;
3767     choose_encoder(o, oc, ost);
3768     if (ost->enc) {
3769         ost->opts  = filter_codec_opts(codec_opts, ost->enc->id, oc, st);
3770     }
3771
3772     avcodec_get_context_defaults3(st->codec, ost->enc);
3773     st->codec->codec_type = type; // XXX hack, avcodec_get_context_defaults2() sets type to unknown for stream copy
3774
3775     MATCH_PER_STREAM_OPT(presets, str, preset, oc, st);
3776     if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) {
3777         do  {
3778             buf = get_line(s);
3779             if (!buf[0] || buf[0] == '#') {
3780                 av_free(buf);
3781                 continue;
3782             }
3783             if (!(arg = strchr(buf, '='))) {
3784                 av_log(NULL, AV_LOG_FATAL, "Invalid line found in the preset file.\n");
3785                 exit_program(1);
3786             }
3787             *arg++ = 0;
3788             av_dict_set(&ost->opts, buf, arg, AV_DICT_DONT_OVERWRITE);
3789             av_free(buf);
3790         } while (!s->eof_reached);
3791         avio_close(s);
3792     }
3793     if (ret) {
3794         av_log(NULL, AV_LOG_FATAL,
3795                "Preset %s specified for stream %d:%d, but could not be opened.\n",
3796                preset, ost->file_index, ost->index);
3797         exit_program(1);
3798     }
3799
3800     ost->max_frames = INT64_MAX;
3801     MATCH_PER_STREAM_OPT(max_frames, i64, ost->max_frames, oc, st);
3802
3803     MATCH_PER_STREAM_OPT(bitstream_filters, str, bsf, oc, st);
3804     while (bsf) {
3805         if (next = strchr(bsf, ','))
3806             *next++ = 0;
3807         if (!(bsfc = av_bitstream_filter_init(bsf))) {
3808             av_log(NULL, AV_LOG_FATAL, "Unknown bitstream filter %s\n", bsf);
3809             exit_program(1);
3810         }
3811         if (bsfc_prev)
3812             bsfc_prev->next = bsfc;
3813         else
3814             ost->bitstream_filters = bsfc;
3815
3816         bsfc_prev = bsfc;
3817         bsf       = next;
3818     }
3819
3820     MATCH_PER_STREAM_OPT(codec_tags, str, codec_tag, oc, st);
3821     if (codec_tag) {
3822         uint32_t tag = strtol(codec_tag, &next, 0);
3823         if (*next)
3824             tag = AV_RL32(codec_tag);
3825         st->codec->codec_tag = tag;
3826     }
3827
3828     MATCH_PER_STREAM_OPT(qscale, dbl, qscale, oc, st);
3829     if (qscale >= 0 || same_quant) {
3830         st->codec->flags |= CODEC_FLAG_QSCALE;
3831         st->codec->global_quality = FF_QP2LAMBDA * qscale;
3832     }
3833
3834     if (oc->oformat->flags & AVFMT_GLOBALHEADER)
3835         st->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
3836
3837     av_opt_get_int(sws_opts, "sws_flags", 0, &ost->sws_flags);
3838
3839     ost->pix_fmts[0] = ost->pix_fmts[1] = PIX_FMT_NONE;
3840
3841     return ost;
3842 }
3843
3844 static void parse_matrix_coeffs(uint16_t *dest, const char *str)
3845 {
3846     int i;
3847     const char *p = str;
3848     for (i = 0;; i++) {
3849         dest[i] = atoi(p);
3850         if (i == 63)
3851             break;
3852         p = strchr(p, ',');
3853         if (!p) {
3854             av_log(NULL, AV_LOG_FATAL, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
3855             exit_program(1);
3856         }
3857         p++;
3858     }
3859 }
3860
3861 static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc)
3862 {
3863     AVStream *st;
3864     OutputStream *ost;
3865     AVCodecContext *video_enc;
3866
3867     ost = new_output_stream(o, oc, AVMEDIA_TYPE_VIDEO);
3868     st  = ost->st;
3869     video_enc = st->codec;
3870
3871     if (!ost->stream_copy) {
3872         const char *p = NULL;
3873         char *frame_rate = NULL, *frame_size = NULL;
3874         char *frame_aspect_ratio = NULL, *frame_pix_fmt = NULL;
3875         char *intra_matrix = NULL, *inter_matrix = NULL;
3876         const char *filters = "null";
3877         int i;
3878
3879         MATCH_PER_STREAM_OPT(frame_rates, str, frame_rate, oc, st);
3880         if (frame_rate && av_parse_video_rate(&ost->frame_rate, frame_rate) < 0) {
3881             av_log(NULL, AV_LOG_FATAL, "Invalid framerate value: %s\n", frame_rate);
3882             exit_program(1);
3883         }
3884
3885         MATCH_PER_STREAM_OPT(frame_sizes, str, frame_size, oc, st);
3886         if (frame_size && av_parse_video_size(&video_enc->width, &video_enc->height, frame_size) < 0) {
3887             av_log(NULL, AV_LOG_FATAL, "Invalid frame size: %s.\n", frame_size);
3888             exit_program(1);
3889         }
3890
3891         MATCH_PER_STREAM_OPT(frame_aspect_ratios, str, frame_aspect_ratio, oc, st);
3892         if (frame_aspect_ratio)
3893             ost->frame_aspect_ratio = parse_frame_aspect_ratio(frame_aspect_ratio);
3894
3895         MATCH_PER_STREAM_OPT(frame_pix_fmts, str, frame_pix_fmt, oc, st);
3896         if (frame_pix_fmt && (video_enc->pix_fmt = av_get_pix_fmt(frame_pix_fmt)) == PIX_FMT_NONE) {
3897             av_log(NULL, AV_LOG_FATAL, "Unknown pixel format requested: %s.\n", frame_pix_fmt);
3898             exit_program(1);
3899         }
3900         st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
3901
3902         MATCH_PER_STREAM_OPT(intra_matrices, str, intra_matrix, oc, st);
3903         if (intra_matrix) {
3904             if (!(video_enc->intra_matrix = av_mallocz(sizeof(*video_enc->intra_matrix) * 64))) {
3905                 av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for intra matrix.\n");
3906                 exit_program(1);
3907             }
3908             parse_matrix_coeffs(video_enc->intra_matrix, intra_matrix);
3909         }
3910         MATCH_PER_STREAM_OPT(inter_matrices, str, inter_matrix, oc, st);
3911         if (inter_matrix) {
3912             if (!(video_enc->inter_matrix = av_mallocz(sizeof(*video_enc->inter_matrix) * 64))) {
3913                 av_log(NULL, AV_LOG_FATAL, "Could not allocate memory for inter matrix.\n");
3914                 exit_program(1);
3915             }
3916             parse_matrix_coeffs(video_enc->inter_matrix, inter_matrix);
3917         }
3918
3919         MATCH_PER_STREAM_OPT(rc_overrides, str, p, oc, st);
3920         for (i = 0; p; i++) {
3921             int start, end, q;
3922             int e = sscanf(p, "%d,%d,%d", &start, &end, &q);
3923             if (e != 3) {
3924                 av_log(NULL, AV_LOG_FATAL, "error parsing rc_override\n");
3925                 exit_program(1);
3926             }
3927             video_enc->rc_override =
3928                 av_realloc(video_enc->rc_override,
3929                            sizeof(RcOverride) * (i + 1));
3930             video_enc->rc_override[i].start_frame = start;
3931             video_enc->rc_override[i].end_frame   = end;
3932             if (q > 0) {
3933                 video_enc->rc_override[i].qscale         = q;
3934                 video_enc->rc_override[i].quality_factor = 1.0;
3935             }
3936             else {
3937                 video_enc->rc_override[i].qscale         = 0;
3938                 video_enc->rc_override[i].quality_factor = -q/100.0;
3939             }
3940             p = strchr(p, '/');
3941             if (p) p++;
3942         }
3943         video_enc->rc_override_count = i;
3944         if (!video_enc->rc_initial_buffer_occupancy)
3945             video_enc->rc_initial_buffer_occupancy = video_enc->rc_buffer_size * 3 / 4;
3946         video_enc->intra_dc_precision = intra_dc_precision - 8;
3947
3948         /* two pass mode */
3949         if (do_pass) {
3950             if (do_pass == 1) {
3951                 video_enc->flags |= CODEC_FLAG_PASS1;
3952             } else {
3953                 video_enc->flags |= CODEC_FLAG_PASS2;
3954             }
3955         }
3956
3957         MATCH_PER_STREAM_OPT(forced_key_frames, str, ost->forced_keyframes, oc, st);
3958         if (ost->forced_keyframes)
3959             ost->forced_keyframes = av_strdup(ost->forced_keyframes);
3960
3961         MATCH_PER_STREAM_OPT(force_fps, i, ost->force_fps, oc, st);
3962
3963         ost->top_field_first = -1;
3964         MATCH_PER_STREAM_OPT(top_field_first, i, ost->top_field_first, oc, st);
3965
3966         MATCH_PER_STREAM_OPT(filters, str, filters, oc, st);
3967         ost->avfilter = av_strdup(filters);
3968     } else {
3969         MATCH_PER_STREAM_OPT(copy_initial_nonkeyframes, i, ost->copy_initial_nonkeyframes, oc ,st);
3970     }
3971
3972     return ost;
3973 }
3974
3975 static OutputStream *new_audio_stream(OptionsContext *o, AVFormatContext *oc)
3976 {
3977     AVStream *st;
3978     OutputStream *ost;
3979     AVCodecContext *audio_enc;
3980
3981     ost = new_output_stream(o, oc, AVMEDIA_TYPE_AUDIO);
3982     st  = ost->st;
3983
3984     audio_enc = st->codec;
3985     audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
3986
3987     if (!ost->stream_copy) {
3988         char *sample_fmt = NULL;
3989         const char *filters = "anull";
3990
3991         MATCH_PER_STREAM_OPT(audio_channels, i, audio_enc->channels, oc, st);
3992
3993         MATCH_PER_STREAM_OPT(sample_fmts, str, sample_fmt, oc, st);
3994         if (sample_fmt &&
3995             (audio_enc->sample_fmt = av_get_sample_fmt(sample_fmt)) == AV_SAMPLE_FMT_NONE) {
3996             av_log(NULL, AV_LOG_FATAL, "Invalid sample format '%s'\n", sample_fmt);
3997             exit_program(1);
3998         }
3999
4000         MATCH_PER_STREAM_OPT(audio_sample_rate, i, audio_enc->sample_rate, oc, st);
4001
4002         MATCH_PER_STREAM_OPT(filters, str, filters, oc, st);
4003         ost->avfilter = av_strdup(filters);
4004     }
4005
4006     return ost;
4007 }
4008
4009 static OutputStream *new_data_stream(OptionsContext *o, AVFormatContext *oc)
4010 {
4011     OutputStream *ost;
4012
4013     ost = new_output_stream(o, oc, AVMEDIA_TYPE_DATA);
4014     if (!ost->stream_copy) {
4015         av_log(NULL, AV_LOG_FATAL, "Data stream encoding not supported yet (only streamcopy)\n");
4016         exit_program(1);
4017     }
4018
4019     return ost;
4020 }
4021
4022 static OutputStream *new_attachment_stream(OptionsContext *o, AVFormatContext *oc)
4023 {
4024     OutputStream *ost = new_output_stream(o, oc, AVMEDIA_TYPE_ATTACHMENT);
4025     ost->stream_copy = 1;
4026     return ost;
4027 }
4028
4029 static OutputStream *new_subtitle_stream(OptionsContext *o, AVFormatContext *oc)
4030 {
4031     AVStream *st;
4032     OutputStream *ost;
4033     AVCodecContext *subtitle_enc;
4034
4035     ost = new_output_stream(o, oc, AVMEDIA_TYPE_SUBTITLE);
4036     st  = ost->st;
4037     subtitle_enc = st->codec;
4038
4039     subtitle_enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
4040
4041     return ost;
4042 }
4043
4044 /* arg format is "output-stream-index:streamid-value". */
4045 static int opt_streamid(OptionsContext *o, const char *opt, const char *arg)
4046 {
4047     int idx;
4048     char *p;
4049     char idx_str[16];
4050
4051     av_strlcpy(idx_str, arg, sizeof(idx_str));
4052     p = strchr(idx_str, ':');
4053     if (!p) {
4054         av_log(NULL, AV_LOG_FATAL,
4055                "Invalid value '%s' for option '%s', required syntax is 'index:value'\n",
4056                arg, opt);
4057         exit_program(1);
4058     }
4059     *p++ = '\0';
4060     idx = parse_number_or_die(opt, idx_str, OPT_INT, 0, INT_MAX);
4061     o->streamid_map = grow_array(o->streamid_map, sizeof(*o->streamid_map), &o->nb_streamid_map, idx+1);
4062     o->streamid_map[idx] = parse_number_or_die(opt, p, OPT_INT, 0, INT_MAX);
4063     return 0;
4064 }
4065
4066 static int copy_chapters(InputFile *ifile, OutputFile *ofile, int copy_metadata)
4067 {
4068     AVFormatContext *is = ifile->ctx;
4069     AVFormatContext *os = ofile->ctx;
4070     int i;
4071
4072     for (i = 0; i < is->nb_chapters; i++) {
4073         AVChapter *in_ch = is->chapters[i], *out_ch;
4074         int64_t ts_off   = av_rescale_q(ofile->start_time - ifile->ts_offset,
4075                                        AV_TIME_BASE_Q, in_ch->time_base);
4076         int64_t rt       = (ofile->recording_time == INT64_MAX) ? INT64_MAX :
4077                            av_rescale_q(ofile->recording_time, AV_TIME_BASE_Q, in_ch->time_base);
4078
4079
4080         if (in_ch->end < ts_off)
4081             continue;
4082         if (rt != INT64_MAX && in_ch->start > rt + ts_off)
4083             break;
4084
4085         out_ch = av_mallocz(sizeof(AVChapter));
4086         if (!out_ch)
4087             return AVERROR(ENOMEM);
4088
4089         out_ch->id        = in_ch->id;
4090         out_ch->time_base = in_ch->time_base;
4091         out_ch->start     = FFMAX(0,  in_ch->start - ts_off);
4092         out_ch->end       = FFMIN(rt, in_ch->end   - ts_off);
4093
4094         if (copy_metadata)
4095             av_dict_copy(&out_ch->metadata, in_ch->metadata, 0);
4096
4097         os->nb_chapters++;
4098         os->chapters = av_realloc(os->chapters, sizeof(AVChapter) * os->nb_chapters);
4099         if (!os->chapters)
4100             return AVERROR(ENOMEM);
4101         os->chapters[os->nb_chapters - 1] = out_ch;
4102     }
4103     return 0;
4104 }
4105
4106 static void init_output_filter(OutputFilter *ofilter, OptionsContext *o,
4107                                AVFormatContext *oc)
4108 {
4109     OutputStream *ost;
4110
4111     switch (avfilter_pad_get_type(ofilter->out_tmp->filter_ctx->output_pads,
4112                                   ofilter->out_tmp->pad_idx)) {
4113     case AVMEDIA_TYPE_VIDEO: ost = new_video_stream(o, oc); break;
4114     case AVMEDIA_TYPE_AUDIO: ost = new_audio_stream(o, oc); break;
4115     default:
4116         av_log(NULL, AV_LOG_FATAL, "Only video and audio filters are supported "
4117                "currently.\n");
4118         exit_program(1);
4119     }
4120
4121     ost->source_index = -1;
4122     ost->filter       = ofilter;
4123
4124     ofilter->ost      = ost;
4125
4126     if (ost->stream_copy) {
4127         av_log(NULL, AV_LOG_ERROR, "Streamcopy requested for output stream %d:%d, "
4128                "which is fed from a complex filtergraph. Filtering and streamcopy "
4129                "cannot be used together.\n", ost->file_index, ost->index);
4130         exit_program(1);
4131     }
4132
4133     if (configure_output_filter(ofilter->graph, ofilter, ofilter->out_tmp) < 0) {
4134         av_log(NULL, AV_LOG_FATAL, "Error configuring filter.\n");
4135         exit_program(1);
4136     }
4137     avfilter_inout_free(&ofilter->out_tmp);
4138 }
4139
4140 static void opt_output_file(void *optctx, const char *filename)
4141 {
4142     OptionsContext *o = optctx;
4143     AVFormatContext *oc;
4144     int i, j, err;
4145     AVOutputFormat *file_oformat;
4146     OutputStream *ost;
4147     InputStream  *ist;
4148
4149     if (configure_complex_filters() < 0) {
4150         av_log(NULL, AV_LOG_FATAL, "Error configuring filters.\n");
4151         exit_program(1);
4152     }
4153
4154     if (!strcmp(filename, "-"))
4155         filename = "pipe:";
4156
4157     oc = avformat_alloc_context();
4158     if (!oc) {
4159         print_error(filename, AVERROR(ENOMEM));
4160         exit_program(1);
4161     }
4162
4163     if (o->format) {
4164         file_oformat = av_guess_format(o->format, NULL, NULL);
4165         if (!file_oformat) {
4166             av_log(NULL, AV_LOG_FATAL, "Requested output format '%s' is not a suitable output format\n", o->format);
4167             exit_program(1);
4168         }
4169     } else {
4170         file_oformat = av_guess_format(NULL, filename, NULL);
4171         if (!file_oformat) {
4172             av_log(NULL, AV_LOG_FATAL, "Unable to find a suitable output format for '%s'\n",
4173                    filename);
4174             exit_program(1);
4175         }
4176     }
4177
4178     oc->oformat = file_oformat;
4179     oc->interrupt_callback = int_cb;
4180     av_strlcpy(oc->filename, filename, sizeof(oc->filename));
4181
4182     /* create streams for all unlabeled output pads */
4183     for (i = 0; i < nb_filtergraphs; i++) {
4184         FilterGraph *fg = filtergraphs[i];
4185         for (j = 0; j < fg->nb_outputs; j++) {
4186             OutputFilter *ofilter = fg->outputs[j];
4187
4188             if (!ofilter->out_tmp || ofilter->out_tmp->name)
4189                 continue;
4190
4191             switch (avfilter_pad_get_type(ofilter->out_tmp->filter_ctx->output_pads,
4192                                           ofilter->out_tmp->pad_idx)) {
4193             case AVMEDIA_TYPE_VIDEO:    o->video_disable    = 1; break;
4194             case AVMEDIA_TYPE_AUDIO:    o->audio_disable    = 1; break;
4195             case AVMEDIA_TYPE_SUBTITLE: o->subtitle_disable = 1; break;
4196             }
4197             init_output_filter(ofilter, o, oc);
4198         }
4199     }
4200
4201     if (!o->nb_stream_maps) {
4202         /* pick the "best" stream of each type */
4203 #define NEW_STREAM(type, index)\
4204         if (index >= 0) {\
4205             ost = new_ ## type ## _stream(o, oc);\
4206             ost->source_index = index;\
4207             ost->sync_ist     = input_streams[index];\
4208             input_streams[index]->discard = 0;\
4209             input_streams[index]->st->discard = AVDISCARD_NONE;\
4210         }
4211
4212         /* video: highest resolution */
4213         if (!o->video_disable && oc->oformat->video_codec != CODEC_ID_NONE) {
4214             int area = 0, idx = -1;
4215             for (i = 0; i < nb_input_streams; i++) {
4216                 ist = input_streams[i];
4217                 if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
4218                     ist->st->codec->width * ist->st->codec->height > area) {
4219                     area = ist->st->codec->width * ist->st->codec->height;
4220                     idx = i;
4221                 }
4222             }
4223             NEW_STREAM(video, idx);
4224         }
4225
4226         /* audio: most channels */
4227         if (!o->audio_disable && oc->oformat->audio_codec != CODEC_ID_NONE) {
4228             int channels = 0, idx = -1;
4229             for (i = 0; i < nb_input_streams; i++) {
4230                 ist = input_streams[i];
4231                 if (ist->st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
4232                     ist->st->codec->channels > channels) {
4233                     channels = ist->st->codec->channels;
4234                     idx = i;
4235                 }
4236             }
4237             NEW_STREAM(audio, idx);
4238         }
4239
4240         /* subtitles: pick first */
4241         if (!o->subtitle_disable && oc->oformat->subtitle_codec != CODEC_ID_NONE) {
4242             for (i = 0; i < nb_input_streams; i++)
4243                 if (input_streams[i]->st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
4244                     NEW_STREAM(subtitle, i);
4245                     break;
4246                 }
4247         }
4248         /* do something with data? */
4249     } else {
4250         for (i = 0; i < o->nb_stream_maps; i++) {
4251             StreamMap *map = &o->stream_maps[i];
4252
4253             if (map->disabled)
4254                 continue;
4255
4256             if (map->linklabel) {
4257                 FilterGraph *fg;
4258                 OutputFilter *ofilter = NULL;
4259                 int j, k;
4260
4261                 for (j = 0; j < nb_filtergraphs; j++) {
4262                     fg = filtergraphs[j];
4263                     for (k = 0; k < fg->nb_outputs; k++) {
4264                         AVFilterInOut *out = fg->outputs[k]->out_tmp;
4265                         if (out && !strcmp(out->name, map->linklabel)) {
4266                             ofilter = fg->outputs[k];
4267                             goto loop_end;
4268                         }
4269                     }
4270                 }
4271 loop_end:
4272                 if (!ofilter) {
4273                     av_log(NULL, AV_LOG_FATAL, "Output with label '%s' does not exist "
4274                            "in any defined filter graph.\n", map->linklabel);
4275                     exit_program(1);
4276                 }
4277                 init_output_filter(ofilter, o, oc);
4278             } else {
4279                 ist = input_streams[input_files[map->file_index]->ist_index + map->stream_index];
4280                 switch (ist->st->codec->codec_type) {
4281                 case AVMEDIA_TYPE_VIDEO:    ost = new_video_stream(o, oc);    break;
4282                 case AVMEDIA_TYPE_AUDIO:    ost = new_audio_stream(o, oc);    break;
4283                 case AVMEDIA_TYPE_SUBTITLE: ost = new_subtitle_stream(o, oc); break;
4284                 case AVMEDIA_TYPE_DATA:     ost = new_data_stream(o, oc);     break;
4285                 case AVMEDIA_TYPE_ATTACHMENT: ost = new_attachment_stream(o, oc); break;
4286                 default:
4287                     av_log(NULL, AV_LOG_FATAL, "Cannot map stream #%d:%d - unsupported type.\n",
4288                            map->file_index, map->stream_index);
4289                     exit_program(1);
4290                 }
4291
4292                 ost->source_index = input_files[map->file_index]->ist_index + map->stream_index;
4293                 ost->sync_ist     = input_streams[input_files[map->sync_file_index]->ist_index +
4294                                                map->sync_stream_index];
4295                 ist->discard = 0;
4296                 ist->st->discard = AVDISCARD_NONE;
4297             }
4298         }
4299     }
4300
4301     /* handle attached files */
4302     for (i = 0; i < o->nb_attachments; i++) {
4303         AVIOContext *pb;
4304         uint8_t *attachment;
4305         const char *p;
4306         int64_t len;
4307
4308         if ((err = avio_open2(&pb, o->attachments[i], AVIO_FLAG_READ, &int_cb, NULL)) < 0) {
4309             av_log(NULL, AV_LOG_FATAL, "Could not open attachment file %s.\n",
4310                    o->attachments[i]);
4311             exit_program(1);
4312         }
4313         if ((len = avio_size(pb)) <= 0) {
4314             av_log(NULL, AV_LOG_FATAL, "Could not get size of the attachment %s.\n",
4315                    o->attachments[i]);
4316             exit_program(1);
4317         }
4318         if (!(attachment = av_malloc(len))) {
4319             av_log(NULL, AV_LOG_FATAL, "Attachment %s too large to fit into memory.\n",
4320                    o->attachments[i]);
4321             exit_program(1);
4322         }
4323         avio_read(pb, attachment, len);
4324
4325         ost = new_attachment_stream(o, oc);
4326         ost->stream_copy               = 0;
4327         ost->source_index              = -1;
4328         ost->attachment_filename       = o->attachments[i];
4329         ost->st->codec->extradata      = attachment;
4330         ost->st->codec->extradata_size = len;
4331
4332         p = strrchr(o->attachments[i], '/');
4333         av_dict_set(&ost->st->metadata, "filename", (p && *p) ? p + 1 : o->attachments[i], AV_DICT_DONT_OVERWRITE);
4334         avio_close(pb);
4335     }
4336
4337     output_files = grow_array(output_files, sizeof(*output_files), &nb_output_files, nb_output_files + 1);
4338     if (!(output_files[nb_output_files - 1] = av_mallocz(sizeof(*output_files[0]))))
4339         exit_program(1);
4340
4341     output_files[nb_output_files - 1]->ctx            = oc;
4342     output_files[nb_output_files - 1]->ost_index      = nb_output_streams - oc->nb_streams;
4343     output_files[nb_output_files - 1]->recording_time = o->recording_time;
4344     if (o->recording_time != INT64_MAX)
4345         oc->duration = o->recording_time;
4346     output_files[nb_output_files - 1]->start_time     = o->start_time;
4347     output_files[nb_output_files - 1]->limit_filesize = o->limit_filesize;
4348     av_dict_copy(&output_files[nb_output_files - 1]->opts, format_opts, 0);
4349
4350     /* check filename in case of an image number is expected */
4351     if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
4352         if (!av_filename_number_test(oc->filename)) {
4353             print_error(oc->filename, AVERROR(EINVAL));
4354             exit_program(1);
4355         }
4356     }
4357
4358     if (!(oc->oformat->flags & AVFMT_NOFILE)) {
4359         /* test if it already exists to avoid losing precious files */
4360         assert_file_overwrite(filename);
4361
4362         /* open the file */
4363         if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
4364                               &oc->interrupt_callback,
4365                               &output_files[nb_output_files - 1]->opts)) < 0) {
4366             print_error(filename, err);
4367             exit_program(1);
4368         }
4369     }
4370
4371     if (o->mux_preload) {
4372         uint8_t buf[64];
4373         snprintf(buf, sizeof(buf), "%d", (int)(o->mux_preload*AV_TIME_BASE));
4374         av_dict_set(&output_files[nb_output_files - 1]->opts, "preload", buf, 0);
4375     }
4376     oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
4377     oc->flags |= AVFMT_FLAG_NONBLOCK;
4378
4379     /* copy metadata */
4380     for (i = 0; i < o->nb_metadata_map; i++) {
4381         char *p;
4382         int in_file_index = strtol(o->metadata_map[i].u.str, &p, 0);
4383
4384     &nbs