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.

2014 lines
62KB

  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 chcoeffs[6];
  137. float cplco[5][18];
  138. int ncplbnd;
  139. int ncplsubnd;
  140. int cplstrtmant;
  141. int cplendmant;
  142. int endmant[5];
  143. uint8_t dcplexps[256];
  144. uint8_t dexps[5][256];
  145. uint8_t dlfeexps[256];
  146. uint8_t cplbap[256];
  147. uint8_t bap[5][256];
  148. uint8_t lfebap[256];
  149. int blkoutput;
  150. DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]);
  151. /* For IMDCT. */
  152. MDCTContext imdct_512; //N/8 point IFFT context
  153. MDCTContext imdct_256; //N/4 point IFFT context
  154. DSPContext dsp; //for optimization
  155. DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]);
  156. DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]);
  157. DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);
  158. DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);
  159. /* Miscellaneous. */
  160. GetBitContext gb;
  161. dither_state dith_state;
  162. } AC3DecodeContext;
  163. /* BEGIN Mersenne Twister Code. */
  164. static void dither_seed(dither_state *state, uint32_t seed)
  165. {
  166. if (seed == 0)
  167. seed = 0x1f2e3d4c;
  168. state->mt[0] = seed;
  169. for (state->mti = 1; state->mti < NMT; state->mti++)
  170. state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
  171. }
  172. static uint32_t dither_uint32(dither_state *state)
  173. {
  174. uint32_t y;
  175. static const uint32_t mag01[2] = { 0x00, MATRIX_A };
  176. int kk;
  177. if (state->mti >= NMT) {
  178. for (kk = 0; kk < NMT - MMT; kk++) {
  179. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  180. state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
  181. }
  182. for (;kk < NMT - 1; kk++) {
  183. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  184. state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
  185. }
  186. y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
  187. state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
  188. state->mti = 0;
  189. }
  190. y = state->mt[state->mti++];
  191. y ^= (y >> 11);
  192. y ^= ((y << 7) & 0x9d2c5680);
  193. y ^= ((y << 15) & 0xefc60000);
  194. y ^= (y >> 18);
  195. return y;
  196. }
  197. static inline int16_t dither_int16(dither_state *state)
  198. {
  199. return ((dither_uint32(state) << 16) >> 16);
  200. }
  201. /* END Mersenne Twister */
  202. /**
  203. * Generate a Kaiser Window.
  204. */
  205. static void
  206. k_window_init(int alpha, float *window, int n, int iter)
  207. {
  208. int j, k;
  209. float a, x;
  210. a = alpha * M_PI / n;
  211. a = a*a;
  212. for(k=0; k<n; k++) {
  213. x = k * (n - k) * a;
  214. window[k] = 1.0;
  215. for(j=iter; j>0; j--) {
  216. window[k] = (window[k] * x / (j*j)) + 1.0;
  217. }
  218. }
  219. }
  220. /**
  221. * Generate a Kaiser-Bessel Derived Window.
  222. * @param alpha determines window shape
  223. * @param window array to fill with window values
  224. * @param n length of the window
  225. * @param iter number of iterations to use in BesselI0
  226. */
  227. static void
  228. kbd_window_init(int alpha, float *window, int n, int iter)
  229. {
  230. int k, n2;
  231. float *kwindow;
  232. n2 = n >> 1;
  233. kwindow = &window[n2];
  234. k_window_init(alpha, kwindow, n2, iter);
  235. window[0] = kwindow[0];
  236. for(k=1; k<n2; k++) {
  237. window[k] = window[k-1] + kwindow[k];
  238. }
  239. for(k=0; k<n2; k++) {
  240. window[k] = sqrt(window[k] / (window[n2-1]+1));
  241. window[n-1-k] = window[k];
  242. }
  243. }
  244. static void generate_quantizers_table(int16_t quantizers[], int level, int length)
  245. {
  246. int i;
  247. for (i = 0; i < length; i++)
  248. quantizers[i] = ((2 * i - level + 1) << 15) / level;
  249. }
  250. static void generate_quantizers_table_1(int16_t quantizers[], int level, int length1, int length2, int size)
  251. {
  252. int i, j;
  253. int16_t v;
  254. for (i = 0; i < length1; i++) {
  255. v = ((2 * i - level + 1) << 15) / level;
  256. for (j = 0; j < length2; j++)
  257. quantizers[i * length2 + j] = v;
  258. }
  259. for (i = length1 * length2; i < size; i++)
  260. quantizers[i] = 0;
  261. }
  262. static void generate_quantizers_table_2(int16_t quantizers[], int level, int length1, int length2, int size)
  263. {
  264. int i, j;
  265. int16_t v;
  266. for (i = 0; i < length1; i++) {
  267. v = ((2 * (i % level) - level + 1) << 15) / level;
  268. for (j = 0; j < length2; j++)
  269. quantizers[i * length2 + j] = v;
  270. }
  271. for (i = length1 * length2; i < size; i++)
  272. quantizers[i] = 0;
  273. }
  274. static void generate_quantizers_table_3(int16_t quantizers[], int level, int length1, int length2, int size)
  275. {
  276. int i, j;
  277. for (i = 0; i < length1; i++)
  278. for (j = 0; j < length2; j++)
  279. quantizers[i * length2 + j] = ((2 * (j % level) - level + 1) << 15) / level;
  280. for (i = length1 * length2; i < size; i++)
  281. quantizers[i] = 0;
  282. }
  283. static void ac3_tables_init(void)
  284. {
  285. int i, j, k, l, v;
  286. /* compute bndtab and masktab from bandsz */
  287. k = 0;
  288. l = 0;
  289. for(i=0;i<50;i++) {
  290. bndtab[i] = l;
  291. v = bndsz[i];
  292. for(j=0;j<v;j++) masktab[k++]=i;
  293. l += v;
  294. }
  295. masktab[253] = masktab[254] = masktab[255] = 0;
  296. bndtab[50] = 0;
  297. /* Exponent Decoding Tables */
  298. for (i = 0; i < 5; i++) {
  299. v = i - 2;
  300. for (j = 0; j < 25; j++)
  301. exp_1[i * 25 + j] = v;
  302. }
  303. for (i = 0; i < 25; i++) {
  304. v = (i % 5) - 2;
  305. for (j = 0; j < 5; j++)
  306. exp_2[i * 5 + j] = v;
  307. }
  308. for (i = 0; i < 25; i++) {
  309. v = -2;
  310. for (j = 0; j < 5; j++)
  311. exp_3[i * 5 + j] = v++;
  312. }
  313. for (i = 125; i < 128; i++)
  314. exp_1[i] = exp_2[i] = exp_3[i] = 25;
  315. /* End Exponent Decoding Tables */
  316. /* Quantizer ungrouping tables. */
  317. // for level-3 quantizers
  318. generate_quantizers_table_1(l3_quantizers_1, 3, 3, 9, 32);
  319. generate_quantizers_table_2(l3_quantizers_2, 3, 9, 3, 32);
  320. generate_quantizers_table_3(l3_quantizers_3, 3, 9, 3, 32);
  321. //for level-5 quantizers
  322. generate_quantizers_table_1(l5_quantizers_1, 5, 5, 25, 128);
  323. generate_quantizers_table_2(l5_quantizers_2, 5, 25, 5, 128);
  324. generate_quantizers_table_3(l5_quantizers_3, 5, 25, 5, 128);
  325. //for level-7 quantizers
  326. generate_quantizers_table(l7_quantizers, 7, 7);
  327. //for level-4 quantizers
  328. generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
  329. generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
  330. //for level-15 quantizers
  331. generate_quantizers_table(l15_quantizers, 15, 15);
  332. /* Kaiser-Bessel derived window. */
  333. kbd_window_init(5, window, 256, 100);
  334. }
  335. static int ac3_decode_init(AVCodecContext *avctx)
  336. {
  337. AC3DecodeContext *ctx = avctx->priv_data;
  338. ac3_tables_init();
  339. ff_mdct_init(&ctx->imdct_256, 8, 1);
  340. ff_mdct_init(&ctx->imdct_512, 9, 1);
  341. dsputil_init(&ctx->dsp, avctx);
  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. float *ptr;
  1389. for (k = 0; k < N / 4; k++) {
  1390. x1[k] = ctx->transform_coeffs[chindex][2 * k];
  1391. x2[k] = ctx->transform_coeffs[chindex][2 * k + 1];
  1392. }
  1393. ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
  1394. ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
  1395. ptr = ctx->output[chindex];
  1396. ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
  1397. ptr = ctx->delay[chindex];
  1398. ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, window, BLOCK_SIZE);
  1399. }
  1400. static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
  1401. {
  1402. float *ptr;
  1403. ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output,
  1404. ctx->transform_coeffs[chindex], ctx->tmp_imdct);
  1405. ptr = ctx->output[chindex];
  1406. ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
  1407. ptr = ctx->delay[chindex];
  1408. ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, window, BLOCK_SIZE);
  1409. }
  1410. static inline void do_imdct(AC3DecodeContext *ctx)
  1411. {
  1412. int i;
  1413. if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
  1414. do_imdct_512(ctx, 0);
  1415. }
  1416. for (i = 0; i < ctx->nfchans; i++) {
  1417. if ((ctx->blksw >> i) & 1)
  1418. do_imdct_256(ctx, i + 1);
  1419. else
  1420. do_imdct_512(ctx, i + 1);
  1421. }
  1422. }
  1423. static int ac3_parse_audio_block(AC3DecodeContext * ctx)
  1424. {
  1425. int nfchans = ctx->nfchans;
  1426. int acmod = ctx->acmod;
  1427. int i, bnd, rbnd, seg, grpsize;
  1428. GetBitContext *gb = &ctx->gb;
  1429. int bit_alloc_flags = 0;
  1430. float drange;
  1431. uint8_t *dexps;
  1432. int mstrcplco, cplcoexp, cplcomant;
  1433. int dynrng, chbwcod, ngrps, cplabsexp, skipl;
  1434. for (i = 0; i < 5; i++)
  1435. ctx->chcoeffs[i] = 2.0;
  1436. ctx->blksw = 0;
  1437. for (i = 0; i < nfchans; i++) /*block switch flag */
  1438. ctx->blksw |= get_bits1(gb) << i;
  1439. ctx->dithflag = 0;
  1440. for (i = 0; i < nfchans; i++) /* dithering flag */
  1441. ctx->dithflag |= get_bits1(gb) << i;
  1442. if (get_bits1(gb)) { /* dynamic range */
  1443. dynrng = get_sbits(gb, 8);
  1444. drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
  1445. for (i = 0; i < nfchans; i++)
  1446. ctx->chcoeffs[i] *= drange;
  1447. }
  1448. if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
  1449. dynrng = get_sbits(gb, 8);
  1450. drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
  1451. ctx->chcoeffs[1] *= drange;
  1452. }
  1453. get_downmix_coeffs(ctx);
  1454. if (get_bits1(gb)) { /* coupling strategy */
  1455. ctx->cplinu = get_bits1(gb);
  1456. ctx->cplbndstrc = 0;
  1457. ctx->chincpl = 0;
  1458. if (ctx->cplinu) { /* coupling in use */
  1459. for (i = 0; i < nfchans; i++)
  1460. ctx->chincpl |= get_bits1(gb) << i;
  1461. if (acmod == 0x02)
  1462. ctx->phsflginu = get_bits1(gb); //phase flag in use
  1463. ctx->cplbegf = get_bits(gb, 4);
  1464. ctx->cplendf = get_bits(gb, 4);
  1465. if (3 + ctx->cplendf - ctx->cplbegf < 0) {
  1466. av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", ctx->cplendf, ctx->cplbegf);
  1467. return -1;
  1468. }
  1469. ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
  1470. ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
  1471. ctx->cplendmant = ctx->cplendf * 12 + 73;
  1472. for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
  1473. if (get_bits1(gb)) {
  1474. ctx->cplbndstrc |= 1 << i;
  1475. ctx->ncplbnd--;
  1476. }
  1477. }
  1478. }
  1479. if (ctx->cplinu) {
  1480. ctx->cplcoe = 0;
  1481. for (i = 0; i < nfchans; i++)
  1482. if ((ctx->chincpl) >> i & 1)
  1483. if (get_bits1(gb)) { /* coupling co-ordinates */
  1484. ctx->cplcoe |= 1 << i;
  1485. mstrcplco = 3 * get_bits(gb, 2);
  1486. for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
  1487. cplcoexp = get_bits(gb, 4);
  1488. cplcomant = get_bits(gb, 4);
  1489. if (cplcoexp == 15)
  1490. cplcomant <<= 14;
  1491. else
  1492. cplcomant = (cplcomant | 0x10) << 13;
  1493. ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
  1494. }
  1495. }
  1496. if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
  1497. for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
  1498. if (get_bits1(gb))
  1499. ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
  1500. }
  1501. if (acmod == 0x02) {/* rematrixing */
  1502. ctx->rematstr = get_bits1(gb);
  1503. if (ctx->rematstr) {
  1504. ctx->rematflg = 0;
  1505. if (!(ctx->cplinu) || ctx->cplbegf > 2)
  1506. for (rbnd = 0; rbnd < 4; rbnd++)
  1507. ctx->rematflg |= get_bits1(gb) << rbnd;
  1508. if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && ctx->cplinu)
  1509. for (rbnd = 0; rbnd < 3; rbnd++)
  1510. ctx->rematflg |= get_bits1(gb) << rbnd;
  1511. if (ctx->cplbegf == 0 && ctx->cplinu)
  1512. for (rbnd = 0; rbnd < 2; rbnd++)
  1513. ctx->rematflg |= get_bits1(gb) << rbnd;
  1514. }
  1515. }
  1516. ctx->cplexpstr = AC3_EXPSTR_REUSE;
  1517. ctx->lfeexpstr = AC3_EXPSTR_REUSE;
  1518. if (ctx->cplinu) /* coupling exponent strategy */
  1519. ctx->cplexpstr = get_bits(gb, 2);
  1520. for (i = 0; i < nfchans; i++) /* channel exponent strategy */
  1521. ctx->chexpstr[i] = get_bits(gb, 2);
  1522. if (ctx->lfeon) /* lfe exponent strategy */
  1523. ctx->lfeexpstr = get_bits1(gb);
  1524. for (i = 0; i < nfchans; i++) /* channel bandwidth code */
  1525. if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
  1526. if ((ctx->chincpl >> i) & 1)
  1527. ctx->endmant[i] = ctx->cplstrtmant;
  1528. else {
  1529. chbwcod = get_bits(gb, 6);
  1530. if (chbwcod > 60) {
  1531. av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
  1532. return -1;
  1533. }
  1534. ctx->endmant[i] = chbwcod * 3 + 73;
  1535. }
  1536. }
  1537. if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
  1538. bit_alloc_flags = 64;
  1539. cplabsexp = get_bits(gb, 4) << 1;
  1540. ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
  1541. if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
  1542. av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
  1543. return -1;
  1544. }
  1545. }
  1546. for (i = 0; i < nfchans; i++) /* fbw channel exponents */
  1547. if (ctx->chexpstr[i] != AC3_EXPSTR_REUSE) {
  1548. bit_alloc_flags |= 1 << i;
  1549. grpsize = 3 << (ctx->chexpstr[i] - 1);
  1550. ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
  1551. dexps = ctx->dexps[i];
  1552. dexps[0] = get_bits(gb, 4);
  1553. if (decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1)) {
  1554. av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
  1555. return -1;
  1556. }
  1557. skip_bits(gb, 2); /* skip gainrng */
  1558. }
  1559. if (ctx->lfeexpstr != AC3_EXPSTR_REUSE) { /* lfe exponents */
  1560. bit_alloc_flags |= 32;
  1561. ctx->dlfeexps[0] = get_bits(gb, 4);
  1562. if (decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1)) {
  1563. av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
  1564. return -1;
  1565. }
  1566. }
  1567. if (get_bits1(gb)) { /* bit allocation information */
  1568. bit_alloc_flags = 127;
  1569. ctx->sdcycod = get_bits(gb, 2);
  1570. ctx->fdcycod = get_bits(gb, 2);
  1571. ctx->sgaincod = get_bits(gb, 2);
  1572. ctx->dbpbcod = get_bits(gb, 2);
  1573. ctx->floorcod = get_bits(gb, 3);
  1574. }
  1575. if (get_bits1(gb)) { /* snroffset */
  1576. bit_alloc_flags = 127;
  1577. ctx->csnroffst = get_bits(gb, 6);
  1578. if (ctx->cplinu) { /* couling fine snr offset and fast gain code */
  1579. ctx->cplfsnroffst = get_bits(gb, 4);
  1580. ctx->cplfgaincod = get_bits(gb, 3);
  1581. }
  1582. for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
  1583. ctx->fsnroffst[i] = get_bits(gb, 4);
  1584. ctx->fgaincod[i] = get_bits(gb, 3);
  1585. }
  1586. if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
  1587. ctx->lfefsnroffst = get_bits(gb, 4);
  1588. ctx->lfefgaincod = get_bits(gb, 3);
  1589. }
  1590. }
  1591. if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
  1592. bit_alloc_flags |= 64;
  1593. ctx->cplfleak = get_bits(gb, 3);
  1594. ctx->cplsleak = get_bits(gb, 3);
  1595. }
  1596. if (get_bits1(gb)) { /* delta bit allocation information */
  1597. bit_alloc_flags = 127;
  1598. if (ctx->cplinu) {
  1599. ctx->cpldeltbae = get_bits(gb, 2);
  1600. if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) {
  1601. av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
  1602. return -1;
  1603. }
  1604. }
  1605. for (i = 0; i < nfchans; i++) {
  1606. ctx->deltbae[i] = get_bits(gb, 2);
  1607. if (ctx->deltbae[i] == AC3_DBASTR_RESERVED) {
  1608. av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
  1609. return -1;
  1610. }
  1611. }
  1612. if (ctx->cplinu)
  1613. if (ctx->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
  1614. ctx->cpldeltnseg = get_bits(gb, 3);
  1615. for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
  1616. ctx->cpldeltoffst[seg] = get_bits(gb, 5);
  1617. ctx->cpldeltlen[seg] = get_bits(gb, 4);
  1618. ctx->cpldeltba[seg] = get_bits(gb, 3);
  1619. }
  1620. }
  1621. for (i = 0; i < nfchans; i++)
  1622. if (ctx->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
  1623. ctx->deltnseg[i] = get_bits(gb, 3);
  1624. for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
  1625. ctx->deltoffst[i][seg] = get_bits(gb, 5);
  1626. ctx->deltlen[i][seg] = get_bits(gb, 4);
  1627. ctx->deltba[i][seg] = get_bits(gb, 3);
  1628. }
  1629. }
  1630. }
  1631. if (bit_alloc_flags) {
  1632. if (is_snr_offsets_zero(ctx)) {
  1633. memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
  1634. memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
  1635. for (i = 0; i < nfchans; i++)
  1636. memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
  1637. } else {
  1638. if (ctx->chincpl && (bit_alloc_flags & 64))
  1639. do_bit_allocation(ctx, 5);
  1640. for (i = 0; i < nfchans; i++)
  1641. if ((bit_alloc_flags >> i) & 1)
  1642. do_bit_allocation(ctx, i);
  1643. if (ctx->lfeon && (bit_alloc_flags & 32))
  1644. do_bit_allocation(ctx, 6);
  1645. }
  1646. }
  1647. if (get_bits1(gb)) { /* unused dummy data */
  1648. skipl = get_bits(gb, 9);
  1649. while(skipl--)
  1650. skip_bits(gb, 8);
  1651. }
  1652. /* unpack the transform coefficients
  1653. * * this also uncouples channels if coupling is in use.
  1654. */
  1655. if (get_transform_coeffs(ctx)) {
  1656. av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
  1657. return -1;
  1658. }
  1659. /*for (i = 0; i < nfchans; i++)
  1660. dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
  1661. /* recover coefficients if rematrixing is in use */
  1662. if (ctx->rematflg)
  1663. do_rematrixing(ctx);
  1664. do_downmix(ctx);
  1665. do_imdct(ctx);
  1666. /*for(i = 0; i < nfchans; i++)
  1667. dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
  1668. return 0;
  1669. }
  1670. /*static inline int16_t convert(float f)
  1671. {
  1672. if (f >= 1.0)
  1673. return 32767;
  1674. else if (f <= -1.0)
  1675. return -32768;
  1676. else
  1677. return (lrintf(f * 32767.0));
  1678. }*/
  1679. static inline int16_t convert(int32_t i)
  1680. {
  1681. if (i > 0x43c07fff)
  1682. return 32767;
  1683. else if (i <= 0x43bf8000)
  1684. return -32768;
  1685. else
  1686. return (i - 0x43c00000);
  1687. }
  1688. static int frame_count = 0;
  1689. static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  1690. {
  1691. AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
  1692. int frame_start;
  1693. int16_t *out_samples = (int16_t *)data;
  1694. int i, j, k, start;
  1695. int32_t *int_ptr[6];
  1696. for (i = 0; i < 6; i++)
  1697. int_ptr[i] = (int32_t *)(&ctx->output[i]);
  1698. //av_log(NULL, AV_LOG_INFO, "decoding frame %d buf_size = %d\n", frame_count++, buf_size);
  1699. //Synchronize the frame.
  1700. frame_start = ac3_synchronize(buf, buf_size);
  1701. if (frame_start == -1) {
  1702. av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
  1703. *data_size = 0;
  1704. return buf_size;
  1705. }
  1706. //Initialize the GetBitContext with the start of valid AC3 Frame.
  1707. init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
  1708. //Parse the syncinfo.
  1709. //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
  1710. if (!ac3_parse_sync_info(ctx)) {
  1711. av_log(avctx, AV_LOG_ERROR, "\n");
  1712. *data_size = 0;
  1713. return buf_size;
  1714. }
  1715. //Parse the BSI.
  1716. //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
  1717. ac3_parse_bsi(ctx);
  1718. avctx->sample_rate = ctx->sampling_rate;
  1719. avctx->bit_rate = ctx->bit_rate;
  1720. if (avctx->channels == 0) {
  1721. ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
  1722. if (ctx->lfeon)
  1723. ctx->blkoutput |= AC3_OUTPUT_LFEON;
  1724. avctx->channels = ctx->nfchans + ctx->lfeon;
  1725. }
  1726. else if (avctx->channels == 1)
  1727. ctx->blkoutput |= AC3_OUTPUT_MONO;
  1728. else if (avctx->channels == 2) {
  1729. if (ctx->dsurmod == 0x02)
  1730. ctx->blkoutput |= AC3_OUTPUT_DOLBY;
  1731. else
  1732. ctx->blkoutput |= AC3_OUTPUT_STEREO;
  1733. }
  1734. else {
  1735. if (avctx->channels < (ctx->nfchans + ctx->lfeon))
  1736. 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);
  1737. ctx->blkoutput |= AC3_OUTPUT_UNMODIFIED;
  1738. if (ctx->lfeon)
  1739. ctx->blkoutput |= AC3_OUTPUT_LFEON;
  1740. avctx->channels = ctx->nfchans + ctx->lfeon;
  1741. }
  1742. //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);
  1743. //Parse the Audio Blocks.
  1744. for (i = 0; i < AUDIO_BLOCKS; i++) {
  1745. if (ac3_parse_audio_block(ctx)) {
  1746. av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
  1747. *data_size = 0;
  1748. return ctx->frame_size;
  1749. }
  1750. start = (ctx->blkoutput & AC3_OUTPUT_LFEON) ? 0 : 1;
  1751. for (k = 0; k < BLOCK_SIZE; k++)
  1752. for (j = start; j <= avctx->channels; j++)
  1753. *(out_samples++) = convert(int_ptr[j][k]);
  1754. }
  1755. *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
  1756. return ctx->frame_size;
  1757. }
  1758. static int ac3_decode_end(AVCodecContext *ctx)
  1759. {
  1760. return 0;
  1761. }
  1762. AVCodec lgpl_ac3_decoder = {
  1763. .name = "ac3",
  1764. .type = CODEC_TYPE_AUDIO,
  1765. .id = CODEC_ID_AC3,
  1766. .priv_data_size = sizeof (AC3DecodeContext),
  1767. .init = ac3_decode_init,
  1768. .close = ac3_decode_end,
  1769. .decode = ac3_decode_frame,
  1770. };