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