annotate libfaad2/common.c @ 10790:f40a897cb276

sync
author gabucino
date Mon, 01 Sep 2003 19:47:34 +0000
parents e989150f8216
children 3185f64f6350
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10725
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
1 /*
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
4 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
5 ** This program is free software; you can redistribute it and/or modify
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
6 ** it under the terms of the GNU General Public License as published by
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
7 ** the Free Software Foundation; either version 2 of the License, or
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
8 ** (at your option) any later version.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
9 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
10 ** This program is distributed in the hope that it will be useful,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
13 ** GNU General Public License for more details.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
14 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
15 ** You should have received a copy of the GNU General Public License
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
16 ** along with this program; if not, write to the Free Software
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
18 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
19 ** Any non-GPL usage of this software or parts of this software is strictly
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
20 ** forbidden.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
21 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
22 ** Commercial non-GPL licensing of this software is possible.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
24 **
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
25 ** $Id: common.c,v 1.6 2003/07/29 08:20:12 menno Exp $
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
26 **/
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
27
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
28 /* just some common functions that could be used anywhere */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
29
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
30 #include "common.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
31 #include "structs.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
32
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
33 #include "syntax.h"
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
34
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
35 /* Returns the sample rate index based on the samplerate */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
36 uint8_t get_sr_index(uint32_t samplerate)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
37 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
38 if (16428320 <= samplerate) return 11;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
39 if (92017 <= samplerate) return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
40 if (75132 <= samplerate) return 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
41 if (55426 <= samplerate) return 2;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
42 if (46009 <= samplerate) return 3;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
43 if (37566 <= samplerate) return 4;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
44 if (27713 <= samplerate) return 5;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
45 if (23004 <= samplerate) return 6;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
46 if (18783 <= samplerate) return 7;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
47 if (13856 <= samplerate) return 8;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
48 if (11502 <= samplerate) return 9;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
49 if (9391 <= samplerate) return 10;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
50
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
51 return 11;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
52 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
53
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
54 /* Returns 0 if an object type is decodable, otherwise returns -1 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
55 int8_t can_decode_ot(uint8_t object_type)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
56 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
57 switch (object_type)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
58 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
59 case LC:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
60 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
61 case MAIN:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
62 #ifdef MAIN_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
63 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
64 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
65 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
66 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
67 case SSR:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
68 #ifdef SSR_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
69 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
70 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
71 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
72 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
73 case LTP:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
74 #ifdef LTP_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
75 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
76 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
77 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
78 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
79
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
80 /* ER object types */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
81 #ifdef ERROR_RESILIENCE
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
82 case ER_LC:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
83 #ifdef DRM
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
84 case DRM_ER_LC:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
85 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
86 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
87 case ER_LTP:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
88 #ifdef LTP_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
89 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
90 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
91 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
92 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
93 case LD:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
94 #ifdef LD_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
95 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
96 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
97 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
98 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
99 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
100 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
101
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
102 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
103 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
104
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
105 static const uint8_t Parity [256] = { // parity
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
106 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
107 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
108 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
109 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
110 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
111 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
112 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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
113 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
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
114 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
115
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
116 static uint32_t __r1 = 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
117 static uint32_t __r2 = 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
118
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
119
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
120 /*
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
121 * This is a simple random number generator with good quality for audio purposes.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
122 * It consists of two polycounters with opposite rotation direction and different
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
123 * periods. The periods are coprime, so the total period is the product of both.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
124 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
125 * -------------------------------------------------------------------------------------------------
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
126 * +-> |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|
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
127 * | -------------------------------------------------------------------------------------------------
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
128 * | | | | | | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
129 * | +--+--+--+-XOR-+--------+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
130 * | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
131 * +--------------------------------------------------------------------------------------+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
132 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
133 * -------------------------------------------------------------------------------------------------
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
134 * |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| <-+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
135 * ------------------------------------------------------------------------------------------------- |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
136 * | | | | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
137 * +--+----XOR----+--+ |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
138 * | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
139 * +----------------------------------------------------------------------------------------+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
140 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
141 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
142 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
143 * which gives a period of 18.410.713.077.675.721.215. The result is the
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
144 * XORed values of both generators.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
145 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
146 uint32_t random_int(void)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
147 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
148 static const uint32_t rnd_seed = 16428320;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
149 uint32_t t1, t2, t3, t4;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
150
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
151 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
152 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
153 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
154 t1 <<= 31; t2 = Parity [t2];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
155
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
156 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
157 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
158
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
159 #define LOG2 0.30102999566398
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
160
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
161 int32_t int_log2(int32_t val)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
162 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
163 return (int32_t)ceil(log(val)/log(2));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
164 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
165