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