[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 176 - (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 : h 30 return lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3;
222 : Isibaar 3 }
223 :    
224 :    
225 :     LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf)
226 :     {
227 :     // DEBUG2("frate fscale", codec->frate, codec->fscale);
228 :     codec->fincr = icf->dwScale;
229 :     codec->fbase = icf->dwRate;
230 :     return ICERR_OK;
231 :     }
232 :    
233 :    
234 :     LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
235 :     {
236 :     XVID_ENC_PARAM param;
237 :     XVID_INIT_PARAM init_param;
238 :    
239 :     switch (codec->config.mode)
240 :     {
241 :     case DLG_MODE_CBR :
242 : h 121 param.rc_bitrate = codec->config.rc_bitrate;
243 :     param.rc_reaction_delay_factor = codec->config.rc_reaction_delay_factor;
244 :     param.rc_averaging_period = codec->config.rc_averaging_period;
245 :     param.rc_buffer = codec->config.rc_buffer;
246 : Isibaar 3 break;
247 :    
248 :     case DLG_MODE_VBR_QUAL :
249 :     codec->config.fquant = 0;
250 : h 121 param.rc_bitrate = 0;
251 : Isibaar 3 break;
252 :    
253 :     case DLG_MODE_VBR_QUANT :
254 :     codec->config.fquant = (float) codec->config.quant;
255 : h 121 param.rc_bitrate = 0;
256 : Isibaar 3 break;
257 :    
258 : h 61 case DLG_MODE_2PASS_1 :
259 :     case DLG_MODE_2PASS_2_INT :
260 :     case DLG_MODE_2PASS_2_EXT :
261 : h 121 param.rc_bitrate = 0;
262 : h 109 codec->twopass.max_framesize = (int)((double)codec->config.twopass_max_bitrate / 8.0 / ((double)codec->fbase / (double)codec->fincr));
263 : Isibaar 3 break;
264 :    
265 : h 61 case DLG_MODE_NULL :
266 :     return ICERR_OK;
267 :    
268 : Isibaar 3 default :
269 :     break;
270 :     }
271 :    
272 :     if(codec->ehandle)
273 :     {
274 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
275 :     codec->ehandle = NULL;
276 :     }
277 :    
278 :     init_param.cpu_flags = codec->config.cpu;
279 :     xvid_init(0, 0, &init_param, NULL);
280 :     if((codec->config.cpu & XVID_CPU_FORCE) <= 0)
281 :     codec->config.cpu = init_param.cpu_flags;
282 :    
283 : h 30 param.width = lpbiInput->bmiHeader.biWidth;
284 :     param.height = lpbiInput->bmiHeader.biHeight;
285 : Isibaar 3 param.fincr = codec->fincr;
286 :     param.fbase = codec->fbase;
287 :    
288 : h 30 param.min_quantizer = codec->config.min_pquant;
289 :     param.max_quantizer = codec->config.max_pquant;
290 : Isibaar 3 param.max_key_interval = codec->config.max_key_interval;
291 :    
292 :     switch(xvid_encore(0, XVID_ENC_CREATE, &param, NULL))
293 :     {
294 :     case XVID_ERR_FAIL :
295 :     return ICERR_ERROR;
296 :    
297 :     case XVID_ERR_MEMORY :
298 :     return ICERR_MEMORY;
299 :    
300 :     case XVID_ERR_FORMAT :
301 :     return ICERR_BADFORMAT;
302 :     }
303 :    
304 :     codec->ehandle = param.handle;
305 :     codec->framenum = 0;
306 :     codec->keyspacing = 0;
307 :    
308 : h 102 codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE;
309 :     codec->twopass.hintstream = NULL;
310 : Isibaar 3
311 :     return ICERR_OK;
312 :     }
313 :    
314 :    
315 :     LRESULT compress_end(CODEC * codec)
316 :     {
317 :     if (codec->ehandle != NULL)
318 :     {
319 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
320 : h 102
321 :     if (codec->twopass.hints != INVALID_HANDLE_VALUE)
322 :     {
323 :     CloseHandle(codec->twopass.hints);
324 :     }
325 : Isibaar 3 if (codec->twopass.stats1 != INVALID_HANDLE_VALUE)
326 :     {
327 :     CloseHandle(codec->twopass.stats1);
328 :     }
329 :     if (codec->twopass.stats2 != INVALID_HANDLE_VALUE)
330 :     {
331 :     CloseHandle(codec->twopass.stats2);
332 :     }
333 : h 102 if (codec->twopass.hintstream != NULL)
334 :     {
335 :     free(codec->twopass.hintstream);
336 :     }
337 :    
338 : Isibaar 3 codec->ehandle = NULL;
339 : h 105
340 :     codec_2pass_finish(codec);
341 : Isibaar 3 }
342 :    
343 :     return ICERR_OK;
344 :     }
345 :    
346 :    
347 :     LRESULT compress(CODEC * codec, ICCOMPRESS * icc)
348 :     {
349 : h 30 BITMAPINFOHEADER * inhdr = icc->lpbiInput;
350 :     BITMAPINFOHEADER * outhdr = icc->lpbiOutput;
351 : Isibaar 3 XVID_ENC_FRAME frame;
352 :     XVID_ENC_STATS stats;
353 :    
354 :     // mpeg2avi yuv bug workaround (2 instances of CODEC)
355 :     if (codec->twopass.stats1 == INVALID_HANDLE_VALUE)
356 :     {
357 :     if (codec_2pass_init(codec) == ICERR_ERROR)
358 :     {
359 :     return ICERR_ERROR;
360 :     }
361 :     }
362 :    
363 :     frame.general = 0;
364 :     frame.motion = 0;
365 : h 30 frame.intra = -1;
366 : Isibaar 3
367 :     frame.general |= XVID_HALFPEL;
368 :    
369 : h 127 if (codec->config.motion_search > 4)
370 : Isibaar 3 frame.general |= XVID_INTER4V;
371 :    
372 : h 127 if (codec->config.lum_masking)
373 : Isibaar 3 frame.general |= XVID_LUMIMASKING;
374 :    
375 : h 102 if (codec->config.interlacing)
376 :     frame.general |= XVID_INTERLACING;
377 : Isibaar 176 // fix 1pass modes/hinted MV by koepi
378 :     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))
379 :     {
380 :     codec->config.hinted_me = 0;
381 :     }
382 :     // end of ugly hack
383 : h 102
384 :     if (codec->config.hinted_me && codec->config.mode == DLG_MODE_2PASS_1)
385 :     {
386 :     frame.hint.hintstream = codec->twopass.hintstream;
387 :     frame.hint.rawhints = 0;
388 :     frame.general |= XVID_HINTEDME_GET;
389 :     }
390 : h 103 else if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT))
391 : h 102 {
392 :     DWORD read;
393 :     DWORD blocksize;
394 :    
395 :     frame.hint.hintstream = codec->twopass.hintstream;
396 :     frame.hint.rawhints = 0;
397 :     frame.general |= XVID_HINTEDME_SET;
398 :    
399 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
400 :     {
401 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
402 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
403 :     {
404 :     DEBUGERR("couldn't open hints file");
405 :     return ICERR_ERROR;
406 :     }
407 :     }
408 :     if (!ReadFile(codec->twopass.hints, &blocksize, sizeof(DWORD), &read, 0) || read != sizeof(DWORD) ||
409 :     !ReadFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &read, 0) || read != blocksize)
410 :     {
411 :     DEBUGERR("couldn't read from hints file");
412 :     return ICERR_ERROR;
413 :     }
414 :     }
415 :    
416 : Isibaar 3 frame.motion = pmvfast_presets[codec->config.motion_search];
417 : h 30
418 : Isibaar 3 frame.image = icc->lpInput;
419 :    
420 : h 30 if ((frame.colorspace = get_colorspace(inhdr)) == XVID_CSP_NULL)
421 : Isibaar 3 return ICERR_BADFORMAT;
422 :    
423 :     frame.bitstream = icc->lpOutput;
424 :     frame.length = icc->lpbiOutput->biSizeImage;
425 :    
426 :     switch (codec->config.mode)
427 :     {
428 :     case DLG_MODE_CBR :
429 :     frame.quant = 0;
430 :     break;
431 :    
432 :     case DLG_MODE_VBR_QUAL :
433 :     case DLG_MODE_VBR_QUANT :
434 :     case DLG_MODE_2PASS_1 :
435 :     if (codec_get_quant(codec, &frame) == ICERR_ERROR)
436 :     {
437 :     return ICERR_ERROR;
438 :     }
439 :     break;
440 :    
441 :     case DLG_MODE_2PASS_2_EXT :
442 :     case DLG_MODE_2PASS_2_INT :
443 :     if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR)
444 :     {
445 :     return ICERR_ERROR;
446 :     }
447 :     if (codec->config.dummy2pass)
448 :     {
449 : h 30 outhdr->biSizeImage = codec->twopass.bytes2;
450 : Isibaar 3 *icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0;
451 :     return ICERR_OK;
452 :     }
453 :     break;
454 :    
455 :     case DLG_MODE_NULL :
456 : h 30 outhdr->biSizeImage = 0;
457 : Isibaar 3 *icc->lpdwFlags = AVIIF_KEYFRAME;
458 :     return ICERR_OK;
459 :    
460 :     default :
461 : h 61 DEBUGERR("Invalid encoding mode");
462 : Isibaar 3 return ICERR_ERROR;
463 :     }
464 :    
465 : h 30 if (codec->config.quant_type == QUANT_MODE_H263)
466 : Isibaar 3 {
467 : h 30 frame.general |= XVID_H263QUANT;
468 :     }
469 :     else
470 :     {
471 :     frame.general |= XVID_MPEGQUANT;
472 :    
473 :     // we actually need "default/custom" selectbox for both inter/intra
474 :     // this will do for now
475 :     if (codec->config.quant_type == QUANT_MODE_CUSTOM)
476 :     {
477 :     frame.general |= XVID_CUSTOM_QMATRIX;
478 :     frame.quant_intra_matrix = codec->config.qmatrix_intra;
479 :     frame.quant_inter_matrix = codec->config.qmatrix_inter;
480 :     }
481 :     else
482 :     {
483 :     frame.quant_intra_matrix = NULL;
484 :     frame.quant_inter_matrix = NULL;
485 :     }
486 :     }
487 :    
488 :     // force keyframe spacing in 2-pass 1st pass
489 :     if (codec->config.motion_search == 0)
490 :     {
491 :     frame.intra = 1;
492 :     }
493 : h 109 else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum)
494 : h 30 {
495 : Isibaar 3 DEBUG("current frame forced to p-frame");
496 :     frame.intra = 0;
497 :     }
498 :    
499 :     switch (xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats))
500 :     {
501 :     case XVID_ERR_FAIL :
502 :     return ICERR_ERROR;
503 :    
504 :     case XVID_ERR_MEMORY :
505 :     return ICERR_MEMORY;
506 :    
507 :     case XVID_ERR_FORMAT :
508 :     return ICERR_BADFORMAT;
509 :     }
510 :    
511 :     if (frame.intra)
512 :     {
513 :     codec->keyspacing = 0;
514 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
515 :     }
516 :     else
517 :     {
518 :     *icc->lpdwFlags = 0;
519 :     }
520 :    
521 : h 30 outhdr->biSizeImage = frame.length;
522 : Isibaar 3
523 : h 30 if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass)
524 : Isibaar 3 {
525 : h 30 outhdr->biSizeImage = 0;
526 : Isibaar 3 }
527 :    
528 : h 102 if (frame.general & XVID_HINTEDME_GET)
529 :     {
530 :     DWORD wrote;
531 :     DWORD blocksize = frame.hint.hintlength;
532 :    
533 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
534 :     {
535 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
536 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
537 :     {
538 :     DEBUGERR("couldn't create hints file");
539 :     return ICERR_ERROR;
540 :     }
541 :     }
542 :     if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) ||
543 :     !WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize)
544 :     {
545 :     DEBUGERR("couldn't write to hints file");
546 :     return ICERR_ERROR;
547 :     }
548 :     }
549 :    
550 : Isibaar 3 codec_2pass_update(codec, &frame, &stats);
551 :    
552 :     ++codec->framenum;
553 :     ++codec->keyspacing;
554 :    
555 :     return ICERR_OK;
556 :     }
557 :    
558 :    
559 :     /* decompressor */
560 :    
561 :    
562 :     LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
563 :     {
564 : h 30 BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
565 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
566 : Isibaar 3
567 :     if (lpbiInput == NULL)
568 :     {
569 :     return ICERR_ERROR;
570 :     }
571 :    
572 : h 30 if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX)
573 : Isibaar 3 {
574 :     return ICERR_BADFORMAT;
575 :     }
576 :    
577 :     if (lpbiOutput == NULL)
578 :     {
579 :     return ICERR_OK;
580 :     }
581 :    
582 : h 30 if (inhdr->biWidth != outhdr->biWidth ||
583 :     inhdr->biHeight != outhdr->biHeight ||
584 : Isibaar 3 get_colorspace(outhdr) == XVID_CSP_NULL)
585 :     {
586 :     return ICERR_BADFORMAT;
587 :     }
588 :    
589 :     return ICERR_OK;
590 :     }
591 :    
592 :    
593 :     LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
594 :     {
595 : h 30 BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
596 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
597 : Isibaar 3 LRESULT result;
598 :    
599 :     if (lpbiOutput == NULL)
600 :     {
601 : h 30 return sizeof(BITMAPINFOHEADER);
602 : Isibaar 3 }
603 :    
604 :     result = decompress_query(codec, lpbiInput, lpbiOutput);
605 :     if (result != ICERR_OK)
606 :     {
607 :     return result;
608 :     }
609 :    
610 : h 30 memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
611 :     outhdr->biSize = sizeof(BITMAPINFOHEADER);
612 :     outhdr->biCompression = FOURCC_YUY2;
613 :     outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
614 :     outhdr->biXPelsPerMeter = 0;
615 :     outhdr->biYPelsPerMeter = 0;
616 :     outhdr->biClrUsed = 0;
617 :     outhdr->biClrImportant = 0;
618 : Isibaar 3
619 :     return ICERR_OK;
620 :     }
621 :    
622 :    
623 :     LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
624 :     {
625 :     XVID_DEC_PARAM param;
626 :     XVID_INIT_PARAM init_param;
627 :    
628 :     init_param.cpu_flags = codec->config.cpu;
629 :     xvid_init(0, 0, &init_param, NULL);
630 :     if((codec->config.cpu & XVID_CPU_FORCE) <= 0)
631 :     {
632 :     codec->config.cpu = init_param.cpu_flags;
633 :     }
634 :    
635 : h 30 param.width = lpbiInput->bmiHeader.biWidth;
636 :     param.height = lpbiInput->bmiHeader.biHeight;
637 : Isibaar 3
638 :     switch(xvid_decore(0, XVID_DEC_CREATE, &param, NULL))
639 :     {
640 :     case XVID_ERR_FAIL :
641 :     return ICERR_ERROR;
642 :    
643 :     case XVID_ERR_MEMORY :
644 :     return ICERR_MEMORY;
645 :    
646 :     case XVID_ERR_FORMAT :
647 :     return ICERR_BADFORMAT;
648 :     }
649 :    
650 :     codec->dhandle = param.handle;
651 :    
652 :     return ICERR_OK;
653 :     }
654 :    
655 :    
656 :     LRESULT decompress_end(CODEC * codec)
657 :     {
658 :     if (codec->dhandle != NULL)
659 :     {
660 :     xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
661 :     codec->dhandle = NULL;
662 :     }
663 :     return ICERR_OK;
664 :     }
665 :    
666 :    
667 :     LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
668 :     {
669 :     XVID_DEC_FRAME frame;
670 :    
671 :     frame.bitstream = icd->lpInput;
672 :     frame.length = icd->lpbiInput->biSizeImage;
673 :    
674 :     frame.image = icd->lpOutput;
675 :     frame.stride = icd->lpbiOutput->biWidth;
676 :    
677 : suxen_drol 137 if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
678 : Isibaar 3 {
679 : h 30 if ((frame.colorspace = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
680 : Isibaar 3 {
681 :     return ICERR_BADFORMAT;
682 :     }
683 :     }
684 :     else
685 :     {
686 :     frame.colorspace = XVID_CSP_NULL;
687 :     }
688 :    
689 :     switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
690 :     {
691 :     case XVID_ERR_FAIL :
692 :     return ICERR_ERROR;
693 :    
694 :     case XVID_ERR_MEMORY :
695 :     return ICERR_MEMORY;
696 :    
697 :     case XVID_ERR_FORMAT :
698 :     return ICERR_BADFORMAT;
699 :     }
700 :    
701 :     return ICERR_OK;
702 :     }
703 :    
704 :     int codec_get_quant(CODEC* codec, XVID_ENC_FRAME* frame)
705 :     {
706 :     switch (codec->config.mode)
707 :     {
708 :     case DLG_MODE_VBR_QUAL :
709 :     if (codec_is_in_credits(&codec->config, codec->framenum))
710 :     {
711 :     switch (codec->config.credits_mode)
712 :     {
713 :     case CREDITS_MODE_RATE :
714 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100);
715 :     break;
716 :    
717 :     case CREDITS_MODE_QUANT :
718 :     frame->quant = codec->config.credits_quant_p;
719 :     break;
720 :    
721 :     default :
722 : h 61 DEBUGERR("Can't use credits size mode in quality mode");
723 : Isibaar 3 return ICERR_ERROR;
724 :     }
725 :     }
726 :     else
727 :     {
728 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality);
729 :     }
730 :     return ICERR_OK;
731 :    
732 :     case DLG_MODE_VBR_QUANT :
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 =
739 : h 30 codec->config.max_pquant -
740 :     ((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100);
741 : Isibaar 3 break;
742 :    
743 :     case CREDITS_MODE_QUANT :
744 :     frame->quant = codec->config.credits_quant_p;
745 :     break;
746 :    
747 :     default :
748 : h 61 DEBUGERR("Can't use credits size mode in quantizer mode");
749 : Isibaar 3 return ICERR_ERROR;
750 :     }
751 :     }
752 :     else
753 :     {
754 :     frame->quant = codec->config.quant;
755 :     }
756 :     return ICERR_OK;
757 :    
758 :     case DLG_MODE_2PASS_1 :
759 :     if (codec->config.credits_mode == CREDITS_MODE_QUANT)
760 :     {
761 :     if (codec_is_in_credits(&codec->config, codec->framenum))
762 :     {
763 :     frame->quant = codec->config.credits_quant_p;
764 :     }
765 :     else
766 :     {
767 :     frame->quant = 2;
768 :     }
769 :     }
770 :     else
771 :     {
772 :     frame->quant = 2;
773 :     }
774 :     return ICERR_OK;
775 :    
776 :     default:
777 : h 61 DEBUGERR("get quant: invalid mode");
778 : Isibaar 3 return ICERR_ERROR;
779 :     }
780 :     }
781 :    
782 :    
783 :     int codec_is_in_credits(CONFIG* config, int framenum)
784 :     {
785 :     if (config->credits_start)
786 :     {
787 :     if (framenum >= config->credits_start_begin &&
788 :     framenum <= config->credits_start_end)
789 :     {
790 :     return CREDITS_START;
791 :     }
792 :     }
793 :    
794 :     if (config->credits_end)
795 :     {
796 :     if (framenum >= config->credits_end_begin &&
797 :     framenum <= config->credits_end_end)
798 :     {
799 :     return CREDITS_END;
800 :     }
801 :     }
802 :    
803 :     return 0;
804 :     }
805 :    
806 :    
807 :     int codec_get_vbr_quant(CONFIG* config, int quality)
808 :     {
809 :     static float fquant_running = 0;
810 :     static int my_quality = -1;
811 :     int quant;
812 :    
813 :     // if quality changes, recalculate fquant (credits)
814 :     if (quality != my_quality)
815 :     {
816 :     config->fquant = 0;
817 :     }
818 :    
819 :     my_quality = quality;
820 :    
821 :     // desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ
822 :     if (!config->fquant)
823 :     {
824 :     config->fquant =
825 : h 30 ((float) (config->max_pquant - config->min_pquant) / 100) *
826 : Isibaar 3 (100 - quality) +
827 : h 30 (float) config->min_pquant;
828 : Isibaar 3
829 :     fquant_running = config->fquant;
830 :     }
831 :    
832 : h 30 if (fquant_running < config->min_pquant)
833 : Isibaar 3 {
834 : h 30 fquant_running = (float) config->min_pquant;
835 : Isibaar 3 }
836 : h 30 else if(fquant_running > config->max_pquant)
837 : Isibaar 3 {
838 : h 30 fquant_running = (float) config->max_pquant;
839 : Isibaar 3 }
840 :    
841 :     quant = (int) fquant_running;
842 :    
843 :     // add error between fquant and quant to fquant_running
844 :     fquant_running += config->fquant - quant;
845 :    
846 :     return quant;
847 :     }
848 :    

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