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.

1992 lines
61KB

  1. /* AC3 Audio Decoder.
  2. *
  3. * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include <stdio.h>
  20. #include <stddef.h>
  21. #include <math.h>
  22. #include <string.h>
  23. #define ALT_BITSTREAM_READER
  24. #include "ac3tab.h"
  25. #include "ac3_decoder.h"
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "dsputil.h"
  29. #include "avutil.h"
  30. #include "common.h"
  31. #define N 512 /* constant for IMDCT Block size */
  32. #define MAX_CHANNELS 6
  33. #define BLOCK_SIZE 256
  34. #define AUDIO_BLOCKS 6
  35. /* Exponent strategies. */
  36. #define AC3_EXPSTR_D15 0x01
  37. #define AC3_EXPSTR_D25 0x02
  38. #define AC3_EXPSTR_D45 0x03
  39. #define AC3_EXPSTR_REUSE 0x00
  40. /* Bit allocation strategies. */
  41. #define AC3_DBASTR_NEW 0x01
  42. #define AC3_DBASTR_NONE 0x02
  43. #define AC3_DBASTR_RESERVED 0x03
  44. #define AC3_DBASTR_REUSE 0x00
  45. /* Output and input configurations. */
  46. #define AC3_OUTPUT_UNMODIFIED 0x01
  47. #define AC3_OUTPUT_MONO 0x02
  48. #define AC3_OUTPUT_STEREO 0x04
  49. #define AC3_OUTPUT_DOLBY 0x08
  50. #define AC3_OUTPUT_LFEON 0x10
  51. #define AC3_INPUT_DUALMONO 0x00
  52. #define AC3_INPUT_MONO 0x01
  53. #define AC3_INPUT_STEREO 0x02
  54. #define AC3_INPUT_3F 0x03
  55. #define AC3_INPUT_2F_1R 0x04
  56. #define AC3_INPUT_3F_1R 0x05
  57. #define AC3_INPUT_2F_2R 0x06
  58. #define AC3_INPUT_3F_2R 0x07
  59. /* Mersenne Twister */
  60. #define NMT 624
  61. #define MMT 397
  62. #define MATRIX_A 0x9908b0df
  63. #define UPPER_MASK 0x80000000
  64. #define LOWER_MASK 0x7fffffff
  65. typedef struct {
  66. uint32_t mt[NMT];
  67. int mti;
  68. } dither_state;
  69. /* Mersenne Twister */
  70. typedef struct {
  71. uint32_t flags;
  72. uint16_t crc1;
  73. uint8_t fscod;
  74. uint8_t acmod;
  75. uint8_t cmixlev;
  76. uint8_t surmixlev;
  77. uint8_t dsurmod;
  78. uint8_t blksw;
  79. uint8_t dithflag;
  80. uint8_t cplinu;
  81. uint8_t chincpl;
  82. uint8_t phsflginu;
  83. uint8_t cplbegf;
  84. uint8_t cplendf;
  85. uint8_t cplcoe;
  86. uint32_t cplbndstrc;
  87. uint8_t rematstr;
  88. uint8_t rematflg;
  89. uint8_t cplexpstr;
  90. uint8_t lfeexpstr;
  91. uint8_t chexpstr[5];
  92. uint8_t sdcycod;
  93. uint8_t fdcycod;
  94. uint8_t sgaincod;
  95. uint8_t dbpbcod;
  96. uint8_t floorcod;
  97. uint8_t csnroffst;
  98. uint8_t cplfsnroffst;
  99. uint8_t cplfgaincod;
  100. uint8_t fsnroffst[5];
  101. uint8_t fgaincod[5];
  102. uint8_t lfefsnroffst;
  103. uint8_t lfefgaincod;
  104. uint8_t cplfleak;
  105. uint8_t cplsleak;
  106. uint8_t cpldeltbae;
  107. uint8_t deltbae[5];
  108. uint8_t cpldeltnseg;
  109. uint8_t cpldeltoffst[8];
  110. uint8_t cpldeltlen[8];
  111. uint8_t cpldeltba[8];
  112. uint8_t deltnseg[5];
  113. uint8_t deltoffst[5][8];
  114. uint8_t deltlen[5][8];
  115. uint8_t deltba[5][8];
  116. /* Derived Attributes. */
  117. int sampling_rate;
  118. int bit_rate;
  119. int frame_size;
  120. int nfchans;
  121. int lfeon;
  122. float chcoeffs[6];
  123. float cplco[5][18];
  124. int ncplbnd;
  125. int ncplsubnd;
  126. int cplstrtmant;
  127. int cplendmant;
  128. int endmant[5];
  129. uint8_t dcplexps[256];
  130. uint8_t dexps[5][256];
  131. uint8_t dlfeexps[256];
  132. uint8_t cplbap[256];
  133. uint8_t bap[5][256];
  134. uint8_t lfebap[256];
  135. int blkoutput;
  136. DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]);
  137. /* For IMDCT. */
  138. MDCTContext imdct_512; //N/8 point IFFT context
  139. MDCTContext imdct_256; //N/4 point IFFT context
  140. DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]);
  141. DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]);
  142. DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);
  143. DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);
  144. /* Miscellaneous. */
  145. GetBitContext gb;
  146. dither_state dith_state;
  147. } AC3DecodeContext;
  148. /* BEGIN Mersenne Twister Code. */
  149. static void dither_seed(dither_state *state, uint32_t seed)
  150. {
  151. if (seed == 0)
  152. seed = 0x1f2e3d4c;
  153. state->mt[0] = seed;
  154. for (state->mti = 1; state->mti < NMT; state->mti++)
  155. state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
  156. }
  157. static uint32_t dither_uint32(dither_state *state)
  158. {
  159. uint32_t y;
  160. static const uint32_t mag01[2] = { 0x00, MATRIX_A };
  161. int kk;
  162. if (state->mti >= NMT) {
  163. for (kk = 0; kk < NMT - MMT; kk++) {
  164. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  165. state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
  166. }
  167. for (;kk < NMT - 1; kk++) {
  168. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  169. state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
  170. }
  171. y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
  172. state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
  173. state->mti = 0;
  174. }
  175. y = state->mt[state->mti++];
  176. y ^= (y >> 11);
  177. y ^= ((y << 7) & 0x9d2c5680);
  178. y ^= ((y << 15) & 0xefc60000);
  179. y ^= (y >> 18);
  180. return y;
  181. }
  182. static inline int16_t dither_int16(dither_state *state)
  183. {
  184. return ((dither_uint32(state) << 16) >> 16);
  185. }
  186. /* END Mersenne Twister */
  187. /**
  188. * Generate a Kaiser Window.
  189. */
  190. static void
  191. k_window_init(int alpha, float *window, int n, int iter)
  192. {
  193. int j, k;
  194. float a, x;
  195. a = alpha * M_PI / n;
  196. a = a*a;
  197. for(k=0; k<n; k++) {
  198. x = k * (n - k) * a;
  199. window[k] = 1.0;
  200. for(j=iter; j>0; j--) {
  201. window[k] = (window[k] * x / (j*j)) + 1.0;
  202. }
  203. }
  204. }
  205. /**
  206. * Generate a Kaiser-Bessel Derived Window.
  207. * @param alpha determines window shape
  208. * @param window array to fill with window values
  209. * @param n length of the window
  210. * @param iter number of iterations to use in BesselI0
  211. */
  212. static void
  213. kbd_window_init(int alpha, float *window, int n, int iter)
  214. {
  215. int k, n2;
  216. float *kwindow;
  217. n2 = n >> 1;
  218. kwindow = &window[n2];
  219. k_window_init(alpha, kwindow, n2, iter);
  220. window[0] = kwindow[0];
  221. for(k=1; k<n2; k++) {
  222. window[k] = window[k-1] + kwindow[k];
  223. }
  224. for(k=0; k<n2; k++) {
  225. window[k] = sqrt(window[k] / (window[n2-1]+1));
  226. window[n-1-k] = window[k];
  227. }
  228. }
  229. static void generate_quantizers_table(int16_t quantizers[], int level, int length)
  230. {
  231. int i;
  232. for (i = 0; i < length; i++)
  233. quantizers[i] = ((2 * i - level + 1) << 15) / level;
  234. }
  235. static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
  236. {
  237. int i, j;
  238. int16_t v;
  239. for (i = 0; i < length1; i++) {
  240. v = ((2 * i - level + 1) << 15) / level;
  241. for (j = 0; j < length2; j++)
  242. quantizers[i * length2 + j] = v;
  243. }
  244. for (i = length1 * length2; i < size; i++)
  245. quantizers[i] = 0;
  246. }
  247. static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
  248. {
  249. int i, j;
  250. int16_t v;
  251. for (i = 0; i < length1; i++) {
  252. v = ((2 * (i % level) - level + 1) << 15) / level;
  253. for (j = 0; j < length2; j++)
  254. quantizers[i * length2 + j] = v;
  255. }
  256. for (i = length1 * length2; i < size; i++)
  257. quantizers[i] = 0;
  258. }
  259. static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
  260. {
  261. int i, j;
  262. for (i = 0; i < length1; i++)
  263. for (j = 0; j < length2; j++)
  264. quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
  265. for (i = length1 * length2; i < size; i++)
  266. quantizers[i] = 0;
  267. }
  268. static void ac3_tables_init(void)
  269. {
  270. int i, j, k, l, v;
  271. /* compute bndtab and masktab from bandsz */
  272. k = 0;
  273. l = 0;
  274. for(i=0;i<50;i++) {
  275. bndtab[i] = l;
  276. v = bndsz[i];
  277. for(j=0;j<v;j++) masktab[k++]=i;
  278. l += v;
  279. }
  280. masktab[253] = masktab[254] = masktab[255] = 0;
  281. bndtab[50] = 0;
  282. /* Exponent Decoding Tables */
  283. for (i = 0; i < 5; i++) {
  284. v = i - 2;
  285. for (j = 0; j < 25; j++)
  286. exp_1[i * 25 + j] = v;
  287. }
  288. for (i = 0; i < 25; i++) {
  289. v = (i % 5) - 2;
  290. for (j = 0; j < 5; j++)
  291. exp_2[i * 5 + j] = v;
  292. }
  293. for (i = 0; i < 25; i++) {
  294. v = -2;
  295. for (j = 0; j < 5; j++)
  296. exp_3[i * 5 + j] = v++;
  297. }
  298. for (i = 125; i < 128; i++)
  299. exp_1[i] = exp_2[i] = exp_3[i] = 25;
  300. /* End Exponent Decoding Tables */
  301. /* Quantizer ungrouping tables. */
  302. // for level-3 quantizers
  303. generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
  304. generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
  305. generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
  306. //for level-5 quantizers
  307. generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
  308. generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
  309. generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
  310. //for level-7 quantizers
  311. generate_quantizers_table(l7_quantizers, 7, 7);
  312. //for level-4 quantizers
  313. generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
  314. generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
  315. //for level-15 quantizers
  316. generate_quantizers_table(l15_quantizers, 15, 15);
  317. /* Kaiser-Bessel derived window. */
  318. kbd_window_init(5, window, 256, 100);
  319. }
  320. static int ac3_decode_init(AVCodecContext *avctx)
  321. {
  322. AC3DecodeContext *ctx = avctx->priv_data;
  323. ac3_tables_init();
  324. ff_mdct_init(&ctx->imdct_256, 8, 1);
  325. ff_mdct_init(&ctx->imdct_512, 9, 1);
  326. dither_seed(&ctx->dith_state, 0);
  327. return 0;
  328. }
  329. static int ac3_synchronize(uint8_t *buf, int buf_size)
  330. {
  331. int i;
  332. for (i = 0; i < buf_size - 1; i++)
  333. if (buf[i] == 0x0b && buf[i + 1] == 0x77)
  334. return i;
  335. return -1;
  336. }
  337. //Returns -1 when 'fscod' is not valid;
  338. static int ac3_parse_sync_info(AC3DecodeContext *ctx)
  339. {
  340. GetBitContext *gb = &ctx->gb;
  341. int frmsizecod, bsid;
  342. skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
  343. ctx->crc1 = get_bits(gb, 16);
  344. ctx->fscod = get_bits(gb, 2);
  345. if (ctx->fscod == 0x03)
  346. return 0;
  347. frmsizecod = get_bits(gb, 6);
  348. if (frmsizecod >= 38)
  349. return 0;
  350. ctx->sampling_rate = ac3_freqs[ctx->fscod];
  351. ctx->bit_rate = ac3_bitratetab[frmsizecod >> 1];
  352. /* we include it here in order to determine validity of ac3 frame */
  353. bsid = get_bits(gb, 5);
  354. if (bsid > 0x08)
  355. return 0;
  356. skip_bits(gb, 3); //skip the bsmod, bsi->bsmod = get_bits(gb, 3);
  357. switch (ctx->fscod) {
  358. case 0x00:
  359. ctx->frame_size = 4 * ctx->bit_rate;
  360. return ctx->frame_size;
  361. case 0x01:
  362. ctx->frame_size = 2 * (320 * ctx->bit_rate / 147 + (frmsizecod & 1));
  363. return ctx->frame_size;
  364. case 0x02:
  365. ctx->frame_size = 6 * ctx->bit_rate;
  366. return ctx->frame_size;
  367. }
  368. /* never reached */
  369. return 0;
  370. }
  371. static void ac3_parse_bsi(AC3DecodeContext *ctx)
  372. {
  373. GetBitContext *gb = &ctx->gb;
  374. int i;
  375. ctx->cmixlev = 0;
  376. ctx->surmixlev = 0;
  377. ctx->dsurmod = 0;
  378. ctx->nfchans = 0;
  379. ctx->cpldeltbae = AC3_DBASTR_NONE;
  380. ctx->cpldeltnseg = 0;
  381. for (i = 0; i < 5; i++) {
  382. ctx->deltbae[i] = AC3_DBASTR_NONE;
  383. ctx->deltnseg[i] = 0;
  384. }
  385. ctx->acmod = get_bits(gb, 3);
  386. ctx->nfchans = nfchans_tbl[ctx->acmod];
  387. if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
  388. ctx->cmixlev = get_bits(gb, 2);
  389. if (ctx->acmod & 0x04)
  390. ctx->surmixlev = get_bits(gb, 2);
  391. if (ctx->acmod == 0x02)
  392. ctx->dsurmod = get_bits(gb, 2);
  393. ctx->lfeon = get_bits1(gb);
  394. i = !(ctx->acmod);
  395. do {
  396. skip_bits(gb, 5); //skip dialog normalization
  397. if (get_bits1(gb))
  398. skip_bits(gb, 8); //skip compression
  399. if (get_bits1(gb))
  400. skip_bits(gb, 8); //skip language code
  401. if (get_bits1(gb))
  402. skip_bits(gb, 7); //skip audio production information
  403. } while (i--);
  404. skip_bits(gb, 2); //skip copyright bit and original bitstream bit
  405. if (get_bits1(gb))
  406. skip_bits(gb, 14); //skip timecode1
  407. if (get_bits1(gb))
  408. skip_bits(gb, 14); //skip timecode2
  409. if (get_bits1(gb)) {
  410. i = get_bits(gb, 6); //additional bsi length
  411. do {
  412. skip_bits(gb, 8);
  413. } while(i--);
  414. }
  415. }
  416. /* Decodes the grouped exponents and stores them
  417. * in decoded exponents (dexps).
  418. * The code is derived from liba52.
  419. * Uses liba52 tables.
  420. */
  421. static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
  422. {
  423. int exps;
  424. while (ngrps--) {
  425. exps = get_bits(gb, 7);
  426. absexp += exp_1[exps];
  427. if (absexp > 24) {
  428. av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
  429. return -ngrps;
  430. }
  431. switch (expstr) {
  432. case AC3_EXPSTR_D45:
  433. *(dexps++) = absexp;
  434. *(dexps++) = absexp;
  435. case AC3_EXPSTR_D25:
  436. *(dexps++) = absexp;
  437. case AC3_EXPSTR_D15:
  438. *(dexps++) = absexp;
  439. }
  440. absexp += exp_2[exps];
  441. if (absexp > 24) {
  442. av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
  443. return -ngrps;
  444. }
  445. switch (expstr) {
  446. case AC3_EXPSTR_D45:
  447. *(dexps++) = absexp;
  448. *(dexps++) = absexp;
  449. case AC3_EXPSTR_D25:
  450. *(dexps++) = absexp;
  451. case AC3_EXPSTR_D15:
  452. *(dexps++) = absexp;
  453. }
  454. absexp += exp_3[exps];
  455. if (absexp > 24) {
  456. av_log(NULL, AV_LOG_ERROR, "Absolute Exponent > 24, ngrp = %d\n", ngrps);
  457. return -ngrps;
  458. }
  459. switch (expstr) {
  460. case AC3_EXPSTR_D45:
  461. *(dexps++) = absexp;
  462. *(dexps++) = absexp;
  463. case AC3_EXPSTR_D25:
  464. *(dexps++) = absexp;
  465. case AC3_EXPSTR_D15:
  466. *(dexps++) = absexp;
  467. }
  468. }
  469. return 0;
  470. }
  471. static inline int logadd(int a, int b)
  472. {
  473. int c = a - b;
  474. int address;
  475. address = FFMIN((ABS(c) >> 1), 255);
  476. if (c >= 0)
  477. return (a + latab[address]);
  478. else
  479. return (b + latab[address]);
  480. }
  481. static inline int calc_lowcomp(int a, int b0, int b1, int bin)
  482. {
  483. if (bin < 7) {
  484. if ((b0 + 256) == b1)
  485. a = 384;
  486. else if (b0 > b1)
  487. a = FFMAX(0, (a - 64));
  488. }
  489. else if (bin < 20) {
  490. if ((b0 + 256) == b1)
  491. a = 320;
  492. else if (b0 > b1)
  493. a = FFMAX(0, (a - 64));
  494. }
  495. else
  496. a = FFMAX(0, (a - 128));
  497. return a;
  498. }
  499. /* do the bit allocation for chnl.
  500. * chnl = 0 to 4 - fbw channel
  501. * chnl = 5 coupling channel
  502. * chnl = 6 lfe channel
  503. */
  504. static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
  505. {
  506. int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
  507. int sdecay, fdecay, sgain, dbknee, floor;
  508. int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
  509. int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
  510. int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
  511. int fscod = ctx->fscod;
  512. uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
  513. uint8_t *exps = 0, *bap = 0;
  514. /* initialization */
  515. sdecay = sdecaytab[ctx->sdcycod];
  516. fdecay = fdecaytab[ctx->fdcycod];
  517. sgain = sgaintab[ctx->sgaincod];
  518. dbknee = dbkneetab[ctx->dbpbcod];
  519. floor = floortab[ctx->floorcod];
  520. if (chnl == 5) {
  521. start = ctx->cplstrtmant;
  522. end = ctx->cplendmant;
  523. fgain = fgaintab[ctx->cplfgaincod];
  524. snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
  525. fastleak = (ctx->cplfleak << 8) + 768;
  526. slowleak = (ctx->cplsleak << 8) + 768;
  527. exps = ctx->dcplexps;
  528. bap = ctx->cplbap;
  529. if (ctx->cpldeltbae == AC3_DBASTR_NEW || ctx->deltbae == AC3_DBASTR_REUSE) {
  530. do_delta = 1;
  531. deltnseg = ctx->cpldeltnseg;
  532. deltoffst = ctx->cpldeltoffst;
  533. deltlen = ctx->cpldeltlen;
  534. deltba = ctx->cpldeltba;
  535. }
  536. }
  537. else if (chnl == 6) {
  538. start = 0;
  539. end = 7;
  540. lowcomp = 0;
  541. fastleak = 0;
  542. slowleak = 0;
  543. fgain = fgaintab[ctx->lfefgaincod];
  544. snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
  545. exps = ctx->dlfeexps;
  546. bap = ctx->lfebap;
  547. }
  548. else {
  549. start = 0;
  550. end = ctx->endmant[chnl];
  551. lowcomp = 0;
  552. fastleak = 0;
  553. slowleak = 0;
  554. fgain = fgaintab[ctx->fgaincod[chnl]];
  555. snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
  556. exps = ctx->dexps[chnl];
  557. bap = ctx->bap[chnl];
  558. if (ctx->deltbae[chnl] == AC3_DBASTR_NEW || ctx->deltbae[chnl] == AC3_DBASTR_REUSE) {
  559. do_delta = 1;
  560. deltnseg = ctx->deltnseg[chnl];
  561. deltoffst = ctx->deltoffst[chnl];
  562. deltlen = ctx->deltlen[chnl];
  563. deltba = ctx->deltba[chnl];
  564. }
  565. }
  566. for (bin = start; bin < end; bin++) /* exponent mapping into psd */
  567. psd[bin] = (3072 - (exps[bin] << 7));
  568. /* psd integration */
  569. j = start;
  570. k = masktab[start];
  571. do {
  572. lastbin = FFMIN((bndtab[k] + bndsz[k]), end);
  573. bndpsd[k] = psd[j];
  574. j++;
  575. for (i = j; i < lastbin; i++) {
  576. bndpsd[k] = logadd(bndpsd[k], psd[j]);
  577. j++;
  578. }
  579. k++;
  580. } while (end > lastbin);
  581. /* compute the excite function */
  582. bndstrt = masktab[start];
  583. bndend = masktab[end - 1] + 1;
  584. if (bndstrt == 0) {
  585. lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
  586. excite[0] = bndpsd[0] - fgain - lowcomp;
  587. lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
  588. excite[1] = bndpsd[1] - fgain - lowcomp;
  589. begin = 7;
  590. for (bin = 2; bin < 7; bin++) {
  591. if ((bndend != 7) || (bin != 6))
  592. lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
  593. fastleak = bndpsd[bin] - fgain;
  594. slowleak = bndpsd[bin] - sgain;
  595. excite[bin] = fastleak - lowcomp;
  596. if ((bndend != 7) || (bin != 6))
  597. if (bndpsd[bin] <= bndpsd[bin + 1]) {
  598. begin = bin + 1;
  599. break;
  600. }
  601. }
  602. for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
  603. if ((bndend != 7) || (bin != 6))
  604. lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
  605. fastleak -= fdecay;
  606. fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
  607. slowleak -= sdecay;
  608. slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
  609. excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
  610. }
  611. begin = 22;
  612. }
  613. else {
  614. begin = bndstrt;
  615. }
  616. for (bin = begin; bin < bndend; bin++) {
  617. fastleak -= fdecay;
  618. fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
  619. slowleak -= sdecay;
  620. slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
  621. excite[bin] = FFMAX(fastleak, slowleak);
  622. }
  623. /* compute the masking curve */
  624. for (bin = bndstrt; bin < bndend; bin++) {
  625. if (bndpsd[bin] < dbknee)
  626. excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
  627. mask[bin] = FFMAX(excite[bin], hth[bin][fscod]);
  628. }
  629. /* apply the delta bit allocation */
  630. if (do_delta) {
  631. band = 0;
  632. for (seg = 0; seg < deltnseg + 1; seg++) {
  633. band += deltoffst[seg];
  634. if (deltba[seg] >= 4)
  635. delta = (deltba[seg] - 3) << 7;
  636. else
  637. delta = (deltba[seg] - 4) << 7;
  638. for (k = 0; k < deltlen[seg]; k++) {
  639. mask[band] += delta;
  640. band++;
  641. }
  642. }
  643. }
  644. /*compute the bit allocation */
  645. i = start;
  646. j = masktab[start];
  647. do {
  648. lastbin = FFMIN((bndtab[j] + bndsz[j]), end);
  649. mask[j] -= snroffset;
  650. mask[j] -= floor;
  651. if (mask[j] < 0)
  652. mask[j] = 0;
  653. mask[j] &= 0x1fe0;
  654. mask[j] += floor;
  655. for (k = i; k < lastbin; k++) {
  656. address = (psd[i] - mask[j]) >> 5;
  657. address = FFMIN(63, (FFMAX(0, address)));
  658. bap[i] = baptab[address];
  659. i++;
  660. }
  661. j++;
  662. } while (end > lastbin);
  663. }
  664. /* Check if snroffsets are zero. */
  665. static int is_snr_offsets_zero(AC3DecodeContext *ctx)
  666. {
  667. int i;
  668. if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
  669. (ctx->lfeon && ctx->lfefsnroffst))
  670. return 0;
  671. for (i = 0; i < ctx->nfchans; i++)
  672. if (ctx->fsnroffst[i])
  673. return 0;
  674. return 1;
  675. }
  676. typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
  677. int16_t l3_quantizers[3];
  678. int16_t l5_quantizers[3];
  679. int16_t l11_quantizers[2];
  680. int l3ptr;
  681. int l5ptr;
  682. int l11ptr;
  683. } mant_groups;
  684. #define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
  685. /* Get the transform coefficients for coupling channel and uncouple channels.
  686. * The coupling transform coefficients starts at the the cplstrtmant, which is
  687. * equal to endmant[ch] for fbw channels. Hence we can uncouple channels before
  688. * getting transform coefficients for the channel.
  689. */
  690. static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
  691. {
  692. GetBitContext *gb = &ctx->gb;
  693. int ch, start, end, cplbndstrc, bnd, gcode, tbap;
  694. float cplcos[5], cplcoeff;
  695. uint8_t *exps = ctx->dcplexps;
  696. uint8_t *bap = ctx->cplbap;
  697. cplbndstrc = ctx->cplbndstrc;
  698. start = ctx->cplstrtmant;
  699. bnd = 0;
  700. while (start < ctx->cplendmant) {
  701. end = start + 12;
  702. while (cplbndstrc & 1) {
  703. end += 12;
  704. cplbndstrc >>= 1;
  705. }
  706. cplbndstrc >>= 1;
  707. for (ch = 0; ch < ctx->nfchans; ch++)
  708. cplcos[ch] = ctx->chcoeffs[ch] * ctx->cplco[ch][bnd];
  709. bnd++;
  710. while (start < end) {
  711. tbap = bap[start];
  712. switch(tbap) {
  713. case 0:
  714. for (ch = 0; ch < ctx->nfchans; ch++)
  715. if (((ctx->chincpl) >> ch) & 1) {
  716. if ((ctx->dithflag >> ch) & 1) {
  717. TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors);
  718. ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch] * LEVEL_MINUS_3DB;
  719. } else
  720. ctx->transform_coeffs[ch + 1][start] = 0;
  721. }
  722. start++;
  723. continue;
  724. case 1:
  725. if (m->l3ptr > 2) {
  726. gcode = get_bits(gb, 5);
  727. m->l3_quantizers[0] = l3_quantizers_1[gcode];
  728. m->l3_quantizers[1] = l3_quantizers_2[gcode];
  729. m->l3_quantizers[2] = l3_quantizers_3[gcode];
  730. m->l3ptr = 0;
  731. }
  732. TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
  733. break;
  734. case 2:
  735. if (m->l5ptr > 2) {
  736. gcode = get_bits(gb, 7);
  737. m->l5_quantizers[0] = l5_quantizers_1[gcode];
  738. m->l5_quantizers[1] = l5_quantizers_2[gcode];
  739. m->l5_quantizers[2] = l5_quantizers_3[gcode];
  740. m->l5ptr = 0;
  741. }
  742. TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
  743. break;
  744. case 3:
  745. TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
  746. break;
  747. case 4:
  748. if (m->l11ptr > 1) {
  749. gcode = get_bits(gb, 7);
  750. m->l11_quantizers[0] = l11_quantizers_1[gcode];
  751. m->l11_quantizers[1] = l11_quantizers_2[gcode];
  752. m->l11ptr = 0;
  753. }
  754. TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
  755. break;
  756. case 5:
  757. TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
  758. break;
  759. default:
  760. TRANSFORM_COEFF(cplcoeff, get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
  761. exps[start], scale_factors);
  762. }
  763. for (ch = 0; ch < ctx->nfchans; ch++)
  764. if ((ctx->chincpl >> ch) & 1)
  765. ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
  766. start++;
  767. }
  768. }
  769. return 0;
  770. }
  771. /* Get the transform coefficients for particular channel */
  772. static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
  773. {
  774. GetBitContext *gb = &ctx->gb;
  775. int i, gcode, tbap, dithflag, end;
  776. uint8_t *exps;
  777. uint8_t *bap;
  778. float *coeffs;
  779. float factors[25];
  780. for (i = 0; i < 25; i++)
  781. factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
  782. if (ch_index != -1) { /* fbw channels */
  783. dithflag = (ctx->dithflag >> ch_index) & 1;
  784. exps = ctx->dexps[ch_index];
  785. bap = ctx->bap[ch_index];
  786. coeffs = ctx->transform_coeffs[ch_index + 1];
  787. end = ctx->endmant[ch_index];
  788. } else if (ch_index == -1) {
  789. dithflag = 0;
  790. exps = ctx->dlfeexps;
  791. bap = ctx->lfebap;
  792. coeffs = ctx->transform_coeffs[0];
  793. end = 7;
  794. }
  795. for (i = 0; i < end; i++) {
  796. tbap = bap[i];
  797. switch (tbap) {
  798. case 0:
  799. if (!dithflag) {
  800. coeffs[i] = 0;
  801. continue;
  802. }
  803. else {
  804. TRANSFORM_COEFF(coeffs[i], dither_int16(&ctx->dith_state), exps[i], factors);
  805. coeffs[i] *= LEVEL_MINUS_3DB;
  806. continue;
  807. }
  808. case 1:
  809. if (m->l3ptr > 2) {
  810. gcode = get_bits(gb, 5);
  811. m->l3_quantizers[0] = l3_quantizers_1[gcode];
  812. m->l3_quantizers[1] = l3_quantizers_2[gcode];
  813. m->l3_quantizers[2] = l3_quantizers_3[gcode];
  814. m->l3ptr = 0;
  815. }
  816. TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
  817. continue;
  818. case 2:
  819. if (m->l5ptr > 2) {
  820. gcode = get_bits(gb, 7);
  821. m->l5_quantizers[0] = l5_quantizers_1[gcode];
  822. m->l5_quantizers[1] = l5_quantizers_2[gcode];
  823. m->l5_quantizers[2] = l5_quantizers_3[gcode];
  824. m->l5ptr = 0;
  825. }
  826. TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
  827. continue;
  828. case 3:
  829. TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
  830. continue;
  831. case 4:
  832. if (m->l11ptr > 1) {
  833. gcode = get_bits(gb, 7);
  834. m->l11_quantizers[0] = l11_quantizers_1[gcode];
  835. m->l11_quantizers[1] = l11_quantizers_2[gcode];
  836. m->l11ptr = 0;
  837. }
  838. TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
  839. continue;
  840. case 5:
  841. TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
  842. continue;
  843. default:
  844. TRANSFORM_COEFF(coeffs[i], get_sbits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
  845. continue;
  846. }
  847. }
  848. return 0;
  849. }
  850. static int get_transform_coeffs(AC3DecodeContext * ctx)
  851. {
  852. int i, end;
  853. int got_cplchan = 0;
  854. mant_groups m;
  855. m.l3ptr = m.l5ptr = m.l11ptr = 3;
  856. for (i = 0; i < ctx->nfchans; i++) {
  857. /* transform coefficients for individual channel */
  858. if (get_transform_coeffs_ch(ctx, i, &m))
  859. return -1;
  860. /* tranform coefficients for coupling channels */
  861. if ((ctx->chincpl >> i) & 1) {
  862. if (!got_cplchan) {
  863. if (get_transform_coeffs_cpling(ctx, &m)) {
  864. av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
  865. return -1;
  866. }
  867. got_cplchan = 1;
  868. }
  869. end = ctx->cplendmant;
  870. } else
  871. end = ctx->endmant[i];
  872. do
  873. ctx->transform_coeffs[i + 1][end] = 0;
  874. while(++end < 256);
  875. }
  876. if (ctx->lfeon) {
  877. if (get_transform_coeffs_ch(ctx, -1, &m))
  878. return -1;
  879. for (i = 7; i < 256; i++) {
  880. ctx->transform_coeffs[0][i] = 0;
  881. }
  882. }
  883. return 0;
  884. }
  885. static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
  886. {
  887. float tmp0, tmp1;
  888. while (start < end) {
  889. tmp0 = ctx->transform_coeffs[1][start];
  890. tmp1 = ctx->transform_coeffs[2][start];
  891. ctx->transform_coeffs[1][start] = tmp0 + tmp1;
  892. ctx->transform_coeffs[2][start] = tmp0 - tmp1;
  893. start++;
  894. }
  895. }
  896. static void do_rematrixing(AC3DecodeContext *ctx)
  897. {
  898. int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
  899. int end, bndend;
  900. end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
  901. if (ctx->rematflg & 1)
  902. do_rematrixing1(ctx, bnd1, bnd2);
  903. if (ctx->rematflg & 2)
  904. do_rematrixing1(ctx, bnd2, bnd3);
  905. bndend = bnd4;
  906. if (bndend > end) {
  907. bndend = end;
  908. if (ctx->rematflg & 4)
  909. do_rematrixing1(ctx, bnd3, bndend);
  910. } else {
  911. if (ctx->rematflg & 4)
  912. do_rematrixing1(ctx, bnd3, bnd4);
  913. if (ctx->rematflg & 8)
  914. do_rematrixing1(ctx, bnd4, end);
  915. }
  916. }
  917. static void get_downmix_coeffs(AC3DecodeContext *ctx)
  918. {
  919. int from = ctx->acmod;
  920. int to = ctx->blkoutput;
  921. float clev = clevs[ctx->cmixlev];
  922. float slev = slevs[ctx->surmixlev];
  923. float nf = 1.0; //normalization factor for downmix coeffs
  924. if (to == AC3_OUTPUT_UNMODIFIED)
  925. return;
  926. switch (from) {
  927. case AC3_INPUT_DUALMONO:
  928. switch (to) {
  929. case AC3_OUTPUT_MONO:
  930. case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
  931. nf = 0.5;
  932. ctx->chcoeffs[0] *= nf;
  933. ctx->chcoeffs[1] *= nf;
  934. break;
  935. }
  936. break;
  937. case AC3_INPUT_MONO:
  938. switch (to) {
  939. case AC3_OUTPUT_STEREO:
  940. nf = LEVEL_MINUS_3DB;
  941. ctx->chcoeffs[0] *= nf;
  942. break;
  943. }
  944. break;
  945. case AC3_INPUT_STEREO:
  946. switch (to) {
  947. case AC3_OUTPUT_MONO:
  948. nf = LEVEL_MINUS_3DB;
  949. ctx->chcoeffs[0] *= nf;
  950. ctx->chcoeffs[1] *= nf;
  951. break;
  952. }
  953. break;
  954. case AC3_INPUT_3F:
  955. switch (to) {
  956. case AC3_OUTPUT_MONO:
  957. nf = LEVEL_MINUS_3DB / (1.0 + clev);
  958. ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
  959. ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
  960. ctx->chcoeffs[1] *= ((nf * clev * LEVEL_MINUS_3DB) / 2.0);
  961. break;
  962. case AC3_OUTPUT_STEREO:
  963. nf = 1.0 / (1.0 + clev);
  964. ctx->chcoeffs[0] *= nf;
  965. ctx->chcoeffs[2] *= nf;
  966. ctx->chcoeffs[1] *= (nf * clev);
  967. break;
  968. }
  969. break;
  970. case AC3_INPUT_2F_1R:
  971. switch (to) {
  972. case AC3_OUTPUT_MONO:
  973. nf = 2.0 * LEVEL_MINUS_3DB / (2.0 + slev);
  974. ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
  975. ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
  976. ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
  977. break;
  978. case AC3_OUTPUT_STEREO:
  979. nf = 1.0 / (1.0 + (slev * LEVEL_MINUS_3DB));
  980. ctx->chcoeffs[0] *= nf;
  981. ctx->chcoeffs[1] *= nf;
  982. ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
  983. break;
  984. case AC3_OUTPUT_DOLBY:
  985. nf = 1.0 / (1.0 + LEVEL_MINUS_3DB);
  986. ctx->chcoeffs[0] *= nf;
  987. ctx->chcoeffs[1] *= nf;
  988. ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
  989. break;
  990. }
  991. break;
  992. case AC3_INPUT_3F_1R:
  993. switch (to) {
  994. case AC3_OUTPUT_MONO:
  995. nf = LEVEL_MINUS_3DB / (1.0 + clev + (slev / 2.0));
  996. ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
  997. ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
  998. ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
  999. ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
  1000. break;
  1001. case AC3_OUTPUT_STEREO:
  1002. nf = 1.0 / (1.0 + clev + (slev * LEVEL_MINUS_3DB));
  1003. ctx->chcoeffs[0] *= nf;
  1004. ctx->chcoeffs[2] *= nf;
  1005. ctx->chcoeffs[1] *= (nf * clev);
  1006. ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
  1007. break;
  1008. case AC3_OUTPUT_DOLBY:
  1009. nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
  1010. ctx->chcoeffs[0] *= nf;
  1011. ctx->chcoeffs[1] *= nf;
  1012. ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
  1013. ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
  1014. break;
  1015. }
  1016. break;
  1017. case AC3_INPUT_2F_2R:
  1018. switch (to) {
  1019. case AC3_OUTPUT_MONO:
  1020. nf = LEVEL_MINUS_3DB / (1.0 + slev);
  1021. ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
  1022. ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
  1023. ctx->chcoeffs[2] *= (nf * slev * LEVEL_MINUS_3DB);
  1024. ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
  1025. break;
  1026. case AC3_OUTPUT_STEREO:
  1027. nf = 1.0 / (1.0 + slev);
  1028. ctx->chcoeffs[0] *= nf;
  1029. ctx->chcoeffs[1] *= nf;
  1030. ctx->chcoeffs[2] *= (nf * slev);
  1031. ctx->chcoeffs[3] *= (nf * slev);
  1032. break;
  1033. case AC3_OUTPUT_DOLBY:
  1034. nf = 1.0 / (1.0 + (2.0 * LEVEL_MINUS_3DB));
  1035. ctx->chcoeffs[0] *= nf;
  1036. ctx->chcoeffs[1] *= nf;
  1037. ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
  1038. ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
  1039. break;
  1040. }
  1041. break;
  1042. case AC3_INPUT_3F_2R:
  1043. switch (to) {
  1044. case AC3_OUTPUT_MONO:
  1045. nf = LEVEL_MINUS_3DB / (1.0 + clev + slev);
  1046. ctx->chcoeffs[0] *= (nf * LEVEL_MINUS_3DB);
  1047. ctx->chcoeffs[2] *= (nf * LEVEL_MINUS_3DB);
  1048. ctx->chcoeffs[1] *= (nf * clev * LEVEL_PLUS_3DB);
  1049. ctx->chcoeffs[3] *= (nf * slev * LEVEL_MINUS_3DB);
  1050. ctx->chcoeffs[4] *= (nf * slev * LEVEL_MINUS_3DB);
  1051. break;
  1052. case AC3_OUTPUT_STEREO:
  1053. nf = 1.0 / (1.0 + clev + slev);
  1054. ctx->chcoeffs[0] *= nf;
  1055. ctx->chcoeffs[2] *= nf;
  1056. ctx->chcoeffs[1] *= (nf * clev);
  1057. ctx->chcoeffs[3] *= (nf * slev);
  1058. ctx->chcoeffs[4] *= (nf * slev);
  1059. break;
  1060. case AC3_OUTPUT_DOLBY:
  1061. nf = 1.0 / (1.0 + (3.0 * LEVEL_MINUS_3DB));
  1062. ctx->chcoeffs[0] *= nf;
  1063. ctx->chcoeffs[1] *= nf;
  1064. ctx->chcoeffs[1] *= (nf * LEVEL_MINUS_3DB);
  1065. ctx->chcoeffs[3] *= (nf * LEVEL_MINUS_3DB);
  1066. ctx->chcoeffs[4] *= (nf * LEVEL_MINUS_3DB);
  1067. break;
  1068. }
  1069. break;
  1070. }
  1071. }
  1072. static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
  1073. {
  1074. int i;
  1075. float (*output)[BLOCK_SIZE] = ctx->output;
  1076. for (i = 0; i < 256; i++)
  1077. output[1][i] += output[2][i];
  1078. memset(output[2], 0, sizeof(output[2]));
  1079. }
  1080. static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
  1081. {
  1082. int i;
  1083. float tmp;
  1084. float (*output)[BLOCK_SIZE] = ctx->output;
  1085. for (i = 0; i < 256; i++) {
  1086. tmp = output[1][i] + output[2][i];
  1087. output[1][i] = output[2][i] = tmp;
  1088. }
  1089. }
  1090. static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
  1091. {
  1092. int i;
  1093. float (*output)[BLOCK_SIZE] = ctx->output;
  1094. for (i = 0; i < 256; i++)
  1095. output[2][i] = output[1][i];
  1096. }
  1097. static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
  1098. {
  1099. int i;
  1100. float (*output)[BLOCK_SIZE] = ctx->output;
  1101. for (i = 0; i < 256; i++)
  1102. output[1][i] += output[2][i];
  1103. memset(output[2], 0, sizeof(output[2]));
  1104. }
  1105. static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
  1106. {
  1107. int i;
  1108. float (*output)[BLOCK_SIZE] = ctx->output;
  1109. for (i = 0; i < 256; i++)
  1110. output[1][i] += (output[2][i] + output[3][i]);
  1111. memset(output[2], 0, sizeof(output[2]));
  1112. memset(output[3], 0, sizeof(output[3]));
  1113. }
  1114. static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
  1115. {
  1116. int i;
  1117. float (*output)[BLOCK_SIZE] = ctx->output;
  1118. for (i = 0; i < 256; i++) {
  1119. output[1][i] += output[2][i];
  1120. output[2][i] += output[3][i];
  1121. }
  1122. memset(output[3], 0, sizeof(output[3]));
  1123. }
  1124. static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
  1125. {
  1126. int i;
  1127. float (*output)[BLOCK_SIZE] = ctx->output;
  1128. for (i = 0; i < 256; i++)
  1129. output[1][i] += (output[2][i] + output[3][i]);
  1130. memset(output[2], 0, sizeof(output[2]));
  1131. memset(output[3], 0, sizeof(output[3]));
  1132. }
  1133. static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
  1134. {
  1135. int i;
  1136. float (*output)[BLOCK_SIZE] = ctx->output;
  1137. for (i = 0; i < 256; i++) {
  1138. output[1][i] += output[2][i];
  1139. output[2][i] += output[3][i];
  1140. }
  1141. memset(output[3], 0, sizeof(output[3]));
  1142. }
  1143. static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
  1144. {
  1145. int i;
  1146. float (*output)[BLOCK_SIZE] = ctx->output;
  1147. for (i = 0; i < 256; i++) {
  1148. output[1][i] -= output[3][i];
  1149. output[2][i] += output[3][i];
  1150. }
  1151. memset(output[3], 0, sizeof(output[3]));
  1152. }
  1153. static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
  1154. {
  1155. int i;
  1156. float (*output)[BLOCK_SIZE] = ctx->output;
  1157. for (i = 0; i < 256; i++)
  1158. output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
  1159. memset(output[2], 0, sizeof(output[2]));
  1160. memset(output[3], 0, sizeof(output[3]));
  1161. memset(output[4], 0, sizeof(output[4]));
  1162. }
  1163. static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
  1164. {
  1165. int i;
  1166. float (*output)[BLOCK_SIZE] = ctx->output;
  1167. for (i = 0; i < 256; i++) {
  1168. output[1][i] += (output[2][i] + output[4][i]);
  1169. output[2][i] += (output[3][i] + output[4][i]);
  1170. }
  1171. memset(output[3], 0, sizeof(output[3]));
  1172. memset(output[4], 0, sizeof(output[4]));
  1173. }
  1174. static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
  1175. {
  1176. int i;
  1177. float (*output)[BLOCK_SIZE] = ctx->output;
  1178. for (i = 0; i < 256; i++) {
  1179. output[1][i] += (output[2][i] - output[4][i]);
  1180. output[2][i] += (output[3][i] + output[4][i]);
  1181. }
  1182. memset(output[3], 0, sizeof(output[3]));
  1183. memset(output[4], 0, sizeof(output[4]));
  1184. }
  1185. static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
  1186. {
  1187. int i;
  1188. float (*output)[BLOCK_SIZE] = ctx->output;
  1189. for (i = 0; i < 256; i++)
  1190. output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
  1191. memset(output[2], 0, sizeof(output[2]));
  1192. memset(output[3], 0, sizeof(output[3]));
  1193. memset(output[4], 0, sizeof(output[4]));
  1194. }
  1195. static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
  1196. {
  1197. int i;
  1198. float (*output)[BLOCK_SIZE] = ctx->output;
  1199. for (i = 0; i < 256; i++) {
  1200. output[1][i] += output[3][i];
  1201. output[2][i] += output[4][i];
  1202. }
  1203. memset(output[3], 0, sizeof(output[3]));
  1204. memset(output[4], 0, sizeof(output[4]));
  1205. }
  1206. static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
  1207. {
  1208. int i;
  1209. float (*output)[BLOCK_SIZE] = ctx->output;
  1210. for (i = 0; i < 256; i++) {
  1211. output[1][i] -= output[3][i];
  1212. output[2][i] += output[4][i];
  1213. }
  1214. memset(output[3], 0, sizeof(output[3]));
  1215. memset(output[4], 0, sizeof(output[4]));
  1216. }
  1217. static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
  1218. {
  1219. int i;
  1220. float (*output)[BLOCK_SIZE] = ctx->output;
  1221. for (i = 0; i < 256; i++)
  1222. output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
  1223. memset(output[2], 0, sizeof(output[2]));
  1224. memset(output[3], 0, sizeof(output[3]));
  1225. memset(output[4], 0, sizeof(output[4]));
  1226. memset(output[5], 0, sizeof(output[5]));
  1227. }
  1228. static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
  1229. {
  1230. int i;
  1231. float (*output)[BLOCK_SIZE] = ctx->output;
  1232. for (i = 0; i < 256; i++) {
  1233. output[1][i] += (output[2][i] + output[4][i]);
  1234. output[2][i] += (output[3][i] + output[5][i]);
  1235. }
  1236. memset(output[3], 0, sizeof(output[3]));
  1237. memset(output[4], 0, sizeof(output[4]));
  1238. memset(output[5], 0, sizeof(output[5]));
  1239. }
  1240. static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
  1241. {
  1242. int i;
  1243. float (*output)[BLOCK_SIZE] = ctx->output;
  1244. for (i = 0; i < 256; i++) {
  1245. output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
  1246. output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
  1247. }
  1248. memset(output[3], 0, sizeof(output[3]));
  1249. memset(output[4], 0, sizeof(output[4]));
  1250. memset(output[5], 0, sizeof(output[5]));
  1251. }
  1252. static void do_downmix(AC3DecodeContext *ctx)
  1253. {
  1254. int from = ctx->acmod;
  1255. int to = ctx->blkoutput;
  1256. switch (from) {
  1257. case AC3_INPUT_DUALMONO:
  1258. switch (to) {
  1259. case AC3_OUTPUT_MONO:
  1260. mix_dualmono_to_mono(ctx);
  1261. break;
  1262. case AC3_OUTPUT_STEREO: /* We assume that sum of both mono channels is requested */
  1263. mix_dualmono_to_stereo(ctx);
  1264. break;
  1265. }
  1266. break;
  1267. case AC3_INPUT_MONO:
  1268. switch (to) {
  1269. case AC3_OUTPUT_STEREO:
  1270. upmix_mono_to_stereo(ctx);
  1271. break;
  1272. }
  1273. break;
  1274. case AC3_INPUT_STEREO:
  1275. switch (to) {
  1276. case AC3_OUTPUT_MONO:
  1277. mix_stereo_to_mono(ctx);
  1278. break;
  1279. }
  1280. break;
  1281. case AC3_INPUT_3F:
  1282. switch (to) {
  1283. case AC3_OUTPUT_MONO:
  1284. mix_3f_to_mono(ctx);
  1285. break;
  1286. case AC3_OUTPUT_STEREO:
  1287. mix_3f_to_stereo(ctx);
  1288. break;
  1289. }
  1290. break;
  1291. case AC3_INPUT_2F_1R:
  1292. switch (to) {
  1293. case AC3_OUTPUT_MONO:
  1294. mix_2f_1r_to_mono(ctx);
  1295. break;
  1296. case AC3_OUTPUT_STEREO:
  1297. mix_2f_1r_to_stereo(ctx);
  1298. break;
  1299. case AC3_OUTPUT_DOLBY:
  1300. mix_2f_1r_to_dolby(ctx);
  1301. break;
  1302. }
  1303. break;
  1304. case AC3_INPUT_3F_1R:
  1305. switch (to) {
  1306. case AC3_OUTPUT_MONO:
  1307. mix_3f_1r_to_mono(ctx);
  1308. break;
  1309. case AC3_OUTPUT_STEREO:
  1310. mix_3f_1r_to_stereo(ctx);
  1311. break;
  1312. case AC3_OUTPUT_DOLBY:
  1313. mix_3f_1r_to_dolby(ctx);
  1314. break;
  1315. }
  1316. break;
  1317. case AC3_INPUT_2F_2R:
  1318. switch (to) {
  1319. case AC3_OUTPUT_MONO:
  1320. mix_2f_2r_to_mono(ctx);
  1321. break;
  1322. case AC3_OUTPUT_STEREO:
  1323. mix_2f_2r_to_stereo(ctx);
  1324. break;
  1325. case AC3_OUTPUT_DOLBY:
  1326. mix_2f_2r_to_dolby(ctx);
  1327. break;
  1328. }
  1329. break;
  1330. case AC3_INPUT_3F_2R:
  1331. switch (to) {
  1332. case AC3_OUTPUT_MONO:
  1333. mix_3f_2r_to_mono(ctx);
  1334. break;
  1335. case AC3_OUTPUT_STEREO:
  1336. mix_3f_2r_to_stereo(ctx);
  1337. break;
  1338. case AC3_OUTPUT_DOLBY:
  1339. mix_3f_2r_to_dolby(ctx);
  1340. break;
  1341. }
  1342. break;
  1343. }
  1344. }
  1345. static void dump_floats(const char *name, int prec, const float *tab, int n)
  1346. {
  1347. int i;
  1348. av_log(NULL, AV_LOG_INFO, "%s[%d]:\n", name, n);
  1349. for(i=0;i<n;i++) {
  1350. if ((i & 7) == 0)
  1351. av_log(NULL, AV_LOG_INFO, "%4d: ", i);
  1352. av_log(NULL, AV_LOG_INFO, " %8.*f", prec, tab[i]);
  1353. if ((i & 7) == 7)
  1354. av_log(NULL, AV_LOG_INFO, "\n");
  1355. }
  1356. if ((i & 7) != 0)
  1357. av_log(NULL, AV_LOG_INFO, "\n");
  1358. }
  1359. #define CMUL(pre, pim, are, aim, bre, bim) \
  1360. {\
  1361. float _are = (are);\
  1362. float _aim = (aim);\
  1363. float _bre = (bre);\
  1364. float _bim = (bim);\
  1365. (pre) = _are * _bre - _aim * _bim;\
  1366. (pim) = _are * _bim + _aim * _bre;\
  1367. }
  1368. static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
  1369. {
  1370. int k;
  1371. float x1[128], x2[128];
  1372. for (k = 0; k < N / 4; k++) {
  1373. x1[k] = ctx->transform_coeffs[chindex][2 * k];
  1374. x2[k] = ctx->transform_coeffs[chindex][2 * k + 1];
  1375. }
  1376. ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
  1377. ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
  1378. for (k = 0; k < N / 2; k++) {
  1379. ctx->output[chindex][k] = ctx->tmp_output[k] * window[k] + ctx->delay[chindex][k];
  1380. //dump_floats("samples", 10, ctx->output[chindex], 256);
  1381. ctx->delay[chindex][k] = ctx->tmp_output[N / 2 + k] * window[255 - k];
  1382. }
  1383. }
  1384. static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
  1385. {
  1386. int k;
  1387. ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output,
  1388. ctx->transform_coeffs[chindex], ctx->tmp_imdct);
  1389. //ff_imdct_calc_ac3_512(&ctx->imdct_512, ctx->tmp_output, ctx->transform_coeffs[chindex],
  1390. // ctx->tmp_imdct, window);
  1391. for (k = 0; k < N / 2; k++) {
  1392. ctx->output[chindex][k] = ctx->tmp_output[k] * window[k] + ctx->delay[chindex][k];
  1393. //dump_floats("samples", 10, ctx->output[chindex], 256);
  1394. ctx->delay[chindex][k] = ctx->tmp_output[N / 2 + k] * window[255 - k];
  1395. }
  1396. }
  1397. static inline void do_imdct(AC3DecodeContext *ctx)
  1398. {
  1399. int i;
  1400. if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
  1401. do_imdct_512(ctx, 0);
  1402. }
  1403. for (i = 0; i < ctx->nfchans; i++) {
  1404. if ((ctx->blksw >> i) & 1)
  1405. do_imdct_256(ctx, i + 1);
  1406. else
  1407. do_imdct_512(ctx, i + 1);
  1408. }
  1409. }
  1410. static int ac3_parse_audio_block(AC3DecodeContext * ctx)
  1411. {
  1412. int nfchans = ctx->nfchans;
  1413. int acmod = ctx->acmod;
  1414. int i, bnd, rbnd, seg, grpsize;
  1415. GetBitContext *gb = &ctx->gb;
  1416. int bit_alloc_flags = 0;
  1417. float drange;
  1418. uint8_t *dexps;
  1419. int mstrcplco, cplcoexp, cplcomant;
  1420. int dynrng, chbwcod, ngrps, cplabsexp, skipl;
  1421. for (i = 0; i < 5; i++)
  1422. ctx->chcoeffs[i] = 2.0;
  1423. ctx->blksw = 0;
  1424. for (i = 0; i < nfchans; i++) /*block switch flag */
  1425. ctx->blksw |= get_bits1(gb) << i;
  1426. ctx->dithflag = 0;
  1427. for (i = 0; i < nfchans; i++) /* dithering flag */
  1428. ctx->dithflag |= get_bits1(gb) << i;
  1429. if (get_bits1(gb)) { /* dynamic range */
  1430. dynrng = get_sbits(gb, 8);
  1431. drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
  1432. for (i = 0; i < nfchans; i++)
  1433. ctx->chcoeffs[i] *= drange;
  1434. }
  1435. if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
  1436. dynrng = get_sbits(gb, 8);
  1437. drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
  1438. ctx->chcoeffs[1] *= drange;
  1439. }
  1440. get_downmix_coeffs(ctx);
  1441. if (get_bits1(gb)) { /* coupling strategy */
  1442. ctx->cplinu = get_bits1(gb);
  1443. ctx->cplbndstrc = 0;
  1444. ctx->chincpl = 0;
  1445. if (ctx->cplinu) { /* coupling in use */
  1446. for (i = 0; i < nfchans; i++)
  1447. ctx->chincpl |= get_bits1(gb) << i;
  1448. if (acmod == 0x02)
  1449. ctx->phsflginu = get_bits1(gb); //phase flag in use
  1450. ctx->cplbegf = get_bits(gb, 4);
  1451. ctx->cplendf = get_bits(gb, 4);
  1452. if (3 + ctx->cplendf - ctx->cplbegf < 0) {
  1453. av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
  1454. return -1;
  1455. }
  1456. ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
  1457. ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
  1458. ctx->cplendmant = ctx->cplendf * 12 + 73;
  1459. for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
  1460. if (get_bits1(gb)) {
  1461. ctx->cplbndstrc |= 1 << i;
  1462. ctx->ncplbnd--;
  1463. }
  1464. }
  1465. }
  1466. if (ctx->cplinu) {
  1467. ctx->cplcoe = 0;
  1468. for (i = 0; i < nfchans; i++)
  1469. if ((ctx->chincpl) >> i & 1)
  1470. if (get_bits1(gb)) { /* coupling co-ordinates */
  1471. ctx->cplcoe |= 1 << i;
  1472. mstrcplco = 3 * get_bits(gb, 2);
  1473. for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
  1474. cplcoexp = get_bits(gb, 4);
  1475. cplcomant = get_bits(gb, 4);
  1476. if (cplcoexp == 15)
  1477. cplcomant <<= 14;
  1478. else
  1479. cplcomant = (cplcomant | 0x10) << 13;
  1480. ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
  1481. }
  1482. }
  1483. if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
  1484. for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
  1485. if (get_bits1(gb))
  1486. ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
  1487. }
  1488. if (acmod == 0x02) {/* rematrixing */
  1489. ctx->rematstr = get_bits1(gb);
  1490. if (ctx->rematstr) {
  1491. ctx->rematflg = 0;
  1492. if (!(ctx->cplinu) || ctx->cplbegf > 2)
  1493. for (rbnd = 0; rbnd < 4; rbnd++)
  1494. ctx->rematflg |= get_bits1(gb) << rbnd;
  1495. if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
  1496. for (rbnd = 0; rbnd < 3; rbnd++)
  1497. ctx->rematflg |= get_bits1(gb) << rbnd;
  1498. if (ctx->cplbegf == 0 && ctx->cplinu)
  1499. for (rbnd = 0; rbnd < 2; rbnd++)
  1500. ctx->rematflg |= get_bits1(gb) << rbnd;
  1501. }
  1502. }
  1503. ctx->cplexpstr = AC3_EXPSTR_REUSE;
  1504. ctx->lfeexpstr = AC3_EXPSTR_REUSE;
  1505. if (ctx->cplinu) /* coupling exponent strategy */
  1506. ctx->cplexpstr = get_bits(gb, 2);
  1507. for (i = 0; i < nfchans; i++) /* channel exponent strategy */
  1508. ctx->chexpstr[i] = get_bits(gb, 2);
  1509. if (ctx->lfeon) /* lfe exponent strategy */
  1510. ctx->lfeexpstr = get_bits1(gb);
  1511. for (i = 0; i < nfchans; i++) /* channel bandwidth code */
  1512. if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
  1513. if ((ctx->chincpl >> i) & 1)
  1514. ctx->endmant[i] = ctx->cplstrtmant;
  1515. else {
  1516. chbwcod = get_bits(gb, 6);
  1517. if (chbwcod > 60) {
  1518. av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
  1519. return -1;
  1520. }
  1521. ctx->endmant[i] = chbwcod * 3 + 73;
  1522. }
  1523. }
  1524. if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
  1525. bit_alloc_flags = 64;
  1526. cplabsexp = get_bits(gb, 4) << 1;
  1527. ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
  1528. if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
  1529. av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
  1530. return -1;
  1531. }
  1532. }
  1533. for (i = 0; i < nfchans; i++) /* fbw channel exponents */
  1534. if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
  1535. bit_alloc_flags |= 1 << i;
  1536. grpsize = 3 << (ctx->chexpstr[i] - 1);
  1537. ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
  1538. dexps = ctx->dexps[i];
  1539. dexps[0] = get_bits(gb, 4);
  1540. if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
  1541. av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
  1542. return -1;
  1543. }
  1544. skip_bits(gb, 2); /* skip gainrng */
  1545. }
  1546. if (ctx->lfeexpstr != AC3_EXPSTR_REUSE) { /* lfe exponents */
  1547. bit_alloc_flags |= 32;
  1548. ctx->dlfeexps[0] = get_bits(gb, 4);
  1549. if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
  1550. av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
  1551. return -1;
  1552. }
  1553. }
  1554. if (get_bits1(gb)) { /* bit allocation information */
  1555. bit_alloc_flags = 127;
  1556. ctx->sdcycod = get_bits(gb, 2);
  1557. ctx->fdcycod = get_bits(gb, 2);
  1558. ctx->sgaincod = get_bits(gb, 2);
  1559. ctx->dbpbcod = get_bits(gb, 2);
  1560. ctx->floorcod = get_bits(gb, 3);
  1561. }
  1562. if (get_bits1(gb)) { /* snroffset */
  1563. bit_alloc_flags = 127;
  1564. ctx->csnroffst = get_bits(gb, 6);
  1565. if (ctx->cplinu) { /* couling fine snr offset and fast gain code */
  1566. ctx->cplfsnroffst = get_bits(gb, 4);
  1567. ctx->cplfgaincod = get_bits(gb, 3);
  1568. }
  1569. for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
  1570. ctx->fsnroffst[i] = get_bits(gb, 4);
  1571. ctx->fgaincod[i] = get_bits(gb, 3);
  1572. }
  1573. if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
  1574. ctx->lfefsnroffst = get_bits(gb, 4);
  1575. ctx->lfefgaincod = get_bits(gb, 3);
  1576. }
  1577. }
  1578. if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
  1579. bit_alloc_flags |= 64;
  1580. ctx->cplfleak = get_bits(gb, 3);
  1581. ctx->cplsleak = get_bits(gb, 3);
  1582. }
  1583. if (get_bits1(gb)) { /* delta bit allocation information */
  1584. bit_alloc_flags = 127;
  1585. if (ctx->cplinu) {
  1586. ctx->cpldeltbae = get_bits(gb, 2);
  1587. if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) {
  1588. av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
  1589. return -1;
  1590. }
  1591. }
  1592. for (i = 0; i < nfchans; i++) {
  1593. ctx->deltbae[i] = get_bits(gb, 2);
  1594. if (ctx->deltbae[i] == AC3_DBASTR_RESERVED) {
  1595. av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
  1596. return -1;
  1597. }
  1598. }
  1599. if (ctx->cplinu)
  1600. if (ctx->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
  1601. ctx->cpldeltnseg = get_bits(gb, 3);
  1602. for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
  1603. ctx->cpldeltoffst[seg] = get_bits(gb, 5);
  1604. ctx->cpldeltlen[seg] = get_bits(gb, 4);
  1605. ctx->cpldeltba[seg] = get_bits(gb, 3);
  1606. }
  1607. }
  1608. for (i = 0; i < nfchans; i++)
  1609. if (ctx->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
  1610. ctx->deltnseg[i] = get_bits(gb, 3);
  1611. for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
  1612. ctx->deltoffst[i][seg] = get_bits(gb, 5);
  1613. ctx->deltlen[i][seg] = get_bits(gb, 4);
  1614. ctx->deltba[i][seg] = get_bits(gb, 3);
  1615. }
  1616. }
  1617. }
  1618. if (bit_alloc_flags) {
  1619. if (is_snr_offsets_zero(ctx)) {
  1620. memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
  1621. memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
  1622. for (i = 0; i < nfchans; i++)
  1623. memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
  1624. } else {
  1625. if (ctx->chincpl && (bit_alloc_flags & 64))
  1626. do_bit_allocation(ctx, 5);
  1627. for (i = 0; i < nfchans; i++)
  1628. if ((bit_alloc_flags >> i) & 1)
  1629. do_bit_allocation(ctx, i);
  1630. if (ctx->lfeon && (bit_alloc_flags & 32))
  1631. do_bit_allocation(ctx, 6);
  1632. }
  1633. }
  1634. if (get_bits1(gb)) { /* unused dummy data */
  1635. skipl = get_bits(gb, 9);
  1636. while(skipl--)
  1637. skip_bits(gb, 8);
  1638. }
  1639. /* unpack the transform coefficients
  1640. * * this also uncouples channels if coupling is in use.
  1641. */
  1642. if (get_transform_coeffs(ctx)) {
  1643. av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
  1644. return -1;
  1645. }
  1646. /*for (i = 0; i < nfchans; i++)
  1647. dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
  1648. /* recover coefficients if rematrixing is in use */
  1649. if (ctx->rematflg)
  1650. do_rematrixing(ctx);
  1651. do_imdct(ctx);
  1652. /*for(i = 0; i < nfchans; i++)
  1653. dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
  1654. do_downmix(ctx);
  1655. return 0;
  1656. }
  1657. static inline int16_t convert(float f)
  1658. {
  1659. if (f >= 1.0)
  1660. return 32767;
  1661. else if (f <= -1.0)
  1662. return -32768;
  1663. else
  1664. return (lrintf(f * 32767.0));
  1665. }
  1666. static int frame_count = 0;
  1667. static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  1668. {
  1669. AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
  1670. int frame_start;
  1671. int16_t *out_samples = (int16_t *)data;
  1672. int i, j, k, value;
  1673. av_log(NULL, AV_LOG_INFO, "decoding frame %d buf_size = %d\n", frame_count++, buf_size);
  1674. //Synchronize the frame.
  1675. frame_start = ac3_synchronize(buf, buf_size);
  1676. if (frame_start == -1) {
  1677. av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
  1678. *data_size = 0;
  1679. return buf_size;
  1680. }
  1681. //Initialize the GetBitContext with the start of valid AC3 Frame.
  1682. init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
  1683. //Parse the syncinfo.
  1684. //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
  1685. if (!ac3_parse_sync_info(ctx)) {
  1686. av_log(avctx, AV_LOG_ERROR, "\n");
  1687. *data_size = 0;
  1688. return buf_size;
  1689. }
  1690. //Parse the BSI.
  1691. //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
  1692. ac3_parse_bsi(ctx);
  1693. avctx->sample_rate = ctx->sampling_rate;
  1694. avctx->bit_rate = ctx->bit_rate;
  1695. if (avctx->channels == 0) {
  1696. ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
  1697. if (ctx->lfeon)
  1698. ctx->blkoutput |= AC3_OUTPUT_LFEON;
  1699. avctx->channels = ctx->nfchans + ctx->lfeon;
  1700. }
  1701. else if (avctx->channels == 1)
  1702. ctx->blkoutput |= AC3_OUTPUT_MONO;
  1703. else if (avctx->channels == 2) {
  1704. if (ctx->dsurmod == 0x02)
  1705. ctx->blkoutput |= AC3_OUTPUT_DOLBY;
  1706. else
  1707. ctx->blkoutput |= AC3_OUTPUT_STEREO;
  1708. }
  1709. else {
  1710. if (avctx->channels < (ctx->nfchans + ctx->lfeon))
  1711. av_log(avctx, AV_LOG_INFO, "ac3_decoder: AC3 Source Channels Are Less Then Specified %d: Output to %d Channels\n",avctx->channels, ctx->nfchans + ctx->lfeon);
  1712. ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
  1713. if (ctx->lfeon)
  1714. ctx->blkoutput |= AC3_OUTPUT_LFEON;
  1715. avctx->channels = ctx->nfchans + ctx->lfeon;
  1716. }
  1717. av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->bit_rate * 1000, avctx->sample_rate);
  1718. //Parse the Audio Blocks.
  1719. for (i = 0; i < AUDIO_BLOCKS; i++) {
  1720. if (ac3_parse_audio_block(ctx)) {
  1721. av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
  1722. *data_size = 0;
  1723. return ctx->frame_size;
  1724. }
  1725. for (k = 0; k < BLOCK_SIZE; k++) {
  1726. j = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
  1727. for (; j <= avctx->channels; j++) {
  1728. value = convert(ctx->output[j][k]);
  1729. *(out_samples++) = value;
  1730. }
  1731. }
  1732. }
  1733. *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
  1734. return ctx->frame_size;
  1735. }
  1736. static int ac3_decode_end(AVCodecContext *ctx)
  1737. {
  1738. return 0;
  1739. }
  1740. AVCodec lgpl_ac3_decoder = {
  1741. "ac3",
  1742. CODEC_TYPE_AUDIO,
  1743. CODEC_ID_AC3,
  1744. sizeof (AC3DecodeContext),
  1745. ac3_decode_init,
  1746. NULL,
  1747. ac3_decode_end,
  1748. ac3_decode_frame,
  1749. };