Mercurial > mplayer.hg
annotate libmpcodecs/ve_divx4.c @ 9679:beebca423182
Spellchecked, slightly reworded, parts corrected.
author | diego |
---|---|
date | Mon, 24 Mar 2003 17:40:44 +0000 |
parents | 32be26de0d7c |
children | d275152390ee |
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 | |
8585 | 16 #include "muxer.h" |
5550 | 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; |
47 | |
9300
32be26de0d7c
cleanup detection of various divx4 versions/alternatives
arpi
parents:
9014
diff
changeset
|
48 #ifdef ENCORE_XVID |
32be26de0d7c
cleanup detection of various divx4 versions/alternatives
arpi
parents:
9014
diff
changeset
|
49 #include <divx4.h> |
32be26de0d7c
cleanup detection of various divx4 versions/alternatives
arpi
parents:
9014
diff
changeset
|
50 #else |
5550 | 51 #include <encore2.h> |
9300
32be26de0d7c
cleanup detection of various divx4 versions/alternatives
arpi
parents:
9014
diff
changeset
|
52 #endif |
5550 | 53 |
5854 | 54 #ifndef ENCORE_MAJOR_VERSION |
55 #define ENCORE_MAJOR_VERSION 4000 | |
56 #endif | |
57 | |
5550 | 58 ENC_PARAM divx4_param; |
59 int divx4_crispness; | |
7332 | 60 #ifdef HAVE_XVID_VBR |
61 static int vbrpass = -1; | |
62 static int vbrdebug = 0; | |
63 #endif | |
5550 | 64 |
65 #include "cfgparser.h" | |
66 | |
67 struct config divx4opts_conf[]={ | |
8032
136e7e515764
removed -pass nonsense from mencoder! it's now a suboption to -divx4opts
rfelker
parents:
7368
diff
changeset
|
68 {"pass", &pass, CONF_TYPE_INT, CONF_RANGE,0,2, NULL}, |
5550 | 69 {"br", &divx4_param.bitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, |
70 {"rc_period", &divx4_param.rc_period, CONF_TYPE_INT, 0,0,0, NULL}, | |
71 {"rc_reaction_period", &divx4_param.rc_reaction_period, CONF_TYPE_INT, 0,0,0, NULL}, | |
72 {"rc_reaction_ratio", &divx4_param.rc_reaction_ratio, CONF_TYPE_INT, 0,0,0, NULL}, | |
73 {"min_quant", &divx4_param.min_quantizer, CONF_TYPE_INT, CONF_RANGE,0,32, NULL}, | |
74 {"max_quant", &divx4_param.max_quantizer, CONF_TYPE_INT, CONF_RANGE,0,32, NULL}, | |
75 {"key", &divx4_param.max_key_interval, CONF_TYPE_INT, CONF_MIN,0,0, NULL}, | |
76 {"deinterlace", &divx4_param.deinterlace, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
77 {"q", &divx4_param.quality, CONF_TYPE_INT, CONF_RANGE, 1, 5, NULL}, | |
78 {"crispness", &divx4_crispness, CONF_TYPE_INT, CONF_RANGE,0,100, NULL}, | |
5854 | 79 #if ENCORE_MAJOR_VERSION >= 5010 |
80 {"bidirect", &divx4_param.extensions.use_bidirect, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
81 {"obmc", &divx4_param.extensions.obmc, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
82 {"data_partitioning", &divx4_param.extensions.data_partitioning, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
83 {"qpel", &divx4_param.extensions.quarter_pel, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
84 {"intra_frame_threshold", &divx4_param.extensions.intra_frame_threshold, CONF_TYPE_INT, CONF_RANGE,1,100, NULL}, | |
85 {"psychovisual", &divx4_param.extensions.psychovisual, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
86 {"testing_param", &divx4_param.extensions.testing_param, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
87 {"gmc", &divx4_param.extensions.use_gmc, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
88 {"interlace_mode", &divx4_param.extensions.interlace_mode, CONF_TYPE_INT, CONF_RANGE,0,2, NULL}, | |
89 {"temporal", &divx4_param.extensions.temporal_enable, CONF_TYPE_FLAG, 0,0,1, NULL}, | |
90 {"spatial", &divx4_param.extensions.spatial_passes, CONF_TYPE_INT, 0,0,1, NULL}, | |
91 {"mv_file", &divx4_param.extensions.mv_file, CONF_TYPE_STRING, 0,0,1, NULL}, | |
92 #endif | |
7332 | 93 #ifdef HAVE_XVID_VBR |
94 {"vbrpass", &vbrpass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, | |
95 {"vbrdebug", &vbrdebug, CONF_TYPE_INT, CONF_RANGE, 0, 1, NULL}, | |
96 #endif | |
5550 | 97 {"help", "TODO: divx4opts help!\n", CONF_TYPE_PRINT, CONF_NOCFG, 0, 0, NULL}, |
98 {NULL, NULL, 0, 0, 0, 0, NULL} | |
99 }; | |
100 | |
101 struct vf_priv_s { | |
8585 | 102 muxer_stream_t* mux; |
5550 | 103 ENC_RESULT enc_result; |
104 ENC_FRAME enc_frame; | |
105 void* enc_handle; | |
7332 | 106 #ifdef HAVE_XVID_VBR |
107 vbr_control_t vbr_state; | |
108 #endif | |
5550 | 109 }; |
110 | |
111 #define mux_v (vf->priv->mux) | |
112 | |
113 static int config(struct vf_instance_s* vf, | |
114 int width, int height, int d_width, int d_height, | |
115 unsigned int flags, unsigned int outfmt){ | |
116 | |
117 mux_v->bih->biWidth=width; | |
118 mux_v->bih->biHeight=height; | |
119 | |
120 divx4_param.x_dim=width; | |
121 divx4_param.y_dim=height; | |
122 divx4_param.framerate=(float)mux_v->h.dwRate/mux_v->h.dwScale; | |
123 mux_v->bih->biSizeImage=mux_v->bih->biWidth*mux_v->bih->biHeight*3; | |
124 | |
125 if(!divx4_param.bitrate) divx4_param.bitrate=800000; | |
126 else if(divx4_param.bitrate<=16000) divx4_param.bitrate*=1000; | |
127 if(!divx4_param.quality) divx4_param.quality=5; // the quality of compression ( 1 - fastest, 5 - best ) | |
128 | |
5551 | 129 // set some usefull defaults: |
130 if(!divx4_param.min_quantizer) divx4_param.min_quantizer=2; | |
131 if(!divx4_param.max_quantizer) divx4_param.max_quantizer=31; | |
132 if(!divx4_param.rc_period) divx4_param.rc_period=2000; | |
133 if(!divx4_param.rc_reaction_period) divx4_param.rc_reaction_period=10; | |
134 if(!divx4_param.rc_reaction_ratio) divx4_param.rc_reaction_ratio=20; | |
135 | |
7332 | 136 #ifdef HAVE_XVID_VBR |
137 if (vbrpass >= 0) { | |
138 vbrSetDefaults(&vf->priv->vbr_state); | |
139 vf->priv->vbr_state.desired_bitrate = divx4_param.bitrate; | |
140 switch (vbrpass) { | |
141 case 0: | |
142 vf->priv->vbr_state.mode = VBR_MODE_1PASS; | |
143 break; | |
144 case 1: | |
145 vf->priv->vbr_state.mode = VBR_MODE_2PASS_1; | |
146 break; | |
147 case 2: | |
148 vf->priv->vbr_state.mode = VBR_MODE_2PASS_2; | |
149 break; | |
150 default: | |
151 abort(); | |
152 } | |
153 vf->priv->vbr_state.debug = vbrdebug; | |
154 if (vbrInit(&vf->priv->vbr_state) == -1) | |
155 abort(); | |
156 /* XXX - kludge to workaround some DivX encoder limitations */ | |
157 if (vf->priv->vbr_state.mode != VBR_MODE_2PASS_2) | |
158 divx4_param.min_quantizer = divx4_param.max_quantizer = vbrGetQuant(&vf->priv->vbr_state); | |
159 } | |
160 #endif | |
161 | |
5550 | 162 divx4_param.handle=NULL; |
163 encore(NULL,ENC_OPT_INIT,&divx4_param,NULL); | |
164 vf->priv->enc_handle=divx4_param.handle; | |
165 switch(outfmt){ | |
166 case IMGFMT_YV12: vf->priv->enc_frame.colorspace=ENC_CSP_YV12; break; | |
167 case IMGFMT_IYUV: | |
168 case IMGFMT_I420: vf->priv->enc_frame.colorspace=ENC_CSP_I420; break; | |
169 case IMGFMT_YUY2: vf->priv->enc_frame.colorspace=ENC_CSP_YUY2; break; | |
170 case IMGFMT_UYVY: vf->priv->enc_frame.colorspace=ENC_CSP_UYVY; break; | |
171 case IMGFMT_RGB24: | |
172 case IMGFMT_BGR24: | |
173 vf->priv->enc_frame.colorspace=ENC_CSP_RGB24; break; | |
174 default: | |
175 mp_msg(MSGT_MENCODER,MSGL_ERR,"divx4: unsupported picture format (%s)!\n", | |
176 vo_format_name(outfmt)); | |
177 return 0; | |
178 } | |
179 | |
180 switch(pass){ | |
181 case 1: | |
182 if (VbrControl_init_2pass_vbr_analysis(passtmpfile, divx4_param.quality) == -1){ | |
183 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
184 pass=0; | |
185 } | |
186 break; | |
187 case 2: | |
188 if (VbrControl_init_2pass_vbr_encoding(passtmpfile, | |
189 divx4_param.bitrate, | |
190 divx4_param.framerate, | |
191 divx4_crispness, | |
192 divx4_param.quality) == -1){ | |
193 mp_msg(MSGT_MENCODER,MSGL_ERR,"2pass failed: filename=%s\n", passtmpfile); | |
194 pass=0; | |
195 } | |
196 break; | |
197 } | |
198 | |
199 return 1; | |
200 } | |
201 | |
7332 | 202 #ifdef HAVE_XVID_VBR |
203 static void uninit(struct vf_instance_s* vf){ | |
204 if (vbrpass >= 0 && vbrFinish(&vf->priv->vbr_state) == -1) | |
205 abort(); | |
206 } | |
207 #endif | |
208 | |
5550 | 209 static int control(struct vf_instance_s* vf, int request, void* data){ |
210 | |
211 return CONTROL_UNKNOWN; | |
212 } | |
213 | |
214 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
215 switch(fmt){ | |
216 case IMGFMT_YV12: | |
217 case IMGFMT_IYUV: | |
218 case IMGFMT_I420: | |
219 return 3; // no conversion | |
220 case IMGFMT_YUY2: | |
221 case IMGFMT_UYVY: | |
5706 | 222 return 1; // conversion |
5550 | 223 case IMGFMT_RGB24: |
224 case IMGFMT_BGR24: | |
5706 | 225 return 1 | VFCAP_FLIPPED; // conversion+flipped |
5550 | 226 } |
227 return 0; | |
228 } | |
229 | |
7368 | 230 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ |
5550 | 231 ENC_RESULT enc_result; |
232 vf->priv->enc_frame.image=mpi->planes[0]; | |
233 vf->priv->enc_frame.bitstream=mux_v->buffer; | |
234 vf->priv->enc_frame.length=mux_v->buffer_size; | |
235 vf->priv->enc_frame.mvs=NULL; | |
7332 | 236 #ifdef HAVE_XVID_VBR |
237 if (vbrpass >= 0) { | |
238 int quant = vbrGetQuant(&vf->priv->vbr_state); | |
239 int intra = vbrGetIntra(&vf->priv->vbr_state); | |
240 vf->priv->enc_frame.quant = quant ? quant : 1; | |
241 vf->priv->enc_frame.intra = intra; | |
242 /* XXX - kludge to workaround some DivX encoder limitations: | |
243 only pass 2 needs to call encore with VBR, and then it does | |
244 not report quantizer and intra*/ | |
245 if (vf->priv->vbr_state.mode != VBR_MODE_2PASS_2) | |
246 encore(vf->priv->enc_handle, ENC_OPT_ENCODE, &vf->priv->enc_frame, &enc_result); | |
247 else { | |
248 encore(vf->priv->enc_handle, ENC_OPT_ENCODE_VBR, &vf->priv->enc_frame, &enc_result); | |
249 enc_result.quantizer = quant; | |
250 if (intra >= 0) | |
251 enc_result.is_key_frame = intra; | |
252 } | |
253 if (vbrUpdate(&vf->priv->vbr_state, enc_result.quantizer, enc_result.is_key_frame, | |
254 (enc_result.total_bits - enc_result.texture_bits) / 8, enc_result.total_bits / 8, | |
255 0, 0, 0) == -1) | |
256 abort(); | |
257 } | |
258 else | |
259 #endif | |
5550 | 260 if(pass==2){ // handle 2-pass: |
261 vf->priv->enc_frame.quant = VbrControl_get_quant(); | |
262 vf->priv->enc_frame.intra = VbrControl_get_intra(); | |
263 encore(vf->priv->enc_handle,ENC_OPT_ENCODE_VBR,&vf->priv->enc_frame,&enc_result); | |
264 VbrControl_update_2pass_vbr_encoding(enc_result.motion_bits, | |
265 enc_result.texture_bits, | |
266 enc_result.total_bits); | |
267 } else { | |
268 vf->priv->enc_frame.quant=0; | |
269 vf->priv->enc_frame.intra=0; | |
270 encore(vf->priv->enc_handle,ENC_OPT_ENCODE,&vf->priv->enc_frame,&enc_result); | |
271 if(pass==1){ | |
272 VbrControl_update_2pass_vbr_analysis(enc_result.is_key_frame, | |
273 enc_result.motion_bits, | |
274 enc_result.texture_bits, | |
275 enc_result.total_bits, | |
276 enc_result.quantizer); | |
277 } | |
278 } | |
9014
c671e9adbe22
Cleanup of the muxer API, func parameters muxer & muxer_f eliminated.
arpi
parents:
8585
diff
changeset
|
279 muxer_write_chunk(mux_v,vf->priv->enc_frame.length,enc_result.is_key_frame?0x10:0); |
7368 | 280 return 1; |
5550 | 281 } |
282 | |
283 //===========================================================================// | |
284 | |
285 static int vf_open(vf_instance_t *vf, char* args){ | |
286 vf->config=config; | |
287 vf->control=control; | |
288 vf->query_format=query_format; | |
289 vf->put_image=put_image; | |
7332 | 290 #ifdef HAVE_XVID_VBR |
291 vf->uninit = uninit; | |
292 #endif | |
5550 | 293 vf->priv=malloc(sizeof(struct vf_priv_s)); |
294 memset(vf->priv,0,sizeof(struct vf_priv_s)); | |
8585 | 295 vf->priv->mux=(muxer_stream_t*)args; |
5550 | 296 |
297 mux_v->bih=malloc(sizeof(BITMAPINFOHEADER)); | |
298 mux_v->bih->biSize=sizeof(BITMAPINFOHEADER); | |
299 mux_v->bih->biWidth=0; | |
300 mux_v->bih->biHeight=0; | |
301 mux_v->bih->biPlanes=1; | |
302 mux_v->bih->biBitCount=24; | |
5854 | 303 #if ENCORE_MAJOR_VERSION >= 5010 |
304 mux_v->bih->biCompression=mmioFOURCC('D','X','5','0'); | |
305 #else | |
5550 | 306 mux_v->bih->biCompression=mmioFOURCC('d','i','v','x'); |
5854 | 307 #endif |
5550 | 308 |
309 return 1; | |
310 } | |
311 | |
312 vf_info_t ve_info_divx4 = { | |
313 "divx4 encoder", | |
314 "divx4", | |
315 "A'rpi", | |
316 "for internal use by mencoder", | |
317 vf_open | |
318 }; | |
319 | |
320 //===========================================================================// | |
321 #endif |