Coverage Report

Created: 2026-01-16 07:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/sbrdsp_fixed.c
Line
Count
Source
1
/*
2
 * AAC Spectral Band Replication decoding functions
3
 * Copyright (c) 2008-2009 Robert Swain ( rob opendot cl )
4
 * Copyright (c) 2009-2010 Alex Converse <alex.converse@gmail.com>
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * Note: Rounding-to-nearest used unless otherwise stated
23
 *
24
 */
25
26
#define USE_FIXED 1
27
28
#include "aac.h"
29
#include "libavutil/attributes.h"
30
#include "libavutil/intfloat.h"
31
#include "sbrdsp.h"
32
33
static SoftFloat sbr_sum_square_c(int (*x)[2], int n)
34
7.70M
{
35
7.70M
    SoftFloat ret;
36
7.70M
    uint64_t accu = 0, round;
37
7.70M
    uint64_t accu0 = 0, accu1 = 0, accu2 = 0, accu3 = 0;
38
7.70M
    int i, nz, nz0;
39
7.70M
    unsigned u;
40
41
7.70M
    nz = 0;
42
67.4M
    for (i = 0; i < n; i += 2) {
43
59.7M
        accu0 += (int64_t)x[i + 0][0] * x[i + 0][0];
44
59.7M
        accu1 += (int64_t)x[i + 0][1] * x[i + 0][1];
45
59.7M
        accu2 += (int64_t)x[i + 1][0] * x[i + 1][0];
46
59.7M
        accu3 += (int64_t)x[i + 1][1] * x[i + 1][1];
47
59.7M
        if ((accu0|accu1|accu2|accu3) > UINT64_MAX - INT32_MIN*(int64_t)INT32_MIN || i+2>=n) {
48
7.71M
            accu0 >>= nz;
49
7.71M
            accu1 >>= nz;
50
7.71M
            accu2 >>= nz;
51
7.71M
            accu3 >>= nz;
52
7.74M
            while ((accu0|accu1|accu2|accu3) > (UINT64_MAX - accu) >> 2) {
53
30.3k
                accu0 >>= 1;
54
30.3k
                accu1 >>= 1;
55
30.3k
                accu2 >>= 1;
56
30.3k
                accu3 >>= 1;
57
30.3k
                accu  >>= 1;
58
30.3k
                nz ++;
59
30.3k
            }
60
7.71M
            accu += accu0 + accu1 + accu2 + accu3;
61
7.71M
            accu0 = accu1 = accu2 = accu3 = 0;
62
7.71M
        }
63
59.7M
    }
64
65
7.70M
    nz0 = 15 - nz;
66
67
7.70M
    u = accu >> 32;
68
7.70M
    if (u) {
69
2.72M
        nz = 33;
70
36.0M
        while (u < 0x80000000U) {
71
33.2M
            u <<= 1;
72
33.2M
            nz--;
73
33.2M
        }
74
2.72M
    } else
75
4.97M
        nz = 1;
76
77
7.70M
    round = 1ULL << (nz-1);
78
7.70M
    u = ((accu + round) >> nz);
79
7.70M
    u >>= 1;
80
7.70M
    ret = av_int2sf(u, nz0 - nz);
81
82
7.70M
    return ret;
83
7.70M
}
84
85
static void sbr_neg_odd_64_c(int *x)
86
14.7M
{
87
14.7M
    int i;
88
486M
    for (i = 1; i < 64; i += 2)
89
471M
        x[i] = -(unsigned)x[i];
90
14.7M
}
91
92
static void sbr_qmf_pre_shuffle_c(int *z)
93
13.3M
{
94
13.3M
    int k;
95
13.3M
    z[64] = z[0];
96
13.3M
    z[65] = z[1];
97
426M
    for (k = 1; k < 32; k++) {
98
413M
        z[64+2*k  ] = -z[64 - k];
99
413M
        z[64+2*k+1] =  z[ k + 1];
100
413M
    }
101
13.3M
}
102
103
static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
104
13.3M
{
105
13.3M
    int k;
106
440M
    for (k = 0; k < 32; k++) {
107
426M
        W[k][0] = -z[63-k];
108
426M
        W[k][1] = z[k];
109
426M
    }
110
13.3M
}
111
112
static void sbr_qmf_deint_neg_c(int *v, const int *src)
113
2.81M
{
114
2.81M
    int i;
115
93.0M
    for (i = 0; i < 32; i++) {
116
90.2M
        v[     i] = (int)(0x10U + src[63 - 2*i    ]) >> 5;
117
90.2M
        v[63 - i] = (int)(0x10U - src[63 - 2*i - 1]) >> 5;
118
90.2M
    }
119
2.81M
}
120
121
static av_always_inline SoftFloat autocorr_calc(int64_t accu)
122
23.8M
{
123
23.8M
        int nz, mant, expo;
124
23.8M
        unsigned round;
125
23.8M
        int i = (int)(accu >> 32);
126
23.8M
        if (i == 0) {
127
14.7M
            nz = 1;
128
14.7M
        } else {
129
9.07M
            nz = 0;
130
128M
            while (FFABS(i) < 0x40000000) {
131
119M
                i *= 2;
132
119M
                nz++;
133
119M
            }
134
9.07M
            nz = 32-nz;
135
9.07M
        }
136
137
23.8M
        round = 1U << (nz-1);
138
23.8M
        mant = (int)((accu + round) >> nz);
139
23.8M
        mant = (mant + 0x40LL)>>7;
140
23.8M
        mant *= 64;
141
23.8M
        expo = nz + 15;
142
23.8M
        return av_int2sf(mant, 30 - expo);
143
23.8M
}
144
145
static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
146
8.93M
{
147
8.93M
    int i;
148
8.93M
    int64_t real_sum, imag_sum;
149
8.93M
    int64_t accu_re = 0, accu_im = 0;
150
151
8.93M
    if (lag) {
152
226M
        for (i = 1; i < 38; i++) {
153
220M
            accu_re += (uint64_t)x[i][0] * x[i+lag][0];
154
220M
            accu_re += (uint64_t)x[i][1] * x[i+lag][1];
155
220M
            accu_im += (uint64_t)x[i][0] * x[i+lag][1];
156
220M
            accu_im -= (uint64_t)x[i][1] * x[i+lag][0];
157
220M
        }
158
159
5.95M
        real_sum = accu_re;
160
5.95M
        imag_sum = accu_im;
161
162
5.95M
        accu_re += (uint64_t)x[ 0][0] * x[lag][0];
163
5.95M
        accu_re += (uint64_t)x[ 0][1] * x[lag][1];
164
5.95M
        accu_im += (uint64_t)x[ 0][0] * x[lag][1];
165
5.95M
        accu_im -= (uint64_t)x[ 0][1] * x[lag][0];
166
167
5.95M
        phi[2-lag][1][0] = autocorr_calc(accu_re);
168
5.95M
        phi[2-lag][1][1] = autocorr_calc(accu_im);
169
170
5.95M
        if (lag == 1) {
171
2.97M
            accu_re = real_sum;
172
2.97M
            accu_im = imag_sum;
173
2.97M
            accu_re += (uint64_t)x[38][0] * x[39][0];
174
2.97M
            accu_re += (uint64_t)x[38][1] * x[39][1];
175
2.97M
            accu_im += (uint64_t)x[38][0] * x[39][1];
176
2.97M
            accu_im -= (uint64_t)x[38][1] * x[39][0];
177
178
2.97M
            phi[0][0][0] = autocorr_calc(accu_re);
179
2.97M
            phi[0][0][1] = autocorr_calc(accu_im);
180
2.97M
        }
181
5.95M
    } else {
182
113M
        for (i = 1; i < 38; i++) {
183
110M
            accu_re += (uint64_t)x[i][0] * x[i][0];
184
110M
            accu_re += (uint64_t)x[i][1] * x[i][1];
185
110M
        }
186
2.97M
        real_sum = accu_re;
187
2.97M
        accu_re += (uint64_t)x[ 0][0] * x[ 0][0];
188
2.97M
        accu_re += (uint64_t)x[ 0][1] * x[ 0][1];
189
190
2.97M
        phi[2][1][0] = autocorr_calc(accu_re);
191
192
2.97M
        accu_re = real_sum;
193
2.97M
        accu_re += (uint64_t)x[38][0] * x[38][0];
194
2.97M
        accu_re += (uint64_t)x[38][1] * x[38][1];
195
196
2.97M
        phi[1][0][0] = autocorr_calc(accu_re);
197
2.97M
    }
198
8.93M
}
199
200
static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
201
2.97M
{
202
2.97M
    autocorrelate(x, phi, 0);
203
2.97M
    autocorrelate(x, phi, 1);
204
2.97M
    autocorrelate(x, phi, 2);
205
2.97M
}
206
207
static void sbr_hf_gen_c(int (*X_high)[2], const int (*X_low)[2],
208
                       const int alpha0[2], const int alpha1[2],
209
                       int bw, int start, int end)
210
3.58M
{
211
3.58M
    int alpha[4];
212
3.58M
    int i;
213
3.58M
    int64_t accu;
214
215
3.58M
    accu = (int64_t)alpha0[0] * bw;
216
3.58M
    alpha[2] = (int)((accu + 0x40000000) >> 31);
217
3.58M
    accu = (int64_t)alpha0[1] * bw;
218
3.58M
    alpha[3] = (int)((accu + 0x40000000) >> 31);
219
3.58M
    accu = (int64_t)bw * bw;
220
3.58M
    bw = (int)((accu + 0x40000000) >> 31);
221
3.58M
    accu = (int64_t)alpha1[0] * bw;
222
3.58M
    alpha[0] = (int)((accu + 0x40000000) >> 31);
223
3.58M
    accu = (int64_t)alpha1[1] * bw;
224
3.58M
    alpha[1] = (int)((accu + 0x40000000) >> 31);
225
226
123M
    for (i = start; i < end; i++) {
227
119M
        accu  = (int64_t)X_low[i][0] * 0x20000000;
228
119M
        accu += (int64_t)X_low[i - 2][0] * alpha[0];
229
119M
        accu -= (int64_t)X_low[i - 2][1] * alpha[1];
230
119M
        accu += (int64_t)X_low[i - 1][0] * alpha[2];
231
119M
        accu -= (int64_t)X_low[i - 1][1] * alpha[3];
232
119M
        X_high[i][0] = (int)((accu + 0x10000000) >> 29);
233
234
119M
        accu  = (int64_t)X_low[i][1] * 0x20000000;
235
119M
        accu += (int64_t)X_low[i - 2][1] * alpha[0];
236
119M
        accu += (int64_t)X_low[i - 2][0] * alpha[1];
237
119M
        accu += (int64_t)X_low[i - 1][1] * alpha[2];
238
119M
        accu += (int64_t)X_low[i - 1][0] * alpha[3];
239
119M
        X_high[i][1] = (int)((accu + 0x10000000) >> 29);
240
119M
    }
241
3.58M
}
242
243
static void sbr_hf_g_filt_c(int (*Y)[2], const int (*X_high)[40][2],
244
                          const SoftFloat *g_filt, int m_max, intptr_t ixh)
245
5.15M
{
246
5.15M
    int m;
247
5.15M
    int64_t accu;
248
249
121M
    for (m = 0; m < m_max; m++) {
250
116M
        if (22 - g_filt[m].exp < 61) {
251
116M
            int64_t r = 1LL << (22-g_filt[m].exp);
252
116M
            accu = (int64_t)X_high[m][ixh][0] * ((g_filt[m].mant + 0x40)>>7);
253
116M
            Y[m][0] = (int)((accu + r) >> (23-g_filt[m].exp));
254
255
116M
            accu = (int64_t)X_high[m][ixh][1] * ((g_filt[m].mant + 0x40)>>7);
256
116M
            Y[m][1] = (int)((accu + r) >> (23-g_filt[m].exp));
257
116M
        }
258
116M
    }
259
5.15M
}
260
261
static av_always_inline int sbr_hf_apply_noise(int (*Y)[2],
262
                                                const SoftFloat *s_m,
263
                                                const SoftFloat *q_filt,
264
                                                int noise,
265
                                                int phi_sign0,
266
                                                int phi_sign1,
267
                                                int m_max)
268
4.93M
{
269
4.93M
    int m;
270
271
113M
    for (m = 0; m < m_max; m++) {
272
108M
        unsigned y0 = Y[m][0];
273
108M
        unsigned y1 = Y[m][1];
274
108M
        noise = (noise + 1) & 0x1ff;
275
108M
        if (s_m[m].mant) {
276
1.70M
            int shift, round;
277
278
1.70M
            shift = 22 - s_m[m].exp;
279
1.70M
            if (shift < 1) {
280
114k
                av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
281
114k
                return AVERROR(ERANGE);
282
1.59M
            } else if (shift < 30) {
283
1.47M
                round = 1 << (shift-1);
284
1.47M
                y0 += (s_m[m].mant * phi_sign0 + round) >> shift;
285
1.47M
                y1 += (s_m[m].mant * phi_sign1 + round) >> shift;
286
1.47M
            }
287
106M
        } else {
288
106M
            int shift, round, tmp;
289
106M
            int64_t accu;
290
291
106M
            shift = 22 - q_filt[m].exp;
292
106M
            if (shift < 1) {
293
174k
                av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
294
174k
                return AVERROR(ERANGE);
295
106M
            } else if (shift < 30) {
296
98.5M
                round = 1 << (shift-1);
297
298
98.5M
                accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][0];
299
98.5M
                tmp = (int)((accu + 0x40000000) >> 31);
300
98.5M
                y0 += (tmp + round) >> shift;
301
302
98.5M
                accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][1];
303
98.5M
                tmp = (int)((accu + 0x40000000) >> 31);
304
98.5M
                y1 += (tmp + round) >> shift;
305
98.5M
            }
306
106M
        }
307
108M
        Y[m][0] = y0;
308
108M
        Y[m][1] = y1;
309
108M
        phi_sign1 = -phi_sign1;
310
108M
    }
311
4.64M
    return 0;
312
4.93M
}
313
314
#include "sbrdsp_template.c"