Mercurial > mplayer.hg
annotate libmpcodecs/ve_divx4.c @ 8102:2759150cc687
qt swot support
author | alex |
---|---|
date | Mon, 04 Nov 2002 20:28:21 +0000 |
parents | 136e7e515764 |
children | 27da710563c2 |
rev | line source |
---|---|
7332 | 1 #define HAVE_XVID_VBR |
5550 | 2 #include <stdio.h> |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 | |
6 #include "../config.h" | |
7 #include "../mp_msg.h" | |
8 | |
9 #ifdef HAVE_DIVX4ENCORE | |
10 | |
11 #include "codec-cfg.h" | |
12 #include "stream.h" | |
13 #include "demuxer.h" | |
14 #include "stheader.h" | |
15 | |
16 #include "aviwrite.h" | |
17 | |
5607 | 18 #include "img_format.h" |
19 #include "mp_image.h" | |
5550 | 20 #include "vf.h" |
21 | |
7332 | 22 /* About XviD VBR Library, Edouard Gomez (GomGom) said: |
23 <GomGom> header bytes == frame header bytes :-) | |
24 <GomGom> total bytes = frame bytes == texture + header | |
25 <GomGom> quant = quant returned by xvidcore | |
26 <GomGom> it's possible that xvid lowers or increases the passed quant because of lumimasking | |
27 <GomGom> kblks = blocks coded as intra blocks | |
28 <GomGom> mblks = blocks coded as predicted blocks | |
29 <GomGom> ublks = skipped blocks | |
30 <GomGom> at the moemnt le vbr lib uses total bytes, and quant | |
31 <GomGom> so it's easy to use it with divx5 (wo bframes) | |
32 <klOUg> bframes breaks what assumptions? | |
33 <GomGom> quant estimation for next frame | |
34 <GomGom> because of the bframe quant multiplier given to divx5 | |
35 <GomGom> that the vbr lib does not "know" | |
36 */ | |
37 | |
5550 | 38 //===========================================================================// |
39 | |
40 #include "divx4_vbr.h" | |
7332 | 41 #ifdef HAVE_XVID_VBR |
42 #include "xvid_vbr.h" | |
43 #endif | |
5550 | 44 |
8032
136e7e515764
removed -pass nonsense from mencoder! it's now a suboption to -divx4opts
rfelker
parents:
7368
diff
changeset
|
45 static int pass; |
5550 | 46 extern char* passtmpfile; |
7127 | 47 extern void mencoder_write_chunk(aviwrite_stream_t *s,int len,unsigned int flags); |
5550 | 48 |
49 #include <encore2.h> | |
50 | |
5854 | 51 #ifndef ENCORE_MAJOR_VERSION |
52 #define ENCORE_MAJOR_VERSION 4000 | |
53 #endif | |
54 | |
5550 | 55 ENC_PARAM divx4_param; |
56 int divx4_crispness; | |
7332 | 57 #ifdef HAVE_XVID_VBR |
58 static int vbrpass = -1; | |
59 static int vbrdebug = 0; | |
60 #endif | |
5550 | 61 |
62 #include "cfgparser.h" | |
63 | |
64 struct config divx4opts_conf[]={ | |
8032
136e7e515764
removed -pass nonsense from mencoder! it's now a suboption to -divx4opts
rfelker
parents:
7368
diff
changeset
|
65 {"pass", &pass, CONF_TYPE_INT, CONF_RANGE,0,2, NULL}, |
5550 | 66 {"br", &divx4_param.bitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, |
67 {"rc_period", &divx4_param.rc_period, CONF_TYPE_INT, 0,0,0, NULL}, | |
68 {"rc_reaction_period", &divx4_param.rc_reaction_period, CONF_TYPE_INT, 0,0,0, NULL}, | |
69 {"rc_reaction_ratio", &divx4_param.rc_reaction_ratio, CONF_TYPE_INT, 0,0,0, NULL}, | |
70 {"min_quant", &divx4_param.min_quantizer, CONF_TYPE_INT, CONF_RANGE,0,32, NULL}, | |
71 {"max_quant", &divx4_param.max_quantizer, CONF_TYPE_INT, CONF_RANGE,0,32, NULL}, | |
72 {"key", &divx4_param.max_key_interval, CONF_TYPE_INT, CONF_MIN,0,0, NULL}, | |
73 {"deinterlace", &divx4_param.deinterlace, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
74 {"q", &divx4_param.quality, CONF_TYPE_INT, CONF_RANGE, 1, 5, NULL}, | |
75 {"crispness", &divx4_crispness, CONF_TYPE_INT, CONF_RANGE,0,100, NULL}, | |
5854 | 76 #if ENCORE_MAJOR_VERSION >= 5010 |
77 {"bidirect", &divx4_param.extensions.use_bidirect, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
78 {"obmc", &divx4_param.extensions.obmc, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
79 {"data_partitioning", &divx4_param.extensions.data_partitioning, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
80 {"qpel", &divx4_param.extensions.quarter_pel, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
81 {"intra_frame_threshold", &divx4_param.extensions.intra_frame_threshold, CONF_TYPE_INT, CONF_RANGE,1,100, NULL}, | |
82 {"psychovisual", &divx4_param.extensions.psychovisual, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
83 {"testing_param", &divx4_param.extensions.testing_param, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
84 {"gmc", &divx4_param.extensions.use_gmc, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
85 {"interlace_mode", &divx4_param.extensions.interlace_mode, CONF_TYPE_INT, CONF_RANGE,0,2, NULL}, | |
86 {"temporal", &divx4_param.extensions.temporal_enable, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
87 {"spatial", &divx4_param.extensions.spatial_passes, CONF_TYPE_INT, 0,0,1, NULL}, | |
88 {"mv_file", &divx4_param.extensions.mv_file, CONF_TYPE_STRING, 0,0,1, NULL}, | |
89 #endif | |
7332 | 90 #ifdef HAVE_XVID_VBR |
91 {"vbrpass", &vbrpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, | |
92 {"vbrdebug", &vbrdebug, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL}, | |
93 #endif | |
5550 | 94 {"help", "TODO: divx4opts help!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, |
95 {NULL, NULL, 0, 0, 0, 0, NULL} | |
96 }; | |
97 | |
98 struct vf_priv_s { | |
99 aviwrite_stream_t* mux; | |
100 ENC_RESULT enc_result; | |
101 ENC_FRAME enc_frame; | |
102 void* enc_handle; | |
7332 | 103 #ifdef HAVE_XVID_VBR |
104 vbr_control_t vbr_state; | |
105 #endif | |
5550 | 106 }; |
107 | |
108 #define mux_v (vf->priv->mux) | |
109 | |
110 static int config(struct vf_instance_s* vf, | |
111 int width, int height, int d_width, int d_height, | |
112 unsigned int flags, unsigned int outfmt){ | |
113 | |
114 mux_v->bih->biWidth=width; | |
115 mux_v->bih->biHeight=height; | |
116 | |
117 divx4_param.x_dim=width; | |
118 divx4_param.y_dim=height; | |
119 divx4_param.framerate=(float)mux_v->h.dwRate/mux_v->h.dwScale; | |
120 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*3; | |
121 | |
122 if(!divx4_param.bitrate) divx4_param.bitrate=800000; | |
123 else if(divx4_param.bitrate<=16000) divx4_param.bitrate*=1000; | |
124 if(!divx4_param.quality) divx4_param.quality=5; // the quality of compression ( 1 - fastest, 5 - best ) | |
125 | |
5551 | 126 // set some usefull defaults: |
127 if(!divx4_param.min_quantizer) divx4_param.min_quantizer=2; | |
128 if(!divx4_param.max_quantizer) divx4_param.max_quantizer=31; | |
129 if(!divx4_param.rc_period) divx4_param.rc_period=2000; | |
130 if(!divx4_param.rc_reaction_period) divx4_param.rc_reaction_period=10; | |
131 if(!divx4_param.rc_reaction_ratio) divx4_param.rc_reaction_ratio=20; | |
132 | |
7332 | 133 #ifdef HAVE_XVID_VBR |
134 if (vbrpass >= 0) { | |
135 vbrSetDefaults(&vf->priv->vbr_state); | |
136 vf->priv->vbr_state.desired_bitrate = divx4_param.bitrate; | |
137 switch (vbrpass) { | |
138 case 0: | |
139 vf->priv->vbr_state.mode = VBR_MODE_1PASS; | |
140 break; | |
141 case 1: | |
142 vf->priv->vbr_state.mode = VBR_MODE_2PASS_1; | |
143 break; | |
144 case 2: | |
145 vf->priv->vbr_state.mode = VBR_MODE_2PASS_2; | |
146 break; | |
147 default: | |
148 abort(); | |
149 } | |
150 vf->priv->vbr_state.debug = vbrdebug; | |
151 if (vbrInit(&vf->priv->vbr_state) == -1) | |
152 abort(); | |
153 /* XXX - kludge to workaround some DivX encoder limitations */ | |
154 if (vf->priv->vbr_state.mode != VBR_MODE_2PASS_2) | |
155 divx4_param.min_quantizer = divx4_param.max_quantizer = vbrGetQuant(&vf->priv->vbr_state); | |
156 } | |
157 #endif | |
158 | |
5550 | 159 divx4_param.handle=NULL; |
160 encore(NULL,ENC_OPT_INIT,&divx4_param,NULL); | |
161 vf->priv->enc_handle=divx4_param.handle; | |
162 switch(outfmt){ | |
163 case IMGFMT_YV12: vf->priv->enc_frame.colorspace=ENC_CSP_YV12; break; | |
164 case IMGFMT_IYUV: | |
165 case IMGFMT_I420: vf->priv->enc_frame.colorspace=ENC_CSP_I420; break; | |
166 case IMGFMT_YUY2: vf->priv->enc_frame.colorspace=ENC_CSP_YUY2; break; | |
167 case IMGFMT_UYVY: vf->priv->enc_frame.colorspace=ENC_CSP_UYVY; break; | |
168 case IMGFMT_RGB24: | |
169 case IMGFMT_BGR24: | |
170 vf->priv->enc_frame.colorspace=ENC_CSP_RGB24; break; | |
171 default: | |
172 mp_msg(MSGT_MENCODER,MSGL_ERR,"divx4: unsupported picture format (%s)!\n", | |
173 vo_format_name(outfmt)); | |
174 return 0; | |
175 } | |
176 | |
177 switch(pass){ | |
178 case 1: | |
179 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, divx4_param.quality) == -1){ | |
180 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
181 pass=0; | |
182 } | |
183 break; | |
184 case 2: | |
185 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, | |
186 divx4_param.bitrate, | |
187 divx4_param.framerate, | |
188 divx4_crispness, | |
189 divx4_param.quality) == -1){ | |
190 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
191 pass=0; | |
192 } | |
193 break; | |
194 } | |
195 | |
196 return 1; | |
197 } | |
198 | |
7332 | 199 #ifdef HAVE_XVID_VBR |
200 static void uninit(struct vf_instance_s* vf){ | |
201 if (vbrpass >= 0 && vbrFinish(&vf->priv->vbr_state) == -1) | |
202 abort(); | |
203 } | |
204 #endif | |
205 | |
5550 | 206 static int control(struct vf_instance_s* vf, int request, void* data){ |
207 | |
208 return CONTROL_UNKNOWN; | |
209 } | |
210 | |
211 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
212 switch(fmt){ | |
213 case IMGFMT_YV12: | |
214 case IMGFMT_IYUV: | |
215 case IMGFMT_I420: | |
216 return 3; // no conversion | |
217 case IMGFMT_YUY2: | |
218 case IMGFMT_UYVY: | |
5706 | 219 return 1; // conversion |
5550 | 220 case IMGFMT_RGB24: |
221 case IMGFMT_BGR24: | |
5706 | 222 return 1 | VFCAP_FLIPPED; // conversion+flipped |
5550 | 223 } |
224 return 0; | |
225 } | |
226 | |
7368 | 227 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ |
5550 | 228 ENC_RESULT enc_result; |
229 vf->priv->enc_frame.image=mpi->planes[0]; | |
230 vf->priv->enc_frame.bitstream=mux_v->buffer; | |
231 vf->priv->enc_frame.length=mux_v->buffer_size; | |
232 vf->priv->enc_frame.mvs=NULL; | |
7332 | 233 #ifdef HAVE_XVID_VBR |
234 if (vbrpass >= 0) { | |
235 int quant = vbrGetQuant(&vf->priv->vbr_state); | |
236 int intra = vbrGetIntra(&vf->priv->vbr_state); | |
237 vf->priv->enc_frame.quant = quant ? quant : 1; | |
238 vf->priv->enc_frame.intra = intra; | |
239 /* XXX - kludge to workaround some DivX encoder limitations: | |
240 only pass 2 needs to call encore with VBR, and then it does | |
241 not report quantizer and intra*/ | |
242 if (vf->priv->vbr_state.mode != VBR_MODE_2PASS_2) | |
243 encore(vf->priv->enc_handle, ENC_OPT_ENCODE, &vf->priv->enc_frame, &enc_result); | |
244 else { | |
245 encore(vf->priv->enc_handle, ENC_OPT_ENCODE_VBR, &vf->priv->enc_frame, &enc_result); | |
246 enc_result.quantizer = quant; | |
247 if (intra >= 0) | |
248 enc_result.is_key_frame = intra; | |
249 } | |
250 if (vbrUpdate(&vf->priv->vbr_state, enc_result.quantizer, enc_result.is_key_frame, | |
251 (enc_result.total_bits - enc_result.texture_bits) / 8, enc_result.total_bits / 8, | |
252 0, 0, 0) == -1) | |
253 abort(); | |
254 } | |
255 else | |
256 #endif | |
5550 | 257 if(pass==2){ // handle 2-pass: |
258 vf->priv->enc_frame.quant = VbrControl_get_quant(); | |
259 vf->priv->enc_frame.intra = VbrControl_get_intra(); | |
260 encore(vf->priv->enc_handle,ENC_OPT_ENCODE_VBR,&vf->priv->enc_frame,&enc_result); | |
261 VbrControl_update_2pass_vbr_encoding(enc_result.motion_bits, | |
262 enc_result.texture_bits, | |
263 enc_result.total_bits); | |
264 } else { | |
265 vf->priv->enc_frame.quant=0; | |
266 vf->priv->enc_frame.intra=0; | |
267 encore(vf->priv->enc_handle,ENC_OPT_ENCODE,&vf->priv->enc_frame,&enc_result); | |
268 if(pass==1){ | |
269 VbrControl_update_2pass_vbr_analysis(enc_result.is_key_frame, | |
270 enc_result.motion_bits, | |
271 enc_result.texture_bits, | |
272 enc_result.total_bits, | |
273 enc_result.quantizer); | |
274 } | |
275 } | |
5551 | 276 mencoder_write_chunk(mux_v,vf->priv->enc_frame.length,enc_result.is_key_frame?0x10:0); |
7368 | 277 return 1; |
5550 | 278 } |
279 | |
280 //===========================================================================// | |
281 | |
282 static int vf_open(vf_instance_t *vf, char* args){ | |
283 vf->config=config; | |
284 vf->control=control; | |
285 vf->query_format=query_format; | |
286 vf->put_image=put_image; | |
7332 | 287 #ifdef HAVE_XVID_VBR |
288 vf->uninit = uninit; | |
289 #endif | |
5550 | 290 vf->priv=malloc(sizeof(struct vf_priv_s)); |
291 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
7127 | 292 vf->priv->mux=(aviwrite_stream_t*)args; |
5550 | 293 |
294 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
295 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); | |
296 mux_v->bih->biWidth=0; | |
297 mux_v->bih->biHeight=0; | |
298 mux_v->bih->biPlanes=1; | |
299 mux_v->bih->biBitCount=24; | |
5854 | 300 #if ENCORE_MAJOR_VERSION >= 5010 |
301 mux_v->bih->biCompression=mmioFOURCC('D','X','5','0'); | |
302 #else | |
5550 | 303 mux_v->bih->biCompression=mmioFOURCC('d','i','v','x'); |
5854 | 304 #endif |
5550 | 305 |
306 return 1; | |
307 } | |
308 | |
309 vf_info_t ve_info_divx4 = { | |
310 "divx4 encoder", | |
311 "divx4", | |
312 "A'rpi", | |
313 "for internal use by mencoder", | |
314 vf_open | |
315 }; | |
316 | |
317 //===========================================================================// | |
318 #endif |