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.

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