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.

1845 lines
59KB

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