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.

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