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