Coverage Report

Created: 2026-05-23 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/sbcenc.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 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
static int sbc_analyze_audio(SBCDSPContext *s, struct sbc_frame *frame)
54
0
{
55
0
    int ch, blk;
56
0
    int16_t *x;
57
58
0
    switch (frame->subbands) {
59
0
    case 4:
60
0
        for (ch = 0; ch < frame->channels; ch++) {
61
0
            x = &s->X[ch][s->position - 4 *
62
0
                    s->increment + frame->blocks * 4];
63
0
            for (blk = 0; blk < frame->blocks;
64
0
                        blk += s->increment) {
65
0
                s->sbc_analyze_4s(
66
0
                    s, x,
67
0
                    frame->sb_sample_f[blk][ch],
68
0
                    frame->sb_sample_f[blk + 1][ch] -
69
0
                    frame->sb_sample_f[blk][ch]);
70
0
                x -= 4 * s->increment;
71
0
            }
72
0
        }
73
0
        return frame->blocks * 4;
74
75
0
    case 8:
76
0
        for (ch = 0; ch < frame->channels; ch++) {
77
0
            x = &s->X[ch][s->position - 8 *
78
0
                    s->increment + frame->blocks * 8];
79
0
            for (blk = 0; blk < frame->blocks;
80
0
                        blk += s->increment) {
81
0
                s->sbc_analyze_8s(
82
0
                    s, x,
83
0
                    frame->sb_sample_f[blk][ch],
84
0
                    frame->sb_sample_f[blk + 1][ch] -
85
0
                    frame->sb_sample_f[blk][ch]);
86
0
                x -= 8 * s->increment;
87
0
            }
88
0
        }
89
0
        return frame->blocks * 8;
90
91
0
    default:
92
0
        return AVERROR(EIO);
93
0
    }
94
0
}
95
96
/*
97
 * Packs the SBC frame from frame into the memory in avpkt.
98
 * Returns the length of the packed frame.
99
 */
100
static size_t sbc_pack_frame(AVPacket *avpkt, struct sbc_frame *frame,
101
                             int joint, int msbc)
102
0
{
103
0
    PutBitContext pb;
104
105
    /* Will copy the header parts for CRC-8 calculation here */
106
0
    uint8_t crc_header[11] = { 0 };
107
0
    int crc_pos;
108
109
0
    uint32_t audio_sample;
110
111
0
    int ch, sb, blk;        /* channel, subband, block and bit counters */
112
0
    int bits[2][8];         /* bits distribution */
113
0
    uint32_t levels[2][8];  /* levels are derived from that */
114
0
    uint32_t sb_sample_delta[2][8];
115
116
0
    if (msbc) {
117
0
        avpkt->data[0] = MSBC_SYNCWORD;
118
0
        avpkt->data[1] = 0;
119
0
        avpkt->data[2] = 0;
120
0
    } else {
121
0
        avpkt->data[0] = SBC_SYNCWORD;
122
123
0
        avpkt->data[1]  = (frame->frequency           & 0x03) << 6;
124
0
        avpkt->data[1] |= (((frame->blocks >> 2) - 1) & 0x03) << 4;
125
0
        avpkt->data[1] |= (frame->mode                & 0x03) << 2;
126
0
        avpkt->data[1] |= (frame->allocation          & 0x01) << 1;
127
0
        avpkt->data[1] |= ((frame->subbands == 8)     & 0x01) << 0;
128
129
0
        avpkt->data[2] = frame->bitpool;
130
0
    }
131
132
    /* Can't fill in crc yet */
133
0
    crc_header[0] = avpkt->data[1];
134
0
    crc_header[1] = avpkt->data[2];
135
0
    crc_pos = 16;
136
137
0
    init_put_bits(&pb, avpkt->data + 4, avpkt->size - 4);
138
139
0
    if (frame->mode == JOINT_STEREO) {
140
0
        put_bits(&pb, frame->subbands, joint);
141
0
        crc_header[crc_pos >> 3] = joint;
142
0
        crc_pos += frame->subbands;
143
0
    }
144
145
0
    for (ch = 0; ch < frame->channels; ch++) {
146
0
        for (sb = 0; sb < frame->subbands; sb++) {
147
0
            put_bits(&pb, 4, frame->scale_factor[ch][sb] & 0x0F);
148
0
            crc_header[crc_pos >> 3] <<= 4;
149
0
            crc_header[crc_pos >> 3] |= frame->scale_factor[ch][sb] & 0x0F;
150
0
            crc_pos += 4;
151
0
        }
152
0
    }
153
154
    /* align the last crc byte */
155
0
    if (crc_pos % 8)
156
0
        crc_header[crc_pos >> 3] <<= 8 - (crc_pos % 8);
157
158
0
    avpkt->data[3] = ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos);
159
160
0
    ff_sbc_calculate_bits(frame, bits);
161
162
0
    for (ch = 0; ch < frame->channels; ch++) {
163
0
        for (sb = 0; sb < frame->subbands; sb++) {
164
0
            levels[ch][sb] = ((1 << bits[ch][sb]) - 1) <<
165
0
                (32 - (frame->scale_factor[ch][sb] +
166
0
                    SCALE_OUT_BITS + 2));
167
0
            sb_sample_delta[ch][sb] = (uint32_t) 1 <<
168
0
                (frame->scale_factor[ch][sb] +
169
0
                    SCALE_OUT_BITS + 1);
170
0
        }
171
0
    }
172
173
0
    for (blk = 0; blk < frame->blocks; blk++) {
174
0
        for (ch = 0; ch < frame->channels; ch++) {
175
0
            for (sb = 0; sb < frame->subbands; sb++) {
176
177
0
                if (bits[ch][sb] == 0)
178
0
                    continue;
179
180
0
                audio_sample = ((uint64_t) levels[ch][sb] *
181
0
                    (sb_sample_delta[ch][sb] +
182
0
                    frame->sb_sample_f[blk][ch][sb])) >> 32;
183
184
0
                put_bits(&pb, bits[ch][sb], audio_sample);
185
0
            }
186
0
        }
187
0
    }
188
189
0
    flush_put_bits(&pb);
190
191
0
    return put_bytes_output(&pb);
192
0
}
193
194
static av_cold int sbc_encode_init(AVCodecContext *avctx)
195
0
{
196
0
    SBCEncContext *sbc = avctx->priv_data;
197
0
    struct sbc_frame *frame = &sbc->frame;
198
199
0
    if (avctx->profile == AV_PROFILE_SBC_MSBC)
200
0
        sbc->msbc = 1;
201
202
0
    if (sbc->msbc) {
203
0
        if (avctx->ch_layout.nb_channels != 1) {
204
0
            av_log(avctx, AV_LOG_ERROR, "mSBC require mono channel.\n");
205
0
            return AVERROR(EINVAL);
206
0
        }
207
208
0
        if (avctx->sample_rate != 16000) {
209
0
            av_log(avctx, AV_LOG_ERROR, "mSBC require 16 kHz samplerate.\n");
210
0
            return AVERROR(EINVAL);
211
0
        }
212
213
0
        frame->mode = SBC_MODE_MONO;
214
0
        frame->subbands = 8;
215
0
        frame->blocks = MSBC_BLOCKS;
216
0
        frame->allocation = SBC_AM_LOUDNESS;
217
0
        frame->bitpool = 26;
218
219
0
        avctx->frame_size = 8 * MSBC_BLOCKS;
220
0
    } else {
221
0
        int d;
222
223
0
        if (avctx->global_quality > 255*FF_QP2LAMBDA) {
224
0
            av_log(avctx, AV_LOG_ERROR, "bitpool > 255 is not allowed.\n");
225
0
            return AVERROR(EINVAL);
226
0
        }
227
228
0
        if (avctx->ch_layout.nb_channels == 1) {
229
0
            frame->mode = SBC_MODE_MONO;
230
0
            if (sbc->max_delay <= 3000 || avctx->bit_rate > 270000)
231
0
                frame->subbands = 4;
232
0
            else
233
0
                frame->subbands = 8;
234
0
        } else {
235
0
            if (avctx->bit_rate < 180000 || avctx->bit_rate > 420000)
236
0
                frame->mode = SBC_MODE_JOINT_STEREO;
237
0
            else
238
0
                frame->mode = SBC_MODE_STEREO;
239
0
            if (sbc->max_delay <= 4000 || avctx->bit_rate > 420000)
240
0
                frame->subbands = 4;
241
0
            else
242
0
                frame->subbands = 8;
243
0
        }
244
        /* sbc algorithmic delay is ((blocks + 10) * subbands - 2) / sample_rate */
245
0
        frame->blocks = av_clip(((sbc->max_delay * avctx->sample_rate + 2)
246
0
                               / (1000000 * frame->subbands)) - 10, 4, 16) & ~3;
247
248
0
        frame->allocation = SBC_AM_LOUDNESS;
249
250
0
        d = frame->blocks * ((frame->mode == SBC_MODE_DUAL_CHANNEL) + 1);
251
0
        frame->bitpool = (((avctx->bit_rate * frame->subbands * frame->blocks) / avctx->sample_rate)
252
0
                          - 4 * frame->subbands * avctx->ch_layout.nb_channels
253
0
                          - (frame->mode == SBC_MODE_JOINT_STEREO)*frame->subbands - 32 + d/2) / d;
254
0
        if (avctx->global_quality > 0)
255
0
            frame->bitpool = avctx->global_quality / FF_QP2LAMBDA;
256
257
0
        if (frame->bitpool > frame->subbands << (4 + (frame->mode == STEREO
258
0
                                                   || frame->mode == JOINT_STEREO))) {
259
0
            av_log(avctx, AV_LOG_ERROR, "Invalid parameter combination\n");
260
0
            return AVERROR_PATCHWELCOME;
261
0
        }
262
263
0
        avctx->frame_size = 4*((frame->subbands >> 3) + 1) * 4*(frame->blocks >> 2);
264
0
    }
265
266
0
    for (int i = 0; sbc_samplerates[i]; i++)
267
0
        if (avctx->sample_rate == sbc_samplerates[i])
268
0
            frame->frequency = i;
269
270
0
    frame->channels = avctx->ch_layout.nb_channels;
271
0
    frame->codesize = frame->subbands * frame->blocks * avctx->ch_layout.nb_channels * 2;
272
0
    frame->crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
273
274
0
    sbc->dsp.position = (SBC_X_BUFFER_SIZE - frame->subbands * 9) & ~7;
275
0
    sbc->dsp.increment = sbc->msbc ? 1 : 4;
276
0
    ff_sbcdsp_init(&sbc->dsp);
277
278
0
    return 0;
279
0
}
280
281
static int sbc_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
282
                            const AVFrame *av_frame, int *got_packet_ptr)
283
0
{
284
0
    SBCEncContext *sbc = avctx->priv_data;
285
0
    struct sbc_frame *frame = &sbc->frame;
286
0
    uint8_t joint = frame->mode == SBC_MODE_JOINT_STEREO;
287
0
    uint8_t dual  = frame->mode == SBC_MODE_DUAL_CHANNEL;
288
0
    int ret, j = 0;
289
290
0
    int frame_length = 4 + (4 * frame->subbands * frame->channels) / 8
291
0
                     + ((frame->blocks * frame->bitpool * (1 + dual)
292
0
                     + joint * frame->subbands) + 7) / 8;
293
294
    /* input must be large enough to encode a complete frame */
295
0
    if (av_frame->nb_samples * frame->channels * 2 < frame->codesize)
296
0
        return 0;
297
298
0
    if ((ret = ff_get_encode_buffer(avctx, avpkt, frame_length, 0)) < 0)
299
0
        return ret;
300
301
    /* Select the needed input data processing function and call it */
302
0
    if (frame->subbands == 8)
303
0
        sbc->dsp.position = sbc->dsp.sbc_enc_process_input_8s(
304
0
                sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
305
0
                frame->subbands * frame->blocks, frame->channels);
306
0
    else
307
0
        sbc->dsp.position = sbc->dsp.sbc_enc_process_input_4s(
308
0
                sbc->dsp.position, av_frame->data[0], sbc->dsp.X,
309
0
                frame->subbands * frame->blocks, frame->channels);
310
311
0
    sbc_analyze_audio(&sbc->dsp, &sbc->frame);
312
313
0
    if (frame->mode == JOINT_STEREO)
314
0
        j = sbc->dsp.sbc_calc_scalefactors_j(frame->sb_sample_f,
315
0
                                             frame->scale_factor,
316
0
                                             frame->blocks,
317
0
                                             frame->subbands);
318
0
    else
319
0
        sbc->dsp.sbc_calc_scalefactors(frame->sb_sample_f,
320
0
                                       frame->scale_factor,
321
0
                                       frame->blocks,
322
0
                                       frame->channels,
323
0
                                       frame->subbands);
324
0
    sbc_pack_frame(avpkt, frame, j, sbc->msbc);
325
326
0
    *got_packet_ptr = 1;
327
0
    return 0;
328
0
}
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
};