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