3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library 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 GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define HAVE_AV_CONFIG_H
26 #include <sys/ioctl.h>
29 #include <sys/resource.h>
39 #define MAXINT64 INT64_C(0x7fffffffffffffff)
44 #define HAS_ARG 0x0001
45 #define OPT_BOOL 0x0002
46 #define OPT_EXPERT 0x0004
47 #define OPT_STRING 0x0008
57 /* select an input stream for an output stream */
58 typedef struct AVStreamMap {
63 extern const OptionDef options[];
69 static AVFormatContext *input_files[MAX_FILES];
70 static int nb_input_files = 0;
72 static AVFormatContext *output_files[MAX_FILES];
73 static int nb_output_files = 0;
75 static AVStreamMap stream_maps[MAX_FILES];
76 static int nb_stream_maps;
78 static AVInputFormat *file_iformat;
79 static AVOutputFormat *file_oformat;
80 static int frame_width = 160;
81 static int frame_height = 128;
82 static int frame_rate = 25 * FRAME_RATE_BASE;
83 static int video_bit_rate = 200000;
84 static int video_bit_rate_tolerance = 200000;
85 static int video_qscale = 0;
86 static int video_qmin = 3;
87 static int video_qmax = 15;
88 static int video_qdiff = 3;
89 static float video_qblur = 0.5;
90 static float video_qcomp = 0.5;
91 static int me_method = 0;
92 static int video_disable = 0;
93 static int video_codec_id = CODEC_ID_NONE;
94 static int same_quality = 0;
95 static int b_frames = 0;
96 static int use_hq = 0;
97 static int use_4mv = 0;
98 static int do_deinterlace = 0;
99 static int workaround_bugs = 0;
101 static int gop_size = 12;
102 static int intra_only = 0;
103 static int audio_sample_rate = 44100;
104 static int audio_bit_rate = 64000;
105 static int audio_disable = 0;
106 static int audio_channels = 1;
107 static int audio_codec_id = CODEC_ID_NONE;
109 static INT64 recording_time = 0;
110 static int file_overwrite = 0;
111 static char *str_title = NULL;
112 static char *str_author = NULL;
113 static char *str_copyright = NULL;
114 static char *str_comment = NULL;
115 static int do_benchmark = 0;
116 static int do_hex_dump = 0;
117 static int do_play = 0;
118 static int do_psnr = 0;
119 static int do_vstats = 0;
120 static int mpeg_vcd = 0;
122 #ifndef CONFIG_AUDIO_OSS
123 const char *audio_device = "none";
125 #ifndef CONFIG_VIDEO4LINUX
126 const char *v4l_device = "none";
129 typedef struct AVOutputStream {
130 int file_index; /* file index */
131 int index; /* stream index in the output file */
132 int source_index; /* AVInputStream index */
133 AVStream *st; /* stream in the output file */
134 int encoding_needed; /* true if encoding needed for this stream */
137 AVPicture pict_tmp; /* temporary image for resizing */
139 ImgReSampleContext *img_resample_ctx; /* for image resampling */
143 ReSampleContext *resample; /* for audio resampling */
144 FifoBuffer fifo; /* for compression: one audio fifo per codec */
147 typedef struct AVInputStream {
151 int discard; /* true if stream data should be discarded */
152 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
153 Ticker pts_ticker; /* Ticker for PTS calculation */
154 int ticker_inited; /* to signal if the ticker was initialized */
155 INT64 pts; /* current pts */
156 int pts_increment; /* expected pts increment for next packet */
157 int frame_number; /* current frame */
158 INT64 sample_index; /* current sample */
161 typedef struct AVInputFile {
162 int eof_reached; /* true if eof reached */
163 int ist_index; /* index of first stream in ist_table */
164 int buffer_size; /* current total buffer size */
165 int buffer_size_max; /* buffer size at which we consider we can stop
167 int nb_streams; /* nb streams we are aware of */
172 /* init terminal so that we can grab keys */
173 static struct termios oldtty;
175 static void term_exit(void)
177 tcsetattr (0, TCSANOW, &oldtty);
180 static void term_init(void)
187 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
188 |INLCR|IGNCR|ICRNL|IXON);
189 tty.c_oflag |= OPOST;
190 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
191 tty.c_cflag &= ~(CSIZE|PARENB);
196 tcsetattr (0, TCSANOW, &tty);
201 /* read a key without blocking */
202 static int read_key(void)
213 n = select(1, &rfds, NULL, NULL, &tv);
226 /* no interactive support */
227 static void term_exit(void)
231 static void term_init(void)
235 static int read_key(void)
242 int read_ffserver_streams(AVFormatContext *s, const char *filename)
247 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
250 /* copy stream format */
251 s->nb_streams = ic->nb_streams;
252 for(i=0;i<ic->nb_streams;i++) {
254 st = av_mallocz(sizeof(AVFormatContext));
255 memcpy(st, ic->streams[i], sizeof(AVStream));
259 av_close_input_file(ic);
263 #define MAX_AUDIO_PACKET_SIZE 16384
265 static void do_audio_out(AVFormatContext *s,
268 unsigned char *buf, int size)
271 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
272 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
273 int size_out, frame_bytes, ret;
276 enc = &ost->st->codec;
278 if (ost->audio_resample) {
280 size_out = audio_resample(ost->resample,
281 (short *)buftmp, (short *)buf,
282 size / (ist->st->codec.channels * 2));
283 size_out = size_out * enc->channels * 2;
289 /* now encode as many frames as possible */
290 if (enc->frame_size > 1) {
291 /* output resampled raw samples */
292 fifo_write(&ost->fifo, buftmp, size_out,
295 frame_bytes = enc->frame_size * 2 * enc->channels;
297 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
298 &ost->fifo.rptr) == 0) {
299 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
301 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
304 /* output a pcm frame */
305 /* XXX: change encoding codec API to avoid this ? */
306 switch(enc->codec->id) {
307 case CODEC_ID_PCM_S16LE:
308 case CODEC_ID_PCM_S16BE:
309 case CODEC_ID_PCM_U16LE:
310 case CODEC_ID_PCM_U16BE:
313 size_out = size_out >> 1;
316 ret = avcodec_encode_audio(enc, audio_out, size_out,
318 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
322 /* write a picture to a raw mux */
323 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
324 int pix_fmt, int w, int h)
326 UINT8 *buf, *src, *dest;
329 /* XXX: not efficient, should add test if we can take
330 directly the AVPicture */
332 case PIX_FMT_YUV420P:
333 size = avpicture_get_size(pix_fmt, w, h);
334 buf = av_malloc(size);
343 src = picture->data[i];
345 memcpy(dest, src, w);
347 src += picture->linesize[i];
351 case PIX_FMT_YUV422P:
353 buf = av_malloc(size);
361 src = picture->data[i];
363 memcpy(dest, src, w);
365 src += picture->linesize[i];
369 case PIX_FMT_YUV444P:
371 buf = av_malloc(size);
376 src = picture->data[i];
378 memcpy(dest, src, w);
380 src += picture->linesize[i];
386 buf = av_malloc(size);
390 src = picture->data[0];
392 memcpy(dest, src, w * 2);
394 src += picture->linesize[0];
400 buf = av_malloc(size);
404 src = picture->data[0];
406 memcpy(dest, src, w * 3);
408 src += picture->linesize[0];
414 s->oformat->write_packet(s, index, buf, size, 0);
419 static void do_video_out(AVFormatContext *s,
425 int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
426 AVPicture *picture, *picture2, *pict;
427 AVPicture picture_tmp1, picture_tmp2;
428 static UINT8 *video_buffer;
429 UINT8 *buf = NULL, *buf1 = NULL;
430 AVCodecContext *enc, *dec;
432 #define VIDEO_BUFFER_SIZE (1024*1024)
434 enc = &ost->st->codec;
435 dec = &ist->st->codec;
437 frame_number = ist->frame_number;
438 dec_frame_rate = ist->st->r_frame_rate;
439 // fprintf(stderr, "\n%d", dec_frame_rate);
440 /* first drop frame if needed */
441 n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
442 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
448 video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
449 if(!video_buffer) return;
451 /* deinterlace : must be done before any resize */
452 if (do_deinterlace) {
455 /* create temporary picture */
456 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
457 buf1 = av_malloc(size);
461 picture2 = &picture_tmp2;
462 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
464 if (avpicture_deinterlace(picture2, picture1,
465 dec->pix_fmt, dec->width, dec->height) < 0) {
466 /* if error, do not deinterlace */
475 /* convert pixel format if needed */
476 if (enc->pix_fmt != dec->pix_fmt) {
479 /* create temporary picture */
480 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
481 buf = av_malloc(size);
484 pict = &picture_tmp1;
485 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
487 if (img_convert(pict, enc->pix_fmt,
488 picture2, dec->pix_fmt,
489 dec->width, dec->height) < 0) {
490 fprintf(stderr, "pixel format conversion not handled\n");
497 /* XXX: resampling could be done before raw format convertion in
498 some cases to go faster */
499 /* XXX: only works for YUV420P */
500 if (ost->video_resample) {
501 picture = &ost->pict_tmp;
502 img_resample(ost->img_resample_ctx, picture, pict);
507 /* duplicates frame if needed */
508 /* XXX: pb because no interleaving */
510 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
511 /* handles sameq here. This is not correct because it may
512 not be a global option */
514 enc->quality = dec->quality;
517 ret = avcodec_encode_video(enc,
518 video_buffer, VIDEO_BUFFER_SIZE,
520 //enc->frame_number = enc->real_pict_num;
521 s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
523 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
524 // enc->frame_number-1, enc->real_pict_num, ret,
527 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
535 static void do_video_stats(AVOutputStream *ost,
539 static FILE *fvstats=NULL;
540 static INT64 total_size = 0;
547 double ti1, bitrate, avg_bitrate;
551 today = localtime(&today2);
552 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
555 fvstats = fopen(filename,"w");
563 enc = &ost->st->codec;
564 total_size += frame_size;
565 if (enc->codec_type == CODEC_TYPE_VIDEO) {
566 frame_number = ist->frame_number;
567 fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
569 fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
571 fprintf(fvstats,"f_size= %6d ", frame_size);
572 /* compute min pts value */
573 if (!ist->discard && ist->pts < ti) {
576 ti1 = (double)ti / 1000000.0;
580 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
581 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
582 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
583 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
584 fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
592 * The following code is the main loop of the file converter
594 static int av_encode(AVFormatContext **output_files,
596 AVFormatContext **input_files,
598 AVStreamMap *stream_maps, int nb_stream_maps)
600 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
601 AVFormatContext *is, *os;
602 AVCodecContext *codec, *icodec;
603 AVOutputStream *ost, **ost_table = NULL;
604 AVInputStream *ist, **ist_table = NULL;
605 INT64 min_pts, start_time;
606 AVInputFile *file_table;
607 AVFormatContext *stream_no_data;
610 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
614 /* input stream init */
616 for(i=0;i<nb_input_files;i++) {
618 file_table[i].ist_index = j;
619 file_table[i].nb_streams = is->nb_streams;
624 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
628 for(i=0;i<nb_istreams;i++) {
629 ist = av_mallocz(sizeof(AVInputStream));
635 for(i=0;i<nb_input_files;i++) {
637 for(k=0;k<is->nb_streams;k++) {
638 ist = ist_table[j++];
639 ist->st = is->streams[k];
642 ist->discard = 1; /* the stream is discarded by default
647 /* output stream init */
649 for(i=0;i<nb_output_files;i++) {
650 os = output_files[i];
651 nb_ostreams += os->nb_streams;
653 os->flags |= AVF_FLAG_VCD;
655 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
656 fprintf(stderr, "Number of stream maps must match number of output streams\n");
660 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
663 for(i=0;i<nb_ostreams;i++) {
664 ost = av_mallocz(sizeof(AVOutputStream));
671 for(k=0;k<nb_output_files;k++) {
672 os = output_files[k];
673 for(i=0;i<os->nb_streams;i++) {
675 ost = ost_table[n++];
678 ost->st = os->streams[i];
679 if (nb_stream_maps > 0) {
680 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
681 stream_maps[n-1].stream_index;
683 /* get corresponding input stream index : we select the first one with the right type */
685 for(j=0;j<nb_istreams;j++) {
688 ist->st->codec.codec_type == ost->st->codec.codec_type) {
689 ost->source_index = j;
695 /* try again and reuse existing stream */
696 for(j=0;j<nb_istreams;j++) {
698 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
699 ost->source_index = j;
704 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
705 ost->file_index, ost->index);
710 ist = ist_table[ost->source_index];
715 /* dump the stream mapping */
716 fprintf(stderr, "Stream mapping:\n");
717 for(i=0;i<nb_ostreams;i++) {
719 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
720 ist_table[ost->source_index]->file_index,
721 ist_table[ost->source_index]->index,
726 /* for each output stream, we compute the right encoding parameters */
727 for(i=0;i<nb_ostreams;i++) {
729 ist = ist_table[ost->source_index];
731 codec = &ost->st->codec;
732 icodec = &ist->st->codec;
734 switch(codec->codec_type) {
735 case CODEC_TYPE_AUDIO:
736 /* check if same codec with same parameters. If so, no
737 reencoding is needed */
738 if (codec->codec_id == icodec->codec_id &&
739 codec->bit_rate == icodec->bit_rate &&
740 codec->sample_rate == icodec->sample_rate &&
741 codec->channels == icodec->channels) {
743 /* use the same frame size */
744 codec->frame_size = icodec->frame_size;
745 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
747 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
749 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
752 if (codec->channels == icodec->channels &&
753 codec->sample_rate == icodec->sample_rate) {
754 ost->audio_resample = 0;
756 if (codec->channels != icodec->channels &&
757 icodec->codec_id == CODEC_ID_AC3) {
758 /* Special case for 5:1 AC3 input */
759 /* and mono or stereo output */
760 /* Request specific number of channels */
761 icodec->channels = codec->channels;
762 if (codec->sample_rate == icodec->sample_rate)
763 ost->audio_resample = 0;
765 ost->audio_resample = 1;
766 ost->resample = audio_resample_init(codec->channels, icodec->channels,
768 icodec->sample_rate);
770 /* Request specific number of channels */
771 icodec->channels = codec->channels;
773 ost->audio_resample = 1;
774 ost->resample = audio_resample_init(codec->channels, icodec->channels,
776 icodec->sample_rate);
779 ist->decoding_needed = 1;
780 ost->encoding_needed = 1;
783 case CODEC_TYPE_VIDEO:
784 /* check if same codec with same parameters. If so, no
785 reencoding is needed */
786 if (codec->codec_id == icodec->codec_id &&
787 codec->bit_rate == icodec->bit_rate &&
788 codec->frame_rate == icodec->frame_rate &&
789 codec->width == icodec->width &&
790 codec->height == icodec->height) {
793 if (codec->width == icodec->width &&
794 codec->height == icodec->height) {
795 ost->video_resample = 0;
798 ost->video_resample = 1;
799 buf = av_malloc((codec->width * codec->height * 3) / 2);
802 ost->pict_tmp.data[0] = buf;
803 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
804 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
805 ost->pict_tmp.linesize[0] = codec->width;
806 ost->pict_tmp.linesize[1] = codec->width / 2;
807 ost->pict_tmp.linesize[2] = codec->width / 2;
809 ost->img_resample_ctx = img_resample_init(
810 ost->st->codec.width, ost->st->codec.height,
811 ist->st->codec.width, ist->st->codec.height);
813 ost->encoding_needed = 1;
814 ist->decoding_needed = 1;
822 /* open each encoder */
823 for(i=0;i<nb_ostreams;i++) {
825 if (ost->encoding_needed) {
827 codec = avcodec_find_encoder(ost->st->codec.codec_id);
829 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
830 ost->file_index, ost->index);
833 if (avcodec_open(&ost->st->codec, codec) < 0) {
834 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
835 ost->file_index, ost->index);
841 /* open each decoder */
842 for(i=0;i<nb_istreams;i++) {
844 if (ist->decoding_needed) {
846 codec = avcodec_find_decoder(ist->st->codec.codec_id);
848 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
849 ist->file_index, ist->index);
852 if (avcodec_open(&ist->st->codec, codec) < 0) {
853 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
854 ist->file_index, ist->index);
857 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
858 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
863 for(i=0;i<nb_istreams;i++) {
866 ist->frame_number = 0;
869 /* compute buffer size max (should use a complete heuristic) */
870 for(i=0;i<nb_input_files;i++) {
871 file_table[i].buffer_size_max = 2048;
874 /* open files and write file headers */
875 for(i=0;i<nb_output_files;i++) {
876 os = output_files[i];
877 if (av_write_header(os) < 0) {
878 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
886 fprintf(stderr, "Press [q] to stop encoding\n");
888 fprintf(stderr, "Press [q] to stop playing\n");
893 start_time = gettime();
899 int file_index, ist_index;
904 int data_size, got_picture;
906 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
909 /* if 'q' pressed, exits */
911 /* read_key() returns 0 on EOF */
917 /* select the input file with the smallest pts */
920 for(i=0;i<nb_istreams;i++) {
922 /* For some reason, the pts_increment code breaks q estimation?!? */
923 if (!ist->discard && !file_table[ist->file_index].eof_reached &&
924 ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
925 min_pts = ist->pts /* + ist->pts_increment */;
926 file_index = ist->file_index;
929 /* if none, if is finished */
930 if (file_index < 0) {
931 if (stream_no_data) {
937 ts.tv_nsec = 1000 * 1000 * 10;
940 snooze(10 * 1000); /* mmu_man */ /* in microsec */
948 /* finish if recording time exhausted */
949 if (recording_time > 0 && min_pts >= recording_time)
951 /* read a packet from it and output it in the fifo */
952 is = input_files[file_index];
953 if (av_read_packet(is, &pkt) < 0) {
954 file_table[file_index].eof_reached = 1;
962 /* the following test is needed in case new streams appear
963 dynamically in stream : we ignore them */
964 if (pkt.stream_index >= file_table[file_index].nb_streams)
966 ist_index = file_table[file_index].ist_index + pkt.stream_index;
967 ist = ist_table[ist_index];
971 if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
975 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
976 av_hex_dump(pkt.data, pkt.size);
979 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
985 /* decode the packet if needed */
986 data_buf = NULL; /* fail safe */
988 if (ist->decoding_needed) {
989 switch(ist->st->codec.codec_type) {
990 case CODEC_TYPE_AUDIO:
991 /* XXX: could avoid copy if PCM 16 bits with same
993 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
997 /* Some bug in mpeg audio decoder gives */
998 /* data_size < 0, it seems they are overflows */
999 if (data_size <= 0) {
1000 /* no audio frame */
1005 data_buf = (UINT8 *)samples;
1007 case CODEC_TYPE_VIDEO:
1008 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1010 size = (ist->st->codec.width * ist->st->codec.height);
1011 avpicture_fill(&picture, ptr,
1012 ist->st->codec.pix_fmt,
1013 ist->st->codec.width,
1014 ist->st->codec.height);
1017 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1018 ret = avcodec_decode_video(&ist->st->codec,
1019 &picture, &got_picture, ptr, len);
1022 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1023 ist->file_index, ist->index);
1024 av_free_packet(&pkt);
1028 /* no picture yet */
1045 if (!ist->ticker_inited) {
1046 switch (ist->st->codec.codec_type) {
1047 case CODEC_TYPE_AUDIO:
1048 ticker_init(&ist->pts_ticker,
1049 (INT64)ist->st->codec.sample_rate,
1051 ist->ticker_inited = 1;
1053 case CODEC_TYPE_VIDEO:
1054 ticker_init(&ist->pts_ticker,
1055 (INT64)ist->st->r_frame_rate,
1056 ((INT64)1000000 * FRAME_RATE_BASE));
1057 ist->ticker_inited = 1;
1064 switch(ist->st->codec.codec_type) {
1065 case CODEC_TYPE_AUDIO:
1066 //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1067 ist->pts = ticker_abs(&ist->pts_ticker, ist->sample_index);
1068 ist->sample_index += data_size / (2 * ist->st->codec.channels);
1069 ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
1071 case CODEC_TYPE_VIDEO:
1072 ist->frame_number++;
1073 //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1074 // ist->st->codec.frame_rate;
1075 ist->pts = ticker_abs(&ist->pts_ticker, ist->frame_number);
1076 ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
1077 ist->st->codec.frame_rate;
1085 /* transcode raw format, encode packets and output them */
1087 for(i=0;i<nb_ostreams;i++) {
1090 if (ost->source_index == ist_index) {
1091 os = output_files[ost->file_index];
1093 if (ost->encoding_needed) {
1094 switch(ost->st->codec.codec_type) {
1095 case CODEC_TYPE_AUDIO:
1096 do_audio_out(os, ost, ist, data_buf, data_size);
1098 case CODEC_TYPE_VIDEO:
1099 do_video_out(os, ost, ist, &picture, &frame_size);
1101 do_video_stats(ost, ist, frame_size);
1107 /* no reencoding needed : output the packet directly */
1108 /* force the input stream PTS */
1109 os->oformat->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
1115 av_free_packet(&pkt);
1117 /* dump report by using the first video and audio streams */
1120 AVFormatContext *oc;
1121 INT64 total_size, ti;
1122 AVCodecContext *enc;
1123 int frame_number, vid;
1124 double bitrate, ti1;
1125 static INT64 last_time;
1127 if ((min_pts - last_time) >= 500000) {
1128 last_time = min_pts;
1130 oc = output_files[0];
1132 total_size = url_ftell(&oc->pb);
1137 for(i=0;i<nb_ostreams;i++) {
1139 enc = &ost->st->codec;
1140 ist = ist_table[ost->source_index];
1141 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1142 frame_number = ist->frame_number;
1143 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1144 frame_number, enc->quality);
1146 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1149 /* compute min pts value */
1150 if (!ist->discard && ist->pts < ti) {
1155 ti1 = (double)ti / 1000000.0;
1158 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1160 sprintf(buf + strlen(buf),
1161 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1162 (double)total_size / 1024, ti1, bitrate);
1164 fprintf(stderr, "%s \r", buf);
1171 /* dump report by using the first video and audio streams */
1174 AVFormatContext *oc;
1175 INT64 total_size, ti;
1176 AVCodecContext *enc;
1177 int frame_number, vid;
1178 double bitrate, ti1;
1180 oc = output_files[0];
1182 total_size = url_ftell(&oc->pb);
1187 for(i=0;i<nb_ostreams;i++) {
1189 enc = &ost->st->codec;
1190 ist = ist_table[ost->source_index];
1191 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1192 frame_number = ist->frame_number;
1193 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1194 frame_number, enc->quality);
1196 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1199 /* compute min pts value */
1200 if (!ist->discard && ist->pts < ti) {
1205 ti1 = ti / 1000000.0;
1208 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1210 sprintf(buf + strlen(buf),
1211 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1212 (double)total_size / 1024, ti1, bitrate);
1214 fprintf(stderr, "%s \n", buf);
1216 /* close each encoder */
1217 for(i=0;i<nb_ostreams;i++) {
1219 if (ost->encoding_needed) {
1220 avcodec_close(&ost->st->codec);
1224 /* close each decoder */
1225 for(i=0;i<nb_istreams;i++) {
1227 if (ist->decoding_needed) {
1228 avcodec_close(&ist->st->codec);
1233 /* write the trailer if needed and close file */
1234 for(i=0;i<nb_output_files;i++) {
1235 os = output_files[i];
1236 av_write_trailer(os);
1242 av_free(file_table);
1245 for(i=0;i<nb_istreams;i++) {
1252 for(i=0;i<nb_ostreams;i++) {
1255 fifo_free(&ost->fifo); /* works even if fifo is not
1256 initialized but set to zero */
1257 av_free(ost->pict_tmp.data[0]);
1258 if (ost->video_resample)
1259 img_resample_close(ost->img_resample_ctx);
1260 if (ost->audio_resample)
1261 audio_resample_close(ost->resample);
1274 int file_read(const char *filename)
1277 unsigned char buffer[1024];
1280 if (url_open(&h, filename, O_RDONLY) < 0) {
1281 printf("could not open '%s'\n", filename);
1285 len = url_read(h, buffer, sizeof(buffer));
1288 for(i=0;i<len;i++) putchar(buffer[i]);
1295 void show_licence(void)
1298 "ffmpeg version " FFMPEG_VERSION "\n"
1299 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1300 "This library is free software; you can redistribute it and/or\n"
1301 "modify it under the terms of the GNU Lesser General Public\n"
1302 "License as published by the Free Software Foundation; either\n"
1303 "version 2 of the License, or (at your option) any later version.\n"
1305 "This library is distributed in the hope that it will be useful,\n"
1306 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1307 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1308 "Lesser General Public License for more details.\n"
1310 "You should have received a copy of the GNU Lesser General Public\n"
1311 "License along with this library; if not, write to the Free Software\n"
1312 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1317 void opt_format(const char *arg)
1319 file_iformat = av_find_input_format(arg);
1320 file_oformat = guess_format(arg, NULL, NULL);
1321 if (!file_iformat && !file_oformat) {
1322 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1327 void opt_video_bitrate(const char *arg)
1329 video_bit_rate = atoi(arg) * 1000;
1332 void opt_video_bitrate_tolerance(const char *arg)
1334 video_bit_rate_tolerance = atoi(arg) * 1000;
1337 void opt_workaround_bugs(const char *arg)
1339 workaround_bugs = atoi(arg);
1342 void opt_frame_rate(const char *arg)
1344 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1347 void opt_frame_size(const char *arg)
1349 parse_image_size(&frame_width, &frame_height, arg);
1350 if (frame_width <= 0 || frame_height <= 0) {
1351 fprintf(stderr, "Incorrect frame size\n");
1354 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1355 fprintf(stderr, "Frame size must be a multiple of 2\n");
1360 void opt_gop_size(const char *arg)
1362 gop_size = atoi(arg);
1365 void opt_b_frames(const char *arg)
1367 b_frames = atoi(arg);
1368 if (b_frames > FF_MAX_B_FRAMES) {
1369 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1371 } else if (b_frames < 1) {
1372 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1377 void opt_qscale(const char *arg)
1379 video_qscale = atoi(arg);
1380 if (video_qscale < 0 ||
1381 video_qscale > 31) {
1382 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1387 void opt_qmin(const char *arg)
1389 video_qmin = atoi(arg);
1390 if (video_qmin < 0 ||
1392 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1397 void opt_qmax(const char *arg)
1399 video_qmax = atoi(arg);
1400 if (video_qmax < 0 ||
1402 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1407 void opt_qdiff(const char *arg)
1409 video_qdiff = atoi(arg);
1410 if (video_qdiff < 0 ||
1412 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1417 void opt_qblur(const char *arg)
1419 video_qblur = atof(arg);
1422 void opt_qcomp(const char *arg)
1424 video_qcomp = atof(arg);
1427 void opt_audio_bitrate(const char *arg)
1429 audio_bit_rate = atoi(arg) * 1000;
1432 void opt_audio_rate(const char *arg)
1434 audio_sample_rate = atoi(arg);
1437 void opt_audio_channels(const char *arg)
1439 audio_channels = atoi(arg);
1442 void opt_video_device(const char *arg)
1444 v4l_device = strdup(arg);
1447 void opt_audio_device(const char *arg)
1449 audio_device = strdup(arg);
1452 void opt_audio_codec(const char *arg)
1458 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1463 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1466 audio_codec_id = p->id;
1470 const char *motion_str[] = {
1480 void opt_motion_estimation(const char *arg)
1486 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1489 if (!strcmp(*p, arg))
1493 me_method = (p - motion_str) + 1;
1496 void opt_video_codec(const char *arg)
1502 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1507 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1510 video_codec_id = p->id;
1514 void opt_map(const char *arg)
1520 m = &stream_maps[nb_stream_maps++];
1522 m->file_index = strtol(arg, (char **)&p, 0);
1526 m->stream_index = strtol(p, (char **)&p, 0);
1529 void opt_recording_time(const char *arg)
1531 recording_time = parse_date(arg, 1);
1534 void print_error(const char *filename, int err)
1537 case AVERROR_NUMEXPECTED:
1538 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1539 "Use '%%d' to specify the image number:\n"
1540 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1541 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1544 case AVERROR_INVALIDDATA:
1545 fprintf(stderr, "%s: Error while parsing header\n", filename);
1548 fprintf(stderr, "%s: Unknown format\n", filename);
1551 fprintf(stderr, "%s: Error while opening file\n", filename);
1556 void opt_input_file(const char *filename)
1558 AVFormatContext *ic;
1559 AVFormatParameters params, *ap = ¶ms;
1560 int err, i, ret, rfps;
1562 /* get default parameters from command line */
1563 memset(ap, 0, sizeof(*ap));
1564 ap->sample_rate = audio_sample_rate;
1565 ap->channels = audio_channels;
1566 ap->frame_rate = frame_rate;
1567 ap->width = frame_width;
1568 ap->height = frame_height;
1570 /* open the input file with generic libav function */
1571 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
1573 print_error(filename, err);
1577 /* If not enough info to get the stream parameters, we decode the
1578 first frames to get it. (used in mpeg case for example) */
1579 ret = av_find_stream_info(ic);
1581 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1585 /* update the current parameters so that they match the one of the input stream */
1586 for(i=0;i<ic->nb_streams;i++) {
1587 AVCodecContext *enc = &ic->streams[i]->codec;
1588 switch(enc->codec_type) {
1589 case CODEC_TYPE_AUDIO:
1590 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1591 audio_channels = enc->channels;
1592 audio_sample_rate = enc->sample_rate;
1594 case CODEC_TYPE_VIDEO:
1595 frame_height = enc->height;
1596 frame_width = enc->width;
1597 rfps = ic->streams[i]->r_frame_rate;
1598 enc->workaround_bugs = workaround_bugs;
1599 if (enc->frame_rate != rfps) {
1600 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
1601 i, (float)enc->frame_rate / FRAME_RATE_BASE,
1602 (float)rfps / FRAME_RATE_BASE);
1604 /* update the current frame rate to match the stream frame rate */
1612 input_files[nb_input_files] = ic;
1613 /* dump the file content */
1614 dump_format(ic, nb_input_files, filename, 0);
1616 file_iformat = NULL;
1617 file_oformat = NULL;
1620 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1622 int has_video, has_audio, i, j;
1623 AVFormatContext *ic;
1627 for(j=0;j<nb_input_files;j++) {
1628 ic = input_files[j];
1629 for(i=0;i<ic->nb_streams;i++) {
1630 AVCodecContext *enc = &ic->streams[i]->codec;
1631 switch(enc->codec_type) {
1632 case CODEC_TYPE_AUDIO:
1635 case CODEC_TYPE_VIDEO:
1643 *has_video_ptr = has_video;
1644 *has_audio_ptr = has_audio;
1647 void opt_output_file(const char *filename)
1650 AVFormatContext *oc;
1651 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1654 if (!strcmp(filename, "-"))
1657 oc = av_mallocz(sizeof(AVFormatContext));
1659 if (!file_oformat) {
1660 file_oformat = guess_format(NULL, filename, NULL);
1661 if (!file_oformat) {
1662 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
1668 oc->oformat = file_oformat;
1670 if (!strcmp(file_oformat->name, "ffm") &&
1671 strstart(filename, "http:", NULL)) {
1672 /* special case for files sent to ffserver: we get the stream
1673 parameters from ffserver */
1674 if (read_ffserver_streams(oc, filename) < 0) {
1675 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1679 use_video = file_oformat->video_codec != CODEC_ID_NONE;
1680 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
1682 /* disable if no corresponding type found and at least one
1684 if (nb_input_files > 0) {
1685 check_audio_video_inputs(&input_has_video, &input_has_audio);
1686 if (!input_has_video)
1688 if (!input_has_audio)
1692 /* manual disable */
1693 if (audio_disable) {
1696 if (video_disable) {
1702 AVCodecContext *video_enc;
1704 st = av_mallocz(sizeof(AVStream));
1706 fprintf(stderr, "Could not alloc stream\n");
1709 video_enc = &st->codec;
1711 codec_id = file_oformat->video_codec;
1712 if (video_codec_id != CODEC_ID_NONE)
1713 codec_id = video_codec_id;
1715 video_enc->codec_id = codec_id;
1716 video_enc->codec_type = CODEC_TYPE_VIDEO;
1718 video_enc->bit_rate = video_bit_rate;
1719 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
1720 video_enc->frame_rate = frame_rate;
1722 video_enc->width = frame_width;
1723 video_enc->height = frame_height;
1725 video_enc->gop_size = gop_size;
1727 video_enc->gop_size = 0;
1728 if (video_qscale || same_quality) {
1729 video_enc->flags |= CODEC_FLAG_QSCALE;
1730 video_enc->quality = video_qscale;
1734 video_enc->flags |= CODEC_FLAG_HQ;
1738 video_enc->flags |= CODEC_FLAG_HQ;
1739 video_enc->flags |= CODEC_FLAG_4MV;
1743 if (codec_id != CODEC_ID_MPEG4) {
1744 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
1747 video_enc->max_b_frames = b_frames;
1748 video_enc->b_frame_strategy = 0;
1749 video_enc->b_quant_factor = 2.0;
1752 video_enc->qmin = video_qmin;
1753 video_enc->qmax = video_qmax;
1754 video_enc->max_qdiff = video_qdiff;
1755 video_enc->qblur = video_qblur;
1756 video_enc->qcompress = video_qcomp;
1759 video_enc->get_psnr = 1;
1761 video_enc->get_psnr = 0;
1763 video_enc->me_method = me_method;
1765 /* XXX: need to find a way to set codec parameters */
1766 if (oc->oformat->flags & AVFMT_RGB24) {
1767 video_enc->pix_fmt = PIX_FMT_RGB24;
1770 oc->streams[nb_streams] = st;
1775 AVCodecContext *audio_enc;
1777 st = av_mallocz(sizeof(AVStream));
1779 fprintf(stderr, "Could not alloc stream\n");
1782 audio_enc = &st->codec;
1783 codec_id = file_oformat->audio_codec;
1784 if (audio_codec_id != CODEC_ID_NONE)
1785 codec_id = audio_codec_id;
1786 audio_enc->codec_id = codec_id;
1787 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1789 audio_enc->bit_rate = audio_bit_rate;
1790 audio_enc->sample_rate = audio_sample_rate;
1791 /* For audio codecs other than AC3 we limit */
1792 /* the number of coded channels to stereo */
1793 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
1794 audio_enc->channels = 2;
1796 audio_enc->channels = audio_channels;
1797 oc->streams[nb_streams] = st;
1801 oc->nb_streams = nb_streams;
1804 fprintf(stderr, "No audio or video streams available\n");
1809 pstrcpy(oc->title, sizeof(oc->title), str_title);
1811 pstrcpy(oc->author, sizeof(oc->author), str_author);
1813 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1815 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1818 output_files[nb_output_files] = oc;
1819 /* dump the file content */
1820 dump_format(oc, nb_output_files, filename, 1);
1823 strcpy(oc->filename, filename);
1825 /* check filename in case of an image number is expected */
1826 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
1827 if (filename_number_test(oc->filename) < 0) {
1828 print_error(oc->filename, AVERROR_NUMEXPECTED);
1833 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
1834 /* test if it already exists to avoid loosing precious files */
1835 if (!file_overwrite &&
1836 (strchr(filename, ':') == NULL ||
1837 strstart(filename, "file:", NULL))) {
1838 if (url_exist(filename)) {
1841 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1844 if (toupper(c) != 'Y') {
1845 fprintf(stderr, "Not overwriting - exiting\n");
1852 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1853 fprintf(stderr, "Could not open '%s'\n", filename);
1858 /* reset some options */
1859 file_oformat = NULL;
1860 file_iformat = NULL;
1863 audio_codec_id = CODEC_ID_NONE;
1864 video_codec_id = CODEC_ID_NONE;
1867 /* prepare dummy protocols for grab */
1868 void prepare_grab(void)
1870 int has_video, has_audio, i, j;
1871 AVFormatContext *oc;
1872 AVFormatContext *ic;
1873 AVFormatParameters ap1, *ap = &ap1;
1875 /* see if audio/video inputs are needed */
1878 memset(ap, 0, sizeof(*ap));
1879 for(j=0;j<nb_output_files;j++) {
1880 oc = output_files[j];
1881 for(i=0;i<oc->nb_streams;i++) {
1882 AVCodecContext *enc = &oc->streams[i]->codec;
1883 switch(enc->codec_type) {
1884 case CODEC_TYPE_AUDIO:
1885 if (enc->sample_rate > ap->sample_rate)
1886 ap->sample_rate = enc->sample_rate;
1887 if (enc->channels > ap->channels)
1888 ap->channels = enc->channels;
1891 case CODEC_TYPE_VIDEO:
1892 if (enc->width > ap->width)
1893 ap->width = enc->width;
1894 if (enc->height > ap->height)
1895 ap->height = enc->height;
1896 if (enc->frame_rate > ap->frame_rate)
1897 ap->frame_rate = enc->frame_rate;
1906 if (has_video == 0 && has_audio == 0) {
1907 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1912 AVInputFormat *fmt1;
1913 fmt1 = av_find_input_format("video_grab_device");
1914 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
1915 fprintf(stderr, "Could not find video grab device\n");
1918 /* by now video grab has one stream */
1919 ic->streams[0]->r_frame_rate = ap->frame_rate;
1920 input_files[nb_input_files] = ic;
1921 dump_format(ic, nb_input_files, v4l_device, 0);
1925 AVInputFormat *fmt1;
1926 fmt1 = av_find_input_format("audio_device");
1927 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
1928 fprintf(stderr, "Could not find audio grab device\n");
1931 input_files[nb_input_files] = ic;
1932 dump_format(ic, nb_input_files, audio_device, 0);
1937 /* open the necessary output devices for playing */
1938 void prepare_play(void)
1940 file_iformat = NULL;
1941 file_oformat = guess_format("audio_device", NULL, NULL);
1942 if (!file_oformat) {
1943 fprintf(stderr, "Could not find audio device\n");
1947 opt_output_file(audio_device);
1951 #ifndef CONFIG_WIN32
1952 INT64 getutime(void)
1954 struct rusage rusage;
1956 getrusage(RUSAGE_SELF, &rusage);
1957 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
1960 INT64 getutime(void)
1966 extern int ffm_nopts;
1968 void opt_bitexact(void)
1970 avcodec_set_bit_exact();
1971 /* disable generate of real time pts in ffm (need to be supressed anyway) */
1975 void show_formats(void)
1977 AVInputFormat *ifmt;
1978 AVOutputFormat *ofmt;
1983 printf("File formats:\n");
1984 printf(" Encoding:");
1985 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
1986 printf(" %s", ofmt->name);
1989 printf(" Decoding:");
1990 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
1991 printf(" %s", ifmt->name);
1995 printf("Codecs:\n");
1996 printf(" Encoders:");
1997 for(p = first_avcodec; p != NULL; p = p->next) {
1999 printf(" %s", p->name);
2003 printf(" Decoders:");
2004 for(p = first_avcodec; p != NULL; p = p->next) {
2006 printf(" %s", p->name);
2010 printf("Supported file protocols:");
2011 for(up = first_protocol; up != NULL; up = up->next)
2012 printf(" %s:", up->name);
2015 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2016 printf("Motion estimation methods:");
2020 if ((pp - motion_str + 1) == ME_ZERO)
2021 printf("(fastest)");
2022 else if ((pp - motion_str + 1) == ME_FULL)
2023 printf("(slowest)");
2024 else if ((pp - motion_str + 1) == ME_EPZS)
2025 printf("(default)");
2032 void show_help(void)
2035 const OptionDef *po;
2038 prog = do_play ? "ffplay" : "ffmpeg";
2040 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2044 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2045 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2047 printf("usage: ffplay [options] input_file...\n"
2048 "Simple audio player\n");
2052 "Main options are:\n");
2055 printf("\nAdvanced options are:\n");
2056 for(po = options; po->name != NULL; po++) {
2058 expert = (po->flags & OPT_EXPERT) != 0;
2060 strcpy(buf, po->name);
2061 if (po->flags & HAS_ARG) {
2063 strcat(buf, po->argname);
2065 printf("-%-17s %s\n", buf, po->help);
2073 const OptionDef options[] = {
2074 { "L", 0, {(void*)show_licence}, "show license" },
2075 { "h", 0, {(void*)show_help}, "show help" },
2076 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2077 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2078 { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
2079 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2080 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2081 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2082 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2083 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2084 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2085 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2086 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2088 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2089 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2090 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2091 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2092 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2093 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2094 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2095 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2096 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2097 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2098 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2099 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2100 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2101 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2102 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2103 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2105 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2106 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2107 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2108 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2109 { "sameq", OPT_BOOL, {(void*)&same_quality},
2110 "use same video quality as source (implies VBR)" },
2112 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2113 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2114 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2115 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2116 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2117 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2118 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2119 "deinterlace pictures" },
2120 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2121 "add timings for benchmarking" },
2122 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2123 "dump each input packet" },
2124 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2125 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2126 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2130 int main(int argc, char **argv)
2133 const char *opt, *arg;
2134 const OptionDef *po;
2139 /* detect if invoked as player */
2140 i = strlen(argv[0]);
2141 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2149 while (optindex < argc) {
2150 opt = argv[optindex++];
2152 if (opt[0] == '-' && opt[1] != '\0') {
2154 while (po->name != NULL) {
2155 if (!strcmp(opt + 1, po->name))
2160 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2164 if (po->flags & HAS_ARG)
2165 arg = argv[optindex++];
2166 if (po->flags & OPT_STRING) {
2169 *po->u.str_arg = str;
2170 } else if (po->flags & OPT_BOOL) {
2173 po->u.func_arg(arg);
2177 opt_output_file(opt);
2179 opt_input_file(opt);
2186 /* file converter / grab */
2187 if (nb_output_files <= 0) {
2188 fprintf(stderr, "Must supply at least one output file\n");
2192 if (nb_input_files == 0) {
2197 if (nb_input_files <= 0) {
2198 fprintf(stderr, "Must supply at least one input file\n");
2205 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2206 stream_maps, nb_stream_maps);
2207 ti = getutime() - ti;
2209 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2213 for(i=0;i<nb_output_files;i++) {
2214 if (!(output_files[i]->oformat->flags & AVFMT_NOFILE))
2215 url_fclose(&output_files[i]->pb);
2217 for(i=0;i<nb_input_files;i++)
2218 av_close_input_file(input_files[i]);