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