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