22 pixel high Nimbus-like font, containing only digits and the period; intended for...
[kugel-rb.git] / apps / codecs / libfaad / common.c
blob025c8f8c5be02cd4dfa41db01d3f18829ef82fd2
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
28 /* just some common functions that could be used anywhere */
30 #include "common.h"
31 #include "structs.h"
33 #include <stdlib.h>
34 #include "syntax.h"
37 /* Returns the sample rate index based on the samplerate */
38 uint8_t get_sr_index(const uint32_t samplerate)
40 if (92017 <= samplerate) return 0;
41 if (75132 <= samplerate) return 1;
42 if (55426 <= samplerate) return 2;
43 if (46009 <= samplerate) return 3;
44 if (37566 <= samplerate) return 4;
45 if (27713 <= samplerate) return 5;
46 if (23004 <= samplerate) return 6;
47 if (18783 <= samplerate) return 7;
48 if (13856 <= samplerate) return 8;
49 if (11502 <= samplerate) return 9;
50 if (9391 <= samplerate) return 10;
51 if (16428320 <= samplerate) return 11;
53 return 11;
56 /* Returns the sample rate based on the sample rate index */
57 uint32_t get_sample_rate(const uint8_t sr_index)
59 static const uint32_t sample_rates[] =
61 96000, 88200, 64000, 48000, 44100, 32000,
62 24000, 22050, 16000, 12000, 11025, 8000
65 if (sr_index < 12)
66 return sample_rates[sr_index];
68 return 0;
71 uint8_t max_pred_sfb(const uint8_t sr_index)
73 static const uint8_t pred_sfb_max[] =
75 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34
79 if (sr_index < 12)
80 return pred_sfb_max[sr_index];
82 return 0;
85 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
86 const uint8_t is_short)
88 /* entry for each sampling rate
89 * 1 Main/LC long window
90 * 2 Main/LC short window
91 * 3 SSR long window
92 * 4 SSR short window
94 static const uint8_t tns_sbf_max[][4] =
96 {31, 9, 28, 7}, /* 96000 */
97 {31, 9, 28, 7}, /* 88200 */
98 {34, 10, 27, 7}, /* 64000 */
99 {40, 14, 26, 6}, /* 48000 */
100 {42, 14, 26, 6}, /* 44100 */
101 {51, 14, 26, 6}, /* 32000 */
102 {46, 14, 29, 7}, /* 24000 */
103 {46, 14, 29, 7}, /* 22050 */
104 {42, 14, 23, 8}, /* 16000 */
105 {42, 14, 23, 8}, /* 12000 */
106 {42, 14, 23, 8}, /* 11025 */
107 {39, 14, 19, 7}, /* 8000 */
108 {39, 14, 19, 7}, /* 7350 */
109 {0,0,0,0},
110 {0,0,0,0},
111 {0,0,0,0}
113 uint8_t i = 0;
115 if (is_short) i++;
116 if (object_type == SSR) i += 2;
118 return tns_sbf_max[sr_index][i];
121 /* Returns 0 if an object type is decodable, otherwise returns -1 */
122 int8_t can_decode_ot(const uint8_t object_type)
124 switch (object_type)
126 case LC:
127 return 0;
128 case MAIN:
129 #ifdef MAIN_DEC
130 return 0;
131 #else
132 return -1;
133 #endif
134 case SSR:
135 #ifdef SSR_DEC
136 return 0;
137 #else
138 return -1;
139 #endif
140 case LTP:
141 #ifdef LTP_DEC
142 return 0;
143 #else
144 return -1;
145 #endif
147 /* ER object types */
148 #ifdef ERROR_RESILIENCE
149 case ER_LC:
150 #ifdef DRM
151 case DRM_ER_LC:
152 #endif
153 return 0;
154 case ER_LTP:
155 #ifdef LTP_DEC
156 return 0;
157 #else
158 return -1;
159 #endif
160 case LD:
161 #ifdef LD_DEC
162 return 0;
163 #else
164 return -1;
165 #endif
166 #endif
169 return -1;
172 void *faad_malloc(size_t size)
174 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
175 return _aligned_malloc(size, 16);
176 #else // #ifdef 0
177 return malloc(size);
178 #endif // #ifdef 0
181 /* common free function */
182 void faad_free(void *b)
184 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
185 _aligned_free(b);
186 #else
187 free(b);
189 #endif
191 static const uint8_t Parity [256] = { // parity
192 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
193 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
194 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
195 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
196 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
197 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
198 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
199 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
202 static uint32_t __r1 = 1;
203 static uint32_t __r2 = 1;
207 * This is a simple random number generator with good quality for audio purposes.
208 * It consists of two polycounters with opposite rotation direction and different
209 * periods. The periods are coprime, so the total period is the product of both.
211 * -------------------------------------------------------------------------------------------------
212 * +-> |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0|
213 * | -------------------------------------------------------------------------------------------------
214 * | | | | | | |
215 * | +--+--+--+-XOR-+--------+
216 * | |
217 * +--------------------------------------------------------------------------------------+
219 * -------------------------------------------------------------------------------------------------
220 * |31:30:29:28:27:26:25:24:23:22:21:20:19:18:17:16:15:14:13:12:11:10: 9: 8: 7: 6: 5: 4: 3: 2: 1: 0| <-+
221 * ------------------------------------------------------------------------------------------------- |
222 * | | | | |
223 * +--+----XOR----+--+ |
224 * | |
225 * +----------------------------------------------------------------------------------------+
228 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
229 * which gives a period of 18.410.713.077.675.721.215. The result is the
230 * XORed values of both generators.
232 uint32_t random_int(void)
234 uint32_t t1, t2, t3, t4;
236 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
237 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
238 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
239 t1 <<= 31; t2 = Parity [t2];
241 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
244 #define floor_log2(x) bs_generic(x, BS_LOG2)
246 #ifdef FIXED_POINT
248 #define TABLE_BITS 6
249 /* just take the maximum number of bits for interpolation */
250 #define INTERP_BITS (REAL_BITS-TABLE_BITS)
251 /* precision of values in pow2_tab */
252 #define POWTBL_BITS (31-(INTERP_BITS))
253 #define POWTBL_PRECIS (1U<<(POWTBL_BITS))
254 static const uint32_t pow2_tab[] ICONST_ATTR = {
255 UFIX_CONST(1.000000000000000,POWTBL_PRECIS),
256 UFIX_CONST(1.010889286051701,POWTBL_PRECIS),
257 UFIX_CONST(1.021897148654117,POWTBL_PRECIS),
258 UFIX_CONST(1.033024879021228,POWTBL_PRECIS),
259 UFIX_CONST(1.044273782427414,POWTBL_PRECIS),
260 UFIX_CONST(1.055645178360557,POWTBL_PRECIS),
261 UFIX_CONST(1.067140400676824,POWTBL_PRECIS),
262 UFIX_CONST(1.078760797757120,POWTBL_PRECIS),
263 UFIX_CONST(1.090507732665258,POWTBL_PRECIS),
264 UFIX_CONST(1.102382583307841,POWTBL_PRECIS),
265 UFIX_CONST(1.114386742595892,POWTBL_PRECIS),
266 UFIX_CONST(1.126521618608242,POWTBL_PRECIS),
267 UFIX_CONST(1.138788634756692,POWTBL_PRECIS),
268 UFIX_CONST(1.151189229952983,POWTBL_PRECIS),
269 UFIX_CONST(1.163724858777578,POWTBL_PRECIS),
270 UFIX_CONST(1.176396991650281,POWTBL_PRECIS),
271 UFIX_CONST(1.189207115002721,POWTBL_PRECIS),
272 UFIX_CONST(1.202156731452703,POWTBL_PRECIS),
273 UFIX_CONST(1.215247359980469,POWTBL_PRECIS),
274 UFIX_CONST(1.228480536106870,POWTBL_PRECIS),
275 UFIX_CONST(1.241857812073484,POWTBL_PRECIS),
276 UFIX_CONST(1.255380757024691,POWTBL_PRECIS),
277 UFIX_CONST(1.269050957191733,POWTBL_PRECIS),
278 UFIX_CONST(1.282870016078778,POWTBL_PRECIS),
279 UFIX_CONST(1.296839554651010,POWTBL_PRECIS),
280 UFIX_CONST(1.310961211524764,POWTBL_PRECIS),
281 UFIX_CONST(1.325236643159741,POWTBL_PRECIS),
282 UFIX_CONST(1.339667524053303,POWTBL_PRECIS),
283 UFIX_CONST(1.354255546936893,POWTBL_PRECIS),
284 UFIX_CONST(1.369002422974591,POWTBL_PRECIS),
285 UFIX_CONST(1.383909881963832,POWTBL_PRECIS),
286 UFIX_CONST(1.398979672538311,POWTBL_PRECIS),
287 UFIX_CONST(1.414213562373095,POWTBL_PRECIS),
288 UFIX_CONST(1.429613338391970,POWTBL_PRECIS),
289 UFIX_CONST(1.445180806977047,POWTBL_PRECIS),
290 UFIX_CONST(1.460917794180647,POWTBL_PRECIS),
291 UFIX_CONST(1.476826145939499,POWTBL_PRECIS),
292 UFIX_CONST(1.492907728291265,POWTBL_PRECIS),
293 UFIX_CONST(1.509164427593423,POWTBL_PRECIS),
294 UFIX_CONST(1.525598150744538,POWTBL_PRECIS),
295 UFIX_CONST(1.542210825407941,POWTBL_PRECIS),
296 UFIX_CONST(1.559004400237837,POWTBL_PRECIS),
297 UFIX_CONST(1.575980845107887,POWTBL_PRECIS),
298 UFIX_CONST(1.593142151342267,POWTBL_PRECIS),
299 UFIX_CONST(1.610490331949254,POWTBL_PRECIS),
300 UFIX_CONST(1.628027421857348,POWTBL_PRECIS),
301 UFIX_CONST(1.645755478153965,POWTBL_PRECIS),
302 UFIX_CONST(1.663676580326736,POWTBL_PRECIS),
303 UFIX_CONST(1.681792830507429,POWTBL_PRECIS),
304 UFIX_CONST(1.700106353718524,POWTBL_PRECIS),
305 UFIX_CONST(1.718619298122478,POWTBL_PRECIS),
306 UFIX_CONST(1.737333835273706,POWTBL_PRECIS),
307 UFIX_CONST(1.756252160373300,POWTBL_PRECIS),
308 UFIX_CONST(1.775376492526521,POWTBL_PRECIS),
309 UFIX_CONST(1.794709075003107,POWTBL_PRECIS),
310 UFIX_CONST(1.814252175500399,POWTBL_PRECIS),
311 UFIX_CONST(1.834008086409342,POWTBL_PRECIS),
312 UFIX_CONST(1.853979125083386,POWTBL_PRECIS),
313 UFIX_CONST(1.874167634110300,POWTBL_PRECIS),
314 UFIX_CONST(1.894575981586966,POWTBL_PRECIS),
315 UFIX_CONST(1.915206561397147,POWTBL_PRECIS),
316 UFIX_CONST(1.936061793492294,POWTBL_PRECIS),
317 UFIX_CONST(1.957144124175400,POWTBL_PRECIS),
318 UFIX_CONST(1.978456026387951,POWTBL_PRECIS),
319 UFIX_CONST(2.000000000000000,POWTBL_PRECIS)
322 static const real_t log2_tab[] ICONST_ATTR = {
323 REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453),
324 REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169),
325 REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312),
326 REAL_CONST(0.189824558880017), REAL_CONST(0.209453365628950), REAL_CONST(0.228818690495881),
327 REAL_CONST(0.247927513443585), REAL_CONST(0.266786540694901), REAL_CONST(0.285402218862248),
328 REAL_CONST(0.303780748177103), REAL_CONST(0.321928094887362), REAL_CONST(0.339850002884625),
329 REAL_CONST(0.357552004618084), REAL_CONST(0.375039431346925), REAL_CONST(0.392317422778760),
330 REAL_CONST(0.409390936137702), REAL_CONST(0.426264754702098), REAL_CONST(0.442943495848728),
331 REAL_CONST(0.459431618637297), REAL_CONST(0.475733430966398), REAL_CONST(0.491853096329675),
332 REAL_CONST(0.507794640198696), REAL_CONST(0.523561956057013), REAL_CONST(0.539158811108031),
333 REAL_CONST(0.554588851677637), REAL_CONST(0.569855608330948), REAL_CONST(0.584962500721156),
334 REAL_CONST(0.599912842187128), REAL_CONST(0.614709844115208), REAL_CONST(0.629356620079610),
335 REAL_CONST(0.643856189774725), REAL_CONST(0.658211482751795), REAL_CONST(0.672425341971496),
336 REAL_CONST(0.686500527183218), REAL_CONST(0.700439718141092), REAL_CONST(0.714245517666123),
337 REAL_CONST(0.727920454563199), REAL_CONST(0.741466986401147), REAL_CONST(0.754887502163469),
338 REAL_CONST(0.768184324776926), REAL_CONST(0.781359713524660), REAL_CONST(0.794415866350106),
339 REAL_CONST(0.807354922057604), REAL_CONST(0.820178962415188), REAL_CONST(0.832890014164742),
340 REAL_CONST(0.845490050944375), REAL_CONST(0.857980995127572), REAL_CONST(0.870364719583405),
341 REAL_CONST(0.882643049361841), REAL_CONST(0.894817763307943), REAL_CONST(0.906890595608519),
342 REAL_CONST(0.918863237274595), REAL_CONST(0.930737337562886), REAL_CONST(0.942514505339240),
343 REAL_CONST(0.954196310386875), REAL_CONST(0.965784284662087), REAL_CONST(0.977279923499917),
344 REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000)
347 uint32_t pow2_fix(real_t val)
349 uint32_t x1, x2;
350 uint32_t errcorr;
351 uint32_t index_frac;
352 uint32_t retval;
353 int32_t whole = (val >> REAL_BITS);
355 /* rest = [0..1] */
356 int32_t rest = val - (whole << REAL_BITS);
358 /* index into pow2_tab */
359 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
361 /* leave INTERP_BITS bits */
362 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
363 index_frac = index_frac & ((1<<INTERP_BITS)-1);
365 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
366 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
367 errcorr = ( (index_frac*(x2-x1)));
369 retval = errcorr + (x1<<INTERP_BITS);
371 retval = DESCALE_SHIFT(retval, whole, POWTBL_BITS + INTERP_BITS - REAL_BITS);
373 return retval;
376 uint32_t pow2_int(real_t val)
378 uint32_t x1, x2;
379 uint32_t errcorr;
380 uint32_t index_frac;
381 uint32_t retval;
382 int32_t whole = (val >> REAL_BITS);
384 /* rest = [0..1] */
385 int32_t rest = val - (whole << REAL_BITS);
387 /* index into pow2_tab */
388 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
390 /* leave INTERP_BITS bits */
391 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
392 index_frac = index_frac & ((1<<INTERP_BITS)-1);
394 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
395 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
396 errcorr = ( (index_frac*(x2-x1)));
398 retval = errcorr + (x1<<INTERP_BITS);
400 retval = DESCALE_SHIFT(retval, whole, POWTBL_BITS + INTERP_BITS);
402 return retval;
405 /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */
406 int32_t log2_int(uint32_t val)
408 uint32_t frac;
409 int32_t exp = 0;
410 uint32_t index;
411 uint32_t index_frac;
412 uint32_t x1, x2;
413 uint32_t errcorr;
415 /* error */
416 if (val == 0)
417 return -10000;
419 exp = floor_log2(val);
420 exp -= REAL_BITS;
422 /* frac = [1..2] */
423 if (exp >= 0)
424 frac = val >> exp;
425 else
426 frac = val << -exp;
428 /* index in the log2 table */
429 index = frac >> (REAL_BITS-TABLE_BITS);
431 /* leftover part for linear interpolation */
432 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
434 /* leave INTERP_BITS bits */
435 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
437 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
438 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
440 /* linear interpolation */
441 /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */
443 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
445 return ((exp+REAL_BITS) << REAL_BITS) + errcorr + x1;
448 /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */
449 real_t log2_fix(uint32_t val)
451 uint32_t frac;
452 int8_t exp = 0;
453 uint32_t index;
454 uint32_t index_frac;
455 uint32_t x1, x2;
456 uint32_t errcorr;
458 /* error */
459 if (val == 0)
460 return -100000;
462 exp = floor_log2(val);
463 exp -= REAL_BITS;
465 /* frac = [1..2] */
466 if (exp >= 0)
467 frac = val >> exp;
468 else
469 frac = val << -exp;
471 /* index in the log2 table */
472 index = frac >> (REAL_BITS-TABLE_BITS);
474 /* leftover part for linear interpolation */
475 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
477 /* leave INTERP_BITS bits */
478 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
480 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
481 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
483 /* linear interpolation */
484 /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */
486 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
488 return (exp << REAL_BITS) + errcorr + x1;
490 #endif