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