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.

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