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