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

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