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.

489 lines
18KB

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