[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 137 - (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 :    
378 :     if (codec->config.hinted_me && codec->config.mode == DLG_MODE_2PASS_1)
379 :     {
380 :     frame.hint.hintstream = codec->twopass.hintstream;
381 :     frame.hint.rawhints = 0;
382 :     frame.general |= XVID_HINTEDME_GET;
383 :     }
384 : h 103 else if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT))
385 : h 102 {
386 :     DWORD read;
387 :     DWORD blocksize;
388 :    
389 :     frame.hint.hintstream = codec->twopass.hintstream;
390 :     frame.hint.rawhints = 0;
391 :     frame.general |= XVID_HINTEDME_SET;
392 :    
393 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
394 :     {
395 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
396 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
397 :     {
398 :     DEBUGERR("couldn't open hints file");
399 :     return ICERR_ERROR;
400 :     }
401 :     }
402 :     if (!ReadFile(codec->twopass.hints, &blocksize, sizeof(DWORD), &read, 0) || read != sizeof(DWORD) ||
403 :     !ReadFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &read, 0) || read != blocksize)
404 :     {
405 :     DEBUGERR("couldn't read from hints file");
406 :     return ICERR_ERROR;
407 :     }
408 :     }
409 :    
410 : Isibaar 3 frame.motion = pmvfast_presets[codec->config.motion_search];
411 : h 30
412 : Isibaar 3 frame.image = icc->lpInput;
413 :    
414 : h 30 if ((frame.colorspace = get_colorspace(inhdr)) == XVID_CSP_NULL)
415 : Isibaar 3 return ICERR_BADFORMAT;
416 :    
417 :     frame.bitstream = icc->lpOutput;
418 :     frame.length = icc->lpbiOutput->biSizeImage;
419 :    
420 :     switch (codec->config.mode)
421 :     {
422 :     case DLG_MODE_CBR :
423 :     frame.quant = 0;
424 :     break;
425 :    
426 :     case DLG_MODE_VBR_QUAL :
427 :     case DLG_MODE_VBR_QUANT :
428 :     case DLG_MODE_2PASS_1 :
429 :     if (codec_get_quant(codec, &frame) == ICERR_ERROR)
430 :     {
431 :     return ICERR_ERROR;
432 :     }
433 :     break;
434 :    
435 :     case DLG_MODE_2PASS_2_EXT :
436 :     case DLG_MODE_2PASS_2_INT :
437 :     if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR)
438 :     {
439 :     return ICERR_ERROR;
440 :     }
441 :     if (codec->config.dummy2pass)
442 :     {
443 : h 30 outhdr->biSizeImage = codec->twopass.bytes2;
444 : Isibaar 3 *icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0;
445 :     return ICERR_OK;
446 :     }
447 :     break;
448 :    
449 :     case DLG_MODE_NULL :
450 : h 30 outhdr->biSizeImage = 0;
451 : Isibaar 3 *icc->lpdwFlags = AVIIF_KEYFRAME;
452 :     return ICERR_OK;
453 :    
454 :     default :
455 : h 61 DEBUGERR("Invalid encoding mode");
456 : Isibaar 3 return ICERR_ERROR;
457 :     }
458 :    
459 : h 30 if (codec->config.quant_type == QUANT_MODE_H263)
460 : Isibaar 3 {
461 : h 30 frame.general |= XVID_H263QUANT;
462 :     }
463 :     else
464 :     {
465 :     frame.general |= XVID_MPEGQUANT;
466 :    
467 :     // we actually need "default/custom" selectbox for both inter/intra
468 :     // this will do for now
469 :     if (codec->config.quant_type == QUANT_MODE_CUSTOM)
470 :     {
471 :     frame.general |= XVID_CUSTOM_QMATRIX;
472 :     frame.quant_intra_matrix = codec->config.qmatrix_intra;
473 :     frame.quant_inter_matrix = codec->config.qmatrix_inter;
474 :     }
475 :     else
476 :     {
477 :     frame.quant_intra_matrix = NULL;
478 :     frame.quant_inter_matrix = NULL;
479 :     }
480 :     }
481 :    
482 :     // force keyframe spacing in 2-pass 1st pass
483 :     if (codec->config.motion_search == 0)
484 :     {
485 :     frame.intra = 1;
486 :     }
487 : h 109 else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum)
488 : h 30 {
489 : Isibaar 3 DEBUG("current frame forced to p-frame");
490 :     frame.intra = 0;
491 :     }
492 :    
493 :     switch (xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats))
494 :     {
495 :     case XVID_ERR_FAIL :
496 :     return ICERR_ERROR;
497 :    
498 :     case XVID_ERR_MEMORY :
499 :     return ICERR_MEMORY;
500 :    
501 :     case XVID_ERR_FORMAT :
502 :     return ICERR_BADFORMAT;
503 :     }
504 :    
505 :     if (frame.intra)
506 :     {
507 :     codec->keyspacing = 0;
508 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
509 :     }
510 :     else
511 :     {
512 :     *icc->lpdwFlags = 0;
513 :     }
514 :    
515 : h 30 outhdr->biSizeImage = frame.length;
516 : Isibaar 3
517 : h 30 if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass)
518 : Isibaar 3 {
519 : h 30 outhdr->biSizeImage = 0;
520 : Isibaar 3 }
521 :    
522 : h 102 if (frame.general & XVID_HINTEDME_GET)
523 :     {
524 :     DWORD wrote;
525 :     DWORD blocksize = frame.hint.hintlength;
526 :    
527 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
528 :     {
529 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
530 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
531 :     {
532 :     DEBUGERR("couldn't create hints file");
533 :     return ICERR_ERROR;
534 :     }
535 :     }
536 :     if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) ||
537 :     !WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize)
538 :     {
539 :     DEBUGERR("couldn't write to hints file");
540 :     return ICERR_ERROR;
541 :     }
542 :     }
543 :    
544 : Isibaar 3 codec_2pass_update(codec, &frame, &stats);
545 :    
546 :     ++codec->framenum;
547 :     ++codec->keyspacing;
548 :    
549 :     return ICERR_OK;
550 :     }
551 :    
552 :    
553 :     /* decompressor */
554 :    
555 :    
556 :     LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
557 :     {
558 : h 30 BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
559 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
560 : Isibaar 3
561 :     if (lpbiInput == NULL)
562 :     {
563 :     return ICERR_ERROR;
564 :     }
565 :    
566 : h 30 if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX)
567 : Isibaar 3 {
568 :     return ICERR_BADFORMAT;
569 :     }
570 :    
571 :     if (lpbiOutput == NULL)
572 :     {
573 :     return ICERR_OK;
574 :     }
575 :    
576 : h 30 if (inhdr->biWidth != outhdr->biWidth ||
577 :     inhdr->biHeight != outhdr->biHeight ||
578 : Isibaar 3 get_colorspace(outhdr) == XVID_CSP_NULL)
579 :     {
580 :     return ICERR_BADFORMAT;
581 :     }
582 :    
583 :     return ICERR_OK;
584 :     }
585 :    
586 :    
587 :     LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
588 :     {
589 : h 30 BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
590 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
591 : Isibaar 3 LRESULT result;
592 :    
593 :     if (lpbiOutput == NULL)
594 :     {
595 : h 30 return sizeof(BITMAPINFOHEADER);
596 : Isibaar 3 }
597 :    
598 :     result = decompress_query(codec, lpbiInput, lpbiOutput);
599 :     if (result != ICERR_OK)
600 :     {
601 :     return result;
602 :     }
603 :    
604 : h 30 memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
605 :     outhdr->biSize = sizeof(BITMAPINFOHEADER);
606 :     outhdr->biCompression = FOURCC_YUY2;
607 :     outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
608 :     outhdr->biXPelsPerMeter = 0;
609 :     outhdr->biYPelsPerMeter = 0;
610 :     outhdr->biClrUsed = 0;
611 :     outhdr->biClrImportant = 0;
612 : Isibaar 3
613 :     return ICERR_OK;
614 :     }
615 :    
616 :    
617 :     LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
618 :     {
619 :     XVID_DEC_PARAM param;
620 :     XVID_INIT_PARAM init_param;
621 :    
622 :     init_param.cpu_flags = codec->config.cpu;
623 :     xvid_init(0, 0, &init_param, NULL);
624 :     if((codec->config.cpu & XVID_CPU_FORCE) <= 0)
625 :     {
626 :     codec->config.cpu = init_param.cpu_flags;
627 :     }
628 :    
629 : h 30 param.width = lpbiInput->bmiHeader.biWidth;
630 :     param.height = lpbiInput->bmiHeader.biHeight;
631 : Isibaar 3
632 :     switch(xvid_decore(0, XVID_DEC_CREATE, &param, NULL))
633 :     {
634 :     case XVID_ERR_FAIL :
635 :     return ICERR_ERROR;
636 :    
637 :     case XVID_ERR_MEMORY :
638 :     return ICERR_MEMORY;
639 :    
640 :     case XVID_ERR_FORMAT :
641 :     return ICERR_BADFORMAT;
642 :     }
643 :    
644 :     codec->dhandle = param.handle;
645 :    
646 :     return ICERR_OK;
647 :     }
648 :    
649 :    
650 :     LRESULT decompress_end(CODEC * codec)
651 :     {
652 :     if (codec->dhandle != NULL)
653 :     {
654 :     xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
655 :     codec->dhandle = NULL;
656 :     }
657 :     return ICERR_OK;
658 :     }
659 :    
660 :    
661 :     LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
662 :     {
663 :     XVID_DEC_FRAME frame;
664 :    
665 :     frame.bitstream = icd->lpInput;
666 :     frame.length = icd->lpbiInput->biSizeImage;
667 :    
668 :     frame.image = icd->lpOutput;
669 :     frame.stride = icd->lpbiOutput->biWidth;
670 :    
671 : suxen_drol 137 if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
672 : Isibaar 3 {
673 : h 30 if ((frame.colorspace = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
674 : Isibaar 3 {
675 :     return ICERR_BADFORMAT;
676 :     }
677 :     }
678 :     else
679 :     {
680 :     frame.colorspace = XVID_CSP_NULL;
681 :     }
682 :    
683 :     switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
684 :     {
685 :     case XVID_ERR_FAIL :
686 :     return ICERR_ERROR;
687 :    
688 :     case XVID_ERR_MEMORY :
689 :     return ICERR_MEMORY;
690 :    
691 :     case XVID_ERR_FORMAT :
692 :     return ICERR_BADFORMAT;
693 :     }
694 :    
695 :     return ICERR_OK;
696 :     }
697 :    
698 :     int codec_get_quant(CODEC* codec, XVID_ENC_FRAME* frame)
699 :     {
700 :     switch (codec->config.mode)
701 :     {
702 :     case DLG_MODE_VBR_QUAL :
703 :     if (codec_is_in_credits(&codec->config, codec->framenum))
704 :     {
705 :     switch (codec->config.credits_mode)
706 :     {
707 :     case CREDITS_MODE_RATE :
708 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100);
709 :     break;
710 :    
711 :     case CREDITS_MODE_QUANT :
712 :     frame->quant = codec->config.credits_quant_p;
713 :     break;
714 :    
715 :     default :
716 : h 61 DEBUGERR("Can't use credits size mode in quality mode");
717 : Isibaar 3 return ICERR_ERROR;
718 :     }
719 :     }
720 :     else
721 :     {
722 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality);
723 :     }
724 :     return ICERR_OK;
725 :    
726 :     case DLG_MODE_VBR_QUANT :
727 :     if (codec_is_in_credits(&codec->config, codec->framenum))
728 :     {
729 :     switch (codec->config.credits_mode)
730 :     {
731 :     case CREDITS_MODE_RATE :
732 :     frame->quant =
733 : h 30 codec->config.max_pquant -
734 :     ((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100);
735 : Isibaar 3 break;
736 :    
737 :     case CREDITS_MODE_QUANT :
738 :     frame->quant = codec->config.credits_quant_p;
739 :     break;
740 :    
741 :     default :
742 : h 61 DEBUGERR("Can't use credits size mode in quantizer mode");
743 : Isibaar 3 return ICERR_ERROR;
744 :     }
745 :     }
746 :     else
747 :     {
748 :     frame->quant = codec->config.quant;
749 :     }
750 :     return ICERR_OK;
751 :    
752 :     case DLG_MODE_2PASS_1 :
753 :     if (codec->config.credits_mode == CREDITS_MODE_QUANT)
754 :     {
755 :     if (codec_is_in_credits(&codec->config, codec->framenum))
756 :     {
757 :     frame->quant = codec->config.credits_quant_p;
758 :     }
759 :     else
760 :     {
761 :     frame->quant = 2;
762 :     }
763 :     }
764 :     else
765 :     {
766 :     frame->quant = 2;
767 :     }
768 :     return ICERR_OK;
769 :    
770 :     default:
771 : h 61 DEBUGERR("get quant: invalid mode");
772 : Isibaar 3 return ICERR_ERROR;
773 :     }
774 :     }
775 :    
776 :    
777 :     int codec_is_in_credits(CONFIG* config, int framenum)
778 :     {
779 :     if (config->credits_start)
780 :     {
781 :     if (framenum >= config->credits_start_begin &&
782 :     framenum <= config->credits_start_end)
783 :     {
784 :     return CREDITS_START;
785 :     }
786 :     }
787 :    
788 :     if (config->credits_end)
789 :     {
790 :     if (framenum >= config->credits_end_begin &&
791 :     framenum <= config->credits_end_end)
792 :     {
793 :     return CREDITS_END;
794 :     }
795 :     }
796 :    
797 :     return 0;
798 :     }
799 :    
800 :    
801 :     int codec_get_vbr_quant(CONFIG* config, int quality)
802 :     {
803 :     static float fquant_running = 0;
804 :     static int my_quality = -1;
805 :     int quant;
806 :    
807 :     // if quality changes, recalculate fquant (credits)
808 :     if (quality != my_quality)
809 :     {
810 :     config->fquant = 0;
811 :     }
812 :    
813 :     my_quality = quality;
814 :    
815 :     // desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ
816 :     if (!config->fquant)
817 :     {
818 :     config->fquant =
819 : h 30 ((float) (config->max_pquant - config->min_pquant) / 100) *
820 : Isibaar 3 (100 - quality) +
821 : h 30 (float) config->min_pquant;
822 : Isibaar 3
823 :     fquant_running = config->fquant;
824 :     }
825 :    
826 : h 30 if (fquant_running < config->min_pquant)
827 : Isibaar 3 {
828 : h 30 fquant_running = (float) config->min_pquant;
829 : Isibaar 3 }
830 : h 30 else if(fquant_running > config->max_pquant)
831 : Isibaar 3 {
832 : h 30 fquant_running = (float) config->max_pquant;
833 : Isibaar 3 }
834 :    
835 :     quant = (int) fquant_running;
836 :    
837 :     // add error between fquant and quant to fquant_running
838 :     fquant_running += config->fquant - quant;
839 :    
840 :     return quant;
841 :     }
842 :    

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