deprecate palette8topacked32 in favor of public API functions sws_convertPalette8ToPa...
[mplayer/greg.git] / libmpcodecs / ve_lavc.c
blob49cd4ade97e8b41592972a167ef695d5a7046501
1 /*
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.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <limits.h>
24 #include <time.h>
25 #include <assert.h>
27 #if !defined(INFINITY) && defined(HUGE_VAL)
28 #define INFINITY HUGE_VAL
29 #endif
31 #include "config.h"
33 #include "mp_msg.h"
34 #include "help_mp.h"
35 #include "av_opts.h"
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"
47 #include "mp_image.h"
48 #include "vf.h"
50 extern char* passtmpfile;
52 //===========================================================================//
54 #include "libavcodec/avcodec.h"
56 extern int avcodec_initialized;
58 /* video options */
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;
124 #ifdef FF_CMP_VSAD
125 static int lavc_param_ildct_cmp= FF_CMP_VSAD;
126 #endif
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 const m_option_t lavcopts_conf[]={
187 {"acodec", &lavc_param_acodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
188 {"abitrate", &lavc_param_abitrate, CONF_TYPE_INT, CONF_RANGE, 1, 1000000, NULL},
189 {"atag", &lavc_param_atag, CONF_TYPE_INT, CONF_RANGE, 0, 0xffff, NULL},
190 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
191 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
192 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
193 {"vhq", &lavc_param_mb_decision, CONF_TYPE_FLAG, 0, 0, 1, NULL},
194 {"mbd", &lavc_param_mb_decision, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
195 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
196 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
197 {"vqscale", &lavc_param_vqscale, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 255.0, NULL},
198 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
199 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
200 {"mbqmin", &lavc_param_mb_qmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
201 {"mbqmax", &lavc_param_mb_qmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
202 {"lmin", &lavc_param_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
203 {"lmax", &lavc_param_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
204 {"mblmin", &lavc_param_mb_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
205 {"mblmax", &lavc_param_mb_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
206 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
207 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
208 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
209 {"vb_qfactor", &lavc_param_vb_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
210 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL},
211 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
212 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
213 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
214 {"vb_qoffset", &lavc_param_vb_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
215 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
216 {"vcelim", &lavc_param_chroma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
217 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
218 {"vstrict", &lavc_param_strict, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
219 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
220 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL},
221 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
222 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL},
223 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
224 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
225 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
226 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
227 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL},
228 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL},
229 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL},
230 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
231 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
232 {"vrc_buf_size", &lavc_param_rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
233 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
234 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL},
235 {"vrc_init_occupancy", &lavc_param_rc_initial_buffer_occupancy, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
236 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
237 {"aspect", &lavc_param_aspect, CONF_TYPE_STRING, 0, 0, 0, NULL},
238 {"autoaspect", &lavc_param_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL},
239 {"lumi_mask", &lavc_param_lumi_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
240 {"tcplx_mask", &lavc_param_temporal_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
241 {"scplx_mask", &lavc_param_spatial_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
242 {"p_mask", &lavc_param_p_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
243 {"naq", &lavc_param_normalize_aqp, CONF_TYPE_FLAG, 0, 0, 1, NULL},
244 {"dark_mask", &lavc_param_dark_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
245 {"ildct", &lavc_param_interlaced_dct, CONF_TYPE_FLAG, 0, 0, 1, NULL},
246 {"idct", &lavc_param_idct, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
247 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
248 {"format", &lavc_param_format, CONF_TYPE_IMGFMT, 0, 0, 0, NULL},
249 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
250 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL},
251 {"precmp", &lavc_param_me_pre_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
252 {"cmp", &lavc_param_me_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
253 {"subcmp", &lavc_param_me_sub_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
254 {"mbcmp", &lavc_param_mb_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
255 {"skipcmp", &lavc_param_skip_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
256 #ifdef FF_CMP_VSAD
257 {"ildctcmp", &lavc_param_ildct_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
258 #endif
259 {"bit_exact", &lavc_param_bit_exact, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_BITEXACT, NULL},
260 {"predia", &lavc_param_pre_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
261 {"dia", &lavc_param_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
262 {"qpel", &lavc_param_qpel, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QPEL, NULL},
263 {"trell", &lavc_param_trell, CONF_TYPE_FLAG, 0, 0, 1, NULL},
264 {"lowdelay", &lavc_param_lowdelay, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOW_DELAY, NULL},
265 {"last_pred", &lavc_param_last_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
266 {"preme", &lavc_param_pre_me, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
267 {"subq", &lavc_param_me_subpel_quality, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
268 {"me_range", &lavc_param_me_range, CONF_TYPE_INT, CONF_RANGE, 0, 16000, NULL},
269 #ifdef CODEC_FLAG_AC_PRED
270 {"aic", &lavc_param_aic, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_AC_PRED, NULL},
271 {"umv", &lavc_param_umv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_UMV, NULL},
272 #endif
273 #ifdef CODEC_FLAG_H263P_AIV
274 {"aiv", &lavc_param_aiv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIV, NULL},
275 #endif
276 #ifdef CODEC_FLAG_OBMC
277 {"obmc", &lavc_param_obmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_OBMC, NULL},
278 #endif
279 #ifdef CODEC_FLAG_LOOP_FILTER
280 {"loop", &lavc_param_loop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOOP_FILTER, NULL},
281 #endif
282 {"ibias", &lavc_param_ibias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
283 {"pbias", &lavc_param_pbias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
284 {"coder", &lavc_param_coder, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
285 {"context", &lavc_param_context, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
286 {"intra_matrix", &lavc_param_intra_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
287 {"inter_matrix", &lavc_param_inter_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
288 {"cbp", &lavc_param_cbp, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CBP_RD, NULL},
289 {"mv0", &lavc_param_mv0, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_MV0, NULL},
290 {"nr", &lavc_param_noise_reduction, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
291 #ifdef CODEC_FLAG_QP_RD
292 {"qprd", &lavc_param_qp_rd, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QP_RD, NULL},
293 #endif
294 #ifdef CODEC_FLAG_H263P_SLICE_STRUCT
295 {"ss", &lavc_param_ss, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_SLICE_STRUCT, NULL},
296 #endif
297 #ifdef CODEC_FLAG_ALT_SCAN
298 {"alt", &lavc_param_alt, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_ALT_SCAN, NULL},
299 #endif
300 #ifdef CODEC_FLAG_INTERLACED_ME
301 {"ilme", &lavc_param_ilme, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_INTERLACED_ME, NULL},
302 #endif
303 #ifdef CODEC_FLAG_CLOSED_GOP
304 {"cgop", &lavc_param_closed_gop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CLOSED_GOP, NULL},
305 #endif
306 #ifdef CODEC_FLAG_GMC
307 {"gmc", &lavc_param_gmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_GMC, NULL},
308 #endif
309 {"dc", &lavc_param_dc_precision, CONF_TYPE_INT, CONF_RANGE, 8, 11, NULL},
310 {"border_mask", &lavc_param_border_masking, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
311 {"inter_threshold", &lavc_param_inter_threshold, CONF_TYPE_INT, CONF_RANGE, -1000000, 1000000, NULL},
312 {"sc_threshold", &lavc_param_sc_threshold, CONF_TYPE_INT, CONF_RANGE, -1000000000, 1000000000, NULL},
313 {"top", &lavc_param_top, CONF_TYPE_INT, CONF_RANGE, -1, 1, NULL},
314 {"qns", &lavc_param_qns, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
315 {"nssew", &lavc_param_nssew, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
316 {"threads", &lavc_param_threads, CONF_TYPE_INT, CONF_RANGE, 1, 8, NULL},
317 {"turbo", &lavc_param_turbo, CONF_TYPE_FLAG, 0, 0, 1, NULL},
318 {"skip_threshold", &lavc_param_skip_threshold, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
319 {"skip_factor", &lavc_param_skip_factor, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
320 {"skip_exp", &lavc_param_skip_exp, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
321 {"brd_scale", &lavc_param_brd_scale, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
322 {"bidir_refine", &lavc_param_bidir_refine, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL},
323 {"sc_factor", &lavc_param_sc_factor, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
324 {"vglobal", &lavc_param_video_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
325 {"aglobal", &lavc_param_audio_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
326 {"mv0_threshold", &lavc_param_mv0_threshold, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
327 {"refs", &lavc_param_refs, CONF_TYPE_INT, CONF_RANGE, 1, 16, NULL},
328 {"b_sensitivity", &lavc_param_b_sensitivity, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
329 {"level", &lavc_param_level, CONF_TYPE_INT, CONF_RANGE, INT_MIN, INT_MAX, NULL},
330 {"o", &lavc_param_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
331 {NULL, NULL, 0, 0, 0, 0, NULL}
334 struct vf_priv_s {
335 muxer_stream_t* mux;
336 AVCodecContext *context;
337 AVFrame *pic;
338 AVCodec *codec;
339 FILE *stats_file;
342 #define stats_file (vf->priv->stats_file)
343 #define mux_v (vf->priv->mux)
344 #define lavc_venc_context (vf->priv->context)
346 static int encode_frame(struct vf_instance *vf, AVFrame *pic, double pts);
348 static int config(struct vf_instance *vf,
349 int width, int height, int d_width, int d_height,
350 unsigned int flags, unsigned int outfmt){
351 int size, i;
352 char *p;
354 mux_v->bih->biWidth=width;
355 mux_v->bih->biHeight=height;
356 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
358 mp_msg(MSGT_MENCODER, MSGL_INFO,"videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
359 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression,
360 (char *)&mux_v->bih->biCompression);
362 lavc_venc_context->width = width;
363 lavc_venc_context->height = height;
364 if (lavc_param_vbitrate > 16000) /* != -1 */
365 lavc_venc_context->bit_rate = lavc_param_vbitrate;
366 else if (lavc_param_vbitrate >= 0) /* != -1 */
367 lavc_venc_context->bit_rate = lavc_param_vbitrate*1000;
368 else
369 lavc_venc_context->bit_rate = 800000; /* default */
371 mux_v->avg_rate= lavc_venc_context->bit_rate;
373 lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000;
374 lavc_venc_context->time_base= (AVRational){mux_v->h.dwScale, mux_v->h.dwRate};
375 lavc_venc_context->qmin= lavc_param_vqmin;
376 lavc_venc_context->qmax= lavc_param_vqmax;
377 lavc_venc_context->mb_qmin= lavc_param_mb_qmin;
378 lavc_venc_context->mb_qmax= lavc_param_mb_qmax;
379 lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5);
380 lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5);
381 lavc_venc_context->mb_lmin= (int)(FF_QP2LAMBDA * lavc_param_mb_lmin + 0.5);
382 lavc_venc_context->mb_lmax= (int)(FF_QP2LAMBDA * lavc_param_mb_lmax + 0.5);
383 lavc_venc_context->max_qdiff= lavc_param_vqdiff;
384 lavc_venc_context->qcompress= lavc_param_vqcompress;
385 lavc_venc_context->qblur= lavc_param_vqblur;
386 lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames;
387 lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor;
388 lavc_venc_context->rc_strategy= lavc_param_vrc_strategy;
389 lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy;
390 lavc_venc_context->b_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vb_qoffset + 0.5);
391 lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold;
392 lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold;
393 lavc_venc_context->rtp_payload_size= lavc_param_packet_size;
394 lavc_venc_context->strict_std_compliance= lavc_param_strict;
395 lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor;
396 lavc_venc_context->i_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vi_qoffset + 0.5);
397 lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish;
398 lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp;
399 lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq;
400 lavc_venc_context->rc_eq= lavc_param_rc_eq;
402 mux_v->max_rate=
403 lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000;
404 lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000;
406 mux_v->vbv_size=
407 lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000;
409 lavc_venc_context->rc_initial_buffer_occupancy=
410 lavc_venc_context->rc_buffer_size *
411 lavc_param_rc_initial_buffer_occupancy;
412 lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity;
413 lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx;
414 lavc_venc_context->debug= lavc_param_debug;
415 lavc_venc_context->last_predictor_count= lavc_param_last_pred;
416 lavc_venc_context->pre_me= lavc_param_pre_me;
417 lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp;
418 lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size;
419 lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality;
420 lavc_venc_context->me_range= lavc_param_me_range;
421 lavc_venc_context->intra_quant_bias= lavc_param_ibias;
422 lavc_venc_context->inter_quant_bias= lavc_param_pbias;
423 lavc_venc_context->coder_type= lavc_param_coder;
424 lavc_venc_context->context_model= lavc_param_context;
425 lavc_venc_context->scenechange_threshold= lavc_param_sc_threshold;
426 lavc_venc_context->noise_reduction= lavc_param_noise_reduction;
427 lavc_venc_context->quantizer_noise_shaping= lavc_param_qns;
428 lavc_venc_context->inter_threshold= lavc_param_inter_threshold;
429 lavc_venc_context->nsse_weight= lavc_param_nssew;
430 lavc_venc_context->frame_skip_threshold= lavc_param_skip_threshold;
431 lavc_venc_context->frame_skip_factor= lavc_param_skip_factor;
432 lavc_venc_context->frame_skip_exp= lavc_param_skip_exp;
433 lavc_venc_context->frame_skip_cmp= lavc_param_skip_cmp;
435 if (lavc_param_intra_matrix)
437 char *tmp;
439 lavc_venc_context->intra_matrix =
440 av_malloc(sizeof(*lavc_venc_context->intra_matrix)*64);
442 i = 0;
443 while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64))
445 if (!tmp || (tmp && !strlen(tmp)))
446 break;
447 lavc_venc_context->intra_matrix[i++] = atoi(tmp);
450 if (i != 64)
451 av_freep(&lavc_venc_context->intra_matrix);
452 else
453 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n");
455 if (lavc_param_inter_matrix)
457 char *tmp;
459 lavc_venc_context->inter_matrix =
460 av_malloc(sizeof(*lavc_venc_context->inter_matrix)*64);
462 i = 0;
463 while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64))
465 if (!tmp || (tmp && !strlen(tmp)))
466 break;
467 lavc_venc_context->inter_matrix[i++] = atoi(tmp);
470 if (i != 64)
471 av_freep(&lavc_venc_context->inter_matrix);
472 else
473 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n");
476 p= lavc_param_rc_override_string;
477 for(i=0; p; i++){
478 int start, end, q;
479 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
480 if(e!=3){
481 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n");
482 return 0;
484 lavc_venc_context->rc_override=
485 realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1));
486 lavc_venc_context->rc_override[i].start_frame= start;
487 lavc_venc_context->rc_override[i].end_frame = end;
488 if(q>0){
489 lavc_venc_context->rc_override[i].qscale= q;
490 lavc_venc_context->rc_override[i].quality_factor= 1.0;
492 else{
493 lavc_venc_context->rc_override[i].qscale= 0;
494 lavc_venc_context->rc_override[i].quality_factor= -q/100.0;
496 p= strchr(p, '/');
497 if(p) p++;
499 lavc_venc_context->rc_override_count=i;
501 lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant;
503 lavc_venc_context->dct_algo= lavc_param_fdct;
504 lavc_venc_context->idct_algo= lavc_param_idct;
506 lavc_venc_context->lumi_masking= lavc_param_lumi_masking;
507 lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking;
508 lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking;
509 lavc_venc_context->p_masking= lavc_param_p_masking;
510 lavc_venc_context->dark_masking= lavc_param_dark_masking;
511 lavc_venc_context->border_masking = lavc_param_border_masking;
513 if (lavc_param_aspect != NULL)
515 int par_width, par_height, e;
516 float ratio=0;
518 e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height);
519 if(e==2){
520 if(par_height)
521 ratio= (float)par_width / (float)par_height;
522 }else{
523 e= sscanf (lavc_param_aspect, "%f", &ratio);
526 if (e && ratio > 0.1 && ratio < 10.0) {
527 lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255);
528 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n",
529 lavc_venc_context->sample_aspect_ratio.num,
530 lavc_venc_context->sample_aspect_ratio.den);
531 mux_v->aspect = ratio;
532 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio);
533 } else {
534 mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect);
535 return 0;
538 else if (lavc_param_autoaspect) {
539 lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255);
540 mux_v->aspect = (float)d_width/d_height;
543 /* keyframe interval */
544 if (lavc_param_keyint >= 0) /* != -1 */
545 lavc_venc_context->gop_size = lavc_param_keyint;
546 else
547 lavc_venc_context->gop_size = 250; /* default */
549 lavc_venc_context->flags = 0;
550 if (lavc_param_mb_decision)
552 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_HighQualityEncodingSelected);
553 lavc_venc_context->mb_decision= lavc_param_mb_decision;
556 lavc_venc_context->me_cmp= lavc_param_me_cmp;
557 lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp;
558 lavc_venc_context->mb_cmp= lavc_param_mb_cmp;
559 #ifdef FF_CMP_VSAD
560 lavc_venc_context->ildct_cmp= lavc_param_ildct_cmp;
561 #endif
562 lavc_venc_context->dia_size= lavc_param_dia_size;
563 lavc_venc_context->flags|= lavc_param_qpel;
564 lavc_venc_context->trellis = lavc_param_trell;
565 lavc_venc_context->flags|= lavc_param_lowdelay;
566 lavc_venc_context->flags|= lavc_param_bit_exact;
567 lavc_venc_context->flags|= lavc_param_aic;
568 lavc_venc_context->flags|= lavc_param_aiv;
569 lavc_venc_context->flags|= lavc_param_umv;
570 lavc_venc_context->flags|= lavc_param_obmc;
571 lavc_venc_context->flags|= lavc_param_loop;
572 lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0;
573 lavc_venc_context->flags|= lavc_param_data_partitioning;
574 lavc_venc_context->flags|= lavc_param_cbp;
575 lavc_venc_context->flags|= lavc_param_mv0;
576 lavc_venc_context->flags|= lavc_param_qp_rd;
577 lavc_venc_context->flags|= lavc_param_ss;
578 lavc_venc_context->flags|= lavc_param_alt;
579 lavc_venc_context->flags|= lavc_param_ilme;
580 lavc_venc_context->flags|= lavc_param_gmc;
581 #ifdef CODEC_FLAG_CLOSED_GOP
582 lavc_venc_context->flags|= lavc_param_closed_gop;
583 #endif
584 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY;
586 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP;
587 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT;
588 lavc_venc_context->flags|= lavc_param_psnr;
589 lavc_venc_context->intra_dc_precision = lavc_param_dc_precision - 8;
590 lavc_venc_context->prediction_method= lavc_param_prediction_method;
591 lavc_venc_context->brd_scale = lavc_param_brd_scale;
592 lavc_venc_context->bidir_refine = lavc_param_bidir_refine;
593 lavc_venc_context->scenechange_factor = lavc_param_sc_factor;
594 if((lavc_param_video_global_header&1)
595 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
596 lavc_venc_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
598 if(lavc_param_video_global_header&2){
599 lavc_venc_context->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
601 lavc_venc_context->mv0_threshold = lavc_param_mv0_threshold;
602 lavc_venc_context->refs = lavc_param_refs;
603 lavc_venc_context->b_sensitivity = lavc_param_b_sensitivity;
604 lavc_venc_context->level = lavc_param_level;
606 if(lavc_param_avopt){
607 if(parse_avopts(lavc_venc_context, lavc_param_avopt) < 0){
608 mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_avopt);
609 return 0;
613 mux_v->imgfmt = lavc_param_format;
614 lavc_venc_context->pix_fmt = imgfmt2pixfmt(lavc_param_format);
615 if (lavc_venc_context->pix_fmt == PIX_FMT_NONE)
616 return 0;
618 if(!stats_file) {
619 /* lavc internal 2pass bitrate control */
620 switch(lavc_param_vpass){
621 case 2:
622 case 3:
623 lavc_venc_context->flags|= CODEC_FLAG_PASS2;
624 stats_file= fopen(passtmpfile, "rb");
625 if(stats_file==NULL){
626 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
627 return 0;
629 fseek(stats_file, 0, SEEK_END);
630 size= ftell(stats_file);
631 fseek(stats_file, 0, SEEK_SET);
633 lavc_venc_context->stats_in= av_malloc(size + 1);
634 lavc_venc_context->stats_in[size]=0;
636 if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){
637 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile);
638 return 0;
640 if(lavc_param_vpass == 2)
641 break;
642 else
643 fclose(stats_file);
644 /* fall through */
645 case 1:
646 lavc_venc_context->flags|= CODEC_FLAG_PASS1;
647 stats_file= fopen(passtmpfile, "wb");
648 if(stats_file==NULL){
649 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
650 return 0;
652 if(lavc_param_turbo && (lavc_param_vpass == 1)) {
653 /* uses SAD comparison functions instead of other hungrier */
654 lavc_venc_context->me_pre_cmp = 0;
655 lavc_venc_context->me_cmp = 0;
656 lavc_venc_context->me_sub_cmp = 0;
657 lavc_venc_context->mb_cmp = 2;
659 /* Disables diamond motion estimation */
660 lavc_venc_context->pre_dia_size = 0;
661 lavc_venc_context->dia_size = 1;
663 lavc_venc_context->quantizer_noise_shaping = 0; // qns=0
664 lavc_venc_context->noise_reduction = 0; // nr=0
665 lavc_venc_context->mb_decision = 0; // mbd=0 ("realtime" encoding)
667 lavc_venc_context->flags &= ~CODEC_FLAG_QPEL;
668 lavc_venc_context->flags &= ~CODEC_FLAG_4MV;
669 lavc_venc_context->trellis = 0;
670 lavc_venc_context->flags &= ~CODEC_FLAG_CBP_RD;
671 lavc_venc_context->flags &= ~CODEC_FLAG_QP_RD;
672 lavc_venc_context->flags &= ~CODEC_FLAG_MV0;
674 break;
678 lavc_venc_context->me_method = ME_ZERO+lavc_param_vme;
680 /* fixed qscale :p */
681 if (lavc_param_vqscale >= 0.0)
683 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_UsingConstantQscale, lavc_param_vqscale);
684 lavc_venc_context->flags |= CODEC_FLAG_QSCALE;
685 lavc_venc_context->global_quality=
686 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5);
689 if(lavc_param_threads > 1)
690 avcodec_thread_init(lavc_venc_context, lavc_param_threads);
692 if (avcodec_open(lavc_venc_context, vf->priv->codec) != 0) {
693 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
694 return 0;
697 if (lavc_venc_context->codec->encode == NULL) {
698 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
699 return 0;
702 /* free second pass buffer, its not needed anymore */
703 av_freep(&lavc_venc_context->stats_in);
704 if(lavc_venc_context->bits_per_coded_sample)
705 mux_v->bih->biBitCount= lavc_venc_context->bits_per_coded_sample;
706 if(lavc_venc_context->extradata_size){
707 mux_v->bih= realloc(mux_v->bih, sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size);
708 memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size);
709 mux_v->bih->biSize= sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size;
712 mux_v->decoder_delay = lavc_venc_context->max_b_frames ? 1 : 0;
714 return 1;
717 static int control(struct vf_instance *vf, int request, void* data){
719 switch(request){
720 case VFCTRL_FLUSH_FRAMES:
721 if(vf->priv->codec->capabilities & CODEC_CAP_DELAY)
722 while(encode_frame(vf, NULL, MP_NOPTS_VALUE) > 0);
723 return CONTROL_TRUE;
724 default:
725 return CONTROL_UNKNOWN;
729 static int query_format(struct vf_instance *vf, unsigned int fmt){
730 switch(fmt){
731 case IMGFMT_YV12:
732 case IMGFMT_IYUV:
733 case IMGFMT_I420:
734 if(lavc_param_format == IMGFMT_YV12)
735 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
736 break;
737 case IMGFMT_411P:
738 if(lavc_param_format == IMGFMT_411P)
739 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
740 break;
741 case IMGFMT_422P:
742 if(lavc_param_format == IMGFMT_422P)
743 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
744 break;
745 case IMGFMT_444P:
746 if(lavc_param_format == IMGFMT_444P)
747 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
748 break;
749 case IMGFMT_YVU9:
750 if(lavc_param_format == IMGFMT_YVU9)
751 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
752 break;
753 case IMGFMT_BGR32:
754 if(lavc_param_format == IMGFMT_BGR32)
755 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
756 break;
758 return 0;
761 static double psnr(double d){
762 if(d==0) return INFINITY;
763 return -10.0*log(d)/log(10);
766 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
767 AVFrame *pic= vf->priv->pic;
769 pic->data[0]=mpi->planes[0];
770 pic->data[1]=mpi->planes[1];
771 pic->data[2]=mpi->planes[2];
772 pic->linesize[0]=mpi->stride[0];
773 pic->linesize[1]=mpi->stride[1];
774 pic->linesize[2]=mpi->stride[2];
776 if(lavc_param_interlaced_dct){
777 if((mpi->fields & MP_IMGFIELD_ORDERED) && (mpi->fields & MP_IMGFIELD_INTERLACED))
778 pic->top_field_first= !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
779 else
780 pic->top_field_first= 1;
782 if(lavc_param_top!=-1)
783 pic->top_field_first= lavc_param_top;
786 return encode_frame(vf, pic, pts) >= 0;
789 static int encode_frame(struct vf_instance *vf, AVFrame *pic, double pts){
790 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
791 int out_size;
792 double dts;
794 if(pts == MP_NOPTS_VALUE)
795 pts= lavc_venc_context->frame_number * av_q2d(lavc_venc_context->time_base);
797 if(pic){
798 #if 0
799 pic->opaque= malloc(sizeof(pts));
800 memcpy(pic->opaque, &pts, sizeof(pts));
801 #else
802 if(pts != MP_NOPTS_VALUE)
803 pic->pts= floor(pts / av_q2d(lavc_venc_context->time_base) + 0.5);
804 else
805 pic->pts= MP_NOPTS_VALUE;
806 #endif
808 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
809 pic);
811 if(pts != MP_NOPTS_VALUE)
812 dts= pts - lavc_venc_context->delay * av_q2d(lavc_venc_context->time_base);
813 else
814 dts= MP_NOPTS_VALUE;
815 #if 0
816 pts= lavc_venc_context->coded_frame->opaque ?
817 *(double*)lavc_venc_context->coded_frame->opaque
818 : MP_NOPTS_VALUE;
819 #else
820 if(lavc_venc_context->coded_frame->pts != MP_NOPTS_VALUE)
821 pts= lavc_venc_context->coded_frame->pts * av_q2d(lavc_venc_context->time_base);
822 else
823 pts= MP_NOPTS_VALUE;
824 assert(MP_NOPTS_VALUE == AV_NOPTS_VALUE);
825 #endif
826 //fprintf(stderr, "ve_lavc %f/%f\n", dts, pts);
827 if(out_size == 0 && lavc_param_skip_threshold==0 && lavc_param_skip_factor==0){
828 ++mux_v->encoder_delay;
829 return 0;
832 muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0,
833 dts, pts);
834 free(lavc_venc_context->coded_frame->opaque);
835 lavc_venc_context->coded_frame->opaque= NULL;
837 /* store psnr / pict size / type / qscale */
838 if(lavc_param_psnr){
839 static FILE *fvstats=NULL;
840 char filename[20];
841 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
842 double quality=0.0;
843 int8_t *q;
845 if(!fvstats) {
846 time_t today2;
847 struct tm *today;
848 today2 = time(NULL);
849 today = localtime(&today2);
850 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour,
851 today->tm_min, today->tm_sec);
852 fvstats = fopen(filename,"w");
853 if(!fvstats) {
854 perror("fopen");
855 lavc_param_psnr=0; // disable block
856 mp_msg(MSGT_MENCODER,MSGL_ERR,"Can't open %s for writing. Check its permissions.\n",filename);
857 return -1;
858 /*exit(1);*/
862 // average MB quantizer
863 q = lavc_venc_context->coded_frame->qscale_table;
864 if(q) {
865 int x, y;
866 int w = (lavc_venc_context->width+15) >> 4;
867 int h = (lavc_venc_context->height+15) >> 4;
868 for( y = 0; y < h; y++ ) {
869 for( x = 0; x < w; x++ )
870 quality += (double)*(q+x);
871 q += lavc_venc_context->coded_frame->qstride;
873 quality /= w * h;
874 } else
875 quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA;
877 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
878 lavc_venc_context->coded_frame->coded_picture_number,
879 quality,
880 out_size,
881 psnr(lavc_venc_context->coded_frame->error[0]/f),
882 psnr(lavc_venc_context->coded_frame->error[1]*4/f),
883 psnr(lavc_venc_context->coded_frame->error[2]*4/f),
884 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)),
885 pict_type_char[lavc_venc_context->coded_frame->pict_type]
888 /* store stats if there are any */
889 if(lavc_venc_context->stats_out && stats_file)
890 fprintf(stats_file, "%s", lavc_venc_context->stats_out);
891 return out_size;
894 static void uninit(struct vf_instance *vf){
896 if(lavc_param_psnr){
897 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
898 f*= lavc_venc_context->coded_frame->coded_picture_number;
900 mp_msg(MSGT_MENCODER, MSGL_INFO, "PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
901 psnr(lavc_venc_context->error[0]/f),
902 psnr(lavc_venc_context->error[1]*4/f),
903 psnr(lavc_venc_context->error[2]*4/f),
904 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5))
908 av_freep(&lavc_venc_context->intra_matrix);
909 av_freep(&lavc_venc_context->inter_matrix);
911 if (lavc_venc_context->codec)
912 avcodec_close(lavc_venc_context);
914 if(stats_file) fclose(stats_file);
916 /* free rc_override */
917 av_freep(&lavc_venc_context->rc_override);
919 av_freep(&vf->priv->context);
922 //===========================================================================//
924 static int vf_open(vf_instance_t *vf, char* args){
925 vf->uninit=uninit;
926 vf->config=config;
927 vf->default_caps=VFCAP_CONSTANT;
928 vf->control=control;
929 vf->query_format=query_format;
930 vf->put_image=put_image;
931 vf->priv=malloc(sizeof(struct vf_priv_s));
932 memset(vf->priv,0,sizeof(struct vf_priv_s));
933 vf->priv->mux=(muxer_stream_t*)args;
935 /* XXX: hack: some of the MJPEG decoder DLL's needs exported huffman
936 table, so we define a zero-table, also lavc mjpeg encoder is putting
937 huffman tables into the stream, so no problem */
938 if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "mjpeg"))
940 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+28);
941 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+28);
942 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+28;
944 else if (lavc_param_vcodec && (!strcasecmp(lavc_param_vcodec, "huffyuv")
945 || !strcasecmp(lavc_param_vcodec, "ffvhuff")))
947 /* XXX: hack: huffyuv needs to store huffman tables (allthough we dunno the size yet ...) */
948 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+1000);
949 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+1000);
950 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+1000;
952 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv1"))
954 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8);
955 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8);
956 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
958 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv2"))
960 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8);
961 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8);
962 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
964 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "wmv2"))
966 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+4);
967 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+4);
968 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+4;
970 else
972 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
973 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER));
974 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
976 mux_v->bih->biWidth=0;
977 mux_v->bih->biHeight=0;
978 mux_v->bih->biPlanes=1;
979 mux_v->bih->biBitCount=24;
980 if (!lavc_param_vcodec)
982 printf("No libavcodec codec specified! It's required!\n");
983 return 0;
986 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video"))
987 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1');
988 else if (!strcasecmp(lavc_param_vcodec, "mpeg2") || !strcasecmp(lavc_param_vcodec, "mpeg2video"))
989 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '2');
990 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p"))
991 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3');
992 else if (!strcasecmp(lavc_param_vcodec, "rv10"))
993 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0');
994 else if (!strcasecmp(lavc_param_vcodec, "mjpeg"))
995 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G');
996 else if (!strcasecmp(lavc_param_vcodec, "ljpeg"))
997 mux_v->bih->biCompression = mmioFOURCC('L', 'J', 'P', 'G');
998 else if (!strcasecmp(lavc_param_vcodec, "mpeg4"))
999 mux_v->bih->biCompression = mmioFOURCC('F', 'M', 'P', '4');
1000 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4"))
1001 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3');
1002 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2"))
1003 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2');
1004 else if (!strcasecmp(lavc_param_vcodec, "wmv1"))
1005 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1');
1006 else if (!strcasecmp(lavc_param_vcodec, "wmv2"))
1007 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '2');
1008 else if (!strcasecmp(lavc_param_vcodec, "huffyuv"))
1009 mux_v->bih->biCompression = mmioFOURCC('H', 'F', 'Y', 'U');
1010 else if (!strcasecmp(lavc_param_vcodec, "ffvhuff"))
1011 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', 'H');
1012 else if (!strcasecmp(lavc_param_vcodec, "asv1"))
1013 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '1');
1014 else if (!strcasecmp(lavc_param_vcodec, "asv2"))
1015 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '2');
1016 else if (!strcasecmp(lavc_param_vcodec, "ffv1"))
1017 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', '1');
1018 else if (!strcasecmp(lavc_param_vcodec, "snow"))
1019 mux_v->bih->biCompression = mmioFOURCC('S', 'N', 'O', 'W');
1020 else if (!strcasecmp(lavc_param_vcodec, "flv"))
1021 mux_v->bih->biCompression = mmioFOURCC('F', 'L', 'V', '1');
1022 else if (!strcasecmp(lavc_param_vcodec, "dvvideo"))
1023 mux_v->bih->biCompression = mmioFOURCC('d', 'v', 's', 'd');
1024 else if (!strcasecmp(lavc_param_vcodec, "libx264"))
1025 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '4');
1026 else if (!strcasecmp(lavc_param_vcodec, "libschroedinger"))
1027 mux_v->bih->biCompression = mmioFOURCC('d', 'r', 'a', 'c');
1028 else if (!strcasecmp(lavc_param_vcodec, "libdirac"))
1029 mux_v->bih->biCompression = mmioFOURCC('d', 'r', 'a', 'c');
1030 else
1031 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0],
1032 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */
1034 if (!avcodec_initialized){
1035 avcodec_init();
1036 avcodec_register_all();
1037 avcodec_initialized=1;
1040 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec);
1041 if (!vf->priv->codec) {
1042 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec);
1043 return 0;
1046 vf->priv->pic = avcodec_alloc_frame();
1047 vf->priv->context = avcodec_alloc_context();
1048 vf->priv->context->codec_type = CODEC_TYPE_VIDEO;
1049 vf->priv->context->codec_id = vf->priv->codec->id;
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 //===========================================================================//