* Sync up to trunk head (r64921).
[reactos.git] / 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 assert(*src <= 6);
255 coeffL = MSADPCM_CoeffSet[*src++];
256 assert(*src <= 6);
257 coeffR = MSADPCM_CoeffSet[*src++];
258
259 ideltaL = R16(src); src += 2;
260 ideltaR = R16(src); src += 2;
261 sample1L = R16(src); src += 2;
262 sample1R = R16(src); src += 2;
263 sample2L = R16(src); src += 2;
264 sample2R = R16(src); src += 2;
265
266 if(adsi->pwfxDst->wBitsPerSample == 8){
267 /* store samples from block head */
268 *dst = C168(sample2L); ++dst;
269 *dst = C168(sample2R); ++dst;
270 *dst = C168(sample1L); ++dst;
271 *dst = C168(sample1R); ++dst;
272
273 for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
274 {
275 process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
276 *dst = C168(sample1L); ++dst;
277 process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
278 *dst = C168(sample1R); ++dst;
279 }
280 }else if(adsi->pwfxDst->wBitsPerSample == 16){
281 /* store samples from block head */
282 W16(dst, sample2L); dst += 2;
283 W16(dst, sample2R); dst += 2;
284 W16(dst, sample1L); dst += 2;
285 W16(dst, sample1R); dst += 2;
286
287 for (nsamp = nsamp_blk; nsamp > 0; nsamp--)
288 {
289 process_nibble(*src >> 4, &ideltaL, &sample1L, &sample2L, &coeffL);
290 W16(dst, sample1L); dst += 2;
291 process_nibble(*src++ & 0x0F, &ideltaR, &sample1R, &sample2R, &coeffR);
292 W16(dst, sample1R); dst += 2;
293 }
294 }
295 src = in_src + adsi->pwfxSrc->nBlockAlign;
296 }
297 }
298
299 static void cvtMMms16K(const ACMDRVSTREAMINSTANCE *adsi,
300 const unsigned char* src, LPDWORD nsrc,
301 unsigned char* dst, LPDWORD ndst)
302 {
303 int idelta;
304 int sample1, sample2;
305 ADPCMCOEFSET coeff;
306 int nsamp;
307 int nsamp_blk = ((ADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
308 DWORD nblock = min(*nsrc / adsi->pwfxSrc->nBlockAlign,
309 *ndst / (nsamp_blk * adsi->pwfxDst->nBlockAlign));
310
311 *nsrc = nblock * adsi->pwfxSrc->nBlockAlign;
312 *ndst = nblock * nsamp_blk * adsi->pwfxDst->nBlockAlign;
313
314 nsamp_blk -= 2; /* see below for samples from block head */
315 for (; nblock > 0; nblock--)
316 {
317 const unsigned char* in_src = src;
318
319 assert(*src <= 6);
320 coeff = MSADPCM_CoeffSet[*src++];
321
322 idelta = R16(src); src += 2;
323 sample1 = R16(src); src += 2;
324 sample2 = R16(src); src += 2;
325
326 /* store samples from block head */
327 if(adsi->pwfxDst->wBitsPerSample == 8){
328 *dst = C168(sample2); ++dst;
329 *dst = C168(sample1); ++dst;
330
331 for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
332 {
333 process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
334 *dst = C168(sample1); ++dst;
335 process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
336 *dst = C168(sample1); ++dst;
337 }
338 }else if(adsi->pwfxDst->wBitsPerSample == 16){
339 W16(dst, sample2); dst += 2;
340 W16(dst, sample1); dst += 2;
341
342 for (nsamp = nsamp_blk; nsamp > 0; nsamp -= 2)
343 {
344 process_nibble(*src >> 4, &idelta, &sample1, &sample2, &coeff);
345 W16(dst, sample1); dst += 2;
346 process_nibble(*src++ & 0x0F, &idelta, &sample1, &sample2, &coeff);
347 W16(dst, sample1); dst += 2;
348 }
349 }
350
351 src = in_src + adsi->pwfxSrc->nBlockAlign;
352 }
353 }
354
355 #if 0
356 static void cvtSS16msK(PACMDRVSTREAMINSTANCE adsi,
357 const unsigned char* src, LPDWORD nsrc,
358 unsigned char* dst, LPDWORD ndst)
359 {
360 }
361
362 static void cvtMM16msK(PACMDRVSTREAMINSTANCE adsi,
363 const unsigned char* src, LPDWORD nsrc,
364 unsigned char* dst, LPDWORD ndst)
365 {
366 }
367 #endif
368
369 /***********************************************************************
370 * ADPCM_DriverDetails
371 *
372 */
373 static LRESULT ADPCM_DriverDetails(PACMDRIVERDETAILSW add)
374 {
375 add->fccType = ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC;
376 add->fccComp = ACMDRIVERDETAILS_FCCCOMP_UNDEFINED;
377 add->wMid = 0xFF;
378 add->wPid = 0x00;
379 add->vdwACM = 0x01000000;
380 add->vdwDriver = 0x01000000;
381 add->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
382 add->cFormatTags = 2; /* PCM, MS ADPCM */
383 add->cFilterTags = 0;
384 add->hicon = NULL;
385 MultiByteToWideChar( CP_ACP, 0, "MS-ADPCM", -1,
386 add->szShortName, sizeof(add->szShortName)/sizeof(WCHAR) );
387 MultiByteToWideChar( CP_ACP, 0, "Wine MS ADPCM converter", -1,
388 add->szLongName, sizeof(add->szLongName)/sizeof(WCHAR) );
389 MultiByteToWideChar( CP_ACP, 0, "Brought to you by the Wine team...", -1,
390 add->szCopyright, sizeof(add->szCopyright)/sizeof(WCHAR) );
391 MultiByteToWideChar( CP_ACP, 0, "Refer to LICENSE file", -1,
392 add->szLicensing, sizeof(add->szLicensing)/sizeof(WCHAR) );
393 add->szFeatures[0] = 0;
394
395 return MMSYSERR_NOERROR;
396 }
397
398 /***********************************************************************
399 * ADPCM_FormatTagDetails
400 *
401 */
402 static LRESULT ADPCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd, DWORD dwQuery)
403 {
404 static const WCHAR szPcm[]={'P','C','M',0};
405 static const WCHAR szMsAdPcm[]={'M','i','c','r','o','s','o','f','t',' ','A','D','P','C','M',0};
406
407 switch (dwQuery)
408 {
409 case ACM_FORMATTAGDETAILSF_INDEX:
410 if (aftd->dwFormatTagIndex >= 2) return ACMERR_NOTPOSSIBLE;
411 break;
412 case ACM_FORMATTAGDETAILSF_LARGESTSIZE:
413 if (aftd->dwFormatTag == WAVE_FORMAT_UNKNOWN)
414 {
415 aftd->dwFormatTagIndex = 1; /* WAVE_FORMAT_ADPCM is bigger than PCM */
416 break;
417 }
418 /* fall through */
419 case ACM_FORMATTAGDETAILSF_FORMATTAG:
420 switch (aftd->dwFormatTag)
421 {
422 case WAVE_FORMAT_PCM: aftd->dwFormatTagIndex = 0; break;
423 case WAVE_FORMAT_ADPCM: aftd->dwFormatTagIndex = 1; break;
424 default: return ACMERR_NOTPOSSIBLE;
425 }
426 break;
427 default:
428 WARN("Unsupported query %08x\n", dwQuery);
429 return MMSYSERR_NOTSUPPORTED;
430 }
431
432 aftd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
433 switch (aftd->dwFormatTagIndex)
434 {
435 case 0:
436 aftd->dwFormatTag = WAVE_FORMAT_PCM;
437 aftd->cbFormatSize = sizeof(PCMWAVEFORMAT);
438 aftd->cStandardFormats = NUM_PCM_FORMATS;
439 lstrcpyW(aftd->szFormatTag, szPcm);
440 break;
441 case 1:
442 aftd->dwFormatTag = WAVE_FORMAT_ADPCM;
443 aftd->cbFormatSize = sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET);
444 aftd->cStandardFormats = NUM_ADPCM_FORMATS;
445 lstrcpyW(aftd->szFormatTag, szMsAdPcm);
446 break;
447 }
448 return MMSYSERR_NOERROR;
449 }
450
451 /***********************************************************************
452 * ADPCM_FormatDetails
453 *
454 */
455 static LRESULT ADPCM_FormatDetails(PACMFORMATDETAILSW afd, DWORD dwQuery)
456 {
457 switch (dwQuery)
458 {
459 case ACM_FORMATDETAILSF_FORMAT:
460 if (ADPCM_GetFormatIndex(afd->pwfx) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
461 break;
462 case ACM_FORMATDETAILSF_INDEX:
463 afd->pwfx->wFormatTag = afd->dwFormatTag;
464 switch (afd->dwFormatTag)
465 {
466 case WAVE_FORMAT_PCM:
467 if (afd->dwFormatIndex >= NUM_PCM_FORMATS) return ACMERR_NOTPOSSIBLE;
468 afd->pwfx->nChannels = PCM_Formats[afd->dwFormatIndex].nChannels;
469 afd->pwfx->nSamplesPerSec = PCM_Formats[afd->dwFormatIndex].rate;
470 afd->pwfx->wBitsPerSample = PCM_Formats[afd->dwFormatIndex].nBits;
471 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
472 * afd->pwfx->cbSize = 0;
473 */
474 afd->pwfx->nBlockAlign =
475 (afd->pwfx->nChannels * afd->pwfx->wBitsPerSample) / 8;
476 afd->pwfx->nAvgBytesPerSec =
477 afd->pwfx->nSamplesPerSec * afd->pwfx->nBlockAlign;
478 break;
479 case WAVE_FORMAT_ADPCM:
480 if (afd->dwFormatIndex >= NUM_ADPCM_FORMATS) return ACMERR_NOTPOSSIBLE;
481 if (afd->cbwfx < sizeof(ADPCMWAVEFORMAT) + (7 - 1) * sizeof(ADPCMCOEFSET))
482 return ACMERR_NOTPOSSIBLE;
483 afd->pwfx->nChannels = ADPCM_Formats[afd->dwFormatIndex].nChannels;
484 afd->pwfx->nSamplesPerSec = ADPCM_Formats[afd->dwFormatIndex].rate;
485 afd->pwfx->wBitsPerSample = ADPCM_Formats[afd->dwFormatIndex].nBits;
486 init_wfx_adpcm((ADPCMWAVEFORMAT*)afd->pwfx);
487 break;
488 default:
489 WARN("Unsupported tag %08x\n", afd->dwFormatTag);
490 return MMSYSERR_INVALPARAM;
491 }
492 break;
493 default:
494 WARN("Unsupported query %08x\n", dwQuery);
495 return MMSYSERR_NOTSUPPORTED;
496 }
497 afd->fdwSupport = ACMDRIVERDETAILS_SUPPORTF_CODEC;
498 afd->szFormat[0] = 0; /* let MSACM format this for us... */
499
500 return MMSYSERR_NOERROR;
501 }
502
503 /***********************************************************************
504 * ADPCM_FormatSuggest
505 *
506 */
507 static LRESULT ADPCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs)
508 {
509 /* some tests ... */
510 if (adfs->cbwfxSrc < sizeof(PCMWAVEFORMAT) ||
511 adfs->cbwfxDst < sizeof(PCMWAVEFORMAT) ||
512 adfs->pwfxSrc->wFormatTag == adfs->pwfxDst->wFormatTag ||
513 ADPCM_GetFormatIndex(adfs->pwfxSrc) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
514 /* FIXME: should do those tests against the real size (according to format tag */
515
516 /* If no suggestion for destination, then copy source value */
517 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NCHANNELS))
518 adfs->pwfxDst->nChannels = adfs->pwfxSrc->nChannels;
519 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_NSAMPLESPERSEC))
520 adfs->pwfxDst->nSamplesPerSec = adfs->pwfxSrc->nSamplesPerSec;
521
522 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WBITSPERSAMPLE))
523 {
524 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
525 adfs->pwfxDst->wBitsPerSample = 4;
526 else
527 adfs->pwfxDst->wBitsPerSample = 16;
528 }
529 if (!(adfs->fdwSuggest & ACM_FORMATSUGGESTF_WFORMATTAG))
530 {
531 if (adfs->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM)
532 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_ADPCM;
533 else
534 adfs->pwfxDst->wFormatTag = WAVE_FORMAT_PCM;
535 }
536
537 /* recompute other values */
538 switch (adfs->pwfxDst->wFormatTag)
539 {
540 case WAVE_FORMAT_PCM:
541 adfs->pwfxDst->nBlockAlign = (adfs->pwfxDst->nChannels * adfs->pwfxDst->wBitsPerSample) / 8;
542 adfs->pwfxDst->nAvgBytesPerSec = adfs->pwfxDst->nSamplesPerSec * adfs->pwfxDst->nBlockAlign;
543 /* check if result is ok */
544 if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
545 break;
546 case WAVE_FORMAT_ADPCM:
547 init_wfx_adpcm((ADPCMWAVEFORMAT*)adfs->pwfxDst);
548 /* check if result is ok */
549 if (ADPCM_GetFormatIndex(adfs->pwfxDst) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE;
550 break;
551 default:
552 return ACMERR_NOTPOSSIBLE;
553 }
554
555 return MMSYSERR_NOERROR;
556 }
557
558 /***********************************************************************
559 * ADPCM_Reset
560 *
561 */
562 static void ADPCM_Reset(PACMDRVSTREAMINSTANCE adsi, AcmAdpcmData* aad)
563 {
564 }
565
566 /***********************************************************************
567 * ADPCM_StreamOpen
568 *
569 */
570 static LRESULT ADPCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi)
571 {
572 AcmAdpcmData* aad;
573
574 assert(!(adsi->fdwOpen & ACM_STREAMOPENF_ASYNC));
575
576 if (ADPCM_GetFormatIndex(adsi->pwfxSrc) == 0xFFFFFFFF ||
577 ADPCM_GetFormatIndex(adsi->pwfxDst) == 0xFFFFFFFF)
578 return ACMERR_NOTPOSSIBLE;
579
580 aad = HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData));
581 if (aad == 0) return MMSYSERR_NOMEM;
582
583 adsi->dwDriver = (DWORD_PTR)aad;
584
585 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
586 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
587 {
588 goto theEnd;
589 }
590 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
591 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
592 {
593 /* resampling or mono <=> stereo not available */
594 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
595 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels)
596 goto theEnd;
597
598 #if 0
599 {
600 unsigned int nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxSrc)->wSamplesPerBlock;
601 FIXME("spb=%u\n", nspb);
602
603 /* we check that in a block, after the header, samples are present on
604 * 4-sample packet pattern
605 * we also check that the block alignment is bigger than the expected size
606 */
607 if (((nspb - 1) & 3) != 0) goto theEnd;
608 if ((((nspb - 1) / 2) + 4) * adsi->pwfxSrc->nChannels < adsi->pwfxSrc->nBlockAlign)
609 goto theEnd;
610 }
611 #endif
612
613 /* adpcm decoding... */
614 if (adsi->pwfxDst->nChannels == 2)
615 aad->convert = cvtSSms16K;
616 else if (adsi->pwfxDst->nChannels == 1)
617 aad->convert = cvtMMms16K;
618 }
619 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
620 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
621 {
622 if (adsi->pwfxSrc->nSamplesPerSec != adsi->pwfxDst->nSamplesPerSec ||
623 adsi->pwfxSrc->nChannels != adsi->pwfxDst->nChannels ||
624 adsi->pwfxSrc->wBitsPerSample != 16)
625 goto theEnd;
626 #if 0
627 nspb = ((IMAADPCMWAVEFORMAT*)adsi->pwfxDst)->wSamplesPerBlock;
628 FIXME("spb=%u\n", nspb);
629
630 /* we check that in a block, after the header, samples are present on
631 * 4-sample packet pattern
632 * we also check that the block alignment is bigger than the expected size
633 */
634 if (((nspb - 1) & 3) != 0) goto theEnd;
635 if ((((nspb - 1) / 2) + 4) * adsi->pwfxDst->nChannels < adsi->pwfxDst->nBlockAlign)
636 goto theEnd;
637 #endif
638 #if 0
639 /* adpcm coding... */
640 if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 2)
641 aad->convert = cvtSS16msK;
642 if (adsi->pwfxSrc->wBitsPerSample == 16 && adsi->pwfxSrc->nChannels == 1)
643 aad->convert = cvtMM16msK;
644 #endif
645 FIXME("We don't support encoding yet\n");
646 goto theEnd;
647 }
648 else goto theEnd;
649 ADPCM_Reset(adsi, aad);
650
651 return MMSYSERR_NOERROR;
652
653 theEnd:
654 HeapFree(GetProcessHeap(), 0, aad);
655 adsi->dwDriver = 0L;
656 return MMSYSERR_NOTSUPPORTED;
657 }
658
659 /***********************************************************************
660 * ADPCM_StreamClose
661 *
662 */
663 static LRESULT ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi)
664 {
665 HeapFree(GetProcessHeap(), 0, (void*)adsi->dwDriver);
666 return MMSYSERR_NOERROR;
667 }
668
669 /***********************************************************************
670 * ADPCM_StreamSize
671 *
672 */
673 static LRESULT ADPCM_StreamSize(const ACMDRVSTREAMINSTANCE *adsi, PACMDRVSTREAMSIZE adss)
674 {
675 DWORD nblocks;
676 WORD wSamplesPerBlock;
677 /* wSamplesPerBlock formula comes from MSDN ADPCMWAVEFORMAT page.*/
678 switch (adss->fdwSize)
679 {
680 case ACM_STREAMSIZEF_DESTINATION:
681 /* cbDstLength => cbSrcLength */
682 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
683 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
684 {
685 wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
686 nblocks = adss->cbDstLength / adsi->pwfxDst->nBlockAlign;
687 if (nblocks == 0)
688 return ACMERR_NOTPOSSIBLE;
689 adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock;
690 }
691 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
692 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
693 {
694 wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
695 nblocks = adss->cbDstLength / (adsi->pwfxDst->nBlockAlign * wSamplesPerBlock);
696 if (nblocks == 0)
697 return ACMERR_NOTPOSSIBLE;
698 adss->cbSrcLength = nblocks * adsi->pwfxSrc->nBlockAlign;
699 }
700 else
701 {
702 return MMSYSERR_NOTSUPPORTED;
703 }
704 break;
705 case ACM_STREAMSIZEF_SOURCE:
706 /* cbSrcLength => cbDstLength */
707 if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_PCM &&
708 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_ADPCM)
709 {
710 wSamplesPerBlock = adsi->pwfxDst->nBlockAlign * 2 / adsi->pwfxDst->nChannels - 12;
711 nblocks = adss->cbSrcLength / (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock);
712 if (nblocks == 0)
713 return ACMERR_NOTPOSSIBLE;
714 if (adss->cbSrcLength % (adsi->pwfxSrc->nBlockAlign * wSamplesPerBlock))
715 /* Round block count up. */
716 nblocks++;
717 adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign;
718 }
719 else if (adsi->pwfxSrc->wFormatTag == WAVE_FORMAT_ADPCM &&
720 adsi->pwfxDst->wFormatTag == WAVE_FORMAT_PCM)
721 {
722 wSamplesPerBlock = adsi->pwfxSrc->nBlockAlign * 2 / adsi->pwfxSrc->nChannels - 12;
723 nblocks = adss->cbSrcLength / adsi->pwfxSrc->nBlockAlign;
724 if (nblocks == 0)
725 return ACMERR_NOTPOSSIBLE;
726 if (adss->cbSrcLength % adsi->pwfxSrc->nBlockAlign)
727 /* Round block count up. */
728 nblocks++;
729 adss->cbDstLength = nblocks * adsi->pwfxDst->nBlockAlign * wSamplesPerBlock;
730 }
731 else
732 {
733 return MMSYSERR_NOTSUPPORTED;
734 }
735 break;
736 default:
737 WARN("Unsupported query %08x\n", adss->fdwSize);
738 return MMSYSERR_NOTSUPPORTED;
739 }
740 return MMSYSERR_NOERROR;
741 }
742
743 /***********************************************************************
744 * ADPCM_StreamConvert
745 *
746 */
747 static LRESULT ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi, PACMDRVSTREAMHEADER adsh)
748 {
749 AcmAdpcmData* aad = (AcmAdpcmData*)adsi->dwDriver;
750 DWORD nsrc = adsh->cbSrcLength;
751 DWORD ndst = adsh->cbDstLength;
752
753 if (adsh->fdwConvert &
754 ~(ACM_STREAMCONVERTF_BLOCKALIGN|
755 ACM_STREAMCONVERTF_END|
756 ACM_STREAMCONVERTF_START))
757 {
758 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh->fdwConvert);
759 }
760 /* ACM_STREAMCONVERTF_BLOCKALIGN
761 * currently all conversions are block aligned, so do nothing for this flag
762 * ACM_STREAMCONVERTF_END
763 * no pending data, so do nothing for this flag
764 */
765 if ((adsh->fdwConvert & ACM_STREAMCONVERTF_START))
766 {
767 ADPCM_Reset(adsi, aad);
768 }
769
770 aad->convert(adsi, adsh->pbSrc, &nsrc, adsh->pbDst, &ndst);
771 adsh->cbSrcLengthUsed = nsrc;
772 adsh->cbDstLengthUsed = ndst;
773
774 return MMSYSERR_NOERROR;
775 }
776
777 /**************************************************************************
778 * ADPCM_DriverProc [exported]
779 */
780 LRESULT CALLBACK ADPCM_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
781 LPARAM dwParam1, LPARAM dwParam2)
782 {
783 TRACE("(%08lx %p %04x %08lx %08lx);\n",
784 dwDevID, hDriv, wMsg, dwParam1, dwParam2);
785
786 switch (wMsg)
787 {
788 case DRV_LOAD: return 1;
789 case DRV_FREE: return 1;
790 case DRV_OPEN: return ADPCM_drvOpen((LPSTR)dwParam1);
791 case DRV_CLOSE: return ADPCM_drvClose(dwDevID);
792 case DRV_ENABLE: return 1;
793 case DRV_DISABLE: return 1;
794 case DRV_QUERYCONFIGURE: return 1;
795 case DRV_CONFIGURE: MessageBoxA(0, "MSACM MS ADPCM filter !", "Wine Driver", MB_OK); return 1;
796 case DRV_INSTALL: return DRVCNF_RESTART;
797 case DRV_REMOVE: return DRVCNF_RESTART;
798
799 case ACMDM_DRIVER_NOTIFY:
800 /* no caching from other ACM drivers is done so far */
801 return MMSYSERR_NOERROR;
802
803 case ACMDM_DRIVER_DETAILS:
804 return ADPCM_DriverDetails((PACMDRIVERDETAILSW)dwParam1);
805
806 case ACMDM_FORMATTAG_DETAILS:
807 return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW)dwParam1, dwParam2);
808
809 case ACMDM_FORMAT_DETAILS:
810 return ADPCM_FormatDetails((PACMFORMATDETAILSW)dwParam1, dwParam2);
811
812 case ACMDM_FORMAT_SUGGEST:
813 return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST)dwParam1);
814
815 case ACMDM_STREAM_OPEN:
816 return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE)dwParam1);
817
818 case ACMDM_STREAM_CLOSE:
819 return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE)dwParam1);
820
821 case ACMDM_STREAM_SIZE:
822 return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMSIZE)dwParam2);
823
824 case ACMDM_STREAM_CONVERT:
825 return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE)dwParam1, (PACMDRVSTREAMHEADER)dwParam2);
826
827 case ACMDM_HARDWARE_WAVE_CAPS_INPUT:
828 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT:
829 /* this converter is not a hardware driver */
830 case ACMDM_FILTERTAG_DETAILS:
831 case ACMDM_FILTER_DETAILS:
832 /* this converter is not a filter */
833 case ACMDM_STREAM_RESET:
834 /* only needed for asynchronous driver... we aren't, so just say it */
835 return MMSYSERR_NOTSUPPORTED;
836 case ACMDM_STREAM_PREPARE:
837 case ACMDM_STREAM_UNPREPARE:
838 /* nothing special to do here... so don't do anything */
839 return MMSYSERR_NOERROR;
840
841 default:
842 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
843 }
844 }