📄 atrac3.c
字号:
/* * 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/* 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;typedef struct { int pos; int numCoefs; float coef[8];} tonal_component;typedef struct { int bandsCoded; int numComponents; tonal_component components[64]; float prevFrame[1024]; int gcBlkSwitch; gain_block gainBlock[2]; DECLARE_ALIGNED_16(float, spectrum[1024]); DECLARE_ALIGNED_16(float, IMDCT_buf[1024]); float delayBuf1[46]; ///<qmf delay buffers 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 matrix_coeff_index_prev[4]; int matrix_coeff_index_now[4]; int matrix_coeff_index_next[4]; int weighting_delay[6]; //@} //@{ /** data buffers */ float outSamples[2048]; uint8_t* decoded_bytes_buffer; float tempBuf[1070]; DECLARE_ALIGNED_16(float,mdct_tmp[512]); //@} //@{ /** 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];static MDCTContext mdct_ctx;static DSPContext dsp;/* quadrature mirror synthesis filter *//** * Quadrature mirror synthesis filter. * * @param inlo lower part of spectrum * @param inhi higher part of spectrum * @param nIn size of spectrum buffer * @param pOut out buffer * @param delayBuf delayBuf buffer * @param temp temp buffer */static void iqmf (float *inlo, float *inhi, unsigned int nIn, float *pOut, float *delayBuf, float *temp){ int i, j; float *p1, *p3; memcpy(temp, delayBuf, 46*sizeof(float)); p3 = temp + 46; /* loop1 */ for(i=0; i<nIn; i+=2){ p3[2*i+0] = inlo[i ] + inhi[i ]; p3[2*i+1] = inlo[i ] - inhi[i ]; p3[2*i+2] = inlo[i+1] + inhi[i+1]; p3[2*i+3] = inlo[i+1] - inhi[i+1]; } /* loop2 */ p1 = temp; for (j = nIn; j != 0; j--) { float s1 = 0.0; float s2 = 0.0; for (i = 0; i < 48; i += 2) { s1 += p1[i] * qmf_window[i]; s2 += p1[i+1] * qmf_window[i+1]; } pOut[0] = s2; pOut[1] = s1; p1 += 2; pOut += 2; } /* Update the delay buffer. */ memcpy(delayBuf, temp + nIn*2, 46*sizeof(float));}/** * Regular 512 points IMDCT without overlapping, with the exception of the swapping of odd bands * caused by the reverse spectra of the QMF. * * @param pInput float input * @param pOutput float output * @param odd_band 1 if the band is an odd band * @param mdct_tmp aligned temporary buffer for the mdct */static void IMLT(float *pInput, float *pOutput, int odd_band, float* mdct_tmp){ 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. */ for (i=0; i<128; i++) FFSWAP(float, pInput[i], pInput[255-i]); } mdct_ctx.fft.imdct_calc(&mdct_ctx,pOutput,pInput,mdct_tmp); /* Perform windowing on the output. */ dsp.vector_fmul(pOutput,mdct_window,512);}/** * 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 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, free all allocated memory */static int atrac3_decode_close(AVCodecContext *avctx){ ATRAC3Context *q = avctx->priv_data; av_free(q->pUnits); av_free(q->decoded_bytes_buffer); return 0;}/**/ * 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 readQuantSpectralCoeffs (GetBitContext *gb, int selector, int codingFlag, int* mantissas, int numCodes){ int numBits, cnt, code, huffSymb; if (selector == 1) numCodes /= 2; if (codingFlag != 0) { /* constant length coding (CLC) */ //FIXME we don't have any samples coded in CLC mode numBits = CLCLengthTab[selector]; if (selector > 1) { for (cnt = 0; cnt < numCodes; cnt++) { if (numBits) code = get_sbits(gb, numBits); else code = 0; mantissas[cnt] = code; } } else { for (cnt = 0; cnt < numCodes; cnt++) { if (numBits) code = get_bits(gb, numBits); //numBits is always 4 in this case else code = 0; mantissas[cnt*2] = seTab_0[code >> 2]; mantissas[cnt*2+1] = seTab_0[code & 3]; } } } else { /* variable length coding (VLC) */ 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 = 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 * @return outSubbands subband counter, fix for broken specification/files */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -