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