Coverage Report

Created: 2025-07-23 06:30

/proc/self/cwd/libfaad/ic_predict.c
Line
Count
Source (jump to first uncovered line)
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: ic_predict.c,v 1.28 2007/11/01 12:33:31 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#ifdef MAIN_DEC
35
36
#include "syntax.h"
37
#include "ic_predict.h"
38
#include "pns.h"
39
40
41
356M
static uint32_t float_to_bits(float32_t f32) {
42
356M
    uint32_t u32;
43
356M
    memcpy(&u32, &f32, 4);
44
356M
    return u32;
45
356M
}
46
47
356M
static float32_t bits_to_float(uint32_t u32) {
48
356M
    float32_t f32;
49
356M
    memcpy(&f32, &u32, 4);
50
356M
    return f32;
51
356M
}
52
53
static float32_t flt_round(float32_t pf)
54
17.5k
{
55
17.5k
    int32_t flg;
56
17.5k
    uint32_t tmp, tmp1, tmp2;
57
58
17.5k
    tmp = float_to_bits(pf);
59
17.5k
    flg = tmp & (uint32_t)0x00008000;
60
17.5k
    tmp &= (uint32_t)0xffff0000;
61
17.5k
    tmp1 = tmp;
62
    /* round 1/2 lsb toward infinity */
63
17.5k
    if (flg)
64
0
    {
65
0
        tmp &= (uint32_t)0xff800000;       /* extract exponent and sign */
66
0
        tmp |= (uint32_t)0x00010000;       /* insert 1 lsb */
67
0
        tmp2 = tmp;                             /* add 1 lsb and elided one */
68
0
        tmp &= (uint32_t)0xff800000;       /* extract exponent and sign */
69
70
0
        return bits_to_float(tmp1) + bits_to_float(tmp2) - bits_to_float(tmp);
71
17.5k
    } else {
72
17.5k
        return bits_to_float(tmp);
73
17.5k
    }
74
17.5k
}
75
76
static int16_t quant_pred(float32_t x)
77
356M
{
78
356M
    return (int16_t)(float_to_bits(x) >> 16);
79
356M
}
80
81
static float32_t inv_quant_pred(int16_t q)
82
356M
{
83
356M
    uint16_t u16 = (uint16_t)q;
84
356M
    return bits_to_float((uint32_t)u16 << 16);
85
356M
}
86
87
static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
88
59.3M
{
89
#ifdef FIXED_POINT
90
    // main codepath is simply not ready for FIXED_POINT, better not to run it at all.
91
    if (pred)
92
        *output = input;
93
#else
94
59.3M
    uint16_t tmp;
95
59.3M
    int16_t i, j;
96
59.3M
    real_t dr1;
97
59.3M
  float32_t predictedvalue;
98
59.3M
    real_t e0, e1;
99
59.3M
    real_t k1, k2;
100
101
59.3M
    real_t r[2];
102
59.3M
    real_t COR[2];
103
59.3M
    real_t VAR[2];
104
105
59.3M
    r[0] = inv_quant_pred(state->r[0]);
106
59.3M
    r[1] = inv_quant_pred(state->r[1]);
107
59.3M
    COR[0] = inv_quant_pred(state->COR[0]);
108
59.3M
    COR[1] = inv_quant_pred(state->COR[1]);
109
59.3M
    VAR[0] = inv_quant_pred(state->VAR[0]);
110
59.3M
    VAR[1] = inv_quant_pred(state->VAR[1]);
111
112
113
59.3M
#if 1
114
59.3M
    tmp = state->VAR[0];
115
59.3M
    j = (tmp >> 7);
116
59.3M
    i = tmp & 0x7f;
117
59.3M
    if (j >= 128)
118
5.47k
    {
119
5.47k
        j -= 128;
120
5.47k
        k1 = COR[0] * exp_table[j] * mnt_table[i];
121
59.3M
    } else {
122
59.3M
        k1 = REAL_CONST(0);
123
59.3M
    }
124
#else
125
126
    {
127
#define B 0.953125
128
        real_t c = COR[0];
129
        real_t v = VAR[0];
130
        float32_t tmp;
131
        if (c == 0 || v <= 1)
132
        {
133
            k1 = 0;
134
        } else {
135
            tmp = B / v;
136
            flt_round(&tmp);
137
            k1 = c * tmp;
138
        }
139
    }
140
#endif
141
142
59.3M
    if (pred)
143
17.5k
    {
144
17.5k
#if 1
145
17.5k
        tmp = state->VAR[1];
146
17.5k
        j = (tmp >> 7);
147
17.5k
        i = tmp & 0x7f;
148
17.5k
        if (j >= 128)
149
490
        {
150
490
            j -= 128;
151
490
            k2 = COR[1] * exp_table[j] * mnt_table[i];
152
17.0k
        } else {
153
17.0k
            k2 = REAL_CONST(0);
154
17.0k
        }
155
#else
156
157
#define B 0.953125
158
        real_t c = COR[1];
159
        real_t v = VAR[1];
160
        float32_t tmp;
161
        if (c == 0 || v <= 1)
162
        {
163
            k2 = 0;
164
        } else {
165
            tmp = B / v;
166
            flt_round(&tmp);
167
            k2 = c * tmp;
168
        }
169
#endif
170
171
17.5k
        predictedvalue = k1*r[0] + k2*r[1];
172
17.5k
        predictedvalue = flt_round(predictedvalue);
173
17.5k
        *output = input + predictedvalue;
174
17.5k
    }
175
176
    /* calculate new state data */
177
59.3M
    e0 = *output;
178
59.3M
    e1 = e0 - k1*r[0];
179
59.3M
    dr1 = k1*e0;
180
181
59.3M
    VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0);
182
59.3M
    COR[0] = ALPHA*COR[0] + r[0]*e0;
183
59.3M
    VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1);
184
59.3M
    COR[1] = ALPHA*COR[1] + r[1]*e1;
185
186
59.3M
    r[1] = A * (r[0]-dr1);
187
59.3M
    r[0] = A * e0;
188
189
59.3M
    state->r[0] = quant_pred(r[0]);
190
59.3M
    state->r[1] = quant_pred(r[1]);
191
59.3M
    state->COR[0] = quant_pred(COR[0]);
192
59.3M
    state->COR[1] = quant_pred(COR[1]);
193
59.3M
    state->VAR[0] = quant_pred(VAR[0]);
194
59.3M
    state->VAR[1] = quant_pred(VAR[1]);
195
59.3M
#endif
196
59.3M
}
197
198
static void reset_pred_state(pred_state *state)
199
109M
{
200
109M
    state->r[0]   = 0;
201
109M
    state->r[1]   = 0;
202
109M
    state->COR[0] = 0;
203
109M
    state->COR[1] = 0;
204
109M
    state->VAR[0] = 0x3F80;
205
109M
    state->VAR[1] = 0x3F80;
206
109M
}
207
208
void pns_reset_pred_state(ic_stream *ics, pred_state *state)
209
109k
{
210
109k
    uint8_t sfb, g, b;
211
109k
    uint16_t i, offs, offs2;
212
213
    /* prediction only for long blocks */
214
109k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
215
10.5k
        return;
216
217
197k
    for (g = 0; g < ics->num_window_groups; g++)
218
98.7k
    {
219
197k
        for (b = 0; b < ics->window_group_length[g]; b++)
220
98.7k
        {
221
118k
            for (sfb = 0; sfb < ics->max_sfb; sfb++)
222
19.8k
            {
223
19.8k
                if (is_noise(ics, g, sfb))
224
7.09k
                {
225
7.09k
                    offs = ics->swb_offset[sfb];
226
7.09k
                    offs2 = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
227
228
226k
                    for (i = offs; i < offs2; i++)
229
219k
                        reset_pred_state(&state[i]);
230
7.09k
                }
231
19.8k
            }
232
98.7k
        }
233
98.7k
    }
234
98.7k
}
235
236
void reset_all_predictors(pred_state *state, uint16_t frame_len)
237
107k
{
238
107k
    uint16_t i;
239
240
109M
    for (i = 0; i < frame_len; i++)
241
109M
        reset_pred_state(&state[i]);
242
107k
}
243
244
/* intra channel prediction */
245
void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state,
246
                   uint16_t frame_len, uint8_t sf_index)
247
109k
{
248
109k
    uint8_t sfb;
249
109k
    uint16_t bin;
250
251
109k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
252
10.5k
    {
253
10.5k
        reset_all_predictors(state, frame_len);
254
98.7k
    } else {
255
3.59M
        for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++)
256
3.49M
        {
257
3.49M
            uint16_t low  = ics->swb_offset[sfb];
258
3.49M
            uint16_t high = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
259
260
62.8M
            for (bin = low; bin < high; bin++)
261
59.3M
            {
262
59.3M
                ic_predict(&state[bin], spec[bin], &spec[bin],
263
59.3M
                    (ics->predictor_data_present && ics->pred.prediction_used[sfb]));
264
59.3M
            }
265
3.49M
        }
266
267
98.7k
        if (ics->predictor_data_present)
268
16.0k
        {
269
16.0k
            if (ics->pred.predictor_reset)
270
14.4k
            {
271
14.4k
                for (bin = ics->pred.predictor_reset_group_number - 1;
272
467k
                     bin < frame_len; bin += 30)
273
452k
                {
274
452k
                    reset_pred_state(&state[bin]);
275
452k
                }
276
14.4k
            }
277
16.0k
        }
278
98.7k
    }
279
109k
}
280
281
#endif