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