10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
|
|
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 **
|
|
25 ** $Id: sbr_fbt.c,v 1.1 2003/07/29 08:20:13 menno Exp $
|
|
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
|
|
40
|
|
41 /* calculate the start QMF channel for the master frequency band table */
|
|
42 /* parameter is also called k0 */
|
|
43 uint16_t qmf_start_channel(uint8_t bs_start_freq, uint8_t bs_samplerate_mode,
|
|
44 uint32_t sample_rate)
|
|
45 {
|
|
46 static int8_t offset[7][16] = {
|
|
47 { -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 },
|
|
48 { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13 },
|
|
49 { -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 },
|
|
50 { -6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 },
|
|
51 { -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20 },
|
|
52 { -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24 },
|
|
53 { 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24, 28, 33 }
|
|
54 };
|
|
55 uint8_t startMin;
|
|
56
|
|
57 if (sample_rate >= 64000)
|
|
58 {
|
|
59 startMin = (uint8_t)((5000.*128.)/(float)sample_rate + 0.5);
|
|
60 } else if (sample_rate < 32000) {
|
|
61 startMin = (uint8_t)((3000.*128.)/(float)sample_rate + 0.5);
|
|
62 } else {
|
|
63 startMin = (uint8_t)((4000.*128.)/(float)sample_rate + 0.5);
|
|
64 }
|
|
65
|
|
66 if (bs_samplerate_mode)
|
|
67 {
|
|
68 switch (sample_rate)
|
|
69 {
|
|
70 case 16000:
|
|
71 return startMin + offset[0][bs_start_freq];
|
|
72 case 22050:
|
|
73 return startMin + offset[1][bs_start_freq];
|
|
74 case 24000:
|
|
75 return startMin + offset[2][bs_start_freq];
|
|
76 case 32000:
|
|
77 return startMin + offset[3][bs_start_freq];
|
|
78 default:
|
|
79 if (sample_rate > 64000)
|
|
80 {
|
|
81 return startMin + offset[5][bs_start_freq];
|
|
82 } else { /* 44100 <= sample_rate <= 64000 */
|
|
83 return startMin + offset[4][bs_start_freq];
|
|
84 }
|
|
85 }
|
|
86 } else {
|
|
87 return startMin + offset[6][bs_start_freq];
|
|
88 }
|
|
89 }
|
|
90
|
|
91 static int32_t longcmp(const void *a, const void *b)
|
|
92 {
|
|
93 return ((int32_t)(*(int32_t*)a - *(int32_t*)b));
|
|
94 }
|
|
95
|
|
96 /* calculate the stop QMF channel for the master frequency band table */
|
|
97 /* parameter is also called k2 */
|
|
98 uint16_t qmf_stop_channel(uint8_t bs_stop_freq, uint32_t sample_rate,
|
|
99 uint16_t k0)
|
|
100 {
|
|
101 if (bs_stop_freq == 15)
|
|
102 {
|
|
103 return min(64, k0 * 3);
|
|
104 } else if (bs_stop_freq == 14) {
|
|
105 return min(64, k0 * 2);
|
|
106 } else {
|
|
107 uint8_t i;
|
|
108 uint8_t stopMin;
|
|
109 int32_t stopDk[13], stopDk_t[14], k2;
|
|
110
|
|
111 if (sample_rate >= 64000)
|
|
112 {
|
|
113 stopMin = (uint8_t)((10000.*128.)/(float)sample_rate + 0.5);
|
|
114 } else if (sample_rate < 32000) {
|
|
115 stopMin = (uint8_t)((6000.*128.)/(float)sample_rate + 0.5);
|
|
116 } else {
|
|
117 stopMin = (uint8_t)((8000.*128.)/(float)sample_rate + 0.5);
|
|
118 }
|
|
119
|
|
120 for (i = 0; i <= 13; i++)
|
|
121 {
|
|
122 stopDk_t[i] = (int32_t)(stopMin*pow(64.0/stopMin, i/13.0) + 0.5);
|
|
123 }
|
|
124 for (i = 0; i < 13; i++)
|
|
125 {
|
|
126 stopDk[i] = stopDk_t[i+1] - stopDk_t[i];
|
|
127 }
|
|
128
|
|
129 /* needed? or does this always reverse the array? */
|
|
130 qsort(stopDk, 13, sizeof(stopDk[0]), longcmp);
|
|
131
|
|
132 k2 = stopMin;
|
|
133 for (i = 0; i < bs_stop_freq; i++)
|
|
134 {
|
|
135 k2 += stopDk[i];
|
|
136 }
|
|
137 return min(64, k2);
|
|
138 }
|
|
139
|
|
140 return 0;
|
|
141 }
|
|
142
|
|
143 /* calculate the master frequency table from k0, k2, bs_freq_scale
|
|
144 and bs_alter_scale
|
|
145
|
|
146 version for bs_freq_scale = 0
|
|
147 */
|
|
148 void master_frequency_table_fs0(sbr_info *sbr, uint16_t k0, uint16_t k2,
|
|
149 uint8_t bs_alter_scale)
|
|
150 {
|
|
151 int8_t incr;
|
|
152 uint8_t k;
|
|
153 uint8_t dk;
|
|
154 uint32_t nrBands, k2Achieved;
|
|
155 int32_t k2Diff, vDk[64];
|
|
156
|
|
157 memset(vDk, 0, 64*sizeof(int32_t));
|
|
158
|
|
159 /* mft only defined for k2 > k0 */
|
|
160 if (k2 <= k0)
|
|
161 {
|
|
162 sbr->N_master = 0;
|
|
163 return;
|
|
164 }
|
|
165
|
|
166 dk = bs_alter_scale ? 2 : 1;
|
|
167 nrBands = 2 * (int32_t)((float)(k2-k0)/(dk*2) + (-1+dk)/2.0f);
|
|
168 nrBands = min(nrBands, 64);
|
|
169
|
|
170 k2Achieved = k0 + nrBands * dk;
|
|
171 k2Diff = k2 - k2Achieved;
|
|
172 /* for (k = 0; k <= nrBands; k++) Typo fix */
|
|
173 for (k = 0; k < nrBands; k++)
|
|
174 vDk[k] = dk;
|
|
175
|
|
176 if (k2Diff)
|
|
177 {
|
|
178 incr = (k2Diff > 0) ? -1 : 1;
|
|
179 k = (k2Diff > 0) ? (nrBands-1) : 0;
|
|
180
|
|
181 while (k2Diff != 0)
|
|
182 {
|
|
183 vDk[k] -= incr;
|
|
184 k += incr;
|
|
185 k2Diff += incr;
|
|
186 }
|
|
187 }
|
|
188
|
|
189 sbr->f_master[0] = k0;
|
|
190 for (k = 1; k <= nrBands; k++)
|
|
191 sbr->f_master[k] = sbr->f_master[k-1] + vDk[k-1];
|
|
192
|
|
193 sbr->N_master = nrBands;
|
|
194 sbr->N_master = min(sbr->N_master, 64);
|
|
195
|
|
196 #if 0
|
|
197 printf("f_master[%d]: ", nrBands);
|
|
198 for (k = 0; k <= nrBands; k++)
|
|
199 {
|
|
200 printf("%d ", sbr->f_master[k]);
|
|
201 }
|
|
202 printf("\n");
|
|
203 #endif
|
|
204 }
|
|
205
|
|
206 /*
|
|
207 version for bs_freq_scale > 0
|
|
208 */
|
|
209 void master_frequency_table(sbr_info *sbr, uint16_t k0, uint16_t k2,
|
|
210 uint8_t bs_freq_scale, uint8_t bs_alter_scale)
|
|
211 {
|
|
212 uint8_t k, bands, twoRegions;
|
|
213 uint16_t k1;
|
|
214 uint32_t nrBand0, nrBand1;
|
|
215 int32_t vDk0[64], vDk1[64];
|
|
216 int32_t vk0[64], vk1[64];
|
|
217 float warp;
|
|
218 uint8_t temp1[] = { 12, 10, 8 };
|
|
219 float temp2[] = { 1.0, 1.3 };
|
|
220
|
|
221 /* without memset code enters infinte loop,
|
|
222 so there must be some wrong table access */
|
|
223 memset(vDk0, 0, 64*sizeof(int32_t));
|
|
224 memset(vDk1, 0, 64*sizeof(int32_t));
|
|
225 memset(vk0, 0, 64*sizeof(int32_t));
|
|
226 memset(vk1, 0, 64*sizeof(int32_t));
|
|
227
|
|
228 /* mft only defined for k2 > k0 */
|
|
229 if (k2 <= k0)
|
|
230 {
|
|
231 sbr->N_master = 0;
|
|
232 return;
|
|
233 }
|
|
234
|
|
235 bands = temp1[bs_freq_scale-1];
|
|
236 warp = temp2[bs_alter_scale];
|
|
237
|
|
238 if ((float)k2/(float)k0 > 2.2449)
|
|
239 {
|
|
240 twoRegions = 1;
|
|
241 k1 = 2 * k0;
|
|
242 } else {
|
|
243 twoRegions = 0;
|
|
244 k1 = k2;
|
|
245 }
|
|
246
|
|
247 nrBand0 = 2 * (int32_t)(bands * log((float)k1/(float)k0)/(2.0*log(2.0)) + 0.5);
|
|
248 nrBand0 = min(nrBand0, 64);
|
|
249 for (k = 0; k <= nrBand0; k++)
|
|
250 {
|
|
251 /* MAPLE */
|
|
252 vDk0[k] = (int32_t)(k0 * pow((float)k1/(float)k0, (k+1)/(float)nrBand0)+0.5) -
|
|
253 (int32_t)(k0 * pow((float)k1/(float)k0, k/(float)nrBand0)+0.5);
|
|
254 }
|
|
255
|
|
256 /* needed? */
|
|
257 qsort(vDk0, nrBand0, sizeof(vDk0[0]), longcmp);
|
|
258
|
|
259 vk0[0] = k0;
|
|
260 for (k = 1; k <= nrBand0; k++)
|
|
261 {
|
|
262 vk0[k] = vk0[k-1] + vDk0[k-1];
|
|
263 }
|
|
264
|
|
265 if (!twoRegions)
|
|
266 {
|
|
267 for (k = 0; k <= nrBand0; k++)
|
|
268 sbr->f_master[k] = vk0[k];
|
|
269
|
|
270 sbr->N_master = nrBand0;
|
|
271 sbr->N_master = min(sbr->N_master, 64);
|
|
272 return;
|
|
273 }
|
|
274
|
|
275 nrBand1 = 2 * (int32_t)(bands * log((float)k2/(float)k1)/(2.0 * log(2.0) * warp) + 0.5);
|
|
276 nrBand1 = min(nrBand1, 64);
|
|
277
|
|
278 for (k = 0; k <= nrBand1 - 1; k++)
|
|
279 {
|
|
280 vDk1[k] = (int32_t)(k1 * pow((float)k2/(float)k1, (k+1)/(float)nrBand1)+0.5) -
|
|
281 (int32_t)(k1 * pow((float)k2/(float)k1, k/(float)nrBand1)+0.5);
|
|
282 }
|
|
283
|
|
284 if (vDk1[0] < vDk0[nrBand0 - 1])
|
|
285 {
|
|
286 int32_t change;
|
|
287
|
|
288 /* needed? */
|
|
289 qsort(vDk1, nrBand1 + 1, sizeof(vDk1[0]), longcmp);
|
|
290 change = vDk0[nrBand0 - 1] - vDk1[0];
|
|
291 vDk1[0] = vDk0[nrBand0 - 1];
|
|
292 vDk1[nrBand1 - 1] = vDk1[nrBand1 - 1] - change;
|
|
293 }
|
|
294
|
|
295 /* needed? */
|
|
296 qsort(vDk1, nrBand1, sizeof(vDk1[0]), longcmp);
|
|
297 vk1[0] = k1;
|
|
298 for (k = 1; k <= nrBand1; k++)
|
|
299 {
|
|
300 vk1[k] = vk1[k-1] + vDk1[k-1];
|
|
301 }
|
|
302
|
|
303 sbr->N_master = nrBand0 + nrBand1;
|
|
304 sbr->N_master = min(sbr->N_master, 64);
|
|
305 for (k = 0; k <= nrBand0; k++)
|
|
306 {
|
|
307 sbr->f_master[k] = vk0[k];
|
|
308 }
|
|
309 for (k = nrBand0 + 1; k <= sbr->N_master; k++)
|
|
310 {
|
|
311 sbr->f_master[k] = vk1[k - nrBand0];
|
|
312 }
|
|
313
|
|
314 #if 0
|
|
315 printf("f_master[%d]: ", sbr->N_master);
|
|
316 for (k = 0; k <= sbr->N_master; k++)
|
|
317 {
|
|
318 printf("%d ", sbr->f_master[k]);
|
|
319 }
|
|
320 printf("\n");
|
|
321 #endif
|
|
322 }
|
|
323
|
|
324 /* calculate the derived frequency border tables from f_master */
|
|
325 void derived_frequency_table(sbr_info *sbr, uint8_t bs_xover_band,
|
|
326 uint16_t k2)
|
|
327 {
|
|
328 uint8_t k, i;
|
|
329 uint32_t minus;
|
|
330
|
|
331 sbr->N_high = sbr->N_master - bs_xover_band;
|
|
332
|
|
333 /* is this accurate? */
|
|
334 sbr->N_low = sbr->N_high/2 + (sbr->N_high - 2 * (sbr->N_high/2));
|
|
335
|
|
336 sbr->n[0] = sbr->N_low;
|
|
337 sbr->n[1] = sbr->N_high;
|
|
338
|
|
339 for (k = 0; k <= sbr->N_high; k++)
|
|
340 {
|
|
341 sbr->f_table_res[HI_RES][k] = sbr->f_master[k + bs_xover_band];
|
|
342 }
|
|
343
|
|
344 sbr->M = sbr->f_table_res[HI_RES][sbr->N_high] - sbr->f_table_res[HI_RES][0];
|
|
345 sbr->kx = sbr->f_table_res[HI_RES][0];
|
|
346
|
|
347 /* correct? */
|
|
348 minus = (sbr->N_high & 1) ? 1 : 0;
|
|
349
|
|
350 for (k = 0; k <= sbr->N_low; k++)
|
|
351 {
|
|
352 if (k == 0)
|
|
353 i = 0;
|
|
354 else
|
|
355 i = 2*k - minus;
|
|
356 sbr->f_table_res[LO_RES][k] = sbr->f_table_res[HI_RES][i];
|
|
357 }
|
|
358 /* end correct? */
|
|
359
|
|
360 #if 0
|
|
361 printf("f_table_res[HI_RES][%d]: ", sbr->N_high);
|
|
362 for (k = 0; k <= sbr->N_high; k++)
|
|
363 {
|
|
364 printf("%d ", sbr->f_table_res[HI_RES][k]);
|
|
365 }
|
|
366 printf("\n");
|
|
367 #endif
|
|
368 #if 0
|
|
369 printf("f_table_res[LO_RES][%d]: ", sbr->N_low);
|
|
370 for (k = 0; k <= sbr->N_low; k++)
|
|
371 {
|
|
372 printf("%d ", sbr->f_table_res[LO_RES][k]);
|
|
373 }
|
|
374 printf("\n");
|
|
375 #endif
|
|
376
|
|
377 sbr->N_Q = 0;
|
|
378 if (sbr->bs_noise_bands == 0)
|
|
379 {
|
|
380 sbr->N_Q = 1;
|
|
381 } else {
|
|
382 /* MAPLE */
|
|
383 sbr->N_Q = max(1, (int32_t)(sbr->bs_noise_bands*(log(k2/(float)sbr->kx)/log(2.0)) + 0.5));
|
|
384 if (sbr->N_Q == 0)
|
|
385 sbr->N_Q = 1;
|
|
386 }
|
|
387 sbr->N_Q = min(5, sbr->N_Q);
|
|
388
|
|
389 for (k = 0; k <= sbr->N_Q; k++)
|
|
390 {
|
|
391 if (k == 0)
|
|
392 i = 0;
|
|
393 else /* is this accurate? */
|
|
394 i = i + (int32_t)((sbr->N_low - i)/(sbr->N_Q + 1 - k));
|
|
395 sbr->f_table_noise[k] = sbr->f_table_res[LO_RES][i];
|
|
396 }
|
|
397
|
|
398 /* build table for mapping k to g in hf patching */
|
|
399 for (k = 0; k < 64; k++)
|
|
400 {
|
|
401 uint8_t g;
|
|
402 for (g = 0; g < sbr->N_Q; g++)
|
|
403 {
|
|
404 if ((sbr->f_table_noise[g] <= k) &&
|
|
405 (k < sbr->f_table_noise[g+1]))
|
|
406 {
|
|
407 sbr->table_map_k_to_g[k] = g;
|
|
408 break;
|
|
409 }
|
|
410 }
|
|
411 }
|
|
412
|
|
413 #if 0
|
|
414 printf("f_table_noise[%d]: ", sbr->N_Q);
|
|
415 for (k = 0; k <= sbr->N_Q; k++)
|
|
416 {
|
|
417 printf("%d ", sbr->f_table_noise[k]);
|
|
418 }
|
|
419 printf("\n");
|
|
420 #endif
|
|
421 }
|
|
422
|
|
423 /* TODO: blegh, ugly */
|
|
424 /* Modified to calculate for all possible bs_limiter_bands always
|
|
425 * This reduces the number calls to this functions needed (now only on
|
|
426 * header reset)
|
|
427 */
|
|
428 void limiter_frequency_table(sbr_info *sbr)
|
|
429 {
|
|
430 static real_t limiterBandsPerOctave[] = { REAL_CONST(1.2),
|
|
431 REAL_CONST(2), REAL_CONST(3) };
|
|
432 uint8_t k, s;
|
|
433 int8_t nrLim;
|
|
434 int32_t limTable[100 /*TODO*/];
|
|
435 uint8_t patchBorders[64/*??*/];
|
|
436 real_t limBands;
|
|
437
|
|
438 sbr->f_table_lim[0][0] = sbr->f_table_res[LO_RES][0] - sbr->kx;
|
|
439 sbr->f_table_lim[0][1] = sbr->f_table_res[LO_RES][sbr->N_low] - sbr->kx;
|
|
440 sbr->N_L[0] = 1;
|
|
441
|
|
442 for (s = 1; s < 4; s++)
|
|
443 {
|
|
444 memset(limTable, 0, 100*sizeof(int32_t));
|
|
445
|
|
446 limBands = limiterBandsPerOctave[s - 1];
|
|
447
|
|
448 patchBorders[0] = sbr->kx;
|
|
449 for (k = 1; k <= sbr->noPatches; k++)
|
|
450 {
|
|
451 patchBorders[k] = patchBorders[k-1] + sbr->patchNoSubbands[k-1];
|
|
452 }
|
|
453
|
|
454 for (k = 0; k <= sbr->N_low; k++)
|
|
455 {
|
|
456 limTable[k] = sbr->f_table_res[LO_RES][k];
|
|
457 }
|
|
458 for (k = 1; k < sbr->noPatches; k++)
|
|
459 {
|
|
460 limTable[k+sbr->N_low] = patchBorders[k];
|
|
461 }
|
|
462
|
|
463 /* needed */
|
|
464 qsort(limTable, sbr->noPatches + sbr->N_low, sizeof(limTable[0]), longcmp);
|
|
465 k = 1;
|
|
466 nrLim = sbr->noPatches + sbr->N_low - 1;
|
|
467
|
|
468 if (nrLim < 0) // TODO: BIG FAT PROBLEM
|
|
469 return;
|
|
470
|
|
471 restart:
|
|
472 if (k <= nrLim)
|
|
473 {
|
|
474 real_t nOctaves;
|
|
475
|
|
476 if (limTable[k-1] != 0)
|
|
477 nOctaves = REAL_CONST(log((float)limTable[k]/(float)limTable[k-1])/log(2.0));
|
|
478 else
|
|
479 nOctaves = 0;
|
|
480
|
|
481 if ((MUL(nOctaves,limBands)) < REAL_CONST(0.49))
|
|
482 {
|
|
483 uint8_t i;
|
|
484 if (limTable[k] != limTable[k-1])
|
|
485 {
|
|
486 uint8_t found = 0, found2 = 0;
|
|
487 for (i = 0; i <= sbr->noPatches; i++)
|
|
488 {
|
|
489 if (limTable[k] == patchBorders[i])
|
|
490 found = 1;
|
|
491 }
|
|
492 if (found)
|
|
493 {
|
|
494 found2 = 0;
|
|
495 for (i = 0; i <= sbr->noPatches; i++)
|
|
496 {
|
|
497 if (limTable[k-1] == patchBorders[i])
|
|
498 found2 = 1;
|
|
499 }
|
|
500 if (found2)
|
|
501 {
|
|
502 k++;
|
|
503 goto restart;
|
|
504 } else {
|
|
505 /* remove (k-1)th element */
|
|
506 limTable[k-1] = sbr->f_table_res[LO_RES][sbr->N_low];
|
|
507 qsort(limTable, sbr->noPatches + sbr->N_low, sizeof(limTable[0]), longcmp);
|
|
508 nrLim--;
|
|
509 goto restart;
|
|
510 }
|
|
511 }
|
|
512 }
|
|
513 /* remove kth element */
|
|
514 limTable[k] = sbr->f_table_res[LO_RES][sbr->N_low];
|
|
515 qsort(limTable, nrLim, sizeof(limTable[0]), longcmp);
|
|
516 nrLim--;
|
|
517 goto restart;
|
|
518 } else {
|
|
519 k++;
|
|
520 goto restart;
|
|
521 }
|
|
522 }
|
|
523
|
|
524 sbr->N_L[s] = nrLim;
|
|
525 for (k = 0; k <= nrLim; k++)
|
|
526 {
|
|
527 sbr->f_table_lim[s][k] = limTable[k] - sbr->kx;
|
|
528 }
|
|
529
|
|
530 #if 0
|
|
531 printf("f_table_lim[%d][%d]: ", s, sbr->N_L[s]);
|
|
532 for (k = 0; k <= sbr->N_L[s]; k++)
|
|
533 {
|
|
534 printf("%d ", sbr->f_table_lim[s][k]);
|
|
535 }
|
|
536 printf("\n");
|
|
537 #endif
|
|
538 }
|
|
539 }
|
|
540
|
|
541 #endif
|