2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #if !defined(INFINITY) && defined(HUGE_VAL)
28 #define INFINITY HUGE_VAL
36 #include "osdep/strsep.h"
38 #include "codec-cfg.h"
39 #include "stream/stream.h"
40 #include "libmpdemux/demuxer.h"
41 #include "libmpdemux/stheader.h"
43 #include "libmpdemux/muxer.h"
45 #include "img_format.h"
46 #include "fmt-conversion.h"
50 extern char* passtmpfile
;
52 //===========================================================================//
54 #include "libavcodec/avcodec.h"
56 extern int avcodec_initialized
;
59 static char *lavc_param_vcodec
= "mpeg4";
60 static int lavc_param_vbitrate
= -1;
61 static int lavc_param_vrate_tolerance
= 1000*8;
62 static int lavc_param_mb_decision
= 0; /* default is realtime encoding */
63 static int lavc_param_v4mv
= 0;
64 static int lavc_param_vme
= 4;
65 static float lavc_param_vqscale
= -1;
66 static int lavc_param_vqmin
= 2;
67 static int lavc_param_vqmax
= 31;
68 static int lavc_param_mb_qmin
= 2;
69 static int lavc_param_mb_qmax
= 31;
70 static float lavc_param_lmin
= 2;
71 static float lavc_param_lmax
= 31;
72 static float lavc_param_mb_lmin
= 2;
73 static float lavc_param_mb_lmax
= 31;
74 static int lavc_param_vqdiff
= 3;
75 static float lavc_param_vqcompress
= 0.5;
76 static float lavc_param_vqblur
= 0.5;
77 static float lavc_param_vb_qfactor
= 1.25;
78 static float lavc_param_vb_qoffset
= 1.25;
79 static float lavc_param_vi_qfactor
= 0.8;
80 static float lavc_param_vi_qoffset
= 0.0;
81 static int lavc_param_vmax_b_frames
= 0;
82 static int lavc_param_keyint
= -1;
83 static int lavc_param_vpass
= 0;
84 static int lavc_param_vrc_strategy
= 0;
85 static int lavc_param_vb_strategy
= 0;
86 static int lavc_param_luma_elim_threshold
= 0;
87 static int lavc_param_chroma_elim_threshold
= 0;
88 static int lavc_param_packet_size
= 0;
89 static int lavc_param_strict
= -1;
90 static int lavc_param_data_partitioning
= 0;
91 static int lavc_param_gray
=0;
92 static float lavc_param_rc_qsquish
=1.0;
93 static float lavc_param_rc_qmod_amp
=0;
94 static int lavc_param_rc_qmod_freq
=0;
95 static char *lavc_param_rc_override_string
=NULL
;
96 static char *lavc_param_rc_eq
="tex^qComp";
97 static int lavc_param_rc_buffer_size
=0;
98 static float lavc_param_rc_buffer_aggressivity
=1.0;
99 static int lavc_param_rc_max_rate
=0;
100 static int lavc_param_rc_min_rate
=0;
101 static float lavc_param_rc_initial_cplx
=0;
102 static float lavc_param_rc_initial_buffer_occupancy
=0.9;
103 static int lavc_param_mpeg_quant
=0;
104 static int lavc_param_fdct
=0;
105 static int lavc_param_idct
=0;
106 static char* lavc_param_aspect
= NULL
;
107 static int lavc_param_autoaspect
= 0;
108 static float lavc_param_lumi_masking
= 0.0;
109 static float lavc_param_dark_masking
= 0.0;
110 static float lavc_param_temporal_cplx_masking
= 0.0;
111 static float lavc_param_spatial_cplx_masking
= 0.0;
112 static float lavc_param_p_masking
= 0.0;
113 static float lavc_param_border_masking
= 0.0;
114 static int lavc_param_normalize_aqp
= 0;
115 static int lavc_param_interlaced_dct
= 0;
116 static int lavc_param_prediction_method
= FF_PRED_LEFT
;
117 static int lavc_param_format
= IMGFMT_YV12
;
118 static int lavc_param_debug
= 0;
119 static int lavc_param_psnr
= 0;
120 static int lavc_param_me_pre_cmp
= 0;
121 static int lavc_param_me_cmp
= 0;
122 static int lavc_param_me_sub_cmp
= 0;
123 static int lavc_param_mb_cmp
= 0;
125 static int lavc_param_ildct_cmp
= FF_CMP_VSAD
;
127 static int lavc_param_pre_dia_size
= 0;
128 static int lavc_param_dia_size
= 0;
129 static int lavc_param_qpel
= 0;
130 static int lavc_param_trell
= 0;
131 static int lavc_param_lowdelay
= 0;
132 static int lavc_param_bit_exact
= 0;
133 static int lavc_param_aic
= 0;
134 static int lavc_param_aiv
= 0;
135 static int lavc_param_umv
= 0;
136 static int lavc_param_gmc
= 0;
137 static int lavc_param_obmc
= 0;
138 static int lavc_param_loop
= 0;
139 static int lavc_param_last_pred
= 0;
140 static int lavc_param_pre_me
= 1;
141 static int lavc_param_me_subpel_quality
= 8;
142 static int lavc_param_me_range
= 0;
143 static int lavc_param_ibias
= FF_DEFAULT_QUANT_BIAS
;
144 static int lavc_param_pbias
= FF_DEFAULT_QUANT_BIAS
;
145 static int lavc_param_coder
= 0;
146 static int lavc_param_context
= 0;
147 static char *lavc_param_intra_matrix
= NULL
;
148 static char *lavc_param_inter_matrix
= NULL
;
149 static int lavc_param_cbp
= 0;
150 static int lavc_param_mv0
= 0;
151 static int lavc_param_noise_reduction
= 0;
152 static int lavc_param_qns
= 0;
153 static int lavc_param_qp_rd
= 0;
154 static int lavc_param_inter_threshold
= 0;
155 static int lavc_param_sc_threshold
= 0;
156 static int lavc_param_ss
= 0;
157 static int lavc_param_top
= -1;
158 static int lavc_param_alt
= 0;
159 static int lavc_param_ilme
= 0;
160 static int lavc_param_nssew
= 8;
161 static int lavc_param_closed_gop
= 0;
162 static int lavc_param_dc_precision
= 8;
163 static int lavc_param_threads
= 1;
164 static int lavc_param_turbo
= 0;
165 static int lavc_param_skip_threshold
=0;
166 static int lavc_param_skip_factor
=0;
167 static int lavc_param_skip_exp
=0;
168 static int lavc_param_skip_cmp
=0;
169 static int lavc_param_brd_scale
= 0;
170 static int lavc_param_bidir_refine
= 0;
171 static int lavc_param_sc_factor
= 1;
172 static int lavc_param_video_global_header
= 0;
173 static int lavc_param_mv0_threshold
= 256;
174 static int lavc_param_refs
= 1;
175 static int lavc_param_b_sensitivity
= 40;
176 static int lavc_param_level
= FF_LEVEL_UNKNOWN
;
178 char *lavc_param_acodec
= "mp2";
179 int lavc_param_atag
= 0;
180 int lavc_param_abitrate
= 224;
181 int lavc_param_audio_global_header
= 0;
182 static char *lavc_param_avopt
= NULL
;
184 #include "m_option.h"
186 #ifdef CONFIG_LIBAVCODEC
187 m_option_t lavcopts_conf
[]={
188 {"acodec", &lavc_param_acodec
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
189 {"abitrate", &lavc_param_abitrate
, CONF_TYPE_INT
, CONF_RANGE
, 1, 1000000, NULL
},
190 {"atag", &lavc_param_atag
, CONF_TYPE_INT
, CONF_RANGE
, 0, 0xffff, NULL
},
191 {"vcodec", &lavc_param_vcodec
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
192 {"vbitrate", &lavc_param_vbitrate
, CONF_TYPE_INT
, CONF_RANGE
, 4, 24000000, NULL
},
193 {"vratetol", &lavc_param_vrate_tolerance
, CONF_TYPE_INT
, CONF_RANGE
, 4, 24000000, NULL
},
194 {"vhq", &lavc_param_mb_decision
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
195 {"mbd", &lavc_param_mb_decision
, CONF_TYPE_INT
, CONF_RANGE
, 0, 9, NULL
},
196 {"v4mv", &lavc_param_v4mv
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
197 {"vme", &lavc_param_vme
, CONF_TYPE_INT
, CONF_RANGE
, 0, 8, NULL
},
198 {"vqscale", &lavc_param_vqscale
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 255.0, NULL
},
199 {"vqmin", &lavc_param_vqmin
, CONF_TYPE_INT
, CONF_RANGE
, 1, 31, NULL
},
200 {"vqmax", &lavc_param_vqmax
, CONF_TYPE_INT
, CONF_RANGE
, 1, 31, NULL
},
201 {"mbqmin", &lavc_param_mb_qmin
, CONF_TYPE_INT
, CONF_RANGE
, 1, 31, NULL
},
202 {"mbqmax", &lavc_param_mb_qmax
, CONF_TYPE_INT
, CONF_RANGE
, 1, 31, NULL
},
203 {"lmin", &lavc_param_lmin
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.01, 255.0, NULL
},
204 {"lmax", &lavc_param_lmax
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.01, 255.0, NULL
},
205 {"mblmin", &lavc_param_mb_lmin
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.01, 255.0, NULL
},
206 {"mblmax", &lavc_param_mb_lmax
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.01, 255.0, NULL
},
207 {"vqdiff", &lavc_param_vqdiff
, CONF_TYPE_INT
, CONF_RANGE
, 1, 31, NULL
},
208 {"vqcomp", &lavc_param_vqcompress
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 1.0, NULL
},
209 {"vqblur", &lavc_param_vqblur
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 1.0, NULL
},
210 {"vb_qfactor", &lavc_param_vb_qfactor
, CONF_TYPE_FLOAT
, CONF_RANGE
, -31.0, 31.0, NULL
},
211 {"vmax_b_frames", &lavc_param_vmax_b_frames
, CONF_TYPE_INT
, CONF_RANGE
, 0, FF_MAX_B_FRAMES
, NULL
},
212 {"vpass", &lavc_param_vpass
, CONF_TYPE_INT
, CONF_RANGE
, 0, 3, NULL
},
213 {"vrc_strategy", &lavc_param_vrc_strategy
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2, NULL
},
214 {"vb_strategy", &lavc_param_vb_strategy
, CONF_TYPE_INT
, CONF_RANGE
, 0, 10, NULL
},
215 {"vb_qoffset", &lavc_param_vb_qoffset
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 31.0, NULL
},
216 {"vlelim", &lavc_param_luma_elim_threshold
, CONF_TYPE_INT
, CONF_RANGE
, -99, 99, NULL
},
217 {"vcelim", &lavc_param_chroma_elim_threshold
, CONF_TYPE_INT
, CONF_RANGE
, -99, 99, NULL
},
218 {"vpsize", &lavc_param_packet_size
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100000000, NULL
},
219 {"vstrict", &lavc_param_strict
, CONF_TYPE_INT
, CONF_RANGE
, -99, 99, NULL
},
220 {"vdpart", &lavc_param_data_partitioning
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_PART
, NULL
},
221 {"keyint", &lavc_param_keyint
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
222 {"gray", &lavc_param_gray
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_PART
, NULL
},
223 {"mpeg_quant", &lavc_param_mpeg_quant
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
224 {"vi_qfactor", &lavc_param_vi_qfactor
, CONF_TYPE_FLOAT
, CONF_RANGE
, -31.0, 31.0, NULL
},
225 {"vi_qoffset", &lavc_param_vi_qoffset
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 31.0, NULL
},
226 {"vqsquish", &lavc_param_rc_qsquish
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 99.0, NULL
},
227 {"vqmod_amp", &lavc_param_rc_qmod_amp
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 99.0, NULL
},
228 {"vqmod_freq", &lavc_param_rc_qmod_freq
, CONF_TYPE_INT
, 0, 0, 0, NULL
},
229 {"vrc_eq", &lavc_param_rc_eq
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
230 {"vrc_override", &lavc_param_rc_override_string
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
231 {"vrc_maxrate", &lavc_param_rc_max_rate
, CONF_TYPE_INT
, CONF_RANGE
, 0, 24000000, NULL
},
232 {"vrc_minrate", &lavc_param_rc_min_rate
, CONF_TYPE_INT
, CONF_RANGE
, 0, 24000000, NULL
},
233 {"vrc_buf_size", &lavc_param_rc_buffer_size
, CONF_TYPE_INT
, CONF_RANGE
, 4, 24000000, NULL
},
234 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 99.0, NULL
},
235 {"vrc_init_cplx", &lavc_param_rc_initial_cplx
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 9999999.0, NULL
},
236 {"vrc_init_occupancy", &lavc_param_rc_initial_buffer_occupancy
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 1.0, NULL
},
237 {"vfdct", &lavc_param_fdct
, CONF_TYPE_INT
, CONF_RANGE
, 0, 10, NULL
},
238 {"aspect", &lavc_param_aspect
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
239 {"autoaspect", &lavc_param_autoaspect
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
240 {"lumi_mask", &lavc_param_lumi_masking
, CONF_TYPE_FLOAT
, CONF_RANGE
, -1.0, 1.0, NULL
},
241 {"tcplx_mask", &lavc_param_temporal_cplx_masking
, CONF_TYPE_FLOAT
, CONF_RANGE
, -1.0, 1.0, NULL
},
242 {"scplx_mask", &lavc_param_spatial_cplx_masking
, CONF_TYPE_FLOAT
, CONF_RANGE
, -1.0, 1.0, NULL
},
243 {"p_mask", &lavc_param_p_masking
, CONF_TYPE_FLOAT
, CONF_RANGE
, -1.0, 1.0, NULL
},
244 {"naq", &lavc_param_normalize_aqp
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
245 {"dark_mask", &lavc_param_dark_masking
, CONF_TYPE_FLOAT
, CONF_RANGE
, -1.0, 1.0, NULL
},
246 {"ildct", &lavc_param_interlaced_dct
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
247 {"idct", &lavc_param_idct
, CONF_TYPE_INT
, CONF_RANGE
, 0, 20, NULL
},
248 {"pred", &lavc_param_prediction_method
, CONF_TYPE_INT
, CONF_RANGE
, 0, 20, NULL
},
249 {"format", &lavc_param_format
, CONF_TYPE_IMGFMT
, 0, 0, 0, NULL
},
250 {"debug", &lavc_param_debug
, CONF_TYPE_INT
, CONF_RANGE
, 0, 100000000, NULL
},
251 {"psnr", &lavc_param_psnr
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_PSNR
, NULL
},
252 {"precmp", &lavc_param_me_pre_cmp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
253 {"cmp", &lavc_param_me_cmp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
254 {"subcmp", &lavc_param_me_sub_cmp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
255 {"mbcmp", &lavc_param_mb_cmp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
256 {"skipcmp", &lavc_param_skip_cmp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
258 {"ildctcmp", &lavc_param_ildct_cmp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
260 {"bit_exact", &lavc_param_bit_exact
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_BITEXACT
, NULL
},
261 {"predia", &lavc_param_pre_dia_size
, CONF_TYPE_INT
, CONF_RANGE
, -2000, 2000, NULL
},
262 {"dia", &lavc_param_dia_size
, CONF_TYPE_INT
, CONF_RANGE
, -2000, 2000, NULL
},
263 {"qpel", &lavc_param_qpel
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_QPEL
, NULL
},
264 {"trell", &lavc_param_trell
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
265 {"lowdelay", &lavc_param_lowdelay
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_LOW_DELAY
, NULL
},
266 {"last_pred", &lavc_param_last_pred
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
267 {"preme", &lavc_param_pre_me
, CONF_TYPE_INT
, CONF_RANGE
, 0, 2000, NULL
},
268 {"subq", &lavc_param_me_subpel_quality
, CONF_TYPE_INT
, CONF_RANGE
, 0, 8, NULL
},
269 {"me_range", &lavc_param_me_range
, CONF_TYPE_INT
, CONF_RANGE
, 0, 16000, NULL
},
270 #ifdef CODEC_FLAG_AC_PRED
271 {"aic", &lavc_param_aic
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_AC_PRED
, NULL
},
272 {"umv", &lavc_param_umv
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_H263P_UMV
, NULL
},
274 #ifdef CODEC_FLAG_H263P_AIV
275 {"aiv", &lavc_param_aiv
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_H263P_AIV
, NULL
},
277 #ifdef CODEC_FLAG_OBMC
278 {"obmc", &lavc_param_obmc
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_OBMC
, NULL
},
280 #ifdef CODEC_FLAG_LOOP_FILTER
281 {"loop", &lavc_param_loop
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_LOOP_FILTER
, NULL
},
283 {"ibias", &lavc_param_ibias
, CONF_TYPE_INT
, CONF_RANGE
, -512, 512, NULL
},
284 {"pbias", &lavc_param_pbias
, CONF_TYPE_INT
, CONF_RANGE
, -512, 512, NULL
},
285 {"coder", &lavc_param_coder
, CONF_TYPE_INT
, CONF_RANGE
, 0, 10, NULL
},
286 {"context", &lavc_param_context
, CONF_TYPE_INT
, CONF_RANGE
, 0, 10, NULL
},
287 {"intra_matrix", &lavc_param_intra_matrix
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
288 {"inter_matrix", &lavc_param_inter_matrix
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
289 {"cbp", &lavc_param_cbp
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_CBP_RD
, NULL
},
290 {"mv0", &lavc_param_mv0
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_MV0
, NULL
},
291 {"nr", &lavc_param_noise_reduction
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000000, NULL
},
292 #ifdef CODEC_FLAG_QP_RD
293 {"qprd", &lavc_param_qp_rd
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_QP_RD
, NULL
},
295 #ifdef CODEC_FLAG_H263P_SLICE_STRUCT
296 {"ss", &lavc_param_ss
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_H263P_SLICE_STRUCT
, NULL
},
298 #ifdef CODEC_FLAG_ALT_SCAN
299 {"alt", &lavc_param_alt
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_ALT_SCAN
, NULL
},
301 #ifdef CODEC_FLAG_INTERLACED_ME
302 {"ilme", &lavc_param_ilme
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_INTERLACED_ME
, NULL
},
304 #ifdef CODEC_FLAG_CLOSED_GOP
305 {"cgop", &lavc_param_closed_gop
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_CLOSED_GOP
, NULL
},
307 #ifdef CODEC_FLAG_GMC
308 {"gmc", &lavc_param_gmc
, CONF_TYPE_FLAG
, 0, 0, CODEC_FLAG_GMC
, NULL
},
310 {"dc", &lavc_param_dc_precision
, CONF_TYPE_INT
, CONF_RANGE
, 8, 11, NULL
},
311 {"border_mask", &lavc_param_border_masking
, CONF_TYPE_FLOAT
, CONF_RANGE
, 0.0, 1.0, NULL
},
312 {"inter_threshold", &lavc_param_inter_threshold
, CONF_TYPE_INT
, CONF_RANGE
, -1000000, 1000000, NULL
},
313 {"sc_threshold", &lavc_param_sc_threshold
, CONF_TYPE_INT
, CONF_RANGE
, -1000000000, 1000000000, NULL
},
314 {"top", &lavc_param_top
, CONF_TYPE_INT
, CONF_RANGE
, -1, 1, NULL
},
315 {"qns", &lavc_param_qns
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000000, NULL
},
316 {"nssew", &lavc_param_nssew
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000000, NULL
},
317 {"threads", &lavc_param_threads
, CONF_TYPE_INT
, CONF_RANGE
, 1, 8, NULL
},
318 {"turbo", &lavc_param_turbo
, CONF_TYPE_FLAG
, 0, 0, 1, NULL
},
319 {"skip_threshold", &lavc_param_skip_threshold
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000000, NULL
},
320 {"skip_factor", &lavc_param_skip_factor
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000000, NULL
},
321 {"skip_exp", &lavc_param_skip_exp
, CONF_TYPE_INT
, CONF_RANGE
, 0, 1000000, NULL
},
322 {"brd_scale", &lavc_param_brd_scale
, CONF_TYPE_INT
, CONF_RANGE
, 0, 10, NULL
},
323 {"bidir_refine", &lavc_param_bidir_refine
, CONF_TYPE_INT
, CONF_RANGE
, 0, 4, NULL
},
324 {"sc_factor", &lavc_param_sc_factor
, CONF_TYPE_INT
, CONF_RANGE
, 1, INT_MAX
, NULL
},
325 {"vglobal", &lavc_param_video_global_header
, CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
326 {"aglobal", &lavc_param_audio_global_header
, CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
327 {"mv0_threshold", &lavc_param_mv0_threshold
, CONF_TYPE_INT
, CONF_RANGE
, 0, INT_MAX
, NULL
},
328 {"refs", &lavc_param_refs
, CONF_TYPE_INT
, CONF_RANGE
, 1, 16, NULL
},
329 {"b_sensitivity", &lavc_param_b_sensitivity
, CONF_TYPE_INT
, CONF_RANGE
, 1, INT_MAX
, NULL
},
330 {"level", &lavc_param_level
, CONF_TYPE_INT
, CONF_RANGE
, INT_MIN
, INT_MAX
, NULL
},
331 {"o", &lavc_param_avopt
, CONF_TYPE_STRING
, 0, 0, 0, NULL
},
332 {NULL
, NULL
, 0, 0, 0, 0, NULL
}
338 AVCodecContext
*context
;
344 #define stats_file (vf->priv->stats_file)
345 #define mux_v (vf->priv->mux)
346 #define lavc_venc_context (vf->priv->context)
348 static int encode_frame(struct vf_instance
* vf
, AVFrame
*pic
, double pts
);
350 static int config(struct vf_instance
* vf
,
351 int width
, int height
, int d_width
, int d_height
,
352 unsigned int flags
, unsigned int outfmt
){
356 mux_v
->bih
->biWidth
=width
;
357 mux_v
->bih
->biHeight
=height
;
358 mux_v
->bih
->biSizeImage
=mux_v
->bih
->biWidth
*mux_v
->bih
->biHeight
*(mux_v
->bih
->biBitCount
/8);
360 mp_msg(MSGT_MENCODER
, MSGL_INFO
,"videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
361 mux_v
->bih
->biWidth
, mux_v
->bih
->biHeight
, mux_v
->bih
->biCompression
,
362 (char *)&mux_v
->bih
->biCompression
);
364 lavc_venc_context
->width
= width
;
365 lavc_venc_context
->height
= height
;
366 if (lavc_param_vbitrate
> 16000) /* != -1 */
367 lavc_venc_context
->bit_rate
= lavc_param_vbitrate
;
368 else if (lavc_param_vbitrate
>= 0) /* != -1 */
369 lavc_venc_context
->bit_rate
= lavc_param_vbitrate
*1000;
371 lavc_venc_context
->bit_rate
= 800000; /* default */
373 mux_v
->avg_rate
= lavc_venc_context
->bit_rate
;
375 lavc_venc_context
->bit_rate_tolerance
= lavc_param_vrate_tolerance
*1000;
376 lavc_venc_context
->time_base
= (AVRational
){mux_v
->h
.dwScale
, mux_v
->h
.dwRate
};
377 lavc_venc_context
->qmin
= lavc_param_vqmin
;
378 lavc_venc_context
->qmax
= lavc_param_vqmax
;
379 lavc_venc_context
->mb_qmin
= lavc_param_mb_qmin
;
380 lavc_venc_context
->mb_qmax
= lavc_param_mb_qmax
;
381 lavc_venc_context
->lmin
= (int)(FF_QP2LAMBDA
* lavc_param_lmin
+ 0.5);
382 lavc_venc_context
->lmax
= (int)(FF_QP2LAMBDA
* lavc_param_lmax
+ 0.5);
383 lavc_venc_context
->mb_lmin
= (int)(FF_QP2LAMBDA
* lavc_param_mb_lmin
+ 0.5);
384 lavc_venc_context
->mb_lmax
= (int)(FF_QP2LAMBDA
* lavc_param_mb_lmax
+ 0.5);
385 lavc_venc_context
->max_qdiff
= lavc_param_vqdiff
;
386 lavc_venc_context
->qcompress
= lavc_param_vqcompress
;
387 lavc_venc_context
->qblur
= lavc_param_vqblur
;
388 lavc_venc_context
->max_b_frames
= lavc_param_vmax_b_frames
;
389 lavc_venc_context
->b_quant_factor
= lavc_param_vb_qfactor
;
390 lavc_venc_context
->rc_strategy
= lavc_param_vrc_strategy
;
391 lavc_venc_context
->b_frame_strategy
= lavc_param_vb_strategy
;
392 lavc_venc_context
->b_quant_offset
= (int)(FF_QP2LAMBDA
* lavc_param_vb_qoffset
+ 0.5);
393 lavc_venc_context
->luma_elim_threshold
= lavc_param_luma_elim_threshold
;
394 lavc_venc_context
->chroma_elim_threshold
= lavc_param_chroma_elim_threshold
;
395 lavc_venc_context
->rtp_payload_size
= lavc_param_packet_size
;
396 lavc_venc_context
->strict_std_compliance
= lavc_param_strict
;
397 lavc_venc_context
->i_quant_factor
= lavc_param_vi_qfactor
;
398 lavc_venc_context
->i_quant_offset
= (int)(FF_QP2LAMBDA
* lavc_param_vi_qoffset
+ 0.5);
399 lavc_venc_context
->rc_qsquish
= lavc_param_rc_qsquish
;
400 lavc_venc_context
->rc_qmod_amp
= lavc_param_rc_qmod_amp
;
401 lavc_venc_context
->rc_qmod_freq
= lavc_param_rc_qmod_freq
;
402 lavc_venc_context
->rc_eq
= lavc_param_rc_eq
;
405 lavc_venc_context
->rc_max_rate
= lavc_param_rc_max_rate
*1000;
406 lavc_venc_context
->rc_min_rate
= lavc_param_rc_min_rate
*1000;
409 lavc_venc_context
->rc_buffer_size
= lavc_param_rc_buffer_size
*1000;
411 lavc_venc_context
->rc_initial_buffer_occupancy
=
412 lavc_venc_context
->rc_buffer_size
*
413 lavc_param_rc_initial_buffer_occupancy
;
414 lavc_venc_context
->rc_buffer_aggressivity
= lavc_param_rc_buffer_aggressivity
;
415 lavc_venc_context
->rc_initial_cplx
= lavc_param_rc_initial_cplx
;
416 lavc_venc_context
->debug
= lavc_param_debug
;
417 lavc_venc_context
->last_predictor_count
= lavc_param_last_pred
;
418 lavc_venc_context
->pre_me
= lavc_param_pre_me
;
419 lavc_venc_context
->me_pre_cmp
= lavc_param_me_pre_cmp
;
420 lavc_venc_context
->pre_dia_size
= lavc_param_pre_dia_size
;
421 lavc_venc_context
->me_subpel_quality
= lavc_param_me_subpel_quality
;
422 lavc_venc_context
->me_range
= lavc_param_me_range
;
423 lavc_venc_context
->intra_quant_bias
= lavc_param_ibias
;
424 lavc_venc_context
->inter_quant_bias
= lavc_param_pbias
;
425 lavc_venc_context
->coder_type
= lavc_param_coder
;
426 lavc_venc_context
->context_model
= lavc_param_context
;
427 lavc_venc_context
->scenechange_threshold
= lavc_param_sc_threshold
;
428 lavc_venc_context
->noise_reduction
= lavc_param_noise_reduction
;
429 lavc_venc_context
->quantizer_noise_shaping
= lavc_param_qns
;
430 lavc_venc_context
->inter_threshold
= lavc_param_inter_threshold
;
431 lavc_venc_context
->nsse_weight
= lavc_param_nssew
;
432 lavc_venc_context
->frame_skip_threshold
= lavc_param_skip_threshold
;
433 lavc_venc_context
->frame_skip_factor
= lavc_param_skip_factor
;
434 lavc_venc_context
->frame_skip_exp
= lavc_param_skip_exp
;
435 lavc_venc_context
->frame_skip_cmp
= lavc_param_skip_cmp
;
437 if (lavc_param_intra_matrix
)
441 lavc_venc_context
->intra_matrix
=
442 av_malloc(sizeof(*lavc_venc_context
->intra_matrix
)*64);
445 while ((tmp
= strsep(&lavc_param_intra_matrix
, ",")) && (i
< 64))
447 if (!tmp
|| (tmp
&& !strlen(tmp
)))
449 lavc_venc_context
->intra_matrix
[i
++] = atoi(tmp
);
453 av_freep(&lavc_venc_context
->intra_matrix
);
455 mp_msg(MSGT_MENCODER
, MSGL_V
, "Using user specified intra matrix\n");
457 if (lavc_param_inter_matrix
)
461 lavc_venc_context
->inter_matrix
=
462 av_malloc(sizeof(*lavc_venc_context
->inter_matrix
)*64);
465 while ((tmp
= strsep(&lavc_param_inter_matrix
, ",")) && (i
< 64))
467 if (!tmp
|| (tmp
&& !strlen(tmp
)))
469 lavc_venc_context
->inter_matrix
[i
++] = atoi(tmp
);
473 av_freep(&lavc_venc_context
->inter_matrix
);
475 mp_msg(MSGT_MENCODER
, MSGL_V
, "Using user specified inter matrix\n");
478 p
= lavc_param_rc_override_string
;
481 int e
=sscanf(p
, "%d,%d,%d", &start
, &end
, &q
);
483 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"error parsing vrc_q\n");
486 lavc_venc_context
->rc_override
=
487 realloc(lavc_venc_context
->rc_override
, sizeof(RcOverride
)*(i
+1));
488 lavc_venc_context
->rc_override
[i
].start_frame
= start
;
489 lavc_venc_context
->rc_override
[i
].end_frame
= end
;
491 lavc_venc_context
->rc_override
[i
].qscale
= q
;
492 lavc_venc_context
->rc_override
[i
].quality_factor
= 1.0;
495 lavc_venc_context
->rc_override
[i
].qscale
= 0;
496 lavc_venc_context
->rc_override
[i
].quality_factor
= -q
/100.0;
501 lavc_venc_context
->rc_override_count
=i
;
503 lavc_venc_context
->mpeg_quant
=lavc_param_mpeg_quant
;
505 lavc_venc_context
->dct_algo
= lavc_param_fdct
;
506 lavc_venc_context
->idct_algo
= lavc_param_idct
;
508 lavc_venc_context
->lumi_masking
= lavc_param_lumi_masking
;
509 lavc_venc_context
->temporal_cplx_masking
= lavc_param_temporal_cplx_masking
;
510 lavc_venc_context
->spatial_cplx_masking
= lavc_param_spatial_cplx_masking
;
511 lavc_venc_context
->p_masking
= lavc_param_p_masking
;
512 lavc_venc_context
->dark_masking
= lavc_param_dark_masking
;
513 lavc_venc_context
->border_masking
= lavc_param_border_masking
;
515 if (lavc_param_aspect
!= NULL
)
517 int par_width
, par_height
, e
;
520 e
= sscanf (lavc_param_aspect
, "%d/%d", &par_width
, &par_height
);
523 ratio
= (float)par_width
/ (float)par_height
;
525 e
= sscanf (lavc_param_aspect
, "%f", &ratio
);
528 if (e
&& ratio
> 0.1 && ratio
< 10.0) {
529 lavc_venc_context
->sample_aspect_ratio
= av_d2q(ratio
* height
/ width
, 255);
530 mp_dbg(MSGT_MENCODER
, MSGL_DBG2
, "sample_aspect_ratio: %d/%d\n",
531 lavc_venc_context
->sample_aspect_ratio
.num
,
532 lavc_venc_context
->sample_aspect_ratio
.den
);
533 mux_v
->aspect
= ratio
;
534 mp_dbg(MSGT_MENCODER
, MSGL_DBG2
, "aspect_ratio: %f\n", ratio
);
536 mp_dbg(MSGT_MENCODER
, MSGL_ERR
, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect
);
540 else if (lavc_param_autoaspect
) {
541 lavc_venc_context
->sample_aspect_ratio
= av_d2q((float)d_width
/d_height
*height
/ width
, 255);
542 mux_v
->aspect
= (float)d_width
/d_height
;
545 /* keyframe interval */
546 if (lavc_param_keyint
>= 0) /* != -1 */
547 lavc_venc_context
->gop_size
= lavc_param_keyint
;
549 lavc_venc_context
->gop_size
= 250; /* default */
551 lavc_venc_context
->flags
= 0;
552 if (lavc_param_mb_decision
)
554 mp_tmsg(MSGT_MENCODER
, MSGL_INFO
, "[VE_LAVC] High quality encoding selected (non-realtime)!\n");
555 lavc_venc_context
->mb_decision
= lavc_param_mb_decision
;
558 lavc_venc_context
->me_cmp
= lavc_param_me_cmp
;
559 lavc_venc_context
->me_sub_cmp
= lavc_param_me_sub_cmp
;
560 lavc_venc_context
->mb_cmp
= lavc_param_mb_cmp
;
562 lavc_venc_context
->ildct_cmp
= lavc_param_ildct_cmp
;
564 lavc_venc_context
->dia_size
= lavc_param_dia_size
;
565 lavc_venc_context
->flags
|= lavc_param_qpel
;
566 lavc_venc_context
->trellis
= lavc_param_trell
;
567 lavc_venc_context
->flags
|= lavc_param_lowdelay
;
568 lavc_venc_context
->flags
|= lavc_param_bit_exact
;
569 lavc_venc_context
->flags
|= lavc_param_aic
;
570 lavc_venc_context
->flags
|= lavc_param_aiv
;
571 lavc_venc_context
->flags
|= lavc_param_umv
;
572 lavc_venc_context
->flags
|= lavc_param_obmc
;
573 lavc_venc_context
->flags
|= lavc_param_loop
;
574 lavc_venc_context
->flags
|= lavc_param_v4mv
? CODEC_FLAG_4MV
: 0;
575 lavc_venc_context
->flags
|= lavc_param_data_partitioning
;
576 lavc_venc_context
->flags
|= lavc_param_cbp
;
577 lavc_venc_context
->flags
|= lavc_param_mv0
;
578 lavc_venc_context
->flags
|= lavc_param_qp_rd
;
579 lavc_venc_context
->flags
|= lavc_param_ss
;
580 lavc_venc_context
->flags
|= lavc_param_alt
;
581 lavc_venc_context
->flags
|= lavc_param_ilme
;
582 lavc_venc_context
->flags
|= lavc_param_gmc
;
583 #ifdef CODEC_FLAG_CLOSED_GOP
584 lavc_venc_context
->flags
|= lavc_param_closed_gop
;
586 if(lavc_param_gray
) lavc_venc_context
->flags
|= CODEC_FLAG_GRAY
;
588 if(lavc_param_normalize_aqp
) lavc_venc_context
->flags
|= CODEC_FLAG_NORMALIZE_AQP
;
589 if(lavc_param_interlaced_dct
) lavc_venc_context
->flags
|= CODEC_FLAG_INTERLACED_DCT
;
590 lavc_venc_context
->flags
|= lavc_param_psnr
;
591 lavc_venc_context
->intra_dc_precision
= lavc_param_dc_precision
- 8;
592 lavc_venc_context
->prediction_method
= lavc_param_prediction_method
;
593 lavc_venc_context
->brd_scale
= lavc_param_brd_scale
;
594 lavc_venc_context
->bidir_refine
= lavc_param_bidir_refine
;
595 lavc_venc_context
->scenechange_factor
= lavc_param_sc_factor
;
596 if((lavc_param_video_global_header
&1)
597 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
598 lavc_venc_context
->flags
|= CODEC_FLAG_GLOBAL_HEADER
;
600 if(lavc_param_video_global_header
&2){
601 lavc_venc_context
->flags2
|= CODEC_FLAG2_LOCAL_HEADER
;
603 lavc_venc_context
->mv0_threshold
= lavc_param_mv0_threshold
;
604 lavc_venc_context
->refs
= lavc_param_refs
;
605 lavc_venc_context
->b_sensitivity
= lavc_param_b_sensitivity
;
606 lavc_venc_context
->level
= lavc_param_level
;
608 if(lavc_param_avopt
){
609 if(parse_avopts(lavc_venc_context
, lavc_param_avopt
) < 0){
610 mp_msg(MSGT_MENCODER
,MSGL_ERR
, "Your options /%s/ look like gibberish to me pal\n", lavc_param_avopt
);
615 mux_v
->imgfmt
= lavc_param_format
;
616 lavc_venc_context
->pix_fmt
= imgfmt2pixfmt(lavc_param_format
);
617 if (lavc_venc_context
->pix_fmt
== PIX_FMT_NONE
)
621 /* lavc internal 2pass bitrate control */
622 switch(lavc_param_vpass
){
625 lavc_venc_context
->flags
|= CODEC_FLAG_PASS2
;
626 stats_file
= fopen(passtmpfile
, "rb");
627 if(stats_file
==NULL
){
628 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"2pass failed: filename=%s\n", passtmpfile
);
631 fseek(stats_file
, 0, SEEK_END
);
632 size
= ftell(stats_file
);
633 fseek(stats_file
, 0, SEEK_SET
);
635 lavc_venc_context
->stats_in
= av_malloc(size
+ 1);
636 lavc_venc_context
->stats_in
[size
]=0;
638 if(fread(lavc_venc_context
->stats_in
, size
, 1, stats_file
)<1){
639 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"2pass failed: reading from filename=%s\n", passtmpfile
);
642 if(lavc_param_vpass
== 2)
648 lavc_venc_context
->flags
|= CODEC_FLAG_PASS1
;
649 stats_file
= fopen(passtmpfile
, "wb");
650 if(stats_file
==NULL
){
651 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"2pass failed: filename=%s\n", passtmpfile
);
654 if(lavc_param_turbo
&& (lavc_param_vpass
== 1)) {
655 /* uses SAD comparison functions instead of other hungrier */
656 lavc_venc_context
->me_pre_cmp
= 0;
657 lavc_venc_context
->me_cmp
= 0;
658 lavc_venc_context
->me_sub_cmp
= 0;
659 lavc_venc_context
->mb_cmp
= 2;
661 /* Disables diamond motion estimation */
662 lavc_venc_context
->pre_dia_size
= 0;
663 lavc_venc_context
->dia_size
= 1;
665 lavc_venc_context
->quantizer_noise_shaping
= 0; // qns=0
666 lavc_venc_context
->noise_reduction
= 0; // nr=0
667 lavc_venc_context
->mb_decision
= 0; // mbd=0 ("realtime" encoding)
669 lavc_venc_context
->flags
&= ~CODEC_FLAG_QPEL
;
670 lavc_venc_context
->flags
&= ~CODEC_FLAG_4MV
;
671 lavc_venc_context
->trellis
= 0;
672 lavc_venc_context
->flags
&= ~CODEC_FLAG_CBP_RD
;
673 lavc_venc_context
->flags
&= ~CODEC_FLAG_QP_RD
;
674 lavc_venc_context
->flags
&= ~CODEC_FLAG_MV0
;
680 lavc_venc_context
->me_method
= ME_ZERO
+lavc_param_vme
;
682 /* fixed qscale :p */
683 if (lavc_param_vqscale
>= 0.0)
685 mp_tmsg(MSGT_MENCODER
, MSGL_INFO
, "[VE_LAVC] Using constant qscale = %f (VBR).\n", lavc_param_vqscale
);
686 lavc_venc_context
->flags
|= CODEC_FLAG_QSCALE
;
687 lavc_venc_context
->global_quality
=
688 vf
->priv
->pic
->quality
= (int)(FF_QP2LAMBDA
* lavc_param_vqscale
+ 0.5);
691 if(lavc_param_threads
> 1)
692 avcodec_thread_init(lavc_venc_context
, lavc_param_threads
);
694 if (avcodec_open(lavc_venc_context
, vf
->priv
->codec
) != 0) {
695 mp_tmsg(MSGT_MENCODER
,MSGL_ERR
,"Could not open codec.\n");
699 if (lavc_venc_context
->codec
->encode
== NULL
) {
700 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"avcodec init failed (ctx->codec->encode == NULL)!\n");
704 /* free second pass buffer, its not needed anymore */
705 av_freep(&lavc_venc_context
->stats_in
);
706 if(lavc_venc_context
->bits_per_coded_sample
)
707 mux_v
->bih
->biBitCount
= lavc_venc_context
->bits_per_coded_sample
;
708 if(lavc_venc_context
->extradata_size
){
709 mux_v
->bih
= realloc(mux_v
->bih
, sizeof(BITMAPINFOHEADER
) + lavc_venc_context
->extradata_size
);
710 memcpy(mux_v
->bih
+ 1, lavc_venc_context
->extradata
, lavc_venc_context
->extradata_size
);
711 mux_v
->bih
->biSize
= sizeof(BITMAPINFOHEADER
) + lavc_venc_context
->extradata_size
;
714 mux_v
->decoder_delay
= lavc_venc_context
->max_b_frames
? 1 : 0;
719 static int control(struct vf_instance
* vf
, int request
, void* data
){
722 case VFCTRL_FLUSH_FRAMES
:
723 if(vf
->priv
->codec
->capabilities
& CODEC_CAP_DELAY
)
724 while(encode_frame(vf
, NULL
, MP_NOPTS_VALUE
) > 0);
727 return CONTROL_UNKNOWN
;
731 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
736 if(lavc_param_format
== IMGFMT_YV12
)
737 return VFCAP_CSP_SUPPORTED
| VFCAP_ACCEPT_STRIDE
;
740 if(lavc_param_format
== IMGFMT_411P
)
741 return VFCAP_CSP_SUPPORTED
| VFCAP_ACCEPT_STRIDE
;
744 if(lavc_param_format
== IMGFMT_422P
)
745 return VFCAP_CSP_SUPPORTED
| VFCAP_ACCEPT_STRIDE
;
748 if(lavc_param_format
== IMGFMT_444P
)
749 return VFCAP_CSP_SUPPORTED
| VFCAP_ACCEPT_STRIDE
;
752 if(lavc_param_format
== IMGFMT_YVU9
)
753 return VFCAP_CSP_SUPPORTED
| VFCAP_ACCEPT_STRIDE
;
756 if(lavc_param_format
== IMGFMT_BGR32
)
757 return VFCAP_CSP_SUPPORTED
| VFCAP_ACCEPT_STRIDE
;
763 static double psnr(double d
){
764 if(d
==0) return INFINITY
;
765 return -10.0*log(d
)/log(10);
768 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
769 AVFrame
*pic
= vf
->priv
->pic
;
771 pic
->data
[0]=mpi
->planes
[0];
772 pic
->data
[1]=mpi
->planes
[1];
773 pic
->data
[2]=mpi
->planes
[2];
774 pic
->linesize
[0]=mpi
->stride
[0];
775 pic
->linesize
[1]=mpi
->stride
[1];
776 pic
->linesize
[2]=mpi
->stride
[2];
778 if(lavc_param_interlaced_dct
){
779 if((mpi
->fields
& MP_IMGFIELD_ORDERED
) && (mpi
->fields
& MP_IMGFIELD_INTERLACED
))
780 pic
->top_field_first
= !!(mpi
->fields
& MP_IMGFIELD_TOP_FIRST
);
782 pic
->top_field_first
= 1;
784 if(lavc_param_top
!=-1)
785 pic
->top_field_first
= lavc_param_top
;
788 return encode_frame(vf
, pic
, pts
) >= 0;
791 static int encode_frame(struct vf_instance
* vf
, AVFrame
*pic
, double pts
){
792 const char pict_type_char
[5]= {'?', 'I', 'P', 'B', 'S'};
796 if(pts
== MP_NOPTS_VALUE
)
797 pts
= lavc_venc_context
->frame_number
* av_q2d(lavc_venc_context
->time_base
);
801 pic
->opaque
= malloc(sizeof(pts
));
802 memcpy(pic
->opaque
, &pts
, sizeof(pts
));
804 if(pts
!= MP_NOPTS_VALUE
)
805 pic
->pts
= floor(pts
/ av_q2d(lavc_venc_context
->time_base
) + 0.5);
807 pic
->pts
= MP_NOPTS_VALUE
;
810 out_size
= avcodec_encode_video(lavc_venc_context
, mux_v
->buffer
, mux_v
->buffer_size
,
813 if(pts
!= MP_NOPTS_VALUE
)
814 dts
= pts
- lavc_venc_context
->delay
* av_q2d(lavc_venc_context
->time_base
);
818 pts
= lavc_venc_context
->coded_frame
->opaque
?
819 *(double*)lavc_venc_context
->coded_frame
->opaque
822 if(lavc_venc_context
->coded_frame
->pts
!= MP_NOPTS_VALUE
)
823 pts
= lavc_venc_context
->coded_frame
->pts
* av_q2d(lavc_venc_context
->time_base
);
826 assert(MP_NOPTS_VALUE
== AV_NOPTS_VALUE
);
828 //fprintf(stderr, "ve_lavc %f/%f\n", dts, pts);
829 if(out_size
== 0 && lavc_param_skip_threshold
==0 && lavc_param_skip_factor
==0){
830 ++mux_v
->encoder_delay
;
834 muxer_write_chunk(mux_v
,out_size
,lavc_venc_context
->coded_frame
->key_frame
?0x10:0,
836 free(lavc_venc_context
->coded_frame
->opaque
);
837 lavc_venc_context
->coded_frame
->opaque
= NULL
;
839 /* store psnr / pict size / type / qscale */
841 static FILE *fvstats
=NULL
;
843 double f
= lavc_venc_context
->width
*lavc_venc_context
->height
*255.0*255.0;
851 today
= localtime(&today2
);
852 sprintf(filename
, "psnr_%02d%02d%02d.log", today
->tm_hour
,
853 today
->tm_min
, today
->tm_sec
);
854 fvstats
= fopen(filename
,"w");
857 lavc_param_psnr
=0; // disable block
858 mp_msg(MSGT_MENCODER
,MSGL_ERR
,"Can't open %s for writing. Check its permissions.\n",filename
);
864 // average MB quantizer
865 q
= lavc_venc_context
->coded_frame
->qscale_table
;
868 int w
= (lavc_venc_context
->width
+15) >> 4;
869 int h
= (lavc_venc_context
->height
+15) >> 4;
870 for( y
= 0; y
< h
; y
++ ) {
871 for( x
= 0; x
< w
; x
++ )
872 quality
+= (double)*(q
+x
);
873 q
+= lavc_venc_context
->coded_frame
->qstride
;
877 quality
= lavc_venc_context
->coded_frame
->quality
/ (float)FF_QP2LAMBDA
;
879 fprintf(fvstats
, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
880 lavc_venc_context
->coded_frame
->coded_picture_number
,
883 psnr(lavc_venc_context
->coded_frame
->error
[0]/f
),
884 psnr(lavc_venc_context
->coded_frame
->error
[1]*4/f
),
885 psnr(lavc_venc_context
->coded_frame
->error
[2]*4/f
),
886 psnr((lavc_venc_context
->coded_frame
->error
[0]+lavc_venc_context
->coded_frame
->error
[1]+lavc_venc_context
->coded_frame
->error
[2])/(f
*1.5)),
887 pict_type_char
[lavc_venc_context
->coded_frame
->pict_type
]
890 /* store stats if there are any */
891 if(lavc_venc_context
->stats_out
&& stats_file
)
892 fprintf(stats_file
, "%s", lavc_venc_context
->stats_out
);
896 static void uninit(struct vf_instance
* vf
){
899 double f
= lavc_venc_context
->width
*lavc_venc_context
->height
*255.0*255.0;
900 f
*= lavc_venc_context
->coded_frame
->coded_picture_number
;
902 mp_msg(MSGT_MENCODER
, MSGL_INFO
, "PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
903 psnr(lavc_venc_context
->error
[0]/f
),
904 psnr(lavc_venc_context
->error
[1]*4/f
),
905 psnr(lavc_venc_context
->error
[2]*4/f
),
906 psnr((lavc_venc_context
->error
[0]+lavc_venc_context
->error
[1]+lavc_venc_context
->error
[2])/(f
*1.5))
910 av_freep(&lavc_venc_context
->intra_matrix
);
911 av_freep(&lavc_venc_context
->inter_matrix
);
913 if (lavc_venc_context
->codec
)
914 avcodec_close(lavc_venc_context
);
916 if(stats_file
) fclose(stats_file
);
918 /* free rc_override */
919 av_freep(&lavc_venc_context
->rc_override
);
921 av_freep(&vf
->priv
->context
);
924 //===========================================================================//
926 static int vf_open(vf_instance_t
*vf
, char* args
){
929 vf
->default_caps
=VFCAP_CONSTANT
;
931 vf
->query_format
=query_format
;
932 vf
->put_image
=put_image
;
933 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
934 memset(vf
->priv
,0,sizeof(struct vf_priv_s
));
935 vf
->priv
->mux
=(muxer_stream_t
*)args
;
937 /* XXX: hack: some of the MJPEG decoder DLL's needs exported huffman
938 table, so we define a zero-table, also lavc mjpeg encoder is putting
939 huffman tables into the stream, so no problem */
940 if (lavc_param_vcodec
&& !strcasecmp(lavc_param_vcodec
, "mjpeg"))
942 mux_v
->bih
=malloc(sizeof(BITMAPINFOHEADER
)+28);
943 memset(mux_v
->bih
, 0, sizeof(BITMAPINFOHEADER
)+28);
944 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
)+28;
946 else if (lavc_param_vcodec
&& (!strcasecmp(lavc_param_vcodec
, "huffyuv")
947 || !strcasecmp(lavc_param_vcodec
, "ffvhuff")))
949 /* XXX: hack: huffyuv needs to store huffman tables (allthough we dunno the size yet ...) */
950 mux_v
->bih
=malloc(sizeof(BITMAPINFOHEADER
)+1000);
951 memset(mux_v
->bih
, 0, sizeof(BITMAPINFOHEADER
)+1000);
952 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
)+1000;
954 else if (lavc_param_vcodec
&& !strcasecmp(lavc_param_vcodec
, "asv1"))
956 mux_v
->bih
=malloc(sizeof(BITMAPINFOHEADER
)+8);
957 memset(mux_v
->bih
, 0, sizeof(BITMAPINFOHEADER
)+8);
958 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
)+8;
960 else if (lavc_param_vcodec
&& !strcasecmp(lavc_param_vcodec
, "asv2"))
962 mux_v
->bih
=malloc(sizeof(BITMAPINFOHEADER
)+8);
963 memset(mux_v
->bih
, 0, sizeof(BITMAPINFOHEADER
)+8);
964 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
)+8;
966 else if (lavc_param_vcodec
&& !strcasecmp(lavc_param_vcodec
, "wmv2"))
968 mux_v
->bih
=malloc(sizeof(BITMAPINFOHEADER
)+4);
969 memset(mux_v
->bih
, 0, sizeof(BITMAPINFOHEADER
)+4);
970 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
)+4;
974 mux_v
->bih
=malloc(sizeof(BITMAPINFOHEADER
));
975 memset(mux_v
->bih
, 0, sizeof(BITMAPINFOHEADER
));
976 mux_v
->bih
->biSize
=sizeof(BITMAPINFOHEADER
);
978 mux_v
->bih
->biWidth
=0;
979 mux_v
->bih
->biHeight
=0;
980 mux_v
->bih
->biPlanes
=1;
981 mux_v
->bih
->biBitCount
=24;
982 if (!lavc_param_vcodec
)
984 printf("No libavcodec codec specified! It's required!\n");
988 if (!strcasecmp(lavc_param_vcodec
, "mpeg1") || !strcasecmp(lavc_param_vcodec
, "mpeg1video"))
989 mux_v
->bih
->biCompression
= mmioFOURCC('m', 'p', 'g', '1');
990 else if (!strcasecmp(lavc_param_vcodec
, "mpeg2") || !strcasecmp(lavc_param_vcodec
, "mpeg2video"))
991 mux_v
->bih
->biCompression
= mmioFOURCC('m', 'p', 'g', '2');
992 else if (!strcasecmp(lavc_param_vcodec
, "h263") || !strcasecmp(lavc_param_vcodec
, "h263p"))
993 mux_v
->bih
->biCompression
= mmioFOURCC('h', '2', '6', '3');
994 else if (!strcasecmp(lavc_param_vcodec
, "rv10"))
995 mux_v
->bih
->biCompression
= mmioFOURCC('R', 'V', '1', '0');
996 else if (!strcasecmp(lavc_param_vcodec
, "mjpeg"))
997 mux_v
->bih
->biCompression
= mmioFOURCC('M', 'J', 'P', 'G');
998 else if (!strcasecmp(lavc_param_vcodec
, "ljpeg"))
999 mux_v
->bih
->biCompression
= mmioFOURCC('L', 'J', 'P', 'G');
1000 else if (!strcasecmp(lavc_param_vcodec
, "mpeg4"))
1001 mux_v
->bih
->biCompression
= mmioFOURCC('F', 'M', 'P', '4');
1002 else if (!strcasecmp(lavc_param_vcodec
, "msmpeg4"))
1003 mux_v
->bih
->biCompression
= mmioFOURCC('d', 'i', 'v', '3');
1004 else if (!strcasecmp(lavc_param_vcodec
, "msmpeg4v2"))
1005 mux_v
->bih
->biCompression
= mmioFOURCC('M', 'P', '4', '2');
1006 else if (!strcasecmp(lavc_param_vcodec
, "wmv1"))
1007 mux_v
->bih
->biCompression
= mmioFOURCC('W', 'M', 'V', '1');
1008 else if (!strcasecmp(lavc_param_vcodec
, "wmv2"))
1009 mux_v
->bih
->biCompression
= mmioFOURCC('W', 'M', 'V', '2');
1010 else if (!strcasecmp(lavc_param_vcodec
, "huffyuv"))
1011 mux_v
->bih
->biCompression
= mmioFOURCC('H', 'F', 'Y', 'U');
1012 else if (!strcasecmp(lavc_param_vcodec
, "ffvhuff"))
1013 mux_v
->bih
->biCompression
= mmioFOURCC('F', 'F', 'V', 'H');
1014 else if (!strcasecmp(lavc_param_vcodec
, "asv1"))
1015 mux_v
->bih
->biCompression
= mmioFOURCC('A', 'S', 'V', '1');
1016 else if (!strcasecmp(lavc_param_vcodec
, "asv2"))
1017 mux_v
->bih
->biCompression
= mmioFOURCC('A', 'S', 'V', '2');
1018 else if (!strcasecmp(lavc_param_vcodec
, "ffv1"))
1019 mux_v
->bih
->biCompression
= mmioFOURCC('F', 'F', 'V', '1');
1020 else if (!strcasecmp(lavc_param_vcodec
, "snow"))
1021 mux_v
->bih
->biCompression
= mmioFOURCC('S', 'N', 'O', 'W');
1022 else if (!strcasecmp(lavc_param_vcodec
, "flv"))
1023 mux_v
->bih
->biCompression
= mmioFOURCC('F', 'L', 'V', '1');
1024 else if (!strcasecmp(lavc_param_vcodec
, "dvvideo"))
1025 mux_v
->bih
->biCompression
= mmioFOURCC('d', 'v', 's', 'd');
1026 else if (!strcasecmp(lavc_param_vcodec
, "libx264"))
1027 mux_v
->bih
->biCompression
= mmioFOURCC('h', '2', '6', '4');
1028 else if (!strcasecmp(lavc_param_vcodec
, "libschroedinger"))
1029 mux_v
->bih
->biCompression
= mmioFOURCC('d', 'r', 'a', 'c');
1030 else if (!strcasecmp(lavc_param_vcodec
, "libdirac"))
1031 mux_v
->bih
->biCompression
= mmioFOURCC('d', 'r', 'a', 'c');
1033 mux_v
->bih
->biCompression
= mmioFOURCC(lavc_param_vcodec
[0],
1034 lavc_param_vcodec
[1], lavc_param_vcodec
[2], lavc_param_vcodec
[3]); /* FIXME!!! */
1036 if (!avcodec_initialized
){
1038 avcodec_register_all();
1039 avcodec_initialized
=1;
1042 vf
->priv
->codec
= (AVCodec
*)avcodec_find_encoder_by_name(lavc_param_vcodec
);
1043 if (!vf
->priv
->codec
) {
1044 mp_tmsg(MSGT_MENCODER
,MSGL_ERR
,"Cannot find codec '%s' in libavcodec...\n", lavc_param_vcodec
);
1048 vf
->priv
->pic
= avcodec_alloc_frame();
1049 vf
->priv
->context
= avcodec_alloc_context();
1050 vf
->priv
->context
->codec_type
= CODEC_TYPE_VIDEO
;
1051 vf
->priv
->context
->codec_id
= vf
->priv
->codec
->id
;
1056 vf_info_t ve_info_lavc
= {
1057 "libavcodec encoder",
1059 "A'rpi, Alex, Michael",
1060 "for internal use by mencoder",
1064 //===========================================================================//