This repository was archived by the owner on May 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriverproc.c
More file actions
320 lines (269 loc) · 10.1 KB
/
driverproc.c
File metadata and controls
320 lines (269 loc) · 10.1 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*****************************************************************************
* driverproc.c: vfw wrapper
*****************************************************************************
* Copyright (C) 2003-2017 x264vfw project
*
* Authors: Justin Clay
* Laurent Aimar <fenrir@via.ecp.fr>
* Anton Mitrofanov <BugMaster@narod.ru>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*****************************************************************************/
#include "x264vfw.h"
#ifdef PTW32_STATIC_LIB
#include <pthread.h>
#endif
/* Global DLL instance */
HINSTANCE x264vfw_hInst;
/* Global DLL critical section */
CRITICAL_SECTION x264vfw_CS;
/* Calling back point for our DLL so we can keep track of the window in x264vfw_hInst */
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
x264vfw_hInst = hinstDLL;
#ifdef PTW32_STATIC_LIB
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
InitializeCriticalSection(&x264vfw_CS);
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
break;
case DLL_THREAD_ATTACH:
pthread_win32_thread_attach_np();
break;
case DLL_THREAD_DETACH:
pthread_win32_thread_detach_np();
break;
case DLL_PROCESS_DETACH:
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
DeleteCriticalSection(&x264vfw_CS);
break;
}
#else
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
InitializeCriticalSection(&x264vfw_CS);
break;
case DLL_PROCESS_DETACH:
DeleteCriticalSection(&x264vfw_CS);
break;
}
#endif
return TRUE;
}
#if defined(HAVE_FFMPEG)
static void log_callback(void *ptr, int level, const char *fmt, va_list vl)
{
if (level <= av_log_get_level())
DVPRINTF(fmt, vl);
}
#endif
/* This little puppy handles the calls which VFW programs send out to the codec */
extern "C" LRESULT WINAPI attribute_align_arg DriverProc(DWORD_PTR dwDriverId, HDRVR hDriver, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
{
CODEC *codec = (CODEC *)dwDriverId;
switch (uMsg)
{
case DRV_LOAD:
#if defined(HAVE_FFMPEG)
avcodec_register_all();
av_log_set_callback(log_callback);
#endif
return DRV_OK;
case DRV_FREE:
return DRV_OK;
case DRV_OPEN:
{
ICOPEN *icopen = (ICOPEN *)lParam2;
if (icopen && icopen->fccType != ICTYPE_VIDEO)
return 0;
if (!(codec = (CODEC*)malloc(sizeof(CODEC))))
{
if (icopen)
icopen->dwError = ICERR_MEMORY;
return 0;
}
memset(codec, 0, sizeof(CODEC));
x264vfw_config_reg_load(&codec->config);
#if defined(HAVE_FFMPEG) && X264VFW_USE_DECODER
codec->decoder_enabled = !codec->config.b_disable_decoder;
#endif
x264vfw_default_compress_frames_info(codec);
if (icopen)
icopen->dwError = ICERR_OK;
return (LRESULT)codec;
}
case DRV_CLOSE:
/* From xvid: x264vfw_compress_end/x264vfw_decompress_end don't always get called */
x264vfw_compress_end(codec);
#if defined(HAVE_FFMPEG) && X264VFW_USE_DECODER
x264vfw_decompress_end(codec);
#endif
x264vfw_log_destroy(codec);
free(codec);
return DRV_OK;
case DRV_QUERYCONFIGURE:
return 0;
case DRV_CONFIGURE:
return DRV_CANCEL;
/*
case DRV_DISABLE:
case DRV_ENABLE:
case DRV_INSTALL:
case DRV_REMOVE:
case DRV_EXITSESSION:
case DRV_POWER:
return DRV_OK;
*/
/* ICM */
case ICM_GETSTATE:
if (!(void *)lParam1)
return sizeof(CONFIG);
if (lParam2 != sizeof(CONFIG))
return ICERR_BADSIZE;
memcpy((void *)lParam1, &codec->config, sizeof(CONFIG));
/* Set format version */
((CONFIG *)lParam1)->i_format_version = X264VFW_FORMAT_VERSION;
return ICERR_OK;
case ICM_SETSTATE:
if (!(void *)lParam1)
{
x264vfw_config_reg_load(&codec->config);
return 0;
}
if (lParam2 != sizeof(CONFIG) || ((CONFIG *)lParam1)->i_format_version != X264VFW_FORMAT_VERSION)
return 0;
memcpy(&codec->config, (void *)lParam1, sizeof(CONFIG));
return sizeof(CONFIG);
case ICM_GETINFO:
{
ICINFO *icinfo = (ICINFO *)lParam1;
if (lParam2 < sizeof(ICINFO))
return 0;
/* Fill all members of the ICINFO structure except szDriver */
icinfo->dwSize = sizeof(ICINFO);
icinfo->fccType = ICTYPE_VIDEO;
icinfo->fccHandler = FOURCC_X264;
icinfo->dwFlags = VIDCF_COMPRESSFRAMES | VIDCF_FASTTEMPORALC;
#if defined(HAVE_FFMPEG) && X264VFW_USE_DECODER
/* ICM_GETINFO may be called before DRV_OPEN so 'codec' can point to NULL */
if (!codec || codec->decoder_enabled)
icinfo->dwFlags |= VIDCF_FASTTEMPORALD;
#endif
icinfo->dwVersion = 0;
#ifdef ICVERSION
icinfo->dwVersionICM = ICVERSION;
#else
icinfo->dwVersionICM = 0x0104; /* MinGW's vfw.h doesn't define ICVERSION for some weird reason */
#endif
wcscpy(icinfo->szName, X264VFW_NAME_L);
wcscpy(icinfo->szDescription, X264VFW_DESC_L);
return sizeof(ICINFO);
}
case ICM_CONFIGURE:
if (lParam1 != -1)
{
CONFIG_DATA temp;
memset(&temp, 0, sizeof(CONFIG_DATA));
memcpy(&temp.config, &codec->config, sizeof(CONFIG));
DialogBoxParamW(x264vfw_hInst, MAKEINTRESOURCEW(IDD_CONFIG), (HWND)lParam1, x264vfw_callback_main, (LPARAM)&temp);
if (temp.b_save)
{
memcpy(&codec->config, &temp.config, sizeof(CONFIG));
x264vfw_config_reg_save(&codec->config);
}
}
return ICERR_OK;
case ICM_ABOUT:
if (lParam1 != -1)
DialogBoxParamW(x264vfw_hInst, MAKEINTRESOURCEW(IDD_ABOUT), (HWND)lParam1, x264vfw_callback_about, 0);
return ICERR_OK;
case ICM_GET:
if (!(void *)lParam1)
return 0;
return ICERR_OK;
case ICM_SET:
return 0;
/* Compressor */
case ICM_COMPRESS_GET_FORMAT:
return x264vfw_compress_get_format(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
case ICM_COMPRESS_GET_SIZE:
return x264vfw_compress_get_size(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
case ICM_COMPRESS_QUERY:
#if X264_BIT_DEPTH>8
return ICERR_UNSUPPORTED;
#endif
return x264vfw_compress_query(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
case ICM_COMPRESS_BEGIN:
return x264vfw_compress_begin(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
case ICM_COMPRESS:
return x264vfw_compress(codec, (ICCOMPRESS *)lParam1);
case ICM_COMPRESS_END:
x264vfw_default_compress_frames_info(codec);
return x264vfw_compress_end(codec);
case ICM_COMPRESS_FRAMES_INFO:
return x264vfw_compress_frames_info(codec, (ICCOMPRESSFRAMES *)lParam1);
#if defined(HAVE_FFMPEG) && X264VFW_USE_DECODER
/* Decompressor */
case ICM_DECOMPRESS_GET_FORMAT:
if (codec->decoder_enabled)
return x264vfw_decompress_get_format(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
else
return ICERR_UNSUPPORTED;
case ICM_DECOMPRESS_QUERY:
if (codec->decoder_enabled)
return x264vfw_decompress_query(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
else
return ICERR_UNSUPPORTED;
case ICM_DECOMPRESS_BEGIN:
if (codec->decoder_enabled)
return x264vfw_decompress_begin(codec, (BITMAPINFO *)lParam1, (BITMAPINFO *)lParam2);
else
return ICERR_UNSUPPORTED;
case ICM_DECOMPRESS:
if (codec->decoder_enabled)
return x264vfw_decompress(codec, (ICDECOMPRESS *)lParam1);
else
return ICERR_UNSUPPORTED;
case ICM_DECOMPRESS_END:
if (codec->decoder_enabled)
return x264vfw_decompress_end(codec);
else
return ICERR_UNSUPPORTED;
#endif
default:
if (uMsg < DRV_USER)
return DefDriverProc(dwDriverId, hDriver, uMsg, lParam1, lParam2);
else
return ICERR_UNSUPPORTED;
}
}
extern "C" void WINAPI Configure(HWND hwnd, HINSTANCE hinst, LPTSTR lpCmdLine, int nCmdShow)
{
if (DriverProc(0, 0, DRV_LOAD, 0, 0))
{
DWORD_PTR dwDriverId;
dwDriverId = DriverProc(0, 0, DRV_OPEN, 0, 0);
if (dwDriverId != (DWORD_PTR)NULL)
{
DriverProc(dwDriverId, 0, ICM_CONFIGURE, (LPARAM)GetDesktopWindow(), 0);
DriverProc(dwDriverId, 0, DRV_CLOSE, 0, 0);
}
DriverProc(0, 0, DRV_FREE, 0, 0);
}
}