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