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;
101 typedef struct AVOutputStream {
102 int file_index; /* file index */
103 int index; /* stream index in the output file */
104 int source_index; /* AVInputStream index */
105 AVStream *st; /* stream in the output file */
106 int encoding_needed; /* true if encoding needed for this stream */
108 int fifo_packet_rptr; /* read index in the corresponding
109 avinputstream packet fifo */
111 AVPicture pict_tmp; /* temporary image for resizing */
113 ImgReSampleContext *img_resample_ctx; /* for image resampling */
117 ReSampleContext *resample; /* for audio resampling */
118 FifoBuffer fifo; /* for compression: one audio fifo per codec */
121 typedef struct AVInputStream {
125 int discard; /* true if stream data should be discarded */
126 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
127 INT64 pts; /* current pts */
128 int frame_number; /* current frame */
129 INT64 sample_index; /* current sample */
132 typedef struct AVInputFile {
133 int eof_reached; /* true if eof reached */
134 int ist_index; /* index of first stream in ist_table */
135 int buffer_size; /* current total buffer size */
136 int buffer_size_max; /* buffer size at which we consider we can stop
142 /* init terminal so that we can grab keys */
143 static struct termios oldtty;
145 static void term_exit(void)
147 tcsetattr (0, TCSANOW, &oldtty);
150 static void term_init(void)
157 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
158 |INLCR|IGNCR|ICRNL|IXON);
159 tty.c_oflag |= OPOST;
160 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
161 tty.c_cflag &= ~(CSIZE|PARENB);
166 tcsetattr (0, TCSANOW, &tty);
171 /* read a key without blocking */
172 static int read_key(void)
183 n = select(1, &rfds, NULL, NULL, &tv);
185 if (read(0, &ch, 1) == 1)
191 #define AUDIO_FIFO_SIZE 8192
193 /* main loop for grabbing */
194 int av_grab(AVFormatContext *s)
196 UINT8 audio_buf[AUDIO_FIFO_SIZE];
197 UINT8 audio_buf1[AUDIO_FIFO_SIZE];
198 UINT8 audio_out[AUDIO_FIFO_SIZE];
199 UINT8 video_buffer[1024*1024];
202 URLContext *audio_handle = NULL, *video_handle = NULL;
204 AVCodecContext *enc, *first_video_enc = NULL;
205 int frame_size, frame_bytes;
206 int use_audio, use_video;
207 int frame_rate, sample_rate, channels;
208 int width, height, frame_number, i, pix_fmt = 0;
209 AVOutputStream *ost_table[s->nb_streams], *ost;
210 UINT8 *picture_in_buf = NULL, *picture_420p = NULL;
211 int audio_fifo_size = 0, picture_size = 0;
214 /* init output stream info */
215 for(i=0;i<s->nb_streams;i++)
218 /* output stream init */
219 for(i=0;i<s->nb_streams;i++) {
220 ost = av_mallocz(sizeof(AVOutputStream));
224 ost->st = s->streams[i];
238 for(i=0;i<s->nb_streams;i++) {
242 enc = &ost->st->codec;
243 codec = avcodec_find_encoder(enc->codec_id);
245 fprintf(stderr, "Unknown codec\n");
248 if (avcodec_open(enc, codec) < 0) {
249 fprintf(stderr, "Incorrect encode parameters\n");
252 switch(enc->codec_type) {
253 case CODEC_TYPE_AUDIO:
255 if (enc->sample_rate > sample_rate)
256 sample_rate = enc->sample_rate;
257 if (enc->frame_size > frame_size)
258 frame_size = enc->frame_size;
259 if (enc->channels > channels)
260 channels = enc->channels;
262 case CODEC_TYPE_VIDEO:
263 if (!first_video_enc)
264 first_video_enc = enc;
266 if (enc->frame_rate > frame_rate)
267 frame_rate = enc->frame_rate;
268 if (enc->width > width)
270 if (enc->height > height)
271 height = enc->height;
279 snprintf(buf, sizeof(buf), "audio:%d,%d", sample_rate, channels);
280 ret = url_open(&audio_handle, buf, URL_RDONLY);
282 fprintf(stderr, "Could not open audio device: disabling audio capture\n");
286 /* read back exact grab parameters */
287 if (url_getformat(audio_handle, &f) < 0) {
288 fprintf(stderr, "could not read back video grab parameters\n");
291 sample_rate = f.sample_rate;
292 channels = f.channels;
293 audio_fifo_size = ((AUDIO_FIFO_SIZE / 2) / audio_handle->packet_size) *
294 audio_handle->packet_size;
295 fprintf(stderr, "Audio sampling: %d Hz, %s\n",
296 sample_rate, channels == 2 ? "stereo" : "mono");
302 snprintf(buf, sizeof(buf), "video:%d,%d,%f",
303 width, height, (float)frame_rate / FRAME_RATE_BASE);
305 ret = url_open(&video_handle, buf, URL_RDONLY);
307 fprintf(stderr,"Could not init video 4 linux capture: disabling video capture\n");
311 const char *pix_fmt_str;
312 /* read back exact grab parameters */
313 if (url_getformat(video_handle, &f) < 0) {
314 fprintf(stderr, "could not read back video grab parameters\n");
321 case PIX_FMT_YUV420P:
322 pix_fmt_str = "420P";
328 pix_fmt_str = "RGB24";
331 pix_fmt_str = "BGR24";
337 picture_size = video_handle->packet_size;
338 picture_in_buf = malloc(picture_size);
341 /* allocate a temporary picture if not grabbing in 420P format */
342 if (pix_fmt != PIX_FMT_YUV420P) {
343 picture_420p = malloc((width * height * 3) / 2);
345 fprintf(stderr, "Video sampling: %dx%d, %s format, %0.2f fps\n",
346 width, height, pix_fmt_str, (float)frame_rate / FRAME_RATE_BASE);
350 if (!use_video && !use_audio) {
351 fprintf(stderr,"Could not open grab devices : exiting\n");
355 /* init built in conversion functions */
356 for(i=0;i<s->nb_streams;i++) {
358 enc = &ost->st->codec;
359 switch(enc->codec_type) {
360 case CODEC_TYPE_AUDIO:
361 ost->audio_resample = 0;
362 if ((enc->channels != channels ||
363 enc->sample_rate != sample_rate)) {
364 ost->audio_resample = 1;
365 ost->resample = audio_resample_init(enc->channels, channels,
366 enc->sample_rate, sample_rate);
368 if (fifo_init(&ost->fifo, (2 * audio_fifo_size * enc->sample_rate *
369 enc->channels) / sample_rate))
372 case CODEC_TYPE_VIDEO:
373 ost->video_resample = 0;
374 if (enc->width != width ||
375 enc->height != height) {
377 ost->video_resample = 1;
378 buf = malloc((enc->width * enc->height * 3) / 2);
381 ost->pict_tmp.data[0] = buf;
382 ost->pict_tmp.data[1] = buf + enc->width * height;
383 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (enc->width * height) / 4;
384 ost->pict_tmp.linesize[0] = enc->width;
385 ost->pict_tmp.linesize[1] = enc->width / 2;
386 ost->pict_tmp.linesize[2] = enc->width / 2;
387 ost->img_resample_ctx = img_resample_init(
388 ost->st->codec.width, ost->st->codec.height,
394 fprintf(stderr, "Press [q] to stop encoding\n");
396 s->format->write_header(s);
397 time_start = gettime();
401 /* if 'q' pressed, exits */
402 if (read_key() == 'q')
405 /* read & compress audio frames */
407 int ret, nb_samples, nb_samples_out;
411 ret = url_read(audio_handle, audio_buf, audio_fifo_size);
414 /* fill each codec fifo by doing the right sample
415 rate conversion. This is not optimal because we
416 do too much work, but it is easy to do */
417 nb_samples = ret / (channels * 2);
418 for(i=0;i<s->nb_streams;i++) {
420 enc = &ost->st->codec;
421 if (enc->codec_type == CODEC_TYPE_AUDIO) {
422 /* rate & stereo convertion */
423 if (!ost->audio_resample) {
425 nb_samples_out = nb_samples;
428 nb_samples_out = audio_resample(ost->resample,
429 (short *)buftmp, (short *)audio_buf,
432 fifo_write(&ost->fifo, buftmp, nb_samples_out * enc->channels * 2,
437 /* compress as many frame as possible with each audio codec */
438 for(i=0;i<s->nb_streams;i++) {
440 enc = &ost->st->codec;
441 if (enc->codec_type == CODEC_TYPE_AUDIO) {
442 frame_bytes = enc->frame_size * 2 * enc->channels;
444 while (fifo_read(&ost->fifo, audio_buf,
445 frame_bytes, &ost->fifo.rptr) == 0) {
446 ret = avcodec_encode_audio(enc,
447 audio_out, sizeof(audio_out),
449 s->format->write_packet(s, ost->index, audio_out, ret);
457 AVPicture *picture1, *picture2, *picture;
458 AVPicture picture_tmp0, picture_tmp1;
460 ret = url_read(video_handle, picture_in_buf, picture_size);
464 picture2 = &picture_tmp0;
465 avpicture_fill(picture2, picture_in_buf, pix_fmt, width, height);
467 if (pix_fmt != PIX_FMT_YUV420P) {
468 picture = &picture_tmp1;
469 avpicture_fill(picture, picture_420p,
470 PIX_FMT_YUV420P, width, height);
471 img_convert(picture, PIX_FMT_YUV420P,
478 for(i=0;i<s->nb_streams;i++) {
480 enc = &ost->st->codec;
481 if (enc->codec_type == CODEC_TYPE_VIDEO) {
484 /* feed each codec with its requested frame rate */
485 n1 = ((INT64)frame_number * enc->frame_rate) / frame_rate;
486 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / frame_rate;
489 /* resize the picture if needed */
490 if (ost->video_resample) {
491 picture1 = &ost->pict_tmp;
492 img_resample(ost->img_resample_ctx,
497 ret = avcodec_encode_video(enc, video_buffer,
498 sizeof(video_buffer),
500 s->format->write_packet(s, ost->index, video_buffer, ret);
512 static float last_ti;
515 total_size = url_ftell(&s->pb);
516 ti1 = gettime() - time_start;
517 /* check elapsed time */
518 if (recording_time && ti1 >= recording_time)
521 ti = ti1 / 1000000.0;
524 /* dispaly twice per second */
525 if ((ti - last_ti) >= 0.5) {
527 bitrate = (int)((total_size * 8) / ti / 1000.0);
531 sprintf(buf + strlen(buf), "frame=%5d fps=%4.1f q=%2d ",
532 frame_number, (float)frame_number / ti, first_video_enc->quality);
535 sprintf(buf + strlen(buf), "size=%8LdkB time=%0.1f bitrate=%6.1fkbits/s",
536 total_size / 1024, ti, bitrate);
537 fprintf(stderr, "%s \r", buf);
544 for(i=0;i<s->nb_streams;i++) {
546 enc = &ost->st->codec;
549 s->format->write_trailer(s);
552 url_close(audio_handle);
555 url_close(video_handle);
562 total_size = url_ftell(&s->pb);
564 ti = (gettime() - time_start) / 1000000.0;
567 bitrate = (int)((total_size * 8) / ti / 1000.0);
569 fprintf(stderr, "\033[K\nTotal time = %0.1f s, %Ld KBytes, %0.1f kbits/s\n",
570 ti, total_size / 1024, bitrate);
572 fprintf(stderr, "Total frames = %d\n", frame_number);
579 free(picture_in_buf);
582 for(i=0;i<s->nb_streams;i++) {
585 if (ost->fifo.buffer)
586 fifo_free(&ost->fifo);
587 if (ost->pict_tmp.data[0])
588 free(ost->pict_tmp.data[0]);
589 if (ost->video_resample)
590 img_resample_close(ost->img_resample_ctx);
591 if (ost->audio_resample)
592 audio_resample_close(ost->resample);
602 #endif /* CONFIG_WIN32 */
604 int read_ffserver_streams(AVFormatContext *s, const char *filename)
609 ic = av_open_input_file(filename, FFM_PACKET_SIZE);
612 /* copy stream format */
613 s->nb_streams = ic->nb_streams;
614 for(i=0;i<ic->nb_streams;i++) {
616 st = av_mallocz(sizeof(AVFormatContext));
617 memcpy(st, ic->streams[i], sizeof(AVStream));
621 av_close_input_file(ic);
625 #define MAX_AUDIO_PACKET_SIZE 16384
627 static void do_audio_out(AVFormatContext *s,
630 unsigned char *buf, int size)
633 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
634 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
635 int size_out, frame_bytes, ret;
638 enc = &ost->st->codec;
640 if (ost->audio_resample) {
642 size_out = audio_resample(ost->resample,
643 (short *)buftmp, (short *)buf,
644 size / (ist->st->codec.channels * 2));
645 size_out = size_out * enc->channels * 2;
651 /* now encode as many frames as possible */
652 if (enc->frame_size > 1) {
653 /* output resampled raw samples */
654 fifo_write(&ost->fifo, buftmp, size_out,
657 frame_bytes = enc->frame_size * 2 * enc->channels;
659 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
660 &ost->fifo.rptr) == 0) {
661 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
663 s->format->write_packet(s, ost->index, audio_out, ret);
666 /* output a pcm frame */
667 /* XXX: change encoding codec API to avoid this ? */
668 switch(enc->codec->id) {
669 case CODEC_ID_PCM_S16LE:
670 case CODEC_ID_PCM_S16BE:
671 case CODEC_ID_PCM_U16LE:
672 case CODEC_ID_PCM_U16BE:
675 size_out = size_out >> 1;
678 ret = avcodec_encode_audio(enc, audio_out, size_out,
680 s->format->write_packet(s, ost->index, audio_out, ret);
684 /* write a picture to a raw mux */
685 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
686 int pix_fmt, int w, int h)
688 UINT8 *buf, *src, *dest;
691 size = avpicture_get_size(pix_fmt, w, h);
696 /* XXX: not efficient, should add test if we can take
697 directly the AVPicture */
699 case PIX_FMT_YUV420P:
706 src = picture->data[i];
708 memcpy(dest, src, w);
710 src += picture->linesize[i];
714 case PIX_FMT_YUV422P:
722 src = picture->data[i];
724 memcpy(dest, src, w);
726 src += picture->linesize[i];
730 case PIX_FMT_YUV444P:
735 src = picture->data[i];
737 memcpy(dest, src, w);
739 src += picture->linesize[i];
747 src = picture->data[0];
749 memcpy(dest, src, w * 2);
751 src += picture->linesize[0];
759 src = picture->data[0];
761 memcpy(dest, src, w * 3);
763 src += picture->linesize[0];
769 s->format->write_packet(s, index, buf, size);
774 static void do_video_out(AVFormatContext *s,
779 int n1, n2, nb, i, ret, frame_number;
780 AVPicture *picture, *picture2, *pict;
781 AVPicture picture_tmp1, picture_tmp2;
782 UINT8 video_buffer[1024*1024];
783 UINT8 *buf = NULL, *buf1 = NULL;
784 AVCodecContext *enc, *dec;
786 enc = &ost->st->codec;
787 dec = &ist->st->codec;
789 frame_number = ist->frame_number;
790 /* first drop frame if needed */
791 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
792 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
797 /* deinterlace : must be done before any resize */
798 if (do_deinterlace) {
801 /* create temporary picture */
802 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
807 picture2 = &picture_tmp2;
808 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
810 if (avpicture_deinterlace(picture2, picture1,
811 dec->pix_fmt, dec->width, dec->height) < 0) {
812 /* if error, do not deinterlace */
821 /* convert pixel format if needed */
822 if (enc->pix_fmt != dec->pix_fmt) {
825 /* create temporary picture */
826 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
830 pict = &picture_tmp1;
831 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
833 if (img_convert(pict, enc->pix_fmt,
834 picture2, dec->pix_fmt,
835 dec->width, dec->height) < 0) {
836 fprintf(stderr, "pixel format conversion not handled\n");
843 /* XXX: resampling could be done before raw format convertion in
844 some cases to go faster */
845 /* XXX: only works for YUV420P */
846 if (ost->video_resample) {
847 picture = &ost->pict_tmp;
848 img_resample(ost->img_resample_ctx, picture, pict);
853 /* duplicates frame if needed */
854 /* XXX: pb because no interleaving */
856 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
857 /* handles sameq here. This is not correct because it may
858 not be a global option */
860 enc->quality = dec->quality;
862 ret = avcodec_encode_video(enc,
863 video_buffer, sizeof(video_buffer),
865 s->format->write_packet(s, ost->index, video_buffer, ret);
867 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
877 static void hex_dump(UINT8 *buf, int size)
881 for(i=0;i<size;i+=16) {
888 printf(" %02x", buf[i+j]);
895 if (c < ' ' || c > '~')
904 * The following code is the main loop of the file converter
906 static int av_encode(AVFormatContext **output_files,
908 AVFormatContext **input_files,
910 AVStreamMap *stream_maps, int nb_stream_maps)
912 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
913 AVFormatContext *is, *os;
914 AVCodecContext *codec, *icodec;
915 AVOutputStream *ost, **ost_table = NULL;
916 AVInputStream *ist, **ist_table = NULL;
917 INT64 min_pts, start_time;
918 AVInputFile *file_table;
920 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
924 memset(file_table, 0, sizeof(file_table));
926 /* input stream init */
928 for(i=0;i<nb_input_files;i++) {
930 file_table[i].ist_index = j;
935 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
939 for(i=0;i<nb_istreams;i++) {
940 ist = av_mallocz(sizeof(AVInputStream));
946 for(i=0;i<nb_input_files;i++) {
948 for(k=0;k<is->nb_streams;k++) {
949 ist = ist_table[j++];
950 ist->st = is->streams[k];
953 ist->discard = 1; /* the stream is discarded by default
958 /* output stream init */
960 for(i=0;i<nb_output_files;i++) {
961 os = output_files[i];
962 nb_ostreams += os->nb_streams;
964 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
965 fprintf(stderr, "Number of stream maps must match number of output streams\n");
969 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
972 for(i=0;i<nb_ostreams;i++) {
973 ost = av_mallocz(sizeof(AVOutputStream));
980 for(k=0;k<nb_output_files;k++) {
981 os = output_files[k];
982 for(i=0;i<os->nb_streams;i++) {
984 ost = ost_table[n++];
987 ost->st = os->streams[i];
988 if (nb_stream_maps > 0) {
989 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
990 stream_maps[n-1].stream_index;
992 /* get corresponding input stream index : we select the first one with the right type */
994 for(j=0;j<nb_istreams;j++) {
997 ist->st->codec.codec_type == ost->st->codec.codec_type) {
998 ost->source_index = j;
1004 /* try again and reuse existing stream */
1005 for(j=0;j<nb_istreams;j++) {
1007 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
1008 ost->source_index = j;
1013 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1014 ost->file_index, ost->index);
1019 ist = ist_table[ost->source_index];
1024 /* dump the stream mapping */
1025 fprintf(stderr, "Stream mapping:\n");
1026 for(i=0;i<nb_ostreams;i++) {
1028 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1029 ist_table[ost->source_index]->file_index,
1030 ist_table[ost->source_index]->index,
1035 /* for each output stream, we compute the right encoding parameters */
1036 for(i=0;i<nb_ostreams;i++) {
1038 ist = ist_table[ost->source_index];
1040 codec = &ost->st->codec;
1041 icodec = &ist->st->codec;
1043 switch(codec->codec_type) {
1044 case CODEC_TYPE_AUDIO:
1045 /* check if same codec with same parameters. If so, no
1046 reencoding is needed */
1047 if (codec->codec_id == icodec->codec_id &&
1048 codec->bit_rate == icodec->bit_rate &&
1049 codec->sample_rate == icodec->sample_rate &&
1050 codec->channels == icodec->channels) {
1053 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1056 if (codec->channels == icodec->channels &&
1057 codec->sample_rate == icodec->sample_rate) {
1058 ost->audio_resample = 0;
1060 ost->audio_resample = 1;
1061 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1063 icodec->sample_rate);
1065 ist->decoding_needed = 1;
1066 ost->encoding_needed = 1;
1069 case CODEC_TYPE_VIDEO:
1070 /* check if same codec with same parameters. If so, no
1071 reencoding is needed */
1072 if (codec->codec_id == icodec->codec_id &&
1073 codec->bit_rate == icodec->bit_rate &&
1074 codec->frame_rate == icodec->frame_rate &&
1075 codec->width == icodec->width &&
1076 codec->height == icodec->height) {
1079 if (codec->width == icodec->width &&
1080 codec->height == icodec->height) {
1081 ost->video_resample = 0;
1084 ost->video_resample = 1;
1085 buf = malloc((codec->width * codec->height * 3) / 2);
1088 ost->pict_tmp.data[0] = buf;
1089 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
1090 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
1091 ost->pict_tmp.linesize[0] = codec->width;
1092 ost->pict_tmp.linesize[1] = codec->width / 2;
1093 ost->pict_tmp.linesize[2] = codec->width / 2;
1095 ost->img_resample_ctx = img_resample_init(
1096 ost->st->codec.width, ost->st->codec.height,
1097 ist->st->codec.width, ist->st->codec.height);
1099 ost->encoding_needed = 1;
1100 ist->decoding_needed = 1;
1106 /* open each encoder */
1107 for(i=0;i<nb_ostreams;i++) {
1109 if (ost->encoding_needed) {
1111 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1113 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1114 ost->file_index, ost->index);
1117 if (avcodec_open(&ost->st->codec, codec) < 0) {
1118 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1119 ost->file_index, ost->index);
1125 /* open each decoder */
1126 for(i=0;i<nb_istreams;i++) {
1128 if (ist->decoding_needed) {
1130 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1132 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
1133 ist->file_index, ist->index);
1136 if (avcodec_open(&ist->st->codec, codec) < 0) {
1137 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1138 ist->file_index, ist->index);
1145 for(i=0;i<nb_istreams;i++) {
1148 ist->frame_number = 0;
1151 /* compute buffer size max (should use a complete heuristic) */
1152 for(i=0;i<nb_input_files;i++) {
1153 file_table[i].buffer_size_max = 2048;
1156 /* open files and write file headers */
1157 for(i=0;i<nb_output_files;i++) {
1158 os = output_files[i];
1159 os->format->write_header(os);
1162 start_time = gettime();
1165 int file_index, ist_index;
1170 int data_size, got_picture;
1172 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1174 /* select the input file with the smallest pts */
1178 for(i=0;i<nb_istreams;i++) {
1180 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
1182 file_index = ist->file_index;
1185 /* if none, if is finished */
1188 /* finish if recording time exhausted */
1189 if (recording_time > 0 && min_pts >= recording_time)
1191 /* read a packet from it and output it in the fifo */
1193 is = input_files[file_index];
1194 if (av_read_packet(is, &pkt) < 0) {
1195 file_table[file_index].eof_reached = 1;
1198 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1199 ist = ist_table[ist_index];
1205 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1206 hex_dump(pkt.data, pkt.size);
1209 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1215 /* decode the packet if needed */
1216 data_buf = NULL; /* fail safe */
1218 if (ist->decoding_needed) {
1219 switch(ist->st->codec.codec_type) {
1220 case CODEC_TYPE_AUDIO:
1221 /* XXX: could avoid copy if PCM 16 bits with same
1222 endianness as CPU */
1223 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1227 if (data_size == 0) {
1228 /* no audio frame */
1233 data_buf = (UINT8 *)samples;
1235 case CODEC_TYPE_VIDEO:
1236 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1238 size = (ist->st->codec.width * ist->st->codec.height);
1239 avpicture_fill(&picture, ptr,
1240 ist->st->codec.pix_fmt,
1241 ist->st->codec.width,
1242 ist->st->codec.height);
1245 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1246 ret = avcodec_decode_video(&ist->st->codec,
1247 &picture, &got_picture, ptr, len);
1250 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1251 ist->file_index, ist->index);
1252 av_free_packet(&pkt);
1256 /* no picture yet */
1272 switch(ist->st->codec.codec_type) {
1273 case CODEC_TYPE_AUDIO:
1274 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1275 ist->sample_index += data_size / (2 * ist->st->codec.channels);
1277 case CODEC_TYPE_VIDEO:
1278 ist->frame_number++;
1279 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1280 ist->st->codec.frame_rate;
1286 /* transcode raw format, encode packets and output them */
1288 for(i=0;i<nb_ostreams;i++) {
1290 if (ost->source_index == ist_index) {
1291 os = output_files[ost->file_index];
1293 if (ost->encoding_needed) {
1294 switch(ost->st->codec.codec_type) {
1295 case CODEC_TYPE_AUDIO:
1296 do_audio_out(os, ost, ist, data_buf, data_size);
1298 case CODEC_TYPE_VIDEO:
1299 do_video_out(os, ost, ist, &picture);
1303 /* no reencoding needed : output the packet directly */
1304 os->format->write_packet(os, ost->index, data_buf, data_size);
1309 av_free_packet(&pkt);
1311 /* dump report by using the first video and audio streams */
1314 AVFormatContext *oc;
1315 INT64 total_size, ti;
1316 AVCodecContext *enc;
1317 int frame_number, vid;
1318 double bitrate, ti1;
1319 static INT64 last_time;
1321 if ((min_pts - last_time) >= 500000) {
1322 last_time = min_pts;
1324 oc = output_files[0];
1326 total_size = url_ftell(&oc->pb);
1331 for(i=0;i<nb_ostreams;i++) {
1333 enc = &ost->st->codec;
1334 ist = ist_table[ost->source_index];
1335 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1336 frame_number = ist->frame_number;
1337 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1338 frame_number, enc->quality);
1341 /* compute min pts value */
1342 if (!ist->discard && ist->pts < ti) {
1347 ti1 = (double)ti / 1000000.0;
1350 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1352 sprintf(buf + strlen(buf),
1353 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1354 (double)total_size / 1024, ti1, bitrate);
1356 fprintf(stderr, "%s \r", buf);
1362 /* dump report by using the first video and audio streams */
1365 AVFormatContext *oc;
1366 INT64 total_size, ti;
1367 AVCodecContext *enc;
1368 int frame_number, vid;
1369 double bitrate, ti1;
1371 oc = output_files[0];
1373 total_size = url_ftell(&oc->pb);
1378 for(i=0;i<nb_ostreams;i++) {
1380 enc = &ost->st->codec;
1381 ist = ist_table[ost->source_index];
1382 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1383 frame_number = ist->frame_number;
1384 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1385 frame_number, enc->quality);
1388 /* compute min pts value */
1389 if (!ist->discard && ist->pts < ti) {
1394 ti1 = ti / 1000000.0;
1397 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1399 sprintf(buf + strlen(buf),
1400 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1401 (double)total_size / 1024, ti1, bitrate);
1403 fprintf(stderr, "%s \n", buf);
1405 /* close each encoder */
1406 for(i=0;i<nb_ostreams;i++) {
1408 if (ost->encoding_needed) {
1409 avcodec_close(&ost->st->codec);
1413 /* close each decoder */
1414 for(i=0;i<nb_istreams;i++) {
1416 if (ist->decoding_needed) {
1417 avcodec_close(&ist->st->codec);
1422 /* write the trailer if needed and close file */
1423 for(i=0;i<nb_output_files;i++) {
1424 os = output_files[i];
1425 os->format->write_trailer(os);
1434 for(i=0;i<nb_istreams;i++) {
1443 for(i=0;i<nb_ostreams;i++) {
1446 if (ost->pict_tmp.data[0])
1447 free(ost->pict_tmp.data[0]);
1448 if (ost->video_resample)
1449 img_resample_close(ost->img_resample_ctx);
1450 if (ost->audio_resample)
1451 audio_resample_close(ost->resample);
1464 int file_read(const char *filename)
1467 unsigned char buffer[1024];
1470 if (url_open(&h, filename, O_RDONLY) < 0) {
1471 printf("could not open '%s'\n", filename);
1475 len = url_read(h, buffer, sizeof(buffer));
1478 for(i=0;i<len;i++) putchar(buffer[i]);
1485 void show_licence(void)
1488 "ffmpeg version " FFMPEG_VERSION "\n"
1489 "Copyright (c) 2000,2001 Gerard Lantau\n"
1490 "This program is free software; you can redistribute it and/or modify\n"
1491 "it under the terms of the GNU General Public License as published by\n"
1492 "the Free Software Foundation; either version 2 of the License, or\n"
1493 "(at your option) any later version.\n"
1495 "This program is distributed in the hope that it will be useful,\n"
1496 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1497 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1498 "GNU General Public License for more details.\n"
1500 "You should have received a copy of the GNU General Public License\n"
1501 "along with this program; if not, write to the Free Software\n"
1502 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1507 void opt_format(const char *arg)
1511 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1513 fprintf(stderr, "Invalid format: %s\n", arg);
1519 void opt_video_bitrate(const char *arg)
1521 video_bit_rate = atoi(arg) * 1000;
1524 void opt_frame_rate(const char *arg)
1526 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1529 void opt_frame_size(const char *arg)
1531 parse_image_size(&frame_width, &frame_height, arg);
1532 if (frame_width <= 0 || frame_height <= 0) {
1533 fprintf(stderr, "Incorrect frame size\n");
1536 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1537 fprintf(stderr, "Frame size must be a multiple of 2\n");
1542 void opt_gop_size(const char *arg)
1544 gop_size = atoi(arg);
1547 void opt_qscale(const char *arg)
1549 video_qscale = atoi(arg);
1550 if (video_qscale < 0 ||
1551 video_qscale > 31) {
1552 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1558 void opt_audio_bitrate(const char *arg)
1560 audio_bit_rate = atoi(arg) * 1000;
1563 void opt_audio_rate(const char *arg)
1565 audio_sample_rate = atoi(arg);
1568 void opt_audio_channels(const char *arg)
1570 audio_channels = atoi(arg);
1574 void opt_video_device(const char *arg)
1576 v4l_device = strdup(arg);
1579 void opt_audio_device(const char *arg)
1581 audio_device = strdup(arg);
1585 void opt_audio_codec(const char *arg)
1591 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1596 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1599 audio_codec_id = p->id;
1603 const char *motion_str[] = {
1611 void opt_motion_estimation(const char *arg)
1617 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1620 if (!strcmp(*p, arg))
1624 motion_estimation_method = p - motion_str;
1627 void opt_video_codec(const char *arg)
1633 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1638 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1641 video_codec_id = p->id;
1645 void opt_map(const char *arg)
1651 m = &stream_maps[nb_stream_maps++];
1653 m->file_index = strtol(arg, (char **)&p, 0);
1656 m->stream_index = strtol(arg, (char **)&p, 0);
1659 void opt_recording_time(const char *arg)
1661 recording_time = parse_date(arg, 1);
1664 /* return the number of packet read to find the codec parameters */
1665 int find_codec_parameters(AVFormatContext *ic)
1667 int val, i, count, ret, got_picture, size;
1669 AVCodecContext *enc;
1673 AVPacketList *pktl, **ppktl;
1674 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1678 ppktl = &ic->packet_buffer;
1680 for(i=0;i<ic->nb_streams;i++) {
1681 enc = &ic->streams[i]->codec;
1683 switch(enc->codec_type) {
1684 case CODEC_TYPE_AUDIO:
1685 val = enc->sample_rate;
1687 case CODEC_TYPE_VIDEO:
1694 /* if no parameters supplied, then we should read it from
1699 if (i == ic->nb_streams) {
1705 /* open each codec */
1706 for(i=0;i<ic->nb_streams;i++) {
1707 st = ic->streams[i];
1708 codec = avcodec_find_decoder(st->codec.codec_id);
1709 if (codec == NULL) {
1713 avcodec_open(&st->codec, codec);
1716 pktl = av_mallocz(sizeof(AVPacketList));
1722 /* add the packet in the buffered packet list */
1724 ppktl = &pktl->next;
1727 if (ic->format->read_packet(ic, pkt) < 0) {
1731 st = ic->streams[pkt->stream_index];
1733 /* decode the data and update codec parameters */
1737 switch(st->codec.codec_type) {
1738 case CODEC_TYPE_VIDEO:
1739 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1741 case CODEC_TYPE_AUDIO:
1742 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1762 /* close each codec */
1763 for(i=0;i<ic->nb_streams;i++) {
1764 st = ic->streams[i];
1765 avcodec_close(&st->codec);
1771 int filename_number_test(const char *filename)
1775 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1776 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1777 "Use '%%d' to specify the image number:\n"
1778 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1779 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1787 void opt_input_file(const char *filename)
1789 AVFormatContext *ic;
1790 AVFormatParameters params, *ap = ¶ms;
1791 URLFormat url_format;
1795 ic = av_mallocz(sizeof(AVFormatContext));
1796 strcpy(ic->filename, filename);
1797 /* first format guess to know if we must open file */
1800 fmt = guess_format(NULL, filename, NULL);
1802 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1804 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1805 fprintf(stderr, "Could not open '%s'\n", filename);
1809 /* find format and set default parameters */
1811 err = url_getformat(url_fileno(&ic->pb), &url_format);
1814 fmt = guess_format(url_format.format_name, NULL, NULL);
1815 ap->sample_rate = url_format.sample_rate;
1816 ap->frame_rate = url_format.frame_rate;
1817 ap->channels = url_format.channels;
1818 ap->width = url_format.width;
1819 ap->height = url_format.height;
1820 ap->pix_fmt = url_format.pix_fmt;
1823 fmt = guess_format(NULL, filename, NULL);
1824 memset(ap, 0, sizeof(*ap));
1827 memset(ap, 0, sizeof(*ap));
1830 if (!fmt || !fmt->read_header) {
1831 fprintf(stderr, "%s: Unknown file format\n", filename);
1836 /* get default parameters from command line */
1837 if (!ap->sample_rate)
1838 ap->sample_rate = audio_sample_rate;
1840 ap->channels = audio_channels;
1842 if (!ap->frame_rate)
1843 ap->frame_rate = frame_rate;
1845 ap->width = frame_width;
1847 ap->height = frame_height;
1849 /* check filename in case of an image number is expected */
1850 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1851 if (filename_number_test(ic->filename) < 0)
1855 err = ic->format->read_header(ic, ap);
1857 fprintf(stderr, "%s: Error while parsing header\n", filename);
1861 /* If not enough info for the codecs, we decode the first frames
1862 to get it. (used in mpeg case for example) */
1863 ret = find_codec_parameters(ic);
1865 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1869 /* update the current parameters so that they match the one of the input stream */
1870 for(i=0;i<ic->nb_streams;i++) {
1871 AVCodecContext *enc = &ic->streams[i]->codec;
1872 switch(enc->codec_type) {
1873 case CODEC_TYPE_AUDIO:
1874 audio_channels = enc->channels;
1875 audio_sample_rate = enc->sample_rate;
1877 case CODEC_TYPE_VIDEO:
1878 frame_height = enc->height;
1879 frame_width = enc->width;
1880 frame_rate = enc->frame_rate;
1885 input_files[nb_input_files] = ic;
1886 /* dump the file content */
1887 dump_format(ic, nb_input_files, filename, 0);
1892 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1894 int has_video, has_audio, i, j;
1895 AVFormatContext *ic;
1899 for(j=0;j<nb_input_files;j++) {
1900 ic = input_files[j];
1901 for(i=0;i<ic->nb_streams;i++) {
1902 AVCodecContext *enc = &ic->streams[i]->codec;
1903 switch(enc->codec_type) {
1904 case CODEC_TYPE_AUDIO:
1907 case CODEC_TYPE_VIDEO:
1913 *has_video_ptr = has_video;
1914 *has_audio_ptr = has_audio;
1917 void opt_output_file(const char *filename)
1920 AVFormatContext *oc;
1921 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1924 if (!strcmp(filename, "-"))
1927 oc = av_mallocz(sizeof(AVFormatContext));
1930 file_format = guess_format(NULL, filename, NULL);
1932 file_format = &mpeg_mux_format;
1935 oc->format = file_format;
1937 if (!strcmp(file_format->name, "ffm") &&
1938 strstart(filename, "http:", NULL)) {
1939 /* special case for files sent to ffserver: we get the stream
1940 parameters from ffserver */
1941 if (read_ffserver_streams(oc, filename) < 0) {
1942 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1946 use_video = file_format->video_codec != CODEC_ID_NONE;
1947 use_audio = file_format->audio_codec != CODEC_ID_NONE;
1949 /* disable if no corresponding type found and at least one
1951 if (nb_input_files > 0) {
1952 check_audio_video_inputs(&input_has_video, &input_has_audio);
1953 if (!input_has_video)
1955 if (!input_has_audio)
1959 /* manual disable */
1960 if (audio_disable) {
1963 if (video_disable) {
1969 AVCodecContext *video_enc;
1971 st = av_mallocz(sizeof(AVStream));
1973 fprintf(stderr, "Could not alloc stream\n");
1976 video_enc = &st->codec;
1978 codec_id = file_format->video_codec;
1979 if (video_codec_id != CODEC_ID_NONE)
1980 codec_id = video_codec_id;
1982 video_enc->codec_id = codec_id;
1983 video_enc->codec_type = CODEC_TYPE_VIDEO;
1985 video_enc->bit_rate = video_bit_rate;
1986 video_enc->frame_rate = frame_rate;
1988 video_enc->width = frame_width;
1989 video_enc->height = frame_height;
1991 video_enc->gop_size = gop_size;
1993 video_enc->gop_size = 0;
1994 if (video_qscale || same_quality) {
1995 video_enc->flags |= CODEC_FLAG_QSCALE;
1996 video_enc->quality = video_qscale;
1998 /* XXX: need to find a way to set codec parameters */
1999 if (oc->format == &ppm_format ||
2000 oc->format == &ppmpipe_format) {
2001 video_enc->pix_fmt = PIX_FMT_RGB24;
2004 oc->streams[nb_streams] = st;
2009 AVCodecContext *audio_enc;
2011 st = av_mallocz(sizeof(AVStream));
2013 fprintf(stderr, "Could not alloc stream\n");
2016 audio_enc = &st->codec;
2017 codec_id = file_format->audio_codec;
2018 if (audio_codec_id != CODEC_ID_NONE)
2019 codec_id = audio_codec_id;
2020 audio_enc->codec_id = codec_id;
2021 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2023 audio_enc->bit_rate = audio_bit_rate;
2024 audio_enc->sample_rate = audio_sample_rate;
2025 audio_enc->channels = audio_channels;
2026 oc->streams[nb_streams] = st;
2030 oc->nb_streams = nb_streams;
2033 fprintf(stderr, "No audio or video streams available\n");
2038 nstrcpy(oc->title, sizeof(oc->title), str_title);
2040 nstrcpy(oc->author, sizeof(oc->author), str_author);
2042 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2044 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2047 output_files[nb_output_files] = oc;
2048 /* dump the file content */
2049 dump_format(oc, nb_output_files, filename, 1);
2052 strcpy(oc->filename, filename);
2054 /* check filename in case of an image number is expected */
2055 if (oc->format->flags & AVFMT_NEEDNUMBER) {
2056 if (filename_number_test(oc->filename) < 0)
2060 if (!(oc->format->flags & AVFMT_NOFILE)) {
2061 /* test if it already exists to avoid loosing precious files */
2062 if (!file_overwrite &&
2063 (strchr(filename, ':') == NULL ||
2064 strstart(filename, "file:", NULL))) {
2065 if (url_exist(filename)) {
2068 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2071 if (toupper(c) != 'Y') {
2072 fprintf(stderr, "Not overwriting - exiting\n");
2079 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2080 fprintf(stderr, "Could not open '%s'\n", filename);
2085 /* reset some options */
2089 audio_codec_id = CODEC_ID_NONE;
2090 video_codec_id = CODEC_ID_NONE;
2093 #ifndef CONFIG_WIN32
2094 INT64 getutime(void)
2096 struct rusage rusage;
2098 getrusage(RUSAGE_SELF, &rusage);
2099 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2102 INT64 getutime(void)
2108 void show_formats(void)
2115 printf("File formats:\n");
2116 printf(" Encoding:");
2117 for(f = first_format; f != NULL; f = f->next) {
2118 if (f->write_header)
2119 printf(" %s", f->name);
2122 printf(" Decoding:");
2123 for(f = first_format; f != NULL; f = f->next) {
2125 printf(" %s", f->name);
2129 printf("Codecs:\n");
2130 printf(" Encoders:");
2131 for(p = first_avcodec; p != NULL; p = p->next) {
2133 printf(" %s", p->name);
2137 printf(" Decoders:");
2138 for(p = first_avcodec; p != NULL; p = p->next) {
2140 printf(" %s", p->name);
2144 printf("Supported file protocols:");
2145 for(up = first_protocol; up != NULL; up = up->next)
2146 printf(" %s:", up->name);
2149 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2150 printf("Motion estimation methods:");
2154 if ((pp - motion_str) == ME_ZERO)
2155 printf("(fastest)");
2156 else if ((pp - motion_str) == ME_FULL)
2157 printf("(slowest)");
2158 else if ((pp - motion_str) == ME_LOG)
2159 printf("(default)");
2166 void show_help(void)
2168 const OptionDef *po;
2171 printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000,2001 Gerard Lantau\n"
2172 "usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2173 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n"
2175 "Main options are:\n");
2178 printf("\nAdvanced options are:\n");
2179 for(po = options; po->name != NULL; po++) {
2181 expert = (po->flags & OPT_EXPERT) != 0;
2183 strcpy(buf, po->name);
2184 if (po->flags & HAS_ARG) {
2186 strcat(buf, po->argname);
2188 printf("-%-17s %s\n", buf, po->help);
2196 const OptionDef options[] = {
2197 { "L", 0, {(void*)show_licence}, "show license" },
2198 { "h", 0, {(void*)show_help}, "show help" },
2199 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2200 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2201 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2202 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2203 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2204 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2205 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2206 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2207 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2208 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2210 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2211 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2212 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2213 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2214 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2215 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2216 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2218 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
2220 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2221 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2223 { "sameq", OPT_BOOL, {(void*)&same_quality},
2224 "use same video quality as source (implies VBR)" },
2226 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2227 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2228 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2229 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2231 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2233 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2234 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2235 "deinterlace pictures" },
2236 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2237 "add timings for benchmarking" },
2238 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2239 "dump each input packet" },
2244 int main(int argc, char **argv)
2247 const char *opt, *arg;
2248 const OptionDef *po;
2256 while (optindex < argc) {
2257 opt = argv[optindex++];
2259 if (opt[0] == '-' && opt[1] != '\0') {
2261 while (po->name != NULL) {
2262 if (!strcmp(opt + 1, po->name))
2267 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2271 if (po->flags & HAS_ARG)
2272 arg = argv[optindex++];
2273 if (po->flags & OPT_STRING) {
2276 *po->u.str_arg = str;
2277 } else if (po->flags & OPT_BOOL) {
2280 po->u.func_arg(arg);
2283 opt_output_file(opt);
2288 if (nb_input_files == 0) {
2290 if (nb_output_files != 1) {
2291 fprintf(stderr, "Only one output file supported when grabbing\n");
2294 av_grab(output_files[0]);
2296 fprintf(stderr, "Must supply at least one input file\n");
2301 if (nb_output_files <= 0) {
2302 fprintf(stderr, "Must supply at least one output file\n");
2306 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2307 stream_maps, nb_stream_maps);
2308 ti = getutime() - ti;
2310 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2315 for(i=0;i<nb_output_files;i++) {
2316 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2317 url_fclose(&output_files[i]->pb);
2319 for(i=0;i<nb_input_files;i++) {
2320 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2321 url_fclose(&input_files[i]->pb);