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.

1836 lines
63KB

  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 "ac3.h"
  26. #include "ac3tab.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 MAX_BLOCK_SIZE 256
  35. #define MAX_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. uint8_t mstrcplco[5]; //master coupling co-ordinate for channels in use
  131. uint8_t cplcoexp[5][18]; //coupling co-ordinate exponenets
  132. uint8_t cplcomant[5][18]; //coupling co-ordinate mantissas
  133. uint32_t phsflg; //phase flag per band
  134. uint8_t rematflg; //rematrixing flag
  135. uint8_t cplexpstr; //coupling exponent strategy
  136. uint8_t chexpstr[5]; //channel exponent strategy
  137. uint8_t lfeexpstr; //lfe exponent strategy
  138. uint8_t chbwcod[5]; //channel bandwdith code for channels in use
  139. uint8_t cplabsexp; //coupling absolute exponent
  140. uint8_t gainrng[5]; //gain range
  141. uint8_t sdcycod; //slow decay code
  142. uint8_t fdcycod; //fast decay code
  143. uint8_t sgaincod; //slow gain code
  144. uint8_t dbpbcod; //dB per bit code
  145. uint8_t floorcod; //masking floor code
  146. uint8_t csnroffst; //coarse SNR offset
  147. uint8_t cplfsnroffst; //coupling fine SNR offset
  148. uint8_t cplfgaincod; //coupling fast gain code
  149. uint8_t fsnroffst[5]; //fine SNR offset for channels in use
  150. uint8_t fgaincod[5]; //fast gain code for channels in use
  151. uint8_t lfefsnroffst; //lfe fine SNR offset
  152. uint8_t lfefgaincod; //lfe fast gain code
  153. uint8_t cplfleak; //coupling fast leak initialization value
  154. uint8_t cplsleak; //coupling slow leak initialization value
  155. uint8_t cpldeltbae; //coupling delta bit allocation exists
  156. uint8_t deltbae[5]; //delta bit allocation exists for channels in use
  157. uint8_t cpldeltnseg; //coupling delta bit allocation number of segments
  158. uint8_t cpldeltoffst[8]; //coupling delta offset
  159. uint8_t cpldeltlen[8]; //coupling delta len
  160. uint8_t cpldeltba[8]; //coupling delta bit allocation
  161. uint8_t deltnseg[5]; //delta bit allocation number of segments per channel
  162. uint8_t deltoffst[5][8]; //delta offset for channels in use
  163. uint8_t deltlen[5][8]; //delta len for channels in use
  164. uint8_t deltba[5][8]; //delta bit allocation
  165. uint16_t skipl; //skip length
  166. /* Derived Attributes */
  167. int ncplsubnd; //number of active coupling sub bands = 3 + cplendf - cplbegf
  168. int ncplbnd; //derived from ncplsubnd and cplbndstrc
  169. int ncplgrps; //derived from ncplsubnd, cplexpstr
  170. int nchgrps[5]; //derived from chexpstr, and cplbegf or chbwcod
  171. int nchmant[5]; //derived from cplbegf or chbwcod
  172. int ncplmant; //derived from ncplsubnd = 12 * ncplsubnd
  173. uint8_t cplstrtbnd; //coupling start band for bit allocation
  174. uint8_t cplstrtmant; //coupling start mantissa
  175. uint8_t cplendmant; //coupling end mantissa
  176. uint8_t endmant[5]; //channel end mantissas
  177. uint8_t dcplexps[256]; //decoded coupling exponents
  178. uint8_t dexps[5][256]; //decoded fbw channel exponents
  179. uint8_t dlfeexps[256]; //decoded lfe exponents
  180. uint8_t cplbap[256]; //coupling bit allocation parameters table
  181. uint8_t bap[5][256]; //fbw channels bit allocation parameters table
  182. uint8_t lfebap[256]; //lfe bit allocaiton parameters table
  183. DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][MAX_BLOCK_SIZE]); //transform coefficients
  184. DECLARE_ALIGNED_16(float, cplcoeffs[256]); //temporary storage for coupling transform coefficients
  185. DECLARE_ALIGNED_16(float, block_output[MAX_CHANNELS][MAX_BLOCK_SIZE]);
  186. float cplco[5][18]; //coupling coordinates
  187. float chcoeffs[6]; //channel coefficients for downmix
  188. } ac3_audio_block;
  189. #define AC3_OUTPUT_UNMODIFIED 0x00
  190. #define AC3_OUTPUT_MONO 0x01
  191. #define AC3_OUTPUT_STEREO 0x02
  192. #define AC3_OUTPUT_DOLBY 0x03
  193. #define AC3_INPUT_DUALMONO 0x00
  194. #define AC3_INPUT_MONO 0x01
  195. #define AC3_INPUT_STEREO 0x02
  196. #define AC3_INPUT_3F 0x03
  197. #define AC3_INPUT_2F_1R 0x04
  198. #define AC3_INPUT_3F_1R 0x05
  199. #define AC3_INPUT_2F_2R 0x06
  200. #define AC3_INPUT_3F_2R 0x07
  201. /* BEGIN Mersenne Twister Code. */
  202. #define N 624
  203. #define M 397
  204. #define MATRIX_A 0x9908b0df
  205. #define UPPER_MASK 0x80000000
  206. #define LOWER_MASK 0x7fffffff
  207. typedef struct {
  208. uint32_t mt[N];
  209. int mti;
  210. } dither_state;
  211. static void dither_seed(dither_state *state, uint32_t seed)
  212. {
  213. if (seed == 0)
  214. seed = 0x1f2e3d4c;
  215. state->mt[0] = seed;
  216. for (state->mti = 1; state->mti < N; state->mti++)
  217. state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
  218. }
  219. static uint32_t dither_uint32(dither_state *state)
  220. {
  221. uint32_t y;
  222. static const uint32_t mag01[2] = { 0x00, MATRIX_A };
  223. int kk;
  224. if (state->mti >= N) {
  225. for (kk = 0; kk < N - M; kk++) {
  226. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  227. state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x01];
  228. }
  229. for (;kk < N - 1; kk++) {
  230. y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
  231. state->mt[kk] = state->mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x01];
  232. }
  233. y = (state->mt[N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
  234. state->mt[N - 1] = state->mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x01];
  235. state->mti = 0;
  236. }
  237. y = state->mt[state->mti++];
  238. y ^= (y >> 11);
  239. y ^= ((y << 7) & 0x9d2c5680);
  240. y ^= ((y << 15) & 0xefc60000);
  241. y ^= (y >> 18);
  242. return y;
  243. }
  244. static inline int16_t dither_int16(dither_state *state)
  245. {
  246. return ((dither_uint32(state) << 16) >> 16);
  247. }
  248. /* END Mersenne Twister */
  249. /* AC3 Context. */
  250. typedef struct {
  251. ac3_sync_info sync_info;
  252. ac3_bsi bsi;
  253. ac3_audio_block audio_block;
  254. dither_state state;
  255. MDCTContext imdct_ctx_256;
  256. MDCTContext imdct_ctx_512;
  257. GetBitContext gb;
  258. int output;
  259. DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][MAX_BLOCK_SIZE]);
  260. DECLARE_ALIGNED_16(FFTSample, tmp_imdct[MAX_BLOCK_SIZE * 2]);
  261. DECLARE_ALIGNED_16(FFTSample, tmp_output[MAX_BLOCK_SIZE * 2]);
  262. } AC3DecodeContext;
  263. static void ac3_common_init1(void)
  264. {
  265. int i, j, k, l, v;
  266. /* compute bndtab and masktab from bandsz */
  267. k = 0;
  268. l = 0;
  269. for(i=0;i<50;i++) {
  270. bndtab[i] = l;
  271. v = bndsz[i];
  272. for(j=0;j<v;j++) masktab[k++]=i;
  273. l += v;
  274. }
  275. masktab[253] = masktab[254] = masktab[255] = 0;
  276. bndtab[50] = 0;
  277. }
  278. static int ac3_decode_init(AVCodecContext *avctx)
  279. {
  280. AC3DecodeContext *ctx = avctx->priv_data;
  281. ac3_common_init1();
  282. ff_mdct_init(&ctx->imdct_ctx_256, 8, 1);
  283. ff_mdct_init(&ctx->imdct_ctx_512, 9, 1);
  284. dither_seed(&ctx->state, 0);
  285. return 0;
  286. }
  287. static int ac3_synchronize(uint8_t *buf, int buf_size)
  288. {
  289. int i;
  290. for (i = 0; i < buf_size - 1; i++)
  291. if (buf[i] == 0x0b && buf[i + 1] == 0x77)
  292. return i;
  293. return -1;
  294. }
  295. //Returns -1 when 'fscod' is not valid;
  296. static int ac3_parse_sync_info(AC3DecodeContext *ctx)
  297. {
  298. ac3_sync_info *sync_info = &ctx->sync_info;
  299. ac3_bsi *bsi = &ctx->bsi;
  300. GetBitContext *gb = &ctx->gb;
  301. sync_info->sync_word = get_bits(gb, 16);
  302. sync_info->crc1 = get_bits(gb, 16);
  303. sync_info->fscod = get_bits(gb, 2);
  304. if (sync_info->fscod == 0x03)
  305. return 0;
  306. sync_info->frmsizecod = get_bits(gb, 6);
  307. if (sync_info->frmsizecod >= 38)
  308. return 0;
  309. sync_info->sampling_rate = ac3_freqs[sync_info->fscod];
  310. sync_info->bit_rate = ac3_bitratetab[sync_info->frmsizecod >> 1];
  311. /* we include it here in order to determine validity of ac3 frame */
  312. bsi->bsid = get_bits(gb, 5);
  313. if (bsi->bsid > 0x08)
  314. return 0;
  315. bsi->bsmod = get_bits(gb, 3);
  316. switch (sync_info->fscod) {
  317. case 0x00:
  318. sync_info->framesize = 4 * sync_info->bit_rate;
  319. return sync_info->framesize;
  320. case 0x01:
  321. sync_info->framesize = 2 * (320 * sync_info->bit_rate / 147 + (sync_info->frmsizecod & 1));
  322. return sync_info->framesize;
  323. case 0x02:
  324. sync_info->framesize = 6 * sync_info->bit_rate;
  325. return sync_info->framesize;
  326. }
  327. /* never reached */
  328. return 0;
  329. }
  330. //Returns -1 when
  331. static int ac3_parse_bsi(AC3DecodeContext *ctx)
  332. {
  333. ac3_bsi *bsi = &ctx->bsi;
  334. uint32_t *flags = &bsi->flags;
  335. GetBitContext *gb = &ctx->gb;
  336. int i;
  337. *flags = 0;
  338. bsi->cmixlev = 0;
  339. bsi->surmixlev = 0;
  340. bsi->dsurmod = 0;
  341. ctx->audio_block.cpldeltbae = AC3_DBASTR_NONE;
  342. ctx->audio_block.cpldeltnseg = 0;
  343. for (i = 0; i < 5; i++) {
  344. ctx->audio_block.deltbae[i] = AC3_DBASTR_NONE;
  345. ctx->audio_block.deltnseg[i] = 0;
  346. }
  347. bsi->acmod = get_bits(gb, 3);
  348. if (bsi->acmod & 0x01 && bsi->acmod != 0x01)
  349. bsi->cmixlev = get_bits(gb, 2);
  350. if (bsi->acmod & 0x04)
  351. bsi->surmixlev = get_bits(gb, 2);
  352. if (bsi->acmod == 0x02)
  353. bsi->dsurmod = get_bits(gb, 2);
  354. if (get_bits1(gb))
  355. *flags |= AC3_BSI_LFEON;
  356. bsi->dialnorm = get_bits(gb, 5);
  357. if (get_bits1(gb)) {
  358. *flags |= AC3_BSI_COMPRE;
  359. bsi->compr = get_bits(gb, 8);
  360. }
  361. if (get_bits1(gb)) {
  362. *flags |= AC3_BSI_LANGCODE;
  363. bsi->langcod = get_bits(gb, 8);
  364. }
  365. if (get_bits1(gb)) {
  366. *flags |= AC3_BSI_AUDPRODIE;
  367. bsi->mixlevel = get_bits(gb, 5);
  368. bsi->roomtyp = get_bits(gb, 2);
  369. }
  370. if (bsi->acmod == 0x00) {
  371. bsi->dialnorm2 = get_bits(gb, 5);
  372. if (get_bits1(gb)) {
  373. *flags |= AC3_BSI_COMPR2E;
  374. bsi->compr2 = get_bits(gb, 8);
  375. }
  376. if (get_bits1(gb)) {
  377. *flags |= AC3_BSI_LANGCOD2E;
  378. bsi->langcod2 = get_bits(gb, 8);
  379. }
  380. if (get_bits1(gb)) {
  381. *flags |= AC3_BSI_AUDPRODIE;
  382. bsi->mixlevel2 = get_bits(gb, 5);
  383. bsi->roomtyp2 = get_bits(gb, 2);
  384. }
  385. }
  386. if (get_bits1(gb))
  387. *flags |= AC3_BSI_COPYRIGHTB;
  388. if (get_bits1(gb))
  389. *flags |= AC3_BSI_ORIGBS;
  390. if (get_bits1(gb)) {
  391. *flags |= AC3_BSI_TIMECOD1E;
  392. bsi->timecod1 = get_bits(gb, 14);
  393. }
  394. if (get_bits1(gb)) {
  395. *flags |= AC3_BSI_TIMECOD2E;
  396. bsi->timecod2 = get_bits(gb, 14);
  397. }
  398. if (get_bits1(gb)) {
  399. *flags |= AC3_BSI_ADDBSIE;
  400. bsi->addbsil = get_bits(gb, 6);
  401. for (i = 0; i < (bsi->addbsil + 1); i++)
  402. skip_bits(gb, 8);
  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 (bndend != 7 || 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 (bndend != 7 || 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 (bndend != 7 || 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 & AC3_AB_CPLINU)
  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->flags & AC3_AB_CPLINU) && (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. static inline float to_float(uint8_t exp, int16_t mantissa)
  689. {
  690. return ((float) (mantissa * scale_factors[exp]));
  691. }
  692. typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
  693. uint8_t gcodes[3];
  694. uint8_t gcptr;
  695. } mant_group;
  696. /* Get the transform coefficients for particular channel */
  697. static int get_transform_coeffs1(uint8_t *exps, uint8_t *bap, float chcoeff,
  698. float *coeffs, int start, int end, int dith_flag, GetBitContext *gb,
  699. dither_state *state)
  700. {
  701. int16_t mantissa;
  702. int i;
  703. int gcode;
  704. mant_group l3_grp, l5_grp, l11_grp;
  705. for (i = 0; i < 3; i++)
  706. l3_grp.gcodes[i] = l5_grp.gcodes[i] = l11_grp.gcodes[i] = -1;
  707. l3_grp.gcptr = l5_grp.gcptr = 3;
  708. l11_grp.gcptr = 2;
  709. i = 0;
  710. while (i < start)
  711. coeffs[i++] = 0;
  712. for (i = start; i < end; i++) {
  713. switch (bap[i]) {
  714. case 0:
  715. if (!dith_flag) {
  716. coeffs[i] = 0;
  717. continue;
  718. }
  719. else {
  720. mantissa = dither_int16(state);
  721. coeffs[i] = to_float(exps[i], mantissa) * chcoeff;
  722. continue;
  723. }
  724. case 1:
  725. if (l3_grp.gcptr > 2) {
  726. gcode = get_bits(gb, 5);
  727. if (gcode > 26)
  728. return -1;
  729. l3_grp.gcodes[0] = gcode / 9;
  730. l3_grp.gcodes[1] = (gcode % 9) / 3;
  731. l3_grp.gcodes[2] = (gcode % 9) % 3;
  732. l3_grp.gcptr = 0;
  733. }
  734. mantissa = l3_q_tab[l3_grp.gcodes[l3_grp.gcptr++]];
  735. coeffs[i] = to_float(exps[i], mantissa) * chcoeff;
  736. continue;
  737. case 2:
  738. if (l5_grp.gcptr > 2) {
  739. gcode = get_bits(gb, 7);
  740. if (gcode > 124)
  741. return -1;
  742. l5_grp.gcodes[0] = gcode / 25;
  743. l5_grp.gcodes[1] = (gcode % 25) / 5;
  744. l5_grp.gcodes[2] = (gcode % 25) % 5;
  745. l5_grp.gcptr = 0;
  746. }
  747. mantissa = l5_q_tab[l5_grp.gcodes[l5_grp.gcptr++]];
  748. coeffs[i] = to_float(exps[i], mantissa) * chcoeff;
  749. continue;
  750. case 3:
  751. mantissa = get_bits(gb, 3);
  752. if (mantissa > 6)
  753. return -1;
  754. mantissa = l7_q_tab[mantissa];
  755. coeffs[i] = to_float(exps[i], mantissa);
  756. continue;
  757. case 4:
  758. if (l11_grp.gcptr > 1) {
  759. gcode = get_bits(gb, 7);
  760. if (gcode > 120)
  761. return -1;
  762. l11_grp.gcodes[0] = gcode / 11;
  763. l11_grp.gcodes[1] = gcode % 11;
  764. }
  765. mantissa = l11_q_tab[l11_grp.gcodes[l11_grp.gcptr++]];
  766. coeffs[i] = to_float(exps[i], mantissa) * chcoeff;
  767. continue;
  768. case 5:
  769. mantissa = get_bits(gb, 4);
  770. if (mantissa > 14)
  771. return -1;
  772. mantissa = l15_q_tab[mantissa];
  773. coeffs[i] = to_float(exps[i], mantissa) * chcoeff;
  774. continue;
  775. default:
  776. mantissa = get_bits(gb, qntztab[bap[i]]) << (16 - qntztab[bap[i]]);
  777. coeffs[i] = to_float(exps[i], mantissa) * chcoeff;
  778. continue;
  779. }
  780. }
  781. i = end;
  782. while (i < 256)
  783. coeffs[i++] = 0;
  784. return 0;
  785. }
  786. static void uncouple_channels(AC3DecodeContext * ctx)
  787. {
  788. ac3_audio_block *ab = &ctx->audio_block;
  789. int ch, sbnd, bin;
  790. int index;
  791. int16_t mantissa;
  792. /* uncouple channels */
  793. for (ch = 0; ch < ctx->bsi.nfchans; ch++)
  794. if (ab->chincpl & (1 << ch))
  795. for (sbnd = ab->cplbegf; sbnd < 3 + ab->cplendf; sbnd++)
  796. for (bin = 0; bin < 12; bin++) {
  797. index = sbnd * 12 + bin + 37;
  798. ab->transform_coeffs[ch + 1][index] = ab->cplcoeffs[index] * ab->cplco[ch][sbnd] * ab->chcoeffs[ch];
  799. /* generate dither if required */
  800. if (!ab->bap[ch][index] && (ab->chincpl & (1 << ch)) && (ab->dithflag & (1 << ch))) {
  801. mantissa = dither_int16(&ctx->state);
  802. ab->transform_coeffs[ch + 1][index] = to_float(ab->dexps[ch][index], mantissa) * ab->chcoeffs[ch];
  803. }
  804. }
  805. }
  806. static int get_transform_coeffs(AC3DecodeContext * ctx)
  807. {
  808. int i;
  809. ac3_audio_block *ab = &ctx->audio_block;
  810. int got_cplchan = 0;
  811. int dithflag = 0;
  812. for (i = 0; i < ctx->bsi.nfchans; i++) {
  813. dithflag = ab->dithflag & (1 << i);
  814. /* transform coefficients for individual channel */
  815. if (get_transform_coeffs1(ab->dexps[i], ab->bap[i], ab->chcoeffs[i], ab->transform_coeffs[i + 1],
  816. 0, ab->endmant[i], dithflag, &ctx->gb, &ctx->state))
  817. return -1;
  818. /* tranform coefficients for coupling channels */
  819. if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)) && !got_cplchan) {
  820. if (get_transform_coeffs1(ab->dcplexps, ab->cplbap, 1.0f, ab->cplcoeffs,
  821. ab->cplstrtmant, ab->cplendmant, 0, &ctx->gb, &ctx->state))
  822. return -1;
  823. got_cplchan = 1;
  824. }
  825. }
  826. if (ctx->bsi.flags & AC3_BSI_LFEON)
  827. if (get_transform_coeffs1(ab->dlfeexps, ab->lfebap, 1.0f, ab->transform_coeffs[0], 0, 7, 0, &ctx->gb, &ctx->state))
  828. return -1;
  829. /* uncouple the channels from the coupling channel */
  830. if (ab->flags & AC3_AB_CPLINU)
  831. uncouple_channels(ctx);
  832. return 0;
  833. }
  834. /* generate coupling co-ordinates for each coupling subband
  835. * from coupling co-ordinates of each band and coupling band
  836. * structure information
  837. */
  838. static void generate_coupling_coordinates(AC3DecodeContext * ctx)
  839. {
  840. ac3_audio_block *ab = &ctx->audio_block;
  841. uint8_t exp, mstrcplco;
  842. int16_t mant;
  843. uint32_t cplbndstrc = (1 << ab->ncplsubnd) >> 1;
  844. int ch, bnd, sbnd;
  845. float cplco;
  846. if (ab->cplcoe)
  847. for (ch = 0; ch < ctx->bsi.nfchans; ch++)
  848. if (ab->cplcoe & (1 << ch)) {
  849. mstrcplco = 3 * ab->mstrcplco[ch];
  850. sbnd = ab->cplbegf;
  851. for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
  852. exp = ab->cplcoexp[ch][bnd];
  853. if (exp == 15)
  854. mant = ab->cplcomant[ch][bnd] <<= 14;
  855. else
  856. mant = (ab->cplcomant[ch][bnd] | 0x10) << 13;
  857. cplco = to_float(exp + mstrcplco, mant);
  858. if (ctx->bsi.acmod == 0x02 && (ab->flags & AC3_AB_PHSFLGINU) && ch == 1
  859. && (ab->phsflg & (1 << bnd)))
  860. cplco = -cplco; /* invert the right channel */
  861. ab->cplco[ch][sbnd++] = cplco;
  862. while (cplbndstrc & ab->cplbndstrc) {
  863. cplbndstrc >>= 1;
  864. ab->cplco[ch][sbnd++] = cplco;
  865. }
  866. cplbndstrc >>= 1;
  867. }
  868. }
  869. }
  870. static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
  871. {
  872. float tmp0, tmp1;
  873. while (start < end) {
  874. tmp0 = ctx->audio_block.transform_coeffs[1][start];
  875. tmp1 = ctx->audio_block.transform_coeffs[2][start];
  876. ctx->audio_block.transform_coeffs[1][start] = tmp0 + tmp1;
  877. ctx->audio_block.transform_coeffs[2][start] = tmp0 - tmp1;
  878. start++;
  879. }
  880. }
  881. static void do_rematrixing(AC3DecodeContext *ctx)
  882. {
  883. ac3_audio_block *ab = &ctx->audio_block;
  884. uint8_t bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
  885. uint8_t bndend;
  886. bndend = FFMIN(ab->endmant[0], ab->endmant[1]);
  887. if (ab->rematflg & 1)
  888. do_rematrixing1(ctx, bnd1, bnd2);
  889. if (ab->rematflg & 2)
  890. do_rematrixing1(ctx, bnd2, bnd3);
  891. if (ab->rematflg & 4) {
  892. if (ab->cplbegf > 0 && ab->cplbegf <= 2 && (ab->flags & AC3_AB_CPLINU))
  893. do_rematrixing1(ctx, bnd3, bndend);
  894. else {
  895. do_rematrixing1(ctx, bnd3, bnd4);
  896. if (ab->rematflg & 8)
  897. do_rematrixing1(ctx, bnd4, bndend);
  898. }
  899. }
  900. }
  901. static void get_downmix_coeffs(AC3DecodeContext *ctx)
  902. {
  903. int from = ctx->bsi.acmod;
  904. int to = ctx->output;
  905. float clev = clevs[ctx->bsi.cmixlev];
  906. float slev = slevs[ctx->bsi.surmixlev];
  907. ac3_audio_block *ab = &ctx->audio_block;
  908. if (to == AC3_OUTPUT_UNMODIFIED)
  909. return;
  910. switch (from) {
  911. case AC3_INPUT_DUALMONO:
  912. switch (to) {
  913. case AC3_OUTPUT_MONO:
  914. case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
  915. ab->chcoeffs[0] *= LEVEL_MINUS_6DB;
  916. ab->chcoeffs[1] *= LEVEL_MINUS_6DB;
  917. break;
  918. }
  919. break;
  920. case AC3_INPUT_MONO:
  921. switch (to) {
  922. case AC3_OUTPUT_STEREO:
  923. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  924. break;
  925. }
  926. break;
  927. case AC3_INPUT_STEREO:
  928. switch (to) {
  929. case AC3_OUTPUT_MONO:
  930. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  931. ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
  932. break;
  933. }
  934. break;
  935. case AC3_INPUT_3F:
  936. switch (to) {
  937. case AC3_OUTPUT_MONO:
  938. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  939. ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
  940. ab->chcoeffs[1] *= clev * LEVEL_PLUS_3DB;
  941. break;
  942. case AC3_OUTPUT_STEREO:
  943. ab->chcoeffs[1] *= clev;
  944. break;
  945. }
  946. break;
  947. case AC3_INPUT_2F_1R:
  948. switch (to) {
  949. case AC3_OUTPUT_MONO:
  950. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  951. ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
  952. ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB;
  953. break;
  954. case AC3_OUTPUT_STEREO:
  955. ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB;
  956. break;
  957. case AC3_OUTPUT_DOLBY:
  958. ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
  959. break;
  960. }
  961. break;
  962. case AC3_INPUT_3F_1R:
  963. switch (to) {
  964. case AC3_OUTPUT_MONO:
  965. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  966. ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
  967. ab->chcoeffs[1] *= clev * LEVEL_PLUS_3DB;
  968. ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB;
  969. break;
  970. case AC3_OUTPUT_STEREO:
  971. ab->chcoeffs[1] *= clev;
  972. ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB;
  973. break;
  974. case AC3_OUTPUT_DOLBY:
  975. ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
  976. ab->chcoeffs[3] *= LEVEL_MINUS_3DB;
  977. break;
  978. }
  979. break;
  980. case AC3_INPUT_2F_2R:
  981. switch (to) {
  982. case AC3_OUTPUT_MONO:
  983. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  984. ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
  985. ab->chcoeffs[2] *= slev * LEVEL_MINUS_3DB;
  986. ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB;
  987. break;
  988. case AC3_OUTPUT_STEREO:
  989. ab->chcoeffs[2] *= slev;
  990. ab->chcoeffs[3] *= slev;
  991. break;
  992. case AC3_OUTPUT_DOLBY:
  993. ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
  994. ab->chcoeffs[3] *= LEVEL_MINUS_3DB;
  995. break;
  996. }
  997. break;
  998. case AC3_INPUT_3F_2R:
  999. switch (to) {
  1000. case AC3_OUTPUT_MONO:
  1001. ab->chcoeffs[0] *= LEVEL_MINUS_3DB;
  1002. ab->chcoeffs[2] *= LEVEL_MINUS_3DB;
  1003. ab->chcoeffs[1] *= clev * LEVEL_PLUS_3DB;
  1004. ab->chcoeffs[3] *= slev * LEVEL_MINUS_3DB;
  1005. ab->chcoeffs[4] *= slev * LEVEL_MINUS_3DB;
  1006. break;
  1007. case AC3_OUTPUT_STEREO:
  1008. ab->chcoeffs[1] *= clev;
  1009. ab->chcoeffs[3] *= slev;
  1010. ab->chcoeffs[4] *= slev;
  1011. break;
  1012. case AC3_OUTPUT_DOLBY:
  1013. ab->chcoeffs[1] *= LEVEL_MINUS_3DB;
  1014. ab->chcoeffs[3] *= LEVEL_MINUS_3DB;
  1015. ab->chcoeffs[4] *= LEVEL_MINUS_3DB;
  1016. break;
  1017. }
  1018. break;
  1019. }
  1020. }
  1021. static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
  1022. {
  1023. int i;
  1024. float (*output)[256] = ctx->audio_block.block_output;
  1025. for (i = 0; i < 256; i++)
  1026. output[1][i] += output[2][i];
  1027. memset(output[2], 0, sizeof(output[2]));
  1028. }
  1029. static inline void mix_dualmono_to_stereo(AC3DecodeContext *ctx)
  1030. {
  1031. int i;
  1032. float tmp;
  1033. float (*output)[256] = ctx->audio_block.block_output;
  1034. for (i = 0; i < 256; i++) {
  1035. tmp = output[1][i] + output[2][i];
  1036. output[1][i] = output[2][i] = tmp;
  1037. }
  1038. }
  1039. static inline void upmix_mono_to_stereo(AC3DecodeContext *ctx)
  1040. {
  1041. int i;
  1042. float (*output)[256] = ctx->audio_block.block_output;
  1043. for (i = 0; i < 256; i++)
  1044. output[2][i] = output[1][i];
  1045. }
  1046. static inline void mix_stereo_to_mono(AC3DecodeContext *ctx)
  1047. {
  1048. int i;
  1049. float (*output)[256] = ctx->audio_block.block_output;
  1050. for (i = 0; i < 256; i++)
  1051. output[1][i] += output[2][i];
  1052. memset(output[2], 0, sizeof(output[2]));
  1053. }
  1054. static inline void mix_3f_to_mono(AC3DecodeContext *ctx)
  1055. {
  1056. int i;
  1057. float (*output)[256] = ctx->audio_block.block_output;
  1058. for (i = 0; i < 256; i++)
  1059. output[1][i] += (output[2][i] + output[3][i]);
  1060. memset(output[2], 0, sizeof(output[2]));
  1061. memset(output[3], 0, sizeof(output[3]));
  1062. }
  1063. static inline void mix_3f_to_stereo(AC3DecodeContext *ctx)
  1064. {
  1065. int i;
  1066. float (*output)[256] = ctx->audio_block.block_output;
  1067. for (i = 0; i < 256; i++) {
  1068. output[1][i] += output[2][i];
  1069. output[2][i] += output[3][i];
  1070. }
  1071. memset(output[3], 0, sizeof(output[3]));
  1072. }
  1073. static inline void mix_2f_1r_to_mono(AC3DecodeContext *ctx)
  1074. {
  1075. int i;
  1076. float (*output)[256] = ctx->audio_block.block_output;
  1077. for (i = 0; i < 256; i++)
  1078. output[1][i] += (output[2][i] + output[3][i]);
  1079. memset(output[2], 0, sizeof(output[2]));
  1080. memset(output[3], 0, sizeof(output[3]));
  1081. }
  1082. static inline void mix_2f_1r_to_stereo(AC3DecodeContext *ctx)
  1083. {
  1084. int i;
  1085. float (*output)[256] = ctx->audio_block.block_output;
  1086. for (i = 0; i < 256; i++) {
  1087. output[1][i] += output[2][i];
  1088. output[2][i] += output[3][i];
  1089. }
  1090. memset(output[3], 0, sizeof(output[3]));
  1091. }
  1092. static inline void mix_2f_1r_to_dolby(AC3DecodeContext *ctx)
  1093. {
  1094. int i;
  1095. float (*output)[256] = ctx->audio_block.block_output;
  1096. for (i = 0; i < 256; i++) {
  1097. output[1][i] -= output[3][i];
  1098. output[2][i] += output[3][i];
  1099. }
  1100. memset(output[3], 0, sizeof(output[3]));
  1101. }
  1102. static inline void mix_3f_1r_to_mono(AC3DecodeContext *ctx)
  1103. {
  1104. int i;
  1105. float (*output)[256] = ctx->audio_block.block_output;
  1106. for (i = 0; i < 256; i++)
  1107. output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
  1108. memset(output[2], 0, sizeof(output[2]));
  1109. memset(output[3], 0, sizeof(output[3]));
  1110. memset(output[4], 0, sizeof(output[4]));
  1111. }
  1112. static inline void mix_3f_1r_to_stereo(AC3DecodeContext *ctx)
  1113. {
  1114. int i;
  1115. float (*output)[256] = ctx->audio_block.block_output;
  1116. for (i = 0; i < 256; i++) {
  1117. output[1][i] += (output[2][i] + output[4][i]);
  1118. output[2][i] += (output[3][i] + output[4][i]);
  1119. }
  1120. memset(output[3], 0, sizeof(output[3]));
  1121. memset(output[4], 0, sizeof(output[4]));
  1122. }
  1123. static inline void mix_3f_1r_to_dolby(AC3DecodeContext *ctx)
  1124. {
  1125. int i;
  1126. float (*output)[256] = ctx->audio_block.block_output;
  1127. for (i = 0; i < 256; i++) {
  1128. output[1][i] += (output[2][i] - output[4][i]);
  1129. output[2][i] += (output[3][i] + output[4][i]);
  1130. }
  1131. memset(output[3], 0, sizeof(output[3]));
  1132. memset(output[4], 0, sizeof(output[4]));
  1133. }
  1134. static inline void mix_2f_2r_to_mono(AC3DecodeContext *ctx)
  1135. {
  1136. int i;
  1137. float (*output)[256] = ctx->audio_block.block_output;
  1138. for (i = 0; i < 256; i++)
  1139. output[1][i] = (output[2][i] + output[3][i] + output[4][i]);
  1140. memset(output[2], 0, sizeof(output[2]));
  1141. memset(output[3], 0, sizeof(output[3]));
  1142. memset(output[4], 0, sizeof(output[4]));
  1143. }
  1144. static inline void mix_2f_2r_to_stereo(AC3DecodeContext *ctx)
  1145. {
  1146. int i;
  1147. float (*output)[256] = ctx->audio_block.block_output;
  1148. for (i = 0; i < 256; i++) {
  1149. output[1][i] += output[3][i];
  1150. output[2][i] += output[4][i];
  1151. }
  1152. memset(output[3], 0, sizeof(output[3]));
  1153. memset(output[4], 0, sizeof(output[4]));
  1154. }
  1155. static inline void mix_2f_2r_to_dolby(AC3DecodeContext *ctx)
  1156. {
  1157. int i;
  1158. float (*output)[256] = ctx->audio_block.block_output;
  1159. for (i = 0; i < 256; i++) {
  1160. output[1][i] -= output[3][i];
  1161. output[2][i] += output[4][i];
  1162. }
  1163. memset(output[3], 0, sizeof(output[3]));
  1164. memset(output[4], 0, sizeof(output[4]));
  1165. }
  1166. static inline void mix_3f_2r_to_mono(AC3DecodeContext *ctx)
  1167. {
  1168. int i;
  1169. float (*output)[256] = ctx->audio_block.block_output;
  1170. for (i = 0; i < 256; i++)
  1171. output[1][i] += (output[2][i] + output[3][i] + output[4][i] + output[5][i]);
  1172. memset(output[2], 0, sizeof(output[2]));
  1173. memset(output[3], 0, sizeof(output[3]));
  1174. memset(output[4], 0, sizeof(output[4]));
  1175. memset(output[5], 0, sizeof(output[5]));
  1176. }
  1177. static inline void mix_3f_2r_to_stereo(AC3DecodeContext *ctx)
  1178. {
  1179. int i;
  1180. float (*output)[256] = ctx->audio_block.block_output;
  1181. for (i = 0; i < 256; i++) {
  1182. output[1][i] += (output[2][i] + output[4][i]);
  1183. output[2][i] += (output[3][i] + output[5][i]);
  1184. }
  1185. memset(output[3], 0, sizeof(output[3]));
  1186. memset(output[4], 0, sizeof(output[4]));
  1187. memset(output[5], 0, sizeof(output[5]));
  1188. }
  1189. static inline void mix_3f_2r_to_dolby(AC3DecodeContext *ctx)
  1190. {
  1191. int i;
  1192. float (*output)[256] = ctx->audio_block.block_output;
  1193. for (i = 0; i < 256; i++) {
  1194. output[1][i] += (output[2][i] - output[4][i] - output[5][i]);
  1195. output[2][i] += (output[3][i] + output[4][i] + output[5][i]);
  1196. }
  1197. memset(output[3], 0, sizeof(output[3]));
  1198. memset(output[4], 0, sizeof(output[4]));
  1199. memset(output[5], 0, sizeof(output[5]));
  1200. }
  1201. static void do_downmix(AC3DecodeContext *ctx)
  1202. {
  1203. int from = ctx->bsi.acmod;
  1204. int to = ctx->output;
  1205. switch (from) {
  1206. case AC3_INPUT_DUALMONO:
  1207. switch (to) {
  1208. case AC3_OUTPUT_MONO:
  1209. mix_dualmono_to_mono(ctx);
  1210. break;
  1211. case AC3_OUTPUT_STEREO: /* We Assume that sum of both mono channels is requested */
  1212. mix_dualmono_to_stereo(ctx);
  1213. break;
  1214. }
  1215. break;
  1216. case AC3_INPUT_MONO:
  1217. switch (to) {
  1218. case AC3_OUTPUT_STEREO:
  1219. upmix_mono_to_stereo(ctx);
  1220. break;
  1221. }
  1222. break;
  1223. case AC3_INPUT_STEREO:
  1224. switch (to) {
  1225. case AC3_OUTPUT_MONO:
  1226. mix_stereo_to_mono(ctx);
  1227. break;
  1228. }
  1229. break;
  1230. case AC3_INPUT_3F:
  1231. switch (to) {
  1232. case AC3_OUTPUT_MONO:
  1233. mix_3f_to_mono(ctx);
  1234. break;
  1235. case AC3_OUTPUT_STEREO:
  1236. mix_3f_to_stereo(ctx);
  1237. break;
  1238. }
  1239. break;
  1240. case AC3_INPUT_2F_1R:
  1241. switch (to) {
  1242. case AC3_OUTPUT_MONO:
  1243. mix_2f_1r_to_mono(ctx);
  1244. break;
  1245. case AC3_OUTPUT_STEREO:
  1246. mix_2f_1r_to_stereo(ctx);
  1247. break;
  1248. case AC3_OUTPUT_DOLBY:
  1249. mix_2f_1r_to_dolby(ctx);
  1250. break;
  1251. }
  1252. break;
  1253. case AC3_INPUT_3F_1R:
  1254. switch (to) {
  1255. case AC3_OUTPUT_MONO:
  1256. mix_3f_1r_to_mono(ctx);
  1257. break;
  1258. case AC3_OUTPUT_STEREO:
  1259. mix_3f_1r_to_stereo(ctx);
  1260. break;
  1261. case AC3_OUTPUT_DOLBY:
  1262. mix_3f_1r_to_dolby(ctx);
  1263. break;
  1264. }
  1265. break;
  1266. case AC3_INPUT_2F_2R:
  1267. switch (to) {
  1268. case AC3_OUTPUT_MONO:
  1269. mix_2f_2r_to_mono(ctx);
  1270. break;
  1271. case AC3_OUTPUT_STEREO:
  1272. mix_2f_2r_to_stereo(ctx);
  1273. break;
  1274. case AC3_OUTPUT_DOLBY:
  1275. mix_2f_2r_to_dolby(ctx);
  1276. break;
  1277. }
  1278. break;
  1279. case AC3_INPUT_3F_2R:
  1280. switch (to) {
  1281. case AC3_OUTPUT_MONO:
  1282. mix_3f_2r_to_mono(ctx);
  1283. break;
  1284. case AC3_OUTPUT_STEREO:
  1285. mix_3f_2r_to_stereo(ctx);
  1286. break;
  1287. case AC3_OUTPUT_DOLBY:
  1288. mix_3f_2r_to_dolby(ctx);
  1289. break;
  1290. }
  1291. break;
  1292. }
  1293. }
  1294. static int ac3_parse_audio_block(AC3DecodeContext * ctx, int index)
  1295. {
  1296. ac3_audio_block *ab = &ctx->audio_block;
  1297. int nfchans = ctx->bsi.nfchans;
  1298. int acmod = ctx->bsi.acmod;
  1299. int i, bnd, rbnd, seg, grpsize;
  1300. GetBitContext *gb = &ctx->gb;
  1301. uint32_t *flags = &ab->flags;
  1302. int bit_alloc_flags = 0;
  1303. float drange;
  1304. uint8_t *dexps;
  1305. *flags = 0;
  1306. ab->blksw = 0;
  1307. for (i = 0; i < 5; i++)
  1308. ab->chcoeffs[i] = 1.0;
  1309. for (i = 0; i < nfchans; i++) /*block switch flag */
  1310. ab->blksw |= get_bits1(gb) << i;
  1311. ab->dithflag = 0;
  1312. for (i = 0; i < nfchans; i++) /* dithering flag */
  1313. ab->dithflag |= get_bits1(gb) << i;
  1314. if (get_bits1(gb)) { /* dynamic range */
  1315. *flags |= AC3_AB_DYNRNGE;
  1316. ab->dynrng = get_bits(gb, 8);
  1317. drange = ((((ab->dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (ab->dynrng >> 5)]);
  1318. for (i = 0; i < nfchans; i++)
  1319. ab->chcoeffs[i] *= drange;
  1320. }
  1321. if (acmod == 0x00) { /* dynamic range 1+1 mode */
  1322. if (get_bits1(gb)) {
  1323. *flags |= AC3_AB_DYNRNG2E;
  1324. ab->dynrng2 = get_bits(gb, 8);
  1325. drange = ((((ab->dynrng2 & 0x1f) | 0x20) << 13) * scale_factors[3 - (ab->dynrng2 >> 5)]);
  1326. ab->chcoeffs[1] *= drange;
  1327. }
  1328. }
  1329. get_downmix_coeffs(ctx);
  1330. ab->chincpl = 0;
  1331. if (get_bits1(gb)) { /* coupling strategy */
  1332. *flags |= AC3_AB_CPLSTRE;
  1333. ab->cplbndstrc = 0;
  1334. if (get_bits1(gb)) { /* coupling in use */
  1335. *flags |= AC3_AB_CPLINU;
  1336. for (i = 0; i < nfchans; i++)
  1337. ab->chincpl |= get_bits1(gb) << i;
  1338. if (acmod == 0x00 || acmod == 0x01)
  1339. return -1; /* coupling needs atleast two shared channels */
  1340. if (acmod == 0x02)
  1341. if (get_bits1(gb)) /* phase flag in use */
  1342. *flags |= AC3_AB_PHSFLGINU;
  1343. ab->cplbegf = get_bits(gb, 4);
  1344. ab->cplendf = get_bits(gb, 4);
  1345. if (3 + ab->cplendf - ab->cplbegf < 0)
  1346. return -1;
  1347. ab->ncplbnd = ab->ncplsubnd = 3 + ab->cplendf - ab->cplbegf;
  1348. ab->cplstrtmant = ab->cplbegf * 12 + 37;
  1349. ab->cplendmant = ((ab->cplendf + 3) * 12) + 37;
  1350. for (i = 0; i < ab->ncplsubnd - 1; i++) /* coupling band structure */
  1351. if (get_bits1(gb)) {
  1352. ab->cplbndstrc |= 1 << i;
  1353. ab->ncplbnd--;
  1354. }
  1355. }
  1356. }
  1357. if (*flags & AC3_AB_CPLINU) {
  1358. ab->cplcoe = 0;
  1359. for (i = 0; i < nfchans; i++)
  1360. if (ab->chincpl & (1 << i))
  1361. if (get_bits1(gb)) { /* coupling co-ordinates */
  1362. ab->cplcoe |= 1 << i;
  1363. ab->mstrcplco[i] = get_bits(gb, 2);
  1364. for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
  1365. ab->cplcoexp[i][bnd] = get_bits(gb, 4);
  1366. ab->cplcomant[i][bnd] = get_bits(gb, 4);
  1367. }
  1368. }
  1369. ab->phsflg = 0;
  1370. if ((acmod == 0x02) && (*flags & AC3_AB_PHSFLGINU) && (ab->cplcoe & 1 || ab->cplcoe & (1 << 1))) {
  1371. for (bnd = 0; bnd < ab->ncplbnd; bnd++)
  1372. if (get_bits1(gb))
  1373. ab->phsflg |= 1 << bnd;
  1374. }
  1375. }
  1376. generate_coupling_coordinates(ctx);
  1377. ab->rematflg = 0;
  1378. if (acmod == 0x02) /* rematrixing */
  1379. if (get_bits1(gb)) {
  1380. *flags |= AC3_AB_REMATSTR;
  1381. if (!(*flags & AC3_AB_CPLINU) || ab->cplbegf > 2)
  1382. for (rbnd = 0; rbnd < 4; rbnd++)
  1383. ab->rematflg |= get_bits1(gb) << rbnd;
  1384. if (ab->cplbegf > 0 && ab->cplbegf <= 2 && (*flags & AC3_AB_CPLINU))
  1385. for (rbnd = 0; rbnd < 3; rbnd++)
  1386. ab->rematflg |= get_bits1(gb) << rbnd;
  1387. if (ab->cplbegf == 0 && (*flags & AC3_AB_CPLINU))
  1388. for (rbnd = 0; rbnd < 2; rbnd++)
  1389. ab->rematflg |= get_bits1(gb) << rbnd;
  1390. }
  1391. ab->cplexpstr = AC3_EXPSTR_REUSE;
  1392. ab->lfeexpstr = AC3_EXPSTR_REUSE;
  1393. if (*flags & AC3_AB_CPLINU) /* coupling exponent strategy */
  1394. ab->cplexpstr = get_bits(gb, 2);
  1395. for (i = 0; i < nfchans; i++) /* channel exponent strategy */
  1396. ab->chexpstr[i] = get_bits(gb, 2);
  1397. if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponent strategy */
  1398. ab->lfeexpstr = get_bits1(gb);
  1399. for (i = 0; i < nfchans; i++) /* channel bandwidth code */
  1400. if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
  1401. if ((ab->chincpl & (1 << i)))
  1402. ab->endmant[i] = ab->cplstrtmant;
  1403. else {
  1404. ab->chbwcod[i] = get_bits(gb, 6);
  1405. if (ab->chbwcod[i] > 60) {
  1406. av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", ab->chbwcod[i]);
  1407. return -1;
  1408. }
  1409. ab->endmant[i] = ((ab->chbwcod[i] + 12) * 3) + 37;
  1410. }
  1411. }
  1412. if (*flags & AC3_AB_CPLINU)
  1413. if (ab->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
  1414. bit_alloc_flags |= 64;
  1415. ab->cplabsexp = get_bits(gb, 4) << 1;
  1416. ab->ncplgrps = (ab->cplendmant - ab->cplstrtmant) / (3 << (ab->cplexpstr - 1));
  1417. if (decode_exponents(gb, ab->cplexpstr, ab->ncplgrps, ab->cplabsexp, ab->dcplexps + ab->cplstrtmant)) {
  1418. av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
  1419. return -1;
  1420. }
  1421. }
  1422. for (i = 0; i < nfchans; i++) /* fbw channel exponents */
  1423. if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
  1424. bit_alloc_flags |= 1 << i;
  1425. grpsize = 3 << (ab->chexpstr[i] - 1);
  1426. ab->nchgrps[i] = (ab->endmant[i] + grpsize - 4) / grpsize;
  1427. dexps = ab->dexps[i];
  1428. dexps[0] = get_bits(gb, 4);
  1429. if (decode_exponents(gb, ab->chexpstr[i], ab->nchgrps[i], dexps[0], dexps + 1)) {
  1430. av_log(NULL, AV_LOG_ERROR, "error decoding channel %d exponents\n", i);
  1431. return -1;
  1432. }
  1433. ab->gainrng[i] = get_bits(gb, 2);
  1434. }
  1435. if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponents */
  1436. if (ab->lfeexpstr != AC3_EXPSTR_REUSE) {
  1437. bit_alloc_flags |= 32;
  1438. ab->dlfeexps[0] = get_bits(gb, 4);
  1439. if (decode_exponents(gb, ab->lfeexpstr, 2, ab->dlfeexps[0], ab->dlfeexps + 1)) {
  1440. av_log(NULL, AV_LOG_ERROR, "error decoding lfe exponents\n");
  1441. return -1;
  1442. }
  1443. }
  1444. if (get_bits1(gb)) { /* bit allocation information */
  1445. *flags |= AC3_AB_BAIE;
  1446. bit_alloc_flags |= 127;
  1447. ab->sdcycod = get_bits(gb, 2);
  1448. ab->fdcycod = get_bits(gb, 2);
  1449. ab->sgaincod = get_bits(gb, 2);
  1450. ab->dbpbcod = get_bits(gb, 2);
  1451. ab->floorcod = get_bits(gb, 3);
  1452. }
  1453. if (get_bits1(gb)) { /* snroffset */
  1454. *flags |= AC3_AB_SNROFFSTE;
  1455. bit_alloc_flags |= 127;
  1456. ab->csnroffst = get_bits(gb, 6);
  1457. if (*flags & AC3_AB_CPLINU) { /* couling fine snr offset and fast gain code */
  1458. ab->cplfsnroffst = get_bits(gb, 4);
  1459. ab->cplfgaincod = get_bits(gb, 3);
  1460. }
  1461. for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
  1462. ab->fsnroffst[i] = get_bits(gb, 4);
  1463. ab->fgaincod[i] = get_bits(gb, 3);
  1464. }
  1465. if (ctx->bsi.flags & AC3_BSI_LFEON) { /* lfe fine snr offset and fast gain code */
  1466. ab->lfefsnroffst = get_bits(gb, 4);
  1467. ab->lfefgaincod = get_bits(gb, 3);
  1468. }
  1469. }
  1470. if (*flags & AC3_AB_CPLINU)
  1471. if (get_bits1(gb)) { /* coupling leak information */
  1472. bit_alloc_flags |= 64;
  1473. *flags |= AC3_AB_CPLLEAKE;
  1474. ab->cplfleak = get_bits(gb, 3);
  1475. ab->cplsleak = get_bits(gb, 3);
  1476. }
  1477. if (get_bits1(gb)) { /* delta bit allocation information */
  1478. *flags |= AC3_AB_DELTBAIE;
  1479. bit_alloc_flags |= 127;
  1480. if (*flags & AC3_AB_CPLINU) {
  1481. ab->cpldeltbae = get_bits(gb, 2);
  1482. if (ab->cpldeltbae == AC3_DBASTR_RESERVED) {
  1483. av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
  1484. return -1;
  1485. }
  1486. }
  1487. for (i = 0; i < nfchans; i++) {
  1488. ab->deltbae[i] = get_bits(gb, 2);
  1489. if (ab->deltbae[i] == AC3_DBASTR_RESERVED) {
  1490. av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
  1491. return -1;
  1492. }
  1493. }
  1494. if (*flags & AC3_AB_CPLINU)
  1495. if (ab->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
  1496. ab->cpldeltnseg = get_bits(gb, 3);
  1497. for (seg = 0; seg <= ab->cpldeltnseg; seg++) {
  1498. ab->cpldeltoffst[seg] = get_bits(gb, 5);
  1499. ab->cpldeltlen[seg] = get_bits(gb, 4);
  1500. ab->cpldeltba[seg] = get_bits(gb, 3);
  1501. }
  1502. }
  1503. for (i = 0; i < nfchans; i++)
  1504. if (ab->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
  1505. ab->deltnseg[i] = get_bits(gb, 3);
  1506. for (seg = 0; seg <= ab->deltnseg[i]; seg++) {
  1507. ab->deltoffst[i][seg] = get_bits(gb, 5);
  1508. ab->deltlen[i][seg] = get_bits(gb, 4);
  1509. ab->deltba[i][seg] = get_bits(gb, 3);
  1510. }
  1511. }
  1512. }
  1513. do_bit_allocation (ctx, bit_alloc_flags); /* perform the bit allocation */
  1514. if (get_bits1(gb)) { /* unused dummy data */
  1515. *flags |= AC3_AB_SKIPLE;
  1516. ab->skipl = get_bits(gb, 9);
  1517. for (i = 0; i < ab->skipl; i++)
  1518. skip_bits(gb, 8);
  1519. }
  1520. /* unpack the transform coefficients
  1521. * * this also uncouples channels if coupling is in use.
  1522. */
  1523. if (get_transform_coeffs(ctx)) {
  1524. av_log(NULL, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");
  1525. return -1;
  1526. }
  1527. /* recover coefficients if rematrixing is in use */
  1528. if (*flags & AC3_AB_REMATSTR)
  1529. do_rematrixing(ctx);
  1530. return 0;
  1531. }
  1532. static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  1533. {
  1534. AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
  1535. ac3_audio_block *ab = &ctx->audio_block;
  1536. int frame_start;
  1537. int i, j, k, l, value;
  1538. float tmp_block_first_half[128], tmp_block_second_half[128];
  1539. int16_t *out_samples = (int16_t *)data;
  1540. int nfchans;
  1541. //Synchronize the frame.
  1542. frame_start = ac3_synchronize(buf, buf_size);
  1543. if (frame_start == -1) {
  1544. av_log(avctx, AV_LOG_ERROR, "frame is not synchronized\n");
  1545. *data_size = 0;
  1546. return buf_size;
  1547. }
  1548. //Initialize the GetBitContext with the start of valid AC3 Frame.
  1549. init_get_bits(&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
  1550. //Parse the syncinfo.
  1551. //If 'fscod' or 'bsid' is not valid the decoder shall mute as per the standard.
  1552. if (!ac3_parse_sync_info(ctx)) {
  1553. av_log(avctx, AV_LOG_ERROR, "\n");
  1554. *data_size = 0;
  1555. return -1;
  1556. }
  1557. //Check for the errors.
  1558. /* if (ac3_error_check(ctx)) {
  1559. *data_size = 0;
  1560. return -1;
  1561. } */
  1562. //Parse the BSI.
  1563. //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
  1564. if (ac3_parse_bsi(ctx)) {
  1565. av_log(avctx, AV_LOG_ERROR, "bsid is not valid\n");
  1566. *data_size = 0;
  1567. return -1;
  1568. }
  1569. for (i = 0; i < MAX_BLOCKS; i++)
  1570. memset(ctx->delay[i], 0, sizeof(ctx->delay[i]));
  1571. avctx->sample_rate = ctx->sync_info.sampling_rate;
  1572. avctx->bit_rate = ctx->sync_info.bit_rate;
  1573. if (avctx->channels == 0) {
  1574. //avctx->channels = ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0);
  1575. ctx->output = AC3_OUTPUT_UNMODIFIED;
  1576. }
  1577. else if ((ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0)) < avctx->channels) {
  1578. av_log(avctx, AV_LOG_INFO, "ac3_decoder: AC3 Source Channels Are Less Then Specified %d: Output to %d Channels\n",
  1579. avctx->channels, (ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0)));
  1580. //avctx->channels = ctx->bsi.nfchans + ((ctx->bsi.flags & AC3_BSI_LFEON) ? 1 : 0);
  1581. ctx->output = AC3_OUTPUT_UNMODIFIED;
  1582. }
  1583. else if (avctx->channels == 1) {
  1584. ctx->output = AC3_OUTPUT_MONO;
  1585. } else if (avctx->channels == 2) {
  1586. if (ctx->bsi.dsurmod == 0x02)
  1587. ctx->output = AC3_OUTPUT_DOLBY;
  1588. else
  1589. ctx->output = AC3_OUTPUT_STEREO;
  1590. }
  1591. av_log(avctx, AV_LOG_INFO, "channels = %d \t bit rate = %d \t sampling rate = %d \n", avctx->channels, avctx->sample_rate, avctx->bit_rate);
  1592. //Parse the Audio Blocks.
  1593. *data_size = 0;
  1594. for (i = 0; i < 6; i++) {
  1595. if (ac3_parse_audio_block(ctx, i)) {
  1596. av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
  1597. *data_size = 0;
  1598. return -1;
  1599. }
  1600. av_log(NULL, AV_LOG_INFO, "doing imdct\n");
  1601. if (ctx->bsi.flags & AC3_BSI_LFEON) {
  1602. ff_imdct_calc(&ctx->imdct_ctx_512, ctx->tmp_output, ab->transform_coeffs[0], ctx->tmp_imdct);
  1603. for (l = 0; l < 256; l++)
  1604. ab->block_output[0][l] = ctx->tmp_output[l] * window[l] + ctx->delay[0][l] * window[255 -l];
  1605. memcpy(ctx->delay[0], ctx->tmp_output + 256, sizeof(ctx->delay[0]));
  1606. }
  1607. for (j = 0; j < ctx->bsi.nfchans; j++) {
  1608. if (ctx->audio_block.blksw & (1 << j)) {
  1609. for (k = 0; k < 128; k++) {
  1610. tmp_block_first_half[k] = ab->transform_coeffs[j + 1][2 * k];
  1611. tmp_block_second_half[k] = ab->transform_coeffs[j + 1][2 * k + 1];
  1612. }
  1613. ff_imdct_calc(&ctx->imdct_ctx_256, ctx->tmp_output, tmp_block_first_half, ctx->tmp_imdct);
  1614. for (l = 0; l < 256; l++)
  1615. ab->block_output[j + 1][l] = ctx->tmp_output[l] * window[l] + ctx->delay[j + 1][l] * window[255 - l];
  1616. ff_imdct_calc(&ctx->imdct_ctx_256, ctx->delay[j + 1], tmp_block_second_half, ctx->tmp_imdct);
  1617. } else {
  1618. ff_imdct_calc(&ctx->imdct_ctx_512, ctx->tmp_output, ab->transform_coeffs[j + 1], ctx->tmp_imdct);
  1619. for (l = 0; l < 256; l++)
  1620. ab->block_output[j + 1][l] = ctx->tmp_output[l] * window[l] + ctx->delay[j + 1][l] * window[255 - l];
  1621. memcpy(ctx->delay[j + 1], ctx->tmp_output + 256, sizeof(ctx->delay[j + 1]));
  1622. }
  1623. }
  1624. if (ctx->bsi.flags & AC3_BSI_LFEON) {
  1625. for (l = 0; l < 256; l++) {
  1626. value = lrint(ab->block_output[0][l]);
  1627. if (value < -32768)
  1628. value = -32768;
  1629. else if (value > 32767)
  1630. value = 32767;
  1631. *(out_samples++) = value;
  1632. }
  1633. *data_size += 256 * sizeof(int16_t);
  1634. }
  1635. do_downmix(ctx);
  1636. if (ctx->output == AC3_OUTPUT_UNMODIFIED)
  1637. nfchans = ctx->bsi.nfchans;
  1638. else
  1639. nfchans = avctx->channels;
  1640. for (k = 0; k < nfchans; k++)
  1641. for (l = 0; l < 256; l++) {
  1642. value = lrint(ab->block_output[k + 1][l]);
  1643. if (value < -32768)
  1644. value = -32768;
  1645. else if (value > 32767)
  1646. value = 32767;
  1647. *(out_samples++) = value;
  1648. }
  1649. *data_size += nfchans * 256 * sizeof (int16_t);
  1650. }
  1651. return ctx->sync_info.framesize;
  1652. }
  1653. static int ac3_decode_end(AVCodecContext *ctx)
  1654. {
  1655. return 0;
  1656. }
  1657. AVCodec lgpl_ac3_decoder = {
  1658. "ac3",
  1659. CODEC_TYPE_AUDIO,
  1660. CODEC_ID_AC3,
  1661. sizeof (AC3DecodeContext),
  1662. ac3_decode_init,
  1663. NULL,
  1664. ac3_decode_end,
  1665. ac3_decode_frame,
  1666. };