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