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