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.

2010 lines
64KB

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