Coverage Report

Created: 2025-11-16 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/tns.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: tns.c,v 1.40 2007/11/01 12:33:40 menno Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include "syntax.h"
35
#include "tns.h"
36
37
38
/* static function declarations */
39
/* returns exp */
40
static uint8_t tns_decode_coef(uint8_t order, uint8_t coef_res_bits,
41
                               uint8_t coef_compress, uint8_t *coef, real_t *a);
42
static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
43
                          uint8_t order, uint8_t exp);
44
static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
45
                          uint8_t order, uint8_t exp);
46
47
48
#ifdef _MSC_VER
49
#pragma warning(disable:4305)
50
#pragma warning(disable:4244)
51
#endif
52
static real_t tns_coef_0_3[] =
53
{
54
    COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(0.7818314825), COEF_CONST(0.9749279122),
55
    COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
56
    COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.9749279122), COEF_CONST(-0.9749279122),
57
    COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433)
58
};
59
static real_t tns_coef_0_4[] =
60
{
61
    COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523),
62
    COEF_CONST(0.7431448255), COEF_CONST(0.8660254038), COEF_CONST(0.9510565163), COEF_CONST(0.9945218954),
63
    COEF_CONST(-0.9957341763), COEF_CONST(-0.9618256432), COEF_CONST(-0.8951632914), COEF_CONST(-0.7980172273),
64
    COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178)
65
};
66
static real_t tns_coef_1_3[] =
67
{
68
    COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
69
    COEF_CONST(0.9749279122), COEF_CONST(0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
70
    COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
71
    COEF_CONST(-0.7818314825), COEF_CONST(-0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433)
72
};
73
static real_t tns_coef_1_4[] =
74
{
75
    COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523),
76
    COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178),
77
    COEF_CONST(0.9945218954), COEF_CONST(0.9510565163), COEF_CONST(0.8660254038), COEF_CONST(0.7431448255),
78
    COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178)
79
};
80
81
static real_t* all_tns_coefs[] = {tns_coef_0_3, tns_coef_0_4, tns_coef_1_3, tns_coef_1_4};
82
83
/* TNS decoding for one channel and frame */
84
void tns_decode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
85
                      uint8_t object_type, real_t *spec, uint16_t frame_len)
86
509k
{
87
509k
    uint8_t w, f, tns_order;
88
509k
    int8_t inc;
89
509k
    int16_t size;
90
509k
    uint16_t bottom, top, start, end;
91
509k
    uint16_t nshort = frame_len/8;
92
509k
    real_t lpc[TNS_MAX_ORDER+1];
93
509k
    uint8_t exp;
94
95
509k
    if (!ics->tns_data_present)
96
473k
        return;
97
98
111k
    for (w = 0; w < ics->num_windows; w++)
99
75.3k
    {
100
75.3k
        bottom = ics->num_swb;
101
102
107k
        for (f = 0; f < tns->n_filt[w]; f++)
103
32.1k
        {
104
32.1k
            top = bottom;
105
32.1k
            bottom = max(top - tns->length[w][f], 0);
106
32.1k
            tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
107
32.1k
            if (!tns_order)
108
13.7k
                continue;
109
110
18.3k
            exp = tns_decode_coef(tns_order, tns->coef_res[w]+3,
111
18.3k
                tns->coef_compress[w][f], tns->coef[w][f], lpc);
112
113
18.3k
            start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
114
18.3k
            start = min(start, ics->max_sfb);
115
18.3k
            start = min(ics->swb_offset[start], ics->swb_offset_max);
116
117
18.3k
            end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
118
18.3k
            end = min(end, ics->max_sfb);
119
18.3k
            end = min(ics->swb_offset[end], ics->swb_offset_max);
120
121
18.3k
            size = end - start;
122
18.3k
            if (size <= 0)
123
15.6k
                continue;
124
125
2.74k
            if (tns->direction[w][f])
126
1.33k
            {
127
1.33k
                inc = -1;
128
1.33k
                start = end - 1;
129
1.41k
            } else {
130
1.41k
                inc = 1;
131
1.41k
            }
132
133
2.74k
            tns_ar_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order, exp);
134
2.74k
        }
135
75.3k
    }
136
36.2k
}
137
138
/* TNS encoding for one channel and frame */
139
void tns_encode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
140
                      uint8_t object_type, real_t *spec, uint16_t frame_len)
141
11.8k
{
142
11.8k
    uint8_t w, f, tns_order;
143
11.8k
    int8_t inc;
144
11.8k
    int16_t size;
145
11.8k
    uint16_t bottom, top, start, end;
146
11.8k
    uint16_t nshort = frame_len/8;
147
11.8k
    real_t lpc[TNS_MAX_ORDER+1];
148
11.8k
    uint8_t exp;
149
150
11.8k
    if (!ics->tns_data_present)
151
10.2k
        return;
152
153
3.24k
    for (w = 0; w < ics->num_windows; w++)
154
1.62k
    {
155
1.62k
        bottom = ics->num_swb;
156
157
5.41k
        for (f = 0; f < tns->n_filt[w]; f++)
158
3.79k
        {
159
3.79k
            top = bottom;
160
3.79k
            bottom = max(top - tns->length[w][f], 0);
161
3.79k
            tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
162
3.79k
            if (!tns_order)
163
806
                continue;
164
165
2.98k
            exp = tns_decode_coef(tns_order, tns->coef_res[w]+3,
166
2.98k
                tns->coef_compress[w][f], tns->coef[w][f], lpc);
167
168
2.98k
            start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
169
2.98k
            start = min(start, ics->max_sfb);
170
2.98k
            start = min(ics->swb_offset[start], ics->swb_offset_max);
171
172
2.98k
            end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
173
2.98k
            end = min(end, ics->max_sfb);
174
2.98k
            end = min(ics->swb_offset[end], ics->swb_offset_max);
175
176
2.98k
            size = end - start;
177
2.98k
            if (size <= 0)
178
1.69k
                continue;
179
180
1.28k
            if (tns->direction[w][f])
181
454
            {
182
454
                inc = -1;
183
454
                start = end - 1;
184
833
            } else {
185
833
                inc = 1;
186
833
            }
187
188
1.28k
            tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order, exp);
189
1.28k
        }
190
1.62k
    }
191
1.62k
}
192
193
/* Decoder transmitted coefficients for one TNS filter */
194
static uint8_t tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
195
                            uint8_t *coef, real_t *a)
196
21.3k
{
197
21.3k
    uint8_t i, m;
198
21.3k
    real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
199
21.3k
    uint8_t table_index = 2 * (coef_compress != 0) + (coef_res_bits != 3);
200
21.3k
    real_t* tns_coef = all_tns_coefs[table_index];
201
21.3k
    uint8_t exp = 0;
202
203
    /* Conversion to signed integer */
204
130k
    for (i = 0; i < order; i++)
205
108k
        tmp2[i] = tns_coef[coef[i]];
206
207
    /* Conversion to LPC coefficients */
208
21.3k
    a[0] = COEF_CONST(1.0);
209
130k
    for (m = 1; m <= order; m++)
210
108k
    {
211
108k
        a[m] = tmp2[m-1]; /* changed */
212
625k
        for (i = 1; i < m; i++) /* loop only while i<m */
213
516k
            b[i] = a[i] + MUL_C(a[m], a[m-i]);
214
215
625k
        for (i = 1; i < m; i++) /* loop only while i<m */
216
516k
            a[i] = b[i];
217
218
#ifdef FIXED_POINT
219
        a[m] >>= exp;
220
221
        /* OK not to check after the last iteration. */
222
50.2k
        if (m < order)
223
40.5k
        {
224
40.5k
            real_t sum_abs = COEF_CONST(0.0);
225
293k
            for (i = 1; i <= m; ++i)
226
252k
            {
227
252k
                sum_abs += (a[i] >= 0) ? a[i] : -a[i];
228
252k
            }
229
            /* Next iteration would turn sum to 2*sum + 1; maximal "coef" is 7.999 */
230
40.5k
            if (sum_abs >= COEF_CONST(3.5))
231
5.44k
            {
232
5.44k
                exp++;
233
61.2k
                for (i = 1; i <= m; ++i)
234
55.7k
                {
235
55.7k
                    a[i] >>= 1;
236
55.7k
                }
237
5.44k
            }
238
40.5k
        }
239
#endif
240
108k
    }
241
21.3k
    return exp;
242
21.3k
}
tns.c:tns_decode_coef
Line
Count
Source
196
9.65k
{
197
9.65k
    uint8_t i, m;
198
9.65k
    real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
199
9.65k
    uint8_t table_index = 2 * (coef_compress != 0) + (coef_res_bits != 3);
200
9.65k
    real_t* tns_coef = all_tns_coefs[table_index];
201
9.65k
    uint8_t exp = 0;
202
203
    /* Conversion to signed integer */
204
59.8k
    for (i = 0; i < order; i++)
205
50.2k
        tmp2[i] = tns_coef[coef[i]];
206
207
    /* Conversion to LPC coefficients */
208
9.65k
    a[0] = COEF_CONST(1.0);
209
59.8k
    for (m = 1; m <= order; m++)
210
50.2k
    {
211
50.2k
        a[m] = tmp2[m-1]; /* changed */
212
302k
        for (i = 1; i < m; i++) /* loop only while i<m */
213
252k
            b[i] = a[i] + MUL_C(a[m], a[m-i]);
214
215
302k
        for (i = 1; i < m; i++) /* loop only while i<m */
216
252k
            a[i] = b[i];
217
218
50.2k
#ifdef FIXED_POINT
219
50.2k
        a[m] >>= exp;
220
221
        /* OK not to check after the last iteration. */
222
50.2k
        if (m < order)
223
40.5k
        {
224
40.5k
            real_t sum_abs = COEF_CONST(0.0);
225
293k
            for (i = 1; i <= m; ++i)
226
252k
            {
227
252k
                sum_abs += (a[i] >= 0) ? a[i] : -a[i];
228
252k
            }
229
            /* Next iteration would turn sum to 2*sum + 1; maximal "coef" is 7.999 */
230
40.5k
            if (sum_abs >= COEF_CONST(3.5))
231
5.44k
            {
232
5.44k
                exp++;
233
61.2k
                for (i = 1; i <= m; ++i)
234
55.7k
                {
235
55.7k
                    a[i] >>= 1;
236
55.7k
                }
237
5.44k
            }
238
40.5k
        }
239
50.2k
#endif
240
50.2k
    }
241
9.65k
    return exp;
242
9.65k
}
tns.c:tns_decode_coef
Line
Count
Source
196
11.7k
{
197
11.7k
    uint8_t i, m;
198
11.7k
    real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
199
11.7k
    uint8_t table_index = 2 * (coef_compress != 0) + (coef_res_bits != 3);
200
11.7k
    real_t* tns_coef = all_tns_coefs[table_index];
201
11.7k
    uint8_t exp = 0;
202
203
    /* Conversion to signed integer */
204
70.3k
    for (i = 0; i < order; i++)
205
58.6k
        tmp2[i] = tns_coef[coef[i]];
206
207
    /* Conversion to LPC coefficients */
208
11.7k
    a[0] = COEF_CONST(1.0);
209
70.3k
    for (m = 1; m <= order; m++)
210
58.6k
    {
211
58.6k
        a[m] = tmp2[m-1]; /* changed */
212
322k
        for (i = 1; i < m; i++) /* loop only while i<m */
213
263k
            b[i] = a[i] + MUL_C(a[m], a[m-i]);
214
215
322k
        for (i = 1; i < m; i++) /* loop only while i<m */
216
263k
            a[i] = b[i];
217
218
#ifdef FIXED_POINT
219
        a[m] >>= exp;
220
221
        /* OK not to check after the last iteration. */
222
        if (m < order)
223
        {
224
            real_t sum_abs = COEF_CONST(0.0);
225
            for (i = 1; i <= m; ++i)
226
            {
227
                sum_abs += (a[i] >= 0) ? a[i] : -a[i];
228
            }
229
            /* Next iteration would turn sum to 2*sum + 1; maximal "coef" is 7.999 */
230
            if (sum_abs >= COEF_CONST(3.5))
231
            {
232
                exp++;
233
                for (i = 1; i <= m; ++i)
234
                {
235
                    a[i] >>= 1;
236
                }
237
            }
238
        }
239
#endif
240
58.6k
    }
241
11.7k
    return exp;
242
11.7k
}
243
244
static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
245
                          uint8_t order, uint8_t exp)
246
2.74k
{
247
    /*
248
     - Simple all-pole filter of order "order" defined by
249
       y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
250
     - The state variables of the filter are initialized to zero every time
251
     - The output data is written over the input data ("in-place operation")
252
     - An input vector of "size" samples is processed and the index increment
253
       to the next data sample is given by "inc"
254
    */
255
256
2.74k
    uint8_t j;
257
2.74k
    uint16_t i;
258
    /* state is stored as a double ringbuffer */
259
2.74k
    real_t state[2*TNS_MAX_ORDER] = {0};
260
2.74k
    int8_t state_index = 0;
261
2.74k
    int32_t mul = 1;
262
263
#ifdef FIXED_POINT
264
1.12k
    if (exp >= 4)
265
262
        return;
266
867
    mul = 1 << exp;
267
#else
268
    (void)exp;
269
#endif
270
271
164k
    for (i = 0; i < size; i++)
272
161k
    {
273
161k
        real_t y = REAL_CONST(0.0);
274
1.56M
        for (j = 0; j < order; j++)
275
1.40M
            y += MUL_C(state[state_index+j], lpc[j+1]);
276
161k
        y = *spectrum - (y * mul);
277
278
        /* double ringbuffer state */
279
161k
        state_index--;
280
161k
        if (state_index < 0)
281
44.7k
            state_index = order-1;
282
161k
        state[state_index] = state[state_index + order] = y;
283
284
161k
        *spectrum = y;
285
161k
        spectrum += inc;
286
287
//#define TNS_PRINT
288
#ifdef TNS_PRINT
289
        //printf("%d\n", y);
290
        printf("0x%.8X\n", y);
291
#endif
292
161k
    }
293
867
}
tns.c:tns_ar_filter
Line
Count
Source
246
1.12k
{
247
    /*
248
     - Simple all-pole filter of order "order" defined by
249
       y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
250
     - The state variables of the filter are initialized to zero every time
251
     - The output data is written over the input data ("in-place operation")
252
     - An input vector of "size" samples is processed and the index increment
253
       to the next data sample is given by "inc"
254
    */
255
256
1.12k
    uint8_t j;
257
1.12k
    uint16_t i;
258
    /* state is stored as a double ringbuffer */
259
1.12k
    real_t state[2*TNS_MAX_ORDER] = {0};
260
1.12k
    int8_t state_index = 0;
261
1.12k
    int32_t mul = 1;
262
263
1.12k
#ifdef FIXED_POINT
264
1.12k
    if (exp >= 4)
265
262
        return;
266
867
    mul = 1 << exp;
267
#else
268
    (void)exp;
269
#endif
270
271
53.8k
    for (i = 0; i < size; i++)
272
52.9k
    {
273
52.9k
        real_t y = REAL_CONST(0.0);
274
579k
        for (j = 0; j < order; j++)
275
526k
            y += MUL_C(state[state_index+j], lpc[j+1]);
276
52.9k
        y = *spectrum - (y * mul);
277
278
        /* double ringbuffer state */
279
52.9k
        state_index--;
280
52.9k
        if (state_index < 0)
281
13.4k
            state_index = order-1;
282
52.9k
        state[state_index] = state[state_index + order] = y;
283
284
52.9k
        *spectrum = y;
285
52.9k
        spectrum += inc;
286
287
//#define TNS_PRINT
288
#ifdef TNS_PRINT
289
        //printf("%d\n", y);
290
        printf("0x%.8X\n", y);
291
#endif
292
52.9k
    }
293
867
}
tns.c:tns_ar_filter
Line
Count
Source
246
1.61k
{
247
    /*
248
     - Simple all-pole filter of order "order" defined by
249
       y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
250
     - The state variables of the filter are initialized to zero every time
251
     - The output data is written over the input data ("in-place operation")
252
     - An input vector of "size" samples is processed and the index increment
253
       to the next data sample is given by "inc"
254
    */
255
256
1.61k
    uint8_t j;
257
1.61k
    uint16_t i;
258
    /* state is stored as a double ringbuffer */
259
1.61k
    real_t state[2*TNS_MAX_ORDER] = {0};
260
1.61k
    int8_t state_index = 0;
261
1.61k
    int32_t mul = 1;
262
263
#ifdef FIXED_POINT
264
    if (exp >= 4)
265
        return;
266
    mul = 1 << exp;
267
#else
268
1.61k
    (void)exp;
269
1.61k
#endif
270
271
110k
    for (i = 0; i < size; i++)
272
108k
    {
273
108k
        real_t y = REAL_CONST(0.0);
274
986k
        for (j = 0; j < order; j++)
275
877k
            y += MUL_C(state[state_index+j], lpc[j+1]);
276
108k
        y = *spectrum - (y * mul);
277
278
        /* double ringbuffer state */
279
108k
        state_index--;
280
108k
        if (state_index < 0)
281
31.2k
            state_index = order-1;
282
108k
        state[state_index] = state[state_index + order] = y;
283
284
108k
        *spectrum = y;
285
108k
        spectrum += inc;
286
287
//#define TNS_PRINT
288
#ifdef TNS_PRINT
289
        //printf("%d\n", y);
290
        printf("0x%.8X\n", y);
291
#endif
292
108k
    }
293
1.61k
}
294
295
static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
296
                          uint8_t order, uint8_t exp)
297
1.28k
{
298
    /*
299
     - Simple all-zero filter of order "order" defined by
300
       y(n) =  x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
301
     - The state variables of the filter are initialized to zero every time
302
     - The output data is written over the input data ("in-place operation")
303
     - An input vector of "size" samples is processed and the index increment
304
       to the next data sample is given by "inc"
305
    */
306
307
1.28k
    uint8_t j;
308
1.28k
    uint16_t i;
309
    /* state is stored as a double ringbuffer */
310
1.28k
    real_t state[2*TNS_MAX_ORDER] = {0};
311
1.28k
    int8_t state_index = 0;
312
1.28k
    int32_t mul = 1;
313
314
#ifdef FIXED_POINT
315
438
    if (exp >= 4)
316
81
        return;
317
357
    mul = 1 << exp;
318
#else
319
    (void)exp;
320
#endif
321
322
31.1k
    for (i = 0; i < size; i++)
323
29.9k
    {
324
29.9k
        real_t y = REAL_CONST(0.0);
325
223k
        for (j = 0; j < order; j++)
326
193k
            y += MUL_C(state[state_index+j], lpc[j+1]);
327
328
29.9k
        y = *spectrum + (y * mul);
329
330
        /* double ringbuffer state */
331
29.9k
        state_index--;
332
29.9k
        if (state_index < 0)
333
12.8k
            state_index = order-1;
334
29.9k
        state[state_index] = state[state_index + order] = *spectrum;
335
336
29.9k
        *spectrum = y;
337
29.9k
        spectrum += inc;
338
29.9k
    }
339
357
}
tns.c:tns_ma_filter
Line
Count
Source
297
438
{
298
    /*
299
     - Simple all-zero filter of order "order" defined by
300
       y(n) =  x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
301
     - The state variables of the filter are initialized to zero every time
302
     - The output data is written over the input data ("in-place operation")
303
     - An input vector of "size" samples is processed and the index increment
304
       to the next data sample is given by "inc"
305
    */
306
307
438
    uint8_t j;
308
438
    uint16_t i;
309
    /* state is stored as a double ringbuffer */
310
438
    real_t state[2*TNS_MAX_ORDER] = {0};
311
438
    int8_t state_index = 0;
312
438
    int32_t mul = 1;
313
314
438
#ifdef FIXED_POINT
315
438
    if (exp >= 4)
316
81
        return;
317
357
    mul = 1 << exp;
318
#else
319
    (void)exp;
320
#endif
321
322
4.86k
    for (i = 0; i < size; i++)
323
4.50k
    {
324
4.50k
        real_t y = REAL_CONST(0.0);
325
33.0k
        for (j = 0; j < order; j++)
326
28.5k
            y += MUL_C(state[state_index+j], lpc[j+1]);
327
328
4.50k
        y = *spectrum + (y * mul);
329
330
        /* double ringbuffer state */
331
4.50k
        state_index--;
332
4.50k
        if (state_index < 0)
333
2.27k
            state_index = order-1;
334
4.50k
        state[state_index] = state[state_index + order] = *spectrum;
335
336
4.50k
        *spectrum = y;
337
4.50k
        spectrum += inc;
338
4.50k
    }
339
357
}
tns.c:tns_ma_filter
Line
Count
Source
297
849
{
298
    /*
299
     - Simple all-zero filter of order "order" defined by
300
       y(n) =  x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
301
     - The state variables of the filter are initialized to zero every time
302
     - The output data is written over the input data ("in-place operation")
303
     - An input vector of "size" samples is processed and the index increment
304
       to the next data sample is given by "inc"
305
    */
306
307
849
    uint8_t j;
308
849
    uint16_t i;
309
    /* state is stored as a double ringbuffer */
310
849
    real_t state[2*TNS_MAX_ORDER] = {0};
311
849
    int8_t state_index = 0;
312
849
    int32_t mul = 1;
313
314
#ifdef FIXED_POINT
315
    if (exp >= 4)
316
        return;
317
    mul = 1 << exp;
318
#else
319
849
    (void)exp;
320
849
#endif
321
322
26.2k
    for (i = 0; i < size; i++)
323
25.4k
    {
324
25.4k
        real_t y = REAL_CONST(0.0);
325
190k
        for (j = 0; j < order; j++)
326
165k
            y += MUL_C(state[state_index+j], lpc[j+1]);
327
328
25.4k
        y = *spectrum + (y * mul);
329
330
        /* double ringbuffer state */
331
25.4k
        state_index--;
332
25.4k
        if (state_index < 0)
333
10.6k
            state_index = order-1;
334
25.4k
        state[state_index] = state[state_index + order] = *spectrum;
335
336
25.4k
        *spectrum = y;
337
25.4k
        spectrum += inc;
338
25.4k
    }
339
849
}