annotate src/madplug/dither.c @ 968:317a7a8c002d trunk

[svn] - Rename flacng debug flag to FLACNG_DEBUG
author ertzing
date Fri, 20 Apr 2007 07:42:53 -0700
parents f931c9d744a5
children 59addab003d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
1 #include <stdio.h>
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
2 #include <assert.h>
922
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
3 #include "../../config.h"
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
4
922
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
5 #define MEXP 19937
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
6
922
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
7 #ifdef HAVE_SSE2
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
8 #define SSE2 1
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
9 #endif
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
10 #ifdef HAVE_ALTIVEC
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
11 #define ALTIVEC 1
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
12 #endif
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
13
922
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
14 #include "SFMT.h"
7e14701aef54 [svn] - replace random number generator in dithering code with SIMD-oriented Fast Mersenne Twister (SFMT). it reduces CPU load on SSE2 or AltiVec capable platform.
yaz
parents: 610
diff changeset
15 #include "SFMT.c"
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
16
924
f931c9d744a5 [svn] -adjust size of variable in dither code.
yaz
parents: 922
diff changeset
17 int triangular_dither_noise(int nbits)
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
18 {
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
19 // parameter nbits : the peak-to-peak amplitude desired (in bits)
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
20 // use with nbits set to 2 + nber of bits to be trimmed.
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
21 // (because triangular is made from two uniformly distributed processes,
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
22 // it starts at 2 bits peak-to-peak amplitude)
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
23 // see The Theory of Dithered Quantization by Robert Alexander Wannamaker
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
24 // for complete proof of why that's optimal
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
25
924
f931c9d744a5 [svn] -adjust size of variable in dither code.
yaz
parents: 922
diff changeset
26 int v = (gen_rand32() / 2 - gen_rand32() / 2); // in ]-2^31, 2^31[
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
27 //int signe = (v>0) ? 1 : -1;
924
f931c9d744a5 [svn] -adjust size of variable in dither code.
yaz
parents: 922
diff changeset
28 int P = 1 << (32 - nbits); // the power of 2
610
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
29 v /= P;
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
30 // now v in ]-2^(nbits-1), 2^(nbits-1) [
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
31
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
32 return v;
862190d39e00 [svn] - add madplug. It is not yet hooked up, I'll do that later.
nenolod
parents:
diff changeset
33 }