2009-05-10 22:26:02 +00:00
|
|
|
/*
|
|
|
|
* COOK compatible decoder, fixed point implementation.
|
|
|
|
* Copyright (c) 2007 Ian Braithwaite
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-08-13 20:26:31 +00:00
|
|
|
* @file cook_fixpoint.h
|
2009-05-10 22:26:02 +00:00
|
|
|
*
|
|
|
|
* Cook AKA RealAudio G2 fixed point functions.
|
|
|
|
*
|
|
|
|
* Fixed point values are represented as 32 bit signed integers,
|
|
|
|
* which can be added and subtracted directly in C (without checks for
|
|
|
|
* overflow/saturation.
|
|
|
|
* Two multiplication routines are provided:
|
|
|
|
* 1) Multiplication by powers of two (2^-31 .. 2^31), implemented
|
|
|
|
* with C's bit shift operations.
|
|
|
|
* 2) Multiplication by 16 bit fractions (0 <= x < 1), implemented
|
|
|
|
* in C using two 32 bit integer multiplications.
|
|
|
|
*/
|
|
|
|
|
2009-07-27 11:28:14 +00:00
|
|
|
#ifdef ROCKBOX
|
2010-02-21 22:05:48 +00:00
|
|
|
/* get definitions of MULT31, MULT31_SHIFT15, vect_add, from codelib */
|
2009-07-26 19:06:36 +00:00
|
|
|
#include "codeclib_misc.h"
|
2009-12-05 16:47:43 +00:00
|
|
|
#include "codeclib.h"
|
2009-07-27 11:28:14 +00:00
|
|
|
#endif
|
2009-07-26 19:06:36 +00:00
|
|
|
|
2009-05-12 20:50:35 +00:00
|
|
|
/* cplscales was moved from cookdata_fixpoint.h since only *
|
|
|
|
* cook_fixpoint.h should see/use it. */
|
|
|
|
static const FIXPU* cplscales[5] = {
|
|
|
|
cplscale2, cplscale3, cplscale4, cplscale5, cplscale6
|
|
|
|
};
|
|
|
|
|
2009-05-10 22:26:02 +00:00
|
|
|
/**
|
|
|
|
* Fixed point multiply by power of two.
|
|
|
|
*
|
|
|
|
* @param x fix point value
|
|
|
|
* @param i integer power-of-two, -31..+31
|
|
|
|
*/
|
|
|
|
static inline FIXP fixp_pow2(FIXP x, int i)
|
|
|
|
{
|
|
|
|
if (i < 0)
|
2010-02-21 21:18:05 +00:00
|
|
|
return (x >> -i);
|
2009-05-10 22:26:02 +00:00
|
|
|
else
|
|
|
|
return x << i; /* no check for overflow */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fixed point multiply by fraction.
|
|
|
|
*
|
|
|
|
* @param a fix point value
|
|
|
|
* @param b fix point fraction, 0 <= b < 1
|
|
|
|
*/
|
2009-07-27 11:28:14 +00:00
|
|
|
#ifdef ROCKBOX
|
2009-07-26 19:06:36 +00:00
|
|
|
#define fixp_mult_su(x,y) (MULT31_SHIFT15(x,y))
|
2009-07-27 11:28:14 +00:00
|
|
|
#else
|
|
|
|
static inline FIXP fixp_mult_su(FIXP a, FIXPU b)
|
|
|
|
{
|
2010-02-21 19:47:05 +00:00
|
|
|
int32_t hb = (a >> 16) * b;
|
|
|
|
uint32_t lb = (a & 0xffff) * b;
|
2009-07-27 11:28:14 +00:00
|
|
|
|
2010-02-21 19:47:05 +00:00
|
|
|
return hb + (lb >> 16) + ((lb & 0x8000) >> 15);
|
2009-07-27 11:28:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-10 22:26:02 +00:00
|
|
|
|
2009-07-17 23:53:40 +00:00
|
|
|
/* Faster version of the above using 32x32=64 bit multiply */
|
2009-07-27 11:28:14 +00:00
|
|
|
#ifdef ROCKBOX
|
2009-07-26 19:06:36 +00:00
|
|
|
#define fixmul31(x,y) (MULT31(x,y))
|
2010-02-21 19:47:05 +00:00
|
|
|
#else
|
|
|
|
static inline int32_t fixmul31(int32_t x, int32_t y)
|
|
|
|
{
|
|
|
|
int64_t temp;
|
2009-07-27 11:28:14 +00:00
|
|
|
|
2010-02-21 19:47:05 +00:00
|
|
|
temp = x;
|
|
|
|
temp *= y;
|
2009-07-27 11:28:14 +00:00
|
|
|
|
2010-02-21 19:47:05 +00:00
|
|
|
temp >>= 31; //16+31-16 = 31 bits
|
2009-07-27 11:28:14 +00:00
|
|
|
|
2010-02-21 19:47:05 +00:00
|
|
|
return (int32_t)temp;
|
|
|
|
}
|
2009-07-27 11:28:14 +00:00
|
|
|
#endif
|
2009-07-17 23:53:40 +00:00
|
|
|
|
2009-05-12 20:50:35 +00:00
|
|
|
/**
|
|
|
|
* Clips a signed integer value into the amin-amax range.
|
|
|
|
* @param a value to clip
|
|
|
|
* @param amin minimum value of the clip range
|
|
|
|
* @param amax maximum value of the clip range
|
|
|
|
* @return clipped value
|
|
|
|
*/
|
|
|
|
static inline int av_clip(int a, int amin, int amax)
|
|
|
|
{
|
|
|
|
if (a < amin) return amin;
|
|
|
|
else if (a > amax) return amax;
|
|
|
|
else return a;
|
|
|
|
}
|
2009-05-10 22:26:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The real requantization of the mltcoefs
|
|
|
|
*
|
|
|
|
* @param q pointer to the COOKContext
|
|
|
|
* @param index index
|
|
|
|
* @param quant_index quantisation index for this band
|
|
|
|
* @param subband_coef_index array of indexes to quant_centroid_tab
|
|
|
|
* @param subband_coef_sign use random noise instead of predetermined value
|
|
|
|
* @param mlt_ptr pointer to the mlt coefficients
|
|
|
|
*/
|
|
|
|
static void scalar_dequant_math(COOKContext *q, int index,
|
|
|
|
int quant_index, int* subband_coef_index,
|
|
|
|
int* subband_coef_sign, REAL_T *mlt_p)
|
|
|
|
{
|
|
|
|
/* Num. half bits to right shift */
|
2009-07-26 19:06:36 +00:00
|
|
|
const int s = (33 - quant_index + av_log2(q->samples_per_channel)) >> 1;
|
2009-05-10 22:26:02 +00:00
|
|
|
const FIXP *table = quant_tables[s & 1][index];
|
|
|
|
FIXP f;
|
|
|
|
int i;
|
|
|
|
|
2009-07-21 03:40:53 +00:00
|
|
|
|
2009-07-26 19:06:36 +00:00
|
|
|
if(s >= 32)
|
2009-07-21 03:47:41 +00:00
|
|
|
memset(mlt_p, 0, sizeof(REAL_T)*SUBBAND_SIZE);
|
2009-07-21 03:40:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for(i=0 ; i<SUBBAND_SIZE ; i++) {
|
2010-02-21 21:18:05 +00:00
|
|
|
f = (table[subband_coef_index[i]])>>s;
|
2009-07-21 03:40:53 +00:00
|
|
|
/* noise coding if subband_coef_index[i] == 0 */
|
|
|
|
if (((subband_coef_index[i] == 0) && cook_random(q)) ||
|
|
|
|
((subband_coef_index[i] != 0) && subband_coef_sign[i]))
|
|
|
|
f = -f;
|
|
|
|
|
2010-02-21 21:18:05 +00:00
|
|
|
*mlt_p++ = f;
|
2009-07-21 03:40:53 +00:00
|
|
|
}
|
2009-05-10 22:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The modulated lapped transform, this takes transform coefficients
|
|
|
|
* and transforms them into timedomain samples.
|
|
|
|
* A window step is also included.
|
|
|
|
*
|
|
|
|
* @param q pointer to the COOKContext
|
|
|
|
* @param inbuffer pointer to the mltcoefficients
|
|
|
|
* @param outbuffer pointer to the timedomain buffer
|
|
|
|
* @param mlt_tmp pointer to temporary storage space
|
|
|
|
*/
|
2009-08-13 20:26:31 +00:00
|
|
|
#include "../lib/mdct_lookup.h"
|
2009-07-06 22:40:45 +00:00
|
|
|
|
2010-02-21 19:47:05 +00:00
|
|
|
void imlt_math(COOKContext *q, FIXP *in) ICODE_ATTR;
|
|
|
|
void imlt_math(COOKContext *q, FIXP *in)
|
2009-07-06 22:40:45 +00:00
|
|
|
{
|
|
|
|
const int n = q->samples_per_channel;
|
2009-07-17 23:53:40 +00:00
|
|
|
const int step = 2 << (10 - av_log2(n));
|
2010-02-21 21:18:05 +00:00
|
|
|
REAL_T *mdct_out = q->mono_mdct_output;
|
|
|
|
REAL_T tmp;
|
2009-07-17 23:53:40 +00:00
|
|
|
int i = 0, j = 0;
|
2010-02-17 00:49:53 +00:00
|
|
|
|
|
|
|
ff_imdct_calc(q->mdct_nbits, q->mono_mdct_output, in);
|
2009-05-10 22:26:02 +00:00
|
|
|
|
2009-07-06 22:40:45 +00:00
|
|
|
do {
|
2010-02-21 21:18:05 +00:00
|
|
|
tmp = mdct_out[i];
|
|
|
|
mdct_out[i ] = fixmul31(-mdct_out[n+i], (sincos_lookup0[j ]));
|
|
|
|
mdct_out[n+i] = fixmul31(tmp , (sincos_lookup0[j+1]));
|
2009-07-17 23:53:40 +00:00
|
|
|
|
2010-02-21 21:18:05 +00:00
|
|
|
j += step;
|
2009-07-06 22:40:45 +00:00
|
|
|
} while (++i < n/2);
|
2009-07-17 23:53:40 +00:00
|
|
|
|
2009-07-06 22:40:45 +00:00
|
|
|
do {
|
|
|
|
j -= step;
|
2010-02-21 21:18:05 +00:00
|
|
|
|
|
|
|
tmp = mdct_out[i];
|
|
|
|
mdct_out[i ] = fixmul31(-mdct_out[n+i], (sincos_lookup0[j+1]));
|
|
|
|
mdct_out[n+i] = fixmul31(tmp , (sincos_lookup0[j ]));
|
2009-07-06 22:40:45 +00:00
|
|
|
} while (++i < n);
|
|
|
|
}
|
2009-05-10 22:26:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform buffer overlapping.
|
|
|
|
*
|
|
|
|
* @param q pointer to the COOKContext
|
|
|
|
* @param gain gain correction to apply first to output buffer
|
|
|
|
* @param buffer data to overlap
|
|
|
|
*/
|
2010-02-21 19:47:05 +00:00
|
|
|
void overlap_math(COOKContext *q, int gain, FIXP buffer[]) ICODE_ATTR;
|
|
|
|
void overlap_math(COOKContext *q, int gain, FIXP buffer[])
|
2009-05-10 22:26:02 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-07-27 11:28:14 +00:00
|
|
|
#ifdef ROCKBOX
|
2009-07-26 19:06:36 +00:00
|
|
|
if(LIKELY(gain == 0))
|
|
|
|
{
|
|
|
|
vect_add(q->mono_mdct_output, buffer, q->samples_per_channel);
|
2009-07-18 00:24:54 +00:00
|
|
|
|
|
|
|
} else if (gain > 0){
|
|
|
|
for(i=0 ; i<q->samples_per_channel ; i++) {
|
|
|
|
q->mono_mdct_output[i] = (q->mono_mdct_output[i]<< gain) + buffer[i]; }
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for(i=0 ; i<q->samples_per_channel ; i++) {
|
2010-02-21 21:18:05 +00:00
|
|
|
q->mono_mdct_output[i] = (q->mono_mdct_output[i]>>-gain) + buffer[i];
|
2009-07-18 00:24:54 +00:00
|
|
|
}
|
2009-05-10 22:26:02 +00:00
|
|
|
}
|
2009-07-27 11:28:14 +00:00
|
|
|
#else
|
|
|
|
for(i=0 ; i<q->samples_per_channel ; i++) {
|
|
|
|
q->mono_mdct_output[i] =
|
|
|
|
fixp_pow2(q->mono_mdct_output[i], gain) + buffer[i];
|
|
|
|
}
|
|
|
|
#endif
|
2009-05-10 22:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the actual requantization of the timedomain samples
|
|
|
|
*
|
|
|
|
* @param q pointer to the COOKContext
|
|
|
|
* @param buffer pointer to the timedomain buffer
|
|
|
|
* @param gain_index index for the block multiplier
|
|
|
|
* @param gain_index_next index for the next block multiplier
|
|
|
|
*/
|
|
|
|
static inline void
|
2009-07-26 19:06:36 +00:00
|
|
|
interpolate_math(COOKContext *q, register FIXP* buffer,
|
2009-05-10 22:26:02 +00:00
|
|
|
int gain_index, int gain_index_next)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int gain_size_factor = q->samples_per_channel / 8;
|
|
|
|
|
|
|
|
if(gain_index == gain_index_next){ //static gain
|
|
|
|
for(i = 0; i < gain_size_factor; i++) {
|
|
|
|
buffer[i] = fixp_pow2(buffer[i], gain_index);
|
|
|
|
}
|
|
|
|
} else { //smooth gain
|
|
|
|
int step = (gain_index_next - gain_index)
|
|
|
|
<< (7 - av_log2(gain_size_factor));
|
|
|
|
int x = 0;
|
2009-07-26 19:06:36 +00:00
|
|
|
register FIXP* bufferend = buffer+gain_size_factor;
|
|
|
|
while(buffer < bufferend )
|
|
|
|
{
|
|
|
|
*buffer = fixp_pow2(
|
|
|
|
fixp_mult_su(*buffer, pow128_tab[x]),
|
|
|
|
gain_index+1);
|
|
|
|
buffer++;
|
2009-05-10 22:26:02 +00:00
|
|
|
|
|
|
|
x += step;
|
2009-07-26 19:06:36 +00:00
|
|
|
gain_index += ( (x + 128) >> 7 ) - 1;
|
|
|
|
x = ( (x + 128) & 127 );
|
2009-05-10 22:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decoupling calculation for joint stereo coefficients.
|
|
|
|
*
|
|
|
|
* @param x mono coefficient
|
|
|
|
* @param table number of decoupling table
|
|
|
|
* @param i table index
|
|
|
|
*/
|
|
|
|
static inline FIXP cplscale_math(FIXP x, int table, int i)
|
|
|
|
{
|
|
|
|
return fixp_mult_su(x, cplscales[table-2][i]);
|
|
|
|
}
|