atrac9dec.c 33 KB

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