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.

2001 lines
61KB

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