various resampling fixes
[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
ff906888 1022 if (pkt && pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
e928649b 1023 ist->next_pts = ist->pts = pkt->dts;
a700a6ae
FB
1024 } else {
1025 ist->pts = ist->next_pts;
1026 }
ff906888 1027
a700a6ae
FB
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;
23ffe323
MN
1695 double ipts_min= 1e100;
1696 double opts_min= 1e100;
1697
85f07f22 1698 redo:
a38469e1 1699 /* if 'q' pressed, exits */
d9a916e2 1700 if (!using_stdin) {
b51469a0
LS
1701 if (q_pressed)
1702 break;
cb09b2ed
PG
1703 /* read_key() returns 0 on EOF */
1704 key = read_key();
1705 if (key == 'q')
1706 break;
1707 }
a38469e1 1708
ec5517d5
FB
1709 /* select the stream that we must read now by looking at the
1710 smallest output pts */
85f07f22 1711 file_index = -1;
ec5517d5 1712 for(i=0;i<nb_ostreams;i++) {
23ffe323 1713 double ipts, opts;
ec5517d5
FB
1714 ost = ost_table[i];
1715 os = output_files[ost->file_index];
1716 ist = ist_table[ost->source_index];
23ffe323
MN
1717 if(ost->st->codec.codec_type == CODEC_TYPE_VIDEO)
1718 opts = (double)ost->sync_opts * ost->st->codec.frame_rate_base / ost->st->codec.frame_rate;
1719 else
1720 opts = (double)ost->st->pts.val * ost->st->time_base.num / ost->st->time_base.den;
1721 ipts = (double)ist->pts;
1722 if (!file_table[ist->file_index].eof_reached){
1723 if(ipts < ipts_min) {
1724 ipts_min = ipts;
1725 if(input_sync ) file_index = ist->file_index;
1726 }
1727 if(opts < opts_min) {
1728 opts_min = opts;
1729 if(!input_sync) file_index = ist->file_index;
1730 }
85f07f22
FB
1731 }
1732 }
1733 /* if none, if is finished */
51bd4565 1734 if (file_index < 0) {
85f07f22 1735 break;
ec5517d5
FB
1736 }
1737
85f07f22 1738 /* finish if recording time exhausted */
23ffe323 1739 if (recording_time > 0 && opts_min >= (recording_time / 1000000.0))
85f07f22 1740 break;
ec5517d5 1741
254abc2e 1742 /* read a frame from it and output it in the fifo */
85f07f22 1743 is = input_files[file_index];
254abc2e 1744 if (av_read_frame(is, &pkt) < 0) {
85f07f22
FB
1745 file_table[file_index].eof_reached = 1;
1746 continue;
1747 }
303e50e6 1748
51bd4565
PG
1749 if (!pkt.size) {
1750 stream_no_data = is;
1751 } else {
1752 stream_no_data = 0;
1753 }
254abc2e
FB
1754 if (do_pkt_dump) {
1755 av_pkt_dump(stdout, &pkt, do_hex_dump);
817b23ff 1756 }
79fdaa4c
FB
1757 /* the following test is needed in case new streams appear
1758 dynamically in stream : we ignore them */
1759 if (pkt.stream_index >= file_table[file_index].nb_streams)
bf5af568 1760 goto discard_packet;
85f07f22
FB
1761 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1762 ist = ist_table[ist_index];
bf5af568
FB
1763 if (ist->discard)
1764 goto discard_packet;
85f07f22 1765
72bd8100
MN
1766// 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);
1767 if (pkt.dts != AV_NOPTS_VALUE) {
1768 int64_t delta= pkt.dts - ist->next_pts;
1769 if(ABS(delta) > 10LL*AV_TIME_BASE && !copy_ts){
1770 input_files_ts_offset[ist->file_index]-= delta;
1771 if (verbose > 2)
1772 fprintf(stderr, "timestamp discontinuity %lld, new offset= %lld\n", delta, input_files_ts_offset[ist->file_index]);
1773 for(i=0; i<file_table[file_index].nb_streams; i++){
1774 int index= file_table[file_index].ist_index + i;
1775 ist_table[index]->next_pts += delta;
1776 }
1777 }
1778 }
1779
8831db5c 1780 //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
a700a6ae 1781 if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
d8019eb5
AD
1782
1783 if (verbose >= 0)
1784 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1785 ist->file_index, ist->index);
1786
a700a6ae
FB
1787 av_free_packet(&pkt);
1788 goto redo;
fe08925f 1789 }
a700a6ae 1790
bf5af568 1791 discard_packet:
85f07f22
FB
1792 av_free_packet(&pkt);
1793
ec5517d5
FB
1794 /* dump report by using the output first video and audio streams */
1795 print_report(output_files, ost_table, nb_ostreams, 0);
85f07f22 1796 }
a700a6ae
FB
1797
1798 /* at the end of stream, we must flush the decoder buffers */
1799 for(i=0;i<nb_istreams;i++) {
1800 ist = ist_table[i];
1801 if (ist->decoding_needed) {
1802 output_packet(ist, i, ost_table, nb_ostreams, NULL);
1803 }
1804 }
1805
a38469e1 1806 term_exit();
85f07f22 1807
c4e37247
MN
1808 /* write the trailer if needed and close file */
1809 for(i=0;i<nb_output_files;i++) {
1810 os = output_files[i];
1811 av_write_trailer(os);
1812 }
1813
37f5cd5a
MN
1814 /* dump report by using the first video and audio streams */
1815 print_report(output_files, ost_table, nb_ostreams, 1);
1816
85f07f22
FB
1817 /* close each encoder */
1818 for(i=0;i<nb_ostreams;i++) {
1819 ost = ost_table[i];
1820 if (ost->encoding_needed) {
5abdb4b1 1821 av_freep(&ost->st->codec.stats_in);
85f07f22
FB
1822 avcodec_close(&ost->st->codec);
1823 }
1824 }
1825
1826 /* close each decoder */
1827 for(i=0;i<nb_istreams;i++) {
1828 ist = ist_table[i];
1829 if (ist->decoding_needed) {
1830 avcodec_close(&ist->st->codec);
1831 }
1832 }
85f07f22 1833
85f07f22
FB
1834 /* finished ! */
1835
1836 ret = 0;
1837 fail1:
0f1578af 1838 av_free(file_table);
bdc4796f 1839
85f07f22
FB
1840 if (ist_table) {
1841 for(i=0;i<nb_istreams;i++) {
1842 ist = ist_table[i];
0f1578af 1843 av_free(ist);
85f07f22 1844 }
0f1578af 1845 av_free(ist_table);
85f07f22
FB
1846 }
1847 if (ost_table) {
1848 for(i=0;i<nb_ostreams;i++) {
1849 ost = ost_table[i];
1850 if (ost) {
5abdb4b1
FB
1851 if (ost->logfile) {
1852 fclose(ost->logfile);
1853 ost->logfile = NULL;
1854 }
bf5af568
FB
1855 fifo_free(&ost->fifo); /* works even if fifo is not
1856 initialized but set to zero */
0f1578af 1857 av_free(ost->pict_tmp.data[0]);
85f07f22
FB
1858 if (ost->video_resample)
1859 img_resample_close(ost->img_resample_ctx);
1860 if (ost->audio_resample)
1861 audio_resample_close(ost->resample);
0f1578af 1862 av_free(ost);
85f07f22
FB
1863 }
1864 }
0f1578af 1865 av_free(ost_table);
85f07f22
FB
1866 }
1867 return ret;
1868 fail:
1869 ret = -ENOMEM;
1870 goto fail1;
1871}
1872
1873#if 0
1874int file_read(const char *filename)
1875{
1876 URLContext *h;
1877 unsigned char buffer[1024];
1878 int len, i;
1879
1880 if (url_open(&h, filename, O_RDONLY) < 0) {
1881 printf("could not open '%s'\n", filename);
1882 return -1;
1883 }
1884 for(;;) {
1885 len = url_read(h, buffer, sizeof(buffer));
1886 if (len <= 0)
1887 break;
1888 for(i=0;i<len;i++) putchar(buffer[i]);
1889 }
1890 url_close(h);
1891 return 0;
1892}
1893#endif
1894
b29f97d1 1895static void opt_image_format(const char *arg)
817b23ff
FB
1896{
1897 AVImageFormat *f;
1898
1899 for(f = first_image_format; f != NULL; f = f->next) {
1900 if (!strcmp(arg, f->name))
1901 break;
1902 }
1903 if (!f) {
1904 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1905 exit(1);
1906 }
1907 image_format = f;
1908}
1909
b29f97d1 1910static void opt_format(const char *arg)
85f07f22 1911{
817b23ff
FB
1912 /* compatibility stuff for pgmyuv */
1913 if (!strcmp(arg, "pgmyuv")) {
1914 opt_image_format(arg);
1915 arg = "image";
1916 }
1917
79fdaa4c
FB
1918 file_iformat = av_find_input_format(arg);
1919 file_oformat = guess_format(arg, NULL, NULL);
1920 if (!file_iformat && !file_oformat) {
1921 fprintf(stderr, "Unknown input or output format: %s\n", arg);
85f07f22
FB
1922 exit(1);
1923 }
85f07f22
FB
1924}
1925
b29f97d1 1926static void opt_video_bitrate(const char *arg)
85f07f22
FB
1927{
1928 video_bit_rate = atoi(arg) * 1000;
1929}
1930
b29f97d1 1931static void opt_video_bitrate_tolerance(const char *arg)
9cdd6a24
MN
1932{
1933 video_bit_rate_tolerance = atoi(arg) * 1000;
1934}
1935
b29f97d1 1936static void opt_video_bitrate_max(const char *arg)
3aa102be
MN
1937{
1938 video_rc_max_rate = atoi(arg) * 1000;
1939}
1940
b29f97d1 1941static void opt_video_bitrate_min(const char *arg)
3aa102be
MN
1942{
1943 video_rc_min_rate = atoi(arg) * 1000;
1944}
1945
b29f97d1 1946static void opt_video_buffer_size(const char *arg)
946c8a12 1947{
622348f9 1948 video_rc_buffer_size = atoi(arg) * 8*1024;
946c8a12
MN
1949}
1950
b29f97d1 1951static void opt_video_rc_eq(char *arg)
3aa102be
MN
1952{
1953 video_rc_eq = arg;
1954}
1955
b29f97d1 1956static void opt_video_rc_override_string(char *arg)
ac2830ec
MN
1957{
1958 video_rc_override_string = arg;
1959}
1960
3aa102be 1961
b29f97d1 1962static void opt_workaround_bugs(const char *arg)
fe670d09
MN
1963{
1964 workaround_bugs = atoi(arg);
1965}
1966
b29f97d1 1967static void opt_dct_algo(const char *arg)
463678ac
MN
1968{
1969 dct_algo = atoi(arg);
1970}
1971
b29f97d1 1972static void opt_idct_algo(const char *arg)
2ad1516a
MN
1973{
1974 idct_algo = atoi(arg);
1975}
1976
f4f3223f
MN
1977static void opt_me_threshold(const char *arg)
1978{
1979 me_threshold = atoi(arg);
1980}
1981
f20f8a8b
MN
1982static void opt_mb_threshold(const char *arg)
1983{
1984 mb_threshold = atoi(arg);
1985}
2ad1516a 1986
b29f97d1 1987static void opt_error_resilience(const char *arg)
8409b8fe
MN
1988{
1989 error_resilience = atoi(arg);
1990}
1991
b29f97d1 1992static void opt_error_concealment(const char *arg)
4d2858de
MN
1993{
1994 error_concealment = atoi(arg);
1995}
1996
b29f97d1 1997static void opt_debug(const char *arg)
59b571c1
MN
1998{
1999 debug = atoi(arg);
2000}
4d2858de 2001
0c9bbaec
WH
2002static void opt_vismv(const char *arg)
2003{
2004 debug_mv = atoi(arg);
2005}
2006
f068206e
BE
2007static void opt_verbose(const char *arg)
2008{
2009 verbose = atoi(arg);
d8019eb5 2010 av_log_set_level(atoi(arg));
f068206e
BE
2011}
2012
f4f3223f
MN
2013static void opt_sync_method(const char *arg)
2014{
2015 sync_method = atoi(arg);
2016}
2017
b29f97d1 2018static void opt_frame_rate(const char *arg)
85f07f22 2019{
445f1b83
RS
2020 if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
2021 fprintf(stderr, "Incorrect frame rate\n");
2022 exit(1);
2023 }
85f07f22
FB
2024}
2025
b29f97d1 2026static void opt_frame_crop_top(const char *arg)
ab6d194a
MN
2027{
2028 frame_topBand = atoi(arg);
2029 if (frame_topBand < 0) {
2030 fprintf(stderr, "Incorrect top crop size\n");
2031 exit(1);
2032 }
2033 if ((frame_topBand % 2) != 0) {
2034 fprintf(stderr, "Top crop size must be a multiple of 2\n");
2035 exit(1);
2036 }
2037 if ((frame_topBand) >= frame_height){
2038 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2039 exit(1);
2040 }
2041 frame_height -= frame_topBand;
2042}
2043
b29f97d1 2044static void opt_frame_crop_bottom(const char *arg)
ab6d194a
MN
2045{
2046 frame_bottomBand = atoi(arg);
2047 if (frame_bottomBand < 0) {
2048 fprintf(stderr, "Incorrect bottom crop size\n");
2049 exit(1);
2050 }
2051 if ((frame_bottomBand % 2) != 0) {
2052 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
2053 exit(1);
2054 }
2055 if ((frame_bottomBand) >= frame_height){
2056 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2057 exit(1);
2058 }
2059 frame_height -= frame_bottomBand;
2060}
2061
b29f97d1 2062static void opt_frame_crop_left(const char *arg)
ab6d194a
MN
2063{
2064 frame_leftBand = atoi(arg);
2065 if (frame_leftBand < 0) {
2066 fprintf(stderr, "Incorrect left crop size\n");
2067 exit(1);
2068 }
2069 if ((frame_leftBand % 2) != 0) {
2070 fprintf(stderr, "Left crop size must be a multiple of 2\n");
2071 exit(1);
2072 }
2073 if ((frame_leftBand) >= frame_width){
2074 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2075 exit(1);
2076 }
2077 frame_width -= frame_leftBand;
2078}
2079
b29f97d1 2080static void opt_frame_crop_right(const char *arg)
ab6d194a
MN
2081{
2082 frame_rightBand = atoi(arg);
2083 if (frame_rightBand < 0) {
2084 fprintf(stderr, "Incorrect right crop size\n");
2085 exit(1);
2086 }
2087 if ((frame_rightBand % 2) != 0) {
2088 fprintf(stderr, "Right crop size must be a multiple of 2\n");
2089 exit(1);
2090 }
2091 if ((frame_rightBand) >= frame_width){
2092 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
2093 exit(1);
2094 }
2095 frame_width -= frame_rightBand;
2096}
2097
b29f97d1 2098static void opt_frame_size(const char *arg)
85f07f22 2099{
445f1b83 2100 if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
85f07f22
FB
2101 fprintf(stderr, "Incorrect frame size\n");
2102 exit(1);
2103 }
2104 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
2105 fprintf(stderr, "Frame size must be a multiple of 2\n");
2106 exit(1);
2107 }
2108}
2109
1ff93ffc
TK
2110
2111#define SCALEBITS 10
2112#define ONE_HALF (1 << (SCALEBITS - 1))
2113#define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
2114
2115#define RGB_TO_Y(r, g, b) \
2116((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
2117 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
2118
2119#define RGB_TO_U(r1, g1, b1, shift)\
2120(((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
2121 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2122
2123#define RGB_TO_V(r1, g1, b1, shift)\
2124(((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
2125 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
2126
2127static void opt_pad_color(const char *arg) {
2128 /* Input is expected to be six hex digits similar to
2129 how colors are expressed in html tags (but without the #) */
2130 int rgb = strtol(arg, NULL, 16);
2131 int r,g,b;
2132
2133 r = (rgb >> 16);
2134 g = ((rgb >> 8) & 255);
2135 b = (rgb & 255);
2136
2137 padcolor[0] = RGB_TO_Y(r,g,b);
2138 padcolor[1] = RGB_TO_U(r,g,b,0);
2139 padcolor[2] = RGB_TO_V(r,g,b,0);
2140}
2141
2142static void opt_frame_pad_top(const char *arg)
2143{
2144 frame_padtop = atoi(arg);
2145 if (frame_padtop < 0) {
2146 fprintf(stderr, "Incorrect top pad size\n");
2147 exit(1);
2148 }
2149 if ((frame_padtop % 2) != 0) {
2150 fprintf(stderr, "Top pad size must be a multiple of 2\n");
2151 exit(1);
2152 }
2153}
2154
2155static void opt_frame_pad_bottom(const char *arg)
2156{
2157 frame_padbottom = atoi(arg);
2158 if (frame_padbottom < 0) {
2159 fprintf(stderr, "Incorrect bottom pad size\n");
2160 exit(1);
2161 }
2162 if ((frame_padbottom % 2) != 0) {
2163 fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
2164 exit(1);
2165 }
2166}
2167
2168
2169static void opt_frame_pad_left(const char *arg)
2170{
2171 frame_padleft = atoi(arg);
2172 if (frame_padleft < 0) {
2173 fprintf(stderr, "Incorrect left pad size\n");
2174 exit(1);
2175 }
2176 if ((frame_padleft % 2) != 0) {
2177 fprintf(stderr, "Left pad size must be a multiple of 2\n");
2178 exit(1);
2179 }
2180}
2181
2182
2183static void opt_frame_pad_right(const char *arg)
2184{
2185 frame_padright = atoi(arg);
2186 if (frame_padright < 0) {
2187 fprintf(stderr, "Incorrect right pad size\n");
2188 exit(1);
2189 }
2190 if ((frame_padright % 2) != 0) {
2191 fprintf(stderr, "Right pad size must be a multiple of 2\n");
2192 exit(1);
2193 }
2194}
2195
2196
63167088
RS
2197static void opt_frame_pix_fmt(const char *arg)
2198{
2199 frame_pix_fmt = avcodec_get_pix_fmt(arg);
2200}
2201
5fe03e38
RS
2202static void opt_frame_aspect_ratio(const char *arg)
2203{
2204 int x = 0, y = 0;
2205 double ar = 0;
2206 const char *p;
2207
2208 p = strchr(arg, ':');
2209 if (p) {
2210 x = strtol(arg, (char **)&arg, 10);
2211 if (arg == p)
2212 y = strtol(arg+1, (char **)&arg, 10);
2213 if (x > 0 && y > 0)
2214 ar = (double)x / (double)y;
2215 } else
2216 ar = strtod(arg, (char **)&arg);
2217
2218 if (!ar) {
2219 fprintf(stderr, "Incorrect aspect ratio specification.\n");
2220 exit(1);
2221 }
2222 frame_aspect_ratio = ar;
2223}
2224
b29f97d1 2225static void opt_gop_size(const char *arg)
85f07f22
FB
2226{
2227 gop_size = atoi(arg);
2228}
2229
b29f97d1 2230static void opt_b_frames(const char *arg)
bc6caae2
J
2231{
2232 b_frames = atoi(arg);
2233 if (b_frames > FF_MAX_B_FRAMES) {
2234 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
2235 exit(1);
2236 } else if (b_frames < 1) {
2237 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
2238 exit(1);
2239 }
2240}
2241
7d1c3fc1
MN
2242static void opt_mb_decision(const char *arg)
2243{
2244 mb_decision = atoi(arg);
2245}
2246
5d43635e
MN
2247static void opt_mb_cmp(const char *arg)
2248{
2249 mb_cmp = atoi(arg);
2250}
2251
622348f9
MN
2252static void opt_ildct_cmp(const char *arg)
2253{
2254 ildct_cmp = atoi(arg);
2255}
2256
5d43635e
MN
2257static void opt_sub_cmp(const char *arg)
2258{
2259 sub_cmp = atoi(arg);
2260}
2261
2262static void opt_cmp(const char *arg)
2263{
2264 cmp = atoi(arg);
2265}
2266
9c3d33d6
MN
2267static void opt_pre_cmp(const char *arg)
2268{
2269 pre_cmp = atoi(arg);
2270}
2271
2272static void opt_pre_me(const char *arg)
2273{
2274 pre_me = atoi(arg);
2275}
2276
2277static void opt_lumi_mask(const char *arg)
2278{
2279 lumi_mask = atof(arg);
2280}
2281
2282static void opt_dark_mask(const char *arg)
2283{
2284 dark_mask = atof(arg);
2285}
2286
2287static void opt_scplx_mask(const char *arg)
2288{
2289 scplx_mask = atof(arg);
2290}
2291
2292static void opt_tcplx_mask(const char *arg)
2293{
2294 tcplx_mask = atof(arg);
2295}
2296
2297static void opt_p_mask(const char *arg)
2298{
2299 p_mask = atof(arg);
2300}
2301
b29f97d1 2302static void opt_qscale(const char *arg)
85f07f22 2303{
158c7f05
MN
2304 video_qscale = atof(arg);
2305 if (video_qscale < 0.01 ||
2306 video_qscale > 255) {
2307 fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
85f07f22
FB
2308 exit(1);
2309 }
2310}
2311
4ea4b274
MN
2312static void opt_lmax(const char *arg)
2313{
2314 video_lmax = atof(arg)*FF_QP2LAMBDA;
2315}
2316
2317static void opt_lmin(const char *arg)
2318{
2319 video_lmin = atof(arg)*FF_QP2LAMBDA;
2320}
2321
b29f97d1 2322static void opt_qmin(const char *arg)
9cdd6a24
MN
2323{
2324 video_qmin = atoi(arg);
2325 if (video_qmin < 0 ||
2326 video_qmin > 31) {
2327 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
2328 exit(1);
2329 }
2330}
2331
b29f97d1 2332static void opt_qmax(const char *arg)
9cdd6a24
MN
2333{
2334 video_qmax = atoi(arg);
2335 if (video_qmax < 0 ||
2336 video_qmax > 31) {
2337 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
2338 exit(1);
2339 }
2340}
2341
b29f97d1 2342static void opt_mb_qmin(const char *arg)
17a70fde
MN
2343{
2344 video_mb_qmin = atoi(arg);
2345 if (video_mb_qmin < 0 ||
2346 video_mb_qmin > 31) {
2347 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
2348 exit(1);
2349 }
2350}
2351
b29f97d1 2352static void opt_mb_qmax(const char *arg)
17a70fde
MN
2353{
2354 video_mb_qmax = atoi(arg);
2355 if (video_mb_qmax < 0 ||
2356 video_mb_qmax > 31) {
2357 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
2358 exit(1);
2359 }
2360}
2361
b29f97d1 2362static void opt_qdiff(const char *arg)
9cdd6a24
MN
2363{
2364 video_qdiff = atoi(arg);
2365 if (video_qdiff < 0 ||
2366 video_qdiff > 31) {
2367 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
2368 exit(1);
2369 }
2370}
2371
b29f97d1 2372static void opt_qblur(const char *arg)
9cdd6a24
MN
2373{
2374 video_qblur = atof(arg);
2375}
2376
b29f97d1 2377static void opt_qcomp(const char *arg)
9cdd6a24
MN
2378{
2379 video_qcomp = atof(arg);
2380}
85f07f22 2381
b29f97d1 2382static void opt_rc_initial_cplx(const char *arg)
ac2830ec
MN
2383{
2384 video_rc_initial_cplx = atof(arg);
2385}
b29f97d1 2386static void opt_b_qfactor(const char *arg)
29700fa6
MN
2387{
2388 video_b_qfactor = atof(arg);
2389}
b29f97d1 2390static void opt_i_qfactor(const char *arg)
29700fa6
MN
2391{
2392 video_i_qfactor = atof(arg);
2393}
b29f97d1 2394static void opt_b_qoffset(const char *arg)
29700fa6
MN
2395{
2396 video_b_qoffset = atof(arg);
2397}
b29f97d1 2398static void opt_i_qoffset(const char *arg)
29700fa6
MN
2399{
2400 video_i_qoffset = atof(arg);
2401}
2402
303e50e6
MN
2403static void opt_ibias(const char *arg)
2404{
2405 video_intra_quant_bias = atoi(arg);
2406}
2407static void opt_pbias(const char *arg)
2408{
2409 video_inter_quant_bias = atoi(arg);
2410}
2411
b29f97d1 2412static void opt_packet_size(const char *arg)
1dbb6d90
MN
2413{
2414 packet_size= atoi(arg);
2415}
2416
7ebfc0ea
MN
2417static void opt_error_rate(const char *arg)
2418{
2419 error_rate= atoi(arg);
2420}
2421
b29f97d1 2422static void opt_strict(const char *arg)
f560dd82
MN
2423{
2424 strict= atoi(arg);
2425}
2426
bb198e19
MN
2427static void opt_top_field_first(const char *arg)
2428{
2429 top_field_first= atoi(arg);
2430}
2431
2432static void opt_noise_reduction(const char *arg)
2433{
2434 noise_reduction= atoi(arg);
2435}
2436
77ea0d4b
MN
2437static void opt_qns(const char *arg)
2438{
2439 qns= atoi(arg);
2440}
2441
303e50e6
MN
2442static void opt_sc_threshold(const char *arg)
2443{
2444 sc_threshold= atoi(arg);
2445}
2446
2f0472ff
TB
2447static void opt_me_range(const char *arg)
2448{
2449 me_range = atoi(arg);
2450}
2451
9c3d33d6
MN
2452static void opt_thread_count(const char *arg)
2453{
2454 thread_count= atoi(arg);
16806499 2455#if !defined(HAVE_PTHREADS) && !defined(HAVE_W32THREADS)
d8019eb5
AD
2456 if (verbose >= 0)
2457 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
842b556a 2458#endif
9c3d33d6
MN
2459}
2460
b29f97d1 2461static void opt_audio_bitrate(const char *arg)
85f07f22
FB
2462{
2463 audio_bit_rate = atoi(arg) * 1000;
2464}
2465
b29f97d1 2466static void opt_audio_rate(const char *arg)
85f07f22
FB
2467{
2468 audio_sample_rate = atoi(arg);
2469}
2470
b29f97d1 2471static void opt_audio_channels(const char *arg)
85f07f22
FB
2472{
2473 audio_channels = atoi(arg);
2474}
2475
b29f97d1 2476static void opt_video_device(const char *arg)
85f07f22 2477{
e9a9e0c2 2478 video_device = av_strdup(arg);
85f07f22
FB
2479}
2480
b29f97d1 2481static void opt_video_channel(const char *arg)
a5df11ab
FB
2482{
2483 video_channel = strtol(arg, NULL, 0);
2484}
2485
e3ee3283
AB
2486static void opt_video_standard(const char *arg)
2487{
2488 video_standard = av_strdup(arg);
2489}
2490
b29f97d1 2491static void opt_audio_device(const char *arg)
85f07f22 2492{
e9a9e0c2 2493 audio_device = av_strdup(arg);
85f07f22
FB
2494}
2495
b29f97d1 2496static void opt_dv1394(const char *arg)
8aa3ee32
MK
2497{
2498 video_grab_format = "dv1394";
1501987b 2499 audio_grab_format = NULL;
8aa3ee32
MK
2500}
2501
b29f97d1 2502static void opt_audio_codec(const char *arg)
85f07f22
FB
2503{
2504 AVCodec *p;
2505
1629626f
FB
2506 if (!strcmp(arg, "copy")) {
2507 audio_stream_copy = 1;
85f07f22 2508 } else {
1629626f
FB
2509 p = first_avcodec;
2510 while (p) {
2511 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
2512 break;
2513 p = p->next;
2514 }
2515 if (p == NULL) {
2516 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
2517 exit(1);
2518 } else {
2519 audio_codec_id = p->id;
2520 }
85f07f22
FB
2521 }
2522}
2523
b29f97d1 2524static void add_frame_hooker(const char *arg)
10d104e4
PG
2525{
2526 int argc = 0;
2527 char *argv[64];
2528 int i;
e9a9e0c2 2529 char *args = av_strdup(arg);
10d104e4 2530
bee0d9e5
CY
2531 using_vhook = 1;
2532
10d104e4
PG
2533 argv[0] = strtok(args, " ");
2534 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
2535 }
2536
2537 i = frame_hook_add(argc, argv);
2538
2539 if (i != 0) {
2540 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
2541 exit(1);
2542 }
2543}
2544
85f07f22
FB
2545const char *motion_str[] = {
2546 "zero",
2547 "full",
2548 "log",
2549 "phods",
7084c149
MN
2550 "epzs",
2551 "x1",
85f07f22
FB
2552 NULL,
2553};
2554
b29f97d1 2555static void opt_motion_estimation(const char *arg)
85f07f22
FB
2556{
2557 const char **p;
2558 p = motion_str;
2559 for(;;) {
2560 if (!*p) {
2561 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
2562 exit(1);
2563 }
2564 if (!strcmp(*p, arg))
2565 break;
2566 p++;
2567 }
101bea5f 2568 me_method = (p - motion_str) + 1;
85f07f22
FB
2569}
2570
b29f97d1 2571static void opt_video_codec(const char *arg)
85f07f22
FB
2572{
2573 AVCodec *p;
2574
1629626f
FB
2575 if (!strcmp(arg, "copy")) {
2576 video_stream_copy = 1;
85f07f22 2577 } else {
1629626f
FB
2578 p = first_avcodec;
2579 while (p) {
2580 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
2581 break;
2582 p = p->next;
2583 }
2584 if (p == NULL) {
2585 fprintf(stderr, "Unknown video codec '%s'\n", arg);
2586 exit(1);
2587 } else {
2588 video_codec_id = p->id;
2589 }
85f07f22
FB
2590 }
2591}
2592
b29f97d1 2593static void opt_map(const char *arg)
85f07f22
FB
2594{
2595 AVStreamMap *m;
2596 const char *p;
2597
2598 p = arg;
2599 m = &stream_maps[nb_stream_maps++];
2600
2601 m->file_index = strtol(arg, (char **)&p, 0);
2602 if (*p)
2603 p++;
a5dc85ef
J
2604
2605 m->stream_index = strtol(p, (char **)&p, 0);
85f07f22
FB
2606}
2607
b29f97d1 2608static void opt_recording_time(const char *arg)
85f07f22
FB
2609{
2610 recording_time = parse_date(arg, 1);
2611}
2612
8831db5c
MN
2613static void opt_start_time(const char *arg)
2614{
2615 start_time = parse_date(arg, 1);
2616}
2617
4568325a
RS
2618static void opt_rec_timestamp(const char *arg)
2619{
2620 rec_timestamp = parse_date(arg, 0) / 1000000;
2621}
2622
a6a92a9a
WG
2623static void opt_input_ts_offset(const char *arg)
2624{
2625 input_ts_offset = parse_date(arg, 1);
2626}
2627
b29f97d1 2628static void opt_input_file(const char *filename)
85f07f22
FB
2629{
2630 AVFormatContext *ic;
2631 AVFormatParameters params, *ap = &params;
14bea432 2632 int err, i, ret, rfps, rfps_base;
85f07f22 2633
b242baa4
FB
2634 if (!strcmp(filename, "-"))
2635 filename = "pipe:";
2636
d9a916e2
CY
2637 using_stdin |= !strcmp(filename, "pipe:" ) ||
2638 !strcmp( filename, "/dev/stdin" );
2639
85f07f22 2640 /* get default parameters from command line */
79fdaa4c
FB
2641 memset(ap, 0, sizeof(*ap));
2642 ap->sample_rate = audio_sample_rate;
2643 ap->channels = audio_channels;
2644 ap->frame_rate = frame_rate;
14bea432 2645 ap->frame_rate_base = frame_rate_base;
1ff93ffc
TK
2646 ap->width = frame_width + frame_padleft + frame_padright;
2647 ap->height = frame_height + frame_padtop + frame_padbottom;
817b23ff 2648 ap->image_format = image_format;
63167088 2649 ap->pix_fmt = frame_pix_fmt;
79fdaa4c
FB
2650
2651 /* open the input file with generic libav function */
2652 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
85f07f22 2653 if (err < 0) {
79fdaa4c 2654 print_error(filename, err);
85f07f22
FB
2655 exit(1);
2656 }
2657
79fdaa4c
FB
2658 /* If not enough info to get the stream parameters, we decode the
2659 first frames to get it. (used in mpeg case for example) */
2660 ret = av_find_stream_info(ic);
d8019eb5 2661 if (ret < 0 && verbose >= 0) {
85f07f22
FB
2662 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2663 exit(1);
2664 }
2665
254abc2e
FB
2666 /* if seeking requested, we execute it */
2667 if (start_time != 0) {
2668 int64_t timestamp;
2669
2670 timestamp = start_time;
2671 /* add the stream start time */
2672 if (ic->start_time != AV_NOPTS_VALUE)
2673 timestamp += ic->start_time;
2674 ret = av_seek_frame(ic, -1, timestamp);
2675 if (ret < 0) {
2676 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2677 filename, (double)timestamp / AV_TIME_BASE);
2678 }
2679 /* reset seek info */
2680 start_time = 0;
2681 }
2682
85f07f22
FB
2683 /* update the current parameters so that they match the one of the input stream */
2684 for(i=0;i<ic->nb_streams;i++) {
2685 AVCodecContext *enc = &ic->streams[i]->codec;
c62c07d3
MN
2686#if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2687 if(thread_count>1)
2688 avcodec_thread_init(enc, thread_count);
2689#endif
2690 enc->thread_count= thread_count;
85f07f22
FB
2691 switch(enc->codec_type) {
2692 case CODEC_TYPE_AUDIO:
e0d2714a 2693 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
85f07f22
FB
2694 audio_channels = enc->channels;
2695 audio_sample_rate = enc->sample_rate;
2696 break;
2697 case CODEC_TYPE_VIDEO:
2698 frame_height = enc->height;
2699 frame_width = enc->width;
5ff85f1d 2700 frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
63167088 2701 frame_pix_fmt = enc->pix_fmt;
14bea432
MN
2702 rfps = ic->streams[i]->r_frame_rate;
2703 rfps_base = ic->streams[i]->r_frame_rate_base;
fe670d09 2704 enc->workaround_bugs = workaround_bugs;
8409b8fe 2705 enc->error_resilience = error_resilience;
4d2858de 2706 enc->error_concealment = error_concealment;
0c9bbaec
WH
2707 enc->idct_algo = idct_algo;
2708 enc->debug = debug;
2709 enc->debug_mv = debug_mv;
b0368839
MN
2710 if(bitexact)
2711 enc->flags|= CODEC_FLAG_BITEXACT;
f4f3223f
MN
2712 if(me_threshold)
2713 enc->debug |= FF_DEBUG_MV;
d7425f59 2714
14bea432
MN
2715 assert(enc->frame_rate_base == rfps_base); // should be true for now
2716 if (enc->frame_rate != rfps) {
d8019eb5
AD
2717
2718 if (verbose >= 0)
2719 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2720 i, (float)enc->frame_rate / enc->frame_rate_base,
2721
14bea432 2722 (float)rfps / rfps_base);
79fdaa4c 2723 }
bf5af568 2724 /* update the current frame rate to match the stream frame rate */
14bea432
MN
2725 frame_rate = rfps;
2726 frame_rate_base = rfps_base;
bdfcbbed
MK
2727
2728 enc->rate_emu = rate_emu;
85f07f22 2729 break;
254abc2e
FB
2730 case CODEC_TYPE_DATA:
2731 break;
51bd4565 2732 default:
c04643a2 2733 av_abort();
85f07f22
FB
2734 }
2735 }
2736
2737 input_files[nb_input_files] = ic;
a6a92a9a 2738 input_files_ts_offset[nb_input_files] = input_ts_offset;
85f07f22 2739 /* dump the file content */
d8019eb5
AD
2740 if (verbose >= 0)
2741 dump_format(ic, nb_input_files, filename, 0);
2742
85f07f22 2743 nb_input_files++;
79fdaa4c
FB
2744 file_iformat = NULL;
2745 file_oformat = NULL;
817b23ff 2746 image_format = NULL;
bdfcbbed
MK
2747
2748 rate_emu = 0;
85f07f22
FB
2749}
2750
b29f97d1 2751static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
919f448d
FB
2752{
2753 int has_video, has_audio, i, j;
2754 AVFormatContext *ic;
2755
2756 has_video = 0;
2757 has_audio = 0;
2758 for(j=0;j<nb_input_files;j++) {
2759 ic = input_files[j];
2760 for(i=0;i<ic->nb_streams;i++) {
2761 AVCodecContext *enc = &ic->streams[i]->codec;
2762 switch(enc->codec_type) {
2763 case CODEC_TYPE_AUDIO:
2764 has_audio = 1;
2765 break;
2766 case CODEC_TYPE_VIDEO:
2767 has_video = 1;
2768 break;
6fb316d5
WG
2769 case CODEC_TYPE_DATA:
2770 break;
51bd4565 2771 default:
c04643a2 2772 av_abort();
919f448d
FB
2773 }
2774 }
2775 }
2776 *has_video_ptr = has_video;
2777 *has_audio_ptr = has_audio;
2778}
2779
b29f97d1 2780static void opt_output_file(const char *filename)
85f07f22
FB
2781{
2782 AVStream *st;
2783 AVFormatContext *oc;
919f448d 2784 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
85f07f22 2785 int codec_id;
817b23ff 2786 AVFormatParameters params, *ap = &params;
85f07f22
FB
2787
2788 if (!strcmp(filename, "-"))
2789 filename = "pipe:";
2790
bc874dae 2791 oc = av_alloc_format_context();
85f07f22 2792
79fdaa4c
FB
2793 if (!file_oformat) {
2794 file_oformat = guess_format(NULL, filename, NULL);
2795 if (!file_oformat) {
2796 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2797 filename);
2798 exit(1);
2799 }
85f07f22
FB
2800 }
2801
79fdaa4c 2802 oc->oformat = file_oformat;
85f07f22 2803
79fdaa4c 2804 if (!strcmp(file_oformat->name, "ffm") &&
85f07f22
FB
2805 strstart(filename, "http:", NULL)) {
2806 /* special case for files sent to ffserver: we get the stream
2807 parameters from ffserver */
2808 if (read_ffserver_streams(oc, filename) < 0) {
2809 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2810 exit(1);
2811 }
2812 } else {
8857a7ef
MN
2813 use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy;
2814 use_audio = file_oformat->audio_codec != CODEC_ID_NONE || audio_stream_copy;
919f448d 2815
e30a2846
FB
2816 /* disable if no corresponding type found and at least one
2817 input file */
2818 if (nb_input_files > 0) {
2819 check_audio_video_inputs(&input_has_video, &input_has_audio);
2820 if (!input_has_video)
2821 use_video = 0;
2822 if (!input_has_audio)
2823 use_audio = 0;
2824 }
919f448d
FB
2825
2826 /* manual disable */
85f07f22
FB
2827 if (audio_disable) {
2828 use_audio = 0;
2829 }
2830 if (video_disable) {
2831 use_video = 0;
2832 }
2833
2834 nb_streams = 0;
2835 if (use_video) {
2836 AVCodecContext *video_enc;
2837
9ee91c2f 2838 st = av_new_stream(oc, nb_streams++);
85f07f22
FB
2839 if (!st) {
2840 fprintf(stderr, "Could not alloc stream\n");
2841 exit(1);
2842 }
16806499 2843#if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
9c3d33d6 2844 if(thread_count>1)
16806499 2845 avcodec_thread_init(&st->codec, thread_count);
9c3d33d6 2846#endif
85f07f22 2847
1629626f 2848 video_enc = &st->codec;
6e6d6dc0
MN
2849
2850 if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp"))
2851 video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
1629626f
FB
2852 if (video_stream_copy) {
2853 st->stream_copy = 1;
2854 video_enc->codec_type = CODEC_TYPE_VIDEO;
2855 } else {
ac2830ec
MN
2856 char *p;
2857 int i;
fcee0164 2858 AVCodec *codec;
ac2830ec 2859
1629626f
FB
2860 codec_id = file_oformat->video_codec;
2861 if (video_codec_id != CODEC_ID_NONE)
2862 codec_id = video_codec_id;
2863
2864 video_enc->codec_id = codec_id;
22f7a060 2865 codec = avcodec_find_encoder(codec_id);
1629626f
FB
2866
2867 video_enc->bit_rate = video_bit_rate;
2868 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2869 video_enc->frame_rate = frame_rate;
14bea432 2870 video_enc->frame_rate_base = frame_rate_base;
22f7a060
MN
2871 if(codec && codec->supported_framerates){
2872 const AVRational *p= codec->supported_framerates;
2873 AVRational req= (AVRational){frame_rate, frame_rate_base};
2874 const AVRational *best=NULL;
2875 AVRational best_error= (AVRational){INT_MAX, 1};
2876 for(; p->den!=0; p++){
2877 AVRational error= av_sub_q(req, *p);
2878 if(error.num <0) error.num *= -1;
2879 if(av_cmp_q(error, best_error) < 0){
2880 best_error= error;
2881 best= p;
2882 }
2883 }
2884 video_enc->frame_rate = best->num;
2885 video_enc->frame_rate_base= best->den;
2886 }
1629626f 2887
1ff93ffc
TK
2888 video_enc->width = frame_width + frame_padright + frame_padleft;
2889 video_enc->height = frame_height + frame_padtop + frame_padbottom;
1c0dcc39 2890 video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
fcee0164
MN
2891 video_enc->pix_fmt = frame_pix_fmt;
2892
fcee0164
MN
2893 if(codec && codec->pix_fmts){
2894 const enum PixelFormat *p= codec->pix_fmts;
2895 for(; *p!=-1; p++){
2896 if(*p == video_enc->pix_fmt)
2897 break;
2898 }
2899 if(*p == -1)
2900 video_enc->pix_fmt = codec->pix_fmts[0];
2901 }
1629626f
FB
2902
2903 if (!intra_only)
2904 video_enc->gop_size = gop_size;
2905 else
2906 video_enc->gop_size = 0;
2907 if (video_qscale || same_quality) {
2908 video_enc->flags |= CODEC_FLAG_QSCALE;
158c7f05 2909 st->quality = FF_QP2LAMBDA * video_qscale;
1629626f 2910 }
84f608f4
VM
2911
2912 if(intra_matrix)
2913 video_enc->intra_matrix = intra_matrix;
2914 if(inter_matrix)
2915 video_enc->inter_matrix = inter_matrix;
2916
b0368839
MN
2917 if(bitexact)
2918 video_enc->flags |= CODEC_FLAG_BITEXACT;
2919
7d1c3fc1 2920 video_enc->mb_decision = mb_decision;
5d43635e 2921 video_enc->mb_cmp = mb_cmp;
622348f9 2922 video_enc->ildct_cmp = ildct_cmp;
5d43635e
MN
2923 video_enc->me_sub_cmp = sub_cmp;
2924 video_enc->me_cmp = cmp;
9c3d33d6
MN
2925 video_enc->me_pre_cmp = pre_cmp;
2926 video_enc->pre_me = pre_me;
2927 video_enc->lumi_masking = lumi_mask;
2928 video_enc->dark_masking = dark_mask;
2929 video_enc->spatial_cplx_masking = scplx_mask;
2930 video_enc->temporal_cplx_masking = tcplx_mask;
2931 video_enc->p_masking = p_mask;
77ea0d4b 2932 video_enc->quantizer_noise_shaping= qns;
7d1c3fc1 2933
21e59552
MN
2934 if (use_umv) {
2935 video_enc->flags |= CODEC_FLAG_H263P_UMV;
2936 }
458eadda
MN
2937 if (use_ss) {
2938 video_enc->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
2939 }
21e59552
MN
2940 if (use_aic) {
2941 video_enc->flags |= CODEC_FLAG_H263P_AIC;
2942 }
dba019da
MN
2943 if (use_aiv) {
2944 video_enc->flags |= CODEC_FLAG_H263P_AIV;
2945 }
1629626f 2946 if (use_4mv) {
1629626f
FB
2947 video_enc->flags |= CODEC_FLAG_4MV;
2948 }
8e2162f0
MN
2949 if (use_obmc) {
2950 video_enc->flags |= CODEC_FLAG_OBMC;
2951 }
d7646d7d
MN
2952 if (use_loop) {
2953 video_enc->flags |= CODEC_FLAG_LOOP_FILTER;
2954 }
8e2162f0 2955
e56d417b 2956 if(use_part) {
1629626f 2957 video_enc->flags |= CODEC_FLAG_PART;
e56d417b 2958 }
bb198e19
MN
2959 if (use_alt_scan) {
2960 video_enc->flags |= CODEC_FLAG_ALT_SCAN;
2961 }
c0baa56a
MN
2962 if (use_trell) {
2963 video_enc->flags |= CODEC_FLAG_TRELLIS_QUANT;
2964 }
baaf3f46
MN
2965 if (use_scan_offset) {
2966 video_enc->flags |= CODEC_FLAG_SVCD_SCAN_OFFSET;
2967 }
303e50e6
MN
2968 if (closed_gop) {
2969 video_enc->flags |= CODEC_FLAG_CLOSED_GOP;
2970 }
5efe481a
MN
2971 if (use_qpel) {
2972 video_enc->flags |= CODEC_FLAG_QPEL;
2973 }
f7e1e69e
MN
2974 if (use_qprd) {
2975 video_enc->flags |= CODEC_FLAG_QP_RD;
2976 }
1a11cbcc
MN
2977 if (use_cbprd) {
2978 video_enc->flags |= CODEC_FLAG_CBP_RD;
2979 }
1629626f 2980 if (b_frames) {
1629626f
FB
2981 video_enc->max_b_frames = b_frames;
2982 video_enc->b_frame_strategy = 0;
2983 video_enc->b_quant_factor = 2.0;
bc6caae2 2984 }
bb198e19 2985 if (do_interlace_dct) {
e56d417b
FB
2986 video_enc->flags |= CODEC_FLAG_INTERLACED_DCT;
2987 }
bb198e19
MN
2988 if (do_interlace_me) {
2989 video_enc->flags |= CODEC_FLAG_INTERLACED_ME;
2990 }
1629626f
FB
2991 video_enc->qmin = video_qmin;
2992 video_enc->qmax = video_qmax;
4ea4b274
MN
2993 video_enc->lmin = video_lmin;
2994 video_enc->lmax = video_lmax;
17a70fde
MN
2995 video_enc->mb_qmin = video_mb_qmin;
2996 video_enc->mb_qmax = video_mb_qmax;
1629626f
FB
2997 video_enc->max_qdiff = video_qdiff;
2998 video_enc->qblur = video_qblur;
2999 video_enc->qcompress = video_qcomp;
3000 video_enc->rc_eq = video_rc_eq;
0c9bbaec 3001 video_enc->debug = debug;
9c3d33d6
MN
3002 video_enc->debug_mv = debug_mv;
3003 video_enc->thread_count = thread_count;
ac2830ec
MN
3004 p= video_rc_override_string;
3005 for(i=0; p; i++){
3006 int start, end, q;
3007 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
3008 if(e!=3){
3009 fprintf(stderr, "error parsing rc_override\n");
3010 exit(1);
3011 }
3012 video_enc->rc_override=
47e2a6e6
FB
3013 av_realloc(video_enc->rc_override,
3014 sizeof(RcOverride)*(i+1));
ac2830ec
MN
3015 video_enc->rc_override[i].start_frame= start;
3016 video_enc->rc_override[i].end_frame = end;
3017 if(q>0){
3018 video_enc->rc_override[i].qscale= q;
3019 video_enc->rc_override[i].quality_factor= 1.0;
3020 }
3021 else{
3022 video_enc->rc_override[i].qscale= 0;
3023 video_enc->rc_override[i].quality_factor= -q/100.0;
3024 }
3025 p= strchr(p, '/');
3026 if(p) p++;
3027 }
3028 video_enc->rc_override_count=i;
3029
1629626f
FB
3030 video_enc->rc_max_rate = video_rc_max_rate;
3031 video_enc->rc_min_rate = video_rc_min_rate;
3032 video_enc->rc_buffer_size = video_rc_buffer_size;
3033 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
ac2830ec 3034 video_enc->rc_initial_cplx= video_rc_initial_cplx;
1629626f
FB
3035 video_enc->i_quant_factor = video_i_qfactor;
3036 video_enc->b_quant_factor = video_b_qfactor;
3037 video_enc->i_quant_offset = video_i_qoffset;
3038 video_enc->b_quant_offset = video_b_qoffset;
303e50e6
MN
3039 video_enc->intra_quant_bias = video_intra_quant_bias;
3040 video_enc->inter_quant_bias = video_inter_quant_bias;
1629626f
FB
3041 video_enc->dct_algo = dct_algo;
3042 video_enc->idct_algo = idct_algo;
f4f3223f 3043 video_enc->me_threshold= me_threshold;
f20f8a8b 3044 video_enc->mb_threshold= mb_threshold;
1a11cbcc 3045 video_enc->intra_dc_precision= intra_dc_precision - 8;
f560dd82 3046 video_enc->strict_std_compliance = strict;
7ebfc0ea 3047 video_enc->error_rate = error_rate;
bb198e19 3048 video_enc->noise_reduction= noise_reduction;
303e50e6 3049 video_enc->scenechange_threshold= sc_threshold;
2f0472ff 3050 video_enc->me_range = me_range;
8a0c66a7
MN
3051 video_enc->coder_type= coder;
3052 video_enc->context_model= context;
3053 video_enc->prediction_method= predictor;
2f0472ff 3054
1629626f
FB
3055 if(packet_size){
3056 video_enc->rtp_mode= 1;
3057 video_enc->rtp_payload_size= packet_size;
3058 }
9cdd6a24 3059
1629626f 3060 if (do_psnr)
140cb663 3061 video_enc->flags|= CODEC_FLAG_PSNR;
e4986da9 3062
1629626f 3063 video_enc->me_method = me_method;
5abdb4b1 3064
1629626f
FB
3065 /* two pass mode */
3066 if (do_pass) {
3067 if (do_pass == 1) {
3068 video_enc->flags |= CODEC_FLAG_PASS1;
3069 } else {
3070 video_enc->flags |= CODEC_FLAG_PASS2;
3071 }
5abdb4b1 3072 }
cfcf0ffd 3073 }
85f07f22
FB
3074 }
3075
3076 if (use_audio) {
3077 AVCodecContext *audio_enc;
3078
9ee91c2f 3079 st = av_new_stream(oc, nb_streams++);
85f07f22
FB
3080 if (!st) {
3081 fprintf(stderr, "Could not alloc stream\n");
3082 exit(1);
3083 }
16806499 3084#if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
9c3d33d6 3085 if(thread_count>1)
16806499 3086 avcodec_thread_init(&st->codec, thread_count);
9c3d33d6 3087#endif
1629626f 3088
85f07f22 3089 audio_enc = &st->codec;
85f07f22 3090 audio_enc->codec_type = CODEC_TYPE_AUDIO;
7f96ed5b
MN
3091
3092 if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp"))
3093 audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
1629626f
FB
3094 if (audio_stream_copy) {
3095 st->stream_copy = 1;
cfcbbe48 3096 audio_enc->channels = audio_channels;
1629626f
FB
3097 } else {
3098 codec_id = file_oformat->audio_codec;
3099 if (audio_codec_id != CODEC_ID_NONE)
3100 codec_id = audio_codec_id;
3101 audio_enc->codec_id = codec_id;
3102
3103 audio_enc->bit_rate = audio_bit_rate;
ae2e7208 3104 audio_enc->strict_std_compliance = strict;
9c3d33d6 3105 audio_enc->thread_count = thread_count;
1629626f
FB
3106 /* For audio codecs other than AC3 we limit */
3107 /* the number of coded channels to stereo */
3108 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
3109 audio_enc->channels = 2;
3110 } else
3111 audio_enc->channels = audio_channels;
3112 }
cfcbbe48 3113 audio_enc->sample_rate = audio_sample_rate;
85f07f22
FB
3114 }
3115
3116 oc->nb_streams = nb_streams;
3117
3118 if (!nb_streams) {
919f448d 3119 fprintf(stderr, "No audio or video streams available\n");
85f07f22
FB
3120 exit(1);
3121 }
3122
4568325a
RS
3123 oc->timestamp = rec_timestamp;
3124
3125 if (str_title)
79fdaa4c 3126 pstrcpy(oc->title, sizeof(oc->title), str_title);
85f07f22 3127 if (str_author)
79fdaa4c 3128 pstrcpy(oc->author, sizeof(oc->author), str_author);
85f07f22 3129 if (str_copyright)
79fdaa4c 3130 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
85f07f22 3131 if (str_comment)
79fdaa4c 3132 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
85f07f22
FB
3133 }
3134
1629626f 3135 output_files[nb_output_files++] = oc;
85f07f22
FB
3136
3137 strcpy(oc->filename, filename);
919f448d
FB
3138
3139 /* check filename in case of an image number is expected */
79fdaa4c
FB
3140 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
3141 if (filename_number_test(oc->filename) < 0) {
3142 print_error(oc->filename, AVERROR_NUMEXPECTED);
919f448d 3143 exit(1);
79fdaa4c 3144 }
919f448d
FB
3145 }
3146
79fdaa4c 3147 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
85f07f22
FB
3148 /* test if it already exists to avoid loosing precious files */
3149 if (!file_overwrite &&
3150 (strchr(filename, ':') == NULL ||
3151 strstart(filename, "file:", NULL))) {
3152 if (url_exist(filename)) {
3153 int c;
3154
d9a916e2
CY
3155 if ( !using_stdin ) {
3156 fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
3157 fflush(stderr);
3158 c = getchar();
3159 if (toupper(c) != 'Y') {
3160 fprintf(stderr, "Not overwriting - exiting\n");
3161 exit(1);
3162 }
3163 }
3164 else {
3165 fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
85f07f22 3166 exit(1);
d9a916e2 3167 }
85f07f22
FB
3168 }
3169 }
3170
3171 /* open the file */
3172 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
3173 fprintf(stderr, "Could not open '%s'\n", filename);
3174 exit(1);
3175 }
3176 }
3177
817b23ff
FB
3178 memset(ap, 0, sizeof(*ap));
3179 ap->image_format = image_format;
3180 if (av_set_parameters(oc, ap) < 0) {
3181 fprintf(stderr, "%s: Invalid encoding parameters\n",
3182 oc->filename);
3183 exit(1);
3184 }
3185
85f07f22 3186 /* reset some options */
79fdaa4c
FB
3187 file_oformat = NULL;
3188 file_iformat = NULL;
817b23ff 3189 image_format = NULL;
85f07f22
FB
3190 audio_disable = 0;
3191 video_disable = 0;
3192 audio_codec_id = CODEC_ID_NONE;
3193 video_codec_id = CODEC_ID_NONE;
1629626f
FB
3194 audio_stream_copy = 0;
3195 video_stream_copy = 0;
85f07f22
FB
3196}
3197
a38469e1 3198/* prepare dummy protocols for grab */
b29f97d1 3199static void prepare_grab(void)
a38469e1
FB
3200{
3201 int has_video, has_audio, i, j;
3202 AVFormatContext *oc;
3203 AVFormatContext *ic;
79a7c268 3204 AVFormatParameters vp1, *vp = &vp1;
a38469e1 3205 AVFormatParameters ap1, *ap = &ap1;
79a7c268 3206
a38469e1
FB
3207 /* see if audio/video inputs are needed */
3208 has_video = 0;
3209 has_audio = 0;
3210 memset(ap, 0, sizeof(*ap));
79a7c268 3211 memset(vp, 0, sizeof(*vp));
a38469e1
FB
3212 for(j=0;j<nb_output_files;j++) {
3213 oc = output_files[j];
3214 for(i=0;i<oc->nb_streams;i++) {
3215 AVCodecContext *enc = &oc->streams[i]->codec;
3216 switch(enc->codec_type) {
3217 case CODEC_TYPE_AUDIO:
3218 if (enc->sample_rate > ap->sample_rate)
3219 ap->sample_rate = enc->sample_rate;
3220 if (enc->channels > ap->channels)
3221 ap->channels = enc->channels;
3222 has_audio = 1;
3223 break;
3224 case CODEC_TYPE_VIDEO:
79a7c268
FB
3225 if (enc->width > vp->width)
3226 vp->width = enc->width;
3227 if (enc->height > vp->height)
3228 vp->height = enc->height;
14bea432
MN
3229
3230 assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
3231 if (enc->frame_rate > vp->frame_rate){
3232 vp->frame_rate = enc->frame_rate;
3233 vp->frame_rate_base = enc->frame_rate_base;
3234 }
a38469e1
FB
3235 has_video = 1;
3236 break;
51bd4565 3237 default:
c04643a2 3238 av_abort();
a38469e1
FB
3239 }
3240 }
3241 }
3242
3243 if (has_video == 0 && has_audio == 0) {
3244 fprintf(stderr, "Output file must have at least one audio or video stream\n");
3245 exit(1);
3246 }
3247
3248 if (has_video) {
79fdaa4c 3249 AVInputFormat *fmt1;
8aa3ee32 3250 fmt1 = av_find_input_format(video_grab_format);
a5df11ab
FB
3251 vp->device = video_device;
3252 vp->channel = video_channel;
e3ee3283 3253 vp->standard = video_standard;
79a7c268 3254 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
bf5af568 3255 fprintf(stderr, "Could not find video grab device\n");
a38469e1
FB
3256 exit(1);
3257 }
ddaae6a9
RS
3258 /* If not enough info to get the stream parameters, we decode the
3259 first frames to get it. */
3260 if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && av_find_stream_info(ic) < 0) {
3261 fprintf(stderr, "Could not find video grab parameters\n");
3262 exit(1);
3263 }
79fdaa4c 3264 /* by now video grab has one stream */
14bea432
MN
3265 ic->streams[0]->r_frame_rate = vp->frame_rate;
3266 ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
a38469e1 3267 input_files[nb_input_files] = ic;
d8019eb5
AD
3268
3269 if (verbose >= 0)
3270 dump_format(ic, nb_input_files, "", 0);
3271
a38469e1
FB
3272 nb_input_files++;
3273 }
1501987b 3274 if (has_audio && audio_grab_format) {
79fdaa4c 3275 AVInputFormat *fmt1;
8aa3ee32 3276 fmt1 = av_find_input_format(audio_grab_format);
79a7c268 3277 ap->device = audio_device;
79fdaa4c 3278 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
bf5af568 3279 fprintf(stderr, "Could not find audio grab device\n");
a38469e1
FB
3280 exit(1);
3281 }
3282 input_files[nb_input_files] = ic;
d8019eb5
AD
3283
3284 if (verbose >= 0)
3285 dump_format(ic, nb_input_files, "", 0);
3286
a38469e1
FB
3287 nb_input_files++;
3288 }
3289}
3290
5abdb4b1 3291/* same option as mencoder */
b29f97d1 3292static void opt_pass(const char *pass_str)
5abdb4b1
FB
3293{
3294 int pass;
3295 pass = atoi(pass_str);
3296 if (pass != 1 && pass != 2) {
3297 fprintf(stderr, "pass number can be only 1 or 2\n");
3298 exit(1);
3299 }
3300 do_pass = pass;
3301}
a38469e1 3302
f3ec2d46 3303#if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
0c1a9eda 3304static int64_t getutime(void)
5727b222 3305{
f3ec2d46 3306 return av_gettime();
5727b222 3307}
bdc4796f 3308#else
0c1a9eda 3309static int64_t getutime(void)
bdc4796f 3310{
f3ec2d46
SG
3311 struct rusage rusage;
3312
3313 getrusage(RUSAGE_SELF, &rusage);
3314 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
bdc4796f
FB
3315}
3316#endif
5727b222 3317
79fdaa4c
FB
3318extern int ffm_nopts;
3319
b29f97d1 3320static void opt_bitexact(void)
79fdaa4c 3321{
b0368839 3322 bitexact=1;
79fdaa4c
FB
3323 /* disable generate of real time pts in ffm (need to be supressed anyway) */
3324 ffm_nopts = 1;
3325}
3326
b29f97d1 3327static void show_formats(void)
85f07f22 3328{
79fdaa4c
FB
3329 AVInputFormat *ifmt;
3330 AVOutputFormat *ofmt;
817b23ff 3331 AVImageFormat *image_fmt;
85f07f22 3332 URLProtocol *up;
3738fe1a
MN
3333 AVCodec *p, *p2;
3334 const char **pp, *last_name;
3335
3336 printf("File formats:\n");
3337 last_name= "000";
3338 for(;;){
3339 int decode=0;
3340 int encode=0;
3341 const char *name=NULL;
3342
3343 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
3344 if((name == NULL || strcmp(ofmt->name, name)<0) &&
3345 strcmp(ofmt->name, last_name)>0){
3346 name= ofmt->name;
3347 encode=1;
3348 }
3349 }
3350 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
3351 if((name == NULL || strcmp(ifmt->name, name)<0) &&
3352 strcmp(ifmt->name, last_name)>0){
3353 name= ifmt->name;
3354 encode=0;
3355 }
3356 if(name && strcmp(ifmt->name, name)==0)
3357 decode=1;
3358 }
3359 if(name==NULL)
3360 break;
3361 last_name= name;
3362
3363 printf(
3364 " %s%s %s\n",
3365 decode ? "D":" ",
3366 encode ? "E":" ",
3367 name);
817b23ff
FB
3368 }
3369 printf("\n");
3370
3738fe1a 3371 printf("Image formats:\n");
817b23ff
FB
3372 for(image_fmt = first_image_format; image_fmt != NULL;
3373 image_fmt = image_fmt->next) {
3738fe1a
MN
3374 printf(
3375 " %s%s %s\n",
3376 image_fmt->img_read ? "D":" ",
3377 image_fmt->img_write ? "E":" ",
3378 image_fmt->name);
817b23ff
FB
3379 }
3380 printf("\n");
3381
85f07f22 3382 printf("Codecs:\n");
3738fe1a
MN
3383 last_name= "000";
3384 for(;;){
3385 int decode=0;
3386 int encode=0;
3387 int cap=0;
3388
3389 p2=NULL;
3390 for(p = first_avcodec; p != NULL; p = p->next) {
3391 if((p2==NULL || strcmp(p->name, p2->name)<0) &&
3392 strcmp(p->name, last_name)>0){
3393 p2= p;
3394 decode= encode= cap=0;
3395 }
3396 if(p2 && strcmp(p->name, p2->name)==0){
3397 if(p->decode) decode=1;
3398 if(p->encode) encode=1;
3399 cap |= p->capabilities;
3400 }
3401 }
3402 if(p2==NULL)
3403 break;
3404 last_name= p2->name;
3405
3406 printf(
3407 " %s%s%s%s%s%s %s",
3408 decode ? "D": (/*p2->decoder ? "d":*/" "),
3409 encode ? "E":" ",
3410 p2->type == CODEC_TYPE_AUDIO ? "A":"V",
3411 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
3412 cap & CODEC_CAP_DR1 ? "D":" ",
3413 cap & CODEC_CAP_TRUNCATED ? "T":" ",
3414 p2->name);
3415 /* if(p2->decoder && decode==0)
3416 printf(" use %s for decoding", p2->decoder->name);*/
3417 printf("\n");
85f07f22
FB
3418 }
3419 printf("\n");
3420
3738fe1a 3421 printf("Supported file protocols:\n");
85f07f22
FB
3422 for(up = first_protocol; up != NULL; up = up->next)
3423 printf(" %s:", up->name);
3424 printf("\n");
3425
3738fe1a
MN
3426 printf("Frame size, frame rate abbreviations:\n ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
3427 printf("Motion estimation methods:\n");
85f07f22
FB
3428 pp = motion_str;
3429 while (*pp) {
3430 printf(" %s", *pp);
101bea5f 3431 if ((pp - motion_str + 1) == ME_ZERO)
85f07f22 3432 printf("(fastest)");
101bea5f 3433 else if ((pp - motion_str + 1) == ME_FULL)
85f07f22 3434 printf("(slowest)");
101bea5f 3435 else if ((pp - motion_str + 1) == ME_EPZS)
85f07f22
FB
3436 printf("(default)");
3437 pp++;
3438 }
3738fe1a
MN
3439 printf("\n\n");
3440 printf(
3441"Note, the names of encoders and decoders dont always match, so there are\n"
3442"several cases where the above table shows encoder only or decoder only entries\n"
3443"even though both encoding and decoding are supported for example, the h263\n"
3444"decoder corresponds to the h263 and h263p encoders, for file formats its even\n"
3445"worse\n");
85f07f22
FB
3446 exit(1);
3447}
3448
84f608f4
VM
3449void parse_matrix_coeffs(uint16_t *dest, const char *str)
3450{
3451 int i;
3452 const char *p = str;
3453 for(i = 0;; i++) {
3454 dest[i] = atoi(p);
3455 if(i == 63)
3456 break;
3457 p = strchr(p, ',');
3458 if(!p) {
3459 fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
3460 exit(1);
3461 }
3462 p++;
3463 }
3464}
3465
3466void opt_inter_matrix(const char *arg)
3467{
3468 inter_matrix = av_mallocz(sizeof(uint16_t) * 64);
3469 parse_matrix_coeffs(inter_matrix, arg);
3470}
3471
3472void opt_intra_matrix(const char *arg)
3473{
3474 intra_matrix = av_mallocz(sizeof(uint16_t) * 64);
3475 parse_matrix_coeffs(intra_matrix, arg);
3476}
3477
e0595741
VM
3478static void opt_target(const char *arg)
3479{
3480 int norm = -1;
3481
3482 if(!strncmp(arg, "pal-", 4)) {
3483 norm = 0;
3484 arg += 4;
3485 } else if(!strncmp(arg, "ntsc-", 5)) {
3486 norm = 1;
3487 arg += 5;
3488 } else {
3489 int fr;
3490 /* Calculate FR via float to avoid int overflow */
3491 fr = (int)(frame_rate * 1000.0 / frame_rate_base);
3492 if(fr == 25000) {
3493 norm = 0;
3494 } else if((fr == 29970) || (fr == 23976)) {
3495 norm = 1;
3496 } else {
3497 /* Try to determine PAL/NTSC by peeking in the input files */
3498 if(nb_input_files) {
3499 int i, j;
3500 for(j = 0; j < nb_input_files; j++) {
3501 for(i = 0; i < input_files[j]->nb_streams; i++) {
3502 AVCodecContext *c = &input_files[j]->streams[i]->codec;
3503 if(c->codec_type != CODEC_TYPE_VIDEO)
3504 continue;
3505 fr = c->frame_rate * 1000 / c->frame_rate_base;
3506 if(fr == 25000) {
3507 norm = 0;
3508 break;
3509 } else if((fr == 29970) || (fr == 23976)) {
3510 norm = 1;
3511 break;
3512 }
3513 }
3514 if(norm >= 0)
3515 break;
3516 }
3517 }
3518 }
3519 if(verbose && norm >= 0)
3520 printf("Assuming %s for target.\n", norm ? "NTSC" : "PAL");
3521 }
3522
3523 if(norm < 0) {
3524 fprintf(stderr, "Could not determine norm (PAL/NTSC) for target.\n");
3525 fprintf(stderr, "Please prefix target with \"pal-\" or \"ntsc-\",\n");
3526 fprintf(stderr, "or set a framerate with \"-r xxx\".\n");
3527 exit(1);
3528 }
3529
3530 if(!strcmp(arg, "vcd")) {
3531
3532 opt_video_codec("mpeg1video");
3533 opt_audio_codec("mp2");
3534 opt_format("vcd");
3535
3536 opt_frame_size(norm ? "352x240" : "352x288");
3537
3538 video_bit_rate = 1150000;
3539 video_rc_max_rate = 1150000;
3540 video_rc_min_rate = 1150000;
fb066639 3541 video_rc_buffer_size = 40*1024*8;
e0595741
VM
3542
3543 audio_bit_rate = 224000;
3544 audio_sample_rate = 44100;
3545
3546 } else if(!strcmp(arg, "svcd")) {
3547
3548 opt_video_codec("mpeg2video");
3549 opt_audio_codec("mp2");
24515926 3550 opt_format("svcd");
e0595741
VM
3551
3552 opt_frame_size(norm ? "480x480" : "480x576");
3553 opt_gop_size(norm ? "18" : "15");
3554
3555 video_bit_rate = 2040000;
3556 video_rc_max_rate = 2516000;
55bbad6f 3557 video_rc_min_rate = 0; //1145000;
fb066639 3558 video_rc_buffer_size = 224*1024*8;
baaf3f46 3559 use_scan_offset = 1;
e0595741
VM
3560
3561 audio_bit_rate = 224000;
3562 audio_sample_rate = 44100;
3563
3564 } else if(!strcmp(arg, "dvd")) {
3565
3566 opt_video_codec("mpeg2video");
3567 opt_audio_codec("ac3");
3568 opt_format("vob");
3569
3570 opt_frame_size(norm ? "720x480" : "720x576");
3571 opt_gop_size(norm ? "18" : "15");
3572
3573 video_bit_rate = 6000000;
3574 video_rc_max_rate = 9000000;
55bbad6f 3575 video_rc_min_rate = 0; //1500000;
fb066639 3576 video_rc_buffer_size = 224*1024*8;
e0595741
VM
3577
3578 audio_bit_rate = 448000;
3579 audio_sample_rate = 48000;
3580
3581 } else {
3582 fprintf(stderr, "Unknown target: %s\n", arg);
3583 exit(1);
3584 }
3585}
3586
242f5b0a
MN
3587static void show_version(void)
3588{
3589 printf("ffmpeg " FFMPEG_VERSION "\n"
3590 "libavcodec %d\n"
3591 "libavformat %d\n",
3592 avcodec_build(), LIBAVFORMAT_BUILD);
3593 exit(1);
3594}
3595
85f07f22 3596const OptionDef options[] = {
02d504a7
FB
3597 /* main options */
3598 { "L", 0, {(void*)show_license}, "show license" },
bdc4796f 3599 { "h", 0, {(void*)show_help}, "show help" },
242f5b0a 3600 { "version", 0, {(void*)show_version}, "show version" },
bdc4796f
FB
3601 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
3602 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
817b23ff 3603 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
bdc4796f
FB
3604 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
3605 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
3606 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
3607 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
254abc2e 3608 { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" },
a6a92a9a 3609 { "itsoffset", HAS_ARG, {(void*)opt_input_ts_offset}, "set the input ts offset", "time_off" },
bdc4796f 3610 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
4568325a 3611 { "timestamp", HAS_ARG, {(void*)&opt_rec_timestamp}, "set the timestamp", "time" },
bdc4796f
FB
3612 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
3613 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
3614 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
1d366fce 3615 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },