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
21 #include "framehook.h"
26 #include <sys/ioctl.h>
29 #include <sys/resource.h>
32 #include <sys/types.h>
33 #include <sys/select.h>
39 #if !defined(INFINITY) && defined(HUGE_VAL)
40 #define INFINITY HUGE_VAL
43 #define MAXINT64 INT64_C(0x7fffffffffffffff)
48 #define HAS_ARG 0x0001
49 #define OPT_BOOL 0x0002
50 #define OPT_EXPERT 0x0004
51 #define OPT_STRING 0x0008
61 /* select an input stream for an output stream */
62 typedef struct AVStreamMap {
67 extern const OptionDef options[];
73 static AVFormatContext *input_files[MAX_FILES];
74 static int nb_input_files = 0;
76 static AVFormatContext *output_files[MAX_FILES];
77 static int nb_output_files = 0;
79 static AVStreamMap stream_maps[MAX_FILES];
80 static int nb_stream_maps;
82 static AVInputFormat *file_iformat;
83 static AVOutputFormat *file_oformat;
84 static AVImageFormat *image_format;
85 static int frame_width = 160;
86 static int frame_height = 128;
87 static int frame_topBand = 0;
88 static int frame_bottomBand = 0;
89 static int frame_leftBand = 0;
90 static int frame_rightBand = 0;
91 static int frame_rate = 25 * FRAME_RATE_BASE;
92 extern int emulate_frame_rate;
93 static int video_bit_rate = 200*1000;
94 static int video_bit_rate_tolerance = 4000*1000;
95 static int video_qscale = 0;
96 static int video_qmin = 2;
97 static int video_qmax = 31;
98 static int video_mb_qmin = 2;
99 static int video_mb_qmax = 31;
100 static int video_qdiff = 3;
101 static float video_qblur = 0.5;
102 static float video_qcomp = 0.5;
103 #if 0 //experimental, (can be removed)
104 static float video_rc_qsquish=1.0;
105 static float video_rc_qmod_amp=0;
106 static int video_rc_qmod_freq=0;
108 static char *video_rc_override_string=NULL;
109 static char *video_rc_eq="tex^qComp";
110 static int video_rc_buffer_size=0;
111 static float video_rc_buffer_aggressivity=1.0;
112 static int video_rc_max_rate=0;
113 static int video_rc_min_rate=0;
114 static float video_rc_initial_cplx=0;
115 static float video_b_qfactor = 1.25;
116 static float video_b_qoffset = 1.25;
117 static float video_i_qfactor = -0.8;
118 static float video_i_qoffset = 0.0;
119 static int me_method = ME_EPZS;
120 static int video_disable = 0;
121 static int video_codec_id = CODEC_ID_NONE;
122 static int same_quality = 0;
123 static int b_frames = 0;
124 static int use_hq = 0;
125 static int use_4mv = 0;
126 static int do_deinterlace = 0;
127 static int workaround_bugs = FF_BUG_AUTODETECT;
128 static int error_resilience = 2;
129 static int error_concealment = 3;
130 static int dct_algo = 0;
131 static int idct_algo = 0;
132 static int use_part = 0;
133 static int packet_size = 0;
134 static int strict = 0;
135 static int debug = 0;
137 static int gop_size = 12;
138 static int intra_only = 0;
139 static int audio_sample_rate = 44100;
140 static int audio_bit_rate = 64000;
141 static int audio_disable = 0;
142 static int audio_channels = 1;
143 static int audio_codec_id = CODEC_ID_NONE;
145 static INT64 recording_time = 0;
146 static int file_overwrite = 0;
147 static char *str_title = NULL;
148 static char *str_author = NULL;
149 static char *str_copyright = NULL;
150 static char *str_comment = NULL;
151 static int do_benchmark = 0;
152 static int do_hex_dump = 0;
153 static int do_play = 0;
154 static int do_psnr = 0;
155 static int do_vstats = 0;
156 static int do_pass = 0;
157 static char *pass_logfilename = NULL;
158 static int audio_stream_copy = 0;
159 static int video_stream_copy = 0;
161 static char *video_grab_format = "video4linux";
162 static char *video_device = NULL;
163 static int video_channel = 0;
165 static char *audio_grab_format = "audio_device";
166 static char *audio_device = NULL;
168 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
170 typedef struct AVOutputStream {
171 int file_index; /* file index */
172 int index; /* stream index in the output file */
173 int source_index; /* AVInputStream index */
174 AVStream *st; /* stream in the output file */
175 int encoding_needed; /* true if encoding needed for this stream */
177 /* input pts and corresponding output pts
180 double sync_ipts_offset;
183 int video_resample; /* video_resample and video_crop are mutually exclusive */
184 AVPicture pict_tmp; /* temporary image for resampling */
185 ImgReSampleContext *img_resample_ctx; /* for image resampling */
187 int video_crop; /* video_resample and video_crop are mutually exclusive */
188 int topBand; /* cropping area sizes */
193 ReSampleContext *resample; /* for audio resampling */
194 FifoBuffer fifo; /* for compression: one audio fifo per codec */
198 typedef struct AVInputStream {
202 int discard; /* true if stream data should be discarded */
203 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
204 INT64 sample_index; /* current sample */
205 int frame_decoded; /* true if a video or audio frame has been decoded */
208 typedef struct AVInputFile {
209 int eof_reached; /* true if eof reached */
210 int ist_index; /* index of first stream in ist_table */
211 int buffer_size; /* current total buffer size */
212 int buffer_size_max; /* buffer size at which we consider we can stop
214 int nb_streams; /* nb streams we are aware of */
219 /* init terminal so that we can grab keys */
220 static struct termios oldtty;
222 static void term_exit(void)
224 tcsetattr (0, TCSANOW, &oldtty);
227 static void term_init(void)
234 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
235 |INLCR|IGNCR|ICRNL|IXON);
236 tty.c_oflag |= OPOST;
237 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
238 tty.c_cflag &= ~(CSIZE|PARENB);
243 tcsetattr (0, TCSANOW, &tty);
246 #ifdef CONFIG_BEOS_NETSERVER
247 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
251 /* read a key without blocking */
252 static int read_key(void)
256 #ifndef CONFIG_BEOS_NETSERVER
264 n = select(1, &rfds, NULL, NULL, &tv);
278 /* no interactive support */
279 static void term_exit(void)
283 static void term_init(void)
287 static int read_key(void)
294 int read_ffserver_streams(AVFormatContext *s, const char *filename)
299 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
302 /* copy stream format */
303 s->nb_streams = ic->nb_streams;
304 for(i=0;i<ic->nb_streams;i++) {
307 st = av_mallocz(sizeof(AVFormatContext));
308 memcpy(st, ic->streams[i], sizeof(AVStream));
312 av_close_input_file(ic);
316 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
318 static void do_audio_out(AVFormatContext *s,
321 unsigned char *buf, int size)
324 UINT8 audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
325 UINT8 audio_out[4*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it - yep really WMA */
326 int size_out, frame_bytes, ret;
329 enc = &ost->st->codec;
331 if (ost->audio_resample) {
333 size_out = audio_resample(ost->resample,
334 (short *)buftmp, (short *)buf,
335 size / (ist->st->codec.channels * 2));
336 size_out = size_out * enc->channels * 2;
342 /* now encode as many frames as possible */
343 if (enc->frame_size > 1) {
344 /* output resampled raw samples */
345 fifo_write(&ost->fifo, buftmp, size_out,
348 frame_bytes = enc->frame_size * 2 * enc->channels;
350 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
351 &ost->fifo.rptr) == 0) {
352 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
354 av_write_frame(s, ost->index, audio_out, ret);
357 /* output a pcm frame */
358 /* XXX: change encoding codec API to avoid this ? */
359 switch(enc->codec->id) {
360 case CODEC_ID_PCM_S16LE:
361 case CODEC_ID_PCM_S16BE:
362 case CODEC_ID_PCM_U16LE:
363 case CODEC_ID_PCM_U16BE:
366 size_out = size_out >> 1;
369 ret = avcodec_encode_audio(enc, audio_out, size_out,
371 av_write_frame(s, ost->index, audio_out, ret);
375 /* write a picture to a raw mux */
376 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
377 int pix_fmt, int w, int h)
379 UINT8 *buf, *src, *dest;
382 /* XXX: not efficient, should add test if we can take
383 directly the AVPicture */
385 case PIX_FMT_YUV420P:
386 size = avpicture_get_size(pix_fmt, w, h);
387 buf = av_malloc(size);
396 src = picture->data[i];
398 memcpy(dest, src, w);
400 src += picture->linesize[i];
404 case PIX_FMT_YUV422P:
406 buf = av_malloc(size);
414 src = picture->data[i];
416 memcpy(dest, src, w);
418 src += picture->linesize[i];
422 case PIX_FMT_YUV444P:
424 buf = av_malloc(size);
429 src = picture->data[i];
431 memcpy(dest, src, w);
433 src += picture->linesize[i];
439 buf = av_malloc(size);
443 src = picture->data[0];
445 memcpy(dest, src, w * 2);
447 src += picture->linesize[0];
453 buf = av_malloc(size);
457 src = picture->data[0];
459 memcpy(dest, src, w * 3);
461 src += picture->linesize[0];
467 av_write_frame(s, index, buf, size);
471 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
475 AVPicture picture_tmp;
478 dec = &ist->st->codec;
480 /* deinterlace : must be done before any resize */
481 if (do_deinterlace) {
484 /* create temporary picture */
485 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
486 buf = av_malloc(size);
490 picture2 = &picture_tmp;
491 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
493 if (avpicture_deinterlace(picture2, picture,
494 dec->pix_fmt, dec->width, dec->height) < 0) {
495 /* if error, do not deinterlace */
504 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
506 if (picture != picture2)
507 *picture = *picture2;
511 /* we begin to correct av delay at this threshold */
512 #define AV_DELAY_MAX 0.100
514 static void do_video_out(AVFormatContext *s,
517 AVPicture *in_picture,
518 int *frame_size, AVOutputStream *audio_sync)
520 int nb_frames, i, ret;
521 AVPicture *final_picture, *formatted_picture;
522 AVPicture picture_format_temp, picture_crop_temp;
523 static UINT8 *video_buffer;
524 UINT8 *buf = NULL, *buf1 = NULL;
525 AVCodecContext *enc, *dec;
527 #define VIDEO_BUFFER_SIZE (1024*1024)
529 enc = &ost->st->codec;
530 dec = &ist->st->codec;
532 /* by default, we output a single frame */
537 /* NOTE: the A/V sync is always done by considering the audio is
538 the master clock. It is suffisant for transcoding or playing,
539 but not for the general case */
541 /* compute the A-V delay and duplicate/remove frames if needed */
542 double adelta, vdelta, apts, vpts, av_delay;
544 if (audio_sync->sync_ipts != AV_NOPTS_VALUE &&
545 ost->sync_ipts != AV_NOPTS_VALUE) {
547 adelta = (double)(ost->st->pts.val - audio_sync->sync_opts) *
548 s->pts_num / s->pts_den;
549 apts = audio_sync->sync_ipts + adelta;
551 vdelta = (double)(ost->st->pts.val - ost->sync_opts) *
552 s->pts_num / s->pts_den;
553 vpts = ost->sync_ipts + vdelta;
555 av_delay = apts - vpts;
556 // printf("delay=%f\n", av_delay);
557 if (av_delay < -AV_DELAY_MAX)
559 else if (av_delay > AV_DELAY_MAX)
565 if (ost->sync_ipts != AV_NOPTS_VALUE) {
566 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
567 if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
568 if (vdelta < -AV_DELAY_MAX)
570 else if (vdelta > AV_DELAY_MAX)
573 ost->sync_ipts_offset -= vdelta;
574 if (!ost->sync_ipts_offset)
575 ost->sync_ipts_offset = 0.000001; /* one microsecond */
580 static char *action[] = { "drop frame", "copy frame", "dup frame" };
581 printf("Input PTS %12.6f, output PTS %12.6f: %s\n",
582 (double) ost->sync_ipts, (double) ost->st->pts.val * s->pts_num / s->pts_den,
592 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
596 /* convert pixel format if needed */
597 if (enc->pix_fmt != dec->pix_fmt) {
600 /* create temporary picture */
601 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
602 buf = av_malloc(size);
605 formatted_picture = &picture_format_temp;
606 avpicture_fill(formatted_picture, buf, enc->pix_fmt, dec->width, dec->height);
608 if (img_convert(formatted_picture, enc->pix_fmt,
609 in_picture, dec->pix_fmt,
610 dec->width, dec->height) < 0) {
611 fprintf(stderr, "pixel format conversion not handled\n");
615 formatted_picture = in_picture;
618 /* XXX: resampling could be done before raw format convertion in
619 some cases to go faster */
620 /* XXX: only works for YUV420P */
621 if (ost->video_resample) {
622 final_picture = &ost->pict_tmp;
623 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
624 } else if (ost->video_crop) {
625 picture_crop_temp.data[0] = formatted_picture->data[0] +
626 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
628 picture_crop_temp.data[1] = formatted_picture->data[1] +
629 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
630 (ost->leftBand >> 1);
632 picture_crop_temp.data[2] = formatted_picture->data[2] +
633 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
634 (ost->leftBand >> 1);
636 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
637 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
638 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
639 final_picture = &picture_crop_temp;
641 final_picture = formatted_picture;
643 /* duplicates frame if needed */
644 /* XXX: pb because no interleaving */
645 for(i=0;i<nb_frames;i++) {
646 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
649 memset(&big_picture, 0, sizeof(AVFrame));
650 *(AVPicture*)&big_picture= *final_picture;
652 /* handles sameq here. This is not correct because it may
653 not be a global option */
655 big_picture.quality = ist->st->quality;
657 big_picture.quality = ost->st->quality;
659 ret = avcodec_encode_video(enc,
660 video_buffer, VIDEO_BUFFER_SIZE,
662 //enc->frame_number = enc->real_pict_num;
663 av_write_frame(s, ost->index, video_buffer, ret);
665 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
666 // enc->frame_number-1, enc->real_pict_num, ret,
668 /* if two pass, output log */
669 if (ost->logfile && enc->stats_out) {
670 fprintf(ost->logfile, "%s", enc->stats_out);
673 if (s->oformat->flags & AVFMT_RAWPICTURE) {
674 /* raw pictures are written as AVPicture structure to
675 avoid any copies. We support temorarily the older
677 av_write_frame(s, ost->index,
678 (UINT8 *)final_picture, sizeof(AVPicture));
680 write_picture(s, ost->index, final_picture, enc->pix_fmt,
681 enc->width, enc->height);
691 static double psnr(double d){
692 if(d==0) return INFINITY;
693 return -10.0*log(d)/log(10);
696 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
699 static FILE *fvstats=NULL;
700 static INT64 total_size = 0;
707 double ti1, bitrate, avg_bitrate;
711 today = localtime(&today2);
712 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
715 fvstats = fopen(filename,"w");
723 enc = &ost->st->codec;
724 total_size += frame_size;
725 if (enc->codec_type == CODEC_TYPE_VIDEO) {
726 frame_number = ost->frame_number;
727 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality);
728 if (enc->flags&CODEC_FLAG_PSNR)
729 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
731 fprintf(fvstats,"f_size= %6d ", frame_size);
732 /* compute pts value */
733 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
737 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
738 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
739 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
740 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
741 fprintf(fvstats,"type= %s\n", enc->coded_frame->key_frame == 1 ? "I" : "P");
745 void print_report(AVFormatContext **output_files,
746 AVOutputStream **ost_table, int nb_ostreams,
751 AVFormatContext *oc, *os;
754 int frame_number, vid, i;
755 double bitrate, ti1, pts;
756 static INT64 last_time = -1;
758 if (!is_last_report) {
760 /* display the report every 0.5 seconds */
761 cur_time = av_gettime();
762 if (last_time == -1) {
763 last_time = cur_time;
766 if ((cur_time - last_time) < 500000)
768 last_time = cur_time;
772 oc = output_files[0];
774 total_size = url_ftell(&oc->pb);
779 for(i=0;i<nb_ostreams;i++) {
781 os = output_files[ost->file_index];
782 enc = &ost->st->codec;
783 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
784 sprintf(buf + strlen(buf), "q=%2.1f ",
785 enc->coded_frame->quality);
787 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
788 frame_number = ost->frame_number;
789 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
790 frame_number, enc->coded_frame ? enc->coded_frame->quality : 0);
791 if (enc->flags&CODEC_FLAG_PSNR)
792 sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
795 /* compute min output value */
796 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
797 if ((pts < ti1) && (pts > 0))
802 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
804 sprintf(buf + strlen(buf),
805 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
806 (double)total_size / 1024, ti1, bitrate);
808 fprintf(stderr, "%s ", buf);
810 if (is_last_report) {
811 fprintf(stderr, "\n");
813 fprintf(stderr, "\r");
819 * The following code is the main loop of the file converter
821 static int av_encode(AVFormatContext **output_files,
823 AVFormatContext **input_files,
825 AVStreamMap *stream_maps, int nb_stream_maps)
827 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0, pts_set;
828 AVFormatContext *is, *os;
829 AVCodecContext *codec, *icodec;
830 AVOutputStream *ost, **ost_table = NULL;
831 AVInputStream *ist, **ist_table = NULL;
832 AVInputFile *file_table;
833 AVFormatContext *stream_no_data;
836 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
840 /* input stream init */
842 for(i=0;i<nb_input_files;i++) {
844 file_table[i].ist_index = j;
845 file_table[i].nb_streams = is->nb_streams;
850 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
854 for(i=0;i<nb_istreams;i++) {
855 ist = av_mallocz(sizeof(AVInputStream));
861 for(i=0;i<nb_input_files;i++) {
863 for(k=0;k<is->nb_streams;k++) {
864 ist = ist_table[j++];
865 ist->st = is->streams[k];
868 ist->discard = 1; /* the stream is discarded by default
873 /* output stream init */
875 for(i=0;i<nb_output_files;i++) {
876 os = output_files[i];
877 nb_ostreams += os->nb_streams;
879 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
880 fprintf(stderr, "Number of stream maps must match number of output streams\n");
884 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
887 for(i=0;i<nb_ostreams;i++) {
888 ost = av_mallocz(sizeof(AVOutputStream));
895 for(k=0;k<nb_output_files;k++) {
896 os = output_files[k];
897 for(i=0;i<os->nb_streams;i++) {
899 ost = ost_table[n++];
902 ost->st = os->streams[i];
903 if (nb_stream_maps > 0) {
904 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
905 stream_maps[n-1].stream_index;
907 /* get corresponding input stream index : we select the first one with the right type */
909 for(j=0;j<nb_istreams;j++) {
912 ist->st->codec.codec_type == ost->st->codec.codec_type) {
913 ost->source_index = j;
919 /* try again and reuse existing stream */
920 for(j=0;j<nb_istreams;j++) {
922 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
923 ost->source_index = j;
928 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
929 ost->file_index, ost->index);
934 ist = ist_table[ost->source_index];
939 /* for each output stream, we compute the right encoding parameters */
940 for(i=0;i<nb_ostreams;i++) {
942 ist = ist_table[ost->source_index];
944 codec = &ost->st->codec;
945 icodec = &ist->st->codec;
947 if (ost->st->stream_copy) {
948 /* if stream_copy is selected, no need to decode or encode */
949 codec->codec_id = icodec->codec_id;
950 codec->codec_type = icodec->codec_type;
951 codec->codec_tag = icodec->codec_tag;
952 codec->bit_rate = icodec->bit_rate;
953 switch(codec->codec_type) {
954 case CODEC_TYPE_AUDIO:
955 codec->sample_rate = icodec->sample_rate;
956 codec->channels = icodec->channels;
958 case CODEC_TYPE_VIDEO:
959 codec->frame_rate = icodec->frame_rate;
960 codec->width = icodec->width;
961 codec->height = icodec->height;
967 switch(codec->codec_type) {
968 case CODEC_TYPE_AUDIO:
969 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
972 if (codec->channels == icodec->channels &&
973 codec->sample_rate == icodec->sample_rate) {
974 ost->audio_resample = 0;
976 if (codec->channels != icodec->channels &&
977 icodec->codec_id == CODEC_ID_AC3) {
978 /* Special case for 5:1 AC3 input */
979 /* and mono or stereo output */
980 /* Request specific number of channels */
981 icodec->channels = codec->channels;
982 if (codec->sample_rate == icodec->sample_rate)
983 ost->audio_resample = 0;
985 ost->audio_resample = 1;
986 ost->resample = audio_resample_init(codec->channels, icodec->channels,
988 icodec->sample_rate);
990 /* Request specific number of channels */
991 icodec->channels = codec->channels;
993 ost->audio_resample = 1;
994 ost->resample = audio_resample_init(codec->channels, icodec->channels,
996 icodec->sample_rate);
999 ist->decoding_needed = 1;
1000 ost->encoding_needed = 1;
1002 case CODEC_TYPE_VIDEO:
1003 if (codec->width == icodec->width &&
1004 codec->height == icodec->height &&
1005 frame_topBand == 0 &&
1006 frame_bottomBand == 0 &&
1007 frame_leftBand == 0 &&
1008 frame_rightBand == 0)
1010 ost->video_resample = 0;
1011 ost->video_crop = 0;
1012 } else if ((codec->width == icodec->width -
1013 (frame_leftBand + frame_rightBand)) &&
1014 (codec->height == icodec->height -
1015 (frame_topBand + frame_bottomBand)))
1017 ost->video_resample = 0;
1018 ost->video_crop = 1;
1019 ost->topBand = frame_topBand;
1020 ost->leftBand = frame_leftBand;
1023 ost->video_resample = 1;
1024 ost->video_crop = 0; // cropping is handled as part of resample
1025 buf = av_malloc((codec->width * codec->height * 3) / 2);
1028 ost->pict_tmp.data[0] = buf;
1029 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
1030 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
1031 ost->pict_tmp.linesize[0] = codec->width;
1032 ost->pict_tmp.linesize[1] = codec->width / 2;
1033 ost->pict_tmp.linesize[2] = codec->width / 2;
1035 ost->img_resample_ctx = img_resample_full_init(
1036 ost->st->codec.width, ost->st->codec.height,
1037 ist->st->codec.width, ist->st->codec.height,
1038 frame_topBand, frame_bottomBand,
1039 frame_leftBand, frame_rightBand);
1041 ost->encoding_needed = 1;
1042 ist->decoding_needed = 1;
1048 if (ost->encoding_needed &&
1049 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1050 char logfilename[1024];
1055 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1057 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1058 if (codec->flags & CODEC_FLAG_PASS1) {
1059 f = fopen(logfilename, "w");
1061 perror(logfilename);
1066 /* read the log file */
1067 f = fopen(logfilename, "r");
1069 perror(logfilename);
1072 fseek(f, 0, SEEK_END);
1074 fseek(f, 0, SEEK_SET);
1075 logbuffer = av_malloc(size + 1);
1077 fprintf(stderr, "Could not allocate log buffer\n");
1080 fread(logbuffer, 1, size, f);
1082 logbuffer[size] = '\0';
1083 codec->stats_in = logbuffer;
1089 /* dump the file output parameters - cannot be done before in case
1091 for(i=0;i<nb_output_files;i++) {
1092 dump_format(output_files[i], i, output_files[i]->filename, 1);
1095 /* dump the stream mapping */
1096 fprintf(stderr, "Stream mapping:\n");
1097 for(i=0;i<nb_ostreams;i++) {
1099 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1100 ist_table[ost->source_index]->file_index,
1101 ist_table[ost->source_index]->index,
1106 /* open each encoder */
1107 for(i=0;i<nb_ostreams;i++) {
1109 if (ost->encoding_needed) {
1111 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1113 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1114 ost->file_index, ost->index);
1117 if (avcodec_open(&ost->st->codec, codec) < 0) {
1118 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1119 ost->file_index, ost->index);
1125 /* open each decoder */
1126 for(i=0;i<nb_istreams;i++) {
1128 if (ist->decoding_needed) {
1130 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1132 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1133 ist->st->codec.codec_id, ist->file_index, ist->index);
1136 if (avcodec_open(&ist->st->codec, codec) < 0) {
1137 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1138 ist->file_index, ist->index);
1141 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1142 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1143 ist->frame_decoded = 1;
1148 for(i=0;i<nb_istreams;i++) {
1152 /* compute buffer size max (should use a complete heuristic) */
1153 for(i=0;i<nb_input_files;i++) {
1154 file_table[i].buffer_size_max = 2048;
1157 /* open files and write file headers */
1158 for(i=0;i<nb_output_files;i++) {
1159 os = output_files[i];
1160 if (av_write_header(os) < 0) {
1161 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1167 #ifndef CONFIG_WIN32
1169 fprintf(stderr, "Press [q] to stop encoding\n");
1171 fprintf(stderr, "Press [q] to stop playing\n");
1180 int file_index, ist_index;
1185 int data_size, got_picture;
1187 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1188 void *buffer_to_free;
1192 /* if 'q' pressed, exits */
1194 /* read_key() returns 0 on EOF */
1200 /* select the stream that we must read now by looking at the
1201 smallest output pts */
1204 for(i=0;i<nb_ostreams;i++) {
1207 os = output_files[ost->file_index];
1208 ist = ist_table[ost->source_index];
1209 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1210 if (!file_table[ist->file_index].eof_reached &&
1213 file_index = ist->file_index;
1216 /* if none, if is finished */
1217 if (file_index < 0) {
1221 /* finish if recording time exhausted */
1222 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1225 /* read a packet from it and output it in the fifo */
1226 is = input_files[file_index];
1227 if (av_read_packet(is, &pkt) < 0) {
1228 file_table[file_index].eof_reached = 1;
1232 stream_no_data = is;
1237 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1238 av_hex_dump(pkt.data, pkt.size);
1240 /* the following test is needed in case new streams appear
1241 dynamically in stream : we ignore them */
1242 if (pkt.stream_index >= file_table[file_index].nb_streams)
1243 goto discard_packet;
1244 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1245 ist = ist_table[ist_index];
1247 goto discard_packet;
1249 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1257 ipts = AV_NOPTS_VALUE;
1259 /* decode the packet if needed */
1260 data_buf = NULL; /* fail safe */
1262 if (ist->decoding_needed) {
1263 /* NOTE1: we only take into account the PTS if a new
1264 frame has begun (MPEG semantics) */
1265 /* NOTE2: even if the fraction is not initialized,
1266 av_frac_set can be used to set the integer part */
1267 if (ist->frame_decoded &&
1268 pkt.pts != AV_NOPTS_VALUE &&
1271 ist->frame_decoded = 0;
1275 switch(ist->st->codec.codec_type) {
1276 case CODEC_TYPE_AUDIO:
1277 /* XXX: could avoid copy if PCM 16 bits with same
1278 endianness as CPU */
1279 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1283 /* Some bug in mpeg audio decoder gives */
1284 /* data_size < 0, it seems they are overflows */
1285 if (data_size <= 0) {
1286 /* no audio frame */
1291 data_buf = (UINT8 *)samples;
1293 case CODEC_TYPE_VIDEO:
1294 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1297 size = (ist->st->codec.width * ist->st->codec.height);
1298 avpicture_fill(&picture, ptr,
1299 ist->st->codec.pix_fmt,
1300 ist->st->codec.width,
1301 ist->st->codec.height);
1304 AVFrame big_picture;
1306 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1307 ret = avcodec_decode_video(&ist->st->codec,
1308 &big_picture, &got_picture, ptr, len);
1309 picture= *(AVPicture*)&big_picture;
1310 ist->st->quality= big_picture.quality;
1313 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1314 ist->file_index, ist->index);
1315 av_free_packet(&pkt);
1319 /* no picture yet */
1339 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1340 pre_process_video_frame(ist, &picture, &buffer_to_free);
1343 ist->frame_decoded = 1;
1346 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1347 is the one of the next displayed one */
1348 /* XXX: add mpeg4 too ? */
1349 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1350 if (ist->st->codec.pict_type != B_TYPE) {
1352 tmp = ist->last_ip_pts;
1353 ist->last_ip_pts = ist->frac_pts.val;
1354 ist->frac_pts.val = tmp;
1358 /* transcode raw format, encode packets and output them */
1360 for(i=0;i<nb_ostreams;i++) {
1364 if (ost->source_index == ist_index) {
1365 os = output_files[ost->file_index];
1367 if (ipts != AV_NOPTS_VALUE) {
1369 printf("%d: got pts=%f %f\n",
1370 i, pkt.pts / 90000.0,
1371 (ipts - ost->st->pts.val) / 90000.0);
1373 /* set the input output pts pairs */
1374 ost->sync_ipts = (double)ipts * is->pts_num /
1376 /* XXX: take into account the various fifos,
1377 in particular for audio */
1378 ost->sync_opts = ost->st->pts.val;
1379 //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt.pts=%lld\n", ipts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt.pts);
1381 //printf("pts.val=%lld\n", ost->st->pts.val);
1382 ost->sync_ipts = AV_NOPTS_VALUE;
1385 if (ost->encoding_needed) {
1386 switch(ost->st->codec.codec_type) {
1387 case CODEC_TYPE_AUDIO:
1388 do_audio_out(os, ost, ist, data_buf, data_size);
1390 case CODEC_TYPE_VIDEO:
1391 /* find an audio stream for synchro */
1394 AVOutputStream *audio_sync, *ost1;
1396 for(i=0;i<nb_ostreams;i++) {
1397 ost1 = ost_table[i];
1398 if (ost1->file_index == ost->file_index &&
1399 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1405 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1406 if (do_vstats && frame_size)
1407 do_video_stats(os, ost, frame_size);
1416 /* no reencoding needed : output the packet directly */
1417 /* force the input stream PTS */
1419 //XXX/FIXME set keyframe flag from demuxer (or optionally from decoder)
1420 memset(&avframe, 0, sizeof(AVFrame));
1421 ost->st->codec.coded_frame= &avframe;
1423 av_write_frame(os, ost->index, data_buf, data_size);
1424 ost->st->codec.frame_number++;
1425 ost->frame_number++;
1429 av_free(buffer_to_free);
1430 ipts = AV_NOPTS_VALUE;
1433 av_free_packet(&pkt);
1435 /* dump report by using the output first video and audio streams */
1436 print_report(output_files, ost_table, nb_ostreams, 0);
1440 /* dump report by using the first video and audio streams */
1441 print_report(output_files, ost_table, nb_ostreams, 1);
1443 /* close each encoder */
1444 for(i=0;i<nb_ostreams;i++) {
1446 if (ost->encoding_needed) {
1447 av_freep(&ost->st->codec.stats_in);
1448 avcodec_close(&ost->st->codec);
1452 /* close each decoder */
1453 for(i=0;i<nb_istreams;i++) {
1455 if (ist->decoding_needed) {
1456 avcodec_close(&ist->st->codec);
1461 /* write the trailer if needed and close file */
1462 for(i=0;i<nb_output_files;i++) {
1463 os = output_files[i];
1464 av_write_trailer(os);
1470 av_free(file_table);
1473 for(i=0;i<nb_istreams;i++) {
1480 for(i=0;i<nb_ostreams;i++) {
1484 fclose(ost->logfile);
1485 ost->logfile = NULL;
1487 fifo_free(&ost->fifo); /* works even if fifo is not
1488 initialized but set to zero */
1489 av_free(ost->pict_tmp.data[0]);
1490 if (ost->video_resample)
1491 img_resample_close(ost->img_resample_ctx);
1492 if (ost->audio_resample)
1493 audio_resample_close(ost->resample);
1506 int file_read(const char *filename)
1509 unsigned char buffer[1024];
1512 if (url_open(&h, filename, O_RDONLY) < 0) {
1513 printf("could not open '%s'\n", filename);
1517 len = url_read(h, buffer, sizeof(buffer));
1520 for(i=0;i<len;i++) putchar(buffer[i]);
1527 void show_licence(void)
1530 "ffmpeg version " FFMPEG_VERSION "\n"
1531 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1532 "This library is free software; you can redistribute it and/or\n"
1533 "modify it under the terms of the GNU Lesser General Public\n"
1534 "License as published by the Free Software Foundation; either\n"
1535 "version 2 of the License, or (at your option) any later version.\n"
1537 "This library is distributed in the hope that it will be useful,\n"
1538 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1539 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1540 "Lesser General Public License for more details.\n"
1542 "You should have received a copy of the GNU Lesser General Public\n"
1543 "License along with this library; if not, write to the Free Software\n"
1544 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1549 void opt_image_format(const char *arg)
1553 for(f = first_image_format; f != NULL; f = f->next) {
1554 if (!strcmp(arg, f->name))
1558 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1564 void opt_format(const char *arg)
1566 /* compatibility stuff for pgmyuv */
1567 if (!strcmp(arg, "pgmyuv")) {
1568 opt_image_format(arg);
1572 file_iformat = av_find_input_format(arg);
1573 file_oformat = guess_format(arg, NULL, NULL);
1574 if (!file_iformat && !file_oformat) {
1575 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1580 void opt_video_bitrate(const char *arg)
1582 video_bit_rate = atoi(arg) * 1000;
1585 void opt_video_bitrate_tolerance(const char *arg)
1587 video_bit_rate_tolerance = atoi(arg) * 1000;
1590 void opt_video_bitrate_max(const char *arg)
1592 video_rc_max_rate = atoi(arg) * 1000;
1595 void opt_video_bitrate_min(const char *arg)
1597 video_rc_min_rate = atoi(arg) * 1000;
1600 void opt_video_buffer_size(const char *arg)
1602 video_rc_buffer_size = atoi(arg) * 1000;
1605 void opt_video_rc_eq(char *arg)
1610 void opt_video_rc_override_string(char *arg)
1612 video_rc_override_string = arg;
1616 void opt_workaround_bugs(const char *arg)
1618 workaround_bugs = atoi(arg);
1621 void opt_dct_algo(const char *arg)
1623 dct_algo = atoi(arg);
1626 void opt_idct_algo(const char *arg)
1628 idct_algo = atoi(arg);
1632 void opt_error_resilience(const char *arg)
1634 error_resilience = atoi(arg);
1637 void opt_error_concealment(const char *arg)
1639 error_concealment = atoi(arg);
1642 void opt_debug(const char *arg)
1647 void opt_frame_rate(const char *arg)
1649 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1653 void opt_frame_crop_top(const char *arg)
1655 frame_topBand = atoi(arg);
1656 if (frame_topBand < 0) {
1657 fprintf(stderr, "Incorrect top crop size\n");
1660 if ((frame_topBand % 2) != 0) {
1661 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1664 if ((frame_topBand) >= frame_height){
1665 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1668 frame_height -= frame_topBand;
1671 void opt_frame_crop_bottom(const char *arg)
1673 frame_bottomBand = atoi(arg);
1674 if (frame_bottomBand < 0) {
1675 fprintf(stderr, "Incorrect bottom crop size\n");
1678 if ((frame_bottomBand % 2) != 0) {
1679 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1682 if ((frame_bottomBand) >= frame_height){
1683 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1686 frame_height -= frame_bottomBand;
1689 void opt_frame_crop_left(const char *arg)
1691 frame_leftBand = atoi(arg);
1692 if (frame_leftBand < 0) {
1693 fprintf(stderr, "Incorrect left crop size\n");
1696 if ((frame_leftBand % 2) != 0) {
1697 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1700 if ((frame_leftBand) >= frame_width){
1701 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1704 frame_width -= frame_leftBand;
1707 void opt_frame_crop_right(const char *arg)
1709 frame_rightBand = atoi(arg);
1710 if (frame_rightBand < 0) {
1711 fprintf(stderr, "Incorrect right crop size\n");
1714 if ((frame_rightBand % 2) != 0) {
1715 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1718 if ((frame_rightBand) >= frame_width){
1719 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1722 frame_width -= frame_rightBand;
1725 void opt_frame_size(const char *arg)
1727 parse_image_size(&frame_width, &frame_height, arg);
1728 if (frame_width <= 0 || frame_height <= 0) {
1729 fprintf(stderr, "Incorrect frame size\n");
1732 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1733 fprintf(stderr, "Frame size must be a multiple of 2\n");
1738 void opt_gop_size(const char *arg)
1740 gop_size = atoi(arg);
1743 void opt_b_frames(const char *arg)
1745 b_frames = atoi(arg);
1746 if (b_frames > FF_MAX_B_FRAMES) {
1747 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1749 } else if (b_frames < 1) {
1750 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1755 void opt_qscale(const char *arg)
1757 video_qscale = atoi(arg);
1758 if (video_qscale < 0 ||
1759 video_qscale > 31) {
1760 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1765 void opt_qmin(const char *arg)
1767 video_qmin = atoi(arg);
1768 if (video_qmin < 0 ||
1770 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1775 void opt_qmax(const char *arg)
1777 video_qmax = atoi(arg);
1778 if (video_qmax < 0 ||
1780 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1785 void opt_mb_qmin(const char *arg)
1787 video_mb_qmin = atoi(arg);
1788 if (video_mb_qmin < 0 ||
1789 video_mb_qmin > 31) {
1790 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1795 void opt_mb_qmax(const char *arg)
1797 video_mb_qmax = atoi(arg);
1798 if (video_mb_qmax < 0 ||
1799 video_mb_qmax > 31) {
1800 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1805 void opt_qdiff(const char *arg)
1807 video_qdiff = atoi(arg);
1808 if (video_qdiff < 0 ||
1810 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1815 void opt_qblur(const char *arg)
1817 video_qblur = atof(arg);
1820 void opt_qcomp(const char *arg)
1822 video_qcomp = atof(arg);
1825 void opt_rc_initial_cplx(const char *arg)
1827 video_rc_initial_cplx = atof(arg);
1829 void opt_b_qfactor(const char *arg)
1831 video_b_qfactor = atof(arg);
1833 void opt_i_qfactor(const char *arg)
1835 video_i_qfactor = atof(arg);
1837 void opt_b_qoffset(const char *arg)
1839 video_b_qoffset = atof(arg);
1841 void opt_i_qoffset(const char *arg)
1843 video_i_qoffset = atof(arg);
1846 void opt_packet_size(const char *arg)
1848 packet_size= atoi(arg);
1851 void opt_strict(const char *arg)
1856 void opt_audio_bitrate(const char *arg)
1858 audio_bit_rate = atoi(arg) * 1000;
1861 void opt_audio_rate(const char *arg)
1863 audio_sample_rate = atoi(arg);
1866 void opt_audio_channels(const char *arg)
1868 audio_channels = atoi(arg);
1871 void opt_video_device(const char *arg)
1873 video_device = av_strdup(arg);
1876 void opt_video_channel(const char *arg)
1878 video_channel = strtol(arg, NULL, 0);
1881 void opt_audio_device(const char *arg)
1883 audio_device = av_strdup(arg);
1886 void opt_dv1394(const char *arg)
1888 video_grab_format = "dv1394";
1889 audio_grab_format = NULL;
1892 void opt_audio_codec(const char *arg)
1896 if (!strcmp(arg, "copy")) {
1897 audio_stream_copy = 1;
1901 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1906 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1909 audio_codec_id = p->id;
1914 void add_frame_hooker(const char *arg)
1919 char *args = av_strdup(arg);
1921 argv[0] = strtok(args, " ");
1922 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
1925 i = frame_hook_add(argc, argv);
1928 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
1933 const char *motion_str[] = {
1943 void opt_motion_estimation(const char *arg)
1949 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1952 if (!strcmp(*p, arg))
1956 me_method = (p - motion_str) + 1;
1959 void opt_video_codec(const char *arg)
1963 if (!strcmp(arg, "copy")) {
1964 video_stream_copy = 1;
1968 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1973 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1976 video_codec_id = p->id;
1981 void opt_map(const char *arg)
1987 m = &stream_maps[nb_stream_maps++];
1989 m->file_index = strtol(arg, (char **)&p, 0);
1993 m->stream_index = strtol(p, (char **)&p, 0);
1996 void opt_recording_time(const char *arg)
1998 recording_time = parse_date(arg, 1);
2001 void print_error(const char *filename, int err)
2004 case AVERROR_NUMEXPECTED:
2005 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
2006 "Use '%%d' to specify the image number:\n"
2007 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
2008 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
2011 case AVERROR_INVALIDDATA:
2012 fprintf(stderr, "%s: Error while parsing header\n", filename);
2015 fprintf(stderr, "%s: Unknown format\n", filename);
2018 fprintf(stderr, "%s: Error while opening file\n", filename);
2023 void opt_input_file(const char *filename)
2025 AVFormatContext *ic;
2026 AVFormatParameters params, *ap = ¶ms;
2027 int err, i, ret, rfps;
2029 if (!strcmp(filename, "-"))
2032 /* get default parameters from command line */
2033 memset(ap, 0, sizeof(*ap));
2034 ap->sample_rate = audio_sample_rate;
2035 ap->channels = audio_channels;
2036 ap->frame_rate = frame_rate;
2037 ap->width = frame_width;
2038 ap->height = frame_height;
2039 ap->image_format = image_format;
2041 /* open the input file with generic libav function */
2042 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
2044 print_error(filename, err);
2048 /* If not enough info to get the stream parameters, we decode the
2049 first frames to get it. (used in mpeg case for example) */
2050 ret = av_find_stream_info(ic);
2052 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2056 /* update the current parameters so that they match the one of the input stream */
2057 for(i=0;i<ic->nb_streams;i++) {
2058 AVCodecContext *enc = &ic->streams[i]->codec;
2059 switch(enc->codec_type) {
2060 case CODEC_TYPE_AUDIO:
2061 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2062 audio_channels = enc->channels;
2063 audio_sample_rate = enc->sample_rate;
2065 case CODEC_TYPE_VIDEO:
2066 frame_height = enc->height;
2067 frame_width = enc->width;
2068 rfps = ic->streams[i]->r_frame_rate;
2069 enc->workaround_bugs = workaround_bugs;
2070 enc->error_resilience = error_resilience;
2071 enc->error_concealment = error_concealment;
2072 enc->idct_algo= idct_algo;
2074 /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
2075 enc->flags|= CODEC_FLAG_TRUNCATED; */
2076 if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO)
2077 enc->flags|= CODEC_FLAG_TRUNCATED;
2079 if (enc->frame_rate != rfps) {
2080 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2081 i, (float)enc->frame_rate / FRAME_RATE_BASE,
2082 (float)rfps / FRAME_RATE_BASE);
2084 /* update the current frame rate to match the stream frame rate */
2092 input_files[nb_input_files] = ic;
2093 /* dump the file content */
2094 dump_format(ic, nb_input_files, filename, 0);
2096 file_iformat = NULL;
2097 file_oformat = NULL;
2098 image_format = NULL;
2101 void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2103 int has_video, has_audio, i, j;
2104 AVFormatContext *ic;
2108 for(j=0;j<nb_input_files;j++) {
2109 ic = input_files[j];
2110 for(i=0;i<ic->nb_streams;i++) {
2111 AVCodecContext *enc = &ic->streams[i]->codec;
2112 switch(enc->codec_type) {
2113 case CODEC_TYPE_AUDIO:
2116 case CODEC_TYPE_VIDEO:
2124 *has_video_ptr = has_video;
2125 *has_audio_ptr = has_audio;
2128 void opt_output_file(const char *filename)
2131 AVFormatContext *oc;
2132 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2134 AVFormatParameters params, *ap = ¶ms;
2136 if (!strcmp(filename, "-"))
2139 oc = av_mallocz(sizeof(AVFormatContext));
2141 if (!file_oformat) {
2142 file_oformat = guess_format(NULL, filename, NULL);
2143 if (!file_oformat) {
2144 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2150 oc->oformat = file_oformat;
2152 if (!strcmp(file_oformat->name, "ffm") &&
2153 strstart(filename, "http:", NULL)) {
2154 /* special case for files sent to ffserver: we get the stream
2155 parameters from ffserver */
2156 if (read_ffserver_streams(oc, filename) < 0) {
2157 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2161 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2162 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2164 /* disable if no corresponding type found and at least one
2166 if (nb_input_files > 0) {
2167 check_audio_video_inputs(&input_has_video, &input_has_audio);
2168 if (!input_has_video)
2170 if (!input_has_audio)
2174 /* manual disable */
2175 if (audio_disable) {
2178 if (video_disable) {
2184 AVCodecContext *video_enc;
2186 st = av_mallocz(sizeof(AVStream));
2188 fprintf(stderr, "Could not alloc stream\n");
2191 avcodec_get_context_defaults(&st->codec);
2193 video_enc = &st->codec;
2194 if (video_stream_copy) {
2195 st->stream_copy = 1;
2196 video_enc->codec_type = CODEC_TYPE_VIDEO;
2201 codec_id = file_oformat->video_codec;
2202 if (video_codec_id != CODEC_ID_NONE)
2203 codec_id = video_codec_id;
2205 video_enc->codec_id = codec_id;
2207 video_enc->bit_rate = video_bit_rate;
2208 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2209 video_enc->frame_rate = frame_rate;
2211 video_enc->width = frame_width;
2212 video_enc->height = frame_height;
2215 video_enc->gop_size = gop_size;
2217 video_enc->gop_size = 0;
2218 if (video_qscale || same_quality) {
2219 video_enc->flags |= CODEC_FLAG_QSCALE;
2220 st->quality = video_qscale;
2224 video_enc->flags |= CODEC_FLAG_HQ;
2228 video_enc->flags |= CODEC_FLAG_HQ;
2229 video_enc->flags |= CODEC_FLAG_4MV;
2233 video_enc->flags |= CODEC_FLAG_PART;
2237 if (codec_id != CODEC_ID_MPEG4) {
2238 fprintf(stderr, "\nB frames encoding only supported by MPEG-4.\n");
2241 video_enc->max_b_frames = b_frames;
2242 video_enc->b_frame_strategy = 0;
2243 video_enc->b_quant_factor = 2.0;
2246 video_enc->qmin = video_qmin;
2247 video_enc->qmax = video_qmax;
2248 video_enc->mb_qmin = video_mb_qmin;
2249 video_enc->mb_qmax = video_mb_qmax;
2250 video_enc->max_qdiff = video_qdiff;
2251 video_enc->qblur = video_qblur;
2252 video_enc->qcompress = video_qcomp;
2253 video_enc->rc_eq = video_rc_eq;
2254 video_enc->debug= debug;
2256 p= video_rc_override_string;
2259 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2261 fprintf(stderr, "error parsing rc_override\n");
2264 video_enc->rc_override=
2265 av_realloc(video_enc->rc_override,
2266 sizeof(RcOverride)*(i+1));
2267 video_enc->rc_override[i].start_frame= start;
2268 video_enc->rc_override[i].end_frame = end;
2270 video_enc->rc_override[i].qscale= q;
2271 video_enc->rc_override[i].quality_factor= 1.0;
2274 video_enc->rc_override[i].qscale= 0;
2275 video_enc->rc_override[i].quality_factor= -q/100.0;
2280 video_enc->rc_override_count=i;
2282 video_enc->rc_max_rate = video_rc_max_rate;
2283 video_enc->rc_min_rate = video_rc_min_rate;
2284 video_enc->rc_buffer_size = video_rc_buffer_size;
2285 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2286 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2287 video_enc->i_quant_factor = video_i_qfactor;
2288 video_enc->b_quant_factor = video_b_qfactor;
2289 video_enc->i_quant_offset = video_i_qoffset;
2290 video_enc->b_quant_offset = video_b_qoffset;
2291 video_enc->dct_algo = dct_algo;
2292 video_enc->idct_algo = idct_algo;
2293 video_enc->strict_std_compliance = strict;
2295 video_enc->rtp_mode= 1;
2296 video_enc->rtp_payload_size= packet_size;
2300 video_enc->flags|= CODEC_FLAG_PSNR;
2302 video_enc->me_method = me_method;
2307 video_enc->flags |= CODEC_FLAG_PASS1;
2309 video_enc->flags |= CODEC_FLAG_PASS2;
2313 oc->streams[nb_streams] = st;
2318 AVCodecContext *audio_enc;
2320 st = av_mallocz(sizeof(AVStream));
2322 fprintf(stderr, "Could not alloc stream\n");
2325 avcodec_get_context_defaults(&st->codec);
2327 audio_enc = &st->codec;
2328 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2329 if (audio_stream_copy) {
2330 st->stream_copy = 1;
2332 codec_id = file_oformat->audio_codec;
2333 if (audio_codec_id != CODEC_ID_NONE)
2334 codec_id = audio_codec_id;
2335 audio_enc->codec_id = codec_id;
2337 audio_enc->bit_rate = audio_bit_rate;
2338 audio_enc->sample_rate = audio_sample_rate;
2339 /* For audio codecs other than AC3 we limit */
2340 /* the number of coded channels to stereo */
2341 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2342 audio_enc->channels = 2;
2344 audio_enc->channels = audio_channels;
2346 oc->streams[nb_streams] = st;
2350 oc->nb_streams = nb_streams;
2353 fprintf(stderr, "No audio or video streams available\n");
2358 pstrcpy(oc->title, sizeof(oc->title), str_title);
2360 pstrcpy(oc->author, sizeof(oc->author), str_author);
2362 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2364 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2367 output_files[nb_output_files++] = oc;
2369 strcpy(oc->filename, filename);
2371 /* check filename in case of an image number is expected */
2372 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2373 if (filename_number_test(oc->filename) < 0) {
2374 print_error(oc->filename, AVERROR_NUMEXPECTED);
2379 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2380 /* test if it already exists to avoid loosing precious files */
2381 if (!file_overwrite &&
2382 (strchr(filename, ':') == NULL ||
2383 strstart(filename, "file:", NULL))) {
2384 if (url_exist(filename)) {
2387 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2390 if (toupper(c) != 'Y') {
2391 fprintf(stderr, "Not overwriting - exiting\n");
2398 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2399 fprintf(stderr, "Could not open '%s'\n", filename);
2404 memset(ap, 0, sizeof(*ap));
2405 ap->image_format = image_format;
2406 if (av_set_parameters(oc, ap) < 0) {
2407 fprintf(stderr, "%s: Invalid encoding parameters\n",
2412 /* reset some options */
2413 file_oformat = NULL;
2414 file_iformat = NULL;
2415 image_format = NULL;
2418 audio_codec_id = CODEC_ID_NONE;
2419 video_codec_id = CODEC_ID_NONE;
2420 audio_stream_copy = 0;
2421 video_stream_copy = 0;
2424 /* prepare dummy protocols for grab */
2425 void prepare_grab(void)
2427 int has_video, has_audio, i, j;
2428 AVFormatContext *oc;
2429 AVFormatContext *ic;
2430 AVFormatParameters vp1, *vp = &vp1;
2431 AVFormatParameters ap1, *ap = &ap1;
2433 /* see if audio/video inputs are needed */
2436 memset(ap, 0, sizeof(*ap));
2437 memset(vp, 0, sizeof(*vp));
2438 for(j=0;j<nb_output_files;j++) {
2439 oc = output_files[j];
2440 for(i=0;i<oc->nb_streams;i++) {
2441 AVCodecContext *enc = &oc->streams[i]->codec;
2442 switch(enc->codec_type) {
2443 case CODEC_TYPE_AUDIO:
2444 if (enc->sample_rate > ap->sample_rate)
2445 ap->sample_rate = enc->sample_rate;
2446 if (enc->channels > ap->channels)
2447 ap->channels = enc->channels;
2450 case CODEC_TYPE_VIDEO:
2451 if (enc->width > vp->width)
2452 vp->width = enc->width;
2453 if (enc->height > vp->height)
2454 vp->height = enc->height;
2455 if (enc->frame_rate > vp->frame_rate)
2456 vp->frame_rate = enc->frame_rate;
2465 if (has_video == 0 && has_audio == 0) {
2466 fprintf(stderr, "Output file must have at least one audio or video stream\n");
2471 AVInputFormat *fmt1;
2472 fmt1 = av_find_input_format(video_grab_format);
2473 vp->device = video_device;
2474 vp->channel = video_channel;
2475 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
2476 fprintf(stderr, "Could not find video grab device\n");
2479 /* by now video grab has one stream */
2480 ic->streams[0]->r_frame_rate = vp->frame_rate;
2481 input_files[nb_input_files] = ic;
2482 dump_format(ic, nb_input_files, "", 0);
2485 if (has_audio && audio_grab_format) {
2486 AVInputFormat *fmt1;
2487 fmt1 = av_find_input_format(audio_grab_format);
2488 ap->device = audio_device;
2489 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2490 fprintf(stderr, "Could not find audio grab device\n");
2493 input_files[nb_input_files] = ic;
2494 dump_format(ic, nb_input_files, "", 0);
2499 /* open the necessary output devices for playing */
2500 void prepare_play(void)
2502 int has_video, has_audio;
2504 check_audio_video_inputs(&has_video, &has_audio);
2506 /* manual disable */
2507 if (audio_disable) {
2510 if (video_disable) {
2515 file_oformat = guess_format("audio_device", NULL, NULL);
2516 if (!file_oformat) {
2517 fprintf(stderr, "Could not find audio device\n");
2520 opt_output_file(audio_device);
2524 file_oformat = guess_format("framebuffer_device", NULL, NULL);
2525 if (!file_oformat) {
2526 fprintf(stderr, "Could not find framebuffer device\n");
2529 opt_output_file("");
2533 /* same option as mencoder */
2534 void opt_pass(const char *pass_str)
2537 pass = atoi(pass_str);
2538 if (pass != 1 && pass != 2) {
2539 fprintf(stderr, "pass number can be only 1 or 2\n");
2545 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
2546 INT64 getutime(void)
2548 return av_gettime();
2551 INT64 getutime(void)
2553 struct rusage rusage;
2555 getrusage(RUSAGE_SELF, &rusage);
2556 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2560 extern int ffm_nopts;
2562 void opt_bitexact(void)
2564 avcodec_set_bit_exact();
2565 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2569 void show_formats(void)
2571 AVInputFormat *ifmt;
2572 AVOutputFormat *ofmt;
2573 AVImageFormat *image_fmt;
2578 printf("Output audio/video file formats:");
2579 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2580 printf(" %s", ofmt->name);
2584 printf("Input audio/video file formats:");
2585 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2586 printf(" %s", ifmt->name);
2590 printf("Output image formats:");
2591 for(image_fmt = first_image_format; image_fmt != NULL;
2592 image_fmt = image_fmt->next) {
2593 if (image_fmt->img_write)
2594 printf(" %s", image_fmt->name);
2598 printf("Input image formats:");
2599 for(image_fmt = first_image_format; image_fmt != NULL;
2600 image_fmt = image_fmt->next) {
2601 if (image_fmt->img_read)
2602 printf(" %s", image_fmt->name);
2606 printf("Codecs:\n");
2607 printf(" Encoders:");
2608 for(p = first_avcodec; p != NULL; p = p->next) {
2610 printf(" %s", p->name);
2614 printf(" Decoders:");
2615 for(p = first_avcodec; p != NULL; p = p->next) {
2617 printf(" %s", p->name);
2621 printf("Supported file protocols:");
2622 for(up = first_protocol; up != NULL; up = up->next)
2623 printf(" %s:", up->name);
2626 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2627 printf("Motion estimation methods:");
2631 if ((pp - motion_str + 1) == ME_ZERO)
2632 printf("(fastest)");
2633 else if ((pp - motion_str + 1) == ME_FULL)
2634 printf("(slowest)");
2635 else if ((pp - motion_str + 1) == ME_EPZS)
2636 printf("(default)");
2643 void show_help(void)
2646 const OptionDef *po;
2649 prog = do_play ? "ffplay" : "ffmpeg";
2651 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2655 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2656 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2658 printf("usage: ffplay [options] input_file...\n"
2659 "Simple audio player\n");
2663 "Main options are:\n");
2666 printf("\nAdvanced options are:\n");
2667 for(po = options; po->name != NULL; po++) {
2669 expert = (po->flags & OPT_EXPERT) != 0;
2671 strcpy(buf, po->name);
2672 if (po->flags & HAS_ARG) {
2674 strcat(buf, po->argname);
2676 printf("-%-17s %s\n", buf, po->help);
2684 const OptionDef options[] = {
2685 { "L", 0, {(void*)show_licence}, "show license" },
2686 { "h", 0, {(void*)show_help}, "show help" },
2687 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2688 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2689 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
2690 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2691 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2692 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2693 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2694 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2695 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2696 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2697 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2698 { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
2699 { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
2701 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2702 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2703 { "em_rate", OPT_BOOL|OPT_EXPERT, {(void*)&emulate_frame_rate}, "makes img reading happen at nominal frame rate" },
2704 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2705 { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
2706 { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
2707 { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
2708 { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
2709 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2710 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2711 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2712 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2713 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2714 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2715 { "mbqmin", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
2716 { "mbqmax", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
2717 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2718 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2719 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2720 { "rc_init_cplx", HAS_ARG | OPT_EXPERT, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
2721 { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
2722 { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
2723 { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
2724 { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
2725 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
2726 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
2727 { "rc_override", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_override_string}, "Rate control override", "qualities for specific intervals" },
2728 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2729 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2730 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2731 { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
2732 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2733 { "vc", HAS_ARG | OPT_EXPERT, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
2734 { "dv1394", OPT_EXPERT, {(void*)opt_dv1394}, "set DV1394 grab", "" },
2735 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
2736 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2738 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2739 { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo", "algo" },
2740 { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
2741 { "ec", HAS_ARG | OPT_EXPERT, {(void*)opt_error_concealment}, "set error concealment", "" },
2742 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2743 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2744 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2745 { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
2746 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2747 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
2748 { "strict", HAS_ARG | OPT_EXPERT, {(void*)opt_strict}, "strictness", "how strictly to follow the standarts" },
2749 { "sameq", OPT_BOOL, {(void*)&same_quality},
2750 "use same video quality as source (implies VBR)" },
2751 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
2753 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2754 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2755 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2756 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2757 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2758 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
2759 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2760 "deinterlace pictures" },
2761 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2762 "add timings for benchmarking" },
2763 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2764 "dump each input packet" },
2765 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2766 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2767 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2768 { "vhook", HAS_ARG | OPT_EXPERT, {(void*)add_frame_hooker}, "insert video processing module", "module name and parameters" },
2772 int main(int argc, char **argv)
2775 const char *opt, *arg;
2776 const OptionDef *po;
2781 /* detect if invoked as player */
2782 i = strlen(argv[0]);
2783 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2791 while (optindex < argc) {
2792 opt = argv[optindex++];
2794 if (opt[0] == '-' && opt[1] != '\0') {
2796 while (po->name != NULL) {
2797 if (!strcmp(opt + 1, po->name))
2802 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2806 if (po->flags & HAS_ARG) {
2807 arg = argv[optindex++];
2809 fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
2813 if (po->flags & OPT_STRING) {
2815 str = av_strdup(arg);
2816 *po->u.str_arg = str;
2817 } else if (po->flags & OPT_BOOL) {
2820 po->u.func_arg(arg);
2824 opt_output_file(opt);
2826 opt_input_file(opt);
2833 /* file converter / grab */
2834 if (nb_output_files <= 0) {
2835 fprintf(stderr, "Must supply at least one output file\n");
2839 if (nb_input_files == 0) {
2844 if (nb_input_files <= 0) {
2845 fprintf(stderr, "Must supply at least one input file\n");
2852 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2853 stream_maps, nb_stream_maps);
2854 ti = getutime() - ti;
2856 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2860 for(i=0;i<nb_output_files;i++) {
2861 /* maybe av_close_output_file ??? */
2862 AVFormatContext *s = output_files[i];
2864 if (!(s->oformat->flags & AVFMT_NOFILE))
2866 for(j=0;j<s->nb_streams;j++)
2867 av_free(s->streams[j]);
2870 for(i=0;i<nb_input_files;i++)
2871 av_close_input_file(input_files[i]);
2876 #ifdef POWERPC_TBL_PERFORMANCE_REPORT
2877 extern void powerpc_display_perf_report(void);
2878 powerpc_display_perf_report();
2879 #endif /* POWERPC_TBL_PERFORMANCE_REPORT */