Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * ATRAC3 compatible decoder | ||
3 | * Copyright (c) 2006-2008 Maxim Poliakovski | ||
4 | * Copyright (c) 2006-2008 Benjamin Larsson | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file | ||
25 | * ATRAC3 compatible decoder. | ||
26 | * This decoder handles Sony's ATRAC3 data. | ||
27 | * | ||
28 | * Container formats used to store ATRAC3 data: | ||
29 | * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3). | ||
30 | * | ||
31 | * To use this decoder, a calling application must supply the extradata | ||
32 | * bytes provided in the containers above. | ||
33 | */ | ||
34 | |||
35 | #include <math.h> | ||
36 | #include <stddef.h> | ||
37 | |||
38 | #include "libavutil/attributes.h" | ||
39 | #include "libavutil/float_dsp.h" | ||
40 | #include "libavutil/libm.h" | ||
41 | #include "libavutil/mem_internal.h" | ||
42 | #include "libavutil/thread.h" | ||
43 | #include "libavutil/tx.h" | ||
44 | |||
45 | #include "avcodec.h" | ||
46 | #include "bytestream.h" | ||
47 | #include "codec_internal.h" | ||
48 | #include "decode.h" | ||
49 | #include "get_bits.h" | ||
50 | |||
51 | #include "atrac.h" | ||
52 | #include "atrac3data.h" | ||
53 | |||
54 | #define MIN_CHANNELS 1 | ||
55 | #define MAX_CHANNELS 8 | ||
56 | #define MAX_JS_PAIRS 8 / 2 | ||
57 | |||
58 | #define JOINT_STEREO 0x12 | ||
59 | #define SINGLE 0x2 | ||
60 | |||
61 | #define SAMPLES_PER_FRAME 1024 | ||
62 | #define MDCT_SIZE 512 | ||
63 | |||
64 | #define ATRAC3_VLC_BITS 8 | ||
65 | |||
66 | typedef struct GainBlock { | ||
67 | AtracGainInfo g_block[4]; | ||
68 | } GainBlock; | ||
69 | |||
70 | typedef struct TonalComponent { | ||
71 | int pos; | ||
72 | int num_coefs; | ||
73 | float coef[8]; | ||
74 | } TonalComponent; | ||
75 | |||
76 | typedef struct ChannelUnit { | ||
77 | int bands_coded; | ||
78 | int num_components; | ||
79 | float prev_frame[SAMPLES_PER_FRAME]; | ||
80 | int gc_blk_switch; | ||
81 | TonalComponent components[64]; | ||
82 | GainBlock gain_block[2]; | ||
83 | |||
84 | DECLARE_ALIGNED(32, float, spectrum)[SAMPLES_PER_FRAME]; | ||
85 | DECLARE_ALIGNED(32, float, imdct_buf)[SAMPLES_PER_FRAME]; | ||
86 | |||
87 | float delay_buf1[46]; ///<qmf delay buffers | ||
88 | float delay_buf2[46]; | ||
89 | float delay_buf3[46]; | ||
90 | } ChannelUnit; | ||
91 | |||
92 | typedef struct ATRAC3Context { | ||
93 | GetBitContext gb; | ||
94 | //@{ | ||
95 | /** stream data */ | ||
96 | int coding_mode; | ||
97 | |||
98 | ChannelUnit *units; | ||
99 | //@} | ||
100 | //@{ | ||
101 | /** joint-stereo related variables */ | ||
102 | int matrix_coeff_index_prev[MAX_JS_PAIRS][4]; | ||
103 | int matrix_coeff_index_now[MAX_JS_PAIRS][4]; | ||
104 | int matrix_coeff_index_next[MAX_JS_PAIRS][4]; | ||
105 | int weighting_delay[MAX_JS_PAIRS][6]; | ||
106 | //@} | ||
107 | //@{ | ||
108 | /** data buffers */ | ||
109 | uint8_t *decoded_bytes_buffer; | ||
110 | float temp_buf[1070]; | ||
111 | //@} | ||
112 | //@{ | ||
113 | /** extradata */ | ||
114 | int scrambled_stream; | ||
115 | //@} | ||
116 | |||
117 | AtracGCContext gainc_ctx; | ||
118 | AVTXContext *mdct_ctx; | ||
119 | av_tx_fn mdct_fn; | ||
120 | void (*vector_fmul)(float *dst, const float *src0, const float *src1, | ||
121 | int len); | ||
122 | } ATRAC3Context; | ||
123 | |||
124 | static DECLARE_ALIGNED(32, float, mdct_window)[MDCT_SIZE]; | ||
125 | static VLCElem atrac3_vlc_table[7 * 1 << ATRAC3_VLC_BITS]; | ||
126 | static VLC spectral_coeff_tab[7]; | ||
127 | |||
128 | /** | ||
129 | * Regular 512 points IMDCT without overlapping, with the exception of the | ||
130 | * swapping of odd bands caused by the reverse spectra of the QMF. | ||
131 | * | ||
132 | * @param odd_band 1 if the band is an odd band | ||
133 | */ | ||
134 | 2800 | static void imlt(ATRAC3Context *q, float *input, float *output, int odd_band) | |
135 | { | ||
136 | int i; | ||
137 | |||
138 |
2/2✓ Branch 0 taken 848 times.
✓ Branch 1 taken 1952 times.
|
2800 | if (odd_band) { |
139 | /** | ||
140 | * Reverse the odd bands before IMDCT, this is an effect of the QMF | ||
141 | * transform or it gives better compression to do it this way. | ||
142 | * FIXME: It should be possible to handle this in imdct_calc | ||
143 | * for that to happen a modification of the prerotation step of | ||
144 | * all SIMD code and C code is needed. | ||
145 | * Or fix the functions before so they generate a pre reversed spectrum. | ||
146 | */ | ||
147 |
2/2✓ Branch 0 taken 108544 times.
✓ Branch 1 taken 848 times.
|
109392 | for (i = 0; i < 128; i++) |
148 | 108544 | FFSWAP(float, input[i], input[255 - i]); | |
149 | } | ||
150 | |||
151 | 2800 | q->mdct_fn(q->mdct_ctx, output, input, sizeof(float)); | |
152 | |||
153 | /* Perform windowing on the output. */ | ||
154 | 2800 | q->vector_fmul(output, output, mdct_window, MDCT_SIZE); | |
155 | 2800 | } | |
156 | |||
157 | /* | ||
158 | * indata descrambling, only used for data coming from the rm container | ||
159 | */ | ||
160 | ✗ | static int decode_bytes(const uint8_t *input, uint8_t *out, int bytes) | |
161 | { | ||
162 | int i, off; | ||
163 | uint32_t c; | ||
164 | const uint32_t *buf; | ||
165 | ✗ | uint32_t *output = (uint32_t *)out; | |
166 | |||
167 | ✗ | off = (intptr_t)input & 3; | |
168 | ✗ | buf = (const uint32_t *)(input - off); | |
169 | ✗ | if (off) | |
170 | ✗ | c = av_be2ne32((0x537F6103U >> (off * 8)) | (0x537F6103U << (32 - (off * 8)))); | |
171 | else | ||
172 | ✗ | c = av_be2ne32(0x537F6103U); | |
173 | ✗ | bytes += 3 + off; | |
174 | ✗ | for (i = 0; i < bytes / 4; i++) | |
175 | ✗ | output[i] = c ^ buf[i]; | |
176 | |||
177 | ✗ | if (off) | |
178 | ✗ | avpriv_request_sample(NULL, "Offset of %d", off); | |
179 | |||
180 | ✗ | return off; | |
181 | } | ||
182 | |||
183 | 6 | static av_cold void init_imdct_window(void) | |
184 | { | ||
185 | int i, j; | ||
186 | |||
187 | /* generate the mdct window, for details see | ||
188 | * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */ | ||
189 |
2/2✓ Branch 0 taken 768 times.
✓ Branch 1 taken 6 times.
|
774 | for (i = 0, j = 255; i < 128; i++, j--) { |
190 | 768 | float wi = sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0; | |
191 | 768 | float wj = sin(((j + 0.5) / 256.0 - 0.5) * M_PI) + 1.0; | |
192 | 768 | float w = 0.5 * (wi * wi + wj * wj); | |
193 | 768 | mdct_window[i] = mdct_window[511 - i] = wi / w; | |
194 | 768 | mdct_window[j] = mdct_window[511 - j] = wj / w; | |
195 | } | ||
196 | 6 | } | |
197 | |||
198 | 9 | static av_cold int atrac3_decode_close(AVCodecContext *avctx) | |
199 | { | ||
200 | 9 | ATRAC3Context *q = avctx->priv_data; | |
201 | |||
202 | 9 | av_freep(&q->units); | |
203 | 9 | av_freep(&q->decoded_bytes_buffer); | |
204 | |||
205 | 9 | av_tx_uninit(&q->mdct_ctx); | |
206 | |||
207 | 9 | return 0; | |
208 | } | ||
209 | |||
210 | /** | ||
211 | * Mantissa decoding | ||
212 | * | ||
213 | * @param selector which table the output values are coded with | ||
214 | * @param coding_flag constant length coding or variable length coding | ||
215 | * @param mantissas mantissa output table | ||
216 | * @param num_codes number of values to get | ||
217 | */ | ||
218 | 28209 | static void read_quant_spectral_coeffs(GetBitContext *gb, int selector, | |
219 | int coding_flag, int *mantissas, | ||
220 | int num_codes) | ||
221 | { | ||
222 | int i, code, huff_symb; | ||
223 | |||
224 |
2/2✓ Branch 0 taken 15517 times.
✓ Branch 1 taken 12692 times.
|
28209 | if (selector == 1) |
225 | 15517 | num_codes /= 2; | |
226 | |||
227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28209 times.
|
28209 | if (coding_flag != 0) { |
228 | /* constant length coding (CLC) */ | ||
229 | ✗ | int num_bits = clc_length_tab[selector]; | |
230 | |||
231 | ✗ | if (selector > 1) { | |
232 | ✗ | for (i = 0; i < num_codes; i++) { | |
233 | ✗ | if (num_bits) | |
234 | ✗ | code = get_sbits(gb, num_bits); | |
235 | else | ||
236 | ✗ | code = 0; | |
237 | ✗ | mantissas[i] = code; | |
238 | } | ||
239 | } else { | ||
240 | ✗ | for (i = 0; i < num_codes; i++) { | |
241 | ✗ | if (num_bits) | |
242 | ✗ | code = get_bits(gb, num_bits); // num_bits is always 4 in this case | |
243 | else | ||
244 | ✗ | code = 0; | |
245 | ✗ | mantissas[i * 2 ] = mantissa_clc_tab[code >> 2]; | |
246 | ✗ | mantissas[i * 2 + 1] = mantissa_clc_tab[code & 3]; | |
247 | } | ||
248 | } | ||
249 | } else { | ||
250 | /* variable length coding (VLC) */ | ||
251 |
2/2✓ Branch 0 taken 12692 times.
✓ Branch 1 taken 15517 times.
|
28209 | if (selector != 1) { |
252 |
2/2✓ Branch 0 taken 179824 times.
✓ Branch 1 taken 12692 times.
|
192516 | for (i = 0; i < num_codes; i++) { |
253 | 179824 | mantissas[i] = get_vlc2(gb, spectral_coeff_tab[selector-1].table, | |
254 | ATRAC3_VLC_BITS, 1); | ||
255 | } | ||
256 | } else { | ||
257 |
2/2✓ Branch 0 taken 234888 times.
✓ Branch 1 taken 15517 times.
|
250405 | for (i = 0; i < num_codes; i++) { |
258 | 234888 | huff_symb = get_vlc2(gb, spectral_coeff_tab[selector - 1].table, | |
259 | ATRAC3_VLC_BITS, 1); | ||
260 | 234888 | mantissas[i * 2 ] = mantissa_vlc_tab[huff_symb * 2 ]; | |
261 | 234888 | mantissas[i * 2 + 1] = mantissa_vlc_tab[huff_symb * 2 + 1]; | |
262 | } | ||
263 | } | ||
264 | } | ||
265 | 28209 | } | |
266 | |||
267 | /** | ||
268 | * Restore the quantized band spectrum coefficients | ||
269 | * | ||
270 | * @return subband count, fix for broken specification/files | ||
271 | */ | ||
272 | 1108 | static int decode_spectrum(GetBitContext *gb, float *output) | |
273 | { | ||
274 | int num_subbands, coding_mode, i, j, first, last, subband_size; | ||
275 | int subband_vlc_index[32], sf_index[32]; | ||
276 | int mantissas[128]; | ||
277 | float scale_factor; | ||
278 | |||
279 | 1108 | num_subbands = get_bits(gb, 5); // number of coded subbands | |
280 | 1108 | coding_mode = get_bits1(gb); // coding Mode: 0 - VLC/ 1-CLC | |
281 | |||
282 | /* get the VLC selector table for the subbands, 0 means not coded */ | ||
283 |
2/2✓ Branch 0 taken 28209 times.
✓ Branch 1 taken 1108 times.
|
29317 | for (i = 0; i <= num_subbands; i++) |
284 | 28209 | subband_vlc_index[i] = get_bits(gb, 3); | |
285 | |||
286 | /* read the scale factor indexes from the stream */ | ||
287 |
2/2✓ Branch 0 taken 28209 times.
✓ Branch 1 taken 1108 times.
|
29317 | for (i = 0; i <= num_subbands; i++) { |
288 |
1/2✓ Branch 0 taken 28209 times.
✗ Branch 1 not taken.
|
28209 | if (subband_vlc_index[i] != 0) |
289 | 28209 | sf_index[i] = get_bits(gb, 6); | |
290 | } | ||
291 | |||
292 |
2/2✓ Branch 0 taken 28209 times.
✓ Branch 1 taken 1108 times.
|
29317 | for (i = 0; i <= num_subbands; i++) { |
293 | 28209 | first = subband_tab[i ]; | |
294 | 28209 | last = subband_tab[i + 1]; | |
295 | |||
296 | 28209 | subband_size = last - first; | |
297 | |||
298 |
1/2✓ Branch 0 taken 28209 times.
✗ Branch 1 not taken.
|
28209 | if (subband_vlc_index[i] != 0) { |
299 | /* decode spectral coefficients for this subband */ | ||
300 | /* TODO: This can be done faster is several blocks share the | ||
301 | * same VLC selector (subband_vlc_index) */ | ||
302 | 28209 | read_quant_spectral_coeffs(gb, subband_vlc_index[i], coding_mode, | |
303 | mantissas, subband_size); | ||
304 | |||
305 | /* decode the scale factor for this subband */ | ||
306 | 28209 | scale_factor = ff_atrac_sf_table[sf_index[i]] * | |
307 | 28209 | inv_max_quant[subband_vlc_index[i]]; | |
308 | |||
309 | /* inverse quantize the coefficients */ | ||
310 |
2/2✓ Branch 0 taken 649600 times.
✓ Branch 1 taken 28209 times.
|
677809 | for (j = 0; first < last; first++, j++) |
311 | 649600 | output[first] = mantissas[j] * scale_factor; | |
312 | } else { | ||
313 | /* this subband was not coded, so zero the entire subband */ | ||
314 | ✗ | memset(output + first, 0, subband_size * sizeof(*output)); | |
315 | } | ||
316 | } | ||
317 | |||
318 | /* clear the subbands that were not coded */ | ||
319 | 1108 | first = subband_tab[i]; | |
320 | 1108 | memset(output + first, 0, (SAMPLES_PER_FRAME - first) * sizeof(*output)); | |
321 | 1108 | return num_subbands; | |
322 | } | ||
323 | |||
324 | /** | ||
325 | * Restore the quantized tonal components | ||
326 | * | ||
327 | * @param components tonal components | ||
328 | * @param num_bands number of coded bands | ||
329 | */ | ||
330 | 1108 | static int decode_tonal_components(GetBitContext *gb, | |
331 | TonalComponent *components, int num_bands) | ||
332 | { | ||
333 | int i, b, c, m; | ||
334 | int nb_components, coding_mode_selector, coding_mode; | ||
335 | int band_flags[4], mantissa[8]; | ||
336 | 1108 | int component_count = 0; | |
337 | |||
338 | 1108 | nb_components = get_bits(gb, 5); | |
339 | |||
340 | /* no tonal components */ | ||
341 |
1/2✓ Branch 0 taken 1108 times.
✗ Branch 1 not taken.
|
1108 | if (nb_components == 0) |
342 | 1108 | return 0; | |
343 | |||
344 | ✗ | coding_mode_selector = get_bits(gb, 2); | |
345 | ✗ | if (coding_mode_selector == 2) | |
346 | ✗ | return AVERROR_INVALIDDATA; | |
347 | |||
348 | ✗ | coding_mode = coding_mode_selector & 1; | |
349 | |||
350 | ✗ | for (i = 0; i < nb_components; i++) { | |
351 | int coded_values_per_component, quant_step_index; | ||
352 | |||
353 | ✗ | for (b = 0; b <= num_bands; b++) | |
354 | ✗ | band_flags[b] = get_bits1(gb); | |
355 | |||
356 | ✗ | coded_values_per_component = get_bits(gb, 3); | |
357 | |||
358 | ✗ | quant_step_index = get_bits(gb, 3); | |
359 | ✗ | if (quant_step_index <= 1) | |
360 | ✗ | return AVERROR_INVALIDDATA; | |
361 | |||
362 | ✗ | if (coding_mode_selector == 3) | |
363 | ✗ | coding_mode = get_bits1(gb); | |
364 | |||
365 | ✗ | for (b = 0; b < (num_bands + 1) * 4; b++) { | |
366 | int coded_components; | ||
367 | |||
368 | ✗ | if (band_flags[b >> 2] == 0) | |
369 | ✗ | continue; | |
370 | |||
371 | ✗ | coded_components = get_bits(gb, 3); | |
372 | |||
373 | ✗ | for (c = 0; c < coded_components; c++) { | |
374 | ✗ | TonalComponent *cmp = &components[component_count]; | |
375 | int sf_index, coded_values, max_coded_values; | ||
376 | float scale_factor; | ||
377 | |||
378 | ✗ | sf_index = get_bits(gb, 6); | |
379 | ✗ | if (component_count >= 64) | |
380 | ✗ | return AVERROR_INVALIDDATA; | |
381 | |||
382 | ✗ | cmp->pos = b * 64 + get_bits(gb, 6); | |
383 | |||
384 | ✗ | max_coded_values = SAMPLES_PER_FRAME - cmp->pos; | |
385 | ✗ | coded_values = coded_values_per_component + 1; | |
386 | ✗ | coded_values = FFMIN(max_coded_values, coded_values); | |
387 | |||
388 | ✗ | scale_factor = ff_atrac_sf_table[sf_index] * | |
389 | ✗ | inv_max_quant[quant_step_index]; | |
390 | |||
391 | ✗ | read_quant_spectral_coeffs(gb, quant_step_index, coding_mode, | |
392 | mantissa, coded_values); | ||
393 | |||
394 | ✗ | cmp->num_coefs = coded_values; | |
395 | |||
396 | /* inverse quant */ | ||
397 | ✗ | for (m = 0; m < coded_values; m++) | |
398 | ✗ | cmp->coef[m] = mantissa[m] * scale_factor; | |
399 | |||
400 | ✗ | component_count++; | |
401 | } | ||
402 | } | ||
403 | } | ||
404 | |||
405 | ✗ | return component_count; | |
406 | } | ||
407 | |||
408 | /** | ||
409 | * Decode gain parameters for the coded bands | ||
410 | * | ||
411 | * @param block the gainblock for the current band | ||
412 | * @param num_bands amount of coded bands | ||
413 | */ | ||
414 | 1108 | static int decode_gain_control(GetBitContext *gb, GainBlock *block, | |
415 | int num_bands) | ||
416 | { | ||
417 | int b, j; | ||
418 | int *level, *loc; | ||
419 | |||
420 | 1108 | AtracGainInfo *gain = block->g_block; | |
421 | |||
422 |
2/2✓ Branch 0 taken 2804 times.
✓ Branch 1 taken 1108 times.
|
3912 | for (b = 0; b <= num_bands; b++) { |
423 | 2804 | gain[b].num_points = get_bits(gb, 3); | |
424 | 2804 | level = gain[b].lev_code; | |
425 | 2804 | loc = gain[b].loc_code; | |
426 | |||
427 |
2/2✓ Branch 0 taken 874 times.
✓ Branch 1 taken 2804 times.
|
3678 | for (j = 0; j < gain[b].num_points; j++) { |
428 | 874 | level[j] = get_bits(gb, 4); | |
429 | 874 | loc[j] = get_bits(gb, 5); | |
430 |
3/4✓ Branch 0 taken 295 times.
✓ Branch 1 taken 579 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 295 times.
|
874 | if (j && loc[j] <= loc[j - 1]) |
431 | ✗ | return AVERROR_INVALIDDATA; | |
432 | } | ||
433 | } | ||
434 | |||
435 | /* Clear the unused blocks. */ | ||
436 |
2/2✓ Branch 0 taken 1628 times.
✓ Branch 1 taken 1108 times.
|
2736 | for (; b < 4 ; b++) |
437 | 1628 | gain[b].num_points = 0; | |
438 | |||
439 | 1108 | return 0; | |
440 | } | ||
441 | |||
442 | /** | ||
443 | * Combine the tonal band spectrum and regular band spectrum | ||
444 | * | ||
445 | * @param spectrum output spectrum buffer | ||
446 | * @param num_components number of tonal components | ||
447 | * @param components tonal components for this band | ||
448 | * @return position of the last tonal coefficient | ||
449 | */ | ||
450 | 1108 | static int add_tonal_components(float *spectrum, int num_components, | |
451 | TonalComponent *components) | ||
452 | { | ||
453 | 1108 | int i, j, last_pos = -1; | |
454 | float *input, *output; | ||
455 | |||
456 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
|
1108 | for (i = 0; i < num_components; i++) { |
457 | ✗ | last_pos = FFMAX(components[i].pos + components[i].num_coefs, last_pos); | |
458 | ✗ | input = components[i].coef; | |
459 | ✗ | output = &spectrum[components[i].pos]; | |
460 | |||
461 | ✗ | for (j = 0; j < components[i].num_coefs; j++) | |
462 | ✗ | output[j] += input[j]; | |
463 | } | ||
464 | |||
465 | 1108 | return last_pos; | |
466 | } | ||
467 | |||
468 | #define INTERPOLATE(old, new, nsample) \ | ||
469 | ((old) + (nsample) * 0.125 * ((new) - (old))) | ||
470 | |||
471 | 260 | static void reverse_matrixing(float *su1, float *su2, int *prev_code, | |
472 | int *curr_code) | ||
473 | { | ||
474 | int i, nsample, band; | ||
475 | float mc1_l, mc1_r, mc2_l, mc2_r; | ||
476 | |||
477 |
2/2✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 260 times.
|
1300 | for (i = 0, band = 0; band < 4 * 256; band += 256, i++) { |
478 | 1040 | int s1 = prev_code[i]; | |
479 | 1040 | int s2 = curr_code[i]; | |
480 | 1040 | nsample = band; | |
481 | |||
482 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 1002 times.
|
1040 | if (s1 != s2) { |
483 | /* Selector value changed, interpolation needed. */ | ||
484 | 38 | mc1_l = matrix_coeffs[s1 * 2 ]; | |
485 | 38 | mc1_r = matrix_coeffs[s1 * 2 + 1]; | |
486 | 38 | mc2_l = matrix_coeffs[s2 * 2 ]; | |
487 | 38 | mc2_r = matrix_coeffs[s2 * 2 + 1]; | |
488 | |||
489 | /* Interpolation is done over the first eight samples. */ | ||
490 |
2/2✓ Branch 0 taken 304 times.
✓ Branch 1 taken 38 times.
|
342 | for (; nsample < band + 8; nsample++) { |
491 | 304 | float c1 = su1[nsample]; | |
492 | 304 | float c2 = su2[nsample]; | |
493 | 304 | c2 = c1 * INTERPOLATE(mc1_l, mc2_l, nsample - band) + | |
494 | 304 | c2 * INTERPOLATE(mc1_r, mc2_r, nsample - band); | |
495 | 304 | su1[nsample] = c2; | |
496 | 304 | su2[nsample] = c1 * 2.0 - c2; | |
497 | } | ||
498 | } | ||
499 | |||
500 | /* Apply the matrix without interpolation. */ | ||
501 |
2/4✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 995 times.
✗ Branch 3 not taken.
|
1040 | switch (s2) { |
502 | 45 | case 0: /* M/S decoding */ | |
503 |
2/2✓ Branch 0 taken 11368 times.
✓ Branch 1 taken 45 times.
|
11413 | for (; nsample < band + 256; nsample++) { |
504 | 11368 | float c1 = su1[nsample]; | |
505 | 11368 | float c2 = su2[nsample]; | |
506 | 11368 | su1[nsample] = c2 * 2.0; | |
507 | 11368 | su2[nsample] = (c1 - c2) * 2.0; | |
508 | } | ||
509 | 45 | break; | |
510 | ✗ | case 1: | |
511 | ✗ | for (; nsample < band + 256; nsample++) { | |
512 | ✗ | float c1 = su1[nsample]; | |
513 | ✗ | float c2 = su2[nsample]; | |
514 | ✗ | su1[nsample] = (c1 + c2) * 2.0; | |
515 | ✗ | su2[nsample] = c2 * -2.0; | |
516 | } | ||
517 | ✗ | break; | |
518 | 995 | case 2: | |
519 | case 3: | ||
520 |
2/2✓ Branch 0 taken 254568 times.
✓ Branch 1 taken 995 times.
|
255563 | for (; nsample < band + 256; nsample++) { |
521 | 254568 | float c1 = su1[nsample]; | |
522 | 254568 | float c2 = su2[nsample]; | |
523 | 254568 | su1[nsample] = c1 + c2; | |
524 | 254568 | su2[nsample] = c1 - c2; | |
525 | } | ||
526 | 995 | break; | |
527 | 1040 | default: | |
528 | av_assert1(0); | ||
529 | } | ||
530 | } | ||
531 | 260 | } | |
532 | |||
533 | 492 | static void get_channel_weights(int index, int flag, float ch[2]) | |
534 | { | ||
535 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 439 times.
|
492 | if (index == 7) { |
536 | 53 | ch[0] = 1.0; | |
537 | 53 | ch[1] = 1.0; | |
538 | } else { | ||
539 | 439 | ch[0] = (index & 7) / 7.0; | |
540 | 439 | ch[1] = sqrt(2 - ch[0] * ch[0]); | |
541 |
2/2✓ Branch 0 taken 115 times.
✓ Branch 1 taken 324 times.
|
439 | if (flag) |
542 | 115 | FFSWAP(float, ch[0], ch[1]); | |
543 | } | ||
544 | 492 | } | |
545 | |||
546 | 260 | static void channel_weighting(float *su1, float *su2, int *p3) | |
547 | { | ||
548 | int band, nsample; | ||
549 | /* w[x][y] y=0 is left y=1 is right */ | ||
550 | float w[2][2]; | ||
551 | |||
552 |
4/4✓ Branch 0 taken 41 times.
✓ Branch 1 taken 219 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 14 times.
|
260 | if (p3[1] != 7 || p3[3] != 7) { |
553 | 246 | get_channel_weights(p3[1], p3[0], w[0]); | |
554 | 246 | get_channel_weights(p3[3], p3[2], w[1]); | |
555 | |||
556 |
2/2✓ Branch 0 taken 738 times.
✓ Branch 1 taken 246 times.
|
984 | for (band = 256; band < 4 * 256; band += 256) { |
557 |
2/2✓ Branch 0 taken 5904 times.
✓ Branch 1 taken 738 times.
|
6642 | for (nsample = band; nsample < band + 8; nsample++) { |
558 | 5904 | su1[nsample] *= INTERPOLATE(w[0][0], w[0][1], nsample - band); | |
559 | 5904 | su2[nsample] *= INTERPOLATE(w[1][0], w[1][1], nsample - band); | |
560 | } | ||
561 |
2/2✓ Branch 0 taken 183024 times.
✓ Branch 1 taken 738 times.
|
183762 | for(; nsample < band + 256; nsample++) { |
562 | 183024 | su1[nsample] *= w[1][0]; | |
563 | 183024 | su2[nsample] *= w[1][1]; | |
564 | } | ||
565 | } | ||
566 | } | ||
567 | 260 | } | |
568 | |||
569 | /** | ||
570 | * Decode a Sound Unit | ||
571 | * | ||
572 | * @param snd the channel unit to be used | ||
573 | * @param output the decoded samples before IQMF in float representation | ||
574 | * @param channel_num channel number | ||
575 | * @param coding_mode the coding mode (JOINT_STEREO or single channels) | ||
576 | */ | ||
577 | 1108 | static int decode_channel_sound_unit(ATRAC3Context *q, GetBitContext *gb, | |
578 | ChannelUnit *snd, float *output, | ||
579 | int channel_num, int coding_mode) | ||
580 | { | ||
581 | int band, ret, num_subbands, last_tonal, num_bands; | ||
582 | 1108 | GainBlock *gain1 = &snd->gain_block[ snd->gc_blk_switch]; | |
583 | 1108 | GainBlock *gain2 = &snd->gain_block[1 - snd->gc_blk_switch]; | |
584 | |||
585 |
4/4✓ Branch 0 taken 520 times.
✓ Branch 1 taken 588 times.
✓ Branch 2 taken 260 times.
✓ Branch 3 taken 260 times.
|
1108 | if (coding_mode == JOINT_STEREO && (channel_num % 2) == 1) { |
586 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 260 times.
|
260 | if (get_bits(gb, 2) != 3) { |
587 | ✗ | av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n"); | |
588 | ✗ | return AVERROR_INVALIDDATA; | |
589 | } | ||
590 | } else { | ||
591 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 848 times.
|
848 | if (get_bits(gb, 6) != 0x28) { |
592 | ✗ | av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n"); | |
593 | ✗ | return AVERROR_INVALIDDATA; | |
594 | } | ||
595 | } | ||
596 | |||
597 | /* number of coded QMF bands */ | ||
598 | 1108 | snd->bands_coded = get_bits(gb, 2); | |
599 | |||
600 | 1108 | ret = decode_gain_control(gb, gain2, snd->bands_coded); | |
601 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
|
1108 | if (ret) |
602 | ✗ | return ret; | |
603 | |||
604 | 1108 | snd->num_components = decode_tonal_components(gb, snd->components, | |
605 | snd->bands_coded); | ||
606 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
|
1108 | if (snd->num_components < 0) |
607 | ✗ | return snd->num_components; | |
608 | |||
609 | 1108 | num_subbands = decode_spectrum(gb, snd->spectrum); | |
610 | |||
611 | /* Merge the decoded spectrum and tonal components. */ | ||
612 | 1108 | last_tonal = add_tonal_components(snd->spectrum, snd->num_components, | |
613 | 1108 | snd->components); | |
614 | |||
615 | |||
616 | /* calculate number of used MLT/QMF bands according to the amount of coded | ||
617 | spectral lines */ | ||
618 | 1108 | num_bands = (subband_tab[num_subbands] - 1) >> 8; | |
619 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
|
1108 | if (last_tonal >= 0) |
620 | ✗ | num_bands = FFMAX((last_tonal + 256) >> 8, num_bands); | |
621 | |||
622 | |||
623 | /* Reconstruct time domain samples. */ | ||
624 |
2/2✓ Branch 0 taken 4432 times.
✓ Branch 1 taken 1108 times.
|
5540 | for (band = 0; band < 4; band++) { |
625 | /* Perform the IMDCT step without overlapping. */ | ||
626 |
2/2✓ Branch 0 taken 2800 times.
✓ Branch 1 taken 1632 times.
|
4432 | if (band <= num_bands) |
627 | 2800 | imlt(q, &snd->spectrum[band * 256], snd->imdct_buf, band & 1); | |
628 | else | ||
629 | 1632 | memset(snd->imdct_buf, 0, 512 * sizeof(*snd->imdct_buf)); | |
630 | |||
631 | /* gain compensation and overlapping */ | ||
632 | 4432 | ff_atrac_gain_compensation(&q->gainc_ctx, snd->imdct_buf, | |
633 | 4432 | &snd->prev_frame[band * 256], | |
634 | &gain1->g_block[band], &gain2->g_block[band], | ||
635 | 4432 | 256, &output[band * 256]); | |
636 | } | ||
637 | |||
638 | /* Swap the gain control buffers for the next frame. */ | ||
639 | 1108 | snd->gc_blk_switch ^= 1; | |
640 | |||
641 | 1108 | return 0; | |
642 | } | ||
643 | |||
644 | 554 | static int decode_frame(AVCodecContext *avctx, const uint8_t *databuf, | |
645 | float **out_samples) | ||
646 | { | ||
647 | 554 | ATRAC3Context *q = avctx->priv_data; | |
648 | int ret, i, ch; | ||
649 | uint8_t *ptr1; | ||
650 | 554 | int channels = avctx->ch_layout.nb_channels; | |
651 | |||
652 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 294 times.
|
554 | if (q->coding_mode == JOINT_STEREO) { |
653 | /* channel coupling mode */ | ||
654 | |||
655 | /* Decode sound unit pairs (channels are expected to be even). | ||
656 | * Multichannel joint stereo interleaves pairs (6ch: 2ch + 2ch + 2ch) */ | ||
657 | const uint8_t *js_databuf; | ||
658 | int js_pair, js_block_align; | ||
659 | |||
660 | 260 | js_block_align = (avctx->block_align / channels) * 2; /* block pair */ | |
661 | |||
662 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 260 times.
|
520 | for (ch = 0; ch < channels; ch = ch + 2) { |
663 | 260 | js_pair = ch/2; | |
664 | 260 | js_databuf = databuf + js_pair * js_block_align; /* align to current pair */ | |
665 | |||
666 | /* Set the bitstream reader at the start of first channel sound unit. */ | ||
667 | 260 | init_get_bits(&q->gb, | |
668 | js_databuf, js_block_align * 8); | ||
669 | |||
670 | /* decode Sound Unit 1 */ | ||
671 | 260 | ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch], | |
672 | 260 | out_samples[ch], ch, JOINT_STEREO); | |
673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
|
260 | if (ret != 0) |
674 | ✗ | return ret; | |
675 | |||
676 | /* Framedata of the su2 in the joint-stereo mode is encoded in | ||
677 | * reverse byte order so we need to swap it first. */ | ||
678 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
|
260 | if (js_databuf == q->decoded_bytes_buffer) { |
679 | ✗ | uint8_t *ptr2 = q->decoded_bytes_buffer + js_block_align - 1; | |
680 | ✗ | ptr1 = q->decoded_bytes_buffer; | |
681 | ✗ | for (i = 0; i < js_block_align / 2; i++, ptr1++, ptr2--) | |
682 | ✗ | FFSWAP(uint8_t, *ptr1, *ptr2); | |
683 | } else { | ||
684 | 260 | const uint8_t *ptr2 = js_databuf + js_block_align - 1; | |
685 |
2/2✓ Branch 0 taken 49920 times.
✓ Branch 1 taken 260 times.
|
50180 | for (i = 0; i < js_block_align; i++) |
686 | 49920 | q->decoded_bytes_buffer[i] = *ptr2--; | |
687 | } | ||
688 | |||
689 | /* Skip the sync codes (0xF8). */ | ||
690 | 260 | ptr1 = q->decoded_bytes_buffer; | |
691 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
|
260 | for (i = 4; *ptr1 == 0xF8; i++, ptr1++) { |
692 | ✗ | if (i >= js_block_align) | |
693 | ✗ | return AVERROR_INVALIDDATA; | |
694 | } | ||
695 | |||
696 | |||
697 | /* set the bitstream reader at the start of the second Sound Unit */ | ||
698 | 260 | ret = init_get_bits8(&q->gb, | |
699 | 260 | ptr1, q->decoded_bytes_buffer + js_block_align - ptr1); | |
700 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
|
260 | if (ret < 0) |
701 | ✗ | return ret; | |
702 | |||
703 | /* Fill the Weighting coeffs delay buffer */ | ||
704 | 260 | memmove(q->weighting_delay[js_pair], &q->weighting_delay[js_pair][2], | |
705 | 4 * sizeof(*q->weighting_delay[js_pair])); | ||
706 | 260 | q->weighting_delay[js_pair][4] = get_bits1(&q->gb); | |
707 | 260 | q->weighting_delay[js_pair][5] = get_bits(&q->gb, 3); | |
708 | |||
709 |
2/2✓ Branch 0 taken 1040 times.
✓ Branch 1 taken 260 times.
|
1300 | for (i = 0; i < 4; i++) { |
710 | 1040 | q->matrix_coeff_index_prev[js_pair][i] = q->matrix_coeff_index_now[js_pair][i]; | |
711 | 1040 | q->matrix_coeff_index_now[js_pair][i] = q->matrix_coeff_index_next[js_pair][i]; | |
712 | 1040 | q->matrix_coeff_index_next[js_pair][i] = get_bits(&q->gb, 2); | |
713 | } | ||
714 | |||
715 | /* Decode Sound Unit 2. */ | ||
716 | 260 | ret = decode_channel_sound_unit(q, &q->gb, &q->units[ch+1], | |
717 | 260 | out_samples[ch+1], ch+1, JOINT_STEREO); | |
718 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 260 times.
|
260 | if (ret != 0) |
719 | ✗ | return ret; | |
720 | |||
721 | /* Reconstruct the channel coefficients. */ | ||
722 | 260 | reverse_matrixing(out_samples[ch], out_samples[ch+1], | |
723 | 260 | q->matrix_coeff_index_prev[js_pair], | |
724 | 260 | q->matrix_coeff_index_now[js_pair]); | |
725 | |||
726 | 260 | channel_weighting(out_samples[ch], out_samples[ch+1], q->weighting_delay[js_pair]); | |
727 | } | ||
728 | } else { | ||
729 | /* single channels */ | ||
730 | /* Decode the channel sound units. */ | ||
731 |
2/2✓ Branch 0 taken 588 times.
✓ Branch 1 taken 294 times.
|
882 | for (i = 0; i < channels; i++) { |
732 | /* Set the bitstream reader at the start of a channel sound unit. */ | ||
733 | 588 | init_get_bits(&q->gb, | |
734 | 588 | databuf + i * avctx->block_align / channels, | |
735 | 588 | avctx->block_align * 8 / channels); | |
736 | |||
737 | 588 | ret = decode_channel_sound_unit(q, &q->gb, &q->units[i], | |
738 | 588 | out_samples[i], i, q->coding_mode); | |
739 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 588 times.
|
588 | if (ret != 0) |
740 | ✗ | return ret; | |
741 | } | ||
742 | } | ||
743 | |||
744 | /* Apply the iQMF synthesis filter. */ | ||
745 |
2/2✓ Branch 0 taken 1108 times.
✓ Branch 1 taken 554 times.
|
1662 | for (i = 0; i < channels; i++) { |
746 | 1108 | float *p1 = out_samples[i]; | |
747 | 1108 | float *p2 = p1 + 256; | |
748 | 1108 | float *p3 = p2 + 256; | |
749 | 1108 | float *p4 = p3 + 256; | |
750 | 1108 | ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf); | |
751 | 1108 | ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf); | |
752 | 1108 | ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf); | |
753 | } | ||
754 | |||
755 | 554 | return 0; | |
756 | } | ||
757 | |||
758 | ✗ | static int al_decode_frame(AVCodecContext *avctx, const uint8_t *databuf, | |
759 | int size, float **out_samples) | ||
760 | { | ||
761 | ✗ | ATRAC3Context *q = avctx->priv_data; | |
762 | ✗ | int channels = avctx->ch_layout.nb_channels; | |
763 | int ret, i; | ||
764 | |||
765 | /* Set the bitstream reader at the start of a channel sound unit. */ | ||
766 | ✗ | init_get_bits(&q->gb, databuf, size * 8); | |
767 | /* single channels */ | ||
768 | /* Decode the channel sound units. */ | ||
769 | ✗ | for (i = 0; i < channels; i++) { | |
770 | ✗ | ret = decode_channel_sound_unit(q, &q->gb, &q->units[i], | |
771 | ✗ | out_samples[i], i, q->coding_mode); | |
772 | ✗ | if (ret != 0) | |
773 | ✗ | return ret; | |
774 | ✗ | while (i < channels && get_bits_left(&q->gb) > 6 && show_bits(&q->gb, 6) != 0x28) { | |
775 | ✗ | skip_bits(&q->gb, 1); | |
776 | } | ||
777 | } | ||
778 | |||
779 | /* Apply the iQMF synthesis filter. */ | ||
780 | ✗ | for (i = 0; i < channels; i++) { | |
781 | ✗ | float *p1 = out_samples[i]; | |
782 | ✗ | float *p2 = p1 + 256; | |
783 | ✗ | float *p3 = p2 + 256; | |
784 | ✗ | float *p4 = p3 + 256; | |
785 | ✗ | ff_atrac_iqmf(p1, p2, 256, p1, q->units[i].delay_buf1, q->temp_buf); | |
786 | ✗ | ff_atrac_iqmf(p4, p3, 256, p3, q->units[i].delay_buf2, q->temp_buf); | |
787 | ✗ | ff_atrac_iqmf(p1, p3, 512, p1, q->units[i].delay_buf3, q->temp_buf); | |
788 | } | ||
789 | |||
790 | ✗ | return 0; | |
791 | } | ||
792 | |||
793 | 557 | static int atrac3_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
794 | int *got_frame_ptr, AVPacket *avpkt) | ||
795 | { | ||
796 | 557 | const uint8_t *buf = avpkt->data; | |
797 | 557 | int buf_size = avpkt->size; | |
798 | 557 | ATRAC3Context *q = avctx->priv_data; | |
799 | int ret; | ||
800 | const uint8_t *databuf; | ||
801 | |||
802 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 554 times.
|
557 | if (buf_size < avctx->block_align) { |
803 | 3 | av_log(avctx, AV_LOG_ERROR, | |
804 | "Frame too small (%d bytes). Truncated file?\n", buf_size); | ||
805 | 3 | return AVERROR_INVALIDDATA; | |
806 | } | ||
807 | |||
808 | /* get output buffer */ | ||
809 | 554 | frame->nb_samples = SAMPLES_PER_FRAME; | |
810 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 554 times.
|
554 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
811 | ✗ | return ret; | |
812 | |||
813 | /* Check if we need to descramble and what buffer to pass on. */ | ||
814 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 554 times.
|
554 | if (q->scrambled_stream) { |
815 | ✗ | decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align); | |
816 | ✗ | databuf = q->decoded_bytes_buffer; | |
817 | } else { | ||
818 | 554 | databuf = buf; | |
819 | } | ||
820 | |||
821 | 554 | ret = decode_frame(avctx, databuf, (float **)frame->extended_data); | |
822 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 554 times.
|
554 | if (ret) { |
823 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n"); | |
824 | ✗ | return ret; | |
825 | } | ||
826 | |||
827 | 554 | *got_frame_ptr = 1; | |
828 | |||
829 | 554 | return avctx->block_align; | |
830 | } | ||
831 | |||
832 | ✗ | static int atrac3al_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
833 | int *got_frame_ptr, AVPacket *avpkt) | ||
834 | { | ||
835 | int ret; | ||
836 | |||
837 | ✗ | frame->nb_samples = SAMPLES_PER_FRAME; | |
838 | ✗ | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) | |
839 | ✗ | return ret; | |
840 | |||
841 | ✗ | ret = al_decode_frame(avctx, avpkt->data, avpkt->size, | |
842 | ✗ | (float **)frame->extended_data); | |
843 | ✗ | if (ret) { | |
844 | ✗ | av_log(avctx, AV_LOG_ERROR, "Frame decoding error!\n"); | |
845 | ✗ | return ret; | |
846 | } | ||
847 | |||
848 | ✗ | *got_frame_ptr = 1; | |
849 | |||
850 | ✗ | return avpkt->size; | |
851 | } | ||
852 | |||
853 | 6 | static av_cold void atrac3_init_static_data(void) | |
854 | { | ||
855 | 6 | VLCElem *table = atrac3_vlc_table; | |
856 | 6 | const uint8_t (*hufftabs)[2] = atrac3_hufftabs; | |
857 | int i; | ||
858 | |||
859 | 6 | init_imdct_window(); | |
860 | 6 | ff_atrac_generate_tables(); | |
861 | |||
862 | /* Initialize the VLC tables. */ | ||
863 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 6 times.
|
48 | for (i = 0; i < 7; i++) { |
864 | 42 | spectral_coeff_tab[i].table = table; | |
865 | 42 | spectral_coeff_tab[i].table_allocated = 256; | |
866 | 42 | ff_vlc_init_from_lengths(&spectral_coeff_tab[i], ATRAC3_VLC_BITS, huff_tab_sizes[i], | |
867 | 42 | &hufftabs[0][1], 2, | |
868 | 42 | &hufftabs[0][0], 2, 1, | |
869 | -31, VLC_INIT_USE_STATIC, NULL); | ||
870 | 42 | hufftabs += huff_tab_sizes[i]; | |
871 | 42 | table += 256; | |
872 | } | ||
873 | 6 | } | |
874 | |||
875 | 9 | static av_cold int atrac3_decode_init(AVCodecContext *avctx) | |
876 | { | ||
877 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
878 | int i, js_pair, ret; | ||
879 | int version, delay, samples_per_frame, frame_factor; | ||
880 | 9 | const uint8_t *edata_ptr = avctx->extradata; | |
881 | 9 | ATRAC3Context *q = avctx->priv_data; | |
882 | AVFloatDSPContext *fdsp; | ||
883 | 9 | float scale = 1.0 / 32768; | |
884 | 9 | int channels = avctx->ch_layout.nb_channels; | |
885 | |||
886 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | if (channels < MIN_CHANNELS || channels > MAX_CHANNELS) { |
887 | ✗ | av_log(avctx, AV_LOG_ERROR, "Channel configuration error!\n"); | |
888 | ✗ | return AVERROR(EINVAL); | |
889 | } | ||
890 | |||
891 | /* Take care of the codec-specific extradata. */ | ||
892 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (avctx->codec_id == AV_CODEC_ID_ATRAC3AL) { |
893 | ✗ | version = 4; | |
894 | ✗ | samples_per_frame = SAMPLES_PER_FRAME * channels; | |
895 | ✗ | delay = 0x88E; | |
896 | ✗ | q->coding_mode = SINGLE; | |
897 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
9 | } else if (avctx->extradata_size == 14) { |
898 | /* Parse the extradata, WAV format */ | ||
899 | 9 | av_log(avctx, AV_LOG_DEBUG, "[0-1] %d\n", | |
900 | bytestream_get_le16(&edata_ptr)); // Unknown value always 1 | ||
901 | 9 | edata_ptr += 4; // samples per channel | |
902 | 9 | q->coding_mode = bytestream_get_le16(&edata_ptr); | |
903 | 9 | av_log(avctx, AV_LOG_DEBUG,"[8-9] %d\n", | |
904 | bytestream_get_le16(&edata_ptr)); //Dupe of coding mode | ||
905 | 9 | frame_factor = bytestream_get_le16(&edata_ptr); // Unknown always 1 | |
906 | 9 | av_log(avctx, AV_LOG_DEBUG,"[12-13] %d\n", | |
907 | bytestream_get_le16(&edata_ptr)); // Unknown always 0 | ||
908 | |||
909 | /* setup */ | ||
910 | 9 | samples_per_frame = SAMPLES_PER_FRAME * channels; | |
911 | 9 | version = 4; | |
912 | 9 | delay = 0x88E; | |
913 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 7 times.
|
9 | q->coding_mode = q->coding_mode ? JOINT_STEREO : SINGLE; |
914 | 9 | q->scrambled_stream = 0; | |
915 | |||
916 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | if (avctx->block_align != 96 * channels * frame_factor && |
917 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | avctx->block_align != 152 * channels * frame_factor && |
918 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | avctx->block_align != 192 * channels * frame_factor) { |
919 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown frame/channel/frame_factor " | |
920 | "configuration %d/%d/%d\n", avctx->block_align, | ||
921 | channels, frame_factor); | ||
922 | ✗ | return AVERROR_INVALIDDATA; | |
923 | } | ||
924 | ✗ | } else if (avctx->extradata_size == 12 || avctx->extradata_size == 10) { | |
925 | /* Parse the extradata, RM format. */ | ||
926 | ✗ | version = bytestream_get_be32(&edata_ptr); | |
927 | ✗ | samples_per_frame = bytestream_get_be16(&edata_ptr); | |
928 | ✗ | delay = bytestream_get_be16(&edata_ptr); | |
929 | ✗ | q->coding_mode = bytestream_get_be16(&edata_ptr); | |
930 | ✗ | q->scrambled_stream = 1; | |
931 | |||
932 | } else { | ||
933 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown extradata size %d.\n", | |
934 | avctx->extradata_size); | ||
935 | ✗ | return AVERROR(EINVAL); | |
936 | } | ||
937 | |||
938 | /* Check the extradata */ | ||
939 | |||
940 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (version != 4) { |
941 | ✗ | av_log(avctx, AV_LOG_ERROR, "Version %d != 4.\n", version); | |
942 | ✗ | return AVERROR_INVALIDDATA; | |
943 | } | ||
944 | |||
945 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (samples_per_frame != SAMPLES_PER_FRAME * channels) { |
946 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown amount of samples per frame %d.\n", | |
947 | samples_per_frame); | ||
948 | ✗ | return AVERROR_INVALIDDATA; | |
949 | } | ||
950 | |||
951 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (delay != 0x88E) { |
952 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown amount of delay %x != 0x88E.\n", | |
953 | delay); | ||
954 | ✗ | return AVERROR_INVALIDDATA; | |
955 | } | ||
956 | |||
957 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | if (q->coding_mode == SINGLE) |
958 | 7 | av_log(avctx, AV_LOG_DEBUG, "Single channels detected.\n"); | |
959 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | else if (q->coding_mode == JOINT_STEREO) { |
960 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (channels % 2 == 1) { /* Joint stereo channels must be even */ |
961 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid joint stereo channel configuration.\n"); | |
962 | ✗ | return AVERROR_INVALIDDATA; | |
963 | } | ||
964 | 2 | av_log(avctx, AV_LOG_DEBUG, "Joint stereo detected.\n"); | |
965 | } else { | ||
966 | ✗ | av_log(avctx, AV_LOG_ERROR, "Unknown channel coding mode %x!\n", | |
967 | q->coding_mode); | ||
968 | ✗ | return AVERROR_INVALIDDATA; | |
969 | } | ||
970 | |||
971 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | if (avctx->block_align > 4096 || avctx->block_align <= 0) |
972 | ✗ | return AVERROR(EINVAL); | |
973 | |||
974 | 9 | q->decoded_bytes_buffer = av_mallocz(FFALIGN(avctx->block_align, 4) + | |
975 | AV_INPUT_BUFFER_PADDING_SIZE); | ||
976 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!q->decoded_bytes_buffer) |
977 | ✗ | return AVERROR(ENOMEM); | |
978 | |||
979 | 9 | avctx->sample_fmt = AV_SAMPLE_FMT_FLTP; | |
980 | |||
981 | /* initialize the MDCT transform */ | ||
982 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
|
9 | if ((ret = av_tx_init(&q->mdct_ctx, &q->mdct_fn, AV_TX_FLOAT_MDCT, 1, 256, |
983 | &scale, AV_TX_FULL_IMDCT)) < 0) { | ||
984 | ✗ | av_log(avctx, AV_LOG_ERROR, "Error initializing MDCT\n"); | |
985 | ✗ | return ret; | |
986 | } | ||
987 | |||
988 | /* init the joint-stereo decoding data */ | ||
989 |
2/2✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9 times.
|
45 | for (js_pair = 0; js_pair < MAX_JS_PAIRS; js_pair++) { |
990 | 36 | q->weighting_delay[js_pair][0] = 0; | |
991 | 36 | q->weighting_delay[js_pair][1] = 7; | |
992 | 36 | q->weighting_delay[js_pair][2] = 0; | |
993 | 36 | q->weighting_delay[js_pair][3] = 7; | |
994 | 36 | q->weighting_delay[js_pair][4] = 0; | |
995 | 36 | q->weighting_delay[js_pair][5] = 7; | |
996 | |||
997 |
2/2✓ Branch 0 taken 144 times.
✓ Branch 1 taken 36 times.
|
180 | for (i = 0; i < 4; i++) { |
998 | 144 | q->matrix_coeff_index_prev[js_pair][i] = 3; | |
999 | 144 | q->matrix_coeff_index_now[js_pair][i] = 3; | |
1000 | 144 | q->matrix_coeff_index_next[js_pair][i] = 3; | |
1001 | } | ||
1002 | } | ||
1003 | |||
1004 | 9 | ff_atrac_init_gain_compensation(&q->gainc_ctx, 4, 3); | |
1005 | 9 | fdsp = avpriv_float_dsp_alloc(avctx->flags & AV_CODEC_FLAG_BITEXACT); | |
1006 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!fdsp) |
1007 | ✗ | return AVERROR(ENOMEM); | |
1008 | 9 | q->vector_fmul = fdsp->vector_fmul; | |
1009 | 9 | av_free(fdsp); | |
1010 | |||
1011 | 9 | q->units = av_calloc(channels, sizeof(*q->units)); | |
1012 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!q->units) |
1013 | ✗ | return AVERROR(ENOMEM); | |
1014 | |||
1015 | 9 | ff_thread_once(&init_static_once, atrac3_init_static_data); | |
1016 | |||
1017 | 9 | return 0; | |
1018 | } | ||
1019 | |||
1020 | const FFCodec ff_atrac3_decoder = { | ||
1021 | .p.name = "atrac3", | ||
1022 | CODEC_LONG_NAME("ATRAC3 (Adaptive TRansform Acoustic Coding 3)"), | ||
1023 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
1024 | .p.id = AV_CODEC_ID_ATRAC3, | ||
1025 | .priv_data_size = sizeof(ATRAC3Context), | ||
1026 | .init = atrac3_decode_init, | ||
1027 | .close = atrac3_decode_close, | ||
1028 | FF_CODEC_DECODE_CB(atrac3_decode_frame), | ||
1029 | .p.capabilities = | ||
1030 | #if FF_API_SUBFRAMES | ||
1031 | AV_CODEC_CAP_SUBFRAMES | | ||
1032 | #endif | ||
1033 | AV_CODEC_CAP_DR1, | ||
1034 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, | ||
1035 | AV_SAMPLE_FMT_NONE }, | ||
1036 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1037 | }; | ||
1038 | |||
1039 | const FFCodec ff_atrac3al_decoder = { | ||
1040 | .p.name = "atrac3al", | ||
1041 | CODEC_LONG_NAME("ATRAC3 AL (Adaptive TRansform Acoustic Coding 3 Advanced Lossless)"), | ||
1042 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
1043 | .p.id = AV_CODEC_ID_ATRAC3AL, | ||
1044 | .priv_data_size = sizeof(ATRAC3Context), | ||
1045 | .init = atrac3_decode_init, | ||
1046 | .close = atrac3_decode_close, | ||
1047 | FF_CODEC_DECODE_CB(atrac3al_decode_frame), | ||
1048 | .p.capabilities = | ||
1049 | #if FF_API_SUBFRAMES | ||
1050 | AV_CODEC_CAP_SUBFRAMES | | ||
1051 | #endif | ||
1052 | AV_CODEC_CAP_DR1, | ||
1053 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLTP, | ||
1054 | AV_SAMPLE_FMT_NONE }, | ||
1055 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
1056 | }; | ||
1057 |