1#include <math.h>
2#include <string.h>
3#include "synth_filter.h"
4#include "mpc_dec.h"
5
6// unsigned long  clips = 0;
7
8/* C O N S T A N T S */
9#undef _
10#define _(value)  (float)(value##.##L / 0x10000)
11
12const float  Di_opt [32] [16] = {
13   { _(  0), _( -29), _( 213), _( -459), _( 2037), _(-5153), _(  6574), _(-37489), _(75038), _(37489), _(6574), _( 5153), _(2037), _( 459), _(213), _(29) },
14   { _( -1), _( -31), _( 218), _( -519), _( 2000), _(-5517), _(  5959), _(-39336), _(74992), _(35640), _(7134), _( 4788), _(2063), _( 401), _(208), _(26) },
15   { _( -1), _( -35), _( 222), _( -581), _( 1952), _(-5879), _(  5288), _(-41176), _(74856), _(33791), _(7640), _( 4425), _(2080), _( 347), _(202), _(24) },
16   { _( -1), _( -38), _( 225), _( -645), _( 1893), _(-6237), _(  4561), _(-43006), _(74630), _(31947), _(8092), _( 4063), _(2087), _( 294), _(196), _(21) },
17   { _( -1), _( -41), _( 227), _( -711), _( 1822), _(-6589), _(  3776), _(-44821), _(74313), _(30112), _(8492), _( 3705), _(2085), _( 244), _(190), _(19) },
18   { _( -1), _( -45), _( 228), _( -779), _( 1739), _(-6935), _(  2935), _(-46617), _(73908), _(28289), _(8840), _( 3351), _(2075), _( 197), _(183), _(17) },
19   { _( -1), _( -49), _( 228), _( -848), _( 1644), _(-7271), _(  2037), _(-48390), _(73415), _(26482), _(9139), _( 3004), _(2057), _( 153), _(176), _(16) },
20   { _( -2), _( -53), _( 227), _( -919), _( 1535), _(-7597), _(  1082), _(-50137), _(72835), _(24694), _(9389), _( 2663), _(2032), _( 111), _(169), _(14) },
21   { _( -2), _( -58), _( 224), _( -991), _( 1414), _(-7910), _(    70), _(-51853), _(72169), _(22929), _(9592), _( 2330), _(2001), _(  72), _(161), _(13) },
22   { _( -2), _( -63), _( 221), _(-1064), _( 1280), _(-8209), _(  -998), _(-53534), _(71420), _(21189), _(9750), _( 2006), _(1962), _(  36), _(154), _(11) },
23   { _( -2), _( -68), _( 215), _(-1137), _( 1131), _(-8491), _( -2122), _(-55178), _(70590), _(19478), _(9863), _( 1692), _(1919), _(   2), _(147), _(10) },
24   { _( -3), _( -73), _( 208), _(-1210), _(  970), _(-8755), _( -3300), _(-56778), _(69679), _(17799), _(9935), _( 1388), _(1870), _( -29), _(139), _( 9) },
25   { _( -3), _( -79), _( 200), _(-1283), _(  794), _(-8998), _( -4533), _(-58333), _(68692), _(16155), _(9966), _( 1095), _(1817), _( -57), _(132), _( 8) },
26   { _( -4), _( -85), _( 189), _(-1356), _(  605), _(-9219), _( -5818), _(-59838), _(67629), _(14548), _(9959), _(  814), _(1759), _( -83), _(125), _( 7) },
27   { _( -4), _( -91), _( 177), _(-1428), _(  402), _(-9416), _( -7154), _(-61289), _(66494), _(12980), _(9916), _(  545), _(1698), _(-106), _(117), _( 7) },
28   { _( -5), _( -97), _( 163), _(-1498), _(  185), _(-9585), _( -8540), _(-62684), _(65290), _(11455), _(9838), _(  288), _(1634), _(-127), _(111), _( 6) },
29   { _( -5), _(-104), _( 146), _(-1567), _(  -45), _(-9727), _( -9975), _(-64019), _(64019), _( 9975), _(9727), _(   45), _(1567), _(-146), _(104), _( 5) },
30   { _( -6), _(-111), _( 127), _(-1634), _( -288), _(-9838), _(-11455), _(-65290), _(62684), _( 8540), _(9585), _( -185), _(1498), _(-163), _( 97), _( 5) },
31   { _( -7), _(-117), _( 106), _(-1698), _( -545), _(-9916), _(-12980), _(-66494), _(61289), _( 7154), _(9416), _( -402), _(1428), _(-177), _( 91), _( 4) },
32   { _( -7), _(-125), _(  83), _(-1759), _( -814), _(-9959), _(-14548), _(-67629), _(59838), _( 5818), _(9219), _( -605), _(1356), _(-189), _( 85), _( 4) },
33   { _( -8), _(-132), _(  57), _(-1817), _(-1095), _(-9966), _(-16155), _(-68692), _(58333), _( 4533), _(8998), _( -794), _(1283), _(-200), _( 79), _( 3) },
34   { _( -9), _(-139), _(  29), _(-1870), _(-1388), _(-9935), _(-17799), _(-69679), _(56778), _( 3300), _(8755), _( -970), _(1210), _(-208), _( 73), _( 3) },
35   { _(-10), _(-147), _(  -2), _(-1919), _(-1692), _(-9863), _(-19478), _(-70590), _(55178), _( 2122), _(8491), _(-1131), _(1137), _(-215), _( 68), _( 2) },
36   { _(-11), _(-154), _( -36), _(-1962), _(-2006), _(-9750), _(-21189), _(-71420), _(53534), _(  998), _(8209), _(-1280), _(1064), _(-221), _( 63), _( 2) },
37   { _(-13), _(-161), _( -72), _(-2001), _(-2330), _(-9592), _(-22929), _(-72169), _(51853), _(  -70), _(7910), _(-1414), _( 991), _(-224), _( 58), _( 2) },
38   { _(-14), _(-169), _(-111), _(-2032), _(-2663), _(-9389), _(-24694), _(-72835), _(50137), _(-1082), _(7597), _(-1535), _( 919), _(-227), _( 53), _( 2) },
39   { _(-16), _(-176), _(-153), _(-2057), _(-3004), _(-9139), _(-26482), _(-73415), _(48390), _(-2037), _(7271), _(-1644), _( 848), _(-228), _( 49), _( 1) },
40   { _(-17), _(-183), _(-197), _(-2075), _(-3351), _(-8840), _(-28289), _(-73908), _(46617), _(-2935), _(6935), _(-1739), _( 779), _(-228), _( 45), _( 1) },
41   { _(-19), _(-190), _(-244), _(-2085), _(-3705), _(-8492), _(-30112), _(-74313), _(44821), _(-3776), _(6589), _(-1822), _( 711), _(-227), _( 41), _( 1) },
42   { _(-21), _(-196), _(-294), _(-2087), _(-4063), _(-8092), _(-31947), _(-74630), _(43006), _(-4561), _(6237), _(-1893), _( 645), _(-225), _( 38), _( 1) },
43   { _(-24), _(-202), _(-347), _(-2080), _(-4425), _(-7640), _(-33791), _(-74856), _(41176), _(-5288), _(5879), _(-1952), _( 581), _(-222), _( 35), _( 1) },
44   { _(-26), _(-208), _(-401), _(-2063), _(-4788), _(-7134), _(-35640), _(-74992), _(39336), _(-5959), _(5517), _(-2000), _( 519), _(-218), _( 31), _( 1) }
45};
46
47#undef  _
48
49/* F U N K T I O N E N */
50void
51MPC_decoder::Reset_V ( void )
52{
53    memset ( V_L, 0, sizeof V_L );
54    memset ( V_R, 0, sizeof V_R );
55}
56
57void
58MPC_decoder::Calculate_New_V ( const float* Sample, float* V )
59{
60    // Calculating new V-buffer values for left channel
61    // calculate new V-values (ISO-11172-3, p. 39)
62    // based upon fast-MDCT algorithm by Byeong Gi Lee
63    /*static*/ float  A00, A01, A02, A03, A04, A05, A06, A07, A08, A09, A10, A11, A12, A13, A14, A15;
64    /*static*/ float  B00, B01, B02, B03, B04, B05, B06, B07, B08, B09, B10, B11, B12, B13, B14, B15;
65    float         tmp;
66
67    A00 = Sample[ 0] + Sample[31];
68    A01 = Sample[ 1] + Sample[30];
69    A02 = Sample[ 2] + Sample[29];
70    A03 = Sample[ 3] + Sample[28];
71    A04 = Sample[ 4] + Sample[27];
72    A05 = Sample[ 5] + Sample[26];
73    A06 = Sample[ 6] + Sample[25];
74    A07 = Sample[ 7] + Sample[24];
75    A08 = Sample[ 8] + Sample[23];
76    A09 = Sample[ 9] + Sample[22];
77    A10 = Sample[10] + Sample[21];
78    A11 = Sample[11] + Sample[20];
79    A12 = Sample[12] + Sample[19];
80    A13 = Sample[13] + Sample[18];
81    A14 = Sample[14] + Sample[17];
82    A15 = Sample[15] + Sample[16];
83
84    B00 =  A00 + A15;
85    B01 =  A01 + A14;
86    B02 =  A02 + A13;
87    B03 =  A03 + A12;
88    B04 =  A04 + A11;
89    B05 =  A05 + A10;
90    B06 =  A06 + A09;
91    B07 =  A07 + A08;
92    B08 = (A00 - A15) * 0.5024192929f;
93    B09 = (A01 - A14) * 0.5224986076f;
94    B10 = (A02 - A13) * 0.5669440627f;
95    B11 = (A03 - A12) * 0.6468217969f;
96    B12 = (A04 - A11) * 0.7881546021f;
97    B13 = (A05 - A10) * 1.0606776476f;
98    B14 = (A06 - A09) * 1.7224471569f;
99    B15 = (A07 - A08) * 5.1011486053f;
100
101    A00 =  B00 + B07;
102    A01 =  B01 + B06;
103    A02 =  B02 + B05;
104    A03 =  B03 + B04;
105    A04 = (B00 - B07) * 0.5097956061f;
106    A05 = (B01 - B06) * 0.6013448834f;
107    A06 = (B02 - B05) * 0.8999761939f;
108    A07 = (B03 - B04) * 2.5629155636f;
109    A08 =  B08 + B15;
110    A09 =  B09 + B14;
111    A10 =  B10 + B13;
112    A11 =  B11 + B12;
113    A12 = (B08 - B15) * 0.5097956061f;
114    A13 = (B09 - B14) * 0.6013448834f;
115    A14 = (B10 - B13) * 0.8999761939f;
116    A15 = (B11 - B12) * 2.5629155636f;
117
118    B00 =  A00 + A03;
119    B01 =  A01 + A02;
120    B02 = (A00 - A03) * 0.5411961079f;
121    B03 = (A01 - A02) * 1.3065630198f;
122    B04 =  A04 + A07;
123    B05 =  A05 + A06;
124    B06 = (A04 - A07) * 0.5411961079f;
125    B07 = (A05 - A06) * 1.3065630198f;
126    B08 =  A08 + A11;
127    B09 =  A09 + A10;
128    B10 = (A08 - A11) * 0.5411961079f;
129    B11 = (A09 - A10) * 1.3065630198f;
130    B12 =  A12 + A15;
131    B13 =  A13 + A14;
132    B14 = (A12 - A15) * 0.5411961079f;
133    B15 = (A13 - A14) * 1.3065630198f;
134
135    A00 =  B00 + B01;
136    A01 = (B00 - B01) * 0.7071067691f;
137    A02 =  B02 + B03;
138    A03 = (B02 - B03) * 0.7071067691f;
139    A04 =  B04 + B05;
140    A05 = (B04 - B05) * 0.7071067691f;
141    A06 =  B06 + B07;
142    A07 = (B06 - B07) * 0.7071067691f;
143    A08 =  B08 + B09;
144    A09 = (B08 - B09) * 0.7071067691f;
145    A10 =  B10 + B11;
146    A11 = (B10 - B11) * 0.7071067691f;
147    A12 =  B12 + B13;
148    A13 = (B12 - B13) * 0.7071067691f;
149    A14 =  B14 + B15;
150    A15 = (B14 - B15) * 0.7071067691f;
151
152    V[48] = -A00;
153    V[ 0] =  A01;
154    V[40] = -A02 - (V[ 8] = A03);
155    V[36] = -((V[ 4] = A05 + (V[12] = A07)) + A06);
156    V[44] = - A04 - A06 - A07;
157    V[ 6] = (V[10] = A11 + (V[14] = A15)) + A13;
158    V[38] = (V[34] = -(V[ 2] = A09 + A13 + A15) - A14) + A09 - A10 - A11;
159    V[46] = (tmp = -(A12 + A14 + A15)) - A08;
160    V[42] = tmp - A10 - A11;
161
162    A00 = (Sample[ 0] - Sample[31]) * 0.5006030202f;
163    A01 = (Sample[ 1] - Sample[30]) * 0.5054709315f;
164    A02 = (Sample[ 2] - Sample[29]) * 0.5154473186f;
165    A03 = (Sample[ 3] - Sample[28]) * 0.5310425758f;
166    A04 = (Sample[ 4] - Sample[27]) * 0.5531039238f;
167    A05 = (Sample[ 5] - Sample[26]) * 0.5829349756f;
168    A06 = (Sample[ 6] - Sample[25]) * 0.6225041151f;
169    A07 = (Sample[ 7] - Sample[24]) * 0.6748083234f;
170    A08 = (Sample[ 8] - Sample[23]) * 0.7445362806f;
171    A09 = (Sample[ 9] - Sample[22]) * 0.8393496275f;
172    A10 = (Sample[10] - Sample[21]) * 0.9725682139f;
173    A11 = (Sample[11] - Sample[20]) * 1.1694399118f;
174    A12 = (Sample[12] - Sample[19]) * 1.4841645956f;
175    A13 = (Sample[13] - Sample[18]) * 2.0577809811f;
176    A14 = (Sample[14] - Sample[17]) * 3.4076085091f;
177    A15 = (Sample[15] - Sample[16]) *10.1900081635f;
178
179    B00 =  A00 + A15;
180    B01 =  A01 + A14;
181    B02 =  A02 + A13;
182    B03 =  A03 + A12;
183    B04 =  A04 + A11;
184    B05 =  A05 + A10;
185    B06 =  A06 + A09;
186    B07 =  A07 + A08;
187    B08 = (A00 - A15) * 0.5024192929f;
188    B09 = (A01 - A14) * 0.5224986076f;
189    B10 = (A02 - A13) * 0.5669440627f;
190    B11 = (A03 - A12) * 0.6468217969f;
191    B12 = (A04 - A11) * 0.7881546021f;
192    B13 = (A05 - A10) * 1.0606776476f;
193    B14 = (A06 - A09) * 1.7224471569f;
194    B15 = (A07 - A08) * 5.1011486053f;
195
196    A00 =  B00 + B07;
197    A01 =  B01 + B06;
198    A02 =  B02 + B05;
199    A03 =  B03 + B04;
200    A04 = (B00 - B07) * 0.5097956061f;
201    A05 = (B01 - B06) * 0.6013448834f;
202    A06 = (B02 - B05) * 0.8999761939f;
203    A07 = (B03 - B04) * 2.5629155636f;
204    A08 =  B08 + B15;
205    A09 =  B09 + B14;
206    A10 =  B10 + B13;
207    A11 =  B11 + B12;
208    A12 = (B08 - B15) * 0.5097956061f;
209    A13 = (B09 - B14) * 0.6013448834f;
210    A14 = (B10 - B13) * 0.8999761939f;
211    A15 = (B11 - B12) * 2.5629155636f;
212
213    B00 =  A00 + A03;
214    B01 =  A01 + A02;
215    B02 = (A00 - A03) * 0.5411961079f;
216    B03 = (A01 - A02) * 1.3065630198f;
217    B04 =  A04 + A07;
218    B05 =  A05 + A06;
219    B06 = (A04 - A07) * 0.5411961079f;
220    B07 = (A05 - A06) * 1.3065630198f;
221    B08 =  A08 + A11;
222    B09 =  A09 + A10;
223    B10 = (A08 - A11) * 0.5411961079f;
224    B11 = (A09 - A10) * 1.3065630198f;
225    B12 =  A12 + A15;
226    B13 =  A13 + A14;
227    B14 = (A12 - A15) * 0.5411961079f;
228    B15 = (A13 - A14) * 1.3065630198f;
229
230    A00 =  B00 + B01;
231    A01 = (B00 - B01) * 0.7071067691f;
232    A02 =  B02 + B03;
233    A03 = (B02 - B03) * 0.7071067691f;
234    A04 =  B04 + B05;
235    A05 = (B04 - B05) * 0.7071067691f;
236    A06 =  B06 + B07;
237    A07 = (B06 - B07) * 0.7071067691f;
238    A08 =  B08 + B09;
239    A09 = (B08 - B09) * 0.7071067691f;
240    A10 =  B10 + B11;
241    A11 = (B10 - B11) * 0.7071067691f;
242    A12 =  B12 + B13;
243    A13 = (B12 - B13) * 0.7071067691f;
244    A14 =  B14 + B15;
245    A15 = (B14 - B15) * 0.7071067691f;
246
247    // mehrfach verwendete Ausdr�cke: A04+A06+A07, A09+A13+A15
248    V[ 5] = (V[11] = (V[13] = A07 + (V[15] = A15)) + A11) + A05 + A13;
249    V[ 7] = (V[ 9] = A03 + A11 + A15) + A13;
250    V[33] = -(V[ 1] = A01 + A09 + A13 + A15) - A14;
251    V[35] = -(V[ 3] = A05 + A07 + A09 + A13 + A15) - A06 - A14;
252    V[37] = (tmp = -(A10 + A11 + A13 + A14 + A15)) - A05 - A06 - A07;
253    V[39] = tmp - A02 - A03;                      // abh�ngig vom Befehl dr�ber
254    V[41] = (tmp += A13 - A12) - A02 - A03;       // abh�ngig vom Befehl 2 dr�ber
255    V[43] = tmp - A04 - A06 - A07;                // abh�ngig von Befehlen 1 und 3 dr�ber
256    V[47] = (tmp = -(A08 + A12 + A14 + A15)) - A00;
257    V[45] = tmp - A04 - A06 - A07;                // abh�ngig vom Befehl dr�ber
258
259    V[32] = -V[ 0];
260    V[31] = -V[ 1];
261    V[30] = -V[ 2];
262    V[29] = -V[ 3];
263    V[28] = -V[ 4];
264    V[27] = -V[ 5];
265    V[26] = -V[ 6];
266    V[25] = -V[ 7];
267    V[24] = -V[ 8];
268    V[23] = -V[ 9];
269    V[22] = -V[10];
270    V[21] = -V[11];
271    V[20] = -V[12];
272    V[19] = -V[13];
273    V[18] = -V[14];
274    V[17] = -V[15];
275
276    V[63] =  V[33];
277    V[62] =  V[34];
278    V[61] =  V[35];
279    V[60] =  V[36];
280    V[59] =  V[37];
281    V[58] =  V[38];
282    V[57] =  V[39];
283    V[56] =  V[40];
284    V[55] =  V[41];
285    V[54] =  V[42];
286    V[53] =  V[43];
287    V[52] =  V[44];
288    V[51] =  V[45];
289    V[50] =  V[46];
290    V[49] =  V[47];
291}
292
293void
294MPC_decoder::Vectoring_float ( float* Data, const float* V )
295{
296    const float*  D = (float*) Di_opt;
297    double        Sum;
298    int           k;
299    //int           tmp;
300
301    for ( k = 0; k < 32; k++, D += 16, V++ ) {
302        Sum = ( V[  0]*D[ 0] + V[ 96]*D[ 1] + V[128]*D[ 2] + V[224]*D[ 3]
303              + V[256]*D[ 4] + V[352]*D[ 5] + V[384]*D[ 6] + V[480]*D[ 7]
304              + V[512]*D[ 8] + V[608]*D[ 9] + V[640]*D[10] + V[736]*D[11]
305              + V[768]*D[12] + V[864]*D[13] + V[896]*D[14] + V[992]*D[15] ); // + 0xFF8000;
306        //ftol ( Sum, *Data );    // copy to PCM (ftol mit S�ttigung, +0xFF8000 geh�rt bereits dazu)
307        *Data = (float)(Sum * (1.0/32768.0));
308        Data += 2;
309    }
310}
311
312void
313MPC_decoder::Synthese_Filter_float ( float* OutData )
314{
315    int           n;
316    float*        V;
317    const float*  Y;
318
319    /********* left channel ********/
320    memmove ( V_L + V_MEM, V_L, 960 * sizeof(float) );
321    V = V_L + V_MEM;
322    Y = Y_L [0];
323
324    for ( n = 0; n < 36; n++, OutData += 64, Y += 32 ) {
325        V -= 64;
326        Calculate_New_V ( Y, V );
327        Vectoring_float ( OutData, V );
328    }
329    // set pointer to OutData[1] for filling right channel
330    OutData -= 2*1152 - 1;
331
332    /******** right channel ********/
333    memmove ( V_R + V_MEM, V_R, 960 * sizeof(float) );
334    V = V_R + V_MEM;
335    Y = Y_R [0];
336
337    for ( n = 0; n < 36; n++, OutData += 64, Y += 32 ) {
338        V -= 64;
339        Calculate_New_V ( Y, V );
340        Vectoring_float ( OutData, V );
341    }
342}
343
344void
345MPC_decoder::Vectoring ( short* Data, const float* V )
346{
347    const float*  D = (float*) Di_opt;
348    float         Sum;
349    int           k;
350    int           tmp;
351
352    for ( k = 0; k < 32; k++, D += 16, V++ ) {
353        Sum = V[  0]*D[ 0] + V[ 96]*D[ 1] + V[128]*D[ 2] + V[224]*D[ 3]
354            + V[256]*D[ 4] + V[352]*D[ 5] + V[384]*D[ 6] + V[480]*D[ 7]
355            + V[512]*D[ 8] + V[608]*D[ 9] + V[640]*D[10] + V[736]*D[11]
356            + V[768]*D[12] + V[864]*D[13] + V[896]*D[14] + V[992]*D[15] + 0xFF8000;
357        ftol ( Sum, *Data );    // copy to PCM (ftol mit S�ttigung, +0xFF8000 geh�rt bereits dazu)
358        Data += 2;
359    }
360}
361
362void
363MPC_decoder::Synthese_Filter_opt ( short* OutData )
364{
365    int           n;
366    float*        V;
367    const float*  Y;
368
369    /********* left channel ********/
370    memmove ( V_L + V_MEM, V_L, 960 * sizeof(float) );
371    V = V_L + V_MEM;
372    Y = Y_L [0];
373
374    for ( n = 0; n < 36; n++, OutData += 64, Y += 32 ) {
375        V -= 64;
376        Calculate_New_V ( Y, V );
377        Vectoring       ( OutData, V );
378    }
379    // set pointer to OutData[1] for filling right channel
380    OutData -= 2*1152 - 1;
381
382    /******** right channel ********/
383    memmove ( V_R + V_MEM, V_R, 960 * sizeof(float) );
384    V = V_R + V_MEM;
385    Y = Y_R [0];
386
387    for ( n = 0; n < 36; n++, OutData += 64, Y += 32 ) {
388        V -= 64;
389        Calculate_New_V ( Y, V );
390        Vectoring       ( OutData, V );
391    }
392}
393
394/*******************************************/
395/*                                         */
396/*            dithered synthesis           */
397/*                                         */
398/*******************************************/
399
400static const unsigned char    Parity [256] = {  // parity
401    0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
402    1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
403    1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
404    0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
405    1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
406    0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
407    0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
408    1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
409};
410
411/*
412 *  This is a simple random number generator with good quality for audio purposes.
413 *  It consists of two polycounters with opposite rotation direction and different
414 *  periods. The periods are coprime, so the total period is the product of both.
415 *
416 *     -------------------------------------------------------------------------------------------------
417 * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0|
418 * |   -------------------------------------------------------------------------------------------------
419 * |                                                                          |  |  |  |     |        |
420 * |                                                                          +--+--+--+-XOR-+--------+
421 * |                                                                                      |
422 * +--------------------------------------------------------------------------------------+
423 *
424 *     -------------------------------------------------------------------------------------------------
425 *     |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+
426 *     -------------------------------------------------------------------------------------------------   |
427 *       |  |           |  |                                                                               |
428 *       +--+----XOR----+--+                                                                               |
429 *                |                                                                                        |
430 *                +----------------------------------------------------------------------------------------+
431 *
432 *
433 *  The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
434 *  which gives a period of 18.410.713.077.675.721.215. The result is the
435 *  XORed values of both generators.
436 */
437
438unsigned int
439MPC_decoder::random_int ( void )
440{
441#if 1
442    unsigned int  t1, t2, t3, t4;
443
444    t3   = t1 = __r1;   t4   = t2 = __r2;       // Parity calculation is done via table lookup, this is also available
445    t1  &= 0xF5;        t2 >>= 25;              // on CPUs without parity, can be implemented in C and avoid unpredictable
446    t1   = Parity [t1]; t2  &= 0x63;            // jumps and slow rotate through the carry flag operations.
447    t1 <<= 31;          t2   = Parity [t2];
448
449    return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
450#else
451    return (__r1 = (__r1 >> 1) | ((unsigned int)Parity [__r1 & 0xF5] << 31) ) ^
452           (__r2 = (__r2 << 1) |  (unsigned int)Parity [(__r2 >> 25) & 0x63] );
453#endif
454}
455
456// generates triangular dither
457void
458MPC_decoder::GenerateDither_old ( float* p, size_t len )
459{
460    while (len--)
461        *p++ = ( (float)(int)random_int() + (float)(int)random_int() ) * (1.f/65536.f/65536.f);
462}
463
464void
465MPC_decoder::GenerateDither ( float* p, size_t len )
466{
467    /*static*/ int  last = 0;
468    int         tmp;
469
470    while (len--) {
471        tmp  = (int) random_int();
472        *p++ = ((float)tmp - last) * (1.f/65536.f/65536.f);
473        last = tmp;
474    }
475}
476
477// dithered vectoring
478void
479MPC_decoder::Vectoring_dithered ( short* Data, const float* V, const float* dither )
480{
481    const float*  D = (float*) Di_opt;
482    float         Sum;
483    int           k;
484    int           tmp;
485
486    for ( k = 0; k < 32; k++, D += 16, V++ ) {
487        Sum = V[  0]*D[ 0] + V[ 96]*D[ 1] + V[128]*D[ 2] + V[224]*D[ 3]
488            + V[256]*D[ 4] + V[352]*D[ 5] + V[384]*D[ 6] + V[480]*D[ 7]
489            + V[512]*D[ 8] + V[608]*D[ 9] + V[640]*D[10] + V[736]*D[11]
490            + V[768]*D[12] + V[864]*D[13] + V[896]*D[14] + V[992]*D[15] + *dither++ + 0xFF8000 ;
491        ftol ( Sum, *Data );    // copy to PCM (ftol mit S�ttigung, +0xFF8000 geh�rt bereits dazu)
492        Data += 2;
493    }
494}
495
496// dithered synthesis
497void
498MPC_decoder::Synthese_Filter_dithered ( short* OutData )
499{
500    int           n;
501    float*        V;
502    float         DitherData [36 * 32];
503    const float*  Y;
504
505    GenerateDither ( DitherData, 32*36 );
506    /********* left channel ********/
507    memmove ( V_L + V_MEM, V_L, 960 * sizeof(float) );
508    V = V_L + V_MEM;
509    Y = Y_L [0];
510
511    for ( n = 0; n < 36; n++, OutData += 64, Y += 32 ) {
512        V -= 64;
513        Calculate_New_V    ( Y, V );
514        Vectoring_dithered ( OutData, V, DitherData + 32*n );
515    }
516    // set pointer to OutData[1] for filling right channel
517    OutData    -= 2*1152 - 1;
518
519    /******** right channel ********/
520    memmove ( V_R + V_MEM, V_R, 960 * sizeof(float) );
521    V = V_R + V_MEM;
522    Y = Y_R [0];
523
524    for ( n = 0; n < 36; n++, OutData += 64, Y += 32 ) {
525        V -= 64;
526        Calculate_New_V    ( Y, V );
527        Vectoring_dithered ( OutData, V, DitherData + 32*n );
528    }
529}
530
531/* end of synth_filter.c */
532