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
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
57 * Initialise fixed point implementation.
58 * Nothing to do for fixed point.
60 * @param q pointer to the COOKContext
62 static inline int init_cook_math(COOKContext
*q
)
68 * Free resources used by floating point implementation.
69 * Nothing to do for fixed point.
71 * @param q pointer to the COOKContext
73 static inline void free_cook_math(COOKContext
*q
)
80 * Fixed point multiply by power of two.
82 * @param x fix point value
83 * @param i integer power-of-two, -31..+31
85 static inline FIXP
fixp_pow2(FIXP x
, int i
)
88 return (x
>> -i
) + ((x
>> (-i
-1)) & 1);
90 return x
<< i
; /* no check for overflow */
94 * Fixed point multiply by fraction.
96 * @param a fix point value
97 * @param b fix point fraction, 0 <= b < 1
99 static inline FIXP
fixp_mult_su(FIXP a
, FIXPU b
)
101 int32_t hb
= (a
>> 16) * b
;
102 uint32_t lb
= (a
& 0xffff) * b
;
104 return hb
+ (lb
>> 16) + ((lb
& 0x8000) >> 15);
107 /* math functions taken from libavutil/common.h */
109 static inline int av_log2(unsigned int v
)
112 if (v
& 0xffff0000) {
126 * Clips a signed integer value into the amin-amax range.
127 * @param a value to clip
128 * @param amin minimum value of the clip range
129 * @param amax maximum value of the clip range
130 * @return clipped value
132 static inline int av_clip(int a
, int amin
, int amax
)
134 if (a
< amin
) return amin
;
135 else if (a
> amax
) return amax
;
140 * The real requantization of the mltcoefs
142 * @param q pointer to the COOKContext
144 * @param quant_index quantisation index for this band
145 * @param subband_coef_index array of indexes to quant_centroid_tab
146 * @param subband_coef_sign use random noise instead of predetermined value
147 * @param mlt_ptr pointer to the mlt coefficients
149 static void scalar_dequant_math(COOKContext
*q
, int index
,
150 int quant_index
, int* subband_coef_index
,
151 int* subband_coef_sign
, REAL_T
*mlt_p
)
153 /* Num. half bits to right shift */
154 const int s
= 33 - quant_index
+ av_log2(q
->samples_per_channel
);
155 const FIXP
*table
= quant_tables
[s
& 1][index
];
159 for(i
=0 ; i
<SUBBAND_SIZE
; i
++) {
160 f
= table
[subband_coef_index
[i
]];
161 /* noise coding if subband_coef_index[i] == 0 */
162 if (((subband_coef_index
[i
] == 0) && cook_random(q
)) ||
163 ((subband_coef_index
[i
] != 0) && subband_coef_sign
[i
]))
166 mlt_p
[i
] = (s
>= 64) ? 0 : fixp_pow2(f
, -(s
/2));
172 * The modulated lapped transform, this takes transform coefficients
173 * and transforms them into timedomain samples.
174 * A window step is also included.
176 * @param q pointer to the COOKContext
177 * @param inbuffer pointer to the mltcoefficients
178 * @param outbuffer pointer to the timedomain buffer
179 * @param mlt_tmp pointer to temporary storage space
181 #include "cook_fixp_mdct.h"
183 static inline void imlt_math(COOKContext
*q
, FIXP
*in
)
185 const int n
= q
->samples_per_channel
;
186 const int step
= 4 << (10 - av_log2(n
));
187 int i
= 0, j
= step
>>1;
189 cook_mdct_backward(2 * n
, in
, q
->mono_mdct_output
);
192 FIXP tmp
= q
->mono_mdct_output
[i
];
194 q
->mono_mdct_output
[i
] =
195 fixp_mult_su(-q
->mono_mdct_output
[n
+ i
], sincos_lookup
[j
]);
196 q
->mono_mdct_output
[n
+ i
] = fixp_mult_su(tmp
, sincos_lookup
[j
+1]);
200 FIXP tmp
= q
->mono_mdct_output
[i
];
203 q
->mono_mdct_output
[i
] =
204 fixp_mult_su(-q
->mono_mdct_output
[n
+ i
], sincos_lookup
[j
+1]);
205 q
->mono_mdct_output
[n
+ i
] = fixp_mult_su(tmp
, sincos_lookup
[j
]);
211 * Perform buffer overlapping.
213 * @param q pointer to the COOKContext
214 * @param gain gain correction to apply first to output buffer
215 * @param buffer data to overlap
217 static inline void overlap_math(COOKContext
*q
, int gain
, FIXP buffer
[])
220 for(i
=0 ; i
<q
->samples_per_channel
; i
++) {
221 q
->mono_mdct_output
[i
] =
222 fixp_pow2(q
->mono_mdct_output
[i
], gain
) + buffer
[i
];
228 * the actual requantization of the timedomain samples
230 * @param q pointer to the COOKContext
231 * @param buffer pointer to the timedomain buffer
232 * @param gain_index index for the block multiplier
233 * @param gain_index_next index for the next block multiplier
236 interpolate_math(COOKContext
*q
, FIXP
* buffer
,
237 int gain_index
, int gain_index_next
)
240 int gain_size_factor
= q
->samples_per_channel
/ 8;
242 if(gain_index
== gain_index_next
){ //static gain
243 for(i
= 0; i
< gain_size_factor
; i
++) {
244 buffer
[i
] = fixp_pow2(buffer
[i
], gain_index
);
246 } else { //smooth gain
247 int step
= (gain_index_next
- gain_index
)
248 << (7 - av_log2(gain_size_factor
));
251 for(i
= 0; i
< gain_size_factor
; i
++) {
252 buffer
[i
] = fixp_mult_su(buffer
[i
], pow128_tab
[x
]);
253 buffer
[i
] = fixp_pow2(buffer
[i
], gain_index
+1);
256 gain_index
+= (x
+ 128) / 128 - 1;
264 * Decoupling calculation for joint stereo coefficients.
266 * @param x mono coefficient
267 * @param table number of decoupling table
268 * @param i table index
270 static inline FIXP
cplscale_math(FIXP x
, int table
, int i
)
272 return fixp_mult_su(x
, cplscales
[table
-2][i
]);
277 * Final converion from floating point values to
278 * signed, 16 bit sound samples. Round and clip.
280 * @param q pointer to the COOKContext
281 * @param out pointer to the output buffer
282 * @param chan 0: left or single channel, 1: right channel
284 static inline void output_math(COOKContext
*q
, int16_t *out
, int chan
)
288 for (j
= 0; j
< q
->samples_per_channel
; j
++) {
289 out
[chan
+ q
->nb_channels
* j
] =
290 av_clip(fixp_pow2(q
->mono_mdct_output
[j
], -11), -32768, 32767);