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
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(msacm
);
51 /***********************************************************************
54 static DWORD
PCM_drvOpen(LPCSTR str
, PACMDRVOPENDESCW adod
)
56 TRACE("(%p, %p)\n", str
, adod
);
58 return (adod
== NULL
) ||
59 (adod
->fccType
== ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
&&
60 adod
->fccComp
== ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
);
63 /***********************************************************************
66 static DWORD
PCM_drvClose(DWORD dwDevID
)
68 TRACE("(%d)\n", dwDevID
);
73 #define NUM_OF(a,b) ((a)/(b))
75 /* flags for fdwDriver */
76 #define PCM_RESAMPLE 1
78 typedef void (*PCM_CONVERT_KEEP_RATE
)(const unsigned char*, int, unsigned char*);
80 typedef void (*PCM_CONVERT_CHANGE_RATE
)(const DWORD
, const unsigned char*, DWORD
*, const DWORD
, unsigned char*, DWORD
*);
82 /* data used while converting */
83 typedef struct tagAcmPcmData
{
84 /* conversion routine, depending if rate conversion is required */
86 PCM_CONVERT_KEEP_RATE cvtKeepRate
;
87 PCM_CONVERT_CHANGE_RATE cvtChangeRate
;
91 /* table to list all supported formats... those are the basic ones. this
92 * also helps given a unique index to each of the supported formats
99 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000}, {1, 24, 8000}, {2, 24, 8000},
100 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025}, {1, 24, 11025}, {2, 24, 11025},
101 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050}, {1, 24, 22050}, {2, 24, 22050},
102 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100}, {1, 24, 44100}, {2, 24, 44100},
103 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000}, {1, 24, 48000}, {2, 24, 48000},
104 {1, 8, 96000}, {2, 8, 96000}, {1, 16, 96000}, {2, 16, 96000}, {1, 24, 96000}, {2, 24, 96000},
107 /***********************************************************************
110 static DWORD
PCM_GetFormatIndex(LPWAVEFORMATEX wfx
)
113 TRACE("(%p)\n", wfx
);
115 for (i
= 0; i
< ARRAY_SIZE(PCM_Formats
); i
++) {
116 if (wfx
->nChannels
== PCM_Formats
[i
].nChannels
&&
117 wfx
->nSamplesPerSec
== PCM_Formats
[i
].rate
&&
118 wfx
->wBitsPerSample
== PCM_Formats
[i
].nBits
)
127 * + 8 bit unsigned vs 16 bit signed
128 * + mono vs stereo (1 or 2 channels)
129 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo
130 * shall work in all cases)
132 * mono => stereo: copy the same sample on Left & Right channels
133 * stereo => mono: use the sum of Left & Right channels
136 /***********************************************************************
139 * Converts a 8 bit sample to a 16 bit one
141 static inline short C816(unsigned char b
)
143 return (b
- 128) << 8;
146 /***********************************************************************
149 * Converts a 16 bit sample to a 8 bit one (data loss !!)
151 static inline unsigned char C168(short s
)
153 return HIBYTE(s
) ^ (unsigned char)0x80;
156 /***********************************************************************
159 * Converts a 24 bit sample to a 8 bit one (data loss !!)
161 static inline unsigned char C248(int s
)
163 return HIBYTE(HIWORD(s
)) ^ (unsigned char)0x80;
166 /***********************************************************************
169 * Converts a 24 bit sample to a 16 bit one (data loss !!)
171 static inline short C2416(int s
)
176 /***********************************************************************
179 * Read a 16 bit sample (correctly handles endianness)
181 static inline short R16(const unsigned char* src
)
183 return (short)((unsigned short)src
[0] | ((unsigned short)src
[1] << 8));
186 /***********************************************************************
189 * Read a 24 bit sample (correctly handles endianness)
190 * Note, to support signed arithmetic, the values are shifted high in the int
191 * and low 8 bytes are unused.
193 static inline int R24(const unsigned char* src
)
195 return ((int)src
[0] | (int)src
[1] << 8 | (int)src
[2] << 16) << 8;
198 /***********************************************************************
201 * Write a 16 bit sample (correctly handles endianness)
203 static inline void W16(unsigned char* dst
, short s
)
209 /***********************************************************************
212 * Write a 24 bit sample (correctly handles endianness)
214 static inline void W24(unsigned char* dst
, int s
)
216 dst
[0] = HIBYTE(LOWORD(s
));
217 dst
[1] = LOBYTE(HIWORD(s
));
218 dst
[2] = HIBYTE(HIWORD(s
));
221 /***********************************************************************
224 * Convert the (l,r) 24 bit stereo sample into a 24 bit mono
225 * (takes the sum of the two values)
227 static inline int M24(int l
, int r
)
229 LONGLONG sum
= l
+ r
;
231 /* clip sum to saturation */
232 if (sum
> 0x7fffff00)
234 else if (sum
< -0x7fffff00)
240 /***********************************************************************
243 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
244 * (takes the sum of the two values)
246 static inline short M16(short l
, short r
)
250 /* clip sum to saturation */
253 else if (sum
< -32768)
259 /***********************************************************************
262 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
263 * (takes the sum of the two values)
265 static inline unsigned char M8(unsigned char a
, unsigned char b
)
269 int sum
= (l
+ r
) + 128;
271 /* clip sum to saturation */
280 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
282 * <X> is the (M)ono/(S)tereo configuration of input channel
283 * <Y> is the (M)ono/(S)tereo configuration of output channel
284 * <N> is the number of bits of input channel (8 or 16)
285 * <M> is the number of bits of output channel (8 or 16)
287 * in the parameters, ns is always the number of samples, so the size of input
288 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
291 static void cvtMM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
293 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
294 memcpy(dst
, src
, ns
);
297 static void cvtSS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
299 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
300 memcpy(dst
, src
, ns
* 2);
303 static void cvtMM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
305 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
306 memcpy(dst
, src
, ns
* 2);
309 static void cvtSS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
311 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
312 memcpy(dst
, src
, ns
* 4);
315 static void cvtMS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
317 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
325 static void cvtMS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
328 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
332 W16(dst
, v
); dst
+= 2;
333 W16(dst
, v
); dst
+= 2;
337 static void cvtMS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
340 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
343 v
= C168(R16(src
)); src
+= 2;
349 static void cvtMS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
352 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
355 v
= R16(src
); src
+= 2;
356 W16(dst
, v
); dst
+= 2;
357 W16(dst
, v
); dst
+= 2;
361 static void cvtSM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
363 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
366 *dst
++ = M8(src
[0], src
[1]);
371 static void cvtSM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
374 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
377 v
= M16(C816(src
[0]), C816(src
[1]));
379 W16(dst
, v
); dst
+= 2;
383 static void cvtSM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
385 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
388 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
393 static void cvtSM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
395 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
398 W16(dst
, M16(R16(src
),R16(src
+2))); dst
+= 2;
403 static void cvtMM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
405 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
408 W16(dst
, C816(*src
++)); dst
+= 2;
412 static void cvtSS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
414 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
417 W16(dst
, C816(*src
++)); dst
+= 2;
418 W16(dst
, C816(*src
++)); dst
+= 2;
422 static void cvtMM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
424 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
427 *dst
++ = C168(R16(src
)); src
+= 2;
431 static void cvtSS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
433 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
436 *dst
++ = C168(R16(src
)); src
+= 2;
437 *dst
++ = C168(R16(src
)); src
+= 2;
441 static void cvtMS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
444 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
447 v
= C248(R24(src
)); src
+= 3;
453 static void cvtSM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
455 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
458 *dst
++ = C248(M24(R24(src
), R24(src
+ 3)));
463 static void cvtMM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
465 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
468 *dst
++ = C248(R24(src
)); src
+= 3;
472 static void cvtSS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
474 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
477 *dst
++ = C248(R24(src
)); src
+= 3;
478 *dst
++ = C248(R24(src
)); src
+= 3;
482 static void cvtMS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
485 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
488 v
= C2416(R24(src
)); src
+= 3;
489 W16(dst
, v
); dst
+= 2;
490 W16(dst
, v
); dst
+= 2;
494 static void cvtSM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
496 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
499 W16(dst
, C2416(M24(R24(src
), R24(src
+ 3))));
505 static void cvtMM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
507 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
510 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
514 static void cvtSS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
516 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
519 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
520 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
525 static const PCM_CONVERT_KEEP_RATE PCM_ConvertKeepRate
[] = {
526 cvtSS88K
, cvtSM88K
, cvtMS88K
, cvtMM88K
,
527 cvtSS816K
, cvtSM816K
, cvtMS816K
, cvtMM816K
,
528 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
529 cvtSS168K
, cvtSM168K
, cvtMS168K
, cvtMM168K
,
530 cvtSS1616K
, cvtSM1616K
, cvtMS1616K
, cvtMM1616K
,
531 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
532 cvtSS248K
, cvtSM248K
, cvtMS248K
, cvtMM248K
,
533 cvtSS2416K
, cvtSM2416K
, cvtMS2416K
, cvtMM2416K
,
534 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->24 */
537 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
539 * <X> is the (M)ono/(S)tereo configuration of input channel
540 * <Y> is the (M)ono/(S)tereo configuration of output channel
541 * <N> is the number of bits of input channel (8 or 16)
542 * <M> is the number of bits of output channel (8 or 16)
546 static void cvtSS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
547 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
549 DWORD error
= srcRate
/ 2;
550 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
552 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
554 while (error
> srcRate
) {
567 static void cvtSM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
568 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
570 DWORD error
= srcRate
/ 2;
571 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
573 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
575 while (error
> srcRate
) {
581 *dst
++ = M8(src
[0], src
[1]);
587 static void cvtMS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
588 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
590 DWORD error
= srcRate
/ 2;
591 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
593 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
595 while (error
> srcRate
) {
608 static void cvtMM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
609 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
611 DWORD error
= srcRate
/ 2;
612 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
614 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
616 while (error
> srcRate
) {
628 static void cvtSS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
629 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
631 DWORD error
= srcRate
/ 2;
632 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
634 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
636 while (error
> srcRate
) {
642 W16(dst
, C816(src
[0])); dst
+= 2;
643 W16(dst
, C816(src
[1])); dst
+= 2;
649 static void cvtSM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
650 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
652 DWORD error
= srcRate
/ 2;
653 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
655 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
657 while (error
> srcRate
) {
663 W16(dst
, M16(C816(src
[0]), C816(src
[1]))); dst
+= 2;
669 static void cvtMS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
670 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
672 DWORD error
= srcRate
/ 2;
673 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
675 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
677 while (error
> srcRate
) {
683 W16(dst
, C816(src
[0])); dst
+= 2;
684 W16(dst
, C816(src
[0])); dst
+= 2;
690 static void cvtMM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
691 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
693 DWORD error
= srcRate
/ 2;
694 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
696 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
698 while (error
> srcRate
) {
704 W16(dst
, C816(src
[0])); dst
+= 2;
710 static void cvtSS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
711 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
713 DWORD error
= srcRate
/ 2;
714 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
716 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
718 while (error
> srcRate
) {
724 *dst
++ = C168(R16(src
));
725 *dst
++ = C168(R16(src
+ 2));
731 static void cvtSM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
732 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
734 DWORD error
= srcRate
/ 2;
735 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
737 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
739 while (error
> srcRate
) {
745 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
751 static void cvtMS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
752 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
754 DWORD error
= srcRate
/ 2;
755 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
757 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
759 while (error
> srcRate
) {
765 *dst
++ = C168(R16(src
));
766 *dst
++ = C168(R16(src
));
772 static void cvtMM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
773 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
775 DWORD error
= srcRate
/ 2;
776 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
778 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
780 while (error
> srcRate
) {
786 *dst
++ = C168(R16(src
));
792 static void cvtSS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
793 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
795 DWORD error
= srcRate
/ 2;
796 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
798 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
800 while (error
> srcRate
) {
806 W16(dst
, R16(src
)); dst
+= 2;
807 W16(dst
, R16(src
+ 2)); dst
+= 2;
813 static void cvtSM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
814 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
816 DWORD error
= srcRate
/ 2;
817 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
819 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
821 while (error
> srcRate
) {
827 W16(dst
, M16(R16(src
), R16(src
+ 2))); dst
+= 2;
833 static void cvtMS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
834 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
836 DWORD error
= srcRate
/ 2;
837 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
839 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
841 while (error
> srcRate
) {
847 W16(dst
, R16(src
)); dst
+= 2;
848 W16(dst
, R16(src
)); dst
+= 2;
854 static void cvtMM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
855 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
857 DWORD error
= srcRate
/ 2;
858 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
860 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
862 while (error
> srcRate
) {
868 W16(dst
, R16(src
)); dst
+= 2;
874 static void cvtSS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
875 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
877 DWORD error
= srcRate
/ 2;
878 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
880 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
882 while (error
> srcRate
) {
888 W24(dst
, R24(src
)); dst
+= 3;
889 W24(dst
, R24(src
+ 3)); dst
+= 3;
895 static void cvtSM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
896 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
898 DWORD error
= srcRate
/ 2;
899 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
901 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
903 while (error
> srcRate
) {
909 W24(dst
, M24(R24(src
), R24(src
+ 3))); dst
+= 3;
915 static void cvtMS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
916 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
918 DWORD error
= srcRate
/ 2;
919 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
921 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
923 while (error
> srcRate
) {
929 W24(dst
, R24(src
)); dst
+= 3;
930 W24(dst
, R24(src
)); dst
+= 3;
936 static void cvtMM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
937 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
939 DWORD error
= srcRate
/ 2;
940 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
942 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
944 while (error
> srcRate
) {
950 W24(dst
, R24(src
)); dst
+= 3;
956 static const PCM_CONVERT_CHANGE_RATE PCM_ConvertChangeRate
[] = {
957 cvtSS88C
, cvtSM88C
, cvtMS88C
, cvtMM88C
,
958 cvtSS816C
, cvtSM816C
, cvtMS816C
, cvtMM816C
,
959 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
960 cvtSS168C
, cvtSM168C
, cvtMS168C
, cvtMM168C
,
961 cvtSS1616C
, cvtSM1616C
, cvtMS1616C
, cvtMM1616C
,
962 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
963 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->8 */
964 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->16 */
965 cvtSS2424C
, cvtSM2424C
, cvtMS2424C
, cvtMM2424C
,
968 /***********************************************************************
972 static LRESULT
PCM_DriverDetails(PACMDRIVERDETAILSW add
)
974 TRACE("(%p)\n", add
);
976 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
977 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
978 add
->wMid
= MM_MICROSOFT
;
979 add
->wPid
= MM_MSFT_ACM_PCM
;
980 add
->vdwACM
= 0x01000000;
981 add
->vdwDriver
= 0x01000000;
982 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
983 add
->cFormatTags
= 1;
984 add
->cFilterTags
= 0;
986 MultiByteToWideChar(CP_ACP
, 0, "MS-PCM", -1, add
->szShortName
, ARRAY_SIZE(add
->szShortName
));
987 MultiByteToWideChar(CP_ACP
, 0, "Wine PCM converter", -1,
988 add
->szLongName
, ARRAY_SIZE(add
->szLongName
));
989 MultiByteToWideChar(CP_ACP
, 0, "Brought to you by the Wine team...", -1,
990 add
->szCopyright
, ARRAY_SIZE(add
->szCopyright
));
991 MultiByteToWideChar(CP_ACP
, 0, "Refer to LICENSE file", -1,
992 add
->szLicensing
, ARRAY_SIZE(add
->szLicensing
) );
993 add
->szFeatures
[0] = 0;
995 return MMSYSERR_NOERROR
;
998 /***********************************************************************
999 * PCM_FormatTagDetails
1002 static LRESULT
PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
1004 TRACE("(%p, %08x)\n", aftd
, dwQuery
);
1007 case ACM_FORMATTAGDETAILSF_INDEX
:
1008 if (aftd
->dwFormatTagIndex
!= 0) {
1009 WARN("not possible\n");
1010 return ACMERR_NOTPOSSIBLE
;
1013 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
1014 if (aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1015 WARN("not possible\n");
1016 return ACMERR_NOTPOSSIBLE
;
1019 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
1020 if (aftd
->dwFormatTag
!= WAVE_FORMAT_UNKNOWN
&&
1021 aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1022 WARN("not possible\n");
1023 return ACMERR_NOTPOSSIBLE
;
1027 WARN("Unsupported query %08x\n", dwQuery
);
1028 return MMSYSERR_NOTSUPPORTED
;
1031 aftd
->dwFormatTagIndex
= 0;
1032 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1033 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
1034 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1035 aftd
->cStandardFormats
= ARRAY_SIZE(PCM_Formats
);
1036 aftd
->szFormatTag
[0] = 0;
1038 return MMSYSERR_NOERROR
;
1041 /***********************************************************************
1045 static LRESULT
PCM_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
1047 TRACE("(%p, %08x)\n", afd
, dwQuery
);
1050 case ACM_FORMATDETAILSF_FORMAT
:
1051 if (PCM_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) {
1052 WARN("not possible\n");
1053 return ACMERR_NOTPOSSIBLE
;
1056 case ACM_FORMATDETAILSF_INDEX
:
1057 assert(afd
->dwFormatIndex
< ARRAY_SIZE(PCM_Formats
));
1058 afd
->pwfx
->wFormatTag
= WAVE_FORMAT_PCM
;
1059 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
1060 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
1061 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
1062 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
1063 * accessible afd->pwfx->cbSize = 0;
1065 afd
->pwfx
->nBlockAlign
=
1066 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
1067 afd
->pwfx
->nAvgBytesPerSec
=
1068 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
1071 WARN("Unsupported query %08x\n", dwQuery
);
1072 return MMSYSERR_NOTSUPPORTED
;
1075 afd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1076 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1077 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
1078 afd
->cbwfx
= sizeof(PCMWAVEFORMAT
);
1080 return MMSYSERR_NOERROR
;
1083 /***********************************************************************
1087 static LRESULT
PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
1089 TRACE("(%p)\n", adfs
);
1091 /* some tests ... */
1092 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
1093 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
1094 PCM_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) {
1095 WARN("not possible\n");
1096 return ACMERR_NOTPOSSIBLE
;
1099 /* is no suggestion for destination, then copy source value */
1100 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
)) {
1101 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
1103 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
)) {
1104 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
1106 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
)) {
1107 adfs
->pwfxDst
->wBitsPerSample
= adfs
->pwfxSrc
->wBitsPerSample
;
1109 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
)) {
1110 if (adfs
->pwfxSrc
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1111 WARN("source format 0x%x not supported\n", adfs
->pwfxSrc
->wFormatTag
);
1112 return ACMERR_NOTPOSSIBLE
;
1114 adfs
->pwfxDst
->wFormatTag
= adfs
->pwfxSrc
->wFormatTag
;
1116 if (adfs
->pwfxDst
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1117 WARN("destination format 0x%x not supported\n", adfs
->pwfxDst
->wFormatTag
);
1118 return ACMERR_NOTPOSSIBLE
;
1121 /* check if result is ok */
1122 if (PCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) {
1123 WARN("not possible\n");
1124 return ACMERR_NOTPOSSIBLE
;
1127 /* recompute other values */
1128 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
1129 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
1131 return MMSYSERR_NOERROR
;
1134 /***********************************************************************
1138 static LRESULT
PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
1144 TRACE("(%p)\n", adsi
);
1146 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
1148 switch(adsi
->pwfxSrc
->wBitsPerSample
){
1156 if (adsi
->pwfxSrc
->nBlockAlign
!= 3 * adsi
->pwfxSrc
->nChannels
) {
1157 FIXME("Source: 24-bit samples must be packed\n");
1158 return MMSYSERR_NOTSUPPORTED
;
1163 FIXME("Unsupported source bit depth: %u\n", adsi
->pwfxSrc
->wBitsPerSample
);
1164 return MMSYSERR_NOTSUPPORTED
;
1167 switch(adsi
->pwfxDst
->wBitsPerSample
){
1174 if (adsi
->pwfxDst
->nBlockAlign
!= 3 * adsi
->pwfxDst
->nChannels
) {
1175 FIXME("Destination: 24-bit samples must be packed\n");
1176 return MMSYSERR_NOTSUPPORTED
;
1181 FIXME("Unsupported destination bit depth: %u\n", adsi
->pwfxDst
->wBitsPerSample
);
1182 return MMSYSERR_NOTSUPPORTED
;
1185 if (adsi
->pwfxSrc
->nChannels
== 1) idx
+= 2;
1187 if (adsi
->pwfxDst
->nChannels
== 1) idx
+= 1;
1189 apd
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData
));
1191 return MMSYSERR_NOMEM
;
1193 if (adsi
->pwfxSrc
->nSamplesPerSec
== adsi
->pwfxDst
->nSamplesPerSec
) {
1195 apd
->cvt
.cvtKeepRate
= PCM_ConvertKeepRate
[idx
];
1197 flags
= PCM_RESAMPLE
;
1198 apd
->cvt
.cvtChangeRate
= PCM_ConvertChangeRate
[idx
];
1201 if(!apd
->cvt
.cvtChangeRate
&& !apd
->cvt
.cvtKeepRate
){
1202 FIXME("Unimplemented conversion from %u -> %u bps\n",
1203 adsi
->pwfxSrc
->wBitsPerSample
,
1204 adsi
->pwfxDst
->wBitsPerSample
);
1205 HeapFree(GetProcessHeap(), 0, apd
);
1206 return MMSYSERR_NOTSUPPORTED
;
1209 adsi
->dwDriver
= (DWORD_PTR
)apd
;
1210 adsi
->fdwDriver
= flags
;
1212 return MMSYSERR_NOERROR
;
1215 /***********************************************************************
1219 static LRESULT
PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
1221 TRACE("(%p)\n", adsi
);
1223 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
1224 return MMSYSERR_NOERROR
;
1227 /***********************************************************************
1231 static inline DWORD
PCM_round(DWORD a
, DWORD b
, DWORD c
)
1234 /* to be sure, always return an entire number of c... */
1235 return ((double)a
* (double)b
+ (double)c
- 1) / (double)c
;
1238 /***********************************************************************
1242 static LRESULT
PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMSIZE adss
)
1244 DWORD srcMask
= ~(adsi
->pwfxSrc
->nBlockAlign
- 1);
1245 DWORD dstMask
= ~(adsi
->pwfxDst
->nBlockAlign
- 1);
1247 TRACE("(%p, %p)\n", adsi
, adss
);
1249 switch (adss
->fdwSize
) {
1250 case ACM_STREAMSIZEF_DESTINATION
:
1251 /* cbDstLength => cbSrcLength */
1252 adss
->cbSrcLength
= PCM_round(adss
->cbDstLength
& dstMask
,
1253 adsi
->pwfxSrc
->nAvgBytesPerSec
,
1254 adsi
->pwfxDst
->nAvgBytesPerSec
) & srcMask
;
1256 case ACM_STREAMSIZEF_SOURCE
:
1257 /* cbSrcLength => cbDstLength */
1258 adss
->cbDstLength
= PCM_round(adss
->cbSrcLength
& srcMask
,
1259 adsi
->pwfxDst
->nAvgBytesPerSec
,
1260 adsi
->pwfxSrc
->nAvgBytesPerSec
) & dstMask
;
1263 WARN("Unsupported query %08x\n", adss
->fdwSize
);
1264 return MMSYSERR_NOTSUPPORTED
;
1266 return MMSYSERR_NOERROR
;
1269 /***********************************************************************
1273 static LRESULT
PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
1275 AcmPcmData
* apd
= (AcmPcmData
*)adsi
->dwDriver
;
1276 DWORD nsrc
= NUM_OF(adsh
->cbSrcLength
, adsi
->pwfxSrc
->nBlockAlign
);
1277 DWORD ndst
= NUM_OF(adsh
->cbDstLength
, adsi
->pwfxDst
->nBlockAlign
);
1279 TRACE("(%p, %p)\n", adsi
, adsh
);
1281 TRACE("nsrc=%d,adsh->cbSrcLength=%d\n", nsrc
, adsh
->cbSrcLength
);
1282 TRACE("ndst=%d,adsh->cbDstLength=%d\n", ndst
, adsh
->cbDstLength
);
1283 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1284 adsi
->pwfxSrc
->wFormatTag
, adsi
->pwfxSrc
->nChannels
, adsi
->pwfxSrc
->nSamplesPerSec
, adsi
->pwfxSrc
->nAvgBytesPerSec
,
1285 adsi
->pwfxSrc
->nBlockAlign
, adsi
->pwfxSrc
->wBitsPerSample
, adsi
->pwfxSrc
->cbSize
);
1286 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1287 adsi
->pwfxDst
->wFormatTag
, adsi
->pwfxDst
->nChannels
, adsi
->pwfxDst
->nSamplesPerSec
, adsi
->pwfxDst
->nAvgBytesPerSec
,
1288 adsi
->pwfxDst
->nBlockAlign
, adsi
->pwfxDst
->wBitsPerSample
, adsi
->pwfxDst
->cbSize
);
1290 if (adsh
->fdwConvert
&
1291 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
1292 ACM_STREAMCONVERTF_END
|
1293 ACM_STREAMCONVERTF_START
)) {
1294 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh
->fdwConvert
);
1296 /* ACM_STREAMCONVERTF_BLOCKALIGN
1297 * currently all conversions are block aligned, so do nothing for this flag
1298 * ACM_STREAMCONVERTF_END
1299 * no pending data, so do nothing for this flag
1301 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
) &&
1302 (adsi
->fdwDriver
& PCM_RESAMPLE
)) {
1306 if (adsi
->fdwDriver
& PCM_RESAMPLE
) {
1307 apd
->cvt
.cvtChangeRate(adsi
->pwfxSrc
->nSamplesPerSec
, adsh
->pbSrc
, &nsrc
,
1308 adsi
->pwfxDst
->nSamplesPerSec
, adsh
->pbDst
, &ndst
);
1310 if (nsrc
< ndst
) ndst
= nsrc
; else nsrc
= ndst
;
1312 /* nsrc is now equal to ndst */
1313 apd
->cvt
.cvtKeepRate(adsh
->pbSrc
, nsrc
, adsh
->pbDst
);
1316 adsh
->cbSrcLengthUsed
= nsrc
* adsi
->pwfxSrc
->nBlockAlign
;
1317 adsh
->cbDstLengthUsed
= ndst
* adsi
->pwfxDst
->nBlockAlign
;
1319 return MMSYSERR_NOERROR
;
1322 /**************************************************************************
1323 * DriverProc (MSACM32.@)
1325 LRESULT CALLBACK
PCM_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1326 LPARAM dwParam1
, LPARAM dwParam2
)
1328 TRACE("(%08lx %p %u %08lx %08lx);\n",
1329 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1332 case DRV_LOAD
: return 1;
1333 case DRV_FREE
: return 1;
1334 case DRV_OPEN
: return PCM_drvOpen((LPSTR
)dwParam1
, (PACMDRVOPENDESCW
)dwParam2
);
1335 case DRV_CLOSE
: return PCM_drvClose(dwDevID
);
1336 case DRV_ENABLE
: return 1;
1337 case DRV_DISABLE
: return 1;
1338 case DRV_QUERYCONFIGURE
: return 1;
1339 case DRV_CONFIGURE
: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK
); return 1;
1340 case DRV_INSTALL
: return DRVCNF_RESTART
;
1341 case DRV_REMOVE
: return DRVCNF_RESTART
;
1343 case ACMDM_DRIVER_NOTIFY
:
1344 /* no caching from other ACM drivers is done so far */
1345 return MMSYSERR_NOERROR
;
1347 case ACMDM_DRIVER_DETAILS
:
1348 return PCM_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
1350 case ACMDM_FORMATTAG_DETAILS
:
1351 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
1353 case ACMDM_FORMAT_DETAILS
:
1354 return PCM_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
1356 case ACMDM_FORMAT_SUGGEST
:
1357 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
1359 case ACMDM_STREAM_OPEN
:
1360 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
1362 case ACMDM_STREAM_CLOSE
:
1363 return PCM_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
1365 case ACMDM_STREAM_SIZE
:
1366 return PCM_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
1368 case ACMDM_STREAM_CONVERT
:
1369 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
1371 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
1372 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
1373 /* this converter is not a hardware driver */
1374 case ACMDM_FILTERTAG_DETAILS
:
1375 case ACMDM_FILTER_DETAILS
:
1376 /* this converter is not a filter */
1377 case ACMDM_STREAM_RESET
:
1378 /* only needed for asynchronous driver... we aren't, so just say it */
1379 case ACMDM_STREAM_PREPARE
:
1380 case ACMDM_STREAM_UNPREPARE
:
1381 /* nothing special to do here... so don't do anything */
1382 return MMSYSERR_NOTSUPPORTED
;
1385 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);