Parent Directory | Revision Log
Revision 1358 - (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 : | edgomez | 1203 | #include <stdio.h> |
53 : | suxen_drol | 1017 | #include "vfwext.h" |
54 : | suxen_drol | 889 | |
55 : | suxen_drol | 983 | #include <xvid.h> |
56 : | #include "debug.h" | ||
57 : | suxen_drol | 889 | #include "codec.h" |
58 : | suxen_drol | 1061 | #include "status.h" |
59 : | suxen_drol | 889 | |
60 : | suxen_drol | 1358 | HINSTANCE m_hdll; |
61 : | int (*xvid_global_func)(void *handle, int opt, void *param1, void *param2); | ||
62 : | int (*xvid_encore_func)(void *handle, int opt, void *param1, void *param2); | ||
63 : | int (*xvid_decore_func)(void *handle, int opt, void *param1, void *param2); | ||
64 : | suxen_drol | 983 | |
65 : | suxen_drol | 1358 | xvid_plugin_func *xvid_plugin_single_func, |
66 : | *xvid_plugin_2pass1_func, | ||
67 : | *xvid_plugin_2pass2_func, | ||
68 : | *xvid_plugin_lumimasking_func, | ||
69 : | *xvid_plugin_psnr_func; | ||
70 : | |||
71 : | |||
72 : | |||
73 : | suxen_drol | 983 | static const int pmvfast_presets[7] = { |
74 : | suxen_drol | 889 | 0, 0, 0, 0, |
75 : | edgomez | 949 | 0 | XVID_ME_HALFPELREFINE16 | 0, |
76 : | 0 | XVID_ME_HALFPELREFINE16 | 0 | | ||
77 : | XVID_ME_ADVANCEDDIAMOND16, XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | | ||
78 : | XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 | ||
79 : | suxen_drol | 889 | }; |
80 : | |||
81 : | suxen_drol | 983 | |
82 : | |||
83 : | suxen_drol | 889 | /* return xvid compatbile colorspace, |
84 : | or XVID_CSP_NULL if failure | ||
85 : | */ | ||
86 : | |||
87 : | suxen_drol | 1333 | static int get_colorspace(BITMAPINFOHEADER * hdr) |
88 : | suxen_drol | 889 | { |
89 : | /* rgb only: negative height specifies top down image */ | ||
90 : | int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP); | ||
91 : | |||
92 : | switch(hdr->biCompression) | ||
93 : | { | ||
94 : | case BI_RGB : | ||
95 : | if (hdr->biBitCount == 16) | ||
96 : | { | ||
97 : | suxen_drol | 983 | DPRINTF("RGB16 (RGB555)"); |
98 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_RGB555; |
99 : | } | ||
100 : | if (hdr->biBitCount == 24) | ||
101 : | { | ||
102 : | suxen_drol | 983 | DPRINTF("RGB24"); |
103 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_BGR; |
104 : | } | ||
105 : | if (hdr->biBitCount == 32) | ||
106 : | { | ||
107 : | suxen_drol | 983 | DPRINTF("RGB32"); |
108 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_BGRA; |
109 : | } | ||
110 : | |||
111 : | suxen_drol | 983 | DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount); |
112 : | suxen_drol | 889 | return XVID_CSP_NULL; |
113 : | |||
114 : | case BI_BITFIELDS : | ||
115 : | if (hdr->biSize >= sizeof(BITMAPV4HEADER)) | ||
116 : | { | ||
117 : | BITMAPV4HEADER * hdr4 = (BITMAPV4HEADER *)hdr; | ||
118 : | |||
119 : | if (hdr4->bV4BitCount == 16 && | ||
120 : | hdr4->bV4RedMask == 0x7c00 && | ||
121 : | hdr4->bV4GreenMask == 0x3e0 && | ||
122 : | hdr4->bV4BlueMask == 0x1f) | ||
123 : | { | ||
124 : | suxen_drol | 983 | DPRINTF("RGB555"); |
125 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_RGB555; |
126 : | } | ||
127 : | |||
128 : | if(hdr4->bV4BitCount == 16 && | ||
129 : | hdr4->bV4RedMask == 0xf800 && | ||
130 : | hdr4->bV4GreenMask == 0x7e0 && | ||
131 : | hdr4->bV4BlueMask == 0x1f) | ||
132 : | { | ||
133 : | suxen_drol | 983 | DPRINTF("RGB565"); |
134 : | suxen_drol | 889 | return rgb_flip | XVID_CSP_RGB565; |
135 : | } | ||
136 : | |||
137 : | suxen_drol | 983 | DPRINTF("unsupported BI_BITFIELDS mode"); |
138 : | suxen_drol | 889 | return XVID_CSP_NULL; |
139 : | } | ||
140 : | |||
141 : | suxen_drol | 983 | DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
142 : | suxen_drol | 889 | return XVID_CSP_NULL; |
143 : | |||
144 : | case FOURCC_I420 : | ||
145 : | case FOURCC_IYUV : | ||
146 : | suxen_drol | 983 | DPRINTF("IYUY"); |
147 : | suxen_drol | 889 | return XVID_CSP_I420; |
148 : | |||
149 : | case FOURCC_YV12 : | ||
150 : | suxen_drol | 983 | DPRINTF("YV12"); |
151 : | suxen_drol | 889 | return XVID_CSP_YV12; |
152 : | |||
153 : | case FOURCC_YUYV : | ||
154 : | case FOURCC_YUY2 : | ||
155 : | suxen_drol | 983 | DPRINTF("YUY2"); |
156 : | suxen_drol | 889 | return XVID_CSP_YUY2; |
157 : | |||
158 : | case FOURCC_YVYU : | ||
159 : | suxen_drol | 983 | DPRINTF("YVYU"); |
160 : | suxen_drol | 889 | return XVID_CSP_YVYU; |
161 : | |||
162 : | case FOURCC_UYVY : | ||
163 : | suxen_drol | 983 | DPRINTF("UYVY"); |
164 : | suxen_drol | 889 | return XVID_CSP_UYVY; |
165 : | |||
166 : | default : | ||
167 : | suxen_drol | 983 | DPRINTF("unsupported colorspace %c%c%c%c", |
168 : | syskin | 1311 | hdr->biCompression&0xff, |
169 : | (hdr->biCompression>>8)&0xff, | ||
170 : | (hdr->biCompression>>16)&0xff, | ||
171 : | (hdr->biCompression>>24)&0xff); | ||
172 : | suxen_drol | 889 | return XVID_CSP_NULL; |
173 : | } | ||
174 : | } | ||
175 : | |||
176 : | |||
177 : | /* compressor */ | ||
178 : | |||
179 : | |||
180 : | /* test the output format */ | ||
181 : | |||
182 : | LRESULT compress_query(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
183 : | { | ||
184 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
185 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
186 : | |||
187 : | syskin | 1311 | /* VFWEXT detection */ |
188 : | if (inhdr->biCompression == VFWEXT_FOURCC) { | ||
189 : | return (ICM_USER+0x0fff); | ||
190 : | } | ||
191 : | suxen_drol | 1017 | |
192 : | suxen_drol | 889 | if (get_colorspace(inhdr) == XVID_CSP_NULL) |
193 : | { | ||
194 : | return ICERR_BADFORMAT; | ||
195 : | } | ||
196 : | |||
197 : | if (lpbiOutput == NULL) | ||
198 : | { | ||
199 : | return ICERR_OK; | ||
200 : | } | ||
201 : | |||
202 : | if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight || | ||
203 : | (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX && outhdr->biCompression != FOURCC_DX50)) | ||
204 : | { | ||
205 : | return ICERR_BADFORMAT; | ||
206 : | } | ||
207 : | |||
208 : | return ICERR_OK; | ||
209 : | } | ||
210 : | |||
211 : | |||
212 : | LRESULT compress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
213 : | { | ||
214 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
215 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
216 : | |||
217 : | if (get_colorspace(inhdr) == XVID_CSP_NULL) | ||
218 : | { | ||
219 : | return ICERR_BADFORMAT; | ||
220 : | } | ||
221 : | |||
222 : | if (lpbiOutput == NULL) | ||
223 : | { | ||
224 : | syskin | 1091 | return sizeof(BITMAPINFOHEADER); |
225 : | suxen_drol | 889 | } |
226 : | |||
227 : | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); | ||
228 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
229 : | outhdr->biSizeImage = compress_get_size(codec, lpbiInput, lpbiOutput); | ||
230 : | outhdr->biXPelsPerMeter = 0; | ||
231 : | outhdr->biYPelsPerMeter = 0; | ||
232 : | outhdr->biClrUsed = 0; | ||
233 : | outhdr->biClrImportant = 0; | ||
234 : | |||
235 : | if (codec->config.fourcc_used == 0) | ||
236 : | { | ||
237 : | outhdr->biCompression = FOURCC_XVID; | ||
238 : | } | ||
239 : | else if (codec->config.fourcc_used == 1) | ||
240 : | { | ||
241 : | outhdr->biCompression = FOURCC_DIVX; | ||
242 : | } | ||
243 : | else | ||
244 : | { | ||
245 : | outhdr->biCompression = FOURCC_DX50; | ||
246 : | } | ||
247 : | |||
248 : | return ICERR_OK; | ||
249 : | } | ||
250 : | |||
251 : | |||
252 : | LRESULT compress_get_size(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
253 : | { | ||
254 : | return 2 * lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3; | ||
255 : | } | ||
256 : | |||
257 : | |||
258 : | LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) | ||
259 : | { | ||
260 : | edgomez | 1053 | #if 0 |
261 : | syskin | 1311 | DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); |
262 : | edgomez | 1053 | #endif |
263 : | suxen_drol | 889 | codec->fincr = icf->dwScale; |
264 : | codec->fbase = icf->dwRate; | ||
265 : | return ICERR_OK; | ||
266 : | } | ||
267 : | |||
268 : | |||
269 : | suxen_drol | 1333 | static char type2char(int type) |
270 : | suxen_drol | 983 | { |
271 : | syskin | 1311 | if (type==XVID_TYPE_IVOP) |
272 : | return 'I'; | ||
273 : | if (type==XVID_TYPE_PVOP) | ||
274 : | return 'P'; | ||
275 : | if (type==XVID_TYPE_BVOP) | ||
276 : | return 'B'; | ||
277 : | return 'S'; | ||
278 : | suxen_drol | 983 | } |
279 : | |||
280 : | suxen_drol | 1333 | static int vfw_debug(void *handle, |
281 : | suxen_drol | 983 | int opt, |
282 : | void *param1, | ||
283 : | edgomez | 1326 | void *param2) |
284 : | suxen_drol | 983 | { |
285 : | switch (opt) { | ||
286 : | syskin | 1311 | case XVID_PLG_CREATE: |
287 : | edgomez | 1326 | *((void**)param2) = NULL; |
288 : | suxen_drol | 983 | case XVID_PLG_INFO: |
289 : | case XVID_PLG_DESTROY: | ||
290 : | case XVID_PLG_BEFORE: | ||
291 : | return 0; | ||
292 : | |||
293 : | case XVID_PLG_AFTER: | ||
294 : | { | ||
295 : | xvid_plg_data_t *data = (xvid_plg_data_t *) param1; | ||
296 : | |||
297 : | edgomez | 1203 | /* We don't use DPRINTF here because it's active only for _DEBUG |
298 : | * builds and that activates lot of other debug printfs. We only | ||
299 : | * want these all the time */ | ||
300 : | char buf[1024]; | ||
301 : | edgomez | 1267 | sprintf(buf, "[%6i] type=%c Q:%2i length:%6i", |
302 : | edgomez | 1203 | data->frame_num, |
303 : | type2char(data->type), | ||
304 : | data->quant, | ||
305 : | data->length); | ||
306 : | OutputDebugString(buf); | ||
307 : | |||
308 : | suxen_drol | 983 | return 0; |
309 : | } | ||
310 : | } | ||
311 : | |||
312 : | return XVID_ERR_FAIL; | ||
313 : | } | ||
314 : | |||
315 : | syskin | 1301 | #define XVID_DLL_NAME "xvidcore.dll" |
316 : | suxen_drol | 983 | |
317 : | syskin | 1301 | static int init_dll() |
318 : | { | ||
319 : | syskin | 1305 | if (m_hdll != NULL) return 0; |
320 : | suxen_drol | 983 | |
321 : | syskin | 1301 | DPRINTF("init_dll"); |
322 : | m_hdll = LoadLibrary(XVID_DLL_NAME); | ||
323 : | if (m_hdll == NULL) { | ||
324 : | DPRINTF("dll load failed"); | ||
325 : | syskin | 1329 | MessageBox(0, XVID_DLL_NAME " not found!","Error!", MB_ICONEXCLAMATION|MB_OK); |
326 : | syskin | 1301 | return XVID_ERR_FAIL; |
327 : | } | ||
328 : | |||
329 : | xvid_global_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global"); | ||
330 : | if (xvid_global_func == NULL) { | ||
331 : | MessageBox(0, "xvid_global() not found", "Error", 0); | ||
332 : | return XVID_ERR_FAIL; | ||
333 : | } | ||
334 : | |||
335 : | xvid_encore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_encore"); | ||
336 : | if (xvid_encore_func == NULL) { | ||
337 : | MessageBox(0, "xvid_encore() not found", "Error", 0); | ||
338 : | return XVID_ERR_FAIL; | ||
339 : | } | ||
340 : | |||
341 : | xvid_decore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_decore"); | ||
342 : | if (xvid_decore_func == NULL) { | ||
343 : | MessageBox(0, "xvid_decore() not found", "Error", 0); | ||
344 : | return XVID_ERR_FAIL; | ||
345 : | } | ||
346 : | |||
347 : | xvid_plugin_single_func = | ||
348 : | (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_single")); | ||
349 : | xvid_plugin_2pass1_func = | ||
350 : | (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass1")); | ||
351 : | xvid_plugin_2pass2_func = | ||
352 : | (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass2")); | ||
353 : | xvid_plugin_lumimasking_func = | ||
354 : | (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_lumimasking")); | ||
355 : | xvid_plugin_psnr_func = | ||
356 : | (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_psnr")); | ||
357 : | |||
358 : | return 0; | ||
359 : | } | ||
360 : | |||
361 : | syskin | 1346 | /* constant-quant zones for fixed quant encoding */ |
362 : | syskin | 1329 | static void |
363 : | prepare_cquant_zones(CONFIG * config) { | ||
364 : | |||
365 : | int i = 0; | ||
366 : | if (config->num_zones == 0 || config->zones[0].frame != 0) { | ||
367 : | /* first zone does not start at frame 0 or doesn't exist */ | ||
368 : | |||
369 : | if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */ | ||
370 : | |||
371 : | config->zones[config->num_zones].frame = 0; | ||
372 : | config->zones[config->num_zones].mode = RC_ZONE_QUANT; | ||
373 : | config->zones[config->num_zones].weight = 100; | ||
374 : | config->zones[config->num_zones].quant = config->desired_quant; | ||
375 : | config->zones[config->num_zones].type = XVID_TYPE_AUTO; | ||
376 : | config->zones[config->num_zones].greyscale = 0; | ||
377 : | config->zones[config->num_zones].chroma_opt = 0; | ||
378 : | config->zones[config->num_zones].bvop_threshold = 0; | ||
379 : | config->num_zones++; | ||
380 : | |||
381 : | sort_zones(config->zones, config->num_zones, &i); | ||
382 : | } | ||
383 : | |||
384 : | /* step 2: let's change all weight zones into quant zones */ | ||
385 : | |||
386 : | for(i = 0; i < config->num_zones; i++) | ||
387 : | if (config->zones[i].mode == RC_ZONE_WEIGHT) { | ||
388 : | config->zones[i].mode = RC_ZONE_QUANT; | ||
389 : | config->zones[i].quant = (100*config->desired_quant) / config->zones[i].weight; | ||
390 : | } | ||
391 : | } | ||
392 : | |||
393 : | syskin | 1346 | /* full first pass zones */ |
394 : | static void | ||
395 : | prepare_full1pass_zones(CONFIG * config) { | ||
396 : | |||
397 : | int i = 0; | ||
398 : | if (config->num_zones == 0 || config->zones[0].frame != 0) { | ||
399 : | /* first zone does not start at frame 0 or doesn't exist */ | ||
400 : | syskin | 1329 | |
401 : | syskin | 1346 | if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */ |
402 : | |||
403 : | config->zones[config->num_zones].frame = 0; | ||
404 : | config->zones[config->num_zones].mode = RC_ZONE_QUANT; | ||
405 : | config->zones[config->num_zones].weight = 100; | ||
406 : | config->zones[config->num_zones].quant = 200; | ||
407 : | config->zones[config->num_zones].type = XVID_TYPE_AUTO; | ||
408 : | config->zones[config->num_zones].greyscale = 0; | ||
409 : | config->zones[config->num_zones].chroma_opt = 0; | ||
410 : | config->zones[config->num_zones].bvop_threshold = 0; | ||
411 : | config->num_zones++; | ||
412 : | |||
413 : | sort_zones(config->zones, config->num_zones, &i); | ||
414 : | } | ||
415 : | |||
416 : | /* step 2: let's change all weight zones into quant zones */ | ||
417 : | |||
418 : | for(i = 0; i < config->num_zones; i++) | ||
419 : | if (config->zones[i].mode == RC_ZONE_WEIGHT) { | ||
420 : | config->zones[i].mode = RC_ZONE_QUANT; | ||
421 : | config->zones[i].quant = 200; | ||
422 : | } | ||
423 : | } | ||
424 : | |||
425 : | |||
426 : | suxen_drol | 889 | LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
427 : | { | ||
428 : | xvid_gbl_init_t init; | ||
429 : | xvid_enc_create_t create; | ||
430 : | syskin | 1311 | xvid_enc_plugin_t plugins[3]; |
431 : | suxen_drol | 1017 | xvid_plugin_single_t single; |
432 : | suxen_drol | 983 | xvid_plugin_2pass1_t pass1; |
433 : | xvid_plugin_2pass2_t pass2; | ||
434 : | syskin | 1311 | int i; |
435 : | syskin | 1329 | HANDLE hFile; |
436 : | suxen_drol | 889 | |
437 : | syskin | 1329 | CONFIG tmpCfg; /* if we want to alter config to suit our needs, it shouldn't be visible to user later */ |
438 : | memcpy(&tmpCfg, &codec->config, sizeof(CONFIG)); | ||
439 : | |||
440 : | syskin | 1301 | if (init_dll() != 0) return ICERR_ERROR; |
441 : | syskin | 1311 | /* destroy previously created codec */ |
442 : | suxen_drol | 983 | if(codec->ehandle) { |
443 : | syskin | 1301 | xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
444 : | suxen_drol | 983 | codec->ehandle = NULL; |
445 : | } | ||
446 : | |||
447 : | syskin | 1311 | memset(&init, 0, sizeof(init)); |
448 : | suxen_drol | 983 | init.version = XVID_VERSION; |
449 : | init.cpu_flags = codec->config.cpu; | ||
450 : | syskin | 1311 | init.debug = codec->config.debug; |
451 : | syskin | 1301 | xvid_global_func(0, XVID_GBL_INIT, &init, NULL); |
452 : | suxen_drol | 983 | |
453 : | memset(&create, 0, sizeof(create)); | ||
454 : | create.version = XVID_VERSION; | ||
455 : | suxen_drol | 1017 | |
456 : | syskin | 1311 | /* plugins */ |
457 : | suxen_drol | 983 | create.plugins = plugins; |
458 : | suxen_drol | 889 | switch (codec->config.mode) |
459 : | { | ||
460 : | suxen_drol | 1017 | case RC_MODE_1PASS : |
461 : | syskin | 1311 | memset(&single, 0, sizeof(single)); |
462 : | single.version = XVID_VERSION; | ||
463 : | single.bitrate = codec->config.bitrate * CONFIG_KBPS; | ||
464 : | single.reaction_delay_factor = codec->config.rc_reaction_delay_factor; | ||
465 : | suxen_drol | 1017 | single.averaging_period = codec->config.rc_averaging_period; |
466 : | single.buffer = codec->config.rc_buffer; | ||
467 : | syskin | 1311 | plugins[create.num_plugins].func = xvid_plugin_single_func; |
468 : | plugins[create.num_plugins].param = &single; | ||
469 : | create.num_plugins++; | ||
470 : | syskin | 1329 | if (!codec->config.use_2pass_bitrate) /* constant-quant mode */ |
471 : | prepare_cquant_zones(&tmpCfg); | ||
472 : | syskin | 1311 | break; |
473 : | suxen_drol | 889 | |
474 : | suxen_drol | 983 | case RC_MODE_2PASS1 : |
475 : | syskin | 1311 | memset(&pass1, 0, sizeof(pass1)); |
476 : | pass1.version = XVID_VERSION; | ||
477 : | pass1.filename = codec->config.stats; | ||
478 : | syskin | 1346 | if (codec->config.full1pass) |
479 : | prepare_full1pass_zones(&tmpCfg); | ||
480 : | syskin | 1311 | plugins[create.num_plugins].func = xvid_plugin_2pass1_func; |
481 : | plugins[create.num_plugins].param = &pass1; | ||
482 : | create.num_plugins++; | ||
483 : | suxen_drol | 889 | break; |
484 : | |||
485 : | suxen_drol | 1017 | case RC_MODE_2PASS2 : |
486 : | syskin | 1311 | memset(&pass2, 0, sizeof(pass2)); |
487 : | pass2.version = XVID_VERSION; | ||
488 : | if (codec->config.use_2pass_bitrate) { | ||
489 : | pass2.bitrate = codec->config.bitrate * CONFIG_KBPS; | ||
490 : | syskin | 1329 | } else { |
491 : | syskin | 1311 | pass2.bitrate = -codec->config.desired_size; /* kilobytes */ |
492 : | } | ||
493 : | suxen_drol | 983 | pass2.filename = codec->config.stats; |
494 : | suxen_drol | 1017 | |
495 : | syskin | 1329 | hFile = CreateFile(pass2.filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
496 : | if (hFile == INVALID_HANDLE_VALUE) | ||
497 : | { | ||
498 : | MessageBox(0, "Statsfile not found!","Error!", MB_ICONEXCLAMATION|MB_OK); | ||
499 : | return XVID_ERR_FAIL; | ||
500 : | } else | ||
501 : | { | ||
502 : | CloseHandle(hFile); | ||
503 : | } | ||
504 : | |||
505 : | syskin | 1311 | pass2.keyframe_boost = codec->config.keyframe_boost; /* keyframe boost percentage: [0..100...]; */ |
506 : | pass2.curve_compression_high = codec->config.curve_compression_high; | ||
507 : | pass2.curve_compression_low = codec->config.curve_compression_low; | ||
508 : | edgomez | 1202 | pass2.overflow_control_strength = codec->config.overflow_control_strength; |
509 : | syskin | 1311 | pass2.max_overflow_improvement = codec->config.twopass_max_overflow_improvement; |
510 : | pass2.max_overflow_degradation = codec->config.twopass_max_overflow_degradation; | ||
511 : | pass2.kfreduction = codec->config.kfreduction; | ||
512 : | pass2.kfthreshold = codec->config.kfthreshold; | ||
513 : | pass2.container_frame_overhead = 24; /* AVI */ | ||
514 : | suxen_drol | 1052 | |
515 : | syskin | 1311 | plugins[create.num_plugins].func = xvid_plugin_2pass2_func; |
516 : | plugins[create.num_plugins].param = &pass2; | ||
517 : | create.num_plugins++; | ||
518 : | suxen_drol | 889 | break; |
519 : | |||
520 : | suxen_drol | 983 | case RC_MODE_NULL : |
521 : | suxen_drol | 889 | return ICERR_OK; |
522 : | |||
523 : | default : | ||
524 : | break; | ||
525 : | } | ||
526 : | |||
527 : | syskin | 1329 | /* zones - copy from tmpCfg in case we automatically altered them above */ |
528 : | create.zones = malloc(sizeof(xvid_enc_zone_t) * tmpCfg.num_zones); | ||
529 : | create.num_zones = tmpCfg.num_zones; | ||
530 : | for (i=0; i < create.num_zones; i++) { | ||
531 : | create.zones[i].frame = tmpCfg.zones[i].frame; | ||
532 : | if (tmpCfg.zones[i].mode == RC_ZONE_QUANT) { | ||
533 : | create.zones[i].mode = XVID_ZONE_QUANT; | ||
534 : | create.zones[i].increment = tmpCfg.zones[i].quant; | ||
535 : | }else{ | ||
536 : | create.zones[i].mode = XVID_ZONE_WEIGHT; | ||
537 : | create.zones[i].increment = tmpCfg.zones[i].weight; | ||
538 : | } | ||
539 : | create.zones[i].base = 100; | ||
540 : | } | ||
541 : | |||
542 : | /* lumimasking plugin */ | ||
543 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { |
544 : | syskin | 1311 | plugins[create.num_plugins].func = xvid_plugin_lumimasking_func; |
545 : | plugins[create.num_plugins].param = NULL; | ||
546 : | create.num_plugins++; | ||
547 : | suxen_drol | 889 | } |
548 : | |||
549 : | syskin | 1311 | plugins[create.num_plugins].func = vfw_debug; |
550 : | plugins[create.num_plugins].param = NULL; | ||
551 : | create.num_plugins++; | ||
552 : | suxen_drol | 889 | |
553 : | syskin | 1311 | create.profile = profiles[codec->config.profile].id; |
554 : | suxen_drol | 1017 | |
555 : | suxen_drol | 889 | create.width = lpbiInput->bmiHeader.biWidth; |
556 : | create.height = lpbiInput->bmiHeader.biHeight; | ||
557 : | create.fincr = codec->fincr; | ||
558 : | create.fbase = codec->fbase; | ||
559 : | |||
560 : | syskin | 1311 | create.max_key_interval = codec->config.max_key_interval; |
561 : | suxen_drol | 889 | |
562 : | syskin | 1311 | create.min_quant[0] = codec->config.min_iquant; |
563 : | create.max_quant[0] = codec->config.max_iquant; | ||
564 : | create.min_quant[1] = codec->config.min_pquant; | ||
565 : | create.max_quant[1] = codec->config.max_pquant; | ||
566 : | create.min_quant[2] = codec->config.min_bquant; | ||
567 : | create.max_quant[2] = codec->config.max_bquant; | ||
568 : | suxen_drol | 1017 | |
569 : | syskin | 1311 | if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) { |
570 : | create.max_bframes = codec->config.max_bframes; | ||
571 : | create.bquant_ratio = codec->config.bquant_ratio; | ||
572 : | create.bquant_offset = codec->config.bquant_offset; | ||
573 : | suxen_drol | 1017 | |
574 : | syskin | 1311 | if (codec->config.packed) |
575 : | create.global |= XVID_GLOBAL_PACKED; | ||
576 : | suxen_drol | 1017 | |
577 : | syskin | 1311 | if (codec->config.closed_gov) |
578 : | create.global |= XVID_GLOBAL_CLOSED_GOP; | ||
579 : | suxen_drol | 1017 | |
580 : | syskin | 1311 | } |
581 : | suxen_drol | 1017 | |
582 : | suxen_drol | 889 | create.frame_drop_ratio = codec->config.frame_drop_ratio; |
583 : | |||
584 : | syskin | 1311 | create.num_threads = codec->config.num_threads; |
585 : | suxen_drol | 889 | |
586 : | syskin | 1301 | switch(xvid_encore_func(0, XVID_ENC_CREATE, &create, NULL)) |
587 : | suxen_drol | 889 | { |
588 : | case XVID_ERR_FAIL : | ||
589 : | return ICERR_ERROR; | ||
590 : | |||
591 : | case XVID_ERR_MEMORY : | ||
592 : | return ICERR_MEMORY; | ||
593 : | |||
594 : | case XVID_ERR_FORMAT : | ||
595 : | return ICERR_BADFORMAT; | ||
596 : | |||
597 : | case XVID_ERR_VERSION : | ||
598 : | return ICERR_UNSUPPORTED; | ||
599 : | } | ||
600 : | |||
601 : | codec->ehandle = create.handle; | ||
602 : | codec->framenum = 0; | ||
603 : | codec->keyspacing = 0; | ||
604 : | |||
605 : | syskin | 1311 | if (codec->config.display_status) { |
606 : | status_destroy_always(&codec->status); | ||
607 : | status_create(&codec->status, codec->fincr, codec->fbase); | ||
608 : | } | ||
609 : | suxen_drol | 1061 | |
610 : | suxen_drol | 889 | return ICERR_OK; |
611 : | } | ||
612 : | |||
613 : | |||
614 : | LRESULT compress_end(CODEC * codec) | ||
615 : | { | ||
616 : | syskin | 1311 | if (m_hdll != NULL) { |
617 : | syskin | 1301 | if (codec->ehandle != NULL) { |
618 : | xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); | ||
619 : | codec->ehandle = NULL; | ||
620 : | } | ||
621 : | FreeLibrary(m_hdll); | ||
622 : | m_hdll = NULL; | ||
623 : | suxen_drol | 889 | } |
624 : | |||
625 : | syskin | 1311 | if (codec->config.display_status) |
626 : | status_destroy(&codec->status); | ||
627 : | suxen_drol | 1061 | |
628 : | suxen_drol | 889 | return ICERR_OK; |
629 : | } | ||
630 : | |||
631 : | suxen_drol | 1017 | |
632 : | static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum) | ||
633 : | { | ||
634 : | syskin | 1311 | int i; |
635 : | suxen_drol | 1017 | |
636 : | syskin | 1311 | for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ; |
637 : | suxen_drol | 1017 | |
638 : | syskin | 1311 | if (--i < 0) return; /* there are no zones, or we're before the first zone */ |
639 : | suxen_drol | 1069 | |
640 : | syskin | 1311 | if (framenum == config->zones[i].frame) |
641 : | syskin | 1245 | frame->type = config->zones[i].type; |
642 : | |||
643 : | syskin | 1311 | if (config->zones[i].greyscale) { |
644 : | frame->vop_flags |= XVID_VOP_GREYSCALE; | ||
645 : | } | ||
646 : | suxen_drol | 1017 | |
647 : | syskin | 1311 | if (config->zones[i].chroma_opt) { |
648 : | frame->vop_flags |= XVID_VOP_CHROMAOPT; | ||
649 : | } | ||
650 : | suxen_drol | 1017 | |
651 : | syskin | 1311 | if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) { |
652 : | frame->bframe_threshold = config->zones[i].bvop_threshold; | ||
653 : | } | ||
654 : | suxen_drol | 1017 | } |
655 : | |||
656 : | |||
657 : | suxen_drol | 889 | LRESULT compress(CODEC * codec, ICCOMPRESS * icc) |
658 : | { | ||
659 : | BITMAPINFOHEADER * inhdr = icc->lpbiInput; | ||
660 : | BITMAPINFOHEADER * outhdr = icc->lpbiOutput; | ||
661 : | xvid_enc_frame_t frame; | ||
662 : | xvid_enc_stats_t stats; | ||
663 : | int length; | ||
664 : | |||
665 : | memset(&frame, 0, sizeof(frame)); | ||
666 : | frame.version = XVID_VERSION; | ||
667 : | |||
668 : | syskin | 1311 | frame.type = XVID_TYPE_AUTO; |
669 : | suxen_drol | 889 | |
670 : | /* vol stuff */ | ||
671 : | |||
672 : | syskin | 1311 | if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) && |
673 : | codec->config.quant_type != QUANT_MODE_H263) | ||
674 : | suxen_drol | 889 | { |
675 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_MPEGQUANT; |
676 : | suxen_drol | 889 | |
677 : | suxen_drol | 1017 | if (codec->config.quant_type == QUANT_MODE_CUSTOM) { |
678 : | suxen_drol | 889 | frame.quant_intra_matrix = codec->config.qmatrix_intra; |
679 : | frame.quant_inter_matrix = codec->config.qmatrix_inter; | ||
680 : | suxen_drol | 1017 | }else{ |
681 : | suxen_drol | 889 | frame.quant_intra_matrix = NULL; |
682 : | frame.quant_inter_matrix = NULL; | ||
683 : | } | ||
684 : | } | ||
685 : | |||
686 : | syskin | 1311 | if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) && |
687 : | codec->config.reduced_resolution) { | ||
688 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_REDUCED_ENABLE; |
689 : | frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */ | ||
690 : | suxen_drol | 889 | } |
691 : | |||
692 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) { |
693 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_QUARTERPEL; |
694 : | frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; | ||
695 : | suxen_drol | 889 | } |
696 : | |||
697 : | syskin | 1087 | if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) { |
698 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_GMC; |
699 : | edgomez | 1107 | frame.motion |= XVID_ME_GME_REFINE; |
700 : | syskin | 1087 | } |
701 : | suxen_drol | 889 | |
702 : | suxen_drol | 1017 | if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) |
703 : | edgomez | 949 | frame.vol_flags |= XVID_VOL_INTERLACING; |
704 : | suxen_drol | 889 | |
705 : | syskin | 1277 | if (codec->config.ar_mode == 0) { /* PAR */ |
706 : | if (codec->config.display_aspect_ratio != 5) { | ||
707 : | frame.par = codec->config.display_aspect_ratio + 1; | ||
708 : | } else { | ||
709 : | frame.par = XVID_PAR_EXT; | ||
710 : | frame.par_width = codec->config.par_x; | ||
711 : | frame.par_height= codec->config.par_y; | ||
712 : | edgomez | 1267 | } |
713 : | syskin | 1277 | } else { /* AR */ |
714 : | edgomez | 1267 | /* custom pixel aspect ratio -> calculated from DAR */ |
715 : | frame.par = XVID_PAR_EXT; | ||
716 : | syskin | 1277 | frame.par_width = (100 * inhdr->biHeight) / codec->config.ar_y; |
717 : | edgomez | 1286 | frame.par_height= (100 * inhdr->biWidth) / codec->config.ar_x; |
718 : | edgomez | 1267 | } |
719 : | |||
720 : | syskin | 1311 | /* vop stuff */ |
721 : | suxen_drol | 889 | |
722 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_HALFPEL; |
723 : | frame.vop_flags |= XVID_VOP_HQACPRED; | ||
724 : | suxen_drol | 889 | |
725 : | suxen_drol | 1032 | if (codec->config.vop_debug) |
726 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_DEBUG; |
727 : | suxen_drol | 889 | |
728 : | syskin | 1311 | if (codec->config.trellis_quant) { |
729 : | frame.vop_flags |= XVID_VOP_TRELLISQUANT; | ||
730 : | } | ||
731 : | suxen_drol | 1017 | |
732 : | syskin | 1311 | if (codec->config.motion_search > 4) |
733 : | edgomez | 949 | frame.vop_flags |= XVID_VOP_INTER4V; |
734 : | suxen_drol | 889 | |
735 : | if (codec->config.chromame) | ||
736 : | edgomez | 1107 | frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP; |
737 : | suxen_drol | 889 | |
738 : | edgomez | 1193 | if (codec->config.cartoon_mode) { |
739 : | frame.vop_flags |= XVID_VOP_CARTOON; | ||
740 : | frame.motion |= XVID_ME_DETECT_STATIC_MOTION; | ||
741 : | } | ||
742 : | |||
743 : | Isibaar | 1294 | if (codec->config.turbo) |
744 : | frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 | | ||
745 : | XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE | | ||
746 : | XVID_ME_BFRAME_EARLYSTOP; | ||
747 : | |||
748 : | suxen_drol | 889 | frame.motion |= pmvfast_presets[codec->config.motion_search]; |
749 : | |||
750 : | switch (codec->config.vhq_mode) | ||
751 : | { | ||
752 : | case VHQ_MODE_DECISION : | ||
753 : | edgomez | 1107 | frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
754 : | suxen_drol | 889 | break; |
755 : | |||
756 : | case VHQ_LIMITED_SEARCH : | ||
757 : | edgomez | 1107 | frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
758 : | frame.motion |= XVID_ME_HALFPELREFINE16_RD; | ||
759 : | frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; | ||
760 : | suxen_drol | 889 | break; |
761 : | |||
762 : | case VHQ_MEDIUM_SEARCH : | ||
763 : | edgomez | 1107 | frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
764 : | frame.motion |= XVID_ME_HALFPELREFINE16_RD; | ||
765 : | frame.motion |= XVID_ME_HALFPELREFINE8_RD; | ||
766 : | frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; | ||
767 : | frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; | ||
768 : | frame.motion |= XVID_ME_CHECKPREDICTION_RD; | ||
769 : | suxen_drol | 889 | break; |
770 : | |||
771 : | case VHQ_WIDE_SEARCH : | ||
772 : | edgomez | 1107 | frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
773 : | frame.motion |= XVID_ME_HALFPELREFINE16_RD; | ||
774 : | frame.motion |= XVID_ME_HALFPELREFINE8_RD; | ||
775 : | frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; | ||
776 : | frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; | ||
777 : | frame.motion |= XVID_ME_CHECKPREDICTION_RD; | ||
778 : | frame.motion |= XVID_ME_EXTSEARCH_RD; | ||
779 : | suxen_drol | 889 | break; |
780 : | |||
781 : | default : | ||
782 : | break; | ||
783 : | } | ||
784 : | |||
785 : | frame.input.plane[0] = icc->lpInput; | ||
786 : | frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3; | ||
787 : | |||
788 : | if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL) | ||
789 : | return ICERR_BADFORMAT; | ||
790 : | |||
791 : | syskin | 1356 | if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12) { |
792 : | frame.input.stride[0] = (4 * icc->lpbiInput->biWidth + 3) / 4; | ||
793 : | frame.input.stride[1] = frame.input.stride[2] = frame.input.stride[0] / 2 ; | ||
794 : | } | ||
795 : | suxen_drol | 889 | |
796 : | frame.bitstream = icc->lpOutput; | ||
797 : | frame.length = icc->lpbiOutput->biSizeImage; | ||
798 : | |||
799 : | syskin | 1311 | frame.quant = 0; |
800 : | suxen_drol | 889 | |
801 : | syskin | 1311 | if (codec->config.mode == RC_MODE_NULL) { |
802 : | suxen_drol | 889 | outhdr->biSizeImage = 0; |
803 : | *icc->lpdwFlags = AVIIF_KEYFRAME; | ||
804 : | return ICERR_OK; | ||
805 : | syskin | 1311 | } |
806 : | suxen_drol | 889 | |
807 : | // force keyframe spacing in 2-pass 1st pass | ||
808 : | if (codec->config.motion_search == 0) | ||
809 : | frame.type = XVID_TYPE_IVOP; | ||
810 : | |||
811 : | syskin | 1311 | /* frame-based stuff */ |
812 : | apply_zone_modifiers(&frame, &codec->config, codec->framenum); | ||
813 : | suxen_drol | 1017 | |
814 : | syskin | 1311 | /* call encore */ |
815 : | suxen_drol | 1017 | |
816 : | suxen_drol | 889 | memset(&stats, 0, sizeof(stats)); |
817 : | stats.version = XVID_VERSION; | ||
818 : | |||
819 : | syskin | 1311 | length = xvid_encore_func(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats); |
820 : | suxen_drol | 889 | switch (length) |
821 : | { | ||
822 : | case XVID_ERR_FAIL : | ||
823 : | return ICERR_ERROR; | ||
824 : | |||
825 : | case XVID_ERR_MEMORY : | ||
826 : | return ICERR_MEMORY; | ||
827 : | |||
828 : | case XVID_ERR_FORMAT : | ||
829 : | return ICERR_BADFORMAT; | ||
830 : | |||
831 : | case XVID_ERR_VERSION : | ||
832 : | return ICERR_UNSUPPORTED; | ||
833 : | } | ||
834 : | |||
835 : | syskin | 1311 | if (codec->config.display_status && stats.type>0) { |
836 : | status_update(&codec->status, stats.type, stats.length, stats.quant); | ||
837 : | } | ||
838 : | suxen_drol | 1061 | |
839 : | suxen_drol | 983 | DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); |
840 : | suxen_drol | 889 | |
841 : | syskin | 1311 | if (length == 0) /* no encoder output */ |
842 : | suxen_drol | 889 | { |
843 : | *icc->lpdwFlags = 0; | ||
844 : | ((char*)icc->lpOutput)[0] = 0x7f; /* virtual dub skip frame */ | ||
845 : | outhdr->biSizeImage = 1; | ||
846 : | |||
847 : | }else{ | ||
848 : | if (frame.out_flags & XVID_KEYFRAME) | ||
849 : | syskin | 1311 | { |
850 : | codec->keyspacing = 0; | ||
851 : | *icc->lpdwFlags = AVIIF_KEYFRAME; | ||
852 : | } | ||
853 : | else | ||
854 : | { | ||
855 : | *icc->lpdwFlags = 0; | ||
856 : | } | ||
857 : | suxen_drol | 889 | |
858 : | syskin | 1311 | outhdr->biSizeImage = length; |
859 : | suxen_drol | 889 | |
860 : | syskin | 1311 | if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass) |
861 : | { | ||
862 : | outhdr->biSizeImage = 0; | ||
863 : | } | ||
864 : | } | ||
865 : | suxen_drol | 889 | |
866 : | suxen_drol | 1017 | codec->framenum++; |
867 : | codec->keyspacing++; | ||
868 : | suxen_drol | 889 | |
869 : | return ICERR_OK; | ||
870 : | } | ||
871 : | |||
872 : | |||
873 : | /* decompressor */ | ||
874 : | |||
875 : | |||
876 : | LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) | ||
877 : | { | ||
878 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
879 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
880 : | |||
881 : | if (lpbiInput == NULL) | ||
882 : | { | ||
883 : | return ICERR_ERROR; | ||
884 : | } | ||
885 : | |||
886 : | if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL) | ||
887 : | { | ||
888 : | return ICERR_BADFORMAT; | ||
889 : | } | ||
890 : | |||
891 : | if (lpbiOutput == NULL) | ||
892 : | { | ||
893 : | return ICERR_OK; | ||
894 : | } | ||
895 : | |||
896 : | if (inhdr->biWidth != outhdr->biWidth || | ||
897 : | inhdr->biHeight != outhdr->biHeight || | ||
898 : | get_colorspace(outhdr) == XVID_CSP_NULL) | ||
899 : | { | ||
900 : | return ICERR_BADFORMAT; | ||
901 : | } | ||
902 : | |||
903 : | return ICERR_OK; | ||
904 : | } | ||
905 : | |||
906 : | |||
907 : | LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
908 : | { | ||
909 : | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; | ||
910 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
911 : | LRESULT result; | ||
912 : | |||
913 : | if (lpbiOutput == NULL) | ||
914 : | { | ||
915 : | return sizeof(BITMAPINFOHEADER); | ||
916 : | } | ||
917 : | |||
918 : | /* --- yv12 --- */ | ||
919 : | |||
920 : | if (get_colorspace(inhdr) != XVID_CSP_NULL) { | ||
921 : | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); | ||
922 : | edgomez | 1053 | /* XXX: should we set outhdr->biSize ?? */ |
923 : | suxen_drol | 889 | return ICERR_OK; |
924 : | } | ||
925 : | /* --- yv12 --- */ | ||
926 : | |||
927 : | result = decompress_query(codec, lpbiInput, lpbiOutput); | ||
928 : | if (result != ICERR_OK) | ||
929 : | { | ||
930 : | return result; | ||
931 : | } | ||
932 : | |||
933 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
934 : | outhdr->biWidth = inhdr->biWidth; | ||
935 : | outhdr->biHeight = inhdr->biHeight; | ||
936 : | outhdr->biPlanes = 1; | ||
937 : | outhdr->biBitCount = 24; | ||
938 : | outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */ | ||
939 : | outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount; | ||
940 : | outhdr->biXPelsPerMeter = 0; | ||
941 : | outhdr->biYPelsPerMeter = 0; | ||
942 : | outhdr->biClrUsed = 0; | ||
943 : | outhdr->biClrImportant = 0; | ||
944 : | |||
945 : | return ICERR_OK; | ||
946 : | } | ||
947 : | |||
948 : | edgomez | 1326 | #define REG_GET_N(X, Y, Z) \ |
949 : | { \ | ||
950 : | DWORD size = sizeof(int); \ | ||
951 : | if (RegQueryValueEx(hKey, X, 0, 0, (LPBYTE)&Y, &size) != ERROR_SUCCESS) { \ | ||
952 : | Y=Z; \ | ||
953 : | } \ | ||
954 : | }while(0) | ||
955 : | suxen_drol | 889 | |
956 : | LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
957 : | { | ||
958 : | xvid_gbl_init_t init; | ||
959 : | xvid_dec_create_t create; | ||
960 : | syskin | 1323 | HKEY hKey; |
961 : | suxen_drol | 889 | |
962 : | syskin | 1301 | if (init_dll() != 0) return ICERR_ERROR; |
963 : | |||
964 : | suxen_drol | 889 | memset(&init, 0, sizeof(init)); |
965 : | init.version = XVID_VERSION; | ||
966 : | init.cpu_flags = codec->config.cpu; | ||
967 : | syskin | 1301 | xvid_global_func(0, XVID_GBL_INIT, &init, NULL); |
968 : | suxen_drol | 889 | |
969 : | memset(&create, 0, sizeof(create)); | ||
970 : | create.version = XVID_VERSION; | ||
971 : | create.width = lpbiInput->bmiHeader.biWidth; | ||
972 : | create.height = lpbiInput->bmiHeader.biHeight; | ||
973 : | |||
974 : | syskin | 1301 | switch(xvid_decore_func(0, XVID_DEC_CREATE, &create, NULL)) |
975 : | suxen_drol | 889 | { |
976 : | syskin | 1329 | case XVID_ERR_FAIL : |
977 : | suxen_drol | 889 | return ICERR_ERROR; |
978 : | |||
979 : | case XVID_ERR_MEMORY : | ||
980 : | return ICERR_MEMORY; | ||
981 : | |||
982 : | case XVID_ERR_FORMAT : | ||
983 : | return ICERR_BADFORMAT; | ||
984 : | |||
985 : | case XVID_ERR_VERSION : | ||
986 : | return ICERR_UNSUPPORTED; | ||
987 : | } | ||
988 : | |||
989 : | codec->dhandle = create.handle; | ||
990 : | |||
991 : | syskin | 1323 | RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey); |
992 : | |||
993 : | REG_GET_N("Deblock_Y", pp_dy, 0) | ||
994 : | REG_GET_N("Deblock_UV", pp_duv, 0) | ||
995 : | REG_GET_N("Dering", pp_dr, 0) | ||
996 : | REG_GET_N("FilmEffect", pp_fe, 0) | ||
997 : | |||
998 : | RegCloseKey(hKey); | ||
999 : | |||
1000 : | suxen_drol | 889 | return ICERR_OK; |
1001 : | } | ||
1002 : | |||
1003 : | |||
1004 : | LRESULT decompress_end(CODEC * codec) | ||
1005 : | { | ||
1006 : | syskin | 1301 | if (m_hdll != NULL) { |
1007 : | if (codec->dhandle != NULL) { | ||
1008 : | xvid_decore_func(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); | ||
1009 : | codec->dhandle = NULL; | ||
1010 : | } | ||
1011 : | FreeLibrary(m_hdll); | ||
1012 : | m_hdll = NULL; | ||
1013 : | suxen_drol | 889 | } |
1014 : | syskin | 1301 | |
1015 : | suxen_drol | 889 | return ICERR_OK; |
1016 : | } | ||
1017 : | |||
1018 : | |||
1019 : | LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd) | ||
1020 : | { | ||
1021 : | xvid_dec_frame_t frame; | ||
1022 : | |||
1023 : | /* --- yv12 --- */ | ||
1024 : | if (icd->lpbiInput->biCompression != FOURCC_XVID && | ||
1025 : | icd->lpbiInput->biCompression != FOURCC_DIVX && | ||
1026 : | icd->lpbiInput->biCompression != FOURCC_DX50) | ||
1027 : | { | ||
1028 : | xvid_gbl_convert_t convert; | ||
1029 : | |||
1030 : | suxen_drol | 983 | DPRINTF("input=%c%c%c%c output=%c%c%c%c", |
1031 : | syskin | 1311 | icd->lpbiInput->biCompression&0xff, |
1032 : | (icd->lpbiInput->biCompression>>8)&0xff, | ||
1033 : | (icd->lpbiInput->biCompression>>16)&0xff, | ||
1034 : | (icd->lpbiInput->biCompression>>24)&0xff, | ||
1035 : | icd->lpbiOutput->biCompression&0xff, | ||
1036 : | (icd->lpbiOutput->biCompression>>8)&0xff, | ||
1037 : | (icd->lpbiOutput->biCompression>>16)&0xff, | ||
1038 : | (icd->lpbiOutput->biCompression>>24)&0xff); | ||
1039 : | suxen_drol | 889 | |
1040 : | memset(&convert, 0, sizeof(convert)); | ||
1041 : | convert.version = XVID_VERSION; | ||
1042 : | |||
1043 : | convert.input.csp = get_colorspace(icd->lpbiInput); | ||
1044 : | convert.input.plane[0] = icd->lpInput; | ||
1045 : | convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3; | ||
1046 : | if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12) | ||
1047 : | convert.input.stride[0] = (convert.input.stride[0]*2)/3; | ||
1048 : | |||
1049 : | convert.output.csp = get_colorspace(icd->lpbiOutput); | ||
1050 : | convert.output.plane[0] = icd->lpOutput; | ||
1051 : | convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; | ||
1052 : | if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12) | ||
1053 : | convert.output.stride[0] = (convert.output.stride[0]*2)/3; | ||
1054 : | |||
1055 : | convert.width = icd->lpbiInput->biWidth; | ||
1056 : | convert.height = icd->lpbiInput->biHeight; | ||
1057 : | convert.interlacing = 0; | ||
1058 : | if (convert.input.csp == XVID_CSP_NULL || | ||
1059 : | convert.output.csp == XVID_CSP_NULL || | ||
1060 : | syskin | 1301 | xvid_global_func(0, XVID_GBL_CONVERT, &convert, NULL) < 0) |
1061 : | suxen_drol | 889 | { |
1062 : | return ICERR_BADFORMAT; | ||
1063 : | } | ||
1064 : | return ICERR_OK; | ||
1065 : | } | ||
1066 : | /* --- yv12 --- */ | ||
1067 : | |||
1068 : | syskin | 1311 | memset(&frame, 0, sizeof(frame)); |
1069 : | frame.version = XVID_VERSION; | ||
1070 : | frame.general = XVID_LOWDELAY; /* force low_delay_default mode */ | ||
1071 : | suxen_drol | 889 | frame.bitstream = icd->lpInput; |
1072 : | frame.length = icd->lpbiInput->biSizeImage; | ||
1073 : | |||
1074 : | if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL))) | ||
1075 : | { | ||
1076 : | if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL) | ||
1077 : | { | ||
1078 : | return ICERR_BADFORMAT; | ||
1079 : | } | ||
1080 : | frame.output.plane[0] = icd->lpOutput; | ||
1081 : | frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; | ||
1082 : | if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12) | ||
1083 : | frame.output.stride[0] = (frame.output.stride[0]*2)/3; | ||
1084 : | } | ||
1085 : | else | ||
1086 : | { | ||
1087 : | frame.output.csp = XVID_CSP_NULL; | ||
1088 : | } | ||
1089 : | |||
1090 : | syskin | 1323 | if (pp_dy)frame.general |= XVID_DEBLOCKY; |
1091 : | if (pp_duv) frame.general |= XVID_DEBLOCKUV; | ||
1092 : | /* if (pp_dr) frame.general |= XVID_DERING; */ | ||
1093 : | if (pp_fe) frame.general |= XVID_FILMEFFECT; | ||
1094 : | |||
1095 : | syskin | 1301 | switch (xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) |
1096 : | suxen_drol | 889 | { |
1097 : | case XVID_ERR_FAIL : | ||
1098 : | return ICERR_ERROR; | ||
1099 : | |||
1100 : | case XVID_ERR_MEMORY : | ||
1101 : | return ICERR_MEMORY; | ||
1102 : | |||
1103 : | case XVID_ERR_FORMAT : | ||
1104 : | return ICERR_BADFORMAT; | ||
1105 : | |||
1106 : | case XVID_ERR_VERSION : | ||
1107 : | return ICERR_UNSUPPORTED; | ||
1108 : | } | ||
1109 : | |||
1110 : | return ICERR_OK; | ||
1111 : | } | ||
1112 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |