1 /* DirectSound format conversion and mixing routines
3 * Copyright 2007 Maarten Lankhorst
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 /* 8 bits is unsigned, the rest is signed.
21 * First I tried to reuse existing stuff from alsa-lib, after that
22 * didn't work, I gave up and just went for individual hacks.
24 * 24 bit is expensive to do, due to unaligned access.
25 * In dlls/winex11.drv/dib_convert.c convert_888_to_0888_asis there is a way
26 * around it, but I'm happy current code works, maybe something for later.
28 * The ^ 0x80 flips the signed bit, this is the conversion from
29 * signed (-128.. 0.. 127) to unsigned (0...255)
30 * This is only temporary: All 8 bit data should be converted to signed.
31 * then when fed to the sound card, it should be converted to unsigned again.
33 * Sound is LITTLE endian
42 #define NONAMELESSSTRUCT
43 #define NONAMELESSUNION
48 #include "wine/debug.h"
51 #include "dsound_private.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(dsound
);
55 #ifdef WORDS_BIGENDIAN
56 #define le16(x) RtlUshortByteSwap((x))
57 #define le32(x) RtlUlongByteSwap((x))
63 static void convert_8_to_8 (const void *src
, void *dst
)
66 *dest
= *(uint8_t*)src
;
69 static void convert_8_to_16 (const void *src
, void *dst
)
71 uint16_t dest
= *(uint8_t*)src
, *dest16
= dst
;
72 *dest16
= le16(dest
* 257 - 32768);
75 static void convert_8_to_24 (const void *src
, void *dst
)
77 uint8_t dest
= *(uint8_t*)src
;
78 uint8_t *dest24
= dst
;
81 dest24
[2] = dest
- 0x80;
84 static void convert_8_to_32 (const void *src
, void *dst
)
86 uint32_t dest
= *(uint8_t*)src
, *dest32
= dst
;
87 *dest32
= le32(dest
* 16843009 - 2147483648U);
90 static void convert_16_to_8 (const void *src
, void *dst
)
93 *dst8
= (le16(*(uint16_t*)src
)) / 256;
97 static void convert_16_to_16 (const void *src
, void *dst
)
100 *dest
= *(uint16_t*)src
;
103 static void convert_16_to_24 (const void *src
, void *dst
)
105 uint16_t dest
= le16(*(uint16_t*)src
);
106 uint8_t *dest24
= dst
;
108 dest24
[0] = dest
/ 256;
110 dest24
[2] = dest
/ 256;
113 static void convert_16_to_32 (const void *src
, void *dst
)
115 uint32_t dest
= *(uint16_t*)src
, *dest32
= dst
;
116 *dest32
= dest
* 65537;
119 static void convert_24_to_8 (const void *src
, void *dst
)
122 *dst8
= ((uint8_t *)src
)[2];
125 static void convert_24_to_16 (const void *src
, void *dst
)
127 uint16_t *dest16
= dst
;
128 const uint8_t *source
= src
;
129 *dest16
= le16(source
[2] * 256 + source
[1]);
132 static void convert_24_to_24 (const void *src
, void *dst
)
134 uint8_t *dest24
= dst
;
135 const uint8_t *src24
= src
;
137 dest24
[0] = src24
[0];
138 dest24
[1] = src24
[1];
139 dest24
[2] = src24
[2];
142 static void convert_24_to_32 (const void *src
, void *dst
)
144 uint32_t *dest32
= dst
;
145 const uint8_t *source
= src
;
146 *dest32
= le32(source
[2] * 16777217 + source
[1] * 65536 + source
[0] * 256);
149 static void convert_32_to_8 (const void *src
, void *dst
)
152 *dst8
= (le32(*(uint32_t *)src
) / 16777216);
156 static void convert_32_to_16 (const void *src
, void *dst
)
158 uint16_t *dest16
= dst
;
159 *dest16
= le16(le32(*(uint32_t*)src
) / 65536);
162 static void convert_32_to_24 (const void *src
, void *dst
)
164 uint32_t dest
= le32(*(uint32_t*)dst
);
165 uint8_t *dest24
= dst
;
167 dest24
[0] = dest
/ 256;
168 dest24
[1] = dest
/ 65536;
169 dest24
[2] = dest
/ 16777216;
172 static void convert_32_to_32 (const void *src
, void *dst
)
174 uint32_t *dest
= dst
;
175 *dest
= *(uint32_t*)src
;
178 const bitsconvertfunc convertbpp
[4][4] = {
179 { convert_8_to_8
, convert_8_to_16
, convert_8_to_24
, convert_8_to_32
},
180 { convert_16_to_8
, convert_16_to_16
, convert_16_to_24
, convert_16_to_32
},
181 { convert_24_to_8
, convert_24_to_16
, convert_24_to_24
, convert_24_to_32
},
182 { convert_32_to_8
, convert_32_to_16
, convert_32_to_24
, convert_32_to_32
},
185 static void mix8(int8_t *src
, int32_t *dst
, unsigned len
)
187 TRACE("%p - %p %d\n", src
, dst
, len
);
189 /* 8-bit WAV is unsigned, it's here converted to signed, normalize function will convert it back again */
190 *(dst
++) += (int8_t)((uint8_t)*(src
++) - (uint8_t)0x80);
193 static void mix16(int16_t *src
, int32_t *dst
, unsigned len
)
195 TRACE("%p - %p %d\n", src
, dst
, len
);
204 static void mix24(uint8_t *src
, int32_t *dst
, unsigned len
)
206 TRACE("%p - %p %d\n", src
, dst
, len
);
211 field
= ((unsigned)src
[2] << 16) + ((unsigned)src
[1] << 8) + (unsigned)src
[0];
213 field
|= 0xFF000000U
;
219 static void mix32(int32_t *src
, int64_t *dst
, unsigned len
)
221 TRACE("%p - %p %d\n", src
, dst
, len
);
224 *(dst
++) += le32(*(src
++));
227 const mixfunc mixfunctions
[4] = {
234 static void norm8(int32_t *src
, int8_t *dst
, unsigned len
)
236 TRACE("%p - %p %d\n", src
, dst
, len
);
239 *dst
= (*src
) + 0x80;
242 else if (*src
> 0x7f)
249 static void norm16(int32_t *src
, int16_t *dst
, unsigned len
)
251 TRACE("%p - %p %d\n", src
, dst
, len
);
258 else if (*src
> 0x7fff)
265 static void norm24(int32_t *src
, uint8_t *dst
, unsigned len
)
267 TRACE("%p - %p %d\n", src
, dst
, len
);
271 if (*src
<= -0x800000)
277 else if (*src
> 0x7fffff)
294 static void norm32(int64_t *src
, int32_t *dst
, unsigned len
)
296 TRACE("%p - %p %d\n", src
, dst
, len
);
301 if (*src
<= -(int64_t)0x80000000)
302 *dst
= le32(0x80000000);
303 else if (*src
> 0x7fffffff)
304 *dst
= le32(0x7fffffff);
310 const normfunc normfunctions
[4] = {