sbr_fbt.c

00001 /*
00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
00004 **  
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 ** 
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 ** 
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software 
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 **
00019 ** Any non-GPL usage of this software or parts of this software is strictly
00020 ** forbidden.
00021 **
00022 ** Software using this code must display the following message visibly in the
00023 ** software:
00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
00025 ** in, for example, the about-box or help/startup screen.
00026 **
00027 ** Commercial non-GPL licensing of this software is possible.
00028 ** For more info contact Ahead Software through [email protected].
00029 **
00030 ** $Id: sbr_fbt.c,v 1.2 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 /* Calculate frequency band tables */
00034 
00035 #include "common.h"
00036 #include "structs.h"
00037 
00038 #ifdef SBR_DEC
00039 
00040 #include <stdlib.h>
00041 
00042 #include "sbr_syntax.h"
00043 #include "sbr_fbt.h"
00044 
00045 /* static function declarations */
00046 static int32_t find_bands(uint8_t warp, uint8_t bands, uint8_t a0, uint8_t a1);
00047 
00048 
00049 /* calculate the start QMF channel for the master frequency band table */
00050 /* parameter is also called k0 */
00051 uint8_t qmf_start_channel(uint8_t bs_start_freq, uint8_t bs_samplerate_mode,
00052                            uint32_t sample_rate)
00053 {
00054     static const uint8_t startMinTable[12] = { 7, 7, 10, 11, 12, 16, 16,
00055         17, 24, 32, 35, 48 };
00056     static const uint8_t offsetIndexTable[12] = { 5, 5, 4, 4, 4, 3, 2, 1, 0,
00057         6, 6, 6 };
00058     static const int8_t offset[7][16] = {
00059         { -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 },
00060         { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13 },
00061         { -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 },
00062         { -6, -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16 },
00063         { -4, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20 },
00064         { -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24 },
00065         { 0, 1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 16, 20, 24, 28, 33 }
00066     };
00067     uint8_t startMin = startMinTable[get_sr_index(sample_rate)];
00068     uint8_t offsetIndex = offsetIndexTable[get_sr_index(sample_rate)];
00069 
00070 #if 0 /* replaced with table (startMinTable) */
00071     if (sample_rate >= 64000)
00072     {
00073         startMin = (uint8_t)((5000.*128.)/(float)sample_rate + 0.5);
00074     } else if (sample_rate < 32000) {
00075         startMin = (uint8_t)((3000.*128.)/(float)sample_rate + 0.5);
00076     } else {
00077         startMin = (uint8_t)((4000.*128.)/(float)sample_rate + 0.5);
00078     }
00079 #endif
00080 
00081     if (bs_samplerate_mode)
00082     {
00083         return startMin + offset[offsetIndex][bs_start_freq];
00084 
00085 #if 0 /* replaced by offsetIndexTable */ 
00086         switch (sample_rate)
00087         {
00088         case 16000:
00089             return startMin + offset[0][bs_start_freq];
00090         case 22050:
00091             return startMin + offset[1][bs_start_freq];
00092         case 24000:
00093             return startMin + offset[2][bs_start_freq];
00094         case 32000:
00095             return startMin + offset[3][bs_start_freq];
00096         default:
00097             if (sample_rate > 64000)
00098             {
00099                 return startMin + offset[5][bs_start_freq];
00100             } else { /* 44100 <= sample_rate <= 64000 */
00101                 return startMin + offset[4][bs_start_freq];
00102             }
00103         }
00104 #endif
00105     } else {
00106         return startMin + offset[6][bs_start_freq];
00107     }
00108 }
00109 
00110 static int longcmp(const void *a, const void *b)
00111 {
00112     return ((int)(*(int32_t*)a - *(int32_t*)b));
00113 }
00114 
00115 /* calculate the stop QMF channel for the master frequency band table */
00116 /* parameter is also called k2 */
00117 uint8_t qmf_stop_channel(uint8_t bs_stop_freq, uint32_t sample_rate,
00118                           uint8_t k0)
00119 {
00120     if (bs_stop_freq == 15)
00121     {
00122         return min(64, k0 * 3);
00123     } else if (bs_stop_freq == 14) {
00124         return min(64, k0 * 2);
00125     } else {
00126         static const uint8_t stopMinTable[12] = { 13, 15, 20, 21, 23,
00127             32, 32, 35, 48, 64, 70, 96 };
00128         static const int8_t offset[12][14] = {
00129             { 0, 2, 4, 6, 8, 11, 14, 18, 22, 26, 31, 37, 44, 51 },
00130             { 0, 2, 4, 6, 8, 11, 14, 18, 22, 26, 31, 36, 42, 49 },
00131             { 0, 2, 4, 6, 8, 11, 14, 17, 21, 25, 29, 34, 39, 44 },
00132             { 0, 2, 4, 6, 8, 11, 14, 17, 20, 24, 28, 33, 38, 43 },
00133             { 0, 2, 4, 6, 8, 11, 14, 17, 20, 24, 28, 32, 36, 41 },
00134             { 0, 2, 4, 6, 8, 10, 12, 14, 17, 20, 23, 26, 29, 32 },
00135             { 0, 2, 4, 6, 8, 10, 12, 14, 17, 20, 23, 26, 29, 32 },
00136             { 0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 20, 23, 26, 29 },
00137             { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16 },
00138             { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
00139             { 0, -1, -2, -3, -4, -5, -6, -6, -6, -6, -6, -6, -6, -6 },
00140             { 0, -3, -6, -9, -12, -15, -18, -20, -22, -24, -26, -28, -30, -32 }
00141         };
00142 #if 0
00143         uint8_t i;
00144         int32_t stopDk[13], stopDk_t[14], k2;
00145 #endif
00146         uint8_t stopMin = stopMinTable[get_sr_index(sample_rate)];
00147 
00148 #if 0 /* replaced by table lookup */
00149         if (sample_rate >= 64000)
00150         {
00151             stopMin = (uint8_t)((10000.*128.)/(float)sample_rate + 0.5);
00152         } else if (sample_rate < 32000) {
00153             stopMin = (uint8_t)((6000.*128.)/(float)sample_rate + 0.5);
00154         } else {
00155             stopMin = (uint8_t)((8000.*128.)/(float)sample_rate + 0.5);
00156         }
00157 #endif
00158 
00159 #if 0 /* replaced by table lookup */
00160         /* diverging power series */
00161         for (i = 0; i <= 13; i++)
00162         {
00163             stopDk_t[i] = (int32_t)(stopMin*pow(64.0/stopMin, i/13.0) + 0.5);
00164         }
00165         for (i = 0; i < 13; i++)
00166         {
00167             stopDk[i] = stopDk_t[i+1] - stopDk_t[i];
00168         }
00169 
00170         /* needed? */
00171         qsort(stopDk, 13, sizeof(stopDk[0]), longcmp);
00172 
00173         k2 = stopMin;
00174         for (i = 0; i < bs_stop_freq; i++)
00175         {
00176             k2 += stopDk[i];
00177         }
00178         return min(64, k2);
00179 #endif
00180         /* bs_stop_freq <= 13 */
00181         return min(64, stopMin + offset[get_sr_index(sample_rate)][min(bs_stop_freq, 13)]);
00182     }
00183 
00184     return 0;
00185 }
00186 
00187 /* calculate the master frequency table from k0, k2, bs_freq_scale
00188    and bs_alter_scale
00189 
00190    version for bs_freq_scale = 0
00191 */
00192 uint8_t master_frequency_table_fs0(sbr_info *sbr, uint8_t k0, uint8_t k2,
00193                                    uint8_t bs_alter_scale)
00194 {
00195     int8_t incr;
00196     uint8_t k;
00197     uint8_t dk;
00198     uint32_t nrBands, k2Achieved;
00199     int32_t k2Diff, vDk[64] = {0};
00200 
00201     /* mft only defined for k2 > k0 */
00202     if (k2 <= k0)
00203     {
00204         sbr->N_master = 0;
00205         return 1;
00206     }
00207 
00208     dk = bs_alter_scale ? 2 : 1;
00209 
00210 #if 0 /* replaced by float-less design */
00211     nrBands = 2 * (int32_t)((float)(k2-k0)/(dk*2) + (-1+dk)/2.0f);
00212 #else
00213     if (bs_alter_scale)
00214     {
00215         nrBands = (((k2-k0+2)>>2)<<1);
00216     } else {
00217         nrBands = (((k2-k0)>>1)<<1);
00218     }
00219 #endif
00220     nrBands = min(nrBands, 63);
00221     if (nrBands <= 0)
00222         return 1;
00223 
00224     k2Achieved = k0 + nrBands * dk;
00225     k2Diff = k2 - k2Achieved;
00226     for (k = 0; k < nrBands; k++)
00227         vDk[k] = dk;
00228 
00229     if (k2Diff)
00230     {
00231         incr = (k2Diff > 0) ? -1 : 1;
00232         k = (uint8_t) ((k2Diff > 0) ? (nrBands-1) : 0);
00233 
00234         while (k2Diff != 0)
00235         {
00236             vDk[k] -= incr;
00237             k += incr;
00238             k2Diff += incr;
00239         }
00240     }
00241 
00242     sbr->f_master[0] = k0;
00243     for (k = 1; k <= nrBands; k++)
00244         sbr->f_master[k] = (uint8_t)(sbr->f_master[k-1] + vDk[k-1]);
00245 
00246     sbr->N_master = (uint8_t)nrBands;
00247     sbr->N_master = (min(sbr->N_master, 64));
00248 
00249 #if 0
00250     printf("f_master[%d]: ", nrBands);
00251     for (k = 0; k <= nrBands; k++)
00252     {
00253         printf("%d ", sbr->f_master[k]);
00254     }
00255     printf("\n");
00256 #endif
00257 
00258     return 0;
00259 }
00260 
00261 /*
00262    This function finds the number of bands using this formula:
00263     bands * log(a1/a0)/log(2.0) + 0.5
00264 */
00265 static int32_t find_bands(uint8_t warp, uint8_t bands, uint8_t a0, uint8_t a1)
00266 {
00267 #ifdef FIXED_POINT
00268     /* table with log2() values */
00269     static const real_t log2Table[65] = {
00270         COEF_CONST(0.0), COEF_CONST(0.0), COEF_CONST(1.0000000000), COEF_CONST(1.5849625007),
00271         COEF_CONST(2.0000000000), COEF_CONST(2.3219280949), COEF_CONST(2.5849625007), COEF_CONST(2.8073549221),
00272         COEF_CONST(3.0000000000), COEF_CONST(3.1699250014), COEF_CONST(3.3219280949), COEF_CONST(3.4594316186),
00273         COEF_CONST(3.5849625007), COEF_CONST(3.7004397181), COEF_CONST(3.8073549221), COEF_CONST(3.9068905956),
00274         COEF_CONST(4.0000000000), COEF_CONST(4.0874628413), COEF_CONST(4.1699250014), COEF_CONST(4.2479275134),
00275         COEF_CONST(4.3219280949), COEF_CONST(4.3923174228), COEF_CONST(4.4594316186), COEF_CONST(4.5235619561),
00276         COEF_CONST(4.5849625007), COEF_CONST(4.6438561898), COEF_CONST(4.7004397181), COEF_CONST(4.7548875022),
00277         COEF_CONST(4.8073549221), COEF_CONST(4.8579809951), COEF_CONST(4.9068905956), COEF_CONST(4.9541963104),
00278         COEF_CONST(5.0000000000), COEF_CONST(5.0443941194), COEF_CONST(5.0874628413), COEF_CONST(5.1292830169),
00279         COEF_CONST(5.1699250014), COEF_CONST(5.2094533656), COEF_CONST(5.2479275134), COEF_CONST(5.2854022189),
00280         COEF_CONST(5.3219280949), COEF_CONST(5.3575520046), COEF_CONST(5.3923174228), COEF_CONST(5.4262647547),
00281         COEF_CONST(5.4594316186), COEF_CONST(5.4918530963), COEF_CONST(5.5235619561), COEF_CONST(5.5545888517),
00282         COEF_CONST(5.5849625007), COEF_CONST(5.6147098441), COEF_CONST(5.6438561898), COEF_CONST(5.6724253420),
00283         COEF_CONST(5.7004397181), COEF_CONST(5.7279204546), COEF_CONST(5.7548875022), COEF_CONST(5.7813597135),
00284         COEF_CONST(5.8073549221), COEF_CONST(5.8328900142), COEF_CONST(5.8579809951), COEF_CONST(5.8826430494),
00285         COEF_CONST(5.9068905956), COEF_CONST(5.9307373376), COEF_CONST(5.9541963104), COEF_CONST(5.9772799235),
00286         COEF_CONST(6.0)
00287     };
00288     real_t r0 = log2Table[a0]; /* coef */
00289     real_t r1 = log2Table[a1]; /* coef */
00290     real_t r2 = (r1 - r0); /* coef */
00291 
00292     if (warp)
00293         r2 = MUL_C(r2, COEF_CONST(1.0/1.3));
00294 
00295     /* convert r2 to real and then multiply and round */
00296     r2 = (r2 >> (COEF_BITS-REAL_BITS)) * bands + (1<<(REAL_BITS-1));
00297 
00298     return (r2 >> REAL_BITS);
00299 #else
00300     real_t div = (real_t)log(2.0);
00301     if (warp) div *= (real_t)1.3;
00302 
00303     return (int32_t)(bands * log((float)a1/(float)a0)/div + 0.5);
00304 #endif
00305 }
00306 
00307 static real_t find_initial_power(uint8_t bands, uint8_t a0, uint8_t a1)
00308 {
00309 #ifdef FIXED_POINT
00310     /* table with log() values */
00311     static const real_t logTable[65] = {
00312         COEF_CONST(0.0), COEF_CONST(0.0), COEF_CONST(0.6931471806), COEF_CONST(1.0986122887),
00313         COEF_CONST(1.3862943611), COEF_CONST(1.6094379124), COEF_CONST(1.7917594692), COEF_CONST(1.9459101491),
00314         COEF_CONST(2.0794415417), COEF_CONST(2.1972245773), COEF_CONST(2.3025850930), COEF_CONST(2.3978952728),
00315         COEF_CONST(2.4849066498), COEF_CONST(2.5649493575), COEF_CONST(2.6390573296), COEF_CONST(2.7080502011),
00316         COEF_CONST(2.7725887222), COEF_CONST(2.8332133441), COEF_CONST(2.8903717579), COEF_CONST(2.9444389792),
00317         COEF_CONST(2.9957322736), COEF_CONST(3.0445224377), COEF_CONST(3.0910424534), COEF_CONST(3.1354942159),
00318         COEF_CONST(3.1780538303), COEF_CONST(3.2188758249), COEF_CONST(3.2580965380), COEF_CONST(3.2958368660),
00319         COEF_CONST(3.3322045102), COEF_CONST(3.3672958300), COEF_CONST(3.4011973817), COEF_CONST(3.4339872045),
00320         COEF_CONST(3.4657359028), COEF_CONST(3.4965075615), COEF_CONST(3.5263605246), COEF_CONST(3.5553480615),
00321         COEF_CONST(3.5835189385), COEF_CONST(3.6109179126), COEF_CONST(3.6375861597), COEF_CONST(3.6635616461),
00322         COEF_CONST(3.6888794541), COEF_CONST(3.7135720667), COEF_CONST(3.7376696183), COEF_CONST(3.7612001157),
00323         COEF_CONST(3.7841896339), COEF_CONST(3.8066624898), COEF_CONST(3.8286413965), COEF_CONST(3.8501476017),
00324         COEF_CONST(3.8712010109), COEF_CONST(3.8918202981), COEF_CONST(3.9120230054), COEF_CONST(3.9318256327),
00325         COEF_CONST(3.9512437186), COEF_CONST(3.9702919136), COEF_CONST(3.9889840466), COEF_CONST(4.0073331852),
00326         COEF_CONST(4.0253516907), COEF_CONST(4.0430512678), COEF_CONST(4.0604430105), COEF_CONST(4.0775374439),
00327         COEF_CONST(4.0943445622), COEF_CONST(4.1108738642), COEF_CONST(4.1271343850), COEF_CONST(4.1431347264),
00328         COEF_CONST(4.158883083)
00329     };
00330     /* standard Taylor polynomial coefficients for exp(x) around 0 */
00331     /* a polynomial around x=1 is more precise, as most values are around 1.07,
00332        but this is just fine already */
00333     static const real_t c1 = COEF_CONST(1.0);
00334     static const real_t c2 = COEF_CONST(1.0/2.0);
00335     static const real_t c3 = COEF_CONST(1.0/6.0);
00336     static const real_t c4 = COEF_CONST(1.0/24.0);
00337 
00338     real_t r0 = logTable[a0]; /* coef */
00339     real_t r1 = logTable[a1]; /* coef */
00340     real_t r2 = (r1 - r0) / bands; /* coef */
00341     real_t rexp = c1 + MUL_C((c1 + MUL_C((c2 + MUL_C((c3 + MUL_C(c4,r2)), r2)), r2)), r2);
00342 
00343     return (rexp >> (COEF_BITS-REAL_BITS)); /* real */
00344 #else
00345     return (real_t)pow((real_t)a1/(real_t)a0, 1.0/(real_t)bands);
00346 #endif
00347 }
00348 
00349 /*
00350    version for bs_freq_scale > 0
00351 */
00352 uint8_t master_frequency_table(sbr_info *sbr, uint8_t k0, uint8_t k2,
00353                                uint8_t bs_freq_scale, uint8_t bs_alter_scale)
00354 {
00355     uint8_t k, bands, twoRegions;
00356     uint8_t k1;
00357     uint8_t nrBand0, nrBand1;
00358     int32_t vDk0[64] = {0}, vDk1[64] = {0};
00359     int32_t vk0[64] = {0}, vk1[64] = {0};
00360     uint8_t temp1[] = { 6, 5, 4 };
00361     real_t q, qk;
00362     int32_t A_1;
00363 #ifdef FIXED_POINT
00364     real_t rk2, rk0;
00365 #endif
00366 
00367     /* mft only defined for k2 > k0 */
00368     if (k2 <= k0)
00369     {
00370         sbr->N_master = 0;
00371         return 1;
00372     }
00373 
00374     bands = temp1[bs_freq_scale-1];
00375 
00376 #ifdef FIXED_POINT
00377     rk0 = (real_t)k0 << REAL_BITS;
00378     rk2 = (real_t)k2 << REAL_BITS;
00379     if (rk2 > MUL_C(rk0, COEF_CONST(2.2449)))
00380 #else
00381     if ((float)k2/(float)k0 > 2.2449)
00382 #endif
00383     {
00384         twoRegions = 1;
00385         k1 = k0 << 1;
00386     } else {
00387         twoRegions = 0;
00388         k1 = k2;
00389     }
00390 
00391     nrBand0 = (uint8_t)(2 * find_bands(0, bands, k0, k1));
00392     nrBand0 = min(nrBand0, 63);
00393     if (nrBand0 <= 0)
00394         return 1;
00395 
00396     q = find_initial_power(nrBand0, k0, k1);
00397 #ifdef FIXED_POINT
00398     qk = (real_t)k0 << REAL_BITS;
00399     //A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
00400     A_1 = k0;
00401 #else
00402     qk = REAL_CONST(k0);
00403     A_1 = (int32_t)(qk + .5);
00404 #endif
00405     for (k = 0; k <= nrBand0; k++)
00406     {
00407         int32_t A_0 = A_1;
00408 #ifdef FIXED_POINT
00409         qk = MUL_R(qk,q);
00410         A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
00411 #else
00412         qk *= q;
00413         A_1 = (int32_t)(qk + 0.5);
00414 #endif
00415         vDk0[k] = A_1 - A_0;
00416     }
00417 
00418     /* needed? */
00419     qsort(vDk0, nrBand0, sizeof(vDk0[0]), longcmp);
00420 
00421     vk0[0] = k0;
00422     for (k = 1; k <= nrBand0; k++)
00423     {
00424         vk0[k] = vk0[k-1] + vDk0[k-1];
00425         if (vDk0[k-1] == 0)
00426             return 1;
00427     }
00428 
00429     if (!twoRegions)
00430     {
00431         for (k = 0; k <= nrBand0; k++)
00432             sbr->f_master[k] = (uint8_t) vk0[k];
00433 
00434         sbr->N_master = nrBand0;
00435         sbr->N_master = min(sbr->N_master, 64);
00436         return 0;
00437     }
00438 
00439     nrBand1 = (uint8_t)(2 * find_bands(1 /* warped */, bands, k1, k2));
00440     nrBand1 = min(nrBand1, 63);
00441 
00442     q = find_initial_power(nrBand1, k1, k2);
00443 #ifdef FIXED_POINT
00444     qk = (real_t)k1 << REAL_BITS;
00445     //A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
00446     A_1 = k1;
00447 #else
00448     qk = REAL_CONST(k1);
00449     A_1 = (int32_t)(qk + .5);
00450 #endif
00451     for (k = 0; k <= nrBand1 - 1; k++)
00452     {
00453         int32_t A_0 = A_1;
00454 #ifdef FIXED_POINT
00455         qk = MUL_R(qk,q);
00456         A_1 = (int32_t)((qk + REAL_CONST(0.5)) >> REAL_BITS);
00457 #else
00458         qk *= q;
00459         A_1 = (int32_t)(qk + 0.5);
00460 #endif
00461         vDk1[k] = A_1 - A_0;
00462     }
00463 
00464     if (vDk1[0] < vDk0[nrBand0 - 1])
00465     {
00466         int32_t change;
00467 
00468         /* needed? */
00469         qsort(vDk1, nrBand1 + 1, sizeof(vDk1[0]), longcmp);
00470         change = vDk0[nrBand0 - 1] - vDk1[0];
00471         vDk1[0] = vDk0[nrBand0 - 1];
00472         vDk1[nrBand1 - 1] = vDk1[nrBand1 - 1] - change;
00473     }
00474 
00475     /* needed? */
00476     qsort(vDk1, nrBand1, sizeof(vDk1[0]), longcmp);
00477     vk1[0] = k1;
00478     for (k = 1; k <= nrBand1; k++)
00479     {
00480         vk1[k] = vk1[k-1] + vDk1[k-1];
00481         if (vDk1[k-1] == 0)
00482             return 1;
00483     }
00484 
00485     sbr->N_master = nrBand0 + nrBand1;
00486     sbr->N_master = min(sbr->N_master, 64);
00487     for (k = 0; k <= nrBand0; k++)
00488     {
00489         sbr->f_master[k] =  (uint8_t) vk0[k];
00490     }
00491     for (k = nrBand0 + 1; k <= sbr->N_master; k++)
00492     {
00493         sbr->f_master[k] = (uint8_t) vk1[k - nrBand0];
00494     }
00495 
00496 #if 0
00497     printf("f_master[%d]: ", sbr->N_master);
00498     for (k = 0; k <= sbr->N_master; k++)
00499     {
00500         printf("%d ", sbr->f_master[k]);
00501     }
00502     printf("\n");
00503 #endif
00504 
00505     return 0;
00506 }
00507 
00508 /* calculate the derived frequency border tables from f_master */
00509 uint8_t derived_frequency_table(sbr_info *sbr, uint8_t bs_xover_band,
00510                                 uint8_t k2)
00511 {
00512     uint8_t k, i;
00513     uint32_t minus;
00514 
00515     /* The following relation shall be satisfied: bs_xover_band < N_Master */
00516     if (sbr->N_master <= bs_xover_band)
00517         return 1;
00518 
00519     sbr->N_high = sbr->N_master - bs_xover_band;
00520     sbr->N_low = (sbr->N_high>>1) + (sbr->N_high - ((sbr->N_high>>1)<<1));
00521 
00522     sbr->n[0] = sbr->N_low;
00523     sbr->n[1] = sbr->N_high;
00524 
00525     for (k = 0; k <= sbr->N_high; k++)
00526     {
00527         sbr->f_table_res[HI_RES][k] = sbr->f_master[k + bs_xover_band];
00528     }
00529 
00530     sbr->M = sbr->f_table_res[HI_RES][sbr->N_high] - sbr->f_table_res[HI_RES][0];
00531     sbr->kx = sbr->f_table_res[HI_RES][0];
00532     if (sbr->kx > 32)
00533         return 1;
00534     if (sbr->kx + sbr->M > 64)
00535         return 1;
00536 
00537     minus = (sbr->N_high & 1) ? 1 : 0;
00538 
00539     for (k = 0; k <= sbr->N_low; k++)
00540     {
00541         if (k == 0)
00542             i = 0;
00543         else
00544             i = (uint8_t)(2*k - minus);
00545         sbr->f_table_res[LO_RES][k] = sbr->f_table_res[HI_RES][i];
00546     }
00547 
00548 #if 0
00549     printf("bs_freq_scale: %d\n", sbr->bs_freq_scale);
00550     printf("bs_limiter_bands: %d\n", sbr->bs_limiter_bands);
00551     printf("f_table_res[HI_RES][%d]: ", sbr->N_high);
00552     for (k = 0; k <= sbr->N_high; k++)
00553     {
00554         printf("%d ", sbr->f_table_res[HI_RES][k]);
00555     }
00556     printf("\n");
00557 #endif
00558 #if 0
00559     printf("f_table_res[LO_RES][%d]: ", sbr->N_low);
00560     for (k = 0; k <= sbr->N_low; k++)
00561     {
00562         printf("%d ", sbr->f_table_res[LO_RES][k]);
00563     }
00564     printf("\n");
00565 #endif
00566 
00567     sbr->N_Q = 0;
00568     if (sbr->bs_noise_bands == 0)
00569     {
00570         sbr->N_Q = 1;
00571     } else {
00572 #if 0
00573         sbr->N_Q = max(1, (int32_t)(sbr->bs_noise_bands*(log(k2/(float)sbr->kx)/log(2.0)) + 0.5));
00574 #else
00575         sbr->N_Q = (uint8_t)(max(1, find_bands(0, sbr->bs_noise_bands, sbr->kx, k2)));
00576 #endif
00577         sbr->N_Q = min(5, sbr->N_Q);
00578     }
00579 
00580     for (k = 0; k <= sbr->N_Q; k++)
00581     {
00582         if (k == 0)
00583         {
00584             i = 0;
00585         } else {
00586             /* i = i + (int32_t)((sbr->N_low - i)/(sbr->N_Q + 1 - k)); */
00587             i = i + (sbr->N_low - i)/(sbr->N_Q + 1 - k);
00588         }
00589         sbr->f_table_noise[k] = sbr->f_table_res[LO_RES][i];
00590     }
00591 
00592     /* build table for mapping k to g in hf patching */
00593     for (k = 0; k < 64; k++)
00594     {
00595         uint8_t g;
00596         for (g = 0; g < sbr->N_Q; g++)
00597         {
00598             if ((sbr->f_table_noise[g] <= k) &&
00599                 (k < sbr->f_table_noise[g+1]))
00600             {
00601                 sbr->table_map_k_to_g[k] = g;
00602                 break;
00603             }
00604         }
00605     }
00606 
00607 #if 0
00608     printf("f_table_noise[%d]: ", sbr->N_Q);
00609     for (k = 0; k <= sbr->N_Q; k++)
00610     {
00611         printf("%d ", sbr->f_table_noise[k] - sbr->kx);
00612     }
00613     printf("\n");
00614 #endif
00615 
00616     return 0;
00617 }
00618 
00619 /* TODO: blegh, ugly */
00620 /* Modified to calculate for all possible bs_limiter_bands always
00621  * This reduces the number calls to this functions needed (now only on
00622  * header reset)
00623  */
00624 void limiter_frequency_table(sbr_info *sbr)
00625 {
00626 #if 0
00627     static const real_t limiterBandsPerOctave[] = { REAL_CONST(1.2),
00628         REAL_CONST(2), REAL_CONST(3) };
00629 #else
00630     static const real_t limiterBandsCompare[] = { REAL_CONST(1.327152),
00631         REAL_CONST(1.185093), REAL_CONST(1.119872) };
00632 #endif
00633     uint8_t k, s;
00634     int8_t nrLim;
00635 #if 0
00636     real_t limBands;
00637 #endif
00638 
00639     sbr->f_table_lim[0][0] = sbr->f_table_res[LO_RES][0] - sbr->kx;
00640     sbr->f_table_lim[0][1] = sbr->f_table_res[LO_RES][sbr->N_low] - sbr->kx;
00641     sbr->N_L[0] = 1;
00642 
00643 #if 0
00644     printf("f_table_lim[%d][%d]: ", 0, sbr->N_L[0]);
00645     for (k = 0; k <= sbr->N_L[0]; k++)
00646     {
00647         printf("%d ", sbr->f_table_lim[0][k]);
00648     }
00649     printf("\n");
00650 #endif
00651 
00652     for (s = 1; s < 4; s++)
00653     {
00654         int32_t limTable[100 /*TODO*/] = {0};
00655         uint8_t patchBorders[64/*??*/] = {0};
00656 
00657 #if 0
00658         limBands = limiterBandsPerOctave[s - 1];
00659 #endif
00660 
00661         patchBorders[0] = sbr->kx;
00662         for (k = 1; k <= sbr->noPatches; k++)
00663         {
00664             patchBorders[k] = patchBorders[k-1] + sbr->patchNoSubbands[k-1];
00665         }
00666 
00667         for (k = 0; k <= sbr->N_low; k++)
00668         {
00669             limTable[k] = sbr->f_table_res[LO_RES][k];
00670         }
00671         for (k = 1; k < sbr->noPatches; k++)
00672         {
00673             limTable[k+sbr->N_low] = patchBorders[k];
00674         }
00675 
00676         /* needed */
00677         qsort(limTable, sbr->noPatches + sbr->N_low, sizeof(limTable[0]), longcmp);
00678         k = 1;
00679         nrLim = sbr->noPatches + sbr->N_low - 1;
00680 
00681         if (nrLim < 0) // TODO: BIG FAT PROBLEM
00682             return;
00683 
00684 restart:
00685         if (k <= nrLim)
00686         {
00687             real_t nOctaves;
00688 
00689             if (limTable[k-1] != 0)
00690 #if 0
00691                 nOctaves = REAL_CONST(log((float)limTable[k]/(float)limTable[k-1])/log(2.0));
00692 #else
00693 #ifdef FIXED_POINT
00694                 nOctaves = DIV_R((limTable[k]<<REAL_BITS),REAL_CONST(limTable[k-1]));
00695 #else
00696                 nOctaves = (real_t)limTable[k]/(real_t)limTable[k-1];
00697 #endif
00698 #endif
00699             else
00700                 nOctaves = 0;
00701 
00702 #if 0
00703             if ((MUL_R(nOctaves,limBands)) < REAL_CONST(0.49))
00704 #else
00705             if (nOctaves < limiterBandsCompare[s - 1])
00706 #endif
00707             {
00708                 uint8_t i;
00709                 if (limTable[k] != limTable[k-1])
00710                 {
00711                     uint8_t found = 0, found2 = 0;
00712                     for (i = 0; i <= sbr->noPatches; i++)
00713                     {
00714                         if (limTable[k] == patchBorders[i])
00715                             found = 1;
00716                     }
00717                     if (found)
00718                     {
00719                         found2 = 0;
00720                         for (i = 0; i <= sbr->noPatches; i++)
00721                         {
00722                             if (limTable[k-1] == patchBorders[i])
00723                                 found2 = 1;
00724                         }
00725                         if (found2)
00726                         {
00727                             k++;
00728                             goto restart;
00729                         } else {
00730                             /* remove (k-1)th element */
00731                             limTable[k-1] = sbr->f_table_res[LO_RES][sbr->N_low];
00732                             qsort(limTable, sbr->noPatches + sbr->N_low, sizeof(limTable[0]), longcmp);
00733                             nrLim--;
00734                             goto restart;
00735                         }
00736                     }
00737                 }
00738                 /* remove kth element */
00739                 limTable[k] = sbr->f_table_res[LO_RES][sbr->N_low];
00740                 qsort(limTable, nrLim, sizeof(limTable[0]), longcmp);
00741                 nrLim--;
00742                 goto restart;
00743             } else {
00744                 k++;
00745                 goto restart;
00746             }
00747         }
00748 
00749         sbr->N_L[s] = nrLim;
00750         for (k = 0; k <= nrLim; k++)
00751         {
00752             sbr->f_table_lim[s][k] = limTable[k] - sbr->kx;
00753         }
00754 
00755 #if 0
00756         printf("f_table_lim[%d][%d]: ", s, sbr->N_L[s]);
00757         for (k = 0; k <= sbr->N_L[s]; k++)
00758         {
00759             printf("%d ", sbr->f_table_lim[s][k]);
00760         }
00761         printf("\n");
00762 #endif
00763     }
00764 }
00765 
00766 #endif

Generated on Tue Dec 13 14:47:44 2005 for guliverkli by  doxygen 1.4.5