windows.networking.hostname/tests: Add IHostNameFactory::CreateHostName() tests.
[wine.git] / dlls / msadp32.acm / msadp32.c
blobb31f6371a7a20b807bb71491a747ee1857c9e2ca
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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"
36 /* see http://www.pcisys.net/~melanson/codecs/adpcm.txt for the details */
38 WINE_DEFAULT_DEBUG_CHANNEL(adpcm);
40 /***********************************************************************
41 * ADPCM_drvOpen
43 static LRESULT ADPCM_drvOpen(LPCSTR str)
45 return 1;
48 /***********************************************************************
49 * ADPCM_drvClose
51 static LRESULT ADPCM_drvClose(DWORD_PTR dwDevID)
53 return 1;
56 typedef struct tagAcmAdpcmData
58 void (*convert)(const ACMDRVSTREAMINSTANCE *adsi,
59 const unsigned char*, LPDWORD, unsigned char*, LPDWORD);
60 } AcmAdpcmData;
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
65 typedef struct
67 int nChannels;
68 int nBits;
69 int rate;
70 } Format;
72 static const Format PCM_Formats[] =
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},
80 static const Format ADPCM_Formats[] =
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},
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(const WAVEFORMATEX* wfx)
103 int i, hi;
104 const Format* fmts;
106 switch (wfx->wFormatTag)
108 case WAVE_FORMAT_PCM:
109 hi = ARRAY_SIZE(PCM_Formats);
110 fmts = PCM_Formats;
111 break;
112 case WAVE_FORMAT_ADPCM:
113 hi = ARRAY_SIZE(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 switch (wfx->wFormatTag)
130 case WAVE_FORMAT_PCM:
131 if(3 > wfx->nChannels &&
132 wfx->nChannels > 0 &&
133 wfx->nAvgBytesPerSec == 2 * wfx->nSamplesPerSec * wfx->nChannels &&
134 wfx->nBlockAlign == 2 * wfx->nChannels &&
135 wfx->wBitsPerSample == 16)
136 return hi;
137 break;
138 case WAVE_FORMAT_ADPCM:
139 if(3 > wfx->nChannels &&
140 wfx->nChannels > 0 &&
141 wfx->wBitsPerSample == 4 &&
142 wfx->cbSize == 32)
143 return hi;
144 break;
147 return 0xFFFFFFFF;
150 static void init_wfx_adpcm(ADPCMWAVEFORMAT* awfx)
152 register WAVEFORMATEX* pwfx = &awfx->wfx;
154 /* we assume wFormatTag, nChannels, nSamplesPerSec and wBitsPerSample
155 * have been initialized... */
157 if (pwfx->wFormatTag != WAVE_FORMAT_ADPCM) {FIXME("wrong FT\n"); return;}
158 if (ADPCM_GetFormatIndex(pwfx) == 0xFFFFFFFF) {FIXME("wrong fmt\n"); return;}
160 switch (pwfx->nSamplesPerSec)
162 case 8000: pwfx->nBlockAlign = 256 * pwfx->nChannels; break;
163 case 11025: pwfx->nBlockAlign = 256 * pwfx->nChannels; break;
164 case 22050: pwfx->nBlockAlign = 512 * pwfx->nChannels; break;
165 case 44100: pwfx->nBlockAlign = 1024 * pwfx->nChannels; break;
166 default: break;
168 pwfx->cbSize = 2 * sizeof(WORD) + 7 * sizeof(ADPCMCOEFSET);
169 /* 7 is the size of the block head (which contains two samples) */
171 awfx->wSamplesPerBlock = pwfx->nBlockAlign * 2 / pwfx->nChannels - 12;
172 pwfx->nAvgBytesPerSec = (pwfx->nSamplesPerSec * pwfx->nBlockAlign) / awfx->wSamplesPerBlock;
173 awfx->wNumCoef = 7;
174 memcpy(awfx->aCoef, MSADPCM_CoeffSet, 7 * sizeof(ADPCMCOEFSET));
177 /***********************************************************************
178 * R16
180 * Read a 16 bit sample (correctly handles endianness)
182 static inline short R16(const unsigned char* src)
184 return (short)((unsigned short)src[0] | ((unsigned short)src[1] << 8));
187 /***********************************************************************
188 * W16
190 * Write a 16 bit sample (correctly handles endianness)
192 static inline void W16(unsigned char* dst, short s)
194 dst[0] = LOBYTE(s);
195 dst[1] = HIBYTE(s);
198 static inline void clamp_sample(int* sample)
200 if (*sample < -32768) *sample = -32768;
201 if (*sample > 32767) *sample = 32767;
204 static inline void process_nibble(unsigned nibble, int* idelta,
205 int* sample1, int* sample2,
206 const ADPCMCOEFSET* coeff)
208 int sample;
209 int snibble;
211 /* nibble is in fact a signed 4 bit integer => propagate sign if needed */
212 snibble = (nibble & 0x08) ? (nibble - 16) : nibble;
213 sample = ((*sample1 * coeff->iCoef1) + (*sample2 * coeff->iCoef2)) / 256 +
214 snibble * *idelta;
215 clamp_sample(&sample);
217 *sample2 = *sample1;
218 *sample1 = sample;
219 *idelta = ((MS_Delta[nibble] * *idelta) / 256);
220 if (*idelta < 16) *idelta = 16;
223 static inline unsigned char C168(short s)
225 return HIBYTE(s) ^ (unsigned char)0x80;
228 static void cvtSSms16K(const ACMDRVSTREAMINSTANCE *adsi,
229 const unsigned char* src, LPDWORD nsrc,
230 unsigned char* dst, LPDWORD ndst)
232 int ideltaL, ideltaR;
233 int sample1L, sample2L;
234 int sample1R, sample2R;
235 ADPCMCOEFSET coeffL, coeffR;
236 int nsamp;
237 int nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
238 DWORD nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
239 *ndst / (nsamp_blk * adsi->pwfxDst->nBlockAlign));
241 *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
242 *ndst = nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
244 nsamp_blk -= 2; /* see below for samples from block head */
245 for (; nblock > 0; nblock--)
247 const unsigned char* in_src = src;
249 /* Catch a problem from Tomb Raider III (bug 21000) where it passes
250 * invalid data after a valid sequence of blocks */
251 if (*src > 6 || *(src + 1) > 6)
253 /* Recalculate the amount of used output buffer. We are not changing
254 * nsrc, let's assume the bad data was parsed */
255 *ndst -= nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
256 WARN("Invalid ADPCM data, stopping conversion\n");
257 break;
259 coeffL = MSADPCM_CoeffSet[*src++];
260 coeffR = MSADPCM_CoeffSet[*src++];
262 ideltaL = R16(src); src += 2;
263 ideltaR = R16(src); src += 2;
264 sample1L = R16(src); src += 2;
265 sample1R = R16(src); src += 2;
266 sample2L = R16(src); src += 2;
267 sample2R = R16(src); src += 2;
269 if(adsi->pwfxDst->wBitsPerSample == 8){
270 /* store samples from block head */
271 *dst = C168(sample2L); ++dst;
272 *dst = C168(sample2R); ++dst;
273 *dst = C168(sample1L); ++dst;
274 *dst = C168(sample1R); ++dst;
276 for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
278 process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
279 *dst = C168(sample1L); ++dst;
280 process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
281 *dst = C168(sample1R); ++dst;
283 }else if(adsi->pwfxDst->wBitsPerSample == 16){
284 /* store samples from block head */
285 W16(dst, sample2L); dst += 2;
286 W16(dst, sample2R); dst += 2;
287 W16(dst, sample1L); dst += 2;
288 W16(dst, sample1R); dst += 2;
290 for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
292 process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
293 W16(dst, sample1L); dst += 2;
294 process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
295 W16(dst, sample1R); dst += 2;
298 src = in_src + adsi->pwfxSrc->nBlockAlign;
302 static void cvtMMms16K(const ACMDRVSTREAMINSTANCE *adsi,
303 const unsigned char* src, LPDWORD nsrc,
304 unsigned char* dst, LPDWORD ndst)
306 int idelta;
307 int sample1, sample2;
308 ADPCMCOEFSET coeff;
309 int nsamp;
310 int nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
311 DWORD nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
312 *ndst / (nsamp_blk * adsi->pwfxDst->nBlockAlign));
314 *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
315 *ndst = nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
317 nsamp_blk -= 2; /* see below for samples from block head */
318 for (; nblock > 0; nblock--)
320 const unsigned char* in_src = src;
322 /* Catch a problem from Lord of the Rings War of the Ring where it
323 * passes invalid data. */
324 if (*src > 6)
326 *ndst -= nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
327 WARN("Invalid ADPCM data, stopping conversion\n");
328 break;
330 coeff = MSADPCM_CoeffSet[*src++];
332 idelta = R16(src); src += 2;
333 sample1 = R16(src); src += 2;
334 sample2 = R16(src); src += 2;
336 /* store samples from block head */
337 if(adsi->pwfxDst->wBitsPerSample == 8){
338 *dst = C168(sample2); ++dst;
339 *dst = C168(sample1); ++dst;
341 for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
343 process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
344 *dst = C168(sample1); ++dst;
345 process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
346 *dst = C168(sample1); ++dst;
348 }else if(adsi->pwfxDst->wBitsPerSample == 16){
349 W16(dst, sample2); dst += 2;
350 W16(dst, sample1); dst += 2;
352 for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
354 process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
355 W16(dst, sample1); dst += 2;
356 process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
357 W16(dst, sample1); dst += 2;
361 src = in_src + adsi->pwfxSrc->nBlockAlign;
365 #if 0
366 static void cvtSS16msK(PACMDRVSTREAMINSTANCE adsi,
367 const unsigned char* src, LPDWORD nsrc,
368 unsigned char* dst, LPDWORD ndst)
372 static void cvtMM16msK(PACMDRVSTREAMINSTANCE adsi,
373 const unsigned char* src, LPDWORD nsrc,
374 unsigned char* dst, LPDWORD ndst)
377 #endif
379 /***********************************************************************
380 * ADPCM_DriverDetails
383 static LRESULT ADPCM_DriverDetails(PACMDRIVERDETAILSW add)
385 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
386 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
387 add->wMid = MM_MICROSOFT;
388 add->wPid = MM_MSFT_ACM_MSADPCM;
389 add->vdwACM = 0x01000000;
390 add->vdwDriver = 0x01000000;
391 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
392 add->cFormatTags = 2; /* PCM, MS ADPCM */
393 add->cFilterTags = 0;
394 add->hicon = NULL;
395 MultiByteToWideChar( CP_ACP, 0, "MS-ADPCM", -1,
396 add->szShortName, ARRAY_SIZE( add->szShortName ));
397 MultiByteToWideChar( CP_ACP, 0, "Wine MS ADPCM converter", -1,
398 add->szLongName, ARRAY_SIZE( add->szLongName ));
399 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
400 add->szCopyright, ARRAY_SIZE( add->szCopyright ));
401 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
402 add->szLicensing, ARRAY_SIZE( add->szLicensing ));
403 add->szFeatures[0] = 0;
405 return MMSYSERR_NOERROR;
408 /***********************************************************************
409 * ADPCM_FormatTagDetails
412 static LRESULT ADPCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
414 switch (dwQuery)
416 case ACM_FORMATTAGDETAILSF_INDEX:
417 if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
418 break;
419 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
420 if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
422 aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_ADPCM is bigger than PCM */
423 break;
425 /* fall through */
426 case ACM_FORMATTAGDETAILSF_FORMATTAG:
427 switch (aftd->dwFormatTag)
429 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
430 case WAVE_FORMAT_ADPCM: aftd->dwFormatTagIndex = 1; break;
431 default: return ACMERR_NOTPOSSIBLE;
433 break;
434 default:
435 WARN("Unsupported query %08lx\n", dwQuery);
436 return MMSYSERR_NOTSUPPORTED;
439 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
440 switch (aftd->dwFormatTagIndex)
442 case 0:
443 aftd->dwFormatTag = WAVE_FORMAT_PCM;
444 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
445 aftd->cStandardFormats = ARRAY_SIZE(PCM_Formats);
446 lstrcpyW(aftd->szFormatTag, L"PCM");
447 break;
448 case 1:
449 aftd->dwFormatTag = WAVE_FORMAT_ADPCM;
450 aftd->cbFormatSize = sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET);
451 aftd->cStandardFormats = ARRAY_SIZE(ADPCM_Formats);
452 lstrcpyW(aftd->szFormatTag, L"Microsoft ADPCM");
453 break;
455 return MMSYSERR_NOERROR;
458 /***********************************************************************
459 * ADPCM_FormatDetails
462 static LRESULT ADPCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
464 switch (dwQuery)
466 case ACM_FORMATDETAILSF_FORMAT:
467 if (ADPCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
468 break;
469 case ACM_FORMATDETAILSF_INDEX:
470 afd->pwfx->wFormatTag = afd->dwFormatTag;
471 switch (afd->dwFormatTag)
473 case WAVE_FORMAT_PCM:
474 if (afd->dwFormatIndex >= ARRAY_SIZE(PCM_Formats)) return ACMERR_NOTPOSSIBLE;
475 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
476 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
477 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
478 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
479 * afd->pwfx->cbSize = 0;
481 afd->pwfx->nBlockAlign =
482 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
483 afd->pwfx->nAvgBytesPerSec =
484 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
485 break;
486 case WAVE_FORMAT_ADPCM:
487 if (afd->dwFormatIndex >= ARRAY_SIZE(ADPCM_Formats)) return ACMERR_NOTPOSSIBLE;
488 if (afd->cbwfx < sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET))
489 return ACMERR_NOTPOSSIBLE;
490 afd->pwfx->nChannels = ADPCM_Formats[afd->dwFormatIndex].nChannels;
491 afd->pwfx->nSamplesPerSec = ADPCM_Formats[afd->dwFormatIndex].rate;
492 afd->pwfx->wBitsPerSample = ADPCM_Formats[afd->dwFormatIndex].nBits;
493 init_wfx_adpcm((ADPCMWAVEFORMAT*)afd->pwfx);
494 break;
495 default:
496 WARN("Unsupported tag %08lx\n", afd->dwFormatTag);
497 return MMSYSERR_INVALPARAM;
499 break;
500 default:
501 WARN("Unsupported query %08lx\n", dwQuery);
502 return MMSYSERR_NOTSUPPORTED;
504 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
505 afd->szFormat[0] = 0; /* let MSACM format this for us... */
507 return MMSYSERR_NOERROR;
510 /***********************************************************************
511 * ADPCM_FormatSuggest
514 static LRESULT ADPCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
516 /* some tests ... */
517 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
518 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
519 adfs->pwfxSrc->wFormatTag == adfs->pwfxDst->wFormatTag ||
520 ADPCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
521 /* FIXME: should do those tests against the real size (according to format tag */
523 /* If no suggestion for destination, then copy source value */
524 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
525 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
526 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
527 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
529 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
531 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
532 adfs->pwfxDst->wBitsPerSample = 4;
533 else
534 adfs->pwfxDst->wBitsPerSample = 16;
536 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
538 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
539 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_ADPCM;
540 else
541 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
544 /* recompute other values */
545 switch (adfs->pwfxDst->wFormatTag)
547 case WAVE_FORMAT_PCM:
548 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
549 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
550 /* check if result is ok */
551 if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
552 break;
553 case WAVE_FORMAT_ADPCM:
554 if (adfs->cbwfxDst < sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET))
555 return ACMERR_NOTPOSSIBLE;
556 init_wfx_adpcm((ADPCMWAVEFORMAT*)adfs->pwfxDst);
557 /* check if result is ok */
558 if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
559 break;
560 default:
561 return ACMERR_NOTPOSSIBLE;
564 return MMSYSERR_NOERROR;
567 /***********************************************************************
568 * ADPCM_Reset
571 static void ADPCM_Reset(PACMDRVSTREAMINSTANCE adsi, AcmAdpcmData* aad)
575 /***********************************************************************
576 * ADPCM_StreamOpen
579 static LRESULT ADPCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
581 AcmAdpcmData* aad;
583 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
585 if (ADPCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
586 ADPCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
587 return ACMERR_NOTPOSSIBLE;
589 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData));
590 if (aad == 0) return MMSYSERR_NOMEM;
592 adsi->dwDriver = (DWORD_PTR)aad;
594 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
595 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
597 goto theEnd;
599 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
600 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
602 /* resampling or mono <=> stereo not available */
603 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
604 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels)
605 goto theEnd;
607 #if 0
609 unsigned int nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
610 FIXME("spb=%u\n", nspb);
612 /* we check that in a block, after the header, samples are present on
613 * 4-sample packet pattern
614 * we also check that the block alignment is bigger than the expected size
616 if (((nspb - 1) & 3) != 0) goto theEnd;
617 if ((((nspb - 1) / 2) + 4) * adsi->pwfxSrc->nChannels < adsi->pwfxSrc->nBlockAlign)
618 goto theEnd;
620 #endif
622 /* adpcm decoding... */
623 if (adsi->pwfxDst->nChannels == 2)
624 aad->convert = cvtSSms16K;
625 else if (adsi->pwfxDst->nChannels == 1)
626 aad->convert = cvtMMms16K;
628 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
629 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
631 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
632 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
633 adsi->pwfxSrc->wBitsPerSample != 16)
634 goto theEnd;
635 #if 0
636 nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxDst)->wSamplesPerBlock;
637 FIXME("spb=%u\n", nspb);
639 /* we check that in a block, after the header, samples are present on
640 * 4-sample packet pattern
641 * we also check that the block alignment is bigger than the expected size
643 if (((nspb - 1) & 3) != 0) goto theEnd;
644 if ((((nspb - 1) / 2) + 4) * adsi->pwfxDst->nChannels < adsi->pwfxDst->nBlockAlign)
645 goto theEnd;
646 #endif
647 #if 0
648 /* adpcm coding... */
649 if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 2)
650 aad->convert = cvtSS16msK;
651 if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 1)
652 aad->convert = cvtMM16msK;
653 #endif
654 FIXME("We don't support encoding yet\n");
655 goto theEnd;
657 else goto theEnd;
658 ADPCM_Reset(adsi, aad);
660 return MMSYSERR_NOERROR;
662 theEnd:
663 HeapFree(GetProcessHeap(), 0, aad);
664 adsi->dwDriver = 0L;
665 return MMSYSERR_NOTSUPPORTED;
668 /***********************************************************************
669 * ADPCM_StreamClose
672 static LRESULT ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
674 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
675 return MMSYSERR_NOERROR;
678 /***********************************************************************
679 * ADPCM_StreamSize
682 static LRESULT ADPCM_StreamSize(const ACMDRVSTREAMINSTANCE *adsi, PACMDRVSTREAMSIZE adss)
684 DWORD nblocks;
685 WORD wSamplesPerBlock;
686 /* wSamplesPerBlock formula comes from MSDN ADPCMWAVEFORMAT page.*/
687 switch (adss->fdwSize)
689 case ACM_STREAMSIZEF_DESTINATION:
690 /* cbDstLength => cbSrcLength */
691 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
692 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
694 wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
695 nblocks = adss->cbDstLength / adsi->pwfxDst->nBlockAlign;
696 if (nblocks == 0)
697 return ACMERR_NOTPOSSIBLE;
698 adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock;
700 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
701 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
703 wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
704 nblocks = adss->cbDstLength / (adsi->pwfxDst->nBlockAlign * wSamplesPerBlock);
705 if (nblocks == 0)
706 return ACMERR_NOTPOSSIBLE;
707 adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign;
709 else
711 return MMSYSERR_NOTSUPPORTED;
713 break;
714 case ACM_STREAMSIZEF_SOURCE:
715 /* cbSrcLength => cbDstLength */
716 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
717 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
719 wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
720 nblocks = adss->cbSrcLength / (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock);
721 if (nblocks == 0)
722 return ACMERR_NOTPOSSIBLE;
723 if (adss->cbSrcLength % (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock))
724 /* Round block count up. */
725 nblocks++;
726 adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign;
728 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
729 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
731 wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
732 nblocks = adss->cbSrcLength / adsi->pwfxSrc->nBlockAlign;
733 if (nblocks == 0)
734 return ACMERR_NOTPOSSIBLE;
735 if (adss->cbSrcLength % adsi->pwfxSrc->nBlockAlign)
736 /* Round block count up. */
737 nblocks++;
738 adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign * wSamplesPerBlock;
740 else
742 return MMSYSERR_NOTSUPPORTED;
744 break;
745 default:
746 WARN("Unsupported query %08lx\n", adss->fdwSize);
747 return MMSYSERR_NOTSUPPORTED;
749 return MMSYSERR_NOERROR;
752 /***********************************************************************
753 * ADPCM_StreamConvert
756 static LRESULT ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
758 AcmAdpcmData* aad = (AcmAdpcmData*)adsi->dwDriver;
759 DWORD nsrc = adsh->cbSrcLength;
760 DWORD ndst = adsh->cbDstLength;
762 if (adsh->fdwConvert &
763 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
764 ACM_STREAMCONVERTF_END|
765 ACM_STREAMCONVERTF_START))
767 FIXME("Unsupported fdwConvert (%08lx), ignoring it\n", adsh->fdwConvert);
769 /* ACM_STREAMCONVERTF_BLOCKALIGN
770 * currently all conversions are block aligned, so do nothing for this flag
771 * ACM_STREAMCONVERTF_END
772 * no pending data, so do nothing for this flag
774 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
776 ADPCM_Reset(adsi, aad);
779 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
780 adsh->cbSrcLengthUsed = nsrc;
781 adsh->cbDstLengthUsed = ndst;
783 return MMSYSERR_NOERROR;
786 /**************************************************************************
787 * ADPCM_DriverProc [exported]
789 LRESULT CALLBACK ADPCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
790 LPARAM dwParam1, LPARAM dwParam2)
792 TRACE("(%08Ix %p %04x %08Ix %08Ix);\n",
793 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
795 switch (wMsg)
797 case DRV_LOAD: return 1;
798 case DRV_FREE: return 1;
799 case DRV_OPEN: return ADPCM_drvOpen((LPSTR)dwParam1);
800 case DRV_CLOSE: return ADPCM_drvClose(dwDevID);
801 case DRV_ENABLE: return 1;
802 case DRV_DISABLE: return 1;
803 case DRV_QUERYCONFIGURE: return 1;
804 case DRV_CONFIGURE: MessageBoxA(0, "MSACM MS ADPCM filter !", "Wine Driver", MB_OK); return 1;
805 case DRV_INSTALL: return DRVCNF_RESTART;
806 case DRV_REMOVE: return DRVCNF_RESTART;
808 case ACMDM_DRIVER_NOTIFY:
809 /* no caching from other ACM drivers is done so far */
810 return MMSYSERR_NOERROR;
812 case ACMDM_DRIVER_DETAILS:
813 return ADPCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
815 case ACMDM_FORMATTAG_DETAILS:
816 return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
818 case ACMDM_FORMAT_DETAILS:
819 return ADPCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
821 case ACMDM_FORMAT_SUGGEST:
822 return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
824 case ACMDM_STREAM_OPEN:
825 return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
827 case ACMDM_STREAM_CLOSE:
828 return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
830 case ACMDM_STREAM_SIZE:
831 return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
833 case ACMDM_STREAM_CONVERT:
834 return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
836 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
837 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
838 /* this converter is not a hardware driver */
839 case ACMDM_FILTERTAG_DETAILS:
840 case ACMDM_FILTER_DETAILS:
841 /* this converter is not a filter */
842 case ACMDM_STREAM_RESET:
843 /* only needed for asynchronous driver... we aren't, so just say it */
844 return MMSYSERR_NOTSUPPORTED;
845 case ACMDM_STREAM_PREPARE:
846 case ACMDM_STREAM_UNPREPARE:
847 /* nothing special to do here... so don't do anything */
848 return MMSYSERR_NOERROR;
850 default:
851 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);