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