Rearrange the MDCT library lookup tables so that codecs can access them. Access...
[kugel-rb.git] / apps / codecs / libcook / cook_fixpoint.h
blobc2ab9299c668a6313d7ea2164ee0024f60e173b6
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_float.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 /* The following table is taken from libavutil/mathematics.c */
39 const uint8_t ff_log2_tab[256]={
40 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
41 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
42 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
43 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
44 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
45 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
46 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
47 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
50 /* cplscales was moved from cookdata_fixpoint.h since only *
51 * cook_fixpoint.h should see/use it. */
52 static const FIXPU* cplscales[5] = {
53 cplscale2, cplscale3, cplscale4, cplscale5, cplscale6
56 /**
57 * Fixed point multiply by power of two.
59 * @param x fix point value
60 * @param i integer power-of-two, -31..+31
62 static inline FIXP fixp_pow2(FIXP x, int i)
64 if (i < 0)
65 return (x >> -i) + ((x >> (-i-1)) & 1);
66 else
67 return x << i; /* no check for overflow */
70 /**
71 * Fixed point multiply by fraction.
73 * @param a fix point value
74 * @param b fix point fraction, 0 <= b < 1
77 static inline FIXP fixp_mult_su(FIXP a, FIXPU b)
80 int32_t hb = (a >> 16) * b;
81 uint32_t lb = (a & 0xffff) * b;
83 return hb + (lb >> 16) + ((lb & 0x8000) >> 15);
86 /* Faster version of the above using 32x32=64 bit multiply */
87 #ifdef CPU_ARM
88 #define fixmul31(x, y) \
89 ({ int32_t __hi; \
90 uint32_t __lo; \
91 int32_t __result; \
92 asm ("smull %0, %1, %3, %4\n\t" \
93 "movs %2, %1, lsl #1" \
94 : "=&r" (__lo), "=&r" (__hi), "=r" (__result) \
95 : "%r" (x), "r" (y) \
96 : "cc"); \
97 __result; \
100 #elif defined(CPU_COLDFIRE)
101 static inline int32_t fixmul31(int32_t x, int32_t y)
103 asm (
104 "mac.l %[x], %[y], %%acc0 \n" /* multiply */
105 "movclr.l %%acc0, %[x] \n" /* get higher half */
106 : [x] "+d" (x)
107 : [y] "d" (y)
109 return x;
111 #else
112 static inline int32_t fixmul31(int32_t x, int32_t y)
114 int64_t temp;
116 temp = x;
117 temp *= y;
119 temp >>= 31; //16+31-16 = 31 bits
121 return (int32_t)temp;
123 #endif
125 /* math functions taken from libavutil/common.h */
127 static inline int av_log2(unsigned int v)
129 int n = 0;
130 if (v & 0xffff0000) {
131 v >>= 16;
132 n += 16;
134 if (v & 0xff00) {
135 v >>= 8;
136 n += 8;
138 n += ff_log2_tab[v];
140 return n;
144 * Clips a signed integer value into the amin-amax range.
145 * @param a value to clip
146 * @param amin minimum value of the clip range
147 * @param amax maximum value of the clip range
148 * @return clipped value
150 static inline int av_clip(int a, int amin, int amax)
152 if (a < amin) return amin;
153 else if (a > amax) return amax;
154 else return a;
158 * The real requantization of the mltcoefs
160 * @param q pointer to the COOKContext
161 * @param index index
162 * @param quant_index quantisation index for this band
163 * @param subband_coef_index array of indexes to quant_centroid_tab
164 * @param subband_coef_sign use random noise instead of predetermined value
165 * @param mlt_ptr pointer to the mlt coefficients
167 static void scalar_dequant_math(COOKContext *q, int index,
168 int quant_index, int* subband_coef_index,
169 int* subband_coef_sign, REAL_T *mlt_p)
171 /* Num. half bits to right shift */
172 const int s = 33 - quant_index + av_log2(q->samples_per_channel);
173 const FIXP *table = quant_tables[s & 1][index];
174 FIXP f;
175 int i;
177 for(i=0 ; i<SUBBAND_SIZE ; i++) {
178 f = table[subband_coef_index[i]];
179 /* noise coding if subband_coef_index[i] == 0 */
180 if (((subband_coef_index[i] == 0) && cook_random(q)) ||
181 ((subband_coef_index[i] != 0) && subband_coef_sign[i]))
182 f = -f;
184 mlt_p[i] = (s >= 64) ? 0 : fixp_pow2(f, -(s/2));
188 #ifdef TEST
190 * The modulated lapped transform, this takes transform coefficients
191 * and transforms them into timedomain samples.
192 * A window step is also included.
194 * @param q pointer to the COOKContext
195 * @param inbuffer pointer to the mltcoefficients
196 * @param outbuffer pointer to the timedomain buffer
197 * @param mlt_tmp pointer to temporary storage space
199 #include "cook_fixp_mdct.h"
201 static inline void imlt_math(COOKContext *q, FIXP *in)
203 const int n = q->samples_per_channel;
204 const int step = 4 << (10 - av_log2(n));
205 int i = 0, j = step>>1;
207 cook_mdct_backward(2 * n, in, q->mono_mdct_output);
209 do {
210 FIXP tmp = q->mono_mdct_output[i];
212 q->mono_mdct_output[i] =
213 fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j]);
214 q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j+1]);
215 j += step;
216 } while (++i < n/2);
217 do {
218 FIXP tmp = q->mono_mdct_output[i];
220 j -= step;
221 q->mono_mdct_output[i] =
222 fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j+1]);
223 q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j]);
224 } while (++i < n);
226 #else
227 #include <codecs/lib/codeclib.h>
228 #include <codecs/lib/mdct_lookup.h>
230 static inline void imlt_math(COOKContext *q, FIXP *in)
232 const int n = q->samples_per_channel;
233 const int step = 2 << (10 - av_log2(n));
234 int i = 0, j = 0;
236 mdct_backward(2 * n, in, q->mono_mdct_output);
238 do {
239 FIXP tmp = q->mono_mdct_output[i];
241 q->mono_mdct_output[i] =
242 fixmul31(-q->mono_mdct_output[n + i], (sincos_lookup0[j]));
244 q->mono_mdct_output[n + i] = fixmul31(tmp, (sincos_lookup0[j+1]) );
246 j += step;
248 } while (++i < n/2);
250 do {
251 FIXP tmp = q->mono_mdct_output[i];
253 j -= step;
254 q->mono_mdct_output[i] =
255 fixmul31(-q->mono_mdct_output[n + i], (sincos_lookup0[j+1]) );
256 q->mono_mdct_output[n + i] = fixmul31(tmp, (sincos_lookup0[j]) );
257 } while (++i < n);
259 #endif
262 * Perform buffer overlapping.
264 * @param q pointer to the COOKContext
265 * @param gain gain correction to apply first to output buffer
266 * @param buffer data to overlap
268 static inline void overlap_math(COOKContext *q, int gain, FIXP buffer[])
270 int i;
271 for(i=0 ; i<q->samples_per_channel ; i++) {
272 q->mono_mdct_output[i] =
273 fixp_pow2(q->mono_mdct_output[i], gain) + buffer[i];
279 * the actual requantization of the timedomain samples
281 * @param q pointer to the COOKContext
282 * @param buffer pointer to the timedomain buffer
283 * @param gain_index index for the block multiplier
284 * @param gain_index_next index for the next block multiplier
286 static inline void
287 interpolate_math(COOKContext *q, FIXP* buffer,
288 int gain_index, int gain_index_next)
290 int i;
291 int gain_size_factor = q->samples_per_channel / 8;
293 if(gain_index == gain_index_next){ //static gain
294 for(i = 0; i < gain_size_factor; i++) {
295 buffer[i] = fixp_pow2(buffer[i], gain_index);
297 } else { //smooth gain
298 int step = (gain_index_next - gain_index)
299 << (7 - av_log2(gain_size_factor));
300 int x = 0;
302 for(i = 0; i < gain_size_factor; i++) {
303 buffer[i] = fixp_mult_su(buffer[i], pow128_tab[x]);
304 buffer[i] = fixp_pow2(buffer[i], gain_index+1);
306 x += step;
307 gain_index += (x + 128) / 128 - 1;
308 x = (x + 128) % 128;
315 * Decoupling calculation for joint stereo coefficients.
317 * @param x mono coefficient
318 * @param table number of decoupling table
319 * @param i table index
321 static inline FIXP cplscale_math(FIXP x, int table, int i)
323 return fixp_mult_su(x, cplscales[table-2][i]);
328 * Final converion from floating point values to
329 * signed, 16 bit sound samples. Round and clip.
331 * @param q pointer to the COOKContext
332 * @param out pointer to the output buffer
333 * @param chan 0: left or single channel, 1: right channel
335 static inline void output_math(COOKContext *q, int16_t *out, int chan)
337 int j;
339 for (j = 0; j < q->samples_per_channel; j++) {
340 out[chan + q->nb_channels * j] =
341 av_clip(fixp_pow2(q->mono_mdct_output[j], -11), -32768, 32767);