FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sbcenc.c
Date: 2026-04-25 22:17:55
Exec Total Coverage
Lines: 139 163 85.3%
Functions: 4 4 100.0%
Branches: 66 81 81.5%

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 encoder implementation
31 */
32
33 #include "libavutil/channel_layout.h"
34 #include "libavutil/opt.h"
35 #include "avcodec.h"
36 #include "codec_internal.h"
37 #include "encode.h"
38 #include "profiles.h"
39 #include "put_bits.h"
40 #include "sbc.h"
41 #include "sbcdsp.h"
42
43 typedef struct SBCEncContext {
44 AVClass *class;
45 int64_t max_delay;
46 int msbc;
47 DECLARE_ALIGNED(SBC_ALIGN, struct sbc_frame, frame);
48 DECLARE_ALIGNED(SBC_ALIGN, SBCDSPContext, dsp);
49 } SBCEncContext;
50
51 static const int sbc_samplerates[] = { 16000, 32000, 44100, 48000, 0 };
52
53 33219 static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
54 {
55 int ch, blk;
56 int16_t *x;
57
58
2/3
✓ Branch 0 taken 26268 times.
✓ Branch 1 taken 6951 times.
✗ Branch 2 not taken.
33219 switch (frame->subbands) {
59 26268 case 4:
60
2/2
✓ Branch 0 taken 34536 times.
✓ Branch 1 taken 26268 times.
60804 for (ch = 0; ch < frame->channels; ch++) {
61 34536 x = &s->X[ch][s->position - 4 *
62 34536 s->increment + frame->blocks * 4];
63
2/2
✓ Branch 0 taken 84144 times.
✓ Branch 1 taken 34536 times.
118680 for (blk = 0; blk < frame->blocks;
64 84144 blk += s->increment) {
65 84144 s->sbc_analyze_4s(
66 s, x,
67 84144 frame->sb_sample_f[blk][ch],
68 frame->sb_sample_f[blk + 1][ch] -
69 frame->sb_sample_f[blk][ch]);
70 84144 x -= 4 * s->increment;
71 }
72 }
73 26268 return frame->blocks * 4;
74
75 6951 case 8:
76
2/2
✓ Branch 0 taken 11085 times.
✓ Branch 1 taken 6951 times.
18036 for (ch = 0; ch < frame->channels; ch++) {
77 11085 x = &s->X[ch][s->position - 8 *
78 11085 s->increment + frame->blocks * 8];
79
2/2
✓ Branch 0 taken 44340 times.
✓ Branch 1 taken 11085 times.
55425 for (blk = 0; blk < frame->blocks;
80 44340 blk += s->increment) {
81 44340 s->sbc_analyze_8s(
82 s, x,
83 44340 frame->sb_sample_f[blk][ch],
84 frame->sb_sample_f[blk + 1][ch] -
85 frame->sb_sample_f[blk][ch]);
86 44340 x -= 8 * s->increment;
87 }
88 }
89 6951 return frame->blocks * 8;
90
91 default:
92 return AVERROR(EIO);
93 }
94 }
95
96 /*
97 * Packs the SBC frame from frame into the memory in avpkt.
98 * Returns the length of the packed frame.
99 */
100 33219 static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
101 int joint, int msbc)
102 {
103 PutBitContext pb;
104
105 /* Will copy the header parts for CRC-8 calculation here */
106 33219 uint8_t crc_header[11] = { 0 };
107 int crc_pos;
108
109 uint32_t audio_sample;
110
111 int ch, sb, blk; /* channel, subband, block and bit counters */
112 int bits[2][8]; /* bits distribution */
113 uint32_t levels[2][8]; /* levels are derived from that */
114 uint32_t sb_sample_delta[2][8];
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33219 times.
33219 if (msbc) {
117 avpkt->data[0] = MSBC_SYNCWORD;
118 avpkt->data[1] = 0;
119 avpkt->data[2] = 0;
120 } else {
121 33219 avpkt->data[0] = SBC_SYNCWORD;
122
123 33219 avpkt->data[1] = (frame->frequency & 0x03) << 6;
124 33219 avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4;
125 33219 avpkt->data[1] |= (frame->mode & 0x03) << 2;
126 33219 avpkt->data[1] |= (frame->allocation & 0x01) << 1;
127 33219 avpkt->data[1] |= ((frame->subbands == 8) & 0x01) << 0;
128
129 33219 avpkt->data[2] = frame->bitpool;
130 }
131
132 /* Can't fill in crc yet */
133 33219 crc_header[0] = avpkt->data[1];
134 33219 crc_header[1] = avpkt->data[2];
135 33219 crc_pos = 16;
136
137 33219 init_put_bits(&pb, avpkt->data + 4, avpkt->size - 4);
138
139
2/2
✓ Branch 0 taken 6201 times.
✓ Branch 1 taken 27018 times.
33219 if (frame->mode == JOINT_STEREO) {
140 6201 put_bits(&pb, frame->subbands, joint);
141 6201 crc_header[crc_pos >> 3] = joint;
142 6201 crc_pos += frame->subbands;
143 }
144
145
2/2
✓ Branch 0 taken 45621 times.
✓ Branch 1 taken 33219 times.
78840 for (ch = 0; ch < frame->channels; ch++) {
146
2/2
✓ Branch 0 taken 226824 times.
✓ Branch 1 taken 45621 times.
272445 for (sb = 0; sb < frame->subbands; sb++) {
147 226824 put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F);
148 226824 crc_header[crc_pos >> 3] <<= 4;
149 226824 crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
150 226824 crc_pos += 4;
151 }
152 }
153
154 /* align the last crc byte */
155
2/2
✓ Branch 0 taken 4134 times.
✓ Branch 1 taken 29085 times.
33219 if (crc_pos % 8)
156 4134 crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
157
158 33219 avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos);
159
160 33219 ff_sbc_calculate_bits(frame, bits);
161
162
2/2
✓ Branch 0 taken 45621 times.
✓ Branch 1 taken 33219 times.
78840 for (ch = 0; ch < frame->channels; ch++) {
163
2/2
✓ Branch 0 taken 226824 times.
✓ Branch 1 taken 45621 times.
272445 for (sb = 0; sb < frame->subbands; sb++) {
164 226824 levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
165 226824 (32 - (frame->scale_factor[ch][sb] +
166 SCALE_OUT_BITS + 2));
167 226824 sb_sample_delta[ch][sb] = (uint32_t) 1 <<
168 226824 (frame->scale_factor[ch][sb] +
169 226824 SCALE_OUT_BITS + 1);
170 }
171 }
172
173
2/2
✓ Branch 0 taken 315504 times.
✓ Branch 1 taken 33219 times.
348723 for (blk = 0; blk < frame->blocks; blk++) {
174
2/2
✓ Branch 0 taken 513936 times.
✓ Branch 1 taken 315504 times.
829440 for (ch = 0; ch < frame->channels; ch++) {
175
2/2
✓ Branch 0 taken 2765184 times.
✓ Branch 1 taken 513936 times.
3279120 for (sb = 0; sb < frame->subbands; sb++) {
176
177
2/2
✓ Branch 0 taken 1319540 times.
✓ Branch 1 taken 1445644 times.
2765184 if (bits[ch][sb] == 0)
178 1319540 continue;
179
180 1445644 audio_sample = ((uint64_t) levels[ch][sb] *
181 1445644 (sb_sample_delta[ch][sb] +
182 1445644 frame->sb_sample_f[blk][ch][sb])) >> 32;
183
184 1445644 put_bits(&pb, bits[ch][sb], audio_sample);
185 }
186 }
187 }
188
189 33219 flush_put_bits(&pb);
190
191 33219 return put_bytes_output(&pb);
192 }
193
194 7 static av_cold int sbc_encode_init(AVCodecContext *avctx)
195 {
196 7 SBCEncContext *sbc = avctx->priv_data;
197 7 struct sbc_frame *frame = &sbc->frame;
198
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (avctx->profile == AV_PROFILE_SBC_MSBC)
200 sbc->msbc = 1;
201
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (sbc->msbc) {
203 if (avctx->ch_layout.nb_channels != 1) {
204 av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n");
205 return AVERROR(EINVAL);
206 }
207
208 if (avctx->sample_rate != 16000) {
209 av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n");
210 return AVERROR(EINVAL);
211 }
212
213 frame->mode = SBC_MODE_MONO;
214 frame->subbands = 8;
215 frame->blocks = MSBC_BLOCKS;
216 frame->allocation = SBC_AM_LOUDNESS;
217 frame->bitpool = 26;
218
219 avctx->frame_size = 8 * MSBC_BLOCKS;
220 } else {
221 int d;
222
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (avctx->global_quality > 255*FF_QP2LAMBDA) {
224 av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n");
225 return AVERROR(EINVAL);
226 }
227
228
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (avctx->ch_layout.nb_channels == 1) {
229 3 frame->mode = SBC_MODE_MONO;
230
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
3 if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000)
231 1 frame->subbands = 4;
232 else
233 2 frame->subbands = 8;
234 } else {
235
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000)
236 2 frame->mode = SBC_MODE_JOINT_STEREO;
237 else
238 2 frame->mode = SBC_MODE_STEREO;
239
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000)
240 2 frame->subbands = 4;
241 else
242 2 frame->subbands = 8;
243 }
244 /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
245 7 frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2)
246 7 / (1000000 * frame->subbands)) - 10, 4, 16) & ~3;
247
248 7 frame->allocation = SBC_AM_LOUDNESS;
249
250
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1);
251 7 frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate)
252 7 - 4 * frame->subbands * avctx->ch_layout.nb_channels
253 7 - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d;
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (avctx->global_quality > 0)
255 frame->bitpool = avctx->global_quality / FF_QP2LAMBDA;
256
257
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO
258
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
7 || frame->mode == JOINT_STEREO))) {
259 av_log(avctx, AV_LOG_ERROR, "Invalid parameter combination\n");
260 return AVERROR_PATCHWELCOME;
261 }
262
263 7 avctx->frame_size = 4*((frame->subbands >> 3) + 1) * 4*(frame->blocks >> 2);
264 }
265
266
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 7 times.
35 for (int i = 0; sbc_samplerates[i]; i++)
267
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 21 times.
28 if (avctx->sample_rate == sbc_samplerates[i])
268 7 frame->frequency = i;
269
270 7 frame->channels = avctx->ch_layout.nb_channels;
271 7 frame->codesize = frame->subbands * frame->blocks * avctx->ch_layout.nb_channels * 2;
272 7 frame->crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
273
274 7 sbc->dsp.position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 sbc->dsp.increment = sbc->msbc ? 1 : 4;
276 7 ff_sbcdsp_init(&sbc->dsp);
277
278 7 return 0;
279 }
280
281 33224 static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
282 const AVFrame *av_frame, int *got_packet_ptr)
283 {
284 33224 SBCEncContext *sbc = avctx->priv_data;
285 33224 struct sbc_frame *frame = &sbc->frame;
286 33224 uint8_t joint = frame->mode == SBC_MODE_JOINT_STEREO;
287 33224 uint8_t dual = frame->mode == SBC_MODE_DUAL_CHANNEL;
288 33224 int ret, j = 0;
289
290 33224 int frame_length = 4 + (4 * frame->subbands * frame->channels) / 8
291 33224 + ((frame->blocks * frame->bitpool * (1 + dual)
292 33224 + joint * frame->subbands) + 7) / 8;
293
294 /* input must be large enough to encode a complete frame */
295
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 33219 times.
33224 if (av_frame->nb_samples * frame->channels * 2 < frame->codesize)
296 5 return 0;
297
298
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33219 times.
33219 if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_length, 0)) < 0)
299 return ret;
300
301 /* Select the needed input data processing function and call it */
302
2/2
✓ Branch 0 taken 6951 times.
✓ Branch 1 taken 26268 times.
33219 if (frame->subbands == 8)
303 6951 sbc->dsp.position = sbc->dsp.sbc_enc_process_input_8s(
304 6951 sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
305 6951 frame->subbands * frame->blocks, frame->channels);
306 else
307 26268 sbc->dsp.position = sbc->dsp.sbc_enc_process_input_4s(
308 26268 sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
309 26268 frame->subbands * frame->blocks, frame->channels);
310
311 33219 sbc_analyze_audio(&sbc->dsp, &sbc->frame);
312
313
2/2
✓ Branch 0 taken 6201 times.
✓ Branch 1 taken 27018 times.
33219 if (frame->mode == JOINT_STEREO)
314 6201 j = sbc->dsp.sbc_calc_scalefactors_j(frame->sb_sample_f,
315 6201 frame->scale_factor,
316 6201 frame->blocks,
317 6201 frame->subbands);
318 else
319 27018 sbc->dsp.sbc_calc_scalefactors(frame->sb_sample_f,
320 27018 frame->scale_factor,
321 27018 frame->blocks,
322 27018 frame->channels,
323 27018 frame->subbands);
324 33219 sbc_pack_frame(avpkt, frame, j, sbc->msbc);
325
326 33219 *got_packet_ptr = 1;
327 33219 return 0;
328 }
329
330 #define OFFSET(x) offsetof(SBCEncContext, x)
331 #define AE AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
332 static const AVOption options[] = {
333 { "sbc_delay", "set maximum algorithmic latency",
334 OFFSET(max_delay), AV_OPT_TYPE_DURATION, {.i64 = 13000}, 1000,13000, AE },
335 { "msbc", "use mSBC mode (wideband speech mono SBC)",
336 OFFSET(msbc), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, AE },
337 FF_AVCTX_PROFILE_OPTION("msbc", NULL, AUDIO, AV_PROFILE_SBC_MSBC)
338 { NULL },
339 };
340
341 static const AVClass sbc_class = {
342 .class_name = "sbc encoder",
343 .item_name = av_default_item_name,
344 .option = options,
345 .version = LIBAVUTIL_VERSION_INT,
346 };
347
348 const FFCodec ff_sbc_encoder = {
349 .p.name = "sbc",
350 CODEC_LONG_NAME("SBC (low-complexity subband codec)"),
351 .p.type = AVMEDIA_TYPE_AUDIO,
352 .p.id = AV_CODEC_ID_SBC,
353 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_SMALL_LAST_FRAME |
354 AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE,
355 .priv_data_size = sizeof(SBCEncContext),
356 .init = sbc_encode_init,
357 FF_CODEC_ENCODE_CB(sbc_encode_frame),
358 CODEC_CH_LAYOUTS(AV_CHANNEL_LAYOUT_MONO, AV_CHANNEL_LAYOUT_STEREO),
359 CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16),
360 CODEC_SAMPLERATES_ARRAY(sbc_samplerates),
361 .p.priv_class = &sbc_class,
362 .p.profiles = NULL_IF_CONFIG_SMALL(ff_sbc_profiles),
363 };
364