10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
12527
|
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
10725
|
4 **
|
|
5 ** This program is free software; you can redistribute it and/or modify
|
|
6 ** it under the terms of the GNU General Public License as published by
|
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
|
8 ** (at your option) any later version.
|
|
9 **
|
|
10 ** This program is distributed in the hope that it will be useful,
|
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 ** GNU General Public License for more details.
|
|
14 **
|
|
15 ** You should have received a copy of the GNU General Public License
|
|
16 ** along with this program; if not, write to the Free Software
|
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
12527
|
25 ** $Id: sbr_fbt.c,v 1.2 2003/10/03 22:22:27 alex Exp $
|
10725
|
26 **/
|
|
27
|
|
28 /* Calculate frequency band tables */
|
|
29
|
|
30 #include "common.h"
|
|
31 #include "structs.h"
|
|
32
|
|
33 #ifdef SBR_DEC
|
|
34
|
|
35 #include <stdlib.h>
|
|
36
|
|
37 #include "sbr_syntax.h"
|
|
38 #include "sbr_fbt.h"
|
|
39
|
12527
|
40 /* static function declarations */
|
|
41 static int32_t find_bands(uint8_t warp, uint8_t bands, uint8_t a0, uint8_t a1);
|
|
42
|
|
43
|
10725
|
44 /* calculate the start QMF channel for the master frequency band table */
|
|
45 /* parameter is also called k0 */
|
10989
|
46 uint8_t qmf_start_channel(uint8_t bs_start_freq, uint8_t bs_samplerate_mode,
|
10725
|
47 uint32_t sample_rate)
|
|
48 {
|
10989
|
49 static const uint8_t startMinTable[12] = { 7, 7, 10, 11, 12, 16, 16,
|
|
50 17, 24, 32, 35, 48 };
|
|
51 static const uint8_t offsetIndexTable[12] = { 5, 5, 4, 4, 4, 3, 2, 1, 0,
|
|
52 6, 6, 6 };
|
|
53 static const int8_t offset[7][16] = {
|
10725
|
54 { -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 },
|
|
55 { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13 },
|
|
56 { -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 },
|
|
57 { -6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 },
|
|
58 { -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20 },
|
|
59 { -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24 },
|
|
60 { 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24, 28, 33 }
|
|
61 };
|
10989
|
62 uint8_t startMin = startMinTable[get_sr_index(sample_rate)];
|
|
63 uint8_t offsetIndex = offsetIndexTable[get_sr_index(sample_rate)];
|
10725
|
64
|
10989
|
65 #if 0 /* replaced with table (startMinTable) */
|
10725
|
66 if (sample_rate >= 64000)
|
|
67 {
|
|
68 startMin = (uint8_t)((5000.*128.)/(float)sample_rate + 0.5);
|
|
69 } else if (sample_rate < 32000) {
|
|
70 startMin = (uint8_t)((3000.*128.)/(float)sample_rate + 0.5);
|
|
71 } else {
|
|
72 startMin = (uint8_t)((4000.*128.)/(float)sample_rate + 0.5);
|
|
73 }
|
10989
|
74 #endif
|
10725
|
75
|
|
76 if (bs_samplerate_mode)
|
|
77 {
|
10989
|
78 return startMin + offset[offsetIndex][bs_start_freq];
|
|
79
|
|
80 #if 0 /* replaced by offsetIndexTable */
|
10725
|
81 switch (sample_rate)
|
|
82 {
|
|
83 case 16000:
|
|
84 return startMin + offset[0][bs_start_freq];
|
|
85 case 22050:
|
|
86 return startMin + offset[1][bs_start_freq];
|
|
87 case 24000:
|
|
88 return startMin + offset[2][bs_start_freq];
|
|
89 case 32000:
|
|
90 return startMin + offset[3][bs_start_freq];
|
|
91 default:
|
|
92 if (sample_rate > 64000)
|
|
93 {
|
|
94 return startMin + offset[5][bs_start_freq];
|
|
95 } else { /* 44100 <= sample_rate <= 64000 */
|
|
96 return startMin + offset[4][bs_start_freq];
|
|
97 }
|
|
98 }
|
10989
|
99 #endif
|
10725
|
100 } else {
|
|
101 return startMin + offset[6][bs_start_freq];
|
|
102 }
|
|
103 }
|
|
104
|
12527
|
105 static int longcmp(const void *a, const void *b)
|
10725
|
106 {
|
12527
|
107 return ((int)(*(int32_t*)a - *(int32_t*)b));
|
10725
|
108 }
|
|
109
|
|
110 /* calculate the stop QMF channel for the master frequency band table */
|
|
111 /* parameter is also called k2 */
|
10989
|
112 uint8_t qmf_stop_channel(uint8_t bs_stop_freq, uint32_t sample_rate,
|
|
113 uint8_t k0)
|
10725
|
114 {
|
|
115 if (bs_stop_freq == 15)
|
|
116 {
|
|
117 return min(64, k0 * 3);
|
|
118 } else if (bs_stop_freq == 14) {
|
|
119 return min(64, k0 * 2);
|
|
120 } else {
|
10989
|
121 static const uint8_t stopMinTable[12] = { 13, 15, 20, 21, 23,
|
|
122 32, 32, 35, 48, 64, 70, 96 };
|
|
123 static const int8_t offset[12][14] = {
|
|
124 { 0, 2, 4, 6, 8, 11, 14, 18, 22, 26, 31, 37, 44, 51 },
|
|
125 { 0, 2, 4, 6, 8, 11, 14, 18, 22, 26, 31, 36, 42, 49 },
|
|
126 { 0, 2, 4, 6, 8, 11, 14, 17, 21, 25, 29, 34, 39, 44 },
|
|
127 { 0, 2, 4, 6, 8, 11, 14, 17, 20, 24, 28, 33, 38, 43 },
|
|
128 { 0, 2, 4, 6, 8, 11, 14, 17, 20, 24, 28, 32, 36, 41 },
|
|
129 { 0, 2, 4, 6, 8, 10, 12, 14, 17, 20, 23, 26, 29, 32 },
|
|
130 { 0, 2, 4, 6, 8, 10, 12, 14, 17, 20, 23, 26, 29, 32 },
|
|
131 { 0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 20, 23, 26, 29 },
|
|
132 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16 },
|
|
133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
|
|
134 { 0, -1, -2, -3, -4, -5, -6, -6, -6, -6, -6, -6, -6, -6 },
|
|
135 { 0, -3, -6, -9, -12, -15, -18, -20, -22, -24, -26, -28, -30, -32 }
|
|
136 };
|
|
137 #if 0
|
10725
|
138 uint8_t i;
|
|
139 int32_t stopDk[13], stopDk_t[14], k2;
|
10989
|
140 #endif
|
|
141 uint8_t stopMin = stopMinTable[get_sr_index(sample_rate)];
|
10725
|
142
|
10989
|
143 #if 0 /* replaced by table lookup */
|
10725
|
144 if (sample_rate >= 64000)
|
|
145 {
|
|
146 stopMin = (uint8_t)((10000.*128.)/(float)sample_rate + 0.5);
|
|
147 } else if (sample_rate < 32000) {
|
|
148 stopMin = (uint8_t)((6000.*128.)/(float)sample_rate + 0.5);
|
|
149 } else {
|
|
150 stopMin = (uint8_t)((8000.*128.)/(float)sample_rate + 0.5);
|
|
151 }
|
10989
|
152 #endif
|
10725
|
153
|
10989
|
154 #if 0 /* replaced by table lookup */
|
|
155 /* diverging power series */
|
10725
|
156 for (i = 0; i <= 13; i++)
|
|
157 {
|
|
158 stopDk_t[i] = (int32_t)(stopMin*pow(64.0/stopMin, i/13.0) + 0.5);
|
|
159 }
|
|
160 for (i = 0; i < 13; i++)
|
|
161 {
|
|
162 stopDk[i] = stopDk_t[i+1] - stopDk_t[i];
|
|
163 }
|
|
164
|
10989
|
165 /* needed? */
|
10725
|
166 qsort(stopDk, 13, sizeof(stopDk[0]), longcmp);
|
|
167
|
|
168 k2 = stopMin;
|
|
169 for (i = 0; i < bs_stop_freq; i++)
|
|
170 {
|
|
171 k2 += stopDk[i];
|
|
172 }
|
|
173 return min(64, k2);
|
10989
|
174 #endif
|
|
175 /* bs_stop_freq <= 13 */
|
|
176 return min(64, stopMin + offset[get_sr_index(sample_rate)][min(bs_stop_freq, 13)]);
|
10725
|
177 }
|
|
178
|
|
179 return 0;
|
|
180 }
|
|
181
|
|
182 /* calculate the master frequency table from k0, k2, bs_freq_scale
|
|
183 and bs_alter_scale
|
|
184
|
|
185 version for bs_freq_scale = 0
|
|
186 */
|
12527
|
187 uint8_t master_frequency_table_fs0(sbr_info *sbr, uint8_t k0, uint8_t k2,
|
|
188 uint8_t bs_alter_scale)
|
10725
|
189 {
|
|
190 int8_t incr;
|
|
191 uint8_t k;
|
|
192 uint8_t dk;
|
|
193 uint32_t nrBands, k2Achieved;
|
12527
|
194 int32_t k2Diff, vDk[64] = {0};
|
10725
|
195
|
|
196 /* mft only defined for k2 > k0 */
|
|
197 if (k2 <= k0)
|
|
198 {
|
|
199 sbr->N_master = 0;
|
12527
|
200 return 0;
|
10725
|
201 }
|
|
202
|
|
203 dk = bs_alter_scale ? 2 : 1;
|
10989
|
204
|
|
205 #if 0 /* replaced by float-less design */
|
10725
|
206 nrBands = 2 * (int32_t)((float)(k2-k0)/(dk*2) + (-1+dk)/2.0f);
|
10989
|
207 #else
|
|
208 if (bs_alter_scale)
|
|
209 {
|
|
210 nrBands = (((k2-k0+2)>>2)<<1);
|
|
211 } else {
|
|
212 nrBands = (((k2-k0)>>1)<<1);
|
|
213 }
|
|
214 #endif
|
|
215 nrBands = min(nrBands, 63);
|
12527
|
216 if (nrBands <= 0)
|
|
217 return 1;
|
10725
|
218
|
|
219 k2Achieved = k0 + nrBands * dk;
|
|
220 k2Diff = k2 - k2Achieved;
|
|
221 for (k = 0; k < nrBands; k++)
|
|
222 vDk[k] = dk;
|
|
223
|
|
224 if (k2Diff)
|
|
225 {
|
|
226 incr = (k2Diff > 0) ? -1 : 1;
|
|
227 k = (k2Diff > 0) ? (nrBands-1) : 0;
|
|
228
|
|
229 while (k2Diff != 0)
|
|
230 {
|
|
231 vDk[k] -= incr;
|
|
232 k += incr;
|
|
233 k2Diff += incr;
|
|
234 }
|
|
235 }
|
|
236
|
|
237 sbr->f_master[0] = k0;
|
|
238 for (k = 1; k <= nrBands; k++)
|
|
239 sbr->f_master[k] = sbr->f_master[k-1] + vDk[k-1];
|
|
240
|
|
241 sbr->N_master = nrBands;
|
|
242 sbr->N_master = min(sbr->N_master, 64);
|
|
243
|
|
244 #if 0
|
|
245 printf("f_master[%d]: ", nrBands);
|
|
246 for (k = 0; k <= nrBands; k++)
|
|
247 {
|
|
248 printf("%d ", sbr->f_master[k]);
|
|
249 }
|
|
250 printf("\n");
|
|
251 #endif
|
12527
|
252
|
|
253 return 0;
|
10725
|
254 }
|
|
255
|
10989
|
256 /*
|
|
257 This function finds the number of bands using this formula:
|
|
258 bands * log(a1/a0)/log(2.0) + 0.5
|
|
259 */
|
|
260 static int32_t find_bands(uint8_t warp, uint8_t bands, uint8_t a0, uint8_t a1)
|
|
261 {
|
12527
|
262 #ifdef FIXED_POINT
|
|
263 /* table with log2() values */
|
|
264 static const real_t log2Table[65] = {
|
|
265 COEF_CONST(0.0), COEF_CONST(0.0), COEF_CONST(1.0000000000), COEF_CONST(1.5849625007),
|
|
266 COEF_CONST(2.0000000000), COEF_CONST(2.3219280949), COEF_CONST(2.5849625007), COEF_CONST(2.8073549221),
|
|
267 COEF_CONST(3.0000000000), COEF_CONST(3.1699250014), COEF_CONST(3.3219280949), COEF_CONST(3.4594316186),
|
|
268 COEF_CONST(3.5849625007), COEF_CONST(3.7004397181), COEF_CONST(3.8073549221), COEF_CONST(3.9068905956),
|
|
269 COEF_CONST(4.0000000000), COEF_CONST(4.0874628413), COEF_CONST(4.1699250014), COEF_CONST(4.2479275134),
|
|
270 COEF_CONST(4.3219280949), COEF_CONST(4.3923174228), COEF_CONST(4.4594316186), COEF_CONST(4.5235619561),
|
|
271 COEF_CONST(4.5849625007), COEF_CONST(4.6438561898), COEF_CONST(4.7004397181), COEF_CONST(4.7548875022),
|
|
272 COEF_CONST(4.8073549221), COEF_CONST(4.8579809951), COEF_CONST(4.9068905956), COEF_CONST(4.9541963104),
|
|
273 COEF_CONST(5.0000000000), COEF_CONST(5.0443941194), COEF_CONST(5.0874628413), COEF_CONST(5.1292830169),
|
|
274 COEF_CONST(5.1699250014), COEF_CONST(5.2094533656), COEF_CONST(5.2479275134), COEF_CONST(5.2854022189),
|
|
275 COEF_CONST(5.3219280949), COEF_CONST(5.3575520046), COEF_CONST(5.3923174228), COEF_CONST(5.4262647547),
|
|
276 COEF_CONST(5.4594316186), COEF_CONST(5.4918530963), COEF_CONST(5.5235619561), COEF_CONST(5.5545888517),
|
|
277 COEF_CONST(5.5849625007), COEF_CONST(5.6147098441), COEF_CONST(5.6438561898), COEF_CONST(5.6724253420),
|
|
278 COEF_CONST(5.7004397181), COEF_CONST(5.7279204546), COEF_CONST(5.7548875022), COEF_CONST(5.7813597135),
|
|
279 COEF_CONST(5.8073549221), COEF_CONST(5.8328900142), COEF_CONST(5.8579809951), COEF_CONST(5.8826430494),
|
|
280 COEF_CONST(5.9068905956), COEF_CONST(5.9307373376), COEF_CONST(5.9541963104), COEF_CONST(5.9772799235),
|
|
281 COEF_CONST(6.0)
|
|
282 };
|
|
283 real_t r0 = log2Table[a0]; /* coef */
|
|
284 real_t r1 = log2Table[a1]; /* coef */
|
|
285 real_t r2 = (r1 - r0); /* coef */
|
|
286
|
|
287 if (warp)
|
|
288 r2 = MUL_C(r2, COEF_CONST(1.0/1.3));
|
|
289
|
|
290 /* convert r2 to real and then multiply and round */
|
|
291 r2 = (r2 >> (COEF_BITS-REAL_BITS)) * bands + (1<<(REAL_BITS-1));
|
|
292
|
|
293 return (r2 >> REAL_BITS);
|
|
294 #else
|
10989
|
295 real_t div = (real_t)log(2.0);
|
|
296 if (warp) div *= (real_t)1.3;
|
|
297
|
|
298 return (int32_t)(bands * log((float)a1/(float)a0)/div + 0.5);
|
12527
|
299 #endif
|
10989
|
300 }
|
|
301
|
12527
|
302 static real_t find_initial_power(uint8_t bands, uint8_t a0, uint8_t a1)
|
|
303 {
|
|
304 #ifdef FIXED_POINT
|
|
305 /* table with log() values */
|
|
306 static const real_t logTable[65] = {
|
|
307 COEF_CONST(0.0), COEF_CONST(0.0), COEF_CONST(0.6931471806), COEF_CONST(1.0986122887),
|
|
308 COEF_CONST(1.3862943611), COEF_CONST(1.6094379124), COEF_CONST(1.7917594692), COEF_CONST(1.9459101491),
|
|
309 COEF_CONST(2.0794415417), COEF_CONST(2.1972245773), COEF_CONST(2.3025850930), COEF_CONST(2.3978952728),
|
|
310 COEF_CONST(2.4849066498), COEF_CONST(2.5649493575), COEF_CONST(2.6390573296), COEF_CONST(2.7080502011),
|
|
311 COEF_CONST(2.7725887222), COEF_CONST(2.8332133441), COEF_CONST(2.8903717579), COEF_CONST(2.9444389792),
|
|
312 COEF_CONST(2.9957322736), COEF_CONST(3.0445224377), COEF_CONST(3.0910424534), COEF_CONST(3.1354942159),
|
|
313 COEF_CONST(3.1780538303), COEF_CONST(3.2188758249), COEF_CONST(3.2580965380), COEF_CONST(3.2958368660),
|
|
314 COEF_CONST(3.3322045102), COEF_CONST(3.3672958300), COEF_CONST(3.4011973817), COEF_CONST(3.4339872045),
|
|
315 COEF_CONST(3.4657359028), COEF_CONST(3.4965075615), COEF_CONST(3.5263605246), COEF_CONST(3.5553480615),
|
|
316 COEF_CONST(3.5835189385), COEF_CONST(3.6109179126), COEF_CONST(3.6375861597), COEF_CONST(3.6635616461),
|
|
317 COEF_CONST(3.6888794541), COEF_CONST(3.7135720667), COEF_CONST(3.7376696183), COEF_CONST(3.7612001157),
|
|
318 COEF_CONST(3.7841896339), COEF_CONST(3.8066624898), COEF_CONST(3.8286413965), COEF_CONST(3.8501476017),
|
|
319 COEF_CONST(3.8712010109), COEF_CONST(3.8918202981), COEF_CONST(3.9120230054), COEF_CONST(3.9318256327),
|
|
320 COEF_CONST(3.9512437186), COEF_CONST(3.9702919136), COEF_CONST(3.9889840466), COEF_CONST(4.0073331852),
|
|
321 COEF_CONST(4.0253516907), COEF_CONST(4.0430512678), COEF_CONST(4.0604430105), COEF_CONST(4.0775374439),
|
|
322 COEF_CONST(4.0943445622), COEF_CONST(4.1108738642), COEF_CONST(4.1271343850), COEF_CONST(4.1431347264),
|
|
323 COEF_CONST(4.158883083)
|
|
324 };
|
|
325 /* standard Taylor polynomial coefficients for exp(x) around 0 */
|
|
326 /* a polynomial around x=1 is more precise, as most values are around 1.07,
|
|
327 but this is just fine already */
|
|
328 static const real_t c1 = COEF_CONST(1.0);
|
|
329 static const real_t c2 = COEF_CONST(1.0/2.0);
|
|
330 static const real_t c3 = COEF_CONST(1.0/6.0);
|
|
331 static const real_t c4 = COEF_CONST(1.0/24.0);
|
|
332
|
|
333 real_t r0 = logTable[a0]; /* coef */
|
|
334 real_t r1 = logTable[a1]; /* coef */
|
|
335 real_t r2 = (r1 - r0) / bands; /* coef */
|
|
336 real_t rexp = c1 + MUL_C((c1 + MUL_C((c2 + MUL_C((c3 + MUL_C(c4,r2)), r2)), r2)), r2);
|
|
337
|
|
338 return (rexp >> (COEF_BITS-REAL_BITS)); /* real */
|
|
339 #else
|
|
340 return (real_t)pow((real_t)a1/(real_t)a0, 1.0/(real_t)bands);
|
|
341 #endif
|
|
342 }
|
10989
|
343
|
10725
|
344 /*
|
|
345 version for bs_freq_scale > 0
|
|
346 */
|
12527
|
347 uint8_t master_frequency_table(sbr_info *sbr, uint8_t k0, uint8_t k2,
|
|
348 uint8_t bs_freq_scale, uint8_t bs_alter_scale)
|
10725
|
349 {
|
|
350 uint8_t k, bands, twoRegions;
|
10989
|
351 uint8_t k1;
|
12527
|
352 uint8_t nrBand0, nrBand1;
|
|
353 int32_t vDk0[64] = {0}, vDk1[64] = {0};
|
|
354 int32_t vk0[64] = {0}, vk1[64] = {0};
|
10989
|
355 uint8_t temp1[] = { 6, 5, 4 };
|
12527
|
356 real_t q, qk;
|
|
357 int32_t A_1;
|
10725
|
358
|
|
359 /* mft only defined for k2 > k0 */
|
|
360 if (k2 <= k0)
|
|
361 {
|
|
362 sbr->N_master = 0;
|
12527
|
363 return 0;
|
10725
|
364 }
|
|
365
|
|
366 bands = temp1[bs_freq_scale-1];
|
|
367
|
12527
|
368 #ifdef FIXED_POINT
|
|
369 if (REAL_CONST(k2) > MUL_R(REAL_CONST(k0),REAL_CONST(2.2449)))
|
|
370 #else
|
10725
|
371 if ((float)k2/(float)k0 > 2.2449)
|
12527
|
372 #endif
|
10725
|
373 {
|
|
374 twoRegions = 1;
|
10989
|
375 k1 = k0 << 1;
|
10725
|
376 } else {
|
|
377 twoRegions = 0;
|
|
378 k1 = k2;
|
|
379 }
|
|
380
|
10989
|
381 nrBand0 = 2 * find_bands(0, bands, k0, k1);
|
|
382 nrBand0 = min(nrBand0, 63);
|
12527
|
383 if (nrBand0 <= 0)
|
|
384 return 1;
|
10989
|
385
|
12527
|
386 q = find_initial_power(nrBand0, k0, k1);
|
|
387 qk = REAL_CONST(k0);
|
|
388 #ifdef FIXED_POINT
|
|
389 A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
|
|
390 #else
|
|
391 A_1 = (int32_t)(qk + .5);
|
|
392 #endif
|
10725
|
393 for (k = 0; k <= nrBand0; k++)
|
|
394 {
|
12527
|
395 int32_t A_0 = A_1;
|
|
396 #ifdef FIXED_POINT
|
|
397 qk = MUL_R(qk,q);
|
|
398 A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
|
|
399 #else
|
|
400 qk *= q;
|
|
401 A_1 = (int32_t)(qk + 0.5);
|
|
402 #endif
|
|
403 vDk0[k] = A_1 - A_0;
|
10725
|
404 }
|
|
405
|
|
406 /* needed? */
|
|
407 qsort(vDk0, nrBand0, sizeof(vDk0[0]), longcmp);
|
|
408
|
|
409 vk0[0] = k0;
|
|
410 for (k = 1; k <= nrBand0; k++)
|
|
411 {
|
|
412 vk0[k] = vk0[k-1] + vDk0[k-1];
|
12527
|
413 if (vDk0[k-1] == 0)
|
|
414 return 1;
|
10725
|
415 }
|
|
416
|
|
417 if (!twoRegions)
|
|
418 {
|
|
419 for (k = 0; k <= nrBand0; k++)
|
|
420 sbr->f_master[k] = vk0[k];
|
|
421
|
|
422 sbr->N_master = nrBand0;
|
|
423 sbr->N_master = min(sbr->N_master, 64);
|
12527
|
424 return 0;
|
10725
|
425 }
|
|
426
|
10989
|
427 nrBand1 = 2 * find_bands(1 /* warped */, bands, k1, k2);
|
|
428 nrBand1 = min(nrBand1, 63);
|
10725
|
429
|
12527
|
430 q = find_initial_power(nrBand1, k1, k2);
|
|
431 qk = REAL_CONST(k1);
|
|
432 #ifdef FIXED_POINT
|
|
433 A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
|
|
434 #else
|
|
435 A_1 = (int32_t)(qk + .5);
|
|
436 #endif
|
10725
|
437 for (k = 0; k <= nrBand1 - 1; k++)
|
|
438 {
|
12527
|
439 int32_t A_0 = A_1;
|
|
440 #ifdef FIXED_POINT
|
|
441 qk = MUL_R(qk,q);
|
|
442 A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
|
|
443 #else
|
|
444 qk *= q;
|
|
445 A_1 = (int32_t)(qk + 0.5);
|
|
446 #endif
|
|
447 vDk1[k] = A_1 - A_0;
|
10725
|
448 }
|
|
449
|
|
450 if (vDk1[0] < vDk0[nrBand0 - 1])
|
|
451 {
|
|
452 int32_t change;
|
|
453
|
|
454 /* needed? */
|
|
455 qsort(vDk1, nrBand1 + 1, sizeof(vDk1[0]), longcmp);
|
|
456 change = vDk0[nrBand0 - 1] - vDk1[0];
|
|
457 vDk1[0] = vDk0[nrBand0 - 1];
|
|
458 vDk1[nrBand1 - 1] = vDk1[nrBand1 - 1] - change;
|
|
459 }
|
|
460
|
|
461 /* needed? */
|
|
462 qsort(vDk1, nrBand1, sizeof(vDk1[0]), longcmp);
|
|
463 vk1[0] = k1;
|
|
464 for (k = 1; k <= nrBand1; k++)
|
|
465 {
|
|
466 vk1[k] = vk1[k-1] + vDk1[k-1];
|
12527
|
467 if (vDk1[k-1] == 0)
|
|
468 return 1;
|
10725
|
469 }
|
|
470
|
|
471 sbr->N_master = nrBand0 + nrBand1;
|
|
472 sbr->N_master = min(sbr->N_master, 64);
|
|
473 for (k = 0; k <= nrBand0; k++)
|
|
474 {
|
|
475 sbr->f_master[k] = vk0[k];
|
|
476 }
|
|
477 for (k = nrBand0 + 1; k <= sbr->N_master; k++)
|
|
478 {
|
|
479 sbr->f_master[k] = vk1[k - nrBand0];
|
|
480 }
|
|
481
|
|
482 #if 0
|
|
483 printf("f_master[%d]: ", sbr->N_master);
|
|
484 for (k = 0; k <= sbr->N_master; k++)
|
|
485 {
|
|
486 printf("%d ", sbr->f_master[k]);
|
|
487 }
|
|
488 printf("\n");
|
|
489 #endif
|
12527
|
490
|
|
491 return 0;
|
10725
|
492 }
|
|
493
|
|
494 /* calculate the derived frequency border tables from f_master */
|
10989
|
495 uint8_t derived_frequency_table(sbr_info *sbr, uint8_t bs_xover_band,
|
|
496 uint8_t k2)
|
10725
|
497 {
|
|
498 uint8_t k, i;
|
|
499 uint32_t minus;
|
|
500
|
10989
|
501 /* The following relation shall be satisfied: bs_xover_band < N_Master */
|
|
502 if (sbr->N_master <= bs_xover_band)
|
|
503 return 1;
|
10725
|
504
|
10989
|
505 sbr->N_high = sbr->N_master - bs_xover_band;
|
|
506 sbr->N_low = (sbr->N_high>>1) + (sbr->N_high - ((sbr->N_high>>1)<<1));
|
10725
|
507
|
|
508 sbr->n[0] = sbr->N_low;
|
|
509 sbr->n[1] = sbr->N_high;
|
|
510
|
|
511 for (k = 0; k <= sbr->N_high; k++)
|
|
512 {
|
|
513 sbr->f_table_res[HI_RES][k] = sbr->f_master[k + bs_xover_band];
|
|
514 }
|
|
515
|
|
516 sbr->M = sbr->f_table_res[HI_RES][sbr->N_high] - sbr->f_table_res[HI_RES][0];
|
|
517 sbr->kx = sbr->f_table_res[HI_RES][0];
|
12527
|
518 if (sbr->kx > 32)
|
|
519 return 1;
|
|
520 if (sbr->kx + sbr->M > 64)
|
|
521 return 1;
|
10725
|
522
|
|
523 minus = (sbr->N_high & 1) ? 1 : 0;
|
|
524
|
|
525 for (k = 0; k <= sbr->N_low; k++)
|
|
526 {
|
|
527 if (k == 0)
|
|
528 i = 0;
|
|
529 else
|
|
530 i = 2*k - minus;
|
|
531 sbr->f_table_res[LO_RES][k] = sbr->f_table_res[HI_RES][i];
|
|
532 }
|
|
533
|
|
534 #if 0
|
|
535 printf("f_table_res[HI_RES][%d]: ", sbr->N_high);
|
|
536 for (k = 0; k <= sbr->N_high; k++)
|
|
537 {
|
|
538 printf("%d ", sbr->f_table_res[HI_RES][k]);
|
|
539 }
|
|
540 printf("\n");
|
|
541 #endif
|
|
542 #if 0
|
|
543 printf("f_table_res[LO_RES][%d]: ", sbr->N_low);
|
|
544 for (k = 0; k <= sbr->N_low; k++)
|
|
545 {
|
|
546 printf("%d ", sbr->f_table_res[LO_RES][k]);
|
|
547 }
|
|
548 printf("\n");
|
|
549 #endif
|
|
550
|
|
551 sbr->N_Q = 0;
|
|
552 if (sbr->bs_noise_bands == 0)
|
|
553 {
|
|
554 sbr->N_Q = 1;
|
|
555 } else {
|
10989
|
556 #if 0
|
10725
|
557 sbr->N_Q = max(1, (int32_t)(sbr->bs_noise_bands*(log(k2/(float)sbr->kx)/log(2.0)) + 0.5));
|
10989
|
558 #else
|
|
559 sbr->N_Q = max(1, find_bands(0, sbr->bs_noise_bands, sbr->kx, k2));
|
|
560 #endif
|
12527
|
561 sbr->N_Q = min(5, sbr->N_Q);
|
10725
|
562 }
|
|
563
|
|
564 for (k = 0; k <= sbr->N_Q; k++)
|
|
565 {
|
|
566 if (k == 0)
|
10989
|
567 {
|
10725
|
568 i = 0;
|
12527
|
569 } else {
|
|
570 /* i = i + (int32_t)((sbr->N_low - i)/(sbr->N_Q + 1 - k)); */
|
10989
|
571 i = i + (sbr->N_low - i)/(sbr->N_Q + 1 - k);
|
|
572 }
|
10725
|
573 sbr->f_table_noise[k] = sbr->f_table_res[LO_RES][i];
|
|
574 }
|
|
575
|
|
576 /* build table for mapping k to g in hf patching */
|
|
577 for (k = 0; k < 64; k++)
|
|
578 {
|
|
579 uint8_t g;
|
|
580 for (g = 0; g < sbr->N_Q; g++)
|
|
581 {
|
|
582 if ((sbr->f_table_noise[g] <= k) &&
|
|
583 (k < sbr->f_table_noise[g+1]))
|
|
584 {
|
|
585 sbr->table_map_k_to_g[k] = g;
|
|
586 break;
|
|
587 }
|
|
588 }
|
|
589 }
|
|
590
|
|
591 #if 0
|
|
592 printf("f_table_noise[%d]: ", sbr->N_Q);
|
|
593 for (k = 0; k <= sbr->N_Q; k++)
|
|
594 {
|
|
595 printf("%d ", sbr->f_table_noise[k]);
|
|
596 }
|
|
597 printf("\n");
|
|
598 #endif
|
10989
|
599
|
|
600 return 0;
|
10725
|
601 }
|
|
602
|
|
603 /* TODO: blegh, ugly */
|
|
604 /* Modified to calculate for all possible bs_limiter_bands always
|
|
605 * This reduces the number calls to this functions needed (now only on
|
|
606 * header reset)
|
|
607 */
|
|
608 void limiter_frequency_table(sbr_info *sbr)
|
|
609 {
|
10989
|
610 #if 0
|
|
611 static const real_t limiterBandsPerOctave[] = { REAL_CONST(1.2),
|
10725
|
612 REAL_CONST(2), REAL_CONST(3) };
|
10989
|
613 #else
|
|
614 static const real_t limiterBandsCompare[] = { REAL_CONST(1.328125),
|
|
615 REAL_CONST(1.1875), REAL_CONST(1.125) };
|
|
616 #endif
|
10725
|
617 uint8_t k, s;
|
|
618 int8_t nrLim;
|
10989
|
619 #if 0
|
10725
|
620 real_t limBands;
|
10989
|
621 #endif
|
10725
|
622
|
|
623 sbr->f_table_lim[0][0] = sbr->f_table_res[LO_RES][0] - sbr->kx;
|
|
624 sbr->f_table_lim[0][1] = sbr->f_table_res[LO_RES][sbr->N_low] - sbr->kx;
|
|
625 sbr->N_L[0] = 1;
|
|
626
|
|
627 for (s = 1; s < 4; s++)
|
|
628 {
|
12527
|
629 int32_t limTable[100 /*TODO*/] = {0};
|
|
630 uint8_t patchBorders[64/*??*/] = {0};
|
10725
|
631
|
10989
|
632 #if 0
|
10725
|
633 limBands = limiterBandsPerOctave[s - 1];
|
10989
|
634 #endif
|
10725
|
635
|
|
636 patchBorders[0] = sbr->kx;
|
|
637 for (k = 1; k <= sbr->noPatches; k++)
|
|
638 {
|
|
639 patchBorders[k] = patchBorders[k-1] + sbr->patchNoSubbands[k-1];
|
|
640 }
|
|
641
|
|
642 for (k = 0; k <= sbr->N_low; k++)
|
|
643 {
|
|
644 limTable[k] = sbr->f_table_res[LO_RES][k];
|
|
645 }
|
|
646 for (k = 1; k < sbr->noPatches; k++)
|
|
647 {
|
|
648 limTable[k+sbr->N_low] = patchBorders[k];
|
|
649 }
|
|
650
|
|
651 /* needed */
|
|
652 qsort(limTable, sbr->noPatches + sbr->N_low, sizeof(limTable[0]), longcmp);
|
|
653 k = 1;
|
|
654 nrLim = sbr->noPatches + sbr->N_low - 1;
|
|
655
|
|
656 if (nrLim < 0) // TODO: BIG FAT PROBLEM
|
|
657 return;
|
|
658
|
|
659 restart:
|
|
660 if (k <= nrLim)
|
|
661 {
|
|
662 real_t nOctaves;
|
|
663
|
|
664 if (limTable[k-1] != 0)
|
10989
|
665 #if 0
|
10725
|
666 nOctaves = REAL_CONST(log((float)limTable[k]/(float)limTable[k-1])/log(2.0));
|
10989
|
667 #endif
|
12527
|
668 #ifdef FIXED_POINT
|
|
669 nOctaves = SBR_DIV(REAL_CONST(limTable[k]),REAL_CONST(limTable[k-1]));
|
|
670 #else
|
10989
|
671 nOctaves = (real_t)limTable[k]/(real_t)limTable[k-1];
|
12527
|
672 #endif
|
10725
|
673 else
|
|
674 nOctaves = 0;
|
|
675
|
10989
|
676 #if 0
|
10725
|
677 if ((MUL(nOctaves,limBands)) < REAL_CONST(0.49))
|
10989
|
678 #else
|
|
679 if (nOctaves < limiterBandsCompare[s - 1])
|
|
680 #endif
|
10725
|
681 {
|
|
682 uint8_t i;
|
|
683 if (limTable[k] != limTable[k-1])
|
|
684 {
|
|
685 uint8_t found = 0, found2 = 0;
|
|
686 for (i = 0; i <= sbr->noPatches; i++)
|
|
687 {
|
|
688 if (limTable[k] == patchBorders[i])
|
|
689 found = 1;
|
|
690 }
|
|
691 if (found)
|
|
692 {
|
|
693 found2 = 0;
|
|
694 for (i = 0; i <= sbr->noPatches; i++)
|
|
695 {
|
|
696 if (limTable[k-1] == patchBorders[i])
|
|
697 found2 = 1;
|
|
698 }
|
|
699 if (found2)
|
|
700 {
|
|
701 k++;
|
|
702 goto restart;
|
|
703 } else {
|
|
704 /* remove (k-1)th element */
|
|
705 limTable[k-1] = sbr->f_table_res[LO_RES][sbr->N_low];
|
|
706 qsort(limTable, sbr->noPatches + sbr->N_low, sizeof(limTable[0]), longcmp);
|
|
707 nrLim--;
|
|
708 goto restart;
|
|
709 }
|
|
710 }
|
|
711 }
|
|
712 /* remove kth element */
|
|
713 limTable[k] = sbr->f_table_res[LO_RES][sbr->N_low];
|
|
714 qsort(limTable, nrLim, sizeof(limTable[0]), longcmp);
|
|
715 nrLim--;
|
|
716 goto restart;
|
|
717 } else {
|
|
718 k++;
|
|
719 goto restart;
|
|
720 }
|
|
721 }
|
|
722
|
|
723 sbr->N_L[s] = nrLim;
|
|
724 for (k = 0; k <= nrLim; k++)
|
|
725 {
|
|
726 sbr->f_table_lim[s][k] = limTable[k] - sbr->kx;
|
|
727 }
|
|
728
|
|
729 #if 0
|
|
730 printf("f_table_lim[%d][%d]: ", s, sbr->N_L[s]);
|
|
731 for (k = 0; k <= sbr->N_L[s]; k++)
|
|
732 {
|
|
733 printf("%d ", sbr->f_table_lim[s][k]);
|
|
734 }
|
|
735 printf("\n");
|
|
736 #endif
|
|
737 }
|
|
738 }
|
|
739
|
|
740 #endif
|