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
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 "bitstream.h"
42 #include "bytestream.h"
44 #include "atrac3data.h"
46 #define JOINT_STEREO 0x12
50 /* These structures are needed to store the parsed gain control data. */
70 tonal_component components
[64];
71 float prevFrame
[1024];
73 gain_block gainBlock
[2];
75 DECLARE_ALIGNED_16(float, spectrum
[1024]);
76 DECLARE_ALIGNED_16(float, IMDCT_buf
[1024]);
78 float delayBuf1
[46]; ///<qmf delay buffers
91 int samples_per_channel
;
92 int samples_per_frame
;
100 /** joint-stereo related variables */
101 int matrix_coeff_index_prev
[4];
102 int matrix_coeff_index_now
[4];
103 int matrix_coeff_index_next
[4];
104 int weighting_delay
[6];
108 float outSamples
[2048];
109 uint8_t* decoded_bytes_buffer
;
111 DECLARE_ALIGNED_16(float,mdct_tmp
[512]);
117 int scrambled_stream
;
122 static DECLARE_ALIGNED_16(float,mdct_window
[512]);
123 static float qmf_window
[48];
124 static VLC spectral_coeff_tab
[7];
125 static float SFTable
[64];
126 static float gain_tab1
[16];
127 static float gain_tab2
[31];
128 static MDCTContext mdct_ctx
;
129 static DSPContext dsp
;
132 /* quadrature mirror synthesis filter */
135 * Quadrature mirror synthesis filter.
137 * @param inlo lower part of spectrum
138 * @param inhi higher part of spectrum
139 * @param nIn size of spectrum buffer
140 * @param pOut out buffer
141 * @param delayBuf delayBuf buffer
142 * @param temp temp buffer
146 static void iqmf (float *inlo
, float *inhi
, unsigned int nIn
, float *pOut
, float *delayBuf
, float *temp
)
151 memcpy(temp
, delayBuf
, 46*sizeof(float));
156 for(i
=0; i
<nIn
; i
+=2){
157 p3
[2*i
+0] = inlo
[i
] + inhi
[i
];
158 p3
[2*i
+1] = inlo
[i
] - inhi
[i
];
159 p3
[2*i
+2] = inlo
[i
+1] + inhi
[i
+1];
160 p3
[2*i
+3] = inlo
[i
+1] - inhi
[i
+1];
165 for (j
= nIn
; j
!= 0; j
--) {
169 for (i
= 0; i
< 48; i
+= 2) {
170 s1
+= p1
[i
] * qmf_window
[i
];
171 s2
+= p1
[i
+1] * qmf_window
[i
+1];
181 /* Update the delay buffer. */
182 memcpy(delayBuf
, temp
+ nIn
*2, 46*sizeof(float));
186 * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands
187 * caused by the reverse spectra of the QMF.
189 * @param pInput float input
190 * @param pOutput float output
191 * @param odd_band 1 if the band is an odd band
192 * @param mdct_tmp aligned temporary buffer for the mdct
195 static void IMLT(float *pInput
, float *pOutput
, int odd_band
, float* mdct_tmp
)
201 * Reverse the odd bands before IMDCT, this is an effect of the QMF transform
202 * or it gives better compression to do it this way.
203 * FIXME: It should be possible to handle this in ff_imdct_calc
204 * for that to happen a modification of the prerotation step of
205 * all SIMD code and C code is needed.
206 * Or fix the functions before so they generate a pre reversed spectrum.
209 for (i
=0; i
<128; i
++)
210 FFSWAP(float, pInput
[i
], pInput
[255-i
]);
213 mdct_ctx
.fft
.imdct_calc(&mdct_ctx
,pOutput
,pInput
,mdct_tmp
);
215 /* Perform windowing on the output. */
216 dsp
.vector_fmul(pOutput
,mdct_window
,512);
222 * Atrac 3 indata descrambling, only used for data coming from the rm container
224 * @param in pointer to 8 bit array of indata
225 * @param bits amount of bits
226 * @param out pointer to 8 bit array of outdata
229 static int decode_bytes(const uint8_t* inbuffer
, uint8_t* out
, int bytes
){
233 uint32_t* obuf
= (uint32_t*) out
;
235 off
= (int)((long)inbuffer
& 3);
236 buf
= (const uint32_t*) (inbuffer
- off
);
237 c
= be2me_32((0x537F6103 >> (off
*8)) | (0x537F6103 << (32-(off
*8))));
239 for (i
= 0; i
< bytes
/4; i
++)
240 obuf
[i
] = c
^ buf
[i
];
243 av_log(NULL
,AV_LOG_DEBUG
,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off
);
249 static void init_atrac3_transforms(ATRAC3Context
*q
) {
250 float enc_window
[256];
254 /* Generate the mdct window, for details see
255 * http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
256 for (i
=0 ; i
<256; i
++)
257 enc_window
[i
] = (sin(((i
+ 0.5) / 256.0 - 0.5) * M_PI
) + 1.0) * 0.5;
260 for (i
=0 ; i
<256; i
++) {
261 mdct_window
[i
] = enc_window
[i
]/(enc_window
[i
]*enc_window
[i
] + enc_window
[255-i
]*enc_window
[255-i
]);
262 mdct_window
[511-i
] = mdct_window
[i
];
265 /* Generate the QMF window. */
266 for (i
=0 ; i
<24; i
++) {
267 s
= qmf_48tap_half
[i
] * 2.0;
269 qmf_window
[47 - i
] = s
;
272 /* Initialize the MDCT transform. */
273 ff_mdct_init(&mdct_ctx
, 9, 1);
277 * Atrac3 uninit, free all allocated memory
280 static int atrac3_decode_close(AVCodecContext
*avctx
)
282 ATRAC3Context
*q
= avctx
->priv_data
;
285 av_free(q
->decoded_bytes_buffer
);
291 / * Mantissa decoding
293 * @param gb the GetBit context
294 * @param selector what table is the output values coded with
295 * @param codingFlag constant length coding or variable length coding
296 * @param mantissas mantissa output table
297 * @param numCodes amount of values to get
300 static void readQuantSpectralCoeffs (GetBitContext
*gb
, int selector
, int codingFlag
, int* mantissas
, int numCodes
)
302 int numBits
, cnt
, code
, huffSymb
;
307 if (codingFlag
!= 0) {
308 /* constant length coding (CLC) */
309 //FIXME we don't have any samples coded in CLC mode
310 numBits
= CLCLengthTab
[selector
];
313 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
315 code
= get_sbits(gb
, numBits
);
318 mantissas
[cnt
] = code
;
321 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
323 code
= get_bits(gb
, numBits
); //numBits is always 4 in this case
326 mantissas
[cnt
*2] = seTab_0
[code
>> 2];
327 mantissas
[cnt
*2+1] = seTab_0
[code
& 3];
331 /* variable length coding (VLC) */
333 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
334 huffSymb
= get_vlc2(gb
, spectral_coeff_tab
[selector
-1].table
, spectral_coeff_tab
[selector
-1].bits
, 3);
336 code
= huffSymb
>> 1;
339 mantissas
[cnt
] = code
;
342 for (cnt
= 0; cnt
< numCodes
; cnt
++) {
343 huffSymb
= get_vlc2(gb
, spectral_coeff_tab
[selector
-1].table
, spectral_coeff_tab
[selector
-1].bits
, 3);
344 mantissas
[cnt
*2] = decTable1
[huffSymb
*2];
345 mantissas
[cnt
*2+1] = decTable1
[huffSymb
*2+1];
352 * Restore the quantized band spectrum coefficients
354 * @param gb the GetBit context
355 * @param pOut decoded band spectrum
356 * @return outSubbands subband counter, fix for broken specification/files
359 static int decodeSpectrum (GetBitContext
*gb
, float *pOut
)
361 int numSubbands
, codingMode
, cnt
, first
, last
, subbWidth
, *pIn
;
362 int subband_vlc_index
[32], SF_idxs
[32];
366 numSubbands
= get_bits(gb
, 5); // number of coded subbands
367 codingMode
= get_bits1(gb
); // coding Mode: 0 - VLC/ 1-CLC
369 /* Get the VLC selector table for the subbands, 0 means not coded. */
370 for (cnt
= 0; cnt
<= numSubbands
; cnt
++)
371 subband_vlc_index
[cnt
] = get_bits(gb
, 3);
373 /* Read the scale factor indexes from the stream. */
374 for (cnt
= 0; cnt
<= numSubbands
; cnt
++) {
375 if (subband_vlc_index
[cnt
] != 0)
376 SF_idxs
[cnt
] = get_bits(gb
, 6);
379 for (cnt
= 0; cnt
<= numSubbands
; cnt
++) {
380 first
= subbandTab
[cnt
];
381 last
= subbandTab
[cnt
+1];
383 subbWidth
= last
- first
;
385 if (subband_vlc_index
[cnt
] != 0) {
386 /* Decode spectral coefficients for this subband. */
387 /* TODO: This can be done faster is several blocks share the
388 * same VLC selector (subband_vlc_index) */
389 readQuantSpectralCoeffs (gb
, subband_vlc_index
[cnt
], codingMode
, mantissas
, subbWidth
);
391 /* Decode the scale factor for this subband. */
392 SF
= SFTable
[SF_idxs
[cnt
]] * iMaxQuant
[subband_vlc_index
[cnt
]];
394 /* Inverse quantize the coefficients. */
395 for (pIn
=mantissas
; first
<last
; first
++, pIn
++)
396 pOut
[first
] = *pIn
* SF
;
398 /* This subband was not coded, so zero the entire subband. */
399 memset(pOut
+first
, 0, subbWidth
*sizeof(float));
403 /* Clear the subbands that were not coded. */
404 first
= subbandTab
[cnt
];
405 memset(pOut
+first
, 0, (1024 - first
) * sizeof(float));
410 * Restore the quantized tonal components
412 * @param gb the GetBit context
413 * @param pComponent tone component
414 * @param numBands amount of coded bands
417 static int decodeTonalComponents (GetBitContext
*gb
, tonal_component
*pComponent
, int numBands
)
420 int components
, coding_mode_selector
, coding_mode
, coded_values_per_component
;
421 int sfIndx
, coded_values
, max_coded_values
, quant_step_index
, coded_components
;
422 int band_flags
[4], mantissa
[8];
425 int component_count
= 0;
427 components
= get_bits(gb
,5);
429 /* no tonal components */
433 coding_mode_selector
= get_bits(gb
,2);
434 if (coding_mode_selector
== 2)
437 coding_mode
= coding_mode_selector
& 1;
439 for (i
= 0; i
< components
; i
++) {
440 for (cnt
= 0; cnt
<= numBands
; cnt
++)
441 band_flags
[cnt
] = get_bits1(gb
);
443 coded_values_per_component
= get_bits(gb
,3);
445 quant_step_index
= get_bits(gb
,3);
446 if (quant_step_index
<= 1)
449 if (coding_mode_selector
== 3)
450 coding_mode
= get_bits1(gb
);
452 for (j
= 0; j
< (numBands
+ 1) * 4; j
++) {
453 if (band_flags
[j
>> 2] == 0)
456 coded_components
= get_bits(gb
,3);
458 for (k
=0; k
<coded_components
; k
++) {
459 sfIndx
= get_bits(gb
,6);
460 pComponent
[component_count
].pos
= j
* 64 + (get_bits(gb
,6));
461 max_coded_values
= 1024 - pComponent
[component_count
].pos
;
462 coded_values
= coded_values_per_component
+ 1;
463 coded_values
= FFMIN(max_coded_values
,coded_values
);
465 scalefactor
= SFTable
[sfIndx
] * iMaxQuant
[quant_step_index
];
467 readQuantSpectralCoeffs(gb
, quant_step_index
, coding_mode
, mantissa
, coded_values
);
469 pComponent
[component_count
].numCoefs
= coded_values
;
472 pCoef
= pComponent
[component_count
].coef
;
473 for (cnt
= 0; cnt
< coded_values
; cnt
++)
474 pCoef
[cnt
] = mantissa
[cnt
] * scalefactor
;
481 return component_count
;
485 * Decode gain parameters for the coded bands
487 * @param gb the GetBit context
488 * @param pGb the gainblock for the current band
489 * @param numBands amount of coded bands
492 static int decodeGainControl (GetBitContext
*gb
, gain_block
*pGb
, int numBands
)
497 gain_info
*pGain
= pGb
->gBlock
;
499 for (i
=0 ; i
<=numBands
; i
++)
501 numData
= get_bits(gb
,3);
502 pGain
[i
].num_gain_data
= numData
;
503 pLevel
= pGain
[i
].levcode
;
504 pLoc
= pGain
[i
].loccode
;
506 for (cf
= 0; cf
< numData
; cf
++){
507 pLevel
[cf
]= get_bits(gb
,4);
508 pLoc
[cf
]= get_bits(gb
,5);
509 if(cf
&& pLoc
[cf
] <= pLoc
[cf
-1])
514 /* Clear the unused blocks. */
516 pGain
[i
].num_gain_data
= 0;
522 * Apply gain parameters and perform the MDCT overlapping part
524 * @param pIn input float buffer
525 * @param pPrev previous float buffer to perform overlap against
526 * @param pOut output float buffer
527 * @param pGain1 current band gain info
528 * @param pGain2 next band gain info
531 static void gainCompensateAndOverlap (float *pIn
, float *pPrev
, float *pOut
, gain_info
*pGain1
, gain_info
*pGain2
)
533 /* gain compensation function */
534 float gain1
, gain2
, gain_inc
;
535 int cnt
, numdata
, nsample
, startLoc
, endLoc
;
538 if (pGain2
->num_gain_data
== 0)
541 gain1
= gain_tab1
[pGain2
->levcode
[0]];
543 if (pGain1
->num_gain_data
== 0) {
544 for (cnt
= 0; cnt
< 256; cnt
++)
545 pOut
[cnt
] = pIn
[cnt
] * gain1
+ pPrev
[cnt
];
547 numdata
= pGain1
->num_gain_data
;
548 pGain1
->loccode
[numdata
] = 32;
549 pGain1
->levcode
[numdata
] = 4;
551 nsample
= 0; // current sample = 0
553 for (cnt
= 0; cnt
< numdata
; cnt
++) {
554 startLoc
= pGain1
->loccode
[cnt
] * 8;
555 endLoc
= startLoc
+ 8;
557 gain2
= gain_tab1
[pGain1
->levcode
[cnt
]];
558 gain_inc
= gain_tab2
[(pGain1
->levcode
[cnt
+1] - pGain1
->levcode
[cnt
])+15];
561 for (; nsample
< startLoc
; nsample
++)
562 pOut
[nsample
] = (pIn
[nsample
] * gain1
+ pPrev
[nsample
]) * gain2
;
564 /* interpolation is done over eight samples */
565 for (; nsample
< endLoc
; nsample
++) {
566 pOut
[nsample
] = (pIn
[nsample
] * gain1
+ pPrev
[nsample
]) * gain2
;
571 for (; nsample
< 256; nsample
++)
572 pOut
[nsample
] = (pIn
[nsample
] * gain1
) + pPrev
[nsample
];
575 /* Delay for the overlapping part. */
576 memcpy(pPrev
, &pIn
[256], 256*sizeof(float));
580 * Combine the tonal band spectrum and regular band spectrum
581 * Return position of the last tonal coefficient
583 * @param pSpectrum output spectrum buffer
584 * @param numComponents amount of tonal components
585 * @param pComponent tonal components for this band
588 static int addTonalComponents (float *pSpectrum
, int numComponents
, tonal_component
*pComponent
)
590 int cnt
, i
, lastPos
= -1;
593 for (cnt
= 0; cnt
< numComponents
; cnt
++){
594 lastPos
= FFMAX(pComponent
[cnt
].pos
+ pComponent
[cnt
].numCoefs
, lastPos
);
595 pIn
= pComponent
[cnt
].coef
;
596 pOut
= &(pSpectrum
[pComponent
[cnt
].pos
]);
598 for (i
=0 ; i
<pComponent
[cnt
].numCoefs
; i
++)
606 #define INTERPOLATE(old,new,nsample) ((old) + (nsample)*0.125*((new)-(old)))
608 static void reverseMatrixing(float *su1
, float *su2
, int *pPrevCode
, int *pCurrCode
)
610 int i
, band
, nsample
, s1
, s2
;
612 float mc1_l
, mc1_r
, mc2_l
, mc2_r
;
614 for (i
=0,band
= 0; band
< 4*256; band
+=256,i
++) {
620 /* Selector value changed, interpolation needed. */
621 mc1_l
= matrixCoeffs
[s1
*2];
622 mc1_r
= matrixCoeffs
[s1
*2+1];
623 mc2_l
= matrixCoeffs
[s2
*2];
624 mc2_r
= matrixCoeffs
[s2
*2+1];
626 /* Interpolation is done over the first eight samples. */
627 for(; nsample
< 8; nsample
++) {
628 c1
= su1
[band
+nsample
];
629 c2
= su2
[band
+nsample
];
630 c2
= c1
* INTERPOLATE(mc1_l
,mc2_l
,nsample
) + c2
* INTERPOLATE(mc1_r
,mc2_r
,nsample
);
631 su1
[band
+nsample
] = c2
;
632 su2
[band
+nsample
] = c1
* 2.0 - c2
;
636 /* Apply the matrix without interpolation. */
638 case 0: /* M/S decoding */
639 for (; nsample
< 256; nsample
++) {
640 c1
= su1
[band
+nsample
];
641 c2
= su2
[band
+nsample
];
642 su1
[band
+nsample
] = c2
* 2.0;
643 su2
[band
+nsample
] = (c1
- c2
) * 2.0;
648 for (; nsample
< 256; nsample
++) {
649 c1
= su1
[band
+nsample
];
650 c2
= su2
[band
+nsample
];
651 su1
[band
+nsample
] = (c1
+ c2
) * 2.0;
652 su2
[band
+nsample
] = c2
* -2.0;
657 for (; nsample
< 256; nsample
++) {
658 c1
= su1
[band
+nsample
];
659 c2
= su2
[band
+nsample
];
660 su1
[band
+nsample
] = c1
+ c2
;
661 su2
[band
+nsample
] = c1
- c2
;
670 static void getChannelWeights (int indx
, int flag
, float ch
[2]){
676 ch
[0] = (float)(indx
& 7) / 7.0;
677 ch
[1] = sqrt(2 - ch
[0]*ch
[0]);
679 FFSWAP(float, ch
[0], ch
[1]);
683 static void channelWeighting (float *su1
, float *su2
, int *p3
)
686 /* w[x][y] y=0 is left y=1 is right */
689 if (p3
[1] != 7 || p3
[3] != 7){
690 getChannelWeights(p3
[1], p3
[0], w
[0]);
691 getChannelWeights(p3
[3], p3
[2], w
[1]);
693 for(band
= 1; band
< 4; band
++) {
694 /* scale the channels by the weights */
695 for(nsample
= 0; nsample
< 8; nsample
++) {
696 su1
[band
*256+nsample
] *= INTERPOLATE(w
[0][0], w
[0][1], nsample
);
697 su2
[band
*256+nsample
] *= INTERPOLATE(w
[1][0], w
[1][1], nsample
);
700 for(; nsample
< 256; nsample
++) {
701 su1
[band
*256+nsample
] *= w
[1][0];
702 su2
[band
*256+nsample
] *= w
[1][1];
710 * Decode a Sound Unit
712 * @param gb the GetBit context
713 * @param pSnd the channel unit to be used
714 * @param pOut the decoded samples before IQMF in float representation
715 * @param channelNum channel number
716 * @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
720 static int decodeChannelSoundUnit (ATRAC3Context
*q
, GetBitContext
*gb
, channel_unit
*pSnd
, float *pOut
, int channelNum
, int codingMode
)
722 int band
, result
=0, numSubbands
, lastTonal
, numBands
;
724 if (codingMode
== JOINT_STEREO
&& channelNum
== 1) {
725 if (get_bits(gb
,2) != 3) {
726 av_log(NULL
,AV_LOG_ERROR
,"JS mono Sound Unit id != 3.\n");
730 if (get_bits(gb
,6) != 0x28) {
731 av_log(NULL
,AV_LOG_ERROR
,"Sound Unit id != 0x28.\n");
736 /* number of coded QMF bands */
737 pSnd
->bandsCoded
= get_bits(gb
,2);
739 result
= decodeGainControl (gb
, &(pSnd
->gainBlock
[pSnd
->gcBlkSwitch
]), pSnd
->bandsCoded
);
740 if (result
) return result
;
742 pSnd
->numComponents
= decodeTonalComponents (gb
, pSnd
->components
, pSnd
->bandsCoded
);
743 if (pSnd
->numComponents
== -1) return -1;
745 numSubbands
= decodeSpectrum (gb
, pSnd
->spectrum
);
747 /* Merge the decoded spectrum and tonal components. */
748 lastTonal
= addTonalComponents (pSnd
->spectrum
, pSnd
->numComponents
, pSnd
->components
);
751 /* calculate number of used MLT/QMF bands according to the amount of coded spectral lines */
752 numBands
= (subbandTab
[numSubbands
] - 1) >> 8;
754 numBands
= FFMAX((lastTonal
+ 256) >> 8, numBands
);
757 /* Reconstruct time domain samples. */
758 for (band
=0; band
<4; band
++) {
759 /* Perform the IMDCT step without overlapping. */
760 if (band
<= numBands
) {
761 IMLT(&(pSnd
->spectrum
[band
*256]), pSnd
->IMDCT_buf
, band
&1,q
->mdct_tmp
);
763 memset(pSnd
->IMDCT_buf
, 0, 512 * sizeof(float));
765 /* gain compensation and overlapping */
766 gainCompensateAndOverlap (pSnd
->IMDCT_buf
, &(pSnd
->prevFrame
[band
*256]), &(pOut
[band
*256]),
767 &((pSnd
->gainBlock
[1 - (pSnd
->gcBlkSwitch
)]).gBlock
[band
]),
768 &((pSnd
->gainBlock
[pSnd
->gcBlkSwitch
]).gBlock
[band
]));
771 /* Swap the gain control buffers for the next frame. */
772 pSnd
->gcBlkSwitch
^= 1;
780 * @param q Atrac3 private context
781 * @param databuf the input data
784 static int decodeFrame(ATRAC3Context
*q
, uint8_t* databuf
)
787 float *p1
, *p2
, *p3
, *p4
;
788 uint8_t *ptr1
, *ptr2
;
790 if (q
->codingMode
== JOINT_STEREO
) {
792 /* channel coupling mode */
793 /* decode Sound Unit 1 */
794 init_get_bits(&q
->gb
,databuf
,q
->bits_per_frame
);
796 result
= decodeChannelSoundUnit(q
,&q
->gb
, q
->pUnits
, q
->outSamples
, 0, JOINT_STEREO
);
800 /* Framedata of the su2 in the joint-stereo mode is encoded in
801 * reverse byte order so we need to swap it first. */
803 ptr2
= databuf
+q
->bytes_per_frame
-1;
804 for (i
= 0; i
< (q
->bytes_per_frame
/2); i
++, ptr1
++, ptr2
--) {
805 FFSWAP(uint8_t,*ptr1
,*ptr2
);
808 /* Skip the sync codes (0xF8). */
810 for (i
= 4; *ptr1
== 0xF8; i
++, ptr1
++) {
811 if (i
>= q
->bytes_per_frame
)
816 /* set the bitstream reader at the start of the second Sound Unit*/
817 init_get_bits(&q
->gb
,ptr1
,q
->bits_per_frame
);
819 /* Fill the Weighting coeffs delay buffer */
820 memmove(q
->weighting_delay
,&(q
->weighting_delay
[2]),4*sizeof(int));
821 q
->weighting_delay
[4] = get_bits1(&q
->gb
);
822 q
->weighting_delay
[5] = get_bits(&q
->gb
,3);
824 for (i
= 0; i
< 4; i
++) {
825 q
->matrix_coeff_index_prev
[i
] = q
->matrix_coeff_index_now
[i
];
826 q
->matrix_coeff_index_now
[i
] = q
->matrix_coeff_index_next
[i
];
827 q
->matrix_coeff_index_next
[i
] = get_bits(&q
->gb
,2);
830 /* Decode Sound Unit 2. */
831 result
= decodeChannelSoundUnit(q
,&q
->gb
, &q
->pUnits
[1], &q
->outSamples
[1024], 1, JOINT_STEREO
);
835 /* Reconstruct the channel coefficients. */
836 reverseMatrixing(q
->outSamples
, &q
->outSamples
[1024], q
->matrix_coeff_index_prev
, q
->matrix_coeff_index_now
);
838 channelWeighting(q
->outSamples
, &q
->outSamples
[1024], q
->weighting_delay
);
841 /* normal stereo mode or mono */
842 /* Decode the channel sound units. */
843 for (i
=0 ; i
<q
->channels
; i
++) {
845 /* Set the bitstream reader at the start of a channel sound unit. */
846 init_get_bits(&q
->gb
, databuf
+((i
*q
->bytes_per_frame
)/q
->channels
), (q
->bits_per_frame
)/q
->channels
);
848 result
= decodeChannelSoundUnit(q
,&q
->gb
, &q
->pUnits
[i
], &q
->outSamples
[i
*1024], i
, q
->codingMode
);
854 /* Apply the iQMF synthesis filter. */
856 for (i
=0 ; i
<q
->channels
; i
++) {
860 iqmf (p1
, p2
, 256, p1
, q
->pUnits
[i
].delayBuf1
, q
->tempBuf
);
861 iqmf (p4
, p3
, 256, p3
, q
->pUnits
[i
].delayBuf2
, q
->tempBuf
);
862 iqmf (p1
, p3
, 512, p1
, q
->pUnits
[i
].delayBuf3
, q
->tempBuf
);
871 * Atrac frame decoding
873 * @param avctx pointer to the AVCodecContext
876 static int atrac3_decode_frame(AVCodecContext
*avctx
,
877 void *data
, int *data_size
,
878 const uint8_t *buf
, int buf_size
) {
879 ATRAC3Context
*q
= avctx
->priv_data
;
882 int16_t* samples
= data
;
884 if (buf_size
< avctx
->block_align
)
887 /* Check if we need to descramble and what buffer to pass on. */
888 if (q
->scrambled_stream
) {
889 decode_bytes(buf
, q
->decoded_bytes_buffer
, avctx
->block_align
);
890 databuf
= q
->decoded_bytes_buffer
;
895 result
= decodeFrame(q
, databuf
);
898 av_log(NULL
,AV_LOG_ERROR
,"Frame decoding error!\n");
902 if (q
->channels
== 1) {
904 for (i
= 0; i
<1024; i
++)
905 samples
[i
] = av_clip_int16(round(q
->outSamples
[i
]));
906 *data_size
= 1024 * sizeof(int16_t);
909 for (i
= 0; i
< 1024; i
++) {
910 samples
[i
*2] = av_clip_int16(round(q
->outSamples
[i
]));
911 samples
[i
*2+1] = av_clip_int16(round(q
->outSamples
[1024+i
]));
913 *data_size
= 2048 * sizeof(int16_t);
916 return avctx
->block_align
;
921 * Atrac3 initialization
923 * @param avctx pointer to the AVCodecContext
926 static int atrac3_decode_init(AVCodecContext
*avctx
)
929 const uint8_t *edata_ptr
= avctx
->extradata
;
930 ATRAC3Context
*q
= avctx
->priv_data
;
932 /* Take data from the AVCodecContext (RM container). */
933 q
->sample_rate
= avctx
->sample_rate
;
934 q
->channels
= avctx
->channels
;
935 q
->bit_rate
= avctx
->bit_rate
;
936 q
->bits_per_frame
= avctx
->block_align
* 8;
937 q
->bytes_per_frame
= avctx
->block_align
;
939 /* Take care of the codec-specific extradata. */
940 if (avctx
->extradata_size
== 14) {
941 /* Parse the extradata, WAV format */
942 av_log(avctx
,AV_LOG_DEBUG
,"[0-1] %d\n",bytestream_get_le16(&edata_ptr
)); //Unknown value always 1
943 q
->samples_per_channel
= bytestream_get_le32(&edata_ptr
);
944 q
->codingMode
= bytestream_get_le16(&edata_ptr
);
945 av_log(avctx
,AV_LOG_DEBUG
,"[8-9] %d\n",bytestream_get_le16(&edata_ptr
)); //Dupe of coding mode
946 q
->frame_factor
= bytestream_get_le16(&edata_ptr
); //Unknown always 1
947 av_log(avctx
,AV_LOG_DEBUG
,"[12-13] %d\n",bytestream_get_le16(&edata_ptr
)); //Unknown always 0
950 q
->samples_per_frame
= 1024 * q
->channels
;
951 q
->atrac3version
= 4;
954 q
->codingMode
= JOINT_STEREO
;
956 q
->codingMode
= STEREO
;
958 q
->scrambled_stream
= 0;
960 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
)) {
962 av_log(avctx
,AV_LOG_ERROR
,"Unknown frame/channel/frame_factor configuration %d/%d/%d\n", q
->bytes_per_frame
, q
->channels
, q
->frame_factor
);
966 } else if (avctx
->extradata_size
== 10) {
967 /* Parse the extradata, RM format. */
968 q
->atrac3version
= bytestream_get_be32(&edata_ptr
);
969 q
->samples_per_frame
= bytestream_get_be16(&edata_ptr
);
970 q
->delay
= bytestream_get_be16(&edata_ptr
);
971 q
->codingMode
= bytestream_get_be16(&edata_ptr
);
973 q
->samples_per_channel
= q
->samples_per_frame
/ q
->channels
;
974 q
->scrambled_stream
= 1;
977 av_log(NULL
,AV_LOG_ERROR
,"Unknown extradata size %d.\n",avctx
->extradata_size
);
979 /* Check the extradata. */
981 if (q
->atrac3version
!= 4) {
982 av_log(avctx
,AV_LOG_ERROR
,"Version %d != 4.\n",q
->atrac3version
);
986 if (q
->samples_per_frame
!= 1024 && q
->samples_per_frame
!= 2048) {
987 av_log(avctx
,AV_LOG_ERROR
,"Unknown amount of samples per frame %d.\n",q
->samples_per_frame
);
991 if (q
->delay
!= 0x88E) {
992 av_log(avctx
,AV_LOG_ERROR
,"Unknown amount of delay %x != 0x88E.\n",q
->delay
);
996 if (q
->codingMode
== STEREO
) {
997 av_log(avctx
,AV_LOG_DEBUG
,"Normal stereo detected.\n");
998 } else if (q
->codingMode
== JOINT_STEREO
) {
999 av_log(avctx
,AV_LOG_DEBUG
,"Joint stereo detected.\n");
1001 av_log(avctx
,AV_LOG_ERROR
,"Unknown channel coding mode %x!\n",q
->codingMode
);
1005 if (avctx
->channels
<= 0 || avctx
->channels
> 2 /*|| ((avctx->channels * 1024) != q->samples_per_frame)*/) {
1006 av_log(avctx
,AV_LOG_ERROR
,"Channel configuration error!\n");
1011 if(avctx
->block_align
>= UINT_MAX
/2)
1014 /* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
1015 * this is for the bitstream reader. */
1016 if ((q
->decoded_bytes_buffer
= av_mallocz((avctx
->block_align
+(4-avctx
->block_align
%4) + FF_INPUT_BUFFER_PADDING_SIZE
))) == NULL
)
1017 return AVERROR(ENOMEM
);
1020 /* Initialize the VLC tables. */
1021 for (i
=0 ; i
<7 ; i
++) {
1022 init_vlc (&spectral_coeff_tab
[i
], 9, huff_tab_sizes
[i
],
1024 huff_codes
[i
], 1, 1, INIT_VLC_USE_STATIC
);
1027 init_atrac3_transforms(q
);
1029 /* Generate the scale factors. */
1030 for (i
=0 ; i
<64 ; i
++)
1031 SFTable
[i
] = pow(2.0, (i
- 15) / 3.0);
1033 /* Generate gain tables. */
1034 for (i
=0 ; i
<16 ; i
++)
1035 gain_tab1
[i
] = powf (2.0, (4 - i
));
1037 for (i
=-15 ; i
<16 ; i
++)
1038 gain_tab2
[i
+15] = powf (2.0, i
* -0.125);
1040 /* init the joint-stereo decoding data */
1041 q
->weighting_delay
[0] = 0;
1042 q
->weighting_delay
[1] = 7;
1043 q
->weighting_delay
[2] = 0;
1044 q
->weighting_delay
[3] = 7;
1045 q
->weighting_delay
[4] = 0;
1046 q
->weighting_delay
[5] = 7;
1048 for (i
=0; i
<4; i
++) {
1049 q
->matrix_coeff_index_prev
[i
] = 3;
1050 q
->matrix_coeff_index_now
[i
] = 3;
1051 q
->matrix_coeff_index_next
[i
] = 3;
1054 dsputil_init(&dsp
, avctx
);
1056 q
->pUnits
= av_mallocz(sizeof(channel_unit
)*q
->channels
);
1058 av_free(q
->decoded_bytes_buffer
);
1059 return AVERROR(ENOMEM
);
1066 AVCodec atrac3_decoder
=
1069 .type
= CODEC_TYPE_AUDIO
,
1070 .id
= CODEC_ID_ATRAC3
,
1071 .priv_data_size
= sizeof(ATRAC3Context
),
1072 .init
= atrac3_decode_init
,
1073 .close
= atrac3_decode_close
,
1074 .decode
= atrac3_decode_frame
,
1075 .long_name
= "Atrac 3 (Adaptive TRansform Acoustic Coding 3)",