FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/atrac9dec.c
Date: 2021-09-22 23:55:11
Exec Total Coverage
Lines: 0 539 0.0%
Branches: 0 313 0.0%

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