stream/tv: move new_handle() function from header to tv.c
[mplayer.git] / libmpcodecs / ve_lavc.c
blob258dbf2cc5545febe63bffc3dc7844cd21fc5848
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 "av_opts.h"
35 #include "osdep/strsep.h"
37 #include "codec-cfg.h"
38 #include "stream/stream.h"
39 #include "libmpdemux/demuxer.h"
40 #include "libmpdemux/stheader.h"
42 #include "libmpdemux/muxer.h"
44 #include "img_format.h"
45 #include "fmt-conversion.h"
46 #include "mp_image.h"
47 #include "vf.h"
48 #include "vd_ffmpeg.h"
50 extern char* passtmpfile;
52 //===========================================================================//
54 #include "libavcodec/avcodec.h"
57 /* video options */
58 static char *lavc_param_vcodec = "mpeg4";
59 static int lavc_param_vbitrate = -1;
60 static int lavc_param_vrate_tolerance = 1000*8;
61 static int lavc_param_mb_decision = 0; /* default is realtime encoding */
62 static int lavc_param_v4mv = 0;
63 static int lavc_param_vme = 4;
64 static float lavc_param_vqscale = -1;
65 static int lavc_param_vqmin = 2;
66 static int lavc_param_vqmax = 31;
67 static int lavc_param_mb_qmin = 2;
68 static int lavc_param_mb_qmax = 31;
69 static float lavc_param_lmin = 2;
70 static float lavc_param_lmax = 31;
71 static float lavc_param_mb_lmin = 2;
72 static float lavc_param_mb_lmax = 31;
73 static int lavc_param_vqdiff = 3;
74 static float lavc_param_vqcompress = 0.5;
75 static float lavc_param_vqblur = 0.5;
76 static float lavc_param_vb_qfactor = 1.25;
77 static float lavc_param_vb_qoffset = 1.25;
78 static float lavc_param_vi_qfactor = 0.8;
79 static float lavc_param_vi_qoffset = 0.0;
80 static int lavc_param_vmax_b_frames = 0;
81 static int lavc_param_keyint = -1;
82 static int lavc_param_vpass = 0;
83 static int lavc_param_vrc_strategy = 0;
84 static int lavc_param_vb_strategy = 0;
85 static int lavc_param_luma_elim_threshold = 0;
86 static int lavc_param_chroma_elim_threshold = 0;
87 static int lavc_param_packet_size= 0;
88 static int lavc_param_strict= -1;
89 static int lavc_param_data_partitioning= 0;
90 static int lavc_param_gray=0;
91 static float lavc_param_rc_qsquish=1.0;
92 static float lavc_param_rc_qmod_amp=0;
93 static int lavc_param_rc_qmod_freq=0;
94 static char *lavc_param_rc_override_string=NULL;
95 static char *lavc_param_rc_eq="tex^qComp";
96 static int lavc_param_rc_buffer_size=0;
97 static float lavc_param_rc_buffer_aggressivity=1.0;
98 static int lavc_param_rc_max_rate=0;
99 static int lavc_param_rc_min_rate=0;
100 static float lavc_param_rc_initial_cplx=0;
101 static float lavc_param_rc_initial_buffer_occupancy=0.9;
102 static int lavc_param_mpeg_quant=0;
103 static int lavc_param_fdct=0;
104 static int lavc_param_idct=0;
105 static char* lavc_param_aspect = NULL;
106 static int lavc_param_autoaspect = 0;
107 static float lavc_param_lumi_masking= 0.0;
108 static float lavc_param_dark_masking= 0.0;
109 static float lavc_param_temporal_cplx_masking= 0.0;
110 static float lavc_param_spatial_cplx_masking= 0.0;
111 static float lavc_param_p_masking= 0.0;
112 static float lavc_param_border_masking= 0.0;
113 static int lavc_param_normalize_aqp= 0;
114 static int lavc_param_interlaced_dct= 0;
115 static int lavc_param_prediction_method= FF_PRED_LEFT;
116 static int lavc_param_format= IMGFMT_YV12;
117 static int lavc_param_debug= 0;
118 static int lavc_param_psnr= 0;
119 static int lavc_param_me_pre_cmp= 0;
120 static int lavc_param_me_cmp= 0;
121 static int lavc_param_me_sub_cmp= 0;
122 static int lavc_param_mb_cmp= 0;
123 #ifdef FF_CMP_VSAD
124 static int lavc_param_ildct_cmp= FF_CMP_VSAD;
125 #endif
126 static int lavc_param_pre_dia_size= 0;
127 static int lavc_param_dia_size= 0;
128 static int lavc_param_qpel= 0;
129 static int lavc_param_trell= 0;
130 static int lavc_param_lowdelay= 0;
131 static int lavc_param_bit_exact = 0;
132 static int lavc_param_aic= 0;
133 static int lavc_param_aiv= 0;
134 static int lavc_param_umv= 0;
135 static int lavc_param_gmc= 0;
136 static int lavc_param_obmc= 0;
137 static int lavc_param_loop= 0;
138 static int lavc_param_last_pred= 0;
139 static int lavc_param_pre_me= 1;
140 static int lavc_param_me_subpel_quality= 8;
141 static int lavc_param_me_range= 0;
142 static int lavc_param_ibias= FF_DEFAULT_QUANT_BIAS;
143 static int lavc_param_pbias= FF_DEFAULT_QUANT_BIAS;
144 static int lavc_param_coder= 0;
145 static int lavc_param_context= 0;
146 static char *lavc_param_intra_matrix = NULL;
147 static char *lavc_param_inter_matrix = NULL;
148 static int lavc_param_cbp= 0;
149 static int lavc_param_mv0= 0;
150 static int lavc_param_noise_reduction= 0;
151 static int lavc_param_qns= 0;
152 static int lavc_param_qp_rd= 0;
153 static int lavc_param_inter_threshold= 0;
154 static int lavc_param_sc_threshold= 0;
155 static int lavc_param_ss= 0;
156 static int lavc_param_top= -1;
157 static int lavc_param_alt= 0;
158 static int lavc_param_ilme= 0;
159 static int lavc_param_nssew= 8;
160 static int lavc_param_closed_gop = 0;
161 static int lavc_param_dc_precision = 8;
162 static int lavc_param_threads= 1;
163 static int lavc_param_turbo = 0;
164 static int lavc_param_skip_threshold=0;
165 static int lavc_param_skip_factor=0;
166 static int lavc_param_skip_exp=0;
167 static int lavc_param_skip_cmp=0;
168 static int lavc_param_brd_scale = 0;
169 static int lavc_param_bidir_refine = 0;
170 static int lavc_param_sc_factor = 1;
171 static int lavc_param_video_global_header= 0;
172 static int lavc_param_mv0_threshold = 256;
173 static int lavc_param_refs = 1;
174 static int lavc_param_b_sensitivity = 40;
175 static int lavc_param_level = FF_LEVEL_UNKNOWN;
177 char *lavc_param_acodec = "mp2";
178 int lavc_param_atag = 0;
179 int lavc_param_abitrate = 224;
180 int lavc_param_audio_global_header= 0;
181 static char *lavc_param_avopt = NULL;
183 #include "m_option.h"
185 const m_option_t lavcopts_conf[]={
186 {"acodec", &lavc_param_acodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
187 {"abitrate", &lavc_param_abitrate, CONF_TYPE_INT, CONF_RANGE, 1, 1000000, NULL},
188 {"atag", &lavc_param_atag, CONF_TYPE_INT, CONF_RANGE, 0, 0xffff, NULL},
189 {"vcodec", &lavc_param_vcodec, CONF_TYPE_STRING, 0, 0, 0, NULL},
190 {"vbitrate", &lavc_param_vbitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
191 {"vratetol", &lavc_param_vrate_tolerance, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
192 {"vhq", &lavc_param_mb_decision, CONF_TYPE_FLAG, 0, 0, 1, NULL},
193 {"mbd", &lavc_param_mb_decision, CONF_TYPE_INT, CONF_RANGE, 0, 9, NULL},
194 {"v4mv", &lavc_param_v4mv, CONF_TYPE_FLAG, 0, 0, 1, NULL},
195 {"vme", &lavc_param_vme, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
196 {"vqscale", &lavc_param_vqscale, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 255.0, NULL},
197 {"vqmin", &lavc_param_vqmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
198 {"vqmax", &lavc_param_vqmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
199 {"mbqmin", &lavc_param_mb_qmin, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
200 {"mbqmax", &lavc_param_mb_qmax, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
201 {"lmin", &lavc_param_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
202 {"lmax", &lavc_param_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
203 {"mblmin", &lavc_param_mb_lmin, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
204 {"mblmax", &lavc_param_mb_lmax, CONF_TYPE_FLOAT, CONF_RANGE, 0.01, 255.0, NULL},
205 {"vqdiff", &lavc_param_vqdiff, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL},
206 {"vqcomp", &lavc_param_vqcompress, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
207 {"vqblur", &lavc_param_vqblur, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
208 {"vb_qfactor", &lavc_param_vb_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
209 {"vmax_b_frames", &lavc_param_vmax_b_frames, CONF_TYPE_INT, CONF_RANGE, 0, FF_MAX_B_FRAMES, NULL},
210 {"vpass", &lavc_param_vpass, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
211 {"vrc_strategy", &lavc_param_vrc_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL},
212 {"vb_strategy", &lavc_param_vb_strategy, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
213 {"vb_qoffset", &lavc_param_vb_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
214 {"vlelim", &lavc_param_luma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
215 {"vcelim", &lavc_param_chroma_elim_threshold, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
216 {"vpsize", &lavc_param_packet_size, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
217 {"vstrict", &lavc_param_strict, CONF_TYPE_INT, CONF_RANGE, -99, 99, NULL},
218 {"vdpart", &lavc_param_data_partitioning, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
219 {"keyint", &lavc_param_keyint, CONF_TYPE_INT, 0, 0, 0, NULL},
220 {"gray", &lavc_param_gray, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PART, NULL},
221 {"mpeg_quant", &lavc_param_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL},
222 {"vi_qfactor", &lavc_param_vi_qfactor, CONF_TYPE_FLOAT, CONF_RANGE, -31.0, 31.0, NULL},
223 {"vi_qoffset", &lavc_param_vi_qoffset, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 31.0, NULL},
224 {"vqsquish", &lavc_param_rc_qsquish, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
225 {"vqmod_amp", &lavc_param_rc_qmod_amp, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
226 {"vqmod_freq", &lavc_param_rc_qmod_freq, CONF_TYPE_INT, 0, 0, 0, NULL},
227 {"vrc_eq", &lavc_param_rc_eq, CONF_TYPE_STRING, 0, 0, 0, NULL},
228 {"vrc_override", &lavc_param_rc_override_string, CONF_TYPE_STRING, 0, 0, 0, NULL},
229 {"vrc_maxrate", &lavc_param_rc_max_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
230 {"vrc_minrate", &lavc_param_rc_min_rate, CONF_TYPE_INT, CONF_RANGE, 0, 24000000, NULL},
231 {"vrc_buf_size", &lavc_param_rc_buffer_size, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL},
232 {"vrc_buf_aggressivity", &lavc_param_rc_buffer_aggressivity, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 99.0, NULL},
233 {"vrc_init_cplx", &lavc_param_rc_initial_cplx, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 9999999.0, NULL},
234 {"vrc_init_occupancy", &lavc_param_rc_initial_buffer_occupancy, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
235 {"vfdct", &lavc_param_fdct, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
236 {"aspect", &lavc_param_aspect, CONF_TYPE_STRING, 0, 0, 0, NULL},
237 {"autoaspect", &lavc_param_autoaspect, CONF_TYPE_FLAG, 0, 0, 1, NULL},
238 {"lumi_mask", &lavc_param_lumi_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
239 {"tcplx_mask", &lavc_param_temporal_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
240 {"scplx_mask", &lavc_param_spatial_cplx_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
241 {"p_mask", &lavc_param_p_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
242 {"naq", &lavc_param_normalize_aqp, CONF_TYPE_FLAG, 0, 0, 1, NULL},
243 {"dark_mask", &lavc_param_dark_masking, CONF_TYPE_FLOAT, CONF_RANGE, -1.0, 1.0, NULL},
244 {"ildct", &lavc_param_interlaced_dct, CONF_TYPE_FLAG, 0, 0, 1, NULL},
245 {"idct", &lavc_param_idct, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
246 {"pred", &lavc_param_prediction_method, CONF_TYPE_INT, CONF_RANGE, 0, 20, NULL},
247 {"format", &lavc_param_format, CONF_TYPE_IMGFMT, 0, 0, 0, NULL},
248 {"debug", &lavc_param_debug, CONF_TYPE_INT, CONF_RANGE, 0, 100000000, NULL},
249 {"psnr", &lavc_param_psnr, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_PSNR, NULL},
250 {"precmp", &lavc_param_me_pre_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
251 {"cmp", &lavc_param_me_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
252 {"subcmp", &lavc_param_me_sub_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
253 {"mbcmp", &lavc_param_mb_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
254 {"skipcmp", &lavc_param_skip_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
255 #ifdef FF_CMP_VSAD
256 {"ildctcmp", &lavc_param_ildct_cmp, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
257 #endif
258 {"bit_exact", &lavc_param_bit_exact, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_BITEXACT, NULL},
259 {"predia", &lavc_param_pre_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
260 {"dia", &lavc_param_dia_size, CONF_TYPE_INT, CONF_RANGE, -2000, 2000, NULL},
261 {"qpel", &lavc_param_qpel, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_QPEL, NULL},
262 {"trell", &lavc_param_trell, CONF_TYPE_FLAG, 0, 0, 1, NULL},
263 {"lowdelay", &lavc_param_lowdelay, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOW_DELAY, NULL},
264 {"last_pred", &lavc_param_last_pred, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
265 {"preme", &lavc_param_pre_me, CONF_TYPE_INT, CONF_RANGE, 0, 2000, NULL},
266 {"subq", &lavc_param_me_subpel_quality, CONF_TYPE_INT, CONF_RANGE, 0, 8, NULL},
267 {"me_range", &lavc_param_me_range, CONF_TYPE_INT, CONF_RANGE, 0, 16000, NULL},
268 #ifdef CODEC_FLAG_AC_PRED
269 {"aic", &lavc_param_aic, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_AC_PRED, NULL},
270 {"umv", &lavc_param_umv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_UMV, NULL},
271 #endif
272 #ifdef CODEC_FLAG_H263P_AIV
273 {"aiv", &lavc_param_aiv, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_H263P_AIV, NULL},
274 #endif
275 #ifdef CODEC_FLAG_OBMC
276 {"obmc", &lavc_param_obmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_OBMC, NULL},
277 #endif
278 #ifdef CODEC_FLAG_LOOP_FILTER
279 {"loop", &lavc_param_loop, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_LOOP_FILTER, NULL},
280 #endif
281 {"ibias", &lavc_param_ibias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
282 {"pbias", &lavc_param_pbias, CONF_TYPE_INT, CONF_RANGE, -512, 512, NULL},
283 {"coder", &lavc_param_coder, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
284 {"context", &lavc_param_context, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
285 {"intra_matrix", &lavc_param_intra_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
286 {"inter_matrix", &lavc_param_inter_matrix, CONF_TYPE_STRING, 0, 0, 0, NULL},
287 {"cbp", &lavc_param_cbp, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_CBP_RD, NULL},
288 {"mv0", &lavc_param_mv0, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_MV0, NULL},
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 #ifdef CODEC_FLAG_GMC
306 {"gmc", &lavc_param_gmc, CONF_TYPE_FLAG, 0, 0, CODEC_FLAG_GMC, NULL},
307 #endif
308 {"dc", &lavc_param_dc_precision, CONF_TYPE_INT, CONF_RANGE, 8, 11, NULL},
309 {"border_mask", &lavc_param_border_masking, CONF_TYPE_FLOAT, CONF_RANGE, 0.0, 1.0, NULL},
310 {"inter_threshold", &lavc_param_inter_threshold, CONF_TYPE_INT, CONF_RANGE, -1000000, 1000000, NULL},
311 {"sc_threshold", &lavc_param_sc_threshold, CONF_TYPE_INT, CONF_RANGE, -1000000000, 1000000000, NULL},
312 {"top", &lavc_param_top, CONF_TYPE_INT, CONF_RANGE, -1, 1, NULL},
313 {"qns", &lavc_param_qns, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
314 {"nssew", &lavc_param_nssew, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
315 {"threads", &lavc_param_threads, CONF_TYPE_INT, CONF_RANGE, 1, 8, NULL},
316 {"turbo", &lavc_param_turbo, CONF_TYPE_FLAG, 0, 0, 1, NULL},
317 {"skip_threshold", &lavc_param_skip_threshold, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
318 {"skip_factor", &lavc_param_skip_factor, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
319 {"skip_exp", &lavc_param_skip_exp, CONF_TYPE_INT, CONF_RANGE, 0, 1000000, NULL},
320 {"brd_scale", &lavc_param_brd_scale, CONF_TYPE_INT, CONF_RANGE, 0, 10, NULL},
321 {"bidir_refine", &lavc_param_bidir_refine, CONF_TYPE_INT, CONF_RANGE, 0, 4, NULL},
322 {"sc_factor", &lavc_param_sc_factor, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
323 {"vglobal", &lavc_param_video_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
324 {"aglobal", &lavc_param_audio_global_header, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
325 {"mv0_threshold", &lavc_param_mv0_threshold, CONF_TYPE_INT, CONF_RANGE, 0, INT_MAX, NULL},
326 {"refs", &lavc_param_refs, CONF_TYPE_INT, CONF_RANGE, 1, 16, NULL},
327 {"b_sensitivity", &lavc_param_b_sensitivity, CONF_TYPE_INT, CONF_RANGE, 1, INT_MAX, NULL},
328 {"level", &lavc_param_level, CONF_TYPE_INT, CONF_RANGE, INT_MIN, INT_MAX, NULL},
329 {"o", &lavc_param_avopt, CONF_TYPE_STRING, 0, 0, 0, NULL},
330 {NULL, NULL, 0, 0, 0, 0, NULL}
333 struct vf_priv_s {
334 muxer_stream_t* mux;
335 AVCodecContext *context;
336 AVFrame *pic;
337 AVCodec *codec;
338 FILE *stats_file;
341 #define stats_file (vf->priv->stats_file)
342 #define mux_v (vf->priv->mux)
343 #define lavc_venc_context (vf->priv->context)
345 static int encode_frame(struct vf_instance *vf, AVFrame *pic, double pts);
347 static int config(struct vf_instance *vf,
348 int width, int height, int d_width, int d_height,
349 unsigned int flags, unsigned int outfmt){
350 int size, i;
351 char *p;
353 mux_v->bih->biWidth=width;
354 mux_v->bih->biHeight=height;
355 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*(mux_v->bih->biBitCount/8);
357 mp_msg(MSGT_MENCODER, MSGL_INFO,"videocodec: libavcodec (%dx%d fourcc=%x [%.4s])\n",
358 mux_v->bih->biWidth, mux_v->bih->biHeight, mux_v->bih->biCompression,
359 (char *)&mux_v->bih->biCompression);
361 lavc_venc_context->width = width;
362 lavc_venc_context->height = height;
363 if (lavc_param_vbitrate > 16000) /* != -1 */
364 lavc_venc_context->bit_rate = lavc_param_vbitrate;
365 else if (lavc_param_vbitrate >= 0) /* != -1 */
366 lavc_venc_context->bit_rate = lavc_param_vbitrate*1000;
367 else
368 lavc_venc_context->bit_rate = 800000; /* default */
370 mux_v->avg_rate= lavc_venc_context->bit_rate;
372 lavc_venc_context->bit_rate_tolerance= lavc_param_vrate_tolerance*1000;
373 lavc_venc_context->time_base= (AVRational){mux_v->h.dwScale, mux_v->h.dwRate};
374 lavc_venc_context->qmin= lavc_param_vqmin;
375 lavc_venc_context->qmax= lavc_param_vqmax;
376 lavc_venc_context->mb_qmin= lavc_param_mb_qmin;
377 lavc_venc_context->mb_qmax= lavc_param_mb_qmax;
378 lavc_venc_context->lmin= (int)(FF_QP2LAMBDA * lavc_param_lmin + 0.5);
379 lavc_venc_context->lmax= (int)(FF_QP2LAMBDA * lavc_param_lmax + 0.5);
380 lavc_venc_context->mb_lmin= (int)(FF_QP2LAMBDA * lavc_param_mb_lmin + 0.5);
381 lavc_venc_context->mb_lmax= (int)(FF_QP2LAMBDA * lavc_param_mb_lmax + 0.5);
382 lavc_venc_context->max_qdiff= lavc_param_vqdiff;
383 lavc_venc_context->qcompress= lavc_param_vqcompress;
384 lavc_venc_context->qblur= lavc_param_vqblur;
385 lavc_venc_context->max_b_frames= lavc_param_vmax_b_frames;
386 lavc_venc_context->b_quant_factor= lavc_param_vb_qfactor;
387 lavc_venc_context->rc_strategy= lavc_param_vrc_strategy;
388 lavc_venc_context->b_frame_strategy= lavc_param_vb_strategy;
389 lavc_venc_context->b_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vb_qoffset + 0.5);
390 lavc_venc_context->luma_elim_threshold= lavc_param_luma_elim_threshold;
391 lavc_venc_context->chroma_elim_threshold= lavc_param_chroma_elim_threshold;
392 lavc_venc_context->rtp_payload_size= lavc_param_packet_size;
393 lavc_venc_context->strict_std_compliance= lavc_param_strict;
394 lavc_venc_context->i_quant_factor= lavc_param_vi_qfactor;
395 lavc_venc_context->i_quant_offset= (int)(FF_QP2LAMBDA * lavc_param_vi_qoffset + 0.5);
396 lavc_venc_context->rc_qsquish= lavc_param_rc_qsquish;
397 lavc_venc_context->rc_qmod_amp= lavc_param_rc_qmod_amp;
398 lavc_venc_context->rc_qmod_freq= lavc_param_rc_qmod_freq;
399 lavc_venc_context->rc_eq= lavc_param_rc_eq;
401 mux_v->max_rate=
402 lavc_venc_context->rc_max_rate= lavc_param_rc_max_rate*1000;
403 lavc_venc_context->rc_min_rate= lavc_param_rc_min_rate*1000;
405 mux_v->vbv_size=
406 lavc_venc_context->rc_buffer_size= lavc_param_rc_buffer_size*1000;
408 lavc_venc_context->rc_initial_buffer_occupancy=
409 lavc_venc_context->rc_buffer_size *
410 lavc_param_rc_initial_buffer_occupancy;
411 lavc_venc_context->rc_buffer_aggressivity= lavc_param_rc_buffer_aggressivity;
412 lavc_venc_context->rc_initial_cplx= lavc_param_rc_initial_cplx;
413 lavc_venc_context->debug= lavc_param_debug;
414 lavc_venc_context->last_predictor_count= lavc_param_last_pred;
415 lavc_venc_context->pre_me= lavc_param_pre_me;
416 lavc_venc_context->me_pre_cmp= lavc_param_me_pre_cmp;
417 lavc_venc_context->pre_dia_size= lavc_param_pre_dia_size;
418 lavc_venc_context->me_subpel_quality= lavc_param_me_subpel_quality;
419 lavc_venc_context->me_range= lavc_param_me_range;
420 lavc_venc_context->intra_quant_bias= lavc_param_ibias;
421 lavc_venc_context->inter_quant_bias= lavc_param_pbias;
422 lavc_venc_context->coder_type= lavc_param_coder;
423 lavc_venc_context->context_model= lavc_param_context;
424 lavc_venc_context->scenechange_threshold= lavc_param_sc_threshold;
425 lavc_venc_context->noise_reduction= lavc_param_noise_reduction;
426 lavc_venc_context->quantizer_noise_shaping= lavc_param_qns;
427 lavc_venc_context->inter_threshold= lavc_param_inter_threshold;
428 lavc_venc_context->nsse_weight= lavc_param_nssew;
429 lavc_venc_context->frame_skip_threshold= lavc_param_skip_threshold;
430 lavc_venc_context->frame_skip_factor= lavc_param_skip_factor;
431 lavc_venc_context->frame_skip_exp= lavc_param_skip_exp;
432 lavc_venc_context->frame_skip_cmp= lavc_param_skip_cmp;
434 if (lavc_param_intra_matrix)
436 char *tmp;
438 lavc_venc_context->intra_matrix =
439 av_malloc(sizeof(*lavc_venc_context->intra_matrix)*64);
441 i = 0;
442 while ((tmp = strsep(&lavc_param_intra_matrix, ",")) && (i < 64))
444 if (!tmp || (tmp && !strlen(tmp)))
445 break;
446 lavc_venc_context->intra_matrix[i++] = atoi(tmp);
449 if (i != 64)
450 av_freep(&lavc_venc_context->intra_matrix);
451 else
452 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified intra matrix\n");
454 if (lavc_param_inter_matrix)
456 char *tmp;
458 lavc_venc_context->inter_matrix =
459 av_malloc(sizeof(*lavc_venc_context->inter_matrix)*64);
461 i = 0;
462 while ((tmp = strsep(&lavc_param_inter_matrix, ",")) && (i < 64))
464 if (!tmp || (tmp && !strlen(tmp)))
465 break;
466 lavc_venc_context->inter_matrix[i++] = atoi(tmp);
469 if (i != 64)
470 av_freep(&lavc_venc_context->inter_matrix);
471 else
472 mp_msg(MSGT_MENCODER, MSGL_V, "Using user specified inter matrix\n");
475 p= lavc_param_rc_override_string;
476 for(i=0; p; i++){
477 int start, end, q;
478 int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
479 if(e!=3){
480 mp_msg(MSGT_MENCODER,MSGL_ERR,"error parsing vrc_q\n");
481 return 0;
483 lavc_venc_context->rc_override=
484 realloc(lavc_venc_context->rc_override, sizeof(RcOverride)*(i+1));
485 lavc_venc_context->rc_override[i].start_frame= start;
486 lavc_venc_context->rc_override[i].end_frame = end;
487 if(q>0){
488 lavc_venc_context->rc_override[i].qscale= q;
489 lavc_venc_context->rc_override[i].quality_factor= 1.0;
491 else{
492 lavc_venc_context->rc_override[i].qscale= 0;
493 lavc_venc_context->rc_override[i].quality_factor= -q/100.0;
495 p= strchr(p, '/');
496 if(p) p++;
498 lavc_venc_context->rc_override_count=i;
500 lavc_venc_context->mpeg_quant=lavc_param_mpeg_quant;
502 lavc_venc_context->dct_algo= lavc_param_fdct;
503 lavc_venc_context->idct_algo= lavc_param_idct;
505 lavc_venc_context->lumi_masking= lavc_param_lumi_masking;
506 lavc_venc_context->temporal_cplx_masking= lavc_param_temporal_cplx_masking;
507 lavc_venc_context->spatial_cplx_masking= lavc_param_spatial_cplx_masking;
508 lavc_venc_context->p_masking= lavc_param_p_masking;
509 lavc_venc_context->dark_masking= lavc_param_dark_masking;
510 lavc_venc_context->border_masking = lavc_param_border_masking;
512 if (lavc_param_aspect != NULL)
514 int par_width, par_height, e;
515 float ratio=0;
517 e= sscanf (lavc_param_aspect, "%d/%d", &par_width, &par_height);
518 if(e==2){
519 if(par_height)
520 ratio= (float)par_width / (float)par_height;
521 }else{
522 e= sscanf (lavc_param_aspect, "%f", &ratio);
525 if (e && ratio > 0.1 && ratio < 10.0) {
526 lavc_venc_context->sample_aspect_ratio= av_d2q(ratio * height / width, 255);
527 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "sample_aspect_ratio: %d/%d\n",
528 lavc_venc_context->sample_aspect_ratio.num,
529 lavc_venc_context->sample_aspect_ratio.den);
530 mux_v->aspect = ratio;
531 mp_dbg(MSGT_MENCODER, MSGL_DBG2, "aspect_ratio: %f\n", ratio);
532 } else {
533 mp_dbg(MSGT_MENCODER, MSGL_ERR, "aspect ratio: cannot parse \"%s\"\n", lavc_param_aspect);
534 return 0;
537 else if (lavc_param_autoaspect) {
538 lavc_venc_context->sample_aspect_ratio = av_d2q((float)d_width/d_height*height / width, 255);
539 mux_v->aspect = (float)d_width/d_height;
542 /* keyframe interval */
543 if (lavc_param_keyint >= 0) /* != -1 */
544 lavc_venc_context->gop_size = lavc_param_keyint;
545 else
546 lavc_venc_context->gop_size = 250; /* default */
548 lavc_venc_context->flags = 0;
549 if (lavc_param_mb_decision)
551 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "[VE_LAVC] High quality encoding selected (non-realtime)!\n");
552 lavc_venc_context->mb_decision= lavc_param_mb_decision;
555 lavc_venc_context->me_cmp= lavc_param_me_cmp;
556 lavc_venc_context->me_sub_cmp= lavc_param_me_sub_cmp;
557 lavc_venc_context->mb_cmp= lavc_param_mb_cmp;
558 #ifdef FF_CMP_VSAD
559 lavc_venc_context->ildct_cmp= lavc_param_ildct_cmp;
560 #endif
561 lavc_venc_context->dia_size= lavc_param_dia_size;
562 lavc_venc_context->flags|= lavc_param_qpel;
563 lavc_venc_context->trellis = lavc_param_trell;
564 lavc_venc_context->flags|= lavc_param_lowdelay;
565 lavc_venc_context->flags|= lavc_param_bit_exact;
566 lavc_venc_context->flags|= lavc_param_aic;
567 lavc_venc_context->flags|= lavc_param_aiv;
568 lavc_venc_context->flags|= lavc_param_umv;
569 lavc_venc_context->flags|= lavc_param_obmc;
570 lavc_venc_context->flags|= lavc_param_loop;
571 lavc_venc_context->flags|= lavc_param_v4mv ? CODEC_FLAG_4MV : 0;
572 lavc_venc_context->flags|= lavc_param_data_partitioning;
573 lavc_venc_context->flags|= lavc_param_cbp;
574 lavc_venc_context->flags|= lavc_param_mv0;
575 lavc_venc_context->flags|= lavc_param_qp_rd;
576 lavc_venc_context->flags|= lavc_param_ss;
577 lavc_venc_context->flags|= lavc_param_alt;
578 lavc_venc_context->flags|= lavc_param_ilme;
579 lavc_venc_context->flags|= lavc_param_gmc;
580 #ifdef CODEC_FLAG_CLOSED_GOP
581 lavc_venc_context->flags|= lavc_param_closed_gop;
582 #endif
583 if(lavc_param_gray) lavc_venc_context->flags|= CODEC_FLAG_GRAY;
585 if(lavc_param_normalize_aqp) lavc_venc_context->flags|= CODEC_FLAG_NORMALIZE_AQP;
586 if(lavc_param_interlaced_dct) lavc_venc_context->flags|= CODEC_FLAG_INTERLACED_DCT;
587 lavc_venc_context->flags|= lavc_param_psnr;
588 lavc_venc_context->intra_dc_precision = lavc_param_dc_precision - 8;
589 lavc_venc_context->prediction_method= lavc_param_prediction_method;
590 lavc_venc_context->brd_scale = lavc_param_brd_scale;
591 lavc_venc_context->bidir_refine = lavc_param_bidir_refine;
592 lavc_venc_context->scenechange_factor = lavc_param_sc_factor;
593 if((lavc_param_video_global_header&1)
594 /*|| (video_global_header==0 && (oc->oformat->flags & AVFMT_GLOBALHEADER))*/){
595 lavc_venc_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
597 if(lavc_param_video_global_header&2){
598 lavc_venc_context->flags2 |= CODEC_FLAG2_LOCAL_HEADER;
600 lavc_venc_context->mv0_threshold = lavc_param_mv0_threshold;
601 lavc_venc_context->refs = lavc_param_refs;
602 lavc_venc_context->b_sensitivity = lavc_param_b_sensitivity;
603 lavc_venc_context->level = lavc_param_level;
605 if(lavc_param_avopt){
606 if(parse_avopts(lavc_venc_context, lavc_param_avopt) < 0){
607 mp_msg(MSGT_MENCODER,MSGL_ERR, "Your options /%s/ look like gibberish to me pal\n", lavc_param_avopt);
608 return 0;
612 mux_v->imgfmt = lavc_param_format;
613 lavc_venc_context->pix_fmt = imgfmt2pixfmt(lavc_param_format);
614 if (lavc_venc_context->pix_fmt == PIX_FMT_NONE)
615 return 0;
617 if(!stats_file) {
618 /* lavc internal 2pass bitrate control */
619 switch(lavc_param_vpass){
620 case 2:
621 case 3:
622 lavc_venc_context->flags|= CODEC_FLAG_PASS2;
623 stats_file= fopen(passtmpfile, "rb");
624 if(stats_file==NULL){
625 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
626 return 0;
628 fseek(stats_file, 0, SEEK_END);
629 size= ftell(stats_file);
630 fseek(stats_file, 0, SEEK_SET);
632 lavc_venc_context->stats_in= av_malloc(size + 1);
633 lavc_venc_context->stats_in[size]=0;
635 if(fread(lavc_venc_context->stats_in, size, 1, stats_file)<1){
636 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: reading from filename=%s\n", passtmpfile);
637 return 0;
639 if(lavc_param_vpass == 2)
640 break;
641 else
642 fclose(stats_file);
643 /* fall through */
644 case 1:
645 lavc_venc_context->flags|= CODEC_FLAG_PASS1;
646 stats_file= fopen(passtmpfile, "wb");
647 if(stats_file==NULL){
648 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile);
649 return 0;
651 if(lavc_param_turbo && (lavc_param_vpass == 1)) {
652 /* uses SAD comparison functions instead of other hungrier */
653 lavc_venc_context->me_pre_cmp = 0;
654 lavc_venc_context->me_cmp = 0;
655 lavc_venc_context->me_sub_cmp = 0;
656 lavc_venc_context->mb_cmp = 2;
658 /* Disables diamond motion estimation */
659 lavc_venc_context->pre_dia_size = 0;
660 lavc_venc_context->dia_size = 1;
662 lavc_venc_context->quantizer_noise_shaping = 0; // qns=0
663 lavc_venc_context->noise_reduction = 0; // nr=0
664 lavc_venc_context->mb_decision = 0; // mbd=0 ("realtime" encoding)
666 lavc_venc_context->flags &= ~CODEC_FLAG_QPEL;
667 lavc_venc_context->flags &= ~CODEC_FLAG_4MV;
668 lavc_venc_context->trellis = 0;
669 lavc_venc_context->flags &= ~CODEC_FLAG_CBP_RD;
670 lavc_venc_context->flags &= ~CODEC_FLAG_QP_RD;
671 lavc_venc_context->flags &= ~CODEC_FLAG_MV0;
673 break;
677 lavc_venc_context->me_method = ME_ZERO+lavc_param_vme;
679 /* fixed qscale :p */
680 if (lavc_param_vqscale >= 0.0)
682 mp_tmsg(MSGT_MENCODER, MSGL_INFO, "[VE_LAVC] Using constant qscale = %f (VBR).\n", lavc_param_vqscale);
683 lavc_venc_context->flags |= CODEC_FLAG_QSCALE;
684 lavc_venc_context->global_quality=
685 vf->priv->pic->quality = (int)(FF_QP2LAMBDA * lavc_param_vqscale + 0.5);
688 if(lavc_param_threads > 1)
689 avcodec_thread_init(lavc_venc_context, lavc_param_threads);
691 if (avcodec_open(lavc_venc_context, vf->priv->codec) != 0) {
692 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"Could not open codec.\n");
693 return 0;
696 if (lavc_venc_context->codec->encode == NULL) {
697 mp_msg(MSGT_MENCODER,MSGL_ERR,"avcodec init failed (ctx->codec->encode == NULL)!\n");
698 return 0;
701 /* free second pass buffer, its not needed anymore */
702 av_freep(&lavc_venc_context->stats_in);
703 if(lavc_venc_context->bits_per_coded_sample)
704 mux_v->bih->biBitCount= lavc_venc_context->bits_per_coded_sample;
705 if(lavc_venc_context->extradata_size){
706 mux_v->bih= realloc(mux_v->bih, sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size);
707 memcpy(mux_v->bih + 1, lavc_venc_context->extradata, lavc_venc_context->extradata_size);
708 mux_v->bih->biSize= sizeof(BITMAPINFOHEADER) + lavc_venc_context->extradata_size;
711 mux_v->decoder_delay = lavc_venc_context->max_b_frames ? 1 : 0;
713 return 1;
716 static int control(struct vf_instance *vf, int request, void* data){
718 switch(request){
719 case VFCTRL_FLUSH_FRAMES:
720 if(vf->priv->codec->capabilities & CODEC_CAP_DELAY)
721 while(encode_frame(vf, NULL, MP_NOPTS_VALUE) > 0);
722 return CONTROL_TRUE;
723 default:
724 return CONTROL_UNKNOWN;
728 static int query_format(struct vf_instance *vf, unsigned int fmt){
729 switch(fmt){
730 case IMGFMT_YV12:
731 case IMGFMT_IYUV:
732 case IMGFMT_I420:
733 if(lavc_param_format == IMGFMT_YV12)
734 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
735 break;
736 case IMGFMT_411P:
737 if(lavc_param_format == IMGFMT_411P)
738 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
739 break;
740 case IMGFMT_422P:
741 if(lavc_param_format == IMGFMT_422P)
742 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
743 break;
744 case IMGFMT_444P:
745 if(lavc_param_format == IMGFMT_444P)
746 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
747 break;
748 case IMGFMT_YVU9:
749 if(lavc_param_format == IMGFMT_YVU9)
750 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
751 break;
752 case IMGFMT_BGR32:
753 if(lavc_param_format == IMGFMT_BGR32)
754 return VFCAP_CSP_SUPPORTED | VFCAP_ACCEPT_STRIDE;
755 break;
757 return 0;
760 static double psnr(double d){
761 if(d==0) return INFINITY;
762 return -10.0*log(d)/log(10);
765 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
766 AVFrame *pic= vf->priv->pic;
768 pic->data[0]=mpi->planes[0];
769 pic->data[1]=mpi->planes[1];
770 pic->data[2]=mpi->planes[2];
771 pic->linesize[0]=mpi->stride[0];
772 pic->linesize[1]=mpi->stride[1];
773 pic->linesize[2]=mpi->stride[2];
775 if(lavc_param_interlaced_dct){
776 if((mpi->fields & MP_IMGFIELD_ORDERED) && (mpi->fields & MP_IMGFIELD_INTERLACED))
777 pic->top_field_first= !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
778 else
779 pic->top_field_first= 1;
781 if(lavc_param_top!=-1)
782 pic->top_field_first= lavc_param_top;
785 return encode_frame(vf, pic, pts) >= 0;
788 static int encode_frame(struct vf_instance *vf, AVFrame *pic, double pts){
789 const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'};
790 int out_size;
791 double dts;
793 if(pts == MP_NOPTS_VALUE)
794 pts= lavc_venc_context->frame_number * av_q2d(lavc_venc_context->time_base);
796 if(pic){
797 #if 0
798 pic->opaque= malloc(sizeof(pts));
799 memcpy(pic->opaque, &pts, sizeof(pts));
800 #else
801 if(pts != MP_NOPTS_VALUE)
802 pic->pts= floor(pts / av_q2d(lavc_venc_context->time_base) + 0.5);
803 else
804 pic->pts= MP_NOPTS_VALUE;
805 #endif
807 out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size,
808 pic);
810 if(pts != MP_NOPTS_VALUE)
811 dts= pts - lavc_venc_context->delay * av_q2d(lavc_venc_context->time_base);
812 else
813 dts= MP_NOPTS_VALUE;
814 #if 0
815 pts= lavc_venc_context->coded_frame->opaque ?
816 *(double*)lavc_venc_context->coded_frame->opaque
817 : MP_NOPTS_VALUE;
818 #else
819 if(lavc_venc_context->coded_frame->pts != MP_NOPTS_VALUE)
820 pts= lavc_venc_context->coded_frame->pts * av_q2d(lavc_venc_context->time_base);
821 else
822 pts= MP_NOPTS_VALUE;
823 assert(MP_NOPTS_VALUE == AV_NOPTS_VALUE);
824 #endif
825 //fprintf(stderr, "ve_lavc %f/%f\n", dts, pts);
826 if(out_size == 0 && lavc_param_skip_threshold==0 && lavc_param_skip_factor==0){
827 ++mux_v->encoder_delay;
828 return 0;
831 muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0,
832 dts, pts);
833 free(lavc_venc_context->coded_frame->opaque);
834 lavc_venc_context->coded_frame->opaque= NULL;
836 /* store psnr / pict size / type / qscale */
837 if(lavc_param_psnr){
838 static FILE *fvstats=NULL;
839 char filename[20];
840 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
841 double quality=0.0;
842 int8_t *q;
844 if(!fvstats) {
845 time_t today2;
846 struct tm *today;
847 today2 = time(NULL);
848 today = localtime(&today2);
849 sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour,
850 today->tm_min, today->tm_sec);
851 fvstats = fopen(filename,"w");
852 if(!fvstats) {
853 perror("fopen");
854 lavc_param_psnr=0; // disable block
855 mp_msg(MSGT_MENCODER,MSGL_ERR,"Can't open %s for writing. Check its permissions.\n",filename);
856 return -1;
857 /*exit(1);*/
861 // average MB quantizer
862 q = lavc_venc_context->coded_frame->qscale_table;
863 if(q) {
864 int x, y;
865 int w = (lavc_venc_context->width+15) >> 4;
866 int h = (lavc_venc_context->height+15) >> 4;
867 for( y = 0; y < h; y++ ) {
868 for( x = 0; x < w; x++ )
869 quality += (double)*(q+x);
870 q += lavc_venc_context->coded_frame->qstride;
872 quality /= w * h;
873 } else
874 quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA;
876 fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n",
877 lavc_venc_context->coded_frame->coded_picture_number,
878 quality,
879 out_size,
880 psnr(lavc_venc_context->coded_frame->error[0]/f),
881 psnr(lavc_venc_context->coded_frame->error[1]*4/f),
882 psnr(lavc_venc_context->coded_frame->error[2]*4/f),
883 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)),
884 pict_type_char[lavc_venc_context->coded_frame->pict_type]
887 /* store stats if there are any */
888 if(lavc_venc_context->stats_out && stats_file)
889 fprintf(stats_file, "%s", lavc_venc_context->stats_out);
890 return out_size;
893 static void uninit(struct vf_instance *vf){
895 if(lavc_param_psnr){
896 double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0;
897 f*= lavc_venc_context->coded_frame->coded_picture_number;
899 mp_msg(MSGT_MENCODER, MSGL_INFO, "PSNR: Y:%2.2f, Cb:%2.2f, Cr:%2.2f, All:%2.2f\n",
900 psnr(lavc_venc_context->error[0]/f),
901 psnr(lavc_venc_context->error[1]*4/f),
902 psnr(lavc_venc_context->error[2]*4/f),
903 psnr((lavc_venc_context->error[0]+lavc_venc_context->error[1]+lavc_venc_context->error[2])/(f*1.5))
907 av_freep(&lavc_venc_context->intra_matrix);
908 av_freep(&lavc_venc_context->inter_matrix);
910 if (lavc_venc_context->codec)
911 avcodec_close(lavc_venc_context);
913 if(stats_file) fclose(stats_file);
915 /* free rc_override */
916 av_freep(&lavc_venc_context->rc_override);
918 av_freep(&vf->priv->context);
921 //===========================================================================//
923 static int vf_open(vf_instance_t *vf, char* args){
924 vf->uninit=uninit;
925 vf->config=config;
926 vf->default_caps=VFCAP_CONSTANT;
927 vf->control=control;
928 vf->query_format=query_format;
929 vf->put_image=put_image;
930 vf->priv=calloc(1, sizeof(struct vf_priv_s));
931 vf->priv->mux=(muxer_stream_t*)args;
933 /* XXX: hack: some of the MJPEG decoder DLL's needs exported huffman
934 table, so we define a zero-table, also lavc mjpeg encoder is putting
935 huffman tables into the stream, so no problem */
936 if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "mjpeg"))
938 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER)+28);
939 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+28;
941 else if (lavc_param_vcodec && (!strcasecmp(lavc_param_vcodec, "huffyuv")
942 || !strcasecmp(lavc_param_vcodec, "ffvhuff")))
944 /* XXX: hack: huffyuv needs to store huffman tables (allthough we dunno the size yet ...) */
945 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER)+1000);
946 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+1000;
948 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv1"))
950 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER)+8);
951 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
953 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "asv2"))
955 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER)+8);
956 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+8;
958 else if (lavc_param_vcodec && !strcasecmp(lavc_param_vcodec, "wmv2"))
960 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER)+4);
961 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER)+4;
963 else
965 mux_v->bih=calloc(1, sizeof(BITMAPINFOHEADER));
966 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER);
968 mux_v->bih->biWidth=0;
969 mux_v->bih->biHeight=0;
970 mux_v->bih->biPlanes=1;
971 mux_v->bih->biBitCount=24;
972 if (!lavc_param_vcodec)
974 printf("No libavcodec codec specified! It's required!\n");
975 return 0;
978 if (!strcasecmp(lavc_param_vcodec, "mpeg1") || !strcasecmp(lavc_param_vcodec, "mpeg1video"))
979 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '1');
980 else if (!strcasecmp(lavc_param_vcodec, "mpeg2") || !strcasecmp(lavc_param_vcodec, "mpeg2video"))
981 mux_v->bih->biCompression = mmioFOURCC('m', 'p', 'g', '2');
982 else if (!strcasecmp(lavc_param_vcodec, "h263") || !strcasecmp(lavc_param_vcodec, "h263p"))
983 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '3');
984 else if (!strcasecmp(lavc_param_vcodec, "rv10"))
985 mux_v->bih->biCompression = mmioFOURCC('R', 'V', '1', '0');
986 else if (!strcasecmp(lavc_param_vcodec, "mjpeg"))
987 mux_v->bih->biCompression = mmioFOURCC('M', 'J', 'P', 'G');
988 else if (!strcasecmp(lavc_param_vcodec, "ljpeg"))
989 mux_v->bih->biCompression = mmioFOURCC('L', 'J', 'P', 'G');
990 else if (!strcasecmp(lavc_param_vcodec, "mpeg4"))
991 mux_v->bih->biCompression = mmioFOURCC('F', 'M', 'P', '4');
992 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4"))
993 mux_v->bih->biCompression = mmioFOURCC('d', 'i', 'v', '3');
994 else if (!strcasecmp(lavc_param_vcodec, "msmpeg4v2"))
995 mux_v->bih->biCompression = mmioFOURCC('M', 'P', '4', '2');
996 else if (!strcasecmp(lavc_param_vcodec, "wmv1"))
997 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '1');
998 else if (!strcasecmp(lavc_param_vcodec, "wmv2"))
999 mux_v->bih->biCompression = mmioFOURCC('W', 'M', 'V', '2');
1000 else if (!strcasecmp(lavc_param_vcodec, "huffyuv"))
1001 mux_v->bih->biCompression = mmioFOURCC('H', 'F', 'Y', 'U');
1002 else if (!strcasecmp(lavc_param_vcodec, "ffvhuff"))
1003 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', 'H');
1004 else if (!strcasecmp(lavc_param_vcodec, "asv1"))
1005 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '1');
1006 else if (!strcasecmp(lavc_param_vcodec, "asv2"))
1007 mux_v->bih->biCompression = mmioFOURCC('A', 'S', 'V', '2');
1008 else if (!strcasecmp(lavc_param_vcodec, "ffv1"))
1009 mux_v->bih->biCompression = mmioFOURCC('F', 'F', 'V', '1');
1010 else if (!strcasecmp(lavc_param_vcodec, "snow"))
1011 mux_v->bih->biCompression = mmioFOURCC('S', 'N', 'O', 'W');
1012 else if (!strcasecmp(lavc_param_vcodec, "flv"))
1013 mux_v->bih->biCompression = mmioFOURCC('F', 'L', 'V', '1');
1014 else if (!strcasecmp(lavc_param_vcodec, "dvvideo"))
1015 mux_v->bih->biCompression = mmioFOURCC('d', 'v', 's', 'd');
1016 else if (!strcasecmp(lavc_param_vcodec, "libx264"))
1017 mux_v->bih->biCompression = mmioFOURCC('h', '2', '6', '4');
1018 else if (!strcasecmp(lavc_param_vcodec, "libschroedinger"))
1019 mux_v->bih->biCompression = mmioFOURCC('d', 'r', 'a', 'c');
1020 else if (!strcasecmp(lavc_param_vcodec, "libdirac"))
1021 mux_v->bih->biCompression = mmioFOURCC('d', 'r', 'a', 'c');
1022 else
1023 mux_v->bih->biCompression = mmioFOURCC(lavc_param_vcodec[0],
1024 lavc_param_vcodec[1], lavc_param_vcodec[2], lavc_param_vcodec[3]); /* FIXME!!! */
1026 init_avcodec();
1028 vf->priv->codec = (AVCodec *)avcodec_find_encoder_by_name(lavc_param_vcodec);
1029 if (!vf->priv->codec) {
1030 mp_tmsg(MSGT_MENCODER,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n", lavc_param_vcodec);
1031 return 0;
1034 vf->priv->pic = avcodec_alloc_frame();
1035 vf->priv->context = avcodec_alloc_context();
1036 vf->priv->context->codec_type = CODEC_TYPE_VIDEO;
1037 vf->priv->context->codec_id = vf->priv->codec->id;
1039 return 1;
1042 vf_info_t ve_info_lavc = {
1043 "libavcodec encoder",
1044 "lavc",
1045 "A'rpi, Alex, Michael",
1046 "for internal use by mencoder",
1047 vf_open
1050 //===========================================================================//