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.

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