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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 926 - (view) (download)

1 : suxen_drol 889 /**************************************************************************
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 :     * 12.07.2002 num_threads
27 :     * 23.06.2002 XVID_CPU_CHKONLY; loading speed up
28 :     * 25.04.2002 ICDECOMPRESS_PREROLL
29 :     * 17.04.2002 re-enabled lumi masking for 1st pass
30 :     * 15.04.2002 updated cbr support
31 :     * 04.04.2002 separated 2-pass code to 2pass.c
32 :     * interlacing support
33 :     * hinted ME support
34 :     * 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 :     * 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 :     * 01.12.2001 inital version; (c)2001 peter ross <pross@xvid.org>
47 :     *
48 :     *************************************************************************/
49 :    
50 :     #include <windows.h>
51 :     #include <vfw.h>
52 :    
53 :     #include "codec.h"
54 :     #include "2pass.h"
55 :    
56 :     int pmvfast_presets[7] = {
57 :     0, 0, 0, 0,
58 :     0 | PMV_HALFPELREFINE16 | 0,
59 :     0 | PMV_HALFPELREFINE16 | 0 |
60 :     PMV_ADVANCEDDIAMOND16, PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 |
61 :     PMV_HALFPELREFINE8 | 0 | PMV_USESQUARES16
62 :     };
63 :    
64 :     /* return xvid compatbile colorspace,
65 :     or XVID_CSP_NULL if failure
66 :     */
67 :    
68 :     int get_colorspace(BITMAPINFOHEADER * hdr)
69 :     {
70 :     /* rgb only: negative height specifies top down image */
71 :     int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP);
72 :    
73 :     switch(hdr->biCompression)
74 :     {
75 :     case BI_RGB :
76 :     if (hdr->biBitCount == 16)
77 :     {
78 :     DEBUG("RGB16 (RGB555)");
79 :     return rgb_flip | XVID_CSP_RGB555;
80 :     }
81 :     if (hdr->biBitCount == 24)
82 :     {
83 :     DEBUG("RGB24");
84 :     return rgb_flip | XVID_CSP_BGR;
85 :     }
86 :     if (hdr->biBitCount == 32)
87 :     {
88 :     DEBUG("RGB32");
89 :     return rgb_flip | XVID_CSP_BGRA;
90 :     }
91 :    
92 :     DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount);
93 :     return XVID_CSP_NULL;
94 :    
95 :     case BI_BITFIELDS :
96 :     if (hdr->biSize >= sizeof(BITMAPV4HEADER))
97 :     {
98 :     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 :     }
121 :    
122 :     DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination");
123 :     return XVID_CSP_NULL;
124 :    
125 :     case FOURCC_I420 :
126 :     case FOURCC_IYUV :
127 :     DEBUG("IYUY");
128 :     return XVID_CSP_I420;
129 :    
130 :     case FOURCC_YV12 :
131 :     DEBUG("YV12");
132 :     return XVID_CSP_YV12;
133 :    
134 :     case FOURCC_YUYV :
135 :     case FOURCC_YUY2 :
136 :     DEBUG("YUY2");
137 :     return XVID_CSP_YUY2;
138 :    
139 :     case FOURCC_YVYU :
140 :     DEBUG("YVYU");
141 :     return XVID_CSP_YVYU;
142 :    
143 :     case FOURCC_UYVY :
144 :     DEBUG("UYVY");
145 :     return XVID_CSP_UYVY;
146 :    
147 :     default :
148 :     DEBUGFOURCC("unsupported colorspace", hdr->biCompression);
149 :     return XVID_CSP_NULL;
150 :     }
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 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
162 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
163 :    
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 :    
174 :     if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight ||
175 :     (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX && outhdr->biCompression != FOURCC_DX50))
176 :     {
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 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
187 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
188 :    
189 :     if (get_colorspace(inhdr) == XVID_CSP_NULL)
190 :     {
191 :     return ICERR_BADFORMAT;
192 :     }
193 :    
194 :     if (lpbiOutput == NULL)
195 :     {
196 :     return sizeof(BITMAPV4HEADER);
197 :     }
198 :    
199 :     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 :    
207 :     if (codec->config.fourcc_used == 0)
208 :     {
209 :     outhdr->biCompression = FOURCC_XVID;
210 :     }
211 :     else if (codec->config.fourcc_used == 1)
212 :     {
213 :     outhdr->biCompression = FOURCC_DIVX;
214 :     }
215 :     else
216 :     {
217 :     outhdr->biCompression = FOURCC_DX50;
218 :     }
219 :    
220 :     return ICERR_OK;
221 :     }
222 :    
223 :    
224 :     LRESULT compress_get_size(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
225 :     {
226 :     return 2 * lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3;
227 :     }
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_gbl_init_t init;
242 :     xvid_enc_create_t create;
243 :     xvid_enc_rc_t rc;
244 : suxen_drol 926 xvid_enc_plugin_t plugins[1];
245 : suxen_drol 889
246 :     memset(&rc, 0, sizeof(rc));
247 :     rc.version = XVID_VERSION;
248 :    
249 :     switch (codec->config.mode)
250 :     {
251 :     case DLG_MODE_CBR :
252 :     //rc.type = XVID_RC_CBR;
253 :     rc.min_iquant = codec->config.min_iquant;
254 :     rc.max_iquant = codec->config.max_iquant;
255 :     rc.min_pquant = codec->config.min_pquant;
256 :     rc.max_pquant = codec->config.max_pquant;
257 :     rc.min_bquant = 0; /* XXX: todo */
258 :     rc.max_bquant = 0; /* XXX: todo */
259 :     rc.data.cbr.bitrate = codec->config.rc_bitrate;
260 :     rc.data.cbr.reaction_delay_factor = codec->config.rc_reaction_delay_factor;
261 :     rc.data.cbr.averaging_period = codec->config.rc_averaging_period;
262 :     rc.data.cbr.buffer = codec->config.rc_buffer;
263 :     break;
264 :    
265 :     case DLG_MODE_VBR_QUAL :
266 :     codec->config.fquant = 0;
267 :     //param.rc_bitrate = 0;
268 :     break;
269 :    
270 :     case DLG_MODE_VBR_QUANT :
271 :     codec->config.fquant = (float) codec->config.quant;
272 :     //param.rc_bitrate = 0;
273 :     break;
274 :    
275 :     case DLG_MODE_2PASS_1 :
276 :     case DLG_MODE_2PASS_2_INT :
277 :     case DLG_MODE_2PASS_2_EXT :
278 :     //param.rc_bitrate = 0;
279 :     codec->twopass.max_framesize = (int)((double)codec->config.twopass_max_bitrate / 8.0 / ((double)codec->fbase / (double)codec->fincr));
280 :     break;
281 :    
282 :     case DLG_MODE_NULL :
283 :     return ICERR_OK;
284 :    
285 :     default :
286 :     break;
287 :     }
288 :    
289 :     /* destroy previously created codec */
290 :     if(codec->ehandle)
291 :     {
292 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
293 :     codec->ehandle = NULL;
294 :     }
295 :    
296 :     memset(&init, 0, sizeof(init));
297 :     init.version = XVID_VERSION;
298 :     init.cpu_flags = codec->config.cpu;
299 :     xvid_global(0, XVID_GBL_INIT, &init, NULL);
300 :    
301 :     memset(&create, 0, sizeof(create));
302 :     create.version = XVID_VERSION;
303 :    
304 :     create.width = lpbiInput->bmiHeader.biWidth;
305 :     create.height = lpbiInput->bmiHeader.biHeight;
306 :     create.fincr = codec->fincr;
307 :     create.fbase = codec->fbase;
308 :    
309 : suxen_drol 926 create.plugins = plugins;
310 :     create.num_plugins = 0;
311 :     if (codec->config.lum_masking) {
312 :     plugins[create.num_plugins].func = xvid_plugin_lumimasking;
313 :     plugins[create.num_plugins].param = NULL;
314 :     create.num_plugins++;
315 :     }
316 :    
317 : suxen_drol 889 if (codec->config.packed)
318 :     create.global |= XVID_PACKED;
319 :     if (codec->config.dx50bvop)
320 :     create.global |= XVID_CLOSED_GOP;
321 :    
322 :     create.max_key_interval = codec->config.max_key_interval;
323 :     /* XXX: param.min_quantizer = codec->config.min_pquant;
324 :     param.max_quantizer = codec->config.max_pquant; */
325 :    
326 :     create.max_bframes = codec->config.max_bframes;
327 :     create.frame_drop_ratio = codec->config.frame_drop_ratio;
328 :    
329 :     create.bquant_ratio = codec->config.bquant_ratio;
330 :     create.bquant_offset = codec->config.bquant_offset;
331 :    
332 :     create.num_threads = codec->config.num_threads;
333 :    
334 :     switch(xvid_encore(0, XVID_ENC_CREATE, &create, (codec->config.mode==DLG_MODE_CBR)?&rc:NULL ))
335 :     {
336 :     case XVID_ERR_FAIL :
337 :     return ICERR_ERROR;
338 :    
339 :     case XVID_ERR_MEMORY :
340 :     return ICERR_MEMORY;
341 :    
342 :     case XVID_ERR_FORMAT :
343 :     return ICERR_BADFORMAT;
344 :    
345 :     case XVID_ERR_VERSION :
346 :     return ICERR_UNSUPPORTED;
347 :     }
348 :    
349 :     codec->ehandle = create.handle;
350 :     codec->framenum = 0;
351 :     codec->keyspacing = 0;
352 :    
353 :     codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE;
354 :     codec->twopass.hintstream = NULL;
355 :    
356 :     return ICERR_OK;
357 :     }
358 :    
359 :    
360 :     LRESULT compress_end(CODEC * codec)
361 :     {
362 :     if (codec->ehandle != NULL)
363 :     {
364 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
365 :    
366 :     if (codec->twopass.hints != INVALID_HANDLE_VALUE)
367 :     {
368 :     CloseHandle(codec->twopass.hints);
369 :     }
370 :     if (codec->twopass.stats1 != INVALID_HANDLE_VALUE)
371 :     {
372 :     CloseHandle(codec->twopass.stats1);
373 :     }
374 :     if (codec->twopass.stats2 != INVALID_HANDLE_VALUE)
375 :     {
376 :     CloseHandle(codec->twopass.stats2);
377 :     }
378 :     if (codec->twopass.hintstream != NULL)
379 :     {
380 :     free(codec->twopass.hintstream);
381 :     }
382 :    
383 :     codec->ehandle = NULL;
384 :    
385 :     codec_2pass_finish(codec);
386 :     }
387 :    
388 :     return ICERR_OK;
389 :     }
390 :    
391 :     LRESULT compress(CODEC * codec, ICCOMPRESS * icc)
392 :     {
393 :     BITMAPINFOHEADER * inhdr = icc->lpbiInput;
394 :     BITMAPINFOHEADER * outhdr = icc->lpbiOutput;
395 :     xvid_enc_frame_t frame;
396 :     xvid_enc_stats_t stats;
397 :     int length;
398 :    
399 :     // mpeg2avi yuv bug workaround (2 instances of CODEC)
400 :     if (codec->twopass.stats1 == INVALID_HANDLE_VALUE)
401 :     {
402 :     if (codec_2pass_init(codec) == ICERR_ERROR)
403 :     {
404 :     return ICERR_ERROR;
405 :     }
406 :     }
407 :    
408 :     memset(&frame, 0, sizeof(frame));
409 :     frame.version = XVID_VERSION;
410 :    
411 :     frame.type = XVID_TYPE_AUTO;
412 :    
413 :     /* vol stuff */
414 :    
415 :     if (codec->config.quant_type != QUANT_MODE_H263)
416 :     {
417 :     frame.vol_flags |= XVID_MPEGQUANT;
418 :    
419 :     // we actually need "default/custom" selectbox for both inter/intra
420 :     // this will do for now
421 :     if (codec->config.quant_type == QUANT_MODE_CUSTOM)
422 :     {
423 :     frame.quant_intra_matrix = codec->config.qmatrix_intra;
424 :     frame.quant_inter_matrix = codec->config.qmatrix_inter;
425 :     }
426 :     else
427 :     {
428 :     frame.quant_intra_matrix = NULL;
429 :     frame.quant_inter_matrix = NULL;
430 :     }
431 :     }
432 :    
433 :     if (codec->config.reduced_resolution) {
434 :     frame.vol_flags |= XVID_REDUCED_ENABLE;
435 :     frame.vop_flags |= XVID_REDUCED; /* XXX: need auto decion mode */
436 :     }
437 :    
438 :     if (codec->config.qpel) {
439 :     frame.vol_flags |= XVID_QUARTERPEL;
440 :     frame.motion |= PMV_QUARTERPELREFINE16 | PMV_QUARTERPELREFINE8;
441 :     }
442 :    
443 :     if (codec->config.gmc)
444 :     frame.vol_flags |= XVID_GMC;
445 :    
446 :     if (codec->config.interlacing)
447 :     frame.vol_flags |= XVID_INTERLACING;
448 :    
449 :     /* vop stuff */
450 :    
451 :     frame.vop_flags |= XVID_HALFPEL;
452 :     frame.vop_flags |= XVID_HQACPRED;
453 :    
454 :     if (codec->config.debug)
455 :     frame.vop_flags |= XVID_DEBUG;
456 :    
457 :     if (codec->config.motion_search > 4)
458 :     frame.vop_flags |= XVID_INTER4V;
459 :    
460 :     if (codec->config.chromame)
461 :     frame.vop_flags |= PMV_CHROMA16 + PMV_CHROMA8;
462 :    
463 :     if (codec->config.chroma_opt)
464 :     frame.vop_flags |= XVID_CHROMAOPT;
465 :    
466 :     check_greyscale_mode(&codec->config, &frame, codec->framenum);
467 :    
468 :     /* XXX: hinted me
469 :    
470 :     // fix 1pass modes/hinted MV by koepi
471 :     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))
472 :     {
473 :     codec->config.hinted_me = 0;
474 :     }
475 :     // end of ugly hack
476 :    
477 :     if (codec->config.hinted_me && codec->config.mode == DLG_MODE_2PASS_1)
478 :     {
479 :     frame.hint.hintstream = codec->twopass.hintstream;
480 :     frame.hint.rawhints = 0;
481 :     frame.general |= XVID_HINTEDME_GET;
482 :     }
483 :     else if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT))
484 :     {
485 :     DWORD read;
486 :     DWORD blocksize;
487 :    
488 :     frame.hint.hintstream = codec->twopass.hintstream;
489 :     frame.hint.rawhints = 0;
490 :     frame.general |= XVID_HINTEDME_SET;
491 :    
492 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
493 :     {
494 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
495 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
496 :     {
497 :     DEBUGERR("couldn't open hints file");
498 :     return ICERR_ERROR;
499 :     }
500 :     }
501 :     if (!ReadFile(codec->twopass.hints, &blocksize, sizeof(DWORD), &read, 0) || read != sizeof(DWORD) ||
502 :     !ReadFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &read, 0) || read != blocksize)
503 :     {
504 :     DEBUGERR("couldn't read from hints file");
505 :     return ICERR_ERROR;
506 :     }
507 :     } */
508 :    
509 :     frame.motion |= pmvfast_presets[codec->config.motion_search];
510 :    
511 :     switch (codec->config.vhq_mode)
512 :     {
513 :     case VHQ_MODE_DECISION :
514 :     frame.vop_flags |= XVID_MODEDECISION_BITS;
515 :     break;
516 :    
517 :     case VHQ_LIMITED_SEARCH :
518 :     frame.vop_flags |= XVID_MODEDECISION_BITS;
519 :     frame.motion |= HALFPELREFINE16_BITS;
520 :     frame.motion |= QUARTERPELREFINE16_BITS;
521 :     break;
522 :    
523 :     case VHQ_MEDIUM_SEARCH :
524 :     frame.vop_flags |= XVID_MODEDECISION_BITS;
525 :     frame.motion |= HALFPELREFINE16_BITS;
526 :     frame.motion |= HALFPELREFINE8_BITS;
527 :     frame.motion |= QUARTERPELREFINE16_BITS;
528 :     frame.motion |= QUARTERPELREFINE8_BITS;
529 :     frame.motion |= CHECKPREDICTION_BITS;
530 :     break;
531 :    
532 :     case VHQ_WIDE_SEARCH :
533 :     frame.vop_flags |= XVID_MODEDECISION_BITS;
534 :     frame.motion |= HALFPELREFINE16_BITS;
535 :     frame.motion |= HALFPELREFINE8_BITS;
536 :     frame.motion |= QUARTERPELREFINE16_BITS;
537 :     frame.motion |= QUARTERPELREFINE8_BITS;
538 :     frame.motion |= CHECKPREDICTION_BITS;
539 :     frame.motion |= EXTSEARCH_BITS;
540 :     break;
541 :    
542 :     default :
543 :     break;
544 :     }
545 :    
546 :     frame.input.plane[0] = icc->lpInput;
547 :     frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3;
548 :    
549 :     if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL)
550 :     return ICERR_BADFORMAT;
551 :    
552 :     if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12)
553 :     frame.input.stride[0] = (frame.input.stride[0]*2)/3;
554 :    
555 :     frame.bitstream = icc->lpOutput;
556 :     frame.length = icc->lpbiOutput->biSizeImage;
557 :    
558 :     switch (codec->config.mode)
559 :     {
560 :     case DLG_MODE_CBR :
561 :     frame.quant = 0; /* use xvidcore cbr rate control */
562 :     break;
563 :    
564 :     case DLG_MODE_VBR_QUAL :
565 :     case DLG_MODE_VBR_QUANT :
566 :     case DLG_MODE_2PASS_1 :
567 :     if (codec_get_quant(codec, &frame) == ICERR_ERROR)
568 :     {
569 :     return ICERR_ERROR;
570 :     }
571 :     break;
572 :    
573 :     case DLG_MODE_2PASS_2_EXT :
574 :     case DLG_MODE_2PASS_2_INT :
575 :     if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR)
576 :     {
577 :     return ICERR_ERROR;
578 :     }
579 :     if (codec->config.dummy2pass)
580 :     {
581 :     outhdr->biSizeImage = codec->twopass.bytes2;
582 :     *icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0;
583 :     return ICERR_OK;
584 :     }
585 :     break;
586 :    
587 :     case DLG_MODE_NULL :
588 :     outhdr->biSizeImage = 0;
589 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
590 :     return ICERR_OK;
591 :    
592 :     default :
593 :     DEBUGERR("Invalid encoding mode");
594 :     return ICERR_ERROR;
595 :     }
596 :    
597 :    
598 :     // force keyframe spacing in 2-pass 1st pass
599 :     if (codec->config.motion_search == 0)
600 :     {
601 :     frame.type = XVID_TYPE_IVOP;
602 :     }
603 :     else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum)
604 :     {
605 :     DEBUG("current frame forced to p-frame");
606 :     frame.type = XVID_TYPE_PVOP;
607 :     }
608 :    
609 :     memset(&stats, 0, sizeof(stats));
610 :     stats.version = XVID_VERSION;
611 :    
612 :     length = xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats);
613 :     switch (length)
614 :     {
615 :     case XVID_ERR_FAIL :
616 :     return ICERR_ERROR;
617 :    
618 :     case XVID_ERR_MEMORY :
619 :     return ICERR_MEMORY;
620 :    
621 :     case XVID_ERR_FORMAT :
622 :     return ICERR_BADFORMAT;
623 :    
624 :     case XVID_ERR_VERSION :
625 :     return ICERR_UNSUPPORTED;
626 :     }
627 :    
628 :     { /* XXX: debug text */
629 :     char tmp[100];
630 :     wsprintf(tmp, " {type=%i len=%i} length=%i", stats.type, stats.length, length);
631 :     OutputDebugString(tmp);
632 :     }
633 :    
634 :     if (length == 0) /* no encoder output */
635 :     {
636 :     *icc->lpdwFlags = 0;
637 :     ((char*)icc->lpOutput)[0] = 0x7f; /* virtual dub skip frame */
638 :     outhdr->biSizeImage = 1;
639 :    
640 :     }else{
641 :     if (frame.out_flags & XVID_KEYFRAME)
642 :     {
643 :     codec->keyspacing = 0;
644 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
645 :     }
646 :     else
647 :     {
648 :     *icc->lpdwFlags = 0;
649 :     }
650 :    
651 :     outhdr->biSizeImage = length;
652 :    
653 :     if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass)
654 :     {
655 :     outhdr->biSizeImage = 0;
656 :     }
657 :    
658 :     /* XXX: hinted me
659 :     if (frame.general & XVID_HINTEDME_GET)
660 :     {
661 :     DWORD wrote;
662 :     DWORD blocksize = frame.hint.hintlength;
663 :    
664 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
665 :     {
666 :     codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
667 :     if (codec->twopass.hints == INVALID_HANDLE_VALUE)
668 :     {
669 :     DEBUGERR("couldn't create hints file");
670 :     return ICERR_ERROR;
671 :     }
672 :     }
673 :     if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) ||
674 :     !WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize)
675 :     {
676 :     DEBUGERR("couldn't write to hints file");
677 :     return ICERR_ERROR;
678 :     }
679 :     }
680 :     */
681 :    
682 :     if (stats.type > 0)
683 :     {
684 :     codec_2pass_update(codec, &stats);
685 :     }
686 :     }
687 :    
688 :     ++codec->framenum;
689 :     ++codec->keyspacing;
690 :    
691 :     return ICERR_OK;
692 :     }
693 :    
694 :    
695 :     /* decompressor */
696 :    
697 :    
698 :     LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
699 :     {
700 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
701 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
702 :    
703 :     if (lpbiInput == NULL)
704 :     {
705 :     return ICERR_ERROR;
706 :     }
707 :    
708 :     if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL)
709 :     {
710 :     return ICERR_BADFORMAT;
711 :     }
712 :    
713 :     if (lpbiOutput == NULL)
714 :     {
715 :     return ICERR_OK;
716 :     }
717 :    
718 :     if (inhdr->biWidth != outhdr->biWidth ||
719 :     inhdr->biHeight != outhdr->biHeight ||
720 :     get_colorspace(outhdr) == XVID_CSP_NULL)
721 :     {
722 :     return ICERR_BADFORMAT;
723 :     }
724 :    
725 :     return ICERR_OK;
726 :     }
727 :    
728 :    
729 :     LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
730 :     {
731 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
732 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
733 :     LRESULT result;
734 :    
735 :     if (lpbiOutput == NULL)
736 :     {
737 :     return sizeof(BITMAPINFOHEADER);
738 :     }
739 :    
740 :     /* --- yv12 --- */
741 :    
742 :     if (get_colorspace(inhdr) != XVID_CSP_NULL) {
743 :     memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
744 :     // XXX: should we set outhdr->biSize ??
745 :     return ICERR_OK;
746 :     }
747 :     /* --- yv12 --- */
748 :    
749 :     result = decompress_query(codec, lpbiInput, lpbiOutput);
750 :     if (result != ICERR_OK)
751 :     {
752 :     return result;
753 :     }
754 :    
755 :     outhdr->biSize = sizeof(BITMAPINFOHEADER);
756 :     outhdr->biWidth = inhdr->biWidth;
757 :     outhdr->biHeight = inhdr->biHeight;
758 :     outhdr->biPlanes = 1;
759 :     outhdr->biBitCount = 24;
760 :     outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */
761 :     outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
762 :     outhdr->biXPelsPerMeter = 0;
763 :     outhdr->biYPelsPerMeter = 0;
764 :     outhdr->biClrUsed = 0;
765 :     outhdr->biClrImportant = 0;
766 :    
767 :     return ICERR_OK;
768 :     }
769 :    
770 :    
771 :     LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
772 :     {
773 :     xvid_gbl_init_t init;
774 :     xvid_dec_create_t create;
775 :    
776 :     memset(&init, 0, sizeof(init));
777 :     init.version = XVID_VERSION;
778 :     init.cpu_flags = codec->config.cpu;
779 :     xvid_global(0, XVID_GBL_INIT, &init, NULL);
780 :    
781 :     memset(&create, 0, sizeof(create));
782 :     create.version = XVID_VERSION;
783 :     create.width = lpbiInput->bmiHeader.biWidth;
784 :     create.height = lpbiInput->bmiHeader.biHeight;
785 :    
786 :     switch(xvid_decore(0, XVID_DEC_CREATE, &create, NULL))
787 :     {
788 :     case XVID_ERR_FAIL :
789 :     return ICERR_ERROR;
790 :    
791 :     case XVID_ERR_MEMORY :
792 :     return ICERR_MEMORY;
793 :    
794 :     case XVID_ERR_FORMAT :
795 :     return ICERR_BADFORMAT;
796 :    
797 :     case XVID_ERR_VERSION :
798 :     return ICERR_UNSUPPORTED;
799 :     }
800 :    
801 :     codec->dhandle = create.handle;
802 :    
803 :     return ICERR_OK;
804 :     }
805 :    
806 :    
807 :     LRESULT decompress_end(CODEC * codec)
808 :     {
809 :     if (codec->dhandle != NULL)
810 :     {
811 :     xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
812 :     codec->dhandle = NULL;
813 :     }
814 :     return ICERR_OK;
815 :     }
816 :    
817 :    
818 :     LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
819 :     {
820 :     xvid_dec_frame_t frame;
821 :    
822 :     /* --- yv12 --- */
823 :     if (icd->lpbiInput->biCompression != FOURCC_XVID &&
824 :     icd->lpbiInput->biCompression != FOURCC_DIVX &&
825 :     icd->lpbiInput->biCompression != FOURCC_DX50)
826 :     {
827 :     xvid_gbl_convert_t convert;
828 :    
829 :     DEBUGFOURCC("input", icd->lpbiInput->biCompression);
830 :     DEBUGFOURCC("output", icd->lpbiOutput->biCompression);
831 :    
832 :     memset(&convert, 0, sizeof(convert));
833 :     convert.version = XVID_VERSION;
834 :    
835 :     convert.input.csp = get_colorspace(icd->lpbiInput);
836 :     convert.input.plane[0] = icd->lpInput;
837 :     convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3;
838 :     if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12)
839 :     convert.input.stride[0] = (convert.input.stride[0]*2)/3;
840 :    
841 :     convert.output.csp = get_colorspace(icd->lpbiOutput);
842 :     convert.output.plane[0] = icd->lpOutput;
843 :     convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
844 :     if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12)
845 :     convert.output.stride[0] = (convert.output.stride[0]*2)/3;
846 :    
847 :     convert.width = icd->lpbiInput->biWidth;
848 :     convert.height = icd->lpbiInput->biHeight;
849 :     convert.interlacing = 0;
850 :     if (convert.input.csp == XVID_CSP_NULL ||
851 :     convert.output.csp == XVID_CSP_NULL ||
852 :     xvid_global(0, XVID_GBL_CONVERT, &convert, NULL) < 0)
853 :     {
854 :     return ICERR_BADFORMAT;
855 :     }
856 :     return ICERR_OK;
857 :     }
858 :     /* --- yv12 --- */
859 :    
860 :     memset(&frame, 0, sizeof(frame));
861 :     frame.version = XVID_VERSION;
862 :     frame.general = XVID_LOWDELAY; /* force low_delay_default mode */
863 :     frame.bitstream = icd->lpInput;
864 :     frame.length = icd->lpbiInput->biSizeImage;
865 :    
866 :     if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
867 :     {
868 :     if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
869 :     {
870 :     return ICERR_BADFORMAT;
871 :     }
872 :     frame.output.plane[0] = icd->lpOutput;
873 :     frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
874 :     if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12)
875 :     frame.output.stride[0] = (frame.output.stride[0]*2)/3;
876 :     }
877 :     else
878 :     {
879 :     frame.output.csp = XVID_CSP_NULL;
880 :     }
881 :    
882 :     switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
883 :     {
884 :     case XVID_ERR_FAIL :
885 :     return ICERR_ERROR;
886 :    
887 :     case XVID_ERR_MEMORY :
888 :     return ICERR_MEMORY;
889 :    
890 :     case XVID_ERR_FORMAT :
891 :     return ICERR_BADFORMAT;
892 :    
893 :     case XVID_ERR_VERSION :
894 :     return ICERR_UNSUPPORTED;
895 :     }
896 :    
897 :     return ICERR_OK;
898 :     }
899 :    
900 :     int codec_get_quant(CODEC* codec, xvid_enc_frame_t* frame)
901 :     {
902 :     switch (codec->config.mode)
903 :     {
904 :     case DLG_MODE_VBR_QUAL :
905 :     if (codec_is_in_credits(&codec->config, codec->framenum))
906 :     {
907 :     // added by koepi for credits greyscale
908 :    
909 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
910 :    
911 :     // end of koepi's addition
912 :    
913 :     switch (codec->config.credits_mode)
914 :     {
915 :     case CREDITS_MODE_RATE :
916 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100);
917 :     break;
918 :    
919 :     case CREDITS_MODE_QUANT :
920 :     frame->quant = codec->config.credits_quant_p;
921 :     break;
922 :    
923 :     default :
924 :     DEBUGERR("Can't use credits size mode in quality mode");
925 :     return ICERR_ERROR;
926 :     }
927 :     }
928 :     else
929 :     {
930 :     // added by koepi for credits greyscale
931 :    
932 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
933 :    
934 :     // end of koepi's addition
935 :    
936 :     frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality);
937 :     }
938 :     return ICERR_OK;
939 :    
940 :     case DLG_MODE_VBR_QUANT :
941 :     if (codec_is_in_credits(&codec->config, codec->framenum))
942 :     {
943 :     // added by koepi for credits greyscale
944 :    
945 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
946 :    
947 :     // end of koepi's addition
948 :    
949 :     switch (codec->config.credits_mode)
950 :     {
951 :     case CREDITS_MODE_RATE :
952 :     frame->quant =
953 :     codec->config.max_pquant -
954 :     ((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100);
955 :     break;
956 :    
957 :     case CREDITS_MODE_QUANT :
958 :     frame->quant = codec->config.credits_quant_p;
959 :     break;
960 :    
961 :     default :
962 :     DEBUGERR("Can't use credits size mode in quantizer mode");
963 :     return ICERR_ERROR;
964 :     }
965 :     }
966 :     else
967 :     {
968 :     // added by koepi for credits greyscale
969 :    
970 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
971 :    
972 :     // end of koepi's addition
973 :    
974 :     frame->quant = codec->config.quant;
975 :     }
976 :     return ICERR_OK;
977 :    
978 :     case DLG_MODE_2PASS_1 :
979 :     // added by koepi for credits greyscale
980 :    
981 :     check_greyscale_mode(&codec->config, frame, codec->framenum);
982 :    
983 :     // end of koepi's addition
984 :    
985 :     if (codec->config.credits_mode == CREDITS_MODE_QUANT)
986 :     {
987 :     if (codec_is_in_credits(&codec->config, codec->framenum))
988 :     {
989 :     frame->quant = codec->config.credits_quant_p;
990 :     }
991 :     else
992 :     {
993 :     frame->quant = 2;
994 :     }
995 :     }
996 :     else
997 :     {
998 :     frame->quant = 2;
999 :     }
1000 :     return ICERR_OK;
1001 :    
1002 :     default:
1003 :     DEBUGERR("get quant: invalid mode");
1004 :     return ICERR_ERROR;
1005 :     }
1006 :     }
1007 :    
1008 :    
1009 :     int codec_is_in_credits(CONFIG* config, int framenum)
1010 :     {
1011 :     if (config->credits_start)
1012 :     {
1013 :     if (framenum >= config->credits_start_begin &&
1014 :     framenum <= config->credits_start_end)
1015 :     {
1016 :     return CREDITS_START;
1017 :     }
1018 :     }
1019 :    
1020 :     if (config->credits_end)
1021 :     {
1022 :     if (framenum >= config->credits_end_begin &&
1023 :     framenum <= config->credits_end_end)
1024 :     {
1025 :     return CREDITS_END;
1026 :     }
1027 :     }
1028 :    
1029 :     return 0;
1030 :     }
1031 :    
1032 :    
1033 :     int codec_get_vbr_quant(CONFIG* config, int quality)
1034 :     {
1035 :     static float fquant_running = 0;
1036 :     static int my_quality = -1;
1037 :     int quant;
1038 :    
1039 :     // if quality changes, recalculate fquant (credits)
1040 :     if (quality != my_quality)
1041 :     {
1042 :     config->fquant = 0;
1043 :     }
1044 :    
1045 :     my_quality = quality;
1046 :    
1047 :     // desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ
1048 :     if (!config->fquant)
1049 :     {
1050 :     config->fquant =
1051 :     ((float) (config->max_pquant - config->min_pquant) / 100) *
1052 :     (100 - quality) +
1053 :     (float) config->min_pquant;
1054 :    
1055 :     fquant_running = config->fquant;
1056 :     }
1057 :    
1058 :     if (fquant_running < config->min_pquant)
1059 :     {
1060 :     fquant_running = (float) config->min_pquant;
1061 :     }
1062 :     else if(fquant_running > config->max_pquant)
1063 :     {
1064 :     fquant_running = (float) config->max_pquant;
1065 :     }
1066 :    
1067 :     quant = (int) fquant_running;
1068 :    
1069 :     // add error between fquant and quant to fquant_running
1070 :     fquant_running += config->fquant - quant;
1071 :    
1072 :     return quant;
1073 :     }
1074 :    
1075 :     // added by koepi for credits greyscale
1076 :    
1077 :     int check_greyscale_mode(CONFIG* config, xvid_enc_frame_t* frame, int framenum)
1078 :    
1079 :     {
1080 :    
1081 :     if ((codec_is_in_credits(config, framenum)) && (config->mode!=DLG_MODE_CBR))
1082 :    
1083 :     {
1084 :    
1085 :     if (config->credits_greyscale)
1086 :    
1087 :     {
1088 :    
1089 :     if ((frame->vop_flags && XVID_GREYSCALE)) // use only if not already in greyscale
1090 :    
1091 :     frame->vop_flags |= XVID_GREYSCALE;
1092 :    
1093 :     } else {
1094 :    
1095 :     if (!(frame->vop_flags && XVID_GREYSCALE)) // if movie is in greyscale, switch back
1096 :    
1097 :     frame->vop_flags |= XVID_GREYSCALE;
1098 :    
1099 :     }
1100 :    
1101 :     } else {
1102 :    
1103 :     if (config->greyscale)
1104 :    
1105 :     {
1106 :    
1107 :     if ((frame->vop_flags && XVID_GREYSCALE)) // use only if not already in greyscale
1108 :    
1109 :     frame->vop_flags |= XVID_GREYSCALE;
1110 :    
1111 :     } else {
1112 :    
1113 :     if (!(frame->vop_flags && XVID_GREYSCALE)) // if credits is in greyscale, switch back
1114 :    
1115 :     frame->vop_flags |= XVID_GREYSCALE;
1116 :    
1117 :     }
1118 :    
1119 :     }
1120 :    
1121 :     return 0;
1122 :    
1123 :     }
1124 :    
1125 :     // end of koepi's addition
1126 :    

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