msadp32.acm: Changed pointer cast from DWORD to DWORD_PTR.
[wine] / dlls / msadp32.acm / msadp32.c
1 /*
2  * MS ADPCM handling
3  *
4  *      Copyright (C) 2002              Eric Pouech
5  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <assert.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "mmsystem.h"
31 #include "mmreg.h"
32 #include "msacm.h"
33 #include "msacmdrv.h"
34 #include "wine/debug.h"
35
36 /* see http://www.pcisys.net/~melanson/codecs/adpcm.txt for the details */
37
38 WINE_DEFAULT_DEBUG_CHANNEL(adpcm);
39
40 /***********************************************************************
41  *           ADPCM_drvOpen
42  */
43 static LRESULT ADPCM_drvOpen(LPCSTR str)
44 {
45     return 1;
46 }
47
48 /***********************************************************************
49  *           ADPCM_drvClose
50  */
51 static LRESULT ADPCM_drvClose(DWORD_PTR dwDevID)
52 {
53     return 1;
54 }
55
56 typedef struct tagAcmAdpcmData
57 {
58     void (*convert)(const ACMDRVSTREAMINSTANCE *adsi,
59                     const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
60 } AcmAdpcmData;
61
62 /* table to list all supported formats... those are the basic ones. this
63  * also helps given a unique index to each of the supported formats
64  */
65 typedef struct
66 {
67     int         nChannels;
68     int         nBits;
69     int         rate;
70 } Format;
71
72 static const Format PCM_Formats[] =
73 {
74     {1,  8,  8000}, {2,  8,  8000}, {1, 16,  8000}, {2, 16,  8000},
75     {1,  8, 11025}, {2,  8, 11025}, {1, 16, 11025}, {2, 16, 11025},
76     {1,  8, 22050}, {2,  8, 22050}, {1, 16, 22050}, {2, 16, 22050},
77     {1,  8, 44100}, {2,  8, 44100}, {1, 16, 44100}, {2, 16, 44100},
78 };
79
80 static const Format ADPCM_Formats[] =
81 {
82     {1,  4,  8000}, {2, 4,  8000},  {1,  4, 11025}, {2,  4, 11025},
83     {1,  4, 22050}, {2, 4, 22050},  {1,  4, 44100}, {2,  4, 44100},
84 };
85
86 #define NUM_PCM_FORMATS         (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
87 #define NUM_ADPCM_FORMATS       (sizeof(ADPCM_Formats) / sizeof(ADPCM_Formats[0]))
88
89 static int MS_Delta[] =
90 {
91     230, 230, 230, 230, 307, 409, 512, 614,
92     768, 614, 512, 409, 307, 230, 230, 230
93 };
94
95
96 static ADPCMCOEFSET MSADPCM_CoeffSet[] =
97 {
98     {256, 0}, {512, -256}, {0, 0}, {192, 64}, {240, 0}, {460, -208}, {392, -232}
99 };
100
101 /***********************************************************************
102  *           ADPCM_GetFormatIndex
103  */
104 static  DWORD   ADPCM_GetFormatIndex(const WAVEFORMATEX* wfx)
105 {
106     int             i, hi;
107     const Format*   fmts;
108
109     switch (wfx->wFormatTag)
110     {
111     case WAVE_FORMAT_PCM:
112         hi = NUM_PCM_FORMATS;
113         fmts = PCM_Formats;
114         break;
115     case WAVE_FORMAT_ADPCM:
116         hi = NUM_ADPCM_FORMATS;
117         fmts = ADPCM_Formats;
118         break;
119     default:
120         return 0xFFFFFFFF;
121     }
122
123     for (i = 0; i < hi; i++)
124     {
125         if (wfx->nChannels == fmts[i].nChannels &&
126             wfx->nSamplesPerSec == fmts[i].rate &&
127             wfx->wBitsPerSample == fmts[i].nBits)
128             return i;
129     }
130
131     switch (wfx->wFormatTag)
132     {
133     case WAVE_FORMAT_PCM:
134         if(3 > wfx->nChannels &&
135            wfx->nChannels > 0 &&
136            wfx->nAvgBytesPerSec == 2 * wfx->nSamplesPerSec * wfx->nChannels &&
137            wfx->nBlockAlign == 2 * wfx->nChannels &&
138            wfx->wBitsPerSample == 16)
139            return hi;
140         break;
141     case WAVE_FORMAT_ADPCM:
142         if(3 > wfx->nChannels &&
143            wfx->nChannels > 0 &&
144            wfx->wBitsPerSample == 4 &&
145            wfx->cbSize == 32)
146            return hi;
147         break;
148     }
149
150     return 0xFFFFFFFF;
151 }
152
153 static void     init_wfx_adpcm(ADPCMWAVEFORMAT* awfx)
154 {
155     register WAVEFORMATEX*      pwfx = &awfx->wfx;
156
157     /* we assume wFormatTag, nChannels, nSamplesPerSec and wBitsPerSample
158      * have been initialized... */
159
160     if (pwfx->wFormatTag != WAVE_FORMAT_ADPCM) {FIXME("wrong FT\n"); return;}
161     if (ADPCM_GetFormatIndex(pwfx) == 0xFFFFFFFF) {FIXME("wrong fmt\n"); return;}
162
163     switch (pwfx->nSamplesPerSec)
164     {
165     case  8000: pwfx->nBlockAlign = 256 * pwfx->nChannels;   break;
166     case 11025: pwfx->nBlockAlign = 256 * pwfx->nChannels;   break;
167     case 22050: pwfx->nBlockAlign = 512 * pwfx->nChannels;   break;
168     case 44100: pwfx->nBlockAlign = 1024 * pwfx->nChannels;  break;
169     default:                               break;
170     }
171     pwfx->cbSize = 2 * sizeof(WORD) + 7 * sizeof(ADPCMCOEFSET);
172     /* 7 is the size of the block head (which contains two samples) */
173
174     awfx->wSamplesPerBlock = pwfx->nBlockAlign * 2 / pwfx->nChannels - 12;
175     pwfx->nAvgBytesPerSec = (pwfx->nSamplesPerSec * pwfx->nBlockAlign) / awfx->wSamplesPerBlock;
176     awfx->wNumCoef = 7;
177     memcpy(awfx->aCoef, MSADPCM_CoeffSet, 7 * sizeof(ADPCMCOEFSET));
178 }
179
180 /***********************************************************************
181  *           R16
182  *
183  * Read a 16 bit sample (correctly handles endianess)
184  */
185 static inline short  R16(const unsigned char* src)
186 {
187     return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
188 }
189
190 /***********************************************************************
191  *           W16
192  *
193  * Write a 16 bit sample (correctly handles endianess)
194  */
195 static inline void  W16(unsigned char* dst, short s)
196 {
197     dst[0] = LOBYTE(s);
198     dst[1] = HIBYTE(s);
199 }
200
201 static inline void clamp_sample(int* sample)
202 {
203     if (*sample < -32768) *sample = -32768;
204     if (*sample >  32767) *sample =  32767;
205 }
206
207 static inline void process_nibble(unsigned nibble, int* idelta,
208                                   int* sample1, int* sample2,
209                                   const ADPCMCOEFSET* coeff)
210 {
211     int sample;
212     int snibble;
213
214     /* nibble is in fact a signed 4 bit integer => propagate sign if needed */
215     snibble = (nibble & 0x08) ? (nibble - 16) : nibble;
216     sample = ((*sample1 * coeff->iCoef1) + (*sample2 * coeff->iCoef2)) / 256 +
217         snibble * *idelta;
218     clamp_sample(&sample);
219
220     *sample2 = *sample1;
221     *sample1 = sample;
222     *idelta = ((MS_Delta[nibble] * *idelta) / 256);
223     if (*idelta < 16) *idelta = 16;
224 }
225
226 static  void cvtSSms16K(const ACMDRVSTREAMINSTANCE *adsi,
227                         const unsigned char* src, LPDWORD nsrc,
228                         unsigned char* dst, LPDWORD ndst)
229 {
230     int                 ideltaL, ideltaR;
231     int                 sample1L, sample2L;
232     int                 sample1R, sample2R;
233     ADPCMCOEFSET        coeffL, coeffR;
234     int                 nsamp;
235     int                 nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
236     DWORD               nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
237                                      *ndst / (nsamp_blk * 2 * 2));
238
239     *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
240     *ndst = nblock * nsamp_blk * 2 * 2;
241
242     nsamp_blk -= 2; /* see below for samples from block head */
243     for (; nblock > 0; nblock--)
244     {
245         const unsigned char*    in_src = src;
246
247         assert(*src <= 6);
248         coeffL = MSADPCM_CoeffSet[*src++];
249         assert(*src <= 6);
250         coeffR = MSADPCM_CoeffSet[*src++];
251
252         ideltaL  = R16(src);    src += 2;
253         ideltaR  = R16(src);    src += 2;
254         sample1L = R16(src);    src += 2;
255         sample1R = R16(src);    src += 2;
256         sample2L = R16(src);    src += 2;
257         sample2R = R16(src);    src += 2;
258
259         /* store samples from block head */
260         W16(dst, sample2L);      dst += 2;
261         W16(dst, sample2R);      dst += 2;
262         W16(dst, sample1L);      dst += 2;
263         W16(dst, sample1R);      dst += 2;
264
265         for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
266         {
267             process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
268             W16(dst, sample1L); dst += 2;
269             process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
270             W16(dst, sample1R); dst += 2;
271         }
272         src = in_src + adsi->pwfxSrc->nBlockAlign;
273     }
274 }
275
276 static  void cvtMMms16K(const ACMDRVSTREAMINSTANCE *adsi,
277                         const unsigned char* src, LPDWORD nsrc,
278                         unsigned char* dst, LPDWORD ndst)
279 {
280     int                 idelta;
281     int                 sample1, sample2;
282     ADPCMCOEFSET        coeff;
283     int                 nsamp;
284     int                 nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
285     DWORD               nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
286                                      *ndst / (nsamp_blk * 2));
287
288     *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
289     *ndst = nblock * nsamp_blk * 2;
290
291     nsamp_blk -= 2; /* see below for samples from block head */
292     for (; nblock > 0; nblock--)
293     {
294         const unsigned char*    in_src = src;
295
296         assert(*src <= 6);
297         coeff = MSADPCM_CoeffSet[*src++];
298
299         idelta =  R16(src);     src += 2;
300         sample1 = R16(src);     src += 2;
301         sample2 = R16(src);     src += 2;
302
303         /* store samples from block head */
304         W16(dst, sample2);      dst += 2;
305         W16(dst, sample1);      dst += 2;
306
307         for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
308         {
309             process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
310             W16(dst, sample1); dst += 2;
311             process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
312             W16(dst, sample1); dst += 2;
313         }
314         src = in_src + adsi->pwfxSrc->nBlockAlign;
315     }
316 }
317
318 #if 0
319 static  void cvtSS16msK(PACMDRVSTREAMINSTANCE adsi,
320                         const unsigned char* src, LPDWORD nsrc,
321                         unsigned char* dst, LPDWORD ndst)
322 {
323 }
324
325 static  void cvtMM16msK(PACMDRVSTREAMINSTANCE adsi,
326                         const unsigned char* src, LPDWORD nsrc,
327                         unsigned char* dst, LPDWORD ndst)
328 {
329 }
330 #endif
331
332 /***********************************************************************
333  *           ADPCM_DriverDetails
334  *
335  */
336 static  LRESULT ADPCM_DriverDetails(PACMDRIVERDETAILSW add)
337 {
338     add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
339     add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
340     add->wMid = 0xFF;
341     add->wPid = 0x00;
342     add->vdwACM = 0x01000000;
343     add->vdwDriver = 0x01000000;
344     add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
345     add->cFormatTags = 2; /* PCM, MS ADPCM */
346     add->cFilterTags = 0;
347     add->hicon = NULL;
348     MultiByteToWideChar( CP_ACP, 0, "MS-ADPCM", -1,
349                          add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
350     MultiByteToWideChar( CP_ACP, 0, "Wine MS ADPCM converter", -1,
351                          add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
352     MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
353                          add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
354     MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
355                          add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
356     add->szFeatures[0] = 0;
357
358     return MMSYSERR_NOERROR;
359 }
360
361 /***********************************************************************
362  *           ADPCM_FormatTagDetails
363  *
364  */
365 static  LRESULT ADPCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
366 {
367     static const WCHAR szPcm[]={'P','C','M',0};
368     static const WCHAR szMsAdPcm[]={'M','i','c','r','o','s','o','f','t',' ','A','D','P','C','M',0};
369
370     switch (dwQuery)
371     {
372     case ACM_FORMATTAGDETAILSF_INDEX:
373         if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
374         break;
375     case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
376         if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
377         {
378             aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_ADPCM is bigger than PCM */
379             break;
380         }
381         /* fall thru */
382     case ACM_FORMATTAGDETAILSF_FORMATTAG:
383         switch (aftd->dwFormatTag)
384         {
385         case WAVE_FORMAT_PCM:   aftd->dwFormatTagIndex = 0; break;
386         case WAVE_FORMAT_ADPCM: aftd->dwFormatTagIndex = 1; break;
387         default:                return ACMERR_NOTPOSSIBLE;
388         }
389         break;
390     default:
391         WARN("Unsupported query %08x\n", dwQuery);
392         return MMSYSERR_NOTSUPPORTED;
393     }
394
395     aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
396     switch (aftd->dwFormatTagIndex)
397     {
398     case 0:
399         aftd->dwFormatTag = WAVE_FORMAT_PCM;
400         aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
401         aftd->cStandardFormats = NUM_PCM_FORMATS;
402         lstrcpyW(aftd->szFormatTag, szPcm);
403         break;
404     case 1:
405         aftd->dwFormatTag = WAVE_FORMAT_ADPCM;
406         aftd->cbFormatSize = sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET);
407         aftd->cStandardFormats = NUM_ADPCM_FORMATS;
408         lstrcpyW(aftd->szFormatTag, szMsAdPcm);
409         break;
410     }
411     return MMSYSERR_NOERROR;
412 }
413
414 /***********************************************************************
415  *           ADPCM_FormatDetails
416  *
417  */
418 static  LRESULT ADPCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
419 {
420     switch (dwQuery)
421     {
422     case ACM_FORMATDETAILSF_FORMAT:
423         if (ADPCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
424         break;
425     case ACM_FORMATDETAILSF_INDEX:
426         afd->pwfx->wFormatTag = afd->dwFormatTag;
427         switch (afd->dwFormatTag)
428         {
429         case WAVE_FORMAT_PCM:
430             if (afd->dwFormatIndex >= NUM_PCM_FORMATS) return ACMERR_NOTPOSSIBLE;
431             afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
432             afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
433             afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
434             /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
435              * afd->pwfx->cbSize = 0;
436              */
437             afd->pwfx->nBlockAlign =
438                 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
439             afd->pwfx->nAvgBytesPerSec =
440                 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
441             break;
442         case WAVE_FORMAT_ADPCM:
443             if (afd->dwFormatIndex >= NUM_ADPCM_FORMATS) return ACMERR_NOTPOSSIBLE;
444             if (afd->cbwfx < sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET))
445                 return ACMERR_NOTPOSSIBLE;
446             afd->pwfx->nChannels = ADPCM_Formats[afd->dwFormatIndex].nChannels;
447             afd->pwfx->nSamplesPerSec = ADPCM_Formats[afd->dwFormatIndex].rate;
448             afd->pwfx->wBitsPerSample = ADPCM_Formats[afd->dwFormatIndex].nBits;
449             init_wfx_adpcm((ADPCMWAVEFORMAT*)afd->pwfx);
450             break;
451         default:
452             WARN("Unsupported tag %08x\n", afd->dwFormatTag);
453             return MMSYSERR_INVALPARAM;
454         }
455         break;
456     default:
457         WARN("Unsupported query %08x\n", dwQuery);
458         return MMSYSERR_NOTSUPPORTED;
459     }
460     afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
461     afd->szFormat[0] = 0; /* let MSACM format this for us... */
462
463     return MMSYSERR_NOERROR;
464 }
465
466 /***********************************************************************
467  *           ADPCM_FormatSuggest
468  *
469  */
470 static  LRESULT ADPCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
471 {
472     /* some tests ... */
473     if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
474         adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
475         adfs->pwfxSrc->wFormatTag == adfs->pwfxDst->wFormatTag ||
476         ADPCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
477     /* FIXME: should do those tests against the real size (according to format tag */
478
479     /* If no suggestion for destination, then copy source value */
480     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
481         adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
482     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
483         adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
484
485     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
486     {
487         if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
488             adfs->pwfxDst->wBitsPerSample = 4;
489         else
490             adfs->pwfxDst->wBitsPerSample = 16;
491     }
492     if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
493     {
494         if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
495             adfs->pwfxDst->wFormatTag = WAVE_FORMAT_ADPCM;
496         else
497             adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
498     }
499
500     /* recompute other values */
501     switch (adfs->pwfxDst->wFormatTag)
502     {
503     case WAVE_FORMAT_PCM:
504         adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
505         adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
506         /* check if result is ok */
507         if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
508         break;
509     case WAVE_FORMAT_ADPCM:
510         init_wfx_adpcm((ADPCMWAVEFORMAT*)adfs->pwfxDst);
511         /* check if result is ok */
512         if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
513         break;
514     default:
515         FIXME("\n");
516         break;
517     }
518
519     return MMSYSERR_NOERROR;
520 }
521
522 /***********************************************************************
523  *           ADPCM_Reset
524  *
525  */
526 static  void    ADPCM_Reset(PACMDRVSTREAMINSTANCE adsi, AcmAdpcmData* aad)
527 {
528 }
529
530 /***********************************************************************
531  *           ADPCM_StreamOpen
532  *
533  */
534 static  LRESULT ADPCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
535 {
536     AcmAdpcmData*       aad;
537
538     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
539
540     if (ADPCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
541         ADPCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
542         return ACMERR_NOTPOSSIBLE;
543
544     aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData));
545     if (aad == 0) return MMSYSERR_NOMEM;
546
547     adsi->dwDriver = (DWORD_PTR)aad;
548
549     if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
550         adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
551     {
552         goto theEnd;
553     }
554     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
555              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
556     {
557         /* resampling or mono <=> stereo not available
558          * ADPCM algo only define 16 bit per sample output
559          */
560         if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
561             adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
562             adsi->pwfxDst->wBitsPerSample != 16)
563             goto theEnd;
564
565 #if 0
566         {
567             unsigned int nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
568             FIXME("spb=%u\n", nspb);
569
570             /* we check that in a block, after the header, samples are present on
571              * 4-sample packet pattern
572              * we also check that the block alignment is bigger than the expected size
573              */
574             if (((nspb - 1) & 3) != 0) goto theEnd;
575             if ((((nspb - 1) / 2) + 4) * adsi->pwfxSrc->nChannels < adsi->pwfxSrc->nBlockAlign)
576                 goto theEnd;
577         }
578 #endif
579
580         /* adpcm decoding... */
581         if (adsi->pwfxDst->wBitsPerSample == 16 && adsi->pwfxDst->nChannels == 2)
582             aad->convert = cvtSSms16K;
583         if (adsi->pwfxDst->wBitsPerSample == 16 && adsi->pwfxDst->nChannels == 1)
584             aad->convert = cvtMMms16K;
585     }
586     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
587              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
588     {
589         if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
590             adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
591             adsi->pwfxSrc->wBitsPerSample != 16)
592             goto theEnd;
593 #if 0
594         nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxDst)->wSamplesPerBlock;
595         FIXME("spb=%u\n", nspb);
596
597         /* we check that in a block, after the header, samples are present on
598          * 4-sample packet pattern
599          * we also check that the block alignment is bigger than the expected size
600          */
601         if (((nspb - 1) & 3) != 0) goto theEnd;
602         if ((((nspb - 1) / 2) + 4) * adsi->pwfxDst->nChannels < adsi->pwfxDst->nBlockAlign)
603             goto theEnd;
604 #endif
605 #if 0
606         /* adpcm coding... */
607         if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 2)
608             aad->convert = cvtSS16msK;
609         if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 1)
610             aad->convert = cvtMM16msK;
611 #endif
612         FIXME("We don't support encoding yet\n");
613         goto theEnd;
614     }
615     else goto theEnd;
616     ADPCM_Reset(adsi, aad);
617
618     return MMSYSERR_NOERROR;
619
620  theEnd:
621     HeapFree(GetProcessHeap(), 0, aad);
622     adsi->dwDriver = 0L;
623     return MMSYSERR_NOTSUPPORTED;
624 }
625
626 /***********************************************************************
627  *           ADPCM_StreamClose
628  *
629  */
630 static  LRESULT ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
631 {
632     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
633     return MMSYSERR_NOERROR;
634 }
635
636 /***********************************************************************
637  *           ADPCM_StreamSize
638  *
639  */
640 static  LRESULT ADPCM_StreamSize(const ACMDRVSTREAMINSTANCE *adsi, PACMDRVSTREAMSIZE adss)
641 {
642     DWORD nblocks;
643     WORD wSamplesPerBlock;
644     /* wSamplesPerBlock formula comes from MSDN ADPCMWAVEFORMAT page.*/
645     switch (adss->fdwSize)
646     {
647     case ACM_STREAMSIZEF_DESTINATION:
648         /* cbDstLength => cbSrcLength */
649         if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
650             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
651         {
652             wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
653             nblocks = adss->cbDstLength / adsi->pwfxDst->nBlockAlign;
654             if (nblocks == 0)
655                 return ACMERR_NOTPOSSIBLE;
656             adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock;
657         }
658         else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
659                  adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
660         {
661             wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
662             nblocks = adss->cbDstLength / (adsi->pwfxDst->nBlockAlign * wSamplesPerBlock);
663             if (nblocks == 0)
664                 return ACMERR_NOTPOSSIBLE;
665             adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign;
666         }
667         else
668         {
669             return MMSYSERR_NOTSUPPORTED;
670         }
671         break;
672     case ACM_STREAMSIZEF_SOURCE:
673         /* cbSrcLength => cbDstLength */
674         if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
675             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
676         {
677             wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
678             nblocks = adss->cbSrcLength / (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock);
679             if (nblocks == 0)
680                 return ACMERR_NOTPOSSIBLE;
681             if (adss->cbSrcLength % (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock))
682                 /* Round block count up. */
683                 nblocks++;
684             adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign;
685         }
686         else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
687                  adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
688         {
689             wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
690             nblocks = adss->cbSrcLength / adsi->pwfxSrc->nBlockAlign;
691             if (nblocks == 0)
692                 return ACMERR_NOTPOSSIBLE;
693             if (adss->cbSrcLength % adsi->pwfxSrc->nBlockAlign)
694                 /* Round block count up. */
695                 nblocks++;
696             adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign * wSamplesPerBlock;
697         }
698         else
699         {
700             return MMSYSERR_NOTSUPPORTED;
701         }
702         break;
703     default:
704         WARN("Unsupported query %08x\n", adss->fdwSize);
705         return MMSYSERR_NOTSUPPORTED;
706     }
707     return MMSYSERR_NOERROR;
708 }
709
710 /***********************************************************************
711  *           ADPCM_StreamConvert
712  *
713  */
714 static LRESULT ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
715 {
716     AcmAdpcmData*       aad = (AcmAdpcmData*)adsi->dwDriver;
717     DWORD               nsrc = adsh->cbSrcLength;
718     DWORD               ndst = adsh->cbDstLength;
719
720     if (adsh->fdwConvert &
721         ~(ACM_STREAMCONVERTF_BLOCKALIGN|
722           ACM_STREAMCONVERTF_END|
723           ACM_STREAMCONVERTF_START))
724     {
725         FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
726     }
727     /* ACM_STREAMCONVERTF_BLOCKALIGN
728      *  currently all conversions are block aligned, so do nothing for this flag
729      * ACM_STREAMCONVERTF_END
730      *  no pending data, so do nothing for this flag
731      */
732     if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
733     {
734         ADPCM_Reset(adsi, aad);
735     }
736
737     aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
738     adsh->cbSrcLengthUsed = nsrc;
739     adsh->cbDstLengthUsed = ndst;
740
741     return MMSYSERR_NOERROR;
742 }
743
744 /**************************************************************************
745  *                      ADPCM_DriverProc                        [exported]
746  */
747 LRESULT CALLBACK ADPCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
748                                          LPARAM dwParam1, LPARAM dwParam2)
749 {
750     TRACE("(%08lx %p %04x %08lx %08lx);\n",
751           dwDevID, hDriv, wMsg, dwParam1, dwParam2);
752
753     switch (wMsg)
754     {
755     case DRV_LOAD:              return 1;
756     case DRV_FREE:              return 1;
757     case DRV_OPEN:              return ADPCM_drvOpen((LPSTR)dwParam1);
758     case DRV_CLOSE:             return ADPCM_drvClose(dwDevID);
759     case DRV_ENABLE:            return 1;
760     case DRV_DISABLE:           return 1;
761     case DRV_QUERYCONFIGURE:    return 1;
762     case DRV_CONFIGURE:         MessageBoxA(0, "MSACM MS ADPCM filter !", "Wine Driver", MB_OK); return 1;
763     case DRV_INSTALL:           return DRVCNF_RESTART;
764     case DRV_REMOVE:            return DRVCNF_RESTART;
765
766     case ACMDM_DRIVER_NOTIFY:
767         /* no caching from other ACM drivers is done so far */
768         return MMSYSERR_NOERROR;
769
770     case ACMDM_DRIVER_DETAILS:
771         return ADPCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
772
773     case ACMDM_FORMATTAG_DETAILS:
774         return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
775
776     case ACMDM_FORMAT_DETAILS:
777         return ADPCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
778
779     case ACMDM_FORMAT_SUGGEST:
780         return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
781
782     case ACMDM_STREAM_OPEN:
783         return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
784
785     case ACMDM_STREAM_CLOSE:
786         return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
787
788     case ACMDM_STREAM_SIZE:
789         return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
790
791     case ACMDM_STREAM_CONVERT:
792         return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
793
794     case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
795     case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
796         /* this converter is not a hardware driver */
797     case ACMDM_FILTERTAG_DETAILS:
798     case ACMDM_FILTER_DETAILS:
799         /* this converter is not a filter */
800     case ACMDM_STREAM_RESET:
801         /* only needed for asynchronous driver... we aren't, so just say it */
802         return MMSYSERR_NOTSUPPORTED;
803     case ACMDM_STREAM_PREPARE:
804     case ACMDM_STREAM_UNPREPARE:
805         /* nothing special to do here... so don't do anything */
806         return MMSYSERR_NOERROR;
807
808     default:
809         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
810     }
811 }