10725
|
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 **
|
10989
|
25 ** $Id: common.c,v 1.1 2003/08/30 22:30:21 arpi Exp $
|
10725
|
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;
|
10989
|
49 if (16428320 <= samplerate) return 11;
|
10725
|
50
|
|
51 return 11;
|
|
52 }
|
|
53
|
10989
|
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
|
10725
|
69 /* Returns 0 if an object type is decodable, otherwise returns -1 */
|
|
70 int8_t can_decode_ot(uint8_t object_type)
|
|
71 {
|
|
72 switch (object_type)
|
|
73 {
|
|
74 case LC:
|
|
75 return 0;
|
|
76 case MAIN:
|
|
77 #ifdef MAIN_DEC
|
|
78 return 0;
|
|
79 #else
|
|
80 return -1;
|
|
81 #endif
|
|
82 case SSR:
|
|
83 #ifdef SSR_DEC
|
|
84 return 0;
|
|
85 #else
|
|
86 return -1;
|
|
87 #endif
|
|
88 case LTP:
|
|
89 #ifdef LTP_DEC
|
|
90 return 0;
|
|
91 #else
|
|
92 return -1;
|
|
93 #endif
|
|
94
|
|
95 /* ER object types */
|
|
96 #ifdef ERROR_RESILIENCE
|
|
97 case ER_LC:
|
|
98 #ifdef DRM
|
|
99 case DRM_ER_LC:
|
|
100 #endif
|
|
101 return 0;
|
|
102 case ER_LTP:
|
|
103 #ifdef LTP_DEC
|
|
104 return 0;
|
|
105 #else
|
|
106 return -1;
|
|
107 #endif
|
|
108 case LD:
|
|
109 #ifdef LD_DEC
|
|
110 return 0;
|
|
111 #else
|
|
112 return -1;
|
|
113 #endif
|
|
114 #endif
|
|
115 }
|
|
116
|
|
117 return -1;
|
|
118 }
|
|
119
|
|
120 static const uint8_t Parity [256] = { // parity
|
|
121 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,
|
|
122 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,
|
|
123 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,
|
|
124 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,
|
|
125 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,
|
|
126 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,
|
|
127 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,
|
|
128 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
|
|
129 };
|
|
130
|
|
131 static uint32_t __r1 = 1;
|
|
132 static uint32_t __r2 = 1;
|
|
133
|
|
134
|
|
135 /*
|
|
136 * This is a simple random number generator with good quality for audio purposes.
|
|
137 * It consists of two polycounters with opposite rotation direction and different
|
|
138 * periods. The periods are coprime, so the total period is the product of both.
|
|
139 *
|
|
140 * -------------------------------------------------------------------------------------------------
|
|
141 * +-> |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|
|
|
142 * | -------------------------------------------------------------------------------------------------
|
|
143 * | | | | | | |
|
|
144 * | +--+--+--+-XOR-+--------+
|
|
145 * | |
|
|
146 * +--------------------------------------------------------------------------------------+
|
|
147 *
|
|
148 * -------------------------------------------------------------------------------------------------
|
|
149 * |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| <-+
|
|
150 * ------------------------------------------------------------------------------------------------- |
|
|
151 * | | | | |
|
|
152 * +--+----XOR----+--+ |
|
|
153 * | |
|
|
154 * +----------------------------------------------------------------------------------------+
|
|
155 *
|
|
156 *
|
|
157 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
|
|
158 * which gives a period of 18.410.713.077.675.721.215. The result is the
|
|
159 * XORed values of both generators.
|
|
160 */
|
|
161 uint32_t random_int(void)
|
|
162 {
|
|
163 static const uint32_t rnd_seed = 16428320;
|
|
164 uint32_t t1, t2, t3, t4;
|
|
165
|
|
166 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
|
|
167 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
|
|
168 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
|
|
169 t1 <<= 31; t2 = Parity [t2];
|
|
170
|
|
171 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
|
|
172 }
|
|
173
|
|
174 #define LOG2 0.30102999566398
|
|
175
|
|
176 int32_t int_log2(int32_t val)
|
|
177 {
|
|
178 return (int32_t)ceil(log(val)/log(2));
|
|
179 }
|
|
180
|