1/* 2 * layer2.c: Mpeg Layer-2 audio decoder 3 * 4 * Copyright (C) 1999-2010 The L.A.M.E. project 5 * 6 * Initially written by Michael Hipp, see also AUTHORS and README. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Library General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Library General Public License for more details. 17 * 18 * You should have received a copy of the GNU Library General Public 19 * License along with this library; if not, write to the 20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21 * Boston, MA 02111-1307, USA. 22 */ 23/* $Id$ */ 24 25#ifdef HAVE_CONFIG_H 26# include <config.h> 27#endif 28 29#include "common.h" 30#include "layer2.h" 31#include "l2tables.h" 32#include "decode_i386.h" 33 34#ifdef WITH_DMALLOC 35#include <dmalloc.h> 36#endif 37#include <assert.h> 38 39static int gd_are_hip_tables_layer2_initialized = 0; 40 41static unsigned char grp_3tab[32 * 3] = { 0, }; /* used: 27 */ 42static unsigned char grp_5tab[128 * 3] = { 0, }; /* used: 125 */ 43static unsigned char grp_9tab[1024 * 3] = { 0, }; /* used: 729 */ 44 45 46void 47hip_init_tables_layer2(void) 48{ 49 static const double mulmul[27] = { 50 0.0, -2.0 / 3.0, 2.0 / 3.0, 51 2.0 / 7.0, 2.0 / 15.0, 2.0 / 31.0, 2.0 / 63.0, 2.0 / 127.0, 2.0 / 255.0, 52 2.0 / 511.0, 2.0 / 1023.0, 2.0 / 2047.0, 2.0 / 4095.0, 2.0 / 8191.0, 53 2.0 / 16383.0, 2.0 / 32767.0, 2.0 / 65535.0, 54 -4.0 / 5.0, -2.0 / 5.0, 2.0 / 5.0, 4.0 / 5.0, 55 -8.0 / 9.0, -4.0 / 9.0, -2.0 / 9.0, 2.0 / 9.0, 4.0 / 9.0, 8.0 / 9.0 56 }; 57 static const unsigned char base[3][9] = { 58 {1, 0, 2,}, 59 {17, 18, 0, 19, 20,}, 60 {21, 1, 22, 23, 0, 24, 25, 2, 26} 61 }; 62 int i, j, k, l, len; 63 real *table; 64 static const int tablen[3] = { 3, 5, 9 }; 65 static unsigned char *itable, *tables[3] = { grp_3tab, grp_5tab, grp_9tab }; 66 67 if (gd_are_hip_tables_layer2_initialized) { 68 return; 69 } 70 gd_are_hip_tables_layer2_initialized = 1; 71 72 for (i = 0; i < 3; i++) { 73 itable = tables[i]; 74 len = tablen[i]; 75 for (j = 0; j < len; j++) 76 for (k = 0; k < len; k++) 77 for (l = 0; l < len; l++) { 78 *itable++ = base[i][l]; 79 *itable++ = base[i][k]; 80 *itable++ = base[i][j]; 81 } 82 } 83 84 for (k = 0; k < 27; k++) { 85 double m = mulmul[k]; 86 table = muls[k]; 87 for (j = 3, i = 0; i < 63; i++, j--) 88 *table++ = (real) (m * pow(2.0, (double) j / 3.0)); 89 *table++ = 0.0; 90 } 91} 92 93 94static unsigned char* 95grp_table_select(short d1, unsigned int idx) 96{ 97 /* RH: it seems to be common, that idx is larger than the table's sizes. 98 is it OK to return a zero vector in this case? FIXME 99 /*/ 100 static unsigned char dummy_table[] = { 0,0,0 }; 101 unsigned int x; 102 switch (d1) { 103 case 3: 104 x = 3*3*3; 105 idx = idx < x ? idx : x; 106 return &grp_3tab[3 * idx]; 107 case 5: 108 x = 5*5*5; 109 idx = idx < x ? idx : x; 110 return &grp_5tab[3 * idx]; 111 case 9: 112 x = 9*9*9; 113 idx = idx < x ? idx : x; 114 return &grp_9tab[3 * idx]; 115 default: 116 /* fatal error */ 117 assert(0); 118 } 119 return &dummy_table[0]; 120} 121 122typedef struct sideinfo_layer_II_struct 123{ 124 unsigned char allocation[SBLIMIT][2]; 125 unsigned char scalefactor[SBLIMIT][2][3]; /* subband / channel / block */ 126} sideinfo_layer_II; 127 128 129 130static void 131II_step_one(PMPSTR mp, sideinfo_layer_II *si, struct frame *fr) 132{ 133 int nch = fr->stereo; 134 int sblimit = fr->II_sblimit; 135 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit; 136 struct al_table2 const *alloc1 = fr->alloc; 137 unsigned char scfsi[SBLIMIT][2]; 138 int i, ch; 139 140 memset(si, 0, sizeof(*si)); 141 if (jsbound > sblimit) 142 jsbound = sblimit; 143 if (nch == 2) { 144 for (i = 0; i < jsbound; ++i) { 145 short step = alloc1->bits; 146 unsigned char b0 = get_leq_8_bits(mp, step); 147 unsigned char b1 = get_leq_8_bits(mp, step); 148 alloc1 += ((size_t)1 << step); 149 si->allocation[i][0] = b0; 150 si->allocation[i][1] = b1; 151 } 152 for (i = jsbound; i < sblimit; ++i) { 153 short step = alloc1->bits; 154 unsigned char b0 = get_leq_8_bits(mp, step); 155 alloc1 += ((size_t)1 << step); 156 si->allocation[i][0] = b0; 157 si->allocation[i][1] = b0; 158 } 159 for (i = 0; i < sblimit; ++i) { 160 unsigned char n0 = si->allocation[i][0]; 161 unsigned char n1 = si->allocation[i][1]; 162 unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0; 163 unsigned char b1 = n1 ? get_leq_8_bits(mp, 2) : 0; 164 scfsi[i][0] = b0; 165 scfsi[i][1] = b1; 166 } 167 } 168 else { /* mono */ 169 for (i = 0; i < sblimit; ++i) { 170 short step = alloc1->bits; 171 unsigned char b0 = get_leq_8_bits(mp, step); 172 alloc1 += ((size_t)1 << step); 173 si->allocation[i][0] = b0; 174 } 175 for (i = 0; i < sblimit; ++i) { 176 unsigned char n0 = si->allocation[i][0]; 177 unsigned char b0 = n0 ? get_leq_8_bits(mp, 2) : 0; 178 scfsi[i][0] = b0; 179 } 180 } 181 for (i = 0; i < sblimit; ++i) { 182 for (ch = 0; ch < nch; ++ch) { 183 unsigned char s0 = 0, s1 = 0, s2 = 0; 184 if (si->allocation[i][ch]) { 185 switch (scfsi[i][ch]) { 186 case 0: 187 s0 = get_leq_8_bits(mp, 6); 188 s1 = get_leq_8_bits(mp, 6); 189 s2 = get_leq_8_bits(mp, 6); 190 break; 191 case 1: 192 s0 = get_leq_8_bits(mp, 6); 193 s1 = s0; 194 s2 = get_leq_8_bits(mp, 6); 195 break; 196 case 2: 197 s0 = get_leq_8_bits(mp, 6); 198 s1 = s0; 199 s2 = s0; 200 break; 201 case 3: 202 s0 = get_leq_8_bits(mp, 6); 203 s1 = get_leq_8_bits(mp, 6); 204 s2 = s1; 205 break; 206 default: 207 assert(0); 208 } 209 } 210 si->scalefactor[i][ch][0] = s0; 211 si->scalefactor[i][ch][1] = s1; 212 si->scalefactor[i][ch][2] = s2; 213 } 214 } 215} 216 217static void 218II_step_two(PMPSTR mp, sideinfo_layer_II* si, struct frame *fr, int gr, real fraction[2][4][SBLIMIT]) 219{ 220 struct al_table2 const *alloc1 = fr->alloc; 221 int sblimit = fr->II_sblimit; 222 int jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext << 2) + 4 : fr->II_sblimit; 223 int i, ch, nch = fr->stereo; 224 double cm, r0, r1, r2; 225 226 if (jsbound > sblimit) 227 jsbound = sblimit; 228 for (i = 0; i < jsbound; ++i) { 229 short step = alloc1->bits; 230 for (ch = 0; ch < nch; ++ch) { 231 unsigned char ba = si->allocation[i][ch]; 232 if (ba) { 233 unsigned char x1 = si->scalefactor[i][ch][gr]; 234 struct al_table2 const *alloc2 = alloc1 + ba; 235 short k = alloc2->bits; 236 short d1 = alloc2->d; 237 assert( k <= 16 ); 238 k = (k <= 16) ? k : 16; 239 assert( x1 < 64 ); 240 x1 = (x1 < 64) ? x1 : 63; 241 if (d1 < 0) { 242 int v0 = getbits(mp, k); 243 int v1 = getbits(mp, k); 244 int v2 = getbits(mp, k); 245 cm = muls[k][x1]; 246 r0 = (v0 + d1) * cm; 247 r1 = (v1 + d1) * cm; 248 r2 = (v2 + d1) * cm; 249 } 250 else { 251 unsigned int idx = getbits(mp, k); 252 unsigned char *tab = grp_table_select(d1, idx); 253 unsigned char k0 = tab[0]; 254 unsigned char k1 = tab[1]; 255 unsigned char k2 = tab[2]; 256 r0 = muls[k0][x1]; 257 r1 = muls[k1][x1]; 258 r2 = muls[k2][x1]; 259 } 260 fraction[ch][0][i] = (real) r0; 261 fraction[ch][1][i] = (real) r1; 262 fraction[ch][2][i] = (real) r2; 263 } 264 else { 265 fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0; 266 } 267 } 268 alloc1 += ((size_t)1 << step); 269 } 270 271 for (i = jsbound; i < sblimit; i++) { 272 short step = alloc1->bits; 273 unsigned char ba = si->allocation[i][0]; 274 if (ba) { 275 struct al_table2 const *alloc2 = alloc1 + ba; 276 short k = alloc2->bits; 277 short d1 = alloc2->d; 278 assert( k <= 16 ); 279 k = (k <= 16) ? k : 16; 280 if (d1 < 0) { 281 int v0 = getbits(mp, k); 282 int v1 = getbits(mp, k); 283 int v2 = getbits(mp, k); 284 for (ch = 0; ch < nch; ++ch) { 285 unsigned char x1 = si->scalefactor[i][ch][gr]; 286 assert( x1 < 64 ); 287 x1 = (x1 < 64) ? x1 : 63; 288 cm = muls[k][x1]; 289 r0 = (v0 + d1) * cm; 290 r1 = (v1 + d1) * cm; 291 r2 = (v2 + d1) * cm; 292 fraction[ch][0][i] = (real) r0; 293 fraction[ch][1][i] = (real) r1; 294 fraction[ch][2][i] = (real) r2; 295 } 296 } 297 else { 298 unsigned int idx = getbits(mp, k); 299 unsigned char *tab = grp_table_select(d1, idx); 300 unsigned char k0 = tab[0]; 301 unsigned char k1 = tab[1]; 302 unsigned char k2 = tab[2]; 303 for (ch = 0; ch < nch; ++ch) { 304 unsigned char x1 = si->scalefactor[i][ch][gr]; 305 assert( x1 < 64 ); 306 x1 = (x1 < 64) ? x1 : 63; 307 r0 = muls[k0][x1]; 308 r1 = muls[k1][x1]; 309 r2 = muls[k2][x1]; 310 fraction[ch][0][i] = (real) r0; 311 fraction[ch][1][i] = (real) r1; 312 fraction[ch][2][i] = (real) r2; 313 } 314 } 315 } 316 else { 317 fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = 0.0; 318 fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0; 319 } 320 alloc1 += ((size_t)1 << step); 321 } 322 if (sblimit > fr->down_sample_sblimit) { 323 sblimit = fr->down_sample_sblimit; 324 } 325 for (ch = 0; ch < nch; ++ch) { 326 for (i = sblimit; i < SBLIMIT; ++i) { 327 fraction[ch][0][i] = fraction[ch][1][i] = fraction[ch][2][i] = 0.0; 328 } 329 } 330} 331 332static void 333II_select_table(struct frame *fr) 334{ 335 /* *INDENT-OFF* */ 336 static const int translate[3][2][16] = 337 { { { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 } , 338 { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } } , 339 { { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 } , 340 { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } } , 341 { { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 } , 342 { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } } }; 343 /* *INDENT-ON* */ 344 345 int table, sblim; 346 static const struct al_table2 *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3, alloc_4 }; 347 static const int sblims[5] = { 27, 30, 8, 12, 30 }; 348 349 if (fr->lsf) 350 table = 4; 351 else 352 table = translate[fr->sampling_frequency][2 - fr->stereo][fr->bitrate_index]; 353 sblim = sblims[table]; 354 355 fr->alloc = (struct al_table2 const *) tables[table]; 356 fr->II_sblimit = sblim; 357} 358 359 360int 361decode_layer2_sideinfo(PMPSTR mp) 362{ 363 (void) mp; 364 /* FIXME: extract side information and check values */ 365 return 0; 366} 367 368int 369decode_layer2_frame(PMPSTR mp, unsigned char *pcm_sample, int *pcm_point) 370{ 371 real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */ 372 sideinfo_layer_II si; 373 struct frame *fr = &(mp->fr); 374 int single = fr->single; 375 int i, j, clip = 0; 376 377 II_select_table(fr); 378 II_step_one(mp, &si, fr); 379 380 if (fr->stereo == 1 || single == 3) 381 single = 0; 382 383 if (single >= 0) { 384 for (i = 0; i < SCALE_BLOCK; i++) { 385 II_step_two(mp, &si, fr, i >> 2, fraction); 386 for (j = 0; j < 3; j++) { 387 clip += synth_1to1_mono(mp, fraction[single][j], pcm_sample, pcm_point); 388 } 389 } 390 } 391 else { 392 for (i = 0; i < SCALE_BLOCK; i++) { 393 II_step_two(mp, &si, fr, i >> 2, fraction); 394 for (j = 0; j < 3; j++) { 395 int p1 = *pcm_point; 396 clip += synth_1to1(mp, fraction[0][j], 0, pcm_sample, &p1); 397 clip += synth_1to1(mp, fraction[1][j], 1, pcm_sample, pcm_point); 398 } 399 } 400 } 401 402 return clip; 403} 404