Parent Directory | Revision Log
Revision 609 - (view) (download)
1 : | Isibaar | 3 | /************************************************************************** |
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 : | suxen_drol | 295 | * 12.07.2002 num_threads |
27 : | suxen_drol | 235 | * 23.06.2002 XVID_CPU_CHKONLY; loading speed up |
28 : | suxen_drol | 137 | * 25.04.2002 ICDECOMPRESS_PREROLL |
29 : | h | 127 | * 17.04.2002 re-enabled lumi masking for 1st pass |
30 : | h | 121 | * 15.04.2002 updated cbr support |
31 : | h | 102 | * 04.04.2002 separated 2-pass code to 2pass.c |
32 : | * interlacing support | ||
33 : | * hinted ME support | ||
34 : | h | 61 | * 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 : | h | 30 | * 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 : | h | 526 | * 01.12.2001 inital version; (c)2001 peter ross <pross@xvid.org> |
47 : | Isibaar | 3 | * |
48 : | *************************************************************************/ | ||
49 : | |||
50 : | #include <windows.h> | ||
51 : | #include <vfw.h> | ||
52 : | |||
53 : | #include "codec.h" | ||
54 : | h | 102 | #include "2pass.h" |
55 : | Isibaar | 3 | |
56 : | int pmvfast_presets[7] = { | ||
57 : | Isibaar | 593 | 0, PMV_QUICKSTOP16, 0, 0, |
58 : | 0 | PMV_HALFPELREFINE16 | PMV_HALFPELDIAMOND8, | ||
59 : | 0 | PMV_HALFPELREFINE16 | PMV_HALFPELDIAMOND8 | | ||
60 : | PMV_ADVANCEDDIAMOND16, PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | | ||
61 : | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 | PMV_USESQUARES16 | ||
62 : | Isibaar | 3 | }; |
63 : | |||
64 : | /* return xvid compatbile colorspace, | ||
65 : | or XVID_CSP_NULL if failure | ||
66 : | */ | ||
67 : | |||
68 : | h | 30 | int get_colorspace(BITMAPINFOHEADER * hdr) |
69 : | Isibaar | 3 | { |
70 : | suxen_drol | 595 | /* rgb only: negative height specifies top down image */ |
71 : | int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP); | ||
72 : | |||
73 : | h | 30 | switch(hdr->biCompression) |
74 : | Isibaar | 3 | { |
75 : | case BI_RGB : | ||
76 : | h | 30 | if (hdr->biBitCount == 16) |
77 : | Isibaar | 3 | { |
78 : | DEBUG("RGB16 (RGB555)"); | ||
79 : | suxen_drol | 595 | return rgb_flip | XVID_CSP_RGB555; |
80 : | Isibaar | 3 | } |
81 : | h | 30 | if (hdr->biBitCount == 24) |
82 : | Isibaar | 3 | { |
83 : | DEBUG("RGB24"); | ||
84 : | suxen_drol | 595 | return rgb_flip | XVID_CSP_RGB24; |
85 : | Isibaar | 3 | } |
86 : | h | 30 | if (hdr->biBitCount == 32) |
87 : | Isibaar | 3 | { |
88 : | DEBUG("RGB32"); | ||
89 : | suxen_drol | 595 | return rgb_flip | XVID_CSP_RGB32; |
90 : | Isibaar | 3 | } |
91 : | |||
92 : | suxen_drol | 595 | DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount); |
93 : | Isibaar | 3 | return XVID_CSP_NULL; |
94 : | |||
95 : | suxen_drol | 595 | case BI_BITFIELDS : |
96 : | if (hdr->biSize >= sizeof(BITMAPV4HEADER)) | ||
97 : | Isibaar | 3 | { |
98 : | suxen_drol | 595 | BITMAPV4HEADER * hdr4 = (BITMAPV4HEADER *)hdr; |
99 : | |||
100 : | if (hdr4->bV4BitCount == 16 && | ||
101 : | hdr4->bV4RedMask == 0x7c00 && | ||
102 : | hdr4->bV4GreenMask == 0x3e0 && | ||
103 : | hdr4->bV4BlueMask == 0x1f) | ||
104 : | { | ||
105 : | DEBUG("RGB555"); | ||
106 : | return rgb_flip | XVID_CSP_RGB555; | ||
107 : | } | ||
108 : | |||
109 : | if(hdr4->bV4BitCount == 16 && | ||
110 : | hdr4->bV4RedMask == 0xf800 && | ||
111 : | hdr4->bV4GreenMask == 0x7e0 && | ||
112 : | hdr4->bV4BlueMask == 0x1f) | ||
113 : | { | ||
114 : | DEBUG("RGB565"); | ||
115 : | return rgb_flip | XVID_CSP_RGB565; | ||
116 : | } | ||
117 : | |||
118 : | DEBUG("unsupported BI_BITFIELDS mode"); | ||
119 : | return XVID_CSP_NULL; | ||
120 : | Isibaar | 3 | } |
121 : | |||
122 : | suxen_drol | 595 | DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
123 : | Isibaar | 3 | return XVID_CSP_NULL; |
124 : | suxen_drol | 595 | |
125 : | case FOURCC_I420 : | ||
126 : | case FOURCC_IYUV : | ||
127 : | Isibaar | 3 | DEBUG("IYUY"); |
128 : | return XVID_CSP_I420; | ||
129 : | |||
130 : | case FOURCC_YV12 : | ||
131 : | DEBUG("YV12"); | ||
132 : | return XVID_CSP_YV12; | ||
133 : | |||
134 : | case FOURCC_YUYV : | ||
135 : | case FOURCC_YUY2 : | ||
136 : | DEBUG("YUY2"); | ||
137 : | return XVID_CSP_YUY2; | ||
138 : | |||
139 : | suxen_drol | 595 | case FOURCC_YVYU : |
140 : | Isibaar | 3 | DEBUG("YVYU"); |
141 : | return XVID_CSP_YVYU; | ||
142 : | |||
143 : | suxen_drol | 595 | case FOURCC_UYVY : |
144 : | Isibaar | 3 | DEBUG("UYVY"); |
145 : | return XVID_CSP_UYVY; | ||
146 : | |||
147 : | suxen_drol | 595 | default : |
148 : | DEBUGFOURCC("unsupported colorspace", hdr->biCompression); | ||
149 : | return XVID_CSP_NULL; | ||
150 : | Isibaar | 3 | } |
151 : | } | ||
152 : | |||
153 : | |||
154 : | /* compressor */ | ||
155 : | |||
156 : | |||
157 : | /* test the output format */ | ||
158 : | |||
159 : | LRESULT compress_query(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
160 : | { | ||
161 : | h | 30 | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
162 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
163 : | Isibaar | 3 | |
164 : | if (get_colorspace(inhdr) == XVID_CSP_NULL) | ||
165 : | { | ||
166 : | return ICERR_BADFORMAT; | ||
167 : | } | ||
168 : | |||
169 : | if (lpbiOutput == NULL) | ||
170 : | { | ||
171 : | return ICERR_OK; | ||
172 : | } | ||
173 : | h | 30 | |
174 : | if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight || | ||
175 : | (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX)) | ||
176 : | Isibaar | 3 | { |
177 : | return ICERR_BADFORMAT; | ||
178 : | } | ||
179 : | |||
180 : | return ICERR_OK; | ||
181 : | } | ||
182 : | |||
183 : | |||
184 : | LRESULT compress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
185 : | { | ||
186 : | h | 30 | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
187 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
188 : | Isibaar | 3 | |
189 : | h | 30 | if (get_colorspace(inhdr) == XVID_CSP_NULL) |
190 : | Isibaar | 3 | { |
191 : | h | 30 | return ICERR_BADFORMAT; |
192 : | Isibaar | 3 | } |
193 : | |||
194 : | if (lpbiOutput == NULL) | ||
195 : | { | ||
196 : | return sizeof(BITMAPV4HEADER); | ||
197 : | } | ||
198 : | |||
199 : | h | 30 | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
200 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
201 : | outhdr->biSizeImage = compress_get_size(codec, lpbiInput, lpbiOutput); | ||
202 : | outhdr->biXPelsPerMeter = 0; | ||
203 : | outhdr->biYPelsPerMeter = 0; | ||
204 : | outhdr->biClrUsed = 0; | ||
205 : | outhdr->biClrImportant = 0; | ||
206 : | Isibaar | 3 | |
207 : | if (codec->config.fourcc_used == 0) | ||
208 : | { | ||
209 : | h | 30 | outhdr->biCompression = FOURCC_XVID; |
210 : | Isibaar | 3 | } |
211 : | h | 30 | else if (codec->config.fourcc_used == 1) |
212 : | { | ||
213 : | outhdr->biCompression = FOURCC_DIVX; | ||
214 : | } | ||
215 : | Isibaar | 3 | else |
216 : | { | ||
217 : | h | 30 | outhdr->biCompression = FOURCC_DX50; |
218 : | Isibaar | 3 | } |
219 : | |||
220 : | return ICERR_OK; | ||
221 : | } | ||
222 : | |||
223 : | |||
224 : | LRESULT compress_get_size(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
225 : | { | ||
226 : | suxen_drol | 228 | return |
227 : | #ifdef BFRAMES | ||
228 : | 2 * | ||
229 : | #endif | ||
230 : | lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3; | ||
231 : | Isibaar | 3 | } |
232 : | |||
233 : | |||
234 : | LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) | ||
235 : | { | ||
236 : | // DEBUG2("frate fscale", codec->frate, codec->fscale); | ||
237 : | codec->fincr = icf->dwScale; | ||
238 : | codec->fbase = icf->dwRate; | ||
239 : | h | 609 | |
240 : | if (codec->fbase == 0 || codec->fincr == 0) | ||
241 : | { | ||
242 : | codec->fbase = 25; | ||
243 : | codec->fincr = 1; | ||
244 : | } | ||
245 : | |||
246 : | Isibaar | 3 | return ICERR_OK; |
247 : | } | ||
248 : | |||
249 : | |||
250 : | LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
251 : | { | ||
252 : | XVID_ENC_PARAM param; | ||
253 : | XVID_INIT_PARAM init_param; | ||
254 : | |||
255 : | switch (codec->config.mode) | ||
256 : | { | ||
257 : | case DLG_MODE_CBR : | ||
258 : | h | 121 | param.rc_bitrate = codec->config.rc_bitrate; |
259 : | param.rc_reaction_delay_factor = codec->config.rc_reaction_delay_factor; | ||
260 : | param.rc_averaging_period = codec->config.rc_averaging_period; | ||
261 : | param.rc_buffer = codec->config.rc_buffer; | ||
262 : | Isibaar | 3 | break; |
263 : | |||
264 : | case DLG_MODE_VBR_QUAL : | ||
265 : | codec->config.fquant = 0; | ||
266 : | h | 121 | param.rc_bitrate = 0; |
267 : | Isibaar | 3 | break; |
268 : | |||
269 : | case DLG_MODE_VBR_QUANT : | ||
270 : | codec->config.fquant = (float) codec->config.quant; | ||
271 : | h | 121 | param.rc_bitrate = 0; |
272 : | Isibaar | 3 | break; |
273 : | |||
274 : | h | 61 | case DLG_MODE_2PASS_1 : |
275 : | case DLG_MODE_2PASS_2_INT : | ||
276 : | case DLG_MODE_2PASS_2_EXT : | ||
277 : | h | 121 | param.rc_bitrate = 0; |
278 : | h | 109 | codec->twopass.max_framesize = (int)((double)codec->config.twopass_max_bitrate / 8.0 / ((double)codec->fbase / (double)codec->fincr)); |
279 : | Isibaar | 3 | break; |
280 : | |||
281 : | h | 61 | case DLG_MODE_NULL : |
282 : | return ICERR_OK; | ||
283 : | |||
284 : | Isibaar | 3 | default : |
285 : | break; | ||
286 : | } | ||
287 : | |||
288 : | if(codec->ehandle) | ||
289 : | { | ||
290 : | xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); | ||
291 : | codec->ehandle = NULL; | ||
292 : | } | ||
293 : | |||
294 : | init_param.cpu_flags = codec->config.cpu; | ||
295 : | xvid_init(0, 0, &init_param, NULL); | ||
296 : | if((codec->config.cpu & XVID_CPU_FORCE) <= 0) | ||
297 : | codec->config.cpu = init_param.cpu_flags; | ||
298 : | |||
299 : | h | 30 | param.width = lpbiInput->bmiHeader.biWidth; |
300 : | param.height = lpbiInput->bmiHeader.biHeight; | ||
301 : | Isibaar | 3 | param.fincr = codec->fincr; |
302 : | param.fbase = codec->fbase; | ||
303 : | |||
304 : | h | 30 | param.min_quantizer = codec->config.min_pquant; |
305 : | param.max_quantizer = codec->config.max_pquant; | ||
306 : | Isibaar | 3 | param.max_key_interval = codec->config.max_key_interval; |
307 : | |||
308 : | suxen_drol | 295 | #ifdef _SMP |
309 : | param.num_threads = codec->config.num_threads; | ||
310 : | #endif | ||
311 : | |||
312 : | suxen_drol | 228 | #ifdef BFRAMES |
313 : | suxen_drol | 235 | param.global = 0; |
314 : | if (codec->config.packed) param.global |= XVID_GLOBAL_PACKED; | ||
315 : | if (codec->config.dx50bvop) param.global |= XVID_GLOBAL_DX50BVOP; | ||
316 : | if (codec->config.debug) param.global |= XVID_GLOBAL_DEBUG; | ||
317 : | suxen_drol | 228 | param.max_bframes = codec->config.max_bframes; |
318 : | param.bquant_ratio = codec->config.bquant_ratio; | ||
319 : | suxen_drol | 324 | param.frame_drop_ratio = codec->config.frame_drop_ratio; |
320 : | suxen_drol | 228 | #endif |
321 : | |||
322 : | Isibaar | 3 | switch(xvid_encore(0, XVID_ENC_CREATE, ¶m, NULL)) |
323 : | { | ||
324 : | case XVID_ERR_FAIL : | ||
325 : | return ICERR_ERROR; | ||
326 : | |||
327 : | case XVID_ERR_MEMORY : | ||
328 : | return ICERR_MEMORY; | ||
329 : | |||
330 : | case XVID_ERR_FORMAT : | ||
331 : | return ICERR_BADFORMAT; | ||
332 : | } | ||
333 : | |||
334 : | codec->ehandle = param.handle; | ||
335 : | codec->framenum = 0; | ||
336 : | codec->keyspacing = 0; | ||
337 : | |||
338 : | h | 102 | codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE; |
339 : | codec->twopass.hintstream = NULL; | ||
340 : | Isibaar | 3 | |
341 : | return ICERR_OK; | ||
342 : | } | ||
343 : | |||
344 : | |||
345 : | LRESULT compress_end(CODEC * codec) | ||
346 : | { | ||
347 : | if (codec->ehandle != NULL) | ||
348 : | { | ||
349 : | xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); | ||
350 : | h | 102 | |
351 : | if (codec->twopass.hints != INVALID_HANDLE_VALUE) | ||
352 : | { | ||
353 : | CloseHandle(codec->twopass.hints); | ||
354 : | } | ||
355 : | Isibaar | 3 | if (codec->twopass.stats1 != INVALID_HANDLE_VALUE) |
356 : | { | ||
357 : | CloseHandle(codec->twopass.stats1); | ||
358 : | } | ||
359 : | if (codec->twopass.stats2 != INVALID_HANDLE_VALUE) | ||
360 : | { | ||
361 : | CloseHandle(codec->twopass.stats2); | ||
362 : | } | ||
363 : | h | 102 | if (codec->twopass.hintstream != NULL) |
364 : | { | ||
365 : | free(codec->twopass.hintstream); | ||
366 : | } | ||
367 : | |||
368 : | Isibaar | 3 | codec->ehandle = NULL; |
369 : | h | 105 | |
370 : | codec_2pass_finish(codec); | ||
371 : | Isibaar | 3 | } |
372 : | |||
373 : | return ICERR_OK; | ||
374 : | } | ||
375 : | |||
376 : | |||
377 : | LRESULT compress(CODEC * codec, ICCOMPRESS * icc) | ||
378 : | { | ||
379 : | h | 30 | BITMAPINFOHEADER * inhdr = icc->lpbiInput; |
380 : | BITMAPINFOHEADER * outhdr = icc->lpbiOutput; | ||
381 : | Isibaar | 3 | XVID_ENC_FRAME frame; |
382 : | XVID_ENC_STATS stats; | ||
383 : | |||
384 : | // mpeg2avi yuv bug workaround (2 instances of CODEC) | ||
385 : | if (codec->twopass.stats1 == INVALID_HANDLE_VALUE) | ||
386 : | { | ||
387 : | if (codec_2pass_init(codec) == ICERR_ERROR) | ||
388 : | { | ||
389 : | return ICERR_ERROR; | ||
390 : | } | ||
391 : | } | ||
392 : | |||
393 : | frame.general = 0; | ||
394 : | frame.motion = 0; | ||
395 : | h | 30 | frame.intra = -1; |
396 : | Isibaar | 3 | |
397 : | frame.general |= XVID_HALFPEL; | ||
398 : | Isibaar | 364 | // frame.general |= XVID_ME_EPZS; |
399 : | Isibaar | 3 | |
400 : | Isibaar | 364 | |
401 : | h | 127 | if (codec->config.motion_search > 4) |
402 : | Isibaar | 3 | frame.general |= XVID_INTER4V; |
403 : | |||
404 : | h | 127 | if (codec->config.lum_masking) |
405 : | Isibaar | 3 | frame.general |= XVID_LUMIMASKING; |
406 : | |||
407 : | h | 102 | if (codec->config.interlacing) |
408 : | Isibaar | 364 | frame.general |= XVID_INTERLACING; |
409 : | chl | 355 | |
410 : | Isibaar | 364 | |
411 : | |||
412 : | // added by koepi for credits greyscale | ||
413 : | |||
414 : | check_greyscale_mode(&codec->config, &frame, codec->framenum); | ||
415 : | |||
416 : | // end of koepi's addition | ||
417 : | |||
418 : | |||
419 : | Isibaar | 176 | // fix 1pass modes/hinted MV by koepi |
420 : | if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_CBR || codec->config.mode == DLG_MODE_VBR_QUAL || codec->config.mode == DLG_MODE_VBR_QUANT)) | ||
421 : | { | ||
422 : | codec->config.hinted_me = 0; | ||
423 : | } | ||
424 : | // end of ugly hack | ||
425 : | h | 102 | |
426 : | if (codec->config.hinted_me && codec->config.mode == DLG_MODE_2PASS_1) | ||
427 : | { | ||
428 : | frame.hint.hintstream = codec->twopass.hintstream; | ||
429 : | frame.hint.rawhints = 0; | ||
430 : | frame.general |= XVID_HINTEDME_GET; | ||
431 : | } | ||
432 : | h | 103 | else if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT)) |
433 : | h | 102 | { |
434 : | DWORD read; | ||
435 : | DWORD blocksize; | ||
436 : | |||
437 : | frame.hint.hintstream = codec->twopass.hintstream; | ||
438 : | frame.hint.rawhints = 0; | ||
439 : | frame.general |= XVID_HINTEDME_SET; | ||
440 : | |||
441 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
442 : | { | ||
443 : | codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); | ||
444 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
445 : | { | ||
446 : | DEBUGERR("couldn't open hints file"); | ||
447 : | return ICERR_ERROR; | ||
448 : | } | ||
449 : | } | ||
450 : | if (!ReadFile(codec->twopass.hints, &blocksize, sizeof(DWORD), &read, 0) || read != sizeof(DWORD) || | ||
451 : | !ReadFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &read, 0) || read != blocksize) | ||
452 : | { | ||
453 : | DEBUGERR("couldn't read from hints file"); | ||
454 : | return ICERR_ERROR; | ||
455 : | } | ||
456 : | } | ||
457 : | |||
458 : | Isibaar | 3 | frame.motion = pmvfast_presets[codec->config.motion_search]; |
459 : | h | 30 | |
460 : | Isibaar | 3 | frame.image = icc->lpInput; |
461 : | |||
462 : | h | 30 | if ((frame.colorspace = get_colorspace(inhdr)) == XVID_CSP_NULL) |
463 : | Isibaar | 3 | return ICERR_BADFORMAT; |
464 : | |||
465 : | frame.bitstream = icc->lpOutput; | ||
466 : | frame.length = icc->lpbiOutput->biSizeImage; | ||
467 : | |||
468 : | switch (codec->config.mode) | ||
469 : | { | ||
470 : | case DLG_MODE_CBR : | ||
471 : | frame.quant = 0; | ||
472 : | break; | ||
473 : | |||
474 : | case DLG_MODE_VBR_QUAL : | ||
475 : | case DLG_MODE_VBR_QUANT : | ||
476 : | case DLG_MODE_2PASS_1 : | ||
477 : | if (codec_get_quant(codec, &frame) == ICERR_ERROR) | ||
478 : | { | ||
479 : | return ICERR_ERROR; | ||
480 : | } | ||
481 : | break; | ||
482 : | |||
483 : | case DLG_MODE_2PASS_2_EXT : | ||
484 : | case DLG_MODE_2PASS_2_INT : | ||
485 : | if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR) | ||
486 : | { | ||
487 : | return ICERR_ERROR; | ||
488 : | } | ||
489 : | if (codec->config.dummy2pass) | ||
490 : | { | ||
491 : | h | 30 | outhdr->biSizeImage = codec->twopass.bytes2; |
492 : | Isibaar | 3 | *icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0; |
493 : | return ICERR_OK; | ||
494 : | } | ||
495 : | break; | ||
496 : | |||
497 : | case DLG_MODE_NULL : | ||
498 : | h | 30 | outhdr->biSizeImage = 0; |
499 : | Isibaar | 3 | *icc->lpdwFlags = AVIIF_KEYFRAME; |
500 : | return ICERR_OK; | ||
501 : | |||
502 : | default : | ||
503 : | h | 61 | DEBUGERR("Invalid encoding mode"); |
504 : | Isibaar | 3 | return ICERR_ERROR; |
505 : | } | ||
506 : | |||
507 : | h | 30 | if (codec->config.quant_type == QUANT_MODE_H263) |
508 : | Isibaar | 3 | { |
509 : | h | 30 | frame.general |= XVID_H263QUANT; |
510 : | } | ||
511 : | else | ||
512 : | { | ||
513 : | frame.general |= XVID_MPEGQUANT; | ||
514 : | |||
515 : | // we actually need "default/custom" selectbox for both inter/intra | ||
516 : | // this will do for now | ||
517 : | if (codec->config.quant_type == QUANT_MODE_CUSTOM) | ||
518 : | { | ||
519 : | frame.general |= XVID_CUSTOM_QMATRIX; | ||
520 : | frame.quant_intra_matrix = codec->config.qmatrix_intra; | ||
521 : | frame.quant_inter_matrix = codec->config.qmatrix_inter; | ||
522 : | } | ||
523 : | else | ||
524 : | { | ||
525 : | frame.quant_intra_matrix = NULL; | ||
526 : | frame.quant_inter_matrix = NULL; | ||
527 : | } | ||
528 : | } | ||
529 : | |||
530 : | // force keyframe spacing in 2-pass 1st pass | ||
531 : | if (codec->config.motion_search == 0) | ||
532 : | { | ||
533 : | frame.intra = 1; | ||
534 : | } | ||
535 : | h | 109 | else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) |
536 : | h | 30 | { |
537 : | Isibaar | 3 | DEBUG("current frame forced to p-frame"); |
538 : | frame.intra = 0; | ||
539 : | } | ||
540 : | |||
541 : | suxen_drol | 278 | #ifdef BFRAMES |
542 : | frame.bquant = 0; | ||
543 : | #endif | ||
544 : | |||
545 : | suxen_drol | 324 | // OutputDebugString(" "); |
546 : | Isibaar | 3 | switch (xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats)) |
547 : | { | ||
548 : | case XVID_ERR_FAIL : | ||
549 : | return ICERR_ERROR; | ||
550 : | |||
551 : | case XVID_ERR_MEMORY : | ||
552 : | return ICERR_MEMORY; | ||
553 : | |||
554 : | case XVID_ERR_FORMAT : | ||
555 : | return ICERR_BADFORMAT; | ||
556 : | } | ||
557 : | |||
558 : | if (frame.intra) | ||
559 : | { | ||
560 : | codec->keyspacing = 0; | ||
561 : | *icc->lpdwFlags = AVIIF_KEYFRAME; | ||
562 : | } | ||
563 : | else | ||
564 : | { | ||
565 : | *icc->lpdwFlags = 0; | ||
566 : | } | ||
567 : | |||
568 : | h | 30 | outhdr->biSizeImage = frame.length; |
569 : | Isibaar | 3 | |
570 : | h | 30 | if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass) |
571 : | Isibaar | 3 | { |
572 : | h | 30 | outhdr->biSizeImage = 0; |
573 : | Isibaar | 3 | } |
574 : | |||
575 : | h | 102 | if (frame.general & XVID_HINTEDME_GET) |
576 : | { | ||
577 : | DWORD wrote; | ||
578 : | DWORD blocksize = frame.hint.hintlength; | ||
579 : | |||
580 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
581 : | { | ||
582 : | codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); | ||
583 : | suxen_drol | 228 | if (codec->twopass.hints == INVALID_HANDLE_VALUE) |
584 : | h | 102 | { |
585 : | DEBUGERR("couldn't create hints file"); | ||
586 : | return ICERR_ERROR; | ||
587 : | } | ||
588 : | } | ||
589 : | if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) || | ||
590 : | !WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize) | ||
591 : | { | ||
592 : | DEBUGERR("couldn't write to hints file"); | ||
593 : | return ICERR_ERROR; | ||
594 : | } | ||
595 : | } | ||
596 : | |||
597 : | Isibaar | 3 | codec_2pass_update(codec, &frame, &stats); |
598 : | |||
599 : | ++codec->framenum; | ||
600 : | ++codec->keyspacing; | ||
601 : | |||
602 : | return ICERR_OK; | ||
603 : | } | ||
604 : | |||
605 : | |||
606 : | /* decompressor */ | ||
607 : | |||
608 : | |||
609 : | LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) | ||
610 : | { | ||
611 : | h | 30 | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
612 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
613 : | Isibaar | 3 | |
614 : | if (lpbiInput == NULL) | ||
615 : | { | ||
616 : | return ICERR_ERROR; | ||
617 : | } | ||
618 : | |||
619 : | h | 30 | if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX) |
620 : | Isibaar | 3 | { |
621 : | return ICERR_BADFORMAT; | ||
622 : | } | ||
623 : | |||
624 : | if (lpbiOutput == NULL) | ||
625 : | { | ||
626 : | return ICERR_OK; | ||
627 : | } | ||
628 : | |||
629 : | h | 30 | if (inhdr->biWidth != outhdr->biWidth || |
630 : | inhdr->biHeight != outhdr->biHeight || | ||
631 : | Isibaar | 3 | get_colorspace(outhdr) == XVID_CSP_NULL) |
632 : | { | ||
633 : | return ICERR_BADFORMAT; | ||
634 : | } | ||
635 : | |||
636 : | return ICERR_OK; | ||
637 : | } | ||
638 : | |||
639 : | |||
640 : | LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
641 : | { | ||
642 : | h | 30 | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
643 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
644 : | Isibaar | 3 | LRESULT result; |
645 : | |||
646 : | if (lpbiOutput == NULL) | ||
647 : | { | ||
648 : | h | 30 | return sizeof(BITMAPINFOHEADER); |
649 : | Isibaar | 3 | } |
650 : | |||
651 : | result = decompress_query(codec, lpbiInput, lpbiOutput); | ||
652 : | if (result != ICERR_OK) | ||
653 : | { | ||
654 : | return result; | ||
655 : | } | ||
656 : | |||
657 : | h | 30 | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
658 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
659 : | outhdr->biCompression = FOURCC_YUY2; | ||
660 : | outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount; | ||
661 : | outhdr->biXPelsPerMeter = 0; | ||
662 : | outhdr->biYPelsPerMeter = 0; | ||
663 : | outhdr->biClrUsed = 0; | ||
664 : | outhdr->biClrImportant = 0; | ||
665 : | Isibaar | 3 | |
666 : | return ICERR_OK; | ||
667 : | } | ||
668 : | |||
669 : | |||
670 : | LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
671 : | { | ||
672 : | XVID_DEC_PARAM param; | ||
673 : | XVID_INIT_PARAM init_param; | ||
674 : | |||
675 : | init_param.cpu_flags = codec->config.cpu; | ||
676 : | xvid_init(0, 0, &init_param, NULL); | ||
677 : | if((codec->config.cpu & XVID_CPU_FORCE) <= 0) | ||
678 : | { | ||
679 : | codec->config.cpu = init_param.cpu_flags; | ||
680 : | } | ||
681 : | |||
682 : | h | 30 | param.width = lpbiInput->bmiHeader.biWidth; |
683 : | param.height = lpbiInput->bmiHeader.biHeight; | ||
684 : | Isibaar | 3 | |
685 : | switch(xvid_decore(0, XVID_DEC_CREATE, ¶m, NULL)) | ||
686 : | { | ||
687 : | case XVID_ERR_FAIL : | ||
688 : | return ICERR_ERROR; | ||
689 : | |||
690 : | case XVID_ERR_MEMORY : | ||
691 : | return ICERR_MEMORY; | ||
692 : | |||
693 : | case XVID_ERR_FORMAT : | ||
694 : | return ICERR_BADFORMAT; | ||
695 : | } | ||
696 : | |||
697 : | codec->dhandle = param.handle; | ||
698 : | |||
699 : | return ICERR_OK; | ||
700 : | } | ||
701 : | |||
702 : | |||
703 : | LRESULT decompress_end(CODEC * codec) | ||
704 : | { | ||
705 : | if (codec->dhandle != NULL) | ||
706 : | { | ||
707 : | xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); | ||
708 : | codec->dhandle = NULL; | ||
709 : | } | ||
710 : | return ICERR_OK; | ||
711 : | } | ||
712 : | |||
713 : | |||
714 : | LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd) | ||
715 : | { | ||
716 : | XVID_DEC_FRAME frame; | ||
717 : | |||
718 : | frame.bitstream = icd->lpInput; | ||
719 : | frame.length = icd->lpbiInput->biSizeImage; | ||
720 : | |||
721 : | frame.image = icd->lpOutput; | ||
722 : | frame.stride = icd->lpbiOutput->biWidth; | ||
723 : | |||
724 : | suxen_drol | 137 | if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL))) |
725 : | Isibaar | 3 | { |
726 : | h | 30 | if ((frame.colorspace = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL) |
727 : | Isibaar | 3 | { |
728 : | return ICERR_BADFORMAT; | ||
729 : | } | ||
730 : | } | ||
731 : | else | ||
732 : | { | ||
733 : | frame.colorspace = XVID_CSP_NULL; | ||
734 : | } | ||
735 : | |||
736 : | switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) | ||
737 : | { | ||
738 : | case XVID_ERR_FAIL : | ||
739 : | return ICERR_ERROR; | ||
740 : | |||
741 : | case XVID_ERR_MEMORY : | ||
742 : | return ICERR_MEMORY; | ||
743 : | |||
744 : | case XVID_ERR_FORMAT : | ||
745 : | return ICERR_BADFORMAT; | ||
746 : | } | ||
747 : | |||
748 : | return ICERR_OK; | ||
749 : | } | ||
750 : | |||
751 : | int codec_get_quant(CODEC* codec, XVID_ENC_FRAME* frame) | ||
752 : | { | ||
753 : | switch (codec->config.mode) | ||
754 : | { | ||
755 : | case DLG_MODE_VBR_QUAL : | ||
756 : | if (codec_is_in_credits(&codec->config, codec->framenum)) | ||
757 : | { | ||
758 : | Isibaar | 364 | // added by koepi for credits greyscale |
759 : | |||
760 : | check_greyscale_mode(&codec->config, frame, codec->framenum); | ||
761 : | |||
762 : | // end of koepi's addition | ||
763 : | |||
764 : | Isibaar | 3 | switch (codec->config.credits_mode) |
765 : | { | ||
766 : | case CREDITS_MODE_RATE : | ||
767 : | frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100); | ||
768 : | break; | ||
769 : | |||
770 : | case CREDITS_MODE_QUANT : | ||
771 : | frame->quant = codec->config.credits_quant_p; | ||
772 : | break; | ||
773 : | |||
774 : | default : | ||
775 : | h | 61 | DEBUGERR("Can't use credits size mode in quality mode"); |
776 : | Isibaar | 3 | return ICERR_ERROR; |
777 : | } | ||
778 : | } | ||
779 : | else | ||
780 : | { | ||
781 : | Isibaar | 364 | // added by koepi for credits greyscale |
782 : | |||
783 : | check_greyscale_mode(&codec->config, frame, codec->framenum); | ||
784 : | |||
785 : | // end of koepi's addition | ||
786 : | |||
787 : | Isibaar | 3 | frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality); |
788 : | } | ||
789 : | return ICERR_OK; | ||
790 : | |||
791 : | case DLG_MODE_VBR_QUANT : | ||
792 : | if (codec_is_in_credits(&codec->config, codec->framenum)) | ||
793 : | { | ||
794 : | Isibaar | 364 | // added by koepi for credits greyscale |
795 : | |||
796 : | check_greyscale_mode(&codec->config, frame, codec->framenum); | ||
797 : | |||
798 : | // end of koepi's addition | ||
799 : | |||
800 : | Isibaar | 3 | switch (codec->config.credits_mode) |
801 : | { | ||
802 : | case CREDITS_MODE_RATE : | ||
803 : | frame->quant = | ||
804 : | h | 30 | codec->config.max_pquant - |
805 : | ((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100); | ||
806 : | Isibaar | 3 | break; |
807 : | |||
808 : | case CREDITS_MODE_QUANT : | ||
809 : | frame->quant = codec->config.credits_quant_p; | ||
810 : | break; | ||
811 : | |||
812 : | default : | ||
813 : | h | 61 | DEBUGERR("Can't use credits size mode in quantizer mode"); |
814 : | Isibaar | 3 | return ICERR_ERROR; |
815 : | } | ||
816 : | } | ||
817 : | else | ||
818 : | { | ||
819 : | Isibaar | 364 | // added by koepi for credits greyscale |
820 : | |||
821 : | check_greyscale_mode(&codec->config, frame, codec->framenum); | ||
822 : | |||
823 : | // end of koepi's addition | ||
824 : | |||
825 : | Isibaar | 3 | frame->quant = codec->config.quant; |
826 : | } | ||
827 : | return ICERR_OK; | ||
828 : | |||
829 : | case DLG_MODE_2PASS_1 : | ||
830 : | Isibaar | 364 | // added by koepi for credits greyscale |
831 : | |||
832 : | check_greyscale_mode(&codec->config, frame, codec->framenum); | ||
833 : | |||
834 : | // end of koepi's addition | ||
835 : | |||
836 : | Isibaar | 3 | if (codec->config.credits_mode == CREDITS_MODE_QUANT) |
837 : | { | ||
838 : | if (codec_is_in_credits(&codec->config, codec->framenum)) | ||
839 : | { | ||
840 : | frame->quant = codec->config.credits_quant_p; | ||
841 : | } | ||
842 : | else | ||
843 : | { | ||
844 : | frame->quant = 2; | ||
845 : | } | ||
846 : | } | ||
847 : | else | ||
848 : | { | ||
849 : | frame->quant = 2; | ||
850 : | } | ||
851 : | return ICERR_OK; | ||
852 : | |||
853 : | default: | ||
854 : | h | 61 | DEBUGERR("get quant: invalid mode"); |
855 : | Isibaar | 3 | return ICERR_ERROR; |
856 : | } | ||
857 : | } | ||
858 : | |||
859 : | |||
860 : | int codec_is_in_credits(CONFIG* config, int framenum) | ||
861 : | { | ||
862 : | if (config->credits_start) | ||
863 : | { | ||
864 : | if (framenum >= config->credits_start_begin && | ||
865 : | framenum <= config->credits_start_end) | ||
866 : | { | ||
867 : | return CREDITS_START; | ||
868 : | } | ||
869 : | } | ||
870 : | |||
871 : | if (config->credits_end) | ||
872 : | { | ||
873 : | if (framenum >= config->credits_end_begin && | ||
874 : | framenum <= config->credits_end_end) | ||
875 : | { | ||
876 : | return CREDITS_END; | ||
877 : | } | ||
878 : | } | ||
879 : | |||
880 : | return 0; | ||
881 : | } | ||
882 : | |||
883 : | |||
884 : | int codec_get_vbr_quant(CONFIG* config, int quality) | ||
885 : | { | ||
886 : | static float fquant_running = 0; | ||
887 : | static int my_quality = -1; | ||
888 : | int quant; | ||
889 : | |||
890 : | // if quality changes, recalculate fquant (credits) | ||
891 : | if (quality != my_quality) | ||
892 : | { | ||
893 : | config->fquant = 0; | ||
894 : | } | ||
895 : | |||
896 : | my_quality = quality; | ||
897 : | |||
898 : | // desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ | ||
899 : | if (!config->fquant) | ||
900 : | { | ||
901 : | config->fquant = | ||
902 : | h | 30 | ((float) (config->max_pquant - config->min_pquant) / 100) * |
903 : | Isibaar | 3 | (100 - quality) + |
904 : | h | 30 | (float) config->min_pquant; |
905 : | Isibaar | 3 | |
906 : | fquant_running = config->fquant; | ||
907 : | } | ||
908 : | |||
909 : | h | 30 | if (fquant_running < config->min_pquant) |
910 : | Isibaar | 3 | { |
911 : | h | 30 | fquant_running = (float) config->min_pquant; |
912 : | Isibaar | 3 | } |
913 : | h | 30 | else if(fquant_running > config->max_pquant) |
914 : | Isibaar | 3 | { |
915 : | h | 30 | fquant_running = (float) config->max_pquant; |
916 : | Isibaar | 3 | } |
917 : | |||
918 : | quant = (int) fquant_running; | ||
919 : | |||
920 : | // add error between fquant and quant to fquant_running | ||
921 : | fquant_running += config->fquant - quant; | ||
922 : | |||
923 : | return quant; | ||
924 : | } | ||
925 : | |||
926 : | Isibaar | 364 | // added by koepi for credits greyscale |
927 : | |||
928 : | int check_greyscale_mode(CONFIG* config, XVID_ENC_FRAME* frame, int framenum) | ||
929 : | |||
930 : | { | ||
931 : | |||
932 : | if ((codec_is_in_credits(config, framenum)) && (config->mode!=DLG_MODE_CBR)) | ||
933 : | |||
934 : | { | ||
935 : | |||
936 : | if (config->credits_greyscale) | ||
937 : | |||
938 : | { | ||
939 : | |||
940 : | if ((frame->general && XVID_GREYSCALE)) // use only if not already in greyscale | ||
941 : | |||
942 : | frame->general |= XVID_GREYSCALE; | ||
943 : | |||
944 : | } else { | ||
945 : | |||
946 : | if (!(frame->general && XVID_GREYSCALE)) // if movie is in greyscale, switch back | ||
947 : | |||
948 : | frame->general |= XVID_GREYSCALE; | ||
949 : | |||
950 : | } | ||
951 : | |||
952 : | } else { | ||
953 : | |||
954 : | if (config->greyscale) | ||
955 : | |||
956 : | { | ||
957 : | |||
958 : | if ((frame->general && XVID_GREYSCALE)) // use only if not already in greyscale | ||
959 : | |||
960 : | frame->general |= XVID_GREYSCALE; | ||
961 : | |||
962 : | } else { | ||
963 : | |||
964 : | if (!(frame->general && XVID_GREYSCALE)) // if credits is in greyscale, switch back | ||
965 : | |||
966 : | frame->general |= XVID_GREYSCALE; | ||
967 : | |||
968 : | } | ||
969 : | |||
970 : | } | ||
971 : | |||
972 : | return 0; | ||
973 : | |||
974 : | } | ||
975 : | |||
976 : | // end of koepi's addition | ||
977 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |