1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 2000 Eric Pouech
7 * Copyright 2004 Robert Reif
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * + get rid of hack for PCM_DriverProc (msacm32.dll shouldn't export
25 * a DriverProc, but this would require implementing a generic
26 * embedded driver handling scheme in msacm32.dll which isn't done yet
48 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(msacm
);
52 /***********************************************************************
55 static DWORD
PCM_drvOpen(LPCSTR str
, PACMDRVOPENDESCW adod
)
57 TRACE("(%p, %p)\n", str
, adod
);
59 return (adod
== NULL
) ||
60 (adod
->fccType
== ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
&&
61 adod
->fccComp
== ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
);
64 /***********************************************************************
67 static DWORD
PCM_drvClose(DWORD dwDevID
)
69 TRACE("(%d)\n", dwDevID
);
74 #define NUM_OF(a,b) ((a)/(b))
76 /* flags for fdwDriver */
77 #define PCM_RESAMPLE 1
79 typedef void (*PCM_CONVERT_KEEP_RATE
)(const unsigned char*, int, unsigned char*);
81 typedef void (*PCM_CONVERT_CHANGE_RATE
)(const DWORD
, const unsigned char*, DWORD
*, const DWORD
, unsigned char*, DWORD
*);
83 /* data used while converting */
84 typedef struct tagAcmPcmData
{
85 /* conversion routine, depending if rate conversion is required */
87 PCM_CONVERT_KEEP_RATE cvtKeepRate
;
88 PCM_CONVERT_CHANGE_RATE cvtChangeRate
;
92 /* table to list all supported formats... those are the basic ones. this
93 * also helps given a unique index to each of the supported formats
100 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000}, {1, 24, 8000}, {2, 24, 8000},
101 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025}, {1, 24, 11025}, {2, 24, 11025},
102 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050}, {1, 24, 22050}, {2, 24, 22050},
103 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100}, {1, 24, 44100}, {2, 24, 44100},
104 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000}, {1, 24, 48000}, {2, 24, 48000},
105 {1, 8, 96000}, {2, 8, 96000}, {1, 16, 96000}, {2, 16, 96000}, {1, 24, 96000}, {2, 24, 96000},
108 /***********************************************************************
111 static DWORD
PCM_GetFormatIndex(LPWAVEFORMATEX wfx
)
114 TRACE("(%p)\n", wfx
);
116 for (i
= 0; i
< ARRAY_SIZE(PCM_Formats
); i
++) {
117 if (wfx
->nChannels
== PCM_Formats
[i
].nChannels
&&
118 wfx
->nSamplesPerSec
== PCM_Formats
[i
].rate
&&
119 wfx
->wBitsPerSample
== PCM_Formats
[i
].nBits
)
128 * + 8 bit unsigned vs 16 bit signed
129 * + mono vs stereo (1 or 2 channels)
130 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo
131 * shall work in all cases)
133 * mono => stereo: copy the same sample on Left & Right channels
134 * stereo => mono: use the sum of Left & Right channels
137 /***********************************************************************
140 * Converts a 8 bit sample to a 16 bit one
142 static inline short C816(unsigned char b
)
144 return (b
- 128) << 8;
147 /***********************************************************************
150 * Converts a 16 bit sample to a 8 bit one (data loss !!)
152 static inline unsigned char C168(short s
)
154 return HIBYTE(s
) ^ (unsigned char)0x80;
157 /***********************************************************************
160 * Converts a 24 bit sample to a 8 bit one (data loss !!)
162 static inline unsigned char C248(int s
)
164 return HIBYTE(HIWORD(s
)) ^ (unsigned char)0x80;
167 /***********************************************************************
170 * Converts a 24 bit sample to a 16 bit one (data loss !!)
172 static inline short C2416(int s
)
177 /***********************************************************************
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 /***********************************************************************
190 * Read a 24 bit sample (correctly handles endianness)
191 * Note, to support signed arithmetic, the values are shifted high in the int
192 * and low 8 bytes are unused.
194 static inline int R24(const unsigned char* src
)
196 return ((int)src
[0] | (int)src
[1] << 8 | (int)src
[2] << 16) << 8;
199 /***********************************************************************
202 * Write a 16 bit sample (correctly handles endianness)
204 static inline void W16(unsigned char* dst
, short s
)
210 /***********************************************************************
213 * Write a 24 bit sample (correctly handles endianness)
215 static inline void W24(unsigned char* dst
, int s
)
217 dst
[0] = HIBYTE(LOWORD(s
));
218 dst
[1] = LOBYTE(HIWORD(s
));
219 dst
[2] = HIBYTE(HIWORD(s
));
222 /***********************************************************************
225 * Convert the (l,r) 24 bit stereo sample into a 24 bit mono
226 * (takes the sum of the two values)
228 static inline int M24(int l
, int r
)
230 LONGLONG sum
= l
+ r
;
232 /* clip sum to saturation */
233 if (sum
> 0x7fffff00)
235 else if (sum
< -0x7fffff00)
241 /***********************************************************************
244 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
245 * (takes the sum of the two values)
247 static inline short M16(short l
, short r
)
251 /* clip sum to saturation */
254 else if (sum
< -32768)
260 /***********************************************************************
263 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
264 * (takes the sum of the two values)
266 static inline unsigned char M8(unsigned char a
, unsigned char b
)
270 int sum
= (l
+ r
) + 128;
272 /* clip sum to saturation */
281 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
283 * <X> is the (M)ono/(S)tereo configuration of input channel
284 * <Y> is the (M)ono/(S)tereo configuration of output channel
285 * <N> is the number of bits of input channel (8 or 16)
286 * <M> is the number of bits of output channel (8 or 16)
288 * in the parameters, ns is always the number of samples, so the size of input
289 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
292 static void cvtMM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
294 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
295 memcpy(dst
, src
, ns
);
298 static void cvtSS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
300 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
301 memcpy(dst
, src
, ns
* 2);
304 static void cvtMM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
306 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
307 memcpy(dst
, src
, ns
* 2);
310 static void cvtSS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
312 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
313 memcpy(dst
, src
, ns
* 4);
316 static void cvtMS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
318 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
326 static void cvtMS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
329 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
333 W16(dst
, v
); dst
+= 2;
334 W16(dst
, v
); dst
+= 2;
338 static void cvtMS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
341 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
344 v
= C168(R16(src
)); src
+= 2;
350 static void cvtMS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
353 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
356 v
= R16(src
); src
+= 2;
357 W16(dst
, v
); dst
+= 2;
358 W16(dst
, v
); dst
+= 2;
362 static void cvtSM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
364 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
367 *dst
++ = M8(src
[0], src
[1]);
372 static void cvtSM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
375 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
378 v
= M16(C816(src
[0]), C816(src
[1]));
380 W16(dst
, v
); dst
+= 2;
384 static void cvtSM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
386 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
389 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
394 static void cvtSM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
396 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
399 W16(dst
, M16(R16(src
),R16(src
+2))); dst
+= 2;
404 static void cvtMM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
406 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
409 W16(dst
, C816(*src
++)); dst
+= 2;
413 static void cvtSS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
415 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
418 W16(dst
, C816(*src
++)); dst
+= 2;
419 W16(dst
, C816(*src
++)); dst
+= 2;
423 static void cvtMM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
425 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
428 *dst
++ = C168(R16(src
)); src
+= 2;
432 static void cvtSS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
434 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
437 *dst
++ = C168(R16(src
)); src
+= 2;
438 *dst
++ = C168(R16(src
)); src
+= 2;
442 static void cvtMS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
445 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
448 v
= C248(R24(src
)); src
+= 3;
454 static void cvtSM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
456 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
459 *dst
++ = C248(M24(R24(src
), R24(src
+ 3)));
464 static void cvtMM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
466 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
469 *dst
++ = C248(R24(src
)); src
+= 3;
473 static void cvtSS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
475 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
478 *dst
++ = C248(R24(src
)); src
+= 3;
479 *dst
++ = C248(R24(src
)); src
+= 3;
483 static void cvtMS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
486 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
489 v
= C2416(R24(src
)); src
+= 3;
490 W16(dst
, v
); dst
+= 2;
491 W16(dst
, v
); dst
+= 2;
495 static void cvtSM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
497 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
500 W16(dst
, C2416(M24(R24(src
), R24(src
+ 3))));
506 static void cvtMM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
508 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
511 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
515 static void cvtSS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
517 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
520 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
521 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
526 static const PCM_CONVERT_KEEP_RATE PCM_ConvertKeepRate
[] = {
527 cvtSS88K
, cvtSM88K
, cvtMS88K
, cvtMM88K
,
528 cvtSS816K
, cvtSM816K
, cvtMS816K
, cvtMM816K
,
529 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
530 cvtSS168K
, cvtSM168K
, cvtMS168K
, cvtMM168K
,
531 cvtSS1616K
, cvtSM1616K
, cvtMS1616K
, cvtMM1616K
,
532 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
533 cvtSS248K
, cvtSM248K
, cvtMS248K
, cvtMM248K
,
534 cvtSS2416K
, cvtSM2416K
, cvtMS2416K
, cvtMM2416K
,
535 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->24 */
538 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
540 * <X> is the (M)ono/(S)tereo configuration of input channel
541 * <Y> is the (M)ono/(S)tereo configuration of output channel
542 * <N> is the number of bits of input channel (8 or 16)
543 * <M> is the number of bits of output channel (8 or 16)
547 static void cvtSS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
548 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
550 DWORD error
= srcRate
/ 2;
551 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
553 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
555 while (error
> srcRate
) {
568 static void cvtSM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
569 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
571 DWORD error
= srcRate
/ 2;
572 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
574 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
576 while (error
> srcRate
) {
582 *dst
++ = M8(src
[0], src
[1]);
588 static void cvtMS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
589 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
591 DWORD error
= srcRate
/ 2;
592 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
594 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
596 while (error
> srcRate
) {
609 static void cvtMM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
610 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
612 DWORD error
= srcRate
/ 2;
613 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
615 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
617 while (error
> srcRate
) {
629 static void cvtSS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
630 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
632 DWORD error
= srcRate
/ 2;
633 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
635 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
637 while (error
> srcRate
) {
643 W16(dst
, C816(src
[0])); dst
+= 2;
644 W16(dst
, C816(src
[1])); dst
+= 2;
650 static void cvtSM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
651 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
653 DWORD error
= srcRate
/ 2;
654 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
656 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
658 while (error
> srcRate
) {
664 W16(dst
, M16(C816(src
[0]), C816(src
[1]))); dst
+= 2;
670 static void cvtMS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
671 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
673 DWORD error
= srcRate
/ 2;
674 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
676 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
678 while (error
> srcRate
) {
684 W16(dst
, C816(src
[0])); dst
+= 2;
685 W16(dst
, C816(src
[0])); dst
+= 2;
691 static void cvtMM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
692 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
694 DWORD error
= srcRate
/ 2;
695 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
697 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
699 while (error
> srcRate
) {
705 W16(dst
, C816(src
[0])); dst
+= 2;
711 static void cvtSS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
712 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
714 DWORD error
= srcRate
/ 2;
715 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
717 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
719 while (error
> srcRate
) {
725 *dst
++ = C168(R16(src
));
726 *dst
++ = C168(R16(src
+ 2));
732 static void cvtSM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
733 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
735 DWORD error
= srcRate
/ 2;
736 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
738 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
740 while (error
> srcRate
) {
746 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
752 static void cvtMS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
753 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
755 DWORD error
= srcRate
/ 2;
756 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
758 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
760 while (error
> srcRate
) {
766 *dst
++ = C168(R16(src
));
767 *dst
++ = C168(R16(src
));
773 static void cvtMM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
774 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
776 DWORD error
= srcRate
/ 2;
777 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
779 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
781 while (error
> srcRate
) {
787 *dst
++ = C168(R16(src
));
793 static void cvtSS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
794 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
796 DWORD error
= srcRate
/ 2;
797 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
799 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
801 while (error
> srcRate
) {
807 W16(dst
, R16(src
)); dst
+= 2;
808 W16(dst
, R16(src
+ 2)); dst
+= 2;
814 static void cvtSM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
815 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
817 DWORD error
= srcRate
/ 2;
818 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
820 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
822 while (error
> srcRate
) {
828 W16(dst
, M16(R16(src
), R16(src
+ 2))); dst
+= 2;
834 static void cvtMS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
835 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
837 DWORD error
= srcRate
/ 2;
838 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
840 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
842 while (error
> srcRate
) {
848 W16(dst
, R16(src
)); dst
+= 2;
849 W16(dst
, R16(src
)); dst
+= 2;
855 static void cvtMM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
856 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
858 DWORD error
= srcRate
/ 2;
859 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
861 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
863 while (error
> srcRate
) {
869 W16(dst
, R16(src
)); dst
+= 2;
875 static void cvtSS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
876 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
878 DWORD error
= srcRate
/ 2;
879 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
881 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
883 while (error
> srcRate
) {
889 W24(dst
, R24(src
)); dst
+= 3;
890 W24(dst
, R24(src
+ 3)); dst
+= 3;
896 static void cvtSM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
897 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
899 DWORD error
= srcRate
/ 2;
900 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
902 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
904 while (error
> srcRate
) {
910 W24(dst
, M24(R24(src
), R24(src
+ 3))); dst
+= 3;
916 static void cvtMS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
917 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
919 DWORD error
= srcRate
/ 2;
920 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
922 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
924 while (error
> srcRate
) {
930 W24(dst
, R24(src
)); dst
+= 3;
931 W24(dst
, R24(src
)); dst
+= 3;
937 static void cvtMM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
938 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
940 DWORD error
= srcRate
/ 2;
941 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
943 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
945 while (error
> srcRate
) {
951 W24(dst
, R24(src
)); dst
+= 3;
957 static const PCM_CONVERT_CHANGE_RATE PCM_ConvertChangeRate
[] = {
958 cvtSS88C
, cvtSM88C
, cvtMS88C
, cvtMM88C
,
959 cvtSS816C
, cvtSM816C
, cvtMS816C
, cvtMM816C
,
960 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
961 cvtSS168C
, cvtSM168C
, cvtMS168C
, cvtMM168C
,
962 cvtSS1616C
, cvtSM1616C
, cvtMS1616C
, cvtMM1616C
,
963 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
964 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->8 */
965 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->16 */
966 cvtSS2424C
, cvtSM2424C
, cvtMS2424C
, cvtMM2424C
,
969 /***********************************************************************
973 static LRESULT
PCM_DriverDetails(PACMDRIVERDETAILSW add
)
975 TRACE("(%p)\n", add
);
977 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
978 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
979 add
->wMid
= MM_MICROSOFT
;
980 add
->wPid
= MM_MSFT_ACM_PCM
;
981 add
->vdwACM
= 0x01000000;
982 add
->vdwDriver
= 0x01000000;
983 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
984 add
->cFormatTags
= 1;
985 add
->cFilterTags
= 0;
987 MultiByteToWideChar(CP_ACP
, 0, "MS-PCM", -1, add
->szShortName
, ARRAY_SIZE(add
->szShortName
));
988 MultiByteToWideChar(CP_ACP
, 0, "Wine PCM converter", -1,
989 add
->szLongName
, ARRAY_SIZE(add
->szLongName
));
990 MultiByteToWideChar(CP_ACP
, 0, "Brought to you by the Wine team...", -1,
991 add
->szCopyright
, ARRAY_SIZE(add
->szCopyright
));
992 MultiByteToWideChar(CP_ACP
, 0, "Refer to LICENSE file", -1,
993 add
->szLicensing
, ARRAY_SIZE(add
->szLicensing
) );
994 add
->szFeatures
[0] = 0;
996 return MMSYSERR_NOERROR
;
999 /***********************************************************************
1000 * PCM_FormatTagDetails
1003 static LRESULT
PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
1005 TRACE("(%p, %08x)\n", aftd
, dwQuery
);
1008 case ACM_FORMATTAGDETAILSF_INDEX
:
1009 if (aftd
->dwFormatTagIndex
!= 0) {
1010 WARN("not possible\n");
1011 return ACMERR_NOTPOSSIBLE
;
1014 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
1015 if (aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1016 WARN("not possible\n");
1017 return ACMERR_NOTPOSSIBLE
;
1020 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
1021 if (aftd
->dwFormatTag
!= WAVE_FORMAT_UNKNOWN
&&
1022 aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1023 WARN("not possible\n");
1024 return ACMERR_NOTPOSSIBLE
;
1028 WARN("Unsupported query %08x\n", dwQuery
);
1029 return MMSYSERR_NOTSUPPORTED
;
1032 aftd
->dwFormatTagIndex
= 0;
1033 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1034 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
1035 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1036 aftd
->cStandardFormats
= ARRAY_SIZE(PCM_Formats
);
1037 aftd
->szFormatTag
[0] = 0;
1039 return MMSYSERR_NOERROR
;
1042 /***********************************************************************
1046 static LRESULT
PCM_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
1048 TRACE("(%p, %08x)\n", afd
, dwQuery
);
1051 case ACM_FORMATDETAILSF_FORMAT
:
1052 if (PCM_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) {
1053 WARN("not possible\n");
1054 return ACMERR_NOTPOSSIBLE
;
1057 case ACM_FORMATDETAILSF_INDEX
:
1058 assert(afd
->dwFormatIndex
< ARRAY_SIZE(PCM_Formats
));
1059 afd
->pwfx
->wFormatTag
= WAVE_FORMAT_PCM
;
1060 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
1061 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
1062 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
1063 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
1064 * accessible afd->pwfx->cbSize = 0;
1066 afd
->pwfx
->nBlockAlign
=
1067 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
1068 afd
->pwfx
->nAvgBytesPerSec
=
1069 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
1072 WARN("Unsupported query %08x\n", dwQuery
);
1073 return MMSYSERR_NOTSUPPORTED
;
1076 afd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1077 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1078 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
1079 afd
->cbwfx
= sizeof(PCMWAVEFORMAT
);
1081 return MMSYSERR_NOERROR
;
1084 /***********************************************************************
1088 static LRESULT
PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
1090 TRACE("(%p)\n", adfs
);
1092 /* some tests ... */
1093 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
1094 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
1095 PCM_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) {
1096 WARN("not possible\n");
1097 return ACMERR_NOTPOSSIBLE
;
1100 /* is no suggestion for destination, then copy source value */
1101 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
)) {
1102 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
1104 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
)) {
1105 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
1107 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
)) {
1108 adfs
->pwfxDst
->wBitsPerSample
= adfs
->pwfxSrc
->wBitsPerSample
;
1110 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
)) {
1111 if (adfs
->pwfxSrc
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1112 WARN("source format 0x%x not supported\n", adfs
->pwfxSrc
->wFormatTag
);
1113 return ACMERR_NOTPOSSIBLE
;
1115 adfs
->pwfxDst
->wFormatTag
= adfs
->pwfxSrc
->wFormatTag
;
1117 if (adfs
->pwfxDst
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1118 WARN("destination format 0x%x not supported\n", adfs
->pwfxDst
->wFormatTag
);
1119 return ACMERR_NOTPOSSIBLE
;
1122 /* check if result is ok */
1123 if (PCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) {
1124 WARN("not possible\n");
1125 return ACMERR_NOTPOSSIBLE
;
1128 /* recompute other values */
1129 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
1130 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
1132 return MMSYSERR_NOERROR
;
1135 /***********************************************************************
1139 static LRESULT
PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
1145 TRACE("(%p)\n", adsi
);
1147 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
1149 switch(adsi
->pwfxSrc
->wBitsPerSample
){
1157 if (adsi
->pwfxSrc
->nBlockAlign
!= 3 * adsi
->pwfxSrc
->nChannels
) {
1158 FIXME("Source: 24-bit samples must be packed\n");
1159 return MMSYSERR_NOTSUPPORTED
;
1164 FIXME("Unsupported source bit depth: %u\n", adsi
->pwfxSrc
->wBitsPerSample
);
1165 return MMSYSERR_NOTSUPPORTED
;
1168 switch(adsi
->pwfxDst
->wBitsPerSample
){
1175 if (adsi
->pwfxDst
->nBlockAlign
!= 3 * adsi
->pwfxDst
->nChannels
) {
1176 FIXME("Destination: 24-bit samples must be packed\n");
1177 return MMSYSERR_NOTSUPPORTED
;
1182 FIXME("Unsupported destination bit depth: %u\n", adsi
->pwfxDst
->wBitsPerSample
);
1183 return MMSYSERR_NOTSUPPORTED
;
1186 if (adsi
->pwfxSrc
->nChannels
== 1) idx
+= 2;
1188 if (adsi
->pwfxDst
->nChannels
== 1) idx
+= 1;
1190 apd
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData
));
1192 return MMSYSERR_NOMEM
;
1194 if (adsi
->pwfxSrc
->nSamplesPerSec
== adsi
->pwfxDst
->nSamplesPerSec
) {
1196 apd
->cvt
.cvtKeepRate
= PCM_ConvertKeepRate
[idx
];
1198 flags
= PCM_RESAMPLE
;
1199 apd
->cvt
.cvtChangeRate
= PCM_ConvertChangeRate
[idx
];
1202 if(!apd
->cvt
.cvtChangeRate
&& !apd
->cvt
.cvtKeepRate
){
1203 FIXME("Unimplemented conversion from %u -> %u bps\n",
1204 adsi
->pwfxSrc
->wBitsPerSample
,
1205 adsi
->pwfxDst
->wBitsPerSample
);
1206 HeapFree(GetProcessHeap(), 0, apd
);
1207 return MMSYSERR_NOTSUPPORTED
;
1210 adsi
->dwDriver
= (DWORD_PTR
)apd
;
1211 adsi
->fdwDriver
= flags
;
1213 return MMSYSERR_NOERROR
;
1216 /***********************************************************************
1220 static LRESULT
PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
1222 TRACE("(%p)\n", adsi
);
1224 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
1225 return MMSYSERR_NOERROR
;
1228 /***********************************************************************
1232 static inline DWORD
PCM_round(DWORD a
, DWORD b
, DWORD c
)
1235 /* to be sure, always return an entire number of c... */
1236 return ((double)a
* (double)b
+ (double)c
- 1) / (double)c
;
1239 /***********************************************************************
1243 static LRESULT
PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMSIZE adss
)
1245 DWORD srcMask
= ~(adsi
->pwfxSrc
->nBlockAlign
- 1);
1246 DWORD dstMask
= ~(adsi
->pwfxDst
->nBlockAlign
- 1);
1248 TRACE("(%p, %p)\n", adsi
, adss
);
1250 switch (adss
->fdwSize
) {
1251 case ACM_STREAMSIZEF_DESTINATION
:
1252 /* cbDstLength => cbSrcLength */
1253 adss
->cbSrcLength
= PCM_round(adss
->cbDstLength
& dstMask
,
1254 adsi
->pwfxSrc
->nAvgBytesPerSec
,
1255 adsi
->pwfxDst
->nAvgBytesPerSec
) & srcMask
;
1257 case ACM_STREAMSIZEF_SOURCE
:
1258 /* cbSrcLength => cbDstLength */
1259 adss
->cbDstLength
= PCM_round(adss
->cbSrcLength
& srcMask
,
1260 adsi
->pwfxDst
->nAvgBytesPerSec
,
1261 adsi
->pwfxSrc
->nAvgBytesPerSec
) & dstMask
;
1264 WARN("Unsupported query %08x\n", adss
->fdwSize
);
1265 return MMSYSERR_NOTSUPPORTED
;
1267 return MMSYSERR_NOERROR
;
1270 /***********************************************************************
1274 static LRESULT
PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
1276 AcmPcmData
* apd
= (AcmPcmData
*)adsi
->dwDriver
;
1277 DWORD nsrc
= NUM_OF(adsh
->cbSrcLength
, adsi
->pwfxSrc
->nBlockAlign
);
1278 DWORD ndst
= NUM_OF(adsh
->cbDstLength
, adsi
->pwfxDst
->nBlockAlign
);
1280 TRACE("(%p, %p)\n", adsi
, adsh
);
1282 TRACE("nsrc=%d,adsh->cbSrcLength=%d\n", nsrc
, adsh
->cbSrcLength
);
1283 TRACE("ndst=%d,adsh->cbDstLength=%d\n", ndst
, adsh
->cbDstLength
);
1284 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1285 adsi
->pwfxSrc
->wFormatTag
, adsi
->pwfxSrc
->nChannels
, adsi
->pwfxSrc
->nSamplesPerSec
, adsi
->pwfxSrc
->nAvgBytesPerSec
,
1286 adsi
->pwfxSrc
->nBlockAlign
, adsi
->pwfxSrc
->wBitsPerSample
, adsi
->pwfxSrc
->cbSize
);
1287 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1288 adsi
->pwfxDst
->wFormatTag
, adsi
->pwfxDst
->nChannels
, adsi
->pwfxDst
->nSamplesPerSec
, adsi
->pwfxDst
->nAvgBytesPerSec
,
1289 adsi
->pwfxDst
->nBlockAlign
, adsi
->pwfxDst
->wBitsPerSample
, adsi
->pwfxDst
->cbSize
);
1291 if (adsh
->fdwConvert
&
1292 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
1293 ACM_STREAMCONVERTF_END
|
1294 ACM_STREAMCONVERTF_START
)) {
1295 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh
->fdwConvert
);
1297 /* ACM_STREAMCONVERTF_BLOCKALIGN
1298 * currently all conversions are block aligned, so do nothing for this flag
1299 * ACM_STREAMCONVERTF_END
1300 * no pending data, so do nothing for this flag
1302 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
) &&
1303 (adsi
->fdwDriver
& PCM_RESAMPLE
)) {
1307 if (adsi
->fdwDriver
& PCM_RESAMPLE
) {
1308 apd
->cvt
.cvtChangeRate(adsi
->pwfxSrc
->nSamplesPerSec
, adsh
->pbSrc
, &nsrc
,
1309 adsi
->pwfxDst
->nSamplesPerSec
, adsh
->pbDst
, &ndst
);
1311 if (nsrc
< ndst
) ndst
= nsrc
; else nsrc
= ndst
;
1313 /* nsrc is now equal to ndst */
1314 apd
->cvt
.cvtKeepRate(adsh
->pbSrc
, nsrc
, adsh
->pbDst
);
1317 adsh
->cbSrcLengthUsed
= nsrc
* adsi
->pwfxSrc
->nBlockAlign
;
1318 adsh
->cbDstLengthUsed
= ndst
* adsi
->pwfxDst
->nBlockAlign
;
1320 return MMSYSERR_NOERROR
;
1323 /**************************************************************************
1324 * DriverProc (MSACM32.@)
1326 LRESULT CALLBACK
PCM_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1327 LPARAM dwParam1
, LPARAM dwParam2
)
1329 TRACE("(%08lx %p %u %08lx %08lx);\n",
1330 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1333 case DRV_LOAD
: return 1;
1334 case DRV_FREE
: return 1;
1335 case DRV_OPEN
: return PCM_drvOpen((LPSTR
)dwParam1
, (PACMDRVOPENDESCW
)dwParam2
);
1336 case DRV_CLOSE
: return PCM_drvClose(dwDevID
);
1337 case DRV_ENABLE
: return 1;
1338 case DRV_DISABLE
: return 1;
1339 case DRV_QUERYCONFIGURE
: return 1;
1340 case DRV_CONFIGURE
: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK
); return 1;
1341 case DRV_INSTALL
: return DRVCNF_RESTART
;
1342 case DRV_REMOVE
: return DRVCNF_RESTART
;
1344 case ACMDM_DRIVER_NOTIFY
:
1345 /* no caching from other ACM drivers is done so far */
1346 return MMSYSERR_NOERROR
;
1348 case ACMDM_DRIVER_DETAILS
:
1349 return PCM_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
1351 case ACMDM_FORMATTAG_DETAILS
:
1352 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
1354 case ACMDM_FORMAT_DETAILS
:
1355 return PCM_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
1357 case ACMDM_FORMAT_SUGGEST
:
1358 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
1360 case ACMDM_STREAM_OPEN
:
1361 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
1363 case ACMDM_STREAM_CLOSE
:
1364 return PCM_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
1366 case ACMDM_STREAM_SIZE
:
1367 return PCM_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
1369 case ACMDM_STREAM_CONVERT
:
1370 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
1372 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
1373 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
1374 /* this converter is not a hardware driver */
1375 case ACMDM_FILTERTAG_DETAILS
:
1376 case ACMDM_FILTER_DETAILS
:
1377 /* this converter is not a filter */
1378 case ACMDM_STREAM_RESET
:
1379 /* only needed for asynchronous driver... we aren't, so just say it */
1380 case ACMDM_STREAM_PREPARE
:
1381 case ACMDM_STREAM_UNPREPARE
:
1382 /* nothing special to do here... so don't do anything */
1383 return MMSYSERR_NOTSUPPORTED
;
1386 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);