[svn] / trunk / vfw / src / codec.c Repository:
ViewVC logotype

Annotation of /trunk/vfw/src/codec.c

Parent Directory Parent Directory | Revision Log Revision Log


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

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4