2 * Atrac 3 compatible decoder
3 * Copyright (c) 2006-2008 Maxim Poliakovski
4 * Copyright (c) 2006-2008 Benjamin Larsson
6 * This file is part of FFmpeg.
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.
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.
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
24 * @file libavcodec/atrac3.c
25 * Atrac 3 compatible decoder.
26 * This decoder handles Sony's ATRAC3 data.
28 * Container formats used to store atrac 3 data:
29 * RealMedia (.rm), RIFF WAV (.wav, .at3), Sony OpenMG (.oma, .aa3).
31 * To use this decoder, a calling application must supply the extradata
32 * bytes provided in the containers above.
40 #include "atrac3data.h"
41 #include "atrac3data_fixed.h"
42 #include "fixp_math.h"
44 #define JOINT_STEREO 0x12
52 /* FFMAX/MIN/SWAP and av_clip were taken from libavutil/common.h */
53 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
54 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
55 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
57 static VLC spectral_coeff_tab
[7];
58 static int32_t qmf_window
[48] IBSS_ATTR
;
59 static int32_t atrac3_spectrum
[2][1024] IBSS_ATTR
__attribute__((aligned(16)));
60 static int32_t atrac3_IMDCT_buf
[2][ 512] IBSS_ATTR
__attribute__((aligned(16)));
61 static int32_t atrac3_prevFrame
[2][1024] IBSS_ATTR
;
62 static channel_unit channel_units
[2] IBSS_ATTR_LARGE_IRAM
;
66 * Matrixing within quadrature mirror synthesis filter.
68 * @param p3 output buffer
69 * @param inlo lower part of spectrum
70 * @param inhi higher part of spectrum
71 * @param nIn size of spectrum buffer
76 atrac3_iqmf_matrixing(int32_t *p3
,
82 atrac3_iqmf_matrixing(int32_t *p3
,
88 for(i
=0; i
<nIn
; i
+=2){
89 p3
[2*i
+0] = inlo
[i
] + inhi
[i
];
90 p3
[2*i
+1] = inlo
[i
] - inhi
[i
];
91 p3
[2*i
+2] = inlo
[i
+1] + inhi
[i
+1];
92 p3
[2*i
+3] = inlo
[i
+1] - inhi
[i
+1];
99 * Matrixing within quadrature mirror synthesis filter.
101 * @param out output buffer
102 * @param in input buffer
103 * @param win windowing coefficients
104 * @param nIn size of spectrum buffer
105 * Reference implementation:
107 * for (j = nIn; j != 0; j--) {
108 * s1 = fixmul32(in[0], win[0]);
109 * s2 = fixmul32(in[1], win[1]);
110 * for (i = 2; i < 48; i += 2) {
111 * s1 += fixmul31(in[i ], win[i ]);
112 * s2 += fixmul31(in[i+1], win[i+1]);
123 atrac3_iqmf_dewindowing(int32_t *out
,
129 atrac3_iqmf_dewindowing(int32_t *out
,
134 int32_t i
, j
, s1
, s2
;
136 for (j
= nIn
; j
!= 0; j
--) {
139 s1
= fixmul31(win
[i
], in
[i
]); i
++;
140 s2
= fixmul31(win
[i
], in
[i
]); i
++;
141 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
142 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
143 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
144 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
145 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
146 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
148 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
149 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
150 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
151 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
152 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
153 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
154 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
155 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
157 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
158 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
159 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
160 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
161 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
162 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
163 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
164 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
166 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
167 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
168 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
169 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
170 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
171 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
172 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
173 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
175 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
176 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
177 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
178 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
179 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
180 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
181 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
182 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
184 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
185 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
186 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
187 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
188 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
189 s2
+= fixmul31(win
[i
], in
[i
]); i
++;
190 s1
+= fixmul31(win
[i
], in
[i
]); i
++;
191 s2
+= fixmul31(win
[i
], in
[i
]);
206 * @param buffer sample buffer
207 * @param win window coefficients
211 atrac3_imdct_windowing(int32_t *buffer
,
215 /* win[0..127] = win[511..384], win[128..383] = 1 */
216 for(i
= 0; i
<128; i
++) {
217 buffer
[ i
] = fixmul31(win
[i
], buffer
[ i
]);
218 buffer
[511-i
] = fixmul31(win
[i
], buffer
[511-i
]);
224 * Quadrature mirror synthesis filter.
226 * @param inlo lower part of spectrum
227 * @param inhi higher part of spectrum
228 * @param nIn size of spectrum buffer
229 * @param pOut out buffer
230 * @param delayBuf delayBuf buffer
231 * @param temp temp buffer
234 static void iqmf (int32_t *inlo
, int32_t *inhi
, unsigned int nIn
, int32_t *pOut
, int32_t *delayBuf
, int32_t *temp
)
236 /* Restore the delay buffer */
237 memcpy(temp
, delayBuf
, 46*sizeof(int32_t));
239 /* loop1: matrixing */
240 atrac3_iqmf_matrixing(temp
+ 46, inlo
, inhi
, nIn
);
242 /* loop2: dewindowing */
243 atrac3_iqmf_dewindowing(pOut
, temp
, qmf_window
, nIn
);
245 /* Save the delay buffer */
246 memcpy(delayBuf
, temp
+ (nIn
<< 1), 46*sizeof(int32_t));
251 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
252 * caused by the reverse spectra of the QMF.
254 * @param pInput input
255 * @param pOutput output
256 * @param odd_band 1 if the band is an odd band
259 static void IMLT(int32_t *pInput
, int32_t *pOutput
)
261 /* Apply the imdct. */
262 ff_imdct_calc(9, pOutput
, pInput
);
265 atrac3_imdct_windowing(pOutput
, window_lookup
);
270 * Atrac 3 indata descrambling, only used for data coming from the rm container
272 * @param in pointer to 8 bit array of indata
273 * @param bits amount of bits
274 * @param out pointer to 8 bit array of outdata
277 static int decode_bytes(const uint8_t* inbuffer
, uint8_t* out
, int bytes
){
281 uint32_t* obuf
= (uint32_t*) out
;
283 #if ((defined(TEST) || defined(SIMULATOR)) && !defined(CPU_ARM))
284 off
= 0; /* no check for memory alignment of inbuffer */
286 off
= (intptr_t)inbuffer
& 3;
288 buf
= (const uint32_t*) (inbuffer
- off
);
290 c
= be2me_32((0x537F6103 >> (off
*8)) | (0x537F6103 << (32-(off
*8))));
292 for (i
= 0; i
< bytes
/4; i
++)
293 obuf
[i
] = c
^ buf
[i
];
299 static void init_atrac3_transforms(void)
304 /* Generate the mdct window, for details see
305 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
307 /* mdct window had been generated and saved as a lookup table in atrac3data_fixed.h */
309 /* Generate the QMF window. */
310 for (i
=0 ; i
<24; i
++) {
311 s
= qmf_48tap_half_fix
[i
] << 1;
313 qmf_window
[47 - i
] = s
;
321 * @param gb the GetBit context
322 * @param selector what table is the output values coded with
323 * @param codingFlag constant length coding or variable length coding
324 * @param mantissas mantissa output table
325 * @param numCodes amount of values to get
328 static void readQuantSpectralCoeffs (GetBitContext
*gb
, int selector
, int codingFlag
, int* mantissas
, int numCodes
)
330 int numBits
, cnt
, code
, huffSymb
;
335 if (codingFlag
!= 0) {
336 /* constant length coding (CLC) */
337 numBits
= CLCLengthTab
[selector
];
340 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
342 code
= get_sbits(gb
, numBits
);
345 mantissas
[cnt
] = code
;
348 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
350 code
= get_bits(gb
, numBits
); /* numBits is always 4 in this case */
353 mantissas
[cnt
*2] = seTab_0
[code
>> 2];
354 mantissas
[cnt
*2+1] = seTab_0
[code
& 3];
358 /* variable length coding (VLC) */
360 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
361 huffSymb
= get_vlc2(gb
, spectral_coeff_tab
[selector
-1].table
, spectral_coeff_tab
[selector
-1].bits
, 3);
363 code
= huffSymb
>> 1;
366 mantissas
[cnt
] = code
;
369 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
370 huffSymb
= get_vlc2(gb
, spectral_coeff_tab
[selector
-1].table
, spectral_coeff_tab
[selector
-1].bits
, 3);
371 mantissas
[cnt
*2] = decTable1
[huffSymb
*2];
372 mantissas
[cnt
*2+1] = decTable1
[huffSymb
*2+1];
380 * Requantize the spectrum.
382 * @param *mantissas pointer to mantissas for each spectral line
383 * @param pOut requantized band spectrum
384 * @param first first spectral line in subband
385 * @param last last spectral line in subband
386 * @param SF scalefactor for all spectral lines of this band
389 static void inverseQuantizeSpectrum(int *mantissas
, int32_t *pOut
,
390 int32_t first
, int32_t last
, int32_t SF
)
392 int *pIn
= mantissas
;
394 /* Inverse quantize the coefficients. */
396 /* Odd band - Reverse coefficients */
398 pOut
[last
--] = fixmul16(*pIn
++, SF
);
399 pOut
[last
--] = fixmul16(*pIn
++, SF
);
400 pOut
[last
--] = fixmul16(*pIn
++, SF
);
401 pOut
[last
--] = fixmul16(*pIn
++, SF
);
402 pOut
[last
--] = fixmul16(*pIn
++, SF
);
403 pOut
[last
--] = fixmul16(*pIn
++, SF
);
404 pOut
[last
--] = fixmul16(*pIn
++, SF
);
405 pOut
[last
--] = fixmul16(*pIn
++, SF
);
406 } while (last
>first
);
408 /* Even band - Do not reverse coefficients */
410 pOut
[first
++] = fixmul16(*pIn
++, SF
);
411 pOut
[first
++] = fixmul16(*pIn
++, SF
);
412 pOut
[first
++] = fixmul16(*pIn
++, SF
);
413 pOut
[first
++] = fixmul16(*pIn
++, SF
);
414 pOut
[first
++] = fixmul16(*pIn
++, SF
);
415 pOut
[first
++] = fixmul16(*pIn
++, SF
);
416 pOut
[first
++] = fixmul16(*pIn
++, SF
);
417 pOut
[first
++] = fixmul16(*pIn
++, SF
);
418 } while (first
<last
);
424 * Restore the quantized band spectrum coefficients
426 * @param gb the GetBit context
427 * @param pOut decoded band spectrum
428 * @return outSubbands subband counter, fix for broken specification/files
431 int decodeSpectrum (GetBitContext
*gb
, int32_t *pOut
) ICODE_ATTR_LARGE_IRAM
;
432 int decodeSpectrum (GetBitContext
*gb
, int32_t *pOut
)
434 int numSubbands
, codingMode
, cnt
, first
, last
, subbWidth
;
435 int subband_vlc_index
[32], SF_idxs
[32];
439 numSubbands
= get_bits(gb
, 5); /* number of coded subbands */
440 codingMode
= get_bits1(gb
); /* coding Mode: 0 - VLC/ 1-CLC */
442 /* Get the VLC selector table for the subbands, 0 means not coded. */
443 for (cnt
= 0; cnt
<= numSubbands
; cnt
++)
444 subband_vlc_index
[cnt
] = get_bits(gb
, 3);
446 /* Read the scale factor indexes from the stream. */
447 for (cnt
= 0; cnt
<= numSubbands
; cnt
++) {
448 if (subband_vlc_index
[cnt
] != 0)
449 SF_idxs
[cnt
] = get_bits(gb
, 6);
452 for (cnt
= 0; cnt
<= numSubbands
; cnt
++) {
453 first
= subbandTab
[cnt
];
454 last
= subbandTab
[cnt
+1];
456 subbWidth
= last
- first
;
458 if (subband_vlc_index
[cnt
] != 0) {
459 /* Decode spectral coefficients for this subband. */
460 /* TODO: This can be done faster is several blocks share the
461 * same VLC selector (subband_vlc_index) */
462 readQuantSpectralCoeffs (gb
, subband_vlc_index
[cnt
], codingMode
, mantissas
, subbWidth
);
464 /* Decode the scale factor for this subband. */
465 SF
= fixmul31(SFTable_fixed
[SF_idxs
[cnt
]], iMaxQuant_fix
[subband_vlc_index
[cnt
]]);
466 /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample
467 * representation. Needed for higher accuracy in internal calculations as
468 * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH
472 /* Inverse quantize the coefficients. */
473 inverseQuantizeSpectrum(mantissas
, pOut
, first
, last
, SF
);
476 /* This subband was not coded, so zero the entire subband. */
477 memset(pOut
+first
, 0, subbWidth
*sizeof(int32_t));
481 /* Clear the subbands that were not coded. */
482 first
= subbandTab
[cnt
];
483 memset(pOut
+first
, 0, (1024 - first
) * sizeof(int32_t));
489 * Restore the quantized tonal components
491 * @param gb the GetBit context
492 * @param pComponent tone component
493 * @param numBands amount of coded bands
496 static int decodeTonalComponents (GetBitContext
*gb
, tonal_component
*pComponent
, int numBands
)
499 int components
, coding_mode_selector
, coding_mode
, coded_values_per_component
;
500 int sfIndx
, coded_values
, max_coded_values
, quant_step_index
, coded_components
;
501 int band_flags
[4], mantissa
[8];
504 int component_count
= 0;
506 components
= get_bits(gb
,5);
508 /* no tonal components */
512 coding_mode_selector
= get_bits(gb
,2);
513 if (coding_mode_selector
== 2)
516 coding_mode
= coding_mode_selector
& 1;
518 for (i
= 0; i
< components
; i
++) {
519 for (cnt
= 0; cnt
<= numBands
; cnt
++)
520 band_flags
[cnt
] = get_bits1(gb
);
522 coded_values_per_component
= get_bits(gb
,3);
524 quant_step_index
= get_bits(gb
,3);
525 if (quant_step_index
<= 1)
528 if (coding_mode_selector
== 3)
529 coding_mode
= get_bits1(gb
);
531 for (j
= 0; j
< (numBands
+ 1) * 4; j
++) {
532 if (band_flags
[j
>> 2] == 0)
535 coded_components
= get_bits(gb
,3);
537 for (k
=0; k
<coded_components
; k
++) {
538 sfIndx
= get_bits(gb
,6);
539 pComponent
[component_count
].pos
= j
* 64 + (get_bits(gb
,6));
540 max_coded_values
= 1024 - pComponent
[component_count
].pos
;
541 coded_values
= coded_values_per_component
+ 1;
542 coded_values
= FFMIN(max_coded_values
,coded_values
);
544 scalefactor
= fixmul31(SFTable_fixed
[sfIndx
], iMaxQuant_fix
[quant_step_index
]);
545 /* Remark: Hardcoded hack to add 2 bits (empty) fract part to internal sample
546 * representation. Needed for higher accuracy in internal calculations as
547 * well as for DSP configuration. See also: ../atrac3_rm.c, DSP_SET_SAMPLE_DEPTH
551 readQuantSpectralCoeffs(gb
, quant_step_index
, coding_mode
, mantissa
, coded_values
);
553 pComponent
[component_count
].numCoefs
= coded_values
;
556 pCoef
= pComponent
[component_count
].coef
;
557 for (cnt
= 0; cnt
< coded_values
; cnt
++)
558 pCoef
[cnt
] = fixmul16(mantissa
[cnt
], scalefactor
);
565 return component_count
;
570 * Decode gain parameters for the coded bands
572 * @param gb the GetBit context
573 * @param pGb the gainblock for the current band
574 * @param numBands amount of coded bands
577 static int decodeGainControl (GetBitContext
*gb
, gain_block
*pGb
, int numBands
)
582 gain_info
*pGain
= pGb
->gBlock
;
584 for (i
=0 ; i
<=numBands
; i
++)
586 numData
= get_bits(gb
,3);
587 pGain
[i
].num_gain_data
= numData
;
588 pLevel
= pGain
[i
].levcode
;
589 pLoc
= pGain
[i
].loccode
;
591 for (cf
= 0; cf
< numData
; cf
++){
592 pLevel
[cf
]= get_bits(gb
,4);
593 pLoc
[cf
]= get_bits(gb
,5);
594 if(cf
&& pLoc
[cf
] <= pLoc
[cf
-1])
599 /* Clear the unused blocks. */
601 pGain
[i
].num_gain_data
= 0;
608 * Apply fix (constant) gain and overlap for sample[start...255].
610 * @param pIn input buffer
611 * @param pPrev previous buffer to perform overlap against
612 * @param pOut output buffer
613 * @param start index to start with (always a multiple of 8)
614 * @param gain gain to apply
617 static void applyFixGain (int32_t *pIn
, int32_t *pPrev
, int32_t *pOut
,
618 int32_t start
, int32_t gain
)
622 /* start is always a multiple of 8 and therefore allows us to unroll the
623 * loop to 8 calculation per loop
625 if (ONE_16
== gain
) {
626 /* gain1 = 1.0 -> no multiplication needed, just adding */
627 /* Remark: This path is called >90%. */
629 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
630 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
631 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
632 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
633 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
634 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
635 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
636 pOut
[i
] = pIn
[i
] + pPrev
[i
]; i
++;
639 /* gain1 != 1.0 -> we need to do a multiplication */
640 /* Remark: This path is called seldom. */
642 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
643 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
644 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
645 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
646 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
647 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
648 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
649 pOut
[i
] = fixmul16(pIn
[i
], gain
) + pPrev
[i
]; i
++;
656 * Apply variable gain and overlap. Returns sample index after applying gain,
657 * resulting sample index is always a multiple of 8.
659 * @param pIn input buffer
660 * @param pPrev previous buffer to perform overlap against
661 * @param pOut output buffer
662 * @param start index to start with (always a multiple of 8)
663 * @param end end index for first loop (always a multiple of 8)
664 * @param gain1 current bands gain to apply
665 * @param gain2 next bands gain to apply
666 * @param gain_inc stepwise adaption from gain1 to gain2
669 static int applyVariableGain (int32_t *pIn
, int32_t *pPrev
, int32_t *pOut
,
670 int32_t start
, int32_t end
,
671 int32_t gain1
, int32_t gain2
, int32_t gain_inc
)
675 /* Apply fix gains until end index is reached */
677 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
678 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
679 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
680 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
681 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
682 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
683 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
684 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
687 /* Interpolation is done over next eight samples */
688 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
689 gain2
= fixmul16(gain2
, gain_inc
);
690 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
691 gain2
= fixmul16(gain2
, gain_inc
);
692 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
693 gain2
= fixmul16(gain2
, gain_inc
);
694 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
695 gain2
= fixmul16(gain2
, gain_inc
);
696 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
697 gain2
= fixmul16(gain2
, gain_inc
);
698 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
699 gain2
= fixmul16(gain2
, gain_inc
);
700 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
701 gain2
= fixmul16(gain2
, gain_inc
);
702 pOut
[i
] = fixmul16((fixmul16(pIn
[i
], gain1
) + pPrev
[i
]), gain2
); i
++;
703 gain2
= fixmul16(gain2
, gain_inc
);
710 * Apply gain parameters and perform the MDCT overlapping part
712 * @param pIn input buffer
713 * @param pPrev previous buffer to perform overlap against
714 * @param pOut output buffer
715 * @param pGain1 current band gain info
716 * @param pGain2 next band gain info
719 static void gainCompensateAndOverlap (int32_t *pIn
, int32_t *pPrev
, int32_t *pOut
,
720 gain_info
*pGain1
, gain_info
*pGain2
)
722 /* gain compensation function */
723 int32_t gain1
, gain2
, gain_inc
;
724 int cnt
, numdata
, nsample
, startLoc
;
726 if (pGain2
->num_gain_data
== 0)
729 gain1
= (ONE_16
<<4)>>(pGain2
->levcode
[0]);
731 if (pGain1
->num_gain_data
== 0) {
732 /* Remark: This path is called >90%. */
733 /* Apply gain for all samples from 0...255 */
734 applyFixGain(pIn
, pPrev
, pOut
, 0, gain1
);
736 /* Remark: This path is called seldom. */
737 numdata
= pGain1
->num_gain_data
;
738 pGain1
->loccode
[numdata
] = 32;
739 pGain1
->levcode
[numdata
] = 4;
741 nsample
= 0; /* starting loop with =0 */
743 for (cnt
= 0; cnt
< numdata
; cnt
++) {
744 startLoc
= pGain1
->loccode
[cnt
] * 8;
746 gain2
= (ONE_16
<<4)>>(pGain1
->levcode
[cnt
]);
747 gain_inc
= gain_tab2
[(pGain1
->levcode
[cnt
+1] - pGain1
->levcode
[cnt
])+15];
749 /* Apply variable gain (gain1 -> gain2) to samples */
750 nsample
= applyVariableGain(pIn
, pPrev
, pOut
, nsample
, startLoc
, gain1
, gain2
, gain_inc
);
752 /* Apply gain for the residual samples from nsample...255 */
753 applyFixGain(pIn
, pPrev
, pOut
, nsample
, gain1
);
756 /* Delay for the overlapping part. */
757 memcpy(pPrev
, &pIn
[256], 256*sizeof(int32_t));
762 * Combine the tonal band spectrum and regular band spectrum
763 * Return position of the last tonal coefficient
766 * @param pSpectrum output spectrum buffer
767 * @param numComponents amount of tonal components
768 * @param pComponent tonal components for this band
771 static int addTonalComponents (int32_t *pSpectrum
, int numComponents
, tonal_component
*pComponent
)
773 int cnt
, i
, lastPos
= -1;
777 for (cnt
= 0; cnt
< numComponents
; cnt
++){
778 lastPos
= FFMAX(pComponent
[cnt
].pos
+ pComponent
[cnt
].numCoefs
, lastPos
);
779 pIn
= pComponent
[cnt
].coef
;
780 pOut
= &(pSpectrum
[pComponent
[cnt
].pos
]);
782 for (i
=0 ; i
<pComponent
[cnt
].numCoefs
; i
++)
791 * Linear equidistant interpolation between two points x and y. 7 interpolation
792 * points can be calculated.
793 * Result for s=0 is x
794 * Result for s=8 is y
796 * @param x first input point
797 * @param y second input point
798 * @param s index of interpolation point (0..7)
801 /* rockbox: Not used anymore. Faster version defined below.
802 #define INTERPOLATE_FP16(x, y, s) ((x) + fixmul16(((s*ONE_16)>>3), (((y) - (x)))))
804 #define INTERPOLATE_FP16(x, y, s) ((x) + ((s*((y)-(x)))>>3))
806 static void reverseMatrixing(int32_t *su1
, int32_t *su2
, int *pPrevCode
, int *pCurrCode
)
808 int i
, band
, nsample
, s1
, s2
;
810 int32_t mc1_l
, mc1_r
, mc2_l
, mc2_r
;
812 for (i
=0,band
= 0; band
< 4*256; band
+=256,i
++) {
818 /* Selector value changed, interpolation needed. */
819 mc1_l
= matrixCoeffs_fix
[s1
<<1];
820 mc1_r
= matrixCoeffs_fix
[(s1
<<1)+1];
821 mc2_l
= matrixCoeffs_fix
[s2
<<1];
822 mc2_r
= matrixCoeffs_fix
[(s2
<<1)+1];
824 /* Interpolation is done over the first eight samples. */
825 for(; nsample
< 8; nsample
++) {
826 c1
= su1
[band
+nsample
];
827 c2
= su2
[band
+nsample
];
828 c2
= fixmul16(c1
, INTERPOLATE_FP16(mc1_l
, mc2_l
, nsample
)) + fixmul16(c2
, INTERPOLATE_FP16(mc1_r
, mc2_r
, nsample
));
829 su1
[band
+nsample
] = c2
;
830 su2
[band
+nsample
] = (c1
<< 1) - c2
;
834 /* Apply the matrix without interpolation. */
836 case 0: /* M/S decoding */
837 for (; nsample
< 256; nsample
++) {
838 c1
= su1
[band
+nsample
];
839 c2
= su2
[band
+nsample
];
840 su1
[band
+nsample
] = c2
<< 1;
841 su2
[band
+nsample
] = (c1
- c2
) << 1;
846 for (; nsample
< 256; nsample
++) {
847 c1
= su1
[band
+nsample
];
848 c2
= su2
[band
+nsample
];
849 su1
[band
+nsample
] = (c1
+ c2
) << 1;
850 su2
[band
+nsample
] = -1*(c2
<< 1);
855 for (; nsample
< 256; nsample
++) {
856 c1
= su1
[band
+nsample
];
857 c2
= su2
[band
+nsample
];
858 su1
[band
+nsample
] = c1
+ c2
;
859 su2
[band
+nsample
] = c1
- c2
;
869 static void getChannelWeights (int indx
, int flag
, int32_t ch
[2]){
870 /* Read channel weights from table */
872 /* Swap channel weights */
873 ch
[1] = channelWeights0
[indx
&7];
874 ch
[0] = channelWeights1
[indx
&7];
876 ch
[0] = channelWeights0
[indx
&7];
877 ch
[1] = channelWeights1
[indx
&7];
881 static void channelWeighting (int32_t *su1
, int32_t *su2
, int *p3
)
884 /* w[x][y] y=0 is left y=1 is right */
887 if (p3
[1] != 7 || p3
[3] != 7){
888 getChannelWeights(p3
[1], p3
[0], w
[0]);
889 getChannelWeights(p3
[3], p3
[2], w
[1]);
891 for(band
= 1; band
< 4; band
++) {
892 /* scale the channels by the weights */
893 for(nsample
= 0; nsample
< 8; nsample
++) {
894 su1
[band
*256+nsample
] = fixmul16(su1
[band
*256+nsample
], INTERPOLATE_FP16(w
[0][0], w
[0][1], nsample
));
895 su2
[band
*256+nsample
] = fixmul16(su2
[band
*256+nsample
], INTERPOLATE_FP16(w
[1][0], w
[1][1], nsample
));
898 for(; nsample
< 256; nsample
++) {
899 su1
[band
*256+nsample
] = fixmul16(su1
[band
*256+nsample
], w
[1][0]);
900 su2
[band
*256+nsample
] = fixmul16(su2
[band
*256+nsample
], w
[1][1]);
907 * Decode a Sound Unit
909 * @param gb the GetBit context
910 * @param pSnd the channel unit to be used
911 * @param pOut the decoded samples before IQMF
912 * @param channelNum channel number
913 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
916 static int decodeChannelSoundUnit (GetBitContext
*gb
, channel_unit
*pSnd
, int32_t *pOut
, int channelNum
, int codingMode
)
918 int band
, result
=0, numSubbands
, lastTonal
, numBands
;
919 if (codingMode
== JOINT_STEREO
&& channelNum
== 1) {
920 if (get_bits(gb
,2) != 3) {
921 DEBUGF("JS mono Sound Unit id != 3.\n");
925 if (get_bits(gb
,6) != 0x28) {
926 DEBUGF("Sound Unit id != 0x28.\n");
931 /* number of coded QMF bands */
932 pSnd
->bandsCoded
= get_bits(gb
,2);
934 result
= decodeGainControl (gb
, &(pSnd
->gainBlock
[pSnd
->gcBlkSwitch
]), pSnd
->bandsCoded
);
935 if (result
) return result
;
937 pSnd
->numComponents
= decodeTonalComponents (gb
, pSnd
->components
, pSnd
->bandsCoded
);
938 if (pSnd
->numComponents
== -1) return -1;
940 numSubbands
= decodeSpectrum (gb
, pSnd
->spectrum
);
942 /* Merge the decoded spectrum and tonal components. */
943 lastTonal
= addTonalComponents (pSnd
->spectrum
, pSnd
->numComponents
, pSnd
->components
);
946 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
947 numBands
= (subbandTab
[numSubbands
] - 1) >> 8;
949 numBands
= FFMAX((lastTonal
+ 256) >> 8, numBands
);
951 /* Reconstruct time domain samples. */
952 for (band
=0; band
<4; band
++) {
953 /* Perform the IMDCT step without overlapping. */
954 if (band
<= numBands
) {
955 IMLT(&(pSnd
->spectrum
[band
*256]), pSnd
->IMDCT_buf
);
957 memset(pSnd
->IMDCT_buf
, 0, 512 * sizeof(int32_t));
960 /* gain compensation and overlapping */
961 gainCompensateAndOverlap (pSnd
->IMDCT_buf
, &(pSnd
->prevFrame
[band
*256]), &(pOut
[band
*256]),
962 &((pSnd
->gainBlock
[1 - (pSnd
->gcBlkSwitch
)]).gBlock
[band
]),
963 &((pSnd
->gainBlock
[pSnd
->gcBlkSwitch
]).gBlock
[band
]));
966 /* Swap the gain control buffers for the next frame. */
967 pSnd
->gcBlkSwitch
^= 1;
975 * @param q Atrac3 private context
976 * @param databuf the input data
979 static int decodeFrame(ATRAC3Context
*q
, const uint8_t* databuf
, int off
)
982 int32_t *p1
, *p2
, *p3
, *p4
;
985 if (q
->codingMode
== JOINT_STEREO
) {
987 /* channel coupling mode */
988 /* decode Sound Unit 1 */
989 init_get_bits(&q
->gb
,databuf
,q
->bits_per_frame
);
991 result
= decodeChannelSoundUnit(&q
->gb
, q
->pUnits
, q
->outSamples
, 0, JOINT_STEREO
);
995 /* Framedata of the su2 in the joint-stereo mode is encoded in
996 * reverse byte order so we need to swap it first. */
997 if (databuf
== q
->decoded_bytes_buffer
) {
998 uint8_t *ptr2
= q
->decoded_bytes_buffer
+q
->bytes_per_frame
-1;
999 ptr1
= q
->decoded_bytes_buffer
;
1000 for (i
= 0; i
< (q
->bytes_per_frame
/2); i
++, ptr1
++, ptr2
--) {
1001 FFSWAP(uint8_t,*ptr1
,*ptr2
);
1004 const uint8_t *ptr2
= databuf
+q
->bytes_per_frame
-1;
1005 for (i
= 0; i
< q
->bytes_per_frame
; i
++)
1006 q
->decoded_bytes_buffer
[i
] = *ptr2
--;
1009 /* Skip the sync codes (0xF8). */
1010 ptr1
= q
->decoded_bytes_buffer
;
1011 for (i
= 4; *ptr1
== 0xF8; i
++, ptr1
++) {
1012 if (i
>= q
->bytes_per_frame
)
1017 /* set the bitstream reader at the start of the second Sound Unit*/
1018 init_get_bits(&q
->gb
,ptr1
,q
->bits_per_frame
);
1020 /* Fill the Weighting coeffs delay buffer */
1021 memmove(q
->weighting_delay
,&(q
->weighting_delay
[2]),4*sizeof(int));
1022 q
->weighting_delay
[4] = get_bits1(&q
->gb
);
1023 q
->weighting_delay
[5] = get_bits(&q
->gb
,3);
1025 for (i
= 0; i
< 4; i
++) {
1026 q
->matrix_coeff_index_prev
[i
] = q
->matrix_coeff_index_now
[i
];
1027 q
->matrix_coeff_index_now
[i
] = q
->matrix_coeff_index_next
[i
];
1028 q
->matrix_coeff_index_next
[i
] = get_bits(&q
->gb
,2);
1031 /* Decode Sound Unit 2. */
1032 result
= decodeChannelSoundUnit(&q
->gb
, &q
->pUnits
[1], &q
->outSamples
[1024], 1, JOINT_STEREO
);
1036 /* Reconstruct the channel coefficients. */
1037 reverseMatrixing(q
->outSamples
, &q
->outSamples
[1024], q
->matrix_coeff_index_prev
, q
->matrix_coeff_index_now
);
1039 channelWeighting(q
->outSamples
, &q
->outSamples
[1024], q
->weighting_delay
);
1042 /* normal stereo mode or mono */
1043 /* Decode the channel sound units. */
1044 for (i
=0 ; i
<q
->channels
; i
++) {
1046 /* Set the bitstream reader at the start of a channel sound unit. */
1047 init_get_bits(&q
->gb
, databuf
+((i
*q
->bytes_per_frame
)/q
->channels
)+off
, (q
->bits_per_frame
)/q
->channels
);
1049 result
= decodeChannelSoundUnit(&q
->gb
, &q
->pUnits
[i
], &q
->outSamples
[i
*1024], i
, q
->codingMode
);
1055 /* Apply the iQMF synthesis filter. */
1057 for (i
=0 ; i
<q
->channels
; i
++) {
1061 iqmf (p1
, p2
, 256, p1
, q
->pUnits
[i
].delayBuf1
, q
->tempBuf
);
1062 iqmf (p4
, p3
, 256, p3
, q
->pUnits
[i
].delayBuf2
, q
->tempBuf
);
1063 iqmf (p1
, p3
, 512, p1
, q
->pUnits
[i
].delayBuf3
, q
->tempBuf
);
1072 * Atrac frame decoding
1074 * @param rmctx pointer to the AVCodecContext
1077 int atrac3_decode_frame(unsigned long block_align
, ATRAC3Context
*q
,
1078 int *data_size
, const uint8_t *buf
, int buf_size
) {
1079 int result
= 0, off
= 0;
1080 const uint8_t* databuf
;
1082 if ((unsigned)buf_size
< block_align
)
1085 /* Check if we need to descramble and what buffer to pass on. */
1086 if (q
->scrambled_stream
) {
1087 off
= decode_bytes(buf
, q
->decoded_bytes_buffer
, block_align
);
1088 databuf
= q
->decoded_bytes_buffer
;
1093 result
= decodeFrame(q
, databuf
, off
);
1096 DEBUGF("Frame decoding error!\n");
1100 if (q
->channels
== 1)
1101 *data_size
= 1024 * sizeof(int32_t);
1103 *data_size
= 2048 * sizeof(int32_t);
1110 * Atrac3 initialization
1112 * @param rmctx pointer to the RMContext
1114 int atrac3_decode_init(ATRAC3Context
*q
, struct mp3entry
*id3
)
1117 uint8_t *edata_ptr
= (uint8_t*)&id3
->id3v2buf
;
1118 static VLC_TYPE atrac3_vlc_table
[4096][2];
1119 static int vlcs_initialized
= 0;
1121 /* Take data from the RM container. */
1122 q
->sample_rate
= id3
->frequency
;
1123 q
->channels
= id3
->channels
;
1124 q
->bit_rate
= id3
->bitrate
* 1000;
1125 q
->bits_per_frame
= id3
->bytesperframe
* 8;
1126 q
->bytes_per_frame
= id3
->bytesperframe
;
1128 /* Take care of the codec-specific extradata. */
1130 if (id3
->extradata_size
== 14) {
1131 /* Parse the extradata, WAV format */
1132 DEBUGF("[0-1] %d\n",rm_get_uint16le(&edata_ptr
[0])); /* Unknown value always 1 */
1133 q
->samples_per_channel
= rm_get_uint32le(&edata_ptr
[2]);
1134 q
->codingMode
= rm_get_uint16le(&edata_ptr
[6]);
1135 DEBUGF("[8-9] %d\n",rm_get_uint16le(&edata_ptr
[8])); /* Dupe of coding mode */
1136 q
->frame_factor
= rm_get_uint16le(&edata_ptr
[10]); /* Unknown always 1 */
1137 DEBUGF("[12-13] %d\n",rm_get_uint16le(&edata_ptr
[12])); /* Unknown always 0 */
1140 q
->samples_per_frame
= 1024 * q
->channels
;
1141 q
->atrac3version
= 4;
1144 q
->codingMode
= JOINT_STEREO
;
1146 q
->codingMode
= STEREO
;
1147 q
->scrambled_stream
= 0;
1149 if ((q
->bytes_per_frame
== 96*q
->channels
*q
->frame_factor
) || (q
->bytes_per_frame
== 152*q
->channels
*q
->frame_factor
) || (q
->bytes_per_frame
== 192*q
->channels
*q
->frame_factor
)) {
1151 DEBUGF("Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q
->bytes_per_frame
, q
->channels
, q
->frame_factor
);
1155 } else if (id3
->extradata_size
== 10) {
1156 /* Parse the extradata, RM format. */
1157 q
->atrac3version
= rm_get_uint32be(&edata_ptr
[0]);
1158 q
->samples_per_frame
= rm_get_uint16be(&edata_ptr
[4]);
1159 q
->delay
= rm_get_uint16be(&edata_ptr
[6]);
1160 q
->codingMode
= rm_get_uint16be(&edata_ptr
[8]);
1162 q
->samples_per_channel
= q
->samples_per_frame
/ q
->channels
;
1163 q
->scrambled_stream
= 1;
1166 DEBUGF("Unknown extradata size %d.\n",id3
->extradata_size
);
1168 /* Check the extradata. */
1170 if (q
->atrac3version
!= 4) {
1171 DEBUGF("Version %d != 4.\n",q
->atrac3version
);
1175 if (q
->samples_per_frame
!= 1024 && q
->samples_per_frame
!= 2048) {
1176 DEBUGF("Unknown amount of samples per frame %d.\n",q
->samples_per_frame
);
1180 if (q
->delay
!= 0x88E) {
1181 DEBUGF("Unknown amount of delay %x != 0x88E.\n",q
->delay
);
1185 if (q
->codingMode
== STEREO
) {
1186 DEBUGF("Normal stereo detected.\n");
1187 } else if (q
->codingMode
== JOINT_STEREO
) {
1188 DEBUGF("Joint stereo detected.\n");
1190 DEBUGF("Unknown channel coding mode %x!\n",q
->codingMode
);
1194 if (id3
->channels
<= 0 || id3
->channels
> 2 ) {
1195 DEBUGF("Channel configuration error!\n");
1200 if(id3
->bytesperframe
>= UINT16_MAX
/2)
1204 /* Initialize the VLC tables. */
1205 if (!vlcs_initialized
) {
1206 for (i
=0 ; i
<7 ; i
++) {
1207 spectral_coeff_tab
[i
].table
= &atrac3_vlc_table
[atrac3_vlc_offs
[i
]];
1208 spectral_coeff_tab
[i
].table_allocated
= atrac3_vlc_offs
[i
+ 1] - atrac3_vlc_offs
[i
];
1209 init_vlc (&spectral_coeff_tab
[i
], 9, huff_tab_sizes
[i
],
1211 huff_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
1214 vlcs_initialized
= 1;
1218 init_atrac3_transforms();
1220 /* init the joint-stereo decoding data */
1221 q
->weighting_delay
[0] = 0;
1222 q
->weighting_delay
[1] = 7;
1223 q
->weighting_delay
[2] = 0;
1224 q
->weighting_delay
[3] = 7;
1225 q
->weighting_delay
[4] = 0;
1226 q
->weighting_delay
[5] = 7;
1228 for (i
=0; i
<4; i
++) {
1229 q
->matrix_coeff_index_prev
[i
] = 3;
1230 q
->matrix_coeff_index_now
[i
] = 3;
1231 q
->matrix_coeff_index_next
[i
] = 3;
1234 /* Link the iram'ed arrays to the decoder's data structure */
1235 q
->pUnits
= channel_units
;
1236 q
->pUnits
[0].spectrum
= &atrac3_spectrum
[0][0];
1237 q
->pUnits
[1].spectrum
= &atrac3_spectrum
[1][0];
1238 q
->pUnits
[0].IMDCT_buf
= &atrac3_IMDCT_buf
[0][0];
1239 q
->pUnits
[1].IMDCT_buf
= &atrac3_IMDCT_buf
[1][0];
1240 q
->pUnits
[0].prevFrame
= &atrac3_prevFrame
[0][0];
1241 q
->pUnits
[1].prevFrame
= &atrac3_prevFrame
[1][0];