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_PCM_FORMATS (sizeof(PCM_Formats) / sizeof(PCM_Formats[0]))
75 #define NUM_OF(a,b) ((a)/(b))
77 /* flags for fdwDriver */
78 #define PCM_RESAMPLE 1
80 typedef void (*PCM_CONVERT_KEEP_RATE
)(const unsigned char*, int, unsigned char*);
82 typedef void (*PCM_CONVERT_CHANGE_RATE
)(const DWORD
, const unsigned char*, DWORD
*, const DWORD
, unsigned char*, DWORD
*);
84 /* data used while converting */
85 typedef struct tagAcmPcmData
{
86 /* conversion routine, depending if rate conversion is required */
88 PCM_CONVERT_KEEP_RATE cvtKeepRate
;
89 PCM_CONVERT_CHANGE_RATE cvtChangeRate
;
93 /* table to list all supported formats... those are the basic ones. this
94 * also helps given a unique index to each of the supported formats
101 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000}, {1, 24, 8000}, {2, 24, 8000},
102 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025}, {1, 24, 11025}, {2, 24, 11025},
103 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050}, {1, 24, 22050}, {2, 24, 22050},
104 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100}, {1, 24, 44100}, {2, 24, 44100},
105 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000}, {1, 24, 48000}, {2, 24, 48000},
106 {1, 8, 96000}, {2, 8, 96000}, {1, 16, 96000}, {2, 16, 96000}, {1, 24, 96000}, {2, 24, 96000},
109 /***********************************************************************
112 static DWORD
PCM_GetFormatIndex(LPWAVEFORMATEX wfx
)
115 TRACE("(%p)\n", wfx
);
117 for (i
= 0; i
< NUM_PCM_FORMATS
; i
++) {
118 if (wfx
->nChannels
== PCM_Formats
[i
].nChannels
&&
119 wfx
->nSamplesPerSec
== PCM_Formats
[i
].rate
&&
120 wfx
->wBitsPerSample
== PCM_Formats
[i
].nBits
)
129 * + 8 bit unsigned vs 16 bit signed
130 * + mono vs stereo (1 or 2 channels)
131 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo
132 * shall work in all cases)
134 * mono => stereo: copy the same sample on Left & Right channels
135 * stereo => mono: use the sum of Left & Right channels
138 /***********************************************************************
141 * Converts a 8 bit sample to a 16 bit one
143 static inline short C816(unsigned char b
)
145 return (b
- 128) << 8;
148 /***********************************************************************
151 * Converts a 16 bit sample to a 8 bit one (data loss !!)
153 static inline unsigned char C168(short s
)
155 return HIBYTE(s
) ^ (unsigned char)0x80;
158 /***********************************************************************
161 * Converts a 24 bit sample to a 8 bit one (data loss !!)
163 static inline unsigned char C248(int s
)
165 return HIBYTE(HIWORD(s
)) ^ (unsigned char)0x80;
168 /***********************************************************************
171 * Converts a 24 bit sample to a 16 bit one (data loss !!)
173 static inline short C2416(int s
)
178 /***********************************************************************
181 * Read a 16 bit sample (correctly handles endianness)
183 static inline short R16(const unsigned char* src
)
185 return (short)((unsigned short)src
[0] | ((unsigned short)src
[1] << 8));
188 /***********************************************************************
191 * Read a 24 bit sample (correctly handles endianness)
192 * Note, to support signed arithmetic, the values are shifted high in the int
193 * and low 8 bytes are unused.
195 static inline int R24(const unsigned char* src
)
197 return ((int)src
[0] | (int)src
[1] << 8 | (int)src
[2] << 16) << 8;
200 /***********************************************************************
203 * Write a 16 bit sample (correctly handles endianness)
205 static inline void W16(unsigned char* dst
, short s
)
211 /***********************************************************************
214 * Write a 24 bit sample (correctly handles endianness)
216 static inline void W24(unsigned char* dst
, int s
)
218 dst
[0] = HIBYTE(LOWORD(s
));
219 dst
[1] = LOBYTE(HIWORD(s
));
220 dst
[2] = HIBYTE(HIWORD(s
));
223 /***********************************************************************
226 * Convert the (l,r) 24 bit stereo sample into a 24 bit mono
227 * (takes the sum of the two values)
229 static inline int M24(int l
, int r
)
231 LONGLONG sum
= l
+ r
;
233 /* clip sum to saturation */
234 if (sum
> 0x7fffff00)
236 else if (sum
< -0x7fffff00)
242 /***********************************************************************
245 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
246 * (takes the sum of the two values)
248 static inline short M16(short l
, short r
)
252 /* clip sum to saturation */
255 else if (sum
< -32768)
261 /***********************************************************************
264 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
265 * (takes the sum of the two values)
267 static inline unsigned char M8(unsigned char a
, unsigned char b
)
271 int sum
= (l
+ r
) + 128;
273 /* clip sum to saturation */
282 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
284 * <X> is the (M)ono/(S)tereo configuration of input channel
285 * <Y> is the (M)ono/(S)tereo configuration of output channel
286 * <N> is the number of bits of input channel (8 or 16)
287 * <M> is the number of bits of output channel (8 or 16)
289 * in the parameters, ns is always the number of samples, so the size of input
290 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
293 static void cvtMM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
295 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
296 memcpy(dst
, src
, ns
);
299 static void cvtSS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
301 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
302 memcpy(dst
, src
, ns
* 2);
305 static void cvtMM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
307 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
308 memcpy(dst
, src
, ns
* 2);
311 static void cvtSS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
313 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
314 memcpy(dst
, src
, ns
* 4);
317 static void cvtMS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
319 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
327 static void cvtMS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
330 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
334 W16(dst
, v
); dst
+= 2;
335 W16(dst
, v
); dst
+= 2;
339 static void cvtMS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
342 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
345 v
= C168(R16(src
)); src
+= 2;
351 static void cvtMS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
354 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
357 v
= R16(src
); src
+= 2;
358 W16(dst
, v
); dst
+= 2;
359 W16(dst
, v
); dst
+= 2;
363 static void cvtSM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
365 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
368 *dst
++ = M8(src
[0], src
[1]);
373 static void cvtSM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
376 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
379 v
= M16(C816(src
[0]), C816(src
[1]));
381 W16(dst
, v
); dst
+= 2;
385 static void cvtSM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
387 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
390 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
395 static void cvtSM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
397 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
400 W16(dst
, M16(R16(src
),R16(src
+2))); dst
+= 2;
405 static void cvtMM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
407 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
410 W16(dst
, C816(*src
++)); dst
+= 2;
414 static void cvtSS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
416 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
419 W16(dst
, C816(*src
++)); dst
+= 2;
420 W16(dst
, C816(*src
++)); dst
+= 2;
424 static void cvtMM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
426 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
429 *dst
++ = C168(R16(src
)); src
+= 2;
433 static void cvtSS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
435 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
438 *dst
++ = C168(R16(src
)); src
+= 2;
439 *dst
++ = C168(R16(src
)); src
+= 2;
443 static void cvtMS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
446 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
449 v
= C248(R24(src
)); src
+= 3;
455 static void cvtSM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
457 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
460 *dst
++ = C248(M24(R24(src
), R24(src
+ 3)));
465 static void cvtMM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
467 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
470 *dst
++ = C248(R24(src
)); src
+= 3;
474 static void cvtSS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
476 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
479 *dst
++ = C248(R24(src
)); src
+= 3;
480 *dst
++ = C248(R24(src
)); src
+= 3;
484 static void cvtMS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
487 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
490 v
= C2416(R24(src
)); src
+= 3;
491 W16(dst
, v
); dst
+= 2;
492 W16(dst
, v
); dst
+= 2;
496 static void cvtSM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
498 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
501 W16(dst
, C2416(M24(R24(src
), R24(src
+ 3))));
507 static void cvtMM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
509 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
512 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
516 static void cvtSS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
518 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
521 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
522 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
527 static const PCM_CONVERT_KEEP_RATE PCM_ConvertKeepRate
[] = {
528 cvtSS88K
, cvtSM88K
, cvtMS88K
, cvtMM88K
,
529 cvtSS816K
, cvtSM816K
, cvtMS816K
, cvtMM816K
,
530 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
531 cvtSS168K
, cvtSM168K
, cvtMS168K
, cvtMM168K
,
532 cvtSS1616K
, cvtSM1616K
, cvtMS1616K
, cvtMM1616K
,
533 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
534 cvtSS248K
, cvtSM248K
, cvtMS248K
, cvtMM248K
,
535 cvtSS2416K
, cvtSM2416K
, cvtMS2416K
, cvtMM2416K
,
536 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->24 */
539 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
541 * <X> is the (M)ono/(S)tereo configuration of input channel
542 * <Y> is the (M)ono/(S)tereo configuration of output channel
543 * <N> is the number of bits of input channel (8 or 16)
544 * <M> is the number of bits of output channel (8 or 16)
548 static void cvtSS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
549 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
551 DWORD error
= srcRate
/ 2;
552 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
554 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
556 while (error
> srcRate
) {
569 static void cvtSM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
570 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
572 DWORD error
= srcRate
/ 2;
573 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
575 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
577 while (error
> srcRate
) {
583 *dst
++ = M8(src
[0], src
[1]);
589 static void cvtMS88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
590 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
592 DWORD error
= srcRate
/ 2;
593 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
595 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
597 while (error
> srcRate
) {
610 static void cvtMM88C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
611 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
613 DWORD error
= srcRate
/ 2;
614 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
616 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
618 while (error
> srcRate
) {
630 static void cvtSS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
631 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
633 DWORD error
= srcRate
/ 2;
634 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
636 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
638 while (error
> srcRate
) {
644 W16(dst
, C816(src
[0])); dst
+= 2;
645 W16(dst
, C816(src
[1])); dst
+= 2;
651 static void cvtSM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
652 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
654 DWORD error
= srcRate
/ 2;
655 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
657 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
659 while (error
> srcRate
) {
665 W16(dst
, M16(C816(src
[0]), C816(src
[1]))); dst
+= 2;
671 static void cvtMS816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
672 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
674 DWORD error
= srcRate
/ 2;
675 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
677 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
679 while (error
> srcRate
) {
685 W16(dst
, C816(src
[0])); dst
+= 2;
686 W16(dst
, C816(src
[0])); dst
+= 2;
692 static void cvtMM816C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
693 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
695 DWORD error
= srcRate
/ 2;
696 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
698 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
700 while (error
> srcRate
) {
706 W16(dst
, C816(src
[0])); dst
+= 2;
712 static void cvtSS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
713 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
715 DWORD error
= srcRate
/ 2;
716 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
718 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
720 while (error
> srcRate
) {
726 *dst
++ = C168(R16(src
));
727 *dst
++ = C168(R16(src
+ 2));
733 static void cvtSM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
734 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
736 DWORD error
= srcRate
/ 2;
737 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
739 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
741 while (error
> srcRate
) {
747 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
753 static void cvtMS168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
754 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
756 DWORD error
= srcRate
/ 2;
757 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
759 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
761 while (error
> srcRate
) {
767 *dst
++ = C168(R16(src
));
768 *dst
++ = C168(R16(src
));
774 static void cvtMM168C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
775 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
777 DWORD error
= srcRate
/ 2;
778 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
780 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
782 while (error
> srcRate
) {
788 *dst
++ = C168(R16(src
));
794 static void cvtSS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
795 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
797 DWORD error
= srcRate
/ 2;
798 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
800 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
802 while (error
> srcRate
) {
808 W16(dst
, R16(src
)); dst
+= 2;
809 W16(dst
, R16(src
+ 2)); dst
+= 2;
815 static void cvtSM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
816 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
818 DWORD error
= srcRate
/ 2;
819 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
821 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
823 while (error
> srcRate
) {
829 W16(dst
, M16(R16(src
), R16(src
+ 2))); dst
+= 2;
835 static void cvtMS1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
836 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
838 DWORD error
= srcRate
/ 2;
839 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
841 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
843 while (error
> srcRate
) {
849 W16(dst
, R16(src
)); dst
+= 2;
850 W16(dst
, R16(src
)); dst
+= 2;
856 static void cvtMM1616C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
857 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
859 DWORD error
= srcRate
/ 2;
860 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
862 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
864 while (error
> srcRate
) {
870 W16(dst
, R16(src
)); dst
+= 2;
876 static void cvtSS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
877 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
879 DWORD error
= srcRate
/ 2;
880 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
882 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
884 while (error
> srcRate
) {
890 W24(dst
, R24(src
)); dst
+= 3;
891 W24(dst
, R24(src
+ 3)); dst
+= 3;
897 static void cvtSM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
898 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
900 DWORD error
= srcRate
/ 2;
901 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
903 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
905 while (error
> srcRate
) {
911 W24(dst
, M24(R24(src
), R24(src
+ 3))); dst
+= 3;
917 static void cvtMS2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
918 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
920 DWORD error
= srcRate
/ 2;
921 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
923 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
925 while (error
> srcRate
) {
931 W24(dst
, R24(src
)); dst
+= 3;
932 W24(dst
, R24(src
)); dst
+= 3;
938 static void cvtMM2424C(const DWORD srcRate
, const unsigned char *src
, DWORD
*nsrc
,
939 const DWORD dstRate
, unsigned char *dst
, DWORD
*ndst
)
941 DWORD error
= srcRate
/ 2;
942 DWORD maxSrc
= *nsrc
, maxDst
= *ndst
;
944 for (*nsrc
= 0; *nsrc
< maxSrc
; (*nsrc
)++) {
946 while (error
> srcRate
) {
952 W24(dst
, R24(src
)); dst
+= 3;
958 static const PCM_CONVERT_CHANGE_RATE PCM_ConvertChangeRate
[] = {
959 cvtSS88C
, cvtSM88C
, cvtMS88C
, cvtMM88C
,
960 cvtSS816C
, cvtSM816C
, cvtMS816C
, cvtMM816C
,
961 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
962 cvtSS168C
, cvtSM168C
, cvtMS168C
, cvtMM168C
,
963 cvtSS1616C
, cvtSM1616C
, cvtMS1616C
, cvtMM1616C
,
964 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
965 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->8 */
966 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->16 */
967 cvtSS2424C
, cvtSM2424C
, cvtMS2424C
, cvtMM2424C
,
970 /***********************************************************************
974 static LRESULT
PCM_DriverDetails(PACMDRIVERDETAILSW add
)
976 TRACE("(%p)\n", add
);
978 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
979 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
980 add
->wMid
= MM_MICROSOFT
;
981 add
->wPid
= MM_MSFT_ACM_PCM
;
982 add
->vdwACM
= 0x01000000;
983 add
->vdwDriver
= 0x01000000;
984 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
985 add
->cFormatTags
= 1;
986 add
->cFilterTags
= 0;
988 MultiByteToWideChar( CP_ACP
, 0, "MS-PCM", -1,
989 add
->szShortName
, sizeof(add
->szShortName
)/sizeof(WCHAR
) );
990 MultiByteToWideChar( CP_ACP
, 0, "Wine PCM converter", -1,
991 add
->szLongName
, sizeof(add
->szLongName
)/sizeof(WCHAR
) );
992 MultiByteToWideChar( CP_ACP
, 0, "Brought to you by the Wine team...", -1,
993 add
->szCopyright
, sizeof(add
->szCopyright
)/sizeof(WCHAR
) );
994 MultiByteToWideChar( CP_ACP
, 0, "Refer to LICENSE file", -1,
995 add
->szLicensing
, sizeof(add
->szLicensing
)/sizeof(WCHAR
) );
996 add
->szFeatures
[0] = 0;
998 return MMSYSERR_NOERROR
;
1001 /***********************************************************************
1002 * PCM_FormatTagDetails
1005 static LRESULT
PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
1007 TRACE("(%p, %08x)\n", aftd
, dwQuery
);
1010 case ACM_FORMATTAGDETAILSF_INDEX
:
1011 if (aftd
->dwFormatTagIndex
!= 0) {
1012 WARN("not possible\n");
1013 return ACMERR_NOTPOSSIBLE
;
1016 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
1017 if (aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1018 WARN("not possible\n");
1019 return ACMERR_NOTPOSSIBLE
;
1022 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
1023 if (aftd
->dwFormatTag
!= WAVE_FORMAT_UNKNOWN
&&
1024 aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1025 WARN("not possible\n");
1026 return ACMERR_NOTPOSSIBLE
;
1030 WARN("Unsupported query %08x\n", dwQuery
);
1031 return MMSYSERR_NOTSUPPORTED
;
1034 aftd
->dwFormatTagIndex
= 0;
1035 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1036 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
1037 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1038 aftd
->cStandardFormats
= NUM_PCM_FORMATS
;
1039 aftd
->szFormatTag
[0] = 0;
1041 return MMSYSERR_NOERROR
;
1044 /***********************************************************************
1048 static LRESULT
PCM_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
1050 TRACE("(%p, %08x)\n", afd
, dwQuery
);
1053 case ACM_FORMATDETAILSF_FORMAT
:
1054 if (PCM_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) {
1055 WARN("not possible\n");
1056 return ACMERR_NOTPOSSIBLE
;
1059 case ACM_FORMATDETAILSF_INDEX
:
1060 assert(afd
->dwFormatIndex
< NUM_PCM_FORMATS
);
1061 afd
->pwfx
->wFormatTag
= WAVE_FORMAT_PCM
;
1062 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
1063 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
1064 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
1065 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
1066 * accessible afd->pwfx->cbSize = 0;
1068 afd
->pwfx
->nBlockAlign
=
1069 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
1070 afd
->pwfx
->nAvgBytesPerSec
=
1071 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
1074 WARN("Unsupported query %08x\n", dwQuery
);
1075 return MMSYSERR_NOTSUPPORTED
;
1078 afd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1079 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1080 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
1081 afd
->cbwfx
= sizeof(PCMWAVEFORMAT
);
1083 return MMSYSERR_NOERROR
;
1086 /***********************************************************************
1090 static LRESULT
PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
1092 TRACE("(%p)\n", adfs
);
1094 /* some tests ... */
1095 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
1096 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
1097 PCM_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) {
1098 WARN("not possible\n");
1099 return ACMERR_NOTPOSSIBLE
;
1102 /* is no suggestion for destination, then copy source value */
1103 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
)) {
1104 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
1106 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
)) {
1107 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
1109 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
)) {
1110 adfs
->pwfxDst
->wBitsPerSample
= adfs
->pwfxSrc
->wBitsPerSample
;
1112 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
)) {
1113 if (adfs
->pwfxSrc
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1114 WARN("source format 0x%x not supported\n", adfs
->pwfxSrc
->wFormatTag
);
1115 return ACMERR_NOTPOSSIBLE
;
1117 adfs
->pwfxDst
->wFormatTag
= adfs
->pwfxSrc
->wFormatTag
;
1119 if (adfs
->pwfxDst
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1120 WARN("destination format 0x%x not supported\n", adfs
->pwfxDst
->wFormatTag
);
1121 return ACMERR_NOTPOSSIBLE
;
1124 /* check if result is ok */
1125 if (PCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) {
1126 WARN("not possible\n");
1127 return ACMERR_NOTPOSSIBLE
;
1130 /* recompute other values */
1131 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
1132 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
1134 return MMSYSERR_NOERROR
;
1137 /***********************************************************************
1141 static LRESULT
PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
1147 TRACE("(%p)\n", adsi
);
1149 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
1151 switch(adsi
->pwfxSrc
->wBitsPerSample
){
1159 if (adsi
->pwfxSrc
->nBlockAlign
!= 3 * adsi
->pwfxSrc
->nChannels
) {
1160 FIXME("Source: 24-bit samples must be packed\n");
1161 return MMSYSERR_NOTSUPPORTED
;
1166 FIXME("Unsupported source bit depth: %u\n", adsi
->pwfxSrc
->wBitsPerSample
);
1167 return MMSYSERR_NOTSUPPORTED
;
1170 switch(adsi
->pwfxDst
->wBitsPerSample
){
1177 if (adsi
->pwfxDst
->nBlockAlign
!= 3 * adsi
->pwfxDst
->nChannels
) {
1178 FIXME("Destination: 24-bit samples must be packed\n");
1179 return MMSYSERR_NOTSUPPORTED
;
1184 FIXME("Unsupported destination bit depth: %u\n", adsi
->pwfxDst
->wBitsPerSample
);
1185 return MMSYSERR_NOTSUPPORTED
;
1188 if (adsi
->pwfxSrc
->nChannels
== 1) idx
+= 2;
1190 if (adsi
->pwfxDst
->nChannels
== 1) idx
+= 1;
1192 apd
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData
));
1194 return MMSYSERR_NOMEM
;
1196 if (adsi
->pwfxSrc
->nSamplesPerSec
== adsi
->pwfxDst
->nSamplesPerSec
) {
1198 apd
->cvt
.cvtKeepRate
= PCM_ConvertKeepRate
[idx
];
1200 flags
= PCM_RESAMPLE
;
1201 apd
->cvt
.cvtChangeRate
= PCM_ConvertChangeRate
[idx
];
1204 if(!apd
->cvt
.cvtChangeRate
&& !apd
->cvt
.cvtKeepRate
){
1205 FIXME("Unimplemented conversion from %u -> %u bps\n",
1206 adsi
->pwfxSrc
->wBitsPerSample
,
1207 adsi
->pwfxDst
->wBitsPerSample
);
1208 HeapFree(GetProcessHeap(), 0, apd
);
1209 return MMSYSERR_NOTSUPPORTED
;
1212 adsi
->dwDriver
= (DWORD_PTR
)apd
;
1213 adsi
->fdwDriver
= flags
;
1215 return MMSYSERR_NOERROR
;
1218 /***********************************************************************
1222 static LRESULT
PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
1224 TRACE("(%p)\n", adsi
);
1226 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
1227 return MMSYSERR_NOERROR
;
1230 /***********************************************************************
1234 static inline DWORD
PCM_round(DWORD a
, DWORD b
, DWORD c
)
1237 /* to be sure, always return an entire number of c... */
1238 return ((double)a
* (double)b
+ (double)c
- 1) / (double)c
;
1241 /***********************************************************************
1245 static LRESULT
PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMSIZE adss
)
1247 DWORD srcMask
= ~(adsi
->pwfxSrc
->nBlockAlign
- 1);
1248 DWORD dstMask
= ~(adsi
->pwfxDst
->nBlockAlign
- 1);
1250 TRACE("(%p, %p)\n", adsi
, adss
);
1252 switch (adss
->fdwSize
) {
1253 case ACM_STREAMSIZEF_DESTINATION
:
1254 /* cbDstLength => cbSrcLength */
1255 adss
->cbSrcLength
= PCM_round(adss
->cbDstLength
& dstMask
,
1256 adsi
->pwfxSrc
->nAvgBytesPerSec
,
1257 adsi
->pwfxDst
->nAvgBytesPerSec
) & srcMask
;
1259 case ACM_STREAMSIZEF_SOURCE
:
1260 /* cbSrcLength => cbDstLength */
1261 adss
->cbDstLength
= PCM_round(adss
->cbSrcLength
& srcMask
,
1262 adsi
->pwfxDst
->nAvgBytesPerSec
,
1263 adsi
->pwfxSrc
->nAvgBytesPerSec
) & dstMask
;
1266 WARN("Unsupported query %08x\n", adss
->fdwSize
);
1267 return MMSYSERR_NOTSUPPORTED
;
1269 return MMSYSERR_NOERROR
;
1272 /***********************************************************************
1276 static LRESULT
PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
1278 AcmPcmData
* apd
= (AcmPcmData
*)adsi
->dwDriver
;
1279 DWORD nsrc
= NUM_OF(adsh
->cbSrcLength
, adsi
->pwfxSrc
->nBlockAlign
);
1280 DWORD ndst
= NUM_OF(adsh
->cbDstLength
, adsi
->pwfxDst
->nBlockAlign
);
1282 TRACE("(%p, %p)\n", adsi
, adsh
);
1284 TRACE("nsrc=%d,adsh->cbSrcLength=%d\n", nsrc
, adsh
->cbSrcLength
);
1285 TRACE("ndst=%d,adsh->cbDstLength=%d\n", ndst
, adsh
->cbDstLength
);
1286 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1287 adsi
->pwfxSrc
->wFormatTag
, adsi
->pwfxSrc
->nChannels
, adsi
->pwfxSrc
->nSamplesPerSec
, adsi
->pwfxSrc
->nAvgBytesPerSec
,
1288 adsi
->pwfxSrc
->nBlockAlign
, adsi
->pwfxSrc
->wBitsPerSample
, adsi
->pwfxSrc
->cbSize
);
1289 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1290 adsi
->pwfxDst
->wFormatTag
, adsi
->pwfxDst
->nChannels
, adsi
->pwfxDst
->nSamplesPerSec
, adsi
->pwfxDst
->nAvgBytesPerSec
,
1291 adsi
->pwfxDst
->nBlockAlign
, adsi
->pwfxDst
->wBitsPerSample
, adsi
->pwfxDst
->cbSize
);
1293 if (adsh
->fdwConvert
&
1294 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
1295 ACM_STREAMCONVERTF_END
|
1296 ACM_STREAMCONVERTF_START
)) {
1297 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh
->fdwConvert
);
1299 /* ACM_STREAMCONVERTF_BLOCKALIGN
1300 * currently all conversions are block aligned, so do nothing for this flag
1301 * ACM_STREAMCONVERTF_END
1302 * no pending data, so do nothing for this flag
1304 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
) &&
1305 (adsi
->fdwDriver
& PCM_RESAMPLE
)) {
1309 if (adsi
->fdwDriver
& PCM_RESAMPLE
) {
1310 apd
->cvt
.cvtChangeRate(adsi
->pwfxSrc
->nSamplesPerSec
, adsh
->pbSrc
, &nsrc
,
1311 adsi
->pwfxDst
->nSamplesPerSec
, adsh
->pbDst
, &ndst
);
1313 if (nsrc
< ndst
) ndst
= nsrc
; else nsrc
= ndst
;
1315 /* nsrc is now equal to ndst */
1316 apd
->cvt
.cvtKeepRate(adsh
->pbSrc
, nsrc
, adsh
->pbDst
);
1319 adsh
->cbSrcLengthUsed
= nsrc
* adsi
->pwfxSrc
->nBlockAlign
;
1320 adsh
->cbDstLengthUsed
= ndst
* adsi
->pwfxDst
->nBlockAlign
;
1322 return MMSYSERR_NOERROR
;
1325 /**************************************************************************
1326 * DriverProc (MSACM32.@)
1328 LRESULT CALLBACK
PCM_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1329 LPARAM dwParam1
, LPARAM dwParam2
)
1331 TRACE("(%08lx %p %u %08lx %08lx);\n",
1332 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1335 case DRV_LOAD
: return 1;
1336 case DRV_FREE
: return 1;
1337 case DRV_OPEN
: return PCM_drvOpen((LPSTR
)dwParam1
, (PACMDRVOPENDESCW
)dwParam2
);
1338 case DRV_CLOSE
: return PCM_drvClose(dwDevID
);
1339 case DRV_ENABLE
: return 1;
1340 case DRV_DISABLE
: return 1;
1341 case DRV_QUERYCONFIGURE
: return 1;
1342 case DRV_CONFIGURE
: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK
); return 1;
1343 case DRV_INSTALL
: return DRVCNF_RESTART
;
1344 case DRV_REMOVE
: return DRVCNF_RESTART
;
1346 case ACMDM_DRIVER_NOTIFY
:
1347 /* no caching from other ACM drivers is done so far */
1348 return MMSYSERR_NOERROR
;
1350 case ACMDM_DRIVER_DETAILS
:
1351 return PCM_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
1353 case ACMDM_FORMATTAG_DETAILS
:
1354 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
1356 case ACMDM_FORMAT_DETAILS
:
1357 return PCM_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
1359 case ACMDM_FORMAT_SUGGEST
:
1360 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
1362 case ACMDM_STREAM_OPEN
:
1363 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
1365 case ACMDM_STREAM_CLOSE
:
1366 return PCM_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
1368 case ACMDM_STREAM_SIZE
:
1369 return PCM_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
1371 case ACMDM_STREAM_CONVERT
:
1372 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
1374 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
1375 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
1376 /* this converter is not a hardware driver */
1377 case ACMDM_FILTERTAG_DETAILS
:
1378 case ACMDM_FILTER_DETAILS
:
1379 /* this converter is not a filter */
1380 case ACMDM_STREAM_RESET
:
1381 /* only needed for asynchronous driver... we aren't, so just say it */
1382 case ACMDM_STREAM_PREPARE
:
1383 case ACMDM_STREAM_UNPREPARE
:
1384 /* nothing special to do here... so don't do anything */
1385 return MMSYSERR_NOTSUPPORTED
;
1388 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);