Coverage Report

Created: 2025-07-11 06:39

/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
0
static uint32_t float_to_bits(float32_t f32) {
42
0
    uint32_t u32;
43
0
    memcpy(&u32, &f32, 4);
44
0
    return u32;
45
0
}
46
47
0
static float32_t bits_to_float(uint32_t u32) {
48
0
    float32_t f32;
49
0
    memcpy(&f32, &u32, 4);
50
0
    return f32;
51
0
}
52
53
static float32_t flt_round(float32_t pf)
54
0
{
55
0
    int32_t flg;
56
0
    uint32_t tmp, tmp1, tmp2;
57
58
0
    tmp = float_to_bits(pf);
59
0
    flg = tmp & (uint32_t)0x00008000;
60
0
    tmp &= (uint32_t)0xffff0000;
61
0
    tmp1 = tmp;
62
    /* round 1/2 lsb toward infinity */
63
0
    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
0
    } else {
72
0
        return bits_to_float(tmp);
73
0
    }
74
0
}
75
76
static int16_t quant_pred(float32_t x)
77
0
{
78
0
    return (int16_t)(float_to_bits(x) >> 16);
79
0
}
80
81
static float32_t inv_quant_pred(int16_t q)
82
0
{
83
0
    uint16_t u16 = (uint16_t)q;
84
0
    return bits_to_float((uint32_t)u16 << 16);
85
0
}
86
87
static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
88
0
{
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
0
    uint16_t tmp;
95
0
    int16_t i, j;
96
0
    real_t dr1;
97
0
  float32_t predictedvalue;
98
0
    real_t e0, e1;
99
0
    real_t k1, k2;
100
101
0
    real_t r[2];
102
0
    real_t COR[2];
103
0
    real_t VAR[2];
104
105
0
    r[0] = inv_quant_pred(state->r[0]);
106
0
    r[1] = inv_quant_pred(state->r[1]);
107
0
    COR[0] = inv_quant_pred(state->COR[0]);
108
0
    COR[1] = inv_quant_pred(state->COR[1]);
109
0
    VAR[0] = inv_quant_pred(state->VAR[0]);
110
0
    VAR[1] = inv_quant_pred(state->VAR[1]);
111
112
113
0
#if 1
114
0
    tmp = state->VAR[0];
115
0
    j = (tmp >> 7);
116
0
    i = tmp & 0x7f;
117
0
    if (j >= 128)
118
0
    {
119
0
        j -= 128;
120
0
        k1 = COR[0] * exp_table[j] * mnt_table[i];
121
0
    } else {
122
0
        k1 = REAL_CONST(0);
123
0
    }
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
0
    if (pred)
143
0
    {
144
0
#if 1
145
0
        tmp = state->VAR[1];
146
0
        j = (tmp >> 7);
147
0
        i = tmp & 0x7f;
148
0
        if (j >= 128)
149
0
        {
150
0
            j -= 128;
151
0
            k2 = COR[1] * exp_table[j] * mnt_table[i];
152
0
        } else {
153
0
            k2 = REAL_CONST(0);
154
0
        }
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
0
        predictedvalue = k1*r[0] + k2*r[1];
172
0
        predictedvalue = flt_round(predictedvalue);
173
0
        *output = input + predictedvalue;
174
0
    }
175
176
    /* calculate new state data */
177
0
    e0 = *output;
178
0
    e1 = e0 - k1*r[0];
179
0
    dr1 = k1*e0;
180
181
0
    VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0);
182
0
    COR[0] = ALPHA*COR[0] + r[0]*e0;
183
0
    VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1);
184
0
    COR[1] = ALPHA*COR[1] + r[1]*e1;
185
186
0
    r[1] = A * (r[0]-dr1);
187
0
    r[0] = A * e0;
188
189
0
    state->r[0] = quant_pred(r[0]);
190
0
    state->r[1] = quant_pred(r[1]);
191
0
    state->COR[0] = quant_pred(COR[0]);
192
0
    state->COR[1] = quant_pred(COR[1]);
193
0
    state->VAR[0] = quant_pred(VAR[0]);
194
0
    state->VAR[1] = quant_pred(VAR[1]);
195
0
#endif
196
0
}
197
198
static void reset_pred_state(pred_state *state)
199
0
{
200
0
    state->r[0]   = 0;
201
0
    state->r[1]   = 0;
202
0
    state->COR[0] = 0;
203
0
    state->COR[1] = 0;
204
0
    state->VAR[0] = 0x3F80;
205
0
    state->VAR[1] = 0x3F80;
206
0
}
207
208
void pns_reset_pred_state(ic_stream *ics, pred_state *state)
209
0
{
210
0
    uint8_t sfb, g, b;
211
0
    uint16_t i, offs, offs2;
212
213
    /* prediction only for long blocks */
214
0
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
215
0
        return;
216
217
0
    for (g = 0; g < ics->num_window_groups; g++)
218
0
    {
219
0
        for (b = 0; b < ics->window_group_length[g]; b++)
220
0
        {
221
0
            for (sfb = 0; sfb < ics->max_sfb; sfb++)
222
0
            {
223
0
                if (is_noise(ics, g, sfb))
224
0
                {
225
0
                    offs = ics->swb_offset[sfb];
226
0
                    offs2 = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
227
228
0
                    for (i = offs; i < offs2; i++)
229
0
                        reset_pred_state(&state[i]);
230
0
                }
231
0
            }
232
0
        }
233
0
    }
234
0
}
235
236
void reset_all_predictors(pred_state *state, uint16_t frame_len)
237
0
{
238
0
    uint16_t i;
239
240
0
    for (i = 0; i < frame_len; i++)
241
0
        reset_pred_state(&state[i]);
242
0
}
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
0
{
248
0
    uint8_t sfb;
249
0
    uint16_t bin;
250
251
0
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
252
0
    {
253
0
        reset_all_predictors(state, frame_len);
254
0
    } else {
255
0
        for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++)
256
0
        {
257
0
            uint16_t low  = ics->swb_offset[sfb];
258
0
            uint16_t high = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
259
260
0
            for (bin = low; bin < high; bin++)
261
0
            {
262
0
                ic_predict(&state[bin], spec[bin], &spec[bin],
263
0
                    (ics->predictor_data_present && ics->pred.prediction_used[sfb]));
264
0
            }
265
0
        }
266
267
0
        if (ics->predictor_data_present)
268
0
        {
269
0
            if (ics->pred.predictor_reset)
270
0
            {
271
0
                for (bin = ics->pred.predictor_reset_group_number - 1;
272
0
                     bin < frame_len; bin += 30)
273
0
                {
274
0
                    reset_pred_state(&state[bin]);
275
0
                }
276
0
            }
277
0
        }
278
0
    }
279
0
}
280
281
#endif