annotate libfaad2/dither.c @ 10911:a2b9b13af73f

Update readme's. Remove incorrect bug :)
author lumag
date Sun, 21 Sep 2003 13:01:25 +0000
parents e989150f8216
children 3185f64f6350
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10725
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
1 /* This program is licensed under the GNU Library General Public License, version 2,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
2 * a copy of which is included with this program (with filename LICENSE.LGPL).
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
3 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
4 * (c) 2002 John Edwards
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
5 * mostly lifted from work by Frank Klemm
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
6 * random functions for dithering.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
7 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
8 * last modified:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
9 * $Id$
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
10 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
11 #include "common.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
12
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
13 #ifndef FIXED_POINT
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
14
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
15 #include <string.h>
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
16 #include "dither.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
17 #include "common.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
18
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
19
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
20 double
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
21 Random_Equi ( double mult ) // gives a equal distributed random number
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
22 { // between -2^31*mult and +2^31*mult
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
23 return mult * (int) random_int ();
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
24 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
25
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
26 double
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
27 Random_Triangular ( double mult ) // gives a triangular distributed random number
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
28 { // between -2^32*mult and +2^32*mult
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
29 return mult * ( (double) (int) random_int () + (double) (int) random_int () );
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
30 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
31
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
32 /*********************************************************************************************************************/
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
33
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
34 static const float32_t F44_0 [16 + 32] = {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
35 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
36 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
37
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
38 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
39 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
40
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
41 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
42 (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0, (float)0
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
43 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
44
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
45
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
46 static const float32_t F44_1 [16 + 32] = { /* SNR(w) = 4.843163 dB, SNR = -3.192134 dB */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
47 (float) 0.85018292704024355931, (float) 0.29089597350995344721, (float)-0.05021866022121039450, (float)-0.23545456294599161833,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
48 (float)-0.58362726442227032096, (float)-0.67038978965193036429, (float)-0.38566861572833459221, (float)-0.15218663390367969967,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
49 (float)-0.02577543084864530676, (float) 0.14119295297688728127, (float) 0.22398848581628781612, (float) 0.15401727203382084116,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
50 (float) 0.05216161232906000929, (float)-0.00282237820999675451, (float)-0.03042794608323867363, (float)-0.03109780942998826024,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
51
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
52 (float) 0.85018292704024355931, (float) 0.29089597350995344721, (float)-0.05021866022121039450, (float)-0.23545456294599161833,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
53 (float)-0.58362726442227032096, (float)-0.67038978965193036429, (float)-0.38566861572833459221, (float)-0.15218663390367969967,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
54 (float)-0.02577543084864530676, (float) 0.14119295297688728127, (float) 0.22398848581628781612, (float) 0.15401727203382084116,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
55 (float) 0.05216161232906000929, (float)-0.00282237820999675451, (float)-0.03042794608323867363, (float)-0.03109780942998826024,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
56
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
57 (float) 0.85018292704024355931, (float) 0.29089597350995344721, (float)-0.05021866022121039450, (float)-0.23545456294599161833,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
58 (float)-0.58362726442227032096, (float)-0.67038978965193036429, (float)-0.38566861572833459221, (float)-0.15218663390367969967,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
59 (float)-0.02577543084864530676, (float) 0.14119295297688728127, (float) 0.22398848581628781612, (float) 0.15401727203382084116,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
60 (float) 0.05216161232906000929, (float)-0.00282237820999675451, (float)-0.03042794608323867363, (float)-0.03109780942998826024,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
61 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
62
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
63
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
64 static const float32_t F44_2 [16 + 32] = { /* SNR(w) = 10.060213 dB, SNR = -12.766730 dB */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
65 (float) 1.78827593892108555290, (float) 0.95508210637394326553, (float)-0.18447626783899924429, (float)-0.44198126506275016437,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
66 (float)-0.88404052492547413497, (float)-1.42218907262407452967, (float)-1.02037566838362314995, (float)-0.34861755756425577264,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
67 (float)-0.11490230170431934434, (float) 0.12498899339968611803, (float) 0.38065885268563131927, (float) 0.31883491321310506562,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
68 (float) 0.10486838686563442765, (float)-0.03105361685110374845, (float)-0.06450524884075370758, (float)-0.02939198261121969816,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
69
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
70 (float) 1.78827593892108555290, (float) 0.95508210637394326553, (float)-0.18447626783899924429, (float)-0.44198126506275016437,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
71 (float)-0.88404052492547413497, (float)-1.42218907262407452967, (float)-1.02037566838362314995, (float)-0.34861755756425577264,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
72 (float)-0.11490230170431934434, (float) 0.12498899339968611803, (float) 0.38065885268563131927, (float) 0.31883491321310506562,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
73 (float) 0.10486838686563442765, (float)-0.03105361685110374845, (float)-0.06450524884075370758, (float)-0.02939198261121969816,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
74
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
75 (float) 1.78827593892108555290, (float) 0.95508210637394326553, (float)-0.18447626783899924429, (float)-0.44198126506275016437,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
76 (float)-0.88404052492547413497, (float)-1.42218907262407452967, (float)-1.02037566838362314995, (float)-0.34861755756425577264,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
77 (float)-0.11490230170431934434, (float) 0.12498899339968611803, (float) 0.38065885268563131927, (float) 0.31883491321310506562,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
78 (float) 0.10486838686563442765, (float)-0.03105361685110374845, (float)-0.06450524884075370758, (float)-0.02939198261121969816,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
79 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
80
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
81
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
82 static const float32_t F44_3 [16 + 32] = { /* SNR(w) = 15.382598 dB, SNR = -29.402334 dB */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
83 (float) 2.89072132015058161445, (float) 2.68932810943698754106, (float) 0.21083359339410251227, (float)-0.98385073324997617515,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
84 (float)-1.11047823227097316719, (float)-2.18954076314139673147, (float)-2.36498032881953056225, (float)-0.95484132880101140785,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
85 (float)-0.23924057925542965158, (float)-0.13865235703915925642, (float) 0.43587843191057992846, (float) 0.65903257226026665927,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
86 (float) 0.24361815372443152787, (float)-0.00235974960154720097, (float) 0.01844166574603346289, (float) 0.01722945988740875099,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
87
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
88 (float) 2.89072132015058161445, (float) 2.68932810943698754106, (float) 0.21083359339410251227, (float)-0.98385073324997617515,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
89 (float)-1.11047823227097316719, (float)-2.18954076314139673147, (float)-2.36498032881953056225, (float)-0.95484132880101140785,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
90 (float)-0.23924057925542965158, (float)-0.13865235703915925642, (float) 0.43587843191057992846, (float) 0.65903257226026665927,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
91 (float) 0.24361815372443152787, (float)-0.00235974960154720097, (float) 0.01844166574603346289, (float) 0.01722945988740875099,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
92
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
93 (float) 2.89072132015058161445, (float) 2.68932810943698754106, (float) 0.21083359339410251227, (float)-0.98385073324997617515,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
94 (float)-1.11047823227097316719, (float)-2.18954076314139673147, (float)-2.36498032881953056225, (float)-0.95484132880101140785,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
95 (float)-0.23924057925542965158, (float)-0.13865235703915925642, (float) 0.43587843191057992846, (float) 0.65903257226026665927,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
96 (float) 0.24361815372443152787, (float)-0.00235974960154720097, (float) 0.01844166574603346289, (float) 0.01722945988740875099
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
97 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
98
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
99
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
100 double
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
101 scalar16 ( const float32_t* x, const float32_t* y )
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
102 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
103 return x[ 0]*y[ 0] + x[ 1]*y[ 1] + x[ 2]*y[ 2] + x[ 3]*y[ 3]
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
104 + x[ 4]*y[ 4] + x[ 5]*y[ 5] + x[ 6]*y[ 6] + x[ 7]*y[ 7]
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
105 + x[ 8]*y[ 8] + x[ 9]*y[ 9] + x[10]*y[10] + x[11]*y[11]
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
106 + x[12]*y[12] + x[13]*y[13] + x[14]*y[14] + x[15]*y[15];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
107 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
108
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
109
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
110 void
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
111 Init_Dither ( unsigned char bits, unsigned char shapingtype )
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
112 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
113 static uint8_t default_dither [] = { 92, 92, 88, 84, 81, 78, 74, 67, 0, 0 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
114 static const float32_t* F [] = { F44_0, F44_1, F44_2, F44_3 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
115 uint8_t index;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
116
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
117 if (shapingtype > 3) shapingtype = 3;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
118 index = bits - 11 - shapingtype;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
119 if (index > 9) index = 9;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
120
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
121 memset ( Dither.ErrorHistory , 0, sizeof (Dither.ErrorHistory ) );
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
122 memset ( Dither.DitherHistory, 0, sizeof (Dither.DitherHistory) );
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
123
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
124 Dither.FilterCoeff = F [shapingtype];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
125 Dither.Mask = ((uint64_t)-1) << (32 - bits);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
126 Dither.Add = 0.5 * ((1L << (32 - bits)) - 1);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
127 Dither.Dither = 0.01*default_dither[index] / (((int64_t)1) << bits);
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
128 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
129
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
130 #endif