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 = 200*1000;
84 static int video_bit_rate_tolerance = 4000*1000;
85 static int video_qscale = 0;
86 static int video_qmin = 2;
87 static int video_qmax = 31;
88 static int video_qdiff = 3;
89 static float video_qblur = 0.5;
90 static float video_qcomp = 0.5;
91 static float video_rc_qsquish=1.0;
92 static float video_rc_qmod_amp=0;
93 static int video_rc_qmod_freq=0;
94 static char *video_rc_override_string=NULL;
95 static char *video_rc_eq="tex^qComp";
96 static int video_rc_buffer_size=0;
97 static float video_rc_buffer_aggressivity=1.0;
98 static int video_rc_max_rate=0;
99 static int video_rc_min_rate=0;
100 static float video_rc_initial_cplx=0;
101 static float video_b_qfactor = 1.25;
102 static float video_b_qoffset = 1.25;
103 static float video_i_qfactor = 0.8;
104 static float video_i_qoffset = 0.0;
105 static int me_method = 0;
106 static int video_disable = 0;
107 static int video_codec_id = CODEC_ID_NONE;
108 static int same_quality = 0;
109 static int b_frames = 0;
110 static int use_hq = 0;
111 static int use_4mv = 0;
112 static int do_deinterlace = 0;
113 static int workaround_bugs = 0;
114 static int dct_algo = 0;
116 static int gop_size = 12;
117 static int intra_only = 0;
118 static int audio_sample_rate = 44100;
119 static int audio_bit_rate = 64000;
120 static int audio_disable = 0;
121 static int audio_channels = 1;
122 static int audio_codec_id = CODEC_ID_NONE;
124 static INT64 recording_time = 0;
125 static int file_overwrite = 0;
126 static char *str_title = NULL;
127 static char *str_author = NULL;
128 static char *str_copyright = NULL;
129 static char *str_comment = NULL;
130 static int do_benchmark = 0;
131 static int do_hex_dump = 0;
132 static int do_play = 0;
133 static int do_psnr = 0;
134 static int do_vstats = 0;
135 static int mpeg_vcd = 0;
137 #ifndef CONFIG_AUDIO_OSS
138 const char *audio_device = "none";
140 #ifndef CONFIG_VIDEO4LINUX
141 const char *v4l_device = "none";
144 typedef struct AVOutputStream {
145 int file_index; /* file index */
146 int index; /* stream index in the output file */
147 int source_index; /* AVInputStream index */
148 AVStream *st; /* stream in the output file */
149 int encoding_needed; /* true if encoding needed for this stream */
152 AVPicture pict_tmp; /* temporary image for resizing */
154 ImgReSampleContext *img_resample_ctx; /* for image resampling */
158 ReSampleContext *resample; /* for audio resampling */
159 FifoBuffer fifo; /* for compression: one audio fifo per codec */
162 typedef struct AVInputStream {
166 int discard; /* true if stream data should be discarded */
167 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
168 Ticker pts_ticker; /* Ticker for PTS calculation */
169 int ticker_inited; /* to signal if the ticker was initialized */
170 INT64 pts; /* current pts */
171 int pts_increment; /* expected pts increment for next packet */
172 int frame_number; /* current frame */
173 INT64 sample_index; /* current sample */
176 typedef struct AVInputFile {
177 int eof_reached; /* true if eof reached */
178 int ist_index; /* index of first stream in ist_table */
179 int buffer_size; /* current total buffer size */
180 int buffer_size_max; /* buffer size at which we consider we can stop
182 int nb_streams; /* nb streams we are aware of */
187 /* init terminal so that we can grab keys */
188 static struct termios oldtty;
190 static void term_exit(void)
192 tcsetattr (0, TCSANOW, &oldtty);
195 static void term_init(void)
202 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
203 |INLCR|IGNCR|ICRNL|IXON);
204 tty.c_oflag |= OPOST;
205 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
206 tty.c_cflag &= ~(CSIZE|PARENB);
211 tcsetattr (0, TCSANOW, &tty);
216 /* read a key without blocking */
217 static int read_key(void)
228 n = select(1, &rfds, NULL, NULL, &tv);
241 /* no interactive support */
242 static void term_exit(void)
246 static void term_init(void)
250 static int read_key(void)
257 int read_ffserver_streams(AVFormatContext *s, const char *filename)
262 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
265 /* copy stream format */
266 s->nb_streams = ic->nb_streams;
267 for(i=0;i<ic->nb_streams;i++) {
269 st = av_mallocz(sizeof(AVFormatContext));
270 memcpy(st, ic->streams[i], sizeof(AVStream));
274 av_close_input_file(ic);
278 #define MAX_AUDIO_PACKET_SIZE 16384
280 static void do_audio_out(AVFormatContext *s,
283 unsigned char *buf, int size)
286 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
287 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
288 int size_out, frame_bytes, ret;
291 enc = &ost->st->codec;
293 if (ost->audio_resample) {
295 size_out = audio_resample(ost->resample,
296 (short *)buftmp, (short *)buf,
297 size / (ist->st->codec.channels * 2));
298 size_out = size_out * enc->channels * 2;
304 /* now encode as many frames as possible */
305 if (enc->frame_size > 1) {
306 /* output resampled raw samples */
307 fifo_write(&ost->fifo, buftmp, size_out,
310 frame_bytes = enc->frame_size * 2 * enc->channels;
312 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
313 &ost->fifo.rptr) == 0) {
314 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
316 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
319 /* output a pcm frame */
320 /* XXX: change encoding codec API to avoid this ? */
321 switch(enc->codec->id) {
322 case CODEC_ID_PCM_S16LE:
323 case CODEC_ID_PCM_S16BE:
324 case CODEC_ID_PCM_U16LE:
325 case CODEC_ID_PCM_U16BE:
328 size_out = size_out >> 1;
331 ret = avcodec_encode_audio(enc, audio_out, size_out,
333 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
337 /* write a picture to a raw mux */
338 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
339 int pix_fmt, int w, int h)
341 UINT8 *buf, *src, *dest;
344 /* XXX: not efficient, should add test if we can take
345 directly the AVPicture */
347 case PIX_FMT_YUV420P:
348 size = avpicture_get_size(pix_fmt, w, h);
349 buf = av_malloc(size);
358 src = picture->data[i];
360 memcpy(dest, src, w);
362 src += picture->linesize[i];
366 case PIX_FMT_YUV422P:
368 buf = av_malloc(size);
376 src = picture->data[i];
378 memcpy(dest, src, w);
380 src += picture->linesize[i];
384 case PIX_FMT_YUV444P:
386 buf = av_malloc(size);
391 src = picture->data[i];
393 memcpy(dest, src, w);
395 src += picture->linesize[i];
401 buf = av_malloc(size);
405 src = picture->data[0];
407 memcpy(dest, src, w * 2);
409 src += picture->linesize[0];
415 buf = av_malloc(size);
419 src = picture->data[0];
421 memcpy(dest, src, w * 3);
423 src += picture->linesize[0];
429 s->oformat->write_packet(s, index, buf, size, 0);
434 static void do_video_out(AVFormatContext *s,
440 int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
441 AVPicture *picture, *picture2, *pict;
442 AVPicture picture_tmp1, picture_tmp2;
443 static UINT8 *video_buffer;
444 UINT8 *buf = NULL, *buf1 = NULL;
445 AVCodecContext *enc, *dec;
447 #define VIDEO_BUFFER_SIZE (1024*1024)
449 enc = &ost->st->codec;
450 dec = &ist->st->codec;
452 frame_number = ist->frame_number;
453 dec_frame_rate = ist->st->r_frame_rate;
454 // fprintf(stderr, "\n%d", dec_frame_rate);
455 /* first drop frame if needed */
456 n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
457 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
463 video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
464 if(!video_buffer) return;
466 /* deinterlace : must be done before any resize */
467 if (do_deinterlace) {
470 /* create temporary picture */
471 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
472 buf1 = av_malloc(size);
476 picture2 = &picture_tmp2;
477 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
479 if (avpicture_deinterlace(picture2, picture1,
480 dec->pix_fmt, dec->width, dec->height) < 0) {
481 /* if error, do not deinterlace */
490 /* convert pixel format if needed */
491 if (enc->pix_fmt != dec->pix_fmt) {
494 /* create temporary picture */
495 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
496 buf = av_malloc(size);
499 pict = &picture_tmp1;
500 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
502 if (img_convert(pict, enc->pix_fmt,
503 picture2, dec->pix_fmt,
504 dec->width, dec->height) < 0) {
505 fprintf(stderr, "pixel format conversion not handled\n");
512 /* XXX: resampling could be done before raw format convertion in
513 some cases to go faster */
514 /* XXX: only works for YUV420P */
515 if (ost->video_resample) {
516 picture = &ost->pict_tmp;
517 img_resample(ost->img_resample_ctx, picture, pict);
522 /* duplicates frame if needed */
523 /* XXX: pb because no interleaving */
525 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
526 /* handles sameq here. This is not correct because it may
527 not be a global option */
529 enc->quality = dec->quality;
532 ret = avcodec_encode_video(enc,
533 video_buffer, VIDEO_BUFFER_SIZE,
535 //enc->frame_number = enc->real_pict_num;
536 s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
538 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
539 // enc->frame_number-1, enc->real_pict_num, ret,
542 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
550 static void do_video_stats(AVOutputStream *ost,
554 static FILE *fvstats=NULL;
555 static INT64 total_size = 0;
562 double ti1, bitrate, avg_bitrate;
566 today = localtime(&today2);
567 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
570 fvstats = fopen(filename,"w");
578 enc = &ost->st->codec;
579 total_size += frame_size;
580 if (enc->codec_type == CODEC_TYPE_VIDEO) {
581 frame_number = ist->frame_number;
582 fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
584 fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
586 fprintf(fvstats,"f_size= %6d ", frame_size);
587 /* compute min pts value */
588 if (!ist->discard && ist->pts < ti) {
591 ti1 = (double)ti / 1000000.0;
595 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
596 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
597 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
598 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
599 fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
607 * The following code is the main loop of the file converter
609 static int av_encode(AVFormatContext **output_files,
611 AVFormatContext **input_files,
613 AVStreamMap *stream_maps, int nb_stream_maps)
615 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
616 AVFormatContext *is, *os;
617 AVCodecContext *codec, *icodec;
618 AVOutputStream *ost, **ost_table = NULL;
619 AVInputStream *ist, **ist_table = NULL;
620 INT64 min_pts, start_time;
621 AVInputFile *file_table;
622 AVFormatContext *stream_no_data;
625 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
629 /* input stream init */
631 for(i=0;i<nb_input_files;i++) {
633 file_table[i].ist_index = j;
634 file_table[i].nb_streams = is->nb_streams;
639 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
643 for(i=0;i<nb_istreams;i++) {
644 ist = av_mallocz(sizeof(AVInputStream));
650 for(i=0;i<nb_input_files;i++) {
652 for(k=0;k<is->nb_streams;k++) {
653 ist = ist_table[j++];
654 ist->st = is->streams[k];
657 ist->discard = 1; /* the stream is discarded by default
662 /* output stream init */
664 for(i=0;i<nb_output_files;i++) {
665 os = output_files[i];
666 nb_ostreams += os->nb_streams;
668 os->flags |= AVF_FLAG_VCD;
670 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
671 fprintf(stderr, "Number of stream maps must match number of output streams\n");
675 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
678 for(i=0;i<nb_ostreams;i++) {
679 ost = av_mallocz(sizeof(AVOutputStream));
686 for(k=0;k<nb_output_files;k++) {
687 os = output_files[k];
688 for(i=0;i<os->nb_streams;i++) {
690 ost = ost_table[n++];
693 ost->st = os->streams[i];
694 if (nb_stream_maps > 0) {
695 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
696 stream_maps[n-1].stream_index;
698 /* get corresponding input stream index : we select the first one with the right type */
700 for(j=0;j<nb_istreams;j++) {
703 ist->st->codec.codec_type == ost->st->codec.codec_type) {
704 ost->source_index = j;
710 /* try again and reuse existing stream */
711 for(j=0;j<nb_istreams;j++) {
713 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
714 ost->source_index = j;
719 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
720 ost->file_index, ost->index);
725 ist = ist_table[ost->source_index];
730 /* dump the stream mapping */
731 fprintf(stderr, "Stream mapping:\n");
732 for(i=0;i<nb_ostreams;i++) {
734 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
735 ist_table[ost->source_index]->file_index,
736 ist_table[ost->source_index]->index,
741 /* for each output stream, we compute the right encoding parameters */
742 for(i=0;i<nb_ostreams;i++) {
744 ist = ist_table[ost->source_index];
746 codec = &ost->st->codec;
747 icodec = &ist->st->codec;
749 switch(codec->codec_type) {
750 case CODEC_TYPE_AUDIO:
751 /* check if same codec with same parameters. If so, no
752 reencoding is needed */
753 if (codec->codec_id == icodec->codec_id &&
754 codec->bit_rate == icodec->bit_rate &&
755 codec->sample_rate == icodec->sample_rate &&
756 codec->channels == icodec->channels) {
758 /* use the same frame size */
759 codec->frame_size = icodec->frame_size;
760 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
762 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
764 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
767 if (codec->channels == icodec->channels &&
768 codec->sample_rate == icodec->sample_rate) {
769 ost->audio_resample = 0;
771 if (codec->channels != icodec->channels &&
772 icodec->codec_id == CODEC_ID_AC3) {
773 /* Special case for 5:1 AC3 input */
774 /* and mono or stereo output */
775 /* Request specific number of channels */
776 icodec->channels = codec->channels;
777 if (codec->sample_rate == icodec->sample_rate)
778 ost->audio_resample = 0;
780 ost->audio_resample = 1;
781 ost->resample = audio_resample_init(codec->channels, icodec->channels,
783 icodec->sample_rate);
785 /* Request specific number of channels */
786 icodec->channels = codec->channels;
788 ost->audio_resample = 1;
789 ost->resample = audio_resample_init(codec->channels, icodec->channels,
791 icodec->sample_rate);
794 ist->decoding_needed = 1;
795 ost->encoding_needed = 1;
798 case CODEC_TYPE_VIDEO:
799 /* check if same codec with same parameters. If so, no
800 reencoding is needed */
801 if (codec->codec_id == icodec->codec_id &&
802 codec->bit_rate == icodec->bit_rate &&
803 codec->frame_rate == icodec->frame_rate &&
804 codec->width == icodec->width &&
805 codec->height == icodec->height) {
808 if (codec->width == icodec->width &&
809 codec->height == icodec->height) {
810 ost->video_resample = 0;
813 ost->video_resample = 1;
814 buf = av_malloc((codec->width * codec->height * 3) / 2);
817 ost->pict_tmp.data[0] = buf;
818 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
819 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
820 ost->pict_tmp.linesize[0] = codec->width;
821 ost->pict_tmp.linesize[1] = codec->width / 2;
822 ost->pict_tmp.linesize[2] = codec->width / 2;
824 ost->img_resample_ctx = img_resample_init(
825 ost->st->codec.width, ost->st->codec.height,
826 ist->st->codec.width, ist->st->codec.height);
828 ost->encoding_needed = 1;
829 ist->decoding_needed = 1;
837 /* open each encoder */
838 for(i=0;i<nb_ostreams;i++) {
840 if (ost->encoding_needed) {
842 codec = avcodec_find_encoder(ost->st->codec.codec_id);
844 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
845 ost->file_index, ost->index);
848 if (avcodec_open(&ost->st->codec, codec) < 0) {
849 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
850 ost->file_index, ost->index);
856 /* open each decoder */
857 for(i=0;i<nb_istreams;i++) {
859 if (ist->decoding_needed) {
861 codec = avcodec_find_decoder(ist->st->codec.codec_id);
863 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
864 ist->file_index, ist->index);
867 if (avcodec_open(&ist->st->codec, codec) < 0) {
868 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
869 ist->file_index, ist->index);
872 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
873 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
878 for(i=0;i<nb_istreams;i++) {
881 ist->frame_number = 0;
884 /* compute buffer size max (should use a complete heuristic) */
885 for(i=0;i<nb_input_files;i++) {
886 file_table[i].buffer_size_max = 2048;
889 /* open files and write file headers */
890 for(i=0;i<nb_output_files;i++) {
891 os = output_files[i];
892 if (av_write_header(os) < 0) {
893 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
901 fprintf(stderr, "Press [q] to stop encoding\n");
903 fprintf(stderr, "Press [q] to stop playing\n");
908 start_time = av_gettime();
914 int file_index, ist_index;
919 int data_size, got_picture;
921 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
924 /* if 'q' pressed, exits */
926 /* read_key() returns 0 on EOF */
932 /* select the input file with the smallest pts */
935 for(i=0;i<nb_istreams;i++) {
937 /* For some reason, the pts_increment code breaks q estimation?!? */
938 if (!ist->discard && !file_table[ist->file_index].eof_reached &&
939 ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
940 min_pts = ist->pts /* + ist->pts_increment */;
941 file_index = ist->file_index;
944 /* if none, if is finished */
945 if (file_index < 0) {
946 if (stream_no_data) {
947 #ifndef CONFIG_WIN32 /* no usleep in VisualC ? */
949 snooze(10 * 1000); /* mmu_man */ /* in microsec */
950 #elif defined(__CYGWIN__)
956 ts.tv_nsec = 1000 * 1000 * 10;
965 /* finish if recording time exhausted */
966 if (recording_time > 0 && min_pts >= recording_time)
968 /* read a packet from it and output it in the fifo */
969 is = input_files[file_index];
970 if (av_read_packet(is, &pkt) < 0) {
971 file_table[file_index].eof_reached = 1;
979 /* the following test is needed in case new streams appear
980 dynamically in stream : we ignore them */
981 if (pkt.stream_index >= file_table[file_index].nb_streams)
983 ist_index = file_table[file_index].ist_index + pkt.stream_index;
984 ist = ist_table[ist_index];
988 if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
992 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
993 av_hex_dump(pkt.data, pkt.size);
996 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1002 /* decode the packet if needed */
1003 data_buf = NULL; /* fail safe */
1005 if (ist->decoding_needed) {
1006 switch(ist->st->codec.codec_type) {
1007 case CODEC_TYPE_AUDIO:
1008 /* XXX: could avoid copy if PCM 16 bits with same
1009 endianness as CPU */
1010 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1014 /* Some bug in mpeg audio decoder gives */
1015 /* data_size < 0, it seems they are overflows */
1016 if (data_size <= 0) {
1017 /* no audio frame */
1022 data_buf = (UINT8 *)samples;
1024 case CODEC_TYPE_VIDEO:
1025 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1027 size = (ist->st->codec.width * ist->st->codec.height);
1028 avpicture_fill(&picture, ptr,
1029 ist->st->codec.pix_fmt,
1030 ist->st->codec.width,
1031 ist->st->codec.height);
1034 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1035 ret = avcodec_decode_video(&ist->st->codec,
1036 &picture, &got_picture, ptr, len);
1039 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1040 ist->file_index, ist->index);
1041 av_free_packet(&pkt);
1045 /* no picture yet */
1062 if (!ist->ticker_inited) {
1063 switch (ist->st->codec.codec_type) {
1064 case CODEC_TYPE_AUDIO:
1065 ticker_init(&ist->pts_ticker,
1066 (INT64)ist->st->codec.sample_rate,
1068 ist->ticker_inited = 1;
1070 case CODEC_TYPE_VIDEO:
1071 ticker_init(&ist->pts_ticker,
1072 (INT64)ist->st->r_frame_rate,
1073 ((INT64)1000000 * FRAME_RATE_BASE));
1074 ist->ticker_inited = 1;
1081 switch(ist->st->codec.codec_type) {
1082 case CODEC_TYPE_AUDIO:
1083 //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1084 ist->pts = ticker_abs(&ist->pts_ticker, ist->sample_index);
1085 ist->sample_index += data_size / (2 * ist->st->codec.channels);
1086 ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
1088 case CODEC_TYPE_VIDEO:
1089 ist->frame_number++;
1090 //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1091 // ist->st->codec.frame_rate;
1092 ist->pts = ticker_abs(&ist->pts_ticker, ist->frame_number);
1093 ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
1094 ist->st->codec.frame_rate;
1102 /* transcode raw format, encode packets and output them */
1104 for(i=0;i<nb_ostreams;i++) {
1107 if (ost->source_index == ist_index) {
1108 os = output_files[ost->file_index];
1110 if (ost->encoding_needed) {
1111 switch(ost->st->codec.codec_type) {
1112 case CODEC_TYPE_AUDIO:
1113 do_audio_out(os, ost, ist, data_buf, data_size);
1115 case CODEC_TYPE_VIDEO:
1116 do_video_out(os, ost, ist, &picture, &frame_size);
1118 do_video_stats(ost, ist, frame_size);
1124 /* no reencoding needed : output the packet directly */
1125 /* force the input stream PTS */
1126 os->oformat->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
1132 av_free_packet(&pkt);
1134 /* dump report by using the first video and audio streams */
1137 AVFormatContext *oc;
1138 INT64 total_size, ti;
1139 AVCodecContext *enc;
1140 int frame_number, vid;
1141 double bitrate, ti1;
1142 static INT64 last_time;
1144 if ((min_pts - last_time) >= 500000) {
1145 last_time = min_pts;
1147 oc = output_files[0];
1149 total_size = url_ftell(&oc->pb);
1154 for(i=0;i<nb_ostreams;i++) {
1156 enc = &ost->st->codec;
1157 ist = ist_table[ost->source_index];
1158 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1159 frame_number = ist->frame_number;
1160 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1161 frame_number, enc->quality);
1163 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1166 /* compute min pts value */
1167 if (!ist->discard && ist->pts < ti) {
1172 ti1 = (double)ti / 1000000.0;
1175 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1177 sprintf(buf + strlen(buf),
1178 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1179 (double)total_size / 1024, ti1, bitrate);
1181 fprintf(stderr, "%s \r", buf);
1188 /* dump report by using the first video and audio streams */
1191 AVFormatContext *oc;
1192 INT64 total_size, ti;
1193 AVCodecContext *enc;
1194 int frame_number, vid;
1195 double bitrate, ti1;
1197 oc = output_files[0];
1199 total_size = url_ftell(&oc->pb);
1204 for(i=0;i<nb_ostreams;i++) {
1206 enc = &ost->st->codec;
1207 ist = ist_table[ost->source_index];
1208 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1209 frame_number = ist->frame_number;
1210 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1211 frame_number, enc->quality);
1213 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1216 /* compute min pts value */
1217 if (!ist->discard && ist->pts < ti) {
1222 ti1 = ti / 1000000.0;
1225 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1227 sprintf(buf + strlen(buf),
1228 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1229 (double)total_size / 1024, ti1, bitrate);
1231 fprintf(stderr, "%s \n", buf);
1233 /* close each encoder */
1234 for(i=0;i<nb_ostreams;i++) {
1236 if (ost->encoding_needed) {
1237 avcodec_close(&ost->st->codec);
1241 /* close each decoder */
1242 for(i=0;i<nb_istreams;i++) {
1244 if (ist->decoding_needed) {
1245 avcodec_close(&ist->st->codec);
1250 /* write the trailer if needed and close file */
1251 for(i=0;i<nb_output_files;i++) {
1252 os = output_files[i];
1253 av_write_trailer(os);
1259 av_free(file_table);
1262 for(i=0;i<nb_istreams;i++) {
1269 for(i=0;i<nb_ostreams;i++) {
1272 fifo_free(&ost->fifo); /* works even if fifo is not
1273 initialized but set to zero */
1274 av_free(ost->pict_tmp.data[0]);
1275 if (ost->video_resample)
1276 img_resample_close(ost->img_resample_ctx);
1277 if (ost->audio_resample)
1278 audio_resample_close(ost->resample);
1291 int file_read(const char *filename)
1294 unsigned char buffer[1024];
1297 if (url_open(&h, filename, O_RDONLY) < 0) {
1298 printf("could not open '%s'\n", filename);
1302 len = url_read(h, buffer, sizeof(buffer));
1305 for(i=0;i<len;i++) putchar(buffer[i]);
1312 void show_licence(void)
1315 "ffmpeg version " FFMPEG_VERSION "\n"
1316 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1317 "This library is free software; you can redistribute it and/or\n"
1318 "modify it under the terms of the GNU Lesser General Public\n"
1319 "License as published by the Free Software Foundation; either\n"
1320 "version 2 of the License, or (at your option) any later version.\n"
1322 "This library is distributed in the hope that it will be useful,\n"
1323 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1324 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1325 "Lesser General Public License for more details.\n"
1327 "You should have received a copy of the GNU Lesser General Public\n"
1328 "License along with this library; if not, write to the Free Software\n"
1329 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1334 void opt_format(const char *arg)
1336 file_iformat = av_find_input_format(arg);
1337 file_oformat = guess_format(arg, NULL, NULL);
1338 if (!file_iformat && !file_oformat) {
1339 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1344 void opt_video_bitrate(const char *arg)
1346 video_bit_rate = atoi(arg) * 1000;
1349 void opt_video_bitrate_tolerance(const char *arg)
1351 video_bit_rate_tolerance = atoi(arg) * 1000;
1354 void opt_video_bitrate_max(const char *arg)
1356 video_rc_max_rate = atoi(arg) * 1000;
1359 void opt_video_bitrate_min(const char *arg)
1361 video_rc_min_rate = atoi(arg) * 1000;
1364 void opt_video_rc_eq(char *arg)
1370 void opt_workaround_bugs(const char *arg)
1372 workaround_bugs = atoi(arg);
1375 void opt_dct_algo(const char *arg)
1377 dct_algo = atoi(arg);
1380 void opt_frame_rate(const char *arg)
1382 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1385 void opt_frame_size(const char *arg)
1387 parse_image_size(&frame_width, &frame_height, arg);
1388 if (frame_width <= 0 || frame_height <= 0) {
1389 fprintf(stderr, "Incorrect frame size\n");
1392 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1393 fprintf(stderr, "Frame size must be a multiple of 2\n");
1398 void opt_gop_size(const char *arg)
1400 gop_size = atoi(arg);
1403 void opt_b_frames(const char *arg)
1405 b_frames = atoi(arg);
1406 if (b_frames > FF_MAX_B_FRAMES) {
1407 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1409 } else if (b_frames < 1) {
1410 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1415 void opt_qscale(const char *arg)
1417 video_qscale = atoi(arg);
1418 if (video_qscale < 0 ||
1419 video_qscale > 31) {
1420 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1425 void opt_qmin(const char *arg)
1427 video_qmin = atoi(arg);
1428 if (video_qmin < 0 ||
1430 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1435 void opt_qmax(const char *arg)
1437 video_qmax = atoi(arg);
1438 if (video_qmax < 0 ||
1440 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1445 void opt_qdiff(const char *arg)
1447 video_qdiff = atoi(arg);
1448 if (video_qdiff < 0 ||
1450 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1455 void opt_qblur(const char *arg)
1457 video_qblur = atof(arg);
1460 void opt_qcomp(const char *arg)
1462 video_qcomp = atof(arg);
1465 void opt_audio_bitrate(const char *arg)
1467 audio_bit_rate = atoi(arg) * 1000;
1470 void opt_audio_rate(const char *arg)
1472 audio_sample_rate = atoi(arg);
1475 void opt_audio_channels(const char *arg)
1477 audio_channels = atoi(arg);
1480 void opt_video_device(const char *arg)
1482 v4l_device = strdup(arg);
1485 void opt_audio_device(const char *arg)
1487 audio_device = strdup(arg);
1490 void opt_audio_codec(const char *arg)
1496 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1501 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1504 audio_codec_id = p->id;
1508 const char *motion_str[] = {
1518 void opt_motion_estimation(const char *arg)
1524 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1527 if (!strcmp(*p, arg))
1531 me_method = (p - motion_str) + 1;
1534 void opt_video_codec(const char *arg)
1540 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1545 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1548 video_codec_id = p->id;
1552 void opt_map(const char *arg)
1558 m = &stream_maps[nb_stream_maps++];
1560 m->file_index = strtol(arg, (char **)&p, 0);
1564 m->stream_index = strtol(p, (char **)&p, 0);
1567 void opt_recording_time(const char *arg)
1569 recording_time = parse_date(arg, 1);
1572 void print_error(const char *filename, int err)
1575 case AVERROR_NUMEXPECTED:
1576 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1577 "Use '%%d' to specify the image number:\n"
1578 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1579 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1582 case AVERROR_INVALIDDATA:
1583 fprintf(stderr, "%s: Error while parsing header\n", filename);
1586 fprintf(stderr, "%s: Unknown format\n", filename);
1589 fprintf(stderr, "%s: Error while opening file\n", filename);
1594 void opt_input_file(const char *filename)
1596 AVFormatContext *ic;
1597 AVFormatParameters params, *ap = ¶ms;
1598 int err, i, ret, rfps;
1600 /* get default parameters from command line */
1601 memset(ap, 0, sizeof(*ap));
1602 ap->sample_rate = audio_sample_rate;
1603 ap->channels = audio_channels;
1604 ap->frame_rate = frame_rate;
1605 ap->width = frame_width;
1606 ap->height = frame_height;
1608 /* open the input file with generic libav function */
1609 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
1611 print_error(filename, err);
1615 /* If not enough info to get the stream parameters, we decode the
1616 first frames to get it. (used in mpeg case for example) */
1617 ret = av_find_stream_info(ic);
1619 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1623 /* update the current parameters so that they match the one of the input stream */
1624 for(i=0;i<ic->nb_streams;i++) {
1625 AVCodecContext *enc = &ic->streams[i]->codec;
1626 switch(enc->codec_type) {
1627 case CODEC_TYPE_AUDIO:
1628 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1629 audio_channels = enc->channels;
1630 audio_sample_rate = enc->sample_rate;
1632 case CODEC_TYPE_VIDEO:
1633 frame_height = enc->height;
1634 frame_width = enc->width;
1635 rfps = ic->streams[i]->r_frame_rate;
1636 enc->workaround_bugs = workaround_bugs;
1637 if (enc->frame_rate != rfps) {
1638 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
1639 i, (float)enc->frame_rate / FRAME_RATE_BASE,
1640 (float)rfps / FRAME_RATE_BASE);
1642 /* update the current frame rate to match the stream frame rate */
1650 input_files[nb_input_files] = ic;
1651 /* dump the file content */
1652 dump_format(ic, nb_input_files, filename, 0);
1654 file_iformat = NULL;
1655 file_oformat = NULL;
1658 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1660 int has_video, has_audio, i, j;
1661 AVFormatContext *ic;
1665 for(j=0;j<nb_input_files;j++) {
1666 ic = input_files[j];
1667 for(i=0;i<ic->nb_streams;i++) {
1668 AVCodecContext *enc = &ic->streams[i]->codec;
1669 switch(enc->codec_type) {
1670 case CODEC_TYPE_AUDIO:
1673 case CODEC_TYPE_VIDEO:
1681 *has_video_ptr = has_video;
1682 *has_audio_ptr = has_audio;
1685 void opt_output_file(const char *filename)
1688 AVFormatContext *oc;
1689 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1692 if (!strcmp(filename, "-"))
1695 oc = av_mallocz(sizeof(AVFormatContext));
1697 if (!file_oformat) {
1698 file_oformat = guess_format(NULL, filename, NULL);
1699 if (!file_oformat) {
1700 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
1706 oc->oformat = file_oformat;
1708 if (!strcmp(file_oformat->name, "ffm") &&
1709 strstart(filename, "http:", NULL)) {
1710 /* special case for files sent to ffserver: we get the stream
1711 parameters from ffserver */
1712 if (read_ffserver_streams(oc, filename) < 0) {
1713 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1717 use_video = file_oformat->video_codec != CODEC_ID_NONE;
1718 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
1720 /* disable if no corresponding type found and at least one
1722 if (nb_input_files > 0) {
1723 check_audio_video_inputs(&input_has_video, &input_has_audio);
1724 if (!input_has_video)
1726 if (!input_has_audio)
1730 /* manual disable */
1731 if (audio_disable) {
1734 if (video_disable) {
1740 AVCodecContext *video_enc;
1742 st = av_mallocz(sizeof(AVStream));
1744 fprintf(stderr, "Could not alloc stream\n");
1747 video_enc = &st->codec;
1749 codec_id = file_oformat->video_codec;
1750 if (video_codec_id != CODEC_ID_NONE)
1751 codec_id = video_codec_id;
1753 video_enc->codec_id = codec_id;
1754 video_enc->codec_type = CODEC_TYPE_VIDEO;
1756 video_enc->bit_rate = video_bit_rate;
1757 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
1758 video_enc->frame_rate = frame_rate;
1760 video_enc->width = frame_width;
1761 video_enc->height = frame_height;
1763 video_enc->gop_size = gop_size;
1765 video_enc->gop_size = 0;
1766 if (video_qscale || same_quality) {
1767 video_enc->flags |= CODEC_FLAG_QSCALE;
1768 video_enc->quality = video_qscale;
1772 video_enc->flags |= CODEC_FLAG_HQ;
1776 video_enc->flags |= CODEC_FLAG_HQ;
1777 video_enc->flags |= CODEC_FLAG_4MV;
1781 if (codec_id != CODEC_ID_MPEG4) {
1782 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
1785 video_enc->max_b_frames = b_frames;
1786 video_enc->b_frame_strategy = 0;
1787 video_enc->b_quant_factor = 2.0;
1790 video_enc->qmin = video_qmin;
1791 video_enc->qmax = video_qmax;
1792 video_enc->max_qdiff = video_qdiff;
1793 video_enc->qblur = video_qblur;
1794 video_enc->qcompress = video_qcomp;
1795 video_enc->rc_eq = video_rc_eq;
1796 video_enc->rc_max_rate = video_rc_max_rate;
1797 video_enc->rc_min_rate = video_rc_min_rate;
1798 video_enc->rc_buffer_size = video_rc_buffer_size;
1799 video_enc->i_quant_factor = video_i_qfactor;
1800 video_enc->b_quant_factor = video_b_qfactor;
1801 video_enc->i_quant_offset = video_i_qoffset;
1802 video_enc->b_quant_offset = video_b_qoffset;
1803 video_enc->dct_algo = dct_algo;
1806 video_enc->get_psnr = 1;
1808 video_enc->get_psnr = 0;
1810 video_enc->me_method = me_method;
1812 /* XXX: need to find a way to set codec parameters */
1813 if (oc->oformat->flags & AVFMT_RGB24) {
1814 video_enc->pix_fmt = PIX_FMT_RGB24;
1817 oc->streams[nb_streams] = st;
1822 AVCodecContext *audio_enc;
1824 st = av_mallocz(sizeof(AVStream));
1826 fprintf(stderr, "Could not alloc stream\n");
1829 audio_enc = &st->codec;
1830 codec_id = file_oformat->audio_codec;
1831 if (audio_codec_id != CODEC_ID_NONE)
1832 codec_id = audio_codec_id;
1833 audio_enc->codec_id = codec_id;
1834 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1836 audio_enc->bit_rate = audio_bit_rate;
1837 audio_enc->sample_rate = audio_sample_rate;
1838 /* For audio codecs other than AC3 we limit */
1839 /* the number of coded channels to stereo */
1840 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
1841 audio_enc->channels = 2;
1843 audio_enc->channels = audio_channels;
1844 oc->streams[nb_streams] = st;
1848 oc->nb_streams = nb_streams;
1851 fprintf(stderr, "No audio or video streams available\n");
1856 pstrcpy(oc->title, sizeof(oc->title), str_title);
1858 pstrcpy(oc->author, sizeof(oc->author), str_author);
1860 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1862 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1865 output_files[nb_output_files] = oc;
1866 /* dump the file content */
1867 dump_format(oc, nb_output_files, filename, 1);
1870 strcpy(oc->filename, filename);
1872 /* check filename in case of an image number is expected */
1873 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
1874 if (filename_number_test(oc->filename) < 0) {
1875 print_error(oc->filename, AVERROR_NUMEXPECTED);
1880 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
1881 /* test if it already exists to avoid loosing precious files */
1882 if (!file_overwrite &&
1883 (strchr(filename, ':') == NULL ||
1884 strstart(filename, "file:", NULL))) {
1885 if (url_exist(filename)) {
1888 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1891 if (toupper(c) != 'Y') {
1892 fprintf(stderr, "Not overwriting - exiting\n");
1899 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1900 fprintf(stderr, "Could not open '%s'\n", filename);
1905 /* reset some options */
1906 file_oformat = NULL;
1907 file_iformat = NULL;
1910 audio_codec_id = CODEC_ID_NONE;
1911 video_codec_id = CODEC_ID_NONE;
1914 /* prepare dummy protocols for grab */
1915 void prepare_grab(void)
1917 int has_video, has_audio, i, j;
1918 AVFormatContext *oc;
1919 AVFormatContext *ic;
1920 AVFormatParameters ap1, *ap = &ap1;
1922 /* see if audio/video inputs are needed */
1925 memset(ap, 0, sizeof(*ap));
1926 for(j=0;j<nb_output_files;j++) {
1927 oc = output_files[j];
1928 for(i=0;i<oc->nb_streams;i++) {
1929 AVCodecContext *enc = &oc->streams[i]->codec;
1930 switch(enc->codec_type) {
1931 case CODEC_TYPE_AUDIO:
1932 if (enc->sample_rate > ap->sample_rate)
1933 ap->sample_rate = enc->sample_rate;
1934 if (enc->channels > ap->channels)
1935 ap->channels = enc->channels;
1938 case CODEC_TYPE_VIDEO:
1939 if (enc->width > ap->width)
1940 ap->width = enc->width;
1941 if (enc->height > ap->height)
1942 ap->height = enc->height;
1943 if (enc->frame_rate > ap->frame_rate)
1944 ap->frame_rate = enc->frame_rate;
1953 if (has_video == 0 && has_audio == 0) {
1954 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1959 AVInputFormat *fmt1;
1960 fmt1 = av_find_input_format("video_grab_device");
1961 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
1962 fprintf(stderr, "Could not find video grab device\n");
1965 /* by now video grab has one stream */
1966 ic->streams[0]->r_frame_rate = ap->frame_rate;
1967 input_files[nb_input_files] = ic;
1968 dump_format(ic, nb_input_files, v4l_device, 0);
1972 AVInputFormat *fmt1;
1973 fmt1 = av_find_input_format("audio_device");
1974 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
1975 fprintf(stderr, "Could not find audio grab device\n");
1978 input_files[nb_input_files] = ic;
1979 dump_format(ic, nb_input_files, audio_device, 0);
1984 /* open the necessary output devices for playing */
1985 void prepare_play(void)
1987 file_iformat = NULL;
1988 file_oformat = guess_format("audio_device", NULL, NULL);
1989 if (!file_oformat) {
1990 fprintf(stderr, "Could not find audio device\n");
1994 opt_output_file(audio_device);
1998 #ifndef CONFIG_WIN32
1999 INT64 getutime(void)
2001 struct rusage rusage;
2003 getrusage(RUSAGE_SELF, &rusage);
2004 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2007 INT64 getutime(void)
2009 return av_gettime();
2013 extern int ffm_nopts;
2015 void opt_bitexact(void)
2017 avcodec_set_bit_exact();
2018 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2022 void show_formats(void)
2024 AVInputFormat *ifmt;
2025 AVOutputFormat *ofmt;
2030 printf("File formats:\n");
2031 printf(" Encoding:");
2032 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2033 printf(" %s", ofmt->name);
2036 printf(" Decoding:");
2037 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2038 printf(" %s", ifmt->name);
2042 printf("Codecs:\n");
2043 printf(" Encoders:");
2044 for(p = first_avcodec; p != NULL; p = p->next) {
2046 printf(" %s", p->name);
2050 printf(" Decoders:");
2051 for(p = first_avcodec; p != NULL; p = p->next) {
2053 printf(" %s", p->name);
2057 printf("Supported file protocols:");
2058 for(up = first_protocol; up != NULL; up = up->next)
2059 printf(" %s:", up->name);
2062 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2063 printf("Motion estimation methods:");
2067 if ((pp - motion_str + 1) == ME_ZERO)
2068 printf("(fastest)");
2069 else if ((pp - motion_str + 1) == ME_FULL)
2070 printf("(slowest)");
2071 else if ((pp - motion_str + 1) == ME_EPZS)
2072 printf("(default)");
2079 void show_help(void)
2082 const OptionDef *po;
2085 prog = do_play ? "ffplay" : "ffmpeg";
2087 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2091 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2092 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2094 printf("usage: ffplay [options] input_file...\n"
2095 "Simple audio player\n");
2099 "Main options are:\n");
2102 printf("\nAdvanced options are:\n");
2103 for(po = options; po->name != NULL; po++) {
2105 expert = (po->flags & OPT_EXPERT) != 0;
2107 strcpy(buf, po->name);
2108 if (po->flags & HAS_ARG) {
2110 strcat(buf, po->argname);
2112 printf("-%-17s %s\n", buf, po->help);
2120 const OptionDef options[] = {
2121 { "L", 0, {(void*)show_licence}, "show license" },
2122 { "h", 0, {(void*)show_help}, "show help" },
2123 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2124 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2125 { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
2126 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2127 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2128 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2129 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2130 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2131 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2132 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2133 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2135 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2136 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2137 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2138 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2139 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2140 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2141 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2142 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2143 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2144 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2145 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2146 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2147 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
2148 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2149 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2150 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2151 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2152 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2153 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2155 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2156 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2157 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2158 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2159 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2160 { "sameq", OPT_BOOL, {(void*)&same_quality},
2161 "use same video quality as source (implies VBR)" },
2163 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2164 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2165 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2166 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2167 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2168 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2169 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2170 "deinterlace pictures" },
2171 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2172 "add timings for benchmarking" },
2173 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2174 "dump each input packet" },
2175 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2176 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2177 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2181 int main(int argc, char **argv)
2184 const char *opt, *arg;
2185 const OptionDef *po;
2190 /* detect if invoked as player */
2191 i = strlen(argv[0]);
2192 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2200 while (optindex < argc) {
2201 opt = argv[optindex++];
2203 if (opt[0] == '-' && opt[1] != '\0') {
2205 while (po->name != NULL) {
2206 if (!strcmp(opt + 1, po->name))
2211 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2215 if (po->flags & HAS_ARG)
2216 arg = argv[optindex++];
2217 if (po->flags & OPT_STRING) {
2220 *po->u.str_arg = str;
2221 } else if (po->flags & OPT_BOOL) {
2224 po->u.func_arg(arg);
2228 opt_output_file(opt);
2230 opt_input_file(opt);
2237 /* file converter / grab */
2238 if (nb_output_files <= 0) {
2239 fprintf(stderr, "Must supply at least one output file\n");
2243 if (nb_input_files == 0) {
2248 if (nb_input_files <= 0) {
2249 fprintf(stderr, "Must supply at least one input file\n");
2256 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2257 stream_maps, nb_stream_maps);
2258 ti = getutime() - ti;
2260 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2264 for(i=0;i<nb_output_files;i++) {
2265 if (!(output_files[i]->oformat->flags & AVFMT_NOFILE))
2266 url_fclose(&output_files[i]->pb);
2268 for(i=0;i<nb_input_files;i++)
2269 av_close_input_file(input_files[i]);