Remove CLIP_TO_15 from codeclib. Remove tabs.
[kugel-rb.git] / apps / codecs / libcook / cook_fixpoint.h
blobfaa86dc77ac9a5a35426f8ca46eb2e112368331c
1 /*
2 * COOK compatible decoder, fixed point implementation.
3 * Copyright (c) 2007 Ian Braithwaite
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 /**
24 * @file cook_fixpoint.h
26 * Cook AKA RealAudio G2 fixed point functions.
28 * Fixed point values are represented as 32 bit signed integers,
29 * which can be added and subtracted directly in C (without checks for
30 * overflow/saturation.
31 * Two multiplication routines are provided:
32 * 1) Multiplication by powers of two (2^-31 .. 2^31), implemented
33 * with C's bit shift operations.
34 * 2) Multiplication by 16 bit fractions (0 <= x < 1), implemented
35 * in C using two 32 bit integer multiplications.
38 #ifdef ROCKBOX
39 /* get definitions of MULT31, MULT31_SHIFT15, vect_add, from codelib */
40 #include "asm_arm.h"
41 #include "asm_mcf5249.h"
42 #include "codeclib_misc.h"
43 #include "codeclib.h"
44 #endif
46 /* cplscales was moved from cookdata_fixpoint.h since only *
47 * cook_fixpoint.h should see/use it. */
48 static const FIXPU* cplscales[5] = {
49 cplscale2, cplscale3, cplscale4, cplscale5, cplscale6
52 /**
53 * Fixed point multiply by power of two.
55 * @param x fix point value
56 * @param i integer power-of-two, -31..+31
58 static inline FIXP fixp_pow2(FIXP x, int i)
60 if (i < 0)
61 return (x >> -i);
62 else
63 return x << i; /* no check for overflow */
66 /**
67 * Fixed point multiply by fraction.
69 * @param a fix point value
70 * @param b fix point fraction, 0 <= b < 1
72 #ifdef ROCKBOX
73 #define fixp_mult_su(x,y) (MULT31_SHIFT15(x,y))
74 #else
75 static inline FIXP fixp_mult_su(FIXP a, FIXPU b)
77 int32_t hb = (a >> 16) * b;
78 uint32_t lb = (a & 0xffff) * b;
80 return hb + (lb >> 16) + ((lb & 0x8000) >> 15);
82 #endif
84 /* Faster version of the above using 32x32=64 bit multiply */
85 #ifdef ROCKBOX
86 #define fixmul31(x,y) (MULT31(x,y))
87 #else
88 static inline int32_t fixmul31(int32_t x, int32_t y)
90 int64_t temp;
92 temp = x;
93 temp *= y;
95 temp >>= 31; //16+31-16 = 31 bits
97 return (int32_t)temp;
99 #endif
102 * Clips a signed integer value into the amin-amax range.
103 * @param a value to clip
104 * @param amin minimum value of the clip range
105 * @param amax maximum value of the clip range
106 * @return clipped value
108 static inline int av_clip(int a, int amin, int amax)
110 if (a < amin) return amin;
111 else if (a > amax) return amax;
112 else return a;
116 * The real requantization of the mltcoefs
118 * @param q pointer to the COOKContext
119 * @param index index
120 * @param quant_index quantisation index for this band
121 * @param subband_coef_index array of indexes to quant_centroid_tab
122 * @param subband_coef_sign use random noise instead of predetermined value
123 * @param mlt_ptr pointer to the mlt coefficients
125 static void scalar_dequant_math(COOKContext *q, int index,
126 int quant_index, int* subband_coef_index,
127 int* subband_coef_sign, REAL_T *mlt_p)
129 /* Num. half bits to right shift */
130 const int s = (33 - quant_index + av_log2(q->samples_per_channel)) >> 1;
131 const FIXP *table = quant_tables[s & 1][index];
132 FIXP f;
133 int i;
136 if(s >= 32)
137 memset(mlt_p, 0, sizeof(REAL_T)*SUBBAND_SIZE);
138 else
140 for(i=0 ; i<SUBBAND_SIZE ; i++) {
141 f = (table[subband_coef_index[i]])>>s;
142 /* noise coding if subband_coef_index[i] == 0 */
143 if (((subband_coef_index[i] == 0) && cook_random(q)) ||
144 ((subband_coef_index[i] != 0) && subband_coef_sign[i]))
145 f = -f;
147 *mlt_p++ = f;
153 * The modulated lapped transform, this takes transform coefficients
154 * and transforms them into timedomain samples.
155 * A window step is also included.
157 * @param q pointer to the COOKContext
158 * @param inbuffer pointer to the mltcoefficients
159 * @param outbuffer pointer to the timedomain buffer
160 * @param mlt_tmp pointer to temporary storage space
162 #include "../lib/mdct_lookup.h"
164 void imlt_math(COOKContext *q, FIXP *in) ICODE_ATTR;
165 void imlt_math(COOKContext *q, FIXP *in)
167 const int n = q->samples_per_channel;
168 const int step = 2 << (10 - av_log2(n));
169 REAL_T *mdct_out = q->mono_mdct_output;
170 REAL_T tmp;
171 int i = 0, j = 0;
173 ff_imdct_calc(q->mdct_nbits, q->mono_mdct_output, in);
175 do {
176 tmp = mdct_out[i];
177 mdct_out[i ] = fixmul31(-mdct_out[n+i], (sincos_lookup0[j ]));
178 mdct_out[n+i] = fixmul31(tmp , (sincos_lookup0[j+1]));
180 j += step;
181 } while (++i < n/2);
183 do {
184 j -= step;
186 tmp = mdct_out[i];
187 mdct_out[i ] = fixmul31(-mdct_out[n+i], (sincos_lookup0[j+1]));
188 mdct_out[n+i] = fixmul31(tmp , (sincos_lookup0[j ]));
189 } while (++i < n);
193 * Perform buffer overlapping.
195 * @param q pointer to the COOKContext
196 * @param gain gain correction to apply first to output buffer
197 * @param buffer data to overlap
199 void overlap_math(COOKContext *q, int gain, FIXP buffer[]) ICODE_ATTR;
200 void overlap_math(COOKContext *q, int gain, FIXP buffer[])
202 int i;
203 #ifdef ROCKBOX
204 if(LIKELY(gain == 0))
206 vect_add(q->mono_mdct_output, buffer, q->samples_per_channel);
208 } else if (gain > 0){
209 for(i=0 ; i<q->samples_per_channel ; i++) {
210 q->mono_mdct_output[i] = (q->mono_mdct_output[i]<< gain) + buffer[i]; }
212 } else {
213 for(i=0 ; i<q->samples_per_channel ; i++) {
214 q->mono_mdct_output[i] = (q->mono_mdct_output[i]>>-gain) + buffer[i];
217 #else
218 for(i=0 ; i<q->samples_per_channel ; i++) {
219 q->mono_mdct_output[i] =
220 fixp_pow2(q->mono_mdct_output[i], gain) + buffer[i];
222 #endif
227 * the actual requantization of the timedomain samples
229 * @param q pointer to the COOKContext
230 * @param buffer pointer to the timedomain buffer
231 * @param gain_index index for the block multiplier
232 * @param gain_index_next index for the next block multiplier
234 static inline void
235 interpolate_math(COOKContext *q, register FIXP* buffer,
236 int gain_index, int gain_index_next)
238 int i;
239 int gain_size_factor = q->samples_per_channel / 8;
241 if(gain_index == gain_index_next){ //static gain
242 for(i = 0; i < gain_size_factor; i++) {
243 buffer[i] = fixp_pow2(buffer[i], gain_index);
245 } else { //smooth gain
246 int step = (gain_index_next - gain_index)
247 << (7 - av_log2(gain_size_factor));
248 int x = 0;
249 register FIXP* bufferend = buffer+gain_size_factor;
250 while(buffer < bufferend )
252 *buffer = fixp_pow2(
253 fixp_mult_su(*buffer, pow128_tab[x]),
254 gain_index+1);
255 buffer++;
257 x += step;
258 gain_index += ( (x + 128) >> 7 ) - 1;
259 x = ( (x + 128) & 127 );
266 * Decoupling calculation for joint stereo coefficients.
268 * @param x mono coefficient
269 * @param table number of decoupling table
270 * @param i table index
272 static inline FIXP cplscale_math(FIXP x, int table, int i)
274 return fixp_mult_su(x, cplscales[table-2][i]);