2 * GSM 06.10 codec handling
3 * Copyright (C) 2009 Maarten Lankhorst
6 * Copyright (C) 2002 Eric Pouech
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.
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.
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
24 #include <wine/port.h>
43 #include "wine/library.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(gsm);
50 static void *libgsm_handle;
51 #define FUNCPTR(f) static typeof(f) * p##f
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; \
65 /***********************************************************************
68 static LRESULT GSM_drvLoad(void)
72 libgsm_handle = wine_dlopen(SONAME_LIBGSM, RTLD_NOW, error, sizeof(error));
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);
84 ERR("Couldn't load " SONAME_LIBGSM ": %s\n", error);
89 /***********************************************************************
92 static LRESULT GSM_drvFree(void)
95 wine_dlclose(libgsm_handle, NULL, 0);
101 static LRESULT GSM_drvLoad(void)
106 static LRESULT GSM_drvFree(void)
113 /***********************************************************************
117 static LRESULT GSM_DriverDetails(PACMDRIVERDETAILSW add)
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;
124 add->vdwACM = 0x3320000;
125 add->vdwDriver = 0x4000000;
126 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
127 add->cFormatTags = 2;
128 add->cFilterTags = 0;
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;
142 /* Validate a WAVEFORMATEX structure */
143 static DWORD GSM_FormatValidate(const WAVEFORMATEX *wfx)
145 if (wfx->nChannels != 1)
148 switch (wfx->wFormatTag)
150 case WAVE_FORMAT_PCM:
151 if (wfx->wBitsPerSample != 16)
153 WARN("PCM wBitsPerSample %u\n", wfx->wBitsPerSample);
156 if (wfx->nBlockAlign != 2)
158 WARN("PCM nBlockAlign %u\n", wfx->nBlockAlign);
161 if (wfx->nAvgBytesPerSec != wfx->nBlockAlign * wfx->nSamplesPerSec)
163 WARN("PCM nAvgBytesPerSec %u/%u\n",
164 wfx->nAvgBytesPerSec,
165 wfx->nBlockAlign * wfx->nSamplesPerSec);
169 case WAVE_FORMAT_GSM610:
170 if (wfx->cbSize < sizeof(WORD))
172 WARN("GSM cbSize %u\n", wfx->cbSize);
175 if (wfx->wBitsPerSample != 0)
177 WARN("GSM wBitsPerSample %u\n", wfx->wBitsPerSample);
180 if (wfx->nBlockAlign != 65)
182 WARN("GSM nBlockAlign %u\n", wfx->nBlockAlign);
185 if (((GSM610WAVEFORMAT*)wfx)->wSamplesPerBlock != 320)
187 WARN("GSM wSamplesPerBlock %u\n",
188 ((GSM610WAVEFORMAT*)wfx)->wSamplesPerBlock);
191 if (wfx->nAvgBytesPerSec != wfx->nSamplesPerSec * 65 / 320)
193 WARN("GSM nAvgBytesPerSec %d / %d\n",
194 wfx->nAvgBytesPerSec, wfx->nSamplesPerSec * 65 / 320);
204 static const DWORD gsm_rates[] = { 8000, 11025, 22050, 44100, 48000, 96000 };
205 #define NUM_RATES (sizeof(gsm_rates)/sizeof(*gsm_rates))
207 /***********************************************************************
208 * GSM_FormatTagDetails
211 static LRESULT GSM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
213 static const WCHAR szPcm[]={'P','C','M',0};
214 static const WCHAR szGsm[]={'G','S','M',' ','6','.','1','0',0};
218 case ACM_FORMATTAGDETAILSF_INDEX:
219 if (aftd->dwFormatTagIndex > 1) return ACMERR_NOTPOSSIBLE;
221 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
222 if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
224 aftd->dwFormatTagIndex = 1;
228 case ACM_FORMATTAGDETAILSF_FORMATTAG:
229 switch (aftd->dwFormatTag)
231 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
232 case WAVE_FORMAT_GSM610: aftd->dwFormatTagIndex = 1; break;
233 default: return ACMERR_NOTPOSSIBLE;
237 WARN("Unsupported query %08x\n", dwQuery);
238 return MMSYSERR_NOTSUPPORTED;
241 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
242 switch (aftd->dwFormatTagIndex)
245 aftd->dwFormatTag = WAVE_FORMAT_PCM;
246 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
247 aftd->cStandardFormats = NUM_RATES;
248 lstrcpyW(aftd->szFormatTag, szPcm);
251 aftd->dwFormatTag = WAVE_FORMAT_GSM610;
252 aftd->cbFormatSize = sizeof(GSM610WAVEFORMAT);
253 aftd->cStandardFormats = NUM_RATES;
254 lstrcpyW(aftd->szFormatTag, szGsm);
257 return MMSYSERR_NOERROR;
260 /***********************************************************************
264 static LRESULT GSM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
268 case ACM_FORMATDETAILSF_FORMAT:
269 if (!GSM_FormatValidate(afd->pwfx)) return ACMERR_NOTPOSSIBLE;
271 case ACM_FORMATDETAILSF_INDEX:
272 afd->pwfx->wFormatTag = afd->dwFormatTag;
273 switch (afd->dwFormatTag)
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;
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;
294 WARN("Unsupported tag %08x\n", afd->dwFormatTag);
295 return MMSYSERR_INVALPARAM;
299 WARN("Unsupported query %08x\n", dwQuery);
300 return MMSYSERR_NOTSUPPORTED;
302 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
303 afd->szFormat[0] = 0; /* let MSACM format this for us... */
305 return MMSYSERR_NOERROR;
308 /***********************************************************************
312 static LRESULT GSM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
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 */
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;
326 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
328 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
329 adfs->pwfxDst->wBitsPerSample = 0;
331 adfs->pwfxDst->wBitsPerSample = 16;
333 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
335 switch (adfs->pwfxSrc->wFormatTag)
337 case WAVE_FORMAT_PCM: adfs->pwfxDst->wFormatTag = WAVE_FORMAT_GSM610; break;
338 case WAVE_FORMAT_GSM610: adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM; break;
342 /* recompute other values */
343 switch (adfs->pwfxDst->wFormatTag)
345 case WAVE_FORMAT_PCM:
346 adfs->pwfxDst->nBlockAlign = 2;
347 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * 2;
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;
357 return ACMERR_NOTPOSSIBLE;
360 /* check if result is ok */
361 if (!GSM_FormatValidate(adfs->pwfxDst)) return ACMERR_NOTPOSSIBLE;
362 return MMSYSERR_NOERROR;
365 /**************************************************************************
366 * GSM_DriverProc [exported]
368 LRESULT CALLBACK GSM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
369 LPARAM dwParam1, LPARAM dwParam2)
371 TRACE("(%08lx %p %04x %08lx %08lx);\n",
372 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
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;
387 case ACMDM_DRIVER_NOTIFY:
388 /* no caching from other ACM drivers is done so far */
389 return MMSYSERR_NOERROR;
391 case ACMDM_DRIVER_DETAILS:
392 return GSM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
394 case ACMDM_FORMATTAG_DETAILS:
395 return GSM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
397 case ACMDM_FORMAT_DETAILS:
398 return GSM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
400 case ACMDM_FORMAT_SUGGEST:
401 return GSM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
403 case ACMDM_STREAM_OPEN:
404 case ACMDM_STREAM_CLOSE:
405 case ACMDM_STREAM_SIZE:
406 case ACMDM_STREAM_CONVERT:
407 return MMSYSERR_NOTSUPPORTED;
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;
424 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);