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.

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