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