Coverage Report

Created: 2026-09-14 08:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/sbc.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-2008  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 common functions for the encoder and decoder
31
 */
32
33
#include "sbc.h"
34
35
/* A2DP specification: Appendix B, page 69 */
36
static const int sbc_offset4[4][4] = {
37
    { -1, 0, 0, 0 },
38
    { -2, 0, 0, 1 },
39
    { -2, 0, 0, 1 },
40
    { -2, 0, 0, 1 }
41
};
42
43
/* A2DP specification: Appendix B, page 69 */
44
static const int sbc_offset8[4][8] = {
45
    { -2, 0, 0, 0, 0, 0, 0, 1 },
46
    { -3, 0, 0, 0, 0, 0, 1, 2 },
47
    { -4, 0, 0, 0, 0, 0, 1, 2 },
48
    { -4, 0, 0, 0, 0, 0, 1, 2 }
49
};
50
51
/*
52
 * Calculates the CRC-8 of the first len bits in data
53
 */
54
uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len)
55
74.2k
{
56
74.2k
    size_t byte_length = len >> 3;
57
74.2k
    int bit_length = len & 7;
58
74.2k
    uint8_t crc;
59
60
74.2k
    crc = av_crc(ctx, 0x0F, data, byte_length);
61
62
74.2k
    if (bit_length) {
63
2.37k
        uint8_t bits = data[byte_length];
64
11.8k
        while (bit_length--) {
65
9.51k
            int8_t mask = bits ^ crc;
66
9.51k
            crc = (crc << 1) ^ ((mask >> 7) & 0x1D);
67
9.51k
            bits <<= 1;
68
9.51k
        }
69
2.37k
    }
70
71
74.2k
    return crc;
72
74.2k
}
73
74
/*
75
 * Code straight from the spec to calculate the bits array
76
 * Takes a pointer to the frame in question and a pointer to the bits array
77
 */
78
void ff_sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8])
79
39.5k
{
80
39.5k
    int subbands = frame->subbands;
81
39.5k
    uint8_t sf = frame->frequency;
82
83
39.5k
    if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
84
15.7k
        int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
85
15.7k
        int ch, sb;
86
87
31.5k
        for (ch = 0; ch < frame->channels; ch++) {
88
15.8k
            max_bitneed = 0;
89
15.8k
            if (frame->allocation == SNR) {
90
13.4k
                for (sb = 0; sb < subbands; sb++) {
91
11.9k
                    bitneed[ch][sb] = frame->scale_factor[ch][sb];
92
11.9k
                    if (bitneed[ch][sb] > max_bitneed)
93
2.89k
                        max_bitneed = bitneed[ch][sb];
94
11.9k
                }
95
14.3k
            } else {
96
77.0k
                for (sb = 0; sb < subbands; sb++) {
97
62.7k
                    if (frame->scale_factor[ch][sb] == 0)
98
1.01k
                        bitneed[ch][sb] = -5;
99
61.7k
                    else {
100
61.7k
                        if (subbands == 4)
101
51.7k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
102
10.0k
                        else
103
10.0k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
104
61.7k
                        if (loudness > 0)
105
61.7k
                            bitneed[ch][sb] = loudness / 2;
106
0
                        else
107
0
                            bitneed[ch][sb] = loudness;
108
61.7k
                    }
109
62.7k
                    if (bitneed[ch][sb] > max_bitneed)
110
40.1k
                        max_bitneed = bitneed[ch][sb];
111
62.7k
                }
112
14.3k
            }
113
114
15.8k
            bitcount = 0;
115
15.8k
            slicecount = 0;
116
15.8k
            bitslice = max_bitneed + 1;
117
49.4k
            do {
118
49.4k
                bitslice--;
119
49.4k
                bitcount += slicecount;
120
49.4k
                slicecount = 0;
121
339k
                for (sb = 0; sb < subbands; sb++) {
122
289k
                    if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
123
92.8k
                        slicecount++;
124
196k
                    else if (bitneed[ch][sb] == bitslice + 1)
125
32.1k
                        slicecount += 2;
126
289k
                }
127
49.4k
            } while (bitcount + slicecount < frame->bitpool);
128
129
15.8k
            if (bitcount + slicecount == frame->bitpool) {
130
186
                bitcount += slicecount;
131
186
                bitslice--;
132
186
            }
133
134
90.4k
            for (sb = 0; sb < subbands; sb++) {
135
74.6k
                if (bitneed[ch][sb] < bitslice + 2)
136
58.1k
                    bits[ch][sb] = 0;
137
16.5k
                else {
138
16.5k
                    bits[ch][sb] = bitneed[ch][sb] - bitslice;
139
16.5k
                    if (bits[ch][sb] > 16)
140
50
                        bits[ch][sb] = 16;
141
16.5k
                }
142
74.6k
            }
143
144
81.1k
            for (sb = 0; bitcount < frame->bitpool &&
145
78.3k
                            sb < subbands; sb++) {
146
65.3k
                if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
147
11.8k
                    bits[ch][sb]++;
148
11.8k
                    bitcount++;
149
53.5k
                } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
150
1.42k
                    bits[ch][sb] = 2;
151
1.42k
                    bitcount += 2;
152
1.42k
                }
153
65.3k
            }
154
155
28.7k
            for (sb = 0; bitcount < frame->bitpool &&
156
12.9k
                            sb < subbands; sb++) {
157
12.9k
                if (bits[ch][sb] < 16) {
158
12.9k
                    bits[ch][sb]++;
159
12.9k
                    bitcount++;
160
12.9k
                }
161
12.9k
            }
162
163
15.8k
        }
164
165
23.8k
    } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
166
23.8k
        int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
167
23.8k
        int ch, sb;
168
169
23.8k
        max_bitneed = 0;
170
23.8k
        if (frame->allocation == SNR) {
171
63.9k
            for (ch = 0; ch < 2; ch++) {
172
383k
                for (sb = 0; sb < subbands; sb++) {
173
340k
                    bitneed[ch][sb] = frame->scale_factor[ch][sb];
174
340k
                    if (bitneed[ch][sb] > max_bitneed)
175
347
                        max_bitneed = bitneed[ch][sb];
176
340k
                }
177
42.6k
            }
178
21.3k
        } else {
179
7.48k
            for (ch = 0; ch < 2; ch++) {
180
28.5k
                for (sb = 0; sb < subbands; sb++) {
181
23.5k
                    if (frame->scale_factor[ch][sb] == 0)
182
5.70k
                        bitneed[ch][sb] = -5;
183
17.8k
                    else {
184
17.8k
                        if (subbands == 4)
185
16.2k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
186
1.62k
                        else
187
1.62k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
188
17.8k
                        if (loudness > 0)
189
15.8k
                            bitneed[ch][sb] = loudness / 2;
190
2.01k
                        else
191
2.01k
                            bitneed[ch][sb] = loudness;
192
17.8k
                    }
193
23.5k
                    if (bitneed[ch][sb] > max_bitneed)
194
2.54k
                        max_bitneed = bitneed[ch][sb];
195
23.5k
                }
196
4.98k
            }
197
2.49k
        }
198
199
23.8k
        bitcount = 0;
200
23.8k
        slicecount = 0;
201
23.8k
        bitslice = max_bitneed + 1;
202
40.4k
        do {
203
40.4k
            bitslice--;
204
40.4k
            bitcount += slicecount;
205
40.4k
            slicecount = 0;
206
121k
            for (ch = 0; ch < 2; ch++) {
207
623k
                for (sb = 0; sb < subbands; sb++) {
208
542k
                    if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
209
50.0k
                        slicecount++;
210
492k
                    else if (bitneed[ch][sb] == bitslice + 1)
211
15.6k
                        slicecount += 2;
212
542k
                }
213
80.8k
            }
214
40.4k
        } while (bitcount + slicecount < frame->bitpool);
215
216
23.8k
        if (bitcount + slicecount == frame->bitpool) {
217
21.1k
            bitcount += slicecount;
218
21.1k
            bitslice--;
219
21.1k
        }
220
221
71.4k
        for (ch = 0; ch < 2; ch++) {
222
411k
            for (sb = 0; sb < subbands; sb++) {
223
364k
                if (bitneed[ch][sb] < bitslice + 2) {
224
350k
                    bits[ch][sb] = 0;
225
350k
                } else {
226
13.5k
                    bits[ch][sb] = bitneed[ch][sb] - bitslice;
227
13.5k
                    if (bits[ch][sb] > 16)
228
175
                        bits[ch][sb] = 16;
229
13.5k
                }
230
364k
            }
231
47.6k
        }
232
233
23.8k
        ch = 0;
234
23.8k
        sb = 0;
235
29.6k
        while (bitcount < frame->bitpool) {
236
5.87k
            if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
237
5.44k
                bits[ch][sb]++;
238
5.44k
                bitcount++;
239
5.44k
            } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
240
50
                bits[ch][sb] = 2;
241
50
                bitcount += 2;
242
50
            }
243
5.87k
            if (ch == 1) {
244
2.65k
                ch = 0;
245
2.65k
                sb++;
246
2.65k
                if (sb >= subbands)
247
6
                    break;
248
2.65k
            } else
249
3.21k
                ch = 1;
250
5.87k
        }
251
252
23.8k
        ch = 0;
253
23.8k
        sb = 0;
254
23.8k
        while (bitcount < frame->bitpool) {
255
6
            if (bits[ch][sb] < 16) {
256
6
                bits[ch][sb]++;
257
6
                bitcount++;
258
6
            }
259
6
            if (ch == 1) {
260
0
                ch = 0;
261
0
                sb++;
262
0
                if (sb >= subbands)
263
0
                    break;
264
0
            } else
265
6
                ch = 1;
266
6
        }
267
268
23.8k
    }
269
270
39.5k
}