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.

2061 lines
65KB

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