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 /* select an input stream for an output stream */
50 typedef struct AVStreamMap {
55 extern const OptionDef options[];
57 static void show_help(void);
58 static void show_license(void);
62 static AVFormatContext *input_files[MAX_FILES];
63 static int nb_input_files = 0;
65 static AVFormatContext *output_files[MAX_FILES];
66 static int nb_output_files = 0;
68 static AVStreamMap stream_maps[MAX_FILES];
69 static int nb_stream_maps;
71 static AVInputFormat *file_iformat;
72 static AVOutputFormat *file_oformat;
73 static AVImageFormat *image_format;
74 static int frame_width = 160;
75 static int frame_height = 128;
76 static float frame_aspect_ratio = 0;
77 static enum PixelFormat frame_pix_fmt = PIX_FMT_YUV420P;
78 static int frame_topBand = 0;
79 static int frame_bottomBand = 0;
80 static int frame_leftBand = 0;
81 static int frame_rightBand = 0;
82 static int frame_rate = 25;
83 static int frame_rate_base = 1;
84 static int video_bit_rate = 200*1000;
85 static int video_bit_rate_tolerance = 4000*1000;
86 static int video_qscale = 0;
87 static int video_qmin = 2;
88 static int video_qmax = 31;
89 static int video_mb_qmin = 2;
90 static int video_mb_qmax = 31;
91 static int video_qdiff = 3;
92 static float video_qblur = 0.5;
93 static float video_qcomp = 0.5;
94 #if 0 //experimental, (can be removed)
95 static float video_rc_qsquish=1.0;
96 static float video_rc_qmod_amp=0;
97 static int video_rc_qmod_freq=0;
99 static char *video_rc_override_string=NULL;
100 static char *video_rc_eq="tex^qComp";
101 static int video_rc_buffer_size=0;
102 static float video_rc_buffer_aggressivity=1.0;
103 static int video_rc_max_rate=0;
104 static int video_rc_min_rate=0;
105 static float video_rc_initial_cplx=0;
106 static float video_b_qfactor = 1.25;
107 static float video_b_qoffset = 1.25;
108 static float video_i_qfactor = -0.8;
109 static float video_i_qoffset = 0.0;
110 static int me_method = ME_EPZS;
111 static int video_disable = 0;
112 static int video_codec_id = CODEC_ID_NONE;
113 static int same_quality = 0;
114 static int b_frames = 0;
115 static int mb_decision = FF_MB_DECISION_SIMPLE;
116 static int use_4mv = 0;
117 static int use_aic = 0;
118 static int use_umv = 0;
119 static int do_deinterlace = 0;
120 static int workaround_bugs = FF_BUG_AUTODETECT;
121 static int error_resilience = 2;
122 static int error_concealment = 3;
123 static int dct_algo = 0;
124 static int idct_algo = 0;
125 static int use_part = 0;
126 static int packet_size = 0;
127 static int strict = 0;
128 static int debug = 0;
129 extern int loop_input; /* currently a hack */
131 static int gop_size = 12;
132 static int intra_only = 0;
133 static int audio_sample_rate = 44100;
134 static int audio_bit_rate = 64000;
135 static int audio_disable = 0;
136 static int audio_channels = 1;
137 static int audio_codec_id = CODEC_ID_NONE;
139 static int64_t recording_time = 0;
140 static int file_overwrite = 0;
141 static char *str_title = NULL;
142 static char *str_author = NULL;
143 static char *str_copyright = NULL;
144 static char *str_comment = NULL;
145 static int do_benchmark = 0;
146 static int do_hex_dump = 0;
147 static int do_psnr = 0;
148 static int do_vstats = 0;
149 static int do_pass = 0;
150 static int bitexact = 0;
151 static char *pass_logfilename = NULL;
152 static int audio_stream_copy = 0;
153 static int video_stream_copy = 0;
155 static int rate_emu = 0;
157 static char *video_grab_format = "video4linux";
158 static char *video_device = NULL;
159 static int video_channel = 0;
160 static char *video_standard = "ntsc";
162 static char *audio_grab_format = "audio_device";
163 static char *audio_device = NULL;
165 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
167 typedef struct AVOutputStream {
168 int file_index; /* file index */
169 int index; /* stream index in the output file */
170 int source_index; /* AVInputStream index */
171 AVStream *st; /* stream in the output file */
172 int encoding_needed; /* true if encoding needed for this stream */
174 /* input pts and corresponding output pts
177 double sync_ipts_offset;
180 int video_resample; /* video_resample and video_crop are mutually exclusive */
181 AVPicture pict_tmp; /* temporary image for resampling */
182 ImgReSampleContext *img_resample_ctx; /* for image resampling */
184 int video_crop; /* video_resample and video_crop are mutually exclusive */
185 int topBand; /* cropping area sizes */
190 ReSampleContext *resample; /* for audio resampling */
191 FifoBuffer fifo; /* for compression: one audio fifo per codec */
195 typedef struct AVInputStream {
199 int discard; /* true if stream data should be discarded */
200 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
201 int64_t sample_index; /* current sample */
202 int frame_decoded; /* true if a video or audio frame has been decoded */
204 int64_t start; /* time when read started */
205 unsigned long frame; /* current frame */
206 AVFrac next_pts; /* synthetic pts for cases where pkt.pts == 0 */
207 int64_t pts; /* current pts */
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, av_delay;
476 adelta = audio_sync->sync_ipts - ((double)audio_sync->sync_opts *
477 s->pts_num / s->pts_den);
479 vdelta = ost->sync_ipts - ((double)ost->sync_opts *
480 s->pts_num / s->pts_den);
482 av_delay = adelta - vdelta;
483 // printf("delay=%f\n", av_delay);
484 if (av_delay < -AV_DELAY_MAX)
486 else if (av_delay > AV_DELAY_MAX)
491 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
492 if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
493 if (vdelta < -AV_DELAY_MAX)
495 else if (vdelta > AV_DELAY_MAX)
498 ost->sync_ipts_offset -= vdelta;
499 if (!ost->sync_ipts_offset)
500 ost->sync_ipts_offset = 0.000001; /* one microsecond */
504 #if defined(AVSYNC_DEBUG)
505 static char *action[] = { "drop frame", "copy frame", "dup frame" };
507 fprintf(stderr, "Input APTS %12.6f, output APTS %12.6f, ",
508 (double) audio_sync->sync_ipts,
509 (double) audio_sync->st->pts.val * s->pts_num / s->pts_den);
510 fprintf(stderr, "Input VPTS %12.6f, output VPTS %12.6f: %s\n",
511 (double) ost->sync_ipts,
512 (double) ost->st->pts.val * s->pts_num / s->pts_den,
520 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
524 /* convert pixel format if needed */
525 if (enc->pix_fmt != dec->pix_fmt) {
528 /* create temporary picture */
529 size = avpicture_get_size(enc->pix_fmt, dec->width, dec->height);
530 buf = av_malloc(size);
533 formatted_picture = &picture_format_temp;
534 avpicture_fill(formatted_picture, buf, enc->pix_fmt, dec->width, dec->height);
536 if (img_convert(formatted_picture, enc->pix_fmt,
537 in_picture, dec->pix_fmt,
538 dec->width, dec->height) < 0) {
539 fprintf(stderr, "pixel format conversion not handled\n");
543 formatted_picture = in_picture;
546 /* XXX: resampling could be done before raw format convertion in
547 some cases to go faster */
548 /* XXX: only works for YUV420P */
549 if (ost->video_resample) {
550 final_picture = &ost->pict_tmp;
551 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
552 } else if (ost->video_crop) {
553 picture_crop_temp.data[0] = formatted_picture->data[0] +
554 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
556 picture_crop_temp.data[1] = formatted_picture->data[1] +
557 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
558 (ost->leftBand >> 1);
560 picture_crop_temp.data[2] = formatted_picture->data[2] +
561 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
562 (ost->leftBand >> 1);
564 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
565 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
566 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
567 final_picture = &picture_crop_temp;
569 final_picture = formatted_picture;
571 /* duplicates frame if needed */
572 /* XXX: pb because no interleaving */
573 for(i=0;i<nb_frames;i++) {
574 if (s->oformat->flags & AVFMT_RAWPICTURE) {
575 /* raw pictures are written as AVPicture structure to
576 avoid any copies. We support temorarily the older
578 av_write_frame(s, ost->index,
579 (uint8_t *)final_picture, sizeof(AVPicture));
583 memset(&big_picture, 0, sizeof(AVFrame));
584 *(AVPicture*)&big_picture= *final_picture;
586 /* handles sameq here. This is not correct because it may
587 not be a global option */
589 big_picture.quality = ist->st->quality;
591 big_picture.quality = ost->st->quality;
593 ret = avcodec_encode_video(enc,
594 video_buffer, VIDEO_BUFFER_SIZE,
596 //enc->frame_number = enc->real_pict_num;
597 av_write_frame(s, ost->index, video_buffer, ret);
599 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
600 // enc->frame_number-1, enc->real_pict_num, ret,
602 /* if two pass, output log */
603 if (ost->logfile && enc->stats_out) {
604 fprintf(ost->logfile, "%s", enc->stats_out);
614 static double psnr(double d){
615 if(d==0) return INFINITY;
616 return -10.0*log(d)/log(10.0);
619 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
622 static FILE *fvstats=NULL;
623 static int64_t total_size = 0;
630 double ti1, bitrate, avg_bitrate;
634 today = localtime(&today2);
635 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
638 fvstats = fopen(filename,"w");
646 enc = &ost->st->codec;
647 total_size += frame_size;
648 if (enc->codec_type == CODEC_TYPE_VIDEO) {
649 frame_number = ost->frame_number;
650 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality);
651 if (enc->flags&CODEC_FLAG_PSNR)
652 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
654 fprintf(fvstats,"f_size= %6d ", frame_size);
655 /* compute pts value */
656 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
660 bitrate = (double)(frame_size * 8) * enc->frame_rate / enc->frame_rate_base / 1000.0;
661 avg_bitrate = (double)(total_size * 8) / ti1 / 1000.0;
662 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
663 (double)total_size / 1024, ti1, bitrate, avg_bitrate);
664 fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
668 static void print_report(AVFormatContext **output_files,
669 AVOutputStream **ost_table, int nb_ostreams,
674 AVFormatContext *oc, *os;
677 int frame_number, vid, i;
678 double bitrate, ti1, pts;
679 static int64_t last_time = -1;
681 if (!is_last_report) {
683 /* display the report every 0.5 seconds */
684 cur_time = av_gettime();
685 if (last_time == -1) {
686 last_time = cur_time;
689 if ((cur_time - last_time) < 500000)
691 last_time = cur_time;
695 oc = output_files[0];
697 total_size = url_ftell(&oc->pb);
702 for(i=0;i<nb_ostreams;i++) {
704 os = output_files[ost->file_index];
705 enc = &ost->st->codec;
706 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
707 sprintf(buf + strlen(buf), "q=%2.1f ",
708 enc->coded_frame->quality);
710 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
711 frame_number = ost->frame_number;
712 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
713 frame_number, enc->coded_frame ? enc->coded_frame->quality : 0);
714 if (enc->flags&CODEC_FLAG_PSNR)
715 sprintf(buf + strlen(buf), "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
718 /* compute min output value */
719 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
720 if ((pts < ti1) && (pts > 0))
725 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
727 sprintf(buf + strlen(buf),
728 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
729 (double)total_size / 1024, ti1, bitrate);
731 fprintf(stderr, "%s ", buf);
733 if (is_last_report) {
734 fprintf(stderr, "\n");
736 fprintf(stderr, "\r");
742 * The following code is the main loop of the file converter
744 static int av_encode(AVFormatContext **output_files,
746 AVFormatContext **input_files,
748 AVStreamMap *stream_maps, int nb_stream_maps)
750 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
751 AVFormatContext *is, *os;
752 AVCodecContext *codec, *icodec;
753 AVOutputStream *ost, **ost_table = NULL;
754 AVInputStream *ist, **ist_table = NULL;
755 AVInputFile *file_table;
756 AVFormatContext *stream_no_data;
759 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
763 /* input stream init */
765 for(i=0;i<nb_input_files;i++) {
767 file_table[i].ist_index = j;
768 file_table[i].nb_streams = is->nb_streams;
773 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
777 for(i=0;i<nb_istreams;i++) {
778 ist = av_mallocz(sizeof(AVInputStream));
784 for(i=0;i<nb_input_files;i++) {
786 for(k=0;k<is->nb_streams;k++) {
787 ist = ist_table[j++];
788 ist->st = is->streams[k];
791 ist->discard = 1; /* the stream is discarded by default
794 if (ist->st->codec.rate_emu) {
795 ist->start = av_gettime();
801 /* output stream init */
803 for(i=0;i<nb_output_files;i++) {
804 os = output_files[i];
805 nb_ostreams += os->nb_streams;
807 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
808 fprintf(stderr, "Number of stream maps must match number of output streams\n");
812 /* Sanity check the mapping args -- do the input files & streams exist? */
813 for(i=0;i<nb_stream_maps;i++) {
814 int fi = stream_maps[i].file_index;
815 int si = stream_maps[i].stream_index;
817 if (fi < 0 || fi > nb_input_files - 1 ||
818 si < 0 || si > file_table[fi].nb_streams - 1) {
819 fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
824 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
827 for(i=0;i<nb_ostreams;i++) {
828 ost = av_mallocz(sizeof(AVOutputStream));
835 for(k=0;k<nb_output_files;k++) {
836 os = output_files[k];
837 for(i=0;i<os->nb_streams;i++) {
839 ost = ost_table[n++];
842 ost->st = os->streams[i];
843 if (nb_stream_maps > 0) {
844 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
845 stream_maps[n-1].stream_index;
847 /* Sanity check that the stream types match */
848 if (ist_table[ost->source_index]->st->codec.codec_type != ost->st->codec.codec_type) {
849 fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
850 stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
851 ost->file_index, ost->index);
856 /* get corresponding input stream index : we select the first one with the right type */
858 for(j=0;j<nb_istreams;j++) {
861 ist->st->codec.codec_type == ost->st->codec.codec_type) {
862 ost->source_index = j;
868 /* try again and reuse existing stream */
869 for(j=0;j<nb_istreams;j++) {
871 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
872 ost->source_index = j;
877 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
878 ost->file_index, ost->index);
883 ist = ist_table[ost->source_index];
888 /* for each output stream, we compute the right encoding parameters */
889 for(i=0;i<nb_ostreams;i++) {
891 ist = ist_table[ost->source_index];
893 codec = &ost->st->codec;
894 icodec = &ist->st->codec;
896 if (ost->st->stream_copy) {
897 /* if stream_copy is selected, no need to decode or encode */
898 codec->codec_id = icodec->codec_id;
899 codec->codec_type = icodec->codec_type;
900 codec->codec_tag = icodec->codec_tag;
901 codec->bit_rate = icodec->bit_rate;
902 switch(codec->codec_type) {
903 case CODEC_TYPE_AUDIO:
904 codec->sample_rate = icodec->sample_rate;
905 codec->channels = icodec->channels;
907 case CODEC_TYPE_VIDEO:
908 codec->frame_rate = icodec->frame_rate;
909 codec->frame_rate_base = icodec->frame_rate_base;
910 codec->width = icodec->width;
911 codec->height = icodec->height;
917 switch(codec->codec_type) {
918 case CODEC_TYPE_AUDIO:
919 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
922 if (codec->channels == icodec->channels &&
923 codec->sample_rate == icodec->sample_rate) {
924 ost->audio_resample = 0;
926 if (codec->channels != icodec->channels &&
927 icodec->codec_id == CODEC_ID_AC3) {
928 /* Special case for 5:1 AC3 input */
929 /* and mono or stereo output */
930 /* Request specific number of channels */
931 icodec->channels = codec->channels;
932 if (codec->sample_rate == icodec->sample_rate)
933 ost->audio_resample = 0;
935 ost->audio_resample = 1;
936 ost->resample = audio_resample_init(codec->channels, icodec->channels,
938 icodec->sample_rate);
941 printf("Can't resample. Aborting.\n");
945 /* Request specific number of channels */
946 icodec->channels = codec->channels;
948 ost->audio_resample = 1;
949 ost->resample = audio_resample_init(codec->channels, icodec->channels,
951 icodec->sample_rate);
954 printf("Can't resample. Aborting.\n");
959 ist->decoding_needed = 1;
960 ost->encoding_needed = 1;
962 case CODEC_TYPE_VIDEO:
963 if (codec->width == icodec->width &&
964 codec->height == icodec->height &&
965 frame_topBand == 0 &&
966 frame_bottomBand == 0 &&
967 frame_leftBand == 0 &&
968 frame_rightBand == 0)
970 ost->video_resample = 0;
972 } else if ((codec->width == icodec->width -
973 (frame_leftBand + frame_rightBand)) &&
974 (codec->height == icodec->height -
975 (frame_topBand + frame_bottomBand)))
977 ost->video_resample = 0;
979 ost->topBand = frame_topBand;
980 ost->leftBand = frame_leftBand;
983 ost->video_resample = 1;
984 ost->video_crop = 0; // cropping is handled as part of resample
985 buf = av_malloc((codec->width * codec->height * 3) / 2);
988 ost->pict_tmp.data[0] = buf;
989 ost->pict_tmp.data[1] = ost->pict_tmp.data[0] + (codec->width * codec->height);
990 ost->pict_tmp.data[2] = ost->pict_tmp.data[1] + (codec->width * codec->height) / 4;
991 ost->pict_tmp.linesize[0] = codec->width;
992 ost->pict_tmp.linesize[1] = codec->width / 2;
993 ost->pict_tmp.linesize[2] = codec->width / 2;
995 ost->img_resample_ctx = img_resample_full_init(
996 ost->st->codec.width, ost->st->codec.height,
997 ist->st->codec.width, ist->st->codec.height,
998 frame_topBand, frame_bottomBand,
999 frame_leftBand, frame_rightBand);
1001 ost->encoding_needed = 1;
1002 ist->decoding_needed = 1;
1008 if (ost->encoding_needed &&
1009 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1010 char logfilename[1024];
1015 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1017 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1018 if (codec->flags & CODEC_FLAG_PASS1) {
1019 f = fopen(logfilename, "w");
1021 perror(logfilename);
1026 /* read the log file */
1027 f = fopen(logfilename, "r");
1029 perror(logfilename);
1032 fseek(f, 0, SEEK_END);
1034 fseek(f, 0, SEEK_SET);
1035 logbuffer = av_malloc(size + 1);
1037 fprintf(stderr, "Could not allocate log buffer\n");
1040 fread(logbuffer, 1, size, f);
1042 logbuffer[size] = '\0';
1043 codec->stats_in = logbuffer;
1049 /* dump the file output parameters - cannot be done before in case
1051 for(i=0;i<nb_output_files;i++) {
1052 dump_format(output_files[i], i, output_files[i]->filename, 1);
1055 /* dump the stream mapping */
1056 fprintf(stderr, "Stream mapping:\n");
1057 for(i=0;i<nb_ostreams;i++) {
1059 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1060 ist_table[ost->source_index]->file_index,
1061 ist_table[ost->source_index]->index,
1066 /* open each encoder */
1067 for(i=0;i<nb_ostreams;i++) {
1069 if (ost->encoding_needed) {
1071 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1073 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1074 ost->file_index, ost->index);
1077 if (avcodec_open(&ost->st->codec, codec) < 0) {
1078 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1079 ost->file_index, ost->index);
1085 /* open each decoder */
1086 for(i=0;i<nb_istreams;i++) {
1088 if (ist->decoding_needed) {
1090 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1092 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1093 ist->st->codec.codec_id, ist->file_index, ist->index);
1096 if (avcodec_open(&ist->st->codec, codec) < 0) {
1097 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1098 ist->file_index, ist->index);
1101 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1102 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1103 ist->frame_decoded = 1;
1108 for(i=0;i<nb_istreams;i++) {
1110 is = input_files[ist->file_index];
1112 switch (ist->st->codec.codec_type) {
1113 case CODEC_TYPE_AUDIO:
1114 av_frac_init(&ist->next_pts,
1115 0, 0, is->pts_num * ist->st->codec.sample_rate);
1117 case CODEC_TYPE_VIDEO:
1118 av_frac_init(&ist->next_pts,
1119 0, 0, is->pts_num * ist->st->codec.frame_rate);
1126 /* compute buffer size max (should use a complete heuristic) */
1127 for(i=0;i<nb_input_files;i++) {
1128 file_table[i].buffer_size_max = 2048;
1131 /* open files and write file headers */
1132 for(i=0;i<nb_output_files;i++) {
1133 os = output_files[i];
1134 if (av_write_header(os) < 0) {
1135 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1141 #ifndef CONFIG_WIN32
1142 fprintf(stderr, "Press [q] to stop encoding\n");
1150 int file_index, ist_index;
1155 int data_size, got_picture;
1157 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
1158 void *buffer_to_free;
1162 /* if 'q' pressed, exits */
1164 /* read_key() returns 0 on EOF */
1170 /* select the stream that we must read now by looking at the
1171 smallest output pts */
1174 for(i=0;i<nb_ostreams;i++) {
1177 os = output_files[ost->file_index];
1178 ist = ist_table[ost->source_index];
1179 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1180 if (!file_table[ist->file_index].eof_reached &&
1183 file_index = ist->file_index;
1186 /* if none, if is finished */
1187 if (file_index < 0) {
1191 /* finish if recording time exhausted */
1192 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1195 /* read a packet from it and output it in the fifo */
1196 is = input_files[file_index];
1197 if (av_read_packet(is, &pkt) < 0) {
1198 file_table[file_index].eof_reached = 1;
1202 stream_no_data = is;
1207 printf("stream #%d, size=%d:\n", pkt.stream_index, pkt.size);
1208 av_hex_dump(pkt.data, pkt.size);
1210 /* the following test is needed in case new streams appear
1211 dynamically in stream : we ignore them */
1212 if (pkt.stream_index >= file_table[file_index].nb_streams)
1213 goto discard_packet;
1214 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1215 ist = ist_table[ist_index];
1217 goto discard_packet;
1219 // printf("read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1224 /* decode the packet if needed */
1225 data_buf = NULL; /* fail safe */
1227 if (ist->decoding_needed) {
1228 /* NOTE1: we only take into account the PTS if a new
1229 frame has begun (MPEG semantics) */
1230 /* NOTE2: even if the fraction is not initialized,
1231 av_frac_set can be used to set the integer part */
1232 if (ist->frame_decoded) {
1233 /* If pts is unavailable -- we have to use synthetic one */
1234 if( pkt.pts != AV_NOPTS_VALUE )
1236 ist->pts = ist->next_pts.val = pkt.pts;
1240 ist->pts = ist->next_pts.val;
1242 ist->frame_decoded = 0;
1245 switch(ist->st->codec.codec_type) {
1246 case CODEC_TYPE_AUDIO:
1247 /* XXX: could avoid copy if PCM 16 bits with same
1248 endianness as CPU */
1249 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1253 /* Some bug in mpeg audio decoder gives */
1254 /* data_size < 0, it seems they are overflows */
1255 if (data_size <= 0) {
1256 /* no audio frame */
1261 data_buf = (uint8_t *)samples;
1262 av_frac_add(&ist->next_pts,
1263 is->pts_den * data_size / (2 * ist->st->codec.channels));
1265 case CODEC_TYPE_VIDEO:
1267 AVFrame big_picture;
1269 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1270 ret = avcodec_decode_video(&ist->st->codec,
1271 &big_picture, &got_picture, ptr, len);
1272 picture= *(AVPicture*)&big_picture;
1273 ist->st->quality= big_picture.quality;
1276 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1277 ist->file_index, ist->index);
1278 av_free_packet(&pkt);
1282 /* no picture yet */
1287 av_frac_add(&ist->next_pts,
1288 is->pts_den * ist->st->codec.frame_rate_base);
1303 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1304 pre_process_video_frame(ist, &picture, &buffer_to_free);
1307 ist->frame_decoded = 1;
1309 /* frame rate emulation */
1310 if (ist->st->codec.rate_emu) {
1311 int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate);
1312 int64_t now = av_gettime() - ist->start;
1320 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1321 is the one of the next displayed one */
1322 /* XXX: add mpeg4 too ? */
1323 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1324 if (ist->st->codec.pict_type != B_TYPE) {
1326 tmp = ist->last_ip_pts;
1327 ist->last_ip_pts = ist->frac_pts.val;
1328 ist->frac_pts.val = tmp;
1332 /* transcode raw format, encode packets and output them */
1334 for(i=0;i<nb_ostreams;i++) {
1338 if (ost->source_index == ist_index) {
1339 os = output_files[ost->file_index];
1342 printf("%d: got pts=%f %f\n", i, pkt.pts / 90000.0,
1343 (ist->pts - ost->st->pts.val) / 90000.0);
1345 /* set the input output pts pairs */
1346 ost->sync_ipts = (double)ist->pts * is->pts_num /
1348 /* XXX: take into account the various fifos,
1349 in particular for audio */
1350 ost->sync_opts = ost->st->pts.val;
1351 //printf("ipts=%lld sync_ipts=%f sync_opts=%lld pts.val=%lld pkt.pts=%lld\n", ist->pts, ost->sync_ipts, ost->sync_opts, ost->st->pts.val, pkt.pts);
1353 if (ost->encoding_needed) {
1354 switch(ost->st->codec.codec_type) {
1355 case CODEC_TYPE_AUDIO:
1356 do_audio_out(os, ost, ist, data_buf, data_size);
1358 case CODEC_TYPE_VIDEO:
1359 /* find an audio stream for synchro */
1362 AVOutputStream *audio_sync, *ost1;
1364 for(i=0;i<nb_ostreams;i++) {
1365 ost1 = ost_table[i];
1366 if (ost1->file_index == ost->file_index &&
1367 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1373 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1374 if (do_vstats && frame_size)
1375 do_video_stats(os, ost, frame_size);
1384 /* no reencoding needed : output the packet directly */
1385 /* force the input stream PTS */
1387 memset(&avframe, 0, sizeof(AVFrame));
1388 ost->st->codec.coded_frame= &avframe;
1389 avframe.key_frame = pkt.flags & PKT_FLAG_KEY;
1391 av_write_frame(os, ost->index, data_buf, data_size);
1392 ost->st->codec.frame_number++;
1393 ost->frame_number++;
1397 av_free(buffer_to_free);
1400 av_free_packet(&pkt);
1402 /* dump report by using the output first video and audio streams */
1403 print_report(output_files, ost_table, nb_ostreams, 0);
1407 /* dump report by using the first video and audio streams */
1408 print_report(output_files, ost_table, nb_ostreams, 1);
1410 /* close each encoder */
1411 for(i=0;i<nb_ostreams;i++) {
1413 if (ost->encoding_needed) {
1414 av_freep(&ost->st->codec.stats_in);
1415 avcodec_close(&ost->st->codec);
1419 /* close each decoder */
1420 for(i=0;i<nb_istreams;i++) {
1422 if (ist->decoding_needed) {
1423 avcodec_close(&ist->st->codec);
1428 /* write the trailer if needed and close file */
1429 for(i=0;i<nb_output_files;i++) {
1430 os = output_files[i];
1431 av_write_trailer(os);
1437 av_free(file_table);
1440 for(i=0;i<nb_istreams;i++) {
1447 for(i=0;i<nb_ostreams;i++) {
1451 fclose(ost->logfile);
1452 ost->logfile = NULL;
1454 fifo_free(&ost->fifo); /* works even if fifo is not
1455 initialized but set to zero */
1456 av_free(ost->pict_tmp.data[0]);
1457 if (ost->video_resample)
1458 img_resample_close(ost->img_resample_ctx);
1459 if (ost->audio_resample)
1460 audio_resample_close(ost->resample);
1473 int file_read(const char *filename)
1476 unsigned char buffer[1024];
1479 if (url_open(&h, filename, O_RDONLY) < 0) {
1480 printf("could not open '%s'\n", filename);
1484 len = url_read(h, buffer, sizeof(buffer));
1487 for(i=0;i<len;i++) putchar(buffer[i]);
1494 static void opt_image_format(const char *arg)
1498 for(f = first_image_format; f != NULL; f = f->next) {
1499 if (!strcmp(arg, f->name))
1503 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1509 static void opt_format(const char *arg)
1511 /* compatibility stuff for pgmyuv */
1512 if (!strcmp(arg, "pgmyuv")) {
1513 opt_image_format(arg);
1517 file_iformat = av_find_input_format(arg);
1518 file_oformat = guess_format(arg, NULL, NULL);
1519 if (!file_iformat && !file_oformat) {
1520 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1525 static void opt_video_bitrate(const char *arg)
1527 video_bit_rate = atoi(arg) * 1000;
1530 static void opt_video_bitrate_tolerance(const char *arg)
1532 video_bit_rate_tolerance = atoi(arg) * 1000;
1535 static void opt_video_bitrate_max(const char *arg)
1537 video_rc_max_rate = atoi(arg) * 1000;
1540 static void opt_video_bitrate_min(const char *arg)
1542 video_rc_min_rate = atoi(arg) * 1000;
1545 static void opt_video_buffer_size(const char *arg)
1547 video_rc_buffer_size = atoi(arg) * 1000;
1550 static void opt_video_rc_eq(char *arg)
1555 static void opt_video_rc_override_string(char *arg)
1557 video_rc_override_string = arg;
1561 static void opt_workaround_bugs(const char *arg)
1563 workaround_bugs = atoi(arg);
1566 static void opt_dct_algo(const char *arg)
1568 dct_algo = atoi(arg);
1571 static void opt_idct_algo(const char *arg)
1573 idct_algo = atoi(arg);
1577 static void opt_error_resilience(const char *arg)
1579 error_resilience = atoi(arg);
1582 static void opt_error_concealment(const char *arg)
1584 error_concealment = atoi(arg);
1587 static void opt_debug(const char *arg)
1592 static void opt_frame_rate(const char *arg)
1594 if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
1595 fprintf(stderr, "Incorrect frame rate\n");
1600 static void opt_frame_crop_top(const char *arg)
1602 frame_topBand = atoi(arg);
1603 if (frame_topBand < 0) {
1604 fprintf(stderr, "Incorrect top crop size\n");
1607 if ((frame_topBand % 2) != 0) {
1608 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1611 if ((frame_topBand) >= frame_height){
1612 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1615 frame_height -= frame_topBand;
1618 static void opt_frame_crop_bottom(const char *arg)
1620 frame_bottomBand = atoi(arg);
1621 if (frame_bottomBand < 0) {
1622 fprintf(stderr, "Incorrect bottom crop size\n");
1625 if ((frame_bottomBand % 2) != 0) {
1626 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1629 if ((frame_bottomBand) >= frame_height){
1630 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1633 frame_height -= frame_bottomBand;
1636 static void opt_frame_crop_left(const char *arg)
1638 frame_leftBand = atoi(arg);
1639 if (frame_leftBand < 0) {
1640 fprintf(stderr, "Incorrect left crop size\n");
1643 if ((frame_leftBand % 2) != 0) {
1644 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1647 if ((frame_leftBand) >= frame_width){
1648 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1651 frame_width -= frame_leftBand;
1654 static void opt_frame_crop_right(const char *arg)
1656 frame_rightBand = atoi(arg);
1657 if (frame_rightBand < 0) {
1658 fprintf(stderr, "Incorrect right crop size\n");
1661 if ((frame_rightBand % 2) != 0) {
1662 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1665 if ((frame_rightBand) >= frame_width){
1666 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1669 frame_width -= frame_rightBand;
1672 static void opt_frame_size(const char *arg)
1674 if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
1675 fprintf(stderr, "Incorrect frame size\n");
1678 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1679 fprintf(stderr, "Frame size must be a multiple of 2\n");
1684 static void opt_frame_pix_fmt(const char *arg)
1686 frame_pix_fmt = avcodec_get_pix_fmt(arg);
1689 static void opt_frame_aspect_ratio(const char *arg)
1695 p = strchr(arg, ':');
1697 x = strtol(arg, (char **)&arg, 10);
1699 y = strtol(arg+1, (char **)&arg, 10);
1701 ar = (double)x / (double)y;
1703 ar = strtod(arg, (char **)&arg);
1706 fprintf(stderr, "Incorrect aspect ratio specification.\n");
1709 frame_aspect_ratio = ar;
1712 static void opt_gop_size(const char *arg)
1714 gop_size = atoi(arg);
1717 static void opt_b_frames(const char *arg)
1719 b_frames = atoi(arg);
1720 if (b_frames > FF_MAX_B_FRAMES) {
1721 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
1723 } else if (b_frames < 1) {
1724 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
1729 static void opt_mb_decision(const char *arg)
1731 mb_decision = atoi(arg);
1734 static void opt_qscale(const char *arg)
1736 video_qscale = atoi(arg);
1737 if (video_qscale < 0 ||
1738 video_qscale > 31) {
1739 fprintf(stderr, "qscale must be >= 1 and <= 31\n");
1744 static void opt_qmin(const char *arg)
1746 video_qmin = atoi(arg);
1747 if (video_qmin < 0 ||
1749 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1754 static void opt_qmax(const char *arg)
1756 video_qmax = atoi(arg);
1757 if (video_qmax < 0 ||
1759 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1764 static void opt_mb_qmin(const char *arg)
1766 video_mb_qmin = atoi(arg);
1767 if (video_mb_qmin < 0 ||
1768 video_mb_qmin > 31) {
1769 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
1774 static void opt_mb_qmax(const char *arg)
1776 video_mb_qmax = atoi(arg);
1777 if (video_mb_qmax < 0 ||
1778 video_mb_qmax > 31) {
1779 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
1784 static void opt_qdiff(const char *arg)
1786 video_qdiff = atoi(arg);
1787 if (video_qdiff < 0 ||
1789 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
1794 static void opt_qblur(const char *arg)
1796 video_qblur = atof(arg);
1799 static void opt_qcomp(const char *arg)
1801 video_qcomp = atof(arg);
1804 static void opt_rc_initial_cplx(const char *arg)
1806 video_rc_initial_cplx = atof(arg);
1808 static void opt_b_qfactor(const char *arg)
1810 video_b_qfactor = atof(arg);
1812 static void opt_i_qfactor(const char *arg)
1814 video_i_qfactor = atof(arg);
1816 static void opt_b_qoffset(const char *arg)
1818 video_b_qoffset = atof(arg);
1820 static void opt_i_qoffset(const char *arg)
1822 video_i_qoffset = atof(arg);
1825 static void opt_packet_size(const char *arg)
1827 packet_size= atoi(arg);
1830 static void opt_strict(const char *arg)
1835 static void opt_audio_bitrate(const char *arg)
1837 audio_bit_rate = atoi(arg) * 1000;
1840 static void opt_audio_rate(const char *arg)
1842 audio_sample_rate = atoi(arg);
1845 static void opt_audio_channels(const char *arg)
1847 audio_channels = atoi(arg);
1850 static void opt_video_device(const char *arg)
1852 video_device = av_strdup(arg);
1855 static void opt_video_channel(const char *arg)
1857 video_channel = strtol(arg, NULL, 0);
1860 static void opt_video_standard(const char *arg)
1862 video_standard = av_strdup(arg);
1865 static void opt_audio_device(const char *arg)
1867 audio_device = av_strdup(arg);
1870 static void opt_dv1394(const char *arg)
1872 video_grab_format = "dv1394";
1873 audio_grab_format = NULL;
1876 static void opt_audio_codec(const char *arg)
1880 if (!strcmp(arg, "copy")) {
1881 audio_stream_copy = 1;
1885 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
1890 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
1893 audio_codec_id = p->id;
1898 static void add_frame_hooker(const char *arg)
1903 char *args = av_strdup(arg);
1905 argv[0] = strtok(args, " ");
1906 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
1909 i = frame_hook_add(argc, argv);
1912 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
1917 const char *motion_str[] = {
1927 static void opt_motion_estimation(const char *arg)
1933 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
1936 if (!strcmp(*p, arg))
1940 me_method = (p - motion_str) + 1;
1943 static void opt_video_codec(const char *arg)
1947 if (!strcmp(arg, "copy")) {
1948 video_stream_copy = 1;
1952 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
1957 fprintf(stderr, "Unknown video codec '%s'\n", arg);
1960 video_codec_id = p->id;
1965 static void opt_map(const char *arg)
1971 m = &stream_maps[nb_stream_maps++];
1973 m->file_index = strtol(arg, (char **)&p, 0);
1977 m->stream_index = strtol(p, (char **)&p, 0);
1980 static void opt_recording_time(const char *arg)
1982 recording_time = parse_date(arg, 1);
1985 static void opt_input_file(const char *filename)
1987 AVFormatContext *ic;
1988 AVFormatParameters params, *ap = ¶ms;
1989 int err, i, ret, rfps, rfps_base;
1991 if (!strcmp(filename, "-"))
1994 /* get default parameters from command line */
1995 memset(ap, 0, sizeof(*ap));
1996 ap->sample_rate = audio_sample_rate;
1997 ap->channels = audio_channels;
1998 ap->frame_rate = frame_rate;
1999 ap->frame_rate_base = frame_rate_base;
2000 ap->width = frame_width;
2001 ap->height = frame_height;
2002 ap->image_format = image_format;
2003 ap->pix_fmt = frame_pix_fmt;
2005 /* open the input file with generic libav function */
2006 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
2008 print_error(filename, err);
2012 /* If not enough info to get the stream parameters, we decode the
2013 first frames to get it. (used in mpeg case for example) */
2014 ret = av_find_stream_info(ic);
2016 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2020 /* update the current parameters so that they match the one of the input stream */
2021 for(i=0;i<ic->nb_streams;i++) {
2022 AVCodecContext *enc = &ic->streams[i]->codec;
2023 switch(enc->codec_type) {
2024 case CODEC_TYPE_AUDIO:
2025 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2026 audio_channels = enc->channels;
2027 audio_sample_rate = enc->sample_rate;
2029 case CODEC_TYPE_VIDEO:
2030 frame_height = enc->height;
2031 frame_width = enc->width;
2032 frame_aspect_ratio = enc->aspect_ratio;
2033 frame_pix_fmt = enc->pix_fmt;
2034 rfps = ic->streams[i]->r_frame_rate;
2035 rfps_base = ic->streams[i]->r_frame_rate_base;
2036 enc->workaround_bugs = workaround_bugs;
2037 enc->error_resilience = error_resilience;
2038 enc->error_concealment = error_concealment;
2039 enc->idct_algo= idct_algo;
2041 /* if(enc->codec->capabilities & CODEC_CAP_TRUNCATED)
2042 enc->flags|= CODEC_FLAG_TRUNCATED; */
2043 if(/*enc->codec_id==CODEC_ID_MPEG4 || */enc->codec_id==CODEC_ID_MPEG1VIDEO || enc->codec_id==CODEC_ID_H264)
2044 enc->flags|= CODEC_FLAG_TRUNCATED;
2047 enc->flags|= CODEC_FLAG_BITEXACT;
2049 assert(enc->frame_rate_base == rfps_base); // should be true for now
2050 if (enc->frame_rate != rfps) {
2051 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2052 i, (float)enc->frame_rate / enc->frame_rate_base,
2053 (float)rfps / rfps_base);
2055 /* update the current frame rate to match the stream frame rate */
2057 frame_rate_base = rfps_base;
2059 enc->rate_emu = rate_emu;
2066 input_files[nb_input_files] = ic;
2067 /* dump the file content */
2068 dump_format(ic, nb_input_files, filename, 0);
2070 file_iformat = NULL;
2071 file_oformat = NULL;
2072 image_format = NULL;
2077 static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2079 int has_video, has_audio, i, j;
2080 AVFormatContext *ic;
2084 for(j=0;j<nb_input_files;j++) {
2085 ic = input_files[j];
2086 for(i=0;i<ic->nb_streams;i++) {
2087 AVCodecContext *enc = &ic->streams[i]->codec;
2088 switch(enc->codec_type) {
2089 case CODEC_TYPE_AUDIO:
2092 case CODEC_TYPE_VIDEO:
2100 *has_video_ptr = has_video;
2101 *has_audio_ptr = has_audio;
2104 static void opt_output_file(const char *filename)
2107 AVFormatContext *oc;
2108 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2110 AVFormatParameters params, *ap = ¶ms;
2112 if (!strcmp(filename, "-"))
2115 oc = av_mallocz(sizeof(AVFormatContext));
2117 if (!file_oformat) {
2118 file_oformat = guess_format(NULL, filename, NULL);
2119 if (!file_oformat) {
2120 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2126 oc->oformat = file_oformat;
2128 if (!strcmp(file_oformat->name, "ffm") &&
2129 strstart(filename, "http:", NULL)) {
2130 /* special case for files sent to ffserver: we get the stream
2131 parameters from ffserver */
2132 if (read_ffserver_streams(oc, filename) < 0) {
2133 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2137 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2138 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2140 /* disable if no corresponding type found and at least one
2142 if (nb_input_files > 0) {
2143 check_audio_video_inputs(&input_has_video, &input_has_audio);
2144 if (!input_has_video)
2146 if (!input_has_audio)
2150 /* manual disable */
2151 if (audio_disable) {
2154 if (video_disable) {
2160 AVCodecContext *video_enc;
2162 st = av_mallocz(sizeof(AVStream));
2164 fprintf(stderr, "Could not alloc stream\n");
2167 avcodec_get_context_defaults(&st->codec);
2169 video_enc = &st->codec;
2170 if (video_stream_copy) {
2171 st->stream_copy = 1;
2172 video_enc->codec_type = CODEC_TYPE_VIDEO;
2177 codec_id = file_oformat->video_codec;
2178 if (video_codec_id != CODEC_ID_NONE)
2179 codec_id = video_codec_id;
2181 video_enc->codec_id = codec_id;
2183 video_enc->bit_rate = video_bit_rate;
2184 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2185 video_enc->frame_rate = frame_rate;
2186 video_enc->frame_rate_base = frame_rate_base;
2188 video_enc->width = frame_width;
2189 video_enc->height = frame_height;
2190 video_enc->aspect_ratio = frame_aspect_ratio;
2191 video_enc->pix_fmt = frame_pix_fmt;
2194 video_enc->gop_size = gop_size;
2196 video_enc->gop_size = 0;
2197 if (video_qscale || same_quality) {
2198 video_enc->flags |= CODEC_FLAG_QSCALE;
2199 st->quality = video_qscale;
2203 video_enc->flags |= CODEC_FLAG_BITEXACT;
2205 video_enc->mb_decision = mb_decision;
2208 video_enc->flags |= CODEC_FLAG_H263P_UMV;
2211 video_enc->flags |= CODEC_FLAG_H263P_AIC;
2214 video_enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
2215 video_enc->flags |= CODEC_FLAG_4MV;
2219 video_enc->flags |= CODEC_FLAG_PART;
2223 video_enc->max_b_frames = b_frames;
2224 video_enc->b_frame_strategy = 0;
2225 video_enc->b_quant_factor = 2.0;
2228 video_enc->qmin = video_qmin;
2229 video_enc->qmax = video_qmax;
2230 video_enc->mb_qmin = video_mb_qmin;
2231 video_enc->mb_qmax = video_mb_qmax;
2232 video_enc->max_qdiff = video_qdiff;
2233 video_enc->qblur = video_qblur;
2234 video_enc->qcompress = video_qcomp;
2235 video_enc->rc_eq = video_rc_eq;
2236 video_enc->debug= debug;
2238 p= video_rc_override_string;
2241 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2243 fprintf(stderr, "error parsing rc_override\n");
2246 video_enc->rc_override=
2247 av_realloc(video_enc->rc_override,
2248 sizeof(RcOverride)*(i+1));
2249 video_enc->rc_override[i].start_frame= start;
2250 video_enc->rc_override[i].end_frame = end;
2252 video_enc->rc_override[i].qscale= q;
2253 video_enc->rc_override[i].quality_factor= 1.0;
2256 video_enc->rc_override[i].qscale= 0;
2257 video_enc->rc_override[i].quality_factor= -q/100.0;
2262 video_enc->rc_override_count=i;
2264 video_enc->rc_max_rate = video_rc_max_rate;
2265 video_enc->rc_min_rate = video_rc_min_rate;
2266 video_enc->rc_buffer_size = video_rc_buffer_size;
2267 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2268 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2269 video_enc->i_quant_factor = video_i_qfactor;
2270 video_enc->b_quant_factor = video_b_qfactor;
2271 video_enc->i_quant_offset = video_i_qoffset;
2272 video_enc->b_quant_offset = video_b_qoffset;
2273 video_enc->dct_algo = dct_algo;
2274 video_enc->idct_algo = idct_algo;
2275 video_enc->strict_std_compliance = strict;
2277 video_enc->rtp_mode= 1;
2278 video_enc->rtp_payload_size= packet_size;
2282 video_enc->flags|= CODEC_FLAG_PSNR;
2284 video_enc->me_method = me_method;
2289 video_enc->flags |= CODEC_FLAG_PASS1;
2291 video_enc->flags |= CODEC_FLAG_PASS2;
2295 oc->streams[nb_streams] = st;
2300 AVCodecContext *audio_enc;
2302 st = av_mallocz(sizeof(AVStream));
2304 fprintf(stderr, "Could not alloc stream\n");
2307 avcodec_get_context_defaults(&st->codec);
2309 audio_enc = &st->codec;
2310 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2311 if (audio_stream_copy) {
2312 st->stream_copy = 1;
2314 codec_id = file_oformat->audio_codec;
2315 if (audio_codec_id != CODEC_ID_NONE)
2316 codec_id = audio_codec_id;
2317 audio_enc->codec_id = codec_id;
2319 audio_enc->bit_rate = audio_bit_rate;
2320 audio_enc->sample_rate = audio_sample_rate;
2321 /* For audio codecs other than AC3 we limit */
2322 /* the number of coded channels to stereo */
2323 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2324 audio_enc->channels = 2;
2326 audio_enc->channels = audio_channels;
2328 oc->streams[nb_streams] = st;
2332 oc->nb_streams = nb_streams;
2335 fprintf(stderr, "No audio or video streams available\n");
2340 pstrcpy(oc->title, sizeof(oc->title), str_title);
2342 pstrcpy(oc->author, sizeof(oc->author), str_author);
2344 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2346 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2349 output_files[nb_output_files++] = oc;
2351 strcpy(oc->filename, filename);
2353 /* check filename in case of an image number is expected */
2354 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2355 if (filename_number_test(oc->filename) < 0) {
2356 print_error(oc->filename, AVERROR_NUMEXPECTED);
2361 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2362 /* test if it already exists to avoid loosing precious files */
2363 if (!file_overwrite &&
2364 (strchr(filename, ':') == NULL ||
2365 strstart(filename, "file:", NULL))) {
2366 if (url_exist(filename)) {
2369 printf("File '%s' already exists. Overwrite ? [y/N] ", filename);
2372 if (toupper(c) != 'Y') {
2373 fprintf(stderr, "Not overwriting - exiting\n");
2380 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2381 fprintf(stderr, "Could not open '%s'\n", filename);
2386 memset(ap, 0, sizeof(*ap));
2387 ap->image_format = image_format;
2388 if (av_set_parameters(oc, ap) < 0) {
2389 fprintf(stderr, "%s: Invalid encoding parameters\n",
2394 /* reset some options */
2395 file_oformat = NULL;
2396 file_iformat = NULL;
2397 image_format = NULL;
2400 audio_codec_id = CODEC_ID_NONE;
2401 video_codec_id = CODEC_ID_NONE;
2402 audio_stream_copy = 0;
2403 video_stream_copy = 0;
2406 /* prepare dummy protocols for grab */
2407 static void prepare_grab(void)
2409 int has_video, has_audio, i, j;
2410 AVFormatContext *oc;
2411 AVFormatContext *ic;
2412 AVFormatParameters vp1, *vp = &vp1;
2413 AVFormatParameters ap1, *ap = &ap1;
2415 /* see if audio/video inputs are needed */
2418 memset(ap, 0, sizeof(*ap));
2419 memset(vp, 0, sizeof(*vp));
2420 for(j=0;j<nb_output_files;j++) {
2421 oc = output_files[j];
2422 for(i=0;i<oc->nb_streams;i++) {
2423 AVCodecContext *enc = &oc->streams[i]->codec;
2424 switch(enc->codec_type) {
2425 case CODEC_TYPE_AUDIO:
2426 if (enc->sample_rate > ap->sample_rate)
2427 ap->sample_rate = enc->sample_rate;
2428 if (enc->channels > ap->channels)
2429 ap->channels = enc->channels;
2432 case CODEC_TYPE_VIDEO:
2433 if (enc->width > vp->width)
2434 vp->width = enc->width;
2435 if (enc->height > vp->height)
2436 vp->height = enc->height;
2438 assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
2439 if (enc->frame_rate > vp->frame_rate){
2440 vp->frame_rate = enc->frame_rate;
2441 vp->frame_rate_base = enc->frame_rate_base;
2451 if (has_video == 0 && has_audio == 0) {
2452 fprintf(stderr, "Output file must have at least one audio or video stream\n");
2457 AVInputFormat *fmt1;
2458 fmt1 = av_find_input_format(video_grab_format);
2459 vp->device = video_device;
2460 vp->channel = video_channel;
2461 vp->standard = video_standard;
2462 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
2463 fprintf(stderr, "Could not find video grab device\n");
2466 /* by now video grab has one stream */
2467 ic->streams[0]->r_frame_rate = vp->frame_rate;
2468 ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
2469 input_files[nb_input_files] = ic;
2470 dump_format(ic, nb_input_files, "", 0);
2473 if (has_audio && audio_grab_format) {
2474 AVInputFormat *fmt1;
2475 fmt1 = av_find_input_format(audio_grab_format);
2476 ap->device = audio_device;
2477 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
2478 fprintf(stderr, "Could not find audio grab device\n");
2481 input_files[nb_input_files] = ic;
2482 dump_format(ic, nb_input_files, "", 0);
2487 /* same option as mencoder */
2488 static void opt_pass(const char *pass_str)
2491 pass = atoi(pass_str);
2492 if (pass != 1 && pass != 2) {
2493 fprintf(stderr, "pass number can be only 1 or 2\n");
2499 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
2500 static int64_t getutime(void)
2502 return av_gettime();
2505 static int64_t getutime(void)
2507 struct rusage rusage;
2509 getrusage(RUSAGE_SELF, &rusage);
2510 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
2514 extern int ffm_nopts;
2516 static void opt_bitexact(void)
2519 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2523 static void show_formats(void)
2525 AVInputFormat *ifmt;
2526 AVOutputFormat *ofmt;
2527 AVImageFormat *image_fmt;
2532 printf("Output audio/video file formats:");
2533 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
2534 printf(" %s", ofmt->name);
2538 printf("Input audio/video file formats:");
2539 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
2540 printf(" %s", ifmt->name);
2544 printf("Output image formats:");
2545 for(image_fmt = first_image_format; image_fmt != NULL;
2546 image_fmt = image_fmt->next) {
2547 if (image_fmt->img_write)
2548 printf(" %s", image_fmt->name);
2552 printf("Input image formats:");
2553 for(image_fmt = first_image_format; image_fmt != NULL;
2554 image_fmt = image_fmt->next) {
2555 if (image_fmt->img_read)
2556 printf(" %s", image_fmt->name);
2560 printf("Codecs:\n");
2561 printf(" Encoders:");
2562 for(p = first_avcodec; p != NULL; p = p->next) {
2564 printf(" %s", p->name);
2568 printf(" Decoders:");
2569 for(p = first_avcodec; p != NULL; p = p->next) {
2571 printf(" %s", p->name);
2575 printf("Supported file protocols:");
2576 for(up = first_protocol; up != NULL; up = up->next)
2577 printf(" %s:", up->name);
2580 printf("Frame size, frame rate abbreviations: ntsc pal film ntsc-film sqcif qcif cif 4cif\n");
2581 printf("Motion estimation methods:");
2585 if ((pp - motion_str + 1) == ME_ZERO)
2586 printf("(fastest)");
2587 else if ((pp - motion_str + 1) == ME_FULL)
2588 printf("(slowest)");
2589 else if ((pp - motion_str + 1) == ME_EPZS)
2590 printf("(default)");
2597 const OptionDef options[] = {
2599 { "L", 0, {(void*)show_license}, "show license" },
2600 { "h", 0, {(void*)show_help}, "show help" },
2601 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
2602 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
2603 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
2604 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
2605 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
2606 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
2607 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
2608 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
2609 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
2610 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
2611 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
2612 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
2613 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
2614 "add timings for benchmarking" },
2615 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
2616 "dump each input packet" },
2617 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
2618 { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
2619 { "loop", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
2622 { "b", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
2623 { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
2624 { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
2625 { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
2626 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
2627 { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
2628 { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
2629 { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
2630 { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
2631 { "g", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
2632 { "intra", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_only}, "use only intra frames"},
2633 { "vn", OPT_BOOL | OPT_VIDEO, {(void*)&video_disable}, "disable video" },
2634 { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
2635 { "qmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
2636 { "qmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
2637 { "mbqmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
2638 { "mbqmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
2639 { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
2640 { "qblur", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
2641 { "qcomp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
2642 { "rc_init_cplx", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
2643 { "b_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
2644 { "i_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
2645 { "b_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
2646 { "i_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
2647 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
2648 { "rc_eq", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_eq}, "set rate control equation", "equation" },
2649 { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" },
2650 { "bt", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
2651 { "maxrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
2652 { "minrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
2653 { "bufsize", HAS_ARG | OPT_VIDEO, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kbit)", "size" },
2654 { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
2655 { "me", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_motion_estimation}, "set motion estimation method",
2657 { "dct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dct_algo}, "set dct algo", "algo" },
2658 { "idct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_idct_algo}, "set idct algo", "algo" },
2659 { "er", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_resilience}, "set error resilience", "n" },
2660 { "ec", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_concealment}, "set error concealment", "bit_mask" },
2661 { "bf", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_frames}, "use 'frames' B frames (only MPEG-4)", "frames" },
2662 { "hq", OPT_BOOL, {(void*)&mb_decision}, "activate high quality settings" },
2663 { "mbd", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_decision}, "macroblock decision", "mode" },
2664 { "4mv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_4mv}, "use four motion vector by macroblock (only MPEG-4)" },
2665 { "part", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_part}, "use data partitioning (only MPEG-4)" },
2666 { "bug", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
2667 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "set packet size in bits", "size" },
2668 { "strict", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_strict}, "how strictly to follow the standarts", "strictness" },
2669 { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality},
2670 "use same video quality as source (implies VBR)" },
2671 { "pass", HAS_ARG | OPT_VIDEO, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
2672 { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
2673 { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
2674 "deinterlace pictures" },
2675 { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
2676 { "vstats", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_vstats}, "dump video coding statistics to file" },
2677 { "vhook", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)add_frame_hooker}, "insert video processing module", "module" },
2678 { "aic", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
2679 { "umv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
2682 { "ab", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
2683 { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
2684 { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
2685 { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" },
2686 { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
2689 { "vd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_device}, "set video grab device", "device" },
2690 { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
2691 { "tvstd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_standard}, "set television standard (NTSC, PAL (SECAM))", "standard" },
2692 { "dv1394", OPT_EXPERT | OPT_GRAB, {(void*)opt_dv1394}, "set DV1394 grab", "" },
2693 { "ad", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_GRAB, {(void*)opt_audio_device}, "set audio device", "device" },
2697 static void show_banner(void)
2699 printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000-2003 Fabrice Bellard\n");
2702 static void show_license(void)
2706 "This library is free software; you can redistribute it and/or\n"
2707 "modify it under the terms of the GNU Lesser General Public\n"
2708 "License as published by the Free Software Foundation; either\n"
2709 "version 2 of the License, or (at your option) any later version.\n"
2711 "This library is distributed in the hope that it will be useful,\n"
2712 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
2713 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
2714 "Lesser General Public License for more details.\n"
2716 "You should have received a copy of the GNU Lesser General Public\n"
2717 "License along with this library; if not, write to the Free Software\n"
2718 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
2723 static void show_help(void)
2726 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
2727 "Hyper fast Audio and Video encoder\n");
2729 show_help_options(options, "Main options:\n",
2730 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO, 0);
2731 show_help_options(options, "\nVideo options:\n",
2732 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
2734 show_help_options(options, "\nAdvanced Video options:\n",
2735 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
2736 OPT_VIDEO | OPT_EXPERT);
2737 show_help_options(options, "\nAudio options:\n",
2738 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
2740 show_help_options(options, "\nAdvanced Audio options:\n",
2741 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
2742 OPT_AUDIO | OPT_EXPERT);
2743 show_help_options(options, "\nAudio/Video grab options:\n",
2746 show_help_options(options, "\nAdvanced options:\n",
2747 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
2752 void parse_arg_file(const char *filename)
2754 opt_output_file(filename);
2757 int main(int argc, char **argv)
2768 parse_options(argc, argv, options);
2770 /* file converter / grab */
2771 if (nb_output_files <= 0) {
2772 fprintf(stderr, "Must supply at least one output file\n");
2776 if (nb_input_files == 0) {
2781 av_encode(output_files, nb_output_files, input_files, nb_input_files,
2782 stream_maps, nb_stream_maps);
2783 ti = getutime() - ti;
2785 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
2789 for(i=0;i<nb_output_files;i++) {
2790 /* maybe av_close_output_file ??? */
2791 AVFormatContext *s = output_files[i];
2793 if (!(s->oformat->flags & AVFMT_NOFILE))
2795 for(j=0;j<s->nb_streams;j++)
2796 av_free(s->streams[j]);
2799 for(i=0;i<nb_input_files;i++)
2800 av_close_input_file(input_files[i]);
2805 #ifdef POWERPC_PERFORMANCE_REPORT
2806 extern void powerpc_display_perf_report(void);
2807 powerpc_display_perf_report();
2808 #endif /* POWERPC_PERFORMANCE_REPORT */
2810 #ifndef CONFIG_WIN32
2811 if (received_sigterm) {
2813 "Received signal %d: terminating.\n",
2814 (int) received_sigterm);
2818 exit(0); /* not all OS-es handle main() return value */