FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sbcdsp.c
Date: 2026-05-03 13:33:45
Exec Total Coverage
Lines: 159 192 82.8%
Functions: 10 12 83.3%
Branches: 65 72 90.3%

Line Branch Exec 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 513942 static av_always_inline void sbc_analyze_simd(const int16_t *in, int32_t *out,
63 const int16_t *consts,
64 unsigned subbands)
65 {
66 int32_t t1[8];
67 int16_t t2[8];
68 513942 int i, j, hop = 0;
69
70 /* rounding coefficient */
71
2/2
✓ Branch 0 taken 2765220 times.
✓ Branch 1 taken 513942 times.
3279162 for (i = 0; i < subbands; i++)
72 2765220 t1[i] = 1 << (SBC_PROTO_FIXED_SCALE - 1);
73
74 /* low pass polyphase filter */
75
2/2
✓ Branch 0 taken 2569710 times.
✓ Branch 1 taken 513942 times.
3083652 for (hop = 0; hop < 10*subbands; hop += 2*subbands)
76
2/2
✓ Branch 0 taken 27652200 times.
✓ Branch 1 taken 2569710 times.
30221910 for (i = 0; i < 2*subbands; i++)
77 27652200 t1[i >> 1] += in[hop + i] * consts[hop + i];
78
79 /* scaling */
80
2/2
✓ Branch 0 taken 2765220 times.
✓ Branch 1 taken 513942 times.
3279162 for (i = 0; i < subbands; i++)
81 2765220 t2[i] = t1[i] >> SBC_PROTO_FIXED_SCALE;
82
83 513942 memset(t1, 0, sizeof(t1));
84
85 /* do the cos transform */
86
2/2
✓ Branch 0 taken 1382610 times.
✓ Branch 1 taken 513942 times.
1896552 for (i = 0; i < subbands/2; i++)
87
2/2
✓ Branch 0 taken 16736496 times.
✓ Branch 1 taken 1382610 times.
18119106 for (j = 0; j < 2*subbands; j++)
88 16736496 t1[j>>1] += t2[i * 2 + (j&1)] * consts[10*subbands + i*2*subbands + j];
89
90
2/2
✓ Branch 0 taken 2765220 times.
✓ Branch 1 taken 513942 times.
3279162 for (i = 0; i < subbands; i++)
91 2765220 out[i] = t1[i] >> (SBC_COS_TABLE_FIXED_SCALE - SCALE_OUT_BITS);
92 513942 }
93
94 336579 static void sbc_analyze_4_simd(const int16_t *in, int32_t *out,
95 const int16_t *consts)
96 {
97 336579 sbc_analyze_simd(in, out, consts, 4);
98 336579 }
99
100 177363 static void sbc_analyze_8_simd(const int16_t *in, int32_t *out,
101 const int16_t *consts)
102 {
103 177363 sbc_analyze_simd(in, out, consts, 8);
104 177363 }
105
106 84144 static inline void sbc_analyze_4b_4s_simd(SBCDSPContext *s, const int16_t *x,
107 int32_t *out, int out_stride)
108 {
109 /* Analyze blocks */
110 84144 s->sbc_analyze_4(x + 12, out, sbcdsp_analysis_consts_fixed4_simd_odd);
111 84144 out += out_stride;
112 84144 s->sbc_analyze_4(x + 8, out, sbcdsp_analysis_consts_fixed4_simd_even);
113 84144 out += out_stride;
114 84144 s->sbc_analyze_4(x + 4, out, sbcdsp_analysis_consts_fixed4_simd_odd);
115 84144 out += out_stride;
116 84144 s->sbc_analyze_4(x + 0, out, sbcdsp_analysis_consts_fixed4_simd_even);
117 84144 }
118
119 44340 static inline void sbc_analyze_4b_8s_simd(SBCDSPContext *s, const int16_t *x,
120 int32_t *out, int out_stride)
121 {
122 /* Analyze blocks */
123 44340 s->sbc_analyze_8(x + 24, out, sbcdsp_analysis_consts_fixed8_simd_odd);
124 44340 out += out_stride;
125 44340 s->sbc_analyze_8(x + 16, out, sbcdsp_analysis_consts_fixed8_simd_even);
126 44340 out += out_stride;
127 44340 s->sbc_analyze_8(x + 8, out, sbcdsp_analysis_consts_fixed8_simd_odd);
128 44340 out += out_stride;
129 44340 s->sbc_analyze_8(x + 0, out, sbcdsp_analysis_consts_fixed8_simd_even);
130 44340 }
131
132 static inline void sbc_analyze_1b_8s_simd_even(SBCDSPContext *s,
133 const int16_t *x, int32_t *out,
134 int out_stride);
135
136 static inline void sbc_analyze_1b_8s_simd_odd(SBCDSPContext *s,
137 const int16_t *x, int32_t *out,
138 int out_stride)
139 {
140 s->sbc_analyze_8(x, out, sbcdsp_analysis_consts_fixed8_simd_odd);
141 s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_even;
142 }
143
144 static inline void sbc_analyze_1b_8s_simd_even(SBCDSPContext *s,
145 const int16_t *x, int32_t *out,
146 int out_stride)
147 {
148 s->sbc_analyze_8(x, out, sbcdsp_analysis_consts_fixed8_simd_even);
149 s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_odd;
150 }
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 26268 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 {
168 int c;
169
170 /* handle X buffer wraparound */
171
2/2
✓ Branch 0 taken 3065 times.
✓ Branch 1 taken 23203 times.
26268 if (position < nsamples) {
172
2/2
✓ Branch 0 taken 5131 times.
✓ Branch 1 taken 3065 times.
8196 for (c = 0; c < nchannels; c++)
173 5131 memcpy(&X[c][SBC_X_BUFFER_SIZE - 40], &X[c][position],
174 36 * sizeof(int16_t));
175 3065 position = SBC_X_BUFFER_SIZE - 40;
176 }
177
178 /* copy/permutate audio samples */
179
2/2
✓ Branch 0 taken 102144 times.
✓ Branch 1 taken 26268 times.
128412 for (; nsamples >= 8; nsamples -= 8, pcm += 16 * nchannels) {
180 102144 position -= 8;
181
2/2
✓ Branch 0 taken 168288 times.
✓ Branch 1 taken 102144 times.
270432 for (c = 0; c < nchannels; c++) {
182 168288 int16_t *x = &X[c][position];
183 168288 x[0] = AV_RN16(pcm + 14*nchannels + 2*c);
184 168288 x[1] = AV_RN16(pcm + 6*nchannels + 2*c);
185 168288 x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
186 168288 x[3] = AV_RN16(pcm + 8*nchannels + 2*c);
187 168288 x[4] = AV_RN16(pcm + 0*nchannels + 2*c);
188 168288 x[5] = AV_RN16(pcm + 4*nchannels + 2*c);
189 168288 x[6] = AV_RN16(pcm + 2*nchannels + 2*c);
190 168288 x[7] = AV_RN16(pcm + 10*nchannels + 2*c);
191 }
192 }
193
194 26268 return position;
195 }
196
197 6951 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 {
201 int c;
202
203 /* handle X buffer wraparound */
204
2/2
✓ Branch 0 taken 3473 times.
✓ Branch 1 taken 3478 times.
6951 if (position < nsamples) {
205
2/2
✓ Branch 0 taken 5539 times.
✓ Branch 1 taken 3473 times.
9012 for (c = 0; c < nchannels; c++)
206 5539 memcpy(&X[c][SBC_X_BUFFER_SIZE - 72], &X[c][position],
207 72 * sizeof(int16_t));
208 3473 position = SBC_X_BUFFER_SIZE - 72;
209 }
210
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6951 times.
6951 if (position % 16 == 8) {
212 position -= 8;
213 nsamples -= 8;
214 for (c = 0; c < nchannels; c++) {
215 int16_t *x = &X[c][position];
216 x[0] = AV_RN16(pcm + 14*nchannels + 2*c);
217 x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
218 x[3] = AV_RN16(pcm + 0*nchannels + 2*c);
219 x[4] = AV_RN16(pcm + 10*nchannels + 2*c);
220 x[5] = AV_RN16(pcm + 2*nchannels + 2*c);
221 x[6] = AV_RN16(pcm + 8*nchannels + 2*c);
222 x[7] = AV_RN16(pcm + 4*nchannels + 2*c);
223 x[8] = AV_RN16(pcm + 6*nchannels + 2*c);
224 }
225 pcm += 16 * nchannels;
226 }
227
228 /* copy/permutate audio samples */
229
2/2
✓ Branch 0 taken 55608 times.
✓ Branch 1 taken 6951 times.
62559 for (; nsamples >= 16; nsamples -= 16, pcm += 32 * nchannels) {
230 55608 position -= 16;
231
2/2
✓ Branch 0 taken 88680 times.
✓ Branch 1 taken 55608 times.
144288 for (c = 0; c < nchannels; c++) {
232 88680 int16_t *x = &X[c][position];
233 88680 x[0] = AV_RN16(pcm + 30*nchannels + 2*c);
234 88680 x[1] = AV_RN16(pcm + 14*nchannels + 2*c);
235 88680 x[2] = AV_RN16(pcm + 28*nchannels + 2*c);
236 88680 x[3] = AV_RN16(pcm + 16*nchannels + 2*c);
237 88680 x[4] = AV_RN16(pcm + 26*nchannels + 2*c);
238 88680 x[5] = AV_RN16(pcm + 18*nchannels + 2*c);
239 88680 x[6] = AV_RN16(pcm + 24*nchannels + 2*c);
240 88680 x[7] = AV_RN16(pcm + 20*nchannels + 2*c);
241 88680 x[8] = AV_RN16(pcm + 22*nchannels + 2*c);
242 88680 x[9] = AV_RN16(pcm + 6*nchannels + 2*c);
243 88680 x[10] = AV_RN16(pcm + 12*nchannels + 2*c);
244 88680 x[11] = AV_RN16(pcm + 0*nchannels + 2*c);
245 88680 x[12] = AV_RN16(pcm + 10*nchannels + 2*c);
246 88680 x[13] = AV_RN16(pcm + 2*nchannels + 2*c);
247 88680 x[14] = AV_RN16(pcm + 8*nchannels + 2*c);
248 88680 x[15] = AV_RN16(pcm + 4*nchannels + 2*c);
249 }
250 }
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6951 times.
6951 if (nsamples == 8) {
253 position -= 8;
254 for (c = 0; c < nchannels; c++) {
255 int16_t *x = &X[c][position];
256 x[-7] = AV_RN16(pcm + 14*nchannels + 2*c);
257 x[1] = AV_RN16(pcm + 6*nchannels + 2*c);
258 x[2] = AV_RN16(pcm + 12*nchannels + 2*c);
259 x[3] = AV_RN16(pcm + 0*nchannels + 2*c);
260 x[4] = AV_RN16(pcm + 10*nchannels + 2*c);
261 x[5] = AV_RN16(pcm + 2*nchannels + 2*c);
262 x[6] = AV_RN16(pcm + 8*nchannels + 2*c);
263 x[7] = AV_RN16(pcm + 4*nchannels + 2*c);
264 }
265 }
266
267 6951 return position;
268 }
269
270 27030 static void sbc_calc_scalefactors(const int32_t sb_sample_f[16][2][8],
271 uint32_t scale_factor[2][8],
272 int blocks, int channels, int subbands)
273 {
274 int ch, sb, blk;
275
2/2
✓ Branch 0 taken 33237 times.
✓ Branch 1 taken 27030 times.
60267 for (ch = 0; ch < channels; ch++) {
276
2/2
✓ Branch 0 taken 160788 times.
✓ Branch 1 taken 33237 times.
194025 for (sb = 0; sb < subbands; sb++) {
277 160788 uint32_t x = 1 << SCALE_OUT_BITS;
278
2/2
✓ Branch 0 taken 1707744 times.
✓ Branch 1 taken 160788 times.
1868532 for (blk = 0; blk < blocks; blk++) {
279 1707744 int32_t tmp = FFABS(sb_sample_f[blk][ch][sb]);
280
2/2
✓ Branch 0 taken 1702918 times.
✓ Branch 1 taken 4826 times.
1707744 if (tmp != 0)
281 1702918 x |= tmp - 1;
282 }
283 160788 scale_factor[ch][sb] = (31 - SCALE_OUT_BITS) - ff_clz(x);
284 }
285 }
286 27030 }
287
288 6201 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 {
292 6201 int blk, joint = 0;
293 int32_t tmp0, tmp1;
294 uint32_t x, y;
295
296 /* last subband does not use joint stereo */
297 6201 int sb = subbands - 1;
298 6201 x = 1 << SCALE_OUT_BITS;
299 6201 y = 1 << SCALE_OUT_BITS;
300
2/2
✓ Branch 0 taken 99216 times.
✓ Branch 1 taken 6201 times.
105417 for (blk = 0; blk < blocks; blk++) {
301 99216 tmp0 = FFABS(sb_sample_f[blk][0][sb]);
302 99216 tmp1 = FFABS(sb_sample_f[blk][1][sb]);
303
2/2
✓ Branch 0 taken 98913 times.
✓ Branch 1 taken 303 times.
99216 if (tmp0 != 0)
304 98913 x |= tmp0 - 1;
305
2/2
✓ Branch 0 taken 99012 times.
✓ Branch 1 taken 204 times.
99216 if (tmp1 != 0)
306 99012 y |= tmp1 - 1;
307 }
308 6201 scale_factor[0][sb] = (31 - SCALE_OUT_BITS) - ff_clz(x);
309 6201 scale_factor[1][sb] = (31 - SCALE_OUT_BITS) - ff_clz(y);
310
311 /* the rest of subbands can use joint stereo */
312
2/2
✓ Branch 0 taken 26871 times.
✓ Branch 1 taken 6201 times.
33072 while (--sb >= 0) {
313 int32_t sb_sample_j[16][2];
314 26871 x = 1 << SCALE_OUT_BITS;
315 26871 y = 1 << SCALE_OUT_BITS;
316
2/2
✓ Branch 0 taken 429936 times.
✓ Branch 1 taken 26871 times.
456807 for (blk = 0; blk < blocks; blk++) {
317 429936 tmp0 = sb_sample_f[blk][0][sb];
318 429936 tmp1 = sb_sample_f[blk][1][sb];
319 429936 sb_sample_j[blk][0] = (tmp0 >> 1) + (tmp1 >> 1);
320 429936 sb_sample_j[blk][1] = (tmp0 >> 1) - (tmp1 >> 1);
321 429936 tmp0 = FFABS(tmp0);
322 429936 tmp1 = FFABS(tmp1);
323
2/2
✓ Branch 0 taken 428698 times.
✓ Branch 1 taken 1238 times.
429936 if (tmp0 != 0)
324 428698 x |= tmp0 - 1;
325
2/2
✓ Branch 0 taken 429077 times.
✓ Branch 1 taken 859 times.
429936 if (tmp1 != 0)
326 429077 y |= tmp1 - 1;
327 }
328 26871 scale_factor[0][sb] = (31 - SCALE_OUT_BITS) -
329 26871 ff_clz(x);
330 26871 scale_factor[1][sb] = (31 - SCALE_OUT_BITS) -
331 26871 ff_clz(y);
332 26871 x = 1 << SCALE_OUT_BITS;
333 26871 y = 1 << SCALE_OUT_BITS;
334
2/2
✓ Branch 0 taken 429936 times.
✓ Branch 1 taken 26871 times.
456807 for (blk = 0; blk < blocks; blk++) {
335 429936 tmp0 = FFABS(sb_sample_j[blk][0]);
336 429936 tmp1 = FFABS(sb_sample_j[blk][1]);
337
2/2
✓ Branch 0 taken 429923 times.
✓ Branch 1 taken 13 times.
429936 if (tmp0 != 0)
338 429923 x |= tmp0 - 1;
339
2/2
✓ Branch 0 taken 214934 times.
✓ Branch 1 taken 215002 times.
429936 if (tmp1 != 0)
340 214934 y |= tmp1 - 1;
341 }
342 26871 x = (31 - SCALE_OUT_BITS) - ff_clz(x);
343 26871 y = (31 - SCALE_OUT_BITS) - ff_clz(y);
344
345 /* decide whether to use joint stereo for this subband */
346
2/2
✓ Branch 0 taken 12444 times.
✓ Branch 1 taken 14427 times.
26871 if ((scale_factor[0][sb] + scale_factor[1][sb]) > x + y) {
347 12444 joint |= 1 << (subbands - 1 - sb);
348 12444 scale_factor[0][sb] = x;
349 12444 scale_factor[1][sb] = y;
350
2/2
✓ Branch 0 taken 199104 times.
✓ Branch 1 taken 12444 times.
211548 for (blk = 0; blk < blocks; blk++) {
351 199104 sb_sample_f[blk][0][sb] = sb_sample_j[blk][0];
352 199104 sb_sample_f[blk][1][sb] = sb_sample_j[blk][1];
353 }
354 }
355 }
356
357 /* bitmask with the information about subbands using joint stereo */
358 6201 return joint;
359 }
360
361 /*
362 * Detect CPU features and setup function pointers
363 */
364 21 av_cold void ff_sbcdsp_init(SBCDSPContext *s)
365 {
366 /* Default implementation for analyze functions */
367 21 s->sbc_analyze_4 = sbc_analyze_4_simd;
368 21 s->sbc_analyze_8 = sbc_analyze_8_simd;
369 21 s->sbc_analyze_4s = sbc_analyze_4b_4s_simd;
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (s->increment == 1)
371 s->sbc_analyze_8s = sbc_analyze_1b_8s_simd_odd;
372 else
373 21 s->sbc_analyze_8s = sbc_analyze_4b_8s_simd;
374
375 /* Default implementation for input reordering / deinterleaving */
376 21 s->sbc_enc_process_input_4s = sbc_enc_process_input_4s;
377 21 s->sbc_enc_process_input_8s = sbc_enc_process_input_8s;
378
379 /* Default implementation for scale factors calculation */
380 21 s->sbc_calc_scalefactors = sbc_calc_scalefactors;
381 21 s->sbc_calc_scalefactors_j = sbc_calc_scalefactors_j;
382
383 #if ARCH_ARM
384 ff_sbcdsp_init_arm(s);
385 #elif ARCH_X86 && HAVE_X86ASM
386 21 ff_sbcdsp_init_x86(s);
387 #endif
388 21 }
389