10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
12527
|
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
|
10725
|
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 **
|
12527
|
25 ** $Id: pns.c,v 1.2 2003/10/03 22:22:27 alex Exp $
|
10725
|
26 **/
|
|
27
|
|
28 #include "common.h"
|
|
29 #include "structs.h"
|
|
30
|
|
31 #include "pns.h"
|
|
32
|
|
33
|
12527
|
34 /* static function declarations */
|
|
35 static void gen_rand_vector(real_t *spec, int16_t scale_factor, uint16_t size,
|
|
36 uint8_t sub);
|
|
37
|
|
38
|
10725
|
39 #ifdef FIXED_POINT
|
|
40
|
10989
|
41 #define DIV(A, B) (((int64_t)A << REAL_BITS)/B)
|
10725
|
42
|
|
43 #define step(shift) \
|
|
44 if ((0x40000000l >> shift) + root <= value) \
|
|
45 { \
|
|
46 value -= (0x40000000l >> shift) + root; \
|
|
47 root = (root >> 1) | (0x40000000l >> shift); \
|
|
48 } else { \
|
|
49 root = root >> 1; \
|
|
50 }
|
|
51
|
|
52 /* fixed point square root approximation */
|
10989
|
53 /* !!!! ONLY WORKS FOR EVEN %REAL_BITS% !!!! */
|
10725
|
54 real_t fp_sqrt(real_t value)
|
|
55 {
|
|
56 real_t root = 0;
|
|
57
|
|
58 step( 0); step( 2); step( 4); step( 6);
|
|
59 step( 8); step(10); step(12); step(14);
|
|
60 step(16); step(18); step(20); step(22);
|
|
61 step(24); step(26); step(28); step(30);
|
|
62
|
|
63 if (root < value)
|
|
64 ++root;
|
|
65
|
10989
|
66 root <<= (REAL_BITS/2);
|
10725
|
67
|
|
68 return root;
|
|
69 }
|
|
70
|
|
71 static real_t pow2_table[] =
|
|
72 {
|
|
73 COEF_CONST(0.59460355750136),
|
|
74 COEF_CONST(0.70710678118655),
|
|
75 COEF_CONST(0.84089641525371),
|
|
76 COEF_CONST(1.0),
|
|
77 COEF_CONST(1.18920711500272),
|
|
78 COEF_CONST(1.41421356237310),
|
|
79 COEF_CONST(1.68179283050743)
|
|
80 };
|
|
81 #endif
|
|
82
|
|
83 /* The function gen_rand_vector(addr, size) generates a vector of length
|
|
84 <size> with signed random values of average energy MEAN_NRG per random
|
|
85 value. A suitable random number generator can be realized using one
|
|
86 multiplication/accumulation per random value.
|
|
87 */
|
10989
|
88 static INLINE void gen_rand_vector(real_t *spec, int16_t scale_factor, uint16_t size,
|
|
89 uint8_t sub)
|
10725
|
90 {
|
|
91 #ifndef FIXED_POINT
|
|
92 uint16_t i;
|
|
93 real_t energy = 0.0;
|
|
94
|
10989
|
95 real_t scale = (real_t)1.0/(real_t)size;
|
10725
|
96
|
|
97 for (i = 0; i < size; i++)
|
|
98 {
|
|
99 real_t tmp = scale*(real_t)(int32_t)random_int();
|
|
100 spec[i] = tmp;
|
|
101 energy += tmp*tmp;
|
|
102 }
|
|
103
|
10989
|
104 scale = (real_t)1.0/(real_t)sqrt(energy);
|
10725
|
105 scale *= (real_t)pow(2.0, 0.25 * scale_factor);
|
|
106 for (i = 0; i < size; i++)
|
|
107 {
|
|
108 spec[i] *= scale;
|
|
109 }
|
|
110 #else
|
|
111 uint16_t i;
|
|
112 real_t energy = 0, scale;
|
|
113 int32_t exp, frac;
|
|
114
|
|
115 for (i = 0; i < size; i++)
|
|
116 {
|
10989
|
117 /* this can be replaced by a 16 bit random generator!!!! */
|
|
118 real_t tmp = (int32_t)random_int();
|
|
119 if (tmp < 0)
|
|
120 tmp = -(tmp & ((1<<(REAL_BITS-1))-1));
|
|
121 else
|
|
122 tmp = (tmp & ((1<<(REAL_BITS-1))-1));
|
10725
|
123
|
12527
|
124 energy += MUL_R(tmp,tmp);
|
10725
|
125
|
10989
|
126 spec[i] = tmp;
|
10725
|
127 }
|
|
128
|
|
129 energy = fp_sqrt(energy);
|
|
130 if (energy > 0)
|
|
131 {
|
10989
|
132 scale = DIV(REAL_CONST(1),energy);
|
10725
|
133
|
|
134 exp = scale_factor / 4;
|
|
135 frac = scale_factor % 4;
|
|
136
|
10989
|
137 /* IMDCT pre-scaling */
|
|
138 exp -= sub;
|
|
139
|
10725
|
140 if (exp < 0)
|
|
141 scale >>= -exp;
|
|
142 else
|
|
143 scale <<= exp;
|
|
144
|
|
145 if (frac)
|
12527
|
146 scale = MUL_C(scale, pow2_table[frac + 3]);
|
10725
|
147
|
|
148 for (i = 0; i < size; i++)
|
|
149 {
|
12527
|
150 spec[i] = MUL_R(spec[i], scale);
|
10725
|
151 }
|
|
152 }
|
|
153 #endif
|
|
154 }
|
|
155
|
|
156 void pns_decode(ic_stream *ics_left, ic_stream *ics_right,
|
|
157 real_t *spec_left, real_t *spec_right, uint16_t frame_len,
|
10989
|
158 uint8_t channel_pair, uint8_t object_type)
|
10725
|
159 {
|
|
160 uint8_t g, sfb, b;
|
|
161 uint16_t size, offs;
|
|
162
|
|
163 uint8_t group = 0;
|
|
164 uint16_t nshort = frame_len >> 3;
|
|
165
|
10989
|
166 uint8_t sub = 0;
|
|
167
|
|
168 #ifdef FIXED_POINT
|
|
169 /* IMDCT scaling */
|
|
170 if (object_type == LD)
|
|
171 {
|
|
172 sub = 9 /*9*/;
|
|
173 } else {
|
|
174 if (ics_left->window_sequence == EIGHT_SHORT_SEQUENCE)
|
|
175 sub = 7 /*7*/;
|
|
176 else
|
|
177 sub = 10 /*10*/;
|
|
178 }
|
|
179 #endif
|
|
180
|
10725
|
181 for (g = 0; g < ics_left->num_window_groups; g++)
|
|
182 {
|
|
183 /* Do perceptual noise substitution decoding */
|
|
184 for (b = 0; b < ics_left->window_group_length[g]; b++)
|
|
185 {
|
|
186 for (sfb = 0; sfb < ics_left->max_sfb; sfb++)
|
|
187 {
|
|
188 if (is_noise(ics_left, g, sfb))
|
|
189 {
|
|
190 /* Simultaneous use of LTP and PNS is not prevented in the
|
|
191 syntax. If both LTP, and PNS are enabled on the same
|
|
192 scalefactor band, PNS takes precedence, and no prediction
|
|
193 is applied to this band.
|
|
194 */
|
|
195 ics_left->ltp.long_used[sfb] = 0;
|
|
196 ics_left->ltp2.long_used[sfb] = 0;
|
|
197
|
|
198 /* For scalefactor bands coded using PNS the corresponding
|
|
199 predictors are switched to "off".
|
|
200 */
|
|
201 ics_left->pred.prediction_used[sfb] = 0;
|
|
202
|
|
203 offs = ics_left->swb_offset[sfb];
|
|
204 size = ics_left->swb_offset[sfb+1] - offs;
|
|
205
|
|
206 /* Generate random vector */
|
|
207 gen_rand_vector(&spec_left[(group*nshort)+offs],
|
10989
|
208 ics_left->scale_factors[g][sfb], size, sub);
|
10725
|
209 }
|
|
210
|
|
211 /* From the spec:
|
|
212 If the same scalefactor band and group is coded by perceptual noise
|
|
213 substitution in both channels of a channel pair, the correlation of
|
|
214 the noise signal can be controlled by means of the ms_used field: While
|
|
215 the default noise generation process works independently for each channel
|
|
216 (separate generation of random vectors), the same random vector is used
|
|
217 for both channels if ms_used[] is set for a particular scalefactor band
|
|
218 and group. In this case, no M/S stereo coding is carried out (because M/S
|
|
219 stereo coding and noise substitution coding are mutually exclusive).
|
|
220 If the same scalefactor band and group is coded by perceptual noise
|
|
221 substitution in only one channel of a channel pair the setting of ms_used[]
|
|
222 is not evaluated.
|
|
223 */
|
|
224 if (channel_pair)
|
|
225 {
|
|
226 if (is_noise(ics_right, g, sfb))
|
|
227 {
|
|
228 if (((ics_left->ms_mask_present == 1) &&
|
|
229 (ics_left->ms_used[g][sfb])) ||
|
|
230 (ics_left->ms_mask_present == 2))
|
|
231 {
|
|
232 uint16_t c;
|
|
233
|
|
234 offs = ics_right->swb_offset[sfb];
|
|
235 size = ics_right->swb_offset[sfb+1] - offs;
|
|
236
|
|
237 for (c = 0; c < size; c++)
|
|
238 {
|
|
239 spec_right[(group*nshort) + offs + c] =
|
|
240 spec_left[(group*nshort) + offs + c];
|
|
241 }
|
|
242 } else /*if (ics_left->ms_mask_present == 0)*/ {
|
|
243 ics_right->ltp.long_used[sfb] = 0;
|
|
244 ics_right->ltp2.long_used[sfb] = 0;
|
|
245 ics_right->pred.prediction_used[sfb] = 0;
|
|
246
|
|
247 offs = ics_right->swb_offset[sfb];
|
|
248 size = ics_right->swb_offset[sfb+1] - offs;
|
|
249
|
|
250 /* Generate random vector */
|
|
251 gen_rand_vector(&spec_right[(group*nshort)+offs],
|
10989
|
252 ics_right->scale_factors[g][sfb], size, sub);
|
10725
|
253 }
|
|
254 }
|
|
255 }
|
|
256 } /* sfb */
|
|
257 group++;
|
|
258 } /* b */
|
|
259 } /* g */
|
|
260 }
|