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