00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "common.h"
00034 #include "structs.h"
00035
00036 #ifdef SBR_DEC
00037
00038 #include "sbr_syntax.h"
00039 #include "bits.h"
00040 #include "sbr_huff.h"
00041 #include "sbr_e_nf.h"
00042
00043
00044 typedef const int8_t (*sbr_huff_tab)[2];
00045
00046 static const int8_t t_huffman_env_1_5dB[120][2] = {
00047 { 1, 2 }, { -64, -65 }, { 3, 4 }, { -63, -66 },
00048 { 5, 6 }, { -62, -67 }, { 7, 8 }, { -61, -68 },
00049 { 9, 10 }, { -60, -69 }, { 11, 12 }, { -59, -70 },
00050 { 13, 14 }, { -58, -71 }, { 15, 16 }, { -57, -72 },
00051 { 17, 18 }, { -73, -56 }, { 19, 21 }, { -74, 20 },
00052 { -55, -75 }, { 22, 26 }, { 23, 24 }, { -54, -76 },
00053 { -77, 25 }, { -53, -78 }, { 27, 34 }, { 28, 29 },
00054 { -52, -79 }, { 30, 31 }, { -80, -51 }, { 32, 33 },
00055 { -83, -82 }, { -81, -50 }, { 35, 57 }, { 36, 40 },
00056 { 37, 38 }, { -88, -84 }, { -48, 39 }, { -90, -85 },
00057 { 41, 46 }, { 42, 43 }, { -49, -87 }, { 44, 45 },
00058 { -89, -86 }, {-124,-123 }, { 47, 50 }, { 48, 49 },
00059 {-122,-121 }, {-120,-119 }, { 51, 54 }, { 52, 53 },
00060 {-118,-117 }, {-116,-115 }, { 55, 56 }, {-114,-113 },
00061 {-112,-111 }, { 58, 89 }, { 59, 74 }, { 60, 67 },
00062 { 61, 64 }, { 62, 63 }, {-110,-109 }, {-108,-107 },
00063 { 65, 66 }, {-106,-105 }, {-104,-103 }, { 68, 71 },
00064 { 69, 70 }, {-102,-101 }, {-100, -99 }, { 72, 73 },
00065 { -98, -97 }, { -96, -95 }, { 75, 82 }, { 76, 79 },
00066 { 77, 78 }, { -94, -93 }, { -92, -91 }, { 80, 81 },
00067 { -47, -46 }, { -45, -44 }, { 83, 86 }, { 84, 85 },
00068 { -43, -42 }, { -41, -40 }, { 87, 88 }, { -39, -38 },
00069 { -37, -36 }, { 90, 105 }, { 91, 98 }, { 92, 95 },
00070 { 93, 94 }, { -35, -34 }, { -33, -32 }, { 96, 97 },
00071 { -31, -30 }, { -29, -28 }, { 99, 102 }, { 100, 101 },
00072 { -27, -26 }, { -25, -24 }, { 103, 104 }, { -23, -22 },
00073 { -21, -20 }, { 106, 113 }, { 107, 110 }, { 108, 109 },
00074 { -19, -18 }, { -17, -16 }, { 111, 112 }, { -15, -14 },
00075 { -13, -12 }, { 114, 117 }, { 115, 116 }, { -11, -10 },
00076 { -9, -8 }, { 118, 119 }, { -7, -6 }, { -5, -4 }
00077 };
00078
00079 static const int8_t f_huffman_env_1_5dB[120][2] = {
00080 { 1, 2 }, { -64, -65 }, { 3, 4 }, { -63, -66 },
00081 { 5, 6 }, { -67, -62 }, { 7, 8 }, { -68, -61 },
00082 { 9, 10 }, { -69, -60 }, { 11, 13 }, { -70, 12 },
00083 { -59, -71 }, { 14, 16 }, { -58, 15 }, { -72, -57 },
00084 { 17, 19 }, { -73, 18 }, { -56, -74 }, { 20, 23 },
00085 { 21, 22 }, { -55, -75 }, { -54, -53 }, { 24, 27 },
00086 { 25, 26 }, { -76, -52 }, { -77, -51 }, { 28, 31 },
00087 { 29, 30 }, { -50, -78 }, { -79, -49 }, { 32, 36 },
00088 { 33, 34 }, { -48, -47 }, { -80, 35 }, { -81, -82 },
00089 { 37, 47 }, { 38, 41 }, { 39, 40 }, { -83, -46 },
00090 { -45, -84 }, { 42, 44 }, { -85, 43 }, { -44, -43 },
00091 { 45, 46 }, { -88, -87 }, { -86, -90 }, { 48, 66 },
00092 { 49, 56 }, { 50, 53 }, { 51, 52 }, { -92, -42 },
00093 { -41, -39 }, { 54, 55 }, {-105, -89 }, { -38, -37 },
00094 { 57, 60 }, { 58, 59 }, { -94, -91 }, { -40, -36 },
00095 { 61, 63 }, { -20, 62 }, {-115,-110 }, { 64, 65 },
00096 {-108,-107 }, {-101, -97 }, { 67, 89 }, { 68, 75 },
00097 { 69, 72 }, { 70, 71 }, { -95, -93 }, { -34, -27 },
00098 { 73, 74 }, { -22, -17 }, { -16,-124 }, { 76, 82 },
00099 { 77, 79 }, {-123, 78 }, {-122,-121 }, { 80, 81 },
00100 {-120,-119 }, {-118,-117 }, { 83, 86 }, { 84, 85 },
00101 {-116,-114 }, {-113,-112 }, { 87, 88 }, {-111,-109 },
00102 {-106,-104 }, { 90, 105 }, { 91, 98 }, { 92, 95 },
00103 { 93, 94 }, {-103,-102 }, {-100, -99 }, { 96, 97 },
00104 { -98, -96 }, { -35, -33 }, { 99, 102 }, { 100, 101 },
00105 { -32, -31 }, { -30, -29 }, { 103, 104 }, { -28, -26 },
00106 { -25, -24 }, { 106, 113 }, { 107, 110 }, { 108, 109 },
00107 { -23, -21 }, { -19, -18 }, { 111, 112 }, { -15, -14 },
00108 { -13, -12 }, { 114, 117 }, { 115, 116 }, { -11, -10 },
00109 { -9, -8 }, { 118, 119 }, { -7, -6 }, { -5, -4 }
00110 };
00111
00112 static const int8_t t_huffman_env_bal_1_5dB[48][2] = {
00113 { -64, 1 }, { -63, 2 }, { -65, 3 }, { -62, 4 },
00114 { -66, 5 }, { -61, 6 }, { -67, 7 }, { -60, 8 },
00115 { -68, 9 }, { 10, 11 }, { -69, -59 }, { 12, 13 },
00116 { -70, -58 }, { 14, 28 }, { 15, 21 }, { 16, 18 },
00117 { -57, 17 }, { -71, -56 }, { 19, 20 }, { -88, -87 },
00118 { -86, -85 }, { 22, 25 }, { 23, 24 }, { -84, -83 },
00119 { -82, -81 }, { 26, 27 }, { -80, -79 }, { -78, -77 },
00120 { 29, 36 }, { 30, 33 }, { 31, 32 }, { -76, -75 },
00121 { -74, -73 }, { 34, 35 }, { -72, -55 }, { -54, -53 },
00122 { 37, 41 }, { 38, 39 }, { -52, -51 }, { -50, 40 },
00123 { -49, -48 }, { 42, 45 }, { 43, 44 }, { -47, -46 },
00124 { -45, -44 }, { 46, 47 }, { -43, -42 }, { -41, -40 }
00125 };
00126
00127 static const int8_t f_huffman_env_bal_1_5dB[48][2] = {
00128 { -64, 1 }, { -65, 2 }, { -63, 3 }, { -66, 4 },
00129 { -62, 5 }, { -61, 6 }, { -67, 7 }, { -68, 8 },
00130 { -60, 9 }, { 10, 11 }, { -69, -59 }, { -70, 12 },
00131 { -58, 13 }, { 14, 17 }, { -71, 15 }, { -57, 16 },
00132 { -56, -73 }, { 18, 32 }, { 19, 25 }, { 20, 22 },
00133 { -72, 21 }, { -88, -87 }, { 23, 24 }, { -86, -85 },
00134 { -84, -83 }, { 26, 29 }, { 27, 28 }, { -82, -81 },
00135 { -80, -79 }, { 30, 31 }, { -78, -77 }, { -76, -75 },
00136 { 33, 40 }, { 34, 37 }, { 35, 36 }, { -74, -55 },
00137 { -54, -53 }, { 38, 39 }, { -52, -51 }, { -50, -49 },
00138 { 41, 44 }, { 42, 43 }, { -48, -47 }, { -46, -45 },
00139 { 45, 46 }, { -44, -43 }, { -42, 47 }, { -41, -40 }
00140 };
00141
00142 static const int8_t t_huffman_env_3_0dB[62][2] = {
00143 { -64, 1 }, { -65, 2 }, { -63, 3 }, { -66, 4 },
00144 { -62, 5 }, { -67, 6 }, { -61, 7 }, { -68, 8 },
00145 { -60, 9 }, { 10, 11 }, { -69, -59 }, { 12, 14 },
00146 { -70, 13 }, { -71, -58 }, { 15, 18 }, { 16, 17 },
00147 { -72, -57 }, { -73, -74 }, { 19, 22 }, { -56, 20 },
00148 { -55, 21 }, { -54, -77 }, { 23, 31 }, { 24, 25 },
00149 { -75, -76 }, { 26, 27 }, { -78, -53 }, { 28, 29 },
00150 { -52, -95 }, { -94, 30 }, { -93, -92 }, { 32, 47 },
00151 { 33, 40 }, { 34, 37 }, { 35, 36 }, { -91, -90 },
00152 { -89, -88 }, { 38, 39 }, { -87, -86 }, { -85, -84 },
00153 { 41, 44 }, { 42, 43 }, { -83, -82 }, { -81, -80 },
00154 { 45, 46 }, { -79, -51 }, { -50, -49 }, { 48, 55 },
00155 { 49, 52 }, { 50, 51 }, { -48, -47 }, { -46, -45 },
00156 { 53, 54 }, { -44, -43 }, { -42, -41 }, { 56, 59 },
00157 { 57, 58 }, { -40, -39 }, { -38, -37 }, { 60, 61 },
00158 { -36, -35 }, { -34, -33 }
00159 };
00160
00161 static const int8_t f_huffman_env_3_0dB[62][2] = {
00162 { -64, 1 }, { -65, 2 }, { -63, 3 }, { -66, 4 },
00163 { -62, 5 }, { -67, 6 }, { 7, 8 }, { -61, -68 },
00164 { 9, 10 }, { -60, -69 }, { 11, 12 }, { -59, -70 },
00165 { 13, 14 }, { -58, -71 }, { 15, 16 }, { -57, -72 },
00166 { 17, 19 }, { -56, 18 }, { -55, -73 }, { 20, 24 },
00167 { 21, 22 }, { -74, -54 }, { -53, 23 }, { -75, -76 },
00168 { 25, 30 }, { 26, 27 }, { -52, -51 }, { 28, 29 },
00169 { -77, -79 }, { -50, -49 }, { 31, 39 }, { 32, 35 },
00170 { 33, 34 }, { -78, -46 }, { -82, -88 }, { 36, 37 },
00171 { -83, -48 }, { -47, 38 }, { -86, -85 }, { 40, 47 },
00172 { 41, 44 }, { 42, 43 }, { -80, -44 }, { -43, -42 },
00173 { 45, 46 }, { -39, -87 }, { -84, -40 }, { 48, 55 },
00174 { 49, 52 }, { 50, 51 }, { -95, -94 }, { -93, -92 },
00175 { 53, 54 }, { -91, -90 }, { -89, -81 }, { 56, 59 },
00176 { 57, 58 }, { -45, -41 }, { -38, -37 }, { 60, 61 },
00177 { -36, -35 }, { -34, -33 }
00178 };
00179
00180 static const int8_t t_huffman_env_bal_3_0dB[24][2] = {
00181 { -64, 1 }, { -63, 2 }, { -65, 3 }, { -66, 4 },
00182 { -62, 5 }, { -61, 6 }, { -67, 7 }, { -68, 8 },
00183 { -60, 9 }, { 10, 16 }, { 11, 13 }, { -69, 12 },
00184 { -76, -75 }, { 14, 15 }, { -74, -73 }, { -72, -71 },
00185 { 17, 20 }, { 18, 19 }, { -70, -59 }, { -58, -57 },
00186 { 21, 22 }, { -56, -55 }, { -54, 23 }, { -53, -52 }
00187 };
00188
00189 static const int8_t f_huffman_env_bal_3_0dB[24][2] = {
00190 { -64, 1 }, { -65, 2 }, { -63, 3 }, { -66, 4 },
00191 { -62, 5 }, { -61, 6 }, { -67, 7 }, { -68, 8 },
00192 { -60, 9 }, { 10, 13 }, { -69, 11 }, { -59, 12 },
00193 { -58, -76 }, { 14, 17 }, { 15, 16 }, { -75, -74 },
00194 { -73, -72 }, { 18, 21 }, { 19, 20 }, { -71, -70 },
00195 { -57, -56 }, { 22, 23 }, { -55, -54 }, { -53, -52 }
00196 };
00197
00198 static const int8_t t_huffman_noise_3_0dB[62][2] = {
00199 { -64, 1 }, { -63, 2 }, { -65, 3 }, { -66, 4 },
00200 { -62, 5 }, { -67, 6 }, { 7, 8 }, { -61, -68 },
00201 { 9, 30 }, { 10, 15 }, { -60, 11 }, { -69, 12 },
00202 { 13, 14 }, { -59, -53 }, { -95, -94 }, { 16, 23 },
00203 { 17, 20 }, { 18, 19 }, { -93, -92 }, { -91, -90 },
00204 { 21, 22 }, { -89, -88 }, { -87, -86 }, { 24, 27 },
00205 { 25, 26 }, { -85, -84 }, { -83, -82 }, { 28, 29 },
00206 { -81, -80 }, { -79, -78 }, { 31, 46 }, { 32, 39 },
00207 { 33, 36 }, { 34, 35 }, { -77, -76 }, { -75, -74 },
00208 { 37, 38 }, { -73, -72 }, { -71, -70 }, { 40, 43 },
00209 { 41, 42 }, { -58, -57 }, { -56, -55 }, { 44, 45 },
00210 { -54, -52 }, { -51, -50 }, { 47, 54 }, { 48, 51 },
00211 { 49, 50 }, { -49, -48 }, { -47, -46 }, { 52, 53 },
00212 { -45, -44 }, { -43, -42 }, { 55, 58 }, { 56, 57 },
00213 { -41, -40 }, { -39, -38 }, { 59, 60 }, { -37, -36 },
00214 { -35, 61 }, { -34, -33 }
00215 };
00216
00217 static const int8_t t_huffman_noise_bal_3_0dB[24][2] = {
00218 { -64, 1 }, { -65, 2 }, { -63, 3 }, { 4, 9 },
00219 { -66, 5 }, { -62, 6 }, { 7, 8 }, { -76, -75 },
00220 { -74, -73 }, { 10, 17 }, { 11, 14 }, { 12, 13 },
00221 { -72, -71 }, { -70, -69 }, { 15, 16 }, { -68, -67 },
00222 { -61, -60 }, { 18, 21 }, { 19, 20 }, { -59, -58 },
00223 { -57, -56 }, { 22, 23 }, { -55, -54 }, { -53, -52 }
00224 };
00225
00226
00227 static INLINE int16_t sbr_huff_dec(bitfile *ld, sbr_huff_tab t_huff)
00228 {
00229 uint8_t bit;
00230 int16_t index = 0;
00231
00232 while (index >= 0)
00233 {
00234 bit = (uint8_t)faad_get1bit(ld);
00235 index = t_huff[index][bit];
00236 }
00237
00238 return index + 64;
00239 }
00240
00241
00242 void sbr_envelope(bitfile *ld, sbr_info *sbr, uint8_t ch)
00243 {
00244 uint8_t env, band;
00245 int8_t delta = 0;
00246 sbr_huff_tab t_huff, f_huff;
00247
00248 if ((sbr->L_E[ch] == 1) && (sbr->bs_frame_class[ch] == FIXFIX))
00249 sbr->amp_res[ch] = 0;
00250 else
00251 sbr->amp_res[ch] = sbr->bs_amp_res;
00252
00253 if ((sbr->bs_coupling) && (ch == 1))
00254 {
00255 delta = 1;
00256 if (sbr->amp_res[ch])
00257 {
00258 t_huff = t_huffman_env_bal_3_0dB;
00259 f_huff = f_huffman_env_bal_3_0dB;
00260 } else {
00261 t_huff = t_huffman_env_bal_1_5dB;
00262 f_huff = f_huffman_env_bal_1_5dB;
00263 }
00264 } else {
00265 delta = 0;
00266 if (sbr->amp_res[ch])
00267 {
00268 t_huff = t_huffman_env_3_0dB;
00269 f_huff = f_huffman_env_3_0dB;
00270 } else {
00271 t_huff = t_huffman_env_1_5dB;
00272 f_huff = f_huffman_env_1_5dB;
00273 }
00274 }
00275
00276 for (env = 0; env < sbr->L_E[ch]; env++)
00277 {
00278 if (sbr->bs_df_env[ch][env] == 0)
00279 {
00280 if ((sbr->bs_coupling == 1) && (ch == 1))
00281 {
00282 if (sbr->amp_res[ch])
00283 {
00284 sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 5
00285 DEBUGVAR(1,272,"sbr_envelope(): bs_data_env")) << delta);
00286 } else {
00287 sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6
00288 DEBUGVAR(1,273,"sbr_envelope(): bs_data_env")) << delta);
00289 }
00290 } else {
00291 if (sbr->amp_res[ch])
00292 {
00293 sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 6
00294 DEBUGVAR(1,274,"sbr_envelope(): bs_data_env")) << delta);
00295 } else {
00296 sbr->E[ch][0][env] = (uint16_t)(faad_getbits(ld, 7
00297 DEBUGVAR(1,275,"sbr_envelope(): bs_data_env")) << delta);
00298 }
00299 }
00300
00301 for (band = 1; band < sbr->n[sbr->f[ch][env]]; band++)
00302 {
00303 sbr->E[ch][band][env] = (sbr_huff_dec(ld, f_huff) << delta);
00304 }
00305
00306 } else {
00307 for (band = 0; band < sbr->n[sbr->f[ch][env]]; band++)
00308 {
00309 sbr->E[ch][band][env] = (sbr_huff_dec(ld, t_huff) << delta);
00310 }
00311 }
00312 }
00313
00314 extract_envelope_data(sbr, ch);
00315 }
00316
00317
00318 void sbr_noise(bitfile *ld, sbr_info *sbr, uint8_t ch)
00319 {
00320 uint8_t noise, band;
00321 int8_t delta = 0;
00322 sbr_huff_tab t_huff, f_huff;
00323
00324 if ((sbr->bs_coupling == 1) && (ch == 1))
00325 {
00326 delta = 1;
00327 t_huff = t_huffman_noise_bal_3_0dB;
00328 f_huff = f_huffman_env_bal_3_0dB;
00329 } else {
00330 delta = 0;
00331 t_huff = t_huffman_noise_3_0dB;
00332 f_huff = f_huffman_env_3_0dB;
00333 }
00334
00335 for (noise = 0; noise < sbr->L_Q[ch]; noise++)
00336 {
00337 if(sbr->bs_df_noise[ch][noise] == 0)
00338 {
00339 if ((sbr->bs_coupling == 1) && (ch == 1))
00340 {
00341 sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
00342 DEBUGVAR(1,276,"sbr_noise(): bs_data_noise")) << delta);
00343 } else {
00344 sbr->Q[ch][0][noise] = (faad_getbits(ld, 5
00345 DEBUGVAR(1,277,"sbr_noise(): bs_data_noise")) << delta);
00346 }
00347 for (band = 1; band < sbr->N_Q; band++)
00348 {
00349 sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, f_huff) << delta);
00350 }
00351 } else {
00352 for (band = 0; band < sbr->N_Q; band++)
00353 {
00354 sbr->Q[ch][band][noise] = (sbr_huff_dec(ld, t_huff) << delta);
00355 }
00356 }
00357 }
00358
00359 extract_noise_floor_data(sbr, ch);
00360 }
00361
00362 #endif