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.

1717 lines
65KB

  1. /*
  2. * ATRAC3+ compatible decoder
  3. *
  4. * Copyright (c) 2010-2013 Maxim Poliakovski
  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. * @file
  24. * Bitstream parser for ATRAC3+ decoder.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "atrac3plus.h"
  30. #include "atrac3plus_data.h"
  31. static VLC_TYPE tables_data[154276][2];
  32. static VLC wl_vlc_tabs[4];
  33. static VLC sf_vlc_tabs[8];
  34. static VLC ct_vlc_tabs[4];
  35. static VLC spec_vlc_tabs[112];
  36. static VLC gain_vlc_tabs[11];
  37. static VLC tone_vlc_tabs[7];
  38. /**
  39. * Generate canonical VLC table from given descriptor.
  40. *
  41. * @param[in] cb ptr to codebook descriptor
  42. * @param[in,out] xlat ptr to ptr to translation table
  43. * @param[in,out] tab_offset starting offset to the generated vlc table
  44. * @param[out] out_vlc ptr to vlc table to be generated
  45. */
  46. static av_cold void build_canonical_huff(const uint8_t *cb, const uint8_t **xlat,
  47. int *tab_offset, VLC *out_vlc)
  48. {
  49. int i, max_len;
  50. uint8_t bits[256];
  51. int index = 0;
  52. for (int b = 1; b <= 12; b++) {
  53. for (i = *cb++; i > 0; i--) {
  54. av_assert0(index < 256);
  55. bits[index] = b;
  56. index++;
  57. }
  58. }
  59. max_len = bits[index - 1];
  60. out_vlc->table = &tables_data[*tab_offset];
  61. out_vlc->table_allocated = 1 << max_len;
  62. ff_init_vlc_from_lengths(out_vlc, max_len, index, bits, 1,
  63. *xlat, 1, 1, 0, INIT_VLC_USE_NEW_STATIC, NULL);
  64. *tab_offset += 1 << max_len;
  65. *xlat += index;
  66. }
  67. av_cold void ff_atrac3p_init_vlcs(void)
  68. {
  69. int i, tab_offset = 0;
  70. const uint8_t *xlats;
  71. xlats = atrac3p_wl_ct_xlats;
  72. for (int i = 0; i < 4; i++) {
  73. build_canonical_huff(atrac3p_wl_cbs[i], &xlats,
  74. &tab_offset, &wl_vlc_tabs[i]);
  75. build_canonical_huff(atrac3p_ct_cbs[i], &xlats,
  76. &tab_offset, &ct_vlc_tabs[i]);
  77. }
  78. xlats = atrac3p_sf_xlats;
  79. for (int i = 0; i < 8; i++)
  80. build_canonical_huff(atrac3p_sf_cbs[i], &xlats,
  81. &tab_offset, &sf_vlc_tabs[i]);
  82. /* build huffman tables for spectrum decoding */
  83. xlats = atrac3p_spectra_xlats;
  84. for (i = 0; i < 112; i++) {
  85. if (atrac3p_spectra_cbs[i][0] >= 0)
  86. build_canonical_huff(atrac3p_spectra_cbs[i],
  87. &xlats, &tab_offset, &spec_vlc_tabs[i]);
  88. else /* Reuse already initialized VLC table */
  89. spec_vlc_tabs[i] = spec_vlc_tabs[-atrac3p_spectra_cbs[i][0]];
  90. }
  91. /* build huffman tables for gain data decoding */
  92. xlats = atrac3p_gain_xlats;
  93. for (i = 0; i < 11; i++)
  94. build_canonical_huff(atrac3p_gain_cbs[i], &xlats,
  95. &tab_offset, &gain_vlc_tabs[i]);
  96. /* build huffman tables for tone decoding */
  97. xlats = atrac3p_tone_xlats;
  98. for (i = 0; i < 7; i++)
  99. build_canonical_huff(atrac3p_tone_cbs[i], &xlats,
  100. &tab_offset, &tone_vlc_tabs[i]);
  101. }
  102. /**
  103. * Decode number of coded quantization units.
  104. *
  105. * @param[in] gb the GetBit context
  106. * @param[in,out] chan ptr to the channel parameters
  107. * @param[in,out] ctx ptr to the channel unit context
  108. * @param[in] avctx ptr to the AVCodecContext
  109. * @return result code: 0 = OK, otherwise - error code
  110. */
  111. static int num_coded_units(GetBitContext *gb, Atrac3pChanParams *chan,
  112. Atrac3pChanUnitCtx *ctx, AVCodecContext *avctx)
  113. {
  114. chan->fill_mode = get_bits(gb, 2);
  115. if (!chan->fill_mode) {
  116. chan->num_coded_vals = ctx->num_quant_units;
  117. } else {
  118. chan->num_coded_vals = get_bits(gb, 5);
  119. if (chan->num_coded_vals > ctx->num_quant_units) {
  120. av_log(avctx, AV_LOG_ERROR,
  121. "Invalid number of transmitted units!\n");
  122. return AVERROR_INVALIDDATA;
  123. }
  124. if (chan->fill_mode == 3)
  125. chan->split_point = get_bits(gb, 2) + (chan->ch_num << 1) + 1;
  126. }
  127. return 0;
  128. }
  129. /**
  130. * Add weighting coefficients to the decoded word-length information.
  131. *
  132. * @param[in,out] ctx ptr to the channel unit context
  133. * @param[in,out] chan ptr to the channel parameters
  134. * @param[in] wtab_idx index of the table of weights
  135. * @param[in] avctx ptr to the AVCodecContext
  136. * @return result code: 0 = OK, otherwise - error code
  137. */
  138. static int add_wordlen_weights(Atrac3pChanUnitCtx *ctx,
  139. Atrac3pChanParams *chan, int wtab_idx,
  140. AVCodecContext *avctx)
  141. {
  142. int i;
  143. const int8_t *weights_tab =
  144. &atrac3p_wl_weights[chan->ch_num * 3 + wtab_idx - 1][0];
  145. for (i = 0; i < ctx->num_quant_units; i++) {
  146. chan->qu_wordlen[i] += weights_tab[i];
  147. if (chan->qu_wordlen[i] < 0 || chan->qu_wordlen[i] > 7) {
  148. av_log(avctx, AV_LOG_ERROR,
  149. "WL index out of range: pos=%d, val=%d!\n",
  150. i, chan->qu_wordlen[i]);
  151. return AVERROR_INVALIDDATA;
  152. }
  153. }
  154. return 0;
  155. }
  156. /**
  157. * Subtract weighting coefficients from decoded scalefactors.
  158. *
  159. * @param[in,out] ctx ptr to the channel unit context
  160. * @param[in,out] chan ptr to the channel parameters
  161. * @param[in] wtab_idx index of table of weights
  162. * @param[in] avctx ptr to the AVCodecContext
  163. * @return result code: 0 = OK, otherwise - error code
  164. */
  165. static int subtract_sf_weights(Atrac3pChanUnitCtx *ctx,
  166. Atrac3pChanParams *chan, int wtab_idx,
  167. AVCodecContext *avctx)
  168. {
  169. int i;
  170. const int8_t *weights_tab = &atrac3p_sf_weights[wtab_idx - 1][0];
  171. for (i = 0; i < ctx->used_quant_units; i++) {
  172. chan->qu_sf_idx[i] -= weights_tab[i];
  173. if (chan->qu_sf_idx[i] < 0 || chan->qu_sf_idx[i] > 63) {
  174. av_log(avctx, AV_LOG_ERROR,
  175. "SF index out of range: pos=%d, val=%d!\n",
  176. i, chan->qu_sf_idx[i]);
  177. return AVERROR_INVALIDDATA;
  178. }
  179. }
  180. return 0;
  181. }
  182. /**
  183. * Unpack vector quantization tables.
  184. *
  185. * @param[in] start_val start value for the unpacked table
  186. * @param[in] shape_vec ptr to table to unpack
  187. * @param[out] dst ptr to output array
  188. * @param[in] num_values number of values to unpack
  189. */
  190. static inline void unpack_vq_shape(int start_val, const int8_t *shape_vec,
  191. int *dst, int num_values)
  192. {
  193. int i;
  194. if (num_values) {
  195. dst[0] = dst[1] = dst[2] = start_val;
  196. for (i = 3; i < num_values; i++)
  197. dst[i] = start_val - shape_vec[atrac3p_qu_num_to_seg[i] - 1];
  198. }
  199. }
  200. #define UNPACK_SF_VQ_SHAPE(gb, dst, num_vals) \
  201. start_val = get_bits((gb), 6); \
  202. unpack_vq_shape(start_val, &atrac3p_sf_shapes[get_bits((gb), 6)][0], \
  203. (dst), (num_vals))
  204. /**
  205. * Decode word length for each quantization unit of a channel.
  206. *
  207. * @param[in] gb the GetBit context
  208. * @param[in,out] ctx ptr to the channel unit context
  209. * @param[in] ch_num channel to process
  210. * @param[in] avctx ptr to the AVCodecContext
  211. * @return result code: 0 = OK, otherwise - error code
  212. */
  213. static int decode_channel_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  214. int ch_num, AVCodecContext *avctx)
  215. {
  216. int i, weight_idx = 0, delta, diff, pos, delta_bits, min_val, flag,
  217. ret, start_val;
  218. VLC *vlc_tab;
  219. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  220. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  221. chan->fill_mode = 0;
  222. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  223. case 0: /* coded using constant number of bits */
  224. for (i = 0; i < ctx->num_quant_units; i++)
  225. chan->qu_wordlen[i] = get_bits(gb, 3);
  226. break;
  227. case 1:
  228. if (ch_num) {
  229. if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
  230. return ret;
  231. if (chan->num_coded_vals) {
  232. vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
  233. for (i = 0; i < chan->num_coded_vals; i++) {
  234. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  235. chan->qu_wordlen[i] = (ref_chan->qu_wordlen[i] + delta) & 7;
  236. }
  237. }
  238. } else {
  239. weight_idx = get_bits(gb, 2);
  240. if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
  241. return ret;
  242. if (chan->num_coded_vals) {
  243. pos = get_bits(gb, 5);
  244. if (pos > chan->num_coded_vals) {
  245. av_log(avctx, AV_LOG_ERROR,
  246. "WL mode 1: invalid position!\n");
  247. return AVERROR_INVALIDDATA;
  248. }
  249. delta_bits = get_bits(gb, 2);
  250. min_val = get_bits(gb, 3);
  251. for (i = 0; i < pos; i++)
  252. chan->qu_wordlen[i] = get_bits(gb, 3);
  253. for (i = pos; i < chan->num_coded_vals; i++)
  254. chan->qu_wordlen[i] = (min_val + get_bitsz(gb, delta_bits)) & 7;
  255. }
  256. }
  257. break;
  258. case 2:
  259. if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
  260. return ret;
  261. if (ch_num && chan->num_coded_vals) {
  262. vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
  263. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  264. chan->qu_wordlen[0] = (ref_chan->qu_wordlen[0] + delta) & 7;
  265. for (i = 1; i < chan->num_coded_vals; i++) {
  266. diff = ref_chan->qu_wordlen[i] - ref_chan->qu_wordlen[i - 1];
  267. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  268. chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + diff + delta) & 7;
  269. }
  270. } else if (chan->num_coded_vals) {
  271. flag = get_bits(gb, 1);
  272. vlc_tab = &wl_vlc_tabs[get_bits(gb, 1)];
  273. start_val = get_bits(gb, 3);
  274. unpack_vq_shape(start_val,
  275. &atrac3p_wl_shapes[start_val][get_bits(gb, 4)][0],
  276. chan->qu_wordlen, chan->num_coded_vals);
  277. if (!flag) {
  278. for (i = 0; i < chan->num_coded_vals; i++) {
  279. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  280. chan->qu_wordlen[i] = (chan->qu_wordlen[i] + delta) & 7;
  281. }
  282. } else {
  283. for (i = 0; i < (chan->num_coded_vals & - 2); i += 2)
  284. if (!get_bits1(gb)) {
  285. chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
  286. get_vlc2(gb, vlc_tab->table,
  287. vlc_tab->bits, 1)) & 7;
  288. chan->qu_wordlen[i + 1] = (chan->qu_wordlen[i + 1] +
  289. get_vlc2(gb, vlc_tab->table,
  290. vlc_tab->bits, 1)) & 7;
  291. }
  292. if (chan->num_coded_vals & 1)
  293. chan->qu_wordlen[i] = (chan->qu_wordlen[i] +
  294. get_vlc2(gb, vlc_tab->table,
  295. vlc_tab->bits, 1)) & 7;
  296. }
  297. }
  298. break;
  299. case 3:
  300. weight_idx = get_bits(gb, 2);
  301. if ((ret = num_coded_units(gb, chan, ctx, avctx)) < 0)
  302. return ret;
  303. if (chan->num_coded_vals) {
  304. vlc_tab = &wl_vlc_tabs[get_bits(gb, 2)];
  305. /* first coefficient is coded directly */
  306. chan->qu_wordlen[0] = get_bits(gb, 3);
  307. for (i = 1; i < chan->num_coded_vals; i++) {
  308. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  309. chan->qu_wordlen[i] = (chan->qu_wordlen[i - 1] + delta) & 7;
  310. }
  311. }
  312. break;
  313. }
  314. if (chan->fill_mode == 2) {
  315. for (i = chan->num_coded_vals; i < ctx->num_quant_units; i++)
  316. chan->qu_wordlen[i] = ch_num ? get_bits1(gb) : 1;
  317. } else if (chan->fill_mode == 3) {
  318. pos = ch_num ? chan->num_coded_vals + chan->split_point
  319. : ctx->num_quant_units - chan->split_point;
  320. if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {
  321. av_log(avctx, AV_LOG_ERROR, "Split point beyond array\n");
  322. pos = FF_ARRAY_ELEMS(chan->qu_wordlen);
  323. }
  324. for (i = chan->num_coded_vals; i < pos; i++)
  325. chan->qu_wordlen[i] = 1;
  326. }
  327. if (weight_idx)
  328. return add_wordlen_weights(ctx, chan, weight_idx, avctx);
  329. return 0;
  330. }
  331. /**
  332. * Decode scale factor indexes for each quant unit of a channel.
  333. *
  334. * @param[in] gb the GetBit context
  335. * @param[in,out] ctx ptr to the channel unit context
  336. * @param[in] ch_num channel to process
  337. * @param[in] avctx ptr to the AVCodecContext
  338. * @return result code: 0 = OK, otherwise - error code
  339. */
  340. static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  341. int ch_num, AVCodecContext *avctx)
  342. {
  343. int i, weight_idx = 0, delta, diff, num_long_vals,
  344. delta_bits, min_val, vlc_sel, start_val;
  345. VLC *vlc_tab;
  346. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  347. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  348. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  349. case 0: /* coded using constant number of bits */
  350. for (i = 0; i < ctx->used_quant_units; i++)
  351. chan->qu_sf_idx[i] = get_bits(gb, 6);
  352. break;
  353. case 1:
  354. if (ch_num) {
  355. vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
  356. for (i = 0; i < ctx->used_quant_units; i++) {
  357. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  358. chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
  359. }
  360. } else {
  361. weight_idx = get_bits(gb, 2);
  362. if (weight_idx == 3) {
  363. UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
  364. num_long_vals = get_bits(gb, 5);
  365. delta_bits = get_bits(gb, 2);
  366. min_val = get_bits(gb, 4) - 7;
  367. for (i = 0; i < num_long_vals; i++)
  368. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
  369. get_bits(gb, 4) - 7) & 0x3F;
  370. /* all others are: min_val + delta */
  371. for (i = num_long_vals; i < ctx->used_quant_units; i++)
  372. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
  373. get_bitsz(gb, delta_bits)) & 0x3F;
  374. } else {
  375. num_long_vals = get_bits(gb, 5);
  376. delta_bits = get_bits(gb, 3);
  377. min_val = get_bits(gb, 6);
  378. if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
  379. av_log(avctx, AV_LOG_ERROR,
  380. "SF mode 1: invalid parameters!\n");
  381. return AVERROR_INVALIDDATA;
  382. }
  383. /* read full-precision SF indexes */
  384. for (i = 0; i < num_long_vals; i++)
  385. chan->qu_sf_idx[i] = get_bits(gb, 6);
  386. /* all others are: min_val + delta */
  387. for (i = num_long_vals; i < ctx->used_quant_units; i++)
  388. chan->qu_sf_idx[i] = (min_val +
  389. get_bitsz(gb, delta_bits)) & 0x3F;
  390. }
  391. }
  392. break;
  393. case 2:
  394. if (ch_num) {
  395. vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
  396. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  397. chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
  398. for (i = 1; i < ctx->used_quant_units; i++) {
  399. diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
  400. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  401. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
  402. }
  403. } else {
  404. vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
  405. UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
  406. for (i = 0; i < ctx->used_quant_units; i++) {
  407. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  408. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
  409. sign_extend(delta, 4)) & 0x3F;
  410. }
  411. }
  412. break;
  413. case 3:
  414. if (ch_num) {
  415. /* copy coefficients from reference channel */
  416. for (i = 0; i < ctx->used_quant_units; i++)
  417. chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
  418. } else {
  419. weight_idx = get_bits(gb, 2);
  420. vlc_sel = get_bits(gb, 2);
  421. vlc_tab = &sf_vlc_tabs[vlc_sel];
  422. if (weight_idx == 3) {
  423. vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
  424. UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
  425. diff = (get_bits(gb, 4) + 56) & 0x3F;
  426. chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
  427. for (i = 1; i < ctx->used_quant_units; i++) {
  428. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  429. diff = (diff + sign_extend(delta, 4)) & 0x3F;
  430. chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
  431. }
  432. } else {
  433. /* 1st coefficient is coded directly */
  434. chan->qu_sf_idx[0] = get_bits(gb, 6);
  435. for (i = 1; i < ctx->used_quant_units; i++) {
  436. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  437. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
  438. }
  439. }
  440. }
  441. break;
  442. }
  443. if (weight_idx && weight_idx < 3)
  444. return subtract_sf_weights(ctx, chan, weight_idx, avctx);
  445. return 0;
  446. }
  447. /**
  448. * Decode word length information for each channel.
  449. *
  450. * @param[in] gb the GetBit context
  451. * @param[in,out] ctx ptr to the channel unit context
  452. * @param[in] num_channels number of channels to process
  453. * @param[in] avctx ptr to the AVCodecContext
  454. * @return result code: 0 = OK, otherwise - error code
  455. */
  456. static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  457. int num_channels, AVCodecContext *avctx)
  458. {
  459. int ch_num, i, ret;
  460. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  461. memset(ctx->channels[ch_num].qu_wordlen, 0,
  462. sizeof(ctx->channels[ch_num].qu_wordlen));
  463. if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0)
  464. return ret;
  465. }
  466. /* scan for last non-zero coeff in both channels and
  467. * set number of quant units having coded spectrum */
  468. for (i = ctx->num_quant_units - 1; i >= 0; i--)
  469. if (ctx->channels[0].qu_wordlen[i] ||
  470. (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
  471. break;
  472. ctx->used_quant_units = i + 1;
  473. return 0;
  474. }
  475. /**
  476. * Decode scale factor indexes for each channel.
  477. *
  478. * @param[in] gb the GetBit context
  479. * @param[in,out] ctx ptr to the channel unit context
  480. * @param[in] num_channels number of channels to process
  481. * @param[in] avctx ptr to the AVCodecContext
  482. * @return result code: 0 = OK, otherwise - error code
  483. */
  484. static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  485. int num_channels, AVCodecContext *avctx)
  486. {
  487. int ch_num, ret;
  488. if (!ctx->used_quant_units)
  489. return 0;
  490. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  491. memset(ctx->channels[ch_num].qu_sf_idx, 0,
  492. sizeof(ctx->channels[ch_num].qu_sf_idx));
  493. if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0)
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. /**
  499. * Decode number of code table values.
  500. *
  501. * @param[in] gb the GetBit context
  502. * @param[in,out] ctx ptr to the channel unit context
  503. * @param[in] avctx ptr to the AVCodecContext
  504. * @return result code: 0 = OK, otherwise - error code
  505. */
  506. static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  507. AVCodecContext *avctx)
  508. {
  509. int num_coded_vals;
  510. if (get_bits1(gb)) {
  511. num_coded_vals = get_bits(gb, 5);
  512. if (num_coded_vals > ctx->used_quant_units) {
  513. av_log(avctx, AV_LOG_ERROR,
  514. "Invalid number of code table indexes: %d!\n", num_coded_vals);
  515. return AVERROR_INVALIDDATA;
  516. }
  517. return num_coded_vals;
  518. } else
  519. return ctx->used_quant_units;
  520. }
  521. #define DEC_CT_IDX_COMMON(OP) \
  522. num_vals = get_num_ct_values(gb, ctx, avctx); \
  523. if (num_vals < 0) \
  524. return num_vals; \
  525. \
  526. for (i = 0; i < num_vals; i++) { \
  527. if (chan->qu_wordlen[i]) { \
  528. chan->qu_tab_idx[i] = OP; \
  529. } else if (ch_num && ref_chan->qu_wordlen[i]) \
  530. /* get clone master flag */ \
  531. chan->qu_tab_idx[i] = get_bits1(gb); \
  532. }
  533. #define CODING_DIRECT get_bits(gb, num_bits)
  534. #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
  535. #define CODING_VLC_DELTA \
  536. (!i) ? CODING_VLC \
  537. : (pred + get_vlc2(gb, delta_vlc->table, \
  538. delta_vlc->bits, 1)) & mask; \
  539. pred = chan->qu_tab_idx[i]
  540. #define CODING_VLC_DIFF \
  541. (ref_chan->qu_tab_idx[i] + \
  542. get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
  543. /**
  544. * Decode code table indexes for each quant unit of a channel.
  545. *
  546. * @param[in] gb the GetBit context
  547. * @param[in,out] ctx ptr to the channel unit context
  548. * @param[in] ch_num channel to process
  549. * @param[in] avctx ptr to the AVCodecContext
  550. * @return result code: 0 = OK, otherwise - error code
  551. */
  552. static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  553. int ch_num, AVCodecContext *avctx)
  554. {
  555. int i, num_vals, num_bits, pred;
  556. int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
  557. VLC *vlc_tab, *delta_vlc;
  558. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  559. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  560. chan->table_type = get_bits1(gb);
  561. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  562. case 0: /* directly coded */
  563. num_bits = ctx->use_full_table + 2;
  564. DEC_CT_IDX_COMMON(CODING_DIRECT);
  565. break;
  566. case 1: /* entropy-coded */
  567. vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
  568. : ct_vlc_tabs;
  569. DEC_CT_IDX_COMMON(CODING_VLC);
  570. break;
  571. case 2: /* entropy-coded delta */
  572. if (ctx->use_full_table) {
  573. vlc_tab = &ct_vlc_tabs[1];
  574. delta_vlc = &ct_vlc_tabs[2];
  575. } else {
  576. vlc_tab = ct_vlc_tabs;
  577. delta_vlc = ct_vlc_tabs;
  578. }
  579. pred = 0;
  580. DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
  581. break;
  582. case 3: /* entropy-coded difference to master */
  583. if (ch_num) {
  584. vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
  585. : ct_vlc_tabs;
  586. DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
  587. }
  588. break;
  589. }
  590. return 0;
  591. }
  592. /**
  593. * Decode code table indexes for each channel.
  594. *
  595. * @param[in] gb the GetBit context
  596. * @param[in,out] ctx ptr to the channel unit context
  597. * @param[in] num_channels number of channels to process
  598. * @param[in] avctx ptr to the AVCodecContext
  599. * @return result code: 0 = OK, otherwise - error code
  600. */
  601. static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  602. int num_channels, AVCodecContext *avctx)
  603. {
  604. int ch_num, ret;
  605. if (!ctx->used_quant_units)
  606. return 0;
  607. ctx->use_full_table = get_bits1(gb);
  608. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  609. memset(ctx->channels[ch_num].qu_tab_idx, 0,
  610. sizeof(ctx->channels[ch_num].qu_tab_idx));
  611. if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0)
  612. return ret;
  613. }
  614. return 0;
  615. }
  616. /**
  617. * Decode huffman-coded spectral lines for a given quant unit.
  618. *
  619. * This is a generalized version for all known coding modes.
  620. * Its speed can be improved by creating separate functions for each mode.
  621. *
  622. * @param[in] gb the GetBit context
  623. * @param[in] tab code table telling how to decode spectral lines
  624. * @param[in] vlc_tab ptr to the huffman table associated with the code table
  625. * @param[out] out pointer to buffer where decoded data should be stored
  626. * @param[in] num_specs number of spectral lines to decode
  627. */
  628. static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
  629. VLC *vlc_tab, int16_t *out, const int num_specs)
  630. {
  631. int i, j, pos, cf;
  632. int group_size = tab->group_size;
  633. int num_coeffs = tab->num_coeffs;
  634. int bits = tab->bits;
  635. int is_signed = tab->is_signed;
  636. unsigned val;
  637. for (pos = 0; pos < num_specs;) {
  638. if (group_size == 1 || get_bits1(gb)) {
  639. for (j = 0; j < group_size; j++) {
  640. val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  641. for (i = 0; i < num_coeffs; i++) {
  642. cf = av_mod_uintp2(val, bits);
  643. if (is_signed)
  644. cf = sign_extend(cf, bits);
  645. else if (cf && get_bits1(gb))
  646. cf = -cf;
  647. out[pos++] = cf;
  648. val >>= bits;
  649. }
  650. }
  651. } else /* group skipped */
  652. pos += group_size * num_coeffs;
  653. }
  654. }
  655. /**
  656. * Decode huffman-coded IMDCT spectrum for all channels.
  657. *
  658. * @param[in] gb the GetBit context
  659. * @param[in,out] ctx ptr to the channel unit context
  660. * @param[in] num_channels number of channels to process
  661. * @param[in] avctx ptr to the AVCodecContext
  662. */
  663. static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  664. int num_channels, AVCodecContext *avctx)
  665. {
  666. int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
  667. const Atrac3pSpecCodeTab *tab;
  668. Atrac3pChanParams *chan;
  669. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  670. chan = &ctx->channels[ch_num];
  671. memset(chan->spectrum, 0, sizeof(chan->spectrum));
  672. /* set power compensation level to disabled */
  673. memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
  674. for (qu = 0; qu < ctx->used_quant_units; qu++) {
  675. num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
  676. ff_atrac3p_qu_to_spec_pos[qu];
  677. wordlen = chan->qu_wordlen[qu];
  678. codetab = chan->qu_tab_idx[qu];
  679. if (wordlen) {
  680. if (!ctx->use_full_table)
  681. codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
  682. tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
  683. tab = &atrac3p_spectra_tabs[tab_index];
  684. decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
  685. &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
  686. num_specs);
  687. } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
  688. /* copy coefficients from master */
  689. memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
  690. &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
  691. num_specs *
  692. sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
  693. chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
  694. }
  695. }
  696. /* Power compensation levels only present in the bitstream
  697. * if there are more than 2 quant units. The lowest two units
  698. * correspond to the frequencies 0...351 Hz, whose shouldn't
  699. * be affected by the power compensation. */
  700. if (ctx->used_quant_units > 2) {
  701. num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
  702. for (i = 0; i < num_specs; i++)
  703. chan->power_levs[i] = get_bits(gb, 4);
  704. }
  705. }
  706. }
  707. /**
  708. * Retrieve specified amount of flag bits from the input bitstream.
  709. * The data can be shortened in the case of the following two common conditions:
  710. * if all bits are zero then only one signal bit = 0 will be stored,
  711. * if all bits are ones then two signal bits = 1,0 will be stored.
  712. * Otherwise, all necessary bits will be directly stored
  713. * prefixed by two signal bits = 1,1.
  714. *
  715. * @param[in] gb ptr to the GetBitContext
  716. * @param[out] out where to place decoded flags
  717. * @param[in] num_flags number of flags to process
  718. * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
  719. */
  720. static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
  721. {
  722. int i, result;
  723. memset(out, 0, num_flags);
  724. result = get_bits1(gb);
  725. if (result) {
  726. if (get_bits1(gb))
  727. for (i = 0; i < num_flags; i++)
  728. out[i] = get_bits1(gb);
  729. else
  730. memset(out, 1, num_flags);
  731. }
  732. return result;
  733. }
  734. /**
  735. * Decode mdct window shape flags for all channels.
  736. *
  737. * @param[in] gb the GetBit context
  738. * @param[in,out] ctx ptr to the channel unit context
  739. * @param[in] num_channels number of channels to process
  740. */
  741. static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  742. int num_channels)
  743. {
  744. int ch_num;
  745. for (ch_num = 0; ch_num < num_channels; ch_num++)
  746. get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
  747. ctx->num_subbands);
  748. }
  749. /**
  750. * Decode number of gain control points.
  751. *
  752. * @param[in] gb the GetBit context
  753. * @param[in,out] ctx ptr to the channel unit context
  754. * @param[in] ch_num channel to process
  755. * @param[in] coded_subbands number of subbands to process
  756. * @return result code: 0 = OK, otherwise - error code
  757. */
  758. static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  759. int ch_num, int coded_subbands)
  760. {
  761. int i, delta, delta_bits, min_val;
  762. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  763. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  764. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  765. case 0: /* fixed-length coding */
  766. for (i = 0; i < coded_subbands; i++)
  767. chan->gain_data[i].num_points = get_bits(gb, 3);
  768. break;
  769. case 1: /* variable-length coding */
  770. for (i = 0; i < coded_subbands; i++)
  771. chan->gain_data[i].num_points =
  772. get_vlc2(gb, gain_vlc_tabs[0].table,
  773. gain_vlc_tabs[0].bits, 1);
  774. break;
  775. case 2:
  776. if (ch_num) { /* VLC modulo delta to master channel */
  777. for (i = 0; i < coded_subbands; i++) {
  778. delta = get_vlc2(gb, gain_vlc_tabs[1].table,
  779. gain_vlc_tabs[1].bits, 1);
  780. chan->gain_data[i].num_points =
  781. (ref_chan->gain_data[i].num_points + delta) & 7;
  782. }
  783. } else { /* VLC modulo delta to previous */
  784. chan->gain_data[0].num_points =
  785. get_vlc2(gb, gain_vlc_tabs[0].table,
  786. gain_vlc_tabs[0].bits, 1);
  787. for (i = 1; i < coded_subbands; i++) {
  788. delta = get_vlc2(gb, gain_vlc_tabs[1].table,
  789. gain_vlc_tabs[1].bits, 1);
  790. chan->gain_data[i].num_points =
  791. (chan->gain_data[i - 1].num_points + delta) & 7;
  792. }
  793. }
  794. break;
  795. case 3:
  796. if (ch_num) { /* copy data from master channel */
  797. for (i = 0; i < coded_subbands; i++)
  798. chan->gain_data[i].num_points =
  799. ref_chan->gain_data[i].num_points;
  800. } else { /* shorter delta to min */
  801. delta_bits = get_bits(gb, 2);
  802. min_val = get_bits(gb, 3);
  803. for (i = 0; i < coded_subbands; i++) {
  804. chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);
  805. if (chan->gain_data[i].num_points > 7)
  806. return AVERROR_INVALIDDATA;
  807. }
  808. }
  809. }
  810. return 0;
  811. }
  812. /**
  813. * Implements coding mode 3 (slave) for gain compensation levels.
  814. *
  815. * @param[out] dst ptr to the output array
  816. * @param[in] ref ptr to the reference channel
  817. */
  818. static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
  819. {
  820. int i;
  821. for (i = 0; i < dst->num_points; i++)
  822. dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
  823. }
  824. /**
  825. * Implements coding mode 1 (master) for gain compensation levels.
  826. *
  827. * @param[in] gb the GetBit context
  828. * @param[in] ctx ptr to the channel unit context
  829. * @param[out] dst ptr to the output array
  830. */
  831. static inline void gainc_level_mode1m(GetBitContext *gb,
  832. Atrac3pChanUnitCtx *ctx,
  833. AtracGainInfo *dst)
  834. {
  835. int i, delta;
  836. if (dst->num_points > 0)
  837. dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
  838. gain_vlc_tabs[2].bits, 1);
  839. for (i = 1; i < dst->num_points; i++) {
  840. delta = get_vlc2(gb, gain_vlc_tabs[3].table,
  841. gain_vlc_tabs[3].bits, 1);
  842. dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
  843. }
  844. }
  845. /**
  846. * Decode level code for each gain control point.
  847. *
  848. * @param[in] gb the GetBit context
  849. * @param[in,out] ctx ptr to the channel unit context
  850. * @param[in] ch_num channel to process
  851. * @param[in] coded_subbands number of subbands to process
  852. * @return result code: 0 = OK, otherwise - error code
  853. */
  854. static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  855. int ch_num, int coded_subbands)
  856. {
  857. int sb, i, delta, delta_bits, min_val, pred;
  858. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  859. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  860. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  861. case 0: /* fixed-length coding */
  862. for (sb = 0; sb < coded_subbands; sb++)
  863. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  864. chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
  865. break;
  866. case 1:
  867. if (ch_num) { /* VLC modulo delta to master channel */
  868. for (sb = 0; sb < coded_subbands; sb++)
  869. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  870. delta = get_vlc2(gb, gain_vlc_tabs[5].table,
  871. gain_vlc_tabs[5].bits, 1);
  872. pred = (i >= ref_chan->gain_data[sb].num_points)
  873. ? 7 : ref_chan->gain_data[sb].lev_code[i];
  874. chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
  875. }
  876. } else { /* VLC modulo delta to previous */
  877. for (sb = 0; sb < coded_subbands; sb++)
  878. gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
  879. }
  880. break;
  881. case 2:
  882. if (ch_num) { /* VLC modulo delta to previous or clone master */
  883. for (sb = 0; sb < coded_subbands; sb++)
  884. if (chan->gain_data[sb].num_points > 0) {
  885. if (get_bits1(gb))
  886. gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
  887. else
  888. gainc_level_mode3s(&chan->gain_data[sb],
  889. &ref_chan->gain_data[sb]);
  890. }
  891. } else { /* VLC modulo delta to lev_codes of previous subband */
  892. if (chan->gain_data[0].num_points > 0)
  893. gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
  894. for (sb = 1; sb < coded_subbands; sb++)
  895. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  896. delta = get_vlc2(gb, gain_vlc_tabs[4].table,
  897. gain_vlc_tabs[4].bits, 1);
  898. pred = (i >= chan->gain_data[sb - 1].num_points)
  899. ? 7 : chan->gain_data[sb - 1].lev_code[i];
  900. chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
  901. }
  902. }
  903. break;
  904. case 3:
  905. if (ch_num) { /* clone master */
  906. for (sb = 0; sb < coded_subbands; sb++)
  907. gainc_level_mode3s(&chan->gain_data[sb],
  908. &ref_chan->gain_data[sb]);
  909. } else { /* shorter delta to min */
  910. delta_bits = get_bits(gb, 2);
  911. min_val = get_bits(gb, 4);
  912. for (sb = 0; sb < coded_subbands; sb++)
  913. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  914. chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);
  915. if (chan->gain_data[sb].lev_code[i] > 15)
  916. return AVERROR_INVALIDDATA;
  917. }
  918. }
  919. break;
  920. }
  921. return 0;
  922. }
  923. /**
  924. * Implements coding mode 0 for gain compensation locations.
  925. *
  926. * @param[in] gb the GetBit context
  927. * @param[in] ctx ptr to the channel unit context
  928. * @param[out] dst ptr to the output array
  929. * @param[in] pos position of the value to be processed
  930. */
  931. static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  932. AtracGainInfo *dst, int pos)
  933. {
  934. int delta_bits;
  935. if (!pos || dst->loc_code[pos - 1] < 15)
  936. dst->loc_code[pos] = get_bits(gb, 5);
  937. else if (dst->loc_code[pos - 1] >= 30)
  938. dst->loc_code[pos] = 31;
  939. else {
  940. delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
  941. dst->loc_code[pos] = dst->loc_code[pos - 1] +
  942. get_bits(gb, delta_bits) + 1;
  943. }
  944. }
  945. /**
  946. * Implements coding mode 1 for gain compensation locations.
  947. *
  948. * @param[in] gb the GetBit context
  949. * @param[in] ctx ptr to the channel unit context
  950. * @param[out] dst ptr to the output array
  951. */
  952. static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  953. AtracGainInfo *dst)
  954. {
  955. int i;
  956. VLC *tab;
  957. if (dst->num_points > 0) {
  958. /* 1st coefficient is stored directly */
  959. dst->loc_code[0] = get_bits(gb, 5);
  960. for (i = 1; i < dst->num_points; i++) {
  961. /* switch VLC according to the curve direction
  962. * (ascending/descending) */
  963. tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
  964. ? &gain_vlc_tabs[7]
  965. : &gain_vlc_tabs[9];
  966. dst->loc_code[i] = dst->loc_code[i - 1] +
  967. get_vlc2(gb, tab->table, tab->bits, 1);
  968. }
  969. }
  970. }
  971. /**
  972. * Decode location code for each gain control point.
  973. *
  974. * @param[in] gb the GetBit context
  975. * @param[in,out] ctx ptr to the channel unit context
  976. * @param[in] ch_num channel to process
  977. * @param[in] coded_subbands number of subbands to process
  978. * @param[in] avctx ptr to the AVCodecContext
  979. * @return result code: 0 = OK, otherwise - error code
  980. */
  981. static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  982. int ch_num, int coded_subbands,
  983. AVCodecContext *avctx)
  984. {
  985. int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
  986. AtracGainInfo *dst, *ref;
  987. VLC *tab;
  988. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  989. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  990. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  991. case 0: /* sequence of numbers in ascending order */
  992. for (sb = 0; sb < coded_subbands; sb++)
  993. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  994. gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
  995. break;
  996. case 1:
  997. if (ch_num) {
  998. for (sb = 0; sb < coded_subbands; sb++) {
  999. if (chan->gain_data[sb].num_points <= 0)
  1000. continue;
  1001. dst = &chan->gain_data[sb];
  1002. ref = &ref_chan->gain_data[sb];
  1003. /* 1st value is vlc-coded modulo delta to master */
  1004. delta = get_vlc2(gb, gain_vlc_tabs[10].table,
  1005. gain_vlc_tabs[10].bits, 1);
  1006. pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
  1007. dst->loc_code[0] = (pred + delta) & 0x1F;
  1008. for (i = 1; i < dst->num_points; i++) {
  1009. more_than_ref = i >= ref->num_points;
  1010. if (dst->lev_code[i] > dst->lev_code[i - 1]) {
  1011. /* ascending curve */
  1012. if (more_than_ref) {
  1013. delta =
  1014. get_vlc2(gb, gain_vlc_tabs[9].table,
  1015. gain_vlc_tabs[9].bits, 1);
  1016. dst->loc_code[i] = dst->loc_code[i - 1] + delta;
  1017. } else {
  1018. if (get_bits1(gb))
  1019. gainc_loc_mode0(gb, ctx, dst, i); // direct coding
  1020. else
  1021. dst->loc_code[i] = ref->loc_code[i]; // clone master
  1022. }
  1023. } else { /* descending curve */
  1024. tab = more_than_ref ? &gain_vlc_tabs[7]
  1025. : &gain_vlc_tabs[10];
  1026. delta = get_vlc2(gb, tab->table, tab->bits, 1);
  1027. if (more_than_ref)
  1028. dst->loc_code[i] = dst->loc_code[i - 1] + delta;
  1029. else
  1030. dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
  1031. }
  1032. }
  1033. }
  1034. } else /* VLC delta to previous */
  1035. for (sb = 0; sb < coded_subbands; sb++)
  1036. gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
  1037. break;
  1038. case 2:
  1039. if (ch_num) {
  1040. for (sb = 0; sb < coded_subbands; sb++) {
  1041. if (chan->gain_data[sb].num_points <= 0)
  1042. continue;
  1043. dst = &chan->gain_data[sb];
  1044. ref = &ref_chan->gain_data[sb];
  1045. if (dst->num_points > ref->num_points || get_bits1(gb))
  1046. gainc_loc_mode1(gb, ctx, dst);
  1047. else /* clone master for the whole subband */
  1048. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  1049. dst->loc_code[i] = ref->loc_code[i];
  1050. }
  1051. } else {
  1052. /* data for the first subband is coded directly */
  1053. for (i = 0; i < chan->gain_data[0].num_points; i++)
  1054. gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
  1055. for (sb = 1; sb < coded_subbands; sb++) {
  1056. if (chan->gain_data[sb].num_points <= 0)
  1057. continue;
  1058. dst = &chan->gain_data[sb];
  1059. /* 1st value is vlc-coded modulo delta to the corresponding
  1060. * value of the previous subband if any or zero */
  1061. delta = get_vlc2(gb, gain_vlc_tabs[6].table,
  1062. gain_vlc_tabs[6].bits, 1);
  1063. pred = dst[-1].num_points > 0
  1064. ? dst[-1].loc_code[0] : 0;
  1065. dst->loc_code[0] = (pred + delta) & 0x1F;
  1066. for (i = 1; i < dst->num_points; i++) {
  1067. more_than_ref = i >= dst[-1].num_points;
  1068. /* Select VLC table according to curve direction and
  1069. * presence of prediction. */
  1070. tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
  1071. 2 + more_than_ref + 6];
  1072. delta = get_vlc2(gb, tab->table, tab->bits, 1);
  1073. if (more_than_ref)
  1074. dst->loc_code[i] = dst->loc_code[i - 1] + delta;
  1075. else
  1076. dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
  1077. }
  1078. }
  1079. }
  1080. break;
  1081. case 3:
  1082. if (ch_num) { /* clone master or direct or direct coding */
  1083. for (sb = 0; sb < coded_subbands; sb++)
  1084. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  1085. if (i >= ref_chan->gain_data[sb].num_points)
  1086. gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
  1087. else
  1088. chan->gain_data[sb].loc_code[i] =
  1089. ref_chan->gain_data[sb].loc_code[i];
  1090. }
  1091. } else { /* shorter delta to min */
  1092. delta_bits = get_bits(gb, 2) + 1;
  1093. min_val = get_bits(gb, 5);
  1094. for (sb = 0; sb < coded_subbands; sb++)
  1095. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  1096. chan->gain_data[sb].loc_code[i] = min_val + i +
  1097. get_bits(gb, delta_bits);
  1098. }
  1099. break;
  1100. }
  1101. /* Validate decoded information */
  1102. for (sb = 0; sb < coded_subbands; sb++) {
  1103. dst = &chan->gain_data[sb];
  1104. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  1105. if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
  1106. (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
  1107. av_log(avctx, AV_LOG_ERROR,
  1108. "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
  1109. ch_num, sb, i, dst->loc_code[i]);
  1110. return AVERROR_INVALIDDATA;
  1111. }
  1112. }
  1113. }
  1114. return 0;
  1115. }
  1116. /**
  1117. * Decode gain control data for all channels.
  1118. *
  1119. * @param[in] gb the GetBit context
  1120. * @param[in,out] ctx ptr to the channel unit context
  1121. * @param[in] num_channels number of channels to process
  1122. * @param[in] avctx ptr to the AVCodecContext
  1123. * @return result code: 0 = OK, otherwise - error code
  1124. */
  1125. static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1126. int num_channels, AVCodecContext *avctx)
  1127. {
  1128. int ch_num, coded_subbands, sb, ret;
  1129. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  1130. memset(ctx->channels[ch_num].gain_data, 0,
  1131. sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
  1132. if (get_bits1(gb)) { /* gain control data present? */
  1133. coded_subbands = get_bits(gb, 4) + 1;
  1134. if (get_bits1(gb)) /* is high band gain data replication on? */
  1135. ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
  1136. else
  1137. ctx->channels[ch_num].num_gain_subbands = coded_subbands;
  1138. if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
  1139. (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||
  1140. (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0)
  1141. return ret;
  1142. if (coded_subbands > 0) { /* propagate gain data if requested */
  1143. for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
  1144. ctx->channels[ch_num].gain_data[sb] =
  1145. ctx->channels[ch_num].gain_data[sb - 1];
  1146. }
  1147. } else {
  1148. ctx->channels[ch_num].num_gain_subbands = 0;
  1149. }
  1150. }
  1151. return 0;
  1152. }
  1153. /**
  1154. * Decode envelope for all tones of a channel.
  1155. *
  1156. * @param[in] gb the GetBit context
  1157. * @param[in,out] ctx ptr to the channel unit context
  1158. * @param[in] ch_num channel to process
  1159. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1160. * 1 - tone data present
  1161. */
  1162. static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1163. int ch_num, int band_has_tones[])
  1164. {
  1165. int sb;
  1166. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1167. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1168. if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
  1169. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1170. if (!band_has_tones[sb])
  1171. continue;
  1172. dst[sb].pend_env.has_start_point = get_bits1(gb);
  1173. dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
  1174. ? get_bits(gb, 5) : -1;
  1175. dst[sb].pend_env.has_stop_point = get_bits1(gb);
  1176. dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
  1177. ? get_bits(gb, 5) : 32;
  1178. }
  1179. } else { /* mode 1(slave only): copy master */
  1180. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1181. if (!band_has_tones[sb])
  1182. continue;
  1183. dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
  1184. dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
  1185. dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
  1186. dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
  1187. }
  1188. }
  1189. }
  1190. /**
  1191. * Decode number of tones for each subband of a channel.
  1192. *
  1193. * @param[in] gb the GetBit context
  1194. * @param[in,out] ctx ptr to the channel unit context
  1195. * @param[in] ch_num channel to process
  1196. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1197. * 1 - tone data present
  1198. * @param[in] avctx ptr to the AVCodecContext
  1199. * @return result code: 0 = OK, otherwise - error code
  1200. */
  1201. static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1202. int ch_num, int band_has_tones[],
  1203. AVCodecContext *avctx)
  1204. {
  1205. int mode, sb, delta;
  1206. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1207. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1208. mode = get_bits(gb, ch_num + 1);
  1209. switch (mode) {
  1210. case 0: /** fixed-length coding */
  1211. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1212. if (band_has_tones[sb])
  1213. dst[sb].num_wavs = get_bits(gb, 4);
  1214. break;
  1215. case 1: /** variable-length coding */
  1216. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1217. if (band_has_tones[sb])
  1218. dst[sb].num_wavs =
  1219. get_vlc2(gb, tone_vlc_tabs[1].table,
  1220. tone_vlc_tabs[1].bits, 1);
  1221. break;
  1222. case 2: /** VLC modulo delta to master (slave only) */
  1223. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1224. if (band_has_tones[sb]) {
  1225. delta = get_vlc2(gb, tone_vlc_tabs[2].table,
  1226. tone_vlc_tabs[2].bits, 1);
  1227. delta = sign_extend(delta, 3);
  1228. dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
  1229. }
  1230. break;
  1231. case 3: /** copy master (slave only) */
  1232. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1233. if (band_has_tones[sb])
  1234. dst[sb].num_wavs = ref[sb].num_wavs;
  1235. break;
  1236. }
  1237. /** initialize start tone index for each subband */
  1238. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1239. if (band_has_tones[sb]) {
  1240. if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
  1241. av_log(avctx, AV_LOG_ERROR,
  1242. "Too many tones: %d (max. 48), frame: %d!\n",
  1243. ctx->waves_info->tones_index + dst[sb].num_wavs,
  1244. avctx->frame_number);
  1245. return AVERROR_INVALIDDATA;
  1246. }
  1247. dst[sb].start_index = ctx->waves_info->tones_index;
  1248. ctx->waves_info->tones_index += dst[sb].num_wavs;
  1249. }
  1250. return 0;
  1251. }
  1252. /**
  1253. * Decode frequency information for each subband of a channel.
  1254. *
  1255. * @param[in] gb the GetBit context
  1256. * @param[in,out] ctx ptr to the channel unit context
  1257. * @param[in] ch_num channel to process
  1258. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1259. * 1 - tone data present
  1260. */
  1261. static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1262. int ch_num, int band_has_tones[])
  1263. {
  1264. int sb, i, direction, nbits, pred, delta;
  1265. Atrac3pWaveParam *iwav, *owav;
  1266. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1267. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1268. if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
  1269. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1270. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1271. continue;
  1272. iwav = &ctx->waves_info->waves[dst[sb].start_index];
  1273. direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
  1274. if (direction) { /** packed numbers in descending order */
  1275. if (dst[sb].num_wavs)
  1276. iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
  1277. for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
  1278. nbits = av_log2(iwav[i+1].freq_index) + 1;
  1279. iwav[i].freq_index = get_bits(gb, nbits);
  1280. }
  1281. } else { /** packed numbers in ascending order */
  1282. for (i = 0; i < dst[sb].num_wavs; i++) {
  1283. if (!i || iwav[i - 1].freq_index < 512)
  1284. iwav[i].freq_index = get_bits(gb, 10);
  1285. else {
  1286. nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
  1287. iwav[i].freq_index = get_bits(gb, nbits) +
  1288. 1024 - (1 << nbits);
  1289. }
  1290. }
  1291. }
  1292. }
  1293. } else { /* mode 1: VLC modulo delta to master (slave only) */
  1294. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1295. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1296. continue;
  1297. iwav = &ctx->waves_info->waves[ref[sb].start_index];
  1298. owav = &ctx->waves_info->waves[dst[sb].start_index];
  1299. for (i = 0; i < dst[sb].num_wavs; i++) {
  1300. delta = get_vlc2(gb, tone_vlc_tabs[6].table,
  1301. tone_vlc_tabs[6].bits, 1);
  1302. delta = sign_extend(delta, 8);
  1303. pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
  1304. (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
  1305. owav[i].freq_index = (pred + delta) & 0x3FF;
  1306. }
  1307. }
  1308. }
  1309. }
  1310. /**
  1311. * Decode amplitude information for each subband of a channel.
  1312. *
  1313. * @param[in] gb the GetBit context
  1314. * @param[in,out] ctx ptr to the channel unit context
  1315. * @param[in] ch_num channel to process
  1316. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1317. * 1 - tone data present
  1318. */
  1319. static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1320. int ch_num, int band_has_tones[])
  1321. {
  1322. int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
  1323. Atrac3pWaveParam *wsrc, *wref;
  1324. int refwaves[48] = { 0 };
  1325. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1326. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1327. if (ch_num) {
  1328. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1329. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1330. continue;
  1331. wsrc = &ctx->waves_info->waves[dst[sb].start_index];
  1332. wref = &ctx->waves_info->waves[ref[sb].start_index];
  1333. for (j = 0; j < dst[sb].num_wavs; j++) {
  1334. for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
  1335. diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
  1336. if (diff < maxdiff) {
  1337. maxdiff = diff;
  1338. fi = i;
  1339. }
  1340. }
  1341. if (maxdiff < 8)
  1342. refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
  1343. else if (j < ref[sb].num_wavs)
  1344. refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
  1345. else
  1346. refwaves[dst[sb].start_index + j] = -1;
  1347. }
  1348. }
  1349. }
  1350. mode = get_bits(gb, ch_num + 1);
  1351. switch (mode) {
  1352. case 0: /** fixed-length coding */
  1353. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1354. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1355. continue;
  1356. if (ctx->waves_info->amplitude_mode)
  1357. for (i = 0; i < dst[sb].num_wavs; i++)
  1358. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
  1359. else
  1360. ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
  1361. }
  1362. break;
  1363. case 1: /** min + VLC delta */
  1364. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1365. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1366. continue;
  1367. if (ctx->waves_info->amplitude_mode)
  1368. for (i = 0; i < dst[sb].num_wavs; i++)
  1369. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
  1370. get_vlc2(gb, tone_vlc_tabs[3].table,
  1371. tone_vlc_tabs[3].bits, 1) + 20;
  1372. else
  1373. ctx->waves_info->waves[dst[sb].start_index].amp_sf =
  1374. get_vlc2(gb, tone_vlc_tabs[4].table,
  1375. tone_vlc_tabs[4].bits, 1) + 24;
  1376. }
  1377. break;
  1378. case 2: /** VLC modulo delta to master (slave only) */
  1379. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1380. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1381. continue;
  1382. for (i = 0; i < dst[sb].num_wavs; i++) {
  1383. delta = get_vlc2(gb, tone_vlc_tabs[5].table,
  1384. tone_vlc_tabs[5].bits, 1);
  1385. delta = sign_extend(delta, 5);
  1386. pred = refwaves[dst[sb].start_index + i] >= 0 ?
  1387. ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
  1388. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
  1389. }
  1390. }
  1391. break;
  1392. case 3: /** clone master (slave only) */
  1393. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1394. if (!band_has_tones[sb])
  1395. continue;
  1396. for (i = 0; i < dst[sb].num_wavs; i++)
  1397. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
  1398. refwaves[dst[sb].start_index + i] >= 0
  1399. ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
  1400. : 32;
  1401. }
  1402. break;
  1403. }
  1404. }
  1405. /**
  1406. * Decode phase information for each subband of a channel.
  1407. *
  1408. * @param[in] gb the GetBit context
  1409. * @param[in,out] ctx ptr to the channel unit context
  1410. * @param[in] ch_num channel to process
  1411. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1412. * 1 - tone data present
  1413. */
  1414. static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1415. int ch_num, int band_has_tones[])
  1416. {
  1417. int sb, i;
  1418. Atrac3pWaveParam *wparam;
  1419. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1420. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1421. if (!band_has_tones[sb])
  1422. continue;
  1423. wparam = &ctx->waves_info->waves[dst[sb].start_index];
  1424. for (i = 0; i < dst[sb].num_wavs; i++)
  1425. wparam[i].phase_index = get_bits(gb, 5);
  1426. }
  1427. }
  1428. /**
  1429. * Decode tones info for all channels.
  1430. *
  1431. * @param[in] gb the GetBit context
  1432. * @param[in,out] ctx ptr to the channel unit context
  1433. * @param[in] num_channels number of channels to process
  1434. * @param[in] avctx ptr to the AVCodecContext
  1435. * @return result code: 0 = OK, otherwise - error code
  1436. */
  1437. static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1438. int num_channels, AVCodecContext *avctx)
  1439. {
  1440. int ch_num, i, ret;
  1441. int band_has_tones[16];
  1442. for (ch_num = 0; ch_num < num_channels; ch_num++)
  1443. memset(ctx->channels[ch_num].tones_info, 0,
  1444. sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
  1445. ctx->waves_info->tones_present = get_bits1(gb);
  1446. if (!ctx->waves_info->tones_present)
  1447. return 0;
  1448. memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
  1449. ctx->waves_info->amplitude_mode = get_bits1(gb);
  1450. if (!ctx->waves_info->amplitude_mode) {
  1451. avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
  1452. return AVERROR_PATCHWELCOME;
  1453. }
  1454. ctx->waves_info->num_tone_bands =
  1455. get_vlc2(gb, tone_vlc_tabs[0].table,
  1456. tone_vlc_tabs[0].bits, 1) + 1;
  1457. if (num_channels == 2) {
  1458. get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
  1459. get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
  1460. get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);
  1461. }
  1462. ctx->waves_info->tones_index = 0;
  1463. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  1464. for (i = 0; i < ctx->waves_info->num_tone_bands; i++)
  1465. band_has_tones[i] = !ch_num ? 1 : !ctx->waves_info->tone_sharing[i];
  1466. decode_tones_envelope(gb, ctx, ch_num, band_has_tones);
  1467. if ((ret = decode_band_numwavs(gb, ctx, ch_num, band_has_tones,
  1468. avctx)) < 0)
  1469. return ret;
  1470. decode_tones_frequency(gb, ctx, ch_num, band_has_tones);
  1471. decode_tones_amplitude(gb, ctx, ch_num, band_has_tones);
  1472. decode_tones_phase(gb, ctx, ch_num, band_has_tones);
  1473. }
  1474. if (num_channels == 2) {
  1475. for (i = 0; i < ctx->waves_info->num_tone_bands; i++) {
  1476. if (ctx->waves_info->tone_sharing[i])
  1477. ctx->channels[1].tones_info[i] = ctx->channels[0].tones_info[i];
  1478. if (ctx->waves_info->tone_master[i])
  1479. FFSWAP(Atrac3pWavesData, ctx->channels[0].tones_info[i],
  1480. ctx->channels[1].tones_info[i]);
  1481. }
  1482. }
  1483. return 0;
  1484. }
  1485. int ff_atrac3p_decode_channel_unit(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1486. int num_channels, AVCodecContext *avctx)
  1487. {
  1488. int ret;
  1489. /* parse sound header */
  1490. ctx->num_quant_units = get_bits(gb, 5) + 1;
  1491. if (ctx->num_quant_units > 28 && ctx->num_quant_units < 32) {
  1492. av_log(avctx, AV_LOG_ERROR,
  1493. "Invalid number of quantization units: %d!\n",
  1494. ctx->num_quant_units);
  1495. return AVERROR_INVALIDDATA;
  1496. }
  1497. ctx->mute_flag = get_bits1(gb);
  1498. /* decode various sound parameters */
  1499. if ((ret = decode_quant_wordlen(gb, ctx, num_channels, avctx)) < 0)
  1500. return ret;
  1501. ctx->num_subbands = atrac3p_qu_to_subband[ctx->num_quant_units - 1] + 1;
  1502. ctx->num_coded_subbands = ctx->used_quant_units
  1503. ? atrac3p_qu_to_subband[ctx->used_quant_units - 1] + 1
  1504. : 0;
  1505. if ((ret = decode_scale_factors(gb, ctx, num_channels, avctx)) < 0)
  1506. return ret;
  1507. if ((ret = decode_code_table_indexes(gb, ctx, num_channels, avctx)) < 0)
  1508. return ret;
  1509. decode_spectrum(gb, ctx, num_channels, avctx);
  1510. if (num_channels == 2) {
  1511. get_subband_flags(gb, ctx->swap_channels, ctx->num_coded_subbands);
  1512. get_subband_flags(gb, ctx->negate_coeffs, ctx->num_coded_subbands);
  1513. }
  1514. decode_window_shape(gb, ctx, num_channels);
  1515. if ((ret = decode_gainc_data(gb, ctx, num_channels, avctx)) < 0)
  1516. return ret;
  1517. if ((ret = decode_tones_info(gb, ctx, num_channels, avctx)) < 0)
  1518. return ret;
  1519. /* decode global noise info */
  1520. ctx->noise_present = get_bits1(gb);
  1521. if (ctx->noise_present) {
  1522. ctx->noise_level_index = get_bits(gb, 4);
  1523. ctx->noise_table_index = get_bits(gb, 4);
  1524. }
  1525. return 0;
  1526. }