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