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

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