Statistics
| Branch: | Revision:

ffmpeg / ffmpeg.c @ bb54f6ab

History | View | Annotate | Download (137 KB)

1 85f07f22 Fabrice Bellard
/*
2 115329f1 Diego Biurrun
 * FFmpeg main
3 01310af2 Fabrice Bellard
 * Copyright (c) 2000-2003 Fabrice Bellard
4 85f07f22 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 bf5af568 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 85f07f22 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 85f07f22 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 bf5af568 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 85f07f22 Fabrice Bellard
 *
17 bf5af568 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 85f07f22 Fabrice Bellard
 */
21 daf8e955 Fabrice Bellard
#define HAVE_AV_CONFIG_H
22 22f7a060 Michael Niedermayer
#include <limits.h>
23 daf8e955 Fabrice Bellard
#include "avformat.h"
24 18a54b04 Luca Abeni
#include "swscale.h"
25 10d104e4 Philip Gladstone
#include "framehook.h"
26 1ac31760 Michael Niedermayer
#include "dsputil.h"
27 6fd46414 Michael Niedermayer
#include "opt.h"
28 f5a478f6 Roman Shaposhnik
#include "fifo.h"
29 daf8e955 Fabrice Bellard
30 2f30a81d Diego Biurrun
#ifndef __MINGW32__
31 85f07f22 Fabrice Bellard
#include <unistd.h>
32
#include <fcntl.h>
33
#include <sys/ioctl.h>
34
#include <sys/time.h>
35
#include <termios.h>
36 5727b222 Fabrice Bellard
#include <sys/resource.h>
37 9680a722 Roumen Petrov
#include <signal.h>
38 bdc4796f Fabrice Bellard
#endif
39 f3ec2d46 Slavik Gnatenko
#ifdef CONFIG_OS2
40
#include <sys/types.h>
41
#include <sys/select.h>
42
#include <stdlib.h>
43
#endif
44 64c020a8 Michael Niedermayer
#undef time //needed because HAVE_AV_CONFIG_H is defined on top
45 bf5af568 Fabrice Bellard
#include <time.h>
46 85f07f22 Fabrice Bellard
47 f1cc88a5 Diego Biurrun
#include "version.h"
48 01310af2 Fabrice Bellard
#include "cmdutils.h"
49
50 2b18dcd0 Michael Niedermayer
#undef NDEBUG
51
#include <assert.h>
52
53 8aa1e3da Michael Niedermayer
#if !defined(INFINITY) && defined(HUGE_VAL)
54
#define INFINITY HUGE_VAL
55
#endif
56 85f07f22 Fabrice Bellard
57
/* select an input stream for an output stream */
58
typedef struct AVStreamMap {
59
    int file_index;
60
    int stream_index;
61 b4a3389e Wolfram Gloger
    int sync_file_index;
62
    int sync_stream_index;
63 85f07f22 Fabrice Bellard
} AVStreamMap;
64
65 0a38bafd Patrice Bensoussan
/** select an input file for an output file */
66
typedef struct AVMetaDataMap {
67
    int out_file;
68
    int in_file;
69
} AVMetaDataMap;
70
71 85f07f22 Fabrice Bellard
extern const OptionDef options[];
72
73 02d504a7 Fabrice Bellard
static void show_help(void);
74
static void show_license(void);
75 637b5326 Michael Niedermayer
static int opt_default(const char *opt, const char *arg);
76 85f07f22 Fabrice Bellard
77
#define MAX_FILES 20
78
79
static AVFormatContext *input_files[MAX_FILES];
80 a6a92a9a Wolfram Gloger
static int64_t input_files_ts_offset[MAX_FILES];
81 85f07f22 Fabrice Bellard
static int nb_input_files = 0;
82
83
static AVFormatContext *output_files[MAX_FILES];
84
static int nb_output_files = 0;
85
86
static AVStreamMap stream_maps[MAX_FILES];
87
static int nb_stream_maps;
88
89 0a38bafd Patrice Bensoussan
static AVMetaDataMap meta_data_maps[MAX_FILES];
90
static int nb_meta_data_maps;
91
92 79fdaa4c Fabrice Bellard
static AVInputFormat *file_iformat;
93
static AVOutputFormat *file_oformat;
94 55cf1959 Michael Niedermayer
static int frame_width  = 0;
95
static int frame_height = 0;
96 880e8ba7 Roman Shaposhnik
static float frame_aspect_ratio = 0;
97 644a9262 Michael Niedermayer
static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
98 1ff93ffc Todd Kirby
static int frame_padtop  = 0;
99
static int frame_padbottom = 0;
100
static int frame_padleft  = 0;
101
static int frame_padright = 0;
102
static int padcolor[3] = {16,128,128}; /* default to black */
103 ab6d194a Michael Niedermayer
static int frame_topBand  = 0;
104
static int frame_bottomBand = 0;
105
static int frame_leftBand  = 0;
106
static int frame_rightBand = 0;
107 cf7fc795 Fabrice Bellard
static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
108 14bea432 Michael Niedermayer
static int frame_rate = 25;
109
static int frame_rate_base = 1;
110 158c7f05 Michael Niedermayer
static float video_qscale = 0;
111 9cdd6a24 Michael Niedermayer
static int video_qdiff = 3;
112 84f608f4 Vidar Madsen
static uint16_t *intra_matrix = NULL;
113
static uint16_t *inter_matrix = NULL;
114 ac2830ec Michael Niedermayer
#if 0 //experimental, (can be removed)
115 3aa102be Michael Niedermayer
static float video_rc_qsquish=1.0;
116
static float video_rc_qmod_amp=0;
117
static int video_rc_qmod_freq=0;
118 ac2830ec Michael Niedermayer
#endif
119 3aa102be Michael Niedermayer
static char *video_rc_override_string=NULL;
120
static char *video_rc_eq="tex^qComp";
121 3bea5386 Michael Niedermayer
static int me_method = ME_EPZS;
122 85f07f22 Fabrice Bellard
static int video_disable = 0;
123 f3356e9c Michael Niedermayer
static int video_discard = 0;
124 85f07f22 Fabrice Bellard
static int video_codec_id = CODEC_ID_NONE;
125 b2a2197e Michael Niedermayer
static int video_codec_tag = 0;
126 85f07f22 Fabrice Bellard
static int same_quality = 0;
127 cfcf0ffd Fabrice Bellard
static int do_deinterlace = 0;
128 1dbb6d90 Michael Niedermayer
static int packet_size = 0;
129 f560dd82 Michael Niedermayer
static int strict = 0;
130 bb198e19 Michael Niedermayer
static int top_field_first = -1;
131 f4f3223f Michael Niedermayer
static int me_threshold = 0;
132 1a11cbcc Michael Niedermayer
static int intra_dc_precision = 8;
133 5894e1bb Víctor Paesa
static int loop_input = 0;
134 8108551a Todd Kirby
static int loop_output = AVFMT_NOOUTPUTLOOP;
135 0888fd22 Michael Niedermayer
static int qp_hist = 0;
136 85f07f22 Fabrice Bellard
137
static int intra_only = 0;
138
static int audio_sample_rate = 44100;
139
static int audio_bit_rate = 64000;
140 c57c770d Justin Ruggles
#define QSCALE_NONE -99999
141
static float audio_qscale = QSCALE_NONE;
142 85f07f22 Fabrice Bellard
static int audio_disable = 0;
143
static int audio_channels = 1;
144
static int audio_codec_id = CODEC_ID_NONE;
145 b2a2197e Michael Niedermayer
static int audio_codec_tag = 0;
146 cf7fc795 Fabrice Bellard
static char *audio_language = NULL;
147
148
static int subtitle_codec_id = CODEC_ID_NONE;
149
static char *subtitle_language = NULL;
150 85f07f22 Fabrice Bellard
151 17c88cb0 Michael Niedermayer
static float mux_preload= 0.5;
152
static float mux_max_delay= 0.7;
153 2db3c638 Michael Niedermayer
154 0c1a9eda Zdenek Kabelac
static int64_t recording_time = 0;
155 8831db5c Michael Niedermayer
static int64_t start_time = 0;
156 4568325a Roman Shaposhnik
static int64_t rec_timestamp = 0;
157 a6a92a9a Wolfram Gloger
static int64_t input_ts_offset = 0;
158 85f07f22 Fabrice Bellard
static int file_overwrite = 0;
159
static char *str_title = NULL;
160
static char *str_author = NULL;
161
static char *str_copyright = NULL;
162
static char *str_comment = NULL;
163 63d5075c Panagiotis Issaris
static char *str_album = NULL;
164 5727b222 Fabrice Bellard
static int do_benchmark = 0;
165 a0663ba4 Fabrice Bellard
static int do_hex_dump = 0;
166 254abc2e Fabrice Bellard
static int do_pkt_dump = 0;
167 43f1708f Juanjo
static int do_psnr = 0;
168 ce7c56c2 Juanjo
static int do_vstats = 0;
169 5abdb4b1 Fabrice Bellard
static int do_pass = 0;
170
static char *pass_logfilename = NULL;
171 1629626f Fabrice Bellard
static int audio_stream_copy = 0;
172
static int video_stream_copy = 0;
173 cf7fc795 Fabrice Bellard
static int subtitle_stream_copy = 0;
174 986ebcdb Michael Niedermayer
static int video_sync_method= 1;
175
static int audio_sync_method= 0;
176 72bd8100 Michael Niedermayer
static int copy_ts= 0;
177 ca80810b Calcium
static int opt_shortest = 0; //
178 90ad92b3 Michael Niedermayer
static int video_global_header = 0;
179 5abdb4b1 Fabrice Bellard
180 bdfcbbed Max Krasnyansky
static int rate_emu = 0;
181
182 6beefa40 Michael Niedermayer
#ifdef CONFIG_BKTR
183 7b49ce2e Stefan Huehner
static const char *video_grab_format = "bktr";
184 6beefa40 Michael Niedermayer
#else
185 0a7b514f Luca Abeni
#ifdef CONFIG_VIDEO4LINUX2
186 7b49ce2e Stefan Huehner
static const char *video_grab_format = "video4linux2";
187 0a7b514f Luca Abeni
#else
188 7b49ce2e Stefan Huehner
static const char *video_grab_format = "video4linux";
189 6beefa40 Michael Niedermayer
#endif
190 0a7b514f Luca Abeni
#endif
191 79a7c268 Fabrice Bellard
static char *video_device = NULL;
192 2639c651 Roman Shaposhnik
static char *grab_device = NULL;
193 a5df11ab Fabrice Bellard
static int  video_channel = 0;
194 e3ee3283 Alex Beregszaszi
static char *video_standard = "ntsc";
195 79a7c268 Fabrice Bellard
196 7b49ce2e Stefan Huehner
static const char *audio_grab_format = "audio_device";
197 79a7c268 Fabrice Bellard
static char *audio_device = NULL;
198 a9aa3467 Michael Niedermayer
static int audio_volume = 256;
199 8aa3ee32 Max Krasnyansky
200 d9a916e2 Charles Yates
static int using_stdin = 0;
201 bee0d9e5 Charles Yates
static int using_vhook = 0;
202 f068206e Bill Eldridge
static int verbose = 1;
203 9c3d33d6 Michael Niedermayer
static int thread_count= 1;
204 b51469a0 Leon van Stuivenberg
static int q_pressed = 0;
205 1008ceb3 Michael Niedermayer
static int64_t video_size = 0;
206
static int64_t audio_size = 0;
207
static int64_t extra_size = 0;
208 a6a92a9a Wolfram Gloger
static int nb_frames_dup = 0;
209
static int nb_frames_drop = 0;
210 6e454c38 Luca Abeni
static int input_sync;
211 b6e16b86 Calcium
static int limit_filesize = 0; //
212 d9a916e2 Charles Yates
213 5b6d5596 Michael Niedermayer
static int pgmyuv_compatibility_hack=0;
214 023857be Harikrishnan Varma
static int dts_delta_threshold = 10;
215 5b6d5596 Michael Niedermayer
216 18a54b04 Luca Abeni
static int sws_flags = SWS_BICUBIC;
217
218 8bbf6db9 Michael Niedermayer
const char **opt_names=NULL;
219
int opt_name_count=0;
220
AVCodecContext *avctx_opts;
221 4eb72c6b Nico Sabbi
AVFormatContext *avformat_opts;
222 8bbf6db9 Michael Niedermayer
223 748c2fca Michael Niedermayer
static AVBitStreamFilterContext *video_bitstream_filters=NULL;
224
static AVBitStreamFilterContext *audio_bitstream_filters=NULL;
225
static AVBitStreamFilterContext *bitstream_filters[MAX_FILES][MAX_STREAMS];
226 5b6d5596 Michael Niedermayer
227 5abdb4b1 Fabrice Bellard
#define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
228 85f07f22 Fabrice Bellard
229 b4a3389e Wolfram Gloger
struct AVInputStream;
230
231 85f07f22 Fabrice Bellard
typedef struct AVOutputStream {
232
    int file_index;          /* file index */
233
    int index;               /* stream index in the output file */
234
    int source_index;        /* AVInputStream index */
235
    AVStream *st;            /* stream in the output file */
236 ec5517d5 Fabrice Bellard
    int encoding_needed;     /* true if encoding needed for this stream */
237
    int frame_number;
238
    /* input pts and corresponding output pts
239
       for A/V sync */
240 b4a3389e Wolfram Gloger
    //double sync_ipts;        /* dts from the AVPacket of the demuxer in second units */
241
    struct AVInputStream *sync_ist; /* input stream to sync against */
242 e928649b Michael Niedermayer
    int64_t sync_opts;       /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number
243 85f07f22 Fabrice Bellard
    /* video only */
244 07d0cdfc Luca Abeni
    int video_resample;
245 a4d36c11 Michael Niedermayer
    AVFrame pict_tmp;      /* temporary image for resampling */
246 18a54b04 Luca Abeni
    struct SwsContext *img_resample_ctx; /* for image resampling */
247
    int resample_height;
248 34b10a57 Dieter
249 07d0cdfc Luca Abeni
    int video_crop;
250 34b10a57 Dieter
    int topBand;             /* cropping area sizes */
251
    int leftBand;
252 115329f1 Diego Biurrun
253 07d0cdfc Luca Abeni
    int video_pad;
254 1ff93ffc Todd Kirby
    int padtop;              /* padding area sizes */
255
    int padbottom;
256
    int padleft;
257
    int padright;
258 115329f1 Diego Biurrun
259 85f07f22 Fabrice Bellard
    /* audio only */
260
    int audio_resample;
261
    ReSampleContext *resample; /* for audio resampling */
262 f5a478f6 Roman Shaposhnik
    AVFifoBuffer fifo;     /* for compression: one audio fifo per codec */
263 5abdb4b1 Fabrice Bellard
    FILE *logfile;
264 85f07f22 Fabrice Bellard
} AVOutputStream;
265
266
typedef struct AVInputStream {
267
    int file_index;
268
    int index;
269
    AVStream *st;
270
    int discard;             /* true if stream data should be discarded */
271
    int decoding_needed;     /* true if the packets must be decoded in 'raw_fifo' */
272 0c1a9eda Zdenek Kabelac
    int64_t sample_index;      /* current sample */
273 bdfcbbed Max Krasnyansky
274
    int64_t       start;     /* time when read started */
275
    unsigned long frame;     /* current frame */
276 254abc2e Fabrice Bellard
    int64_t       next_pts;  /* synthetic pts for cases where pkt.pts
277
                                is not defined */
278 e7d0374f Roman Shaposhnik
    int64_t       pts;       /* current pts */
279 ff4905a5 Michael Niedermayer
    int is_start;            /* is 1 at the start and after a discontinuity */
280 85f07f22 Fabrice Bellard
} AVInputStream;
281
282
typedef struct AVInputFile {
283
    int eof_reached;      /* true if eof reached */
284
    int ist_index;        /* index of first stream in ist_table */
285
    int buffer_size;      /* current total buffer size */
286
    int buffer_size_max;  /* buffer size at which we consider we can stop
287
                             buffering */
288 79fdaa4c Fabrice Bellard
    int nb_streams;       /* nb streams we are aware of */
289 85f07f22 Fabrice Bellard
} AVInputFile;
290
291 2f30a81d Diego Biurrun
#ifndef __MINGW32__
292 bdc4796f Fabrice Bellard
293 85f07f22 Fabrice Bellard
/* init terminal so that we can grab keys */
294
static struct termios oldtty;
295
296
static void term_exit(void)
297
{
298
    tcsetattr (0, TCSANOW, &oldtty);
299
}
300
301 9680a722 Roumen Petrov
static volatile sig_atomic_t received_sigterm = 0;
302
303
static void
304
sigterm_handler(int sig)
305
{
306
    received_sigterm = sig;
307
    term_exit();
308
}
309
310 85f07f22 Fabrice Bellard
static void term_init(void)
311
{
312
    struct termios tty;
313
314
    tcgetattr (0, &tty);
315
    oldtty = tty;
316
317
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
318
                          |INLCR|IGNCR|ICRNL|IXON);
319
    tty.c_oflag |= OPOST;
320
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
321
    tty.c_cflag &= ~(CSIZE|PARENB);
322
    tty.c_cflag |= CS8;
323
    tty.c_cc[VMIN] = 1;
324
    tty.c_cc[VTIME] = 0;
325 115329f1 Diego Biurrun
326 85f07f22 Fabrice Bellard
    tcsetattr (0, TCSANOW, &tty);
327
328 9680a722 Roumen Petrov
    signal(SIGINT , sigterm_handler); /* Interrupt (ANSI).  */
329
    signal(SIGQUIT, sigterm_handler); /* Quit (POSIX).  */
330
    signal(SIGTERM, sigterm_handler); /* Termination (ANSI).  */
331
    /*
332
    register a function to be called at normal program termination
333
    */
334 85f07f22 Fabrice Bellard
    atexit(term_exit);
335 9ddd71fc François Revol
#ifdef CONFIG_BEOS_NETSERVER
336
    fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
337
#endif
338 85f07f22 Fabrice Bellard
}
339
340
/* read a key without blocking */
341
static int read_key(void)
342
{
343 9ddd71fc François Revol
    int n = 1;
344 85f07f22 Fabrice Bellard
    unsigned char ch;
345 9ddd71fc François Revol
#ifndef CONFIG_BEOS_NETSERVER
346
    struct timeval tv;
347 85f07f22 Fabrice Bellard
    fd_set rfds;
348
349
    FD_ZERO(&rfds);
350
    FD_SET(0, &rfds);
351
    tv.tv_sec = 0;
352
    tv.tv_usec = 0;
353
    n = select(1, &rfds, NULL, NULL, &tv);
354 9ddd71fc François Revol
#endif
355 85f07f22 Fabrice Bellard
    if (n > 0) {
356 cb09b2ed Philip Gladstone
        n = read(0, &ch, 1);
357
        if (n == 1)
358 85f07f22 Fabrice Bellard
            return ch;
359 cb09b2ed Philip Gladstone
360
        return n;
361 85f07f22 Fabrice Bellard
    }
362
    return -1;
363
}
364
365 b51469a0 Leon van Stuivenberg
static int decode_interrupt_cb(void)
366
{
367
    return q_pressed || (q_pressed = read_key() == 'q');
368
}
369
370 a38469e1 Fabrice Bellard
#else
371 85f07f22 Fabrice Bellard
372 bee0d9e5 Charles Yates
static volatile int received_sigterm = 0;
373
374 a38469e1 Fabrice Bellard
/* no interactive support */
375
static void term_exit(void)
376 85f07f22 Fabrice Bellard
{
377 a38469e1 Fabrice Bellard
}
378 85f07f22 Fabrice Bellard
379 a38469e1 Fabrice Bellard
static void term_init(void)
380
{
381
}
382 85f07f22 Fabrice Bellard
383 a38469e1 Fabrice Bellard
static int read_key(void)
384
{
385 cb09b2ed Philip Gladstone
    return 0;
386 85f07f22 Fabrice Bellard
}
387
388 a38469e1 Fabrice Bellard
#endif
389 bdc4796f Fabrice Bellard
390 b29f97d1 Zdenek Kabelac
static int read_ffserver_streams(AVFormatContext *s, const char *filename)
391 85f07f22 Fabrice Bellard
{
392 79fdaa4c Fabrice Bellard
    int i, err;
393 85f07f22 Fabrice Bellard
    AVFormatContext *ic;
394
395 79fdaa4c Fabrice Bellard
    err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
396
    if (err < 0)
397
        return err;
398 85f07f22 Fabrice Bellard
    /* copy stream format */
399
    s->nb_streams = ic->nb_streams;
400
    for(i=0;i<ic->nb_streams;i++) {
401
        AVStream *st;
402 1e491e29 Michael Niedermayer
403 f37f8d4c Alex Beregszaszi
        // FIXME: a more elegant solution is needed
404 e1031171 Alex Beregszaszi
        st = av_mallocz(sizeof(AVStream));
405 85f07f22 Fabrice Bellard
        memcpy(st, ic->streams[i], sizeof(AVStream));
406 f37f8d4c Alex Beregszaszi
        st->codec = avcodec_alloc_context();
407
        memcpy(st->codec, ic->streams[i]->codec, sizeof(AVCodecContext));
408 85f07f22 Fabrice Bellard
        s->streams[i] = st;
409
    }
410
411
    av_close_input_file(ic);
412
    return 0;
413
}
414
415 b4a3389e Wolfram Gloger
static double
416
get_sync_ipts(const AVOutputStream *ost)
417
{
418
    const AVInputStream *ist = ost->sync_ist;
419
    return (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/AV_TIME_BASE;
420
}
421
422 748c2fca Michael Niedermayer
static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
423
    while(bsfc){
424
        AVPacket new_pkt= *pkt;
425
        int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
426
                                          &new_pkt.data, &new_pkt.size,
427
                                          pkt->data, pkt->size,
428
                                          pkt->flags & PKT_FLAG_KEY);
429
        if(a){
430
            av_free_packet(pkt);
431
            new_pkt.destruct= av_destruct_packet;
432
        }
433
        *pkt= new_pkt;
434
435
        bsfc= bsfc->next;
436
    }
437
438
    av_interleaved_write_frame(s, pkt);
439
}
440
441 817b23ff Fabrice Bellard
#define MAX_AUDIO_PACKET_SIZE (128 * 1024)
442 85f07f22 Fabrice Bellard
443 115329f1 Diego Biurrun
static void do_audio_out(AVFormatContext *s,
444
                         AVOutputStream *ost,
445 85f07f22 Fabrice Bellard
                         AVInputStream *ist,
446
                         unsigned char *buf, int size)
447
{
448 0c1a9eda Zdenek Kabelac
    uint8_t *buftmp;
449 d66c7abc Sylvain Corré
    static uint8_t *audio_buf = NULL;
450
    static uint8_t *audio_out = NULL;
451 558eae03 Michael Niedermayer
    const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
452 d66c7abc Sylvain Corré
453 85f07f22 Fabrice Bellard
    int size_out, frame_bytes, ret;
454 01f4895c Michael Niedermayer
    AVCodecContext *enc= ost->st->codec;
455 85f07f22 Fabrice Bellard
456 d66c7abc Sylvain Corré
    /* SC: dynamic allocation of buffers */
457
    if (!audio_buf)
458
        audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
459
    if (!audio_out)
460 558eae03 Michael Niedermayer
        audio_out = av_malloc(audio_out_size);
461 d66c7abc Sylvain Corré
    if (!audio_buf || !audio_out)
462
        return;               /* Should signal an error ! */
463
464 986ebcdb Michael Niedermayer
    if(audio_sync_method){
465 115329f1 Diego Biurrun
        double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
466 f5a478f6 Roman Shaposhnik
                - av_fifo_size(&ost->fifo)/(ost->st->codec->channels * 2);
467 01f4895c Michael Niedermayer
        double idelta= delta*ist->st->codec->sample_rate / enc->sample_rate;
468
        int byte_delta= ((int)idelta)*2*ist->st->codec->channels;
469 ff4905a5 Michael Niedermayer
470 986ebcdb Michael Niedermayer
        //FIXME resample delay
471
        if(fabs(delta) > 50){
472 ff4905a5 Michael Niedermayer
            if(ist->is_start){
473
                if(byte_delta < 0){
474 f41dd5aa Michael Niedermayer
                    byte_delta= FFMAX(byte_delta, -size);
475 ff4905a5 Michael Niedermayer
                    size += byte_delta;
476
                    buf  -= byte_delta;
477
                    if(verbose > 2)
478
                        fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
479
                    if(!size)
480
                        return;
481
                    ist->is_start=0;
482
                }else{
483
                    static uint8_t *input_tmp= NULL;
484
                    input_tmp= av_realloc(input_tmp, byte_delta + size);
485
486
                    if(byte_delta + size <= MAX_AUDIO_PACKET_SIZE)
487
                        ist->is_start=0;
488
                    else
489
                        byte_delta= MAX_AUDIO_PACKET_SIZE - size;
490
491
                    memset(input_tmp, 0, byte_delta);
492
                    memcpy(input_tmp + byte_delta, buf, size);
493
                    buf= input_tmp;
494
                    size += byte_delta;
495
                    if(verbose > 2)
496
                        fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
497
                }
498
            }else if(audio_sync_method>1){
499
                int comp= clip(delta, -audio_sync_method, audio_sync_method);
500
                assert(ost->audio_resample);
501
                if(verbose > 2)
502
                    fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
503 949b1a13 Steve L'Homme
//                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));
504 ff4905a5 Michael Niedermayer
                av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
505
            }
506 115329f1 Diego Biurrun
        }
507 986ebcdb Michael Niedermayer
    }else
508 b4a3389e Wolfram Gloger
        ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate)
509 f5a478f6 Roman Shaposhnik
                        - av_fifo_size(&ost->fifo)/(ost->st->codec->channels * 2); //FIXME wrong
510 85f07f22 Fabrice Bellard
511
    if (ost->audio_resample) {
512
        buftmp = audio_buf;
513 115329f1 Diego Biurrun
        size_out = audio_resample(ost->resample,
514 85f07f22 Fabrice Bellard
                                  (short *)buftmp, (short *)buf,
515 01f4895c Michael Niedermayer
                                  size / (ist->st->codec->channels * 2));
516 85f07f22 Fabrice Bellard
        size_out = size_out * enc->channels * 2;
517
    } else {
518
        buftmp = buf;
519
        size_out = size;
520
    }
521
522
    /* now encode as many frames as possible */
523 a0663ba4 Fabrice Bellard
    if (enc->frame_size > 1) {
524 85f07f22 Fabrice Bellard
        /* output resampled raw samples */
525 f5a478f6 Roman Shaposhnik
        av_fifo_write(&ost->fifo, buftmp, size_out);
526 85f07f22 Fabrice Bellard
527
        frame_bytes = enc->frame_size * 2 * enc->channels;
528 115329f1 Diego Biurrun
529 f5a478f6 Roman Shaposhnik
        while (av_fifo_read(&ost->fifo, audio_buf, frame_bytes) == 0) {
530 e928649b Michael Niedermayer
            AVPacket pkt;
531
            av_init_packet(&pkt);
532
533 115329f1 Diego Biurrun
            ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
534 a0663ba4 Fabrice Bellard
                                       (short *)audio_buf);
535 1008ceb3 Michael Niedermayer
            audio_size += ret;
536 e928649b Michael Niedermayer
            pkt.stream_index= ost->index;
537
            pkt.data= audio_out;
538
            pkt.size= ret;
539 e7902f20 Michael Niedermayer
            if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
540 c0df9d75 Michael Niedermayer
                pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
541 e928649b Michael Niedermayer
            pkt.flags |= PKT_FLAG_KEY;
542 748c2fca Michael Niedermayer
            write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
543 115329f1 Diego Biurrun
544 986ebcdb Michael Niedermayer
            ost->sync_opts += enc->frame_size;
545 85f07f22 Fabrice Bellard
        }
546
    } else {
547 e928649b Michael Niedermayer
        AVPacket pkt;
548
        av_init_packet(&pkt);
549 986ebcdb Michael Niedermayer
550 c43042a7 Michael Niedermayer
        ost->sync_opts += size_out / (2 * enc->channels);
551 986ebcdb Michael Niedermayer
552 a0663ba4 Fabrice Bellard
        /* output a pcm frame */
553
        /* XXX: change encoding codec API to avoid this ? */
554
        switch(enc->codec->id) {
555 b461b3bc Reimar Döffinger
        case CODEC_ID_PCM_S32LE:
556
        case CODEC_ID_PCM_S32BE:
557
        case CODEC_ID_PCM_U32LE:
558
        case CODEC_ID_PCM_U32BE:
559
            size_out = size_out << 1;
560
            break;
561
        case CODEC_ID_PCM_S24LE:
562
        case CODEC_ID_PCM_S24BE:
563
        case CODEC_ID_PCM_U24LE:
564
        case CODEC_ID_PCM_U24BE:
565
        case CODEC_ID_PCM_S24DAUD:
566
            size_out = size_out / 2 * 3;
567
            break;
568 a0663ba4 Fabrice Bellard
        case CODEC_ID_PCM_S16LE:
569
        case CODEC_ID_PCM_S16BE:
570
        case CODEC_ID_PCM_U16LE:
571
        case CODEC_ID_PCM_U16BE:
572
            break;
573
        default:
574
            size_out = size_out >> 1;
575
            break;
576
        }
577 115329f1 Diego Biurrun
        ret = avcodec_encode_audio(enc, audio_out, size_out,
578 bb270c08 Diego Biurrun
                                   (short *)buftmp);
579 1008ceb3 Michael Niedermayer
        audio_size += ret;
580 e928649b Michael Niedermayer
        pkt.stream_index= ost->index;
581
        pkt.data= audio_out;
582
        pkt.size= ret;
583 e7902f20 Michael Niedermayer
        if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
584 c0df9d75 Michael Niedermayer
            pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
585 e928649b Michael Niedermayer
        pkt.flags |= PKT_FLAG_KEY;
586 748c2fca Michael Niedermayer
        write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
587 85f07f22 Fabrice Bellard
    }
588
}
589
590 10d104e4 Philip Gladstone
static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
591
{
592
    AVCodecContext *dec;
593
    AVPicture *picture2;
594
    AVPicture picture_tmp;
595 0c1a9eda Zdenek Kabelac
    uint8_t *buf = 0;
596 10d104e4 Philip Gladstone
597 01f4895c Michael Niedermayer
    dec = ist->st->codec;
598 10d104e4 Philip Gladstone
599
    /* deinterlace : must be done before any resize */
600 bee0d9e5 Charles Yates
    if (do_deinterlace || using_vhook) {
601 10d104e4 Philip Gladstone
        int size;
602
603
        /* create temporary picture */
604
        size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
605
        buf = av_malloc(size);
606
        if (!buf)
607
            return;
608 115329f1 Diego Biurrun
609 10d104e4 Philip Gladstone
        picture2 = &picture_tmp;
610
        avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
611
612 dafc3856 Michael Niedermayer
        if (do_deinterlace){
613 115329f1 Diego Biurrun
            if(avpicture_deinterlace(picture2, picture,
614 dafc3856 Michael Niedermayer
                                     dec->pix_fmt, dec->width, dec->height) < 0) {
615
                /* if error, do not deinterlace */
616
                av_free(buf);
617
                buf = NULL;
618
                picture2 = picture;
619
            }
620
        } else {
621 c17be817 Luca Abeni
            img_copy(picture2, picture, dec->pix_fmt, dec->width, dec->height);
622 bee0d9e5 Charles Yates
        }
623 10d104e4 Philip Gladstone
    } else {
624
        picture2 = picture;
625
    }
626
627
    frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
628
629
    if (picture != picture2)
630
        *picture = *picture2;
631
    *bufp = buf;
632
}
633
634 ec5517d5 Fabrice Bellard
/* we begin to correct av delay at this threshold */
635
#define AV_DELAY_MAX 0.100
636 85f07f22 Fabrice Bellard
637 115329f1 Diego Biurrun
static void do_subtitle_out(AVFormatContext *s,
638
                            AVOutputStream *ost,
639 cf7fc795 Fabrice Bellard
                            AVInputStream *ist,
640
                            AVSubtitle *sub,
641
                            int64_t pts)
642
{
643
    static uint8_t *subtitle_out = NULL;
644
    int subtitle_out_max_size = 65536;
645
    int subtitle_out_size, nb, i;
646
    AVCodecContext *enc;
647
    AVPacket pkt;
648
649
    if (pts == AV_NOPTS_VALUE) {
650
        fprintf(stderr, "Subtitle packets must have a pts\n");
651
        return;
652
    }
653
654 01f4895c Michael Niedermayer
    enc = ost->st->codec;
655 cf7fc795 Fabrice Bellard
656
    if (!subtitle_out) {
657
        subtitle_out = av_malloc(subtitle_out_max_size);
658
    }
659
660
    /* Note: DVB subtitle need one packet to draw them and one other
661
       packet to clear them */
662
    /* XXX: signal it in the codec context ? */
663
    if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
664
        nb = 2;
665
    else
666
        nb = 1;
667
668
    for(i = 0; i < nb; i++) {
669 115329f1 Diego Biurrun
        subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
670 cf7fc795 Fabrice Bellard
                                                    subtitle_out_max_size, sub);
671 115329f1 Diego Biurrun
672 cf7fc795 Fabrice Bellard
        av_init_packet(&pkt);
673
        pkt.stream_index = ost->index;
674
        pkt.data = subtitle_out;
675
        pkt.size = subtitle_out_size;
676
        pkt.pts = av_rescale_q(av_rescale_q(pts, ist->st->time_base, AV_TIME_BASE_Q) + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q,  ost->st->time_base);
677
        if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
678
            /* XXX: the pts correction is handled here. Maybe handling
679
               it in the codec would be better */
680
            if (i == 0)
681
                pkt.pts += 90 * sub->start_display_time;
682
            else
683
                pkt.pts += 90 * sub->end_display_time;
684
        }
685 748c2fca Michael Niedermayer
        write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
686 cf7fc795 Fabrice Bellard
    }
687
}
688
689 8a6cb114 Michael Niedermayer
static int bit_buffer_size= 1024*256;
690 27537106 Michael Niedermayer
static uint8_t *bit_buffer= NULL;
691 1ff93ffc Todd Kirby
692 115329f1 Diego Biurrun
static void do_video_out(AVFormatContext *s,
693
                         AVOutputStream *ost,
694 85f07f22 Fabrice Bellard
                         AVInputStream *ist,
695 7a0f9d7e Fabrice Bellard
                         AVFrame *in_picture,
696 986ebcdb Michael Niedermayer
                         int *frame_size)
697 85f07f22 Fabrice Bellard
{
698 ec5517d5 Fabrice Bellard
    int nb_frames, i, ret;
699 07d0cdfc Luca Abeni
    AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;
700 18a54b04 Luca Abeni
    AVFrame picture_crop_temp, picture_pad_temp;
701 0c1a9eda Zdenek Kabelac
    uint8_t *buf = NULL, *buf1 = NULL;
702 cfcf0ffd Fabrice Bellard
    AVCodecContext *enc, *dec;
703 115329f1 Diego Biurrun
704 a4d36c11 Michael Niedermayer
    avcodec_get_frame_defaults(&picture_crop_temp);
705 07d0cdfc Luca Abeni
    avcodec_get_frame_defaults(&picture_pad_temp);
706 a4d36c11 Michael Niedermayer
707 01f4895c Michael Niedermayer
    enc = ost->st->codec;
708
    dec = ist->st->codec;
709 85f07f22 Fabrice Bellard
710 ec5517d5 Fabrice Bellard
    /* by default, we output a single frame */
711
    nb_frames = 1;
712
713 204c0f48 Philip Gladstone
    *frame_size = 0;
714
715 986ebcdb Michael Niedermayer
    if(video_sync_method){
716 10d104e4 Philip Gladstone
        double vdelta;
717 b4a3389e Wolfram Gloger
        vdelta = get_sync_ipts(ost) / av_q2d(enc->time_base) - ost->sync_opts;
718 e928649b Michael Niedermayer
        //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
719
        if (vdelta < -1.1)
720
            nb_frames = 0;
721
        else if (vdelta > 1.1)
722 70122f29 Michael Niedermayer
            nb_frames = lrintf(vdelta);
723 949b1a13 Steve L'Homme
//fprintf(stderr, "vdelta:%f, ost->sync_opts:%"PRId64", ost->sync_ipts:%f nb_frames:%d\n", vdelta, ost->sync_opts, ost->sync_ipts, nb_frames);
724 50c3dd32 Michael Niedermayer
        if (nb_frames == 0){
725
            ++nb_frames_drop;
726
            if (verbose>2)
727
                fprintf(stderr, "*** drop!\n");
728 8300609b Michael Niedermayer
        }else if (nb_frames > 1) {
729
            nb_frames_dup += nb_frames;
730 50c3dd32 Michael Niedermayer
            if (verbose>2)
731 8300609b Michael Niedermayer
                fprintf(stderr, "*** %d dup!\n", nb_frames-1);
732 50c3dd32 Michael Niedermayer
        }
733
    }else
734 b4a3389e Wolfram Gloger
        ost->sync_opts= lrintf(get_sync_ipts(ost) / av_q2d(enc->time_base));
735 445f1b83 Roman Shaposhnik
736 7ca60994 Michael Niedermayer
    nb_frames= FFMIN(nb_frames, max_frames[CODEC_TYPE_VIDEO] - ost->frame_number);
737 115329f1 Diego Biurrun
    if (nb_frames <= 0)
738 85f07f22 Fabrice Bellard
        return;
739 ce7c56c2 Juanjo
740 07d0cdfc Luca Abeni
    if (ost->video_crop) {
741 18a54b04 Luca Abeni
        if (img_crop((AVPicture *)&picture_crop_temp, (AVPicture *)in_picture, dec->pix_fmt, ost->topBand, ost->leftBand) < 0) {
742 07d0cdfc Luca Abeni
            av_log(NULL, AV_LOG_ERROR, "error cropping picture\n");
743
            goto the_end;
744
        }
745
        formatted_picture = &picture_crop_temp;
746 18a54b04 Luca Abeni
    } else {
747
        formatted_picture = in_picture;
748 07d0cdfc Luca Abeni
    }
749
750
    final_picture = formatted_picture;
751
    padding_src = formatted_picture;
752
    resampling_dst = &ost->pict_tmp;
753
    if (ost->video_pad) {
754
        final_picture = &ost->pict_tmp;
755
        if (ost->video_resample) {
756 18a54b04 Luca Abeni
            if (img_crop((AVPicture *)&picture_pad_temp, (AVPicture *)final_picture, enc->pix_fmt, ost->padtop, ost->padleft) < 0) {
757 07d0cdfc Luca Abeni
                av_log(NULL, AV_LOG_ERROR, "error padding picture\n");
758
                goto the_end;
759
            }
760
            resampling_dst = &picture_pad_temp;
761
        }
762
    }
763
764 85f07f22 Fabrice Bellard
    if (ost->video_resample) {
765 07d0cdfc Luca Abeni
        padding_src = NULL;
766 34b10a57 Dieter
        final_picture = &ost->pict_tmp;
767 18a54b04 Luca Abeni
        sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize,
768
              0, ost->resample_height, resampling_dst->data, resampling_dst->linesize);
769 f122deb4 Luca Abeni
    }
770 07d0cdfc Luca Abeni
771
    if (ost->video_pad) {
772
        img_pad((AVPicture*)final_picture, (AVPicture *)padding_src,
773
                enc->height, enc->width, enc->pix_fmt,
774
                ost->padtop, ost->padbottom, ost->padleft, ost->padright, padcolor);
775 85f07f22 Fabrice Bellard
    }
776 07d0cdfc Luca Abeni
777 85f07f22 Fabrice Bellard
    /* duplicates frame if needed */
778 ec5517d5 Fabrice Bellard
    for(i=0;i<nb_frames;i++) {
779 e928649b Michael Niedermayer
        AVPacket pkt;
780
        av_init_packet(&pkt);
781
        pkt.stream_index= ost->index;
782
783 e8750b00 Fred Rothganger
        if (s->oformat->flags & AVFMT_RAWPICTURE) {
784
            /* raw pictures are written as AVPicture structure to
785
               avoid any copies. We support temorarily the older
786
               method. */
787 2744ca9a Roman Shaposhnik
            AVFrame* old_frame = enc->coded_frame;
788 bb270c08 Diego Biurrun
            enc->coded_frame = dec->coded_frame; //FIXME/XXX remove this hack
789 e928649b Michael Niedermayer
            pkt.data= (uint8_t *)final_picture;
790
            pkt.size=  sizeof(AVPicture);
791 e7902f20 Michael Niedermayer
            if(dec->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
792 c0df9d75 Michael Niedermayer
                pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
793 e928649b Michael Niedermayer
            if(dec->coded_frame && dec->coded_frame->key_frame)
794
                pkt.flags |= PKT_FLAG_KEY;
795
796 748c2fca Michael Niedermayer
            write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
797 bb270c08 Diego Biurrun
            enc->coded_frame = old_frame;
798 e8750b00 Fred Rothganger
        } else {
799 492cd3a9 Michael Niedermayer
            AVFrame big_picture;
800 a4d36c11 Michael Niedermayer
801
            big_picture= *final_picture;
802 7a0f9d7e Fabrice Bellard
            /* better than nothing: use input picture interlaced
803
               settings */
804
            big_picture.interlaced_frame = in_picture->interlaced_frame;
805 637b5326 Michael Niedermayer
            if(avctx_opts->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)){
806 bb198e19 Michael Niedermayer
                if(top_field_first == -1)
807
                    big_picture.top_field_first = in_picture->top_field_first;
808
                else
809 2a8edc5d Michael Niedermayer
                    big_picture.top_field_first = top_field_first;
810 bb198e19 Michael Niedermayer
            }
811 7a0f9d7e Fabrice Bellard
812 85f07f22 Fabrice Bellard
            /* handles sameq here. This is not correct because it may
813
               not be a global option */
814
            if (same_quality) {
815 1e491e29 Michael Niedermayer
                big_picture.quality = ist->st->quality;
816
            }else
817
                big_picture.quality = ost->st->quality;
818 f4f3223f Michael Niedermayer
            if(!me_threshold)
819
                big_picture.pict_type = 0;
820 50c3dd32 Michael Niedermayer
//            big_picture.pts = AV_NOPTS_VALUE;
821 c0df9d75 Michael Niedermayer
            big_picture.pts= ost->sync_opts;
822
//            big_picture.pts= av_rescale(ost->sync_opts, AV_TIME_BASE*(int64_t)enc->time_base.num, enc->time_base.den);
823 949b1a13 Steve L'Homme
//av_log(NULL, AV_LOG_DEBUG, "%"PRId64" -> encoder\n", ost->sync_opts);
824 115329f1 Diego Biurrun
            ret = avcodec_encode_video(enc,
825 8a6cb114 Michael Niedermayer
                                       bit_buffer, bit_buffer_size,
826 1e491e29 Michael Niedermayer
                                       &big_picture);
827 4156a436 Panagiotis Issaris
            if (ret == -1) {
828
                fprintf(stderr, "Video encoding failed\n");
829
                exit(1);
830
            }
831 44429457 Juanjo
            //enc->frame_number = enc->real_pict_num;
832 4bbc6260 Michael Niedermayer
            if(ret>0){
833 27537106 Michael Niedermayer
                pkt.data= bit_buffer;
834 e928649b Michael Niedermayer
                pkt.size= ret;
835 e7902f20 Michael Niedermayer
                if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
836 c0df9d75 Michael Niedermayer
                    pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
837 949b1a13 Steve L'Homme
/*av_log(NULL, AV_LOG_DEBUG, "encoder -> %"PRId64"/%"PRId64"\n",
838 c0df9d75 Michael Niedermayer
   pkt.pts != AV_NOPTS_VALUE ? av_rescale(pkt.pts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1,
839
   pkt.dts != AV_NOPTS_VALUE ? av_rescale(pkt.dts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1);*/
840 50c3dd32 Michael Niedermayer
841 e928649b Michael Niedermayer
                if(enc->coded_frame && enc->coded_frame->key_frame)
842
                    pkt.flags |= PKT_FLAG_KEY;
843 748c2fca Michael Niedermayer
                write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
844 e928649b Michael Niedermayer
                *frame_size = ret;
845
                //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
846
                //        enc->frame_number-1, enc->real_pict_num, ret,
847
                //        enc->pict_type);
848
                /* if two pass, output log */
849
                if (ost->logfile && enc->stats_out) {
850
                    fprintf(ost->logfile, "%s", enc->stats_out);
851
                }
852 5abdb4b1 Fabrice Bellard
            }
853 85f07f22 Fabrice Bellard
        }
854 50c3dd32 Michael Niedermayer
        ost->sync_opts++;
855 ec5517d5 Fabrice Bellard
        ost->frame_number++;
856 85f07f22 Fabrice Bellard
    }
857 ec5517d5 Fabrice Bellard
 the_end:
858 0f1578af Fabrice Bellard
    av_free(buf);
859
    av_free(buf1);
860 85f07f22 Fabrice Bellard
}
861
862 140cb663 Michael Niedermayer
static double psnr(double d){
863
    if(d==0) return INFINITY;
864 b29f97d1 Zdenek Kabelac
    return -10.0*log(d)/log(10.0);
865 140cb663 Michael Niedermayer
}
866
867 115329f1 Diego Biurrun
static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
868 ec5517d5 Fabrice Bellard
                           int frame_size)
869 ce7c56c2 Juanjo
{
870
    static FILE *fvstats=NULL;
871
    char filename[40];
872 bf5af568 Fabrice Bellard
    time_t today2;
873
    struct tm *today;
874 ce7c56c2 Juanjo
    AVCodecContext *enc;
875
    int frame_number;
876 0c1a9eda Zdenek Kabelac
    int64_t ti;
877 ce7c56c2 Juanjo
    double ti1, bitrate, avg_bitrate;
878 115329f1 Diego Biurrun
879 ce7c56c2 Juanjo
    if (!fvstats) {
880
        today2 = time(NULL);
881
        today = localtime(&today2);
882 2fc8ea24 Michael Niedermayer
        snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour,
883 ce7c56c2 Juanjo
                                               today->tm_min,
884
                                               today->tm_sec);
885
        fvstats = fopen(filename,"w");
886
        if (!fvstats) {
887
            perror("fopen");
888
            exit(1);
889
        }
890
    }
891 115329f1 Diego Biurrun
892 ce7c56c2 Juanjo
    ti = MAXINT64;
893 01f4895c Michael Niedermayer
    enc = ost->st->codec;
894 ce7c56c2 Juanjo
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
895 ec5517d5 Fabrice Bellard
        frame_number = ost->frame_number;
896 158c7f05 Michael Niedermayer
        fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
897 140cb663 Michael Niedermayer
        if (enc->flags&CODEC_FLAG_PSNR)
898 492cd3a9 Michael Niedermayer
            fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
899 115329f1 Diego Biurrun
900 ce7c56c2 Juanjo
        fprintf(fvstats,"f_size= %6d ", frame_size);
901 ec5517d5 Fabrice Bellard
        /* compute pts value */
902 c0df9d75 Michael Niedermayer
        ti1 = ost->sync_opts * av_q2d(enc->time_base);
903 ce7c56c2 Juanjo
        if (ti1 < 0.01)
904
            ti1 = 0.01;
905 115329f1 Diego Biurrun
906 c0df9d75 Michael Niedermayer
        bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
907 1008ceb3 Michael Niedermayer
        avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
908 ce7c56c2 Juanjo
        fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
909 1008ceb3 Michael Niedermayer
            (double)video_size / 1024, ti1, bitrate, avg_bitrate);
910 115329f1 Diego Biurrun
        fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
911 ce7c56c2 Juanjo
    }
912 ec5517d5 Fabrice Bellard
}
913
914 b29f97d1 Zdenek Kabelac
static void print_report(AVFormatContext **output_files,
915 bb270c08 Diego Biurrun
                         AVOutputStream **ost_table, int nb_ostreams,
916
                         int is_last_report)
917 ec5517d5 Fabrice Bellard
{
918
    char buf[1024];
919
    AVOutputStream *ost;
920
    AVFormatContext *oc, *os;
921 0c1a9eda Zdenek Kabelac
    int64_t total_size;
922 ec5517d5 Fabrice Bellard
    AVCodecContext *enc;
923
    int frame_number, vid, i;
924
    double bitrate, ti1, pts;
925 0c1a9eda Zdenek Kabelac
    static int64_t last_time = -1;
926 0888fd22 Michael Niedermayer
    static int qp_histogram[52];
927 115329f1 Diego Biurrun
928 ec5517d5 Fabrice Bellard
    if (!is_last_report) {
929 0c1a9eda Zdenek Kabelac
        int64_t cur_time;
930 ec5517d5 Fabrice Bellard
        /* display the report every 0.5 seconds */
931
        cur_time = av_gettime();
932
        if (last_time == -1) {
933
            last_time = cur_time;
934
            return;
935 115329f1 Diego Biurrun
        }
936 ec5517d5 Fabrice Bellard
        if ((cur_time - last_time) < 500000)
937
            return;
938
        last_time = cur_time;
939
    }
940
941 ce7c56c2 Juanjo
942 ec5517d5 Fabrice Bellard
    oc = output_files[0];
943
944
    total_size = url_ftell(&oc->pb);
945 115329f1 Diego Biurrun
946 ec5517d5 Fabrice Bellard
    buf[0] = '\0';
947
    ti1 = 1e10;
948
    vid = 0;
949
    for(i=0;i<nb_ostreams;i++) {
950
        ost = ost_table[i];
951
        os = output_files[ost->file_index];
952 01f4895c Michael Niedermayer
        enc = ost->st->codec;
953 10d104e4 Philip Gladstone
        if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
954 0ecca7a4 Michael Niedermayer
            snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ",
955 158c7f05 Michael Niedermayer
                    enc->coded_frame->quality/(float)FF_QP2LAMBDA);
956 10d104e4 Philip Gladstone
        }
957 ec5517d5 Fabrice Bellard
        if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
958
            frame_number = ost->frame_number;
959 0888fd22 Michael Niedermayer
            snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d q=%3.1f ",
960
                    frame_number, enc->coded_frame ? enc->coded_frame->quality/(float)FF_QP2LAMBDA : -1);
961 890972be Michael Niedermayer
            if(is_last_report)
962 0ecca7a4 Michael Niedermayer
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
963 0888fd22 Michael Niedermayer
            if(qp_hist && enc->coded_frame){
964
                int j;
965
                int qp= lrintf(enc->coded_frame->quality/(float)FF_QP2LAMBDA);
966
                if(qp>=0 && qp<sizeof(qp_histogram)/sizeof(int))
967
                    qp_histogram[qp]++;
968
                for(j=0; j<32; j++)
969
                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2)));
970
            }
971 890972be Michael Niedermayer
            if (enc->flags&CODEC_FLAG_PSNR){
972
                int j;
973
                double error, error_sum=0;
974
                double scale, scale_sum=0;
975
                char type[3]= {'Y','U','V'};
976 0ecca7a4 Michael Niedermayer
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
977 890972be Michael Niedermayer
                for(j=0; j<3; j++){
978
                    if(is_last_report){
979
                        error= enc->error[j];
980
                        scale= enc->width*enc->height*255.0*255.0*frame_number;
981
                    }else{
982
                        error= enc->coded_frame->error[j];
983
                        scale= enc->width*enc->height*255.0*255.0;
984
                    }
985
                    if(j) scale/=4;
986
                    error_sum += error;
987
                    scale_sum += scale;
988 0ecca7a4 Michael Niedermayer
                    snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
989 890972be Michael Niedermayer
                }
990 0ecca7a4 Michael Niedermayer
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
991 890972be Michael Niedermayer
            }
992 ec5517d5 Fabrice Bellard
            vid = 1;
993
        }
994
        /* compute min output value */
995 9ee91c2f Michael Niedermayer
        pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
996 5d9827bc Kareila
        if ((pts < ti1) && (pts > 0))
997 ec5517d5 Fabrice Bellard
            ti1 = pts;
998
    }
999
    if (ti1 < 0.01)
1000
        ti1 = 0.01;
1001 115329f1 Diego Biurrun
1002 f068206e Bill Eldridge
    if (verbose || is_last_report) {
1003
        bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1004 115329f1 Diego Biurrun
1005
        snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1006 ec5517d5 Fabrice Bellard
            "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1007
            (double)total_size / 1024, ti1, bitrate);
1008 a6a92a9a Wolfram Gloger
1009 bb270c08 Diego Biurrun
        if (verbose > 1)
1010
          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
1011
                  nb_frames_dup, nb_frames_drop);
1012 115329f1 Diego Biurrun
1013 d8019eb5 Allen Day
        if (verbose >= 0)
1014
            fprintf(stderr, "%s    \r", buf);
1015
1016 ec5517d5 Fabrice Bellard
        fflush(stderr);
1017
    }
1018 115329f1 Diego Biurrun
1019 1008ceb3 Michael Niedermayer
    if (is_last_report && verbose >= 0){
1020
        int64_t raw= audio_size + video_size + extra_size;
1021 f068206e Bill Eldridge
        fprintf(stderr, "\n");
1022 1008ceb3 Michael Niedermayer
        fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
1023
                video_size/1024.0,
1024
                audio_size/1024.0,
1025
                extra_size/1024.0,
1026
                100.0*(total_size - raw)/raw
1027
        );
1028
    }
1029 ce7c56c2 Juanjo
}
1030
1031 a700a6ae Fabrice Bellard
/* pkt = NULL means EOF (needed to flush decoder buffers) */
1032
static int output_packet(AVInputStream *ist, int ist_index,
1033
                         AVOutputStream **ost_table, int nb_ostreams,
1034 4b85a28f Wolfram Gloger
                         const AVPacket *pkt)
1035 a700a6ae Fabrice Bellard
{
1036
    AVFormatContext *os;
1037
    AVOutputStream *ost;
1038
    uint8_t *ptr;
1039
    int len, ret, i;
1040
    uint8_t *data_buf;
1041
    int data_size, got_picture;
1042
    AVFrame picture;
1043
    void *buffer_to_free;
1044 f038fe8b Diego Biurrun
    static unsigned int samples_size= 0;
1045 78953e62 Michael Niedermayer
    static short *samples= NULL;
1046 cf7fc795 Fabrice Bellard
    AVSubtitle subtitle, *subtitle_to_free;
1047
    int got_subtitle;
1048 115329f1 Diego Biurrun
1049 2b18dcd0 Michael Niedermayer
    if(!pkt){
1050
        ist->pts= ist->next_pts; // needed for last packet if vsync=0
1051
    } else if (pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
1052 c0df9d75 Michael Niedermayer
        ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
1053 a700a6ae Fabrice Bellard
    } else {
1054 a603bf8d Michael Niedermayer
//        assert(ist->pts == ist->next_pts);
1055 a700a6ae Fabrice Bellard
    }
1056 115329f1 Diego Biurrun
1057 a700a6ae Fabrice Bellard
    if (pkt == NULL) {
1058
        /* EOF handling */
1059
        ptr = NULL;
1060
        len = 0;
1061
        goto handle_eof;
1062
    }
1063
1064
    len = pkt->size;
1065
    ptr = pkt->data;
1066
    while (len > 0) {
1067
    handle_eof:
1068
        /* decode the packet if needed */
1069
        data_buf = NULL; /* fail safe */
1070
        data_size = 0;
1071 cf7fc795 Fabrice Bellard
        subtitle_to_free = NULL;
1072 a700a6ae Fabrice Bellard
        if (ist->decoding_needed) {
1073 01f4895c Michael Niedermayer
            switch(ist->st->codec->codec_type) {
1074 df84ac2e Michael Niedermayer
            case CODEC_TYPE_AUDIO:{
1075 115329f1 Diego Biurrun
                if(pkt)
1076 78953e62 Michael Niedermayer
                    samples= av_fast_realloc(samples, &samples_size, FFMAX(pkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE));
1077 a700a6ae Fabrice Bellard
                    /* XXX: could avoid copy if PCM 16 bits with same
1078
                       endianness as CPU */
1079 01f4895c Michael Niedermayer
                ret = avcodec_decode_audio(ist->st->codec, samples, &data_size,
1080 a700a6ae Fabrice Bellard
                                           ptr, len);
1081
                if (ret < 0)
1082
                    goto fail_decode;
1083
                ptr += ret;
1084
                len -= ret;
1085
                /* Some bug in mpeg audio decoder gives */
1086
                /* data_size < 0, it seems they are overflows */
1087
                if (data_size <= 0) {
1088
                    /* no audio frame */
1089
                    continue;
1090
                }
1091
                data_buf = (uint8_t *)samples;
1092 115329f1 Diego Biurrun
                ist->next_pts += ((int64_t)AV_TIME_BASE/2 * data_size) /
1093 01f4895c Michael Niedermayer
                    (ist->st->codec->sample_rate * ist->st->codec->channels);
1094 df84ac2e Michael Niedermayer
                break;}
1095 a700a6ae Fabrice Bellard
            case CODEC_TYPE_VIDEO:
1096 01f4895c Michael Niedermayer
                    data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
1097 a700a6ae Fabrice Bellard
                    /* XXX: allocate picture correctly */
1098 9740beff Michael Niedermayer
                    avcodec_get_frame_defaults(&picture);
1099
1100 115329f1 Diego Biurrun
                    ret = avcodec_decode_video(ist->st->codec,
1101 a700a6ae Fabrice Bellard
                                               &picture, &got_picture, ptr, len);
1102
                    ist->st->quality= picture.quality;
1103 115329f1 Diego Biurrun
                    if (ret < 0)
1104 a700a6ae Fabrice Bellard
                        goto fail_decode;
1105
                    if (!got_picture) {
1106
                        /* no picture yet */
1107
                        goto discard_packet;
1108
                    }
1109 01f4895c Michael Niedermayer
                    if (ist->st->codec->time_base.num != 0) {
1110 115329f1 Diego Biurrun
                        ist->next_pts += ((int64_t)AV_TIME_BASE *
1111 01f4895c Michael Niedermayer
                                          ist->st->codec->time_base.num) /
1112
                            ist->st->codec->time_base.den;
1113 a700a6ae Fabrice Bellard
                    }
1114
                    len = 0;
1115
                    break;
1116 cf7fc795 Fabrice Bellard
            case CODEC_TYPE_SUBTITLE:
1117 115329f1 Diego Biurrun
                ret = avcodec_decode_subtitle(ist->st->codec,
1118 cf7fc795 Fabrice Bellard
                                              &subtitle, &got_subtitle, ptr, len);
1119
                if (ret < 0)
1120 a700a6ae Fabrice Bellard
                    goto fail_decode;
1121 cf7fc795 Fabrice Bellard
                if (!got_subtitle) {
1122
                    goto discard_packet;
1123 a700a6ae Fabrice Bellard
                }
1124 cf7fc795 Fabrice Bellard
                subtitle_to_free = &subtitle;
1125
                len = 0;
1126
                break;
1127
            default:
1128
                goto fail_decode;
1129
            }
1130
        } else {
1131 01f4895c Michael Niedermayer
                switch(ist->st->codec->codec_type) {
1132 2fef0bdf Michael Niedermayer
                case CODEC_TYPE_AUDIO:
1133 115329f1 Diego Biurrun
                    ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
1134 01f4895c Michael Niedermayer
                        (ist->st->codec->sample_rate * ist->st->codec->channels);
1135 2fef0bdf Michael Niedermayer
                    break;
1136
                case CODEC_TYPE_VIDEO:
1137 01f4895c Michael Niedermayer
                    if (ist->st->codec->time_base.num != 0) {
1138 115329f1 Diego Biurrun
                        ist->next_pts += ((int64_t)AV_TIME_BASE *
1139 01f4895c Michael Niedermayer
                                          ist->st->codec->time_base.num) /
1140
                            ist->st->codec->time_base.den;
1141 2fef0bdf Michael Niedermayer
                    }
1142
                    break;
1143
                }
1144 a700a6ae Fabrice Bellard
                data_buf = ptr;
1145
                data_size = len;
1146
                ret = len;
1147
                len = 0;
1148
            }
1149
1150
            buffer_to_free = NULL;
1151 01f4895c Michael Niedermayer
            if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO) {
1152 115329f1 Diego Biurrun
                pre_process_video_frame(ist, (AVPicture *)&picture,
1153 a700a6ae Fabrice Bellard
                                        &buffer_to_free);
1154
            }
1155
1156 7e987c33 Calcium
            // preprocess audio (volume)
1157 01f4895c Michael Niedermayer
            if (ist->st->codec->codec_type == CODEC_TYPE_AUDIO) {
1158 a9aa3467 Michael Niedermayer
                if (audio_volume != 256) {
1159 7e987c33 Calcium
                    short *volp;
1160
                    volp = samples;
1161
                    for(i=0;i<(data_size / sizeof(short));i++) {
1162 a9aa3467 Michael Niedermayer
                        int v = ((*volp) * audio_volume + 128) >> 8;
1163 7e987c33 Calcium
                        if (v < -32768) v = -32768;
1164
                        if (v >  32767) v = 32767;
1165
                        *volp++ = v;
1166
                    }
1167
                }
1168
            }
1169
1170 a700a6ae Fabrice Bellard
            /* frame rate emulation */
1171 01f4895c Michael Niedermayer
            if (ist->st->codec->rate_emu) {
1172
                int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec->time_base.num, 1000000, ist->st->codec->time_base.den);
1173 a700a6ae Fabrice Bellard
                int64_t now = av_gettime() - ist->start;
1174
                if (pts > now)
1175
                    usleep(pts - now);
1176
1177
                ist->frame++;
1178
            }
1179
1180
#if 0
1181
            /* mpeg PTS deordering : if it is a P or I frame, the PTS
1182
               is the one of the next displayed one */
1183
            /* XXX: add mpeg4 too ? */
1184 01f4895c Michael Niedermayer
            if (ist->st->codec->codec_id == CODEC_ID_MPEG1VIDEO) {
1185
                if (ist->st->codec->pict_type != B_TYPE) {
1186 a700a6ae Fabrice Bellard
                    int64_t tmp;
1187
                    tmp = ist->last_ip_pts;
1188
                    ist->last_ip_pts  = ist->frac_pts.val;
1189
                    ist->frac_pts.val = tmp;
1190
                }
1191
            }
1192
#endif
1193 115329f1 Diego Biurrun
            /* if output time reached then transcode raw format,
1194 bb270c08 Diego Biurrun
               encode packets and output them */
1195 a700a6ae Fabrice Bellard
            if (start_time == 0 || ist->pts >= start_time)
1196
                for(i=0;i<nb_ostreams;i++) {
1197
                    int frame_size;
1198
1199
                    ost = ost_table[i];
1200
                    if (ost->source_index == ist_index) {
1201
                        os = output_files[ost->file_index];
1202
1203
#if 0
1204 115329f1 Diego Biurrun
                        printf("%d: got pts=%0.3f %0.3f\n", i,
1205
                               (double)pkt->pts / AV_TIME_BASE,
1206
                               ((double)ist->pts / AV_TIME_BASE) -
1207 986ebcdb Michael Niedermayer
                               ((double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den));
1208 a700a6ae Fabrice Bellard
#endif
1209
                        /* set the input output pts pairs */
1210 b4a3389e Wolfram Gloger
                        //ost->sync_ipts = (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/ AV_TIME_BASE;
1211 a700a6ae Fabrice Bellard
1212
                        if (ost->encoding_needed) {
1213 01f4895c Michael Niedermayer
                            switch(ost->st->codec->codec_type) {
1214 a700a6ae Fabrice Bellard
                            case CODEC_TYPE_AUDIO:
1215
                                do_audio_out(os, ost, ist, data_buf, data_size);
1216
                                break;
1217
                            case CODEC_TYPE_VIDEO:
1218 986ebcdb Michael Niedermayer
                                    do_video_out(os, ost, ist, &picture, &frame_size);
1219 1008ceb3 Michael Niedermayer
                                    video_size += frame_size;
1220 a700a6ae Fabrice Bellard
                                    if (do_vstats && frame_size)
1221
                                        do_video_stats(os, ost, frame_size);
1222
                                break;
1223 cf7fc795 Fabrice Bellard
                            case CODEC_TYPE_SUBTITLE:
1224
                                do_subtitle_out(os, ost, ist, &subtitle,
1225
                                                pkt->pts);
1226
                                break;
1227 a700a6ae Fabrice Bellard
                            default:
1228
                                av_abort();
1229
                            }
1230
                        } else {
1231 e928649b Michael Niedermayer
                            AVFrame avframe; //FIXME/XXX remove this
1232
                            AVPacket opkt;
1233
                            av_init_packet(&opkt);
1234
1235 a700a6ae Fabrice Bellard
                            /* no reencoding needed : output the packet directly */
1236
                            /* force the input stream PTS */
1237 115329f1 Diego Biurrun
1238 9740beff Michael Niedermayer
                            avcodec_get_frame_defaults(&avframe);
1239 01f4895c Michael Niedermayer
                            ost->st->codec->coded_frame= &avframe;
1240 115329f1 Diego Biurrun
                            avframe.key_frame = pkt->flags & PKT_FLAG_KEY;
1241 bf17ff61 Michael Niedermayer
1242 01f4895c Michael Niedermayer
                            if(ost->st->codec->codec_type == CODEC_TYPE_AUDIO)
1243 c7baf14b Michael Niedermayer
                                audio_size += data_size;
1244 01f4895c Michael Niedermayer
                            else if (ost->st->codec->codec_type == CODEC_TYPE_VIDEO) {
1245 c7baf14b Michael Niedermayer
                                video_size += data_size;
1246 daed32f7 Wolfram Gloger
                                ost->sync_opts++;
1247
                            }
1248 c7baf14b Michael Niedermayer
1249 e928649b Michael Niedermayer
                            opkt.stream_index= ost->index;
1250 6c55b27f Michael Niedermayer
                            if(pkt->pts != AV_NOPTS_VALUE)
1251 c0df9d75 Michael Niedermayer
                                opkt.pts= av_rescale_q(av_rescale_q(pkt->pts, ist->st->time_base, AV_TIME_BASE_Q) + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q,  ost->st->time_base);
1252 6c55b27f Michael Niedermayer
                            else
1253
                                opkt.pts= AV_NOPTS_VALUE;
1254 cf7fc795 Fabrice Bellard
1255
                            {
1256
                                int64_t dts;
1257
                                if (pkt->dts == AV_NOPTS_VALUE)
1258
                                    dts = ist->next_pts;
1259
                                else
1260
                                    dts= av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
1261
                                opkt.dts= av_rescale_q(dts + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q,  ost->st->time_base);
1262
                            }
1263 e928649b Michael Niedermayer
                            opkt.flags= pkt->flags;
1264 748c2fca Michael Niedermayer
1265
                            //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
1266 01f4895c Michael Niedermayer
                            if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & PKT_FLAG_KEY))
1267 90ad92b3 Michael Niedermayer
                                opkt.destruct= av_destruct_packet;
1268 748c2fca Michael Niedermayer
1269
                            write_frame(os, &opkt, ost->st->codec, bitstream_filters[ost->file_index][pkt->stream_index]);
1270 01f4895c Michael Niedermayer
                            ost->st->codec->frame_number++;
1271 a700a6ae Fabrice Bellard
                            ost->frame_number++;
1272 90ad92b3 Michael Niedermayer
                            av_free_packet(&opkt);
1273 a700a6ae Fabrice Bellard
                        }
1274
                    }
1275
                }
1276
            av_free(buffer_to_free);
1277 cf7fc795 Fabrice Bellard
            /* XXX: allocate the subtitles in the codec ? */
1278
            if (subtitle_to_free) {
1279 c6ec28b1 Michael Niedermayer
                if (subtitle_to_free->rects != NULL) {
1280
                    for (i = 0; i < subtitle_to_free->num_rects; i++) {
1281
                        av_free(subtitle_to_free->rects[i].bitmap);
1282
                        av_free(subtitle_to_free->rects[i].rgba_palette);
1283
                    }
1284
                    av_freep(&subtitle_to_free->rects);
1285
                }
1286
                subtitle_to_free->num_rects = 0;
1287 cf7fc795 Fabrice Bellard
                subtitle_to_free = NULL;
1288
            }
1289 a700a6ae Fabrice Bellard
        }
1290
 discard_packet:
1291 6f824977 Michael Niedermayer
    if (pkt == NULL) {
1292
        /* EOF handling */
1293 115329f1 Diego Biurrun
1294 6f824977 Michael Niedermayer
        for(i=0;i<nb_ostreams;i++) {
1295
            ost = ost_table[i];
1296
            if (ost->source_index == ist_index) {
1297 01f4895c Michael Niedermayer
                AVCodecContext *enc= ost->st->codec;
1298 6f824977 Michael Niedermayer
                os = output_files[ost->file_index];
1299 115329f1 Diego Biurrun
1300 01f4895c Michael Niedermayer
                if(ost->st->codec->codec_type == CODEC_TYPE_AUDIO && enc->frame_size <=1)
1301 6f824977 Michael Niedermayer
                    continue;
1302 01f4895c Michael Niedermayer
                if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
1303 6f824977 Michael Niedermayer
                    continue;
1304
1305
                if (ost->encoding_needed) {
1306
                    for(;;) {
1307
                        AVPacket pkt;
1308 cef7cc72 Justin Ruggles
                        int fifo_bytes;
1309 6f824977 Michael Niedermayer
                        av_init_packet(&pkt);
1310
                        pkt.stream_index= ost->index;
1311 115329f1 Diego Biurrun
1312 01f4895c Michael Niedermayer
                        switch(ost->st->codec->codec_type) {
1313 115329f1 Diego Biurrun
                        case CODEC_TYPE_AUDIO:
1314 f5a478f6 Roman Shaposhnik
                            fifo_bytes = av_fifo_size(&ost->fifo);
1315 cef7cc72 Justin Ruggles
                            ret = 0;
1316
                            /* encode any samples remaining in fifo */
1317
                            if(fifo_bytes > 0 && enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1318
                                int fs_tmp = enc->frame_size;
1319
                                enc->frame_size = fifo_bytes / (2 * enc->channels);
1320 f5a478f6 Roman Shaposhnik
                                if(av_fifo_read(&ost->fifo, (uint8_t *)samples, fifo_bytes) == 0) {
1321 cef7cc72 Justin Ruggles
                                    ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, samples);
1322
                                }
1323
                                enc->frame_size = fs_tmp;
1324 9e0db7d5 Michael Niedermayer
                            }
1325
                            if(ret <= 0) {
1326 cef7cc72 Justin Ruggles
                                ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
1327
                            }
1328 6f824977 Michael Niedermayer
                            audio_size += ret;
1329
                            pkt.flags |= PKT_FLAG_KEY;
1330
                            break;
1331
                        case CODEC_TYPE_VIDEO:
1332 8a6cb114 Michael Niedermayer
                            ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
1333 6f824977 Michael Niedermayer
                            video_size += ret;
1334
                            if(enc->coded_frame && enc->coded_frame->key_frame)
1335
                                pkt.flags |= PKT_FLAG_KEY;
1336
                            if (ost->logfile && enc->stats_out) {
1337
                                fprintf(ost->logfile, "%s", enc->stats_out);
1338
                            }
1339
                            break;
1340
                        default:
1341
                            ret=-1;
1342
                        }
1343 115329f1 Diego Biurrun
1344 6f824977 Michael Niedermayer
                        if(ret<=0)
1345
                            break;
1346 27537106 Michael Niedermayer
                        pkt.data= bit_buffer;
1347 6f824977 Michael Niedermayer
                        pkt.size= ret;
1348 e7902f20 Michael Niedermayer
                        if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
1349 c0df9d75 Michael Niedermayer
                            pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
1350 748c2fca Michael Niedermayer
                        write_frame(os, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
1351 6f824977 Michael Niedermayer
                    }
1352
                }
1353
            }
1354
        }
1355
    }
1356 115329f1 Diego Biurrun
1357 a700a6ae Fabrice Bellard
    return 0;
1358
 fail_decode:
1359
    return -1;
1360
}
1361
1362
1363 85f07f22 Fabrice Bellard
/*
1364
 * The following code is the main loop of the file converter
1365
 */
1366
static int av_encode(AVFormatContext **output_files,
1367
                     int nb_output_files,
1368
                     AVFormatContext **input_files,
1369
                     int nb_input_files,
1370
                     AVStreamMap *stream_maps, int nb_stream_maps)
1371
{
1372 445f1b83 Roman Shaposhnik
    int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
1373 85f07f22 Fabrice Bellard
    AVFormatContext *is, *os;
1374
    AVCodecContext *codec, *icodec;
1375
    AVOutputStream *ost, **ost_table = NULL;
1376
    AVInputStream *ist, **ist_table = NULL;
1377 bdc4796f Fabrice Bellard
    AVInputFile *file_table;
1378 51bd4565 Philip Gladstone
    AVFormatContext *stream_no_data;
1379 cb09b2ed Philip Gladstone
    int key;
1380 bdc4796f Fabrice Bellard
1381 51bd4565 Philip Gladstone
    file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
1382 bdc4796f Fabrice Bellard
    if (!file_table)
1383
        goto fail;
1384 115329f1 Diego Biurrun
1385 85f07f22 Fabrice Bellard
    /* input stream init */
1386
    j = 0;
1387
    for(i=0;i<nb_input_files;i++) {
1388
        is = input_files[i];
1389
        file_table[i].ist_index = j;
1390 79fdaa4c Fabrice Bellard
        file_table[i].nb_streams = is->nb_streams;
1391 85f07f22 Fabrice Bellard
        j += is->nb_streams;
1392
    }
1393
    nb_istreams = j;
1394
1395
    ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
1396
    if (!ist_table)
1397 bdc4796f Fabrice Bellard
        goto fail;
1398 115329f1 Diego Biurrun
1399 85f07f22 Fabrice Bellard
    for(i=0;i<nb_istreams;i++) {
1400
        ist = av_mallocz(sizeof(AVInputStream));
1401
        if (!ist)
1402
            goto fail;
1403
        ist_table[i] = ist;
1404
    }
1405
    j = 0;
1406
    for(i=0;i<nb_input_files;i++) {
1407
        is = input_files[i];
1408
        for(k=0;k<is->nb_streams;k++) {
1409
            ist = ist_table[j++];
1410
            ist->st = is->streams[k];
1411
            ist->file_index = i;
1412
            ist->index = k;
1413
            ist->discard = 1; /* the stream is discarded by default
1414
                                 (changed later) */
1415 bdfcbbed Max Krasnyansky
1416 01f4895c Michael Niedermayer
            if (ist->st->codec->rate_emu) {
1417 bdfcbbed Max Krasnyansky
                ist->start = av_gettime();
1418
                ist->frame = 0;
1419
            }
1420 85f07f22 Fabrice Bellard
        }
1421
    }
1422
1423
    /* output stream init */
1424
    nb_ostreams = 0;
1425
    for(i=0;i<nb_output_files;i++) {
1426
        os = output_files[i];
1427 8a7bde1c Baptiste Coudurier
        if (!os->nb_streams) {
1428
            fprintf(stderr, "Output file does not contain any stream\n");
1429
            exit(1);
1430
        }
1431 85f07f22 Fabrice Bellard
        nb_ostreams += os->nb_streams;
1432
    }
1433
    if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
1434
        fprintf(stderr, "Number of stream maps must match number of output streams\n");
1435
        exit(1);
1436
    }
1437
1438 bd073980 Brian Foley
    /* Sanity check the mapping args -- do the input files & streams exist? */
1439
    for(i=0;i<nb_stream_maps;i++) {
1440
        int fi = stream_maps[i].file_index;
1441
        int si = stream_maps[i].stream_index;
1442 115329f1 Diego Biurrun
1443 bd073980 Brian Foley
        if (fi < 0 || fi > nb_input_files - 1 ||
1444
            si < 0 || si > file_table[fi].nb_streams - 1) {
1445
            fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
1446
            exit(1);
1447
        }
1448 b4a3389e Wolfram Gloger
        fi = stream_maps[i].sync_file_index;
1449
        si = stream_maps[i].sync_stream_index;
1450
        if (fi < 0 || fi > nb_input_files - 1 ||
1451
            si < 0 || si > file_table[fi].nb_streams - 1) {
1452
            fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
1453
            exit(1);
1454
        }
1455 bd073980 Brian Foley
    }
1456 115329f1 Diego Biurrun
1457 85f07f22 Fabrice Bellard
    ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
1458
    if (!ost_table)
1459
        goto fail;
1460
    for(i=0;i<nb_ostreams;i++) {
1461
        ost = av_mallocz(sizeof(AVOutputStream));
1462
        if (!ost)
1463
            goto fail;
1464
        ost_table[i] = ost;
1465
    }
1466 115329f1 Diego Biurrun
1467 85f07f22 Fabrice Bellard
    n = 0;
1468
    for(k=0;k<nb_output_files;k++) {
1469
        os = output_files[k];
1470
        for(i=0;i<os->nb_streams;i++) {
1471
            int found;
1472
            ost = ost_table[n++];
1473
            ost->file_index = k;
1474
            ost->index = i;
1475
            ost->st = os->streams[i];
1476
            if (nb_stream_maps > 0) {
1477 115329f1 Diego Biurrun
                ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
1478 85f07f22 Fabrice Bellard
                    stream_maps[n-1].stream_index;
1479 115329f1 Diego Biurrun
1480 bd073980 Brian Foley
                /* Sanity check that the stream types match */
1481 01f4895c Michael Niedermayer
                if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) {
1482 bd073980 Brian Foley
                    fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
1483
                        stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
1484
                        ost->file_index, ost->index);
1485
                    exit(1);
1486
                }
1487 115329f1 Diego Biurrun
1488 85f07f22 Fabrice Bellard
            } else {
1489
                /* get corresponding input stream index : we select the first one with the right type */
1490
                found = 0;
1491
                for(j=0;j<nb_istreams;j++) {
1492
                    ist = ist_table[j];
1493 115329f1 Diego Biurrun
                    if (ist->discard &&
1494 01f4895c Michael Niedermayer
                        ist->st->codec->codec_type == ost->st->codec->codec_type) {
1495 85f07f22 Fabrice Bellard
                        ost->source_index = j;
1496
                        found = 1;
1497 c19e76ce Nathan Kurz
                        break;
1498 85f07f22 Fabrice Bellard
                    }
1499
                }
1500 115329f1 Diego Biurrun
1501 85f07f22 Fabrice Bellard
                if (!found) {
1502
                    /* try again and reuse existing stream */
1503
                    for(j=0;j<nb_istreams;j++) {
1504
                        ist = ist_table[j];
1505 01f4895c Michael Niedermayer
                        if (ist->st->codec->codec_type == ost->st->codec->codec_type) {
1506 85f07f22 Fabrice Bellard
                            ost->source_index = j;
1507
                            found = 1;
1508
                        }
1509
                    }
1510
                    if (!found) {
1511
                        fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1512
                                ost->file_index, ost->index);
1513
                        exit(1);
1514
                    }
1515
                }
1516
            }
1517
            ist = ist_table[ost->source_index];
1518
            ist->discard = 0;
1519 b4a3389e Wolfram Gloger
            ost->sync_ist = (nb_stream_maps > 0) ?
1520
                ist_table[file_table[stream_maps[n-1].sync_file_index].ist_index +
1521
                         stream_maps[n-1].sync_stream_index] : ist;
1522 85f07f22 Fabrice Bellard
        }
1523
    }
1524
1525
    /* for each output stream, we compute the right encoding parameters */
1526
    for(i=0;i<nb_ostreams;i++) {
1527
        ost = ost_table[i];
1528
        ist = ist_table[ost->source_index];
1529
1530 01f4895c Michael Niedermayer
        codec = ost->st->codec;
1531
        icodec = ist->st->codec;
1532 85f07f22 Fabrice Bellard
1533 1629626f Fabrice Bellard
        if (ost->st->stream_copy) {
1534
            /* if stream_copy is selected, no need to decode or encode */
1535
            codec->codec_id = icodec->codec_id;
1536
            codec->codec_type = icodec->codec_type;
1537 b2a2197e Michael Niedermayer
            if(!codec->codec_tag) codec->codec_tag = icodec->codec_tag;
1538 1629626f Fabrice Bellard
            codec->bit_rate = icodec->bit_rate;
1539 dffbfd06 Michael Niedermayer
            codec->extradata= icodec->extradata;
1540
            codec->extradata_size= icodec->extradata_size;
1541 4408e75e Michael Niedermayer
            if(av_q2d(icodec->time_base) > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/1000)
1542 d29de719 Michael Niedermayer
                codec->time_base = icodec->time_base;
1543
            else
1544
                codec->time_base = ist->st->time_base;
1545 1629626f Fabrice Bellard
            switch(codec->codec_type) {
1546
            case CODEC_TYPE_AUDIO:
1547
                codec->sample_rate = icodec->sample_rate;
1548
                codec->channels = icodec->channels;
1549 0a3b0447 Michael Niedermayer
                codec->frame_size = icodec->frame_size;
1550 c1344911 Michael Niedermayer
                codec->block_align= icodec->block_align;
1551 1629626f Fabrice Bellard
                break;
1552
            case CODEC_TYPE_VIDEO:
1553 9df3437f Baptiste Coudurier
                codec->pix_fmt = icodec->pix_fmt;
1554 1629626f Fabrice Bellard
                codec->width = icodec->width;
1555
                codec->height = icodec->height;
1556 ff8cc24b Michael Niedermayer
                codec->has_b_frames = icodec->has_b_frames;
1557 1629626f Fabrice Bellard
                break;
1558 cf7fc795 Fabrice Bellard
            case CODEC_TYPE_SUBTITLE:
1559
                break;
1560 1629626f Fabrice Bellard
            default:
1561
                av_abort();
1562
            }
1563
        } else {
1564
            switch(codec->codec_type) {
1565
            case CODEC_TYPE_AUDIO:
1566 f5a478f6 Roman Shaposhnik
                if (av_fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1567 85f07f22 Fabrice Bellard
                    goto fail;
1568 115329f1 Diego Biurrun
1569 85f07f22 Fabrice Bellard
                if (codec->channels == icodec->channels &&
1570
                    codec->sample_rate == icodec->sample_rate) {
1571
                    ost->audio_resample = 0;
1572
                } else {
1573 e0d2714a Juanjo
                    if (codec->channels != icodec->channels &&
1574 23c99253 Michael Niedermayer
                        (icodec->codec_id == CODEC_ID_AC3 ||
1575
                         icodec->codec_id == CODEC_ID_DTS)) {
1576
                        /* Special case for 5:1 AC3 and DTS input */
1577 e0d2714a Juanjo
                        /* and mono or stereo output      */
1578 6dc96cb0 Juanjo
                        /* Request specific number of channels */
1579
                        icodec->channels = codec->channels;
1580
                        if (codec->sample_rate == icodec->sample_rate)
1581
                            ost->audio_resample = 0;
1582
                        else {
1583
                            ost->audio_resample = 1;
1584
                        }
1585 e0d2714a Juanjo
                    } else {
1586 115329f1 Diego Biurrun
                        ost->audio_resample = 1;
1587 986ebcdb Michael Niedermayer
                    }
1588
                }
1589 ff4905a5 Michael Niedermayer
                if(audio_sync_method>1)
1590 986ebcdb Michael Niedermayer
                    ost->audio_resample = 1;
1591
1592
                if(ost->audio_resample){
1593
                    ost->resample = audio_resample_init(codec->channels, icodec->channels,
1594
                                                    codec->sample_rate, icodec->sample_rate);
1595
                    if(!ost->resample){
1596
                        printf("Can't resample.  Aborting.\n");
1597
                        av_abort();
1598 e0d2714a Juanjo
                    }
1599 85f07f22 Fabrice Bellard
                }
1600
                ist->decoding_needed = 1;
1601
                ost->encoding_needed = 1;
1602 1629626f Fabrice Bellard
                break;
1603
            case CODEC_TYPE_VIDEO:
1604 c3f11d19 Luca Abeni
                ost->video_crop = ((frame_leftBand + frame_rightBand + frame_topBand + frame_bottomBand) != 0);
1605
                ost->video_pad = ((frame_padleft + frame_padright + frame_padtop + frame_padbottom) != 0);
1606
                ost->video_resample = ((codec->width != icodec->width -
1607
                                (frame_leftBand + frame_rightBand) +
1608
                                (frame_padleft + frame_padright)) ||
1609
                        (codec->height != icodec->height -
1610
                                (frame_topBand  + frame_bottomBand) +
1611 18a54b04 Luca Abeni
                                (frame_padtop + frame_padbottom)) ||
1612
                        (codec->pix_fmt != icodec->pix_fmt));
1613 c3f11d19 Luca Abeni
                if (ost->video_crop) {
1614 34b10a57 Dieter
                    ost->topBand = frame_topBand;
1615
                    ost->leftBand = frame_leftBand;
1616 c3f11d19 Luca Abeni
                }
1617
                if (ost->video_pad) {
1618 1ff93ffc Todd Kirby
                    ost->padtop = frame_padtop;
1619
                    ost->padleft = frame_padleft;
1620
                    ost->padbottom = frame_padbottom;
1621
                    ost->padright = frame_padright;
1622 c3f11d19 Luca Abeni
                    if (!ost->video_resample) {
1623
                        avcodec_get_frame_defaults(&ost->pict_tmp);
1624
                        if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, codec->pix_fmt,
1625
                                         codec->width, codec->height ) )
1626
                            goto fail;
1627
                    }
1628
                }
1629
                if (ost->video_resample) {
1630 2de28abb Michael Niedermayer
                    avcodec_get_frame_defaults(&ost->pict_tmp);
1631 18a54b04 Luca Abeni
                    if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, codec->pix_fmt,
1632 2caa92d9 Michael Niedermayer
                                         codec->width, codec->height ) )
1633 85f07f22 Fabrice Bellard
                        goto fail;
1634
1635 18a54b04 Luca Abeni
                    ost->img_resample_ctx = sws_getContext(
1636
                            icodec->width - (frame_leftBand + frame_rightBand),
1637
                            icodec->height - (frame_topBand + frame_bottomBand),
1638
                            icodec->pix_fmt,
1639 07d0cdfc Luca Abeni
                            codec->width - (frame_padleft + frame_padright),
1640
                            codec->height - (frame_padtop + frame_padbottom),
1641 18a54b04 Luca Abeni
                            codec->pix_fmt,
1642
                            sws_flags, NULL, NULL, NULL);
1643 0b50ac8a Luca Abeni
                    if (ost->img_resample_ctx == NULL) {
1644
                        fprintf(stderr, "Cannot get resampling context\n");
1645
                        exit(1);
1646
                    }
1647 18a54b04 Luca Abeni
                    ost->resample_height = icodec->height - (frame_topBand + frame_bottomBand);
1648 85f07f22 Fabrice Bellard
                }
1649
                ost->encoding_needed = 1;
1650
                ist->decoding_needed = 1;
1651 1629626f Fabrice Bellard
                break;
1652 cf7fc795 Fabrice Bellard
            case CODEC_TYPE_SUBTITLE:
1653
                ost->encoding_needed = 1;
1654
                ist->decoding_needed = 1;
1655
                break;
1656 1629626f Fabrice Bellard
            default:
1657
                av_abort();
1658 cf7fc795 Fabrice Bellard
                break;
1659 85f07f22 Fabrice Bellard
            }
1660 1629626f Fabrice Bellard
            /* two pass mode */
1661 115329f1 Diego Biurrun
            if (ost->encoding_needed &&
1662 1629626f Fabrice Bellard
                (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1663
                char logfilename[1024];
1664
                FILE *f;
1665
                int size;
1666
                char *logbuffer;
1667 115329f1 Diego Biurrun
1668
                snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1669
                         pass_logfilename ?
1670 1629626f Fabrice Bellard
                         pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1671
                if (codec->flags & CODEC_FLAG_PASS1) {
1672
                    f = fopen(logfilename, "w");
1673
                    if (!f) {
1674
                        perror(logfilename);
1675
                        exit(1);
1676
                    }
1677
                    ost->logfile = f;
1678
                } else {
1679
                    /* read the log file */
1680
                    f = fopen(logfilename, "r");
1681
                    if (!f) {
1682
                        perror(logfilename);
1683
                        exit(1);
1684
                    }
1685
                    fseek(f, 0, SEEK_END);
1686
                    size = ftell(f);
1687
                    fseek(f, 0, SEEK_SET);
1688
                    logbuffer = av_malloc(size + 1);
1689
                    if (!logbuffer) {
1690
                        fprintf(stderr, "Could not allocate log buffer\n");
1691
                        exit(1);
1692
                    }
1693 4776fa92 Gael Chardon
                    size = fread(logbuffer, 1, size, f);
1694 1629626f Fabrice Bellard
                    fclose(f);
1695
                    logbuffer[size] = '\0';
1696
                    codec->stats_in = logbuffer;
1697 5abdb4b1 Fabrice Bellard
                }
1698
            }
1699
        }
1700 8a6cb114 Michael Niedermayer
        if(codec->codec_type == CODEC_TYPE_VIDEO){
1701
            int size= codec->width * codec->height;
1702
            bit_buffer_size= FFMAX(bit_buffer_size, 4*size);
1703
        }
1704 85f07f22 Fabrice Bellard
    }
1705
1706 8a6cb114 Michael Niedermayer
    if (!bit_buffer)
1707
        bit_buffer = av_malloc(bit_buffer_size);
1708
    if (!bit_buffer)
1709
        goto fail;
1710
1711 1629626f Fabrice Bellard
    /* dump the file output parameters - cannot be done before in case
1712
       of stream copy */
1713
    for(i=0;i<nb_output_files;i++) {
1714
        dump_format(output_files[i], i, output_files[i]->filename, 1);
1715
    }
1716
1717
    /* dump the stream mapping */
1718 d8019eb5 Allen Day
    if (verbose >= 0) {
1719
        fprintf(stderr, "Stream mapping:\n");
1720
        for(i=0;i<nb_ostreams;i++) {
1721
            ost = ost_table[i];
1722 b4a3389e Wolfram Gloger
            fprintf(stderr, "  Stream #%d.%d -> #%d.%d",
1723 d8019eb5 Allen Day
                    ist_table[ost->source_index]->file_index,
1724
                    ist_table[ost->source_index]->index,
1725 115329f1 Diego Biurrun
                    ost->file_index,
1726 d8019eb5 Allen Day
                    ost->index);
1727 b4a3389e Wolfram Gloger
            if (ost->sync_ist != ist_table[ost->source_index])
1728
                fprintf(stderr, " [sync #%d.%d]",
1729
                        ost->sync_ist->file_index,
1730
                        ost->sync_ist->index);
1731
            fprintf(stderr, "\n");
1732 d8019eb5 Allen Day
        }
1733 1629626f Fabrice Bellard
    }
1734
1735 85f07f22 Fabrice Bellard
    /* open each encoder */
1736
    for(i=0;i<nb_ostreams;i++) {
1737
        ost = ost_table[i];
1738
        if (ost->encoding_needed) {
1739
            AVCodec *codec;
1740 01f4895c Michael Niedermayer
            codec = avcodec_find_encoder(ost->st->codec->codec_id);
1741 85f07f22 Fabrice Bellard
            if (!codec) {
1742 115329f1 Diego Biurrun
                fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1743 85f07f22 Fabrice Bellard
                        ost->file_index, ost->index);
1744
                exit(1);
1745
            }
1746 01f4895c Michael Niedermayer
            if (avcodec_open(ost->st->codec, codec) < 0) {
1747 115329f1 Diego Biurrun
                fprintf(stderr, "Error while opening codec for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1748 85f07f22 Fabrice Bellard
                        ost->file_index, ost->index);
1749
                exit(1);
1750
            }
1751 01f4895c Michael Niedermayer
            extra_size += ost->st->codec->extradata_size;
1752 85f07f22 Fabrice Bellard
        }
1753
    }
1754
1755
    /* open each decoder */
1756
    for(i=0;i<nb_istreams;i++) {
1757
        ist = ist_table[i];
1758
        if (ist->decoding_needed) {
1759
            AVCodec *codec;
1760 01f4895c Michael Niedermayer
            codec = avcodec_find_decoder(ist->st->codec->codec_id);
1761 85f07f22 Fabrice Bellard
            if (!codec) {
1762 115329f1 Diego Biurrun
                fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1763 01f4895c Michael Niedermayer
                        ist->st->codec->codec_id, ist->file_index, ist->index);
1764 85f07f22 Fabrice Bellard
                exit(1);
1765
            }
1766 01f4895c Michael Niedermayer
            if (avcodec_open(ist->st->codec, codec) < 0) {
1767 115329f1 Diego Biurrun
                fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1768 85f07f22 Fabrice Bellard
                        ist->file_index, ist->index);
1769
                exit(1);
1770
            }
1771 01f4895c Michael Niedermayer
            //if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO)
1772
            //    ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
1773 85f07f22 Fabrice Bellard
        }
1774
    }
1775
1776
    /* init pts */
1777
    for(i=0;i<nb_istreams;i++) {
1778
        ist = ist_table[i];
1779 bb270c08 Diego Biurrun
        is = input_files[ist->file_index];
1780 e7d0374f Roman Shaposhnik
        ist->pts = 0;
1781 c0df9d75 Michael Niedermayer
        ist->next_pts = av_rescale_q(ist->st->start_time, ist->st->time_base, AV_TIME_BASE_Q);
1782 115329f1 Diego Biurrun
        if(ist->st->start_time == AV_NOPTS_VALUE)
1783 bd1b79a1 Michael Niedermayer
            ist->next_pts=0;
1784 fdb86eb1 Michael Niedermayer
        if(input_files_ts_offset[ist->file_index])
1785
            ist->next_pts= AV_NOPTS_VALUE;
1786 ff4905a5 Michael Niedermayer
        ist->is_start = 1;
1787 85f07f22 Fabrice Bellard
    }
1788 bd1b79a1 Michael Niedermayer
1789 85f07f22 Fabrice Bellard
    /* compute buffer size max (should use a complete heuristic) */
1790
    for(i=0;i<nb_input_files;i++) {
1791
        file_table[i].buffer_size_max = 2048;
1792
    }
1793
1794 0a38bafd Patrice Bensoussan
    /* set meta data information from input file if required */
1795
    for (i=0;i<nb_meta_data_maps;i++) {
1796
        AVFormatContext *out_file;
1797
        AVFormatContext *in_file;
1798
1799
        int out_file_index = meta_data_maps[i].out_file;
1800
        int in_file_index = meta_data_maps[i].in_file;
1801
        if ( out_file_index < 0 || out_file_index >= nb_output_files ) {
1802
            fprintf(stderr, "Invalid output file index %d map_meta_data(%d,%d)\n", out_file_index, out_file_index, in_file_index);
1803
            ret = -EINVAL;
1804
            goto fail;
1805
        }
1806
        if ( in_file_index < 0 || in_file_index >= nb_input_files ) {
1807
            fprintf(stderr, "Invalid input file index %d map_meta_data(%d,%d)\n", in_file_index, out_file_index, in_file_index);
1808
            ret = -EINVAL;
1809
            goto fail;
1810 115329f1 Diego Biurrun
        }
1811
1812 0a38bafd Patrice Bensoussan
        out_file = output_files[out_file_index];
1813
        in_file = input_files[in_file_index];
1814
1815
        strcpy(out_file->title, in_file->title);
1816
        strcpy(out_file->author, in_file->author);
1817
        strcpy(out_file->copyright, in_file->copyright);
1818
        strcpy(out_file->comment, in_file->comment);
1819
        strcpy(out_file->album, in_file->album);
1820
        out_file->year = in_file->year;
1821
        out_file->track = in_file->track;
1822
        strcpy(out_file->genre, in_file->genre);
1823
    }
1824 115329f1 Diego Biurrun
1825 85f07f22 Fabrice Bellard
    /* open files and write file headers */
1826
    for(i=0;i<nb_output_files;i++) {
1827
        os = output_files[i];
1828 79fdaa4c Fabrice Bellard
        if (av_write_header(os) < 0) {
1829 65bf3c53 Michael Niedermayer
            fprintf(stderr, "Could not write header for output file #%d (incorrect codec parameters ?)\n", i);
1830 a38469e1 Fabrice Bellard
            ret = -EINVAL;
1831
            goto fail;
1832
        }
1833 85f07f22 Fabrice Bellard
    }
1834
1835 2f30a81d Diego Biurrun
#ifndef __MINGW32__
1836 d8019eb5 Allen Day
    if ( !using_stdin && verbose >= 0) {
1837 d9a916e2 Charles Yates
        fprintf(stderr, "Press [q] to stop encoding\n");
1838 b51469a0 Leon van Stuivenberg
        url_set_interrupt_cb(decode_interrupt_cb);
1839
    }
1840 a38469e1 Fabrice Bellard
#endif
1841
    term_init();
1842
1843 51bd4565 Philip Gladstone
    stream_no_data = 0;
1844 cb09b2ed Philip Gladstone
    key = -1;
1845 51bd4565 Philip Gladstone
1846 d9a916e2 Charles Yates
    for(; received_sigterm == 0;) {
1847 85f07f22 Fabrice Bellard
        int file_index, ist_index;
1848
        AVPacket pkt;
1849 a9aeda81 Patrice Bensoussan
        double ipts_min;
1850
        double opts_min;
1851 23ffe323 Michael Niedermayer
1852 85f07f22 Fabrice Bellard
    redo:
1853 a9aeda81 Patrice Bensoussan
        ipts_min= 1e100;
1854
        opts_min= 1e100;
1855 a38469e1 Fabrice Bellard
        /* if 'q' pressed, exits */
1856 d9a916e2 Charles Yates
        if (!using_stdin) {
1857 b51469a0 Leon van Stuivenberg
            if (q_pressed)
1858
                break;
1859 cb09b2ed Philip Gladstone
            /* read_key() returns 0 on EOF */
1860
            key = read_key();
1861
            if (key == 'q')
1862
                break;
1863
        }
1864 a38469e1 Fabrice Bellard
1865 ec5517d5 Fabrice Bellard
        /* select the stream that we must read now by looking at the
1866
           smallest output pts */
1867 85f07f22 Fabrice Bellard
        file_index = -1;
1868 ec5517d5 Fabrice Bellard
        for(i=0;i<nb_ostreams;i++) {
1869 23ffe323 Michael Niedermayer
            double ipts, opts;
1870 ec5517d5 Fabrice Bellard
            ost = ost_table[i];
1871
            os = output_files[ost->file_index];
1872
            ist = ist_table[ost->source_index];
1873 01f4895c Michael Niedermayer
            if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO)
1874
                opts = ost->sync_opts * av_q2d(ost->st->codec->time_base);
1875 23ffe323 Michael Niedermayer
            else
1876 c0df9d75 Michael Niedermayer
                opts = ost->st->pts.val * av_q2d(ost->st->time_base);
1877 23ffe323 Michael Niedermayer
            ipts = (double)ist->pts;
1878
            if (!file_table[ist->file_index].eof_reached){
1879
                if(ipts < ipts_min) {
1880
                    ipts_min = ipts;
1881
                    if(input_sync ) file_index = ist->file_index;
1882
                }
1883
                if(opts < opts_min) {
1884
                    opts_min = opts;
1885
                    if(!input_sync) file_index = ist->file_index;
1886
                }
1887 85f07f22 Fabrice Bellard
            }
1888 01f4895c Michael Niedermayer
            if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
1889 7ca60994 Michael Niedermayer
                file_index= -1;
1890
                break;
1891
            }
1892 85f07f22 Fabrice Bellard
        }
1893
        /* if none, if is finished */
1894 51bd4565 Philip Gladstone
        if (file_index < 0) {
1895 85f07f22 Fabrice Bellard
            break;
1896 ec5517d5 Fabrice Bellard
        }
1897
1898 85f07f22 Fabrice Bellard
        /* finish if recording time exhausted */
1899 23ffe323 Michael Niedermayer
        if (recording_time > 0 && opts_min >= (recording_time / 1000000.0))
1900 85f07f22 Fabrice Bellard
            break;
1901 ec5517d5 Fabrice Bellard
1902 b6e16b86 Calcium
        /* finish if limit size exhausted */
1903
        if (limit_filesize != 0 && (limit_filesize * 1024) < url_ftell(&output_files[0]->pb))
1904
            break;
1905
1906 254abc2e Fabrice Bellard
        /* read a frame from it and output it in the fifo */
1907 85f07f22 Fabrice Bellard
        is = input_files[file_index];
1908 254abc2e Fabrice Bellard
        if (av_read_frame(is, &pkt) < 0) {
1909 85f07f22 Fabrice Bellard
            file_table[file_index].eof_reached = 1;
1910 ca80810b Calcium
            if (opt_shortest) break; else continue; //
1911 85f07f22 Fabrice Bellard
        }
1912 303e50e6 Michael Niedermayer
1913 51bd4565 Philip Gladstone
        if (!pkt.size) {
1914
            stream_no_data = is;
1915
        } else {
1916
            stream_no_data = 0;
1917
        }
1918 254abc2e Fabrice Bellard
        if (do_pkt_dump) {
1919
            av_pkt_dump(stdout, &pkt, do_hex_dump);
1920 817b23ff Fabrice Bellard
        }
1921 79fdaa4c Fabrice Bellard
        /* the following test is needed in case new streams appear
1922
           dynamically in stream : we ignore them */
1923
        if (pkt.stream_index >= file_table[file_index].nb_streams)
1924 bf5af568 Fabrice Bellard
            goto discard_packet;
1925 85f07f22 Fabrice Bellard
        ist_index = file_table[file_index].ist_index + pkt.stream_index;
1926
        ist = ist_table[ist_index];
1927 bf5af568 Fabrice Bellard
        if (ist->discard)
1928
            goto discard_packet;
1929 85f07f22 Fabrice Bellard
1930 949b1a13 Steve L'Homme
//        fprintf(stderr, "next:%"PRId64" dts:%"PRId64" off:%"PRId64" %d\n", ist->next_pts, pkt.dts, input_files_ts_offset[ist->file_index], ist->st->codec->codec_type);
1931 fdb86eb1 Michael Niedermayer
        if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE) {
1932 c0df9d75 Michael Niedermayer
            int64_t delta= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q) - ist->next_pts;
1933 c26abfa5 Diego Biurrun
            if(FFABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE && !copy_ts){
1934 72bd8100 Michael Niedermayer
                input_files_ts_offset[ist->file_index]-= delta;
1935
                if (verbose > 2)
1936 4733abcb Måns Rullgård
                    fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", delta, input_files_ts_offset[ist->file_index]);
1937 72bd8100 Michael Niedermayer
                for(i=0; i<file_table[file_index].nb_streams; i++){
1938
                    int index= file_table[file_index].ist_index + i;
1939
                    ist_table[index]->next_pts += delta;
1940 ff4905a5 Michael Niedermayer
                    ist_table[index]->is_start=1;
1941 72bd8100 Michael Niedermayer
                }
1942
            }
1943
        }
1944
1945 8831db5c Michael Niedermayer
        //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1946 a700a6ae Fabrice Bellard
        if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
1947 d8019eb5 Allen Day
1948
            if (verbose >= 0)
1949
                fprintf(stderr, "Error while decoding stream #%d.%d\n",
1950
                        ist->file_index, ist->index);
1951
1952 a700a6ae Fabrice Bellard
            av_free_packet(&pkt);
1953
            goto redo;
1954 fe08925f Michael Niedermayer
        }
1955 cf7fc795 Fabrice Bellard
1956 bf5af568 Fabrice Bellard
    discard_packet:
1957 85f07f22 Fabrice Bellard
        av_free_packet(&pkt);
1958 115329f1 Diego Biurrun
1959 ec5517d5 Fabrice Bellard
        /* dump report by using the output first video and audio streams */
1960
        print_report(output_files, ost_table, nb_ostreams, 0);
1961 85f07f22 Fabrice Bellard
    }
1962 a700a6ae Fabrice Bellard
1963
    /* at the end of stream, we must flush the decoder buffers */
1964
    for(i=0;i<nb_istreams;i++) {
1965
        ist = ist_table[i];
1966
        if (ist->decoding_needed) {
1967
            output_packet(ist, i, ost_table, nb_ostreams, NULL);
1968
        }
1969
    }
1970
1971 a38469e1 Fabrice Bellard
    term_exit();
1972 85f07f22 Fabrice Bellard
1973 c4e37247 Michael Niedermayer
    /* write the trailer if needed and close file */
1974
    for(i=0;i<nb_output_files;i++) {
1975
        os = output_files[i];
1976
        av_write_trailer(os);
1977
    }
1978
1979 37f5cd5a Michael Niedermayer
    /* dump report by using the first video and audio streams */
1980
    print_report(output_files, ost_table, nb_ostreams, 1);
1981
1982 85f07f22 Fabrice Bellard
    /* close each encoder */
1983
    for(i=0;i<nb_ostreams;i++) {
1984
        ost = ost_table[i];
1985
        if (ost->encoding_needed) {
1986 01f4895c Michael Niedermayer
            av_freep(&ost->st->codec->stats_in);
1987
            avcodec_close(ost->st->codec);
1988 85f07f22 Fabrice Bellard
        }
1989
    }
1990 115329f1 Diego Biurrun
1991 85f07f22 Fabrice Bellard
    /* close each decoder */
1992
    for(i=0;i<nb_istreams;i++) {
1993
        ist = ist_table[i];
1994
        if (ist->decoding_needed) {
1995 01f4895c Michael Niedermayer
            avcodec_close(ist->st->codec);
1996 85f07f22 Fabrice Bellard
        }
1997
    }
1998
1999
    /* finished ! */
2000 115329f1 Diego Biurrun
2001 85f07f22 Fabrice Bellard
    ret = 0;
2002
 fail1:
2003 83b07470 Michael Niedermayer
    av_freep(&bit_buffer);
2004 0f1578af Fabrice Bellard
    av_free(file_table);
2005 bdc4796f Fabrice Bellard
2006 85f07f22 Fabrice Bellard
    if (ist_table) {
2007
        for(i=0;i<nb_istreams;i++) {
2008
            ist = ist_table[i];
2009 0f1578af Fabrice Bellard
            av_free(ist);
2010 85f07f22 Fabrice Bellard
        }
2011 0f1578af Fabrice Bellard
        av_free(ist_table);
2012 85f07f22 Fabrice Bellard
    }
2013
    if (ost_table) {
2014
        for(i=0;i<nb_ostreams;i++) {
2015
            ost = ost_table[i];
2016
            if (ost) {
2017 5abdb4b1 Fabrice Bellard
                if (ost->logfile) {
2018
                    fclose(ost->logfile);
2019
                    ost->logfile = NULL;
2020
                }
2021 f5a478f6 Roman Shaposhnik
                av_fifo_free(&ost->fifo); /* works even if fifo is not
2022
                                             initialized but set to zero */
2023 0f1578af Fabrice Bellard
                av_free(ost->pict_tmp.data[0]);
2024 85f07f22 Fabrice Bellard
                if (ost->video_resample)
2025 18a54b04 Luca Abeni
                    sws_freeContext(ost->img_resample_ctx);
2026 85f07f22 Fabrice Bellard
                if (ost->audio_resample)
2027
                    audio_resample_close(ost->resample);
2028 0f1578af Fabrice Bellard
                av_free(ost);
2029 85f07f22 Fabrice Bellard
            }
2030
        }
2031 0f1578af Fabrice Bellard
        av_free(ost_table);
2032 85f07f22 Fabrice Bellard
    }
2033
    return ret;
2034
 fail:
2035
    ret = -ENOMEM;
2036
    goto fail1;
2037
}
2038
2039
#if 0
2040
int file_read(const char *filename)
2041
{
2042
    URLContext *h;
2043
    unsigned char buffer[1024];
2044
    int len, i;
2045

2046
    if (url_open(&h, filename, O_RDONLY) < 0) {
2047
        printf("could not open '%s'\n", filename);
2048
        return -1;
2049
    }
2050
    for(;;) {
2051
        len = url_read(h, buffer, sizeof(buffer));
2052
        if (len <= 0)
2053
            break;
2054
        for(i=0;i<len;i++) putchar(buffer[i]);
2055
    }
2056
    url_close(h);
2057
    return 0;
2058
}
2059
#endif
2060
2061 b29f97d1 Zdenek Kabelac
static void opt_format(const char *arg)
2062 85f07f22 Fabrice Bellard
{
2063 817b23ff Fabrice Bellard
    /* compatibility stuff for pgmyuv */
2064
    if (!strcmp(arg, "pgmyuv")) {
2065 5b6d5596 Michael Niedermayer
        pgmyuv_compatibility_hack=1;
2066 9286699a Michael Niedermayer
//        opt_image_format(arg);
2067 5b6d5596 Michael Niedermayer
        arg = "image2";
2068 817b23ff Fabrice Bellard
    }
2069
2070 79fdaa4c Fabrice Bellard
    file_iformat = av_find_input_format(arg);
2071
    file_oformat = guess_format(arg, NULL, NULL);
2072
    if (!file_iformat && !file_oformat) {
2073
        fprintf(stderr, "Unknown input or output format: %s\n", arg);
2074 85f07f22 Fabrice Bellard
        exit(1);
2075
    }
2076
}
2077
2078 b29f97d1 Zdenek Kabelac
static void opt_video_rc_eq(char *arg)
2079 3aa102be Michael Niedermayer
{
2080
    video_rc_eq = arg;
2081
}
2082
2083 b29f97d1 Zdenek Kabelac
static void opt_video_rc_override_string(char *arg)
2084 ac2830ec Michael Niedermayer
{
2085
    video_rc_override_string = arg;
2086
}
2087
2088 f4f3223f Michael Niedermayer
static void opt_me_threshold(const char *arg)
2089
{
2090
    me_threshold = atoi(arg);
2091
}
2092
2093 f068206e Bill Eldridge
static void opt_verbose(const char *arg)
2094
{
2095
    verbose = atoi(arg);
2096 d8019eb5 Allen Day
    av_log_set_level(atoi(arg));
2097 f068206e Bill Eldridge
}
2098
2099 b29f97d1 Zdenek Kabelac
static void opt_frame_rate(const char *arg)
2100 85f07f22 Fabrice Bellard
{
2101 445f1b83 Roman Shaposhnik
    if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
2102
        fprintf(stderr, "Incorrect frame rate\n");
2103 bb270c08 Diego Biurrun
        exit(1);
2104 445f1b83 Roman Shaposhnik
    }
2105 85f07f22 Fabrice Bellard
}
2106
2107 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_top(const char *arg)
2108 ab6d194a Michael Niedermayer
{
2109 115329f1 Diego Biurrun
    frame_topBand = atoi(arg);
2110 ab6d194a Michael Niedermayer
    if (frame_topBand < 0) {
2111
        fprintf(stderr, "Incorrect top crop size\n");
2112
        exit(1);
2113
    }
2114
    if ((frame_topBand % 2) != 0) {
2115
        fprintf(stderr, "Top crop size must be a multiple of 2\n");
2116
        exit(1);
2117
    }
2118
    if ((frame_topBand) >= frame_height){
2119 bb270c08 Diego Biurrun
        fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2120 ab6d194a Michael Niedermayer
        exit(1);
2121
    }
2122
    frame_height -= frame_topBand;
2123
}
2124
2125 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_bottom(const char *arg)
2126 ab6d194a Michael Niedermayer
{
2127
    frame_bottomBand = atoi(arg);
2128
    if (frame_bottomBand < 0) {
2129
        fprintf(stderr, "Incorrect bottom crop size\n");
2130
        exit(1);
2131
    }
2132
    if ((frame_bottomBand % 2) != 0) {
2133
        fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
2134 115329f1 Diego Biurrun
        exit(1);
2135 ab6d194a Michael Niedermayer
    }
2136
    if ((frame_bottomBand) >= frame_height){
2137 bb270c08 Diego Biurrun
        fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2138 ab6d194a Michael Niedermayer
        exit(1);
2139
    }
2140
    frame_height -= frame_bottomBand;
2141
}
2142
2143 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_left(const char *arg)
2144 ab6d194a Michael Niedermayer
{
2145
    frame_leftBand = atoi(arg);
2146
    if (frame_leftBand < 0) {
2147
        fprintf(stderr, "Incorrect left crop size\n");
2148
        exit(1);
2149
    }
2150
    if ((frame_leftBand % 2) != 0) {
2151
        fprintf(stderr, "Left crop size must be a multiple of 2\n");
2152
        exit(1);
2153
    }
2154
    if ((frame_leftBand) >= frame_width){
2155 bb270c08 Diego Biurrun
        fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2156 ab6d194a Michael Niedermayer
        exit(1);
2157
    }
2158
    frame_width -= frame_leftBand;
2159
}
2160
2161 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_right(const char *arg)
2162 ab6d194a Michael Niedermayer
{
2163
    frame_rightBand = atoi(arg);
2164
    if (frame_rightBand < 0) {
2165
        fprintf(stderr, "Incorrect right crop size\n");
2166
        exit(1);
2167
    }
2168
    if ((frame_rightBand % 2) != 0) {
2169
        fprintf(stderr, "Right crop size must be a multiple of 2\n");
2170 115329f1 Diego Biurrun
        exit(1);
2171 ab6d194a Michael Niedermayer
    }
2172
    if ((frame_rightBand) >= frame_width){
2173 bb270c08 Diego Biurrun
        fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2174 ab6d194a Michael Niedermayer
        exit(1);
2175
    }
2176
    frame_width -= frame_rightBand;
2177
}
2178
2179 b29f97d1 Zdenek Kabelac
static void opt_frame_size(const char *arg)
2180 85f07f22 Fabrice Bellard
{
2181 445f1b83 Roman Shaposhnik
    if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
2182 85f07f22 Fabrice Bellard
        fprintf(stderr, "Incorrect frame size\n");
2183
        exit(1);
2184
    }
2185
    if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
2186
        fprintf(stderr, "Frame size must be a multiple of 2\n");
2187
        exit(1);
2188
    }
2189
}
2190
2191 1ff93ffc Todd Kirby
2192
#define SCALEBITS 10
2193
#define ONE_HALF  (1 << (SCALEBITS - 1))
2194 bb270c08 Diego Biurrun
#define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
2195 1ff93ffc Todd Kirby
2196
#define RGB_TO_Y(r, g, b) \
2197
((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
2198
  FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
2199
2200
#define RGB_TO_U(r1, g1, b1, shift)\
2201
(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +         \
2202
     FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2203
2204
#define RGB_TO_V(r1, g1, b1, shift)\
2205
(((FIX(0.50000) * r1 - FIX(0.41869) * g1 -           \
2206
   FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2207
2208
static void opt_pad_color(const char *arg) {
2209
    /* Input is expected to be six hex digits similar to
2210
       how colors are expressed in html tags (but without the #) */
2211
    int rgb = strtol(arg, NULL, 16);
2212
    int r,g,b;
2213 115329f1 Diego Biurrun
2214
    r = (rgb >> 16);
2215 1ff93ffc Todd Kirby
    g = ((rgb >> 8) & 255);
2216
    b = (rgb & 255);
2217
2218
    padcolor[0] = RGB_TO_Y(r,g,b);
2219
    padcolor[1] = RGB_TO_U(r,g,b,0);
2220
    padcolor[2] = RGB_TO_V(r,g,b,0);
2221
}
2222
2223
static void opt_frame_pad_top(const char *arg)
2224
{
2225 115329f1 Diego Biurrun
    frame_padtop = atoi(arg);
2226 1ff93ffc Todd Kirby
    if (frame_padtop < 0) {
2227
        fprintf(stderr, "Incorrect top pad size\n");
2228
        exit(1);
2229
    }
2230
    if ((frame_padtop % 2) != 0) {
2231
        fprintf(stderr, "Top pad size must be a multiple of 2\n");
2232
        exit(1);
2233
    }
2234
}
2235
2236
static void opt_frame_pad_bottom(const char *arg)
2237
{
2238 115329f1 Diego Biurrun
    frame_padbottom = atoi(arg);
2239 1ff93ffc Todd Kirby
    if (frame_padbottom < 0) {
2240
        fprintf(stderr, "Incorrect bottom pad size\n");
2241
        exit(1);
2242
    }
2243
    if ((frame_padbottom % 2) != 0) {
2244
        fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
2245
        exit(1);
2246
    }
2247
}
2248
2249
2250
static void opt_frame_pad_left(const char *arg)
2251
{
2252 115329f1 Diego Biurrun
    frame_padleft = atoi(arg);
2253 1ff93ffc Todd Kirby
    if (frame_padleft < 0) {
2254
        fprintf(stderr, "Incorrect left pad size\n");
2255
        exit(1);
2256
    }
2257
    if ((frame_padleft % 2) != 0) {
2258
        fprintf(stderr, "Left pad size must be a multiple of 2\n");
2259
        exit(1);
2260
    }
2261
}
2262
2263
2264
static void opt_frame_pad_right(const char *arg)
2265
{
2266 115329f1 Diego Biurrun
    frame_padright = atoi(arg);
2267 1ff93ffc Todd Kirby
    if (frame_padright < 0) {
2268
        fprintf(stderr, "Incorrect right pad size\n");
2269
        exit(1);
2270
    }
2271
    if ((frame_padright % 2) != 0) {
2272
        fprintf(stderr, "Right pad size must be a multiple of 2\n");
2273
        exit(1);
2274
    }
2275
}
2276
2277
2278 63167088 Roman Shaposhnik
static void opt_frame_pix_fmt(const char *arg)
2279
{
2280
    frame_pix_fmt = avcodec_get_pix_fmt(arg);
2281
}
2282
2283 5fe03e38 Roman Shaposhnik
static void opt_frame_aspect_ratio(const char *arg)
2284
{
2285
    int x = 0, y = 0;
2286
    double ar = 0;
2287
    const char *p;
2288 115329f1 Diego Biurrun
2289 5fe03e38 Roman Shaposhnik
    p = strchr(arg, ':');
2290
    if (p) {
2291
        x = strtol(arg, (char **)&arg, 10);
2292 bb270c08 Diego Biurrun
        if (arg == p)
2293
            y = strtol(arg+1, (char **)&arg, 10);
2294
        if (x > 0 && y > 0)
2295
            ar = (double)x / (double)y;
2296 5fe03e38 Roman Shaposhnik
    } else
2297
        ar = strtod(arg, (char **)&arg);
2298
2299
    if (!ar) {
2300
        fprintf(stderr, "Incorrect aspect ratio specification.\n");
2301 bb270c08 Diego Biurrun
        exit(1);
2302 5fe03e38 Roman Shaposhnik
    }
2303
    frame_aspect_ratio = ar;
2304
}
2305
2306 b29f97d1 Zdenek Kabelac
static void opt_qscale(const char *arg)
2307 85f07f22 Fabrice Bellard
{
2308 158c7f05 Michael Niedermayer
    video_qscale = atof(arg);
2309 4e64bead Loren Merritt
    if (video_qscale <= 0 ||
2310 158c7f05 Michael Niedermayer
        video_qscale > 255) {
2311 4e64bead Loren Merritt
        fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
2312 85f07f22 Fabrice Bellard
        exit(1);
2313
    }
2314
}
2315
2316 b29f97d1 Zdenek Kabelac
static void opt_qdiff(const char *arg)
2317 9cdd6a24 Michael Niedermayer
{
2318
    video_qdiff = atoi(arg);
2319
    if (video_qdiff < 0 ||
2320
        video_qdiff > 31) {
2321
        fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
2322
        exit(1);
2323
    }
2324
}
2325
2326 b29f97d1 Zdenek Kabelac
static void opt_packet_size(const char *arg)
2327 1dbb6d90 Michael Niedermayer
{
2328
    packet_size= atoi(arg);
2329
}
2330
2331 b29f97d1 Zdenek Kabelac
static void opt_strict(const char *arg)
2332 f560dd82 Michael Niedermayer
{
2333
    strict= atoi(arg);
2334
}
2335
2336 bb198e19 Michael Niedermayer
static void opt_top_field_first(const char *arg)
2337
{
2338
    top_field_first= atoi(arg);
2339
}
2340
2341 9c3d33d6 Michael Niedermayer
static void opt_thread_count(const char *arg)
2342
{
2343
    thread_count= atoi(arg);
2344 2450cff2 François Revol
#if !defined(HAVE_THREADS)
2345 d8019eb5 Allen Day
    if (verbose >= 0)
2346
        fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
2347 842b556a Michael Niedermayer
#endif
2348 9c3d33d6 Michael Niedermayer
}
2349
2350 b29f97d1 Zdenek Kabelac
static void opt_audio_bitrate(const char *arg)
2351 85f07f22 Fabrice Bellard
{
2352
    audio_bit_rate = atoi(arg) * 1000;
2353
}
2354
2355 b29f97d1 Zdenek Kabelac
static void opt_audio_rate(const char *arg)
2356 85f07f22 Fabrice Bellard
{
2357
    audio_sample_rate = atoi(arg);
2358
}
2359
2360 b29f97d1 Zdenek Kabelac
static void opt_audio_channels(const char *arg)
2361 85f07f22 Fabrice Bellard
{
2362
    audio_channels = atoi(arg);
2363
}
2364
2365 b29f97d1 Zdenek Kabelac
static void opt_video_device(const char *arg)
2366 85f07f22 Fabrice Bellard
{
2367 e9a9e0c2 Fabrice Bellard
    video_device = av_strdup(arg);
2368 85f07f22 Fabrice Bellard
}
2369
2370 2639c651 Roman Shaposhnik
static void opt_grab_device(const char *arg)
2371
{
2372
    grab_device = av_strdup(arg);
2373
}
2374
2375 b29f97d1 Zdenek Kabelac
static void opt_video_channel(const char *arg)
2376 a5df11ab Fabrice Bellard
{
2377
    video_channel = strtol(arg, NULL, 0);
2378
}
2379
2380 e3ee3283 Alex Beregszaszi
static void opt_video_standard(const char *arg)
2381
{
2382
    video_standard = av_strdup(arg);
2383
}
2384
2385 b29f97d1 Zdenek Kabelac
static void opt_audio_device(const char *arg)
2386 85f07f22 Fabrice Bellard
{
2387 e9a9e0c2 Fabrice Bellard
    audio_device = av_strdup(arg);
2388 85f07f22 Fabrice Bellard
}
2389
2390 cf7fc795 Fabrice Bellard
static void opt_codec(int *pstream_copy, int *pcodec_id,
2391
                      int codec_type, const char *arg)
2392 85f07f22 Fabrice Bellard
{
2393
    AVCodec *p;
2394
2395 1629626f Fabrice Bellard
    if (!strcmp(arg, "copy")) {
2396 cf7fc795 Fabrice Bellard
        *pstream_copy = 1;
2397 85f07f22 Fabrice Bellard
    } else {
2398 1629626f Fabrice Bellard
        p = first_avcodec;
2399
        while (p) {
2400 cf7fc795 Fabrice Bellard
            if (!strcmp(p->name, arg) && p->type == codec_type)
2401 1629626f Fabrice Bellard
                break;
2402
            p = p->next;
2403
        }
2404
        if (p == NULL) {
2405 cf7fc795 Fabrice Bellard
            fprintf(stderr, "Unknown codec '%s'\n", arg);
2406 1629626f Fabrice Bellard
            exit(1);
2407
        } else {
2408 cf7fc795 Fabrice Bellard
            *pcodec_id = p->id;
2409 1629626f Fabrice Bellard
        }
2410 85f07f22 Fabrice Bellard
    }
2411
}
2412
2413 cf7fc795 Fabrice Bellard
static void opt_audio_codec(const char *arg)
2414
{
2415
    opt_codec(&audio_stream_copy, &audio_codec_id, CODEC_TYPE_AUDIO, arg);
2416
}
2417
2418 b2a2197e Michael Niedermayer
static void opt_audio_tag(const char *arg)
2419
{
2420
    char *tail;
2421
    audio_codec_tag= strtol(arg, &tail, 0);
2422
2423
    if(!tail || *tail)
2424
        audio_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
2425
}
2426
2427
static void opt_video_tag(const char *arg)
2428
{
2429
    char *tail;
2430
    video_codec_tag= strtol(arg, &tail, 0);
2431
2432
    if(!tail || *tail)
2433
        video_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
2434
}
2435
2436 b29f97d1 Zdenek Kabelac
static void add_frame_hooker(const char *arg)
2437 10d104e4 Philip Gladstone
{
2438
    int argc = 0;
2439
    char *argv[64];
2440
    int i;
2441 e9a9e0c2 Fabrice Bellard
    char *args = av_strdup(arg);
2442 10d104e4 Philip Gladstone
2443 bee0d9e5 Charles Yates
    using_vhook = 1;
2444
2445 10d104e4 Philip Gladstone
    argv[0] = strtok(args, " ");
2446
    while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
2447
    }
2448
2449
    i = frame_hook_add(argc, argv);
2450
2451
    if (i != 0) {
2452
        fprintf(stderr, "Failed to add video hook function: %s\n", arg);
2453
        exit(1);
2454
    }
2455
}
2456
2457 85f07f22 Fabrice Bellard
const char *motion_str[] = {
2458
    "zero",
2459
    "full",
2460
    "log",
2461
    "phods",
2462 7084c149 Michael Niedermayer
    "epzs",
2463
    "x1",
2464 51d6a3cf Michael Niedermayer
    "hex",
2465
    "umh",
2466
    "iter",
2467 85f07f22 Fabrice Bellard
    NULL,
2468
};
2469
2470 b29f97d1 Zdenek Kabelac
static void opt_motion_estimation(const char *arg)
2471 85f07f22 Fabrice Bellard
{
2472
    const char **p;
2473
    p = motion_str;
2474
    for(;;) {
2475
        if (!*p) {
2476
            fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
2477
            exit(1);
2478
        }
2479
        if (!strcmp(*p, arg))
2480
            break;
2481
        p++;
2482
    }
2483 101bea5f Juanjo
    me_method = (p - motion_str) + 1;
2484 85f07f22 Fabrice Bellard
}
2485
2486 b29f97d1 Zdenek Kabelac
static void opt_video_codec(const char *arg)
2487 85f07f22 Fabrice Bellard
{
2488 cf7fc795 Fabrice Bellard
    opt_codec(&video_stream_copy, &video_codec_id, CODEC_TYPE_VIDEO, arg);
2489
}
2490 85f07f22 Fabrice Bellard
2491 cf7fc795 Fabrice Bellard
static void opt_subtitle_codec(const char *arg)
2492
{
2493
    opt_codec(&subtitle_stream_copy, &subtitle_codec_id, CODEC_TYPE_SUBTITLE, arg);
2494 85f07f22 Fabrice Bellard
}
2495
2496 b29f97d1 Zdenek Kabelac
static void opt_map(const char *arg)
2497 85f07f22 Fabrice Bellard
{
2498
    AVStreamMap *m;
2499
    const char *p;
2500
2501
    p = arg;
2502
    m = &stream_maps[nb_stream_maps++];
2503
2504
    m->file_index = strtol(arg, (char **)&p, 0);
2505
    if (*p)
2506
        p++;
2507 a5dc85ef Juanjo
2508
    m->stream_index = strtol(p, (char **)&p, 0);
2509 b4a3389e Wolfram Gloger
    if (*p) {
2510
        p++;
2511
        m->sync_file_index = strtol(p, (char **)&p, 0);
2512
        if (*p)
2513
            p++;
2514
        m->sync_stream_index = strtol(p, (char **)&p, 0);
2515
    } else {
2516
        m->sync_file_index = m->file_index;
2517
        m->sync_stream_index = m->stream_index;
2518
    }
2519 85f07f22 Fabrice Bellard
}
2520
2521 0a38bafd Patrice Bensoussan
static void opt_map_meta_data(const char *arg)
2522
{
2523
    AVMetaDataMap *m;
2524
    const char *p;
2525 115329f1 Diego Biurrun
2526 0a38bafd Patrice Bensoussan
    p = arg;
2527
    m = &meta_data_maps[nb_meta_data_maps++];
2528
2529
    m->out_file = strtol(arg, (char **)&p, 0);
2530
    if (*p)
2531
        p++;
2532
2533
    m->in_file = strtol(p, (char **)&p, 0);
2534
}
2535
2536 b29f97d1 Zdenek Kabelac
static void opt_recording_time(const char *arg)
2537 85f07f22 Fabrice Bellard
{
2538
    recording_time = parse_date(arg, 1);
2539
}
2540
2541 8831db5c Michael Niedermayer
static void opt_start_time(const char *arg)
2542
{
2543
    start_time = parse_date(arg, 1);
2544
}
2545
2546 4568325a Roman Shaposhnik
static void opt_rec_timestamp(const char *arg)
2547
{
2548
    rec_timestamp = parse_date(arg, 0) / 1000000;
2549
}
2550
2551 a6a92a9a Wolfram Gloger
static void opt_input_ts_offset(const char *arg)
2552
{
2553
    input_ts_offset = parse_date(arg, 1);
2554
}
2555
2556 b29f97d1 Zdenek Kabelac
static void opt_input_file(const char *filename)
2557 85f07f22 Fabrice Bellard
{
2558
    AVFormatContext *ic;
2559
    AVFormatParameters params, *ap = &params;
2560 14bea432 Michael Niedermayer
    int err, i, ret, rfps, rfps_base;
2561 bd1b79a1 Michael Niedermayer
    int64_t timestamp;
2562 85f07f22 Fabrice Bellard
2563 b242baa4 Fabrice Bellard
    if (!strcmp(filename, "-"))
2564
        filename = "pipe:";
2565
2566 115329f1 Diego Biurrun
    using_stdin |= !strncmp(filename, "pipe:", 5) ||
2567 d9a916e2 Charles Yates
                   !strcmp( filename, "/dev/stdin" );
2568
2569 85f07f22 Fabrice Bellard
    /* get default parameters from command line */
2570 4eb72c6b Nico Sabbi
    ic = av_alloc_format_context();
2571
2572 79fdaa4c Fabrice Bellard
    memset(ap, 0, sizeof(*ap));
2573 4eb72c6b Nico Sabbi
    ap->prealloced_context = 1;
2574 79fdaa4c Fabrice Bellard
    ap->sample_rate = audio_sample_rate;
2575
    ap->channels = audio_channels;
2576 c0df9d75 Michael Niedermayer
    ap->time_base.den = frame_rate;
2577
    ap->time_base.num = frame_rate_base;
2578 1ff93ffc Todd Kirby
    ap->width = frame_width + frame_padleft + frame_padright;
2579
    ap->height = frame_height + frame_padtop + frame_padbottom;
2580 63167088 Roman Shaposhnik
    ap->pix_fmt = frame_pix_fmt;
2581 2639c651 Roman Shaposhnik
    ap->device  = grab_device;
2582
    ap->channel = video_channel;
2583
    ap->standard = video_standard;
2584 5b6d5596 Michael Niedermayer
    ap->video_codec_id = video_codec_id;
2585
    ap->audio_codec_id = audio_codec_id;
2586
    if(pgmyuv_compatibility_hack)
2587
        ap->video_codec_id= CODEC_ID_PGMYUV;
2588 79fdaa4c Fabrice Bellard
2589 4eb72c6b Nico Sabbi
    for(i=0; i<opt_name_count; i++){
2590
        AVOption *opt;
2591
        double d= av_get_double(avformat_opts, opt_names[i], &opt);
2592
        if(d==d && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2593
            av_set_double(ic, opt_names[i], d);
2594
    }
2595 79fdaa4c Fabrice Bellard
    /* open the input file with generic libav function */
2596
    err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
2597 85f07f22 Fabrice Bellard
    if (err < 0) {
2598 79fdaa4c Fabrice Bellard
        print_error(filename, err);
2599 85f07f22 Fabrice Bellard
        exit(1);
2600
    }
2601 30bc6613 Michael Niedermayer
2602 5894e1bb Víctor Paesa
    ic->loop_input = loop_input;
2603
2604 79fdaa4c Fabrice Bellard
    /* If not enough info to get the stream parameters, we decode the
2605
       first frames to get it. (used in mpeg case for example) */
2606
    ret = av_find_stream_info(ic);
2607 d8019eb5 Allen Day
    if (ret < 0 && verbose >= 0) {
2608 85f07f22 Fabrice Bellard
        fprintf(stderr, "%s: could not find codec parameters\n", filename);
2609
        exit(1);
2610
    }
2611
2612 bd1b79a1 Michael Niedermayer
    timestamp = start_time;
2613
    /* add the stream start time */
2614
    if (ic->start_time != AV_NOPTS_VALUE)
2615
        timestamp += ic->start_time;
2616
2617 254abc2e Fabrice Bellard
    /* if seeking requested, we execute it */
2618
    if (start_time != 0) {
2619 3ba1438d Michael Niedermayer
        ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
2620 254abc2e Fabrice Bellard
        if (ret < 0) {
2621 115329f1 Diego Biurrun
            fprintf(stderr, "%s: could not seek to position %0.3f\n",
2622 254abc2e Fabrice Bellard
                    filename, (double)timestamp / AV_TIME_BASE);
2623
        }
2624
        /* reset seek info */
2625
        start_time = 0;
2626
    }
2627
2628 85f07f22 Fabrice Bellard
    /* update the current parameters so that they match the one of the input stream */
2629
    for(i=0;i<ic->nb_streams;i++) {
2630 637b5326 Michael Niedermayer
        int j;
2631 01f4895c Michael Niedermayer
        AVCodecContext *enc = ic->streams[i]->codec;
2632 2450cff2 François Revol
#if defined(HAVE_THREADS)
2633 c62c07d3 Michael Niedermayer
        if(thread_count>1)
2634
            avcodec_thread_init(enc, thread_count);
2635
#endif
2636
        enc->thread_count= thread_count;
2637 85f07f22 Fabrice Bellard
        switch(enc->codec_type) {
2638
        case CODEC_TYPE_AUDIO:
2639 637b5326 Michael Niedermayer
            for(j=0; j<opt_name_count; j++){
2640
                AVOption *opt;
2641
                double d= av_get_double(avctx_opts, opt_names[j], &opt);
2642
                if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2643
                    av_set_double(enc, opt_names[j], d);
2644
            }
2645 e0d2714a Juanjo
            //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2646 85f07f22 Fabrice Bellard
            audio_channels = enc->channels;
2647
            audio_sample_rate = enc->sample_rate;
2648 b4aea108 Michael Niedermayer
            if(audio_disable)
2649 f3356e9c Michael Niedermayer
                ic->streams[i]->discard= AVDISCARD_ALL;
2650 85f07f22 Fabrice Bellard
            break;
2651
        case CODEC_TYPE_VIDEO:
2652 637b5326 Michael Niedermayer
            for(j=0; j<opt_name_count; j++){
2653
                AVOption *opt;
2654
                double d= av_get_double(avctx_opts, opt_names[j], &opt);
2655
                if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2656
                    av_set_double(enc, opt_names[j], d);
2657
            }
2658 85f07f22 Fabrice Bellard
            frame_height = enc->height;
2659
            frame_width = enc->width;
2660 bb270c08 Diego Biurrun
            frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
2661
            frame_pix_fmt = enc->pix_fmt;
2662 c0df9d75 Michael Niedermayer
            rfps      = ic->streams[i]->r_frame_rate.num;
2663
            rfps_base = ic->streams[i]->r_frame_rate.den;
2664 637b5326 Michael Niedermayer
            if(enc->lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
2665 f4f3223f Michael Niedermayer
            if(me_threshold)
2666
                enc->debug |= FF_DEBUG_MV;
2667 d7425f59 Michael Niedermayer
2668 115329f1 Diego Biurrun
            if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) {
2669 d8019eb5 Allen Day
2670
                if (verbose >= 0)
2671 15bc38e5 Michael Niedermayer
                    fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
2672 c0df9d75 Michael Niedermayer
                            i, (float)enc->time_base.den / enc->time_base.num, enc->time_base.den, enc->time_base.num,
2673 d8019eb5 Allen Day
2674 15bc38e5 Michael Niedermayer
                    (float)rfps / rfps_base, rfps, rfps_base);
2675 79fdaa4c Fabrice Bellard
            }
2676 bf5af568 Fabrice Bellard
            /* update the current frame rate to match the stream frame rate */