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