ntdll: Fix frame address check in RtlCaptureStackBackTrace to match what RtlUnwind...
[wine.git] / dlls / dsound / dsound_convert.c
blob057ae16cb0c9a40820a8057a684c9a6a5ebd42f0
1 /* DirectSound format conversion and mixing routines
3 * Copyright 2007 Maarten Lankhorst
4 * Copyright 2011 Owen Rudge for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* 8 bits is unsigned, the rest is signed.
22 * First I tried to reuse existing stuff from alsa-lib, after that
23 * didn't work, I gave up and just went for individual hacks.
25 * 24 bit is expensive to do, due to unaligned access.
26 * In dlls/winex11.drv/dib_convert.c convert_888_to_0888_asis there is a way
27 * around it, but I'm happy current code works, maybe something for later.
29 * The ^ 0x80 flips the signed bit, this is the conversion from
30 * signed (-128.. 0.. 127) to unsigned (0...255)
31 * This is only temporary: All 8 bit data should be converted to signed.
32 * then when fed to the sound card, it should be converted to unsigned again.
34 * Sound is LITTLE endian
37 #include "config.h"
39 #include <stdarg.h>
41 #define NONAMELESSSTRUCT
42 #define NONAMELESSUNION
43 #include "windef.h"
44 #include "winbase.h"
45 #include "mmsystem.h"
46 #include "winternl.h"
47 #include "wine/debug.h"
48 #include "dsound.h"
49 #include "dsdriver.h"
50 #include "dsound_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
54 #ifdef WORDS_BIGENDIAN
55 #define le16(x) RtlUshortByteSwap((x))
56 #define le32(x) RtlUlongByteSwap((x))
57 #else
58 #define le16(x) (x)
59 #define le32(x) (x)
60 #endif
62 static inline void src_advance(const void **src, UINT stride, INT *count, UINT *freqAcc, UINT adj)
64 *freqAcc += adj;
65 if (*freqAcc >= (1 << DSOUND_FREQSHIFT))
67 ULONG adv = (*freqAcc >> DSOUND_FREQSHIFT);
68 *freqAcc &= (1 << DSOUND_FREQSHIFT) - 1;
69 *(const char **)src += adv * stride;
70 *count -= adv;
74 static void convert_8_to_8 (const void *src, void *dst, UINT src_stride,
75 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
77 while (count > 0)
79 *(BYTE *)dst = *(const BYTE *)src;
81 dst = (char *)dst + dst_stride;
82 src_advance(&src, src_stride, &count, &freqAcc, adj);
86 static void convert_8_to_16 (const void *src, void *dst, UINT src_stride,
87 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
89 while (count > 0)
91 WORD dest = *(const BYTE *)src, *dest16 = dst;
92 *dest16 = le16(dest * 257 - 32768);
94 dst = (char *)dst + dst_stride;
95 src_advance(&src, src_stride, &count, &freqAcc, adj);
99 static void convert_8_to_24 (const void *src, void *dst, UINT src_stride,
100 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
102 while (count > 0)
104 BYTE dest = *(const BYTE *)src;
105 BYTE *dest24 = dst;
106 dest24[0] = dest;
107 dest24[1] = dest;
108 dest24[2] = dest - 0x80;
110 dst = (char *)dst + dst_stride;
111 src_advance(&src, src_stride, &count, &freqAcc, adj);
115 static void convert_8_to_32 (const void *src, void *dst, UINT src_stride,
116 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
118 while (count > 0)
120 DWORD dest = *(const BYTE *)src, *dest32 = dst;
121 *dest32 = le32(dest * 16843009 - 2147483648U);
123 dst = (char *)dst + dst_stride;
124 src_advance(&src, src_stride, &count, &freqAcc, adj);
128 static void convert_16_to_8 (const void *src, void *dst, UINT src_stride,
129 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
131 while (count > 0)
133 BYTE *dst8 = dst;
134 *dst8 = (le16(*(const WORD *)src)) / 256;
135 *dst8 -= 0x80;
137 dst = (char *)dst + dst_stride;
138 src_advance(&src, src_stride, &count, &freqAcc, adj);
142 static void convert_16_to_16 (const void *src, void *dst, UINT src_stride,
143 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
145 while (count > 0)
147 *(WORD *)dst = *(const WORD *)src;
149 dst = (char *)dst + dst_stride;
150 src_advance(&src, src_stride, &count, &freqAcc, adj);
154 static void convert_16_to_24 (const void *src, void *dst, UINT src_stride,
155 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
157 while (count > 0)
159 WORD dest = le16(*(const WORD *)src);
160 BYTE *dest24 = dst;
162 dest24[0] = dest / 256;
163 dest24[1] = dest;
164 dest24[2] = dest / 256;
166 dst = (char *)dst + dst_stride;
167 src_advance(&src, src_stride, &count, &freqAcc, adj);
171 static void convert_16_to_32 (const void *src, void *dst, UINT src_stride,
172 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
174 while (count > 0)
176 DWORD dest = *(const WORD *)src, *dest32 = dst;
177 *dest32 = dest * 65537;
179 dst = (char *)dst + dst_stride;
180 src_advance(&src, src_stride, &count, &freqAcc, adj);
184 static void convert_24_to_8 (const void *src, void *dst, UINT src_stride,
185 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
187 while (count > 0)
189 BYTE *dst8 = dst;
190 *dst8 = ((const BYTE *)src)[2];
192 dst = (char *)dst + dst_stride;
193 src_advance(&src, src_stride, &count, &freqAcc, adj);
197 static void convert_24_to_16 (const void *src, void *dst, UINT src_stride,
198 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
200 while (count > 0)
202 WORD *dest16 = dst;
203 const BYTE *source = src;
204 *dest16 = le16(source[2] * 256 + source[1]);
206 dst = (char *)dst + dst_stride;
207 src_advance(&src, src_stride, &count, &freqAcc, adj);
211 static void convert_24_to_24 (const void *src, void *dst, UINT src_stride,
212 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
214 while (count > 0)
216 BYTE *dest24 = dst;
217 const BYTE *src24 = src;
219 dest24[0] = src24[0];
220 dest24[1] = src24[1];
221 dest24[2] = src24[2];
223 dst = (char *)dst + dst_stride;
224 src_advance(&src, src_stride, &count, &freqAcc, adj);
228 static void convert_24_to_32 (const void *src, void *dst, UINT src_stride,
229 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
231 while (count > 0)
233 DWORD *dest32 = dst;
234 const BYTE *source = src;
235 *dest32 = le32(source[2] * 16777217 + source[1] * 65536 + source[0] * 256);
237 dst = (char *)dst + dst_stride;
238 src_advance(&src, src_stride, &count, &freqAcc, adj);
242 static void convert_32_to_8 (const void *src, void *dst, UINT src_stride,
243 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
245 while (count > 0)
247 BYTE *dst8 = dst;
248 *dst8 = (le32(*(const DWORD *)src) / 16777216);
249 *dst8 -= 0x80;
251 dst = (char *)dst + dst_stride;
252 src_advance(&src, src_stride, &count, &freqAcc, adj);
256 static void convert_32_to_16 (const void *src, void *dst, UINT src_stride,
257 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
259 while (count > 0)
261 WORD *dest16 = dst;
262 *dest16 = le16(le32(*(const DWORD *)src) / 65536);
264 dst = (char *)dst + dst_stride;
265 src_advance(&src, src_stride, &count, &freqAcc, adj);
269 static void convert_32_to_24 (const void *src, void *dst, UINT src_stride,
270 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
272 while (count > 0)
274 DWORD dest = le32(*(const DWORD *)src);
275 BYTE *dest24 = dst;
277 dest24[0] = dest / 256;
278 dest24[1] = dest / 65536;
279 dest24[2] = dest / 16777216;
281 dst = (char *)dst + dst_stride;
282 src_advance(&src, src_stride, &count, &freqAcc, adj);
286 static void convert_32_to_32 (const void *src, void *dst, UINT src_stride,
287 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
289 while (count > 0)
291 DWORD *dest = dst;
292 *dest = *(const DWORD *)src;
294 dst = (char *)dst + dst_stride;
295 src_advance(&src, src_stride, &count, &freqAcc, adj);
299 static void convert_ieee_32_to_8 (const void *src, void *dst, UINT src_stride,
300 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
302 while (count > 0)
304 DWORD src_le = le32(*(DWORD *) src);
305 float v = *((float *) &src_le);
306 INT8 d = 0;
308 if (v < -1.0f)
309 d = -128;
310 else if (v > 1.0f)
311 d = 127;
312 else
313 d = v * 127.5f - 0.5f;
315 *(BYTE *) dst = d ^ 0x80;
317 dst = (char *)dst + dst_stride;
318 src_advance(&src, src_stride, &count, &freqAcc, adj);
322 static void convert_ieee_32_to_16 (const void *src, void *dst, UINT src_stride,
323 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
325 while (count > 0)
327 DWORD src_le = le32(*(DWORD *) src);
328 float v = *((float *) &src_le);
330 INT16 *d = (INT16 *) dst;
332 if (v < -1.0f)
333 *d = -32768;
334 else if (v > 1.0f)
335 *d = 32767;
336 else
337 *d = v * 32767.5f - 0.5f;
339 *d = le16(*d);
341 dst = (char *)dst + dst_stride;
342 src_advance(&src, src_stride, &count, &freqAcc, adj);
346 static void convert_ieee_32_to_24 (const void *src, void *dst, UINT src_stride,
347 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
349 while (count > 0)
351 DWORD src_le = le32(*(DWORD *) src);
352 float v = *((float *) &src_le);
353 BYTE *dest24 = dst;
355 if (v < -1.0f)
357 dest24[0] = 0;
358 dest24[1] = 0;
359 dest24[2] = 0x80;
361 else if (v > 1.0f)
363 dest24[0] = 0xff;
364 dest24[1] = 0xff;
365 dest24[2] = 0x7f;
367 else if (v < 0.0f)
369 dest24[0] = v * 8388608.0f;
370 dest24[1] = v * 32768.0f;
371 dest24[2] = v * 128.0f;
373 else if (v >= 0.0f)
375 dest24[0] = v * 8388608.0f;
376 dest24[1] = v * 32768.0f;
377 dest24[2] = v * 127.0f;
380 dst = (char *)dst + dst_stride;
381 src_advance(&src, src_stride, &count, &freqAcc, adj);
385 static void convert_ieee_32_to_32 (const void *src, void *dst, UINT src_stride,
386 UINT dst_stride, INT count, UINT freqAcc, UINT adj)
388 while (count > 0)
390 DWORD src_le = le32(*(DWORD *) src);
391 float v = *((float *) &src_le);
392 INT32 *d = (INT32 *) dst;
394 if (v < -1.0f)
395 *d = -2147483647 - 1; /* silence warning */
396 else if (v > 1.0f)
397 *d = 2147483647;
398 else
399 *d = v * 2147483647.5f - 0.5f;
401 *d = le32(*d);
403 dst = (char *)dst + dst_stride;
404 src_advance(&src, src_stride, &count, &freqAcc, adj);
408 const bitsconvertfunc convertbpp[5][4] = {
409 { convert_8_to_8, convert_8_to_16, convert_8_to_24, convert_8_to_32 },
410 { convert_16_to_8, convert_16_to_16, convert_16_to_24, convert_16_to_32 },
411 { convert_24_to_8, convert_24_to_16, convert_24_to_24, convert_24_to_32 },
412 { convert_32_to_8, convert_32_to_16, convert_32_to_24, convert_32_to_32 },
413 { convert_ieee_32_to_8, convert_ieee_32_to_16, convert_ieee_32_to_24, convert_ieee_32_to_32 },
416 static void mix8(signed char *src, INT *dst, unsigned len)
418 TRACE("%p - %p %d\n", src, dst, len);
419 while (len--)
420 /* 8-bit WAV is unsigned, it's here converted to signed, normalize function will convert it back again */
421 *(dst++) += (signed char)((BYTE)*(src++) - (BYTE)0x80);
424 static void mix16(SHORT *src, INT *dst, unsigned len)
426 TRACE("%p - %p %d\n", src, dst, len);
427 len /= 2;
428 while (len--)
430 *dst += le16(*src);
431 ++dst; ++src;
435 static void mix24(BYTE *src, INT *dst, unsigned len)
437 TRACE("%p - %p %d\n", src, dst, len);
438 len /= 3;
439 while (len--)
441 DWORD field;
442 field = ((DWORD)src[2] << 16) + ((DWORD)src[1] << 8) + (DWORD)src[0];
443 if (src[2] & 0x80)
444 field |= 0xFF000000U;
445 *(dst++) += field;
446 ++src;
450 static void mix32(INT *src, LONGLONG *dst, unsigned len)
452 TRACE("%p - %p %d\n", src, dst, len);
453 len /= 4;
454 while (len--)
455 *(dst++) += le32(*(src++));
458 const mixfunc mixfunctions[4] = {
459 (mixfunc)mix8,
460 (mixfunc)mix16,
461 (mixfunc)mix24,
462 (mixfunc)mix32
465 static void norm8(INT *src, signed char *dst, unsigned len)
467 TRACE("%p - %p %d\n", src, dst, len);
468 while (len--)
470 *dst = (*src) + 0x80;
471 if (*src < -0x80)
472 *dst = 0;
473 else if (*src > 0x7f)
474 *dst = 0xff;
475 ++dst;
476 ++src;
480 static void norm16(INT *src, SHORT *dst, unsigned len)
482 TRACE("%p - %p %d\n", src, dst, len);
483 len /= 2;
484 while (len--)
486 *dst = le16(*src);
487 if (*src <= -0x8000)
488 *dst = le16(0x8000);
489 else if (*src > 0x7fff)
490 *dst = le16(0x7fff);
491 ++dst;
492 ++src;
496 static void norm24(INT *src, BYTE *dst, unsigned len)
498 TRACE("%p - %p %d\n", src, dst, len);
499 len /= 3;
500 while (len--)
502 if (*src <= -0x800000)
504 dst[0] = 0;
505 dst[1] = 0;
506 dst[2] = 0x80;
508 else if (*src > 0x7fffff)
510 dst[0] = 0xff;
511 dst[1] = 0xff;
512 dst[2] = 0x7f;
514 else
516 dst[0] = *src;
517 dst[1] = *src >> 8;
518 dst[2] = *src >> 16;
520 ++dst;
521 ++src;
525 static void norm32(LONGLONG *src, INT *dst, unsigned len)
527 TRACE("%p - %p %d\n", src, dst, len);
528 len /= 4;
529 while (len--)
531 *dst = le32(*src);
532 if (*src <= -(LONGLONG)0x80000000)
533 *dst = le32(0x80000000);
534 else if (*src > 0x7fffffff)
535 *dst = le32(0x7fffffff);
536 ++dst;
537 ++src;
541 const normfunc normfunctions[4] = {
542 (normfunc)norm8,
543 (normfunc)norm16,
544 (normfunc)norm24,
545 (normfunc)norm32,