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