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