1 /**
2 *
3 * Lame ACM wrapper, encode/decode MP3 based RIFF/AVI files in MS Windows
4 *
5 * Copyright (c) 2002 Steve Lhomme <steve.lhomme at free.fr>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 /*!
24 \author Steve Lhomme
25 \version \$Id$
26 */
27
28 #if !defined(STRICT)
29 #define STRICT
30 #endif // STRICT
31
32 #include <windows.h>
33
34 /// The ACM is considered as a driver and run in Kernel-Mode
35 /// So the new/delete operators have to be overriden in order to use memory
36 /// readable out of the calling process
37
operator new(unsigned int cb)38 void * operator new( unsigned int cb )
39 {
40 return LocalAlloc(LPTR, cb); // VirtualAlloc
41 }
42
operator delete(void * block)43 void operator delete(void *block) {
44 LocalFree(block);
45 }
46
47 extern "C" {
48
acm_Calloc(size_t num,size_t size)49 void *acm_Calloc( size_t num, size_t size )
50 {
51 return LocalAlloc(LPTR, num * size); // VirtualAlloc
52 }
53
acm_Malloc(size_t size)54 void *acm_Malloc( size_t size )
55 {
56 return LocalAlloc(LPTR, size); // VirtualAlloc
57 }
58
acm_Free(void * mem)59 void acm_Free( void * mem)
60 {
61 LocalFree(mem);
62 }
63 };
64
65 ////// End of memory instrumentation
66
67 #include <mmreg.h>
68 #include <msacm.h>
69 #include <msacmdrv.h>
70
71 #include <assert.h>
72
73 #include "AEncodeProperties.h"
74 #include "ACM.h"
75 #include "resource.h"
76 #include "adebug.h"
77
78
79 ADbg * debug = NULL;
80
DriverProc(DWORD dwDriverId,HDRVR hdrvr,UINT msg,LONG lParam1,LONG lParam2)81 LONG WINAPI DriverProc(DWORD dwDriverId, HDRVR hdrvr, UINT msg, LONG lParam1, LONG lParam2)
82 {
83
84 switch (msg)
85 {
86 case DRV_OPEN: // acmDriverOpen
87 {
88 if (debug == NULL) {
89 debug = new ADbg(DEBUG_LEVEL_CREATION);
90 debug->setPrefix("LAMEdrv");
91 }
92
93 if (debug != NULL)
94 {
95 // Sent when the driver is opened.
96 if (lParam2 != NULL)
97 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_OPEN (ID 0x%08X), pDesc = 0x%08X",dwDriverId,lParam2);
98 else
99 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_OPEN (ID 0x%08X), pDesc = NULL",dwDriverId);
100 }
101
102 if (lParam2 != NULL) {
103 LPACMDRVOPENDESC pDesc = (LPACMDRVOPENDESC)lParam2;
104
105 if (pDesc->fccType != ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC) {
106 if (debug != NULL)
107 {
108 debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "wrong pDesc->fccType (0x%08X)",pDesc->fccType);
109 }
110 return NULL;
111 }
112 } else {
113 if (debug != NULL)
114 {
115 debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "pDesc == NULL");
116 }
117 }
118
119 ACM * ThisACM = new ACM(GetDriverModuleHandle(hdrvr));
120
121 if (debug != NULL)
122 {
123 debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "OPENED instance 0x%08X",ThisACM);
124 }
125
126 return (LONG)ThisACM;// returns 0L to fail
127 // value subsequently used
128 // for dwDriverId.
129 }
130 break;
131
132 case DRV_CLOSE: // acmDriverClose
133 {
134 if (debug != NULL)
135 {
136 // Sent when the driver is closed. Drivers are
137 // unloaded when the open count reaches zero.
138 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_CLOSE");
139 }
140
141 ACM * ThisACM = (ACM *)dwDriverId;
142 delete ThisACM;
143 if (debug != NULL)
144 {
145 debug->OutPut(DEBUG_LEVEL_FUNC_CODE, "CLOSED instance 0x%08X",ThisACM);
146 delete debug;
147 debug = NULL;
148 }
149 return 1L; // returns 0L to fail
150 }
151 break;
152
153 case DRV_LOAD:
154 {
155 // nothing to do
156 if (debug != NULL)
157 {
158 // debug->OutPut(DEBUG_LEVEL_MSG, "DRV_LOAD, version %s %s %s", ACM_VERSION, __DATE__, __TIME__);
159 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_LOAD, %s %s", __DATE__, __TIME__);
160 }
161 return 1L;
162 }
163 break;
164
165 case DRV_ENABLE:
166 {
167 // nothing to do
168 if (debug != NULL)
169 {
170 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_ENABLE");
171 }
172 return 1L;
173 }
174 break;
175
176 case DRV_DISABLE:
177 {
178 // nothing to do
179 if (debug != NULL)
180 {
181 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_DISABLE");
182 }
183 return 1L;
184 }
185 break;
186
187 case DRV_FREE:
188 {
189 if (debug != NULL)
190 {
191 debug->OutPut(DEBUG_LEVEL_MSG, "DRV_FREE");
192 }
193 return 1L;
194 }
195 break;
196
197 default:
198 {
199 ACM * ThisACM = (ACM *)dwDriverId;
200
201 if (ThisACM != NULL)
202 return ThisACM->DriverProcedure(hdrvr, msg, lParam1, lParam2);
203 else
204 {
205 if (debug != NULL)
206 {
207 debug->OutPut(DEBUG_LEVEL_MSG, "Driver not opened, unknown message (0x%08X), lParam1 = 0x%08X, lParam2 = 0x%08X", msg, lParam1, lParam2);
208 }
209
210 return DefDriverProc (dwDriverId, hdrvr, msg, lParam1, lParam2);
211 }
212 }
213 break;
214 }
215 }
216
217