Remove a call to av_clip() which limits the PCM output of the decoder to 16-bit.
[kugel-rb.git] / apps / codecs / libcook / cook_fixpoint.h
blob2e7f68913bd5d4ff5c7a0e26a9e6596f2cf59105
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
76 static inline FIXP fixp_mult_su(FIXP a, FIXPU b)
78 int32_t hb = (a >> 16) * b;
79 uint32_t lb = (a & 0xffff) * b;
81 return hb + (lb >> 16) + ((lb & 0x8000) >> 15);
84 /* math functions taken from libavutil/common.h */
86 static inline int av_log2(unsigned int v)
88 int n = 0;
89 if (v & 0xffff0000) {
90 v >>= 16;
91 n += 16;
93 if (v & 0xff00) {
94 v >>= 8;
95 n += 8;
97 n += ff_log2_tab[v];
99 return n;
103 * Clips a signed integer value into the amin-amax range.
104 * @param a value to clip
105 * @param amin minimum value of the clip range
106 * @param amax maximum value of the clip range
107 * @return clipped value
109 static inline int av_clip(int a, int amin, int amax)
111 if (a < amin) return amin;
112 else if (a > amax) return amax;
113 else return a;
117 * The real requantization of the mltcoefs
119 * @param q pointer to the COOKContext
120 * @param index index
121 * @param quant_index quantisation index for this band
122 * @param subband_coef_index array of indexes to quant_centroid_tab
123 * @param subband_coef_sign use random noise instead of predetermined value
124 * @param mlt_ptr pointer to the mlt coefficients
126 static void scalar_dequant_math(COOKContext *q, int index,
127 int quant_index, int* subband_coef_index,
128 int* subband_coef_sign, REAL_T *mlt_p)
130 /* Num. half bits to right shift */
131 const int s = 33 - quant_index + av_log2(q->samples_per_channel);
132 const FIXP *table = quant_tables[s & 1][index];
133 FIXP f;
134 int i;
136 for(i=0 ; i<SUBBAND_SIZE ; i++) {
137 f = table[subband_coef_index[i]];
138 /* noise coding if subband_coef_index[i] == 0 */
139 if (((subband_coef_index[i] == 0) && cook_random(q)) ||
140 ((subband_coef_index[i] != 0) && subband_coef_sign[i]))
141 f = -f;
143 mlt_p[i] = (s >= 64) ? 0 : fixp_pow2(f, -(s/2));
147 #ifdef TEST
149 * The modulated lapped transform, this takes transform coefficients
150 * and transforms them into timedomain samples.
151 * A window step is also included.
153 * @param q pointer to the COOKContext
154 * @param inbuffer pointer to the mltcoefficients
155 * @param outbuffer pointer to the timedomain buffer
156 * @param mlt_tmp pointer to temporary storage space
158 #include "cook_fixp_mdct.h"
160 static inline void imlt_math(COOKContext *q, FIXP *in)
162 const int n = q->samples_per_channel;
163 const int step = 4 << (10 - av_log2(n));
164 int i = 0, j = step>>1;
166 cook_mdct_backward(2 * n, in, q->mono_mdct_output);
168 do {
169 FIXP tmp = q->mono_mdct_output[i];
171 q->mono_mdct_output[i] =
172 fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j]);
173 q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j+1]);
174 j += step;
175 } while (++i < n/2);
176 do {
177 FIXP tmp = q->mono_mdct_output[i];
179 j -= step;
180 q->mono_mdct_output[i] =
181 fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j+1]);
182 q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j]);
183 } while (++i < n);
185 #else
186 #include <codecs/lib/codeclib.h>
188 static inline void imlt_math(COOKContext *q, FIXP *in)
190 const int n = q->samples_per_channel;
191 const int step = 4 << (10 - av_log2(n));
192 int i = 0, j = step>>1;
194 mdct_backward(2 * n, in, q->mono_mdct_output);
196 do {
197 FIXP tmp = q->mono_mdct_output[i];
199 q->mono_mdct_output[i] =
200 fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j]);
201 q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j+1]);
202 j += step;
203 } while (++i < n/2);
204 do {
205 FIXP tmp = q->mono_mdct_output[i];
207 j -= step;
208 q->mono_mdct_output[i] =
209 fixp_mult_su(-q->mono_mdct_output[n + i], sincos_lookup[j+1]);
210 q->mono_mdct_output[n + i] = fixp_mult_su(tmp, sincos_lookup[j]);
211 } while (++i < n);
213 #endif
216 * Perform buffer overlapping.
218 * @param q pointer to the COOKContext
219 * @param gain gain correction to apply first to output buffer
220 * @param buffer data to overlap
222 static inline void overlap_math(COOKContext *q, int gain, FIXP buffer[])
224 int i;
225 for(i=0 ; i<q->samples_per_channel ; i++) {
226 q->mono_mdct_output[i] =
227 fixp_pow2(q->mono_mdct_output[i], gain) + buffer[i];
233 * the actual requantization of the timedomain samples
235 * @param q pointer to the COOKContext
236 * @param buffer pointer to the timedomain buffer
237 * @param gain_index index for the block multiplier
238 * @param gain_index_next index for the next block multiplier
240 static inline void
241 interpolate_math(COOKContext *q, FIXP* buffer,
242 int gain_index, int gain_index_next)
244 int i;
245 int gain_size_factor = q->samples_per_channel / 8;
247 if(gain_index == gain_index_next){ //static gain
248 for(i = 0; i < gain_size_factor; i++) {
249 buffer[i] = fixp_pow2(buffer[i], gain_index);
251 } else { //smooth gain
252 int step = (gain_index_next - gain_index)
253 << (7 - av_log2(gain_size_factor));
254 int x = 0;
256 for(i = 0; i < gain_size_factor; i++) {
257 buffer[i] = fixp_mult_su(buffer[i], pow128_tab[x]);
258 buffer[i] = fixp_pow2(buffer[i], gain_index+1);
260 x += step;
261 gain_index += (x + 128) / 128 - 1;
262 x = (x + 128) % 128;
269 * Decoupling calculation for joint stereo coefficients.
271 * @param x mono coefficient
272 * @param table number of decoupling table
273 * @param i table index
275 static inline FIXP cplscale_math(FIXP x, int table, int i)
277 return fixp_mult_su(x, cplscales[table-2][i]);
282 * Final converion from floating point values to
283 * signed, 16 bit sound samples. Round and clip.
285 * @param q pointer to the COOKContext
286 * @param out pointer to the output buffer
287 * @param chan 0: left or single channel, 1: right channel
289 static inline void output_math(COOKContext *q, int16_t *out, int chan)
291 int j;
293 for (j = 0; j < q->samples_per_channel; j++) {
294 out[chan + q->nb_channels * j] = fixp_pow2(q->mono_mdct_output[j], -11);