- Merge from trunk up to r45543
[reactos.git] / dll / directx / dsound_new / 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 inline void src_advance(const void **src, UINT stride, INT *count, UINT *freqAcc, UINT adj)
62 {
63 *freqAcc += adj;
64 if (*freqAcc >= (1 << DSOUND_FREQSHIFT))
65 {
66 ULONG adv = (*freqAcc >> DSOUND_FREQSHIFT);
67 *freqAcc &= (1 << DSOUND_FREQSHIFT) - 1;
68 *(const char **)src += adv * stride;
69 *count -= adv;
70 }
71 }
72
73 static void convert_8_to_8 (const void *src, void *dst, UINT src_stride,
74 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
75 {
76 while (count > 0)
77 {
78 *(BYTE *)dst = *(const BYTE *)src;
79
80 dst = (char *)dst + dst_stride;
81 src_advance(&src, src_stride, &count, &freqAcc, adj);
82 }
83 }
84
85 static void convert_8_to_16 (const void *src, void *dst, UINT src_stride,
86 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
87 {
88 while (count > 0)
89 {
90 WORD dest = *(const BYTE *)src, *dest16 = dst;
91 *dest16 = le16(dest * 257 - 32768);
92
93 dst = (char *)dst + dst_stride;
94 src_advance(&src, src_stride, &count, &freqAcc, adj);
95 }
96 }
97
98 static void convert_8_to_24 (const void *src, void *dst, UINT src_stride,
99 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
100 {
101 while (count > 0)
102 {
103 BYTE dest = *(const BYTE *)src;
104 BYTE *dest24 = dst;
105 dest24[0] = dest;
106 dest24[1] = dest;
107 dest24[2] = dest - 0x80;
108
109 dst = (char *)dst + dst_stride;
110 src_advance(&src, src_stride, &count, &freqAcc, adj);
111 }
112 }
113
114 static void convert_8_to_32 (const void *src, void *dst, UINT src_stride,
115 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
116 {
117 while (count > 0)
118 {
119 DWORD dest = *(const BYTE *)src, *dest32 = dst;
120 *dest32 = le32(dest * 16843009 - 2147483648U);
121
122 dst = (char *)dst + dst_stride;
123 src_advance(&src, src_stride, &count, &freqAcc, adj);
124 }
125 }
126
127 static void convert_16_to_8 (const void *src, void *dst, UINT src_stride,
128 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
129 {
130 while (count > 0)
131 {
132 BYTE *dst8 = dst;
133 *dst8 = (le16(*(const WORD *)src)) / 256;
134 *dst8 -= 0x80;
135
136 dst = (char *)dst + dst_stride;
137 src_advance(&src, src_stride, &count, &freqAcc, adj);
138 }
139 }
140
141 static void convert_16_to_16 (const void *src, void *dst, UINT src_stride,
142 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
143 {
144 while (count > 0)
145 {
146 *(WORD *)dst = *(const WORD *)src;
147
148 dst = (char *)dst + dst_stride;
149 src_advance(&src, src_stride, &count, &freqAcc, adj);
150 }
151 }
152
153 static void convert_16_to_24 (const void *src, void *dst, UINT src_stride,
154 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
155 {
156 while (count > 0)
157 {
158 WORD dest = le16(*(const WORD *)src);
159 BYTE *dest24 = dst;
160
161 dest24[0] = dest / 256;
162 dest24[1] = dest;
163 dest24[2] = dest / 256;
164
165 dst = (char *)dst + dst_stride;
166 src_advance(&src, src_stride, &count, &freqAcc, adj);
167 }
168 }
169
170 static void convert_16_to_32 (const void *src, void *dst, UINT src_stride,
171 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
172 {
173 while (count > 0)
174 {
175 DWORD dest = *(const WORD *)src, *dest32 = dst;
176 *dest32 = dest * 65537;
177
178 dst = (char *)dst + dst_stride;
179 src_advance(&src, src_stride, &count, &freqAcc, adj);
180 }
181 }
182
183 static void convert_24_to_8 (const void *src, void *dst, UINT src_stride,
184 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
185 {
186 while (count > 0)
187 {
188 BYTE *dst8 = dst;
189 *dst8 = ((const BYTE *)src)[2];
190
191 dst = (char *)dst + dst_stride;
192 src_advance(&src, src_stride, &count, &freqAcc, adj);
193 }
194 }
195
196 static void convert_24_to_16 (const void *src, void *dst, UINT src_stride,
197 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
198 {
199 while (count > 0)
200 {
201 WORD *dest16 = dst;
202 const BYTE *source = src;
203 *dest16 = le16(source[2] * 256 + source[1]);
204
205 dst = (char *)dst + dst_stride;
206 src_advance(&src, src_stride, &count, &freqAcc, adj);
207 }
208 }
209
210 static void convert_24_to_24 (const void *src, void *dst, UINT src_stride,
211 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
212 {
213 while (count > 0)
214 {
215 BYTE *dest24 = dst;
216 const BYTE *src24 = src;
217
218 dest24[0] = src24[0];
219 dest24[1] = src24[1];
220 dest24[2] = src24[2];
221
222 dst = (char *)dst + dst_stride;
223 src_advance(&src, src_stride, &count, &freqAcc, adj);
224 }
225 }
226
227 static void convert_24_to_32 (const void *src, void *dst, UINT src_stride,
228 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
229 {
230 while (count > 0)
231 {
232 DWORD *dest32 = dst;
233 const BYTE *source = src;
234 *dest32 = le32(source[2] * 16777217 + source[1] * 65536 + source[0] * 256);
235
236 dst = (char *)dst + dst_stride;
237 src_advance(&src, src_stride, &count, &freqAcc, adj);
238 }
239 }
240
241 static void convert_32_to_8 (const void *src, void *dst, UINT src_stride,
242 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
243 {
244 while (count > 0)
245 {
246 BYTE *dst8 = dst;
247 *dst8 = (le32(*(const DWORD *)src) / 16777216);
248 *dst8 -= 0x80;
249
250 dst = (char *)dst + dst_stride;
251 src_advance(&src, src_stride, &count, &freqAcc, adj);
252 }
253 }
254
255 static void convert_32_to_16 (const void *src, void *dst, UINT src_stride,
256 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
257 {
258 while (count > 0)
259 {
260 WORD *dest16 = dst;
261 *dest16 = le16(le32(*(const DWORD *)src) / 65536);
262
263 dst = (char *)dst + dst_stride;
264 src_advance(&src, src_stride, &count, &freqAcc, adj);
265 }
266 }
267
268 static void convert_32_to_24 (const void *src, void *dst, UINT src_stride,
269 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
270 {
271 while (count > 0)
272 {
273 DWORD dest = le32(*(const DWORD *)src);
274 BYTE *dest24 = dst;
275
276 dest24[0] = dest / 256;
277 dest24[1] = dest / 65536;
278 dest24[2] = dest / 16777216;
279
280 dst = (char *)dst + dst_stride;
281 src_advance(&src, src_stride, &count, &freqAcc, adj);
282 }
283 }
284
285 static void convert_32_to_32 (const void *src, void *dst, UINT src_stride,
286 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
287 {
288 while (count > 0)
289 {
290 DWORD *dest = dst;
291 *dest = *(const DWORD *)src;
292
293 dst = (char *)dst + dst_stride;
294 src_advance(&src, src_stride, &count, &freqAcc, adj);
295 }
296 }
297
298 const bitsconvertfunc convertbpp[4][4] = {
299 { convert_8_to_8, convert_8_to_16, convert_8_to_24, convert_8_to_32 },
300 { convert_16_to_8, convert_16_to_16, convert_16_to_24, convert_16_to_32 },
301 { convert_24_to_8, convert_24_to_16, convert_24_to_24, convert_24_to_32 },
302 { convert_32_to_8, convert_32_to_16, convert_32_to_24, convert_32_to_32 },
303 };
304
305 static void mix8(signed char *src, INT *dst, unsigned len)
306 {
307 TRACE("%p - %p %d\n", src, dst, len);
308 while (len--)
309 /* 8-bit WAV is unsigned, it's here converted to signed, normalize function will convert it back again */
310 *(dst++) += (signed char)((BYTE)*(src++) - (BYTE)0x80);
311 }
312
313 static void mix16(SHORT *src, INT *dst, unsigned len)
314 {
315 TRACE("%p - %p %d\n", src, dst, len);
316 len /= 2;
317 while (len--)
318 {
319 *dst += le16(*src);
320 ++dst; ++src;
321 }
322 }
323
324 static void mix24(BYTE *src, INT *dst, unsigned len)
325 {
326 TRACE("%p - %p %d\n", src, dst, len);
327 len /= 3;
328 while (len--)
329 {
330 DWORD field;
331 field = ((DWORD)src[2] << 16) + ((DWORD)src[1] << 8) + (DWORD)src[0];
332 if (src[2] & 0x80)
333 field |= 0xFF000000U;
334 *(dst++) += field;
335 ++src;
336 }
337 }
338
339 static void mix32(INT *src, LONGLONG *dst, unsigned len)
340 {
341 TRACE("%p - %p %d\n", src, dst, len);
342 len /= 4;
343 while (len--)
344 *(dst++) += le32(*(src++));
345 }
346
347 const mixfunc mixfunctions[4] = {
348 (mixfunc)mix8,
349 (mixfunc)mix16,
350 (mixfunc)mix24,
351 (mixfunc)mix32
352 };
353
354 static void norm8(INT *src, signed char *dst, unsigned len)
355 {
356 TRACE("%p - %p %d\n", src, dst, len);
357 while (len--)
358 {
359 *dst = (*src) + 0x80;
360 if (*src < -0x80)
361 *dst = 0;
362 else if (*src > 0x7f)
363 *dst = 0xff;
364 ++dst;
365 ++src;
366 }
367 }
368
369 static void norm16(INT *src, SHORT *dst, unsigned len)
370 {
371 TRACE("%p - %p %d\n", src, dst, len);
372 len /= 2;
373 while (len--)
374 {
375 *dst = le16(*src);
376 if (*src <= -0x8000)
377 *dst = le16(0x8000);
378 else if (*src > 0x7fff)
379 *dst = le16(0x7fff);
380 ++dst;
381 ++src;
382 }
383 }
384
385 static void norm24(INT *src, BYTE *dst, unsigned len)
386 {
387 TRACE("%p - %p %d\n", src, dst, len);
388 len /= 3;
389 while (len--)
390 {
391 if (*src <= -0x800000)
392 {
393 dst[0] = 0;
394 dst[1] = 0;
395 dst[2] = 0x80;
396 }
397 else if (*src > 0x7fffff)
398 {
399 dst[0] = 0xff;
400 dst[1] = 0xff;
401 dst[2] = 0x7f;
402 }
403 else
404 {
405 dst[0] = *src;
406 dst[1] = *src >> 8;
407 dst[2] = *src >> 16;
408 }
409 ++dst;
410 ++src;
411 }
412 }
413
414 static void norm32(LONGLONG *src, INT *dst, unsigned len)
415 {
416 TRACE("%p - %p %d\n", src, dst, len);
417 len /= 4;
418 while (len--)
419 {
420 *dst = le32(*src);
421 if (*src <= -(LONGLONG)0x80000000)
422 *dst = le32(0x80000000);
423 else if (*src > 0x7fffffff)
424 *dst = le32(0x7fffffff);
425 ++dst;
426 ++src;
427 }
428 }
429
430 const normfunc normfunctions[4] = {
431 (normfunc)norm8,
432 (normfunc)norm16,
433 (normfunc)norm24,
434 (normfunc)norm32,
435 };