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.

1001 lines
33KB

  1. /*
  2. * ATRAC9 decoder
  3. * Copyright (c) 2018 Rostislav Pehlivanov <atomnuker@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/thread.h"
  22. #include "internal.h"
  23. #include "get_bits.h"
  24. #include "fft.h"
  25. #include "atrac9tab.h"
  26. #include "libavutil/lfg.h"
  27. #include "libavutil/float_dsp.h"
  28. #define ATRAC9_SF_VLC_BITS 8
  29. #define ATRAC9_COEFF_VLC_BITS 9
  30. typedef struct ATRAC9ChannelData {
  31. int band_ext;
  32. int q_unit_cnt;
  33. int band_ext_data[4];
  34. int32_t scalefactors[31];
  35. int32_t scalefactors_prev[31];
  36. int precision_coarse[30];
  37. int precision_fine[30];
  38. int precision_mask[30];
  39. int codebookset[30];
  40. int32_t q_coeffs_coarse[256];
  41. int32_t q_coeffs_fine[256];
  42. DECLARE_ALIGNED(32, float, coeffs )[256];
  43. DECLARE_ALIGNED(32, float, prev_win)[128];
  44. } ATRAC9ChannelData;
  45. typedef struct ATRAC9BlockData {
  46. ATRAC9ChannelData channel[2];
  47. /* Base */
  48. int band_count;
  49. int q_unit_cnt;
  50. int q_unit_cnt_prev;
  51. /* Stereo block only */
  52. int stereo_q_unit;
  53. /* Band extension only */
  54. int has_band_ext;
  55. int has_band_ext_data;
  56. int band_ext_q_unit;
  57. /* Gradient */
  58. int grad_mode;
  59. int grad_boundary;
  60. int gradient[31];
  61. /* Stereo */
  62. int cpe_base_channel;
  63. int is_signs[30];
  64. int reuseable;
  65. } ATRAC9BlockData;
  66. typedef struct ATRAC9Context {
  67. AVCodecContext *avctx;
  68. AVFloatDSPContext *fdsp;
  69. FFTContext imdct;
  70. ATRAC9BlockData block[5];
  71. AVLFG lfg;
  72. /* Set on init */
  73. int frame_log2;
  74. int avg_frame_size;
  75. int frame_count;
  76. int samplerate_idx;
  77. const ATRAC9BlockConfig *block_config;
  78. /* Generated on init */
  79. uint8_t alloc_curve[48][48];
  80. DECLARE_ALIGNED(32, float, imdct_win)[256];
  81. DECLARE_ALIGNED(32, float, temp)[256];
  82. } ATRAC9Context;
  83. static VLC sf_vlc[2][8]; /* Signed/unsigned, length */
  84. static VLC coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */
  85. static inline int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b,
  86. GetBitContext *gb)
  87. {
  88. int grad_range[2];
  89. int grad_value[2];
  90. int values, sign, base;
  91. uint8_t *curve;
  92. float scale;
  93. b->grad_mode = get_bits(gb, 2);
  94. if (b->grad_mode) {
  95. grad_range[0] = get_bits(gb, 5);
  96. grad_range[1] = 31;
  97. grad_value[0] = get_bits(gb, 5);
  98. grad_value[1] = 31;
  99. } else {
  100. grad_range[0] = get_bits(gb, 6);
  101. grad_range[1] = get_bits(gb, 6) + 1;
  102. grad_value[0] = get_bits(gb, 5);
  103. grad_value[1] = get_bits(gb, 5);
  104. }
  105. b->grad_boundary = get_bits(gb, 4);
  106. if (grad_range[0] >= grad_range[1] || grad_range[1] > 31)
  107. return AVERROR_INVALIDDATA;
  108. if (b->grad_boundary > b->q_unit_cnt)
  109. return AVERROR_INVALIDDATA;
  110. values = grad_value[1] - grad_value[0];
  111. sign = 1 - 2*(values < 0);
  112. base = grad_value[0] + sign;
  113. scale = (FFABS(values) - 1) / 31.0f;
  114. curve = s->alloc_curve[grad_range[1] - grad_range[0] - 1];
  115. for (int i = 0; i <= b->q_unit_cnt; i++)
  116. b->gradient[i] = grad_value[i >= grad_range[0]];
  117. for (int i = grad_range[0]; i < grad_range[1]; i++)
  118. b->gradient[i] = base + sign*((int)(scale*curve[i - grad_range[0]]));
  119. return 0;
  120. }
  121. static inline void calc_precision(ATRAC9Context *s, ATRAC9BlockData *b,
  122. ATRAC9ChannelData *c)
  123. {
  124. memset(c->precision_mask, 0, sizeof(c->precision_mask));
  125. for (int i = 1; i < b->q_unit_cnt; i++) {
  126. const int delta = FFABS(c->scalefactors[i] - c->scalefactors[i - 1]) - 1;
  127. if (delta > 0) {
  128. const int neg = c->scalefactors[i - 1] > c->scalefactors[i];
  129. c->precision_mask[i - neg] += FFMIN(delta, 5);
  130. }
  131. }
  132. if (b->grad_mode) {
  133. for (int i = 0; i < b->q_unit_cnt; i++) {
  134. c->precision_coarse[i] = c->scalefactors[i];
  135. c->precision_coarse[i] += c->precision_mask[i] - b->gradient[i];
  136. if (c->precision_coarse[i] < 0)
  137. continue;
  138. switch (b->grad_mode) {
  139. case 1:
  140. c->precision_coarse[i] >>= 1;
  141. break;
  142. case 2:
  143. c->precision_coarse[i] = (3 * c->precision_coarse[i]) >> 3;
  144. break;
  145. case 3:
  146. c->precision_coarse[i] >>= 2;
  147. break;
  148. }
  149. }
  150. } else {
  151. for (int i = 0; i < b->q_unit_cnt; i++)
  152. c->precision_coarse[i] = c->scalefactors[i] - b->gradient[i];
  153. }
  154. for (int i = 0; i < b->q_unit_cnt; i++)
  155. c->precision_coarse[i] = FFMAX(c->precision_coarse[i], 1);
  156. for (int i = 0; i < b->grad_boundary; i++)
  157. c->precision_coarse[i]++;
  158. for (int i = 0; i < b->q_unit_cnt; i++) {
  159. c->precision_fine[i] = 0;
  160. if (c->precision_coarse[i] > 15) {
  161. c->precision_fine[i] = FFMIN(c->precision_coarse[i], 30) - 15;
  162. c->precision_coarse[i] = 15;
  163. }
  164. }
  165. }
  166. static inline int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b,
  167. GetBitContext *gb, int stereo)
  168. {
  169. int ext_band = 0;
  170. if (b->has_band_ext) {
  171. if (b->q_unit_cnt < 13 || b->q_unit_cnt > 20)
  172. return AVERROR_INVALIDDATA;
  173. ext_band = at9_tab_band_ext_group[b->q_unit_cnt - 13][2];
  174. if (stereo) {
  175. b->channel[1].band_ext = get_bits(gb, 2);
  176. b->channel[1].band_ext = ext_band > 2 ? b->channel[1].band_ext : 4;
  177. } else {
  178. skip_bits1(gb);
  179. }
  180. }
  181. b->has_band_ext_data = get_bits1(gb);
  182. if (!b->has_band_ext_data)
  183. return 0;
  184. if (!b->has_band_ext) {
  185. skip_bits(gb, 2);
  186. skip_bits_long(gb, get_bits(gb, 5));
  187. return 0;
  188. }
  189. b->channel[0].band_ext = get_bits(gb, 2);
  190. b->channel[0].band_ext = ext_band > 2 ? b->channel[0].band_ext : 4;
  191. if (!get_bits(gb, 5)) {
  192. for (int i = 0; i <= stereo; i++) {
  193. ATRAC9ChannelData *c = &b->channel[i];
  194. const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
  195. for (int j = 0; j < count; j++) {
  196. int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
  197. c->band_ext_data[j] = av_clip_uintp2_c(c->band_ext_data[j], len);
  198. }
  199. }
  200. return 0;
  201. }
  202. for (int i = 0; i <= stereo; i++) {
  203. ATRAC9ChannelData *c = &b->channel[i];
  204. const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
  205. for (int j = 0; j < count; j++) {
  206. int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
  207. c->band_ext_data[j] = get_bits(gb, len);
  208. }
  209. }
  210. return 0;
  211. }
  212. static inline int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b,
  213. ATRAC9ChannelData *c, GetBitContext *gb,
  214. int channel_idx, int first_in_pkt)
  215. {
  216. static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
  217. const int mode = mode_map[channel_idx][get_bits(gb, 2)];
  218. memset(c->scalefactors, 0, sizeof(c->scalefactors));
  219. if (first_in_pkt && (mode == 4 || ((mode == 3) && !channel_idx))) {
  220. av_log(s->avctx, AV_LOG_ERROR, "Invalid scalefactor coding mode!\n");
  221. return AVERROR_INVALIDDATA;
  222. }
  223. switch (mode) {
  224. case 0: { /* VLC delta offset */
  225. const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
  226. const int base = get_bits(gb, 5);
  227. const int len = get_bits(gb, 2) + 3;
  228. const VLC *tab = &sf_vlc[0][len];
  229. c->scalefactors[0] = get_bits(gb, len);
  230. for (int i = 1; i < b->band_ext_q_unit; i++) {
  231. int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table,
  232. ATRAC9_SF_VLC_BITS, 1);
  233. c->scalefactors[i] = val & ((1 << len) - 1);
  234. }
  235. for (int i = 0; i < b->band_ext_q_unit; i++)
  236. c->scalefactors[i] += base - sf_weights[i];
  237. break;
  238. }
  239. case 1: { /* CLC offset */
  240. const int len = get_bits(gb, 2) + 2;
  241. const int base = len < 5 ? get_bits(gb, 5) : 0;
  242. for (int i = 0; i < b->band_ext_q_unit; i++)
  243. c->scalefactors[i] = base + get_bits(gb, len);
  244. break;
  245. }
  246. case 2:
  247. case 4: { /* VLC dist to baseline */
  248. const int *baseline = mode == 4 ? c->scalefactors_prev :
  249. channel_idx ? b->channel[0].scalefactors :
  250. c->scalefactors_prev;
  251. const int baseline_len = mode == 4 ? b->q_unit_cnt_prev :
  252. channel_idx ? b->band_ext_q_unit :
  253. b->q_unit_cnt_prev;
  254. const int len = get_bits(gb, 2) + 2;
  255. const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
  256. const VLC *tab = &sf_vlc[1][len];
  257. for (int i = 0; i < unit_cnt; i++) {
  258. int dist = get_vlc2(gb, tab->table, ATRAC9_SF_VLC_BITS, 1);
  259. c->scalefactors[i] = baseline[i] + dist;
  260. }
  261. for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
  262. c->scalefactors[i] = get_bits(gb, 5);
  263. break;
  264. }
  265. case 3: { /* VLC offset with baseline */
  266. const int *baseline = channel_idx ? b->channel[0].scalefactors :
  267. c->scalefactors_prev;
  268. const int baseline_len = channel_idx ? b->band_ext_q_unit :
  269. b->q_unit_cnt_prev;
  270. const int base = get_bits(gb, 5) - (1 << (5 - 1));
  271. const int len = get_bits(gb, 2) + 1;
  272. const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
  273. const VLC *tab = &sf_vlc[0][len];
  274. c->scalefactors[0] = get_bits(gb, len);
  275. for (int i = 1; i < unit_cnt; i++) {
  276. int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table,
  277. ATRAC9_SF_VLC_BITS, 1);
  278. c->scalefactors[i] = val & ((1 << len) - 1);
  279. }
  280. for (int i = 0; i < unit_cnt; i++)
  281. c->scalefactors[i] += base + baseline[i];
  282. for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
  283. c->scalefactors[i] = get_bits(gb, 5);
  284. break;
  285. }
  286. }
  287. for (int i = 0; i < b->band_ext_q_unit; i++)
  288. if (c->scalefactors[i] < 0 || c->scalefactors[i] > 31)
  289. return AVERROR_INVALIDDATA;
  290. memcpy(c->scalefactors_prev, c->scalefactors, sizeof(c->scalefactors));
  291. return 0;
  292. }
  293. static inline void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b,
  294. ATRAC9ChannelData *c)
  295. {
  296. int avg = 0;
  297. const int last_sf = c->scalefactors[c->q_unit_cnt];
  298. memset(c->codebookset, 0, sizeof(c->codebookset));
  299. if (c->q_unit_cnt <= 1)
  300. return;
  301. if (s->samplerate_idx > 7)
  302. return;
  303. c->scalefactors[c->q_unit_cnt] = c->scalefactors[c->q_unit_cnt - 1];
  304. if (c->q_unit_cnt > 12) {
  305. for (int i = 0; i < 12; i++)
  306. avg += c->scalefactors[i];
  307. avg = (avg + 6) / 12;
  308. }
  309. for (int i = 8; i < c->q_unit_cnt; i++) {
  310. const int prev = c->scalefactors[i - 1];
  311. const int cur = c->scalefactors[i ];
  312. const int next = c->scalefactors[i + 1];
  313. const int min = FFMIN(prev, next);
  314. if ((cur - min >= 3 || 2*cur - prev - next >= 3))
  315. c->codebookset[i] = 1;
  316. }
  317. for (int i = 12; i < c->q_unit_cnt; i++) {
  318. const int cur = c->scalefactors[i];
  319. const int cnd = at9_q_unit_to_coeff_cnt[i] == 16;
  320. const int min = FFMIN(c->scalefactors[i + 1], c->scalefactors[i - 1]);
  321. if (c->codebookset[i])
  322. continue;
  323. c->codebookset[i] = (((cur - min) >= 2) && (cur >= (avg - cnd)));
  324. }
  325. c->scalefactors[c->q_unit_cnt] = last_sf;
  326. }
  327. static inline void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b,
  328. ATRAC9ChannelData *c, GetBitContext *gb)
  329. {
  330. const int max_prec = s->samplerate_idx > 7 ? 1 : 7;
  331. memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
  332. for (int i = 0; i < c->q_unit_cnt; i++) {
  333. int *coeffs = &c->q_coeffs_coarse[at9_q_unit_to_coeff_idx[i]];
  334. const int bands = at9_q_unit_to_coeff_cnt[i];
  335. const int prec = c->precision_coarse[i] + 1;
  336. if (prec <= max_prec) {
  337. const int cb = c->codebookset[i];
  338. const int cbi = at9_q_unit_to_codebookidx[i];
  339. const VLC *tab = &coeff_vlc[cb][prec][cbi];
  340. const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
  341. const int groups = bands >> huff->value_cnt_pow;
  342. for (int j = 0; j < groups; j++) {
  343. uint16_t val = get_vlc2(gb, tab->table, ATRAC9_COEFF_VLC_BITS, 2);
  344. for (int k = 0; k < huff->value_cnt; k++) {
  345. coeffs[k] = sign_extend(val, huff->value_bits);
  346. val >>= huff->value_bits;
  347. }
  348. coeffs += huff->value_cnt;
  349. }
  350. } else {
  351. for (int j = 0; j < bands; j++)
  352. coeffs[j] = sign_extend(get_bits(gb, prec), prec);
  353. }
  354. }
  355. }
  356. static inline void read_coeffs_fine(ATRAC9Context *s, ATRAC9BlockData *b,
  357. ATRAC9ChannelData *c, GetBitContext *gb)
  358. {
  359. memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
  360. for (int i = 0; i < c->q_unit_cnt; i++) {
  361. const int start = at9_q_unit_to_coeff_idx[i + 0];
  362. const int end = at9_q_unit_to_coeff_idx[i + 1];
  363. const int len = c->precision_fine[i] + 1;
  364. if (c->precision_fine[i] <= 0)
  365. continue;
  366. for (int j = start; j < end; j++)
  367. c->q_coeffs_fine[j] = sign_extend(get_bits(gb, len), len);
  368. }
  369. }
  370. static inline void dequantize(ATRAC9Context *s, ATRAC9BlockData *b,
  371. ATRAC9ChannelData *c)
  372. {
  373. memset(c->coeffs, 0, sizeof(c->coeffs));
  374. for (int i = 0; i < c->q_unit_cnt; i++) {
  375. const int start = at9_q_unit_to_coeff_idx[i + 0];
  376. const int end = at9_q_unit_to_coeff_idx[i + 1];
  377. const float coarse_c = at9_quant_step_coarse[c->precision_coarse[i]];
  378. const float fine_c = at9_quant_step_fine[c->precision_fine[i]];
  379. for (int j = start; j < end; j++) {
  380. const float vc = c->q_coeffs_coarse[j] * coarse_c;
  381. const float vf = c->q_coeffs_fine[j] * fine_c;
  382. c->coeffs[j] = vc + vf;
  383. }
  384. }
  385. }
  386. static inline void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b,
  387. const int stereo)
  388. {
  389. float *src = b->channel[ b->cpe_base_channel].coeffs;
  390. float *dst = b->channel[!b->cpe_base_channel].coeffs;
  391. if (!stereo)
  392. return;
  393. if (b->q_unit_cnt <= b->stereo_q_unit)
  394. return;
  395. for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++) {
  396. const int sign = b->is_signs[i];
  397. const int start = at9_q_unit_to_coeff_idx[i + 0];
  398. const int end = at9_q_unit_to_coeff_idx[i + 1];
  399. for (int j = start; j < end; j++)
  400. dst[j] = sign*src[j];
  401. }
  402. }
  403. static inline void apply_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b,
  404. const int stereo)
  405. {
  406. for (int i = 0; i <= stereo; i++) {
  407. float *coeffs = b->channel[i].coeffs;
  408. for (int j = 0; j < b->q_unit_cnt; j++) {
  409. const int start = at9_q_unit_to_coeff_idx[j + 0];
  410. const int end = at9_q_unit_to_coeff_idx[j + 1];
  411. const int scalefactor = b->channel[i].scalefactors[j];
  412. const float scale = at9_scalefactor_c[scalefactor];
  413. for (int k = start; k < end; k++)
  414. coeffs[k] *= scale;
  415. }
  416. }
  417. }
  418. static inline void fill_with_noise(ATRAC9Context *s, ATRAC9ChannelData *c,
  419. int start, int count)
  420. {
  421. float maxval = 0.0f;
  422. for (int i = 0; i < count; i += 2) {
  423. double tmp[2];
  424. av_bmg_get(&s->lfg, tmp);
  425. c->coeffs[start + i + 0] = tmp[0];
  426. c->coeffs[start + i + 1] = tmp[1];
  427. maxval = FFMAX(FFMAX(FFABS(tmp[0]), FFABS(tmp[1])), maxval);
  428. }
  429. /* Normalize */
  430. for (int i = 0; i < count; i++)
  431. c->coeffs[start + i] /= maxval;
  432. }
  433. static inline void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6],
  434. const int s_unit, const int e_unit)
  435. {
  436. for (int i = s_unit; i < e_unit; i++) {
  437. const int start = at9_q_unit_to_coeff_idx[i + 0];
  438. const int end = at9_q_unit_to_coeff_idx[i + 1];
  439. for (int j = start; j < end; j++)
  440. c->coeffs[j] *= sf[i - s_unit];
  441. }
  442. }
  443. static inline void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b,
  444. const int stereo)
  445. {
  446. const int g_units[4] = { /* A, B, C, total units */
  447. b->q_unit_cnt,
  448. at9_tab_band_ext_group[b->q_unit_cnt - 13][0],
  449. at9_tab_band_ext_group[b->q_unit_cnt - 13][1],
  450. FFMAX(g_units[2], 22),
  451. };
  452. const int g_bins[4] = { /* A, B, C, total bins */
  453. at9_q_unit_to_coeff_idx[g_units[0]],
  454. at9_q_unit_to_coeff_idx[g_units[1]],
  455. at9_q_unit_to_coeff_idx[g_units[2]],
  456. at9_q_unit_to_coeff_idx[g_units[3]],
  457. };
  458. for (int ch = 0; ch <= stereo; ch++) {
  459. ATRAC9ChannelData *c = &b->channel[ch];
  460. /* Mirror the spectrum */
  461. for (int i = 0; i < 3; i++)
  462. for (int j = 0; j < (g_bins[i + 1] - g_bins[i + 0]); j++)
  463. c->coeffs[g_bins[i] + j] = c->coeffs[g_bins[i] - j - 1];
  464. switch (c->band_ext) {
  465. case 0: {
  466. float sf[6] = { 0.0f };
  467. const int l = g_units[3] - g_units[0] - 1;
  468. const int n_start = at9_q_unit_to_coeff_idx[g_units[3] - 1];
  469. const int n_cnt = at9_q_unit_to_coeff_cnt[g_units[3] - 1];
  470. switch (at9_tab_band_ext_group[b->q_unit_cnt - 13][2]) {
  471. case 3:
  472. sf[0] = at9_band_ext_scales_m0[0][0][c->band_ext_data[0]];
  473. sf[1] = at9_band_ext_scales_m0[0][1][c->band_ext_data[0]];
  474. sf[2] = at9_band_ext_scales_m0[0][2][c->band_ext_data[1]];
  475. sf[3] = at9_band_ext_scales_m0[0][3][c->band_ext_data[2]];
  476. sf[4] = at9_band_ext_scales_m0[0][4][c->band_ext_data[3]];
  477. break;
  478. case 4:
  479. sf[0] = at9_band_ext_scales_m0[1][0][c->band_ext_data[0]];
  480. sf[1] = at9_band_ext_scales_m0[1][1][c->band_ext_data[0]];
  481. sf[2] = at9_band_ext_scales_m0[1][2][c->band_ext_data[1]];
  482. sf[3] = at9_band_ext_scales_m0[1][3][c->band_ext_data[2]];
  483. sf[4] = at9_band_ext_scales_m0[1][4][c->band_ext_data[3]];
  484. break;
  485. case 5:
  486. sf[0] = at9_band_ext_scales_m0[2][0][c->band_ext_data[0]];
  487. sf[1] = at9_band_ext_scales_m0[2][1][c->band_ext_data[1]];
  488. sf[2] = at9_band_ext_scales_m0[2][2][c->band_ext_data[1]];
  489. break;
  490. }
  491. sf[l] = at9_scalefactor_c[c->scalefactors[g_units[0]]];
  492. fill_with_noise(s, c, n_start, n_cnt);
  493. scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
  494. break;
  495. }
  496. case 1: {
  497. float sf[6];
  498. for (int i = g_units[0]; i < g_units[3]; i++)
  499. sf[i - g_units[0]] = at9_scalefactor_c[c->scalefactors[i]];
  500. fill_with_noise(s, c, g_bins[0], g_bins[3] - g_bins[0]);
  501. scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
  502. break;
  503. }
  504. case 2: {
  505. const float g_sf[2] = {
  506. at9_band_ext_scales_m2[c->band_ext_data[0]],
  507. at9_band_ext_scales_m2[c->band_ext_data[1]],
  508. };
  509. for (int i = 0; i < 2; i++)
  510. for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
  511. c->coeffs[j] *= g_sf[i];
  512. break;
  513. }
  514. case 3: {
  515. float scale = at9_band_ext_scales_m3[c->band_ext_data[0]][0];
  516. float rate = at9_band_ext_scales_m3[c->band_ext_data[1]][1];
  517. rate = pow(2, rate);
  518. for (int i = g_bins[0]; i < g_bins[3]; i++) {
  519. scale *= rate;
  520. c->coeffs[i] *= scale;
  521. }
  522. break;
  523. }
  524. case 4: {
  525. const float m = at9_band_ext_scales_m4[c->band_ext_data[0]];
  526. const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
  527. for (int i = 0; i < 3; i++)
  528. for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
  529. c->coeffs[j] *= g_sf[i];
  530. break;
  531. }
  532. }
  533. }
  534. }
  535. static int atrac9_decode_block(ATRAC9Context *s, GetBitContext *gb,
  536. ATRAC9BlockData *b, AVFrame *frame,
  537. int frame_idx, int block_idx)
  538. {
  539. const int first_in_pkt = !get_bits1(gb);
  540. const int reuse_params = get_bits1(gb);
  541. const int stereo = s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_CPE;
  542. if (s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_LFE) {
  543. ATRAC9ChannelData *c = &b->channel[0];
  544. const int precision = reuse_params ? 8 : 4;
  545. c->q_unit_cnt = b->q_unit_cnt = 2;
  546. memset(c->scalefactors, 0, sizeof(c->scalefactors));
  547. memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
  548. memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
  549. for (int i = 0; i < b->q_unit_cnt; i++) {
  550. c->scalefactors[i] = get_bits(gb, 5);
  551. c->precision_coarse[i] = precision;
  552. c->precision_fine[i] = 0;
  553. }
  554. for (int i = 0; i < c->q_unit_cnt; i++) {
  555. const int start = at9_q_unit_to_coeff_idx[i + 0];
  556. const int end = at9_q_unit_to_coeff_idx[i + 1];
  557. for (int j = start; j < end; j++)
  558. c->q_coeffs_coarse[j] = get_bits(gb, c->precision_coarse[i] + 1);
  559. }
  560. dequantize (s, b, c);
  561. apply_scalefactors(s, b, 0);
  562. goto imdct;
  563. }
  564. if (first_in_pkt && reuse_params) {
  565. av_log(s->avctx, AV_LOG_ERROR, "Invalid block flags!\n");
  566. return AVERROR_INVALIDDATA;
  567. }
  568. /* Band parameters */
  569. if (!reuse_params) {
  570. int stereo_band, ext_band;
  571. const int min_band_count = s->samplerate_idx > 7 ? 1 : 3;
  572. b->reuseable = 0;
  573. b->band_count = get_bits(gb, 4) + min_band_count;
  574. b->q_unit_cnt = at9_tab_band_q_unit_map[b->band_count];
  575. b->band_ext_q_unit = b->stereo_q_unit = b->q_unit_cnt;
  576. if (b->band_count > at9_tab_sri_max_bands[s->samplerate_idx]) {
  577. av_log(s->avctx, AV_LOG_ERROR, "Invalid band count %i!\n",
  578. b->band_count);
  579. return AVERROR_INVALIDDATA;
  580. }
  581. if (stereo) {
  582. stereo_band = get_bits(gb, 4) + min_band_count;
  583. if (stereo_band > b->band_count) {
  584. av_log(s->avctx, AV_LOG_ERROR, "Invalid stereo band %i!\n",
  585. stereo_band);
  586. return AVERROR_INVALIDDATA;
  587. }
  588. b->stereo_q_unit = at9_tab_band_q_unit_map[stereo_band];
  589. }
  590. b->has_band_ext = get_bits1(gb);
  591. if (b->has_band_ext) {
  592. ext_band = get_bits(gb, 4) + min_band_count;
  593. if (ext_band < b->band_count) {
  594. av_log(s->avctx, AV_LOG_ERROR, "Invalid extension band %i!\n",
  595. ext_band);
  596. return AVERROR_INVALIDDATA;
  597. }
  598. b->band_ext_q_unit = at9_tab_band_q_unit_map[ext_band];
  599. }
  600. b->reuseable = 1;
  601. }
  602. if (!b->reuseable) {
  603. av_log(s->avctx, AV_LOG_ERROR, "invalid block reused!\n");
  604. return AVERROR_INVALIDDATA;
  605. }
  606. /* Calculate bit alloc gradient */
  607. if (parse_gradient(s, b, gb))
  608. return AVERROR_INVALIDDATA;
  609. /* IS data */
  610. b->cpe_base_channel = 0;
  611. if (stereo) {
  612. b->cpe_base_channel = get_bits1(gb);
  613. if (get_bits1(gb)) {
  614. for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++)
  615. b->is_signs[i] = 1 - 2*get_bits1(gb);
  616. } else {
  617. for (int i = 0; i < FF_ARRAY_ELEMS(b->is_signs); i++)
  618. b->is_signs[i] = 1;
  619. }
  620. }
  621. /* Band extension */
  622. if (parse_band_ext(s, b, gb, stereo))
  623. return AVERROR_INVALIDDATA;
  624. /* Scalefactors */
  625. for (int i = 0; i <= stereo; i++) {
  626. ATRAC9ChannelData *c = &b->channel[i];
  627. c->q_unit_cnt = i == b->cpe_base_channel ? b->q_unit_cnt :
  628. b->stereo_q_unit;
  629. if (read_scalefactors(s, b, c, gb, i, first_in_pkt))
  630. return AVERROR_INVALIDDATA;
  631. calc_precision (s, b, c);
  632. calc_codebook_idx (s, b, c);
  633. read_coeffs_coarse(s, b, c, gb);
  634. read_coeffs_fine (s, b, c, gb);
  635. dequantize (s, b, c);
  636. }
  637. b->q_unit_cnt_prev = b->has_band_ext ? b->band_ext_q_unit : b->q_unit_cnt;
  638. apply_intensity_stereo(s, b, stereo);
  639. apply_scalefactors (s, b, stereo);
  640. if (b->has_band_ext && b->has_band_ext_data)
  641. apply_band_extension (s, b, stereo);
  642. imdct:
  643. for (int i = 0; i <= stereo; i++) {
  644. ATRAC9ChannelData *c = &b->channel[i];
  645. const int dst_idx = s->block_config->plane_map[block_idx][i];
  646. const int wsize = 1 << s->frame_log2;
  647. const ptrdiff_t offset = wsize*frame_idx*sizeof(float);
  648. float *dst = (float *)(frame->extended_data[dst_idx] + offset);
  649. s->imdct.imdct_half(&s->imdct, s->temp, c->coeffs);
  650. s->fdsp->vector_fmul_window(dst, c->prev_win, s->temp,
  651. s->imdct_win, wsize >> 1);
  652. memcpy(c->prev_win, s->temp + (wsize >> 1), sizeof(float)*wsize >> 1);
  653. }
  654. return 0;
  655. }
  656. static int atrac9_decode_frame(AVCodecContext *avctx, void *data,
  657. int *got_frame_ptr, AVPacket *avpkt)
  658. {
  659. int ret;
  660. GetBitContext gb;
  661. AVFrame *frame = data;
  662. ATRAC9Context *s = avctx->priv_data;
  663. const int frames = FFMIN(avpkt->size / s->avg_frame_size, s->frame_count);
  664. frame->nb_samples = (1 << s->frame_log2) * frames;
  665. ret = ff_get_buffer(avctx, frame, 0);
  666. if (ret < 0)
  667. return ret;
  668. init_get_bits8(&gb, avpkt->data, avpkt->size);
  669. for (int i = 0; i < frames; i++) {
  670. for (int j = 0; j < s->block_config->count; j++) {
  671. ret = atrac9_decode_block(s, &gb, &s->block[j], frame, i, j);
  672. if (ret)
  673. return ret;
  674. align_get_bits(&gb);
  675. }
  676. }
  677. *got_frame_ptr = 1;
  678. return avctx->block_align;
  679. }
  680. static void atrac9_decode_flush(AVCodecContext *avctx)
  681. {
  682. ATRAC9Context *s = avctx->priv_data;
  683. for (int j = 0; j < s->block_config->count; j++) {
  684. ATRAC9BlockData *b = &s->block[j];
  685. const int stereo = s->block_config->type[j] == ATRAC9_BLOCK_TYPE_CPE;
  686. for (int i = 0; i <= stereo; i++) {
  687. ATRAC9ChannelData *c = &b->channel[i];
  688. memset(c->prev_win, 0, sizeof(c->prev_win));
  689. }
  690. }
  691. }
  692. static av_cold int atrac9_decode_close(AVCodecContext *avctx)
  693. {
  694. ATRAC9Context *s = avctx->priv_data;
  695. ff_mdct_end(&s->imdct);
  696. av_freep(&s->fdsp);
  697. return 0;
  698. }
  699. static av_cold void atrac9_init_vlc(VLC *vlc, int nb_bits, int nb_codes,
  700. const uint8_t (**tab)[2],
  701. unsigned *buf_offset, int offset)
  702. {
  703. static VLC_TYPE vlc_buf[24812][2];
  704. vlc->table = &vlc_buf[*buf_offset];
  705. vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
  706. ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
  707. &(*tab)[0][1], 2, &(*tab)[0][0], 2, 1,
  708. offset, INIT_VLC_STATIC_OVERLONG, NULL);
  709. *buf_offset += vlc->table_size;
  710. *tab += nb_codes;
  711. }
  712. static av_cold void atrac9_init_static(void)
  713. {
  714. const uint8_t (*tab)[2];
  715. unsigned offset = 0;
  716. /* Unsigned scalefactor VLCs */
  717. tab = at9_sfb_a_tab;
  718. for (int i = 1; i < 7; i++) {
  719. const HuffmanCodebook *hf = &at9_huffman_sf_unsigned[i];
  720. atrac9_init_vlc(&sf_vlc[0][i], ATRAC9_SF_VLC_BITS,
  721. hf->size, &tab, &offset, 0);
  722. }
  723. /* Signed scalefactor VLCs */
  724. tab = at9_sfb_b_tab;
  725. for (int i = 2; i < 6; i++) {
  726. const HuffmanCodebook *hf = &at9_huffman_sf_signed[i];
  727. /* The symbols are signed integers in the range -16..15;
  728. * the values in the source table are offset by 16 to make
  729. * them fit into an uint8_t; the -16 reverses this shift. */
  730. atrac9_init_vlc(&sf_vlc[1][i], ATRAC9_SF_VLC_BITS,
  731. hf->size, &tab, &offset, -16);
  732. }
  733. /* Coefficient VLCs */
  734. tab = at9_coeffs_tab;
  735. for (int i = 0; i < 2; i++) {
  736. for (int j = 2; j < 8; j++) {
  737. for (int k = i; k < 4; k++) {
  738. const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
  739. atrac9_init_vlc(&coeff_vlc[i][j][k], ATRAC9_COEFF_VLC_BITS,
  740. hf->size, &tab, &offset, 0);
  741. }
  742. }
  743. }
  744. }
  745. static av_cold int atrac9_decode_init(AVCodecContext *avctx)
  746. {
  747. static AVOnce static_table_init = AV_ONCE_INIT;
  748. GetBitContext gb;
  749. ATRAC9Context *s = avctx->priv_data;
  750. int version, block_config_idx, superframe_idx, alloc_c_len;
  751. s->avctx = avctx;
  752. av_lfg_init(&s->lfg, 0xFBADF00D);
  753. if (avctx->block_align <= 0) {
  754. av_log(avctx, AV_LOG_ERROR, "Invalid block align\n");
  755. return AVERROR_INVALIDDATA;
  756. }
  757. if (avctx->extradata_size != 12) {
  758. av_log(avctx, AV_LOG_ERROR, "Invalid extradata length!\n");
  759. return AVERROR_INVALIDDATA;
  760. }
  761. version = AV_RL32(avctx->extradata);
  762. if (version > 2) {
  763. av_log(avctx, AV_LOG_ERROR, "Unsupported version (%i)!\n", version);
  764. return AVERROR_INVALIDDATA;
  765. }
  766. init_get_bits8(&gb, avctx->extradata + 4, avctx->extradata_size);
  767. if (get_bits(&gb, 8) != 0xFE) {
  768. av_log(avctx, AV_LOG_ERROR, "Incorrect magic byte!\n");
  769. return AVERROR_INVALIDDATA;
  770. }
  771. s->samplerate_idx = get_bits(&gb, 4);
  772. avctx->sample_rate = at9_tab_samplerates[s->samplerate_idx];
  773. block_config_idx = get_bits(&gb, 3);
  774. if (block_config_idx > 5) {
  775. av_log(avctx, AV_LOG_ERROR, "Incorrect block config!\n");
  776. return AVERROR_INVALIDDATA;
  777. }
  778. s->block_config = &at9_block_layout[block_config_idx];
  779. avctx->channel_layout = s->block_config->channel_layout;
  780. avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
  781. avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
  782. if (get_bits1(&gb)) {
  783. av_log(avctx, AV_LOG_ERROR, "Incorrect verification bit!\n");
  784. return AVERROR_INVALIDDATA;
  785. }
  786. /* Average frame size in bytes */
  787. s->avg_frame_size = get_bits(&gb, 11) + 1;
  788. superframe_idx = get_bits(&gb, 2);
  789. if (superframe_idx & 1) {
  790. av_log(avctx, AV_LOG_ERROR, "Invalid superframe index!\n");
  791. return AVERROR_INVALIDDATA;
  792. }
  793. s->frame_count = 1 << superframe_idx;
  794. s->frame_log2 = at9_tab_sri_frame_log2[s->samplerate_idx];
  795. if (ff_mdct_init(&s->imdct, s->frame_log2 + 1, 1, 1.0f / 32768.0f))
  796. return AVERROR(ENOMEM);
  797. s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
  798. if (!s->fdsp)
  799. return AVERROR(ENOMEM);
  800. /* iMDCT window */
  801. for (int i = 0; i < (1 << s->frame_log2); i++) {
  802. const int len = 1 << s->frame_log2;
  803. const float sidx = ( i + 0.5f) / len;
  804. const float eidx = (len - i - 0.5f) / len;
  805. const float s_c = sinf(sidx*M_PI - M_PI_2)*0.5f + 0.5f;
  806. const float e_c = sinf(eidx*M_PI - M_PI_2)*0.5f + 0.5f;
  807. s->imdct_win[i] = s_c / ((s_c * s_c) + (e_c * e_c));
  808. }
  809. /* Allocation curve */
  810. alloc_c_len = FF_ARRAY_ELEMS(at9_tab_b_dist);
  811. for (int i = 1; i <= alloc_c_len; i++)
  812. for (int j = 0; j < i; j++)
  813. s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
  814. ff_thread_once(&static_table_init, atrac9_init_static);
  815. return 0;
  816. }
  817. AVCodec ff_atrac9_decoder = {
  818. .name = "atrac9",
  819. .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
  820. .type = AVMEDIA_TYPE_AUDIO,
  821. .id = AV_CODEC_ID_ATRAC9,
  822. .priv_data_size = sizeof(ATRAC9Context),
  823. .init = atrac9_decode_init,
  824. .close = atrac9_decode_close,
  825. .decode = atrac9_decode_frame,
  826. .flush = atrac9_decode_flush,
  827. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
  828. .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1,
  829. };