1 #include "common.h"
2 #include "encoder.h"
3 #include "lfe.h"
4
5 static double a[17] = {
6 0.750000000, 0.625000000, 0.875000000, 0.562500000, 0.937500000,
7 0.968750000, 0.984375000, 0.992187500, 0.996093750, 0.998046875,
8 0.999023438, 0.999511719, 0.999755859, 0.999877930, 0.999938965,
9 0.999969482, 0.999984741
10 };
11
12 static double b[17] = {
13 -0.250000000, -0.375000000, -0.125000000, -0.437500000, -0.062500000,
14 -0.031250000, -0.015625000, -0.007812500, -0.003906250, -0.001953125,
15 -0.000976563, -0.000488281, -0.000244141, -0.000122070, -0.000061035,
16 -0.000030518, -0.000015259
17 };
18
19 /************************************************************************
20 *
21 * encode_lfe
22 *
23 ************************************************************************/
24
lf_scalefactor(double lfe_sample[12],unsigned int * lfe_scalar)25 void lf_scalefactor (double lfe_sample[12], unsigned int *lfe_scalar)
26 {
27 int j;
28 double s;
29
30 for (j = 1, s = fabs (lfe_sample[0]); j < 12; j++)
31 if (fabs (lfe_sample[j]) > s)
32 s = fabs (lfe_sample[j]);
33
34 for (j = SCALE_RANGE - 1, *lfe_scalar = 0; j >= 0; j--)
35 if (s <= multiple[j]) {
36 *lfe_scalar = j;
37 break;
38 }
39
40 }
41
42
II_encode_lfe_scale(unsigned int lfe_scalar,Bit_stream_struc * bs)43 void II_encode_lfe_scale (unsigned int lfe_scalar, Bit_stream_struc * bs)
44 {
45 putbits (bs, lfe_scalar, 6);
46 }
47
48
lfe_allocation(unsigned int * lfe_alloc,int * adb)49 void lfe_allocation (unsigned int *lfe_alloc, int *adb)
50 { /* 70 */
51 int nbal, bspl, bscf;
52
53 /* *lfe_alloc = 4 index (Lay II bit alloc tabel) */
54 nbal = 4; /* 4 bits (nbal) */
55 bscf = 6; /* bits for scale factor */
56 bspl = 12 * 5; /* bits used for quantization */
57 *adb -= nbal + bscf + bspl;
58 }
59
60
II_lfe_quantisation(unsigned int lfe_scalar,double lfe_samples[12],unsigned int lfe_alloc,unsigned int sbband[12],frame_params * fr_ps)61 void II_lfe_quantisation (unsigned int lfe_scalar,
62 double lfe_samples[12],
63 unsigned int lfe_alloc,
64 unsigned int sbband[12], frame_params * fr_ps)
65 {
66 int s, qnt, n, sig;
67 unsigned int stps;
68 double d;
69 al_table *alloc = fr_ps->alloc;
70 for (s = 0; s < 12; s++) {
71 d = lfe_samples[s] / multiple[lfe_scalar];
72 if (fabs (d) > 1.0)
73 printf ("In LFE, not scaled properly %d %12.10e\n", s, d);
74 qnt = (*alloc)[0][lfe_alloc].quant;
75 d = d * a[qnt] + b[qnt];
76 /* extract MSB N-1 bits from the floating point sample */
77 if (d >= 0)
78 sig = 1;
79 else {
80 sig = 0;
81 d += 1.0;
82 }
83 n = 0;
84 #ifndef MS_DOS
85 stps = (*alloc)[0][lfe_alloc].steps;
86 while ((1L << n) < stps)
87 n++;
88 #else
89 while (((unsigned long) (1L << (long) n) <
90 ((unsigned long) ((*alloc)[i][bit_alloc[k][i]].steps)
91 & 0xffff)
92 ) && (n < 16)
93 )
94 n++;
95 #endif
96 n--;
97 sbband[s] = (unsigned int) (d * (double) (1L << n));
98 /* tag the inverted sign bit to sbband at position N */
99 /* The bit inversion is a must for grouping with 3,5,9 steps
100 so it is done for all subbands */
101 if (sig)
102 sbband[s] |= 1 << n;
103 }
104 }
105
106
107
II_encode_lfe_alloc(unsigned int lfe_alloc,frame_params * fr_ps,Bit_stream_struc * bs)108 void II_encode_lfe_alloc (unsigned int lfe_alloc,
109 frame_params * fr_ps, Bit_stream_struc * bs)
110 {
111 al_table *alloc = fr_ps->alloc;
112 putbits (bs, lfe_alloc, (*alloc)[0][0].bits);
113 }
114
115
II_lfe_sample_encoding(unsigned int lfe_sbband[12],unsigned int lfe_alloc,frame_params * fr_ps,Bit_stream_struc * bs)116 void II_lfe_sample_encoding (unsigned int lfe_sbband[12],
117 unsigned int lfe_alloc,
118 frame_params * fr_ps, Bit_stream_struc * bs)
119 {
120 unsigned int s;
121 al_table *alloc = fr_ps->alloc;
122
123 for (s = 0; s < 12; s++)
124 if ((*alloc)[0][lfe_alloc].group == 3) {
125 putbits (bs, lfe_sbband[s], (*alloc)[0][lfe_alloc].bits);
126 }
127 }
128
129
130 /***********************************************************************
131 *
132 *
133 * filter()
134 *
135 * A lf-filter to produce the LFE signal
136 *
137 **********************************************************************/
138
lfe_filter(double ** buffer,double lfe_sample[12])139 void lfe_filter (double **buffer, double lfe_sample[12])
140 {
141 static double c;
142 static double d[8];
143 static double w1[8], w2[8], v0[8], v1[8], v2[8];
144 static double a0[8], a1[8], a2[8], b0[8], b1[8], b2[8];
145 static double A0[8], B0[8], B1[8], B2[8];
146 static int fa, fg;
147 static int init = 0;
148 int i, j, k;
149 double sambuf[1152];
150
151 if (init == 0) {
152 for (i = 0; i < 8; i++)
153 w1[i] = w2[i] = v0[i] = v1[i] = v2[i] = 0.0;
154
155
156
157 fg = 125; /* cutoff-frequency */
158 fa = 48000; /* sampling-frequency */
159
160 /* filter coefficients */
161
162 for (i = 0; i < 8; i++) {
163 A0[i] = B0[i] = B2[i] = 1.0;
164 B1[i] = 2 * cos ((2 * i + 1) * PI / (4 * 8));
165 }
166
167 c = 1 / tan (PI * fg / fa);
168
169 for (i = 0; i < 8; i++) {
170 d[i] = (B0[i] + (B1[i] * c) + (B2[i] * c * c));
171 a0[i] = A0[i] / d[i];
172 a1[i] = 2 * A0[i] / d[i];
173 a2[i] = A0[i] / d[i];
174 b0[i] = 1;
175 b1[i] = 2 * (B0[i] - (B2[i] * c * c)) / d[i];
176 b2[i] = (B0[i] - (B1[i] * c) + (B2[i] * c * c)) / d[i];
177 }
178 init = 1;
179 }
180 j = 0;
181 for (i = 0; i < 1152; i++) {
182 sambuf[i] = **buffer;
183 for (k = 0; k < 8; k++) {
184 v0[k] = sambuf[i];
185 sambuf[i] =
186 v0[k] * a0[k] + v1[k] * a1[k] + v2[k] * a2[k] - w1[k] * b1[k] -
187 w2[k] * b2[k];
188 v2[k] = v1[k];
189 v1[k] = v0[k];
190 w2[k] = w1[k];
191 w1[k] = sambuf[i];
192 }
193 (*buffer)++;
194 }
195 i = 0;
196 for (j = 0; j < 12; j++) {
197 lfe_sample[j] = sambuf[i] / SCALE;
198 i += 96;
199 }
200 }
201