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
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 /***********************************************************************
43 static LRESULT
ADPCM_drvOpen(LPCSTR str
)
48 /***********************************************************************
51 static LRESULT
ADPCM_drvClose(DWORD_PTR dwDevID
)
56 typedef struct tagAcmAdpcmData
58 void (*convert
)(const ACMDRVSTREAMINSTANCE
*adsi
,
59 const unsigned char*, LPDWORD
, unsigned char*, LPDWORD
);
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
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 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
87 #define NUM_ADPCM_FORMATS (sizeof(ADPCM_Formats) / sizeof(ADPCM_Formats[0]))
89 static int MS_Delta
[] =
91 230, 230, 230, 230, 307, 409, 512, 614,
92 768, 614, 512, 409, 307, 230, 230, 230
96 static ADPCMCOEFSET MSADPCM_CoeffSet
[] =
98 {256, 0}, {512, -256}, {0, 0}, {192, 64}, {240, 0}, {460, -208}, {392, -232}
101 /***********************************************************************
102 * ADPCM_GetFormatIndex
104 static DWORD
ADPCM_GetFormatIndex(const WAVEFORMATEX
* wfx
)
109 switch (wfx
->wFormatTag
)
111 case WAVE_FORMAT_PCM
:
112 hi
= NUM_PCM_FORMATS
;
115 case WAVE_FORMAT_ADPCM
:
116 hi
= NUM_ADPCM_FORMATS
;
117 fmts
= ADPCM_Formats
;
123 for (i
= 0; i
< hi
; i
++)
125 if (wfx
->nChannels
== fmts
[i
].nChannels
&&
126 wfx
->nSamplesPerSec
== fmts
[i
].rate
&&
127 wfx
->wBitsPerSample
== fmts
[i
].nBits
)
131 switch (wfx
->wFormatTag
)
133 case WAVE_FORMAT_PCM
:
134 if(3 > wfx
->nChannels
&&
135 wfx
->nChannels
> 0 &&
136 wfx
->nAvgBytesPerSec
== 2 * wfx
->nSamplesPerSec
* wfx
->nChannels
&&
137 wfx
->nBlockAlign
== 2 * wfx
->nChannels
&&
138 wfx
->wBitsPerSample
== 16)
141 case WAVE_FORMAT_ADPCM
:
142 if(3 > wfx
->nChannels
&&
143 wfx
->nChannels
> 0 &&
144 wfx
->wBitsPerSample
== 4 &&
153 static void init_wfx_adpcm(ADPCMWAVEFORMAT
* awfx
)
155 register WAVEFORMATEX
* pwfx
= &awfx
->wfx
;
157 /* we assume wFormatTag, nChannels, nSamplesPerSec and wBitsPerSample
158 * have been initialized... */
160 if (pwfx
->wFormatTag
!= WAVE_FORMAT_ADPCM
) {FIXME("wrong FT\n"); return;}
161 if (ADPCM_GetFormatIndex(pwfx
) == 0xFFFFFFFF) {FIXME("wrong fmt\n"); return;}
163 switch (pwfx
->nSamplesPerSec
)
165 case 8000: pwfx
->nBlockAlign
= 256 * pwfx
->nChannels
; break;
166 case 11025: pwfx
->nBlockAlign
= 256 * pwfx
->nChannels
; break;
167 case 22050: pwfx
->nBlockAlign
= 512 * pwfx
->nChannels
; break;
168 case 44100: pwfx
->nBlockAlign
= 1024 * pwfx
->nChannels
; break;
171 pwfx
->cbSize
= 2 * sizeof(WORD
) + 7 * sizeof(ADPCMCOEFSET
);
172 /* 7 is the size of the block head (which contains two samples) */
174 awfx
->wSamplesPerBlock
= pwfx
->nBlockAlign
* 2 / pwfx
->nChannels
- 12;
175 pwfx
->nAvgBytesPerSec
= (pwfx
->nSamplesPerSec
* pwfx
->nBlockAlign
) / awfx
->wSamplesPerBlock
;
177 memcpy(awfx
->aCoef
, MSADPCM_CoeffSet
, 7 * sizeof(ADPCMCOEFSET
));
180 /***********************************************************************
183 * Read a 16 bit sample (correctly handles endianness)
185 static inline short R16(const unsigned char* src
)
187 return (short)((unsigned short)src
[0] | ((unsigned short)src
[1] << 8));
190 /***********************************************************************
193 * Write a 16 bit sample (correctly handles endianness)
195 static inline void W16(unsigned char* dst
, short s
)
201 static inline void clamp_sample(int* sample
)
203 if (*sample
< -32768) *sample
= -32768;
204 if (*sample
> 32767) *sample
= 32767;
207 static inline void process_nibble(unsigned nibble
, int* idelta
,
208 int* sample1
, int* sample2
,
209 const ADPCMCOEFSET
* coeff
)
214 /* nibble is in fact a signed 4 bit integer => propagate sign if needed */
215 snibble
= (nibble
& 0x08) ? (nibble
- 16) : nibble
;
216 sample
= ((*sample1
* coeff
->iCoef1
) + (*sample2
* coeff
->iCoef2
)) / 256 +
218 clamp_sample(&sample
);
222 *idelta
= ((MS_Delta
[nibble
] * *idelta
) / 256);
223 if (*idelta
< 16) *idelta
= 16;
226 static inline unsigned char C168(short s
)
228 return HIBYTE(s
) ^ (unsigned char)0x80;
231 static void cvtSSms16K(const ACMDRVSTREAMINSTANCE
*adsi
,
232 const unsigned char* src
, LPDWORD nsrc
,
233 unsigned char* dst
, LPDWORD ndst
)
235 int ideltaL
, ideltaR
;
236 int sample1L
, sample2L
;
237 int sample1R
, sample2R
;
238 ADPCMCOEFSET coeffL
, coeffR
;
240 int nsamp_blk
= ((ADPCMWAVEFORMAT
*)adsi
->pwfxSrc
)->wSamplesPerBlock
;
241 DWORD nblock
= min(*nsrc
/ adsi
->pwfxSrc
->nBlockAlign
,
242 *ndst
/ (nsamp_blk
* adsi
->pwfxDst
->nBlockAlign
));
244 *nsrc
= nblock
* adsi
->pwfxSrc
->nBlockAlign
;
245 *ndst
= nblock
* nsamp_blk
* adsi
->pwfxDst
->nBlockAlign
;
247 nsamp_blk
-= 2; /* see below for samples from block head */
248 for (; nblock
> 0; nblock
--)
250 const unsigned char* in_src
= src
;
253 coeffL
= MSADPCM_CoeffSet
[*src
++];
255 coeffR
= MSADPCM_CoeffSet
[*src
++];
257 ideltaL
= R16(src
); src
+= 2;
258 ideltaR
= R16(src
); src
+= 2;
259 sample1L
= R16(src
); src
+= 2;
260 sample1R
= R16(src
); src
+= 2;
261 sample2L
= R16(src
); src
+= 2;
262 sample2R
= R16(src
); src
+= 2;
264 if(adsi
->pwfxDst
->wBitsPerSample
== 8){
265 /* store samples from block head */
266 *dst
= C168(sample2L
); ++dst
;
267 *dst
= C168(sample2R
); ++dst
;
268 *dst
= C168(sample1L
); ++dst
;
269 *dst
= C168(sample1R
); ++dst
;
271 for (nsamp
= nsamp_blk
; nsamp
> 0; nsamp
--)
273 process_nibble(*src
>> 4, &ideltaL
, &sample1L
, &sample2L
, &coeffL
);
274 *dst
= C168(sample1L
); ++dst
;
275 process_nibble(*src
++ & 0x0F, &ideltaR
, &sample1R
, &sample2R
, &coeffR
);
276 *dst
= C168(sample1R
); ++dst
;
278 }else if(adsi
->pwfxDst
->wBitsPerSample
== 16){
279 /* store samples from block head */
280 W16(dst
, sample2L
); dst
+= 2;
281 W16(dst
, sample2R
); dst
+= 2;
282 W16(dst
, sample1L
); dst
+= 2;
283 W16(dst
, sample1R
); dst
+= 2;
285 for (nsamp
= nsamp_blk
; nsamp
> 0; nsamp
--)
287 process_nibble(*src
>> 4, &ideltaL
, &sample1L
, &sample2L
, &coeffL
);
288 W16(dst
, sample1L
); dst
+= 2;
289 process_nibble(*src
++ & 0x0F, &ideltaR
, &sample1R
, &sample2R
, &coeffR
);
290 W16(dst
, sample1R
); dst
+= 2;
293 src
= in_src
+ adsi
->pwfxSrc
->nBlockAlign
;
297 static void cvtMMms16K(const ACMDRVSTREAMINSTANCE
*adsi
,
298 const unsigned char* src
, LPDWORD nsrc
,
299 unsigned char* dst
, LPDWORD ndst
)
302 int sample1
, sample2
;
305 int nsamp_blk
= ((ADPCMWAVEFORMAT
*)adsi
->pwfxSrc
)->wSamplesPerBlock
;
306 DWORD nblock
= min(*nsrc
/ adsi
->pwfxSrc
->nBlockAlign
,
307 *ndst
/ (nsamp_blk
* adsi
->pwfxDst
->nBlockAlign
));
309 *nsrc
= nblock
* adsi
->pwfxSrc
->nBlockAlign
;
310 *ndst
= nblock
* nsamp_blk
* adsi
->pwfxDst
->nBlockAlign
;
312 nsamp_blk
-= 2; /* see below for samples from block head */
313 for (; nblock
> 0; nblock
--)
315 const unsigned char* in_src
= src
;
318 coeff
= MSADPCM_CoeffSet
[*src
++];
320 idelta
= R16(src
); src
+= 2;
321 sample1
= R16(src
); src
+= 2;
322 sample2
= R16(src
); src
+= 2;
324 /* store samples from block head */
325 if(adsi
->pwfxDst
->wBitsPerSample
== 8){
326 *dst
= C168(sample2
); ++dst
;
327 *dst
= C168(sample1
); ++dst
;
329 for (nsamp
= nsamp_blk
; nsamp
> 0; nsamp
-= 2)
331 process_nibble(*src
>> 4, &idelta
, &sample1
, &sample2
, &coeff
);
332 *dst
= C168(sample1
); ++dst
;
333 process_nibble(*src
++ & 0x0F, &idelta
, &sample1
, &sample2
, &coeff
);
334 *dst
= C168(sample1
); ++dst
;
336 }else if(adsi
->pwfxDst
->wBitsPerSample
== 16){
337 W16(dst
, sample2
); dst
+= 2;
338 W16(dst
, sample1
); dst
+= 2;
340 for (nsamp
= nsamp_blk
; nsamp
> 0; nsamp
-= 2)
342 process_nibble(*src
>> 4, &idelta
, &sample1
, &sample2
, &coeff
);
343 W16(dst
, sample1
); dst
+= 2;
344 process_nibble(*src
++ & 0x0F, &idelta
, &sample1
, &sample2
, &coeff
);
345 W16(dst
, sample1
); dst
+= 2;
349 src
= in_src
+ adsi
->pwfxSrc
->nBlockAlign
;
354 static void cvtSS16msK(PACMDRVSTREAMINSTANCE adsi
,
355 const unsigned char* src
, LPDWORD nsrc
,
356 unsigned char* dst
, LPDWORD ndst
)
360 static void cvtMM16msK(PACMDRVSTREAMINSTANCE adsi
,
361 const unsigned char* src
, LPDWORD nsrc
,
362 unsigned char* dst
, LPDWORD ndst
)
367 /***********************************************************************
368 * ADPCM_DriverDetails
371 static LRESULT
ADPCM_DriverDetails(PACMDRIVERDETAILSW add
)
373 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
374 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
377 add
->vdwACM
= 0x01000000;
378 add
->vdwDriver
= 0x01000000;
379 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CODEC
;
380 add
->cFormatTags
= 2; /* PCM, MS ADPCM */
381 add
->cFilterTags
= 0;
383 MultiByteToWideChar( CP_ACP
, 0, "MS-ADPCM", -1,
384 add
->szShortName
, sizeof(add
->szShortName
)/sizeof(WCHAR
) );
385 MultiByteToWideChar( CP_ACP
, 0, "Wine MS ADPCM converter", -1,
386 add
->szLongName
, sizeof(add
->szLongName
)/sizeof(WCHAR
) );
387 MultiByteToWideChar( CP_ACP
, 0, "Brought to you by the Wine team...", -1,
388 add
->szCopyright
, sizeof(add
->szCopyright
)/sizeof(WCHAR
) );
389 MultiByteToWideChar( CP_ACP
, 0, "Refer to LICENSE file", -1,
390 add
->szLicensing
, sizeof(add
->szLicensing
)/sizeof(WCHAR
) );
391 add
->szFeatures
[0] = 0;
393 return MMSYSERR_NOERROR
;
396 /***********************************************************************
397 * ADPCM_FormatTagDetails
400 static LRESULT
ADPCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
402 static const WCHAR szPcm
[]={'P','C','M',0};
403 static const WCHAR szMsAdPcm
[]={'M','i','c','r','o','s','o','f','t',' ','A','D','P','C','M',0};
407 case ACM_FORMATTAGDETAILSF_INDEX
:
408 if (aftd
->dwFormatTagIndex
>= 2) return ACMERR_NOTPOSSIBLE
;
410 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
411 if (aftd
->dwFormatTag
== WAVE_FORMAT_UNKNOWN
)
413 aftd
->dwFormatTagIndex
= 1; /* WAVE_FORMAT_ADPCM is bigger than PCM */
417 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
418 switch (aftd
->dwFormatTag
)
420 case WAVE_FORMAT_PCM
: aftd
->dwFormatTagIndex
= 0; break;
421 case WAVE_FORMAT_ADPCM
: aftd
->dwFormatTagIndex
= 1; break;
422 default: return ACMERR_NOTPOSSIBLE
;
426 WARN("Unsupported query %08x\n", dwQuery
);
427 return MMSYSERR_NOTSUPPORTED
;
430 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CODEC
;
431 switch (aftd
->dwFormatTagIndex
)
434 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
435 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
436 aftd
->cStandardFormats
= NUM_PCM_FORMATS
;
437 lstrcpyW(aftd
->szFormatTag
, szPcm
);
440 aftd
->dwFormatTag
= WAVE_FORMAT_ADPCM
;
441 aftd
->cbFormatSize
= sizeof(ADPCMWAVEFORMAT
) + (7 - 1) * sizeof(ADPCMCOEFSET
);
442 aftd
->cStandardFormats
= NUM_ADPCM_FORMATS
;
443 lstrcpyW(aftd
->szFormatTag
, szMsAdPcm
);
446 return MMSYSERR_NOERROR
;
449 /***********************************************************************
450 * ADPCM_FormatDetails
453 static LRESULT
ADPCM_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
457 case ACM_FORMATDETAILSF_FORMAT
:
458 if (ADPCM_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
460 case ACM_FORMATDETAILSF_INDEX
:
461 afd
->pwfx
->wFormatTag
= afd
->dwFormatTag
;
462 switch (afd
->dwFormatTag
)
464 case WAVE_FORMAT_PCM
:
465 if (afd
->dwFormatIndex
>= NUM_PCM_FORMATS
) return ACMERR_NOTPOSSIBLE
;
466 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
467 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
468 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
469 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not accessible
470 * afd->pwfx->cbSize = 0;
472 afd
->pwfx
->nBlockAlign
=
473 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
474 afd
->pwfx
->nAvgBytesPerSec
=
475 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
477 case WAVE_FORMAT_ADPCM
:
478 if (afd
->dwFormatIndex
>= NUM_ADPCM_FORMATS
) return ACMERR_NOTPOSSIBLE
;
479 if (afd
->cbwfx
< sizeof(ADPCMWAVEFORMAT
) + (7 - 1) * sizeof(ADPCMCOEFSET
))
480 return ACMERR_NOTPOSSIBLE
;
481 afd
->pwfx
->nChannels
= ADPCM_Formats
[afd
->dwFormatIndex
].nChannels
;
482 afd
->pwfx
->nSamplesPerSec
= ADPCM_Formats
[afd
->dwFormatIndex
].rate
;
483 afd
->pwfx
->wBitsPerSample
= ADPCM_Formats
[afd
->dwFormatIndex
].nBits
;
484 init_wfx_adpcm((ADPCMWAVEFORMAT
*)afd
->pwfx
);
487 WARN("Unsupported tag %08x\n", afd
->dwFormatTag
);
488 return MMSYSERR_INVALPARAM
;
492 WARN("Unsupported query %08x\n", dwQuery
);
493 return MMSYSERR_NOTSUPPORTED
;
495 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CODEC
;
496 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
498 return MMSYSERR_NOERROR
;
501 /***********************************************************************
502 * ADPCM_FormatSuggest
505 static LRESULT
ADPCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
508 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
509 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
510 adfs
->pwfxSrc
->wFormatTag
== adfs
->pwfxDst
->wFormatTag
||
511 ADPCM_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
512 /* FIXME: should do those tests against the real size (according to format tag */
514 /* If no suggestion for destination, then copy source value */
515 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
))
516 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
517 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
))
518 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
520 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
))
522 if (adfs
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
)
523 adfs
->pwfxDst
->wBitsPerSample
= 4;
525 adfs
->pwfxDst
->wBitsPerSample
= 16;
527 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
))
529 if (adfs
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
)
530 adfs
->pwfxDst
->wFormatTag
= WAVE_FORMAT_ADPCM
;
532 adfs
->pwfxDst
->wFormatTag
= WAVE_FORMAT_PCM
;
535 /* recompute other values */
536 switch (adfs
->pwfxDst
->wFormatTag
)
538 case WAVE_FORMAT_PCM
:
539 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
540 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
541 /* check if result is ok */
542 if (ADPCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
544 case WAVE_FORMAT_ADPCM
:
545 init_wfx_adpcm((ADPCMWAVEFORMAT
*)adfs
->pwfxDst
);
546 /* check if result is ok */
547 if (ADPCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) return ACMERR_NOTPOSSIBLE
;
550 return ACMERR_NOTPOSSIBLE
;
553 return MMSYSERR_NOERROR
;
556 /***********************************************************************
560 static void ADPCM_Reset(PACMDRVSTREAMINSTANCE adsi
, AcmAdpcmData
* aad
)
564 /***********************************************************************
568 static LRESULT
ADPCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
572 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
574 if (ADPCM_GetFormatIndex(adsi
->pwfxSrc
) == 0xFFFFFFFF ||
575 ADPCM_GetFormatIndex(adsi
->pwfxDst
) == 0xFFFFFFFF)
576 return ACMERR_NOTPOSSIBLE
;
578 aad
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmAdpcmData
));
579 if (aad
== 0) return MMSYSERR_NOMEM
;
581 adsi
->dwDriver
= (DWORD_PTR
)aad
;
583 if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
584 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
588 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_ADPCM
&&
589 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
591 /* resampling or mono <=> stereo not available */
592 if (adsi
->pwfxSrc
->nSamplesPerSec
!= adsi
->pwfxDst
->nSamplesPerSec
||
593 adsi
->pwfxSrc
->nChannels
!= adsi
->pwfxDst
->nChannels
)
598 unsigned int nspb
= ((IMAADPCMWAVEFORMAT
*)adsi
->pwfxSrc
)->wSamplesPerBlock
;
599 FIXME("spb=%u\n", nspb
);
601 /* we check that in a block, after the header, samples are present on
602 * 4-sample packet pattern
603 * we also check that the block alignment is bigger than the expected size
605 if (((nspb
- 1) & 3) != 0) goto theEnd
;
606 if ((((nspb
- 1) / 2) + 4) * adsi
->pwfxSrc
->nChannels
< adsi
->pwfxSrc
->nBlockAlign
)
611 /* adpcm decoding... */
612 if (adsi
->pwfxDst
->nChannels
== 2)
613 aad
->convert
= cvtSSms16K
;
614 else if (adsi
->pwfxDst
->nChannels
== 1)
615 aad
->convert
= cvtMMms16K
;
617 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
618 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_ADPCM
)
620 if (adsi
->pwfxSrc
->nSamplesPerSec
!= adsi
->pwfxDst
->nSamplesPerSec
||
621 adsi
->pwfxSrc
->nChannels
!= adsi
->pwfxDst
->nChannels
||
622 adsi
->pwfxSrc
->wBitsPerSample
!= 16)
625 nspb
= ((IMAADPCMWAVEFORMAT
*)adsi
->pwfxDst
)->wSamplesPerBlock
;
626 FIXME("spb=%u\n", nspb
);
628 /* we check that in a block, after the header, samples are present on
629 * 4-sample packet pattern
630 * we also check that the block alignment is bigger than the expected size
632 if (((nspb
- 1) & 3) != 0) goto theEnd
;
633 if ((((nspb
- 1) / 2) + 4) * adsi
->pwfxDst
->nChannels
< adsi
->pwfxDst
->nBlockAlign
)
637 /* adpcm coding... */
638 if (adsi
->pwfxSrc
->wBitsPerSample
== 16 && adsi
->pwfxSrc
->nChannels
== 2)
639 aad
->convert
= cvtSS16msK
;
640 if (adsi
->pwfxSrc
->wBitsPerSample
== 16 && adsi
->pwfxSrc
->nChannels
== 1)
641 aad
->convert
= cvtMM16msK
;
643 FIXME("We don't support encoding yet\n");
647 ADPCM_Reset(adsi
, aad
);
649 return MMSYSERR_NOERROR
;
652 HeapFree(GetProcessHeap(), 0, aad
);
654 return MMSYSERR_NOTSUPPORTED
;
657 /***********************************************************************
661 static LRESULT
ADPCM_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
663 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
664 return MMSYSERR_NOERROR
;
667 /***********************************************************************
671 static LRESULT
ADPCM_StreamSize(const ACMDRVSTREAMINSTANCE
*adsi
, PACMDRVSTREAMSIZE adss
)
674 WORD wSamplesPerBlock
;
675 /* wSamplesPerBlock formula comes from MSDN ADPCMWAVEFORMAT page.*/
676 switch (adss
->fdwSize
)
678 case ACM_STREAMSIZEF_DESTINATION
:
679 /* cbDstLength => cbSrcLength */
680 if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
681 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_ADPCM
)
683 wSamplesPerBlock
= adsi
->pwfxDst
->nBlockAlign
* 2 / adsi
->pwfxDst
->nChannels
- 12;
684 nblocks
= adss
->cbDstLength
/ adsi
->pwfxDst
->nBlockAlign
;
686 return ACMERR_NOTPOSSIBLE
;
687 adss
->cbSrcLength
= nblocks
* adsi
->pwfxSrc
->nBlockAlign
* wSamplesPerBlock
;
689 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_ADPCM
&&
690 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
692 wSamplesPerBlock
= adsi
->pwfxSrc
->nBlockAlign
* 2 / adsi
->pwfxSrc
->nChannels
- 12;
693 nblocks
= adss
->cbDstLength
/ (adsi
->pwfxDst
->nBlockAlign
* wSamplesPerBlock
);
695 return ACMERR_NOTPOSSIBLE
;
696 adss
->cbSrcLength
= nblocks
* adsi
->pwfxSrc
->nBlockAlign
;
700 return MMSYSERR_NOTSUPPORTED
;
703 case ACM_STREAMSIZEF_SOURCE
:
704 /* cbSrcLength => cbDstLength */
705 if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_PCM
&&
706 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_ADPCM
)
708 wSamplesPerBlock
= adsi
->pwfxDst
->nBlockAlign
* 2 / adsi
->pwfxDst
->nChannels
- 12;
709 nblocks
= adss
->cbSrcLength
/ (adsi
->pwfxSrc
->nBlockAlign
* wSamplesPerBlock
);
711 return ACMERR_NOTPOSSIBLE
;
712 if (adss
->cbSrcLength
% (adsi
->pwfxSrc
->nBlockAlign
* wSamplesPerBlock
))
713 /* Round block count up. */
715 adss
->cbDstLength
= nblocks
* adsi
->pwfxDst
->nBlockAlign
;
717 else if (adsi
->pwfxSrc
->wFormatTag
== WAVE_FORMAT_ADPCM
&&
718 adsi
->pwfxDst
->wFormatTag
== WAVE_FORMAT_PCM
)
720 wSamplesPerBlock
= adsi
->pwfxSrc
->nBlockAlign
* 2 / adsi
->pwfxSrc
->nChannels
- 12;
721 nblocks
= adss
->cbSrcLength
/ adsi
->pwfxSrc
->nBlockAlign
;
723 return ACMERR_NOTPOSSIBLE
;
724 if (adss
->cbSrcLength
% adsi
->pwfxSrc
->nBlockAlign
)
725 /* Round block count up. */
727 adss
->cbDstLength
= nblocks
* adsi
->pwfxDst
->nBlockAlign
* wSamplesPerBlock
;
731 return MMSYSERR_NOTSUPPORTED
;
735 WARN("Unsupported query %08x\n", adss
->fdwSize
);
736 return MMSYSERR_NOTSUPPORTED
;
738 return MMSYSERR_NOERROR
;
741 /***********************************************************************
742 * ADPCM_StreamConvert
745 static LRESULT
ADPCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
747 AcmAdpcmData
* aad
= (AcmAdpcmData
*)adsi
->dwDriver
;
748 DWORD nsrc
= adsh
->cbSrcLength
;
749 DWORD ndst
= adsh
->cbDstLength
;
751 if (adsh
->fdwConvert
&
752 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
753 ACM_STREAMCONVERTF_END
|
754 ACM_STREAMCONVERTF_START
))
756 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh
->fdwConvert
);
758 /* ACM_STREAMCONVERTF_BLOCKALIGN
759 * currently all conversions are block aligned, so do nothing for this flag
760 * ACM_STREAMCONVERTF_END
761 * no pending data, so do nothing for this flag
763 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
))
765 ADPCM_Reset(adsi
, aad
);
768 aad
->convert(adsi
, adsh
->pbSrc
, &nsrc
, adsh
->pbDst
, &ndst
);
769 adsh
->cbSrcLengthUsed
= nsrc
;
770 adsh
->cbDstLengthUsed
= ndst
;
772 return MMSYSERR_NOERROR
;
775 /**************************************************************************
776 * ADPCM_DriverProc [exported]
778 LRESULT CALLBACK
ADPCM_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
779 LPARAM dwParam1
, LPARAM dwParam2
)
781 TRACE("(%08lx %p %04x %08lx %08lx);\n",
782 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
786 case DRV_LOAD
: return 1;
787 case DRV_FREE
: return 1;
788 case DRV_OPEN
: return ADPCM_drvOpen((LPSTR
)dwParam1
);
789 case DRV_CLOSE
: return ADPCM_drvClose(dwDevID
);
790 case DRV_ENABLE
: return 1;
791 case DRV_DISABLE
: return 1;
792 case DRV_QUERYCONFIGURE
: return 1;
793 case DRV_CONFIGURE
: MessageBoxA(0, "MSACM MS ADPCM filter !", "Wine Driver", MB_OK
); return 1;
794 case DRV_INSTALL
: return DRVCNF_RESTART
;
795 case DRV_REMOVE
: return DRVCNF_RESTART
;
797 case ACMDM_DRIVER_NOTIFY
:
798 /* no caching from other ACM drivers is done so far */
799 return MMSYSERR_NOERROR
;
801 case ACMDM_DRIVER_DETAILS
:
802 return ADPCM_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
804 case ACMDM_FORMATTAG_DETAILS
:
805 return ADPCM_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
807 case ACMDM_FORMAT_DETAILS
:
808 return ADPCM_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
810 case ACMDM_FORMAT_SUGGEST
:
811 return ADPCM_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
813 case ACMDM_STREAM_OPEN
:
814 return ADPCM_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
816 case ACMDM_STREAM_CLOSE
:
817 return ADPCM_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
819 case ACMDM_STREAM_SIZE
:
820 return ADPCM_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
822 case ACMDM_STREAM_CONVERT
:
823 return ADPCM_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
825 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
826 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
827 /* this converter is not a hardware driver */
828 case ACMDM_FILTERTAG_DETAILS
:
829 case ACMDM_FILTER_DETAILS
:
830 /* this converter is not a filter */
831 case ACMDM_STREAM_RESET
:
832 /* only needed for asynchronous driver... we aren't, so just say it */
833 return MMSYSERR_NOTSUPPORTED
;
834 case ACMDM_STREAM_PREPARE
:
835 case ACMDM_STREAM_UNPREPARE
:
836 /* nothing special to do here... so don't do anything */
837 return MMSYSERR_NOERROR
;
840 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);