|
Subject: [PATCH] ATRAC3 decoder Newsgroups: gmane.comp.video.ffmpeg.devel Date: 2007-02-18 10:34:29 GMT (2 years, 19 weeks, 4 days, 18 hours and 15 minutes ago) Incomplete specifications can be found here: http://wiki.multimedia.cx/index.php?title=RealAudio_atrc Samples here: http://samples.mplayerhq.hu/real/AC-atrc/ and here: http://samples.mplayerhq.hu/A-codecs/ATRAC3/ Currently atrac3 in oma/omg (http://samples.mplayerhq.hu/oma/) or psmf (http://samples.mplayerhq.hu/PSMF/) is not supported. Transcoding to/from the rm container would need a bitstream filter which is not supported either (yet). Any clarifications or fixes are welcome. MvH Benjamin Larsson
/*
* Atrac 3 compatible decoder
* Copyright (c) 2006-2007 Maxim Poliakovski
* Copyright (c) 2006-2007 Benjamin Larsson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file atrac3.c
* Atrac 3 compatible decoder.
* This decoder handles RealNetworks, RealAudio atrc data.
* Atrac 3 is identified by the codec name atrc in RealMedia files.
*
* To use this decoder, a calling application must supply the extradata
* bytes provided from the RealMedia container: 10 bytes or 14 bytes
* from the WAV container.
*/
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include "avcodec.h"
#include "bitstream.h"
#include "dsputil.h"
#include "bytestream.h"
#include "atrac3data.h"
#define JOINT_STEREO 0x12
#define STEREO 0x2
#define OK 0
#define ERROR -1
/* These structures are needed to store the parsed gain control data. */
typedef struct {
int num_gain_data;
int levcode[8];
int loccode[8];
} GAIN_INFO;
typedef struct {
GAIN_INFO gBlock[4];
} GAIN_BLOCK;
/* tonal component structure */
typedef struct {
int pos;
int numCoefs;
float coef[8];
} TONE_COMP;
typedef struct {
int bandsCoded;
int numComponents;
TONE_COMP components[64];
float prevFrame[1024];
int gcBlkSwitch;
GAIN_BLOCK gainBlock[2];
DECLARE_ALIGNED_16(float, spectrum[1024]);
DECLARE_ALIGNED_16(float, IMDCT_buf[1024]);
/* qmf delay buffers */
float delayBuf1[46];
float delayBuf2[46];
float delayBuf3[46];
} CHANNEL_UNIT;
typedef struct {
GetBitContext gb;
/* stream data */
int channels;
int codingMode;
int bit_rate;
int sample_rate;
int samples_per_channel;
int samples_per_frame;
int bits_per_frame;
int bytes_per_frame;
int pBs;
CHANNEL_UNIT* pUnits;
/* joint-stereo related variables */
int arr1C[4];
int arr2C[4];
int arr3C[4];
int arr4C[6];
/* data buffers */
float outSamples[2048];
uint8_t* decoded_bytes_buffer;
float tempBuf[1070];
/* extradata */
int atrac3version;
int delay;
int scrambled_stream;
int frame_factor;
} ATRAC3Context;
static DECLARE_ALIGNED_16(float,mdct_window[512]);
static float qmf_window[48];
static VLC spectral_coeff_tab[7];
static float SFTable[64];
static float gain_tab1[16];
static float gain_tab2[31];
/* FIXME: Should this be moved to the ATRAC3Context?
* Regarding multiple instances. */
static MDCTContext mdct_ctx;
static DSPContext dsp;
/* quadrature mirror synthesis filter */
static void iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp,
float *pWindow)
{
int cnt, j;
float *p1, *p2, *p3;
float s1, s2;
memcpy(temp, delayBuf, 46*sizeof(float));
p1 = inlo;
p2 = inhi;
p3 = temp + 46;
/* loop1 */
for (cnt = nIn / 2; cnt != 0; cnt--) {
/* Butterfly operation */
p3[0] = p1[0] + p2[0];
p3[1] = p1[0] - p2[0];
p3[2] = p1[1] + p2[1];
p3[3] = p1[1] - p2[1];
p1 += 2;
p2 += 2;
p3 += 4;
}
/* loop2 */
p1 = temp;
p2 = pOut;
for (j = nIn; j != 0; j--) {
s1 = 0.0;
s2 = 0.0;
for (cnt = 0; cnt < 48; cnt += 2) {
s1 += p1[cnt] * pWindow[cnt];
s2 += p1[cnt+1] * pWindow[cnt+1];
}
p2[0] = s2;
p2[1] = s1;
p1 += 2;
p2 += 2;
}
/* Update the delay buffer. */
memcpy(delayBuf, (char *)(temp + nIn*2), 46*sizeof(float));
}
/**
* Regular 512 points IMDCT, with the exception of the swapping of odd bands
*
* @param pInput float input
* @param pOutput float output
* @param odd_band 1 if the band is an odd band
*/
void IMLT_NoOverlap (float *pInput, float *pOutput, int odd_band)
{
//FIXME alignment problem when using SIMD ?
float ref_out[512];
int i;
if (odd_band) {
/**
* Reverse the odd bands before IMDCT, this is an effect of the QMF transform
* or it gives better compression to do it this way.
* FIXME: It should be possible to handle this in ff_imdct_calc
* for that to happen a modification of the prerotation step of
* all SIMD code and C code is needed.
* Or fix the functions before so they generate a pre reversed spectrum.
*/
memcpy(ref_out,pInput,256*sizeof(float));
for (i=0; i<256; i++)
pInput[i] = ref_out[255-i];
}
//ff_imdct_calc(&mdct_ctx,out,pInput,ref_out);
mdct_ctx.fft.imdct_calc(&mdct_ctx,pOutput,pInput,ref_out);
/* Perform windowing on the output. */
dsp.vector_fmul(pOutput,mdct_window,512);
/* for (i=0; i<512; i++) {
pOutput[i] *= mdct_window[i]; */
}
/**
* Atrac 3 indata descrambling, only used for data coming from the rm container
*
* @param in pointer to 8 bit array of indata
* @param bits amount of bits
* @param out pointer to 8 bit array of outdata
*/
static inline int decode_bytes(uint8_t* inbuffer, uint8_t* out, int bytes){
int i, off;
uint32_t c;
uint32_t* buf;
uint32_t* obuf = (uint32_t*) out;
off = (int)((long)inbuffer & 3);
buf = (uint32_t*) (inbuffer - off);
c = be2me_32((0x537F6103 >> (off*8)) | (0x537F6103 << (32-(off*8))));
bytes += 3 + off;
for (i = 0; i < bytes/4; i++)
obuf[i] = c ^ buf[i];
if (off)
av_log(NULL,AV_LOG_DEBUG,"Offset of %d not handled, post sample on ffmpeg-dev.\n",off);
return off;
}
static void init_atrac3_transforms(ATRAC3Context *q) {
float enc_window[256];
float s;
int i;
/* Generate the mdct window, for details see
* http://wiki.multimedia.cx/index.php?title=RealAudio_atrc#Windows */
for (i=0 ; i<256; i++)
enc_window[i] = (sin(((i + 0.5) / 256.0 - 0.5) * M_PI) + 1.0) * 0.5;
if (!mdct_window[0])
for (i=0 ; i<256; i++) {
mdct_window[i] = enc_window[i]/(enc_window[i]*enc_window[i] + enc_window[255-i]*enc_window[255-i]);
mdct_window[511-i] = mdct_window[i];
}
/* Generate the QMF window. */
for (i=0 ; i<24; i++) {
s = qmf_48tap_half[i] * 2.0;
qmf_window[i] = s;
qmf_window[47 - i] = s;
}
/* Initialize the MDCT transform. */
ff_mdct_init(&mdct_ctx, 9, 1);
}
/**
* Atrac3 uninit
*/
static int atrac3_decode_close(AVCodecContext *avctx)
{
ATRAC3Context *q = avctx->priv_data;
//int i;
av_log(NULL,AV_LOG_DEBUG, "Deallocating memory.\n");
/* Free allocated memory buffers. */
av_free(q->pUnits);
av_free(q->decoded_bytes_buffer);
ff_mdct_end(&mdct_ctx);
//FIXME needed or not ?
/* Free VLC tables. */
//for (i=0 ; i<7 ; i++)
// free_vlc(&spectral_coeff_tab[i]);
av_log(NULL,AV_LOG_DEBUG,"Memory deallocated.\n");
return OK;
}
/**
* Mantissa decoding
*
* @param gb the GetBit context
* @param selector what table is the output values coded with
* @param codingFlag constant length coding or variable length coding
* @param mantissas mantissa output table
* @param numCodes amount of values to get
*/
static void DecSpec (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes)
{
int numBits, cnt, signShift, code, huffSymb;
uint8_t *pTable;
if (selector == 1)
numCodes /= 2;
if (codingFlag != 0) {
/* constant length coding (CLC) */
numBits = CLCLengthTab[selector];
if (selector > 1) {
signShift = 32 - numBits;
for (cnt = 0; cnt < numCodes; cnt++) {
code = get_bits(gb, numBits);
/* sign extension */
code = (code << signShift) >> signShift;
mantissas[cnt] = code;
}
} else {
for (cnt = 0; cnt < numCodes; cnt++) {
code = get_bits(gb, numBits);
mantissas[cnt*2] = seTab_0[code >> 2];
mantissas[cnt*2+1] = seTab_0[code & 3];
}
}
} else {
/* variable length coding (VLC) */
pTable = (uint8_t*)decTables[selector];
if (selector != 1) {
for (cnt = 0; cnt < numCodes; cnt++) {
huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
spectral_coeff_tab[selector-1].bits, 3);
huffSymb += 1;
code = pTable[huffSymb >> 1];
if (huffSymb & 1)
code = -code;
mantissas[cnt] = code;
}
} else {
for (cnt = 0; cnt < numCodes; cnt++) {
huffSymb = get_vlc2(gb, spectral_coeff_tab[selector-1].table,
spectral_coeff_tab[selector-1].bits, 3);
mantissas[cnt*2] = decTable1[huffSymb*2];
mantissas[cnt*2+1] = decTable1[huffSymb*2+1];
}
}
}
}
/**
* Restore the quantized band spectrum coefficients
*
* @param gb the GetBit context
* @param pOut decoded band spectrum
* @param outSubbands subband counter, fix for broken specification/files
*/
static void decodeSpectrum (GetBitContext *gb, float *pOut, int *outSubbands)
{
int numSubbands, codingMode, cnt, first, last, subbWidth, *pIn;
int subband_vlc_index[32], SF_idxs[32];
int mantissas[128];
float SF;
numSubbands = get_bits(gb, 5); // number of coded subbands
codingMode = get_bits(gb, 1); // coding Mode: 0 - VLC/ 1-CLC
/* Return the amount of coded subbands. */
*outSubbands = numSubbands;
/* Get the VLC selector table for the subbands, 0 means not coded. */
for (cnt = 0; cnt <= numSubbands; cnt++) {
subband_vlc_index[cnt] = get_bits(gb, 3);
}
/* Read the scale factor indexes from the stream. */
for (cnt = 0; cnt <= numSubbands; cnt++) {
if (subband_vlc_index[cnt] != 0)
SF_idxs[cnt] = get_bits(gb, 6);
}
for (cnt = 0; cnt <= numSubbands; cnt++) {
first = subbandTab[cnt];
last = subbandTab[cnt+1];
subbWidth = last - first;
if (subband_vlc_index[cnt] != 0) {
/* Decode spectral coefficients for this subband. */
/* TODO: This can be done faster is several blocks share the
* same VLC selector (subband_vlc_index) */
DecSpec (gb, subband_vlc_index[cnt], codingMode, mantissas, subbWidth);
/* Decode the scale factor for this subband. */
SF = SFTable[SF_idxs[cnt]] * iMaxQuant[subband_vlc_index[cnt]];
/* Inverse quantize the coefficients. */
for (pIn=mantissas ; first<last; first++, pIn++)
pOut[first] = ((float)*pIn * SF);
} else {
/* This subband was not coded, so zero the entire subband. */
memset(&(pOut[first]), 0, subbWidth * 4);
}
}
/* Clear the subbands that were not coded. */
first = subbandTab[cnt];
memset(&(pOut[first]), 0, (1024 - first) * sizeof(float));
}
/**
* Restore the quantized tonal components
*
* @param gb the GetBit context
* @param numComponents tonal components to report back
* @param pComponent tone component
* @param numBands amount of coded bands
*/
static int decodeTonalComponents (GetBitContext *gb, int *numComponents, TONE_COMP *pComponent, int numBands)
{
int component_count, components, var48, var4C, i, var54, quant_step_index, cnt, j, var5C, var40;
int var60, sfIndx, coded_values, eax;
int band_flags[4];
int mantissa[8];
float *pCoef;
float sf;
component_count = 0;
*numComponents = 0;
components = get_bits(gb,5);
/* no tonal components */
if (components == 0)
return OK;
var48 = get_bits(gb,2);
if (var48 == 2)
return ERROR;
var4C = var48 & 1;
for (i = 0; i < components; i++) {
/* Read flags. */
for (cnt = 0; cnt <= numBands; cnt++)
band_flags[cnt] = get_bits1(gb);
var54 = get_bits(gb,3);
quant_step_index = get_bits(gb,3);
if (quant_step_index <= 1)
return ERROR;
if (var48 == 3)
var4C = get_bits1(gb);
for (j = 0; j < (numBands + 1) * 4; j++) {
if (band_flags[j >> 2] == 0)
continue;
var5C = get_bits(gb,3);
for (var40 = 0; var40 < var5C; var40++) {
sfIndx = get_bits(gb,6);
var60 = j * 64 + (get_bits(gb,6));
eax = 1024 - var60;
coded_values = var54 + 1;
if (coded_values > eax)
coded_values = eax;
sf = SFTable[sfIndx] * iMaxQuant[quant_step_index];
DecSpec(gb, quant_step_index, var4C, mantissa, coded_values);
(pComponent + component_count)->pos = var60;
(pComponent + component_count)->numCoefs = coded_values;
/* inverse quant */
pCoef = &((pComponent + component_count)->coef[0]);
for (cnt = 0; cnt < coded_values; cnt++)
pCoef[cnt] = ((float)mantissa[cnt] * sf);
component_count++;
}
}
}
*numComponents = component_count;
return OK;
}
/**
* Decode gain parameters for the coded bands
*
* @param gb the GetBit context
* @param pGb the gainblock for the current band
* @param numBands amount of coded bands
*/
static int decodeGainControl (GetBitContext *gb, GAIN_BLOCK *pGb, int numBands)
{
int i, cf, numData, loc;
int *pLevel, *pLoc;
GAIN_INFO *pGain = pGb->gBlock;
for (i=0 ; i<=numBands; i++)
{
numData = get_bits(gb,3);
pGain[i].num_gain_data = numData;
pLevel = pGain[i].levcode;
pLoc = pGain[i].loccode;
for (cf = 0; cf < numData; cf++)
{
*pLevel = get_bits(gb,4);
loc = get_bits(gb,5);
if ((cf > 0 && loc <= *(pLoc-1)) || (loc << 3) > 248)
return ERROR;
*pLoc = loc;
pLevel++;
pLoc++;
}
}
/* Clear the unused blocks. */
for (; i<4 ; i++)
pGain[i].num_gain_data = 0;
return OK;
}
/**
* Apply gain parameters and perform the MDCT overlapping part
*
* @param pIn input float buffer
* @param pPrev previous float buffer to perform overlap against
* @param pOut output float buffer
* @param pGain1 current band gain info
* @param pGain2 next band gain info
*/
static void gainCompensateAndOverlap (float *pIn, float *pPrev, float *pOut, GAIN_INFO *pGain1,
GAIN_INFO *pGain2)
{
/* gain compensation function */
float gain1, gain2, gain_inc;
int cnt, numdata, nsample, startLoc, endLoc;
if (pGain2->num_gain_data == 0)
gain1 = 1.0;
else
gain1 = gain_tab1[pGain2->levcode[0]];
if (pGain1->num_gain_data == 0) {
for (cnt = 0; cnt < 256; cnt++)
pOut[cnt] = (pIn[cnt] * gain1) + (pPrev[cnt]);
} else {
numdata = pGain1->num_gain_data;
pGain1->loccode[numdata] = 32;
pGain1->levcode[numdata] = 4;
nsample = 0; // current sample = 0
for (cnt = 0; cnt < numdata; cnt++) {
startLoc = (pGain1->loccode[cnt]) * 8;
endLoc = startLoc + 8;
gain2 = gain_tab1[pGain1->levcode[cnt]];
gain_inc = gain_tab2[(pGain1->levcode[cnt+1] - pGain1->levcode[cnt])+15];
/* interpolate */
for (; nsample < startLoc; nsample++)
pOut[nsample] = ((pIn[nsample] * gain1) + pPrev[nsample]) * gain2;
/* interpolation is done over eight samples */
for (; nsample < endLoc; nsample++) {
pOut[nsample] = ((pIn[nsample] * gain1) + pPrev[nsample]) * gain2;
gain2 *= gain_inc;
}
}
for (; nsample < 256; nsample++)
pOut[nsample] = (pIn[nsample] * gain1) + (pPrev[nsample]);
}
/* Delay for the overlapping part. */
memcpy(pPrev, &pIn[256], 256*sizeof(float));
}
/**
* Combine the tonal band spectrum and regular band spectrum
*
* @param pSpectrum output spectrum buffer
* @param numComponents amount of tonal components
* @param pComponent tonal components for this band
*/
static void addTonalComponents (float *pSpectrum, int numComponents, TONE_COMP *pComponent)
{
int cnt, cnt1;
float *pIn, *pOut;
for (cnt = 0; cnt < numComponents; cnt++)
{
pIn = &(pComponent[cnt].coef[0]);
pOut = &(pSpectrum[pComponent[cnt].pos]);
for (cnt1 = 0; cnt1 < (pComponent[cnt].numCoefs); cnt1++)
{
*pOut += *pIn;
pIn++;
pOut++;
}
}
}
#define INTERPOLATE(old,new,nsample) (((1.0-((float)(nsample)*0.125))*(old)) + (((float)(nsample)*0.125)*(new)))
static int applyChannelMatrix (float *su1, float *su2, int *pPrevCode, int *pCurrCode)
{
int band, nsample, s1, s2;
float c1, c2;
float mc1_l, mc1_r, mc2_l, mc2_r;
for (band = 0; band < 4; band++) {
s1 = pPrevCode[band];
s2 = pCurrCode[band];
nsample = 0;
if (s1 != s2) {
/* Selector value changed, interpolation needed. */
mc1_l = matrixCoeffs[s1*2];
mc1_r = matrixCoeffs[s1*2+1];
mc2_l = matrixCoeffs[s2*2];
mc2_r = matrixCoeffs[s2*2+1];
/* Interpolation is done over the first eight samples. */
for(; nsample < 8; nsample++) {
c1 = su1[band*256+nsample];
c2 = su2[band*256+nsample];
c2 = (c1 * INTERPOLATE(mc1_l,mc2_l,nsample)) + (c2 * INTERPOLATE(mc1_r,mc2_r,nsample));
su1[band*256+nsample] = c2;
su2[band*256+nsample] = c1 * 2.0 - c2;
}
}
/* Apply the matrix without interpolation. */
switch (s2) {
case 0: /* M/S decoding */
for (; nsample < 256; nsample++) {
c1 = su1[band*256+nsample];
c2 = su2[band*256+nsample];
su1[band*256+nsample] = c2 * 2.0;
su2[band*256+nsample] = (c1 - c2) * 2.0;
}
break;
case 1:
for (; nsample < 256; nsample++) {
c1 = su1[band*256+nsample];
c2 = su2[band*256+nsample];
su1[band*256+nsample] = (c1 + c2) * 2.0;
su2[band*256+nsample] = c2 * -2.0;
}
break;
case 2:
case 3:
for (; nsample < 256; nsample++) {
c1 = su1[band*256+nsample];
c2 = su2[band*256+nsample];
su1[band*256+nsample] = c1 + c2;
su2[band*256+nsample] = c1 - c2;
}
break;
default:
return ERROR;
}
}
}
static void getChannelWeights (int indx, int flag, float *ch1, float *ch2)
{
float w1, w2;
if (indx == 7) {
*ch1 = 1.0;
*ch2 = 1.0;
} else {
w1 = (float)(indx & 7) * (1.0/7.0);
w2 = sqrt(2.0 - (w1 * w1));
if (flag == 0) {
*ch1 = w1;
*ch2 = w2;
}
else {
*ch1 = w2;
*ch2 = w1;
}
}
}
static void applyChannelWeighting (float *su1, float *su2, int *p3)
{
int band, nsample;
float w1_l, w1_r, w2_l, w2_r, w3_l, w3_r;
if (p3[1] == 7 && p3[3] == 7)
return;
else {
getChannelWeights(p3[1], p3[0], &w1_l, &w1_r);
getChannelWeights(p3[3], p3[2], &w2_l, &w2_r);
for(band = 1; band < 4; band++) {
for(nsample = 0; nsample < 256; nsample++) {
if (nsample < 8) {
/* interpolation */
w3_l = INTERPOLATE(w1_l, w2_l, nsample);
w3_r = INTERPOLATE(w1_r, w2_r, nsample);
}
else {
w3_l = w2_l;
w3_r = w2_r;
}
/* scale the channels by the weights */
su1[band*256+nsample] *= w3_l;
su2[band*256+nsample] *= w3_r;
}
}
}
}
/**
* Decode a Sound Unit
*
* @param gb the GetBit context
* @param pSnd the channel unit to be used
* @param pOut the decoded samples before IQMF in float representation
* @param channelNum channel number
* @param codingMode the coding mode (JOINT_STEREO or regular stereo/mono)
*/
static int decodeChannelSoundUnit (GetBitContext *gb, CHANNEL_UNIT *pSnd, float *pOut, int
channelNum, int codingMode)
{
int band, result=0, numSubbands, numBands;
if (codingMode == JOINT_STEREO && channelNum == 1) {
if (get_bits(gb,2) != 3) {
av_log(NULL,AV_LOG_ERROR,"JS mono Sound Unit id != 3.\n");
return ERROR;
}
} else {
if (get_bits(gb,6) != 0x28) {
av_log(NULL,AV_LOG_ERROR,"Sound Unit id != 0x28.\n");
return ERROR;
}
}
/* number of coded QMF bands */
pSnd->bandsCoded = get_bits(gb,2);
result = decodeGainControl (gb, &(pSnd->gainBlock[pSnd->gcBlkSwitch]), pSnd->bandsCoded);
if (result) return result;
result = decodeTonalComponents (gb, &(pSnd->numComponents), &(pSnd->components[0]), pSnd->bandsCoded);
if (result) return (result);
decodeSpectrum (gb, pSnd->spectrum, &numSubbands);
/* Merge the decoded spectrum and tonal components. */
addTonalComponents (&(pSnd->spectrum[0]), pSnd->numComponents, &(pSnd->components[0]));
/* Convert number of subbands into number of MLT/QMF bands
* CAUTION: This may not match the bandsCoded parameter! */
numBands = ((subbandTab[numSubbands] + 255) >> 8) - 1;
/* Check if the first 8 coefficients in the not-coded band are zero,
* otherwise increment numBands. */
/* if ((numBands+1) < 4) {
for (i = 0, pCoef = &pSnd -> spectrum[(numBands+1)*256]; i < 8; i++) {
if (pCoef[i] != (float)0.0) {
numBands++;
break;
}
}
}*/
/* Reconstruct time domain samples. */
for (band=0; band<4; band++) {
/* Perform the IMDCT step without overlapping. */
if (band <= numBands) {
IMLT_NoOverlap (&(pSnd->spectrum[band*256]), &(pSnd->IMDCT_buf[0]), band&1);
} else
memset(&(pSnd->IMDCT_buf[0]), 0, 512 * sizeof(float));
/* gain compensation and overlapping */
gainCompensateAndOverlap (&(pSnd->IMDCT_buf[0]), &(pSnd->prevFrame[band*256]), &(pOut[band*256]),
&((pSnd->gainBlock[1 - (pSnd->gcBlkSwitch)]).gBlock[band]),
&((pSnd->gainBlock[pSnd->gcBlkSwitch]).gBlock[band]));
}
/* Swap the gain control buffers for the next frame. */
pSnd->gcBlkSwitch = 1 - (pSnd->gcBlkSwitch);
return OK;
}
/**
* Frame handling
*
* @param q Atrac3 private context
* @param databuf the input data
*/
static int decodeFrame(ATRAC3Context *q, uint8_t* databuf)
{
int result, i;
float *p1, *p2, *p3, *p4;
uint8_t *ptr1, *ptr2, tmp;
if (q->codingMode == JOINT_STEREO) {
/* channel coupling mode */
/* decode Sound Unit 1 */
init_get_bits(&q->gb,databuf,q->bits_per_frame);
result = decodeChannelSoundUnit(&q->gb, &q->pUnits[0], &q->outSamples[0], 0, JOINT_STEREO);
if (result != OK)
return (result);
/* Framedata of the su2 in the joint-stereo mode is encoded in
* reverse byte order so we need to swap it first. */
ptr1 = databuf;
ptr2 = databuf+q->bytes_per_frame-1;
for (i = 0; i < (q->bytes_per_frame/2); i++, ptr1++, ptr2--) {
tmp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = tmp;
}
/* Skip the sync codes (0xF8). */
ptr1 = databuf;
for (i = 4; *ptr1 == 0xF8; i++, ptr1++) {
if (i >= q->bytes_per_frame)
return ERROR;
}
/* set the bitstream reader at the start of the second Sound Unit*/
init_get_bits(&q->gb,ptr1,q->bits_per_frame);
q->arr4C[0] = q->arr4C[2];
q->arr4C[1] = q->arr4C[3];
q->arr4C[2] = q->arr4C[4];
q->arr4C[3] = q->arr4C[5];
q->arr4C[4] = get_bits(&q->gb,1);
q->arr4C[5] = get_bits(&q->gb,3);
for (i = 0; i < 4; i++) {
q->arr1C[i] = q->arr2C[i];
q->arr2C[i] = q->arr3C[i];
q->arr3C[i] = get_bits(&q->gb,2);
}
/* Decode Sound Unit 2. */
result = decodeChannelSoundUnit(&q->gb, &q->pUnits[1], &q->outSamples[1024], 1, JOINT_STEREO);
if (result != OK)
return (result);
/* Reconstruct the channel coefficients. */
applyChannelMatrix(&q->outSamples[0], &q->outSamples[1024], &q->arr1C[0], &q->arr2C[0]);
applyChannelWeighting(&q->outSamples[0], &q->outSamples[1024], &q->arr4C[0]);
} else {
/* normal stereo mode or mono */
/* Decode the channel sound units. */
for (i=0 ; i<q->channels ; i++) {
/* Set the bitstream reader at the start of a channel sound unit. */
init_get_bits(&q->gb, databuf+((i*q->bytes_per_frame)/q->channels), (q->bits_per_frame)/q->channels);
result = decodeChannelSoundUnit(&q->gb, &q->pUnits[i], &q->outSamples[i*1024], i, q->codingMode);
if (result != OK)
return (result);
}
}
/* Apply the iQMF synthesis filter. */
p1 = q->outSamples;
p2 = &(q->outSamples[256]);
p3 = &(q->outSamples[512]);
p4 = &(q->outSamples[768]);
for (i=0 ; i<q->channels ; i++) {
iqmf (p1, p2, 256, p1, q->pUnits[i].delayBuf1, q->tempBuf, qmf_window);
iqmf (p4, p3, 256, p3, q->pUnits[i].delayBuf2, q->tempBuf, qmf_window);
iqmf (p1, p3, 512, p1, q->pUnits[i].delayBuf3, q->tempBuf, qmf_window);
p1 += 1024;
p2 += 1024;
p3 += 1024;
p4 += 1024;
}
return OK;
}
/**
* Atrac frame decoding
*
* @param avctx pointer to the AVCodecContext
*/
static int atrac3_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
uint8_t *buf, int buf_size) {
ATRAC3Context *q = avctx->priv_data;
int result = 0, i;
uint8_t* databuf;
int16_t* samples = (int16_t*)data;
if (buf_size < avctx->block_align)
return buf_size;
/* Check if we need to descramble and what buffer to pass on. */
if (q->scrambled_stream) {
decode_bytes(buf, q->decoded_bytes_buffer, avctx->block_align);
databuf = q->decoded_bytes_buffer;
} else {
databuf = buf;
}
result = decodeFrame(q, databuf);
if (result != OK) {
av_log(NULL,AV_LOG_ERROR,"Frame decoding error!\n");
return ERROR;
}
if (q->channels == 1) {
/* mono */
for (i = 0; i<1024; i++)
samples[i] = clip(lrintf(q->outSamples[i]), -32768, 32767);
*data_size = 1024 * sizeof(int16_t);
} else {
/* stereo */
for (i = 0; i < 1024; i++) {
samples[i*2] = clip(round(q->outSamples[i]), -32768, 32767);
samples[i*2+1] = clip(round(q->outSamples[1024+i]), -32768, 32767);
}
*data_size = 2048 * sizeof(int16_t);
}
return avctx->block_align;
}
/**
* Atrac3 initialization
*
* @param avctx pointer to the AVCodecContext
*/
static int atrac3_decode_init(AVCodecContext *avctx)
{
int i;
uint8_t *edata_ptr = avctx->extradata;
ATRAC3Context *q = avctx->priv_data;
/* Take data from the AVCodecContext (RM container). */
q->sample_rate = avctx->sample_rate;
q->channels = avctx->channels;
q->bit_rate = avctx->bit_rate;
q->bits_per_frame = avctx->block_align * 8;
q->bytes_per_frame = avctx->block_align;
/* Take care of the codec-specific extradata. */
if (avctx->extradata_size == 14) {
/* Parse the extradata, WAV format */
av_log(avctx,AV_LOG_DEBUG,"[0-1] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown value
always 1
q->samples_per_channel = bytestream_get_le32(&edata_ptr);
q->codingMode = bytestream_get_le16(&edata_ptr);
av_log(avctx,AV_LOG_DEBUG,"[8-9] %d\n",bytestream_get_le16(&edata_ptr)); //Dupe of coding mode
q->frame_factor = bytestream_get_le16(&edata_ptr); //Unknown always 1
av_log(avctx,AV_LOG_DEBUG,"[12-13] %d\n",bytestream_get_le16(&edata_ptr)); //Unknown always 0
/* setup */
q->samples_per_frame = 1024 * q->channels;
q->atrac3version = 4;
q->delay = 0x88E;
if (q->codingMode)
q->codingMode = JOINT_STEREO;
else
q->codingMode = STEREO;
q->scrambled_stream = 0;
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)) {
} else {
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);
return -1;
}
} else if (avctx->extradata_size == 10) {
/* Parse the extradata, RM format. */
q->atrac3version = be2me_32(bytestream_get_le32(&edata_ptr));
q->samples_per_frame = be2me_16(bytestream_get_le16(&edata_ptr));
q->delay = be2me_16(bytestream_get_le16(&edata_ptr));
q->codingMode = be2me_16(bytestream_get_le16(&edata_ptr));
q->samples_per_channel = q->samples_per_frame / q->channels;
q->scrambled_stream = 1;
} else {
av_log(NULL,AV_LOG_ERROR,"Unknown extradata size %d.\n",avctx->extradata_size);
}
/* Check the extradata. */
if (q->atrac3version != 4) {
av_log(avctx,AV_LOG_ERROR,"Version %d != 4.\n",q->atrac3version);
return -1;
}
if ((q->samples_per_frame != 1024) && (q->samples_per_frame != 2048)) {
av_log(avctx,AV_LOG_ERROR,"Unknown amount of samples per frame %d.\n",q->samples_per_frame);
return -1;
}
if (q->delay != 0x88E) {
av_log(avctx,AV_LOG_ERROR,"Unknown amount of delay %x != 0x88E.\n",q->delay);
return -1;
}
if (q->codingMode == STEREO) {
av_log(avctx,AV_LOG_DEBUG,"Normal stereo detected.\n");
} else if (q->codingMode == JOINT_STEREO) {
av_log(avctx,AV_LOG_DEBUG,"Joint stereo detected.\n");
} else {
av_log(avctx,AV_LOG_ERROR,"Unknown channel coding mode %x!\n",q->codingMode);
return -1;
}
if (avctx->channels <= 0 || avctx->channels > 2 /*|| ((avctx->channels * 1024) !=
q->samples_per_frame)*/) {
av_log(avctx,AV_LOG_ERROR,"Channel configuration error!\n");
return -1;
}
if(avctx->block_align >= UINT_MAX/2)
return -1;
/* Pad the data buffer with FF_INPUT_BUFFER_PADDING_SIZE,
* this is for the bitstream reader. */
if ((q->decoded_bytes_buffer = av_mallocz((avctx->block_align+(4-
/*
* Atrac 3 compatible decoder data
* Copyright (c) 2006-2207 Maxim Poliakovski
* Copyright (c) 2006-2007 Benjamin Larsson
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file atrac3data.h
* Atrac 3 AKA RealAudio 8 compatible decoder data
*/
/* VLC tables */
static const uint8_t huffcode1[9] = {
0x0,0x4,0x5,0xC,0xD,0x1C,0x1D,0x1E,0x1F,
};
static const uint8_t huffbits1[9] = {
1,3,3,4,4,5,5,5,5,
};
static const uint8_t huffcode2[5] = {
0x0,0x4,0x5,0x6,0x7,
};
static const uint8_t huffbits2[5] = {
1,3,3,3,3,
};
static const uint8_t huffcode3[7] = {
0x0,0x4,0x5,0xC,0xD,0xE,0xF,
};
static const uint8_t huffbits3[7] = {
1,3,3,4,4,4,4,
};
static const uint8_t huffcode4[9] = {
0x0,0x4,0x5,0xC,0xD,0x1C,0x1D,0x1E,0x1F,
};
static const uint8_t huffbits4[9] = {
1,3,3,4,4,5,5,5,5,
};
static const uint8_t huffcode5[15] = {
0x0,0x2,0x3,0x8,0x9,0xA,0xB,0xC,0xD,0x1C,0x1D,0x3C,0x3D,0x3E,0x3F,
};
static const uint8_t huffbits5[15] = {
2,3,3,4,4,4,4,4,4,5,5,6,6,6,6,
};
static const uint8_t huffcode6[31] = {
0x0,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0x14,0x15,0x16,0x17,0x18,0x19,0x34,0x35,
0x36,0x37,0x38,0x39,0x3A,0x3B,0x78,0x79,0x7A,0x7B,0x7C,0x7D,0x7E,0x7F,
};
static const uint8_t huffbits6[31] = {
3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,
};
static const uint8_t huffcode7[63] = {
0x0,0x2,0x3,0x8,0x9,0xA,0xB,0xC,0xD,0xE,0xF,0x10,0x11,0x24,0x25,0x26,0x27,0x28,
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x68,0x69,0x6A,0x6B,0x6C,
0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0xEC,0xED,0xEE,0xEF,0xF0,0xF1,0xF2,
0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFE,0xFF,
};
static const uint8_t huffbits7[63] = {
3,4,4,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,
7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
};
static const uint8_t huff_tab_sizes[7] = {
9, 5, 7, 9, 15, 31, 63,
};
static const uint8_t* huff_codes[7] = {
huffcode1,huffcode2,huffcode3,huffcode4,huffcode5,huffcode6,huffcode7,
};
static const uint8_t* huff_bits[7] = {
huffbits1,huffbits2,huffbits3,huffbits4,huffbits5,huffbits6,huffbits7,
};
/* selector tables */
static const uint8_t CLCLengthTab[8] = {0, 4, 3, 3, 4, 4, 5, 6};
static const int8_t seTab_0[4] = {0, 1, -2, -1};
static const int8_t decTable1[18] = {0,0, 0,1, 0,-1, 1,0, -1,0, 1,1, 1,-1, -1,1, -1,-1};
static const uint8_t decTable2[3] = {0, 1, 2};
static const uint8_t decTable4[4] = {0, 1, 2, 3};
static const uint8_t decTable6[7] = {0, 1, 2, 3, 4, 0, 0};
static const uint8_t decTable8[8] = {0, 1, 2, 3, 7, 4, 5, 6};
static const uint8_t decTable10[16] = {0, 1, 2, 3, 15, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
static const uint8_t decTable12[32] = {
0, 0x1F, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E
};
static const uint8_t *decTables[8] = {0, 0, decTable2, decTable4, decTable6, decTable8, decTable10, decTable12};
/* tables for the scalefactor decoding */
static const float mantissaTab[3] = {
0.62996054, 0.79370052, 1.0
};
//reciprocals table
// 1/1.5 1/2.5 1/3.5 1/4.5 1/7.5 1/15.5 1/31.5
static const float iMaxQuant[8] = {
0.0, 0.66666669, 0.40000001, 0.2857143, 0.22222222, 0.13333334, 0.064516127, 0.031746034
};
static const uint16_t subbandTab[33] = {
0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224,
256, 288, 320, 352, 384, 416, 448, 480, 512, 576, 640, 704, 768, 896, 1024
};
/* transform data */
static const float qmf_48tap_half[24] = {
-0.00001461907, -0.00009205479, -0.000056157569, 0.00030117269,
0.0002422519,-0.00085293897, -0.0005205574, 0.0020340169,
0.00078333891, -0.0042153862, -0.00075614988, 0.0078402944,
-0.000061169922, -0.01344162, 0.0024626821, 0.021736089,
-0.007801671, -0.034090221, 0.01880949, 0.054326009,
-0.043596379, -0.099384367, 0.13207909, 0.46424159
};
/* joint stereo related tables */
static const float matrixCoeffs[8] = {0.0, 2.0, 2.0, 2.0, 0.0, 0.0, 1.0, 1.0};
Index: libavcodec/allcodecs.c
===================================================================
--- libavcodec/allcodecs.c (revision 7965)
+++ libavcodec/allcodecs.c (working copy)
@@ -53,6 +53,7 @@
REGISTER_DECODER(AASC, aasc);
REGISTER_ENCDEC (ASV1, asv1);
REGISTER_ENCDEC (ASV2, asv2);
+ REGISTER_DECODER(ATRAC3, atrac3);
REGISTER_DECODER(AVS, avs);
REGISTER_ENCDEC (BMP, bmp);
REGISTER_DECODER(CAVS, cavs);
Index: libavcodec/Makefile
===================================================================
--- libavcodec/Makefile (revision 7965)
+++ libavcodec/Makefile (working copy)
@@ -53,6 +53,7 @@
OBJS-$(CONFIG_ASV1_ENCODER) += asv1.o
OBJS-$(CONFIG_ASV2_DECODER) += asv1.o
OBJS-$(CONFIG_ASV2_ENCODER) += asv1.o
+OBJS-$(CONFIG_ATRAC3_DECODER) += atrac3.o
OBJS-$(CONFIG_AVS_DECODER) += avs.o
OBJS-$(CONFIG_BMP_DECODER) += bmp.o
OBJS-$(CONFIG_BMP_ENCODER) += bmpenc.o
Index: libavcodec/avcodec.h
===================================================================
--- libavcodec/avcodec.h (revision 7965)
+++ libavcodec/avcodec.h (working copy)
@@ -239,6 +239,7 @@
CODEC_ID_IMC,
CODEC_ID_MUSEPACK7,
CODEC_ID_MLP,
+ CODEC_ID_ATRAC3,
/* subtitle codecs */
CODEC_ID_DVD_SUBTITLE= 0x17000,
@@ -2208,6 +2209,7 @@
extern AVCodec amr_wb_decoder;
extern AVCodec asv1_decoder;
extern AVCodec asv2_decoder;
+extern AVCodec atrac3_decoder;
extern AVCodec avs_decoder;
extern AVCodec bmp_decoder;
extern AVCodec cavs_decoder;
Index: libavformat/rm.c
===================================================================
--- libavformat/rm.c (revision 7965)
+++ libavformat/rm.c (working copy)
@@ -565,7 +565,7 @@
}
rm->audiobuf = av_malloc(rm->audio_framesize * sub_packet_h);
- } else if (!strcmp(buf, "cook")) {
+ } else if ((!strcmp(buf, "cook")) || (!strcmp(buf, "atrc"))) {
int codecdata_length, i;
get_be16(pb); get_byte(pb);
if (((version >> 16) & 0xff) == 5)
@@ -576,7 +576,8 @@
return -1;
}
- st->codec->codec_id = CODEC_ID_COOK;
+ if (!strcmp(buf, "cook")) st->codec->codec_id = CODEC_ID_COOK;
+ else st->codec->codec_id = CODEC_ID_ATRAC3;
st->codec->extradata_size= codecdata_length;
st->codec->extradata= av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
for(i = 0; i < codecdata_length; i++)
@@ -958,7 +959,8 @@
if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
if ((st->codec->codec_id == CODEC_ID_RA_288) ||
- (st->codec->codec_id == CODEC_ID_COOK)) {
+ (st->codec->codec_id == CODEC_ID_COOK) ||
+ (st->codec->codec_id == CODEC_ID_ATRAC3)) {
int x;
int sps = rm->sub_packet_size;
int cfs = rm->coded_framesize;
@@ -976,6 +978,7 @@
for (x = 0; x < h/2; x++)
get_buffer(pb, rm->audiobuf+x*2*w+y*cfs, cfs);
break;
+ case CODEC_ID_ATRAC3:
case CODEC_ID_COOK:
for (x = 0; x < w/sps; x++)
get_buffer(pb, rm->audiobuf+sps*(h*x+((h+1)/2)*(y&1)+(y>>1)), sps);
_______________________________________________ ffmpeg-devel mailing list ffmpeg-devel <at> mplayerhq.hu http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-devel |
|
|