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

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