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 FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Bitstream parser for ATRAC3+ decoder.
  25. */
  26. #include "libavutil/avassert.h"
  27. #include "avcodec.h"
  28. #include "get_bits.h"
  29. #include "atrac3plus.h"
  30. #include "atrac3plus_data.h"
  31. static VLC_TYPE tables_data[154276][2];
  32. static VLC wl_vlc_tabs[4];
  33. static VLC sf_vlc_tabs[8];
  34. static VLC ct_vlc_tabs[4];
  35. static VLC spec_vlc_tabs[112];
  36. static VLC gain_vlc_tabs[11];
  37. static VLC tone_vlc_tabs[7];
  38. /**
  39. * Generate canonical VLC table from given descriptor.
  40. *
  41. * @param[in] cb ptr to codebook descriptor
  42. * @param[in] 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(void)
  72. {
  73. int i, wl_vlc_offs, ct_vlc_offs, sf_vlc_offs, tab_offset;
  74. static const int wl_nb_bits[4] = { 2, 3, 5, 5 };
  75. static const int wl_nb_codes[4] = { 3, 5, 8, 8 };
  76. static const uint8_t * const 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 * const 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 * const wl_xlats[4] = {
  85. atrac3p_wl_huff_xlat1, atrac3p_wl_huff_xlat2, NULL, NULL
  86. };
  87. static const int ct_nb_bits[4] = { 3, 4, 4, 4 };
  88. static const int ct_nb_codes[4] = { 4, 8, 8, 8 };
  89. static const uint8_t * const 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 * const 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 * const ct_xlats[4] = {
  98. NULL, NULL, atrac3p_ct_huff_xlat1, NULL
  99. };
  100. static const int sf_nb_bits[8] = { 9, 9, 9, 9, 6, 6, 7, 7 };
  101. static const int sf_nb_codes[8] = { 64, 64, 64, 64, 16, 16, 16, 16 };
  102. static const uint8_t * const 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 * const 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 * const 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 * const 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 * const 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 * const 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 * const 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. if (pos > FF_ARRAY_ELEMS(chan->qu_wordlen)) {
  406. av_log(avctx, AV_LOG_ERROR, "Split point beyond array\n");
  407. pos = FF_ARRAY_ELEMS(chan->qu_wordlen);
  408. }
  409. for (i = chan->num_coded_vals; i < pos; i++)
  410. chan->qu_wordlen[i] = 1;
  411. }
  412. if (weight_idx)
  413. return add_wordlen_weights(ctx, chan, weight_idx, avctx);
  414. return 0;
  415. }
  416. /**
  417. * Decode scale factor indexes for each quant unit of a channel.
  418. *
  419. * @param[in] gb the GetBit context
  420. * @param[in,out] ctx ptr to the channel unit context
  421. * @param[in] ch_num channel to process
  422. * @param[in] avctx ptr to the AVCodecContext
  423. * @return result code: 0 = OK, otherwise - error code
  424. */
  425. static int decode_channel_sf_idx(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  426. int ch_num, AVCodecContext *avctx)
  427. {
  428. int i, weight_idx = 0, delta, diff, num_long_vals,
  429. delta_bits, min_val, vlc_sel, start_val;
  430. VLC *vlc_tab;
  431. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  432. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  433. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  434. case 0: /* coded using constant number of bits */
  435. for (i = 0; i < ctx->used_quant_units; i++)
  436. chan->qu_sf_idx[i] = get_bits(gb, 6);
  437. break;
  438. case 1:
  439. if (ch_num) {
  440. vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
  441. for (i = 0; i < ctx->used_quant_units; i++) {
  442. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  443. chan->qu_sf_idx[i] = (ref_chan->qu_sf_idx[i] + delta) & 0x3F;
  444. }
  445. } else {
  446. weight_idx = get_bits(gb, 2);
  447. if (weight_idx == 3) {
  448. UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
  449. num_long_vals = get_bits(gb, 5);
  450. delta_bits = get_bits(gb, 2);
  451. min_val = get_bits(gb, 4) - 7;
  452. for (i = 0; i < num_long_vals; i++)
  453. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
  454. get_bits(gb, 4) - 7) & 0x3F;
  455. /* all others are: min_val + delta */
  456. for (i = num_long_vals; i < ctx->used_quant_units; i++)
  457. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] + min_val +
  458. get_bitsz(gb, delta_bits)) & 0x3F;
  459. } else {
  460. num_long_vals = get_bits(gb, 5);
  461. delta_bits = get_bits(gb, 3);
  462. min_val = get_bits(gb, 6);
  463. if (num_long_vals > ctx->used_quant_units || delta_bits == 7) {
  464. av_log(avctx, AV_LOG_ERROR,
  465. "SF mode 1: invalid parameters!\n");
  466. return AVERROR_INVALIDDATA;
  467. }
  468. /* read full-precision SF indexes */
  469. for (i = 0; i < num_long_vals; i++)
  470. chan->qu_sf_idx[i] = get_bits(gb, 6);
  471. /* all others are: min_val + delta */
  472. for (i = num_long_vals; i < ctx->used_quant_units; i++)
  473. chan->qu_sf_idx[i] = (min_val +
  474. get_bitsz(gb, delta_bits)) & 0x3F;
  475. }
  476. }
  477. break;
  478. case 2:
  479. if (ch_num) {
  480. vlc_tab = &sf_vlc_tabs[get_bits(gb, 2)];
  481. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  482. chan->qu_sf_idx[0] = (ref_chan->qu_sf_idx[0] + delta) & 0x3F;
  483. for (i = 1; i < ctx->used_quant_units; i++) {
  484. diff = ref_chan->qu_sf_idx[i] - ref_chan->qu_sf_idx[i - 1];
  485. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  486. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + diff + delta) & 0x3F;
  487. }
  488. } else {
  489. vlc_tab = &sf_vlc_tabs[get_bits(gb, 2) + 4];
  490. UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
  491. for (i = 0; i < ctx->used_quant_units; i++) {
  492. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  493. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i] +
  494. sign_extend(delta, 4)) & 0x3F;
  495. }
  496. }
  497. break;
  498. case 3:
  499. if (ch_num) {
  500. /* copy coefficients from reference channel */
  501. for (i = 0; i < ctx->used_quant_units; i++)
  502. chan->qu_sf_idx[i] = ref_chan->qu_sf_idx[i];
  503. } else {
  504. weight_idx = get_bits(gb, 2);
  505. vlc_sel = get_bits(gb, 2);
  506. vlc_tab = &sf_vlc_tabs[vlc_sel];
  507. if (weight_idx == 3) {
  508. vlc_tab = &sf_vlc_tabs[vlc_sel + 4];
  509. UNPACK_SF_VQ_SHAPE(gb, chan->qu_sf_idx, ctx->used_quant_units);
  510. diff = (get_bits(gb, 4) + 56) & 0x3F;
  511. chan->qu_sf_idx[0] = (chan->qu_sf_idx[0] + diff) & 0x3F;
  512. for (i = 1; i < ctx->used_quant_units; i++) {
  513. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  514. diff = (diff + sign_extend(delta, 4)) & 0x3F;
  515. chan->qu_sf_idx[i] = (diff + chan->qu_sf_idx[i]) & 0x3F;
  516. }
  517. } else {
  518. /* 1st coefficient is coded directly */
  519. chan->qu_sf_idx[0] = get_bits(gb, 6);
  520. for (i = 1; i < ctx->used_quant_units; i++) {
  521. delta = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  522. chan->qu_sf_idx[i] = (chan->qu_sf_idx[i - 1] + delta) & 0x3F;
  523. }
  524. }
  525. }
  526. break;
  527. }
  528. if (weight_idx && weight_idx < 3)
  529. return subtract_sf_weights(ctx, chan, weight_idx, avctx);
  530. return 0;
  531. }
  532. /**
  533. * Decode word length information for each channel.
  534. *
  535. * @param[in] gb the GetBit context
  536. * @param[in,out] ctx ptr to the channel unit context
  537. * @param[in] num_channels number of channels to process
  538. * @param[in] avctx ptr to the AVCodecContext
  539. * @return result code: 0 = OK, otherwise - error code
  540. */
  541. static int decode_quant_wordlen(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  542. int num_channels, AVCodecContext *avctx)
  543. {
  544. int ch_num, i, ret;
  545. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  546. memset(ctx->channels[ch_num].qu_wordlen, 0,
  547. sizeof(ctx->channels[ch_num].qu_wordlen));
  548. if ((ret = decode_channel_wordlen(gb, ctx, ch_num, avctx)) < 0)
  549. return ret;
  550. }
  551. /* scan for last non-zero coeff in both channels and
  552. * set number of quant units having coded spectrum */
  553. for (i = ctx->num_quant_units - 1; i >= 0; i--)
  554. if (ctx->channels[0].qu_wordlen[i] ||
  555. (num_channels == 2 && ctx->channels[1].qu_wordlen[i]))
  556. break;
  557. ctx->used_quant_units = i + 1;
  558. return 0;
  559. }
  560. /**
  561. * Decode scale factor indexes for each channel.
  562. *
  563. * @param[in] gb the GetBit context
  564. * @param[in,out] ctx ptr to the channel unit context
  565. * @param[in] num_channels number of channels to process
  566. * @param[in] avctx ptr to the AVCodecContext
  567. * @return result code: 0 = OK, otherwise - error code
  568. */
  569. static int decode_scale_factors(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  570. int num_channels, AVCodecContext *avctx)
  571. {
  572. int ch_num, ret;
  573. if (!ctx->used_quant_units)
  574. return 0;
  575. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  576. memset(ctx->channels[ch_num].qu_sf_idx, 0,
  577. sizeof(ctx->channels[ch_num].qu_sf_idx));
  578. if ((ret = decode_channel_sf_idx(gb, ctx, ch_num, avctx)) < 0)
  579. return ret;
  580. }
  581. return 0;
  582. }
  583. /**
  584. * Decode number of code table values.
  585. *
  586. * @param[in] gb the GetBit context
  587. * @param[in,out] ctx ptr to the channel unit context
  588. * @param[in] avctx ptr to the AVCodecContext
  589. * @return result code: 0 = OK, otherwise - error code
  590. */
  591. static int get_num_ct_values(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  592. AVCodecContext *avctx)
  593. {
  594. int num_coded_vals;
  595. if (get_bits1(gb)) {
  596. num_coded_vals = get_bits(gb, 5);
  597. if (num_coded_vals > ctx->used_quant_units) {
  598. av_log(avctx, AV_LOG_ERROR,
  599. "Invalid number of code table indexes: %d!\n", num_coded_vals);
  600. return AVERROR_INVALIDDATA;
  601. }
  602. return num_coded_vals;
  603. } else
  604. return ctx->used_quant_units;
  605. }
  606. #define DEC_CT_IDX_COMMON(OP) \
  607. num_vals = get_num_ct_values(gb, ctx, avctx); \
  608. if (num_vals < 0) \
  609. return num_vals; \
  610. \
  611. for (i = 0; i < num_vals; i++) { \
  612. if (chan->qu_wordlen[i]) { \
  613. chan->qu_tab_idx[i] = OP; \
  614. } else if (ch_num && ref_chan->qu_wordlen[i]) \
  615. /* get clone master flag */ \
  616. chan->qu_tab_idx[i] = get_bits1(gb); \
  617. }
  618. #define CODING_DIRECT get_bits(gb, num_bits)
  619. #define CODING_VLC get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)
  620. #define CODING_VLC_DELTA \
  621. (!i) ? CODING_VLC \
  622. : (pred + get_vlc2(gb, delta_vlc->table, \
  623. delta_vlc->bits, 1)) & mask; \
  624. pred = chan->qu_tab_idx[i]
  625. #define CODING_VLC_DIFF \
  626. (ref_chan->qu_tab_idx[i] + \
  627. get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1)) & mask
  628. /**
  629. * Decode code table indexes for each quant unit of a channel.
  630. *
  631. * @param[in] gb the GetBit context
  632. * @param[in,out] ctx ptr to the channel unit context
  633. * @param[in] ch_num channel to process
  634. * @param[in] avctx ptr to the AVCodecContext
  635. * @return result code: 0 = OK, otherwise - error code
  636. */
  637. static int decode_channel_code_tab(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  638. int ch_num, AVCodecContext *avctx)
  639. {
  640. int i, num_vals, num_bits, pred;
  641. int mask = ctx->use_full_table ? 7 : 3; /* mask for modular arithmetic */
  642. VLC *vlc_tab, *delta_vlc;
  643. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  644. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  645. chan->table_type = get_bits1(gb);
  646. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  647. case 0: /* directly coded */
  648. num_bits = ctx->use_full_table + 2;
  649. DEC_CT_IDX_COMMON(CODING_DIRECT);
  650. break;
  651. case 1: /* entropy-coded */
  652. vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[1]
  653. : ct_vlc_tabs;
  654. DEC_CT_IDX_COMMON(CODING_VLC);
  655. break;
  656. case 2: /* entropy-coded delta */
  657. if (ctx->use_full_table) {
  658. vlc_tab = &ct_vlc_tabs[1];
  659. delta_vlc = &ct_vlc_tabs[2];
  660. } else {
  661. vlc_tab = ct_vlc_tabs;
  662. delta_vlc = ct_vlc_tabs;
  663. }
  664. pred = 0;
  665. DEC_CT_IDX_COMMON(CODING_VLC_DELTA);
  666. break;
  667. case 3: /* entropy-coded difference to master */
  668. if (ch_num) {
  669. vlc_tab = ctx->use_full_table ? &ct_vlc_tabs[3]
  670. : ct_vlc_tabs;
  671. DEC_CT_IDX_COMMON(CODING_VLC_DIFF);
  672. }
  673. break;
  674. }
  675. return 0;
  676. }
  677. /**
  678. * Decode code table indexes for each channel.
  679. *
  680. * @param[in] gb the GetBit context
  681. * @param[in,out] ctx ptr to the channel unit context
  682. * @param[in] num_channels number of channels to process
  683. * @param[in] avctx ptr to the AVCodecContext
  684. * @return result code: 0 = OK, otherwise - error code
  685. */
  686. static int decode_code_table_indexes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  687. int num_channels, AVCodecContext *avctx)
  688. {
  689. int ch_num, ret;
  690. if (!ctx->used_quant_units)
  691. return 0;
  692. ctx->use_full_table = get_bits1(gb);
  693. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  694. memset(ctx->channels[ch_num].qu_tab_idx, 0,
  695. sizeof(ctx->channels[ch_num].qu_tab_idx));
  696. if ((ret = decode_channel_code_tab(gb, ctx, ch_num, avctx)) < 0)
  697. return ret;
  698. }
  699. return 0;
  700. }
  701. /**
  702. * Decode huffman-coded spectral lines for a given quant unit.
  703. *
  704. * This is a generalized version for all known coding modes.
  705. * Its speed can be improved by creating separate functions for each mode.
  706. *
  707. * @param[in] gb the GetBit context
  708. * @param[in] tab code table telling how to decode spectral lines
  709. * @param[in] vlc_tab ptr to the huffman table associated with the code table
  710. * @param[out] out pointer to buffer where decoded data should be stored
  711. * @param[in] num_specs number of spectral lines to decode
  712. */
  713. static void decode_qu_spectra(GetBitContext *gb, const Atrac3pSpecCodeTab *tab,
  714. VLC *vlc_tab, int16_t *out, const int num_specs)
  715. {
  716. int i, j, pos, cf;
  717. int group_size = tab->group_size;
  718. int num_coeffs = tab->num_coeffs;
  719. int bits = tab->bits;
  720. int is_signed = tab->is_signed;
  721. unsigned val;
  722. for (pos = 0; pos < num_specs;) {
  723. if (group_size == 1 || get_bits1(gb)) {
  724. for (j = 0; j < group_size; j++) {
  725. val = get_vlc2(gb, vlc_tab->table, vlc_tab->bits, 1);
  726. for (i = 0; i < num_coeffs; i++) {
  727. cf = av_mod_uintp2(val, bits);
  728. if (is_signed)
  729. cf = sign_extend(cf, bits);
  730. else if (cf && get_bits1(gb))
  731. cf = -cf;
  732. out[pos++] = cf;
  733. val >>= bits;
  734. }
  735. }
  736. } else /* group skipped */
  737. pos += group_size * num_coeffs;
  738. }
  739. }
  740. /**
  741. * Decode huffman-coded IMDCT spectrum for all channels.
  742. *
  743. * @param[in] gb the GetBit context
  744. * @param[in,out] ctx ptr to the channel unit context
  745. * @param[in] num_channels number of channels to process
  746. * @param[in] avctx ptr to the AVCodecContext
  747. */
  748. static void decode_spectrum(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  749. int num_channels, AVCodecContext *avctx)
  750. {
  751. int i, ch_num, qu, wordlen, codetab, tab_index, num_specs;
  752. const Atrac3pSpecCodeTab *tab;
  753. Atrac3pChanParams *chan;
  754. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  755. chan = &ctx->channels[ch_num];
  756. memset(chan->spectrum, 0, sizeof(chan->spectrum));
  757. /* set power compensation level to disabled */
  758. memset(chan->power_levs, ATRAC3P_POWER_COMP_OFF, sizeof(chan->power_levs));
  759. for (qu = 0; qu < ctx->used_quant_units; qu++) {
  760. num_specs = ff_atrac3p_qu_to_spec_pos[qu + 1] -
  761. ff_atrac3p_qu_to_spec_pos[qu];
  762. wordlen = chan->qu_wordlen[qu];
  763. codetab = chan->qu_tab_idx[qu];
  764. if (wordlen) {
  765. if (!ctx->use_full_table)
  766. codetab = atrac3p_ct_restricted_to_full[chan->table_type][wordlen - 1][codetab];
  767. tab_index = (chan->table_type * 8 + codetab) * 7 + wordlen - 1;
  768. tab = &atrac3p_spectra_tabs[tab_index];
  769. /* this allows reusing VLC tables */
  770. if (tab->redirect >= 0)
  771. tab_index = tab->redirect;
  772. decode_qu_spectra(gb, tab, &spec_vlc_tabs[tab_index],
  773. &chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
  774. num_specs);
  775. } else if (ch_num && ctx->channels[0].qu_wordlen[qu] && !codetab) {
  776. /* copy coefficients from master */
  777. memcpy(&chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
  778. &ctx->channels[0].spectrum[ff_atrac3p_qu_to_spec_pos[qu]],
  779. num_specs *
  780. sizeof(chan->spectrum[ff_atrac3p_qu_to_spec_pos[qu]]));
  781. chan->qu_wordlen[qu] = ctx->channels[0].qu_wordlen[qu];
  782. }
  783. }
  784. /* Power compensation levels only present in the bitstream
  785. * if there are more than 2 quant units. The lowest two units
  786. * correspond to the frequencies 0...351 Hz, whose shouldn't
  787. * be affected by the power compensation. */
  788. if (ctx->used_quant_units > 2) {
  789. num_specs = atrac3p_subband_to_num_powgrps[ctx->num_coded_subbands - 1];
  790. for (i = 0; i < num_specs; i++)
  791. chan->power_levs[i] = get_bits(gb, 4);
  792. }
  793. }
  794. }
  795. /**
  796. * Retrieve specified amount of flag bits from the input bitstream.
  797. * The data can be shortened in the case of the following two common conditions:
  798. * if all bits are zero then only one signal bit = 0 will be stored,
  799. * if all bits are ones then two signal bits = 1,0 will be stored.
  800. * Otherwise, all necessary bits will be directly stored
  801. * prefixed by two signal bits = 1,1.
  802. *
  803. * @param[in] gb ptr to the GetBitContext
  804. * @param[out] out where to place decoded flags
  805. * @param[in] num_flags number of flags to process
  806. * @return: 0 = all flag bits are zero, 1 = there is at least one non-zero flag bit
  807. */
  808. static int get_subband_flags(GetBitContext *gb, uint8_t *out, int num_flags)
  809. {
  810. int i, result;
  811. memset(out, 0, num_flags);
  812. result = get_bits1(gb);
  813. if (result) {
  814. if (get_bits1(gb))
  815. for (i = 0; i < num_flags; i++)
  816. out[i] = get_bits1(gb);
  817. else
  818. memset(out, 1, num_flags);
  819. }
  820. return result;
  821. }
  822. /**
  823. * Decode mdct window shape flags for all channels.
  824. *
  825. * @param[in] gb the GetBit context
  826. * @param[in,out] ctx ptr to the channel unit context
  827. * @param[in] num_channels number of channels to process
  828. */
  829. static void decode_window_shape(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  830. int num_channels)
  831. {
  832. int ch_num;
  833. for (ch_num = 0; ch_num < num_channels; ch_num++)
  834. get_subband_flags(gb, ctx->channels[ch_num].wnd_shape,
  835. ctx->num_subbands);
  836. }
  837. /**
  838. * Decode number of gain control points.
  839. *
  840. * @param[in] gb the GetBit context
  841. * @param[in,out] ctx ptr to the channel unit context
  842. * @param[in] ch_num channel to process
  843. * @param[in] coded_subbands number of subbands to process
  844. * @return result code: 0 = OK, otherwise - error code
  845. */
  846. static int decode_gainc_npoints(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  847. int ch_num, int coded_subbands)
  848. {
  849. int i, delta, delta_bits, min_val;
  850. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  851. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  852. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  853. case 0: /* fixed-length coding */
  854. for (i = 0; i < coded_subbands; i++)
  855. chan->gain_data[i].num_points = get_bits(gb, 3);
  856. break;
  857. case 1: /* variable-length coding */
  858. for (i = 0; i < coded_subbands; i++)
  859. chan->gain_data[i].num_points =
  860. get_vlc2(gb, gain_vlc_tabs[0].table,
  861. gain_vlc_tabs[0].bits, 1);
  862. break;
  863. case 2:
  864. if (ch_num) { /* VLC modulo delta to master channel */
  865. for (i = 0; i < coded_subbands; i++) {
  866. delta = get_vlc2(gb, gain_vlc_tabs[1].table,
  867. gain_vlc_tabs[1].bits, 1);
  868. chan->gain_data[i].num_points =
  869. (ref_chan->gain_data[i].num_points + delta) & 7;
  870. }
  871. } else { /* VLC modulo delta to previous */
  872. chan->gain_data[0].num_points =
  873. get_vlc2(gb, gain_vlc_tabs[0].table,
  874. gain_vlc_tabs[0].bits, 1);
  875. for (i = 1; i < coded_subbands; i++) {
  876. delta = get_vlc2(gb, gain_vlc_tabs[1].table,
  877. gain_vlc_tabs[1].bits, 1);
  878. chan->gain_data[i].num_points =
  879. (chan->gain_data[i - 1].num_points + delta) & 7;
  880. }
  881. }
  882. break;
  883. case 3:
  884. if (ch_num) { /* copy data from master channel */
  885. for (i = 0; i < coded_subbands; i++)
  886. chan->gain_data[i].num_points =
  887. ref_chan->gain_data[i].num_points;
  888. } else { /* shorter delta to min */
  889. delta_bits = get_bits(gb, 2);
  890. min_val = get_bits(gb, 3);
  891. for (i = 0; i < coded_subbands; i++) {
  892. chan->gain_data[i].num_points = min_val + get_bitsz(gb, delta_bits);
  893. if (chan->gain_data[i].num_points > 7)
  894. return AVERROR_INVALIDDATA;
  895. }
  896. }
  897. }
  898. return 0;
  899. }
  900. /**
  901. * Implements coding mode 3 (slave) for gain compensation levels.
  902. *
  903. * @param[out] dst ptr to the output array
  904. * @param[in] ref ptr to the reference channel
  905. */
  906. static inline void gainc_level_mode3s(AtracGainInfo *dst, AtracGainInfo *ref)
  907. {
  908. int i;
  909. for (i = 0; i < dst->num_points; i++)
  910. dst->lev_code[i] = (i >= ref->num_points) ? 7 : ref->lev_code[i];
  911. }
  912. /**
  913. * Implements coding mode 1 (master) for gain compensation levels.
  914. *
  915. * @param[in] gb the GetBit context
  916. * @param[in] ctx ptr to the channel unit context
  917. * @param[out] dst ptr to the output array
  918. */
  919. static inline void gainc_level_mode1m(GetBitContext *gb,
  920. Atrac3pChanUnitCtx *ctx,
  921. AtracGainInfo *dst)
  922. {
  923. int i, delta;
  924. if (dst->num_points > 0)
  925. dst->lev_code[0] = get_vlc2(gb, gain_vlc_tabs[2].table,
  926. gain_vlc_tabs[2].bits, 1);
  927. for (i = 1; i < dst->num_points; i++) {
  928. delta = get_vlc2(gb, gain_vlc_tabs[3].table,
  929. gain_vlc_tabs[3].bits, 1);
  930. dst->lev_code[i] = (dst->lev_code[i - 1] + delta) & 0xF;
  931. }
  932. }
  933. /**
  934. * Decode level code for each gain control point.
  935. *
  936. * @param[in] gb the GetBit context
  937. * @param[in,out] ctx ptr to the channel unit context
  938. * @param[in] ch_num channel to process
  939. * @param[in] coded_subbands number of subbands to process
  940. * @return result code: 0 = OK, otherwise - error code
  941. */
  942. static int decode_gainc_levels(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  943. int ch_num, int coded_subbands)
  944. {
  945. int sb, i, delta, delta_bits, min_val, pred;
  946. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  947. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  948. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  949. case 0: /* fixed-length coding */
  950. for (sb = 0; sb < coded_subbands; sb++)
  951. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  952. chan->gain_data[sb].lev_code[i] = get_bits(gb, 4);
  953. break;
  954. case 1:
  955. if (ch_num) { /* VLC modulo delta to master channel */
  956. for (sb = 0; sb < coded_subbands; sb++)
  957. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  958. delta = get_vlc2(gb, gain_vlc_tabs[5].table,
  959. gain_vlc_tabs[5].bits, 1);
  960. pred = (i >= ref_chan->gain_data[sb].num_points)
  961. ? 7 : ref_chan->gain_data[sb].lev_code[i];
  962. chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
  963. }
  964. } else { /* VLC modulo delta to previous */
  965. for (sb = 0; sb < coded_subbands; sb++)
  966. gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
  967. }
  968. break;
  969. case 2:
  970. if (ch_num) { /* VLC modulo delta to previous or clone master */
  971. for (sb = 0; sb < coded_subbands; sb++)
  972. if (chan->gain_data[sb].num_points > 0) {
  973. if (get_bits1(gb))
  974. gainc_level_mode1m(gb, ctx, &chan->gain_data[sb]);
  975. else
  976. gainc_level_mode3s(&chan->gain_data[sb],
  977. &ref_chan->gain_data[sb]);
  978. }
  979. } else { /* VLC modulo delta to lev_codes of previous subband */
  980. if (chan->gain_data[0].num_points > 0)
  981. gainc_level_mode1m(gb, ctx, &chan->gain_data[0]);
  982. for (sb = 1; sb < coded_subbands; sb++)
  983. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  984. delta = get_vlc2(gb, gain_vlc_tabs[4].table,
  985. gain_vlc_tabs[4].bits, 1);
  986. pred = (i >= chan->gain_data[sb - 1].num_points)
  987. ? 7 : chan->gain_data[sb - 1].lev_code[i];
  988. chan->gain_data[sb].lev_code[i] = (pred + delta) & 0xF;
  989. }
  990. }
  991. break;
  992. case 3:
  993. if (ch_num) { /* clone master */
  994. for (sb = 0; sb < coded_subbands; sb++)
  995. gainc_level_mode3s(&chan->gain_data[sb],
  996. &ref_chan->gain_data[sb]);
  997. } else { /* shorter delta to min */
  998. delta_bits = get_bits(gb, 2);
  999. min_val = get_bits(gb, 4);
  1000. for (sb = 0; sb < coded_subbands; sb++)
  1001. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  1002. chan->gain_data[sb].lev_code[i] = min_val + get_bitsz(gb, delta_bits);
  1003. if (chan->gain_data[sb].lev_code[i] > 15)
  1004. return AVERROR_INVALIDDATA;
  1005. }
  1006. }
  1007. break;
  1008. }
  1009. return 0;
  1010. }
  1011. /**
  1012. * Implements coding mode 0 for gain compensation locations.
  1013. *
  1014. * @param[in] gb the GetBit context
  1015. * @param[in] ctx ptr to the channel unit context
  1016. * @param[out] dst ptr to the output array
  1017. * @param[in] pos position of the value to be processed
  1018. */
  1019. static inline void gainc_loc_mode0(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1020. AtracGainInfo *dst, int pos)
  1021. {
  1022. int delta_bits;
  1023. if (!pos || dst->loc_code[pos - 1] < 15)
  1024. dst->loc_code[pos] = get_bits(gb, 5);
  1025. else if (dst->loc_code[pos - 1] >= 30)
  1026. dst->loc_code[pos] = 31;
  1027. else {
  1028. delta_bits = av_log2(30 - dst->loc_code[pos - 1]) + 1;
  1029. dst->loc_code[pos] = dst->loc_code[pos - 1] +
  1030. get_bits(gb, delta_bits) + 1;
  1031. }
  1032. }
  1033. /**
  1034. * Implements coding mode 1 for gain compensation locations.
  1035. *
  1036. * @param[in] gb the GetBit context
  1037. * @param[in] ctx ptr to the channel unit context
  1038. * @param[out] dst ptr to the output array
  1039. */
  1040. static inline void gainc_loc_mode1(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1041. AtracGainInfo *dst)
  1042. {
  1043. int i;
  1044. VLC *tab;
  1045. if (dst->num_points > 0) {
  1046. /* 1st coefficient is stored directly */
  1047. dst->loc_code[0] = get_bits(gb, 5);
  1048. for (i = 1; i < dst->num_points; i++) {
  1049. /* switch VLC according to the curve direction
  1050. * (ascending/descending) */
  1051. tab = (dst->lev_code[i] <= dst->lev_code[i - 1])
  1052. ? &gain_vlc_tabs[7]
  1053. : &gain_vlc_tabs[9];
  1054. dst->loc_code[i] = dst->loc_code[i - 1] +
  1055. get_vlc2(gb, tab->table, tab->bits, 1);
  1056. }
  1057. }
  1058. }
  1059. /**
  1060. * Decode location code for each gain control point.
  1061. *
  1062. * @param[in] gb the GetBit context
  1063. * @param[in,out] ctx ptr to the channel unit context
  1064. * @param[in] ch_num channel to process
  1065. * @param[in] coded_subbands number of subbands to process
  1066. * @param[in] avctx ptr to the AVCodecContext
  1067. * @return result code: 0 = OK, otherwise - error code
  1068. */
  1069. static int decode_gainc_loc_codes(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1070. int ch_num, int coded_subbands,
  1071. AVCodecContext *avctx)
  1072. {
  1073. int sb, i, delta, delta_bits, min_val, pred, more_than_ref;
  1074. AtracGainInfo *dst, *ref;
  1075. VLC *tab;
  1076. Atrac3pChanParams *chan = &ctx->channels[ch_num];
  1077. Atrac3pChanParams *ref_chan = &ctx->channels[0];
  1078. switch (get_bits(gb, 2)) { /* switch according to coding mode */
  1079. case 0: /* sequence of numbers in ascending order */
  1080. for (sb = 0; sb < coded_subbands; sb++)
  1081. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  1082. gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
  1083. break;
  1084. case 1:
  1085. if (ch_num) {
  1086. for (sb = 0; sb < coded_subbands; sb++) {
  1087. if (chan->gain_data[sb].num_points <= 0)
  1088. continue;
  1089. dst = &chan->gain_data[sb];
  1090. ref = &ref_chan->gain_data[sb];
  1091. /* 1st value is vlc-coded modulo delta to master */
  1092. delta = get_vlc2(gb, gain_vlc_tabs[10].table,
  1093. gain_vlc_tabs[10].bits, 1);
  1094. pred = ref->num_points > 0 ? ref->loc_code[0] : 0;
  1095. dst->loc_code[0] = (pred + delta) & 0x1F;
  1096. for (i = 1; i < dst->num_points; i++) {
  1097. more_than_ref = i >= ref->num_points;
  1098. if (dst->lev_code[i] > dst->lev_code[i - 1]) {
  1099. /* ascending curve */
  1100. if (more_than_ref) {
  1101. delta =
  1102. get_vlc2(gb, gain_vlc_tabs[9].table,
  1103. gain_vlc_tabs[9].bits, 1);
  1104. dst->loc_code[i] = dst->loc_code[i - 1] + delta;
  1105. } else {
  1106. if (get_bits1(gb))
  1107. gainc_loc_mode0(gb, ctx, dst, i); // direct coding
  1108. else
  1109. dst->loc_code[i] = ref->loc_code[i]; // clone master
  1110. }
  1111. } else { /* descending curve */
  1112. tab = more_than_ref ? &gain_vlc_tabs[7]
  1113. : &gain_vlc_tabs[10];
  1114. delta = get_vlc2(gb, tab->table, tab->bits, 1);
  1115. if (more_than_ref)
  1116. dst->loc_code[i] = dst->loc_code[i - 1] + delta;
  1117. else
  1118. dst->loc_code[i] = (ref->loc_code[i] + delta) & 0x1F;
  1119. }
  1120. }
  1121. }
  1122. } else /* VLC delta to previous */
  1123. for (sb = 0; sb < coded_subbands; sb++)
  1124. gainc_loc_mode1(gb, ctx, &chan->gain_data[sb]);
  1125. break;
  1126. case 2:
  1127. if (ch_num) {
  1128. for (sb = 0; sb < coded_subbands; sb++) {
  1129. if (chan->gain_data[sb].num_points <= 0)
  1130. continue;
  1131. dst = &chan->gain_data[sb];
  1132. ref = &ref_chan->gain_data[sb];
  1133. if (dst->num_points > ref->num_points || get_bits1(gb))
  1134. gainc_loc_mode1(gb, ctx, dst);
  1135. else /* clone master for the whole subband */
  1136. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  1137. dst->loc_code[i] = ref->loc_code[i];
  1138. }
  1139. } else {
  1140. /* data for the first subband is coded directly */
  1141. for (i = 0; i < chan->gain_data[0].num_points; i++)
  1142. gainc_loc_mode0(gb, ctx, &chan->gain_data[0], i);
  1143. for (sb = 1; sb < coded_subbands; sb++) {
  1144. if (chan->gain_data[sb].num_points <= 0)
  1145. continue;
  1146. dst = &chan->gain_data[sb];
  1147. /* 1st value is vlc-coded modulo delta to the corresponding
  1148. * value of the previous subband if any or zero */
  1149. delta = get_vlc2(gb, gain_vlc_tabs[6].table,
  1150. gain_vlc_tabs[6].bits, 1);
  1151. pred = dst[-1].num_points > 0
  1152. ? dst[-1].loc_code[0] : 0;
  1153. dst->loc_code[0] = (pred + delta) & 0x1F;
  1154. for (i = 1; i < dst->num_points; i++) {
  1155. more_than_ref = i >= dst[-1].num_points;
  1156. /* Select VLC table according to curve direction and
  1157. * presence of prediction. */
  1158. tab = &gain_vlc_tabs[(dst->lev_code[i] > dst->lev_code[i - 1]) *
  1159. 2 + more_than_ref + 6];
  1160. delta = get_vlc2(gb, tab->table, tab->bits, 1);
  1161. if (more_than_ref)
  1162. dst->loc_code[i] = dst->loc_code[i - 1] + delta;
  1163. else
  1164. dst->loc_code[i] = (dst[-1].loc_code[i] + delta) & 0x1F;
  1165. }
  1166. }
  1167. }
  1168. break;
  1169. case 3:
  1170. if (ch_num) { /* clone master or direct or direct coding */
  1171. for (sb = 0; sb < coded_subbands; sb++)
  1172. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  1173. if (i >= ref_chan->gain_data[sb].num_points)
  1174. gainc_loc_mode0(gb, ctx, &chan->gain_data[sb], i);
  1175. else
  1176. chan->gain_data[sb].loc_code[i] =
  1177. ref_chan->gain_data[sb].loc_code[i];
  1178. }
  1179. } else { /* shorter delta to min */
  1180. delta_bits = get_bits(gb, 2) + 1;
  1181. min_val = get_bits(gb, 5);
  1182. for (sb = 0; sb < coded_subbands; sb++)
  1183. for (i = 0; i < chan->gain_data[sb].num_points; i++)
  1184. chan->gain_data[sb].loc_code[i] = min_val + i +
  1185. get_bits(gb, delta_bits);
  1186. }
  1187. break;
  1188. }
  1189. /* Validate decoded information */
  1190. for (sb = 0; sb < coded_subbands; sb++) {
  1191. dst = &chan->gain_data[sb];
  1192. for (i = 0; i < chan->gain_data[sb].num_points; i++) {
  1193. if (dst->loc_code[i] < 0 || dst->loc_code[i] > 31 ||
  1194. (i && dst->loc_code[i] <= dst->loc_code[i - 1])) {
  1195. av_log(avctx, AV_LOG_ERROR,
  1196. "Invalid gain location: ch=%d, sb=%d, pos=%d, val=%d\n",
  1197. ch_num, sb, i, dst->loc_code[i]);
  1198. return AVERROR_INVALIDDATA;
  1199. }
  1200. }
  1201. }
  1202. return 0;
  1203. }
  1204. /**
  1205. * Decode gain control data for all channels.
  1206. *
  1207. * @param[in] gb the GetBit context
  1208. * @param[in,out] ctx ptr to the channel unit context
  1209. * @param[in] num_channels number of channels to process
  1210. * @param[in] avctx ptr to the AVCodecContext
  1211. * @return result code: 0 = OK, otherwise - error code
  1212. */
  1213. static int decode_gainc_data(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1214. int num_channels, AVCodecContext *avctx)
  1215. {
  1216. int ch_num, coded_subbands, sb, ret;
  1217. for (ch_num = 0; ch_num < num_channels; ch_num++) {
  1218. memset(ctx->channels[ch_num].gain_data, 0,
  1219. sizeof(*ctx->channels[ch_num].gain_data) * ATRAC3P_SUBBANDS);
  1220. if (get_bits1(gb)) { /* gain control data present? */
  1221. coded_subbands = get_bits(gb, 4) + 1;
  1222. if (get_bits1(gb)) /* is high band gain data replication on? */
  1223. ctx->channels[ch_num].num_gain_subbands = get_bits(gb, 4) + 1;
  1224. else
  1225. ctx->channels[ch_num].num_gain_subbands = coded_subbands;
  1226. if ((ret = decode_gainc_npoints(gb, ctx, ch_num, coded_subbands)) < 0 ||
  1227. (ret = decode_gainc_levels(gb, ctx, ch_num, coded_subbands)) < 0 ||
  1228. (ret = decode_gainc_loc_codes(gb, ctx, ch_num, coded_subbands, avctx)) < 0)
  1229. return ret;
  1230. if (coded_subbands > 0) { /* propagate gain data if requested */
  1231. for (sb = coded_subbands; sb < ctx->channels[ch_num].num_gain_subbands; sb++)
  1232. ctx->channels[ch_num].gain_data[sb] =
  1233. ctx->channels[ch_num].gain_data[sb - 1];
  1234. }
  1235. } else {
  1236. ctx->channels[ch_num].num_gain_subbands = 0;
  1237. }
  1238. }
  1239. return 0;
  1240. }
  1241. /**
  1242. * Decode envelope for all tones of a channel.
  1243. *
  1244. * @param[in] gb the GetBit context
  1245. * @param[in,out] ctx ptr to the channel unit context
  1246. * @param[in] ch_num channel to process
  1247. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1248. * 1 - tone data present
  1249. */
  1250. static void decode_tones_envelope(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1251. int ch_num, int band_has_tones[])
  1252. {
  1253. int sb;
  1254. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1255. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1256. if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
  1257. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1258. if (!band_has_tones[sb])
  1259. continue;
  1260. dst[sb].pend_env.has_start_point = get_bits1(gb);
  1261. dst[sb].pend_env.start_pos = dst[sb].pend_env.has_start_point
  1262. ? get_bits(gb, 5) : -1;
  1263. dst[sb].pend_env.has_stop_point = get_bits1(gb);
  1264. dst[sb].pend_env.stop_pos = dst[sb].pend_env.has_stop_point
  1265. ? get_bits(gb, 5) : 32;
  1266. }
  1267. } else { /* mode 1(slave only): copy master */
  1268. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1269. if (!band_has_tones[sb])
  1270. continue;
  1271. dst[sb].pend_env.has_start_point = ref[sb].pend_env.has_start_point;
  1272. dst[sb].pend_env.has_stop_point = ref[sb].pend_env.has_stop_point;
  1273. dst[sb].pend_env.start_pos = ref[sb].pend_env.start_pos;
  1274. dst[sb].pend_env.stop_pos = ref[sb].pend_env.stop_pos;
  1275. }
  1276. }
  1277. }
  1278. /**
  1279. * Decode number of tones for each subband of a channel.
  1280. *
  1281. * @param[in] gb the GetBit context
  1282. * @param[in,out] ctx ptr to the channel unit context
  1283. * @param[in] ch_num channel to process
  1284. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1285. * 1 - tone data present
  1286. * @param[in] avctx ptr to the AVCodecContext
  1287. * @return result code: 0 = OK, otherwise - error code
  1288. */
  1289. static int decode_band_numwavs(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1290. int ch_num, int band_has_tones[],
  1291. AVCodecContext *avctx)
  1292. {
  1293. int mode, sb, delta;
  1294. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1295. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1296. mode = get_bits(gb, ch_num + 1);
  1297. switch (mode) {
  1298. case 0: /** fixed-length coding */
  1299. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1300. if (band_has_tones[sb])
  1301. dst[sb].num_wavs = get_bits(gb, 4);
  1302. break;
  1303. case 1: /** variable-length coding */
  1304. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1305. if (band_has_tones[sb])
  1306. dst[sb].num_wavs =
  1307. get_vlc2(gb, tone_vlc_tabs[1].table,
  1308. tone_vlc_tabs[1].bits, 1);
  1309. break;
  1310. case 2: /** VLC modulo delta to master (slave only) */
  1311. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1312. if (band_has_tones[sb]) {
  1313. delta = get_vlc2(gb, tone_vlc_tabs[2].table,
  1314. tone_vlc_tabs[2].bits, 1);
  1315. delta = sign_extend(delta, 3);
  1316. dst[sb].num_wavs = (ref[sb].num_wavs + delta) & 0xF;
  1317. }
  1318. break;
  1319. case 3: /** copy master (slave only) */
  1320. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1321. if (band_has_tones[sb])
  1322. dst[sb].num_wavs = ref[sb].num_wavs;
  1323. break;
  1324. }
  1325. /** initialize start tone index for each subband */
  1326. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++)
  1327. if (band_has_tones[sb]) {
  1328. if (ctx->waves_info->tones_index + dst[sb].num_wavs > 48) {
  1329. av_log(avctx, AV_LOG_ERROR,
  1330. "Too many tones: %d (max. 48), frame: %d!\n",
  1331. ctx->waves_info->tones_index + dst[sb].num_wavs,
  1332. avctx->frame_number);
  1333. return AVERROR_INVALIDDATA;
  1334. }
  1335. dst[sb].start_index = ctx->waves_info->tones_index;
  1336. ctx->waves_info->tones_index += dst[sb].num_wavs;
  1337. }
  1338. return 0;
  1339. }
  1340. /**
  1341. * Decode frequency information for each subband of a channel.
  1342. *
  1343. * @param[in] gb the GetBit context
  1344. * @param[in,out] ctx ptr to the channel unit context
  1345. * @param[in] ch_num channel to process
  1346. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1347. * 1 - tone data present
  1348. */
  1349. static void decode_tones_frequency(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1350. int ch_num, int band_has_tones[])
  1351. {
  1352. int sb, i, direction, nbits, pred, delta;
  1353. Atrac3pWaveParam *iwav, *owav;
  1354. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1355. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1356. if (!ch_num || !get_bits1(gb)) { /* mode 0: fixed-length coding */
  1357. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1358. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1359. continue;
  1360. iwav = &ctx->waves_info->waves[dst[sb].start_index];
  1361. direction = (dst[sb].num_wavs > 1) ? get_bits1(gb) : 0;
  1362. if (direction) { /** packed numbers in descending order */
  1363. if (dst[sb].num_wavs)
  1364. iwav[dst[sb].num_wavs - 1].freq_index = get_bits(gb, 10);
  1365. for (i = dst[sb].num_wavs - 2; i >= 0 ; i--) {
  1366. nbits = av_log2(iwav[i+1].freq_index) + 1;
  1367. iwav[i].freq_index = get_bits(gb, nbits);
  1368. }
  1369. } else { /** packed numbers in ascending order */
  1370. for (i = 0; i < dst[sb].num_wavs; i++) {
  1371. if (!i || iwav[i - 1].freq_index < 512)
  1372. iwav[i].freq_index = get_bits(gb, 10);
  1373. else {
  1374. nbits = av_log2(1023 - iwav[i - 1].freq_index) + 1;
  1375. iwav[i].freq_index = get_bits(gb, nbits) +
  1376. 1024 - (1 << nbits);
  1377. }
  1378. }
  1379. }
  1380. }
  1381. } else { /* mode 1: VLC modulo delta to master (slave only) */
  1382. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1383. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1384. continue;
  1385. iwav = &ctx->waves_info->waves[ref[sb].start_index];
  1386. owav = &ctx->waves_info->waves[dst[sb].start_index];
  1387. for (i = 0; i < dst[sb].num_wavs; i++) {
  1388. delta = get_vlc2(gb, tone_vlc_tabs[6].table,
  1389. tone_vlc_tabs[6].bits, 1);
  1390. delta = sign_extend(delta, 8);
  1391. pred = (i < ref[sb].num_wavs) ? iwav[i].freq_index :
  1392. (ref[sb].num_wavs ? iwav[ref[sb].num_wavs - 1].freq_index : 0);
  1393. owav[i].freq_index = (pred + delta) & 0x3FF;
  1394. }
  1395. }
  1396. }
  1397. }
  1398. /**
  1399. * Decode amplitude information for each subband of a channel.
  1400. *
  1401. * @param[in] gb the GetBit context
  1402. * @param[in,out] ctx ptr to the channel unit context
  1403. * @param[in] ch_num channel to process
  1404. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1405. * 1 - tone data present
  1406. */
  1407. static void decode_tones_amplitude(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1408. int ch_num, int band_has_tones[])
  1409. {
  1410. int mode, sb, j, i, diff, maxdiff, fi, delta, pred;
  1411. Atrac3pWaveParam *wsrc, *wref;
  1412. int refwaves[48] = { 0 };
  1413. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1414. Atrac3pWavesData *ref = ctx->channels[0].tones_info;
  1415. if (ch_num) {
  1416. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1417. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1418. continue;
  1419. wsrc = &ctx->waves_info->waves[dst[sb].start_index];
  1420. wref = &ctx->waves_info->waves[ref[sb].start_index];
  1421. for (j = 0; j < dst[sb].num_wavs; j++) {
  1422. for (i = 0, fi = 0, maxdiff = 1024; i < ref[sb].num_wavs; i++) {
  1423. diff = FFABS(wsrc[j].freq_index - wref[i].freq_index);
  1424. if (diff < maxdiff) {
  1425. maxdiff = diff;
  1426. fi = i;
  1427. }
  1428. }
  1429. if (maxdiff < 8)
  1430. refwaves[dst[sb].start_index + j] = fi + ref[sb].start_index;
  1431. else if (j < ref[sb].num_wavs)
  1432. refwaves[dst[sb].start_index + j] = j + ref[sb].start_index;
  1433. else
  1434. refwaves[dst[sb].start_index + j] = -1;
  1435. }
  1436. }
  1437. }
  1438. mode = get_bits(gb, ch_num + 1);
  1439. switch (mode) {
  1440. case 0: /** fixed-length coding */
  1441. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1442. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1443. continue;
  1444. if (ctx->waves_info->amplitude_mode)
  1445. for (i = 0; i < dst[sb].num_wavs; i++)
  1446. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = get_bits(gb, 6);
  1447. else
  1448. ctx->waves_info->waves[dst[sb].start_index].amp_sf = get_bits(gb, 6);
  1449. }
  1450. break;
  1451. case 1: /** min + VLC delta */
  1452. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1453. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1454. continue;
  1455. if (ctx->waves_info->amplitude_mode)
  1456. for (i = 0; i < dst[sb].num_wavs; i++)
  1457. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
  1458. get_vlc2(gb, tone_vlc_tabs[3].table,
  1459. tone_vlc_tabs[3].bits, 1) + 20;
  1460. else
  1461. ctx->waves_info->waves[dst[sb].start_index].amp_sf =
  1462. get_vlc2(gb, tone_vlc_tabs[4].table,
  1463. tone_vlc_tabs[4].bits, 1) + 24;
  1464. }
  1465. break;
  1466. case 2: /** VLC modulo delta to master (slave only) */
  1467. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1468. if (!band_has_tones[sb] || !dst[sb].num_wavs)
  1469. continue;
  1470. for (i = 0; i < dst[sb].num_wavs; i++) {
  1471. delta = get_vlc2(gb, tone_vlc_tabs[5].table,
  1472. tone_vlc_tabs[5].bits, 1);
  1473. delta = sign_extend(delta, 5);
  1474. pred = refwaves[dst[sb].start_index + i] >= 0 ?
  1475. ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf : 34;
  1476. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf = (pred + delta) & 0x3F;
  1477. }
  1478. }
  1479. break;
  1480. case 3: /** clone master (slave only) */
  1481. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1482. if (!band_has_tones[sb])
  1483. continue;
  1484. for (i = 0; i < dst[sb].num_wavs; i++)
  1485. ctx->waves_info->waves[dst[sb].start_index + i].amp_sf =
  1486. refwaves[dst[sb].start_index + i] >= 0
  1487. ? ctx->waves_info->waves[refwaves[dst[sb].start_index + i]].amp_sf
  1488. : 32;
  1489. }
  1490. break;
  1491. }
  1492. }
  1493. /**
  1494. * Decode phase information for each subband of a channel.
  1495. *
  1496. * @param[in] gb the GetBit context
  1497. * @param[in,out] ctx ptr to the channel unit context
  1498. * @param[in] ch_num channel to process
  1499. * @param[in] band_has_tones ptr to an array of per-band-flags:
  1500. * 1 - tone data present
  1501. */
  1502. static void decode_tones_phase(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1503. int ch_num, int band_has_tones[])
  1504. {
  1505. int sb, i;
  1506. Atrac3pWaveParam *wparam;
  1507. Atrac3pWavesData *dst = ctx->channels[ch_num].tones_info;
  1508. for (sb = 0; sb < ctx->waves_info->num_tone_bands; sb++) {
  1509. if (!band_has_tones[sb])
  1510. continue;
  1511. wparam = &ctx->waves_info->waves[dst[sb].start_index];
  1512. for (i = 0; i < dst[sb].num_wavs; i++)
  1513. wparam[i].phase_index = get_bits(gb, 5);
  1514. }
  1515. }
  1516. /**
  1517. * Decode tones info for all channels.
  1518. *
  1519. * @param[in] gb the GetBit context
  1520. * @param[in,out] ctx ptr to the channel unit context
  1521. * @param[in] num_channels number of channels to process
  1522. * @param[in] avctx ptr to the AVCodecContext
  1523. * @return result code: 0 = OK, otherwise - error code
  1524. */
  1525. static int decode_tones_info(GetBitContext *gb, Atrac3pChanUnitCtx *ctx,
  1526. int num_channels, AVCodecContext *avctx)
  1527. {
  1528. int ch_num, i, ret;
  1529. int band_has_tones[16];
  1530. for (ch_num = 0; ch_num < num_channels; ch_num++)
  1531. memset(ctx->channels[ch_num].tones_info, 0,
  1532. sizeof(*ctx->channels[ch_num].tones_info) * ATRAC3P_SUBBANDS);
  1533. ctx->waves_info->tones_present = get_bits1(gb);
  1534. if (!ctx->waves_info->tones_present)
  1535. return 0;
  1536. memset(ctx->waves_info->waves, 0, sizeof(ctx->waves_info->waves));
  1537. ctx->waves_info->amplitude_mode = get_bits1(gb);
  1538. if (!ctx->waves_info->amplitude_mode) {
  1539. avpriv_report_missing_feature(avctx, "GHA amplitude mode 0");
  1540. return AVERROR_PATCHWELCOME;
  1541. }
  1542. ctx->waves_info->num_tone_bands =
  1543. get_vlc2(gb, tone_vlc_tabs[0].table,
  1544. tone_vlc_tabs[0].bits, 1) + 1;
  1545. if (num_channels == 2) {
  1546. get_subband_flags(gb, ctx->waves_info->tone_sharing, ctx->waves_info->num_tone_bands);
  1547. get_subband_flags(gb, ctx->waves_info->tone_master, ctx->waves_info->num_tone_bands);
  1548. get_subband_flags(gb, ctx->waves_info->invert_phase, ctx->waves_info->num_tone_bands);
  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. }