annotate libfaad2/common.c @ 12443:ae4ae7ab636c

ENCA support (http://trific.ath.cx/software/enca/)
author henry
date Sat, 08 May 2004 17:52:25 +0000
parents 3185f64f6350
children 4a370c80fe5c
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 **
10989
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
25 ** $Id: common.c,v 1.1 2003/08/30 22:30:21 arpi Exp $
10725
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 (92017 <= samplerate) return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
39 if (75132 <= samplerate) return 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
40 if (55426 <= samplerate) return 2;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
41 if (46009 <= samplerate) return 3;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
42 if (37566 <= samplerate) return 4;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
43 if (27713 <= samplerate) return 5;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
44 if (23004 <= samplerate) return 6;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
45 if (18783 <= samplerate) return 7;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
46 if (13856 <= samplerate) return 8;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
47 if (11502 <= samplerate) return 9;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
48 if (9391 <= samplerate) return 10;
10989
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
49 if (16428320 <= samplerate) return 11;
10725
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
10989
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
54 /* Returns the sample rate based on the sample rate index */
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
55 uint32_t get_sample_rate(uint8_t sr_index)
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
56 {
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
57 static const uint32_t sample_rates[] =
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
58 {
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
59 96000, 88200, 64000, 48000, 44100, 32000,
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
60 24000, 22050, 16000, 12000, 11025, 8000
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
61 };
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
62
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
63 if (sr_index < 12)
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
64 return sample_rates[sr_index];
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
65
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
66 return 0;
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
67 }
3185f64f6350 synced with current cvs
alex
parents: 10725
diff changeset
68
10725
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
69 /* Returns 0 if an object type is decodable, otherwise returns -1 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
70 int8_t can_decode_ot(uint8_t object_type)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
71 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
72 switch (object_type)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
73 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
74 case LC:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
75 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
76 case MAIN:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
77 #ifdef MAIN_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
78 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
79 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
80 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
81 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
82 case SSR:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
83 #ifdef SSR_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
84 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
85 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
86 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
87 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
88 case LTP:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
89 #ifdef LTP_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
90 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
91 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
92 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
93 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
94
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
95 /* ER object types */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
96 #ifdef ERROR_RESILIENCE
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
97 case ER_LC:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
98 #ifdef DRM
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
99 case DRM_ER_LC:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
100 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
101 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
102 case ER_LTP:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
103 #ifdef LTP_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
104 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
105 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
106 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
107 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
108 case LD:
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
109 #ifdef LD_DEC
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
110 return 0;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
111 #else
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
112 return -1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
113 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
114 #endif
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
115 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
116
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
117 return -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 static const uint8_t Parity [256] = { // parity
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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,
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
129 };
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
130
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
131 static uint32_t __r1 = 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
132 static uint32_t __r2 = 1;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
133
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
134
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
135 /*
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
136 * This is a simple random number generator with good quality for audio purposes.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
137 * It consists of two polycounters with opposite rotation direction and different
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
138 * periods. The periods are coprime, so the total period is the product of both.
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 * +-> |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
142 * | -------------------------------------------------------------------------------------------------
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
143 * | | | | | | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
144 * | +--+--+--+-XOR-+--------+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
145 * | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
146 * +--------------------------------------------------------------------------------------+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
147 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
148 * -------------------------------------------------------------------------------------------------
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
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| <-+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
150 * ------------------------------------------------------------------------------------------------- |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
151 * | | | | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
152 * +--+----XOR----+--+ |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
153 * | |
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
154 * +----------------------------------------------------------------------------------------+
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
155 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
156 *
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
157 * 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
158 * 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
159 * XORed values of both generators.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
160 */
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
161 uint32_t random_int(void)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
162 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
163 static const uint32_t rnd_seed = 16428320;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
164 uint32_t t1, t2, t3, t4;
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
165
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
166 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
167 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
168 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
169 t1 <<= 31; t2 = Parity [t2];
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
170
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
171 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
172 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
173
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
174 #define LOG2 0.30102999566398
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
175
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
176 int32_t int_log2(int32_t val)
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
177 {
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
178 return (int32_t)ceil(log(val)/log(2));
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
179 }
e989150f8216 libfaad2 v2.0rc1 imported
arpi
parents:
diff changeset
180