dsound: Avoid use of stdint types.
[wine] / dlls / dsound / dsound_convert.c
1 /* DirectSound format conversion and mixing routines
2  *
3  * Copyright 2007 Maarten Lankhorst
4  *
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.
9  *
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.
14  *
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
18  */
19
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.
23  *
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.
27  *
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.
32  *
33  * Sound is LITTLE endian
34  */
35
36 #include "config.h"
37
38 #include <stdarg.h>
39
40 #define NONAMELESSSTRUCT
41 #define NONAMELESSUNION
42 #include "windef.h"
43 #include "winbase.h"
44 #include "mmsystem.h"
45 #include "winternl.h"
46 #include "wine/debug.h"
47 #include "dsound.h"
48 #include "dsdriver.h"
49 #include "dsound_private.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
52
53 #ifdef WORDS_BIGENDIAN
54 #define le16(x) RtlUshortByteSwap((x))
55 #define le32(x) RtlUlongByteSwap((x))
56 #else
57 #define le16(x) (x)
58 #define le32(x) (x)
59 #endif
60
61 static void convert_8_to_8 (const void *src, void *dst)
62 {
63     BYTE *dest = dst;
64     *dest = *(BYTE*)src;
65 }
66
67 static void convert_8_to_16 (const void *src, void *dst)
68 {
69     WORD dest = *(BYTE*)src, *dest16 = dst;
70     *dest16 = le16(dest * 257 - 32768);
71 }
72
73 static void convert_8_to_24 (const void *src, void *dst)
74 {
75     BYTE dest = *(BYTE*)src;
76     BYTE *dest24 = dst;
77     dest24[0] = dest;
78     dest24[1] = dest;
79     dest24[2] = dest - 0x80;
80 }
81
82 static void convert_8_to_32 (const void *src, void *dst)
83 {
84     DWORD dest = *(BYTE*)src, *dest32 = dst;
85     *dest32 = le32(dest * 16843009 - 2147483648U);
86 }
87
88 static void convert_16_to_8 (const void *src, void *dst)
89 {
90     BYTE *dst8 = dst;
91     *dst8 = (le16(*(WORD*)src)) / 256;
92     *dst8 -= 0x80;
93 }
94
95 static void convert_16_to_16 (const void *src, void *dst)
96 {
97     WORD *dest = dst;
98     *dest = *(WORD*)src;
99 }
100
101 static void convert_16_to_24 (const void *src, void *dst)
102 {
103     WORD dest = le16(*(WORD*)src);
104     BYTE *dest24 = dst;
105
106     dest24[0] = dest / 256;
107     dest24[1] = dest;
108     dest24[2] = dest / 256;
109 }
110
111 static void convert_16_to_32 (const void *src, void *dst)
112 {
113     DWORD dest = *(WORD*)src, *dest32 = dst;
114     *dest32 = dest * 65537;
115 }
116
117 static void convert_24_to_8 (const void *src, void *dst)
118 {
119     BYTE *dst8 = dst;
120     *dst8 = ((BYTE *)src)[2];
121 }
122
123 static void convert_24_to_16 (const void *src, void *dst)
124 {
125     WORD *dest16 = dst;
126     const BYTE *source = src;
127     *dest16 = le16(source[2] * 256 + source[1]);
128 }
129
130 static void convert_24_to_24 (const void *src, void *dst)
131 {
132     BYTE *dest24 = dst;
133     const BYTE *src24 = src;
134
135     dest24[0] = src24[0];
136     dest24[1] = src24[1];
137     dest24[2] = src24[2];
138 }
139
140 static void convert_24_to_32 (const void *src, void *dst)
141 {
142     DWORD *dest32 = dst;
143     const BYTE *source = src;
144     *dest32 = le32(source[2] * 16777217 + source[1] * 65536 + source[0] * 256);
145 }
146
147 static void convert_32_to_8 (const void *src, void *dst)
148 {
149     BYTE *dst8 = dst;
150     *dst8 = (le32(*(DWORD *)src) / 16777216);
151     *dst8 -= 0x80;
152 }
153
154 static void convert_32_to_16 (const void *src, void *dst)
155 {
156     WORD *dest16 = dst;
157     *dest16 = le16(le32(*(DWORD*)src) / 65536);
158 }
159
160 static void convert_32_to_24 (const void *src, void *dst)
161 {
162     DWORD dest = le32(*(DWORD*)dst);
163     BYTE *dest24 = dst;
164
165     dest24[0] = dest / 256;
166     dest24[1] = dest / 65536;
167     dest24[2] = dest / 16777216;
168 }
169
170 static void convert_32_to_32 (const void *src, void *dst)
171 {
172     DWORD *dest = dst;
173     *dest = *(DWORD*)src;
174 }
175
176 const bitsconvertfunc convertbpp[4][4] = {
177     { convert_8_to_8, convert_8_to_16, convert_8_to_24, convert_8_to_32 },
178     { convert_16_to_8, convert_16_to_16, convert_16_to_24, convert_16_to_32 },
179     { convert_24_to_8, convert_24_to_16, convert_24_to_24, convert_24_to_32 },
180     { convert_32_to_8, convert_32_to_16, convert_32_to_24, convert_32_to_32 },
181 };
182
183 static void mix8(signed char *src, INT *dst, unsigned len)
184 {
185     TRACE("%p - %p %d\n", src, dst, len);
186     while (len--)
187         /* 8-bit WAV is unsigned, it's here converted to signed, normalize function will convert it back again */
188         *(dst++) += (signed char)((BYTE)*(src++) - (BYTE)0x80);
189 }
190
191 static void mix16(SHORT *src, INT *dst, unsigned len)
192 {
193     TRACE("%p - %p %d\n", src, dst, len);
194     len /= 2;
195     while (len--)
196     {
197         *dst += le16(*src);
198         ++dst; ++src;
199     }
200 }
201
202 static void mix24(BYTE *src, INT *dst, unsigned len)
203 {
204     TRACE("%p - %p %d\n", src, dst, len);
205     len /= 3;
206     while (len--)
207     {
208         DWORD field;
209         field = ((DWORD)src[2] << 16) + ((DWORD)src[1] << 8) + (DWORD)src[0];
210         if (src[2] & 0x80)
211             field |= 0xFF000000U;
212         *(dst++) += field;
213         ++src;
214     }
215 }
216
217 static void mix32(INT *src, LONGLONG *dst, unsigned len)
218 {
219     TRACE("%p - %p %d\n", src, dst, len);
220     len /= 4;
221     while (len--)
222         *(dst++) += le32(*(src++));
223 }
224
225 const mixfunc mixfunctions[4] = {
226     (mixfunc)mix8,
227     (mixfunc)mix16,
228     (mixfunc)mix24,
229     (mixfunc)mix32
230 };
231
232 static void norm8(INT *src, signed char *dst, unsigned len)
233 {
234     TRACE("%p - %p %d\n", src, dst, len);
235     while (len--)
236     {
237         *dst = (*src) + 0x80;
238         if (*src < -0x80)
239             *dst = 0;
240         else if (*src > 0x7f)
241             *dst = 0xff;
242         ++dst;
243         ++src;
244     }
245 }
246
247 static void norm16(INT *src, SHORT *dst, unsigned len)
248 {
249     TRACE("%p - %p %d\n", src, dst, len);
250     len /= 2;
251     while (len--)
252     {
253         *dst = le16(*src);
254         if (*src <= -0x8000)
255             *dst = le16(0x8000);
256         else if (*src > 0x7fff)
257             *dst = le16(0x7fff);
258         ++dst;
259         ++src;
260     }
261 }
262
263 static void norm24(INT *src, BYTE *dst, unsigned len)
264 {
265     TRACE("%p - %p %d\n", src, dst, len);
266     len /= 3;
267     while (len--)
268     {
269         if (*src <= -0x800000)
270         {
271             dst[0] = 0;
272             dst[1] = 0;
273             dst[2] = 0x80;
274         }
275         else if (*src > 0x7fffff)
276         {
277             dst[0] = 0xff;
278             dst[1] = 0xff;
279             dst[2] = 0x7f;
280         }
281         else
282         {
283             dst[0] = *src;
284             dst[1] = *src >> 8;
285             dst[2] = *src >> 16;
286         }
287         ++dst;
288         ++src;
289     }
290 }
291
292 static void norm32(LONGLONG *src, INT *dst, unsigned len)
293 {
294     TRACE("%p - %p %d\n", src, dst, len);
295     len /= 4;
296     while (len--)
297     {
298         *dst = le32(*src);
299         if (*src <= -(LONGLONG)0x80000000)
300             *dst = le32(0x80000000);
301         else if (*src > 0x7fffffff)
302             *dst = le32(0x7fffffff);
303         ++dst;
304         ++src;
305     }
306 }
307
308 const normfunc normfunctions[4] = {
309     (normfunc)norm8,
310     (normfunc)norm16,
311     (normfunc)norm24,
312     (normfunc)norm32,
313 };