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.

2117 lines
67KB

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