Make sure to always set X focus on the top-level window, not on
[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 = NULL;
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
514     assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
515
516     if (ADPCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
517         ADPCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
518         return ACMERR_NOTPOSSIBLE;
519
520     aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData));
521     if (aad == 0) return MMSYSERR_NOMEM;
522
523     adsi->dwDriver = (DWORD)aad;
524
525     if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
526         adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
527     {
528         goto theEnd;
529     }
530     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
531              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
532     {
533         /* resampling or mono <=> stereo not available
534          * ADPCM algo only define 16 bit per sample output
535          */
536         if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
537             adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
538             adsi->pwfxDst->wBitsPerSample != 16)
539             goto theEnd;
540
541 #if 0
542         {
543             unsigned int nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
544             FIXME("spb=%u\n", nspb);
545
546             /* we check that in a block, after the header, samples are present on
547              * 4-sample packet pattern
548              * we also check that the block alignement is bigger than the expected size
549              */
550             if (((nspb - 1) & 3) != 0) goto theEnd;
551             if ((((nspb - 1) / 2) + 4) * adsi->pwfxSrc->nChannels < adsi->pwfxSrc->nBlockAlign)
552                 goto theEnd;
553         }
554 #endif
555
556         /* adpcm decoding... */
557         if (adsi->pwfxDst->wBitsPerSample == 16 && adsi->pwfxDst->nChannels == 2)
558             aad->convert = cvtSSms16K;
559         if (adsi->pwfxDst->wBitsPerSample == 16 && adsi->pwfxDst->nChannels == 1)
560             aad->convert = cvtMMms16K;
561     }
562     else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
563              adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
564     {
565         if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
566             adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
567             adsi->pwfxSrc->wBitsPerSample != 16)
568             goto theEnd;
569 #if 0
570         nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxDst)->wSamplesPerBlock;
571         FIXME("spb=%u\n", nspb);
572
573         /* we check that in a block, after the header, samples are present on
574          * 4-sample packet pattern
575          * we also check that the block alignement is bigger than the expected size
576          */
577         if (((nspb - 1) & 3) != 0) goto theEnd;
578         if ((((nspb - 1) / 2) + 4) * adsi->pwfxDst->nChannels < adsi->pwfxDst->nBlockAlign)
579             goto theEnd;
580 #endif
581 #if 0
582         /* adpcm coding... */
583         if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 2)
584             aad->convert = cvtSS16msK;
585         if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 1)
586             aad->convert = cvtMM16msK;
587 #endif
588         FIXME("We don't support encoding yet\n");
589         goto theEnd;
590     }
591     else goto theEnd;
592     ADPCM_Reset(adsi, aad);
593
594     return MMSYSERR_NOERROR;
595
596  theEnd:
597     HeapFree(GetProcessHeap(), 0, aad);
598     adsi->dwDriver = 0L;
599     return MMSYSERR_NOTSUPPORTED;
600 }
601
602 /***********************************************************************
603  *           ADPCM_StreamClose
604  *
605  */
606 static  LRESULT ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
607 {
608     HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
609     return MMSYSERR_NOERROR;
610 }
611
612 /***********************************************************************
613  *           ADPCM_round
614  *
615  */
616 static  inline DWORD    ADPCM_round(DWORD a, DWORD b, DWORD c)
617 {
618     assert(a && b && c);
619     /* to be sure, always return an entire number of c... */
620     return ((double)a * (double)b + (double)c - 1) / (double)c;
621 }
622
623 /***********************************************************************
624  *           ADPCM_StreamSize
625  *
626  */
627 static  LRESULT ADPCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
628 {
629     switch (adss->fdwSize)
630     {
631     case ACM_STREAMSIZEF_DESTINATION:
632         /* cbDstLength => cbSrcLength */
633         if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
634             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
635         {
636             /* don't take block overhead into account, doesn't matter too much */
637             adss->cbSrcLength = adss->cbDstLength * 4;
638         }
639         else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
640                  adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
641         {
642             FIXME("misses the block header overhead\n");
643             adss->cbSrcLength = 256 + adss->cbDstLength / 4;
644         }
645         else
646         {
647             return MMSYSERR_NOTSUPPORTED;
648         }
649         break;
650     case ACM_STREAMSIZEF_SOURCE:
651         /* cbSrcLength => cbDstLength */
652         if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
653             adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
654         {
655             FIXME("misses the block header overhead\n");
656             adss->cbDstLength = 256 + adss->cbSrcLength / 4;
657         }
658         else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
659                  adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
660         {
661             /* don't take block overhead into account, doesn't matter too much */
662             adss->cbDstLength = adss->cbSrcLength * 4;
663         }
664         else
665         {
666             return MMSYSERR_NOTSUPPORTED;
667         }
668         break;
669     default:
670         WARN("Unsupported query %08lx\n", adss->fdwSize);
671         return MMSYSERR_NOTSUPPORTED;
672     }
673     return MMSYSERR_NOERROR;
674 }
675
676 /***********************************************************************
677  *           ADPCM_StreamConvert
678  *
679  */
680 static LRESULT ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
681 {
682     AcmAdpcmData*       aad = (AcmAdpcmData*)adsi->dwDriver;
683     DWORD               nsrc = adsh->cbSrcLength;
684     DWORD               ndst = adsh->cbDstLength;
685
686     if (adsh->fdwConvert &
687         ~(ACM_STREAMCONVERTF_BLOCKALIGN|
688           ACM_STREAMCONVERTF_END|
689           ACM_STREAMCONVERTF_START))
690     {
691         FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
692     }
693     /* ACM_STREAMCONVERTF_BLOCKALIGN
694      *  currently all conversions are block aligned, so do nothing for this flag
695      * ACM_STREAMCONVERTF_END
696      *  no pending data, so do nothing for this flag
697      */
698     if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
699     {
700         ADPCM_Reset(adsi, aad);
701     }
702
703     aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
704     adsh->cbSrcLengthUsed = nsrc;
705     adsh->cbDstLengthUsed = ndst;
706
707     return MMSYSERR_NOERROR;
708 }
709
710 /**************************************************************************
711  *                      ADPCM_DriverProc                        [exported]
712  */
713 LRESULT CALLBACK        ADPCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
714                                          LPARAM dwParam1, LPARAM dwParam2)
715 {
716     TRACE("(%08lx %08lx %04x %08lx %08lx);\n",
717           dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
718
719     switch (wMsg)
720     {
721     case DRV_LOAD:              return 1;
722     case DRV_FREE:              return 1;
723     case DRV_OPEN:              return ADPCM_drvOpen((LPSTR)dwParam1);
724     case DRV_CLOSE:             return ADPCM_drvClose(dwDevID);
725     case DRV_ENABLE:            return 1;
726     case DRV_DISABLE:           return 1;
727     case DRV_QUERYCONFIGURE:    return 1;
728     case DRV_CONFIGURE:         MessageBoxA(0, "MSACM MS ADPCM filter !", "Wine Driver", MB_OK); return 1;
729     case DRV_INSTALL:           return DRVCNF_RESTART;
730     case DRV_REMOVE:            return DRVCNF_RESTART;
731
732     case ACMDM_DRIVER_NOTIFY:
733         /* no caching from other ACM drivers is done so far */
734         return MMSYSERR_NOERROR;
735
736     case ACMDM_DRIVER_DETAILS:
737         return ADPCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
738
739     case ACMDM_FORMATTAG_DETAILS:
740         return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
741
742     case ACMDM_FORMAT_DETAILS:
743         return ADPCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
744
745     case ACMDM_FORMAT_SUGGEST:
746         return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
747
748     case ACMDM_STREAM_OPEN:
749         return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
750
751     case ACMDM_STREAM_CLOSE:
752         return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
753
754     case ACMDM_STREAM_SIZE:
755         return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
756
757     case ACMDM_STREAM_CONVERT:
758         return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
759
760     case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
761     case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
762         /* this converter is not a hardware driver */
763     case ACMDM_FILTERTAG_DETAILS:
764     case ACMDM_FILTER_DETAILS:
765         /* this converter is not a filter */
766     case ACMDM_STREAM_RESET:
767         /* only needed for asynchronous driver... we aren't, so just say it */
768         return MMSYSERR_NOTSUPPORTED;
769     case ACMDM_STREAM_PREPARE:
770     case ACMDM_STREAM_UNPREPARE:
771         /* nothing special to do here... so don't do anything */
772         return MMSYSERR_NOERROR;
773
774     default:
775         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
776     }
777     return 0;
778 }