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 /* data used while converting */
81 typedef struct tagAcmPcmData
{
82 /* conversion routine, depending if rate conversion is required */
84 void (*cvtKeepRate
)(const unsigned char*, int, unsigned char*);
85 void (*cvtChangeRate
)(DWORD
, const unsigned char*, LPDWORD
,
86 DWORD
, unsigned char*, LPDWORD
);
90 /* table to list all supported formats... those are the basic ones. this
91 * also helps given a unique index to each of the supported formats
98 {1, 8, 8000}, {2, 8, 8000}, {1, 16, 8000}, {2, 16, 8000}, {1, 24, 8000}, {2, 24, 8000},
99 {1, 8, 11025}, {2, 8, 11025}, {1, 16, 11025}, {2, 16, 11025}, {1, 24, 11025}, {2, 24, 11025},
100 {1, 8, 22050}, {2, 8, 22050}, {1, 16, 22050}, {2, 16, 22050}, {1, 24, 22050}, {2, 24, 22050},
101 {1, 8, 44100}, {2, 8, 44100}, {1, 16, 44100}, {2, 16, 44100}, {1, 24, 44100}, {2, 24, 44100},
102 {1, 8, 48000}, {2, 8, 48000}, {1, 16, 48000}, {2, 16, 48000}, {1, 24, 48000}, {2, 24, 48000},
103 {1, 8, 96000}, {2, 8, 96000}, {1, 16, 96000}, {2, 16, 96000}, {1, 24, 96000}, {2, 24, 96000},
106 /***********************************************************************
109 static DWORD
PCM_GetFormatIndex(LPWAVEFORMATEX wfx
)
112 TRACE("(%p)\n", wfx
);
114 for (i
= 0; i
< NUM_PCM_FORMATS
; i
++) {
115 if (wfx
->nChannels
== PCM_Formats
[i
].nChannels
&&
116 wfx
->nSamplesPerSec
== PCM_Formats
[i
].rate
&&
117 wfx
->wBitsPerSample
== PCM_Formats
[i
].nBits
)
126 * + 8 bit unsigned vs 16 bit signed
127 * + mono vs stereo (1 or 2 channels)
128 * + sampling rate (8.0, 11.025, 22.05, 44.1 kHz are defined, but algo
129 * shall work in all cases)
131 * mono => stereo: copy the same sample on Left & Right channels
132 * stereo => mono: use the sum of Left & Right channels
135 /***********************************************************************
138 * Converts a 8 bit sample to a 16 bit one
140 static inline short C816(unsigned char b
)
142 return (b
- 128) << 8;
145 /***********************************************************************
148 * Converts a 16 bit sample to a 8 bit one (data loss !!)
150 static inline unsigned char C168(short s
)
152 return HIBYTE(s
) ^ (unsigned char)0x80;
155 /***********************************************************************
158 * Converts a 24 bit sample to a 8 bit one (data loss !!)
160 static inline unsigned char C248(int s
)
162 return HIBYTE(HIWORD(s
)) ^ (unsigned char)0x80;
165 /***********************************************************************
168 * Converts a 24 bit sample to a 16 bit one (data loss !!)
170 static inline short C2416(int s
)
175 /***********************************************************************
178 * Read a 16 bit sample (correctly handles endianness)
180 static inline short R16(const unsigned char* src
)
182 return (short)((unsigned short)src
[0] | ((unsigned short)src
[1] << 8));
185 /***********************************************************************
188 * Read a 24 bit sample (correctly handles endianness)
189 * Note, to support signed arithmetic, the values are shifted high in the int
190 * and low 8 bytes are unused.
192 static inline int R24(const unsigned char* src
)
194 return ((int)src
[0] | (int)src
[1] << 8 | (int)src
[2] << 16) << 8;
197 /***********************************************************************
200 * Write a 16 bit sample (correctly handles endianness)
202 static inline void W16(unsigned char* dst
, short s
)
208 /***********************************************************************
211 * Write a 24 bit sample (correctly handles endianness)
213 static inline void W24(unsigned char* dst
, int s
)
215 dst
[0] = HIBYTE(LOWORD(s
));
216 dst
[1] = LOBYTE(HIWORD(s
));
217 dst
[2] = HIBYTE(HIWORD(s
));
220 /***********************************************************************
223 * Convert the (l,r) 24 bit stereo sample into a 24 bit mono
224 * (takes the sum of the two values)
226 static inline int M24(int l
, int r
)
228 LONGLONG sum
= l
+ r
;
230 /* clip sum to saturation */
231 if (sum
> 0x7fffff00)
233 else if (sum
< -0x7fffff00)
239 /***********************************************************************
242 * Convert the (l,r) 16 bit stereo sample into a 16 bit mono
243 * (takes the sum of the two values)
245 static inline short M16(short l
, short r
)
249 /* clip sum to saturation */
252 else if (sum
< -32768)
258 /***********************************************************************
261 * Convert the (l,r) 8 bit stereo sample into a 8 bit mono
262 * (takes the sum of the two values)
264 static inline unsigned char M8(unsigned char a
, unsigned char b
)
268 int sum
= (l
+ r
) + 128;
270 /* clip sum to saturation */
279 /* the conversion routines without rate conversion are labelled cvt<X><Y><N><M>K
281 * <X> is the (M)ono/(S)tereo configuration of input channel
282 * <Y> is the (M)ono/(S)tereo configuration of output channel
283 * <N> is the number of bits of input channel (8 or 16)
284 * <M> is the number of bits of output channel (8 or 16)
286 * in the parameters, ns is always the number of samples, so the size of input
287 * buffer (resp output buffer) is ns * (<X> == 'Mono' ? 1:2) * (<N> == 8 ? 1:2)
290 static void cvtMM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
292 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
293 memcpy(dst
, src
, ns
);
296 static void cvtSS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
298 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
299 memcpy(dst
, src
, ns
* 2);
302 static void cvtMM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
304 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
305 memcpy(dst
, src
, ns
* 2);
308 static void cvtSS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
310 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
311 memcpy(dst
, src
, ns
* 4);
314 static void cvtMS88K(const unsigned char* src
, int ns
, unsigned char* dst
)
316 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
324 static void cvtMS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
327 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
331 W16(dst
, v
); dst
+= 2;
332 W16(dst
, v
); dst
+= 2;
336 static void cvtMS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
339 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
342 v
= C168(R16(src
)); src
+= 2;
348 static void cvtMS1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
351 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
354 v
= R16(src
); src
+= 2;
355 W16(dst
, v
); dst
+= 2;
356 W16(dst
, v
); dst
+= 2;
360 static void cvtSM88K(const unsigned char* src
, int ns
, unsigned char* dst
)
362 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
365 *dst
++ = M8(src
[0], src
[1]);
370 static void cvtSM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
373 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
376 v
= M16(C816(src
[0]), C816(src
[1]));
378 W16(dst
, v
); dst
+= 2;
382 static void cvtSM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
384 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
387 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
392 static void cvtSM1616K(const unsigned char* src
, int ns
, unsigned char* dst
)
394 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
397 W16(dst
, M16(R16(src
),R16(src
+2))); dst
+= 2;
402 static void cvtMM816K(const unsigned char* src
, int ns
, unsigned char* dst
)
404 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
407 W16(dst
, C816(*src
++)); dst
+= 2;
411 static void cvtSS816K(const unsigned char* src
, int ns
, unsigned char* dst
)
413 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
416 W16(dst
, C816(*src
++)); dst
+= 2;
417 W16(dst
, C816(*src
++)); dst
+= 2;
421 static void cvtMM168K(const unsigned char* src
, int ns
, unsigned char* dst
)
423 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
426 *dst
++ = C168(R16(src
)); src
+= 2;
430 static void cvtSS168K(const unsigned char* src
, int ns
, unsigned char* dst
)
432 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
435 *dst
++ = C168(R16(src
)); src
+= 2;
436 *dst
++ = C168(R16(src
)); src
+= 2;
440 static void cvtMS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
443 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
446 v
= C248(R24(src
)); src
+= 3;
452 static void cvtSM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
454 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
457 *dst
++ = C248(M24(R24(src
), R24(src
+ 3)));
462 static void cvtMM248K(const unsigned char* src
, int ns
, unsigned char* dst
)
464 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
467 *dst
++ = C248(R24(src
)); src
+= 3;
471 static void cvtSS248K(const unsigned char* src
, int ns
, unsigned char* dst
)
473 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
476 *dst
++ = C248(R24(src
)); src
+= 3;
477 *dst
++ = C248(R24(src
)); src
+= 3;
481 static void cvtMS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
484 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
487 v
= C2416(R24(src
)); src
+= 3;
488 W16(dst
, v
); dst
+= 2;
489 W16(dst
, v
); dst
+= 2;
493 static void cvtSM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
495 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
498 W16(dst
, C2416(M24(R24(src
), R24(src
+ 3))));
504 static void cvtMM2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
506 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
509 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
513 static void cvtSS2416K(const unsigned char* src
, int ns
, unsigned char* dst
)
515 TRACE("(%p, %d, %p)\n", src
, ns
, dst
);
518 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
519 W16(dst
, C2416(R24(src
))); dst
+= 2; src
+= 3;
524 typedef void (*PCM_CONVERT_KEEP_RATE
)(const unsigned char*, int, unsigned char*);
526 static const PCM_CONVERT_KEEP_RATE PCM_ConvertKeepRate
[] = {
527 cvtSS88K
, cvtSM88K
, cvtMS88K
, cvtMM88K
,
528 cvtSS816K
, cvtSM816K
, cvtMS816K
, cvtMM816K
,
529 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
530 cvtSS168K
, cvtSM168K
, cvtMS168K
, cvtMM168K
,
531 cvtSS1616K
, cvtSM1616K
, cvtMS1616K
, cvtMM1616K
,
532 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
533 cvtSS248K
, cvtSM248K
, cvtMS248K
, cvtMM248K
,
534 cvtSS2416K
, cvtSM2416K
, cvtMS2416K
, cvtMM2416K
,
535 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->24 */
538 /* the conversion routines with rate conversion are labelled cvt<X><Y><N><M>C
540 * <X> is the (M)ono/(S)tereo configuration of input channel
541 * <Y> is the (M)ono/(S)tereo configuration of output channel
542 * <N> is the number of bits of input channel (8 or 16)
543 * <M> is the number of bits of output channel (8 or 16)
546 static void cvtSS88C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
547 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
549 DWORD error
= dstRate
/ 2;
550 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
555 error
= error
+ srcRate
;
556 while (error
> dstRate
) {
561 error
= error
- dstRate
;
566 static void cvtSM88C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
567 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
569 DWORD error
= dstRate
/ 2;
570 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
573 *dst
++ = M8(src
[0], src
[1]);
574 error
= error
+ srcRate
;
575 while (error
> dstRate
) {
580 error
= error
- dstRate
;
585 static void cvtMS88C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
586 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
588 DWORD error
= dstRate
/ 2;
589 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
594 error
= error
+ srcRate
;
595 while (error
> dstRate
) {
600 error
= error
- dstRate
;
605 static void cvtMM88C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
606 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
608 DWORD error
= dstRate
/ 2;
609 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
613 error
= error
+ srcRate
;
614 while (error
> dstRate
) {
619 error
= error
- dstRate
;
624 static void cvtSS816C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
625 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
627 DWORD error
= dstRate
/ 2;
628 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
631 W16(dst
, C816(src
[0])); dst
+= 2;
632 W16(dst
, C816(src
[1])); dst
+= 2;
633 error
= error
+ srcRate
;
634 while (error
> dstRate
) {
639 error
= error
- dstRate
;
644 static void cvtSM816C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
645 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
647 DWORD error
= dstRate
/ 2;
648 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
651 W16(dst
, M16(C816(src
[0]), C816(src
[1]))); dst
+= 2;
652 error
= error
+ srcRate
;
653 while (error
> dstRate
) {
658 error
= error
- dstRate
;
663 static void cvtMS816C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
664 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
666 DWORD error
= dstRate
/ 2;
667 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
670 W16(dst
, C816(*src
)); dst
+= 2;
671 W16(dst
, C816(*src
)); dst
+= 2;
672 error
= error
+ srcRate
;
673 while (error
> dstRate
) {
678 error
= error
- dstRate
;
683 static void cvtMM816C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
684 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
686 DWORD error
= dstRate
/ 2;
687 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
690 W16(dst
, C816(*src
)); dst
+= 2;
691 error
= error
+ srcRate
;
692 while (error
> dstRate
) {
697 error
= error
- dstRate
;
702 static void cvtSS168C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
703 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
705 DWORD error
= dstRate
/ 2;
706 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
709 *dst
++ = C168(R16(src
));
710 *dst
++ = C168(R16(src
+ 2));
711 error
= error
+ srcRate
;
712 while (error
> dstRate
) {
717 error
= error
- dstRate
;
722 static void cvtSM168C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
723 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
725 DWORD error
= dstRate
/ 2;
726 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
729 *dst
++ = C168(M16(R16(src
), R16(src
+ 2)));
730 error
= error
+ srcRate
;
731 while (error
> dstRate
) {
736 error
= error
- dstRate
;
741 static void cvtMS168C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
742 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
744 DWORD error
= dstRate
/ 2;
745 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
748 *dst
++ = C168(R16(src
));
749 *dst
++ = C168(R16(src
));
750 error
= error
+ srcRate
;
751 while (error
> dstRate
) {
756 error
= error
- dstRate
;
761 static void cvtMM168C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
762 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
764 DWORD error
= dstRate
/ 2;
765 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
768 *dst
++ = C168(R16(src
));
769 error
= error
+ srcRate
;
770 while (error
> dstRate
) {
775 error
= error
- dstRate
;
780 static void cvtSS1616C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
781 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
783 DWORD error
= dstRate
/ 2;
784 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
787 W16(dst
, R16(src
)); dst
+= 2;
788 W16(dst
, R16(src
)); dst
+= 2;
789 error
= error
+ srcRate
;
790 while (error
> dstRate
) {
795 error
= error
- dstRate
;
800 static void cvtSM1616C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
801 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
803 DWORD error
= dstRate
/ 2;
804 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
807 W16(dst
, M16(R16(src
), R16(src
+ 2))); dst
+= 2;
808 error
= error
+ srcRate
;
809 while (error
> dstRate
) {
814 error
= error
- dstRate
;
819 static void cvtMS1616C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
820 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
822 DWORD error
= dstRate
/ 2;
823 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
826 W16(dst
, R16(src
)); dst
+= 2;
827 W16(dst
, R16(src
)); dst
+= 2;
828 error
= error
+ srcRate
;
829 while (error
> dstRate
) {
834 error
= error
- dstRate
;
839 static void cvtMM1616C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
840 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
842 DWORD error
= dstRate
/ 2;
843 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
846 W16(dst
, R16(src
)); dst
+= 2;
847 error
= error
+ srcRate
;
848 while (error
> dstRate
) {
853 error
= error
- dstRate
;
858 static void cvtSS2424C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
859 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
861 DWORD error
= dstRate
/ 2;
862 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
865 W24(dst
, R24(src
)); dst
+= 3;
866 W24(dst
, R24(src
)); dst
+= 3;
867 error
= error
+ srcRate
;
868 while (error
> dstRate
) {
873 error
= error
- dstRate
;
878 static void cvtSM2424C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
879 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
881 DWORD error
= dstRate
/ 2;
882 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
885 W24(dst
, M24(R24(src
), R24(src
+ 3))); dst
+= 3;
886 error
= error
+ srcRate
;
887 while (error
> dstRate
) {
892 error
= error
- dstRate
;
897 static void cvtMS2424C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
898 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
900 DWORD error
= dstRate
/ 2;
901 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
904 W24(dst
, R24(src
)); dst
+= 3;
905 W24(dst
, R24(src
)); dst
+= 3;
906 error
= error
+ srcRate
;
907 while (error
> dstRate
) {
912 error
= error
- dstRate
;
917 static void cvtMM2424C(DWORD srcRate
, const unsigned char* src
, LPDWORD nsrc
,
918 DWORD dstRate
, unsigned char* dst
, LPDWORD ndst
)
920 DWORD error
= dstRate
/ 2;
921 TRACE("(%d, %p, %p, %d, %p, %p)\n", srcRate
, src
, nsrc
, dstRate
, dst
, ndst
);
924 W24(dst
, R24(src
)); dst
+= 3;
925 error
= error
+ srcRate
;
926 while (error
> dstRate
) {
931 error
= error
- dstRate
;
936 typedef void (*PCM_CONVERT_CHANGE_RATE
)(DWORD
, const unsigned char*, LPDWORD
, DWORD
, unsigned char*, LPDWORD
);
938 static const PCM_CONVERT_CHANGE_RATE PCM_ConvertChangeRate
[] = {
939 cvtSS88C
, cvtSM88C
, cvtMS88C
, cvtMM88C
,
940 cvtSS816C
, cvtSM816C
, cvtMS816C
, cvtMM816C
,
941 NULL
, NULL
, NULL
, NULL
, /* TODO: 8->24 */
942 cvtSS168C
, cvtSM168C
, cvtMS168C
, cvtMM168C
,
943 cvtSS1616C
, cvtSM1616C
, cvtMS1616C
, cvtMM1616C
,
944 NULL
, NULL
, NULL
, NULL
, /* TODO: 16->24 */
945 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->8 */
946 NULL
, NULL
, NULL
, NULL
, /* TODO: 24->16 */
947 cvtSS2424C
, cvtSM2424C
, cvtMS2424C
, cvtMM2424C
,
950 /***********************************************************************
954 static LRESULT
PCM_DriverDetails(PACMDRIVERDETAILSW add
)
956 TRACE("(%p)\n", add
);
958 add
->fccType
= ACMDRIVERDETAILS_FCCTYPE_AUDIOCODEC
;
959 add
->fccComp
= ACMDRIVERDETAILS_FCCCOMP_UNDEFINED
;
960 add
->wMid
= MM_MICROSOFT
;
961 add
->wPid
= MM_MSFT_ACM_PCM
;
962 add
->vdwACM
= 0x01000000;
963 add
->vdwDriver
= 0x01000000;
964 add
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
965 add
->cFormatTags
= 1;
966 add
->cFilterTags
= 0;
968 MultiByteToWideChar( CP_ACP
, 0, "MS-PCM", -1,
969 add
->szShortName
, sizeof(add
->szShortName
)/sizeof(WCHAR
) );
970 MultiByteToWideChar( CP_ACP
, 0, "Wine PCM converter", -1,
971 add
->szLongName
, sizeof(add
->szLongName
)/sizeof(WCHAR
) );
972 MultiByteToWideChar( CP_ACP
, 0, "Brought to you by the Wine team...", -1,
973 add
->szCopyright
, sizeof(add
->szCopyright
)/sizeof(WCHAR
) );
974 MultiByteToWideChar( CP_ACP
, 0, "Refer to LICENSE file", -1,
975 add
->szLicensing
, sizeof(add
->szLicensing
)/sizeof(WCHAR
) );
976 add
->szFeatures
[0] = 0;
978 return MMSYSERR_NOERROR
;
981 /***********************************************************************
982 * PCM_FormatTagDetails
985 static LRESULT
PCM_FormatTagDetails(PACMFORMATTAGDETAILSW aftd
, DWORD dwQuery
)
987 TRACE("(%p, %08x)\n", aftd
, dwQuery
);
990 case ACM_FORMATTAGDETAILSF_INDEX
:
991 if (aftd
->dwFormatTagIndex
!= 0) {
992 WARN("not possible\n");
993 return ACMERR_NOTPOSSIBLE
;
996 case ACM_FORMATTAGDETAILSF_FORMATTAG
:
997 if (aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
998 WARN("not possible\n");
999 return ACMERR_NOTPOSSIBLE
;
1002 case ACM_FORMATTAGDETAILSF_LARGESTSIZE
:
1003 if (aftd
->dwFormatTag
!= WAVE_FORMAT_UNKNOWN
&&
1004 aftd
->dwFormatTag
!= WAVE_FORMAT_PCM
) {
1005 WARN("not possible\n");
1006 return ACMERR_NOTPOSSIBLE
;
1010 WARN("Unsupported query %08x\n", dwQuery
);
1011 return MMSYSERR_NOTSUPPORTED
;
1014 aftd
->dwFormatTagIndex
= 0;
1015 aftd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1016 aftd
->cbFormatSize
= sizeof(PCMWAVEFORMAT
);
1017 aftd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1018 aftd
->cStandardFormats
= NUM_PCM_FORMATS
;
1019 aftd
->szFormatTag
[0] = 0;
1021 return MMSYSERR_NOERROR
;
1024 /***********************************************************************
1028 static LRESULT
PCM_FormatDetails(PACMFORMATDETAILSW afd
, DWORD dwQuery
)
1030 TRACE("(%p, %08x)\n", afd
, dwQuery
);
1033 case ACM_FORMATDETAILSF_FORMAT
:
1034 if (PCM_GetFormatIndex(afd
->pwfx
) == 0xFFFFFFFF) {
1035 WARN("not possible\n");
1036 return ACMERR_NOTPOSSIBLE
;
1039 case ACM_FORMATDETAILSF_INDEX
:
1040 assert(afd
->dwFormatIndex
< NUM_PCM_FORMATS
);
1041 afd
->pwfx
->wFormatTag
= WAVE_FORMAT_PCM
;
1042 afd
->pwfx
->nChannels
= PCM_Formats
[afd
->dwFormatIndex
].nChannels
;
1043 afd
->pwfx
->nSamplesPerSec
= PCM_Formats
[afd
->dwFormatIndex
].rate
;
1044 afd
->pwfx
->wBitsPerSample
= PCM_Formats
[afd
->dwFormatIndex
].nBits
;
1045 /* native MSACM uses a PCMWAVEFORMAT structure, so cbSize is not
1046 * accessible afd->pwfx->cbSize = 0;
1048 afd
->pwfx
->nBlockAlign
=
1049 (afd
->pwfx
->nChannels
* afd
->pwfx
->wBitsPerSample
) / 8;
1050 afd
->pwfx
->nAvgBytesPerSec
=
1051 afd
->pwfx
->nSamplesPerSec
* afd
->pwfx
->nBlockAlign
;
1054 WARN("Unsupported query %08x\n", dwQuery
);
1055 return MMSYSERR_NOTSUPPORTED
;
1058 afd
->dwFormatTag
= WAVE_FORMAT_PCM
;
1059 afd
->fdwSupport
= ACMDRIVERDETAILS_SUPPORTF_CONVERTER
;
1060 afd
->szFormat
[0] = 0; /* let MSACM format this for us... */
1061 afd
->cbwfx
= sizeof(PCMWAVEFORMAT
);
1063 return MMSYSERR_NOERROR
;
1066 /***********************************************************************
1070 static LRESULT
PCM_FormatSuggest(PACMDRVFORMATSUGGEST adfs
)
1072 TRACE("(%p)\n", adfs
);
1074 /* some tests ... */
1075 if (adfs
->cbwfxSrc
< sizeof(PCMWAVEFORMAT
) ||
1076 adfs
->cbwfxDst
< sizeof(PCMWAVEFORMAT
) ||
1077 PCM_GetFormatIndex(adfs
->pwfxSrc
) == 0xFFFFFFFF) {
1078 WARN("not possible\n");
1079 return ACMERR_NOTPOSSIBLE
;
1082 /* is no suggestion for destination, then copy source value */
1083 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NCHANNELS
)) {
1084 adfs
->pwfxDst
->nChannels
= adfs
->pwfxSrc
->nChannels
;
1086 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_NSAMPLESPERSEC
)) {
1087 adfs
->pwfxDst
->nSamplesPerSec
= adfs
->pwfxSrc
->nSamplesPerSec
;
1089 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WBITSPERSAMPLE
)) {
1090 adfs
->pwfxDst
->wBitsPerSample
= adfs
->pwfxSrc
->wBitsPerSample
;
1092 if (!(adfs
->fdwSuggest
& ACM_FORMATSUGGESTF_WFORMATTAG
)) {
1093 if (adfs
->pwfxSrc
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1094 WARN("source format 0x%x not supported\n", adfs
->pwfxSrc
->wFormatTag
);
1095 return ACMERR_NOTPOSSIBLE
;
1097 adfs
->pwfxDst
->wFormatTag
= adfs
->pwfxSrc
->wFormatTag
;
1099 if (adfs
->pwfxDst
->wFormatTag
!= WAVE_FORMAT_PCM
) {
1100 WARN("destination format 0x%x not supported\n", adfs
->pwfxDst
->wFormatTag
);
1101 return ACMERR_NOTPOSSIBLE
;
1104 /* check if result is ok */
1105 if (PCM_GetFormatIndex(adfs
->pwfxDst
) == 0xFFFFFFFF) {
1106 WARN("not possible\n");
1107 return ACMERR_NOTPOSSIBLE
;
1110 /* recompute other values */
1111 adfs
->pwfxDst
->nBlockAlign
= (adfs
->pwfxDst
->nChannels
* adfs
->pwfxDst
->wBitsPerSample
) / 8;
1112 adfs
->pwfxDst
->nAvgBytesPerSec
= adfs
->pwfxDst
->nSamplesPerSec
* adfs
->pwfxDst
->nBlockAlign
;
1114 return MMSYSERR_NOERROR
;
1117 /***********************************************************************
1121 static LRESULT
PCM_StreamOpen(PACMDRVSTREAMINSTANCE adsi
)
1127 TRACE("(%p)\n", adsi
);
1129 assert(!(adsi
->fdwOpen
& ACM_STREAMOPENF_ASYNC
));
1131 switch(adsi
->pwfxSrc
->wBitsPerSample
){
1139 if (adsi
->pwfxSrc
->nBlockAlign
!= 3 * adsi
->pwfxSrc
->nChannels
) {
1140 FIXME("Source: 24-bit samples must be packed\n");
1141 return MMSYSERR_NOTSUPPORTED
;
1146 FIXME("Unsupported source bit depth: %u\n", adsi
->pwfxSrc
->wBitsPerSample
);
1147 return MMSYSERR_NOTSUPPORTED
;
1150 switch(adsi
->pwfxDst
->wBitsPerSample
){
1157 if (adsi
->pwfxDst
->nBlockAlign
!= 3 * adsi
->pwfxDst
->nChannels
) {
1158 FIXME("Destination: 24-bit samples must be packed\n");
1159 return MMSYSERR_NOTSUPPORTED
;
1164 FIXME("Unsupported destination bit depth: %u\n", adsi
->pwfxDst
->wBitsPerSample
);
1165 return MMSYSERR_NOTSUPPORTED
;
1168 if (adsi
->pwfxSrc
->nChannels
== 1) idx
+= 2;
1170 if (adsi
->pwfxDst
->nChannels
== 1) idx
+= 1;
1172 apd
= HeapAlloc(GetProcessHeap(), 0, sizeof(AcmPcmData
));
1174 return MMSYSERR_NOMEM
;
1176 if (adsi
->pwfxSrc
->nSamplesPerSec
== adsi
->pwfxDst
->nSamplesPerSec
) {
1178 apd
->cvt
.cvtKeepRate
= PCM_ConvertKeepRate
[idx
];
1180 flags
= PCM_RESAMPLE
;
1181 apd
->cvt
.cvtChangeRate
= PCM_ConvertChangeRate
[idx
];
1184 if(!apd
->cvt
.cvtChangeRate
&& !apd
->cvt
.cvtKeepRate
){
1185 FIXME("Unimplemented conversion from %u -> %u bps\n",
1186 adsi
->pwfxSrc
->wBitsPerSample
,
1187 adsi
->pwfxDst
->wBitsPerSample
);
1188 HeapFree(GetProcessHeap(), 0, apd
);
1189 return MMSYSERR_NOTSUPPORTED
;
1192 adsi
->dwDriver
= (DWORD_PTR
)apd
;
1193 adsi
->fdwDriver
= flags
;
1195 return MMSYSERR_NOERROR
;
1198 /***********************************************************************
1202 static LRESULT
PCM_StreamClose(PACMDRVSTREAMINSTANCE adsi
)
1204 TRACE("(%p)\n", adsi
);
1206 HeapFree(GetProcessHeap(), 0, (void*)adsi
->dwDriver
);
1207 return MMSYSERR_NOERROR
;
1210 /***********************************************************************
1214 static inline DWORD
PCM_round(DWORD a
, DWORD b
, DWORD c
)
1217 /* to be sure, always return an entire number of c... */
1218 return ((double)a
* (double)b
+ (double)c
- 1) / (double)c
;
1221 /***********************************************************************
1225 static LRESULT
PCM_StreamSize(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMSIZE adss
)
1227 DWORD srcMask
= ~(adsi
->pwfxSrc
->nBlockAlign
- 1);
1228 DWORD dstMask
= ~(adsi
->pwfxDst
->nBlockAlign
- 1);
1230 TRACE("(%p, %p)\n", adsi
, adss
);
1232 switch (adss
->fdwSize
) {
1233 case ACM_STREAMSIZEF_DESTINATION
:
1234 /* cbDstLength => cbSrcLength */
1235 adss
->cbSrcLength
= PCM_round(adss
->cbDstLength
& dstMask
,
1236 adsi
->pwfxSrc
->nAvgBytesPerSec
,
1237 adsi
->pwfxDst
->nAvgBytesPerSec
) & srcMask
;
1239 case ACM_STREAMSIZEF_SOURCE
:
1240 /* cbSrcLength => cbDstLength */
1241 adss
->cbDstLength
= PCM_round(adss
->cbSrcLength
& srcMask
,
1242 adsi
->pwfxDst
->nAvgBytesPerSec
,
1243 adsi
->pwfxSrc
->nAvgBytesPerSec
) & dstMask
;
1246 WARN("Unsupported query %08x\n", adss
->fdwSize
);
1247 return MMSYSERR_NOTSUPPORTED
;
1249 return MMSYSERR_NOERROR
;
1252 /***********************************************************************
1256 static LRESULT
PCM_StreamConvert(PACMDRVSTREAMINSTANCE adsi
, PACMDRVSTREAMHEADER adsh
)
1258 AcmPcmData
* apd
= (AcmPcmData
*)adsi
->dwDriver
;
1259 DWORD nsrc
= NUM_OF(adsh
->cbSrcLength
, adsi
->pwfxSrc
->nBlockAlign
);
1260 DWORD ndst
= NUM_OF(adsh
->cbDstLength
, adsi
->pwfxDst
->nBlockAlign
);
1262 TRACE("(%p, %p)\n", adsi
, adsh
);
1264 TRACE("nsrc=%d,adsh->cbSrcLength=%d\n", nsrc
, adsh
->cbSrcLength
);
1265 TRACE("ndst=%d,adsh->cbDstLength=%d\n", ndst
, adsh
->cbDstLength
);
1266 TRACE("src [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1267 adsi
->pwfxSrc
->wFormatTag
, adsi
->pwfxSrc
->nChannels
, adsi
->pwfxSrc
->nSamplesPerSec
, adsi
->pwfxSrc
->nAvgBytesPerSec
,
1268 adsi
->pwfxSrc
->nBlockAlign
, adsi
->pwfxSrc
->wBitsPerSample
, adsi
->pwfxSrc
->cbSize
);
1269 TRACE("dst [wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u]\n",
1270 adsi
->pwfxDst
->wFormatTag
, adsi
->pwfxDst
->nChannels
, adsi
->pwfxDst
->nSamplesPerSec
, adsi
->pwfxDst
->nAvgBytesPerSec
,
1271 adsi
->pwfxDst
->nBlockAlign
, adsi
->pwfxDst
->wBitsPerSample
, adsi
->pwfxDst
->cbSize
);
1273 if (adsh
->fdwConvert
&
1274 ~(ACM_STREAMCONVERTF_BLOCKALIGN
|
1275 ACM_STREAMCONVERTF_END
|
1276 ACM_STREAMCONVERTF_START
)) {
1277 FIXME("Unsupported fdwConvert (%08x), ignoring it\n", adsh
->fdwConvert
);
1279 /* ACM_STREAMCONVERTF_BLOCKALIGN
1280 * currently all conversions are block aligned, so do nothing for this flag
1281 * ACM_STREAMCONVERTF_END
1282 * no pending data, so do nothing for this flag
1284 if ((adsh
->fdwConvert
& ACM_STREAMCONVERTF_START
) &&
1285 (adsi
->fdwDriver
& PCM_RESAMPLE
)) {
1289 if (adsi
->fdwDriver
& PCM_RESAMPLE
) {
1292 apd
->cvt
.cvtChangeRate(adsi
->pwfxSrc
->nSamplesPerSec
, adsh
->pbSrc
, &nsrc2
,
1293 adsi
->pwfxDst
->nSamplesPerSec
, adsh
->pbDst
, &ndst2
);
1297 if (nsrc
< ndst
) ndst
= nsrc
; else nsrc
= ndst
;
1299 /* nsrc is now equal to ndst */
1300 apd
->cvt
.cvtKeepRate(adsh
->pbSrc
, nsrc
, adsh
->pbDst
);
1303 adsh
->cbSrcLengthUsed
= nsrc
* adsi
->pwfxSrc
->nBlockAlign
;
1304 adsh
->cbDstLengthUsed
= ndst
* adsi
->pwfxDst
->nBlockAlign
;
1306 return MMSYSERR_NOERROR
;
1309 /**************************************************************************
1310 * DriverProc (MSACM32.@)
1312 LRESULT CALLBACK
PCM_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1313 LPARAM dwParam1
, LPARAM dwParam2
)
1315 TRACE("(%08lx %p %u %08lx %08lx);\n",
1316 dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1319 case DRV_LOAD
: return 1;
1320 case DRV_FREE
: return 1;
1321 case DRV_OPEN
: return PCM_drvOpen((LPSTR
)dwParam1
, (PACMDRVOPENDESCW
)dwParam2
);
1322 case DRV_CLOSE
: return PCM_drvClose(dwDevID
);
1323 case DRV_ENABLE
: return 1;
1324 case DRV_DISABLE
: return 1;
1325 case DRV_QUERYCONFIGURE
: return 1;
1326 case DRV_CONFIGURE
: MessageBoxA(0, "MSACM PCM filter !", "Wine Driver", MB_OK
); return 1;
1327 case DRV_INSTALL
: return DRVCNF_RESTART
;
1328 case DRV_REMOVE
: return DRVCNF_RESTART
;
1330 case ACMDM_DRIVER_NOTIFY
:
1331 /* no caching from other ACM drivers is done so far */
1332 return MMSYSERR_NOERROR
;
1334 case ACMDM_DRIVER_DETAILS
:
1335 return PCM_DriverDetails((PACMDRIVERDETAILSW
)dwParam1
);
1337 case ACMDM_FORMATTAG_DETAILS
:
1338 return PCM_FormatTagDetails((PACMFORMATTAGDETAILSW
)dwParam1
, dwParam2
);
1340 case ACMDM_FORMAT_DETAILS
:
1341 return PCM_FormatDetails((PACMFORMATDETAILSW
)dwParam1
, dwParam2
);
1343 case ACMDM_FORMAT_SUGGEST
:
1344 return PCM_FormatSuggest((PACMDRVFORMATSUGGEST
)dwParam1
);
1346 case ACMDM_STREAM_OPEN
:
1347 return PCM_StreamOpen((PACMDRVSTREAMINSTANCE
)dwParam1
);
1349 case ACMDM_STREAM_CLOSE
:
1350 return PCM_StreamClose((PACMDRVSTREAMINSTANCE
)dwParam1
);
1352 case ACMDM_STREAM_SIZE
:
1353 return PCM_StreamSize((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMSIZE
)dwParam2
);
1355 case ACMDM_STREAM_CONVERT
:
1356 return PCM_StreamConvert((PACMDRVSTREAMINSTANCE
)dwParam1
, (PACMDRVSTREAMHEADER
)dwParam2
);
1358 case ACMDM_HARDWARE_WAVE_CAPS_INPUT
:
1359 case ACMDM_HARDWARE_WAVE_CAPS_OUTPUT
:
1360 /* this converter is not a hardware driver */
1361 case ACMDM_FILTERTAG_DETAILS
:
1362 case ACMDM_FILTER_DETAILS
:
1363 /* this converter is not a filter */
1364 case ACMDM_STREAM_RESET
:
1365 /* only needed for asynchronous driver... we aren't, so just say it */
1366 case ACMDM_STREAM_PREPARE
:
1367 case ACMDM_STREAM_UNPREPARE
:
1368 /* nothing special to do here... so don't do anything */
1369 return MMSYSERR_NOTSUPPORTED
;
1372 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);