[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 901 - (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 : suxen_drol 499 * 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 : suxen_drol 858 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 858 /* 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 858 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 858 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 858 return rgb_flip | XVID_CSP_RGB32;
90 : Isibaar 3 }
91 :    
92 : suxen_drol 858 DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount);
93 : Isibaar 3 return XVID_CSP_NULL;
94 :    
95 : suxen_drol 858 case BI_BITFIELDS :
96 :     if (hdr->biSize >= sizeof(BITMAPV4HEADER))
97 : Isibaar 3 {
98 : suxen_drol 858 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 858 DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination");
123 : Isibaar 3 return XVID_CSP_NULL;
124 : suxen_drol 858
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 858
134 : Isibaar 3 case FOURCC_YUYV :
135 :     case FOURCC_YUY2 :
136 :     DEBUG("YUY2");
137 :     return XVID_CSP_YUY2;
138 :    
139 : suxen_drol 858 case FOURCC_YVYU :
140 : Isibaar 3 DEBUG("YVYU");
141 :     return XVID_CSP_YVYU;
142 :    
143 : suxen_drol 858 case FOURCC_UYVY :
144 : Isibaar 3 DEBUG("UYVY");
145 :     return XVID_CSP_UYVY;
146 :    
147 : suxen_drol 858 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 858 (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 858 return 2 * lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3;
227 : Isibaar 3 }
228 :    
229 :    
230 :     LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf)
231 :     {
232 :     // DEBUG2("frate fscale", codec->frate, codec->fscale);
233 :     codec->fincr = icf->dwScale;
234 :     codec->fbase = icf->dwRate;
235 :     return ICERR_OK;
236 :     }
237 :    
238 :    
239 :     LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
240 :     {
241 :     XVID_ENC_PARAM param;
242 :     XVID_INIT_PARAM init_param;
243 :    
244 :     switch (codec->config.mode)
245 :     {
246 :     case DLG_MODE_CBR :
247 : h 121 param.rc_bitrate = codec->config.rc_bitrate;
248 :     param.rc_reaction_delay_factor = codec->config.rc_reaction_delay_factor;
249 :     param.rc_averaging_period = codec->config.rc_averaging_period;
250 :     param.rc_buffer = codec->config.rc_buffer;
251 : Isibaar 3 break;
252 :    
253 :     case DLG_MODE_VBR_QUAL :
254 :     codec->config.fquant = 0;
255 : h 121 param.rc_bitrate = 0;
256 : Isibaar 3 break;
257 :    
258 :     case DLG_MODE_VBR_QUANT :
259 :     codec->config.fquant = (float) codec->config.quant;
260 : h 121 param.rc_bitrate = 0;
261 : Isibaar 3 break;
262 :    
263 : h 61 case DLG_MODE_2PASS_1 :
264 :     case DLG_MODE_2PASS_2_INT :
265 :     case DLG_MODE_2PASS_2_EXT :
266 : h 121 param.rc_bitrate = 0;
267 : h 109 codec->twopass.max_framesize = (int)((double)codec->config.twopass_max_bitrate / 8.0 / ((double)codec->fbase / (double)codec->fincr));
268 : Isibaar 3 break;
269 :    
270 : h 61 case DLG_MODE_NULL :
271 :     return ICERR_OK;
272 :    
273 : Isibaar 3 default :
274 :     break;
275 :     }
276 :    
277 :     if(codec->ehandle)
278 :     {
279 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
280 :     codec->ehandle = NULL;
281 :     }
282 :    
283 :     init_param.cpu_flags = codec->config.cpu;
284 :     xvid_init(0, 0, &init_param, NULL);
285 :     if((codec->config.cpu & XVID_CPU_FORCE) <= 0)
286 :     codec->config.cpu = init_param.cpu_flags;
287 :    
288 : h 30 param.width = lpbiInput->bmiHeader.biWidth;
289 :     param.height = lpbiInput->bmiHeader.biHeight;
290 : Isibaar 3 param.fincr = codec->fincr;
291 :     param.fbase = codec->fbase;
292 :    
293 : h 30 param.min_quantizer = codec->config.min_pquant;
294 :     param.max_quantizer = codec->config.max_pquant;
295 : Isibaar 3 param.max_key_interval = codec->config.max_key_interval;
296 :    
297 : suxen_drol 295 #ifdef _SMP
298 :     param.num_threads = codec->config.num_threads;
299 :     #endif
300 :    
301 : suxen_drol 235 param.global = 0;
302 :     if (codec->config.packed) param.global |= XVID_GLOBAL_PACKED;
303 :     if (codec->config.dx50bvop) param.global |= XVID_GLOBAL_DX50BVOP;
304 :     if (codec->config.debug) param.global |= XVID_GLOBAL_DEBUG;
305 : suxen_drol 858 if (codec->config.reduced_resolution) param.global |= XVID_GLOBAL_REDUCED;
306 : suxen_drol 228 param.max_bframes = codec->config.max_bframes;
307 :     param.bquant_ratio = codec->config.bquant_ratio;
308 : suxen_drol 858 param.bquant_offset = codec->config.bquant_offset;
309 : suxen_drol 324 param.frame_drop_ratio = codec->config.frame_drop_ratio;
310 : suxen_drol 228
311 : Isibaar 3 switch(xvid_encore(0, XVID_ENC_CREATE, &param, NULL))
312 :     {
313 :     case XVID_ERR_FAIL :
314 :     return ICERR_ERROR;
315 :    
316 :     case XVID_ERR_MEMORY :
317 :     return ICERR_MEMORY;
318 :    
319 :     case XVID_ERR_FORMAT :
320 :     return ICERR_BADFORMAT;
321 :     }
322 :    
323 :     codec->ehandle = param.handle;
324 :     codec->framenum = 0;
325 :     codec->keyspacing = 0;
326 :    
327 : h 102 codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE;
328 :     codec->twopass.hintstream = NULL;
329 : Isibaar 3
330 :     return ICERR_OK;
331 :     }
332 :    
333 :    
334 :     LRESULT compress_end(CODEC * codec)
335 :     {
336 :     if (codec->ehandle != NULL)
337 :     {
338 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
339 : h 102
340 :     if (codec->twopass.hints != INVALID_HANDLE_VALUE)
341 :     {
342 :     CloseHandle(codec->twopass.hints);
343 :     }
344 : Isibaar 3 if (codec->twopass.stats1 != INVALID_HANDLE_VALUE)
345 :     {
346 :     CloseHandle(codec->twopass.stats1);
347 :     }
348 :     if (codec->twopass.stats2 != INVALID_HANDLE_VALUE)
349 :     {
350 :     CloseHandle(codec->twopass.stats2);
351 :     }
352 : h 102 if (codec->twopass.hintstream != NULL)
353 :     {
354 :     free(codec->twopass.hintstream);
355 :     }
356 :    
357 : Isibaar 3 codec->ehandle = NULL;
358 : h 105
359 :     codec_2pass_finish(codec);
360 : Isibaar 3 }
361 :    
362 :     return ICERR_OK;
363 :     }
364 :    
365 :    
366 :     LRESULT compress(CODEC * codec, ICCOMPRESS * icc)
367 :     {
368 : h 30 BITMAPINFOHEADER * inhdr = icc->lpbiInput;
369 :     BITMAPINFOHEADER * outhdr = icc->lpbiOutput;
370 : Isibaar 3 XVID_ENC_FRAME frame;
371 :     XVID_ENC_STATS stats;
372 :    
373 :     // mpeg2avi yuv bug workaround (2 instances of CODEC)
374 :     if (codec->twopass.stats1 == INVALID_HANDLE_VALUE)
375 :     {
376 :     if (codec_2pass_init(codec) == ICERR_ERROR)
377 :     {
378 :     return ICERR_ERROR;
379 :     }
380 :     }
381 :    
382 :     frame.general = 0;
383 :     frame.motion = 0;
384 : h 30 frame.intra = -1;
385 : Isibaar 3
386 :     frame.general |= XVID_HALFPEL;
387 : Isibaar 364 // frame.general |= XVID_ME_EPZS;
388 : Isibaar 3
389 : suxen_drol 858 frame.general |= XVID_HQACPRED;
390 : Isibaar 364
391 : h 127 if (codec->config.motion_search > 4)
392 : Isibaar 3 frame.general |= XVID_INTER4V;
393 :    
394 : h 127 if (codec->config.lum_masking)
395 : Isibaar 3 frame.general |= XVID_LUMIMASKING;
396 :    
397 : h 102 if (codec->config.interlacing)
398 : Isibaar 364 frame.general |= XVID_INTERLACING;
399 : chl 355
400 : suxen_drol 858 if (codec->config.qpel) {
401 :     frame.general |= XVID_QUARTERPEL;
402 :     frame.motion |= PMV_QUARTERPELREFINE16 | PMV_QUARTERPELREFINE8;
403 :     }
404 : Isibaar 364
405 : suxen_drol 858 if (codec->config.gmc)
406 :     frame.general |= XVID_GMC;
407 : Isibaar 364
408 : suxen_drol 858 if (codec->config.chromame)
409 :     frame.motion |= PMV_CHROMA16 + PMV_CHROMA8;
410 :    
411 :     if (codec->config.reduced_resolution)
412 :     frame.general |= XVID_REDUCED;
413 :    
414 :     if (codec->config.chroma_opt)
415 :     frame.general |= XVID_CHROMAOPT;
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 858 frame.motion |= pmvfast_presets[codec->config.motion_search];
463 : h 30
464 : suxen_drol 858 switch (codec->config.vhq_mode)
465 :     {
466 :     case VHQ_MODE_DECISION :
467 :     frame.general |= XVID_MODEDECISION_BITS;
468 :     break;
469 :    
470 :     case VHQ_LIMITED_SEARCH :
471 :     frame.general |= XVID_MODEDECISION_BITS;
472 :     frame.motion |= HALFPELREFINE16_BITS;
473 :     frame.motion |= QUARTERPELREFINE16_BITS;
474 :     break;
475 :    
476 :     case VHQ_MEDIUM_SEARCH :
477 :     frame.general |= XVID_MODEDECISION_BITS;
478 :     frame.motion |= HALFPELREFINE16_BITS;
479 :     frame.motion |= HALFPELREFINE8_BITS;
480 :     frame.motion |= QUARTERPELREFINE16_BITS;
481 :     frame.motion |= QUARTERPELREFINE8_BITS;
482 :     frame.motion |= CHECKPREDICTION_BITS;
483 :     break;
484 :    
485 :     case VHQ_WIDE_SEARCH :
486 :     frame.general |= XVID_MODEDECISION_BITS;
487 :     frame.motion |= HALFPELREFINE16_BITS;
488 :     frame.motion |= HALFPELREFINE8_BITS;
489 :     frame.motion |= QUARTERPELREFINE16_BITS;
490 :     frame.motion |= QUARTERPELREFINE8_BITS;
491 :     frame.motion |= CHECKPREDICTION_BITS;
492 :     frame.motion |= EXTSEARCH_BITS;
493 :     break;
494 :    
495 :     default :
496 :     break;
497 :     }
498 :    
499 : Isibaar 3 frame.image = icc->lpInput;
500 : suxen_drol 858 frame.stride = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3;
501 : Isibaar 3
502 : h 30 if ((frame.colorspace = get_colorspace(inhdr)) == XVID_CSP_NULL)
503 : Isibaar 3 return ICERR_BADFORMAT;
504 :    
505 : suxen_drol 858 if (frame.colorspace == XVID_CSP_I420 || frame.colorspace == XVID_CSP_YV12)
506 :     frame.stride = (frame.stride*2)/3;
507 :    
508 : Isibaar 3 frame.bitstream = icc->lpOutput;
509 :     frame.length = icc->lpbiOutput->biSizeImage;
510 :    
511 :     switch (codec->config.mode)
512 :     {
513 :     case DLG_MODE_CBR :
514 :     frame.quant = 0;
515 :     break;
516 :    
517 :     case DLG_MODE_VBR_QUAL :
518 :     case DLG_MODE_VBR_QUANT :
519 :     case DLG_MODE_2PASS_1 :
520 :     if (codec_get_quant(codec, &frame) == ICERR_ERROR)
521 :     {
522 :     return ICERR_ERROR;
523 :     }
524 :     break;
525 :    
526 :     case DLG_MODE_2PASS_2_EXT :
527 :     case DLG_MODE_2PASS_2_INT :
528 :     if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR)
529 :     {
530 :     return ICERR_ERROR;
531 :     }
532 :     if (codec->config.dummy2pass)
533 :     {
534 : h 30 outhdr->biSizeImage = codec->twopass.bytes2;
535 : Isibaar 3 *icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0;
536 :     return ICERR_OK;
537 :     }
538 :     break;
539 :    
540 :     case DLG_MODE_NULL :
541 : h 30 outhdr->biSizeImage = 0;
542 : Isibaar 3 *icc->lpdwFlags = AVIIF_KEYFRAME;
543 :     return ICERR_OK;
544 :    
545 :     default :
546 : h 61 DEBUGERR("Invalid encoding mode");
547 : Isibaar 3 return ICERR_ERROR;
548 :     }
549 :    
550 : h 30 if (codec->config.quant_type == QUANT_MODE_H263)
551 : Isibaar 3 {
552 : h 30 frame.general |= XVID_H263QUANT;
553 :     }
554 :     else
555 :     {
556 :     frame.general |= XVID_MPEGQUANT;
557 :    
558 :     // we actually need "default/custom" selectbox for both inter/intra
559 :     // this will do for now
560 :     if (codec->config.quant_type == QUANT_MODE_CUSTOM)
561 :     {
562 :     frame.general |= XVID_CUSTOM_QMATRIX;
563 :     frame.quant_intra_matrix = codec->config.qmatrix_intra;
564 :     frame.quant_inter_matrix = codec->config.qmatrix_inter;
565 :     }
566 :     else
567 :     {
568 :     frame.quant_intra_matrix = NULL;
569 :     frame.quant_inter_matrix = NULL;
570 :     }
571 :     }
572 :    
573 :     // force keyframe spacing in 2-pass 1st pass
574 :     if (codec->config.motion_search == 0)
575 :     {
576 :     frame.intra = 1;
577 :     }
578 : h 109 else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum)
579 : h 30 {
580 : Isibaar 3 DEBUG("current frame forced to p-frame");
581 :     frame.intra = 0;
582 :     }
583 :    
584 : suxen_drol 278 frame.bquant = 0;
585 :    
586 : suxen_drol 324 // OutputDebugString(" ");
587 : Isibaar 3 switch (xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats))
588 :     {
589 :     case XVID_ERR_FAIL :
590 :     return ICERR_ERROR;
591 :    
592 :     case XVID_ERR_MEMORY :
593 :     return ICERR_MEMORY;
594 :    
595 :     case XVID_ERR_FORMAT :
596 :     return ICERR_BADFORMAT;
597 :     }
598 :    
599 : suxen_drol 858 if (frame.intra==1)
600 : Isibaar 3 {
601 :     codec->keyspacing = 0;
602 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
603 :     }
604 :     else
605 :     {
606 :     *icc->lpdwFlags = 0;
607 :     }
608 : syskin 901 if (frame.length == 0) { frame.length = 1; *((unsigned char*)frame.bitstream) = 0x7f; }
609 : Isibaar 3
610 : h 30 outhdr->biSizeImage = frame.length;
611 : Isibaar 3
612 : h 30 if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass)
613 : Isibaar 3 {
614 : h 30 outhdr->biSizeImage = 0;
615 : Isibaar 3 }
616 :    
617 : h 102 if (frame.general & XVID_HINTEDME_GET)
618 :     {
619 :     DWORD wrote;
620 :     DWORD blocksize = frame.hint.hintlength;
621 :    
622 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
623 :     {
624 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
625 : suxen_drol 228 if (codec->twopass.hints == INVALID_HANDLE_VALUE)
626 : h 102 {
627 :     DEBUGERR("couldn't create hints file");
628 :     return ICERR_ERROR;
629 :     }
630 :     }
631 :     if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) ||
632 :     !WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize)
633 :     {
634 :     DEBUGERR("couldn't write to hints file");
635 :     return ICERR_ERROR;
636 :     }
637 :     }
638 :    
639 : suxen_drol 858 //quick fix for delayed frames
640 :     // if (frame.intra != 5)
641 :     codec_2pass_update(codec, &frame, &stats);
642 : Isibaar 3
643 :     ++codec->framenum;
644 :     ++codec->keyspacing;
645 :    
646 :     return ICERR_OK;
647 :     }
648 :    
649 :    
650 :     /* decompressor */
651 :    
652 :    
653 :     LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
654 :     {
655 : h 30 BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
656 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
657 : Isibaar 3
658 :     if (lpbiInput == NULL)
659 :     {
660 :     return ICERR_ERROR;
661 :     }
662 :    
663 : suxen_drol 858 if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL)
664 : Isibaar 3 {
665 :     return ICERR_BADFORMAT;
666 :     }
667 :    
668 :     if (lpbiOutput == NULL)
669 :     {
670 :     return ICERR_OK;
671 :     }
672 :    
673 : h 30 if (inhdr->biWidth != outhdr->biWidth ||
674 :     inhdr->biHeight != outhdr->biHeight ||
675 : Isibaar 3 get_colorspace(outhdr) == XVID_CSP_NULL)
676 :     {
677 :     return ICERR_BADFORMAT;
678 :     }
679 :    
680 :     return ICERR_OK;
681 :     }
682 :    
683 :    
684 :     LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
685 :     {
686 : h 30 BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
687 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
688 : Isibaar 3 LRESULT result;
689 :    
690 :     if (lpbiOutput == NULL)
691 :     {
692 : h 30 return sizeof(BITMAPINFOHEADER);
693 : Isibaar 3 }
694 :    
695 : suxen_drol 858 /* --- yv12 --- */
696 :    
697 :     if (get_colorspace(inhdr) != XVID_CSP_NULL) {
698 :     memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
699 :     // XXX: should we set outhdr->biSize ??
700 :     return ICERR_OK;
701 :     }
702 :     /* --- yv12 --- */
703 :    
704 :     result = decompress_query(codec, lpbiInput, NULL);
705 : Isibaar 3 if (result != ICERR_OK)
706 :     {
707 :     return result;
708 :     }
709 :    
710 : h 30 outhdr->biSize = sizeof(BITMAPINFOHEADER);
711 : suxen_drol 858 outhdr->biWidth = inhdr->biWidth;
712 :     outhdr->biHeight = inhdr->biHeight;
713 :     outhdr->biPlanes = 1;
714 :     outhdr->biBitCount = 24;
715 :     outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */
716 : h 30 outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
717 :     outhdr->biXPelsPerMeter = 0;
718 :     outhdr->biYPelsPerMeter = 0;
719 :     outhdr->biClrUsed = 0;
720 :     outhdr->biClrImportant = 0;
721 : Isibaar 3
722 :     return ICERR_OK;
723 :     }
724 :    
725 :    
726 :     LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
727 :     {
728 :     XVID_DEC_PARAM param;
729 :     XVID_INIT_PARAM init_param;
730 :    
731 :     init_param.cpu_flags = codec->config.cpu;
732 :     xvid_init(0, 0, &init_param, NULL);
733 :     if((codec->config.cpu & XVID_CPU_FORCE) <= 0)
734 :     {
735 :     codec->config.cpu = init_param.cpu_flags;
736 :     }
737 :    
738 : h 30 param.width = lpbiInput->bmiHeader.biWidth;
739 :     param.height = lpbiInput->bmiHeader.biHeight;
740 : Isibaar 3
741 :     switch(xvid_decore(0, XVID_DEC_CREATE, &param, NULL))
742 :     {
743 :     case XVID_ERR_FAIL :
744 :     return ICERR_ERROR;
745 :    
746 :     case XVID_ERR_MEMORY :
747 :     return ICERR_MEMORY;
748 :    
749 :     case XVID_ERR_FORMAT :
750 :     return ICERR_BADFORMAT;
751 :     }
752 :    
753 :     codec->dhandle = param.handle;
754 :    
755 :     return ICERR_OK;
756 :     }
757 :    
758 :    
759 :     LRESULT decompress_end(CODEC * codec)
760 :     {
761 :     if (codec->dhandle != NULL)
762 :     {
763 :     xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
764 :     codec->dhandle = NULL;
765 :     }
766 :     return ICERR_OK;
767 :     }
768 :    
769 :    
770 :     LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
771 :     {
772 :     XVID_DEC_FRAME frame;
773 :    
774 :     frame.bitstream = icd->lpInput;
775 :     frame.length = icd->lpbiInput->biSizeImage;
776 : suxen_drol 858 frame.general = XVID_DEC_LOWDELAY; /* force low_delay_default mode */
777 :     if (codec->config.deblock_y)
778 :     frame.general |= XVID_DEC_DEBLOCKY;
779 :     if (codec->config.deblock_uv)
780 :     frame.general |= XVID_DEC_DEBLOCKUV;
781 : Isibaar 3
782 :     frame.image = icd->lpOutput;
783 : suxen_drol 858 frame.stride = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
784 : Isibaar 3
785 : suxen_drol 858 /* --- yv12 --- */
786 :     if (icd->lpbiInput->biCompression != FOURCC_XVID &&
787 :     icd->lpbiInput->biCompression != FOURCC_DIVX &&
788 :     icd->lpbiInput->biCompression != FOURCC_DX50)
789 :     {
790 :     XVID_INIT_CONVERTINFO convert;
791 :     DEBUGFOURCC("input", icd->lpbiInput->biCompression);
792 :     DEBUGFOURCC("output", icd->lpbiOutput->biCompression);
793 :     convert.input.colorspace = get_colorspace(icd->lpbiInput);
794 :     convert.input.y = icd->lpInput;
795 :     convert.input.y_stride = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3;
796 :     if (convert.input.colorspace == XVID_CSP_I420 || convert.input.colorspace == XVID_CSP_YV12)
797 :     convert.input.y_stride = (convert.input.y_stride*2)/3;
798 :    
799 :     convert.output.colorspace = get_colorspace(icd->lpbiOutput);
800 :     convert.output.y = icd->lpOutput;
801 :     convert.output.y_stride = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
802 :     if (convert.output.colorspace == XVID_CSP_I420 || convert.output.colorspace == XVID_CSP_YV12)
803 :     convert.output.y_stride = (convert.output.y_stride*2)/3;
804 :    
805 :     convert.width = icd->lpbiInput->biWidth;
806 :     convert.height = icd->lpbiInput->biHeight;
807 :     convert.interlacing = 0;
808 :     if (convert.input.colorspace == XVID_CSP_NULL ||
809 :     convert.output.colorspace == XVID_CSP_NULL ||
810 :     xvid_init(NULL, XVID_INIT_CONVERT, &convert, NULL) != XVID_ERR_OK)
811 :     {
812 :     return ICERR_BADFORMAT;
813 :     }
814 :     return ICERR_OK;
815 :     }
816 :     /* --- yv12 --- */
817 :    
818 :    
819 : suxen_drol 137 if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
820 : Isibaar 3 {
821 : h 30 if ((frame.colorspace = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
822 : Isibaar 3 {
823 :     return ICERR_BADFORMAT;
824 :     }
825 :     }
826 :     else
827 :     {
828 :     frame.colorspace = XVID_CSP_NULL;
829 :     }
830 :    
831 : suxen_drol 858 if (frame.colorspace == XVID_CSP_I420 || frame.colorspace == XVID_CSP_YV12)
832 :     frame.stride = (frame.stride*2)/3;
833 :    
834 : Isibaar 3 switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
835 :     {
836 :     case XVID_ERR_FAIL :
837 :     return ICERR_ERROR;
838 :    
839 :     case XVID_ERR_MEMORY :
840 :     return ICERR_MEMORY;
841 :    
842 :     case XVID_ERR_FORMAT :
843 :     return ICERR_BADFORMAT;
844 :     }
845 :    
846 :     return ICERR_OK;
847 :     }
848 :    
849 :     int codec_get_quant(CODEC* codec, XVID_ENC_FRAME* frame)
850 :     {
851 :     switch (codec->config.mode)
852 :     {
853 :     case DLG_MODE_VBR_QUAL :
854 :     if (codec_is_in_credits(&codec->config, codec->framenum))
855 :     {
856 : Isibaar 364 // added by koepi for credits greyscale
857 :    
858 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
859 :    
860 :     // end of koepi's addition
861 :    
862 : Isibaar 3 switch (codec->config.credits_mode)
863 :     {
864 :     case CREDITS_MODE_RATE :
865 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100);
866 :     break;
867 :    
868 :     case CREDITS_MODE_QUANT :
869 :     frame->quant = codec->config.credits_quant_p;
870 :     break;
871 :    
872 :     default :
873 : h 61 DEBUGERR("Can't use credits size mode in quality mode");
874 : Isibaar 3 return ICERR_ERROR;
875 :     }
876 :     }
877 :     else
878 :     {
879 : Isibaar 364 // added by koepi for credits greyscale
880 :    
881 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
882 :    
883 :     // end of koepi's addition
884 :    
885 : Isibaar 3 frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality);
886 :     }
887 :     return ICERR_OK;
888 :    
889 :     case DLG_MODE_VBR_QUANT :
890 :     if (codec_is_in_credits(&codec->config, codec->framenum))
891 :     {
892 : Isibaar 364 // added by koepi for credits greyscale
893 :    
894 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
895 :    
896 :     // end of koepi's addition
897 :    
898 : Isibaar 3 switch (codec->config.credits_mode)
899 :     {
900 :     case CREDITS_MODE_RATE :
901 :     frame->quant =
902 : h 30 codec->config.max_pquant -
903 :     ((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100);
904 : Isibaar 3 break;
905 :    
906 :     case CREDITS_MODE_QUANT :
907 :     frame->quant = codec->config.credits_quant_p;
908 :     break;
909 :    
910 :     default :
911 : h 61 DEBUGERR("Can't use credits size mode in quantizer mode");
912 : Isibaar 3 return ICERR_ERROR;
913 :     }
914 :     }
915 :     else
916 :     {
917 : Isibaar 364 // added by koepi for credits greyscale
918 :    
919 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
920 :    
921 :     // end of koepi's addition
922 :    
923 : Isibaar 3 frame->quant = codec->config.quant;
924 :     }
925 :     return ICERR_OK;
926 :    
927 :     case DLG_MODE_2PASS_1 :
928 : Isibaar 364 // added by koepi for credits greyscale
929 :    
930 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
931 :    
932 :     // end of koepi's addition
933 :    
934 : Isibaar 3 if (codec->config.credits_mode == CREDITS_MODE_QUANT)
935 :     {
936 :     if (codec_is_in_credits(&codec->config, codec->framenum))
937 :     {
938 :     frame->quant = codec->config.credits_quant_p;
939 :     }
940 :     else
941 :     {
942 :     frame->quant = 2;
943 :     }
944 :     }
945 :     else
946 :     {
947 :     frame->quant = 2;
948 :     }
949 :     return ICERR_OK;
950 :    
951 :     default:
952 : h 61 DEBUGERR("get quant: invalid mode");
953 : Isibaar 3 return ICERR_ERROR;
954 :     }
955 :     }
956 :    
957 :    
958 :     int codec_is_in_credits(CONFIG* config, int framenum)
959 :     {
960 :     if (config->credits_start)
961 :     {
962 :     if (framenum >= config->credits_start_begin &&
963 :     framenum <= config->credits_start_end)
964 :     {
965 :     return CREDITS_START;
966 :     }
967 :     }
968 :    
969 :     if (config->credits_end)
970 :     {
971 :     if (framenum >= config->credits_end_begin &&
972 :     framenum <= config->credits_end_end)
973 :     {
974 :     return CREDITS_END;
975 :     }
976 :     }
977 :    
978 :     return 0;
979 :     }
980 :    
981 :    
982 :     int codec_get_vbr_quant(CONFIG* config, int quality)
983 :     {
984 :     static float fquant_running = 0;
985 :     static int my_quality = -1;
986 :     int quant;
987 :    
988 :     // if quality changes, recalculate fquant (credits)
989 :     if (quality != my_quality)
990 :     {
991 :     config->fquant = 0;
992 :     }
993 :    
994 :     my_quality = quality;
995 :    
996 :     // desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ
997 :     if (!config->fquant)
998 :     {
999 :     config->fquant =
1000 : h 30 ((float) (config->max_pquant - config->min_pquant) / 100) *
1001 : Isibaar 3 (100 - quality) +
1002 : h 30 (float) config->min_pquant;
1003 : Isibaar 3
1004 :     fquant_running = config->fquant;
1005 :     }
1006 :    
1007 : h 30 if (fquant_running < config->min_pquant)
1008 : Isibaar 3 {
1009 : h 30 fquant_running = (float) config->min_pquant;
1010 : Isibaar 3 }
1011 : h 30 else if(fquant_running > config->max_pquant)
1012 : Isibaar 3 {
1013 : h 30 fquant_running = (float) config->max_pquant;
1014 : Isibaar 3 }
1015 :    
1016 :     quant = (int) fquant_running;
1017 :    
1018 :     // add error between fquant and quant to fquant_running
1019 :     fquant_running += config->fquant - quant;
1020 :    
1021 :     return quant;
1022 :     }
1023 :    
1024 : Isibaar 364 // added by koepi for credits greyscale
1025 :    
1026 :     int check_greyscale_mode(CONFIG* config, XVID_ENC_FRAME* frame, int framenum)
1027 :    
1028 :     {
1029 :    
1030 :     if ((codec_is_in_credits(config, framenum)) && (config->mode!=DLG_MODE_CBR))
1031 :    
1032 :     {
1033 :    
1034 :     if (config->credits_greyscale)
1035 :    
1036 :     {
1037 :    
1038 :     if ((frame->general && XVID_GREYSCALE)) // use only if not already in greyscale
1039 :    
1040 :     frame->general |= XVID_GREYSCALE;
1041 :    
1042 :     } else {
1043 :    
1044 :     if (!(frame->general && XVID_GREYSCALE)) // if movie is in greyscale, switch back
1045 :    
1046 :     frame->general |= XVID_GREYSCALE;
1047 :    
1048 :     }
1049 :    
1050 :     } else {
1051 :    
1052 :     if (config->greyscale)
1053 :    
1054 :     {
1055 :    
1056 :     if ((frame->general && XVID_GREYSCALE)) // use only if not already in greyscale
1057 :    
1058 :     frame->general |= XVID_GREYSCALE;
1059 :    
1060 :     } else {
1061 :    
1062 :     if (!(frame->general && XVID_GREYSCALE)) // if credits is in greyscale, switch back
1063 :    
1064 :     frame->general |= XVID_GREYSCALE;
1065 :    
1066 :     }
1067 :    
1068 :     }
1069 :    
1070 :     return 0;
1071 :    
1072 :     }
1073 :    
1074 :     // end of koepi's addition
1075 :    

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