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

Diff of /xvidcore/vfw/src/config.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.1.2.36, Mon Feb 9 10:14:54 2004 UTC
# Line 0  Line 1 
1    /**************************************************************************
2     *
3     *      XVID VFW FRONTEND
4     *      config
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     *      15.06.2002      added bframes options
27     *      21.04.2002      fixed custom matrix support, tried to get dll size down
28     *      17.04.2002      re-enabled lumi masking in 1st pass
29     *      15.04.2002      updated cbr support
30     *      07.04.2002      min keyframe interval checkbox
31     *                              2-pass max bitrate and overflow customization
32     *      04.04.2002      interlacing support
33     *                              hinted ME support
34     *      24.03.2002      daniel smith <danielsmith@astroboymail.com>
35     *                              added Foxer's new CBR engine
36     *                              - cbr_buffer is being used as reaction delay (quick hack)
37     *      23.03.2002      daniel smith <danielsmith@astroboymail.com>
38     *                              added load defaults button
39     *                              merged foxer's alternative 2-pass code (2-pass alt tab)
40     *                              added proper tooltips
41     *                              moved registry data into reg_ints/reg_strs arrays
42     *                              added DEBUGERR output on errors instead of returning
43     *      16.03.2002      daniel smith <danielsmith@astroboymail.com>
44     *                              rewrote/restructured most of file
45     *                              added tooltips (kind of - dirty message hook method)
46     *                              split tabs into a main dialog / advanced prop sheet
47     *                              advanced controls are now enabled/disabled by mode
48     *                              added modulated quantization, DX50 fourcc
49     *      11.03.2002  Min Chen <chenm001@163.com>
50     *                        now get Core Version use xvid_init()
51     *      05.03.2002  Min Chen <chenm001@163.com>
52     *                              Add Core version display to about box
53     *      01.12.2001      inital version; (c)2001 peter ross <pross@xvid.org>
54     *
55     *************************************************************************/
56    
57    
58    #include <windows.h>
59    #include <commctrl.h>
60    #include <stdio.h>  /* sprintf */
61    #include <xvid.h>       /* XviD API */
62    
63    #include "debug.h"
64    #include "config.h"
65    #include "resource.h"
66    
67    
68    #define CONSTRAINVAL(X,Y,Z) if((X)<(Y)) X=Y; if((X)>(Z)) X=Z;
69    #define IsDlgChecked(hwnd,idc)  (IsDlgButtonChecked(hwnd,idc) == BST_CHECKED)
70    #define CheckDlg(hwnd,idc,value) CheckDlgButton(hwnd,idc, value?BST_CHECKED:BST_UNCHECKED)
71    #define EnableDlgWindow(hwnd,idc,state) EnableWindow(GetDlgItem(hwnd,idc),state)
72    
73    static void zones_update(HWND hDlg, CONFIG * config);
74    
75    HINSTANCE g_hInst;
76    HWND g_hTooltip;
77    
78    static int g_use_bitrate = 1;
79    
80    
81    int pp_dy, pp_duv, pp_dr, pp_fe; /* decoder options */
82    
83    /* enumerates child windows, assigns tooltips */
84    BOOL CALLBACK enum_tooltips(HWND hWnd, LPARAM lParam)
85    {
86            char help[500];
87    
88            if (LoadString(g_hInst, GetDlgCtrlID(hWnd), help, 500))
89            {
90                    TOOLINFO ti;
91                    ti.cbSize = sizeof(TOOLINFO);
92                    ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
93                    ti.hwnd = GetParent(hWnd);
94                    ti.uId  = (LPARAM)hWnd;
95                    ti.lpszText = help;
96                    SendMessage(g_hTooltip, TTM_ADDTOOL, 0, (LPARAM)&ti);
97            }
98    
99            return TRUE;
100    }
101    
102    
103    /* ===================================================================================== */
104    /* MPEG-4 PROFILES/LEVELS ============================================================== */
105    /* ===================================================================================== */
106    
107    
108    
109    /* default vbv_occupancy is (64/170)*vbv_buffer_size */
110    
111    const profile_t profiles[] =
112    {
113    /*      name                             p@l,   w       h  fps  obj  Tvmv  vmv  vcv   ac%        vbv      pkt   kbps  flags */
114            { "Simple @ L0",           0x08,  176, 144, 15,  1,  198,   99,   1485, 100,  10*16368,  2048,   64, 0 },
115            /* simple@l0: max f_code=1, intra_dc_vlc_threshold=0 */
116            /* if ac preidition is used, adaptive quantization must not be used */
117            /* <=qcif must be used */
118            { "Simple @ L1",           0x01,  176, 144, 15,  4,  198,   99,   1485, 100,  10*16368,  2048,   64, PROFILE_ADAPTQUANT },
119            { "Simple @ L2",           0x02,  352, 288, 15,  4,  792,  396,   5940, 100,  40*16368,  4096,  128, PROFILE_ADAPTQUANT },
120            { "Simple @ L3",           0x03,  352, 288, 15,  4,  792,  396,  11880, 100,  40*16368,  8192,  384, PROFILE_ADAPTQUANT },
121    
122            { "ARTS @ L1",           0x91,  176, 144, 15,  4,  198,   99,   1485, 100,  10*16368,  8192,   64, PROFILE_ARTS },
123            { "ARTS @ L2",           0x92,  352, 288, 15,  4,  792,  396,   5940, 100,  40*16368, 16384,  128, PROFILE_ARTS },
124            { "ARTS @ L3",           0x93,  352, 288, 30,  4,  792,  396,  11880, 100,  40*16368, 16384,  384, PROFILE_ARTS },
125            { "ARTS @ L4",           0x94,  352, 288, 30, 16,  792,  396,  11880, 100,  80*16368, 16384, 2000, PROFILE_ARTS },
126    
127            { "AS @ L0",               0xf0,  176, 144, 30,  1,  297,   99,   2970, 100,  10*16368,  2048,  128, PROFILE_AS },
128            { "AS @ L1",               0xf1,  176, 144, 30,  4,  297,   99,   2970, 100,  10*16368,  2048,  128, PROFILE_AS },
129            { "AS @ L2",               0xf2,  352, 288, 15,  4, 1188,  396,   5940, 100,  40*16368,  4096,  384, PROFILE_AS },
130            { "AS @ L3",               0xf3,  352, 288, 30,  4, 1188,  396,  11880, 100,  40*16368,  4096,  768, PROFILE_AS },
131     /*  ISMA Profile 1, (ASP) @ L3b (CIF, 1.5 Mb/s) CIF(352x288), 30fps, 1.5Mbps max ??? */
132            { "AS @ L4",               0xf4,  352, 576, 30,  4, 2376,  792,  23760,  50,  80*16368,  8192, 3000, PROFILE_AS },
133            { "AS @ L5",               0xf5,  720, 576, 30,  4, 4860, 1620,  48600,  25, 112*16368, 16384, 8000, PROFILE_AS },
134    
135    #ifdef DXN_PROFILES
136            { "DXN Handheld",          0x00,  176, 144, 15, -1,  198,   99,   1485, 100,  16*16368, -1,  128, PROFILE_ADAPTQUANT },
137            { "DXN Portable NTSC", 0x00,  352, 240, 30, -1,  990,  330,   9900, 100,  64*16368,     -1,  768, PROFILE_ADAPTQUANT|PROFILE_BVOP },
138            { "DXN Portable PAL",  0x00,  352, 288, 25, -1, 1188,  396,   9900, 100,  64*16368,     -1,  768, PROFILE_ADAPTQUANT|PROFILE_BVOP },
139            { "DXN HT NTSC",           0x00,  720, 480, 30, -1, 4050, 1350,  40500, 100, 192*16368, -1, 4000, PROFILE_ADAPTQUANT|PROFILE_BVOP|PROFILE_INTERLACE },
140            { "DXN HT PAL",         0x00,  720, 576, 25, -1, 4860, 1620,  40500, 100, 192*16368,    -1, 4000, PROFILE_ADAPTQUANT|PROFILE_BVOP|PROFILE_INTERLACE },
141            { "DXN HDTV",             0x00, 1280, 720, 30, -1,10800, 3600, 108000, 100, 384*16368,  -1, 8000, PROFILE_ADAPTQUANT|PROFILE_BVOP|PROFILE_INTERLACE },
142    #endif
143    
144            { "(unrestricted)",     0x00,   0,   0,  0,  0, 0,      0,        0, 100,   0*16368,     0,     0, 0xffffffff },
145    };
146    
147    
148    typedef struct {
149            char * name;
150            float value;
151    } named_float_t;
152    
153    static const named_float_t video_fps_list[] = {
154            {  "15.0",                      15.0F   },
155            {  "23.976 (FILM)",     23.976F },
156            {  "25.0 (PAL)",        25.0F   },
157            {  "29.97 (NTSC)",      29.970F },
158            {  "30.0",                      30.0F   }
159    };
160    
161    
162    typedef struct {
163            char * name;
164            int avi_interval;               /* audio overhead intervals (milliseconds) */
165            float mkv_multiplier;   /* mkv multiplier */
166    } named_int_t;
167    
168    
169    #define NO_AUDIO        5
170    static const named_int_t audio_type_list[] = {
171            {       "MP3-CBR",              1000,   48000/1152/6                                    },
172            {       "MP3-VBR",                24,   48000/1152/6                                    },
173            {       "OGG",     /*?*/1000,   48000*(0.7F/1024 + 0.3F/180)    },
174            {       "AC3",                    64,   48000/1536/6                                    },
175            {       "DTS",                    21,   /*?*/48000/1152/6                               },
176            {       "(None)",                  0,   0                                                               },
177    };
178    
179    
180    /* ===================================================================================== */
181    /* REGISTRY ============================================================================ */
182    /* ===================================================================================== */
183    
184    /* registry info structs */
185    CONFIG reg;
186    
187    static const REG_INT reg_ints[] = {
188            {"mode",                                        &reg.mode,                                              RC_MODE_1PASS},
189            {"bitrate",                                     &reg.bitrate,                                   700},
190            {"desired_size",                        &reg.desired_size,                              570000},
191            {"use_2pass_bitrate",           &reg.use_2pass_bitrate,                 0},
192            {"desired_quant",                       &reg.desired_quant,                             DEFAULT_QUANT}, /* 100-base float */
193    
194            /* profile */
195            {"quant_type",                          &reg.quant_type,                                0},
196            {"lum_masking",                         &reg.lum_masking,                               0},
197            {"interlacing",                         &reg.interlacing,                               0},
198            {"qpel",                                        &reg.qpel,                                              0},
199            {"gmc",                                         &reg.gmc,                                               0},
200            {"reduced_resolution",          &reg.reduced_resolution,                0},
201            {"use_bvop",                            &reg.use_bvop,                                  1},
202            {"max_bframes",                         &reg.max_bframes,                               2},
203            {"bquant_ratio",                        &reg.bquant_ratio,                              150},   /* 100-base float */
204            {"bquant_offset",                       &reg.bquant_offset,                             100},   /* 100-base float */
205            {"packed",                                      &reg.packed,                                    1},
206            {"closed_gov",                          &reg.closed_gov,                                1},
207    
208            /* aspect ratio */
209            {"ar_mode",                                     &reg.ar_mode,                                   0},
210            {"aspect_ratio",                        &reg.display_aspect_ratio,              0},
211            {"par_x",                                       &reg.par_x,                                             1},
212            {"par_y",                                       &reg.par_y,                                             1},
213            {"ar_x",                                        &reg.ar_x,                                              4},
214            {"ar_y",                                        &reg.ar_y,                                              3},
215    
216            /* zones */
217            {"num_zones",                           &reg.num_zones,                                 1},
218    
219            /* single pass */
220            {"rc_reaction_delay_factor",&reg.rc_reaction_delay_factor,      16},
221            {"rc_averaging_period",         &reg.rc_averaging_period,               100},
222            {"rc_buffer",                           &reg.rc_buffer,                                 100},
223    
224            /* 2pass1 */
225            {"discard1pass",                        &reg.discard1pass,                              1},
226            {"full1pass",                           &reg.full1pass,                                 0},
227    
228            /* 2pass2 */
229            {"keyframe_boost",                      &reg.keyframe_boost,                    10},
230            {"kfreduction",                         &reg.kfreduction,                               20},
231            {"kfthreshold",                         &reg.kfthreshold,                               1},
232            {"curve_compression_high",      &reg.curve_compression_high,    0},
233            {"curve_compression_low",       &reg.curve_compression_low,             0},
234            {"overflow_control_strength", &reg.overflow_control_strength, 5},
235            {"twopass_max_overflow_improvement", &reg.twopass_max_overflow_improvement, 5},
236            {"twopass_max_overflow_degradation", &reg.twopass_max_overflow_degradation, 5},
237    
238            /* bitrate calculator */
239            {"container_type",                      &reg.container_type,                    1},
240            {"target_size",                         &reg.target_size,                               650 * 1024},
241            {"subtitle_size",                       &reg.subtitle_size,                             0},
242            {"hours",                               &reg.hours,                                             1},
243            {"minutes",                             &reg.minutes,                                   30},
244            {"seconds",                             &reg.seconds,                                   0},
245            {"fps",                                 &reg.fps,                                               2},
246            {"audio_mode",                          &reg.audio_mode,                                0},
247            {"audio_type",                          &reg.audio_type,                                0},
248            {"audio_rate",                          &reg.audio_rate,                                128},
249            {"audio_size",                          &reg.audio_size,                                0},
250    
251            /* motion */
252            {"motion_search",                       &reg.motion_search,                             6},
253            {"vhq_mode",                            &reg.vhq_mode,                                  1},
254            {"chromame",                            &reg.chromame,                                  1},
255            {"cartoon_mode",                        &reg.cartoon_mode,                              0},
256            {"turbo",                                       &reg.turbo,                                             0},
257            {"max_key_interval",            &reg.max_key_interval,                  300},
258            {"frame_drop_ratio",            &reg.frame_drop_ratio,                  0},
259    
260            /* quant */
261            {"min_iquant",                          &reg.min_iquant,                                1},
262            {"max_iquant",                          &reg.max_iquant,                                31},
263            {"min_pquant",                          &reg.min_pquant,                                1},
264            {"max_pquant",                          &reg.max_pquant,                                31},
265            {"min_bquant",                          &reg.min_bquant,                                1},
266            {"max_bquant",                          &reg.max_bquant,                                31},
267            {"trellis_quant",                       &reg.trellis_quant,                             0},
268    
269            /* debug */
270            {"fourcc_used",                         &reg.fourcc_used,                               0},
271            {"debug",                                       &reg.debug,                                             0x0},
272            {"vop_debug",                           &reg.vop_debug,                                 0},
273            {"display_status",                      &reg.display_status,                    1},
274    
275            /* decoder, shared with dshow */
276            {"Deblock_Y",                           &pp_dy,                                                 0},
277            {"Deblock_UV",                          &pp_duv,                                                0},
278            {"Dering",                                      &pp_dr,                                                 0},
279            {"FilmEffect",                          &pp_fe,                                                 0},
280    
281    };
282    
283    static const REG_STR reg_strs[] = {
284            {"profile",                                     reg.profile_name,                               "AS @ L5"},
285            {"stats",                                       reg.stats,                                              CONFIG_2PASS_FILE},
286    };
287    
288    
289    zone_t stmp;
290    static const REG_INT reg_zone[] = {
291            {"zone%i_frame",                        &stmp.frame,                                    0},
292            {"zone%i_mode",                         &stmp.mode,                                             RC_ZONE_WEIGHT},
293            {"zone%i_weight",                       &stmp.weight,                                   100},     /* 100-base float */
294            {"zone%i_quant",                        &stmp.quant,                                    500},     /* 100-base float */
295            {"zone%i_type",                         &stmp.type,                                             XVID_TYPE_AUTO},
296            {"zone%i_greyscale",            &stmp.greyscale,                                0},
297            {"zone%i_chroma_opt",           &stmp.chroma_opt,                               0},
298            {"zone%i_bvop_threshold",   &stmp.bvop_threshold,                       0},
299    };
300    
301    static const BYTE default_qmatrix_intra[] = {
302            8, 17,18,19,21,23,25,27,
303            17,18,19,21,23,25,27,28,
304            20,21,22,23,24,26,28,30,
305            21,22,23,24,26,28,30,32,
306            22,23,24,26,28,30,32,35,
307            23,24,26,28,30,32,35,38,
308            25,26,28,30,32,35,38,41,
309            27,28,30,32,35,38,41,45
310    };
311    
312    static const BYTE default_qmatrix_inter[] = {
313            16,17,18,19,20,21,22,23,
314            17,18,19,20,21,22,23,24,
315            18,19,20,21,22,23,24,25,
316            19,20,21,22,23,24,26,27,
317            20,21,22,23,25,26,27,28,
318            21,22,23,24,26,27,28,30,
319            22,23,24,26,27,28,30,31,
320            23,24,25,27,28,30,31,33
321    };
322    
323    
324    
325    #define REG_GET_B(X, Y, Z) size=sizeof((Z));if(RegQueryValueEx(hKey, X, 0, 0, Y, &size) != ERROR_SUCCESS) {memcpy(Y, Z, sizeof((Z)));}
326    
327    #define XVID_DLL_NAME "xvidcore.dll"
328    
329    void config_reg_get(CONFIG * config)
330    {
331            char tmp[32];
332            HKEY hKey;
333            DWORD size;
334            int i,j;
335            xvid_gbl_info_t info;
336            HINSTANCE m_hdll;
337    
338            memset(&info, 0, sizeof(info));
339            info.version = XVID_VERSION;
340    
341            m_hdll = LoadLibrary(XVID_DLL_NAME);
342            if (m_hdll != NULL) {
343    
344                    ((int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global"))
345                            (0, XVID_GBL_INFO, &info, NULL);
346    
347                    FreeLibrary(m_hdll);
348            }
349    
350            reg.cpu = info.cpu_flags;
351            reg.num_threads = info.num_threads;
352    
353            RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey);
354    
355            /* read integer values */
356            for (i=0 ; i<sizeof(reg_ints)/sizeof(REG_INT); i++) {
357                    size = sizeof(int);
358                    if (RegQueryValueEx(hKey, reg_ints[i].reg_value, 0, 0, (LPBYTE)reg_ints[i].config_int, &size) != ERROR_SUCCESS) {
359                            *reg_ints[i].config_int = reg_ints[i].def;
360                    }
361            }
362    
363            /* read string values */
364            for (i=0 ; i<sizeof(reg_strs)/sizeof(REG_STR); i++) {
365                    size = MAX_PATH;
366                    if (RegQueryValueEx(hKey, reg_strs[i].reg_value, 0, 0, (LPBYTE)reg_strs[i].config_str, &size) != ERROR_SUCCESS) {
367                            memcpy(reg_strs[i].config_str, reg_strs[i].def, MAX_PATH);
368                    }
369            }
370    
371            reg.profile = 0;
372            for (i=0; i<sizeof(profiles)/sizeof(profile_t); i++) {
373                    if (lstrcmpi(profiles[i].name, reg.profile_name) == 0) {
374                            reg.profile = i;
375                    }
376            }
377    
378            memcpy(config, &reg, sizeof(CONFIG));
379    
380    
381            /* read quant matrices */
382            REG_GET_B("qmatrix_intra", config->qmatrix_intra, default_qmatrix_intra);
383            REG_GET_B("qmatrix_inter", config->qmatrix_inter, default_qmatrix_inter);
384    
385    
386            /* read zones */
387            if (config->num_zones>MAX_ZONES) {
388                    config->num_zones=MAX_ZONES;
389            }else if (config->num_zones<=0) {
390                    config->num_zones = 1;
391            }
392    
393            for (i=0; i<config->num_zones; i++) {
394                    for (j=0; j<sizeof(reg_zone)/sizeof(REG_INT); j++)  {
395                            size = sizeof(int);
396    
397                            wsprintf(tmp, reg_zone[j].reg_value, i);
398                            if (RegQueryValueEx(hKey, tmp, 0, 0, (LPBYTE)reg_zone[j].config_int, &size) != ERROR_SUCCESS)
399                                    *reg_zone[j].config_int = reg_zone[j].def;
400                    }
401    
402                    memcpy(&config->zones[i], &stmp, sizeof(zone_t));
403            }
404    
405            RegCloseKey(hKey);
406    }
407    
408    
409    /* put config settings in registry */
410    
411    #define REG_SET_B(X, Y) RegSetValueEx(hKey, X, 0, REG_BINARY, Y, sizeof((Y)))
412    
413    void config_reg_set(CONFIG * config)
414    {
415            char tmp[64];
416            HKEY hKey;
417            DWORD dispo;
418            int i,j;
419    
420            if (RegCreateKeyEx(
421                            XVID_REG_KEY,
422                            XVID_REG_PARENT "\\" XVID_REG_CHILD,
423                            0,
424                            XVID_REG_CLASS,
425                            REG_OPTION_NON_VOLATILE,
426                            KEY_WRITE,
427                            0,
428                            &hKey,
429                            &dispo) != ERROR_SUCCESS)
430            {
431                    DPRINTF("Couldn't create XVID_REG_SUBKEY - GetLastError=%i", GetLastError());
432                    return;
433            }
434    
435            memcpy(&reg, config, sizeof(CONFIG));
436    
437            /* set integer values */
438            for (i=0 ; i<sizeof(reg_ints)/sizeof(REG_INT); i++) {
439                    RegSetValueEx(hKey, reg_ints[i].reg_value, 0, REG_DWORD, (LPBYTE)reg_ints[i].config_int, sizeof(int));
440            }
441    
442            /* set string values */
443            strcpy(reg.profile_name, profiles[reg.profile].name);
444            for (i=0 ; i<sizeof(reg_strs)/sizeof(REG_STR); i++) {
445                    RegSetValueEx(hKey, reg_strs[i].reg_value, 0, REG_SZ, reg_strs[i].config_str, lstrlen(reg_strs[i].config_str)+1);
446            }
447    
448            /* set quant matrices */
449            REG_SET_B("qmatrix_intra", config->qmatrix_intra);
450            REG_SET_B("qmatrix_inter", config->qmatrix_inter);
451    
452            /* set seections */
453            for (i=0; i<config->num_zones; i++) {
454                    memcpy(&stmp, &config->zones[i], sizeof(zone_t));
455                    for (j=0; j<sizeof(reg_zone)/sizeof(REG_INT); j++)  {
456                            wsprintf(tmp, reg_zone[j].reg_value, i);
457                            RegSetValueEx(hKey, tmp, 0, REG_DWORD, (LPBYTE)reg_zone[j].config_int, sizeof(int));
458                    }
459            }
460    
461            RegCloseKey(hKey);
462    }
463    
464    
465    /* clear XviD registry key, load defaults */
466    
467    static void config_reg_default(CONFIG * config)
468    {
469            HKEY hKey;
470    
471            if (RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT, 0, KEY_ALL_ACCESS, &hKey)) {
472                    DPRINTF("Couldn't open registry key for deletion - GetLastError=%i", GetLastError());
473                    return;
474            }
475    
476            if (RegDeleteKey(hKey, XVID_REG_CHILD)) {
477                    DPRINTF("Couldn't delete registry key - GetLastError=%i", GetLastError());
478                    return;
479            }
480    
481            RegCloseKey(hKey);
482            config_reg_get(config);
483            config_reg_set(config);
484    }
485    
486    
487    /* leaves current config value if dialog item is empty */
488    static int config_get_int(HWND hDlg, INT item, int config)
489    {
490            BOOL success = FALSE;
491            int tmp = GetDlgItemInt(hDlg, item, &success, TRUE);
492            return (success) ? tmp : config;
493    }
494    
495    
496    static int config_get_uint(HWND hDlg, UINT item, int config)
497    {
498            BOOL success = FALSE;
499            int tmp = GetDlgItemInt(hDlg, item, &success, FALSE);
500            return (success) ? tmp : config;
501    }
502    
503    
504    /* we use "100 base" floats */
505    
506    #define FLOAT_BUF_SZ    20
507    static int get_dlgitem_float(HWND hDlg, UINT item, int def)
508    {
509            char buf[FLOAT_BUF_SZ];
510    
511            if (GetDlgItemText(hDlg, item, buf, FLOAT_BUF_SZ) == 0)
512                    return def;
513    
514            return (int)(atof(buf)*100);
515    }
516    
517    static void set_dlgitem_float(HWND hDlg, UINT item, int value)
518    {
519            char buf[FLOAT_BUF_SZ];
520            sprintf(buf, "%.2f", (float)value/100);
521            SetDlgItemText(hDlg, item, buf);
522    }
523    
524    
525    #define HEX_BUF_SZ  16
526    static unsigned int get_dlgitem_hex(HWND hDlg, UINT item, unsigned int def)
527    {
528            char buf[HEX_BUF_SZ];
529            unsigned int value;
530    
531            if (GetDlgItemText(hDlg, item, buf, HEX_BUF_SZ) == 0)
532                    return def;
533    
534            if (sscanf(buf,"0x%x", &value)==1 || sscanf(buf,"%x", &value)==1) {
535                    return value;
536            }
537    
538            return def;
539    }
540    
541    static void set_dlgitem_hex(HWND hDlg, UINT item, int value)
542    {
543            char buf[HEX_BUF_SZ];
544            wsprintf(buf, "0x%x", value);
545            SetDlgItemText(hDlg, item, buf);
546    }
547    
548    /* ===================================================================================== */
549    /* QUANT MATRIX DIALOG ================================================================= */
550    /* ===================================================================================== */
551    
552    static void quant_upload(HWND hDlg, CONFIG* config)
553    {
554            int i;
555    
556            for (i=0 ; i<64; i++) {
557                    SetDlgItemInt(hDlg, IDC_QINTRA00 + i, config->qmatrix_intra[i], FALSE);
558                    SetDlgItemInt(hDlg, IDC_QINTER00 + i, config->qmatrix_inter[i], FALSE);
559            }
560    }
561    
562    
563    static void quant_download(HWND hDlg, CONFIG* config)
564    {
565            int i;
566    
567            for (i=0; i<64; i++) {
568                    int temp;
569    
570                    temp = config_get_uint(hDlg, i + IDC_QINTRA00, config->qmatrix_intra[i]);
571                    CONSTRAINVAL(temp, 1, 255);
572                    config->qmatrix_intra[i] = temp;
573    
574                    temp = config_get_uint(hDlg, i + IDC_QINTER00, config->qmatrix_inter[i]);
575                    CONSTRAINVAL(temp, 1, 255);
576                    config->qmatrix_inter[i] = temp;
577            }
578    }
579    
580    
581    static void quant_loadsave(HWND hDlg, CONFIG * config, int save)
582    {
583            char file[MAX_PATH];
584            OPENFILENAME ofn;
585            HANDLE hFile;
586            DWORD read=128, wrote=0;
587            BYTE quant_data[128];
588    
589            strcpy(file, "\\matrix");
590            memset(&ofn, 0, sizeof(OPENFILENAME));
591            ofn.lStructSize = sizeof(OPENFILENAME);
592    
593            ofn.hwndOwner = hDlg;
594            ofn.lpstrFilter = "All files (*.*)\0*.*\0\0";
595            ofn.lpstrFile = file;
596            ofn.nMaxFile = MAX_PATH;
597            ofn.Flags = OFN_PATHMUSTEXIST;
598    
599            if (save) {
600                    ofn.Flags |= OFN_OVERWRITEPROMPT;
601                    if (GetSaveFileName(&ofn)) {
602                            hFile = CreateFile(file, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
603    
604                            quant_download(hDlg, config);
605                            memcpy(quant_data, config->qmatrix_intra, 64);
606                            memcpy(quant_data+64, config->qmatrix_inter, 64);
607    
608                            if (hFile == INVALID_HANDLE_VALUE) {
609                                    DPRINTF("Couldn't save quant matrix");
610                            }else{
611                                    if (!WriteFile(hFile, quant_data, 128, &wrote, 0)) {
612                                            DPRINTF("Couldnt write quant matrix");
613                                    }
614                            }
615                            CloseHandle(hFile);
616                    }
617            }else{
618                    ofn.Flags |= OFN_FILEMUSTEXIST;
619                    if (GetOpenFileName(&ofn)) {
620                            hFile = CreateFile(file, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
621    
622                            if (hFile == INVALID_HANDLE_VALUE) {
623                                    DPRINTF("Couldn't load quant matrix");
624                            } else {
625                                    if (!ReadFile(hFile, quant_data, 128, &read, 0)) {
626                                            DPRINTF("Couldnt read quant matrix");
627                                    }else{
628                                            memcpy(config->qmatrix_intra, quant_data, 64);
629                                            memcpy(config->qmatrix_inter, quant_data+64, 64);
630                                            quant_upload(hDlg, config);
631                                    }
632                            }
633                            CloseHandle(hFile);
634                    }
635            }
636    }
637    
638    /* quantization matrix dialog proc */
639    
640    static BOOL CALLBACK quantmatrix_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
641    {
642            CONFIG* config = (CONFIG*)GetWindowLong(hDlg, GWL_USERDATA);
643    
644            switch (uMsg)
645            {
646            case WM_INITDIALOG :
647                    SetWindowLong(hDlg, GWL_USERDATA, lParam);
648                    config = (CONFIG*)lParam;
649                    quant_upload(hDlg, config);
650    
651                    if (g_hTooltip)
652                    {
653                            EnumChildWindows(hDlg, enum_tooltips, 0);
654                    }
655                    break;
656    
657            case WM_COMMAND :
658    
659                    if (HIWORD(wParam) == BN_CLICKED) {
660                            switch(LOWORD(wParam)) {
661                            case IDOK :
662                                    quant_download(hDlg, config);
663                                    EndDialog(hDlg, IDOK);
664                                    break;
665    
666                            case IDCANCEL :
667                                    EndDialog(hDlg, IDCANCEL);
668                                    break;
669    
670                            case IDC_SAVE :
671                                    quant_loadsave(hDlg, config, 1);
672                                    break;
673    
674                            case IDC_LOAD :
675                                    quant_loadsave(hDlg, config, 0);
676                                    break;
677    
678                            default :
679                                    return FALSE;
680                            }
681                            break;
682                    }
683                    return FALSE;
684    
685            default :
686                    return FALSE;
687            }
688    
689            return TRUE;
690    }
691    
692    
693    /* ===================================================================================== */
694    /* ADVANCED DIALOG PAGES ================================================================ */
695    /* ===================================================================================== */
696    
697    /* initialise pages */
698    static void adv_init(HWND hDlg, int idd, CONFIG * config)
699    {
700            unsigned int i;
701    
702            switch(idd) {
703            case IDD_PROFILE :
704                    for (i=0; i<sizeof(profiles)/sizeof(profile_t); i++)
705                            SendDlgItemMessage(hDlg, IDC_PROFILE_PROFILE, CB_ADDSTRING, 0, (LPARAM)profiles[i].name);
706                    SendDlgItemMessage(hDlg, IDC_QUANTTYPE, CB_ADDSTRING, 0, (LPARAM)"H.263");
707                    SendDlgItemMessage(hDlg, IDC_QUANTTYPE, CB_ADDSTRING, 0, (LPARAM)"MPEG");
708                    SendDlgItemMessage(hDlg, IDC_QUANTTYPE, CB_ADDSTRING, 0, (LPARAM)"MPEG-Custom");
709                    break;
710    
711            case IDD_AR:
712                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_ADDSTRING, 0, (LPARAM)"Square (default)");
713                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_ADDSTRING, 0, (LPARAM)"4:3 PAL");
714                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_ADDSTRING, 0, (LPARAM)"4:3 NTSC");
715                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_ADDSTRING, 0, (LPARAM)"16:9 PAL");
716                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_ADDSTRING, 0, (LPARAM)"16:9 NTSC");
717                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_ADDSTRING, 0, (LPARAM)"Custom...");
718                    break;
719    
720            case IDD_LEVEL :
721                    for (i=0; i<sizeof(profiles)/sizeof(profile_t); i++)
722                            SendDlgItemMessage(hDlg, IDC_LEVEL_PROFILE, CB_ADDSTRING, 0, (LPARAM)profiles[i].name);
723                    break;
724    
725            case IDD_BITRATE :
726                    SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_ADDSTRING, 0, (LPARAM)"AVI-Legacy");
727                    SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_ADDSTRING, 0, (LPARAM)"AVI-OpenDML");
728                    SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_ADDSTRING, 0, (LPARAM)"Matroska");
729                    SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_ADDSTRING, 0, (LPARAM)"OGM");
730                    SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_ADDSTRING, 0, (LPARAM)"(None)");
731    
732                    SendDlgItemMessage(hDlg, IDC_BITRATE_TSIZE, CB_ADDSTRING, 0, (LPARAM)"665600");
733                    SendDlgItemMessage(hDlg, IDC_BITRATE_TSIZE, CB_ADDSTRING, 0, (LPARAM)"716800");
734    
735                    for (i=0; i<sizeof(video_fps_list)/sizeof(named_float_t); i++)
736                            SendDlgItemMessage(hDlg, IDC_BITRATE_FPS, CB_ADDSTRING, 0, (LPARAM)video_fps_list[i].name);
737    
738                    for (i=0; i<sizeof(audio_type_list)/sizeof(named_int_t); i++)
739                            SendDlgItemMessage(hDlg, IDC_BITRATE_AFORMAT, CB_ADDSTRING, 0, (LPARAM)audio_type_list[i].name);
740    
741                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"32");
742                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"56");
743                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"64");
744                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"96");
745                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"112");
746                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"128");
747                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"160");
748                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"192");
749                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"224");
750                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"256");
751                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"384");
752                    SendDlgItemMessage(hDlg, IDC_BITRATE_ARATE, CB_ADDSTRING, 0, (LPARAM)"512");
753                    break;
754    
755            case IDD_ZONE :
756                    EnableDlgWindow(hDlg, IDC_ZONE_FETCH, config->ci_valid);
757                    break;
758    
759            case IDD_MOTION :
760                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"0 - None");
761                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"1 - Very Low");
762                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"2 - Low");
763                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"3 - Medium");
764                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"4 - High");
765                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"5 - Very High");
766                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_ADDSTRING, 0, (LPARAM)"6 - Ultra High");
767    
768                    SendDlgItemMessage(hDlg, IDC_VHQ, CB_ADDSTRING, 0, (LPARAM)"0 - Off");
769                    SendDlgItemMessage(hDlg, IDC_VHQ, CB_ADDSTRING, 0, (LPARAM)"1 - Mode Decision");
770                    SendDlgItemMessage(hDlg, IDC_VHQ, CB_ADDSTRING, 0, (LPARAM)"2 - Limited Search");
771                    SendDlgItemMessage(hDlg, IDC_VHQ, CB_ADDSTRING, 0, (LPARAM)"3 - Medium Search");
772                    SendDlgItemMessage(hDlg, IDC_VHQ, CB_ADDSTRING, 0, (LPARAM)"4 - Wide Search");
773                    break;
774    
775            case IDD_DEBUG :
776                    /* force threads disabled */
777                    EnableWindow(GetDlgItem(hDlg, IDC_NUMTHREADS_STATIC), FALSE);
778                    EnableWindow(GetDlgItem(hDlg, IDC_NUMTHREADS), FALSE);
779    
780                    SendDlgItemMessage(hDlg, IDC_FOURCC, CB_ADDSTRING, 0, (LPARAM)"XVID");
781                    SendDlgItemMessage(hDlg, IDC_FOURCC, CB_ADDSTRING, 0, (LPARAM)"DIVX");
782                    SendDlgItemMessage(hDlg, IDC_FOURCC, CB_ADDSTRING, 0, (LPARAM)"DX50");
783                    break;
784            }
785    }
786    
787    
788    /* enable/disable controls based on encoder-mode or user selection */
789    
790    static void adv_mode(HWND hDlg, int idd, CONFIG * config)
791    {
792            int profile;
793            int weight_en, quant_en;
794            int cpu_force;
795            int custom_quant, bvops;
796            int ar_mode, ar_par;
797    
798            switch(idd) {
799            case IDD_PROFILE :
800                    profile = SendDlgItemMessage(hDlg, IDC_PROFILE_PROFILE, CB_GETCURSEL, 0, 0);
801                    EnableDlgWindow(hDlg, IDC_BVOP, profiles[profile].flags&PROFILE_BVOP);
802    
803                    EnableDlgWindow(hDlg, IDC_QUANTTYPE_S, profiles[profile].flags&PROFILE_MPEGQUANT);
804                    EnableDlgWindow(hDlg, IDC_QUANTTYPE_S, profiles[profile].flags&PROFILE_MPEGQUANT);
805                    EnableDlgWindow(hDlg, IDC_QUANTTYPE, profiles[profile].flags&PROFILE_MPEGQUANT);
806                    custom_quant = (profiles[profile].flags&PROFILE_MPEGQUANT) && SendDlgItemMessage(hDlg, IDC_QUANTTYPE, CB_GETCURSEL, 0, 0)==QUANT_MODE_CUSTOM;
807                    EnableDlgWindow(hDlg, IDC_QUANTMATRIX, custom_quant);
808                    EnableDlgWindow(hDlg, IDC_LUMMASK, profiles[profile].flags&PROFILE_ADAPTQUANT);
809                    EnableDlgWindow(hDlg, IDC_INTERLACING, profiles[profile].flags&PROFILE_INTERLACE);
810                    EnableDlgWindow(hDlg, IDC_QPEL, profiles[profile].flags&PROFILE_QPEL);
811                    EnableDlgWindow(hDlg, IDC_GMC, profiles[profile].flags&PROFILE_GMC);
812                    EnableDlgWindow(hDlg, IDC_REDUCED, profiles[profile].flags&PROFILE_REDUCED);
813    
814                    bvops = (profiles[profile].flags&PROFILE_BVOP) && IsDlgChecked(hDlg, IDC_BVOP);
815                    EnableDlgWindow(hDlg, IDC_MAXBFRAMES,      bvops);
816                    EnableDlgWindow(hDlg, IDC_BQUANTRATIO,    bvops);
817                    EnableDlgWindow(hDlg, IDC_BQUANTOFFSET,  bvops);
818                    EnableDlgWindow(hDlg, IDC_MAXBFRAMES_S,  bvops);
819                    EnableDlgWindow(hDlg, IDC_BQUANTRATIO_S,        bvops);
820                    EnableDlgWindow(hDlg, IDC_BQUANTOFFSET_S,   bvops);
821                    EnableDlgWindow(hDlg, IDC_PACKED,                  bvops);
822                    EnableDlgWindow(hDlg, IDC_CLOSEDGOV,            bvops);
823                    break;
824    
825            case IDD_AR:
826                    ar_mode = IsDlgChecked(hDlg, IDC_PAR);
827                    EnableDlgWindow(hDlg, IDC_ASPECT_RATIO, ar_mode);
828    
829                    ar_par = SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_GETCURSEL, 0, 0);
830                    if (ar_par == 5) { /* custom par */
831                            SetDlgItemInt(hDlg, IDC_PARY, config->par_y, FALSE);
832                            SetDlgItemInt(hDlg, IDC_PARX, config->par_x, FALSE);
833    
834                            EnableDlgWindow(hDlg, IDC_PARX, ar_mode);
835                            EnableDlgWindow(hDlg, IDC_PARY, ar_mode);
836                    } else {
837                            SetDlgItemInt(hDlg, IDC_PARX, PARS[ar_par][0], FALSE);
838                            SetDlgItemInt(hDlg, IDC_PARY, PARS[ar_par][1], FALSE);
839                            EnableDlgWindow(hDlg, IDC_PARX, FALSE);
840                            EnableDlgWindow(hDlg, IDC_PARY, FALSE);
841                    }
842    
843                    ar_mode = IsDlgChecked(hDlg, IDC_AR);
844    
845                    config->ar_x = config_get_uint(hDlg, IDC_ARX, config->ar_x);
846                    config->ar_y = config_get_uint(hDlg, IDC_ARY, config->ar_y);
847    
848                    EnableDlgWindow(hDlg, IDC_ARX, ar_mode);
849                    EnableDlgWindow(hDlg, IDC_ARY, ar_mode);
850                    break;
851    
852            case IDD_LEVEL :
853                    profile = SendDlgItemMessage(hDlg, IDC_LEVEL_PROFILE, CB_GETCURSEL, 0, 0);
854                    SetDlgItemInt(hDlg, IDC_LEVEL_WIDTH, profiles[profile].width, FALSE);
855                    SetDlgItemInt(hDlg, IDC_LEVEL_HEIGHT, profiles[profile].height, FALSE);
856                    SetDlgItemInt(hDlg, IDC_LEVEL_FPS, profiles[profile].fps, FALSE);
857                    SetDlgItemInt(hDlg, IDC_LEVEL_VMV, profiles[profile].max_vmv_buffer_sz, FALSE);
858                    SetDlgItemInt(hDlg, IDC_LEVEL_VCV, profiles[profile].vcv_decoder_rate, FALSE);
859                    SetDlgItemInt(hDlg, IDC_LEVEL_VBV, profiles[profile].max_vbv_size, FALSE);
860                    SetDlgItemInt(hDlg, IDC_LEVEL_BITRATE, profiles[profile].max_bitrate, FALSE);
861                    break;
862    
863            case IDD_BITRATE :
864                    {
865                            int ctype = SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_GETCURSEL, 0, 0);
866                            int target_size = config_get_uint(hDlg, IDC_BITRATE_TSIZE, 0);
867                            int subtitle_size = config_get_uint(hDlg, IDC_BITRATE_SSIZE, 0);
868                            int fps = SendDlgItemMessage(hDlg, IDC_BITRATE_FPS, CB_GETCURSEL, 0, 0);
869    
870                            int duration =
871                                    3600 * config_get_uint(hDlg, IDC_BITRATE_HOURS, 0) +
872                                    60 * config_get_uint(hDlg, IDC_BITRATE_MINUTES, 0) +
873                                    config_get_uint(hDlg, IDC_BITRATE_SECONDS, 0);
874    
875                            int audio_type = SendDlgItemMessage(hDlg, IDC_BITRATE_AFORMAT, CB_GETCURSEL, 0, 0);
876                            int audio_mode = IsDlgChecked(hDlg, IDC_BITRATE_AMODE_SIZE);
877                            int audio_rate = config_get_uint(hDlg, IDC_BITRATE_ARATE, 0);
878                            int audio_size = config_get_uint(hDlg, IDC_BITRATE_ASIZE, 0);
879    
880                            int frames;
881                            int overhead;
882                            int vsize;
883    
884                            if (duration == 0)
885                                    break;
886    
887                            if (fps < 0 || fps >= sizeof(video_fps_list)/sizeof(named_float_t)) {
888                                    fps = 0;
889                            }
890                            if (audio_type < 0 || audio_type >= sizeof(audio_type_list)/sizeof(named_int_t)) {
891                                    audio_type = 0;
892                            }
893    
894                            EnableDlgWindow(hDlg, IDC_BITRATE_AMODE_RATE, audio_type!=NO_AUDIO);
895                            EnableDlgWindow(hDlg, IDC_BITRATE_AMODE_SIZE, audio_type!=NO_AUDIO);
896                            EnableDlgWindow(hDlg, IDC_BITRATE_ARATE, audio_type!=NO_AUDIO && !audio_mode);
897                            EnableDlgWindow(hDlg, IDC_BITRATE_ASIZE, audio_type!=NO_AUDIO && audio_mode);
898                            EnableDlgWindow(hDlg, IDC_BITRATE_ASELECT, audio_type!=NO_AUDIO && audio_mode);
899    
900                            /* step 1: calculate number of frames */
901                            frames = (int)(duration * video_fps_list[fps].value);
902    
903                            /* step 2: calculate audio_size (kbytes)*/
904                            if (audio_type!=NO_AUDIO) {
905                                    if (audio_mode==0) {
906                                            audio_size = (duration * audio_rate) / 8;
907                                    }
908                            }else{
909                                    audio_size = 0;
910                            }
911    
912                            /* step 3: calculate container overhead */
913    
914                            switch(ctype) {
915                            case 0 :        /* AVI */
916                            case 1 :        /* AVI-OpenDML */
917    
918                                    overhead = frames;
919    
920                                    if (audio_type!=NO_AUDIO) {
921                                            overhead += (duration * 1000) / audio_type_list[audio_type].avi_interval;
922                                    }
923    
924                                    overhead *= (ctype==0) ? 24 : 16;
925    
926                                    overhead /= 1024;
927                                    break;
928    
929                            case 2 :        /* Matroska: gknot formula */
930    
931                                    /* common overhead */
932                                    overhead = 40 + 12 + 8+ 16*duration + 200 + 100*1/*one audio stream*/ + 11*duration;
933    
934                                    /* video overhead */
935                                    overhead += frames*8 + (int)(frames * 4 * 0.94);
936    
937                                    /* cue tables and menu seek entries (300k default) */
938                                    overhead += 300 * 1024;
939    
940                                    /* audio */
941                                    overhead += (int)(duration * audio_type_list[audio_type].mkv_multiplier);
942    
943                                    overhead /= 1024;
944                                    break;
945    
946                            case 3 :        /* OGM: inaccurate model */
947                                    overhead = (int)(0.0039F * (target_size - subtitle_size));
948                                    break;
949    
950                            default :       /* (none) */
951                                    overhead = 0;
952                                    break;
953                            }
954    
955                            SetDlgItemInt(hDlg, IDC_BITRATE_COVERHEAD, overhead, TRUE);
956    
957                            /* final video bitstream size */
958                            vsize = target_size - subtitle_size - audio_size - overhead;
959                            if (vsize > 0) {
960                                    SetDlgItemInt(hDlg, IDC_BITRATE_VSIZE, vsize, TRUE);
961                                    /* convert from kbytes to kbits-per-second */
962                                    SetDlgItemInt(hDlg, IDC_BITRATE_VRATE, (vsize * 8 * 128) / (duration * 125), TRUE);
963                            }else{
964                                    SetDlgItemText(hDlg, IDC_BITRATE_VSIZE, "Overflow");
965                                    SetDlgItemText(hDlg, IDC_BITRATE_VRATE, "Overflow");
966                            }
967    
968                    }
969                    break;
970    
971            case IDD_ZONE :
972                    weight_en = IsDlgChecked(hDlg, IDC_ZONE_MODE_WEIGHT);
973                    quant_en =   IsDlgChecked(hDlg, IDC_ZONE_MODE_QUANT);
974                    EnableDlgWindow(hDlg, IDC_ZONE_WEIGHT, weight_en);
975                    EnableDlgWindow(hDlg, IDC_ZONE_QUANT, quant_en);
976                    EnableDlgWindow(hDlg, IDC_ZONE_SLIDER, weight_en|quant_en);
977    
978                    if (weight_en) {
979                            SendDlgItemMessage(hDlg, IDC_ZONE_SLIDER, TBM_SETRANGE, TRUE, MAKELONG(001,200));
980                            SendDlgItemMessage(hDlg, IDC_ZONE_SLIDER, TBM_SETPOS, TRUE, get_dlgitem_float(hDlg, IDC_ZONE_WEIGHT, 100));
981                            SetDlgItemText(hDlg, IDC_ZONE_MIN, "0.01");
982                            SetDlgItemText(hDlg, IDC_ZONE_MAX, "2.00");
983                    }else if (quant_en) {
984                            SendDlgItemMessage(hDlg, IDC_ZONE_SLIDER, TBM_SETRANGE, TRUE, MAKELONG(100,3100));
985                            SendDlgItemMessage(hDlg, IDC_ZONE_SLIDER, TBM_SETPOS, TRUE, get_dlgitem_float(hDlg, IDC_ZONE_QUANT, 100));
986                            SetDlgItemText(hDlg, IDC_ZONE_MIN, "1");
987                            SetDlgItemText(hDlg, IDC_ZONE_MAX, "31");
988                    }
989    
990                    bvops = (profiles[config->profile].flags&PROFILE_BVOP) && config->use_bvop;
991                    EnableDlgWindow(hDlg, IDC_ZONE_BVOPTHRESHOLD_S, bvops);
992                    EnableDlgWindow(hDlg, IDC_ZONE_BVOPTHRESHOLD, bvops);
993                    break;
994    
995            case IDD_DEBUG :
996                    cpu_force                       = IsDlgChecked(hDlg, IDC_CPU_FORCE);
997                    EnableDlgWindow(hDlg, IDC_CPU_MMX,              cpu_force);
998                    EnableDlgWindow(hDlg, IDC_CPU_MMXEXT,   cpu_force);
999                    EnableDlgWindow(hDlg, IDC_CPU_SSE,              cpu_force);
1000                    EnableDlgWindow(hDlg, IDC_CPU_SSE2,             cpu_force);
1001                    EnableDlgWindow(hDlg, IDC_CPU_3DNOW,    cpu_force);
1002                    EnableDlgWindow(hDlg, IDC_CPU_3DNOWEXT, cpu_force);
1003                    break;
1004            }
1005    }
1006    
1007    
1008    /* upload config data into dialog */
1009    static void adv_upload(HWND hDlg, int idd, CONFIG * config)
1010    {
1011            switch (idd)
1012            {
1013            case IDD_PROFILE :
1014                    SendDlgItemMessage(hDlg, IDC_PROFILE_PROFILE, CB_SETCURSEL, config->profile, 0);
1015    
1016                    SendDlgItemMessage(hDlg, IDC_QUANTTYPE, CB_SETCURSEL, config->quant_type, 0);
1017                    CheckDlg(hDlg, IDC_LUMMASK, config->lum_masking);
1018                    CheckDlg(hDlg, IDC_INTERLACING, config->interlacing);
1019                    CheckDlg(hDlg, IDC_QPEL, config->qpel);
1020                    CheckDlg(hDlg, IDC_GMC, config->gmc);
1021                    CheckDlg(hDlg, IDC_REDUCED, config->reduced_resolution);
1022                    CheckDlg(hDlg, IDC_BVOP, config->use_bvop);
1023    
1024                    SetDlgItemInt(hDlg, IDC_MAXBFRAMES, config->max_bframes, FALSE);
1025                    set_dlgitem_float(hDlg, IDC_BQUANTRATIO, config->bquant_ratio);
1026                    set_dlgitem_float(hDlg, IDC_BQUANTOFFSET, config->bquant_offset);
1027                    CheckDlg(hDlg, IDC_PACKED, config->packed);
1028                    CheckDlg(hDlg, IDC_CLOSEDGOV, config->closed_gov);
1029    
1030                    break;
1031            case IDD_AR:
1032                    CheckRadioButton(hDlg, IDC_AR, IDC_PAR, config->ar_mode == 0 ? IDC_PAR : IDC_AR);
1033                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_SETCURSEL, (config->display_aspect_ratio), 0);
1034                    SetDlgItemInt(hDlg, IDC_ARX, config->ar_x, FALSE);
1035                    SetDlgItemInt(hDlg, IDC_ARY, config->ar_y, FALSE);
1036                    break;
1037    
1038            case IDD_LEVEL :
1039                    SendDlgItemMessage(hDlg, IDC_LEVEL_PROFILE, CB_SETCURSEL, config->profile, 0);
1040                    break;
1041    
1042            case IDD_RC_CBR :
1043                    SetDlgItemInt(hDlg, IDC_CBR_REACTIONDELAY, config->rc_reaction_delay_factor, FALSE);
1044                    SetDlgItemInt(hDlg, IDC_CBR_AVERAGINGPERIOD, config->rc_averaging_period, FALSE);
1045                    SetDlgItemInt(hDlg, IDC_CBR_BUFFER, config->rc_buffer, FALSE);
1046                    break;
1047    
1048            case IDD_RC_2PASS1 :
1049                    SetDlgItemText(hDlg, IDC_STATS, config->stats);
1050                    CheckDlg(hDlg, IDC_DISCARD1PASS, config->discard1pass);
1051                    CheckDlg(hDlg, IDC_FULL1PASS, config->full1pass);
1052                    break;
1053    
1054            case IDD_RC_2PASS2 :
1055                    SetDlgItemText(hDlg, IDC_STATS, config->stats);
1056                    SetDlgItemInt(hDlg, IDC_KFBOOST, config->keyframe_boost, FALSE);
1057                    SetDlgItemInt(hDlg, IDC_KFREDUCTION, config->kfreduction, FALSE);
1058    
1059                    SetDlgItemInt(hDlg, IDC_OVERFLOW_CONTROL_STRENGTH, config->overflow_control_strength, FALSE);
1060                    SetDlgItemInt(hDlg, IDC_OVERIMP, config->twopass_max_overflow_improvement, FALSE);
1061                    SetDlgItemInt(hDlg, IDC_OVERDEG, config->twopass_max_overflow_degradation, FALSE);
1062    
1063                    SetDlgItemInt(hDlg, IDC_CURVECOMPH, config->curve_compression_high, FALSE);
1064                    SetDlgItemInt(hDlg, IDC_CURVECOMPL, config->curve_compression_low, FALSE);
1065                    SetDlgItemInt(hDlg, IDC_MINKEY, config->kfthreshold, FALSE);
1066                    break;
1067    
1068            case IDD_BITRATE :
1069                    SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_SETCURSEL, config->container_type, 0);
1070                    SetDlgItemInt(hDlg, IDC_BITRATE_TSIZE, config->target_size, FALSE);
1071                    SetDlgItemInt(hDlg, IDC_BITRATE_SSIZE, config->subtitle_size, FALSE);
1072    
1073                    SetDlgItemInt(hDlg, IDC_BITRATE_HOURS, config->hours, FALSE);
1074                    SetDlgItemInt(hDlg, IDC_BITRATE_MINUTES, config->minutes, FALSE);
1075                    SetDlgItemInt(hDlg, IDC_BITRATE_SECONDS, config->seconds, FALSE);
1076                    SendDlgItemMessage(hDlg, IDC_BITRATE_FPS, CB_SETCURSEL, config->fps, 0);
1077    
1078                    SendDlgItemMessage(hDlg, IDC_BITRATE_AFORMAT, CB_SETCURSEL, config->audio_type, 0);
1079                    CheckRadioButton(hDlg, IDC_BITRATE_AMODE_RATE, IDC_BITRATE_AMODE_SIZE, config->audio_mode == 0 ? IDC_BITRATE_AMODE_RATE : IDC_BITRATE_AMODE_SIZE);
1080                    SetDlgItemInt(hDlg, IDC_BITRATE_ARATE, config->audio_rate, FALSE);
1081                    SetDlgItemInt(hDlg, IDC_BITRATE_ASIZE, config->audio_size, FALSE);
1082                    break;
1083    
1084            case IDD_ZONE :
1085                    SetDlgItemInt(hDlg, IDC_ZONE_FRAME, config->zones[config->cur_zone].frame, FALSE);
1086    
1087                    CheckDlgButton(hDlg, IDC_ZONE_MODE_WEIGHT,   config->zones[config->cur_zone].mode == RC_ZONE_WEIGHT);
1088                    CheckDlgButton(hDlg, IDC_ZONE_MODE_QUANT,                config->zones[config->cur_zone].mode == RC_ZONE_QUANT);
1089    
1090                    set_dlgitem_float(hDlg, IDC_ZONE_WEIGHT, config->zones[config->cur_zone].weight);
1091                    set_dlgitem_float(hDlg, IDC_ZONE_QUANT, config->zones[config->cur_zone].quant);
1092    
1093                    CheckDlgButton(hDlg, IDC_ZONE_FORCEIVOP, config->zones[config->cur_zone].type==XVID_TYPE_IVOP);
1094                    CheckDlgButton(hDlg, IDC_ZONE_GREYSCALE, config->zones[config->cur_zone].greyscale);
1095                    CheckDlgButton(hDlg, IDC_ZONE_CHROMAOPT, config->zones[config->cur_zone].chroma_opt);
1096    
1097                    SetDlgItemInt(hDlg, IDC_ZONE_BVOPTHRESHOLD, config->zones[config->cur_zone].bvop_threshold, TRUE);
1098                    break;
1099    
1100            case IDD_MOTION :
1101                    SendDlgItemMessage(hDlg, IDC_MOTION, CB_SETCURSEL, config->motion_search, 0);
1102                    SendDlgItemMessage(hDlg, IDC_VHQ, CB_SETCURSEL, config->vhq_mode, 0);
1103                    CheckDlg(hDlg, IDC_CHROMAME, config->chromame);
1104                    CheckDlg(hDlg, IDC_CARTOON, config->cartoon_mode);
1105                    CheckDlg(hDlg, IDC_TURBO, config->turbo);
1106                    SetDlgItemInt(hDlg, IDC_FRAMEDROP, config->frame_drop_ratio, FALSE);
1107                    SetDlgItemInt(hDlg, IDC_MAXKEY, config->max_key_interval, FALSE);
1108                    break;
1109    
1110            case IDD_QUANT :
1111                    SetDlgItemInt(hDlg, IDC_MINIQUANT, config->min_iquant, FALSE);
1112                    SetDlgItemInt(hDlg, IDC_MAXIQUANT, config->max_iquant, FALSE);
1113                    SetDlgItemInt(hDlg, IDC_MINPQUANT, config->min_pquant, FALSE);
1114                    SetDlgItemInt(hDlg, IDC_MAXPQUANT, config->max_pquant, FALSE);
1115                    SetDlgItemInt(hDlg, IDC_MINBQUANT, config->min_bquant, FALSE);
1116                    SetDlgItemInt(hDlg, IDC_MAXBQUANT, config->max_bquant, FALSE);
1117                    CheckDlg(hDlg, IDC_TRELLISQUANT, config->trellis_quant);
1118                    break;
1119    
1120            case IDD_DEBUG :
1121                    CheckDlg(hDlg, IDC_CPU_MMX, (config->cpu & XVID_CPU_MMX));
1122                    CheckDlg(hDlg, IDC_CPU_MMXEXT, (config->cpu & XVID_CPU_MMXEXT));
1123                    CheckDlg(hDlg, IDC_CPU_SSE, (config->cpu & XVID_CPU_SSE));
1124                    CheckDlg(hDlg, IDC_CPU_SSE2, (config->cpu & XVID_CPU_SSE2));
1125                    CheckDlg(hDlg, IDC_CPU_3DNOW, (config->cpu & XVID_CPU_3DNOW));
1126                    CheckDlg(hDlg, IDC_CPU_3DNOWEXT, (config->cpu & XVID_CPU_3DNOWEXT));
1127    
1128                    CheckRadioButton(hDlg, IDC_CPU_AUTO, IDC_CPU_FORCE,
1129                            config->cpu & XVID_CPU_FORCE ? IDC_CPU_FORCE : IDC_CPU_AUTO );
1130    
1131                    SetDlgItemInt(hDlg, IDC_NUMTHREADS, config->num_threads, FALSE);
1132    
1133                    SendDlgItemMessage(hDlg, IDC_FOURCC, CB_SETCURSEL, config->fourcc_used, 0);
1134                    set_dlgitem_hex(hDlg, IDC_DEBUG, config->debug);
1135                    CheckDlg(hDlg, IDC_VOPDEBUG, config->vop_debug);
1136                    CheckDlg(hDlg, IDC_DISPLAY_STATUS, config->display_status);
1137                    break;
1138    
1139            case IDD_DEC :
1140                    CheckDlg(hDlg, IDC_DEC_DY,      pp_dy);
1141                    CheckDlg(hDlg, IDC_DEC_DUV,     pp_duv);
1142                    CheckDlg(hDlg, IDC_DEC_DR,      pp_dr);
1143                    CheckDlg(hDlg, IDC_DEC_FE,      pp_fe);
1144                    break;
1145            }
1146    }
1147    
1148    
1149    /* download config data from dialog */
1150    
1151    static void adv_download(HWND hDlg, int idd, CONFIG * config)
1152    {
1153            switch (idd)
1154            {
1155            case IDD_PROFILE :
1156                    config->profile = SendDlgItemMessage(hDlg, IDC_PROFILE_PROFILE, CB_GETCURSEL, 0, 0);
1157    
1158                    config->quant_type = SendDlgItemMessage(hDlg, IDC_QUANTTYPE, CB_GETCURSEL, 0, 0);
1159                    config->lum_masking = IsDlgChecked(hDlg, IDC_LUMMASK);
1160                    config->interlacing = IsDlgChecked(hDlg, IDC_INTERLACING);
1161                    config->qpel = IsDlgChecked(hDlg, IDC_QPEL);
1162                    config->gmc = IsDlgChecked(hDlg, IDC_GMC);
1163                    config->reduced_resolution = IsDlgChecked(hDlg, IDC_REDUCED);
1164    
1165                    config->use_bvop = IsDlgChecked(hDlg, IDC_BVOP);
1166                    config->max_bframes = config_get_uint(hDlg, IDC_MAXBFRAMES, config->max_bframes);
1167                    config->bquant_ratio = get_dlgitem_float(hDlg, IDC_BQUANTRATIO, config->bquant_ratio);
1168                    config->bquant_offset = get_dlgitem_float(hDlg, IDC_BQUANTOFFSET, config->bquant_offset);
1169                    config->packed = IsDlgChecked(hDlg, IDC_PACKED);
1170                    config->closed_gov = IsDlgChecked(hDlg, IDC_CLOSEDGOV);
1171                    break;
1172    
1173            case IDD_AR:
1174                    config->ar_mode = IsDlgChecked(hDlg, IDC_PAR) ? 0:1;
1175                    config->ar_x = config_get_uint(hDlg, IDC_ARX, config->ar_x);
1176                    config->ar_y = config_get_uint(hDlg, IDC_ARY, config->ar_y);
1177                    config->display_aspect_ratio = SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_GETCURSEL, 0, 0);
1178                    if (config->display_aspect_ratio == 5) {
1179                            config->par_x = config_get_uint(hDlg, IDC_PARX, config->par_x);
1180                            config->par_y = config_get_uint(hDlg, IDC_PARY, config->par_y);
1181                    }
1182                    break;
1183    
1184            case IDD_LEVEL :
1185                    config->profile = SendDlgItemMessage(hDlg, IDC_LEVEL_PROFILE, CB_GETCURSEL, 0, 0);
1186                    break;
1187    
1188            case IDD_RC_CBR :
1189                    config->rc_reaction_delay_factor = config_get_uint(hDlg, IDC_CBR_REACTIONDELAY, config->rc_reaction_delay_factor);
1190                    config->rc_averaging_period = config_get_uint(hDlg, IDC_CBR_AVERAGINGPERIOD, config->rc_averaging_period);
1191                    config->rc_buffer = config_get_uint(hDlg, IDC_CBR_BUFFER, config->rc_buffer);
1192                    break;
1193    
1194            case IDD_RC_2PASS1 :
1195                    if (GetDlgItemText(hDlg, IDC_STATS, config->stats, MAX_PATH) == 0)
1196                            lstrcpy(config->stats, CONFIG_2PASS_FILE);
1197                    config->discard1pass = IsDlgChecked(hDlg, IDC_DISCARD1PASS);
1198                    config->full1pass = IsDlgChecked(hDlg, IDC_FULL1PASS);
1199                    break;
1200    
1201            case IDD_RC_2PASS2 :
1202                    if (GetDlgItemText(hDlg, IDC_STATS, config->stats, MAX_PATH) == 0)
1203                            lstrcpy(config->stats, CONFIG_2PASS_FILE);
1204    
1205                    config->keyframe_boost = GetDlgItemInt(hDlg, IDC_KFBOOST, NULL, FALSE);
1206                    config->kfreduction = GetDlgItemInt(hDlg, IDC_KFREDUCTION, NULL, FALSE);
1207                    CONSTRAINVAL(config->keyframe_boost, 0, 1000);
1208    
1209                    config->overflow_control_strength = GetDlgItemInt(hDlg, IDC_OVERFLOW_CONTROL_STRENGTH, NULL, FALSE);
1210                    config->twopass_max_overflow_improvement = config_get_uint(hDlg, IDC_OVERIMP, config->twopass_max_overflow_improvement);
1211                    config->twopass_max_overflow_degradation = config_get_uint(hDlg, IDC_OVERDEG, config->twopass_max_overflow_degradation);
1212                    CONSTRAINVAL(config->twopass_max_overflow_improvement, 1, 80);
1213                    CONSTRAINVAL(config->twopass_max_overflow_degradation, 1, 80);
1214                    CONSTRAINVAL(config->overflow_control_strength, 0, 100);
1215    
1216                    config->curve_compression_high = GetDlgItemInt(hDlg, IDC_CURVECOMPH, NULL, FALSE);
1217                    config->curve_compression_low = GetDlgItemInt(hDlg, IDC_CURVECOMPL, NULL, FALSE);
1218                    CONSTRAINVAL(config->curve_compression_high, 0, 100);
1219                    CONSTRAINVAL(config->curve_compression_low, 0, 100);
1220    
1221                    config->kfthreshold = config_get_uint(hDlg, IDC_MINKEY, config->kfthreshold);
1222    
1223                    break;
1224    
1225            case IDD_BITRATE :
1226                    config->container_type = SendDlgItemMessage(hDlg, IDC_BITRATE_CFORMAT, CB_GETCURSEL, 0, 0);
1227                    config->target_size = config_get_uint(hDlg, IDC_BITRATE_TSIZE, config->target_size);
1228                    config->subtitle_size = config_get_uint(hDlg, IDC_BITRATE_SSIZE, config->subtitle_size);
1229    
1230                    config->hours = config_get_uint(hDlg, IDC_BITRATE_HOURS, config->hours);
1231                    config->minutes = config_get_uint(hDlg, IDC_BITRATE_MINUTES, config->minutes);
1232                    config->seconds = config_get_uint(hDlg, IDC_BITRATE_SECONDS, config->seconds);
1233                    config->fps = SendDlgItemMessage(hDlg, IDC_BITRATE_FPS, CB_GETCURSEL, 0, 0);
1234    
1235                    config->audio_type = SendDlgItemMessage(hDlg, IDC_BITRATE_AFORMAT, CB_GETCURSEL, 0, 0);
1236                    config->audio_mode = IsDlgChecked(hDlg, IDC_BITRATE_AMODE_SIZE) ? 1 : 0 ;
1237                    config->audio_rate = config_get_uint(hDlg, IDC_BITRATE_ARATE, config->audio_rate);
1238                    config->audio_size = config_get_uint(hDlg, IDC_BITRATE_ASIZE, config->audio_size);
1239    
1240                    /* the main window uses "AVI bitrate/filesize" not "video bitrate/filesize",
1241                       so we have to compensate by frames * 24 bytes */
1242                    {
1243                            int frame_compensate = 24 * (int)(
1244                                    (3600*config->hours +
1245                                       60*config->minutes +
1246                                          config->seconds) * video_fps_list[config->fps].value) / 1024;
1247    
1248                            int bitrate_compensate = (int)(24 * video_fps_list[config->fps].value) / 125;
1249    
1250                            config->desired_size =
1251                                                    config_get_uint(hDlg, IDC_BITRATE_VSIZE, config->desired_size) - frame_compensate;
1252    
1253                            config->bitrate =
1254                                                    config_get_uint(hDlg, IDC_BITRATE_VRATE, config->bitrate) - bitrate_compensate;
1255                    }
1256                    break;
1257    
1258            case IDD_ZONE :
1259                    config->zones[config->cur_zone].frame = config_get_uint(hDlg, IDC_ZONE_FRAME, config->zones[config->cur_zone].frame);
1260    
1261                    if (IsDlgChecked(hDlg, IDC_ZONE_MODE_WEIGHT)) {
1262                            config->zones[config->cur_zone].mode = RC_ZONE_WEIGHT;
1263                    }else if (IsDlgChecked(hDlg, IDC_ZONE_MODE_QUANT)) {
1264                            config->zones[config->cur_zone].mode = RC_ZONE_QUANT;
1265                    }
1266    
1267                    config->zones[config->cur_zone].weight = get_dlgitem_float(hDlg, IDC_ZONE_WEIGHT, config->zones[config->cur_zone].weight);
1268                    config->zones[config->cur_zone].quant =  get_dlgitem_float(hDlg, IDC_ZONE_QUANT, config->zones[config->cur_zone].quant);
1269    
1270                    config->zones[config->cur_zone].type = IsDlgButtonChecked(hDlg, IDC_ZONE_FORCEIVOP)?XVID_TYPE_IVOP:XVID_TYPE_AUTO;
1271                    config->zones[config->cur_zone].greyscale = IsDlgButtonChecked(hDlg, IDC_ZONE_GREYSCALE);
1272                    config->zones[config->cur_zone].chroma_opt = IsDlgButtonChecked(hDlg, IDC_ZONE_CHROMAOPT);
1273    
1274                    config->zones[config->cur_zone].bvop_threshold = config_get_int(hDlg, IDC_ZONE_BVOPTHRESHOLD, config->zones[config->cur_zone].bvop_threshold);
1275                    break;
1276    
1277            case IDD_MOTION :
1278                    config->motion_search = SendDlgItemMessage(hDlg, IDC_MOTION, CB_GETCURSEL, 0, 0);
1279                    config->vhq_mode = SendDlgItemMessage(hDlg, IDC_VHQ, CB_GETCURSEL, 0, 0);
1280                    config->chromame = IsDlgChecked(hDlg, IDC_CHROMAME);
1281                    config->cartoon_mode = IsDlgChecked(hDlg, IDC_CARTOON);
1282                    config->turbo = IsDlgChecked(hDlg, IDC_TURBO);
1283    
1284                    config->frame_drop_ratio = config_get_uint(hDlg, IDC_FRAMEDROP, config->frame_drop_ratio);
1285    
1286                    config->max_key_interval = config_get_uint(hDlg, IDC_MAXKEY, config->max_key_interval);
1287                    break;
1288    
1289            case IDD_QUANT :
1290                    config->min_iquant = config_get_uint(hDlg, IDC_MINIQUANT, config->min_iquant);
1291                    config->max_iquant = config_get_uint(hDlg, IDC_MAXIQUANT, config->max_iquant);
1292                    config->min_pquant = config_get_uint(hDlg, IDC_MINPQUANT, config->min_pquant);
1293                    config->max_pquant = config_get_uint(hDlg, IDC_MAXPQUANT, config->max_pquant);
1294                    config->min_bquant = config_get_uint(hDlg, IDC_MINBQUANT, config->min_bquant);
1295                    config->max_bquant = config_get_uint(hDlg, IDC_MAXBQUANT, config->max_bquant);
1296    
1297                    CONSTRAINVAL(config->min_iquant, 1, 31);
1298                    CONSTRAINVAL(config->max_iquant, config->min_iquant, 31);
1299                    CONSTRAINVAL(config->min_pquant, 1, 31);
1300                    CONSTRAINVAL(config->max_pquant, config->min_pquant, 31);
1301                    CONSTRAINVAL(config->min_bquant, 1, 31);
1302                    CONSTRAINVAL(config->max_bquant, config->min_bquant, 31);
1303    
1304                    config->trellis_quant = IsDlgChecked(hDlg, IDC_TRELLISQUANT);
1305                    break;
1306    
1307            case IDD_DEBUG :
1308                    config->cpu = 0;
1309                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_MMX)    ? XVID_CPU_MMX : 0;
1310                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_MMXEXT)   ? XVID_CPU_MMXEXT : 0;
1311                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_SSE)    ? XVID_CPU_SSE : 0;
1312                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_SSE2)  ? XVID_CPU_SSE2 : 0;
1313                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_3DNOW)        ? XVID_CPU_3DNOW : 0;
1314                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_3DNOWEXT) ? XVID_CPU_3DNOWEXT : 0;
1315                    config->cpu |= IsDlgChecked(hDlg, IDC_CPU_FORCE)        ? XVID_CPU_FORCE : 0;
1316    
1317                    config->num_threads = config_get_uint(hDlg, IDC_NUMTHREADS, config->num_threads);
1318    
1319                    config->fourcc_used = SendDlgItemMessage(hDlg, IDC_FOURCC, CB_GETCURSEL, 0, 0);
1320                    config->debug = get_dlgitem_hex(hDlg, IDC_DEBUG, config->debug);
1321                    config->vop_debug = IsDlgChecked(hDlg, IDC_VOPDEBUG);
1322                    config->display_status = IsDlgChecked(hDlg, IDC_DISPLAY_STATUS);
1323                    break;
1324    
1325            case IDD_DEC :
1326                    pp_dy = IsDlgChecked(hDlg, IDC_DEC_DY);
1327                    pp_duv = IsDlgChecked(hDlg, IDC_DEC_DUV);
1328                    pp_dr = IsDlgChecked(hDlg, IDC_DEC_DR);
1329                    pp_fe = IsDlgChecked(hDlg, IDC_DEC_FE);
1330                    break;
1331            }
1332    }
1333    
1334    
1335    
1336    /* advanced dialog proc */
1337    
1338    static BOOL CALLBACK adv_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1339    {
1340            PROPSHEETINFO *psi;
1341    
1342            psi = (PROPSHEETINFO*)GetWindowLong(hDlg, GWL_USERDATA);
1343    
1344            switch (uMsg)
1345            {
1346            case WM_INITDIALOG :
1347                    psi = (PROPSHEETINFO*) ((LPPROPSHEETPAGE)lParam)->lParam;
1348                    SetWindowLong(hDlg, GWL_USERDATA, (LPARAM)psi);
1349    
1350                    if (g_hTooltip)
1351                            EnumChildWindows(hDlg, enum_tooltips, 0);
1352    
1353                    adv_init(hDlg, psi->idd, psi->config);
1354                    break;
1355    
1356            case WM_COMMAND :
1357                    if (HIWORD(wParam) == BN_CLICKED)
1358                    {
1359                            switch (LOWORD(wParam))
1360                            {
1361                            case IDC_BVOP :
1362                            case IDC_ZONE_MODE_WEIGHT :
1363                            case IDC_ZONE_MODE_QUANT :
1364                            case IDC_ZONE_BVOPTHRESHOLD_ENABLE :
1365                            case IDC_CPU_AUTO :
1366                            case IDC_CPU_FORCE :
1367                            case IDC_AR :
1368                            case IDC_PAR :
1369                            case IDC_BITRATE_AMODE_RATE :
1370                            case IDC_BITRATE_AMODE_SIZE :
1371                                    adv_mode(hDlg, psi->idd, psi->config);
1372                                    break;
1373    
1374                            case IDC_BITRATE_SSELECT :
1375                            case IDC_BITRATE_ASELECT :
1376                                    {
1377                                    OPENFILENAME ofn;
1378                                    char filename[MAX_PATH] = "";
1379    
1380                                    memset(&ofn, 0, sizeof(OPENFILENAME));
1381                                    ofn.lStructSize = sizeof(OPENFILENAME);
1382    
1383                                    ofn.hwndOwner = hDlg;
1384                                    if (LOWORD(wParam)==IDC_BITRATE_SSELECT) {
1385                                            ofn.lpstrFilter = "Subtitle files (*.sub, *.ssa)\0*.sub;*.ssa\0All files (*.*)\0*.*\0\0";
1386                                    }else{
1387                                            ofn.lpstrFilter = "Audio files (*.mp3, *.ac3)\0*.mp3; *.ac3\0All files (*.*)\0*.*\0\0";
1388                                    }
1389    
1390                                    ofn.lpstrFile = filename;
1391                                    ofn.nMaxFile = MAX_PATH;
1392                                    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
1393    
1394                                    if (GetOpenFileName(&ofn)) {
1395                                            HANDLE hFile;
1396                                            DWORD filesize;
1397    
1398                                            if ((hFile = CreateFile(filename, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE ||
1399                                                    (filesize = GetFileSize(hFile, NULL)) == INVALID_FILE_SIZE) {
1400                                                    MessageBox(hDlg, "Could not get file size", "Error", 0);
1401                                            }else{
1402                                                    SetDlgItemInt(hDlg,
1403                                                                    LOWORD(wParam)==IDC_BITRATE_SSELECT? IDC_BITRATE_SSIZE : IDC_BITRATE_ASIZE,
1404                                                                    filesize / 1024, FALSE);
1405                                                    CloseHandle(hFile);
1406                                            }
1407                                    }
1408                                    }
1409                                    break;
1410    
1411                            case IDC_QUANTMATRIX :
1412                                    DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_QUANTMATRIX), hDlg, quantmatrix_proc, (LPARAM)psi->config);
1413                                    break;
1414    
1415                            case IDC_STATS_BROWSE :
1416                            {
1417                                    OPENFILENAME ofn;
1418                                    char tmp[MAX_PATH];
1419    
1420                                    GetDlgItemText(hDlg, IDC_STATS, tmp, MAX_PATH);
1421    
1422                                    memset(&ofn, 0, sizeof(OPENFILENAME));
1423                                    ofn.lStructSize = sizeof(OPENFILENAME);
1424    
1425                                    ofn.hwndOwner = hDlg;
1426                                    ofn.lpstrFilter = "bitrate curve (*.pass)\0*.pass\0All files (*.*)\0*.*\0\0";
1427                                    ofn.lpstrFile = tmp;
1428                                    ofn.nMaxFile = MAX_PATH;
1429                                    ofn.Flags = OFN_PATHMUSTEXIST;
1430    
1431                                    if (psi->idd == IDD_RC_2PASS1) {
1432                                            ofn.Flags |= OFN_OVERWRITEPROMPT;
1433                                    }else{
1434                                            ofn.Flags |= OFN_FILEMUSTEXIST;
1435                                    }
1436    
1437                                    if ((psi->idd==IDD_RC_2PASS1 && GetSaveFileName(&ofn)) ||
1438                                            (psi->idd==IDD_RC_2PASS2 && GetOpenFileName(&ofn))) {
1439                                            SetDlgItemText(hDlg, IDC_STATS, tmp);
1440                                    }
1441                                    }
1442                                    break;
1443    
1444                            case IDC_ZONE_FETCH :
1445                                    SetDlgItemInt(hDlg, IDC_ZONE_FRAME, psi->config->ci.ciActiveFrame, FALSE);
1446                                    break;
1447    
1448                            case IDC_AR_DEFAULT:
1449                                    CheckRadioButton(hDlg, IDC_AR, IDC_PAR, IDC_PAR);
1450                                    SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_SETCURSEL, 0, 0);
1451                                    adv_mode(hDlg, psi->idd, psi->config);
1452                                    break;
1453                            case IDC_AR_4_3:
1454                                    SetDlgItemInt(hDlg, IDC_ARX, 4, FALSE);
1455                                    SetDlgItemInt(hDlg, IDC_ARY, 3, FALSE);
1456                                    CheckRadioButton(hDlg, IDC_AR, IDC_PAR, IDC_AR);
1457                                    adv_mode(hDlg, psi->idd, psi->config);
1458                                    break;
1459                            case IDC_AR_16_9:
1460                                    SetDlgItemInt(hDlg, IDC_ARX, 16, FALSE);
1461                                    SetDlgItemInt(hDlg, IDC_ARY, 9, FALSE);
1462                                    CheckRadioButton(hDlg, IDC_AR, IDC_PAR, IDC_AR);
1463                                    adv_mode(hDlg, psi->idd, psi->config);
1464                                    break;
1465                            case IDC_AR_235_100:
1466                                    SetDlgItemInt(hDlg, IDC_ARX, 235, FALSE);
1467                                    SetDlgItemInt(hDlg, IDC_ARY, 100, FALSE);
1468                                    CheckRadioButton(hDlg, IDC_AR, IDC_PAR, IDC_AR);
1469                                    adv_mode(hDlg, psi->idd, psi->config);
1470                                    break;
1471    
1472                            default :
1473                                    return TRUE;
1474                            }
1475                    }else if ((HIWORD(wParam) == CBN_EDITCHANGE || HIWORD(wParam)==CBN_SELCHANGE) &&
1476                            (LOWORD(wParam)==IDC_BITRATE_TSIZE ||
1477                             LOWORD(wParam)==IDC_BITRATE_ARATE )) {
1478                            adv_mode(hDlg, psi->idd, psi->config);
1479                    }else if (HIWORD(wParam) == LBN_SELCHANGE &&
1480                            (LOWORD(wParam) == IDC_PROFILE_PROFILE ||
1481                             LOWORD(wParam) == IDC_LEVEL_PROFILE ||
1482                             LOWORD(wParam) == IDC_QUANTTYPE ||
1483                             LOWORD(wParam) == IDC_ASPECT_RATIO ||
1484                             LOWORD(wParam) == IDC_BITRATE_CFORMAT ||
1485                             LOWORD(wParam) == IDC_BITRATE_AFORMAT ||
1486                             LOWORD(wParam) == IDC_BITRATE_FPS)) {
1487                            adv_mode(hDlg, psi->idd, psi->config);
1488                    }else if (HIWORD(wParam) == EN_UPDATE && (LOWORD(wParam)==IDC_ZONE_WEIGHT || LOWORD(wParam)==IDC_ZONE_QUANT)) {
1489    
1490                            SendDlgItemMessage(hDlg, IDC_ZONE_SLIDER, TBM_SETPOS, TRUE,
1491                                            get_dlgitem_float(hDlg, LOWORD(wParam), 100));
1492    
1493                    } else if (HIWORD(wParam) == EN_UPDATE && (LOWORD(wParam)==IDC_PARX || LOWORD(wParam)==IDC_PARY)) {
1494    
1495                            if (5 == SendDlgItemMessage(hDlg, IDC_ASPECT_RATIO, CB_GETCURSEL, 0, 0)) {
1496                                    if(LOWORD(wParam)==IDC_PARX)
1497                                            psi->config->par_x = config_get_uint(hDlg, LOWORD(wParam), psi->config->par_x);
1498                                    else
1499                                            psi->config->par_y = config_get_uint(hDlg, LOWORD(wParam), psi->config->par_y);
1500                            }
1501                    } else if (HIWORD(wParam) == EN_UPDATE &&
1502                            (LOWORD(wParam)==IDC_BITRATE_SSIZE ||
1503                             LOWORD(wParam)==IDC_BITRATE_HOURS ||
1504                             LOWORD(wParam)==IDC_BITRATE_MINUTES ||
1505                             LOWORD(wParam)==IDC_BITRATE_SECONDS ||
1506                             LOWORD(wParam)==IDC_BITRATE_ASIZE)) {
1507                            adv_mode(hDlg, psi->idd, psi->config);
1508                    } else
1509                            return 0;
1510                    break;
1511    
1512            case WM_HSCROLL :
1513                    if((HWND)lParam == GetDlgItem(hDlg, IDC_ZONE_SLIDER)) {
1514                            int idc = IsDlgChecked(hDlg, IDC_ZONE_MODE_WEIGHT) ? IDC_ZONE_WEIGHT : IDC_ZONE_QUANT;
1515                            set_dlgitem_float(hDlg, idc, SendMessage((HWND)lParam, TBM_GETPOS, 0, 0) );
1516                            break;
1517                    }
1518                    return 0;
1519    
1520    
1521            case WM_NOTIFY :
1522                    switch (((NMHDR *)lParam)->code)
1523                    {
1524                    case PSN_SETACTIVE :
1525                            OutputDebugString("PSN_SET");
1526                            adv_upload(hDlg, psi->idd, psi->config);
1527                            adv_mode(hDlg, psi->idd, psi->config);
1528                            SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
1529                            break;
1530    
1531                    case PSN_KILLACTIVE :
1532                            OutputDebugString("PSN_KILL");
1533                            adv_download(hDlg, psi->idd, psi->config);
1534                            SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
1535                            break;
1536    
1537                    case PSN_APPLY :
1538                            OutputDebugString("PSN_APPLY");
1539                            psi->config->save = TRUE;
1540                            SetWindowLong(hDlg, DWL_MSGRESULT, FALSE);
1541                            break;
1542                    }
1543                    break;
1544    
1545            default :
1546                    return 0;
1547            }
1548    
1549            return 1;
1550    }
1551    
1552    
1553    
1554    
1555    /* load advanced options property sheet
1556      returns true, if the user accepted the changes
1557      or fasle if changes were canceled.
1558    
1559      */
1560    static BOOL adv_dialog(HWND hParent, CONFIG * config, const int * dlgs, int size)
1561    {
1562            PROPSHEETINFO psi[6];
1563            PROPSHEETPAGE psp[6];
1564            PROPSHEETHEADER psh;
1565            CONFIG temp;
1566            int i;
1567    
1568            config->save = FALSE;
1569            memcpy(&temp, config, sizeof(CONFIG));
1570    
1571            for (i=0; i<size; i++)
1572            {
1573                    psp[i].dwSize = sizeof(PROPSHEETPAGE);
1574                    psp[i].dwFlags = 0;
1575                    psp[i].hInstance = g_hInst;
1576                    psp[i].pfnDlgProc = adv_proc;
1577                    psp[i].lParam = (LPARAM)&psi[i];
1578                    psp[i].pfnCallback = NULL;
1579                    psp[i].pszTemplate = MAKEINTRESOURCE(dlgs[i]);
1580    
1581                    psi[i].idd = dlgs[i];
1582                    psi[i].config = &temp;
1583            }
1584    
1585            psh.dwSize = sizeof(PROPSHEETHEADER);
1586            psh.dwFlags = PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW | PSH_NOCONTEXTHELP;
1587            psh.hwndParent = hParent;
1588            psh.hInstance = g_hInst;
1589            psh.pszCaption = (LPSTR) "XviD Configuration";
1590            psh.nPages = size;
1591            psh.nStartPage = 0;
1592            psh.ppsp = (LPCPROPSHEETPAGE)&psp;
1593            psh.pfnCallback = NULL;
1594            PropertySheet(&psh);
1595    
1596            if (temp.save)
1597                    memcpy(config, &temp, sizeof(CONFIG));
1598    
1599            return temp.save;
1600    }
1601    
1602    /* ===================================================================================== */
1603    /* MAIN DIALOG ========================================================================= */
1604    /* ===================================================================================== */
1605    
1606    
1607    static void main_insert_zone(HWND hDlg, zone_t * s, int i, BOOL insert)
1608    {
1609            char tmp[32];
1610    
1611            wsprintf(tmp,"%i",s->frame);
1612    
1613            if (insert) {
1614                    LVITEM lvi;
1615    
1616                    lvi.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
1617                    lvi.state = 0;
1618                    lvi.stateMask = 0;
1619                    lvi.iImage = 0;
1620                    lvi.pszText = tmp;
1621                    lvi.cchTextMax = strlen(tmp);
1622                    lvi.iItem = i;
1623                    lvi.iSubItem = 0;
1624                    ListView_InsertItem(hDlg, &lvi);
1625            }else{
1626                    ListView_SetItemText(hDlg, i, 0, tmp);
1627            }
1628    
1629            if (s->mode == RC_ZONE_WEIGHT) {
1630                    sprintf(tmp,"W %.2f",(float)s->weight/100);
1631            }else if (s->mode == RC_ZONE_QUANT) {
1632                    sprintf(tmp,"Q %.2f",(float)s->quant/100);
1633            }else {
1634                    strcpy(tmp,"EXT");
1635            }
1636            ListView_SetItemText(hDlg, i, 1, tmp);
1637    
1638            tmp[0] = '\0';
1639            if (s->type==XVID_TYPE_IVOP)
1640                    strcat(tmp, "K ");
1641    
1642            if (s->greyscale)
1643                    strcat(tmp, "G ");
1644    
1645            if (s->chroma_opt)
1646                    strcat(tmp, "C ");
1647    
1648            ListView_SetItemText(hDlg, i, 2, tmp);
1649    }
1650    
1651    
1652    static void main_mode(HWND hDlg, CONFIG * config)
1653    {
1654            const int profile = SendDlgItemMessage(hDlg, IDC_PROFILE, CB_GETCURSEL, 0, 0);
1655            const int rc_mode = SendDlgItemMessage(hDlg, IDC_MODE, CB_GETCURSEL, 0, 0);
1656            /* enable target rate/size control only for 1pass and 2pass  modes*/
1657            const int target_en = rc_mode==RC_MODE_1PASS || rc_mode==RC_MODE_2PASS2;
1658            const int target_en_slider = rc_mode==RC_MODE_1PASS ||
1659                    (rc_mode==RC_MODE_2PASS2 && config->use_2pass_bitrate);
1660    
1661            char buf[16];
1662            int max;
1663    
1664            g_use_bitrate = config->use_2pass_bitrate;
1665    
1666            if (g_use_bitrate) {
1667                    SetDlgItemText(hDlg, IDC_BITRATE_S, "Target bitrate (kbps):");
1668    
1669                    wsprintf(buf, "%i kbps", DEFAULT_MIN_KBPS);
1670                    SetDlgItemText(hDlg, IDC_BITRATE_MIN, buf);
1671    
1672                    max = profiles[profile].max_bitrate;
1673                    if (max == 0) max = DEFAULT_MAX_KBPS;
1674                    wsprintf(buf, "%i kbps", max);
1675                    SetDlgItemText(hDlg, IDC_BITRATE_MAX, buf);
1676    
1677                    SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETRANGE, TRUE, MAKELONG(DEFAULT_MIN_KBPS, max));
1678                    SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETPOS, TRUE,
1679                                                    config_get_uint(hDlg, IDC_BITRATE, DEFAULT_MIN_KBPS) );
1680    
1681            } else if (rc_mode==RC_MODE_2PASS2) {
1682                    SetDlgItemText(hDlg, IDC_BITRATE_S, "Target size (kbytes):");
1683            } else if (rc_mode==RC_MODE_1PASS) {
1684                    SetDlgItemText(hDlg, IDC_BITRATE_S, "Target quantizer:");
1685                    SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETRANGE, TRUE, MAKELONG(100, 3100));
1686                    SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETPOS, TRUE,
1687                                                            get_dlgitem_float(hDlg, IDC_BITRATE, DEFAULT_QUANT ));
1688                    SetDlgItemText(hDlg, IDC_BITRATE_MIN, "1 (maximum quality)");
1689                    SetDlgItemText(hDlg, IDC_BITRATE_MAX, "(smallest file) 31");
1690    
1691            }
1692    
1693            EnableDlgWindow(hDlg, IDC_BITRATE_S, target_en);
1694            EnableDlgWindow(hDlg, IDC_BITRATE, target_en);
1695            EnableDlgWindow(hDlg, IDC_BITRATE_ADV, target_en);
1696    
1697            EnableDlgWindow(hDlg, IDC_BITRATE_MIN, target_en_slider);
1698            EnableDlgWindow(hDlg, IDC_BITRATE_MAX, target_en_slider);
1699            EnableDlgWindow(hDlg, IDC_SLIDER, target_en_slider);
1700    }
1701    
1702    
1703    static void main_upload(HWND hDlg, CONFIG * config)
1704    {
1705    
1706            SendDlgItemMessage(hDlg, IDC_PROFILE, CB_SETCURSEL, config->profile, 0);
1707            SendDlgItemMessage(hDlg, IDC_MODE, CB_SETCURSEL, config->mode, 0);
1708    
1709            g_use_bitrate = config->use_2pass_bitrate;
1710    
1711            if (g_use_bitrate) {
1712                    SetDlgItemInt(hDlg, IDC_BITRATE, config->bitrate, FALSE);
1713            } else if (config->mode == RC_MODE_2PASS2) {
1714                    SetDlgItemInt(hDlg, IDC_BITRATE, config->desired_size, FALSE);
1715            } else if (config->mode == RC_MODE_1PASS) {
1716                    set_dlgitem_float(hDlg, IDC_BITRATE, config->desired_quant);
1717            }
1718    
1719            zones_update(hDlg, config);
1720    }
1721    
1722    
1723    /* downloads data from main dialog */
1724    static void main_download(HWND hDlg, CONFIG * config)
1725    {
1726            config->profile = SendDlgItemMessage(hDlg, IDC_PROFILE, CB_GETCURSEL, 0, 0);
1727            config->mode = SendDlgItemMessage(hDlg, IDC_MODE, CB_GETCURSEL, 0, 0);
1728    
1729            if (g_use_bitrate) {
1730                    config->bitrate = config_get_uint(hDlg, IDC_BITRATE, config->bitrate);
1731            } else if (config->mode == RC_MODE_2PASS2) {
1732                    config->desired_size = config_get_uint(hDlg, IDC_BITRATE, config->desired_size);
1733            } else if (config->mode == RC_MODE_1PASS) {
1734                    config->desired_quant = get_dlgitem_float(hDlg, IDC_BITRATE, config->desired_quant);
1735            }
1736    }
1737    
1738    
1739    /* main dialog proc */
1740    
1741    static const int profile_dlgs[] = { IDD_PROFILE, IDD_LEVEL, IDD_AR };
1742    static const int single_dlgs[] = { IDD_RC_CBR };
1743    static const int pass1_dlgs[] = { IDD_RC_2PASS1 };
1744    static const int pass2_dlgs[] = { IDD_RC_2PASS2 };
1745    static const int bitrate_dlgs[] = { IDD_BITRATE };
1746    static const int zone_dlgs[] = { IDD_ZONE };
1747    static const int decoder_dlgs[] = { IDD_DEC };
1748    static const int adv_dlgs[] = { IDD_MOTION, IDD_QUANT, IDD_DEBUG};
1749    
1750    
1751    BOOL CALLBACK main_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1752    {
1753            CONFIG* config = (CONFIG*)GetWindowLong(hDlg, GWL_USERDATA);
1754            unsigned int i;
1755    
1756            switch (uMsg)
1757            {
1758            case WM_INITDIALOG :
1759                    SetWindowLong(hDlg, GWL_USERDATA, lParam);
1760                    config = (CONFIG*)lParam;
1761    
1762                    for (i=0; i<sizeof(profiles)/sizeof(profile_t); i++)
1763                            SendDlgItemMessage(hDlg, IDC_PROFILE, CB_ADDSTRING, 0, (LPARAM)profiles[i].name);
1764    
1765                    SendDlgItemMessage(hDlg, IDC_MODE, CB_ADDSTRING, 0, (LPARAM)"Single pass");
1766                    SendDlgItemMessage(hDlg, IDC_MODE, CB_ADDSTRING, 0, (LPARAM)"Twopass - 1st pass");
1767                    SendDlgItemMessage(hDlg, IDC_MODE, CB_ADDSTRING, 0, (LPARAM)"Twopass - 2nd pass");
1768    #ifdef _DEBUG
1769                    SendDlgItemMessage(hDlg, IDC_MODE, CB_ADDSTRING, 0, (LPARAM)"Null test speed");
1770    #endif
1771    
1772                    InitCommonControls();
1773    
1774                    if ((g_hTooltip = CreateWindow(TOOLTIPS_CLASS, NULL, TTS_ALWAYSTIP,
1775                                    CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1776                                    NULL, NULL, g_hInst, NULL)))
1777                    {
1778                            SetWindowPos(g_hTooltip, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
1779                            SendMessage(g_hTooltip, TTM_SETDELAYTIME, TTDT_AUTOMATIC, MAKELONG(1500, 0));
1780                            SendMessage(g_hTooltip, TTM_SETMAXTIPWIDTH, 0, 400);
1781    
1782                            EnumChildWindows(hDlg, enum_tooltips, 0);
1783                    }
1784    
1785                    SetClassLong(GetDlgItem(hDlg, IDC_BITRATE_S), GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_HAND));
1786    
1787                    {
1788                            DWORD ext_style = ListView_GetExtendedListViewStyle(GetDlgItem(hDlg,IDC_ZONES));
1789                            ext_style |= LVS_EX_FULLROWSELECT | LVS_EX_FLATSB ;
1790                            ListView_SetExtendedListViewStyle(GetDlgItem(hDlg,IDC_ZONES), ext_style);
1791                    }
1792    
1793                    {
1794                            typedef struct {
1795                                    char * name;
1796                                    int value;
1797                            } char_int_t;
1798    
1799                            const static char_int_t columns[] = {
1800                                    {"Frame #",      64},
1801                                    {"Weight/Quant",  82},
1802                                    {"Modifiers",   120}};
1803    
1804                            LVCOLUMN lvc;
1805                            int i;
1806    
1807                            /* Initialize the LVCOLUMN structure.  */
1808                            lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
1809                            lvc.fmt = LVCFMT_LEFT;
1810    
1811                            /* Add the columns.  */
1812                            for (i=0; i<sizeof(columns)/sizeof(char_int_t); i++) {
1813                                    lvc.pszText = (char*)columns[i].name;
1814                                    lvc.cchTextMax = strlen(columns[i].name);
1815                                    lvc.iSubItem = i;
1816                                    lvc.cx = columns[i].value;  /* column width, pixels */
1817                                    ListView_InsertColumn(GetDlgItem(hDlg,IDC_ZONES), i, &lvc);
1818                            }
1819                    }
1820    
1821                    /* XXX: main_mode needs RC_MODE_xxx, main_upload needs g_use_bitrate set correctly... */
1822                    main_upload(hDlg, config);
1823                    main_mode(hDlg, config);
1824                    main_upload(hDlg, config);
1825                    break;
1826    
1827            case WM_NOTIFY :
1828                    {
1829                            NMHDR * n = (NMHDR*)lParam;
1830    
1831                            if (n->code == NM_DBLCLK) {
1832                                     NMLISTVIEW * nmlv = (NMLISTVIEW*) lParam;
1833                                     config->cur_zone = nmlv->iItem;
1834    
1835                                     main_download(hDlg, config);
1836                                     if (config->cur_zone >= 0 && adv_dialog(hDlg, config, zone_dlgs, sizeof(zone_dlgs)/sizeof(int))) {
1837                                             zones_update(hDlg, config);
1838                                     }
1839                                     break;
1840                            }
1841    
1842                            if (n->code == NM_RCLICK) {
1843                                    OutputDebugString("Right click");
1844                            }
1845                    break;
1846                    }
1847    
1848            case WM_COMMAND :
1849                    if (HIWORD(wParam) == BN_CLICKED) {
1850    
1851                            switch(LOWORD(wParam)) {
1852                            case IDC_PROFILE_ADV :
1853                                    main_download(hDlg, config);
1854                                    adv_dialog(hDlg, config, profile_dlgs, sizeof(profile_dlgs)/sizeof(int));
1855    
1856                                    SendDlgItemMessage(hDlg, IDC_PROFILE, CB_SETCURSEL, config->profile, 0);
1857                                    main_mode(hDlg, config);
1858                                    break;
1859    
1860                            case IDC_MODE_ADV :
1861                                    main_download(hDlg, config);
1862                                    if (config->mode == RC_MODE_1PASS) {
1863                                            adv_dialog(hDlg, config, single_dlgs, sizeof(single_dlgs)/sizeof(int));
1864                                    }else if (config->mode == RC_MODE_2PASS1) {
1865                                            adv_dialog(hDlg, config, pass1_dlgs, sizeof(pass1_dlgs)/sizeof(int));
1866                                    }else if (config->mode == RC_MODE_2PASS2) {
1867                                            adv_dialog(hDlg, config, pass2_dlgs, sizeof(pass2_dlgs)/sizeof(int));
1868                                    }
1869                                    break;
1870    
1871                            case IDC_BITRATE_S :
1872                                    /* alternate between bitrate/desired_length metrics */
1873                                    main_download(hDlg, config);
1874                                    config->use_2pass_bitrate = !config->use_2pass_bitrate;
1875                                    main_mode(hDlg, config);
1876                                    main_upload(hDlg, config);
1877                                    break;
1878    
1879                            case IDC_BITRATE_ADV :
1880                                    main_download(hDlg, config);
1881                                    adv_dialog(hDlg, config, bitrate_dlgs, sizeof(bitrate_dlgs)/sizeof(int));
1882                                    main_mode(hDlg, config);
1883                                    main_upload(hDlg, config);
1884                                    break;
1885    
1886                            case IDC_DECODER :
1887                                    main_download(hDlg, config);
1888                                    adv_dialog(hDlg, config, decoder_dlgs, sizeof(decoder_dlgs)/sizeof(int));
1889                                    main_mode(hDlg, config);
1890                                    break;
1891    
1892                            case IDC_ADD :
1893                            {
1894                                    int i, sel, new_frame;
1895    
1896                                    if (config->num_zones >= MAX_ZONES) {
1897                                            MessageBox(hDlg, "Exceeded maximum number of zones.\nIncrease config.h:MAX_ZONES and rebuild.", "Warning", 0);
1898                                            break;
1899                                    }
1900    
1901                                    sel = ListView_GetNextItem(GetDlgItem(hDlg, IDC_ZONES), -1, LVNI_SELECTED);
1902    
1903                                    if (sel<0) {
1904                                            if (config->ci_valid && config->ci.ciActiveFrame>0) {
1905                                                    for(sel=0; sel<config->num_zones-1 && config->zones[sel].frame<config->ci.ciActiveFrame; sel++) ;
1906                                                    sel--;
1907                                                    new_frame = config->ci.ciActiveFrame;
1908                                            }else{
1909                                                    sel = config->num_zones-1;
1910                                                    new_frame = sel<0 ? 0 : config->zones[sel].frame + 1;
1911                                            }
1912                                    }else{
1913                                            new_frame = config->zones[sel].frame + 1;
1914                                    }
1915    
1916                                    for(i=config->num_zones-1; i>sel; i--) {
1917                                            config->zones[i+1] = config->zones[i];
1918                                    }
1919                                    config->num_zones++;
1920                                    config->zones[sel+1].frame = new_frame;
1921                                    config->zones[sel+1].mode = RC_ZONE_WEIGHT;
1922                                    config->zones[sel+1].weight = 100;
1923                                    config->zones[sel+1].quant = 500;
1924                                    config->zones[sel+1].type = XVID_TYPE_AUTO;
1925                                    config->zones[sel+1].greyscale = 0;
1926                                    config->zones[sel+1].chroma_opt = 0;
1927                                    config->zones[sel+1].bvop_threshold = 0;
1928    
1929                                    ListView_SetItemState(GetDlgItem(hDlg, IDC_ZONES), sel, 0x00000000, LVIS_SELECTED);
1930                                    zones_update(hDlg, config);
1931                                    ListView_SetItemState(GetDlgItem(hDlg, IDC_ZONES), sel+1, 0xffffffff, LVIS_SELECTED);
1932                                    break;
1933                            }
1934    
1935                            case IDC_REMOVE :
1936                            {
1937                                    int i, sel;
1938                                    sel = ListView_GetNextItem(GetDlgItem(hDlg, IDC_ZONES), -1, LVNI_SELECTED);
1939    
1940                                    if (sel == -1 || config->num_zones < 1) {
1941                                            /*MessageBox(hDlg, "Nothing selected", "Warning", 0);*/
1942                                            break;
1943                                    }
1944    
1945                                    for (i=sel; i<config->num_zones-1; i++)
1946                                            config->zones[i] = config->zones[i+1];
1947    
1948                                    config->num_zones--;
1949    
1950                                    zones_update(hDlg, config);
1951                                    break;
1952                            }
1953    
1954                            case IDC_EDIT :
1955                                    main_download(hDlg, config);
1956                                    config->cur_zone = ListView_GetNextItem(GetDlgItem(hDlg, IDC_ZONES), -1, LVNI_SELECTED);
1957                                    if (config->cur_zone != -1 && adv_dialog(hDlg, config, zone_dlgs, sizeof(zone_dlgs)/sizeof(int))) {
1958                                            zones_update(hDlg, config);
1959                                    }
1960                                    break;
1961    
1962                            case IDC_ADVANCED :
1963                                    main_download(hDlg, config);
1964                                    adv_dialog(hDlg, config, adv_dlgs, sizeof(adv_dlgs)/sizeof(int));
1965                                    break;
1966    
1967                            case IDC_DEFAULTS :
1968                                    config_reg_default(config);
1969                                    main_mode(hDlg, config);
1970                                    main_upload(hDlg, config);
1971                                    break;
1972    
1973                            case IDOK :
1974                                    main_download(hDlg, config);
1975                                    config->save = TRUE;
1976                                    EndDialog(hDlg, IDOK);
1977                                    break;
1978    
1979                            case IDCANCEL :
1980                                    config->save = FALSE;
1981                                    EndDialog(hDlg, IDCANCEL);
1982                                    break;
1983                            }
1984                    } else if (HIWORD(wParam) == LBN_SELCHANGE &&
1985                            (LOWORD(wParam)==IDC_PROFILE || LOWORD(wParam)==IDC_MODE)) {
1986    
1987                            config->mode = SendDlgItemMessage(hDlg, IDC_MODE, CB_GETCURSEL, 0, 0);
1988                            config->profile = SendDlgItemMessage(hDlg, IDC_PROFILE, CB_GETCURSEL, 0, 0);
1989    
1990                            if (!g_use_bitrate) {
1991                                    if (config->mode == RC_MODE_1PASS)
1992                                            set_dlgitem_float(hDlg, IDC_BITRATE, config->desired_quant);
1993                                    else if (config->mode == RC_MODE_2PASS2)
1994                                            SetDlgItemInt(hDlg, IDC_BITRATE, config->desired_size, FALSE);
1995                            }
1996    
1997                            main_mode(hDlg, config);
1998                            main_upload(hDlg, config);
1999    
2000                    }else if (HIWORD(wParam)==EN_UPDATE && LOWORD(wParam)==IDC_BITRATE) {
2001    
2002                            if (g_use_bitrate) {
2003                                    SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETPOS, TRUE,
2004                                                    config_get_uint(hDlg, IDC_BITRATE, DEFAULT_MIN_KBPS) );
2005                            } else if (config->mode == RC_MODE_1PASS) {
2006                                    SendDlgItemMessage(hDlg, IDC_SLIDER, TBM_SETPOS, TRUE,
2007                                                    get_dlgitem_float(hDlg, IDC_BITRATE, DEFAULT_QUANT) );
2008                            }
2009                            main_download(hDlg, config);
2010    
2011                    }else {
2012                            return 0;
2013                    }
2014                    break;
2015    
2016            case WM_HSCROLL :
2017                    if((HWND)lParam == GetDlgItem(hDlg, IDC_SLIDER)) {
2018                            if (g_use_bitrate)
2019                                    SetDlgItemInt(hDlg, IDC_BITRATE, SendMessage((HWND)lParam, TBM_GETPOS, 0, 0), FALSE);
2020                            else
2021                                    set_dlgitem_float(hDlg, IDC_BITRATE, SendMessage((HWND)lParam, TBM_GETPOS, 0, 0));
2022    
2023                            main_download(hDlg, config);
2024                            break;
2025                    }
2026                    return 0;
2027    
2028            default :
2029                    return 0;
2030            }
2031    
2032            return 1;
2033    }
2034    
2035    
2036    
2037    /* ===================================================================================== */
2038    /* ABOUT DIALOG ======================================================================== */
2039    /* ===================================================================================== */
2040    
2041    BOOL CALLBACK about_proc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
2042    {
2043            switch (uMsg)
2044            {
2045            case WM_INITDIALOG :
2046                    {
2047                            xvid_gbl_info_t info;
2048                            char core[100];
2049                            HFONT hFont;
2050                            LOGFONT lfData;
2051                            HINSTANCE m_hdll;
2052    
2053                            SetDlgItemText(hDlg, IDC_BUILD, XVID_BUILD);
2054                            SetDlgItemText(hDlg, IDC_SPECIAL_BUILD, XVID_SPECIAL_BUILD);
2055    
2056                            memset(&info, 0, sizeof(info));
2057                            info.version = XVID_VERSION;
2058    
2059                            m_hdll = LoadLibrary(XVID_DLL_NAME);
2060                            if (m_hdll != NULL) {
2061    
2062                                    ((int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global"))
2063                                            (0, XVID_GBL_INFO, &info, NULL);
2064    
2065                                    wsprintf(core, "xvidcore.dll version %d.%d.%d (\"%s\")",
2066                                            XVID_VERSION_MAJOR(info.actual_version),
2067                                            XVID_VERSION_MINOR(info.actual_version),
2068                                            XVID_VERSION_PATCH(info.actual_version),
2069                                            info.build);
2070    
2071                                    FreeLibrary(m_hdll);
2072                            } else {
2073                                    wsprintf(core, "xvidcore.dll not found!");
2074                            }
2075    
2076                            SetDlgItemText(hDlg, IDC_CORE, core);
2077    
2078                            hFont = (HFONT)SendDlgItemMessage(hDlg, IDC_WEBSITE, WM_GETFONT, 0, 0L);
2079    
2080                            if (GetObject(hFont, sizeof(LOGFONT), &lfData)) {
2081                                    lfData.lfUnderline = 1;
2082    
2083                                    hFont = CreateFontIndirect(&lfData);
2084                                    if (hFont) {
2085                                            SendDlgItemMessage(hDlg, IDC_WEBSITE, WM_SETFONT, (WPARAM)hFont, 1L);
2086                                    }
2087                            }
2088    
2089                            SetClassLong(GetDlgItem(hDlg, IDC_WEBSITE), GCL_HCURSOR, (LONG)LoadCursor(NULL, IDC_HAND));
2090                            SetDlgItemText(hDlg, IDC_WEBSITE, XVID_WEBSITE);
2091                    }
2092                    break;
2093            case WM_CTLCOLORSTATIC :
2094                    if ((HWND)lParam == GetDlgItem(hDlg, IDC_WEBSITE))
2095                    {
2096                            SetBkMode((HDC)wParam, TRANSPARENT) ;
2097                            SetTextColor((HDC)wParam, RGB(0x00,0x00,0xc0));
2098                            return (BOOL)GetStockObject(NULL_BRUSH);
2099                    }
2100                    return 0;
2101    
2102            case WM_COMMAND :
2103                    if (LOWORD(wParam) == IDC_WEBSITE && HIWORD(wParam) == STN_CLICKED)
2104                    {
2105                            ShellExecute(hDlg, "open", XVID_WEBSITE, NULL, NULL, SW_SHOWNORMAL);
2106                    }
2107                    else if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
2108                    {
2109                            EndDialog(hDlg, LOWORD(wParam));
2110                    }
2111                    break;
2112    
2113            default :
2114                    return 0;
2115            }
2116    
2117            return 1;
2118    }
2119    
2120    
2121    void
2122    sort_zones(zone_t * zones, int zone_num, int * sel)
2123    {
2124            int i, j;
2125            zone_t tmp;
2126            for (i = 0; i < zone_num; i++) {
2127                    int cur = i;
2128                    int min_f = zones[i].frame;
2129                    for (j = i + 1; j < zone_num; j++) {
2130                            if (zones[j].frame < min_f) {
2131                                    min_f = zones[j].frame;
2132                                    cur = j;
2133                            }
2134                    }
2135                    if (cur != i) {
2136                            tmp = zones[i];
2137                            zones[i] = zones[cur];
2138                            zones[cur] = tmp;
2139                            if (i == *sel) *sel = cur;
2140                            else if (cur == *sel) *sel = i;
2141                    }
2142            }
2143    }
2144    
2145    
2146    static void
2147    zones_update(HWND hDlg, CONFIG * config)
2148    {
2149            int i, sel;
2150    
2151            sel = ListView_GetNextItem(GetDlgItem(hDlg, IDC_ZONES), -1, LVNI_SELECTED);
2152    
2153            sort_zones(config->zones, config->num_zones, &sel);
2154    
2155            ListView_DeleteAllItems(GetDlgItem(hDlg,IDC_ZONES));
2156    
2157            for (i = 0; i < config->num_zones; i++)
2158                    main_insert_zone(GetDlgItem(hDlg,IDC_ZONES), &config->zones[i], i, TRUE);
2159    
2160            if (sel == -1 && config->num_zones > 0) sel = 0;
2161            if (sel >= config->num_zones) sel = config->num_zones-1;
2162    
2163            config->cur_zone = sel;
2164            ListView_SetItemState(GetDlgItem(hDlg, IDC_ZONES), sel, 0xffffffff, LVIS_SELECTED);
2165    }

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

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