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.

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