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 **
|
|
25 ** $Id: common.c,v 1.6 2003/07/29 08:20:12 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 (16428320 <= samplerate) return 11;
|
|
39 if (92017 <= samplerate) return 0;
|
|
40 if (75132 <= samplerate) return 1;
|
|
41 if (55426 <= samplerate) return 2;
|
|
42 if (46009 <= samplerate) return 3;
|
|
43 if (37566 <= samplerate) return 4;
|
|
44 if (27713 <= samplerate) return 5;
|
|
45 if (23004 <= samplerate) return 6;
|
|
46 if (18783 <= samplerate) return 7;
|
|
47 if (13856 <= samplerate) return 8;
|
|
48 if (11502 <= samplerate) return 9;
|
|
49 if (9391 <= samplerate) return 10;
|
|
50
|
|
51 return 11;
|
|
52 }
|
|
53
|
|
54 /* Returns 0 if an object type is decodable, otherwise returns -1 */
|
|
55 int8_t can_decode_ot(uint8_t object_type)
|
|
56 {
|
|
57 switch (object_type)
|
|
58 {
|
|
59 case LC:
|
|
60 return 0;
|
|
61 case MAIN:
|
|
62 #ifdef MAIN_DEC
|
|
63 return 0;
|
|
64 #else
|
|
65 return -1;
|
|
66 #endif
|
|
67 case SSR:
|
|
68 #ifdef SSR_DEC
|
|
69 return 0;
|
|
70 #else
|
|
71 return -1;
|
|
72 #endif
|
|
73 case LTP:
|
|
74 #ifdef LTP_DEC
|
|
75 return 0;
|
|
76 #else
|
|
77 return -1;
|
|
78 #endif
|
|
79
|
|
80 /* ER object types */
|
|
81 #ifdef ERROR_RESILIENCE
|
|
82 case ER_LC:
|
|
83 #ifdef DRM
|
|
84 case DRM_ER_LC:
|
|
85 #endif
|
|
86 return 0;
|
|
87 case ER_LTP:
|
|
88 #ifdef LTP_DEC
|
|
89 return 0;
|
|
90 #else
|
|
91 return -1;
|
|
92 #endif
|
|
93 case LD:
|
|
94 #ifdef LD_DEC
|
|
95 return 0;
|
|
96 #else
|
|
97 return -1;
|
|
98 #endif
|
|
99 #endif
|
|
100 }
|
|
101
|
|
102 return -1;
|
|
103 }
|
|
104
|
|
105 static const uint8_t Parity [256] = { // parity
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
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
|
|
114 };
|
|
115
|
|
116 static uint32_t __r1 = 1;
|
|
117 static uint32_t __r2 = 1;
|
|
118
|
|
119
|
|
120 /*
|
|
121 * This is a simple random number generator with good quality for audio purposes.
|
|
122 * It consists of two polycounters with opposite rotation direction and different
|
|
123 * periods. The periods are coprime, so the total period is the product of both.
|
|
124 *
|
|
125 * -------------------------------------------------------------------------------------------------
|
|
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|
|
|
127 * | -------------------------------------------------------------------------------------------------
|
|
128 * | | | | | | |
|
|
129 * | +--+--+--+-XOR-+--------+
|
|
130 * | |
|
|
131 * +--------------------------------------------------------------------------------------+
|
|
132 *
|
|
133 * -------------------------------------------------------------------------------------------------
|
|
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| <-+
|
|
135 * ------------------------------------------------------------------------------------------------- |
|
|
136 * | | | | |
|
|
137 * +--+----XOR----+--+ |
|
|
138 * | |
|
|
139 * +----------------------------------------------------------------------------------------+
|
|
140 *
|
|
141 *
|
|
142 * The first has an period of 3*5*17*257*65537, the second of 7*47*73*178481,
|
|
143 * which gives a period of 18.410.713.077.675.721.215. The result is the
|
|
144 * XORed values of both generators.
|
|
145 */
|
|
146 uint32_t random_int(void)
|
|
147 {
|
|
148 static const uint32_t rnd_seed = 16428320;
|
|
149 uint32_t t1, t2, t3, t4;
|
|
150
|
|
151 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
|
|
152 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
|
|
153 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
|
|
154 t1 <<= 31; t2 = Parity [t2];
|
|
155
|
|
156 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
|
|
157 }
|
|
158
|
|
159 #define LOG2 0.30102999566398
|
|
160
|
|
161 int32_t int_log2(int32_t val)
|
|
162 {
|
|
163 return (int32_t)ceil(log(val)/log(2));
|
|
164 }
|
|
165
|