Coverage Report

Created: 2025-08-29 06:11

/proc/self/cwd/libfaad/pns.c
Line
Count
Source
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.39 2010/06/04 20:47:56 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include "pns.h"
35
36
37
/* static function declarations */
38
static 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
static real_t const pow2_table[] =
46
{
47
    COEF_CONST(1.0),
48
    COEF_CONST(1.18920711500272),
49
    COEF_CONST(1.41421356237310),
50
    COEF_CONST(1.68179283050743)
51
};
52
53
// mean_energy_table[x] == sqrt(3 / x)
54
static real_t const mean_energy_table[] =
55
{
56
    COEF_CONST(0.0),                // should not happen
57
    COEF_CONST(1.7320508075688772),
58
    COEF_CONST(1.224744871391589),
59
    COEF_CONST(1.0),                // sqrt(3/3)
60
    COEF_CONST(0.8660254037844386),
61
    COEF_CONST(0.7745966692414834),
62
    COEF_CONST(0.7071067811865476),
63
    COEF_CONST(0.6546536707079771),
64
    COEF_CONST(0.6123724356957945),
65
    COEF_CONST(0.5773502691896257),
66
    COEF_CONST(0.5477225575051661),
67
    COEF_CONST(0.5222329678670935),
68
    COEF_CONST(0.5),                // sqrt(3/12)
69
    COEF_CONST(0.4803844614152614),
70
    COEF_CONST(0.4629100498862757),
71
    COEF_CONST(0.4472135954999579),
72
};
73
#endif
74
75
/* The function gen_rand_vector(addr, size) generates a vector of length
76
   <size> with signed random values of average energy MEAN_NRG per random
77
   value. A suitable random number generator can be realized using one
78
   multiplication/accumulation per random value.
79
*/
80
static INLINE void gen_rand_vector(real_t *spec, int16_t scale_factor, uint16_t size,
81
                                   uint8_t sub,
82
                                   /* RNG states */ uint32_t *__r1, uint32_t *__r2)
83
39.6k
{
84
39.6k
#ifndef FIXED_POINT
85
39.6k
    uint16_t i;
86
39.6k
    real_t energy = 0.0;
87
39.6k
    (void)sub;
88
89
39.6k
    scale_factor = min(max(scale_factor, -120), 120);
90
91
392k
    for (i = 0; i < size; i++)
92
353k
    {
93
353k
        real_t tmp = (real_t)(int32_t)ne_rng(__r1, __r2);
94
353k
        spec[i] = tmp;
95
353k
        energy += tmp*tmp;
96
353k
    }
97
98
39.6k
    if (energy > 0)
99
17.7k
    {
100
17.7k
        real_t scale = (real_t)1.0/(real_t)sqrt(energy);
101
17.7k
        scale *= (real_t)pow(2.0, 0.25 * scale_factor);
102
370k
        for (i = 0; i < size; i++)
103
353k
        {
104
353k
            spec[i] *= scale;
105
353k
        }
106
17.7k
    }
107
#else
108
    uint16_t i;
109
    real_t scale;
110
    int32_t exp, frac;
111
    int32_t idx, mask;
112
113
    /* IMDCT pre-scaling */
114
    scale_factor -= 4 * sub;
115
116
    // 52 stands for 2**13 == 8192 factor; larger factor causes overflows later (in cfft).
117
    scale_factor = min(max(scale_factor, -(REAL_BITS * 4)), 52);
118
119
    exp = scale_factor >> 2;
120
    frac = scale_factor & 3;
121
122
    /* 29 <= REAL_BITS + exp <= 0 */
123
    mask = (1 << (REAL_BITS + exp)) - 1;
124
125
    idx = size;
126
    scale = COEF_CONST(1);
127
    // At most 2 iterations.
128
    while (idx >= 16)
129
    {
130
        idx >>= 2;
131
        scale >>= 1;
132
    }
133
    scale = MUL_C(scale, mean_energy_table[idx]);
134
    if (frac)
135
        scale = MUL_C(scale, pow2_table[frac]);
136
    // scale is less than 4.0 now.
137
138
    for (i = 0; i < size; i++)
139
    {
140
        real_t tmp = (int32_t)ne_rng(__r1, __r2);
141
        if (tmp < 0)
142
            tmp = -(tmp & mask);
143
        else
144
            tmp = (tmp & mask);
145
        spec[i] = MUL_C(tmp, scale);
146
    }
147
#endif
148
39.6k
}
149
150
void pns_decode(ic_stream *ics_left, ic_stream *ics_right,
151
                real_t *spec_left, real_t *spec_right, uint16_t frame_len,
152
                uint8_t channel_pair, uint8_t object_type,
153
                /* RNG states */ uint32_t *__r1, uint32_t *__r2)
154
221k
{
155
221k
    uint8_t g, sfb, b;
156
221k
    uint16_t begin, end;
157
158
221k
    uint8_t group = 0;
159
221k
    uint16_t nshort = frame_len >> 3;
160
161
221k
    uint8_t sub = 0;
162
163
#ifdef FIXED_POINT
164
    /* IMDCT scaling */
165
    if (object_type == LD)
166
    {
167
        sub = 9 /*9*/;
168
    } else {
169
        if (ics_left->window_sequence == EIGHT_SHORT_SEQUENCE)
170
            sub = 7 /*7*/;
171
        else
172
            sub = 10 /*10*/;
173
    }
174
#else
175
221k
    (void)object_type;
176
221k
#endif
177
178
565k
    for (g = 0; g < ics_left->num_window_groups; g++)
179
343k
    {
180
        /* Do perceptual noise substitution decoding */
181
715k
        for (b = 0; b < ics_left->window_group_length[g]; b++)
182
372k
        {
183
372k
            uint16_t base = group * nshort;
184
490k
            for (sfb = 0; sfb < ics_left->max_sfb; sfb++)
185
118k
            {
186
118k
                uint32_t r1_dep = 0, r2_dep = 0;
187
188
118k
                if (is_noise(ics_left, g, sfb))
189
26.9k
                {
190
26.9k
#ifdef LTP_DEC
191
                    /* Simultaneous use of LTP and PNS is not prevented in the
192
                       syntax. If both LTP, and PNS are enabled on the same
193
                       scalefactor band, PNS takes precedence, and no prediction
194
                       is applied to this band.
195
                    */
196
26.9k
                    ics_left->ltp.long_used[sfb] = 0;
197
26.9k
                    ics_left->ltp2.long_used[sfb] = 0;
198
26.9k
#endif
199
200
26.9k
#ifdef MAIN_DEC
201
                    /* For scalefactor bands coded using PNS the corresponding
202
                       predictors are switched to "off".
203
                    */
204
26.9k
                    ics_left->pred.prediction_used[sfb] = 0;
205
26.9k
#endif
206
26.9k
                    begin = min(base + ics_left->swb_offset[sfb], ics_left->swb_offset_max);
207
26.9k
                    end = min(base + ics_left->swb_offset[sfb+1], ics_left->swb_offset_max);
208
209
26.9k
                    r1_dep = *__r1;
210
26.9k
                    r2_dep = *__r2;
211
212
                    /* Generate random vector */
213
26.9k
                    gen_rand_vector(&spec_left[begin],
214
26.9k
                        ics_left->scale_factors[g][sfb], end - begin, sub, __r1, __r2);
215
26.9k
                }
216
217
/* From the spec:
218
   If the same scalefactor band and group is coded by perceptual noise
219
   substitution in both channels of a channel pair, the correlation of
220
   the noise signal can be controlled by means of the ms_used field: While
221
   the default noise generation process works independently for each channel
222
   (separate generation of random vectors), the same random vector is used
223
   for both channels if ms_used[] is set for a particular scalefactor band
224
   and group. In this case, no M/S stereo coding is carried out (because M/S
225
   stereo coding and noise substitution coding are mutually exclusive).
226
   If the same scalefactor band and group is coded by perceptual noise
227
   substitution in only one channel of a channel pair the setting of ms_used[]
228
   is not evaluated.
229
*/
230
118k
                if ((ics_right != NULL)
231
118k
                    && is_noise(ics_right, g, sfb))
232
12.6k
                {
233
12.6k
#ifdef LTP_DEC
234
                    /* See comment above. */
235
12.6k
                    ics_right->ltp.long_used[sfb] = 0;
236
12.6k
                    ics_right->ltp2.long_used[sfb] = 0;
237
12.6k
#endif
238
12.6k
#ifdef MAIN_DEC
239
                    /* See comment above. */
240
12.6k
                    ics_right->pred.prediction_used[sfb] = 0;
241
12.6k
#endif
242
243
12.6k
                    if (channel_pair && is_noise(ics_left, g, sfb) &&
244
12.6k
                        (((ics_left->ms_mask_present == 1) &&
245
9.60k
                        (ics_left->ms_used[g][sfb])) ||
246
9.60k
                        (ics_left->ms_mask_present == 2)))
247
6.42k
                    {
248
                        /*uint16_t c;*/
249
250
6.42k
                        begin = min(base + ics_right->swb_offset[sfb], ics_right->swb_offset_max);
251
6.42k
                        end = min(base + ics_right->swb_offset[sfb+1], ics_right->swb_offset_max);
252
253
                        /* Generate random vector dependent on left channel*/
254
6.42k
                        gen_rand_vector(&spec_right[begin],
255
6.42k
                            ics_right->scale_factors[g][sfb], end - begin, sub, &r1_dep, &r2_dep);
256
257
6.42k
                    } else /*if (ics_left->ms_mask_present == 0)*/ {
258
6.24k
                        begin = min(base + ics_right->swb_offset[sfb], ics_right->swb_offset_max);
259
6.24k
                        end = min(base + ics_right->swb_offset[sfb+1], ics_right->swb_offset_max);
260
261
                        /* Generate random vector */
262
6.24k
                        gen_rand_vector(&spec_right[begin],
263
6.24k
                            ics_right->scale_factors[g][sfb], end - begin, sub, __r1, __r2);
264
6.24k
                    }
265
12.6k
                }
266
118k
            } /* sfb */
267
372k
            group++;
268
372k
        } /* b */
269
343k
    } /* g */
270
221k
}