Mercurial > libavcodec.hg
annotate liba52/parse.c @ 4301:b43bd0c56eaa libavcodec
Bug fix for crashes when SSE is used on unaligned arrays.
No measureable change in speed. This gave random crashes on Win32
and BeOS. The cause for this bug is that gcc doesn't align the
stackframe. Linux and glibc always ensure this to be true thus
this never affected Linux.
| author | banan |
|---|---|
| date | Thu, 14 Dec 2006 17:50:23 +0000 |
| parents | 0b546eab515d |
| children |
| rev | line source |
|---|---|
| 332 | 1 /* |
| 2 * parse.c | |
| 1072 | 3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org> |
| 332 | 4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> |
| 5 * | |
| 6 * This file is part of a52dec, a free ATSC A-52 stream decoder. | |
| 7 * See http://liba52.sourceforge.net/ for updates. | |
| 8 * | |
| 9 * a52dec is free software; you can redistribute it and/or modify | |
| 10 * it under the terms of the GNU General Public License as published by | |
| 11 * the Free Software Foundation; either version 2 of the License, or | |
| 12 * (at your option) any later version. | |
| 13 * | |
| 14 * a52dec is distributed in the hope that it will be useful, | |
| 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 * GNU General Public License for more details. | |
| 18 * | |
| 19 * You should have received a copy of the GNU General Public License | |
| 20 * along with this program; if not, write to the Free Software | |
|
3036
0b546eab515d
Update licensing information: The FSF changed postal address.
diego
parents:
2967
diff
changeset
|
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 332 | 22 */ |
| 23 #include "a52.h" | |
| 24 #include "a52_internal.h" | |
| 25 #include "bitstream.h" | |
| 26 #include "tables.h" | |
| 27 | |
| 1072 | 28 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus) |
| 332 | 29 /* some systems have memalign() but no declaration for it */ |
| 30 void * memalign (size_t align, size_t size); | |
| 31 #else | |
| 32 /* assume malloc alignment is sufficient */ | |
| 33 #define memalign(align,size) malloc (size) | |
| 34 #endif | |
| 35 | |
| 36 typedef struct { | |
| 1072 | 37 quantizer_t q1[2]; |
| 38 quantizer_t q2[2]; | |
| 39 quantizer_t q4; | |
| 332 | 40 int q1_ptr; |
| 41 int q2_ptr; | |
| 42 int q4_ptr; | |
| 1072 | 43 } quantizer_set_t; |
| 332 | 44 |
| 45 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}; | |
| 46 | |
| 1018 | 47 a52_state_t * a52_init (uint32_t mm_accel) |
| 332 | 48 { |
| 49 a52_state_t * state; | |
| 50 int i; | |
| 51 | |
| 1072 | 52 state = (a52_state_t *) malloc (sizeof (a52_state_t)); |
| 332 | 53 if (state == NULL) |
| 54 return NULL; | |
| 55 | |
| 1072 | 56 state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t)); |
| 332 | 57 if (state->samples == NULL) { |
| 58 free (state); | |
| 59 return NULL; | |
| 60 } | |
| 61 | |
| 62 for (i = 0; i < 256 * 12; i++) | |
| 63 state->samples[i] = 0; | |
| 64 | |
| 65 state->downmixed = 1; | |
| 66 | |
| 1072 | 67 state->lfsr_state = 1; |
| 68 | |
| 332 | 69 a52_imdct_init (mm_accel); |
| 70 | |
| 71 return state; | |
| 72 } | |
| 73 | |
| 1018 | 74 sample_t * a52_samples (a52_state_t * state) |
| 332 | 75 { |
| 76 return state->samples; | |
| 77 } | |
| 78 | |
| 1018 | 79 int a52_syncinfo (uint8_t * buf, int * flags, |
| 332 | 80 int * sample_rate, int * bit_rate) |
| 81 { | |
| 82 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112, | |
| 83 128, 160, 192, 224, 256, 320, 384, 448, | |
| 84 512, 576, 640}; | |
| 85 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01}; | |
| 86 int frmsizecod; | |
| 87 int bitrate; | |
| 88 int half; | |
| 89 int acmod; | |
| 90 | |
| 91 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */ | |
| 92 return 0; | |
| 93 | |
| 94 if (buf[5] >= 0x60) /* bsid >= 12 */ | |
| 95 return 0; | |
| 96 half = halfrate[buf[5] >> 3]; | |
| 97 | |
| 98 /* acmod, dsurmod and lfeon */ | |
| 99 acmod = buf[6] >> 5; | |
| 100 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) | | |
| 101 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0)); | |
| 102 | |
| 103 frmsizecod = buf[4] & 63; | |
| 104 if (frmsizecod >= 38) | |
| 105 return 0; | |
| 106 bitrate = rate [frmsizecod >> 1]; | |
| 107 *bit_rate = (bitrate * 1000) >> half; | |
| 108 | |
| 109 switch (buf[4] & 0xc0) { | |
| 110 case 0: | |
| 111 *sample_rate = 48000 >> half; | |
| 112 return 4 * bitrate; | |
| 113 case 0x40: | |
| 114 *sample_rate = 44100 >> half; | |
| 115 return 2 * (320 * bitrate / 147 + (frmsizecod & 1)); | |
| 116 case 0x80: | |
| 117 *sample_rate = 32000 >> half; | |
| 118 return 6 * bitrate; | |
| 119 default: | |
| 120 return 0; | |
| 121 } | |
| 122 } | |
| 123 | |
| 1018 | 124 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags, |
| 1072 | 125 level_t * level, sample_t bias) |
| 332 | 126 { |
| 1072 | 127 static level_t clev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_45DB), |
| 128 LEVEL (LEVEL_6DB), LEVEL (LEVEL_45DB) }; | |
| 2967 | 129 static level_t slev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_6DB), |
| 1072 | 130 0, LEVEL (LEVEL_6DB) }; |
| 332 | 131 int chaninfo; |
| 132 int acmod; | |
| 133 | |
| 134 state->fscod = buf[4] >> 6; | |
| 135 state->halfrate = halfrate[buf[5] >> 3]; | |
| 136 state->acmod = acmod = buf[6] >> 5; | |
| 137 | |
| 1072 | 138 a52_bitstream_set_ptr (state, buf + 6); |
| 139 bitstream_get (state, 3); /* skip acmod we already parsed */ | |
| 332 | 140 |
| 1072 | 141 if ((acmod == 2) && (bitstream_get (state, 2) == 2)) /* dsurmod */ |
| 332 | 142 acmod = A52_DOLBY; |
| 143 | |
| 1072 | 144 state->clev = state->slev = 0; |
| 145 | |
| 332 | 146 if ((acmod & 1) && (acmod != 1)) |
| 1072 | 147 state->clev = clev[bitstream_get (state, 2)]; /* cmixlev */ |
| 332 | 148 |
| 149 if (acmod & 4) | |
| 1072 | 150 state->slev = slev[bitstream_get (state, 2)]; /* surmixlev */ |
| 332 | 151 |
| 1072 | 152 state->lfeon = bitstream_get (state, 1); |
| 332 | 153 |
| 154 state->output = a52_downmix_init (acmod, *flags, level, | |
| 155 state->clev, state->slev); | |
| 156 if (state->output < 0) | |
| 157 return 1; | |
| 158 if (state->lfeon && (*flags & A52_LFE)) | |
| 159 state->output |= A52_LFE; | |
| 160 *flags = state->output; | |
| 161 /* the 2* compensates for differences in imdct */ | |
| 1072 | 162 state->dynrng = state->level = MUL_C (*level, 2); |
| 332 | 163 state->bias = bias; |
| 164 state->dynrnge = 1; | |
| 165 state->dynrngcall = NULL; | |
| 166 state->cplba.deltbae = DELTA_BIT_NONE; | |
| 167 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae = | |
| 168 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE; | |
| 169 | |
| 170 chaninfo = !acmod; | |
| 171 do { | |
| 1072 | 172 bitstream_get (state, 5); /* dialnorm */ |
| 173 if (bitstream_get (state, 1)) /* compre */ | |
| 174 bitstream_get (state, 8); /* compr */ | |
| 175 if (bitstream_get (state, 1)) /* langcode */ | |
| 176 bitstream_get (state, 8); /* langcod */ | |
| 177 if (bitstream_get (state, 1)) /* audprodie */ | |
| 178 bitstream_get (state, 7); /* mixlevel + roomtyp */ | |
| 332 | 179 } while (chaninfo--); |
| 180 | |
| 1072 | 181 bitstream_get (state, 2); /* copyrightb + origbs */ |
| 332 | 182 |
| 1072 | 183 if (bitstream_get (state, 1)) /* timecod1e */ |
| 184 bitstream_get (state, 14); /* timecod1 */ | |
| 185 if (bitstream_get (state, 1)) /* timecod2e */ | |
| 186 bitstream_get (state, 14); /* timecod2 */ | |
| 332 | 187 |
| 1072 | 188 if (bitstream_get (state, 1)) { /* addbsie */ |
| 332 | 189 int addbsil; |
| 190 | |
| 1072 | 191 addbsil = bitstream_get (state, 6); |
| 332 | 192 do { |
| 1072 | 193 bitstream_get (state, 8); /* addbsi */ |
| 332 | 194 } while (addbsil--); |
| 195 } | |
| 196 | |
| 197 return 0; | |
| 198 } | |
| 199 | |
| 1018 | 200 void a52_dynrng (a52_state_t * state, |
| 1072 | 201 level_t (* call) (level_t, void *), void * data) |
| 332 | 202 { |
| 203 state->dynrnge = 0; | |
| 204 if (call) { | |
| 205 state->dynrnge = 1; | |
| 206 state->dynrngcall = call; | |
| 207 state->dynrngdata = data; | |
| 208 } | |
| 209 } | |
| 210 | |
| 1072 | 211 static int parse_exponents (a52_state_t * state, int expstr, int ngrps, |
| 212 uint8_t exponent, uint8_t * dest) | |
| 332 | 213 { |
| 214 int exps; | |
| 215 | |
| 216 while (ngrps--) { | |
| 1072 | 217 exps = bitstream_get (state, 7); |
| 332 | 218 |
| 219 exponent += exp_1[exps]; | |
| 220 if (exponent > 24) | |
| 221 return 1; | |
| 222 | |
| 223 switch (expstr) { | |
| 224 case EXP_D45: | |
| 225 *(dest++) = exponent; | |
| 226 *(dest++) = exponent; | |
| 227 case EXP_D25: | |
| 228 *(dest++) = exponent; | |
| 229 case EXP_D15: | |
| 230 *(dest++) = exponent; | |
| 231 } | |
| 232 | |
| 233 exponent += exp_2[exps]; | |
| 234 if (exponent > 24) | |
| 235 return 1; | |
| 236 | |
| 237 switch (expstr) { | |
| 238 case EXP_D45: | |
| 239 *(dest++) = exponent; | |
| 240 *(dest++) = exponent; | |
| 241 case EXP_D25: | |
| 242 *(dest++) = exponent; | |
| 243 case EXP_D15: | |
| 244 *(dest++) = exponent; | |
| 245 } | |
| 246 | |
| 247 exponent += exp_3[exps]; | |
| 248 if (exponent > 24) | |
| 249 return 1; | |
| 250 | |
| 251 switch (expstr) { | |
| 252 case EXP_D45: | |
| 253 *(dest++) = exponent; | |
| 254 *(dest++) = exponent; | |
| 255 case EXP_D25: | |
| 256 *(dest++) = exponent; | |
| 257 case EXP_D15: | |
| 258 *(dest++) = exponent; | |
| 259 } | |
| 2967 | 260 } |
| 332 | 261 |
| 262 return 0; | |
| 263 } | |
| 264 | |
| 1072 | 265 static int parse_deltba (a52_state_t * state, int8_t * deltba) |
| 332 | 266 { |
| 267 int deltnseg, deltlen, delta, j; | |
| 268 | |
| 269 memset (deltba, 0, 50); | |
| 270 | |
| 1072 | 271 deltnseg = bitstream_get (state, 3); |
| 332 | 272 j = 0; |
| 273 do { | |
| 1072 | 274 j += bitstream_get (state, 5); |
| 275 deltlen = bitstream_get (state, 4); | |
| 276 delta = bitstream_get (state, 3); | |
| 332 | 277 delta -= (delta >= 4) ? 3 : 4; |
| 278 if (!deltlen) | |
| 279 continue; | |
| 280 if (j + deltlen >= 50) | |
| 281 return 1; | |
| 282 while (deltlen--) | |
| 283 deltba[j++] = delta; | |
| 284 } while (deltnseg--); | |
| 285 | |
| 286 return 0; | |
| 287 } | |
| 288 | |
| 289 static inline int zero_snr_offsets (int nfchans, a52_state_t * state) | |
| 290 { | |
| 291 int i; | |
| 292 | |
| 293 if ((state->csnroffst) || | |
| 294 (state->chincpl && state->cplba.bai >> 3) || /* cplinu, fsnroffst */ | |
| 295 (state->lfeon && state->lfeba.bai >> 3)) /* fsnroffst */ | |
| 296 return 0; | |
| 297 for (i = 0; i < nfchans; i++) | |
| 298 if (state->ba[i].bai >> 3) /* fsnroffst */ | |
| 299 return 0; | |
| 300 return 1; | |
| 301 } | |
| 302 | |
| 1072 | 303 static inline int16_t dither_gen (a52_state_t * state) |
| 332 | 304 { |
| 1072 | 305 int16_t nstate; |
| 332 | 306 |
| 1072 | 307 nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8); |
| 2967 | 308 |
| 1072 | 309 state->lfsr_state = (uint16_t) nstate; |
| 332 | 310 |
| 1072 | 311 return (3 * nstate) >> 2; |
| 332 | 312 } |
| 313 | |
| 1072 | 314 #ifndef LIBA52_FIXED |
| 315 #define COEFF(c,t,l,s,e) (c) = (t) * (s)[e] | |
| 316 #else | |
| 317 #define COEFF(c,_t,_l,s,e) do { \ | |
| 318 quantizer_t t = (_t); \ | |
| 319 level_t l = (_l); \ | |
| 320 int shift = e - 5; \ | |
| 321 sample_t tmp = t * (l >> 16) + ((t * (l & 0xffff)) >> 16); \ | |
| 322 if (shift >= 0) \ | |
| 323 (c) = tmp >> shift; \ | |
| 324 else \ | |
| 325 (c) = tmp << -shift; \ | |
| 326 } while (0) | |
| 327 #endif | |
| 328 | |
| 329 static void coeff_get (a52_state_t * state, sample_t * coeff, | |
| 330 expbap_t * expbap, quantizer_set_t * quant, | |
| 331 level_t level, int dither, int end) | |
| 332 | 332 { |
| 333 int i; | |
| 334 uint8_t * exp; | |
| 335 int8_t * bap; | |
| 1072 | 336 |
| 337 #ifndef LIBA52_FIXED | |
| 332 | 338 sample_t factor[25]; |
| 339 | |
| 340 for (i = 0; i <= 24; i++) | |
| 341 factor[i] = scale_factor[i] * level; | |
| 1072 | 342 #endif |
| 332 | 343 |
| 344 exp = expbap->exp; | |
| 345 bap = expbap->bap; | |
| 346 | |
| 347 for (i = 0; i < end; i++) { | |
| 348 int bapi; | |
| 349 | |
| 350 bapi = bap[i]; | |
| 351 switch (bapi) { | |
| 352 case 0: | |
| 353 if (dither) { | |
| 1072 | 354 COEFF (coeff[i], dither_gen (state), level, factor, exp[i]); |
| 332 | 355 continue; |
| 356 } else { | |
| 357 coeff[i] = 0; | |
| 358 continue; | |
| 359 } | |
| 360 | |
| 361 case -1: | |
| 1072 | 362 if (quant->q1_ptr >= 0) { |
| 363 COEFF (coeff[i], quant->q1[quant->q1_ptr--], level, | |
| 364 factor, exp[i]); | |
| 332 | 365 continue; |
| 366 } else { | |
| 367 int code; | |
| 368 | |
| 1072 | 369 code = bitstream_get (state, 5); |
| 332 | 370 |
| 1072 | 371 quant->q1_ptr = 1; |
| 372 quant->q1[0] = q_1_2[code]; | |
| 373 quant->q1[1] = q_1_1[code]; | |
| 374 COEFF (coeff[i], q_1_0[code], level, factor, exp[i]); | |
| 332 | 375 continue; |
| 376 } | |
| 377 | |
| 378 case -2: | |
| 1072 | 379 if (quant->q2_ptr >= 0) { |
| 380 COEFF (coeff[i], quant->q2[quant->q2_ptr--], level, | |
| 381 factor, exp[i]); | |
| 332 | 382 continue; |
| 383 } else { | |
| 384 int code; | |
| 385 | |
| 1072 | 386 code = bitstream_get (state, 7); |
| 332 | 387 |
| 1072 | 388 quant->q2_ptr = 1; |
| 389 quant->q2[0] = q_2_2[code]; | |
| 390 quant->q2[1] = q_2_1[code]; | |
| 391 COEFF (coeff[i], q_2_0[code], level, factor, exp[i]); | |
| 332 | 392 continue; |
| 393 } | |
| 394 | |
| 395 case 3: | |
| 1072 | 396 COEFF (coeff[i], q_3[bitstream_get (state, 3)], level, |
| 397 factor, exp[i]); | |
| 332 | 398 continue; |
| 399 | |
| 400 case -3: | |
| 1072 | 401 if (quant->q4_ptr == 0) { |
| 402 quant->q4_ptr = -1; | |
| 403 COEFF (coeff[i], quant->q4, level, factor, exp[i]); | |
| 332 | 404 continue; |
| 405 } else { | |
| 406 int code; | |
| 407 | |
| 1072 | 408 code = bitstream_get (state, 7); |
| 332 | 409 |
| 1072 | 410 quant->q4_ptr = 0; |
| 411 quant->q4 = q_4_1[code]; | |
| 412 COEFF (coeff[i], q_4_0[code], level, factor, exp[i]); | |
| 332 | 413 continue; |
| 414 } | |
| 415 | |
| 416 case 4: | |
| 1072 | 417 COEFF (coeff[i], q_5[bitstream_get (state, 4)], level, |
| 418 factor, exp[i]); | |
| 332 | 419 continue; |
| 420 | |
| 421 default: | |
| 1072 | 422 COEFF (coeff[i], bitstream_get_2 (state, bapi) << (16 - bapi), |
| 423 level, factor, exp[i]); | |
| 332 | 424 } |
| 425 } | |
| 426 } | |
| 427 | |
| 428 static void coeff_get_coupling (a52_state_t * state, int nfchans, | |
| 1072 | 429 level_t * coeff, sample_t (* samples)[256], |
| 430 quantizer_set_t * quant, uint8_t dithflag[5]) | |
| 332 | 431 { |
| 432 int cplbndstrc, bnd, i, i_end, ch; | |
| 433 uint8_t * exp; | |
| 434 int8_t * bap; | |
| 1072 | 435 level_t cplco[5]; |
| 332 | 436 |
| 437 exp = state->cpl_expbap.exp; | |
| 438 bap = state->cpl_expbap.bap; | |
| 439 bnd = 0; | |
| 440 cplbndstrc = state->cplbndstrc; | |
| 441 i = state->cplstrtmant; | |
| 442 while (i < state->cplendmant) { | |
| 443 i_end = i + 12; | |
| 444 while (cplbndstrc & 1) { | |
| 445 cplbndstrc >>= 1; | |
| 446 i_end += 12; | |
| 447 } | |
| 448 cplbndstrc >>= 1; | |
| 449 for (ch = 0; ch < nfchans; ch++) | |
| 1072 | 450 cplco[ch] = MUL_L (state->cplco[ch][bnd], coeff[ch]); |
| 332 | 451 bnd++; |
| 452 | |
| 453 while (i < i_end) { | |
| 1072 | 454 quantizer_t cplcoeff; |
| 332 | 455 int bapi; |
| 456 | |
| 457 bapi = bap[i]; | |
| 458 switch (bapi) { | |
| 459 case 0: | |
| 460 for (ch = 0; ch < nfchans; ch++) | |
| 461 if ((state->chincpl >> ch) & 1) { | |
| 462 if (dithflag[ch]) | |
| 1072 | 463 #ifndef LIBA52_FIXED |
| 464 samples[ch][i] = (scale_factor[exp[i]] * | |
| 465 cplco[ch] * dither_gen (state)); | |
| 466 #else | |
| 467 COEFF (samples[ch][i], dither_gen (state), | |
| 468 cplco[ch], scale_factor, exp[i]); | |
| 469 #endif | |
| 332 | 470 else |
| 471 samples[ch][i] = 0; | |
| 472 } | |
| 473 i++; | |
| 474 continue; | |
| 475 | |
| 476 case -1: | |
| 1072 | 477 if (quant->q1_ptr >= 0) { |
| 478 cplcoeff = quant->q1[quant->q1_ptr--]; | |
| 332 | 479 break; |
| 480 } else { | |
| 481 int code; | |
| 482 | |
| 1072 | 483 code = bitstream_get (state, 5); |
| 332 | 484 |
| 1072 | 485 quant->q1_ptr = 1; |
| 486 quant->q1[0] = q_1_2[code]; | |
| 487 quant->q1[1] = q_1_1[code]; | |
| 332 | 488 cplcoeff = q_1_0[code]; |
| 489 break; | |
| 490 } | |
| 491 | |
| 492 case -2: | |
| 1072 | 493 if (quant->q2_ptr >= 0) { |
| 494 cplcoeff = quant->q2[quant->q2_ptr--]; | |
| 332 | 495 break; |
| 496 } else { | |
| 497 int code; | |
| 498 | |
| 1072 | 499 code = bitstream_get (state, 7); |
| 332 | 500 |
| 1072 | 501 quant->q2_ptr = 1; |
| 502 quant->q2[0] = q_2_2[code]; | |
| 503 quant->q2[1] = q_2_1[code]; | |
| 332 | 504 cplcoeff = q_2_0[code]; |
| 505 break; | |
| 506 } | |
| 507 | |
| 508 case 3: | |
| 1072 | 509 cplcoeff = q_3[bitstream_get (state, 3)]; |
| 332 | 510 break; |
| 511 | |
| 512 case -3: | |
| 1072 | 513 if (quant->q4_ptr == 0) { |
| 514 quant->q4_ptr = -1; | |
| 515 cplcoeff = quant->q4; | |
| 332 | 516 break; |
| 517 } else { | |
| 518 int code; | |
| 519 | |
| 1072 | 520 code = bitstream_get (state, 7); |
| 332 | 521 |
| 1072 | 522 quant->q4_ptr = 0; |
| 523 quant->q4 = q_4_1[code]; | |
| 332 | 524 cplcoeff = q_4_0[code]; |
| 525 break; | |
| 526 } | |
| 527 | |
| 528 case 4: | |
| 1072 | 529 cplcoeff = q_5[bitstream_get (state, 4)]; |
| 332 | 530 break; |
| 531 | |
| 532 default: | |
| 1072 | 533 cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi); |
| 332 | 534 } |
| 1072 | 535 #ifndef LIBA52_FIXED |
| 332 | 536 cplcoeff *= scale_factor[exp[i]]; |
| 1072 | 537 #endif |
| 332 | 538 for (ch = 0; ch < nfchans; ch++) |
| 1072 | 539 if ((state->chincpl >> ch) & 1) |
| 540 #ifndef LIBA52_FIXED | |
| 332 | 541 samples[ch][i] = cplcoeff * cplco[ch]; |
| 1072 | 542 #else |
| 543 COEFF (samples[ch][i], cplcoeff, cplco[ch], | |
| 544 scale_factor, exp[i]); | |
| 545 #endif | |
| 332 | 546 i++; |
| 547 } | |
| 548 } | |
| 549 } | |
| 550 | |
| 1018 | 551 int a52_block (a52_state_t * state) |
| 332 | 552 { |
| 553 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2}; | |
| 554 static int rematrix_band[4] = {25, 37, 61, 253}; | |
| 555 int i, nfchans, chaninfo; | |
| 556 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl; | |
| 557 uint8_t blksw[5], dithflag[5]; | |
| 1072 | 558 level_t coeff[5]; |
| 332 | 559 int chanbias; |
| 1072 | 560 quantizer_set_t quant; |
| 332 | 561 sample_t * samples; |
| 562 | |
| 563 nfchans = nfchans_tbl[state->acmod]; | |
| 564 | |
| 565 for (i = 0; i < nfchans; i++) | |
| 1072 | 566 blksw[i] = bitstream_get (state, 1); |
| 332 | 567 |
| 568 for (i = 0; i < nfchans; i++) | |
| 1072 | 569 dithflag[i] = bitstream_get (state, 1); |
| 332 | 570 |
| 571 chaninfo = !state->acmod; | |
| 572 do { | |
| 1072 | 573 if (bitstream_get (state, 1)) { /* dynrnge */ |
| 332 | 574 int dynrng; |
| 575 | |
| 1072 | 576 dynrng = bitstream_get_2 (state, 8); |
| 332 | 577 if (state->dynrnge) { |
| 1072 | 578 level_t range; |
| 332 | 579 |
| 1072 | 580 #if !defined(LIBA52_FIXED) |
| 332 | 581 range = ((((dynrng & 0x1f) | 0x20) << 13) * |
| 582 scale_factor[3 - (dynrng >> 5)]); | |
| 1072 | 583 #else |
| 584 range = ((dynrng & 0x1f) | 0x20) << (21 + (dynrng >> 5)); | |
| 585 #endif | |
| 332 | 586 if (state->dynrngcall) |
| 587 range = state->dynrngcall (range, state->dynrngdata); | |
| 1072 | 588 state->dynrng = MUL_L (state->level, range); |
| 332 | 589 } |
| 590 } | |
| 591 } while (chaninfo--); | |
| 592 | |
| 1072 | 593 if (bitstream_get (state, 1)) { /* cplstre */ |
| 332 | 594 state->chincpl = 0; |
| 1072 | 595 if (bitstream_get (state, 1)) { /* cplinu */ |
| 332 | 596 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44, |
| 597 45, 45, 46, 46, 47, 47, 48, 48}; | |
| 598 int cplbegf; | |
| 599 int cplendf; | |
| 600 int ncplsubnd; | |
| 601 | |
| 602 for (i = 0; i < nfchans; i++) | |
| 1072 | 603 state->chincpl |= bitstream_get (state, 1) << i; |
| 332 | 604 switch (state->acmod) { |
| 605 case 0: case 1: | |
| 606 return 1; | |
| 607 case 2: | |
| 1072 | 608 state->phsflginu = bitstream_get (state, 1); |
| 332 | 609 } |
| 1072 | 610 cplbegf = bitstream_get (state, 4); |
| 611 cplendf = bitstream_get (state, 4); | |
| 332 | 612 |
| 613 if (cplendf + 3 - cplbegf < 0) | |
| 614 return 1; | |
| 615 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf; | |
| 616 state->cplstrtbnd = bndtab[cplbegf]; | |
| 617 state->cplstrtmant = cplbegf * 12 + 37; | |
| 618 state->cplendmant = cplendf * 12 + 73; | |
| 619 | |
| 620 state->cplbndstrc = 0; | |
| 621 for (i = 0; i < ncplsubnd - 1; i++) | |
| 1072 | 622 if (bitstream_get (state, 1)) { |
| 332 | 623 state->cplbndstrc |= 1 << i; |
| 624 state->ncplbnd--; | |
| 625 } | |
| 626 } | |
| 627 } | |
| 628 | |
| 629 if (state->chincpl) { /* cplinu */ | |
| 630 int j, cplcoe; | |
| 631 | |
| 632 cplcoe = 0; | |
| 633 for (i = 0; i < nfchans; i++) | |
| 634 if ((state->chincpl) >> i & 1) | |
| 1072 | 635 if (bitstream_get (state, 1)) { /* cplcoe */ |
| 332 | 636 int mstrcplco, cplcoexp, cplcomant; |
| 637 | |
| 638 cplcoe = 1; | |
| 1072 | 639 mstrcplco = 3 * bitstream_get (state, 2); |
| 332 | 640 for (j = 0; j < state->ncplbnd; j++) { |
| 1072 | 641 cplcoexp = bitstream_get (state, 4); |
| 642 cplcomant = bitstream_get (state, 4); | |
| 332 | 643 if (cplcoexp == 15) |
| 644 cplcomant <<= 14; | |
| 645 else | |
| 646 cplcomant = (cplcomant | 0x10) << 13; | |
| 1072 | 647 #ifndef LIBA52_FIXED |
| 332 | 648 state->cplco[i][j] = |
| 649 cplcomant * scale_factor[cplcoexp + mstrcplco]; | |
| 1072 | 650 #else |
| 651 state->cplco[i][j] = (cplcomant << 11) >> (cplcoexp + mstrcplco); | |
| 652 #endif | |
| 653 | |
| 332 | 654 } |
| 655 } | |
| 656 if ((state->acmod == 2) && state->phsflginu && cplcoe) | |
| 657 for (j = 0; j < state->ncplbnd; j++) | |
| 1072 | 658 if (bitstream_get (state, 1)) /* phsflg */ |
| 332 | 659 state->cplco[1][j] = -state->cplco[1][j]; |
| 660 } | |
| 661 | |
| 1072 | 662 if ((state->acmod == 2) && (bitstream_get (state, 1))) { /* rematstr */ |
| 332 | 663 int end; |
| 664 | |
| 665 state->rematflg = 0; | |
| 666 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */ | |
| 667 i = 0; | |
| 668 do | |
| 1072 | 669 state->rematflg |= bitstream_get (state, 1) << i; |
| 332 | 670 while (rematrix_band[i++] < end); |
| 671 } | |
| 672 | |
| 673 cplexpstr = EXP_REUSE; | |
| 674 lfeexpstr = EXP_REUSE; | |
| 675 if (state->chincpl) /* cplinu */ | |
| 1072 | 676 cplexpstr = bitstream_get (state, 2); |
| 332 | 677 for (i = 0; i < nfchans; i++) |
| 1072 | 678 chexpstr[i] = bitstream_get (state, 2); |
| 2967 | 679 if (state->lfeon) |
| 1072 | 680 lfeexpstr = bitstream_get (state, 1); |
| 332 | 681 |
| 682 for (i = 0; i < nfchans; i++) | |
| 683 if (chexpstr[i] != EXP_REUSE) { | |
| 684 if ((state->chincpl >> i) & 1) | |
| 685 state->endmant[i] = state->cplstrtmant; | |
| 686 else { | |
| 687 int chbwcod; | |
| 688 | |
| 1072 | 689 chbwcod = bitstream_get (state, 6); |
| 332 | 690 if (chbwcod > 60) |
| 691 return 1; | |
| 692 state->endmant[i] = chbwcod * 3 + 73; | |
| 693 } | |
| 694 } | |
| 695 | |
| 696 do_bit_alloc = 0; | |
| 697 | |
| 698 if (cplexpstr != EXP_REUSE) { | |
| 699 int cplabsexp, ncplgrps; | |
| 700 | |
| 701 do_bit_alloc = 64; | |
| 702 ncplgrps = ((state->cplendmant - state->cplstrtmant) / | |
| 703 (3 << (cplexpstr - 1))); | |
| 1072 | 704 cplabsexp = bitstream_get (state, 4) << 1; |
| 705 if (parse_exponents (state, cplexpstr, ncplgrps, cplabsexp, | |
| 332 | 706 state->cpl_expbap.exp + state->cplstrtmant)) |
| 707 return 1; | |
| 708 } | |
| 709 for (i = 0; i < nfchans; i++) | |
| 710 if (chexpstr[i] != EXP_REUSE) { | |
| 711 int grp_size, nchgrps; | |
| 712 | |
| 713 do_bit_alloc |= 1 << i; | |
| 714 grp_size = 3 << (chexpstr[i] - 1); | |
| 715 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size; | |
| 1072 | 716 state->fbw_expbap[i].exp[0] = bitstream_get (state, 4); |
| 717 if (parse_exponents (state, chexpstr[i], nchgrps, | |
| 332 | 718 state->fbw_expbap[i].exp[0], |
| 719 state->fbw_expbap[i].exp + 1)) | |
| 720 return 1; | |
| 1072 | 721 bitstream_get (state, 2); /* gainrng */ |
| 332 | 722 } |
| 723 if (lfeexpstr != EXP_REUSE) { | |
| 724 do_bit_alloc |= 32; | |
| 1072 | 725 state->lfe_expbap.exp[0] = bitstream_get (state, 4); |
| 726 if (parse_exponents (state, lfeexpstr, 2, state->lfe_expbap.exp[0], | |
| 332 | 727 state->lfe_expbap.exp + 1)) |
| 728 return 1; | |
| 729 } | |
| 730 | |
| 1072 | 731 if (bitstream_get (state, 1)) { /* baie */ |
| 732 do_bit_alloc = 127; | |
| 733 state->bai = bitstream_get (state, 11); | |
| 332 | 734 } |
| 1072 | 735 if (bitstream_get (state, 1)) { /* snroffste */ |
| 736 do_bit_alloc = 127; | |
| 737 state->csnroffst = bitstream_get (state, 6); | |
| 332 | 738 if (state->chincpl) /* cplinu */ |
| 1072 | 739 state->cplba.bai = bitstream_get (state, 7); |
| 332 | 740 for (i = 0; i < nfchans; i++) |
| 1072 | 741 state->ba[i].bai = bitstream_get (state, 7); |
| 332 | 742 if (state->lfeon) |
| 1072 | 743 state->lfeba.bai = bitstream_get (state, 7); |
| 332 | 744 } |
| 1072 | 745 if ((state->chincpl) && (bitstream_get (state, 1))) { /* cplleake */ |
| 332 | 746 do_bit_alloc |= 64; |
| 1072 | 747 state->cplfleak = 9 - bitstream_get (state, 3); |
| 748 state->cplsleak = 9 - bitstream_get (state, 3); | |
| 332 | 749 } |
| 750 | |
| 1072 | 751 if (bitstream_get (state, 1)) { /* deltbaie */ |
| 752 do_bit_alloc = 127; | |
| 332 | 753 if (state->chincpl) /* cplinu */ |
| 1072 | 754 state->cplba.deltbae = bitstream_get (state, 2); |
| 332 | 755 for (i = 0; i < nfchans; i++) |
| 1072 | 756 state->ba[i].deltbae = bitstream_get (state, 2); |
| 332 | 757 if (state->chincpl && /* cplinu */ |
| 758 (state->cplba.deltbae == DELTA_BIT_NEW) && | |
| 1072 | 759 parse_deltba (state, state->cplba.deltba)) |
| 332 | 760 return 1; |
| 761 for (i = 0; i < nfchans; i++) | |
| 762 if ((state->ba[i].deltbae == DELTA_BIT_NEW) && | |
| 1072 | 763 parse_deltba (state, state->ba[i].deltba)) |
| 332 | 764 return 1; |
| 765 } | |
| 766 | |
| 767 if (do_bit_alloc) { | |
| 768 if (zero_snr_offsets (nfchans, state)) { | |
| 769 memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap)); | |
| 770 for (i = 0; i < nfchans; i++) | |
| 771 memset (state->fbw_expbap[i].bap, 0, | |
| 772 sizeof (state->fbw_expbap[i].bap)); | |
| 773 memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap)); | |
| 774 } else { | |
| 775 if (state->chincpl && (do_bit_alloc & 64)) /* cplinu */ | |
| 776 a52_bit_allocate (state, &state->cplba, state->cplstrtbnd, | |
| 777 state->cplstrtmant, state->cplendmant, | |
| 778 state->cplfleak << 8, state->cplsleak << 8, | |
| 779 &state->cpl_expbap); | |
| 780 for (i = 0; i < nfchans; i++) | |
| 781 if (do_bit_alloc & (1 << i)) | |
| 782 a52_bit_allocate (state, state->ba + i, 0, 0, | |
| 783 state->endmant[i], 0, 0, | |
| 784 state->fbw_expbap +i); | |
| 785 if (state->lfeon && (do_bit_alloc & 32)) { | |
| 786 state->lfeba.deltbae = DELTA_BIT_NONE; | |
| 787 a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0, | |
| 788 &state->lfe_expbap); | |
| 789 } | |
| 790 } | |
| 791 } | |
| 792 | |
| 1072 | 793 if (bitstream_get (state, 1)) { /* skiple */ |
| 794 i = bitstream_get (state, 9); /* skipl */ | |
| 332 | 795 while (i--) |
| 1072 | 796 bitstream_get (state, 8); |
| 332 | 797 } |
| 798 | |
| 799 samples = state->samples; | |
| 800 if (state->output & A52_LFE) | |
| 801 samples += 256; /* shift for LFE channel */ | |
| 802 | |
| 803 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output, | |
| 804 state->dynrng, state->clev, state->slev); | |
| 805 | |
| 1072 | 806 quant.q1_ptr = quant.q2_ptr = quant.q4_ptr = -1; |
| 332 | 807 done_cpl = 0; |
| 808 | |
| 809 for (i = 0; i < nfchans; i++) { | |
| 810 int j; | |
| 811 | |
| 1072 | 812 coeff_get (state, samples + 256 * i, state->fbw_expbap +i, &quant, |
| 332 | 813 coeff[i], dithflag[i], state->endmant[i]); |
| 814 | |
| 815 if ((state->chincpl >> i) & 1) { | |
| 816 if (!done_cpl) { | |
| 817 done_cpl = 1; | |
| 818 coeff_get_coupling (state, nfchans, coeff, | |
| 1072 | 819 (sample_t (*)[256])samples, &quant, |
| 332 | 820 dithflag); |
| 821 } | |
| 822 j = state->cplendmant; | |
| 823 } else | |
| 824 j = state->endmant[i]; | |
| 825 do | |
| 826 (samples + 256 * i)[j] = 0; | |
| 827 while (++j < 256); | |
| 828 } | |
| 829 | |
| 830 if (state->acmod == 2) { | |
| 831 int j, end, band, rematflg; | |
| 832 | |
| 833 end = ((state->endmant[0] < state->endmant[1]) ? | |
| 834 state->endmant[0] : state->endmant[1]); | |
| 835 | |
| 836 i = 0; | |
| 837 j = 13; | |
| 838 rematflg = state->rematflg; | |
| 839 do { | |
| 840 if (! (rematflg & 1)) { | |
| 841 rematflg >>= 1; | |
| 842 j = rematrix_band[i++]; | |
| 843 continue; | |
| 844 } | |
| 845 rematflg >>= 1; | |
| 846 band = rematrix_band[i++]; | |
| 847 if (band > end) | |
| 848 band = end; | |
| 849 do { | |
| 850 sample_t tmp0, tmp1; | |
| 851 | |
| 852 tmp0 = samples[j]; | |
| 853 tmp1 = (samples+256)[j]; | |
| 854 samples[j] = tmp0 + tmp1; | |
| 855 (samples+256)[j] = tmp0 - tmp1; | |
| 856 } while (++j < band); | |
| 857 } while (j < end); | |
| 858 } | |
| 859 | |
| 860 if (state->lfeon) { | |
| 861 if (state->output & A52_LFE) { | |
| 1072 | 862 coeff_get (state, samples - 256, &state->lfe_expbap, &quant, |
| 332 | 863 state->dynrng, 0, 7); |
| 864 for (i = 7; i < 256; i++) | |
| 865 (samples-256)[i] = 0; | |
| 866 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias); | |
| 867 } else { | |
| 868 /* just skip the LFE coefficients */ | |
| 1072 | 869 coeff_get (state, samples + 1280, &state->lfe_expbap, &quant, |
| 332 | 870 0, 0, 7); |
| 871 } | |
| 872 } | |
| 873 | |
| 874 i = 0; | |
| 875 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans) | |
| 876 for (i = 1; i < nfchans; i++) | |
| 877 if (blksw[i] != blksw[0]) | |
| 878 break; | |
| 879 | |
| 880 if (i < nfchans) { | |
| 881 if (state->downmixed) { | |
| 882 state->downmixed = 0; | |
| 883 a52_upmix (samples + 1536, state->acmod, state->output); | |
| 884 } | |
| 885 | |
| 886 for (i = 0; i < nfchans; i++) { | |
| 887 sample_t bias; | |
| 888 | |
| 889 bias = 0; | |
| 890 if (!(chanbias & (1 << i))) | |
| 891 bias = state->bias; | |
| 892 | |
| 893 if (coeff[i]) { | |
| 894 if (blksw[i]) | |
| 895 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i, | |
| 896 bias); | |
| 2967 | 897 else |
| 332 | 898 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i, |
| 899 bias); | |
| 900 } else { | |
| 901 int j; | |
| 902 | |
| 903 for (j = 0; j < 256; j++) | |
| 904 (samples + 256 * i)[j] = bias; | |
| 905 } | |
| 906 } | |
| 907 | |
| 908 a52_downmix (samples, state->acmod, state->output, state->bias, | |
| 909 state->clev, state->slev); | |
| 910 } else { | |
| 911 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK]; | |
| 912 | |
| 913 a52_downmix (samples, state->acmod, state->output, 0, | |
| 914 state->clev, state->slev); | |
| 915 | |
| 916 if (!state->downmixed) { | |
| 917 state->downmixed = 1; | |
| 918 a52_downmix (samples + 1536, state->acmod, state->output, 0, | |
| 919 state->clev, state->slev); | |
| 920 } | |
| 921 | |
| 922 if (blksw[0]) | |
| 923 for (i = 0; i < nfchans; i++) | |
| 924 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i, | |
| 925 state->bias); | |
| 2967 | 926 else |
| 332 | 927 for (i = 0; i < nfchans; i++) |
| 928 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i, | |
| 929 state->bias); | |
| 930 } | |
| 931 | |
| 932 return 0; | |
| 933 } | |
| 934 | |
| 1018 | 935 void a52_free (a52_state_t * state) |
| 332 | 936 { |
| 937 free (state->samples); | |
| 938 free (state); | |
| 939 } |
