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.

1189 lines
39KB

  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_decoder.h"
  26. #include "avcodec.h"
  27. #include "bitstream.h"
  28. #include "dsputil.h"
  29. #include "avutil.h"
  30. static const int sampling_rates[3] = { 32000, 44100, 48000 };
  31. static const struct
  32. {
  33. int bit_rate;
  34. int frame_sizes[3];
  35. } frame_size_table[38] = {
  36. { 32, { 96, 69, 64 } },
  37. { 32, { 96, 70, 64 } },
  38. { 40, { 120, 87, 80 } },
  39. { 40, { 120, 88, 80 } },
  40. { 48, { 144, 104, 96 } },
  41. { 48, { 144, 105, 96 } },
  42. { 56, { 168, 121, 112 } },
  43. { 56, { 168, 122, 112 } },
  44. { 64, { 192, 139, 128 } },
  45. { 64, { 192, 140, 128 } },
  46. { 80, { 240, 174, 160 } },
  47. { 80, { 240, 175, 160 } },
  48. { 96, { 288, 208, 192 } },
  49. { 96, { 288, 209, 192 } },
  50. { 112, { 336, 243, 224 } },
  51. { 112, { 336, 244, 224 } },
  52. { 128, { 384, 278, 256 } },
  53. { 128, { 384, 279, 256 } },
  54. { 160, { 480, 348, 320 } },
  55. { 160, { 480, 349, 320 } },
  56. { 192, { 576, 417, 384 } },
  57. { 192, { 576, 418, 384 } },
  58. { 224, { 672, 487, 448 } },
  59. { 224, { 672, 488, 448 } },
  60. { 256, { 768, 557, 512 } },
  61. { 256, { 768, 558, 512 } },
  62. { 320, { 960, 696, 640 } },
  63. { 320, { 960, 697, 640 } },
  64. { 384, { 1152, 835, 768 } },
  65. { 384, { 1152, 836, 768 } },
  66. { 448, { 1344, 975, 896 } },
  67. { 448, { 1344, 976, 896 } },
  68. { 512, { 1536, 1114, 1024 } },
  69. { 512, { 1536, 1115, 1024 } },
  70. { 576, { 1728, 1253, 1152 } },
  71. { 576, { 1728, 1254, 1152 } },
  72. { 640, { 1920, 1393, 1280 } }
  73. };
  74. static int
  75. ac3_decode_init (AVCodecContext * avctx)
  76. {
  77. AC3DecodeContext *ctx = avctx->priv_data;
  78. ff_mdct_init (&ctx->mdct_ctx_256, 8, 1);
  79. ff_mdct_init (&ctx->mdct_ctx_512, 9, 1);
  80. ctx->samples = av_mallocz (6 * 6 * 256 * sizeof (float));
  81. if (!(ctx->samples))
  82. return -1;
  83. return 0;
  84. }
  85. static int
  86. ac3_synchronize (uint8_t * buf, int buf_size)
  87. {
  88. int i;
  89. for (i = 0; i < buf_size - 1; i++)
  90. if (buf[i] == 0x0b && buf[i + 1] == 0x77)
  91. return i;
  92. return -1;
  93. }
  94. //Returns -1 when 'fscod' is not valid;
  95. static int
  96. ac3_parse_sync_info (AC3DecodeContext * ctx)
  97. {
  98. ac3_sync_info *sync_info = &ctx->sync_info;
  99. GetBitContext *gb = &ctx->gb;
  100. sync_info->sync_word = get_bits_long (gb, 16);
  101. sync_info->crc1 = get_bits_long (gb, 16);
  102. sync_info->fscod = get_bits_long (gb, 2);
  103. if (sync_info->fscod == 0x03)
  104. return -1;
  105. sync_info->frmsizecod = get_bits_long (gb, 6);
  106. if (sync_info->frmsizecod >= 0x38)
  107. return -1;
  108. sync_info->sampling_rate = sampling_rates[sync_info->fscod];
  109. sync_info->bit_rate = frame_size_table[sync_info->frmsizecod].bit_rate;
  110. sync_info->frame_size = frame_size_table[sync_info->frmsizecod].frame_sizes[sync_info->fscod];
  111. return 0;
  112. }
  113. static const int nfchans_tbl[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
  114. //Returns -1 when
  115. static int
  116. ac3_parse_bsi (AC3DecodeContext * ctx)
  117. {
  118. ac3_bsi *bsi = &ctx->bsi;
  119. uint32_t *flags = &bsi->flags;
  120. GetBitContext *gb = &ctx->gb;
  121. *flags = 0;
  122. bsi->cmixlev = 0;
  123. bsi->surmixlev = 0;
  124. bsi->dsurmod = 0;
  125. bsi->bsid = get_bits_long (gb, 5);
  126. if (bsi->bsid > 0x08)
  127. return -1;
  128. bsi->bsmod = get_bits_long (gb, 3);
  129. bsi->acmod = get_bits_long (gb, 3);
  130. if (bsi->acmod & 0x01 && bsi->acmod != 0x01)
  131. bsi->cmixlev = get_bits_long (gb, 2);
  132. if (bsi->acmod & 0x04)
  133. bsi->surmixlev = get_bits_long (gb, 2);
  134. if (bsi->acmod == 0x02)
  135. bsi->dsurmod = get_bits_long (gb, 2);
  136. if (get_bits_long (gb, 1))
  137. *flags |= AC3_BSI_LFEON;
  138. bsi->dialnorm = get_bits_long (gb, 5);
  139. if (get_bits_long (gb, 1)) {
  140. *flags |= AC3_BSI_COMPRE;
  141. bsi->compr = get_bits_long (gb, 5);
  142. }
  143. if (get_bits_long (gb, 1)) {
  144. *flags |= AC3_BSI_LANGCODE;
  145. bsi->langcod = get_bits_long (gb, 8);
  146. }
  147. if (get_bits_long (gb, 1)) {
  148. *flags |= AC3_BSI_AUDPRODIE;
  149. bsi->mixlevel = get_bits_long (gb, 5);
  150. bsi->roomtyp = get_bits_long (gb, 2);
  151. }
  152. if (bsi->acmod == 0x00) {
  153. bsi->dialnorm2 = get_bits_long (gb, 5);
  154. if (get_bits_long (gb, 1)) {
  155. *flags |= AC3_BSI_COMPR2E;
  156. bsi->compr2 = get_bits_long (gb, 5);
  157. }
  158. if (get_bits_long (gb, 1)) {
  159. *flags |= AC3_BSI_LANGCOD2E;
  160. bsi->langcod2 = get_bits_long (gb, 8);
  161. }
  162. if (get_bits_long (gb, 1)) {
  163. *flags |= AC3_BSI_AUDPRODIE;
  164. bsi->mixlevel2 = get_bits_long (gb, 5);
  165. bsi->roomtyp2 = get_bits_long (gb, 2);
  166. }
  167. }
  168. if (get_bits_long (gb, 1))
  169. *flags |= AC3_BSI_COPYRIGHTB;
  170. if (get_bits_long (gb, 1))
  171. *flags |= AC3_BSI_ORIGBS;
  172. if (get_bits_long (gb, 1)) {
  173. *flags |= AC3_BSI_TIMECOD1E;
  174. bsi->timecod1 = get_bits_long (gb, 14);
  175. }
  176. if (get_bits_long (gb, 1)) {
  177. *flags |= AC3_BSI_TIMECOD2E;
  178. bsi->timecod2 = get_bits_long (gb, 14);
  179. }
  180. if (get_bits_long (gb, 1)) {
  181. *flags |= AC3_BSI_ADDBSIE;
  182. bsi->addbsil = get_bits_long (gb, 6);
  183. do {
  184. get_bits_long (gb, 8);
  185. } while (bsi->addbsil--);
  186. }
  187. bsi->nfchans = nfchans_tbl[bsi->acmod];
  188. return 0;
  189. }
  190. static int bands[16] =
  191. { 31, 35, 37, 39, 41, 42, 43, 44,
  192. 45, 45, 46, 46, 47, 47, 48, 48 };
  193. static const int diff_exps_M1[128] =
  194. { -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
  195. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  196. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  197. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  198. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  199. 25, 25, 25 };
  200. static const int diff_exps_M2[128] =
  201. { -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  202. -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  203. -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  204. -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  205. -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2,
  206. 25, 25, 25 };
  207. static const int diff_exps_M3[128] =
  208. { -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
  209. -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
  210. -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
  211. -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
  212. -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2,
  213. 25, 25, 25 };
  214. /* Decodes the grouped exponents (gexps) and stores them
  215. * in decoded exponents (dexps).
  216. */
  217. static int
  218. _decode_exponents (int expstr, int ngrps, uint8_t absexp, uint8_t * gexps, uint8_t * dexps)
  219. {
  220. int i = 0, exp;
  221. while (ngrps--) {
  222. exp = gexps[i++];
  223. absexp += diff_exps_M1[exp];
  224. if (absexp > 24)
  225. return -1;
  226. if (expstr == AC3_EXPSTR_D45) {
  227. *(dexps++) = absexp;
  228. *(dexps++) = absexp;
  229. }
  230. else if (expstr == AC3_EXPSTR_D25)
  231. *(dexps++) = absexp;
  232. else
  233. *(dexps++) = absexp;
  234. absexp += diff_exps_M2[exp];
  235. if (absexp > 24)
  236. return -1;
  237. if (expstr == AC3_EXPSTR_D45) {
  238. *(dexps++) = absexp;
  239. *(dexps++) = absexp;
  240. }
  241. else if (expstr == AC3_EXPSTR_D25)
  242. *(dexps++) = absexp;
  243. else
  244. *(dexps++) = absexp;
  245. absexp += diff_exps_M3[exp];
  246. if (absexp > 24)
  247. return -1;
  248. if (expstr == AC3_EXPSTR_D45) {
  249. *(dexps++) = absexp;
  250. *(dexps++) = absexp;
  251. }
  252. else if (expstr == AC3_EXPSTR_D25)
  253. *(dexps++) = absexp;
  254. else
  255. *(dexps++) = absexp;
  256. }
  257. return 0;
  258. }
  259. static int
  260. decode_exponents (AC3DecodeContext * ctx)
  261. {
  262. ac3_audio_block *ab = &ctx->audio_block;
  263. int i;
  264. uint8_t *exps;
  265. uint8_t *dexps;
  266. if (ab->flags & AC3_AB_CPLINU && ab->cplexpstr != AC3_EXPSTR_REUSE)
  267. if (_decode_exponents (ab->cplexpstr, ab->ncplgrps, ab->cplabsexp,
  268. ab->cplexps, ab->dcplexps + ab->cplstrtmant))
  269. return -1;
  270. for (i = 0; i < ctx->bsi.nfchans; i++)
  271. if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
  272. exps = ab->exps[i];
  273. dexps = ab->dexps[i];
  274. if (_decode_exponents (ab->chexpstr[i], ab->nchgrps[i], exps[0], exps + 1, dexps + 1))
  275. return -1;
  276. }
  277. if (ctx->bsi.flags & AC3_BSI_LFEON && ab->lfeexpstr != AC3_EXPSTR_REUSE)
  278. if (_decode_exponents (ab->lfeexpstr, 2, ab->lfeexps[0], ab->lfeexps + 1, ab->dlfeexps))
  279. return -1;
  280. return 0;
  281. }
  282. static const int16_t slowdec[4] = { 0x0f, 0x11, 0x13, 0x15 }; /* slow decay table */
  283. static const int16_t fastdec[4] = { 0x3f, 0x53, 0x67, 0x7b }; /* fast decay table */
  284. static const int16_t slowgain[4] = { 0x540, 0x4d8, 0x478, 0x410 }; /* slow gain table */
  285. static const int16_t dbpbtab[4] = { 0x000, 0x700, 0x900, 0xb00 }; /* dB/bit table */
  286. static const int16_t floortab[8] = /* floor table */
  287. { 0x2f0, 0x2b0, 0x270, 0x230,
  288. 0x1f0, 0x170, 0x0f0, 0xf800 };
  289. static const int16_t fastgain[8] = /* fast gain table */
  290. { 0x080, 0x100, 0x180, 0x200,
  291. 0x280, 0x300, 0x380, 0x400 };
  292. static const int16_t bndtab[50] = /* start band table */
  293. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  294. 27, 28, 31, 34, 37, 40, 43, 46, 49, 55, 61, 67, 73, 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
  295. static const int16_t bndsz[50] = /* band size table */
  296. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  297. 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
  298. static const int16_t masktab[256] = /* masking table */
  299. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  300. 25, 26, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34, 34, 35,
  301. 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39,
  302. 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42,
  303. 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44,
  304. 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
  305. 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
  306. 46, 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
  307. 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
  308. 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
  309. 49, 49, 49, 0, 0, 0 };
  310. static const int16_t latab[256] = /* log addition table */
  311. { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039, 0x0038, 0x0037, 0x0036, 0x0035,
  312. 0x0034, 0x0034, 0x0033, 0x0032, 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
  313. 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026, 0x0025, 0x0024, 0x0024, 0x0023,
  314. 0x0023, 0x0022, 0x0021, 0x0021, 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c,
  315. 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018, 0x0018, 0x0017, 0x0017, 0x0016,
  316. 0x0016, 0x0015, 0x0015, 0x0015, 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012,
  317. 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f, 0x000f, 0x000e, 0x000e, 0x000e,
  318. 0x000d, 0x000d, 0x000d, 0x000d, 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b,
  319. 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009, 0x0009, 0x0009, 0x0008, 0x0008,
  320. 0x0008, 0x0008, 0x0008, 0x0008, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006,
  321. 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005,
  322. 0x0005, 0x0005, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
  323. 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
  324. 0x0003, 0x0003, 0x0003, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
  325. 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0001, 0x0001,
  326. 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  327. 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
  328. 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  329. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  330. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  331. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  332. 0x0000, 0x0000, 0x0000, 0x0000 };
  333. static const int16_t hth[3][50] = /* hearing threshold table */
  334. {
  335. {0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0,
  336. 0x03a0, 0x0390, 0x0390, 0x0390, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,
  337. 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310,
  338. 0x0340, 0x0390, 0x03e0, 0x0420, 0x0460, 0x0490, 0x04a0, 0x0440, 0x0440, 0x0400, 0x0520, 0x0800,
  339. 0x0840, 0x0840},
  340. {0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0,
  341. 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360,
  342. 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x02f0, 0x0300,
  343. 0x0320, 0x0350, 0x0390, 0x03e0, 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630,
  344. 0x0840, 0x0840},
  345. {0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0, 0x03c0, 0x03b0, 0x03b0, 0x03b0,
  346. 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390,
  347. 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0,
  348. 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440,
  349. 0x0450, 0x04e0}
  350. };
  351. static const uint8_t baptab[64] = /* bit allocation pointer table */
  352. { 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10,
  353. 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 15,
  354. 15, 15, 15, 15, 15, 15, 15, 15 };
  355. static inline int16_t
  356. logadd (int16_t a, int16_t b)
  357. {
  358. int16_t c = a - b;
  359. uint8_t address = FFMIN ((ABS (c) >> 1), 255);
  360. return ((c >= 0) ? (a + latab[address]) : (b + latab[address]));
  361. }
  362. static inline int16_t
  363. calc_lowcomp (int16_t a, int16_t b0, int16_t b1, uint8_t bin)
  364. {
  365. if (bin < 7) {
  366. if ((b0 + 256) == b1)
  367. a = 384;
  368. else if (b0 > b1)
  369. a = FFMAX (0, a - 64);
  370. }
  371. else if (bin < 20) {
  372. if ((b0 + 256) == b1)
  373. a = 320;
  374. else if (b0 > b1)
  375. a = FFMAX (0, a - 64);
  376. }
  377. else {
  378. a = FFMAX (0, a - 128);
  379. }
  380. return a;
  381. }
  382. /* do the bit allocation for chnl.
  383. * chnl = 0 to 4 - fbw channel
  384. * chnl = 5 coupling channel
  385. * chnl = 6 lfe channel
  386. */
  387. static int
  388. _do_bit_allocation (AC3DecodeContext * ctx, int chnl)
  389. {
  390. ac3_audio_block *ab = &ctx->audio_block;
  391. int16_t sdecay, fdecay, sgain, dbknee, floor;
  392. int16_t lowcomp, fgain, snroffset, fastleak, slowleak;
  393. int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
  394. uint8_t start, end, bin, i, j, k, lastbin, bndstrt, bndend, begin, deltnseg, band, seg, address;
  395. uint8_t fscod = ctx->sync_info.fscod;
  396. uint8_t *exps, *deltoffst, *deltlen, *deltba;
  397. uint8_t *baps;
  398. int do_delta = 0;
  399. /* initialization */
  400. sdecay = slowdec[ab->sdcycod];
  401. fdecay = fastdec[ab->fdcycod];
  402. sgain = slowgain[ab->sgaincod];
  403. dbknee = dbpbtab[ab->dbpbcod];
  404. floor = dbpbtab[ab->floorcod];
  405. if (chnl == 5) {
  406. start = ab->cplstrtmant;
  407. end = ab->cplendmant;
  408. fgain = fastgain[ab->cplfgaincod];
  409. snroffset = (((ab->csnroffst - 15) << 4) + ab->cplfsnroffst) << 2;
  410. fastleak = (ab->cplfleak << 8) + 768;
  411. slowleak = (ab->cplsleak << 8) + 768;
  412. exps = ab->dcplexps;
  413. baps = ab->cplbap;
  414. if (ab->cpldeltbae == 0 || ab->cpldeltbae == 1) {
  415. do_delta = 1;
  416. deltnseg = ab->cpldeltnseg;
  417. deltoffst = ab->cpldeltoffst;
  418. deltlen = ab->cpldeltlen;
  419. deltba = ab->cpldeltba;
  420. }
  421. }
  422. else if (chnl == 6) {
  423. start = 0;
  424. end = 7;
  425. lowcomp = 0;
  426. fgain = fastgain[ab->lfefgaincod];
  427. snroffset = (((ab->csnroffst - 15) << 4) + ab->lfefsnroffst) << 2;
  428. exps = ab->dlfeexps;
  429. baps = ab->lfebap;
  430. }
  431. else {
  432. start = 0;
  433. end = ab->endmant[chnl];
  434. lowcomp = 0;
  435. fgain = fastgain[ab->fgaincod[chnl]];
  436. snroffset = (((ab->csnroffst - 15) << 4) + ab->fsnroffst[chnl]) << 2;
  437. exps = ab->dexps[chnl];
  438. baps = ab->bap[chnl];
  439. if (ab->deltbae[chnl] == 0 || ab->deltbae[chnl] == 1) {
  440. do_delta = 1;
  441. deltnseg = ab->deltnseg[chnl];
  442. deltoffst = ab->deltoffst[chnl];
  443. deltlen = ab->deltlen[chnl];
  444. deltba = ab->deltba[chnl];
  445. }
  446. }
  447. for (bin = start; bin < end; bin++) /* exponent mapping into psd */
  448. psd[bin] = (3072 - ((int16_t) (exps[bin] << 7)));
  449. /* psd integration */
  450. j = start;
  451. k = masktab[start];
  452. do {
  453. lastbin = FFMIN (bndtab[k] + bndsz[k], end);
  454. bndpsd[k] = psd[j];
  455. j++;
  456. for (i = j; i < lastbin; i++) {
  457. bndpsd[k] = logadd (bndpsd[k], psd[j]);
  458. j++;
  459. }
  460. k++;
  461. } while (end > lastbin);
  462. /* compute the excite function */
  463. bndstrt = masktab[start];
  464. bndend = masktab[end - 1] + 1;
  465. if (bndstrt == 0) {
  466. lowcomp = calc_lowcomp (lowcomp, bndpsd[0], bndpsd[1], 0);
  467. excite[0] = bndpsd[0] - fgain - lowcomp;
  468. lowcomp = calc_lowcomp (lowcomp, bndpsd[1], bndpsd[2], 1);
  469. excite[1] = bndpsd[1] - fgain - lowcomp;
  470. begin = 7;
  471. for (bin = 2; bin < 7; bin++) {
  472. if (bndend != 7 || bin != 6)
  473. lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
  474. fastleak = bndpsd[bin] - fgain;
  475. slowleak = bndpsd[bin] - sgain;
  476. excite[bin] = fastleak - lowcomp;
  477. if (bndend != 7 || bin != 6)
  478. if (bndpsd[bin] <= bndpsd[bin + 1]) {
  479. begin = bin + 1;
  480. break;
  481. }
  482. }
  483. for (bin = begin; bin < (FFMIN (bndend, 22)); bin++) {
  484. if (bndend != 7 || bin != 6)
  485. lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
  486. fastleak -= fdecay;
  487. fastleak = FFMAX (fastleak, bndpsd[bin] - fgain);
  488. slowleak -= sdecay;
  489. slowleak = FFMAX (slowleak, bndpsd[bin] - sgain);
  490. excite[bin] = FFMAX (fastleak - lowcomp, slowleak);
  491. }
  492. begin = 22;
  493. }
  494. else {
  495. begin = bndstrt;
  496. }
  497. for (bin = begin; bin < bndend; bin++) {
  498. fastleak -= fdecay;
  499. fastleak = FFMAX (fastleak, bndpsd[bin] - fgain);
  500. slowleak -= sdecay;
  501. slowleak = FFMAX (slowleak, bndpsd[bin] - sgain);
  502. excite[bin] = FFMAX (fastleak, slowleak);
  503. }
  504. /* compute the masking curve */
  505. for (bin = bndstrt; bin < bndend; bin++) {
  506. if (bndpsd[bin] < dbknee)
  507. excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
  508. mask[bin] = FFMAX (excite[bin], hth[fscod][bin]);
  509. }
  510. /* apply the delta bit allocation */
  511. if (do_delta) {
  512. band = 0;
  513. for (seg = 0; seg < deltnseg + 1; seg++) {
  514. band += deltoffst[seg];
  515. if (deltba[seg] >= 4)
  516. delta = (deltba[seg] - 3) << 7;
  517. else
  518. delta = (deltba[seg] - 4) << 7;
  519. for (k = 0; k < deltlen[seg]; k++) {
  520. mask[band] += delta;
  521. band++;
  522. }
  523. }
  524. }
  525. /*compute the bit allocation */
  526. i = start;
  527. j = masktab[start];
  528. do {
  529. lastbin = FFMIN (bndtab[j] + bndsz[j], end);
  530. mask[j] -= snroffset;
  531. mask[j] -= floor;
  532. if (mask[j] < 0)
  533. mask[j] = 0;
  534. mask[j] &= 0x1fe0;
  535. mask[j] += floor;
  536. for (k = i; k < lastbin; k++) {
  537. address = (psd[i] - mask[j]) >> 5;
  538. address = FFMIN (63, (FFMAX (0, address)));
  539. baps[i] = baptab[address];
  540. i++;
  541. }
  542. j++;
  543. } while (end > lastbin);
  544. return 0;
  545. }
  546. static int
  547. do_bit_allocation (AC3DecodeContext * ctx, int flags)
  548. {
  549. ac3_audio_block *ab = &ctx->audio_block;
  550. int i, snroffst = 0;
  551. if (!flags) /* bit allocation is not required */
  552. return 0;
  553. if (ab->flags & AC3_AB_SNROFFSTE) { /* check whether snroffsts are zero */
  554. snroffst += ab->csnroffst;
  555. if (ab->flags & AC3_AB_CPLINU)
  556. snroffst += ab->cplfsnroffst;
  557. for (i = 0; i < ctx->bsi.nfchans; i++)
  558. snroffst += ab->fsnroffst[i];
  559. if (ctx->bsi.flags & AC3_BSI_LFEON)
  560. snroffst += ab->lfefsnroffst;
  561. if (!snroffst) {
  562. memset (ab->cplbap, 0, sizeof (ab->cplbap));
  563. for (i = 0; i < ctx->bsi.nfchans; i++)
  564. memset (ab->bap[i], 0, sizeof (ab->bap[i]));
  565. memset (ab->lfebap, 0, sizeof (ab->lfebap));
  566. return 0;
  567. }
  568. }
  569. /* perform bit allocation */
  570. if ((ab->flags & AC3_AB_CPLINU) && (flags & 64))
  571. if (_do_bit_allocation (ctx, 5))
  572. return -1;
  573. for (i = 0; i < ctx->bsi.nfchans; i++)
  574. if (flags & (1 << i))
  575. if (_do_bit_allocation (ctx, i))
  576. return -1;
  577. if ((ctx->bsi.flags & AC3_BSI_LFEON) && (flags & 32))
  578. if (_do_bit_allocation (ctx, 6))
  579. return -1;
  580. return 0;
  581. }
  582. /* table for exponent to scale_factor mapping
  583. * scale_factor[i] = 2 ^ -(i + 15)
  584. */
  585. static const float scale_factors[25] = {
  586. 0.000030517578125000000000000000000000000,
  587. 0.000015258789062500000000000000000000000,
  588. 0.000007629394531250000000000000000000000,
  589. 0.000003814697265625000000000000000000000,
  590. 0.000001907348632812500000000000000000000,
  591. 0.000000953674316406250000000000000000000,
  592. 0.000000476837158203125000000000000000000,
  593. 0.000000238418579101562500000000000000000,
  594. 0.000000119209289550781250000000000000000,
  595. 0.000000059604644775390625000000000000000,
  596. 0.000000029802322387695312500000000000000,
  597. 0.000000014901161193847656250000000000000,
  598. 0.000000007450580596923828125000000000000,
  599. 0.000000003725290298461914062500000000000,
  600. 0.000000001862645149230957031250000000000,
  601. 0.000000000931322574615478515625000000000,
  602. 0.000000000465661287307739257812500000000,
  603. 0.000000000232830643653869628906250000000,
  604. 0.000000000116415321826934814453125000000,
  605. 0.000000000058207660913467407226562500000,
  606. 0.000000000029103830456733703613281250000,
  607. 0.000000000014551915228366851806640625000,
  608. 0.000000000007275957614183425903320312500,
  609. 0.000000000003637978807091712951660156250,
  610. 0.000000000001818989403545856475830078125
  611. };
  612. static const int16_t l3_q_tab[3] = { /* 3-level quantization table */
  613. (-2 << 15) / 3, 0, (2 << 15) / 3
  614. };
  615. static const int16_t l5_q_tab[5] = { /* 5-level quantization table */
  616. (-4 << 15) / 5, (-2 << 15) / 5, 0, (2 << 15) / 5, (4 << 15) / 5
  617. };
  618. static const int16_t l7_q_tab[7] = { /* 7-level quantization table */
  619. (-6 << 15) / 7, (-4 << 15) / 7, (-2 << 15) / 7, 0,
  620. (2 << 15) / 7, (4 << 15) / 7, (6 << 15) / 7
  621. };
  622. static const int16_t l11_q_tab[11] = { /* 11-level quantization table */
  623. (-10 << 15) / 11, (-8 << 15) / 11, (-6 << 15) / 11, (-4 << 15) / 11, (-2 << 15) / 11, 0,
  624. (2 << 15) / 11, (4 << 15) / 11, (6 << 15) / 11, (8 << 15) / 11, (10 << 15) / 11
  625. };
  626. static const int16_t l15_q_tab[15] = { /* 15-level quantization table */
  627. (-14 << 15) / 15, (-12 << 15) / 15, (-10 << 15) / 15, (-8 << 15) / 15,
  628. (-6 << 15) / 15, (-4 << 15) / 15, (-2 << 15) / 15, 0,
  629. (2 << 15) / 15, (4 << 15) / 15, (6 << 15) / 15, (8 << 15) / 15,
  630. (10 << 15) / 15, (12 << 15) / 15, (14 << 15) / 15
  631. };
  632. static const uint8_t qntztab[16] = { 0, 5, 7, 3, 7, 4, 5, 6, 7, 8, 9, 10, 12, 12, 14, 16 };
  633. static inline float
  634. to_float (uint8_t exp, int16_t mantissa)
  635. {
  636. return ((float) (mantissa * scale_factors[exp]));
  637. }
  638. typedef struct
  639. { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
  640. uint8_t gcodes[3];
  641. uint8_t gcptr;
  642. } mant_group;
  643. /* Get the transform coefficients for particular channel */
  644. static int
  645. _get_transform_coeffs (uint8_t * exps, uint8_t * bap, float *samples,
  646. int start, int end, int dith_flag, GetBitContext * gb)
  647. {
  648. int16_t mantissa;
  649. int i;
  650. int gcode;
  651. mant_group l3_grp, l5_grp, l11_grp;
  652. for (i = 0; i < 3; i++)
  653. l3_grp.gcodes[i] = l5_grp.gcodes[i] = l11_grp.gcodes[i] = -1;
  654. l3_grp.gcptr = l5_grp.gcptr = 3;
  655. l11_grp.gcptr = 2;
  656. i = 0;
  657. while (i < start)
  658. samples[i++] = 0;
  659. for (i = start; i < end; i++) {
  660. switch (bap[i]) {
  661. case 0:
  662. if (!dith_flag)
  663. mantissa = 0;
  664. else
  665. mantissa = gen_dither ();
  666. samples[i] = to_float (exps[i], mantissa);
  667. break;
  668. case 1:
  669. if (l3_grp.gcptr > 2) {
  670. gcode = get_bits_long (gb, qntztab[1]);
  671. if (gcode > 26)
  672. return -1;
  673. l3_grp.gcodes[0] = gcode / 9;
  674. l3_grp.gcodes[1] = (gcode % 9) / 3;
  675. l3_grp.gcodes[2] = (gcode % 9) % 3;
  676. l3_grp.gcptr = 0;
  677. }
  678. mantissa = l3_q_tab[l3_grp.gcodes[l3_grp.gcptr++]];
  679. samples[i] = to_float (exps[i], mantissa);
  680. break;
  681. case 2:
  682. if (l5_grp.gcptr > 2) {
  683. gcode = get_bits_long (gb, qntztab[2]);
  684. if (gcode > 124)
  685. return -1;
  686. l5_grp.gcodes[0] = gcode / 25;
  687. l5_grp.gcodes[1] = (gcode % 25) / 5;
  688. l5_grp.gcodes[2] = (gcode % 25) % 5;
  689. l5_grp.gcptr = 0;
  690. }
  691. mantissa = l5_q_tab[l5_grp.gcodes[l5_grp.gcptr++]];
  692. samples[i] = to_float (exps[i], mantissa);
  693. break;
  694. case 3:
  695. mantissa = get_bits_long (gb, qntztab[3]);
  696. if (mantissa > 6)
  697. return -1;
  698. mantissa = l7_q_tab[mantissa];
  699. samples[i] = to_float (exps[i], mantissa);
  700. break;
  701. case 4:
  702. if (l11_grp.gcptr > 1) {
  703. gcode = get_bits_long (gb, qntztab[4]);
  704. if (gcode > 120)
  705. return -1;
  706. l11_grp.gcodes[0] = gcode / 11;
  707. l11_grp.gcodes[1] = gcode % 11;
  708. }
  709. mantissa = l11_q_tab[l11_grp.gcodes[l11_grp.gcptr++]];
  710. samples[i] = to_float (exps[i], mantissa);
  711. break;
  712. case 5:
  713. mantissa = get_bits_long (gb, qntztab[5]);
  714. if (mantissa > 14)
  715. return -1;
  716. mantissa = l15_q_tab[mantissa];
  717. break;
  718. default:
  719. mantissa = get_bits_long (gb, qntztab[bap[i]]) << (16 - qntztab[bap[i]]);
  720. samples[i] = to_float (exps[i], mantissa);
  721. break;
  722. }
  723. }
  724. i = end;
  725. while (i < 256)
  726. samples[i++] = 0;
  727. return 0;
  728. }
  729. static int
  730. uncouple_channels (AC3DecodeContext * ctx)
  731. {
  732. ac3_audio_block *ab = &ctx->audio_block;
  733. int ch, sbnd, bin;
  734. int index;
  735. float (*samples)[256];
  736. int16_t mantissa;
  737. samples = (float (*)[256]) (ab->ab_samples);
  738. samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0;
  739. /* uncouple channels */
  740. for (ch = 0; ch < ctx->bsi.nfchans; ch++)
  741. if (ab->chincpl & (1 << ch))
  742. for (sbnd = ab->cplbegf; sbnd < 3 + ab->cplendf; sbnd++)
  743. for (bin = 0; bin < 12; bin++) {
  744. index = sbnd * 12 + bin + 37;
  745. samples[ch][index] = ab->cplcoeffs[index] * ab->cplco[ch][sbnd] * 8;
  746. }
  747. /* generate dither if required */
  748. for (ch = 0; ch < ctx->bsi.nfchans; ch++)
  749. if ((ab->chincpl & (1 << ch)) && (ab->dithflag & (1 << ch)))
  750. for (index = 0; index < ab->endmant[ch]; index++)
  751. if (!ab->bap[ch][index]) {
  752. mantissa = gen_dither ();
  753. samples[ch][index] = to_float (ab->dexps[ch][index], mantissa);
  754. }
  755. return 0;
  756. }
  757. static int
  758. get_transform_coeffs (AC3DecodeContext * ctx)
  759. {
  760. int i;
  761. ac3_audio_block *ab = &ctx->audio_block;
  762. float *samples = ab->ab_samples;
  763. int got_cplchan = 0;
  764. int dithflag = 0;
  765. samples += (ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0;
  766. for (i = 0; i < ctx->bsi.nfchans; i++) {
  767. if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)))
  768. dithflag = 0; /* don't generate dither until channels are decoupled */
  769. else
  770. dithflag = ab->dithflag & (1 << i);
  771. /* transform coefficients for individual channel */
  772. if (_get_transform_coeffs (ab->dexps[i], ab->bap[i], samples + (i * 256),
  773. 0, ab->endmant[i], dithflag, &ctx->gb))
  774. return -1;
  775. /* tranform coefficients for coupling channels */
  776. if ((ab->flags & AC3_AB_CPLINU) && (ab->chincpl & (1 << i)) && !got_cplchan) {
  777. if (_get_transform_coeffs (ab->dcplexps, ab->cplbap, ab->cplcoeffs,
  778. ab->cplstrtmant, ab->cplendmant, 0, &ctx->gb))
  779. return -1;
  780. got_cplchan = 1;
  781. }
  782. }
  783. /* uncouple the channels from the coupling channel */
  784. if (ab->flags & AC3_AB_CPLINU)
  785. if (uncouple_channels (ctx))
  786. return -1;
  787. return 0;
  788. }
  789. /* generate coupling co-ordinates for each coupling subband
  790. * from coupling co-ordinates of each band and coupling band
  791. * structure information
  792. */
  793. static int
  794. generate_coupling_coordinates (AC3DecodeContext * ctx)
  795. {
  796. ac3_audio_block *ab = &ctx->audio_block;
  797. uint8_t exp, mstrcplco;
  798. int16_t mant;
  799. uint32_t cplbndstrc = (1 << ab->ncplsubnd) >> 1;
  800. int ch, bnd, sbnd;
  801. float cplco;
  802. if (ab->cplcoe)
  803. for (ch = 0; ch < ctx->bsi.nfchans; ch++)
  804. if (ab->cplcoe & (1 << ch)) {
  805. mstrcplco = 3 * ab->mstrcplco[ch];
  806. sbnd = ab->cplbegf;
  807. for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
  808. exp = ab->cplcoexp[ch][bnd];
  809. if (exp == 15)
  810. mant = ab->cplcomant[ch][bnd] <<= 14;
  811. else
  812. mant = (ab->cplcomant[ch][bnd] | 0x10) << 13;
  813. cplco = to_float (exp + mstrcplco, mant);
  814. if (ctx->bsi.acmod == 0x02 && (ab->flags & AC3_AB_PHSFLGINU) && ch == 1
  815. && (ab->phsflg & (1 << bnd)))
  816. cplco = -cplco; /* invert the right channel */
  817. ab->cplco[ch][sbnd++] = cplco;
  818. while (cplbndstrc & ab->cplbndstrc) {
  819. cplbndstrc >>= 1;
  820. ab->cplco[ch][sbnd++] = cplco;
  821. }
  822. cplbndstrc >>= 1;
  823. }
  824. }
  825. return 0;
  826. }
  827. static int
  828. ac3_parse_audio_block (AC3DecodeContext * ctx, int index)
  829. {
  830. ac3_audio_block *ab = &ctx->audio_block;
  831. int nfchans = ctx->bsi.nfchans;
  832. int acmod = ctx->bsi.acmod;
  833. int i, bnd, rbnd, grp, seg;
  834. GetBitContext *gb = &ctx->gb;
  835. uint32_t *flags = &ab->flags;
  836. int bit_alloc_flags = 0;
  837. *flags = 0;
  838. ab->blksw = 0;
  839. for (i = 0; i < nfchans; i++) /*block switch flag */
  840. ab->blksw |= get_bits_long (gb, 1) << i;
  841. ab->dithflag = 0;
  842. for (i = 0; i < nfchans; i++) /* dithering flag */
  843. ab->dithflag |= get_bits_long (gb, 1) << i;
  844. if (get_bits_long (gb, 1)) { /* dynamic range */
  845. *flags |= AC3_AB_DYNRNGE;
  846. ab->dynrng = get_bits_long (gb, 8);
  847. }
  848. if (acmod == 0x00) { /* dynamic range 1+1 mode */
  849. if (get_bits_long (gb, 1)) {
  850. *flags |= AC3_AB_DYNRNG2E;
  851. ab->dynrng2 = get_bits_long (gb, 8);
  852. }
  853. }
  854. ab->chincpl = 0;
  855. if (get_bits_long (gb, 1)) { /* coupling strategy */
  856. *flags |= AC3_AB_CPLSTRE;
  857. ab->cplbndstrc = 0;
  858. if (get_bits_long (gb, 1)) { /* coupling in use */
  859. *flags |= AC3_AB_CPLINU;
  860. for (i = 0; i < nfchans; i++)
  861. ab->chincpl |= get_bits_long (gb, 1) << i;
  862. if (acmod == 0x02)
  863. if (get_bits_long (gb, 1)) /* phase flag in use */
  864. *flags |= AC3_AB_PHSFLGINU;
  865. ab->cplbegf = get_bits_long (gb, 4);
  866. ab->cplendf = get_bits_long (gb, 4);
  867. if ((ab->ncplsubnd = 3 + ab->cplendf - ab->cplbegf) < 0)
  868. return -1;
  869. ab->ncplbnd = ab->ncplsubnd;
  870. for (i = 0; i < ab->ncplsubnd - 1; i++) /* coupling band structure */
  871. if (get_bits_long (gb, 1)) {
  872. ab->cplbndstrc |= 1 << i;
  873. ab->ncplbnd--;
  874. }
  875. }
  876. }
  877. if (*flags & AC3_AB_CPLINU) {
  878. ab->cplcoe = 0;
  879. for (i = 0; i < nfchans; i++)
  880. if (ab->chincpl & (1 << i))
  881. if (get_bits_long (gb, 1)) { /* coupling co-ordinates */
  882. ab->cplcoe |= 1 << i;
  883. ab->mstrcplco[i] = get_bits_long (gb, 2);
  884. for (bnd = 0; bnd < ab->ncplbnd; bnd++) {
  885. ab->cplcoexp[i][bnd] = get_bits_long (gb, 4);
  886. ab->cplcomant[i][bnd] = get_bits_long (gb, 4);
  887. }
  888. }
  889. }
  890. ab->phsflg = 0;
  891. if ((acmod == 0x02) && (*flags & AC3_AB_PHSFLGINU) && (ab->cplcoe & 1 || ab->cplcoe & (1 << 1))) {
  892. for (bnd = 0; bnd < ab->ncplbnd; bnd++)
  893. if (get_bits_long (gb, 1))
  894. ab->phsflg |= 1 << bnd;
  895. }
  896. generate_coupling_coordinates (ctx);
  897. ab->rematflg = 0;
  898. if (acmod == 0x02) /* rematrixing */
  899. if (get_bits_long (gb, 1)) {
  900. *flags |= AC3_AB_REMATSTR;
  901. if (ab->cplbegf > 2 || !(*flags & AC3_AB_CPLINU))
  902. for (rbnd = 0; rbnd < 4; rbnd++)
  903. ab->rematflg |= get_bits_long (gb, 1) << bnd;
  904. else if (ab->cplbegf > 0 && ab->cplbegf <= 2 && *flags & AC3_AB_CPLINU)
  905. for (rbnd = 0; rbnd < 3; rbnd++)
  906. ab->rematflg |= get_bits_long (gb, 1) << bnd;
  907. else if (!(ab->cplbegf) && *flags & AC3_AB_CPLINU)
  908. for (rbnd = 0; rbnd < 2; rbnd++)
  909. ab->rematflg |= get_bits_long (gb, 1) << bnd;
  910. }
  911. if (*flags & AC3_AB_CPLINU) /* coupling exponent strategy */
  912. ab->cplexpstr = get_bits_long (gb, 2);
  913. for (i = 0; i < nfchans; i++) /* channel exponent strategy */
  914. ab->chexpstr[i] = get_bits_long (gb, 2);
  915. if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponent strategy */
  916. ab->lfeexpstr = get_bits_long (gb, 1);
  917. for (i = 0; i < nfchans; i++) /* channel bandwidth code */
  918. if (ab->chexpstr[i] != AC3_EXPSTR_REUSE)
  919. if (!(ab->chincpl & (1 << i))) {
  920. ab->chbwcod[i] = get_bits_long (gb, 6);
  921. if (ab->chbwcod[i] > 60)
  922. return -1;
  923. }
  924. if (*flags & AC3_AB_CPLINU)
  925. if (ab->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
  926. bit_alloc_flags |= 64;
  927. ab->cplabsexp = get_bits_long (gb, 4) << 1;
  928. ab->cplstrtmant = (ab->cplbegf * 12) + 37;
  929. ab->cplendmant = ((ab->cplendmant + 3) * 12) + 37;
  930. ab->ncplgrps = (ab->cplendmant - ab->cplstrtmant) / (3 << (ab->cplexpstr - 1));
  931. for (grp = 0; grp < ab->ncplgrps; grp++)
  932. ab->cplexps[grp] = get_bits_long (gb, 7);
  933. }
  934. for (i = 0; i < nfchans; i++) /* fbw channel exponents */
  935. if (ab->chexpstr[i] != AC3_EXPSTR_REUSE) {
  936. bit_alloc_flags |= 1 << i;
  937. if (ab->chincpl & (1 << i))
  938. ab->endmant[i] = (ab->cplbegf * 12) + 37;
  939. else
  940. ab->endmant[i] = ((ab->chbwcod[i] + 3) * 12) + 37;
  941. ab->nchgrps[i] =
  942. (ab->endmant[i] + (3 << (ab->chexpstr[i] - 1)) - 4) / (3 << (ab->chexpstr[i] - 1));
  943. ab->exps[i][0] = ab->dexps[i][0] = get_bits_long (gb, 4);
  944. for (grp = 1; grp <= ab->nchgrps[i]; grp++)
  945. ab->exps[i][grp] = get_bits_long (gb, 7);
  946. ab->gainrng[i] = get_bits_long (gb, 2);
  947. }
  948. if (ctx->bsi.flags & AC3_BSI_LFEON) /* lfe exponents */
  949. if (ab->lfeexpstr != AC3_EXPSTR_REUSE) {
  950. bit_alloc_flags |= 32;
  951. ab->lfeexps[0] = ab->dlfeexps[0] = get_bits_long (gb, 4);
  952. ab->lfeexps[1] = get_bits_long (gb, 7);
  953. ab->lfeexps[2] = get_bits_long (gb, 7);
  954. }
  955. if (decode_exponents (ctx)) /* decode the exponents for this block */
  956. return -1;
  957. if (get_bits_long (gb, 1)) { /* bit allocation information */
  958. *flags |= AC3_AB_BAIE;
  959. bit_alloc_flags |= 127;
  960. ab->sdcycod = get_bits_long (gb, 2);
  961. ab->fdcycod = get_bits_long (gb, 2);
  962. ab->sgaincod = get_bits_long (gb, 2);
  963. ab->dbpbcod = get_bits_long (gb, 2);
  964. ab->floorcod = get_bits_long (gb, 3);
  965. }
  966. if (get_bits_long (gb, 1)) { /* snroffset */
  967. *flags |= AC3_AB_SNROFFSTE;
  968. bit_alloc_flags |= 127;
  969. ab->csnroffst = get_bits_long (gb, 6);
  970. if (*flags & AC3_AB_CPLINU) { /* couling fine snr offset and fast gain code */
  971. ab->cplfsnroffst = get_bits_long (gb, 4);
  972. ab->cplfgaincod = get_bits_long (gb, 3);
  973. }
  974. for (i = 0; i < nfchans; i++) { /* channel fine snr offset and fast gain code */
  975. ab->fsnroffst[i] = get_bits_long (gb, 4);
  976. ab->fgaincod[i] = get_bits_long (gb, 3);
  977. }
  978. if (ctx->bsi.flags & AC3_BSI_LFEON) { /* lfe fine snr offset and fast gain code */
  979. ab->lfefsnroffst = get_bits_long (gb, 4);
  980. ab->lfefgaincod = get_bits_long (gb, 3);
  981. }
  982. }
  983. if (*flags & AC3_AB_CPLINU)
  984. if (get_bits_long (gb, 1)) { /* coupling leak information */
  985. bit_alloc_flags |= 64;
  986. *flags |= AC3_AB_CPLLEAKE;
  987. ab->cplfleak = get_bits_long (gb, 3);
  988. ab->cplsleak = get_bits_long (gb, 3);
  989. }
  990. if (get_bits_long (gb, 1)) { /* delta bit allocation information */
  991. *flags |= AC3_AB_DELTBAIE;
  992. bit_alloc_flags |= 127;
  993. if (*flags & AC3_AB_CPLINU) {
  994. ab->cpldeltbae = get_bits_long (gb, 2);
  995. if (ab->cpldeltbae == AC3_DBASTR_RESERVED)
  996. return -1;
  997. }
  998. for (i = 0; i < nfchans; i++) {
  999. ab->deltbae[i] = get_bits_long (gb, 2);
  1000. if (ab->deltbae[i] == AC3_DBASTR_RESERVED)
  1001. return -1;
  1002. }
  1003. if (*flags & AC3_AB_CPLINU)
  1004. if (ab->cpldeltbae == AC3_DBASTR_NEW) { /*coupling delta offset, len and bit allocation */
  1005. ab->cpldeltnseg = get_bits_long (gb, 3);
  1006. for (seg = 0; seg <= ab->cpldeltnseg; seg++) {
  1007. ab->cpldeltoffst[seg] = get_bits_long (gb, 5);
  1008. ab->cpldeltlen[seg] = get_bits_long (gb, 4);
  1009. ab->cpldeltba[seg] = get_bits_long (gb, 3);
  1010. }
  1011. }
  1012. for (i = 0; i < nfchans; i++)
  1013. if (ab->deltbae[i] == AC3_DBASTR_NEW) {/*channel delta offset, len and bit allocation */
  1014. ab->deltnseg[i] = get_bits_long (gb, 3);
  1015. for (seg = 0; seg <= ab->deltnseg[i]; seg++) {
  1016. ab->deltoffst[i][seg] = get_bits_long (gb, 5);
  1017. ab->deltlen[i][seg] = get_bits_long (gb, 4);
  1018. ab->deltba[i][seg] = get_bits_long (gb, 3);
  1019. }
  1020. }
  1021. }
  1022. if (do_bit_allocation (ctx, bit_alloc_flags)) /* perform the bit allocation */
  1023. return -1;
  1024. if (get_bits_long (gb, 1)) { /* unused dummy data */
  1025. *flags |= AC3_AB_SKIPLE;
  1026. ab->skipl = get_bits_long (gb, 9);
  1027. while (ab->skipl) {
  1028. get_bits_long (gb, 8);
  1029. ab->skipl--;
  1030. }
  1031. }
  1032. /* point ab_samples to the right place within smaples */
  1033. if (!index)
  1034. ab->ab_samples = ctx->samples;
  1035. else {
  1036. ab->ab_samples = ctx->samples + (i * nfchans * 256);
  1037. ab->ab_samples += ((ctx->bsi.flags & AC3_BSI_LFEON) ? 256 : 0);
  1038. }
  1039. /* unpack the transform coefficients
  1040. * this also uncouples channels if coupling is in use.
  1041. */
  1042. if (get_transform_coeffs (ctx))
  1043. return -1;
  1044. return 0;
  1045. }
  1046. static int
  1047. ac3_decode_frame (AVCodecContext * avctx, void *data, int *data_size, uint8_t * buf, int buf_size)
  1048. {
  1049. AC3DecodeContext *ctx = avctx->priv_data;
  1050. int frame_start;
  1051. int i;
  1052. //Synchronize the frame.
  1053. frame_start = ac3_synchronize (buf, buf_size);
  1054. if (frame_start == -1) {
  1055. *data_size = 0;
  1056. return -1;
  1057. }
  1058. //Initialize the GetBitContext with the start of valid AC3 Frame.
  1059. init_get_bits (&(ctx->gb), buf + frame_start, (buf_size - frame_start) * 8);
  1060. //Parse the syncinfo.
  1061. //If 'fscod' is not valid the decoder shall mute as per the standard.
  1062. if (ac3_parse_sync_info (ctx)) {
  1063. *data_size = 0;
  1064. return -1;
  1065. }
  1066. //Check for the errors.
  1067. /*if (ac3_error_check(ctx))
  1068. {
  1069. *data_size = 0;
  1070. return -1;
  1071. } */
  1072. //Parse the BSI.
  1073. //If 'bsid' is not valid decoder shall not decode the audio as per the standard.
  1074. if (ac3_parse_bsi (ctx)) {
  1075. *data_size = 0;
  1076. return -1;
  1077. }
  1078. //Parse the Audio Blocks.
  1079. for (i = 0; i < 6; i++)
  1080. if (ac3_parse_audio_block (ctx, i)) {
  1081. *data_size = 0;
  1082. return -1;
  1083. }
  1084. return 0;
  1085. }