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