[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 1053 - (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 : suxen_drol 1017 #include "vfwext.h"
53 : suxen_drol 889
54 : suxen_drol 983 #include <xvid.h>
55 :     #include "debug.h"
56 : suxen_drol 889 #include "codec.h"
57 :    
58 : suxen_drol 983
59 :     static const int pmvfast_presets[7] = {
60 : suxen_drol 889 0, 0, 0, 0,
61 : edgomez 949 0 | XVID_ME_HALFPELREFINE16 | 0,
62 :     0 | XVID_ME_HALFPELREFINE16 | 0 |
63 :     XVID_ME_ADVANCEDDIAMOND16, XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |
64 :     XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16
65 : suxen_drol 889 };
66 :    
67 : suxen_drol 983
68 :    
69 : suxen_drol 889 /* return xvid compatbile colorspace,
70 :     or XVID_CSP_NULL if failure
71 :     */
72 :    
73 :     int get_colorspace(BITMAPINFOHEADER * hdr)
74 :     {
75 :     /* rgb only: negative height specifies top down image */
76 :     int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP);
77 :    
78 :     switch(hdr->biCompression)
79 :     {
80 :     case BI_RGB :
81 :     if (hdr->biBitCount == 16)
82 :     {
83 : suxen_drol 983 DPRINTF("RGB16 (RGB555)");
84 : suxen_drol 889 return rgb_flip | XVID_CSP_RGB555;
85 :     }
86 :     if (hdr->biBitCount == 24)
87 :     {
88 : suxen_drol 983 DPRINTF("RGB24");
89 : suxen_drol 889 return rgb_flip | XVID_CSP_BGR;
90 :     }
91 :     if (hdr->biBitCount == 32)
92 :     {
93 : suxen_drol 983 DPRINTF("RGB32");
94 : suxen_drol 889 return rgb_flip | XVID_CSP_BGRA;
95 :     }
96 :    
97 : suxen_drol 983 DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount);
98 : suxen_drol 889 return XVID_CSP_NULL;
99 :    
100 :     case BI_BITFIELDS :
101 :     if (hdr->biSize >= sizeof(BITMAPV4HEADER))
102 :     {
103 :     BITMAPV4HEADER * hdr4 = (BITMAPV4HEADER *)hdr;
104 :    
105 :     if (hdr4->bV4BitCount == 16 &&
106 :     hdr4->bV4RedMask == 0x7c00 &&
107 :     hdr4->bV4GreenMask == 0x3e0 &&
108 :     hdr4->bV4BlueMask == 0x1f)
109 :     {
110 : suxen_drol 983 DPRINTF("RGB555");
111 : suxen_drol 889 return rgb_flip | XVID_CSP_RGB555;
112 :     }
113 :    
114 :     if(hdr4->bV4BitCount == 16 &&
115 :     hdr4->bV4RedMask == 0xf800 &&
116 :     hdr4->bV4GreenMask == 0x7e0 &&
117 :     hdr4->bV4BlueMask == 0x1f)
118 :     {
119 : suxen_drol 983 DPRINTF("RGB565");
120 : suxen_drol 889 return rgb_flip | XVID_CSP_RGB565;
121 :     }
122 :    
123 : suxen_drol 983 DPRINTF("unsupported BI_BITFIELDS mode");
124 : suxen_drol 889 return XVID_CSP_NULL;
125 :     }
126 :    
127 : suxen_drol 983 DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination");
128 : suxen_drol 889 return XVID_CSP_NULL;
129 :    
130 :     case FOURCC_I420 :
131 :     case FOURCC_IYUV :
132 : suxen_drol 983 DPRINTF("IYUY");
133 : suxen_drol 889 return XVID_CSP_I420;
134 :    
135 :     case FOURCC_YV12 :
136 : suxen_drol 983 DPRINTF("YV12");
137 : suxen_drol 889 return XVID_CSP_YV12;
138 :    
139 :     case FOURCC_YUYV :
140 :     case FOURCC_YUY2 :
141 : suxen_drol 983 DPRINTF("YUY2");
142 : suxen_drol 889 return XVID_CSP_YUY2;
143 :    
144 :     case FOURCC_YVYU :
145 : suxen_drol 983 DPRINTF("YVYU");
146 : suxen_drol 889 return XVID_CSP_YVYU;
147 :    
148 :     case FOURCC_UYVY :
149 : suxen_drol 983 DPRINTF("UYVY");
150 : suxen_drol 889 return XVID_CSP_UYVY;
151 :    
152 :     default :
153 : suxen_drol 983 DPRINTF("unsupported colorspace %c%c%c%c",
154 :     hdr->biCompression&0xff,
155 :     (hdr->biCompression>>8)&0xff,
156 :     (hdr->biCompression>>16)&0xff,
157 :     (hdr->biCompression>>24)&0xff);
158 : suxen_drol 889 return XVID_CSP_NULL;
159 :     }
160 :     }
161 :    
162 :    
163 :     /* compressor */
164 :    
165 :    
166 :     /* test the output format */
167 :    
168 :     LRESULT compress_query(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
169 :     {
170 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
171 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
172 :    
173 : suxen_drol 1017 /* VFWEXT detection */
174 :     if (inhdr->biCompression == VFWEXT_FOURCC) {
175 :     return (ICM_USER+0x0fff);
176 :     }
177 :    
178 : suxen_drol 889 if (get_colorspace(inhdr) == XVID_CSP_NULL)
179 :     {
180 :     return ICERR_BADFORMAT;
181 :     }
182 :    
183 :     if (lpbiOutput == NULL)
184 :     {
185 :     return ICERR_OK;
186 :     }
187 :    
188 :     if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight ||
189 :     (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX && outhdr->biCompression != FOURCC_DX50))
190 :     {
191 :     return ICERR_BADFORMAT;
192 :     }
193 :    
194 :     return ICERR_OK;
195 :     }
196 :    
197 :    
198 :     LRESULT compress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
199 :     {
200 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
201 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
202 :    
203 :     if (get_colorspace(inhdr) == XVID_CSP_NULL)
204 :     {
205 :     return ICERR_BADFORMAT;
206 :     }
207 :    
208 :     if (lpbiOutput == NULL)
209 :     {
210 :     return sizeof(BITMAPV4HEADER);
211 :     }
212 :    
213 :     memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
214 :     outhdr->biSize = sizeof(BITMAPINFOHEADER);
215 :     outhdr->biSizeImage = compress_get_size(codec, lpbiInput, lpbiOutput);
216 :     outhdr->biXPelsPerMeter = 0;
217 :     outhdr->biYPelsPerMeter = 0;
218 :     outhdr->biClrUsed = 0;
219 :     outhdr->biClrImportant = 0;
220 :    
221 :     if (codec->config.fourcc_used == 0)
222 :     {
223 :     outhdr->biCompression = FOURCC_XVID;
224 :     }
225 :     else if (codec->config.fourcc_used == 1)
226 :     {
227 :     outhdr->biCompression = FOURCC_DIVX;
228 :     }
229 :     else
230 :     {
231 :     outhdr->biCompression = FOURCC_DX50;
232 :     }
233 :    
234 :     return ICERR_OK;
235 :     }
236 :    
237 :    
238 :     LRESULT compress_get_size(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
239 :     {
240 :     return 2 * lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3;
241 :     }
242 :    
243 :    
244 :     LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf)
245 :     {
246 : edgomez 1053 #if 0
247 :     DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount);
248 :     #endif
249 : suxen_drol 889 codec->fincr = icf->dwScale;
250 :     codec->fbase = icf->dwRate;
251 :     return ICERR_OK;
252 :     }
253 :    
254 :    
255 : suxen_drol 983 const char type2char(int type)
256 :     {
257 :     if (type==XVID_TYPE_IVOP)
258 :     return 'I';
259 :     if (type==XVID_TYPE_PVOP)
260 :     return 'P';
261 :     if (type==XVID_TYPE_BVOP)
262 :     return 'B';
263 :     return 'S';
264 :     }
265 :    
266 :     int vfw_debug(void *handle,
267 :     int opt,
268 :     void *param1,
269 :     void *param2)
270 :     {
271 :     switch (opt) {
272 :     case XVID_PLG_INFO:
273 :     case XVID_PLG_CREATE:
274 :     case XVID_PLG_DESTROY:
275 :     case XVID_PLG_BEFORE:
276 :     return 0;
277 :    
278 :     case XVID_PLG_AFTER:
279 :     {
280 :     xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
281 :    
282 :     DPRINTF("[%5i] type=%c Q:%2i length:%6i",
283 :     data->frame_num,
284 :     type2char(data->type),
285 :     data->quant,
286 :     data->length);
287 :     return 0;
288 :     }
289 :     }
290 :    
291 :     return XVID_ERR_FAIL;
292 :     }
293 :    
294 :    
295 :    
296 : suxen_drol 889 LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
297 :     {
298 :     xvid_gbl_init_t init;
299 :     xvid_enc_create_t create;
300 : suxen_drol 983 xvid_enc_plugin_t plugins[3];
301 : suxen_drol 1017 xvid_plugin_single_t single;
302 : suxen_drol 983 xvid_plugin_2pass1_t pass1;
303 :     xvid_plugin_2pass2_t pass2;
304 : suxen_drol 1017 int i;
305 : suxen_drol 889
306 : suxen_drol 983 /* destroy previously created codec */
307 :     if(codec->ehandle) {
308 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
309 :     codec->ehandle = NULL;
310 :     }
311 :    
312 :     memset(&init, 0, sizeof(init));
313 :     init.version = XVID_VERSION;
314 :     init.cpu_flags = codec->config.cpu;
315 : suxen_drol 1032 init.debug = codec->config.debug;
316 : suxen_drol 983 xvid_global(0, XVID_GBL_INIT, &init, NULL);
317 :    
318 :     memset(&create, 0, sizeof(create));
319 :     create.version = XVID_VERSION;
320 : suxen_drol 1017
321 : edgomez 1053 /* zones */
322 : suxen_drol 1017 create.zones = malloc(sizeof(xvid_enc_zone_t) * codec->config.num_zones);
323 :     create.num_zones = codec->config.num_zones;
324 :     for (i=0; i < create.num_zones; i++) {
325 :     create.zones[i].frame = codec->config.zones[i].frame;
326 : suxen_drol 1032 if (codec->config.zones[i].mode == RC_ZONE_QUANT) {
327 : suxen_drol 1017 create.zones[i].mode = XVID_ZONE_QUANT;
328 :     create.zones[i].increment = codec->config.zones[i].quant;
329 :     }else{
330 :     create.zones[i].mode = XVID_ZONE_WEIGHT;
331 :     create.zones[i].increment = codec->config.zones[i].weight;
332 :     }
333 :     create.zones[i].base = 100;
334 :     }
335 :    
336 : edgomez 1053 /* plugins */
337 : suxen_drol 983 create.plugins = plugins;
338 : suxen_drol 889 switch (codec->config.mode)
339 :     {
340 : suxen_drol 1017 case RC_MODE_1PASS :
341 :     memset(&single, 0, sizeof(single));
342 :     single.version = XVID_VERSION;
343 : suxen_drol 1032 single.bitrate = codec->config.bitrate * CONFIG_KBPS;
344 : suxen_drol 1017 single.reaction_delay_factor = codec->config.rc_reaction_delay_factor;
345 :     single.averaging_period = codec->config.rc_averaging_period;
346 :     single.buffer = codec->config.rc_buffer;
347 :     plugins[create.num_plugins].func = xvid_plugin_single;
348 :     plugins[create.num_plugins].param = &single;
349 : suxen_drol 983 create.num_plugins++;
350 : suxen_drol 1032 break;
351 : suxen_drol 889
352 : suxen_drol 983 case RC_MODE_2PASS1 :
353 :     memset(&pass1, 0, sizeof(pass1));
354 :     pass1.version = XVID_VERSION;
355 : suxen_drol 1028 pass1.filename = codec->config.stats;
356 : suxen_drol 983
357 :     plugins[create.num_plugins].func = xvid_plugin_2pass1;
358 :     plugins[create.num_plugins].param = &pass1;
359 :     create.num_plugins++;
360 : suxen_drol 889 break;
361 :    
362 : suxen_drol 1017 case RC_MODE_2PASS2 :
363 : suxen_drol 983 memset(&pass2, 0, sizeof(pass2));
364 :     pass2.version = XVID_VERSION;
365 : suxen_drol 1032 pass2.bitrate = codec->config.bitrate * CONFIG_KBPS;
366 : suxen_drol 983 pass2.filename = codec->config.stats;
367 : suxen_drol 1017
368 : suxen_drol 1052 pass2.keyframe_boost = codec->config.keyframe_boost; /* keyframe boost percentage: [0..100...]; */
369 :     pass2.payback_method = codec->config.bitrate_payback_method;
370 :     pass2.bitrate_payback_delay = codec->config.bitrate_payback_delay;
371 :     pass2.curve_compression_high = codec->config.curve_compression_high;
372 :     pass2.curve_compression_low = codec->config.curve_compression_low;
373 :     pass2.max_overflow_improvement = codec->config.twopass_max_overflow_improvement;
374 :     pass2.max_overflow_degradation = codec->config.twopass_max_overflow_degradation;
375 :     pass2.kftreshold = codec->config.kftreshold;
376 :     pass2.kfreduction = codec->config.kfreduction;
377 :     pass2.min_key_interval = codec->config.min_key_interval;
378 :     pass2.container_frame_overhead = 24; /* AVI */
379 :    
380 : suxen_drol 983 plugins[create.num_plugins].func = xvid_plugin_2pass2;
381 :     plugins[create.num_plugins].param = &pass2;
382 :     create.num_plugins++;
383 : suxen_drol 889 break;
384 :    
385 : suxen_drol 983 case RC_MODE_NULL :
386 : suxen_drol 889 return ICERR_OK;
387 :    
388 :     default :
389 :     break;
390 :     }
391 :    
392 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) {
393 : suxen_drol 983 plugins[create.num_plugins].func = xvid_plugin_lumimasking;
394 :     plugins[create.num_plugins].param = NULL;
395 :     create.num_plugins++;
396 : suxen_drol 889 }
397 :    
398 : suxen_drol 983 plugins[create.num_plugins].func = vfw_debug;
399 :     plugins[create.num_plugins].param = NULL;
400 :     create.num_plugins++;
401 : suxen_drol 889
402 : suxen_drol 1017 create.profile = profiles[codec->config.profile].id;
403 :    
404 : suxen_drol 889 create.width = lpbiInput->bmiHeader.biWidth;
405 :     create.height = lpbiInput->bmiHeader.biHeight;
406 :     create.fincr = codec->fincr;
407 :     create.fbase = codec->fbase;
408 :    
409 :     create.max_key_interval = codec->config.max_key_interval;
410 :    
411 : suxen_drol 1017 create.min_quant[0] = codec->config.min_iquant;
412 :     create.max_quant[0] = codec->config.max_iquant;
413 :     create.min_quant[1] = codec->config.min_pquant;
414 :     create.max_quant[1] = codec->config.max_pquant;
415 :     create.min_quant[2] = codec->config.min_bquant;
416 :     create.max_quant[2] = codec->config.max_bquant;
417 :    
418 :     if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) {
419 : suxen_drol 983 create.max_bframes = codec->config.max_bframes;
420 : suxen_drol 1017 create.bquant_ratio = codec->config.bquant_ratio;
421 :     create.bquant_offset = codec->config.bquant_offset;
422 :    
423 :     if (codec->config.packed)
424 :     create.global |= XVID_GLOBAL_PACKED;
425 :    
426 :     if (codec->config.closed_gov)
427 :     create.global |= XVID_GLOBAL_CLOSED_GOP;
428 :    
429 :     }
430 :    
431 : suxen_drol 889 create.frame_drop_ratio = codec->config.frame_drop_ratio;
432 :    
433 : suxen_drol 1017 create.num_threads = codec->config.num_threads;
434 : suxen_drol 889
435 : suxen_drol 983 switch(xvid_encore(0, XVID_ENC_CREATE, &create, NULL))
436 : suxen_drol 889 {
437 :     case XVID_ERR_FAIL :
438 :     return ICERR_ERROR;
439 :    
440 :     case XVID_ERR_MEMORY :
441 :     return ICERR_MEMORY;
442 :    
443 :     case XVID_ERR_FORMAT :
444 :     return ICERR_BADFORMAT;
445 :    
446 :     case XVID_ERR_VERSION :
447 :     return ICERR_UNSUPPORTED;
448 :     }
449 :    
450 :     codec->ehandle = create.handle;
451 :     codec->framenum = 0;
452 :     codec->keyspacing = 0;
453 :    
454 :     return ICERR_OK;
455 :     }
456 :    
457 :    
458 :     LRESULT compress_end(CODEC * codec)
459 :     {
460 :     if (codec->ehandle != NULL)
461 :     {
462 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
463 :     codec->ehandle = NULL;
464 :     }
465 :    
466 :     return ICERR_OK;
467 :     }
468 :    
469 : suxen_drol 1017
470 :     static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum)
471 :     {
472 :     int i;
473 :    
474 :     for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ;
475 :     i--;
476 :    
477 :     if (config->zones[i].greyscale) {
478 :     frame->vop_flags |= XVID_VOP_GREYSCALE;
479 :     }
480 :    
481 :     if (config->zones[i].chroma_opt) {
482 :     frame->vop_flags |= XVID_VOP_CHROMAOPT;
483 :     }
484 :    
485 :     if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) {
486 :     frame->bframe_threshold = config->zones[i].bvop_threshold;
487 :     }
488 :     }
489 :    
490 :    
491 : suxen_drol 889 LRESULT compress(CODEC * codec, ICCOMPRESS * icc)
492 :     {
493 :     BITMAPINFOHEADER * inhdr = icc->lpbiInput;
494 :     BITMAPINFOHEADER * outhdr = icc->lpbiOutput;
495 :     xvid_enc_frame_t frame;
496 :     xvid_enc_stats_t stats;
497 :     int length;
498 :    
499 :     memset(&frame, 0, sizeof(frame));
500 :     frame.version = XVID_VERSION;
501 :    
502 :     frame.type = XVID_TYPE_AUTO;
503 :    
504 :     /* vol stuff */
505 :    
506 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) &&
507 :     codec->config.quant_type != QUANT_MODE_H263)
508 : suxen_drol 889 {
509 : edgomez 949 frame.vol_flags |= XVID_VOL_MPEGQUANT;
510 : suxen_drol 889
511 : suxen_drol 1017 if (codec->config.quant_type == QUANT_MODE_CUSTOM) {
512 : suxen_drol 889 frame.quant_intra_matrix = codec->config.qmatrix_intra;
513 :     frame.quant_inter_matrix = codec->config.qmatrix_inter;
514 : suxen_drol 1017 }else{
515 : suxen_drol 889 frame.quant_intra_matrix = NULL;
516 :     frame.quant_inter_matrix = NULL;
517 :     }
518 :     }
519 :    
520 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) &&
521 :     codec->config.reduced_resolution) {
522 : edgomez 949 frame.vol_flags |= XVID_VOL_REDUCED_ENABLE;
523 :     frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */
524 : suxen_drol 889 }
525 :    
526 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) {
527 : edgomez 949 frame.vol_flags |= XVID_VOL_QUARTERPEL;
528 :     frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8;
529 : suxen_drol 889 }
530 :    
531 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc)
532 : edgomez 949 frame.vol_flags |= XVID_VOL_GMC;
533 : suxen_drol 889
534 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing)
535 : edgomez 949 frame.vol_flags |= XVID_VOL_INTERLACING;
536 : suxen_drol 889
537 :     /* vop stuff */
538 :    
539 : edgomez 949 frame.vop_flags |= XVID_VOP_HALFPEL;
540 :     frame.vop_flags |= XVID_VOP_HQACPRED;
541 : suxen_drol 889
542 : suxen_drol 1032 if (codec->config.vop_debug)
543 : edgomez 949 frame.vop_flags |= XVID_VOP_DEBUG;
544 : suxen_drol 889
545 : suxen_drol 1017 if (codec->config.trellis_quant) {
546 :     frame.vop_flags |= XVID_VOP_TRELLISQUANT;
547 :     }
548 :    
549 :     if (codec->config.motion_search > 4)
550 : edgomez 949 frame.vop_flags |= XVID_VOP_INTER4V;
551 : suxen_drol 889
552 :     if (codec->config.chromame)
553 : edgomez 949 frame.vop_flags |= XVID_ME_CHROMA16 + XVID_ME_CHROMA8;
554 : suxen_drol 889
555 :     frame.motion |= pmvfast_presets[codec->config.motion_search];
556 :    
557 :     switch (codec->config.vhq_mode)
558 :     {
559 :     case VHQ_MODE_DECISION :
560 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
561 : suxen_drol 889 break;
562 :    
563 :     case VHQ_LIMITED_SEARCH :
564 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
565 :     frame.motion |= XVID_ME_HALFPELREFINE16_BITS;
566 :     frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS;
567 : suxen_drol 889 break;
568 :    
569 :     case VHQ_MEDIUM_SEARCH :
570 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
571 :     frame.motion |= XVID_ME_HALFPELREFINE16_BITS;
572 :     frame.motion |= XVID_ME_HALFPELREFINE8_BITS;
573 :     frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS;
574 :     frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS;
575 :     frame.motion |= XVID_ME_CHECKPREDICTION_BITS;
576 : suxen_drol 889 break;
577 :    
578 :     case VHQ_WIDE_SEARCH :
579 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
580 :     frame.motion |= XVID_ME_HALFPELREFINE16_BITS;
581 :     frame.motion |= XVID_ME_HALFPELREFINE8_BITS;
582 :     frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS;
583 :     frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS;
584 :     frame.motion |= XVID_ME_CHECKPREDICTION_BITS;
585 :     frame.motion |= XVID_ME_EXTSEARCH_BITS;
586 : suxen_drol 889 break;
587 :    
588 :     default :
589 :     break;
590 :     }
591 :    
592 :     frame.input.plane[0] = icc->lpInput;
593 :     frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3;
594 :    
595 :     if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL)
596 :     return ICERR_BADFORMAT;
597 :    
598 :     if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12)
599 :     frame.input.stride[0] = (frame.input.stride[0]*2)/3;
600 :    
601 :     frame.bitstream = icc->lpOutput;
602 :     frame.length = icc->lpbiOutput->biSizeImage;
603 :    
604 : suxen_drol 1017 frame.quant = 0;
605 : suxen_drol 889
606 : suxen_drol 1017 if (codec->config.mode == RC_MODE_NULL) {
607 : suxen_drol 889 outhdr->biSizeImage = 0;
608 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
609 :     return ICERR_OK;
610 : suxen_drol 1017 }
611 : suxen_drol 889
612 :     // force keyframe spacing in 2-pass 1st pass
613 :     if (codec->config.motion_search == 0)
614 :     {
615 :     frame.type = XVID_TYPE_IVOP;
616 :     }
617 :     else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum)
618 :     {
619 : suxen_drol 983 DPRINTF("current frame forced to p-frame");
620 : suxen_drol 889 frame.type = XVID_TYPE_PVOP;
621 :     }
622 :    
623 : suxen_drol 1017 /* frame-based stuff */
624 :     apply_zone_modifiers(&frame, &codec->config, codec->framenum);
625 :    
626 :     /* call encore */
627 :    
628 : suxen_drol 889 memset(&stats, 0, sizeof(stats));
629 :     stats.version = XVID_VERSION;
630 :    
631 :     length = xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats);
632 :     switch (length)
633 :     {
634 :     case XVID_ERR_FAIL :
635 :     return ICERR_ERROR;
636 :    
637 :     case XVID_ERR_MEMORY :
638 :     return ICERR_MEMORY;
639 :    
640 :     case XVID_ERR_FORMAT :
641 :     return ICERR_BADFORMAT;
642 :    
643 :     case XVID_ERR_VERSION :
644 :     return ICERR_UNSUPPORTED;
645 :     }
646 :    
647 : suxen_drol 983 DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length);
648 : suxen_drol 889
649 :     if (length == 0) /* no encoder output */
650 :     {
651 :     *icc->lpdwFlags = 0;
652 :     ((char*)icc->lpOutput)[0] = 0x7f; /* virtual dub skip frame */
653 :     outhdr->biSizeImage = 1;
654 :    
655 :     }else{
656 :     if (frame.out_flags & XVID_KEYFRAME)
657 :     {
658 :     codec->keyspacing = 0;
659 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
660 :     }
661 :     else
662 :     {
663 :     *icc->lpdwFlags = 0;
664 :     }
665 :    
666 :     outhdr->biSizeImage = length;
667 :    
668 : suxen_drol 983 if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass)
669 : suxen_drol 889 {
670 :     outhdr->biSizeImage = 0;
671 :     }
672 :     }
673 :    
674 : suxen_drol 1017 codec->framenum++;
675 :     codec->keyspacing++;
676 : suxen_drol 889
677 :     return ICERR_OK;
678 :     }
679 :    
680 :    
681 :     /* decompressor */
682 :    
683 :    
684 :     LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
685 :     {
686 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
687 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
688 :    
689 :     if (lpbiInput == NULL)
690 :     {
691 :     return ICERR_ERROR;
692 :     }
693 :    
694 :     if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL)
695 :     {
696 :     return ICERR_BADFORMAT;
697 :     }
698 :    
699 :     if (lpbiOutput == NULL)
700 :     {
701 :     return ICERR_OK;
702 :     }
703 :    
704 :     if (inhdr->biWidth != outhdr->biWidth ||
705 :     inhdr->biHeight != outhdr->biHeight ||
706 :     get_colorspace(outhdr) == XVID_CSP_NULL)
707 :     {
708 :     return ICERR_BADFORMAT;
709 :     }
710 :    
711 :     return ICERR_OK;
712 :     }
713 :    
714 :    
715 :     LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
716 :     {
717 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
718 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
719 :     LRESULT result;
720 :    
721 :     if (lpbiOutput == NULL)
722 :     {
723 :     return sizeof(BITMAPINFOHEADER);
724 :     }
725 :    
726 :     /* --- yv12 --- */
727 :    
728 :     if (get_colorspace(inhdr) != XVID_CSP_NULL) {
729 :     memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
730 : edgomez 1053 /* XXX: should we set outhdr->biSize ?? */
731 : suxen_drol 889 return ICERR_OK;
732 :     }
733 :     /* --- yv12 --- */
734 :    
735 :     result = decompress_query(codec, lpbiInput, lpbiOutput);
736 :     if (result != ICERR_OK)
737 :     {
738 :     return result;
739 :     }
740 :    
741 :     outhdr->biSize = sizeof(BITMAPINFOHEADER);
742 :     outhdr->biWidth = inhdr->biWidth;
743 :     outhdr->biHeight = inhdr->biHeight;
744 :     outhdr->biPlanes = 1;
745 :     outhdr->biBitCount = 24;
746 :     outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */
747 :     outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
748 :     outhdr->biXPelsPerMeter = 0;
749 :     outhdr->biYPelsPerMeter = 0;
750 :     outhdr->biClrUsed = 0;
751 :     outhdr->biClrImportant = 0;
752 :    
753 :     return ICERR_OK;
754 :     }
755 :    
756 :    
757 :     LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
758 :     {
759 :     xvid_gbl_init_t init;
760 :     xvid_dec_create_t create;
761 :    
762 :     memset(&init, 0, sizeof(init));
763 :     init.version = XVID_VERSION;
764 :     init.cpu_flags = codec->config.cpu;
765 :     xvid_global(0, XVID_GBL_INIT, &init, NULL);
766 :    
767 :     memset(&create, 0, sizeof(create));
768 :     create.version = XVID_VERSION;
769 :     create.width = lpbiInput->bmiHeader.biWidth;
770 :     create.height = lpbiInput->bmiHeader.biHeight;
771 :    
772 :     switch(xvid_decore(0, XVID_DEC_CREATE, &create, NULL))
773 :     {
774 :     case XVID_ERR_FAIL :
775 :     return ICERR_ERROR;
776 :    
777 :     case XVID_ERR_MEMORY :
778 :     return ICERR_MEMORY;
779 :    
780 :     case XVID_ERR_FORMAT :
781 :     return ICERR_BADFORMAT;
782 :    
783 :     case XVID_ERR_VERSION :
784 :     return ICERR_UNSUPPORTED;
785 :     }
786 :    
787 :     codec->dhandle = create.handle;
788 :    
789 :     return ICERR_OK;
790 :     }
791 :    
792 :    
793 :     LRESULT decompress_end(CODEC * codec)
794 :     {
795 :     if (codec->dhandle != NULL)
796 :     {
797 :     xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
798 :     codec->dhandle = NULL;
799 :     }
800 :     return ICERR_OK;
801 :     }
802 :    
803 :    
804 :     LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
805 :     {
806 :     xvid_dec_frame_t frame;
807 :    
808 :     /* --- yv12 --- */
809 :     if (icd->lpbiInput->biCompression != FOURCC_XVID &&
810 :     icd->lpbiInput->biCompression != FOURCC_DIVX &&
811 :     icd->lpbiInput->biCompression != FOURCC_DX50)
812 :     {
813 :     xvid_gbl_convert_t convert;
814 :    
815 : suxen_drol 983 DPRINTF("input=%c%c%c%c output=%c%c%c%c",
816 :     icd->lpbiInput->biCompression&0xff,
817 :     (icd->lpbiInput->biCompression>>8)&0xff,
818 :     (icd->lpbiInput->biCompression>>16)&0xff,
819 :     (icd->lpbiInput->biCompression>>24)&0xff,
820 :     icd->lpbiOutput->biCompression&0xff,
821 :     (icd->lpbiOutput->biCompression>>8)&0xff,
822 :     (icd->lpbiOutput->biCompression>>16)&0xff,
823 :     (icd->lpbiOutput->biCompression>>24)&0xff);
824 : suxen_drol 889
825 :     memset(&convert, 0, sizeof(convert));
826 :     convert.version = XVID_VERSION;
827 :    
828 :     convert.input.csp = get_colorspace(icd->lpbiInput);
829 :     convert.input.plane[0] = icd->lpInput;
830 :     convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3;
831 :     if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12)
832 :     convert.input.stride[0] = (convert.input.stride[0]*2)/3;
833 :    
834 :     convert.output.csp = get_colorspace(icd->lpbiOutput);
835 :     convert.output.plane[0] = icd->lpOutput;
836 :     convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
837 :     if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12)
838 :     convert.output.stride[0] = (convert.output.stride[0]*2)/3;
839 :    
840 :     convert.width = icd->lpbiInput->biWidth;
841 :     convert.height = icd->lpbiInput->biHeight;
842 :     convert.interlacing = 0;
843 :     if (convert.input.csp == XVID_CSP_NULL ||
844 :     convert.output.csp == XVID_CSP_NULL ||
845 :     xvid_global(0, XVID_GBL_CONVERT, &convert, NULL) < 0)
846 :     {
847 :     return ICERR_BADFORMAT;
848 :     }
849 :     return ICERR_OK;
850 :     }
851 :     /* --- yv12 --- */
852 :    
853 :     memset(&frame, 0, sizeof(frame));
854 :     frame.version = XVID_VERSION;
855 :     frame.general = XVID_LOWDELAY; /* force low_delay_default mode */
856 :     frame.bitstream = icd->lpInput;
857 :     frame.length = icd->lpbiInput->biSizeImage;
858 :    
859 :     if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
860 :     {
861 :     if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
862 :     {
863 :     return ICERR_BADFORMAT;
864 :     }
865 :     frame.output.plane[0] = icd->lpOutput;
866 :     frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
867 :     if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12)
868 :     frame.output.stride[0] = (frame.output.stride[0]*2)/3;
869 :     }
870 :     else
871 :     {
872 :     frame.output.csp = XVID_CSP_NULL;
873 :     }
874 :    
875 :     switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
876 :     {
877 :     case XVID_ERR_FAIL :
878 :     return ICERR_ERROR;
879 :    
880 :     case XVID_ERR_MEMORY :
881 :     return ICERR_MEMORY;
882 :    
883 :     case XVID_ERR_FORMAT :
884 :     return ICERR_BADFORMAT;
885 :    
886 :     case XVID_ERR_VERSION :
887 :     return ICERR_UNSUPPORTED;
888 :     }
889 :    
890 :     return ICERR_OK;
891 :     }
892 :    

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