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