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

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