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