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