You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

940 lines
23KB

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