FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sbcdec.c
Date: 2026-04-10 22:11:37
Exec Total Coverage
Lines: 145 166 87.3%
Functions: 6 6 100.0%
Branches: 84 101 83.2%

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-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 decoder implementation
31 */
32
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "decode.h"
36 #include "libavutil/channel_layout.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/mem_internal.h"
39 #include "sbc.h"
40 #include "sbcdec_data.h"
41
42 struct sbc_decoder_state {
43 int32_t V[2][170];
44 int offset[2][16];
45 };
46
47 typedef struct SBCDecContext {
48 DECLARE_ALIGNED(SBC_ALIGN, struct sbc_frame, frame);
49 DECLARE_ALIGNED(SBC_ALIGN, struct sbc_decoder_state, dsp);
50 } SBCDecContext;
51
52 /*
53 * Unpacks a SBC frame at the beginning of the stream in data,
54 * which has at most len bytes into frame.
55 * Returns the length in bytes of the packed frame, or a negative
56 * value on error. The error codes are:
57 *
58 * -1 Data stream too short
59 * -2 Sync byte incorrect
60 * -3 CRC8 incorrect
61 * -4 Bitpool value out of bounds
62 */
63 33226 static int sbc_unpack_frame(const uint8_t *data, struct sbc_frame *frame,
64 size_t len)
65 {
66 unsigned int consumed;
67 /* Will copy the parts of the header that are relevant to crc
68 * calculation here */
69 33226 uint8_t crc_header[11] = { 0 };
70 int crc_pos;
71 int32_t temp;
72
73 uint32_t audio_sample;
74 int ch, sb, blk, bit; /* channel, subband, block and bit standard
75 counters */
76 int bits[2][8]; /* bits distribution */
77 uint32_t levels[2][8]; /* levels derived from that */
78
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33226 times.
33226 if (len < 4)
80 return -1;
81
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33226 times.
33226 if (data[0] == MSBC_SYNCWORD) {
83 if (data[1] != 0)
84 return -2;
85 if (data[2] != 0)
86 return -2;
87
88 frame->frequency = SBC_FREQ_16000;
89 frame->blocks = MSBC_BLOCKS;
90 frame->allocation = LOUDNESS;
91 frame->mode = MONO;
92 frame->channels = 1;
93 frame->subbands = 8;
94 frame->bitpool = 26;
95
1/2
✓ Branch 0 taken 33226 times.
✗ Branch 1 not taken.
33226 } else if (data[0] == SBC_SYNCWORD) {
96 33226 frame->frequency = (data[1] >> 6) & 0x03;
97 33226 frame->blocks = 4 * ((data[1] >> 4) & 0x03) + 4;
98 33226 frame->mode = (data[1] >> 2) & 0x03;
99
2/2
✓ Branch 0 taken 20820 times.
✓ Branch 1 taken 12406 times.
33226 frame->channels = frame->mode == MONO ? 1 : 2;
100 33226 frame->allocation = (data[1] >> 1) & 0x01;
101
2/2
✓ Branch 0 taken 6955 times.
✓ Branch 1 taken 26271 times.
33226 frame->subbands = data[1] & 0x01 ? 8 : 4;
102 33226 frame->bitpool = data[2];
103
104
3/4
✓ Branch 0 taken 12406 times.
✓ Branch 1 taken 20820 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 12406 times.
33226 if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20820 times.
20820 frame->bitpool > 16 * frame->subbands)
106 return -4;
107
108
4/4
✓ Branch 0 taken 27023 times.
✓ Branch 1 taken 6203 times.
✓ Branch 2 taken 6203 times.
✓ Branch 3 taken 20820 times.
33226 if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12406 times.
12406 frame->bitpool > 32 * frame->subbands)
110 return -4;
111 } else
112 return -2;
113
114 33226 consumed = 32;
115 33226 crc_header[0] = data[1];
116 33226 crc_header[1] = data[2];
117 33226 crc_pos = 16;
118
119
2/2
✓ Branch 0 taken 6203 times.
✓ Branch 1 taken 27023 times.
33226 if (frame->mode == JOINT_STEREO) {
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6203 times.
6203 if (len * 8 < consumed + frame->subbands)
121 return -1;
122
123 6203 frame->joint = 0x00;
124
2/2
✓ Branch 0 taken 26881 times.
✓ Branch 1 taken 6203 times.
33084 for (sb = 0; sb < frame->subbands - 1; sb++)
125 26881 frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
126
2/2
✓ Branch 0 taken 4135 times.
✓ Branch 1 taken 2068 times.
6203 if (frame->subbands == 4)
127 4135 crc_header[crc_pos / 8] = data[4] & 0xf0;
128 else
129 2068 crc_header[crc_pos / 8] = data[4];
130
131 6203 consumed += frame->subbands;
132 6203 crc_pos += frame->subbands;
133 }
134
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33226 times.
33226 if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
136 return -1;
137
138
2/2
✓ Branch 0 taken 45632 times.
✓ Branch 1 taken 33226 times.
78858 for (ch = 0; ch < frame->channels; ch++) {
139
2/2
✓ Branch 0 taken 226892 times.
✓ Branch 1 taken 45632 times.
272524 for (sb = 0; sb < frame->subbands; sb++) {
140 /* FIXME assert(consumed % 4 == 0); */
141 226892 frame->scale_factor[ch][sb] =
142 226892 (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
143 226892 crc_header[crc_pos >> 3] |=
144 226892 frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
145
146 226892 consumed += 4;
147 226892 crc_pos += 4;
148 }
149 }
150
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33226 times.
33226 if (data[3] != ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos))
152 return -3;
153
154 33226 ff_sbc_calculate_bits(frame, bits);
155
156
2/2
✓ Branch 0 taken 45632 times.
✓ Branch 1 taken 33226 times.
78858 for (ch = 0; ch < frame->channels; ch++) {
157
2/2
✓ Branch 0 taken 226892 times.
✓ Branch 1 taken 45632 times.
272524 for (sb = 0; sb < frame->subbands; sb++)
158 226892 levels[ch][sb] = (1 << bits[ch][sb]) - 1;
159 }
160
161
2/2
✓ Branch 0 taken 315604 times.
✓ Branch 1 taken 33226 times.
348830 for (blk = 0; blk < frame->blocks; blk++) {
162
2/2
✓ Branch 0 taken 514100 times.
✓ Branch 1 taken 315604 times.
829704 for (ch = 0; ch < frame->channels; ch++) {
163
2/2
✓ Branch 0 taken 2766224 times.
✓ Branch 1 taken 514100 times.
3280324 for (sb = 0; sb < frame->subbands; sb++) {
164 uint32_t shift;
165
166
2/2
✓ Branch 0 taken 1320004 times.
✓ Branch 1 taken 1446220 times.
2766224 if (levels[ch][sb] == 0) {
167 1320004 frame->sb_sample[blk][ch][sb] = 0;
168 1320004 continue;
169 }
170
171 1446220 shift = frame->scale_factor[ch][sb] +
172 1 + SBCDEC_FIXED_EXTRA_BITS;
173
174 1446220 audio_sample = 0;
175
2/2
✓ Branch 0 taken 8703752 times.
✓ Branch 1 taken 1446220 times.
10149972 for (bit = 0; bit < bits[ch][sb]; bit++) {
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8703752 times.
8703752 if (consumed > len * 8)
177 return -1;
178
179
2/2
✓ Branch 0 taken 4296180 times.
✓ Branch 1 taken 4407572 times.
8703752 if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
180 4296180 audio_sample |= 1 << (bits[ch][sb] - bit - 1);
181
182 8703752 consumed++;
183 }
184
185 1446220 frame->sb_sample[blk][ch][sb] = (int32_t)
186 1446220 (((((uint64_t) audio_sample << 1) | 1) << shift) /
187 1446220 levels[ch][sb]) - (1 << shift);
188 }
189 }
190 }
191
192
2/2
✓ Branch 0 taken 6203 times.
✓ Branch 1 taken 27023 times.
33226 if (frame->mode == JOINT_STEREO) {
193
2/2
✓ Branch 0 taken 99248 times.
✓ Branch 1 taken 6203 times.
105451 for (blk = 0; blk < frame->blocks; blk++) {
194
2/2
✓ Branch 0 taken 529344 times.
✓ Branch 1 taken 99248 times.
628592 for (sb = 0; sb < frame->subbands; sb++) {
195
2/2
✓ Branch 0 taken 199264 times.
✓ Branch 1 taken 330080 times.
529344 if (frame->joint & (0x01 << sb)) {
196 199264 temp = frame->sb_sample[blk][0][sb] +
197 199264 frame->sb_sample[blk][1][sb];
198 199264 frame->sb_sample[blk][1][sb] =
199 199264 frame->sb_sample[blk][0][sb] -
200 199264 frame->sb_sample[blk][1][sb];
201 199264 frame->sb_sample[blk][0][sb] = temp;
202 }
203 }
204 }
205 }
206
207
2/2
✓ Branch 0 taken 4135 times.
✓ Branch 1 taken 29091 times.
33226 if ((consumed & 0x7) != 0)
208 4135 consumed += 8 - (consumed & 0x7);
209
210 33226 return consumed >> 3;
211 }
212
213 336644 static inline void sbc_synthesize_four(struct sbc_decoder_state *state,
214 struct sbc_frame *frame,
215 int ch, int blk, AVFrame *output_frame)
216 {
217 int i, k, idx;
218 336644 int32_t *v = state->V[ch];
219 336644 int *offset = state->offset[ch];
220
221
2/2
✓ Branch 0 taken 2693152 times.
✓ Branch 1 taken 336644 times.
3029796 for (i = 0; i < 8; i++) {
222 /* Shifting */
223 2693152 offset[i]--;
224
2/2
✓ Branch 0 taken 33659 times.
✓ Branch 1 taken 2659493 times.
2693152 if (offset[i] < 0) {
225 33659 offset[i] = 79;
226 33659 memcpy(v + 80, v, 9 * sizeof(*v));
227 }
228
229 /* Distribute the new matrix value to the shifted position */
230 2693152 v[offset[i]] =
231 2693152 (int)( (unsigned)synmatrix4[i][0] * frame->sb_sample[blk][ch][0] +
232 2693152 (unsigned)synmatrix4[i][1] * frame->sb_sample[blk][ch][1] +
233 2693152 (unsigned)synmatrix4[i][2] * frame->sb_sample[blk][ch][2] +
234 2693152 (unsigned)synmatrix4[i][3] * frame->sb_sample[blk][ch][3] ) >> 15;
235 }
236
237 /* Compute the samples */
238
2/2
✓ Branch 0 taken 1346576 times.
✓ Branch 1 taken 336644 times.
1683220 for (idx = 0, i = 0; i < 4; i++, idx += 5) {
239 1346576 k = (i + 4) & 0xf;
240
241 /* Store in output, Q0 */
242 1346576 AV_WN16A(&output_frame->data[ch][blk * 8 + i * 2], av_clip_int16(
243 (int)( (unsigned)v[offset[i] + 0] * sbc_proto_4_40m0[idx + 0] +
244 (unsigned)v[offset[k] + 1] * sbc_proto_4_40m1[idx + 0] +
245 (unsigned)v[offset[i] + 2] * sbc_proto_4_40m0[idx + 1] +
246 (unsigned)v[offset[k] + 3] * sbc_proto_4_40m1[idx + 1] +
247 (unsigned)v[offset[i] + 4] * sbc_proto_4_40m0[idx + 2] +
248 (unsigned)v[offset[k] + 5] * sbc_proto_4_40m1[idx + 2] +
249 (unsigned)v[offset[i] + 6] * sbc_proto_4_40m0[idx + 3] +
250 (unsigned)v[offset[k] + 7] * sbc_proto_4_40m1[idx + 3] +
251 (unsigned)v[offset[i] + 8] * sbc_proto_4_40m0[idx + 4] +
252 (unsigned)v[offset[k] + 9] * sbc_proto_4_40m1[idx + 4] ) >> 15));
253 }
254 336644 }
255
256 177456 static inline void sbc_synthesize_eight(struct sbc_decoder_state *state,
257 struct sbc_frame *frame,
258 int ch, int blk, AVFrame *output_frame)
259 {
260 int i, k, idx;
261 177456 int32_t *v = state->V[ch];
262 177456 int *offset = state->offset[ch];
263
264
2/2
✓ Branch 0 taken 2839296 times.
✓ Branch 1 taken 177456 times.
3016752 for (i = 0; i < 16; i++) {
265 /* Shifting */
266 2839296 offset[i]--;
267
2/2
✓ Branch 0 taken 17740 times.
✓ Branch 1 taken 2821556 times.
2839296 if (offset[i] < 0) {
268 17740 offset[i] = 159;
269 17740 memcpy(v + 160, v, 9 * sizeof(*v));
270 }
271
272 /* Distribute the new matrix value to the shifted position */
273 2839296 v[offset[i]] =
274 2839296 (int)( (unsigned)synmatrix8[i][0] * frame->sb_sample[blk][ch][0] +
275 2839296 (unsigned)synmatrix8[i][1] * frame->sb_sample[blk][ch][1] +
276 2839296 (unsigned)synmatrix8[i][2] * frame->sb_sample[blk][ch][2] +
277 2839296 (unsigned)synmatrix8[i][3] * frame->sb_sample[blk][ch][3] +
278 2839296 (unsigned)synmatrix8[i][4] * frame->sb_sample[blk][ch][4] +
279 2839296 (unsigned)synmatrix8[i][5] * frame->sb_sample[blk][ch][5] +
280 2839296 (unsigned)synmatrix8[i][6] * frame->sb_sample[blk][ch][6] +
281 2839296 (unsigned)synmatrix8[i][7] * frame->sb_sample[blk][ch][7] ) >> 15;
282 }
283
284 /* Compute the samples */
285
2/2
✓ Branch 0 taken 1419648 times.
✓ Branch 1 taken 177456 times.
1597104 for (idx = 0, i = 0; i < 8; i++, idx += 5) {
286 1419648 k = (i + 8) & 0xf;
287
288 /* Store in output, Q0 */
289 1419648 AV_WN16A(&output_frame->data[ch][blk * 16 + i * 2], av_clip_int16(
290 (int)( (unsigned)v[offset[i] + 0] * sbc_proto_8_80m0[idx + 0] +
291 (unsigned)v[offset[k] + 1] * sbc_proto_8_80m1[idx + 0] +
292 (unsigned)v[offset[i] + 2] * sbc_proto_8_80m0[idx + 1] +
293 (unsigned)v[offset[k] + 3] * sbc_proto_8_80m1[idx + 1] +
294 (unsigned)v[offset[i] + 4] * sbc_proto_8_80m0[idx + 2] +
295 (unsigned)v[offset[k] + 5] * sbc_proto_8_80m1[idx + 2] +
296 (unsigned)v[offset[i] + 6] * sbc_proto_8_80m0[idx + 3] +
297 (unsigned)v[offset[k] + 7] * sbc_proto_8_80m1[idx + 3] +
298 (unsigned)v[offset[i] + 8] * sbc_proto_8_80m0[idx + 4] +
299 (unsigned)v[offset[k] + 9] * sbc_proto_8_80m1[idx + 4] ) >> 15));
300 }
301 177456 }
302
303 33226 static void sbc_synthesize_audio(struct sbc_decoder_state *state,
304 struct sbc_frame *frame, AVFrame *output_frame)
305 {
306 int ch, blk;
307
308
2/3
✓ Branch 0 taken 26271 times.
✓ Branch 1 taken 6955 times.
✗ Branch 2 not taken.
33226 switch (frame->subbands) {
309 26271 case 4:
310
2/2
✓ Branch 0 taken 34541 times.
✓ Branch 1 taken 26271 times.
60812 for (ch = 0; ch < frame->channels; ch++)
311
2/2
✓ Branch 0 taken 336644 times.
✓ Branch 1 taken 34541 times.
371185 for (blk = 0; blk < frame->blocks; blk++)
312 336644 sbc_synthesize_four(state, frame, ch, blk, output_frame);
313 26271 break;
314
315 6955 case 8:
316
2/2
✓ Branch 0 taken 11091 times.
✓ Branch 1 taken 6955 times.
18046 for (ch = 0; ch < frame->channels; ch++)
317
2/2
✓ Branch 0 taken 177456 times.
✓ Branch 1 taken 11091 times.
188547 for (blk = 0; blk < frame->blocks; blk++)
318 177456 sbc_synthesize_eight(state, frame, ch, blk, output_frame);
319 6955 break;
320 }
321 33226 }
322
323 14 static av_cold int sbc_decode_init(AVCodecContext *avctx)
324 {
325 14 SBCDecContext *sbc = avctx->priv_data;
326 int i, ch;
327
328 14 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
329
330 14 sbc->frame.crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
331
332 14 memset(sbc->dsp.V, 0, sizeof(sbc->dsp.V));
333
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 for (ch = 0; ch < 2; ch++)
334
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 28 times.
476 for (i = 0; i < FF_ARRAY_ELEMS(sbc->dsp.offset[0]); i++)
335 448 sbc->dsp.offset[ch][i] = (10 * i + 10);
336 14 return 0;
337 }
338
339 33226 static int sbc_decode_frame(AVCodecContext *avctx, AVFrame *frame,
340 int *got_frame_ptr, AVPacket *avpkt)
341 {
342 33226 SBCDecContext *sbc = avctx->priv_data;
343 int ret, frame_length;
344
345 33226 frame_length = sbc_unpack_frame(avpkt->data, &sbc->frame, avpkt->size);
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33226 times.
33226 if (frame_length <= 0)
347 return frame_length;
348
349 33226 av_channel_layout_uninit(&avctx->ch_layout);
350 33226 avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
351 33226 avctx->ch_layout.nb_channels = sbc->frame.channels;
352
353 33226 frame->nb_samples = sbc->frame.blocks * sbc->frame.subbands;
354
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33226 times.
33226 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
355 return ret;
356
357 33226 sbc_synthesize_audio(&sbc->dsp, &sbc->frame, frame);
358
359 33226 *got_frame_ptr = 1;
360
361 33226 return frame_length;
362 }
363
364 const FFCodec ff_sbc_decoder = {
365 .p.name = "sbc",
366 CODEC_LONG_NAME("SBC (low-complexity subband codec)"),
367 .p.type = AVMEDIA_TYPE_AUDIO,
368 .p.id = AV_CODEC_ID_SBC,
369 .priv_data_size = sizeof(SBCDecContext),
370 .init = sbc_decode_init,
371 FF_CODEC_DECODE_CB(sbc_decode_frame),
372 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
373 };
374