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

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