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