Remove the "rc_init_cplx" OptionDef option from ffmpeg.c. The equally named
[libav.git] / ffmpeg.c
CommitLineData
85f07f22 1/*
115329f1 2 * FFmpeg main
01310af2 3 * Copyright (c) 2000-2003 Fabrice Bellard
85f07f22 4 *
bf5af568
FB
5 * 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.
85f07f22 9 *
bf5af568 10 * This library is distributed in the hope that it will be useful,
85f07f22 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bf5af568
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
85f07f22 14 *
bf5af568
FB
15 * 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
5509bffa 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
85f07f22 18 */
daf8e955 19#define HAVE_AV_CONFIG_H
22f7a060 20#include <limits.h>
daf8e955 21#include "avformat.h"
18a54b04 22#include "swscale.h"
10d104e4 23#include "framehook.h"
1ac31760 24#include "dsputil.h"
6fd46414 25#include "opt.h"
daf8e955 26
2f30a81d 27#ifndef __MINGW32__
85f07f22
FB
28#include <unistd.h>
29#include <fcntl.h>
30#include <sys/ioctl.h>
85f07f22 31#include <sys/time.h>
85f07f22 32#include <termios.h>
5727b222 33#include <sys/resource.h>
9680a722 34#include <signal.h>
bdc4796f 35#endif
f3ec2d46
SG
36#ifdef CONFIG_OS2
37#include <sys/types.h>
38#include <sys/select.h>
39#include <stdlib.h>
40#endif
64c020a8 41#undef time //needed because HAVE_AV_CONFIG_H is defined on top
bf5af568 42#include <time.h>
85f07f22 43
f1cc88a5 44#include "version.h"
01310af2
FB
45#include "cmdutils.h"
46
2b18dcd0
MN
47#undef NDEBUG
48#include <assert.h>
49
8aa1e3da
MN
50#if !defined(INFINITY) && defined(HUGE_VAL)
51#define INFINITY HUGE_VAL
52#endif
85f07f22 53
85f07f22
FB
54/* select an input stream for an output stream */
55typedef struct AVStreamMap {
56 int file_index;
57 int stream_index;
b4a3389e
WG
58 int sync_file_index;
59 int sync_stream_index;
85f07f22
FB
60} AVStreamMap;
61
0a38bafd
PB
62/** select an input file for an output file */
63typedef struct AVMetaDataMap {
64 int out_file;
65 int in_file;
66} AVMetaDataMap;
67
85f07f22
FB
68extern const OptionDef options[];
69
02d504a7
FB
70static void show_help(void);
71static void show_license(void);
637b5326 72static int opt_default(const char *opt, const char *arg);
85f07f22
FB
73
74#define MAX_FILES 20
75
76static AVFormatContext *input_files[MAX_FILES];
a6a92a9a 77static int64_t input_files_ts_offset[MAX_FILES];
85f07f22
FB
78static int nb_input_files = 0;
79
80static AVFormatContext *output_files[MAX_FILES];
81static int nb_output_files = 0;
82
83static AVStreamMap stream_maps[MAX_FILES];
84static int nb_stream_maps;
85
0a38bafd
PB
86static AVMetaDataMap meta_data_maps[MAX_FILES];
87static int nb_meta_data_maps;
88
79fdaa4c
FB
89static AVInputFormat *file_iformat;
90static AVOutputFormat *file_oformat;
817b23ff 91static AVImageFormat *image_format;
55cf1959
MN
92static int frame_width = 0;
93static int frame_height = 0;
880e8ba7 94static float frame_aspect_ratio = 0;
644a9262 95static enum PixelFormat frame_pix_fmt = PIX_FMT_NONE;
1ff93ffc
TK
96static int frame_padtop = 0;
97static int frame_padbottom = 0;
98static int frame_padleft = 0;
99static int frame_padright = 0;
100static int padcolor[3] = {16,128,128}; /* default to black */
ab6d194a
MN
101static int frame_topBand = 0;
102static int frame_bottomBand = 0;
103static int frame_leftBand = 0;
104static int frame_rightBand = 0;
cf7fc795 105static int max_frames[4] = {INT_MAX, INT_MAX, INT_MAX, INT_MAX};
14bea432
MN
106static int frame_rate = 25;
107static int frame_rate_base = 1;
158c7f05 108static float video_qscale = 0;
3aa102be
MN
109static int video_qmin = 2;
110static int video_qmax = 31;
4ea4b274
MN
111static int video_lmin = 2*FF_QP2LAMBDA;
112static int video_lmax = 31*FF_QP2LAMBDA;
6e0d8c06
MN
113static int video_mb_lmin = 2*FF_QP2LAMBDA;
114static int video_mb_lmax = 31*FF_QP2LAMBDA;
9cdd6a24 115static int video_qdiff = 3;
6a5d4395 116static float video_qsquish = 0.0;
84f608f4
VM
117static uint16_t *intra_matrix = NULL;
118static uint16_t *inter_matrix = NULL;
ac2830ec 119#if 0 //experimental, (can be removed)
3aa102be
MN
120static float video_rc_qsquish=1.0;
121static float video_rc_qmod_amp=0;
122static int video_rc_qmod_freq=0;
ac2830ec 123#endif
3aa102be
MN
124static char *video_rc_override_string=NULL;
125static char *video_rc_eq="tex^qComp";
126static int video_rc_buffer_size=0;
127static float video_rc_buffer_aggressivity=1.0;
3bea5386 128static int me_method = ME_EPZS;
85f07f22 129static int video_disable = 0;
f3356e9c 130static int video_discard = 0;
85f07f22 131static int video_codec_id = CODEC_ID_NONE;
b2a2197e 132static int video_codec_tag = 0;
85f07f22 133static int same_quality = 0;
bc6caae2 134static int b_frames = 0;
cfcf0ffd 135static int do_deinterlace = 0;
4d2858de 136static int workaround_bugs = FF_BUG_AUTODETECT;
1dbb6d90 137static int packet_size = 0;
7ebfc0ea 138static int error_rate = 0;
f560dd82 139static int strict = 0;
bb198e19 140static int top_field_first = -1;
303e50e6 141static int sc_threshold = 0;
f4f3223f 142static int me_threshold = 0;
f20f8a8b 143static int mb_threshold = 0;
1a11cbcc 144static int intra_dc_precision = 8;
09c3e44e 145static int me_penalty_compensation= 256;
bbf18b21
MN
146static int frame_skip_threshold= 0;
147static int frame_skip_factor= 0;
0fd6aea1 148static int frame_skip_exp= 0;
5894e1bb 149static int loop_input = 0;
8108551a 150static int loop_output = AVFMT_NOOUTPUTLOOP;
0888fd22 151static int qp_hist = 0;
85f07f22 152
85f07f22
FB
153static int intra_only = 0;
154static int audio_sample_rate = 44100;
155static int audio_bit_rate = 64000;
c57c770d
JR
156#define QSCALE_NONE -99999
157static float audio_qscale = QSCALE_NONE;
85f07f22
FB
158static int audio_disable = 0;
159static int audio_channels = 1;
160static int audio_codec_id = CODEC_ID_NONE;
b2a2197e 161static int audio_codec_tag = 0;
cf7fc795
FB
162static char *audio_language = NULL;
163
164static int subtitle_codec_id = CODEC_ID_NONE;
165static char *subtitle_language = NULL;
85f07f22 166
2db3c638
MN
167static int mux_rate= 0;
168static int mux_packet_size= 0;
17c88cb0
MN
169static float mux_preload= 0.5;
170static float mux_max_delay= 0.7;
2db3c638 171
0c1a9eda 172static int64_t recording_time = 0;
8831db5c 173static int64_t start_time = 0;
4568325a 174static int64_t rec_timestamp = 0;
a6a92a9a 175static int64_t input_ts_offset = 0;
85f07f22
FB
176static int file_overwrite = 0;
177static char *str_title = NULL;
178static char *str_author = NULL;
179static char *str_copyright = NULL;
180static char *str_comment = NULL;
63d5075c 181static char *str_album = NULL;
5727b222 182static int do_benchmark = 0;
a0663ba4 183static int do_hex_dump = 0;
254abc2e 184static int do_pkt_dump = 0;
43f1708f 185static int do_psnr = 0;
ce7c56c2 186static int do_vstats = 0;
5abdb4b1
FB
187static int do_pass = 0;
188static char *pass_logfilename = NULL;
1629626f
FB
189static int audio_stream_copy = 0;
190static int video_stream_copy = 0;
cf7fc795 191static int subtitle_stream_copy = 0;
986ebcdb
MN
192static int video_sync_method= 1;
193static int audio_sync_method= 0;
72bd8100 194static int copy_ts= 0;
ca80810b 195static int opt_shortest = 0; //
90ad92b3 196static int video_global_header = 0;
5abdb4b1 197
bdfcbbed
MK
198static int rate_emu = 0;
199
6beefa40 200#ifdef CONFIG_BKTR
7b49ce2e 201static const char *video_grab_format = "bktr";
6beefa40 202#else
0a7b514f 203#ifdef CONFIG_VIDEO4LINUX2
7b49ce2e 204static const char *video_grab_format = "video4linux2";
0a7b514f 205#else
7b49ce2e 206static const char *video_grab_format = "video4linux";
6beefa40 207#endif
0a7b514f 208#endif
79a7c268 209static char *video_device = NULL;
2639c651 210static char *grab_device = NULL;
a5df11ab 211static int video_channel = 0;
e3ee3283 212static char *video_standard = "ntsc";
79a7c268 213
7b49ce2e 214static const char *audio_grab_format = "audio_device";
79a7c268 215static char *audio_device = NULL;
a9aa3467 216static int audio_volume = 256;
8aa3ee32 217
d9a916e2 218static int using_stdin = 0;
bee0d9e5 219static int using_vhook = 0;
f068206e 220static int verbose = 1;
9c3d33d6 221static int thread_count= 1;
b51469a0 222static int q_pressed = 0;
1008ceb3
MN
223static int64_t video_size = 0;
224static int64_t audio_size = 0;
225static int64_t extra_size = 0;
a6a92a9a
WG
226static int nb_frames_dup = 0;
227static int nb_frames_drop = 0;
6e454c38 228static int input_sync;
b6e16b86 229static int limit_filesize = 0; //
d9a916e2 230
5b6d5596 231static int pgmyuv_compatibility_hack=0;
023857be 232static int dts_delta_threshold = 10;
5b6d5596 233
18a54b04
LA
234static int sws_flags = SWS_BICUBIC;
235
8bbf6db9
MN
236const char **opt_names=NULL;
237int opt_name_count=0;
238AVCodecContext *avctx_opts;
4eb72c6b 239AVFormatContext *avformat_opts;
8bbf6db9 240
748c2fca
MN
241static AVBitStreamFilterContext *video_bitstream_filters=NULL;
242static AVBitStreamFilterContext *audio_bitstream_filters=NULL;
243static AVBitStreamFilterContext *bitstream_filters[MAX_FILES][MAX_STREAMS];
5b6d5596 244
5abdb4b1 245#define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
85f07f22 246
b4a3389e
WG
247struct AVInputStream;
248
85f07f22
FB
249typedef struct AVOutputStream {
250 int file_index; /* file index */
251 int index; /* stream index in the output file */
252 int source_index; /* AVInputStream index */
253 AVStream *st; /* stream in the output file */
ec5517d5
FB
254 int encoding_needed; /* true if encoding needed for this stream */
255 int frame_number;
256 /* input pts and corresponding output pts
257 for A/V sync */
b4a3389e
WG
258 //double sync_ipts; /* dts from the AVPacket of the demuxer in second units */
259 struct AVInputStream *sync_ist; /* input stream to sync against */
e928649b 260 int64_t sync_opts; /* output frame counter, could be changed to some true timestamp */ //FIXME look at frame_number
85f07f22 261 /* video only */
07d0cdfc 262 int video_resample;
a4d36c11 263 AVFrame pict_tmp; /* temporary image for resampling */
18a54b04
LA
264 struct SwsContext *img_resample_ctx; /* for image resampling */
265 int resample_height;
34b10a57 266
07d0cdfc 267 int video_crop;
34b10a57
D
268 int topBand; /* cropping area sizes */
269 int leftBand;
115329f1 270
07d0cdfc 271 int video_pad;
1ff93ffc
TK
272 int padtop; /* padding area sizes */
273 int padbottom;
274 int padleft;
275 int padright;
115329f1 276
85f07f22
FB
277 /* audio only */
278 int audio_resample;
279 ReSampleContext *resample; /* for audio resampling */
280 FifoBuffer fifo; /* for compression: one audio fifo per codec */
5abdb4b1 281 FILE *logfile;
85f07f22
FB
282} AVOutputStream;
283
284typedef struct AVInputStream {
285 int file_index;
286 int index;
287 AVStream *st;
288 int discard; /* true if stream data should be discarded */
289 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
0c1a9eda 290 int64_t sample_index; /* current sample */
bdfcbbed
MK
291
292 int64_t start; /* time when read started */
293 unsigned long frame; /* current frame */
254abc2e
FB
294 int64_t next_pts; /* synthetic pts for cases where pkt.pts
295 is not defined */
e7d0374f 296 int64_t pts; /* current pts */
ff4905a5 297 int is_start; /* is 1 at the start and after a discontinuity */
85f07f22
FB
298} AVInputStream;
299
300typedef struct AVInputFile {
301 int eof_reached; /* true if eof reached */
302 int ist_index; /* index of first stream in ist_table */
303 int buffer_size; /* current total buffer size */
304 int buffer_size_max; /* buffer size at which we consider we can stop
305 buffering */
79fdaa4c 306 int nb_streams; /* nb streams we are aware of */
85f07f22
FB
307} AVInputFile;
308
2f30a81d 309#ifndef __MINGW32__
bdc4796f 310
85f07f22
FB
311/* init terminal so that we can grab keys */
312static struct termios oldtty;
313
314static void term_exit(void)
315{
316 tcsetattr (0, TCSANOW, &oldtty);
317}
318
9680a722
RP
319static volatile sig_atomic_t received_sigterm = 0;
320
321static void
322sigterm_handler(int sig)
323{
324 received_sigterm = sig;
325 term_exit();
326}
327
85f07f22
FB
328static void term_init(void)
329{
330 struct termios tty;
331
332 tcgetattr (0, &tty);
333 oldtty = tty;
334
335 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
336 |INLCR|IGNCR|ICRNL|IXON);
337 tty.c_oflag |= OPOST;
338 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
339 tty.c_cflag &= ~(CSIZE|PARENB);
340 tty.c_cflag |= CS8;
341 tty.c_cc[VMIN] = 1;
342 tty.c_cc[VTIME] = 0;
115329f1 343
85f07f22
FB
344 tcsetattr (0, TCSANOW, &tty);
345
9680a722
RP
346 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
347 signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
348 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
349 /*
350 register a function to be called at normal program termination
351 */
85f07f22 352 atexit(term_exit);
9ddd71fc
FR
353#ifdef CONFIG_BEOS_NETSERVER
354 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
355#endif
85f07f22
FB
356}
357
358/* read a key without blocking */
359static int read_key(void)
360{
9ddd71fc 361 int n = 1;
85f07f22 362 unsigned char ch;
9ddd71fc
FR
363#ifndef CONFIG_BEOS_NETSERVER
364 struct timeval tv;
85f07f22
FB
365 fd_set rfds;
366
367 FD_ZERO(&rfds);
368 FD_SET(0, &rfds);
369 tv.tv_sec = 0;
370 tv.tv_usec = 0;
371 n = select(1, &rfds, NULL, NULL, &tv);
9ddd71fc 372#endif
85f07f22 373 if (n > 0) {
cb09b2ed
PG
374 n = read(0, &ch, 1);
375 if (n == 1)
85f07f22 376 return ch;
cb09b2ed
PG
377
378 return n;
85f07f22
FB
379 }
380 return -1;
381}
382
b51469a0
LS
383static int decode_interrupt_cb(void)
384{
385 return q_pressed || (q_pressed = read_key() == 'q');
386}
387
a38469e1 388#else
85f07f22 389
bee0d9e5
CY
390static volatile int received_sigterm = 0;
391
a38469e1
FB
392/* no interactive support */
393static void term_exit(void)
85f07f22 394{
a38469e1 395}
85f07f22 396
a38469e1
FB
397static void term_init(void)
398{
399}
85f07f22 400
a38469e1
FB
401static int read_key(void)
402{
cb09b2ed 403 return 0;
85f07f22
FB
404}
405
a38469e1 406#endif
bdc4796f 407
b29f97d1 408static int read_ffserver_streams(AVFormatContext *s, const char *filename)
85f07f22 409{
79fdaa4c 410 int i, err;
85f07f22
FB
411 AVFormatContext *ic;
412
79fdaa4c
FB
413 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
414 if (err < 0)
415 return err;
85f07f22
FB
416 /* copy stream format */
417 s->nb_streams = ic->nb_streams;
418 for(i=0;i<ic->nb_streams;i++) {
419 AVStream *st;
1e491e29 420
f37f8d4c 421 // FIXME: a more elegant solution is needed
e1031171 422 st = av_mallocz(sizeof(AVStream));
85f07f22 423 memcpy(st, ic->streams[i], sizeof(AVStream));
f37f8d4c
AB
424 st->codec = avcodec_alloc_context();
425 memcpy(st->codec, ic->streams[i]->codec, sizeof(AVCodecContext));
85f07f22
FB
426 s->streams[i] = st;
427 }
428
429 av_close_input_file(ic);
430 return 0;
431}
432
b4a3389e
WG
433static double
434get_sync_ipts(const AVOutputStream *ost)
435{
436 const AVInputStream *ist = ost->sync_ist;
437 return (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/AV_TIME_BASE;
438}
439
748c2fca
MN
440static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
441 while(bsfc){
442 AVPacket new_pkt= *pkt;
443 int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
444 &new_pkt.data, &new_pkt.size,
445 pkt->data, pkt->size,
446 pkt->flags & PKT_FLAG_KEY);
447 if(a){
448 av_free_packet(pkt);
449 new_pkt.destruct= av_destruct_packet;
450 }
451 *pkt= new_pkt;
452
453 bsfc= bsfc->next;
454 }
455
456 av_interleaved_write_frame(s, pkt);
457}
458
817b23ff 459#define MAX_AUDIO_PACKET_SIZE (128 * 1024)
85f07f22 460
115329f1
DB
461static void do_audio_out(AVFormatContext *s,
462 AVOutputStream *ost,
85f07f22
FB
463 AVInputStream *ist,
464 unsigned char *buf, int size)
465{
0c1a9eda 466 uint8_t *buftmp;
d66c7abc
SC
467 static uint8_t *audio_buf = NULL;
468 static uint8_t *audio_out = NULL;
558eae03 469 const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
d66c7abc 470
85f07f22 471 int size_out, frame_bytes, ret;
01f4895c 472 AVCodecContext *enc= ost->st->codec;
85f07f22 473
d66c7abc
SC
474 /* SC: dynamic allocation of buffers */
475 if (!audio_buf)
476 audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
477 if (!audio_out)
558eae03 478 audio_out = av_malloc(audio_out_size);
d66c7abc
SC
479 if (!audio_buf || !audio_out)
480 return; /* Should signal an error ! */
481
986ebcdb 482 if(audio_sync_method){
115329f1 483 double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
01f4895c
MN
484 - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec->channels * 2);
485 double idelta= delta*ist->st->codec->sample_rate / enc->sample_rate;
486 int byte_delta= ((int)idelta)*2*ist->st->codec->channels;
ff4905a5 487
986ebcdb
MN
488 //FIXME resample delay
489 if(fabs(delta) > 50){
ff4905a5
MN
490 if(ist->is_start){
491 if(byte_delta < 0){
f41dd5aa 492 byte_delta= FFMAX(byte_delta, -size);
ff4905a5
MN
493 size += byte_delta;
494 buf -= byte_delta;
495 if(verbose > 2)
496 fprintf(stderr, "discarding %d audio samples\n", (int)-delta);
497 if(!size)
498 return;
499 ist->is_start=0;
500 }else{
501 static uint8_t *input_tmp= NULL;
502 input_tmp= av_realloc(input_tmp, byte_delta + size);
503
504 if(byte_delta + size <= MAX_AUDIO_PACKET_SIZE)
505 ist->is_start=0;
506 else
507 byte_delta= MAX_AUDIO_PACKET_SIZE - size;
508
509 memset(input_tmp, 0, byte_delta);
510 memcpy(input_tmp + byte_delta, buf, size);
511 buf= input_tmp;
512 size += byte_delta;
513 if(verbose > 2)
514 fprintf(stderr, "adding %d audio samples of silence\n", (int)delta);
515 }
516 }else if(audio_sync_method>1){
517 int comp= clip(delta, -audio_sync_method, audio_sync_method);
518 assert(ost->audio_resample);
519 if(verbose > 2)
520 fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
01f4895c 521// 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));
ff4905a5
MN
522 av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
523 }
115329f1 524 }
986ebcdb 525 }else
b4a3389e 526 ost->sync_opts= lrintf(get_sync_ipts(ost) * enc->sample_rate)
01f4895c 527 - fifo_size(&ost->fifo, ost->fifo.rptr)/(ost->st->codec->channels * 2); //FIXME wrong
85f07f22
FB
528
529 if (ost->audio_resample) {
530 buftmp = audio_buf;
115329f1 531 size_out = audio_resample(ost->resample,
85f07f22 532 (short *)buftmp, (short *)buf,
01f4895c 533 size / (ist->st->codec->channels * 2));
85f07f22
FB
534 size_out = size_out * enc->channels * 2;
535 } else {
536 buftmp = buf;
537 size_out = size;
538 }
539
540 /* now encode as many frames as possible */
a0663ba4 541 if (enc->frame_size > 1) {
85f07f22 542 /* output resampled raw samples */
115329f1 543 fifo_write(&ost->fifo, buftmp, size_out,
85f07f22
FB
544 &ost->fifo.wptr);
545
546 frame_bytes = enc->frame_size * 2 * enc->channels;
115329f1
DB
547
548 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
85f07f22 549 &ost->fifo.rptr) == 0) {
e928649b
MN
550 AVPacket pkt;
551 av_init_packet(&pkt);
552
115329f1 553 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
a0663ba4 554 (short *)audio_buf);
1008ceb3 555 audio_size += ret;
e928649b
MN
556 pkt.stream_index= ost->index;
557 pkt.data= audio_out;
558 pkt.size= ret;
e7902f20 559 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 560 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
e928649b 561 pkt.flags |= PKT_FLAG_KEY;
748c2fca 562 write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
115329f1 563
986ebcdb 564 ost->sync_opts += enc->frame_size;
85f07f22
FB
565 }
566 } else {
e928649b
MN
567 AVPacket pkt;
568 av_init_packet(&pkt);
986ebcdb 569
c43042a7 570 ost->sync_opts += size_out / (2 * enc->channels);
986ebcdb 571
a0663ba4
FB
572 /* output a pcm frame */
573 /* XXX: change encoding codec API to avoid this ? */
574 switch(enc->codec->id) {
b461b3bc
RD
575 case CODEC_ID_PCM_S32LE:
576 case CODEC_ID_PCM_S32BE:
577 case CODEC_ID_PCM_U32LE:
578 case CODEC_ID_PCM_U32BE:
579 size_out = size_out << 1;
580 break;
581 case CODEC_ID_PCM_S24LE:
582 case CODEC_ID_PCM_S24BE:
583 case CODEC_ID_PCM_U24LE:
584 case CODEC_ID_PCM_U24BE:
585 case CODEC_ID_PCM_S24DAUD:
586 size_out = size_out / 2 * 3;
587 break;
a0663ba4
FB
588 case CODEC_ID_PCM_S16LE:
589 case CODEC_ID_PCM_S16BE:
590 case CODEC_ID_PCM_U16LE:
591 case CODEC_ID_PCM_U16BE:
592 break;
593 default:
594 size_out = size_out >> 1;
595 break;
596 }
115329f1 597 ret = avcodec_encode_audio(enc, audio_out, size_out,
bb270c08 598 (short *)buftmp);
1008ceb3 599 audio_size += ret;
e928649b
MN
600 pkt.stream_index= ost->index;
601 pkt.data= audio_out;
602 pkt.size= ret;
e7902f20 603 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 604 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
e928649b 605 pkt.flags |= PKT_FLAG_KEY;
748c2fca 606 write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
85f07f22
FB
607 }
608}
609
10d104e4
PG
610static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
611{
612 AVCodecContext *dec;
613 AVPicture *picture2;
614 AVPicture picture_tmp;
0c1a9eda 615 uint8_t *buf = 0;
10d104e4 616
01f4895c 617 dec = ist->st->codec;
10d104e4
PG
618
619 /* deinterlace : must be done before any resize */
bee0d9e5 620 if (do_deinterlace || using_vhook) {
10d104e4
PG
621 int size;
622
623 /* create temporary picture */
624 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
625 buf = av_malloc(size);
626 if (!buf)
627 return;
115329f1 628
10d104e4
PG
629 picture2 = &picture_tmp;
630 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
631
dafc3856 632 if (do_deinterlace){
115329f1 633 if(avpicture_deinterlace(picture2, picture,
dafc3856
MN
634 dec->pix_fmt, dec->width, dec->height) < 0) {
635 /* if error, do not deinterlace */
636 av_free(buf);
637 buf = NULL;
638 picture2 = picture;
639 }
640 } else {
c17be817 641 img_copy(picture2, picture, dec->pix_fmt, dec->width, dec->height);
bee0d9e5 642 }
10d104e4
PG
643 } else {
644 picture2 = picture;
645 }
646
647 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
648
649 if (picture != picture2)
650 *picture = *picture2;
651 *bufp = buf;
652}
653
ec5517d5
FB
654/* we begin to correct av delay at this threshold */
655#define AV_DELAY_MAX 0.100
85f07f22 656
115329f1
DB
657static void do_subtitle_out(AVFormatContext *s,
658 AVOutputStream *ost,
cf7fc795
FB
659 AVInputStream *ist,
660 AVSubtitle *sub,
661 int64_t pts)
662{
663 static uint8_t *subtitle_out = NULL;
664 int subtitle_out_max_size = 65536;
665 int subtitle_out_size, nb, i;
666 AVCodecContext *enc;
667 AVPacket pkt;
668
669 if (pts == AV_NOPTS_VALUE) {
670 fprintf(stderr, "Subtitle packets must have a pts\n");
671 return;
672 }
673
01f4895c 674 enc = ost->st->codec;
cf7fc795
FB
675
676 if (!subtitle_out) {
677 subtitle_out = av_malloc(subtitle_out_max_size);
678 }
679
680 /* Note: DVB subtitle need one packet to draw them and one other
681 packet to clear them */
682 /* XXX: signal it in the codec context ? */
683 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE)
684 nb = 2;
685 else
686 nb = 1;
687
688 for(i = 0; i < nb; i++) {
115329f1 689 subtitle_out_size = avcodec_encode_subtitle(enc, subtitle_out,
cf7fc795 690 subtitle_out_max_size, sub);
115329f1 691
cf7fc795
FB
692 av_init_packet(&pkt);
693 pkt.stream_index = ost->index;
694 pkt.data = subtitle_out;
695 pkt.size = subtitle_out_size;
696 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);
697 if (enc->codec_id == CODEC_ID_DVB_SUBTITLE) {
698 /* XXX: the pts correction is handled here. Maybe handling
699 it in the codec would be better */
700 if (i == 0)
701 pkt.pts += 90 * sub->start_display_time;
702 else
703 pkt.pts += 90 * sub->end_display_time;
704 }
748c2fca 705 write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
cf7fc795
FB
706 }
707}
708
8a6cb114 709static int bit_buffer_size= 1024*256;
27537106 710static uint8_t *bit_buffer= NULL;
1ff93ffc 711
115329f1
DB
712static void do_video_out(AVFormatContext *s,
713 AVOutputStream *ost,
85f07f22 714 AVInputStream *ist,
7a0f9d7e 715 AVFrame *in_picture,
986ebcdb 716 int *frame_size)
85f07f22 717{
ec5517d5 718 int nb_frames, i, ret;
07d0cdfc 719 AVFrame *final_picture, *formatted_picture, *resampling_dst, *padding_src;
18a54b04 720 AVFrame picture_crop_temp, picture_pad_temp;
0c1a9eda 721 uint8_t *buf = NULL, *buf1 = NULL;
cfcf0ffd 722 AVCodecContext *enc, *dec;
115329f1 723
a4d36c11 724 avcodec_get_frame_defaults(&picture_crop_temp);
07d0cdfc 725 avcodec_get_frame_defaults(&picture_pad_temp);
a4d36c11 726
01f4895c
MN
727 enc = ost->st->codec;
728 dec = ist->st->codec;
85f07f22 729
ec5517d5
FB
730 /* by default, we output a single frame */
731 nb_frames = 1;
732
204c0f48
PG
733 *frame_size = 0;
734
986ebcdb 735 if(video_sync_method){
10d104e4 736 double vdelta;
b4a3389e 737 vdelta = get_sync_ipts(ost) / av_q2d(enc->time_base) - ost->sync_opts;
e928649b
MN
738 //FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
739 if (vdelta < -1.1)
740 nb_frames = 0;
741 else if (vdelta > 1.1)
70122f29 742 nb_frames = lrintf(vdelta);
72bd8100 743//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);
50c3dd32
MN
744 if (nb_frames == 0){
745 ++nb_frames_drop;
746 if (verbose>2)
747 fprintf(stderr, "*** drop!\n");
8300609b
MN
748 }else if (nb_frames > 1) {
749 nb_frames_dup += nb_frames;
50c3dd32 750 if (verbose>2)
8300609b 751 fprintf(stderr, "*** %d dup!\n", nb_frames-1);
50c3dd32
MN
752 }
753 }else
b4a3389e 754 ost->sync_opts= lrintf(get_sync_ipts(ost) / av_q2d(enc->time_base));
445f1b83 755
7ca60994 756 nb_frames= FFMIN(nb_frames, max_frames[CODEC_TYPE_VIDEO] - ost->frame_number);
115329f1 757 if (nb_frames <= 0)
85f07f22 758 return;
ce7c56c2 759
07d0cdfc 760 if (ost->video_crop) {
18a54b04 761 if (img_crop((AVPicture *)&picture_crop_temp, (AVPicture *)in_picture, dec->pix_fmt, ost->topBand, ost->leftBand) < 0) {
07d0cdfc
LA
762 av_log(NULL, AV_LOG_ERROR, "error cropping picture\n");
763 goto the_end;
764 }
765 formatted_picture = &picture_crop_temp;
18a54b04
LA
766 } else {
767 formatted_picture = in_picture;
07d0cdfc
LA
768 }
769
770 final_picture = formatted_picture;
771 padding_src = formatted_picture;
772 resampling_dst = &ost->pict_tmp;
773 if (ost->video_pad) {
774 final_picture = &ost->pict_tmp;
775 if (ost->video_resample) {
18a54b04 776 if (img_crop((AVPicture *)&picture_pad_temp, (AVPicture *)final_picture, enc->pix_fmt, ost->padtop, ost->padleft) < 0) {
07d0cdfc
LA
777 av_log(NULL, AV_LOG_ERROR, "error padding picture\n");
778 goto the_end;
779 }
780 resampling_dst = &picture_pad_temp;
781 }
782 }
783
85f07f22 784 if (ost->video_resample) {
07d0cdfc 785 padding_src = NULL;
34b10a57 786 final_picture = &ost->pict_tmp;
18a54b04
LA
787 sws_scale(ost->img_resample_ctx, formatted_picture->data, formatted_picture->linesize,
788 0, ost->resample_height, resampling_dst->data, resampling_dst->linesize);
f122deb4 789 }
07d0cdfc
LA
790
791 if (ost->video_pad) {
792 img_pad((AVPicture*)final_picture, (AVPicture *)padding_src,
793 enc->height, enc->width, enc->pix_fmt,
794 ost->padtop, ost->padbottom, ost->padleft, ost->padright, padcolor);
85f07f22 795 }
07d0cdfc 796
85f07f22 797 /* duplicates frame if needed */
ec5517d5 798 for(i=0;i<nb_frames;i++) {
e928649b
MN
799 AVPacket pkt;
800 av_init_packet(&pkt);
801 pkt.stream_index= ost->index;
802
e8750b00
FR
803 if (s->oformat->flags & AVFMT_RAWPICTURE) {
804 /* raw pictures are written as AVPicture structure to
805 avoid any copies. We support temorarily the older
806 method. */
2744ca9a 807 AVFrame* old_frame = enc->coded_frame;
bb270c08 808 enc->coded_frame = dec->coded_frame; //FIXME/XXX remove this hack
e928649b
MN
809 pkt.data= (uint8_t *)final_picture;
810 pkt.size= sizeof(AVPicture);
e7902f20 811 if(dec->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 812 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
e928649b
MN
813 if(dec->coded_frame && dec->coded_frame->key_frame)
814 pkt.flags |= PKT_FLAG_KEY;
815
748c2fca 816 write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
bb270c08 817 enc->coded_frame = old_frame;
e8750b00 818 } else {
492cd3a9 819 AVFrame big_picture;
a4d36c11
MN
820
821 big_picture= *final_picture;
7a0f9d7e
FB
822 /* better than nothing: use input picture interlaced
823 settings */
824 big_picture.interlaced_frame = in_picture->interlaced_frame;
637b5326 825 if(avctx_opts->flags & (CODEC_FLAG_INTERLACED_DCT|CODEC_FLAG_INTERLACED_ME)){
bb198e19
MN
826 if(top_field_first == -1)
827 big_picture.top_field_first = in_picture->top_field_first;
828 else
2a8edc5d 829 big_picture.top_field_first = top_field_first;
bb198e19 830 }
7a0f9d7e 831
85f07f22
FB
832 /* handles sameq here. This is not correct because it may
833 not be a global option */
834 if (same_quality) {
1e491e29
MN
835 big_picture.quality = ist->st->quality;
836 }else
837 big_picture.quality = ost->st->quality;
f4f3223f
MN
838 if(!me_threshold)
839 big_picture.pict_type = 0;
50c3dd32 840// big_picture.pts = AV_NOPTS_VALUE;
c0df9d75
MN
841 big_picture.pts= ost->sync_opts;
842// big_picture.pts= av_rescale(ost->sync_opts, AV_TIME_BASE*(int64_t)enc->time_base.num, enc->time_base.den);
50c3dd32 843//av_log(NULL, AV_LOG_DEBUG, "%lld -> encoder\n", ost->sync_opts);
115329f1 844 ret = avcodec_encode_video(enc,
8a6cb114 845 bit_buffer, bit_buffer_size,
1e491e29 846 &big_picture);
44429457 847 //enc->frame_number = enc->real_pict_num;
4bbc6260 848 if(ret>0){
27537106 849 pkt.data= bit_buffer;
e928649b 850 pkt.size= ret;
e7902f20 851 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 852 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
115329f1 853/*av_log(NULL, AV_LOG_DEBUG, "encoder -> %lld/%lld\n",
c0df9d75
MN
854 pkt.pts != AV_NOPTS_VALUE ? av_rescale(pkt.pts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1,
855 pkt.dts != AV_NOPTS_VALUE ? av_rescale(pkt.dts, enc->time_base.den, AV_TIME_BASE*(int64_t)enc->time_base.num) : -1);*/
50c3dd32 856
e928649b
MN
857 if(enc->coded_frame && enc->coded_frame->key_frame)
858 pkt.flags |= PKT_FLAG_KEY;
748c2fca 859 write_frame(s, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
e928649b
MN
860 *frame_size = ret;
861 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
862 // enc->frame_number-1, enc->real_pict_num, ret,
863 // enc->pict_type);
864 /* if two pass, output log */
865 if (ost->logfile && enc->stats_out) {
866 fprintf(ost->logfile, "%s", enc->stats_out);
867 }
5abdb4b1 868 }
85f07f22 869 }
50c3dd32 870 ost->sync_opts++;
ec5517d5 871 ost->frame_number++;
85f07f22 872 }
ec5517d5 873 the_end:
0f1578af
FB
874 av_free(buf);
875 av_free(buf1);
85f07f22
FB
876}
877
140cb663
MN
878static double psnr(double d){
879 if(d==0) return INFINITY;
b29f97d1 880 return -10.0*log(d)/log(10.0);
140cb663
MN
881}
882
115329f1 883static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
ec5517d5 884 int frame_size)
ce7c56c2
J
885{
886 static FILE *fvstats=NULL;
ce7c56c2 887 char filename[40];
bf5af568
FB
888 time_t today2;
889 struct tm *today;
ce7c56c2
J
890 AVCodecContext *enc;
891 int frame_number;
0c1a9eda 892 int64_t ti;
ce7c56c2 893 double ti1, bitrate, avg_bitrate;
115329f1 894
ce7c56c2
J
895 if (!fvstats) {
896 today2 = time(NULL);
897 today = localtime(&today2);
2fc8ea24 898 snprintf(filename, sizeof(filename), "vstats_%02d%02d%02d.log", today->tm_hour,
ce7c56c2
J
899 today->tm_min,
900 today->tm_sec);
901 fvstats = fopen(filename,"w");
902 if (!fvstats) {
903 perror("fopen");
904 exit(1);
905 }
906 }
115329f1 907
ce7c56c2 908 ti = MAXINT64;
01f4895c 909 enc = ost->st->codec;
ce7c56c2 910 if (enc->codec_type == CODEC_TYPE_VIDEO) {
ec5517d5 911 frame_number = ost->frame_number;
158c7f05 912 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
140cb663 913 if (enc->flags&CODEC_FLAG_PSNR)
492cd3a9 914 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
115329f1 915
ce7c56c2 916 fprintf(fvstats,"f_size= %6d ", frame_size);
ec5517d5 917 /* compute pts value */
c0df9d75 918 ti1 = ost->sync_opts * av_q2d(enc->time_base);
ce7c56c2
J
919 if (ti1 < 0.01)
920 ti1 = 0.01;
115329f1 921
c0df9d75 922 bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
1008ceb3 923 avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
ce7c56c2 924 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
1008ceb3 925 (double)video_size / 1024, ti1, bitrate, avg_bitrate);
115329f1 926 fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
ce7c56c2 927 }
ec5517d5
FB
928}
929
b29f97d1 930static void print_report(AVFormatContext **output_files,
bb270c08
DB
931 AVOutputStream **ost_table, int nb_ostreams,
932 int is_last_report)
ec5517d5
FB
933{
934 char buf[1024];
935 AVOutputStream *ost;
936 AVFormatContext *oc, *os;
0c1a9eda 937 int64_t total_size;
ec5517d5
FB
938 AVCodecContext *enc;
939 int frame_number, vid, i;
940 double bitrate, ti1, pts;
0c1a9eda 941 static int64_t last_time = -1;
0888fd22 942 static int qp_histogram[52];
115329f1 943
ec5517d5 944 if (!is_last_report) {
0c1a9eda 945 int64_t cur_time;
ec5517d5
FB
946 /* display the report every 0.5 seconds */
947 cur_time = av_gettime();
948 if (last_time == -1) {
949 last_time = cur_time;
950 return;
115329f1 951 }
ec5517d5
FB
952 if ((cur_time - last_time) < 500000)
953 return;
954 last_time = cur_time;
955 }
956
ce7c56c2 957
ec5517d5
FB
958 oc = output_files[0];
959
960 total_size = url_ftell(&oc->pb);
115329f1 961
ec5517d5
FB
962 buf[0] = '\0';
963 ti1 = 1e10;
964 vid = 0;
965 for(i=0;i<nb_ostreams;i++) {
966 ost = ost_table[i];
967 os = output_files[ost->file_index];
01f4895c 968 enc = ost->st->codec;
10d104e4 969 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
0ecca7a4 970 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "q=%2.1f ",
158c7f05 971 enc->coded_frame->quality/(float)FF_QP2LAMBDA);
10d104e4 972 }
ec5517d5
FB
973 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
974 frame_number = ost->frame_number;
0888fd22
MN
975 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d q=%3.1f ",
976 frame_number, enc->coded_frame ? enc->coded_frame->quality/(float)FF_QP2LAMBDA : -1);
890972be 977 if(is_last_report)
0ecca7a4 978 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
0888fd22
MN
979 if(qp_hist && enc->coded_frame){
980 int j;
981 int qp= lrintf(enc->coded_frame->quality/(float)FF_QP2LAMBDA);
982 if(qp>=0 && qp<sizeof(qp_histogram)/sizeof(int))
983 qp_histogram[qp]++;
984 for(j=0; j<32; j++)
985 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%X", (int)lrintf(log(qp_histogram[j]+1)/log(2)));
986 }
890972be
MN
987 if (enc->flags&CODEC_FLAG_PSNR){
988 int j;
989 double error, error_sum=0;
990 double scale, scale_sum=0;
991 char type[3]= {'Y','U','V'};
0ecca7a4 992 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "PSNR=");
890972be
MN
993 for(j=0; j<3; j++){
994 if(is_last_report){
995 error= enc->error[j];
996 scale= enc->width*enc->height*255.0*255.0*frame_number;
997 }else{
998 error= enc->coded_frame->error[j];
999 scale= enc->width*enc->height*255.0*255.0;
1000 }
1001 if(j) scale/=4;
1002 error_sum += error;
1003 scale_sum += scale;
0ecca7a4 1004 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
890972be 1005 }
0ecca7a4 1006 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
890972be 1007 }
ec5517d5
FB
1008 vid = 1;
1009 }
1010 /* compute min output value */
9ee91c2f 1011 pts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
5d9827bc 1012 if ((pts < ti1) && (pts > 0))
ec5517d5
FB
1013 ti1 = pts;
1014 }
1015 if (ti1 < 0.01)
1016 ti1 = 0.01;
115329f1 1017
f068206e
BE
1018 if (verbose || is_last_report) {
1019 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
115329f1
DB
1020
1021 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
ec5517d5
FB
1022 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1023 (double)total_size / 1024, ti1, bitrate);
a6a92a9a 1024
bb270c08
DB
1025 if (verbose > 1)
1026 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " dup=%d drop=%d",
1027 nb_frames_dup, nb_frames_drop);
115329f1 1028
d8019eb5
AD
1029 if (verbose >= 0)
1030 fprintf(stderr, "%s \r", buf);
1031
ec5517d5
FB
1032 fflush(stderr);
1033 }
115329f1 1034
1008ceb3
MN
1035 if (is_last_report && verbose >= 0){
1036 int64_t raw= audio_size + video_size + extra_size;
f068206e 1037 fprintf(stderr, "\n");
1008ceb3
MN
1038 fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
1039 video_size/1024.0,
1040 audio_size/1024.0,
1041 extra_size/1024.0,
1042 100.0*(total_size - raw)/raw
1043 );
1044 }
ce7c56c2
J
1045}
1046
a700a6ae
FB
1047/* pkt = NULL means EOF (needed to flush decoder buffers) */
1048static int output_packet(AVInputStream *ist, int ist_index,
1049 AVOutputStream **ost_table, int nb_ostreams,
4b85a28f 1050 const AVPacket *pkt)
a700a6ae
FB
1051{
1052 AVFormatContext *os;
1053 AVOutputStream *ost;
1054 uint8_t *ptr;
1055 int len, ret, i;
1056 uint8_t *data_buf;
1057 int data_size, got_picture;
1058 AVFrame picture;
a700a6ae 1059 void *buffer_to_free;
f038fe8b 1060 static unsigned int samples_size= 0;
78953e62 1061 static short *samples= NULL;
cf7fc795
FB
1062 AVSubtitle subtitle, *subtitle_to_free;
1063 int got_subtitle;
115329f1 1064
2b18dcd0
MN
1065 if(!pkt){
1066 ist->pts= ist->next_pts; // needed for last packet if vsync=0
1067 } else if (pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
c0df9d75 1068 ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
a700a6ae 1069 } else {
a603bf8d 1070// assert(ist->pts == ist->next_pts);
a700a6ae 1071 }
115329f1 1072
a700a6ae
FB
1073 if (pkt == NULL) {
1074 /* EOF handling */
1075 ptr = NULL;
1076 len = 0;
1077 goto handle_eof;
1078 }
1079
1080 len = pkt->size;
1081 ptr = pkt->data;
1082 while (len > 0) {
1083 handle_eof:
1084 /* decode the packet if needed */
1085 data_buf = NULL; /* fail safe */
1086 data_size = 0;
cf7fc795 1087 subtitle_to_free = NULL;
a700a6ae 1088 if (ist->decoding_needed) {
01f4895c 1089 switch(ist->st->codec->codec_type) {
df84ac2e 1090 case CODEC_TYPE_AUDIO:{
115329f1 1091 if(pkt)
78953e62 1092 samples= av_fast_realloc(samples, &samples_size, FFMAX(pkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE));
a700a6ae
FB
1093 /* XXX: could avoid copy if PCM 16 bits with same
1094 endianness as CPU */
01f4895c 1095 ret = avcodec_decode_audio(ist->st->codec, samples, &data_size,
a700a6ae
FB
1096 ptr, len);
1097 if (ret < 0)
1098 goto fail_decode;
1099 ptr += ret;
1100 len -= ret;
1101 /* Some bug in mpeg audio decoder gives */
1102 /* data_size < 0, it seems they are overflows */
1103 if (data_size <= 0) {
1104 /* no audio frame */
1105 continue;
1106 }
1107 data_buf = (uint8_t *)samples;
115329f1 1108 ist->next_pts += ((int64_t)AV_TIME_BASE/2 * data_size) /
01f4895c 1109 (ist->st->codec->sample_rate * ist->st->codec->channels);
df84ac2e 1110 break;}
a700a6ae 1111 case CODEC_TYPE_VIDEO:
01f4895c 1112 data_size = (ist->st->codec->width * ist->st->codec->height * 3) / 2;
a700a6ae 1113 /* XXX: allocate picture correctly */
9740beff
MN
1114 avcodec_get_frame_defaults(&picture);
1115
115329f1 1116 ret = avcodec_decode_video(ist->st->codec,
a700a6ae
FB
1117 &picture, &got_picture, ptr, len);
1118 ist->st->quality= picture.quality;
115329f1 1119 if (ret < 0)
a700a6ae
FB
1120 goto fail_decode;
1121 if (!got_picture) {
1122 /* no picture yet */
1123 goto discard_packet;
1124 }
01f4895c 1125 if (ist->st->codec->time_base.num != 0) {
115329f1 1126 ist->next_pts += ((int64_t)AV_TIME_BASE *
01f4895c
MN
1127 ist->st->codec->time_base.num) /
1128 ist->st->codec->time_base.den;
a700a6ae
FB
1129 }
1130 len = 0;
1131 break;
cf7fc795 1132 case CODEC_TYPE_SUBTITLE:
115329f1 1133 ret = avcodec_decode_subtitle(ist->st->codec,
cf7fc795
FB
1134 &subtitle, &got_subtitle, ptr, len);
1135 if (ret < 0)
a700a6ae 1136 goto fail_decode;
cf7fc795
FB
1137 if (!got_subtitle) {
1138 goto discard_packet;
a700a6ae 1139 }
cf7fc795
FB
1140 subtitle_to_free = &subtitle;
1141 len = 0;
1142 break;
1143 default:
1144 goto fail_decode;
1145 }
1146 } else {
01f4895c 1147 switch(ist->st->codec->codec_type) {
2fef0bdf 1148 case CODEC_TYPE_AUDIO:
115329f1 1149 ist->next_pts += ((int64_t)AV_TIME_BASE * ist->st->codec->frame_size) /
01f4895c 1150 (ist->st->codec->sample_rate * ist->st->codec->channels);
2fef0bdf
MN
1151 break;
1152 case CODEC_TYPE_VIDEO:
01f4895c 1153 if (ist->st->codec->time_base.num != 0) {
115329f1 1154 ist->next_pts += ((int64_t)AV_TIME_BASE *
01f4895c
MN
1155 ist->st->codec->time_base.num) /
1156 ist->st->codec->time_base.den;
2fef0bdf
MN
1157 }
1158 break;
1159 }
a700a6ae
FB
1160 data_buf = ptr;
1161 data_size = len;
1162 ret = len;
1163 len = 0;
1164 }
1165
1166 buffer_to_free = NULL;
01f4895c 1167 if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO) {
115329f1 1168 pre_process_video_frame(ist, (AVPicture *)&picture,
a700a6ae
FB
1169 &buffer_to_free);
1170 }
1171
7e987c33 1172 // preprocess audio (volume)
01f4895c 1173 if (ist->st->codec->codec_type == CODEC_TYPE_AUDIO) {
a9aa3467 1174 if (audio_volume != 256) {
7e987c33 1175 short *volp;
7e987c33
C
1176 volp = samples;
1177 for(i=0;i<(data_size / sizeof(short));i++) {
a9aa3467 1178 int v = ((*volp) * audio_volume + 128) >> 8;
7e987c33
C
1179 if (v < -32768) v = -32768;
1180 if (v > 32767) v = 32767;
1181 *volp++ = v;
1182 }
1183 }
1184 }
1185
a700a6ae 1186 /* frame rate emulation */
01f4895c
MN
1187 if (ist->st->codec->rate_emu) {
1188 int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec->time_base.num, 1000000, ist->st->codec->time_base.den);
a700a6ae
FB
1189 int64_t now = av_gettime() - ist->start;
1190 if (pts > now)
1191 usleep(pts - now);
1192
1193 ist->frame++;
1194 }
1195
1196#if 0
1197 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1198 is the one of the next displayed one */
1199 /* XXX: add mpeg4 too ? */
01f4895c
MN
1200 if (ist->st->codec->codec_id == CODEC_ID_MPEG1VIDEO) {
1201 if (ist->st->codec->pict_type != B_TYPE) {
a700a6ae
FB
1202 int64_t tmp;
1203 tmp = ist->last_ip_pts;
1204 ist->last_ip_pts = ist->frac_pts.val;
1205 ist->frac_pts.val = tmp;
1206 }
1207 }
1208#endif
115329f1 1209 /* if output time reached then transcode raw format,
bb270c08 1210 encode packets and output them */
a700a6ae
FB
1211 if (start_time == 0 || ist->pts >= start_time)
1212 for(i=0;i<nb_ostreams;i++) {
1213 int frame_size;
1214
1215 ost = ost_table[i];
1216 if (ost->source_index == ist_index) {
1217 os = output_files[ost->file_index];
1218
1219#if 0
115329f1
DB
1220 printf("%d: got pts=%0.3f %0.3f\n", i,
1221 (double)pkt->pts / AV_TIME_BASE,
1222 ((double)ist->pts / AV_TIME_BASE) -
986ebcdb 1223 ((double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den));
a700a6ae
FB
1224#endif
1225 /* set the input output pts pairs */
b4a3389e 1226 //ost->sync_ipts = (double)(ist->pts + input_files_ts_offset[ist->file_index] - start_time)/ AV_TIME_BASE;
a700a6ae
FB
1227
1228 if (ost->encoding_needed) {
01f4895c 1229 switch(ost->st->codec->codec_type) {
a700a6ae
FB
1230 case CODEC_TYPE_AUDIO:
1231 do_audio_out(os, ost, ist, data_buf, data_size);
1232 break;
1233 case CODEC_TYPE_VIDEO:
986ebcdb 1234 do_video_out(os, ost, ist, &picture, &frame_size);
1008ceb3 1235 video_size += frame_size;
a700a6ae
FB
1236 if (do_vstats && frame_size)
1237 do_video_stats(os, ost, frame_size);
a700a6ae 1238 break;
cf7fc795
FB
1239 case CODEC_TYPE_SUBTITLE:
1240 do_subtitle_out(os, ost, ist, &subtitle,
1241 pkt->pts);
1242 break;
a700a6ae
FB
1243 default:
1244 av_abort();
1245 }
1246 } else {
e928649b
MN
1247 AVFrame avframe; //FIXME/XXX remove this
1248 AVPacket opkt;
1249 av_init_packet(&opkt);
1250
a700a6ae
FB
1251 /* no reencoding needed : output the packet directly */
1252 /* force the input stream PTS */
115329f1 1253
9740beff 1254 avcodec_get_frame_defaults(&avframe);
01f4895c 1255 ost->st->codec->coded_frame= &avframe;
115329f1 1256 avframe.key_frame = pkt->flags & PKT_FLAG_KEY;
bf17ff61 1257
01f4895c 1258 if(ost->st->codec->codec_type == CODEC_TYPE_AUDIO)
c7baf14b 1259 audio_size += data_size;
01f4895c 1260 else if (ost->st->codec->codec_type == CODEC_TYPE_VIDEO) {
c7baf14b 1261 video_size += data_size;
daed32f7
WG
1262 ost->sync_opts++;
1263 }
c7baf14b 1264
e928649b 1265 opkt.stream_index= ost->index;
6c55b27f 1266 if(pkt->pts != AV_NOPTS_VALUE)
c0df9d75 1267 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);
6c55b27f
MN
1268 else
1269 opkt.pts= AV_NOPTS_VALUE;
cf7fc795
FB
1270
1271 {
1272 int64_t dts;
1273 if (pkt->dts == AV_NOPTS_VALUE)
1274 dts = ist->next_pts;
1275 else
1276 dts= av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
1277 opkt.dts= av_rescale_q(dts + input_files_ts_offset[ist->file_index], AV_TIME_BASE_Q, ost->st->time_base);
1278 }
e928649b 1279 opkt.flags= pkt->flags;
748c2fca
MN
1280
1281 //FIXME remove the following 2 lines they shall be replaced by the bitstream filters
01f4895c 1282 if(av_parser_change(ist->st->parser, ost->st->codec, &opkt.data, &opkt.size, data_buf, data_size, pkt->flags & PKT_FLAG_KEY))
90ad92b3 1283 opkt.destruct= av_destruct_packet;
748c2fca
MN
1284
1285 write_frame(os, &opkt, ost->st->codec, bitstream_filters[ost->file_index][pkt->stream_index]);
01f4895c 1286 ost->st->codec->frame_number++;
a700a6ae 1287 ost->frame_number++;
90ad92b3 1288 av_free_packet(&opkt);
a700a6ae
FB
1289 }
1290 }
1291 }
1292 av_free(buffer_to_free);
cf7fc795
FB
1293 /* XXX: allocate the subtitles in the codec ? */
1294 if (subtitle_to_free) {
c6ec28b1
MN
1295 if (subtitle_to_free->rects != NULL) {
1296 for (i = 0; i < subtitle_to_free->num_rects; i++) {
1297 av_free(subtitle_to_free->rects[i].bitmap);
1298 av_free(subtitle_to_free->rects[i].rgba_palette);
1299 }
1300 av_freep(&subtitle_to_free->rects);
1301 }
1302 subtitle_to_free->num_rects = 0;
cf7fc795
FB
1303 subtitle_to_free = NULL;
1304 }
a700a6ae
FB
1305 }
1306 discard_packet:
6f824977
MN
1307 if (pkt == NULL) {
1308 /* EOF handling */
115329f1 1309
6f824977
MN
1310 for(i=0;i<nb_ostreams;i++) {
1311 ost = ost_table[i];
1312 if (ost->source_index == ist_index) {
01f4895c 1313 AVCodecContext *enc= ost->st->codec;
6f824977 1314 os = output_files[ost->file_index];
115329f1 1315
01f4895c 1316 if(ost->st->codec->codec_type == CODEC_TYPE_AUDIO && enc->frame_size <=1)
6f824977 1317 continue;
01f4895c 1318 if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO && (os->oformat->flags & AVFMT_RAWPICTURE))
6f824977
MN
1319 continue;
1320
1321 if (ost->encoding_needed) {
1322 for(;;) {
1323 AVPacket pkt;
cef7cc72 1324 int fifo_bytes;
6f824977
MN
1325 av_init_packet(&pkt);
1326 pkt.stream_index= ost->index;
115329f1 1327
01f4895c 1328 switch(ost->st->codec->codec_type) {
115329f1 1329 case CODEC_TYPE_AUDIO:
cef7cc72
JR
1330 fifo_bytes = fifo_size(&ost->fifo, NULL);
1331 ret = 0;
1332 /* encode any samples remaining in fifo */
1333 if(fifo_bytes > 0 && enc->codec->capabilities & CODEC_CAP_SMALL_LAST_FRAME) {
1334 int fs_tmp = enc->frame_size;
1335 enc->frame_size = fifo_bytes / (2 * enc->channels);
1336 if(fifo_read(&ost->fifo, (uint8_t *)samples, fifo_bytes,
1337 &ost->fifo.rptr) == 0) {
1338 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, samples);
1339 }
1340 enc->frame_size = fs_tmp;
9e0db7d5
MN
1341 }
1342 if(ret <= 0) {
cef7cc72
JR
1343 ret = avcodec_encode_audio(enc, bit_buffer, bit_buffer_size, NULL);
1344 }
6f824977
MN
1345 audio_size += ret;
1346 pkt.flags |= PKT_FLAG_KEY;
1347 break;
1348 case CODEC_TYPE_VIDEO:
8a6cb114 1349 ret = avcodec_encode_video(enc, bit_buffer, bit_buffer_size, NULL);
6f824977
MN
1350 video_size += ret;
1351 if(enc->coded_frame && enc->coded_frame->key_frame)
1352 pkt.flags |= PKT_FLAG_KEY;
1353 if (ost->logfile && enc->stats_out) {
1354 fprintf(ost->logfile, "%s", enc->stats_out);
1355 }
1356 break;
1357 default:
1358 ret=-1;
1359 }
115329f1 1360
6f824977
MN
1361 if(ret<=0)
1362 break;
27537106 1363 pkt.data= bit_buffer;
6f824977 1364 pkt.size= ret;
e7902f20 1365 if(enc->coded_frame && enc->coded_frame->pts != AV_NOPTS_VALUE)
c0df9d75 1366 pkt.pts= av_rescale_q(enc->coded_frame->pts, enc->time_base, ost->st->time_base);
748c2fca 1367 write_frame(os, &pkt, ost->st->codec, bitstream_filters[ost->file_index][pkt.stream_index]);
6f824977
MN
1368 }
1369 }
1370 }
1371 }
1372 }
115329f1 1373
a700a6ae
FB
1374 return 0;
1375 fail_decode:
1376 return -1;
1377}
1378
1379
85f07f22
FB
1380/*
1381 * The following code is the main loop of the file converter
1382 */
1383static int av_encode(AVFormatContext **output_files,
1384 int nb_output_files,
1385 AVFormatContext **input_files,
1386 int nb_input_files,
1387 AVStreamMap *stream_maps, int nb_stream_maps)
1388{
445f1b83 1389 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
85f07f22
FB
1390 AVFormatContext *is, *os;
1391 AVCodecContext *codec, *icodec;
1392 AVOutputStream *ost, **ost_table = NULL;
1393 AVInputStream *ist, **ist_table = NULL;
bdc4796f 1394 AVInputFile *file_table;
51bd4565 1395 AVFormatContext *stream_no_data;
cb09b2ed 1396 int key;
bdc4796f 1397
51bd4565 1398 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
bdc4796f
FB
1399 if (!file_table)
1400 goto fail;
115329f1 1401
85f07f22
FB
1402 /* input stream init */
1403 j = 0;
1404 for(i=0;i<nb_input_files;i++) {
1405 is = input_files[i];
1406 file_table[i].ist_index = j;
79fdaa4c 1407 file_table[i].nb_streams = is->nb_streams;
85f07f22
FB
1408 j += is->nb_streams;
1409 }
1410 nb_istreams = j;
1411
1412 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
1413 if (!ist_table)
bdc4796f 1414 goto fail;
115329f1 1415
85f07f22
FB
1416 for(i=0;i<nb_istreams;i++) {
1417 ist = av_mallocz(sizeof(AVInputStream));
1418 if (!ist)
1419 goto fail;
1420 ist_table[i] = ist;
1421 }
1422 j = 0;
1423 for(i=0;i<nb_input_files;i++) {
1424 is = input_files[i];
1425 for(k=0;k<is->nb_streams;k++) {
1426 ist = ist_table[j++];
1427 ist->st = is->streams[k];
1428 ist->file_index = i;
1429 ist->index = k;
1430 ist->discard = 1; /* the stream is discarded by default
1431 (changed later) */
bdfcbbed 1432
01f4895c 1433 if (ist->st->codec->rate_emu) {
bdfcbbed
MK
1434 ist->start = av_gettime();
1435 ist->frame = 0;
1436 }
85f07f22
FB
1437 }
1438 }
1439
1440 /* output stream init */
1441 nb_ostreams = 0;
1442 for(i=0;i<nb_output_files;i++) {
1443 os = output_files[i];
1444 nb_ostreams += os->nb_streams;
1445 }
1446 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
1447 fprintf(stderr, "Number of stream maps must match number of output streams\n");
1448 exit(1);
1449 }
1450
bd073980
BF
1451 /* Sanity check the mapping args -- do the input files & streams exist? */
1452 for(i=0;i<nb_stream_maps;i++) {
1453 int fi = stream_maps[i].file_index;
1454 int si = stream_maps[i].stream_index;
115329f1 1455
bd073980
BF
1456 if (fi < 0 || fi > nb_input_files - 1 ||
1457 si < 0 || si > file_table[fi].nb_streams - 1) {
1458 fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
1459 exit(1);
1460 }
b4a3389e
WG
1461 fi = stream_maps[i].sync_file_index;
1462 si = stream_maps[i].sync_stream_index;
1463 if (fi < 0 || fi > nb_input_files - 1 ||
1464 si < 0 || si > file_table[fi].nb_streams - 1) {
1465 fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
1466 exit(1);
1467 }
bd073980 1468 }
115329f1 1469
85f07f22
FB
1470 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
1471 if (!ost_table)
1472 goto fail;
1473 for(i=0;i<nb_ostreams;i++) {
1474 ost = av_mallocz(sizeof(AVOutputStream));
1475 if (!ost)
1476 goto fail;
1477 ost_table[i] = ost;
1478 }
115329f1 1479
85f07f22
FB
1480 n = 0;
1481 for(k=0;k<nb_output_files;k++) {
1482 os = output_files[k];
1483 for(i=0;i<os->nb_streams;i++) {
1484 int found;
1485 ost = ost_table[n++];
1486 ost->file_index = k;
1487 ost->index = i;
1488 ost->st = os->streams[i];
1489 if (nb_stream_maps > 0) {
115329f1 1490 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
85f07f22 1491 stream_maps[n-1].stream_index;
115329f1 1492
bd073980 1493 /* Sanity check that the stream types match */
01f4895c 1494 if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) {
bd073980
BF
1495 fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
1496 stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
1497 ost->file_index, ost->index);
1498 exit(1);
1499 }
115329f1 1500
85f07f22
FB
1501 } else {
1502 /* get corresponding input stream index : we select the first one with the right type */
1503 found = 0;
1504 for(j=0;j<nb_istreams;j++) {
1505 ist = ist_table[j];
115329f1 1506 if (ist->discard &&
01f4895c 1507 ist->st->codec->codec_type == ost->st->codec->codec_type) {
85f07f22
FB
1508 ost->source_index = j;
1509 found = 1;
c19e76ce 1510 break;
85f07f22
FB
1511 }
1512 }
115329f1 1513
85f07f22
FB
1514 if (!found) {
1515 /* try again and reuse existing stream */
1516 for(j=0;j<nb_istreams;j++) {
1517 ist = ist_table[j];
01f4895c 1518 if (ist->st->codec->codec_type == ost->st->codec->codec_type) {
85f07f22
FB
1519 ost->source_index = j;
1520 found = 1;
1521 }
1522 }
1523 if (!found) {
1524 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1525 ost->file_index, ost->index);
1526 exit(1);
1527 }
1528 }
1529 }
1530 ist = ist_table[ost->source_index];
1531 ist->discard = 0;
b4a3389e
WG
1532 ost->sync_ist = (nb_stream_maps > 0) ?
1533 ist_table[file_table[stream_maps[n-1].sync_file_index].ist_index +
1534 stream_maps[n-1].sync_stream_index] : ist;
85f07f22
FB
1535 }
1536 }
1537
85f07f22
FB
1538 /* for each output stream, we compute the right encoding parameters */
1539 for(i=0;i<nb_ostreams;i++) {
1540 ost = ost_table[i];
1541 ist = ist_table[ost->source_index];
1542
01f4895c
MN
1543 codec = ost->st->codec;
1544 icodec = ist->st->codec;
85f07f22 1545
1629626f
FB
1546 if (ost->st->stream_copy) {
1547 /* if stream_copy is selected, no need to decode or encode */
1548 codec->codec_id = icodec->codec_id;
1549 codec->codec_type = icodec->codec_type;
b2a2197e 1550 if(!codec->codec_tag) codec->codec_tag = icodec->codec_tag;
1629626f 1551 codec->bit_rate = icodec->bit_rate;
dffbfd06
MN
1552 codec->extradata= icodec->extradata;
1553 codec->extradata_size= icodec->extradata_size;
78e03516 1554 codec->time_base = icodec->time_base;
1629626f
FB
1555 switch(codec->codec_type) {
1556 case CODEC_TYPE_AUDIO:
1557 codec->sample_rate = icodec->sample_rate;
1558 codec->channels = icodec->channels;
0a3b0447 1559 codec->frame_size = icodec->frame_size;
c1344911 1560 codec->block_align= icodec->block_align;
1629626f
FB
1561 break;
1562 case CODEC_TYPE_VIDEO:
9df3437f 1563 codec->pix_fmt = icodec->pix_fmt;
1629626f
FB
1564 codec->width = icodec->width;
1565 codec->height = icodec->height;
ff8cc24b 1566 codec->has_b_frames = icodec->has_b_frames;
1629626f 1567 break;
cf7fc795
FB
1568 case CODEC_TYPE_SUBTITLE:
1569 break;
1629626f
FB
1570 default:
1571 av_abort();
1572 }
1573 } else {
1574 switch(codec->codec_type) {
1575 case CODEC_TYPE_AUDIO:
85f07f22
FB
1576 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1577 goto fail;
115329f1 1578
85f07f22
FB
1579 if (codec->channels == icodec->channels &&
1580 codec->sample_rate == icodec->sample_rate) {
1581 ost->audio_resample = 0;
1582 } else {
e0d2714a 1583 if (codec->channels != icodec->channels &&
23c99253
MN
1584 (icodec->codec_id == CODEC_ID_AC3 ||
1585 icodec->codec_id == CODEC_ID_DTS)) {
1586 /* Special case for 5:1 AC3 and DTS input */
e0d2714a 1587 /* and mono or stereo output */
6dc96cb0
J
1588 /* Request specific number of channels */
1589 icodec->channels = codec->channels;
1590 if (codec->sample_rate == icodec->sample_rate)
1591 ost->audio_resample = 0;
1592 else {
1593 ost->audio_resample = 1;
6dc96cb0 1594 }
e0d2714a 1595 } else {
115329f1 1596 ost->audio_resample = 1;
986ebcdb
MN
1597 }
1598 }
ff4905a5 1599 if(audio_sync_method>1)
986ebcdb
MN
1600 ost->audio_resample = 1;
1601
1602 if(ost->audio_resample){
1603 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1604 codec->sample_rate, icodec->sample_rate);
1605 if(!ost->resample){
1606 printf("Can't resample. Aborting.\n");
1607 av_abort();
e0d2714a 1608 }
85f07f22
FB
1609 }
1610 ist->decoding_needed = 1;
1611 ost->encoding_needed = 1;
1629626f
FB
1612 break;
1613 case CODEC_TYPE_VIDEO:
c3f11d19
LA
1614 ost->video_crop = ((frame_leftBand + frame_rightBand + frame_topBand + frame_bottomBand) != 0);
1615 ost->video_pad = ((frame_padleft + frame_padright + frame_padtop + frame_padbottom) != 0);
1616 ost->video_resample = ((codec->width != icodec->width -
1617 (frame_leftBand + frame_rightBand) +
1618 (frame_padleft + frame_padright)) ||
1619 (codec->height != icodec->height -
1620 (frame_topBand + frame_bottomBand) +
18a54b04
LA
1621 (frame_padtop + frame_padbottom)) ||
1622 (codec->pix_fmt != icodec->pix_fmt));
c3f11d19 1623 if (ost->video_crop) {
34b10a57
D
1624 ost->topBand = frame_topBand;
1625 ost->leftBand = frame_leftBand;
c3f11d19
LA
1626 }
1627 if (ost->video_pad) {
1ff93ffc
TK
1628 ost->padtop = frame_padtop;
1629 ost->padleft = frame_padleft;
1630 ost->padbottom = frame_padbottom;
1631 ost->padright = frame_padright;
c3f11d19
LA
1632 if (!ost->video_resample) {
1633 avcodec_get_frame_defaults(&ost->pict_tmp);
1634 if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, codec->pix_fmt,
1635 codec->width, codec->height ) )
1636 goto fail;
1637 }
1638 }
1639 if (ost->video_resample) {
2de28abb 1640 avcodec_get_frame_defaults(&ost->pict_tmp);
18a54b04 1641 if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, codec->pix_fmt,
2caa92d9 1642 codec->width, codec->height ) )
85f07f22 1643 goto fail;
85f07f22 1644
18a54b04
LA
1645 ost->img_resample_ctx = sws_getContext(
1646 icodec->width - (frame_leftBand + frame_rightBand),
1647 icodec->height - (frame_topBand + frame_bottomBand),
1648 icodec->pix_fmt,
07d0cdfc
LA
1649 codec->width - (frame_padleft + frame_padright),
1650 codec->height - (frame_padtop + frame_padbottom),
18a54b04
LA
1651 codec->pix_fmt,
1652 sws_flags, NULL, NULL, NULL);
0b50ac8a
LA
1653 if (ost->img_resample_ctx == NULL) {
1654 fprintf(stderr, "Cannot get resampling context\n");
1655 exit(1);
1656 }
18a54b04 1657 ost->resample_height = icodec->height - (frame_topBand + frame_bottomBand);
85f07f22
FB
1658 }
1659 ost->encoding_needed = 1;
1660 ist->decoding_needed = 1;
1629626f 1661 break;
cf7fc795
FB
1662 case CODEC_TYPE_SUBTITLE:
1663 ost->encoding_needed = 1;
1664 ist->decoding_needed = 1;
1665 break;
1629626f
FB
1666 default:
1667 av_abort();
cf7fc795 1668 break;
85f07f22 1669 }
1629626f 1670 /* two pass mode */
115329f1 1671 if (ost->encoding_needed &&
1629626f
FB
1672 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1673 char logfilename[1024];
1674 FILE *f;
1675 int size;
1676 char *logbuffer;
115329f1
DB
1677
1678 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1679 pass_logfilename ?
1629626f
FB
1680 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1681 if (codec->flags & CODEC_FLAG_PASS1) {
1682 f = fopen(logfilename, "w");
1683 if (!f) {
1684 perror(logfilename);
1685 exit(1);
1686 }
1687 ost->logfile = f;
1688 } else {
1689 /* read the log file */
1690 f = fopen(logfilename, "r");
1691 if (!f) {
1692 perror(logfilename);
1693 exit(1);
1694 }
1695 fseek(f, 0, SEEK_END);
1696 size = ftell(f);
1697 fseek(f, 0, SEEK_SET);
1698 logbuffer = av_malloc(size + 1);
1699 if (!logbuffer) {
1700 fprintf(stderr, "Could not allocate log buffer\n");
1701 exit(1);
1702 }
4776fa92 1703 size = fread(logbuffer, 1, size, f);
1629626f
FB
1704 fclose(f);
1705 logbuffer[size] = '\0';
1706 codec->stats_in = logbuffer;
5abdb4b1 1707 }
5abdb4b1
FB
1708 }
1709 }
8a6cb114
MN
1710 if(codec->codec_type == CODEC_TYPE_VIDEO){
1711 int size= codec->width * codec->height;
1712 bit_buffer_size= FFMAX(bit_buffer_size, 4*size);
1713 }
85f07f22
FB
1714 }
1715
8a6cb114
MN
1716 if (!bit_buffer)
1717 bit_buffer = av_malloc(bit_buffer_size);
1718 if (!bit_buffer)
1719 goto fail;
1720
1629626f
FB
1721 /* dump the file output parameters - cannot be done before in case
1722 of stream copy */
1723 for(i=0;i<nb_output_files;i++) {
1724 dump_format(output_files[i], i, output_files[i]->filename, 1);
1725 }
1726
1727 /* dump the stream mapping */
d8019eb5
AD
1728 if (verbose >= 0) {
1729 fprintf(stderr, "Stream mapping:\n");
1730 for(i=0;i<nb_ostreams;i++) {
1731 ost = ost_table[i];
b4a3389e 1732 fprintf(stderr, " Stream #%d.%d -> #%d.%d",
d8019eb5
AD
1733 ist_table[ost->source_index]->file_index,
1734 ist_table[ost->source_index]->index,
115329f1 1735 ost->file_index,
d8019eb5 1736 ost->index);
b4a3389e
WG
1737 if (ost->sync_ist != ist_table[ost->source_index])
1738 fprintf(stderr, " [sync #%d.%d]",
1739 ost->sync_ist->file_index,
1740 ost->sync_ist->index);
1741 fprintf(stderr, "\n");
d8019eb5 1742 }
1629626f
FB
1743 }
1744
85f07f22
FB
1745 /* open each encoder */
1746 for(i=0;i<nb_ostreams;i++) {
1747 ost = ost_table[i];
1748 if (ost->encoding_needed) {
1749 AVCodec *codec;
01f4895c 1750 codec = avcodec_find_encoder(ost->st->codec->codec_id);
85f07f22 1751 if (!codec) {
115329f1 1752 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
85f07f22
FB
1753 ost->file_index, ost->index);
1754 exit(1);
1755 }
01f4895c 1756 if (avcodec_open(ost->st->codec, codec) < 0) {
115329f1 1757 fprintf(stderr, "Error while opening codec for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
85f07f22
FB
1758 ost->file_index, ost->index);
1759 exit(1);
1760 }
01f4895c 1761 extra_size += ost->st->codec->extradata_size;
85f07f22
FB
1762 }
1763 }
1764
1765 /* open each decoder */
1766 for(i=0;i<nb_istreams;i++) {
1767 ist = ist_table[i];
1768 if (ist->decoding_needed) {
1769 AVCodec *codec;
01f4895c 1770 codec = avcodec_find_decoder(ist->st->codec->codec_id);
85f07f22 1771 if (!codec) {
115329f1 1772 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
01f4895c 1773 ist->st->codec->codec_id, ist->file_index, ist->index);
85f07f22
FB
1774 exit(1);
1775 }
01f4895c 1776 if (avcodec_open(ist->st->codec, codec) < 0) {
115329f1 1777 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
85f07f22
FB
1778 ist->file_index, ist->index);
1779 exit(1);
1780 }
01f4895c
MN
1781 //if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO)
1782 // ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
85f07f22
FB
1783 }
1784 }
1785
1786 /* init pts */
1787 for(i=0;i<nb_istreams;i++) {
1788 ist = ist_table[i];
bb270c08 1789 is = input_files[ist->file_index];
e7d0374f 1790 ist->pts = 0;
c0df9d75 1791 ist->next_pts = av_rescale_q(ist->st->start_time, ist->st->time_base, AV_TIME_BASE_Q);
115329f1 1792 if(ist->st->start_time == AV_NOPTS_VALUE)
bd1b79a1 1793 ist->next_pts=0;
fdb86eb1
MN
1794 if(input_files_ts_offset[ist->file_index])
1795 ist->next_pts= AV_NOPTS_VALUE;
ff4905a5 1796 ist->is_start = 1;
85f07f22 1797 }
bd1b79a1 1798
85f07f22
FB
1799 /* compute buffer size max (should use a complete heuristic) */
1800 for(i=0;i<nb_input_files;i++) {
1801 file_table[i].buffer_size_max = 2048;
1802 }
1803
0a38bafd
PB
1804 /* set meta data information from input file if required */
1805 for (i=0;i<nb_meta_data_maps;i++) {
1806 AVFormatContext *out_file;
1807 AVFormatContext *in_file;
1808
1809 int out_file_index = meta_data_maps[i].out_file;
1810 int in_file_index = meta_data_maps[i].in_file;
1811 if ( out_file_index < 0 || out_file_index >= nb_output_files ) {
1812 fprintf(stderr, "Invalid output file index %d map_meta_data(%d,%d)\n", out_file_index, out_file_index, in_file_index);
1813 ret = -EINVAL;
1814 goto fail;
1815 }
1816 if ( in_file_index < 0 || in_file_index >= nb_input_files ) {
1817 fprintf(stderr, "Invalid input file index %d map_meta_data(%d,%d)\n", in_file_index, out_file_index, in_file_index);
1818 ret = -EINVAL;
1819 goto fail;
115329f1
DB
1820 }
1821
0a38bafd
PB
1822 out_file = output_files[out_file_index];
1823 in_file = input_files[in_file_index];
1824
1825 strcpy(out_file->title, in_file->title);
1826 strcpy(out_file->author, in_file->author);
1827 strcpy(out_file->copyright, in_file->copyright);
1828 strcpy(out_file->comment, in_file->comment);
1829 strcpy(out_file->album, in_file->album);
1830 out_file->year = in_file->year;
1831 out_file->track = in_file->track;
1832 strcpy(out_file->genre, in_file->genre);
1833 }
115329f1 1834
85f07f22
FB
1835 /* open files and write file headers */
1836 for(i=0;i<nb_output_files;i++) {
1837 os = output_files[i];
79fdaa4c 1838 if (av_write_header(os) < 0) {
65bf3c53 1839 fprintf(stderr, "Could not write header for output file #%d (incorrect codec parameters ?)\n", i);
a38469e1
FB
1840 ret = -EINVAL;
1841 goto fail;
1842 }
85f07f22
FB
1843 }
1844
2f30a81d 1845#ifndef __MINGW32__
d8019eb5 1846 if ( !using_stdin && verbose >= 0) {
d9a916e2 1847 fprintf(stderr, "Press [q] to stop encoding\n");
b51469a0
LS
1848 url_set_interrupt_cb(decode_interrupt_cb);
1849 }
a38469e1
FB
1850#endif
1851 term_init();
1852
51bd4565 1853 stream_no_data = 0;
cb09b2ed 1854 key = -1;
51bd4565 1855
d9a916e2 1856 for(; received_sigterm == 0;) {
85f07f22
FB
1857 int file_index, ist_index;
1858 AVPacket pkt;
a9aeda81
PB
1859 double ipts_min;
1860 double opts_min;
23ffe323 1861
85f07f22 1862 redo:
a9aeda81
PB
1863 ipts_min= 1e100;
1864 opts_min= 1e100;
a38469e1 1865 /* if 'q' pressed, exits */
d9a916e2 1866 if (!using_stdin) {
b51469a0
LS
1867 if (q_pressed)
1868 break;
cb09b2ed
PG
1869 /* read_key() returns 0 on EOF */
1870 key = read_key();
1871 if (key == 'q')
1872 break;
1873 }
a38469e1 1874
ec5517d5
FB
1875 /* select the stream that we must read now by looking at the
1876 smallest output pts */
85f07f22 1877 file_index = -1;
ec5517d5 1878 for(i=0;i<nb_ostreams;i++) {
23ffe323 1879 double ipts, opts;
ec5517d5
FB
1880 ost = ost_table[i];
1881 os = output_files[ost->file_index];
1882 ist = ist_table[ost->source_index];
01f4895c
MN
1883 if(ost->st->codec->codec_type == CODEC_TYPE_VIDEO)
1884 opts = ost->sync_opts * av_q2d(ost->st->codec->time_base);
23ffe323 1885 else
c0df9d75 1886 opts = ost->st->pts.val * av_q2d(ost->st->time_base);
23ffe323
MN
1887 ipts = (double)ist->pts;
1888 if (!file_table[ist->file_index].eof_reached){
1889 if(ipts < ipts_min) {
1890 ipts_min = ipts;
1891 if(input_sync ) file_index = ist->file_index;
1892 }
1893 if(opts < opts_min) {
1894 opts_min = opts;
1895 if(!input_sync) file_index = ist->file_index;
1896 }
85f07f22 1897 }
01f4895c 1898 if(ost->frame_number >= max_frames[ost->st->codec->codec_type]){
7ca60994
MN
1899 file_index= -1;
1900 break;
1901 }
85f07f22
FB
1902 }
1903 /* if none, if is finished */
51bd4565 1904 if (file_index < 0) {
85f07f22 1905 break;
ec5517d5
FB
1906 }
1907
85f07f22 1908 /* finish if recording time exhausted */
23ffe323 1909 if (recording_time > 0 && opts_min >= (recording_time / 1000000.0))
85f07f22 1910 break;
ec5517d5 1911
b6e16b86
C
1912 /* finish if limit size exhausted */
1913 if (limit_filesize != 0 && (limit_filesize * 1024) < url_ftell(&output_files[0]->pb))
1914 break;
1915
254abc2e 1916 /* read a frame from it and output it in the fifo */
85f07f22 1917 is = input_files[file_index];
254abc2e 1918 if (av_read_frame(is, &pkt) < 0) {
85f07f22 1919 file_table[file_index].eof_reached = 1;
ca80810b 1920 if (opt_shortest) break; else continue; //
85f07f22 1921 }
303e50e6 1922
51bd4565
PG
1923 if (!pkt.size) {
1924 stream_no_data = is;
1925 } else {
1926 stream_no_data = 0;
1927 }
254abc2e
FB
1928 if (do_pkt_dump) {
1929 av_pkt_dump(stdout, &pkt, do_hex_dump);
817b23ff 1930 }
79fdaa4c
FB
1931 /* the following test is needed in case new streams appear
1932 dynamically in stream : we ignore them */
1933 if (pkt.stream_index >= file_table[file_index].nb_streams)
bf5af568 1934 goto discard_packet;
85f07f22
FB
1935 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1936 ist = ist_table[ist_index];
bf5af568
FB
1937 if (ist->discard)
1938 goto discard_packet;
85f07f22 1939
01f4895c 1940// 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);
fdb86eb1 1941 if (pkt.dts != AV_NOPTS_VALUE && ist->next_pts != AV_NOPTS_VALUE) {
c0df9d75 1942 int64_t delta= av_rescale_q(pkt.dts, ist->st->time_base, AV_TIME_BASE_Q) - ist->next_pts;
023857be 1943 if(ABS(delta) > 1LL*dts_delta_threshold*AV_TIME_BASE && !copy_ts){
72bd8100
MN
1944 input_files_ts_offset[ist->file_index]-= delta;
1945 if (verbose > 2)
4733abcb 1946 fprintf(stderr, "timestamp discontinuity %"PRId64", new offset= %"PRId64"\n", delta, input_files_ts_offset[ist->file_index]);
72bd8100
MN
1947 for(i=0; i<file_table[file_index].nb_streams; i++){
1948 int index= file_table[file_index].ist_index + i;
1949 ist_table[index]->next_pts += delta;
ff4905a5 1950 ist_table[index]->is_start=1;
72bd8100
MN
1951 }
1952 }
1953 }
1954
8831db5c 1955 //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
a700a6ae 1956 if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
d8019eb5
AD
1957
1958 if (verbose >= 0)
1959 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1960 ist->file_index, ist->index);
1961
a700a6ae
FB
1962 av_free_packet(&pkt);
1963 goto redo;
fe08925f 1964 }
cf7fc795 1965
bf5af568 1966 discard_packet:
85f07f22 1967 av_free_packet(&pkt);
115329f1 1968
ec5517d5
FB
1969 /* dump report by using the output first video and audio streams */
1970 print_report(output_files, ost_table, nb_ostreams, 0);
85f07f22 1971 }
a700a6ae
FB
1972
1973 /* at the end of stream, we must flush the decoder buffers */
1974 for(i=0;i<nb_istreams;i++) {
1975 ist = ist_table[i];
1976 if (ist->decoding_needed) {
1977 output_packet(ist, i, ost_table, nb_ostreams, NULL);
1978 }
1979 }
1980
a38469e1 1981 term_exit();
85f07f22 1982
c4e37247
MN
1983 /* write the trailer if needed and close file */
1984 for(i=0;i<nb_output_files;i++) {
1985 os = output_files[i];
1986 av_write_trailer(os);
1987 }
1988
37f5cd5a
MN
1989 /* dump report by using the first video and audio streams */
1990 print_report(output_files, ost_table, nb_ostreams, 1);
1991
85f07f22
FB
1992 /* close each encoder */
1993 for(i=0;i<nb_ostreams;i++) {
1994 ost = ost_table[i];
1995 if (ost->encoding_needed) {
01f4895c
MN
1996 av_freep(&ost->st->codec->stats_in);
1997 avcodec_close(ost->st->codec);
85f07f22
FB
1998 }
1999 }
115329f1 2000
85f07f22
FB
2001 /* close each decoder */
2002 for(i=0;i<nb_istreams;i++) {
2003 ist = ist_table[i];
2004 if (ist->decoding_needed) {
01f4895c 2005 avcodec_close(ist->st->codec);
85f07f22
FB
2006 }
2007 }
85f07f22 2008
85f07f22 2009 /* finished ! */
115329f1 2010
85f07f22
FB
2011 ret = 0;
2012 fail1:
83b07470 2013 av_freep(&bit_buffer);
0f1578af 2014 av_free(file_table);
bdc4796f 2015
85f07f22
FB
2016 if (ist_table) {
2017 for(i=0;i<nb_istreams;i++) {
2018 ist = ist_table[i];
0f1578af 2019 av_free(ist);
85f07f22 2020 }
0f1578af 2021 av_free(ist_table);
85f07f22
FB
2022 }
2023 if (ost_table) {
2024 for(i=0;i<nb_ostreams;i++) {
2025 ost = ost_table[i];
2026 if (ost) {
5abdb4b1
FB
2027 if (ost->logfile) {
2028 fclose(ost->logfile);
2029 ost->logfile = NULL;
2030 }
bf5af568
FB
2031 fifo_free(&ost->fifo); /* works even if fifo is not
2032 initialized but set to zero */
0f1578af 2033 av_free(ost->pict_tmp.data[0]);
85f07f22 2034 if (ost->video_resample)
18a54b04 2035 sws_freeContext(ost->img_resample_ctx);
85f07f22
FB
2036 if (ost->audio_resample)
2037 audio_resample_close(ost->resample);
0f1578af 2038 av_free(ost);
85f07f22
FB
2039 }
2040 }
0f1578af 2041 av_free(ost_table);
85f07f22
FB
2042 }
2043 return ret;
2044 fail:
2045 ret = -ENOMEM;
2046 goto fail1;
2047}
2048
2049#if 0
2050int file_read(const char *filename)
2051{
2052 URLContext *h;
2053 unsigned char buffer[1024];
2054 int len, i;
2055
2056 if (url_open(&h, filename, O_RDONLY) < 0) {
2057 printf("could not open '%s'\n", filename);
2058 return -1;
2059 }
2060 for(;;) {
2061 len = url_read(h, buffer, sizeof(buffer));
2062 if (len <= 0)
2063 break;
2064 for(i=0;i<len;i++) putchar(buffer[i]);
2065 }
2066 url_close(h);
2067 return 0;
2068}
2069#endif
2070
b29f97d1 2071static void opt_image_format(const char *arg)
817b23ff
FB
2072{
2073 AVImageFormat *f;
115329f1 2074
817b23ff
FB
2075 for(f = first_image_format; f != NULL; f = f->next) {
2076 if (!strcmp(arg, f->name))
2077 break;
2078 }
2079 if (!f) {
2080 fprintf(stderr, "Unknown image format: '%s'\n", arg);
2081 exit(1);
2082 }
2083 image_format = f;
2084}
2085
b29f97d1 2086static void opt_format(const char *arg)
85f07f22 2087{
817b23ff
FB
2088 /* compatibility stuff for pgmyuv */
2089 if (!strcmp(arg, "pgmyuv")) {
5b6d5596 2090 pgmyuv_compatibility_hack=1;
9286699a 2091// opt_image_format(arg);
5b6d5596 2092 arg = "image2";
817b23ff
FB
2093 }
2094
79fdaa4c
FB
2095 file_iformat = av_find_input_format(arg);
2096 file_oformat = guess_format(arg, NULL, NULL);
2097 if (!file_iformat && !file_oformat) {
2098 fprintf(stderr, "Unknown input or output format: %s\n", arg);
85f07f22
FB
2099 exit(1);
2100 }
85f07f22
FB
2101}
2102
b29f97d1 2103static void opt_video_buffer_size(const char *arg)
946c8a12 2104{
622348f9 2105 video_rc_buffer_size = atoi(arg) * 8*1024;
946c8a12
MN
2106}
2107
b29f97d1 2108static void opt_video_rc_eq(char *arg)
3aa102be
MN
2109{
2110 video_rc_eq = arg;
2111}
2112
b29f97d1 2113static void opt_video_rc_override_string(char *arg)
ac2830ec
MN
2114{
2115 video_rc_override_string = arg;
2116}
2117
3aa102be 2118
b29f97d1 2119static void opt_workaround_bugs(const char *arg)
fe670d09
MN
2120{
2121 workaround_bugs = atoi(arg);
2122}
2123
f4f3223f
MN
2124static void opt_me_threshold(const char *arg)
2125{
2126 me_threshold = atoi(arg);
2127}
2128
f20f8a8b
MN
2129static void opt_mb_threshold(const char *arg)
2130{
2131 mb_threshold = atoi(arg);
2132}
2ad1516a 2133
f068206e
BE
2134static void opt_verbose(const char *arg)
2135{
2136 verbose = atoi(arg);
d8019eb5 2137 av_log_set_level(atoi(arg));
f068206e
BE
2138}
2139
b29f97d1 2140static void opt_frame_rate(const char *arg)
85f07f22 2141{
445f1b83
RS
2142 if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
2143 fprintf(stderr, "Incorrect frame rate\n");
bb270c08 2144 exit(1);
445f1b83 2145 }
85f07f22
FB
2146}
2147
b29f97d1 2148static void opt_frame_crop_top(const char *arg)
ab6d194a 2149{
115329f1 2150 frame_topBand = atoi(arg);
ab6d194a
MN
2151 if (frame_topBand < 0) {
2152 fprintf(stderr, "Incorrect top crop size\n");
2153 exit(1);
2154 }
2155 if ((frame_topBand % 2) != 0) {
2156 fprintf(stderr, "Top crop size must be a multiple of 2\n");
2157 exit(1);
2158 }
2159 if ((frame_topBand) >= frame_height){
bb270c08 2160 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
ab6d194a
MN
2161 exit(1);
2162 }
2163 frame_height -= frame_topBand;
2164}
2165
b29f97d1 2166static void opt_frame_crop_bottom(const char *arg)
ab6d194a
MN
2167{
2168 frame_bottomBand = atoi(arg);
2169 if (frame_bottomBand < 0) {
2170 fprintf(stderr, "Incorrect bottom crop size\n");
2171 exit(1);
2172 }
2173 if ((frame_bottomBand % 2) != 0) {
2174 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
115329f1 2175 exit(1);
ab6d194a
MN
2176 }
2177 if ((frame_bottomBand) >= frame_height){
bb270c08 2178 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
ab6d194a
MN
2179 exit(1);
2180 }
2181 frame_height -= frame_bottomBand;
2182}
2183
b29f97d1 2184static void opt_frame_crop_left(const char *arg)
ab6d194a
MN
2185{
2186 frame_leftBand = atoi(arg);
2187 if (frame_leftBand < 0) {
2188 fprintf(stderr, "Incorrect left crop size\n");
2189 exit(1);
2190 }
2191 if ((frame_leftBand % 2) != 0) {
2192 fprintf(stderr, "Left crop size must be a multiple of 2\n");
2193 exit(1);
2194 }
2195 if ((frame_leftBand) >= frame_width){
bb270c08 2196 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
ab6d194a
MN
2197 exit(1);
2198 }
2199 frame_width -= frame_leftBand;
2200}
2201
b29f97d1 2202static void opt_frame_crop_right(const char *arg)
ab6d194a
MN
2203{
2204 frame_rightBand = atoi(arg);
2205 if (frame_rightBand < 0) {
2206 fprintf(stderr, "Incorrect right crop size\n");
2207 exit(1);
2208 }
2209 if ((frame_rightBand % 2) != 0) {
2210 fprintf(stderr, "Right crop size must be a multiple of 2\n");
115329f1 2211 exit(1);
ab6d194a
MN
2212 }
2213 if ((frame_rightBand) >= frame_width){
bb270c08 2214 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
ab6d194a
MN
2215 exit(1);
2216 }
2217 frame_width -= frame_rightBand;
2218}
2219
b29f97d1 2220static void opt_frame_size(const char *arg)
85f07f22 2221{
445f1b83 2222 if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
85f07f22
FB
2223 fprintf(stderr, "Incorrect frame size\n");
2224 exit(1);
2225 }
2226 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
2227 fprintf(stderr, "Frame size must be a multiple of 2\n");
2228 exit(1);
2229 }
2230}
2231
1ff93ffc
TK
2232
2233#define SCALEBITS 10
2234#define ONE_HALF (1 << (SCALEBITS - 1))
bb270c08 2235#define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1ff93ffc
TK
2236
2237#define RGB_TO_Y(r, g, b) \
2238((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
2239 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
2240
2241#define RGB_TO_U(r1, g1, b1, shift)\
2242(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
2243 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2244
2245#define RGB_TO_V(r1, g1, b1, shift)\
2246(((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
2247 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2248
2249static void opt_pad_color(const char *arg) {
2250 /* Input is expected to be six hex digits similar to
2251 how colors are expressed in html tags (but without the #) */
2252 int rgb = strtol(arg, NULL, 16);
2253 int r,g,b;
115329f1
DB
2254
2255 r = (rgb >> 16);
1ff93ffc
TK
2256 g = ((rgb >> 8) & 255);
2257 b = (rgb & 255);
2258
2259 padcolor[0] = RGB_TO_Y(r,g,b);
2260 padcolor[1] = RGB_TO_U(r,g,b,0);
2261 padcolor[2] = RGB_TO_V(r,g,b,0);
2262}
2263
2264static void opt_frame_pad_top(const char *arg)
2265{
115329f1 2266 frame_padtop = atoi(arg);
1ff93ffc
TK
2267 if (frame_padtop < 0) {
2268 fprintf(stderr, "Incorrect top pad size\n");
2269 exit(1);
2270 }
2271 if ((frame_padtop % 2) != 0) {
2272 fprintf(stderr, "Top pad size must be a multiple of 2\n");
2273 exit(1);
2274 }
2275}
2276
2277static void opt_frame_pad_bottom(const char *arg)
2278{
115329f1 2279 frame_padbottom = atoi(arg);
1ff93ffc
TK
2280 if (frame_padbottom < 0) {
2281 fprintf(stderr, "Incorrect bottom pad size\n");
2282 exit(1);
2283 }
2284 if ((frame_padbottom % 2) != 0) {
2285 fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
2286 exit(1);
2287 }
2288}
2289
2290
2291static void opt_frame_pad_left(const char *arg)
2292{
115329f1 2293 frame_padleft = atoi(arg);
1ff93ffc
TK
2294 if (frame_padleft < 0) {
2295 fprintf(stderr, "Incorrect left pad size\n");
2296 exit(1);
2297 }
2298 if ((frame_padleft % 2) != 0) {
2299 fprintf(stderr, "Left pad size must be a multiple of 2\n");
2300 exit(1);
2301 }
2302}
2303
2304
2305static void opt_frame_pad_right(const char *arg)
2306{
115329f1 2307 frame_padright = atoi(arg);
1ff93ffc
TK
2308 if (frame_padright < 0) {
2309 fprintf(stderr, "Incorrect right pad size\n");
2310 exit(1);
2311 }
2312 if ((frame_padright % 2) != 0) {
2313 fprintf(stderr, "Right pad size must be a multiple of 2\n");
2314 exit(1);
2315 }
2316}
2317
2318
63167088
RS
2319static void opt_frame_pix_fmt(const char *arg)
2320{
2321 frame_pix_fmt = avcodec_get_pix_fmt(arg);
2322}
2323
5fe03e38
RS
2324static void opt_frame_aspect_ratio(const char *arg)
2325{
2326 int x = 0, y = 0;
2327 double ar = 0;
2328 const char *p;
115329f1 2329
5fe03e38
RS
2330 p = strchr(arg, ':');
2331 if (p) {
2332 x = strtol(arg, (char **)&arg, 10);
bb270c08
DB
2333 if (arg == p)
2334 y = strtol(arg+1, (char **)&arg, 10);
2335 if (x > 0 && y > 0)
2336 ar = (double)x / (double)y;
5fe03e38
RS
2337 } else
2338 ar = strtod(arg, (char **)&arg);
2339
2340 if (!ar) {
2341 fprintf(stderr, "Incorrect aspect ratio specification.\n");
bb270c08 2342 exit(1);
5fe03e38
RS
2343 }
2344 frame_aspect_ratio = ar;
2345}
2346
b29f97d1 2347static void opt_b_frames(const char *arg)
bc6caae2
J
2348{
2349 b_frames = atoi(arg);
2350 if (b_frames > FF_MAX_B_FRAMES) {
2351 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
2352 exit(1);
2353 } else if (b_frames < 1) {
2354 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
2355 exit(1);
2356 }
2357}
2358
b29f97d1 2359static void opt_qscale(const char *arg)
85f07f22 2360{
158c7f05 2361 video_qscale = atof(arg);
4e64bead 2362 if (video_qscale <= 0 ||
158c7f05 2363 video_qscale > 255) {
4e64bead 2364 fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
85f07f22
FB
2365 exit(1);
2366 }
2367}
2368
6a5d4395
MD
2369static void opt_qsquish(const char *arg)
2370{
2371 video_qsquish = atof(arg);
2372 if (video_qsquish < 0.0 ||
2373 video_qsquish > 99.0) {
2374 fprintf(stderr, "qsquish must be >= 0.0 and <= 99.0\n");
2375 exit(1);
2376 }
2377}
2378
4ea4b274
MN
2379static void opt_lmax(const char *arg)
2380{
2381 video_lmax = atof(arg)*FF_QP2LAMBDA;
2382}
2383
2384static void opt_lmin(const char *arg)
2385{
2386 video_lmin = atof(arg)*FF_QP2LAMBDA;
2387}
2388
b29f97d1 2389static void opt_qmin(const char *arg)
9cdd6a24
MN
2390{
2391 video_qmin = atoi(arg);
6e0d8c06 2392 if (video_qmin < 1 ||
58f7833e
RS
2393 video_qmin > 51) {
2394 fprintf(stderr, "qmin must be >= 1 and <= 51\n");
9cdd6a24
MN
2395 exit(1);
2396 }
2397}
2398
b29f97d1 2399static void opt_qmax(const char *arg)
9cdd6a24
MN
2400{
2401 video_qmax = atoi(arg);
6e0d8c06 2402 if (video_qmax < 1 ||
58f7833e
RS
2403 video_qmax > 51) {
2404 fprintf(stderr, "qmax must be >= 1 and <= 51\n");
9cdd6a24
MN
2405 exit(1);
2406 }
2407}
2408
6e0d8c06 2409static void opt_mb_lmin(const char *arg)
17a70fde 2410{
6e0d8c06
MN
2411 video_mb_lmin = atof(arg)*FF_QP2LAMBDA;
2412 if (video_mb_lmin < 1 ||
2413 video_mb_lmin > FF_LAMBDA_MAX) {
2414 fprintf(stderr, "mblmin must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
17a70fde
MN
2415 exit(1);
2416 }
2417}
2418
6e0d8c06 2419static void opt_mb_lmax(const char *arg)
17a70fde 2420{
6e0d8c06
MN
2421 video_mb_lmax = atof(arg)*FF_QP2LAMBDA;
2422 if (video_mb_lmax < 1 ||
2423 video_mb_lmax > FF_LAMBDA_MAX) {
2424 fprintf(stderr, "mblmax must be >= 1 and <= %d\n", FF_LAMBDA_MAX / FF_QP2LAMBDA);
17a70fde
MN
2425 exit(1);
2426 }
2427}
2428
b29f97d1 2429static void opt_qdiff(const char *arg)
9cdd6a24
MN
2430{
2431 video_qdiff = atoi(arg);
2432 if (video_qdiff < 0 ||
2433 video_qdiff > 31) {
2434 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
2435 exit(1);
2436 }
2437}
2438
b29f97d1 2439static void opt_packet_size(const char *arg)
1dbb6d90
MN
2440{
2441 packet_size= atoi(arg);
2442}
2443
7ebfc0ea
MN
2444static void opt_error_rate(const char *arg)
2445{
2446 error_rate= atoi(arg);
2447}
2448
b29f97d1 2449static void opt_strict(const char *arg)
f560dd82
MN
2450{
2451 strict= atoi(arg);
2452}
2453
bb198e19
MN
2454static void opt_top_field_first(const char *arg)
2455{
2456 top_field_first= atoi(arg);
2457}
2458
303e50e6
MN
2459static void opt_sc_threshold(const char *arg)
2460{
2461 sc_threshold= atoi(arg);
2462}
2463
9c3d33d6
MN
2464static void opt_thread_count(const char *arg)
2465{
2466 thread_count= atoi(arg);
2450cff2 2467#if !defined(HAVE_THREADS)
d8019eb5
AD
2468 if (verbose >= 0)
2469 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
842b556a 2470#endif
9c3d33d6
MN
2471}
2472
b29f97d1 2473static void opt_audio_bitrate(const char *arg)
85f07f22
FB
2474{
2475 audio_bit_rate = atoi(arg) * 1000;
2476}
2477
b29f97d1 2478static void opt_audio_rate(const char *arg)
85f07f22
FB
2479{
2480 audio_sample_rate = atoi(arg);
2481}
2482
b29f97d1 2483static void opt_audio_channels(const char *arg)
85f07f22
FB
2484{
2485 audio_channels = atoi(arg);
2486}
2487
b29f97d1 2488static void opt_video_device(const char *arg)
85f07f22 2489{
e9a9e0c2 2490 video_device = av_strdup(arg);
85f07f22
FB
2491}
2492
2639c651
RS
2493static void opt_grab_device(const char *arg)
2494{
2495 grab_device = av_strdup(arg);
2496}
2497
b29f97d1 2498static void opt_video_channel(const char *arg)
a5df11ab
FB
2499{
2500 video_channel = strtol(arg, NULL, 0);
2501}
2502
e3ee3283
AB
2503static void opt_video_standard(const char *arg)
2504{
2505 video_standard = av_strdup(arg);
2506}
2507
b29f97d1 2508static void opt_audio_device(const char *arg)
85f07f22 2509{
e9a9e0c2 2510 audio_device = av_strdup(arg);
85f07f22
FB
2511}
2512
cf7fc795
FB
2513static void opt_codec(int *pstream_copy, int *pcodec_id,
2514 int codec_type, const char *arg)
85f07f22
FB
2515{
2516 AVCodec *p;
2517
1629626f 2518 if (!strcmp(arg, "copy")) {
cf7fc795 2519 *pstream_copy = 1;
85f07f22 2520 } else {
1629626f
FB
2521 p = first_avcodec;
2522 while (p) {
cf7fc795 2523 if (!strcmp(p->name, arg) && p->type == codec_type)
1629626f
FB
2524 break;
2525 p = p->next;
2526 }
2527 if (p == NULL) {
cf7fc795 2528 fprintf(stderr, "Unknown codec '%s'\n", arg);
1629626f
FB
2529 exit(1);
2530 } else {
cf7fc795 2531 *pcodec_id = p->id;
1629626f 2532 }
85f07f22
FB
2533 }
2534}
2535
cf7fc795
FB
2536static void opt_audio_codec(const char *arg)
2537{
2538 opt_codec(&audio_stream_copy, &audio_codec_id, CODEC_TYPE_AUDIO, arg);
2539}
2540
b2a2197e
MN
2541static void opt_audio_tag(const char *arg)
2542{
2543 char *tail;
2544 audio_codec_tag= strtol(arg, &tail, 0);
2545
2546 if(!tail || *tail)
2547 audio_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
2548}
2549
2550static void opt_video_tag(const char *arg)
2551{
2552 char *tail;
2553 video_codec_tag= strtol(arg, &tail, 0);
2554
2555 if(!tail || *tail)
2556 video_codec_tag= arg[0] + (arg[1]<<8) + (arg[2]<<16) + (arg[3]<<24);
2557}
2558
b29f97d1 2559static void add_frame_hooker(const char *arg)
10d104e4
PG
2560{
2561 int argc = 0;
2562 char *argv[64];
2563 int i;
e9a9e0c2 2564 char *args = av_strdup(arg);
10d104e4 2565
bee0d9e5
CY
2566 using_vhook = 1;
2567
10d104e4
PG
2568 argv[0] = strtok(args, " ");
2569 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
2570 }
2571
2572 i = frame_hook_add(argc, argv);
2573
2574 if (i != 0) {
2575 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
2576 exit(1);
2577 }
2578}
2579
85f07f22
FB
2580const char *motion_str[] = {
2581 "zero",
2582 "full",
2583 "log",
2584 "phods",
7084c149
MN
2585 "epzs",
2586 "x1",
51d6a3cf
MN
2587 "hex",
2588 "umh",
2589 "iter",
85f07f22
FB
2590 NULL,
2591};
2592
b29f97d1 2593static void opt_motion_estimation(const char *arg)
85f07f22
FB
2594{
2595 const char **p;
2596 p = motion_str;
2597 for(;;) {
2598 if (!*p) {
2599 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
2600 exit(1);
2601 }
2602 if (!strcmp(*p, arg))
2603 break;
2604 p++;
2605 }
101bea5f 2606 me_method = (p - motion_str) + 1;
85f07f22
FB
2607}
2608
b29f97d1 2609static void opt_video_codec(const char *arg)
85f07f22 2610{
cf7fc795
FB
2611 opt_codec(&video_stream_copy, &video_codec_id, CODEC_TYPE_VIDEO, arg);
2612}
85f07f22 2613
cf7fc795
FB
2614static void opt_subtitle_codec(const char *arg)
2615{
2616 opt_codec(&subtitle_stream_copy, &subtitle_codec_id, CODEC_TYPE_SUBTITLE, arg);
85f07f22
FB
2617}
2618
b29f97d1 2619static void opt_map(const char *arg)
85f07f22
FB
2620{
2621 AVStreamMap *m;
2622 const char *p;
2623
2624 p = arg;
2625 m = &stream_maps[nb_stream_maps++];
2626
2627 m->file_index = strtol(arg, (char **)&p, 0);
2628 if (*p)
2629 p++;
a5dc85ef
J
2630
2631 m->stream_index = strtol(p, (char **)&p, 0);
b4a3389e
WG
2632 if (*p) {
2633 p++;
2634 m->sync_file_index = strtol(p, (char **)&p, 0);
2635 if (*p)
2636 p++;
2637 m->sync_stream_index = strtol(p, (char **)&p, 0);
2638 } else {
2639 m->sync_file_index = m->file_index;
2640 m->sync_stream_index = m->stream_index;
2641 }
85f07f22
FB
2642}
2643
0a38bafd
PB
2644static void opt_map_meta_data(const char *arg)
2645{
2646 AVMetaDataMap *m;
2647 const char *p;
115329f1 2648
0a38bafd
PB
2649 p = arg;
2650 m = &meta_data_maps[nb_meta_data_maps++];
2651
2652 m->out_file = strtol(arg, (char **)&p, 0);
2653 if (*p)
2654 p++;
2655
2656 m->in_file = strtol(p, (char **)&p, 0);
2657}
2658
b29f97d1 2659static void opt_recording_time(const char *arg)
85f07f22
FB
2660{
2661 recording_time = parse_date(arg, 1);
2662}
2663
8831db5c
MN
2664static void opt_start_time(const char *arg)
2665{
2666 start_time = parse_date(arg, 1);
2667}
2668
4568325a
RS
2669static void opt_rec_timestamp(const char *arg)
2670{
2671 rec_timestamp = parse_date(arg, 0) / 1000000;
2672}
2673
a6a92a9a
WG
2674static void opt_input_ts_offset(const char *arg)
2675{
2676 input_ts_offset = parse_date(arg, 1);
2677}
2678
b29f97d1 2679static void opt_input_file(const char *filename)
85f07f22
FB
2680{
2681 AVFormatContext *ic;
2682 AVFormatParameters params, *ap = &params;
14bea432 2683 int err, i, ret, rfps, rfps_base;
bd1b79a1 2684 int64_t timestamp;
85f07f22 2685
b242baa4
FB
2686 if (!strcmp(filename, "-"))
2687 filename = "pipe:";
2688
115329f1 2689 using_stdin |= !strncmp(filename, "pipe:", 5) ||
d9a916e2
CY
2690 !strcmp( filename, "/dev/stdin" );
2691
85f07f22 2692 /* get default parameters from command line */
4eb72c6b
NS
2693 ic = av_alloc_format_context();
2694
79fdaa4c 2695 memset(ap, 0, sizeof(*ap));
4eb72c6b 2696 ap->prealloced_context = 1;
79fdaa4c
FB
2697 ap->sample_rate = audio_sample_rate;
2698 ap->channels = audio_channels;
c0df9d75
MN
2699 ap->time_base.den = frame_rate;
2700 ap->time_base.num = frame_rate_base;
1ff93ffc
TK
2701 ap->width = frame_width + frame_padleft + frame_padright;
2702 ap->height = frame_height + frame_padtop + frame_padbottom;
817b23ff 2703 ap->image_format = image_format;
63167088 2704 ap->pix_fmt = frame_pix_fmt;
2639c651
RS
2705 ap->device = grab_device;
2706 ap->channel = video_channel;
2707 ap->standard = video_standard;
5b6d5596
MN
2708 ap->video_codec_id = video_codec_id;
2709 ap->audio_codec_id = audio_codec_id;
2710 if(pgmyuv_compatibility_hack)
2711 ap->video_codec_id= CODEC_ID_PGMYUV;
79fdaa4c 2712
4eb72c6b
NS
2713 for(i=0; i<opt_name_count; i++){
2714 AVOption *opt;
2715 double d= av_get_double(avformat_opts, opt_names[i], &opt);
2716 if(d==d && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2717 av_set_double(ic, opt_names[i], d);
2718 }
79fdaa4c
FB
2719 /* open the input file with generic libav function */
2720 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
85f07f22 2721 if (err < 0) {
79fdaa4c 2722 print_error(filename, err);
85f07f22
FB
2723 exit(1);
2724 }
30bc6613 2725
5894e1bb
VP
2726 ic->loop_input = loop_input;
2727
79fdaa4c
FB
2728 /* If not enough info to get the stream parameters, we decode the
2729 first frames to get it. (used in mpeg case for example) */
2730 ret = av_find_stream_info(ic);
d8019eb5 2731 if (ret < 0 && verbose >= 0) {
85f07f22
FB
2732 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2733 exit(1);
2734 }
2735
bd1b79a1
MN
2736 timestamp = start_time;
2737 /* add the stream start time */
2738 if (ic->start_time != AV_NOPTS_VALUE)
2739 timestamp += ic->start_time;
2740
254abc2e
FB
2741 /* if seeking requested, we execute it */
2742 if (start_time != 0) {
3ba1438d 2743 ret = av_seek_frame(ic, -1, timestamp, AVSEEK_FLAG_BACKWARD);
254abc2e 2744 if (ret < 0) {
115329f1 2745 fprintf(stderr, "%s: could not seek to position %0.3f\n",
254abc2e
FB
2746 filename, (double)timestamp / AV_TIME_BASE);
2747 }
2748 /* reset seek info */
2749 start_time = 0;
2750 }
2751
85f07f22
FB
2752 /* update the current parameters so that they match the one of the input stream */
2753 for(i=0;i<ic->nb_streams;i++) {
637b5326 2754 int j;
01f4895c 2755 AVCodecContext *enc = ic->streams[i]->codec;
2450cff2 2756#if defined(HAVE_THREADS)
c62c07d3
MN
2757 if(thread_count>1)
2758 avcodec_thread_init(enc, thread_count);
2759#endif
2760 enc->thread_count= thread_count;
85f07f22
FB
2761 switch(enc->codec_type) {
2762 case CODEC_TYPE_AUDIO:
637b5326
MN
2763 for(j=0; j<opt_name_count; j++){
2764 AVOption *opt;
2765 double d= av_get_double(avctx_opts, opt_names[j], &opt);
2766 if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2767 av_set_double(enc, opt_names[j], d);
2768 }
e0d2714a 2769 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
85f07f22
FB
2770 audio_channels = enc->channels;
2771 audio_sample_rate = enc->sample_rate;
b4aea108 2772 if(audio_disable)
f3356e9c 2773 ic->streams[i]->discard= AVDISCARD_ALL;
85f07f22
FB
2774 break;
2775 case CODEC_TYPE_VIDEO:
637b5326
MN
2776 for(j=0; j<opt_name_count; j++){
2777 AVOption *opt;
2778 double d= av_get_double(avctx_opts, opt_names[j], &opt);
2779 if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_DECODING_PARAM))
2780 av_set_double(enc, opt_names[j], d);
2781 }
85f07f22
FB
2782 frame_height = enc->height;
2783 frame_width = enc->width;
bb270c08
DB
2784 frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
2785 frame_pix_fmt = enc->pix_fmt;
c0df9d75
MN
2786 rfps = ic->streams[i]->r_frame_rate.num;
2787 rfps_base = ic->streams[i]->r_frame_rate.den;
fe670d09 2788 enc->workaround_bugs = workaround_bugs;
637b5326 2789 if(enc->lowres) enc->flags |= CODEC_FLAG_EMU_EDGE;
f4f3223f
MN
2790 if(me_threshold)
2791 enc->debug |= FF_DEBUG_MV;
d7425f59 2792
115329f1 2793 if (enc->time_base.den != rfps || enc->time_base.num != rfps_base) {
d8019eb5
AD
2794
2795 if (verbose >= 0)
15bc38e5 2796 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f (%d/%d) -> %2.2f (%d/%d)\n",
c0df9d75 2797 i, (float)enc->time_base.den / enc->time_base.num, enc->time_base.den, enc->time_base.num,
d8019eb5 2798
15bc38e5 2799 (float)rfps / rfps_base, rfps, rfps_base);
79fdaa4c 2800 }
bf5af568 2801 /* update the current frame rate to match the stream frame rate */
14bea432
MN
2802 frame_rate = rfps;
2803 frame_rate_base = rfps_base;
bdfcbbed
MK
2804
2805 enc->rate_emu = rate_emu;
b4aea108 2806 if(video_disable)
f3356e9c
MN
2807 ic->streams[i]->discard= AVDISCARD_ALL;
2808 else if(video_discard)
2809 ic->streams[i]->discard= video_discard;
85f07f22 2810 break;
254abc2e
FB
2811 case CODEC_TYPE_DATA:
2812 break;
cf7fc795
FB
2813 case CODEC_TYPE_SUBTITLE:
2814 break;
ae38261e
MB
2815 case CODEC_TYPE_UNKNOWN:
2816 break;
51bd4565 2817 default:
c04643a2 2818 av_abort();
85f07f22
FB
2819 }
2820 }
115329f1 2821
85f07f22 2822 input_files[nb_input_files] = ic;
bd1b79a1 2823 input_files_ts_offset[nb_input_files] = input_ts_offset - (copy_ts ? 0 : timestamp);
85f07f22 2824 /* dump the file content */
d8019eb5
AD
2825 if (verbose >= 0)
2826 dump_format(ic, nb_input_files, filename, 0);
2827
85f07f22 2828 nb_input_files++;
79fdaa4c
FB
2829 file_iformat = NULL;
2830 file_oformat = NULL;
817b23ff 2831 image_format = NULL;
bdfcbbed 2832
2639c651
RS
2833 grab_device = NULL;
2834 video_channel = 0;
115329f1 2835
bdfcbbed 2836 rate_emu = 0;
85f07f22
FB
2837}
2838
2639c651
RS
2839static void opt_grab(const char *arg)
2840{
2841 file_iformat = av_find_input_format(arg);
2842 opt_input_file("");
2843}
2844
b29f97d1 2845static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
919f448d
FB
2846{
2847 int has_video, has_audio, i, j;
2848 AVFormatContext *ic;
2849
2850 has_video = 0;
2851 has_audio = 0;
2852 for(j=0;j<nb_input_files;j++) {
2853 ic = input_files[j];
2854 for(i=0;i<ic->nb_streams;i++) {
01f4895c 2855 AVCodecContext *enc = ic->streams[i]->codec;
919f448d
FB
2856 switch(enc->codec_type) {
2857 case CODEC_TYPE_AUDIO:
2858 has_audio = 1;
2859 break;
2860 case CODEC_TYPE_VIDEO:
2861 has_video = 1;
2862 break;
6fb316d5 2863 case CODEC_TYPE_DATA:
ae38261e 2864 case CODEC_TYPE_UNKNOWN:
cf7fc795 2865 case CODEC_TYPE_SUBTITLE:
6fb316d5 2866 break;
51bd4565 2867 default:
c04643a2 2868 av_abort();
919f448d
FB
2869 }
2870 }
2871 }
2872 *has_video_ptr = has_video;
2873 *has_audio_ptr = has_audio;
2874}
2875
cf7fc795 2876static void new_video_stream(AVFormatContext *oc)
85f07f22
FB
2877{
2878 AVStream *st;
cf7fc795
FB
2879 AVCodecContext *video_enc;
2880 int codec_id;
115329f1 2881
cf7fc795
FB
2882 st = av_new_stream(oc, oc->nb_streams);
2883 if (!st) {
2884 fprintf(stderr, "Could not alloc stream\n");
2885 exit(1);
2886 }
748c2fca
MN
2887 bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;
2888 video_bitstream_filters= NULL;
2889
cf7fc795
FB
2890#if defined(HAVE_THREADS)
2891 if(thread_count>1)
01f4895c 2892 avcodec_thread_init(st->codec, thread_count);
cf7fc795 2893#endif
115329f1 2894
01f4895c 2895 video_enc = st->codec;
115329f1 2896
cf7fc795
FB
2897 if(video_codec_tag)
2898 video_enc->codec_tag= video_codec_tag;
115329f1 2899
90ad92b3 2900 if( (video_global_header&1)
637b5326 2901 || (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))){
cf7fc795 2902 video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
637b5326
MN
2903 avctx_opts->flags|= CODEC_FLAG_GLOBAL_HEADER;
2904 }
2905 if(video_global_header&2){
90ad92b3 2906 video_enc->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
637b5326
MN
2907 avctx_opts->flags2|= CODEC_FLAG2_LOCAL_HEADER;
2908 }
90ad92b3 2909
cf7fc795
FB
2910 if (video_stream_copy) {
2911 st->stream_copy = 1;
2912 video_enc->codec_type = CODEC_TYPE_VIDEO;
2913 } else {
2914 char *p;
2915 int i;
2916 AVCodec *codec;
115329f1 2917
cf7fc795
FB
2918 codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_VIDEO);
2919 if (video_codec_id != CODEC_ID_NONE)
2920 codec_id = video_codec_id;
115329f1 2921
cf7fc795
FB
2922 video_enc->codec_id = codec_id;
2923 codec = avcodec_find_encoder(codec_id);
115329f1 2924
8bbf6db9
MN
2925 for(i=0; i<opt_name_count; i++){
2926 AVOption *opt;
2927 double d= av_get_double(avctx_opts, opt_names[i], &opt);
2928 if(d==d && (opt->flags&AV_OPT_FLAG_VIDEO_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
2929 av_set_double(video_enc, opt_names[i], d);
2930 }
115329f1 2931
115329f1
DB
2932 video_enc->time_base.den = frame_rate;
2933 video_enc->time_base.num = frame_rate_base;
cf7fc795
FB
2934 if(codec && codec->supported_framerates){
2935 const AVRational *p= codec->supported_framerates;
2936 AVRational req= (AVRational){frame_rate, frame_rate_base};
2937 const AVRational *best=NULL;
2938 AVRational best_error= (AVRational){INT_MAX, 1};
2939 for(; p->den!=0; p++){
2940 AVRational error= av_sub_q(req, *p);
2941 if(error.num <0) error.num *= -1;
2942 if(av_cmp_q(error, best_error) < 0){
2943 best_error= error;
2944 best= p;
2945 }
2946 }
2947 video_enc->time_base.den= best->num;
2948 video_enc->time_base.num= best->den;
2949 }
115329f1 2950
cf7fc795
FB
2951 video_enc->width = frame_width + frame_padright + frame_padleft;
2952 video_enc->height = frame_height + frame_padtop + frame_padbottom;
e7268d51 2953 video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*video_enc->height/video_enc->width, 255);
cf7fc795
FB
2954 video_enc->pix_fmt = frame_pix_fmt;
2955
2956 if(codec && codec->pix_fmts){
2957 const enum PixelFormat *p= codec->pix_fmts;
2958 for(; *p!=-1; p++){
2959 if(*p == video_enc->pix_fmt)
2960 break;
2961 }
2962 if(*p == -1)
2963 video_enc->pix_fmt = codec->pix_fmts[0];
2964 }
2965
babe0e8c 2966 if (intra_only)
cf7fc795
FB
2967 video_enc->gop_size = 0;
2968 if (video_qscale || same_quality) {
2969 video_enc->flags |= CODEC_FLAG_QSCALE;
115329f1 2970 video_enc->global_quality=
cf7fc795
FB
2971 st->quality = FF_QP2LAMBDA * video_qscale;
2972 }
2973
2974 if(intra_matrix)
2975 video_enc->intra_matrix = intra_matrix;
2976 if(inter_matrix)
2977 video_enc->inter_matrix = inter_matrix;
2978
3c96abac 2979 video_enc->max_b_frames = b_frames;
cf7fc795
FB
2980 video_enc->qmin = video_qmin;
2981 video_enc->qmax = video_qmax;
2982 video_enc->lmin = video_lmin;
2983 video_enc->lmax = video_lmax;
2984 video_enc->rc_qsquish = video_qsquish;
cf7fc795
FB
2985 video_enc->mb_lmin = video_mb_lmin;
2986 video_enc->mb_lmax = video_mb_lmax;
2987 video_enc->max_qdiff = video_qdiff;
cf7fc795 2988 video_enc->rc_eq = video_rc_eq;
cf7fc795
FB
2989 video_enc->workaround_bugs = workaround_bugs;
2990 video_enc->thread_count = thread_count;
2991 p= video_rc_override_string;
2992 for(i=0; p; i++){
2993 int start, end, q;
2994 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2995 if(e!=3){
2996 fprintf(stderr, "error parsing rc_override\n");
2997 exit(1);
2998 }
115329f1
DB
2999 video_enc->rc_override=
3000 av_realloc(video_enc->rc_override,
cf7fc795
FB
3001 sizeof(RcOverride)*(i+1));
3002 video_enc->rc_override[i].start_frame= start;
3003 video_enc->rc_override[i].end_frame = end;
3004 if(q>0){
3005 video_enc->rc_override[i].qscale= q;
3006 video_enc->rc_override[i].quality_factor= 1.0;
3007 }
3008 else{
3009 video_enc->rc_override[i].qscale= 0;
3010 video_enc->rc_override[i].quality_factor= -q/100.0;
3011 }
3012 p= strchr(p, '/');
3013 if(p) p++;
3014 }
3015 video_enc->rc_override_count=i;
3016
cf7fc795
FB
3017 video_enc->rc_buffer_size = video_rc_buffer_size;
3018 video_enc->rc_initial_buffer_occupancy = video_rc_buffer_size*3/4;
3019 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
cf7fc795
FB
3020 video_enc->me_threshold= me_threshold;
3021 video_enc->mb_threshold= mb_threshold;
3022 video_enc->intra_dc_precision= intra_dc_precision - 8;
3023 video_enc->strict_std_compliance = strict;
3024 video_enc->error_rate = error_rate;
cf7fc795 3025 video_enc->scenechange_threshold= sc_threshold;
cf7fc795
FB
3026 video_enc->me_penalty_compensation= me_penalty_compensation;
3027 video_enc->frame_skip_threshold= frame_skip_threshold;
3028 video_enc->frame_skip_factor= frame_skip_factor;
3029 video_enc->frame_skip_exp= frame_skip_exp;
cf7fc795
FB
3030
3031 if(packet_size){
3032 video_enc->rtp_mode= 1;
3033 video_enc->rtp_payload_size= packet_size;
3034 }
115329f1 3035
cf7fc795
FB
3036 if (do_psnr)
3037 video_enc->flags|= CODEC_FLAG_PSNR;
115329f1 3038
cf7fc795
FB
3039 video_enc->me_method = me_method;
3040
3041 /* two pass mode */
3042 if (do_pass) {
3043 if (do_pass == 1) {
3044 video_enc->flags |= CODEC_FLAG_PASS1;
3045 } else {
3046 video_enc->flags |= CODEC_FLAG_PASS2;
3047 }
3048 }
3049 }
3050
3051 /* reset some key parameters */
3052 video_disable = 0;
3053 video_codec_id = CODEC_ID_NONE;
3054 video_stream_copy = 0;
3055}
3056
3057static void new_audio_stream(AVFormatContext *oc)
3058{
3059 AVStream *st;
3060 AVCodecContext *audio_enc;
8bbf6db9 3061 int codec_id, i;
115329f1 3062
cf7fc795
FB
3063 st = av_new_stream(oc, oc->nb_streams);
3064 if (!st) {
3065 fprintf(stderr, "Could not alloc stream\n");
3066 exit(1);
3067 }
748c2fca
MN
3068
3069 bitstream_filters[nb_output_files][oc->nb_streams - 1]= audio_bitstream_filters;
3070 audio_bitstream_filters= NULL;
3071
cf7fc795
FB
3072#if defined(HAVE_THREADS)
3073 if(thread_count>1)
01f4895c 3074 avcodec_thread_init(st->codec, thread_count);
cf7fc795 3075#endif
115329f1 3076
01f4895c 3077 audio_enc = st->codec;
cf7fc795 3078 audio_enc->codec_type = CODEC_TYPE_AUDIO;
115329f1 3079
cf7fc795
FB
3080 if(audio_codec_tag)
3081 audio_enc->codec_tag= audio_codec_tag;
115329f1 3082
637b5326 3083 if (oc->oformat->flags & AVFMT_GLOBALHEADER) {
cf7fc795 3084 audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
637b5326
MN
3085 avctx_opts->flags|= CODEC_FLAG_GLOBAL_HEADER;
3086 }
cf7fc795
FB
3087 if (audio_stream_copy) {
3088 st->stream_copy = 1;
3089 audio_enc->channels = audio_channels;
3090 } else {
3091 codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, CODEC_TYPE_AUDIO);
115329f1 3092
8bbf6db9
MN
3093 for(i=0; i<opt_name_count; i++){
3094 AVOption *opt;
3095 double d= av_get_double(avctx_opts, opt_names[i], &opt);
3096 if(d==d && (opt->flags&AV_OPT_FLAG_AUDIO_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
3097 av_set_double(audio_enc, opt_names[i], d);
3098 }
115329f1 3099
cf7fc795
FB
3100 if (audio_codec_id != CODEC_ID_NONE)
3101 codec_id = audio_codec_id;
3102 audio_enc->codec_id = codec_id;
115329f1 3103
cf7fc795 3104 audio_enc->bit_rate = audio_bit_rate;
c57c770d
JR
3105 if (audio_qscale > QSCALE_NONE) {
3106 audio_enc->flags |= CODEC_FLAG_QSCALE;
3107 audio_enc->global_quality = st->quality = FF_QP2LAMBDA * audio_qscale;
3108 }
cf7fc795
FB
3109 audio_enc->strict_std_compliance = strict;
3110 audio_enc->thread_count = thread_count;
3111 /* For audio codecs other than AC3 or DTS we limit */
3112 /* the number of coded channels to stereo */
3113 if (audio_channels > 2 && codec_id != CODEC_ID_AC3
3114 && codec_id != CODEC_ID_DTS) {
3115 audio_enc->channels = 2;
3116 } else
3117 audio_enc->channels = audio_channels;
3118 }
3119 audio_enc->sample_rate = audio_sample_rate;
78e03516 3120 audio_enc->time_base= (AVRational){1, audio_sample_rate};
cf7fc795
FB
3121 if (audio_language) {
3122 pstrcpy(st->language, sizeof(st->language), audio_language);
3123 av_free(audio_language);
3124 audio_language = NULL;
3125 }
3126
3127 /* reset some key parameters */
3128 audio_disable = 0;
3129 audio_codec_id = CODEC_ID_NONE;
3130 audio_stream_copy = 0;
3131}
3132
3133static void opt_new_subtitle_stream(void)
3134{
3135 AVFormatContext *oc;
3136 AVStream *st;
3137 AVCodecContext *subtitle_enc;
8bbf6db9 3138 int i;
115329f1 3139
cf7fc795
FB
3140 if (nb_output_files <= 0) {
3141 fprintf(stderr, "At least one output file must be specified\n");
3142 exit(1);
3143 }
3144 oc = output_files[nb_output_files - 1];
3145
3146 st = av_new_stream(oc, oc->nb_streams);
3147 if (!st) {
3148 fprintf(stderr, "Could not alloc stream\n");
3149 exit(1);
3150 }
3151
01f4895c 3152 subtitle_enc = st->codec;
cf7fc795
FB
3153 subtitle_enc->codec_type = CODEC_TYPE_SUBTITLE;
3154 if (subtitle_stream_copy) {
3155 st->stream_copy = 1;
3156 } else {
8bbf6db9
MN
3157 for(i=0; i<opt_name_count; i++){
3158 AVOption *opt;
3159 double d= av_get_double(avctx_opts, opt_names[i], &opt);
3160 if(d==d && (opt->flags&AV_OPT_FLAG_SUBTITLE_PARAM) && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
3161 av_set_double(subtitle_enc, opt_names[i], d);
3162 }
cf7fc795
FB
3163 subtitle_enc->codec_id = subtitle_codec_id;
3164 }
3165
3166 if (subtitle_language) {
3167 pstrcpy(st->language, sizeof(st->language), subtitle_language);
3168 av_free(subtitle_language);
3169 subtitle_language = NULL;
3170 }
3171
3172 subtitle_codec_id = CODEC_ID_NONE;
3173 subtitle_stream_copy = 0;
3174}
3175
3176static void opt_new_audio_stream(void)
3177{
3178 AVFormatContext *oc;
3179 if (nb_output_files <= 0) {
3180 fprintf(stderr, "At least one output file must be specified\n");
3181 exit(1);
3182 }
3183 oc = output_files[nb_output_files - 1];
3184 new_audio_stream(oc);
3185}
3186
3187static void opt_new_video_stream(void)
3188{
3189 AVFormatContext *oc;
3190 if (nb_output_files <= 0) {
3191 fprintf(stderr, "At least one output file must be specified\n");
3192 exit(1);
3193 }
3194 oc = output_files[nb_output_files - 1];
3195 new_video_stream(oc);
3196}
3197
3198static void opt_output_file(const char *filename)
3199{
3200 AVFormatContext *oc;
8e16b25c 3201 int use_video, use_audio, input_has_video, input_has_audio, i;
817b23ff 3202 AVFormatParameters params, *ap = &params;
85f07f22
FB
3203
3204 if (!strcmp(filename, "-"))
3205 filename = "pipe:";
3206
bc874dae 3207 oc = av_alloc_format_context();
85f07f22 3208
79fdaa4c
FB
3209 if (!file_oformat) {
3210 file_oformat = guess_format(NULL, filename, NULL);
3211 if (!file_oformat) {
3212 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
3213 filename);
3214 exit(1);
3215 }
85f07f22 3216 }
115329f1 3217
79fdaa4c 3218 oc->oformat = file_oformat;
cf7fc795 3219 pstrcpy(oc->filename, sizeof(oc->filename), filename);
85f07f22 3220
115329f1 3221 if (!strcmp(file_oformat->name, "ffm") &&
85f07f22
FB
3222 strstart(filename, "http:", NULL)) {
3223 /* special case for files sent to ffserver: we get the stream
3224 parameters from ffserver */
3225 if (read_ffserver_streams(oc, filename) < 0) {
3226 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
3227 exit(1);
3228 }
3229 } else {
f2d9a9f5
LA
3230 use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_id != CODEC_ID_NONE;
3231 use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy || audio_codec_id != CODEC_ID_NONE;
919f448d 3232
e30a2846
FB
3233 /* disable if no corresponding type found and at least one
3234 input file */
3235 if (nb_input_files > 0) {
3236 check_audio_video_inputs(&input_has_video, &input_has_audio);
3237 if (!input_has_video)
3238 use_video = 0;
3239 if (!input_has_audio)
3240 use_audio = 0;
3241 }
919f448d
FB
3242
3243 /* manual disable */
85f07f22
FB
3244 if (audio_disable) {
3245 use_audio = 0;
3246 }
3247 if (video_disable) {
3248 use_video = 0;
3249 }
115329f1 3250
85f07f22 3251 if (use_video) {
cf7fc795 3252 new_video_stream(oc);
85f07f22 3253 }
115329f1 3254
85f07f22 3255 if (use_audio) {
cf7fc795 3256 new_audio_stream(oc);
85f07f22
FB
3257 }
3258
cf7fc795 3259 if (!oc->nb_streams) {
919f448d 3260 fprintf(stderr, "No audio or video streams available\n");
85f07f22
FB
3261 exit(1);
3262 }
3263
4568325a 3264 oc->timestamp = rec_timestamp;
115329f1 3265
bb270c08 3266 if (str_title)
79fdaa4c 3267 pstrcpy(oc->title, sizeof(oc->title), str_title);
85f07f22 3268 if (str_author)
79fdaa4c 3269 pstrcpy(oc->author, sizeof(oc->author), str_author);
85f07f22 3270 if (str_copyright)
79fdaa4c 3271 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
85f07f22 3272 if (str_comment)
79fdaa4c 3273 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
63d5075c
PI
3274 if (str_album)
3275 pstrcpy(oc->album, sizeof(oc->album), str_album);
85f07f22
FB
3276 }
3277
1629626f 3278 output_files[nb_output_files++] = oc;
85f07f22 3279
919f448d 3280 /* check filename in case of an image number is expected */
79fdaa4c 3281 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
5c07cf53 3282 if (!av_filename_number_test(oc->filename)) {
79fdaa4c 3283 print_error(oc->filename, AVERROR_NUMEXPECTED);
919f448d 3284 exit(1);
79fdaa4c 3285 }
919f448d
FB
3286 }
3287
79fdaa4c 3288 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
85f07f22 3289 /* test if it already exists to avoid loosing precious files */
115329f1 3290 if (!file_overwrite &&
85f07f22
FB
3291 (strchr(filename, ':') == NULL ||
3292 strstart(filename, "file:", NULL))) {
3293 if (url_exist(filename)) {
3294 int c;
115329f1 3295
d9a916e2
CY
3296 if ( !using_stdin ) {
3297 fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
3298 fflush(stderr);
3299 c = getchar();
3300 if (toupper(c) != 'Y') {
3301 fprintf(stderr, "Not overwriting - exiting\n");
3302 exit(1);
3303 }
bb270c08
DB
3304 }
3305 else {
d9a916e2 3306 fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
85f07f22 3307 exit(1);
bb270c08 3308 }
85f07f22
FB
3309 }
3310 }
115329f1 3311
85f07f22
FB
3312 /* open the file */
3313 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
3314 fprintf(stderr, "Could not open '%s'\n", filename);
3315 exit(1);
3316 }
3317 }
3318
817b23ff
FB
3319 memset(ap, 0, sizeof(*ap));
3320 ap->image_format = image_format;
3321 if (av_set_parameters(oc, ap) < 0) {
3322 fprintf(stderr, "%s: Invalid encoding parameters\n",
3323 oc->filename);
3324 exit(1);
3325 }
3326
2db3c638
MN
3327 oc->packet_size= mux_packet_size;
3328 oc->mux_rate= mux_rate;
17c88cb0
MN
3329 oc->preload= (int)(mux_preload*AV_TIME_BASE);
3330 oc->max_delay= (int)(mux_max_delay*AV_TIME_BASE);
8108551a 3331 oc->loop_output = loop_output;
2db3c638 3332
8e16b25c
PI
3333 for(i=0; i<opt_name_count; i++){
3334 AVOption *opt;
3335 double d = av_get_double(avformat_opts, opt_names[i], &opt);
3336 if(d==d && (opt->flags&AV_OPT_FLAG_ENCODING_PARAM))
3337 av_set_double(oc, opt_names[i], d);
3338 }
3339
85f07f22 3340 /* reset some options */
79fdaa4c
FB
3341 file_oformat = NULL;
3342 file_iformat = NULL;
817b23ff 3343 image_format = NULL;
85f07f22
FB
3344}
3345
a38469e1 3346/* prepare dummy protocols for grab */
b29f97d1 3347static void prepare_grab(void)
a38469e1
FB
3348{
3349 int has_video, has_audio, i, j;
3350 AVFormatContext *oc;
3351 AVFormatContext *ic;
79a7c268 3352 AVFormatParameters vp1, *vp = &vp1;
a38469e1 3353 AVFormatParameters ap1, *ap = &ap1;
115329f1 3354
a38469e1
FB
3355 /* see if audio/video inputs are needed */
3356 has_video = 0;
3357 has_audio = 0;
3358 memset(ap, 0, sizeof(*ap));
79a7c268 3359 memset(vp, 0, sizeof(*vp));
c0df9d75 3360 vp->time_base.num= 1;
a38469e1
FB
3361 for(j=0;j<nb_output_files;j++) {
3362 oc = output_files[j];
3363 for(i=0;i<oc->nb_streams;i++) {
01f4895c 3364 AVCodecContext *enc = oc->streams[i]->codec;
a38469e1
FB
3365 switch(enc->codec_type) {
3366 case CODEC_TYPE_AUDIO:
3367 if (enc->sample_rate > ap->sample_rate)
3368 ap->sample_rate = enc->sample_rate;
3369 if (enc->channels > ap->channels)
3370 ap->channels = enc->channels;
3371 has_audio = 1;
3372 break;
3373 case CODEC_TYPE_VIDEO:
79a7c268
FB
3374 if (enc->width > vp->width)
3375 vp->width = enc->width;
3376 if (enc->height > vp->height)
3377 vp->height = enc->height;
115329f1 3378
c0df9d75
MN
3379 if (vp->time_base.num*(int64_t)enc->time_base.den > enc->time_base.num*(int64_t)vp->time_base.den){
3380 vp->time_base = enc->time_base;
b997b34f
LA
3381 vp->width += frame_leftBand + frame_rightBand;