3 * Copyright (c) 2018 Rostislav Pehlivanov <atomnuker@gmail.com>
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/thread.h"
27 #include "atrac9tab.h"
28 #include "libavutil/lfg.h"
29 #include "libavutil/float_dsp.h"
30 #include "libavutil/mem_internal.h"
32 #define ATRAC9_SF_VLC_BITS 8
33 #define ATRAC9_COEFF_VLC_BITS 9
35 typedef struct ATRAC9ChannelData {
39 int32_t scalefactors[31];
40 int32_t scalefactors_prev[31];
42 int precision_coarse[30];
43 int precision_fine[30];
44 int precision_mask[30];
48 int32_t q_coeffs_coarse[256];
49 int32_t q_coeffs_fine[256];
51 DECLARE_ALIGNED(32, float, coeffs )[256];
52 DECLARE_ALIGNED(32, float, prev_win)[128];
55 typedef struct ATRAC9BlockData {
56 ATRAC9ChannelData channel[2];
63 /* Stereo block only */
66 /* Band extension only */
68 int has_band_ext_data;
84 typedef struct ATRAC9Context {
85 AVCodecContext *avctx;
86 AVFloatDSPContext *fdsp;
88 ATRAC9BlockData block[5];
96 const ATRAC9BlockConfig *block_config;
98 /* Generated on init */
99 uint8_t alloc_curve[48][48];
100 DECLARE_ALIGNED(32, float, imdct_win)[256];
102 DECLARE_ALIGNED(32, float, temp)[256];
105 static VLC sf_vlc[2][8]; /* Signed/unsigned, length */
106 static VLC coeff_vlc[2][8][4]; /* Cookbook, precision, cookbook index */
108 static inline int parse_gradient(ATRAC9Context *s, ATRAC9BlockData *b,
113 int values, sign, base;
117 b->grad_mode = get_bits(gb, 2);
119 grad_range[0] = get_bits(gb, 5);
121 grad_value[0] = get_bits(gb, 5);
124 grad_range[0] = get_bits(gb, 6);
125 grad_range[1] = get_bits(gb, 6) + 1;
126 grad_value[0] = get_bits(gb, 5);
127 grad_value[1] = get_bits(gb, 5);
129 b->grad_boundary = get_bits(gb, 4);
131 if (grad_range[0] >= grad_range[1] || grad_range[1] > 31)
132 return AVERROR_INVALIDDATA;
134 if (b->grad_boundary > b->q_unit_cnt)
135 return AVERROR_INVALIDDATA;
137 values = grad_value[1] - grad_value[0];
138 sign = 1 - 2*(values < 0);
139 base = grad_value[0] + sign;
140 scale = (FFABS(values) - 1) / 31.0f;
141 curve = s->alloc_curve[grad_range[1] - grad_range[0] - 1];
143 for (int i = 0; i <= b->q_unit_cnt; i++)
144 b->gradient[i] = grad_value[i >= grad_range[0]];
146 for (int i = grad_range[0]; i < grad_range[1]; i++)
147 b->gradient[i] = base + sign*((int)(scale*curve[i - grad_range[0]]));
152 static inline void calc_precision(ATRAC9Context *s, ATRAC9BlockData *b,
153 ATRAC9ChannelData *c)
155 memset(c->precision_mask, 0, sizeof(c->precision_mask));
156 for (int i = 1; i < b->q_unit_cnt; i++) {
157 const int delta = FFABS(c->scalefactors[i] - c->scalefactors[i - 1]) - 1;
159 const int neg = c->scalefactors[i - 1] > c->scalefactors[i];
160 c->precision_mask[i - neg] += FFMIN(delta, 5);
165 for (int i = 0; i < b->q_unit_cnt; i++) {
166 c->precision_coarse[i] = c->scalefactors[i];
167 c->precision_coarse[i] += c->precision_mask[i] - b->gradient[i];
168 if (c->precision_coarse[i] < 0)
170 switch (b->grad_mode) {
172 c->precision_coarse[i] >>= 1;
175 c->precision_coarse[i] = (3 * c->precision_coarse[i]) >> 3;
178 c->precision_coarse[i] >>= 2;
183 for (int i = 0; i < b->q_unit_cnt; i++)
184 c->precision_coarse[i] = c->scalefactors[i] - b->gradient[i];
188 for (int i = 0; i < b->q_unit_cnt; i++)
189 c->precision_coarse[i] = FFMAX(c->precision_coarse[i], 1);
191 for (int i = 0; i < b->grad_boundary; i++)
192 c->precision_coarse[i]++;
194 for (int i = 0; i < b->q_unit_cnt; i++) {
195 c->precision_fine[i] = 0;
196 if (c->precision_coarse[i] > 15) {
197 c->precision_fine[i] = FFMIN(c->precision_coarse[i], 30) - 15;
198 c->precision_coarse[i] = 15;
203 static inline int parse_band_ext(ATRAC9Context *s, ATRAC9BlockData *b,
204 GetBitContext *gb, int stereo)
208 if (b->has_band_ext) {
209 if (b->q_unit_cnt < 13 || b->q_unit_cnt > 20)
210 return AVERROR_INVALIDDATA;
211 ext_band = at9_tab_band_ext_group[b->q_unit_cnt - 13][2];
213 b->channel[1].band_ext = get_bits(gb, 2);
214 b->channel[1].band_ext = ext_band > 2 ? b->channel[1].band_ext : 4;
220 b->has_band_ext_data = get_bits1(gb);
221 if (!b->has_band_ext_data)
224 if (!b->has_band_ext) {
226 skip_bits_long(gb, get_bits(gb, 5));
230 b->channel[0].band_ext = get_bits(gb, 2);
231 b->channel[0].band_ext = ext_band > 2 ? b->channel[0].band_ext : 4;
233 if (!get_bits(gb, 5)) {
234 for (int i = 0; i <= stereo; i++) {
235 ATRAC9ChannelData *c = &b->channel[i];
236 const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
237 for (int j = 0; j < count; j++) {
238 int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
239 c->band_ext_data[j] = av_clip_uintp2_c(c->band_ext_data[j], len);
246 for (int i = 0; i <= stereo; i++) {
247 ATRAC9ChannelData *c = &b->channel[i];
248 const int count = at9_tab_band_ext_cnt[c->band_ext][ext_band];
249 for (int j = 0; j < count; j++) {
250 int len = at9_tab_band_ext_lengths[c->band_ext][ext_band][j];
251 c->band_ext_data[j] = get_bits(gb, len);
258 static inline int read_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b,
259 ATRAC9ChannelData *c, GetBitContext *gb,
260 int channel_idx, int first_in_pkt)
262 static const uint8_t mode_map[2][4] = { { 0, 1, 2, 3 }, { 0, 2, 3, 4 } };
263 const int mode = mode_map[channel_idx][get_bits(gb, 2)];
265 memset(c->scalefactors, 0, sizeof(c->scalefactors));
267 if (first_in_pkt && (mode == 4 || ((mode == 3) && !channel_idx))) {
268 av_log(s->avctx, AV_LOG_ERROR, "Invalid scalefactor coding mode!\n");
269 return AVERROR_INVALIDDATA;
273 case 0: { /* VLC delta offset */
274 const uint8_t *sf_weights = at9_tab_sf_weights[get_bits(gb, 3)];
275 const int base = get_bits(gb, 5);
276 const int len = get_bits(gb, 2) + 3;
277 const VLC *tab = &sf_vlc[0][len];
279 c->scalefactors[0] = get_bits(gb, len);
281 for (int i = 1; i < b->band_ext_q_unit; i++) {
282 int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table,
283 ATRAC9_SF_VLC_BITS, 1);
284 c->scalefactors[i] = val & ((1 << len) - 1);
287 for (int i = 0; i < b->band_ext_q_unit; i++)
288 c->scalefactors[i] += base - sf_weights[i];
292 case 1: { /* CLC offset */
293 const int len = get_bits(gb, 2) + 2;
294 const int base = len < 5 ? get_bits(gb, 5) : 0;
295 for (int i = 0; i < b->band_ext_q_unit; i++)
296 c->scalefactors[i] = base + get_bits(gb, len);
300 case 4: { /* VLC dist to baseline */
301 const int *baseline = mode == 4 ? c->scalefactors_prev :
302 channel_idx ? b->channel[0].scalefactors :
303 c->scalefactors_prev;
304 const int baseline_len = mode == 4 ? b->q_unit_cnt_prev :
305 channel_idx ? b->band_ext_q_unit :
308 const int len = get_bits(gb, 2) + 2;
309 const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
310 const VLC *tab = &sf_vlc[1][len];
312 for (int i = 0; i < unit_cnt; i++) {
313 int dist = get_vlc2(gb, tab->table, ATRAC9_SF_VLC_BITS, 1);
314 c->scalefactors[i] = baseline[i] + dist;
317 for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
318 c->scalefactors[i] = get_bits(gb, 5);
322 case 3: { /* VLC offset with baseline */
323 const int *baseline = channel_idx ? b->channel[0].scalefactors :
324 c->scalefactors_prev;
325 const int baseline_len = channel_idx ? b->band_ext_q_unit :
328 const int base = get_bits(gb, 5) - (1 << (5 - 1));
329 const int len = get_bits(gb, 2) + 1;
330 const int unit_cnt = FFMIN(b->band_ext_q_unit, baseline_len);
331 const VLC *tab = &sf_vlc[0][len];
333 c->scalefactors[0] = get_bits(gb, len);
335 for (int i = 1; i < unit_cnt; i++) {
336 int val = c->scalefactors[i - 1] + get_vlc2(gb, tab->table,
337 ATRAC9_SF_VLC_BITS, 1);
338 c->scalefactors[i] = val & ((1 << len) - 1);
341 for (int i = 0; i < unit_cnt; i++)
342 c->scalefactors[i] += base + baseline[i];
344 for (int i = unit_cnt; i < b->band_ext_q_unit; i++)
345 c->scalefactors[i] = get_bits(gb, 5);
350 for (int i = 0; i < b->band_ext_q_unit; i++)
351 if (c->scalefactors[i] < 0 || c->scalefactors[i] > 31)
352 return AVERROR_INVALIDDATA;
354 memcpy(c->scalefactors_prev, c->scalefactors, sizeof(c->scalefactors));
359 static inline void calc_codebook_idx(ATRAC9Context *s, ATRAC9BlockData *b,
360 ATRAC9ChannelData *c)
363 const int last_sf = c->scalefactors[c->q_unit_cnt];
365 memset(c->codebookset, 0, sizeof(c->codebookset));
367 if (c->q_unit_cnt <= 1)
369 if (s->samplerate_idx > 7)
372 c->scalefactors[c->q_unit_cnt] = c->scalefactors[c->q_unit_cnt - 1];
374 if (c->q_unit_cnt > 12) {
375 for (int i = 0; i < 12; i++)
376 avg += c->scalefactors[i];
377 avg = (avg + 6) / 12;
380 for (int i = 8; i < c->q_unit_cnt; i++) {
381 const int prev = c->scalefactors[i - 1];
382 const int cur = c->scalefactors[i ];
383 const int next = c->scalefactors[i + 1];
384 const int min = FFMIN(prev, next);
385 if ((cur - min >= 3 || 2*cur - prev - next >= 3))
386 c->codebookset[i] = 1;
390 for (int i = 12; i < c->q_unit_cnt; i++) {
391 const int cur = c->scalefactors[i];
392 const int cnd = at9_q_unit_to_coeff_cnt[i] == 16;
393 const int min = FFMIN(c->scalefactors[i + 1], c->scalefactors[i - 1]);
394 if (c->codebookset[i])
397 c->codebookset[i] = (((cur - min) >= 2) && (cur >= (avg - cnd)));
400 c->scalefactors[c->q_unit_cnt] = last_sf;
403 static inline void read_coeffs_coarse(ATRAC9Context *s, ATRAC9BlockData *b,
404 ATRAC9ChannelData *c, GetBitContext *gb)
406 const int max_prec = s->samplerate_idx > 7 ? 1 : 7;
408 memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
410 for (int i = 0; i < c->q_unit_cnt; i++) {
411 int *coeffs = &c->q_coeffs_coarse[at9_q_unit_to_coeff_idx[i]];
412 const int bands = at9_q_unit_to_coeff_cnt[i];
413 const int prec = c->precision_coarse[i] + 1;
415 if (prec <= max_prec) {
416 const int cb = c->codebookset[i];
417 const int cbi = at9_q_unit_to_codebookidx[i];
418 const VLC *tab = &coeff_vlc[cb][prec][cbi];
419 const HuffmanCodebook *huff = &at9_huffman_coeffs[cb][prec][cbi];
420 const int groups = bands >> huff->value_cnt_pow;
422 for (int j = 0; j < groups; j++) {
423 uint16_t val = get_vlc2(gb, tab->table, ATRAC9_COEFF_VLC_BITS, 2);
425 for (int k = 0; k < huff->value_cnt; k++) {
426 coeffs[k] = sign_extend(val, huff->value_bits);
427 val >>= huff->value_bits;
430 coeffs += huff->value_cnt;
433 for (int j = 0; j < bands; j++)
434 coeffs[j] = sign_extend(get_bits(gb, prec), prec);
439 static inline void read_coeffs_fine(ATRAC9Context *s, ATRAC9BlockData *b,
440 ATRAC9ChannelData *c, GetBitContext *gb)
442 memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
444 for (int i = 0; i < c->q_unit_cnt; i++) {
445 const int start = at9_q_unit_to_coeff_idx[i + 0];
446 const int end = at9_q_unit_to_coeff_idx[i + 1];
447 const int len = c->precision_fine[i] + 1;
449 if (c->precision_fine[i] <= 0)
452 for (int j = start; j < end; j++)
453 c->q_coeffs_fine[j] = sign_extend(get_bits(gb, len), len);
457 static inline void dequantize(ATRAC9Context *s, ATRAC9BlockData *b,
458 ATRAC9ChannelData *c)
460 memset(c->coeffs, 0, sizeof(c->coeffs));
462 for (int i = 0; i < c->q_unit_cnt; i++) {
463 const int start = at9_q_unit_to_coeff_idx[i + 0];
464 const int end = at9_q_unit_to_coeff_idx[i + 1];
466 const float coarse_c = at9_quant_step_coarse[c->precision_coarse[i]];
467 const float fine_c = at9_quant_step_fine[c->precision_fine[i]];
469 for (int j = start; j < end; j++) {
470 const float vc = c->q_coeffs_coarse[j] * coarse_c;
471 const float vf = c->q_coeffs_fine[j] * fine_c;
472 c->coeffs[j] = vc + vf;
477 static inline void apply_intensity_stereo(ATRAC9Context *s, ATRAC9BlockData *b,
480 float *src = b->channel[ b->cpe_base_channel].coeffs;
481 float *dst = b->channel[!b->cpe_base_channel].coeffs;
486 if (b->q_unit_cnt <= b->stereo_q_unit)
489 for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++) {
490 const int sign = b->is_signs[i];
491 const int start = at9_q_unit_to_coeff_idx[i + 0];
492 const int end = at9_q_unit_to_coeff_idx[i + 1];
493 for (int j = start; j < end; j++)
494 dst[j] = sign*src[j];
498 static inline void apply_scalefactors(ATRAC9Context *s, ATRAC9BlockData *b,
501 for (int i = 0; i <= stereo; i++) {
502 float *coeffs = b->channel[i].coeffs;
503 for (int j = 0; j < b->q_unit_cnt; j++) {
504 const int start = at9_q_unit_to_coeff_idx[j + 0];
505 const int end = at9_q_unit_to_coeff_idx[j + 1];
506 const int scalefactor = b->channel[i].scalefactors[j];
507 const float scale = at9_scalefactor_c[scalefactor];
508 for (int k = start; k < end; k++)
514 static inline void fill_with_noise(ATRAC9Context *s, ATRAC9ChannelData *c,
515 int start, int count)
518 for (int i = 0; i < count; i += 2) {
520 av_bmg_get(&s->lfg, tmp);
521 c->coeffs[start + i + 0] = tmp[0];
522 c->coeffs[start + i + 1] = tmp[1];
523 maxval = FFMAX(FFMAX(FFABS(tmp[0]), FFABS(tmp[1])), maxval);
526 for (int i = 0; i < count; i++)
527 c->coeffs[start + i] /= maxval;
530 static inline void scale_band_ext_coeffs(ATRAC9ChannelData *c, float sf[6],
531 const int s_unit, const int e_unit)
533 for (int i = s_unit; i < e_unit; i++) {
534 const int start = at9_q_unit_to_coeff_idx[i + 0];
535 const int end = at9_q_unit_to_coeff_idx[i + 1];
536 for (int j = start; j < end; j++)
537 c->coeffs[j] *= sf[i - s_unit];
541 static inline void apply_band_extension(ATRAC9Context *s, ATRAC9BlockData *b,
544 const int g_units[4] = { /* A, B, C, total units */
546 at9_tab_band_ext_group[b->q_unit_cnt - 13][0],
547 at9_tab_band_ext_group[b->q_unit_cnt - 13][1],
548 FFMAX(g_units[2], 22),
551 const int g_bins[4] = { /* A, B, C, total bins */
552 at9_q_unit_to_coeff_idx[g_units[0]],
553 at9_q_unit_to_coeff_idx[g_units[1]],
554 at9_q_unit_to_coeff_idx[g_units[2]],
555 at9_q_unit_to_coeff_idx[g_units[3]],
558 for (int ch = 0; ch <= stereo; ch++) {
559 ATRAC9ChannelData *c = &b->channel[ch];
561 /* Mirror the spectrum */
562 for (int i = 0; i < 3; i++)
563 for (int j = 0; j < (g_bins[i + 1] - g_bins[i + 0]); j++)
564 c->coeffs[g_bins[i] + j] = c->coeffs[g_bins[i] - j - 1];
566 switch (c->band_ext) {
568 float sf[6] = { 0.0f };
569 const int l = g_units[3] - g_units[0] - 1;
570 const int n_start = at9_q_unit_to_coeff_idx[g_units[3] - 1];
571 const int n_cnt = at9_q_unit_to_coeff_cnt[g_units[3] - 1];
572 switch (at9_tab_band_ext_group[b->q_unit_cnt - 13][2]) {
574 sf[0] = at9_band_ext_scales_m0[0][0][c->band_ext_data[0]];
575 sf[1] = at9_band_ext_scales_m0[0][1][c->band_ext_data[0]];
576 sf[2] = at9_band_ext_scales_m0[0][2][c->band_ext_data[1]];
577 sf[3] = at9_band_ext_scales_m0[0][3][c->band_ext_data[2]];
578 sf[4] = at9_band_ext_scales_m0[0][4][c->band_ext_data[3]];
581 sf[0] = at9_band_ext_scales_m0[1][0][c->band_ext_data[0]];
582 sf[1] = at9_band_ext_scales_m0[1][1][c->band_ext_data[0]];
583 sf[2] = at9_band_ext_scales_m0[1][2][c->band_ext_data[1]];
584 sf[3] = at9_band_ext_scales_m0[1][3][c->band_ext_data[2]];
585 sf[4] = at9_band_ext_scales_m0[1][4][c->band_ext_data[3]];
588 sf[0] = at9_band_ext_scales_m0[2][0][c->band_ext_data[0]];
589 sf[1] = at9_band_ext_scales_m0[2][1][c->band_ext_data[1]];
590 sf[2] = at9_band_ext_scales_m0[2][2][c->band_ext_data[1]];
594 sf[l] = at9_scalefactor_c[c->scalefactors[g_units[0]]];
596 fill_with_noise(s, c, n_start, n_cnt);
597 scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
602 for (int i = g_units[0]; i < g_units[3]; i++)
603 sf[i - g_units[0]] = at9_scalefactor_c[c->scalefactors[i]];
605 fill_with_noise(s, c, g_bins[0], g_bins[3] - g_bins[0]);
606 scale_band_ext_coeffs(c, sf, g_units[0], g_units[3]);
610 const float g_sf[2] = {
611 at9_band_ext_scales_m2[c->band_ext_data[0]],
612 at9_band_ext_scales_m2[c->band_ext_data[1]],
615 for (int i = 0; i < 2; i++)
616 for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
617 c->coeffs[j] *= g_sf[i];
621 float scale = at9_band_ext_scales_m3[c->band_ext_data[0]][0];
622 float rate = at9_band_ext_scales_m3[c->band_ext_data[1]][1];
624 for (int i = g_bins[0]; i < g_bins[3]; i++) {
626 c->coeffs[i] *= scale;
631 const float m = at9_band_ext_scales_m4[c->band_ext_data[0]];
632 const float g_sf[3] = { 0.7079468f*m, 0.5011902f*m, 0.3548279f*m };
634 for (int i = 0; i < 3; i++)
635 for (int j = g_bins[i + 0]; j < g_bins[i + 1]; j++)
636 c->coeffs[j] *= g_sf[i];
643 static int atrac9_decode_block(ATRAC9Context *s, GetBitContext *gb,
644 ATRAC9BlockData *b, AVFrame *frame,
645 int frame_idx, int block_idx)
647 const int first_in_pkt = !get_bits1(gb);
648 const int reuse_params = get_bits1(gb);
649 const int stereo = s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_CPE;
651 if (s->block_config->type[block_idx] == ATRAC9_BLOCK_TYPE_LFE) {
652 ATRAC9ChannelData *c = &b->channel[0];
653 const int precision = reuse_params ? 8 : 4;
654 c->q_unit_cnt = b->q_unit_cnt = 2;
656 memset(c->scalefactors, 0, sizeof(c->scalefactors));
657 memset(c->q_coeffs_fine, 0, sizeof(c->q_coeffs_fine));
658 memset(c->q_coeffs_coarse, 0, sizeof(c->q_coeffs_coarse));
660 for (int i = 0; i < b->q_unit_cnt; i++) {
661 c->scalefactors[i] = get_bits(gb, 5);
662 c->precision_coarse[i] = precision;
663 c->precision_fine[i] = 0;
666 for (int i = 0; i < c->q_unit_cnt; i++) {
667 const int start = at9_q_unit_to_coeff_idx[i + 0];
668 const int end = at9_q_unit_to_coeff_idx[i + 1];
669 for (int j = start; j < end; j++)
670 c->q_coeffs_coarse[j] = get_bits(gb, c->precision_coarse[i] + 1);
673 dequantize (s, b, c);
674 apply_scalefactors(s, b, 0);
679 if (first_in_pkt && reuse_params) {
680 av_log(s->avctx, AV_LOG_ERROR, "Invalid block flags!\n");
681 return AVERROR_INVALIDDATA;
684 /* Band parameters */
686 int stereo_band, ext_band;
687 const int min_band_count = s->samplerate_idx > 7 ? 1 : 3;
689 b->band_count = get_bits(gb, 4) + min_band_count;
690 b->q_unit_cnt = at9_tab_band_q_unit_map[b->band_count];
692 b->band_ext_q_unit = b->stereo_q_unit = b->q_unit_cnt;
694 if (b->band_count > at9_tab_sri_max_bands[s->samplerate_idx]) {
695 av_log(s->avctx, AV_LOG_ERROR, "Invalid band count %i!\n",
697 return AVERROR_INVALIDDATA;
701 stereo_band = get_bits(gb, 4) + min_band_count;
702 if (stereo_band > b->band_count) {
703 av_log(s->avctx, AV_LOG_ERROR, "Invalid stereo band %i!\n",
705 return AVERROR_INVALIDDATA;
707 b->stereo_q_unit = at9_tab_band_q_unit_map[stereo_band];
710 b->has_band_ext = get_bits1(gb);
711 if (b->has_band_ext) {
712 ext_band = get_bits(gb, 4) + min_band_count;
713 if (ext_band < b->band_count) {
714 av_log(s->avctx, AV_LOG_ERROR, "Invalid extension band %i!\n",
716 return AVERROR_INVALIDDATA;
718 b->band_ext_q_unit = at9_tab_band_q_unit_map[ext_band];
723 av_log(s->avctx, AV_LOG_ERROR, "invalid block reused!\n");
724 return AVERROR_INVALIDDATA;
727 /* Calculate bit alloc gradient */
728 if (parse_gradient(s, b, gb))
729 return AVERROR_INVALIDDATA;
732 b->cpe_base_channel = 0;
734 b->cpe_base_channel = get_bits1(gb);
736 for (int i = b->stereo_q_unit; i < b->q_unit_cnt; i++)
737 b->is_signs[i] = 1 - 2*get_bits1(gb);
739 for (int i = 0; i < FF_ARRAY_ELEMS(b->is_signs); i++)
745 if (parse_band_ext(s, b, gb, stereo))
746 return AVERROR_INVALIDDATA;
749 for (int i = 0; i <= stereo; i++) {
750 ATRAC9ChannelData *c = &b->channel[i];
751 c->q_unit_cnt = i == b->cpe_base_channel ? b->q_unit_cnt :
753 if (read_scalefactors(s, b, c, gb, i, first_in_pkt))
754 return AVERROR_INVALIDDATA;
756 calc_precision (s, b, c);
757 calc_codebook_idx (s, b, c);
758 read_coeffs_coarse(s, b, c, gb);
759 read_coeffs_fine (s, b, c, gb);
760 dequantize (s, b, c);
763 b->q_unit_cnt_prev = b->has_band_ext ? b->band_ext_q_unit : b->q_unit_cnt;
765 apply_intensity_stereo(s, b, stereo);
766 apply_scalefactors (s, b, stereo);
768 if (b->has_band_ext && b->has_band_ext_data)
769 apply_band_extension (s, b, stereo);
772 for (int i = 0; i <= stereo; i++) {
773 ATRAC9ChannelData *c = &b->channel[i];
774 const int dst_idx = s->block_config->plane_map[block_idx][i];
775 const int wsize = 1 << s->frame_log2;
776 const ptrdiff_t offset = wsize*frame_idx*sizeof(float);
777 float *dst = (float *)(frame->extended_data[dst_idx] + offset);
779 s->imdct.imdct_half(&s->imdct, s->temp, c->coeffs);
780 s->fdsp->vector_fmul_window(dst, c->prev_win, s->temp,
781 s->imdct_win, wsize >> 1);
782 memcpy(c->prev_win, s->temp + (wsize >> 1), sizeof(float)*wsize >> 1);
788 static int atrac9_decode_frame(AVCodecContext *avctx, void *data,
789 int *got_frame_ptr, AVPacket *avpkt)
793 AVFrame *frame = data;
794 ATRAC9Context *s = avctx->priv_data;
795 const int frames = FFMIN(avpkt->size / s->avg_frame_size, s->frame_count);
797 frame->nb_samples = (1 << s->frame_log2) * frames;
798 ret = ff_get_buffer(avctx, frame, 0);
802 init_get_bits8(&gb, avpkt->data, avpkt->size);
804 for (int i = 0; i < frames; i++) {
805 for (int j = 0; j < s->block_config->count; j++) {
806 ret = atrac9_decode_block(s, &gb, &s->block[j], frame, i, j);
815 return avctx->block_align;
818 static void atrac9_decode_flush(AVCodecContext *avctx)
820 ATRAC9Context *s = avctx->priv_data;
822 for (int j = 0; j < s->block_config->count; j++) {
823 ATRAC9BlockData *b = &s->block[j];
824 const int stereo = s->block_config->type[j] == ATRAC9_BLOCK_TYPE_CPE;
825 for (int i = 0; i <= stereo; i++) {
826 ATRAC9ChannelData *c = &b->channel[i];
827 memset(c->prev_win, 0, sizeof(c->prev_win));
832 static av_cold int atrac9_decode_close(AVCodecContext *avctx)
834 ATRAC9Context *s = avctx->priv_data;
836 ff_mdct_end(&s->imdct);
842 static av_cold void atrac9_init_vlc(VLC *vlc, int nb_bits, int nb_codes,
843 const uint8_t (**tab)[2],
844 unsigned *buf_offset, int offset)
846 static VLC_TYPE vlc_buf[24812][2];
848 vlc->table = &vlc_buf[*buf_offset];
849 vlc->table_allocated = FF_ARRAY_ELEMS(vlc_buf) - *buf_offset;
850 ff_init_vlc_from_lengths(vlc, nb_bits, nb_codes,
851 &(*tab)[0][1], 2, &(*tab)[0][0], 2, 1,
852 offset, INIT_VLC_STATIC_OVERLONG, NULL);
853 *buf_offset += vlc->table_size;
857 static av_cold void atrac9_init_static(void)
859 const uint8_t (*tab)[2];
862 /* Unsigned scalefactor VLCs */
864 for (int i = 1; i < 7; i++) {
865 const HuffmanCodebook *hf = &at9_huffman_sf_unsigned[i];
867 atrac9_init_vlc(&sf_vlc[0][i], ATRAC9_SF_VLC_BITS,
868 hf->size, &tab, &offset, 0);
871 /* Signed scalefactor VLCs */
873 for (int i = 2; i < 6; i++) {
874 const HuffmanCodebook *hf = &at9_huffman_sf_signed[i];
876 /* The symbols are signed integers in the range -16..15;
877 * the values in the source table are offset by 16 to make
878 * them fit into an uint8_t; the -16 reverses this shift. */
879 atrac9_init_vlc(&sf_vlc[1][i], ATRAC9_SF_VLC_BITS,
880 hf->size, &tab, &offset, -16);
883 /* Coefficient VLCs */
884 tab = at9_coeffs_tab;
885 for (int i = 0; i < 2; i++) {
886 for (int j = 2; j < 8; j++) {
887 for (int k = i; k < 4; k++) {
888 const HuffmanCodebook *hf = &at9_huffman_coeffs[i][j][k];
889 atrac9_init_vlc(&coeff_vlc[i][j][k], ATRAC9_COEFF_VLC_BITS,
890 hf->size, &tab, &offset, 0);
896 static av_cold int atrac9_decode_init(AVCodecContext *avctx)
898 static AVOnce static_table_init = AV_ONCE_INIT;
900 ATRAC9Context *s = avctx->priv_data;
901 int version, block_config_idx, superframe_idx, alloc_c_len;
905 av_lfg_init(&s->lfg, 0xFBADF00D);
907 if (avctx->block_align <= 0) {
908 av_log(avctx, AV_LOG_ERROR, "Invalid block align\n");
909 return AVERROR_INVALIDDATA;
912 if (avctx->extradata_size != 12) {
913 av_log(avctx, AV_LOG_ERROR, "Invalid extradata length!\n");
914 return AVERROR_INVALIDDATA;
917 version = AV_RL32(avctx->extradata);
919 av_log(avctx, AV_LOG_ERROR, "Unsupported version (%i)!\n", version);
920 return AVERROR_INVALIDDATA;
923 init_get_bits8(&gb, avctx->extradata + 4, avctx->extradata_size);
925 if (get_bits(&gb, 8) != 0xFE) {
926 av_log(avctx, AV_LOG_ERROR, "Incorrect magic byte!\n");
927 return AVERROR_INVALIDDATA;
930 s->samplerate_idx = get_bits(&gb, 4);
931 avctx->sample_rate = at9_tab_samplerates[s->samplerate_idx];
933 block_config_idx = get_bits(&gb, 3);
934 if (block_config_idx > 5) {
935 av_log(avctx, AV_LOG_ERROR, "Incorrect block config!\n");
936 return AVERROR_INVALIDDATA;
938 s->block_config = &at9_block_layout[block_config_idx];
940 avctx->channel_layout = s->block_config->channel_layout;
941 avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
942 avctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
944 if (get_bits1(&gb)) {
945 av_log(avctx, AV_LOG_ERROR, "Incorrect verification bit!\n");
946 return AVERROR_INVALIDDATA;
949 /* Average frame size in bytes */
950 s->avg_frame_size = get_bits(&gb, 11) + 1;
952 superframe_idx = get_bits(&gb, 2);
953 if (superframe_idx & 1) {
954 av_log(avctx, AV_LOG_ERROR, "Invalid superframe index!\n");
955 return AVERROR_INVALIDDATA;
958 s->frame_count = 1 << superframe_idx;
959 s->frame_log2 = at9_tab_sri_frame_log2[s->samplerate_idx];
961 if (ff_mdct_init(&s->imdct, s->frame_log2 + 1, 1, 1.0f / 32768.0f))
962 return AVERROR(ENOMEM);
964 s->fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT);
966 return AVERROR(ENOMEM);
969 for (int i = 0; i < (1 << s->frame_log2); i++) {
970 const int len = 1 << s->frame_log2;
971 const float sidx = ( i + 0.5f) / len;
972 const float eidx = (len - i - 0.5f) / len;
973 const float s_c = sinf(sidx*M_PI - M_PI_2)*0.5f + 0.5f;
974 const float e_c = sinf(eidx*M_PI - M_PI_2)*0.5f + 0.5f;
975 s->imdct_win[i] = s_c / ((s_c * s_c) + (e_c * e_c));
978 /* Allocation curve */
979 alloc_c_len = FF_ARRAY_ELEMS(at9_tab_b_dist);
980 for (int i = 1; i <= alloc_c_len; i++)
981 for (int j = 0; j < i; j++)
982 s->alloc_curve[i - 1][j] = at9_tab_b_dist[(j * alloc_c_len) / i];
984 ff_thread_once(&static_table_init, atrac9_init_static);
989 const AVCodec ff_atrac9_decoder = {
991 .long_name = NULL_IF_CONFIG_SMALL("ATRAC9 (Adaptive TRansform Acoustic Coding 9)"),
992 .type = AVMEDIA_TYPE_AUDIO,
993 .id = AV_CODEC_ID_ATRAC9,
994 .priv_data_size = sizeof(ATRAC9Context),
995 .init = atrac9_decode_init,
996 .close = atrac9_decode_close,
997 .decode = atrac9_decode_frame,
998 .flush = atrac9_decode_flush,
999 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
1000 .capabilities = AV_CODEC_CAP_SUBFRAMES | AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,