eac3dec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * E-AC-3 decoder
  3. * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
  4. * Copyright (c) 2008 Justin Ruggles
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avcodec.h"
  23. #include "internal.h"
  24. #include "aac_ac3_parser.h"
  25. #include "ac3.h"
  26. #include "ac3_parser.h"
  27. #include "ac3dec.h"
  28. #include "ac3dec_data.h"
  29. /** gain adaptive quantization mode */
  30. typedef enum {
  31. EAC3_GAQ_NO =0,
  32. EAC3_GAQ_12,
  33. EAC3_GAQ_14,
  34. EAC3_GAQ_124
  35. } EAC3GaqMode;
  36. #define EAC3_SR_CODE_REDUCED 3
  37. /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
  38. #define COEFF_0 10273905LL
  39. /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
  40. #define COEFF_1 11863283LL
  41. /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
  42. #define COEFF_2 3070444LL
  43. /**
  44. * Calculate 6-point IDCT of the pre-mantissas.
  45. * All calculations are 24-bit fixed-point.
  46. */
  47. static void idct6(int pre_mant[6])
  48. {
  49. int tmp;
  50. int even0, even1, even2, odd0, odd1, odd2;
  51. odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
  52. even2 = ( pre_mant[2] * COEFF_0) >> 23;
  53. tmp = ( pre_mant[4] * COEFF_1) >> 23;
  54. odd0 = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
  55. even0 = pre_mant[0] + (tmp >> 1);
  56. even1 = pre_mant[0] - tmp;
  57. tmp = even0;
  58. even0 = tmp + even2;
  59. even2 = tmp - even2;
  60. tmp = odd0;
  61. odd0 = tmp + pre_mant[1] + pre_mant[3];
  62. odd2 = tmp + pre_mant[5] - pre_mant[3];
  63. pre_mant[0] = even0 + odd0;
  64. pre_mant[1] = even1 + odd1;
  65. pre_mant[2] = even2 + odd2;
  66. pre_mant[3] = even2 - odd2;
  67. pre_mant[4] = even1 - odd1;
  68. pre_mant[5] = even0 - odd0;
  69. }
  70. void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
  71. {
  72. int bin, blk, gs;
  73. int end_bap, gaq_mode;
  74. GetBitContext *gbc = &s->gbc;
  75. int gaq_gain[AC3_MAX_COEFS];
  76. gaq_mode = get_bits(gbc, 2);
  77. end_bap = (gaq_mode < 2) ? 12 : 17;
  78. /* if GAQ gain is used, decode gain codes for bins with hebap between
  79. 8 and end_bap */
  80. gs = 0;
  81. if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
  82. /* read 1-bit GAQ gain codes */
  83. for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
  84. if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
  85. gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
  86. }
  87. } else if (gaq_mode == EAC3_GAQ_124) {
  88. /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
  89. int gc = 2;
  90. for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
  91. if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
  92. if (gc++ == 2) {
  93. int group_code = get_bits(gbc, 5);
  94. if (group_code > 26) {
  95. av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
  96. group_code = 26;
  97. }
  98. gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
  99. gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
  100. gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
  101. gc = 0;
  102. }
  103. }
  104. }
  105. }
  106. gs=0;
  107. for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
  108. int hebap = s->bap[ch][bin];
  109. int bits = ff_eac3_bits_vs_hebap[hebap];
  110. if (!hebap) {
  111. /* zero-mantissa dithering */
  112. for (blk = 0; blk < 6; blk++) {
  113. s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
  114. }
  115. } else if (hebap < 8) {
  116. /* Vector Quantization */
  117. int v = get_bits(gbc, bits);
  118. for (blk = 0; blk < 6; blk++) {
  119. s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] << 8;
  120. }
  121. } else {
  122. /* Gain Adaptive Quantization */
  123. int gbits, log_gain;
  124. if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
  125. log_gain = gaq_gain[gs++];
  126. } else {
  127. log_gain = 0;
  128. }
  129. gbits = bits - log_gain;
  130. for (blk = 0; blk < 6; blk++) {
  131. int mant = get_sbits(gbc, gbits);
  132. if (mant == -(1 << (gbits-1))) {
  133. /* large mantissa */
  134. int b;
  135. mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
  136. /* remap mantissa value to correct for asymmetric quantization */
  137. if (mant >= 0)
  138. b = 32768 >> (log_gain+8);
  139. else
  140. b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1];
  141. mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7;
  142. } else {
  143. /* small mantissa, no GAQ, or Gk=1 */
  144. mant <<= 24 - bits;
  145. if (!log_gain) {
  146. /* remap mantissa value for no GAQ or Gk=1 */
  147. mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7;
  148. }
  149. }
  150. s->pre_mantissa[ch][bin][blk] = mant;
  151. }
  152. }
  153. idct6(s->pre_mantissa[ch][bin]);
  154. }
  155. }
  156. int ff_eac3_parse_header(AC3DecodeContext *s)
  157. {
  158. int i, blk, ch;
  159. int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
  160. int parse_transient_proc_info;
  161. int num_cpl_blocks;
  162. GetBitContext *gbc = &s->gbc;
  163. /* An E-AC-3 stream can have multiple independent streams which the
  164. application can select from. each independent stream can also contain
  165. dependent streams which are used to add or replace channels. */
  166. if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
  167. ff_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
  168. return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
  169. } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
  170. av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
  171. return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
  172. }
  173. /* The substream id indicates which substream this frame belongs to. each
  174. independent stream has its own substream id, and the dependent streams
  175. associated to an independent stream have matching substream id's. */
  176. if (s->substreamid) {
  177. /* only decode substream with id=0. skip any additional substreams. */
  178. ff_log_missing_feature(s->avctx, "Additional substreams", 1);
  179. return AAC_AC3_PARSE_ERROR_FRAME_TYPE;
  180. }
  181. if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
  182. /* The E-AC-3 specification does not tell how to handle reduced sample
  183. rates in bit allocation. The best assumption would be that it is
  184. handled like AC-3 DolbyNet, but we cannot be sure until we have a
  185. sample which utilizes this feature. */
  186. ff_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
  187. return -1;
  188. }
  189. skip_bits(gbc, 5); // skip bitstream id
  190. /* volume control params */
  191. for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
  192. skip_bits(gbc, 5); // skip dialog normalization
  193. if (get_bits1(gbc)) {
  194. skip_bits(gbc, 8); // skip compression gain word
  195. }
  196. }
  197. /* dependent stream channel map */
  198. if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
  199. if (get_bits1(gbc)) {
  200. skip_bits(gbc, 16); // skip custom channel map
  201. }
  202. }
  203. /* mixing metadata */
  204. if (get_bits1(gbc)) {
  205. /* center and surround mix levels */
  206. if (s->channel_mode > AC3_CHMODE_STEREO) {
  207. skip_bits(gbc, 2); // skip preferred stereo downmix mode
  208. if (s->channel_mode & 1) {
  209. /* if three front channels exist */
  210. skip_bits(gbc, 3); //skip Lt/Rt center mix level
  211. s->center_mix_level = get_bits(gbc, 3);
  212. }
  213. if (s->channel_mode & 4) {
  214. /* if a surround channel exists */
  215. skip_bits(gbc, 3); //skip Lt/Rt surround mix level
  216. s->surround_mix_level = get_bits(gbc, 3);
  217. }
  218. }
  219. /* lfe mix level */
  220. if (s->lfe_on && get_bits1(gbc)) {
  221. // TODO: use LFE mix level
  222. skip_bits(gbc, 5); // skip LFE mix level code
  223. }
  224. /* info for mixing with other streams and substreams */
  225. if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
  226. for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
  227. // TODO: apply program scale factor
  228. if (get_bits1(gbc)) {
  229. skip_bits(gbc, 6); // skip program scale factor
  230. }
  231. }
  232. if (get_bits1(gbc)) {
  233. skip_bits(gbc, 6); // skip external program scale factor
  234. }
  235. /* skip mixing parameter data */
  236. switch(get_bits(gbc, 2)) {
  237. case 1: skip_bits(gbc, 5); break;
  238. case 2: skip_bits(gbc, 12); break;
  239. case 3: {
  240. int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
  241. skip_bits_long(gbc, mix_data_size);
  242. break;
  243. }
  244. }
  245. /* skip pan information for mono or dual mono source */
  246. if (s->channel_mode < AC3_CHMODE_STEREO) {
  247. for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
  248. if (get_bits1(gbc)) {
  249. /* note: this is not in the ATSC A/52B specification
  250. reference: ETSI TS 102 366 V1.1.1
  251. section: E.1.3.1.25 */
  252. skip_bits(gbc, 8); // skip pan mean direction index
  253. skip_bits(gbc, 6); // skip reserved paninfo bits
  254. }
  255. }
  256. }
  257. /* skip mixing configuration information */
  258. if (get_bits1(gbc)) {
  259. for (blk = 0; blk < s->num_blocks; blk++) {
  260. if (s->num_blocks == 1 || get_bits1(gbc)) {
  261. skip_bits(gbc, 5);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. /* informational metadata */
  268. if (get_bits1(gbc)) {
  269. skip_bits(gbc, 3); // skip bit stream mode
  270. skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
  271. if (s->channel_mode == AC3_CHMODE_STEREO) {
  272. skip_bits(gbc, 4); // skip Dolby surround and headphone mode
  273. }
  274. if (s->channel_mode >= AC3_CHMODE_2F2R) {
  275. skip_bits(gbc, 2); // skip Dolby surround EX mode
  276. }
  277. for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
  278. if (get_bits1(gbc)) {
  279. skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
  280. }
  281. }
  282. if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
  283. skip_bits1(gbc); // skip source sample rate code
  284. }
  285. }
  286. /* converter synchronization flag
  287. If frames are less than six blocks, this bit should be turned on
  288. once every 6 blocks to indicate the start of a frame set.
  289. reference: RFC 4598, Section 2.1.3 Frame Sets */
  290. if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
  291. skip_bits1(gbc); // skip converter synchronization flag
  292. }
  293. /* original frame size code if this stream was converted from AC-3 */
  294. if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
  295. (s->num_blocks == 6 || get_bits1(gbc))) {
  296. skip_bits(gbc, 6); // skip frame size code
  297. }
  298. /* additional bitstream info */
  299. if (get_bits1(gbc)) {
  300. int addbsil = get_bits(gbc, 6);
  301. for (i = 0; i < addbsil + 1; i++) {
  302. skip_bits(gbc, 8); // skip additional bit stream info
  303. }
  304. }
  305. /* audio frame syntax flags, strategy data, and per-frame data */
  306. if (s->num_blocks == 6) {
  307. ac3_exponent_strategy = get_bits1(gbc);
  308. parse_aht_info = get_bits1(gbc);
  309. } else {
  310. /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
  311. do not use AHT */
  312. ac3_exponent_strategy = 1;
  313. parse_aht_info = 0;
  314. }
  315. s->snr_offset_strategy = get_bits(gbc, 2);
  316. parse_transient_proc_info = get_bits1(gbc);
  317. s->block_switch_syntax = get_bits1(gbc);
  318. if (!s->block_switch_syntax)
  319. memset(s->block_switch, 0, sizeof(s->block_switch));
  320. s->dither_flag_syntax = get_bits1(gbc);
  321. if (!s->dither_flag_syntax) {
  322. for (ch = 1; ch <= s->fbw_channels; ch++)
  323. s->dither_flag[ch] = 1;
  324. }
  325. s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
  326. s->bit_allocation_syntax = get_bits1(gbc);
  327. if (!s->bit_allocation_syntax) {
  328. /* set default bit allocation parameters */
  329. s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
  330. s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
  331. s->bit_alloc_params.slow_gain = ff_ac3_slow_gain_tab [1];
  332. s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
  333. s->bit_alloc_params.floor = ff_ac3_floor_tab [7];
  334. }
  335. s->fast_gain_syntax = get_bits1(gbc);
  336. s->dba_syntax = get_bits1(gbc);
  337. s->skip_syntax = get_bits1(gbc);
  338. parse_spx_atten_data = get_bits1(gbc);
  339. /* coupling strategy occurance and coupling use per block */
  340. num_cpl_blocks = 0;
  341. if (s->channel_mode > 1) {
  342. for (blk = 0; blk < s->num_blocks; blk++) {
  343. s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
  344. if (s->cpl_strategy_exists[blk]) {
  345. s->cpl_in_use[blk] = get_bits1(gbc);
  346. } else {
  347. s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
  348. }
  349. num_cpl_blocks += s->cpl_in_use[blk];
  350. }
  351. } else {
  352. memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
  353. }
  354. /* exponent strategy data */
  355. if (ac3_exponent_strategy) {
  356. /* AC-3-style exponent strategy syntax */
  357. for (blk = 0; blk < s->num_blocks; blk++) {
  358. for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
  359. s->exp_strategy[blk][ch] = get_bits(gbc, 2);
  360. }
  361. }
  362. } else {
  363. /* LUT-based exponent strategy syntax */
  364. for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
  365. int frmchexpstr = get_bits(gbc, 5);
  366. for (blk = 0; blk < 6; blk++) {
  367. s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
  368. }
  369. }
  370. }
  371. /* LFE exponent strategy */
  372. if (s->lfe_on) {
  373. for (blk = 0; blk < s->num_blocks; blk++) {
  374. s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
  375. }
  376. }
  377. /* original exponent strategies if this stream was converted from AC-3 */
  378. if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
  379. (s->num_blocks == 6 || get_bits1(gbc))) {
  380. skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
  381. }
  382. /* determine which channels use AHT */
  383. if (parse_aht_info) {
  384. /* For AHT to be used, all non-zero blocks must reuse exponents from
  385. the first block. Furthermore, for AHT to be used in the coupling
  386. channel, all blocks must use coupling and use the same coupling
  387. strategy. */
  388. s->channel_uses_aht[CPL_CH]=0;
  389. for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
  390. int use_aht = 1;
  391. for (blk = 1; blk < 6; blk++) {
  392. if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
  393. (!ch && s->cpl_strategy_exists[blk])) {
  394. use_aht = 0;
  395. break;
  396. }
  397. }
  398. s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
  399. }
  400. } else {
  401. memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
  402. }
  403. /* per-frame SNR offset */
  404. if (!s->snr_offset_strategy) {
  405. int csnroffst = (get_bits(gbc, 6) - 15) << 4;
  406. int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
  407. for (ch = 0; ch <= s->channels; ch++)
  408. s->snr_offset[ch] = snroffst;
  409. }
  410. /* transient pre-noise processing data */
  411. if (parse_transient_proc_info) {
  412. for (ch = 1; ch <= s->fbw_channels; ch++) {
  413. if (get_bits1(gbc)) { // channel in transient processing
  414. skip_bits(gbc, 10); // skip transient processing location
  415. skip_bits(gbc, 8); // skip transient processing length
  416. }
  417. }
  418. }
  419. /* spectral extension attenuation data */
  420. if (parse_spx_atten_data) {
  421. ff_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
  422. for (ch = 1; ch <= s->fbw_channels; ch++) {
  423. if (get_bits1(gbc)) { // channel has spx attenuation
  424. skip_bits(gbc, 5); // skip spx attenuation code
  425. }
  426. }
  427. }
  428. /* block start information */
  429. if (s->num_blocks > 1 && get_bits1(gbc)) {
  430. /* reference: Section E2.3.2.27
  431. nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
  432. The spec does not say what this data is or what it's used for.
  433. It is likely the offset of each block within the frame. */
  434. int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
  435. skip_bits_long(gbc, block_start_bits);
  436. ff_log_missing_feature(s->avctx, "Block start info", 1);
  437. }
  438. /* syntax state initialization */
  439. for (ch = 1; ch <= s->fbw_channels; ch++) {
  440. s->first_cpl_coords[ch] = 1;
  441. }
  442. s->first_cpl_leak = 1;
  443. return 0;
  444. }