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.

1114 lines
36KB

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