FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sbcenc.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 0 164 0.0%
Functions: 0 4 0.0%
Branches: 0 81 0.0%

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