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 me_method = 0;
86 static int video_disable = 0;
87 static int video_codec_id = CODEC_ID_NONE;
88 static int same_quality = 0;
89 static int b_frames = 0;
90 static int use_hq = 0;
91 static int use_4mv = 0;
92 static int do_deinterlace = 0;
94 static int gop_size = 12;
95 static int intra_only = 0;
96 static int audio_sample_rate = 44100;
97 static int audio_bit_rate = 64000;
98 static int audio_disable = 0;
99 static int audio_channels = 1;
100 static int audio_codec_id = CODEC_ID_NONE;
102 static INT64 recording_time = 0;
103 static int file_overwrite = 0;
104 static char *str_title = NULL;
105 static char *str_author = NULL;
106 static char *str_copyright = NULL;
107 static char *str_comment = NULL;
108 static int do_benchmark = 0;
109 static int do_hex_dump = 0;
110 static int do_play = 0;
111 static int do_psnr = 0;
112 static int do_vstats = 0;
114 typedef struct AVOutputStream {
115 int file_index; /* file index */
116 int index; /* stream index in the output file */
117 int source_index; /* AVInputStream index */
118 AVStream *st; /* stream in the output file */
119 int encoding_needed; /* true if encoding needed for this stream */
121 int fifo_packet_rptr; /* read index in the corresponding
122 avinputstream packet fifo */
124 AVPicture pict_tmp; /* temporary image for resizing */
126 ImgReSampleContext *img_resample_ctx; /* for image resampling */
130 ReSampleContext *resample; /* for audio resampling */
131 FifoBuffer fifo; /* for compression: one audio fifo per codec */
134 typedef struct AVInputStream {
138 int discard; /* true if stream data should be discarded */
139 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
140 INT64 pts; /* current pts */
141 int frame_number; /* current frame */
142 INT64 sample_index; /* current sample */
145 typedef struct AVInputFile {
146 int eof_reached; /* true if eof reached */
147 int ist_index; /* index of first stream in ist_table */
148 int buffer_size; /* current total buffer size */
149 int buffer_size_max; /* buffer size at which we consider we can stop
155 /* init terminal so that we can grab keys */
156 static struct termios oldtty;
158 static void term_exit(void)
160 tcsetattr (0, TCSANOW, &oldtty);
163 static void term_init(void)
170 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
171 |INLCR|IGNCR|ICRNL|IXON);
172 tty.c_oflag |= OPOST;
173 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
174 tty.c_cflag &= ~(CSIZE|PARENB);
179 tcsetattr (0, TCSANOW, &tty);
184 /* read a key without blocking */
185 static int read_key(void)
196 n = select(1, &rfds, NULL, NULL, &tv);
198 if (read(0, &ch, 1) == 1)
206 /* no interactive support */
207 static void term_exit(void)
211 static void term_init(void)
215 static int read_key(void)
222 int read_ffserver_streams(AVFormatContext *s, const char *filename)
227 ic = av_open_input_file(filename, NULL, FFM_PACKET_SIZE, NULL);
230 /* copy stream format */
231 s->nb_streams = ic->nb_streams;
232 for(i=0;i<ic->nb_streams;i++) {
234 st = av_mallocz(sizeof(AVFormatContext));
235 memcpy(st, ic->streams[i], sizeof(AVStream));
239 av_close_input_file(ic);
243 #define MAX_AUDIO_PACKET_SIZE 16384
245 static void do_audio_out(AVFormatContext *s,
248 unsigned char *buf, int size)
251 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
252 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
253 int size_out, frame_bytes, ret;
256 enc = &ost->st->codec;
258 if (ost->audio_resample) {
260 size_out = audio_resample(ost->resample,
261 (short *)buftmp, (short *)buf,
262 size / (ist->st->codec.channels * 2));
263 size_out = size_out * enc->channels * 2;
269 /* now encode as many frames as possible */
270 if (enc->frame_size > 1) {
271 /* output resampled raw samples */
272 fifo_write(&ost->fifo, buftmp, size_out,
275 frame_bytes = enc->frame_size * 2 * enc->channels;
277 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
278 &ost->fifo.rptr) == 0) {
279 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
281 s->format->write_packet(s, ost->index, audio_out, ret, 0);
284 /* output a pcm frame */
285 /* XXX: change encoding codec API to avoid this ? */
286 switch(enc->codec->id) {
287 case CODEC_ID_PCM_S16LE:
288 case CODEC_ID_PCM_S16BE:
289 case CODEC_ID_PCM_U16LE:
290 case CODEC_ID_PCM_U16BE:
293 size_out = size_out >> 1;
296 ret = avcodec_encode_audio(enc, audio_out, size_out,
298 s->format->write_packet(s, ost->index, audio_out, ret, 0);
302 /* write a picture to a raw mux */
303 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
304 int pix_fmt, int w, int h)
306 UINT8 *buf, *src, *dest;
309 /* XXX: not efficient, should add test if we can take
310 directly the AVPicture */
312 case PIX_FMT_YUV420P:
313 size = avpicture_get_size(pix_fmt, w, h);
323 src = picture->data[i];
325 memcpy(dest, src, w);
327 src += picture->linesize[i];
331 case PIX_FMT_YUV422P:
341 src = picture->data[i];
343 memcpy(dest, src, w);
345 src += picture->linesize[i];
349 case PIX_FMT_YUV444P:
356 src = picture->data[i];
358 memcpy(dest, src, w);
360 src += picture->linesize[i];
370 src = picture->data[0];
372 memcpy(dest, src, w * 2);
374 src += picture->linesize[0];
384 src = picture->data[0];
386 memcpy(dest, src, w * 3);
388 src += picture->linesize[0];
394 s->format->write_packet(s, index, buf, size, 0);
399 static void do_video_out(AVFormatContext *s,
405 int n1, n2, nb, i, ret, frame_number;
406 AVPicture *picture, *picture2, *pict;
407 AVPicture picture_tmp1, picture_tmp2;
408 UINT8 video_buffer[1024*1024];
409 UINT8 *buf = NULL, *buf1 = NULL;
410 AVCodecContext *enc, *dec;
412 enc = &ost->st->codec;
413 dec = &ist->st->codec;
415 frame_number = ist->frame_number;
416 /* first drop frame if needed */
417 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
418 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
423 /* deinterlace : must be done before any resize */
424 if (do_deinterlace) {
427 /* create temporary picture */
428 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
433 picture2 = &picture_tmp2;
434 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
436 if (avpicture_deinterlace(picture2, picture1,
437 dec->pix_fmt, dec->width, dec->height) < 0) {
438 /* if error, do not deinterlace */
447 /* convert pixel format if needed */
448 if (enc->pix_fmt != dec->pix_fmt) {
451 /* create temporary picture */
452 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
456 pict = &picture_tmp1;
457 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
459 if (img_convert(pict, enc->pix_fmt,
460 picture2, dec->pix_fmt,
461 dec->width, dec->height) < 0) {
462 fprintf(stderr, "pixel format conversion not handled\n");
469 /* XXX: resampling could be done before raw format convertion in
470 some cases to go faster */
471 /* XXX: only works for YUV420P */
472 if (ost->video_resample) {
473 picture = &ost->pict_tmp;
474 img_resample(ost->img_resample_ctx, picture, pict);
479 /* duplicates frame if needed */
480 /* XXX: pb because no interleaving */
482 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
483 /* handles sameq here. This is not correct because it may
484 not be a global option */
486 enc->quality = dec->quality;
488 ret = avcodec_encode_video(enc,
489 video_buffer, sizeof(video_buffer),
491 s->format->write_packet(s, ost->index, video_buffer, ret, 0);
494 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
504 static void do_video_stats(AVOutputStream *ost,
508 static FILE *fvstats=NULL;
509 static INT64 total_size = 0;
516 double ti1, bitrate, avg_bitrate;
520 today = localtime(&today2);
521 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
524 fvstats = fopen(filename,"w");
532 enc = &ost->st->codec;
533 total_size += frame_size;
534 if (enc->codec_type == CODEC_TYPE_VIDEO) {
535 frame_number = ist->frame_number;
536 fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
538 fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
540 fprintf(fvstats,"f_size= %6d ", frame_size);
541 /* compute min pts value */
542 if (!ist->discard && ist->pts < ti) {
545 ti1 = (double)ti / 1000000.0;
549 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
550 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
551 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
552 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
553 fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
560 static void hex_dump(UINT8 *buf, int size)
564 for(i=0;i<size;i+=16) {
571 printf(" %02x", buf[i+j]);
578 if (c < ' ' || c > '~')
587 * The following code is the main loop of the file converter
589 static int av_encode(AVFormatContext **output_files,
591 AVFormatContext **input_files,
593 AVStreamMap *stream_maps, int nb_stream_maps)
595 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
596 AVFormatContext *is, *os;
597 AVCodecContext *codec, *icodec;
598 AVOutputStream *ost, **ost_table = NULL;
599 AVInputStream *ist, **ist_table = NULL;
600 INT64 min_pts, start_time;
601 AVInputFile *file_table;
603 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
607 memset(file_table, 0, sizeof(file_table));
609 /* input stream init */
611 for(i=0;i<nb_input_files;i++) {
613 file_table[i].ist_index = j;
618 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
622 for(i=0;i<nb_istreams;i++) {
623 ist = av_mallocz(sizeof(AVInputStream));
629 for(i=0;i<nb_input_files;i++) {
631 for(k=0;k<is->nb_streams;k++) {
632 ist = ist_table[j++];
633 ist->st = is->streams[k];
636 ist->discard = 1; /* the stream is discarded by default
641 /* output stream init */
643 for(i=0;i<nb_output_files;i++) {
644 os = output_files[i];
645 nb_ostreams += os->nb_streams;
647 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
648 fprintf(stderr, "Number of stream maps must match number of output streams\n");
652 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
655 for(i=0;i<nb_ostreams;i++) {
656 ost = av_mallocz(sizeof(AVOutputStream));
663 for(k=0;k<nb_output_files;k++) {
664 os = output_files[k];
665 for(i=0;i<os->nb_streams;i++) {
667 ost = ost_table[n++];
670 ost->st = os->streams[i];
671 if (nb_stream_maps > 0) {
672 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
673 stream_maps[n-1].stream_index;
675 /* get corresponding input stream index : we select the first one with the right type */
677 for(j=0;j<nb_istreams;j++) {
680 ist->st->codec.codec_type == ost->st->codec.codec_type) {
681 ost->source_index = j;
687 /* try again and reuse existing stream */
688 for(j=0;j<nb_istreams;j++) {
690 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
691 ost->source_index = j;
696 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
697 ost->file_index, ost->index);
702 ist = ist_table[ost->source_index];
707 /* dump the stream mapping */
708 fprintf(stderr, "Stream mapping:\n");
709 for(i=0;i<nb_ostreams;i++) {
711 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
712 ist_table[ost->source_index]->file_index,
713 ist_table[ost->source_index]->index,
718 /* for each output stream, we compute the right encoding parameters */
719 for(i=0;i<nb_ostreams;i++) {
721 ist = ist_table[ost->source_index];
723 codec = &ost->st->codec;
724 icodec = &ist->st->codec;
726 switch(codec->codec_type) {
727 case CODEC_TYPE_AUDIO:
728 /* check if same codec with same parameters. If so, no
729 reencoding is needed */
730 if (codec->codec_id == icodec->codec_id &&
731 codec->bit_rate == icodec->bit_rate &&
732 codec->sample_rate == icodec->sample_rate &&
733 codec->channels == icodec->channels) {
735 /* use the same frame size */
736 codec->frame_size = icodec->frame_size;
737 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
739 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
741 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
744 if (codec->channels == icodec->channels &&
745 codec->sample_rate == icodec->sample_rate) {
746 ost->audio_resample = 0;
748 if (codec->channels != icodec->channels &&
749 icodec->codec_id == CODEC_ID_AC3) {
750 /* Special case for 5:1 AC3 input */
751 /* and mono or stereo output */
752 ost->audio_resample = 0;
753 /* Request specific number of channels */
754 icodec->channels = codec->channels;
756 ost->audio_resample = 1;
757 ost->resample = audio_resample_init(codec->channels, icodec->channels,
759 icodec->sample_rate);
762 ist->decoding_needed = 1;
763 ost->encoding_needed = 1;
766 case CODEC_TYPE_VIDEO:
767 /* check if same codec with same parameters. If so, no
768 reencoding is needed */
769 if (codec->codec_id == icodec->codec_id &&
770 codec->bit_rate == icodec->bit_rate &&
771 codec->frame_rate == icodec->frame_rate &&
772 codec->width == icodec->width &&
773 codec->height == icodec->height) {
776 if (codec->width == icodec->width &&
777 codec->height == icodec->height) {
778 ost->video_resample = 0;
781 ost->video_resample = 1;
782 buf = malloc((codec->width * codec->height * 3) / 2);
785 ost->pict_tmp.data[0] = buf;
786 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
787 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
788 ost->pict_tmp.linesize[0] = codec->width;
789 ost->pict_tmp.linesize[1] = codec->width / 2;
790 ost->pict_tmp.linesize[2] = codec->width / 2;
792 ost->img_resample_ctx = img_resample_init(
793 ost->st->codec.width, ost->st->codec.height,
794 ist->st->codec.width, ist->st->codec.height);
796 ost->encoding_needed = 1;
797 ist->decoding_needed = 1;
803 /* open each encoder */
804 for(i=0;i<nb_ostreams;i++) {
806 if (ost->encoding_needed) {
808 codec = avcodec_find_encoder(ost->st->codec.codec_id);
810 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
811 ost->file_index, ost->index);
814 if (avcodec_open(&ost->st->codec, codec) < 0) {
815 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
816 ost->file_index, ost->index);
822 /* open each decoder */
823 for(i=0;i<nb_istreams;i++) {
825 if (ist->decoding_needed) {
827 codec = avcodec_find_decoder(ist->st->codec.codec_id);
829 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
830 ist->file_index, ist->index);
833 if (avcodec_open(&ist->st->codec, codec) < 0) {
834 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
835 ist->file_index, ist->index);
842 for(i=0;i<nb_istreams;i++) {
845 ist->frame_number = 0;
848 /* compute buffer size max (should use a complete heuristic) */
849 for(i=0;i<nb_input_files;i++) {
850 file_table[i].buffer_size_max = 2048;
853 /* open files and write file headers */
854 for(i=0;i<nb_output_files;i++) {
855 os = output_files[i];
856 if (os->format->write_header(os) < 0) {
857 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
865 fprintf(stderr, "Press [q] to stop encoding\n");
867 fprintf(stderr, "Press [q] to stop playing\n");
872 start_time = gettime();
875 int file_index, ist_index;
880 int data_size, got_picture;
882 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
885 /* if 'q' pressed, exits */
886 if (read_key() == 'q')
889 /* select the input file with the smallest pts */
892 for(i=0;i<nb_istreams;i++) {
894 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
896 file_index = ist->file_index;
899 /* if none, if is finished */
902 /* finish if recording time exhausted */
903 if (recording_time > 0 && min_pts >= recording_time)
905 /* read a packet from it and output it in the fifo */
906 is = input_files[file_index];
907 if (av_read_packet(is, &pkt) < 0) {
908 file_table[file_index].eof_reached = 1;
911 ist_index = file_table[file_index].ist_index + pkt.stream_index;
912 ist = ist_table[ist_index];
918 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
919 hex_dump(pkt.data, pkt.size);
922 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
928 /* decode the packet if needed */
929 data_buf = NULL; /* fail safe */
931 if (ist->decoding_needed) {
932 switch(ist->st->codec.codec_type) {
933 case CODEC_TYPE_AUDIO:
934 /* XXX: could avoid copy if PCM 16 bits with same
936 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
940 if (data_size == 0) {
946 data_buf = (UINT8 *)samples;
948 case CODEC_TYPE_VIDEO:
949 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
951 size = (ist->st->codec.width * ist->st->codec.height);
952 avpicture_fill(&picture, ptr,
953 ist->st->codec.pix_fmt,
954 ist->st->codec.width,
955 ist->st->codec.height);
958 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
959 ret = avcodec_decode_video(&ist->st->codec,
960 &picture, &got_picture, ptr, len);
963 fprintf(stderr, "Error while decoding stream #%d.%d\n",
964 ist->file_index, ist->index);
965 av_free_packet(&pkt);
985 switch(ist->st->codec.codec_type) {
986 case CODEC_TYPE_AUDIO:
987 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
988 ist->sample_index += data_size / (2 * ist->st->codec.channels);
990 case CODEC_TYPE_VIDEO:
992 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
993 ist->st->codec.frame_rate;
999 /* transcode raw format, encode packets and output them */
1001 for(i=0;i<nb_ostreams;i++) {
1004 if (ost->source_index == ist_index) {
1005 os = output_files[ost->file_index];
1007 if (ost->encoding_needed) {
1008 switch(ost->st->codec.codec_type) {
1009 case CODEC_TYPE_AUDIO:
1010 do_audio_out(os, ost, ist, data_buf, data_size);
1012 case CODEC_TYPE_VIDEO:
1013 do_video_out(os, ost, ist, &picture, &frame_size);
1015 do_video_stats(ost, ist, frame_size);
1019 /* no reencoding needed : output the packet directly */
1020 /* force the input stream PTS */
1021 os->format->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
1026 av_free_packet(&pkt);
1028 /* dump report by using the first video and audio streams */
1031 AVFormatContext *oc;
1032 INT64 total_size, ti;
1033 AVCodecContext *enc;
1034 int frame_number, vid;
1035 double bitrate, ti1;
1036 static INT64 last_time;
1038 if ((min_pts - last_time) >= 500000) {
1039 last_time = min_pts;
1041 oc = output_files[0];
1043 total_size = url_ftell(&oc->pb);
1048 for(i=0;i<nb_ostreams;i++) {
1050 enc = &ost->st->codec;
1051 ist = ist_table[ost->source_index];
1052 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1053 frame_number = ist->frame_number;
1054 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1055 frame_number, enc->quality);
1057 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1060 /* compute min pts value */
1061 if (!ist->discard && ist->pts < ti) {
1066 ti1 = (double)ti / 1000000.0;
1069 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1071 sprintf(buf + strlen(buf),
1072 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1073 (double)total_size / 1024, ti1, bitrate);
1075 fprintf(stderr, "%s \r", buf);
1082 /* dump report by using the first video and audio streams */
1085 AVFormatContext *oc;
1086 INT64 total_size, ti;
1087 AVCodecContext *enc;
1088 int frame_number, vid;
1089 double bitrate, ti1;
1091 oc = output_files[0];
1093 total_size = url_ftell(&oc->pb);
1098 for(i=0;i<nb_ostreams;i++) {
1100 enc = &ost->st->codec;
1101 ist = ist_table[ost->source_index];
1102 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1103 frame_number = ist->frame_number;
1104 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1105 frame_number, enc->quality);
1107 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1110 /* compute min pts value */
1111 if (!ist->discard && ist->pts < ti) {
1116 ti1 = ti / 1000000.0;
1119 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1121 sprintf(buf + strlen(buf),
1122 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1123 (double)total_size / 1024, ti1, bitrate);
1125 fprintf(stderr, "%s \n", buf);
1127 /* close each encoder */
1128 for(i=0;i<nb_ostreams;i++) {
1130 if (ost->encoding_needed) {
1131 avcodec_close(&ost->st->codec);
1135 /* close each decoder */
1136 for(i=0;i<nb_istreams;i++) {
1138 if (ist->decoding_needed) {
1139 avcodec_close(&ist->st->codec);
1144 /* write the trailer if needed and close file */
1145 for(i=0;i<nb_output_files;i++) {
1146 os = output_files[i];
1147 os->format->write_trailer(os);
1156 for(i=0;i<nb_istreams;i++) {
1165 for(i=0;i<nb_ostreams;i++) {
1168 if (ost->pict_tmp.data[0])
1169 free(ost->pict_tmp.data[0]);
1170 if (ost->video_resample)
1171 img_resample_close(ost->img_resample_ctx);
1172 if (ost->audio_resample)
1173 audio_resample_close(ost->resample);
1186 int file_read(const char *filename)
1189 unsigned char buffer[1024];
1192 if (url_open(&h, filename, O_RDONLY) < 0) {
1193 printf("could not open '%s'\n", filename);
1197 len = url_read(h, buffer, sizeof(buffer));
1200 for(i=0;i<len;i++) putchar(buffer[i]);
1207 void show_licence(void)
1210 "ffmpeg version " FFMPEG_VERSION "\n"
1211 "Copyright (c) 2000,2001 Gerard Lantau\n"
1212 "This program is free software; you can redistribute it and/or modify\n"
1213 "it under the terms of the GNU General Public License as published by\n"
1214 "the Free Software Foundation; either version 2 of the License, or\n"
1215 "(at your option) any later version.\n"
1217 "This program is distributed in the hope that it will be useful,\n"
1218 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1219 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1220 "GNU General Public License for more details.\n"
1222 "You should have received a copy of the GNU General Public License\n"
1223 "along with this program; if not, write to the Free Software\n"
1224 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1229 void opt_format(const char *arg)
1233 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1235 fprintf(stderr, "Invalid format: %s\n", arg);
1241 void opt_video_bitrate(const char *arg)
1243 video_bit_rate = atoi(arg) * 1000;
1246 void opt_video_bitrate_tolerance(const char *arg)
1248 video_bit_rate_tolerance = atoi(arg) * 1000;
1251 void opt_frame_rate(const char *arg)
1253 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1256 void opt_frame_size(const char *arg)
1258 parse_image_size(&frame_width, &frame_height, arg);
1259 if (frame_width <= 0 || frame_height <= 0) {
1260 fprintf(stderr, "Incorrect frame size\n");
1263 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1264 fprintf(stderr, "Frame size must be a multiple of 2\n");
1269 void opt_gop_size(const char *arg)
1271 gop_size = atoi(arg);
1274 void opt_b_frames(const char *arg)
1276 b_frames = atoi(arg);
1277 if (b_frames > FF_MAX_B_FRAMES) {
1278 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1280 } else if (b_frames < 1) {
1281 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1286 void opt_qscale(const char *arg)
1288 video_qscale = atoi(arg);
1289 if (video_qscale < 0 ||
1290 video_qscale > 31) {
1291 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1296 void opt_qmin(const char *arg)
1298 video_qmin = atoi(arg);
1299 if (video_qmin < 0 ||
1301 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1306 void opt_qmax(const char *arg)
1308 video_qmax = atoi(arg);
1309 if (video_qmax < 0 ||
1311 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1316 void opt_qdiff(const char *arg)
1318 video_qdiff = atoi(arg);
1319 if (video_qdiff < 0 ||
1321 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1326 void opt_qblur(const char *arg)
1328 video_qblur = atof(arg);
1331 void opt_qcomp(const char *arg)
1333 video_qcomp = atof(arg);
1336 void opt_audio_bitrate(const char *arg)
1338 audio_bit_rate = atoi(arg) * 1000;
1341 void opt_audio_rate(const char *arg)
1343 audio_sample_rate = atoi(arg);
1346 void opt_audio_channels(const char *arg)
1348 audio_channels = atoi(arg);
1352 void opt_video_device(const char *arg)
1354 v4l_device = strdup(arg);
1357 void opt_audio_device(const char *arg)
1359 audio_device = strdup(arg);
1363 void opt_audio_codec(const char *arg)
1369 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1374 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1377 audio_codec_id = p->id;
1381 const char *motion_str[] = {
1391 void opt_motion_estimation(const char *arg)
1397 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1400 if (!strcmp(*p, arg))
1404 me_method = (p - motion_str) + 1;
1407 void opt_video_codec(const char *arg)
1413 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1418 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1421 video_codec_id = p->id;
1425 void opt_map(const char *arg)
1431 m = &stream_maps[nb_stream_maps++];
1433 m->file_index = strtol(arg, (char **)&p, 0);
1436 m->stream_index = strtol(arg, (char **)&p, 0);
1439 void opt_recording_time(const char *arg)
1441 recording_time = parse_date(arg, 1);
1444 /* return the number of packet read to find the codec parameters */
1445 int find_codec_parameters(AVFormatContext *ic)
1447 int val, i, count, ret, got_picture, size;
1449 AVCodecContext *enc;
1453 AVPacketList *pktl, **ppktl;
1454 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1458 ppktl = &ic->packet_buffer;
1460 for(i=0;i<ic->nb_streams;i++) {
1461 enc = &ic->streams[i]->codec;
1463 switch(enc->codec_type) {
1464 case CODEC_TYPE_AUDIO:
1465 val = enc->sample_rate;
1467 case CODEC_TYPE_VIDEO:
1474 /* if no parameters supplied, then we should read it from
1479 if (i == ic->nb_streams) {
1485 /* open each codec */
1486 for(i=0;i<ic->nb_streams;i++) {
1487 st = ic->streams[i];
1488 codec = avcodec_find_decoder(st->codec.codec_id);
1489 if (codec == NULL) {
1493 avcodec_open(&st->codec, codec);
1496 pktl = av_mallocz(sizeof(AVPacketList));
1502 /* add the packet in the buffered packet list */
1504 ppktl = &pktl->next;
1507 if (ic->format->read_packet(ic, pkt) < 0) {
1511 st = ic->streams[pkt->stream_index];
1513 /* decode the data and update codec parameters */
1517 switch(st->codec.codec_type) {
1518 case CODEC_TYPE_VIDEO:
1519 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1521 case CODEC_TYPE_AUDIO:
1522 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1542 /* close each codec */
1543 for(i=0;i<ic->nb_streams;i++) {
1544 st = ic->streams[i];
1545 avcodec_close(&st->codec);
1551 int filename_number_test(const char *filename)
1555 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1556 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1557 "Use '%%d' to specify the image number:\n"
1558 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1559 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1567 void opt_input_file(const char *filename)
1569 AVFormatContext *ic;
1570 AVFormatParameters params, *ap = ¶ms;
1571 URLFormat url_format;
1575 ic = av_mallocz(sizeof(AVFormatContext));
1576 strcpy(ic->filename, filename);
1577 /* first format guess to know if we must open file */
1580 fmt = guess_format(NULL, filename, NULL);
1582 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1584 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1585 fprintf(stderr, "Could not open '%s'\n", filename);
1589 /* find format and set default parameters */
1591 err = url_getformat(url_fileno(&ic->pb), &url_format);
1594 fmt = guess_format(url_format.format_name, NULL, NULL);
1595 ap->sample_rate = url_format.sample_rate;
1596 ap->frame_rate = url_format.frame_rate;
1597 ap->channels = url_format.channels;
1598 ap->width = url_format.width;
1599 ap->height = url_format.height;
1600 ap->pix_fmt = url_format.pix_fmt;
1603 fmt = guess_format(NULL, filename, NULL);
1604 memset(ap, 0, sizeof(*ap));
1607 memset(ap, 0, sizeof(*ap));
1610 if (!fmt || !fmt->read_header) {
1611 fprintf(stderr, "%s: Unknown file format\n", filename);
1616 /* get default parameters from command line */
1617 if (!ap->sample_rate)
1618 ap->sample_rate = audio_sample_rate;
1620 ap->channels = audio_channels;
1622 if (!ap->frame_rate)
1623 ap->frame_rate = frame_rate;
1625 ap->width = frame_width;
1627 ap->height = frame_height;
1629 /* check filename in case of an image number is expected */
1630 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1631 if (filename_number_test(ic->filename) < 0)
1635 err = ic->format->read_header(ic, ap);
1637 fprintf(stderr, "%s: Error while parsing header\n", filename);
1641 /* If not enough info for the codecs, we decode the first frames
1642 to get it. (used in mpeg case for example) */
1643 ret = find_codec_parameters(ic);
1645 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1649 /* update the current parameters so that they match the one of the input stream */
1650 for(i=0;i<ic->nb_streams;i++) {
1651 AVCodecContext *enc = &ic->streams[i]->codec;
1652 switch(enc->codec_type) {
1653 case CODEC_TYPE_AUDIO:
1654 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1655 audio_channels = enc->channels;
1656 audio_sample_rate = enc->sample_rate;
1658 case CODEC_TYPE_VIDEO:
1659 frame_height = enc->height;
1660 frame_width = enc->width;
1661 frame_rate = enc->frame_rate;
1666 input_files[nb_input_files] = ic;
1667 /* dump the file content */
1668 dump_format(ic, nb_input_files, filename, 0);
1673 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1675 int has_video, has_audio, i, j;
1676 AVFormatContext *ic;
1680 for(j=0;j<nb_input_files;j++) {
1681 ic = input_files[j];
1682 for(i=0;i<ic->nb_streams;i++) {
1683 AVCodecContext *enc = &ic->streams[i]->codec;
1684 switch(enc->codec_type) {
1685 case CODEC_TYPE_AUDIO:
1688 case CODEC_TYPE_VIDEO:
1694 *has_video_ptr = has_video;
1695 *has_audio_ptr = has_audio;
1698 void opt_output_file(const char *filename)
1701 AVFormatContext *oc;
1702 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1705 if (!strcmp(filename, "-"))
1708 oc = av_mallocz(sizeof(AVFormatContext));
1711 file_format = guess_format(NULL, filename, NULL);
1713 file_format = &mpeg_mux_format;
1716 oc->format = file_format;
1718 if (!strcmp(file_format->name, "ffm") &&
1719 strstart(filename, "http:", NULL)) {
1720 /* special case for files sent to ffserver: we get the stream
1721 parameters from ffserver */
1722 if (read_ffserver_streams(oc, filename) < 0) {
1723 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1727 use_video = file_format->video_codec != CODEC_ID_NONE;
1728 use_audio = file_format->audio_codec != CODEC_ID_NONE;
1730 /* disable if no corresponding type found and at least one
1732 if (nb_input_files > 0) {
1733 check_audio_video_inputs(&input_has_video, &input_has_audio);
1734 if (!input_has_video)
1736 if (!input_has_audio)
1740 /* manual disable */
1741 if (audio_disable) {
1744 if (video_disable) {
1750 AVCodecContext *video_enc;
1752 st = av_mallocz(sizeof(AVStream));
1754 fprintf(stderr, "Could not alloc stream\n");
1757 video_enc = &st->codec;
1759 codec_id = file_format->video_codec;
1760 if (video_codec_id != CODEC_ID_NONE)
1761 codec_id = video_codec_id;
1763 video_enc->codec_id = codec_id;
1764 video_enc->codec_type = CODEC_TYPE_VIDEO;
1766 video_enc->bit_rate = video_bit_rate;
1767 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
1768 video_enc->frame_rate = frame_rate;
1770 video_enc->width = frame_width;
1771 video_enc->height = frame_height;
1773 video_enc->gop_size = gop_size;
1775 video_enc->gop_size = 0;
1776 if (video_qscale || same_quality) {
1777 video_enc->flags |= CODEC_FLAG_QSCALE;
1778 video_enc->quality = video_qscale;
1782 video_enc->flags |= CODEC_FLAG_HQ;
1786 video_enc->flags |= CODEC_FLAG_HQ;
1787 video_enc->flags |= CODEC_FLAG_4MV;
1791 if (codec_id != CODEC_ID_MPEG4) {
1792 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
1795 video_enc->max_b_frames = b_frames;
1796 video_enc->b_frame_strategy = 0;
1797 video_enc->b_quant_factor = 2.0;
1800 video_enc->qmin = video_qmin;
1801 video_enc->qmax = video_qmax;
1802 video_enc->max_qdiff = video_qdiff;
1803 video_enc->qblur = video_qblur;
1804 video_enc->qcompress = video_qcomp;
1807 video_enc->get_psnr = 1;
1809 video_enc->get_psnr = 0;
1811 video_enc->me_method = me_method;
1813 /* XXX: need to find a way to set codec parameters */
1814 if (oc->format == &ppm_format ||
1815 oc->format == &ppmpipe_format) {
1816 video_enc->pix_fmt = PIX_FMT_RGB24;
1819 oc->streams[nb_streams] = st;
1824 AVCodecContext *audio_enc;
1826 st = av_mallocz(sizeof(AVStream));
1828 fprintf(stderr, "Could not alloc stream\n");
1831 audio_enc = &st->codec;
1832 codec_id = file_format->audio_codec;
1833 if (audio_codec_id != CODEC_ID_NONE)
1834 codec_id = audio_codec_id;
1835 audio_enc->codec_id = codec_id;
1836 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1838 audio_enc->bit_rate = audio_bit_rate;
1839 audio_enc->sample_rate = audio_sample_rate;
1840 /* For audio codecs other than AC3 we limit */
1841 /* the number of coded channels to stereo */
1842 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
1843 audio_enc->channels = 2;
1845 audio_enc->channels = audio_channels;
1846 oc->streams[nb_streams] = st;
1850 oc->nb_streams = nb_streams;
1853 fprintf(stderr, "No audio or video streams available\n");
1858 nstrcpy(oc->title, sizeof(oc->title), str_title);
1860 nstrcpy(oc->author, sizeof(oc->author), str_author);
1862 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1864 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1867 output_files[nb_output_files] = oc;
1868 /* dump the file content */
1869 dump_format(oc, nb_output_files, filename, 1);
1872 strcpy(oc->filename, filename);
1874 /* check filename in case of an image number is expected */
1875 if (oc->format->flags & AVFMT_NEEDNUMBER) {
1876 if (filename_number_test(oc->filename) < 0)
1880 if (!(oc->format->flags & AVFMT_NOFILE)) {
1881 /* test if it already exists to avoid loosing precious files */
1882 if (!file_overwrite &&
1883 (strchr(filename, ':') == NULL ||
1884 strstart(filename, "file:", NULL))) {
1885 if (url_exist(filename)) {
1888 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1891 if (toupper(c) != 'Y') {
1892 fprintf(stderr, "Not overwriting - exiting\n");
1899 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1900 fprintf(stderr, "Could not open '%s'\n", filename);
1905 /* reset some options */
1909 audio_codec_id = CODEC_ID_NONE;
1910 video_codec_id = CODEC_ID_NONE;
1915 /* prepare dummy protocols for grab */
1916 void prepare_grab(void)
1918 int has_video, has_audio, i, j;
1919 AVFormatContext *oc;
1920 AVFormatContext *ic;
1921 AVFormatParameters ap1, *ap = &ap1;
1923 /* see if audio/video inputs are needed */
1926 memset(ap, 0, sizeof(*ap));
1927 for(j=0;j<nb_output_files;j++) {
1928 oc = output_files[j];
1929 for(i=0;i<oc->nb_streams;i++) {
1930 AVCodecContext *enc = &oc->streams[i]->codec;
1931 switch(enc->codec_type) {
1932 case CODEC_TYPE_AUDIO:
1933 if (enc->sample_rate > ap->sample_rate)
1934 ap->sample_rate = enc->sample_rate;
1935 if (enc->channels > ap->channels)
1936 ap->channels = enc->channels;
1939 case CODEC_TYPE_VIDEO:
1940 if (enc->width > ap->width)
1941 ap->width = enc->width;
1942 if (enc->height > ap->height)
1943 ap->height = enc->height;
1944 if (enc->frame_rate > ap->frame_rate)
1945 ap->frame_rate = enc->frame_rate;
1952 if (has_video == 0 && has_audio == 0) {
1953 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1958 ic = av_open_input_file("", "video_grab_device", 0, ap);
1960 fprintf(stderr, "Could not open video grab device\n");
1963 input_files[nb_input_files] = ic;
1964 dump_format(ic, nb_input_files, v4l_device, 0);
1968 ic = av_open_input_file("", "audio_device", 0, ap);
1970 fprintf(stderr, "Could not open audio grab device\n");
1973 input_files[nb_input_files] = ic;
1974 dump_format(ic, nb_input_files, audio_device, 0);
1981 void prepare_grab(void)
1983 fprintf(stderr, "Must supply at least one input file\n");
1989 /* open the necessary output devices for playing */
1990 void prepare_play(void)
1993 file_format = guess_format("audio_device", NULL, NULL);
1995 fprintf(stderr, "Could not find audio device\n");
1999 opt_output_file(audio_device);
2004 #ifndef CONFIG_WIN32
2005 INT64 getutime(void)
2007 struct rusage rusage;
2009 getrusage(RUSAGE_SELF, &rusage);
2010 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2013 INT64 getutime(void)
2019 void show_formats(void)
2026 printf("File formats:\n");
2027 printf(" Encoding:");
2028 for(f = first_format; f != NULL; f = f->next) {
2029 if (f->write_header)
2030 printf(" %s", f->name);
2033 printf(" Decoding:");
2034 for(f = first_format; f != NULL; f = f->next) {
2036 printf(" %s", f->name);
2040 printf("Codecs:\n");
2041 printf(" Encoders:");
2042 for(p = first_avcodec; p != NULL; p = p->next) {
2044 printf(" %s", p->name);
2048 printf(" Decoders:");
2049 for(p = first_avcodec; p != NULL; p = p->next) {
2051 printf(" %s", p->name);
2055 printf("Supported file protocols:");
2056 for(up = first_protocol; up != NULL; up = up->next)
2057 printf(" %s:", up->name);
2060 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2061 printf("Motion estimation methods:");
2065 if ((pp - motion_str + 1) == ME_ZERO)
2066 printf("(fastest)");
2067 else if ((pp - motion_str + 1) == ME_FULL)
2068 printf("(slowest)");
2069 else if ((pp - motion_str + 1) == ME_EPZS)
2070 printf("(default)");
2077 void show_help(void)
2080 const OptionDef *po;
2083 prog = do_play ? "ffplay" : "ffmpeg";
2085 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
2089 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2090 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2092 printf("usage: ffplay [options] input_file...\n"
2093 "Simple audio player\n");
2097 "Main options are:\n");
2100 printf("\nAdvanced options are:\n");
2101 for(po = options; po->name != NULL; po++) {
2103 expert = (po->flags & OPT_EXPERT) != 0;
2105 strcpy(buf, po->name);
2106 if (po->flags & HAS_ARG) {
2108 strcat(buf, po->argname);
2110 printf("-%-17s %s\n", buf, po->help);
2118 const OptionDef options[] = {
2119 { "L", 0, {(void*)show_licence}, "show license" },
2120 { "h", 0, {(void*)show_help}, "show help" },
2121 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2122 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2123 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2124 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2125 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2126 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2127 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2128 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2129 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2130 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2132 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2133 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2134 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2135 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2136 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2137 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2138 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2139 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2140 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2141 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2142 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2143 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2144 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2146 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
2148 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2149 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2151 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2152 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2153 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2154 { "sameq", OPT_BOOL, {(void*)&same_quality},
2155 "use same video quality as source (implies VBR)" },
2157 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2158 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2159 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2160 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2162 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2164 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2165 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2166 "deinterlace pictures" },
2167 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2168 "add timings for benchmarking" },
2169 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2170 "dump each input packet" },
2171 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2172 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2177 int main(int argc, char **argv)
2180 const char *opt, *arg;
2181 const OptionDef *po;
2186 /* detect if invoked as player */
2187 i = strlen(argv[0]);
2188 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2196 while (optindex < argc) {
2197 opt = argv[optindex++];
2199 if (opt[0] == '-' && opt[1] != '\0') {
2201 while (po->name != NULL) {
2202 if (!strcmp(opt + 1, po->name))
2207 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2211 if (po->flags & HAS_ARG)
2212 arg = argv[optindex++];
2213 if (po->flags & OPT_STRING) {
2216 *po->u.str_arg = str;
2217 } else if (po->flags & OPT_BOOL) {
2220 po->u.func_arg(arg);
2224 opt_output_file(opt);
2226 opt_input_file(opt);
2233 /* file converter / grab */
2234 if (nb_output_files <= 0) {
2235 fprintf(stderr, "Must supply at least one output file\n");
2239 if (nb_input_files == 0) {
2244 if (nb_input_files <= 0) {
2245 fprintf(stderr, "Must supply at least one input file\n");
2252 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2253 stream_maps, nb_stream_maps);
2254 ti = getutime() - ti;
2256 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2260 for(i=0;i<nb_output_files;i++) {
2261 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2262 url_fclose(&output_files[i]->pb);
2264 for(i=0;i<nb_input_files;i++) {
2265 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2266 url_fclose(&input_files[i]->pb);