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.

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