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