comparison Plugins/Input/aac/libfaad2/common.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children 0a2ad94e8607
comparison
equal deleted inserted replaced
60:1771f253e1b2 61:fa848bd484d8
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003 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.
14 **
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.
18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
21 **
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 **
25 ** $Id: common.c,v 1.12 2003/11/12 20:47:57 menno Exp $
26 **/
27
28 /* just some common functions that could be used anywhere */
29
30 #include "common.h"
31 #include "structs.h"
32
33 #include "syntax.h"
34
35 /* Returns the sample rate index based on the samplerate */
36 uint8_t get_sr_index(uint32_t samplerate)
37 {
38 if (92017 <= samplerate) return 0;
39 if (75132 <= samplerate) return 1;
40 if (55426 <= samplerate) return 2;
41 if (46009 <= samplerate) return 3;
42 if (37566 <= samplerate) return 4;
43 if (27713 <= samplerate) return 5;
44 if (23004 <= samplerate) return 6;
45 if (18783 <= samplerate) return 7;
46 if (13856 <= samplerate) return 8;
47 if (11502 <= samplerate) return 9;
48 if (9391 <= samplerate) return 10;
49 if (16428320 <= samplerate) return 11;
50
51 return 11;
52 }
53
54 /* Returns the sample rate based on the sample rate index */
55 uint32_t get_sample_rate(uint8_t sr_index)
56 {
57 static const uint32_t sample_rates[] =
58 {
59 96000, 88200, 64000, 48000, 44100, 32000,
60 24000, 22050, 16000, 12000, 11025, 8000
61 };
62
63 if (sr_index < 12)
64 return sample_rates[sr_index];
65
66 return 0;
67 }
68
69 uint8_t max_pred_sfb(uint8_t sr_index)
70 {
71 static const uint8_t pred_sfb_max[] =
72 {
73 33, 33, 38, 40, 40, 40, 41, 41, 37, 37, 37, 34
74 };
75
76
77 if (sr_index < 12)
78 return pred_sfb_max[sr_index];
79
80 return 0;
81 }
82
83 uint8_t max_tns_sfb(uint8_t sr_index, uint8_t object_type, uint8_t is_short)
84 {
85 /* entry for each sampling rate
86 * 1 Main/LC long window
87 * 2 Main/LC short window
88 * 3 SSR long window
89 * 4 SSR short window
90 */
91 static const uint8_t tns_sbf_max[][4] =
92 {
93 {31, 9, 28, 7}, /* 96000 */
94 {31, 9, 28, 7}, /* 88200 */
95 {34, 10, 27, 7}, /* 64000 */
96 {40, 14, 26, 6}, /* 48000 */
97 {42, 14, 26, 6}, /* 44100 */
98 {51, 14, 26, 6}, /* 32000 */
99 {46, 14, 29, 7}, /* 24000 */
100 {46, 14, 29, 7}, /* 22050 */
101 {42, 14, 23, 8}, /* 16000 */
102 {42, 14, 23, 8}, /* 12000 */
103 {42, 14, 23, 8}, /* 11025 */
104 {39, 14, 19, 7}, /* 8000 */
105 {39, 14, 19, 7}, /* 7350 */
106 {0,0,0,0},
107 {0,0,0,0},
108 {0,0,0,0}
109 };
110 uint8_t i = 0;
111
112 if (is_short) i++;
113 if (object_type == SSR) i += 2;
114
115 return tns_sbf_max[sr_index][i];
116 }
117
118 /* Returns 0 if an object type is decodable, otherwise returns -1 */
119 int8_t can_decode_ot(uint8_t object_type)
120 {
121 switch (object_type)
122 {
123 case LC:
124 return 0;
125 case MAIN:
126 #ifdef MAIN_DEC
127 return 0;
128 #else
129 return -1;
130 #endif
131 case SSR:
132 #ifdef SSR_DEC
133 return 0;
134 #else
135 return -1;
136 #endif
137 case LTP:
138 #ifdef LTP_DEC
139 return 0;
140 #else
141 return -1;
142 #endif
143
144 /* ER object types */
145 #ifdef ERROR_RESILIENCE
146 case ER_LC:
147 #ifdef DRM
148 case DRM_ER_LC:
149 #endif
150 return 0;
151 case ER_LTP:
152 #ifdef LTP_DEC
153 return 0;
154 #else
155 return -1;
156 #endif
157 case LD:
158 #ifdef LD_DEC
159 return 0;
160 #else
161 return -1;
162 #endif
163 #endif
164 }
165
166 return -1;
167 }
168
169
170 static const uint8_t Parity [256] = { // parity
171 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,
172 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,
173 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,
174 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,
175 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,
176 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,
177 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,
178 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
179 };
180
181 static uint32_t __r1 = 1;
182 static uint32_t __r2 = 1;
183
184
185 /*
186 * This is a simple random number generator with good quality for audio purposes.
187 * It consists of two polycounters with opposite rotation direction and different
188 * periods. The periods are coprime, so the total period is the product of both.
189 *
190 * -------------------------------------------------------------------------------------------------
191 * +-> |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|
192 * | -------------------------------------------------------------------------------------------------
193 * | | | | | | |
194 * | +--+--+--+-XOR-+--------+
195 * | |
196 * +--------------------------------------------------------------------------------------+
197 *
198 * -------------------------------------------------------------------------------------------------
199 * |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| <-+
200 * ------------------------------------------------------------------------------------------------- |
201 * | | | | |
202 * +--+----XOR----+--+ |
203 * | |
204 * +----------------------------------------------------------------------------------------+
205 *
206 *
207 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
208 * which gives a period of 18.410.713.077.675.721.215. The result is the
209 * XORed values of both generators.
210 */
211 uint32_t random_int(void)
212 {
213 static const uint32_t rnd_seed = 16428320;
214 uint32_t t1, t2, t3, t4;
215
216 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
217 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
218 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
219 t1 <<= 31; t2 = Parity [t2];
220
221 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
222 }
223
224 #define LOG2 0.30102999566398
225
226 int32_t int_log2(int32_t val)
227 {
228 return (int32_t)ceil(log(val)/log(2));
229 }
230