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.

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