typo fixes
[mplayer/greg.git] / libmpcodecs / ve_lavc.c
blobbcff655c38d818f19301ad3fe16a65a9dac4b099
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"
18 #include "codec-cfg.h"
19 #include "stream.h"
20 #include "demuxer.h"
21 #include "stheader.h"
23 #include "muxer.h"
25 #include "img_format.h"
26 #include "mp_image.h"
27 #include "vf.h"
29 extern char* passtmpfile;
31 //===========================================================================//
33 #ifdef USE_LIBAVCODEC_SO
34 #include <ffmpeg/avcodec.h>
35 #else
36 #include "libavcodec/avcodec.h"
37 #endif
39 #if LIBAVCODEC_BUILD < 4641
40 #error we do not support libavcodec prior to build 4641, get the latest libavcodec CVS
41 #endif
43 #if LIBAVCODEC_BUILD < 4659
44 #warning your version of libavcodec is old, u might want to get a newer one
45 #endif
47 #if LIBAVCODEC_BUILD < 4645
48 #define AVFrame AVVideoFrame
49 #define coded_frame coded_picture
50 #endif
52 extern int avcodec_inited;
54 /* video options */
55 static char *lavc_param_vcodec = "mpeg4";
56 static int lavc_param_vbitrate = -1;
57 static int lavc_param_vrate_tolerance = 1000*8;
58 static int lavc_param_mb_decision = 0; /* default is realtime encoding */
59 static int lavc_param_v4mv = 0;
60 static int lavc_param_vme = 4;
61 static float lavc_param_vqscale = 0.0;
62 static int lavc_param_vqmin = 2;
63 static int lavc_param_vqmax = 31;
64 static int lavc_param_mb_qmin = 2;
65 static int lavc_param_mb_qmax = 31;
66 static float lavc_param_lmin = 2;
67 static float lavc_param_lmax = 31;
68 static int lavc_param_vqdiff = 3;
69 static float lavc_param_vqcompress = 0.5;
70 static float lavc_param_vqblur = 0.5;
71 static float lavc_param_vb_qfactor = 1.25;
72 static float lavc_param_vb_qoffset = 1.25;
73 static float lavc_param_vi_qfactor = 0.8;
74 static float lavc_param_vi_qoffset = 0.0;
75 static int lavc_param_vmax_b_frames = 0;
76 static int lavc_param_keyint = -1;
77 static int lavc_param_vpass = 0;
78 static int lavc_param_vrc_strategy = 0;
79 static int lavc_param_vb_strategy = 0;
80 static int lavc_param_luma_elim_threshold = 0;
81 static int lavc_param_chroma_elim_threshold = 0;
82 static int lavc_param_packet_size= 0;
83 static int lavc_param_strict= -1;
84 static int lavc_param_data_partitioning= 0;
85 static int lavc_param_gray=0;
86 static float lavc_param_rc_qsquish=1.0;
87 static float lavc_param_rc_qmod_amp=0;
88 static int lavc_param_rc_qmod_freq=0;
89 static char *lavc_param_rc_override_string=NULL;
90 static char *lavc_param_rc_eq="tex^qComp";
91 static int lavc_param_rc_buffer_size=0;
92 static float lavc_param_rc_buffer_aggressivity=1.0;
93 static int lavc_param_rc_max_rate=0;
94 static int lavc_param_rc_min_rate=0;
95 static float lavc_param_rc_initial_cplx=0;
96 static float lavc_param_rc_initial_buffer_occupancy=0.9;
97 static int lavc_param_mpeg_quant=0;
98 static int lavc_param_fdct=0;
99 static int lavc_param_idct=0;
100 static char* lavc_param_aspect = NULL;
101 static int lavc_param_autoaspect = 0;
102 static float lavc_param_lumi_masking= 0.0;
103 static float lavc_param_dark_masking= 0.0;
104 static float lavc_param_temporal_cplx_masking= 0.0;
105 static float lavc_param_spatial_cplx_masking= 0.0;
106 static float lavc_param_p_masking= 0.0;
107 static float lavc_param_border_masking= 0.0;
108 static int lavc_param_normalize_aqp= 0;
109 static int lavc_param_interlaced_dct= 0;
110 static int lavc_param_prediction_method= FF_PRED_LEFT;
111 static int lavc_param_format= IMGFMT_YV12;
112 static int lavc_param_debug= 0;
113 static int lavc_param_psnr= 0;
114 static int lavc_param_me_pre_cmp= 0;
115 static int lavc_param_me_cmp= 0;
116 static int lavc_param_me_sub_cmp= 0;
117 static int lavc_param_mb_cmp= 0;
118 #ifdef FF_CMP_VSAD
119 static int lavc_param_ildct_cmp= FF_CMP_VSAD;
120 #endif
121 static int lavc_param_pre_dia_size= 0;
122 static int lavc_param_dia_size= 0;
123 static int lavc_param_qpel= 0;
124 static int lavc_param_trell= 0;
125 static int lavc_param_bit_exact = 0;
126 static int lavc_param_aic= 0;
127 static int lavc_param_aiv= 0;
128 static int lavc_param_umv= 0;
129 static int lavc_param_obmc= 0;
130 static int lavc_param_loop= 0;
131 static int lavc_param_last_pred= 0;
132 static int lavc_param_pre_me= 1;
133 static int lavc_param_me_subpel_quality= 8;
134 static int lavc_param_me_range= 0;
135 #if LIBAVCODEC_BUILD >= 4663
136 static int lavc_param_ibias= FF_DEFAULT_QUANT_BIAS;
137 static int lavc_param_pbias= FF_DEFAULT_QUANT_BIAS;
138 #endif
139 static int lavc_param_coder= 0;
140 static int lavc_param_context= 0;
141 static char *lavc_param_intra_matrix = NULL;
142 static char *lavc_param_inter_matrix = NULL;
143 static int lavc_param_cbp= 0;
144 static int lavc_param_mv0= 0;
145 static int lavc_param_noise_reduction= 0;
146 static int lavc_param_qns= 0;
147 static int lavc_param_qp_rd= 0;
148 static int lavc_param_inter_threshold= 0;
149 static int lavc_param_sc_threshold= 0;
150 static int lavc_param_ss= 0;
151 static int lavc_param_top= -1;
152 static int lavc_param_alt= 0;
153 static int lavc_param_ilme= 0;
154 static int lavc_param_nssew= 8;
155 static int lavc_param_closed_gop = 0;
156 static int lavc_param_dc_precision = 8;
157 static int lavc_param_threads= 1;
158 static int lavc_param_turbo = 0;
159 static int lavc_param_brd_scale = 0;
160 static int lavc_param_bidir_refine = 0;
161 static int lavc_param_sc_factor = 1;
162 static int lavc_param_video_global_header= 0;
163 static int lavc_param_mv0_threshold = 256;
165 char *lavc_param_acodec = "mp2";
166 int lavc_param_atag = 0;
167 int lavc_param_abitrate = 224;
168 int lavc_param_audio_global_header= 0;
170 #include "m_option.h"
172 #ifdef USE_LIBAVCODEC
173 m_option_t lavcopts_conf[]={
174 {"acodec", &lavc_param_acodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
175 {"abitrate", &lavc_param_abitrate, CONF_TYPE_INT, CONF_RANGE, 1, 1000, NULL},
176 {"atag", &lavc_param_atag, CONF_TYPE_INT, CONF_RANGE, 0, 0xffff, NULL},
177 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
178 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
179 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
180 {"vhq", &lavc_param_mb_decision, CONF_TYPE_FLAG, 0, 0, 1, NULL},
181 {"mbd", &lavc_param_mb_decision, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
182 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
183 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
184 {"vqscale", &lavc_param_vqscale, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
185 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
186 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
187 {"mbqmin", &lavc_param_mb_qmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
188 {"mbqmax", &lavc_param_mb_qmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
189 {"lmin", &lavc_param_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
190 {"lmax", &lavc_param_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
191 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
192 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
193 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
194 {"vb_qfactor", &lavc_param_vb_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
195 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL},
196 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
197 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
198 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
199 {"vb_qoffset", &lavc_param_vb_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
200 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
201 {"vcelim", &lavc_param_chroma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
202 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
203 {"vstrict", &lavc_param_strict, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
204 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
205 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL},
206 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
207 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL},
208 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
209 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
210 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
211 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
212 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL},
213 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL},
214 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL},
215 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
216 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
217 {"vrc_buf_size", &lavc_param_rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
218 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
219 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL},
220 {"vrc_init_occupancy", &lavc_param_rc_initial_buffer_occupancy, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
221 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
222 {"aspect", &lavc_param_aspect, CONF_TYPE_STRING, 0, 0, 0, NULL},
223 {"autoaspect", &lavc_param_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL},
224 {"lumi_mask", &lavc_param_lumi_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
225 {"tcplx_mask", &lavc_param_temporal_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
226 {"scplx_mask", &lavc_param_spatial_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
227 {"p_mask", &lavc_param_p_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
228 {"naq", &lavc_param_normalize_aqp, CONF_TYPE_FLAG, 0, 0, 1, NULL},
229 {"dark_mask", &lavc_param_dark_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
230 {"ildct", &lavc_param_interlaced_dct, CONF_TYPE_FLAG, 0, 0, 1, NULL},
231 {"idct", &lavc_param_idct, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
232 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
233 {"format", &lavc_param_format, CONF_TYPE_IMGFMT, 0, 0, 0, NULL},
234 #if LIBAVCODEC_BUILD >= 4642
235 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
236 #endif
237 #if LIBAVCODEC_BUILD >= 4643
238 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL},
239 #endif
240 {"precmp", &lavc_param_me_pre_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
241 {"cmp", &lavc_param_me_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
242 {"subcmp", &lavc_param_me_sub_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
243 {"mbcmp", &lavc_param_mb_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
244 #ifdef FF_CMP_VSAD
245 {"ildctcmp", &lavc_param_ildct_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
246 #endif
247 {"bit_exact", &lavc_param_bit_exact, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_BITEXACT, NULL},
248 {"predia", &lavc_param_pre_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
249 {"dia", &lavc_param_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
250 {"qpel", &lavc_param_qpel, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QPEL, NULL},
251 #if LIBAVCODEC_BUILD >= 4648
252 {"trell", &lavc_param_trell, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_TRELLIS_QUANT, NULL},
253 #endif
254 {"last_pred", &lavc_param_last_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
255 {"preme", &lavc_param_pre_me, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
256 {"subq", &lavc_param_me_subpel_quality, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
257 {"me_range", &lavc_param_me_range, CONF_TYPE_INT, CONF_RANGE, 0, 16000, NULL},
258 #ifdef CODEC_FLAG_H263P_AIC
259 {"aic", &lavc_param_aic, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIC, NULL},
260 {"umv", &lavc_param_umv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_UMV, NULL},
261 #endif
262 #ifdef CODEC_FLAG_H263P_AIV
263 {"aiv", &lavc_param_aiv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIV, NULL},
264 #endif
265 #ifdef CODEC_FLAG_OBMC
266 {"obmc", &lavc_param_obmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_OBMC, NULL},
267 #endif
268 #ifdef CODEC_FLAG_LOOP_FILTER
269 {"loop", &lavc_param_loop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOOP_FILTER, NULL},
270 #endif
271 #if LIBAVCODEC_BUILD >= 4663
272 {"ibias", &lavc_param_ibias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
273 {"pbias", &lavc_param_pbias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
274 #endif
275 #if LIBAVCODEC_BUILD >= 4669
276 {"coder", &lavc_param_coder, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
277 {"context", &lavc_param_context, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
278 #endif
279 #if LIBAVCODEC_BUILD >= 4675
280 {"intra_matrix", &lavc_param_intra_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
281 {"inter_matrix", &lavc_param_inter_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
282 #endif
283 #if LIBAVCODEC_BUILD >= 4681
284 {"cbp", &lavc_param_cbp, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CBP_RD, NULL},
285 #endif
286 #if LIBAVCODEC_BUILD >= 4683
287 {"mv0", &lavc_param_mv0, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_MV0, NULL},
288 #endif
289 {"nr", &lavc_param_noise_reduction, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
290 #ifdef CODEC_FLAG_QP_RD
291 {"qprd", &lavc_param_qp_rd, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QP_RD, NULL},
292 #endif
293 #ifdef CODEC_FLAG_H263P_SLICE_STRUCT
294 {"ss", &lavc_param_ss, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_SLICE_STRUCT, NULL},
295 #endif
296 #ifdef CODEC_FLAG_ALT_SCAN
297 {"alt", &lavc_param_alt, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_ALT_SCAN, NULL},
298 #endif
299 #ifdef CODEC_FLAG_INTERLACED_ME
300 {"ilme", &lavc_param_ilme, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_INTERLACED_ME, NULL},
301 #endif
302 #ifdef CODEC_FLAG_CLOSED_GOP
303 {"cgop", &lavc_param_closed_gop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CLOSED_GOP, NULL},
304 #endif
305 #if LIBAVCODEC_BUILD >= 4711
306 {"dc", &lavc_param_dc_precision, CONF_TYPE_INT, CONF_RANGE, 8, 11, NULL},
307 #endif
308 #if LIBAVCODEC_BUILD >= 4741
309 {"border_mask", &lavc_param_border_masking, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
310 #endif
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 {"brd_scale", &lavc_param_brd_scale, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
319 {"bidir_refine", &lavc_param_bidir_refine, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL},
320 {"sc_factor", &lavc_param_sc_factor, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
321 {"vglobal", &lavc_param_video_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
322 {"aglobal", &lavc_param_audio_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
323 {"mv0_threshold", &lavc_param_mv0_threshold, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
324 {NULL, NULL, 0, 0, 0, 0, NULL}
326 #endif
328 struct vf_priv_s {
329 muxer_stream_t* mux;
330 AVCodecContext *context;
331 AVFrame *pic;
332 AVCodec *codec;
333 FILE *stats_file;
336 #define stats_file (vf->priv->stats_file)
337 #define mux_v (vf->priv->mux)
338 #define lavc_venc_context (vf->priv->context)
340 #if LIBAVCODEC_BUILD < 4684
341 #define FF_QP2LAMBDA 1
342 #endif
344 static int encode_frame(struct vf_instance_s* vf, AVFrame *pic, double pts);
346 static int config(struct vf_instance_s* vf,
347 int width, int height, int d_width, int d_height,
348 unsigned int flags, unsigned int outfmt){
349 int size, i;
350 void *p;
352 mux_v->bih->biWidth=width;
353 mux_v->bih->biHeight=height;
354 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
356 mp_msg(MSGT_MENCODER, MSGL_INFO,"videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
357 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression,
358 (char *)&mux_v->bih->biCompression);
360 lavc_venc_context->width = width;
361 lavc_venc_context->height = height;
362 if (lavc_param_vbitrate > 16000) /* != -1 */
363 lavc_venc_context->bit_rate = lavc_param_vbitrate;
364 else if (lavc_param_vbitrate >= 0) /* != -1 */
365 lavc_venc_context->bit_rate = lavc_param_vbitrate*1000;
366 else
367 lavc_venc_context->bit_rate = 800000; /* default */
369 //mux_v->wf->nAvgBytesPerSec= lavc_venc_context->bit_rate/8;
371 lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000;
372 #if LIBAVCODEC_BUILD >= 4754
373 lavc_venc_context->time_base= (AVRational){mux_v->h.dwScale, mux_v->h.dwRate};
374 #else
375 #if LIBAVCODEC_BUILD >= 4662
376 lavc_venc_context->frame_rate = mux_v->h.dwRate;
377 lavc_venc_context->frame_rate_base = mux_v->h.dwScale;
378 #else
379 lavc_venc_context->frame_rate = mux_v->h.dwRate*FRAME_RATE_BASE/mux_v->h.dwScale;
380 #endif
381 #endif
382 lavc_venc_context->qmin= lavc_param_vqmin;
383 lavc_venc_context->qmax= lavc_param_vqmax;
384 #if LIBAVCODEC_BUILD >= 4646
385 lavc_venc_context->mb_qmin= lavc_param_mb_qmin;
386 lavc_venc_context->mb_qmax= lavc_param_mb_qmax;
387 #endif
388 #if LIBAVCODEC_BUILD >= 4684
389 lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5);
390 lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5);
391 #endif
392 lavc_venc_context->max_qdiff= lavc_param_vqdiff;
393 lavc_venc_context->qcompress= lavc_param_vqcompress;
394 lavc_venc_context->qblur= lavc_param_vqblur;
395 lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames;
396 lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor;
397 lavc_venc_context->rc_strategy= lavc_param_vrc_strategy;
398 lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy;
399 lavc_venc_context->b_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vb_qoffset + 0.5);
400 lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold;
401 lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold;
402 lavc_venc_context->rtp_payload_size= lavc_param_packet_size;
403 if(lavc_param_packet_size )lavc_venc_context->rtp_mode=1;
404 lavc_venc_context->strict_std_compliance= lavc_param_strict;
405 lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor;
406 lavc_venc_context->i_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vi_qoffset + 0.5);
407 lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish;
408 lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp;
409 lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq;
410 lavc_venc_context->rc_eq= lavc_param_rc_eq;
412 mux_v->max_rate=
413 lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000;
414 lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000;
416 mux_v->vbv_size=
417 lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000;
419 lavc_venc_context->rc_initial_buffer_occupancy=
420 lavc_venc_context->rc_buffer_size *
421 lavc_param_rc_initial_buffer_occupancy;
422 lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity;
423 lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx;
424 #if LIBAVCODEC_BUILD >= 4642
425 lavc_venc_context->debug= lavc_param_debug;
426 #endif
427 #if LIBAVCODEC_BUILD >= 4649
428 lavc_venc_context->last_predictor_count= lavc_param_last_pred;
429 #endif
430 #if LIBAVCODEC_BUILD >= 4650
431 lavc_venc_context->pre_me= lavc_param_pre_me;
432 #endif
433 #if LIBAVCODEC_BUILD >= 4651
434 lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp;
435 lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size;
436 #endif
437 #if LIBAVCODEC_BUILD >= 4652
438 lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality;
439 #endif
440 #if LIBAVCODEC_BUILD >= 4659
441 lavc_venc_context->me_range= lavc_param_me_range;
442 #endif
443 #if LIBAVCODEC_BUILD >= 4663
444 lavc_venc_context->intra_quant_bias= lavc_param_ibias;
445 lavc_venc_context->inter_quant_bias= lavc_param_pbias;
446 #endif
447 #if LIBAVCODEC_BUILD >= 4669
448 lavc_venc_context->coder_type= lavc_param_coder;
449 lavc_venc_context->context_model= lavc_param_context;
450 #endif
451 #if LIBAVCODEC_BUILD >= 4680
452 lavc_venc_context->scenechange_threshold= lavc_param_sc_threshold;
453 #endif
454 #if LIBAVCODEC_BUILD >= 4690
455 lavc_venc_context->noise_reduction= lavc_param_noise_reduction;
456 #endif
457 #if LIBAVCODEC_BUILD >= 4700
458 lavc_venc_context->quantizer_noise_shaping= lavc_param_qns;
459 #endif
460 #if LIBAVCODEC_BUILD >= 4693
461 lavc_venc_context->inter_threshold= lavc_param_inter_threshold;
462 #endif
463 #if LIBAVCODEC_BUILD >= 4716
464 lavc_venc_context->nsse_weight= lavc_param_nssew;
465 #endif
466 #if LIBAVCODEC_BUILD >= 4675
467 if (lavc_param_intra_matrix)
469 char *tmp;
471 lavc_venc_context->intra_matrix =
472 av_malloc(sizeof(*lavc_venc_context->intra_matrix)*64);
474 i = 0;
475 while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64))
477 if (!tmp || (tmp && !strlen(tmp)))
478 break;
479 lavc_venc_context->intra_matrix[i++] = atoi(tmp);
482 if (i != 64)
483 av_freep(&lavc_venc_context->intra_matrix);
484 else
485 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n");
487 if (lavc_param_inter_matrix)
489 char *tmp;
491 lavc_venc_context->inter_matrix =
492 av_malloc(sizeof(*lavc_venc_context->inter_matrix)*64);
494 i = 0;
495 while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64))
497 if (!tmp || (tmp && !strlen(tmp)))
498 break;
499 lavc_venc_context->inter_matrix[i++] = atoi(tmp);
502 if (i != 64)
503 av_freep(&lavc_venc_context->inter_matrix);
504 else
505 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n");
507 #endif
509 p= lavc_param_rc_override_string;
510 for(i=0; p; i++){
511 int start, end, q;
512 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
513 if(e!=3){
514 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n");
515 return 0;
517 lavc_venc_context->rc_override=
518 realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1));
519 lavc_venc_context->rc_override[i].start_frame= start;
520 lavc_venc_context->rc_override[i].end_frame = end;
521 if(q>0){
522 lavc_venc_context->rc_override[i].qscale= q;
523 lavc_venc_context->rc_override[i].quality_factor= 1.0;
525 else{
526 lavc_venc_context->rc_override[i].qscale= 0;
527 lavc_venc_context->rc_override[i].quality_factor= -q/100.0;
529 p= strchr(p, '/');
530 if(p) p++;
532 lavc_venc_context->rc_override_count=i;
534 lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant;
536 lavc_venc_context->dct_algo= lavc_param_fdct;
537 lavc_venc_context->idct_algo= lavc_param_idct;
539 lavc_venc_context->lumi_masking= lavc_param_lumi_masking;
540 lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking;
541 lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking;
542 lavc_venc_context->p_masking= lavc_param_p_masking;
543 lavc_venc_context->dark_masking= lavc_param_dark_masking;
544 #if LIBAVCODEC_BUILD >= 4741
545 lavc_venc_context->border_masking = lavc_param_border_masking;
546 #endif
548 if (lavc_param_aspect != NULL)
550 int par_width, par_height, e;
551 float ratio=0;
553 e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height);
554 if(e==2){
555 if(par_height)
556 ratio= (float)par_width / (float)par_height;
557 }else{
558 e= sscanf (lavc_param_aspect, "%f", &ratio);
561 if (e && ratio > 0.1 && ratio < 10.0) {
562 #if LIBAVCODEC_BUILD >= 4687
563 lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255);
564 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n",
565 lavc_venc_context->sample_aspect_ratio.num,
566 lavc_venc_context->sample_aspect_ratio.den);
567 #else
568 lavc_venc_context->aspect_ratio= ratio;
569 #endif
570 mux_v->aspect = ratio;
571 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio);
572 } else {
573 mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect);
574 return 0;
577 else if (lavc_param_autoaspect) {
578 #if LIBAVCODEC_BUILD >= 4687
579 lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255);
580 #else
581 lavc_venc_context->aspect_ratio =
582 #endif
583 mux_v->aspect = (float)d_width/d_height;
586 /* keyframe interval */
587 if (lavc_param_keyint >= 0) /* != -1 */
588 lavc_venc_context->gop_size = lavc_param_keyint;
589 else
590 lavc_venc_context->gop_size = 250; /* default */
592 lavc_venc_context->flags = 0;
593 if (lavc_param_mb_decision)
595 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_HighQualityEncodingSelected);
596 #if LIBAVCODEC_BUILD < 4673
597 lavc_venc_context->flags = CODEC_FLAG_HQ;
598 #else
599 lavc_venc_context->mb_decision= lavc_param_mb_decision;
600 #endif
603 #if LIBAVCODEC_BUILD >= 4647
604 lavc_venc_context->me_cmp= lavc_param_me_cmp;
605 lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp;
606 lavc_venc_context->mb_cmp= lavc_param_mb_cmp;
607 #ifdef FF_CMP_VSAD
608 lavc_venc_context->ildct_cmp= lavc_param_ildct_cmp;
609 #endif
610 lavc_venc_context->dia_size= lavc_param_dia_size;
611 lavc_venc_context->flags|= lavc_param_qpel;
612 #endif
613 #if LIBAVCODEC_BUILD >= 4648
614 lavc_venc_context->flags|= lavc_param_trell;
615 #endif
616 lavc_venc_context->flags|= lavc_param_bit_exact;
617 lavc_venc_context->flags|= lavc_param_aic;
618 lavc_venc_context->flags|= lavc_param_aiv;
619 lavc_venc_context->flags|= lavc_param_umv;
620 lavc_venc_context->flags|= lavc_param_obmc;
621 lavc_venc_context->flags|= lavc_param_loop;
622 lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0;
623 lavc_venc_context->flags|= lavc_param_data_partitioning;
624 lavc_venc_context->flags|= lavc_param_cbp;
625 lavc_venc_context->flags|= lavc_param_mv0;
626 lavc_venc_context->flags|= lavc_param_qp_rd;
627 lavc_venc_context->flags|= lavc_param_ss;
628 lavc_venc_context->flags|= lavc_param_alt;
629 lavc_venc_context->flags|= lavc_param_ilme;
630 #ifdef CODEC_FLAG_CLOSED_GOP
631 lavc_venc_context->flags|= lavc_param_closed_gop;
632 #endif
633 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY;
635 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP;
636 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT;
637 #if LIBAVCODEC_BUILD >= 4643
638 lavc_venc_context->flags|= lavc_param_psnr;
639 #endif
640 #if LIBAVCODEC_BUILD >= 4711
641 lavc_venc_context->intra_dc_precision = lavc_param_dc_precision - 8;
642 #endif
643 lavc_venc_context->prediction_method= lavc_param_prediction_method;
644 lavc_venc_context->brd_scale = lavc_param_brd_scale;
645 lavc_venc_context->bidir_refine = lavc_param_bidir_refine;
646 lavc_venc_context->scenechange_factor = lavc_param_sc_factor;
647 if((lavc_param_video_global_header&1)
648 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
649 lavc_venc_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
651 if(lavc_param_video_global_header&2){
652 lavc_venc_context->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
654 lavc_venc_context->mv0_threshold = lavc_param_mv0_threshold;
656 switch(lavc_param_format)
658 case IMGFMT_YV12:
659 lavc_venc_context->pix_fmt = PIX_FMT_YUV420P;
660 break;
661 case IMGFMT_422P:
662 lavc_venc_context->pix_fmt = PIX_FMT_YUV422P;
663 break;
664 case IMGFMT_444P:
665 lavc_venc_context->pix_fmt = PIX_FMT_YUV444P;
666 break;
667 case IMGFMT_411P:
668 lavc_venc_context->pix_fmt = PIX_FMT_YUV411P;
669 break;
670 case IMGFMT_YVU9:
671 lavc_venc_context->pix_fmt = PIX_FMT_YUV410P;
672 break;
673 case IMGFMT_BGR32:
674 lavc_venc_context->pix_fmt = PIX_FMT_RGBA32;
675 break;
676 default:
677 mp_msg(MSGT_MENCODER,MSGL_ERR,"%s is not a supported format\n", vo_format_name(lavc_param_format));
678 return 0;
681 if(!stats_file) {
682 /* lavc internal 2pass bitrate control */
683 switch(lavc_param_vpass){
684 case 2:
685 case 3:
686 lavc_venc_context->flags|= CODEC_FLAG_PASS2;
687 stats_file= fopen(passtmpfile, "rb");
688 if(stats_file==NULL){
689 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
690 return 0;
692 fseek(stats_file, 0, SEEK_END);
693 size= ftell(stats_file);
694 fseek(stats_file, 0, SEEK_SET);
696 lavc_venc_context->stats_in= av_malloc(size + 1);
697 lavc_venc_context->stats_in[size]=0;
699 if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){
700 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile);
701 return 0;
703 if(lavc_param_vpass == 2)
704 break;
705 else
706 fclose(stats_file);
707 /* fall through */
708 case 1:
709 lavc_venc_context->flags|= CODEC_FLAG_PASS1;
710 stats_file= fopen(passtmpfile, "wb");
711 if(stats_file==NULL){
712 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
713 return 0;
715 if(lavc_param_turbo && (lavc_param_vpass == 1)) {
716 /* uses SAD comparison functions instead of other hungrier */
717 lavc_venc_context->me_pre_cmp = 0;
718 lavc_venc_context->me_cmp = 0;
719 lavc_venc_context->me_sub_cmp = 0;
720 lavc_venc_context->mb_cmp = 2;
722 /* Disables diamond motion estimation */
723 lavc_venc_context->pre_dia_size = 0;
724 lavc_venc_context->dia_size = 1;
726 lavc_venc_context->quantizer_noise_shaping = 0; // qns=0
727 lavc_venc_context->noise_reduction = 0; // nr=0
728 lavc_venc_context->mb_decision = 0; // mbd=0 ("realtime" encoding)
730 lavc_venc_context->flags &= ~CODEC_FLAG_QPEL;
731 lavc_venc_context->flags &= ~CODEC_FLAG_4MV;
732 lavc_venc_context->flags &= ~CODEC_FLAG_TRELLIS_QUANT;
733 lavc_venc_context->flags &= ~CODEC_FLAG_CBP_RD;
734 lavc_venc_context->flags &= ~CODEC_FLAG_QP_RD;
735 lavc_venc_context->flags &= ~CODEC_FLAG_MV0;
737 break;
741 lavc_venc_context->me_method = ME_ZERO+lavc_param_vme;
743 /* fixed qscale :p */
744 if (lavc_param_vqscale)
746 mp_msg(MSGT_MENCODER, MSGL_INFO, MSGTR_MPCODECS_UsingConstantQscale, lavc_param_vqscale);
747 lavc_venc_context->flags |= CODEC_FLAG_QSCALE;
748 #if LIBAVCODEC_BUILD >= 4668
749 lavc_venc_context->global_quality=
750 #endif
751 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5);
754 #if LIBAVCODEC_BUILD >= 4716
755 if(lavc_param_threads > 1)
756 avcodec_thread_init(lavc_venc_context, lavc_param_threads);
757 #endif
759 if (avcodec_open(lavc_venc_context, vf->priv->codec) != 0) {
760 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
761 return 0;
764 if (lavc_venc_context->codec->encode == NULL) {
765 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
766 return 0;
769 /* free second pass buffer, its not needed anymore */
770 av_freep(&lavc_venc_context->stats_in);
771 if(lavc_venc_context->bits_per_sample)
772 mux_v->bih->biBitCount= lavc_venc_context->bits_per_sample;
773 if(lavc_venc_context->extradata_size){
774 mux_v->bih= realloc(mux_v->bih, sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size);
775 memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size);
776 mux_v->bih->biSize= sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size;
779 mux_v->decoder_delay = lavc_venc_context->max_b_frames ? 1 : 0;
781 return 1;
784 static int control(struct vf_instance_s* vf, int request, void* data){
786 switch(request){
787 case VFCTRL_FLUSH_FRAMES:
788 if(vf->priv->codec->capabilities & CODEC_CAP_DELAY)
789 while(encode_frame(vf, NULL, MP_NOPTS_VALUE) > 0);
790 return CONTROL_TRUE;
791 default:
792 return CONTROL_UNKNOWN;
796 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
797 switch(fmt){
798 case IMGFMT_YV12:
799 case IMGFMT_IYUV:
800 case IMGFMT_I420:
801 if(lavc_param_format == IMGFMT_YV12)
802 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
803 break;
804 case IMGFMT_411P:
805 if(lavc_param_format == IMGFMT_411P)
806 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
807 break;
808 case IMGFMT_422P:
809 if(lavc_param_format == IMGFMT_422P)
810 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
811 break;
812 case IMGFMT_444P:
813 if(lavc_param_format == IMGFMT_444P)
814 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
815 break;
816 case IMGFMT_YVU9:
817 if(lavc_param_format == IMGFMT_YVU9)
818 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
819 break;
820 case IMGFMT_BGR32:
821 if(lavc_param_format == IMGFMT_BGR32)
822 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
823 break;
825 return 0;
828 static double psnr(double d){
829 if(d==0) return INFINITY;
830 return -10.0*log(d)/log(10);
833 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){
834 AVFrame *pic= vf->priv->pic;
836 pic->data[0]=mpi->planes[0];
837 pic->data[1]=mpi->planes[1];
838 pic->data[2]=mpi->planes[2];
839 pic->linesize[0]=mpi->stride[0];
840 pic->linesize[1]=mpi->stride[1];
841 pic->linesize[2]=mpi->stride[2];
843 #if LIBAVCODEC_BUILD >= 4697
844 if(lavc_param_interlaced_dct){
845 if((mpi->fields & MP_IMGFIELD_ORDERED) && (mpi->fields & MP_IMGFIELD_INTERLACED))
846 pic->top_field_first= !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
847 else
848 pic->top_field_first= 1;
850 if(lavc_param_top!=-1)
851 pic->top_field_first= lavc_param_top;
853 #endif
855 return (encode_frame(vf, pic, pts) >= 0);
858 static int encode_frame(struct vf_instance_s* vf, AVFrame *pic, double pts){
859 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
860 int out_size;
861 double dts;
863 if(pic){
864 #if 0
865 pic->opaque= malloc(sizeof(pts));
866 memcpy(pic->opaque, &pts, sizeof(pts));
867 #else
868 if(pts != MP_NOPTS_VALUE)
869 pic->pts= floor(pts / av_q2d(lavc_venc_context->time_base) + 0.5);
870 else
871 pic->pts= MP_NOPTS_VALUE;
872 #endif
874 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
875 pic);
877 if(pts != MP_NOPTS_VALUE)
878 dts= pts - lavc_venc_context->delay * av_q2d(lavc_venc_context->time_base);
879 else
880 dts= MP_NOPTS_VALUE;
881 #if 0
882 pts= lavc_venc_context->coded_frame->opaque ?
883 *(double*)lavc_venc_context->coded_frame->opaque
884 : MP_NOPTS_VALUE;
885 #else
886 if(lavc_venc_context->coded_frame->pts != MP_NOPTS_VALUE)
887 pts= lavc_venc_context->coded_frame->pts * av_q2d(lavc_venc_context->time_base);
888 else
889 pts= MP_NOPTS_VALUE;
890 assert(MP_NOPTS_VALUE == AV_NOPTS_VALUE);
891 #endif
892 //fprintf(stderr, "ve_lavc %f/%f\n", dts, pts);
893 if(out_size == 0) {
894 ++mux_v->encoder_delay;
895 return 0;
898 muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0,
899 dts, pts);
900 free(lavc_venc_context->coded_frame->opaque);
901 lavc_venc_context->coded_frame->opaque= NULL;
903 #if LIBAVCODEC_BUILD >= 4643
904 /* store psnr / pict size / type / qscale */
905 if(lavc_param_psnr){
906 static FILE *fvstats=NULL;
907 char filename[20];
908 static long long int all_len=0;
909 static int frame_number=0;
910 static double all_frametime=0.0;
911 AVFrame *pic= lavc_venc_context->coded_frame;
912 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
913 double quality=0.0;
914 int8_t *q;
916 if(!fvstats) {
917 time_t today2;
918 struct tm *today;
919 today2 = time(NULL);
920 today = localtime(&today2);
921 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour,
922 today->tm_min, today->tm_sec);
923 fvstats = fopen(filename,"w");
924 if(!fvstats) {
925 perror("fopen");
926 lavc_param_psnr=0; // disable block
927 mp_msg(MSGT_MENCODER,MSGL_ERR,"Can't open %s for writing. Check its permissions.\n",filename);
928 return -1;
929 /*exit(1);*/
933 // average MB quantizer
934 q = lavc_venc_context->coded_frame->qscale_table;
935 if(q) {
936 int x, y;
937 int w = (lavc_venc_context->width+15) >> 4;
938 int h = (lavc_venc_context->height+15) >> 4;
939 for( y = 0; y < h; y++ ) {
940 for( x = 0; x < w; x++ )
941 quality += (double)*(q+x);
942 q += lavc_venc_context->coded_frame->qstride;
944 quality /= w * h;
945 } else
946 quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA;
948 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
949 lavc_venc_context->coded_frame->coded_picture_number,
950 quality,
951 out_size,
952 psnr(lavc_venc_context->coded_frame->error[0]/f),
953 psnr(lavc_venc_context->coded_frame->error[1]*4/f),
954 psnr(lavc_venc_context->coded_frame->error[2]*4/f),
955 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)),
956 pict_type_char[lavc_venc_context->coded_frame->pict_type]
959 #endif
960 /* store stats if there are any */
961 if(lavc_venc_context->stats_out && stats_file)
962 fprintf(stats_file, "%s", lavc_venc_context->stats_out);
963 return out_size;
966 static void uninit(struct vf_instance_s* vf){
967 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
969 #if LIBAVCODEC_BUILD >= 4643
970 if(lavc_param_psnr){
971 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
972 f*= lavc_venc_context->coded_frame->coded_picture_number;
974 mp_msg(MSGT_MENCODER, MSGL_INFO, "PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
975 psnr(lavc_venc_context->error[0]/f),
976 psnr(lavc_venc_context->error[1]*4/f),
977 psnr(lavc_venc_context->error[2]*4/f),
978 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5))
981 #endif
983 #if LIBAVCODEC_BUILD >= 4675
984 av_freep(&lavc_venc_context->intra_matrix);
985 av_freep(&lavc_venc_context->inter_matrix);
986 #endif
988 avcodec_close(lavc_venc_context);
990 if(stats_file) fclose(stats_file);
992 /* free rc_override */
993 av_freep(&lavc_venc_context->rc_override);
995 av_freep(&vf->priv->context);
998 //===========================================================================//
1000 static int vf_open(vf_instance_t *vf, char* args){
1001 vf->uninit=uninit;
1002 vf->config=config;
1003 vf->default_caps=VFCAP_CONSTANT;
1004 vf->control=control;
1005 vf->query_format=query_format;
1006 vf->put_image=put_image;
1007 vf->priv=malloc(sizeof(struct vf_priv_s));
1008 memset(vf->priv,0,sizeof(struct vf_priv_s));
1009 vf->priv->mux=(muxer_stream_t*)args;
1011 /* XXX: hack: some of the MJPEG decoder DLL's needs exported huffman
1012 table, so we define a zero-table, also lavc mjpeg encoder is putting
1013 huffman tables into the stream, so no problem */
1014 if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "mjpeg"))
1016 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+28);
1017 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+28);
1018 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+28;
1020 else if (lavc_param_vcodec && (!strcasecmp(lavc_param_vcodec, "huffyuv")
1021 || !strcasecmp(lavc_param_vcodec, "ffvhuff")))
1023 /* XXX: hack: huffyuv needs to store huffman tables (allthough we dunno the size yet ...) */
1024 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+1000);
1025 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+1000);
1026 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+1000;
1028 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv1"))
1030 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8);
1031 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8);
1032 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
1034 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv2"))
1036 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8);
1037 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8);
1038 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
1040 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "wmv2"))
1042 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+4);
1043 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+4);
1044 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+4;
1046 else
1048 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
1049 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER));
1050 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
1052 mux_v->bih->biWidth=0;
1053 mux_v->bih->biHeight=0;
1054 mux_v->bih->biPlanes=1;
1055 mux_v->bih->biBitCount=24;
1056 if (!lavc_param_vcodec)
1058 printf("No libavcodec codec specified! It's required!\n");
1059 return 0;
1062 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video"))
1063 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1');
1064 else if (!strcasecmp(lavc_param_vcodec, "mpeg2") || !strcasecmp(lavc_param_vcodec, "mpeg2video"))
1065 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '2');
1066 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p"))
1067 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3');
1068 else if (!strcasecmp(lavc_param_vcodec, "rv10"))
1069 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0');
1070 else if (!strcasecmp(lavc_param_vcodec, "mjpeg"))
1071 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G');
1072 else if (!strcasecmp(lavc_param_vcodec, "ljpeg"))
1073 mux_v->bih->biCompression = mmioFOURCC('L', 'J', 'P', 'G');
1074 else if (!strcasecmp(lavc_param_vcodec, "mpeg4"))
1075 mux_v->bih->biCompression = mmioFOURCC('F', 'M', 'P', '4');
1076 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4"))
1077 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3');
1078 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2"))
1079 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2');
1080 else if (!strcasecmp(lavc_param_vcodec, "wmv1"))
1081 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1');
1082 else if (!strcasecmp(lavc_param_vcodec, "wmv2"))
1083 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '2');
1084 else if (!strcasecmp(lavc_param_vcodec, "huffyuv"))
1085 mux_v->bih->biCompression = mmioFOURCC('H', 'F', 'Y', 'U');
1086 else if (!strcasecmp(lavc_param_vcodec, "ffvhuff"))
1087 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', 'H');
1088 else if (!strcasecmp(lavc_param_vcodec, "asv1"))
1089 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '1');
1090 else if (!strcasecmp(lavc_param_vcodec, "asv2"))
1091 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '2');
1092 else if (!strcasecmp(lavc_param_vcodec, "ffv1"))
1093 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', '1');
1094 else if (!strcasecmp(lavc_param_vcodec, "snow"))
1095 mux_v->bih->biCompression = mmioFOURCC('S', 'N', 'O', 'W');
1096 else
1097 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0],
1098 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */
1100 if (!avcodec_inited){
1101 avcodec_init();
1102 avcodec_register_all();
1103 avcodec_inited=1;
1106 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec);
1107 if (!vf->priv->codec) {
1108 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec);
1109 return 0;
1112 #if LIBAVCODEC_BUILD >= 4645
1113 vf->priv->pic = avcodec_alloc_frame();
1114 #else
1115 vf->priv->pic = avcodec_alloc_picture();
1116 #endif
1117 vf->priv->context = avcodec_alloc_context();
1119 return 1;
1122 vf_info_t ve_info_lavc = {
1123 "libavcodec encoder",
1124 "lavc",
1125 "A'rpi, Alex, Michael",
1126 "for internal use by mencoder",
1127 vf_open
1130 //===========================================================================//