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.

685 lines
16KB

  1. /*
  2. * parse.c
  3. *
  4. * Copyright (C) Aaron Holtzman - May 1999
  5. *
  6. * This file is part of ac3dec, a free Dolby AC-3 stream decoder.
  7. *
  8. * ac3dec is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * ac3dec is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Make; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. *
  23. */
  24. #include <inttypes.h>
  25. #include <string.h>
  26. #include "ac3.h"
  27. #include "ac3_internal.h"
  28. #include "bitstream.h"
  29. #include "tables.h"
  30. extern stream_samples_t samples; // FIXME
  31. static float delay[6][256];
  32. void ac3_init (void)
  33. {
  34. imdct_init ();
  35. }
  36. static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
  37. int ac3_syncinfo (uint8_t * buf, int * flags,
  38. int * sample_rate, int * bit_rate)
  39. {
  40. static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
  41. 128, 160, 192, 224, 256, 320, 384, 448,
  42. 512, 576, 640};
  43. static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
  44. int frmsizecod;
  45. int bitrate;
  46. int half;
  47. int acmod;
  48. if ((buf[0] != 0x0b) || (buf[1] != 0x77)) // syncword
  49. return 0;
  50. if (buf[5] >= 0x60) // bsid >= 12
  51. return 0;
  52. half = halfrate[buf[5] >> 3];
  53. // acmod, dsurmod and lfeon
  54. acmod = buf[6] >> 5;
  55. *flags = (((buf[6] & 0xf8) == 0x50) ? AC3_DOLBY : acmod) |
  56. ((buf[6] & lfeon[acmod]) ? AC3_LFE : 0);
  57. frmsizecod = buf[4] & 63;
  58. if (frmsizecod >= 38)
  59. return 0;
  60. bitrate = rate [frmsizecod >> 1];
  61. *bit_rate = (bitrate * 1000) >> half;
  62. switch (buf[4] & 0xc0) {
  63. case 0: // 48 KHz
  64. *sample_rate = 48000 >> half;
  65. return 4 * bitrate;
  66. case 0x40:
  67. *sample_rate = 44100 >> half;
  68. return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
  69. case 0x80:
  70. *sample_rate = 32000 >> half;
  71. return 6 * bitrate;
  72. default:
  73. return 0;
  74. }
  75. }
  76. int ac3_frame (ac3_state_t * state, uint8_t * buf, int * flags, float * level,
  77. float bias)
  78. {
  79. static float clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
  80. static float slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
  81. int chaninfo;
  82. int acmod;
  83. state->fscod = buf[4] >> 6;
  84. state->halfrate = halfrate[buf[5] >> 3];
  85. state->acmod = acmod = buf[6] >> 5;
  86. bitstream_set_ptr (buf + 6);
  87. bitstream_get (3); // skip acmod we already parsed
  88. if ((acmod == 2) && (bitstream_get (2) == 2)) // dsurmod
  89. acmod = AC3_DOLBY;
  90. if ((acmod & 1) && (acmod != 1))
  91. state->clev = clev[bitstream_get (2)]; // cmixlev
  92. if (acmod & 4)
  93. state->slev = slev[bitstream_get (2)]; // surmixlev
  94. state->lfeon = bitstream_get (1);
  95. state->output = downmix_init (acmod, *flags, level,
  96. state->clev, state->slev);
  97. if (state->output < 0)
  98. return 1;
  99. *flags = state->output;
  100. state->level = *level;
  101. state->bias = bias;
  102. chaninfo = !acmod;
  103. do {
  104. bitstream_get (5); // dialnorm
  105. if (bitstream_get (1)) // compre
  106. bitstream_get (8); // compr
  107. if (bitstream_get (1)) // langcode
  108. bitstream_get (8); // langcod
  109. if (bitstream_get (1)) // audprodie
  110. bitstream_get (7); // mixlevel + roomtyp
  111. } while (chaninfo--);
  112. bitstream_get (2); // copyrightb + origbs
  113. if (bitstream_get (1)) // timecod1e
  114. bitstream_get (14); // timecod1
  115. if (bitstream_get (1)) // timecod2e
  116. bitstream_get (14); // timecod2
  117. if (bitstream_get (1)) { // addbsie
  118. int addbsil;
  119. addbsil = bitstream_get (6);
  120. do {
  121. bitstream_get (8); // addbsi
  122. } while (addbsil--);
  123. }
  124. return 0;
  125. }
  126. static int parse_exponents (int expstr, int ngrps, uint8_t exponent,
  127. uint8_t * dest)
  128. {
  129. int exps;
  130. while (ngrps--) {
  131. exps = bitstream_get (7);
  132. exponent += exp_1[exps];
  133. if (exponent > 24)
  134. return 1;
  135. switch (expstr) {
  136. case EXP_D45:
  137. *(dest++) = exponent;
  138. *(dest++) = exponent;
  139. case EXP_D25:
  140. *(dest++) = exponent;
  141. case EXP_D15:
  142. *(dest++) = exponent;
  143. }
  144. exponent += exp_2[exps];
  145. if (exponent > 24)
  146. return 1;
  147. switch (expstr) {
  148. case EXP_D45:
  149. *(dest++) = exponent;
  150. *(dest++) = exponent;
  151. case EXP_D25:
  152. *(dest++) = exponent;
  153. case EXP_D15:
  154. *(dest++) = exponent;
  155. }
  156. exponent += exp_3[exps];
  157. if (exponent > 24)
  158. return 1;
  159. switch (expstr) {
  160. case EXP_D45:
  161. *(dest++) = exponent;
  162. *(dest++) = exponent;
  163. case EXP_D25:
  164. *(dest++) = exponent;
  165. case EXP_D15:
  166. *(dest++) = exponent;
  167. }
  168. }
  169. return 0;
  170. }
  171. static int parse_deltba (int8_t * deltba)
  172. {
  173. int deltnseg, deltlen, delta, j;
  174. memset (deltba, 0, 50);
  175. deltnseg = bitstream_get (3);
  176. j = 0;
  177. do {
  178. j += bitstream_get (5);
  179. deltlen = bitstream_get (4);
  180. delta = bitstream_get (3);
  181. delta -= (delta >= 4) ? 3 : 4;
  182. if (!deltlen)
  183. continue;
  184. if (j + deltlen >= 50)
  185. return 1;
  186. while (deltlen--)
  187. deltba[j++] = delta;
  188. } while (deltnseg--);
  189. return 0;
  190. }
  191. static inline int zero_snr_offsets (int nfchans, ac3_state_t * state)
  192. {
  193. int i;
  194. if ((state->csnroffst) || (state->cplinu && state->cplba.fsnroffst) ||
  195. (state->lfeon && state->lfeba.fsnroffst))
  196. return 0;
  197. for (i = 0; i < nfchans; i++)
  198. if (state->ba[i].fsnroffst)
  199. return 0;
  200. return 1;
  201. }
  202. static float q_1[2];
  203. static float q_2[2];
  204. static float q_4;
  205. static int q_1_pointer;
  206. static int q_2_pointer;
  207. static int q_4_pointer;
  208. #define GET_COEFF(COEFF,DITHER) \
  209. switch (bap[i]) { \
  210. case 0: \
  211. DITHER (scale_factor[exp[i]]); \
  212. \
  213. case -1: \
  214. if (q_1_pointer >= 0) { \
  215. COEFF (q_1[q_1_pointer--] * scale_factor[exp[i]]); \
  216. } else { \
  217. int code; \
  218. \
  219. code = bitstream_get (5); \
  220. \
  221. q_1_pointer = 1; \
  222. q_1[0] = q_1_2[code]; \
  223. q_1[1] = q_1_1[code]; \
  224. COEFF (q_1_0[code] * scale_factor[exp[i]]); \
  225. } \
  226. \
  227. case -2: \
  228. if (q_2_pointer >= 0) { \
  229. COEFF (q_2[q_2_pointer--] * scale_factor[exp[i]]); \
  230. } else { \
  231. int code; \
  232. \
  233. code = bitstream_get (7); \
  234. \
  235. q_2_pointer = 1; \
  236. q_2[0] = q_2_2[code]; \
  237. q_2[1] = q_2_1[code]; \
  238. COEFF (q_2_0[code] * scale_factor[exp[i]]); \
  239. } \
  240. \
  241. case 3: \
  242. COEFF (q_3[bitstream_get (3)] * scale_factor[exp[i]]); \
  243. \
  244. case -3: \
  245. if (q_4_pointer == 0) { \
  246. q_4_pointer = -1; \
  247. COEFF (q_4 * scale_factor[exp[i]]); \
  248. } else { \
  249. int code; \
  250. \
  251. code = bitstream_get (7); \
  252. \
  253. q_4_pointer = 0; \
  254. q_4 = q_4_1[code]; \
  255. COEFF (q_4_0[code] * scale_factor[exp[i]]); \
  256. } \
  257. \
  258. case 4: \
  259. COEFF (q_5[bitstream_get (4)] * scale_factor[exp[i]]); \
  260. \
  261. default: \
  262. COEFF (((int16_t)(bitstream_get(bap[i]) << (16 - bap[i]))) * \
  263. scale_factor[exp[i]]); \
  264. }
  265. #define CHANNEL_COEFF(val) \
  266. coeff[i++] = val; \
  267. continue;
  268. #define CHANNEL_DITHER(val) \
  269. if (dither) { \
  270. coeff[i++] = dither_gen () * val; \
  271. continue; \
  272. } else { \
  273. coeff[i++] = 0; \
  274. continue; \
  275. }
  276. static uint16_t lfsr_state = 1;
  277. static inline int16_t dither_gen(void)
  278. {
  279. int16_t state;
  280. state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
  281. lfsr_state = (uint16_t) state;
  282. return ((state * (int) (LEVEL_3DB * 256)) >> 8);
  283. }
  284. static void coeff_get (float * coeff, uint8_t * exp, int8_t * bap,
  285. int dither, int end)
  286. {
  287. int i;
  288. i = 0;
  289. while (i < end)
  290. GET_COEFF (CHANNEL_COEFF, CHANNEL_DITHER);
  291. }
  292. #define COUPLING_COEFF(val) \
  293. cplcoeff = val; \
  294. break;
  295. #define COUPLING_DITHER(val) \
  296. cplcoeff = val; \
  297. for (ch = 0; ch < nfchans; ch++) \
  298. if (state->chincpl[ch]) { \
  299. if (dithflag[ch]) \
  300. samples[ch][i] = \
  301. state->cplco[ch][bnd] * dither_gen () * cplcoeff; \
  302. else \
  303. samples[ch][i] = 0; \
  304. } \
  305. i++; \
  306. continue;
  307. int ac3_block (ac3_state_t * state)
  308. {
  309. static const uint8_t nfchans_tbl[8] = {2, 1, 2, 3, 3, 4, 4, 5};
  310. static int rematrix_band[4] = {25, 37, 61, 253};
  311. int i, nfchans, chaninfo;
  312. uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
  313. uint8_t blksw[5], dithflag[5];
  314. nfchans = nfchans_tbl[state->acmod];
  315. for (i = 0; i < nfchans; i++)
  316. blksw[i] = bitstream_get (1);
  317. for (i = 0; i < nfchans; i++)
  318. dithflag[i] = bitstream_get (1);
  319. chaninfo = !(state->acmod);
  320. do {
  321. if (bitstream_get (1)) // dynrnge
  322. bitstream_get (8); // dynrng
  323. } while (chaninfo--);
  324. if (bitstream_get (1)) { // cplstre
  325. state->cplinu = bitstream_get (1);
  326. if (state->cplinu) {
  327. static int bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
  328. 45, 45, 46, 46, 47, 47, 48, 48};
  329. int cplbegf;
  330. int cplendf;
  331. int ncplsubnd;
  332. for (i = 0; i < nfchans; i++)
  333. state->chincpl[i] = bitstream_get (1);
  334. switch (state->acmod) {
  335. case 0: case 1:
  336. return 1;
  337. case 2:
  338. state->phsflginu = bitstream_get (1);
  339. }
  340. cplbegf = bitstream_get (4);
  341. cplendf = bitstream_get (4);
  342. if (cplendf + 3 - cplbegf < 0)
  343. return 1;
  344. state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
  345. state->cplstrtbnd = bndtab[cplbegf];
  346. state->cplstrtmant = cplbegf * 12 + 37;
  347. state->cplendmant = cplendf * 12 + 73;
  348. for (i = 0; i < ncplsubnd - 1; i++) {
  349. state->cplbndstrc[i] = bitstream_get (1);
  350. state->ncplbnd -= state->cplbndstrc[i];
  351. }
  352. state->cplbndstrc[i] = 0; // last value is a sentinel
  353. }
  354. }
  355. if (state->cplinu) {
  356. int j, cplcoe;
  357. cplcoe = 0;
  358. for (i = 0; i < nfchans; i++)
  359. if (state->chincpl[i])
  360. if (bitstream_get (1)) { // cplcoe
  361. int mstrcplco, cplcoexp, cplcomant;
  362. cplcoe = 1;
  363. mstrcplco = 3 * bitstream_get (2);
  364. for (j = 0; j < state->ncplbnd; j++) {
  365. cplcoexp = bitstream_get (4);
  366. cplcomant = bitstream_get (4);
  367. if (cplcoexp == 15)
  368. cplcomant <<= 14;
  369. else
  370. cplcomant = (cplcomant | 0x10) << 13;
  371. state->cplco[i][j] =
  372. cplcomant * scale_factor[cplcoexp + mstrcplco];
  373. }
  374. }
  375. if ((state->acmod == 2) && state->phsflginu && cplcoe)
  376. for (j = 0; j < state->ncplbnd; j++)
  377. if (bitstream_get (1)) // phsflg
  378. state->cplco[1][j] = -state->cplco[1][j];
  379. }
  380. if ((state->acmod == 2) && (bitstream_get (1))) { // rematstr
  381. int end;
  382. end = (state->cplinu) ? state->cplstrtmant : 253;
  383. i = 0;
  384. do
  385. state->rematflg[i] = bitstream_get (1);
  386. while (rematrix_band[i++] < end);
  387. }
  388. cplexpstr = EXP_REUSE;
  389. lfeexpstr = EXP_REUSE;
  390. if (state->cplinu)
  391. cplexpstr = bitstream_get (2);
  392. for (i = 0; i < nfchans; i++)
  393. chexpstr[i] = bitstream_get (2);
  394. if (state->lfeon)
  395. lfeexpstr = bitstream_get (1);
  396. for (i = 0; i < nfchans; i++)
  397. if (chexpstr[i] != EXP_REUSE) {
  398. if (state->cplinu && state->chincpl[i])
  399. state->endmant[i] = state->cplstrtmant;
  400. else {
  401. int chbwcod;
  402. chbwcod = bitstream_get (6);
  403. if (chbwcod > 60)
  404. return 1;
  405. state->endmant[i] = chbwcod * 3 + 73;
  406. }
  407. }
  408. do_bit_alloc = 0;
  409. if (cplexpstr != EXP_REUSE) {
  410. int cplabsexp, ncplgrps;
  411. do_bit_alloc = 1;
  412. ncplgrps = ((state->cplendmant - state->cplstrtmant) /
  413. (3 << (cplexpstr - 1)));
  414. cplabsexp = bitstream_get (4) << 1;
  415. if (parse_exponents (cplexpstr, ncplgrps, cplabsexp,
  416. state->cpl_exp + state->cplstrtmant))
  417. return 1;
  418. }
  419. for (i = 0; i < nfchans; i++)
  420. if (chexpstr[i] != EXP_REUSE) {
  421. int grp_size, nchgrps;
  422. do_bit_alloc = 1;
  423. grp_size = 3 << (chexpstr[i] - 1);
  424. nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
  425. state->fbw_exp[i][0] = bitstream_get (4);
  426. if (parse_exponents (chexpstr[i], nchgrps, state->fbw_exp[i][0],
  427. state->fbw_exp[i] + 1))
  428. return 1;
  429. bitstream_get (2); // gainrng
  430. }
  431. if (lfeexpstr != EXP_REUSE) {
  432. do_bit_alloc = 1;
  433. state->lfe_exp[0] = bitstream_get (4);
  434. if (parse_exponents (lfeexpstr, 2, state->lfe_exp[0],
  435. state->lfe_exp + 1))
  436. return 1;
  437. }
  438. if (bitstream_get (1)) { // baie
  439. do_bit_alloc = 1;
  440. state->sdcycod = bitstream_get (2);
  441. state->fdcycod = bitstream_get (2);
  442. state->sgaincod = bitstream_get (2);
  443. state->dbpbcod = bitstream_get (2);
  444. state->floorcod = bitstream_get (3);
  445. }
  446. if (bitstream_get (1)) { //snroffste
  447. do_bit_alloc = 1;
  448. state->csnroffst = bitstream_get (6);
  449. if (state->cplinu) {
  450. state->cplba.fsnroffst = bitstream_get (4);
  451. state->cplba.fgaincod = bitstream_get (3);
  452. }
  453. for (i = 0; i < nfchans; i++) {
  454. state->ba[i].fsnroffst = bitstream_get (4);
  455. state->ba[i].fgaincod = bitstream_get (3);
  456. }
  457. if (state->lfeon) {
  458. state->lfeba.fsnroffst = bitstream_get (4);
  459. state->lfeba.fgaincod = bitstream_get (3);
  460. }
  461. }
  462. if ((state->cplinu) && (bitstream_get (1))) { // cplleake
  463. do_bit_alloc = 1;
  464. state->cplfleak = 2304 - (bitstream_get (3) << 8);
  465. state->cplsleak = 2304 - (bitstream_get (3) << 8);
  466. }
  467. if (bitstream_get (1)) { // deltbaie
  468. do_bit_alloc = 1;
  469. if (state->cplinu)
  470. state->cplba.deltbae = bitstream_get (2);
  471. for (i = 0; i < nfchans; i++)
  472. state->ba[i].deltbae = bitstream_get (2);
  473. if (state->cplinu && (state->cplba.deltbae == DELTA_BIT_NEW) &&
  474. parse_deltba (state->cplba.deltba))
  475. return 1;
  476. for (i = 0; i < nfchans; i++)
  477. if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
  478. parse_deltba (state->ba[i].deltba))
  479. return 1;
  480. }
  481. if (do_bit_alloc) {
  482. if (zero_snr_offsets (nfchans, state)) {
  483. memset (state->cpl_bap, 0, sizeof (state->cpl_bap));
  484. memset (state->fbw_bap, 0, sizeof (state->fbw_bap));
  485. memset (state->lfe_bap, 0, sizeof (state->lfe_bap));
  486. } else {
  487. if (state->cplinu)
  488. bit_allocate (state, &state->cplba, state->cplstrtbnd,
  489. state->cplstrtmant, state->cplendmant,
  490. state->cplfleak, state->cplsleak,
  491. state->cpl_exp, state->cpl_bap);
  492. for (i = 0; i < nfchans; i++)
  493. bit_allocate (state, state->ba + i, 0, 0, state->endmant[i],
  494. 0, 0, state->fbw_exp[i], state->fbw_bap[i]);
  495. if (state->lfeon) {
  496. state->lfeba.deltbae = DELTA_BIT_NONE;
  497. bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
  498. state->lfe_exp, state->lfe_bap);
  499. }
  500. }
  501. }
  502. if (bitstream_get (1)) { // skiple
  503. i = bitstream_get (9); // skipl
  504. while (i--)
  505. bitstream_get (8);
  506. }
  507. q_1_pointer = q_2_pointer = q_4_pointer = -1;
  508. done_cpl = 0;
  509. for (i = 0; i < nfchans; i++) {
  510. int j;
  511. coeff_get (samples[i], state->fbw_exp[i], state->fbw_bap[i],
  512. dithflag[i], state->endmant[i]);
  513. if (state->cplinu && state->chincpl[i]) {
  514. if (!done_cpl) {
  515. int i, i_end, bnd, sub_bnd, ch;
  516. float cplcoeff;
  517. done_cpl = 1;
  518. #define bap state->cpl_bap
  519. #define exp state->cpl_exp
  520. sub_bnd = bnd = 0;
  521. i = state->cplstrtmant;
  522. while (i < state->cplendmant) {
  523. i_end = i + 12;
  524. while (state->cplbndstrc[sub_bnd++])
  525. i_end += 12;
  526. while (i < i_end) {
  527. GET_COEFF (COUPLING_COEFF, COUPLING_DITHER);
  528. for (ch = 0; ch < nfchans; ch++)
  529. if (state->chincpl[ch])
  530. samples[ch][i] =
  531. state->cplco[ch][bnd] * cplcoeff;
  532. i++;
  533. }
  534. bnd++;
  535. }
  536. #undef bap
  537. #undef exp
  538. }
  539. j = state->cplendmant;
  540. } else
  541. j = state->endmant[i];
  542. for (; j < 256; j++)
  543. samples[i][j] = 0;
  544. }
  545. if (state->acmod == 2) {
  546. int j, end, band;
  547. end = ((state->endmant[0] < state->endmant[1]) ?
  548. state->endmant[0] : state->endmant[1]);
  549. i = 0;
  550. j = 13;
  551. do {
  552. if (!state->rematflg[i]) {
  553. j = rematrix_band[i++];
  554. continue;
  555. }
  556. band = rematrix_band[i++];
  557. if (band > end)
  558. band = end;
  559. do {
  560. float tmp0, tmp1;
  561. tmp0 = samples[0][j];
  562. tmp1 = samples[1][j];
  563. samples[0][j] = tmp0 + tmp1;
  564. samples[1][j] = tmp0 - tmp1;
  565. } while (++j < band);
  566. } while (j < end);
  567. }
  568. if (state->lfeon) {
  569. coeff_get (samples[5], state->lfe_exp, state->lfe_bap, 0, 7);
  570. #if 0
  571. for (i = 7; i < 256; i++)
  572. samples[5][i] = 0;
  573. #endif
  574. }
  575. for (i = 0; i < nfchans; i++)
  576. if (blksw[i])
  577. imdct_256 (samples[i], delay[i]);
  578. else
  579. imdct_512 (samples[i], delay[i]);
  580. #if 0
  581. if (state->lfeon)
  582. imdct_512 (samples[5], delay[5]);
  583. #endif
  584. downmix (*samples, state->acmod, state->output, state->level, state->bias,
  585. state->clev, state->slev);
  586. return 0;
  587. }