3 * Copyright (c) 2000-2003 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>
45 #if !defined(INFINITY) && defined(HUGE_VAL)
46 #define INFINITY HUGE_VAL
49 #define MAXINT64 int64_t_C(0x7fffffffffffffff)
51 /* select an input stream for an output stream */
52 typedef struct AVStreamMap {
57 extern const OptionDef options[];
63 static AVFormatContext *input_files[MAX_FILES];
64 static int nb_input_files = 0;
66 static AVFormatContext *output_files[MAX_FILES];
67 static int nb_output_files = 0;
69 static AVStreamMap stream_maps[MAX_FILES];
70 static int nb_stream_maps;
72 static AVInputFormat *file_iformat;
73 static AVOutputFormat *file_oformat;
74 static AVImageFormat *image_format;
75 static int frame_width = 160;
76 static int frame_height = 128;
77 static float frame_aspect_ratio = 0;
78 static enum PixelFormat frame_pix_fmt = PIX_FMT_YUV420P;
79 static int frame_topBand = 0;
80 static int frame_bottomBand = 0;
81 static int frame_leftBand = 0;
82 static int frame_rightBand = 0;
83 static int frame_rate = 25;
84 static int frame_rate_base = 1;
85 static int video_bit_rate = 200*1000;
86 static int video_bit_rate_tolerance = 4000*1000;
87 static int video_qscale = 0;
88 static int video_qmin = 2;
89 static int video_qmax = 31;
90 static int video_mb_qmin = 2;
91 static int video_mb_qmax = 31;
92 static int video_qdiff = 3;
93 static float video_qblur = 0.5;
94 static float video_qcomp = 0.5;
95 #if 0 //experimental, (can be removed)
96 static float video_rc_qsquish=1.0;
97 static float video_rc_qmod_amp=0;
98 static int video_rc_qmod_freq=0;
100 static char *video_rc_override_string=NULL;
101 static char *video_rc_eq="tex^qComp";
102 static int video_rc_buffer_size=0;
103 static float video_rc_buffer_aggressivity=1.0;
104 static int video_rc_max_rate=0;
105 static int video_rc_min_rate=0;
106 static float video_rc_initial_cplx=0;
107 static float video_b_qfactor = 1.25;
108 static float video_b_qoffset = 1.25;
109 static float video_i_qfactor = -0.8;
110 static float video_i_qoffset = 0.0;
111 static int me_method = ME_EPZS;
112 static int video_disable = 0;
113 static int video_codec_id = CODEC_ID_NONE;
114 static int same_quality = 0;
115 static int b_frames = 0;
116 static int use_hq = 0;
117 static int use_4mv = 0;
119 static int use_aic = 0;
120 static int use_umv = 0;
122 static int do_deinterlace = 0;
123 static int workaround_bugs = FF_BUG_AUTODETECT;
124 static int error_resilience = 2;
125 static int error_concealment = 3;
126 static int dct_algo = 0;
127 static int idct_algo = 0;
128 static int use_part = 0;
129 static int packet_size = 0;
130 static int strict = 0;
131 static int debug = 0;
133 static int gop_size = 12;
134 static int intra_only = 0;
135 static int audio_sample_rate = 44100;
136 static int audio_bit_rate = 64000;
137 static int audio_disable = 0;
138 static int audio_channels = 1;
139 static int audio_codec_id = CODEC_ID_NONE;
141 static int64_t recording_time = 0;
142 static int file_overwrite = 0;
143 static char *str_title = NULL;
144 static char *str_author = NULL;
145 static char *str_copyright = NULL;
146 static char *str_comment = NULL;
147 static int do_benchmark = 0;
148 static int do_hex_dump = 0;
149 static int do_psnr = 0;
150 static int do_vstats = 0;
151 static int do_pass = 0;
152 static int bitexact = 0;
153 static char *pass_logfilename = NULL;
154 static int audio_stream_copy = 0;
155 static int video_stream_copy = 0;
157 static int rate_emu = 0;
159 static char *video_grab_format = "video4linux";
160 static char *video_device = NULL;
161 static int video_channel = 0;
162 static char *video_standard = "ntsc";
164 static char *audio_grab_format = "audio_device";
165 static char *audio_device = NULL;
167 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
169 typedef struct AVOutputStream {
170 int file_index; /* file index */
171 int index; /* stream index in the output file */
172 int source_index; /* AVInputStream index */
173 AVStream *st; /* stream in the output file */
174 int encoding_needed; /* true if encoding needed for this stream */
176 /* input pts and corresponding output pts
179 double sync_ipts_offset;
182 int video_resample; /* video_resample and video_crop are mutually exclusive */
183 AVPicture pict_tmp; /* temporary image for resampling */
184 ImgReSampleContext *img_resample_ctx; /* for image resampling */
186 int video_crop; /* video_resample and video_crop are mutually exclusive */
187 int topBand; /* cropping area sizes */
192 ReSampleContext *resample; /* for audio resampling */
193 FifoBuffer fifo; /* for compression: one audio fifo per codec */
197 typedef struct AVInputStream {
201 int discard; /* true if stream data should be discarded */
202 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
203 int64_t sample_index; /* current sample */
204 int frame_decoded; /* true if a video or audio frame has been decoded */
206 int64_t start; /* time when read started */
207 unsigned long frame; /* current frame */
210 typedef struct AVInputFile {
211 int eof_reached; /* true if eof reached */
212 int ist_index; /* index of first stream in ist_table */
213 int buffer_size; /* current total buffer size */
214 int buffer_size_max; /* buffer size at which we consider we can stop
216 int nb_streams; /* nb streams we are aware of */
221 /* init terminal so that we can grab keys */
222 static struct termios oldtty;
224 static void term_exit(void)
226 tcsetattr (0, TCSANOW, &oldtty);
229 static volatile sig_atomic_t received_sigterm = 0;
232 sigterm_handler(int sig)
234 received_sigterm = sig;
238 static void term_init(void)
245 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
246 |INLCR|IGNCR|ICRNL|IXON);
247 tty.c_oflag |= OPOST;
248 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
249 tty.c_cflag &= ~(CSIZE|PARENB);
254 tcsetattr (0, TCSANOW, &tty);
256 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
257 signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
258 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
260 register a function to be called at normal program termination
263 #ifdef CONFIG_BEOS_NETSERVER
264 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
268 /* read a key without blocking */
269 static int read_key(void)
273 #ifndef CONFIG_BEOS_NETSERVER
281 n = select(1, &rfds, NULL, NULL, &tv);
295 /* no interactive support */
296 static void term_exit(void)
300 static void term_init(void)
304 static int read_key(void)
311 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
316 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
319 /* copy stream format */
320 s->nb_streams = ic->nb_streams;
321 for(i=0;i<ic->nb_streams;i++) {
324 st = av_mallocz(sizeof(AVStream));
325 memcpy(st, ic->streams[i], sizeof(AVStream));
329 av_close_input_file(ic);
333 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
335 static void do_audio_out(AVFormatContext *s,
338 unsigned char *buf, int size)
341 static uint8_t *audio_buf = NULL;
342 static uint8_t *audio_out = NULL;
343 const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
345 int size_out, frame_bytes, ret;
348 /* SC: dynamic allocation of buffers */
350 audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
352 audio_out = av_malloc(audio_out_size);
353 if (!audio_buf || !audio_out)
354 return; /* Should signal an error ! */
357 enc = &ost->st->codec;
359 if (ost->audio_resample) {
361 size_out = audio_resample(ost->resample,
362 (short *)buftmp, (short *)buf,
363 size / (ist->st->codec.channels * 2));
364 size_out = size_out * enc->channels * 2;
370 /* now encode as many frames as possible */
371 if (enc->frame_size > 1) {
372 /* output resampled raw samples */
373 fifo_write(&ost->fifo, buftmp, size_out,
376 frame_bytes = enc->frame_size * 2 * enc->channels;
378 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
379 &ost->fifo.rptr) == 0) {
380 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
382 av_write_frame(s, ost->index, audio_out, ret);
385 /* output a pcm frame */
386 /* XXX: change encoding codec API to avoid this ? */
387 switch(enc->codec->id) {
388 case CODEC_ID_PCM_S16LE:
389 case CODEC_ID_PCM_S16BE:
390 case CODEC_ID_PCM_U16LE:
391 case CODEC_ID_PCM_U16BE:
394 size_out = size_out >> 1;
397 ret = avcodec_encode_audio(enc, audio_out, size_out,
399 av_write_frame(s, ost->index, audio_out, ret);
403 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
407 AVPicture picture_tmp;
410 dec = &ist->st->codec;
412 /* deinterlace : must be done before any resize */
413 if (do_deinterlace) {
416 /* create temporary picture */
417 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
418 buf = av_malloc(size);
422 picture2 = &picture_tmp;
423 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
425 if (avpicture_deinterlace(picture2, picture,
426 dec->pix_fmt, dec->width, dec->height) < 0) {
427 /* if error, do not deinterlace */
436 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
438 if (picture != picture2)
439 *picture = *picture2;
443 /* we begin to correct av delay at this threshold */
444 #define AV_DELAY_MAX 0.100
446 static void do_video_out(AVFormatContext *s,
449 AVPicture *in_picture,
450 int *frame_size, AVOutputStream *audio_sync)
452 int nb_frames, i, ret;
453 AVPicture *final_picture, *formatted_picture;
454 AVPicture picture_format_temp, picture_crop_temp;
455 static uint8_t *video_buffer;
456 uint8_t *buf = NULL, *buf1 = NULL;
457 AVCodecContext *enc, *dec;
459 #define VIDEO_BUFFER_SIZE (1024*1024)
461 enc = &ost->st->codec;
462 dec = &ist->st->codec;
464 /* by default, we output a single frame */
469 /* NOTE: the A/V sync is always done by considering the audio is
470 the master clock. It is suffisant for transcoding or playing,
471 but not for the general case */
473 /* compute the A-V delay and duplicate/remove frames if needed */
474 double adelta, vdelta, apts, vpts, av_delay;
476 if (audio_sync->sync_ipts != AV_NOPTS_VALUE &&
477 ost->sync_ipts != AV_NOPTS_VALUE) {
479 adelta = (double)(ost->st->pts.val - audio_sync->sync_opts) *
480 s->pts_num / s->pts_den;
481 apts = audio_sync->sync_ipts + adelta;
483 vdelta = (double)(ost->st->pts.val - ost->sync_opts) *
484 s->pts_num / s->pts_den;
485 vpts = ost->sync_ipts + vdelta;
487 av_delay = apts - vpts;
488 // printf("delay=%f\n", av_delay);
489 if (av_delay < -AV_DELAY_MAX)
491 else if (av_delay > AV_DELAY_MAX)
497 if (ost->sync_ipts != AV_NOPTS_VALUE) {
498 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
499 if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
500 if (vdelta < -AV_DELAY_MAX)
502 else if (vdelta > AV_DELAY_MAX)
505 ost->sync_ipts_offset -= vdelta;
506 if (!ost->sync_ipts_offset)
507 ost->sync_ipts_offset = 0.000001; /* one microsecond */
512 static char *action[] = { "drop frame", "copy frame", "dup frame" };
513 printf("Input PTS %12.6f, output PTS %12.6f: %s\n",
514 (double) ost->sync_ipts, (double) ost->st->pts.val * s->pts_num / s->pts_den,
524 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
528 /* convert pixel format if needed */
529 if (enc->pix_fmt != dec->pix_fmt) {
532 /* create temporary picture */
533 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
534 buf = av_malloc(size);
537 formatted_picture = &picture_format_temp;
538 avpicture_fill(formatted_picture, buf, enc->pix_fmt, dec->width, dec->height);
540 if (img_convert(formatted_picture, enc->pix_fmt,
541 in_picture, dec->pix_fmt,
542 dec->width, dec->height) < 0) {
543 fprintf(stderr, "pixel format conversion not handled\n");
547 formatted_picture = in_picture;
550 /* XXX: resampling could be done before raw format convertion in
551 some cases to go faster */
552 /* XXX: only works for YUV420P */
553 if (ost->video_resample) {
554 final_picture = &ost->pict_tmp;
555 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
556 } else if (ost->video_crop) {
557 picture_crop_temp.data[0] = formatted_picture->data[0] +
558 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
560 picture_crop_temp.data[1] = formatted_picture->data[1] +
561 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
562 (ost->leftBand >> 1);
564 picture_crop_temp.data[2] = formatted_picture->data[2] +
565 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
566 (ost->leftBand >> 1);
568 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
569 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
570 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
571 final_picture = &picture_crop_temp;
573 final_picture = formatted_picture;
575 /* duplicates frame if needed */
576 /* XXX: pb because no interleaving */
577 for(i=0;i<nb_frames;i++) {
578 if (s->oformat->flags & AVFMT_RAWPICTURE) {
579 /* raw pictures are written as AVPicture structure to
580 avoid any copies. We support temorarily the older
582 av_write_frame(s, ost->index,
583 (uint8_t *)final_picture, sizeof(AVPicture));
587 memset(&big_picture, 0, sizeof(AVFrame));
588 *(AVPicture*)&big_picture= *final_picture;
590 /* handles sameq here. This is not correct because it may
591 not be a global option */
593 big_picture.quality = ist->st->quality;
595 big_picture.quality = ost->st->quality;
597 ret = avcodec_encode_video(enc,
598 video_buffer, VIDEO_BUFFER_SIZE,
600 //enc->frame_number = enc->real_pict_num;
601 av_write_frame(s, ost->index, video_buffer, ret);
603 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
604 // enc->frame_number-1, enc->real_pict_num, ret,
606 /* if two pass, output log */
607 if (ost->logfile && enc->stats_out) {
608 fprintf(ost->logfile, "%s", enc->stats_out);
618 static double psnr(double d){
619 if(d==0) return INFINITY;
620 return -10.0*log(d)/log(10.0);
623 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
626 static FILE *fvstats=NULL;
627 static int64_t total_size = 0;
634 double ti1, bitrate, avg_bitrate;
638 today = localtime(&today2);
639 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
642 fvstats = fopen(filename,"w");
650 enc = &ost->st->codec;
651 total_size += frame_size;
652 if (enc->codec_type == CODEC_TYPE_VIDEO) {
653 frame_number = ost->frame_number;
654 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality);
655 if (enc->flags&CODEC_FLAG_PSNR)
656 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
658 fprintf(fvstats,"f_size= %6d ", frame_size);
659 /* compute pts value */
660 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
664 bitrate = (double)(frame_size * 8) * enc->frame_rate / enc->frame_rate_base / 1000.0;
665 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
666 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
667 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
668 fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
672 static void print_report(AVFormatContext **output_files,
673 AVOutputStream **ost_table, int nb_ostreams,
678 AVFormatContext *oc, *os;
681 int frame_number, vid, i;
682 double bitrate, ti1, pts;
683 static int64_t last_time = -1;
685 if (!is_last_report) {
687 /* display the report every 0.5 seconds */
688 cur_time = av_gettime();
689 if (last_time == -1) {
690 last_time = cur_time;
693 if ((cur_time - last_time) < 500000)
695 last_time = cur_time;
699 oc = output_files[0];
701 total_size = url_ftell(&oc->pb);
706 for(i=0;i<nb_ostreams;i++) {
708 os = output_files[ost->file_index];
709 enc = &ost->st->codec;
710 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
711 sprintf(buf + strlen(buf), "q=%2.1f ",
712 enc->coded_frame->quality);
714 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
715 frame_number = ost->frame_number;
716 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
717 frame_number, enc->coded_frame ? enc->coded_frame->quality : 0);
718 if (enc->flags&CODEC_FLAG_PSNR)
719 sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
722 /* compute min output value */
723 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
724 if ((pts < ti1) && (pts > 0))
729 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
731 sprintf(buf + strlen(buf),
732 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
733 (double)total_size / 1024, ti1, bitrate);
735 fprintf(stderr, "%s ", buf);
737 if (is_last_report) {
738 fprintf(stderr, "\n");
740 fprintf(stderr, "\r");
746 * The following code is the main loop of the file converter
748 static int av_encode(AVFormatContext **output_files,
750 AVFormatContext **input_files,
752 AVStreamMap *stream_maps, int nb_stream_maps)
754 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0, pts_set;
755 AVFormatContext *is, *os;
756 AVCodecContext *codec, *icodec;
757 AVOutputStream *ost, **ost_table = NULL;
758 AVInputStream *ist, **ist_table = NULL;
759 AVInputFile *file_table;
760 AVFormatContext *stream_no_data;
763 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
767 /* input stream init */
769 for(i=0;i<nb_input_files;i++) {
771 file_table[i].ist_index = j;
772 file_table[i].nb_streams = is->nb_streams;
777 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
781 for(i=0;i<nb_istreams;i++) {
782 ist = av_mallocz(sizeof(AVInputStream));
788 for(i=0;i<nb_input_files;i++) {
790 for(k=0;k<is->nb_streams;k++) {
791 ist = ist_table[j++];
792 ist->st = is->streams[k];
795 ist->discard = 1; /* the stream is discarded by default
798 if (ist->st->codec.rate_emu) {
799 ist->start = av_gettime();
805 /* output stream init */
807 for(i=0;i<nb_output_files;i++) {
808 os = output_files[i];
809 nb_ostreams += os->nb_streams;
811 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
812 fprintf(stderr, "Number of stream maps must match number of output streams\n");
816 /* Sanity check the mapping args -- do the input files & streams exist? */
817 for(i=0;i<nb_stream_maps;i++) {
818 int fi = stream_maps[i].file_index;
819 int si = stream_maps[i].stream_index;
821 if (fi < 0 || fi > nb_input_files - 1 ||
822 si < 0 || si > file_table[fi].nb_streams - 1) {
823 fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
828 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
831 for(i=0;i<nb_ostreams;i++) {
832 ost = av_mallocz(sizeof(AVOutputStream));
839 for(k=0;k<nb_output_files;k++) {
840 os = output_files[k];
841 for(i=0;i<os->nb_streams;i++) {
843 ost = ost_table[n++];
846 ost->st = os->streams[i];
847 if (nb_stream_maps > 0) {
848 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
849 stream_maps[n-1].stream_index;
851 /* Sanity check that the stream types match */
852 if (ist_table[ost->source_index]->st->codec.codec_type != ost->st->codec.codec_type) {
853 fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
854 stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
855 ost->file_index, ost->index);
860 /* get corresponding input stream index : we select the first one with the right type */
862 for(j=0;j<nb_istreams;j++) {
865 ist->st->codec.codec_type == ost->st->codec.codec_type) {
866 ost->source_index = j;
872 /* try again and reuse existing stream */
873 for(j=0;j<nb_istreams;j++) {
875 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
876 ost->source_index = j;
881 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
882 ost->file_index, ost->index);
887 ist = ist_table[ost->source_index];
892 /* for each output stream, we compute the right encoding parameters */
893 for(i=0;i<nb_ostreams;i++) {
895 ist = ist_table[ost->source_index];
897 codec = &ost->st->codec;
898 icodec = &ist->st->codec;
900 if (ost->st->stream_copy) {
901 /* if stream_copy is selected, no need to decode or encode */
902 codec->codec_id = icodec->codec_id;
903 codec->codec_type = icodec->codec_type;
904 codec->codec_tag = icodec->codec_tag;
905 codec->bit_rate = icodec->bit_rate;
906 switch(codec->codec_type) {
907 case CODEC_TYPE_AUDIO:
908 codec->sample_rate = icodec->sample_rate;
909 codec->channels = icodec->channels;
911 case CODEC_TYPE_VIDEO:
912 codec->frame_rate = icodec->frame_rate;
913 codec->frame_rate_base = icodec->frame_rate_base;
914 codec->width = icodec->width;
915 codec->height = icodec->height;
921 switch(codec->codec_type) {
922 case CODEC_TYPE_AUDIO:
923 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
926 if (codec->channels == icodec->channels &&
927 codec->sample_rate == icodec->sample_rate) {
928 ost->audio_resample = 0;
930 if (codec->channels != icodec->channels &&
931 icodec->codec_id == CODEC_ID_AC3) {
932 /* Special case for 5:1 AC3 input */
933 /* and mono or stereo output */
934 /* Request specific number of channels */
935 icodec->channels = codec->channels;
936 if (codec->sample_rate == icodec->sample_rate)
937 ost->audio_resample = 0;
939 ost->audio_resample = 1;
940 ost->resample = audio_resample_init(codec->channels, icodec->channels,
942 icodec->sample_rate);
944 /* Request specific number of channels */
945 icodec->channels = codec->channels;
947 ost->audio_resample = 1;
948 ost->resample = audio_resample_init(codec->channels, icodec->channels,
950 icodec->sample_rate);
953 ist->decoding_needed = 1;
954 ost->encoding_needed = 1;
956 case CODEC_TYPE_VIDEO:
957 if (codec->width == icodec->width &&
958 codec->height == icodec->height &&
959 frame_topBand == 0 &&
960 frame_bottomBand == 0 &&
961 frame_leftBand == 0 &&
962 frame_rightBand == 0)
964 ost->video_resample = 0;
966 } else if ((codec->width == icodec->width -
967 (frame_leftBand + frame_rightBand)) &&
968 (codec->height == icodec->height -
969 (frame_topBand + frame_bottomBand)))
971 ost->video_resample = 0;
973 ost->topBand = frame_topBand;
974 ost->leftBand = frame_leftBand;
977 ost->video_resample = 1;
978 ost->video_crop = 0; // cropping is handled as part of resample
979 buf = av_malloc((codec->width * codec->height * 3) / 2);
982 ost->pict_tmp.data[0] = buf;
983 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
984 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
985 ost->pict_tmp.linesize[0] = codec->width;
986 ost->pict_tmp.linesize[1] = codec->width / 2;
987 ost->pict_tmp.linesize[2] = codec->width / 2;
989 ost->img_resample_ctx = img_resample_full_init(
990 ost->st->codec.width, ost->st->codec.height,
991 ist->st->codec.width, ist->st->codec.height,
992 frame_topBand, frame_bottomBand,
993 frame_leftBand, frame_rightBand);
995 ost->encoding_needed = 1;
996 ist->decoding_needed = 1;
1002 if (ost->encoding_needed &&
1003 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1004 char logfilename[1024];
1009 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1011 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1012 if (codec->flags & CODEC_FLAG_PASS1) {
1013 f = fopen(logfilename, "w");
1015 perror(logfilename);
1020 /* read the log file */
1021 f = fopen(logfilename, "r");
1023 perror(logfilename);
1026 fseek(f, 0, SEEK_END);
1028 fseek(f, 0, SEEK_SET);
1029 logbuffer = av_malloc(size + 1);
1031 fprintf(stderr, "Could not allocate log buffer\n");
1034 fread(logbuffer, 1, size, f);
1036 logbuffer[size] = '\0';
1037 codec->stats_in = logbuffer;
1043 /* dump the file output parameters - cannot be done before in case
1045 for(i=0;i<nb_output_files;i++) {
1046 dump_format(output_files[i], i, output_files[i]->filename, 1);
1049 /* dump the stream mapping */
1050 fprintf(stderr, "Stream mapping:\n");
1051 for(i=0;i<nb_ostreams;i++) {
1053 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1054 ist_table[ost->source_index]->file_index,
1055 ist_table[ost->source_index]->index,
1060 /* open each encoder */
1061 for(i=0;i<nb_ostreams;i++) {
1063 if (ost->encoding_needed) {
1065 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1067 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1068 ost->file_index, ost->index);
1071 if (avcodec_open(&ost->st->codec, codec) < 0) {
1072 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1073 ost->file_index, ost->index);
1079 /* open each decoder */
1080 for(i=0;i<nb_istreams;i++) {
1082 if (ist->decoding_needed) {
1084 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1086 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1087 ist->st->codec.codec_id, ist->file_index, ist->index);
1090 if (avcodec_open(&ist->st->codec, codec) < 0) {
1091 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1092 ist->file_index, ist->index);
1095 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1096 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1097 ist->frame_decoded = 1;
1102 for(i=0;i<nb_istreams;i++) {
1106 /* compute buffer size max (should use a complete heuristic) */
1107 for(i=0;i<nb_input_files;i++) {
1108 file_table[i].buffer_size_max = 2048;
1111 /* open files and write file headers */
1112 for(i=0;i<nb_output_files;i++) {
1113 os = output_files[i];
1114 if (av_write_header(os) < 0) {
1115 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1121 #ifndef CONFIG_WIN32
1122 fprintf(stderr, "Press [q] to stop encoding\n");
1130 int file_index, ist_index;
1135 int data_size, got_picture;
1137 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1138 void *buffer_to_free;
1142 /* if 'q' pressed, exits */
1144 /* read_key() returns 0 on EOF */
1150 /* select the stream that we must read now by looking at the
1151 smallest output pts */
1154 for(i=0;i<nb_ostreams;i++) {
1157 os = output_files[ost->file_index];
1158 ist = ist_table[ost->source_index];
1159 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1160 if (!file_table[ist->file_index].eof_reached &&
1163 file_index = ist->file_index;
1166 /* if none, if is finished */
1167 if (file_index < 0) {
1171 /* finish if recording time exhausted */
1172 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1175 /* read a packet from it and output it in the fifo */
1176 is = input_files[file_index];
1177 if (av_read_packet(is, &pkt) < 0) {
1178 file_table[file_index].eof_reached = 1;
1182 stream_no_data = is;
1187 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1188 av_hex_dump(pkt.data, pkt.size);
1190 /* the following test is needed in case new streams appear
1191 dynamically in stream : we ignore them */
1192 if (pkt.stream_index >= file_table[file_index].nb_streams)
1193 goto discard_packet;
1194 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1195 ist = ist_table[ist_index];
1197 goto discard_packet;
1199 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1207 ipts = AV_NOPTS_VALUE;
1209 /* decode the packet if needed */
1210 data_buf = NULL; /* fail safe */
1212 if (ist->decoding_needed) {
1213 /* NOTE1: we only take into account the PTS if a new
1214 frame has begun (MPEG semantics) */
1215 /* NOTE2: even if the fraction is not initialized,
1216 av_frac_set can be used to set the integer part */
1217 if (ist->frame_decoded &&
1218 pkt.pts != AV_NOPTS_VALUE &&
1221 ist->frame_decoded = 0;
1225 switch(ist->st->codec.codec_type) {
1226 case CODEC_TYPE_AUDIO:
1227 /* XXX: could avoid copy if PCM 16 bits with same
1228 endianness as CPU */
1229 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1233 /* Some bug in mpeg audio decoder gives */
1234 /* data_size < 0, it seems they are overflows */
1235 if (data_size <= 0) {
1236 /* no audio frame */
1241 data_buf = (uint8_t *)samples;
1243 case CODEC_TYPE_VIDEO:
1245 AVFrame big_picture;
1247 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1248 ret = avcodec_decode_video(&ist->st->codec,
1249 &big_picture, &got_picture, ptr, len);
1250 picture= *(AVPicture*)&big_picture;
1251 ist->st->quality= big_picture.quality;
1254 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1255 ist->file_index, ist->index);
1256 av_free_packet(&pkt);
1260 /* no picture yet */
1280 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1281 pre_process_video_frame(ist, &picture, &buffer_to_free);
1284 ist->frame_decoded = 1;
1286 /* frame rate emulation */
1287 if (ist->st->codec.rate_emu) {
1288 int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate);
1289 int64_t now = av_gettime() - ist->start;
1297 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1298 is the one of the next displayed one */
1299 /* XXX: add mpeg4 too ? */
1300 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1301 if (ist->st->codec.pict_type != B_TYPE) {
1303 tmp = ist->last_ip_pts;
1304 ist->last_ip_pts = ist->frac_pts.val;
1305 ist->frac_pts.val = tmp;
1309 /* transcode raw format, encode packets and output them */
1311 for(i=0;i<nb_ostreams;i++) {
1315 if (ost->source_index == ist_index) {
1316 os = output_files[ost->file_index];
1318 if (ipts != AV_NOPTS_VALUE) {
1320 printf("%d: got pts=%f %f\n",
1321 i, pkt.pts / 90000.0,
1322 (ipts - ost->st->pts.val) / 90000.0);
1324 /* set the input output pts pairs */
1325 ost->sync_ipts = (double)ipts * is->pts_num /
1327 /* XXX: take into account the various fifos,
1328 in particular for audio */
1329 ost->sync_opts = ost->st->pts.val;
1330 //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);
1332 //printf("pts.val=%lld\n", ost->st->pts.val);
1333 ost->sync_ipts = AV_NOPTS_VALUE;
1336 if (ost->encoding_needed) {
1337 switch(ost->st->codec.codec_type) {
1338 case CODEC_TYPE_AUDIO:
1339 do_audio_out(os, ost, ist, data_buf, data_size);
1341 case CODEC_TYPE_VIDEO:
1342 /* find an audio stream for synchro */
1345 AVOutputStream *audio_sync, *ost1;
1347 for(i=0;i<nb_ostreams;i++) {
1348 ost1 = ost_table[i];
1349 if (ost1->file_index == ost->file_index &&
1350 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1356 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1357 if (do_vstats && frame_size)
1358 do_video_stats(os, ost, frame_size);
1367 /* no reencoding needed : output the packet directly */
1368 /* force the input stream PTS */
1370 memset(&avframe, 0, sizeof(AVFrame));
1371 ost->st->codec.coded_frame= &avframe;
1372 avframe.key_frame = pkt.flags & PKT_FLAG_KEY;
1374 av_write_frame(os, ost->index, data_buf, data_size);
1375 ost->st->codec.frame_number++;
1376 ost->frame_number++;
1380 av_free(buffer_to_free);
1381 ipts = AV_NOPTS_VALUE;
1384 av_free_packet(&pkt);
1386 /* dump report by using the output first video and audio streams */
1387 print_report(output_files, ost_table, nb_ostreams, 0);
1391 /* dump report by using the first video and audio streams */
1392 print_report(output_files, ost_table, nb_ostreams, 1);
1394 /* close each encoder */
1395 for(i=0;i<nb_ostreams;i++) {
1397 if (ost->encoding_needed) {
1398 av_freep(&ost->st->codec.stats_in);
1399 avcodec_close(&ost->st->codec);
1403 /* close each decoder */
1404 for(i=0;i<nb_istreams;i++) {
1406 if (ist->decoding_needed) {
1407 avcodec_close(&ist->st->codec);
1412 /* write the trailer if needed and close file */
1413 for(i=0;i<nb_output_files;i++) {
1414 os = output_files[i];
1415 av_write_trailer(os);
1421 av_free(file_table);
1424 for(i=0;i<nb_istreams;i++) {
1431 for(i=0;i<nb_ostreams;i++) {
1435 fclose(ost->logfile);
1436 ost->logfile = NULL;
1438 fifo_free(&ost->fifo); /* works even if fifo is not
1439 initialized but set to zero */
1440 av_free(ost->pict_tmp.data[0]);
1441 if (ost->video_resample)
1442 img_resample_close(ost->img_resample_ctx);
1443 if (ost->audio_resample)
1444 audio_resample_close(ost->resample);
1457 int file_read(const char *filename)
1460 unsigned char buffer[1024];
1463 if (url_open(&h, filename, O_RDONLY) < 0) {
1464 printf("could not open '%s'\n", filename);
1468 len = url_read(h, buffer, sizeof(buffer));
1471 for(i=0;i<len;i++) putchar(buffer[i]);
1478 static void show_licence(void)
1481 "ffmpeg version " FFMPEG_VERSION "\n"
1482 "Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n"
1483 "This library is free software; you can redistribute it and/or\n"
1484 "modify it under the terms of the GNU Lesser General Public\n"
1485 "License as published by the Free Software Foundation; either\n"
1486 "version 2 of the License, or (at your option) any later version.\n"
1488 "This library is distributed in the hope that it will be useful,\n"
1489 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
1490 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
1491 "Lesser General Public License for more details.\n"
1493 "You should have received a copy of the GNU Lesser General Public\n"
1494 "License along with this library; if not, write to the Free Software\n"
1495 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
1500 static void opt_image_format(const char *arg)
1504 for(f = first_image_format; f != NULL; f = f->next) {
1505 if (!strcmp(arg, f->name))
1509 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1515 static void opt_format(const char *arg)
1517 /* compatibility stuff for pgmyuv */
1518 if (!strcmp(arg, "pgmyuv")) {
1519 opt_image_format(arg);
1523 file_iformat = av_find_input_format(arg);
1524 file_oformat = guess_format(arg, NULL, NULL);
1525 if (!file_iformat && !file_oformat) {
1526 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1531 static void opt_video_bitrate(const char *arg)
1533 video_bit_rate = atoi(arg) * 1000;
1536 static void opt_video_bitrate_tolerance(const char *arg)
1538 video_bit_rate_tolerance = atoi(arg) * 1000;
1541 static void opt_video_bitrate_max(const char *arg)
1543 video_rc_max_rate = atoi(arg) * 1000;
1546 static void opt_video_bitrate_min(const char *arg)
1548 video_rc_min_rate = atoi(arg) * 1000;
1551 static void opt_video_buffer_size(const char *arg)
1553 video_rc_buffer_size = atoi(arg) * 1000;
1556 static void opt_video_rc_eq(char *arg)
1561 static void opt_video_rc_override_string(char *arg)
1563 video_rc_override_string = arg;
1567 static void opt_workaround_bugs(const char *arg)
1569 workaround_bugs = atoi(arg);
1572 static void opt_dct_algo(const char *arg)
1574 dct_algo = atoi(arg);
1577 static void opt_idct_algo(const char *arg)
1579 idct_algo = atoi(arg);
1583 static void opt_error_resilience(const char *arg)
1585 error_resilience = atoi(arg);
1588 static void opt_error_concealment(const char *arg)
1590 error_concealment = atoi(arg);
1593 static void opt_debug(const char *arg)
1598 static void opt_frame_rate(const char *arg)
1600 frame_rate_base = DEFAULT_FRAME_RATE_BASE; //FIXME not optimal
1601 frame_rate = (int)(strtod(arg, 0) * frame_rate_base + 0.5);
1602 //FIXME parse fractions
1606 static void opt_frame_crop_top(const char *arg)
1608 frame_topBand = atoi(arg);
1609 if (frame_topBand < 0) {
1610 fprintf(stderr, "Incorrect top crop size\n");
1613 if ((frame_topBand % 2) != 0) {
1614 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1617 if ((frame_topBand) >= frame_height){
1618 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1621 frame_height -= frame_topBand;
1624 static void opt_frame_crop_bottom(const char *arg)
1626 frame_bottomBand = atoi(arg);
1627 if (frame_bottomBand < 0) {
1628 fprintf(stderr, "Incorrect bottom crop size\n");
1631 if ((frame_bottomBand % 2) != 0) {
1632 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1635 if ((frame_bottomBand) >= frame_height){
1636 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1639 frame_height -= frame_bottomBand;
1642 static void opt_frame_crop_left(const char *arg)
1644 frame_leftBand = atoi(arg);
1645 if (frame_leftBand < 0) {
1646 fprintf(stderr, "Incorrect left crop size\n");
1649 if ((frame_leftBand % 2) != 0) {
1650 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1653 if ((frame_leftBand) >= frame_width){
1654 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1657 frame_width -= frame_leftBand;
1660 static void opt_frame_crop_right(const char *arg)
1662 frame_rightBand = atoi(arg);
1663 if (frame_rightBand < 0) {
1664 fprintf(stderr, "Incorrect right crop size\n");
1667 if ((frame_rightBand % 2) != 0) {
1668 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1671 if ((frame_rightBand) >= frame_width){
1672 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1675 frame_width -= frame_rightBand;
1678 static void opt_frame_size(const char *arg)
1680 parse_image_size(&frame_width, &frame_height, arg);
1681 if (frame_width <= 0 || frame_height <= 0) {
1682 fprintf(stderr, "Incorrect frame size\n");
1685 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1686 fprintf(stderr, "Frame size must be a multiple of 2\n");
1691 static void opt_frame_pix_fmt(const char *arg)
1693 frame_pix_fmt = avcodec_get_pix_fmt(arg);
1696 static void opt_frame_aspect_ratio(const char *arg)
1702 p = strchr(arg, ':');
1704 x = strtol(arg, (char **)&arg, 10);
1706 y = strtol(arg+1, (char **)&arg, 10);
1708 ar = (double)x / (double)y;
1710 ar = strtod(arg, (char **)&arg);
1713 fprintf(stderr, "Incorrect aspect ratio specification.\n");
1716 frame_aspect_ratio = ar;
1719 static void opt_gop_size(const char *arg)
1721 gop_size = atoi(arg);
1724 static void opt_b_frames(const char *arg)
1726 b_frames = atoi(arg);
1727 if (b_frames > FF_MAX_B_FRAMES) {
1728 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1730 } else if (b_frames < 1) {
1731 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1736 static void opt_qscale(const char *arg)
1738 video_qscale = atoi(arg);
1739 if (video_qscale < 0 ||
1740 video_qscale > 31) {
1741 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1746 static void opt_qmin(const char *arg)
1748 video_qmin = atoi(arg);
1749 if (video_qmin < 0 ||
1751 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1756 static void opt_qmax(const char *arg)
1758 video_qmax = atoi(arg);
1759 if (video_qmax < 0 ||
1761 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1766 static void opt_mb_qmin(const char *arg)
1768 video_mb_qmin = atoi(arg);
1769 if (video_mb_qmin < 0 ||
1770 video_mb_qmin > 31) {
1771 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1776 static void opt_mb_qmax(const char *arg)
1778 video_mb_qmax = atoi(arg);
1779 if (video_mb_qmax < 0 ||
1780 video_mb_qmax > 31) {
1781 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1786 static void opt_qdiff(const char *arg)
1788 video_qdiff = atoi(arg);
1789 if (video_qdiff < 0 ||
1791 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1796 static void opt_qblur(const char *arg)
1798 video_qblur = atof(arg);
1801 static void opt_qcomp(const char *arg)
1803 video_qcomp = atof(arg);
1806 static void opt_rc_initial_cplx(const char *arg)
1808 video_rc_initial_cplx = atof(arg);
1810 static void opt_b_qfactor(const char *arg)
1812 video_b_qfactor = atof(arg);
1814 static void opt_i_qfactor(const char *arg)
1816 video_i_qfactor = atof(arg);
1818 static void opt_b_qoffset(const char *arg)
1820 video_b_qoffset = atof(arg);
1822 static void opt_i_qoffset(const char *arg)
1824 video_i_qoffset = atof(arg);
1827 static void opt_packet_size(const char *arg)
1829 packet_size= atoi(arg);
1832 static void opt_strict(const char *arg)
1837 static void opt_audio_bitrate(const char *arg)
1839 audio_bit_rate = atoi(arg) * 1000;
1842 static void opt_audio_rate(const char *arg)
1844 audio_sample_rate = atoi(arg);
1847 static void opt_audio_channels(const char *arg)
1849 audio_channels = atoi(arg);
1852 static void opt_video_device(const char *arg)
1854 video_device = av_strdup(arg);
1857 static void opt_video_channel(const char *arg)
1859 video_channel = strtol(arg, NULL, 0);
1862 static void opt_video_standard(const char *arg)
1864 video_standard = av_strdup(arg);
1867 static void opt_audio_device(const char *arg)
1869 audio_device = av_strdup(arg);
1872 static void opt_dv1394(const char *arg)
1874 video_grab_format = "dv1394";
1875 audio_grab_format = NULL;
1878 static void opt_audio_codec(const char *arg)
1882 if (!strcmp(arg, "copy")) {
1883 audio_stream_copy = 1;
1887 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1892 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1895 audio_codec_id = p->id;
1900 static void add_frame_hooker(const char *arg)
1905 char *args = av_strdup(arg);
1907 argv[0] = strtok(args, " ");
1908 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
1911 i = frame_hook_add(argc, argv);
1914 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
1919 const char *motion_str[] = {
1929 static void opt_motion_estimation(const char *arg)
1935 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1938 if (!strcmp(*p, arg))
1942 me_method = (p - motion_str) + 1;
1945 static void opt_video_codec(const char *arg)
1949 if (!strcmp(arg, "copy")) {
1950 video_stream_copy = 1;
1954 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1959 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1962 video_codec_id = p->id;
1967 static void opt_map(const char *arg)
1973 m = &stream_maps[nb_stream_maps++];
1975 m->file_index = strtol(arg, (char **)&p, 0);
1979 m->stream_index = strtol(p, (char **)&p, 0);
1982 static void opt_recording_time(const char *arg)
1984 recording_time = parse_date(arg, 1);
1987 static void opt_input_file(const char *filename)
1989 AVFormatContext *ic;
1990 AVFormatParameters params, *ap = ¶ms;
1991 int err, i, ret, rfps, rfps_base;
1993 if (!strcmp(filename, "-"))
1996 /* get default parameters from command line */
1997 memset(ap, 0, sizeof(*ap));
1998 ap->sample_rate = audio_sample_rate;
1999 ap->channels = audio_channels;
2000 ap->frame_rate = frame_rate;
2001 ap->frame_rate_base = frame_rate_base;
2002 ap->width = frame_width;
2003 ap->height = frame_height;
2004 ap->image_format = image_format;
2005 ap->pix_fmt = frame_pix_fmt;
2007 /* open the input file with generic libav function */
2008 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
2010 print_error(filename, err);
2014 /* If not enough info to get the stream parameters, we decode the
2015 first frames to get it. (used in mpeg case for example) */
2016 ret = av_find_stream_info(ic);
2018 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2022 /* update the current parameters so that they match the one of the input stream */
2023 for(i=0;i<ic->nb_streams;i++) {
2024 AVCodecContext *enc = &ic->streams[i]->codec;
2025 switch(enc->codec_type) {
2026 case CODEC_TYPE_AUDIO:
2027 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2028 audio_channels = enc->channels;
2029 audio_sample_rate = enc->sample_rate;
2031 case CODEC_TYPE_VIDEO:
2032 frame_height = enc->height;
2033 frame_width = enc->width;
2034 frame_aspect_ratio = enc->aspect_ratio;
2035 frame_pix_fmt = enc->pix_fmt;
2036 rfps = ic->streams[i]->r_frame_rate;
2037 rfps_base = ic->streams[i]->r_frame_rate_base;
2038 enc->workaround_bugs = workaround_bugs;
2039 enc->error_resilience = error_resilience;
2040 enc->error_concealment = error_concealment;
2041 enc->idct_algo= idct_algo;
2043 /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
2044 enc->flags|= CODEC_FLAG_TRUNCATED; */
2045 if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO || enc->codec_id==CODEC_ID_H264)
2046 enc->flags|= CODEC_FLAG_TRUNCATED;
2049 enc->flags|= CODEC_FLAG_BITEXACT;
2051 assert(enc->frame_rate_base == rfps_base); // should be true for now
2052 if (enc->frame_rate != rfps) {
2053 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2054 i, (float)enc->frame_rate / enc->frame_rate_base,
2055 (float)rfps / rfps_base);
2057 /* update the current frame rate to match the stream frame rate */
2059 frame_rate_base = rfps_base;
2061 enc->rate_emu = rate_emu;
2068 input_files[nb_input_files] = ic;
2069 /* dump the file content */
2070 dump_format(ic, nb_input_files, filename, 0);
2072 file_iformat = NULL;
2073 file_oformat = NULL;
2074 image_format = NULL;
2079 static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2081 int has_video, has_audio, i, j;
2082 AVFormatContext *ic;
2086 for(j=0;j<nb_input_files;j++) {
2087 ic = input_files[j];
2088 for(i=0;i<ic->nb_streams;i++) {
2089 AVCodecContext *enc = &ic->streams[i]->codec;
2090 switch(enc->codec_type) {
2091 case CODEC_TYPE_AUDIO:
2094 case CODEC_TYPE_VIDEO:
2102 *has_video_ptr = has_video;
2103 *has_audio_ptr = has_audio;
2106 static void opt_output_file(const char *filename)
2109 AVFormatContext *oc;
2110 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2112 AVFormatParameters params, *ap = ¶ms;
2114 if (!strcmp(filename, "-"))
2117 oc = av_mallocz(sizeof(AVFormatContext));
2119 if (!file_oformat) {
2120 file_oformat = guess_format(NULL, filename, NULL);
2121 if (!file_oformat) {
2122 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2128 oc->oformat = file_oformat;
2130 if (!strcmp(file_oformat->name, "ffm") &&
2131 strstart(filename, "http:", NULL)) {
2132 /* special case for files sent to ffserver: we get the stream
2133 parameters from ffserver */
2134 if (read_ffserver_streams(oc, filename) < 0) {
2135 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2139 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2140 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2142 /* disable if no corresponding type found and at least one
2144 if (nb_input_files > 0) {
2145 check_audio_video_inputs(&input_has_video, &input_has_audio);
2146 if (!input_has_video)
2148 if (!input_has_audio)
2152 /* manual disable */
2153 if (audio_disable) {
2156 if (video_disable) {
2162 AVCodecContext *video_enc;
2164 st = av_mallocz(sizeof(AVStream));
2166 fprintf(stderr, "Could not alloc stream\n");
2169 avcodec_get_context_defaults(&st->codec);
2171 video_enc = &st->codec;
2172 if (video_stream_copy) {
2173 st->stream_copy = 1;
2174 video_enc->codec_type = CODEC_TYPE_VIDEO;
2179 codec_id = file_oformat->video_codec;
2180 if (video_codec_id != CODEC_ID_NONE)
2181 codec_id = video_codec_id;
2183 video_enc->codec_id = codec_id;
2185 video_enc->bit_rate = video_bit_rate;
2186 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2187 video_enc->frame_rate = frame_rate;
2188 video_enc->frame_rate_base = frame_rate_base;
2190 video_enc->width = frame_width;
2191 video_enc->height = frame_height;
2192 video_enc->aspect_ratio = frame_aspect_ratio;
2193 video_enc->pix_fmt = frame_pix_fmt;
2196 video_enc->gop_size = gop_size;
2198 video_enc->gop_size = 0;
2199 if (video_qscale || same_quality) {
2200 video_enc->flags |= CODEC_FLAG_QSCALE;
2201 st->quality = video_qscale;
2205 video_enc->flags |= CODEC_FLAG_BITEXACT;
2208 video_enc->flags |= CODEC_FLAG_HQ;
2212 video_enc->flags |= CODEC_FLAG_H263P_UMV;
2215 video_enc->flags |= CODEC_FLAG_H263P_AIC;
2219 video_enc->flags |= CODEC_FLAG_HQ;
2220 video_enc->flags |= CODEC_FLAG_4MV;
2224 video_enc->flags |= CODEC_FLAG_PART;
2228 video_enc->max_b_frames = b_frames;
2229 video_enc->b_frame_strategy = 0;
2230 video_enc->b_quant_factor = 2.0;
2233 video_enc->qmin = video_qmin;
2234 video_enc->qmax = video_qmax;
2235 video_enc->mb_qmin = video_mb_qmin;
2236 video_enc->mb_qmax = video_mb_qmax;
2237 video_enc->max_qdiff = video_qdiff;
2238 video_enc->qblur = video_qblur;
2239 video_enc->qcompress = video_qcomp;
2240 video_enc->rc_eq = video_rc_eq;
2241 video_enc->debug= debug;
2243 p= video_rc_override_string;
2246 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2248 fprintf(stderr, "error parsing rc_override\n");
2251 video_enc->rc_override=
2252 av_realloc(video_enc->rc_override,
2253 sizeof(RcOverride)*(i+1));
2254 video_enc->rc_override[i].start_frame= start;
2255 video_enc->rc_override[i].end_frame = end;
2257 video_enc->rc_override[i].qscale= q;
2258 video_enc->rc_override[i].quality_factor= 1.0;
2261 video_enc->rc_override[i].qscale= 0;
2262 video_enc->rc_override[i].quality_factor= -q/100.0;
2267 video_enc->rc_override_count=i;
2269 video_enc->rc_max_rate = video_rc_max_rate;
2270 video_enc->rc_min_rate = video_rc_min_rate;
2271 video_enc->rc_buffer_size = video_rc_buffer_size;
2272 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2273 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2274 video_enc->i_quant_factor = video_i_qfactor;
2275 video_enc->b_quant_factor = video_b_qfactor;
2276 video_enc->i_quant_offset = video_i_qoffset;
2277 video_enc->b_quant_offset = video_b_qoffset;
2278 video_enc->dct_algo = dct_algo;
2279 video_enc->idct_algo = idct_algo;
2280 video_enc->strict_std_compliance = strict;
2282 video_enc->rtp_mode= 1;
2283 video_enc->rtp_payload_size= packet_size;
2287 video_enc->flags|= CODEC_FLAG_PSNR;
2289 video_enc->me_method = me_method;
2294 video_enc->flags |= CODEC_FLAG_PASS1;
2296 video_enc->flags |= CODEC_FLAG_PASS2;
2300 oc->streams[nb_streams] = st;
2305 AVCodecContext *audio_enc;
2307 st = av_mallocz(sizeof(AVStream));
2309 fprintf(stderr, "Could not alloc stream\n");
2312 avcodec_get_context_defaults(&st->codec);
2314 audio_enc = &st->codec;
2315 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2316 if (audio_stream_copy) {
2317 st->stream_copy = 1;
2319 codec_id = file_oformat->audio_codec;
2320 if (audio_codec_id != CODEC_ID_NONE)
2321 codec_id = audio_codec_id;
2322 audio_enc->codec_id = codec_id;
2324 audio_enc->bit_rate = audio_bit_rate;
2325 audio_enc->sample_rate = audio_sample_rate;
2326 /* For audio codecs other than AC3 we limit */
2327 /* the number of coded channels to stereo */
2328 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2329 audio_enc->channels = 2;
2331 audio_enc->channels = audio_channels;
2333 oc->streams[nb_streams] = st;
2337 oc->nb_streams = nb_streams;
2340 fprintf(stderr, "No audio or video streams available\n");
2345 pstrcpy(oc->title, sizeof(oc->title), str_title);
2347 pstrcpy(oc->author, sizeof(oc->author), str_author);
2349 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2351 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2354 output_files[nb_output_files++] = oc;
2356 strcpy(oc->filename, filename);
2358 /* check filename in case of an image number is expected */
2359 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2360 if (filename_number_test(oc->filename) < 0) {
2361 print_error(oc->filename, AVERROR_NUMEXPECTED);
2366 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2367 /* test if it already exists to avoid loosing precious files */
2368 if (!file_overwrite &&
2369 (strchr(filename, ':') == NULL ||
2370 strstart(filename, "file:", NULL))) {
2371 if (url_exist(filename)) {
2374 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2377 if (toupper(c) != 'Y') {
2378 fprintf(stderr, "Not overwriting - exiting\n");
2385 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2386 fprintf(stderr, "Could not open '%s'\n", filename);
2391 memset(ap, 0, sizeof(*ap));
2392 ap->image_format = image_format;
2393 if (av_set_parameters(oc, ap) < 0) {
2394 fprintf(stderr, "%s: Invalid encoding parameters\n",
2399 /* reset some options */
2400 file_oformat = NULL;
2401 file_iformat = NULL;
2402 image_format = NULL;
2405 audio_codec_id = CODEC_ID_NONE;
2406 video_codec_id = CODEC_ID_NONE;
2407 audio_stream_copy = 0;
2408 video_stream_copy = 0;
2411 /* prepare dummy protocols for grab */
2412 static void prepare_grab(void)
2414 int has_video, has_audio, i, j;
2415 AVFormatContext *oc;
2416 AVFormatContext *ic;
2417 AVFormatParameters vp1, *vp = &vp1;
2418 AVFormatParameters ap1, *ap = &ap1;
2420 /* see if audio/video inputs are needed */
2423 memset(ap, 0, sizeof(*ap));
2424 memset(vp, 0, sizeof(*vp));
2425 for(j=0;j<nb_output_files;j++) {
2426 oc = output_files[j];
2427 for(i=0;i<oc->nb_streams;i++) {
2428 AVCodecContext *enc = &oc->streams[i]->codec;
2429 switch(enc->codec_type) {
2430 case CODEC_TYPE_AUDIO:
2431 if (enc->sample_rate > ap->sample_rate)
2432 ap->sample_rate = enc->sample_rate;
2433 if (enc->channels > ap->channels)
2434 ap->channels = enc->channels;
2437 case CODEC_TYPE_VIDEO:
2438 if (enc->width > vp->width)
2439 vp->width = enc->width;
2440 if (enc->height > vp->height)
2441 vp->height = enc->height;
2443 assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
2444 if (enc->frame_rate > vp->frame_rate){
2445 vp->frame_rate = enc->frame_rate;
2446 vp->frame_rate_base = enc->frame_rate_base;
2456 if (has_video == 0 && has_audio == 0) {
2457 fprintf(stderr, "Output file must have at least one audio or video stream\n");
2462 AVInputFormat *fmt1;
2463 fmt1 = av_find_input_format(video_grab_format);
2464 vp->device = video_device;
2465 vp->channel = video_channel;
2466 vp->standard = video_standard;
2467 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
2468 fprintf(stderr, "Could not find video grab device\n");
2471 /* by now video grab has one stream */
2472 ic->streams[0]->r_frame_rate = vp->frame_rate;
2473 ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
2474 input_files[nb_input_files] = ic;
2475 dump_format(ic, nb_input_files, "", 0);
2478 if (has_audio && audio_grab_format) {
2479 AVInputFormat *fmt1;
2480 fmt1 = av_find_input_format(audio_grab_format);
2481 ap->device = audio_device;
2482 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2483 fprintf(stderr, "Could not find audio grab device\n");
2486 input_files[nb_input_files] = ic;
2487 dump_format(ic, nb_input_files, "", 0);
2492 /* same option as mencoder */
2493 static void opt_pass(const char *pass_str)
2496 pass = atoi(pass_str);
2497 if (pass != 1 && pass != 2) {
2498 fprintf(stderr, "pass number can be only 1 or 2\n");
2504 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
2505 static int64_t getutime(void)
2507 return av_gettime();
2510 static int64_t getutime(void)
2512 struct rusage rusage;
2514 getrusage(RUSAGE_SELF, &rusage);
2515 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2519 extern int ffm_nopts;
2521 static void opt_bitexact(void)
2524 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2528 static void show_formats(void)
2530 AVInputFormat *ifmt;
2531 AVOutputFormat *ofmt;
2532 AVImageFormat *image_fmt;
2537 printf("Output audio/video file formats:");
2538 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2539 printf(" %s", ofmt->name);
2543 printf("Input audio/video file formats:");
2544 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2545 printf(" %s", ifmt->name);
2549 printf("Output image formats:");
2550 for(image_fmt = first_image_format; image_fmt != NULL;
2551 image_fmt = image_fmt->next) {
2552 if (image_fmt->img_write)
2553 printf(" %s", image_fmt->name);
2557 printf("Input image formats:");
2558 for(image_fmt = first_image_format; image_fmt != NULL;
2559 image_fmt = image_fmt->next) {
2560 if (image_fmt->img_read)
2561 printf(" %s", image_fmt->name);
2565 printf("Codecs:\n");
2566 printf(" Encoders:");
2567 for(p = first_avcodec; p != NULL; p = p->next) {
2569 printf(" %s", p->name);
2573 printf(" Decoders:");
2574 for(p = first_avcodec; p != NULL; p = p->next) {
2576 printf(" %s", p->name);
2580 printf("Supported file protocols:");
2581 for(up = first_protocol; up != NULL; up = up->next)
2582 printf(" %s:", up->name);
2585 printf("Frame size abbreviations: sqcif qcif cif 4cif\n");
2586 printf("Motion estimation methods:");
2590 if ((pp - motion_str + 1) == ME_ZERO)
2591 printf("(fastest)");
2592 else if ((pp - motion_str + 1) == ME_FULL)
2593 printf("(slowest)");
2594 else if ((pp - motion_str + 1) == ME_EPZS)
2595 printf("(default)");
2602 const OptionDef options[] = {
2603 { "L", 0, {(void*)show_licence}, "show license" },
2604 { "h", 0, {(void*)show_help}, "show help" },
2605 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2606 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2607 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
2608 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2609 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2610 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2611 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2612 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2613 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2614 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2615 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2616 { "pass", HAS_ARG, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
2617 { "passlogfile", HAS_ARG | OPT_STRING, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
2619 { "b", HAS_ARG, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2620 { "r", HAS_ARG, {(void*)opt_frame_rate}, "set frame rate (in Hz)", "rate" },
2621 { "re", OPT_BOOL|OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate" },
2622 { "s", HAS_ARG, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2623 { "aspect", HAS_ARG, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
2624 { "pix_fmt", HAS_ARG | OPT_EXPERT, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
2625 { "croptop", HAS_ARG, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
2626 { "cropbottom", HAS_ARG, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
2627 { "cropleft", HAS_ARG, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
2628 { "cropright", HAS_ARG, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
2629 { "g", HAS_ARG | OPT_EXPERT, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2630 { "intra", OPT_BOOL | OPT_EXPERT, {(void*)&intra_only}, "use only intra frames"},
2631 { "vn", OPT_BOOL, {(void*)&video_disable}, "disable video" },
2632 { "qscale", HAS_ARG | OPT_EXPERT, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2633 { "qmin", HAS_ARG | OPT_EXPERT, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2634 { "qmax", HAS_ARG | OPT_EXPERT, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2635 { "mbqmin", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
2636 { "mbqmax", HAS_ARG | OPT_EXPERT, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
2637 { "qdiff", HAS_ARG | OPT_EXPERT, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2638 { "qblur", HAS_ARG | OPT_EXPERT, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2639 { "qcomp", HAS_ARG | OPT_EXPERT, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2640 { "rc_init_cplx", HAS_ARG | OPT_EXPERT, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
2641 { "b_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
2642 { "i_qfactor", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
2643 { "b_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
2644 { "i_qoffset", HAS_ARG | OPT_EXPERT, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
2645 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
2646 { "rc_eq", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_eq}, "", "equation" },
2647 { "rc_override", HAS_ARG | OPT_EXPERT, {(void*)opt_video_rc_override_string}, "Rate control override", "qualities for specific intervals" },
2648 { "bt", HAS_ARG, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2649 { "maxrate", HAS_ARG, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2650 { "minrate", HAS_ARG, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2651 { "bufsize", HAS_ARG, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
2652 { "vd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_device}, "set video grab device", "device" },
2653 { "vc", HAS_ARG | OPT_EXPERT, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
2654 { "tvstd", HAS_ARG | OPT_EXPERT, {(void*)opt_video_standard}, "set television standard (NTSC, PAL (SECAM))", "standard" },
2655 { "dv1394", OPT_EXPERT, {(void*)opt_dv1394}, "set DV1394 grab", "" },
2656 { "vcodec", HAS_ARG | OPT_EXPERT, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
2657 { "me", HAS_ARG | OPT_EXPERT, {(void*)opt_motion_estimation}, "set motion estimation method",
2659 { "dct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2660 { "idct_algo", HAS_ARG | OPT_EXPERT, {(void*)opt_idct_algo}, "set idct algo", "algo" },
2661 { "er", HAS_ARG | OPT_EXPERT, {(void*)opt_error_resilience}, "set error resilience", "" },
2662 { "ec", HAS_ARG | OPT_EXPERT, {(void*)opt_error_concealment}, "set error concealment", "" },
2663 { "bf", HAS_ARG | OPT_EXPERT, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2664 { "hq", OPT_BOOL | OPT_EXPERT, {(void*)&use_hq}, "activate high quality settings" },
2665 { "4mv", OPT_BOOL | OPT_EXPERT, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2666 { "part", OPT_BOOL | OPT_EXPERT, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
2667 { "bug", HAS_ARG | OPT_EXPERT, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2668 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "packet size", "size in bits" },
2669 { "strict", HAS_ARG | OPT_EXPERT, {(void*)opt_strict}, "strictness", "how strictly to follow the standarts" },
2670 { "sameq", OPT_BOOL, {(void*)&same_quality},
2671 "use same video quality as source (implies VBR)" },
2672 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
2674 { "ab", HAS_ARG, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2675 { "ar", HAS_ARG, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2676 { "ac", HAS_ARG, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2677 { "an", OPT_BOOL, {(void*)&audio_disable}, "disable audio" },
2678 { "ad", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_device}, "set audio device", "device" },
2679 { "acodec", HAS_ARG | OPT_EXPERT, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
2680 { "deinterlace", OPT_BOOL | OPT_EXPERT, {(void*)&do_deinterlace},
2681 "deinterlace pictures" },
2682 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2683 "add timings for benchmarking" },
2684 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2685 "dump each input packet" },
2686 { "psnr", OPT_BOOL | OPT_EXPERT, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2687 { "vstats", OPT_BOOL | OPT_EXPERT, {(void*)&do_vstats}, "dump video coding statistics to file" },
2688 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2689 { "vhook", HAS_ARG | OPT_EXPERT, {(void*)add_frame_hooker}, "insert video processing module", "module name and parameters" },
2691 { "aic", OPT_BOOL | OPT_EXPERT, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
2692 { "umv", OPT_BOOL | OPT_EXPERT, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
2697 void show_help(void)
2699 printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Fabrice Bellard\n");
2700 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2701 "Hyper fast Audio and Video encoder\n");
2703 show_help_options(options);
2707 void parse_arg_file(const char *filename)
2709 opt_output_file(filename);
2712 int main(int argc, char **argv)
2723 parse_options(argc, argv, options);
2725 /* file converter / grab */
2726 if (nb_output_files <= 0) {
2727 fprintf(stderr, "Must supply at least one output file\n");
2731 if (nb_input_files == 0) {
2736 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2737 stream_maps, nb_stream_maps);
2738 ti = getutime() - ti;
2740 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2744 for(i=0;i<nb_output_files;i++) {
2745 /* maybe av_close_output_file ??? */
2746 AVFormatContext *s = output_files[i];
2748 if (!(s->oformat->flags & AVFMT_NOFILE))
2750 for(j=0;j<s->nb_streams;j++)
2751 av_free(s->streams[j]);
2754 for(i=0;i<nb_input_files;i++)
2755 av_close_input_file(input_files[i]);
2760 #ifdef POWERPC_PERFORMANCE_REPORT
2761 extern void powerpc_display_perf_report(void);
2762 powerpc_display_perf_report();
2763 #endif /* POWERPC_PERFORMANCE_REPORT */
2765 #ifndef CONFIG_WIN32
2766 if (received_sigterm) {
2768 "Received signal %d: terminating.\n",
2769 (int) received_sigterm);
2773 exit(0); /* not all OS-es handle main() return value */