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