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;
102 typedef struct AVOutputStream {
103 int file_index; /* file index */
104 int index; /* stream index in the output file */
105 int source_index; /* AVInputStream index */
106 AVStream *st; /* stream in the output file */
107 int encoding_needed; /* true if encoding needed for this stream */
109 int fifo_packet_rptr; /* read index in the corresponding
110 avinputstream packet fifo */
112 AVPicture pict_tmp; /* temporary image for resizing */
114 ImgReSampleContext *img_resample_ctx; /* for image resampling */
118 ReSampleContext *resample; /* for audio resampling */
119 FifoBuffer fifo; /* for compression: one audio fifo per codec */
122 typedef struct AVInputStream {
126 int discard; /* true if stream data should be discarded */
127 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
128 INT64 pts; /* current pts */
129 int frame_number; /* current frame */
130 INT64 sample_index; /* current sample */
133 typedef struct AVInputFile {
134 int eof_reached; /* true if eof reached */
135 int ist_index; /* index of first stream in ist_table */
136 int buffer_size; /* current total buffer size */
137 int buffer_size_max; /* buffer size at which we consider we can stop
143 /* init terminal so that we can grab keys */
144 static struct termios oldtty;
146 static void term_exit(void)
148 tcsetattr (0, TCSANOW, &oldtty);
151 static void term_init(void)
158 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
159 |INLCR|IGNCR|ICRNL|IXON);
160 tty.c_oflag |= OPOST;
161 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
162 tty.c_cflag &= ~(CSIZE|PARENB);
167 tcsetattr (0, TCSANOW, &tty);
172 /* read a key without blocking */
173 static int read_key(void)
184 n = select(1, &rfds, NULL, NULL, &tv);
186 if (read(0, &ch, 1) == 1)
194 /* no interactive support */
195 static void term_exit(void)
199 static void term_init(void)
203 static int read_key(void)
210 int read_ffserver_streams(AVFormatContext *s, const char *filename)
215 ic = av_open_input_file(filename, NULL, FFM_PACKET_SIZE, NULL);
218 /* copy stream format */
219 s->nb_streams = ic->nb_streams;
220 for(i=0;i<ic->nb_streams;i++) {
222 st = av_mallocz(sizeof(AVFormatContext));
223 memcpy(st, ic->streams[i], sizeof(AVStream));
227 av_close_input_file(ic);
231 #define MAX_AUDIO_PACKET_SIZE 16384
233 static void do_audio_out(AVFormatContext *s,
236 unsigned char *buf, int size)
239 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
240 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
241 int size_out, frame_bytes, ret;
244 enc = &ost->st->codec;
246 if (ost->audio_resample) {
248 size_out = audio_resample(ost->resample,
249 (short *)buftmp, (short *)buf,
250 size / (ist->st->codec.channels * 2));
251 size_out = size_out * enc->channels * 2;
257 /* now encode as many frames as possible */
258 if (enc->frame_size > 1) {
259 /* output resampled raw samples */
260 fifo_write(&ost->fifo, buftmp, size_out,
263 frame_bytes = enc->frame_size * 2 * enc->channels;
265 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
266 &ost->fifo.rptr) == 0) {
267 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
269 s->format->write_packet(s, ost->index, audio_out, ret);
272 /* output a pcm frame */
273 /* XXX: change encoding codec API to avoid this ? */
274 switch(enc->codec->id) {
275 case CODEC_ID_PCM_S16LE:
276 case CODEC_ID_PCM_S16BE:
277 case CODEC_ID_PCM_U16LE:
278 case CODEC_ID_PCM_U16BE:
281 size_out = size_out >> 1;
284 ret = avcodec_encode_audio(enc, audio_out, size_out,
286 s->format->write_packet(s, ost->index, audio_out, ret);
290 /* write a picture to a raw mux */
291 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
292 int pix_fmt, int w, int h)
294 UINT8 *buf, *src, *dest;
297 size = avpicture_get_size(pix_fmt, w, h);
302 /* XXX: not efficient, should add test if we can take
303 directly the AVPicture */
305 case PIX_FMT_YUV420P:
312 src = picture->data[i];
314 memcpy(dest, src, w);
316 src += picture->linesize[i];
320 case PIX_FMT_YUV422P:
328 src = picture->data[i];
330 memcpy(dest, src, w);
332 src += picture->linesize[i];
336 case PIX_FMT_YUV444P:
341 src = picture->data[i];
343 memcpy(dest, src, w);
345 src += picture->linesize[i];
353 src = picture->data[0];
355 memcpy(dest, src, w * 2);
357 src += picture->linesize[0];
365 src = picture->data[0];
367 memcpy(dest, src, w * 3);
369 src += picture->linesize[0];
375 s->format->write_packet(s, index, buf, size);
380 static void do_video_out(AVFormatContext *s,
385 int n1, n2, nb, i, ret, frame_number;
386 AVPicture *picture, *picture2, *pict;
387 AVPicture picture_tmp1, picture_tmp2;
388 UINT8 video_buffer[1024*1024];
389 UINT8 *buf = NULL, *buf1 = NULL;
390 AVCodecContext *enc, *dec;
392 enc = &ost->st->codec;
393 dec = &ist->st->codec;
395 frame_number = ist->frame_number;
396 /* first drop frame if needed */
397 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
398 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
403 /* deinterlace : must be done before any resize */
404 if (do_deinterlace) {
407 /* create temporary picture */
408 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
413 picture2 = &picture_tmp2;
414 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
416 if (avpicture_deinterlace(picture2, picture1,
417 dec->pix_fmt, dec->width, dec->height) < 0) {
418 /* if error, do not deinterlace */
427 /* convert pixel format if needed */
428 if (enc->pix_fmt != dec->pix_fmt) {
431 /* create temporary picture */
432 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
436 pict = &picture_tmp1;
437 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
439 if (img_convert(pict, enc->pix_fmt,
440 picture2, dec->pix_fmt,
441 dec->width, dec->height) < 0) {
442 fprintf(stderr, "pixel format conversion not handled\n");
449 /* XXX: resampling could be done before raw format convertion in
450 some cases to go faster */
451 /* XXX: only works for YUV420P */
452 if (ost->video_resample) {
453 picture = &ost->pict_tmp;
454 img_resample(ost->img_resample_ctx, picture, pict);
459 /* duplicates frame if needed */
460 /* XXX: pb because no interleaving */
462 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
463 /* handles sameq here. This is not correct because it may
464 not be a global option */
466 enc->quality = dec->quality;
468 ret = avcodec_encode_video(enc,
469 video_buffer, sizeof(video_buffer),
471 s->format->write_packet(s, ost->index, video_buffer, ret);
473 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
483 static void hex_dump(UINT8 *buf, int size)
487 for(i=0;i<size;i+=16) {
494 printf(" %02x", buf[i+j]);
501 if (c < ' ' || c > '~')
510 * The following code is the main loop of the file converter
512 static int av_encode(AVFormatContext **output_files,
514 AVFormatContext **input_files,
516 AVStreamMap *stream_maps, int nb_stream_maps)
518 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
519 AVFormatContext *is, *os;
520 AVCodecContext *codec, *icodec;
521 AVOutputStream *ost, **ost_table = NULL;
522 AVInputStream *ist, **ist_table = NULL;
523 INT64 min_pts, start_time;
524 AVInputFile *file_table;
526 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
530 memset(file_table, 0, sizeof(file_table));
532 /* input stream init */
534 for(i=0;i<nb_input_files;i++) {
536 file_table[i].ist_index = j;
541 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
545 for(i=0;i<nb_istreams;i++) {
546 ist = av_mallocz(sizeof(AVInputStream));
552 for(i=0;i<nb_input_files;i++) {
554 for(k=0;k<is->nb_streams;k++) {
555 ist = ist_table[j++];
556 ist->st = is->streams[k];
559 ist->discard = 1; /* the stream is discarded by default
564 /* output stream init */
566 for(i=0;i<nb_output_files;i++) {
567 os = output_files[i];
568 nb_ostreams += os->nb_streams;
570 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
571 fprintf(stderr, "Number of stream maps must match number of output streams\n");
575 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
578 for(i=0;i<nb_ostreams;i++) {
579 ost = av_mallocz(sizeof(AVOutputStream));
586 for(k=0;k<nb_output_files;k++) {
587 os = output_files[k];
588 for(i=0;i<os->nb_streams;i++) {
590 ost = ost_table[n++];
593 ost->st = os->streams[i];
594 if (nb_stream_maps > 0) {
595 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
596 stream_maps[n-1].stream_index;
598 /* get corresponding input stream index : we select the first one with the right type */
600 for(j=0;j<nb_istreams;j++) {
603 ist->st->codec.codec_type == ost->st->codec.codec_type) {
604 ost->source_index = j;
610 /* try again and reuse existing stream */
611 for(j=0;j<nb_istreams;j++) {
613 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
614 ost->source_index = j;
619 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
620 ost->file_index, ost->index);
625 ist = ist_table[ost->source_index];
630 /* dump the stream mapping */
631 fprintf(stderr, "Stream mapping:\n");
632 for(i=0;i<nb_ostreams;i++) {
634 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
635 ist_table[ost->source_index]->file_index,
636 ist_table[ost->source_index]->index,
641 /* for each output stream, we compute the right encoding parameters */
642 for(i=0;i<nb_ostreams;i++) {
644 ist = ist_table[ost->source_index];
646 codec = &ost->st->codec;
647 icodec = &ist->st->codec;
649 switch(codec->codec_type) {
650 case CODEC_TYPE_AUDIO:
651 /* check if same codec with same parameters. If so, no
652 reencoding is needed */
653 if (codec->codec_id == icodec->codec_id &&
654 codec->bit_rate == icodec->bit_rate &&
655 codec->sample_rate == icodec->sample_rate &&
656 codec->channels == icodec->channels) {
659 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
662 if (codec->channels == icodec->channels &&
663 codec->sample_rate == icodec->sample_rate) {
664 ost->audio_resample = 0;
666 ost->audio_resample = 1;
667 ost->resample = audio_resample_init(codec->channels, icodec->channels,
669 icodec->sample_rate);
671 ist->decoding_needed = 1;
672 ost->encoding_needed = 1;
675 case CODEC_TYPE_VIDEO:
676 /* check if same codec with same parameters. If so, no
677 reencoding is needed */
678 if (codec->codec_id == icodec->codec_id &&
679 codec->bit_rate == icodec->bit_rate &&
680 codec->frame_rate == icodec->frame_rate &&
681 codec->width == icodec->width &&
682 codec->height == icodec->height) {
685 if (codec->width == icodec->width &&
686 codec->height == icodec->height) {
687 ost->video_resample = 0;
690 ost->video_resample = 1;
691 buf = malloc((codec->width * codec->height * 3) / 2);
694 ost->pict_tmp.data[0] = buf;
695 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
696 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
697 ost->pict_tmp.linesize[0] = codec->width;
698 ost->pict_tmp.linesize[1] = codec->width / 2;
699 ost->pict_tmp.linesize[2] = codec->width / 2;
701 ost->img_resample_ctx = img_resample_init(
702 ost->st->codec.width, ost->st->codec.height,
703 ist->st->codec.width, ist->st->codec.height);
705 ost->encoding_needed = 1;
706 ist->decoding_needed = 1;
712 /* open each encoder */
713 for(i=0;i<nb_ostreams;i++) {
715 if (ost->encoding_needed) {
717 codec = avcodec_find_encoder(ost->st->codec.codec_id);
719 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
720 ost->file_index, ost->index);
723 if (avcodec_open(&ost->st->codec, codec) < 0) {
724 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
725 ost->file_index, ost->index);
731 /* open each decoder */
732 for(i=0;i<nb_istreams;i++) {
734 if (ist->decoding_needed) {
736 codec = avcodec_find_decoder(ist->st->codec.codec_id);
738 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
739 ist->file_index, ist->index);
742 if (avcodec_open(&ist->st->codec, codec) < 0) {
743 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
744 ist->file_index, ist->index);
751 for(i=0;i<nb_istreams;i++) {
754 ist->frame_number = 0;
757 /* compute buffer size max (should use a complete heuristic) */
758 for(i=0;i<nb_input_files;i++) {
759 file_table[i].buffer_size_max = 2048;
762 /* open files and write file headers */
763 for(i=0;i<nb_output_files;i++) {
764 os = output_files[i];
765 if (os->format->write_header(os) < 0) {
766 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
774 fprintf(stderr, "Press [q] to stop encoding\n");
776 fprintf(stderr, "Press [q] to stop playing\n");
781 start_time = gettime();
784 int file_index, ist_index;
789 int data_size, got_picture;
791 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
794 /* if 'q' pressed, exits */
795 if (read_key() == 'q')
798 /* select the input file with the smallest pts */
801 for(i=0;i<nb_istreams;i++) {
803 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
805 file_index = ist->file_index;
808 /* if none, if is finished */
811 /* finish if recording time exhausted */
812 if (recording_time > 0 && min_pts >= recording_time)
814 /* read a packet from it and output it in the fifo */
815 is = input_files[file_index];
816 if (av_read_packet(is, &pkt) < 0) {
817 file_table[file_index].eof_reached = 1;
820 ist_index = file_table[file_index].ist_index + pkt.stream_index;
821 ist = ist_table[ist_index];
827 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
828 hex_dump(pkt.data, pkt.size);
831 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
837 /* decode the packet if needed */
838 data_buf = NULL; /* fail safe */
840 if (ist->decoding_needed) {
841 switch(ist->st->codec.codec_type) {
842 case CODEC_TYPE_AUDIO:
843 /* XXX: could avoid copy if PCM 16 bits with same
845 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
849 if (data_size == 0) {
855 data_buf = (UINT8 *)samples;
857 case CODEC_TYPE_VIDEO:
858 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
860 size = (ist->st->codec.width * ist->st->codec.height);
861 avpicture_fill(&picture, ptr,
862 ist->st->codec.pix_fmt,
863 ist->st->codec.width,
864 ist->st->codec.height);
867 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
868 ret = avcodec_decode_video(&ist->st->codec,
869 &picture, &got_picture, ptr, len);
872 fprintf(stderr, "Error while decoding stream #%d.%d\n",
873 ist->file_index, ist->index);
874 av_free_packet(&pkt);
894 switch(ist->st->codec.codec_type) {
895 case CODEC_TYPE_AUDIO:
896 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
897 ist->sample_index += data_size / (2 * ist->st->codec.channels);
899 case CODEC_TYPE_VIDEO:
901 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
902 ist->st->codec.frame_rate;
908 /* transcode raw format, encode packets and output them */
910 for(i=0;i<nb_ostreams;i++) {
912 if (ost->source_index == ist_index) {
913 os = output_files[ost->file_index];
915 if (ost->encoding_needed) {
916 switch(ost->st->codec.codec_type) {
917 case CODEC_TYPE_AUDIO:
918 do_audio_out(os, ost, ist, data_buf, data_size);
920 case CODEC_TYPE_VIDEO:
921 do_video_out(os, ost, ist, &picture);
925 /* no reencoding needed : output the packet directly */
926 os->format->write_packet(os, ost->index, data_buf, data_size);
931 av_free_packet(&pkt);
933 /* dump report by using the first video and audio streams */
937 INT64 total_size, ti;
939 int frame_number, vid;
941 static INT64 last_time;
943 if ((min_pts - last_time) >= 500000) {
946 oc = output_files[0];
948 total_size = url_ftell(&oc->pb);
953 for(i=0;i<nb_ostreams;i++) {
955 enc = &ost->st->codec;
956 ist = ist_table[ost->source_index];
957 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
958 frame_number = ist->frame_number;
959 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
960 frame_number, enc->quality);
963 /* compute min pts value */
964 if (!ist->discard && ist->pts < ti) {
969 ti1 = (double)ti / 1000000.0;
972 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
974 sprintf(buf + strlen(buf),
975 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
976 (double)total_size / 1024, ti1, bitrate);
978 fprintf(stderr, "%s \r", buf);
985 /* dump report by using the first video and audio streams */
989 INT64 total_size, ti;
991 int frame_number, vid;
994 oc = output_files[0];
996 total_size = url_ftell(&oc->pb);
1001 for(i=0;i<nb_ostreams;i++) {
1003 enc = &ost->st->codec;
1004 ist = ist_table[ost->source_index];
1005 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1006 frame_number = ist->frame_number;
1007 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1008 frame_number, enc->quality);
1011 /* compute min pts value */
1012 if (!ist->discard && ist->pts < ti) {
1017 ti1 = ti / 1000000.0;
1020 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1022 sprintf(buf + strlen(buf),
1023 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1024 (double)total_size / 1024, ti1, bitrate);
1026 fprintf(stderr, "%s \n", buf);
1028 /* close each encoder */
1029 for(i=0;i<nb_ostreams;i++) {
1031 if (ost->encoding_needed) {
1032 avcodec_close(&ost->st->codec);
1036 /* close each decoder */
1037 for(i=0;i<nb_istreams;i++) {
1039 if (ist->decoding_needed) {
1040 avcodec_close(&ist->st->codec);
1045 /* write the trailer if needed and close file */
1046 for(i=0;i<nb_output_files;i++) {
1047 os = output_files[i];
1048 os->format->write_trailer(os);
1057 for(i=0;i<nb_istreams;i++) {
1066 for(i=0;i<nb_ostreams;i++) {
1069 if (ost->pict_tmp.data[0])
1070 free(ost->pict_tmp.data[0]);
1071 if (ost->video_resample)
1072 img_resample_close(ost->img_resample_ctx);
1073 if (ost->audio_resample)
1074 audio_resample_close(ost->resample);
1087 int file_read(const char *filename)
1090 unsigned char buffer[1024];
1093 if (url_open(&h, filename, O_RDONLY) < 0) {
1094 printf("could not open '%s'\n", filename);
1098 len = url_read(h, buffer, sizeof(buffer));
1101 for(i=0;i<len;i++) putchar(buffer[i]);
1108 void show_licence(void)
1111 "ffmpeg version " FFMPEG_VERSION "\n"
1112 "Copyright (c) 2000,2001 Gerard Lantau\n"
1113 "This program is free software; you can redistribute it and/or modify\n"
1114 "it under the terms of the GNU General Public License as published by\n"
1115 "the Free Software Foundation; either version 2 of the License, or\n"
1116 "(at your option) any later version.\n"
1118 "This program is distributed in the hope that it will be useful,\n"
1119 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1120 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1121 "GNU General Public License for more details.\n"
1123 "You should have received a copy of the GNU General Public License\n"
1124 "along with this program; if not, write to the Free Software\n"
1125 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1130 void opt_format(const char *arg)
1134 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1136 fprintf(stderr, "Invalid format: %s\n", arg);
1142 void opt_video_bitrate(const char *arg)
1144 video_bit_rate = atoi(arg) * 1000;
1147 void opt_frame_rate(const char *arg)
1149 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1152 void opt_frame_size(const char *arg)
1154 parse_image_size(&frame_width, &frame_height, arg);
1155 if (frame_width <= 0 || frame_height <= 0) {
1156 fprintf(stderr, "Incorrect frame size\n");
1159 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1160 fprintf(stderr, "Frame size must be a multiple of 2\n");
1165 void opt_gop_size(const char *arg)
1167 gop_size = atoi(arg);
1170 void opt_qscale(const char *arg)
1172 video_qscale = atoi(arg);
1173 if (video_qscale < 0 ||
1174 video_qscale > 31) {
1175 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1181 void opt_audio_bitrate(const char *arg)
1183 audio_bit_rate = atoi(arg) * 1000;
1186 void opt_audio_rate(const char *arg)
1188 audio_sample_rate = atoi(arg);
1191 void opt_audio_channels(const char *arg)
1193 audio_channels = atoi(arg);
1197 void opt_video_device(const char *arg)
1199 v4l_device = strdup(arg);
1202 void opt_audio_device(const char *arg)
1204 audio_device = strdup(arg);
1208 void opt_audio_codec(const char *arg)
1214 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1219 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1222 audio_codec_id = p->id;
1226 const char *motion_str[] = {
1234 void opt_motion_estimation(const char *arg)
1240 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1243 if (!strcmp(*p, arg))
1247 motion_estimation_method = p - motion_str;
1250 void opt_video_codec(const char *arg)
1256 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1261 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1264 video_codec_id = p->id;
1268 void opt_map(const char *arg)
1274 m = &stream_maps[nb_stream_maps++];
1276 m->file_index = strtol(arg, (char **)&p, 0);
1279 m->stream_index = strtol(arg, (char **)&p, 0);
1282 void opt_recording_time(const char *arg)
1284 recording_time = parse_date(arg, 1);
1287 /* return the number of packet read to find the codec parameters */
1288 int find_codec_parameters(AVFormatContext *ic)
1290 int val, i, count, ret, got_picture, size;
1292 AVCodecContext *enc;
1296 AVPacketList *pktl, **ppktl;
1297 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1301 ppktl = &ic->packet_buffer;
1303 for(i=0;i<ic->nb_streams;i++) {
1304 enc = &ic->streams[i]->codec;
1306 switch(enc->codec_type) {
1307 case CODEC_TYPE_AUDIO:
1308 val = enc->sample_rate;
1310 case CODEC_TYPE_VIDEO:
1317 /* if no parameters supplied, then we should read it from
1322 if (i == ic->nb_streams) {
1328 /* open each codec */
1329 for(i=0;i<ic->nb_streams;i++) {
1330 st = ic->streams[i];
1331 codec = avcodec_find_decoder(st->codec.codec_id);
1332 if (codec == NULL) {
1336 avcodec_open(&st->codec, codec);
1339 pktl = av_mallocz(sizeof(AVPacketList));
1345 /* add the packet in the buffered packet list */
1347 ppktl = &pktl->next;
1350 if (ic->format->read_packet(ic, pkt) < 0) {
1354 st = ic->streams[pkt->stream_index];
1356 /* decode the data and update codec parameters */
1360 switch(st->codec.codec_type) {
1361 case CODEC_TYPE_VIDEO:
1362 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1364 case CODEC_TYPE_AUDIO:
1365 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1385 /* close each codec */
1386 for(i=0;i<ic->nb_streams;i++) {
1387 st = ic->streams[i];
1388 avcodec_close(&st->codec);
1394 int filename_number_test(const char *filename)
1398 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1399 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1400 "Use '%%d' to specify the image number:\n"
1401 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1402 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1410 void opt_input_file(const char *filename)
1412 AVFormatContext *ic;
1413 AVFormatParameters params, *ap = ¶ms;
1414 URLFormat url_format;
1418 ic = av_mallocz(sizeof(AVFormatContext));
1419 strcpy(ic->filename, filename);
1420 /* first format guess to know if we must open file */
1423 fmt = guess_format(NULL, filename, NULL);
1425 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1427 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1428 fprintf(stderr, "Could not open '%s'\n", filename);
1432 /* find format and set default parameters */
1434 err = url_getformat(url_fileno(&ic->pb), &url_format);
1437 fmt = guess_format(url_format.format_name, NULL, NULL);
1438 ap->sample_rate = url_format.sample_rate;
1439 ap->frame_rate = url_format.frame_rate;
1440 ap->channels = url_format.channels;
1441 ap->width = url_format.width;
1442 ap->height = url_format.height;
1443 ap->pix_fmt = url_format.pix_fmt;
1446 fmt = guess_format(NULL, filename, NULL);
1447 memset(ap, 0, sizeof(*ap));
1450 memset(ap, 0, sizeof(*ap));
1453 if (!fmt || !fmt->read_header) {
1454 fprintf(stderr, "%s: Unknown file format\n", filename);
1459 /* get default parameters from command line */
1460 if (!ap->sample_rate)
1461 ap->sample_rate = audio_sample_rate;
1463 ap->channels = audio_channels;
1465 if (!ap->frame_rate)
1466 ap->frame_rate = frame_rate;
1468 ap->width = frame_width;
1470 ap->height = frame_height;
1472 /* check filename in case of an image number is expected */
1473 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1474 if (filename_number_test(ic->filename) < 0)
1478 err = ic->format->read_header(ic, ap);
1480 fprintf(stderr, "%s: Error while parsing header\n", filename);
1484 /* If not enough info for the codecs, we decode the first frames
1485 to get it. (used in mpeg case for example) */
1486 ret = find_codec_parameters(ic);
1488 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1492 /* update the current parameters so that they match the one of the input stream */
1493 for(i=0;i<ic->nb_streams;i++) {
1494 AVCodecContext *enc = &ic->streams[i]->codec;
1495 switch(enc->codec_type) {
1496 case CODEC_TYPE_AUDIO:
1497 audio_channels = enc->channels;
1498 audio_sample_rate = enc->sample_rate;
1500 case CODEC_TYPE_VIDEO:
1501 frame_height = enc->height;
1502 frame_width = enc->width;
1503 frame_rate = enc->frame_rate;
1508 input_files[nb_input_files] = ic;
1509 /* dump the file content */
1510 dump_format(ic, nb_input_files, filename, 0);
1515 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1517 int has_video, has_audio, i, j;
1518 AVFormatContext *ic;
1522 for(j=0;j<nb_input_files;j++) {
1523 ic = input_files[j];
1524 for(i=0;i<ic->nb_streams;i++) {
1525 AVCodecContext *enc = &ic->streams[i]->codec;
1526 switch(enc->codec_type) {
1527 case CODEC_TYPE_AUDIO:
1530 case CODEC_TYPE_VIDEO:
1536 *has_video_ptr = has_video;
1537 *has_audio_ptr = has_audio;
1540 void opt_output_file(const char *filename)
1543 AVFormatContext *oc;
1544 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1547 if (!strcmp(filename, "-"))
1550 oc = av_mallocz(sizeof(AVFormatContext));
1553 file_format = guess_format(NULL, filename, NULL);
1555 file_format = &mpeg_mux_format;
1558 oc->format = file_format;
1560 if (!strcmp(file_format->name, "ffm") &&
1561 strstart(filename, "http:", NULL)) {
1562 /* special case for files sent to ffserver: we get the stream
1563 parameters from ffserver */
1564 if (read_ffserver_streams(oc, filename) < 0) {
1565 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1569 use_video = file_format->video_codec != CODEC_ID_NONE;
1570 use_audio = file_format->audio_codec != CODEC_ID_NONE;
1572 /* disable if no corresponding type found and at least one
1574 if (nb_input_files > 0) {
1575 check_audio_video_inputs(&input_has_video, &input_has_audio);
1576 if (!input_has_video)
1578 if (!input_has_audio)
1582 /* manual disable */
1583 if (audio_disable) {
1586 if (video_disable) {
1592 AVCodecContext *video_enc;
1594 st = av_mallocz(sizeof(AVStream));
1596 fprintf(stderr, "Could not alloc stream\n");
1599 video_enc = &st->codec;
1601 codec_id = file_format->video_codec;
1602 if (video_codec_id != CODEC_ID_NONE)
1603 codec_id = video_codec_id;
1605 video_enc->codec_id = codec_id;
1606 video_enc->codec_type = CODEC_TYPE_VIDEO;
1608 video_enc->bit_rate = video_bit_rate;
1609 video_enc->frame_rate = frame_rate;
1611 video_enc->width = frame_width;
1612 video_enc->height = frame_height;
1614 video_enc->gop_size = gop_size;
1616 video_enc->gop_size = 0;
1617 if (video_qscale || same_quality) {
1618 video_enc->flags |= CODEC_FLAG_QSCALE;
1619 video_enc->quality = video_qscale;
1621 /* XXX: need to find a way to set codec parameters */
1622 if (oc->format == &ppm_format ||
1623 oc->format == &ppmpipe_format) {
1624 video_enc->pix_fmt = PIX_FMT_RGB24;
1627 oc->streams[nb_streams] = st;
1632 AVCodecContext *audio_enc;
1634 st = av_mallocz(sizeof(AVStream));
1636 fprintf(stderr, "Could not alloc stream\n");
1639 audio_enc = &st->codec;
1640 codec_id = file_format->audio_codec;
1641 if (audio_codec_id != CODEC_ID_NONE)
1642 codec_id = audio_codec_id;
1643 audio_enc->codec_id = codec_id;
1644 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1646 audio_enc->bit_rate = audio_bit_rate;
1647 audio_enc->sample_rate = audio_sample_rate;
1648 audio_enc->channels = audio_channels;
1649 oc->streams[nb_streams] = st;
1653 oc->nb_streams = nb_streams;
1656 fprintf(stderr, "No audio or video streams available\n");
1661 nstrcpy(oc->title, sizeof(oc->title), str_title);
1663 nstrcpy(oc->author, sizeof(oc->author), str_author);
1665 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1667 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1670 output_files[nb_output_files] = oc;
1671 /* dump the file content */
1672 dump_format(oc, nb_output_files, filename, 1);
1675 strcpy(oc->filename, filename);
1677 /* check filename in case of an image number is expected */
1678 if (oc->format->flags & AVFMT_NEEDNUMBER) {
1679 if (filename_number_test(oc->filename) < 0)
1683 if (!(oc->format->flags & AVFMT_NOFILE)) {
1684 /* test if it already exists to avoid loosing precious files */
1685 if (!file_overwrite &&
1686 (strchr(filename, ':') == NULL ||
1687 strstart(filename, "file:", NULL))) {
1688 if (url_exist(filename)) {
1691 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1694 if (toupper(c) != 'Y') {
1695 fprintf(stderr, "Not overwriting - exiting\n");
1702 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1703 fprintf(stderr, "Could not open '%s'\n", filename);
1708 /* reset some options */
1712 audio_codec_id = CODEC_ID_NONE;
1713 video_codec_id = CODEC_ID_NONE;
1718 /* prepare dummy protocols for grab */
1719 void prepare_grab(void)
1721 int has_video, has_audio, i, j;
1722 AVFormatContext *oc;
1723 AVFormatContext *ic;
1724 AVFormatParameters ap1, *ap = &ap1;
1726 /* see if audio/video inputs are needed */
1729 memset(ap, 0, sizeof(*ap));
1730 for(j=0;j<nb_output_files;j++) {
1731 oc = output_files[j];
1732 for(i=0;i<oc->nb_streams;i++) {
1733 AVCodecContext *enc = &oc->streams[i]->codec;
1734 switch(enc->codec_type) {
1735 case CODEC_TYPE_AUDIO:
1736 if (enc->sample_rate > ap->sample_rate)
1737 ap->sample_rate = enc->sample_rate;
1738 if (enc->channels > ap->channels)
1739 ap->channels = enc->channels;
1742 case CODEC_TYPE_VIDEO:
1743 if (enc->width > ap->width)
1744 ap->width = enc->width;
1745 if (enc->height > ap->height)
1746 ap->height = enc->height;
1747 if (enc->frame_rate > ap->frame_rate)
1748 ap->frame_rate = enc->frame_rate;
1755 if (has_video == 0 && has_audio == 0) {
1756 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1761 ic = av_open_input_file("", "video_grab_device", 0, ap);
1763 fprintf(stderr, "Could not open video grab device\n");
1766 input_files[nb_input_files] = ic;
1767 dump_format(ic, nb_input_files, v4l_device, 0);
1771 ic = av_open_input_file("", "audio_device", 0, ap);
1773 fprintf(stderr, "Could not open audio grab device\n");
1776 input_files[nb_input_files] = ic;
1777 dump_format(ic, nb_input_files, audio_device, 0);
1784 void prepare_grab(void)
1786 fprintf(stderr, "Must supply at least one input file\n");
1792 /* open the necessary output devices for playing */
1793 void prepare_play(void)
1795 file_format = guess_format("audio_device", NULL, NULL);
1797 fprintf(stderr, "Could not find audio device\n");
1801 opt_output_file(audio_device);
1805 #ifndef CONFIG_WIN32
1806 INT64 getutime(void)
1808 struct rusage rusage;
1810 getrusage(RUSAGE_SELF, &rusage);
1811 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
1814 INT64 getutime(void)
1820 void show_formats(void)
1827 printf("File formats:\n");
1828 printf(" Encoding:");
1829 for(f = first_format; f != NULL; f = f->next) {
1830 if (f->write_header)
1831 printf(" %s", f->name);
1834 printf(" Decoding:");
1835 for(f = first_format; f != NULL; f = f->next) {
1837 printf(" %s", f->name);
1841 printf("Codecs:\n");
1842 printf(" Encoders:");
1843 for(p = first_avcodec; p != NULL; p = p->next) {
1845 printf(" %s", p->name);
1849 printf(" Decoders:");
1850 for(p = first_avcodec; p != NULL; p = p->next) {
1852 printf(" %s", p->name);
1856 printf("Supported file protocols:");
1857 for(up = first_protocol; up != NULL; up = up->next)
1858 printf(" %s:", up->name);
1861 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
1862 printf("Motion estimation methods:");
1866 if ((pp - motion_str) == ME_ZERO)
1867 printf("(fastest)");
1868 else if ((pp - motion_str) == ME_FULL)
1869 printf("(slowest)");
1870 else if ((pp - motion_str) == ME_LOG)
1871 printf("(default)");
1878 void show_help(void)
1881 const OptionDef *po;
1884 prog = do_play ? "ffplay" : "ffmpeg";
1886 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001 Gerard Lantau\n",
1890 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
1891 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
1893 printf("usage: ffplay [options] input_file...\n"
1894 "Simple audio player\n");
1898 "Main options are:\n");
1901 printf("\nAdvanced options are:\n");
1902 for(po = options; po->name != NULL; po++) {
1904 expert = (po->flags & OPT_EXPERT) != 0;
1906 strcpy(buf, po->name);
1907 if (po->flags & HAS_ARG) {
1909 strcat(buf, po->argname);
1911 printf("-%-17s %s\n", buf, po->help);
1919 const OptionDef options[] = {
1920 { "L", 0, {(void*)show_licence}, "show license" },
1921 { "h", 0, {(void*)show_help}, "show help" },
1922 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
1923 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
1924 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
1925 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
1926 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
1927 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
1928 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
1929 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
1930 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
1931 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
1933 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
1934 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
1935 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
1936 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
1937 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
1938 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
1939 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
1941 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
1943 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
1944 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
1946 { "sameq", OPT_BOOL, {(void*)&same_quality},
1947 "use same video quality as source (implies VBR)" },
1949 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
1950 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
1951 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
1952 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
1954 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
1956 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
1957 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
1958 "deinterlace pictures" },
1959 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
1960 "add timings for benchmarking" },
1961 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
1962 "dump each input packet" },
1967 int main(int argc, char **argv)
1970 const char *opt, *arg;
1971 const OptionDef *po;
1976 /* detect if invoked as player */
1977 i = strlen(argv[0]);
1978 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
1986 while (optindex < argc) {
1987 opt = argv[optindex++];
1989 if (opt[0] == '-' && opt[1] != '\0') {
1991 while (po->name != NULL) {
1992 if (!strcmp(opt + 1, po->name))
1997 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2001 if (po->flags & HAS_ARG)
2002 arg = argv[optindex++];
2003 if (po->flags & OPT_STRING) {
2006 *po->u.str_arg = str;
2007 } else if (po->flags & OPT_BOOL) {
2010 po->u.func_arg(arg);
2014 opt_output_file(opt);
2016 opt_input_file(opt);
2023 /* file converter / grab */
2024 if (nb_output_files <= 0) {
2025 fprintf(stderr, "Must supply at least one output file\n");
2029 if (nb_input_files == 0) {
2034 if (nb_input_files <= 0) {
2035 fprintf(stderr, "Must supply at least one input file\n");
2042 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2043 stream_maps, nb_stream_maps);
2044 ti = getutime() - ti;
2046 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2050 for(i=0;i<nb_output_files;i++) {
2051 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2052 url_fclose(&output_files[i]->pb);
2054 for(i=0;i<nb_input_files;i++) {
2055 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2056 url_fclose(&input_files[i]->pb);