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>
35 #define MAXINT64 INT64_C(0x7fffffffffffffff)
40 #define HAS_ARG 0x0001
41 #define OPT_BOOL 0x0002
42 #define OPT_EXPERT 0x0004
43 #define OPT_STRING 0x0008
53 /* select an input stream for an output stream */
54 typedef struct AVStreamMap {
59 extern const OptionDef options[];
65 static AVFormatContext *input_files[MAX_FILES];
66 static int nb_input_files = 0;
68 static AVFormatContext *output_files[MAX_FILES];
69 static int nb_output_files = 0;
71 static AVStreamMap stream_maps[MAX_FILES];
72 static int nb_stream_maps;
74 static AVInputFormat *file_iformat;
75 static AVOutputFormat *file_oformat;
76 static int frame_width = 160;
77 static int frame_height = 128;
78 static int frame_rate = 25 * FRAME_RATE_BASE;
79 static int video_bit_rate = 200000;
80 static int video_bit_rate_tolerance = 200000;
81 static int video_qscale = 0;
82 static int video_qmin = 3;
83 static int video_qmax = 15;
84 static int video_qdiff = 3;
85 static float video_qblur = 0.5;
86 static float video_qcomp = 0.5;
87 static int me_method = 0;
88 static int video_disable = 0;
89 static int video_codec_id = CODEC_ID_NONE;
90 static int same_quality = 0;
91 static int b_frames = 0;
92 static int use_hq = 0;
93 static int use_4mv = 0;
94 static int do_deinterlace = 0;
96 static int gop_size = 12;
97 static int intra_only = 0;
98 static int audio_sample_rate = 44100;
99 static int audio_bit_rate = 64000;
100 static int audio_disable = 0;
101 static int audio_channels = 1;
102 static int audio_codec_id = CODEC_ID_NONE;
104 static INT64 recording_time = 0;
105 static int file_overwrite = 0;
106 static char *str_title = NULL;
107 static char *str_author = NULL;
108 static char *str_copyright = NULL;
109 static char *str_comment = NULL;
110 static int do_benchmark = 0;
111 static int do_hex_dump = 0;
112 static int do_play = 0;
113 static int do_psnr = 0;
114 static int do_vstats = 0;
115 static int mpeg_vcd = 0;
117 #ifndef CONFIG_AUDIO_OSS
118 const char *audio_device = "none";
120 #ifndef CONFIG_VIDEO4LINUX
121 const char *v4l_device = "none";
124 typedef struct AVOutputStream {
125 int file_index; /* file index */
126 int index; /* stream index in the output file */
127 int source_index; /* AVInputStream index */
128 AVStream *st; /* stream in the output file */
129 int encoding_needed; /* true if encoding needed for this stream */
132 AVPicture pict_tmp; /* temporary image for resizing */
134 ImgReSampleContext *img_resample_ctx; /* for image resampling */
138 ReSampleContext *resample; /* for audio resampling */
139 FifoBuffer fifo; /* for compression: one audio fifo per codec */
142 typedef struct AVInputStream {
146 int discard; /* true if stream data should be discarded */
147 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
148 Ticker pts_ticker; /* Ticker for PTS calculation */
149 int ticker_inited; /* to signal if the ticker was initialized */
150 INT64 pts; /* current pts */
151 int pts_increment; /* expected pts increment for next packet */
152 int frame_number; /* current frame */
153 INT64 sample_index; /* current sample */
156 typedef struct AVInputFile {
157 int eof_reached; /* true if eof reached */
158 int ist_index; /* index of first stream in ist_table */
159 int buffer_size; /* current total buffer size */
160 int buffer_size_max; /* buffer size at which we consider we can stop
162 int nb_streams; /* nb streams we are aware of */
167 /* init terminal so that we can grab keys */
168 static struct termios oldtty;
170 static void term_exit(void)
172 tcsetattr (0, TCSANOW, &oldtty);
175 static void term_init(void)
182 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
183 |INLCR|IGNCR|ICRNL|IXON);
184 tty.c_oflag |= OPOST;
185 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
186 tty.c_cflag &= ~(CSIZE|PARENB);
191 tcsetattr (0, TCSANOW, &tty);
196 /* read a key without blocking */
197 static int read_key(void)
208 n = select(1, &rfds, NULL, NULL, &tv);
221 /* no interactive support */
222 static void term_exit(void)
226 static void term_init(void)
230 static int read_key(void)
237 int read_ffserver_streams(AVFormatContext *s, const char *filename)
242 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
245 /* copy stream format */
246 s->nb_streams = ic->nb_streams;
247 for(i=0;i<ic->nb_streams;i++) {
249 st = av_mallocz(sizeof(AVFormatContext));
250 memcpy(st, ic->streams[i], sizeof(AVStream));
254 av_close_input_file(ic);
258 #define MAX_AUDIO_PACKET_SIZE 16384
260 static void do_audio_out(AVFormatContext *s,
263 unsigned char *buf, int size)
266 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
267 UINT8 audio_out[MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
268 int size_out, frame_bytes, ret;
271 enc = &ost->st->codec;
273 if (ost->audio_resample) {
275 size_out = audio_resample(ost->resample,
276 (short *)buftmp, (short *)buf,
277 size / (ist->st->codec.channels * 2));
278 size_out = size_out * enc->channels * 2;
284 /* now encode as many frames as possible */
285 if (enc->frame_size > 1) {
286 /* output resampled raw samples */
287 fifo_write(&ost->fifo, buftmp, size_out,
290 frame_bytes = enc->frame_size * 2 * enc->channels;
292 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
293 &ost->fifo.rptr) == 0) {
294 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
296 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
299 /* output a pcm frame */
300 /* XXX: change encoding codec API to avoid this ? */
301 switch(enc->codec->id) {
302 case CODEC_ID_PCM_S16LE:
303 case CODEC_ID_PCM_S16BE:
304 case CODEC_ID_PCM_U16LE:
305 case CODEC_ID_PCM_U16BE:
308 size_out = size_out >> 1;
311 ret = avcodec_encode_audio(enc, audio_out, size_out,
313 s->oformat->write_packet(s, ost->index, audio_out, ret, 0);
317 /* write a picture to a raw mux */
318 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
319 int pix_fmt, int w, int h)
321 UINT8 *buf, *src, *dest;
324 /* XXX: not efficient, should add test if we can take
325 directly the AVPicture */
327 case PIX_FMT_YUV420P:
328 size = avpicture_get_size(pix_fmt, w, h);
329 buf = av_malloc(size);
338 src = picture->data[i];
340 memcpy(dest, src, w);
342 src += picture->linesize[i];
346 case PIX_FMT_YUV422P:
348 buf = av_malloc(size);
356 src = picture->data[i];
358 memcpy(dest, src, w);
360 src += picture->linesize[i];
364 case PIX_FMT_YUV444P:
366 buf = av_malloc(size);
371 src = picture->data[i];
373 memcpy(dest, src, w);
375 src += picture->linesize[i];
381 buf = av_malloc(size);
385 src = picture->data[0];
387 memcpy(dest, src, w * 2);
389 src += picture->linesize[0];
395 buf = av_malloc(size);
399 src = picture->data[0];
401 memcpy(dest, src, w * 3);
403 src += picture->linesize[0];
409 s->oformat->write_packet(s, index, buf, size, 0);
414 static void do_video_out(AVFormatContext *s,
420 int n1, n2, nb, i, ret, frame_number, dec_frame_rate;
421 AVPicture *picture, *picture2, *pict;
422 AVPicture picture_tmp1, picture_tmp2;
423 static UINT8 *video_buffer;
424 UINT8 *buf = NULL, *buf1 = NULL;
425 AVCodecContext *enc, *dec;
427 #define VIDEO_BUFFER_SIZE (1024*1024)
429 enc = &ost->st->codec;
430 dec = &ist->st->codec;
432 frame_number = ist->frame_number;
433 dec_frame_rate = ist->st->r_frame_rate;
434 // fprintf(stderr, "\n%d", dec_frame_rate);
435 /* first drop frame if needed */
436 n1 = ((INT64)frame_number * enc->frame_rate) / dec_frame_rate;
437 n2 = (((INT64)frame_number + 1) * enc->frame_rate) / dec_frame_rate;
443 video_buffer= av_malloc(VIDEO_BUFFER_SIZE);
444 if(!video_buffer) return;
446 /* deinterlace : must be done before any resize */
447 if (do_deinterlace) {
450 /* create temporary picture */
451 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
452 buf1 = av_malloc(size);
456 picture2 = &picture_tmp2;
457 avpicture_fill(picture2, buf1, dec->pix_fmt, dec->width, dec->height);
459 if (avpicture_deinterlace(picture2, picture1,
460 dec->pix_fmt, dec->width, dec->height) < 0) {
461 /* if error, do not deinterlace */
470 /* convert pixel format if needed */
471 if (enc->pix_fmt != dec->pix_fmt) {
474 /* create temporary picture */
475 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
476 buf = av_malloc(size);
479 pict = &picture_tmp1;
480 avpicture_fill(pict, buf, enc->pix_fmt, dec->width, dec->height);
482 if (img_convert(pict, enc->pix_fmt,
483 picture2, dec->pix_fmt,
484 dec->width, dec->height) < 0) {
485 fprintf(stderr, "pixel format conversion not handled\n");
492 /* XXX: resampling could be done before raw format convertion in
493 some cases to go faster */
494 /* XXX: only works for YUV420P */
495 if (ost->video_resample) {
496 picture = &ost->pict_tmp;
497 img_resample(ost->img_resample_ctx, picture, pict);
502 /* duplicates frame if needed */
503 /* XXX: pb because no interleaving */
505 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
506 /* handles sameq here. This is not correct because it may
507 not be a global option */
509 enc->quality = dec->quality;
512 ret = avcodec_encode_video(enc,
513 video_buffer, VIDEO_BUFFER_SIZE,
515 //enc->frame_number = enc->real_pict_num;
516 s->oformat->write_packet(s, ost->index, video_buffer, ret, 0);
518 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
519 // enc->frame_number-1, enc->real_pict_num, ret,
522 write_picture(s, ost->index, picture, enc->pix_fmt, enc->width, enc->height);
530 static void do_video_stats(AVOutputStream *ost,
534 static FILE *fvstats=NULL;
535 static INT64 total_size = 0;
542 double ti1, bitrate, avg_bitrate;
546 today = localtime(&today2);
547 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
550 fvstats = fopen(filename,"w");
558 enc = &ost->st->codec;
559 total_size += frame_size;
560 if (enc->codec_type == CODEC_TYPE_VIDEO) {
561 frame_number = ist->frame_number;
562 fprintf(fvstats, "frame= %5d q= %2d ", frame_number, enc->quality);
564 fprintf(fvstats, "PSNR= %6.2f ", enc->psnr_y);
566 fprintf(fvstats,"f_size= %6d ", frame_size);
567 /* compute min pts value */
568 if (!ist->discard && ist->pts < ti) {
571 ti1 = (double)ti / 1000000.0;
575 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
576 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
577 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
578 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
579 fprintf(fvstats,"type= %s\n", enc->key_frame == 1 ? "I" : "P");
587 * The following code is the main loop of the file converter
589 static int av_encode(AVFormatContext **output_files,
591 AVFormatContext **input_files,
593 AVStreamMap *stream_maps, int nb_stream_maps)
595 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
596 AVFormatContext *is, *os;
597 AVCodecContext *codec, *icodec;
598 AVOutputStream *ost, **ost_table = NULL;
599 AVInputStream *ist, **ist_table = NULL;
600 INT64 min_pts, start_time;
601 AVInputFile *file_table;
602 AVFormatContext *stream_no_data;
605 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
609 /* input stream init */
611 for(i=0;i<nb_input_files;i++) {
613 file_table[i].ist_index = j;
614 file_table[i].nb_streams = is->nb_streams;
619 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
623 for(i=0;i<nb_istreams;i++) {
624 ist = av_mallocz(sizeof(AVInputStream));
630 for(i=0;i<nb_input_files;i++) {
632 for(k=0;k<is->nb_streams;k++) {
633 ist = ist_table[j++];
634 ist->st = is->streams[k];
637 ist->discard = 1; /* the stream is discarded by default
642 /* output stream init */
644 for(i=0;i<nb_output_files;i++) {
645 os = output_files[i];
646 nb_ostreams += os->nb_streams;
648 os->flags |= AVF_FLAG_VCD;
650 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
651 fprintf(stderr, "Number of stream maps must match number of output streams\n");
655 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
658 for(i=0;i<nb_ostreams;i++) {
659 ost = av_mallocz(sizeof(AVOutputStream));
666 for(k=0;k<nb_output_files;k++) {
667 os = output_files[k];
668 for(i=0;i<os->nb_streams;i++) {
670 ost = ost_table[n++];
673 ost->st = os->streams[i];
674 if (nb_stream_maps > 0) {
675 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
676 stream_maps[n-1].stream_index;
678 /* get corresponding input stream index : we select the first one with the right type */
680 for(j=0;j<nb_istreams;j++) {
683 ist->st->codec.codec_type == ost->st->codec.codec_type) {
684 ost->source_index = j;
690 /* try again and reuse existing stream */
691 for(j=0;j<nb_istreams;j++) {
693 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
694 ost->source_index = j;
699 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
700 ost->file_index, ost->index);
705 ist = ist_table[ost->source_index];
710 /* dump the stream mapping */
711 fprintf(stderr, "Stream mapping:\n");
712 for(i=0;i<nb_ostreams;i++) {
714 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
715 ist_table[ost->source_index]->file_index,
716 ist_table[ost->source_index]->index,
721 /* for each output stream, we compute the right encoding parameters */
722 for(i=0;i<nb_ostreams;i++) {
724 ist = ist_table[ost->source_index];
726 codec = &ost->st->codec;
727 icodec = &ist->st->codec;
729 switch(codec->codec_type) {
730 case CODEC_TYPE_AUDIO:
731 /* check if same codec with same parameters. If so, no
732 reencoding is needed */
733 if (codec->codec_id == icodec->codec_id &&
734 codec->bit_rate == icodec->bit_rate &&
735 codec->sample_rate == icodec->sample_rate &&
736 codec->channels == icodec->channels) {
738 /* use the same frame size */
739 codec->frame_size = icodec->frame_size;
740 //codec->frame_size = 8*icodec->sample_rate*icodec->frame_size/
742 //fprintf(stderr,"\nFrame size: %d", codec->frame_size);
744 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
747 if (codec->channels == icodec->channels &&
748 codec->sample_rate == icodec->sample_rate) {
749 ost->audio_resample = 0;
751 if (codec->channels != icodec->channels &&
752 icodec->codec_id == CODEC_ID_AC3) {
753 /* Special case for 5:1 AC3 input */
754 /* and mono or stereo output */
755 /* Request specific number of channels */
756 icodec->channels = codec->channels;
757 if (codec->sample_rate == icodec->sample_rate)
758 ost->audio_resample = 0;
760 ost->audio_resample = 1;
761 ost->resample = audio_resample_init(codec->channels, icodec->channels,
763 icodec->sample_rate);
765 /* Request specific number of channels */
766 icodec->channels = codec->channels;
768 ost->audio_resample = 1;
769 ost->resample = audio_resample_init(codec->channels, icodec->channels,
771 icodec->sample_rate);
774 ist->decoding_needed = 1;
775 ost->encoding_needed = 1;
778 case CODEC_TYPE_VIDEO:
779 /* check if same codec with same parameters. If so, no
780 reencoding is needed */
781 if (codec->codec_id == icodec->codec_id &&
782 codec->bit_rate == icodec->bit_rate &&
783 codec->frame_rate == icodec->frame_rate &&
784 codec->width == icodec->width &&
785 codec->height == icodec->height) {
788 if (codec->width == icodec->width &&
789 codec->height == icodec->height) {
790 ost->video_resample = 0;
793 ost->video_resample = 1;
794 buf = av_malloc((codec->width * codec->height * 3) / 2);
797 ost->pict_tmp.data[0] = buf;
798 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
799 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
800 ost->pict_tmp.linesize[0] = codec->width;
801 ost->pict_tmp.linesize[1] = codec->width / 2;
802 ost->pict_tmp.linesize[2] = codec->width / 2;
804 ost->img_resample_ctx = img_resample_init(
805 ost->st->codec.width, ost->st->codec.height,
806 ist->st->codec.width, ist->st->codec.height);
808 ost->encoding_needed = 1;
809 ist->decoding_needed = 1;
817 /* open each encoder */
818 for(i=0;i<nb_ostreams;i++) {
820 if (ost->encoding_needed) {
822 codec = avcodec_find_encoder(ost->st->codec.codec_id);
824 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
825 ost->file_index, ost->index);
828 if (avcodec_open(&ost->st->codec, codec) < 0) {
829 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
830 ost->file_index, ost->index);
836 /* open each decoder */
837 for(i=0;i<nb_istreams;i++) {
839 if (ist->decoding_needed) {
841 codec = avcodec_find_decoder(ist->st->codec.codec_id);
843 fprintf(stderr, "Unsupported codec for input stream #%d.%d\n",
844 ist->file_index, ist->index);
847 if (avcodec_open(&ist->st->codec, codec) < 0) {
848 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
849 ist->file_index, ist->index);
852 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
853 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
858 for(i=0;i<nb_istreams;i++) {
861 ist->frame_number = 0;
864 /* compute buffer size max (should use a complete heuristic) */
865 for(i=0;i<nb_input_files;i++) {
866 file_table[i].buffer_size_max = 2048;
869 /* open files and write file headers */
870 for(i=0;i<nb_output_files;i++) {
871 os = output_files[i];
872 if (av_write_header(os) < 0) {
873 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
881 fprintf(stderr, "Press [q] to stop encoding\n");
883 fprintf(stderr, "Press [q] to stop playing\n");
888 start_time = gettime();
894 int file_index, ist_index;
899 int data_size, got_picture;
901 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
904 /* if 'q' pressed, exits */
906 /* read_key() returns 0 on EOF */
912 /* select the input file with the smallest pts */
915 for(i=0;i<nb_istreams;i++) {
917 /* For some reason, the pts_increment code breaks q estimation?!? */
918 if (!ist->discard && !file_table[ist->file_index].eof_reached &&
919 ist->pts /* + ist->pts_increment */ < min_pts && input_files[ist->file_index] != stream_no_data) {
920 min_pts = ist->pts /* + ist->pts_increment */;
921 file_index = ist->file_index;
924 /* if none, if is finished */
925 if (file_index < 0) {
926 if (stream_no_data) {
931 ts.tv_nsec = 1000 * 1000 * 10;
939 /* finish if recording time exhausted */
940 if (recording_time > 0 && min_pts >= recording_time)
942 /* read a packet from it and output it in the fifo */
943 is = input_files[file_index];
944 if (av_read_packet(is, &pkt) < 0) {
945 file_table[file_index].eof_reached = 1;
953 /* the following test is needed in case new streams appear
954 dynamically in stream : we ignore them */
955 if (pkt.stream_index >= file_table[file_index].nb_streams)
957 ist_index = file_table[file_index].ist_index + pkt.stream_index;
958 ist = ist_table[ist_index];
962 if (pkt.flags & PKT_FLAG_DROPPED_FRAME)
966 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
967 av_hex_dump(pkt.data, pkt.size);
970 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
976 /* decode the packet if needed */
977 data_buf = NULL; /* fail safe */
979 if (ist->decoding_needed) {
980 switch(ist->st->codec.codec_type) {
981 case CODEC_TYPE_AUDIO:
982 /* XXX: could avoid copy if PCM 16 bits with same
984 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
988 /* Some bug in mpeg audio decoder gives */
989 /* data_size < 0, it seems they are overflows */
990 if (data_size <= 0) {
996 data_buf = (UINT8 *)samples;
998 case CODEC_TYPE_VIDEO:
999 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1001 size = (ist->st->codec.width * ist->st->codec.height);
1002 avpicture_fill(&picture, ptr,
1003 ist->st->codec.pix_fmt,
1004 ist->st->codec.width,
1005 ist->st->codec.height);
1008 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1009 ret = avcodec_decode_video(&ist->st->codec,
1010 &picture, &got_picture, ptr, len);
1013 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1014 ist->file_index, ist->index);
1015 av_free_packet(&pkt);
1019 /* no picture yet */
1036 if (!ist->ticker_inited) {
1037 switch (ist->st->codec.codec_type) {
1038 case CODEC_TYPE_AUDIO:
1039 ticker_init(&ist->pts_ticker,
1040 (INT64)ist->st->codec.sample_rate,
1042 ist->ticker_inited = 1;
1044 case CODEC_TYPE_VIDEO:
1045 ticker_init(&ist->pts_ticker,
1046 (INT64)ist->st->r_frame_rate,
1047 ((INT64)1000000 * FRAME_RATE_BASE));
1048 ist->ticker_inited = 1;
1055 switch(ist->st->codec.codec_type) {
1056 case CODEC_TYPE_AUDIO:
1057 //ist->pts = (INT64)1000000 * ist->sample_index / ist->st->codec.sample_rate;
1058 ist->pts = ticker_tick(&ist->pts_ticker, ist->sample_index);
1059 ist->sample_index += data_size / (2 * ist->st->codec.channels);
1060 ist->pts_increment = (INT64) (data_size / (2 * ist->st->codec.channels)) * 1000000 / ist->st->codec.sample_rate;
1062 case CODEC_TYPE_VIDEO:
1063 ist->frame_number++;
1064 //ist->pts = ((INT64)ist->frame_number * 1000000 * FRAME_RATE_BASE) /
1065 // ist->st->codec.frame_rate;
1066 ist->pts = ticker_tick(&ist->pts_ticker, ist->frame_number);
1067 ist->pts_increment = ((INT64) 1000000 * FRAME_RATE_BASE) /
1068 ist->st->codec.frame_rate;
1076 /* transcode raw format, encode packets and output them */
1078 for(i=0;i<nb_ostreams;i++) {
1081 if (ost->source_index == ist_index) {
1082 os = output_files[ost->file_index];
1084 if (ost->encoding_needed) {
1085 switch(ost->st->codec.codec_type) {
1086 case CODEC_TYPE_AUDIO:
1087 do_audio_out(os, ost, ist, data_buf, data_size);
1089 case CODEC_TYPE_VIDEO:
1090 do_video_out(os, ost, ist, &picture, &frame_size);
1092 do_video_stats(ost, ist, frame_size);
1098 /* no reencoding needed : output the packet directly */
1099 /* force the input stream PTS */
1100 os->oformat->write_packet(os, ost->index, data_buf, data_size, pkt.pts);
1106 av_free_packet(&pkt);
1108 /* dump report by using the first video and audio streams */
1111 AVFormatContext *oc;
1112 INT64 total_size, ti;
1113 AVCodecContext *enc;
1114 int frame_number, vid;
1115 double bitrate, ti1;
1116 static INT64 last_time;
1118 if ((min_pts - last_time) >= 500000) {
1119 last_time = min_pts;
1121 oc = output_files[0];
1123 total_size = url_ftell(&oc->pb);
1128 for(i=0;i<nb_ostreams;i++) {
1130 enc = &ost->st->codec;
1131 ist = ist_table[ost->source_index];
1132 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1133 frame_number = ist->frame_number;
1134 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1135 frame_number, enc->quality);
1137 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1140 /* compute min pts value */
1141 if (!ist->discard && ist->pts < ti) {
1146 ti1 = (double)ti / 1000000.0;
1149 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1151 sprintf(buf + strlen(buf),
1152 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1153 (double)total_size / 1024, ti1, bitrate);
1155 fprintf(stderr, "%s \r", buf);
1162 /* dump report by using the first video and audio streams */
1165 AVFormatContext *oc;
1166 INT64 total_size, ti;
1167 AVCodecContext *enc;
1168 int frame_number, vid;
1169 double bitrate, ti1;
1171 oc = output_files[0];
1173 total_size = url_ftell(&oc->pb);
1178 for(i=0;i<nb_ostreams;i++) {
1180 enc = &ost->st->codec;
1181 ist = ist_table[ost->source_index];
1182 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
1183 frame_number = ist->frame_number;
1184 sprintf(buf + strlen(buf), "frame=%5d q=%2d ",
1185 frame_number, enc->quality);
1187 sprintf(buf + strlen(buf), "PSNR=%6.2f ", enc->psnr_y);
1190 /* compute min pts value */
1191 if (!ist->discard && ist->pts < ti) {
1196 ti1 = ti / 1000000.0;
1199 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
1201 sprintf(buf + strlen(buf),
1202 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
1203 (double)total_size / 1024, ti1, bitrate);
1205 fprintf(stderr, "%s \n", buf);
1207 /* close each encoder */
1208 for(i=0;i<nb_ostreams;i++) {
1210 if (ost->encoding_needed) {
1211 avcodec_close(&ost->st->codec);
1215 /* close each decoder */
1216 for(i=0;i<nb_istreams;i++) {
1218 if (ist->decoding_needed) {
1219 avcodec_close(&ist->st->codec);
1224 /* write the trailer if needed and close file */
1225 for(i=0;i<nb_output_files;i++) {
1226 os = output_files[i];
1227 av_write_trailer(os);
1233 av_free(file_table);
1236 for(i=0;i<nb_istreams;i++) {
1243 for(i=0;i<nb_ostreams;i++) {
1246 fifo_free(&ost->fifo); /* works even if fifo is not
1247 initialized but set to zero */
1248 av_free(ost->pict_tmp.data[0]);
1249 if (ost->video_resample)
1250 img_resample_close(ost->img_resample_ctx);
1251 if (ost->audio_resample)
1252 audio_resample_close(ost->resample);
1265 int file_read(const char *filename)
1268 unsigned char buffer[1024];
1271 if (url_open(&h, filename, O_RDONLY) < 0) {
1272 printf("could not open '%s'\n", filename);
1276 len = url_read(h, buffer, sizeof(buffer));
1279 for(i=0;i<len;i++) putchar(buffer[i]);
1286 void show_licence(void)
1289 "ffmpeg version " FFMPEG_VERSION "\n"
1290 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1291 "This library is free software; you can redistribute it and/or\n"
1292 "modify it under the terms of the GNU Lesser General Public\n"
1293 "License as published by the Free Software Foundation; either\n"
1294 "version 2 of the License, or (at your option) any later version.\n"
1296 "This library is distributed in the hope that it will be useful,\n"
1297 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1298 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1299 "Lesser General Public License for more details.\n"
1301 "You should have received a copy of the GNU Lesser General Public\n"
1302 "License along with this library; if not, write to the Free Software\n"
1303 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1308 void opt_format(const char *arg)
1310 file_iformat = av_find_input_format(arg);
1311 file_oformat = guess_format(arg, NULL, NULL);
1312 if (!file_iformat && !file_oformat) {
1313 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1318 void opt_video_bitrate(const char *arg)
1320 video_bit_rate = atoi(arg) * 1000;
1323 void opt_video_bitrate_tolerance(const char *arg)
1325 video_bit_rate_tolerance = atoi(arg) * 1000;
1328 void opt_frame_rate(const char *arg)
1330 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1333 void opt_frame_size(const char *arg)
1335 parse_image_size(&frame_width, &frame_height, arg);
1336 if (frame_width <= 0 || frame_height <= 0) {
1337 fprintf(stderr, "Incorrect frame size\n");
1340 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1341 fprintf(stderr, "Frame size must be a multiple of 2\n");
1346 void opt_gop_size(const char *arg)
1348 gop_size = atoi(arg);
1351 void opt_b_frames(const char *arg)
1353 b_frames = atoi(arg);
1354 if (b_frames > FF_MAX_B_FRAMES) {
1355 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1357 } else if (b_frames < 1) {
1358 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1363 void opt_qscale(const char *arg)
1365 video_qscale = atoi(arg);
1366 if (video_qscale < 0 ||
1367 video_qscale > 31) {
1368 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1373 void opt_qmin(const char *arg)
1375 video_qmin = atoi(arg);
1376 if (video_qmin < 0 ||
1378 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1383 void opt_qmax(const char *arg)
1385 video_qmax = atoi(arg);
1386 if (video_qmax < 0 ||
1388 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1393 void opt_qdiff(const char *arg)
1395 video_qdiff = atoi(arg);
1396 if (video_qdiff < 0 ||
1398 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1403 void opt_qblur(const char *arg)
1405 video_qblur = atof(arg);
1408 void opt_qcomp(const char *arg)
1410 video_qcomp = atof(arg);
1413 void opt_audio_bitrate(const char *arg)
1415 audio_bit_rate = atoi(arg) * 1000;
1418 void opt_audio_rate(const char *arg)
1420 audio_sample_rate = atoi(arg);
1423 void opt_audio_channels(const char *arg)
1425 audio_channels = atoi(arg);
1428 void opt_video_device(const char *arg)
1430 v4l_device = strdup(arg);
1433 void opt_audio_device(const char *arg)
1435 audio_device = strdup(arg);
1438 void opt_audio_codec(const char *arg)
1444 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1449 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1452 audio_codec_id = p->id;
1456 const char *motion_str[] = {
1466 void opt_motion_estimation(const char *arg)
1472 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1475 if (!strcmp(*p, arg))
1479 me_method = (p - motion_str) + 1;
1482 void opt_video_codec(const char *arg)
1488 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1493 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1496 video_codec_id = p->id;
1500 void opt_map(const char *arg)
1506 m = &stream_maps[nb_stream_maps++];
1508 m->file_index = strtol(arg, (char **)&p, 0);
1512 m->stream_index = strtol(p, (char **)&p, 0);
1515 void opt_recording_time(const char *arg)
1517 recording_time = parse_date(arg, 1);
1520 void print_error(const char *filename, int err)
1523 case AVERROR_NUMEXPECTED:
1524 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1525 "Use '%%d' to specify the image number:\n"
1526 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1527 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1530 case AVERROR_INVALIDDATA:
1531 fprintf(stderr, "%s: Error while parsing header\n", filename);
1534 fprintf(stderr, "%s: Unknown format\n", filename);
1537 fprintf(stderr, "%s: Error while opening file\n", filename);
1542 void opt_input_file(const char *filename)
1544 AVFormatContext *ic;
1545 AVFormatParameters params, *ap = ¶ms;
1546 int err, i, ret, rfps;
1548 /* get default parameters from command line */
1549 memset(ap, 0, sizeof(*ap));
1550 ap->sample_rate = audio_sample_rate;
1551 ap->channels = audio_channels;
1552 ap->frame_rate = frame_rate;
1553 ap->width = frame_width;
1554 ap->height = frame_height;
1556 /* open the input file with generic libav function */
1557 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
1559 print_error(filename, err);
1563 /* If not enough info to get the stream parameters, we decode the
1564 first frames to get it. (used in mpeg case for example) */
1565 ret = av_find_stream_info(ic);
1567 fprintf(stderr, "%s: could not find codec parameters\n", filename);
1571 /* update the current parameters so that they match the one of the input stream */
1572 for(i=0;i<ic->nb_streams;i++) {
1573 AVCodecContext *enc = &ic->streams[i]->codec;
1574 switch(enc->codec_type) {
1575 case CODEC_TYPE_AUDIO:
1576 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
1577 audio_channels = enc->channels;
1578 audio_sample_rate = enc->sample_rate;
1580 case CODEC_TYPE_VIDEO:
1581 frame_height = enc->height;
1582 frame_width = enc->width;
1583 rfps = ic->streams[i]->r_frame_rate;
1584 if (enc->frame_rate != rfps) {
1585 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
1586 i, (float)enc->frame_rate / FRAME_RATE_BASE,
1587 (float)rfps / FRAME_RATE_BASE);
1589 /* update the current frame rate to match the stream frame rate */
1597 input_files[nb_input_files] = ic;
1598 /* dump the file content */
1599 dump_format(ic, nb_input_files, filename, 0);
1601 file_iformat = NULL;
1602 file_oformat = NULL;
1605 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
1607 int has_video, has_audio, i, j;
1608 AVFormatContext *ic;
1612 for(j=0;j<nb_input_files;j++) {
1613 ic = input_files[j];
1614 for(i=0;i<ic->nb_streams;i++) {
1615 AVCodecContext *enc = &ic->streams[i]->codec;
1616 switch(enc->codec_type) {
1617 case CODEC_TYPE_AUDIO:
1620 case CODEC_TYPE_VIDEO:
1628 *has_video_ptr = has_video;
1629 *has_audio_ptr = has_audio;
1632 void opt_output_file(const char *filename)
1635 AVFormatContext *oc;
1636 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
1639 if (!strcmp(filename, "-"))
1642 oc = av_mallocz(sizeof(AVFormatContext));
1644 if (!file_oformat) {
1645 file_oformat = guess_format(NULL, filename, NULL);
1646 if (!file_oformat) {
1647 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
1653 oc->oformat = file_oformat;
1655 if (!strcmp(file_oformat->name, "ffm") &&
1656 strstart(filename, "http:", NULL)) {
1657 /* special case for files sent to ffserver: we get the stream
1658 parameters from ffserver */
1659 if (read_ffserver_streams(oc, filename) < 0) {
1660 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
1664 use_video = file_oformat->video_codec != CODEC_ID_NONE;
1665 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
1667 /* disable if no corresponding type found and at least one
1669 if (nb_input_files > 0) {
1670 check_audio_video_inputs(&input_has_video, &input_has_audio);
1671 if (!input_has_video)
1673 if (!input_has_audio)
1677 /* manual disable */
1678 if (audio_disable) {
1681 if (video_disable) {
1687 AVCodecContext *video_enc;
1689 st = av_mallocz(sizeof(AVStream));
1691 fprintf(stderr, "Could not alloc stream\n");
1694 video_enc = &st->codec;
1696 codec_id = file_oformat->video_codec;
1697 if (video_codec_id != CODEC_ID_NONE)
1698 codec_id = video_codec_id;
1700 video_enc->codec_id = codec_id;
1701 video_enc->codec_type = CODEC_TYPE_VIDEO;
1703 video_enc->bit_rate = video_bit_rate;
1704 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
1705 video_enc->frame_rate = frame_rate;
1707 video_enc->width = frame_width;
1708 video_enc->height = frame_height;
1710 video_enc->gop_size = gop_size;
1712 video_enc->gop_size = 0;
1713 if (video_qscale || same_quality) {
1714 video_enc->flags |= CODEC_FLAG_QSCALE;
1715 video_enc->quality = video_qscale;
1719 video_enc->flags |= CODEC_FLAG_HQ;
1723 video_enc->flags |= CODEC_FLAG_HQ;
1724 video_enc->flags |= CODEC_FLAG_4MV;
1728 if (codec_id != CODEC_ID_MPEG4) {
1729 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
1732 video_enc->max_b_frames = b_frames;
1733 video_enc->b_frame_strategy = 0;
1734 video_enc->b_quant_factor = 2.0;
1737 video_enc->qmin = video_qmin;
1738 video_enc->qmax = video_qmax;
1739 video_enc->max_qdiff = video_qdiff;
1740 video_enc->qblur = video_qblur;
1741 video_enc->qcompress = video_qcomp;
1744 video_enc->get_psnr = 1;
1746 video_enc->get_psnr = 0;
1748 video_enc->me_method = me_method;
1750 /* XXX: need to find a way to set codec parameters */
1751 if (oc->oformat->flags & AVFMT_RGB24) {
1752 video_enc->pix_fmt = PIX_FMT_RGB24;
1755 oc->streams[nb_streams] = st;
1760 AVCodecContext *audio_enc;
1762 st = av_mallocz(sizeof(AVStream));
1764 fprintf(stderr, "Could not alloc stream\n");
1767 audio_enc = &st->codec;
1768 codec_id = file_oformat->audio_codec;
1769 if (audio_codec_id != CODEC_ID_NONE)
1770 codec_id = audio_codec_id;
1771 audio_enc->codec_id = codec_id;
1772 audio_enc->codec_type = CODEC_TYPE_AUDIO;
1774 audio_enc->bit_rate = audio_bit_rate;
1775 audio_enc->sample_rate = audio_sample_rate;
1776 /* For audio codecs other than AC3 we limit */
1777 /* the number of coded channels to stereo */
1778 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
1779 audio_enc->channels = 2;
1781 audio_enc->channels = audio_channels;
1782 oc->streams[nb_streams] = st;
1786 oc->nb_streams = nb_streams;
1789 fprintf(stderr, "No audio or video streams available\n");
1794 pstrcpy(oc->title, sizeof(oc->title), str_title);
1796 pstrcpy(oc->author, sizeof(oc->author), str_author);
1798 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
1800 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
1803 output_files[nb_output_files] = oc;
1804 /* dump the file content */
1805 dump_format(oc, nb_output_files, filename, 1);
1808 strcpy(oc->filename, filename);
1810 /* check filename in case of an image number is expected */
1811 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
1812 if (filename_number_test(oc->filename) < 0) {
1813 print_error(oc->filename, AVERROR_NUMEXPECTED);
1818 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
1819 /* test if it already exists to avoid loosing precious files */
1820 if (!file_overwrite &&
1821 (strchr(filename, ':') == NULL ||
1822 strstart(filename, "file:", NULL))) {
1823 if (url_exist(filename)) {
1826 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
1829 if (toupper(c) != 'Y') {
1830 fprintf(stderr, "Not overwriting - exiting\n");
1837 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
1838 fprintf(stderr, "Could not open '%s'\n", filename);
1843 /* reset some options */
1844 file_oformat = NULL;
1845 file_iformat = NULL;
1848 audio_codec_id = CODEC_ID_NONE;
1849 video_codec_id = CODEC_ID_NONE;
1852 /* prepare dummy protocols for grab */
1853 void prepare_grab(void)
1855 int has_video, has_audio, i, j;
1856 AVFormatContext *oc;
1857 AVFormatContext *ic;
1858 AVFormatParameters ap1, *ap = &ap1;
1860 /* see if audio/video inputs are needed */
1863 memset(ap, 0, sizeof(*ap));
1864 for(j=0;j<nb_output_files;j++) {
1865 oc = output_files[j];
1866 for(i=0;i<oc->nb_streams;i++) {
1867 AVCodecContext *enc = &oc->streams[i]->codec;
1868 switch(enc->codec_type) {
1869 case CODEC_TYPE_AUDIO:
1870 if (enc->sample_rate > ap->sample_rate)
1871 ap->sample_rate = enc->sample_rate;
1872 if (enc->channels > ap->channels)
1873 ap->channels = enc->channels;
1876 case CODEC_TYPE_VIDEO:
1877 if (enc->width > ap->width)
1878 ap->width = enc->width;
1879 if (enc->height > ap->height)
1880 ap->height = enc->height;
1881 if (enc->frame_rate > ap->frame_rate)
1882 ap->frame_rate = enc->frame_rate;
1891 if (has_video == 0 && has_audio == 0) {
1892 fprintf(stderr, "Output file must have at least one audio or video stream\n");
1897 AVInputFormat *fmt1;
1898 fmt1 = av_find_input_format("video_grab_device");
1899 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
1900 fprintf(stderr, "Could not find video grab device\n");
1903 /* by now video grab has one stream */
1904 ic->streams[0]->r_frame_rate = ap->frame_rate;
1905 input_files[nb_input_files] = ic;
1906 dump_format(ic, nb_input_files, v4l_device, 0);
1910 AVInputFormat *fmt1;
1911 fmt1 = av_find_input_format("audio_device");
1912 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
1913 fprintf(stderr, "Could not find audio grab device\n");
1916 input_files[nb_input_files] = ic;
1917 dump_format(ic, nb_input_files, audio_device, 0);
1922 /* open the necessary output devices for playing */
1923 void prepare_play(void)
1925 file_iformat = NULL;
1926 file_oformat = guess_format("audio_device", NULL, NULL);
1927 if (!file_oformat) {
1928 fprintf(stderr, "Could not find audio device\n");
1932 opt_output_file(audio_device);
1936 #ifndef CONFIG_WIN32
1937 INT64 getutime(void)
1939 struct rusage rusage;
1941 getrusage(RUSAGE_SELF, &rusage);
1942 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
1945 INT64 getutime(void)
1951 extern int ffm_nopts;
1953 void opt_bitexact(void)
1955 avcodec_set_bit_exact();
1956 /* disable generate of real time pts in ffm (need to be supressed anyway) */
1960 void show_formats(void)
1962 AVInputFormat *ifmt;
1963 AVOutputFormat *ofmt;
1968 printf("File formats:\n");
1969 printf(" Encoding:");
1970 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
1971 printf(" %s", ofmt->name);
1974 printf(" Decoding:");
1975 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
1976 printf(" %s", ifmt->name);
1980 printf("Codecs:\n");
1981 printf(" Encoders:");
1982 for(p = first_avcodec; p != NULL; p = p->next) {
1984 printf(" %s", p->name);
1988 printf(" Decoders:");
1989 for(p = first_avcodec; p != NULL; p = p->next) {
1991 printf(" %s", p->name);
1995 printf("Supported file protocols:");
1996 for(up = first_protocol; up != NULL; up = up->next)
1997 printf(" %s:", up->name);
2000 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2001 printf("Motion estimation methods:");
2005 if ((pp - motion_str + 1) == ME_ZERO)
2006 printf("(fastest)");
2007 else if ((pp - motion_str + 1) == ME_FULL)
2008 printf("(slowest)");
2009 else if ((pp - motion_str + 1) == ME_EPZS)
2010 printf("(default)");
2017 void show_help(void)
2020 const OptionDef *po;
2023 prog = do_play ? "ffplay" : "ffmpeg";
2025 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2029 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2030 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2032 printf("usage: ffplay [options] input_file...\n"
2033 "Simple audio player\n");
2037 "Main options are:\n");
2040 printf("\nAdvanced options are:\n");
2041 for(po = options; po->name != NULL; po++) {
2043 expert = (po->flags & OPT_EXPERT) != 0;
2045 strcpy(buf, po->name);
2046 if (po->flags & HAS_ARG) {
2048 strcat(buf, po->argname);
2050 printf("-%-17s %s\n", buf, po->help);
2058 const OptionDef options[] = {
2059 { "L", 0, {(void*)show_licence}, "show license" },
2060 { "h", 0, {(void*)show_help}, "show help" },
2061 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2062 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2063 { "vcd", OPT_BOOL, {(void*)&mpeg_vcd}, "output Video CD MPEG-PS compliant file" },
2064 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2065 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2066 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2067 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2068 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2069 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2070 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2071 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2073 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2074 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2075 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2076 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2077 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2078 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2079 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2080 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2081 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2082 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2083 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2084 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2085 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2086 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2087 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec", "codec" },
2088 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2090 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2091 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2092 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2093 { "sameq", OPT_BOOL, {(void*)&same_quality},
2094 "use same video quality as source (implies VBR)" },
2096 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2097 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2098 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2099 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2100 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2101 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec", "codec" },
2102 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2103 "deinterlace pictures" },
2104 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2105 "add timings for benchmarking" },
2106 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2107 "dump each input packet" },
2108 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2109 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2110 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2114 int main(int argc, char **argv)
2117 const char *opt, *arg;
2118 const OptionDef *po;
2123 /* detect if invoked as player */
2124 i = strlen(argv[0]);
2125 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2133 while (optindex < argc) {
2134 opt = argv[optindex++];
2136 if (opt[0] == '-' && opt[1] != '\0') {
2138 while (po->name != NULL) {
2139 if (!strcmp(opt + 1, po->name))
2144 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2148 if (po->flags & HAS_ARG)
2149 arg = argv[optindex++];
2150 if (po->flags & OPT_STRING) {
2153 *po->u.str_arg = str;
2154 } else if (po->flags & OPT_BOOL) {
2157 po->u.func_arg(arg);
2161 opt_output_file(opt);
2163 opt_input_file(opt);
2170 /* file converter / grab */
2171 if (nb_output_files <= 0) {
2172 fprintf(stderr, "Must supply at least one output file\n");
2176 if (nb_input_files == 0) {
2181 if (nb_input_files <= 0) {
2182 fprintf(stderr, "Must supply at least one input file\n");
2189 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2190 stream_maps, nb_stream_maps);
2191 ti = getutime() - ti;
2193 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2197 for(i=0;i<nb_output_files;i++) {
2198 if (!(output_files[i]->oformat->flags & AVFMT_NOFILE))
2199 url_fclose(&output_files[i]->pb);
2201 for(i=0;i<nb_input_files;i++)
2202 av_close_input_file(input_files[i]);