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.

1073 lines
35KB

  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. /**
  39. * Table of bin locations for rematrixing bands
  40. * reference: Section 7.5.2 Rematrixing : Frequency Band Definitions
  41. */
  42. static const uint8_t rematrix_band_tbl[5] = { 13, 25, 37, 61, 253 };
  43. /* table for exponent to scale_factor mapping
  44. * scale_factor[i] = 2 ^ -(i + 15)
  45. */
  46. static float scale_factors[25];
  47. /** table for grouping exponents */
  48. static uint8_t exp_ungroup_tbl[128][3];
  49. /** tables for ungrouping mantissas */
  50. static float b1_mantissas[32][3];
  51. static float b2_mantissas[128][3];
  52. static float b3_mantissas[8];
  53. static float b4_mantissas[128][2];
  54. static float b5_mantissas[16];
  55. /**
  56. * Quantization table: levels for symmetric. bits for asymmetric.
  57. * reference: Table 7.18 Mapping of bap to Quantizer
  58. */
  59. static const uint8_t qntztab[16] = {
  60. 0, 3, 5, 7, 11, 15,
  61. 5, 6, 7, 8, 9, 10, 11, 12, 14, 16
  62. };
  63. /* Adjustmens in dB gain */
  64. #define LEVEL_MINUS_3DB 0.7071067811865476
  65. #define LEVEL_MINUS_4POINT5DB 0.5946035575013605
  66. #define LEVEL_MINUS_6DB 0.5000000000000000
  67. #define LEVEL_PLUS_3DB 1.4142135623730951
  68. #define LEVEL_PLUS_6DB 2.0000000000000000
  69. #define LEVEL_ZERO 0.0000000000000000
  70. static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
  71. LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
  72. static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
  73. #define AC3_OUTPUT_LFEON 8
  74. typedef struct {
  75. int acmod;
  76. int cmixlev;
  77. int surmixlev;
  78. int dsurmod;
  79. int blksw[AC3_MAX_CHANNELS];
  80. int dithflag[AC3_MAX_CHANNELS];
  81. int dither_all;
  82. int cplinu;
  83. int chincpl[AC3_MAX_CHANNELS];
  84. int phsflginu;
  85. int cplcoe;
  86. uint32_t cplbndstrc;
  87. int rematstr;
  88. int nrematbnd;
  89. int rematflg[AC3_MAX_CHANNELS];
  90. int cplexpstr;
  91. int lfeexpstr;
  92. int chexpstr[5];
  93. int cplsnroffst;
  94. int cplfgain;
  95. int snroffst[5];
  96. int fgain[5];
  97. int lfesnroffst;
  98. int lfefgain;
  99. int cpldeltbae;
  100. int deltbae[5];
  101. int cpldeltnseg;
  102. uint8_t cpldeltoffst[8];
  103. uint8_t cpldeltlen[8];
  104. uint8_t cpldeltba[8];
  105. int deltnseg[5];
  106. uint8_t deltoffst[5][8];
  107. uint8_t deltlen[5][8];
  108. uint8_t deltba[5][8];
  109. /* Derived Attributes. */
  110. int sampling_rate;
  111. int bit_rate;
  112. int frame_size;
  113. int nchans; //number of total channels
  114. int nfchans; //number of full-bandwidth channels
  115. int lfeon; //lfe channel in use
  116. int output_mode; ///< output channel configuration
  117. int out_channels; ///< number of output channels
  118. float dynrng; //dynamic range gain
  119. float dynrng2; //dynamic range gain for 1+1 mode
  120. float cplco[5][18]; //coupling coordinates
  121. int ncplbnd; //number of coupling bands
  122. int ncplsubnd; //number of coupling sub bands
  123. int cplstrtmant; //coupling start mantissa
  124. int cplendmant; //coupling end mantissa
  125. int endmant[5]; //channel end mantissas
  126. AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
  127. int8_t dcplexps[256]; //decoded coupling exponents
  128. int8_t dexps[5][256]; //decoded fbw channel exponents
  129. int8_t dlfeexps[256]; //decoded lfe channel exponents
  130. uint8_t cplbap[256]; //coupling bit allocation pointers
  131. uint8_t bap[5][256]; //fbw channel bit allocation pointers
  132. uint8_t lfebap[256]; //lfe channel bit allocation pointers
  133. float transform_coeffs_cpl[256];
  134. DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); //transform coefficients
  135. /* For IMDCT. */
  136. MDCTContext imdct_512; //for 512 sample imdct transform
  137. MDCTContext imdct_256; //for 256 sample imdct transform
  138. DSPContext dsp; //for optimization
  139. DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][256]); //output after imdct transform and windowing
  140. DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][256]); //delay - added to the next block
  141. DECLARE_ALIGNED_16(float, tmp_imdct[256]); //temporary storage for imdct transform
  142. DECLARE_ALIGNED_16(float, tmp_output[512]); //temporary storage for output before windowing
  143. DECLARE_ALIGNED_16(float, window[256]); //window coefficients
  144. /* Miscellaneous. */
  145. GetBitContext gb;
  146. AVRandomState dith_state; //for dither generation
  147. } AC3DecodeContext;
  148. /*********** BEGIN INIT HELPER FUNCTIONS ***********/
  149. /**
  150. * Generate a Kaiser-Bessel Derived Window.
  151. */
  152. static void ac3_window_init(float *window)
  153. {
  154. int i, j;
  155. double sum = 0.0, bessel, tmp;
  156. double local_window[256];
  157. double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
  158. for (i = 0; i < 256; i++) {
  159. tmp = i * (256 - i) * alpha2;
  160. bessel = 1.0;
  161. for (j = 100; j > 0; j--) /* defaul to 100 iterations */
  162. bessel = bessel * tmp / (j * j) + 1;
  163. sum += bessel;
  164. local_window[i] = sum;
  165. }
  166. sum++;
  167. for (i = 0; i < 256; i++)
  168. window[i] = sqrt(local_window[i] / sum);
  169. }
  170. static inline float
  171. symmetric_dequant(int code, int levels)
  172. {
  173. return (code - (levels >> 1)) * (2.0f / levels);
  174. }
  175. /*
  176. * Initialize tables at runtime.
  177. */
  178. static void ac3_tables_init(void)
  179. {
  180. int i;
  181. /* generate grouped mantissa tables
  182. reference: Section 7.3.5 Ungrouping of Mantissas */
  183. for(i=0; i<32; i++) {
  184. /* bap=1 mantissas */
  185. b1_mantissas[i][0] = symmetric_dequant( i / 9 , 3);
  186. b1_mantissas[i][1] = symmetric_dequant((i % 9) / 3, 3);
  187. b1_mantissas[i][2] = symmetric_dequant((i % 9) % 3, 3);
  188. }
  189. for(i=0; i<128; i++) {
  190. /* bap=2 mantissas */
  191. b2_mantissas[i][0] = symmetric_dequant( i / 25 , 5);
  192. b2_mantissas[i][1] = symmetric_dequant((i % 25) / 5, 5);
  193. b2_mantissas[i][2] = symmetric_dequant((i % 25) % 5, 5);
  194. /* bap=4 mantissas */
  195. b4_mantissas[i][0] = symmetric_dequant(i / 11, 11);
  196. b4_mantissas[i][1] = symmetric_dequant(i % 11, 11);
  197. }
  198. /* generate ungrouped mantissa tables
  199. reference: Tables 7.21 and 7.23 */
  200. for(i=0; i<7; i++) {
  201. /* bap=3 mantissas */
  202. b3_mantissas[i] = symmetric_dequant(i, 7);
  203. }
  204. for(i=0; i<15; i++) {
  205. /* bap=5 mantissas */
  206. b5_mantissas[i] = symmetric_dequant(i, 15);
  207. }
  208. //generate scale factors
  209. for (i = 0; i < 25; i++)
  210. scale_factors[i] = pow(2.0, -i);
  211. /* generate exponent tables
  212. reference: Section 7.1.3 Exponent Decoding */
  213. for(i=0; i<128; i++) {
  214. exp_ungroup_tbl[i][0] = i / 25;
  215. exp_ungroup_tbl[i][1] = (i % 25) / 5;
  216. exp_ungroup_tbl[i][2] = (i % 25) % 5;
  217. }
  218. }
  219. static int ac3_decode_init(AVCodecContext *avctx)
  220. {
  221. AC3DecodeContext *ctx = avctx->priv_data;
  222. ac3_common_init();
  223. ac3_tables_init();
  224. ff_mdct_init(&ctx->imdct_256, 8, 1);
  225. ff_mdct_init(&ctx->imdct_512, 9, 1);
  226. ac3_window_init(ctx->window);
  227. dsputil_init(&ctx->dsp, avctx);
  228. av_init_random(0, &ctx->dith_state);
  229. return 0;
  230. }
  231. /*********** END INIT FUNCTIONS ***********/
  232. /**
  233. * Parses the 'sync info' and 'bit stream info' from the AC-3 bitstream.
  234. * GetBitContext within AC3DecodeContext must point to
  235. * start of the synchronized ac3 bitstream.
  236. */
  237. static int ac3_parse_header(AC3DecodeContext *ctx)
  238. {
  239. AC3HeaderInfo hdr;
  240. GetBitContext *gb = &ctx->gb;
  241. int err, i;
  242. err = ff_ac3_parse_header(gb->buffer, &hdr);
  243. if(err)
  244. return err;
  245. /* get decoding parameters from header info */
  246. ctx->bit_alloc_params.fscod = hdr.fscod;
  247. ctx->acmod = hdr.acmod;
  248. ctx->cmixlev = hdr.cmixlev;
  249. ctx->surmixlev = hdr.surmixlev;
  250. ctx->dsurmod = hdr.dsurmod;
  251. ctx->lfeon = hdr.lfeon;
  252. ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
  253. ctx->sampling_rate = hdr.sample_rate;
  254. ctx->bit_rate = hdr.bit_rate;
  255. ctx->nchans = hdr.channels;
  256. ctx->nfchans = ctx->nchans - ctx->lfeon;
  257. ctx->frame_size = hdr.frame_size;
  258. /* set default output to all source channels */
  259. ctx->out_channels = ctx->nchans;
  260. ctx->output_mode = ctx->acmod;
  261. if(ctx->lfeon)
  262. ctx->output_mode |= AC3_OUTPUT_LFEON;
  263. /* skip over portion of header which has already been read */
  264. skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
  265. skip_bits(gb, 16); // skip crc1
  266. skip_bits(gb, 8); // skip fscod and frmsizecod
  267. skip_bits(gb, 11); // skip bsid, bsmod, and acmod
  268. if(ctx->acmod == AC3_ACMOD_STEREO) {
  269. skip_bits(gb, 2); // skip dsurmod
  270. } else {
  271. if((ctx->acmod & 1) && ctx->acmod != AC3_ACMOD_MONO)
  272. skip_bits(gb, 2); // skip cmixlev
  273. if(ctx->acmod & 4)
  274. skip_bits(gb, 2); // skip surmixlev
  275. }
  276. skip_bits1(gb); // skip lfeon
  277. /* read the rest of the bsi. read twice for dual mono mode. */
  278. i = !(ctx->acmod);
  279. do {
  280. skip_bits(gb, 5); //skip dialog normalization
  281. if (get_bits1(gb))
  282. skip_bits(gb, 8); //skip compression
  283. if (get_bits1(gb))
  284. skip_bits(gb, 8); //skip language code
  285. if (get_bits1(gb))
  286. skip_bits(gb, 7); //skip audio production information
  287. } while (i--);
  288. skip_bits(gb, 2); //skip copyright bit and original bitstream bit
  289. /* FIXME: read & use the xbsi1 downmix levels */
  290. if (get_bits1(gb))
  291. skip_bits(gb, 14); //skip timecode1
  292. if (get_bits1(gb))
  293. skip_bits(gb, 14); //skip timecode2
  294. if (get_bits1(gb)) {
  295. i = get_bits(gb, 6); //additional bsi length
  296. do {
  297. skip_bits(gb, 8);
  298. } while(i--);
  299. }
  300. return 0;
  301. }
  302. /**
  303. * Decodes the grouped exponents.
  304. * This function decodes the coded exponents according to exponent strategy
  305. * and stores them in the decoded exponents buffer.
  306. *
  307. * @param[in] gb GetBitContext which points to start of coded exponents
  308. * @param[in] expstr Exponent coding strategy
  309. * @param[in] ngrps Number of grouped exponents
  310. * @param[in] absexp Absolute exponent or DC exponent
  311. * @param[out] dexps Decoded exponents are stored in dexps
  312. */
  313. static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,
  314. uint8_t absexp, int8_t *dexps)
  315. {
  316. int i, j, grp, grpsize;
  317. int dexp[256];
  318. int expacc, prevexp;
  319. /* unpack groups */
  320. grpsize = expstr + (expstr == EXP_D45);
  321. for(grp=0,i=0; grp<ngrps; grp++) {
  322. expacc = get_bits(gb, 7);
  323. dexp[i++] = exp_ungroup_tbl[expacc][0];
  324. dexp[i++] = exp_ungroup_tbl[expacc][1];
  325. dexp[i++] = exp_ungroup_tbl[expacc][2];
  326. }
  327. /* convert to absolute exps and expand groups */
  328. prevexp = absexp;
  329. for(i=0; i<ngrps*3; i++) {
  330. prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);
  331. for(j=0; j<grpsize; j++) {
  332. dexps[(i*grpsize)+j] = prevexp;
  333. }
  334. }
  335. }
  336. /**
  337. * Generates transform coefficients for each coupled channel in the coupling
  338. * range using the coupling coefficients and coupling coordinates.
  339. * reference: Section 7.4.3 Coupling Coordinate Format
  340. */
  341. static void uncouple_channels(AC3DecodeContext *ctx)
  342. {
  343. int i, j, ch, bnd, subbnd;
  344. subbnd = -1;
  345. i = ctx->cplstrtmant;
  346. for(bnd=0; bnd<ctx->ncplbnd; bnd++) {
  347. do {
  348. subbnd++;
  349. for(j=0; j<12; j++) {
  350. for(ch=1; ch<=ctx->nfchans; ch++) {
  351. if(ctx->chincpl[ch-1])
  352. ctx->transform_coeffs[ch][i] = ctx->transform_coeffs_cpl[i] * ctx->cplco[ch-1][bnd] * 8.0f;
  353. }
  354. i++;
  355. }
  356. } while((ctx->cplbndstrc >> subbnd) & 1);
  357. }
  358. }
  359. typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
  360. float b1_mant[3];
  361. float b2_mant[3];
  362. float b4_mant[2];
  363. int b1ptr;
  364. int b2ptr;
  365. int b4ptr;
  366. } mant_groups;
  367. /* Get the transform coefficients for particular channel */
  368. static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
  369. {
  370. GetBitContext *gb = &ctx->gb;
  371. int i, gcode, tbap, start, end;
  372. uint8_t *exps;
  373. uint8_t *bap;
  374. float *coeffs;
  375. if (ch_index >= 0) { /* fbw channels */
  376. exps = ctx->dexps[ch_index];
  377. bap = ctx->bap[ch_index];
  378. coeffs = ctx->transform_coeffs[ch_index + 1];
  379. start = 0;
  380. end = ctx->endmant[ch_index];
  381. } else if (ch_index == -1) {
  382. exps = ctx->dlfeexps;
  383. bap = ctx->lfebap;
  384. coeffs = ctx->transform_coeffs[0];
  385. start = 0;
  386. end = 7;
  387. } else {
  388. exps = ctx->dcplexps;
  389. bap = ctx->cplbap;
  390. coeffs = ctx->transform_coeffs_cpl;
  391. start = ctx->cplstrtmant;
  392. end = ctx->cplendmant;
  393. }
  394. for (i = start; i < end; i++) {
  395. tbap = bap[i];
  396. switch (tbap) {
  397. case 0:
  398. coeffs[i] = ((av_random(&ctx->dith_state) & 0xFFFF) * LEVEL_MINUS_3DB) / 32768.0f;
  399. break;
  400. case 1:
  401. if(m->b1ptr > 2) {
  402. gcode = get_bits(gb, 5);
  403. m->b1_mant[0] = b1_mantissas[gcode][0];
  404. m->b1_mant[1] = b1_mantissas[gcode][1];
  405. m->b1_mant[2] = b1_mantissas[gcode][2];
  406. m->b1ptr = 0;
  407. }
  408. coeffs[i] = m->b1_mant[m->b1ptr++];
  409. break;
  410. case 2:
  411. if(m->b2ptr > 2) {
  412. gcode = get_bits(gb, 7);
  413. m->b2_mant[0] = b2_mantissas[gcode][0];
  414. m->b2_mant[1] = b2_mantissas[gcode][1];
  415. m->b2_mant[2] = b2_mantissas[gcode][2];
  416. m->b2ptr = 0;
  417. }
  418. coeffs[i] = m->b2_mant[m->b2ptr++];
  419. break;
  420. case 3:
  421. coeffs[i] = b3_mantissas[get_bits(gb, 3)];
  422. break;
  423. case 4:
  424. if(m->b4ptr > 1) {
  425. gcode = get_bits(gb, 7);
  426. m->b4_mant[0] = b4_mantissas[gcode][0];
  427. m->b4_mant[1] = b4_mantissas[gcode][1];
  428. m->b4ptr = 0;
  429. }
  430. coeffs[i] = m->b4_mant[m->b4ptr++];
  431. break;
  432. case 5:
  433. coeffs[i] = b5_mantissas[get_bits(gb, 4)];
  434. break;
  435. default:
  436. coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1];
  437. break;
  438. }
  439. coeffs[i] *= scale_factors[exps[i]];
  440. }
  441. return 0;
  442. }
  443. /**
  444. * Removes random dithering from coefficients with zero-bit mantissas
  445. * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
  446. */
  447. static void remove_dithering(AC3DecodeContext *ctx) {
  448. int ch, i;
  449. int end=0;
  450. float *coeffs;
  451. uint8_t *bap;
  452. for(ch=1; ch<=ctx->nfchans; ch++) {
  453. if(!ctx->dithflag[ch-1]) {
  454. coeffs = ctx->transform_coeffs[ch];
  455. bap = ctx->bap[ch-1];
  456. if(ctx->chincpl[ch-1])
  457. end = ctx->cplstrtmant;
  458. else
  459. end = ctx->endmant[ch-1];
  460. for(i=0; i<end; i++) {
  461. if(bap[i] == 0)
  462. coeffs[i] = 0.0f;
  463. }
  464. if(ctx->chincpl[ch-1]) {
  465. bap = ctx->cplbap;
  466. for(; i<ctx->cplendmant; i++) {
  467. if(bap[i] == 0)
  468. coeffs[i] = 0.0f;
  469. }
  470. }
  471. }
  472. }
  473. }
  474. /* Get the transform coefficients.
  475. * This function extracts the tranform coefficients form the ac3 bitstream.
  476. * This function is called after bit allocation is performed.
  477. */
  478. static int get_transform_coeffs(AC3DecodeContext * ctx)
  479. {
  480. int i, end;
  481. int got_cplchan = 0;
  482. mant_groups m;
  483. m.b1ptr = m.b2ptr = m.b4ptr = 3;
  484. for (i = 0; i < ctx->nfchans; i++) {
  485. /* transform coefficients for individual channel */
  486. if (get_transform_coeffs_ch(ctx, i, &m))
  487. return -1;
  488. /* tranform coefficients for coupling channels */
  489. if (ctx->chincpl[i]) {
  490. if (!got_cplchan) {
  491. if (get_transform_coeffs_ch(ctx, -2, &m)) {
  492. av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
  493. return -1;
  494. }
  495. uncouple_channels(ctx);
  496. got_cplchan = 1;
  497. }
  498. end = ctx->cplendmant;
  499. } else
  500. end = ctx->endmant[i];
  501. do
  502. ctx->transform_coeffs[i + 1][end] = 0;
  503. while(++end < 256);
  504. }
  505. if (ctx->lfeon) {
  506. if (get_transform_coeffs_ch(ctx, -1, &m))
  507. return -1;
  508. for (i = 7; i < 256; i++) {
  509. ctx->transform_coeffs[0][i] = 0;
  510. }
  511. }
  512. /* if any channel doesn't use dithering, zero appropriate coefficients */
  513. if(!ctx->dither_all)
  514. remove_dithering(ctx);
  515. return 0;
  516. }
  517. /**
  518. * Performs stereo rematrixing.
  519. * reference: Section 7.5.4 Rematrixing : Decoding Technique
  520. */
  521. static void do_rematrixing(AC3DecodeContext *ctx)
  522. {
  523. int bnd, i;
  524. int end, bndend;
  525. float tmp0, tmp1;
  526. end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
  527. for(bnd=0; bnd<ctx->nrematbnd; bnd++) {
  528. if(ctx->rematflg[bnd]) {
  529. bndend = FFMIN(end, rematrix_band_tbl[bnd+1]);
  530. for(i=rematrix_band_tbl[bnd]; i<bndend; i++) {
  531. tmp0 = ctx->transform_coeffs[1][i];
  532. tmp1 = ctx->transform_coeffs[2][i];
  533. ctx->transform_coeffs[1][i] = tmp0 + tmp1;
  534. ctx->transform_coeffs[2][i] = tmp0 - tmp1;
  535. }
  536. }
  537. }
  538. }
  539. /* This function performs the imdct on 256 sample transform
  540. * coefficients.
  541. */
  542. static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
  543. {
  544. int i, k;
  545. DECLARE_ALIGNED_16(float, x[128]);
  546. FFTComplex z[2][64];
  547. float *o_ptr = ctx->tmp_output;
  548. for(i=0; i<2; i++) {
  549. /* de-interleave coefficients */
  550. for(k=0; k<128; k++) {
  551. x[k] = ctx->transform_coeffs[chindex][2*k+i];
  552. }
  553. /* run standard IMDCT */
  554. ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);
  555. /* reverse the post-rotation & reordering from standard IMDCT */
  556. for(k=0; k<32; k++) {
  557. z[i][32+k].re = -o_ptr[128+2*k];
  558. z[i][32+k].im = -o_ptr[2*k];
  559. z[i][31-k].re = o_ptr[2*k+1];
  560. z[i][31-k].im = o_ptr[128+2*k+1];
  561. }
  562. }
  563. /* apply AC-3 post-rotation & reordering */
  564. for(k=0; k<64; k++) {
  565. o_ptr[ 2*k ] = -z[0][ k].im;
  566. o_ptr[ 2*k+1] = z[0][63-k].re;
  567. o_ptr[128+2*k ] = -z[0][ k].re;
  568. o_ptr[128+2*k+1] = z[0][63-k].im;
  569. o_ptr[256+2*k ] = -z[1][ k].re;
  570. o_ptr[256+2*k+1] = z[1][63-k].im;
  571. o_ptr[384+2*k ] = z[1][ k].im;
  572. o_ptr[384+2*k+1] = -z[1][63-k].re;
  573. }
  574. }
  575. /* IMDCT Transform. */
  576. static inline void do_imdct(AC3DecodeContext *ctx)
  577. {
  578. int ch;
  579. if (ctx->output_mode & AC3_OUTPUT_LFEON) {
  580. ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
  581. ctx->transform_coeffs[0], ctx->tmp_imdct);
  582. ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output,
  583. ctx->window, ctx->delay[0], 384, 256, 1);
  584. ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256,
  585. ctx->window, 256);
  586. }
  587. for (ch=1; ch<=ctx->nfchans; ch++) {
  588. if (ctx->blksw[ch-1])
  589. do_imdct_256(ctx, ch);
  590. else
  591. ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
  592. ctx->transform_coeffs[ch],
  593. ctx->tmp_imdct);
  594. ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output,
  595. ctx->window, ctx->delay[ch], 384, 256, 1);
  596. ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256,
  597. ctx->window, 256);
  598. }
  599. }
  600. /* Parse the audio block from ac3 bitstream.
  601. * This function extract the audio block from the ac3 bitstream
  602. * and produces the output for the block. This function must
  603. * be called for each of the six audio block in the ac3 bitstream.
  604. */
  605. static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
  606. {
  607. int nfchans = ctx->nfchans;
  608. int acmod = ctx->acmod;
  609. int i, bnd, seg, grpsize, ch;
  610. GetBitContext *gb = &ctx->gb;
  611. int bit_alloc_flags = 0;
  612. int8_t *dexps;
  613. int mstrcplco, cplcoexp, cplcomant;
  614. int dynrng, chbwcod, ngrps, cplabsexp, skipl;
  615. for (i = 0; i < nfchans; i++) /*block switch flag */
  616. ctx->blksw[i] = get_bits1(gb);
  617. ctx->dither_all = 1;
  618. for (i = 0; i < nfchans; i++) { /* dithering flag */
  619. ctx->dithflag[i] = get_bits1(gb);
  620. if(!ctx->dithflag[i])
  621. ctx->dither_all = 0;
  622. }
  623. if (get_bits1(gb)) { /* dynamic range */
  624. dynrng = get_sbits(gb, 8);
  625. ctx->dynrng = (((dynrng & 0x1f) | 0x20) << 13) * pow(2.0, -(18 - (dynrng >> 5)));
  626. } else if(blk == 0) {
  627. ctx->dynrng = 1.0;
  628. }
  629. if(acmod == AC3_ACMOD_DUALMONO) { /* dynamic range 1+1 mode */
  630. if(get_bits1(gb)) {
  631. dynrng = get_sbits(gb, 8);
  632. ctx->dynrng2 = (((dynrng & 0x1f) | 0x20) << 13) * pow(2.0, -(18 - (dynrng >> 5)));
  633. } else if(blk == 0) {
  634. ctx->dynrng2 = 1.0;
  635. }
  636. }
  637. if (get_bits1(gb)) { /* coupling strategy */
  638. ctx->cplinu = get_bits1(gb);
  639. ctx->cplbndstrc = 0;
  640. if (ctx->cplinu) { /* coupling in use */
  641. int cplbegf, cplendf;
  642. for (i = 0; i < nfchans; i++)
  643. ctx->chincpl[i] = get_bits1(gb);
  644. if (acmod == AC3_ACMOD_STEREO)
  645. ctx->phsflginu = get_bits1(gb); //phase flag in use
  646. cplbegf = get_bits(gb, 4);
  647. cplendf = get_bits(gb, 4);
  648. if (3 + cplendf - cplbegf < 0) {
  649. av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);
  650. return -1;
  651. }
  652. ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
  653. ctx->cplstrtmant = cplbegf * 12 + 37;
  654. ctx->cplendmant = cplendf * 12 + 73;
  655. for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
  656. if (get_bits1(gb)) {
  657. ctx->cplbndstrc |= 1 << i;
  658. ctx->ncplbnd--;
  659. }
  660. } else {
  661. for (i = 0; i < nfchans; i++)
  662. ctx->chincpl[i] = 0;
  663. }
  664. }
  665. if (ctx->cplinu) {
  666. ctx->cplcoe = 0;
  667. for (i = 0; i < nfchans; i++)
  668. if (ctx->chincpl[i])
  669. if (get_bits1(gb)) { /* coupling co-ordinates */
  670. ctx->cplcoe |= 1 << i;
  671. mstrcplco = 3 * get_bits(gb, 2);
  672. for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
  673. cplcoexp = get_bits(gb, 4);
  674. cplcomant = get_bits(gb, 4);
  675. if (cplcoexp == 15)
  676. ctx->cplco[i][bnd] = cplcomant / 16.0f;
  677. else
  678. ctx->cplco[i][bnd] = (cplcomant + 16.0f) / 32.0f;
  679. ctx->cplco[i][bnd] *= scale_factors[cplcoexp + mstrcplco];
  680. }
  681. }
  682. if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
  683. for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
  684. if (get_bits1(gb))
  685. ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
  686. }
  687. if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
  688. ctx->rematstr = get_bits1(gb);
  689. if (ctx->rematstr) {
  690. ctx->nrematbnd = 4;
  691. if(ctx->cplinu && ctx->cplstrtmant <= 61)
  692. ctx->nrematbnd -= 1 + (ctx->cplstrtmant == 37);
  693. for(bnd=0; bnd<ctx->nrematbnd; bnd++)
  694. ctx->rematflg[bnd] = get_bits1(gb);
  695. }
  696. }
  697. ctx->cplexpstr = EXP_REUSE;
  698. ctx->lfeexpstr = EXP_REUSE;
  699. if (ctx->cplinu) /* coupling exponent strategy */
  700. ctx->cplexpstr = get_bits(gb, 2);
  701. for (i = 0; i < nfchans; i++) /* channel exponent strategy */
  702. ctx->chexpstr[i] = get_bits(gb, 2);
  703. if (ctx->lfeon) /* lfe exponent strategy */
  704. ctx->lfeexpstr = get_bits1(gb);
  705. for (i = 0; i < nfchans; i++) /* channel bandwidth code */
  706. if (ctx->chexpstr[i] != EXP_REUSE) {
  707. if (ctx->chincpl[i])
  708. ctx->endmant[i] = ctx->cplstrtmant;
  709. else {
  710. chbwcod = get_bits(gb, 6);
  711. if (chbwcod > 60) {
  712. av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
  713. return -1;
  714. }
  715. ctx->endmant[i] = chbwcod * 3 + 73;
  716. }
  717. }
  718. if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */
  719. bit_alloc_flags = 64;
  720. cplabsexp = get_bits(gb, 4) << 1;
  721. ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
  722. decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant);
  723. }
  724. for (i = 0; i < nfchans; i++) /* fbw channel exponents */
  725. if (ctx->chexpstr[i] != EXP_REUSE) {
  726. bit_alloc_flags |= 1 << i;
  727. grpsize = 3 << (ctx->chexpstr[i] - 1);
  728. ngrps = (ctx->endmant[i] + grpsize - 4) / grpsize;
  729. dexps = ctx->dexps[i];
  730. dexps[0] = get_bits(gb, 4);
  731. decode_exponents(gb, ctx->chexpstr[i], ngrps, dexps[0], dexps + 1);
  732. skip_bits(gb, 2); /* skip gainrng */
  733. }
  734. if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
  735. bit_alloc_flags |= 32;
  736. ctx->dlfeexps[0] = get_bits(gb, 4);
  737. decode_exponents(gb, ctx->lfeexpstr, 2, ctx->dlfeexps[0], ctx->dlfeexps + 1);
  738. }
  739. if (get_bits1(gb)) { /* bit allocation information */
  740. bit_alloc_flags = 127;
  741. ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
  742. ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
  743. ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)];
  744. ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
  745. ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)];
  746. }
  747. if (get_bits1(gb)) { /* snroffset */
  748. int csnr;
  749. bit_alloc_flags = 127;
  750. csnr = (get_bits(gb, 6) - 15) << 4;
  751. if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */
  752. ctx->cplsnroffst = (csnr + get_bits(gb, 4)) << 2;
  753. ctx->cplfgain = ff_fgaintab[get_bits(gb, 3)];
  754. }
  755. for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
  756. ctx->snroffst[i] = (csnr + get_bits(gb, 4)) << 2;
  757. ctx->fgain[i] = ff_fgaintab[get_bits(gb, 3)];
  758. }
  759. if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */
  760. ctx->lfesnroffst = (csnr + get_bits(gb, 4)) << 2;
  761. ctx->lfefgain = ff_fgaintab[get_bits(gb, 3)];
  762. }
  763. }
  764. if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
  765. bit_alloc_flags |= 64;
  766. ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
  767. ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
  768. }
  769. if (get_bits1(gb)) { /* delta bit allocation information */
  770. bit_alloc_flags = 127;
  771. if (ctx->cplinu) {
  772. ctx->cpldeltbae = get_bits(gb, 2);
  773. if (ctx->cpldeltbae == DBA_RESERVED) {
  774. av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
  775. return -1;
  776. }
  777. }
  778. for (i = 0; i < nfchans; i++) {
  779. ctx->deltbae[i] = get_bits(gb, 2);
  780. if (ctx->deltbae[i] == DBA_RESERVED) {
  781. av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
  782. return -1;
  783. }
  784. }
  785. if (ctx->cplinu)
  786. if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
  787. ctx->cpldeltnseg = get_bits(gb, 3);
  788. for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
  789. ctx->cpldeltoffst[seg] = get_bits(gb, 5);
  790. ctx->cpldeltlen[seg] = get_bits(gb, 4);
  791. ctx->cpldeltba[seg] = get_bits(gb, 3);
  792. }
  793. }
  794. for (i = 0; i < nfchans; i++)
  795. if (ctx->deltbae[i] == DBA_NEW) {/*channel delta offset, len and bit allocation */
  796. ctx->deltnseg[i] = get_bits(gb, 3);
  797. for (seg = 0; seg <= ctx->deltnseg[i]; seg++) {
  798. ctx->deltoffst[i][seg] = get_bits(gb, 5);
  799. ctx->deltlen[i][seg] = get_bits(gb, 4);
  800. ctx->deltba[i][seg] = get_bits(gb, 3);
  801. }
  802. }
  803. } else if(blk == 0) {
  804. if(ctx->cplinu)
  805. ctx->cpldeltbae = DBA_NONE;
  806. for(i=0; i<nfchans; i++) {
  807. ctx->deltbae[i] = DBA_NONE;
  808. }
  809. }
  810. if (bit_alloc_flags) {
  811. if (ctx->cplinu && (bit_alloc_flags & 64))
  812. ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
  813. ctx->dcplexps, ctx->cplstrtmant,
  814. ctx->cplendmant, ctx->cplsnroffst,
  815. ctx->cplfgain, 0,
  816. ctx->cpldeltbae, ctx->cpldeltnseg,
  817. ctx->cpldeltoffst, ctx->cpldeltlen,
  818. ctx->cpldeltba);
  819. for (i = 0; i < nfchans; i++)
  820. if ((bit_alloc_flags >> i) & 1)
  821. ac3_parametric_bit_allocation(&ctx->bit_alloc_params,
  822. ctx->bap[i], ctx->dexps[i], 0,
  823. ctx->endmant[i], ctx->snroffst[i],
  824. ctx->fgain[i], 0, ctx->deltbae[i],
  825. ctx->deltnseg[i], ctx->deltoffst[i],
  826. ctx->deltlen[i], ctx->deltba[i]);
  827. if (ctx->lfeon && (bit_alloc_flags & 32))
  828. ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
  829. ctx->dlfeexps, 0, 7, ctx->lfesnroffst,
  830. ctx->lfefgain, 1,
  831. DBA_NONE, 0, NULL, NULL, NULL);
  832. }
  833. if (get_bits1(gb)) { /* unused dummy data */
  834. skipl = get_bits(gb, 9);
  835. while(skipl--)
  836. skip_bits(gb, 8);
  837. }
  838. /* unpack the transform coefficients
  839. * * this also uncouples channels if coupling is in use.
  840. */
  841. if (get_transform_coeffs(ctx)) {
  842. av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
  843. return -1;
  844. }
  845. /* recover coefficients if rematrixing is in use */
  846. if(ctx->acmod == AC3_ACMOD_STEREO)
  847. do_rematrixing(ctx);
  848. /* apply scaling to coefficients (headroom, dynrng) */
  849. if(ctx->lfeon) {
  850. for(i=0; i<7; i++) {
  851. ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
  852. }
  853. }
  854. for(ch=1; ch<=ctx->nfchans; ch++) {
  855. float gain = 2.0f;
  856. if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
  857. gain *= ctx->dynrng2;
  858. } else {
  859. gain *= ctx->dynrng;
  860. }
  861. for(i=0; i<ctx->endmant[ch-1]; i++) {
  862. ctx->transform_coeffs[ch][i] *= gain;
  863. }
  864. }
  865. do_imdct(ctx);
  866. return 0;
  867. }
  868. static inline int16_t convert(int32_t i)
  869. {
  870. if (i > 0x43c07fff)
  871. return 32767;
  872. else if (i <= 0x43bf8000)
  873. return -32768;
  874. else
  875. return (i - 0x43c00000);
  876. }
  877. /* Decode ac3 frame.
  878. *
  879. * @param avctx Pointer to AVCodecContext
  880. * @param data Pointer to pcm smaples
  881. * @param data_size Set to number of pcm samples produced by decoding
  882. * @param buf Data to be decoded
  883. * @param buf_size Size of the buffer
  884. */
  885. static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  886. {
  887. AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
  888. int16_t *out_samples = (int16_t *)data;
  889. int i, j, k, start;
  890. int32_t *int_ptr[6];
  891. for (i = 0; i < 6; i++)
  892. int_ptr[i] = (int32_t *)(&ctx->output[i]);
  893. //Initialize the GetBitContext with the start of valid AC3 Frame.
  894. init_get_bits(&ctx->gb, buf, buf_size * 8);
  895. //Parse the syncinfo.
  896. if (ac3_parse_header(ctx)) {
  897. av_log(avctx, AV_LOG_ERROR, "\n");
  898. *data_size = 0;
  899. return buf_size;
  900. }
  901. avctx->sample_rate = ctx->sampling_rate;
  902. avctx->bit_rate = ctx->bit_rate;
  903. /* channel config */
  904. if (avctx->channels == 0) {
  905. avctx->channels = ctx->out_channels;
  906. }
  907. if(avctx->channels != ctx->out_channels) {
  908. av_log(avctx, AV_LOG_ERROR, "Cannot mix AC3 to %d channels.\n",
  909. avctx->channels);
  910. return -1;
  911. }
  912. //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);
  913. //Parse the Audio Blocks.
  914. for (i = 0; i < NB_BLOCKS; i++) {
  915. if (ac3_parse_audio_block(ctx, i)) {
  916. av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
  917. *data_size = 0;
  918. return ctx->frame_size;
  919. }
  920. start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
  921. for (k = 0; k < 256; k++)
  922. for (j = start; j <= ctx->nfchans; j++)
  923. *(out_samples++) = convert(int_ptr[j][k]);
  924. }
  925. *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
  926. return ctx->frame_size;
  927. }
  928. /* Uninitialize ac3 decoder.
  929. */
  930. static int ac3_decode_end(AVCodecContext *avctx)
  931. {
  932. AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
  933. ff_mdct_end(&ctx->imdct_512);
  934. ff_mdct_end(&ctx->imdct_256);
  935. return 0;
  936. }
  937. AVCodec ac3_decoder = {
  938. .name = "ac3",
  939. .type = CODEC_TYPE_AUDIO,
  940. .id = CODEC_ID_AC3,
  941. .priv_data_size = sizeof (AC3DecodeContext),
  942. .init = ac3_decode_init,
  943. .close = ac3_decode_end,
  944. .decode = ac3_decode_frame,
  945. };