msgsm32.acm: Add support for format enumeration.
[wine] / dlls / msgsm32.acm / msgsm32.c
1 /*
2  * GSM 06.10 codec handling
3  * Copyright (C) 2009 Maarten Lankhorst
4  *
5  * Based on msg711.acm
6  * Copyright (C) 2002 Eric Pouech
7  *
8  * This library 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.
12  *
13  * This library 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.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include <wine/port.h>
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29
30 #ifdef HAVE_GSM_H
31 #include <gsm.h>
32 #endif
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "winuser.h"
38 #include "winnls.h"
39 #include "mmsystem.h"
40 #include "mmreg.h"
41 #include "msacm.h"
42 #include "msacmdrv.h"
43 #include "wine/library.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(gsm);
47
48 #ifdef SONAME_LIBGSM
49
50 static void *libgsm_handle;
51 #define FUNCPTR(f) static typeof(f) * p##f
52 FUNCPTR(gsm_create);
53 FUNCPTR(gsm_destroy);
54 FUNCPTR(gsm_option);
55 FUNCPTR(gsm_encode);
56 FUNCPTR(gsm_decode);
57
58 #define LOAD_FUNCPTR(f) \
59     if((p##f = wine_dlsym(libgsm_handle, #f, NULL, 0)) == NULL) { \
60         wine_dlclose(libgsm_handle, NULL, 0); \
61         libgsm_handle = NULL; \
62         return 0; \
63     }
64
65 /***********************************************************************
66  *           GSM_drvLoad
67  */
68 static LRESULT GSM_drvLoad(void)
69 {
70     char error[128];
71
72     libgsm_handle = wine_dlopen(SONAME_LIBGSM, RTLD_NOW, error, sizeof(error));
73     if (libgsm_handle)
74     {
75         LOAD_FUNCPTR(gsm_create);
76         LOAD_FUNCPTR(gsm_destroy);
77         LOAD_FUNCPTR(gsm_option);
78         LOAD_FUNCPTR(gsm_encode);
79         LOAD_FUNCPTR(gsm_decode);
80         return 1;
81     }
82     else
83     {
84         ERR("Couldn't load " SONAME_LIBGSM ": %s\n", error);
85         return 0;
86     }
87 }
88
89 /***********************************************************************
90  *           GSM_drvFree
91  */
92 static LRESULT GSM_drvFree(void)
93 {
94     if (libgsm_handle)
95         wine_dlclose(libgsm_handle, NULL, 0);
96     return 1;
97 }
98
99 #else
100
101 static LRESULT GSM_drvLoad(void)
102 {
103     return 1;
104 }
105
106 static LRESULT GSM_drvFree(void)
107 {
108     return 1;
109 }
110
111 #endif
112
113 /***********************************************************************
114  *           GSM_DriverDetails
115  *
116  */
117 static  LRESULT GSM_DriverDetails(PACMDRIVERDETAILSW add)
118 {
119     add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
120     add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
121     /* Details found from probing native msgsm32.acm */
122     add->wMid = MM_MICROSOFT;
123     add->wPid = 36;
124     add->vdwACM = 0x3320000;
125     add->vdwDriver = 0x4000000;
126     add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
127     add->cFormatTags = 2;
128     add->cFilterTags = 0;
129     add->hicon = NULL;
130     MultiByteToWideChar( CP_ACP, 0, "Wine GSM 6.10", -1,
131                          add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
132     MultiByteToWideChar( CP_ACP, 0, "Wine GSM 6.10 libgsm codec", -1,
133                          add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
134     MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
135                          add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
136     MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
137                          add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
138     add->szFeatures[0] = 0;
139     return MMSYSERR_NOERROR;
140 }
141
142 /* Validate a WAVEFORMATEX structure */
143 static DWORD GSM_FormatValidate(const WAVEFORMATEX *wfx)
144 {
145     if (wfx->nChannels != 1)
146         return 0;
147
148     switch (wfx->wFormatTag)
149     {
150     case WAVE_FORMAT_PCM:
151         if (wfx->wBitsPerSample != 16)
152         {
153             WARN("PCM wBitsPerSample %u\n", wfx->wBitsPerSample);
154             return 0;
155         }
156         if (wfx->nBlockAlign != 2)
157         {
158             WARN("PCM nBlockAlign %u\n", wfx->nBlockAlign);
159             return 0;
160         }
161         if (wfx->nAvgBytesPerSec != wfx->nBlockAlign * wfx->nSamplesPerSec)
162         {
163             WARN("PCM nAvgBytesPerSec %u/%u\n",
164                  wfx->nAvgBytesPerSec,
165                  wfx->nBlockAlign * wfx->nSamplesPerSec);
166             return 0;
167         }
168         return 1;
169     case WAVE_FORMAT_GSM610:
170         if (wfx->cbSize < sizeof(WORD))
171         {
172             WARN("GSM cbSize %u\n", wfx->cbSize);
173             return 0;
174         }
175         if (wfx->wBitsPerSample != 0)
176         {
177             WARN("GSM wBitsPerSample %u\n", wfx->wBitsPerSample);
178             return 0;
179         }
180         if (wfx->nBlockAlign != 65)
181         {
182             WARN("GSM nBlockAlign %u\n", wfx->nBlockAlign);
183             return 0;
184         }
185         if (((GSM610WAVEFORMAT*)wfx)->wSamplesPerBlock != 320)
186         {
187             WARN("GSM wSamplesPerBlock %u\n",
188                  ((GSM610WAVEFORMAT*)wfx)->wSamplesPerBlock);
189             return 0;
190         }
191         if (wfx->nAvgBytesPerSec != wfx->nSamplesPerSec * 65 / 320)
192         {
193             WARN("GSM nAvgBytesPerSec %d / %d\n",
194                  wfx->nAvgBytesPerSec, wfx->nSamplesPerSec * 65 / 320);
195             return 0;
196         }
197         return 1;
198     default:
199         return 0;
200     }
201     return 0;
202 }
203
204 static const DWORD gsm_rates[] = { 8000, 11025, 22050, 44100, 48000, 96000 };
205 #define NUM_RATES (sizeof(gsm_rates)/sizeof(*gsm_rates))
206
207 /***********************************************************************
208  *           GSM_FormatTagDetails
209  *
210  */
211 static  LRESULT GSM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
212 {
213     static const WCHAR szPcm[]={'P','C','M',0};
214     static const WCHAR szGsm[]={'G','S','M',' ','6','.','1','0',0};
215
216     switch (dwQuery)
217     {
218     case ACM_FORMATTAGDETAILSF_INDEX:
219         if (aftd->dwFormatTagIndex > 1) return ACMERR_NOTPOSSIBLE;
220         break;
221     case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
222         if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
223         {
224             aftd->dwFormatTagIndex = 1;
225             break;
226         }
227         /* fall thru */
228     case ACM_FORMATTAGDETAILSF_FORMATTAG:
229         switch (aftd->dwFormatTag)
230         {
231         case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
232         case WAVE_FORMAT_GSM610: aftd->dwFormatTagIndex = 1; break;
233         default: return ACMERR_NOTPOSSIBLE;
234         }
235         break;
236     default:
237         WARN("Unsupported query %08x\n", dwQuery);
238         return MMSYSERR_NOTSUPPORTED;
239     }
240
241     aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
242     switch (aftd->dwFormatTagIndex)
243     {
244     case 0:
245         aftd->dwFormatTag = WAVE_FORMAT_PCM;
246         aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
247         aftd->cStandardFormats = NUM_RATES;
248         lstrcpyW(aftd->szFormatTag, szPcm);
249         break;
250     case 1:
251         aftd->dwFormatTag = WAVE_FORMAT_GSM610;
252         aftd->cbFormatSize = sizeof(GSM610WAVEFORMAT);
253         aftd->cStandardFormats = NUM_RATES;
254         lstrcpyW(aftd->szFormatTag, szGsm);
255         break;
256     }
257     return MMSYSERR_NOERROR;
258 }
259
260 /***********************************************************************
261  *           GSM_FormatDetails
262  *
263  */
264 static  LRESULT GSM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
265 {
266     switch (dwQuery)
267     {
268     case ACM_FORMATDETAILSF_FORMAT:
269         if (!GSM_FormatValidate(afd->pwfx)) return ACMERR_NOTPOSSIBLE;
270         break;
271     case ACM_FORMATDETAILSF_INDEX:
272         afd->pwfx->wFormatTag = afd->dwFormatTag;
273         switch (afd->dwFormatTag)
274         {
275         case WAVE_FORMAT_PCM:
276             if (afd->dwFormatIndex >= NUM_RATES) return ACMERR_NOTPOSSIBLE;
277             afd->pwfx->nChannels = 1;
278             afd->pwfx->nSamplesPerSec = gsm_rates[afd->dwFormatIndex];
279             afd->pwfx->wBitsPerSample = 16;
280             afd->pwfx->nBlockAlign = 2;
281             afd->pwfx->nAvgBytesPerSec = afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
282             break;
283         case WAVE_FORMAT_GSM610:
284             if (afd->dwFormatIndex >= NUM_RATES) return ACMERR_NOTPOSSIBLE;
285             afd->pwfx->nChannels = 1;
286             afd->pwfx->nSamplesPerSec = gsm_rates[afd->dwFormatIndex];
287             afd->pwfx->wBitsPerSample = 0;
288             afd->pwfx->nBlockAlign = 65;
289             afd->pwfx->nAvgBytesPerSec = afd->pwfx->nSamplesPerSec * 65 / 320;
290             afd->pwfx->cbSize = sizeof(WORD);
291             ((GSM610WAVEFORMAT*)afd->pwfx)->wSamplesPerBlock = 320;
292             break;
293         default:
294             WARN("Unsupported tag %08x\n", afd->dwFormatTag);
295             return MMSYSERR_INVALPARAM;
296         }
297         break;
298     default:
299         WARN("Unsupported query %08x\n", dwQuery);
300         return MMSYSERR_NOTSUPPORTED;
301     }
302     afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
303     afd->szFormat[0] = 0; /* let MSACM format this for us... */
304
305     return MMSYSERR_NOERROR;
306 }
307
308 /***********************************************************************
309  *           GSM_FormatSuggest
310  *
311  */
312 static  LRESULT GSM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
313 {
314     /* some tests ... */
315     if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
316         adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
317         !GSM_FormatValidate(adfs->pwfxSrc)) return ACMERR_NOTPOSSIBLE;
318     /* FIXME: should do those tests against the real size (according to format tag */
319
320     /* If no suggestion for destination, then copy source value */
321     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
322         adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
323     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
324         adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
325
326     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
327     {
328         if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
329             adfs->pwfxDst->wBitsPerSample = 0;
330         else
331             adfs->pwfxDst->wBitsPerSample = 16;
332     }
333     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
334     {
335         switch (adfs->pwfxSrc->wFormatTag)
336         {
337         case WAVE_FORMAT_PCM: adfs->pwfxDst->wFormatTag = WAVE_FORMAT_GSM610; break;
338         case WAVE_FORMAT_GSM610: adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM; break;
339         }
340     }
341
342     /* recompute other values */
343     switch (adfs->pwfxDst->wFormatTag)
344     {
345     case WAVE_FORMAT_PCM:
346         adfs->pwfxDst->nBlockAlign = 2;
347         adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * 2;
348         break;
349     case WAVE_FORMAT_GSM610:
350         if (adfs->pwfxDst->cbSize < sizeof(WORD))
351             return ACMERR_NOTPOSSIBLE;
352         adfs->pwfxDst->nBlockAlign = 65;
353         adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * 65 / 320;
354         ((GSM610WAVEFORMAT*)adfs->pwfxDst)->wSamplesPerBlock = 320;
355         break;
356     default:
357         return ACMERR_NOTPOSSIBLE;
358     }
359
360     /* check if result is ok */
361     if (!GSM_FormatValidate(adfs->pwfxDst)) return ACMERR_NOTPOSSIBLE;
362     return MMSYSERR_NOERROR;
363 }
364
365 /**************************************************************************
366  *                      GSM_DriverProc                  [exported]
367  */
368 LRESULT CALLBACK GSM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
369                                          LPARAM dwParam1, LPARAM dwParam2)
370 {
371     TRACE("(%08lx %p %04x %08lx %08lx);\n",
372           dwDevID, hDriv, wMsg, dwParam1, dwParam2);
373
374     switch (wMsg)
375     {
376     case DRV_LOAD:              return GSM_drvLoad();
377     case DRV_FREE:              return GSM_drvFree();
378     case DRV_OPEN:              return 1;
379     case DRV_CLOSE:             return 1;
380     case DRV_ENABLE:            return 1;
381     case DRV_DISABLE:           return 1;
382     case DRV_QUERYCONFIGURE:    return 1;
383     case DRV_CONFIGURE:         MessageBoxA(0, "GSM 06.10 codec", "Wine Driver", MB_OK); return 1;
384     case DRV_INSTALL:           return DRVCNF_RESTART;
385     case DRV_REMOVE:            return DRVCNF_RESTART;
386
387     case ACMDM_DRIVER_NOTIFY:
388         /* no caching from other ACM drivers is done so far */
389         return MMSYSERR_NOERROR;
390
391     case ACMDM_DRIVER_DETAILS:
392         return GSM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
393
394     case ACMDM_FORMATTAG_DETAILS:
395         return GSM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
396
397     case ACMDM_FORMAT_DETAILS:
398         return GSM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
399
400     case ACMDM_FORMAT_SUGGEST:
401         return GSM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
402
403     case ACMDM_STREAM_OPEN:
404     case ACMDM_STREAM_CLOSE:
405     case ACMDM_STREAM_SIZE:
406     case ACMDM_STREAM_CONVERT:
407         return MMSYSERR_NOTSUPPORTED;
408
409     case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
410     case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
411         /* this converter is not a hardware driver */
412     case ACMDM_FILTERTAG_DETAILS:
413     case ACMDM_FILTER_DETAILS:
414         /* this converter is not a filter */
415     case ACMDM_STREAM_RESET:
416         /* only needed for asynchronous driver... we aren't, so just say it */
417         return MMSYSERR_NOTSUPPORTED;
418     case ACMDM_STREAM_PREPARE:
419     case ACMDM_STREAM_UNPREPARE:
420         /* nothing special to do here... so don't do anything */
421         return MMSYSERR_NOERROR;
422
423     default:
424         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
425     }
426 }