revert commits 26437-26439 the right way[tm]
[mplayer/glamo.git] / TOOLS / vfw2menc.c
blob91a2cde3ca58fdf73f9e2603d65d5f8d185bf0e1
1 /*
2 * VFW Compressor Settings Tool
4 * Copyright (c) 2006 Gianluigi Tiesi <sherpya@netfarm.it>
6 * Official Website : http://oss.netfarm.it/mplayer-win32.php
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /* On MinGW compile with: gcc vfw2menc.c -o vfw2menc.exe -lwinmm -lole32 */
24 /* Using Wine: winegcc vfw2menc.c -o vfw2menc -lwinmm -lole32 */
25 /* MSVC requires getopt.c and getopt.h available at the original website */
27 #ifdef _MSC_VER
28 #define _CRT_SECURE_NO_DEPRECATE
29 #pragma warning(disable: 4996)
30 #endif
32 #define VERSION "0.1"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/stat.h>
37 #include <getopt.h>
38 #include <windows.h>
39 #include <vfw.h>
41 #define BAIL(msg) { printf("%s: %s\n", argv[0], msg); ret = -1; goto cleanup; }
43 typedef struct {
44 UINT uDriverSignature;
45 HINSTANCE hDriverModule;
46 DRIVERPROC DriverProc;
47 DWORD dwDriverID;
48 } DRVR;
50 typedef DRVR *PDRVR;
51 typedef DRVR *NPDRVR;
52 typedef DRVR *LPDRVR;
54 enum
56 MODE_NONE = 0,
57 MODE_CHECK,
58 MODE_SAVE,
59 MODE_VIEW
62 int save_settings(HDRVR hDriver, const char *filename)
64 FILE *fd = NULL;
65 DWORD cb = (DWORD) SendDriverMessage(hDriver, ICM_GETSTATE, 0, 0);
66 char *pv = NULL;
68 if (!cb)
70 printf("ICM_GETSTATE returned 0 size\n");
71 return -1;
74 pv = (char *) malloc(cb);
75 if (SendDriverMessage(hDriver, ICM_GETSTATE, (LPARAM) pv, (LPARAM) &cb) != ICERR_OK)
77 printf("ICM_GETSTATE failed\n");
78 free(pv);
79 return -1;
82 fd = fopen(filename, "wb");
83 if (!fd)
85 printf("Cannot open file %s for writing\n", filename);
86 free(pv);
87 return -1;
90 if (fwrite(pv, cb, 1, fd) != 1)
92 printf("fwrite() failed on %s\n", filename);
93 free(pv);
94 fclose(fd);
95 return -1;
97 fclose(fd);
98 free(pv);
99 return 0;
102 int load_settings(HDRVR hDriver, const char *filename)
104 struct stat info;
105 FILE *fd = NULL;
106 char *pv;
108 if (stat(filename, &info) < 0)
110 printf("stat() on %s failed\n", filename);
111 return -1;
114 pv = (char *) malloc(info.st_size);
115 fd = fopen(filename, "rb");
117 if (!fd)
119 printf("Cannot open file %s for reading\n", filename);
120 free(pv);
121 return -1;
124 if (fread(pv, info.st_size, 1, fd) != 1)
126 printf("fread() failed on %s\n", filename);
127 free(pv);
128 fclose(fd);
129 return -1;
131 fclose(fd);
132 if (!SendDriverMessage(hDriver, ICM_SETSTATE, (LPARAM) pv, (LPARAM) info.st_size))
134 printf("ICM_SETSTATE failed\n");
135 free(pv);
136 return -1;
138 free(pv);
139 return 0;
142 static struct option long_options[] =
144 { "help", no_argument, NULL, 'h' },
145 { "driver", required_argument, NULL, 'd' },
146 { "fourcc", required_argument, NULL, 'f' },
147 { "save", required_argument, NULL, 's' },
148 { "check", required_argument, NULL, 'c' },
149 { "view", no_argument, NULL, 'v' },
150 { 0, 0, 0, 0 }
153 void help(const char *progname)
155 printf("VFW to mencoder v"VERSION" - Copyright 2007 - Gianluigi Tiesi <sherpya@netfarm.it>\n");
156 printf("This program is Free Software\n\n");
157 printf("Usage: %s\n", progname);
158 printf(" -h|--help - displays this help\n");
159 printf(" -d|--driver filename - dll or drv to load\n");
160 printf(" -f|--fourcc fourcc - fourcc of selected driver (look at codecs.conf)\n");
161 printf(" -s|--save filename - save settings to file\n");
162 printf(" -c|--check filename - load and show setting in filename\n");
163 printf(" -v|--view - displays the config dialog and do nothing\n");
164 printf("\nExamples:\n");
165 printf(" %s -f VP62 -d vp6vfw.dll -s firstpass.mcf\n", progname);
166 printf(" %s -f VP62 -d vp6vfw.dll -c firstpass.mcf\n", progname);
167 printf(" %s -f VP62 -d vp6vfw.dll -v\n", progname);
168 printf("\nIf the driver dialog doesn't work, you can try without specifing a fourcc,\n");
169 printf("but the compdata file will not work with mencoder.\n");
170 printf("Driver option is required and you must specify at least -s, -c -o -v\n");
171 printf("Usage with mencoder -ovc vfw -xvfwopts codec=vp6vfw.dll:compdata=settings.mcf\n");
174 int main(int argc, char *argv[])
176 char *driver = NULL;
177 char *fourcc = NULL;
178 char *filename = NULL;
179 unsigned char mode = 0;
180 DWORD dwFCC = 0;
181 ICOPEN icopen;
182 HRESULT coinit = -1;
183 /* ICINFO icinfo; */
185 wchar_t drvfile[MAX_PATH];
186 HDRVR hDriver = NULL;
187 int ret = 0;
188 int c = -1, long_options_index = -1;
190 if (argc < 2)
192 help(argv[0]);
193 ret = -1;
194 goto cleanup;
197 while ((c = getopt_long(argc, argv, "hd:f:s:c:v", long_options, &long_options_index)) != -1)
199 switch (c)
201 case 'h':
202 help(argv[0]);
203 ret = 0;
204 goto cleanup;
205 case 'd':
206 driver = strdup(optarg);
207 break;
208 case 'f':
209 fourcc = strdup(optarg);
210 if (strlen(optarg) != 4) BAIL("Fourcc must be exactly 4 chars");
211 break;
212 case 's':
213 if (mode != MODE_NONE) BAIL("Incompatible arguments");
214 filename = strdup(optarg);
215 mode = MODE_SAVE;
216 break;
217 case 'c':
218 if (mode != MODE_NONE) BAIL("Incompatible arguments");
219 filename = strdup(optarg);
220 mode = MODE_CHECK;
221 break;
222 case 'v':
223 if (mode != MODE_NONE) BAIL("Incompatible arguments");
224 mode = MODE_VIEW;
225 break;
226 default:
227 printf("Wrong arguments!\n");
228 help(argv[0]);
229 goto cleanup;
233 if (!(argc == optind) && (mode != MODE_NONE) &&
234 driver && (filename || (mode == MODE_VIEW)))
236 help(argv[0]);
237 goto cleanup;
240 if (!MultiByteToWideChar(CP_ACP, 0, driver, -1, drvfile, MAX_PATH))
241 BAIL("MultiByteToWideChar() failed\n");
243 if (fourcc) memcpy(&dwFCC, fourcc, 4);
244 memset(&icopen, 0, sizeof(icopen));
246 icopen.dwSize = sizeof(icopen);
247 icopen.fccType = ICTYPE_VIDEO; /* VIDC */
248 icopen.fccHandler = dwFCC;
249 icopen.dwVersion = 0x00001000; /* FIXME */
250 icopen.dwFlags = ICMODE_COMPRESS;
251 icopen.dwError = 0;
252 icopen.pV1Reserved = NULL;
253 icopen.pV2Reserved = NULL;
254 icopen.dnDevNode = -1; /* FIXME */
256 coinit = CoInitialize(NULL);
258 if (!(hDriver = OpenDriver(drvfile, NULL, (LPARAM) &icopen)))
259 BAIL("OpenDriver() failed\n");
262 memset(&icinfo, 0, sizeof(ICINFO));
263 icinfo.dwSize = sizeof(ICINFO);
264 SendDriverMessage(hDriver, ICM_GETINFO, (LPARAM) &icinfo, sizeof(ICINFO));
267 if (SendDriverMessage(hDriver, ICM_CONFIGURE, -1, 0) != ICERR_OK)
268 BAIL("The driver doesn't provide a configure dialog");
271 switch(mode)
273 case MODE_CHECK:
274 if (load_settings(hDriver, filename))
275 BAIL("Cannot load settings from file");
276 if (SendDriverMessage(hDriver, ICM_CONFIGURE, 0, 0) != ICERR_OK)
277 BAIL("ICM_CONFIGURE failed");
278 break;
279 case MODE_SAVE:
280 if (SendDriverMessage(hDriver, ICM_CONFIGURE, 0, 0) != ICERR_OK)
281 BAIL("ICM_CONFIGURE failed");
282 if (save_settings(hDriver, filename))
283 BAIL("Cannot save settings to file");
284 break;
285 case MODE_VIEW:
287 HWND hwnd = GetDesktopWindow();
288 if (SendDriverMessage(hDriver, ICM_CONFIGURE, (LPARAM) hwnd, 0) != ICERR_OK)
289 BAIL("ICM_CONFIGURE failed");
291 break;
292 default:
293 BAIL("This should not happen :)");
296 cleanup:
297 if (driver) free(driver);
298 if (fourcc) free(fourcc);
299 if (filename) free(filename);
300 if (hDriver) CloseDriver(hDriver, 0, 0);
301 if ((coinit == S_OK) || coinit == S_FALSE) CoUninitialize();
302 return ret;