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.

1819 lines
70KB

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