3 * Copyright (c) 2000,2001 Gerard Lantau
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.
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.
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.
19 #define HAVE_AV_CONFIG_H
25 #include <sys/ioctl.h>
29 #include <sys/resource.h>
34 #define MAXINT64 INT64_C(0x7fffffffffffffff)
39 #define HAS_ARG 0x0001
40 #define OPT_BOOL 0x0002
41 #define OPT_EXPERT 0x0004
42 #define OPT_STRING 0x0008
52 /* select an input stream for an output stream */
53 typedef struct AVStreamMap {
58 extern const OptionDef options[];
64 static AVFormatContext *input_files[MAX_FILES];
65 static int nb_input_files = 0;
67 static AVFormatContext *output_files[MAX_FILES];
68 static int nb_output_files = 0;
70 static AVStreamMap stream_maps[MAX_FILES];
71 static int nb_stream_maps;
73 static AVFormat *file_format;
74 static int frame_width = 160;
75 static int frame_height = 128;
76 static int frame_rate = 25 * FRAME_RATE_BASE;
77 static int video_bit_rate = 200000;
78 static int video_bit_rate_tolerance = 200000;
79 static int video_qscale = 0;
80 static int video_qmin = 3;
81 static int video_qmax = 15;
82 static int video_qdiff = 3;
83 static float video_qblur = 0.5;
84 static float video_qcomp = 0.5;
85 static int video_disable = 0;
86 static int video_codec_id = CODEC_ID_NONE;
87 static int same_quality = 0;
88 static int use_4mv = 0;
89 static int do_deinterlace = 0;
91 static int gop_size = 12;
92 static int intra_only = 0;
93 static int audio_sample_rate = 44100;
94 static int audio_bit_rate = 64000;
95 static int audio_disable = 0;
96 static int audio_channels = 1;
97 static int audio_codec_id = CODEC_ID_NONE;
99 static INT64 recording_time = 0;
100 static int file_overwrite = 0;
101 static char *str_title = NULL;
102 static char *str_author = NULL;
103 static char *str_copyright = NULL;
104 static char *str_comment = NULL;
105 static int do_benchmark = 0;
106 static int do_hex_dump = 0;
107 static int do_play = 0;
108 static int do_psnr = 0;
109 static int do_vstats = 0;
111 typedef struct AVOutputStream {
112 int file_index; /* file index */
113 int index; /* stream index in the output file */
114 int source_index; /* AVInputStream index */
115 AVStream *st; /* stream in the output file */
116 int encoding_needed; /* true if encoding needed for this stream */
118 int fifo_packet_rptr; /* read index in the corresponding
119 avinputstream packet fifo */
121 AVPicture pict_tmp; /* temporary image for resizing */
123 ImgReSampleContext *img_resample_ctx; /* for image resampling */
127 ReSampleContext *resample; /* for audio resampling */
128 FifoBuffer fifo; /* for compression: one audio fifo per codec */
131 typedef struct AVInputStream {
135 int discard; /* true if stream data should be discarded */
136 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
137 INT64 pts; /* current pts */
138 int frame_number; /* current frame */
139 INT64 sample_index; /* current sample */
142 typedef struct AVInputFile {
143 int eof_reached; /* true if eof reached */
144 int ist_index; /* index of first stream in ist_table */
145 int buffer_size; /* current total buffer size */
146 int buffer_size_max; /* buffer size at which we consider we can stop
152 /* init terminal so that we can grab keys */
153 static struct termios oldtty;
155 static void term_exit(void)
157 tcsetattr (0, TCSANOW, &oldtty);
160 static void term_init(void)
167 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
168 |INLCR|IGNCR|ICRNL|IXON);
169 tty.c_oflag |= OPOST;
170 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
171 tty.c_cflag &= ~(CSIZE|PARENB);
176 tcsetattr (0, TCSANOW, &tty);
181 /* read a key without blocking */
182 static int read_key(void)
193 n = select(1, &rfds, NULL, NULL, &tv);
195 if (read(0, &ch, 1) == 1)
203 /* no interactive support */
204 static void term_exit(void)
208 static void term_init(void)
212 static int read_key(void)
219 int read_ffserver_streams(AVFormatContext *s, const char *filename)
224 ic = av_open_input_file(filename, NULL, FFM_PACKET_SIZE, NULL);
227 /* copy stream format */
228 s->nb_streams = ic->nb_streams;
229 for(i=0;i<ic->nb_streams;i++) {
231 st = av_mallocz(sizeof(AVFormatContext));
232 memcpy(st, ic->streams[i], sizeof(AVStream));
236 av_close_input_file(ic);
240 #define MAX_AUDIO_PACKET_SIZE 16384
242 static void do_audio_out(AVFormatContext *s,
245 unsigned char *buf, int size)
248 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
249 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
250 int size_out, frame_bytes, ret;
253 enc = &ost->st->codec;
255 if (ost->audio_resample) {
257 size_out = audio_resample(ost->resample,
258 (short *)buftmp, (short *)buf,
259 size / (ist->st->codec.channels * 2));
260 size_out = size_out * enc->channels * 2;
266 /* now encode as many frames as possible */
267 if (enc->frame_size > 1) {
268 /* output resampled raw samples */
269 fifo_write(&ost->fifo, buftmp, size_out,
272 frame_bytes = enc->frame_size * 2 * enc->channels;
274 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
275 &ost->fifo.rptr) == 0) {
276 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
278 s->format->write_packet(s, ost->index, audio_out, ret, 0);
281 /* output a pcm frame */
282 /* XXX: change encoding codec API to avoid this ? */
283 switch(enc->codec->id) {
284 case CODEC_ID_PCM_S16LE:
285 case CODEC_ID_PCM_S16BE:
286 case CODEC_ID_PCM_U16LE:
287 case CODEC_ID_PCM_U16BE:
290 size_out = size_out >> 1;
293 ret = avcodec_encode_audio(enc, audio_out, size_out,
295 s->format->write_packet(s, ost->index, audio_out, ret, 0);
299 /* write a picture to a raw mux */
300 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
301 int pix_fmt, int w, int h)
303 UINT8 *buf, *src, *dest;
306 /* XXX: not efficient, should add test if we can take
307 directly the AVPicture */
309 case PIX_FMT_YUV420P:
310 size = avpicture_get_size(pix_fmt, w, h);
320 src = picture->data[i];
322 memcpy(dest, src, w);
324 src += picture->linesize[i];
328 case PIX_FMT_YUV422P:
338 src = picture->data[i];
340 memcpy(dest, src, w);
342 src += picture->linesize[i];
346 case PIX_FMT_YUV444P:
353 src = picture->data[i];
355 memcpy(dest, src, w);
357 src += picture->linesize[i];
367 src = picture->data[0];
369 memcpy(dest, src, w * 2);
371 src += picture->linesize[0];
381 src = picture->data[0];
383 memcpy(dest, src, w * 3);
385 src += picture->linesize[0];
391 s->format->write_packet(s, index, buf, size, 0);
396 static void do_video_out(AVFormatContext *s,
402 int n1, n2, nb, i, ret, frame_number;
403 AVPicture *picture, *picture2, *pict;
404 AVPicture picture_tmp1, picture_tmp2;
405 UINT8 video_buffer[1024*1024];
406 UINT8 *buf = NULL, *buf1 = NULL;
407 AVCodecContext *enc, *dec;
409 enc = &ost->st->codec;
410 dec = &ist->st->codec;
412 frame_number = ist->frame_number;
413 /* first drop frame if needed */
414 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
415 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
420 /* deinterlace : must be done before any resize */
421 if (do_deinterlace) {
424 /* create temporary picture */
425 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
430 picture2 = &picture_tmp2;
431 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
433 if (avpicture_deinterlace(picture2, picture1,
434 dec->pix_fmt, dec->width, dec->height) < 0) {
435 /* if error, do not deinterlace */
444 /* convert pixel format if needed */
445 if (enc->pix_fmt != dec->pix_fmt) {
448 /* create temporary picture */
449 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
453 pict = &picture_tmp1;
454 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
456 if (img_convert(pict, enc->pix_fmt,
457 picture2, dec->pix_fmt,
458 dec->width, dec->height) < 0) {
459 fprintf(stderr, "pixel format conversion not handled\n");
466 /* XXX: resampling could be done before raw format convertion in
467 some cases to go faster */
468 /* XXX: only works for YUV420P */
469 if (ost->video_resample) {
470 picture = &ost->pict_tmp;
471 img_resample(ost->img_resample_ctx, picture, pict);
476 /* duplicates frame if needed */
477 /* XXX: pb because no interleaving */
479 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
480 /* handles sameq here. This is not correct because it may
481 not be a global option */
483 enc->quality = dec->quality;
485 ret = avcodec_encode_video(enc,
486 video_buffer, sizeof(video_buffer),
488 s->format->write_packet(s, ost->index, video_buffer, ret, 0);
491 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
501 static void do_video_stats(AVOutputStream *ost,
505 static FILE *fvstats=NULL;
506 static INT64 total_size = 0;
513 double ti1, bitrate, avg_bitrate;
517 today = localtime(&today2);
518 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
521 fvstats = fopen(filename,"w");
529 enc = &ost->st->codec;
530 total_size += frame_size;
531 if (enc->codec_type == CODEC_TYPE_VIDEO) {
532 frame_number = ist->frame_number;
533 fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
535 fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
537 fprintf(fvstats,"f_size= %6d ", frame_size);
538 /* compute min pts value */
539 if (!ist->discard && ist->pts < ti) {
542 ti1 = (double)ti / 1000000.0;
546 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
547 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
548 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
549 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
550 fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
557 static void hex_dump(UINT8 *buf, int size)
561 for(i=0;i<size;i+=16) {
568 printf(" %02x", buf[i+j]);
575 if (c < ' ' || c > '~')
584 * The following code is the main loop of the file converter
586 static int av_encode(AVFormatContext **output_files,
588 AVFormatContext **input_files,
590 AVStreamMap *stream_maps, int nb_stream_maps)
592 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
593 AVFormatContext *is, *os;
594 AVCodecContext *codec, *icodec;
595 AVOutputStream *ost, **ost_table = NULL;
596 AVInputStream *ist, **ist_table = NULL;
597 INT64 min_pts, start_time;
598 AVInputFile *file_table;
600 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
604 memset(file_table, 0, sizeof(file_table));
606 /* input stream init */
608 for(i=0;i<nb_input_files;i++) {
610 file_table[i].ist_index = j;
615 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
619 for(i=0;i<nb_istreams;i++) {
620 ist = av_mallocz(sizeof(AVInputStream));
626 for(i=0;i<nb_input_files;i++) {
628 for(k=0;k<is->nb_streams;k++) {
629 ist = ist_table[j++];
630 ist->st = is->streams[k];
633 ist->discard = 1; /* the stream is discarded by default
638 /* output stream init */
640 for(i=0;i<nb_output_files;i++) {
641 os = output_files[i];
642 nb_ostreams += os->nb_streams;
644 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
645 fprintf(stderr, "Number of stream maps must match number of output streams\n");
649 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
652 for(i=0;i<nb_ostreams;i++) {
653 ost = av_mallocz(sizeof(AVOutputStream));
660 for(k=0;k<nb_output_files;k++) {
661 os = output_files[k];
662 for(i=0;i<os->nb_streams;i++) {
664 ost = ost_table[n++];
667 ost->st = os->streams[i];
668 if (nb_stream_maps > 0) {
669 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
670 stream_maps[n-1].stream_index;
672 /* get corresponding input stream index : we select the first one with the right type */
674 for(j=0;j<nb_istreams;j++) {
677 ist->st->codec.codec_type == ost->st->codec.codec_type) {
678 ost->source_index = j;
684 /* try again and reuse existing stream */
685 for(j=0;j<nb_istreams;j++) {
687 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
688 ost->source_index = j;
693 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
694 ost->file_index, ost->index);
699 ist = ist_table[ost->source_index];
704 /* dump the stream mapping */
705 fprintf(stderr, "Stream mapping:\n");
706 for(i=0;i<nb_ostreams;i++) {
708 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
709 ist_table[ost->source_index]->file_index,
710 ist_table[ost->source_index]->index,
715 /* for each output stream, we compute the right encoding parameters */
716 for(i=0;i<nb_ostreams;i++) {
718 ist = ist_table[ost->source_index];
720 codec = &ost->st->codec;
721 icodec = &ist->st->codec;
723 switch(codec->codec_type) {
724 case CODEC_TYPE_AUDIO:
725 /* check if same codec with same parameters. If so, no
726 reencoding is needed */
727 if (codec->codec_id == icodec->codec_id &&
728 codec->bit_rate == icodec->bit_rate &&
729 codec->sample_rate == icodec->sample_rate &&
730 codec->channels == icodec->channels) {
732 /* use the same frame size */
733 codec->frame_size = icodec->frame_size;
734 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
736 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
738 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
741 if (codec->channels == icodec->channels &&
742 codec->sample_rate == icodec->sample_rate) {
743 ost->audio_resample = 0;
745 if (codec->channels != icodec->channels &&
746 icodec->codec_id == CODEC_ID_AC3) {
747 /* Special case for 5:1 AC3 input */
748 /* and mono or stereo output */
749 ost->audio_resample = 0;
750 /* Request specific number of channels */
751 icodec->channels = codec->channels;
753 ost->audio_resample = 1;
754 ost->resample = audio_resample_init(codec->channels, icodec->channels,
756 icodec->sample_rate);
759 ist->decoding_needed = 1;
760 ost->encoding_needed = 1;
763 case CODEC_TYPE_VIDEO:
764 /* check if same codec with same parameters. If so, no
765 reencoding is needed */
766 if (codec->codec_id == icodec->codec_id &&
767 codec->bit_rate == icodec->bit_rate &&
768 codec->frame_rate == icodec->frame_rate &&
769 codec->width == icodec->width &&
770 codec->height == icodec->height) {
773 if (codec->width == icodec->width &&
774 codec->height == icodec->height) {
775 ost->video_resample = 0;
778 ost->video_resample = 1;
779 buf = malloc((codec->width * codec->height * 3) / 2);
782 ost->pict_tmp.data[0] = buf;
783 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
784 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
785 ost->pict_tmp.linesize[0] = codec->width;
786 ost->pict_tmp.linesize[1] = codec->width / 2;
787 ost->pict_tmp.linesize[2] = codec->width / 2;
789 ost->img_resample_ctx = img_resample_init(
790 ost->st->codec.width, ost->st->codec.height,
791 ist->st->codec.width, ist->st->codec.height);
793 ost->encoding_needed = 1;
794 ist->decoding_needed = 1;
800 /* open each encoder */
801 for(i=0;i<nb_ostreams;i++) {
803 if (ost->encoding_needed) {
805 codec = avcodec_find_encoder(ost->st->codec.codec_id);
807 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
808 ost->file_index, ost->index);
811 if (avcodec_open(&ost->st->codec, codec) < 0) {
812 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
813 ost->file_index, ost->index);
819 /* open each decoder */
820 for(i=0;i<nb_istreams;i++) {
822 if (ist->decoding_needed) {
824 codec = avcodec_find_decoder(ist->st->codec.codec_id);
826 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
827 ist->file_index, ist->index);
830 if (avcodec_open(&ist->st->codec, codec) < 0) {
831 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
832 ist->file_index, ist->index);
839 for(i=0;i<nb_istreams;i++) {
842 ist->frame_number = 0;
845 /* compute buffer size max (should use a complete heuristic) */
846 for(i=0;i<nb_input_files;i++) {
847 file_table[i].buffer_size_max = 2048;
850 /* open files and write file headers */
851 for(i=0;i<nb_output_files;i++) {
852 os = output_files[i];
853 if (os->format->write_header(os) < 0) {
854 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
862 fprintf(stderr, "Press [q] to stop encoding\n");
864 fprintf(stderr, "Press [q] to stop playing\n");
869 start_time = gettime();
872 int file_index, ist_index;
877 int data_size, got_picture;
879 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
882 /* if 'q' pressed, exits */
883 if (read_key() == 'q')
886 /* select the input file with the smallest pts */
889 for(i=0;i<nb_istreams;i++) {
891 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
893 file_index = ist->file_index;
896 /* if none, if is finished */
899 /* finish if recording time exhausted */
900 if (recording_time > 0 && min_pts >= recording_time)
902 /* read a packet from it and output it in the fifo */
903 is = input_files[file_index];
904 if (av_read_packet(is, &pkt) < 0) {
905 file_table[file_index].eof_reached = 1;
908 ist_index = file_table[file_index].ist_index + pkt.stream_index;
909 ist = ist_table[ist_index];
915 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
916 hex_dump(pkt.data, pkt.size);
919 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
925 /* decode the packet if needed */
926 data_buf = NULL; /* fail safe */
928 if (ist->decoding_needed) {
929 switch(ist->st->codec.codec_type) {
930 case CODEC_TYPE_AUDIO:
931 /* XXX: could avoid copy if PCM 16 bits with same
933 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
937 if (data_size == 0) {
943 data_buf = (UINT8 *)samples;
945 case CODEC_TYPE_VIDEO:
946 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
948 size = (ist->st->codec.width * ist->st->codec.height);
949 avpicture_fill(&picture, ptr,
950 ist->st->codec.pix_fmt,
951 ist->st->codec.width,
952 ist->st->codec.height);
955 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
956 ret = avcodec_decode_video(&ist->st->codec,
957 &picture, &got_picture, ptr, len);
960 fprintf(stderr, "Error while decoding stream #%d.%d\n",
961 ist->file_index, ist->index);
962 av_free_packet(&pkt);
982 switch(ist->st->codec.codec_type) {
983 case CODEC_TYPE_AUDIO:
984 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
985 ist->sample_index += data_size / (2 * ist->st->codec.channels);
987 case CODEC_TYPE_VIDEO:
989 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
990 ist->st->codec.frame_rate;
996 /* transcode raw format, encode packets and output them */
998 for(i=0;i<nb_ostreams;i++) {
1001 if (ost->source_index == ist_index) {
1002 os = output_files[ost->file_index];
1004 if (ost->encoding_needed) {
1005 switch(ost->st->codec.codec_type) {
1006 case CODEC_TYPE_AUDIO:
1007 do_audio_out(os, ost, ist, data_buf, data_size);
1009 case CODEC_TYPE_VIDEO:
1010 do_video_out(os, ost, ist, &picture, &frame_size);
1012 do_video_stats(ost, ist, frame_size);
1016 /* no reencoding needed : output the packet directly */
1017 /* force the input stream PTS */
1018 os->format->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
1023 av_free_packet(&pkt);
1025 /* dump report by using the first video and audio streams */
1028 AVFormatContext *oc;
1029 INT64 total_size, ti;
1030 AVCodecContext *enc;
1031 int frame_number, vid;
1032 double bitrate, ti1;
1033 static INT64 last_time;
1035 if ((min_pts - last_time) >= 500000) {
1036 last_time = min_pts;
1038 oc = output_files[0];
1040 total_size = url_ftell(&oc->pb);
1045 for(i=0;i<nb_ostreams;i++) {
1047 enc = &ost->st->codec;
1048 ist = ist_table[ost->source_index];
1049 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1050 frame_number = ist->frame_number;
1051 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1052 frame_number, enc->quality);
1054 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1057 /* compute min pts value */
1058 if (!ist->discard && ist->pts < ti) {
1063 ti1 = (double)ti / 1000000.0;
1066 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1068 sprintf(buf + strlen(buf),
1069 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1070 (double)total_size / 1024, ti1, bitrate);
1072 fprintf(stderr, "%s \r", buf);
1079 /* dump report by using the first video and audio streams */
1082 AVFormatContext *oc;
1083 INT64 total_size, ti;
1084 AVCodecContext *enc;
1085 int frame_number, vid;
1086 double bitrate, ti1;
1088 oc = output_files[0];
1090 total_size = url_ftell(&oc->pb);
1095 for(i=0;i<nb_ostreams;i++) {
1097 enc = &ost->st->codec;
1098 ist = ist_table[ost->source_index];
1099 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1100 frame_number = ist->frame_number;
1101 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1102 frame_number, enc->quality);
1104 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1107 /* compute min pts value */
1108 if (!ist->discard && ist->pts < ti) {
1113 ti1 = ti / 1000000.0;
1116 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1118 sprintf(buf + strlen(buf),
1119 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1120 (double)total_size / 1024, ti1, bitrate);
1122 fprintf(stderr, "%s \n", buf);
1124 /* close each encoder */
1125 for(i=0;i<nb_ostreams;i++) {
1127 if (ost->encoding_needed) {
1128 avcodec_close(&ost->st->codec);
1132 /* close each decoder */
1133 for(i=0;i<nb_istreams;i++) {
1135 if (ist->decoding_needed) {
1136 avcodec_close(&ist->st->codec);
1141 /* write the trailer if needed and close file */
1142 for(i=0;i<nb_output_files;i++) {
1143 os = output_files[i];
1144 os->format->write_trailer(os);
1153 for(i=0;i<nb_istreams;i++) {
1162 for(i=0;i<nb_ostreams;i++) {
1165 if (ost->pict_tmp.data[0])
1166 free(ost->pict_tmp.data[0]);
1167 if (ost->video_resample)
1168 img_resample_close(ost->img_resample_ctx);
1169 if (ost->audio_resample)
1170 audio_resample_close(ost->resample);
1183 int file_read(const char *filename)
1186 unsigned char buffer[1024];
1189 if (url_open(&h, filename, O_RDONLY) < 0) {
1190 printf("could not open '%s'\n", filename);
1194 len = url_read(h, buffer, sizeof(buffer));
1197 for(i=0;i<len;i++) putchar(buffer[i]);
1204 void show_licence(void)
1207 "ffmpeg version " FFMPEG_VERSION "\n"
1208 "Copyright (c) 2000,2001 Gerard Lantau\n"
1209 "This program is free software; you can redistribute it and/or modify\n"
1210 "it under the terms of the GNU General Public License as published by\n"
1211 "the Free Software Foundation; either version 2 of the License, or\n"
1212 "(at your option) any later version.\n"
1214 "This program is distributed in the hope that it will be useful,\n"
1215 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1216 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1217 "GNU General Public License for more details.\n"
1219 "You should have received a copy of the GNU General Public License\n"
1220 "along with this program; if not, write to the Free Software\n"
1221 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1226 void opt_format(const char *arg)
1230 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1232 fprintf(stderr, "Invalid format: %s\n", arg);
1238 void opt_video_bitrate(const char *arg)
1240 video_bit_rate = atoi(arg) * 1000;
1243 void opt_video_bitrate_tolerance(const char *arg)
1245 video_bit_rate_tolerance = atoi(arg) * 1000;
1248 void opt_frame_rate(const char *arg)
1250 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1253 void opt_frame_size(const char *arg)
1255 parse_image_size(&frame_width, &frame_height, arg);
1256 if (frame_width <= 0 || frame_height <= 0) {
1257 fprintf(stderr, "Incorrect frame size\n");
1260 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1261 fprintf(stderr, "Frame size must be a multiple of 2\n");
1266 void opt_gop_size(const char *arg)
1268 gop_size = atoi(arg);
1271 void opt_qscale(const char *arg)
1273 video_qscale = atoi(arg);
1274 if (video_qscale < 0 ||
1275 video_qscale > 31) {
1276 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1281 void opt_qmin(const char *arg)
1283 video_qmin = atoi(arg);
1284 if (video_qmin < 0 ||
1286 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1291 void opt_qmax(const char *arg)
1293 video_qmax = atoi(arg);
1294 if (video_qmax < 0 ||
1296 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1301 void opt_qdiff(const char *arg)
1303 video_qdiff = atoi(arg);
1304 if (video_qdiff < 0 ||
1306 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1311 void opt_qblur(const char *arg)
1313 video_qblur = atof(arg);
1316 void opt_qcomp(const char *arg)
1318 video_qcomp = atof(arg);
1321 void opt_audio_bitrate(const char *arg)
1323 audio_bit_rate = atoi(arg) * 1000;
1326 void opt_audio_rate(const char *arg)
1328 audio_sample_rate = atoi(arg);
1331 void opt_audio_channels(const char *arg)
1333 audio_channels = atoi(arg);
1337 void opt_video_device(const char *arg)
1339 v4l_device = strdup(arg);
1342 void opt_audio_device(const char *arg)
1344 audio_device = strdup(arg);
1348 void opt_audio_codec(const char *arg)
1354 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1359 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1362 audio_codec_id = p->id;
1366 const char *motion_str[] = {
1376 void opt_motion_estimation(const char *arg)
1382 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1385 if (!strcmp(*p, arg))
1389 motion_estimation_method = p - motion_str;
1392 void opt_video_codec(const char *arg)
1398 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1403 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1406 video_codec_id = p->id;
1410 void opt_map(const char *arg)
1416 m = &stream_maps[nb_stream_maps++];
1418 m->file_index = strtol(arg, (char **)&p, 0);
1421 m->stream_index = strtol(arg, (char **)&p, 0);
1424 void opt_recording_time(const char *arg)
1426 recording_time = parse_date(arg, 1);
1429 /* return the number of packet read to find the codec parameters */
1430 int find_codec_parameters(AVFormatContext *ic)
1432 int val, i, count, ret, got_picture, size;
1434 AVCodecContext *enc;
1438 AVPacketList *pktl, **ppktl;
1439 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1443 ppktl = &ic->packet_buffer;
1445 for(i=0;i<ic->nb_streams;i++) {
1446 enc = &ic->streams[i]->codec;
1448 switch(enc->codec_type) {
1449 case CODEC_TYPE_AUDIO:
1450 val = enc->sample_rate;
1452 case CODEC_TYPE_VIDEO:
1459 /* if no parameters supplied, then we should read it from
1464 if (i == ic->nb_streams) {
1470 /* open each codec */
1471 for(i=0;i<ic->nb_streams;i++) {
1472 st = ic->streams[i];
1473 codec = avcodec_find_decoder(st->codec.codec_id);
1474 if (codec == NULL) {
1478 avcodec_open(&st->codec, codec);
1481 pktl = av_mallocz(sizeof(AVPacketList));
1487 /* add the packet in the buffered packet list */
1489 ppktl = &pktl->next;
1492 if (ic->format->read_packet(ic, pkt) < 0) {
1496 st = ic->streams[pkt->stream_index];
1498 /* decode the data and update codec parameters */
1502 switch(st->codec.codec_type) {
1503 case CODEC_TYPE_VIDEO:
1504 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1506 case CODEC_TYPE_AUDIO:
1507 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1527 /* close each codec */
1528 for(i=0;i<ic->nb_streams;i++) {
1529 st = ic->streams[i];
1530 avcodec_close(&st->codec);
1536 int filename_number_test(const char *filename)
1540 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1541 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1542 "Use '%%d' to specify the image number:\n"
1543 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1544 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1552 void opt_input_file(const char *filename)
1554 AVFormatContext *ic;
1555 AVFormatParameters params, *ap = ¶ms;
1556 URLFormat url_format;
1560 ic = av_mallocz(sizeof(AVFormatContext));
1561 strcpy(ic->filename, filename);
1562 /* first format guess to know if we must open file */
1565 fmt = guess_format(NULL, filename, NULL);
1567 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1569 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1570 fprintf(stderr, "Could not open '%s'\n", filename);
1574 /* find format and set default parameters */
1576 err = url_getformat(url_fileno(&ic->pb), &url_format);
1579 fmt = guess_format(url_format.format_name, NULL, NULL);
1580 ap->sample_rate = url_format.sample_rate;
1581 ap->frame_rate = url_format.frame_rate;
1582 ap->channels = url_format.channels;
1583 ap->width = url_format.width;
1584 ap->height = url_format.height;
1585 ap->pix_fmt = url_format.pix_fmt;
1588 fmt = guess_format(NULL, filename, NULL);
1589 memset(ap, 0, sizeof(*ap));
1592 memset(ap, 0, sizeof(*ap));
1595 if (!fmt || !fmt->read_header) {
1596 fprintf(stderr, "%s: Unknown file format\n", filename);
1601 /* get default parameters from command line */
1602 if (!ap->sample_rate)
1603 ap->sample_rate = audio_sample_rate;
1605 ap->channels = audio_channels;
1607 if (!ap->frame_rate)
1608 ap->frame_rate = frame_rate;
1610 ap->width = frame_width;
1612 ap->height = frame_height;
1614 /* check filename in case of an image number is expected */
1615 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1616 if (filename_number_test(ic->filename) < 0)
1620 err = ic->format->read_header(ic, ap);
1622 fprintf(stderr, "%s: Error while parsing header\n", filename);
1626 /* If not enough info for the codecs, we decode the first frames
1627 to get it. (used in mpeg case for example) */
1628 ret = find_codec_parameters(ic);
1630 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1634 /* update the current parameters so that they match the one of the input stream */
1635 for(i=0;i<ic->nb_streams;i++) {
1636 AVCodecContext *enc = &ic->streams[i]->codec;
1637 switch(enc->codec_type) {
1638 case CODEC_TYPE_AUDIO:
1639 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1640 audio_channels = enc->channels;
1641 audio_sample_rate = enc->sample_rate;
1643 case CODEC_TYPE_VIDEO:
1644 frame_height = enc->height;
1645 frame_width = enc->width;
1646 frame_rate = enc->frame_rate;
1651 input_files[nb_input_files] = ic;
1652 /* dump the file content */
1653 dump_format(ic, nb_input_files, filename, 0);
1658 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1660 int has_video, has_audio, i, j;
1661 AVFormatContext *ic;
1665 for(j=0;j<nb_input_files;j++) {
1666 ic = input_files[j];
1667 for(i=0;i<ic->nb_streams;i++) {
1668 AVCodecContext *enc = &ic->streams[i]->codec;
1669 switch(enc->codec_type) {
1670 case CODEC_TYPE_AUDIO:
1673 case CODEC_TYPE_VIDEO:
1679 *has_video_ptr = has_video;
1680 *has_audio_ptr = has_audio;
1683 void opt_output_file(const char *filename)
1686 AVFormatContext *oc;
1687 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1690 if (!strcmp(filename, "-"))
1693 oc = av_mallocz(sizeof(AVFormatContext));
1696 file_format = guess_format(NULL, filename, NULL);
1698 file_format = &mpeg_mux_format;
1701 oc->format = file_format;
1703 if (!strcmp(file_format->name, "ffm") &&
1704 strstart(filename, "http:", NULL)) {
1705 /* special case for files sent to ffserver: we get the stream
1706 parameters from ffserver */
1707 if (read_ffserver_streams(oc, filename) < 0) {
1708 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1712 use_video = file_format->video_codec != CODEC_ID_NONE;
1713 use_audio = file_format->audio_codec != CODEC_ID_NONE;
1715 /* disable if no corresponding type found and at least one
1717 if (nb_input_files > 0) {
1718 check_audio_video_inputs(&input_has_video, &input_has_audio);
1719 if (!input_has_video)
1721 if (!input_has_audio)
1725 /* manual disable */
1726 if (audio_disable) {
1729 if (video_disable) {
1735 AVCodecContext *video_enc;
1737 st = av_mallocz(sizeof(AVStream));
1739 fprintf(stderr, "Could not alloc stream\n");
1742 video_enc = &st->codec;
1744 codec_id = file_format->video_codec;
1745 if (video_codec_id != CODEC_ID_NONE)
1746 codec_id = video_codec_id;
1748 video_enc->codec_id = codec_id;
1749 video_enc->codec_type = CODEC_TYPE_VIDEO;
1751 video_enc->bit_rate = video_bit_rate;
1752 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
1753 video_enc->frame_rate = frame_rate;
1755 video_enc->width = frame_width;
1756 video_enc->height = frame_height;
1758 video_enc->gop_size = gop_size;
1760 video_enc->gop_size = 0;
1761 if (video_qscale || same_quality) {
1762 video_enc->flags |= CODEC_FLAG_QSCALE;
1763 video_enc->quality = video_qscale;
1767 video_enc->flags |= CODEC_FLAG_HQ;
1768 video_enc->flags |= CODEC_FLAG_4MV;
1770 video_enc->qmin= video_qmin;
1771 video_enc->qmax= video_qmax;
1772 video_enc->max_qdiff= video_qdiff;
1773 video_enc->qblur= video_qblur;
1774 video_enc->qcompress= video_qcomp;
1777 video_enc->get_psnr = 1;
1779 video_enc->get_psnr = 0;
1780 /* XXX: need to find a way to set codec parameters */
1781 if (oc->format == &ppm_format ||
1782 oc->format == &ppmpipe_format) {
1783 video_enc->pix_fmt = PIX_FMT_RGB24;
1786 oc->streams[nb_streams] = st;
1791 AVCodecContext *audio_enc;
1793 st = av_mallocz(sizeof(AVStream));
1795 fprintf(stderr, "Could not alloc stream\n");
1798 audio_enc = &st->codec;
1799 codec_id = file_format->audio_codec;
1800 if (audio_codec_id != CODEC_ID_NONE)
1801 codec_id = audio_codec_id;
1802 audio_enc->codec_id = codec_id;
1803 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1805 audio_enc->bit_rate = audio_bit_rate;
1806 audio_enc->sample_rate = audio_sample_rate;
1807 /* For audio codecs other than AC3 we limit */
1808 /* the number of coded channels to stereo */
1809 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
1810 audio_enc->channels = 2;
1812 audio_enc->channels = audio_channels;
1813 oc->streams[nb_streams] = st;
1817 oc->nb_streams = nb_streams;
1820 fprintf(stderr, "No audio or video streams available\n");
1825 nstrcpy(oc->title, sizeof(oc->title), str_title);
1827 nstrcpy(oc->author, sizeof(oc->author), str_author);
1829 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1831 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1834 output_files[nb_output_files] = oc;
1835 /* dump the file content */
1836 dump_format(oc, nb_output_files, filename, 1);
1839 strcpy(oc->filename, filename);
1841 /* check filename in case of an image number is expected */
1842 if (oc->format->flags & AVFMT_NEEDNUMBER) {
1843 if (filename_number_test(oc->filename) < 0)
1847 if (!(oc->format->flags & AVFMT_NOFILE)) {
1848 /* test if it already exists to avoid loosing precious files */
1849 if (!file_overwrite &&
1850 (strchr(filename, ':') == NULL ||
1851 strstart(filename, "file:", NULL))) {
1852 if (url_exist(filename)) {
1855 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1858 if (toupper(c) != 'Y') {
1859 fprintf(stderr, "Not overwriting - exiting\n");
1866 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1867 fprintf(stderr, "Could not open '%s'\n", filename);
1872 /* reset some options */
1876 audio_codec_id = CODEC_ID_NONE;
1877 video_codec_id = CODEC_ID_NONE;
1882 /* prepare dummy protocols for grab */
1883 void prepare_grab(void)
1885 int has_video, has_audio, i, j;
1886 AVFormatContext *oc;
1887 AVFormatContext *ic;
1888 AVFormatParameters ap1, *ap = &ap1;
1890 /* see if audio/video inputs are needed */
1893 memset(ap, 0, sizeof(*ap));
1894 for(j=0;j<nb_output_files;j++) {
1895 oc = output_files[j];
1896 for(i=0;i<oc->nb_streams;i++) {
1897 AVCodecContext *enc = &oc->streams[i]->codec;
1898 switch(enc->codec_type) {
1899 case CODEC_TYPE_AUDIO:
1900 if (enc->sample_rate > ap->sample_rate)
1901 ap->sample_rate = enc->sample_rate;
1902 if (enc->channels > ap->channels)
1903 ap->channels = enc->channels;
1906 case CODEC_TYPE_VIDEO:
1907 if (enc->width > ap->width)
1908 ap->width = enc->width;
1909 if (enc->height > ap->height)
1910 ap->height = enc->height;
1911 if (enc->frame_rate > ap->frame_rate)
1912 ap->frame_rate = enc->frame_rate;
1919 if (has_video == 0 && has_audio == 0) {
1920 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1925 ic = av_open_input_file("", "video_grab_device", 0, ap);
1927 fprintf(stderr, "Could not open video grab device\n");
1930 input_files[nb_input_files] = ic;
1931 dump_format(ic, nb_input_files, v4l_device, 0);
1935 ic = av_open_input_file("", "audio_device", 0, ap);
1937 fprintf(stderr, "Could not open audio grab device\n");
1940 input_files[nb_input_files] = ic;
1941 dump_format(ic, nb_input_files, audio_device, 0);
1948 void prepare_grab(void)
1950 fprintf(stderr, "Must supply at least one input file\n");
1956 /* open the necessary output devices for playing */
1957 void prepare_play(void)
1960 file_format = guess_format("audio_device", NULL, NULL);
1962 fprintf(stderr, "Could not find audio device\n");
1966 opt_output_file(audio_device);
1971 #ifndef CONFIG_WIN32
1972 INT64 getutime(void)
1974 struct rusage rusage;
1976 getrusage(RUSAGE_SELF, &rusage);
1977 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
1980 INT64 getutime(void)
1986 void show_formats(void)
1993 printf("File formats:\n");
1994 printf(" Encoding:");
1995 for(f = first_format; f != NULL; f = f->next) {
1996 if (f->write_header)
1997 printf(" %s", f->name);
2000 printf(" Decoding:");
2001 for(f = first_format; f != NULL; f = f->next) {
2003 printf(" %s", f->name);
2007 printf("Codecs:\n");
2008 printf(" Encoders:");
2009 for(p = first_avcodec; p != NULL; p = p->next) {
2011 printf(" %s", p->name);
2015 printf(" Decoders:");
2016 for(p = first_avcodec; p != NULL; p = p->next) {
2018 printf(" %s", p->name);
2022 printf("Supported file protocols:");
2023 for(up = first_protocol; up != NULL; up = up->next)
2024 printf(" %s:", up->name);
2027 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2028 printf("Motion estimation methods:");
2032 if ((pp - motion_str) == ME_ZERO)
2033 printf("(fastest)");
2034 else if ((pp - motion_str) == ME_FULL)
2035 printf("(slowest)");
2036 else if ((pp - motion_str) == ME_LOG)
2037 printf("(default)");
2044 void show_help(void)
2047 const OptionDef *po;
2050 prog = do_play ? "ffplay" : "ffmpeg";
2052 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
2056 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2057 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2059 printf("usage: ffplay [options] input_file...\n"
2060 "Simple audio player\n");
2064 "Main options are:\n");
2067 printf("\nAdvanced options are:\n");
2068 for(po = options; po->name != NULL; po++) {
2070 expert = (po->flags & OPT_EXPERT) != 0;
2072 strcpy(buf, po->name);
2073 if (po->flags & HAS_ARG) {
2075 strcat(buf, po->argname);
2077 printf("-%-17s %s\n", buf, po->help);
2085 const OptionDef options[] = {
2086 { "L", 0, {(void*)show_licence}, "show license" },
2087 { "h", 0, {(void*)show_help}, "show help" },
2088 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2089 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2090 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2091 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2092 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2093 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2094 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2095 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2096 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2097 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2099 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2100 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2101 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2102 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2103 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2104 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2105 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2106 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2107 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2108 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2109 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2110 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2111 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2113 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
2115 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2116 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2118 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2119 { "sameq", OPT_BOOL, {(void*)&same_quality},
2120 "use same video quality as source (implies VBR)" },
2122 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2123 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2124 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2125 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2127 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2129 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2130 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2131 "deinterlace pictures" },
2132 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2133 "add timings for benchmarking" },
2134 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2135 "dump each input packet" },
2136 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2137 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2142 int main(int argc, char **argv)
2145 const char *opt, *arg;
2146 const OptionDef *po;
2151 /* detect if invoked as player */
2152 i = strlen(argv[0]);
2153 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2161 while (optindex < argc) {
2162 opt = argv[optindex++];
2164 if (opt[0] == '-' && opt[1] != '\0') {
2166 while (po->name != NULL) {
2167 if (!strcmp(opt + 1, po->name))
2172 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2176 if (po->flags & HAS_ARG)
2177 arg = argv[optindex++];
2178 if (po->flags & OPT_STRING) {
2181 *po->u.str_arg = str;
2182 } else if (po->flags & OPT_BOOL) {
2185 po->u.func_arg(arg);
2189 opt_output_file(opt);
2191 opt_input_file(opt);
2198 /* file converter / grab */
2199 if (nb_output_files <= 0) {
2200 fprintf(stderr, "Must supply at least one output file\n");
2204 if (nb_input_files == 0) {
2209 if (nb_input_files <= 0) {
2210 fprintf(stderr, "Must supply at least one input file\n");
2217 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2218 stream_maps, nb_stream_maps);
2219 ti = getutime() - ti;
2221 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2225 for(i=0;i<nb_output_files;i++) {
2226 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2227 url_fclose(&output_files[i]->pb);
2229 for(i=0;i<nb_input_files;i++) {
2230 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2231 url_fclose(&input_files[i]->pb);