[svn] / branches / dev-api-3 / vfw / src / codec.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/vfw/src/codec.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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