gdiplus: Implemented GdipIsEmptyClip with tests.
[wine.git] / dlls / dsound / dsound_convert.c
blob4b5ff4b99c4ef295efc92f85014208aeafdf7075
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
36 #include "config.h"
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h>
39 #endif
40 #include <stdarg.h>
42 #define NONAMELESSSTRUCT
43 #define NONAMELESSUNION
44 #include "windef.h"
45 #include "winbase.h"
46 #include "mmsystem.h"
47 #include "winternl.h"
48 #include "wine/debug.h"
49 #include "dsound.h"
50 #include "dsdriver.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))
58 #else
59 #define le16(x) (x)
60 #define le32(x) (x)
61 #endif
63 static void convert_8_to_8 (const void *src, void *dst)
65 uint8_t *dest = 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;
79 dest24[0] = dest;
80 dest24[1] = dest;
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)
92 uint8_t *dst8 = dst;
93 *dst8 = (le16(*(uint16_t*)src)) / 256;
94 *dst8 -= 0x80;
97 static void convert_16_to_16 (const void *src, void *dst)
99 uint16_t *dest = 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;
109 dest24[1] = dest;
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)
121 uint8_t *dst8 = 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)
151 uint8_t *dst8 = dst;
152 *dst8 = (le32(*(uint32_t *)src) / 16777216);
153 *dst8 -= 0x80;
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);
188 while (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);
196 len /= 2;
197 while (len--)
199 *dst += le16(*src);
200 ++dst; ++src;
204 static void mix24(uint8_t *src, int32_t *dst, unsigned len)
206 TRACE("%p - %p %d\n", src, dst, len);
207 len /= 3;
208 while (len--)
210 uint32_t field;
211 field = ((unsigned)src[2] << 16) + ((unsigned)src[1] << 8) + (unsigned)src[0];
212 if (src[2] & 0x80)
213 field |= 0xFF000000U;
214 *(dst++) += field;
215 ++src;
219 static void mix32(int32_t *src, int64_t *dst, unsigned len)
221 TRACE("%p - %p %d\n", src, dst, len);
222 len /= 4;
223 while (len--)
224 *(dst++) += le32(*(src++));
227 const mixfunc mixfunctions[4] = {
228 (mixfunc)mix8,
229 (mixfunc)mix16,
230 (mixfunc)mix24,
231 (mixfunc)mix32
234 static void norm8(int32_t *src, int8_t *dst, unsigned len)
236 TRACE("%p - %p %d\n", src, dst, len);
237 while (len--)
239 *dst = (*src) + 0x80;
240 if (*src < -0x80)
241 *dst = 0;
242 else if (*src > 0x7f)
243 *dst = 0xff;
244 ++dst;
245 ++src;
249 static void norm16(int32_t *src, int16_t *dst, unsigned len)
251 TRACE("%p - %p %d\n", src, dst, len);
252 len /= 2;
253 while (len--)
255 *dst = le16(*src);
256 if (*src <= -0x8000)
257 *dst = le16(0x8000);
258 else if (*src > 0x7fff)
259 *dst = le16(0x7fff);
260 ++dst;
261 ++src;
265 static void norm24(int32_t *src, uint8_t *dst, unsigned len)
267 TRACE("%p - %p %d\n", src, dst, len);
268 len /= 3;
269 while (len--)
271 if (*src <= -0x800000)
273 dst[0] = 0;
274 dst[1] = 0;
275 dst[2] = 0x80;
277 else if (*src > 0x7fffff)
279 dst[0] = 0xff;
280 dst[1] = 0xff;
281 dst[2] = 0x7f;
283 else
285 dst[0] = *src;
286 dst[1] = *src >> 8;
287 dst[2] = *src >> 16;
289 ++dst;
290 ++src;
294 static void norm32(int64_t *src, int32_t *dst, unsigned len)
296 TRACE("%p - %p %d\n", src, dst, len);
297 len /= 4;
298 while (len--)
300 *dst = le32(*src);
301 if (*src <= -(int64_t)0x80000000)
302 *dst = le32(0x80000000);
303 else if (*src > 0x7fffffff)
304 *dst = le32(0x7fffffff);
305 ++dst;
306 ++src;
310 const normfunc normfunctions[4] = {
311 (normfunc)norm8,
312 (normfunc)norm16,
313 (normfunc)norm24,
314 (normfunc)norm32,