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