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