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.

614 lines
23KB

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