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;
100 typedef struct AVOutputStream {
101 int file_index; /* file index */
102 int index; /* stream index in the output file */
103 int source_index; /* AVInputStream index */
104 AVStream *st; /* stream in the output file */
105 int encoding_needed; /* true if encoding needed for this stream */
107 int fifo_packet_rptr; /* read index in the corresponding
108 avinputstream packet fifo */
110 AVPicture pict_tmp; /* temporary image for resizing */
112 ImgReSampleContext *img_resample_ctx; /* for image resampling */
116 ReSampleContext *resample; /* for audio resampling */
117 FifoBuffer fifo; /* for compression: one audio fifo per codec */
120 typedef struct AVInputStream {
124 int discard; /* true if stream data should be discarded */
125 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
126 INT64 pts; /* current pts */
127 int frame_number; /* current frame */
128 INT64 sample_index; /* current sample */
131 typedef struct AVInputFile {
132 int eof_reached; /* true if eof reached */
133 int ist_index; /* index of first stream in ist_table */
134 int buffer_size; /* current total buffer size */
135 int buffer_size_max; /* buffer size at which we consider we can stop
141 /* init terminal so that we can grab keys */
142 static struct termios oldtty;
144 static void term_exit(void)
146 tcsetattr (0, TCSANOW, &oldtty);
149 static void term_init(void)
156 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
157 |INLCR|IGNCR|ICRNL|IXON);
158 tty.c_oflag |= OPOST;
159 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
160 tty.c_cflag &= ~(CSIZE|PARENB);
165 tcsetattr (0, TCSANOW, &tty);
170 /* read a key without blocking */
171 static int read_key(void)
182 n = select(1, &rfds, NULL, NULL, &tv);
184 if (read(0, &ch, 1) == 1)
190 #define AUDIO_FIFO_SIZE 8192
192 /* main loop for grabbing */
193 int av_grab(AVFormatContext *s)
195 UINT8 audio_buf[AUDIO_FIFO_SIZE];
196 UINT8 audio_buf1[AUDIO_FIFO_SIZE];
197 UINT8 audio_out[AUDIO_FIFO_SIZE];
198 UINT8 video_buffer[1024*1024];
201 URLContext *audio_handle = NULL, *video_handle = NULL;
203 AVCodecContext *enc, *first_video_enc = NULL;
204 int frame_size, frame_bytes;
205 int use_audio, use_video;
206 int frame_rate, sample_rate, channels;
207 int width, height, frame_number, i, pix_fmt = 0;
208 AVOutputStream *ost_table[s->nb_streams], *ost;
209 UINT8 *picture_in_buf = NULL, *picture_420p = NULL;
210 int audio_fifo_size = 0, picture_size = 0;
213 /* init output stream info */
214 for(i=0;i<s->nb_streams;i++)
217 /* output stream init */
218 for(i=0;i<s->nb_streams;i++) {
219 ost = av_mallocz(sizeof(AVOutputStream));
223 ost->st = s->streams[i];
237 for(i=0;i<s->nb_streams;i++) {
241 enc = &ost->st->codec;
242 codec = avcodec_find_encoder(enc->codec_id);
244 fprintf(stderr, "Unknown codec\n");
247 if (avcodec_open(enc, codec) < 0) {
248 fprintf(stderr, "Incorrect encode parameters\n");
251 switch(enc->codec_type) {
252 case CODEC_TYPE_AUDIO:
254 if (enc->sample_rate > sample_rate)
255 sample_rate = enc->sample_rate;
256 if (enc->frame_size > frame_size)
257 frame_size = enc->frame_size;
258 if (enc->channels > channels)
259 channels = enc->channels;
261 case CODEC_TYPE_VIDEO:
262 if (!first_video_enc)
263 first_video_enc = enc;
265 if (enc->frame_rate > frame_rate)
266 frame_rate = enc->frame_rate;
267 if (enc->width > width)
269 if (enc->height > height)
270 height = enc->height;
278 snprintf(buf, sizeof(buf), "audio:%d,%d", sample_rate, channels);
279 ret = url_open(&audio_handle, buf, URL_RDONLY);
281 fprintf(stderr, "Could not open audio device: disabling audio capture\n");
285 /* read back exact grab parameters */
286 if (url_getformat(audio_handle, &f) < 0) {
287 fprintf(stderr, "could not read back video grab parameters\n");
290 sample_rate = f.sample_rate;
291 channels = f.channels;
292 audio_fifo_size = ((AUDIO_FIFO_SIZE / 2) / audio_handle->packet_size) *
293 audio_handle->packet_size;
294 fprintf(stderr, "Audio sampling: %d Hz, %s\n",
295 sample_rate, channels == 2 ? "stereo" : "mono");
301 snprintf(buf, sizeof(buf), "video:%d,%d,%f",
302 width, height, (float)frame_rate / FRAME_RATE_BASE);
304 ret = url_open(&video_handle, buf, URL_RDONLY);
306 fprintf(stderr,"Could not init video 4 linux capture: disabling video capture\n");
310 const char *pix_fmt_str;
311 /* read back exact grab parameters */
312 if (url_getformat(video_handle, &f) < 0) {
313 fprintf(stderr, "could not read back video grab parameters\n");
320 case PIX_FMT_YUV420P:
321 pix_fmt_str = "420P";
327 pix_fmt_str = "RGB24";
330 pix_fmt_str = "BGR24";
336 picture_size = video_handle->packet_size;
337 picture_in_buf = malloc(picture_size);
340 /* allocate a temporary picture if not grabbing in 420P format */
341 if (pix_fmt != PIX_FMT_YUV420P) {
342 picture_420p = malloc((width * height * 3) / 2);
344 fprintf(stderr, "Video sampling: %dx%d, %s format, %0.2f fps\n",
345 width, height, pix_fmt_str, (float)frame_rate / FRAME_RATE_BASE);
349 if (!use_video && !use_audio) {
350 fprintf(stderr,"Could not open grab devices : exiting\n");
354 /* init built in conversion functions */
355 for(i=0;i<s->nb_streams;i++) {
357 enc = &ost->st->codec;
358 switch(enc->codec_type) {
359 case CODEC_TYPE_AUDIO:
360 ost->audio_resample = 0;
361 if ((enc->channels != channels ||
362 enc->sample_rate != sample_rate)) {
363 ost->audio_resample = 1;
364 ost->resample = audio_resample_init(enc->channels, channels,
365 enc->sample_rate, sample_rate);
367 if (fifo_init(&ost->fifo, (2 * audio_fifo_size * enc->sample_rate *
368 enc->channels) / sample_rate))
371 case CODEC_TYPE_VIDEO:
372 ost->video_resample = 0;
373 if (enc->width != width ||
374 enc->height != height) {
376 ost->video_resample = 1;
377 buf = malloc((enc->width * enc->height * 3) / 2);
380 ost->pict_tmp.data[0] = buf;
381 ost->pict_tmp.data[1] = buf + enc->width * height;
382 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (enc->width * height) / 4;
383 ost->pict_tmp.linesize[0] = enc->width;
384 ost->pict_tmp.linesize[1] = enc->width / 2;
385 ost->pict_tmp.linesize[2] = enc->width / 2;
386 ost->img_resample_ctx = img_resample_init(
387 ost->st->codec.width, ost->st->codec.height,
393 fprintf(stderr, "Press [q] to stop encoding\n");
395 s->format->write_header(s);
396 time_start = gettime();
400 /* if 'q' pressed, exits */
401 if (read_key() == 'q')
404 /* read & compress audio frames */
406 int ret, nb_samples, nb_samples_out;
410 ret = url_read(audio_handle, audio_buf, audio_fifo_size);
413 /* fill each codec fifo by doing the right sample
414 rate conversion. This is not optimal because we
415 do too much work, but it is easy to do */
416 nb_samples = ret / (channels * 2);
417 for(i=0;i<s->nb_streams;i++) {
419 enc = &ost->st->codec;
420 if (enc->codec_type == CODEC_TYPE_AUDIO) {
421 /* rate & stereo convertion */
422 if (!ost->audio_resample) {
424 nb_samples_out = nb_samples;
427 nb_samples_out = audio_resample(ost->resample,
428 (short *)buftmp, (short *)audio_buf,
431 fifo_write(&ost->fifo, buftmp, nb_samples_out * enc->channels * 2,
436 /* compress as many frame as possible with each audio codec */
437 for(i=0;i<s->nb_streams;i++) {
439 enc = &ost->st->codec;
440 if (enc->codec_type == CODEC_TYPE_AUDIO) {
441 frame_bytes = enc->frame_size * 2 * enc->channels;
443 while (fifo_read(&ost->fifo, audio_buf,
444 frame_bytes, &ost->fifo.rptr) == 0) {
445 ret = avcodec_encode_audio(enc,
446 audio_out, sizeof(audio_out),
448 s->format->write_packet(s, ost->index, audio_out, ret);
456 AVPicture *picture1, *picture2, *picture;
457 AVPicture picture_tmp0, picture_tmp1;
459 ret = url_read(video_handle, picture_in_buf, picture_size);
463 picture2 = &picture_tmp0;
464 avpicture_fill(picture2, picture_in_buf, pix_fmt, width, height);
466 if (pix_fmt != PIX_FMT_YUV420P) {
467 picture = &picture_tmp1;
468 avpicture_fill(picture, picture_420p,
469 PIX_FMT_YUV420P, width, height);
470 img_convert(picture, PIX_FMT_YUV420P,
477 for(i=0;i<s->nb_streams;i++) {
479 enc = &ost->st->codec;
480 if (enc->codec_type == CODEC_TYPE_VIDEO) {
483 /* feed each codec with its requested frame rate */
484 n1 = ((INT64)frame_number * enc->frame_rate) / frame_rate;
485 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / frame_rate;
488 /* resize the picture if needed */
489 if (ost->video_resample) {
490 picture1 = &ost->pict_tmp;
491 img_resample(ost->img_resample_ctx,
496 ret = avcodec_encode_video(enc, video_buffer,
497 sizeof(video_buffer),
499 s->format->write_packet(s, ost->index, video_buffer, ret);
511 static float last_ti;
514 total_size = url_ftell(&s->pb);
515 ti1 = gettime() - time_start;
516 /* check elapsed time */
517 if (recording_time && ti1 >= recording_time)
520 ti = ti1 / 1000000.0;
523 /* dispaly twice per second */
524 if ((ti - last_ti) >= 0.5) {
526 bitrate = (int)((total_size * 8) / ti / 1000.0);
530 sprintf(buf + strlen(buf), "frame=%5d fps=%4.1f q=%2d ",
531 frame_number, (float)frame_number / ti, first_video_enc->quality);
534 sprintf(buf + strlen(buf), "size=%8LdkB time=%0.1f bitrate=%6.1fkbits/s",
535 total_size / 1024, ti, bitrate);
536 fprintf(stderr, "%s \r", buf);
543 for(i=0;i<s->nb_streams;i++) {
545 enc = &ost->st->codec;
548 s->format->write_trailer(s);
551 url_close(audio_handle);
554 url_close(video_handle);
561 total_size = url_ftell(&s->pb);
563 ti = (gettime() - time_start) / 1000000.0;
566 bitrate = (int)((total_size * 8) / ti / 1000.0);
568 fprintf(stderr, "\033[K\nTotal time = %0.1f s, %Ld KBytes, %0.1f kbits/s\n",
569 ti, total_size / 1024, bitrate);
571 fprintf(stderr, "Total frames = %d\n", frame_number);
578 free(picture_in_buf);
581 for(i=0;i<s->nb_streams;i++) {
584 if (ost->fifo.buffer)
585 fifo_free(&ost->fifo);
586 if (ost->pict_tmp.data[0])
587 free(ost->pict_tmp.data[0]);
588 if (ost->video_resample)
589 img_resample_close(ost->img_resample_ctx);
590 if (ost->audio_resample)
591 audio_resample_close(ost->resample);
601 #endif /* CONFIG_WIN32 */
603 int read_ffserver_streams(AVFormatContext *s, const char *filename)
608 ic = av_open_input_file(filename, FFM_PACKET_SIZE);
611 /* copy stream format */
612 s->nb_streams = ic->nb_streams;
613 for(i=0;i<ic->nb_streams;i++) {
615 st = av_mallocz(sizeof(AVFormatContext));
616 memcpy(st, ic->streams[i], sizeof(AVStream));
620 av_close_input_file(ic);
624 #define MAX_AUDIO_PACKET_SIZE 16384
626 static void do_audio_out(AVFormatContext *s,
629 unsigned char *buf, int size)
632 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
633 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
634 int size_out, frame_bytes, ret;
637 enc = &ost->st->codec;
639 if (ost->audio_resample) {
641 size_out = audio_resample(ost->resample,
642 (short *)buftmp, (short *)buf,
643 size / (ist->st->codec.channels * 2));
644 size_out = size_out * enc->channels * 2;
650 /* now encode as many frames as possible */
651 if (enc->codec_id != CODEC_ID_PCM) {
652 /* output resampled raw samples */
653 fifo_write(&ost->fifo, buftmp, size_out,
656 frame_bytes = enc->frame_size * 2 * enc->channels;
658 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
659 &ost->fifo.rptr) == 0) {
660 ret = avcodec_encode_audio(enc,
661 audio_out, sizeof(audio_out), (short *)audio_buf);
662 s->format->write_packet(s, ost->index, audio_out, ret);
665 /* XXX: handle endianness */
666 s->format->write_packet(s, ost->index, buftmp, size_out);
670 /* write a picture to a raw mux */
671 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
672 int pix_fmt, int w, int h)
674 UINT8 *buf, *src, *dest;
677 size = avpicture_get_size(pix_fmt, w, h);
682 /* XXX: not efficient, should add test if we can take
683 directly the AVPicture */
685 case PIX_FMT_YUV420P:
692 src = picture->data[i];
694 memcpy(dest, src, w);
696 src += picture->linesize[i];
700 case PIX_FMT_YUV422P:
708 src = picture->data[i];
710 memcpy(dest, src, w);
712 src += picture->linesize[i];
716 case PIX_FMT_YUV444P:
721 src = picture->data[i];
723 memcpy(dest, src, w);
725 src += picture->linesize[i];
733 src = picture->data[0];
735 memcpy(dest, src, w * 2);
737 src += picture->linesize[0];
745 src = picture->data[0];
747 memcpy(dest, src, w * 3);
749 src += picture->linesize[0];
755 s->format->write_packet(s, index, buf, size);
760 static void do_video_out(AVFormatContext *s,
765 int n1, n2, nb, i, ret, frame_number;
766 AVPicture *picture, *picture2, *pict;
767 AVPicture picture_tmp1, picture_tmp2;
768 UINT8 video_buffer[1024*1024];
769 UINT8 *buf = NULL, *buf1 = NULL;
770 AVCodecContext *enc, *dec;
772 enc = &ost->st->codec;
773 dec = &ist->st->codec;
775 frame_number = ist->frame_number;
776 /* first drop frame if needed */
777 n1 = ((INT64)frame_number * enc->frame_rate) / dec->frame_rate;
778 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec->frame_rate;
783 /* deinterlace : must be done before any resize */
784 if (do_deinterlace) {
787 /* create temporary picture */
788 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
793 picture2 = &picture_tmp2;
794 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
796 if (avpicture_deinterlace(picture2, picture1,
797 dec->pix_fmt, dec->width, dec->height) < 0) {
798 /* if error, do not deinterlace */
807 /* convert pixel format if needed */
808 if (enc->pix_fmt != dec->pix_fmt) {
811 /* create temporary picture */
812 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
816 pict = &picture_tmp1;
817 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
819 if (img_convert(pict, enc->pix_fmt,
820 picture2, dec->pix_fmt,
821 dec->width, dec->height) < 0) {
822 fprintf(stderr, "pixel format conversion not handled\n");
829 /* XXX: resampling could be done before raw format convertion in
830 some cases to go faster */
831 /* XXX: only works for YUV420P */
832 if (ost->video_resample) {
833 picture = &ost->pict_tmp;
834 img_resample(ost->img_resample_ctx, picture, pict);
839 /* duplicates frame if needed */
840 /* XXX: pb because no interleaving */
842 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
843 /* handles sameq here. This is not correct because it may
844 not be a global option */
846 enc->quality = dec->quality;
848 ret = avcodec_encode_video(enc,
849 video_buffer, sizeof(video_buffer),
851 s->format->write_packet(s, ost->index, video_buffer, ret);
853 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
866 static void hex_dump(UINT8 *buf, int size)
870 for(i=0;i<size;i+=16) {
877 printf(" %02x", buf[i+j]);
884 if (c < ' ' || c > '~')
894 * The following code is the main loop of the file converter
896 static int av_encode(AVFormatContext **output_files,
898 AVFormatContext **input_files,
900 AVStreamMap *stream_maps, int nb_stream_maps)
902 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
903 AVFormatContext *is, *os;
904 AVCodecContext *codec, *icodec;
905 AVOutputStream *ost, **ost_table = NULL;
906 AVInputStream *ist, **ist_table = NULL;
907 INT64 min_pts, start_time;
908 AVInputFile *file_table;
910 file_table= (AVInputFile*) malloc(nb_input_files * sizeof(AVInputFile));
914 memset(file_table, 0, sizeof(file_table));
916 /* input stream init */
918 for(i=0;i<nb_input_files;i++) {
920 file_table[i].ist_index = j;
925 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
929 for(i=0;i<nb_istreams;i++) {
930 ist = av_mallocz(sizeof(AVInputStream));
936 for(i=0;i<nb_input_files;i++) {
938 for(k=0;k<is->nb_streams;k++) {
939 ist = ist_table[j++];
940 ist->st = is->streams[k];
943 ist->discard = 1; /* the stream is discarded by default
948 /* output stream init */
950 for(i=0;i<nb_output_files;i++) {
951 os = output_files[i];
952 nb_ostreams += os->nb_streams;
954 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
955 fprintf(stderr, "Number of stream maps must match number of output streams\n");
959 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
962 for(i=0;i<nb_ostreams;i++) {
963 ost = av_mallocz(sizeof(AVOutputStream));
970 for(k=0;k<nb_output_files;k++) {
971 os = output_files[k];
972 for(i=0;i<os->nb_streams;i++) {
974 ost = ost_table[n++];
977 ost->st = os->streams[i];
978 if (nb_stream_maps > 0) {
979 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
980 stream_maps[n-1].stream_index;
982 /* get corresponding input stream index : we select the first one with the right type */
984 for(j=0;j<nb_istreams;j++) {
987 ist->st->codec.codec_type == ost->st->codec.codec_type) {
988 ost->source_index = j;
994 /* try again and reuse existing stream */
995 for(j=0;j<nb_istreams;j++) {
997 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
998 ost->source_index = j;
1003 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1004 ost->file_index, ost->index);
1009 ist = ist_table[ost->source_index];
1014 /* dump the stream mapping */
1015 fprintf(stderr, "Stream mapping:\n");
1016 for(i=0;i<nb_ostreams;i++) {
1018 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1019 ist_table[ost->source_index]->file_index,
1020 ist_table[ost->source_index]->index,
1025 /* for each output stream, we compute the right encoding parameters */
1026 for(i=0;i<nb_ostreams;i++) {
1028 ist = ist_table[ost->source_index];
1030 codec = &ost->st->codec;
1031 icodec = &ist->st->codec;
1033 switch(codec->codec_type) {
1034 case CODEC_TYPE_AUDIO:
1035 /* check if same codec with same parameters. If so, no
1036 reencoding is needed */
1037 if (codec->codec_id == icodec->codec_id &&
1038 codec->bit_rate == icodec->bit_rate &&
1039 codec->sample_rate == icodec->sample_rate &&
1040 codec->channels == icodec->channels) {
1043 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1046 if (codec->channels == icodec->channels &&
1047 codec->sample_rate == icodec->sample_rate) {
1048 ost->audio_resample = 0;
1050 ost->audio_resample = 1;
1051 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1053 icodec->sample_rate);
1055 ist->decoding_needed = 1;
1056 ost->encoding_needed = 1;
1059 case CODEC_TYPE_VIDEO:
1060 /* check if same codec with same parameters. If so, no
1061 reencoding is needed */
1062 if (codec->codec_id == icodec->codec_id &&
1063 codec->bit_rate == icodec->bit_rate &&
1064 codec->frame_rate == icodec->frame_rate &&
1065 codec->width == icodec->width &&
1066 codec->height == icodec->height) {
1069 if (codec->width == icodec->width &&
1070 codec->height == icodec->height) {
1071 ost->video_resample = 0;
1074 ost->video_resample = 1;
1075 buf = malloc((codec->width * codec->height * 3) / 2);
1078 ost->pict_tmp.data[0] = buf;
1079 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
1080 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
1081 ost->pict_tmp.linesize[0] = codec->width;
1082 ost->pict_tmp.linesize[1] = codec->width / 2;
1083 ost->pict_tmp.linesize[2] = codec->width / 2;
1085 ost->img_resample_ctx = img_resample_init(
1086 ost->st->codec.width, ost->st->codec.height,
1087 ist->st->codec.width, ist->st->codec.height);
1089 ost->encoding_needed = 1;
1090 ist->decoding_needed = 1;
1096 /* open each encoder */
1097 for(i=0;i<nb_ostreams;i++) {
1099 if (ost->encoding_needed) {
1101 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1103 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1104 ost->file_index, ost->index);
1107 if (avcodec_open(&ost->st->codec, codec) < 0) {
1108 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1109 ost->file_index, ost->index);
1115 /* open each decoder */
1116 for(i=0;i<nb_istreams;i++) {
1118 if (ist->decoding_needed) {
1120 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1122 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
1123 ist->file_index, ist->index);
1126 if (avcodec_open(&ist->st->codec, codec) < 0) {
1127 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1128 ist->file_index, ist->index);
1135 for(i=0;i<nb_istreams;i++) {
1138 ist->frame_number = 0;
1141 /* compute buffer size max (should use a complete heuristic) */
1142 for(i=0;i<nb_input_files;i++) {
1143 file_table[i].buffer_size_max = 2048;
1146 /* open files and write file headers */
1147 for(i=0;i<nb_output_files;i++) {
1148 os = output_files[i];
1149 os->format->write_header(os);
1152 start_time = gettime();
1155 int file_index, ist_index;
1160 int data_size, got_picture;
1162 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1164 /* select the input file with the smallest pts */
1168 for(i=0;i<nb_istreams;i++) {
1170 if (!ist->discard && !file_table[ist->file_index].eof_reached && ist->pts < min_pts) {
1172 file_index = ist->file_index;
1175 /* if none, if is finished */
1178 /* finish if recording time exhausted */
1179 if (recording_time > 0 && min_pts >= recording_time)
1181 /* read a packet from it and output it in the fifo */
1183 is = input_files[file_index];
1184 if (av_read_packet(is, &pkt) < 0) {
1185 file_table[file_index].eof_reached = 1;
1188 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1189 ist = ist_table[ist_index];
1195 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1196 hex_dump(pkt.data, pkt.size);
1199 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1205 /* decode the packet if needed */
1206 data_buf = NULL; /* fail safe */
1208 if (ist->decoding_needed) {
1209 switch(ist->st->codec.codec_type) {
1210 case CODEC_TYPE_AUDIO:
1211 if (ist->st->codec.codec_id == CODEC_ID_PCM) {
1212 /* no need to call a codec */
1217 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1221 if (data_size == 0) {
1222 /* no audio frame */
1227 data_buf = (UINT8 *)samples;
1230 case CODEC_TYPE_VIDEO:
1231 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1233 size = (ist->st->codec.width * ist->st->codec.height);
1234 avpicture_fill(&picture, ptr,
1235 ist->st->codec.pix_fmt,
1236 ist->st->codec.width,
1237 ist->st->codec.height);
1240 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1241 ret = avcodec_decode_video(&ist->st->codec,
1242 &picture, &got_picture, ptr, len);
1245 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1246 ist->file_index, ist->index);
1247 av_free_packet(&pkt);
1251 /* no picture yet */
1267 switch(ist->st->codec.codec_type) {
1268 case CODEC_TYPE_AUDIO:
1269 ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1270 ist->sample_index += data_size / (2 * ist->st->codec.channels);
1272 case CODEC_TYPE_VIDEO:
1273 ist->frame_number++;
1274 ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1275 ist->st->codec.frame_rate;
1281 /* transcode raw format, encode packets and output them */
1283 for(i=0;i<nb_ostreams;i++) {
1285 if (ost->source_index == ist_index) {
1286 os = output_files[ost->file_index];
1288 if (ost->encoding_needed) {
1289 switch(ost->st->codec.codec_type) {
1290 case CODEC_TYPE_AUDIO:
1291 do_audio_out(os, ost, ist, data_buf, data_size);
1293 case CODEC_TYPE_VIDEO:
1294 do_video_out(os, ost, ist, &picture);
1298 /* no reencoding needed : output the packet directly */
1299 os->format->write_packet(os, ost->index, data_buf, data_size);
1304 av_free_packet(&pkt);
1306 /* dump report by using the first video and audio streams */
1309 AVFormatContext *oc;
1310 INT64 total_size, ti;
1311 AVCodecContext *enc;
1312 int frame_number, vid;
1313 double bitrate, ti1;
1314 static INT64 last_time;
1316 if ((min_pts - last_time) >= 500000) {
1317 last_time = min_pts;
1319 oc = output_files[0];
1321 total_size = url_ftell(&oc->pb);
1326 for(i=0;i<nb_ostreams;i++) {
1328 enc = &ost->st->codec;
1329 ist = ist_table[ost->source_index];
1330 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1331 frame_number = ist->frame_number;
1332 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1333 frame_number, enc->quality);
1336 /* compute min pts value */
1337 if (!ist->discard && ist->pts < ti) {
1342 ti1 = (double)ti / 1000000.0;
1345 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1347 sprintf(buf + strlen(buf),
1348 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1349 (double)total_size / 1024, ti1, bitrate);
1351 fprintf(stderr, "%s \r", buf);
1357 /* dump report by using the first video and audio streams */
1360 AVFormatContext *oc;
1361 INT64 total_size, ti;
1362 AVCodecContext *enc;
1363 int frame_number, vid;
1364 double bitrate, ti1;
1366 oc = output_files[0];
1368 total_size = url_ftell(&oc->pb);
1373 for(i=0;i<nb_ostreams;i++) {
1375 enc = &ost->st->codec;
1376 ist = ist_table[ost->source_index];
1377 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1378 frame_number = ist->frame_number;
1379 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1380 frame_number, enc->quality);
1383 /* compute min pts value */
1384 if (!ist->discard && ist->pts < ti) {
1389 ti1 = ti / 1000000.0;
1392 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1394 sprintf(buf + strlen(buf),
1395 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1396 (double)total_size / 1024, ti1, bitrate);
1398 fprintf(stderr, "%s \n", buf);
1400 /* close each encoder */
1401 for(i=0;i<nb_ostreams;i++) {
1403 if (ost->encoding_needed) {
1404 avcodec_close(&ost->st->codec);
1408 /* close each decoder */
1409 for(i=0;i<nb_istreams;i++) {
1411 if (ist->decoding_needed) {
1412 avcodec_close(&ist->st->codec);
1417 /* write the trailer if needed and close file */
1418 for(i=0;i<nb_output_files;i++) {
1419 os = output_files[i];
1420 os->format->write_trailer(os);
1429 for(i=0;i<nb_istreams;i++) {
1438 for(i=0;i<nb_ostreams;i++) {
1441 if (ost->pict_tmp.data[0])
1442 free(ost->pict_tmp.data[0]);
1443 if (ost->video_resample)
1444 img_resample_close(ost->img_resample_ctx);
1445 if (ost->audio_resample)
1446 audio_resample_close(ost->resample);
1459 int file_read(const char *filename)
1462 unsigned char buffer[1024];
1465 if (url_open(&h, filename, O_RDONLY) < 0) {
1466 printf("could not open '%s'\n", filename);
1470 len = url_read(h, buffer, sizeof(buffer));
1473 for(i=0;i<len;i++) putchar(buffer[i]);
1480 void show_licence(void)
1483 "ffmpeg version " FFMPEG_VERSION "\n"
1484 "Copyright (c) 2000,2001 Gerard Lantau\n"
1485 "This program is free software; you can redistribute it and/or modify\n"
1486 "it under the terms of the GNU General Public License as published by\n"
1487 "the Free Software Foundation; either version 2 of the License, or\n"
1488 "(at your option) any later version.\n"
1490 "This program is distributed in the hope that it will be useful,\n"
1491 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1492 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
1493 "GNU General Public License for more details.\n"
1495 "You should have received a copy of the GNU General Public License\n"
1496 "along with this program; if not, write to the Free Software\n"
1497 "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
1502 void opt_format(const char *arg)
1506 while (f != NULL && strcmp(f->name, arg) != 0) f = f->next;
1508 fprintf(stderr, "Invalid format: %s\n", arg);
1514 void opt_video_bitrate(const char *arg)
1516 video_bit_rate = atoi(arg) * 1000;
1519 void opt_frame_rate(const char *arg)
1521 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1524 void opt_frame_size(const char *arg)
1526 parse_image_size(&frame_width, &frame_height, arg);
1527 if (frame_width <= 0 || frame_height <= 0) {
1528 fprintf(stderr, "Incorrect frame size\n");
1531 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1532 fprintf(stderr, "Frame size must be a multiple of 2\n");
1537 void opt_gop_size(const char *arg)
1539 gop_size = atoi(arg);
1542 void opt_qscale(const char *arg)
1544 video_qscale = atoi(arg);
1545 if (video_qscale < 0 ||
1546 video_qscale > 31) {
1547 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1553 void opt_audio_bitrate(const char *arg)
1555 audio_bit_rate = atoi(arg) * 1000;
1558 void opt_audio_rate(const char *arg)
1560 audio_sample_rate = atoi(arg);
1563 void opt_audio_channels(const char *arg)
1565 audio_channels = atoi(arg);
1569 void opt_video_device(const char *arg)
1571 v4l_device = strdup(arg);
1574 void opt_audio_device(const char *arg)
1576 audio_device = strdup(arg);
1580 void opt_audio_codec(const char *arg)
1586 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1591 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1594 audio_codec_id = p->id;
1598 const char *motion_str[] = {
1606 void opt_motion_estimation(const char *arg)
1612 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1615 if (!strcmp(*p, arg))
1619 motion_estimation_method = p - motion_str;
1622 void opt_video_codec(const char *arg)
1628 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1633 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1636 video_codec_id = p->id;
1640 void opt_map(const char *arg)
1646 m = &stream_maps[nb_stream_maps++];
1648 m->file_index = strtol(arg, (char **)&p, 0);
1651 m->stream_index = strtol(arg, (char **)&p, 0);
1654 void opt_recording_time(const char *arg)
1656 recording_time = parse_date(arg, 1);
1659 /* return the number of packet read to find the codec parameters */
1660 int find_codec_parameters(AVFormatContext *ic)
1662 int val, i, count, ret, got_picture, size;
1664 AVCodecContext *enc;
1668 AVPacketList *pktl, **ppktl;
1669 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1673 ppktl = &ic->packet_buffer;
1675 for(i=0;i<ic->nb_streams;i++) {
1676 enc = &ic->streams[i]->codec;
1678 switch(enc->codec_type) {
1679 case CODEC_TYPE_AUDIO:
1680 val = enc->sample_rate;
1682 case CODEC_TYPE_VIDEO:
1689 /* if no parameters supplied, then we should read it from
1694 if (i == ic->nb_streams) {
1700 /* open each codec */
1701 for(i=0;i<ic->nb_streams;i++) {
1702 st = ic->streams[i];
1703 codec = avcodec_find_decoder(st->codec.codec_id);
1704 if (codec == NULL) {
1708 avcodec_open(&st->codec, codec);
1711 pktl = av_mallocz(sizeof(AVPacketList));
1717 /* add the packet in the buffered packet list */
1719 ppktl = &pktl->next;
1722 if (ic->format->read_packet(ic, pkt) < 0) {
1726 st = ic->streams[pkt->stream_index];
1728 /* decode the data and update codec parameters */
1732 switch(st->codec.codec_type) {
1733 case CODEC_TYPE_VIDEO:
1734 ret = avcodec_decode_video(&st->codec, &picture, &got_picture, ptr, size);
1736 case CODEC_TYPE_AUDIO:
1737 ret = avcodec_decode_audio(&st->codec, samples, &got_picture, ptr, size);
1757 /* close each codec */
1758 for(i=0;i<ic->nb_streams;i++) {
1759 st = ic->streams[i];
1760 avcodec_close(&st->codec);
1766 int filename_number_test(const char *filename)
1770 if (get_frame_filename(buf, sizeof(buf), filename, 1) < 0) {
1771 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1772 "Use '%%d' to specify the image number:\n"
1773 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1774 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1782 void opt_input_file(const char *filename)
1784 AVFormatContext *ic;
1785 AVFormatParameters params, *ap = ¶ms;
1786 URLFormat url_format;
1790 ic = av_mallocz(sizeof(AVFormatContext));
1791 strcpy(ic->filename, filename);
1792 /* first format guess to know if we must open file */
1795 fmt = guess_format(NULL, filename, NULL);
1797 if (fmt == NULL || !(fmt->flags & AVFMT_NOFILE)) {
1799 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0) {
1800 fprintf(stderr, "Could not open '%s'\n", filename);
1804 /* find format and set default parameters */
1806 err = url_getformat(url_fileno(&ic->pb), &url_format);
1809 fmt = guess_format(url_format.format_name, NULL, NULL);
1810 ap->sample_rate = url_format.sample_rate;
1811 ap->frame_rate = url_format.frame_rate;
1812 ap->channels = url_format.channels;
1813 ap->width = url_format.width;
1814 ap->height = url_format.height;
1815 ap->pix_fmt = url_format.pix_fmt;
1818 fmt = guess_format(NULL, filename, NULL);
1819 memset(ap, 0, sizeof(*ap));
1822 memset(ap, 0, sizeof(*ap));
1825 if (!fmt || !fmt->read_header) {
1826 fprintf(stderr, "%s: Unknown file format\n", filename);
1831 /* get default parameters from command line */
1832 if (!ap->sample_rate)
1833 ap->sample_rate = audio_sample_rate;
1835 ap->channels = audio_channels;
1837 if (!ap->frame_rate)
1838 ap->frame_rate = frame_rate;
1840 ap->width = frame_width;
1842 ap->height = frame_height;
1844 /* check filename in case of an image number is expected */
1845 if (ic->format->flags & AVFMT_NEEDNUMBER) {
1846 if (filename_number_test(ic->filename) < 0)
1850 err = ic->format->read_header(ic, ap);
1852 fprintf(stderr, "%s: Error while parsing header\n", filename);
1856 /* If not enough info for the codecs, we decode the first frames
1857 to get it. (used in mpeg case for example) */
1858 ret = find_codec_parameters(ic);
1860 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1864 /* update the current parameters so that they match the one of the input stream */
1865 for(i=0;i<ic->nb_streams;i++) {
1866 AVCodecContext *enc = &ic->streams[i]->codec;
1867 switch(enc->codec_type) {
1868 case CODEC_TYPE_AUDIO:
1869 audio_channels = enc->channels;
1870 audio_sample_rate = enc->sample_rate;
1872 case CODEC_TYPE_VIDEO:
1873 frame_height = enc->height;
1874 frame_width = enc->width;
1875 frame_rate = enc->frame_rate;
1880 input_files[nb_input_files] = ic;
1881 /* dump the file content */
1882 dump_format(ic, nb_input_files, filename, 0);
1887 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1889 int has_video, has_audio, i, j;
1890 AVFormatContext *ic;
1894 for(j=0;j<nb_input_files;j++) {
1895 ic = input_files[j];
1896 for(i=0;i<ic->nb_streams;i++) {
1897 AVCodecContext *enc = &ic->streams[i]->codec;
1898 switch(enc->codec_type) {
1899 case CODEC_TYPE_AUDIO:
1902 case CODEC_TYPE_VIDEO:
1908 *has_video_ptr = has_video;
1909 *has_audio_ptr = has_audio;
1912 void opt_output_file(const char *filename)
1915 AVFormatContext *oc;
1916 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1919 if (!strcmp(filename, "-"))
1922 oc = av_mallocz(sizeof(AVFormatContext));
1925 file_format = guess_format(NULL, filename, NULL);
1927 file_format = &mpeg_mux_format;
1930 oc->format = file_format;
1932 if (!strcmp(file_format->name, "ffm") &&
1933 strstart(filename, "http:", NULL)) {
1934 /* special case for files sent to ffserver: we get the stream
1935 parameters from ffserver */
1936 if (read_ffserver_streams(oc, filename) < 0) {
1937 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1941 use_video = file_format->video_codec != CODEC_ID_NONE;
1942 use_audio = file_format->audio_codec != CODEC_ID_NONE;
1944 /* disable if no corresponding type found and at least one
1946 if (nb_input_files > 0) {
1947 check_audio_video_inputs(&input_has_video, &input_has_audio);
1948 if (!input_has_video)
1950 if (!input_has_audio)
1954 /* manual disable */
1955 if (audio_disable) {
1958 if (video_disable) {
1964 AVCodecContext *video_enc;
1966 st = av_mallocz(sizeof(AVStream));
1968 fprintf(stderr, "Could not alloc stream\n");
1971 video_enc = &st->codec;
1973 codec_id = file_format->video_codec;
1974 if (video_codec_id != CODEC_ID_NONE)
1975 codec_id = video_codec_id;
1977 video_enc->codec_id = codec_id;
1978 video_enc->codec_type = CODEC_TYPE_VIDEO;
1980 video_enc->bit_rate = video_bit_rate;
1981 video_enc->frame_rate = frame_rate;
1983 video_enc->width = frame_width;
1984 video_enc->height = frame_height;
1986 video_enc->gop_size = gop_size;
1988 video_enc->gop_size = 0;
1989 if (video_qscale || same_quality) {
1990 video_enc->flags |= CODEC_FLAG_QSCALE;
1991 video_enc->quality = video_qscale;
1993 /* XXX: need to find a way to set codec parameters */
1994 if (oc->format == &ppm_format ||
1995 oc->format == &ppmpipe_format) {
1996 video_enc->pix_fmt = PIX_FMT_RGB24;
1999 oc->streams[nb_streams] = st;
2004 AVCodecContext *audio_enc;
2006 st = av_mallocz(sizeof(AVStream));
2008 fprintf(stderr, "Could not alloc stream\n");
2011 audio_enc = &st->codec;
2012 codec_id = file_format->audio_codec;
2013 if (audio_codec_id != CODEC_ID_NONE)
2014 codec_id = audio_codec_id;
2015 audio_enc->codec_id = codec_id;
2016 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2018 audio_enc->bit_rate = audio_bit_rate;
2019 audio_enc->sample_rate = audio_sample_rate;
2020 audio_enc->channels = audio_channels;
2021 oc->streams[nb_streams] = st;
2025 oc->nb_streams = nb_streams;
2028 fprintf(stderr, "No audio or video streams available\n");
2033 nstrcpy(oc->title, sizeof(oc->title), str_title);
2035 nstrcpy(oc->author, sizeof(oc->author), str_author);
2037 nstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2039 nstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2042 output_files[nb_output_files] = oc;
2043 /* dump the file content */
2044 dump_format(oc, nb_output_files, filename, 1);
2047 strcpy(oc->filename, filename);
2049 /* check filename in case of an image number is expected */
2050 if (oc->format->flags & AVFMT_NEEDNUMBER) {
2051 if (filename_number_test(oc->filename) < 0)
2055 if (!(oc->format->flags & AVFMT_NOFILE)) {
2056 /* test if it already exists to avoid loosing precious files */
2057 if (!file_overwrite &&
2058 (strchr(filename, ':') == NULL ||
2059 strstart(filename, "file:", NULL))) {
2060 if (url_exist(filename)) {
2063 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2066 if (toupper(c) != 'Y') {
2067 fprintf(stderr, "Not overwriting - exiting\n");
2074 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2075 fprintf(stderr, "Could not open '%s'\n", filename);
2080 /* reset some options */
2084 audio_codec_id = CODEC_ID_NONE;
2085 video_codec_id = CODEC_ID_NONE;
2088 #ifndef CONFIG_WIN32
2089 INT64 getutime(void)
2091 struct rusage rusage;
2093 getrusage(RUSAGE_SELF, &rusage);
2094 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2097 INT64 getutime(void)
2103 void show_formats(void)
2110 printf("File formats:\n");
2111 printf(" Encoding:");
2112 for(f = first_format; f != NULL; f = f->next) {
2113 if (f->write_header)
2114 printf(" %s", f->name);
2117 printf(" Decoding:");
2118 for(f = first_format; f != NULL; f = f->next) {
2120 printf(" %s", f->name);
2124 printf("Codecs:\n");
2125 printf(" Encoders:");
2126 for(p = first_avcodec; p != NULL; p = p->next) {
2128 printf(" %s", p->name);
2132 printf(" Decoders:");
2133 for(p = first_avcodec; p != NULL; p = p->next) {
2135 printf(" %s", p->name);
2139 printf("Supported file protocols:");
2140 for(up = first_protocol; up != NULL; up = up->next)
2141 printf(" %s:", up->name);
2144 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2145 printf("Motion estimation methods:");
2149 if ((pp - motion_str) == ME_ZERO)
2150 printf("(fastest)");
2151 else if ((pp - motion_str) == ME_FULL)
2152 printf("(slowest)");
2153 else if ((pp - motion_str) == ME_LOG)
2154 printf("(default)");
2161 void show_help(void)
2163 const OptionDef *po;
2166 printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000,2001 Gerard Lantau\n"
2167 "usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2168 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n"
2170 "Main options are:\n");
2173 printf("\nAdvanced options are:\n");
2174 for(po = options; po->name != NULL; po++) {
2176 expert = (po->flags & OPT_EXPERT) != 0;
2178 strcpy(buf, po->name);
2179 if (po->flags & HAS_ARG) {
2181 strcat(buf, po->argname);
2183 printf("-%-17s %s\n", buf, po->help);
2191 const OptionDef options[] = {
2192 { "L", 0, {(void*)show_licence}, "show license" },
2193 { "h", 0, {(void*)show_help}, "show help" },
2194 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2195 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2196 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2197 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2198 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2199 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2200 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2201 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2202 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2203 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2205 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2206 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2207 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2208 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2209 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2210 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2211 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2213 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video device", "device" },
2215 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2216 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2218 { "sameq", OPT_BOOL, {(void*)&same_quality},
2219 "use same video quality as source (implies VBR)" },
2221 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2222 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2223 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2224 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2226 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2228 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2229 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2230 "deinterlace pictures" },
2231 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2232 "add timings for benchmarking" },
2237 int main(int argc, char **argv)
2240 const char *opt, *arg;
2241 const OptionDef *po;
2249 while (optindex < argc) {
2250 opt = argv[optindex++];
2252 if (opt[0] == '-' && opt[1] != '\0') {
2254 while (po->name != NULL) {
2255 if (!strcmp(opt + 1, po->name))
2260 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2264 if (po->flags & HAS_ARG)
2265 arg = argv[optindex++];
2266 if (po->flags & OPT_STRING) {
2269 *po->u.str_arg = str;
2270 } else if (po->flags & OPT_BOOL) {
2273 po->u.func_arg(arg);
2276 opt_output_file(opt);
2281 if (nb_input_files == 0) {
2283 if (nb_output_files != 1) {
2284 fprintf(stderr, "Only one output file supported when grabbing\n");
2287 av_grab(output_files[0]);
2289 fprintf(stderr, "Must supply at least one input file\n");
2294 if (nb_output_files <= 0) {
2295 fprintf(stderr, "Must supply at least one output file\n");
2299 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2300 stream_maps, nb_stream_maps);
2301 ti = getutime() - ti;
2303 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2308 for(i=0;i<nb_output_files;i++) {
2309 if (!(output_files[i]->format->flags & AVFMT_NOFILE))
2310 url_fclose(&output_files[i]->pb);
2312 for(i=0;i<nb_input_files;i++) {
2313 if (!(input_files[i]->format->flags & AVFMT_NOFILE))
2314 url_fclose(&input_files[i]->pb);