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
33 /***********************************************************************
36 static DWORD
PCM_drvOpen(LPCSTR str
, PACMDRVOPENDESCW adod
)
38 TRACE("(%p, %p)\n", str
, adod
);
40 return (adod
== NULL
) ||
41 (adod
->fccType
== ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
&&
42 adod
->fccComp
== ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
);
45 /***********************************************************************
48 static DWORD
PCM_drvClose(DWORD dwDevID
)
50 TRACE("(%d)\n", dwDevID
);
55 #define NUM_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
56 #define NUM_OF(a,b) ((a)/(b))
58 /* flags for fdwDriver */
59 #define PCM_RESAMPLE 1
61 typedef void (*PCM_CONVERT_KEEP_RATE
)(const unsigned char*, int, unsigned char*);
63 typedef void (*PCM_CONVERT_CHANGE_RATE
)(const DWORD
, const unsigned char*, DWORD
*, const DWORD
, unsigned char*, DWORD
*);
65 /* data used while converting */
66 typedef struct tagAcmPcmData
{
67 /* conversion routine, depending if rate conversion is required */
69 PCM_CONVERT_KEEP_RATE cvtKeepRate
;
70 PCM_CONVERT_CHANGE_RATE cvtChangeRate
;
74 /* table to list all supported formats... those are the basic ones. this
75 * also helps given a unique index to each of the supported formats
82 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000}, {1, 24, 8000}, {2, 24, 8000},
83 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025}, {1, 24, 11025}, {2, 24, 11025},
84 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050}, {1, 24, 22050}, {2, 24, 22050},
85 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100}, {1, 24, 44100}, {2, 24, 44100},
86 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000}, {1, 24, 48000}, {2, 24, 48000},
87 {1, 8, 96000}, {2, 8, 96000}, {1, 16, 96000}, {2, 16, 96000}, {1, 24, 96000}, {2, 24, 96000},
90 /***********************************************************************
93 static DWORD
PCM_GetFormatIndex(LPWAVEFORMATEX wfx
)
98 for (i
= 0; i
< NUM_PCM_FORMATS
; i
++) {
99 if (wfx
->nChannels
== PCM_Formats
[i
].nChannels
&&
100 wfx
->nSamplesPerSec
== PCM_Formats
[i
].rate
&&
101 wfx
->wBitsPerSample
== PCM_Formats
[i
].nBits
)
110 * + 8 bit unsigned vs 16 bit signed
111 * + mono vs stereo (1 or 2 channels)
112 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo
113 * shall work in all cases)
115 * mono => stereo: copy the same sample on Left & Right channels
116 * stereo => mono: use the sum of Left & Right channels
119 /***********************************************************************
122 * Converts a 8 bit sample to a 16 bit one
124 static inline short C816(unsigned char b
)
126 return (b
- 128) << 8;
129 /***********************************************************************
132 * Converts a 16 bit sample to a 8 bit one (data loss !!)
134 static inline unsigned char C168(short s
)
136 return HIBYTE(s
) ^ (unsigned char)0x80;
139 /***********************************************************************
142 * Converts a 24 bit sample to a 8 bit one (data loss !!)
144 static inline unsigned char C248(int s
)
146 return HIBYTE(HIWORD(s
)) ^ (unsigned char)0x80;
149 /***********************************************************************
152 * Converts a 24 bit sample to a 16 bit one (data loss !!)
154 static inline short C2416(int s
)
159 /***********************************************************************
162 * Read a 16 bit sample (correctly handles endianness)
164 static inline short R16(const unsigned char* src
)
166 return (short)((unsigned short)src
[0] | ((unsigned short)src
[1] << 8));
169 /***********************************************************************
172 * Read a 24 bit sample (correctly handles endianness)
173 * Note, to support signed arithmetic, the values are shifted high in the int
174 * and low 8 bytes are unused.
176 static inline int R24(const unsigned char* src
)
178 return ((int)src
[0] | (int)src
[1] << 8 | (int)src
[2] << 16) << 8;
181 /***********************************************************************
184 * Write a 16 bit sample (correctly handles endianness)
186 static inline void W16(unsigned char* dst
, short s
)
192 /***********************************************************************
195 * Write a 24 bit sample (correctly handles endianness)
197 static inline void W24(unsigned char* dst
, int s
)
199 dst
[0] = HIBYTE(LOWORD(s
));
200 dst
[1] = LOBYTE(HIWORD(s
));
201 dst
[2] = HIBYTE(HIWORD(s
));
204 /***********************************************************************
207 * Convert the (l,r) 24 bit stereo sample into a 24 bit mono
208 * (takes the sum of the two values)
210 static inline int M24(int l
, int r
)
212 LONGLONG sum
= l
+ r
;
214 /* clip sum to saturation */
215 if (sum
> 0x7fffff00)
217 else if (sum
< -0x7fffff00)
223 /***********************************************************************
226 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
227 * (takes the sum of the two values)
229 static inline short M16(short l
, short r
)
233 /* clip sum to saturation */
236 else if (sum
< -32768)
242 /***********************************************************************
245 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
246 * (takes the sum of the two values)
248 static inline unsigned char M8(unsigned char a
, unsigned char b
)
252 int sum
= (l
+ r
) + 128;
254 /* clip sum to saturation */
263 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
265 * <X> is the (M)ono/(S)tereo configuration of input channel
266 * <Y> is the (M)ono/(S)tereo configuration of output channel
267 * <N> is the number of bits of input channel (8 or 16)
268 * <M> is the number of bits of output channel (8 or 16)
270 * in the parameters, ns is always the number of samples, so the size of input
271 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
274 static void cvtMM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
276 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
277 memcpy(dst
, src
, ns
);
280 static void cvtSS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
282 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
283 memcpy(dst
, src
, ns
* 2);
286 static void cvtMM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
288 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
289 memcpy(dst
, src
, ns
* 2);
292 static void cvtSS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
294 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
295 memcpy(dst
, src
, ns
* 4);
298 static void cvtMS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
300 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
308 static void cvtMS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
311 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
315 W16(dst
, v
); dst
+= 2;
316 W16(dst
, v
); dst
+= 2;
320 static void cvtMS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
323 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
326 v
= C168(R16(src
)); src
+= 2;
332 static void cvtMS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
335 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
338 v
= R16(src
); src
+= 2;
339 W16(dst
, v
); dst
+= 2;
340 W16(dst
, v
); dst
+= 2;
344 static void cvtSM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
346 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
349 *dst
++ = M8(src
[0], src
[1]);
354 static void cvtSM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
357 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
360 v
= M16(C816(src
[0]), C816(src
[1]));
362 W16(dst
, v
); dst
+= 2;
366 static void cvtSM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
368 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
371 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
376 static void cvtSM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
378 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
381 W16(dst
, M16(R16(src
),R16(src
+2))); dst
+= 2;
386 static void cvtMM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
388 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
391 W16(dst
, C816(*src
++)); dst
+= 2;
395 static void cvtSS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
397 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
400 W16(dst
, C816(*src
++)); dst
+= 2;
401 W16(dst
, C816(*src
++)); dst
+= 2;
405 static void cvtMM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
407 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
410 *dst
++ = C168(R16(src
)); src
+= 2;
414 static void cvtSS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
416 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
419 *dst
++ = C168(R16(src
)); src
+= 2;
420 *dst
++ = C168(R16(src
)); src
+= 2;
424 static void cvtMS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
427 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
430 v
= C248(R24(src
)); src
+= 3;
436 static void cvtSM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
438 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
441 *dst
++ = C248(M24(R24(src
), R24(src
+ 3)));
446 static void cvtMM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
448 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
451 *dst
++ = C248(R24(src
)); src
+= 3;
455 static void cvtSS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
457 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
460 *dst
++ = C248(R24(src
)); src
+= 3;
461 *dst
++ = C248(R24(src
)); src
+= 3;
465 static void cvtMS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
468 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
471 v
= C2416(R24(src
)); src
+= 3;
472 W16(dst
, v
); dst
+= 2;
473 W16(dst
, v
); dst
+= 2;
477 static void cvtSM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
479 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
482 W16(dst
, C2416(M24(R24(src
), R24(src
+ 3))));
488 static void cvtMM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
490 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
493 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
497 static void cvtSS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
499 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
502 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
503 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
508 static const PCM_CONVERT_KEEP_RATE PCM_ConvertKeepRate
[] = {
509 cvtSS88K
, cvtSM88K
, cvtMS88K
, cvtMM88K
,
510 cvtSS816K
, cvtSM816K
, cvtMS816K
, cvtMM816K
,
511 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
512 cvtSS168K
, cvtSM168K
, cvtMS168K
, cvtMM168K
,
513 cvtSS1616K
, cvtSM1616K
, cvtMS1616K
, cvtMM1616K
,
514 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
515 cvtSS248K
, cvtSM248K
, cvtMS248K
, cvtMM248K
,
516 cvtSS2416K
, cvtSM2416K
, cvtMS2416K
, cvtMM2416K
,
517 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->24 */
520 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
522 * <X> is the (M)ono/(S)tereo configuration of input channel
523 * <Y> is the (M)ono/(S)tereo configuration of output channel
524 * <N> is the number of bits of input channel (8 or 16)
525 * <M> is the number of bits of output channel (8 or 16)
529 static void cvtSS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
530 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
532 DWORD error
= srcRate
/ 2;
533 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
535 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
537 while (error
> srcRate
) {
550 static void cvtSM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
551 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
553 DWORD error
= srcRate
/ 2;
554 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
556 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
558 while (error
> srcRate
) {
564 *dst
++ = M8(src
[0], src
[1]);
570 static void cvtMS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
571 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
573 DWORD error
= srcRate
/ 2;
574 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
576 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
578 while (error
> srcRate
) {
591 static void cvtMM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
592 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
594 DWORD error
= srcRate
/ 2;
595 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
597 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
599 while (error
> srcRate
) {
611 static void cvtSS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
612 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
614 DWORD error
= srcRate
/ 2;
615 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
617 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
619 while (error
> srcRate
) {
625 W16(dst
, C816(src
[0])); dst
+= 2;
626 W16(dst
, C816(src
[1])); dst
+= 2;
632 static void cvtSM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
633 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
635 DWORD error
= srcRate
/ 2;
636 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
638 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
640 while (error
> srcRate
) {
646 W16(dst
, M16(C816(src
[0]), C816(src
[1]))); dst
+= 2;
652 static void cvtMS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
653 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
655 DWORD error
= srcRate
/ 2;
656 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
658 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
660 while (error
> srcRate
) {
666 W16(dst
, C816(src
[0])); dst
+= 2;
667 W16(dst
, C816(src
[0])); dst
+= 2;
673 static void cvtMM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
674 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
676 DWORD error
= srcRate
/ 2;
677 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
679 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
681 while (error
> srcRate
) {
687 W16(dst
, C816(src
[0])); dst
+= 2;
693 static void cvtSS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
694 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
696 DWORD error
= srcRate
/ 2;
697 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
699 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
701 while (error
> srcRate
) {
707 *dst
++ = C168(R16(src
));
708 *dst
++ = C168(R16(src
+ 2));
714 static void cvtSM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
715 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
717 DWORD error
= srcRate
/ 2;
718 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
720 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
722 while (error
> srcRate
) {
728 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
734 static void cvtMS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
735 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
737 DWORD error
= srcRate
/ 2;
738 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
740 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
742 while (error
> srcRate
) {
748 *dst
++ = C168(R16(src
));
749 *dst
++ = C168(R16(src
));
755 static void cvtMM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
756 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
758 DWORD error
= srcRate
/ 2;
759 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
761 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
763 while (error
> srcRate
) {
769 *dst
++ = C168(R16(src
));
775 static void cvtSS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
776 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
778 DWORD error
= srcRate
/ 2;
779 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
781 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
783 while (error
> srcRate
) {
789 W16(dst
, R16(src
)); dst
+= 2;
790 W16(dst
, R16(src
+ 2)); dst
+= 2;
796 static void cvtSM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
797 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
799 DWORD error
= srcRate
/ 2;
800 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
802 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
804 while (error
> srcRate
) {
810 W16(dst
, M16(R16(src
), R16(src
+ 2))); dst
+= 2;
816 static void cvtMS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
817 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
819 DWORD error
= srcRate
/ 2;
820 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
822 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
824 while (error
> srcRate
) {
830 W16(dst
, R16(src
)); dst
+= 2;
831 W16(dst
, R16(src
)); dst
+= 2;
837 static void cvtMM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
838 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
840 DWORD error
= srcRate
/ 2;
841 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
843 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
845 while (error
> srcRate
) {
851 W16(dst
, R16(src
)); dst
+= 2;
857 static void cvtSS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
858 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
860 DWORD error
= srcRate
/ 2;
861 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
863 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
865 while (error
> srcRate
) {
871 W24(dst
, R24(src
)); dst
+= 3;
872 W24(dst
, R24(src
+ 3)); dst
+= 3;
878 static void cvtSM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
879 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
881 DWORD error
= srcRate
/ 2;
882 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
884 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
886 while (error
> srcRate
) {
892 W24(dst
, M24(R24(src
), R24(src
+ 3))); dst
+= 3;
898 static void cvtMS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
899 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
901 DWORD error
= srcRate
/ 2;
902 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
904 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
906 while (error
> srcRate
) {
912 W24(dst
, R24(src
)); dst
+= 3;
913 W24(dst
, R24(src
)); dst
+= 3;
919 static void cvtMM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
920 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
922 DWORD error
= srcRate
/ 2;
923 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
925 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
927 while (error
> srcRate
) {
933 W24(dst
, R24(src
)); dst
+= 3;
939 static const PCM_CONVERT_CHANGE_RATE PCM_ConvertChangeRate
[] = {
940 cvtSS88C
, cvtSM88C
, cvtMS88C
, cvtMM88C
,
941 cvtSS816C
, cvtSM816C
, cvtMS816C
, cvtMM816C
,
942 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
943 cvtSS168C
, cvtSM168C
, cvtMS168C
, cvtMM168C
,
944 cvtSS1616C
, cvtSM1616C
, cvtMS1616C
, cvtMM1616C
,
945 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
946 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->8 */
947 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->16 */
948 cvtSS2424C
, cvtSM2424C
, cvtMS2424C
, cvtMM2424C
,
951 /***********************************************************************
955 static LRESULT
PCM_DriverDetails(PACMDRIVERDETAILSW add
)
957 TRACE("(%p)\n", add
);
959 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
960 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
961 add
->wMid
= MM_MICROSOFT
;
962 add
->wPid
= MM_MSFT_ACM_PCM
;
963 add
->vdwACM
= 0x01000000;
964 add
->vdwDriver
= 0x01000000;
965 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
966 add
->cFormatTags
= 1;
967 add
->cFilterTags
= 0;
969 MultiByteToWideChar( CP_ACP
, 0, "MS-PCM", -1,
970 add
->szShortName
, sizeof(add
->szShortName
)/sizeof(WCHAR
) );
971 MultiByteToWideChar( CP_ACP
, 0, "Wine PCM converter", -1,
972 add
->szLongName
, sizeof(add
->szLongName
)/sizeof(WCHAR
) );
973 MultiByteToWideChar( CP_ACP
, 0, "Brought to you by the Wine team...", -1,
974 add
->szCopyright
, sizeof(add
->szCopyright
)/sizeof(WCHAR
) );
975 MultiByteToWideChar( CP_ACP
, 0, "Refer to LICENSE file", -1,
976 add
->szLicensing
, sizeof(add
->szLicensing
)/sizeof(WCHAR
) );
977 add
->szFeatures
[0] = 0;
979 return MMSYSERR_NOERROR
;
982 /***********************************************************************
983 * PCM_FormatTagDetails
986 static LRESULT
PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
988 TRACE("(%p, %08x)\n", aftd
, dwQuery
);
991 case ACM_FORMATTAGDETAILSF_INDEX
:
992 if (aftd
->dwFormatTagIndex
!= 0) {
993 WARN("not possible\n");
994 return ACMERR_NOTPOSSIBLE
;
997 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
998 if (aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
999 WARN("not possible\n");
1000 return ACMERR_NOTPOSSIBLE
;
1003 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
1004 if (aftd
->dwFormatTag
!= WAVE_FORMAT_UNKNOWN
&&
1005 aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1006 WARN("not possible\n");
1007 return ACMERR_NOTPOSSIBLE
;
1011 WARN("Unsupported query %08x\n", dwQuery
);
1012 return MMSYSERR_NOTSUPPORTED
;
1015 aftd
->dwFormatTagIndex
= 0;
1016 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1017 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
1018 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1019 aftd
->cStandardFormats
= NUM_PCM_FORMATS
;
1020 aftd
->szFormatTag
[0] = 0;
1022 return MMSYSERR_NOERROR
;
1025 /***********************************************************************
1029 static LRESULT
PCM_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
1031 TRACE("(%p, %08x)\n", afd
, dwQuery
);
1034 case ACM_FORMATDETAILSF_FORMAT
:
1035 if (PCM_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) {
1036 WARN("not possible\n");
1037 return ACMERR_NOTPOSSIBLE
;
1040 case ACM_FORMATDETAILSF_INDEX
:
1041 assert(afd
->dwFormatIndex
< NUM_PCM_FORMATS
);
1042 afd
->pwfx
->wFormatTag
= WAVE_FORMAT_PCM
;
1043 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
1044 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
1045 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
1046 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
1047 * accessible afd->pwfx->cbSize = 0;
1049 afd
->pwfx
->nBlockAlign
=
1050 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
1051 afd
->pwfx
->nAvgBytesPerSec
=
1052 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
1055 WARN("Unsupported query %08x\n", dwQuery
);
1056 return MMSYSERR_NOTSUPPORTED
;
1059 afd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1060 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1061 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
1062 afd
->cbwfx
= sizeof(PCMWAVEFORMAT
);
1064 return MMSYSERR_NOERROR
;
1067 /***********************************************************************
1071 static LRESULT
PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
1073 TRACE("(%p)\n", adfs
);
1075 /* some tests ... */
1076 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
1077 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
1078 PCM_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) {
1079 WARN("not possible\n");
1080 return ACMERR_NOTPOSSIBLE
;
1083 /* is no suggestion for destination, then copy source value */
1084 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
)) {
1085 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
1087 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
)) {
1088 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
1090 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
)) {
1091 adfs
->pwfxDst
->wBitsPerSample
= adfs
->pwfxSrc
->wBitsPerSample
;
1093 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
)) {
1094 if (adfs
->pwfxSrc
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1095 WARN("source format 0x%x not supported\n", adfs
->pwfxSrc
->wFormatTag
);
1096 return ACMERR_NOTPOSSIBLE
;
1098 adfs
->pwfxDst
->wFormatTag
= adfs
->pwfxSrc
->wFormatTag
;
1100 if (adfs
->pwfxDst
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1101 WARN("destination format 0x%x not supported\n", adfs
->pwfxDst
->wFormatTag
);
1102 return ACMERR_NOTPOSSIBLE
;
1105 /* check if result is ok */
1106 if (PCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) {
1107 WARN("not possible\n");
1108 return ACMERR_NOTPOSSIBLE
;
1111 /* recompute other values */
1112 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
1113 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
1115 return MMSYSERR_NOERROR
;
1118 /***********************************************************************
1122 static LRESULT
PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
1128 TRACE("(%p)\n", adsi
);
1130 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
1132 switch(adsi
->pwfxSrc
->wBitsPerSample
){
1140 if (adsi
->pwfxSrc
->nBlockAlign
!= 3 * adsi
->pwfxSrc
->nChannels
) {
1141 FIXME("Source: 24-bit samples must be packed\n");
1142 return MMSYSERR_NOTSUPPORTED
;
1147 FIXME("Unsupported source bit depth: %u\n", adsi
->pwfxSrc
->wBitsPerSample
);
1148 return MMSYSERR_NOTSUPPORTED
;
1151 switch(adsi
->pwfxDst
->wBitsPerSample
){
1158 if (adsi
->pwfxDst
->nBlockAlign
!= 3 * adsi
->pwfxDst
->nChannels
) {
1159 FIXME("Destination: 24-bit samples must be packed\n");
1160 return MMSYSERR_NOTSUPPORTED
;
1165 FIXME("Unsupported destination bit depth: %u\n", adsi
->pwfxDst
->wBitsPerSample
);
1166 return MMSYSERR_NOTSUPPORTED
;
1169 if (adsi
->pwfxSrc
->nChannels
== 1) idx
+= 2;
1171 if (adsi
->pwfxDst
->nChannels
== 1) idx
+= 1;
1173 apd
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData
));
1175 return MMSYSERR_NOMEM
;
1177 if (adsi
->pwfxSrc
->nSamplesPerSec
== adsi
->pwfxDst
->nSamplesPerSec
) {
1179 apd
->cvt
.cvtKeepRate
= PCM_ConvertKeepRate
[idx
];
1181 flags
= PCM_RESAMPLE
;
1182 apd
->cvt
.cvtChangeRate
= PCM_ConvertChangeRate
[idx
];
1185 if(!apd
->cvt
.cvtChangeRate
&& !apd
->cvt
.cvtKeepRate
){
1186 FIXME("Unimplemented conversion from %u -> %u bps\n",
1187 adsi
->pwfxSrc
->wBitsPerSample
,
1188 adsi
->pwfxDst
->wBitsPerSample
);
1189 HeapFree(GetProcessHeap(), 0, apd
);
1190 return MMSYSERR_NOTSUPPORTED
;
1193 adsi
->dwDriver
= (DWORD_PTR
)apd
;
1194 adsi
->fdwDriver
= flags
;
1196 return MMSYSERR_NOERROR
;
1199 /***********************************************************************
1203 static LRESULT
PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
1205 TRACE("(%p)\n", adsi
);
1207 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
1208 return MMSYSERR_NOERROR
;
1211 /***********************************************************************
1215 static inline DWORD
PCM_round(DWORD a
, DWORD b
, DWORD c
)
1218 /* to be sure, always return an entire number of c... */
1219 return ((double)a
* (double)b
+ (double)c
- 1) / (double)c
;
1222 /***********************************************************************
1226 static LRESULT
PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMSIZE adss
)
1228 DWORD srcMask
= ~(adsi
->pwfxSrc
->nBlockAlign
- 1);
1229 DWORD dstMask
= ~(adsi
->pwfxDst
->nBlockAlign
- 1);
1231 TRACE("(%p, %p)\n", adsi
, adss
);
1233 switch (adss
->fdwSize
) {
1234 case ACM_STREAMSIZEF_DESTINATION
:
1235 /* cbDstLength => cbSrcLength */
1236 adss
->cbSrcLength
= PCM_round(adss
->cbDstLength
& dstMask
,
1237 adsi
->pwfxSrc
->nAvgBytesPerSec
,
1238 adsi
->pwfxDst
->nAvgBytesPerSec
) & srcMask
;
1240 case ACM_STREAMSIZEF_SOURCE
:
1241 /* cbSrcLength => cbDstLength */
1242 adss
->cbDstLength
= PCM_round(adss
->cbSrcLength
& srcMask
,
1243 adsi
->pwfxDst
->nAvgBytesPerSec
,
1244 adsi
->pwfxSrc
->nAvgBytesPerSec
) & dstMask
;
1247 WARN("Unsupported query %08x\n", adss
->fdwSize
);
1248 return MMSYSERR_NOTSUPPORTED
;
1250 return MMSYSERR_NOERROR
;
1253 /***********************************************************************
1257 static LRESULT
PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
1259 AcmPcmData
* apd
= (AcmPcmData
*)adsi
->dwDriver
;
1260 DWORD nsrc
= NUM_OF(adsh
->cbSrcLength
, adsi
->pwfxSrc
->nBlockAlign
);
1261 DWORD ndst
= NUM_OF(adsh
->cbDstLength
, adsi
->pwfxDst
->nBlockAlign
);
1263 TRACE("(%p, %p)\n", adsi
, adsh
);
1265 TRACE("nsrc=%d,adsh->cbSrcLength=%d\n", nsrc
, adsh
->cbSrcLength
);
1266 TRACE("ndst=%d,adsh->cbDstLength=%d\n", ndst
, adsh
->cbDstLength
);
1267 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1268 adsi
->pwfxSrc
->wFormatTag
, adsi
->pwfxSrc
->nChannels
, adsi
->pwfxSrc
->nSamplesPerSec
, adsi
->pwfxSrc
->nAvgBytesPerSec
,
1269 adsi
->pwfxSrc
->nBlockAlign
, adsi
->pwfxSrc
->wBitsPerSample
, adsi
->pwfxSrc
->cbSize
);
1270 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1271 adsi
->pwfxDst
->wFormatTag
, adsi
->pwfxDst
->nChannels
, adsi
->pwfxDst
->nSamplesPerSec
, adsi
->pwfxDst
->nAvgBytesPerSec
,
1272 adsi
->pwfxDst
->nBlockAlign
, adsi
->pwfxDst
->wBitsPerSample
, adsi
->pwfxDst
->cbSize
);
1274 if (adsh
->fdwConvert
&
1275 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
1276 ACM_STREAMCONVERTF_END
|
1277 ACM_STREAMCONVERTF_START
)) {
1278 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh
->fdwConvert
);
1280 /* ACM_STREAMCONVERTF_BLOCKALIGN
1281 * currently all conversions are block aligned, so do nothing for this flag
1282 * ACM_STREAMCONVERTF_END
1283 * no pending data, so do nothing for this flag
1285 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
) &&
1286 (adsi
->fdwDriver
& PCM_RESAMPLE
)) {
1290 if (adsi
->fdwDriver
& PCM_RESAMPLE
) {
1291 apd
->cvt
.cvtChangeRate(adsi
->pwfxSrc
->nSamplesPerSec
, adsh
->pbSrc
, &nsrc
,
1292 adsi
->pwfxDst
->nSamplesPerSec
, adsh
->pbDst
, &ndst
);
1294 if (nsrc
< ndst
) ndst
= nsrc
; else nsrc
= ndst
;
1296 /* nsrc is now equal to ndst */
1297 apd
->cvt
.cvtKeepRate(adsh
->pbSrc
, nsrc
, adsh
->pbDst
);
1300 adsh
->cbSrcLengthUsed
= nsrc
* adsi
->pwfxSrc
->nBlockAlign
;
1301 adsh
->cbDstLengthUsed
= ndst
* adsi
->pwfxDst
->nBlockAlign
;
1303 return MMSYSERR_NOERROR
;
1306 /**************************************************************************
1307 * DriverProc (MSACM32.@)
1309 LRESULT CALLBACK
PCM_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1310 LPARAM dwParam1
, LPARAM dwParam2
)
1312 TRACE("(%08lx %p %u %08lx %08lx);\n",
1313 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1316 case DRV_LOAD
: return 1;
1317 case DRV_FREE
: return 1;
1318 case DRV_OPEN
: return PCM_drvOpen((LPSTR
)dwParam1
, (PACMDRVOPENDESCW
)dwParam2
);
1319 case DRV_CLOSE
: return PCM_drvClose(dwDevID
);
1320 case DRV_ENABLE
: return 1;
1321 case DRV_DISABLE
: return 1;
1322 case DRV_QUERYCONFIGURE
: return 1;
1323 case DRV_CONFIGURE
: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK
); return 1;
1324 case DRV_INSTALL
: return DRVCNF_RESTART
;
1325 case DRV_REMOVE
: return DRVCNF_RESTART
;
1327 case ACMDM_DRIVER_NOTIFY
:
1328 /* no caching from other ACM drivers is done so far */
1329 return MMSYSERR_NOERROR
;
1331 case ACMDM_DRIVER_DETAILS
:
1332 return PCM_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
1334 case ACMDM_FORMATTAG_DETAILS
:
1335 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
1337 case ACMDM_FORMAT_DETAILS
:
1338 return PCM_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
1340 case ACMDM_FORMAT_SUGGEST
:
1341 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
1343 case ACMDM_STREAM_OPEN
:
1344 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
1346 case ACMDM_STREAM_CLOSE
:
1347 return PCM_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
1349 case ACMDM_STREAM_SIZE
:
1350 return PCM_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
1352 case ACMDM_STREAM_CONVERT
:
1353 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
1355 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
1356 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
1357 /* this converter is not a hardware driver */
1358 case ACMDM_FILTERTAG_DETAILS
:
1359 case ACMDM_FILTER_DETAILS
:
1360 /* this converter is not a filter */
1361 case ACMDM_STREAM_RESET
:
1362 /* only needed for asynchronous driver... we aren't, so just say it */
1363 case ACMDM_STREAM_PREPARE
:
1364 case ACMDM_STREAM_UNPREPARE
:
1365 /* nothing special to do here... so don't do anything */
1366 return MMSYSERR_NOTSUPPORTED
;
1369 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);