codec-cfg.c: Remove bogus printf argument
[mplayer.git] / libmpcodecs / ve_lavc.c
blob606de43a87023da7fde17678e5d47be66e9d5a75
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <limits.h>
6 #include <time.h>
7 #include <assert.h>
9 #if !defined(INFINITY) && defined(HUGE_VAL)
10 #define INFINITY HUGE_VAL
11 #endif
13 #include "config.h"
15 #include "mp_msg.h"
16 #include "help_mp.h"
17 #include "av_opts.h"
19 #include "codec-cfg.h"
20 #include "stream/stream.h"
21 #include "libmpdemux/demuxer.h"
22 #include "libmpdemux/stheader.h"
24 #include "libmpdemux/muxer.h"
26 #include "img_format.h"
27 #include "mp_image.h"
28 #include "vf.h"
30 extern char* passtmpfile;
32 //===========================================================================//
34 #include "libavcodec/avcodec.h"
36 extern int avcodec_initialized;
38 /* video options */
39 static char *lavc_param_vcodec = "mpeg4";
40 static int lavc_param_vbitrate = -1;
41 static int lavc_param_vrate_tolerance = 1000*8;
42 static int lavc_param_mb_decision = 0; /* default is realtime encoding */
43 static int lavc_param_v4mv = 0;
44 static int lavc_param_vme = 4;
45 static float lavc_param_vqscale = -1;
46 static int lavc_param_vqmin = 2;
47 static int lavc_param_vqmax = 31;
48 static int lavc_param_mb_qmin = 2;
49 static int lavc_param_mb_qmax = 31;
50 static float lavc_param_lmin = 2;
51 static float lavc_param_lmax = 31;
52 static float lavc_param_mb_lmin = 2;
53 static float lavc_param_mb_lmax = 31;
54 static int lavc_param_vqdiff = 3;
55 static float lavc_param_vqcompress = 0.5;
56 static float lavc_param_vqblur = 0.5;
57 static float lavc_param_vb_qfactor = 1.25;
58 static float lavc_param_vb_qoffset = 1.25;
59 static float lavc_param_vi_qfactor = 0.8;
60 static float lavc_param_vi_qoffset = 0.0;
61 static int lavc_param_vmax_b_frames = 0;
62 static int lavc_param_keyint = -1;
63 static int lavc_param_vpass = 0;
64 static int lavc_param_vrc_strategy = 0;
65 static int lavc_param_vb_strategy = 0;
66 static int lavc_param_luma_elim_threshold = 0;
67 static int lavc_param_chroma_elim_threshold = 0;
68 static int lavc_param_packet_size= 0;
69 static int lavc_param_strict= -1;
70 static int lavc_param_data_partitioning= 0;
71 static int lavc_param_gray=0;
72 static float lavc_param_rc_qsquish=1.0;
73 static float lavc_param_rc_qmod_amp=0;
74 static int lavc_param_rc_qmod_freq=0;
75 static char *lavc_param_rc_override_string=NULL;
76 static char *lavc_param_rc_eq="tex^qComp";
77 static int lavc_param_rc_buffer_size=0;
78 static float lavc_param_rc_buffer_aggressivity=1.0;
79 static int lavc_param_rc_max_rate=0;
80 static int lavc_param_rc_min_rate=0;
81 static float lavc_param_rc_initial_cplx=0;
82 static float lavc_param_rc_initial_buffer_occupancy=0.9;
83 static int lavc_param_mpeg_quant=0;
84 static int lavc_param_fdct=0;
85 static int lavc_param_idct=0;
86 static char* lavc_param_aspect = NULL;
87 static int lavc_param_autoaspect = 0;
88 static float lavc_param_lumi_masking= 0.0;
89 static float lavc_param_dark_masking= 0.0;
90 static float lavc_param_temporal_cplx_masking= 0.0;
91 static float lavc_param_spatial_cplx_masking= 0.0;
92 static float lavc_param_p_masking= 0.0;
93 static float lavc_param_border_masking= 0.0;
94 static int lavc_param_normalize_aqp= 0;
95 static int lavc_param_interlaced_dct= 0;
96 static int lavc_param_prediction_method= FF_PRED_LEFT;
97 static int lavc_param_format= IMGFMT_YV12;
98 static int lavc_param_debug= 0;
99 static int lavc_param_psnr= 0;
100 static int lavc_param_me_pre_cmp= 0;
101 static int lavc_param_me_cmp= 0;
102 static int lavc_param_me_sub_cmp= 0;
103 static int lavc_param_mb_cmp= 0;
104 #ifdef FF_CMP_VSAD
105 static int lavc_param_ildct_cmp= FF_CMP_VSAD;
106 #endif
107 static int lavc_param_pre_dia_size= 0;
108 static int lavc_param_dia_size= 0;
109 static int lavc_param_qpel= 0;
110 static int lavc_param_trell= 0;
111 static int lavc_param_lowdelay= 0;
112 static int lavc_param_bit_exact = 0;
113 static int lavc_param_aic= 0;
114 static int lavc_param_aiv= 0;
115 static int lavc_param_umv= 0;
116 static int lavc_param_gmc= 0;
117 static int lavc_param_obmc= 0;
118 static int lavc_param_loop= 0;
119 static int lavc_param_last_pred= 0;
120 static int lavc_param_pre_me= 1;
121 static int lavc_param_me_subpel_quality= 8;
122 static int lavc_param_me_range= 0;
123 static int lavc_param_ibias= FF_DEFAULT_QUANT_BIAS;
124 static int lavc_param_pbias= FF_DEFAULT_QUANT_BIAS;
125 static int lavc_param_coder= 0;
126 static int lavc_param_context= 0;
127 static char *lavc_param_intra_matrix = NULL;
128 static char *lavc_param_inter_matrix = NULL;
129 static int lavc_param_cbp= 0;
130 static int lavc_param_mv0= 0;
131 static int lavc_param_noise_reduction= 0;
132 static int lavc_param_qns= 0;
133 static int lavc_param_qp_rd= 0;
134 static int lavc_param_inter_threshold= 0;
135 static int lavc_param_sc_threshold= 0;
136 static int lavc_param_ss= 0;
137 static int lavc_param_top= -1;
138 static int lavc_param_alt= 0;
139 static int lavc_param_ilme= 0;
140 static int lavc_param_nssew= 8;
141 static int lavc_param_closed_gop = 0;
142 static int lavc_param_dc_precision = 8;
143 static int lavc_param_threads= 1;
144 static int lavc_param_turbo = 0;
145 static int lavc_param_skip_threshold=0;
146 static int lavc_param_skip_factor=0;
147 static int lavc_param_skip_exp=0;
148 static int lavc_param_skip_cmp=0;
149 static int lavc_param_brd_scale = 0;
150 static int lavc_param_bidir_refine = 0;
151 static int lavc_param_sc_factor = 1;
152 static int lavc_param_video_global_header= 0;
153 static int lavc_param_mv0_threshold = 256;
154 static int lavc_param_refs = 1;
155 static int lavc_param_b_sensitivity = 40;
156 static int lavc_param_level = FF_LEVEL_UNKNOWN;
158 char *lavc_param_acodec = "mp2";
159 int lavc_param_atag = 0;
160 int lavc_param_abitrate = 224;
161 int lavc_param_audio_global_header= 0;
162 static char *lavc_param_avopt = NULL;
164 #include "m_option.h"
166 #ifdef CONFIG_LIBAVCODEC
167 m_option_t lavcopts_conf[]={
168 {"acodec", &lavc_param_acodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
169 {"abitrate", &lavc_param_abitrate, CONF_TYPE_INT, CONF_RANGE, 1, 1000000, NULL},
170 {"atag", &lavc_param_atag, CONF_TYPE_INT, CONF_RANGE, 0, 0xffff, NULL},
171 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
172 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
173 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
174 {"vhq", &lavc_param_mb_decision, CONF_TYPE_FLAG, 0, 0, 1, NULL},
175 {"mbd", &lavc_param_mb_decision, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
176 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
177 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
178 {"vqscale", &lavc_param_vqscale, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 255.0, NULL},
179 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
180 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
181 {"mbqmin", &lavc_param_mb_qmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
182 {"mbqmax", &lavc_param_mb_qmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
183 {"lmin", &lavc_param_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
184 {"lmax", &lavc_param_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
185 {"mblmin", &lavc_param_mb_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
186 {"mblmax", &lavc_param_mb_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
187 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
188 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
189 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
190 {"vb_qfactor", &lavc_param_vb_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
191 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL},
192 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
193 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
194 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
195 {"vb_qoffset", &lavc_param_vb_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
196 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
197 {"vcelim", &lavc_param_chroma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
198 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
199 {"vstrict", &lavc_param_strict, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
200 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
201 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL},
202 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
203 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL},
204 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
205 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
206 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
207 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
208 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL},
209 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL},
210 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL},
211 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
212 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
213 {"vrc_buf_size", &lavc_param_rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
214 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
215 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL},
216 {"vrc_init_occupancy", &lavc_param_rc_initial_buffer_occupancy, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
217 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
218 {"aspect", &lavc_param_aspect, CONF_TYPE_STRING, 0, 0, 0, NULL},
219 {"autoaspect", &lavc_param_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL},
220 {"lumi_mask", &lavc_param_lumi_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
221 {"tcplx_mask", &lavc_param_temporal_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
222 {"scplx_mask", &lavc_param_spatial_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
223 {"p_mask", &lavc_param_p_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
224 {"naq", &lavc_param_normalize_aqp, CONF_TYPE_FLAG, 0, 0, 1, NULL},
225 {"dark_mask", &lavc_param_dark_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
226 {"ildct", &lavc_param_interlaced_dct, CONF_TYPE_FLAG, 0, 0, 1, NULL},
227 {"idct", &lavc_param_idct, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
228 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
229 {"format", &lavc_param_format, CONF_TYPE_IMGFMT, 0, 0, 0, NULL},
230 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
231 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL},
232 {"precmp", &lavc_param_me_pre_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
233 {"cmp", &lavc_param_me_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
234 {"subcmp", &lavc_param_me_sub_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
235 {"mbcmp", &lavc_param_mb_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
236 {"skipcmp", &lavc_param_skip_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
237 #ifdef FF_CMP_VSAD
238 {"ildctcmp", &lavc_param_ildct_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
239 #endif
240 {"bit_exact", &lavc_param_bit_exact, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_BITEXACT, NULL},
241 {"predia", &lavc_param_pre_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
242 {"dia", &lavc_param_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
243 {"qpel", &lavc_param_qpel, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QPEL, NULL},
244 {"trell", &lavc_param_trell, CONF_TYPE_FLAG, 0, 0, 1, NULL},
245 {"lowdelay", &lavc_param_lowdelay, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOW_DELAY, NULL},
246 {"last_pred", &lavc_param_last_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
247 {"preme", &lavc_param_pre_me, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
248 {"subq", &lavc_param_me_subpel_quality, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
249 {"me_range", &lavc_param_me_range, CONF_TYPE_INT, CONF_RANGE, 0, 16000, NULL},
250 #ifdef CODEC_FLAG_AC_PRED
251 {"aic", &lavc_param_aic, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_AC_PRED, NULL},
252 {"umv", &lavc_param_umv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_UMV, NULL},
253 #endif
254 #ifdef CODEC_FLAG_H263P_AIV
255 {"aiv", &lavc_param_aiv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIV, NULL},
256 #endif
257 #ifdef CODEC_FLAG_OBMC
258 {"obmc", &lavc_param_obmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_OBMC, NULL},
259 #endif
260 #ifdef CODEC_FLAG_LOOP_FILTER
261 {"loop", &lavc_param_loop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOOP_FILTER, NULL},
262 #endif
263 {"ibias", &lavc_param_ibias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
264 {"pbias", &lavc_param_pbias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
265 {"coder", &lavc_param_coder, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
266 {"context", &lavc_param_context, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
267 {"intra_matrix", &lavc_param_intra_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
268 {"inter_matrix", &lavc_param_inter_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
269 {"cbp", &lavc_param_cbp, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CBP_RD, NULL},
270 {"mv0", &lavc_param_mv0, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_MV0, NULL},
271 {"nr", &lavc_param_noise_reduction, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
272 #ifdef CODEC_FLAG_QP_RD
273 {"qprd", &lavc_param_qp_rd, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QP_RD, NULL},
274 #endif
275 #ifdef CODEC_FLAG_H263P_SLICE_STRUCT
276 {"ss", &lavc_param_ss, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_SLICE_STRUCT, NULL},
277 #endif
278 #ifdef CODEC_FLAG_ALT_SCAN
279 {"alt", &lavc_param_alt, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_ALT_SCAN, NULL},
280 #endif
281 #ifdef CODEC_FLAG_INTERLACED_ME
282 {"ilme", &lavc_param_ilme, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_INTERLACED_ME, NULL},
283 #endif
284 #ifdef CODEC_FLAG_CLOSED_GOP
285 {"cgop", &lavc_param_closed_gop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CLOSED_GOP, NULL},
286 #endif
287 #ifdef CODEC_FLAG_GMC
288 {"gmc", &lavc_param_gmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_GMC, NULL},
289 #endif
290 {"dc", &lavc_param_dc_precision, CONF_TYPE_INT, CONF_RANGE, 8, 11, NULL},
291 {"border_mask", &lavc_param_border_masking, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
292 {"inter_threshold", &lavc_param_inter_threshold, CONF_TYPE_INT, CONF_RANGE, -1000000, 1000000, NULL},
293 {"sc_threshold", &lavc_param_sc_threshold, CONF_TYPE_INT, CONF_RANGE, -1000000000, 1000000000, NULL},
294 {"top", &lavc_param_top, CONF_TYPE_INT, CONF_RANGE, -1, 1, NULL},
295 {"qns", &lavc_param_qns, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
296 {"nssew", &lavc_param_nssew, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
297 {"threads", &lavc_param_threads, CONF_TYPE_INT, CONF_RANGE, 1, 8, NULL},
298 {"turbo", &lavc_param_turbo, CONF_TYPE_FLAG, 0, 0, 1, NULL},
299 {"skip_threshold", &lavc_param_skip_threshold, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
300 {"skip_factor", &lavc_param_skip_factor, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
301 {"skip_exp", &lavc_param_skip_exp, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
302 {"brd_scale", &lavc_param_brd_scale, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
303 {"bidir_refine", &lavc_param_bidir_refine, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL},
304 {"sc_factor", &lavc_param_sc_factor, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
305 {"vglobal", &lavc_param_video_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
306 {"aglobal", &lavc_param_audio_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
307 {"mv0_threshold", &lavc_param_mv0_threshold, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
308 {"refs", &lavc_param_refs, CONF_TYPE_INT, CONF_RANGE, 1, 16, NULL},
309 {"b_sensitivity", &lavc_param_b_sensitivity, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
310 {"level", &lavc_param_level, CONF_TYPE_INT, CONF_RANGE, INT_MIN, INT_MAX, NULL},
311 {"o", &lavc_param_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
312 {NULL, NULL, 0, 0, 0, 0, NULL}
314 #endif
316 struct vf_priv_s {
317 muxer_stream_t* mux;
318 AVCodecContext *context;
319 AVFrame *pic;
320 AVCodec *codec;
321 FILE *stats_file;
324 #define stats_file (vf->priv->stats_file)
325 #define mux_v (vf->priv->mux)
326 #define lavc_venc_context (vf->priv->context)
328 static int encode_frame(struct vf_instance* vf, AVFrame *pic, double pts);
330 static int config(struct vf_instance* vf,
331 int width, int height, int d_width, int d_height,
332 unsigned int flags, unsigned int outfmt){
333 int size, i;
334 void *p;
336 mux_v->bih->biWidth=width;
337 mux_v->bih->biHeight=height;
338 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
340 mp_msg(MSGT_MENCODER, MSGL_INFO,"videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
341 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression,
342 (char *)&mux_v->bih->biCompression);
344 lavc_venc_context->width = width;
345 lavc_venc_context->height = height;
346 if (lavc_param_vbitrate > 16000) /* != -1 */
347 lavc_venc_context->bit_rate = lavc_param_vbitrate;
348 else if (lavc_param_vbitrate >= 0) /* != -1 */
349 lavc_venc_context->bit_rate = lavc_param_vbitrate*1000;
350 else
351 lavc_venc_context->bit_rate = 800000; /* default */
353 mux_v->avg_rate= lavc_venc_context->bit_rate;
355 lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000;
356 lavc_venc_context->time_base= (AVRational){mux_v->h.dwScale, mux_v->h.dwRate};
357 lavc_venc_context->qmin= lavc_param_vqmin;
358 lavc_venc_context->qmax= lavc_param_vqmax;
359 lavc_venc_context->mb_qmin= lavc_param_mb_qmin;
360 lavc_venc_context->mb_qmax= lavc_param_mb_qmax;
361 lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5);
362 lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5);
363 lavc_venc_context->mb_lmin= (int)(FF_QP2LAMBDA * lavc_param_mb_lmin + 0.5);
364 lavc_venc_context->mb_lmax= (int)(FF_QP2LAMBDA * lavc_param_mb_lmax + 0.5);
365 lavc_venc_context->max_qdiff= lavc_param_vqdiff;
366 lavc_venc_context->qcompress= lavc_param_vqcompress;
367 lavc_venc_context->qblur= lavc_param_vqblur;
368 lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames;
369 lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor;
370 lavc_venc_context->rc_strategy= lavc_param_vrc_strategy;
371 lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy;
372 lavc_venc_context->b_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vb_qoffset + 0.5);
373 lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold;
374 lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold;
375 lavc_venc_context->rtp_payload_size= lavc_param_packet_size;
376 lavc_venc_context->strict_std_compliance= lavc_param_strict;
377 lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor;
378 lavc_venc_context->i_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vi_qoffset + 0.5);
379 lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish;
380 lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp;
381 lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq;
382 lavc_venc_context->rc_eq= lavc_param_rc_eq;
384 mux_v->max_rate=
385 lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000;
386 lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000;
388 mux_v->vbv_size=
389 lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000;
391 lavc_venc_context->rc_initial_buffer_occupancy=
392 lavc_venc_context->rc_buffer_size *
393 lavc_param_rc_initial_buffer_occupancy;
394 lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity;
395 lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx;
396 lavc_venc_context->debug= lavc_param_debug;
397 lavc_venc_context->last_predictor_count= lavc_param_last_pred;
398 lavc_venc_context->pre_me= lavc_param_pre_me;
399 lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp;
400 lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size;
401 lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality;
402 lavc_venc_context->me_range= lavc_param_me_range;
403 lavc_venc_context->intra_quant_bias= lavc_param_ibias;
404 lavc_venc_context->inter_quant_bias= lavc_param_pbias;
405 lavc_venc_context->coder_type= lavc_param_coder;
406 lavc_venc_context->context_model= lavc_param_context;
407 lavc_venc_context->scenechange_threshold= lavc_param_sc_threshold;
408 lavc_venc_context->noise_reduction= lavc_param_noise_reduction;
409 lavc_venc_context->quantizer_noise_shaping= lavc_param_qns;
410 lavc_venc_context->inter_threshold= lavc_param_inter_threshold;
411 lavc_venc_context->nsse_weight= lavc_param_nssew;
412 lavc_venc_context->frame_skip_threshold= lavc_param_skip_threshold;
413 lavc_venc_context->frame_skip_factor= lavc_param_skip_factor;
414 lavc_venc_context->frame_skip_exp= lavc_param_skip_exp;
415 lavc_venc_context->frame_skip_cmp= lavc_param_skip_cmp;
417 if (lavc_param_intra_matrix)
419 char *tmp;
421 lavc_venc_context->intra_matrix =
422 av_malloc(sizeof(*lavc_venc_context->intra_matrix)*64);
424 i = 0;
425 while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64))
427 if (!tmp || (tmp && !strlen(tmp)))
428 break;
429 lavc_venc_context->intra_matrix[i++] = atoi(tmp);
432 if (i != 64)
433 av_freep(&lavc_venc_context->intra_matrix);
434 else
435 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n");
437 if (lavc_param_inter_matrix)
439 char *tmp;
441 lavc_venc_context->inter_matrix =
442 av_malloc(sizeof(*lavc_venc_context->inter_matrix)*64);
444 i = 0;
445 while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64))
447 if (!tmp || (tmp && !strlen(tmp)))
448 break;
449 lavc_venc_context->inter_matrix[i++] = atoi(tmp);
452 if (i != 64)
453 av_freep(&lavc_venc_context->inter_matrix);
454 else
455 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n");
458 p= lavc_param_rc_override_string;
459 for(i=0; p; i++){
460 int start, end, q;
461 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
462 if(e!=3){
463 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n");
464 return 0;
466 lavc_venc_context->rc_override=
467 realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1));
468 lavc_venc_context->rc_override[i].start_frame= start;
469 lavc_venc_context->rc_override[i].end_frame = end;
470 if(q>0){
471 lavc_venc_context->rc_override[i].qscale= q;
472 lavc_venc_context->rc_override[i].quality_factor= 1.0;
474 else{
475 lavc_venc_context->rc_override[i].qscale= 0;
476 lavc_venc_context->rc_override[i].quality_factor= -q/100.0;
478 p= strchr(p, '/');
479 if(p) p++;
481 lavc_venc_context->rc_override_count=i;
483 lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant;
485 lavc_venc_context->dct_algo= lavc_param_fdct;
486 lavc_venc_context->idct_algo= lavc_param_idct;
488 lavc_venc_context->lumi_masking= lavc_param_lumi_masking;
489 lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking;
490 lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking;
491 lavc_venc_context->p_masking= lavc_param_p_masking;
492 lavc_venc_context->dark_masking= lavc_param_dark_masking;
493 lavc_venc_context->border_masking = lavc_param_border_masking;
495 if (lavc_param_aspect != NULL)
497 int par_width, par_height, e;
498 float ratio=0;
500 e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height);
501 if(e==2){
502 if(par_height)
503 ratio= (float)par_width / (float)par_height;
504 }else{
505 e= sscanf (lavc_param_aspect, "%f", &ratio);
508 if (e && ratio > 0.1 && ratio < 10.0) {
509 lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255);
510 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n",
511 lavc_venc_context->sample_aspect_ratio.num,
512 lavc_venc_context->sample_aspect_ratio.den);
513 mux_v->aspect = ratio;
514 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio);
515 } else {
516 mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect);
517 return 0;
520 else if (lavc_param_autoaspect) {
521 lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255);
522 mux_v->aspect = (float)d_width/d_height;
525 /* keyframe interval */
526 if (lavc_param_keyint >= 0) /* != -1 */
527 lavc_venc_context->gop_size = lavc_param_keyint;
528 else
529 lavc_venc_context->gop_size = 250; /* default */
531 lavc_venc_context->flags = 0;
532 if (lavc_param_mb_decision)
534 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_HighQualityEncodingSelected);
535 lavc_venc_context->mb_decision= lavc_param_mb_decision;
538 lavc_venc_context->me_cmp= lavc_param_me_cmp;
539 lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp;
540 lavc_venc_context->mb_cmp= lavc_param_mb_cmp;
541 #ifdef FF_CMP_VSAD
542 lavc_venc_context->ildct_cmp= lavc_param_ildct_cmp;
543 #endif
544 lavc_venc_context->dia_size= lavc_param_dia_size;
545 lavc_venc_context->flags|= lavc_param_qpel;
546 lavc_venc_context->trellis = lavc_param_trell;
547 lavc_venc_context->flags|= lavc_param_lowdelay;
548 lavc_venc_context->flags|= lavc_param_bit_exact;
549 lavc_venc_context->flags|= lavc_param_aic;
550 lavc_venc_context->flags|= lavc_param_aiv;
551 lavc_venc_context->flags|= lavc_param_umv;
552 lavc_venc_context->flags|= lavc_param_obmc;
553 lavc_venc_context->flags|= lavc_param_loop;
554 lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0;
555 lavc_venc_context->flags|= lavc_param_data_partitioning;
556 lavc_venc_context->flags|= lavc_param_cbp;
557 lavc_venc_context->flags|= lavc_param_mv0;
558 lavc_venc_context->flags|= lavc_param_qp_rd;
559 lavc_venc_context->flags|= lavc_param_ss;
560 lavc_venc_context->flags|= lavc_param_alt;
561 lavc_venc_context->flags|= lavc_param_ilme;
562 lavc_venc_context->flags|= lavc_param_gmc;
563 #ifdef CODEC_FLAG_CLOSED_GOP
564 lavc_venc_context->flags|= lavc_param_closed_gop;
565 #endif
566 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY;
568 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP;
569 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT;
570 lavc_venc_context->flags|= lavc_param_psnr;
571 lavc_venc_context->intra_dc_precision = lavc_param_dc_precision - 8;
572 lavc_venc_context->prediction_method= lavc_param_prediction_method;
573 lavc_venc_context->brd_scale = lavc_param_brd_scale;
574 lavc_venc_context->bidir_refine = lavc_param_bidir_refine;
575 lavc_venc_context->scenechange_factor = lavc_param_sc_factor;
576 if((lavc_param_video_global_header&1)
577 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
578 lavc_venc_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
580 if(lavc_param_video_global_header&2){
581 lavc_venc_context->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
583 lavc_venc_context->mv0_threshold = lavc_param_mv0_threshold;
584 lavc_venc_context->refs = lavc_param_refs;
585 lavc_venc_context->b_sensitivity = lavc_param_b_sensitivity;
586 lavc_venc_context->level = lavc_param_level;
588 if(lavc_param_avopt){
589 if(parse_avopts(lavc_venc_context, lavc_param_avopt) < 0){
590 mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_avopt);
591 return 0;
595 mux_v->imgfmt = lavc_param_format;
596 switch(lavc_param_format)
598 case IMGFMT_YV12:
599 lavc_venc_context->pix_fmt = PIX_FMT_YUV420P;
600 break;
601 case IMGFMT_422P:
602 lavc_venc_context->pix_fmt = PIX_FMT_YUV422P;
603 break;
604 case IMGFMT_444P:
605 lavc_venc_context->pix_fmt = PIX_FMT_YUV444P;
606 break;
607 case IMGFMT_411P:
608 lavc_venc_context->pix_fmt = PIX_FMT_YUV411P;
609 break;
610 case IMGFMT_YVU9:
611 lavc_venc_context->pix_fmt = PIX_FMT_YUV410P;
612 break;
613 case IMGFMT_BGR32:
614 lavc_venc_context->pix_fmt = PIX_FMT_RGB32;
615 break;
616 default:
617 mp_msg(MSGT_MENCODER,MSGL_ERR,"%s is not a supported format\n", vo_format_name(lavc_param_format));
618 return 0;
621 if(!stats_file) {
622 /* lavc internal 2pass bitrate control */
623 switch(lavc_param_vpass){
624 case 2:
625 case 3:
626 lavc_venc_context->flags|= CODEC_FLAG_PASS2;
627 stats_file= fopen(passtmpfile, "rb");
628 if(stats_file==NULL){
629 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
630 return 0;
632 fseek(stats_file, 0, SEEK_END);
633 size= ftell(stats_file);
634 fseek(stats_file, 0, SEEK_SET);
636 lavc_venc_context->stats_in= av_malloc(size + 1);
637 lavc_venc_context->stats_in[size]=0;
639 if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){
640 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile);
641 return 0;
643 if(lavc_param_vpass == 2)
644 break;
645 else
646 fclose(stats_file);
647 /* fall through */
648 case 1:
649 lavc_venc_context->flags|= CODEC_FLAG_PASS1;
650 stats_file= fopen(passtmpfile, "wb");
651 if(stats_file==NULL){
652 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
653 return 0;
655 if(lavc_param_turbo && (lavc_param_vpass == 1)) {
656 /* uses SAD comparison functions instead of other hungrier */
657 lavc_venc_context->me_pre_cmp = 0;
658 lavc_venc_context->me_cmp = 0;
659 lavc_venc_context->me_sub_cmp = 0;
660 lavc_venc_context->mb_cmp = 2;
662 /* Disables diamond motion estimation */
663 lavc_venc_context->pre_dia_size = 0;
664 lavc_venc_context->dia_size = 1;
666 lavc_venc_context->quantizer_noise_shaping = 0; // qns=0
667 lavc_venc_context->noise_reduction = 0; // nr=0
668 lavc_venc_context->mb_decision = 0; // mbd=0 ("realtime" encoding)
670 lavc_venc_context->flags &= ~CODEC_FLAG_QPEL;
671 lavc_venc_context->flags &= ~CODEC_FLAG_4MV;
672 lavc_venc_context->trellis = 0;
673 lavc_venc_context->flags &= ~CODEC_FLAG_CBP_RD;
674 lavc_venc_context->flags &= ~CODEC_FLAG_QP_RD;
675 lavc_venc_context->flags &= ~CODEC_FLAG_MV0;
677 break;
681 lavc_venc_context->me_method = ME_ZERO+lavc_param_vme;
683 /* fixed qscale :p */
684 if (lavc_param_vqscale >= 0.0)
686 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_UsingConstantQscale, lavc_param_vqscale);
687 lavc_venc_context->flags |= CODEC_FLAG_QSCALE;
688 lavc_venc_context->global_quality=
689 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5);
692 if(lavc_param_threads > 1)
693 avcodec_thread_init(lavc_venc_context, lavc_param_threads);
695 if (avcodec_open(lavc_venc_context, vf->priv->codec) != 0) {
696 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
697 return 0;
700 if (lavc_venc_context->codec->encode == NULL) {
701 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
702 return 0;
705 /* free second pass buffer, its not needed anymore */
706 av_freep(&lavc_venc_context->stats_in);
707 if(lavc_venc_context->bits_per_coded_sample)
708 mux_v->bih->biBitCount= lavc_venc_context->bits_per_coded_sample;
709 if(lavc_venc_context->extradata_size){
710 mux_v->bih= realloc(mux_v->bih, sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size);
711 memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size);
712 mux_v->bih->biSize= sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size;
715 mux_v->decoder_delay = lavc_venc_context->max_b_frames ? 1 : 0;
717 return 1;
720 static int control(struct vf_instance* vf, int request, void* data){
722 switch(request){
723 case VFCTRL_FLUSH_FRAMES:
724 if(vf->priv->codec->capabilities & CODEC_CAP_DELAY)
725 while(encode_frame(vf, NULL, MP_NOPTS_VALUE) > 0);
726 return CONTROL_TRUE;
727 default:
728 return CONTROL_UNKNOWN;
732 static int query_format(struct vf_instance* vf, unsigned int fmt){
733 switch(fmt){
734 case IMGFMT_YV12:
735 case IMGFMT_IYUV:
736 case IMGFMT_I420:
737 if(lavc_param_format == IMGFMT_YV12)
738 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
739 break;
740 case IMGFMT_411P:
741 if(lavc_param_format == IMGFMT_411P)
742 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
743 break;
744 case IMGFMT_422P:
745 if(lavc_param_format == IMGFMT_422P)
746 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
747 break;
748 case IMGFMT_444P:
749 if(lavc_param_format == IMGFMT_444P)
750 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
751 break;
752 case IMGFMT_YVU9:
753 if(lavc_param_format == IMGFMT_YVU9)
754 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
755 break;
756 case IMGFMT_BGR32:
757 if(lavc_param_format == IMGFMT_BGR32)
758 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
759 break;
761 return 0;
764 static double psnr(double d){
765 if(d==0) return INFINITY;
766 return -10.0*log(d)/log(10);
769 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){
770 AVFrame *pic= vf->priv->pic;
772 pic->data[0]=mpi->planes[0];
773 pic->data[1]=mpi->planes[1];
774 pic->data[2]=mpi->planes[2];
775 pic->linesize[0]=mpi->stride[0];
776 pic->linesize[1]=mpi->stride[1];
777 pic->linesize[2]=mpi->stride[2];
779 if(lavc_param_interlaced_dct){
780 if((mpi->fields & MP_IMGFIELD_ORDERED) && (mpi->fields & MP_IMGFIELD_INTERLACED))
781 pic->top_field_first= !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
782 else
783 pic->top_field_first= 1;
785 if(lavc_param_top!=-1)
786 pic->top_field_first= lavc_param_top;
789 return encode_frame(vf, pic, pts) >= 0;
792 static int encode_frame(struct vf_instance* vf, AVFrame *pic, double pts){
793 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
794 int out_size;
795 double dts;
797 if(pts == MP_NOPTS_VALUE)
798 pts= lavc_venc_context->frame_number * av_q2d(lavc_venc_context->time_base);
800 if(pic){
801 #if 0
802 pic->opaque= malloc(sizeof(pts));
803 memcpy(pic->opaque, &pts, sizeof(pts));
804 #else
805 if(pts != MP_NOPTS_VALUE)
806 pic->pts= floor(pts / av_q2d(lavc_venc_context->time_base) + 0.5);
807 else
808 pic->pts= MP_NOPTS_VALUE;
809 #endif
811 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
812 pic);
814 if(pts != MP_NOPTS_VALUE)
815 dts= pts - lavc_venc_context->delay * av_q2d(lavc_venc_context->time_base);
816 else
817 dts= MP_NOPTS_VALUE;
818 #if 0
819 pts= lavc_venc_context->coded_frame->opaque ?
820 *(double*)lavc_venc_context->coded_frame->opaque
821 : MP_NOPTS_VALUE;
822 #else
823 if(lavc_venc_context->coded_frame->pts != MP_NOPTS_VALUE)
824 pts= lavc_venc_context->coded_frame->pts * av_q2d(lavc_venc_context->time_base);
825 else
826 pts= MP_NOPTS_VALUE;
827 assert(MP_NOPTS_VALUE == AV_NOPTS_VALUE);
828 #endif
829 //fprintf(stderr, "ve_lavc %f/%f\n", dts, pts);
830 if(out_size == 0 && lavc_param_skip_threshold==0 && lavc_param_skip_factor==0){
831 ++mux_v->encoder_delay;
832 return 0;
835 muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0,
836 dts, pts);
837 free(lavc_venc_context->coded_frame->opaque);
838 lavc_venc_context->coded_frame->opaque= NULL;
840 /* store psnr / pict size / type / qscale */
841 if(lavc_param_psnr){
842 static FILE *fvstats=NULL;
843 char filename[20];
844 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
845 double quality=0.0;
846 int8_t *q;
848 if(!fvstats) {
849 time_t today2;
850 struct tm *today;
851 today2 = time(NULL);
852 today = localtime(&today2);
853 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour,
854 today->tm_min, today->tm_sec);
855 fvstats = fopen(filename,"w");
856 if(!fvstats) {
857 perror("fopen");
858 lavc_param_psnr=0; // disable block
859 mp_msg(MSGT_MENCODER,MSGL_ERR,"Can't open %s for writing. Check its permissions.\n",filename);
860 return -1;
861 /*exit(1);*/
865 // average MB quantizer
866 q = lavc_venc_context->coded_frame->qscale_table;
867 if(q) {
868 int x, y;
869 int w = (lavc_venc_context->width+15) >> 4;
870 int h = (lavc_venc_context->height+15) >> 4;
871 for( y = 0; y < h; y++ ) {
872 for( x = 0; x < w; x++ )
873 quality += (double)*(q+x);
874 q += lavc_venc_context->coded_frame->qstride;
876 quality /= w * h;
877 } else
878 quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA;
880 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
881 lavc_venc_context->coded_frame->coded_picture_number,
882 quality,
883 out_size,
884 psnr(lavc_venc_context->coded_frame->error[0]/f),
885 psnr(lavc_venc_context->coded_frame->error[1]*4/f),
886 psnr(lavc_venc_context->coded_frame->error[2]*4/f),
887 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)),
888 pict_type_char[lavc_venc_context->coded_frame->pict_type]
891 /* store stats if there are any */
892 if(lavc_venc_context->stats_out && stats_file)
893 fprintf(stats_file, "%s", lavc_venc_context->stats_out);
894 return out_size;
897 static void uninit(struct vf_instance* vf){
899 if(lavc_param_psnr){
900 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
901 f*= lavc_venc_context->coded_frame->coded_picture_number;
903 mp_msg(MSGT_MENCODER, MSGL_INFO, "PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
904 psnr(lavc_venc_context->error[0]/f),
905 psnr(lavc_venc_context->error[1]*4/f),
906 psnr(lavc_venc_context->error[2]*4/f),
907 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5))
911 av_freep(&lavc_venc_context->intra_matrix);
912 av_freep(&lavc_venc_context->inter_matrix);
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){
927 vf->uninit=uninit;
928 vf->config=config;
929 vf->default_caps=VFCAP_CONSTANT;
930 vf->control=control;
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;
972 else
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");
985 return 0;
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');
1032 else
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){
1037 avcodec_init();
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_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec);
1045 return 0;
1048 vf->priv->pic = avcodec_alloc_frame();
1049 vf->priv->context = avcodec_alloc_context();
1051 return 1;
1054 vf_info_t ve_info_lavc = {
1055 "libavcodec encoder",
1056 "lavc",
1057 "A'rpi, Alex, Michael",
1058 "for internal use by mencoder",
1059 vf_open
1062 //===========================================================================//