Statistics
| Branch: | Revision:

ffmpeg / ffmpeg.c @ 023857be

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

2164
    if (url_open(&h, filename, O_RDONLY) < 0) {
2165
        printf("could not open '%s'\n", filename);
2166
        return -1;
2167
    }
2168
    for(;;) {
2169
        len = url_read(h, buffer, sizeof(buffer));
2170
        if (len <= 0)
2171
            break;
2172
        for(i=0;i<len;i++) putchar(buffer[i]);
2173
    }
2174
    url_close(h);
2175
    return 0;
2176
}
2177
#endif
2178
2179 b29f97d1 Zdenek Kabelac
static void opt_image_format(const char *arg)
2180 817b23ff Fabrice Bellard
{
2181
    AVImageFormat *f;
2182 115329f1 Diego Biurrun
2183 817b23ff Fabrice Bellard
    for(f = first_image_format; f != NULL; f = f->next) {
2184
        if (!strcmp(arg, f->name))
2185
            break;
2186
    }
2187
    if (!f) {
2188
        fprintf(stderr, "Unknown image format: '%s'\n", arg);
2189
        exit(1);
2190
    }
2191
    image_format = f;
2192
}
2193
2194 b29f97d1 Zdenek Kabelac
static void opt_format(const char *arg)
2195 85f07f22 Fabrice Bellard
{
2196 817b23ff Fabrice Bellard
    /* compatibility stuff for pgmyuv */
2197
    if (!strcmp(arg, "pgmyuv")) {
2198 5b6d5596 Michael Niedermayer
        pgmyuv_compatibility_hack=1;
2199 9286699a Michael Niedermayer
//        opt_image_format(arg);
2200 5b6d5596 Michael Niedermayer
        arg = "image2";
2201 817b23ff Fabrice Bellard
    }
2202
2203 79fdaa4c Fabrice Bellard
    file_iformat = av_find_input_format(arg);
2204
    file_oformat = guess_format(arg, NULL, NULL);
2205
    if (!file_iformat && !file_oformat) {
2206
        fprintf(stderr, "Unknown input or output format: %s\n", arg);
2207 85f07f22 Fabrice Bellard
        exit(1);
2208
    }
2209
}
2210
2211 b29f97d1 Zdenek Kabelac
static void opt_video_bitrate(const char *arg)
2212 85f07f22 Fabrice Bellard
{
2213
    video_bit_rate = atoi(arg) * 1000;
2214
}
2215
2216 b29f97d1 Zdenek Kabelac
static void opt_video_bitrate_tolerance(const char *arg)
2217 9cdd6a24 Michael Niedermayer
{
2218
    video_bit_rate_tolerance = atoi(arg) * 1000;
2219
}
2220
2221 b29f97d1 Zdenek Kabelac
static void opt_video_bitrate_max(const char *arg)
2222 3aa102be Michael Niedermayer
{
2223
    video_rc_max_rate = atoi(arg) * 1000;
2224
}
2225
2226 b29f97d1 Zdenek Kabelac
static void opt_video_bitrate_min(const char *arg)
2227 3aa102be Michael Niedermayer
{
2228
    video_rc_min_rate = atoi(arg) * 1000;
2229
}
2230
2231 b29f97d1 Zdenek Kabelac
static void opt_video_buffer_size(const char *arg)
2232 946c8a12 Michael Niedermayer
{
2233 622348f9 Michael Niedermayer
    video_rc_buffer_size = atoi(arg) * 8*1024;
2234 946c8a12 Michael Niedermayer
}
2235
2236 b29f97d1 Zdenek Kabelac
static void opt_video_rc_eq(char *arg)
2237 3aa102be Michael Niedermayer
{
2238
    video_rc_eq = arg;
2239
}
2240
2241 b29f97d1 Zdenek Kabelac
static void opt_video_rc_override_string(char *arg)
2242 ac2830ec Michael Niedermayer
{
2243
    video_rc_override_string = arg;
2244
}
2245
2246 3aa102be Michael Niedermayer
2247 b29f97d1 Zdenek Kabelac
static void opt_workaround_bugs(const char *arg)
2248 fe670d09 Michael Niedermayer
{
2249
    workaround_bugs = atoi(arg);
2250
}
2251
2252 f4f3223f Michael Niedermayer
static void opt_me_threshold(const char *arg)
2253
{
2254
    me_threshold = atoi(arg);
2255
}
2256
2257 f20f8a8b Michael Niedermayer
static void opt_mb_threshold(const char *arg)
2258
{
2259
    mb_threshold = atoi(arg);
2260
}
2261 2ad1516a Michael Niedermayer
2262 f068206e Bill Eldridge
static void opt_verbose(const char *arg)
2263
{
2264
    verbose = atoi(arg);
2265 d8019eb5 Allen Day
    av_log_set_level(atoi(arg));
2266 f068206e Bill Eldridge
}
2267
2268 b29f97d1 Zdenek Kabelac
static void opt_frame_rate(const char *arg)
2269 85f07f22 Fabrice Bellard
{
2270 445f1b83 Roman Shaposhnik
    if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
2271
        fprintf(stderr, "Incorrect frame rate\n");
2272 bb270c08 Diego Biurrun
        exit(1);
2273 445f1b83 Roman Shaposhnik
    }
2274 85f07f22 Fabrice Bellard
}
2275
2276 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_top(const char *arg)
2277 ab6d194a Michael Niedermayer
{
2278 115329f1 Diego Biurrun
    frame_topBand = atoi(arg);
2279 ab6d194a Michael Niedermayer
    if (frame_topBand < 0) {
2280
        fprintf(stderr, "Incorrect top crop size\n");
2281
        exit(1);
2282
    }
2283
    if ((frame_topBand % 2) != 0) {
2284
        fprintf(stderr, "Top crop size must be a multiple of 2\n");
2285
        exit(1);
2286
    }
2287
    if ((frame_topBand) >= frame_height){
2288 bb270c08 Diego Biurrun
        fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2289 ab6d194a Michael Niedermayer
        exit(1);
2290
    }
2291
    frame_height -= frame_topBand;
2292
}
2293
2294 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_bottom(const char *arg)
2295 ab6d194a Michael Niedermayer
{
2296
    frame_bottomBand = atoi(arg);
2297
    if (frame_bottomBand < 0) {
2298
        fprintf(stderr, "Incorrect bottom crop size\n");
2299
        exit(1);
2300
    }
2301
    if ((frame_bottomBand % 2) != 0) {
2302
        fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
2303 115329f1 Diego Biurrun
        exit(1);
2304 ab6d194a Michael Niedermayer
    }
2305
    if ((frame_bottomBand) >= frame_height){
2306 bb270c08 Diego Biurrun
        fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2307 ab6d194a Michael Niedermayer
        exit(1);
2308
    }
2309
    frame_height -= frame_bottomBand;
2310
}
2311
2312 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_left(const char *arg)
2313 ab6d194a Michael Niedermayer
{
2314
    frame_leftBand = atoi(arg);
2315
    if (frame_leftBand < 0) {
2316
        fprintf(stderr, "Incorrect left crop size\n");
2317
        exit(1);
2318
    }
2319
    if ((frame_leftBand % 2) != 0) {
2320
        fprintf(stderr, "Left crop size must be a multiple of 2\n");
2321
        exit(1);
2322
    }
2323
    if ((frame_leftBand) >= frame_width){
2324 bb270c08 Diego Biurrun
        fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2325 ab6d194a Michael Niedermayer
        exit(1);
2326
    }
2327
    frame_width -= frame_leftBand;
2328
}
2329
2330 b29f97d1 Zdenek Kabelac
static void opt_frame_crop_right(const char *arg)
2331 ab6d194a Michael Niedermayer
{
2332
    frame_rightBand = atoi(arg);
2333
    if (frame_rightBand < 0) {
2334
        fprintf(stderr, "Incorrect right crop size\n");
2335
        exit(1);
2336
    }
2337
    if ((frame_rightBand % 2) != 0) {
2338
        fprintf(stderr, "Right crop size must be a multiple of 2\n");
2339 115329f1 Diego Biurrun
        exit(1);
2340 ab6d194a Michael Niedermayer
    }
2341
    if ((frame_rightBand) >= frame_width){
2342 bb270c08 Diego Biurrun
        fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2343 ab6d194a Michael Niedermayer
        exit(1);
2344
    }
2345
    frame_width -= frame_rightBand;
2346
}
2347
2348 b29f97d1 Zdenek Kabelac
static void opt_frame_size(const char *arg)
2349 85f07f22 Fabrice Bellard
{
2350 445f1b83 Roman Shaposhnik
    if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
2351 85f07f22 Fabrice Bellard
        fprintf(stderr, "Incorrect frame size\n");
2352
        exit(1);
2353
    }
2354
    if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
2355
        fprintf(stderr, "Frame size must be a multiple of 2\n");
2356
        exit(1);
2357
    }
2358
}
2359
2360 1ff93ffc Todd Kirby
2361
#define SCALEBITS 10
2362
#define ONE_HALF  (1 << (SCALEBITS - 1))
2363 bb270c08 Diego Biurrun
#define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
2364 1ff93ffc Todd Kirby
2365
#define RGB_TO_Y(r, g, b) \
2366
((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
2367
  FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
2368
2369
#define RGB_TO_U(r1, g1, b1, shift)\
2370
(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +         \
2371
     FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2372
2373
#define RGB_TO_V(r1, g1, b1, shift)\
2374
(((FIX(0.50000) * r1 - FIX(0.41869) * g1 -           \
2375
   FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2376
2377
static void opt_pad_color(const char *arg) {
2378
    /* Input is expected to be six hex digits similar to
2379
       how colors are expressed in html tags (but without the #) */
2380
    int rgb = strtol(arg, NULL, 16);
2381
    int r,g,b;
2382 115329f1 Diego Biurrun
2383
    r = (rgb >> 16);
2384 1ff93ffc Todd Kirby
    g = ((rgb >> 8) & 255);
2385
    b = (rgb & 255);
2386
2387
    padcolor[0] = RGB_TO_Y(r,g,b);
2388
    padcolor[1] = RGB_TO_U(r,g,b,0);
2389
    padcolor[2] = RGB_TO_V(r,g,b,0);
2390
}
2391
2392
static void opt_frame_pad_top(const char *arg)
2393
{
2394 115329f1 Diego Biurrun
    frame_padtop = atoi(arg);
2395 1ff93ffc Todd Kirby
    if (frame_padtop < 0) {
2396
        fprintf(stderr, "Incorrect top pad size\n");
2397
        exit(1);
2398
    }
2399
    if ((frame_padtop % 2) != 0) {
2400
        fprintf(stderr, "Top pad size must be a multiple of 2\n");
2401
        exit(1);
2402
    }
2403
}
2404
2405
static void opt_frame_pad_bottom(const char *arg)
2406
{
2407 115329f1 Diego Biurrun
    frame_padbottom = atoi(arg);
2408 1ff93ffc Todd Kirby
    if (frame_padbottom < 0) {
2409
        fprintf(stderr, "Incorrect bottom pad size\n");
2410
        exit(1);
2411
    }
2412
    if ((frame_padbottom % 2) != 0) {
2413
        fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
2414
        exit(1);
2415
    }
2416
}
2417
2418
2419
static void opt_frame_pad_left(const char *arg)
2420
{
2421 115329f1 Diego Biurrun
    frame_padleft = atoi(arg);
2422 1ff93ffc Todd Kirby
    if (frame_padleft < 0) {
2423
        fprintf(stderr, "Incorrect left pad size\n");
2424
        exit(1);
2425
    }
2426
    if ((frame_padleft % 2) != 0) {
2427
        fprintf(stderr, "Left pad size must be a multiple of 2\n");
2428
        exit(1);
2429
    }
2430
}
2431
2432
2433
static void opt_frame_pad_right(const char *arg)
2434
{
2435 115329f1 Diego Biurrun
    frame_padright = atoi(arg);
2436 1ff93ffc Todd Kirby
    if (frame_padright < 0) {
2437
        fprintf(stderr, "Incorrect right pad size\n");
2438
        exit(1);
2439
    }
2440
    if ((frame_padright % 2) != 0) {
2441
        fprintf(stderr, "Right pad size must be a multiple of 2\n");
2442
        exit(1);
2443
    }
2444
}
2445
2446
2447 63167088 Roman Shaposhnik
static void opt_frame_pix_fmt(const char *arg)
2448
{
2449
    frame_pix_fmt = avcodec_get_pix_fmt(arg);
2450
}
2451
2452 5fe03e38 Roman Shaposhnik
static void opt_frame_aspect_ratio(const char *arg)
2453
{
2454
    int x = 0, y = 0;
2455
    double ar = 0;
2456
    const char *p;
2457 115329f1 Diego Biurrun
2458 5fe03e38 Roman Shaposhnik
    p = strchr(arg, ':');
2459
    if (p) {
2460
        x = strtol(arg, (char **)&arg, 10);
2461 bb270c08 Diego Biurrun
        if (arg == p)
2462
            y = strtol(arg+1, (char **)&arg, 10);
2463
        if (x > 0 && y > 0)
2464
            ar = (double)x / (double)y;
2465 5fe03e38 Roman Shaposhnik
    } else
2466
        ar = strtod(arg, (char **)&arg);
2467
2468
    if (!ar) {
2469
        fprintf(stderr, "Incorrect aspect ratio specification.\n");
2470 bb270c08 Diego Biurrun
        exit(1);
2471 5fe03e38 Roman Shaposhnik
    }
2472
    frame_aspect_ratio = ar;
2473
}
2474
2475 b29f97d1 Zdenek Kabelac
static void opt_gop_size(const char *arg)
2476 85f07f22 Fabrice Bellard
{
2477
    gop_size = atoi(arg);
2478
}
2479
2480 b29f97d1 Zdenek Kabelac
static void opt_b_frames(const char *arg)
2481 bc6caae2 Juanjo
{
2482
    b_frames = atoi(arg);
2483
    if (b_frames > FF_MAX_B_FRAMES) {
2484
        fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
2485
        exit(1);
2486
    } else if (b_frames < 1) {
2487
        fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
2488
        exit(1);
2489
    }
2490
}
2491
2492 9c3d33d6 Michael Niedermayer
static void opt_pre_me(const char *arg)
2493
{
2494
    pre_me = atoi(arg);
2495
}
2496
2497 b29f97d1 Zdenek Kabelac
static void opt_qscale(const char *arg)
2498 85f07f22 Fabrice Bellard
{
2499 158c7f05 Michael Niedermayer
    video_qscale = atof(arg);
2500
    if (video_qscale < 0.01 ||
2501
        video_qscale > 255) {
2502
        fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
2503 85f07f22 Fabrice Bellard
        exit(1);
2504
    }
2505
}
2506
2507 6a5d4395 Martin Drab
static void opt_qsquish(const char *arg)
2508
{
2509
    video_qsquish = atof(arg);
2510
    if (video_qsquish < 0.0 ||
2511
        video_qsquish > 99.0) {
2512
        fprintf(stderr, "qsquish must be >= 0.0 and <= 99.0\n");
2513
        exit(1);
2514
    }
2515
}
2516
2517 4ea4b274 Michael Niedermayer
static void opt_lmax(const char *arg)
2518
{
2519
    video_lmax = atof(arg)*FF_QP2LAMBDA;
2520
}
2521
2522
static void opt_lmin(const char *arg)
2523
{
2524
    video_lmin = atof(arg)*FF_QP2LAMBDA;
2525
}
2526
2527 b29f97d1 Zdenek Kabelac
static void opt_qmin(const char *arg)
2528 9cdd6a24 Michael Niedermayer
{
2529
    video_qmin = atoi(arg);
2530 6e0d8c06 Michael Niedermayer
    if (video_qmin < 1 ||
2531 58f7833e Robert Swain
        video_qmin > 51) {
2532
        fprintf(stderr, "qmin must be >= 1 and <= 51\n");
2533 9cdd6a24 Michael Niedermayer
        exit(1);
2534
    }
2535
}
2536
2537 b29f97d1 Zdenek Kabelac
static void opt_qmax(const char *arg)
2538 9cdd6a24 Michael Niedermayer
{
2539
    video_qmax = atoi(arg);
2540 6e0d8c06 Michael Niedermayer
    if (video_qmax < 1 ||
2541 58f7833e Robert Swain
        video_qmax > 51) {
2542
        fprintf(stderr, "qmax must be >= 1 and <= 51\n");
2543 9cdd6a24 Michael Niedermayer
        exit(1);
2544
    }
2545
}
2546
2547 6e0d8c06 Michael Niedermayer
static void opt_mb_lmin(const char *arg)
2548 17a70fde Michael Niedermayer
{
2549 6e0d8c06 Michael Niedermayer
    video_mb_lmin = atof(arg)*FF_QP2LAMBDA;
2550
    if (video_mb_lmin < 1 ||
2551
        video_mb_lmin > FF_LAMBDA_MAX) {
2552
        fprintf(stderr, "mblmin must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
2553 17a70fde Michael Niedermayer
        exit(1);
2554
    }
2555
}
2556
2557 6e0d8c06 Michael Niedermayer
static void opt_mb_lmax(const char *arg)
2558 17a70fde Michael Niedermayer
{
2559 6e0d8c06 Michael Niedermayer
    video_mb_lmax = atof(arg)*FF_QP2LAMBDA;
2560
    if (video_mb_lmax < 1 ||
2561
        video_mb_lmax > FF_LAMBDA_MAX) {
2562
        fprintf(stderr, "mblmax must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
2563 17a70fde Michael Niedermayer
        exit(1);
2564
    }
2565
}
2566
2567 b29f97d1 Zdenek Kabelac
static void opt_qdiff(const char *arg)
2568 9cdd6a24 Michael Niedermayer
{
2569
    video_qdiff = atoi(arg);
2570
    if (video_qdiff < 0 ||
2571
        video_qdiff > 31) {
2572
        fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
2573
        exit(1);
2574
    }
2575
}
2576
2577 b29f97d1 Zdenek Kabelac
static void opt_qblur(const char *arg)
2578 9cdd6a24 Michael Niedermayer
{
2579
    video_qblur = atof(arg);
2580
}
2581
2582 b29f97d1 Zdenek Kabelac
static void opt_qcomp(const char *arg)
2583 9cdd6a24 Michael Niedermayer
{
2584
    video_qcomp = atof(arg);
2585
}
2586 85f07f22 Fabrice Bellard
2587 b29f97d1 Zdenek Kabelac
static void opt_rc_initial_cplx(const char *arg)
2588 ac2830ec Michael Niedermayer
{
2589
    video_rc_initial_cplx = atof(arg);
2590
}
2591 b29f97d1 Zdenek Kabelac
static void opt_b_qfactor(const char *arg)
2592 29700fa6 Michael Niedermayer
{
2593
    video_b_qfactor = atof(arg);
2594
}
2595 b29f97d1 Zdenek Kabelac
static void opt_i_qfactor(const char *arg)
2596 29700fa6 Michael Niedermayer
{
2597
    video_i_qfactor = atof(arg);
2598
}
2599 b29f97d1 Zdenek Kabelac
static void opt_b_qoffset(const char *arg)
2600 29700fa6 Michael Niedermayer
{
2601
    video_b_qoffset = atof(arg);
2602
}
2603 b29f97d1 Zdenek Kabelac
static void opt_i_qoffset(const char *arg)
2604 29700fa6 Michael Niedermayer
{
2605
    video_i_qoffset = atof(arg);
2606
}
2607
2608 303e50e6 Michael Niedermayer
static void opt_ibias(const char *arg)
2609
{
2610
    video_intra_quant_bias = atoi(arg);
2611
}
2612
static void opt_pbias(const char *arg)
2613
{
2614
    video_inter_quant_bias = atoi(arg);
2615
}
2616
2617 b29f97d1 Zdenek Kabelac
static void opt_packet_size(const char *arg)
2618 1dbb6d90 Michael Niedermayer
{
2619
    packet_size= atoi(arg);
2620
}
2621
2622 7ebfc0ea Michael Niedermayer
static void opt_error_rate(const char *arg)
2623
{
2624
    error_rate= atoi(arg);
2625
}
2626
2627 b29f97d1 Zdenek Kabelac
static void opt_strict(const char *arg)
2628 f560dd82 Michael Niedermayer
{
2629
    strict= atoi(arg);
2630
}
2631
2632 bb198e19 Michael Niedermayer
static void opt_top_field_first(const char *arg)
2633
{
2634
    top_field_first= atoi(arg);
2635
}
2636
2637 303e50e6 Michael Niedermayer
static void opt_sc_threshold(const char *arg)
2638
{
2639
    sc_threshold= atoi(arg);
2640
}
2641
2642 2f0472ff Timofei V. Bondarenko
static void opt_me_range(const char *arg)
2643
{
2644
    me_range = atoi(arg);
2645
}
2646
2647 9c3d33d6 Michael Niedermayer
static void opt_thread_count(const char *arg)
2648
{
2649
    thread_count= atoi(arg);
2650 2450cff2 François Revol
#if !defined(HAVE_THREADS)
2651 d8019eb5 Allen Day
    if (verbose >= 0)
2652
        fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
2653 842b556a Michael Niedermayer
#endif
2654 9c3d33d6 Michael Niedermayer
}
2655
2656 b29f97d1 Zdenek Kabelac
static void opt_audio_bitrate(const char *arg)
2657 85f07f22 Fabrice Bellard
{
2658
    audio_bit_rate = atoi(arg) * 1000;
2659
}
2660
2661 b29f97d1 Zdenek Kabelac
static void opt_audio_rate(const char *arg)
2662 85f07f22 Fabrice Bellard
{
2663
    audio_sample_rate = atoi(arg);
2664
}
2665
2666 b29f97d1 Zdenek Kabelac
static void opt_audio_channels(const char *arg)
2667 85f07f22 Fabrice Bellard
{
2668
    audio_channels = atoi(arg);
2669
}
2670
2671 b29f97d1 Zdenek Kabelac
static void opt_video_device(const char *arg)
2672 85f07f22 Fabrice Bellard
{
2673 e9a9e0c2 Fabrice Bellard
    video_device = av_strdup(arg);
2674 85f07f22 Fabrice Bellard
}
2675
2676 2639c651 Roman Shaposhnik
static void opt_grab_device(const char *arg)
2677
{
2678
    grab_device = av_strdup(arg);
2679
}
2680
2681