Mercurial > mplayer.hg
annotate libmpcodecs/ve_xvid.c @ 8169:7c9253521f9c
A struct setter. It allow you to setup struct from some user
settings.
author | albeu |
---|---|
date | Tue, 12 Nov 2002 14:16:30 +0000 |
parents | 9fc45fe0d444 |
children | 126223903808 |
rev | line source |
---|---|
7456 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
8078 | 4 #include <errno.h> |
7456 | 5 |
6 #include "../config.h" | |
7 #include "../mp_msg.h" | |
8 | |
9 #ifdef HAVE_XVID | |
10 | |
11 #include "codec-cfg.h" | |
12 #include "stream.h" | |
13 #include "demuxer.h" | |
14 #include "stheader.h" | |
15 | |
16 #include "aviwrite.h" | |
17 | |
18 #include "img_format.h" | |
19 #include "mp_image.h" | |
20 #include "vf.h" | |
21 | |
22 #include <xvid.h> | |
23 #include "xvid_vbr.h" | |
24 | |
25 #include "cfgparser.h" | |
26 | |
27 /**********************************************************************/ | |
28 /* Divx4 quality to XviD encoder motion flag presets */ | |
29 static int const divx4_motion_presets[7] = { | |
8078 | 30 0, |
31 PMV_QUICKSTOP16, | |
7456 | 32 PMV_EARLYSTOP16, |
8078 | 33 PMV_EARLYSTOP16 | PMV_EARLYSTOP8, |
34 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, | |
35 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8 | PMV_ADVANCEDDIAMOND16, | |
36 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | | |
37 PMV_HALFPELDIAMOND8 | PMV_USESQUARES16 | |
38 | |
7456 | 39 }; |
40 | |
41 /* Divx4 quality to general encoder flag presets */ | |
42 static int const divx4_general_presets[7] = { | |
43 0, | |
44 XVID_H263QUANT, | |
45 XVID_H263QUANT, | |
46 XVID_H263QUANT | XVID_HALFPEL, | |
47 XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL, | |
48 XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL, | |
49 XVID_H263QUANT | XVID_INTER4V | XVID_HALFPEL | |
50 }; | |
51 | |
8078 | 52 extern char* passtmpfile; |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8078
diff
changeset
|
53 extern void mencoder_write_chunk(aviwrite_stream_t *s,int len,unsigned int flags); |
7456 | 54 |
8078 | 55 static int xvidenc_pass = 0; |
56 static int xvidenc_quality = sizeof(divx4_motion_presets) / sizeof(divx4_motion_presets[0]) - 1; /* best quality */ | |
57 static int xvidenc_bitrate = -1; | |
58 static int xvidenc_rc_reaction_delay_factor = -1; | |
59 static int xvidenc_rc_averaging_period = -1; | |
60 static int xvidenc_rc_buffer = -1; | |
61 static int xvidenc_min_quantizer = 2; | |
62 static int xvidenc_max_quantizer = -1; | |
63 static int xvidenc_min_key_interval = 0; | |
64 static int xvidenc_max_key_interval = -1; | |
65 static int xvidenc_mpeg_quant = 0; | |
66 static int xvidenc_lumi_mask = 0; | |
67 static int xvidenc_keyframe_boost = 0; | |
68 static int xvidenc_kfthreshold = 0; | |
69 static int xvidenc_kfreduction = 0; | |
70 static int xvidenc_fixed_quant = 0; | |
71 static int xvidenc_debug = 0; | |
72 static int xvidenc_hintedme = 0; | |
73 static char* xvidenc_hintfile = "xvid_hint_me.dat"; | |
7458 | 74 |
7456 | 75 struct config xvidencopts_conf[] = { |
8078 | 76 { "pass", &xvidenc_pass, CONF_TYPE_INT, CONF_RANGE, 0, 2, NULL}, |
77 { "quality", &xvidenc_quality, CONF_TYPE_INT, CONF_RANGE, 0, | |
7456 | 78 sizeof(divx4_motion_presets) / sizeof(divx4_motion_presets[0]) - 1, NULL}, |
8078 | 79 { "br", &xvidenc_bitrate, CONF_TYPE_INT, CONF_RANGE, 4, 24000000, NULL}, |
80 { "rc_reaction_delay_factor", &xvidenc_rc_reaction_delay_factor, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
81 { "rc_averaging_period", &xvidenc_rc_averaging_period, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
82 { "rc_buffer", &xvidenc_rc_buffer, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
83 { "min_quantizer", &xvidenc_min_quantizer, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
84 { "max_quantizer", &xvidenc_max_quantizer, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, | |
85 { "min_key_interval", &xvidenc_min_key_interval, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */ | |
86 { "max_key_interval", &xvidenc_max_key_interval, CONF_TYPE_INT, 0, 0, 0, NULL}, | |
87 { "mpeg_quant", &xvidenc_mpeg_quant, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
88 { "lumi_mask", &xvidenc_lumi_mask, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
89 { "keyframe_boost", &xvidenc_keyframe_boost, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */ | |
90 { "kfthreshold", &xvidenc_kfthreshold, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */ | |
91 { "kfreduction", &xvidenc_kfreduction, CONF_TYPE_INT, 0, 0, 0, NULL}, /* for XVID_MODE_2PASS_2 */ | |
92 { "fixed_quant", &xvidenc_fixed_quant, CONF_TYPE_INT, CONF_RANGE, 1, 31, NULL}, /* for XVID_MODE_FIXED_QUANT */ | |
93 { "debug", &xvidenc_debug, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
94 { "hintedme", &xvidenc_hintedme, CONF_TYPE_FLAG, 0, 0, 1, NULL}, | |
95 { "hintfile", &xvidenc_hintfile, CONF_TYPE_STRING, 0, 0, 0, NULL}, | |
7456 | 96 { NULL, NULL, 0, 0, 0, 0, NULL} |
97 }; | |
98 | |
99 struct vf_priv_s { | |
100 aviwrite_stream_t* mux; | |
101 XVID_ENC_FRAME enc_frame; | |
102 void* enc_handle; | |
103 vbr_control_t vbr_state; | |
104 FILE *hintfile; | |
8078 | 105 void *hintstream; |
7456 | 106 }; |
107 | |
108 static int | |
109 config(struct vf_instance_s* vf, | |
110 int width, int height, int d_width, int d_height, | |
111 unsigned int flags, unsigned int outfmt) | |
112 { | |
113 XVID_ENC_PARAM enc_param; | |
8078 | 114 struct vf_priv_s *fp = vf->priv; |
7456 | 115 |
8078 | 116 fp->mux->bih->biWidth = width; |
117 fp->mux->bih->biHeight = height; | |
118 fp->mux->bih->biSizeImage = fp->mux->bih->biWidth * fp->mux->bih->biHeight * 3; | |
119 mp_msg(MSGT_MENCODER,MSGL_INFO,"videocodec: XViD (%dx%d fourcc=%x [%.4s])\n", | |
120 width, height, fp->mux->bih->biCompression, (char *)&fp->mux->bih->biCompression); | |
7456 | 121 |
8078 | 122 // initialize XViD core parameters |
123 // =============================== | |
7456 | 124 memset(&enc_param, 0, sizeof(enc_param)); |
125 enc_param.width = width; | |
126 enc_param.height = height; | |
8078 | 127 enc_param.fincr = fp->mux->h.dwScale; |
128 enc_param.fbase = fp->mux->h.dwRate; | |
129 if (xvidenc_bitrate > 16000) | |
130 enc_param.rc_bitrate = xvidenc_bitrate; | |
131 else if (xvidenc_bitrate > 0) | |
132 enc_param.rc_bitrate = xvidenc_bitrate * 1000; | |
133 else | |
134 enc_param.rc_bitrate = -1; | |
135 enc_param.rc_reaction_delay_factor = xvidenc_rc_reaction_delay_factor; | |
136 enc_param.rc_averaging_period = xvidenc_rc_averaging_period; | |
137 enc_param.rc_buffer = xvidenc_rc_buffer; | |
138 enc_param.min_quantizer = xvidenc_min_quantizer; | |
139 enc_param.max_quantizer = xvidenc_max_quantizer; | |
8123
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8078
diff
changeset
|
140 if( xvidenc_max_key_interval > 0 ) |
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8078
diff
changeset
|
141 enc_param.max_key_interval = xvidenc_max_key_interval; |
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8078
diff
changeset
|
142 else |
9fc45fe0d444
*HUGE* set of compiler warning fixes, unused variables removal
arpi
parents:
8078
diff
changeset
|
143 enc_param.max_key_interval = 10 * enc_param.fbase / enc_param.fincr; |
7456 | 144 switch (xvid_encore(NULL, XVID_ENC_CREATE, &enc_param, NULL)) { |
145 case XVID_ERR_FAIL: | |
8078 | 146 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: encoder creation failed\n"); |
7456 | 147 return 0; |
148 case XVID_ERR_MEMORY: | |
8078 | 149 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: encoder creation failed, out of memory\n"); |
7456 | 150 return 0; |
151 case XVID_ERR_FORMAT: | |
8078 | 152 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: encoder creation failed, bad format\n"); |
7456 | 153 return 0; |
154 } | |
8078 | 155 fp->enc_handle = enc_param.handle; |
7456 | 156 |
8078 | 157 // initialize XViD per-frame static parameters |
158 // =========================================== | |
159 fp->enc_frame.general = divx4_general_presets[xvidenc_quality]; | |
160 fp->enc_frame.motion = divx4_motion_presets[xvidenc_quality]; | |
161 if (xvidenc_mpeg_quant) { | |
162 fp->enc_frame.general &= ~XVID_H263QUANT; | |
163 fp->enc_frame.general |= XVID_MPEGQUANT; | |
164 } | |
165 if (xvidenc_lumi_mask) | |
166 fp->enc_frame.general |= XVID_LUMIMASKING; | |
167 | |
7456 | 168 switch (outfmt) { |
169 case IMGFMT_YV12: | |
8078 | 170 fp->enc_frame.colorspace = XVID_CSP_YV12; |
7456 | 171 break; |
172 case IMGFMT_IYUV: case IMGFMT_I420: | |
8078 | 173 fp->enc_frame.colorspace = XVID_CSP_I420; |
7456 | 174 break; |
175 case IMGFMT_YUY2: | |
8078 | 176 fp->enc_frame.colorspace = XVID_CSP_YUY2; |
7456 | 177 break; |
178 case IMGFMT_UYVY: | |
8078 | 179 fp->enc_frame.colorspace = XVID_CSP_UYVY; |
7456 | 180 break; |
181 case IMGFMT_RGB24: case IMGFMT_BGR24: | |
8078 | 182 fp->enc_frame.colorspace = XVID_CSP_RGB24; |
7456 | 183 break; |
184 default: | |
185 mp_msg(MSGT_MENCODER,MSGL_ERR,"xvid: unsupported picture format (%s)!\n", | |
186 vo_format_name(outfmt)); | |
187 return 0; | |
188 } | |
8078 | 189 fp->enc_frame.quant_intra_matrix = 0; |
190 fp->enc_frame.quant_inter_matrix = 0; | |
191 | |
192 // hinted ME | |
193 fp->hintstream = NULL; | |
194 fp->hintfile = NULL; | |
195 if (xvidenc_hintedme && (xvidenc_pass == 1 || xvidenc_pass == 2)) { | |
196 fp->hintstream = malloc( 100000 ); // this is what the vfw code in XViD CVS allocates | |
197 if (fp->hintstream == NULL) | |
198 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: cannot allocate memory for hinted ME\n"); | |
199 else { | |
200 fp->hintfile = fopen(xvidenc_hintfile, xvidenc_pass == 1 ? "w" : "r"); | |
201 if (fp->hintfile == NULL) { | |
202 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: %s: %s\n", strerror(errno), xvidenc_hintfile); | |
203 free(fp->hintstream); | |
204 } | |
205 } | |
206 if (fp->hintstream == NULL || fp->hintfile == NULL) | |
207 xvidenc_hintedme = 0; | |
208 } | |
7456 | 209 |
8078 | 210 // initialize VBR engine |
211 // ===================== | |
212 vbrSetDefaults(&fp->vbr_state); | |
213 if (xvidenc_pass == 0) { | |
214 if (xvidenc_fixed_quant >= 1) { | |
215 fp->vbr_state.mode = VBR_MODE_FIXED_QUANT; | |
216 fp->vbr_state.fixed_quant = xvidenc_fixed_quant; | |
217 } else | |
218 fp->vbr_state.mode = VBR_MODE_1PASS; | |
7456 | 219 } |
8078 | 220 else if (xvidenc_pass == 1) |
221 fp->vbr_state.mode = VBR_MODE_2PASS_1; | |
222 else if (xvidenc_pass == 2) | |
223 fp->vbr_state.mode = VBR_MODE_2PASS_2; | |
224 else | |
225 return -1; | |
226 fp->vbr_state.fps = (double)enc_param.fbase / enc_param.fincr; | |
227 fp->vbr_state.filename = passtmpfile; | |
228 fp->vbr_state.desired_bitrate = enc_param.rc_bitrate; | |
229 fp->vbr_state.min_iquant = fp->vbr_state.min_pquant = enc_param.min_quantizer; | |
230 fp->vbr_state.max_iquant = fp->vbr_state.max_pquant = enc_param.max_quantizer; | |
231 if (xvidenc_keyframe_boost) | |
232 fp->vbr_state.keyframe_boost = xvidenc_keyframe_boost; | |
233 if (xvidenc_kfthreshold) | |
234 fp->vbr_state.kftreshold = xvidenc_kfthreshold; | |
235 if (xvidenc_kfreduction) | |
236 fp->vbr_state.kfreduction = xvidenc_kfreduction; | |
237 if (xvidenc_min_key_interval) | |
238 fp->vbr_state.min_key_interval = xvidenc_min_key_interval; | |
239 fp->vbr_state.max_key_interval = enc_param.max_key_interval; | |
240 fp->vbr_state.debug = xvidenc_debug; | |
241 vbrInit(&fp->vbr_state); | |
242 | |
7456 | 243 return 1; |
244 } | |
245 | |
246 static void | |
247 uninit(struct vf_instance_s* vf) | |
248 { | |
8078 | 249 struct vf_priv_s *fp = vf->priv; |
250 | |
251 if (fp->hintfile) | |
252 fclose(fp->hintfile); | |
253 if (fp->hintstream) | |
254 free(fp->hintstream); | |
255 vbrFinish(&fp->vbr_state); | |
7456 | 256 } |
257 | |
258 static int | |
259 control(struct vf_instance_s* vf, int request, void* data) | |
260 { | |
261 return CONTROL_UNKNOWN; | |
262 } | |
263 | |
264 static int | |
265 query_format(struct vf_instance_s* vf, unsigned int fmt) | |
266 { | |
267 switch(fmt){ | |
268 case IMGFMT_YV12: case IMGFMT_IYUV: case IMGFMT_I420: | |
269 return VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW; | |
270 case IMGFMT_YUY2: case IMGFMT_UYVY: | |
271 return VFCAP_CSP_SUPPORTED; | |
272 case IMGFMT_RGB24: case IMGFMT_BGR24: | |
273 return VFCAP_CSP_SUPPORTED | VFCAP_FLIPPED; | |
274 } | |
275 return 0; | |
276 } | |
277 | |
278 static int | |
279 put_image(struct vf_instance_s* vf, mp_image_t *mpi) | |
280 { | |
281 XVID_ENC_STATS enc_stats; | |
8078 | 282 struct vf_priv_s *fp = vf->priv; |
7456 | 283 |
8078 | 284 fp->enc_frame.bitstream = fp->mux->buffer; |
285 fp->enc_frame.length = -1 /* fp->mux->buffer_size */; | |
286 fp->enc_frame.image = mpi->planes[0]; | |
287 fp->enc_frame.quant = vbrGetQuant(&fp->vbr_state); | |
288 fp->enc_frame.intra = vbrGetIntra(&fp->vbr_state); | |
289 if (xvidenc_hintedme && xvidenc_pass == 1) { | |
290 fp->enc_frame.hint.hintstream = fp->hintstream; | |
291 fp->enc_frame.hint.rawhints = 0; | |
292 fp->enc_frame.general |= XVID_HINTEDME_GET; | |
7456 | 293 } |
8078 | 294 else if (xvidenc_hintedme && xvidenc_pass == 2) { |
295 size_t read; | |
296 int blocksize; | |
297 fp->enc_frame.general &= ~XVID_HINTEDME_SET; | |
298 read = fread(&blocksize, sizeof(blocksize), 1, fp->hintfile); | |
299 if (read == 1) { | |
300 read = fread(fp->hintstream, (size_t)blocksize, 1, fp->hintfile); | |
7456 | 301 if (read == 1) { |
8078 | 302 fp->enc_frame.hint.hintstream = fp->hintstream; |
303 fp->enc_frame.hint.hintlength = 0; | |
304 fp->enc_frame.hint.rawhints = 0; | |
305 fp->enc_frame.general |= XVID_HINTEDME_SET; | |
306 } | |
7456 | 307 else |
8078 | 308 perror("xvid: hint file read block failure"); |
309 } | |
310 else | |
311 perror("xvid: hint file read failure"); | |
7456 | 312 } |
8078 | 313 switch (xvid_encore(fp->enc_handle, XVID_ENC_ENCODE, &fp->enc_frame, &enc_stats)) { |
7456 | 314 case XVID_ERR_OK: |
315 break; | |
316 case XVID_ERR_MEMORY: | |
317 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: out of memory\n"); | |
318 break; | |
319 case XVID_ERR_FORMAT: | |
320 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: bad format\n"); | |
321 break; | |
322 default: | |
323 mp_msg(MSGT_MENCODER, MSGL_ERR, "xvid: failure\n"); | |
324 break; | |
325 } | |
8078 | 326 mencoder_write_chunk(fp->mux, fp->enc_frame.length, fp->enc_frame.intra ? 0x10 : 0); |
327 vbrUpdate(&fp->vbr_state, enc_stats.quant, fp->enc_frame.intra, | |
328 enc_stats.hlength, fp->enc_frame.length, enc_stats.kblks, enc_stats.mblks, enc_stats.ublks); | |
329 if (fp->enc_frame.general & XVID_HINTEDME_GET) { | |
330 size_t wrote = fwrite(&fp->enc_frame.hint.hintlength, sizeof(fp->enc_frame.hint.hintlength), 1, fp->hintfile); | |
331 if (wrote == 1) { | |
332 wrote = fwrite(fp->enc_frame.hint.hintstream, fp->enc_frame.hint.hintlength, 1, fp->hintfile); | |
333 if (wrote != 1) | |
334 perror("xvid: hint write block failure"); | |
7456 | 335 } |
8078 | 336 else |
337 perror("xvid: hint write failure"); | |
7456 | 338 } |
339 return 1; | |
340 } | |
341 | |
342 //===========================================================================// | |
343 | |
344 static int | |
345 vf_open(vf_instance_t *vf, char* args) | |
346 { | |
347 XVID_INIT_PARAM params = { 0, 0, 0}; | |
348 vf->config = config; | |
349 vf->control = control; | |
350 vf->uninit = uninit; | |
351 vf->query_format = query_format; | |
352 vf->put_image = put_image; | |
353 vf->priv = malloc(sizeof(struct vf_priv_s)); | |
354 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
355 vf->priv->mux = (aviwrite_stream_t*)args; | |
356 | |
357 vf->priv->mux->bih = malloc(sizeof(BITMAPINFOHEADER)); | |
358 vf->priv->mux->bih->biSize = sizeof(BITMAPINFOHEADER); | |
359 vf->priv->mux->bih->biWidth = 0; | |
360 vf->priv->mux->bih->biHeight = 0; | |
361 vf->priv->mux->bih->biPlanes = 1; | |
362 vf->priv->mux->bih->biBitCount = 24; | |
363 vf->priv->mux->bih->biCompression = mmioFOURCC('X','V','I','D'); | |
364 | |
365 if (xvid_init(NULL, 0, ¶ms, NULL) != XVID_ERR_OK) { | |
8078 | 366 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: initialisation failure\n"); |
7456 | 367 abort(); |
368 } | |
369 if (params.api_version != API_VERSION) { | |
8078 | 370 mp_msg(MSGT_MENCODER,MSGL_ERR, "xvid: XviD library API version mismatch\n" |
7456 | 371 "\texpected %d.%d, got %d.%d, you should recompile MPlayer.\n", |
372 API_VERSION >> 16, API_VERSION & 0xff, | |
373 params.api_version >> 16, params.api_version & 0xff); | |
374 abort(); | |
375 } | |
376 | |
377 return 1; | |
378 } | |
379 | |
380 vf_info_t ve_info_xvid = { | |
381 "XviD encoder", | |
382 "xvid", | |
8078 | 383 "Kim Minh Kaplan & Rémi Guyomarch", |
7456 | 384 "for internal use by mencoder", |
385 vf_open | |
386 }; | |
387 | |
388 //===========================================================================// | |
389 #endif |