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.

1650 lines
54KB

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