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