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
22 #include "framehook.h"
27 #include <sys/ioctl.h>
30 #include <sys/resource.h>
33 #include <sys/types.h>
34 #include <sys/select.h>
36 #define usleep(t) _sleep2((t)/1000)
41 #if !defined(INFINITY) && defined(HUGE_VAL)
42 #define INFINITY HUGE_VAL
45 #define MAXINT64 int64_t_C(0x7fffffffffffffff)
50 #define HAS_ARG 0x0001
51 #define OPT_BOOL 0x0002
52 #define OPT_EXPERT 0x0004
53 #define OPT_STRING 0x0008
55 void (*func_arg)(const char *);
63 /* select an input stream for an output stream */
64 typedef struct AVStreamMap {
69 extern const OptionDef options[];
75 static AVFormatContext *input_files[MAX_FILES];
76 static int nb_input_files = 0;
78 static AVFormatContext *output_files[MAX_FILES];
79 static int nb_output_files = 0;
81 static AVStreamMap stream_maps[MAX_FILES];
82 static int nb_stream_maps;
84 static AVInputFormat *file_iformat;
85 static AVOutputFormat *file_oformat;
86 static AVImageFormat *image_format;
87 static int frame_width = 160;
88 static int frame_height = 128;
89 static int frame_topBand = 0;
90 static int frame_bottomBand = 0;
91 static int frame_leftBand = 0;
92 static int frame_rightBand = 0;
93 static int frame_rate = 25 * FRAME_RATE_BASE;
94 static int video_bit_rate = 200*1000;
95 static int video_bit_rate_tolerance = 4000*1000;
96 static int video_qscale = 0;
97 static int video_qmin = 2;
98 static int video_qmax = 31;
99 static int video_mb_qmin = 2;
100 static int video_mb_qmax = 31;
101 static int video_qdiff = 3;
102 static float video_qblur = 0.5;
103 static float video_qcomp = 0.5;
104 #if 0 //experimental, (can be removed)
105 static float video_rc_qsquish=1.0;
106 static float video_rc_qmod_amp=0;
107 static int video_rc_qmod_freq=0;
109 static char *video_rc_override_string=NULL;
110 static char *video_rc_eq="tex^qComp";
111 static int video_rc_buffer_size=0;
112 static float video_rc_buffer_aggressivity=1.0;
113 static int video_rc_max_rate=0;
114 static int video_rc_min_rate=0;
115 static float video_rc_initial_cplx=0;
116 static float video_b_qfactor = 1.25;
117 static float video_b_qoffset = 1.25;
118 static float video_i_qfactor = -0.8;
119 static float video_i_qoffset = 0.0;
120 static int me_method = ME_EPZS;
121 static int video_disable = 0;
122 static int video_codec_id = CODEC_ID_NONE;
123 static int same_quality = 0;
124 static int b_frames = 0;
125 static int use_hq = 0;
126 static int use_4mv = 0;
128 static int use_aic = 0;
129 static int use_umv = 0;
131 static int use_h263p_extra = 0;
132 static int do_deinterlace = 0;
133 static int workaround_bugs = FF_BUG_AUTODETECT;
134 static int error_resilience = 2;
135 static int error_concealment = 3;
136 static int dct_algo = 0;
137 static int idct_algo = 0;
138 static int use_part = 0;
139 static int packet_size = 0;
140 static int strict = 0;
141 static int debug = 0;
143 static int gop_size = 12;
144 static int intra_only = 0;
145 static int audio_sample_rate = 44100;
146 static int audio_bit_rate = 64000;
147 static int audio_disable = 0;
148 static int audio_channels = 1;
149 static int audio_codec_id = CODEC_ID_NONE;
151 static int64_t recording_time = 0;
152 static int file_overwrite = 0;
153 static char *str_title = NULL;
154 static char *str_author = NULL;
155 static char *str_copyright = NULL;
156 static char *str_comment = NULL;
157 static int do_benchmark = 0;
158 static int do_hex_dump = 0;
159 static int do_play = 0;
160 static int do_psnr = 0;
161 static int do_vstats = 0;
162 static int do_pass = 0;
163 static int bitexact = 0;
164 static char *pass_logfilename = NULL;
165 static int audio_stream_copy = 0;
166 static int video_stream_copy = 0;
168 static int rate_emu = 0;
170 static char *video_grab_format = "video4linux";
171 static char *video_device = NULL;
172 static int video_channel = 0;
174 static char *audio_grab_format = "audio_device";
175 static char *audio_device = NULL;
177 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
179 typedef struct AVOutputStream {
180 int file_index; /* file index */
181 int index; /* stream index in the output file */
182 int source_index; /* AVInputStream index */
183 AVStream *st; /* stream in the output file */
184 int encoding_needed; /* true if encoding needed for this stream */
186 /* input pts and corresponding output pts
189 double sync_ipts_offset;
192 int video_resample; /* video_resample and video_crop are mutually exclusive */
193 AVPicture pict_tmp; /* temporary image for resampling */
194 ImgReSampleContext *img_resample_ctx; /* for image resampling */
196 int video_crop; /* video_resample and video_crop are mutually exclusive */
197 int topBand; /* cropping area sizes */
202 ReSampleContext *resample; /* for audio resampling */
203 FifoBuffer fifo; /* for compression: one audio fifo per codec */
207 typedef struct AVInputStream {
211 int discard; /* true if stream data should be discarded */
212 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
213 int64_t sample_index; /* current sample */
214 int frame_decoded; /* true if a video or audio frame has been decoded */
216 int64_t start; /* time when read started */
217 unsigned long frame; /* current frame */
220 typedef struct AVInputFile {
221 int eof_reached; /* true if eof reached */
222 int ist_index; /* index of first stream in ist_table */
223 int buffer_size; /* current total buffer size */
224 int buffer_size_max; /* buffer size at which we consider we can stop
226 int nb_streams; /* nb streams we are aware of */
231 /* init terminal so that we can grab keys */
232 static struct termios oldtty;
234 static void term_exit(void)
236 tcsetattr (0, TCSANOW, &oldtty);
239 static void term_init(void)
246 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
247 |INLCR|IGNCR|ICRNL|IXON);
248 tty.c_oflag |= OPOST;
249 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
250 tty.c_cflag &= ~(CSIZE|PARENB);
255 tcsetattr (0, TCSANOW, &tty);
258 #ifdef CONFIG_BEOS_NETSERVER
259 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
263 /* read a key without blocking */
264 static int read_key(void)
268 #ifndef CONFIG_BEOS_NETSERVER
276 n = select(1, &rfds, NULL, NULL, &tv);
290 /* no interactive support */
291 static void term_exit(void)
295 static void term_init(void)
299 static int read_key(void)
306 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
311 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
314 /* copy stream format */
315 s->nb_streams = ic->nb_streams;
316 for(i=0;i<ic->nb_streams;i++) {
319 st = av_mallocz(sizeof(AVFormatContext));
320 memcpy(st, ic->streams[i], sizeof(AVStream));
324 av_close_input_file(ic);
328 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
330 static void do_audio_out(AVFormatContext *s,
333 unsigned char *buf, int size)
336 uint8_t audio_buf[2*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it */
337 uint8_t audio_out[4*MAX_AUDIO_PACKET_SIZE]; /* XXX: allocate it - yep really WMA */
338 int size_out, frame_bytes, ret;
341 enc = &ost->st->codec;
343 if (ost->audio_resample) {
345 size_out = audio_resample(ost->resample,
346 (short *)buftmp, (short *)buf,
347 size / (ist->st->codec.channels * 2));
348 size_out = size_out * enc->channels * 2;
354 /* now encode as many frames as possible */
355 if (enc->frame_size > 1) {
356 /* output resampled raw samples */
357 fifo_write(&ost->fifo, buftmp, size_out,
360 frame_bytes = enc->frame_size * 2 * enc->channels;
362 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
363 &ost->fifo.rptr) == 0) {
364 ret = avcodec_encode_audio(enc, audio_out, sizeof(audio_out),
366 av_write_frame(s, ost->index, audio_out, ret);
369 /* output a pcm frame */
370 /* XXX: change encoding codec API to avoid this ? */
371 switch(enc->codec->id) {
372 case CODEC_ID_PCM_S16LE:
373 case CODEC_ID_PCM_S16BE:
374 case CODEC_ID_PCM_U16LE:
375 case CODEC_ID_PCM_U16BE:
378 size_out = size_out >> 1;
381 ret = avcodec_encode_audio(enc, audio_out, size_out,
383 av_write_frame(s, ost->index, audio_out, ret);
387 /* write a picture to a raw mux */
388 static void write_picture(AVFormatContext *s, int index, AVPicture *picture,
389 int pix_fmt, int w, int h)
391 uint8_t *buf, *src, *dest;
394 /* XXX: not efficient, should add test if we can take
395 directly the AVPicture */
397 case PIX_FMT_YUV420P:
398 size = avpicture_get_size(pix_fmt, w, h);
399 buf = av_malloc(size);
408 src = picture->data[i];
410 memcpy(dest, src, w);
412 src += picture->linesize[i];
416 case PIX_FMT_YUV422P:
418 buf = av_malloc(size);
426 src = picture->data[i];
428 memcpy(dest, src, w);
430 src += picture->linesize[i];
434 case PIX_FMT_YUV444P:
436 buf = av_malloc(size);
441 src = picture->data[i];
443 memcpy(dest, src, w);
445 src += picture->linesize[i];
451 buf = av_malloc(size);
455 src = picture->data[0];
457 memcpy(dest, src, w * 2);
459 src += picture->linesize[0];
465 buf = av_malloc(size);
469 src = picture->data[0];
471 memcpy(dest, src, w * 3);
473 src += picture->linesize[0];
479 av_write_frame(s, index, buf, size);
483 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
487 AVPicture picture_tmp;
490 dec = &ist->st->codec;
492 /* deinterlace : must be done before any resize */
493 if (do_deinterlace) {
496 /* create temporary picture */
497 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
498 buf = av_malloc(size);
502 picture2 = &picture_tmp;
503 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
505 if (avpicture_deinterlace(picture2, picture,
506 dec->pix_fmt, dec->width, dec->height) < 0) {
507 /* if error, do not deinterlace */
516 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
518 if (picture != picture2)
519 *picture = *picture2;
523 /* we begin to correct av delay at this threshold */
524 #define AV_DELAY_MAX 0.100
526 static void do_video_out(AVFormatContext *s,
529 AVPicture *in_picture,
530 int *frame_size, AVOutputStream *audio_sync)
532 int nb_frames, i, ret;
533 AVPicture *final_picture, *formatted_picture;
534 AVPicture picture_format_temp, picture_crop_temp;
535 static uint8_t *video_buffer;
536 uint8_t *buf = NULL, *buf1 = NULL;
537 AVCodecContext *enc, *dec;
539 #define VIDEO_BUFFER_SIZE (1024*1024)
541 enc = &ost->st->codec;
542 dec = &ist->st->codec;
544 /* by default, we output a single frame */
549 /* NOTE: the A/V sync is always done by considering the audio is
550 the master clock. It is suffisant for transcoding or playing,
551 but not for the general case */
553 /* compute the A-V delay and duplicate/remove frames if needed */
554 double adelta, vdelta, apts, vpts, av_delay;
556 if (audio_sync->sync_ipts != AV_NOPTS_VALUE &&
557 ost->sync_ipts != AV_NOPTS_VALUE) {
559 adelta = (double)(ost->st->pts.val - audio_sync->sync_opts) *
560 s->pts_num / s->pts_den;
561 apts = audio_sync->sync_ipts + adelta;
563 vdelta = (double)(ost->st->pts.val - ost->sync_opts) *
564 s->pts_num / s->pts_den;
565 vpts = ost->sync_ipts + vdelta;
567 av_delay = apts - vpts;
568 // printf("delay=%f\n", av_delay);
569 if (av_delay < -AV_DELAY_MAX)
571 else if (av_delay > AV_DELAY_MAX)
577 if (ost->sync_ipts != AV_NOPTS_VALUE) {
578 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
579 if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
580 if (vdelta < -AV_DELAY_MAX)
582 else if (vdelta > AV_DELAY_MAX)
585 ost->sync_ipts_offset -= vdelta;
586 if (!ost->sync_ipts_offset)
587 ost->sync_ipts_offset = 0.000001; /* one microsecond */
592 static char *action[] = { "drop frame", "copy frame", "dup frame" };
593 printf("Input PTS %12.6f, output PTS %12.6f: %s\n",
594 (double) ost->sync_ipts, (double) ost->st->pts.val * s->pts_num / s->pts_den,
604 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
608 /* convert pixel format if needed */
609 if (enc->pix_fmt != dec->pix_fmt) {
612 /* create temporary picture */
613 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
614 buf = av_malloc(size);
617 formatted_picture = &picture_format_temp;
618 avpicture_fill(formatted_picture, buf, enc->pix_fmt, dec->width, dec->height);
620 if (img_convert(formatted_picture, enc->pix_fmt,
621 in_picture, dec->pix_fmt,
622 dec->width, dec->height) < 0) {
623 fprintf(stderr, "pixel format conversion not handled\n");
627 formatted_picture = in_picture;
630 /* XXX: resampling could be done before raw format convertion in
631 some cases to go faster */
632 /* XXX: only works for YUV420P */
633 if (ost->video_resample) {
634 final_picture = &ost->pict_tmp;
635 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
636 } else if (ost->video_crop) {
637 picture_crop_temp.data[0] = formatted_picture->data[0] +
638 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
640 picture_crop_temp.data[1] = formatted_picture->data[1] +
641 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
642 (ost->leftBand >> 1);
644 picture_crop_temp.data[2] = formatted_picture->data[2] +
645 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
646 (ost->leftBand >> 1);
648 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
649 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
650 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
651 final_picture = &picture_crop_temp;
653 final_picture = formatted_picture;
655 /* duplicates frame if needed */
656 /* XXX: pb because no interleaving */
657 for(i=0;i<nb_frames;i++) {
658 if (enc->codec_id != CODEC_ID_RAWVIDEO) {
661 memset(&big_picture, 0, sizeof(AVFrame));
662 *(AVPicture*)&big_picture= *final_picture;
664 /* handles sameq here. This is not correct because it may
665 not be a global option */
667 big_picture.quality = ist->st->quality;
669 big_picture.quality = ost->st->quality;
671 ret = avcodec_encode_video(enc,
672 video_buffer, VIDEO_BUFFER_SIZE,
674 //enc->frame_number = enc->real_pict_num;
675 av_write_frame(s, ost->index, video_buffer, ret);
677 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
678 // enc->frame_number-1, enc->real_pict_num, ret,
680 /* if two pass, output log */
681 if (ost->logfile && enc->stats_out) {
682 fprintf(ost->logfile, "%s", enc->stats_out);
685 if (s->oformat->flags & AVFMT_RAWPICTURE) {
686 /* raw pictures are written as AVPicture structure to
687 avoid any copies. We support temorarily the older
689 av_write_frame(s, ost->index,
690 (uint8_t *)final_picture, sizeof(AVPicture));
692 write_picture(s, ost->index, final_picture, enc->pix_fmt,
693 enc->width, enc->height);
703 static double psnr(double d){
704 if(d==0) return INFINITY;
705 return -10.0*log(d)/log(10.0);
708 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
711 static FILE *fvstats=NULL;
712 static int64_t total_size = 0;
719 double ti1, bitrate, avg_bitrate;
723 today = localtime(&today2);
724 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
727 fvstats = fopen(filename,"w");
735 enc = &ost->st->codec;
736 total_size += frame_size;
737 if (enc->codec_type == CODEC_TYPE_VIDEO) {
738 frame_number = ost->frame_number;
739 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality);
740 if (enc->flags&CODEC_FLAG_PSNR)
741 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
743 fprintf(fvstats,"f_size= %6d ", frame_size);
744 /* compute pts value */
745 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
749 bitrate = (double)(frame_size * 8) * enc->frame_rate / FRAME_RATE_BASE / 1000.0;
750 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
751 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
752 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
753 fprintf(fvstats,"type= %s\n", enc->coded_frame->key_frame == 1 ? "I" : "P");
757 static void print_report(AVFormatContext **output_files,
758 AVOutputStream **ost_table, int nb_ostreams,
763 AVFormatContext *oc, *os;
766 int frame_number, vid, i;
767 double bitrate, ti1, pts;
768 static int64_t last_time = -1;
770 if (!is_last_report) {
772 /* display the report every 0.5 seconds */
773 cur_time = av_gettime();
774 if (last_time == -1) {
775 last_time = cur_time;
778 if ((cur_time - last_time) < 500000)
780 last_time = cur_time;
784 oc = output_files[0];
786 total_size = url_ftell(&oc->pb);
791 for(i=0;i<nb_ostreams;i++) {
793 os = output_files[ost->file_index];
794 enc = &ost->st->codec;
795 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
796 sprintf(buf + strlen(buf), "q=%2.1f ",
797 enc->coded_frame->quality);
799 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
800 frame_number = ost->frame_number;
801 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
802 frame_number, enc->coded_frame ? enc->coded_frame->quality : 0);
803 if (enc->flags&CODEC_FLAG_PSNR)
804 sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
807 /* compute min output value */
808 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
809 if ((pts < ti1) && (pts > 0))
814 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
816 sprintf(buf + strlen(buf),
817 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
818 (double)total_size / 1024, ti1, bitrate);
820 fprintf(stderr, "%s ", buf);
822 if (is_last_report) {
823 fprintf(stderr, "\n");
825 fprintf(stderr, "\r");
831 * The following code is the main loop of the file converter
833 static int av_encode(AVFormatContext **output_files,
835 AVFormatContext **input_files,
837 AVStreamMap *stream_maps, int nb_stream_maps)
839 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0, pts_set;
840 AVFormatContext *is, *os;
841 AVCodecContext *codec, *icodec;
842 AVOutputStream *ost, **ost_table = NULL;
843 AVInputStream *ist, **ist_table = NULL;
844 AVInputFile *file_table;
845 AVFormatContext *stream_no_data;
848 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
852 /* input stream init */
854 for(i=0;i<nb_input_files;i++) {
856 file_table[i].ist_index = j;
857 file_table[i].nb_streams = is->nb_streams;
862 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
866 for(i=0;i<nb_istreams;i++) {
867 ist = av_mallocz(sizeof(AVInputStream));
873 for(i=0;i<nb_input_files;i++) {
875 for(k=0;k<is->nb_streams;k++) {
876 ist = ist_table[j++];
877 ist->st = is->streams[k];
880 ist->discard = 1; /* the stream is discarded by default
883 if (ist->st->codec.rate_emu) {
884 ist->start = av_gettime();
890 /* output stream init */
892 for(i=0;i<nb_output_files;i++) {
893 os = output_files[i];
894 nb_ostreams += os->nb_streams;
896 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
897 fprintf(stderr, "Number of stream maps must match number of output streams\n");
901 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
904 for(i=0;i<nb_ostreams;i++) {
905 ost = av_mallocz(sizeof(AVOutputStream));
912 for(k=0;k<nb_output_files;k++) {
913 os = output_files[k];
914 for(i=0;i<os->nb_streams;i++) {
916 ost = ost_table[n++];
919 ost->st = os->streams[i];
920 if (nb_stream_maps > 0) {
921 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
922 stream_maps[n-1].stream_index;
924 /* get corresponding input stream index : we select the first one with the right type */
926 for(j=0;j<nb_istreams;j++) {
929 ist->st->codec.codec_type == ost->st->codec.codec_type) {
930 ost->source_index = j;
936 /* try again and reuse existing stream */
937 for(j=0;j<nb_istreams;j++) {
939 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
940 ost->source_index = j;
945 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
946 ost->file_index, ost->index);
951 ist = ist_table[ost->source_index];
956 /* for each output stream, we compute the right encoding parameters */
957 for(i=0;i<nb_ostreams;i++) {
959 ist = ist_table[ost->source_index];
961 codec = &ost->st->codec;
962 icodec = &ist->st->codec;
964 if (ost->st->stream_copy) {
965 /* if stream_copy is selected, no need to decode or encode */
966 codec->codec_id = icodec->codec_id;
967 codec->codec_type = icodec->codec_type;
968 codec->codec_tag = icodec->codec_tag;
969 codec->bit_rate = icodec->bit_rate;
970 switch(codec->codec_type) {
971 case CODEC_TYPE_AUDIO:
972 codec->sample_rate = icodec->sample_rate;
973 codec->channels = icodec->channels;
975 case CODEC_TYPE_VIDEO:
976 codec->frame_rate = icodec->frame_rate;
977 codec->width = icodec->width;
978 codec->height = icodec->height;
984 switch(codec->codec_type) {
985 case CODEC_TYPE_AUDIO:
986 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
989 if (codec->channels == icodec->channels &&
990 codec->sample_rate == icodec->sample_rate) {
991 ost->audio_resample = 0;
993 if (codec->channels != icodec->channels &&
994 icodec->codec_id == CODEC_ID_AC3) {
995 /* Special case for 5:1 AC3 input */
996 /* and mono or stereo output */
997 /* Request specific number of channels */
998 icodec->channels = codec->channels;
999 if (codec->sample_rate == icodec->sample_rate)
1000 ost->audio_resample = 0;
1002 ost->audio_resample = 1;
1003 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1005 icodec->sample_rate);
1007 /* Request specific number of channels */
1008 icodec->channels = codec->channels;
1010 ost->audio_resample = 1;
1011 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1013 icodec->sample_rate);
1016 ist->decoding_needed = 1;
1017 ost->encoding_needed = 1;
1019 case CODEC_TYPE_VIDEO:
1020 if (codec->width == icodec->width &&
1021 codec->height == icodec->height &&
1022 frame_topBand == 0 &&
1023 frame_bottomBand == 0 &&
1024 frame_leftBand == 0 &&
1025 frame_rightBand == 0)
1027 ost->video_resample = 0;
1028 ost->video_crop = 0;
1029 } else if ((codec->width == icodec->width -
1030 (frame_leftBand + frame_rightBand)) &&
1031 (codec->height == icodec->height -
1032 (frame_topBand + frame_bottomBand)))
1034 ost->video_resample = 0;
1035 ost->video_crop = 1;
1036 ost->topBand = frame_topBand;
1037 ost->leftBand = frame_leftBand;
1040 ost->video_resample = 1;
1041 ost->video_crop = 0; // cropping is handled as part of resample
1042 buf = av_malloc((codec->width * codec->height * 3) / 2);
1045 ost->pict_tmp.data[0] = buf;
1046 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
1047 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
1048 ost->pict_tmp.linesize[0] = codec->width;
1049 ost->pict_tmp.linesize[1] = codec->width / 2;
1050 ost->pict_tmp.linesize[2] = codec->width / 2;
1052 ost->img_resample_ctx = img_resample_full_init(
1053 ost->st->codec.width, ost->st->codec.height,
1054 ist->st->codec.width, ist->st->codec.height,
1055 frame_topBand, frame_bottomBand,
1056 frame_leftBand, frame_rightBand);
1058 ost->encoding_needed = 1;
1059 ist->decoding_needed = 1;
1065 if (ost->encoding_needed &&
1066 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1067 char logfilename[1024];
1072 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1074 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1075 if (codec->flags & CODEC_FLAG_PASS1) {
1076 f = fopen(logfilename, "w");
1078 perror(logfilename);
1083 /* read the log file */
1084 f = fopen(logfilename, "r");
1086 perror(logfilename);
1089 fseek(f, 0, SEEK_END);
1091 fseek(f, 0, SEEK_SET);
1092 logbuffer = av_malloc(size + 1);
1094 fprintf(stderr, "Could not allocate log buffer\n");
1097 fread(logbuffer, 1, size, f);
1099 logbuffer[size] = '\0';
1100 codec->stats_in = logbuffer;
1106 /* dump the file output parameters - cannot be done before in case
1108 for(i=0;i<nb_output_files;i++) {
1109 dump_format(output_files[i], i, output_files[i]->filename, 1);
1112 /* dump the stream mapping */
1113 fprintf(stderr, "Stream mapping:\n");
1114 for(i=0;i<nb_ostreams;i++) {
1116 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1117 ist_table[ost->source_index]->file_index,
1118 ist_table[ost->source_index]->index,
1123 /* open each encoder */
1124 for(i=0;i<nb_ostreams;i++) {
1126 if (ost->encoding_needed) {
1128 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1130 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1131 ost->file_index, ost->index);
1134 if (avcodec_open(&ost->st->codec, codec) < 0) {
1135 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1136 ost->file_index, ost->index);
1142 /* open each decoder */
1143 for(i=0;i<nb_istreams;i++) {
1145 if (ist->decoding_needed) {
1147 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1149 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1150 ist->st->codec.codec_id, ist->file_index, ist->index);
1153 if (avcodec_open(&ist->st->codec, codec) < 0) {
1154 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1155 ist->file_index, ist->index);
1158 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1159 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1160 ist->frame_decoded = 1;
1165 for(i=0;i<nb_istreams;i++) {
1169 /* compute buffer size max (should use a complete heuristic) */
1170 for(i=0;i<nb_input_files;i++) {
1171 file_table[i].buffer_size_max = 2048;
1174 /* open files and write file headers */
1175 for(i=0;i<nb_output_files;i++) {
1176 os = output_files[i];
1177 if (av_write_header(os) < 0) {
1178 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1184 #ifndef CONFIG_WIN32
1186 fprintf(stderr, "Press [q] to stop encoding\n");
1188 fprintf(stderr, "Press [q] to stop playing\n");
1197 int file_index, ist_index;
1202 int data_size, got_picture;
1204 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1205 void *buffer_to_free;
1209 /* if 'q' pressed, exits */
1211 /* read_key() returns 0 on EOF */
1217 /* select the stream that we must read now by looking at the
1218 smallest output pts */
1221 for(i=0;i<nb_ostreams;i++) {
1224 os = output_files[ost->file_index];
1225 ist = ist_table[ost->source_index];
1226 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1227 if (!file_table[ist->file_index].eof_reached &&
1230 file_index = ist->file_index;
1233 /* if none, if is finished */
1234 if (file_index < 0) {
1238 /* finish if recording time exhausted */
1239 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1242 /* read a packet from it and output it in the fifo */
1243 is = input_files[file_index];
1244 if (av_read_packet(is, &pkt) < 0) {
1245 file_table[file_index].eof_reached = 1;
1249 stream_no_data = is;
1254 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1255 av_hex_dump(pkt.data, pkt.size);
1257 /* the following test is needed in case new streams appear
1258 dynamically in stream : we ignore them */
1259 if (pkt.stream_index >= file_table[file_index].nb_streams)
1260 goto discard_packet;
1261 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1262 ist = ist_table[ist_index];
1264 goto discard_packet;
1266 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1274 ipts = AV_NOPTS_VALUE;
1276 /* decode the packet if needed */
1277 data_buf = NULL; /* fail safe */
1279 if (ist->decoding_needed) {
1280 /* NOTE1: we only take into account the PTS if a new
1281 frame has begun (MPEG semantics) */
1282 /* NOTE2: even if the fraction is not initialized,
1283 av_frac_set can be used to set the integer part */
1284 if (ist->frame_decoded &&
1285 pkt.pts != AV_NOPTS_VALUE &&
1288 ist->frame_decoded = 0;
1292 switch(ist->st->codec.codec_type) {
1293 case CODEC_TYPE_AUDIO:
1294 /* XXX: could avoid copy if PCM 16 bits with same
1295 endianness as CPU */
1296 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1300 /* Some bug in mpeg audio decoder gives */
1301 /* data_size < 0, it seems they are overflows */
1302 if (data_size <= 0) {
1303 /* no audio frame */
1308 data_buf = (uint8_t *)samples;
1310 case CODEC_TYPE_VIDEO:
1311 if (ist->st->codec.codec_id == CODEC_ID_RAWVIDEO) {
1314 size = (ist->st->codec.width * ist->st->codec.height);
1315 avpicture_fill(&picture, ptr,
1316 ist->st->codec.pix_fmt,
1317 ist->st->codec.width,
1318 ist->st->codec.height);
1321 AVFrame big_picture;
1323 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1324 ret = avcodec_decode_video(&ist->st->codec,
1325 &big_picture, &got_picture, ptr, len);
1326 picture= *(AVPicture*)&big_picture;
1327 ist->st->quality= big_picture.quality;
1330 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1331 ist->file_index, ist->index);
1332 av_free_packet(&pkt);
1336 /* no picture yet */
1356 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1357 pre_process_video_frame(ist, &picture, &buffer_to_free);
1360 ist->frame_decoded = 1;
1362 /* frame rate emulation */
1363 if (ist->st->codec.rate_emu) {
1364 int64_t pts = ((int64_t) ist->frame * FRAME_RATE_BASE * 1000000) / (ist->st->codec.frame_rate);
1365 int64_t now = av_gettime() - ist->start;
1373 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1374 is the one of the next displayed one */
1375 /* XXX: add mpeg4 too ? */
1376 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1377 if (ist->st->codec.pict_type != B_TYPE) {
1379 tmp = ist->last_ip_pts;
1380 ist->last_ip_pts = ist->frac_pts.val;
1381 ist->frac_pts.val = tmp;
1385 /* transcode raw format, encode packets and output them */
1387 for(i=0;i<nb_ostreams;i++) {
1391 if (ost->source_index == ist_index) {
1392 os = output_files[ost->file_index];
1394 if (ipts != AV_NOPTS_VALUE) {
1396 printf("%d: got pts=%f %f\n",
1397 i, pkt.pts / 90000.0,
1398 (ipts - ost->st->pts.val) / 90000.0);
1400 /* set the input output pts pairs */
1401 ost->sync_ipts = (double)ipts * is->pts_num /
1403 /* XXX: take into account the various fifos,
1404 in particular for audio */
1405 ost->sync_opts = ost->st->pts.val;
1406 //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);
1408 //printf("pts.val=%lld\n", ost->st->pts.val);
1409 ost->sync_ipts = AV_NOPTS_VALUE;
1412 if (ost->encoding_needed) {
1413 switch(ost->st->codec.codec_type) {
1414 case CODEC_TYPE_AUDIO:
1415 do_audio_out(os, ost, ist, data_buf, data_size);
1417 case CODEC_TYPE_VIDEO:
1418 /* find an audio stream for synchro */
1421 AVOutputStream *audio_sync, *ost1;
1423 for(i=0;i<nb_ostreams;i++) {
1424 ost1 = ost_table[i];
1425 if (ost1->file_index == ost->file_index &&
1426 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1432 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1433 if (do_vstats && frame_size)
1434 do_video_stats(os, ost, frame_size);
1443 /* no reencoding needed : output the packet directly */
1444 /* force the input stream PTS */
1446 //XXX/FIXME set keyframe flag from demuxer (or optionally from decoder)
1447 memset(&avframe, 0, sizeof(AVFrame));
1448 ost->st->codec.coded_frame= &avframe;
1450 av_write_frame(os, ost->index, data_buf, data_size);
1451 ost->st->codec.frame_number++;
1452 ost->frame_number++;
1456 av_free(buffer_to_free);
1457 ipts = AV_NOPTS_VALUE;
1460 av_free_packet(&pkt);
1462 /* dump report by using the output first video and audio streams */
1463 print_report(output_files, ost_table, nb_ostreams, 0);
1467 /* dump report by using the first video and audio streams */
1468 print_report(output_files, ost_table, nb_ostreams, 1);
1470 /* close each encoder */
1471 for(i=0;i<nb_ostreams;i++) {
1473 if (ost->encoding_needed) {
1474 av_freep(&ost->st->codec.stats_in);
1475 avcodec_close(&ost->st->codec);
1479 /* close each decoder */
1480 for(i=0;i<nb_istreams;i++) {
1482 if (ist->decoding_needed) {
1483 avcodec_close(&ist->st->codec);
1488 /* write the trailer if needed and close file */
1489 for(i=0;i<nb_output_files;i++) {
1490 os = output_files[i];
1491 av_write_trailer(os);
1497 av_free(file_table);
1500 for(i=0;i<nb_istreams;i++) {
1507 for(i=0;i<nb_ostreams;i++) {
1511 fclose(ost->logfile);
1512 ost->logfile = NULL;
1514 fifo_free(&ost->fifo); /* works even if fifo is not
1515 initialized but set to zero */
1516 av_free(ost->pict_tmp.data[0]);
1517 if (ost->video_resample)
1518 img_resample_close(ost->img_resample_ctx);
1519 if (ost->audio_resample)
1520 audio_resample_close(ost->resample);
1533 int file_read(const char *filename)
1536 unsigned char buffer[1024];
1539 if (url_open(&h, filename, O_RDONLY) < 0) {
1540 printf("could not open '%s'\n", filename);
1544 len = url_read(h, buffer, sizeof(buffer));
1547 for(i=0;i<len;i++) putchar(buffer[i]);
1554 static void show_licence(void)
1557 "ffmpeg version " FFMPEG_VERSION "\n"
1558 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1559 "This library is free software; you can redistribute it and/or\n"
1560 "modify it under the terms of the GNU Lesser General Public\n"
1561 "License as published by the Free Software Foundation; either\n"
1562 "version 2 of the License, or (at your option) any later version.\n"
1564 "This library is distributed in the hope that it will be useful,\n"
1565 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1566 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1567 "Lesser General Public License for more details.\n"
1569 "You should have received a copy of the GNU Lesser General Public\n"
1570 "License along with this library; if not, write to the Free Software\n"
1571 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1576 static void opt_image_format(const char *arg)
1580 for(f = first_image_format; f != NULL; f = f->next) {
1581 if (!strcmp(arg, f->name))
1585 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1591 static void opt_format(const char *arg)
1593 /* compatibility stuff for pgmyuv */
1594 if (!strcmp(arg, "pgmyuv")) {
1595 opt_image_format(arg);
1599 file_iformat = av_find_input_format(arg);
1600 file_oformat = guess_format(arg, NULL, NULL);
1601 if (!file_iformat && !file_oformat) {
1602 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1607 static void opt_video_bitrate(const char *arg)
1609 video_bit_rate = atoi(arg) * 1000;
1612 static void opt_video_bitrate_tolerance(const char *arg)
1614 video_bit_rate_tolerance = atoi(arg) * 1000;
1617 static void opt_video_bitrate_max(const char *arg)
1619 video_rc_max_rate = atoi(arg) * 1000;
1622 static void opt_video_bitrate_min(const char *arg)
1624 video_rc_min_rate = atoi(arg) * 1000;
1627 static void opt_video_buffer_size(const char *arg)
1629 video_rc_buffer_size = atoi(arg) * 1000;
1632 static void opt_video_rc_eq(char *arg)
1637 static void opt_video_rc_override_string(char *arg)
1639 video_rc_override_string = arg;
1643 static void opt_workaround_bugs(const char *arg)
1645 workaround_bugs = atoi(arg);
1648 static void opt_dct_algo(const char *arg)
1650 dct_algo = atoi(arg);
1653 static void opt_idct_algo(const char *arg)
1655 idct_algo = atoi(arg);
1659 static void opt_error_resilience(const char *arg)
1661 error_resilience = atoi(arg);
1664 static void opt_error_concealment(const char *arg)
1666 error_concealment = atoi(arg);
1669 static void opt_debug(const char *arg)
1674 static void opt_frame_rate(const char *arg)
1676 frame_rate = (int)(strtod(arg, 0) * FRAME_RATE_BASE);
1680 static void opt_frame_crop_top(const char *arg)
1682 frame_topBand = atoi(arg);
1683 if (frame_topBand < 0) {
1684 fprintf(stderr, "Incorrect top crop size\n");
1687 if ((frame_topBand % 2) != 0) {
1688 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1691 if ((frame_topBand) >= frame_height){
1692 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1695 frame_height -= frame_topBand;
1698 static void opt_frame_crop_bottom(const char *arg)
1700 frame_bottomBand = atoi(arg);
1701 if (frame_bottomBand < 0) {
1702 fprintf(stderr, "Incorrect bottom crop size\n");
1705 if ((frame_bottomBand % 2) != 0) {
1706 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1709 if ((frame_bottomBand) >= frame_height){
1710 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1713 frame_height -= frame_bottomBand;
1716 static void opt_frame_crop_left(const char *arg)
1718 frame_leftBand = atoi(arg);
1719 if (frame_leftBand < 0) {
1720 fprintf(stderr, "Incorrect left crop size\n");
1723 if ((frame_leftBand % 2) != 0) {
1724 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1727 if ((frame_leftBand) >= frame_width){
1728 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1731 frame_width -= frame_leftBand;
1734 static void opt_frame_crop_right(const char *arg)
1736 frame_rightBand = atoi(arg);
1737 if (frame_rightBand < 0) {
1738 fprintf(stderr, "Incorrect right crop size\n");
1741 if ((frame_rightBand % 2) != 0) {
1742 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1745 if ((frame_rightBand) >= frame_width){
1746 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1749 frame_width -= frame_rightBand;
1752 static void opt_frame_size(const char *arg)
1754 parse_image_size(&frame_width, &frame_height, arg);
1755 if (frame_width <= 0 || frame_height <= 0) {
1756 fprintf(stderr, "Incorrect frame size\n");
1759 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1760 fprintf(stderr, "Frame size must be a multiple of 2\n");
1765 static void opt_gop_size(const char *arg)
1767 gop_size = atoi(arg);
1770 static void opt_b_frames(const char *arg)
1772 b_frames = atoi(arg);
1773 if (b_frames > FF_MAX_B_FRAMES) {
1774 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1776 } else if (b_frames < 1) {
1777 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1782 static void opt_qscale(const char *arg)
1784 video_qscale = atoi(arg);
1785 if (video_qscale < 0 ||
1786 video_qscale > 31) {
1787 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1792 static void opt_qmin(const char *arg)
1794 video_qmin = atoi(arg);
1795 if (video_qmin < 0 ||
1797 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1802 static void opt_qmax(const char *arg)
1804 video_qmax = atoi(arg);
1805 if (video_qmax < 0 ||
1807 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1812 static void opt_mb_qmin(const char *arg)
1814 video_mb_qmin = atoi(arg);
1815 if (video_mb_qmin < 0 ||
1816 video_mb_qmin > 31) {
1817 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1822 static void opt_mb_qmax(const char *arg)
1824 video_mb_qmax = atoi(arg);
1825 if (video_mb_qmax < 0 ||
1826 video_mb_qmax > 31) {
1827 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1832 static void opt_qdiff(const char *arg)
1834 video_qdiff = atoi(arg);
1835 if (video_qdiff < 0 ||
1837 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1842 static void opt_qblur(const char *arg)
1844 video_qblur = atof(arg);
1847 static void opt_qcomp(const char *arg)
1849 video_qcomp = atof(arg);
1852 static void opt_rc_initial_cplx(const char *arg)
1854 video_rc_initial_cplx = atof(arg);
1856 static void opt_b_qfactor(const char *arg)
1858 video_b_qfactor = atof(arg);
1860 static void opt_i_qfactor(const char *arg)
1862 video_i_qfactor = atof(arg);
1864 static void opt_b_qoffset(const char *arg)
1866 video_b_qoffset = atof(arg);
1868 static void opt_i_qoffset(const char *arg)
1870 video_i_qoffset = atof(arg);
1873 static void opt_packet_size(const char *arg)
1875 packet_size= atoi(arg);
1878 static void opt_strict(const char *arg)
1883 static void opt_audio_bitrate(const char *arg)
1885 audio_bit_rate = atoi(arg) * 1000;
1888 static void opt_audio_rate(const char *arg)
1890 audio_sample_rate = atoi(arg);
1893 static void opt_audio_channels(const char *arg)
1895 audio_channels = atoi(arg);
1898 static void opt_video_device(const char *arg)
1900 video_device = av_strdup(arg);
1903 static void opt_video_channel(const char *arg)
1905 video_channel = strtol(arg, NULL, 0);
1908 static void opt_audio_device(const char *arg)
1910 audio_device = av_strdup(arg);
1913 static void opt_dv1394(const char *arg)
1915 video_grab_format = "dv1394";
1916 audio_grab_format = NULL;
1919 static void opt_audio_codec(const char *arg)
1923 if (!strcmp(arg, "copy")) {
1924 audio_stream_copy = 1;
1928 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1933 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1936 audio_codec_id = p->id;
1941 static void add_frame_hooker(const char *arg)
1946 char *args = av_strdup(arg);
1948 argv[0] = strtok(args, " ");
1949 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
1952 i = frame_hook_add(argc, argv);
1955 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
1960 const char *motion_str[] = {
1970 static void opt_motion_estimation(const char *arg)
1976 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1979 if (!strcmp(*p, arg))
1983 me_method = (p - motion_str) + 1;
1986 static void opt_video_codec(const char *arg)
1990 if (!strcmp(arg, "copy")) {
1991 video_stream_copy = 1;
1995 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
2000 fprintf(stderr, "Unknown video codec '%s'\n", arg);
2003 video_codec_id = p->id;
2008 static void opt_map(const char *arg)
2014 m = &stream_maps[nb_stream_maps++];
2016 m->file_index = strtol(arg, (char **)&p, 0);
2020 m->stream_index = strtol(p, (char **)&p, 0);
2023 static void opt_recording_time(const char *arg)
2025 recording_time = parse_date(arg, 1);
2028 static void print_error(const char *filename, int err)
2031 case AVERROR_NUMEXPECTED:
2032 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
2033 "Use '%%d' to specify the image number:\n"
2034 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
2035 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
2038 case AVERROR_INVALIDDATA:
2039 fprintf(stderr, "%s: Error while parsing header\n", filename);
2042 fprintf(stderr, "%s: Unknown format\n", filename);
2045 fprintf(stderr, "%s: Error while opening file\n", filename);
2050 static void opt_input_file(const char *filename)
2052 AVFormatContext *ic;
2053 AVFormatParameters params, *ap = ¶ms;
2054 int err, i, ret, rfps;
2056 if (!strcmp(filename, "-"))
2059 /* get default parameters from command line */
2060 memset(ap, 0, sizeof(*ap));
2061 ap->sample_rate = audio_sample_rate;
2062 ap->channels = audio_channels;
2063 ap->frame_rate = frame_rate;
2064 ap->width = frame_width;
2065 ap->height = frame_height;
2066 ap->image_format = image_format;
2068 /* open the input file with generic libav function */
2069 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
2071 print_error(filename, err);
2075 /* If not enough info to get the stream parameters, we decode the
2076 first frames to get it. (used in mpeg case for example) */
2077 ret = av_find_stream_info(ic);
2079 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2083 /* update the current parameters so that they match the one of the input stream */
2084 for(i=0;i<ic->nb_streams;i++) {
2085 AVCodecContext *enc = &ic->streams[i]->codec;
2086 switch(enc->codec_type) {
2087 case CODEC_TYPE_AUDIO:
2088 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2089 audio_channels = enc->channels;
2090 audio_sample_rate = enc->sample_rate;
2092 case CODEC_TYPE_VIDEO:
2093 frame_height = enc->height;
2094 frame_width = enc->width;
2095 rfps = ic->streams[i]->r_frame_rate;
2096 enc->workaround_bugs = workaround_bugs;
2097 enc->error_resilience = error_resilience;
2098 enc->error_concealment = error_concealment;
2099 enc->idct_algo= idct_algo;
2101 /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
2102 enc->flags|= CODEC_FLAG_TRUNCATED; */
2103 if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO)
2104 enc->flags|= CODEC_FLAG_TRUNCATED;
2107 enc->flags|= CODEC_FLAG_BITEXACT;
2109 if (enc->frame_rate != rfps) {
2110 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2111 i, (float)enc->frame_rate / FRAME_RATE_BASE,
2112 (float)rfps / FRAME_RATE_BASE);
2114 /* update the current frame rate to match the stream frame rate */
2117 enc->rate_emu = rate_emu;
2124 input_files[nb_input_files] = ic;
2125 /* dump the file content */
2126 dump_format(ic, nb_input_files, filename, 0);
2128 file_iformat = NULL;
2129 file_oformat = NULL;
2130 image_format = NULL;
2135 static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2137 int has_video, has_audio, i, j;
2138 AVFormatContext *ic;
2142 for(j=0;j<nb_input_files;j++) {
2143 ic = input_files[j];
2144 for(i=0;i<ic->nb_streams;i++) {
2145 AVCodecContext *enc = &ic->streams[i]->codec;
2146 switch(enc->codec_type) {
2147 case CODEC_TYPE_AUDIO:
2150 case CODEC_TYPE_VIDEO:
2158 *has_video_ptr = has_video;
2159 *has_audio_ptr = has_audio;
2162 static void opt_output_file(const char *filename)
2165 AVFormatContext *oc;
2166 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2168 AVFormatParameters params, *ap = ¶ms;
2170 if (!strcmp(filename, "-"))
2173 oc = av_mallocz(sizeof(AVFormatContext));
2175 if (!file_oformat) {
2176 file_oformat = guess_format(NULL, filename, NULL);
2177 if (!file_oformat) {
2178 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2184 oc->oformat = file_oformat;
2186 if (!strcmp(file_oformat->name, "ffm") &&
2187 strstart(filename, "http:", NULL)) {
2188 /* special case for files sent to ffserver: we get the stream
2189 parameters from ffserver */
2190 if (read_ffserver_streams(oc, filename) < 0) {
2191 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2195 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2196 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2198 /* disable if no corresponding type found and at least one
2200 if (nb_input_files > 0) {
2201 check_audio_video_inputs(&input_has_video, &input_has_audio);
2202 if (!input_has_video)
2204 if (!input_has_audio)
2208 /* manual disable */
2209 if (audio_disable) {
2212 if (video_disable) {
2218 AVCodecContext *video_enc;
2220 st = av_mallocz(sizeof(AVStream));
2222 fprintf(stderr, "Could not alloc stream\n");
2225 avcodec_get_context_defaults(&st->codec);
2227 video_enc = &st->codec;
2228 if (video_stream_copy) {
2229 st->stream_copy = 1;
2230 video_enc->codec_type = CODEC_TYPE_VIDEO;
2235 codec_id = file_oformat->video_codec;
2236 if (video_codec_id != CODEC_ID_NONE)
2237 codec_id = video_codec_id;
2239 video_enc->codec_id = codec_id;
2241 video_enc->bit_rate = video_bit_rate;
2242 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2243 video_enc->frame_rate = frame_rate;
2245 video_enc->width = frame_width;
2246 video_enc->height = frame_height;
2249 video_enc->gop_size = gop_size;
2251 video_enc->gop_size = 0;
2252 if (video_qscale || same_quality) {
2253 video_enc->flags |= CODEC_FLAG_QSCALE;
2254 st->quality = video_qscale;
2258 video_enc->flags |= CODEC_FLAG_BITEXACT;
2261 video_enc->flags |= CODEC_FLAG_HQ;
2265 video_enc->flags |= CODEC_FLAG_H263P_UMV;
2268 video_enc->flags |= CODEC_FLAG_H263P_AIC;
2272 video_enc->flags |= CODEC_FLAG_HQ;
2273 video_enc->flags |= CODEC_FLAG_4MV;
2277 video_enc->flags |= CODEC_FLAG_PART;
2281 video_enc->max_b_frames = b_frames;
2282 video_enc->b_frame_strategy = 0;
2283 video_enc->b_quant_factor = 2.0;
2286 video_enc->qmin = video_qmin;
2287 video_enc->qmax = video_qmax;
2288 video_enc->mb_qmin = video_mb_qmin;
2289 video_enc->mb_qmax = video_mb_qmax;
2290 video_enc->max_qdiff = video_qdiff;
2291 video_enc->qblur = video_qblur;
2292 video_enc->qcompress = video_qcomp;
2293 video_enc->rc_eq = video_rc_eq;
2294 video_enc->debug= debug;
2296 p= video_rc_override_string;
2299 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2301 fprintf(stderr, "error parsing rc_override\n");
2304 video_enc->rc_override=
2305 av_realloc(video_enc->rc_override,
2306 sizeof(RcOverride)*(i+1));
2307 video_enc->rc_override[i].start_frame= start;
2308 video_enc->rc_override[i].end_frame = end;
2310 video_enc->rc_override[i].qscale= q;
2311 video_enc->rc_override[i].quality_factor= 1.0;
2314 video_enc->rc_override[i].qscale= 0;
2315 video_enc->rc_override[i].quality_factor= -q/100.0;
2320 video_enc->rc_override_count=i;
2322 video_enc->rc_max_rate = video_rc_max_rate;
2323 video_enc->rc_min_rate = video_rc_min_rate;
2324 video_enc->rc_buffer_size = video_rc_buffer_size;
2325 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2326 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2327 video_enc->i_quant_factor = video_i_qfactor;
2328 video_enc->b_quant_factor = video_b_qfactor;
2329 video_enc->i_quant_offset = video_i_qoffset;
2330 video_enc->b_quant_offset = video_b_qoffset;
2331 video_enc->dct_algo = dct_algo;
2332 video_enc->idct_algo = idct_algo;
2333 video_enc->strict_std_compliance = strict;
2335 video_enc->rtp_mode= 1;
2336 video_enc->rtp_payload_size= packet_size;
2340 video_enc->flags|= CODEC_FLAG_PSNR;
2342 video_enc->me_method = me_method;
2347 video_enc->flags |= CODEC_FLAG_PASS1;
2349 video_enc->flags |= CODEC_FLAG_PASS2;
2353 oc->streams[nb_streams] = st;
2358 AVCodecContext *audio_enc;
2360 st = av_mallocz(sizeof(AVStream));
2362 fprintf(stderr, "Could not alloc stream\n");
2365 avcodec_get_context_defaults(&st->codec);
2367 audio_enc = &st->codec;
2368 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2369 if (audio_stream_copy) {
2370 st->stream_copy = 1;
2372 codec_id = file_oformat->audio_codec;
2373 if (audio_codec_id != CODEC_ID_NONE)
2374 codec_id = audio_codec_id;
2375 audio_enc->codec_id = codec_id;
2377 audio_enc->bit_rate = audio_bit_rate;
2378 audio_enc->sample_rate = audio_sample_rate;
2379 /* For audio codecs other than AC3 we limit */
2380 /* the number of coded channels to stereo */
2381 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2382 audio_enc->channels = 2;
2384 audio_enc->channels = audio_channels;
2386 oc->streams[nb_streams] = st;
2390 oc->nb_streams = nb_streams;
2393 fprintf(stderr, "No audio or video streams available\n");
2398 pstrcpy(oc->title, sizeof(oc->title), str_title);
2400 pstrcpy(oc->author, sizeof(oc->author), str_author);
2402 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2404 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2407 output_files[nb_output_files++] = oc;
2409 strcpy(oc->filename, filename);
2411 /* check filename in case of an image number is expected */
2412 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2413 if (filename_number_test(oc->filename) < 0) {
2414 print_error(oc->filename, AVERROR_NUMEXPECTED);
2419 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2420 /* test if it already exists to avoid loosing precious files */
2421 if (!file_overwrite &&
2422 (strchr(filename, ':') == NULL ||
2423 strstart(filename, "file:", NULL))) {
2424 if (url_exist(filename)) {
2427 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2430 if (toupper(c) != 'Y') {
2431 fprintf(stderr, "Not overwriting - exiting\n");
2438 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2439 fprintf(stderr, "Could not open '%s'\n", filename);
2444 memset(ap, 0, sizeof(*ap));
2445 ap->image_format = image_format;
2446 if (av_set_parameters(oc, ap) < 0) {
2447 fprintf(stderr, "%s: Invalid encoding parameters\n",
2452 /* reset some options */
2453 file_oformat = NULL;
2454 file_iformat = NULL;
2455 image_format = NULL;
2458 audio_codec_id = CODEC_ID_NONE;
2459 video_codec_id = CODEC_ID_NONE;
2460 audio_stream_copy = 0;
2461 video_stream_copy = 0;
2464 /* prepare dummy protocols for grab */
2465 static void prepare_grab(void)
2467 int has_video, has_audio, i, j;
2468 AVFormatContext *oc;
2469 AVFormatContext *ic;
2470 AVFormatParameters vp1, *vp = &vp1;
2471 AVFormatParameters ap1, *ap = &ap1;
2473 /* see if audio/video inputs are needed */
2476 memset(ap, 0, sizeof(*ap));
2477 memset(vp, 0, sizeof(*vp));
2478 for(j=0;j<nb_output_files;j++) {
2479 oc = output_files[j];
2480 for(i=0;i<oc->nb_streams;i++) {
2481 AVCodecContext *enc = &oc->streams[i]->codec;
2482 switch(enc->codec_type) {
2483 case CODEC_TYPE_AUDIO:
2484 if (enc->sample_rate > ap->sample_rate)
2485 ap->sample_rate = enc->sample_rate;
2486 if (enc->channels > ap->channels)
2487 ap->channels = enc->channels;
2490 case CODEC_TYPE_VIDEO:
2491 if (enc->width > vp->width)
2492 vp->width = enc->width;
2493 if (enc->height > vp->height)
2494 vp->height = enc->height;
2495 if (enc->frame_rate > vp->frame_rate)
2496 vp->frame_rate = enc->frame_rate;
2505 if (has_video == 0 && has_audio == 0) {
2506 fprintf(stderr, "Output file must have at least one audio or video stream\n");
2511 AVInputFormat *fmt1;
2512 fmt1 = av_find_input_format(video_grab_format);
2513 vp->device = video_device;
2514 vp->channel = video_channel;
2515 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
2516 fprintf(stderr, "Could not find video grab device\n");
2519 /* by now video grab has one stream */
2520 ic->streams[0]->r_frame_rate = vp->frame_rate;
2521 input_files[nb_input_files] = ic;
2522 dump_format(ic, nb_input_files, "", 0);
2525 if (has_audio && audio_grab_format) {
2526 AVInputFormat *fmt1;
2527 fmt1 = av_find_input_format(audio_grab_format);
2528 ap->device = audio_device;
2529 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2530 fprintf(stderr, "Could not find audio grab device\n");
2533 input_files[nb_input_files] = ic;
2534 dump_format(ic, nb_input_files, "", 0);
2539 /* open the necessary output devices for playing */
2540 static void prepare_play(void)
2542 int has_video, has_audio;
2544 check_audio_video_inputs(&has_video, &has_audio);
2546 /* manual disable */
2547 if (audio_disable) {
2550 if (video_disable) {
2555 file_oformat = guess_format("audio_device", NULL, NULL);
2556 if (!file_oformat) {
2557 fprintf(stderr, "Could not find audio device\n");
2560 opt_output_file(audio_device?audio_device:"/dev/dsp");
2564 file_oformat = guess_format("framebuffer_device", NULL, NULL);
2565 if (!file_oformat) {
2566 fprintf(stderr, "Could not find framebuffer device\n");
2569 opt_output_file("");
2573 /* same option as mencoder */
2574 static void opt_pass(const char *pass_str)
2577 pass = atoi(pass_str);
2578 if (pass != 1 && pass != 2) {
2579 fprintf(stderr, "pass number can be only 1 or 2\n");
2585 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
2586 static int64_t getutime(void)
2588 return av_gettime();
2591 static int64_t getutime(void)
2593 struct rusage rusage;
2595 getrusage(RUSAGE_SELF, &rusage);
2596 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2600 extern int ffm_nopts;
2602 static void opt_bitexact(void)
2605 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2609 static void show_formats(void)
2611 AVInputFormat *ifmt;
2612 AVOutputFormat *ofmt;
2613 AVImageFormat *image_fmt;
2618 printf("Output audio/video file formats:");
2619 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2620 printf(" %s", ofmt->name);
2624 printf("Input audio/video file formats:");
2625 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2626 printf(" %s", ifmt->name);
2630 printf("Output image formats:");
2631 for(image_fmt = first_image_format; image_fmt != NULL;
2632 image_fmt = image_fmt->next) {
2633 if (image_fmt->img_write)
2634 printf(" %s", image_fmt->name);
2638 printf("Input image formats:");
2639 for(image_fmt = first_image_format; image_fmt != NULL;
2640 image_fmt = image_fmt->next) {
2641 if (image_fmt->img_read)
2642 printf(" %s", image_fmt->name);
2646 printf("Codecs:\n");
2647 printf(" Encoders:");
2648 for(p = first_avcodec; p != NULL; p = p->next) {
2650 printf(" %s", p->name);
2654 printf(" Decoders:");
2655 for(p = first_avcodec; p != NULL; p = p->next) {
2657 printf(" %s", p->name);
2661 printf("Supported file protocols:");
2662 for(up = first_protocol; up != NULL; up = up->next)
2663 printf(" %s:", up->name);
2666 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2667 printf("Motion estimation methods:");
2671 if ((pp - motion_str + 1) == ME_ZERO)
2672 printf("(fastest)");
2673 else if ((pp - motion_str + 1) == ME_FULL)
2674 printf("(slowest)");
2675 else if ((pp - motion_str + 1) == ME_EPZS)
2676 printf("(default)");
2683 void show_help(void)
2686 const OptionDef *po;
2689 prog = do_play ? "ffplay" : "ffmpeg";
2691 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2695 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2696 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2698 printf("usage: ffplay [options] input_file...\n"
2699 "Simple audio player\n");
2703 "Main options are:\n");
2706 printf("\nAdvanced options are:\n");
2707 for(po = options; po->name != NULL; po++) {
2709 expert = (po->flags & OPT_EXPERT) != 0;
2711 strcpy(buf, po->name);
2712 if (po->flags & HAS_ARG) {
2714 strcat(buf, po->argname);
2716 printf("-%-17s %s\n", buf, po->help);
2724 const OptionDef options[] = {
2725 { "L", 0, {(void*)show_licence}, "show license" },
2726 { "h", 0, {(void*)show_help}, "show help" },
2727 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2728 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2729 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
2730 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2731 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2732 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2733 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2734 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2735 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2736 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2737 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2738 { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
2739 { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
2741 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2742 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2743 { "re", OPT_BOOL|OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate" },
2744 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2745 { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
2746 { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
2747 { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
2748 { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
2749 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2750 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2751 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2752 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2753 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2754 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2755 { "mbqmin", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
2756 { "mbqmax", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
2757 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2758 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2759 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2760 { "rc_init_cplx", HAS_ARG | OPT_EXPERT, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
2761 { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
2762 { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
2763 { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
2764 { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
2765 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
2766 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
2767 { "rc_override", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_override_string}, "Rate control override", "qualities for specific intervals" },
2768 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2769 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2770 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2771 { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
2772 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2773 { "vc", HAS_ARG | OPT_EXPERT, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
2774 { "dv1394", OPT_EXPERT, {(void*)opt_dv1394}, "set DV1394 grab", "" },
2775 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
2776 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2778 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2779 { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo", "algo" },
2780 { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
2781 { "ec", HAS_ARG | OPT_EXPERT, {(void*)opt_error_concealment}, "set error concealment", "" },
2782 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2783 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2784 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2785 { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
2786 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2787 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
2788 { "strict", HAS_ARG | OPT_EXPERT, {(void*)opt_strict}, "strictness", "how strictly to follow the standarts" },
2789 { "sameq", OPT_BOOL, {(void*)&same_quality},
2790 "use same video quality as source (implies VBR)" },
2791 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
2793 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2794 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2795 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2796 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2797 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2798 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
2799 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2800 "deinterlace pictures" },
2801 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2802 "add timings for benchmarking" },
2803 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2804 "dump each input packet" },
2805 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2806 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2807 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2808 { "vhook", HAS_ARG | OPT_EXPERT, {(void*)add_frame_hooker}, "insert video processing module", "module name and parameters" },
2810 { "aic", OPT_BOOL | OPT_EXPERT, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
2811 { "umv", OPT_BOOL | OPT_EXPERT, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
2816 int main(int argc, char **argv)
2819 const char *opt, *arg;
2820 const OptionDef *po;
2825 /* detect if invoked as player */
2826 i = strlen(argv[0]);
2827 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2835 while (optindex < argc) {
2836 opt = argv[optindex++];
2838 if (opt[0] == '-' && opt[1] != '\0') {
2840 while (po->name != NULL) {
2841 if (!strcmp(opt + 1, po->name))
2846 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2850 if (po->flags & HAS_ARG) {
2851 arg = argv[optindex++];
2853 fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
2857 if (po->flags & OPT_STRING) {
2859 str = av_strdup(arg);
2860 *po->u.str_arg = str;
2861 } else if (po->flags & OPT_BOOL) {
2864 po->u.func_arg(arg);
2868 opt_output_file(opt);
2870 opt_input_file(opt);
2877 /* file converter / grab */
2878 if (nb_output_files <= 0) {
2879 fprintf(stderr, "Must supply at least one output file\n");
2883 if (nb_input_files == 0) {
2888 if (nb_input_files <= 0) {
2889 fprintf(stderr, "Must supply at least one input file\n");
2896 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2897 stream_maps, nb_stream_maps);
2898 ti = getutime() - ti;
2900 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2904 for(i=0;i<nb_output_files;i++) {
2905 /* maybe av_close_output_file ??? */
2906 AVFormatContext *s = output_files[i];
2908 if (!(s->oformat->flags & AVFMT_NOFILE))
2910 for(j=0;j<s->nb_streams;j++)
2911 av_free(s->streams[j]);
2914 for(i=0;i<nb_input_files;i++)
2915 av_close_input_file(input_files[i]);
2920 #ifdef POWERPC_TBL_PERFORMANCE_REPORT
2921 extern void powerpc_display_perf_report(void);
2922 powerpc_display_perf_report();
2923 #endif /* POWERPC_TBL_PERFORMANCE_REPORT */