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.

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