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"
24 #include "os_support.h"
29 #include <sys/ioctl.h>
32 #include <sys/resource.h>
36 #include <sys/types.h>
37 #include <sys/select.h>
43 #if !defined(INFINITY) && defined(HUGE_VAL)
44 #define INFINITY HUGE_VAL
47 #define MAXINT64 int64_t_C(0x7fffffffffffffff)
52 #define HAS_ARG 0x0001
53 #define OPT_BOOL 0x0002
54 #define OPT_EXPERT 0x0004
55 #define OPT_STRING 0x0008
57 void (*func_arg)(const char *);
65 /* select an input stream for an output stream */
66 typedef struct AVStreamMap {
71 extern const OptionDef options[];
77 static AVFormatContext *input_files[MAX_FILES];
78 static int nb_input_files = 0;
80 static AVFormatContext *output_files[MAX_FILES];
81 static int nb_output_files = 0;
83 static AVStreamMap stream_maps[MAX_FILES];
84 static int nb_stream_maps;
86 static AVInputFormat *file_iformat;
87 static AVOutputFormat *file_oformat;
88 static AVImageFormat *image_format;
89 static int frame_width = 160;
90 static int frame_height = 128;
91 static int frame_topBand = 0;
92 static int frame_bottomBand = 0;
93 static int frame_leftBand = 0;
94 static int frame_rightBand = 0;
95 static int frame_rate = 25;
96 static int frame_rate_base = 1;
97 static int video_bit_rate = 200*1000;
98 static int video_bit_rate_tolerance = 4000*1000;
99 static int video_qscale = 0;
100 static int video_qmin = 2;
101 static int video_qmax = 31;
102 static int video_mb_qmin = 2;
103 static int video_mb_qmax = 31;
104 static int video_qdiff = 3;
105 static float video_qblur = 0.5;
106 static float video_qcomp = 0.5;
107 #if 0 //experimental, (can be removed)
108 static float video_rc_qsquish=1.0;
109 static float video_rc_qmod_amp=0;
110 static int video_rc_qmod_freq=0;
112 static char *video_rc_override_string=NULL;
113 static char *video_rc_eq="tex^qComp";
114 static int video_rc_buffer_size=0;
115 static float video_rc_buffer_aggressivity=1.0;
116 static int video_rc_max_rate=0;
117 static int video_rc_min_rate=0;
118 static float video_rc_initial_cplx=0;
119 static float video_b_qfactor = 1.25;
120 static float video_b_qoffset = 1.25;
121 static float video_i_qfactor = -0.8;
122 static float video_i_qoffset = 0.0;
123 static int me_method = ME_EPZS;
124 static int video_disable = 0;
125 static int video_codec_id = CODEC_ID_NONE;
126 static int same_quality = 0;
127 static int b_frames = 0;
128 static int use_hq = 0;
129 static int use_4mv = 0;
131 static int use_aic = 0;
132 static int use_umv = 0;
134 static int use_h263p_extra = 0;
135 static int do_deinterlace = 0;
136 static int workaround_bugs = FF_BUG_AUTODETECT;
137 static int error_resilience = 2;
138 static int error_concealment = 3;
139 static int dct_algo = 0;
140 static int idct_algo = 0;
141 static int use_part = 0;
142 static int packet_size = 0;
143 static int strict = 0;
144 static int debug = 0;
146 static int gop_size = 12;
147 static int intra_only = 0;
148 static int audio_sample_rate = 44100;
149 static int audio_bit_rate = 64000;
150 static int audio_disable = 0;
151 static int audio_channels = 1;
152 static int audio_codec_id = CODEC_ID_NONE;
154 static int64_t recording_time = 0;
155 static int file_overwrite = 0;
156 static char *str_title = NULL;
157 static char *str_author = NULL;
158 static char *str_copyright = NULL;
159 static char *str_comment = NULL;
160 static int do_benchmark = 0;
161 static int do_hex_dump = 0;
162 static int do_play = 0;
163 static int do_psnr = 0;
164 static int do_vstats = 0;
165 static int do_pass = 0;
166 static int bitexact = 0;
167 static char *pass_logfilename = NULL;
168 static int audio_stream_copy = 0;
169 static int video_stream_copy = 0;
171 static int rate_emu = 0;
173 static char *video_grab_format = "video4linux";
174 static char *video_device = NULL;
175 static int video_channel = 0;
177 static char *audio_grab_format = "audio_device";
178 static char *audio_device = NULL;
180 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
182 typedef struct AVOutputStream {
183 int file_index; /* file index */
184 int index; /* stream index in the output file */
185 int source_index; /* AVInputStream index */
186 AVStream *st; /* stream in the output file */
187 int encoding_needed; /* true if encoding needed for this stream */
189 /* input pts and corresponding output pts
192 double sync_ipts_offset;
195 int video_resample; /* video_resample and video_crop are mutually exclusive */
196 AVPicture pict_tmp; /* temporary image for resampling */
197 ImgReSampleContext *img_resample_ctx; /* for image resampling */
199 int video_crop; /* video_resample and video_crop are mutually exclusive */
200 int topBand; /* cropping area sizes */
205 ReSampleContext *resample; /* for audio resampling */
206 FifoBuffer fifo; /* for compression: one audio fifo per codec */
210 typedef struct AVInputStream {
214 int discard; /* true if stream data should be discarded */
215 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
216 int64_t sample_index; /* current sample */
217 int frame_decoded; /* true if a video or audio frame has been decoded */
219 int64_t start; /* time when read started */
220 unsigned long frame; /* current frame */
223 typedef struct AVInputFile {
224 int eof_reached; /* true if eof reached */
225 int ist_index; /* index of first stream in ist_table */
226 int buffer_size; /* current total buffer size */
227 int buffer_size_max; /* buffer size at which we consider we can stop
229 int nb_streams; /* nb streams we are aware of */
234 /* init terminal so that we can grab keys */
235 static struct termios oldtty;
237 static void term_exit(void)
239 tcsetattr (0, TCSANOW, &oldtty);
242 static volatile sig_atomic_t received_sigterm = 0;
245 sigterm_handler(int sig)
247 received_sigterm = sig;
251 static void term_init(void)
258 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
259 |INLCR|IGNCR|ICRNL|IXON);
260 tty.c_oflag |= OPOST;
261 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
262 tty.c_cflag &= ~(CSIZE|PARENB);
267 tcsetattr (0, TCSANOW, &tty);
269 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
270 signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
271 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
273 register a function to be called at normal program termination
276 #ifdef CONFIG_BEOS_NETSERVER
277 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
281 /* read a key without blocking */
282 static int read_key(void)
286 #ifndef CONFIG_BEOS_NETSERVER
294 n = select(1, &rfds, NULL, NULL, &tv);
308 /* no interactive support */
309 static void term_exit(void)
313 static void term_init(void)
317 static int read_key(void)
324 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
329 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
332 /* copy stream format */
333 s->nb_streams = ic->nb_streams;
334 for(i=0;i<ic->nb_streams;i++) {
337 st = av_mallocz(sizeof(AVFormatContext));
338 memcpy(st, ic->streams[i], sizeof(AVStream));
342 av_close_input_file(ic);
346 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
348 static void do_audio_out(AVFormatContext *s,
351 unsigned char *buf, int size)
354 static uint8_t *audio_buf = NULL;
355 static uint8_t *audio_out = NULL;
356 const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
358 int size_out, frame_bytes, ret;
361 /* SC: dynamic allocation of buffers */
363 audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
365 audio_out = av_malloc(audio_out_size);
366 if (!audio_buf || !audio_out)
367 return; /* Should signal an error ! */
370 enc = &ost->st->codec;
372 if (ost->audio_resample) {
374 size_out = audio_resample(ost->resample,
375 (short *)buftmp, (short *)buf,
376 size / (ist->st->codec.channels * 2));
377 size_out = size_out * enc->channels * 2;
383 /* now encode as many frames as possible */
384 if (enc->frame_size > 1) {
385 /* output resampled raw samples */
386 fifo_write(&ost->fifo, buftmp, size_out,
389 frame_bytes = enc->frame_size * 2 * enc->channels;
391 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
392 &ost->fifo.rptr) == 0) {
393 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
395 av_write_frame(s, ost->index, audio_out, ret);
398 /* output a pcm frame */
399 /* XXX: change encoding codec API to avoid this ? */
400 switch(enc->codec->id) {
401 case CODEC_ID_PCM_S16LE:
402 case CODEC_ID_PCM_S16BE:
403 case CODEC_ID_PCM_U16LE:
404 case CODEC_ID_PCM_U16BE:
407 size_out = size_out >> 1;
410 ret = avcodec_encode_audio(enc, audio_out, size_out,
412 av_write_frame(s, ost->index, audio_out, ret);
416 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
420 AVPicture picture_tmp;
423 dec = &ist->st->codec;
425 /* deinterlace : must be done before any resize */
426 if (do_deinterlace) {
429 /* create temporary picture */
430 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
431 buf = av_malloc(size);
435 picture2 = &picture_tmp;
436 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
438 if (avpicture_deinterlace(picture2, picture,
439 dec->pix_fmt, dec->width, dec->height) < 0) {
440 /* if error, do not deinterlace */
449 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
451 if (picture != picture2)
452 *picture = *picture2;
456 /* we begin to correct av delay at this threshold */
457 #define AV_DELAY_MAX 0.100
459 static void do_video_out(AVFormatContext *s,
462 AVPicture *in_picture,
463 int *frame_size, AVOutputStream *audio_sync)
465 int nb_frames, i, ret;
466 AVPicture *final_picture, *formatted_picture;
467 AVPicture picture_format_temp, picture_crop_temp;
468 static uint8_t *video_buffer;
469 uint8_t *buf = NULL, *buf1 = NULL;
470 AVCodecContext *enc, *dec;
472 #define VIDEO_BUFFER_SIZE (1024*1024)
474 enc = &ost->st->codec;
475 dec = &ist->st->codec;
477 /* by default, we output a single frame */
482 /* NOTE: the A/V sync is always done by considering the audio is
483 the master clock. It is suffisant for transcoding or playing,
484 but not for the general case */
486 /* compute the A-V delay and duplicate/remove frames if needed */
487 double adelta, vdelta, apts, vpts, av_delay;
489 if (audio_sync->sync_ipts != AV_NOPTS_VALUE &&
490 ost->sync_ipts != AV_NOPTS_VALUE) {
492 adelta = (double)(ost->st->pts.val - audio_sync->sync_opts) *
493 s->pts_num / s->pts_den;
494 apts = audio_sync->sync_ipts + adelta;
496 vdelta = (double)(ost->st->pts.val - ost->sync_opts) *
497 s->pts_num / s->pts_den;
498 vpts = ost->sync_ipts + vdelta;
500 av_delay = apts - vpts;
501 // printf("delay=%f\n", av_delay);
502 if (av_delay < -AV_DELAY_MAX)
504 else if (av_delay > AV_DELAY_MAX)
510 if (ost->sync_ipts != AV_NOPTS_VALUE) {
511 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
512 if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
513 if (vdelta < -AV_DELAY_MAX)
515 else if (vdelta > AV_DELAY_MAX)
518 ost->sync_ipts_offset -= vdelta;
519 if (!ost->sync_ipts_offset)
520 ost->sync_ipts_offset = 0.000001; /* one microsecond */
525 static char *action[] = { "drop frame", "copy frame", "dup frame" };
526 printf("Input PTS %12.6f, output PTS %12.6f: %s\n",
527 (double) ost->sync_ipts, (double) ost->st->pts.val * s->pts_num / s->pts_den,
537 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
541 /* convert pixel format if needed */
542 if (enc->pix_fmt != dec->pix_fmt) {
545 /* create temporary picture */
546 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
547 buf = av_malloc(size);
550 formatted_picture = &picture_format_temp;
551 avpicture_fill(formatted_picture, buf, enc->pix_fmt, dec->width, dec->height);
553 if (img_convert(formatted_picture, enc->pix_fmt,
554 in_picture, dec->pix_fmt,
555 dec->width, dec->height) < 0) {
556 fprintf(stderr, "pixel format conversion not handled\n");
560 formatted_picture = in_picture;
563 /* XXX: resampling could be done before raw format convertion in
564 some cases to go faster */
565 /* XXX: only works for YUV420P */
566 if (ost->video_resample) {
567 final_picture = &ost->pict_tmp;
568 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
569 } else if (ost->video_crop) {
570 picture_crop_temp.data[0] = formatted_picture->data[0] +
571 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
573 picture_crop_temp.data[1] = formatted_picture->data[1] +
574 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
575 (ost->leftBand >> 1);
577 picture_crop_temp.data[2] = formatted_picture->data[2] +
578 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
579 (ost->leftBand >> 1);
581 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
582 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
583 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
584 final_picture = &picture_crop_temp;
586 final_picture = formatted_picture;
588 /* duplicates frame if needed */
589 /* XXX: pb because no interleaving */
590 for(i=0;i<nb_frames;i++) {
591 if (s->oformat->flags & AVFMT_RAWPICTURE) {
592 /* raw pictures are written as AVPicture structure to
593 avoid any copies. We support temorarily the older
595 av_write_frame(s, ost->index,
596 (uint8_t *)final_picture, sizeof(AVPicture));
600 memset(&big_picture, 0, sizeof(AVFrame));
601 *(AVPicture*)&big_picture= *final_picture;
603 /* handles sameq here. This is not correct because it may
604 not be a global option */
606 big_picture.quality = ist->st->quality;
608 big_picture.quality = ost->st->quality;
610 ret = avcodec_encode_video(enc,
611 video_buffer, VIDEO_BUFFER_SIZE,
613 //enc->frame_number = enc->real_pict_num;
614 av_write_frame(s, ost->index, video_buffer, ret);
616 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
617 // enc->frame_number-1, enc->real_pict_num, ret,
619 /* if two pass, output log */
620 if (ost->logfile && enc->stats_out) {
621 fprintf(ost->logfile, "%s", enc->stats_out);
631 static double psnr(double d){
632 if(d==0) return INFINITY;
633 return -10.0*log(d)/log(10.0);
636 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
639 static FILE *fvstats=NULL;
640 static int64_t total_size = 0;
647 double ti1, bitrate, avg_bitrate;
651 today = localtime(&today2);
652 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
655 fvstats = fopen(filename,"w");
663 enc = &ost->st->codec;
664 total_size += frame_size;
665 if (enc->codec_type == CODEC_TYPE_VIDEO) {
666 frame_number = ost->frame_number;
667 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality);
668 if (enc->flags&CODEC_FLAG_PSNR)
669 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
671 fprintf(fvstats,"f_size= %6d ", frame_size);
672 /* compute pts value */
673 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
677 bitrate = (double)(frame_size * 8) * enc->frame_rate / enc->frame_rate_base / 1000.0;
678 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
679 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
680 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
681 fprintf(fvstats,"type= %s\n", enc->coded_frame->key_frame == 1 ? "I" : "P");
685 static void print_report(AVFormatContext **output_files,
686 AVOutputStream **ost_table, int nb_ostreams,
691 AVFormatContext *oc, *os;
694 int frame_number, vid, i;
695 double bitrate, ti1, pts;
696 static int64_t last_time = -1;
698 if (!is_last_report) {
700 /* display the report every 0.5 seconds */
701 cur_time = av_gettime();
702 if (last_time == -1) {
703 last_time = cur_time;
706 if ((cur_time - last_time) < 500000)
708 last_time = cur_time;
712 oc = output_files[0];
714 total_size = url_ftell(&oc->pb);
719 for(i=0;i<nb_ostreams;i++) {
721 os = output_files[ost->file_index];
722 enc = &ost->st->codec;
723 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
724 sprintf(buf + strlen(buf), "q=%2.1f ",
725 enc->coded_frame->quality);
727 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
728 frame_number = ost->frame_number;
729 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
730 frame_number, enc->coded_frame ? enc->coded_frame->quality : 0);
731 if (enc->flags&CODEC_FLAG_PSNR)
732 sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
735 /* compute min output value */
736 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
737 if ((pts < ti1) && (pts > 0))
742 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
744 sprintf(buf + strlen(buf),
745 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
746 (double)total_size / 1024, ti1, bitrate);
748 fprintf(stderr, "%s ", buf);
750 if (is_last_report) {
751 fprintf(stderr, "\n");
753 fprintf(stderr, "\r");
759 * The following code is the main loop of the file converter
761 static int av_encode(AVFormatContext **output_files,
763 AVFormatContext **input_files,
765 AVStreamMap *stream_maps, int nb_stream_maps)
767 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0, pts_set;
768 AVFormatContext *is, *os;
769 AVCodecContext *codec, *icodec;
770 AVOutputStream *ost, **ost_table = NULL;
771 AVInputStream *ist, **ist_table = NULL;
772 AVInputFile *file_table;
773 AVFormatContext *stream_no_data;
776 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
780 /* input stream init */
782 for(i=0;i<nb_input_files;i++) {
784 file_table[i].ist_index = j;
785 file_table[i].nb_streams = is->nb_streams;
790 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
794 for(i=0;i<nb_istreams;i++) {
795 ist = av_mallocz(sizeof(AVInputStream));
801 for(i=0;i<nb_input_files;i++) {
803 for(k=0;k<is->nb_streams;k++) {
804 ist = ist_table[j++];
805 ist->st = is->streams[k];
808 ist->discard = 1; /* the stream is discarded by default
811 if (ist->st->codec.rate_emu) {
812 ist->start = av_gettime();
818 /* output stream init */
820 for(i=0;i<nb_output_files;i++) {
821 os = output_files[i];
822 nb_ostreams += os->nb_streams;
824 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
825 fprintf(stderr, "Number of stream maps must match number of output streams\n");
829 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
832 for(i=0;i<nb_ostreams;i++) {
833 ost = av_mallocz(sizeof(AVOutputStream));
840 for(k=0;k<nb_output_files;k++) {
841 os = output_files[k];
842 for(i=0;i<os->nb_streams;i++) {
844 ost = ost_table[n++];
847 ost->st = os->streams[i];
848 if (nb_stream_maps > 0) {
849 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
850 stream_maps[n-1].stream_index;
852 /* get corresponding input stream index : we select the first one with the right type */
854 for(j=0;j<nb_istreams;j++) {
857 ist->st->codec.codec_type == ost->st->codec.codec_type) {
858 ost->source_index = j;
864 /* try again and reuse existing stream */
865 for(j=0;j<nb_istreams;j++) {
867 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
868 ost->source_index = j;
873 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
874 ost->file_index, ost->index);
879 ist = ist_table[ost->source_index];
884 /* for each output stream, we compute the right encoding parameters */
885 for(i=0;i<nb_ostreams;i++) {
887 ist = ist_table[ost->source_index];
889 codec = &ost->st->codec;
890 icodec = &ist->st->codec;
892 if (ost->st->stream_copy) {
893 /* if stream_copy is selected, no need to decode or encode */
894 codec->codec_id = icodec->codec_id;
895 codec->codec_type = icodec->codec_type;
896 codec->codec_tag = icodec->codec_tag;
897 codec->bit_rate = icodec->bit_rate;
898 switch(codec->codec_type) {
899 case CODEC_TYPE_AUDIO:
900 codec->sample_rate = icodec->sample_rate;
901 codec->channels = icodec->channels;
903 case CODEC_TYPE_VIDEO:
904 codec->frame_rate = icodec->frame_rate;
905 codec->frame_rate_base = icodec->frame_rate_base;
906 codec->width = icodec->width;
907 codec->height = icodec->height;
913 switch(codec->codec_type) {
914 case CODEC_TYPE_AUDIO:
915 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
918 if (codec->channels == icodec->channels &&
919 codec->sample_rate == icodec->sample_rate) {
920 ost->audio_resample = 0;
922 if (codec->channels != icodec->channels &&
923 icodec->codec_id == CODEC_ID_AC3) {
924 /* Special case for 5:1 AC3 input */
925 /* and mono or stereo output */
926 /* Request specific number of channels */
927 icodec->channels = codec->channels;
928 if (codec->sample_rate == icodec->sample_rate)
929 ost->audio_resample = 0;
931 ost->audio_resample = 1;
932 ost->resample = audio_resample_init(codec->channels, icodec->channels,
934 icodec->sample_rate);
936 /* Request specific number of channels */
937 icodec->channels = codec->channels;
939 ost->audio_resample = 1;
940 ost->resample = audio_resample_init(codec->channels, icodec->channels,
942 icodec->sample_rate);
945 ist->decoding_needed = 1;
946 ost->encoding_needed = 1;
948 case CODEC_TYPE_VIDEO:
949 if (codec->width == icodec->width &&
950 codec->height == icodec->height &&
951 frame_topBand == 0 &&
952 frame_bottomBand == 0 &&
953 frame_leftBand == 0 &&
954 frame_rightBand == 0)
956 ost->video_resample = 0;
958 } else if ((codec->width == icodec->width -
959 (frame_leftBand + frame_rightBand)) &&
960 (codec->height == icodec->height -
961 (frame_topBand + frame_bottomBand)))
963 ost->video_resample = 0;
965 ost->topBand = frame_topBand;
966 ost->leftBand = frame_leftBand;
969 ost->video_resample = 1;
970 ost->video_crop = 0; // cropping is handled as part of resample
971 buf = av_malloc((codec->width * codec->height * 3) / 2);
974 ost->pict_tmp.data[0] = buf;
975 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
976 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
977 ost->pict_tmp.linesize[0] = codec->width;
978 ost->pict_tmp.linesize[1] = codec->width / 2;
979 ost->pict_tmp.linesize[2] = codec->width / 2;
981 ost->img_resample_ctx = img_resample_full_init(
982 ost->st->codec.width, ost->st->codec.height,
983 ist->st->codec.width, ist->st->codec.height,
984 frame_topBand, frame_bottomBand,
985 frame_leftBand, frame_rightBand);
987 ost->encoding_needed = 1;
988 ist->decoding_needed = 1;
994 if (ost->encoding_needed &&
995 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
996 char logfilename[1024];
1001 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1003 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1004 if (codec->flags & CODEC_FLAG_PASS1) {
1005 f = fopen(logfilename, "w");
1007 perror(logfilename);
1012 /* read the log file */
1013 f = fopen(logfilename, "r");
1015 perror(logfilename);
1018 fseek(f, 0, SEEK_END);
1020 fseek(f, 0, SEEK_SET);
1021 logbuffer = av_malloc(size + 1);
1023 fprintf(stderr, "Could not allocate log buffer\n");
1026 fread(logbuffer, 1, size, f);
1028 logbuffer[size] = '\0';
1029 codec->stats_in = logbuffer;
1035 /* dump the file output parameters - cannot be done before in case
1037 for(i=0;i<nb_output_files;i++) {
1038 dump_format(output_files[i], i, output_files[i]->filename, 1);
1041 /* dump the stream mapping */
1042 fprintf(stderr, "Stream mapping:\n");
1043 for(i=0;i<nb_ostreams;i++) {
1045 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1046 ist_table[ost->source_index]->file_index,
1047 ist_table[ost->source_index]->index,
1052 /* open each encoder */
1053 for(i=0;i<nb_ostreams;i++) {
1055 if (ost->encoding_needed) {
1057 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1059 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1060 ost->file_index, ost->index);
1063 if (avcodec_open(&ost->st->codec, codec) < 0) {
1064 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1065 ost->file_index, ost->index);
1071 /* open each decoder */
1072 for(i=0;i<nb_istreams;i++) {
1074 if (ist->decoding_needed) {
1076 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1078 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1079 ist->st->codec.codec_id, ist->file_index, ist->index);
1082 if (avcodec_open(&ist->st->codec, codec) < 0) {
1083 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1084 ist->file_index, ist->index);
1087 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1088 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1089 ist->frame_decoded = 1;
1094 for(i=0;i<nb_istreams;i++) {
1098 /* compute buffer size max (should use a complete heuristic) */
1099 for(i=0;i<nb_input_files;i++) {
1100 file_table[i].buffer_size_max = 2048;
1103 /* open files and write file headers */
1104 for(i=0;i<nb_output_files;i++) {
1105 os = output_files[i];
1106 if (av_write_header(os) < 0) {
1107 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1113 #ifndef CONFIG_WIN32
1115 fprintf(stderr, "Press [q] to stop encoding\n");
1117 fprintf(stderr, "Press [q] to stop playing\n");
1126 int file_index, ist_index;
1131 int data_size, got_picture;
1133 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1134 void *buffer_to_free;
1138 /* if 'q' pressed, exits */
1140 /* read_key() returns 0 on EOF */
1146 /* select the stream that we must read now by looking at the
1147 smallest output pts */
1150 for(i=0;i<nb_ostreams;i++) {
1153 os = output_files[ost->file_index];
1154 ist = ist_table[ost->source_index];
1155 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1156 if (!file_table[ist->file_index].eof_reached &&
1159 file_index = ist->file_index;
1162 /* if none, if is finished */
1163 if (file_index < 0) {
1167 /* finish if recording time exhausted */
1168 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1171 /* read a packet from it and output it in the fifo */
1172 is = input_files[file_index];
1173 if (av_read_packet(is, &pkt) < 0) {
1174 file_table[file_index].eof_reached = 1;
1178 stream_no_data = is;
1183 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1184 av_hex_dump(pkt.data, pkt.size);
1186 /* the following test is needed in case new streams appear
1187 dynamically in stream : we ignore them */
1188 if (pkt.stream_index >= file_table[file_index].nb_streams)
1189 goto discard_packet;
1190 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1191 ist = ist_table[ist_index];
1193 goto discard_packet;
1195 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1203 ipts = AV_NOPTS_VALUE;
1205 /* decode the packet if needed */
1206 data_buf = NULL; /* fail safe */
1208 if (ist->decoding_needed) {
1209 /* NOTE1: we only take into account the PTS if a new
1210 frame has begun (MPEG semantics) */
1211 /* NOTE2: even if the fraction is not initialized,
1212 av_frac_set can be used to set the integer part */
1213 if (ist->frame_decoded &&
1214 pkt.pts != AV_NOPTS_VALUE &&
1217 ist->frame_decoded = 0;
1221 switch(ist->st->codec.codec_type) {
1222 case CODEC_TYPE_AUDIO:
1223 /* XXX: could avoid copy if PCM 16 bits with same
1224 endianness as CPU */
1225 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1229 /* Some bug in mpeg audio decoder gives */
1230 /* data_size < 0, it seems they are overflows */
1231 if (data_size <= 0) {
1232 /* no audio frame */
1237 data_buf = (uint8_t *)samples;
1239 case CODEC_TYPE_VIDEO:
1241 AVFrame big_picture;
1243 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1244 ret = avcodec_decode_video(&ist->st->codec,
1245 &big_picture, &got_picture, ptr, len);
1246 picture= *(AVPicture*)&big_picture;
1247 ist->st->quality= big_picture.quality;
1250 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1251 ist->file_index, ist->index);
1252 av_free_packet(&pkt);
1256 /* no picture yet */
1276 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1277 pre_process_video_frame(ist, &picture, &buffer_to_free);
1280 ist->frame_decoded = 1;
1282 /* frame rate emulation */
1283 if (ist->st->codec.rate_emu) {
1284 int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate);
1285 int64_t now = av_gettime() - ist->start;
1293 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1294 is the one of the next displayed one */
1295 /* XXX: add mpeg4 too ? */
1296 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1297 if (ist->st->codec.pict_type != B_TYPE) {
1299 tmp = ist->last_ip_pts;
1300 ist->last_ip_pts = ist->frac_pts.val;
1301 ist->frac_pts.val = tmp;
1305 /* transcode raw format, encode packets and output them */
1307 for(i=0;i<nb_ostreams;i++) {
1311 if (ost->source_index == ist_index) {
1312 os = output_files[ost->file_index];
1314 if (ipts != AV_NOPTS_VALUE) {
1316 printf("%d: got pts=%f %f\n",
1317 i, pkt.pts / 90000.0,
1318 (ipts - ost->st->pts.val) / 90000.0);
1320 /* set the input output pts pairs */
1321 ost->sync_ipts = (double)ipts * is->pts_num /
1323 /* XXX: take into account the various fifos,
1324 in particular for audio */
1325 ost->sync_opts = ost->st->pts.val;
1326 //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);
1328 //printf("pts.val=%lld\n", ost->st->pts.val);
1329 ost->sync_ipts = AV_NOPTS_VALUE;
1332 if (ost->encoding_needed) {
1333 switch(ost->st->codec.codec_type) {
1334 case CODEC_TYPE_AUDIO:
1335 do_audio_out(os, ost, ist, data_buf, data_size);
1337 case CODEC_TYPE_VIDEO:
1338 /* find an audio stream for synchro */
1341 AVOutputStream *audio_sync, *ost1;
1343 for(i=0;i<nb_ostreams;i++) {
1344 ost1 = ost_table[i];
1345 if (ost1->file_index == ost->file_index &&
1346 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1352 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1353 if (do_vstats && frame_size)
1354 do_video_stats(os, ost, frame_size);
1363 /* no reencoding needed : output the packet directly */
1364 /* force the input stream PTS */
1366 //XXX/FIXME set keyframe flag from demuxer (or optionally from decoder)
1367 memset(&avframe, 0, sizeof(AVFrame));
1368 ost->st->codec.coded_frame= &avframe;
1370 av_write_frame(os, ost->index, data_buf, data_size);
1371 ost->st->codec.frame_number++;
1372 ost->frame_number++;
1376 av_free(buffer_to_free);
1377 ipts = AV_NOPTS_VALUE;
1380 av_free_packet(&pkt);
1382 /* dump report by using the output first video and audio streams */
1383 print_report(output_files, ost_table, nb_ostreams, 0);
1387 /* dump report by using the first video and audio streams */
1388 print_report(output_files, ost_table, nb_ostreams, 1);
1390 /* close each encoder */
1391 for(i=0;i<nb_ostreams;i++) {
1393 if (ost->encoding_needed) {
1394 av_freep(&ost->st->codec.stats_in);
1395 avcodec_close(&ost->st->codec);
1399 /* close each decoder */
1400 for(i=0;i<nb_istreams;i++) {
1402 if (ist->decoding_needed) {
1403 avcodec_close(&ist->st->codec);
1408 /* write the trailer if needed and close file */
1409 for(i=0;i<nb_output_files;i++) {
1410 os = output_files[i];
1411 av_write_trailer(os);
1417 av_free(file_table);
1420 for(i=0;i<nb_istreams;i++) {
1427 for(i=0;i<nb_ostreams;i++) {
1431 fclose(ost->logfile);
1432 ost->logfile = NULL;
1434 fifo_free(&ost->fifo); /* works even if fifo is not
1435 initialized but set to zero */
1436 av_free(ost->pict_tmp.data[0]);
1437 if (ost->video_resample)
1438 img_resample_close(ost->img_resample_ctx);
1439 if (ost->audio_resample)
1440 audio_resample_close(ost->resample);
1453 int file_read(const char *filename)
1456 unsigned char buffer[1024];
1459 if (url_open(&h, filename, O_RDONLY) < 0) {
1460 printf("could not open '%s'\n", filename);
1464 len = url_read(h, buffer, sizeof(buffer));
1467 for(i=0;i<len;i++) putchar(buffer[i]);
1474 static void show_licence(void)
1477 "ffmpeg version " FFMPEG_VERSION "\n"
1478 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1479 "This library is free software; you can redistribute it and/or\n"
1480 "modify it under the terms of the GNU Lesser General Public\n"
1481 "License as published by the Free Software Foundation; either\n"
1482 "version 2 of the License, or (at your option) any later version.\n"
1484 "This library is distributed in the hope that it will be useful,\n"
1485 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1486 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1487 "Lesser General Public License for more details.\n"
1489 "You should have received a copy of the GNU Lesser General Public\n"
1490 "License along with this library; if not, write to the Free Software\n"
1491 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1496 static void opt_image_format(const char *arg)
1500 for(f = first_image_format; f != NULL; f = f->next) {
1501 if (!strcmp(arg, f->name))
1505 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1511 static void opt_format(const char *arg)
1513 /* compatibility stuff for pgmyuv */
1514 if (!strcmp(arg, "pgmyuv")) {
1515 opt_image_format(arg);
1519 file_iformat = av_find_input_format(arg);
1520 file_oformat = guess_format(arg, NULL, NULL);
1521 if (!file_iformat && !file_oformat) {
1522 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1527 static void opt_video_bitrate(const char *arg)
1529 video_bit_rate = atoi(arg) * 1000;
1532 static void opt_video_bitrate_tolerance(const char *arg)
1534 video_bit_rate_tolerance = atoi(arg) * 1000;
1537 static void opt_video_bitrate_max(const char *arg)
1539 video_rc_max_rate = atoi(arg) * 1000;
1542 static void opt_video_bitrate_min(const char *arg)
1544 video_rc_min_rate = atoi(arg) * 1000;
1547 static void opt_video_buffer_size(const char *arg)
1549 video_rc_buffer_size = atoi(arg) * 1000;
1552 static void opt_video_rc_eq(char *arg)
1557 static void opt_video_rc_override_string(char *arg)
1559 video_rc_override_string = arg;
1563 static void opt_workaround_bugs(const char *arg)
1565 workaround_bugs = atoi(arg);
1568 static void opt_dct_algo(const char *arg)
1570 dct_algo = atoi(arg);
1573 static void opt_idct_algo(const char *arg)
1575 idct_algo = atoi(arg);
1579 static void opt_error_resilience(const char *arg)
1581 error_resilience = atoi(arg);
1584 static void opt_error_concealment(const char *arg)
1586 error_concealment = atoi(arg);
1589 static void opt_debug(const char *arg)
1594 static void opt_frame_rate(const char *arg)
1596 frame_rate_base = DEFAULT_FRAME_RATE_BASE; //FIXME not optimal
1597 frame_rate = (int)(strtod(arg, 0) * frame_rate_base + 0.5);
1598 //FIXME parse fractions
1602 static void opt_frame_crop_top(const char *arg)
1604 frame_topBand = atoi(arg);
1605 if (frame_topBand < 0) {
1606 fprintf(stderr, "Incorrect top crop size\n");
1609 if ((frame_topBand % 2) != 0) {
1610 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1613 if ((frame_topBand) >= frame_height){
1614 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1617 frame_height -= frame_topBand;
1620 static void opt_frame_crop_bottom(const char *arg)
1622 frame_bottomBand = atoi(arg);
1623 if (frame_bottomBand < 0) {
1624 fprintf(stderr, "Incorrect bottom crop size\n");
1627 if ((frame_bottomBand % 2) != 0) {
1628 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1631 if ((frame_bottomBand) >= frame_height){
1632 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1635 frame_height -= frame_bottomBand;
1638 static void opt_frame_crop_left(const char *arg)
1640 frame_leftBand = atoi(arg);
1641 if (frame_leftBand < 0) {
1642 fprintf(stderr, "Incorrect left crop size\n");
1645 if ((frame_leftBand % 2) != 0) {
1646 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1649 if ((frame_leftBand) >= frame_width){
1650 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1653 frame_width -= frame_leftBand;
1656 static void opt_frame_crop_right(const char *arg)
1658 frame_rightBand = atoi(arg);
1659 if (frame_rightBand < 0) {
1660 fprintf(stderr, "Incorrect right crop size\n");
1663 if ((frame_rightBand % 2) != 0) {
1664 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1667 if ((frame_rightBand) >= frame_width){
1668 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1671 frame_width -= frame_rightBand;
1674 static void opt_frame_size(const char *arg)
1676 parse_image_size(&frame_width, &frame_height, arg);
1677 if (frame_width <= 0 || frame_height <= 0) {
1678 fprintf(stderr, "Incorrect frame size\n");
1681 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1682 fprintf(stderr, "Frame size must be a multiple of 2\n");
1687 static void opt_gop_size(const char *arg)
1689 gop_size = atoi(arg);
1692 static void opt_b_frames(const char *arg)
1694 b_frames = atoi(arg);
1695 if (b_frames > FF_MAX_B_FRAMES) {
1696 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1698 } else if (b_frames < 1) {
1699 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1704 static void opt_qscale(const char *arg)
1706 video_qscale = atoi(arg);
1707 if (video_qscale < 0 ||
1708 video_qscale > 31) {
1709 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1714 static void opt_qmin(const char *arg)
1716 video_qmin = atoi(arg);
1717 if (video_qmin < 0 ||
1719 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1724 static void opt_qmax(const char *arg)
1726 video_qmax = atoi(arg);
1727 if (video_qmax < 0 ||
1729 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1734 static void opt_mb_qmin(const char *arg)
1736 video_mb_qmin = atoi(arg);
1737 if (video_mb_qmin < 0 ||
1738 video_mb_qmin > 31) {
1739 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1744 static void opt_mb_qmax(const char *arg)
1746 video_mb_qmax = atoi(arg);
1747 if (video_mb_qmax < 0 ||
1748 video_mb_qmax > 31) {
1749 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1754 static void opt_qdiff(const char *arg)
1756 video_qdiff = atoi(arg);
1757 if (video_qdiff < 0 ||
1759 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1764 static void opt_qblur(const char *arg)
1766 video_qblur = atof(arg);
1769 static void opt_qcomp(const char *arg)
1771 video_qcomp = atof(arg);
1774 static void opt_rc_initial_cplx(const char *arg)
1776 video_rc_initial_cplx = atof(arg);
1778 static void opt_b_qfactor(const char *arg)
1780 video_b_qfactor = atof(arg);
1782 static void opt_i_qfactor(const char *arg)
1784 video_i_qfactor = atof(arg);
1786 static void opt_b_qoffset(const char *arg)
1788 video_b_qoffset = atof(arg);
1790 static void opt_i_qoffset(const char *arg)
1792 video_i_qoffset = atof(arg);
1795 static void opt_packet_size(const char *arg)
1797 packet_size= atoi(arg);
1800 static void opt_strict(const char *arg)
1805 static void opt_audio_bitrate(const char *arg)
1807 audio_bit_rate = atoi(arg) * 1000;
1810 static void opt_audio_rate(const char *arg)
1812 audio_sample_rate = atoi(arg);
1815 static void opt_audio_channels(const char *arg)
1817 audio_channels = atoi(arg);
1820 static void opt_video_device(const char *arg)
1822 video_device = av_strdup(arg);
1825 static void opt_video_channel(const char *arg)
1827 video_channel = strtol(arg, NULL, 0);
1830 static void opt_audio_device(const char *arg)
1832 audio_device = av_strdup(arg);
1835 static void opt_dv1394(const char *arg)
1837 video_grab_format = "dv1394";
1838 audio_grab_format = NULL;
1841 static void opt_audio_codec(const char *arg)
1845 if (!strcmp(arg, "copy")) {
1846 audio_stream_copy = 1;
1850 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1855 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1858 audio_codec_id = p->id;
1863 static void add_frame_hooker(const char *arg)
1868 char *args = av_strdup(arg);
1870 argv[0] = strtok(args, " ");
1871 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
1874 i = frame_hook_add(argc, argv);
1877 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
1882 const char *motion_str[] = {
1892 static void opt_motion_estimation(const char *arg)
1898 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1901 if (!strcmp(*p, arg))
1905 me_method = (p - motion_str) + 1;
1908 static void opt_video_codec(const char *arg)
1912 if (!strcmp(arg, "copy")) {
1913 video_stream_copy = 1;
1917 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1922 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1925 video_codec_id = p->id;
1930 static void opt_map(const char *arg)
1936 m = &stream_maps[nb_stream_maps++];
1938 m->file_index = strtol(arg, (char **)&p, 0);
1942 m->stream_index = strtol(p, (char **)&p, 0);
1945 static void opt_recording_time(const char *arg)
1947 recording_time = parse_date(arg, 1);
1950 static void print_error(const char *filename, int err)
1953 case AVERROR_NUMEXPECTED:
1954 fprintf(stderr, "%s: Incorrect image filename syntax.\n"
1955 "Use '%%d' to specify the image number:\n"
1956 " for img1.jpg, img2.jpg, ..., use 'img%%d.jpg';\n"
1957 " for img001.jpg, img002.jpg, ..., use 'img%%03d.jpg'.\n",
1960 case AVERROR_INVALIDDATA:
1961 fprintf(stderr, "%s: Error while parsing header\n", filename);
1964 fprintf(stderr, "%s: Unknown format\n", filename);
1967 fprintf(stderr, "%s: Error while opening file\n", filename);
1972 static void opt_input_file(const char *filename)
1974 AVFormatContext *ic;
1975 AVFormatParameters params, *ap = ¶ms;
1976 int err, i, ret, rfps, rfps_base;
1978 if (!strcmp(filename, "-"))
1981 /* get default parameters from command line */
1982 memset(ap, 0, sizeof(*ap));
1983 ap->sample_rate = audio_sample_rate;
1984 ap->channels = audio_channels;
1985 ap->frame_rate = frame_rate;
1986 ap->frame_rate_base = frame_rate_base;
1987 ap->width = frame_width;
1988 ap->height = frame_height;
1989 ap->image_format = image_format;
1991 /* open the input file with generic libav function */
1992 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
1994 print_error(filename, err);
1998 /* If not enough info to get the stream parameters, we decode the
1999 first frames to get it. (used in mpeg case for example) */
2000 ret = av_find_stream_info(ic);
2002 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2006 /* update the current parameters so that they match the one of the input stream */
2007 for(i=0;i<ic->nb_streams;i++) {
2008 AVCodecContext *enc = &ic->streams[i]->codec;
2009 switch(enc->codec_type) {
2010 case CODEC_TYPE_AUDIO:
2011 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2012 audio_channels = enc->channels;
2013 audio_sample_rate = enc->sample_rate;
2015 case CODEC_TYPE_VIDEO:
2016 frame_height = enc->height;
2017 frame_width = enc->width;
2018 rfps = ic->streams[i]->r_frame_rate;
2019 rfps_base = ic->streams[i]->r_frame_rate_base;
2020 enc->workaround_bugs = workaround_bugs;
2021 enc->error_resilience = error_resilience;
2022 enc->error_concealment = error_concealment;
2023 enc->idct_algo= idct_algo;
2025 /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
2026 enc->flags|= CODEC_FLAG_TRUNCATED; */
2027 if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO || enc->codec_id==CODEC_ID_H264)
2028 enc->flags|= CODEC_FLAG_TRUNCATED;
2031 enc->flags|= CODEC_FLAG_BITEXACT;
2033 assert(enc->frame_rate_base == rfps_base); // should be true for now
2034 if (enc->frame_rate != rfps) {
2035 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2036 i, (float)enc->frame_rate / enc->frame_rate_base,
2037 (float)rfps / rfps_base);
2039 /* update the current frame rate to match the stream frame rate */
2041 frame_rate_base = rfps_base;
2043 enc->rate_emu = rate_emu;
2050 input_files[nb_input_files] = ic;
2051 /* dump the file content */
2052 dump_format(ic, nb_input_files, filename, 0);
2054 file_iformat = NULL;
2055 file_oformat = NULL;
2056 image_format = NULL;
2061 static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2063 int has_video, has_audio, i, j;
2064 AVFormatContext *ic;
2068 for(j=0;j<nb_input_files;j++) {
2069 ic = input_files[j];
2070 for(i=0;i<ic->nb_streams;i++) {
2071 AVCodecContext *enc = &ic->streams[i]->codec;
2072 switch(enc->codec_type) {
2073 case CODEC_TYPE_AUDIO:
2076 case CODEC_TYPE_VIDEO:
2084 *has_video_ptr = has_video;
2085 *has_audio_ptr = has_audio;
2088 static void opt_output_file(const char *filename)
2091 AVFormatContext *oc;
2092 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2094 AVFormatParameters params, *ap = ¶ms;
2096 if (!strcmp(filename, "-"))
2099 oc = av_mallocz(sizeof(AVFormatContext));
2101 if (!file_oformat) {
2102 file_oformat = guess_format(NULL, filename, NULL);
2103 if (!file_oformat) {
2104 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2110 oc->oformat = file_oformat;
2112 if (!strcmp(file_oformat->name, "ffm") &&
2113 strstart(filename, "http:", NULL)) {
2114 /* special case for files sent to ffserver: we get the stream
2115 parameters from ffserver */
2116 if (read_ffserver_streams(oc, filename) < 0) {
2117 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2121 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2122 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2124 /* disable if no corresponding type found and at least one
2126 if (nb_input_files > 0) {
2127 check_audio_video_inputs(&input_has_video, &input_has_audio);
2128 if (!input_has_video)
2130 if (!input_has_audio)
2134 /* manual disable */
2135 if (audio_disable) {
2138 if (video_disable) {
2144 AVCodecContext *video_enc;
2146 st = av_mallocz(sizeof(AVStream));
2148 fprintf(stderr, "Could not alloc stream\n");
2151 avcodec_get_context_defaults(&st->codec);
2153 video_enc = &st->codec;
2154 if (video_stream_copy) {
2155 st->stream_copy = 1;
2156 video_enc->codec_type = CODEC_TYPE_VIDEO;
2161 codec_id = file_oformat->video_codec;
2162 if (video_codec_id != CODEC_ID_NONE)
2163 codec_id = video_codec_id;
2165 video_enc->codec_id = codec_id;
2167 video_enc->bit_rate = video_bit_rate;
2168 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2169 video_enc->frame_rate = frame_rate;
2170 video_enc->frame_rate_base = frame_rate_base;
2172 video_enc->width = frame_width;
2173 video_enc->height = frame_height;
2176 video_enc->gop_size = gop_size;
2178 video_enc->gop_size = 0;
2179 if (video_qscale || same_quality) {
2180 video_enc->flags |= CODEC_FLAG_QSCALE;
2181 st->quality = video_qscale;
2185 video_enc->flags |= CODEC_FLAG_BITEXACT;
2188 video_enc->flags |= CODEC_FLAG_HQ;
2192 video_enc->flags |= CODEC_FLAG_H263P_UMV;
2195 video_enc->flags |= CODEC_FLAG_H263P_AIC;
2199 video_enc->flags |= CODEC_FLAG_HQ;
2200 video_enc->flags |= CODEC_FLAG_4MV;
2204 video_enc->flags |= CODEC_FLAG_PART;
2208 video_enc->max_b_frames = b_frames;
2209 video_enc->b_frame_strategy = 0;
2210 video_enc->b_quant_factor = 2.0;
2213 video_enc->qmin = video_qmin;
2214 video_enc->qmax = video_qmax;
2215 video_enc->mb_qmin = video_mb_qmin;
2216 video_enc->mb_qmax = video_mb_qmax;
2217 video_enc->max_qdiff = video_qdiff;
2218 video_enc->qblur = video_qblur;
2219 video_enc->qcompress = video_qcomp;
2220 video_enc->rc_eq = video_rc_eq;
2221 video_enc->debug= debug;
2223 p= video_rc_override_string;
2226 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2228 fprintf(stderr, "error parsing rc_override\n");
2231 video_enc->rc_override=
2232 av_realloc(video_enc->rc_override,
2233 sizeof(RcOverride)*(i+1));
2234 video_enc->rc_override[i].start_frame= start;
2235 video_enc->rc_override[i].end_frame = end;
2237 video_enc->rc_override[i].qscale= q;
2238 video_enc->rc_override[i].quality_factor= 1.0;
2241 video_enc->rc_override[i].qscale= 0;
2242 video_enc->rc_override[i].quality_factor= -q/100.0;
2247 video_enc->rc_override_count=i;
2249 video_enc->rc_max_rate = video_rc_max_rate;
2250 video_enc->rc_min_rate = video_rc_min_rate;
2251 video_enc->rc_buffer_size = video_rc_buffer_size;
2252 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2253 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2254 video_enc->i_quant_factor = video_i_qfactor;
2255 video_enc->b_quant_factor = video_b_qfactor;
2256 video_enc->i_quant_offset = video_i_qoffset;
2257 video_enc->b_quant_offset = video_b_qoffset;
2258 video_enc->dct_algo = dct_algo;
2259 video_enc->idct_algo = idct_algo;
2260 video_enc->strict_std_compliance = strict;
2262 video_enc->rtp_mode= 1;
2263 video_enc->rtp_payload_size= packet_size;
2267 video_enc->flags|= CODEC_FLAG_PSNR;
2269 video_enc->me_method = me_method;
2274 video_enc->flags |= CODEC_FLAG_PASS1;
2276 video_enc->flags |= CODEC_FLAG_PASS2;
2280 oc->streams[nb_streams] = st;
2285 AVCodecContext *audio_enc;
2287 st = av_mallocz(sizeof(AVStream));
2289 fprintf(stderr, "Could not alloc stream\n");
2292 avcodec_get_context_defaults(&st->codec);
2294 audio_enc = &st->codec;
2295 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2296 if (audio_stream_copy) {
2297 st->stream_copy = 1;
2299 codec_id = file_oformat->audio_codec;
2300 if (audio_codec_id != CODEC_ID_NONE)
2301 codec_id = audio_codec_id;
2302 audio_enc->codec_id = codec_id;
2304 audio_enc->bit_rate = audio_bit_rate;
2305 audio_enc->sample_rate = audio_sample_rate;
2306 /* For audio codecs other than AC3 we limit */
2307 /* the number of coded channels to stereo */
2308 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2309 audio_enc->channels = 2;
2311 audio_enc->channels = audio_channels;
2313 oc->streams[nb_streams] = st;
2317 oc->nb_streams = nb_streams;
2320 fprintf(stderr, "No audio or video streams available\n");
2325 pstrcpy(oc->title, sizeof(oc->title), str_title);
2327 pstrcpy(oc->author, sizeof(oc->author), str_author);
2329 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2331 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2334 output_files[nb_output_files++] = oc;
2336 strcpy(oc->filename, filename);
2338 /* check filename in case of an image number is expected */
2339 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2340 if (filename_number_test(oc->filename) < 0) {
2341 print_error(oc->filename, AVERROR_NUMEXPECTED);
2346 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2347 /* test if it already exists to avoid loosing precious files */
2348 if (!file_overwrite &&
2349 (strchr(filename, ':') == NULL ||
2350 strstart(filename, "file:", NULL))) {
2351 if (url_exist(filename)) {
2354 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2357 if (toupper(c) != 'Y') {
2358 fprintf(stderr, "Not overwriting - exiting\n");
2365 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2366 fprintf(stderr, "Could not open '%s'\n", filename);
2371 memset(ap, 0, sizeof(*ap));
2372 ap->image_format = image_format;
2373 if (av_set_parameters(oc, ap) < 0) {
2374 fprintf(stderr, "%s: Invalid encoding parameters\n",
2379 /* reset some options */
2380 file_oformat = NULL;
2381 file_iformat = NULL;
2382 image_format = NULL;
2385 audio_codec_id = CODEC_ID_NONE;
2386 video_codec_id = CODEC_ID_NONE;
2387 audio_stream_copy = 0;
2388 video_stream_copy = 0;
2391 /* prepare dummy protocols for grab */
2392 static void prepare_grab(void)
2394 int has_video, has_audio, i, j;
2395 AVFormatContext *oc;
2396 AVFormatContext *ic;
2397 AVFormatParameters vp1, *vp = &vp1;
2398 AVFormatParameters ap1, *ap = &ap1;
2400 /* see if audio/video inputs are needed */
2403 memset(ap, 0, sizeof(*ap));
2404 memset(vp, 0, sizeof(*vp));
2405 for(j=0;j<nb_output_files;j++) {
2406 oc = output_files[j];
2407 for(i=0;i<oc->nb_streams;i++) {
2408 AVCodecContext *enc = &oc->streams[i]->codec;
2409 switch(enc->codec_type) {
2410 case CODEC_TYPE_AUDIO:
2411 if (enc->sample_rate > ap->sample_rate)
2412 ap->sample_rate = enc->sample_rate;
2413 if (enc->channels > ap->channels)
2414 ap->channels = enc->channels;
2417 case CODEC_TYPE_VIDEO:
2418 if (enc->width > vp->width)
2419 vp->width = enc->width;
2420 if (enc->height > vp->height)
2421 vp->height = enc->height;
2423 assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
2424 if (enc->frame_rate > vp->frame_rate){
2425 vp->frame_rate = enc->frame_rate;
2426 vp->frame_rate_base = enc->frame_rate_base;
2436 if (has_video == 0 && has_audio == 0) {
2437 fprintf(stderr, "Output file must have at least one audio or video stream\n");
2442 AVInputFormat *fmt1;
2443 fmt1 = av_find_input_format(video_grab_format);
2444 vp->device = video_device;
2445 vp->channel = video_channel;
2446 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
2447 fprintf(stderr, "Could not find video grab device\n");
2450 /* by now video grab has one stream */
2451 ic->streams[0]->r_frame_rate = vp->frame_rate;
2452 ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
2453 input_files[nb_input_files] = ic;
2454 dump_format(ic, nb_input_files, "", 0);
2457 if (has_audio && audio_grab_format) {
2458 AVInputFormat *fmt1;
2459 fmt1 = av_find_input_format(audio_grab_format);
2460 ap->device = audio_device;
2461 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2462 fprintf(stderr, "Could not find audio grab device\n");
2465 input_files[nb_input_files] = ic;
2466 dump_format(ic, nb_input_files, "", 0);
2471 /* open the necessary output devices for playing */
2472 static void prepare_play(void)
2474 int has_video, has_audio;
2476 check_audio_video_inputs(&has_video, &has_audio);
2478 /* manual disable */
2479 if (audio_disable) {
2482 if (video_disable) {
2487 file_oformat = guess_format("audio_device", NULL, NULL);
2488 if (!file_oformat) {
2489 fprintf(stderr, "Could not find audio device\n");
2492 opt_output_file(audio_device?audio_device:"/dev/dsp");
2496 file_oformat = guess_format("framebuffer_device", NULL, NULL);
2497 if (!file_oformat) {
2498 fprintf(stderr, "Could not find framebuffer device\n");
2501 opt_output_file("");
2505 /* same option as mencoder */
2506 static void opt_pass(const char *pass_str)
2509 pass = atoi(pass_str);
2510 if (pass != 1 && pass != 2) {
2511 fprintf(stderr, "pass number can be only 1 or 2\n");
2517 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
2518 static int64_t getutime(void)
2520 return av_gettime();
2523 static int64_t getutime(void)
2525 struct rusage rusage;
2527 getrusage(RUSAGE_SELF, &rusage);
2528 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2532 extern int ffm_nopts;
2534 static void opt_bitexact(void)
2537 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2541 static void show_formats(void)
2543 AVInputFormat *ifmt;
2544 AVOutputFormat *ofmt;
2545 AVImageFormat *image_fmt;
2550 printf("Output audio/video file formats:");
2551 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2552 printf(" %s", ofmt->name);
2556 printf("Input audio/video file formats:");
2557 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2558 printf(" %s", ifmt->name);
2562 printf("Output image formats:");
2563 for(image_fmt = first_image_format; image_fmt != NULL;
2564 image_fmt = image_fmt->next) {
2565 if (image_fmt->img_write)
2566 printf(" %s", image_fmt->name);
2570 printf("Input image formats:");
2571 for(image_fmt = first_image_format; image_fmt != NULL;
2572 image_fmt = image_fmt->next) {
2573 if (image_fmt->img_read)
2574 printf(" %s", image_fmt->name);
2578 printf("Codecs:\n");
2579 printf(" Encoders:");
2580 for(p = first_avcodec; p != NULL; p = p->next) {
2582 printf(" %s", p->name);
2586 printf(" Decoders:");
2587 for(p = first_avcodec; p != NULL; p = p->next) {
2589 printf(" %s", p->name);
2593 printf("Supported file protocols:");
2594 for(up = first_protocol; up != NULL; up = up->next)
2595 printf(" %s:", up->name);
2598 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2599 printf("Motion estimation methods:");
2603 if ((pp - motion_str + 1) == ME_ZERO)
2604 printf("(fastest)");
2605 else if ((pp - motion_str + 1) == ME_FULL)
2606 printf("(slowest)");
2607 else if ((pp - motion_str + 1) == ME_EPZS)
2608 printf("(default)");
2615 void show_help(void)
2618 const OptionDef *po;
2621 prog = do_play ? "ffplay" : "ffmpeg";
2623 printf("%s version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n",
2627 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2628 "Hyper fast MPEG1/MPEG4/H263/RV and AC3/MPEG audio encoder\n");
2630 printf("usage: ffplay [options] input_file...\n"
2631 "Simple audio player\n");
2635 "Main options are:\n");
2638 printf("\nAdvanced options are:\n");
2639 for(po = options; po->name != NULL; po++) {
2641 expert = (po->flags & OPT_EXPERT) != 0;
2643 strcpy(buf, po->name);
2644 if (po->flags & HAS_ARG) {
2646 strcat(buf, po->argname);
2648 printf("-%-17s %s\n", buf, po->help);
2656 const OptionDef options[] = {
2657 { "L", 0, {(void*)show_licence}, "show license" },
2658 { "h", 0, {(void*)show_help}, "show help" },
2659 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2660 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2661 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
2662 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2663 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2664 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2665 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2666 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2667 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2668 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2669 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2670 { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
2671 { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
2673 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2674 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2675 { "re", OPT_BOOL|OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate" },
2676 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2677 { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
2678 { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
2679 { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
2680 { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
2681 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2682 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2683 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2684 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2685 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2686 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2687 { "mbqmin", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
2688 { "mbqmax", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
2689 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2690 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2691 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2692 { "rc_init_cplx", HAS_ARG | OPT_EXPERT, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
2693 { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
2694 { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
2695 { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
2696 { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
2697 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
2698 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
2699 { "rc_override", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_override_string}, "Rate control override", "qualities for specific intervals" },
2700 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2701 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2702 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2703 { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
2704 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2705 { "vc", HAS_ARG | OPT_EXPERT, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
2706 { "dv1394", OPT_EXPERT, {(void*)opt_dv1394}, "set DV1394 grab", "" },
2707 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
2708 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2710 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2711 { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo", "algo" },
2712 { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
2713 { "ec", HAS_ARG | OPT_EXPERT, {(void*)opt_error_concealment}, "set error concealment", "" },
2714 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2715 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2716 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2717 { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
2718 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2719 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
2720 { "strict", HAS_ARG | OPT_EXPERT, {(void*)opt_strict}, "strictness", "how strictly to follow the standarts" },
2721 { "sameq", OPT_BOOL, {(void*)&same_quality},
2722 "use same video quality as source (implies VBR)" },
2723 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
2725 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2726 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2727 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2728 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2729 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2730 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
2731 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2732 "deinterlace pictures" },
2733 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2734 "add timings for benchmarking" },
2735 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2736 "dump each input packet" },
2737 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2738 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2739 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2740 { "vhook", HAS_ARG | OPT_EXPERT, {(void*)add_frame_hooker}, "insert video processing module", "module name and parameters" },
2742 { "aic", OPT_BOOL | OPT_EXPERT, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
2743 { "umv", OPT_BOOL | OPT_EXPERT, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
2748 int main(int argc, char **argv)
2751 const char *opt, *arg;
2752 const OptionDef *po;
2757 /* detect if invoked as player */
2758 i = strlen(argv[0]);
2759 if (i >= 6 && !strcmp(argv[0] + i - 6, "ffplay"))
2767 while (optindex < argc) {
2768 opt = argv[optindex++];
2770 if (opt[0] == '-' && opt[1] != '\0') {
2772 while (po->name != NULL) {
2773 if (!strcmp(opt + 1, po->name))
2778 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], opt);
2782 if (po->flags & HAS_ARG) {
2783 arg = argv[optindex++];
2785 fprintf(stderr, "%s: missing argument for option '%s'\n", argv[0], opt);
2789 if (po->flags & OPT_STRING) {
2791 str = av_strdup(arg);
2792 *po->u.str_arg = str;
2793 } else if (po->flags & OPT_BOOL) {
2796 po->u.func_arg(arg);
2800 opt_output_file(opt);
2802 opt_input_file(opt);
2809 /* file converter / grab */
2810 if (nb_output_files <= 0) {
2811 fprintf(stderr, "Must supply at least one output file\n");
2815 if (nb_input_files == 0) {
2820 if (nb_input_files <= 0) {
2821 fprintf(stderr, "Must supply at least one input file\n");
2828 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2829 stream_maps, nb_stream_maps);
2830 ti = getutime() - ti;
2832 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2836 for(i=0;i<nb_output_files;i++) {
2837 /* maybe av_close_output_file ??? */
2838 AVFormatContext *s = output_files[i];
2840 if (!(s->oformat->flags & AVFMT_NOFILE))
2842 for(j=0;j<s->nb_streams;j++)
2843 av_free(s->streams[j]);
2846 for(i=0;i<nb_input_files;i++)
2847 av_close_input_file(input_files[i]);
2852 #ifdef POWERPC_TBL_PERFORMANCE_REPORT
2853 extern void powerpc_display_perf_report(void);
2854 powerpc_display_perf_report();
2855 #endif /* POWERPC_TBL_PERFORMANCE_REPORT */
2857 #ifndef CONFIG_WIN32
2858 if (received_sigterm) {
2860 "Received signal %d: terminating.\n",
2861 (int) received_sigterm);
2865 exit(0); /* not all OS-es handle main() return value */