Parent Directory
|
Revision Log
Revision 105 - (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 : | h | 105 | |
336 : | codec_2pass_finish(codec); | ||
337 : | Isibaar | 3 | } |
338 : | |||
339 : | return ICERR_OK; | ||
340 : | } | ||
341 : | |||
342 : | |||
343 : | LRESULT compress(CODEC * codec, ICCOMPRESS * icc) | ||
344 : | { | ||
345 : | h | 30 | BITMAPINFOHEADER * inhdr = icc->lpbiInput; |
346 : | BITMAPINFOHEADER * outhdr = icc->lpbiOutput; | ||
347 : | Isibaar | 3 | XVID_ENC_FRAME frame; |
348 : | XVID_ENC_STATS stats; | ||
349 : | |||
350 : | // mpeg2avi yuv bug workaround (2 instances of CODEC) | ||
351 : | if (codec->twopass.stats1 == INVALID_HANDLE_VALUE) | ||
352 : | { | ||
353 : | if (codec_2pass_init(codec) == ICERR_ERROR) | ||
354 : | { | ||
355 : | return ICERR_ERROR; | ||
356 : | } | ||
357 : | } | ||
358 : | |||
359 : | frame.general = 0; | ||
360 : | frame.motion = 0; | ||
361 : | h | 30 | frame.intra = -1; |
362 : | Isibaar | 3 | |
363 : | frame.general |= XVID_HALFPEL; | ||
364 : | |||
365 : | h | 61 | if(codec->config.motion_search > 4) |
366 : | Isibaar | 3 | frame.general |= XVID_INTER4V; |
367 : | |||
368 : | if(((codec->config.mode == DLG_MODE_2PASS_1) ? 0 : codec->config.lum_masking) == 1) | ||
369 : | frame.general |= XVID_LUMIMASKING; | ||
370 : | |||
371 : | h | 102 | if (codec->config.interlacing) |
372 : | frame.general |= XVID_INTERLACING; | ||
373 : | |||
374 : | if (codec->config.hinted_me && codec->config.mode == DLG_MODE_2PASS_1) | ||
375 : | { | ||
376 : | frame.hint.hintstream = codec->twopass.hintstream; | ||
377 : | frame.hint.rawhints = 0; | ||
378 : | frame.general |= XVID_HINTEDME_GET; | ||
379 : | } | ||
380 : | h | 103 | else if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT)) |
381 : | h | 102 | { |
382 : | DWORD read; | ||
383 : | DWORD blocksize; | ||
384 : | |||
385 : | frame.hint.hintstream = codec->twopass.hintstream; | ||
386 : | frame.hint.rawhints = 0; | ||
387 : | frame.general |= XVID_HINTEDME_SET; | ||
388 : | |||
389 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
390 : | { | ||
391 : | codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); | ||
392 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
393 : | { | ||
394 : | DEBUGERR("couldn't open hints file"); | ||
395 : | return ICERR_ERROR; | ||
396 : | } | ||
397 : | } | ||
398 : | if (!ReadFile(codec->twopass.hints, &blocksize, sizeof(DWORD), &read, 0) || read != sizeof(DWORD) || | ||
399 : | !ReadFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &read, 0) || read != blocksize) | ||
400 : | { | ||
401 : | DEBUGERR("couldn't read from hints file"); | ||
402 : | return ICERR_ERROR; | ||
403 : | } | ||
404 : | } | ||
405 : | |||
406 : | Isibaar | 3 | frame.motion = pmvfast_presets[codec->config.motion_search]; |
407 : | h | 30 | |
408 : | Isibaar | 3 | frame.image = icc->lpInput; |
409 : | |||
410 : | h | 30 | if ((frame.colorspace = get_colorspace(inhdr)) == XVID_CSP_NULL) |
411 : | Isibaar | 3 | return ICERR_BADFORMAT; |
412 : | |||
413 : | frame.bitstream = icc->lpOutput; | ||
414 : | frame.length = icc->lpbiOutput->biSizeImage; | ||
415 : | |||
416 : | switch (codec->config.mode) | ||
417 : | { | ||
418 : | case DLG_MODE_CBR : | ||
419 : | frame.quant = 0; | ||
420 : | break; | ||
421 : | |||
422 : | case DLG_MODE_VBR_QUAL : | ||
423 : | case DLG_MODE_VBR_QUANT : | ||
424 : | case DLG_MODE_2PASS_1 : | ||
425 : | if (codec_get_quant(codec, &frame) == ICERR_ERROR) | ||
426 : | { | ||
427 : | return ICERR_ERROR; | ||
428 : | } | ||
429 : | break; | ||
430 : | |||
431 : | case DLG_MODE_2PASS_2_EXT : | ||
432 : | case DLG_MODE_2PASS_2_INT : | ||
433 : | if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR) | ||
434 : | { | ||
435 : | return ICERR_ERROR; | ||
436 : | } | ||
437 : | if (codec->config.dummy2pass) | ||
438 : | { | ||
439 : | h | 30 | outhdr->biSizeImage = codec->twopass.bytes2; |
440 : | Isibaar | 3 | *icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0; |
441 : | return ICERR_OK; | ||
442 : | } | ||
443 : | break; | ||
444 : | |||
445 : | case DLG_MODE_NULL : | ||
446 : | h | 30 | outhdr->biSizeImage = 0; |
447 : | Isibaar | 3 | *icc->lpdwFlags = AVIIF_KEYFRAME; |
448 : | return ICERR_OK; | ||
449 : | |||
450 : | default : | ||
451 : | h | 61 | DEBUGERR("Invalid encoding mode"); |
452 : | Isibaar | 3 | return ICERR_ERROR; |
453 : | } | ||
454 : | |||
455 : | h | 30 | if (codec->config.quant_type == QUANT_MODE_H263) |
456 : | Isibaar | 3 | { |
457 : | h | 30 | frame.general |= XVID_H263QUANT; |
458 : | } | ||
459 : | else | ||
460 : | { | ||
461 : | frame.general |= XVID_MPEGQUANT; | ||
462 : | |||
463 : | // we actually need "default/custom" selectbox for both inter/intra | ||
464 : | // this will do for now | ||
465 : | if (codec->config.quant_type == QUANT_MODE_CUSTOM) | ||
466 : | { | ||
467 : | frame.general |= XVID_CUSTOM_QMATRIX; | ||
468 : | frame.quant_intra_matrix = codec->config.qmatrix_intra; | ||
469 : | frame.quant_inter_matrix = codec->config.qmatrix_inter; | ||
470 : | } | ||
471 : | else | ||
472 : | { | ||
473 : | frame.quant_intra_matrix = NULL; | ||
474 : | frame.quant_inter_matrix = NULL; | ||
475 : | } | ||
476 : | } | ||
477 : | |||
478 : | // force keyframe spacing in 2-pass 1st pass | ||
479 : | if (codec->config.motion_search == 0) | ||
480 : | { | ||
481 : | frame.intra = 1; | ||
482 : | } | ||
483 : | else if ((codec->keyspacing < codec->config.min_key_interval && codec->framenum) && | ||
484 : | h | 105 | (codec->config.mode == DLG_MODE_2PASS_1 || codec->config.mode == DLG_MODE_CBR || codec->config.mode == DLG_MODE_VBR_QUANT || |
485 : | codec->config.mode == DLG_MODE_VBR_QUAL || codec->config.mode == DLG_MODE_NULL)) | ||
486 : | h | 30 | { |
487 : | Isibaar | 3 | DEBUG("current frame forced to p-frame"); |
488 : | frame.intra = 0; | ||
489 : | } | ||
490 : | |||
491 : | switch (xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats)) | ||
492 : | { | ||
493 : | case XVID_ERR_FAIL : | ||
494 : | return ICERR_ERROR; | ||
495 : | |||
496 : | case XVID_ERR_MEMORY : | ||
497 : | return ICERR_MEMORY; | ||
498 : | |||
499 : | case XVID_ERR_FORMAT : | ||
500 : | return ICERR_BADFORMAT; | ||
501 : | } | ||
502 : | |||
503 : | if (frame.intra) | ||
504 : | { | ||
505 : | codec->keyspacing = 0; | ||
506 : | *icc->lpdwFlags = AVIIF_KEYFRAME; | ||
507 : | } | ||
508 : | else | ||
509 : | { | ||
510 : | *icc->lpdwFlags = 0; | ||
511 : | } | ||
512 : | |||
513 : | h | 30 | outhdr->biSizeImage = frame.length; |
514 : | Isibaar | 3 | |
515 : | h | 30 | if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass) |
516 : | Isibaar | 3 | { |
517 : | h | 30 | outhdr->biSizeImage = 0; |
518 : | Isibaar | 3 | } |
519 : | |||
520 : | h | 102 | if (frame.general & XVID_HINTEDME_GET) |
521 : | { | ||
522 : | DWORD wrote; | ||
523 : | DWORD blocksize = frame.hint.hintlength; | ||
524 : | |||
525 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
526 : | { | ||
527 : | codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); | ||
528 : | if (codec->twopass.hints == INVALID_HANDLE_VALUE) | ||
529 : | { | ||
530 : | DEBUGERR("couldn't create hints file"); | ||
531 : | return ICERR_ERROR; | ||
532 : | } | ||
533 : | } | ||
534 : | if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) || | ||
535 : | !WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize) | ||
536 : | { | ||
537 : | DEBUGERR("couldn't write to hints file"); | ||
538 : | return ICERR_ERROR; | ||
539 : | } | ||
540 : | } | ||
541 : | |||
542 : | Isibaar | 3 | codec_2pass_update(codec, &frame, &stats); |
543 : | |||
544 : | ++codec->framenum; | ||
545 : | ++codec->keyspacing; | ||
546 : | |||
547 : | return ICERR_OK; | ||
548 : | } | ||
549 : | |||
550 : | |||
551 : | /* decompressor */ | ||
552 : | |||
553 : | |||
554 : | LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) | ||
555 : | { | ||
556 : | h | 30 | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
557 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
558 : | Isibaar | 3 | |
559 : | if (lpbiInput == NULL) | ||
560 : | { | ||
561 : | return ICERR_ERROR; | ||
562 : | } | ||
563 : | |||
564 : | h | 30 | if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX) |
565 : | Isibaar | 3 | { |
566 : | return ICERR_BADFORMAT; | ||
567 : | } | ||
568 : | |||
569 : | if (lpbiOutput == NULL) | ||
570 : | { | ||
571 : | return ICERR_OK; | ||
572 : | } | ||
573 : | |||
574 : | h | 30 | if (inhdr->biWidth != outhdr->biWidth || |
575 : | inhdr->biHeight != outhdr->biHeight || | ||
576 : | Isibaar | 3 | get_colorspace(outhdr) == XVID_CSP_NULL) |
577 : | { | ||
578 : | return ICERR_BADFORMAT; | ||
579 : | } | ||
580 : | |||
581 : | return ICERR_OK; | ||
582 : | } | ||
583 : | |||
584 : | |||
585 : | LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
586 : | { | ||
587 : | h | 30 | BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
588 : | BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; | ||
589 : | Isibaar | 3 | LRESULT result; |
590 : | |||
591 : | if (lpbiOutput == NULL) | ||
592 : | { | ||
593 : | h | 30 | return sizeof(BITMAPINFOHEADER); |
594 : | Isibaar | 3 | } |
595 : | |||
596 : | result = decompress_query(codec, lpbiInput, lpbiOutput); | ||
597 : | if (result != ICERR_OK) | ||
598 : | { | ||
599 : | return result; | ||
600 : | } | ||
601 : | |||
602 : | h | 30 | memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
603 : | outhdr->biSize = sizeof(BITMAPINFOHEADER); | ||
604 : | outhdr->biCompression = FOURCC_YUY2; | ||
605 : | outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount; | ||
606 : | outhdr->biXPelsPerMeter = 0; | ||
607 : | outhdr->biYPelsPerMeter = 0; | ||
608 : | outhdr->biClrUsed = 0; | ||
609 : | outhdr->biClrImportant = 0; | ||
610 : | Isibaar | 3 | |
611 : | return ICERR_OK; | ||
612 : | } | ||
613 : | |||
614 : | |||
615 : | LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) | ||
616 : | { | ||
617 : | XVID_DEC_PARAM param; | ||
618 : | XVID_INIT_PARAM init_param; | ||
619 : | |||
620 : | init_param.cpu_flags = codec->config.cpu; | ||
621 : | xvid_init(0, 0, &init_param, NULL); | ||
622 : | if((codec->config.cpu & XVID_CPU_FORCE) <= 0) | ||
623 : | { | ||
624 : | codec->config.cpu = init_param.cpu_flags; | ||
625 : | } | ||
626 : | |||
627 : | h | 30 | param.width = lpbiInput->bmiHeader.biWidth; |
628 : | param.height = lpbiInput->bmiHeader.biHeight; | ||
629 : | Isibaar | 3 | |
630 : | switch(xvid_decore(0, XVID_DEC_CREATE, ¶m, NULL)) | ||
631 : | { | ||
632 : | case XVID_ERR_FAIL : | ||
633 : | return ICERR_ERROR; | ||
634 : | |||
635 : | case XVID_ERR_MEMORY : | ||
636 : | return ICERR_MEMORY; | ||
637 : | |||
638 : | case XVID_ERR_FORMAT : | ||
639 : | return ICERR_BADFORMAT; | ||
640 : | } | ||
641 : | |||
642 : | codec->dhandle = param.handle; | ||
643 : | |||
644 : | return ICERR_OK; | ||
645 : | } | ||
646 : | |||
647 : | |||
648 : | LRESULT decompress_end(CODEC * codec) | ||
649 : | { | ||
650 : | if (codec->dhandle != NULL) | ||
651 : | { | ||
652 : | xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); | ||
653 : | codec->dhandle = NULL; | ||
654 : | } | ||
655 : | return ICERR_OK; | ||
656 : | } | ||
657 : | |||
658 : | |||
659 : | LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd) | ||
660 : | { | ||
661 : | XVID_DEC_FRAME frame; | ||
662 : | |||
663 : | frame.bitstream = icd->lpInput; | ||
664 : | frame.length = icd->lpbiInput->biSizeImage; | ||
665 : | |||
666 : | frame.image = icd->lpOutput; | ||
667 : | frame.stride = icd->lpbiOutput->biWidth; | ||
668 : | |||
669 : | if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE))) | ||
670 : | { | ||
671 : | h | 30 | if ((frame.colorspace = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL) |
672 : | Isibaar | 3 | { |
673 : | return ICERR_BADFORMAT; | ||
674 : | } | ||
675 : | } | ||
676 : | else | ||
677 : | { | ||
678 : | frame.colorspace = XVID_CSP_NULL; | ||
679 : | } | ||
680 : | |||
681 : | switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) | ||
682 : | { | ||
683 : | case XVID_ERR_FAIL : | ||
684 : | return ICERR_ERROR; | ||
685 : | |||
686 : | case XVID_ERR_MEMORY : | ||
687 : | return ICERR_MEMORY; | ||
688 : | |||
689 : | case XVID_ERR_FORMAT : | ||
690 : | return ICERR_BADFORMAT; | ||
691 : | } | ||
692 : | |||
693 : | return ICERR_OK; | ||
694 : | } | ||
695 : | |||
696 : | int codec_get_quant(CODEC* codec, XVID_ENC_FRAME* frame) | ||
697 : | { | ||
698 : | switch (codec->config.mode) | ||
699 : | { | ||
700 : | case DLG_MODE_VBR_QUAL : | ||
701 : | if (codec_is_in_credits(&codec->config, codec->framenum)) | ||
702 : | { | ||
703 : | switch (codec->config.credits_mode) | ||
704 : | { | ||
705 : | case CREDITS_MODE_RATE : | ||
706 : | frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100); | ||
707 : | break; | ||
708 : | |||
709 : | case CREDITS_MODE_QUANT : | ||
710 : | frame->quant = codec->config.credits_quant_p; | ||
711 : | break; | ||
712 : | |||
713 : | default : | ||
714 : | h | 61 | DEBUGERR("Can't use credits size mode in quality mode"); |
715 : | Isibaar | 3 | return ICERR_ERROR; |
716 : | } | ||
717 : | } | ||
718 : | else | ||
719 : | { | ||
720 : | frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality); | ||
721 : | } | ||
722 : | return ICERR_OK; | ||
723 : | |||
724 : | case DLG_MODE_VBR_QUANT : | ||
725 : | if (codec_is_in_credits(&codec->config, codec->framenum)) | ||
726 : | { | ||
727 : | switch (codec->config.credits_mode) | ||
728 : | { | ||
729 : | case CREDITS_MODE_RATE : | ||
730 : | frame->quant = | ||
731 : | h | 30 | codec->config.max_pquant - |
732 : | ((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100); | ||
733 : | Isibaar | 3 | break; |
734 : | |||
735 : | case CREDITS_MODE_QUANT : | ||
736 : | frame->quant = codec->config.credits_quant_p; | ||
737 : | break; | ||
738 : | |||
739 : | default : | ||
740 : | h | 61 | DEBUGERR("Can't use credits size mode in quantizer mode"); |
741 : | Isibaar | 3 | return ICERR_ERROR; |
742 : | } | ||
743 : | } | ||
744 : | else | ||
745 : | { | ||
746 : | frame->quant = codec->config.quant; | ||
747 : | } | ||
748 : | return ICERR_OK; | ||
749 : | |||
750 : | case DLG_MODE_2PASS_1 : | ||
751 : | if (codec->config.credits_mode == CREDITS_MODE_QUANT) | ||
752 : | { | ||
753 : | if (codec_is_in_credits(&codec->config, codec->framenum)) | ||
754 : | { | ||
755 : | frame->quant = codec->config.credits_quant_p; | ||
756 : | } | ||
757 : | else | ||
758 : | { | ||
759 : | frame->quant = 2; | ||
760 : | } | ||
761 : | } | ||
762 : | else | ||
763 : | { | ||
764 : | frame->quant = 2; | ||
765 : | } | ||
766 : | return ICERR_OK; | ||
767 : | |||
768 : | default: | ||
769 : | h | 61 | DEBUGERR("get quant: invalid mode"); |
770 : | Isibaar | 3 | return ICERR_ERROR; |
771 : | } | ||
772 : | } | ||
773 : | |||
774 : | |||
775 : | int codec_is_in_credits(CONFIG* config, int framenum) | ||
776 : | { | ||
777 : | if (config->credits_start) | ||
778 : | { | ||
779 : | if (framenum >= config->credits_start_begin && | ||
780 : | framenum <= config->credits_start_end) | ||
781 : | { | ||
782 : | return CREDITS_START; | ||
783 : | } | ||
784 : | } | ||
785 : | |||
786 : | if (config->credits_end) | ||
787 : | { | ||
788 : | if (framenum >= config->credits_end_begin && | ||
789 : | framenum <= config->credits_end_end) | ||
790 : | { | ||
791 : | return CREDITS_END; | ||
792 : | } | ||
793 : | } | ||
794 : | |||
795 : | return 0; | ||
796 : | } | ||
797 : | |||
798 : | |||
799 : | int codec_get_vbr_quant(CONFIG* config, int quality) | ||
800 : | { | ||
801 : | static float fquant_running = 0; | ||
802 : | static int my_quality = -1; | ||
803 : | int quant; | ||
804 : | |||
805 : | // if quality changes, recalculate fquant (credits) | ||
806 : | if (quality != my_quality) | ||
807 : | { | ||
808 : | config->fquant = 0; | ||
809 : | } | ||
810 : | |||
811 : | my_quality = quality; | ||
812 : | |||
813 : | // desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ | ||
814 : | if (!config->fquant) | ||
815 : | { | ||
816 : | config->fquant = | ||
817 : | h | 30 | ((float) (config->max_pquant - config->min_pquant) / 100) * |
818 : | Isibaar | 3 | (100 - quality) + |
819 : | h | 30 | (float) config->min_pquant; |
820 : | Isibaar | 3 | |
821 : | fquant_running = config->fquant; | ||
822 : | } | ||
823 : | |||
824 : | h | 30 | if (fquant_running < config->min_pquant) |
825 : | Isibaar | 3 | { |
826 : | h | 30 | fquant_running = (float) config->min_pquant; |
827 : | Isibaar | 3 | } |
828 : | h | 30 | else if(fquant_running > config->max_pquant) |
829 : | Isibaar | 3 | { |
830 : | h | 30 | fquant_running = (float) config->max_pquant; |
831 : | Isibaar | 3 | } |
832 : | |||
833 : | quant = (int) fquant_running; | ||
834 : | |||
835 : | // add error between fquant and quant to fquant_running | ||
836 : | fquant_running += config->fquant - quant; | ||
837 : | |||
838 : | return quant; | ||
839 : | } | ||
840 : |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |