[cvs] / xvidcore / vfw / src / codec.c Repository:
ViewVC logotype

Diff of /xvidcore/vfw/src/codec.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Sat Feb 22 08:24:01 2003 UTC revision 1.2, Mon Mar 22 22:36:25 2004 UTC
# Line 0  Line 1 
1    /**************************************************************************
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    #include <stdio.h>
53    #include "vfwext.h"
54    
55    #include <xvid.h>
56    #include "debug.h"
57    #include "codec.h"
58    #include "status.h"
59    
60    HINSTANCE m_hdll;
61    int (*xvid_global_func)(void *handle, int opt, void *param1, void *param2);
62    int (*xvid_encore_func)(void *handle, int opt, void *param1, void *param2);
63    int (*xvid_decore_func)(void *handle, int opt, void *param1, void *param2);
64    
65    xvid_plugin_func *xvid_plugin_single_func,
66                                    *xvid_plugin_2pass1_func,
67                                    *xvid_plugin_2pass2_func,
68                                    *xvid_plugin_lumimasking_func,
69                                    *xvid_plugin_psnr_func;
70    
71    
72    
73    static const int pmvfast_presets[7] = {
74            0, 0, 0, 0,
75            0 | XVID_ME_HALFPELREFINE16 | 0,
76            0 | XVID_ME_HALFPELREFINE16 | 0 |
77            XVID_ME_ADVANCEDDIAMOND16, XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |
78            XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16
79    };
80    
81    
82    
83    /*      return xvid compatbile colorspace,
84            or XVID_CSP_NULL if failure
85    */
86    
87    static int get_colorspace(BITMAPINFOHEADER * hdr)
88    {
89            /* rgb only: negative height specifies top down image */
90            int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP);
91    
92            switch(hdr->biCompression)
93            {
94            case BI_RGB :
95                    if (hdr->biBitCount == 16)
96                    {
97                            DPRINTF("RGB16 (RGB555)");
98                            return rgb_flip | XVID_CSP_RGB555;
99                    }
100                    if (hdr->biBitCount == 24)
101                    {
102                            DPRINTF("RGB24");
103                            return rgb_flip | XVID_CSP_BGR;
104                    }
105                    if (hdr->biBitCount == 32)
106                    {
107                            DPRINTF("RGB32");
108                            return rgb_flip | XVID_CSP_BGRA;
109                    }
110    
111                    DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount);
112                    return XVID_CSP_NULL;
113    
114            case BI_BITFIELDS :
115                    if (hdr->biSize >= sizeof(BITMAPV4HEADER))
116                    {
117                            BITMAPV4HEADER * hdr4 = (BITMAPV4HEADER *)hdr;
118    
119                            if (hdr4->bV4BitCount == 16 &&
120                                    hdr4->bV4RedMask == 0x7c00 &&
121                                    hdr4->bV4GreenMask == 0x3e0 &&
122                                    hdr4->bV4BlueMask == 0x1f)
123                            {
124                                    DPRINTF("RGB555");
125                                    return rgb_flip | XVID_CSP_RGB555;
126                            }
127    
128                            if(hdr4->bV4BitCount == 16 &&
129                                    hdr4->bV4RedMask == 0xf800 &&
130                                    hdr4->bV4GreenMask == 0x7e0 &&
131                                    hdr4->bV4BlueMask == 0x1f)
132                            {
133                                    DPRINTF("RGB565");
134                                    return rgb_flip | XVID_CSP_RGB565;
135                            }
136    
137                            DPRINTF("unsupported BI_BITFIELDS mode");
138                            return XVID_CSP_NULL;
139                    }
140    
141                    DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination");
142                    return XVID_CSP_NULL;
143    
144            case FOURCC_I420 :
145            case FOURCC_IYUV :
146                    DPRINTF("IYUY");
147                    return XVID_CSP_I420;
148    
149            case FOURCC_YV12 :
150                    DPRINTF("YV12");
151                    return XVID_CSP_YV12;
152    
153            case FOURCC_YUYV :
154            case FOURCC_YUY2 :
155                    DPRINTF("YUY2");
156                    return XVID_CSP_YUY2;
157    
158            case FOURCC_YVYU :
159                    DPRINTF("YVYU");
160                    return XVID_CSP_YVYU;
161    
162            case FOURCC_UYVY :
163                    DPRINTF("UYVY");
164                    return XVID_CSP_UYVY;
165    
166            default :
167                    DPRINTF("unsupported colorspace %c%c%c%c",
168                            hdr->biCompression&0xff,
169                            (hdr->biCompression>>8)&0xff,
170                            (hdr->biCompression>>16)&0xff,
171                            (hdr->biCompression>>24)&0xff);
172                    return XVID_CSP_NULL;
173            }
174    }
175    
176    
177    /* compressor */
178    
179    
180    /* test the output format */
181    
182    LRESULT compress_query(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
183    {
184            BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
185            BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
186    
187            /* VFWEXT detection */
188            if (inhdr->biCompression == VFWEXT_FOURCC) {
189                    return (ICM_USER+0x0fff);
190            }
191    
192            if (get_colorspace(inhdr) == XVID_CSP_NULL)
193            {
194                    return ICERR_BADFORMAT;
195            }
196    
197            if (lpbiOutput == NULL)
198            {
199                    return ICERR_OK;
200            }
201    
202            if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight ||
203                    (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX && outhdr->biCompression != FOURCC_DX50))
204            {
205                    return ICERR_BADFORMAT;
206            }
207    
208            return ICERR_OK;
209    }
210    
211    
212    LRESULT compress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
213    {
214            BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
215            BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
216    
217            if (get_colorspace(inhdr) == XVID_CSP_NULL)
218            {
219                    return ICERR_BADFORMAT;
220            }
221    
222            if (lpbiOutput == NULL)
223            {
224                    return sizeof(BITMAPINFOHEADER);
225            }
226    
227            memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
228            outhdr->biSize = sizeof(BITMAPINFOHEADER);
229            outhdr->biSizeImage = compress_get_size(codec, lpbiInput, lpbiOutput);
230            outhdr->biXPelsPerMeter = 0;
231            outhdr->biYPelsPerMeter = 0;
232            outhdr->biClrUsed = 0;
233            outhdr->biClrImportant = 0;
234    
235            if (codec->config.fourcc_used == 0)
236            {
237                    outhdr->biCompression = FOURCC_XVID;
238            }
239            else if (codec->config.fourcc_used == 1)
240            {
241                    outhdr->biCompression = FOURCC_DIVX;
242            }
243            else
244            {
245                    outhdr->biCompression = FOURCC_DX50;
246            }
247    
248            return ICERR_OK;
249    }
250    
251    
252    LRESULT compress_get_size(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
253    {
254            return 2 * lpbiOutput->bmiHeader.biWidth * lpbiOutput->bmiHeader.biHeight * 3;
255    }
256    
257    
258    LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf)
259    {
260    #if 0
261            DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount);
262    #endif
263            codec->fincr = icf->dwScale;
264            codec->fbase = icf->dwRate;
265            return ICERR_OK;
266    }
267    
268    
269    static char type2char(int type)
270    {
271            if (type==XVID_TYPE_IVOP)
272                    return 'I';
273            if (type==XVID_TYPE_PVOP)
274                    return 'P';
275            if (type==XVID_TYPE_BVOP)
276                    return 'B';
277            return 'S';
278    }
279    
280    static int vfw_debug(void *handle,
281                             int opt,
282                             void *param1,
283                             void *param2)
284    {
285            switch (opt) {
286            case XVID_PLG_CREATE:
287                    *((void**)param2) = NULL;
288            case XVID_PLG_INFO:
289            case XVID_PLG_DESTROY:
290            case XVID_PLG_BEFORE:
291                    return 0;
292    
293            case XVID_PLG_AFTER:
294                    {
295                            xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
296    
297                            /* We don't use DPRINTF here because it's active only for _DEBUG
298                             * builds and that activates lot of other debug printfs. We only
299                             * want these all the time */
300                            char buf[1024];
301                            sprintf(buf, "[%6i]   type=%c   Q:%2i   length:%6i",
302                                            data->frame_num,
303                                            type2char(data->type),
304                                            data->quant,
305                                            data->length);
306                            OutputDebugString(buf);
307    
308                            return 0;
309                    }
310            }
311    
312            return XVID_ERR_FAIL;
313    }
314    
315    #define XVID_DLL_NAME "xvidcore.dll"
316    
317    static int init_dll()
318    {
319            if (m_hdll != NULL) return 0;
320    
321            DPRINTF("init_dll");
322            m_hdll = LoadLibrary(XVID_DLL_NAME);
323            if (m_hdll == NULL) {
324                    DPRINTF("dll load failed");
325                    MessageBox(0, XVID_DLL_NAME " not found!","Error!", MB_ICONEXCLAMATION|MB_OK);
326                    return XVID_ERR_FAIL;
327            }
328    
329            xvid_global_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global");
330            if (xvid_global_func == NULL) {
331                    MessageBox(0, "xvid_global() not found", "Error", 0);
332                    return XVID_ERR_FAIL;
333            }
334    
335            xvid_encore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_encore");
336            if (xvid_encore_func == NULL) {
337                    MessageBox(0, "xvid_encore() not found", "Error", 0);
338                    return XVID_ERR_FAIL;
339            }
340    
341            xvid_decore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_decore");
342            if (xvid_decore_func == NULL) {
343                    MessageBox(0, "xvid_decore() not found", "Error", 0);
344                    return XVID_ERR_FAIL;
345            }
346    
347            xvid_plugin_single_func =
348                    (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_single"));
349            xvid_plugin_2pass1_func =
350                    (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass1"));
351            xvid_plugin_2pass2_func =
352                    (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass2"));
353            xvid_plugin_lumimasking_func =
354                    (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_lumimasking"));
355            xvid_plugin_psnr_func =
356                    (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_psnr"));
357    
358            return 0;
359    }
360    
361    /* constant-quant zones for fixed quant encoding */
362    static void
363    prepare_cquant_zones(CONFIG * config) {
364    
365            int i = 0;
366            if (config->num_zones == 0 || config->zones[0].frame != 0) {
367                    /* first zone does not start at frame 0 or doesn't exist */
368    
369                    if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */
370    
371                    config->zones[config->num_zones].frame = 0;
372                    config->zones[config->num_zones].mode = RC_ZONE_QUANT;
373                    config->zones[config->num_zones].weight = 100;
374                    config->zones[config->num_zones].quant = config->desired_quant;
375                    config->zones[config->num_zones].type = XVID_TYPE_AUTO;
376                    config->zones[config->num_zones].greyscale = 0;
377                    config->zones[config->num_zones].chroma_opt = 0;
378                    config->zones[config->num_zones].bvop_threshold = 0;
379                    config->num_zones++;
380    
381                    sort_zones(config->zones, config->num_zones, &i);
382            }
383    
384            /* step 2: let's change all weight zones into quant zones */
385    
386            for(i = 0; i < config->num_zones; i++)
387                    if (config->zones[i].mode == RC_ZONE_WEIGHT) {
388                            config->zones[i].mode = RC_ZONE_QUANT;
389                            config->zones[i].quant = (100*config->desired_quant) / config->zones[i].weight;
390                    }
391    }
392    
393    /* full first pass zones */
394    static void
395    prepare_full1pass_zones(CONFIG * config) {
396    
397            int i = 0;
398            if (config->num_zones == 0 || config->zones[0].frame != 0) {
399                    /* first zone does not start at frame 0 or doesn't exist */
400    
401                    if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */
402    
403                    config->zones[config->num_zones].frame = 0;
404                    config->zones[config->num_zones].mode = RC_ZONE_QUANT;
405                    config->zones[config->num_zones].weight = 100;
406                    config->zones[config->num_zones].quant = 200;
407                    config->zones[config->num_zones].type = XVID_TYPE_AUTO;
408                    config->zones[config->num_zones].greyscale = 0;
409                    config->zones[config->num_zones].chroma_opt = 0;
410                    config->zones[config->num_zones].bvop_threshold = 0;
411                    config->num_zones++;
412    
413                    sort_zones(config->zones, config->num_zones, &i);
414            }
415    
416            /* step 2: let's change all weight zones into quant zones */
417    
418            for(i = 0; i < config->num_zones; i++)
419                    if (config->zones[i].mode == RC_ZONE_WEIGHT) {
420                            config->zones[i].mode = RC_ZONE_QUANT;
421                            config->zones[i].quant = 200;
422                    }
423    }
424    
425    
426    LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
427    {
428            xvid_gbl_init_t init;
429            xvid_enc_create_t create;
430            xvid_enc_plugin_t plugins[3];
431            xvid_plugin_single_t single;
432            xvid_plugin_2pass1_t pass1;
433            xvid_plugin_2pass2_t pass2;
434            int i;
435            HANDLE hFile;
436    
437            CONFIG tmpCfg; /* if we want to alter config to suit our needs, it shouldn't be visible to user later */
438            memcpy(&tmpCfg, &codec->config, sizeof(CONFIG));
439    
440            if (init_dll() != 0) return ICERR_ERROR;
441            /* destroy previously created codec */
442            if(codec->ehandle) {
443                    xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
444                    codec->ehandle = NULL;
445            }
446    
447            memset(&init, 0, sizeof(init));
448            init.version = XVID_VERSION;
449            init.cpu_flags = codec->config.cpu;
450            init.debug = codec->config.debug;
451            xvid_global_func(0, XVID_GBL_INIT, &init, NULL);
452    
453            memset(&create, 0, sizeof(create));
454            create.version = XVID_VERSION;
455    
456            /* plugins */
457            create.plugins = plugins;
458            switch (codec->config.mode)
459            {
460            case RC_MODE_1PASS :
461                    memset(&single, 0, sizeof(single));
462                    single.version = XVID_VERSION;
463                    single.bitrate = codec->config.bitrate * CONFIG_KBPS;
464                    single.reaction_delay_factor = codec->config.rc_reaction_delay_factor;
465                    single.averaging_period = codec->config.rc_averaging_period;
466                    single.buffer = codec->config.rc_buffer;
467                    plugins[create.num_plugins].func = xvid_plugin_single_func;
468                    plugins[create.num_plugins].param = &single;
469                    create.num_plugins++;
470                    if (!codec->config.use_2pass_bitrate) /* constant-quant mode */
471                            prepare_cquant_zones(&tmpCfg);
472                    break;
473    
474            case RC_MODE_2PASS1 :
475                    memset(&pass1, 0, sizeof(pass1));
476                    pass1.version = XVID_VERSION;
477                    pass1.filename = codec->config.stats;
478                    if (codec->config.full1pass)
479                            prepare_full1pass_zones(&tmpCfg);
480                    plugins[create.num_plugins].func = xvid_plugin_2pass1_func;
481                    plugins[create.num_plugins].param = &pass1;
482                    create.num_plugins++;
483                    break;
484    
485            case RC_MODE_2PASS2 :
486                    memset(&pass2, 0, sizeof(pass2));
487                    pass2.version = XVID_VERSION;
488                    if (codec->config.use_2pass_bitrate) {
489                            pass2.bitrate = codec->config.bitrate * CONFIG_KBPS;
490                    } else {
491                            pass2.bitrate = -codec->config.desired_size;    /* kilobytes */
492                    }
493                    pass2.filename = codec->config.stats;
494    
495                    hFile = CreateFile(pass2.filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
496                    if (hFile == INVALID_HANDLE_VALUE)
497                    {
498                            MessageBox(0, "Statsfile not found!","Error!", MB_ICONEXCLAMATION|MB_OK);
499                            return XVID_ERR_FAIL;
500                    } else
501                    {
502                            CloseHandle(hFile);
503                    }
504    
505                    pass2.keyframe_boost = codec->config.keyframe_boost;   /* keyframe boost percentage: [0..100...]; */
506                    pass2.curve_compression_high = codec->config.curve_compression_high;
507                    pass2.curve_compression_low = codec->config.curve_compression_low;
508                    pass2.overflow_control_strength = codec->config.overflow_control_strength;
509                    pass2.max_overflow_improvement = codec->config.twopass_max_overflow_improvement;
510                    pass2.max_overflow_degradation = codec->config.twopass_max_overflow_degradation;
511                    pass2.kfreduction = codec->config.kfreduction;
512                    pass2.kfthreshold = codec->config.kfthreshold;
513                    pass2.container_frame_overhead = 24;    /* AVI */
514    
515                    plugins[create.num_plugins].func = xvid_plugin_2pass2_func;
516                    plugins[create.num_plugins].param = &pass2;
517                    create.num_plugins++;
518                    break;
519    
520            case RC_MODE_NULL :
521                    return ICERR_OK;
522    
523            default :
524                    break;
525            }
526    
527            /* zones  - copy from tmpCfg in case we automatically altered them above */
528            create.zones = malloc(sizeof(xvid_enc_zone_t) * tmpCfg.num_zones);
529            create.num_zones = tmpCfg.num_zones;
530            for (i=0; i < create.num_zones; i++) {
531                    create.zones[i].frame = tmpCfg.zones[i].frame;
532                    if (tmpCfg.zones[i].mode == RC_ZONE_QUANT) {
533                            create.zones[i].mode = XVID_ZONE_QUANT;
534                            create.zones[i].increment = tmpCfg.zones[i].quant;
535                    }else{
536                            create.zones[i].mode = XVID_ZONE_WEIGHT;
537                            create.zones[i].increment = tmpCfg.zones[i].weight;
538                    }
539                    create.zones[i].base = 100;
540            }
541    
542            /* lumimasking plugin */
543            if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) {
544                    plugins[create.num_plugins].func = xvid_plugin_lumimasking_func;
545                    plugins[create.num_plugins].param = NULL;
546                    create.num_plugins++;
547            }
548    
549            plugins[create.num_plugins].func = vfw_debug;
550            plugins[create.num_plugins].param = NULL;
551            create.num_plugins++;
552    
553            create.profile = profiles[codec->config.profile].id;
554    
555            create.width = lpbiInput->bmiHeader.biWidth;
556            create.height = lpbiInput->bmiHeader.biHeight;
557            create.fincr = codec->fincr;
558            create.fbase = codec->fbase;
559    
560            create.max_key_interval = codec->config.max_key_interval;
561    
562            create.min_quant[0] = codec->config.min_iquant;
563            create.max_quant[0] = codec->config.max_iquant;
564            create.min_quant[1] = codec->config.min_pquant;
565            create.max_quant[1] = codec->config.max_pquant;
566            create.min_quant[2] = codec->config.min_bquant;
567            create.max_quant[2] = codec->config.max_bquant;
568    
569            if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) {
570                    create.max_bframes = codec->config.max_bframes;
571                    create.bquant_ratio = codec->config.bquant_ratio;
572                    create.bquant_offset = codec->config.bquant_offset;
573    
574                    if (codec->config.packed)
575                            create.global |= XVID_GLOBAL_PACKED;
576    
577                    if (codec->config.closed_gov)
578                            create.global |= XVID_GLOBAL_CLOSED_GOP;
579    
580            }
581    
582            create.frame_drop_ratio = codec->config.frame_drop_ratio;
583    
584            create.num_threads = codec->config.num_threads;
585    
586            switch(xvid_encore_func(0, XVID_ENC_CREATE, &create, NULL))
587            {
588            case XVID_ERR_FAIL :
589                    return ICERR_ERROR;
590    
591            case XVID_ERR_MEMORY :
592                    return ICERR_MEMORY;
593    
594            case XVID_ERR_FORMAT :
595                    return ICERR_BADFORMAT;
596    
597            case XVID_ERR_VERSION :
598                    return ICERR_UNSUPPORTED;
599            }
600    
601            codec->ehandle = create.handle;
602            codec->framenum = 0;
603            codec->keyspacing = 0;
604    
605            if (codec->config.display_status) {
606                    status_destroy_always(&codec->status);
607                    status_create(&codec->status, codec->fincr, codec->fbase);
608            }
609    
610            return ICERR_OK;
611    }
612    
613    
614    LRESULT compress_end(CODEC * codec)
615    {
616            if (m_hdll != NULL) {
617                    if (codec->ehandle != NULL) {
618                            xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL);
619                            codec->ehandle = NULL;
620                    }
621                    FreeLibrary(m_hdll);
622                    m_hdll = NULL;
623            }
624    
625            if (codec->config.display_status)
626                    status_destroy(&codec->status);
627    
628            return ICERR_OK;
629    }
630    
631    
632    static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum)
633    {
634            int i;
635    
636            for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ;
637    
638            if (--i < 0) return; /* there are no zones, or we're before the first zone */
639    
640            if (framenum == config->zones[i].frame)
641                    frame->type = config->zones[i].type;
642    
643            if (config->zones[i].greyscale) {
644                    frame->vop_flags |= XVID_VOP_GREYSCALE;
645            }
646    
647            if (config->zones[i].chroma_opt) {
648                    frame->vop_flags |= XVID_VOP_CHROMAOPT;
649            }
650    
651            if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) {
652                    frame->bframe_threshold = config->zones[i].bvop_threshold;
653            }
654    }
655    
656    
657    LRESULT compress(CODEC * codec, ICCOMPRESS * icc)
658    {
659            BITMAPINFOHEADER * inhdr = icc->lpbiInput;
660            BITMAPINFOHEADER * outhdr = icc->lpbiOutput;
661            xvid_enc_frame_t frame;
662            xvid_enc_stats_t stats;
663            int length;
664    
665            memset(&frame, 0, sizeof(frame));
666            frame.version = XVID_VERSION;
667    
668            frame.type = XVID_TYPE_AUTO;
669    
670            /* vol stuff */
671    
672            if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) &&
673                    codec->config.quant_type != QUANT_MODE_H263)
674            {
675                    frame.vol_flags |= XVID_VOL_MPEGQUANT;
676    
677                    if (codec->config.quant_type == QUANT_MODE_CUSTOM) {
678                            frame.quant_intra_matrix = codec->config.qmatrix_intra;
679                            frame.quant_inter_matrix = codec->config.qmatrix_inter;
680                    }else{
681                            frame.quant_intra_matrix = NULL;
682                            frame.quant_inter_matrix = NULL;
683                    }
684            }
685    
686            if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) &&
687                    codec->config.reduced_resolution) {
688                    frame.vol_flags |= XVID_VOL_REDUCED_ENABLE;
689                    frame.vop_flags |= XVID_VOP_REDUCED;    /* XXX: need auto decion mode */
690            }
691    
692            if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) {
693                    frame.vol_flags |= XVID_VOL_QUARTERPEL;
694                    frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8;
695            }
696    
697            if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) {
698                    frame.vol_flags |= XVID_VOL_GMC;
699                    frame.motion |= XVID_ME_GME_REFINE;
700            }
701    
702            if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing)
703                    frame.vol_flags |= XVID_VOL_INTERLACING;
704    
705            if (codec->config.ar_mode == 0) { /* PAR */
706                    if (codec->config.display_aspect_ratio != 5) {
707                            frame.par = codec->config.display_aspect_ratio + 1;
708                    } else {
709                            frame.par = XVID_PAR_EXT;
710                            frame.par_width = codec->config.par_x;
711                            frame.par_height= codec->config.par_y;
712                    }
713            } else { /* AR */
714                    /* custom pixel aspect ratio -> calculated from DAR */
715                    frame.par = XVID_PAR_EXT;
716                    frame.par_width = (100 * inhdr->biHeight) / codec->config.ar_y;
717                    frame.par_height= (100 * inhdr->biWidth) / codec->config.ar_x;
718            }
719    
720            /* vop stuff */
721    
722            frame.vop_flags |= XVID_VOP_HALFPEL;
723            frame.vop_flags |= XVID_VOP_HQACPRED;
724    
725            if (codec->config.vop_debug)
726                    frame.vop_flags |= XVID_VOP_DEBUG;
727    
728            if (codec->config.trellis_quant) {
729                    frame.vop_flags |= XVID_VOP_TRELLISQUANT;
730            }
731    
732            if (codec->config.motion_search > 4)
733                    frame.vop_flags |= XVID_VOP_INTER4V;
734    
735            if (codec->config.chromame)
736                    frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP;
737    
738            if (codec->config.cartoon_mode) {
739                    frame.vop_flags |= XVID_VOP_CARTOON;
740                    frame.motion |= XVID_ME_DETECT_STATIC_MOTION;
741            }
742    
743            if (codec->config.turbo)
744                    frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 |
745                                                    XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE |
746                                                    XVID_ME_BFRAME_EARLYSTOP;
747    
748            frame.motion |= pmvfast_presets[codec->config.motion_search];
749    
750            switch (codec->config.vhq_mode)
751            {
752            case VHQ_MODE_DECISION :
753                    frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
754                    break;
755    
756            case VHQ_LIMITED_SEARCH :
757                    frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
758                    frame.motion |= XVID_ME_HALFPELREFINE16_RD;
759                    frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
760                    break;
761    
762            case VHQ_MEDIUM_SEARCH :
763                    frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
764                    frame.motion |= XVID_ME_HALFPELREFINE16_RD;
765                    frame.motion |= XVID_ME_HALFPELREFINE8_RD;
766                    frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
767                    frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
768                    frame.motion |= XVID_ME_CHECKPREDICTION_RD;
769                    break;
770    
771            case VHQ_WIDE_SEARCH :
772                    frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
773                    frame.motion |= XVID_ME_HALFPELREFINE16_RD;
774                    frame.motion |= XVID_ME_HALFPELREFINE8_RD;
775                    frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
776                    frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
777                    frame.motion |= XVID_ME_CHECKPREDICTION_RD;
778                    frame.motion |= XVID_ME_EXTSEARCH_RD;
779                    break;
780    
781            default :
782                    break;
783            }
784    
785            frame.input.plane[0] = icc->lpInput;
786            frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3;
787    
788            if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL)
789                    return ICERR_BADFORMAT;
790    
791            if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12) {
792                    frame.input.stride[0] = (4 * icc->lpbiInput->biWidth + 3) / 4;
793                    frame.input.stride[1] = frame.input.stride[2] = frame.input.stride[0] / 2 ;
794            }
795    
796            frame.bitstream = icc->lpOutput;
797            frame.length = icc->lpbiOutput->biSizeImage;
798    
799            frame.quant = 0;
800    
801            if (codec->config.mode == RC_MODE_NULL) {
802                    outhdr->biSizeImage = 0;
803                    *icc->lpdwFlags = AVIIF_KEYFRAME;
804                    return ICERR_OK;
805            }
806    
807            // force keyframe spacing in 2-pass 1st pass
808            if (codec->config.motion_search == 0)
809                    frame.type = XVID_TYPE_IVOP;
810    
811            /* frame-based stuff */
812            apply_zone_modifiers(&frame, &codec->config, codec->framenum);
813    
814            /* call encore */
815    
816            memset(&stats, 0, sizeof(stats));
817            stats.version = XVID_VERSION;
818    
819            length = xvid_encore_func(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats);
820            switch (length)
821            {
822            case XVID_ERR_FAIL :
823                    return ICERR_ERROR;
824    
825            case XVID_ERR_MEMORY :
826                    return ICERR_MEMORY;
827    
828            case XVID_ERR_FORMAT :
829                    return ICERR_BADFORMAT;
830    
831            case XVID_ERR_VERSION :
832                    return ICERR_UNSUPPORTED;
833            }
834    
835            if (codec->config.display_status && stats.type>0) {
836                    status_update(&codec->status, stats.type, stats.length, stats.quant);
837            }
838    
839            DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length);
840    
841            if (length == 0)        /* no encoder output */
842            {
843                    *icc->lpdwFlags = 0;
844                    ((char*)icc->lpOutput)[0] = 0x7f;       /* virtual dub skip frame */
845                    outhdr->biSizeImage = 1;
846    
847            }else{
848                    if (frame.out_flags & XVID_KEYFRAME)
849                    {
850                            codec->keyspacing = 0;
851                            *icc->lpdwFlags = AVIIF_KEYFRAME;
852                    }
853                    else
854                    {
855                             *icc->lpdwFlags = 0;
856                    }
857    
858                    outhdr->biSizeImage = length;
859    
860                    if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass)
861                    {
862                            outhdr->biSizeImage = 0;
863                    }
864            }
865    
866            codec->framenum++;
867            codec->keyspacing++;
868    
869            return ICERR_OK;
870    }
871    
872    
873    /* decompressor */
874    
875    
876    LRESULT decompress_query(CODEC * codec, BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput)
877    {
878            BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
879            BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
880    
881            if (lpbiInput == NULL)
882            {
883                    return ICERR_ERROR;
884            }
885    
886            if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL)
887            {
888                    return ICERR_BADFORMAT;
889            }
890    
891            if (lpbiOutput == NULL)
892            {
893                    return ICERR_OK;
894            }
895    
896            if (inhdr->biWidth != outhdr->biWidth ||
897                    inhdr->biHeight != outhdr->biHeight ||
898                    get_colorspace(outhdr) == XVID_CSP_NULL)
899            {
900                    return ICERR_BADFORMAT;
901            }
902    
903            return ICERR_OK;
904    }
905    
906    
907    LRESULT decompress_get_format(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
908    {
909            BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader;
910            BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader;
911            LRESULT result;
912    
913            if (lpbiOutput == NULL)
914            {
915                    return sizeof(BITMAPINFOHEADER);
916            }
917    
918            /* --- yv12 --- */
919    
920            if (get_colorspace(inhdr) != XVID_CSP_NULL) {
921                    memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER));
922                    /* XXX: should we set outhdr->biSize ?? */
923                    return ICERR_OK;
924            }
925            /* --- yv12 --- */
926    
927            result = decompress_query(codec, lpbiInput, lpbiOutput);
928            if (result != ICERR_OK)
929            {
930                    return result;
931            }
932    
933            outhdr->biSize = sizeof(BITMAPINFOHEADER);
934            outhdr->biWidth = inhdr->biWidth;
935            outhdr->biHeight = inhdr->biHeight;
936            outhdr->biPlanes = 1;
937            outhdr->biBitCount = 24;
938            outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */
939            outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount;
940            outhdr->biXPelsPerMeter = 0;
941            outhdr->biYPelsPerMeter = 0;
942            outhdr->biClrUsed = 0;
943            outhdr->biClrImportant = 0;
944    
945            return ICERR_OK;
946    }
947    
948    #define REG_GET_N(X, Y, Z) \
949    { \
950            DWORD size = sizeof(int); \
951            if (RegQueryValueEx(hKey, X, 0, 0, (LPBYTE)&Y, &size) != ERROR_SUCCESS) { \
952                    Y=Z; \
953            } \
954    }while(0)
955    
956    LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput)
957    {
958            xvid_gbl_init_t init;
959            xvid_dec_create_t create;
960            HKEY hKey;
961    
962            if (init_dll() != 0) return ICERR_ERROR;
963    
964            memset(&init, 0, sizeof(init));
965            init.version = XVID_VERSION;
966            init.cpu_flags = codec->config.cpu;
967            xvid_global_func(0, XVID_GBL_INIT, &init, NULL);
968    
969            memset(&create, 0, sizeof(create));
970            create.version = XVID_VERSION;
971            create.width = lpbiInput->bmiHeader.biWidth;
972            create.height = lpbiInput->bmiHeader.biHeight;
973    
974            switch(xvid_decore_func(0, XVID_DEC_CREATE, &create, NULL))
975            {
976            case XVID_ERR_FAIL :
977                    return ICERR_ERROR;
978    
979            case XVID_ERR_MEMORY :
980                    return ICERR_MEMORY;
981    
982            case XVID_ERR_FORMAT :
983                    return ICERR_BADFORMAT;
984    
985            case XVID_ERR_VERSION :
986                    return ICERR_UNSUPPORTED;
987            }
988    
989            codec->dhandle = create.handle;
990    
991            RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey);
992    
993            REG_GET_N("Deblock_Y",  pp_dy, 0)
994            REG_GET_N("Deblock_UV", pp_duv, 0)
995            REG_GET_N("Dering",  pp_dr, 0)
996            REG_GET_N("FilmEffect", pp_fe, 0)
997    
998            RegCloseKey(hKey);
999    
1000            return ICERR_OK;
1001    }
1002    
1003    
1004    LRESULT decompress_end(CODEC * codec)
1005    {
1006            if (m_hdll != NULL) {
1007                    if (codec->dhandle != NULL) {
1008                            xvid_decore_func(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL);
1009                            codec->dhandle = NULL;
1010                    }
1011                    FreeLibrary(m_hdll);
1012                    m_hdll = NULL;
1013            }
1014    
1015            return ICERR_OK;
1016    }
1017    
1018    
1019    LRESULT decompress(CODEC * codec, ICDECOMPRESS * icd)
1020    {
1021            xvid_dec_frame_t frame;
1022    
1023            /* --- yv12 --- */
1024            if (icd->lpbiInput->biCompression != FOURCC_XVID &&
1025                     icd->lpbiInput->biCompression != FOURCC_DIVX &&
1026                     icd->lpbiInput->biCompression != FOURCC_DX50)
1027            {
1028                    xvid_gbl_convert_t convert;
1029    
1030                    DPRINTF("input=%c%c%c%c output=%c%c%c%c",
1031                            icd->lpbiInput->biCompression&0xff,
1032                            (icd->lpbiInput->biCompression>>8)&0xff,
1033                            (icd->lpbiInput->biCompression>>16)&0xff,
1034                            (icd->lpbiInput->biCompression>>24)&0xff,
1035                            icd->lpbiOutput->biCompression&0xff,
1036                            (icd->lpbiOutput->biCompression>>8)&0xff,
1037                            (icd->lpbiOutput->biCompression>>16)&0xff,
1038                            (icd->lpbiOutput->biCompression>>24)&0xff);
1039    
1040                    memset(&convert, 0, sizeof(convert));
1041                    convert.version = XVID_VERSION;
1042    
1043                    convert.input.csp = get_colorspace(icd->lpbiInput);
1044                    convert.input.plane[0] = icd->lpInput;
1045                    convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3;
1046                    if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12)
1047                            convert.input.stride[0] = (convert.input.stride[0]*2)/3;
1048    
1049                    convert.output.csp = get_colorspace(icd->lpbiOutput);
1050                    convert.output.plane[0] = icd->lpOutput;
1051                    convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
1052                    if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12)
1053                            convert.output.stride[0] = (convert.output.stride[0]*2)/3;
1054    
1055                    convert.width = icd->lpbiInput->biWidth;
1056                    convert.height = icd->lpbiInput->biHeight;
1057                    convert.interlacing = 0;
1058                    if (convert.input.csp == XVID_CSP_NULL ||
1059                            convert.output.csp == XVID_CSP_NULL ||
1060                            xvid_global_func(0, XVID_GBL_CONVERT, &convert, NULL) < 0)
1061                    {
1062                             return ICERR_BADFORMAT;
1063                    }
1064                    return ICERR_OK;
1065            }
1066            /* --- yv12 --- */
1067    
1068            memset(&frame, 0, sizeof(frame));
1069            frame.version = XVID_VERSION;
1070            frame.general = XVID_LOWDELAY;  /* force low_delay_default mode */
1071            frame.bitstream = icd->lpInput;
1072            frame.length = icd->lpbiInput->biSizeImage;
1073    
1074            if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL)))
1075            {
1076                    if ((frame.output.csp = get_colorspace(icd->lpbiOutput)) == XVID_CSP_NULL)
1077                    {
1078                            return ICERR_BADFORMAT;
1079                    }
1080                    frame.output.plane[0] = icd->lpOutput;
1081                    frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3;
1082                    if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12)
1083                            frame.output.stride[0] = (frame.output.stride[0]*2)/3;
1084            }
1085            else
1086            {
1087                    frame.output.csp = XVID_CSP_NULL;
1088            }
1089    
1090            if (pp_dy)frame.general |= XVID_DEBLOCKY;
1091            if (pp_duv) frame.general |= XVID_DEBLOCKUV;
1092    /*      if (pp_dr) frame.general |= XVID_DERING; */
1093            if (pp_fe) frame.general |= XVID_FILMEFFECT;
1094    
1095            switch (xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL))
1096            {
1097            case XVID_ERR_FAIL :
1098                    return ICERR_ERROR;
1099    
1100            case XVID_ERR_MEMORY :
1101                    return ICERR_MEMORY;
1102    
1103            case XVID_ERR_FORMAT :
1104                    return ICERR_BADFORMAT;
1105    
1106            case XVID_ERR_VERSION :
1107                    return ICERR_UNSUPPORTED;
1108            }
1109    
1110            return ICERR_OK;
1111    }
1112    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

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