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.

895 lines
21KB

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