Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/sbcdsp.c
Line
Count
Source
1
/*
2
 * Bluetooth low-complexity, subband codec (SBC)
3
 *
4
 * Copyright (C) 2017  Aurelien Jacobs <aurel@gnuage.org>
5
 * Copyright (C) 2012-2013  Intel Corporation
6
 * Copyright (C) 2008-2010  Nokia Corporation
7
 * Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
8
 * Copyright (C) 2004-2005  Henryk Ploetz <henryk@ploetzli.ch>
9
 * Copyright (C) 2005-2006  Brad Midgley <bmidgley@xmission.com>
10
 *
11
 * This file is part of FFmpeg.
12
 *
13
 * FFmpeg is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU Lesser General Public
15
 * License as published by the Free Software Foundation; either
16
 * version 2.1 of the License, or (at your option) any later version.
17
 *
18
 * FFmpeg is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 * Lesser General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public
24
 * License along with FFmpeg; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26
 */
27
28
/**
29
 * @file
30
 * SBC basic "building bricks"
31
 */
32
33
#include <stdint.h>
34
#include <limits.h>
35
#include <string.h>
36
#include "libavutil/common.h"
37
#include "libavutil/intmath.h"
38
#include "libavutil/intreadwrite.h"
39
#include "sbc.h"
40
#include "sbcdsp.h"
41
#include "sbcdsp_data.h"
42
43
/*
44
 * A reference C code of analysis filter with SIMD-friendly tables
45
 * reordering and code layout. This code can be used to develop platform
46
 * specific SIMD optimizations. Also it may be used as some kind of test
47
 * for compiler autovectorization capabilities (who knows, if the compiler
48
 * is very good at this stuff, hand optimized assembly may be not strictly
49
 * needed for some platform).
50
 *
51
 * Note: It is also possible to make a simple variant of analysis filter,
52
 * which needs only a single constants table without taking care about
53
 * even/odd cases. This simple variant of filter can be implemented without
54
 * input data permutation. The only thing that would be lost is the
55
 * possibility to use pairwise SIMD multiplications. But for some simple
56
 * CPU cores without SIMD extensions it can be useful. If anybody is
57
 * interested in implementing such variant of a filter, sourcecode from
58
 * bluez versions 4.26/4.27 can be used as a reference and the history of
59
 * the changes in git repository done around that time may be worth checking.
60
 */
61
62
static av_always_inline void sbc_analyze_simd(const int16_t *in, int32_t *out,
63
                                              const int16_t *consts,
64
                                              unsigned subbands)
65
0
{
66
0
    int32_t t1[8];
67
0
    int16_t t2[8];
68
0
    int i, j, hop = 0;
69
70
    /* rounding coefficient */
71
0
    for (i = 0; i < subbands; i++)
72
0
        t1[i] = 1 << (SBC_PROTO_FIXED_SCALE - 1);
73
74
    /* low pass polyphase filter */
75
0
    for (hop = 0; hop < 10*subbands; hop += 2*subbands)
76
0
        for (i = 0; i < 2*subbands; i++)
77
0
            t1[i >> 1] += in[hop + i] * consts[hop + i];
78
79
    /* scaling */
80
0
    for (i = 0; i < subbands; i++)
81
0
        t2[i] = t1[i] >> SBC_PROTO_FIXED_SCALE;
82
83
0
    memset(t1, 0, sizeof(t1));
84
85
    /* do the cos transform */
86
0
    for (i = 0; i < subbands/2; i++)
87
0
        for (j = 0; j < 2*subbands; j++)
88
0
            t1[j>>1] += t2[i * 2 + (j&1)] * consts[10*subbands + i*2*subbands + j];
89
90
0
    for (i = 0; i < subbands; i++)
91
0
        out[i] = t1[i] >> (SBC_COS_TABLE_FIXED_SCALE - SCALE_OUT_BITS);
92
0
}
93
94
static void sbc_analyze_4_simd(const int16_t *in, int32_t *out,
95
                               const int16_t *consts)
96
0
{
97
0
    sbc_analyze_simd(in, out, consts, 4);
98
0
}
99
100
static void sbc_analyze_8_simd(const int16_t *in, int32_t *out,
101
                               const int16_t *consts)
102
0
{
103
0
    sbc_analyze_simd(in, out, consts, 8);
104
0
}
105
106
static inline void sbc_analyze_4b_4s_simd(SBCDSPContext *s,
107
                                          int16_t *x, int32_t *out, int out_stride)
108
0
{
109
    /* Analyze blocks */
110
0
    s->sbc_analyze_4(x + 12, out, sbcdsp_analysis_consts_fixed4_simd_odd);
111
0
    out += out_stride;
112
0
    s->sbc_analyze_4(x + 8, out, sbcdsp_analysis_consts_fixed4_simd_even);
113
0
    out += out_stride;
114
0
    s->sbc_analyze_4(x + 4, out, sbcdsp_analysis_consts_fixed4_simd_odd);
115
0
    out += out_stride;
116
0
    s->sbc_analyze_4(x + 0, out, sbcdsp_analysis_consts_fixed4_simd_even);
117
0
}
118
119
static inline void sbc_analyze_4b_8s_simd(SBCDSPContext *s,
120
                                          int16_t *x, int32_t *out, int out_stride)
121
0
{
122
    /* Analyze blocks */
123
0
    s->sbc_analyze_8(x + 24, out, sbcdsp_analysis_consts_fixed8_simd_odd);
124
0
    out += out_stride;
125
0
    s->sbc_analyze_8(x + 16, out, sbcdsp_analysis_consts_fixed8_simd_even);
126
0
    out += out_stride;
127
0
    s->sbc_analyze_8(x + 8, out, sbcdsp_analysis_consts_fixed8_simd_odd);
128
0
    out += out_stride;
129
0
    s->sbc_analyze_8(x + 0, out, sbcdsp_analysis_consts_fixed8_simd_even);
130
0
}
131
132
static inline void sbc_analyze_1b_8s_simd_even(SBCDSPContext *s,
133
                                               int16_t *x, int32_t *out,
134
                                               int out_stride);
135
136
static inline void sbc_analyze_1b_8s_simd_odd(SBCDSPContext *s,
137
                                              int16_t *x, int32_t *out,
138
                                              int out_stride)
139
0
{
140
0
    s->sbc_analyze_8(x, out, sbcdsp_analysis_consts_fixed8_simd_odd);
141
0
    s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_even;
142
0
}
143
144
static inline void sbc_analyze_1b_8s_simd_even(SBCDSPContext *s,
145
                                               int16_t *x, int32_t *out,
146
                                               int out_stride)
147
0
{
148
0
    s->sbc_analyze_8(x, out, sbcdsp_analysis_consts_fixed8_simd_even);
149
0
    s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_odd;
150
0
}
151
152
/*
153
 * Input data processing functions. The data is endian converted if needed,
154
 * channels are deintrleaved and audio samples are reordered for use in
155
 * SIMD-friendly analysis filter function. The results are put into "X"
156
 * array, getting appended to the previous data (or it is better to say
157
 * prepended, as the buffer is filled from top to bottom). Old data is
158
 * discarded when neededed, but availability of (10 * nrof_subbands)
159
 * contiguous samples is always guaranteed for the input to the analysis
160
 * filter. This is achieved by copying a sufficient part of old data
161
 * to the top of the buffer on buffer wraparound.
162
 */
163
164
static int sbc_enc_process_input_4s(int position, const uint8_t *pcm,
165
                                    int16_t X[2][SBC_X_BUFFER_SIZE],
166
                                    int nsamples, int nchannels)
167
0
{
168
0
    int c;
169
170
    /* handle X buffer wraparound */
171
0
    if (position < nsamples) {
172
0
        for (c = 0; c < nchannels; c++)
173
0
            memcpy(&X[c][SBC_X_BUFFER_SIZE - 40], &X[c][position],
174
0
                            36 * sizeof(int16_t));
175
0
        position = SBC_X_BUFFER_SIZE - 40;
176
0
    }
177
178
    /* copy/permutate audio samples */
179
0
    for (; nsamples >= 8; nsamples -= 8, pcm += 16 * nchannels) {
180
0
        position -= 8;
181
0
        for (c = 0; c < nchannels; c++) {
182
0
            int16_t *x = &X[c][position];
183
0
            x[0] = AV_RN16(pcm + 14*nchannels + 2*c);
184
0
            x[1] = AV_RN16(pcm +  6*nchannels + 2*c);
185
0
            x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
186
0
            x[3] = AV_RN16(pcm +  8*nchannels + 2*c);
187
0
            x[4] = AV_RN16(pcm +  0*nchannels + 2*c);
188
0
            x[5] = AV_RN16(pcm +  4*nchannels + 2*c);
189
0
            x[6] = AV_RN16(pcm +  2*nchannels + 2*c);
190
0
            x[7] = AV_RN16(pcm + 10*nchannels + 2*c);
191
0
        }
192
0
    }
193
194
0
    return position;
195
0
}
196
197
static int sbc_enc_process_input_8s(int position, const uint8_t *pcm,
198
                                    int16_t X[2][SBC_X_BUFFER_SIZE],
199
                                    int nsamples, int nchannels)
200
0
{
201
0
    int c;
202
203
    /* handle X buffer wraparound */
204
0
    if (position < nsamples) {
205
0
        for (c = 0; c < nchannels; c++)
206
0
            memcpy(&X[c][SBC_X_BUFFER_SIZE - 72], &X[c][position],
207
0
                            72 * sizeof(int16_t));
208
0
        position = SBC_X_BUFFER_SIZE - 72;
209
0
    }
210
211
0
    if (position % 16 == 8) {
212
0
        position -= 8;
213
0
        nsamples -= 8;
214
0
        for (c = 0; c < nchannels; c++) {
215
0
            int16_t *x = &X[c][position];
216
0
            x[0] = AV_RN16(pcm + 14*nchannels + 2*c);
217
0
            x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
218
0
            x[3] = AV_RN16(pcm +  0*nchannels + 2*c);
219
0
            x[4] = AV_RN16(pcm + 10*nchannels + 2*c);
220
0
            x[5] = AV_RN16(pcm +  2*nchannels + 2*c);
221
0
            x[6] = AV_RN16(pcm +  8*nchannels + 2*c);
222
0
            x[7] = AV_RN16(pcm +  4*nchannels + 2*c);
223
0
            x[8] = AV_RN16(pcm +  6*nchannels + 2*c);
224
0
        }
225
0
        pcm += 16 * nchannels;
226
0
    }
227
228
    /* copy/permutate audio samples */
229
0
    for (; nsamples >= 16; nsamples -= 16, pcm += 32 * nchannels) {
230
0
        position -= 16;
231
0
        for (c = 0; c < nchannels; c++) {
232
0
            int16_t *x = &X[c][position];
233
0
            x[0]  = AV_RN16(pcm + 30*nchannels + 2*c);
234
0
            x[1]  = AV_RN16(pcm + 14*nchannels + 2*c);
235
0
            x[2]  = AV_RN16(pcm + 28*nchannels + 2*c);
236
0
            x[3]  = AV_RN16(pcm + 16*nchannels + 2*c);
237
0
            x[4]  = AV_RN16(pcm + 26*nchannels + 2*c);
238
0
            x[5]  = AV_RN16(pcm + 18*nchannels + 2*c);
239
0
            x[6]  = AV_RN16(pcm + 24*nchannels + 2*c);
240
0
            x[7]  = AV_RN16(pcm + 20*nchannels + 2*c);
241
0
            x[8]  = AV_RN16(pcm + 22*nchannels + 2*c);
242
0
            x[9]  = AV_RN16(pcm +  6*nchannels + 2*c);
243
0
            x[10] = AV_RN16(pcm + 12*nchannels + 2*c);
244
0
            x[11] = AV_RN16(pcm +  0*nchannels + 2*c);
245
0
            x[12] = AV_RN16(pcm + 10*nchannels + 2*c);
246
0
            x[13] = AV_RN16(pcm +  2*nchannels + 2*c);
247
0
            x[14] = AV_RN16(pcm +  8*nchannels + 2*c);
248
0
            x[15] = AV_RN16(pcm +  4*nchannels + 2*c);
249
0
        }
250
0
    }
251
252
0
    if (nsamples == 8) {
253
0
        position -= 8;
254
0
        for (c = 0; c < nchannels; c++) {
255
0
            int16_t *x = &X[c][position];
256
0
            x[-7] = AV_RN16(pcm + 14*nchannels + 2*c);
257
0
            x[1]  = AV_RN16(pcm +  6*nchannels + 2*c);
258
0
            x[2]  = AV_RN16(pcm + 12*nchannels + 2*c);
259
0
            x[3]  = AV_RN16(pcm +  0*nchannels + 2*c);
260
0
            x[4]  = AV_RN16(pcm + 10*nchannels + 2*c);
261
0
            x[5]  = AV_RN16(pcm +  2*nchannels + 2*c);
262
0
            x[6]  = AV_RN16(pcm +  8*nchannels + 2*c);
263
0
            x[7]  = AV_RN16(pcm +  4*nchannels + 2*c);
264
0
        }
265
0
    }
266
267
0
    return position;
268
0
}
269
270
static void sbc_calc_scalefactors(int32_t sb_sample_f[16][2][8],
271
                                  uint32_t scale_factor[2][8],
272
                                  int blocks, int channels, int subbands)
273
0
{
274
0
    int ch, sb, blk;
275
0
    for (ch = 0; ch < channels; ch++) {
276
0
        for (sb = 0; sb < subbands; sb++) {
277
0
            uint32_t x = 1 << SCALE_OUT_BITS;
278
0
            for (blk = 0; blk < blocks; blk++) {
279
0
                int32_t tmp = FFABS(sb_sample_f[blk][ch][sb]);
280
0
                if (tmp != 0)
281
0
                    x |= tmp - 1;
282
0
            }
283
0
            scale_factor[ch][sb] = (31 - SCALE_OUT_BITS) - ff_clz(x);
284
0
        }
285
0
    }
286
0
}
287
288
static int sbc_calc_scalefactors_j(int32_t sb_sample_f[16][2][8],
289
                                   uint32_t scale_factor[2][8],
290
                                   int blocks, int subbands)
291
0
{
292
0
    int blk, joint = 0;
293
0
    int32_t tmp0, tmp1;
294
0
    uint32_t x, y;
295
296
    /* last subband does not use joint stereo */
297
0
    int sb = subbands - 1;
298
0
    x = 1 << SCALE_OUT_BITS;
299
0
    y = 1 << SCALE_OUT_BITS;
300
0
    for (blk = 0; blk < blocks; blk++) {
301
0
        tmp0 = FFABS(sb_sample_f[blk][0][sb]);
302
0
        tmp1 = FFABS(sb_sample_f[blk][1][sb]);
303
0
        if (tmp0 != 0)
304
0
            x |= tmp0 - 1;
305
0
        if (tmp1 != 0)
306
0
            y |= tmp1 - 1;
307
0
    }
308
0
    scale_factor[0][sb] = (31 - SCALE_OUT_BITS) - ff_clz(x);
309
0
    scale_factor[1][sb] = (31 - SCALE_OUT_BITS) - ff_clz(y);
310
311
    /* the rest of subbands can use joint stereo */
312
0
    while (--sb >= 0) {
313
0
        int32_t sb_sample_j[16][2];
314
0
        x = 1 << SCALE_OUT_BITS;
315
0
        y = 1 << SCALE_OUT_BITS;
316
0
        for (blk = 0; blk < blocks; blk++) {
317
0
            tmp0 = sb_sample_f[blk][0][sb];
318
0
            tmp1 = sb_sample_f[blk][1][sb];
319
0
            sb_sample_j[blk][0] = (tmp0 >> 1) + (tmp1 >> 1);
320
0
            sb_sample_j[blk][1] = (tmp0 >> 1) - (tmp1 >> 1);
321
0
            tmp0 = FFABS(tmp0);
322
0
            tmp1 = FFABS(tmp1);
323
0
            if (tmp0 != 0)
324
0
                x |= tmp0 - 1;
325
0
            if (tmp1 != 0)
326
0
                y |= tmp1 - 1;
327
0
        }
328
0
        scale_factor[0][sb] = (31 - SCALE_OUT_BITS) -
329
0
            ff_clz(x);
330
0
        scale_factor[1][sb] = (31 - SCALE_OUT_BITS) -
331
0
            ff_clz(y);
332
0
        x = 1 << SCALE_OUT_BITS;
333
0
        y = 1 << SCALE_OUT_BITS;
334
0
        for (blk = 0; blk < blocks; blk++) {
335
0
            tmp0 = FFABS(sb_sample_j[blk][0]);
336
0
            tmp1 = FFABS(sb_sample_j[blk][1]);
337
0
            if (tmp0 != 0)
338
0
                x |= tmp0 - 1;
339
0
            if (tmp1 != 0)
340
0
                y |= tmp1 - 1;
341
0
        }
342
0
        x = (31 - SCALE_OUT_BITS) - ff_clz(x);
343
0
        y = (31 - SCALE_OUT_BITS) - ff_clz(y);
344
345
        /* decide whether to use joint stereo for this subband */
346
0
        if ((scale_factor[0][sb] + scale_factor[1][sb]) > x + y) {
347
0
            joint |= 1 << (subbands - 1 - sb);
348
0
            scale_factor[0][sb] = x;
349
0
            scale_factor[1][sb] = y;
350
0
            for (blk = 0; blk < blocks; blk++) {
351
0
                sb_sample_f[blk][0][sb] = sb_sample_j[blk][0];
352
0
                sb_sample_f[blk][1][sb] = sb_sample_j[blk][1];
353
0
            }
354
0
        }
355
0
    }
356
357
    /* bitmask with the information about subbands using joint stereo */
358
0
    return joint;
359
0
}
360
361
/*
362
 * Detect CPU features and setup function pointers
363
 */
364
av_cold void ff_sbcdsp_init(SBCDSPContext *s)
365
0
{
366
    /* Default implementation for analyze functions */
367
0
    s->sbc_analyze_4 = sbc_analyze_4_simd;
368
0
    s->sbc_analyze_8 = sbc_analyze_8_simd;
369
0
    s->sbc_analyze_4s = sbc_analyze_4b_4s_simd;
370
0
    if (s->increment == 1)
371
0
        s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_odd;
372
0
    else
373
0
        s->sbc_analyze_8s = sbc_analyze_4b_8s_simd;
374
375
    /* Default implementation for input reordering / deinterleaving */
376
0
    s->sbc_enc_process_input_4s = sbc_enc_process_input_4s;
377
0
    s->sbc_enc_process_input_8s = sbc_enc_process_input_8s;
378
379
    /* Default implementation for scale factors calculation */
380
0
    s->sbc_calc_scalefactors = sbc_calc_scalefactors;
381
0
    s->sbc_calc_scalefactors_j = sbc_calc_scalefactors_j;
382
383
#if ARCH_ARM
384
    ff_sbcdsp_init_arm(s);
385
#elif ARCH_X86
386
    ff_sbcdsp_init_x86(s);
387
0
#endif
388
0
}