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"
43 #include "../lib/mdct2.h"
45 #define JOINT_STEREO 0x12
53 /* FFMAX/MIN/SWAP and av_clip were taken from libavutil/common.h */
54 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
55 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
56 #define FFSWAP(type,a,b) do{type SWAP_tmp= b; b= a; a= SWAP_tmp;}while(0)
59 * Clips a signed integer value into the -32768,32767 range.
61 static inline int16_t av_clip_int16(int a
)
63 if ((a
+32768) & ~65535) return (a
>>31) ^ 32767;
67 static int32_t qmf_window
[48] IBSS_ATTR
;
68 static VLC spectral_coeff_tab
[7];
69 static channel_unit channel_units
[2];
72 * Matrixing within quadrature mirror synthesis filter.
74 * @param p3 output buffer
75 * @param inlo lower part of spectrum
76 * @param inhi higher part of spectrum
77 * @param nIn size of spectrum buffer
82 atrac3_iqmf_matrixing(int32_t *p3
,
88 atrac3_iqmf_matrixing(int32_t *p3
,
94 for(i
=0; i
<nIn
; i
+=2){
95 p3
[2*i
+0] = inlo
[i
] + inhi
[i
];
96 p3
[2*i
+1] = inlo
[i
] - inhi
[i
];
97 p3
[2*i
+2] = inlo
[i
+1] + inhi
[i
+1];
98 p3
[2*i
+3] = inlo
[i
+1] - inhi
[i
+1];
104 * Matrixing within quadrature mirror synthesis filter.
106 * @param out output buffer
107 * @param in input buffer
108 * @param win windowing coefficients
109 * @param nIn size of spectrum buffer
114 atrac3_iqmf_dewindowing(int32_t *out
,
120 atrac3_iqmf_dewindowing(int32_t *out
,
125 int32_t i
, j
, s1
, s2
;
127 for (j
= nIn
; j
!= 0; j
--) {
129 s1
= fixmul31(win
[0], in
[0]);
130 s2
= fixmul31(win
[1], in
[1]);
133 for (i
= 2; i
< 48; i
+= 2) {
134 s1
+= fixmul31(win
[i
], in
[i
]);
135 s2
+= fixmul31(win
[i
+1], in
[i
+1]);
150 * @param buffer sample buffer
151 * @param win window coefficients
155 atrac3_imdct_windowing(int32_t *buffer
,
159 /* win[0..127] = win[511..384], win[128..383] = 1 */
160 for(i
= 0; i
<128; i
++) {
161 buffer
[ i
] = fixmul31(win
[i
], buffer
[ i
]);
162 buffer
[511-i
] = fixmul31(win
[i
], buffer
[511-i
]);
167 * Quadrature mirror synthesis filter.
169 * @param inlo lower part of spectrum
170 * @param inhi higher part of spectrum
171 * @param nIn size of spectrum buffer
172 * @param pOut out buffer
173 * @param delayBuf delayBuf buffer
174 * @param temp temp buffer
177 static void iqmf (int32_t *inlo
, int32_t *inhi
, unsigned int nIn
, int32_t *pOut
, int32_t *delayBuf
, int32_t *temp
)
179 /* Restore the delay buffer */
180 memcpy(temp
, delayBuf
, 46*sizeof(int32_t));
182 /* loop1: matrixing */
183 atrac3_iqmf_matrixing(temp
+ 46, inlo
, inhi
, nIn
);
185 /* loop2: dewindowing */
186 atrac3_iqmf_dewindowing(pOut
, temp
, qmf_window
, nIn
);
188 /* Save the delay buffer */
189 memcpy(delayBuf
, temp
+ (nIn
<< 1), 46*sizeof(int32_t));
193 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
194 * caused by the reverse spectra of the QMF.
196 * @param pInput float input
197 * @param pOutput float output
198 * @param odd_band 1 if the band is an odd band
201 static void IMLT(int32_t *pInput
, int32_t *pOutput
, int odd_band
)
206 * Reverse the odd bands before IMDCT, this is an effect of the QMF transform
207 * or it gives better compression to do it this way.
208 * FIXME: It should be possible to handle this in ff_imdct_calc
209 * for that to happen a modification of the prerotation step of
210 * all SIMD code and C code is needed.
211 * Or fix the functions before so they generate a pre reversed spectrum.
214 for (i
=0; i
<128; i
++)
215 FFSWAP(int32_t, pInput
[i
], pInput
[255-i
]);
218 /* Apply the imdct. */
219 mdct_backward(512, pInput
, pOutput
);
222 atrac3_imdct_windowing(pOutput
, window_lookup
);
227 * Atrac 3 indata descrambling, only used for data coming from the rm container
229 * @param in pointer to 8 bit array of indata
230 * @param bits amount of bits
231 * @param out pointer to 8 bit array of outdata
234 static int decode_bytes(const uint8_t* inbuffer
, uint8_t* out
, int bytes
){
238 uint32_t* obuf
= (uint32_t*) out
;
240 #if ((defined(TEST) || defined(SIMULATOR)) && !defined(CPU_ARM))
241 off
= 0; //no check for memory alignment of inbuffer
243 off
= (intptr_t)inbuffer
& 3;
245 buf
= (const uint32_t*) (inbuffer
- off
);
247 c
= be2me_32((0x537F6103 >> (off
*8)) | (0x537F6103 << (32-(off
*8))));
249 for (i
= 0; i
< bytes
/4; i
++)
250 obuf
[i
] = c
^ buf
[i
];
256 static void init_atrac3_transforms(void) {
260 /* Generate the mdct window, for details see
261 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
263 /* mdct window had been generated and saved as a lookup table in atrac3data_fixed.h */
265 /* Generate the QMF window. */
266 for (i
=0 ; i
<24; i
++) {
267 s
= qmf_48tap_half_fix
[i
] << 1;
269 qmf_window
[47 - i
] = s
;
276 * @param gb the GetBit context
277 * @param selector what table is the output values coded with
278 * @param codingFlag constant length coding or variable length coding
279 * @param mantissas mantissa output table
280 * @param numCodes amount of values to get
283 static void readQuantSpectralCoeffs (GetBitContext
*gb
, int selector
, int codingFlag
, int* mantissas
, int numCodes
)
285 int numBits
, cnt
, code
, huffSymb
;
290 if (codingFlag
!= 0) {
291 /* constant length coding (CLC) */
292 numBits
= CLCLengthTab
[selector
];
295 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
297 code
= get_sbits(gb
, numBits
);
300 mantissas
[cnt
] = code
;
303 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
305 code
= get_bits(gb
, numBits
); //numBits is always 4 in this case
308 mantissas
[cnt
*2] = seTab_0
[code
>> 2];
309 mantissas
[cnt
*2+1] = seTab_0
[code
& 3];
313 /* variable length coding (VLC) */
315 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
316 huffSymb
= get_vlc2(gb
, spectral_coeff_tab
[selector
-1].table
, spectral_coeff_tab
[selector
-1].bits
, 3);
318 code
= huffSymb
>> 1;
321 mantissas
[cnt
] = code
;
324 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
325 huffSymb
= get_vlc2(gb
, spectral_coeff_tab
[selector
-1].table
, spectral_coeff_tab
[selector
-1].bits
, 3);
326 mantissas
[cnt
*2] = decTable1
[huffSymb
*2];
327 mantissas
[cnt
*2+1] = decTable1
[huffSymb
*2+1];
334 * Restore the quantized band spectrum coefficients
336 * @param gb the GetBit context
337 * @param pOut decoded band spectrum
338 * @return outSubbands subband counter, fix for broken specification/files
341 static int decodeSpectrum (GetBitContext
*gb
, int32_t *pOut
)
343 int numSubbands
, codingMode
, cnt
, first
, last
, subbWidth
, *pIn
;
344 int subband_vlc_index
[32], SF_idxs
[32];
348 numSubbands
= get_bits(gb
, 5); // number of coded subbands
349 codingMode
= get_bits1(gb
); // coding Mode: 0 - VLC/ 1-CLC
351 /* Get the VLC selector table for the subbands, 0 means not coded. */
352 for (cnt
= 0; cnt
<= numSubbands
; cnt
++)
353 subband_vlc_index
[cnt
] = get_bits(gb
, 3);
355 /* Read the scale factor indexes from the stream. */
356 for (cnt
= 0; cnt
<= numSubbands
; cnt
++) {
357 if (subband_vlc_index
[cnt
] != 0)
358 SF_idxs
[cnt
] = get_bits(gb
, 6);
361 for (cnt
= 0; cnt
<= numSubbands
; cnt
++) {
362 first
= subbandTab
[cnt
];
363 last
= subbandTab
[cnt
+1];
365 subbWidth
= last
- first
;
367 if (subband_vlc_index
[cnt
] != 0) {
368 /* Decode spectral coefficients for this subband. */
369 /* TODO: This can be done faster is several blocks share the
370 * same VLC selector (subband_vlc_index) */
371 readQuantSpectralCoeffs (gb
, subband_vlc_index
[cnt
], codingMode
, mantissas
, subbWidth
);
373 /* Decode the scale factor for this subband. */
374 SF
= fixmul31(SFTable_fixed
[SF_idxs
[cnt
]], iMaxQuant_fix
[subband_vlc_index
[cnt
]]);
376 /* Inverse quantize the coefficients. */
377 for (pIn
=mantissas
; first
<last
; first
++, pIn
++)
378 pOut
[first
] = fixmul16(*pIn
, SF
);
380 /* This subband was not coded, so zero the entire subband. */
381 memset(pOut
+first
, 0, subbWidth
*sizeof(int32_t));
385 /* Clear the subbands that were not coded. */
386 first
= subbandTab
[cnt
];
387 memset(pOut
+first
, 0, (1024 - first
) * sizeof(int32_t));
392 * Restore the quantized tonal components
394 * @param gb the GetBit context
395 * @param pComponent tone component
396 * @param numBands amount of coded bands
399 static int decodeTonalComponents (GetBitContext
*gb
, tonal_component
*pComponent
, int numBands
)
402 int components
, coding_mode_selector
, coding_mode
, coded_values_per_component
;
403 int sfIndx
, coded_values
, max_coded_values
, quant_step_index
, coded_components
;
404 int band_flags
[4], mantissa
[8];
407 int component_count
= 0;
409 components
= get_bits(gb
,5);
411 /* no tonal components */
415 coding_mode_selector
= get_bits(gb
,2);
416 if (coding_mode_selector
== 2)
419 coding_mode
= coding_mode_selector
& 1;
421 for (i
= 0; i
< components
; i
++) {
422 for (cnt
= 0; cnt
<= numBands
; cnt
++)
423 band_flags
[cnt
] = get_bits1(gb
);
425 coded_values_per_component
= get_bits(gb
,3);
427 quant_step_index
= get_bits(gb
,3);
428 if (quant_step_index
<= 1)
431 if (coding_mode_selector
== 3)
432 coding_mode
= get_bits1(gb
);
434 for (j
= 0; j
< (numBands
+ 1) * 4; j
++) {
435 if (band_flags
[j
>> 2] == 0)
438 coded_components
= get_bits(gb
,3);
440 for (k
=0; k
<coded_components
; k
++) {
441 sfIndx
= get_bits(gb
,6);
442 pComponent
[component_count
].pos
= j
* 64 + (get_bits(gb
,6));
443 max_coded_values
= 1024 - pComponent
[component_count
].pos
;
444 coded_values
= coded_values_per_component
+ 1;
445 coded_values
= FFMIN(max_coded_values
,coded_values
);
447 scalefactor
= fixmul31(SFTable_fixed
[sfIndx
], iMaxQuant_fix
[quant_step_index
]);
449 readQuantSpectralCoeffs(gb
, quant_step_index
, coding_mode
, mantissa
, coded_values
);
451 pComponent
[component_count
].numCoefs
= coded_values
;
454 pCoef
= pComponent
[component_count
].coef
;
455 for (cnt
= 0; cnt
< coded_values
; cnt
++)
456 pCoef
[cnt
] = fixmul16(mantissa
[cnt
], scalefactor
);
463 return component_count
;
467 * Decode gain parameters for the coded bands
469 * @param gb the GetBit context
470 * @param pGb the gainblock for the current band
471 * @param numBands amount of coded bands
474 static int decodeGainControl (GetBitContext
*gb
, gain_block
*pGb
, int numBands
)
479 gain_info
*pGain
= pGb
->gBlock
;
481 for (i
=0 ; i
<=numBands
; i
++)
483 numData
= get_bits(gb
,3);
484 pGain
[i
].num_gain_data
= numData
;
485 pLevel
= pGain
[i
].levcode
;
486 pLoc
= pGain
[i
].loccode
;
488 for (cf
= 0; cf
< numData
; cf
++){
489 pLevel
[cf
]= get_bits(gb
,4);
490 pLoc
[cf
]= get_bits(gb
,5);
491 if(cf
&& pLoc
[cf
] <= pLoc
[cf
-1])
496 /* Clear the unused blocks. */
498 pGain
[i
].num_gain_data
= 0;
504 * Apply gain parameters and perform the MDCT overlapping part
506 * @param pIn input float buffer
507 * @param pPrev previous float buffer to perform overlap against
508 * @param pOut output float buffer
509 * @param pGain1 current band gain info
510 * @param pGain2 next band gain info
513 static void gainCompensateAndOverlap (int32_t *pIn
, int32_t *pPrev
, int32_t *pOut
, gain_info
*pGain1
, gain_info
*pGain2
)
515 /* gain compensation function */
516 int32_t gain1
, gain2
, gain_inc
;
517 int cnt
, numdata
, nsample
, startLoc
, endLoc
;
520 if (pGain2
->num_gain_data
== 0)
523 gain1
= gain_tab1
[pGain2
->levcode
[0]];
525 if (pGain1
->num_gain_data
== 0) {
526 for (cnt
= 0; cnt
< 256; cnt
++)
527 pOut
[cnt
] = fixmul16(pIn
[cnt
], gain1
) + pPrev
[cnt
];
529 numdata
= pGain1
->num_gain_data
;
530 pGain1
->loccode
[numdata
] = 32;
531 pGain1
->levcode
[numdata
] = 4;
533 nsample
= 0; // current sample = 0
535 for (cnt
= 0; cnt
< numdata
; cnt
++) {
536 startLoc
= pGain1
->loccode
[cnt
] * 8;
537 endLoc
= startLoc
+ 8;
539 gain2
= gain_tab1
[pGain1
->levcode
[cnt
]];
540 gain_inc
= gain_tab2
[(pGain1
->levcode
[cnt
+1] - pGain1
->levcode
[cnt
])+15];
543 for (; nsample
< startLoc
; nsample
++)
544 pOut
[nsample
] = fixmul16((fixmul16(pIn
[nsample
], gain1
) + pPrev
[nsample
]), gain2
);
546 /* interpolation is done over eight samples */
547 for (; nsample
< endLoc
; nsample
++) {
548 pOut
[nsample
] = fixmul16((fixmul16(pIn
[nsample
], gain1
) + pPrev
[nsample
]),gain2
);
549 gain2
= fixmul16(gain2
, gain_inc
);
553 for (; nsample
< 256; nsample
++)
554 pOut
[nsample
] = fixmul16(pIn
[nsample
], gain1
) + pPrev
[nsample
];
557 /* Delay for the overlapping part. */
558 memcpy(pPrev
, &pIn
[256], 256*sizeof(int32_t));
562 * Combine the tonal band spectrum and regular band spectrum
563 * Return position of the last tonal coefficient
566 * @param pSpectrum output spectrum buffer
567 * @param numComponents amount of tonal components
568 * @param pComponent tonal components for this band
571 static int addTonalComponents (int32_t *pSpectrum
, int numComponents
, tonal_component
*pComponent
)
573 int cnt
, i
, lastPos
= -1;
577 for (cnt
= 0; cnt
< numComponents
; cnt
++){
578 lastPos
= FFMAX(pComponent
[cnt
].pos
+ pComponent
[cnt
].numCoefs
, lastPos
);
579 pIn
= pComponent
[cnt
].coef
;
580 pOut
= &(pSpectrum
[pComponent
[cnt
].pos
]);
582 for (i
=0 ; i
<pComponent
[cnt
].numCoefs
; i
++)
590 #define INTERPOLATE(old,new,nsample) ((old*ONE_16) + fixmul16(((nsample*ONE_16)>>3), (((new) - (old))*ONE_16)))
592 static void reverseMatrixing(int32_t *su1
, int32_t *su2
, int *pPrevCode
, int *pCurrCode
)
594 int i
, band
, nsample
, s1
, s2
;
596 int32_t mc1_l
, mc1_r
, mc2_l
, mc2_r
;
598 for (i
=0,band
= 0; band
< 4*256; band
+=256,i
++) {
604 /* Selector value changed, interpolation needed. */
605 mc1_l
= matrixCoeffs_fix
[s1
<<1];
606 mc1_r
= matrixCoeffs_fix
[(s1
<<1)+1];
607 mc2_l
= matrixCoeffs_fix
[s2
<<1];
608 mc2_r
= matrixCoeffs_fix
[(s2
<<1)+1];
610 /* Interpolation is done over the first eight samples. */
611 for(; nsample
< 8; nsample
++) {
612 c1
= su1
[band
+nsample
];
613 c2
= su2
[band
+nsample
];
614 c2
= fixmul16(c1
, INTERPOLATE(mc1_l
, mc2_l
, nsample
)) + fixmul16(c2
, INTERPOLATE(mc1_r
, mc2_r
, nsample
));
615 su1
[band
+nsample
] = c2
;
616 su2
[band
+nsample
] = (c1
<< 1) - c2
;
620 /* Apply the matrix without interpolation. */
622 case 0: /* M/S decoding */
623 for (; nsample
< 256; nsample
++) {
624 c1
= su1
[band
+nsample
];
625 c2
= su2
[band
+nsample
];
626 su1
[band
+nsample
] = c2
<< 1;
627 su2
[band
+nsample
] = (c1
- c2
) << 1;
632 for (; nsample
< 256; nsample
++) {
633 c1
= su1
[band
+nsample
];
634 c2
= su2
[band
+nsample
];
635 su1
[band
+nsample
] = (c1
+ c2
) << 1;
636 su2
[band
+nsample
] = -1*(c2
<< 1);
641 for (; nsample
< 256; nsample
++) {
642 c1
= su1
[band
+nsample
];
643 c2
= su2
[band
+nsample
];
644 su1
[band
+nsample
] = c1
+ c2
;
645 su2
[band
+nsample
] = c1
- c2
;
655 static void getChannelWeights (int indx
, int flag
, int32_t ch
[2]){
660 ch
[0] = fixdiv16(((indx
& 7)*ONE_16
), 7*ONE_16
);
661 ch
[1] = fastSqrt((ONE_16
<< 1) - fixmul16(ch
[0], ch
[0]));
663 FFSWAP(int32_t, ch
[0], ch
[1]);
667 static void channelWeighting (int32_t *su1
, int32_t *su2
, int *p3
)
670 /* w[x][y] y=0 is left y=1 is right */
673 if (p3
[1] != 7 || p3
[3] != 7){
674 getChannelWeights(p3
[1], p3
[0], w
[0]);
675 getChannelWeights(p3
[3], p3
[2], w
[1]);
677 for(band
= 1; band
< 4; band
++) {
678 /* scale the channels by the weights */
679 for(nsample
= 0; nsample
< 8; nsample
++) {
680 su1
[band
*256+nsample
] = fixmul16(su1
[band
*256+nsample
], INTERPOLATE(w
[0][0], w
[0][1], nsample
));
681 su2
[band
*256+nsample
] = fixmul16(su2
[band
*256+nsample
], INTERPOLATE(w
[1][0], w
[1][1], nsample
));
684 for(; nsample
< 256; nsample
++) {
685 su1
[band
*256+nsample
] = fixmul16(su1
[band
*256+nsample
], w
[1][0]);
686 su2
[band
*256+nsample
] = fixmul16(su2
[band
*256+nsample
], w
[1][1]);
694 * Decode a Sound Unit
696 * @param gb the GetBit context
697 * @param pSnd the channel unit to be used
698 * @param pOut the decoded samples before IQMF in float representation
699 * @param channelNum channel number
700 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
704 static int decodeChannelSoundUnit (GetBitContext
*gb
, channel_unit
*pSnd
, int32_t *pOut
, int channelNum
, int codingMode
)
706 int band
, result
=0, numSubbands
, lastTonal
, numBands
;
707 if (codingMode
== JOINT_STEREO
&& channelNum
== 1) {
708 if (get_bits(gb
,2) != 3) {
709 DEBUGF("JS mono Sound Unit id != 3.\n");
713 if (get_bits(gb
,6) != 0x28) {
714 DEBUGF("Sound Unit id != 0x28.\n");
719 /* number of coded QMF bands */
720 pSnd
->bandsCoded
= get_bits(gb
,2);
722 result
= decodeGainControl (gb
, &(pSnd
->gainBlock
[pSnd
->gcBlkSwitch
]), pSnd
->bandsCoded
);
723 if (result
) return result
;
725 pSnd
->numComponents
= decodeTonalComponents (gb
, pSnd
->components
, pSnd
->bandsCoded
);
726 if (pSnd
->numComponents
== -1) return -1;
728 numSubbands
= decodeSpectrum (gb
, pSnd
->spectrum
);
730 /* Merge the decoded spectrum and tonal components. */
731 lastTonal
= addTonalComponents (pSnd
->spectrum
, pSnd
->numComponents
, pSnd
->components
);
734 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
735 numBands
= (subbandTab
[numSubbands
] - 1) >> 8;
737 numBands
= FFMAX((lastTonal
+ 256) >> 8, numBands
);
740 /* Reconstruct time domain samples. */
741 for (band
=0; band
<4; band
++) {
742 /* Perform the IMDCT step without overlapping. */
743 if (band
<= numBands
) {
744 IMLT(&(pSnd
->spectrum
[band
*256]), pSnd
->IMDCT_buf
, band
&1);
746 memset(pSnd
->IMDCT_buf
, 0, 512 * sizeof(int32_t));
748 /* gain compensation and overlapping */
749 gainCompensateAndOverlap (pSnd
->IMDCT_buf
, &(pSnd
->prevFrame
[band
*256]), &(pOut
[band
*256]),
750 &((pSnd
->gainBlock
[1 - (pSnd
->gcBlkSwitch
)]).gBlock
[band
]),
751 &((pSnd
->gainBlock
[pSnd
->gcBlkSwitch
]).gBlock
[band
]));
754 /* Swap the gain control buffers for the next frame. */
755 pSnd
->gcBlkSwitch
^= 1;
763 * @param q Atrac3 private context
764 * @param databuf the input data
767 static int decodeFrame(ATRAC3Context
*q
, const uint8_t* databuf
, int off
)
770 int32_t *p1
, *p2
, *p3
, *p4
;
773 if (q
->codingMode
== JOINT_STEREO
) {
775 /* channel coupling mode */
776 /* decode Sound Unit 1 */
777 init_get_bits(&q
->gb
,databuf
,q
->bits_per_frame
);
779 result
= decodeChannelSoundUnit(&q
->gb
, q
->pUnits
, q
->outSamples
, 0, JOINT_STEREO
);
783 /* Framedata of the su2 in the joint-stereo mode is encoded in
784 * reverse byte order so we need to swap it first. */
785 if (databuf
== q
->decoded_bytes_buffer
) {
786 uint8_t *ptr2
= q
->decoded_bytes_buffer
+q
->bytes_per_frame
-1;
787 ptr1
= q
->decoded_bytes_buffer
;
788 for (i
= 0; i
< (q
->bytes_per_frame
/2); i
++, ptr1
++, ptr2
--) {
789 FFSWAP(uint8_t,*ptr1
,*ptr2
);
792 const uint8_t *ptr2
= databuf
+q
->bytes_per_frame
-1;
793 for (i
= 0; i
< q
->bytes_per_frame
; i
++)
794 q
->decoded_bytes_buffer
[i
] = *ptr2
--;
797 /* Skip the sync codes (0xF8). */
798 ptr1
= q
->decoded_bytes_buffer
;
799 for (i
= 4; *ptr1
== 0xF8; i
++, ptr1
++) {
800 if (i
>= q
->bytes_per_frame
)
805 /* set the bitstream reader at the start of the second Sound Unit*/
806 init_get_bits(&q
->gb
,ptr1
,q
->bits_per_frame
);
808 /* Fill the Weighting coeffs delay buffer */
809 memmove(q
->weighting_delay
,&(q
->weighting_delay
[2]),4*sizeof(int));
810 q
->weighting_delay
[4] = get_bits1(&q
->gb
);
811 q
->weighting_delay
[5] = get_bits(&q
->gb
,3);
813 for (i
= 0; i
< 4; i
++) {
814 q
->matrix_coeff_index_prev
[i
] = q
->matrix_coeff_index_now
[i
];
815 q
->matrix_coeff_index_now
[i
] = q
->matrix_coeff_index_next
[i
];
816 q
->matrix_coeff_index_next
[i
] = get_bits(&q
->gb
,2);
819 /* Decode Sound Unit 2. */
820 result
= decodeChannelSoundUnit(&q
->gb
, &q
->pUnits
[1], &q
->outSamples
[1024], 1, JOINT_STEREO
);
824 /* Reconstruct the channel coefficients. */
825 reverseMatrixing(q
->outSamples
, &q
->outSamples
[1024], q
->matrix_coeff_index_prev
, q
->matrix_coeff_index_now
);
827 channelWeighting(q
->outSamples
, &q
->outSamples
[1024], q
->weighting_delay
);
830 /* normal stereo mode or mono */
831 /* Decode the channel sound units. */
832 for (i
=0 ; i
<q
->channels
; i
++) {
834 /* Set the bitstream reader at the start of a channel sound unit. */
835 init_get_bits(&q
->gb
, databuf
+((i
*q
->bytes_per_frame
)/q
->channels
)+off
, (q
->bits_per_frame
)/q
->channels
);
837 result
= decodeChannelSoundUnit(&q
->gb
, &q
->pUnits
[i
], &q
->outSamples
[i
*1024], i
, q
->codingMode
);
843 /* Apply the iQMF synthesis filter. */
845 for (i
=0 ; i
<q
->channels
; i
++) {
849 iqmf (p1
, p2
, 256, p1
, q
->pUnits
[i
].delayBuf1
, q
->tempBuf
);
850 iqmf (p4
, p3
, 256, p3
, q
->pUnits
[i
].delayBuf2
, q
->tempBuf
);
851 iqmf (p1
, p3
, 512, p1
, q
->pUnits
[i
].delayBuf3
, q
->tempBuf
);
860 * Atrac frame decoding
862 * @param rmctx pointer to the AVCodecContext
865 int atrac3_decode_frame(RMContext
*rmctx
, ATRAC3Context
*q
,
866 void *data
, int *data_size
,
867 const uint8_t *buf
, int buf_size
) {
868 int result
= 0, off
= 0, i
;
869 const uint8_t* databuf
;
870 int16_t* samples
= data
;
872 if (buf_size
< rmctx
->block_align
)
875 /* Check if we need to descramble and what buffer to pass on. */
876 if (q
->scrambled_stream
) {
877 off
= decode_bytes(buf
, q
->decoded_bytes_buffer
, rmctx
->block_align
);
878 databuf
= q
->decoded_bytes_buffer
;
883 result
= decodeFrame(q
, databuf
, off
);
886 DEBUGF("Frame decoding error!\n");
890 if (q
->channels
== 1) {
892 for (i
= 0; i
<1024; i
++)
893 samples
[i
] = av_clip_int16(q
->outSamples
[i
]);
894 *data_size
= 1024 * sizeof(int16_t);
897 for (i
= 0; i
< 1024; i
++) {
898 samples
[i
*2] = av_clip_int16(q
->outSamples
[i
]);
899 samples
[i
*2+1] = av_clip_int16(q
->outSamples
[1024+i
]);
901 *data_size
= 2048 * sizeof(int16_t);
904 return rmctx
->block_align
;
909 * Atrac3 initialization
911 * @param rmctx pointer to the RMContext
914 int atrac3_decode_init(ATRAC3Context
*q
, RMContext
*rmctx
)
917 uint8_t *edata_ptr
= rmctx
->codec_extradata
;
918 static VLC_TYPE atrac3_vlc_table
[4096][2];
919 static int vlcs_initialized
= 0;
921 /* Take data from the AVCodecContext (RM container). */
922 q
->sample_rate
= rmctx
->sample_rate
;
923 q
->channels
= rmctx
->nb_channels
;
924 q
->bit_rate
= rmctx
->bit_rate
;
925 q
->bits_per_frame
= rmctx
->block_align
* 8;
926 q
->bytes_per_frame
= rmctx
->block_align
;
928 /* Take care of the codec-specific extradata. */
929 if (rmctx
->extradata_size
== 14) {
930 /* Parse the extradata, WAV format */
931 DEBUGF("[0-1] %d\n",rm_get_uint16le(&edata_ptr
[0])); //Unknown value always 1
932 q
->samples_per_channel
= rm_get_uint32le(&edata_ptr
[2]);
933 q
->codingMode
= rm_get_uint16le(&edata_ptr
[6]);
934 DEBUGF("[8-9] %d\n",rm_get_uint16le(&edata_ptr
[8])); //Dupe of coding mode
935 q
->frame_factor
= rm_get_uint16le(&edata_ptr
[10]); //Unknown always 1
936 DEBUGF("[12-13] %d\n",rm_get_uint16le(&edata_ptr
[12])); //Unknown always 0
939 q
->samples_per_frame
= 1024 * q
->channels
;
940 q
->atrac3version
= 4;
943 q
->codingMode
= JOINT_STEREO
;
945 q
->codingMode
= STEREO
;
946 q
->scrambled_stream
= 0;
948 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
)) {
950 DEBUGF("Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q
->bytes_per_frame
, q
->channels
, q
->frame_factor
);
954 } else if (rmctx
->extradata_size
== 10) {
955 /* Parse the extradata, RM format. */
956 q
->atrac3version
= rm_get_uint32be(&edata_ptr
[0]);
957 q
->samples_per_frame
= rm_get_uint16be(&edata_ptr
[4]);
958 q
->delay
= rm_get_uint16be(&edata_ptr
[6]);
959 q
->codingMode
= rm_get_uint16be(&edata_ptr
[8]);
961 q
->samples_per_channel
= q
->samples_per_frame
/ q
->channels
;
962 q
->scrambled_stream
= 1;
965 DEBUGF("Unknown extradata size %d.\n",rmctx
->extradata_size
);
967 /* Check the extradata. */
969 if (q
->atrac3version
!= 4) {
970 DEBUGF("Version %d != 4.\n",q
->atrac3version
);
974 if (q
->samples_per_frame
!= 1024 && q
->samples_per_frame
!= 2048) {
975 DEBUGF("Unknown amount of samples per frame %d.\n",q
->samples_per_frame
);
979 if (q
->delay
!= 0x88E) {
980 DEBUGF("Unknown amount of delay %x != 0x88E.\n",q
->delay
);
984 if (q
->codingMode
== STEREO
) {
985 DEBUGF("Normal stereo detected.\n");
986 } else if (q
->codingMode
== JOINT_STEREO
) {
987 DEBUGF("Joint stereo detected.\n");
989 DEBUGF("Unknown channel coding mode %x!\n",q
->codingMode
);
993 if (rmctx
->nb_channels
<= 0 || rmctx
->nb_channels
> 2 /*|| ((rmctx->channels * 1024) != q->samples_per_frame)*/) {
994 DEBUGF("Channel configuration error!\n");
999 if(rmctx
->block_align
>= UINT16_MAX
/2)
1003 /* Initialize the VLC tables. */
1004 if (!vlcs_initialized
) {
1005 for (i
=0 ; i
<7 ; i
++) {
1006 spectral_coeff_tab
[i
].table
= &atrac3_vlc_table
[atrac3_vlc_offs
[i
]];
1007 spectral_coeff_tab
[i
].table_allocated
= atrac3_vlc_offs
[i
+ 1] - atrac3_vlc_offs
[i
];
1008 init_vlc (&spectral_coeff_tab
[i
], 9, huff_tab_sizes
[i
],
1010 huff_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
1013 vlcs_initialized
= 1;
1017 init_atrac3_transforms();
1019 /* init the joint-stereo decoding data */
1020 q
->weighting_delay
[0] = 0;
1021 q
->weighting_delay
[1] = 7;
1022 q
->weighting_delay
[2] = 0;
1023 q
->weighting_delay
[3] = 7;
1024 q
->weighting_delay
[4] = 0;
1025 q
->weighting_delay
[5] = 7;
1027 for (i
=0; i
<4; i
++) {
1028 q
->matrix_coeff_index_prev
[i
] = 3;
1029 q
->matrix_coeff_index_now
[i
] = 3;
1030 q
->matrix_coeff_index_next
[i
] = 3;
1033 q
->pUnits
= channel_units
;