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