Parent Directory
|
Revision Log
Revision 1032 - (view) (download)
1 : | suxen_drol | 889 | /************************************************************************** |
2 : | * | ||
3 : | * XVID VFW FRONTEND | ||
4 : | * codec | ||
5 : | * | ||
6 : | * This program is free software; you can redistribute it and/or modify | ||
7 : | * it under the terms of the GNU General Public License as published by | ||
8 : | * the Free Software Foundation; either version 2 of the License, or | ||
9 : | * (at your option) any later version. | ||
10 : | * | ||
11 : | * This program is distributed in the hope that it will be useful, | ||
12 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 : | * GNU General Public License for more details. | ||
15 : | * | ||
16 : | * You should have received a copy of the GNU General Public License | ||
17 : | * along with this program; if not, write to the Free Software | ||
18 : | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
19 : | * | ||
20 : | *************************************************************************/ | ||
21 : | |||
22 : | /************************************************************************** | ||
23 : | * | ||
24 : | * History: | ||
25 : | * | ||
26 : | * 12.07.2002 num_threads | ||
27 : | * 23.06.2002 XVID_CPU_CHKONLY; loading speed up | ||
28 : | * 25.04.2002 ICDECOMPRESS_PREROLL | ||
29 : | * 17.04.2002 re-enabled lumi masking for 1st pass | ||
30 : | * 15.04.2002 updated cbr support | ||
31 : | * 04.04.2002 separated 2-pass code to 2pass.c | ||
32 : | * interlacing support | ||
33 : | * hinted ME support | ||
34 : | * 23.03.2002 daniel smith <danielsmith@astroboymail.com> | ||
35 : | * changed inter4v to only be in modes 5 or 6 | ||
36 : | * fixed null mode crash ? | ||
37 : | * merged foxer's alternative 2-pass code | ||
38 : | * added DEBUGERR output on errors instead of returning | ||
39 : | * 16.03.2002 daniel smith <danielsmith@astroboymail.com> | ||
40 : | * changed BITMAPV4HEADER to BITMAPINFOHEADER | ||
41 : | * - prevents memcpy crash in compress_get_format() | ||
42 : | * credits are processed in external 2pass mode | ||
43 : | * motion search precision = 0 now effective in 2-pass | ||
44 : | * modulated quantization | ||
45 : | * added DX50 fourcc | ||
46 : | * 01.12.2001 inital version; (c)2001 peter ross <pross@xvid.org> | ||
47 : | * | ||
48 : | *************************************************************************/ | ||
49 : | |||
50 : | #include <windows.h> | ||
51 : | #include <vfw.h> | ||
52 : | suxen_drol | 1017 | #include "vfwext.h" |
53 : | suxen_drol | 889 | |
54 : | suxen_drol | 983 | #include <xvid.h> |
55 : | #include "debug.h" | ||
56 : | suxen_drol | 889 | #include "codec.h" |
57 : | |||
58 : | suxen_drol | 983 | |
59 : | static const int pmvfast_presets[7] = { | ||
60 : | suxen_drol | 889 | 0, 0, 0, 0, |
61 : | edgomez | 949 | 0 | XVID_ME_HALFPELREFINE16 | 0, |
62 : | 0 | XVID_ME_HALFPELREFINE16 | 0 | | ||
63 : | XVID_ME_ADVANCEDDIAMOND16, XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | | ||
64 : | XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 | ||
65 : | suxen_drol | 889 | }; |
66 : | |||
67 : | suxen_drol | 983 | |
68 : | |||
69 : | suxen_drol | 889 | /* return xvid compatbile colorspace, |
70 : | or XVID_CSP_NULL if failure | ||
71 : | */ | ||
72 : | |||
73 : | int get_colorspace(BITMAPINFOHEADER * hdr) | ||
74 : | { | ||
75 : | /* rgb only: negative height specifies top down image */ | ||
76 : | int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP); | ||
77 : | |||
78 : | switch(hdr->biCompression) | ||
79 : | { | ||
80 : | case BI_RGB : | ||
81 : | if (hdr->biBitCount == 16) | ||
82 : | { | ||
83 : | suxen_drol | 983 | DPRINTF("RGB16 (RGB555)"); |
84 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_RGB555; |
85 : | } | ||
86 : | if (hdr->biBitCount == 24) | ||
87 : | { | ||
88 : | suxen_drol | 983 | DPRINTF("RGB24"); |
89 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_BGR; |
90 : | } | ||
91 : | if (hdr->biBitCount == 32) | ||
92 : | { | ||
93 : | suxen_drol | 983 | DPRINTF("RGB32"); |
94 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_BGRA; |
95 : | } | ||
96 : | |||
97 : | suxen_drol | 983 | DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount); |
98 : | suxen_drol | 889 | return XVID_CSP_NULL; |
99 : | |||
100 : | case BI_BITFIELDS : | ||
101 : | if (hdr->biSize >= sizeof(BITMAPV4HEADER)) | ||
102 : | { | ||
103 : | BITMAPV4HEADER * hdr4 = (BITMAPV4HEADER *)hdr; | ||
104 : | |||
105 : | if (hdr4->bV4BitCount == 16 && | ||
106 : | hdr4->bV4RedMask == 0x7c00 && | ||
107 : | hdr4->bV4GreenMask == 0x3e0 && | ||
108 : | hdr4->bV4BlueMask == 0x1f) | ||
109 : | { | ||
110 : | suxen_drol | 983 | DPRINTF("RGB555"); |
111 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_RGB555; |
112 : | } | ||
113 : | |||
114 : | if(hdr4->bV4BitCount == 16 && | ||
115 : | hdr4->bV4RedMask == 0xf800 && | ||
116 : | hdr4->bV4GreenMask == 0x7e0 && | ||
117 : | hdr4->bV4BlueMask == 0x1f) | ||
118 : | { | ||
119 : | suxen_drol | 983 | DPRINTF("RGB565"); |
120 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_RGB565; |
121 : | } | ||
122 : | |||
123 : | suxen_drol | 983 | DPRINTF("unsupported BI_BITFIELDS mode"); |
124 : | suxen_drol | 889 | return XVID_CSP_NULL; |
125 : | } | ||
126 : | |||
127 : | suxen_drol | 983 | DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
128 : | suxen_drol | 889 | return XVID_CSP_NULL; |
129 : | |||
130 : | case FOURCC_I420 : | ||
131 : | case FOURCC_IYUV : | ||
132 : | suxen_drol | 983 | DPRINTF("IYUY"); |
133 : | suxen_drol | 889 | return XVID_CSP_I420; |
134 : | |||
135 : | case FOURCC_YV12 : | ||
136 : | suxen_drol | 983 | DPRINTF("YV12"); |
137 : | suxen_drol | 889 | return XVID_CSP_YV12; |
138 : | |||
139 : | case FOURCC_YUYV : | ||
140 : | case FOURCC_YUY2 : | ||
141 : | suxen_drol | 983 | DPRINTF("YUY2"); |
142 : | suxen_drol | 889 | return XVID_CSP_YUY2; |
143 : | |||
144 : | case FOURCC_YVYU : | ||
145 : | suxen_drol | 983 | DPRINTF("YVYU"); |
146 : | suxen_drol | 889 | return XVID_CSP_YVYU; |
147 : | |||
148 : | case FOURCC_UYVY : | ||
149 : | suxen_drol | 983 | DPRINTF("UYVY"); |
150 : | suxen_drol | 889 | return XVID_CSP_UYVY; |
151 : | |||
152 : | default : | ||
153 : | suxen_drol | 983 | DPRINTF("unsupported colorspace %c%c%c%c", |
154 : | hdr->biCompression&0xff, | ||
155 : | (hdr->biCompression>>8)&0xff, | ||
156 : | (hdr->biCompression>>16)&0xff, | ||
157 : | (hdr->biCompression>>24)&0xff); | ||
158 : | suxen_drol | 889 | return XVID_CSP_NULL; |
159 : | } | ||
160 : | } | ||
161 : | |||
162 : | |||
163 : | /* compressor */ | ||
164 : | |||
165 : | |||
166 : | /* test the output format */ | ||
167 : | |||
168 : | LRESULT compress_query(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
169 : | { | ||
170 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
171 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
172 : | |||
173 : | suxen_drol | 1017 | /* VFWEXT detection */ |
174 : | if (inhdr->biCompression == VFWEXT_FOURCC) { | ||
175 : | return (ICM_USER+0x0fff); | ||
176 : | } | ||
177 : | |||
178 : | suxen_drol | 889 | if (get_colorspace(inhdr) == XVID_CSP_NULL) |
179 : | { | ||
180 : | return ICERR_BADFORMAT; | ||
181 : | } | ||
182 : | |||
183 : | if (lpbiOutput == NULL) | ||
184 : | { | ||
185 : | return ICERR_OK; | ||
186 : | } | ||
187 : | |||
188 : | if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight || | ||
189 : | (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX && outhdr->biCompression != FOURCC_DX50)) | ||
190 : | { | ||
191 : | return ICERR_BADFORMAT; | ||
192 : | } | ||
193 : | |||
194 : | return ICERR_OK; | ||
195 : | } | ||
196 : | |||
197 : | |||
198 : | LRESULT compress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
199 : | { | ||
200 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
201 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
202 : | |||
203 : | if (get_colorspace(inhdr) == XVID_CSP_NULL) | ||
204 : | { | ||
205 : | return ICERR_BADFORMAT; | ||
206 : | } | ||
207 : | |||
208 : | if (lpbiOutput == NULL) | ||
209 : | { | ||
210 : | return sizeof(BITMAPV4HEADER); | ||
211 : | } | ||
212 : | |||
213 : | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); | ||
214 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
215 : | outhdr->biSizeImage = compress_get_size(codec, lpbiInput, lpbiOutput); | ||
216 : | outhdr->biXPelsPerMeter = 0; | ||
217 : | outhdr->biYPelsPerMeter = 0; | ||
218 : | outhdr->biClrUsed = 0; | ||
219 : | outhdr->biClrImportant = 0; | ||
220 : | |||
221 : | if (codec->config.fourcc_used == 0) | ||
222 : | { | ||
223 : | outhdr->biCompression = FOURCC_XVID; | ||
224 : | } | ||
225 : | else if (codec->config.fourcc_used == 1) | ||
226 : | { | ||
227 : | outhdr->biCompression = FOURCC_DIVX; | ||
228 : | } | ||
229 : | else | ||
230 : | { | ||
231 : | outhdr->biCompression = FOURCC_DX50; | ||
232 : | } | ||
233 : | |||
234 : | return ICERR_OK; | ||
235 : | } | ||
236 : | |||
237 : | |||
238 : | LRESULT compress_get_size(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
239 : | { | ||
240 : | return 2 * lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3; | ||
241 : | } | ||
242 : | |||
243 : | |||
244 : | LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) | ||
245 : | { | ||
246 : | suxen_drol | 1017 | //DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); |
247 : | suxen_drol | 889 | codec->fincr = icf->dwScale; |
248 : | codec->fbase = icf->dwRate; | ||
249 : | return ICERR_OK; | ||
250 : | } | ||
251 : | |||
252 : | |||
253 : | suxen_drol | 983 | const char type2char(int type) |
254 : | { | ||
255 : | if (type==XVID_TYPE_IVOP) | ||
256 : | return 'I'; | ||
257 : | if (type==XVID_TYPE_PVOP) | ||
258 : | return 'P'; | ||
259 : | if (type==XVID_TYPE_BVOP) | ||
260 : | return 'B'; | ||
261 : | return 'S'; | ||
262 : | } | ||
263 : | |||
264 : | int vfw_debug(void *handle, | ||
265 : | int opt, | ||
266 : | void *param1, | ||
267 : | void *param2) | ||
268 : | { | ||
269 : | switch (opt) { | ||
270 : | case XVID_PLG_INFO: | ||
271 : | case XVID_PLG_CREATE: | ||
272 : | case XVID_PLG_DESTROY: | ||
273 : | case XVID_PLG_BEFORE: | ||
274 : | return 0; | ||
275 : | |||
276 : | case XVID_PLG_AFTER: | ||
277 : | { | ||
278 : | xvid_plg_data_t *data = (xvid_plg_data_t *) param1; | ||
279 : | |||
280 : | DPRINTF("[%5i] type=%c Q:%2i length:%6i", | ||
281 : | data->frame_num, | ||
282 : | type2char(data->type), | ||
283 : | data->quant, | ||
284 : | data->length); | ||
285 : | return 0; | ||
286 : | } | ||
287 : | } | ||
288 : | |||
289 : | return XVID_ERR_FAIL; | ||
290 : | } | ||
291 : | |||
292 : | |||
293 : | |||
294 : | suxen_drol | 889 | LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
295 : | { | ||
296 : | xvid_gbl_init_t init; | ||
297 : | xvid_enc_create_t create; | ||
298 : | suxen_drol | 983 | xvid_enc_plugin_t plugins[3]; |
299 : | suxen_drol | 1017 | xvid_plugin_single_t single; |
300 : | suxen_drol | 983 | xvid_plugin_2pass1_t pass1; |
301 : | xvid_plugin_2pass2_t pass2; | ||
302 : | suxen_drol | 1017 | int i; |
303 : | suxen_drol | 889 | |
304 : | suxen_drol | 983 | /* destroy previously created codec */ |
305 : | if(codec->ehandle) { | ||
306 : | xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); | ||
307 : | codec->ehandle = NULL; | ||
308 : | } | ||
309 : | |||
310 : | memset(&init, 0, sizeof(init)); | ||
311 : | init.version = XVID_VERSION; | ||
312 : | init.cpu_flags = codec->config.cpu; | ||
313 : | suxen_drol | 1032 | init.debug = codec->config.debug; |
314 : | suxen_drol | 983 | xvid_global(0, XVID_GBL_INIT, &init, NULL); |
315 : | |||
316 : | memset(&create, 0, sizeof(create)); | ||
317 : | create.version = XVID_VERSION; | ||
318 : | suxen_drol | 1017 | |
319 : | // zones | ||
320 : | create.zones = malloc(sizeof(xvid_enc_zone_t) * codec->config.num_zones); | ||
321 : | create.num_zones = codec->config.num_zones; | ||
322 : | for (i=0; i < create.num_zones; i++) { | ||
323 : | create.zones[i].frame = codec->config.zones[i].frame; | ||
324 : | suxen_drol | 1032 | if (codec->config.zones[i].mode == RC_ZONE_QUANT) { |
325 : | suxen_drol | 1017 | create.zones[i].mode = XVID_ZONE_QUANT; |
326 : | create.zones[i].increment = codec->config.zones[i].quant; | ||
327 : | }else{ | ||
328 : | create.zones[i].mode = XVID_ZONE_WEIGHT; | ||
329 : | create.zones[i].increment = codec->config.zones[i].weight; | ||
330 : | } | ||
331 : | create.zones[i].base = 100; | ||
332 : | } | ||
333 : | |||
334 : | // plugins | ||
335 : | suxen_drol | 983 | create.plugins = plugins; |
336 : | suxen_drol | 889 | switch (codec->config.mode) |
337 : | { | ||
338 : | suxen_drol | 1017 | case RC_MODE_1PASS : |
339 : | memset(&single, 0, sizeof(single)); | ||
340 : | single.version = XVID_VERSION; | ||
341 : | suxen_drol | 1032 | single.bitrate = codec->config.bitrate * CONFIG_KBPS; |
342 : | suxen_drol | 1017 | single.reaction_delay_factor = codec->config.rc_reaction_delay_factor; |
343 : | single.averaging_period = codec->config.rc_averaging_period; | ||
344 : | single.buffer = codec->config.rc_buffer; | ||
345 : | plugins[create.num_plugins].func = xvid_plugin_single; | ||
346 : | plugins[create.num_plugins].param = &single; | ||
347 : | suxen_drol | 983 | create.num_plugins++; |
348 : | suxen_drol | 1032 | break; |
349 : | suxen_drol | 889 | |
350 : | suxen_drol | 983 | case RC_MODE_2PASS1 : |
351 : | memset(&pass1, 0, sizeof(pass1)); | ||
352 : | pass1.version = XVID_VERSION; | ||
353 : | suxen_drol | 1028 | pass1.filename = codec->config.stats; |
354 : | suxen_drol | 983 | |
355 : | plugins[create.num_plugins].func = xvid_plugin_2pass1; | ||
356 : | plugins[create.num_plugins].param = &pass1; | ||
357 : | create.num_plugins++; | ||
358 : | suxen_drol | 889 | break; |
359 : | |||
360 : | suxen_drol | 1017 | case RC_MODE_2PASS2 : |
361 : | suxen_drol | 983 | memset(&pass2, 0, sizeof(pass2)); |
362 : | pass2.version = XVID_VERSION; | ||
363 : | suxen_drol | 1032 | pass2.bitrate = codec->config.bitrate * CONFIG_KBPS; |
364 : | suxen_drol | 983 | pass2.filename = codec->config.stats; |
365 : | suxen_drol | 1017 | |
366 : | suxen_drol | 983 | plugins[create.num_plugins].func = xvid_plugin_2pass2; |
367 : | plugins[create.num_plugins].param = &pass2; | ||
368 : | create.num_plugins++; | ||
369 : | suxen_drol | 889 | break; |
370 : | |||
371 : | suxen_drol | 983 | case RC_MODE_NULL : |
372 : | suxen_drol | 889 | return ICERR_OK; |
373 : | |||
374 : | default : | ||
375 : | break; | ||
376 : | } | ||
377 : | |||
378 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { |
379 : | suxen_drol | 983 | plugins[create.num_plugins].func = xvid_plugin_lumimasking; |
380 : | plugins[create.num_plugins].param = NULL; | ||
381 : | create.num_plugins++; | ||
382 : | suxen_drol | 889 | } |
383 : | |||
384 : | suxen_drol | 983 | plugins[create.num_plugins].func = vfw_debug; |
385 : | plugins[create.num_plugins].param = NULL; | ||
386 : | create.num_plugins++; | ||
387 : | suxen_drol | 889 | |
388 : | suxen_drol | 1017 | create.profile = profiles[codec->config.profile].id; |
389 : | |||
390 : | suxen_drol | 889 | create.width = lpbiInput->bmiHeader.biWidth; |
391 : | create.height = lpbiInput->bmiHeader.biHeight; | ||
392 : | create.fincr = codec->fincr; | ||
393 : | create.fbase = codec->fbase; | ||
394 : | |||
395 : | create.max_key_interval = codec->config.max_key_interval; | ||
396 : | |||
397 : | suxen_drol | 1017 | create.min_quant[0] = codec->config.min_iquant; |
398 : | create.max_quant[0] = codec->config.max_iquant; | ||
399 : | create.min_quant[1] = codec->config.min_pquant; | ||
400 : | create.max_quant[1] = codec->config.max_pquant; | ||
401 : | create.min_quant[2] = codec->config.min_bquant; | ||
402 : | create.max_quant[2] = codec->config.max_bquant; | ||
403 : | |||
404 : | if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) { | ||
405 : | suxen_drol | 983 | create.max_bframes = codec->config.max_bframes; |
406 : | suxen_drol | 1017 | create.bquant_ratio = codec->config.bquant_ratio; |
407 : | create.bquant_offset = codec->config.bquant_offset; | ||
408 : | |||
409 : | if (codec->config.packed) | ||
410 : | create.global |= XVID_GLOBAL_PACKED; | ||
411 : | |||
412 : | if (codec->config.closed_gov) | ||
413 : | create.global |= XVID_GLOBAL_CLOSED_GOP; | ||
414 : | |||
415 : | } | ||
416 : | |||
417 : | suxen_drol | 889 | create.frame_drop_ratio = codec->config.frame_drop_ratio; |
418 : | |||
419 : | suxen_drol | 1017 | create.num_threads = codec->config.num_threads; |
420 : | suxen_drol | 889 | |
421 : | suxen_drol | 983 | switch(xvid_encore(0, XVID_ENC_CREATE, &create, NULL)) |
422 : | suxen_drol | 889 | { |
423 : | case XVID_ERR_FAIL : | ||
424 : | return ICERR_ERROR; | ||
425 : | |||
426 : | case XVID_ERR_MEMORY : | ||
427 : | return ICERR_MEMORY; | ||
428 : | |||
429 : | case XVID_ERR_FORMAT : | ||
430 : | return ICERR_BADFORMAT; | ||
431 : | |||
432 : | case XVID_ERR_VERSION : | ||
433 : | return ICERR_UNSUPPORTED; | ||
434 : | } | ||
435 : | |||
436 : | codec->ehandle = create.handle; | ||
437 : | codec->framenum = 0; | ||
438 : | codec->keyspacing = 0; | ||
439 : | |||
440 : | return ICERR_OK; | ||
441 : | } | ||
442 : | |||
443 : | |||
444 : | LRESULT compress_end(CODEC * codec) | ||
445 : | { | ||
446 : | if (codec->ehandle != NULL) | ||
447 : | { | ||
448 : | xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); | ||
449 : | codec->ehandle = NULL; | ||
450 : | } | ||
451 : | |||
452 : | return ICERR_OK; | ||
453 : | } | ||
454 : | |||
455 : | suxen_drol | 1017 | |
456 : | static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum) | ||
457 : | { | ||
458 : | int i; | ||
459 : | |||
460 : | for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ; | ||
461 : | i--; | ||
462 : | |||
463 : | if (config->zones[i].greyscale) { | ||
464 : | frame->vop_flags |= XVID_VOP_GREYSCALE; | ||
465 : | } | ||
466 : | |||
467 : | if (config->zones[i].chroma_opt) { | ||
468 : | frame->vop_flags |= XVID_VOP_CHROMAOPT; | ||
469 : | } | ||
470 : | |||
471 : | if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) { | ||
472 : | frame->bframe_threshold = config->zones[i].bvop_threshold; | ||
473 : | } | ||
474 : | } | ||
475 : | |||
476 : | |||
477 : | suxen_drol | 889 | LRESULT compress(CODEC * codec, ICCOMPRESS * icc) |
478 : | { | ||
479 : | BITMAPINFOHEADER * inhdr = icc->lpbiInput; | ||
480 : | BITMAPINFOHEADER * outhdr = icc->lpbiOutput; | ||
481 : | xvid_enc_frame_t frame; | ||
482 : | xvid_enc_stats_t stats; | ||
483 : | int length; | ||
484 : | |||
485 : | memset(&frame, 0, sizeof(frame)); | ||
486 : | frame.version = XVID_VERSION; | ||
487 : | |||
488 : | frame.type = XVID_TYPE_AUTO; | ||
489 : | |||
490 : | /* vol stuff */ | ||
491 : | |||
492 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) && |
493 : | codec->config.quant_type != QUANT_MODE_H263) | ||
494 : | suxen_drol | 889 | { |
495 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_MPEGQUANT; |
496 : | suxen_drol | 889 | |
497 : | suxen_drol | 1017 | if (codec->config.quant_type == QUANT_MODE_CUSTOM) { |
498 : | suxen_drol | 889 | frame.quant_intra_matrix = codec->config.qmatrix_intra; |
499 : | frame.quant_inter_matrix = codec->config.qmatrix_inter; | ||
500 : | suxen_drol | 1017 | }else{ |
501 : | suxen_drol | 889 | frame.quant_intra_matrix = NULL; |
502 : | frame.quant_inter_matrix = NULL; | ||
503 : | } | ||
504 : | } | ||
505 : | |||
506 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) && |
507 : | codec->config.reduced_resolution) { | ||
508 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_REDUCED_ENABLE; |
509 : | frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */ | ||
510 : | suxen_drol | 889 | } |
511 : | |||
512 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) { |
513 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_QUARTERPEL; |
514 : | frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; | ||
515 : | suxen_drol | 889 | } |
516 : | |||
517 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) |
518 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_GMC; |
519 : | suxen_drol | 889 | |
520 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) |
521 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_INTERLACING; |
522 : | suxen_drol | 889 | |
523 : | /* vop stuff */ | ||
524 : | |||
525 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_HALFPEL; |
526 : | frame.vop_flags |= XVID_VOP_HQACPRED; | ||
527 : | suxen_drol | 889 | |
528 : | suxen_drol | 1032 | if (codec->config.vop_debug) |
529 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_DEBUG; |
530 : | suxen_drol | 889 | |
531 : | suxen_drol | 1017 | if (codec->config.trellis_quant) { |
532 : | frame.vop_flags |= XVID_VOP_TRELLISQUANT; | ||
533 : | } | ||
534 : | |||
535 : | if (codec->config.motion_search > 4) | ||
536 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_INTER4V; |
537 : | suxen_drol | 889 | |
538 : | if (codec->config.chromame) | ||
539 : | edgomez | 949 | frame.vop_flags |= XVID_ME_CHROMA16 + XVID_ME_CHROMA8; |
540 : | suxen_drol | 889 | |
541 : | frame.motion |= pmvfast_presets[codec->config.motion_search]; | ||
542 : | |||
543 : | switch (codec->config.vhq_mode) | ||
544 : | { | ||
545 : | case VHQ_MODE_DECISION : | ||
546 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
547 : | suxen_drol | 889 | break; |
548 : | |||
549 : | case VHQ_LIMITED_SEARCH : | ||
550 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
551 : | frame.motion |= XVID_ME_HALFPELREFINE16_BITS; | ||
552 : | frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; | ||
553 : | suxen_drol | 889 | break; |
554 : | |||
555 : | case VHQ_MEDIUM_SEARCH : | ||
556 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
557 : | frame.motion |= XVID_ME_HALFPELREFINE16_BITS; | ||
558 : | frame.motion |= XVID_ME_HALFPELREFINE8_BITS; | ||
559 : | frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; | ||
560 : | frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS; | ||
561 : | frame.motion |= XVID_ME_CHECKPREDICTION_BITS; | ||
562 : | suxen_drol | 889 | break; |
563 : | |||
564 : | case VHQ_WIDE_SEARCH : | ||
565 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
566 : | frame.motion |= XVID_ME_HALFPELREFINE16_BITS; | ||
567 : | frame.motion |= XVID_ME_HALFPELREFINE8_BITS; | ||
568 : | frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; | ||
569 : | frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS; | ||
570 : | frame.motion |= XVID_ME_CHECKPREDICTION_BITS; | ||
571 : | frame.motion |= XVID_ME_EXTSEARCH_BITS; | ||
572 : | suxen_drol | 889 | break; |
573 : | |||
574 : | default : | ||
575 : | break; | ||
576 : | } | ||
577 : | |||
578 : | frame.input.plane[0] = icc->lpInput; | ||
579 : | frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3; | ||
580 : | |||
581 : | if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL) | ||
582 : | return ICERR_BADFORMAT; | ||
583 : | |||
584 : | if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12) | ||
585 : | frame.input.stride[0] = (frame.input.stride[0]*2)/3; | ||
586 : | |||
587 : | frame.bitstream = icc->lpOutput; | ||
588 : | frame.length = icc->lpbiOutput->biSizeImage; | ||
589 : | |||
590 : | suxen_drol | 1017 | frame.quant = 0; |
591 : | suxen_drol | 889 | |
592 : | suxen_drol | 1017 | if (codec->config.mode == RC_MODE_NULL) { |
593 : | suxen_drol | 889 | outhdr->biSizeImage = 0; |
594 : | *icc->lpdwFlags = AVIIF_KEYFRAME; | ||
595 : | return ICERR_OK; | ||
596 : | suxen_drol | 1017 | } |
597 : | suxen_drol | 889 | |
598 : | // force keyframe spacing in 2-pass 1st pass | ||
599 : | if (codec->config.motion_search == 0) | ||
600 : | { | ||
601 : | frame.type = XVID_TYPE_IVOP; | ||
602 : | } | ||
603 : | else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) | ||
604 : | { | ||
605 : | suxen_drol | 983 | DPRINTF("current frame forced to p-frame"); |
606 : | suxen_drol | 889 | frame.type = XVID_TYPE_PVOP; |
607 : | } | ||
608 : | |||
609 : | suxen_drol | 1017 | /* frame-based stuff */ |
610 : | apply_zone_modifiers(&frame, &codec->config, codec->framenum); | ||
611 : | |||
612 : | /* call encore */ | ||
613 : | |||
614 : | suxen_drol | 889 | memset(&stats, 0, sizeof(stats)); |
615 : | stats.version = XVID_VERSION; | ||
616 : | |||
617 : | length = xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats); | ||
618 : | switch (length) | ||
619 : | { | ||
620 : | case XVID_ERR_FAIL : | ||
621 : | return ICERR_ERROR; | ||
622 : | |||
623 : | case XVID_ERR_MEMORY : | ||
624 : | return ICERR_MEMORY; | ||
625 : | |||
626 : | case XVID_ERR_FORMAT : | ||
627 : | return ICERR_BADFORMAT; | ||
628 : | |||
629 : | case XVID_ERR_VERSION : | ||
630 : | return ICERR_UNSUPPORTED; | ||
631 : | } | ||
632 : | |||
633 : | suxen_drol | 983 | DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); |
634 : | suxen_drol | 889 | |
635 : | if (length == 0) /* no encoder output */ | ||
636 : | { | ||
637 : | *icc->lpdwFlags = 0; | ||
638 : | ((char*)icc->lpOutput)[0] = 0x7f; /* virtual dub skip frame */ | ||
639 : | outhdr->biSizeImage = 1; | ||
640 : | |||
641 : | }else{ | ||
642 : | if (frame.out_flags & XVID_KEYFRAME) | ||
643 : | { | ||
644 : | codec->keyspacing = 0; | ||
645 : | *icc->lpdwFlags = AVIIF_KEYFRAME; | ||
646 : | } | ||
647 : | else | ||
648 : | { | ||
649 : | *icc->lpdwFlags = 0; | ||
650 : | } | ||
651 : | |||
652 : | outhdr->biSizeImage = length; | ||
653 : | |||
654 : | suxen_drol | 983 | if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass) |
655 : | suxen_drol | 889 | { |
656 : | outhdr->biSizeImage = 0; | ||
657 : | } | ||
658 : | } | ||
659 : | |||
660 : | suxen_drol | 1017 | codec->framenum++; |
661 : | codec->keyspacing++; | ||
662 : | suxen_drol | 889 | |
663 : | return ICERR_OK; | ||
664 : | } | ||
665 : | |||
666 : | |||
667 : | /* decompressor */ | ||
668 : | |||
669 : | |||
670 : | LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) | ||
671 : | { | ||
672 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
673 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
674 : | |||
675 : | if (lpbiInput == NULL) | ||
676 : | { | ||
677 : | return ICERR_ERROR; | ||
678 : | } | ||
679 : | |||
680 : | if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL) | ||
681 : | { | ||
682 : | return ICERR_BADFORMAT; | ||
683 : | } | ||
684 : | |||
685 : | if (lpbiOutput == NULL) | ||
686 : | { | ||
687 : | return ICERR_OK; | ||
688 : | } | ||
689 : | |||
690 : | if (inhdr->biWidth != outhdr->biWidth || | ||
691 : | inhdr->biHeight != outhdr->biHeight || | ||
692 : | get_colorspace(outhdr) == XVID_CSP_NULL) | ||
693 : | { | ||
694 : | return ICERR_BADFORMAT; | ||
695 : | } | ||
696 : | |||
697 : | return ICERR_OK; | ||
698 : | } | ||
699 : | |||
700 : | |||
701 : | LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
702 : | { | ||
703 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
704 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
705 : | LRESULT result; | ||
706 : | |||
707 : | if (lpbiOutput == NULL) | ||
708 : | { | ||
709 : | return sizeof(BITMAPINFOHEADER); | ||
710 : | } | ||
711 : | |||
712 : | /* --- yv12 --- */ | ||
713 : | |||
714 : | if (get_colorspace(inhdr) != XVID_CSP_NULL) { | ||
715 : | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); | ||
716 : | // XXX: should we set outhdr->biSize ?? | ||
717 : | return ICERR_OK; | ||
718 : | } | ||
719 : | /* --- yv12 --- */ | ||
720 : | |||
721 : | result = decompress_query(codec, lpbiInput, lpbiOutput); | ||
722 : | if (result != ICERR_OK) | ||
723 : | { | ||
724 : | return result; | ||
725 : | } | ||
726 : | |||
727 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
728 : | outhdr->biWidth = inhdr->biWidth; | ||
729 : | outhdr->biHeight = inhdr->biHeight; | ||
730 : | outhdr->biPlanes = 1; | ||
731 : | outhdr->biBitCount = 24; | ||
732 : | outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */ | ||
733 : | outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount; | ||
734 : | outhdr->biXPelsPerMeter = 0; | ||
735 : | outhdr->biYPelsPerMeter = 0; | ||
736 : | outhdr->biClrUsed = 0; | ||
737 : | outhdr->biClrImportant = 0; | ||
738 : | |||
739 : | return ICERR_OK; | ||
740 : | } | ||
741 : | |||
742 : | |||
743 : | LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
744 : | { | ||
745 : | xvid_gbl_init_t init; | ||
746 : | xvid_dec_create_t create; | ||
747 : | |||
748 : | memset(&init, 0, sizeof(init)); | ||
749 : | init.version = XVID_VERSION; | ||
750 : | init.cpu_flags = codec->config.cpu; | ||
751 : | xvid_global(0, XVID_GBL_INIT, &init, NULL); | ||
752 : | |||
753 : | memset(&create, 0, sizeof(create)); | ||
754 : | create.version = XVID_VERSION; | ||
755 : | create.width = lpbiInput->bmiHeader.biWidth; | ||
756 : | create.height = lpbiInput->bmiHeader.biHeight; | ||
757 : | |||
758 : | switch(xvid_decore(0, XVID_DEC_CREATE, &create, NULL)) | ||
759 : | { | ||
760 : | case XVID_ERR_FAIL : | ||
761 : | return ICERR_ERROR; | ||
762 : | |||
763 : | case XVID_ERR_MEMORY : | ||
764 : | return ICERR_MEMORY; | ||
765 : | |||
766 : | case XVID_ERR_FORMAT : | ||
767 : | return ICERR_BADFORMAT; | ||
768 : | |||
769 : | case XVID_ERR_VERSION : | ||
770 : | return ICERR_UNSUPPORTED; | ||
771 : | } | ||
772 : | |||
773 : | codec->dhandle = create.handle; | ||
774 : | |||
775 : | return ICERR_OK; | ||
776 : | } | ||
777 : | |||
778 : | |||
779 : | LRESULT decompress_end(CODEC * codec) | ||
780 : | { | ||
781 : | if (codec->dhandle != NULL) | ||
782 : | { | ||
783 : | xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); | ||
784 : | codec->dhandle = NULL; | ||
785 : | } | ||
786 : | return ICERR_OK; | ||
787 : | } | ||
788 : | |||
789 : | |||
790 : | LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd) | ||
791 : | { | ||
792 : | xvid_dec_frame_t frame; | ||
793 : | |||
794 : | /* --- yv12 --- */ | ||
795 : | if (icd->lpbiInput->biCompression != FOURCC_XVID && | ||
796 : | icd->lpbiInput->biCompression != FOURCC_DIVX && | ||
797 : | icd->lpbiInput->biCompression != FOURCC_DX50) | ||
798 : | { | ||
799 : | xvid_gbl_convert_t convert; | ||
800 : | |||
801 : | suxen_drol | 983 | DPRINTF("input=%c%c%c%c output=%c%c%c%c", |
802 : | icd->lpbiInput->biCompression&0xff, | ||
803 : | (icd->lpbiInput->biCompression>>8)&0xff, | ||
804 : | (icd->lpbiInput->biCompression>>16)&0xff, | ||
805 : | (icd->lpbiInput->biCompression>>24)&0xff, | ||
806 : | icd->lpbiOutput->biCompression&0xff, | ||
807 : | (icd->lpbiOutput->biCompression>>8)&0xff, | ||
808 : | (icd->lpbiOutput->biCompression>>16)&0xff, | ||
809 : | (icd->lpbiOutput->biCompression>>24)&0xff); | ||
810 : | suxen_drol | 889 | |
811 : | memset(&convert, 0, sizeof(convert)); | ||
812 : | convert.version = XVID_VERSION; | ||
813 : | |||
814 : | convert.input.csp = get_colorspace(icd->lpbiInput); | ||
815 : | convert.input.plane[0] = icd->lpInput; | ||
816 : | convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3; | ||
817 : | if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12) | ||
818 : | convert.input.stride[0] = (convert.input.stride[0]*2)/3; | ||
819 : | |||
820 : | convert.output.csp = get_colorspace(icd->lpbiOutput); | ||
821 : | convert.output.plane[0] = icd->lpOutput; | ||
822 : | convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; | ||
823 : | if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12) | ||
824 : | convert.output.stride[0] = (convert.output.stride[0]*2)/3; | ||
825 : | |||
826 : | convert.width = icd->lpbiInput->biWidth; | ||
827 : | convert.height = icd->lpbiInput->biHeight; | ||
828 : | convert.interlacing = 0; | ||
829 : | if (convert.input.csp == XVID_CSP_NULL || | ||
830 : | convert.output.csp == XVID_CSP_NULL || | ||
831 : | xvid_global(0, XVID_GBL_CONVERT, &convert, NULL) < 0) | ||
832 : | { | ||
833 : | return ICERR_BADFORMAT; | ||
834 : | } | ||
835 : | return ICERR_OK; | ||
836 : | } | ||
837 : | /* --- yv12 --- */ | ||
838 : | |||
839 : | memset(&frame, 0, sizeof(frame)); | ||
840 : | frame.version = XVID_VERSION; | ||
841 : | frame.general = XVID_LOWDELAY; /* force low_delay_default mode */ | ||
842 : | frame.bitstream = icd->lpInput; | ||
843 : | frame.length = icd->lpbiInput->biSizeImage; | ||
844 : | |||
845 : | if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL))) | ||
846 : | { | ||
847 : | if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL) | ||
848 : | { | ||
849 : | return ICERR_BADFORMAT; | ||
850 : | } | ||
851 : | frame.output.plane[0] = icd->lpOutput; | ||
852 : | frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; | ||
853 : | if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12) | ||
854 : | frame.output.stride[0] = (frame.output.stride[0]*2)/3; | ||
855 : | } | ||
856 : | else | ||
857 : | { | ||
858 : | frame.output.csp = XVID_CSP_NULL; | ||
859 : | } | ||
860 : | |||
861 : | switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) | ||
862 : | { | ||
863 : | case XVID_ERR_FAIL : | ||
864 : | return ICERR_ERROR; | ||
865 : | |||
866 : | case XVID_ERR_MEMORY : | ||
867 : | return ICERR_MEMORY; | ||
868 : | |||
869 : | case XVID_ERR_FORMAT : | ||
870 : | return ICERR_BADFORMAT; | ||
871 : | |||
872 : | case XVID_ERR_VERSION : | ||
873 : | return ICERR_UNSUPPORTED; | ||
874 : | } | ||
875 : | |||
876 : | return ICERR_OK; | ||
877 : | } | ||
878 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |