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