[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 1028 - (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 : suxen_drol 1017 //DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount);
247 : suxen_drol 889 codec->fincr = icf->dwScale;
248 :     codec->fbase = icf->dwRate;
249 :     return ICERR_OK;
250 :     }
251 :    
252 :    
253 : suxen_drol 983 const char type2char(int type)
254 :     {
255 :     if (type==XVID_TYPE_IVOP)
256 :     return 'I';
257 :     if (type==XVID_TYPE_PVOP)
258 :     return 'P';
259 :     if (type==XVID_TYPE_BVOP)
260 :     return 'B';
261 :     return 'S';
262 :     }
263 :    
264 :     int vfw_debug(void *handle,
265 :     int opt,
266 :     void *param1,
267 :     void *param2)
268 :     {
269 :     switch (opt) {
270 :     case XVID_PLG_INFO:
271 :     case XVID_PLG_CREATE:
272 :     case XVID_PLG_DESTROY:
273 :     case XVID_PLG_BEFORE:
274 :     return 0;
275 :    
276 :     case XVID_PLG_AFTER:
277 :     {
278 :     xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
279 :    
280 :     DPRINTF("[%5i] type=%c Q:%2i length:%6i",
281 :     data->frame_num,
282 :     type2char(data->type),
283 :     data->quant,
284 :     data->length);
285 :     return 0;
286 :     }
287 :     }
288 :    
289 :     return XVID_ERR_FAIL;
290 :     }
291 :    
292 :    
293 :    
294 : suxen_drol 889 LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
295 :     {
296 :     xvid_gbl_init_t init;
297 :     xvid_enc_create_t create;
298 : suxen_drol 983 xvid_enc_plugin_t plugins[3];
299 : suxen_drol 1017 xvid_plugin_single_t single;
300 : suxen_drol 983 xvid_plugin_2pass1_t pass1;
301 :     xvid_plugin_2pass2_t pass2;
302 : suxen_drol 1017 int i;
303 : suxen_drol 889
304 : suxen_drol 983 /* destroy previously created codec */
305 :     if(codec->ehandle) {
306 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
307 :     codec->ehandle = NULL;
308 :     }
309 :    
310 :     memset(&init, 0, sizeof(init));
311 :     init.version = XVID_VERSION;
312 :     init.cpu_flags = codec->config.cpu;
313 :     xvid_global(0, XVID_GBL_INIT, &init, NULL);
314 :    
315 :     memset(&create, 0, sizeof(create));
316 :     create.version = XVID_VERSION;
317 : suxen_drol 1017
318 :     // zones
319 :     create.zones = malloc(sizeof(xvid_enc_zone_t) * codec->config.num_zones);
320 :     create.num_zones = codec->config.num_zones;
321 :     for (i=0; i < create.num_zones; i++) {
322 :     create.zones[i].frame = codec->config.zones[i].frame;
323 :     if (create.zones[i].mode = RC_ZONE_QUANT) {
324 :     create.zones[i].mode = XVID_ZONE_QUANT;
325 :     create.zones[i].increment = codec->config.zones[i].quant;
326 :     }else{
327 :     create.zones[i].mode = XVID_ZONE_WEIGHT;
328 :     create.zones[i].increment = codec->config.zones[i].weight;
329 :     }
330 :     create.zones[i].base = 100;
331 :     }
332 :    
333 :     // plugins
334 : suxen_drol 983 create.plugins = plugins;
335 : suxen_drol 889 switch (codec->config.mode)
336 :     {
337 : suxen_drol 1017 case RC_MODE_1PASS :
338 :     memset(&single, 0, sizeof(single));
339 :     single.version = XVID_VERSION;
340 :     single.bitrate = codec->config.bitrate;
341 :     single.reaction_delay_factor = codec->config.rc_reaction_delay_factor;
342 :     single.averaging_period = codec->config.rc_averaging_period;
343 :     single.buffer = codec->config.rc_buffer;
344 :     plugins[create.num_plugins].func = xvid_plugin_single;
345 :     plugins[create.num_plugins].param = &single;
346 : suxen_drol 983 create.num_plugins++;
347 : suxen_drol 889
348 : suxen_drol 983 case RC_MODE_2PASS1 :
349 :     memset(&pass1, 0, sizeof(pass1));
350 :     pass1.version = XVID_VERSION;
351 : suxen_drol 1028 pass1.filename = codec->config.stats;
352 : suxen_drol 983
353 :     plugins[create.num_plugins].func = xvid_plugin_2pass1;
354 :     plugins[create.num_plugins].param = &pass1;
355 :     create.num_plugins++;
356 : suxen_drol 889 break;
357 :    
358 : suxen_drol 1017 case RC_MODE_2PASS2 :
359 : suxen_drol 983 memset(&pass2, 0, sizeof(pass2));
360 :     pass2.version = XVID_VERSION;
361 : suxen_drol 1017 pass2.bitrate = codec->config.bitrate;
362 : suxen_drol 983 pass2.filename = codec->config.stats;
363 : suxen_drol 1017
364 : suxen_drol 983 plugins[create.num_plugins].func = xvid_plugin_2pass2;
365 :     plugins[create.num_plugins].param = &pass2;
366 :     create.num_plugins++;
367 : suxen_drol 889 break;
368 :    
369 : suxen_drol 983 case RC_MODE_NULL :
370 : suxen_drol 889 return ICERR_OK;
371 :    
372 :     default :
373 :     break;
374 :     }
375 :    
376 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) {
377 : suxen_drol 983 plugins[create.num_plugins].func = xvid_plugin_lumimasking;
378 :     plugins[create.num_plugins].param = NULL;
379 :     create.num_plugins++;
380 : suxen_drol 889 }
381 :    
382 : suxen_drol 983 plugins[create.num_plugins].func = vfw_debug;
383 :     plugins[create.num_plugins].param = NULL;
384 :     create.num_plugins++;
385 : suxen_drol 889
386 : suxen_drol 1017 create.profile = profiles[codec->config.profile].id;
387 :    
388 : suxen_drol 889 create.width = lpbiInput->bmiHeader.biWidth;
389 :     create.height = lpbiInput->bmiHeader.biHeight;
390 :     create.fincr = codec->fincr;
391 :     create.fbase = codec->fbase;
392 :    
393 :     create.max_key_interval = codec->config.max_key_interval;
394 :    
395 : suxen_drol 1017 create.min_quant[0] = codec->config.min_iquant;
396 :     create.max_quant[0] = codec->config.max_iquant;
397 :     create.min_quant[1] = codec->config.min_pquant;
398 :     create.max_quant[1] = codec->config.max_pquant;
399 :     create.min_quant[2] = codec->config.min_bquant;
400 :     create.max_quant[2] = codec->config.max_bquant;
401 :    
402 :     if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) {
403 : suxen_drol 983 create.max_bframes = codec->config.max_bframes;
404 : suxen_drol 1017 create.bquant_ratio = codec->config.bquant_ratio;
405 :     create.bquant_offset = codec->config.bquant_offset;
406 :    
407 :     if (codec->config.packed)
408 :     create.global |= XVID_GLOBAL_PACKED;
409 :    
410 :     if (codec->config.closed_gov)
411 :     create.global |= XVID_GLOBAL_CLOSED_GOP;
412 :    
413 :     }
414 :    
415 : suxen_drol 889 create.frame_drop_ratio = codec->config.frame_drop_ratio;
416 :    
417 : suxen_drol 1017 create.num_threads = codec->config.num_threads;
418 : suxen_drol 889
419 : suxen_drol 983 switch(xvid_encore(0, XVID_ENC_CREATE, &create, NULL))
420 : suxen_drol 889 {
421 :     case XVID_ERR_FAIL :
422 :     return ICERR_ERROR;
423 :    
424 :     case XVID_ERR_MEMORY :
425 :     return ICERR_MEMORY;
426 :    
427 :     case XVID_ERR_FORMAT :
428 :     return ICERR_BADFORMAT;
429 :    
430 :     case XVID_ERR_VERSION :
431 :     return ICERR_UNSUPPORTED;
432 :     }
433 :    
434 :     codec->ehandle = create.handle;
435 :     codec->framenum = 0;
436 :     codec->keyspacing = 0;
437 :    
438 :     return ICERR_OK;
439 :     }
440 :    
441 :    
442 :     LRESULT compress_end(CODEC * codec)
443 :     {
444 :     if (codec->ehandle != NULL)
445 :     {
446 :     xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
447 :     codec->ehandle = NULL;
448 :     }
449 :    
450 :     return ICERR_OK;
451 :     }
452 :    
453 : suxen_drol 1017
454 :     static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum)
455 :     {
456 :     int i;
457 :    
458 :     for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ;
459 :     i--;
460 :    
461 :     if (config->zones[i].greyscale) {
462 :     frame->vop_flags |= XVID_VOP_GREYSCALE;
463 :     }
464 :    
465 :     if (config->zones[i].chroma_opt) {
466 :     frame->vop_flags |= XVID_VOP_CHROMAOPT;
467 :     }
468 :    
469 :     if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) {
470 :     frame->bframe_threshold = config->zones[i].bvop_threshold;
471 :     }
472 :     }
473 :    
474 :    
475 : suxen_drol 889 LRESULT compress(CODEC * codec, ICCOMPRESS * icc)
476 :     {
477 :     BITMAPINFOHEADER * inhdr = icc->lpbiInput;
478 :     BITMAPINFOHEADER * outhdr = icc->lpbiOutput;
479 :     xvid_enc_frame_t frame;
480 :     xvid_enc_stats_t stats;
481 :     int length;
482 :    
483 :     memset(&frame, 0, sizeof(frame));
484 :     frame.version = XVID_VERSION;
485 :    
486 :     frame.type = XVID_TYPE_AUTO;
487 :    
488 :     /* vol stuff */
489 :    
490 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) &&
491 :     codec->config.quant_type != QUANT_MODE_H263)
492 : suxen_drol 889 {
493 : edgomez 949 frame.vol_flags |= XVID_VOL_MPEGQUANT;
494 : suxen_drol 889
495 : suxen_drol 1017 if (codec->config.quant_type == QUANT_MODE_CUSTOM) {
496 : suxen_drol 889 frame.quant_intra_matrix = codec->config.qmatrix_intra;
497 :     frame.quant_inter_matrix = codec->config.qmatrix_inter;
498 : suxen_drol 1017 }else{
499 : suxen_drol 889 frame.quant_intra_matrix = NULL;
500 :     frame.quant_inter_matrix = NULL;
501 :     }
502 :     }
503 :    
504 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) &&
505 :     codec->config.reduced_resolution) {
506 : edgomez 949 frame.vol_flags |= XVID_VOL_REDUCED_ENABLE;
507 :     frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */
508 : suxen_drol 889 }
509 :    
510 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) {
511 : edgomez 949 frame.vol_flags |= XVID_VOL_QUARTERPEL;
512 :     frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8;
513 : suxen_drol 889 }
514 :    
515 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc)
516 : edgomez 949 frame.vol_flags |= XVID_VOL_GMC;
517 : suxen_drol 889
518 : suxen_drol 1017 if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing)
519 : edgomez 949 frame.vol_flags |= XVID_VOL_INTERLACING;
520 : suxen_drol 889
521 :     /* vop stuff */
522 :    
523 : edgomez 949 frame.vop_flags |= XVID_VOP_HALFPEL;
524 :     frame.vop_flags |= XVID_VOP_HQACPRED;
525 : suxen_drol 889
526 :     if (codec->config.debug)
527 : edgomez 949 frame.vop_flags |= XVID_VOP_DEBUG;
528 : suxen_drol 889
529 : suxen_drol 1017 if (codec->config.trellis_quant) {
530 :     frame.vop_flags |= XVID_VOP_TRELLISQUANT;
531 :     }
532 :    
533 :     if (codec->config.motion_search > 4)
534 : edgomez 949 frame.vop_flags |= XVID_VOP_INTER4V;
535 : suxen_drol 889
536 :     if (codec->config.chromame)
537 : edgomez 949 frame.vop_flags |= XVID_ME_CHROMA16 + XVID_ME_CHROMA8;
538 : suxen_drol 889
539 :     frame.motion |= pmvfast_presets[codec->config.motion_search];
540 :    
541 :     switch (codec->config.vhq_mode)
542 :     {
543 :     case VHQ_MODE_DECISION :
544 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
545 : suxen_drol 889 break;
546 :    
547 :     case VHQ_LIMITED_SEARCH :
548 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
549 :     frame.motion |= XVID_ME_HALFPELREFINE16_BITS;
550 :     frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS;
551 : suxen_drol 889 break;
552 :    
553 :     case VHQ_MEDIUM_SEARCH :
554 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
555 :     frame.motion |= XVID_ME_HALFPELREFINE16_BITS;
556 :     frame.motion |= XVID_ME_HALFPELREFINE8_BITS;
557 :     frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS;
558 :     frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS;
559 :     frame.motion |= XVID_ME_CHECKPREDICTION_BITS;
560 : suxen_drol 889 break;
561 :    
562 :     case VHQ_WIDE_SEARCH :
563 : edgomez 949 frame.vop_flags |= XVID_VOP_MODEDECISION_BITS;
564 :     frame.motion |= XVID_ME_HALFPELREFINE16_BITS;
565 :     frame.motion |= XVID_ME_HALFPELREFINE8_BITS;
566 :     frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS;
567 :     frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS;
568 :     frame.motion |= XVID_ME_CHECKPREDICTION_BITS;
569 :     frame.motion |= XVID_ME_EXTSEARCH_BITS;
570 : suxen_drol 889 break;
571 :    
572 :     default :
573 :     break;
574 :     }
575 :    
576 :     frame.input.plane[0] = icc->lpInput;
577 :     frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3;
578 :    
579 :     if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL)
580 :     return ICERR_BADFORMAT;
581 :    
582 :     if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12)
583 :     frame.input.stride[0] = (frame.input.stride[0]*2)/3;
584 :    
585 :     frame.bitstream = icc->lpOutput;
586 :     frame.length = icc->lpbiOutput->biSizeImage;
587 :    
588 : suxen_drol 1017 frame.quant = 0;
589 : suxen_drol 889
590 : suxen_drol 1017 if (codec->config.mode == RC_MODE_NULL) {
591 : suxen_drol 889 outhdr->biSizeImage = 0;
592 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
593 :     return ICERR_OK;
594 : suxen_drol 1017 }
595 : suxen_drol 889
596 :     // force keyframe spacing in 2-pass 1st pass
597 :     if (codec->config.motion_search == 0)
598 :     {
599 :     frame.type = XVID_TYPE_IVOP;
600 :     }
601 :     else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum)
602 :     {
603 : suxen_drol 983 DPRINTF("current frame forced to p-frame");
604 : suxen_drol 889 frame.type = XVID_TYPE_PVOP;
605 :     }
606 :    
607 : suxen_drol 1017 /* frame-based stuff */
608 :     apply_zone_modifiers(&frame, &codec->config, codec->framenum);
609 :    
610 :     /* call encore */
611 :    
612 : suxen_drol 889 memset(&stats, 0, sizeof(stats));
613 :     stats.version = XVID_VERSION;
614 :    
615 :     length = xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats);
616 :     switch (length)
617 :     {
618 :     case XVID_ERR_FAIL :
619 :     return ICERR_ERROR;
620 :    
621 :     case XVID_ERR_MEMORY :
622 :     return ICERR_MEMORY;
623 :    
624 :     case XVID_ERR_FORMAT :
625 :     return ICERR_BADFORMAT;
626 :    
627 :     case XVID_ERR_VERSION :
628 :     return ICERR_UNSUPPORTED;
629 :     }
630 :    
631 : suxen_drol 983 DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length);
632 : suxen_drol 889
633 :     if (length == 0) /* no encoder output */
634 :     {
635 :     *icc->lpdwFlags = 0;
636 :     ((char*)icc->lpOutput)[0] = 0x7f; /* virtual dub skip frame */
637 :     outhdr->biSizeImage = 1;
638 :    
639 :     }else{
640 :     if (frame.out_flags & XVID_KEYFRAME)
641 :     {
642 :     codec->keyspacing = 0;
643 :     *icc->lpdwFlags = AVIIF_KEYFRAME;
644 :     }
645 :     else
646 :     {
647 :     *icc->lpdwFlags = 0;
648 :     }
649 :    
650 :     outhdr->biSizeImage = length;
651 :    
652 : suxen_drol 983 if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass)
653 : suxen_drol 889 {
654 :     outhdr->biSizeImage = 0;
655 :     }
656 :     }
657 :    
658 : suxen_drol 1017 codec->framenum++;
659 :     codec->keyspacing++;
660 : suxen_drol 889
661 :     return ICERR_OK;
662 :     }
663 :    
664 :    
665 :     /* decompressor */
666 :    
667 :    
668 :     LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
669 :     {
670 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
671 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
672 :    
673 :     if (lpbiInput == NULL)
674 :     {
675 :     return ICERR_ERROR;
676 :     }
677 :    
678 :     if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL)
679 :     {
680 :     return ICERR_BADFORMAT;
681 :     }
682 :    
683 :     if (lpbiOutput == NULL)
684 :     {
685 :     return ICERR_OK;
686 :     }
687 :    
688 :     if (inhdr->biWidth != outhdr->biWidth ||
689 :     inhdr->biHeight != outhdr->biHeight ||
690 :     get_colorspace(outhdr) == XVID_CSP_NULL)
691 :     {
692 :     return ICERR_BADFORMAT;
693 :     }
694 :    
695 :     return ICERR_OK;
696 :     }
697 :    
698 :    
699 :     LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
700 :     {
701 :     BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
702 :     BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
703 :     LRESULT result;
704 :    
705 :     if (lpbiOutput == NULL)
706 :     {
707 :     return sizeof(BITMAPINFOHEADER);
708 :     }
709 :    
710 :     /* --- yv12 --- */
711 :    
712 :     if (get_colorspace(inhdr) != XVID_CSP_NULL) {
713 :     memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
714 :     // XXX: should we set outhdr->biSize ??
715 :     return ICERR_OK;
716 :     }
717 :     /* --- yv12 --- */
718 :    
719 :     result = decompress_query(codec, lpbiInput, lpbiOutput);
720 :     if (result != ICERR_OK)
721 :     {
722 :     return result;
723 :     }
724 :    
725 :     outhdr->biSize = sizeof(BITMAPINFOHEADER);
726 :     outhdr->biWidth = inhdr->biWidth;
727 :     outhdr->biHeight = inhdr->biHeight;
728 :     outhdr->biPlanes = 1;
729 :     outhdr->biBitCount = 24;
730 :     outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */
731 :     outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
732 :     outhdr->biXPelsPerMeter = 0;
733 :     outhdr->biYPelsPerMeter = 0;
734 :     outhdr->biClrUsed = 0;
735 :     outhdr->biClrImportant = 0;
736 :    
737 :     return ICERR_OK;
738 :     }
739 :    
740 :    
741 :     LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
742 :     {
743 :     xvid_gbl_init_t init;
744 :     xvid_dec_create_t create;
745 :    
746 :     memset(&init, 0, sizeof(init));
747 :     init.version = XVID_VERSION;
748 :     init.cpu_flags = codec->config.cpu;
749 :     xvid_global(0, XVID_GBL_INIT, &init, NULL);
750 :    
751 :     memset(&create, 0, sizeof(create));
752 :     create.version = XVID_VERSION;
753 :     create.width = lpbiInput->bmiHeader.biWidth;
754 :     create.height = lpbiInput->bmiHeader.biHeight;
755 :    
756 :     switch(xvid_decore(0, XVID_DEC_CREATE, &create, NULL))
757 :     {
758 :     case XVID_ERR_FAIL :
759 :     return ICERR_ERROR;
760 :    
761 :     case XVID_ERR_MEMORY :
762 :     return ICERR_MEMORY;
763 :    
764 :     case XVID_ERR_FORMAT :
765 :     return ICERR_BADFORMAT;
766 :    
767 :     case XVID_ERR_VERSION :
768 :     return ICERR_UNSUPPORTED;
769 :     }
770 :    
771 :     codec->dhandle = create.handle;
772 :    
773 :     return ICERR_OK;
774 :     }
775 :    
776 :    
777 :     LRESULT decompress_end(CODEC * codec)
778 :     {
779 :     if (codec->dhandle != NULL)
780 :     {
781 :     xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
782 :     codec->dhandle = NULL;
783 :     }
784 :     return ICERR_OK;
785 :     }
786 :    
787 :    
788 :     LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
789 :     {
790 :     xvid_dec_frame_t frame;
791 :    
792 :     /* --- yv12 --- */
793 :     if (icd->lpbiInput->biCompression != FOURCC_XVID &&
794 :     icd->lpbiInput->biCompression != FOURCC_DIVX &&
795 :     icd->lpbiInput->biCompression != FOURCC_DX50)
796 :     {
797 :     xvid_gbl_convert_t convert;
798 :    
799 : suxen_drol 983 DPRINTF("input=%c%c%c%c output=%c%c%c%c",
800 :     icd->lpbiInput->biCompression&0xff,
801 :     (icd->lpbiInput->biCompression>>8)&0xff,
802 :     (icd->lpbiInput->biCompression>>16)&0xff,
803 :     (icd->lpbiInput->biCompression>>24)&0xff,
804 :     icd->lpbiOutput->biCompression&0xff,
805 :     (icd->lpbiOutput->biCompression>>8)&0xff,
806 :     (icd->lpbiOutput->biCompression>>16)&0xff,
807 :     (icd->lpbiOutput->biCompression>>24)&0xff);
808 : suxen_drol 889
809 :     memset(&convert, 0, sizeof(convert));
810 :     convert.version = XVID_VERSION;
811 :    
812 :     convert.input.csp = get_colorspace(icd->lpbiInput);
813 :     convert.input.plane[0] = icd->lpInput;
814 :     convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3;
815 :     if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12)
816 :     convert.input.stride[0] = (convert.input.stride[0]*2)/3;
817 :    
818 :     convert.output.csp = get_colorspace(icd->lpbiOutput);
819 :     convert.output.plane[0] = icd->lpOutput;
820 :     convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
821 :     if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12)
822 :     convert.output.stride[0] = (convert.output.stride[0]*2)/3;
823 :    
824 :     convert.width = icd->lpbiInput->biWidth;
825 :     convert.height = icd->lpbiInput->biHeight;
826 :     convert.interlacing = 0;
827 :     if (convert.input.csp == XVID_CSP_NULL ||
828 :     convert.output.csp == XVID_CSP_NULL ||
829 :     xvid_global(0, XVID_GBL_CONVERT, &convert, NULL) < 0)
830 :     {
831 :     return ICERR_BADFORMAT;
832 :     }
833 :     return ICERR_OK;
834 :     }
835 :     /* --- yv12 --- */
836 :    
837 :     memset(&frame, 0, sizeof(frame));
838 :     frame.version = XVID_VERSION;
839 :     frame.general = XVID_LOWDELAY; /* force low_delay_default mode */
840 :     frame.bitstream = icd->lpInput;
841 :     frame.length = icd->lpbiInput->biSizeImage;
842 :    
843 :     if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
844 :     {
845 :     if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
846 :     {
847 :     return ICERR_BADFORMAT;
848 :     }
849 :     frame.output.plane[0] = icd->lpOutput;
850 :     frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
851 :     if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12)
852 :     frame.output.stride[0] = (frame.output.stride[0]*2)/3;
853 :     }
854 :     else
855 :     {
856 :     frame.output.csp = XVID_CSP_NULL;
857 :     }
858 :    
859 :     switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
860 :     {
861 :     case XVID_ERR_FAIL :
862 :     return ICERR_ERROR;
863 :    
864 :     case XVID_ERR_MEMORY :
865 :     return ICERR_MEMORY;
866 :    
867 :     case XVID_ERR_FORMAT :
868 :     return ICERR_BADFORMAT;
869 :    
870 :     case XVID_ERR_VERSION :
871 :     return ICERR_UNSUPPORTED;
872 :     }
873 :    
874 :     return ICERR_OK;
875 :     }
876 :    

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