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