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.

1950 lines
65KB

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