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