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