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>
41 #if !defined(INFINITY) && defined(HUGE_VAL)
42 #define INFINITY HUGE_VAL
45 /* select an input stream for an output stream */
46 typedef struct AVStreamMap
{
51 extern const OptionDef options
[];
53 static void show_help(void);
54 static void show_license(void);
58 static AVFormatContext
*input_files
[MAX_FILES
];
59 static int nb_input_files
= 0;
61 static AVFormatContext
*output_files
[MAX_FILES
];
62 static int nb_output_files
= 0;
64 static AVStreamMap stream_maps
[MAX_FILES
];
65 static int nb_stream_maps
;
67 static AVInputFormat
*file_iformat
;
68 static AVOutputFormat
*file_oformat
;
69 static AVImageFormat
*image_format
;
70 static int frame_width
= 160;
71 static int frame_height
= 128;
72 static float frame_aspect_ratio
= 0;
73 static enum PixelFormat frame_pix_fmt
= PIX_FMT_YUV420P
;
74 static int frame_topBand
= 0;
75 static int frame_bottomBand
= 0;
76 static int frame_leftBand
= 0;
77 static int frame_rightBand
= 0;
78 static int frame_rate
= 25;
79 static int frame_rate_base
= 1;
80 static int video_bit_rate
= 200*1000;
81 static int video_bit_rate_tolerance
= 4000*1000;
82 static float video_qscale
= 0;
83 static int video_qmin
= 2;
84 static int video_qmax
= 31;
85 static int video_mb_qmin
= 2;
86 static int video_mb_qmax
= 31;
87 static int video_qdiff
= 3;
88 static float video_qblur
= 0.5;
89 static float video_qcomp
= 0.5;
90 static uint16_t *intra_matrix
= NULL
;
91 static uint16_t *inter_matrix
= NULL
;
92 #if 0 //experimental, (can be removed)
93 static float video_rc_qsquish
=1.0;
94 static float video_rc_qmod_amp
=0;
95 static int video_rc_qmod_freq
=0;
97 static char *video_rc_override_string
=NULL
;
98 static char *video_rc_eq
="tex^qComp";
99 static int video_rc_buffer_size
=0;
100 static float video_rc_buffer_aggressivity
=1.0;
101 static int video_rc_max_rate
=0;
102 static int video_rc_min_rate
=0;
103 static float video_rc_initial_cplx
=0;
104 static float video_b_qfactor
= 1.25;
105 static float video_b_qoffset
= 1.25;
106 static float video_i_qfactor
= -0.8;
107 static float video_i_qoffset
= 0.0;
108 static int video_intra_quant_bias
= FF_DEFAULT_QUANT_BIAS
;
109 static int video_inter_quant_bias
= FF_DEFAULT_QUANT_BIAS
;
110 static int me_method
= ME_EPZS
;
111 static int video_disable
= 0;
112 static int video_codec_id
= CODEC_ID_NONE
;
113 static int same_quality
= 0;
114 static int b_frames
= 0;
115 static int mb_decision
= FF_MB_DECISION_SIMPLE
;
116 static int ildct_cmp
= FF_CMP_VSAD
;
117 static int mb_cmp
= FF_CMP_SAD
;
118 static int sub_cmp
= FF_CMP_SAD
;
119 static int cmp
= FF_CMP_SAD
;
120 static int pre_cmp
= FF_CMP_SAD
;
121 static int pre_me
= 0;
122 static float lumi_mask
= 0;
123 static float dark_mask
= 0;
124 static float scplx_mask
= 0;
125 static float tcplx_mask
= 0;
126 static float p_mask
= 0;
127 static int use_4mv
= 0;
128 static int use_obmc
= 0;
129 static int use_aic
= 0;
130 static int use_aiv
= 0;
131 static int use_umv
= 0;
132 static int use_alt_scan
= 0;
133 static int use_trell
= 0;
134 static int use_scan_offset
= 0;
136 static int closed_gop
= 0;
137 static int do_deinterlace
= 0;
138 static int do_interlace_dct
= 0;
139 static int do_interlace_me
= 0;
140 static int workaround_bugs
= FF_BUG_AUTODETECT
;
141 static int error_resilience
= 2;
142 static int error_concealment
= 3;
143 static int dct_algo
= 0;
144 static int idct_algo
= 0;
145 static int use_part
= 0;
146 static int packet_size
= 0;
147 static int error_rate
= 0;
148 static int strict
= 0;
149 static int top_field_first
= -1;
150 static int noise_reduction
= 0;
151 static int sc_threshold
= 0;
152 static int debug
= 0;
153 static int debug_mv
= 0;
154 extern int loop_input
; /* currently a hack */
156 static int gop_size
= 12;
157 static int intra_only
= 0;
158 static int audio_sample_rate
= 44100;
159 static int audio_bit_rate
= 64000;
160 static int audio_disable
= 0;
161 static int audio_channels
= 1;
162 static int audio_codec_id
= CODEC_ID_NONE
;
164 static int64_t recording_time
= 0;
165 static int64_t start_time
= 0;
166 static int file_overwrite
= 0;
167 static char *str_title
= NULL
;
168 static char *str_author
= NULL
;
169 static char *str_copyright
= NULL
;
170 static char *str_comment
= NULL
;
171 static int do_benchmark
= 0;
172 static int do_hex_dump
= 0;
173 static int do_pkt_dump
= 0;
174 static int do_psnr
= 0;
175 static int do_vstats
= 0;
176 static int do_pass
= 0;
177 static int bitexact
= 0;
178 static char *pass_logfilename
= NULL
;
179 static int audio_stream_copy
= 0;
180 static int video_stream_copy
= 0;
182 static int rate_emu
= 0;
184 static char *video_grab_format
= "video4linux";
185 static char *video_device
= NULL
;
186 static int video_channel
= 0;
187 static char *video_standard
= "ntsc";
189 static char *audio_grab_format
= "audio_device";
190 static char *audio_device
= NULL
;
192 static int using_stdin
= 0;
193 static int using_vhook
= 0;
194 static int verbose
= 1;
195 static int thread_count
= 1;
197 #define DEFAULT_PASS_LOGFILENAME "ffmpeg2pass"
199 typedef struct AVOutputStream
{
200 int file_index
; /* file index */
201 int index
; /* stream index in the output file */
202 int source_index
; /* AVInputStream index */
203 AVStream
*st
; /* stream in the output file */
204 int encoding_needed
; /* true if encoding needed for this stream */
206 /* input pts and corresponding output pts
209 double sync_ipts_offset
;
212 int video_resample
; /* video_resample and video_crop are mutually exclusive */
213 AVPicture pict_tmp
; /* temporary image for resampling */
214 ImgReSampleContext
*img_resample_ctx
; /* for image resampling */
216 int video_crop
; /* video_resample and video_crop are mutually exclusive */
217 int topBand
; /* cropping area sizes */
222 ReSampleContext
*resample
; /* for audio resampling */
223 FifoBuffer fifo
; /* for compression: one audio fifo per codec */
227 typedef struct AVInputStream
{
231 int discard
; /* true if stream data should be discarded */
232 int decoding_needed
; /* true if the packets must be decoded in 'raw_fifo' */
233 int64_t sample_index
; /* current sample */
235 int64_t start
; /* time when read started */
236 unsigned long frame
; /* current frame */
237 int64_t next_pts
; /* synthetic pts for cases where pkt.pts
239 int64_t pts
; /* current pts */
242 typedef struct AVInputFile
{
243 int eof_reached
; /* true if eof reached */
244 int ist_index
; /* index of first stream in ist_table */
245 int buffer_size
; /* current total buffer size */
246 int buffer_size_max
; /* buffer size at which we consider we can stop
248 int nb_streams
; /* nb streams we are aware of */
253 /* init terminal so that we can grab keys */
254 static struct termios oldtty
;
256 static void term_exit(void)
258 tcsetattr (0, TCSANOW
, &oldtty
);
261 static volatile sig_atomic_t received_sigterm
= 0;
264 sigterm_handler(int sig
)
266 received_sigterm
= sig
;
270 static void term_init(void)
277 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
278 |INLCR
|IGNCR
|ICRNL
|IXON
);
279 tty
.c_oflag
|= OPOST
;
280 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
281 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
286 tcsetattr (0, TCSANOW
, &tty
);
288 signal(SIGINT
, sigterm_handler
); /* Interrupt (ANSI). */
289 signal(SIGQUIT
, sigterm_handler
); /* Quit (POSIX). */
290 signal(SIGTERM
, sigterm_handler
); /* Termination (ANSI). */
292 register a function to be called at normal program termination
295 #ifdef CONFIG_BEOS_NETSERVER
296 fcntl(0, F_SETFL
, fcntl(0, F_GETFL
) | O_NONBLOCK
);
300 /* read a key without blocking */
301 static int read_key(void)
305 #ifndef CONFIG_BEOS_NETSERVER
313 n
= select(1, &rfds
, NULL
, NULL
, &tv
);
327 static volatile int received_sigterm
= 0;
329 /* no interactive support */
330 static void term_exit(void)
334 static void term_init(void)
338 static int read_key(void)
345 static int read_ffserver_streams(AVFormatContext
*s
, const char *filename
)
350 err
= av_open_input_file(&ic
, filename
, NULL
, FFM_PACKET_SIZE
, NULL
);
353 /* copy stream format */
354 s
->nb_streams
= ic
->nb_streams
;
355 for(i
=0;i
<ic
->nb_streams
;i
++) {
358 st
= av_mallocz(sizeof(AVStream
));
359 memcpy(st
, ic
->streams
[i
], sizeof(AVStream
));
363 av_close_input_file(ic
);
367 #define MAX_AUDIO_PACKET_SIZE (128 * 1024)
369 static void do_audio_out(AVFormatContext
*s
,
372 unsigned char *buf
, int size
)
375 static uint8_t *audio_buf
= NULL
;
376 static uint8_t *audio_out
= NULL
;
377 const int audio_out_size
= 4*MAX_AUDIO_PACKET_SIZE
;
379 int size_out
, frame_bytes
, ret
;
382 /* SC: dynamic allocation of buffers */
384 audio_buf
= av_malloc(2*MAX_AUDIO_PACKET_SIZE
);
386 audio_out
= av_malloc(audio_out_size
);
387 if (!audio_buf
|| !audio_out
)
388 return; /* Should signal an error ! */
391 enc
= &ost
->st
->codec
;
393 if (ost
->audio_resample
) {
395 size_out
= audio_resample(ost
->resample
,
396 (short *)buftmp
, (short *)buf
,
397 size
/ (ist
->st
->codec
.channels
* 2));
398 size_out
= size_out
* enc
->channels
* 2;
404 /* now encode as many frames as possible */
405 if (enc
->frame_size
> 1) {
406 /* output resampled raw samples */
407 fifo_write(&ost
->fifo
, buftmp
, size_out
,
410 frame_bytes
= enc
->frame_size
* 2 * enc
->channels
;
412 while (fifo_read(&ost
->fifo
, audio_buf
, frame_bytes
,
413 &ost
->fifo
.rptr
) == 0) {
414 ret
= avcodec_encode_audio(enc
, audio_out
, audio_out_size
,
416 av_write_frame(s
, ost
->index
, audio_out
, ret
);
419 /* output a pcm frame */
420 /* XXX: change encoding codec API to avoid this ? */
421 switch(enc
->codec
->id
) {
422 case CODEC_ID_PCM_S16LE
:
423 case CODEC_ID_PCM_S16BE
:
424 case CODEC_ID_PCM_U16LE
:
425 case CODEC_ID_PCM_U16BE
:
428 size_out
= size_out
>> 1;
431 ret
= avcodec_encode_audio(enc
, audio_out
, size_out
,
433 av_write_frame(s
, ost
->index
, audio_out
, ret
);
437 static void pre_process_video_frame(AVInputStream
*ist
, AVPicture
*picture
, void **bufp
)
441 AVPicture picture_tmp
;
444 dec
= &ist
->st
->codec
;
446 /* deinterlace : must be done before any resize */
447 if (do_deinterlace
|| using_vhook
) {
450 /* create temporary picture */
451 size
= avpicture_get_size(dec
->pix_fmt
, dec
->width
, dec
->height
);
452 buf
= av_malloc(size
);
456 picture2
= &picture_tmp
;
457 avpicture_fill(picture2
, buf
, dec
->pix_fmt
, dec
->width
, dec
->height
);
460 if(avpicture_deinterlace(picture2
, picture
,
461 dec
->pix_fmt
, dec
->width
, dec
->height
) < 0) {
462 /* if error, do not deinterlace */
468 if (img_convert(picture2
, dec
->pix_fmt
, picture
,
469 dec
->pix_fmt
, dec
->width
, dec
->height
) < 0) {
470 /* if error, do not copy */
480 frame_hook_process(picture2
, dec
->pix_fmt
, dec
->width
, dec
->height
);
482 if (picture
!= picture2
)
483 *picture
= *picture2
;
487 /* we begin to correct av delay at this threshold */
488 #define AV_DELAY_MAX 0.100
490 static void do_video_out(AVFormatContext
*s
,
494 int *frame_size
, AVOutputStream
*audio_sync
)
496 int nb_frames
, i
, ret
;
497 AVPicture
*final_picture
, *formatted_picture
;
498 AVPicture picture_format_temp
, picture_crop_temp
;
499 static uint8_t *video_buffer
= NULL
;
500 uint8_t *buf
= NULL
, *buf1
= NULL
;
501 AVCodecContext
*enc
, *dec
;
502 enum PixelFormat target_pixfmt
;
504 #define VIDEO_BUFFER_SIZE (1024*1024)
506 enc
= &ost
->st
->codec
;
507 dec
= &ist
->st
->codec
;
509 /* by default, we output a single frame */
514 /* NOTE: the A/V sync is always done by considering the audio is
515 the master clock. It is suffisant for transcoding or playing,
516 but not for the general case */
518 /* compute the A-V delay and duplicate/remove frames if needed */
519 double adelta
, vdelta
, av_delay
;
521 adelta
= audio_sync
->sync_ipts
- ((double)audio_sync
->sync_opts
*
522 s
->pts_num
/ s
->pts_den
);
524 vdelta
= ost
->sync_ipts
- ((double)ost
->sync_opts
*
525 s
->pts_num
/ s
->pts_den
);
527 av_delay
= adelta
- vdelta
;
528 // printf("delay=%f\n", av_delay);
529 if (av_delay
< -AV_DELAY_MAX
)
531 else if (av_delay
> AV_DELAY_MAX
)
536 vdelta
= (double)(ost
->st
->pts
.val
) * s
->pts_num
/ s
->pts_den
- (ost
->sync_ipts
- ost
->sync_ipts_offset
);
537 if (vdelta
< 100 && vdelta
> -100 && ost
->sync_ipts_offset
) {
538 if (vdelta
< -AV_DELAY_MAX
)
540 else if (vdelta
> AV_DELAY_MAX
)
543 ost
->sync_ipts_offset
-= vdelta
;
544 if (!ost
->sync_ipts_offset
)
545 ost
->sync_ipts_offset
= 0.000001; /* one microsecond */
549 #if defined(AVSYNC_DEBUG)
551 static char *action
[] = { "drop frame", "copy frame", "dup frame" };
553 fprintf(stderr
, "Input APTS %12.6f, output APTS %12.6f, ",
554 (double) audio_sync
->sync_ipts
,
555 (double) audio_sync
->st
->pts
.val
* s
->pts_num
/ s
->pts_den
);
556 fprintf(stderr
, "Input VPTS %12.6f, output VPTS %12.6f: %s\n",
557 (double) ost
->sync_ipts
,
558 (double) ost
->st
->pts
.val
* s
->pts_num
/ s
->pts_den
,
567 video_buffer
= av_malloc(VIDEO_BUFFER_SIZE
);
571 /* convert pixel format if needed */
572 target_pixfmt
= ost
->video_resample ? PIX_FMT_YUV420P
: enc
->pix_fmt
;
573 if (dec
->pix_fmt
!= target_pixfmt
) {
576 /* create temporary picture */
577 size
= avpicture_get_size(target_pixfmt
, dec
->width
, dec
->height
);
578 buf
= av_malloc(size
);
581 formatted_picture
= &picture_format_temp
;
582 avpicture_fill(formatted_picture
, buf
, target_pixfmt
, dec
->width
, dec
->height
);
584 if (img_convert(formatted_picture
, target_pixfmt
,
585 (AVPicture
*)in_picture
, dec
->pix_fmt
,
586 dec
->width
, dec
->height
) < 0) {
587 fprintf(stderr
, "pixel format conversion not handled\n");
591 formatted_picture
= (AVPicture
*)in_picture
;
594 /* XXX: resampling could be done before raw format convertion in
595 some cases to go faster */
596 /* XXX: only works for YUV420P */
597 if (ost
->video_resample
) {
598 final_picture
= &ost
->pict_tmp
;
599 img_resample(ost
->img_resample_ctx
, final_picture
, formatted_picture
);
600 if (enc
->pix_fmt
!= PIX_FMT_YUV420P
) {
604 /* create temporary picture */
605 size
= avpicture_get_size(enc
->pix_fmt
, enc
->width
, enc
->height
);
606 buf
= av_malloc(size
);
609 final_picture
= &picture_format_temp
;
610 avpicture_fill(final_picture
, buf
, enc
->pix_fmt
, enc
->width
, enc
->height
);
612 if (img_convert(final_picture
, enc
->pix_fmt
,
613 &ost
->pict_tmp
, PIX_FMT_YUV420P
,
614 enc
->width
, enc
->height
) < 0) {
615 fprintf(stderr
, "pixel format conversion not handled\n");
619 } else if (ost
->video_crop
) {
620 picture_crop_temp
.data
[0] = formatted_picture
->data
[0] +
621 (ost
->topBand
* formatted_picture
->linesize
[0]) + ost
->leftBand
;
623 picture_crop_temp
.data
[1] = formatted_picture
->data
[1] +
624 ((ost
->topBand
>> 1) * formatted_picture
->linesize
[1]) +
625 (ost
->leftBand
>> 1);
627 picture_crop_temp
.data
[2] = formatted_picture
->data
[2] +
628 ((ost
->topBand
>> 1) * formatted_picture
->linesize
[2]) +
629 (ost
->leftBand
>> 1);
631 picture_crop_temp
.linesize
[0] = formatted_picture
->linesize
[0];
632 picture_crop_temp
.linesize
[1] = formatted_picture
->linesize
[1];
633 picture_crop_temp
.linesize
[2] = formatted_picture
->linesize
[2];
634 final_picture
= &picture_crop_temp
;
636 final_picture
= formatted_picture
;
638 /* duplicates frame if needed */
639 /* XXX: pb because no interleaving */
640 for(i
=0;i
<nb_frames
;i
++) {
641 if (s
->oformat
->flags
& AVFMT_RAWPICTURE
) {
642 /* raw pictures are written as AVPicture structure to
643 avoid any copies. We support temorarily the older
645 AVFrame
* old_frame
= enc
->coded_frame
;
646 enc
->coded_frame
= dec
->coded_frame
;
647 av_write_frame(s
, ost
->index
,
648 (uint8_t *)final_picture
, sizeof(AVPicture
));
649 enc
->coded_frame
= old_frame
;
653 avcodec_get_frame_defaults(&big_picture
);
654 *(AVPicture
*)&big_picture
= *final_picture
;
655 /* better than nothing: use input picture interlaced
657 big_picture
.interlaced_frame
= in_picture
->interlaced_frame
;
658 if(do_interlace_me
|| do_interlace_dct
){
659 if(top_field_first
== -1)
660 big_picture
.top_field_first
= in_picture
->top_field_first
;
662 big_picture
.top_field_first
= 1;
665 /* handles sameq here. This is not correct because it may
666 not be a global option */
668 big_picture
.quality
= ist
->st
->quality
;
670 big_picture
.quality
= ost
->st
->quality
;
671 ret
= avcodec_encode_video(enc
,
672 video_buffer
, VIDEO_BUFFER_SIZE
,
674 //enc->frame_number = enc->real_pict_num;
675 av_write_frame(s
, ost
->index
, video_buffer
, ret
);
677 //fprintf(stderr,"\nFrame: %3d %3d size: %5d type: %d",
678 // enc->frame_number-1, enc->real_pict_num, ret,
680 /* if two pass, output log */
681 if (ost
->logfile
&& enc
->stats_out
) {
682 fprintf(ost
->logfile
, "%s", enc
->stats_out
);
692 static double psnr(double d
){
693 if(d
==0) return INFINITY
;
694 return -10.0*log(d
)/log(10.0);
697 static void do_video_stats(AVFormatContext
*os
, AVOutputStream
*ost
,
700 static FILE *fvstats
=NULL
;
701 static int64_t total_size
= 0;
708 double ti1
, bitrate
, avg_bitrate
;
712 today
= localtime(&today2
);
713 sprintf(filename
, "vstats_%02d%02d%02d.log", today
->tm_hour
,
716 fvstats
= fopen(filename
,"w");
724 enc
= &ost
->st
->codec
;
725 total_size
+= frame_size
;
726 if (enc
->codec_type
== CODEC_TYPE_VIDEO
) {
727 frame_number
= ost
->frame_number
;
728 fprintf(fvstats
, "frame= %5d q= %2.1f ", frame_number
, enc
->coded_frame
->quality
/(float)FF_QP2LAMBDA
);
729 if (enc
->flags
&CODEC_FLAG_PSNR
)
730 fprintf(fvstats
, "PSNR= %6.2f ", psnr(enc
->coded_frame
->error
[0]/(enc
->width
*enc
->height
*255.0*255.0)));
732 fprintf(fvstats
,"f_size= %6d ", frame_size
);
733 /* compute pts value */
734 ti1
= (double)ost
->st
->pts
.val
* os
->pts_num
/ os
->pts_den
;
738 bitrate
= (double)(frame_size
* 8) * enc
->frame_rate
/ enc
->frame_rate_base
/ 1000.0;
739 avg_bitrate
= (double)(total_size
* 8) / ti1
/ 1000.0;
740 fprintf(fvstats
, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
741 (double)total_size
/ 1024, ti1
, bitrate
, avg_bitrate
);
742 fprintf(fvstats
,"type= %c\n", av_get_pict_type_char(enc
->coded_frame
->pict_type
));
746 static void print_report(AVFormatContext
**output_files
,
747 AVOutputStream
**ost_table
, int nb_ostreams
,
752 AVFormatContext
*oc
, *os
;
755 int frame_number
, vid
, i
;
756 double bitrate
, ti1
, pts
;
757 static int64_t last_time
= -1;
759 if (!is_last_report
) {
761 /* display the report every 0.5 seconds */
762 cur_time
= av_gettime();
763 if (last_time
== -1) {
764 last_time
= cur_time
;
767 if ((cur_time
- last_time
) < 500000)
769 last_time
= cur_time
;
773 oc
= output_files
[0];
775 total_size
= url_ftell(&oc
->pb
);
780 for(i
=0;i
<nb_ostreams
;i
++) {
782 os
= output_files
[ost
->file_index
];
783 enc
= &ost
->st
->codec
;
784 if (vid
&& enc
->codec_type
== CODEC_TYPE_VIDEO
) {
785 sprintf(buf
+ strlen(buf
), "q=%2.1f ",
786 enc
->coded_frame
->quality
/(float)FF_QP2LAMBDA
);
788 if (!vid
&& enc
->codec_type
== CODEC_TYPE_VIDEO
) {
789 frame_number
= ost
->frame_number
;
790 sprintf(buf
+ strlen(buf
), "frame=%5d q=%2.1f ",
791 frame_number
, enc
->coded_frame ? enc
->coded_frame
->quality
/(float)FF_QP2LAMBDA
: 0);
793 sprintf(buf
+ strlen(buf
), "L");
794 if (enc
->flags
&CODEC_FLAG_PSNR
){
796 double error
, error_sum
=0;
797 double scale
, scale_sum
=0;
798 char type
[3]= {'Y','U','V'};
799 sprintf(buf
+ strlen(buf
), "PSNR=");
802 error
= enc
->error
[j
];
803 scale
= enc
->width
*enc
->height
*255.0*255.0*frame_number
;
805 error
= enc
->coded_frame
->error
[j
];
806 scale
= enc
->width
*enc
->height
*255.0*255.0;
811 sprintf(buf
+ strlen(buf
), "%c:%2.2f ", type
[j
], psnr(error
/scale
));
813 sprintf(buf
+ strlen(buf
), "*:%2.2f ", psnr(error_sum
/scale_sum
));
817 /* compute min output value */
818 pts
= (double)ost
->st
->pts
.val
* os
->pts_num
/ os
->pts_den
;
819 if ((pts
< ti1
) && (pts
> 0))
825 if (verbose
|| is_last_report
) {
826 bitrate
= (double)(total_size
* 8) / ti1
/ 1000.0;
828 sprintf(buf
+ strlen(buf
),
829 "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
830 (double)total_size
/ 1024, ti1
, bitrate
);
832 fprintf(stderr
, "%s \r", buf
);
837 fprintf(stderr
, "\n");
840 /* pkt = NULL means EOF (needed to flush decoder buffers) */
841 static int output_packet(AVInputStream
*ist
, int ist_index
,
842 AVOutputStream
**ost_table
, int nb_ostreams
,
850 int data_size
, got_picture
;
852 short samples
[AVCODEC_MAX_AUDIO_FRAME_SIZE
/ 2];
853 void *buffer_to_free
;
855 if (pkt
&& pkt
->pts
!= AV_NOPTS_VALUE
) {
858 ist
->pts
= ist
->next_pts
;
872 /* decode the packet if needed */
873 data_buf
= NULL
; /* fail safe */
875 if (ist
->decoding_needed
) {
876 switch(ist
->st
->codec
.codec_type
) {
877 case CODEC_TYPE_AUDIO
:
878 /* XXX: could avoid copy if PCM 16 bits with same
880 ret
= avcodec_decode_audio(&ist
->st
->codec
, samples
, &data_size
,
886 /* Some bug in mpeg audio decoder gives */
887 /* data_size < 0, it seems they are overflows */
888 if (data_size
<= 0) {
892 data_buf
= (uint8_t *)samples
;
893 ist
->next_pts
+= ((int64_t)AV_TIME_BASE
* data_size
) /
894 (2 * ist
->st
->codec
.channels
);
896 case CODEC_TYPE_VIDEO
:
897 data_size
= (ist
->st
->codec
.width
* ist
->st
->codec
.height
* 3) / 2;
898 /* XXX: allocate picture correctly */
899 avcodec_get_frame_defaults(&picture
);
901 ret
= avcodec_decode_video(&ist
->st
->codec
,
902 &picture
, &got_picture
, ptr
, len
);
903 ist
->st
->quality
= picture
.quality
;
910 if (ist
->st
->codec
.frame_rate_base
!= 0) {
911 ist
->next_pts
+= ((int64_t)AV_TIME_BASE
*
912 ist
->st
->codec
.frame_rate_base
) /
913 ist
->st
->codec
.frame_rate
;
927 buffer_to_free
= NULL
;
928 if (ist
->st
->codec
.codec_type
== CODEC_TYPE_VIDEO
) {
929 pre_process_video_frame(ist
, (AVPicture
*)&picture
,
933 /* frame rate emulation */
934 if (ist
->st
->codec
.rate_emu
) {
935 int64_t pts
= av_rescale((int64_t) ist
->frame
* ist
->st
->codec
.frame_rate_base
, 1000000, ist
->st
->codec
.frame_rate
);
936 int64_t now
= av_gettime() - ist
->start
;
944 /* mpeg PTS deordering : if it is a P or I frame, the PTS
945 is the one of the next displayed one */
946 /* XXX: add mpeg4 too ? */
947 if (ist
->st
->codec
.codec_id
== CODEC_ID_MPEG1VIDEO
) {
948 if (ist
->st
->codec
.pict_type
!= B_TYPE
) {
950 tmp
= ist
->last_ip_pts
;
951 ist
->last_ip_pts
= ist
->frac_pts
.val
;
952 ist
->frac_pts
.val
= tmp
;
956 /* if output time reached then transcode raw format,
957 encode packets and output them */
958 if (start_time
== 0 || ist
->pts
>= start_time
)
959 for(i
=0;i
<nb_ostreams
;i
++) {
963 if (ost
->source_index
== ist_index
) {
964 os
= output_files
[ost
->file_index
];
967 printf("%d: got pts=%0.3f %0.3f\n", i
,
968 (double)pkt
->pts
/ AV_TIME_BASE
,
969 ((double)ist
->pts
/ AV_TIME_BASE
) -
970 ((double)ost
->st
->pts
.val
* os
->pts_num
/ os
->pts_den
));
972 /* set the input output pts pairs */
973 ost
->sync_ipts
= (double)ist
->pts
/ AV_TIME_BASE
;
974 /* XXX: take into account the various fifos,
975 in particular for audio */
976 ost
->sync_opts
= ost
->st
->pts
.val
;
977 //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);
979 if (ost
->encoding_needed
) {
980 switch(ost
->st
->codec
.codec_type
) {
981 case CODEC_TYPE_AUDIO
:
982 do_audio_out(os
, ost
, ist
, data_buf
, data_size
);
984 case CODEC_TYPE_VIDEO
:
985 /* find an audio stream for synchro */
988 AVOutputStream
*audio_sync
, *ost1
;
990 for(i
=0;i
<nb_ostreams
;i
++) {
992 if (ost1
->file_index
== ost
->file_index
&&
993 ost1
->st
->codec
.codec_type
== CODEC_TYPE_AUDIO
) {
999 do_video_out(os
, ost
, ist
, &picture
, &frame_size
, audio_sync
);
1000 if (do_vstats
&& frame_size
)
1001 do_video_stats(os
, ost
, frame_size
);
1010 /* no reencoding needed : output the packet directly */
1011 /* force the input stream PTS */
1013 avcodec_get_frame_defaults(&avframe
);
1014 ost
->st
->codec
.coded_frame
= &avframe
;
1015 avframe
.key_frame
= pkt
->flags
& PKT_FLAG_KEY
;
1017 av_write_frame(os
, ost
->index
, data_buf
, data_size
);
1018 ost
->st
->codec
.frame_number
++;
1019 ost
->frame_number
++;
1023 av_free(buffer_to_free
);
1033 * The following code is the main loop of the file converter
1035 static int av_encode(AVFormatContext
**output_files
,
1036 int nb_output_files
,
1037 AVFormatContext
**input_files
,
1039 AVStreamMap
*stream_maps
, int nb_stream_maps
)
1041 int ret
, i
, j
, k
, n
, nb_istreams
= 0, nb_ostreams
= 0;
1042 AVFormatContext
*is
, *os
;
1043 AVCodecContext
*codec
, *icodec
;
1044 AVOutputStream
*ost
, **ost_table
= NULL
;
1045 AVInputStream
*ist
, **ist_table
= NULL
;
1046 AVInputFile
*file_table
;
1047 AVFormatContext
*stream_no_data
;
1050 file_table
= (AVInputFile
*) av_mallocz(nb_input_files
* sizeof(AVInputFile
));
1054 /* input stream init */
1056 for(i
=0;i
<nb_input_files
;i
++) {
1057 is
= input_files
[i
];
1058 file_table
[i
].ist_index
= j
;
1059 file_table
[i
].nb_streams
= is
->nb_streams
;
1060 j
+= is
->nb_streams
;
1064 ist_table
= av_mallocz(nb_istreams
* sizeof(AVInputStream
*));
1068 for(i
=0;i
<nb_istreams
;i
++) {
1069 ist
= av_mallocz(sizeof(AVInputStream
));
1075 for(i
=0;i
<nb_input_files
;i
++) {
1076 is
= input_files
[i
];
1077 for(k
=0;k
<is
->nb_streams
;k
++) {
1078 ist
= ist_table
[j
++];
1079 ist
->st
= is
->streams
[k
];
1080 ist
->file_index
= i
;
1082 ist
->discard
= 1; /* the stream is discarded by default
1085 if (ist
->st
->codec
.rate_emu
) {
1086 ist
->start
= av_gettime();
1092 /* output stream init */
1094 for(i
=0;i
<nb_output_files
;i
++) {
1095 os
= output_files
[i
];
1096 nb_ostreams
+= os
->nb_streams
;
1098 if (nb_stream_maps
> 0 && nb_stream_maps
!= nb_ostreams
) {
1099 fprintf(stderr
, "Number of stream maps must match number of output streams\n");
1103 /* Sanity check the mapping args -- do the input files & streams exist? */
1104 for(i
=0;i
<nb_stream_maps
;i
++) {
1105 int fi
= stream_maps
[i
].file_index
;
1106 int si
= stream_maps
[i
].stream_index
;
1108 if (fi
< 0 || fi
> nb_input_files
- 1 ||
1109 si
< 0 || si
> file_table
[fi
].nb_streams
- 1) {
1110 fprintf(stderr
,"Could not find input stream #%d.%d\n", fi
, si
);
1115 ost_table
= av_mallocz(sizeof(AVOutputStream
*) * nb_ostreams
);
1118 for(i
=0;i
<nb_ostreams
;i
++) {
1119 ost
= av_mallocz(sizeof(AVOutputStream
));
1126 for(k
=0;k
<nb_output_files
;k
++) {
1127 os
= output_files
[k
];
1128 for(i
=0;i
<os
->nb_streams
;i
++) {
1130 ost
= ost_table
[n
++];
1131 ost
->file_index
= k
;
1133 ost
->st
= os
->streams
[i
];
1134 if (nb_stream_maps
> 0) {
1135 ost
->source_index
= file_table
[stream_maps
[n
-1].file_index
].ist_index
+
1136 stream_maps
[n
-1].stream_index
;
1138 /* Sanity check that the stream types match */
1139 if (ist_table
[ost
->source_index
]->st
->codec
.codec_type
!= ost
->st
->codec
.codec_type
) {
1140 fprintf(stderr
, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
1141 stream_maps
[n
-1].file_index
, stream_maps
[n
-1].stream_index
,
1142 ost
->file_index
, ost
->index
);
1147 /* get corresponding input stream index : we select the first one with the right type */
1149 for(j
=0;j
<nb_istreams
;j
++) {
1152 ist
->st
->codec
.codec_type
== ost
->st
->codec
.codec_type
) {
1153 ost
->source_index
= j
;
1159 /* try again and reuse existing stream */
1160 for(j
=0;j
<nb_istreams
;j
++) {
1162 if (ist
->st
->codec
.codec_type
== ost
->st
->codec
.codec_type
) {
1163 ost
->source_index
= j
;
1168 fprintf(stderr
, "Could not find input stream matching output stream #%d.%d\n",
1169 ost
->file_index
, ost
->index
);
1174 ist
= ist_table
[ost
->source_index
];
1179 /* for each output stream, we compute the right encoding parameters */
1180 for(i
=0;i
<nb_ostreams
;i
++) {
1182 ist
= ist_table
[ost
->source_index
];
1184 codec
= &ost
->st
->codec
;
1185 icodec
= &ist
->st
->codec
;
1187 if (ost
->st
->stream_copy
) {
1188 /* if stream_copy is selected, no need to decode or encode */
1189 codec
->codec_id
= icodec
->codec_id
;
1190 codec
->codec_type
= icodec
->codec_type
;
1191 codec
->codec_tag
= icodec
->codec_tag
;
1192 codec
->bit_rate
= icodec
->bit_rate
;
1193 switch(codec
->codec_type
) {
1194 case CODEC_TYPE_AUDIO
:
1195 codec
->sample_rate
= icodec
->sample_rate
;
1196 codec
->channels
= icodec
->channels
;
1198 case CODEC_TYPE_VIDEO
:
1199 codec
->frame_rate
= icodec
->frame_rate
;
1200 codec
->frame_rate_base
= icodec
->frame_rate_base
;
1201 codec
->width
= icodec
->width
;
1202 codec
->height
= icodec
->height
;
1208 switch(codec
->codec_type
) {
1209 case CODEC_TYPE_AUDIO
:
1210 if (fifo_init(&ost
->fifo
, 2 * MAX_AUDIO_PACKET_SIZE
))
1213 if (codec
->channels
== icodec
->channels
&&
1214 codec
->sample_rate
== icodec
->sample_rate
) {
1215 ost
->audio_resample
= 0;
1217 if (codec
->channels
!= icodec
->channels
&&
1218 icodec
->codec_id
== CODEC_ID_AC3
) {
1219 /* Special case for 5:1 AC3 input */
1220 /* and mono or stereo output */
1221 /* Request specific number of channels */
1222 icodec
->channels
= codec
->channels
;
1223 if (codec
->sample_rate
== icodec
->sample_rate
)
1224 ost
->audio_resample
= 0;
1226 ost
->audio_resample
= 1;
1227 ost
->resample
= audio_resample_init(codec
->channels
, icodec
->channels
,
1229 icodec
->sample_rate
);
1232 printf("Can't resample. Aborting.\n");
1236 /* Request specific number of channels */
1237 icodec
->channels
= codec
->channels
;
1239 ost
->audio_resample
= 1;
1240 ost
->resample
= audio_resample_init(codec
->channels
, icodec
->channels
,
1242 icodec
->sample_rate
);
1245 printf("Can't resample. Aborting.\n");
1250 ist
->decoding_needed
= 1;
1251 ost
->encoding_needed
= 1;
1253 case CODEC_TYPE_VIDEO
:
1254 if (codec
->width
== icodec
->width
&&
1255 codec
->height
== icodec
->height
&&
1256 frame_topBand
== 0 &&
1257 frame_bottomBand
== 0 &&
1258 frame_leftBand
== 0 &&
1259 frame_rightBand
== 0)
1261 ost
->video_resample
= 0;
1262 ost
->video_crop
= 0;
1263 } else if ((codec
->width
== icodec
->width
-
1264 (frame_leftBand
+ frame_rightBand
)) &&
1265 (codec
->height
== icodec
->height
-
1266 (frame_topBand
+ frame_bottomBand
)))
1268 ost
->video_resample
= 0;
1269 ost
->video_crop
= 1;
1270 ost
->topBand
= frame_topBand
;
1271 ost
->leftBand
= frame_leftBand
;
1273 ost
->video_resample
= 1;
1274 ost
->video_crop
= 0; // cropping is handled as part of resample
1275 if( avpicture_alloc( &ost
->pict_tmp
, PIX_FMT_YUV420P
,
1276 codec
->width
, codec
->height
) )
1279 ost
->img_resample_ctx
= img_resample_full_init(
1280 ost
->st
->codec
.width
, ost
->st
->codec
.height
,
1281 ist
->st
->codec
.width
, ist
->st
->codec
.height
,
1282 frame_topBand
, frame_bottomBand
,
1283 frame_leftBand
, frame_rightBand
);
1285 ost
->encoding_needed
= 1;
1286 ist
->decoding_needed
= 1;
1292 if (ost
->encoding_needed
&&
1293 (codec
->flags
& (CODEC_FLAG_PASS1
| CODEC_FLAG_PASS2
))) {
1294 char logfilename
[1024];
1299 snprintf(logfilename
, sizeof(logfilename
), "%s-%d.log",
1301 pass_logfilename
: DEFAULT_PASS_LOGFILENAME
, i
);
1302 if (codec
->flags
& CODEC_FLAG_PASS1
) {
1303 f
= fopen(logfilename
, "w");
1305 perror(logfilename
);
1310 /* read the log file */
1311 f
= fopen(logfilename
, "r");
1313 perror(logfilename
);
1316 fseek(f
, 0, SEEK_END
);
1318 fseek(f
, 0, SEEK_SET
);
1319 logbuffer
= av_malloc(size
+ 1);
1321 fprintf(stderr
, "Could not allocate log buffer\n");
1324 size
= fread(logbuffer
, 1, size
, f
);
1326 logbuffer
[size
] = '\0';
1327 codec
->stats_in
= logbuffer
;
1333 /* dump the file output parameters - cannot be done before in case
1335 for(i
=0;i
<nb_output_files
;i
++) {
1336 dump_format(output_files
[i
], i
, output_files
[i
]->filename
, 1);
1339 /* dump the stream mapping */
1340 fprintf(stderr
, "Stream mapping:\n");
1341 for(i
=0;i
<nb_ostreams
;i
++) {
1343 fprintf(stderr
, " Stream #%d.%d -> #%d.%d\n",
1344 ist_table
[ost
->source_index
]->file_index
,
1345 ist_table
[ost
->source_index
]->index
,
1350 /* open each encoder */
1351 for(i
=0;i
<nb_ostreams
;i
++) {
1353 if (ost
->encoding_needed
) {
1355 codec
= avcodec_find_encoder(ost
->st
->codec
.codec_id
);
1357 fprintf(stderr
, "Unsupported codec for output stream #%d.%d\n",
1358 ost
->file_index
, ost
->index
);
1361 if (avcodec_open(&ost
->st
->codec
, codec
) < 0) {
1362 fprintf(stderr
, "Error while opening codec for stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
1363 ost
->file_index
, ost
->index
);
1369 /* open each decoder */
1370 for(i
=0;i
<nb_istreams
;i
++) {
1372 if (ist
->decoding_needed
) {
1374 codec
= avcodec_find_decoder(ist
->st
->codec
.codec_id
);
1376 fprintf(stderr
, "Unsupported codec (id=%d) for input stream #%d.%d\n",
1377 ist
->st
->codec
.codec_id
, ist
->file_index
, ist
->index
);
1380 if (avcodec_open(&ist
->st
->codec
, codec
) < 0) {
1381 fprintf(stderr
, "Error while opening codec for input stream #%d.%d\n",
1382 ist
->file_index
, ist
->index
);
1385 //if (ist->st->codec.codec_type == CODEC_TYPE_VIDEO)
1386 // ist->st->codec.flags |= CODEC_FLAG_REPEAT_FIELD;
1391 for(i
=0;i
<nb_istreams
;i
++) {
1393 is
= input_files
[ist
->file_index
];
1398 /* compute buffer size max (should use a complete heuristic) */
1399 for(i
=0;i
<nb_input_files
;i
++) {
1400 file_table
[i
].buffer_size_max
= 2048;
1403 /* open files and write file headers */
1404 for(i
=0;i
<nb_output_files
;i
++) {
1405 os
= output_files
[i
];
1406 if (av_write_header(os
) < 0) {
1407 fprintf(stderr
, "Could not write header for output file #%d (incorrect codec paramters ?)\n", i
);
1413 #ifndef CONFIG_WIN32
1415 fprintf(stderr
, "Press [q] to stop encoding\n");
1422 for(; received_sigterm
== 0;) {
1423 int file_index
, ist_index
;
1428 /* if 'q' pressed, exits */
1430 /* read_key() returns 0 on EOF */
1436 /* select the stream that we must read now by looking at the
1437 smallest output pts */
1440 for(i
=0;i
<nb_ostreams
;i
++) {
1443 os
= output_files
[ost
->file_index
];
1444 ist
= ist_table
[ost
->source_index
];
1445 pts
= (double)ost
->st
->pts
.val
* os
->pts_num
/ os
->pts_den
;
1446 if (!file_table
[ist
->file_index
].eof_reached
&&
1449 file_index
= ist
->file_index
;
1452 /* if none, if is finished */
1453 if (file_index
< 0) {
1457 /* finish if recording time exhausted */
1458 if (recording_time
> 0 && pts_min
>= (recording_time
/ 1000000.0))
1461 /* read a frame from it and output it in the fifo */
1462 is
= input_files
[file_index
];
1463 if (av_read_frame(is
, &pkt
) < 0) {
1464 file_table
[file_index
].eof_reached
= 1;
1469 stream_no_data
= is
;
1474 av_pkt_dump(stdout
, &pkt
, do_hex_dump
);
1476 /* the following test is needed in case new streams appear
1477 dynamically in stream : we ignore them */
1478 if (pkt
.stream_index
>= file_table
[file_index
].nb_streams
)
1479 goto discard_packet
;
1480 ist_index
= file_table
[file_index
].ist_index
+ pkt
.stream_index
;
1481 ist
= ist_table
[ist_index
];
1483 goto discard_packet
;
1485 //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
1486 if (output_packet(ist
, ist_index
, ost_table
, nb_ostreams
, &pkt
) < 0) {
1487 fprintf(stderr
, "Error while decoding stream #%d.%d\n",
1488 ist
->file_index
, ist
->index
);
1489 av_free_packet(&pkt
);
1494 av_free_packet(&pkt
);
1496 /* dump report by using the output first video and audio streams */
1497 print_report(output_files
, ost_table
, nb_ostreams
, 0);
1500 /* at the end of stream, we must flush the decoder buffers */
1501 for(i
=0;i
<nb_istreams
;i
++) {
1503 if (ist
->decoding_needed
) {
1504 output_packet(ist
, i
, ost_table
, nb_ostreams
, NULL
);
1510 /* dump report by using the first video and audio streams */
1511 print_report(output_files
, ost_table
, nb_ostreams
, 1);
1513 /* write the trailer if needed and close file */
1514 for(i
=0;i
<nb_output_files
;i
++) {
1515 os
= output_files
[i
];
1516 av_write_trailer(os
);
1519 /* close each encoder */
1520 for(i
=0;i
<nb_ostreams
;i
++) {
1522 if (ost
->encoding_needed
) {
1523 av_freep(&ost
->st
->codec
.stats_in
);
1524 avcodec_close(&ost
->st
->codec
);
1528 /* close each decoder */
1529 for(i
=0;i
<nb_istreams
;i
++) {
1531 if (ist
->decoding_needed
) {
1532 avcodec_close(&ist
->st
->codec
);
1540 av_free(file_table
);
1543 for(i
=0;i
<nb_istreams
;i
++) {
1550 for(i
=0;i
<nb_ostreams
;i
++) {
1554 fclose(ost
->logfile
);
1555 ost
->logfile
= NULL
;
1557 fifo_free(&ost
->fifo
); /* works even if fifo is not
1558 initialized but set to zero */
1559 av_free(ost
->pict_tmp
.data
[0]);
1560 if (ost
->video_resample
)
1561 img_resample_close(ost
->img_resample_ctx
);
1562 if (ost
->audio_resample
)
1563 audio_resample_close(ost
->resample
);
1576 int file_read(const char *filename
)
1579 unsigned char buffer
[1024];
1582 if (url_open(&h
, filename
, O_RDONLY
) < 0) {
1583 printf("could not open '%s'\n", filename
);
1587 len
= url_read(h
, buffer
, sizeof(buffer
));
1590 for(i
=0;i
<len
;i
++) putchar(buffer
[i
]);
1597 static void opt_image_format(const char *arg
)
1601 for(f
= first_image_format
; f
!= NULL
; f
= f
->next
) {
1602 if (!strcmp(arg
, f
->name
))
1606 fprintf(stderr
, "Unknown image format: '%s'\n", arg
);
1612 static void opt_format(const char *arg
)
1614 /* compatibility stuff for pgmyuv */
1615 if (!strcmp(arg
, "pgmyuv")) {
1616 opt_image_format(arg
);
1620 file_iformat
= av_find_input_format(arg
);
1621 file_oformat
= guess_format(arg
, NULL
, NULL
);
1622 if (!file_iformat
&& !file_oformat
) {
1623 fprintf(stderr
, "Unknown input or output format: %s\n", arg
);
1628 static void opt_video_bitrate(const char *arg
)
1630 video_bit_rate
= atoi(arg
) * 1000;
1633 static void opt_video_bitrate_tolerance(const char *arg
)
1635 video_bit_rate_tolerance
= atoi(arg
) * 1000;
1638 static void opt_video_bitrate_max(const char *arg
)
1640 video_rc_max_rate
= atoi(arg
) * 1000;
1643 static void opt_video_bitrate_min(const char *arg
)
1645 video_rc_min_rate
= atoi(arg
) * 1000;
1648 static void opt_video_buffer_size(const char *arg
)
1650 video_rc_buffer_size
= atoi(arg
) * 8*1024;
1653 static void opt_video_rc_eq(char *arg
)
1658 static void opt_video_rc_override_string(char *arg
)
1660 video_rc_override_string
= arg
;
1664 static void opt_workaround_bugs(const char *arg
)
1666 workaround_bugs
= atoi(arg
);
1669 static void opt_dct_algo(const char *arg
)
1671 dct_algo
= atoi(arg
);
1674 static void opt_idct_algo(const char *arg
)
1676 idct_algo
= atoi(arg
);
1680 static void opt_error_resilience(const char *arg
)
1682 error_resilience
= atoi(arg
);
1685 static void opt_error_concealment(const char *arg
)
1687 error_concealment
= atoi(arg
);
1690 static void opt_debug(const char *arg
)
1695 static void opt_vismv(const char *arg
)
1697 debug_mv
= atoi(arg
);
1700 static void opt_verbose(const char *arg
)
1702 verbose
= atoi(arg
);
1705 static void opt_frame_rate(const char *arg
)
1707 if (parse_frame_rate(&frame_rate
, &frame_rate_base
, arg
) < 0) {
1708 fprintf(stderr
, "Incorrect frame rate\n");
1713 static void opt_frame_crop_top(const char *arg
)
1715 frame_topBand
= atoi(arg
);
1716 if (frame_topBand
< 0) {
1717 fprintf(stderr
, "Incorrect top crop size\n");
1720 if ((frame_topBand
% 2) != 0) {
1721 fprintf(stderr
, "Top crop size must be a multiple of 2\n");
1724 if ((frame_topBand
) >= frame_height
){
1725 fprintf(stderr
, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1728 frame_height
-= frame_topBand
;
1731 static void opt_frame_crop_bottom(const char *arg
)
1733 frame_bottomBand
= atoi(arg
);
1734 if (frame_bottomBand
< 0) {
1735 fprintf(stderr
, "Incorrect bottom crop size\n");
1738 if ((frame_bottomBand
% 2) != 0) {
1739 fprintf(stderr
, "Bottom crop size must be a multiple of 2\n");
1742 if ((frame_bottomBand
) >= frame_height
){
1743 fprintf(stderr
, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1746 frame_height
-= frame_bottomBand
;
1749 static void opt_frame_crop_left(const char *arg
)
1751 frame_leftBand
= atoi(arg
);
1752 if (frame_leftBand
< 0) {
1753 fprintf(stderr
, "Incorrect left crop size\n");
1756 if ((frame_leftBand
% 2) != 0) {
1757 fprintf(stderr
, "Left crop size must be a multiple of 2\n");
1760 if ((frame_leftBand
) >= frame_width
){
1761 fprintf(stderr
, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1764 frame_width
-= frame_leftBand
;
1767 static void opt_frame_crop_right(const char *arg
)
1769 frame_rightBand
= atoi(arg
);
1770 if (frame_rightBand
< 0) {
1771 fprintf(stderr
, "Incorrect right crop size\n");
1774 if ((frame_rightBand
% 2) != 0) {
1775 fprintf(stderr
, "Right crop size must be a multiple of 2\n");
1778 if ((frame_rightBand
) >= frame_width
){
1779 fprintf(stderr
, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
1782 frame_width
-= frame_rightBand
;
1785 static void opt_frame_size(const char *arg
)
1787 if (parse_image_size(&frame_width
, &frame_height
, arg
) < 0) {
1788 fprintf(stderr
, "Incorrect frame size\n");
1791 if ((frame_width
% 2) != 0 || (frame_height
% 2) != 0) {
1792 fprintf(stderr
, "Frame size must be a multiple of 2\n");
1797 static void opt_frame_pix_fmt(const char *arg
)
1799 frame_pix_fmt
= avcodec_get_pix_fmt(arg
);
1802 static void opt_frame_aspect_ratio(const char *arg
)
1808 p
= strchr(arg
, ':');
1810 x
= strtol(arg
, (char **)&arg
, 10);
1812 y
= strtol(arg
+1, (char **)&arg
, 10);
1814 ar
= (double)x
/ (double)y
;
1816 ar
= strtod(arg
, (char **)&arg
);
1819 fprintf(stderr
, "Incorrect aspect ratio specification.\n");
1822 frame_aspect_ratio
= ar
;
1825 static void opt_gop_size(const char *arg
)
1827 gop_size
= atoi(arg
);
1830 static void opt_b_frames(const char *arg
)
1832 b_frames
= atoi(arg
);
1833 if (b_frames
> FF_MAX_B_FRAMES
) {
1834 fprintf(stderr
, "\nCannot have more than %d B frames, increase FF_MAX_B_FRAMES.\n", FF_MAX_B_FRAMES
);
1836 } else if (b_frames
< 1) {
1837 fprintf(stderr
, "\nNumber of B frames must be higher than 0\n");
1842 static void opt_mb_decision(const char *arg
)
1844 mb_decision
= atoi(arg
);
1847 static void opt_mb_cmp(const char *arg
)
1852 static void opt_ildct_cmp(const char *arg
)
1854 ildct_cmp
= atoi(arg
);
1857 static void opt_sub_cmp(const char *arg
)
1859 sub_cmp
= atoi(arg
);
1862 static void opt_cmp(const char *arg
)
1867 static void opt_pre_cmp(const char *arg
)
1869 pre_cmp
= atoi(arg
);
1872 static void opt_pre_me(const char *arg
)
1877 static void opt_lumi_mask(const char *arg
)
1879 lumi_mask
= atof(arg
);
1882 static void opt_dark_mask(const char *arg
)
1884 dark_mask
= atof(arg
);
1887 static void opt_scplx_mask(const char *arg
)
1889 scplx_mask
= atof(arg
);
1892 static void opt_tcplx_mask(const char *arg
)
1894 tcplx_mask
= atof(arg
);
1897 static void opt_p_mask(const char *arg
)
1902 static void opt_qscale(const char *arg
)
1904 video_qscale
= atof(arg
);
1905 if (video_qscale
< 0.01 ||
1906 video_qscale
> 255) {
1907 fprintf(stderr
, "qscale must be >= 0.01 and <= 255\n");
1912 static void opt_qmin(const char *arg
)
1914 video_qmin
= atoi(arg
);
1915 if (video_qmin
< 0 ||
1917 fprintf(stderr
, "qmin must be >= 1 and <= 31\n");
1922 static void opt_qmax(const char *arg
)
1924 video_qmax
= atoi(arg
);
1925 if (video_qmax
< 0 ||
1927 fprintf(stderr
, "qmax must be >= 1 and <= 31\n");
1932 static void opt_mb_qmin(const char *arg
)
1934 video_mb_qmin
= atoi(arg
);
1935 if (video_mb_qmin
< 0 ||
1936 video_mb_qmin
> 31) {
1937 fprintf(stderr
, "qmin must be >= 1 and <= 31\n");
1942 static void opt_mb_qmax(const char *arg
)
1944 video_mb_qmax
= atoi(arg
);
1945 if (video_mb_qmax
< 0 ||
1946 video_mb_qmax
> 31) {
1947 fprintf(stderr
, "qmax must be >= 1 and <= 31\n");
1952 static void opt_qdiff(const char *arg
)
1954 video_qdiff
= atoi(arg
);
1955 if (video_qdiff
< 0 ||
1957 fprintf(stderr
, "qdiff must be >= 1 and <= 31\n");
1962 static void opt_qblur(const char *arg
)
1964 video_qblur
= atof(arg
);
1967 static void opt_qcomp(const char *arg
)
1969 video_qcomp
= atof(arg
);
1972 static void opt_rc_initial_cplx(const char *arg
)
1974 video_rc_initial_cplx
= atof(arg
);
1976 static void opt_b_qfactor(const char *arg
)
1978 video_b_qfactor
= atof(arg
);
1980 static void opt_i_qfactor(const char *arg
)
1982 video_i_qfactor
= atof(arg
);
1984 static void opt_b_qoffset(const char *arg
)
1986 video_b_qoffset
= atof(arg
);
1988 static void opt_i_qoffset(const char *arg
)
1990 video_i_qoffset
= atof(arg
);
1993 static void opt_ibias(const char *arg
)
1995 video_intra_quant_bias
= atoi(arg
);
1997 static void opt_pbias(const char *arg
)
1999 video_inter_quant_bias
= atoi(arg
);
2002 static void opt_packet_size(const char *arg
)
2004 packet_size
= atoi(arg
);
2007 static void opt_error_rate(const char *arg
)
2009 error_rate
= atoi(arg
);
2012 static void opt_strict(const char *arg
)
2017 static void opt_top_field_first(const char *arg
)
2019 top_field_first
= atoi(arg
);
2022 static void opt_noise_reduction(const char *arg
)
2024 noise_reduction
= atoi(arg
);
2027 static void opt_qns(const char *arg
)
2032 static void opt_sc_threshold(const char *arg
)
2034 sc_threshold
= atoi(arg
);
2037 static void opt_thread_count(const char *arg
)
2039 thread_count
= atoi(arg
);
2040 #if !defined(HAVE_PTHREADS) && !defined(HAVE_W32THREADS)
2041 fprintf(stderr
, "Warning: not compiled with thread support, using thread emulation\n");
2045 static void opt_audio_bitrate(const char *arg
)
2047 audio_bit_rate
= atoi(arg
) * 1000;
2050 static void opt_audio_rate(const char *arg
)
2052 audio_sample_rate
= atoi(arg
);
2055 static void opt_audio_channels(const char *arg
)
2057 audio_channels
= atoi(arg
);
2060 static void opt_video_device(const char *arg
)
2062 video_device
= av_strdup(arg
);
2065 static void opt_video_channel(const char *arg
)
2067 video_channel
= strtol(arg
, NULL
, 0);
2070 static void opt_video_standard(const char *arg
)
2072 video_standard
= av_strdup(arg
);
2075 static void opt_audio_device(const char *arg
)
2077 audio_device
= av_strdup(arg
);
2080 static void opt_dv1394(const char *arg
)
2082 video_grab_format
= "dv1394";
2083 audio_grab_format
= NULL
;
2086 static void opt_audio_codec(const char *arg
)
2090 if (!strcmp(arg
, "copy")) {
2091 audio_stream_copy
= 1;
2095 if (!strcmp(p
->name
, arg
) && p
->type
== CODEC_TYPE_AUDIO
)
2100 fprintf(stderr
, "Unknown audio codec '%s'\n", arg
);
2103 audio_codec_id
= p
->id
;
2108 static void add_frame_hooker(const char *arg
)
2113 char *args
= av_strdup(arg
);
2117 argv
[0] = strtok(args
, " ");
2118 while (argc
< 62 && (argv
[++argc
] = strtok(NULL
, " "))) {
2121 i
= frame_hook_add(argc
, argv
);
2124 fprintf(stderr
, "Failed to add video hook function: %s\n", arg
);
2129 const char *motion_str
[] = {
2139 static void opt_motion_estimation(const char *arg
)
2145 fprintf(stderr
, "Unknown motion estimation method '%s'\n", arg
);
2148 if (!strcmp(*p
, arg
))
2152 me_method
= (p
- motion_str
) + 1;
2155 static void opt_video_codec(const char *arg
)
2159 if (!strcmp(arg
, "copy")) {
2160 video_stream_copy
= 1;
2164 if (!strcmp(p
->name
, arg
) && p
->type
== CODEC_TYPE_VIDEO
)
2169 fprintf(stderr
, "Unknown video codec '%s'\n", arg
);
2172 video_codec_id
= p
->id
;
2177 static void opt_map(const char *arg
)
2183 m
= &stream_maps
[nb_stream_maps
++];
2185 m
->file_index
= strtol(arg
, (char **)&p
, 0);
2189 m
->stream_index
= strtol(p
, (char **)&p
, 0);
2192 static void opt_recording_time(const char *arg
)
2194 recording_time
= parse_date(arg
, 1);
2197 static void opt_start_time(const char *arg
)
2199 start_time
= parse_date(arg
, 1);
2202 static void opt_input_file(const char *filename
)
2204 AVFormatContext
*ic
;
2205 AVFormatParameters params
, *ap
= ¶ms
;
2206 int err
, i
, ret
, rfps
, rfps_base
;
2208 if (!strcmp(filename
, "-"))
2211 using_stdin
|= !strcmp(filename
, "pipe:" ) ||
2212 !strcmp( filename
, "/dev/stdin" );
2214 /* get default parameters from command line */
2215 memset(ap
, 0, sizeof(*ap
));
2216 ap
->sample_rate
= audio_sample_rate
;
2217 ap
->channels
= audio_channels
;
2218 ap
->frame_rate
= frame_rate
;
2219 ap
->frame_rate_base
= frame_rate_base
;
2220 ap
->width
= frame_width
;
2221 ap
->height
= frame_height
;
2222 ap
->image_format
= image_format
;
2223 ap
->pix_fmt
= frame_pix_fmt
;
2225 /* open the input file with generic libav function */
2226 err
= av_open_input_file(&ic
, filename
, file_iformat
, 0, ap
);
2228 print_error(filename
, err
);
2232 /* If not enough info to get the stream parameters, we decode the
2233 first frames to get it. (used in mpeg case for example) */
2234 ret
= av_find_stream_info(ic
);
2236 fprintf(stderr
, "%s: could not find codec parameters\n", filename
);
2240 /* if seeking requested, we execute it */
2241 if (start_time
!= 0) {
2244 timestamp
= start_time
;
2245 /* add the stream start time */
2246 if (ic
->start_time
!= AV_NOPTS_VALUE
)
2247 timestamp
+= ic
->start_time
;
2248 ret
= av_seek_frame(ic
, -1, timestamp
);
2250 fprintf(stderr
, "%s: could not seek to position %0.3f\n",
2251 filename
, (double)timestamp
/ AV_TIME_BASE
);
2253 /* reset seek info */
2257 /* update the current parameters so that they match the one of the input stream */
2258 for(i
=0;i
<ic
->nb_streams
;i
++) {
2259 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
2260 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2262 avcodec_thread_init(enc
, thread_count
);
2264 enc
->thread_count
= thread_count
;
2265 switch(enc
->codec_type
) {
2266 case CODEC_TYPE_AUDIO
:
2267 //fprintf(stderr, "\nInput Audio channels: %d", enc->channels);
2268 audio_channels
= enc
->channels
;
2269 audio_sample_rate
= enc
->sample_rate
;
2271 case CODEC_TYPE_VIDEO
:
2272 frame_height
= enc
->height
;
2273 frame_width
= enc
->width
;
2274 frame_aspect_ratio
= av_q2d(enc
->sample_aspect_ratio
) * enc
->width
/ enc
->height
;
2275 frame_pix_fmt
= enc
->pix_fmt
;
2276 rfps
= ic
->streams
[i
]->r_frame_rate
;
2277 rfps_base
= ic
->streams
[i
]->r_frame_rate_base
;
2278 enc
->workaround_bugs
= workaround_bugs
;
2279 enc
->error_resilience
= error_resilience
;
2280 enc
->error_concealment
= error_concealment
;
2281 enc
->idct_algo
= idct_algo
;
2283 enc
->debug_mv
= debug_mv
;
2285 enc
->flags
|= CODEC_FLAG_BITEXACT
;
2287 assert(enc
->frame_rate_base
== rfps_base
); // should be true for now
2288 if (enc
->frame_rate
!= rfps
) {
2289 fprintf(stderr
,"\nSeems that stream %d comes from film source: %2.2f->%2.2f\n",
2290 i
, (float)enc
->frame_rate
/ enc
->frame_rate_base
,
2291 (float)rfps
/ rfps_base
);
2293 /* update the current frame rate to match the stream frame rate */
2295 frame_rate_base
= rfps_base
;
2297 enc
->rate_emu
= rate_emu
;
2299 case CODEC_TYPE_DATA
:
2306 input_files
[nb_input_files
] = ic
;
2307 /* dump the file content */
2308 dump_format(ic
, nb_input_files
, filename
, 0);
2310 file_iformat
= NULL
;
2311 file_oformat
= NULL
;
2312 image_format
= NULL
;
2317 static void check_audio_video_inputs(int *has_video_ptr
, int *has_audio_ptr
)
2319 int has_video
, has_audio
, i
, j
;
2320 AVFormatContext
*ic
;
2324 for(j
=0;j
<nb_input_files
;j
++) {
2325 ic
= input_files
[j
];
2326 for(i
=0;i
<ic
->nb_streams
;i
++) {
2327 AVCodecContext
*enc
= &ic
->streams
[i
]->codec
;
2328 switch(enc
->codec_type
) {
2329 case CODEC_TYPE_AUDIO
:
2332 case CODEC_TYPE_VIDEO
:
2340 *has_video_ptr
= has_video
;
2341 *has_audio_ptr
= has_audio
;
2344 static void opt_output_file(const char *filename
)
2347 AVFormatContext
*oc
;
2348 int use_video
, use_audio
, nb_streams
, input_has_video
, input_has_audio
;
2350 AVFormatParameters params
, *ap
= ¶ms
;
2352 if (!strcmp(filename
, "-"))
2355 oc
= av_alloc_format_context();
2357 if (!file_oformat
) {
2358 file_oformat
= guess_format(NULL
, filename
, NULL
);
2359 if (!file_oformat
) {
2360 fprintf(stderr
, "Unable for find a suitable output format for '%s'\n",
2366 oc
->oformat
= file_oformat
;
2368 if (!strcmp(file_oformat
->name
, "ffm") &&
2369 strstart(filename
, "http:", NULL
)) {
2370 /* special case for files sent to ffserver: we get the stream
2371 parameters from ffserver */
2372 if (read_ffserver_streams(oc
, filename
) < 0) {
2373 fprintf(stderr
, "Could not read stream parameters from '%s'\n", filename
);
2377 use_video
= file_oformat
->video_codec
!= CODEC_ID_NONE
;
2378 use_audio
= file_oformat
->audio_codec
!= CODEC_ID_NONE
;
2380 /* disable if no corresponding type found and at least one
2382 if (nb_input_files
> 0) {
2383 check_audio_video_inputs(&input_has_video
, &input_has_audio
);
2384 if (!input_has_video
)
2386 if (!input_has_audio
)
2390 /* manual disable */
2391 if (audio_disable
) {
2394 if (video_disable
) {
2400 AVCodecContext
*video_enc
;
2402 st
= av_mallocz(sizeof(AVStream
));
2404 fprintf(stderr
, "Could not alloc stream\n");
2407 avcodec_get_context_defaults(&st
->codec
);
2408 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2410 avcodec_thread_init(&st
->codec
, thread_count
);
2413 video_enc
= &st
->codec
;
2415 if(!strcmp(file_oformat
->name
, "mp4") || !strcmp(file_oformat
->name
, "mov") || !strcmp(file_oformat
->name
, "3gp"))
2416 video_enc
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
2417 if (video_stream_copy
) {
2418 st
->stream_copy
= 1;
2419 video_enc
->codec_type
= CODEC_TYPE_VIDEO
;
2424 codec_id
= file_oformat
->video_codec
;
2425 if (video_codec_id
!= CODEC_ID_NONE
)
2426 codec_id
= video_codec_id
;
2428 video_enc
->codec_id
= codec_id
;
2430 video_enc
->bit_rate
= video_bit_rate
;
2431 video_enc
->bit_rate_tolerance
= video_bit_rate_tolerance
;
2432 video_enc
->frame_rate
= frame_rate
;
2433 video_enc
->frame_rate_base
= frame_rate_base
;
2435 video_enc
->width
= frame_width
;
2436 video_enc
->height
= frame_height
;
2437 video_enc
->sample_aspect_ratio
= av_d2q(frame_aspect_ratio
*frame_height
/frame_width
, 255);
2438 video_enc
->pix_fmt
= frame_pix_fmt
;
2441 video_enc
->gop_size
= gop_size
;
2443 video_enc
->gop_size
= 0;
2444 if (video_qscale
|| same_quality
) {
2445 video_enc
->flags
|= CODEC_FLAG_QSCALE
;
2446 st
->quality
= FF_QP2LAMBDA
* video_qscale
;
2450 video_enc
->intra_matrix
= intra_matrix
;
2452 video_enc
->inter_matrix
= inter_matrix
;
2455 video_enc
->flags
|= CODEC_FLAG_BITEXACT
;
2457 video_enc
->mb_decision
= mb_decision
;
2458 video_enc
->mb_cmp
= mb_cmp
;
2459 video_enc
->ildct_cmp
= ildct_cmp
;
2460 video_enc
->me_sub_cmp
= sub_cmp
;
2461 video_enc
->me_cmp
= cmp
;
2462 video_enc
->me_pre_cmp
= pre_cmp
;
2463 video_enc
->pre_me
= pre_me
;
2464 video_enc
->lumi_masking
= lumi_mask
;
2465 video_enc
->dark_masking
= dark_mask
;
2466 video_enc
->spatial_cplx_masking
= scplx_mask
;
2467 video_enc
->temporal_cplx_masking
= tcplx_mask
;
2468 video_enc
->p_masking
= p_mask
;
2469 video_enc
->quantizer_noise_shaping
= qns
;
2472 video_enc
->flags
|= CODEC_FLAG_H263P_UMV
;
2475 video_enc
->flags
|= CODEC_FLAG_H263P_AIC
;
2478 video_enc
->flags
|= CODEC_FLAG_H263P_AIV
;
2481 video_enc
->flags
|= CODEC_FLAG_4MV
;
2484 video_enc
->flags
|= CODEC_FLAG_OBMC
;
2488 video_enc
->flags
|= CODEC_FLAG_PART
;
2491 video_enc
->flags
|= CODEC_FLAG_ALT_SCAN
;
2494 video_enc
->flags
|= CODEC_FLAG_TRELLIS_QUANT
;
2496 if (use_scan_offset
) {
2497 video_enc
->flags
|= CODEC_FLAG_SVCD_SCAN_OFFSET
;
2500 video_enc
->flags
|= CODEC_FLAG_CLOSED_GOP
;
2503 video_enc
->max_b_frames
= b_frames
;
2504 video_enc
->b_frame_strategy
= 0;
2505 video_enc
->b_quant_factor
= 2.0;
2507 if (do_interlace_dct
) {
2508 video_enc
->flags
|= CODEC_FLAG_INTERLACED_DCT
;
2510 if (do_interlace_me
) {
2511 video_enc
->flags
|= CODEC_FLAG_INTERLACED_ME
;
2513 video_enc
->qmin
= video_qmin
;
2514 video_enc
->qmax
= video_qmax
;
2515 video_enc
->mb_qmin
= video_mb_qmin
;
2516 video_enc
->mb_qmax
= video_mb_qmax
;
2517 video_enc
->max_qdiff
= video_qdiff
;
2518 video_enc
->qblur
= video_qblur
;
2519 video_enc
->qcompress
= video_qcomp
;
2520 video_enc
->rc_eq
= video_rc_eq
;
2521 video_enc
->debug
= debug
;
2522 video_enc
->debug_mv
= debug_mv
;
2523 video_enc
->thread_count
= thread_count
;
2524 p
= video_rc_override_string
;
2527 int e
=sscanf(p
, "%d,%d,%d", &start
, &end
, &q
);
2529 fprintf(stderr
, "error parsing rc_override\n");
2532 video_enc
->rc_override
=
2533 av_realloc(video_enc
->rc_override
,
2534 sizeof(RcOverride
)*(i
+1));
2535 video_enc
->rc_override
[i
].start_frame
= start
;
2536 video_enc
->rc_override
[i
].end_frame
= end
;
2538 video_enc
->rc_override
[i
].qscale
= q
;
2539 video_enc
->rc_override
[i
].quality_factor
= 1.0;
2542 video_enc
->rc_override
[i
].qscale
= 0;
2543 video_enc
->rc_override
[i
].quality_factor
= -q
/100.0;
2548 video_enc
->rc_override_count
=i
;
2550 video_enc
->rc_max_rate
= video_rc_max_rate
;
2551 video_enc
->rc_min_rate
= video_rc_min_rate
;
2552 video_enc
->rc_buffer_size
= video_rc_buffer_size
;
2553 video_enc
->rc_buffer_aggressivity
= video_rc_buffer_aggressivity
;
2554 video_enc
->rc_initial_cplx
= video_rc_initial_cplx
;
2555 video_enc
->i_quant_factor
= video_i_qfactor
;
2556 video_enc
->b_quant_factor
= video_b_qfactor
;
2557 video_enc
->i_quant_offset
= video_i_qoffset
;
2558 video_enc
->b_quant_offset
= video_b_qoffset
;
2559 video_enc
->intra_quant_bias
= video_intra_quant_bias
;
2560 video_enc
->inter_quant_bias
= video_inter_quant_bias
;
2561 video_enc
->dct_algo
= dct_algo
;
2562 video_enc
->idct_algo
= idct_algo
;
2563 video_enc
->strict_std_compliance
= strict
;
2564 video_enc
->error_rate
= error_rate
;
2565 video_enc
->noise_reduction
= noise_reduction
;
2566 video_enc
->scenechange_threshold
= sc_threshold
;
2568 video_enc
->rtp_mode
= 1;
2569 video_enc
->rtp_payload_size
= packet_size
;
2573 video_enc
->flags
|= CODEC_FLAG_PSNR
;
2575 video_enc
->me_method
= me_method
;
2580 video_enc
->flags
|= CODEC_FLAG_PASS1
;
2582 video_enc
->flags
|= CODEC_FLAG_PASS2
;
2586 oc
->streams
[nb_streams
] = st
;
2591 AVCodecContext
*audio_enc
;
2593 st
= av_mallocz(sizeof(AVStream
));
2595 fprintf(stderr
, "Could not alloc stream\n");
2598 avcodec_get_context_defaults(&st
->codec
);
2599 #if defined(HAVE_PTHREADS) || defined(HAVE_W32THREADS)
2601 avcodec_thread_init(&st
->codec
, thread_count
);
2604 audio_enc
= &st
->codec
;
2605 audio_enc
->codec_type
= CODEC_TYPE_AUDIO
;
2607 if(!strcmp(file_oformat
->name
, "mp4") || !strcmp(file_oformat
->name
, "mov") || !strcmp(file_oformat
->name
, "3gp"))
2608 audio_enc
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
2609 if (audio_stream_copy
) {
2610 st
->stream_copy
= 1;
2612 codec_id
= file_oformat
->audio_codec
;
2613 if (audio_codec_id
!= CODEC_ID_NONE
)
2614 codec_id
= audio_codec_id
;
2615 audio_enc
->codec_id
= codec_id
;
2617 audio_enc
->bit_rate
= audio_bit_rate
;
2618 audio_enc
->sample_rate
= audio_sample_rate
;
2619 audio_enc
->strict_std_compliance
= strict
;
2620 audio_enc
->thread_count
= thread_count
;
2621 /* For audio codecs other than AC3 we limit */
2622 /* the number of coded channels to stereo */
2623 if (audio_channels
> 2 && codec_id
!= CODEC_ID_AC3
) {
2624 audio_enc
->channels
= 2;
2626 audio_enc
->channels
= audio_channels
;
2628 oc
->streams
[nb_streams
] = st
;
2632 oc
->nb_streams
= nb_streams
;
2635 fprintf(stderr
, "No audio or video streams available\n");
2640 pstrcpy(oc
->title
, sizeof(oc
->title
), str_title
);
2642 pstrcpy(oc
->author
, sizeof(oc
->author
), str_author
);
2644 pstrcpy(oc
->copyright
, sizeof(oc
->copyright
), str_copyright
);
2646 pstrcpy(oc
->comment
, sizeof(oc
->comment
), str_comment
);
2649 output_files
[nb_output_files
++] = oc
;
2651 strcpy(oc
->filename
, filename
);
2653 /* check filename in case of an image number is expected */
2654 if (oc
->oformat
->flags
& AVFMT_NEEDNUMBER
) {
2655 if (filename_number_test(oc
->filename
) < 0) {
2656 print_error(oc
->filename
, AVERROR_NUMEXPECTED
);
2661 if (!(oc
->oformat
->flags
& AVFMT_NOFILE
)) {
2662 /* test if it already exists to avoid loosing precious files */
2663 if (!file_overwrite
&&
2664 (strchr(filename
, ':') == NULL
||
2665 strstart(filename
, "file:", NULL
))) {
2666 if (url_exist(filename
)) {
2669 if ( !using_stdin
) {
2670 fprintf(stderr
,"File '%s' already exists. Overwrite ? [y/N] ", filename
);
2673 if (toupper(c
) != 'Y') {
2674 fprintf(stderr
, "Not overwriting - exiting\n");
2679 fprintf(stderr
,"File '%s' already exists. Exiting.\n", filename
);
2686 if (url_fopen(&oc
->pb
, filename
, URL_WRONLY
) < 0) {
2687 fprintf(stderr
, "Could not open '%s'\n", filename
);
2692 memset(ap
, 0, sizeof(*ap
));
2693 ap
->image_format
= image_format
;
2694 if (av_set_parameters(oc
, ap
) < 0) {
2695 fprintf(stderr
, "%s: Invalid encoding parameters\n",
2700 /* reset some options */
2701 file_oformat
= NULL
;
2702 file_iformat
= NULL
;
2703 image_format
= NULL
;
2706 audio_codec_id
= CODEC_ID_NONE
;
2707 video_codec_id
= CODEC_ID_NONE
;
2708 audio_stream_copy
= 0;
2709 video_stream_copy
= 0;
2712 /* prepare dummy protocols for grab */
2713 static void prepare_grab(void)
2715 int has_video
, has_audio
, i
, j
;
2716 AVFormatContext
*oc
;
2717 AVFormatContext
*ic
;
2718 AVFormatParameters vp1
, *vp
= &vp1
;
2719 AVFormatParameters ap1
, *ap
= &ap1
;
2721 /* see if audio/video inputs are needed */
2724 memset(ap
, 0, sizeof(*ap
));
2725 memset(vp
, 0, sizeof(*vp
));
2726 for(j
=0;j
<nb_output_files
;j
++) {
2727 oc
= output_files
[j
];
2728 for(i
=0;i
<oc
->nb_streams
;i
++) {
2729 AVCodecContext
*enc
= &oc
->streams
[i
]->codec
;
2730 switch(enc
->codec_type
) {
2731 case CODEC_TYPE_AUDIO
:
2732 if (enc
->sample_rate
> ap
->sample_rate
)
2733 ap
->sample_rate
= enc
->sample_rate
;
2734 if (enc
->channels
> ap
->channels
)
2735 ap
->channels
= enc
->channels
;
2738 case CODEC_TYPE_VIDEO
:
2739 if (enc
->width
> vp
->width
)
2740 vp
->width
= enc
->width
;
2741 if (enc
->height
> vp
->height
)
2742 vp
->height
= enc
->height
;
2744 assert(enc
->frame_rate_base
== DEFAULT_FRAME_RATE_BASE
);
2745 if (enc
->frame_rate
> vp
->frame_rate
){
2746 vp
->frame_rate
= enc
->frame_rate
;
2747 vp
->frame_rate_base
= enc
->frame_rate_base
;
2757 if (has_video
== 0 && has_audio
== 0) {
2758 fprintf(stderr
, "Output file must have at least one audio or video stream\n");
2763 AVInputFormat
*fmt1
;
2764 fmt1
= av_find_input_format(video_grab_format
);
2765 vp
->device
= video_device
;
2766 vp
->channel
= video_channel
;
2767 vp
->standard
= video_standard
;
2768 if (av_open_input_file(&ic
, "", fmt1
, 0, vp
) < 0) {
2769 fprintf(stderr
, "Could not find video grab device\n");
2772 /* If not enough info to get the stream parameters, we decode the
2773 first frames to get it. */
2774 if ((ic
->ctx_flags
& AVFMTCTX_NOHEADER
) && av_find_stream_info(ic
) < 0) {
2775 fprintf(stderr
, "Could not find video grab parameters\n");
2778 /* by now video grab has one stream */
2779 ic
->streams
[0]->r_frame_rate
= vp
->frame_rate
;
2780 ic
->streams
[0]->r_frame_rate_base
= vp
->frame_rate_base
;
2781 input_files
[nb_input_files
] = ic
;
2782 dump_format(ic
, nb_input_files
, "", 0);
2785 if (has_audio
&& audio_grab_format
) {
2786 AVInputFormat
*fmt1
;
2787 fmt1
= av_find_input_format(audio_grab_format
);
2788 ap
->device
= audio_device
;
2789 if (av_open_input_file(&ic
, "", fmt1
, 0, ap
) < 0) {
2790 fprintf(stderr
, "Could not find audio grab device\n");
2793 input_files
[nb_input_files
] = ic
;
2794 dump_format(ic
, nb_input_files
, "", 0);
2799 /* same option as mencoder */
2800 static void opt_pass(const char *pass_str
)
2803 pass
= atoi(pass_str
);
2804 if (pass
!= 1 && pass
!= 2) {
2805 fprintf(stderr
, "pass number can be only 1 or 2\n");
2811 #if defined(CONFIG_WIN32) || defined(CONFIG_OS2)
2812 static int64_t getutime(void)
2814 return av_gettime();
2817 static int64_t getutime(void)
2819 struct rusage rusage
;
2821 getrusage(RUSAGE_SELF
, &rusage
);
2822 return (rusage
.ru_utime
.tv_sec
* 1000000LL) + rusage
.ru_utime
.tv_usec
;
2826 extern int ffm_nopts
;
2828 static void opt_bitexact(void)
2831 /* disable generate of real time pts in ffm (need to be supressed anyway) */
2835 static void show_formats(void)
2837 AVInputFormat
*ifmt
;
2838 AVOutputFormat
*ofmt
;
2839 AVImageFormat
*image_fmt
;
2842 const char **pp
, *last_name
;
2844 printf("File formats:\n");
2849 const char *name
=NULL
;
2851 for(ofmt
= first_oformat
; ofmt
!= NULL
; ofmt
= ofmt
->next
) {
2852 if((name
== NULL
|| strcmp(ofmt
->name
, name
)<0) &&
2853 strcmp(ofmt
->name
, last_name
)>0){
2858 for(ifmt
= first_iformat
; ifmt
!= NULL
; ifmt
= ifmt
->next
) {
2859 if((name
== NULL
|| strcmp(ifmt
->name
, name
)<0) &&
2860 strcmp(ifmt
->name
, last_name
)>0){
2864 if(name
&& strcmp(ifmt
->name
, name
)==0)
2879 printf("Image formats:\n");
2880 for(image_fmt
= first_image_format
; image_fmt
!= NULL
;
2881 image_fmt
= image_fmt
->next
) {
2884 image_fmt
->img_read ?
"D":" ",
2885 image_fmt
->img_write ?
"E":" ",
2890 printf("Codecs:\n");
2898 for(p
= first_avcodec
; p
!= NULL
; p
= p
->next
) {
2899 if((p2
==NULL
|| strcmp(p
->name
, p2
->name
)<0) &&
2900 strcmp(p
->name
, last_name
)>0){
2902 decode
= encode
= cap
=0;
2904 if(p2
&& strcmp(p
->name
, p2
->name
)==0){
2905 if(p
->decode
) decode
=1;
2906 if(p
->encode
) encode
=1;
2907 cap
|= p
->capabilities
;
2912 last_name
= p2
->name
;
2916 decode ?
"D": (/*p2->decoder ? "d":*/" "),
2918 p2
->type
== CODEC_TYPE_AUDIO ?
"A":"V",
2919 cap
& CODEC_CAP_DRAW_HORIZ_BAND ?
"S":" ",
2920 cap
& CODEC_CAP_DR1 ?
"D":" ",
2921 cap
& CODEC_CAP_TRUNCATED ?
"T":" ",
2923 /* if(p2->decoder && decode==0)
2924 printf(" use %s for decoding", p2->decoder->name);*/
2929 printf("Supported file protocols:\n");
2930 for(up
= first_protocol
; up
!= NULL
; up
= up
->next
)
2931 printf(" %s:", up
->name
);
2934 printf("Frame size, frame rate abbreviations:\n ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
2935 printf("Motion estimation methods:\n");
2939 if ((pp
- motion_str
+ 1) == ME_ZERO
)
2940 printf("(fastest)");
2941 else if ((pp
- motion_str
+ 1) == ME_FULL
)
2942 printf("(slowest)");
2943 else if ((pp
- motion_str
+ 1) == ME_EPZS
)
2944 printf("(default)");
2949 "Note, the names of encoders and decoders dont always match, so there are\n"
2950 "several cases where the above table shows encoder only or decoder only entries\n"
2951 "even though both encoding and decoding are supported for example, the h263\n"
2952 "decoder corresponds to the h263 and h263p encoders, for file formats its even\n"
2957 void parse_matrix_coeffs(uint16_t *dest
, const char *str
)
2960 const char *p
= str
;
2967 fprintf(stderr
, "Syntax error in matrix \"%s\" at coeff %d\n", str
, i
);
2974 void opt_inter_matrix(const char *arg
)
2976 inter_matrix
= av_mallocz(sizeof(uint16_t) * 64);
2977 parse_matrix_coeffs(inter_matrix
, arg
);
2980 void opt_intra_matrix(const char *arg
)
2982 intra_matrix
= av_mallocz(sizeof(uint16_t) * 64);
2983 parse_matrix_coeffs(intra_matrix
, arg
);
2986 static void opt_target(const char *arg
)
2990 if(!strncmp(arg
, "pal-", 4)) {
2993 } else if(!strncmp(arg
, "ntsc-", 5)) {
2998 /* Calculate FR via float to avoid int overflow */
2999 fr
= (int)(frame_rate
* 1000.0 / frame_rate_base
);
3002 } else if((fr
== 29970) || (fr
== 23976)) {
3005 /* Try to determine PAL/NTSC by peeking in the input files */
3006 if(nb_input_files
) {
3008 for(j
= 0; j
< nb_input_files
; j
++) {
3009 for(i
= 0; i
< input_files
[j
]->nb_streams
; i
++) {
3010 AVCodecContext
*c
= &input_files
[j
]->streams
[i
]->codec
;
3011 if(c
->codec_type
!= CODEC_TYPE_VIDEO
)
3013 fr
= c
->frame_rate
* 1000 / c
->frame_rate_base
;
3017 } else if((fr
== 29970) || (fr
== 23976)) {
3027 if(verbose
&& norm
>= 0)
3028 printf("Assuming %s for target.\n", norm ?
"NTSC" : "PAL");
3032 fprintf(stderr
, "Could not determine norm (PAL/NTSC) for target.\n");
3033 fprintf(stderr
, "Please prefix target with \"pal-\" or \"ntsc-\",\n");
3034 fprintf(stderr
, "or set a framerate with \"-r xxx\".\n");
3038 if(!strcmp(arg
, "vcd")) {
3040 opt_video_codec("mpeg1video");
3041 opt_audio_codec("mp2");
3044 opt_frame_size(norm ?
"352x240" : "352x288");
3046 video_bit_rate
= 1150000;
3047 video_rc_max_rate
= 1150000;
3048 video_rc_min_rate
= 1150000;
3049 video_rc_buffer_size
= 40*1024*8;
3051 audio_bit_rate
= 224000;
3052 audio_sample_rate
= 44100;
3054 } else if(!strcmp(arg
, "svcd")) {
3056 opt_video_codec("mpeg2video");
3057 opt_audio_codec("mp2");
3060 opt_frame_size(norm ?
"480x480" : "480x576");
3061 opt_gop_size(norm ?
"18" : "15");
3063 video_bit_rate
= 2040000;
3064 video_rc_max_rate
= 2516000;
3065 video_rc_min_rate
= 0; //1145000;
3066 video_rc_buffer_size
= 224*1024*8;
3067 use_scan_offset
= 1;
3069 audio_bit_rate
= 224000;
3070 audio_sample_rate
= 44100;
3072 } else if(!strcmp(arg
, "dvd")) {
3074 opt_video_codec("mpeg2video");
3075 opt_audio_codec("ac3");
3078 opt_frame_size(norm ?
"720x480" : "720x576");
3079 opt_gop_size(norm ?
"18" : "15");
3081 video_bit_rate
= 6000000;
3082 video_rc_max_rate
= 9000000;
3083 video_rc_min_rate
= 0; //1500000;
3084 video_rc_buffer_size
= 224*1024*8;
3086 audio_bit_rate
= 448000;
3087 audio_sample_rate
= 48000;
3090 fprintf(stderr
, "Unknown target: %s\n", arg
);
3095 const OptionDef options
[] = {
3097 { "L", 0, {(void*)show_license
}, "show license" },
3098 { "h", 0, {(void*)show_help
}, "show help" },
3099 { "formats", 0, {(void*)show_formats
}, "show available formats, codecs, protocols, ..." },
3100 { "f", HAS_ARG
, {(void*)opt_format
}, "force format", "fmt" },
3101 { "img", HAS_ARG
, {(void*)opt_image_format
}, "force image format", "img_fmt" },
3102 { "i", HAS_ARG
, {(void*)opt_input_file
}, "input file name", "filename" },
3103 { "y", OPT_BOOL
, {(void*)&file_overwrite
}, "overwrite output files" },
3104 { "map", HAS_ARG
| OPT_EXPERT
, {(void*)opt_map
}, "set input stream mapping", "file:stream" },
3105 { "t", HAS_ARG
, {(void*)opt_recording_time
}, "set the recording time", "duration" },
3106 { "ss", HAS_ARG
, {(void*)opt_start_time
}, "set the start time offset", "time_off" },
3107 { "title", HAS_ARG
| OPT_STRING
, {(void*)&str_title
}, "set the title", "string" },
3108 { "author", HAS_ARG
| OPT_STRING
, {(void*)&str_author
}, "set the author", "string" },
3109 { "copyright", HAS_ARG
| OPT_STRING
, {(void*)&str_copyright
}, "set the copyright", "string" },
3110 { "comment", HAS_ARG
| OPT_STRING
, {(void*)&str_comment
}, "set the comment", "string" },
3111 { "debug", HAS_ARG
| OPT_EXPERT
, {(void*)opt_debug
}, "print specific debug info", "" },
3112 { "vismv", HAS_ARG
| OPT_EXPERT
, {(void*)opt_vismv
}, "visualize motion vectors", "" },
3113 { "benchmark", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_benchmark
},
3114 "add timings for benchmarking" },
3115 { "dump", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_pkt_dump
},
3116 "dump each input packet" },
3117 { "hex", OPT_BOOL
| OPT_EXPERT
, {(void*)&do_hex_dump
},
3118 "when dumping packets, also dump the payload" },
3119 { "bitexact", OPT_EXPERT
, {(void*)opt_bitexact
}, "only use bit exact algorithms (for codec testing)" },
3120 { "re", OPT_BOOL
| OPT_EXPERT
, {(void*)&rate_emu
}, "read input at native frame rate", "" },
3121 { "loop", OPT_BOOL
| OPT_EXPERT
, {(void*)&loop_input
}, "loop (current only works with images)" },
3122 { "v", HAS_ARG
, {(void*)opt_verbose
}, "control amount of logging", "verbose" },
3123 { "target", HAS_ARG
, {(void*)opt_target
}, "specify target file type (\"vcd\", \"svcd\" or \"dvd\")", "type" },
3124 { "threads", HAS_ARG
| OPT_EXPERT
, {(void*)opt_thread_count
}, "thread count", "count" },
3127 { "b", HAS_ARG
| OPT_VIDEO
, {(void*)opt_video_bitrate
}, "set video bitrate (in kbit/s)", "bitrate" },
3128 { "r", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_rate
}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
3129 { "s", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_size
}, "set frame size (WxH or abbreviation)", "size" },
3130 { "aspect", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_aspect_ratio
}, "set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)", "aspect" },
3131 { "pix_fmt", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_frame_pix_fmt
}, "set pixel format", "format" },
3132 { "croptop", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_crop_top
}, "set top crop band size (in pixels)", "size" },
3133 { "cropbottom", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_crop_bottom
}, "set bottom crop band size (in pixels)", "size" },
3134 { "cropleft", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_crop_left
}, "set left crop band size (in pixels)", "size" },
3135 { "cropright", HAS_ARG
| OPT_VIDEO
, {(void*)opt_frame_crop_right
}, "set right crop band size (in pixels)", "size" },
3136 { "g", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_gop_size
}, "set the group of picture size", "gop_size" },
3137 { "intra", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&intra_only
}, "use only intra frames"},
3138 { "vn", OPT_BOOL
| OPT_VIDEO
, {(void*)&video_disable
}, "disable video" },
3139 { "qscale", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qscale
}, "use fixed video quantiser scale (VBR)", "q" },
3140 { "qmin", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qmin
}, "min video quantiser scale (VBR)", "q" },
3141 { "qmax", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qmax
}, "max video quantiser scale (VBR)", "q" },
3142 { "mbqmin", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_mb_qmin
}, "min macroblock quantiser scale (VBR)", "q" },
3143 { "mbqmax", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_mb_qmax
}, "max macroblock quantiser scale (VBR)", "q" },
3144 { "qdiff", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qdiff
}, "max difference between the quantiser scale (VBR)", "q" },
3145 { "qblur", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qblur
}, "video quantiser scale blur (VBR)", "blur" },
3146 { "qcomp", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qcomp
}, "video quantiser scale compression (VBR)", "compression" },
3147 { "rc_init_cplx", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_rc_initial_cplx
}, "initial complexity for 1-pass encoding", "complexity" },
3148 { "b_qfactor", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_b_qfactor
}, "qp factor between p and b frames", "factor" },
3149 { "i_qfactor", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_i_qfactor
}, "qp factor between p and i frames", "factor" },
3150 { "b_qoffset", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_b_qoffset
}, "qp offset between p and b frames", "offset" },
3151 { "i_qoffset", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_i_qoffset
}, "qp offset between p and i frames", "offset" },
3152 { "ibias", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_ibias
}, "intra quant bias", "bias" },
3153 { "pbias", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_pbias
}, "inter quant bias", "bias" },
3154 // { "b_strategy", HAS_ARG | OPT_EXPERT, {(void*)opt_b_strategy}, "dynamic b frame selection strategy", "strategy" },
3155 { "rc_eq", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_video_rc_eq
}, "set rate control equation", "equation" },
3156 { "rc_override", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_video_rc_override_string
}, "rate control override for specific intervals", "override" },
3157 { "bt", HAS_ARG
| OPT_VIDEO
, {(void*)opt_video_bitrate_tolerance
}, "set video bitrate tolerance (in kbit/s)", "tolerance" },
3158 { "maxrate", HAS_ARG
| OPT_VIDEO
, {(void*)opt_video_bitrate_max
}, "set max video bitrate tolerance (in kbit/s)", "bitrate" },
3159 { "minrate", HAS_ARG
| OPT_VIDEO
, {(void*)opt_video_bitrate_min
}, "set min video bitrate tolerance (in kbit/s)", "bitrate" },
3160 { "bufsize", HAS_ARG
| OPT_VIDEO
, {(void*)opt_video_buffer_size
}, "set ratecontrol buffere size (in kByte)", "size" },
3161 { "vcodec", HAS_ARG
| OPT_VIDEO
, {(void*)opt_video_codec
}, "force video codec ('copy' to copy stream)", "codec" },
3162 { "me", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_motion_estimation
}, "set motion estimation method",
3164 { "dct_algo", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_dct_algo
}, "set dct algo", "algo" },
3165 { "idct_algo", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_idct_algo
}, "set idct algo", "algo" },
3166 { "er", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_error_resilience
}, "set error resilience", "n" },
3167 { "ec", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_error_concealment
}, "set error concealment", "bit_mask" },
3168 { "bf", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_b_frames
}, "use 'frames' B frames", "frames" },
3169 { "hq", OPT_BOOL
, {(void*)&mb_decision
}, "activate high quality settings" },
3170 { "mbd", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_mb_decision
}, "macroblock decision", "mode" },
3171 { "mbcmp", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_mb_cmp
}, "macroblock compare function", "cmp function" },
3172 { "ildctcmp", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_ildct_cmp
}, "ildct compare function", "cmp function" },
3173 { "subcmp", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_sub_cmp
}, "subpel compare function", "cmp function" },
3174 { "cmp", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_cmp
}, "fullpel compare function", "cmp function" },
3175 { "precmp", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_pre_cmp
}, "pre motion estimation compare function", "cmp function" },
3176 { "preme", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_pre_me
}, "pre motion estimation", "" },
3177 { "lumi_mask", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_lumi_mask
}, "luminance masking", "" },
3178 { "dark_mask", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_dark_mask
}, "darkness masking", "" },
3179 { "scplx_mask", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_scplx_mask
}, "spatial complexity masking", "" },
3180 { "tcplx_mask", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_tcplx_mask
}, "teporal complexity masking", "" },
3181 { "p_mask", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_p_mask
}, "inter masking", "" },
3182 { "4mv", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_4mv
}, "use four motion vector by macroblock (MPEG4)" },
3183 { "obmc", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_obmc
}, "use overlapped block motion compensation (h263+)" },
3184 { "part", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_part
}, "use data partitioning (MPEG4)" },
3185 { "bug", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_workaround_bugs
}, "workaround not auto detected encoder bugs", "param" },
3186 { "ps", HAS_ARG
| OPT_EXPERT
, {(void*)opt_packet_size
}, "set packet size in bits", "size" },
3187 { "error", HAS_ARG
| OPT_EXPERT
, {(void*)opt_error_rate
}, "error rate", "rate" },
3188 { "strict", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_strict
}, "how strictly to follow the standarts", "strictness" },
3189 { "sameq", OPT_BOOL
| OPT_VIDEO
, {(void*)&same_quality
},
3190 "use same video quality as source (implies VBR)" },
3191 { "pass", HAS_ARG
| OPT_VIDEO
, {(void*)&opt_pass
}, "select the pass number (1 or 2)", "n" },
3192 { "passlogfile", HAS_ARG
| OPT_STRING
| OPT_VIDEO
, {(void*)&pass_logfilename
}, "select two pass log file name", "file" },
3193 { "deinterlace", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&do_deinterlace
},
3194 "deinterlace pictures" },
3195 { "ildct", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&do_interlace_dct
},
3196 "force interlaced dct support in encoder (MPEG2/MPEG4)" },
3197 { "ilme", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&do_interlace_me
},
3198 "force interlacied me support in encoder MPEG2" },
3199 { "psnr", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&do_psnr
}, "calculate PSNR of compressed frames" },
3200 { "vstats", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&do_vstats
}, "dump video coding statistics to file" },
3201 { "vhook", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)add_frame_hooker
}, "insert video processing module", "module" },
3202 { "aic", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_aic
}, "enable Advanced intra coding (h263+)" },
3203 { "aiv", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_aiv
}, "enable Alternative inter vlc (h263+)" },
3204 { "umv", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_umv
}, "enable Unlimited Motion Vector (h263+)" },
3205 { "alt", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_alt_scan
}, "enable alternate scantable (mpeg2)" },
3206 { "trell", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_trell
}, "enable trellis quantization" },
3207 { "cgop", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&closed_gop
}, "closed gop" },
3208 { "scan_offset", OPT_BOOL
| OPT_EXPERT
| OPT_VIDEO
, {(void*)&use_scan_offset
}, "enable SVCD Scan Offset placeholder" },
3209 { "intra_matrix", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_intra_matrix
}, "specify intra matrix coeffs", "matrix" },
3210 { "inter_matrix", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_inter_matrix
}, "specify inter matrix coeffs", "matrix" },
3211 { "top", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_top_field_first
}, "top=1/bottom=0/auto=-1 field first", "" },
3212 { "nr", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_noise_reduction
}, "noise reduction", "" },
3213 { "qns", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_qns
}, "quantization noise shaping", "" },
3214 { "sc_threshold", HAS_ARG
| OPT_EXPERT
| OPT_VIDEO
, {(void*)opt_sc_threshold
}, "scene change threshold", "threshold" },
3217 { "ab", HAS_ARG
| OPT_AUDIO
, {(void*)opt_audio_bitrate
}, "set audio bitrate (in kbit/s)", "bitrate", },
3218 { "ar", HAS_ARG
| OPT_AUDIO
, {(void