Created an ACM MS ADPCM codec.
[wine/wine-kai.git] / dlls / msacm / msadp32 / msadp32.c
blobf0f5b9b782f5f3a41baf0983624f2863fec8cc71
1 /*
2 * MS ADPCM handling
4 * Copyright (C) 2002 Eric Pouech
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.
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.
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
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"
33 /* see http://www.pcisys.net/~melanson/codecs/adpcm.txt for the details */
35 WINE_DEFAULT_DEBUG_CHANNEL(adpcm);
37 /***********************************************************************
38 * ADPCM_drvOpen
40 static DWORD ADPCM_drvOpen(LPCSTR str)
42 return 1;
45 /***********************************************************************
46 * ADPCM_drvClose
48 static DWORD ADPCM_drvClose(DWORD dwDevID)
50 return 1;
53 typedef struct tagAcmAdpcmData
55 void (*convert)(PACMDRVSTREAMINSTANCE adsi,
56 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
57 } AcmAdpcmData;
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
62 typedef struct
64 int nChannels;
65 int nBits;
66 int rate;
67 } Format;
69 static Format PCM_Formats[] =
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},
77 static Format ADPCM_Formats[] =
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},
83 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
84 #define NUM_ADPCM_FORMATS (sizeof(ADPCM_Formats) / sizeof(ADPCM_Formats[0]))
86 static int MS_Delta[] =
88 230, 230, 230, 230, 307, 409, 512, 614,
89 768, 614, 512, 409, 307, 230, 230, 230
93 static ADPCMCOEFSET MSADPCM_CoeffSet[] =
95 {256, 0}, {512, -256}, {0, 0}, {192, 64}, {240, 0}, {460, -208}, {392, -232}
98 /***********************************************************************
99 * ADPCM_GetFormatIndex
101 static DWORD ADPCM_GetFormatIndex(WAVEFORMATEX* wfx)
103 int i, hi;
104 Format* fmts;
106 switch (wfx->wFormatTag)
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;
120 for (i = 0; i < hi; i++)
122 if (wfx->nChannels == fmts[i].nChannels &&
123 wfx->nSamplesPerSec == fmts[i].rate &&
124 wfx->wBitsPerSample == fmts[i].nBits)
125 return i;
128 return 0xFFFFFFFF;
131 static void init_wfx_adpcm(ADPCMWAVEFORMAT* awfx)
133 register WAVEFORMATEX* pwfx = &awfx->wfx;
135 /* we assume wFormatTag, nChannels, nSamplesPerSec and wBitsPerSample
136 * have been initialized... */
138 if (pwfx->wFormatTag != WAVE_FORMAT_ADPCM) {FIXME("wrong FT\n"); return;}
139 if (ADPCM_GetFormatIndex(pwfx) == 0xFFFFFFFF) {FIXME("wrong fmt\n"); return;}
141 switch (pwfx->nSamplesPerSec)
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;
149 pwfx->cbSize = 2 * sizeof(WORD) + 7 * sizeof(ADPCMCOEFSET);
150 /* 7 is the size of the block head (which contains two samples) */
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));
158 /***********************************************************************
159 * R16
161 * Read a 16 bit sample (correctly handles endianess)
163 static inline short R16(const unsigned char* src)
165 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
168 /***********************************************************************
169 * W16
171 * Write a 16 bit sample (correctly handles endianess)
173 static inline void W16(unsigned char* dst, short s)
175 dst[0] = LOBYTE(s);
176 dst[1] = HIBYTE(s);
179 static inline void clamp_sample(int* sample)
181 if (*sample < -32768) *sample = -32768;
182 if (*sample > 32767) *sample = 32767;
185 static inline void process_nibble(unsigned nibble, int* idelta,
186 int* sample1, int* sample2,
187 const ADPCMCOEFSET* coeff)
189 int sample;
190 int snibble;
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);
198 *sample2 = *sample1;
199 *sample1 = sample;
200 *idelta = ((MS_Delta[nibble] * *idelta) / 256);
201 if (*idelta < 16) *idelta = 16;
204 static void cvtSSms16K(PACMDRVSTREAMINSTANCE adsi,
205 const unsigned char* src, LPDWORD nsrc,
206 unsigned char* dst, LPDWORD ndst)
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));
217 *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
218 *ndst = nblock * nsamp_blk * 2 * 2;
220 nsamp_blk -= 2; /* see below for samples from block head */
221 for (; nblock > 0; nblock--)
223 const unsigned char* in_src = src;
225 assert(*src <= 6);
226 coeffL = MSADPCM_CoeffSet[*src++];
227 assert(*src <= 6);
228 coeffR = MSADPCM_CoeffSet[*src++];
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;
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;
243 for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
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;
250 src = in_src + adsi->pwfxSrc->nBlockAlign;
254 static void cvtMMms16K(PACMDRVSTREAMINSTANCE adsi,
255 const unsigned char* src, LPDWORD nsrc,
256 unsigned char* dst, LPDWORD ndst)
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));
266 *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
267 *ndst = nblock * nsamp_blk * 2;
269 nsamp_blk -= 2; /* see below for samples from block head */
270 for (; nblock > 0; nblock--)
272 const unsigned char* in_src = src;
274 assert(*src <= 6);
275 coeff = MSADPCM_CoeffSet[*src++];
277 idelta = R16(src); src += 2;
278 sample1 = R16(src); src += 2;
279 sample2 = R16(src); src += 2;
281 /* store samples from block head */
282 W16(dst, sample2); dst += 2;
283 W16(dst, sample1); dst += 2;
285 for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
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;
292 src = in_src + adsi->pwfxSrc->nBlockAlign;
296 #if 0
297 static void cvtSS16msK(PACMDRVSTREAMINSTANCE adsi,
298 const unsigned char* src, LPDWORD nsrc,
299 unsigned char* dst, LPDWORD ndst)
303 static void cvtMM16msK(PACMDRVSTREAMINSTANCE adsi,
304 const unsigned char* src, LPDWORD nsrc,
305 unsigned char* dst, LPDWORD ndst)
308 #endif
310 /***********************************************************************
311 * ADPCM_DriverDetails
314 static LRESULT ADPCM_DriverDetails(PACMDRIVERDETAILSW add)
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;
336 return MMSYSERR_NOERROR;
339 /***********************************************************************
340 * ADPCM_FormatTagDetails
343 static LRESULT ADPCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
345 static WCHAR szPcm[]={'P','C','M',0};
346 static WCHAR szMsAdPcm[]={'M','S',' ','A','d','P','C','M',0};
348 switch (dwQuery)
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)
356 aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_ADPCM is bigger than PCM */
357 break;
359 /* fall thru */
360 case ACM_FORMATTAGDETAILSF_FORMATTAG:
361 switch (aftd->dwFormatTag)
363 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
364 case WAVE_FORMAT_ADPCM: aftd->dwFormatTagIndex = 1; break;
365 default: return ACMERR_NOTPOSSIBLE;
367 break;
368 default:
369 WARN("Unsupported query %08lx\n", dwQuery);
370 return MMSYSERR_NOTSUPPORTED;
373 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
374 switch (aftd->dwFormatTagIndex)
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;
389 return MMSYSERR_NOERROR;
392 /***********************************************************************
393 * ADPCM_FormatDetails
396 static LRESULT ADPCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
398 switch (dwQuery)
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)
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;
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;
433 break;
434 default:
435 WARN("Unsupported query %08lx\n", dwQuery);
436 return MMSYSERR_NOTSUPPORTED;
438 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
439 afd->szFormat[0] = 0; /* let MSACM format this for us... */
441 return MMSYSERR_NOERROR;
444 /***********************************************************************
445 * ADPCM_FormatSuggest
448 static LRESULT ADPCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
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 */
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;
462 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
464 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
465 adfs->pwfxDst->wBitsPerSample = 4;
466 else
467 adfs->pwfxDst->wBitsPerSample = 16;
469 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
471 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
472 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_ADPCM;
473 else
474 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
477 /* check if result is ok */
478 if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
480 /* recompute other values */
481 switch (adfs->pwfxDst->wFormatTag)
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;
495 return MMSYSERR_NOERROR;
498 /***********************************************************************
499 * ADPCM_Reset
502 static void ADPCM_Reset(PACMDRVSTREAMINSTANCE adsi, AcmAdpcmData* aad)
506 /***********************************************************************
507 * ADPCM_StreamOpen
510 static LRESULT ADPCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
512 AcmAdpcmData* aad;
513 unsigned nspb;
515 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
517 if (ADPCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
518 ADPCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
519 return ACMERR_NOTPOSSIBLE;
521 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData));
522 if (aad == 0) return MMSYSERR_NOMEM;
524 adsi->dwDriver = (DWORD)aad;
526 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
527 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
529 goto theEnd;
531 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
532 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
534 /* resampling or mono <=> stereo not available
535 * ADPCM algo only define 16 bit per sample output
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);
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
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
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;
560 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
561 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
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);
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
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;
589 else goto theEnd;
590 ADPCM_Reset(adsi, aad);
592 return MMSYSERR_NOERROR;
594 theEnd:
595 HeapFree(GetProcessHeap(), 0, aad);
596 adsi->dwDriver = 0L;
597 return MMSYSERR_NOTSUPPORTED;
600 /***********************************************************************
601 * ADPCM_StreamClose
604 static LRESULT ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
606 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
607 return MMSYSERR_NOERROR;
610 /***********************************************************************
611 * ADPCM_round
614 static inline DWORD ADPCM_round(DWORD a, DWORD b, DWORD c)
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;
621 /***********************************************************************
622 * ADPCM_StreamSize
625 static LRESULT ADPCM_StreamSize(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMSIZE adss)
627 switch (adss->fdwSize)
629 case ACM_STREAMSIZEF_DESTINATION:
630 /* cbDstLength => cbSrcLength */
631 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
632 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
634 /* don't take block overhead into account, doesn't matter too much */
635 adss->cbSrcLength = adss->cbDstLength * 4;
637 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
638 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
640 FIXME("misses the block header overhead\n");
641 adss->cbSrcLength = 256 + adss->cbDstLength / 4;
643 else
645 return MMSYSERR_NOTSUPPORTED;
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)
653 FIXME("misses the block header overhead\n");
654 adss->cbDstLength = 256 + adss->cbSrcLength / 4;
656 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
657 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
659 /* don't take block overhead into account, doesn't matter too much */
660 adss->cbDstLength = adss->cbSrcLength * 4;
662 else
664 return MMSYSERR_NOTSUPPORTED;
666 break;
667 default:
668 WARN("Unsupported query %08lx\n", adss->fdwSize);
669 return MMSYSERR_NOTSUPPORTED;
671 return MMSYSERR_NOERROR;
674 /***********************************************************************
675 * ADPCM_StreamConvert
678 static LRESULT ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
680 AcmAdpcmData* aad = (AcmAdpcmData*)adsi->dwDriver;
681 DWORD nsrc = adsh->cbSrcLength;
682 DWORD ndst = adsh->cbDstLength;
684 if (adsh->fdwConvert &
685 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
686 ACM_STREAMCONVERTF_END|
687 ACM_STREAMCONVERTF_START))
689 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
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
696 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
698 ADPCM_Reset(adsi, aad);
701 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
702 adsh->cbSrcLengthUsed = nsrc;
703 adsh->cbDstLengthUsed = ndst;
705 return MMSYSERR_NOERROR;
708 /**************************************************************************
709 * ADPCM_DriverProc [exported]
711 LRESULT CALLBACK ADPCM_DriverProc(DWORD dwDevID, HDRVR hDriv, UINT wMsg,
712 LPARAM dwParam1, LPARAM dwParam2)
714 TRACE("(%08lx %08lx %04x %08lx %08lx);\n",
715 dwDevID, (DWORD)hDriv, wMsg, dwParam1, dwParam2);
717 switch (wMsg)
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;
730 case ACMDM_DRIVER_NOTIFY:
731 /* no caching from other ACM drivers is done so far */
732 return MMSYSERR_NOERROR;
734 case ACMDM_DRIVER_DETAILS:
735 return ADPCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
737 case ACMDM_FORMATTAG_DETAILS:
738 return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
740 case ACMDM_FORMAT_DETAILS:
741 return ADPCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
743 case ACMDM_FORMAT_SUGGEST:
744 return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
746 case ACMDM_STREAM_OPEN:
747 return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
749 case ACMDM_STREAM_CLOSE:
750 return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
752 case ACMDM_STREAM_SIZE:
753 return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
755 case ACMDM_STREAM_CONVERT:
756 return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
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;
772 default:
773 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
775 return 0;