00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "a52.h"
00024 #include "a52_internal.h"
00025 #include "bitstream.h"
00026 #include "tables.h"
00027
00028 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
00029
00030 void * memalign (size_t align, size_t size);
00031 #else
00032
00033 #define memalign(align,size) malloc (size)
00034 #endif
00035
00036 typedef struct {
00037 quantizer_t q1[2];
00038 quantizer_t q2[2];
00039 quantizer_t q4;
00040 int q1_ptr;
00041 int q2_ptr;
00042 int q4_ptr;
00043 } quantizer_set_t;
00044
00045 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
00046
00047 a52_state_t * a52_init (uint32_t mm_accel)
00048 {
00049 a52_state_t * state;
00050 int i;
00051
00052 state = (a52_state_t *) malloc (sizeof (a52_state_t));
00053 if (state == NULL)
00054 return NULL;
00055
00056 state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t));
00057 if (state->samples == NULL) {
00058 free (state);
00059 return NULL;
00060 }
00061
00062 for (i = 0; i < 256 * 12; i++)
00063 state->samples[i] = 0;
00064
00065 state->downmixed = 1;
00066
00067 state->lfsr_state = 1;
00068
00069 a52_imdct_init (mm_accel);
00070
00071 return state;
00072 }
00073
00074 sample_t * a52_samples (a52_state_t * state)
00075 {
00076 return state->samples;
00077 }
00078
00079 int a52_syncinfo (uint8_t * buf, int * flags,
00080 int * sample_rate, int * bit_rate)
00081 {
00082 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
00083 128, 160, 192, 224, 256, 320, 384, 448,
00084 512, 576, 640};
00085 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
00086 int frmsizecod;
00087 int bitrate;
00088 int half;
00089 int acmod;
00090
00091 if ((buf[0] != 0x0b) || (buf[1] != 0x77))
00092 return 0;
00093
00094 if (buf[5] >= 0x60)
00095 return 0;
00096 half = halfrate[buf[5] >> 3];
00097
00098
00099 acmod = buf[6] >> 5;
00100 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
00101 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
00102
00103 frmsizecod = buf[4] & 63;
00104 if (frmsizecod >= 38)
00105 return 0;
00106 bitrate = rate [frmsizecod >> 1];
00107 *bit_rate = (bitrate * 1000) >> half;
00108
00109 switch (buf[4] & 0xc0) {
00110 case 0:
00111 *sample_rate = 48000 >> half;
00112 return 4 * bitrate;
00113 case 0x40:
00114 *sample_rate = 44100 >> half;
00115 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
00116 case 0x80:
00117 *sample_rate = 32000 >> half;
00118 return 6 * bitrate;
00119 default:
00120 return 0;
00121 }
00122 }
00123
00124 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
00125 level_t * level, sample_t bias)
00126 {
00127 static level_t clev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_45DB),
00128 LEVEL (LEVEL_6DB), LEVEL (LEVEL_45DB) };
00129 static level_t slev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_6DB),
00130 0, LEVEL (LEVEL_6DB) };
00131 int chaninfo;
00132 int acmod;
00133
00134 state->fscod = buf[4] >> 6;
00135 state->halfrate = halfrate[buf[5] >> 3];
00136 state->acmod = acmod = buf[6] >> 5;
00137
00138 a52_bitstream_set_ptr (state, buf + 6);
00139 bitstream_get (state, 3);
00140
00141 if ((acmod == 2) && (bitstream_get (state, 2) == 2))
00142 acmod = A52_DOLBY;
00143
00144 state->clev = state->slev = 0;
00145
00146 if ((acmod & 1) && (acmod != 1))
00147 state->clev = clev[bitstream_get (state, 2)];
00148
00149 if (acmod & 4)
00150 state->slev = slev[bitstream_get (state, 2)];
00151
00152 state->lfeon = bitstream_get (state, 1);
00153
00154 state->output = a52_downmix_init (acmod, *flags, level,
00155 state->clev, state->slev);
00156 if (state->output < 0)
00157 return 1;
00158 if (state->lfeon && (*flags & A52_LFE))
00159 state->output |= A52_LFE;
00160 *flags = state->output;
00161
00162 state->dynrng = state->level = MUL_C (*level, 2);
00163 state->bias = bias;
00164 state->dynrnge = 1;
00165 state->dynrngcall = NULL;
00166 state->cplba.deltbae = DELTA_BIT_NONE;
00167 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
00168 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
00169
00170 chaninfo = !acmod;
00171 do {
00172 bitstream_get (state, 5);
00173 if (bitstream_get (state, 1))
00174 bitstream_get (state, 8);
00175 if (bitstream_get (state, 1))
00176 bitstream_get (state, 8);
00177 if (bitstream_get (state, 1))
00178 bitstream_get (state, 7);
00179 } while (chaninfo--);
00180
00181 bitstream_get (state, 2);
00182
00183 if (bitstream_get (state, 1))
00184 bitstream_get (state, 14);
00185 if (bitstream_get (state, 1))
00186 bitstream_get (state, 14);
00187
00188 if (bitstream_get (state, 1)) {
00189 int addbsil;
00190
00191 addbsil = bitstream_get (state, 6);
00192 do {
00193 bitstream_get (state, 8);
00194 } while (addbsil--);
00195 }
00196
00197 return 0;
00198 }
00199
00200 void a52_dynrng (a52_state_t * state,
00201 level_t (* call) (level_t, void *), void * data)
00202 {
00203 state->dynrnge = 0;
00204 if (call) {
00205 state->dynrnge = 1;
00206 state->dynrngcall = call;
00207 state->dynrngdata = data;
00208 }
00209 }
00210
00211 static int parse_exponents (a52_state_t * state, int expstr, int ngrps,
00212 uint8_t exponent, uint8_t * dest)
00213 {
00214 int exps;
00215
00216 while (ngrps--) {
00217 exps = bitstream_get (state, 7);
00218
00219 exponent += exp_1[exps];
00220 if (exponent > 24)
00221 return 1;
00222
00223 switch (expstr) {
00224 case EXP_D45:
00225 *(dest++) = exponent;
00226 *(dest++) = exponent;
00227 case EXP_D25:
00228 *(dest++) = exponent;
00229 case EXP_D15:
00230 *(dest++) = exponent;
00231 }
00232
00233 exponent += exp_2[exps];
00234 if (exponent > 24)
00235 return 1;
00236
00237 switch (expstr) {
00238 case EXP_D45:
00239 *(dest++) = exponent;
00240 *(dest++) = exponent;
00241 case EXP_D25:
00242 *(dest++) = exponent;
00243 case EXP_D15:
00244 *(dest++) = exponent;
00245 }
00246
00247 exponent += exp_3[exps];
00248 if (exponent > 24)
00249 return 1;
00250
00251 switch (expstr) {
00252 case EXP_D45:
00253 *(dest++) = exponent;
00254 *(dest++) = exponent;
00255 case EXP_D25:
00256 *(dest++) = exponent;
00257 case EXP_D15:
00258 *(dest++) = exponent;
00259 }
00260 }
00261
00262 return 0;
00263 }
00264
00265 static int parse_deltba (a52_state_t * state, int8_t * deltba)
00266 {
00267 int deltnseg, deltlen, delta, j;
00268
00269 memset (deltba, 0, 50);
00270
00271 deltnseg = bitstream_get (state, 3);
00272 j = 0;
00273 do {
00274 j += bitstream_get (state, 5);
00275 deltlen = bitstream_get (state, 4);
00276 delta = bitstream_get (state, 3);
00277 delta -= (delta >= 4) ? 3 : 4;
00278 if (!deltlen)
00279 continue;
00280 if (j + deltlen >= 50)
00281 return 1;
00282 while (deltlen--)
00283 deltba[j++] = delta;
00284 } while (deltnseg--);
00285
00286 return 0;
00287 }
00288
00289 static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
00290 {
00291 int i;
00292
00293 if ((state->csnroffst) ||
00294 (state->chincpl && state->cplba.bai >> 3) ||
00295 (state->lfeon && state->lfeba.bai >> 3))
00296 return 0;
00297 for (i = 0; i < nfchans; i++)
00298 if (state->ba[i].bai >> 3)
00299 return 0;
00300 return 1;
00301 }
00302
00303 static inline int16_t dither_gen (a52_state_t * state)
00304 {
00305 int16_t nstate;
00306
00307 nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8);
00308
00309 state->lfsr_state = (uint16_t) nstate;
00310
00311 return (3 * nstate) >> 2;
00312 }
00313
00314 #ifndef LIBA52_FIXED
00315 #define COEFF(c,t,l,s,e) (c) = (t) * (s)[e]
00316 #else
00317 #define COEFF(c,_t,_l,s,e) do { \
00318 quantizer_t t = (_t); \
00319 level_t l = (_l); \
00320 int shift = e - 5; \
00321 sample_t tmp = t * (l >> 16) + ((t * (l & 0xffff)) >> 16); \
00322 if (shift >= 0) \
00323 (c) = tmp >> shift; \
00324 else \
00325 (c) = tmp << -shift; \
00326 } while (0)
00327 #endif
00328
00329 static void coeff_get (a52_state_t * state, sample_t * coeff,
00330 expbap_t * expbap, quantizer_set_t * quant,
00331 level_t level, int dither, int end)
00332 {
00333 int i;
00334 uint8_t * exp;
00335 int8_t * bap;
00336
00337 #ifndef LIBA52_FIXED
00338 sample_t factor[25];
00339
00340 for (i = 0; i <= 24; i++)
00341 factor[i] = scale_factor[i] * level;
00342 #endif
00343
00344 exp = expbap->exp;
00345 bap = expbap->bap;
00346
00347 for (i = 0; i < end; i++) {
00348 int bapi;
00349
00350 bapi = bap[i];
00351 switch (bapi) {
00352 case 0:
00353 if (dither) {
00354 COEFF (coeff[i], dither_gen (state), level, factor, exp[i]);
00355 continue;
00356 } else {
00357 coeff[i] = 0;
00358 continue;
00359 }
00360
00361 case -1:
00362 if (quant->q1_ptr >= 0) {
00363 COEFF (coeff[i], quant->q1[quant->q1_ptr--], level,
00364 factor, exp[i]);
00365 continue;
00366 } else {
00367 int code;
00368
00369 code = bitstream_get (state, 5);
00370
00371 quant->q1_ptr = 1;
00372 quant->q1[0] = q_1_2[code];
00373 quant->q1[1] = q_1_1[code];
00374 COEFF (coeff[i], q_1_0[code], level, factor, exp[i]);
00375 continue;
00376 }
00377
00378 case -2:
00379 if (quant->q2_ptr >= 0) {
00380 COEFF (coeff[i], quant->q2[quant->q2_ptr--], level,
00381 factor, exp[i]);
00382 continue;
00383 } else {
00384 int code;
00385
00386 code = bitstream_get (state, 7);
00387
00388 quant->q2_ptr = 1;
00389 quant->q2[0] = q_2_2[code];
00390 quant->q2[1] = q_2_1[code];
00391 COEFF (coeff[i], q_2_0[code], level, factor, exp[i]);
00392 continue;
00393 }
00394
00395 case 3:
00396 COEFF (coeff[i], q_3[bitstream_get (state, 3)], level,
00397 factor, exp[i]);
00398 continue;
00399
00400 case -3:
00401 if (quant->q4_ptr == 0) {
00402 quant->q4_ptr = -1;
00403 COEFF (coeff[i], quant->q4, level, factor, exp[i]);
00404 continue;
00405 } else {
00406 int code;
00407
00408 code = bitstream_get (state, 7);
00409
00410 quant->q4_ptr = 0;
00411 quant->q4 = q_4_1[code];
00412 COEFF (coeff[i], q_4_0[code], level, factor, exp[i]);
00413 continue;
00414 }
00415
00416 case 4:
00417 COEFF (coeff[i], q_5[bitstream_get (state, 4)], level,
00418 factor, exp[i]);
00419 continue;
00420
00421 default:
00422 COEFF (coeff[i], bitstream_get_2 (state, bapi) << (16 - bapi),
00423 level, factor, exp[i]);
00424 }
00425 }
00426 }
00427
00428 static void coeff_get_coupling (a52_state_t * state, int nfchans,
00429 level_t * coeff, sample_t (* samples)[256],
00430 quantizer_set_t * quant, uint8_t dithflag[5])
00431 {
00432 int cplbndstrc, bnd, i, i_end, ch;
00433 uint8_t * exp;
00434 int8_t * bap;
00435 level_t cplco[5];
00436
00437 exp = state->cpl_expbap.exp;
00438 bap = state->cpl_expbap.bap;
00439 bnd = 0;
00440 cplbndstrc = state->cplbndstrc;
00441 i = state->cplstrtmant;
00442 while (i < state->cplendmant) {
00443 i_end = i + 12;
00444 while (cplbndstrc & 1) {
00445 cplbndstrc >>= 1;
00446 i_end += 12;
00447 }
00448 cplbndstrc >>= 1;
00449 for (ch = 0; ch < nfchans; ch++)
00450 cplco[ch] = MUL_L (state->cplco[ch][bnd], coeff[ch]);
00451 bnd++;
00452
00453 while (i < i_end) {
00454 quantizer_t cplcoeff;
00455 int bapi;
00456
00457 bapi = bap[i];
00458 switch (bapi) {
00459 case 0:
00460 for (ch = 0; ch < nfchans; ch++)
00461 if ((state->chincpl >> ch) & 1) {
00462 if (dithflag[ch])
00463 #ifndef LIBA52_FIXED
00464 samples[ch][i] = (scale_factor[exp[i]] *
00465 cplco[ch] * dither_gen (state));
00466 #else
00467 COEFF (samples[ch][i], dither_gen (state),
00468 cplco[ch], scale_factor, exp[i]);
00469 #endif
00470 else
00471 samples[ch][i] = 0;
00472 }
00473 i++;
00474 continue;
00475
00476 case -1:
00477 if (quant->q1_ptr >= 0) {
00478 cplcoeff = quant->q1[quant->q1_ptr--];
00479 break;
00480 } else {
00481 int code;
00482
00483 code = bitstream_get (state, 5);
00484
00485 quant->q1_ptr = 1;
00486 quant->q1[0] = q_1_2[code];
00487 quant->q1[1] = q_1_1[code];
00488 cplcoeff = q_1_0[code];
00489 break;
00490 }
00491
00492 case -2:
00493 if (quant->q2_ptr >= 0) {
00494 cplcoeff = quant->q2[quant->q2_ptr--];
00495 break;
00496 } else {
00497 int code;
00498
00499 code = bitstream_get (state, 7);
00500
00501 quant->q2_ptr = 1;
00502 quant->q2[0] = q_2_2[code];
00503 quant->q2[1] = q_2_1[code];
00504 cplcoeff = q_2_0[code];
00505 break;
00506 }
00507
00508 case 3:
00509 cplcoeff = q_3[bitstream_get (state, 3)];
00510 break;
00511
00512 case -3:
00513 if (quant->q4_ptr == 0) {
00514 quant->q4_ptr = -1;
00515 cplcoeff = quant->q4;
00516 break;
00517 } else {
00518 int code;
00519
00520 code = bitstream_get (state, 7);
00521
00522 quant->q4_ptr = 0;
00523 quant->q4 = q_4_1[code];
00524 cplcoeff = q_4_0[code];
00525 break;
00526 }
00527
00528 case 4:
00529 cplcoeff = q_5[bitstream_get (state, 4)];
00530 break;
00531
00532 default:
00533 cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi);
00534 }
00535 #ifndef LIBA52_FIXED
00536 cplcoeff *= scale_factor[exp[i]];
00537 #endif
00538 for (ch = 0; ch < nfchans; ch++)
00539 if ((state->chincpl >> ch) & 1)
00540 #ifndef LIBA52_FIXED
00541 samples[ch][i] = cplcoeff * cplco[ch];
00542 #else
00543 COEFF (samples[ch][i], cplcoeff, cplco[ch],
00544 scale_factor, exp[i]);
00545 #endif
00546 i++;
00547 }
00548 }
00549 }
00550
00551 int a52_block (a52_state_t * state)
00552 {
00553 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
00554 static int rematrix_band[4] = {25, 37, 61, 253};
00555 int i, nfchans, chaninfo;
00556 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
00557 uint8_t blksw[5], dithflag[5];
00558 level_t coeff[5];
00559 int chanbias;
00560 quantizer_set_t quant;
00561 sample_t * samples;
00562
00563 nfchans = nfchans_tbl[state->acmod];
00564
00565 for (i = 0; i < nfchans; i++)
00566 blksw[i] = bitstream_get (state, 1);
00567
00568 for (i = 0; i < nfchans; i++)
00569 dithflag[i] = bitstream_get (state, 1);
00570
00571 chaninfo = !state->acmod;
00572 do {
00573 if (bitstream_get (state, 1)) {
00574 int dynrng;
00575
00576 dynrng = bitstream_get_2 (state, 8);
00577 if (state->dynrnge) {
00578 level_t range;
00579
00580 #if !defined(LIBA52_FIXED)
00581 range = ((((dynrng & 0x1f) | 0x20) << 13) *
00582 scale_factor[3 - (dynrng >> 5)]);
00583 #else
00584 range = ((dynrng & 0x1f) | 0x20) << (21 + (dynrng >> 5));
00585 #endif
00586 if (state->dynrngcall)
00587 range = state->dynrngcall (range, state->dynrngdata);
00588 state->dynrng = MUL_L (state->level, range);
00589 }
00590 }
00591 } while (chaninfo--);
00592
00593 if (bitstream_get (state, 1)) {
00594 state->chincpl = 0;
00595 if (bitstream_get (state, 1)) {
00596 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
00597 45, 45, 46, 46, 47, 47, 48, 48};
00598 int cplbegf;
00599 int cplendf;
00600 int ncplsubnd;
00601
00602 for (i = 0; i < nfchans; i++)
00603 state->chincpl |= bitstream_get (state, 1) << i;
00604 switch (state->acmod) {
00605 case 0: case 1:
00606 return 1;
00607 case 2:
00608 state->phsflginu = bitstream_get (state, 1);
00609 }
00610 cplbegf = bitstream_get (state, 4);
00611 cplendf = bitstream_get (state, 4);
00612
00613 if (cplendf + 3 - cplbegf < 0)
00614 return 1;
00615 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
00616 state->cplstrtbnd = bndtab[cplbegf];
00617 state->cplstrtmant = cplbegf * 12 + 37;
00618 state->cplendmant = cplendf * 12 + 73;
00619
00620 state->cplbndstrc = 0;
00621 for (i = 0; i < ncplsubnd - 1; i++)
00622 if (bitstream_get (state, 1)) {
00623 state->cplbndstrc |= 1 << i;
00624 state->ncplbnd--;
00625 }
00626 }
00627 }
00628
00629 if (state->chincpl) {
00630 int j, cplcoe;
00631
00632 cplcoe = 0;
00633 for (i = 0; i < nfchans; i++)
00634 if ((state->chincpl) >> i & 1)
00635 if (bitstream_get (state, 1)) {
00636 int mstrcplco, cplcoexp, cplcomant;
00637
00638 cplcoe = 1;
00639 mstrcplco = 3 * bitstream_get (state, 2);
00640 for (j = 0; j < state->ncplbnd; j++) {
00641 cplcoexp = bitstream_get (state, 4);
00642 cplcomant = bitstream_get (state, 4);
00643 if (cplcoexp == 15)
00644 cplcomant <<= 14;
00645 else
00646 cplcomant = (cplcomant | 0x10) << 13;
00647 #ifndef LIBA52_FIXED
00648 state->cplco[i][j] =
00649 cplcomant * scale_factor[cplcoexp + mstrcplco];
00650 #else
00651 state->cplco[i][j] = (cplcomant << 11) >> (cplcoexp + mstrcplco);
00652 #endif
00653
00654 }
00655 }
00656 if ((state->acmod == 2) && state->phsflginu && cplcoe)
00657 for (j = 0; j < state->ncplbnd; j++)
00658 if (bitstream_get (state, 1))
00659 state->cplco[1][j] = -state->cplco[1][j];
00660 }
00661
00662 if ((state->acmod == 2) && (bitstream_get (state, 1))) {
00663 int end;
00664
00665 state->rematflg = 0;
00666 end = (state->chincpl) ? state->cplstrtmant : 253;
00667 i = 0;
00668 do
00669 state->rematflg |= bitstream_get (state, 1) << i;
00670 while (rematrix_band[i++] < end);
00671 }
00672
00673 cplexpstr = EXP_REUSE;
00674 lfeexpstr = EXP_REUSE;
00675 if (state->chincpl)
00676 cplexpstr = bitstream_get (state, 2);
00677 for (i = 0; i < nfchans; i++)
00678 chexpstr[i] = bitstream_get (state, 2);
00679 if (state->lfeon)
00680 lfeexpstr = bitstream_get (state, 1);
00681
00682 for (i = 0; i < nfchans; i++)
00683 if (chexpstr[i] != EXP_REUSE) {
00684 if ((state->chincpl >> i) & 1)
00685 state->endmant[i] = state->cplstrtmant;
00686 else {
00687 int chbwcod;
00688
00689 chbwcod = bitstream_get (state, 6);
00690 if (chbwcod > 60)
00691 return 1;
00692 state->endmant[i] = chbwcod * 3 + 73;
00693 }
00694 }
00695
00696 do_bit_alloc = 0;
00697
00698 if (cplexpstr != EXP_REUSE) {
00699 int cplabsexp, ncplgrps;
00700
00701 do_bit_alloc = 64;
00702 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
00703 (3 << (cplexpstr - 1)));
00704 cplabsexp = bitstream_get (state, 4) << 1;
00705 if (parse_exponents (state, cplexpstr, ncplgrps, cplabsexp,
00706 state->cpl_expbap.exp + state->cplstrtmant))
00707 return 1;
00708 }
00709 for (i = 0; i < nfchans; i++)
00710 if (chexpstr[i] != EXP_REUSE) {
00711 int grp_size, nchgrps;
00712
00713 do_bit_alloc |= 1 << i;
00714 grp_size = 3 << (chexpstr[i] - 1);
00715 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
00716 state->fbw_expbap[i].exp[0] = bitstream_get (state, 4);
00717 if (parse_exponents (state, chexpstr[i], nchgrps,
00718 state->fbw_expbap[i].exp[0],
00719 state->fbw_expbap[i].exp + 1))
00720 return 1;
00721 bitstream_get (state, 2);
00722 }
00723 if (lfeexpstr != EXP_REUSE) {
00724 do_bit_alloc |= 32;
00725 state->lfe_expbap.exp[0] = bitstream_get (state, 4);
00726 if (parse_exponents (state, lfeexpstr, 2, state->lfe_expbap.exp[0],
00727 state->lfe_expbap.exp + 1))
00728 return 1;
00729 }
00730
00731 if (bitstream_get (state, 1)) {
00732 do_bit_alloc = 127;
00733 state->bai = bitstream_get (state, 11);
00734 }
00735 if (bitstream_get (state, 1)) {
00736 do_bit_alloc = 127;
00737 state->csnroffst = bitstream_get (state, 6);
00738 if (state->chincpl)
00739 state->cplba.bai = bitstream_get (state, 7);
00740 for (i = 0; i < nfchans; i++)
00741 state->ba[i].bai = bitstream_get (state, 7);
00742 if (state->lfeon)
00743 state->lfeba.bai = bitstream_get (state, 7);
00744 }
00745 if ((state->chincpl) && (bitstream_get (state, 1))) {
00746 do_bit_alloc |= 64;
00747 state->cplfleak = 9 - bitstream_get (state, 3);
00748 state->cplsleak = 9 - bitstream_get (state, 3);
00749 }
00750
00751 if (bitstream_get (state, 1)) {
00752 do_bit_alloc = 127;
00753 if (state->chincpl)
00754 state->cplba.deltbae = bitstream_get (state, 2);
00755 for (i = 0; i < nfchans; i++)
00756 state->ba[i].deltbae = bitstream_get (state, 2);
00757 if (state->chincpl &&
00758 (state->cplba.deltbae == DELTA_BIT_NEW) &&
00759 parse_deltba (state, state->cplba.deltba))
00760 return 1;
00761 for (i = 0; i < nfchans; i++)
00762 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
00763 parse_deltba (state, state->ba[i].deltba))
00764 return 1;
00765 }
00766
00767 if (do_bit_alloc) {
00768 if (zero_snr_offsets (nfchans, state)) {
00769 memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap));
00770 for (i = 0; i < nfchans; i++)
00771 memset (state->fbw_expbap[i].bap, 0,
00772 sizeof (state->fbw_expbap[i].bap));
00773 memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap));
00774 } else {
00775 if (state->chincpl && (do_bit_alloc & 64))
00776 a52_bit_allocate (state, &state->cplba, state->cplstrtbnd,
00777 state->cplstrtmant, state->cplendmant,
00778 state->cplfleak << 8, state->cplsleak << 8,
00779 &state->cpl_expbap);
00780 for (i = 0; i < nfchans; i++)
00781 if (do_bit_alloc & (1 << i))
00782 a52_bit_allocate (state, state->ba + i, 0, 0,
00783 state->endmant[i], 0, 0,
00784 state->fbw_expbap +i);
00785 if (state->lfeon && (do_bit_alloc & 32)) {
00786 state->lfeba.deltbae = DELTA_BIT_NONE;
00787 a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
00788 &state->lfe_expbap);
00789 }
00790 }
00791 }
00792
00793 if (bitstream_get (state, 1)) {
00794 i = bitstream_get (state, 9);
00795 while (i--)
00796 bitstream_get (state, 8);
00797 }
00798
00799 samples = state->samples;
00800 if (state->output & A52_LFE)
00801 samples += 256;
00802
00803 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
00804 state->dynrng, state->clev, state->slev);
00805
00806 quant.q1_ptr = quant.q2_ptr = quant.q4_ptr = -1;
00807 done_cpl = 0;
00808
00809 for (i = 0; i < nfchans; i++) {
00810 int j;
00811
00812 coeff_get (state, samples + 256 * i, state->fbw_expbap +i, &quant,
00813 coeff[i], dithflag[i], state->endmant[i]);
00814
00815 if ((state->chincpl >> i) & 1) {
00816 if (!done_cpl) {
00817 done_cpl = 1;
00818 coeff_get_coupling (state, nfchans, coeff,
00819 (sample_t (*)[256])samples, &quant,
00820 dithflag);
00821 }
00822 j = state->cplendmant;
00823 } else
00824 j = state->endmant[i];
00825 do
00826 (samples + 256 * i)[j] = 0;
00827 while (++j < 256);
00828 }
00829
00830 if (state->acmod == 2) {
00831 int j, end, band, rematflg;
00832
00833 end = ((state->endmant[0] < state->endmant[1]) ?
00834 state->endmant[0] : state->endmant[1]);
00835
00836 i = 0;
00837 j = 13;
00838 rematflg = state->rematflg;
00839 do {
00840 if (! (rematflg & 1)) {
00841 rematflg >>= 1;
00842 j = rematrix_band[i++];
00843 continue;
00844 }
00845 rematflg >>= 1;
00846 band = rematrix_band[i++];
00847 if (band > end)
00848 band = end;
00849 do {
00850 sample_t tmp0, tmp1;
00851
00852 tmp0 = samples[j];
00853 tmp1 = (samples+256)[j];
00854 samples[j] = tmp0 + tmp1;
00855 (samples+256)[j] = tmp0 - tmp1;
00856 } while (++j < band);
00857 } while (j < end);
00858 }
00859
00860 if (state->lfeon) {
00861 if (state->output & A52_LFE) {
00862 coeff_get (state, samples - 256, &state->lfe_expbap, &quant,
00863 state->dynrng, 0, 7);
00864 for (i = 7; i < 256; i++)
00865 (samples-256)[i] = 0;
00866 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
00867 } else {
00868
00869 coeff_get (state, samples + 1280, &state->lfe_expbap, &quant,
00870 0, 0, 7);
00871 }
00872 }
00873
00874 i = 0;
00875 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
00876 for (i = 1; i < nfchans; i++)
00877 if (blksw[i] != blksw[0])
00878 break;
00879
00880 if (i < nfchans) {
00881 if (state->downmixed) {
00882 state->downmixed = 0;
00883 a52_upmix (samples + 1536, state->acmod, state->output);
00884 }
00885
00886 for (i = 0; i < nfchans; i++) {
00887 sample_t bias;
00888
00889 bias = 0;
00890 if (!(chanbias & (1 << i)))
00891 bias = state->bias;
00892
00893 if (coeff[i]) {
00894 if (blksw[i])
00895 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
00896 bias);
00897 else
00898 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
00899 bias);
00900 } else {
00901 int j;
00902
00903 for (j = 0; j < 256; j++)
00904 (samples + 256 * i)[j] = bias;
00905 }
00906 }
00907
00908 a52_downmix (samples, state->acmod, state->output, state->bias,
00909 state->clev, state->slev);
00910 } else {
00911 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
00912
00913 a52_downmix (samples, state->acmod, state->output, 0,
00914 state->clev, state->slev);
00915
00916 if (!state->downmixed) {
00917 state->downmixed = 1;
00918 a52_downmix (samples + 1536, state->acmod, state->output, 0,
00919 state->clev, state->slev);
00920 }
00921
00922 if (blksw[0])
00923 for (i = 0; i < nfchans; i++)
00924 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
00925 state->bias);
00926 else
00927 for (i = 0; i < nfchans; i++)
00928 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
00929 state->bias);
00930 }
00931
00932 return 0;
00933 }
00934
00935 void a52_free (a52_state_t * state)
00936 {
00937 free (state->samples);
00938 free (state);
00939 }