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.

1687 lines
55KB

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