summaryrefslogtreecommitdiffstats
path: root/TOOLS/vfw2menc.c
blob: 91a2cde3ca58fdf73f9e2603d65d5f8d185bf0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * VFW Compressor Settings Tool
 *
 * Copyright (c) 2006 Gianluigi Tiesi <sherpya@netfarm.it>
 *
 * Official Website : http://oss.netfarm.it/mplayer-win32.php
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* On MinGW compile with: gcc vfw2menc.c -o vfw2menc.exe -lwinmm -lole32 */
/* Using Wine: winegcc vfw2menc.c -o vfw2menc -lwinmm -lole32 */
/* MSVC requires getopt.c and getopt.h available at the original website */

#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#pragma warning(disable: 4996)
#endif

#define VERSION "0.1"

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <getopt.h>
#include <windows.h>
#include <vfw.h>

#define BAIL(msg) { printf("%s: %s\n", argv[0], msg); ret = -1; goto cleanup; }

typedef struct {
    UINT             uDriverSignature;
    HINSTANCE        hDriverModule;
    DRIVERPROC       DriverProc;
    DWORD            dwDriverID;
} DRVR;

typedef DRVR  *PDRVR;
typedef DRVR  *NPDRVR;
typedef DRVR  *LPDRVR;

enum
{
    MODE_NONE = 0,
    MODE_CHECK,
    MODE_SAVE,
    MODE_VIEW
};

int save_settings(HDRVR hDriver, const char *filename)
{
    FILE *fd = NULL;
    DWORD cb = (DWORD) SendDriverMessage(hDriver, ICM_GETSTATE, 0, 0);
    char *pv = NULL;

    if (!cb)
    {
        printf("ICM_GETSTATE returned 0 size\n");
        return -1;
    }

    pv = (char *) malloc(cb);
    if (SendDriverMessage(hDriver, ICM_GETSTATE, (LPARAM) pv, (LPARAM) &cb) != ICERR_OK)
    {
        printf("ICM_GETSTATE failed\n");
        free(pv);
        return -1;
    }

    fd = fopen(filename, "wb");
    if (!fd)
    {
        printf("Cannot open file %s for writing\n", filename);
        free(pv);
        return -1;
    }

    if (fwrite(pv, cb, 1, fd) != 1)
    {
        printf("fwrite() failed on %s\n", filename);
        free(pv);
        fclose(fd);
        return -1;
    }
    fclose(fd);
    free(pv);
    return 0;
}

int load_settings(HDRVR hDriver, const char *filename)
{
    struct stat info;
    FILE *fd = NULL;
    char *pv;

    if (stat(filename, &info) < 0)
    {
        printf("stat() on %s failed\n", filename);
        return -1;
    }

    pv = (char *) malloc(info.st_size);
    fd = fopen(filename, "rb");

    if (!fd)
    {
        printf("Cannot open file %s for reading\n", filename);
        free(pv);
        return -1;
    }

    if (fread(pv, info.st_size, 1, fd) != 1)
    {
        printf("fread() failed on %s\n", filename);
        free(pv);
        fclose(fd);
        return -1;
    }
    fclose(fd);
    if (!SendDriverMessage(hDriver, ICM_SETSTATE, (LPARAM) pv, (LPARAM) info.st_size))
    {
        printf("ICM_SETSTATE failed\n");
        free(pv);
        return -1;
    }
    free(pv);
    return 0;
}

static struct option long_options[] =
{
    { "help",   no_argument,        NULL,   'h' },
    { "driver", required_argument,  NULL,   'd' },
    { "fourcc", required_argument,  NULL,   'f' },
    { "save",   required_argument,  NULL,   's' },
    { "check",  required_argument,  NULL,   'c' },
    { "view",   no_argument,        NULL,   'v' },
    { 0, 0, 0, 0 }
};

void help(const char *progname)
{
    printf("VFW to mencoder v"VERSION" - Copyright 2007 - Gianluigi Tiesi <sherpya@netfarm.it>\n");
    printf("This program is Free Software\n\n");
    printf("Usage: %s\n", progname);
    printf("      -h|--help            - displays this help\n");
    printf("      -d|--driver filename - dll or drv to load\n");
    printf("      -f|--fourcc fourcc   - fourcc of selected driver (look at codecs.conf)\n");
    printf("      -s|--save filename   - save settings to file\n");
    printf("      -c|--check filename  - load and show setting in filename\n");
    printf("      -v|--view            - displays the config dialog and do nothing\n");
    printf("\nExamples:\n");
    printf(" %s -f VP62 -d vp6vfw.dll -s firstpass.mcf\n", progname);
    printf(" %s -f VP62 -d vp6vfw.dll -c firstpass.mcf\n", progname);
    printf(" %s -f VP62 -d vp6vfw.dll -v\n", progname);
    printf("\nIf the driver dialog doesn't work, you can try without specifing a fourcc,\n");
    printf("but the compdata file will not work with mencoder.\n");
    printf("Driver option is required and you must specify at least -s, -c -o -v\n");
    printf("Usage with mencoder -ovc vfw -xvfwopts codec=vp6vfw.dll:compdata=settings.mcf\n");
}

int main(int argc, char *argv[])
{
    char *driver = NULL;
    char *fourcc = NULL;
    char *filename = NULL;
    unsigned char mode = 0;
    DWORD dwFCC = 0;
    ICOPEN icopen;
    HRESULT coinit = -1;
    /* ICINFO icinfo; */

    wchar_t drvfile[MAX_PATH];
    HDRVR hDriver = NULL;
    int ret = 0;
    int c = -1, long_options_index = -1;

    if (argc < 2)
    {
        help(argv[0]);
        ret = -1;
        goto cleanup;
    }

    while ((c = getopt_long(argc, argv, "hd:f:s:c:v", long_options, &long_options_index)) != -1)
    {
        switch (c)
        {
            case 'h':
                help(argv[0]);
                ret = 0;
                goto cleanup;
            case 'd':
                driver = strdup(optarg);
                break;
            case 'f':
                fourcc = strdup(optarg);
                if (strlen(optarg) != 4) BAIL("Fourcc must be exactly 4 chars");
                break;
            case 's':
                if (mode != MODE_NONE) BAIL("Incompatible arguments");
                filename = strdup(optarg);
                mode = MODE_SAVE;
                break;
            case 'c':
                if (mode != MODE_NONE) BAIL("Incompatible arguments");
                filename = strdup(optarg);
                mode = MODE_CHECK;
                break;
            case 'v':
                if (mode != MODE_NONE) BAIL("Incompatible arguments");
                mode = MODE_VIEW;
                break;
            default:
                printf("Wrong arguments!\n");
                help(argv[0]);
                goto cleanup;
        }
    }

    if (!(argc == optind) && (mode != MODE_NONE) &&
        driver && (filename || (mode == MODE_VIEW)))
    {
        help(argv[0]);
        goto cleanup;
    }

    if (!MultiByteToWideChar(CP_ACP, 0, driver, -1, drvfile, MAX_PATH))
        BAIL("MultiByteToWideChar() failed\n");

    if (fourcc) memcpy(&dwFCC, fourcc, 4);
    memset(&icopen, 0, sizeof(icopen));

    icopen.dwSize = sizeof(icopen);
    icopen.fccType = ICTYPE_VIDEO; /* VIDC */
    icopen.fccHandler = dwFCC;
    icopen.dwVersion  = 0x00001000; /* FIXME */
    icopen.dwFlags = ICMODE_COMPRESS;
    icopen.dwError = 0;
    icopen.pV1Reserved = NULL;
    icopen.pV2Reserved = NULL;
    icopen.dnDevNode = -1; /* FIXME */

    coinit = CoInitialize(NULL);

    if (!(hDriver = OpenDriver(drvfile, NULL, (LPARAM) &icopen)))
        BAIL("OpenDriver() failed\n");

   /*
        memset(&icinfo, 0, sizeof(ICINFO));
        icinfo.dwSize = sizeof(ICINFO);
        SendDriverMessage(hDriver, ICM_GETINFO, (LPARAM) &icinfo, sizeof(ICINFO));
    */

    if (SendDriverMessage(hDriver, ICM_CONFIGURE, -1, 0) != ICERR_OK)
        BAIL("The driver doesn't provide a configure dialog");


    switch(mode)
    {
        case MODE_CHECK:
            if (load_settings(hDriver, filename))
                BAIL("Cannot load settings from file");
            if (SendDriverMessage(hDriver, ICM_CONFIGURE, 0, 0) != ICERR_OK)
                BAIL("ICM_CONFIGURE failed");
            break;
        case MODE_SAVE:
            if (SendDriverMessage(hDriver, ICM_CONFIGURE, 0, 0) != ICERR_OK)
                BAIL("ICM_CONFIGURE failed");
            if (save_settings(hDriver, filename))
                BAIL("Cannot save settings to file");
            break;
        case MODE_VIEW:
            {
                HWND hwnd = GetDesktopWindow();
                if (SendDriverMessage(hDriver, ICM_CONFIGURE, (LPARAM) hwnd, 0) != ICERR_OK)
                    BAIL("ICM_CONFIGURE failed");
            }
            break;
        default:
            BAIL("This should not happen :)");
    }

cleanup:
    if (driver) free(driver);
    if (fourcc) free(fourcc);
    if (filename) free(filename);
    if (hDriver) CloseDriver(hDriver, 0, 0);
    if ((coinit == S_OK) || coinit == S_FALSE) CoUninitialize();
    return ret;
}