dx50 = DX50
[mplayer/glamo.git] / libmpcodecs / ve_lavc.c
blobd6a096f22aca5e807016a60d5a2cfc3bc416ee4c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <time.h>
7 #if !defined(INFINITY) && defined(HUGE_VAL)
8 #define INFINITY HUGE_VAL
9 #endif
11 #include "../config.h"
13 #ifdef USE_LIBAVCODEC
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 = 2;
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= 0;
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 int lavc_param_mpeg_quant=0;
97 static int lavc_param_fdct=0;
98 static int lavc_param_idct=0;
99 static char* lavc_param_aspect = NULL;
100 static int lavc_param_autoaspect = 0;
101 static float lavc_param_lumi_masking= 0.0;
102 static float lavc_param_dark_masking= 0.0;
103 static float lavc_param_temporal_cplx_masking= 0.0;
104 static float lavc_param_spatial_cplx_masking= 0.0;
105 static float lavc_param_p_masking= 0.0;
106 static int lavc_param_normalize_aqp= 0;
107 static int lavc_param_interlaced_dct= 0;
108 static int lavc_param_prediction_method= FF_PRED_LEFT;
109 static int lavc_param_format= IMGFMT_YV12;
110 static int lavc_param_debug= 0;
111 static int lavc_param_psnr= 0;
112 static int lavc_param_me_pre_cmp= 0;
113 static int lavc_param_me_cmp= 0;
114 static int lavc_param_me_sub_cmp= 0;
115 static int lavc_param_mb_cmp= 0;
116 static int lavc_param_pre_dia_size= 0;
117 static int lavc_param_dia_size= 0;
118 static int lavc_param_qpel= 0;
119 static int lavc_param_trell= 0;
120 static int lavc_param_aic= 0;
121 static int lavc_param_umv= 0;
122 static int lavc_param_last_pred= 0;
123 static int lavc_param_pre_me= 1;
124 static int lavc_param_me_subpel_quality= 8;
125 static int lavc_param_me_range= 0;
126 #if LIBAVCODEC_BUILD >= 4663
127 static int lavc_param_ibias= FF_DEFAULT_QUANT_BIAS;
128 static int lavc_param_pbias= FF_DEFAULT_QUANT_BIAS;
129 #endif
130 static int lavc_param_coder= 0;
131 static int lavc_param_context= 0;
132 static char *lavc_param_intra_matrix = NULL;
133 static char *lavc_param_inter_matrix = NULL;
134 static int lavc_param_cbp= 0;
135 static int lavc_param_mv0= 0;
136 static int lavc_param_noise_reduction= 0;
137 static int lavc_param_qp_rd= 0;
139 char *lavc_param_acodec = "mp2";
140 int lavc_param_atag = 0;
141 int lavc_param_abitrate = 224;
143 #include "m_option.h"
145 #ifdef USE_LIBAVCODEC
146 m_option_t lavcopts_conf[]={
147 {"acodec", &lavc_param_acodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
148 {"abitrate", &lavc_param_abitrate, CONF_TYPE_INT, CONF_RANGE, 1, 1000, NULL},
149 {"atag", &lavc_param_atag, CONF_TYPE_INT, CONF_RANGE, 0, 0xffff, NULL},
150 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
151 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
152 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
153 {"vhq", &lavc_param_mb_decision, CONF_TYPE_FLAG, 0, 0, 1, NULL},
154 {"mbd", &lavc_param_mb_decision, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
155 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
156 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 5, NULL},
157 {"vqscale", &lavc_param_vqscale, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
158 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
159 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
160 {"mbqmin", &lavc_param_mb_qmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
161 {"mbqmax", &lavc_param_mb_qmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
162 {"lmin", &lavc_param_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
163 {"lmax", &lavc_param_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
164 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
165 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
166 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
167 {"vb_qfactor", &lavc_param_vb_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
168 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL},
169 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
170 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
171 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
172 {"vb_qoffset", &lavc_param_vb_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
173 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
174 {"vcelim", &lavc_param_chroma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
175 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
176 {"vstrict", &lavc_param_strict, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
177 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
178 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL},
179 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
180 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL},
181 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
182 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
183 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
184 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
185 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL},
186 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL},
187 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL},
188 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
189 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
190 {"vrc_buf_size", &lavc_param_rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
191 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
192 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL},
193 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
194 {"aspect", &lavc_param_aspect, CONF_TYPE_STRING, 0, 0, 0, NULL},
195 {"autoaspect", &lavc_param_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL},
196 {"lumi_mask", &lavc_param_lumi_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
197 {"tcplx_mask", &lavc_param_temporal_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
198 {"scplx_mask", &lavc_param_spatial_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
199 {"p_mask", &lavc_param_p_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
200 {"naq", &lavc_param_normalize_aqp, CONF_TYPE_FLAG, 0, 0, 1, NULL},
201 {"dark_mask", &lavc_param_dark_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
202 {"ildct", &lavc_param_interlaced_dct, CONF_TYPE_FLAG, 0, 0, 1, NULL},
203 {"idct", &lavc_param_idct, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
204 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
205 {"format", &lavc_param_format, CONF_TYPE_IMGFMT, 0, 0, 0, NULL},
206 #if LIBAVCODEC_BUILD >= 4642
207 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
208 #endif
209 #if LIBAVCODEC_BUILD >= 4643
210 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL},
211 #endif
212 {"precmp", &lavc_param_me_pre_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
213 {"cmp", &lavc_param_me_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
214 {"subcmp", &lavc_param_me_sub_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
215 {"mbcmp", &lavc_param_mb_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
216 {"predia", &lavc_param_pre_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
217 {"dia", &lavc_param_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
218 {"qpel", &lavc_param_qpel, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QPEL, NULL},
219 #if LIBAVCODEC_BUILD >= 4648
220 {"trell", &lavc_param_trell, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_TRELLIS_QUANT, NULL},
221 #endif
222 {"last_pred", &lavc_param_last_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
223 {"preme", &lavc_param_pre_me, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
224 {"subq", &lavc_param_me_subpel_quality, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
225 {"me_range", &lavc_param_me_range, CONF_TYPE_INT, CONF_RANGE, 0, 16000, NULL},
226 #ifdef CODEC_FLAG_H263P_AIC
227 {"aic", &lavc_param_aic, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIC, NULL},
228 {"umv", &lavc_param_umv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_UMV, NULL},
229 #endif
230 #if LIBAVCODEC_BUILD >= 4663
231 {"ibias", &lavc_param_ibias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
232 {"pbias", &lavc_param_pbias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
233 #endif
234 #if LIBAVCODEC_BUILD >= 4669
235 {"coder", &lavc_param_coder, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
236 {"context", &lavc_param_context, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
237 #endif
238 #if LIBAVCODEC_BUILD >= 4675
239 {"intra_matrix", &lavc_param_intra_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
240 {"inter_matrix", &lavc_param_inter_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
241 #endif
242 #if LIBAVCODEC_BUILD >= 4681
243 {"cbp", &lavc_param_cbp, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CBP_RD, NULL},
244 #endif
245 #if LIBAVCODEC_BUILD >= 4683
246 {"mv0", &lavc_param_mv0, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_MV0, NULL},
247 #endif
248 {"nr", &lavc_param_noise_reduction, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
249 #ifdef CODEC_FLAG_QP_RD
250 {"qprd", &lavc_param_qp_rd, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QP_RD, NULL},
251 #endif
252 {NULL, NULL, 0, 0, 0, 0, NULL}
254 #endif
256 struct vf_priv_s {
257 muxer_stream_t* mux;
258 AVCodecContext *context;
259 AVFrame *pic;
260 AVCodec *codec;
261 FILE *stats_file;
264 #define stats_file (vf->priv->stats_file)
265 #define mux_v (vf->priv->mux)
266 #define lavc_venc_context (vf->priv->context)
268 #if LIBAVCODEC_BUILD < 4684
269 #define FF_QP2LAMBDA 1
270 #endif
272 static int config(struct vf_instance_s* vf,
273 int width, int height, int d_width, int d_height,
274 unsigned int flags, unsigned int outfmt){
275 int size, i;
276 void *p;
278 mux_v->bih->biWidth=width;
279 mux_v->bih->biHeight=height;
280 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
282 printf("videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
283 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression,
284 (char *)&mux_v->bih->biCompression);
286 lavc_venc_context->width = width;
287 lavc_venc_context->height = height;
288 if (lavc_param_vbitrate > 16000) /* != -1 */
289 lavc_venc_context->bit_rate = lavc_param_vbitrate;
290 else if (lavc_param_vbitrate >= 0) /* != -1 */
291 lavc_venc_context->bit_rate = lavc_param_vbitrate*1000;
292 else
293 lavc_venc_context->bit_rate = 800000; /* default */
294 lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000;
295 #if LIBAVCODEC_BUILD >= 4662
296 lavc_venc_context->frame_rate = mux_v->h.dwRate;
297 lavc_venc_context->frame_rate_base = mux_v->h.dwScale;
298 #else
299 lavc_venc_context->frame_rate = mux_v->h.dwRate*FRAME_RATE_BASE/mux_v->h.dwScale;
300 #endif
301 lavc_venc_context->qmin= lavc_param_vqmin;
302 lavc_venc_context->qmax= lavc_param_vqmax;
303 #if LIBAVCODEC_BUILD >= 4646
304 lavc_venc_context->mb_qmin= lavc_param_mb_qmin;
305 lavc_venc_context->mb_qmax= lavc_param_mb_qmax;
306 #endif
307 #if LIBAVCODEC_BUILD >= 4684
308 lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5);
309 lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5);
310 #endif
311 lavc_venc_context->max_qdiff= lavc_param_vqdiff;
312 lavc_venc_context->qcompress= lavc_param_vqcompress;
313 lavc_venc_context->qblur= lavc_param_vqblur;
314 lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames;
315 lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor;
316 lavc_venc_context->rc_strategy= lavc_param_vrc_strategy;
317 lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy;
318 lavc_venc_context->b_quant_offset= lavc_param_vb_qoffset;
319 lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold;
320 lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold;
321 lavc_venc_context->rtp_payload_size= lavc_param_packet_size;
322 if(lavc_param_packet_size )lavc_venc_context->rtp_mode=1;
323 lavc_venc_context->strict_std_compliance= lavc_param_strict;
324 lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor;
325 lavc_venc_context->i_quant_offset= lavc_param_vi_qoffset;
326 lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish;
327 lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp;
328 lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq;
329 lavc_venc_context->rc_eq= lavc_param_rc_eq;
330 lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000;
331 lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000;
332 lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000;
333 lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity;
334 lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx;
335 #if LIBAVCODEC_BUILD >= 4642
336 lavc_venc_context->debug= lavc_param_debug;
337 #endif
338 #if LIBAVCODEC_BUILD >= 4649
339 lavc_venc_context->last_predictor_count= lavc_param_last_pred;
340 #endif
341 #if LIBAVCODEC_BUILD >= 4650
342 lavc_venc_context->pre_me= lavc_param_pre_me;
343 #endif
344 #if LIBAVCODEC_BUILD >= 4651
345 lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp;
346 lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size;
347 #endif
348 #if LIBAVCODEC_BUILD >= 4652
349 lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality;
350 #endif
351 #if LIBAVCODEC_BUILD >= 4659
352 lavc_venc_context->me_range= lavc_param_me_range;
353 #endif
354 #if LIBAVCODEC_BUILD >= 4663
355 lavc_venc_context->intra_quant_bias= lavc_param_ibias;
356 lavc_venc_context->inter_quant_bias= lavc_param_pbias;
357 #endif
358 #if LIBAVCODEC_BUILD >= 4669
359 lavc_venc_context->coder_type= lavc_param_coder;
360 lavc_venc_context->context_model= lavc_param_context;
361 #endif
362 #if LIBAVCODEC_BUILD >= 4690
363 lavc_venc_context->noise_reduction= lavc_param_noise_reduction;
364 #endif
365 #if LIBAVCODEC_BUILD >= 4675
366 if (lavc_param_intra_matrix)
368 char *tmp;
370 lavc_venc_context->intra_matrix =
371 malloc(sizeof(*lavc_venc_context->intra_matrix)*64);
373 i = 0;
374 while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64))
376 if (!tmp || (tmp && !strlen(tmp)))
377 break;
378 lavc_venc_context->intra_matrix[i++] = atoi(tmp);
381 if (i != 64)
383 free(lavc_venc_context->intra_matrix);
384 lavc_venc_context->intra_matrix = NULL;
386 else
387 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n");
389 if (lavc_param_inter_matrix)
391 char *tmp;
393 lavc_venc_context->inter_matrix =
394 malloc(sizeof(*lavc_venc_context->inter_matrix)*64);
396 i = 0;
397 while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64))
399 if (!tmp || (tmp && !strlen(tmp)))
400 break;
401 lavc_venc_context->inter_matrix[i++] = atoi(tmp);
404 if (i != 64)
406 free(lavc_venc_context->inter_matrix);
407 lavc_venc_context->inter_matrix = NULL;
409 else
410 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n");
412 #endif
414 p= lavc_param_rc_override_string;
415 for(i=0; p; i++){
416 int start, end, q;
417 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
418 if(e!=3){
419 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n");
420 return 0;
422 lavc_venc_context->rc_override=
423 realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1));
424 lavc_venc_context->rc_override[i].start_frame= start;
425 lavc_venc_context->rc_override[i].end_frame = end;
426 if(q>0){
427 lavc_venc_context->rc_override[i].qscale= q;
428 lavc_venc_context->rc_override[i].quality_factor= 1.0;
430 else{
431 lavc_venc_context->rc_override[i].qscale= 0;
432 lavc_venc_context->rc_override[i].quality_factor= -q/100.0;
434 p= strchr(p, '/');
435 if(p) p++;
437 lavc_venc_context->rc_override_count=i;
439 lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant;
441 lavc_venc_context->dct_algo= lavc_param_fdct;
442 lavc_venc_context->idct_algo= lavc_param_idct;
444 lavc_venc_context->lumi_masking= lavc_param_lumi_masking;
445 lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking;
446 lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking;
447 lavc_venc_context->p_masking= lavc_param_p_masking;
448 lavc_venc_context->dark_masking= lavc_param_dark_masking;
450 if (lavc_param_aspect != NULL)
452 int par_width, par_height, e;
453 float ratio=0;
455 e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height);
456 if(e==2){
457 if(par_height)
458 ratio= (float)par_width / (float)par_height;
459 }else{
460 e= sscanf (lavc_param_aspect, "%f", &ratio);
463 if (e && ratio > 0.1 && ratio < 10.0) {
464 #if LIBAVCODEC_BUILD >= 4687
465 lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255);
466 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n",
467 lavc_venc_context->sample_aspect_ratio.num,
468 lavc_venc_context->sample_aspect_ratio.den);
469 #else
470 lavc_venc_context->aspect_ratio= ratio;
471 #endif
472 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio);
473 } else {
474 mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect);
475 return 0;
478 else if (lavc_param_autoaspect)
479 #if LIBAVCODEC_BUILD >= 4687
480 lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255);
481 #else
482 lavc_venc_context->aspect_ratio = (float)d_width/d_height;
483 #endif
485 /* keyframe interval */
486 if (lavc_param_keyint >= 0) /* != -1 */
487 lavc_venc_context->gop_size = lavc_param_keyint;
488 else
489 lavc_venc_context->gop_size = 250; /* default */
491 lavc_venc_context->flags = 0;
492 if (lavc_param_mb_decision)
494 printf("High quality encoding selected (non real time)!\n");
495 #if LIBAVCODEC_BUILD < 4673
496 lavc_venc_context->flags = CODEC_FLAG_HQ;
497 #else
498 lavc_venc_context->mb_decision= lavc_param_mb_decision;
499 #endif
502 #if LIBAVCODEC_BUILD >= 4647
503 lavc_venc_context->me_cmp= lavc_param_me_cmp;
504 lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp;
505 lavc_venc_context->mb_cmp= lavc_param_mb_cmp;
506 lavc_venc_context->dia_size= lavc_param_dia_size;
507 lavc_venc_context->flags|= lavc_param_qpel;
508 #endif
509 #if LIBAVCODEC_BUILD >= 4648
510 lavc_venc_context->flags|= lavc_param_trell;
511 #endif
512 lavc_venc_context->flags|= lavc_param_aic;
513 lavc_venc_context->flags|= lavc_param_umv;
514 lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0;
515 lavc_venc_context->flags|= lavc_param_data_partitioning;
516 lavc_venc_context->flags|= lavc_param_cbp;
517 lavc_venc_context->flags|= lavc_param_mv0;
518 lavc_venc_context->flags|= lavc_param_qp_rd;
519 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY;
521 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP;
522 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT;
523 #if LIBAVCODEC_BUILD >= 4643
524 lavc_venc_context->flags|= lavc_param_psnr;
525 #endif
526 lavc_venc_context->prediction_method= lavc_param_prediction_method;
527 switch(lavc_param_format)
529 case IMGFMT_YV12:
530 lavc_venc_context->pix_fmt = PIX_FMT_YUV420P;
531 break;
532 case IMGFMT_422P:
533 lavc_venc_context->pix_fmt = PIX_FMT_YUV422P;
534 break;
535 case IMGFMT_444P:
536 lavc_venc_context->pix_fmt = PIX_FMT_YUV444P;
537 break;
538 case IMGFMT_411P:
539 lavc_venc_context->pix_fmt = PIX_FMT_YUV411P;
540 break;
541 case IMGFMT_YVU9:
542 lavc_venc_context->pix_fmt = PIX_FMT_YUV410P;
543 break;
544 case IMGFMT_BGR32:
545 lavc_venc_context->pix_fmt = PIX_FMT_RGBA32;
546 break;
547 default:
548 mp_msg(MSGT_MENCODER,MSGL_ERR,"%s is not a supported format\n", vo_format_name(lavc_param_format));
549 return 0;
552 if(!stats_file) {
553 /* lavc internal 2pass bitrate control */
554 switch(lavc_param_vpass){
555 case 1:
556 lavc_venc_context->flags|= CODEC_FLAG_PASS1;
557 stats_file= fopen(passtmpfile, "wb");
558 if(stats_file==NULL){
559 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
560 return 0;
562 break;
563 case 2:
564 lavc_venc_context->flags|= CODEC_FLAG_PASS2;
565 stats_file= fopen(passtmpfile, "rb");
566 if(stats_file==NULL){
567 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
568 return 0;
570 fseek(stats_file, 0, SEEK_END);
571 size= ftell(stats_file);
572 fseek(stats_file, 0, SEEK_SET);
574 lavc_venc_context->stats_in= malloc(size + 1);
575 lavc_venc_context->stats_in[size]=0;
577 if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){
578 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile);
579 return 0;
581 break;
585 lavc_venc_context->me_method = ME_ZERO+lavc_param_vme;
587 /* fixed qscale :p */
588 if (lavc_param_vqscale)
590 printf("Using constant qscale = %f (VBR)\n", lavc_param_vqscale);
591 lavc_venc_context->flags |= CODEC_FLAG_QSCALE;
592 #if LIBAVCODEC_BUILD >= 4668
593 lavc_venc_context->global_quality=
594 #endif
595 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5);
598 if (avcodec_open(lavc_venc_context, vf->priv->codec) != 0) {
599 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_CantOpenCodec);
600 return 0;
603 if (lavc_venc_context->codec->encode == NULL) {
604 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
605 return 0;
608 /* free second pass buffer, its not needed anymore */
609 if(lavc_venc_context->stats_in) free(lavc_venc_context->stats_in);
610 lavc_venc_context->stats_in= NULL;
611 if(lavc_venc_context->bits_per_sample)
612 mux_v->bih->biBitCount= lavc_venc_context->bits_per_sample;
613 if(lavc_venc_context->extradata_size){
614 memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size);
615 mux_v->bih->biSize= sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size;
618 return 1;
621 static int control(struct vf_instance_s* vf, int request, void* data){
623 return CONTROL_UNKNOWN;
626 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
627 switch(fmt){
628 case IMGFMT_YV12:
629 case IMGFMT_IYUV:
630 case IMGFMT_I420:
631 if(lavc_param_format == IMGFMT_YV12)
632 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
633 break;
634 case IMGFMT_411P:
635 if(lavc_param_format == IMGFMT_411P)
636 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
637 break;
638 case IMGFMT_422P:
639 if(lavc_param_format == IMGFMT_422P)
640 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
641 break;
642 case IMGFMT_444P:
643 if(lavc_param_format == IMGFMT_444P)
644 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
645 break;
646 case IMGFMT_YVU9:
647 if(lavc_param_format == IMGFMT_YVU9)
648 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
649 break;
650 case IMGFMT_BGR32:
651 if(lavc_param_format == IMGFMT_BGR32)
652 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
653 break;
655 return 0;
658 static double psnr(double d){
659 if(d==0) return INFINITY;
660 return -10.0*log(d)/log(10);
663 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
664 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
665 int out_size;
666 AVFrame *pic= vf->priv->pic;
668 pic->data[0]=mpi->planes[0];
669 pic->data[1]=mpi->planes[1];
670 pic->data[2]=mpi->planes[2];
671 pic->linesize[0]=mpi->stride[0];
672 pic->linesize[1]=mpi->stride[1];
673 pic->linesize[2]=mpi->stride[2];
675 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
676 pic);
679 muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0);
681 #if LIBAVCODEC_BUILD >= 4643
682 /* store psnr / pict size / type / qscale */
683 if(lavc_param_psnr){
684 static FILE *fvstats=NULL;
685 char filename[20];
686 static long long int all_len=0;
687 static int frame_number=0;
688 static double all_frametime=0.0;
689 AVFrame *pic= lavc_venc_context->coded_frame;
690 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
691 double quality=0.0;
692 int8_t *q;
694 if(!fvstats) {
695 time_t today2;
696 struct tm *today;
697 today2 = time(NULL);
698 today = localtime(&today2);
699 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour,
700 today->tm_min, today->tm_sec);
701 fvstats = fopen(filename,"w");
702 if(!fvstats) {
703 perror("fopen");
704 lavc_param_psnr=0; // disable block
705 /*exit(1);*/
709 // average MB quantizer
710 q = lavc_venc_context->coded_frame->qscale_table;
711 if(q) {
712 int x, y;
713 int w = (lavc_venc_context->width+15) >> 4;
714 int h = (lavc_venc_context->height+15) >> 4;
715 for( y = 0; y < h; y++ ) {
716 for( x = 0; x < w; x++ )
717 quality += (double)*(q+x);
718 q += lavc_venc_context->coded_frame->qstride;
720 quality /= w * h;
721 } else
722 quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA;
724 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
725 lavc_venc_context->coded_frame->coded_picture_number,
726 quality,
727 out_size,
728 psnr(lavc_venc_context->coded_frame->error[0]/f),
729 psnr(lavc_venc_context->coded_frame->error[1]*4/f),
730 psnr(lavc_venc_context->coded_frame->error[2]*4/f),
731 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)),
732 pict_type_char[lavc_venc_context->coded_frame->pict_type]
735 #endif
736 /* store stats if there are any */
737 if(lavc_venc_context->stats_out && stats_file)
738 fprintf(stats_file, "%s", lavc_venc_context->stats_out);
739 return 1;
742 static void uninit(struct vf_instance_s* vf){
743 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
745 #if LIBAVCODEC_BUILD >= 4643
746 if(lavc_param_psnr){
747 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
748 f*= lavc_venc_context->coded_frame->coded_picture_number;
750 printf("PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
751 psnr(lavc_venc_context->error[0]/f),
752 psnr(lavc_venc_context->error[1]*4/f),
753 psnr(lavc_venc_context->error[2]*4/f),
754 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5))
757 #endif
759 #if LIBAVCODEC_BUILD >= 4675
760 if (lavc_venc_context->intra_matrix)
761 free(lavc_venc_context->intra_matrix);
762 lavc_venc_context->intra_matrix = NULL;
763 if (lavc_venc_context->inter_matrix)
764 free(lavc_venc_context->inter_matrix);
765 lavc_venc_context->inter_matrix = NULL;
766 #endif
768 avcodec_close(lavc_venc_context);
770 if(stats_file) fclose(stats_file);
772 /* free rc_override */
773 if(lavc_venc_context->rc_override) free(lavc_venc_context->rc_override);
774 lavc_venc_context->rc_override= NULL;
776 if(vf->priv->context) free(vf->priv->context);
777 vf->priv->context= NULL;
780 //===========================================================================//
782 static int vf_open(vf_instance_t *vf, char* args){
783 vf->uninit=uninit;
784 vf->config=config;
785 vf->control=control;
786 vf->query_format=query_format;
787 vf->put_image=put_image;
788 vf->priv=malloc(sizeof(struct vf_priv_s));
789 memset(vf->priv,0,sizeof(struct vf_priv_s));
790 vf->priv->mux=(muxer_stream_t*)args;
792 /* XXX: hack: some of the MJPEG decoder DLL's needs exported huffman
793 table, so we define a zero-table, also lavc mjpeg encoder is putting
794 huffman tables into the stream, so no problem */
795 if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "mjpeg"))
797 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+28);
798 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+28);
799 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+28;
801 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "huffyuv"))
803 /* XXX: hack: huffyuv needs to store huffman tables (allthough we dunno the size yet ...) */
804 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+1000);
805 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+1000);
806 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+1000;
808 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv1"))
810 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8);
811 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8);
812 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
814 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv2"))
816 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+8);
817 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+8);
818 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
820 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "wmv2"))
822 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)+4);
823 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER)+4);
824 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+4;
826 else
828 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER));
829 memset(mux_v->bih, 0, sizeof(BITMAPINFOHEADER));
830 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
832 mux_v->bih->biWidth=0;
833 mux_v->bih->biHeight=0;
834 mux_v->bih->biPlanes=1;
835 mux_v->bih->biBitCount=24;
836 if (!lavc_param_vcodec)
838 printf("No libavcodec codec specified! It's required!\n");
839 return 0;
842 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video"))
843 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1');
844 else if (!strcasecmp(lavc_param_vcodec, "mpeg2") || !strcasecmp(lavc_param_vcodec, "mpeg2video"))
845 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '2');
846 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p"))
847 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3');
848 else if (!strcasecmp(lavc_param_vcodec, "rv10"))
849 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0');
850 else if (!strcasecmp(lavc_param_vcodec, "mjpeg"))
851 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G');
852 else if (!strcasecmp(lavc_param_vcodec, "ljpeg"))
853 mux_v->bih->biCompression = mmioFOURCC('L', 'J', 'P', 'G');
854 else if (!strcasecmp(lavc_param_vcodec, "mpeg4"))
855 mux_v->bih->biCompression = mmioFOURCC('D', 'I', 'V', 'X');
856 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4"))
857 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3');
858 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2"))
859 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2');
860 else if (!strcasecmp(lavc_param_vcodec, "wmv1"))
861 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1');
862 else if (!strcasecmp(lavc_param_vcodec, "wmv2"))
863 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '2');
864 else if (!strcasecmp(lavc_param_vcodec, "huffyuv"))
865 mux_v->bih->biCompression = mmioFOURCC('H', 'F', 'Y', 'U');
866 else if (!strcasecmp(lavc_param_vcodec, "asv1"))
867 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '1');
868 else if (!strcasecmp(lavc_param_vcodec, "asv2"))
869 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '2');
870 else if (!strcasecmp(lavc_param_vcodec, "ffv1"))
871 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', '1');
872 else
873 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0],
874 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */
876 if (!avcodec_inited){
877 avcodec_init();
878 avcodec_register_all();
879 avcodec_inited=1;
882 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec);
883 if (!vf->priv->codec) {
884 mp_msg(MSGT_MENCODER,MSGL_ERR,MSGTR_MissingLAVCcodec, lavc_param_vcodec);
885 return 0;
888 #if LIBAVCODEC_BUILD >= 4645
889 vf->priv->pic = avcodec_alloc_frame();
890 #else
891 vf->priv->pic = avcodec_alloc_picture();
892 #endif
893 vf->priv->context = avcodec_alloc_context();
895 return 1;
898 vf_info_t ve_info_lavc = {
899 "libavcodec encoder",
900 "lavc",
901 "A'rpi, Alex, Michael",
902 "for internal use by mencoder",
903 vf_open
906 //===========================================================================//
907 #endif