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

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