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_qscale = 0;
79 static int video_disable = 0;
80 static int video_codec_id = CODEC_ID_NONE;
81 static int same_quality = 0;
82 static int do_deinterlace = 0;
84 static int gop_size = 12;
85 static int intra_only = 0;
86 static int audio_sample_rate = 44100;
87 static int audio_bit_rate = 64000;
88 static int audio_disable = 0;
89 static int audio_channels = 1;
90 static int audio_codec_id = CODEC_ID_NONE;
92 static INT64 recording_time = 0;
93 static int file_overwrite = 0;
94 static char *str_title = NULL;
95 static char *str_author = NULL;
96 static char *str_copyright = NULL;
97 static char *str_comment = NULL;
98 static int do_benchmark = 0;
99 static int do_hex_dump = 0;
100 static int do_play = 0;
101 static int do_psnr = 0;
103 typedef struct AVOutputStream {
104 int file_index; /* file index */
105 int index; /* stream index in the output file */
106 int source_index; /* AVInputStream index */
107 AVStream *st; /* stream in the output file */
108 int encoding_needed; /* true if encoding needed for this stream */
110 int fifo_packet_rptr; /* read index in the corresponding
111 avinputstream packet fifo */
113 AVPicture pict_tmp; /* temporary image for resizing */
115 ImgReSampleContext *img_resample_ctx; /* for image resampling */
119 ReSampleContext *resample; /* for audio resampling */
120 FifoBuffer fifo; /* for compression: one audio fifo per codec */
123 typedef struct AVInputStream {
127 int discard; /* true if stream data should be discarded */
128 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
129 INT64 pts; /* current pts */
130 int frame_number; /* current frame */
131 INT64 sample_index; /* current sample */
134 typedef struct AVInputFile {
135 int eof_reached; /* true if eof reached */
136 int ist_index; /* index of first stream in ist_table */
137 int buffer_size; /* current total buffer size */
138 int buffer_size_max; /* buffer size at which we consider we can stop
144 /* init terminal so that we can grab keys */
145 static struct termios oldtty;
147 static void term_exit(void)
149 tcsetattr (0, TCSANOW, &oldtty);
152 static void term_init(void)
159 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
160 |INLCR|IGNCR|ICRNL|IXON);
161 tty.c_oflag |= OPOST;
162 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
163 tty.c_cflag &= ~(CSIZE|PARENB);
168 tcsetattr (0, TCSANOW, &tty);
173 /* read a key without blocking */
174 static int read_key(void)
185 n = select(1, &rfds, NULL, NULL, &tv);
187 if (read(0, &ch, 1) == 1)
195 /* no interactive support */
196 static void term_exit(void)
200 static void term_init(void)
204 static int read_key(void)
211 int read_ffserver_streams(AVFormatContext *s, const char *filename)
216 ic = av_open_input_file(filename, NULL, FFM_PACKET_SIZE, NULL);
219 /* copy stream format */
220 s->nb_streams = ic->nb_streams;
221 for(i=0;i<ic->nb_streams;i++) {
223 st = av_mallocz(sizeof(AVFormatContext));
224 memcpy(st, ic->streams[i], sizeof(AVStream));
228 av_close_input_file(ic);
232 #define MAX_AUDIO_PACKET_SIZE 16384
234 static void do_audio_out(AVFormatContext *s,
237 unsigned char *buf, int size)
240 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
241 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
242 int size_out, frame_bytes, ret;
245 enc = &ost->st->codec;
247 if (ost->audio_resample) {
249 size_out = audio_resample(ost->resample,
250 (short *)buftmp, (short *)buf,
251 size / (ist->st->codec.channels * 2));
252 size_out = size_out * enc->channels * 2;
258 /* now encode as many frames as possible */
259 if (enc->frame_size > 1) {
260 /* output resampled raw samples */
261 fifo_write(&ost->fifo, buftmp, size_out,
264 frame_bytes = enc->frame_size * 2 * enc->channels;
266 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
267 &ost->fifo.rptr) == 0) {
268 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
270 s->format->write_packet(s, ost->index, audio_out, ret);
273 /* output a pcm frame */
274 /* XXX: change encoding codec API to avoid this ? */
275 switch(enc->codec->id) {
276 case CODEC_ID_PCM_S16LE:
277 case CODEC_ID_PCM_S16BE:
278 case CODEC_ID_PCM_U16LE:
279 case CODEC_ID_PCM_U16BE:
282 size_out = size_out >> 1;
285 ret = avcodec_encode_audio(enc, audio_out, size_out,
287 s->format->write_packet(s, ost->index, audio_out, ret);
291 /* write a picture to a raw mux */
292 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
293 int pix_fmt, int w, int h)
295 UINT8 *buf, *src, *dest;
298 size = avpicture_get_size(pix_fmt, w, h);
303 /* XXX: not efficient, should add test if we can take
304 directly the AVPicture */
306 case PIX_FMT_YUV420P:
313 src = picture->data[i];
315 memcpy(dest, src, w);
317 src += picture->linesize[i];
321 case PIX_FMT_YUV422P:
329 src = picture->data[i];
331 memcpy(dest, src, w);
333 src += picture->linesize[i];
337 case PIX_FMT_YUV444P:
342 src = picture->data[i];
344 memcpy(dest, src, w);
346 src += picture->linesize[i];
354 src = picture->data[0];
356 memcpy(dest, src, w * 2);
358 src += picture->linesize[0];
366 src = picture->data[0];
368 memcpy(dest, src, w * 3);
370 src += picture->linesize[0];
376 s->format->write_packet(s, index, buf, size);
381 static void do_video_out(AVFormatContext *s,
386 int n1, n2, nb, i, ret, frame_number;
387 AVPicture *picture, *picture2, *pict;
388 AVPicture picture_tmp1, picture_tmp2;
389 UINT8 video_buffer[1024*1024];
390 UINT8 *buf = NULL, *buf1 = NULL;
391 AVCodecContext *enc, *dec;
393 enc = &ost->st->codec;
394 dec = &ist->st->codec;
396 frame_number = ist->frame_number;
397 /* first drop frame if needed */
398 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
399 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
404 /* deinterlace : must be done before any resize */
405 if (do_deinterlace) {
408 /* create temporary picture */
409 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
414 picture2 = &picture_tmp2;
415 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
417 if (avpicture_deinterlace(picture2, picture1,
418 dec->pix_fmt, dec->width, dec->height) < 0) {
419 /* if error, do not deinterlace */
428 /* convert pixel format if needed */
429 if (enc->pix_fmt != dec->pix_fmt) {
432 /* create temporary picture */
433 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
437 pict = &picture_tmp1;
438 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
440 if (img_convert(pict, enc->pix_fmt,
441 picture2, dec->pix_fmt,
442 dec->width, dec->height) < 0) {
443 fprintf(stderr, "pixel format conversion not handled\n");
450 /* XXX: resampling could be done before raw format convertion in
451 some cases to go faster */
452 /* XXX: only works for YUV420P */
453 if (ost->video_resample) {
454 picture = &ost->pict_tmp;
455 img_resample(ost->img_resample_ctx, picture, pict);
460 /* duplicates frame if needed */
461 /* XXX: pb because no interleaving */
463 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
464 /* handles sameq here. This is not correct because it may
465 not be a global option */
467 enc->quality = dec->quality;
469 ret = avcodec_encode_video(enc,
470 video_buffer, sizeof(video_buffer),
472 s->format->write_packet(s, ost->index, video_buffer, ret);
474 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
484 static void hex_dump(UINT8 *buf, int size)
488 for(i=0;i<size;i+=16) {
495 printf(" %02x", buf[i+j]);
502 if (c < ' ' || c > '~')
511 * The following code is the main loop of the file converter
513 static int av_encode(AVFormatContext **output_files,
515 AVFormatContext **input_files,
517 AVStreamMap *stream_maps, int nb_stream_maps)
519 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
520 AVFormatContext *is, *os;
521 AVCodecContext *codec, *icodec;
522 AVOutputStream *ost, **ost_table = NULL;
523 AVInputStream *ist, **ist_table = NULL;
524 INT64 min_pts, start_time;
525 AVInputFile *file_table;
527 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
531 memset(file_table, 0, sizeof(file_table));
533 /* input stream init */
535 for(i=0;i<nb_input_files;i++) {
537 file_table[i].ist_index = j;
542 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
546 for(i=0;i<nb_istreams;i++) {
547 ist = av_mallocz(sizeof(AVInputStream));
553 for(i=0;i<nb_input_files;i++) {
555 for(k=0;k<is->nb_streams;k++) {
556 ist = ist_table[j++];
557 ist->st = is->streams[k];
560 ist->discard = 1; /* the stream is discarded by default
565 /* output stream init */
567 for(i=0;i<nb_output_files;i++) {
568 os = output_files[i];
569 nb_ostreams += os->nb_streams;
571 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
572 fprintf(stderr, "Number of stream maps must match number of output streams\n");
576 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
579 for(i=0;i<nb_ostreams;i++) {
580 ost = av_mallocz(sizeof(AVOutputStream));
587 for(k=0;k<nb_output_files;k++) {
588 os = output_files[k];
589 for(i=0;i<os->nb_streams;i++) {
591 ost = ost_table[n++];
594 ost->st = os->streams[i];
595 if (nb_stream_maps > 0) {
596 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
597 stream_maps[n-1].stream_index;
599 /* get corresponding input stream index : we select the first one with the right type */
601 for(j=0;j<nb_istreams;j++) {
604 ist->st->codec.codec_type == ost->st->codec.codec_type) {
605 ost->source_index = j;
611 /* try again and reuse existing stream */
612 for(j=0;j<nb_istreams;j++) {
614 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
615 ost->source_index = j;
620 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
621 ost->file_index, ost->index);
626 ist = ist_table[ost->source_index];
631 /* dump the stream mapping */
632 fprintf(stderr, "Stream mapping:\n");
633 for(i=0;i<nb_ostreams;i++) {
635 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
636 ist_table[ost->source_index]->file_index,
637 ist_table[ost->source_index]->index,
642 /* for each output stream, we compute the right encoding parameters */
643 for(i=0;i<nb_ostreams;i++) {
645 ist = ist_table[ost->source_index];
647 codec = &ost->st->codec;
648 icodec = &ist->st->codec;
650 switch(codec->codec_type) {
651 case CODEC_TYPE_AUDIO:
652 /* check if same codec with same parameters. If so, no
653 reencoding is needed */
654 if (codec->codec_id == icodec->codec_id &&
655 codec->bit_rate == icodec->bit_rate &&
656 codec->sample_rate == icodec->sample_rate &&
657 codec->channels == icodec->channels) {
660 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
663 if (codec->channels == icodec->channels &&
664 codec->sample_rate == icodec->sample_rate) {
665 ost->audio_resample = 0;
667 ost->audio_resample = 1;
668 ost->resample = audio_resample_init(codec->channels, icodec->channels,
670 icodec->sample_rate);
672 ist->decoding_needed = 1;
673 ost->encoding_needed = 1;
676 case CODEC_TYPE_VIDEO:
677 /* check if same codec with same parameters. If so, no
678 reencoding is needed */
679 if (codec->codec_id == icodec->codec_id &&
680 codec->bit_rate == icodec->bit_rate &&
681 codec->frame_rate == icodec->frame_rate &&
682 codec->width == icodec->width &&
683 codec->height == icodec->height) {
686 if (codec->width == icodec->width &&
687 codec->height == icodec->height) {
688 ost->video_resample = 0;
691 ost->video_resample = 1;
692 buf = malloc((codec->width * codec->height * 3) / 2);
695 ost->pict_tmp.data[0] = buf;
696 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
697 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
698 ost->pict_tmp.linesize[0] = codec->width;
699 ost->pict_tmp.linesize[1] = codec->width / 2;
700 ost->pict_tmp.linesize[2] = codec->width / 2;
702 ost->img_resample_ctx = img_resample_init(
703 ost->st->codec.width, ost->st->codec.height,
704 ist->st->codec.width, ist->st->codec.height);
706 ost->encoding_needed = 1;
707 ist->decoding_needed = 1;
713 /* open each encoder */
714 for(i=0;i<nb_ostreams;i++) {
716 if (ost->encoding_needed) {
718 codec = avcodec_find_encoder(ost->st->codec.codec_id);
720 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
721 ost->file_index, ost->index);
724 if (avcodec_open(&ost->st->codec, codec) < 0) {
725 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
726 ost->file_index, ost->index);
732 /* open each decoder */
733 for(i=0;i<nb_istreams;i++) {
735 if (ist->decoding_needed) {
737 codec = avcodec_find_decoder(ist->st->codec.codec_id);
739 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
740 ist->file_index, ist->index);
743 if (avcodec_open(&ist->st->codec, codec) < 0) {
744 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
745 ist->file_index, ist->index);
752 for(i=0;i<nb_istreams;i++) {
755 ist->frame_number = 0;
758 /* compute buffer size max (should use a complete heuristic) */
759 for(i=0;i<nb_input_files;i++) {
760 file_table[i].buffer_size_max = 2048;
763 /* open files and write file headers */
764 for(i=0;i<nb_output_files;i++) {
765 os = output_files[i];
766 if (os->format->write_header(os) < 0) {
767 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
775 fprintf(stderr, "Press [q] to stop encoding\n");
777 fprintf(stderr, "Press [q] to stop playing\n");
782 start_time = gettime();
785 int file_index, ist_index;
790 int data_size, got_picture;
792 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
795 /* if 'q' pressed, exits */
796 if (read_key() == 'q')
799 /* select the input file with the smallest pts */
802 for(i=0;i<nb_istreams;i++) {
804 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
806 file_index = ist->file_index;
809 /* if none, if is finished */
812 /* finish if recording time exhausted */
813 if (recording_time > 0 && min_pts >= recording_time)
815 /* read a packet from it and output it in the fifo */
816 is = input_files[file_index];
817 if (av_read_packet(is, &pkt) < 0) {
818 file_table[file_index].eof_reached = 1;
821 ist_index = file_table[file_index].ist_index + pkt.stream_index;
822 ist = ist_table[ist_index];
828 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
829 hex_dump(pkt.data, pkt.size);
832 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
838 /* decode the packet if needed */
839 data_buf = NULL; /* fail safe */
841 if (ist->decoding_needed) {
842 switch(ist->st->codec.codec_type) {
843 case CODEC_TYPE_AUDIO:
844 /* XXX: could avoid copy if PCM 16 bits with same
846 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
850 if (data_size == 0) {
856 data_buf = (UINT8 *)samples;
858 case CODEC_TYPE_VIDEO:
859 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
861 size = (ist->st->codec.width * ist->st->codec.height);
862 avpicture_fill(&picture, ptr,
863 ist->st->codec.pix_fmt,
864 ist->st->codec.width,
865 ist->st->codec.height);
868 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
869 ret = avcodec_decode_video(&ist->st->codec,
870 &picture, &got_picture, ptr, len);
873 fprintf(stderr, "Error while decoding stream #%d.%d\n",
874 ist->file_index, ist->index);
875 av_free_packet(&pkt);
895 switch(ist->st->codec.codec_type) {
896 case CODEC_TYPE_AUDIO:
897 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
898 ist->sample_index += data_size / (2 * ist->st->codec.channels);
900 case CODEC_TYPE_VIDEO:
902 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
903 ist->st->codec.frame_rate;
909 /* transcode raw format, encode packets and output them */
911 for(i=0;i<nb_ostreams;i++) {
913 if (ost->source_index == ist_index) {
914 os = output_files[ost->file_index];
916 if (ost->encoding_needed) {
917 switch(ost->st->codec.codec_type) {
918 case CODEC_TYPE_AUDIO:
919 do_audio_out(os, ost, ist, data_buf, data_size);
921 case CODEC_TYPE_VIDEO:
922 do_video_out(os, ost, ist, &picture);
926 /* no reencoding needed : output the packet directly */
927 os->format->write_packet(os, ost->index, data_buf, data_size);
932 av_free_packet(&pkt);
934 /* dump report by using the first video and audio streams */
938 INT64 total_size, ti;
940 int frame_number, vid;
942 static INT64 last_time;
944 if ((min_pts - last_time) >= 500000) {
947 oc = output_files[0];
949 total_size = url_ftell(&oc->pb);
954 for(i=0;i<nb_ostreams;i++) {
956 enc = &ost->st->codec;
957 ist = ist_table[ost->source_index];
958 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
959 frame_number = ist->frame_number;
960 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
961 frame_number, enc->quality);
963 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
966 /* compute min pts value */
967 if (!ist->discard && ist->pts < ti) {
972 ti1 = (double)ti / 1000000.0;
975 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
977 sprintf(buf + strlen(buf),
978 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
979 (double)total_size / 1024, ti1, bitrate);
981 fprintf(stderr, "%s \r", buf);
988 /* dump report by using the first video and audio streams */
992 INT64 total_size, ti;
994 int frame_number, vid;
997 oc = output_files[0];
999 total_size = url_ftell(&oc->pb);
1004 for(i=0;i<nb_ostreams;i++) {
1006 enc = &ost->st->codec;
1007 ist = ist_table[ost->source_index];
1008 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1009 frame_number = ist->frame_number;
1010 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1011 frame_number, enc->quality);
1013 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1016 /* compute min pts value */
1017 if (!ist->discard && ist->pts < ti) {
1022 ti1 = ti / 1000000.0;
1025 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1027 sprintf(buf + strlen(buf),
1028 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1029 (double)total_size / 1024, ti1, bitrate);
1031 fprintf(stderr, "%s \n", buf);
1033 /* close each encoder */
1034 for(i=0;i<nb_ostreams;i++) {
1036 if (ost->encoding_needed) {
1037 avcodec_close(&ost->st->codec);
1041 /* close each decoder */
1042 for(i=0;i<nb_istreams;i++) {
1044 if (ist->decoding_needed) {
1045 avcodec_close(&ist->st->codec);
1050 /* write the trailer if needed and close file */
1051 for(i=0;i<nb_output_files;i++) {
1052 os = output_files[i];
1053 os->format->write_trailer(os);
1062 for(i=0;i<nb_istreams;i++) {
1071 for(i=0;i<nb_ostreams;i++) {
1074 if (ost->pict_tmp.data[0])
1075 free(ost->pict_tmp.data[0]);
1076 if (ost->video_resample)
1077 img_resample_close(ost->img_resample_ctx);
1078 if (ost->audio_resample)
1079 audio_resample_close(ost->resample);
1092 int file_read(const char *filename)
1095 unsigned char buffer[1024];
1098 if (url_open(&h, filename, O_RDONLY) < 0) {
1099 printf("could not open '%s'\n", filename);
1103 len = url_read(h, buffer, sizeof(buffer));
1106 for(i=0;i<len;i++) putchar(buffer[i]);
1113 void show_licence(void)
1116 "ffmpeg version " FFMPEG_VERSION "\n"
1117 "Copyright (c) 2000,2001 Gerard Lantau\n"
1118 "This program is free software; you can redistribute it and/or modify\n"
1119 "it under the terms of the GNU General Public License as published by\n"
1120 "the Free Software Foundation; either version 2 of the License, or\n"
1121 "(at your option) any later version.\n"
1123 "This program is distributed in the hope that it will be useful,\n"
1124 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1125 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1126 "GNU General Public License for more details.\n"
1128 "You should have received a copy of the GNU General Public License\n"
1129 "along with this program; if not, write to the Free Software\n"
1130 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1135 void opt_format(const char *arg)
1139 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1141 fprintf(stderr, "Invalid format: %s\n", arg);
1147 void opt_video_bitrate(const char *arg)
1149 video_bit_rate = atoi(arg) * 1000;
1152 void opt_frame_rate(const char *arg)
1154 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1157 void opt_frame_size(const char *arg)
1159 parse_image_size(&frame_width, &frame_height, arg);
1160 if (frame_width <= 0 || frame_height <= 0) {
1161 fprintf(stderr, "Incorrect frame size\n");
1164 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1165 fprintf(stderr, "Frame size must be a multiple of 2\n");
1170 void opt_gop_size(const char *arg)
1172 gop_size = atoi(arg);
1175 void opt_qscale(const char *arg)
1177 video_qscale = atoi(arg);
1178 if (video_qscale < 0 ||
1179 video_qscale > 31) {
1180 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1186 void opt_audio_bitrate(const char *arg)
1188 audio_bit_rate = atoi(arg) * 1000;
1191 void opt_audio_rate(const char *arg)
1193 audio_sample_rate = atoi(arg);
1196 void opt_audio_channels(const char *arg)
1198 audio_channels = atoi(arg);
1202 void opt_video_device(const char *arg)
1204 v4l_device = strdup(arg);
1207 void opt_audio_device(const char *arg)
1209 audio_device = strdup(arg);
1213 void opt_audio_codec(const char *arg)
1219 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1224 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1227 audio_codec_id = p->id;
1231 const char *motion_str[] = {
1239 void opt_motion_estimation(const char *arg)
1245 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1248 if (!strcmp(*p, arg))
1252 motion_estimation_method = p - motion_str;
1255 void opt_video_codec(const char *arg)
1261 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1266 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1269 video_codec_id = p->id;
1273 void opt_map(const char *arg)
1279 m = &stream_maps[nb_stream_maps++];
1281 m->file_index = strtol(arg, (char **)&p, 0);
1284 m->stream_index = strtol(arg, (char **)&p, 0);
1287 void opt_recording_time(const char *arg)
1289 recording_time = parse_date(arg, 1);
1292 /* return the number of packet read to find the codec parameters */
1293 int find_codec_parameters(AVFormatContext *ic)
1295 int val, i, count, ret, got_picture, size;
1297 AVCodecContext *enc;
1301 AVPacketList *pktl, **ppktl;
1302 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1306 ppktl = &ic->packet_buffer;
1308 for(i=0;i<ic->nb_streams;i++) {
1309 enc = &ic->streams[i]->codec;
1311 switch(enc->codec_type) {
1312 case CODEC_TYPE_AUDIO:
1313 val = enc->sample_rate;
1315 case CODEC_TYPE_VIDEO:
1322 /* if no parameters supplied, then we should read it from
1327 if (i == ic->nb_streams) {
1333 /* open each codec */
1334 for(i=0;i<ic->nb_streams;i++) {
1335 st = ic->streams[i];
1336 codec = avcodec_find_decoder(st->codec.codec_id);
1337 if (codec == NULL) {
1341 avcodec_open(&st->codec, codec);
1344 pktl = av_mallocz(sizeof(AVPacketList));
1350 /* add the packet in the buffered packet list */
1352 ppktl = &pktl->next;
1355 if (ic->format->read_packet(ic, pkt) < 0) {
1359 st = ic->streams[pkt->stream_index];
1361 /* decode the data and update codec parameters */
1365 switch(st->codec.codec_type) {
1366 case CODEC_TYPE_VIDEO:
1367 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1369 case CODEC_TYPE_AUDIO:
1370 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1390 /* close each codec */
1391 for(i=0;i<ic->nb_streams;i++) {
1392 st = ic->streams[i];
1393 avcodec_close(&st->codec);
1399 int filename_number_test(const char *filename)
1403 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1404 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1405 "Use '%%d' to specify the image number:\n"
1406 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1407 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1415 void opt_input_file(const char *filename)
1417 AVFormatContext *ic;
1418 AVFormatParameters params, *ap = ¶ms;
1419 URLFormat url_format;
1423 ic = av_mallocz(sizeof(AVFormatContext));
1424 strcpy(ic->filename, filename);
1425 /* first format guess to know if we must open file */
1428 fmt = guess_format(NULL, filename, NULL);
1430 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1432 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1433 fprintf(stderr, "Could not open '%s'\n", filename);
1437 /* find format and set default parameters */
1439 err = url_getformat(url_fileno(&ic->pb), &url_format);
1442 fmt = guess_format(url_format.format_name, NULL, NULL);
1443 ap->sample_rate = url_format.sample_rate;
1444 ap->frame_rate = url_format.frame_rate;
1445 ap->channels = url_format.channels;
1446 ap->width = url_format.width;
1447 ap->height = url_format.height;
1448 ap->pix_fmt = url_format.pix_fmt;
1451 fmt = guess_format(NULL, filename, NULL);
1452 memset(ap, 0, sizeof(*ap));
1455 memset(ap, 0, sizeof(*ap));
1458 if (!fmt || !fmt->read_header) {
1459 fprintf(stderr, "%s: Unknown file format\n", filename);
1464 /* get default parameters from command line */
1465 if (!ap->sample_rate)
1466 ap->sample_rate = audio_sample_rate;
1468 ap->channels = audio_channels;
1470 if (!ap->frame_rate)
1471 ap->frame_rate = frame_rate;
1473 ap->width = frame_width;
1475 ap->height = frame_height;
1477 /* check filename in case of an image number is expected */
1478 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1479 if (filename_number_test(ic->filename) < 0)
1483 err = ic->format->read_header(ic, ap);
1485 fprintf(stderr, "%s: Error while parsing header\n", filename);
1489 /* If not enough info for the codecs, we decode the first frames
1490 to get it. (used in mpeg case for example) */
1491 ret = find_codec_parameters(ic);
1493 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1497 /* update the current parameters so that they match the one of the input stream */
1498 for(i=0;i<ic->nb_streams;i++) {
1499 AVCodecContext *enc = &ic->streams[i]->codec;
1500 switch(enc->codec_type) {
1501 case CODEC_TYPE_AUDIO:
1502 audio_channels = enc->channels;
1503 audio_sample_rate = enc->sample_rate;
1505 case CODEC_TYPE_VIDEO:
1506 frame_height = enc->height;
1507 frame_width = enc->width;
1508 frame_rate = enc->frame_rate;
1513 input_files[nb_input_files] = ic;
1514 /* dump the file content */
1515 dump_format(ic, nb_input_files, filename, 0);
1520 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1522 int has_video, has_audio, i, j;
1523 AVFormatContext *ic;
1527 for(j=0;j<nb_input_files;j++) {
1528 ic = input_files[j];
1529 for(i=0;i<ic->nb_streams;i++) {
1530 AVCodecContext *enc = &ic->streams[i]->codec;
1531 switch(enc->codec_type) {
1532 case CODEC_TYPE_AUDIO:
1535 case CODEC_TYPE_VIDEO:
1541 *has_video_ptr = has_video;
1542 *has_audio_ptr = has_audio;
1545 void opt_output_file(const char *filename)
1548 AVFormatContext *oc;
1549 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1552 if (!strcmp(filename, "-"))
1555 oc = av_mallocz(sizeof(AVFormatContext));
1558 file_format = guess_format(NULL, filename, NULL);
1560 file_format = &mpeg_mux_format;
1563 oc->format = file_format;
1565 if (!strcmp(file_format->name, "ffm") &&
1566 strstart(filename, "http:", NULL)) {
1567 /* special case for files sent to ffserver: we get the stream
1568 parameters from ffserver */
1569 if (read_ffserver_streams(oc, filename) < 0) {
1570 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1574 use_video = file_format->video_codec != CODEC_ID_NONE;
1575 use_audio = file_format->audio_codec != CODEC_ID_NONE;
1577 /* disable if no corresponding type found and at least one
1579 if (nb_input_files > 0) {
1580 check_audio_video_inputs(&input_has_video, &input_has_audio);
1581 if (!input_has_video)
1583 if (!input_has_audio)
1587 /* manual disable */
1588 if (audio_disable) {
1591 if (video_disable) {
1597 AVCodecContext *video_enc;
1599 st = av_mallocz(sizeof(AVStream));
1601 fprintf(stderr, "Could not alloc stream\n");
1604 video_enc = &st->codec;
1606 codec_id = file_format->video_codec;
1607 if (video_codec_id != CODEC_ID_NONE)
1608 codec_id = video_codec_id;
1610 video_enc->codec_id = codec_id;
1611 video_enc->codec_type = CODEC_TYPE_VIDEO;
1613 video_enc->bit_rate = video_bit_rate;
1614 video_enc->frame_rate = frame_rate;
1616 video_enc->width = frame_width;
1617 video_enc->height = frame_height;
1619 video_enc->gop_size = gop_size;
1621 video_enc->gop_size = 0;
1622 if (video_qscale || same_quality) {
1623 video_enc->flags |= CODEC_FLAG_QSCALE;
1624 video_enc->quality = video_qscale;
1627 video_enc->get_psnr = 1;
1629 video_enc->get_psnr = 0;
1630 /* XXX: need to find a way to set codec parameters */
1631 if (oc->format == &ppm_format ||
1632 oc->format == &ppmpipe_format) {
1633 video_enc->pix_fmt = PIX_FMT_RGB24;
1636 oc->streams[nb_streams] = st;
1641 AVCodecContext *audio_enc;
1643 st = av_mallocz(sizeof(AVStream));
1645 fprintf(stderr, "Could not alloc stream\n");
1648 audio_enc = &st->codec;
1649 codec_id = file_format->audio_codec;
1650 if (audio_codec_id != CODEC_ID_NONE)
1651 codec_id = audio_codec_id;
1652 audio_enc->codec_id = codec_id;
1653 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1655 audio_enc->bit_rate = audio_bit_rate;
1656 audio_enc->sample_rate = audio_sample_rate;
1657 audio_enc->channels = audio_channels;
1658 oc->streams[nb_streams] = st;
1662 oc->nb_streams = nb_streams;
1665 fprintf(stderr, "No audio or video streams available\n");
1670 nstrcpy(oc->title, sizeof(oc->title), str_title);
1672 nstrcpy(oc->author, sizeof(oc->author), str_author);
1674 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1676 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1679 output_files[nb_output_files] = oc;
1680 /* dump the file content */
1681 dump_format(oc, nb_output_files, filename, 1);
1684 strcpy(oc->filename, filename);
1686 /* check filename in case of an image number is expected */
1687 if (oc->format->flags & AVFMT_NEEDNUMBER) {
1688 if (filename_number_test(oc->filename) < 0)
1692 if (!(oc->format->flags & AVFMT_NOFILE)) {
1693 /* test if it already exists to avoid loosing precious files */
1694 if (!file_overwrite &&
1695 (strchr(filename, ':') == NULL ||
1696 strstart(filename, "file:", NULL))) {
1697 if (url_exist(filename)) {
1700 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1703 if (toupper(c) != 'Y') {
1704 fprintf(stderr, "Not overwriting - exiting\n");
1711 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1712 fprintf(stderr, "Could not open '%s'\n", filename);
1717 /* reset some options */
1721 audio_codec_id = CODEC_ID_NONE;
1722 video_codec_id = CODEC_ID_NONE;
1727 /* prepare dummy protocols for grab */
1728 void prepare_grab(void)
1730 int has_video, has_audio, i, j;
1731 AVFormatContext *oc;
1732 AVFormatContext *ic;
1733 AVFormatParameters ap1, *ap = &ap1;
1735 /* see if audio/video inputs are needed */
1738 memset(ap, 0, sizeof(*ap));
1739 for(j=0;j<nb_output_files;j++) {
1740 oc = output_files[j];
1741 for(i=0;i<oc->nb_streams;i++) {
1742 AVCodecContext *enc = &oc->streams[i]->codec;
1743 switch(enc->codec_type) {
1744 case CODEC_TYPE_AUDIO:
1745 if (enc->sample_rate > ap->sample_rate)
1746 ap->sample_rate = enc->sample_rate;
1747 if (enc->channels > ap->channels)
1748 ap->channels = enc->channels;
1751 case CODEC_TYPE_VIDEO:
1752 if (enc->width > ap->width)
1753 ap->width = enc->width;
1754 if (enc->height > ap->height)
1755 ap->height = enc->height;
1756 if (enc->frame_rate > ap->frame_rate)
1757 ap->frame_rate = enc->frame_rate;
1764 if (has_video == 0 && has_audio == 0) {
1765 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1770 ic = av_open_input_file("", "video_grab_device", 0, ap);
1772 fprintf(stderr, "Could not open video grab device\n");
1775 input_files[nb_input_files] = ic;
1776 dump_format(ic, nb_input_files, v4l_device, 0);
1780 ic = av_open_input_file("", "audio_device", 0, ap);
1782 fprintf(stderr, "Could not open audio grab device\n");
1785 input_files[nb_input_files] = ic;
1786 dump_format(ic, nb_input_files, audio_device, 0);
1793 void prepare_grab(void)
1795 fprintf(stderr, "Must supply at least one input file\n");
1801 /* open the necessary output devices for playing */
1802 void prepare_play(void)
1804 file_format = guess_format("audio_device", NULL, NULL);
1806 fprintf(stderr, "Could not find audio device\n");
1810 opt_output_file(audio_device);
1814 #ifndef CONFIG_WIN32
1815 INT64 getutime(void)
1817 struct rusage rusage;
1819 getrusage(RUSAGE_SELF, &rusage);
1820 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
1823 INT64 getutime(void)
1829 void show_formats(void)
1836 printf("File formats:\n");
1837 printf(" Encoding:");
1838 for(f = first_format; f != NULL; f = f->next) {
1839 if (f->write_header)
1840 printf(" %s", f->name);
1843 printf(" Decoding:");
1844 for(f = first_format; f != NULL; f = f->next) {
1846 printf(" %s", f->name);
1850 printf("Codecs:\n");
1851 printf(" Encoders:");
1852 for(p = first_avcodec; p != NULL; p = p->next) {
1854 printf(" %s", p->name);
1858 printf(" Decoders:");
1859 for(p = first_avcodec; p != NULL; p = p->next) {
1861 printf(" %s", p->name);
1865 printf("Supported file protocols:");
1866 for(up = first_protocol; up != NULL; up = up->next)
1867 printf(" %s:", up->name);
1870 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
1871 printf("Motion estimation methods:");
1875 if ((pp - motion_str) == ME_ZERO)
1876 printf("(fastest)");
1877 else if ((pp - motion_str) == ME_FULL)
1878 printf("(slowest)");
1879 else if ((pp - motion_str) == ME_LOG)
1880 printf("(default)");
1887 void show_help(void)
1890 const OptionDef *po;
1893 prog = do_play ? "ffplay" : "ffmpeg";
1895 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
1899 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
1900 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
1902 printf("usage: ffplay [options] input_file...\n"
1903 "Simple audio player\n");
1907 "Main options are:\n");
1910 printf("\nAdvanced options are:\n");
1911 for(po = options; po->name != NULL; po++) {
1913 expert = (po->flags & OPT_EXPERT) != 0;
1915 strcpy(buf, po->name);
1916 if (po->flags & HAS_ARG) {
1918 strcat(buf, po->argname);
1920 printf("-%-17s %s\n", buf, po->help);
1928 const OptionDef options[] = {
1929 { "L", 0, {(void*)show_licence}, "show license" },
1930 { "h", 0, {(void*)show_help}, "show help" },
1931 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
1932 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
1933 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
1934 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
1935 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
1936 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
1937 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
1938 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
1939 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
1940 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
1942 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
1943 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
1944 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
1945 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
1946 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
1947 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
1948 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
1950 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
1952 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
1953 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
1955 { "sameq", OPT_BOOL, {(void*)&same_quality},
1956 "use same video quality as source (implies VBR)" },
1958 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
1959 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
1960 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
1961 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
1963 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
1965 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
1966 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
1967 "deinterlace pictures" },
1968 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
1969 "add timings for benchmarking" },
1970 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
1971 "dump each input packet" },
1972 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
1977 int main(int argc, char **argv)
1980 const char *opt, *arg;
1981 const OptionDef *po;
1986 /* detect if invoked as player */
1987 i = strlen(argv[0]);
1988 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
1996 while (optindex < argc) {
1997 opt = argv[optindex++];
1999 if (opt[0] == '-' && opt[1] != '\0') {
2001 while (po->name != NULL) {
2002 if (!strcmp(opt + 1, po->name))
2007 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2011 if (po->flags & HAS_ARG)
2012 arg = argv[optindex++];
2013 if (po->flags & OPT_STRING) {
2016 *po->u.str_arg = str;
2017 } else if (po->flags & OPT_BOOL) {
2020 po->u.func_arg(arg);
2024 opt_output_file(opt);
2026 opt_input_file(opt);
2033 /* file converter / grab */
2034 if (nb_output_files <= 0) {
2035 fprintf(stderr, "Must supply at least one output file\n");
2039 if (nb_input_files == 0) {
2044 if (nb_input_files <= 0) {
2045 fprintf(stderr, "Must supply at least one input file\n");
2052 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2053 stream_maps, nb_stream_maps);
2054 ti = getutime() - ti;
2056 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2060 for(i=0;i<nb_output_files;i++) {
2061 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2062 url_fclose(&output_files[i]->pb);
2064 for(i=0;i<nb_input_files;i++) {
2065 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2066 url_fclose(&input_files[i]->pb);