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.

520 lines
19KB

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