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
21 #include "framehook.h"
26 #include <sys/ioctl.h>
29 #include <sys/resource.h>
33 #include <sys/types.h>
34 #include <sys/select.h>
37 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
42 #if !defined(INFINITY) && defined(HUGE_VAL)
43 #define INFINITY HUGE_VAL
46 /* select an input stream for an output stream */
47 typedef struct AVStreamMap {
52 extern const OptionDef options[];
54 static void show_help(void);
55 static void show_license(void);
59 static AVFormatContext *input_files[MAX_FILES];
60 static int nb_input_files = 0;
62 static AVFormatContext *output_files[MAX_FILES];
63 static int nb_output_files = 0;
65 static AVStreamMap stream_maps[MAX_FILES];
66 static int nb_stream_maps;
68 static AVInputFormat *file_iformat;
69 static AVOutputFormat *file_oformat;
70 static AVImageFormat *image_format;
71 static int frame_width = 160;
72 static int frame_height = 128;
73 static float frame_aspect_ratio = 0;
74 static enum PixelFormat frame_pix_fmt = PIX_FMT_YUV420P;
75 static int frame_padtop = 0;
76 static int frame_padbottom = 0;
77 static int frame_padleft = 0;
78 static int frame_padright = 0;
79 static int padcolor[3] = {16,128,128}; /* default to black */
80 static int frame_topBand = 0;
81 static int frame_bottomBand = 0;
82 static int frame_leftBand = 0;
83 static int frame_rightBand = 0;
84 static int frame_rate = 25;
85 static int frame_rate_base = 1;
86 static int video_bit_rate = 200*1000;
87 static int video_bit_rate_tolerance = 4000*1000;
88 static float video_qscale = 0;
89 static int video_qmin = 2;
90 static int video_qmax = 31;
91 static int video_mb_qmin = 2;
92 static int video_mb_qmax = 31;
93 static int video_qdiff = 3;
94 static float video_qblur = 0.5;
95 static float video_qcomp = 0.5;
96 static uint16_t *intra_matrix = NULL;
97 static uint16_t *inter_matrix = NULL;
98 #if 0 //experimental, (can be removed)
99 static float video_rc_qsquish=1.0;
100 static float video_rc_qmod_amp=0;
101 static int video_rc_qmod_freq=0;
103 static char *video_rc_override_string=NULL;
104 static char *video_rc_eq="tex^qComp";
105 static int video_rc_buffer_size=0;
106 static float video_rc_buffer_aggressivity=1.0;
107 static int video_rc_max_rate=0;
108 static int video_rc_min_rate=0;
109 static float video_rc_initial_cplx=0;
110 static float video_b_qfactor = 1.25;
111 static float video_b_qoffset = 1.25;
112 static float video_i_qfactor = -0.8;
113 static float video_i_qoffset = 0.0;
114 static int video_intra_quant_bias= FF_DEFAULT_QUANT_BIAS;
115 static int video_inter_quant_bias= FF_DEFAULT_QUANT_BIAS;
116 static int me_method = ME_EPZS;
117 static int video_disable = 0;
118 static int video_codec_id = CODEC_ID_NONE;
119 static int same_quality = 0;
120 static int b_frames = 0;
121 static int mb_decision = FF_MB_DECISION_SIMPLE;
122 static int ildct_cmp = FF_CMP_VSAD;
123 static int mb_cmp = FF_CMP_SAD;
124 static int sub_cmp = FF_CMP_SAD;
125 static int cmp = FF_CMP_SAD;
126 static int pre_cmp = FF_CMP_SAD;
127 static int pre_me = 0;
128 static float lumi_mask = 0;
129 static float dark_mask = 0;
130 static float scplx_mask = 0;
131 static float tcplx_mask = 0;
132 static float p_mask = 0;
133 static int use_4mv = 0;
134 static int use_obmc = 0;
135 static int use_loop = 0;
136 static int use_aic = 0;
137 static int use_aiv = 0;
138 static int use_umv = 0;
139 static int use_ss = 0;
140 static int use_alt_scan = 0;
141 static int use_trell = 0;
142 static int use_scan_offset = 0;
144 static int closed_gop = 0;
145 static int do_deinterlace = 0;
146 static int do_interlace_dct = 0;
147 static int do_interlace_me = 0;
148 static int workaround_bugs = FF_BUG_AUTODETECT;
149 static int error_resilience = 2;
150 static int error_concealment = 3;
151 static int dct_algo = 0;
152 static int idct_algo = 0;
153 static int use_part = 0;
154 static int packet_size = 0;
155 static int error_rate = 0;
156 static int strict = 0;
157 static int top_field_first = -1;
158 static int noise_reduction = 0;
159 static int sc_threshold = 0;
160 static int debug = 0;
161 static int debug_mv = 0;
162 extern int loop_input; /* currently a hack */
164 static int gop_size = 12;
165 static int intra_only = 0;
166 static int audio_sample_rate = 44100;
167 static int audio_bit_rate = 64000;
168 static int audio_disable = 0;
169 static int audio_channels = 1;
170 static int audio_codec_id = CODEC_ID_NONE;
172 static int64_t recording_time = 0;
173 static int64_t start_time = 0;
174 static int64_t rec_timestamp = 0;
175 static int file_overwrite = 0;
176 static char *str_title = NULL;
177 static char *str_author = NULL;
178 static char *str_copyright = NULL;
179 static char *str_comment = NULL;
180 static int do_benchmark = 0;
181 static int do_hex_dump = 0;
182 static int do_pkt_dump = 0;
183 static int do_psnr = 0;
184 static int do_vstats = 0;
185 static int do_pass = 0;
186 static int bitexact = 0;
187 static char *pass_logfilename = NULL;
188 static int audio_stream_copy = 0;
189 static int video_stream_copy = 0;
191 static int rate_emu = 0;
193 static char *video_grab_format = "video4linux";
194 static char *video_device = NULL;
195 static int video_channel = 0;
196 static char *video_standard = "ntsc";
198 static char *audio_grab_format = "audio_device";
199 static char *audio_device = NULL;
201 static int using_stdin = 0;
202 static int using_vhook = 0;
203 static int verbose = 1;
204 static int thread_count= 1;
205 static int q_pressed = 0;
206 static int64_t video_size = 0;
207 static int64_t audio_size = 0;
208 static int64_t extra_size = 0;
210 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
212 typedef struct AVOutputStream {
213 int file_index; /* file index */
214 int index; /* stream index in the output file */
215 int source_index; /* AVInputStream index */
216 AVStream *st; /* stream in the output file */
217 int encoding_needed; /* true if encoding needed for this stream */
219 /* input pts and corresponding output pts
222 double sync_ipts_offset;
225 int video_resample; /* video_resample and video_crop are mutually exclusive */
226 AVPicture pict_tmp; /* temporary image for resampling */
227 ImgReSampleContext *img_resample_ctx; /* for image resampling */
229 int video_crop; /* video_resample and video_crop are mutually exclusive */
230 int topBand; /* cropping area sizes */
233 int video_pad; /* video_resample and video_pad are mutually exclusive */
234 int padtop; /* padding area sizes */
241 ReSampleContext *resample; /* for audio resampling */
242 FifoBuffer fifo; /* for compression: one audio fifo per codec */
246 typedef struct AVInputStream {
250 int discard; /* true if stream data should be discarded */
251 int decoding_needed; /* true if the packets must be decoded in 'raw_fifo' */
252 int64_t sample_index; /* current sample */
254 int64_t start; /* time when read started */
255 unsigned long frame; /* current frame */
256 int64_t next_pts; /* synthetic pts for cases where pkt.pts
258 int64_t pts; /* current pts */
261 typedef struct AVInputFile {
262 int eof_reached; /* true if eof reached */
263 int ist_index; /* index of first stream in ist_table */
264 int buffer_size; /* current total buffer size */
265 int buffer_size_max; /* buffer size at which we consider we can stop
267 int nb_streams; /* nb streams we are aware of */
272 /* init terminal so that we can grab keys */
273 static struct termios oldtty;
275 static void term_exit(void)
277 tcsetattr (0, TCSANOW, &oldtty);
280 static volatile sig_atomic_t received_sigterm = 0;
283 sigterm_handler(int sig)
285 received_sigterm = sig;
289 static void term_init(void)
296 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
297 |INLCR|IGNCR|ICRNL|IXON);
298 tty.c_oflag |= OPOST;
299 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
300 tty.c_cflag &= ~(CSIZE|PARENB);
305 tcsetattr (0, TCSANOW, &tty);
307 signal(SIGINT , sigterm_handler); /* Interrupt (ANSI). */
308 signal(SIGQUIT, sigterm_handler); /* Quit (POSIX). */
309 signal(SIGTERM, sigterm_handler); /* Termination (ANSI). */
311 register a function to be called at normal program termination
314 #ifdef CONFIG_BEOS_NETSERVER
315 fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
319 /* read a key without blocking */
320 static int read_key(void)
324 #ifndef CONFIG_BEOS_NETSERVER
332 n = select(1, &rfds, NULL, NULL, &tv);
344 static int decode_interrupt_cb(void)
346 return q_pressed || (q_pressed = read_key() == 'q');
351 static volatile int received_sigterm = 0;
353 /* no interactive support */
354 static void term_exit(void)
358 static void term_init(void)
362 static int read_key(void)
369 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
374 err = av_open_input_file(&ic, filename, NULL, FFM_PACKET_SIZE, NULL);
377 /* copy stream format */
378 s->nb_streams = ic->nb_streams;
379 for(i=0;i<ic->nb_streams;i++) {
382 st = av_mallocz(sizeof(AVStream));
383 memcpy(st, ic->streams[i], sizeof(AVStream));
387 av_close_input_file(ic);
391 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
393 static void do_audio_out(AVFormatContext *s,
396 unsigned char *buf, int size)
399 static uint8_t *audio_buf = NULL;
400 static uint8_t *audio_out = NULL;
401 const int audio_out_size= 4*MAX_AUDIO_PACKET_SIZE;
403 int size_out, frame_bytes, ret;
406 /* SC: dynamic allocation of buffers */
408 audio_buf = av_malloc(2*MAX_AUDIO_PACKET_SIZE);
410 audio_out = av_malloc(audio_out_size);
411 if (!audio_buf || !audio_out)
412 return; /* Should signal an error ! */
415 enc = &ost->st->codec;
417 if (ost->audio_resample) {
419 size_out = audio_resample(ost->resample,
420 (short *)buftmp, (short *)buf,
421 size / (ist->st->codec.channels * 2));
422 size_out = size_out * enc->channels * 2;
428 /* now encode as many frames as possible */
429 if (enc->frame_size > 1) {
430 /* output resampled raw samples */
431 fifo_write(&ost->fifo, buftmp, size_out,
434 frame_bytes = enc->frame_size * 2 * enc->channels;
436 while (fifo_read(&ost->fifo, audio_buf, frame_bytes,
437 &ost->fifo.rptr) == 0) {
438 ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
441 av_write_frame(s, ost->index, audio_out, ret);
444 /* output a pcm frame */
445 /* XXX: change encoding codec API to avoid this ? */
446 switch(enc->codec->id) {
447 case CODEC_ID_PCM_S16LE:
448 case CODEC_ID_PCM_S16BE:
449 case CODEC_ID_PCM_U16LE:
450 case CODEC_ID_PCM_U16BE:
453 size_out = size_out >> 1;
456 ret = avcodec_encode_audio(enc, audio_out, size_out,
459 av_write_frame(s, ost->index, audio_out, ret);
463 static void pre_process_video_frame(AVInputStream *ist, AVPicture *picture, void **bufp)
467 AVPicture picture_tmp;
470 dec = &ist->st->codec;
472 /* deinterlace : must be done before any resize */
473 if (do_deinterlace || using_vhook) {
476 /* create temporary picture */
477 size = avpicture_get_size(dec->pix_fmt, dec->width, dec->height);
478 buf = av_malloc(size);
482 picture2 = &picture_tmp;
483 avpicture_fill(picture2, buf, dec->pix_fmt, dec->width, dec->height);
486 if(avpicture_deinterlace(picture2, picture,
487 dec->pix_fmt, dec->width, dec->height) < 0) {
488 /* if error, do not deinterlace */
494 if (img_convert(picture2, dec->pix_fmt, picture,
495 dec->pix_fmt, dec->width, dec->height) < 0) {
496 /* if error, do not copy */
506 frame_hook_process(picture2, dec->pix_fmt, dec->width, dec->height);
508 if (picture != picture2)
509 *picture = *picture2;
513 /* we begin to correct av delay at this threshold */
514 #define AV_DELAY_MAX 0.100
517 /* Expects img to be yuv420 */
518 static void fill_pad_region(AVPicture* img, int height, int width,
519 int padtop, int padbottom, int padleft, int padright, int *color) {
524 for (i = 0; i < 3; i++) {
525 shift = (i == 0) ? 0 : 1;
527 if (padtop || padleft) {
528 memset(img->data[i], color[i], (((img->linesize[i] * padtop) +
532 if (padleft || padright) {
533 optr = img->data[i] + (img->linesize[i] * (padtop >> shift)) +
534 (img->linesize[i] - (padright >> shift));
536 for (y = 0; y < ((height - (padtop + padbottom)) >> shift); y++) {
537 memset(optr, color[i], (padleft + padright) >> shift);
538 optr += img->linesize[i];
543 optr = img->data[i] + (img->linesize[i] * ((height - padbottom) >> shift));
544 memset(optr, color[i], ((img->linesize[i] * padbottom) >> shift));
550 static void do_video_out(AVFormatContext *s,
554 int *frame_size, AVOutputStream *audio_sync)
556 int nb_frames, i, ret;
557 AVPicture *final_picture, *formatted_picture;
558 AVPicture picture_format_temp, picture_crop_temp;
559 static uint8_t *video_buffer= NULL;
560 uint8_t *buf = NULL, *buf1 = NULL;
561 AVCodecContext *enc, *dec;
562 enum PixelFormat target_pixfmt;
564 #define VIDEO_BUFFER_SIZE (1024*1024)
566 enc = &ost->st->codec;
567 dec = &ist->st->codec;
569 /* by default, we output a single frame */
574 /* NOTE: the A/V sync is always done by considering the audio is
575 the master clock. It is suffisant for transcoding or playing,
576 but not for the general case */
578 /* compute the A-V delay and duplicate/remove frames if needed */
579 double adelta, vdelta, av_delay;
581 adelta = audio_sync->sync_ipts - ((double)audio_sync->sync_opts *
582 s->pts_num / s->pts_den);
584 vdelta = ost->sync_ipts - ((double)ost->sync_opts *
585 s->pts_num / s->pts_den);
587 av_delay = adelta - vdelta;
588 // printf("delay=%f\n", av_delay);
589 if (av_delay < -AV_DELAY_MAX)
591 else if (av_delay > AV_DELAY_MAX)
596 vdelta = (double)(ost->st->pts.val) * s->pts_num / s->pts_den - (ost->sync_ipts - ost->sync_ipts_offset);
597 if (vdelta < 100 && vdelta > -100 && ost->sync_ipts_offset) {
598 if (vdelta < -AV_DELAY_MAX)
600 else if (vdelta > AV_DELAY_MAX)
603 ost->sync_ipts_offset -= vdelta;
604 if (!ost->sync_ipts_offset)
605 ost->sync_ipts_offset = 0.000001; /* one microsecond */
609 #if defined(AVSYNC_DEBUG)
611 static char *action[] = { "drop frame", "copy frame", "dup frame" };
612 if (audio_sync && verbose >=0) {
613 fprintf(stderr, "Input APTS %12.6f, output APTS %12.6f, ",
614 (double) audio_sync->sync_ipts,
615 (double) audio_sync->st->pts.val * s->pts_num / s->pts_den);
616 fprintf(stderr, "Input VPTS %12.6f, output VPTS %12.6f: %s\n",
617 (double) ost->sync_ipts,
618 (double) ost->st->pts.val * s->pts_num / s->pts_den,
628 video_buffer = av_malloc(VIDEO_BUFFER_SIZE);
632 /* convert pixel format if needed */
633 target_pixfmt = ost->video_resample || ost->video_pad
634 ? PIX_FMT_YUV420P : enc->pix_fmt;
635 if (dec->pix_fmt != target_pixfmt) {
638 /* create temporary picture */
639 size = avpicture_get_size(target_pixfmt, dec->width, dec->height);
640 buf = av_malloc(size);
643 formatted_picture = &picture_format_temp;
644 avpicture_fill(formatted_picture, buf, target_pixfmt, dec->width, dec->height);
646 if (img_convert(formatted_picture, target_pixfmt,
647 (AVPicture *)in_picture, dec->pix_fmt,
648 dec->width, dec->height) < 0) {
651 fprintf(stderr, "pixel format conversion not handled\n");
656 formatted_picture = (AVPicture *)in_picture;
659 /* XXX: resampling could be done before raw format conversion in
660 some cases to go faster */
661 /* XXX: only works for YUV420P */
662 if (ost->video_resample) {
663 final_picture = &ost->pict_tmp;
664 img_resample(ost->img_resample_ctx, final_picture, formatted_picture);
666 if (ost->padtop || ost->padbottom || ost->padleft || ost->padright) {
667 fill_pad_region(final_picture, enc->height, enc->width,
668 ost->padtop, ost->padbottom, ost->padleft, ost->padright,
672 if (enc->pix_fmt != PIX_FMT_YUV420P) {
676 /* create temporary picture */
677 size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
678 buf = av_malloc(size);
681 final_picture = &picture_format_temp;
682 avpicture_fill(final_picture, buf, enc->pix_fmt, enc->width, enc->height);
684 if (img_convert(final_picture, enc->pix_fmt,
685 &ost->pict_tmp, PIX_FMT_YUV420P,
686 enc->width, enc->height) < 0) {
689 fprintf(stderr, "pixel format conversion not handled\n");
694 } else if (ost->video_crop) {
695 picture_crop_temp.data[0] = formatted_picture->data[0] +
696 (ost->topBand * formatted_picture->linesize[0]) + ost->leftBand;
698 picture_crop_temp.data[1] = formatted_picture->data[1] +
699 ((ost->topBand >> 1) * formatted_picture->linesize[1]) +
700 (ost->leftBand >> 1);
702 picture_crop_temp.data[2] = formatted_picture->data[2] +
703 ((ost->topBand >> 1) * formatted_picture->linesize[2]) +
704 (ost->leftBand >> 1);
706 picture_crop_temp.linesize[0] = formatted_picture->linesize[0];
707 picture_crop_temp.linesize[1] = formatted_picture->linesize[1];
708 picture_crop_temp.linesize[2] = formatted_picture->linesize[2];
709 final_picture = &picture_crop_temp;
710 } else if (ost->video_pad) {
711 final_picture = &ost->pict_tmp;
713 for (i = 0; i < 3; i++) {
714 uint8_t *optr, *iptr;
715 int shift = (i == 0) ? 0 : 1;
718 /* set offset to start writing image into */
719 optr = final_picture->data[i] + (((final_picture->linesize[i] *
720 ost->padtop) + ost->padleft) >> shift);
721 iptr = formatted_picture->data[i];
723 yheight = (enc->height - ost->padtop - ost->padbottom) >> shift;
724 for (y = 0; y < yheight; y++) {
725 /* copy unpadded image row into padded image row */
726 memcpy(optr, iptr, formatted_picture->linesize[i]);
727 optr += final_picture->linesize[i];
728 iptr += formatted_picture->linesize[i];
732 fill_pad_region(final_picture, enc->height, enc->width,
733 ost->padtop, ost->padbottom, ost->padleft, ost->padright,
736 if (enc->pix_fmt != PIX_FMT_YUV420P) {
740 /* create temporary picture */
741 size = avpicture_get_size(enc->pix_fmt, enc->width, enc->height);
742 buf = av_malloc(size);
745 final_picture = &picture_format_temp;
746 avpicture_fill(final_picture, buf, enc->pix_fmt, enc->width, enc->height);
748 if (img_convert(final_picture, enc->pix_fmt,
749 &ost->pict_tmp, PIX_FMT_YUV420P,
750 enc->width, enc->height) < 0) {
753 fprintf(stderr, "pixel format conversion not handled\n");
759 final_picture = formatted_picture;
761 /* duplicates frame if needed */
762 /* XXX: pb because no interleaving */
763 for(i=0;i<nb_frames;i++) {
764 if (s->oformat->flags & AVFMT_RAWPICTURE) {
765 /* raw pictures are written as AVPicture structure to
766 avoid any copies. We support temorarily the older
768 AVFrame* old_frame = enc->coded_frame;
769 enc->coded_frame = dec->coded_frame;
770 av_write_frame(s, ost->index,
771 (uint8_t *)final_picture, sizeof(AVPicture));
772 enc->coded_frame = old_frame;
776 avcodec_get_frame_defaults(&big_picture);
777 *(AVPicture*)&big_picture= *final_picture;
778 /* better than nothing: use input picture interlaced
780 big_picture.interlaced_frame = in_picture->interlaced_frame;
781 if(do_interlace_me || do_interlace_dct){
782 if(top_field_first == -1)
783 big_picture.top_field_first = in_picture->top_field_first;
785 big_picture.top_field_first = 1;
788 /* handles sameq here. This is not correct because it may
789 not be a global option */
791 big_picture.quality = ist->st->quality;
793 big_picture.quality = ost->st->quality;
794 ret = avcodec_encode_video(enc,
795 video_buffer, VIDEO_BUFFER_SIZE,
797 //enc->frame_number = enc->real_pict_num;
798 av_write_frame(s, ost->index, video_buffer, ret);
800 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
801 // enc->frame_number-1, enc->real_pict_num, ret,
803 /* if two pass, output log */
804 if (ost->logfile && enc->stats_out) {
805 fprintf(ost->logfile, "%s", enc->stats_out);
815 static double psnr(double d){
816 if(d==0) return INFINITY;
817 return -10.0*log(d)/log(10.0);
820 static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
823 static FILE *fvstats=NULL;
830 double ti1, bitrate, avg_bitrate;
834 today = localtime(&today2);
835 sprintf(filename, "vstats_%02d%02d%02d.log", today->tm_hour,
838 fvstats = fopen(filename,"w");
846 enc = &ost->st->codec;
847 if (enc->codec_type == CODEC_TYPE_VIDEO) {
848 frame_number = ost->frame_number;
849 fprintf(fvstats, "frame= %5d q= %2.1f ", frame_number, enc->coded_frame->quality/(float)FF_QP2LAMBDA);
850 if (enc->flags&CODEC_FLAG_PSNR)
851 fprintf(fvstats, "PSNR= %6.2f ", psnr(enc->coded_frame->error[0]/(enc->width*enc->height*255.0*255.0)));
853 fprintf(fvstats,"f_size= %6d ", frame_size);
854 /* compute pts value */
855 ti1 = (double)ost->st->pts.val * os->pts_num / os->pts_den;
859 bitrate = (double)(frame_size * 8) * enc->frame_rate / enc->frame_rate_base / 1000.0;
860 avg_bitrate = (double)(video_size * 8) / ti1 / 1000.0;
861 fprintf(fvstats, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
862 (double)video_size / 1024, ti1, bitrate, avg_bitrate);
863 fprintf(fvstats,"type= %c\n", av_get_pict_type_char(enc->coded_frame->pict_type));
867 static void print_report(AVFormatContext **output_files,
868 AVOutputStream **ost_table, int nb_ostreams,
873 AVFormatContext *oc, *os;
876 int frame_number, vid, i;
877 double bitrate, ti1, pts;
878 static int64_t last_time = -1;
880 if (!is_last_report) {
882 /* display the report every 0.5 seconds */
883 cur_time = av_gettime();
884 if (last_time == -1) {
885 last_time = cur_time;
888 if ((cur_time - last_time) < 500000)
890 last_time = cur_time;
894 oc = output_files[0];
896 total_size = url_ftell(&oc->pb);
901 for(i=0;i<nb_ostreams;i++) {
903 os = output_files[ost->file_index];
904 enc = &ost->st->codec;
905 if (vid && enc->codec_type == CODEC_TYPE_VIDEO) {
906 sprintf(buf + strlen(buf), "q=%2.1f ",
907 enc->coded_frame->quality/(float)FF_QP2LAMBDA);
909 if (!vid && enc->codec_type == CODEC_TYPE_VIDEO) {
910 frame_number = ost->frame_number;
911 sprintf(buf + strlen(buf), "frame=%5d q=%2.1f ",
912 frame_number, enc->coded_frame ? enc->coded_frame->quality/(float)FF_QP2LAMBDA : 0);
914 sprintf(buf + strlen(buf), "L");
915 if (enc->flags&CODEC_FLAG_PSNR){
917 double error, error_sum=0;
918 double scale, scale_sum=0;
919 char type[3]= {'Y','U','V'};
920 sprintf(buf + strlen(buf), "PSNR=");
923 error= enc->error[j];
924 scale= enc->width*enc->height*255.0*255.0*frame_number;
926 error= enc->coded_frame->error[j];
927 scale= enc->width*enc->height*255.0*255.0;
932 sprintf(buf + strlen(buf), "%c:%2.2f ", type[j], psnr(error/scale));
934 sprintf(buf + strlen(buf), "*:%2.2f ", psnr(error_sum/scale_sum));
938 /* compute min output value */
939 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
940 if ((pts < ti1) && (pts > 0))
946 if (verbose || is_last_report) {
947 bitrate = (double)(total_size * 8) / ti1 / 1000.0;
949 sprintf(buf + strlen(buf),
950 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
951 (double)total_size / 1024, ti1, bitrate);
954 fprintf(stderr, "%s \r", buf);
959 if (is_last_report && verbose >= 0){
960 int64_t raw= audio_size + video_size + extra_size;
961 fprintf(stderr, "\n");
962 fprintf(stderr, "video:%1.0fkB audio:%1.0fkB global headers:%1.0fkB muxing overhead %f%%\n",
966 100.0*(total_size - raw)/raw
971 /* pkt = NULL means EOF (needed to flush decoder buffers) */
972 static int output_packet(AVInputStream *ist, int ist_index,
973 AVOutputStream **ost_table, int nb_ostreams,
981 int data_size, got_picture;
983 short samples[AVCODEC_MAX_AUDIO_FRAME_SIZE / 2];
984 void *buffer_to_free;
986 if (pkt && pkt->pts != AV_NOPTS_VALUE) {
989 ist->pts = ist->next_pts;
1003 /* decode the packet if needed */
1004 data_buf = NULL; /* fail safe */
1006 if (ist->decoding_needed) {
1007 switch(ist->st->codec.codec_type) {
1008 case CODEC_TYPE_AUDIO:
1009 /* XXX: could avoid copy if PCM 16 bits with same
1010 endianness as CPU */
1011 ret = avcodec_decode_audio(&ist->st->codec, samples, &data_size,
1017 /* Some bug in mpeg audio decoder gives */
1018 /* data_size < 0, it seems they are overflows */
1019 if (data_size <= 0) {
1020 /* no audio frame */
1023 data_buf = (uint8_t *)samples;
1024 ist->next_pts += ((int64_t)AV_TIME_BASE * data_size) /
1025 (2 * ist->st->codec.channels);
1027 case CODEC_TYPE_VIDEO:
1028 data_size = (ist->st->codec.width * ist->st->codec.height * 3) / 2;
1029 /* XXX: allocate picture correctly */
1030 avcodec_get_frame_defaults(&picture);
1032 ret = avcodec_decode_video(&ist->st->codec,
1033 &picture, &got_picture, ptr, len);
1034 ist->st->quality= picture.quality;
1038 /* no picture yet */
1039 goto discard_packet;
1041 if (ist->st->codec.frame_rate_base != 0) {
1042 ist->next_pts += ((int64_t)AV_TIME_BASE *
1043 ist->st->codec.frame_rate_base) /
1044 ist->st->codec.frame_rate;
1058 buffer_to_free = NULL;
1059 if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO) {
1060 pre_process_video_frame(ist, (AVPicture *)&picture,
1064 /* frame rate emulation */
1065 if (ist->st->codec.rate_emu) {
1066 int64_t pts = av_rescale((int64_t) ist->frame * ist->st->codec.frame_rate_base, 1000000, ist->st->codec.frame_rate);
1067 int64_t now = av_gettime() - ist->start;
1075 /* mpeg PTS deordering : if it is a P or I frame, the PTS
1076 is the one of the next displayed one */
1077 /* XXX: add mpeg4 too ? */
1078 if (ist->st->codec.codec_id == CODEC_ID_MPEG1VIDEO) {
1079 if (ist->st->codec.pict_type != B_TYPE) {
1081 tmp = ist->last_ip_pts;
1082 ist->last_ip_pts = ist->frac_pts.val;
1083 ist->frac_pts.val = tmp;
1087 /* if output time reached then transcode raw format,
1088 encode packets and output them */
1089 if (start_time == 0 || ist->pts >= start_time)
1090 for(i=0;i<nb_ostreams;i++) {
1094 if (ost->source_index == ist_index) {
1095 os = output_files[ost->file_index];
1098 printf("%d: got pts=%0.3f %0.3f\n", i,
1099 (double)pkt->pts / AV_TIME_BASE,
1100 ((double)ist->pts / AV_TIME_BASE) -
1101 ((double)ost->st->pts.val * os->pts_num / os->pts_den));
1103 /* set the input output pts pairs */
1104 ost->sync_ipts = (double)ist->pts / AV_TIME_BASE;
1105 /* XXX: take into account the various fifos,
1106 in particular for audio */
1107 ost->sync_opts = ost->st->pts.val;
1108 //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);
1110 if (ost->encoding_needed) {
1111 switch(ost->st->codec.codec_type) {
1112 case CODEC_TYPE_AUDIO:
1113 do_audio_out(os, ost, ist, data_buf, data_size);
1115 case CODEC_TYPE_VIDEO:
1116 /* find an audio stream for synchro */
1119 AVOutputStream *audio_sync, *ost1;
1121 for(i=0;i<nb_ostreams;i++) {
1122 ost1 = ost_table[i];
1123 if (ost1->file_index == ost->file_index &&
1124 ost1->st->codec.codec_type == CODEC_TYPE_AUDIO) {
1130 do_video_out(os, ost, ist, &picture, &frame_size, audio_sync);
1131 video_size += frame_size;
1132 if (do_vstats && frame_size)
1133 do_video_stats(os, ost, frame_size);
1142 /* no reencoding needed : output the packet directly */
1143 /* force the input stream PTS */
1145 avcodec_get_frame_defaults(&avframe);
1146 ost->st->codec.coded_frame= &avframe;
1147 avframe.key_frame = pkt->flags & PKT_FLAG_KEY;
1148 ost->st->pts.val= av_rescale(ist->pts, os->pts_den, os->pts_num*AV_TIME_BASE);
1150 av_write_frame(os, ost->index, data_buf, data_size);
1151 ost->st->codec.frame_number++;
1152 ost->frame_number++;
1156 av_free(buffer_to_free);
1166 * The following code is the main loop of the file converter
1168 static int av_encode(AVFormatContext **output_files,
1169 int nb_output_files,
1170 AVFormatContext **input_files,
1172 AVStreamMap *stream_maps, int nb_stream_maps)
1174 int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
1175 AVFormatContext *is, *os;
1176 AVCodecContext *codec, *icodec;
1177 AVOutputStream *ost, **ost_table = NULL;
1178 AVInputStream *ist, **ist_table = NULL;
1179 AVInputFile *file_table;
1180 AVFormatContext *stream_no_data;
1183 file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
1187 /* input stream init */
1189 for(i=0;i<nb_input_files;i++) {
1190 is = input_files[i];
1191 file_table[i].ist_index = j;
1192 file_table[i].nb_streams = is->nb_streams;
1193 j += is->nb_streams;
1197 ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
1201 for(i=0;i<nb_istreams;i++) {
1202 ist = av_mallocz(sizeof(AVInputStream));
1208 for(i=0;i<nb_input_files;i++) {
1209 is = input_files[i];
1210 for(k=0;k<is->nb_streams;k++) {
1211 ist = ist_table[j++];
1212 ist->st = is->streams[k];
1213 ist->file_index = i;
1215 ist->discard = 1; /* the stream is discarded by default
1218 if (ist->st->codec.rate_emu) {
1219 ist->start = av_gettime();
1225 /* output stream init */
1227 for(i=0;i<nb_output_files;i++) {
1228 os = output_files[i];
1229 nb_ostreams += os->nb_streams;
1231 if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
1232 fprintf(stderr, "Number of stream maps must match number of output streams\n");
1236 /* Sanity check the mapping args -- do the input files & streams exist? */
1237 for(i=0;i<nb_stream_maps;i++) {
1238 int fi = stream_maps[i].file_index;
1239 int si = stream_maps[i].stream_index;
1241 if (fi < 0 || fi > nb_input_files - 1 ||
1242 si < 0 || si > file_table[fi].nb_streams - 1) {
1243 fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
1248 ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
1251 for(i=0;i<nb_ostreams;i++) {
1252 ost = av_mallocz(sizeof(AVOutputStream));
1259 for(k=0;k<nb_output_files;k++) {
1260 os = output_files[k];
1261 for(i=0;i<os->nb_streams;i++) {
1263 ost = ost_table[n++];
1264 ost->file_index = k;
1266 ost->st = os->streams[i];
1267 if (nb_stream_maps > 0) {
1268 ost->source_index = file_table[stream_maps[n-1].file_index].ist_index +
1269 stream_maps[n-1].stream_index;
1271 /* Sanity check that the stream types match */
1272 if (ist_table[ost->source_index]->st->codec.codec_type != ost->st->codec.codec_type) {
1273 fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
1274 stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
1275 ost->file_index, ost->index);
1280 /* get corresponding input stream index : we select the first one with the right type */
1282 for(j=0;j<nb_istreams;j++) {
1285 ist->st->codec.codec_type == ost->st->codec.codec_type) {
1286 ost->source_index = j;
1292 /* try again and reuse existing stream */
1293 for(j=0;j<nb_istreams;j++) {
1295 if (ist->st->codec.codec_type == ost->st->codec.codec_type) {
1296 ost->source_index = j;
1301 fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
1302 ost->file_index, ost->index);
1307 ist = ist_table[ost->source_index];
1312 /* for each output stream, we compute the right encoding parameters */
1313 for(i=0;i<nb_ostreams;i++) {
1315 ist = ist_table[ost->source_index];
1317 codec = &ost->st->codec;
1318 icodec = &ist->st->codec;
1320 if (ost->st->stream_copy) {
1321 /* if stream_copy is selected, no need to decode or encode */
1322 codec->codec_id = icodec->codec_id;
1323 codec->codec_type = icodec->codec_type;
1324 codec->codec_tag = icodec->codec_tag;
1325 codec->bit_rate = icodec->bit_rate;
1326 switch(codec->codec_type) {
1327 case CODEC_TYPE_AUDIO:
1328 codec->sample_rate = icodec->sample_rate;
1329 codec->channels = icodec->channels;
1330 codec->frame_size = icodec->frame_size;
1332 case CODEC_TYPE_VIDEO:
1333 codec->frame_rate = icodec->frame_rate;
1334 codec->frame_rate_base = icodec->frame_rate_base;
1335 codec->width = icodec->width;
1336 codec->height = icodec->height;
1342 switch(codec->codec_type) {
1343 case CODEC_TYPE_AUDIO:
1344 if (fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
1347 if (codec->channels == icodec->channels &&
1348 codec->sample_rate == icodec->sample_rate) {
1349 ost->audio_resample = 0;
1351 if (codec->channels != icodec->channels &&
1352 icodec->codec_id == CODEC_ID_AC3) {
1353 /* Special case for 5:1 AC3 input */
1354 /* and mono or stereo output */
1355 /* Request specific number of channels */
1356 icodec->channels = codec->channels;
1357 if (codec->sample_rate == icodec->sample_rate)
1358 ost->audio_resample = 0;
1360 ost->audio_resample = 1;
1361 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1363 icodec->sample_rate);
1366 printf("Can't resample. Aborting.\n");
1370 /* Request specific number of channels */
1371 icodec->channels = codec->channels;
1373 ost->audio_resample = 1;
1374 ost->resample = audio_resample_init(codec->channels, icodec->channels,
1376 icodec->sample_rate);
1379 printf("Can't resample. Aborting.\n");
1384 ist->decoding_needed = 1;
1385 ost->encoding_needed = 1;
1387 case CODEC_TYPE_VIDEO:
1388 if (codec->width == icodec->width &&
1389 codec->height == icodec->height &&
1390 frame_topBand == 0 &&
1391 frame_bottomBand == 0 &&
1392 frame_leftBand == 0 &&
1393 frame_rightBand == 0 &&
1394 frame_padtop == 0 &&
1395 frame_padbottom == 0 &&
1396 frame_padleft == 0 &&
1397 frame_padright == 0)
1399 ost->video_resample = 0;
1400 ost->video_crop = 0;
1402 } else if ((codec->width == icodec->width -
1403 (frame_leftBand + frame_rightBand)) &&
1404 (codec->height == icodec->height -
1405 (frame_topBand + frame_bottomBand)))
1407 ost->video_resample = 0;
1408 ost->video_crop = 1;
1409 ost->topBand = frame_topBand;
1410 ost->leftBand = frame_leftBand;
1411 } else if ((codec->width == icodec->width +
1412 (frame_padleft + frame_padright)) &&
1413 (codec->height == icodec->height +
1414 (frame_padtop + frame_padbottom))) {
1415 ost->video_resample = 0;
1416 ost->video_crop = 0;
1418 ost->padtop = frame_padtop;
1419 ost->padleft = frame_padleft;
1420 ost->padbottom = frame_padbottom;
1421 ost->padright = frame_padright;
1422 if( avpicture_alloc( &ost->pict_tmp, PIX_FMT_YUV420P,
1423 codec->width, codec->height ) )
1426 ost->video_resample = 1;
1427 ost->video_crop = 0; // cropping is handled as part of resample
1428 if( avpicture_alloc( &ost->pict_tmp, PIX_FMT_YUV420P,
1429 codec->width, codec->height ) )
1432 ost->img_resample_ctx = img_resample_full_init(
1433 ost->st->codec.width, ost->st->codec.height,
1434 ist->st->codec.width, ist->st->codec.height,
1435 frame_topBand, frame_bottomBand,
1436 frame_leftBand, frame_rightBand,
1437 frame_padtop, frame_padbottom,
1438 frame_padleft, frame_padright);
1440 ost->padtop = frame_padtop;
1441 ost->padleft = frame_padleft;
1442 ost->padbottom = frame_padbottom;
1443 ost->padright = frame_padright;
1446 ost->encoding_needed = 1;
1447 ist->decoding_needed = 1;
1453 if (ost->encoding_needed &&
1454 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
1455 char logfilename[1024];
1460 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
1462 pass_logfilename : DEFAULT_PASS_LOGFILENAME, i);
1463 if (codec->flags & CODEC_FLAG_PASS1) {
1464 f = fopen(logfilename, "w");
1466 perror(logfilename);
1471 /* read the log file */
1472 f = fopen(logfilename, "r");
1474 perror(logfilename);
1477 fseek(f, 0, SEEK_END);
1479 fseek(f, 0, SEEK_SET);
1480 logbuffer = av_malloc(size + 1);
1482 fprintf(stderr, "Could not allocate log buffer\n");
1485 size = fread(logbuffer, 1, size, f);
1487 logbuffer[size] = '\0';
1488 codec->stats_in = logbuffer;
1494 /* dump the file output parameters - cannot be done before in case
1496 for(i=0;i<nb_output_files;i++) {
1497 dump_format(output_files[i], i, output_files[i]->filename, 1);
1500 /* dump the stream mapping */
1502 fprintf(stderr, "Stream mapping:\n");
1503 for(i=0;i<nb_ostreams;i++) {
1505 fprintf(stderr, " Stream #%d.%d -> #%d.%d\n",
1506 ist_table[ost->source_index]->file_index,
1507 ist_table[ost->source_index]->index,
1513 /* open each encoder */
1514 for(i=0;i<nb_ostreams;i++) {
1516 if (ost->encoding_needed) {
1518 codec = avcodec_find_encoder(ost->st->codec.codec_id);
1520 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
1521 ost->file_index, ost->index);
1524 if (avcodec_open(&ost->st->codec, codec) < 0) {
1525 fprintf(stderr, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1526 ost->file_index, ost->index);
1529 extra_size += ost->st->codec.extradata_size;
1533 /* open each decoder */
1534 for(i=0;i<nb_istreams;i++) {
1536 if (ist->decoding_needed) {
1538 codec = avcodec_find_decoder(ist->st->codec.codec_id);
1540 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1541 ist->st->codec.codec_id, ist->file_index, ist->index);
1544 if (avcodec_open(&ist->st->codec, codec) < 0) {
1545 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
1546 ist->file_index, ist->index);
1549 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1550 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1555 for(i=0;i<nb_istreams;i++) {
1557 is = input_files[ist->file_index];
1562 /* compute buffer size max (should use a complete heuristic) */
1563 for(i=0;i<nb_input_files;i++) {
1564 file_table[i].buffer_size_max = 2048;
1567 /* open files and write file headers */
1568 for(i=0;i<nb_output_files;i++) {
1569 os = output_files[i];
1570 if (av_write_header(os) < 0) {
1571 fprintf(stderr, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i);
1577 #ifndef CONFIG_WIN32
1578 if ( !using_stdin && verbose >= 0) {
1579 fprintf(stderr, "Press [q] to stop encoding\n");
1580 url_set_interrupt_cb(decode_interrupt_cb);
1588 for(; received_sigterm == 0;) {
1589 int file_index, ist_index;
1594 /* if 'q' pressed, exits */
1598 /* read_key() returns 0 on EOF */
1604 /* select the stream that we must read now by looking at the
1605 smallest output pts */
1608 for(i=0;i<nb_ostreams;i++) {
1611 os = output_files[ost->file_index];
1612 ist = ist_table[ost->source_index];
1613 pts = (double)ost->st->pts.val * os->pts_num / os->pts_den;
1614 if (!file_table[ist->file_index].eof_reached &&
1617 file_index = ist->file_index;
1620 /* if none, if is finished */
1621 if (file_index < 0) {
1625 /* finish if recording time exhausted */
1626 if (recording_time > 0 && pts_min >= (recording_time / 1000000.0))
1629 /* read a frame from it and output it in the fifo */
1630 is = input_files[file_index];
1631 if (av_read_frame(is, &pkt) < 0) {
1632 file_table[file_index].eof_reached = 1;
1637 stream_no_data = is;
1642 av_pkt_dump(stdout, &pkt, do_hex_dump);
1644 /* the following test is needed in case new streams appear
1645 dynamically in stream : we ignore them */
1646 if (pkt.stream_index >= file_table[file_index].nb_streams)
1647 goto discard_packet;
1648 ist_index = file_table[file_index].ist_index + pkt.stream_index;
1649 ist = ist_table[ist_index];
1651 goto discard_packet;
1653 //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1654 if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
1657 fprintf(stderr, "Error while decoding stream #%d.%d\n",
1658 ist->file_index, ist->index);
1660 av_free_packet(&pkt);
1665 av_free_packet(&pkt);
1667 /* dump report by using the output first video and audio streams */
1668 print_report(output_files, ost_table, nb_ostreams, 0);
1671 /* at the end of stream, we must flush the decoder buffers */
1672 for(i=0;i<nb_istreams;i++) {
1674 if (ist->decoding_needed) {
1675 output_packet(ist, i, ost_table, nb_ostreams, NULL);
1681 /* write the trailer if needed and close file */
1682 for(i=0;i<nb_output_files;i++) {
1683 os = output_files[i];
1684 av_write_trailer(os);
1687 /* dump report by using the first video and audio streams */
1688 print_report(output_files, ost_table, nb_ostreams, 1);
1690 /* close each encoder */
1691 for(i=0;i<nb_ostreams;i++) {
1693 if (ost->encoding_needed) {
1694 av_freep(&ost->st->codec.stats_in);
1695 avcodec_close(&ost->st->codec);
1699 /* close each decoder */
1700 for(i=0;i<nb_istreams;i++) {
1702 if (ist->decoding_needed) {
1703 avcodec_close(&ist->st->codec);
1711 av_free(file_table);
1714 for(i=0;i<nb_istreams;i++) {
1721 for(i=0;i<nb_ostreams;i++) {
1725 fclose(ost->logfile);
1726 ost->logfile = NULL;
1728 fifo_free(&ost->fifo); /* works even if fifo is not
1729 initialized but set to zero */
1730 av_free(ost->pict_tmp.data[0]);
1731 if (ost->video_resample)
1732 img_resample_close(ost->img_resample_ctx);
1733 if (ost->audio_resample)
1734 audio_resample_close(ost->resample);
1747 int file_read(const char *filename)
1750 unsigned char buffer[1024];
1753 if (url_open(&h, filename, O_RDONLY) < 0) {
1754 printf("could not open '%s'\n", filename);
1758 len = url_read(h, buffer, sizeof(buffer));
1761 for(i=0;i<len;i++) putchar(buffer[i]);
1768 static void opt_image_format(const char *arg)
1772 for(f = first_image_format; f != NULL; f = f->next) {
1773 if (!strcmp(arg, f->name))
1777 fprintf(stderr, "Unknown image format: '%s'\n", arg);
1783 static void opt_format(const char *arg)
1785 /* compatibility stuff for pgmyuv */
1786 if (!strcmp(arg, "pgmyuv")) {
1787 opt_image_format(arg);
1791 file_iformat = av_find_input_format(arg);
1792 file_oformat = guess_format(arg, NULL, NULL);
1793 if (!file_iformat && !file_oformat) {
1794 fprintf(stderr, "Unknown input or output format: %s\n", arg);
1799 static void opt_video_bitrate(const char *arg)
1801 video_bit_rate = atoi(arg) * 1000;
1804 static void opt_video_bitrate_tolerance(const char *arg)
1806 video_bit_rate_tolerance = atoi(arg) * 1000;
1809 static void opt_video_bitrate_max(const char *arg)
1811 video_rc_max_rate = atoi(arg) * 1000;
1814 static void opt_video_bitrate_min(const char *arg)
1816 video_rc_min_rate = atoi(arg) * 1000;
1819 static void opt_video_buffer_size(const char *arg)
1821 video_rc_buffer_size = atoi(arg) * 8*1024;
1824 static void opt_video_rc_eq(char *arg)
1829 static void opt_video_rc_override_string(char *arg)
1831 video_rc_override_string = arg;
1835 static void opt_workaround_bugs(const char *arg)
1837 workaround_bugs = atoi(arg);
1840 static void opt_dct_algo(const char *arg)
1842 dct_algo = atoi(arg);
1845 static void opt_idct_algo(const char *arg)
1847 idct_algo = atoi(arg);
1851 static void opt_error_resilience(const char *arg)
1853 error_resilience = atoi(arg);
1856 static void opt_error_concealment(const char *arg)
1858 error_concealment = atoi(arg);
1861 static void opt_debug(const char *arg)
1866 static void opt_vismv(const char *arg)
1868 debug_mv = atoi(arg);
1871 static void opt_verbose(const char *arg)
1873 verbose = atoi(arg);
1874 av_log_set_level(atoi(arg));
1877 static void opt_frame_rate(const char *arg)
1879 if (parse_frame_rate(&frame_rate, &frame_rate_base, arg) < 0) {
1880 fprintf(stderr, "Incorrect frame rate\n");
1885 static void opt_frame_crop_top(const char *arg)
1887 frame_topBand = atoi(arg);
1888 if (frame_topBand < 0) {
1889 fprintf(stderr, "Incorrect top crop size\n");
1892 if ((frame_topBand % 2) != 0) {
1893 fprintf(stderr, "Top crop size must be a multiple of 2\n");
1896 if ((frame_topBand) >= frame_height){
1897 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1900 frame_height -= frame_topBand;
1903 static void opt_frame_crop_bottom(const char *arg)
1905 frame_bottomBand = atoi(arg);
1906 if (frame_bottomBand < 0) {
1907 fprintf(stderr, "Incorrect bottom crop size\n");
1910 if ((frame_bottomBand % 2) != 0) {
1911 fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
1914 if ((frame_bottomBand) >= frame_height){
1915 fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1918 frame_height -= frame_bottomBand;
1921 static void opt_frame_crop_left(const char *arg)
1923 frame_leftBand = atoi(arg);
1924 if (frame_leftBand < 0) {
1925 fprintf(stderr, "Incorrect left crop size\n");
1928 if ((frame_leftBand % 2) != 0) {
1929 fprintf(stderr, "Left crop size must be a multiple of 2\n");
1932 if ((frame_leftBand) >= frame_width){
1933 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1936 frame_width -= frame_leftBand;
1939 static void opt_frame_crop_right(const char *arg)
1941 frame_rightBand = atoi(arg);
1942 if (frame_rightBand < 0) {
1943 fprintf(stderr, "Incorrect right crop size\n");
1946 if ((frame_rightBand % 2) != 0) {
1947 fprintf(stderr, "Right crop size must be a multiple of 2\n");
1950 if ((frame_rightBand) >= frame_width){
1951 fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1954 frame_width -= frame_rightBand;
1957 static void opt_frame_size(const char *arg)
1959 if (parse_image_size(&frame_width, &frame_height, arg) < 0) {
1960 fprintf(stderr, "Incorrect frame size\n");
1963 if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
1964 fprintf(stderr, "Frame size must be a multiple of 2\n");
1970 #define SCALEBITS 10
1971 #define ONE_HALF (1 << (SCALEBITS - 1))
1972 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1974 #define RGB_TO_Y(r, g, b) \
1975 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1976 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1978 #define RGB_TO_U(r1, g1, b1, shift)\
1979 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1980 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1982 #define RGB_TO_V(r1, g1, b1, shift)\
1983 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1984 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1986 static void opt_pad_color(const char *arg) {
1987 /* Input is expected to be six hex digits similar to
1988 how colors are expressed in html tags (but without the #) */
1989 int rgb = strtol(arg, NULL, 16);
1993 g = ((rgb >> 8) & 255);
1996 padcolor[0] = RGB_TO_Y(r,g,b);
1997 padcolor[1] = RGB_TO_U(r,g,b,0);
1998 padcolor[2] = RGB_TO_V(r,g,b,0);
2001 static void opt_frame_pad_top(const char *arg)
2003 frame_padtop = atoi(arg);
2004 if (frame_padtop < 0) {
2005 fprintf(stderr, "Incorrect top pad size\n");
2008 if ((frame_padtop % 2) != 0) {
2009 fprintf(stderr, "Top pad size must be a multiple of 2\n");
2014 static void opt_frame_pad_bottom(const char *arg)
2016 frame_padbottom = atoi(arg);
2017 if (frame_padbottom < 0) {
2018 fprintf(stderr, "Incorrect bottom pad size\n");
2021 if ((frame_padbottom % 2) != 0) {
2022 fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
2028 static void opt_frame_pad_left(const char *arg)
2030 frame_padleft = atoi(arg);
2031 if (frame_padleft < 0) {
2032 fprintf(stderr, "Incorrect left pad size\n");
2035 if ((frame_padleft % 2) != 0) {
2036 fprintf(stderr, "Left pad size must be a multiple of 2\n");
2042 static void opt_frame_pad_right(const char *arg)
2044 frame_padright = atoi(arg);
2045 if (frame_padright < 0) {
2046 fprintf(stderr, "Incorrect right pad size\n");
2049 if ((frame_padright % 2) != 0) {
2050 fprintf(stderr, "Right pad size must be a multiple of 2\n");
2056 static void opt_frame_pix_fmt(const char *arg)
2058 frame_pix_fmt = avcodec_get_pix_fmt(arg);
2061 static void opt_frame_aspect_ratio(const char *arg)
2067 p = strchr(arg, ':');
2069 x = strtol(arg, (char **)&arg, 10);
2071 y = strtol(arg+1, (char **)&arg, 10);
2073 ar = (double)x / (double)y;
2075 ar = strtod(arg, (char **)&arg);
2078 fprintf(stderr, "Incorrect aspect ratio specification.\n");
2081 frame_aspect_ratio = ar;
2084 static void opt_gop_size(const char *arg)
2086 gop_size = atoi(arg);
2089 static void opt_b_frames(const char *arg)
2091 b_frames = atoi(arg);
2092 if (b_frames > FF_MAX_B_FRAMES) {
2093 fprintf(stderr, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES);
2095 } else if (b_frames < 1) {
2096 fprintf(stderr, "\nNumber of B frames must be higher than 0\n");
2101 static void opt_mb_decision(const char *arg)
2103 mb_decision = atoi(arg);
2106 static void opt_mb_cmp(const char *arg)
2111 static void opt_ildct_cmp(const char *arg)
2113 ildct_cmp = atoi(arg);
2116 static void opt_sub_cmp(const char *arg)
2118 sub_cmp = atoi(arg);
2121 static void opt_cmp(const char *arg)
2126 static void opt_pre_cmp(const char *arg)
2128 pre_cmp = atoi(arg);
2131 static void opt_pre_me(const char *arg)
2136 static void opt_lumi_mask(const char *arg)
2138 lumi_mask = atof(arg);
2141 static void opt_dark_mask(const char *arg)
2143 dark_mask = atof(arg);
2146 static void opt_scplx_mask(const char *arg)
2148 scplx_mask = atof(arg);
2151 static void opt_tcplx_mask(const char *arg)
2153 tcplx_mask = atof(arg);
2156 static void opt_p_mask(const char *arg)
2161 static void opt_qscale(const char *arg)
2163 video_qscale = atof(arg);
2164 if (video_qscale < 0.01 ||
2165 video_qscale > 255) {
2166 fprintf(stderr, "qscale must be >= 0.01 and <= 255\n");
2171 static void opt_qmin(const char *arg)
2173 video_qmin = atoi(arg);
2174 if (video_qmin < 0 ||
2176 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
2181 static void opt_qmax(const char *arg)
2183 video_qmax = atoi(arg);
2184 if (video_qmax < 0 ||
2186 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
2191 static void opt_mb_qmin(const char *arg)
2193 video_mb_qmin = atoi(arg);
2194 if (video_mb_qmin < 0 ||
2195 video_mb_qmin > 31) {
2196 fprintf(stderr, "qmin must be >= 1 and <= 31\n");
2201 static void opt_mb_qmax(const char *arg)
2203 video_mb_qmax = atoi(arg);
2204 if (video_mb_qmax < 0 ||
2205 video_mb_qmax > 31) {
2206 fprintf(stderr, "qmax must be >= 1 and <= 31\n");
2211 static void opt_qdiff(const char *arg)
2213 video_qdiff = atoi(arg);
2214 if (video_qdiff < 0 ||
2216 fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
2221 static void opt_qblur(const char *arg)
2223 video_qblur = atof(arg);
2226 static void opt_qcomp(const char *arg)
2228 video_qcomp = atof(arg);
2231 static void opt_rc_initial_cplx(const char *arg)
2233 video_rc_initial_cplx = atof(arg);
2235 static void opt_b_qfactor(const char *arg)
2237 video_b_qfactor = atof(arg);
2239 static void opt_i_qfactor(const char *arg)
2241 video_i_qfactor = atof(arg);
2243 static void opt_b_qoffset(const char *arg)
2245 video_b_qoffset = atof(arg);
2247 static void opt_i_qoffset(const char *arg)
2249 video_i_qoffset = atof(arg);
2252 static void opt_ibias(const char *arg)
2254 video_intra_quant_bias = atoi(arg);
2256 static void opt_pbias(const char *arg)
2258 video_inter_quant_bias = atoi(arg);
2261 static void opt_packet_size(const char *arg)
2263 packet_size= atoi(arg);
2266 static void opt_error_rate(const char *arg)
2268 error_rate= atoi(arg);
2271 static void opt_strict(const char *arg)
2276 static void opt_top_field_first(const char *arg)
2278 top_field_first= atoi(arg);
2281 static void opt_noise_reduction(const char *arg)
2283 noise_reduction= atoi(arg);
2286 static void opt_qns(const char *arg)
2291 static void opt_sc_threshold(const char *arg)
2293 sc_threshold= atoi(arg);
2296 static void opt_thread_count(const char *arg)
2298 thread_count= atoi(arg);
2299 #if !defined(HAVE_PTHREADS) && !defined(HAVE_W32THREADS)
2301 fprintf(stderr, "Warning: not compiled with thread support, using thread emulation\n");
2305 static void opt_audio_bitrate(const char *arg)
2307 audio_bit_rate = atoi(arg) * 1000;
2310 static void opt_audio_rate(const char *arg)
2312 audio_sample_rate = atoi(arg);
2315 static void opt_audio_channels(const char *arg)
2317 audio_channels = atoi(arg);
2320 static void opt_video_device(const char *arg)
2322 video_device = av_strdup(arg);
2325 static void opt_video_channel(const char *arg)
2327 video_channel = strtol(arg, NULL, 0);
2330 static void opt_video_standard(const char *arg)
2332 video_standard = av_strdup(arg);
2335 static void opt_audio_device(const char *arg)
2337 audio_device = av_strdup(arg);
2340 static void opt_dv1394(const char *arg)
2342 video_grab_format = "dv1394";
2343 audio_grab_format = NULL;
2346 static void opt_audio_codec(const char *arg)
2350 if (!strcmp(arg, "copy")) {
2351 audio_stream_copy = 1;
2355 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
2360 fprintf(stderr, "Unknown audio codec '%s'\n", arg);
2363 audio_codec_id = p->id;
2368 static void add_frame_hooker(const char *arg)
2373 char *args = av_strdup(arg);
2377 argv[0] = strtok(args, " ");
2378 while (argc < 62 && (argv[++argc] = strtok(NULL, " "))) {
2381 i = frame_hook_add(argc, argv);
2384 fprintf(stderr, "Failed to add video hook function: %s\n", arg);
2389 const char *motion_str[] = {
2399 static void opt_motion_estimation(const char *arg)
2405 fprintf(stderr, "Unknown motion estimation method '%s'\n", arg);
2408 if (!strcmp(*p, arg))
2412 me_method = (p - motion_str) + 1;
2415 static void opt_video_codec(const char *arg)
2419 if (!strcmp(arg, "copy")) {
2420 video_stream_copy = 1;
2424 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
2429 fprintf(stderr, "Unknown video codec '%s'\n", arg);
2432 video_codec_id = p->id;
2437 static void opt_map(const char *arg)
2443 m = &stream_maps[nb_stream_maps++];
2445 m->file_index = strtol(arg, (char **)&p, 0);
2449 m->stream_index = strtol(p, (char **)&p, 0);
2452 static void opt_recording_time(const char *arg)
2454 recording_time = parse_date(arg, 1);
2457 static void opt_start_time(const char *arg)
2459 start_time = parse_date(arg, 1);
2462 static void opt_rec_timestamp(const char *arg)
2464 rec_timestamp = parse_date(arg, 0) / 1000000;
2467 static void opt_input_file(const char *filename)
2469 AVFormatContext *ic;
2470 AVFormatParameters params, *ap = ¶ms;
2471 int err, i, ret, rfps, rfps_base;
2473 if (!strcmp(filename, "-"))
2476 using_stdin |= !strcmp(filename, "pipe:" ) ||
2477 !strcmp( filename, "/dev/stdin" );
2479 /* get default parameters from command line */
2480 memset(ap, 0, sizeof(*ap));
2481 ap->sample_rate = audio_sample_rate;
2482 ap->channels = audio_channels;
2483 ap->frame_rate = frame_rate;
2484 ap->frame_rate_base = frame_rate_base;
2485 ap->width = frame_width + frame_padleft + frame_padright;
2486 ap->height = frame_height + frame_padtop + frame_padbottom;
2487 ap->image_format = image_format;
2488 ap->pix_fmt = frame_pix_fmt;
2490 /* open the input file with generic libav function */
2491 err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
2493 print_error(filename, err);
2497 /* If not enough info to get the stream parameters, we decode the
2498 first frames to get it. (used in mpeg case for example) */
2499 ret = av_find_stream_info(ic);
2500 if (ret < 0 && verbose >= 0) {
2501 fprintf(stderr, "%s: could not find codec parameters\n", filename);
2505 /* if seeking requested, we execute it */
2506 if (start_time != 0) {
2509 timestamp = start_time;
2510 /* add the stream start time */
2511 if (ic->start_time != AV_NOPTS_VALUE)
2512 timestamp += ic->start_time;
2513 ret = av_seek_frame(ic, -1, timestamp);
2515 fprintf(stderr, "%s: could not seek to position %0.3f\n",
2516 filename, (double)timestamp / AV_TIME_BASE);
2518 /* reset seek info */
2522 /* update the current parameters so that they match the one of the input stream */
2523 for(i=0;i<ic->nb_streams;i++) {
2524 AVCodecContext *enc = &ic->streams[i]->codec;
2525 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2527 avcodec_thread_init(enc, thread_count);
2529 enc->thread_count= thread_count;
2530 switch(enc->codec_type) {
2531 case CODEC_TYPE_AUDIO:
2532 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2533 audio_channels = enc->channels;
2534 audio_sample_rate = enc->sample_rate;
2536 case CODEC_TYPE_VIDEO:
2537 frame_height = enc->height;
2538 frame_width = enc->width;
2539 frame_aspect_ratio = av_q2d(enc->sample_aspect_ratio) * enc->width / enc->height;
2540 frame_pix_fmt = enc->pix_fmt;
2541 rfps = ic->streams[i]->r_frame_rate;
2542 rfps_base = ic->streams[i]->r_frame_rate_base;
2543 enc->workaround_bugs = workaround_bugs;
2544 enc->error_resilience = error_resilience;
2545 enc->error_concealment = error_concealment;
2546 enc->idct_algo = idct_algo;
2548 enc->debug_mv = debug_mv;
2550 enc->flags|= CODEC_FLAG_BITEXACT;
2552 assert(enc->frame_rate_base == rfps_base); // should be true for now
2553 if (enc->frame_rate != rfps) {
2556 fprintf(stderr,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2557 i, (float)enc->frame_rate / enc->frame_rate_base,
2559 (float)rfps / rfps_base);
2561 /* update the current frame rate to match the stream frame rate */
2563 frame_rate_base = rfps_base;
2565 enc->rate_emu = rate_emu;
2567 case CODEC_TYPE_DATA:
2574 input_files[nb_input_files] = ic;
2575 /* dump the file content */
2577 dump_format(ic, nb_input_files, filename, 0);
2580 file_iformat = NULL;
2581 file_oformat = NULL;
2582 image_format = NULL;
2587 static void check_audio_video_inputs(int *has_video_ptr, int *has_audio_ptr)
2589 int has_video, has_audio, i, j;
2590 AVFormatContext *ic;
2594 for(j=0;j<nb_input_files;j++) {
2595 ic = input_files[j];
2596 for(i=0;i<ic->nb_streams;i++) {
2597 AVCodecContext *enc = &ic->streams[i]->codec;
2598 switch(enc->codec_type) {
2599 case CODEC_TYPE_AUDIO:
2602 case CODEC_TYPE_VIDEO:
2605 case CODEC_TYPE_DATA:
2612 *has_video_ptr = has_video;
2613 *has_audio_ptr = has_audio;
2616 static void opt_output_file(const char *filename)
2619 AVFormatContext *oc;
2620 int use_video, use_audio, nb_streams, input_has_video, input_has_audio;
2622 AVFormatParameters params, *ap = ¶ms;
2624 if (!strcmp(filename, "-"))
2627 oc = av_alloc_format_context();
2629 if (!file_oformat) {
2630 file_oformat = guess_format(NULL, filename, NULL);
2631 if (!file_oformat) {
2632 fprintf(stderr, "Unable for find a suitable output format for '%s'\n",
2638 oc->oformat = file_oformat;
2640 if (!strcmp(file_oformat->name, "ffm") &&
2641 strstart(filename, "http:", NULL)) {
2642 /* special case for files sent to ffserver: we get the stream
2643 parameters from ffserver */
2644 if (read_ffserver_streams(oc, filename) < 0) {
2645 fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
2649 use_video = file_oformat->video_codec != CODEC_ID_NONE;
2650 use_audio = file_oformat->audio_codec != CODEC_ID_NONE;
2652 /* disable if no corresponding type found and at least one
2654 if (nb_input_files > 0) {
2655 check_audio_video_inputs(&input_has_video, &input_has_audio);
2656 if (!input_has_video)
2658 if (!input_has_audio)
2662 /* manual disable */
2663 if (audio_disable) {
2666 if (video_disable) {
2672 AVCodecContext *video_enc;
2674 st = av_mallocz(sizeof(AVStream));
2676 fprintf(stderr, "Could not alloc stream\n");
2679 avcodec_get_context_defaults(&st->codec);
2680 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2682 avcodec_thread_init(&st->codec, thread_count);
2685 video_enc = &st->codec;
2687 if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp"))
2688 video_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
2689 if (video_stream_copy) {
2690 st->stream_copy = 1;
2691 video_enc->codec_type = CODEC_TYPE_VIDEO;
2696 codec_id = file_oformat->video_codec;
2697 if (video_codec_id != CODEC_ID_NONE)
2698 codec_id = video_codec_id;
2700 video_enc->codec_id = codec_id;
2702 video_enc->bit_rate = video_bit_rate;
2703 video_enc->bit_rate_tolerance = video_bit_rate_tolerance;
2704 video_enc->frame_rate = frame_rate;
2705 video_enc->frame_rate_base = frame_rate_base;
2707 video_enc->width = frame_width + frame_padright + frame_padleft;
2708 video_enc->height = frame_height + frame_padtop + frame_padbottom;
2709 video_enc->sample_aspect_ratio = av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
2710 video_enc->pix_fmt = frame_pix_fmt;
2713 video_enc->gop_size = gop_size;
2715 video_enc->gop_size = 0;
2716 if (video_qscale || same_quality) {
2717 video_enc->flags |= CODEC_FLAG_QSCALE;
2718 st->quality = FF_QP2LAMBDA * video_qscale;
2722 video_enc->intra_matrix = intra_matrix;
2724 video_enc->inter_matrix = inter_matrix;
2727 video_enc->flags |= CODEC_FLAG_BITEXACT;
2729 video_enc->mb_decision = mb_decision;
2730 video_enc->mb_cmp = mb_cmp;
2731 video_enc->ildct_cmp = ildct_cmp;
2732 video_enc->me_sub_cmp = sub_cmp;
2733 video_enc->me_cmp = cmp;
2734 video_enc->me_pre_cmp = pre_cmp;
2735 video_enc->pre_me = pre_me;
2736 video_enc->lumi_masking = lumi_mask;
2737 video_enc->dark_masking = dark_mask;
2738 video_enc->spatial_cplx_masking = scplx_mask;
2739 video_enc->temporal_cplx_masking = tcplx_mask;
2740 video_enc->p_masking = p_mask;
2741 video_enc->quantizer_noise_shaping= qns;
2744 video_enc->flags |= CODEC_FLAG_H263P_UMV;
2747 video_enc->flags |= CODEC_FLAG_H263P_SLICE_STRUCT;
2750 video_enc->flags |= CODEC_FLAG_H263P_AIC;
2753 video_enc->flags |= CODEC_FLAG_H263P_AIV;
2756 video_enc->flags |= CODEC_FLAG_4MV;
2759 video_enc->flags |= CODEC_FLAG_OBMC;
2762 video_enc->flags |= CODEC_FLAG_LOOP_FILTER;
2766 video_enc->flags |= CODEC_FLAG_PART;
2769 video_enc->flags |= CODEC_FLAG_ALT_SCAN;
2772 video_enc->flags |= CODEC_FLAG_TRELLIS_QUANT;
2774 if (use_scan_offset) {
2775 video_enc->flags |= CODEC_FLAG_SVCD_SCAN_OFFSET;
2778 video_enc->flags |= CODEC_FLAG_CLOSED_GOP;
2781 video_enc->max_b_frames = b_frames;
2782 video_enc->b_frame_strategy = 0;
2783 video_enc->b_quant_factor = 2.0;
2785 if (do_interlace_dct) {
2786 video_enc->flags |= CODEC_FLAG_INTERLACED_DCT;
2788 if (do_interlace_me) {
2789 video_enc->flags |= CODEC_FLAG_INTERLACED_ME;
2791 video_enc->qmin = video_qmin;
2792 video_enc->qmax = video_qmax;
2793 video_enc->mb_qmin = video_mb_qmin;
2794 video_enc->mb_qmax = video_mb_qmax;
2795 video_enc->max_qdiff = video_qdiff;
2796 video_enc->qblur = video_qblur;
2797 video_enc->qcompress = video_qcomp;
2798 video_enc->rc_eq = video_rc_eq;
2799 video_enc->debug = debug;
2800 video_enc->debug_mv = debug_mv;
2801 video_enc->thread_count = thread_count;
2802 p= video_rc_override_string;
2805 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
2807 fprintf(stderr, "error parsing rc_override\n");
2810 video_enc->rc_override=
2811 av_realloc(video_enc->rc_override,
2812 sizeof(RcOverride)*(i+1));
2813 video_enc->rc_override[i].start_frame= start;
2814 video_enc->rc_override[i].end_frame = end;
2816 video_enc->rc_override[i].qscale= q;
2817 video_enc->rc_override[i].quality_factor= 1.0;
2820 video_enc->rc_override[i].qscale= 0;
2821 video_enc->rc_override[i].quality_factor= -q/100.0;
2826 video_enc->rc_override_count=i;
2828 video_enc->rc_max_rate = video_rc_max_rate;
2829 video_enc->rc_min_rate = video_rc_min_rate;
2830 video_enc->rc_buffer_size = video_rc_buffer_size;
2831 video_enc->rc_buffer_aggressivity= video_rc_buffer_aggressivity;
2832 video_enc->rc_initial_cplx= video_rc_initial_cplx;
2833 video_enc->i_quant_factor = video_i_qfactor;
2834 video_enc->b_quant_factor = video_b_qfactor;
2835 video_enc->i_quant_offset = video_i_qoffset;
2836 video_enc->b_quant_offset = video_b_qoffset;
2837 video_enc->intra_quant_bias = video_intra_quant_bias;
2838 video_enc->inter_quant_bias = video_inter_quant_bias;
2839 video_enc->dct_algo = dct_algo;
2840 video_enc->idct_algo = idct_algo;
2841 video_enc->strict_std_compliance = strict;
2842 video_enc->error_rate = error_rate;
2843 video_enc->noise_reduction= noise_reduction;
2844 video_enc->scenechange_threshold= sc_threshold;
2846 video_enc->rtp_mode= 1;
2847 video_enc->rtp_payload_size= packet_size;
2851 video_enc->flags|= CODEC_FLAG_PSNR;
2853 video_enc->me_method = me_method;
2858 video_enc->flags |= CODEC_FLAG_PASS1;
2860 video_enc->flags |= CODEC_FLAG_PASS2;
2864 oc->streams[nb_streams] = st;
2869 AVCodecContext *audio_enc;
2871 st = av_mallocz(sizeof(AVStream));
2873 fprintf(stderr, "Could not alloc stream\n");
2876 avcodec_get_context_defaults(&st->codec);
2877 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2879 avcodec_thread_init(&st->codec, thread_count);
2882 audio_enc = &st->codec;
2883 audio_enc->codec_type = CODEC_TYPE_AUDIO;
2885 if(!strcmp(file_oformat->name, "mp4") || !strcmp(file_oformat->name, "mov") || !strcmp(file_oformat->name, "3gp"))
2886 audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
2887 if (audio_stream_copy) {
2888 st->stream_copy = 1;
2890 codec_id = file_oformat->audio_codec;
2891 if (audio_codec_id != CODEC_ID_NONE)
2892 codec_id = audio_codec_id;
2893 audio_enc->codec_id = codec_id;
2895 audio_enc->bit_rate = audio_bit_rate;
2896 audio_enc->sample_rate = audio_sample_rate;
2897 audio_enc->strict_std_compliance = strict;
2898 audio_enc->thread_count = thread_count;
2899 /* For audio codecs other than AC3 we limit */
2900 /* the number of coded channels to stereo */
2901 if (audio_channels > 2 && codec_id != CODEC_ID_AC3) {
2902 audio_enc->channels = 2;
2904 audio_enc->channels = audio_channels;
2906 oc->streams[nb_streams] = st;
2910 oc->nb_streams = nb_streams;
2913 fprintf(stderr, "No audio or video streams available\n");
2917 oc->timestamp = rec_timestamp;
2920 pstrcpy(oc->title, sizeof(oc->title), str_title);
2922 pstrcpy(oc->author, sizeof(oc->author), str_author);
2924 pstrcpy(oc->copyright, sizeof(oc->copyright), str_copyright);
2926 pstrcpy(oc->comment, sizeof(oc->comment), str_comment);
2929 output_files[nb_output_files++] = oc;
2931 strcpy(oc->filename, filename);
2933 /* check filename in case of an image number is expected */
2934 if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
2935 if (filename_number_test(oc->filename) < 0) {
2936 print_error(oc->filename, AVERROR_NUMEXPECTED);
2941 if (!(oc->oformat->flags & AVFMT_NOFILE)) {
2942 /* test if it already exists to avoid loosing precious files */
2943 if (!file_overwrite &&
2944 (strchr(filename, ':') == NULL ||
2945 strstart(filename, "file:", NULL))) {
2946 if (url_exist(filename)) {
2949 if ( !using_stdin ) {
2950 fprintf(stderr,"File '%s' already exists. Overwrite ? [y/N] ", filename);
2953 if (toupper(c) != 'Y') {
2954 fprintf(stderr, "Not overwriting - exiting\n");
2959 fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
2966 if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
2967 fprintf(stderr, "Could not open '%s'\n", filename);
2972 memset(ap, 0, sizeof(*ap));
2973 ap->image_format = image_format;
2974 if (av_set_parameters(oc, ap) < 0) {
2975 fprintf(stderr, "%s: Invalid encoding parameters\n",
2980 /* reset some options */
2981 file_oformat = NULL;
2982 file_iformat = NULL;
2983 image_format = NULL;
2986 audio_codec_id = CODEC_ID_NONE;
2987 video_codec_id = CODEC_ID_NONE;
2988 audio_stream_copy = 0;
2989 video_stream_copy = 0;
2992 /* prepare dummy protocols for grab */
2993 static void prepare_grab(void)
2995 int has_video, has_audio, i, j;
2996 AVFormatContext *oc;
2997 AVFormatContext *ic;
2998 AVFormatParameters vp1, *vp = &vp1;
2999 AVFormatParameters ap1, *ap = &ap1;
3001 /* see if audio/video inputs are needed */
3004 memset(ap, 0, sizeof(*ap));
3005 memset(vp, 0, sizeof(*vp));
3006 for(j=0;j<nb_output_files;j++) {
3007 oc = output_files[j];
3008 for(i=0;i<oc->nb_streams;i++) {
3009 AVCodecContext *enc = &oc->streams[i]->codec;
3010 switch(enc->codec_type) {
3011 case CODEC_TYPE_AUDIO:
3012 if (enc->sample_rate > ap->sample_rate)
3013 ap->sample_rate = enc->sample_rate;
3014 if (enc->channels > ap->channels)
3015 ap->channels = enc->channels;
3018 case CODEC_TYPE_VIDEO:
3019 if (enc->width > vp->width)
3020 vp->width = enc->width;
3021 if (enc->height > vp->height)
3022 vp->height = enc->height;
3024 assert(enc->frame_rate_base == DEFAULT_FRAME_RATE_BASE);
3025 if (enc->frame_rate > vp->frame_rate){
3026 vp->frame_rate = enc->frame_rate;
3027 vp->frame_rate_base = enc->frame_rate_base;
3037 if (has_video == 0 && has_audio == 0) {
3038 fprintf(stderr, "Output file must have at least one audio or video stream\n");
3043 AVInputFormat *fmt1;
3044 fmt1 = av_find_input_format(video_grab_format);
3045 vp->device = video_device;
3046 vp->channel = video_channel;
3047 vp->standard = video_standard;
3048 if (av_open_input_file(&ic, "", fmt1, 0, vp) < 0) {
3049 fprintf(stderr, "Could not find video grab device\n");
3052 /* If not enough info to get the stream parameters, we decode the
3053 first frames to get it. */
3054 if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && av_find_stream_info(ic) < 0) {
3055 fprintf(stderr, "Could not find video grab parameters\n");
3058 /* by now video grab has one stream */
3059 ic->streams[0]->r_frame_rate = vp->frame_rate;
3060 ic->streams[0]->r_frame_rate_base = vp->frame_rate_base;
3061 input_files[nb_input_files] = ic;
3064 dump_format(ic, nb_input_files, "", 0);
3068 if (has_audio && audio_grab_format) {
3069 AVInputFormat *fmt1;
3070 fmt1 = av_find_input_format(audio_grab_format);
3071 ap->device = audio_device;
3072 if (av_open_input_file(&ic, "", fmt1, 0, ap) < 0) {
3073 fprintf(stderr, "Could not find audio grab device\n");
3076 input_files[nb_input_files] = ic;
3079 dump_format(ic, nb_input_files, "", 0);
3085 /* same option as mencoder */
3086 static void opt_pass(const char *pass_str)
3089 pass = atoi(pass_str);
3090 if (pass != 1 && pass != 2) {
3091 fprintf(stderr, "pass number can be only 1 or 2\n");
3097 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
3098 static int64_t getutime(void)
3100 return av_gettime();
3103 static int64_t getutime(void)
3105 struct rusage rusage;
3107 getrusage(RUSAGE_SELF, &rusage);
3108 return (rusage.ru_utime.tv_sec * 1000000LL) + rusage.ru_utime.tv_usec;
3112 extern int ffm_nopts;
3114 static void opt_bitexact(void)
3117 /* disable generate of real time pts in ffm (need to be supressed anyway) */
3121 static void show_formats(void)
3123 AVInputFormat *ifmt;
3124 AVOutputFormat *ofmt;
3125 AVImageFormat *image_fmt;
3128 const char **pp, *last_name;
3130 printf("File formats:\n");
3135 const char *name=NULL;
3137 for(ofmt = first_oformat; ofmt != NULL; ofmt = ofmt->next) {
3138 if((name == NULL || strcmp(ofmt->name, name)<0) &&
3139 strcmp(ofmt->name, last_name)>0){
3144 for(ifmt = first_iformat; ifmt != NULL; ifmt = ifmt->next) {
3145 if((name == NULL || strcmp(ifmt->name, name)<0) &&
3146 strcmp(ifmt->name, last_name)>0){
3150 if(name && strcmp(ifmt->name, name)==0)
3165 printf("Image formats:\n");
3166 for(image_fmt = first_image_format; image_fmt != NULL;
3167 image_fmt = image_fmt->next) {
3170 image_fmt->img_read ? "D":" ",
3171 image_fmt->img_write ? "E":" ",
3176 printf("Codecs:\n");
3184 for(p = first_avcodec; p != NULL; p = p->next) {
3185 if((p2==NULL || strcmp(p->name, p2->name)<0) &&
3186 strcmp(p->name, last_name)>0){
3188 decode= encode= cap=0;
3190 if(p2 && strcmp(p->name, p2->name)==0){
3191 if(p->decode) decode=1;
3192 if(p->encode) encode=1;
3193 cap |= p->capabilities;
3198 last_name= p2->name;
3202 decode ? "D": (/*p2->decoder ? "d":*/" "),
3204 p2->type == CODEC_TYPE_AUDIO ? "A":"V",
3205 cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
3206 cap & CODEC_CAP_DR1 ? "D":" ",
3207 cap & CODEC_CAP_TRUNCATED ? "T":" ",
3209 /* if(p2->decoder && decode==0)
3210 printf(" use %s for decoding", p2->decoder->name);*/
3215 printf("Supported file protocols:\n");
3216 for(up = first_protocol; up != NULL; up = up->next)
3217 printf(" %s:", up->name);
3220 printf("Frame size, frame rate abbreviations:\n ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
3221 printf("Motion estimation methods:\n");
3225 if ((pp - motion_str + 1) == ME_ZERO)
3226 printf("(fastest)");
3227 else if ((pp - motion_str + 1) == ME_FULL)
3228 printf("(slowest)");
3229 else if ((pp - motion_str + 1) == ME_EPZS)
3230 printf("(default)");
3235 "Note, the names of encoders and decoders dont always match, so there are\n"
3236 "several cases where the above table shows encoder only or decoder only entries\n"
3237 "even though both encoding and decoding are supported for example, the h263\n"
3238 "decoder corresponds to the h263 and h263p encoders, for file formats its even\n"
3243 void parse_matrix_coeffs(uint16_t *dest, const char *str)
3246 const char *p = str;
3253 fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
3260 void opt_inter_matrix(const char *arg)
3262 inter_matrix = av_mallocz(sizeof(uint16_t) * 64);
3263 parse_matrix_coeffs(inter_matrix, arg);
3266 void opt_intra_matrix(const char *arg)
3268 intra_matrix = av_mallocz(sizeof(uint16_t) * 64);
3269 parse_matrix_coeffs(intra_matrix, arg);
3272 static void opt_target(const char *arg)
3276 if(!strncmp(arg, "pal-", 4)) {
3279 } else if(!strncmp(arg, "ntsc-", 5)) {
3284 /* Calculate FR via float to avoid int overflow */
3285 fr = (int)(frame_rate * 1000.0 / frame_rate_base);
3288 } else if((fr == 29970) || (fr == 23976)) {
3291 /* Try to determine PAL/NTSC by peeking in the input files */
3292 if(nb_input_files) {
3294 for(j = 0; j < nb_input_files; j++) {
3295 for(i = 0; i < input_files[j]->nb_streams; i++) {
3296 AVCodecContext *c = &input_files[j]->streams[i]->codec;
3297 if(c->codec_type != CODEC_TYPE_VIDEO)
3299 fr = c->frame_rate * 1000 / c->frame_rate_base;
3303 } else if((fr == 29970) || (fr == 23976)) {
3313 if(verbose && norm >= 0)
3314 printf("Assuming %s for target.\n", norm ? "NTSC" : "PAL");
3318 fprintf(stderr, "Could not determine norm (PAL/NTSC) for target.\n");
3319 fprintf(stderr, "Please prefix target with \"pal-\" or \"ntsc-\",\n");
3320 fprintf(stderr, "or set a framerate with \"-r xxx\".\n");
3324 if(!strcmp(arg, "vcd")) {
3326 opt_video_codec("mpeg1video");
3327 opt_audio_codec("mp2");
3330 opt_frame_size(norm ? "352x240" : "352x288");
3332 video_bit_rate = 1150000;
3333 video_rc_max_rate = 1150000;
3334 video_rc_min_rate = 1150000;
3335 video_rc_buffer_size = 40*1024*8;
3337 audio_bit_rate = 224000;
3338 audio_sample_rate = 44100;
3340 } else if(!strcmp(arg, "svcd")) {
3342 opt_video_codec("mpeg2video");
3343 opt_audio_codec("mp2");
3346 opt_frame_size(norm ? "480x480" : "480x576");
3347 opt_gop_size(norm ? "18" : "15");
3349 video_bit_rate = 2040000;
3350 video_rc_max_rate = 2516000;
3351 video_rc_min_rate = 0; //1145000;
3352 video_rc_buffer_size = 224*1024*8;
3353 use_scan_offset = 1;
3355 audio_bit_rate = 224000;
3356 audio_sample_rate = 44100;
3358 } else if(!strcmp(arg, "dvd")) {
3360 opt_video_codec("mpeg2video");
3361 opt_audio_codec("ac3");
3364 opt_frame_size(norm ? "720x480" : "720x576");
3365 opt_gop_size(norm ? "18" : "15");
3367 video_bit_rate = 6000000;
3368 video_rc_max_rate = 9000000;
3369 video_rc_min_rate = 0; //1500000;
3370 video_rc_buffer_size = 224*1024*8;
3372 audio_bit_rate = 448000;
3373 audio_sample_rate = 48000;
3376 fprintf(stderr, "Unknown target: %s\n", arg);
3381 const OptionDef options[] = {
3383 { "L", 0, {(void*)show_license}, "show license" },
3384 { "h", 0, {(void*)show_help}, "show help" },
3385 { "formats", 0, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
3386 { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
3387 { "img", HAS_ARG, {(void*)opt_image_format}, "force image format", "img_fmt" },
3388 { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
3389 { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
3390 { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream" },
3391 { "t", HAS_ARG, {(void*)opt_recording_time}, "set the recording time", "duration" },
3392 { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" },
3393 { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
3394 { "timestamp", HAS_ARG, {(void*)&opt_rec_timestamp}, "set the timestamp", "time" },
3395 { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
3396 { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
3397 { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
3398 { "debug", HAS_ARG | OPT_EXPERT, {(void*)opt_debug}, "print specific debug info", "" },
3399 { "vismv", HAS_ARG | OPT_EXPERT, {(void*)opt_vismv}, "visualize motion vectors", "" },
3400 { "benchmark", OPT_BOOL | OPT_EXPERT, {(void*)&do_benchmark},
3401 "add timings for benchmarking" },
3402 { "dump", OPT_BOOL | OPT_EXPERT, {(void*)&do_pkt_dump},
3403 "dump each input packet" },
3404 { "hex", OPT_BOOL | OPT_EXPERT, {(void*)&do_hex_dump},
3405 "when dumping packets, also dump the payload" },
3406 { "bitexact", OPT_EXPERT, {(void*)opt_bitexact}, "only use bit exact algorithms (for codec testing)" },
3407 { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
3408 { "loop", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
3409 { "v", HAS_ARG, {(void*)opt_verbose}, "control amount of logging", "verbose" },
3410 { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\" or \"dvd\")", "type" },
3411 { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
3414 { "b", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate}, "set video bitrate (in kbit/s)", "bitrate" },
3415 { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
3416 { "s", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_size}, "set frame size (WxH or abbreviation)", "size" },
3417 { "aspect", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_aspect_ratio}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
3418 { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_frame_pix_fmt}, "set pixel format", "format" },
3419 { "croptop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_top}, "set top crop band size (in pixels)", "size" },
3420 { "cropbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_bottom}, "set bottom crop band size (in pixels)", "size" },
3421 { "cropleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_left}, "set left crop band size (in pixels)", "size" },
3422 { "cropright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_crop_right}, "set right crop band size (in pixels)", "size" },
3423 { "padtop", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_pad_top}, "set top pad band size (in pixels)", "size" },
3424 { "padbottom", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_pad_bottom}, "set bottom pad band size (in pixels)", "size" },
3425 { "padleft", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_pad_left}, "set left pad band size (in pixels)", "size" },
3426 { "padright", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_pad_right}, "set right pad band size (in pixels)", "size" },
3427 { "padcolor", HAS_ARG | OPT_VIDEO, {(void*)opt_pad_color}, "set color of pad bands (Hex 000000 thru FFFFFF)", "color" },
3428 { "g", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_gop_size}, "set the group of picture size", "gop_size" },
3429 { "intra", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&intra_only}, "use only intra frames"},
3430 { "vn", OPT_BOOL | OPT_VIDEO, {(void*)&video_disable}, "disable video" },
3431 { "qscale", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qscale}, "use fixed video quantiser scale (VBR)", "q" },
3432 { "qmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmin}, "min video quantiser scale (VBR)", "q" },
3433 { "qmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qmax}, "max video quantiser scale (VBR)", "q" },
3434 { "mbqmin", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmin}, "min macroblock quantiser scale (VBR)", "q" },
3435 { "mbqmax", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_qmax}, "max macroblock quantiser scale (VBR)", "q" },
3436 { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantiser scale (VBR)", "q" },
3437 { "qblur", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qblur}, "video quantiser scale blur (VBR)", "blur" },
3438 { "qcomp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qcomp}, "video quantiser scale compression (VBR)", "compression" },
3439 { "rc_init_cplx", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_rc_initial_cplx}, "initial complexity for 1-pass encoding", "complexity" },
3440 { "b_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qfactor}, "qp factor between p and b frames", "factor" },
3441 { "i_qfactor", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qfactor}, "qp factor between p and i frames", "factor" },
3442 { "b_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_qoffset}, "qp offset between p and b frames", "offset" },
3443 { "i_qoffset", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_i_qoffset}, "qp offset between p and i frames", "offset" },
3444 { "ibias", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_ibias}, "intra quant bias", "bias" },
3445 { "pbias", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_pbias}, "inter quant bias", "bias" },
3446 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
3447 { "rc_eq", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_eq}, "set rate control equation", "equation" },
3448 { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" },
3449 { "bt", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_tolerance}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
3450 { "maxrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_max}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
3451 { "minrate", HAS_ARG | OPT_VIDEO, {(void*)opt_video_bitrate_min}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
3452 { "bufsize", HAS_ARG | OPT_VIDEO, {(void*)opt_video_buffer_size}, "set ratecontrol buffere size (in kByte)", "size" },
3453 { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
3454 { "me", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_motion_estimation}, "set motion estimation method",
3456 { "dct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dct_algo}, "set dct algo", "algo" },
3457 { "idct_algo", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_idct_algo}, "set idct algo", "algo" },
3458 { "er", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_resilience}, "set error resilience", "n" },
3459 { "ec", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_error_concealment}, "set error concealment", "bit_mask" },
3460 { "bf", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_b_frames}, "use 'frames' B frames", "frames" },
3461 { "hq", OPT_BOOL, {(void*)&mb_decision}, "activate high quality settings" },
3462 { "mbd", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_decision}, "macroblock decision", "mode" },
3463 { "mbcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_mb_cmp}, "macroblock compare function", "cmp function" },
3464 { "ildctcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_ildct_cmp}, "ildct compare function", "cmp function" },
3465 { "subcmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_sub_cmp}, "subpel compare function", "cmp function" },
3466 { "cmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_cmp}, "fullpel compare function", "cmp function" },
3467 { "precmp", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_pre_cmp}, "pre motion estimation compare function", "cmp function" },
3468 { "preme", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_pre_me}, "pre motion estimation", "" },
3469 { "lumi_mask", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_lumi_mask}, "luminance masking", "" },
3470 { "dark_mask", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_dark_mask}, "darkness masking", "" },
3471 { "scplx_mask", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_scplx_mask}, "spatial complexity masking", "" },
3472 { "tcplx_mask", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_tcplx_mask}, "teporal complexity masking", "" },
3473 { "p_mask", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_p_mask}, "inter masking", "" },
3474 { "4mv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_4mv}, "use four motion vector by macroblock (MPEG4)" },
3475 { "obmc", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_obmc}, "use overlapped block motion compensation (h263+)" },
3476 { "lf", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_loop}, "use loop filter (h263+)" },
3477 { "part", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_part}, "use data partitioning (MPEG4)" },
3478 { "bug", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_workaround_bugs}, "workaround not auto detected encoder bugs", "param" },
3479 { "ps", HAS_ARG | OPT_EXPERT, {(void*)opt_packet_size}, "set packet size in bits", "size" },
3480 { "error", HAS_ARG | OPT_EXPERT, {(void*)opt_error_rate}, "error rate", "rate" },
3481 { "strict", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_strict}, "how strictly to follow the standards", "strictness" },
3482 { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality},
3483 "use same video quality as source (implies VBR)" },
3484 { "pass", HAS_ARG | OPT_VIDEO, {(void*)&opt_pass}, "select the pass number (1 or 2)", "n" },
3485 { "passlogfile", HAS_ARG | OPT_STRING | OPT_VIDEO, {(void*)&pass_logfilename}, "select two pass log file name", "file" },
3486 { "deinterlace", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_deinterlace},
3487 "deinterlace pictures" },
3488 { "ildct", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_interlace_dct},
3489 "force interlaced dct support in encoder (MPEG2/MPEG4)" },
3490 { "ilme", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_interlace_me},
3491 "force interlaced me support in encoder (MPEG2/MPEG4)" },
3492 { "psnr", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_psnr}, "calculate PSNR of compressed frames" },
3493 { "vstats", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&do_vstats}, "dump video coding statistics to file" },
3494 { "vhook", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)add_frame_hooker}, "insert video processing module", "module" },
3495 { "aic", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aic}, "enable Advanced intra coding (h263+)" },
3496 { "aiv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_aiv}, "enable Alternative inter vlc (h263+)" },
3497 { "umv", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_umv}, "enable Unlimited Motion Vector (h263+)" },
3498 { "ssm", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_ss}, "enable Slice Structured mode (h263+)" },
3499 { "alt", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_alt_scan}, "enable alternate scantable (MPEG2/MPEG4)" },
3500 { "trell", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_trell}, "enable trellis quantization" },
3501 { "cgop", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&closed_gop}, "closed gop" },
3502 { "scan_offset", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, {(void*)&use_scan_offset}, "enable SVCD Scan Offset placeholder" },
3503 { "intra_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_intra_matrix}, "specify intra matrix coeffs", "matrix" },
3504 { "inter_matrix", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_inter_matrix}, "specify inter matrix coeffs", "matrix" },
3505 { "top", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_top_field_first}, "top=1/bottom=0/auto=-1 field first", "" },
3506 { "nr", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_noise_reduction}, "noise reduction", "" },
3507 { "qns", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qns}, "quantization noise shaping", "" },
3508 { "sc_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_sc_threshold}, "scene change threshold", "threshold" },
3511 { "ab", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_bitrate}, "set audio bitrate (in kbit/s)", "bitrate", },
3512 { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
3513 { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
3514 { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" },
3515 { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
3518 { "vd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_device}, "set video grab device", "device" },
3519 { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "set video grab channel (DV1394 only)", "channel" },
3520 { "tvstd", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_standard}, "set television standard (NTSC, PAL (SECAM))", "standard" },
3521 { "dv1394", OPT_EXPERT | OPT_GRAB, {(void*)opt_dv1394}, "set DV1394 grab", "" },
3522 { "ad", HAS_ARG | OPT_EXPERT | OPT_AUDIO | OPT_GRAB, {(void*)opt_audio_device}, "set audio device", "device" },
3526 static void show_banner(void)
3528 printf("ffmpeg version " FFMPEG_VERSION ", Copyright (c) 2000-2003 Fabrice Bellard\n");
3531 static void show_license(void)
3536 "This program is free software; you can redistribute it and/or modify\n"
3537 "it under the terms of the GNU General Public License as published by\n"
3538 "the Free Software Foundation; either version 2 of the License, or\n"
3539 "(at your option) any later version.\n"
3541 "This program is distributed in the hope that it will be useful,\n"
3542 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
3543 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
3544 "GNU General Public License for more details.\n"
3546 "You should have received a copy of the GNU General Public License\n"
3547 "along with this program; if not, write to the Free Software\n"
3548 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
3552 "This library is free software; you can redistribute it and/or\n"
3553 "modify it under the terms of the GNU Lesser General Public\n"
3554 "License as published by the Free Software Foundation; either\n"
3555 "version 2 of the License, or (at your option) any later version.\n"
3557 "This library is distributed in the hope that it will be useful,\n"
3558 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
3559 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
3560 "Lesser General Public License for more details.\n"
3562 "You should have received a copy of the GNU Lesser General Public\n"
3563 "License along with this library; if not, write to the Free Software\n"
3564 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
3570 static void show_help(void)
3573 printf("usage: ffmpeg [[options] -i input_file]... {[options] outfile}...\n"
3574 "Hyper fast Audio and Video encoder\n");
3576 show_help_options(options, "Main options:\n",
3577 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO, 0);
3578 show_help_options(options, "\nVideo options:\n",
3579 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
3581 show_help_options(options, "\nAdvanced Video options:\n",
3582 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
3583 OPT_VIDEO | OPT_EXPERT);
3584 show_help_options(options, "\nAudio options:\n",
3585 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
3587 show_help_options(options, "\nAdvanced Audio options:\n",
3588 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
3589 OPT_AUDIO | OPT_EXPERT);
3590 show_help_options(options, "\nAudio/Video grab options:\n",
3593 show_help_options(options, "\nAdvanced options:\n",
3594 OPT_EXPERT | OPT_AUDIO | OPT_VIDEO | OPT_GRAB,
3599 void parse_arg_file(const char *filename)
3601 opt_output_file(filename);
3604 int main(int argc, char **argv)
3615 parse_options(argc, argv, options);
3617 /* file converter / grab */
3618 if (nb_output_files <= 0) {
3619 fprintf(stderr, "Must supply at least one output file\n");
3623 if (nb_input_files == 0) {
3628 av_encode(output_files, nb_output_files, input_files, nb_input_files,
3629 stream_maps, nb_stream_maps);
3630 ti = getutime() - ti;
3632 printf("bench: utime=%0.3fs\n", ti / 1000000.0);
3636 for(i=0;i<nb_output_files;i++) {
3637 /* maybe av_close_output_file ??? */
3638 AVFormatContext *s = output_files[i];
3640 if (!(s->oformat->flags & AVFMT_NOFILE))
3642 for(j=0;j<s->nb_streams;j++)
3643 av_free(s->streams[j]);
3646 for(i=0;i<nb_input_files;i++)
3647 av_close_input_file(input_files[i]);
3652 av_free(intra_matrix);
3654 av_free(inter_matrix);
3656 #ifdef POWERPC_PERFORMANCE_REPORT
3657 extern void powerpc_display_perf_report(void);
3658 powerpc_display_perf_report();
3659 #endif /* POWERPC_PERFORMANCE_REPORT */
3661 #ifndef CONFIG_WIN32
3662 if (received_sigterm) {
3664 "Received signal %d: terminating.\n",
3665 (int) received_sigterm);
3669 exit(0); /* not all OS-es handle main() return value */