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