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.

1747 lines
57KB

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