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