Coverage Report

Created: 2025-11-16 07:20

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
43.6k
{
56
43.6k
    size_t byte_length = len >> 3;
57
43.6k
    int bit_length = len & 7;
58
43.6k
    uint8_t crc;
59
60
43.6k
    crc = av_crc(ctx, 0x0F, data, byte_length);
61
62
43.6k
    if (bit_length) {
63
3.94k
        uint8_t bits = data[byte_length];
64
19.7k
        while (bit_length--) {
65
15.7k
            int8_t mask = bits ^ crc;
66
15.7k
            crc = (crc << 1) ^ ((mask >> 7) & 0x1D);
67
15.7k
            bits <<= 1;
68
15.7k
        }
69
3.94k
    }
70
71
43.6k
    return crc;
72
43.6k
}
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
18.3k
{
80
18.3k
    int subbands = frame->subbands;
81
18.3k
    uint8_t sf = frame->frequency;
82
83
18.3k
    if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) {
84
6.94k
        int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
85
6.94k
        int ch, sb;
86
87
13.9k
        for (ch = 0; ch < frame->channels; ch++) {
88
6.98k
            max_bitneed = 0;
89
6.98k
            if (frame->allocation == SNR) {
90
7.55k
                for (sb = 0; sb < subbands; sb++) {
91
6.71k
                    bitneed[ch][sb] = frame->scale_factor[ch][sb];
92
6.71k
                    if (bitneed[ch][sb] > max_bitneed)
93
1.62k
                        max_bitneed = bitneed[ch][sb];
94
6.71k
                }
95
6.13k
            } else {
96
34.8k
                for (sb = 0; sb < subbands; sb++) {
97
28.6k
                    if (frame->scale_factor[ch][sb] == 0)
98
63
                        bitneed[ch][sb] = -5;
99
28.6k
                    else {
100
28.6k
                        if (subbands == 4)
101
20.3k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
102
8.23k
                        else
103
8.23k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
104
28.6k
                        if (loudness > 0)
105
28.6k
                            bitneed[ch][sb] = loudness / 2;
106
0
                        else
107
0
                            bitneed[ch][sb] = loudness;
108
28.6k
                    }
109
28.6k
                    if (bitneed[ch][sb] > max_bitneed)
110
16.3k
                        max_bitneed = bitneed[ch][sb];
111
28.6k
                }
112
6.13k
            }
113
114
6.98k
            bitcount = 0;
115
6.98k
            slicecount = 0;
116
6.98k
            bitslice = max_bitneed + 1;
117
24.7k
            do {
118
24.7k
                bitslice--;
119
24.7k
                bitcount += slicecount;
120
24.7k
                slicecount = 0;
121
180k
                for (sb = 0; sb < subbands; sb++) {
122
156k
                    if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
123
54.7k
                        slicecount++;
124
101k
                    else if (bitneed[ch][sb] == bitslice + 1)
125
18.0k
                        slicecount += 2;
126
156k
                }
127
24.7k
            } while (bitcount + slicecount < frame->bitpool);
128
129
6.98k
            if (bitcount + slicecount == frame->bitpool) {
130
54
                bitcount += slicecount;
131
54
                bitslice--;
132
54
            }
133
134
42.3k
            for (sb = 0; sb < subbands; sb++) {
135
35.4k
                if (bitneed[ch][sb] < bitslice + 2)
136
24.6k
                    bits[ch][sb] = 0;
137
10.7k
                else {
138
10.7k
                    bits[ch][sb] = bitneed[ch][sb] - bitslice;
139
10.7k
                    if (bits[ch][sb] > 16)
140
39
                        bits[ch][sb] = 16;
141
10.7k
                }
142
35.4k
            }
143
144
36.3k
            for (sb = 0; bitcount < frame->bitpool &&
145
34.4k
                            sb < subbands; sb++) {
146
29.3k
                if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
147
7.74k
                    bits[ch][sb]++;
148
7.74k
                    bitcount++;
149
21.6k
                } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
150
1.21k
                    bits[ch][sb] = 2;
151
1.21k
                    bitcount += 2;
152
1.21k
                }
153
29.3k
            }
154
155
12.1k
            for (sb = 0; bitcount < frame->bitpool &&
156
5.12k
                            sb < subbands; sb++) {
157
5.12k
                if (bits[ch][sb] < 16) {
158
5.12k
                    bits[ch][sb]++;
159
5.12k
                    bitcount++;
160
5.12k
                }
161
5.12k
            }
162
163
6.98k
        }
164
165
11.3k
    } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) {
166
11.3k
        int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice;
167
11.3k
        int ch, sb;
168
169
11.3k
        max_bitneed = 0;
170
11.3k
        if (frame->allocation == SNR) {
171
24.1k
            for (ch = 0; ch < 2; ch++) {
172
144k
                for (sb = 0; sb < subbands; sb++) {
173
128k
                    bitneed[ch][sb] = frame->scale_factor[ch][sb];
174
128k
                    if (bitneed[ch][sb] > max_bitneed)
175
185
                        max_bitneed = bitneed[ch][sb];
176
128k
                }
177
16.1k
            }
178
8.05k
        } else {
179
10.0k
            for (ch = 0; ch < 2; ch++) {
180
33.6k
                for (sb = 0; sb < subbands; sb++) {
181
26.9k
                    if (frame->scale_factor[ch][sb] == 0)
182
231
                        bitneed[ch][sb] = -5;
183
26.6k
                    else {
184
26.6k
                        if (subbands == 4)
185
26.4k
                            loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb];
186
223
                        else
187
223
                            loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb];
188
26.6k
                        if (loudness > 0)
189
23.4k
                            bitneed[ch][sb] = loudness / 2;
190
3.28k
                        else
191
3.28k
                            bitneed[ch][sb] = loudness;
192
26.6k
                    }
193
26.9k
                    if (bitneed[ch][sb] > max_bitneed)
194
3.38k
                        max_bitneed = bitneed[ch][sb];
195
26.9k
                }
196
6.68k
            }
197
3.34k
        }
198
199
11.3k
        bitcount = 0;
200
11.3k
        slicecount = 0;
201
11.3k
        bitslice = max_bitneed + 1;
202
29.5k
        do {
203
29.5k
            bitslice--;
204
29.5k
            bitcount += slicecount;
205
29.5k
            slicecount = 0;
206
88.6k
            for (ch = 0; ch < 2; ch++) {
207
366k
                for (sb = 0; sb < subbands; sb++) {
208
307k
                    if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16))
209
49.3k
                        slicecount++;
210
257k
                    else if (bitneed[ch][sb] == bitslice + 1)
211
20.9k
                        slicecount += 2;
212
307k
                }
213
59.0k
            }
214
29.5k
        } while (bitcount + slicecount < frame->bitpool);
215
216
11.3k
        if (bitcount + slicecount == frame->bitpool) {
217
7.99k
            bitcount += slicecount;
218
7.99k
            bitslice--;
219
7.99k
        }
220
221
34.1k
        for (ch = 0; ch < 2; ch++) {
222
178k
            for (sb = 0; sb < subbands; sb++) {
223
155k
                if (bitneed[ch][sb] < bitslice + 2) {
224
137k
                    bits[ch][sb] = 0;
225
137k
                } else {
226
17.6k
                    bits[ch][sb] = bitneed[ch][sb] - bitslice;
227
17.6k
                    if (bits[ch][sb] > 16)
228
123
                        bits[ch][sb] = 16;
229
17.6k
                }
230
155k
            }
231
22.7k
        }
232
233
11.3k
        ch = 0;
234
11.3k
        sb = 0;
235
18.6k
        while (bitcount < frame->bitpool) {
236
7.26k
            if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) {
237
7.00k
                bits[ch][sb]++;
238
7.00k
                bitcount++;
239
7.00k
            } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) {
240
44
                bits[ch][sb] = 2;
241
44
                bitcount += 2;
242
44
            }
243
7.26k
            if (ch == 1) {
244
3.58k
                ch = 0;
245
3.58k
                sb++;
246
3.58k
                if (sb >= subbands)
247
5
                    break;
248
3.58k
            } else
249
3.67k
                ch = 1;
250
7.26k
        }
251
252
11.3k
        ch = 0;
253
11.3k
        sb = 0;
254
11.4k
        while (bitcount < frame->bitpool) {
255
5
            if (bits[ch][sb] < 16) {
256
5
                bits[ch][sb]++;
257
5
                bitcount++;
258
5
            }
259
5
            if (ch == 1) {
260
0
                ch = 0;
261
0
                sb++;
262
0
                if (sb >= subbands)
263
0
                    break;
264
0
            } else
265
5
                ch = 1;
266
5
        }
267
268
11.3k
    }
269
270
18.3k
}