Coverage Report

Created: 2025-08-28 07:12

/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
6.95M
{
35
6.95M
    SoftFloat ret;
36
6.95M
    uint64_t accu = 0, round;
37
6.95M
    uint64_t accu0 = 0, accu1 = 0, accu2 = 0, accu3 = 0;
38
6.95M
    int i, nz, nz0;
39
6.95M
    unsigned u;
40
41
6.95M
    nz = 0;
42
63.1M
    for (i = 0; i < n; i += 2) {
43
56.2M
        accu0 += (int64_t)x[i + 0][0] * x[i + 0][0];
44
56.2M
        accu1 += (int64_t)x[i + 0][1] * x[i + 0][1];
45
56.2M
        accu2 += (int64_t)x[i + 1][0] * x[i + 1][0];
46
56.2M
        accu3 += (int64_t)x[i + 1][1] * x[i + 1][1];
47
56.2M
        if ((accu0|accu1|accu2|accu3) > UINT64_MAX - INT32_MIN*(int64_t)INT32_MIN || i+2>=n) {
48
6.96M
            accu0 >>= nz;
49
6.96M
            accu1 >>= nz;
50
6.96M
            accu2 >>= nz;
51
6.96M
            accu3 >>= nz;
52
6.99M
            while ((accu0|accu1|accu2|accu3) > (UINT64_MAX - accu) >> 2) {
53
29.1k
                accu0 >>= 1;
54
29.1k
                accu1 >>= 1;
55
29.1k
                accu2 >>= 1;
56
29.1k
                accu3 >>= 1;
57
29.1k
                accu  >>= 1;
58
29.1k
                nz ++;
59
29.1k
            }
60
6.96M
            accu += accu0 + accu1 + accu2 + accu3;
61
6.96M
            accu0 = accu1 = accu2 = accu3 = 0;
62
6.96M
        }
63
56.2M
    }
64
65
6.95M
    nz0 = 15 - nz;
66
67
6.95M
    u = accu >> 32;
68
6.95M
    if (u) {
69
2.23M
        nz = 33;
70
28.6M
        while (u < 0x80000000U) {
71
26.4M
            u <<= 1;
72
26.4M
            nz--;
73
26.4M
        }
74
2.23M
    } else
75
4.71M
        nz = 1;
76
77
6.95M
    round = 1ULL << (nz-1);
78
6.95M
    u = ((accu + round) >> nz);
79
6.95M
    u >>= 1;
80
6.95M
    ret = av_int2sf(u, nz0 - nz);
81
82
6.95M
    return ret;
83
6.95M
}
84
85
static void sbr_neg_odd_64_c(int *x)
86
16.4M
{
87
16.4M
    int i;
88
542M
    for (i = 1; i < 64; i += 2)
89
525M
        x[i] = -(unsigned)x[i];
90
16.4M
}
91
92
static void sbr_qmf_pre_shuffle_c(int *z)
93
13.7M
{
94
13.7M
    int k;
95
13.7M
    z[64] = z[0];
96
13.7M
    z[65] = z[1];
97
441M
    for (k = 1; k < 32; k++) {
98
427M
        z[64+2*k  ] = -z[64 - k];
99
427M
        z[64+2*k+1] =  z[ k + 1];
100
427M
    }
101
13.7M
}
102
103
static void sbr_qmf_post_shuffle_c(int W[32][2], const int *z)
104
13.7M
{
105
13.7M
    int k;
106
455M
    for (k = 0; k < 32; k++) {
107
441M
        W[k][0] = -z[63-k];
108
441M
        W[k][1] = z[k];
109
441M
    }
110
13.7M
}
111
112
static void sbr_qmf_deint_neg_c(int *v, const int *src)
113
2.79M
{
114
2.79M
    int i;
115
92.2M
    for (i = 0; i < 32; i++) {
116
89.4M
        v[     i] = (int)(0x10U + src[63 - 2*i    ]) >> 5;
117
89.4M
        v[63 - i] = (int)(0x10U - src[63 - 2*i - 1]) >> 5;
118
89.4M
    }
119
2.79M
}
120
121
static av_always_inline SoftFloat autocorr_calc(int64_t accu)
122
21.9M
{
123
21.9M
        int nz, mant, expo;
124
21.9M
        unsigned round;
125
21.9M
        int i = (int)(accu >> 32);
126
21.9M
        if (i == 0) {
127
14.5M
            nz = 1;
128
14.5M
        } else {
129
7.37M
            nz = 0;
130
105M
            while (FFABS(i) < 0x40000000) {
131
98.4M
                i *= 2;
132
98.4M
                nz++;
133
98.4M
            }
134
7.37M
            nz = 32-nz;
135
7.37M
        }
136
137
21.9M
        round = 1U << (nz-1);
138
21.9M
        mant = (int)((accu + round) >> nz);
139
21.9M
        mant = (mant + 0x40LL)>>7;
140
21.9M
        mant *= 64;
141
21.9M
        expo = nz + 15;
142
21.9M
        return av_int2sf(mant, 30 - expo);
143
21.9M
}
144
145
static av_always_inline void autocorrelate(const int x[40][2], SoftFloat phi[3][2][2], int lag)
146
8.23M
{
147
8.23M
    int i;
148
8.23M
    int64_t real_sum, imag_sum;
149
8.23M
    int64_t accu_re = 0, accu_im = 0;
150
151
8.23M
    if (lag) {
152
208M
        for (i = 1; i < 38; i++) {
153
203M
            accu_re += (uint64_t)x[i][0] * x[i+lag][0];
154
203M
            accu_re += (uint64_t)x[i][1] * x[i+lag][1];
155
203M
            accu_im += (uint64_t)x[i][0] * x[i+lag][1];
156
203M
            accu_im -= (uint64_t)x[i][1] * x[i+lag][0];
157
203M
        }
158
159
5.49M
        real_sum = accu_re;
160
5.49M
        imag_sum = accu_im;
161
162
5.49M
        accu_re += (uint64_t)x[ 0][0] * x[lag][0];
163
5.49M
        accu_re += (uint64_t)x[ 0][1] * x[lag][1];
164
5.49M
        accu_im += (uint64_t)x[ 0][0] * x[lag][1];
165
5.49M
        accu_im -= (uint64_t)x[ 0][1] * x[lag][0];
166
167
5.49M
        phi[2-lag][1][0] = autocorr_calc(accu_re);
168
5.49M
        phi[2-lag][1][1] = autocorr_calc(accu_im);
169
170
5.49M
        if (lag == 1) {
171
2.74M
            accu_re = real_sum;
172
2.74M
            accu_im = imag_sum;
173
2.74M
            accu_re += (uint64_t)x[38][0] * x[39][0];
174
2.74M
            accu_re += (uint64_t)x[38][1] * x[39][1];
175
2.74M
            accu_im += (uint64_t)x[38][0] * x[39][1];
176
2.74M
            accu_im -= (uint64_t)x[38][1] * x[39][0];
177
178
2.74M
            phi[0][0][0] = autocorr_calc(accu_re);
179
2.74M
            phi[0][0][1] = autocorr_calc(accu_im);
180
2.74M
        }
181
5.49M
    } else {
182
104M
        for (i = 1; i < 38; i++) {
183
101M
            accu_re += (uint64_t)x[i][0] * x[i][0];
184
101M
            accu_re += (uint64_t)x[i][1] * x[i][1];
185
101M
        }
186
2.74M
        real_sum = accu_re;
187
2.74M
        accu_re += (uint64_t)x[ 0][0] * x[ 0][0];
188
2.74M
        accu_re += (uint64_t)x[ 0][1] * x[ 0][1];
189
190
2.74M
        phi[2][1][0] = autocorr_calc(accu_re);
191
192
2.74M
        accu_re = real_sum;
193
2.74M
        accu_re += (uint64_t)x[38][0] * x[38][0];
194
2.74M
        accu_re += (uint64_t)x[38][1] * x[38][1];
195
196
2.74M
        phi[1][0][0] = autocorr_calc(accu_re);
197
2.74M
    }
198
8.23M
}
199
200
static void sbr_autocorrelate_c(const int x[40][2], SoftFloat phi[3][2][2])
201
2.74M
{
202
2.74M
    autocorrelate(x, phi, 0);
203
2.74M
    autocorrelate(x, phi, 1);
204
2.74M
    autocorrelate(x, phi, 2);
205
2.74M
}
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.38M
{
211
3.38M
    int alpha[4];
212
3.38M
    int i;
213
3.38M
    int64_t accu;
214
215
3.38M
    accu = (int64_t)alpha0[0] * bw;
216
3.38M
    alpha[2] = (int)((accu + 0x40000000) >> 31);
217
3.38M
    accu = (int64_t)alpha0[1] * bw;
218
3.38M
    alpha[3] = (int)((accu + 0x40000000) >> 31);
219
3.38M
    accu = (int64_t)bw * bw;
220
3.38M
    bw = (int)((accu + 0x40000000) >> 31);
221
3.38M
    accu = (int64_t)alpha1[0] * bw;
222
3.38M
    alpha[0] = (int)((accu + 0x40000000) >> 31);
223
3.38M
    accu = (int64_t)alpha1[1] * bw;
224
3.38M
    alpha[1] = (int)((accu + 0x40000000) >> 31);
225
226
115M
    for (i = start; i < end; i++) {
227
112M
        accu  = (int64_t)X_low[i][0] * 0x20000000;
228
112M
        accu += (int64_t)X_low[i - 2][0] * alpha[0];
229
112M
        accu -= (int64_t)X_low[i - 2][1] * alpha[1];
230
112M
        accu += (int64_t)X_low[i - 1][0] * alpha[2];
231
112M
        accu -= (int64_t)X_low[i - 1][1] * alpha[3];
232
112M
        X_high[i][0] = (int)((accu + 0x10000000) >> 29);
233
234
112M
        accu  = (int64_t)X_low[i][1] * 0x20000000;
235
112M
        accu += (int64_t)X_low[i - 2][1] * alpha[0];
236
112M
        accu += (int64_t)X_low[i - 2][0] * alpha[1];
237
112M
        accu += (int64_t)X_low[i - 1][1] * alpha[2];
238
112M
        accu += (int64_t)X_low[i - 1][0] * alpha[3];
239
112M
        X_high[i][1] = (int)((accu + 0x10000000) >> 29);
240
112M
    }
241
3.38M
}
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
4.78M
{
246
4.78M
    int m;
247
4.78M
    int64_t accu;
248
249
114M
    for (m = 0; m < m_max; m++) {
250
109M
        if (22 - g_filt[m].exp < 61) {
251
109M
            int64_t r = 1LL << (22-g_filt[m].exp);
252
109M
            accu = (int64_t)X_high[m][ixh][0] * ((g_filt[m].mant + 0x40)>>7);
253
109M
            Y[m][0] = (int)((accu + r) >> (23-g_filt[m].exp));
254
255
109M
            accu = (int64_t)X_high[m][ixh][1] * ((g_filt[m].mant + 0x40)>>7);
256
109M
            Y[m][1] = (int)((accu + r) >> (23-g_filt[m].exp));
257
109M
        }
258
109M
    }
259
4.78M
}
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.60M
{
269
4.60M
    int m;
270
271
107M
    for (m = 0; m < m_max; m++) {
272
102M
        unsigned y0 = Y[m][0];
273
102M
        unsigned y1 = Y[m][1];
274
102M
        noise = (noise + 1) & 0x1ff;
275
102M
        if (s_m[m].mant) {
276
1.74M
            int shift, round;
277
278
1.74M
            shift = 22 - s_m[m].exp;
279
1.74M
            if (shift < 1) {
280
113k
                av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
281
113k
                return AVERROR(ERANGE);
282
1.63M
            } else if (shift < 30) {
283
1.51M
                round = 1 << (shift-1);
284
1.51M
                y0 += (s_m[m].mant * phi_sign0 + round) >> shift;
285
1.51M
                y1 += (s_m[m].mant * phi_sign1 + round) >> shift;
286
1.51M
            }
287
101M
        } else {
288
101M
            int shift, round, tmp;
289
101M
            int64_t accu;
290
291
101M
            shift = 22 - q_filt[m].exp;
292
101M
            if (shift < 1) {
293
141k
                av_log(NULL, AV_LOG_ERROR, "Overflow in sbr_hf_apply_noise, shift=%d\n", shift);
294
141k
                return AVERROR(ERANGE);
295
101M
            } else if (shift < 30) {
296
93.6M
                round = 1 << (shift-1);
297
298
93.6M
                accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][0];
299
93.6M
                tmp = (int)((accu + 0x40000000) >> 31);
300
93.6M
                y0 += (tmp + round) >> shift;
301
302
93.6M
                accu = (int64_t)q_filt[m].mant * ff_sbr_noise_table_fixed[noise][1];
303
93.6M
                tmp = (int)((accu + 0x40000000) >> 31);
304
93.6M
                y1 += (tmp + round) >> shift;
305
93.6M
            }
306
101M
        }
307
102M
        Y[m][0] = y0;
308
102M
        Y[m][1] = y1;
309
102M
        phi_sign1 = -phi_sign1;
310
102M
    }
311
4.34M
    return 0;
312
4.60M
}
313
314
#include "sbrdsp_template.c"