FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/sbcdec.c
Date: 2021-09-16 08:47:15
Exec Total Coverage
Lines: 0 164 0.0%
Branches: 0 103 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 decoder implementation
31 */
32
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "libavutil/channel_layout.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/mem_internal.h"
38 #include "sbc.h"
39 #include "sbcdec_data.h"
40
41 struct sbc_decoder_state {
42 int32_t V[2][170];
43 int offset[2][16];
44 };
45
46 typedef struct SBCDecContext {
47 AVClass *class;
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 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 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 if (len < 4)
80 return -1;
81
82 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 } else if (data[0] == SBC_SYNCWORD) {
96 frame->frequency = (data[1] >> 6) & 0x03;
97 frame->blocks = 4 * ((data[1] >> 4) & 0x03) + 4;
98 frame->mode = (data[1] >> 2) & 0x03;
99 frame->channels = frame->mode == MONO ? 1 : 2;
100 frame->allocation = (data[1] >> 1) & 0x01;
101 frame->subbands = data[1] & 0x01 ? 8 : 4;
102 frame->bitpool = data[2];
103
104 if ((frame->mode == MONO || frame->mode == DUAL_CHANNEL) &&
105 frame->bitpool > 16 * frame->subbands)
106 return -4;
107
108 if ((frame->mode == STEREO || frame->mode == JOINT_STEREO) &&
109 frame->bitpool > 32 * frame->subbands)
110 return -4;
111 } else
112 return -2;
113
114 consumed = 32;
115 crc_header[0] = data[1];
116 crc_header[1] = data[2];
117 crc_pos = 16;
118
119 if (frame->mode == JOINT_STEREO) {
120 if (len * 8 < consumed + frame->subbands)
121 return -1;
122
123 frame->joint = 0x00;
124 for (sb = 0; sb < frame->subbands - 1; sb++)
125 frame->joint |= ((data[4] >> (7 - sb)) & 0x01) << sb;
126 if (frame->subbands == 4)
127 crc_header[crc_pos / 8] = data[4] & 0xf0;
128 else
129 crc_header[crc_pos / 8] = data[4];
130
131 consumed += frame->subbands;
132 crc_pos += frame->subbands;
133 }
134
135 if (len * 8 < consumed + (4 * frame->subbands * frame->channels))
136 return -1;
137
138 for (ch = 0; ch < frame->channels; ch++) {
139 for (sb = 0; sb < frame->subbands; sb++) {
140 /* FIXME assert(consumed % 4 == 0); */
141 frame->scale_factor[ch][sb] =
142 (data[consumed >> 3] >> (4 - (consumed & 0x7))) & 0x0F;
143 crc_header[crc_pos >> 3] |=
144 frame->scale_factor[ch][sb] << (4 - (crc_pos & 0x7));
145
146 consumed += 4;
147 crc_pos += 4;
148 }
149 }
150
151 if (data[3] != ff_sbc_crc8(frame->crc_ctx, crc_header, crc_pos))
152 return -3;
153
154 ff_sbc_calculate_bits(frame, bits);
155
156 for (ch = 0; ch < frame->channels; ch++) {
157 for (sb = 0; sb < frame->subbands; sb++)
158 levels[ch][sb] = (1 << bits[ch][sb]) - 1;
159 }
160
161 for (blk = 0; blk < frame->blocks; blk++) {
162 for (ch = 0; ch < frame->channels; ch++) {
163 for (sb = 0; sb < frame->subbands; sb++) {
164 uint32_t shift;
165
166 if (levels[ch][sb] == 0) {
167 frame->sb_sample[blk][ch][sb] = 0;
168 continue;
169 }
170
171 shift = frame->scale_factor[ch][sb] +
172 1 + SBCDEC_FIXED_EXTRA_BITS;
173
174 audio_sample = 0;
175 for (bit = 0; bit < bits[ch][sb]; bit++) {
176 if (consumed > len * 8)
177 return -1;
178
179 if ((data[consumed >> 3] >> (7 - (consumed & 0x7))) & 0x01)
180 audio_sample |= 1 << (bits[ch][sb] - bit - 1);
181
182 consumed++;
183 }
184
185 frame->sb_sample[blk][ch][sb] = (int32_t)
186 (((((uint64_t) audio_sample << 1) | 1) << shift) /
187 levels[ch][sb]) - (1 << shift);
188 }
189 }
190 }
191
192 if (frame->mode == JOINT_STEREO) {
193 for (blk = 0; blk < frame->blocks; blk++) {
194 for (sb = 0; sb < frame->subbands; sb++) {
195 if (frame->joint & (0x01 << sb)) {
196 temp = frame->sb_sample[blk][0][sb] +
197 frame->sb_sample[blk][1][sb];
198 frame->sb_sample[blk][1][sb] =
199 frame->sb_sample[blk][0][sb] -
200 frame->sb_sample[blk][1][sb];
201 frame->sb_sample[blk][0][sb] = temp;
202 }
203 }
204 }
205 }
206
207 if ((consumed & 0x7) != 0)
208 consumed += 8 - (consumed & 0x7);
209
210 return consumed >> 3;
211 }
212
213 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 int32_t *v = state->V[ch];
219 int *offset = state->offset[ch];
220
221 for (i = 0; i < 8; i++) {
222 /* Shifting */
223 offset[i]--;
224 if (offset[i] < 0) {
225 offset[i] = 79;
226 memcpy(v + 80, v, 9 * sizeof(*v));
227 }
228
229 /* Distribute the new matrix value to the shifted position */
230 v[offset[i]] =
231 (int)( (unsigned)ff_synmatrix4[i][0] * frame->sb_sample[blk][ch][0] +
232 (unsigned)ff_synmatrix4[i][1] * frame->sb_sample[blk][ch][1] +
233 (unsigned)ff_synmatrix4[i][2] * frame->sb_sample[blk][ch][2] +
234 (unsigned)ff_synmatrix4[i][3] * frame->sb_sample[blk][ch][3] ) >> 15;
235 }
236
237 /* Compute the samples */
238 for (idx = 0, i = 0; i < 4; i++, idx += 5) {
239 k = (i + 4) & 0xf;
240
241 /* Store in output, Q0 */
242 AV_WN16A(&output_frame->data[ch][blk * 8 + i * 2], av_clip_int16(
243 (int)( (unsigned)v[offset[i] + 0] * ff_sbc_proto_4_40m0[idx + 0] +
244 (unsigned)v[offset[k] + 1] * ff_sbc_proto_4_40m1[idx + 0] +
245 (unsigned)v[offset[i] + 2] * ff_sbc_proto_4_40m0[idx + 1] +
246 (unsigned)v[offset[k] + 3] * ff_sbc_proto_4_40m1[idx + 1] +
247 (unsigned)v[offset[i] + 4] * ff_sbc_proto_4_40m0[idx + 2] +
248 (unsigned)v[offset[k] + 5] * ff_sbc_proto_4_40m1[idx + 2] +
249 (unsigned)v[offset[i] + 6] * ff_sbc_proto_4_40m0[idx + 3] +
250 (unsigned)v[offset[k] + 7] * ff_sbc_proto_4_40m1[idx + 3] +
251 (unsigned)v[offset[i] + 8] * ff_sbc_proto_4_40m0[idx + 4] +
252 (unsigned)v[offset[k] + 9] * ff_sbc_proto_4_40m1[idx + 4] ) >> 15));
253 }
254 }
255
256 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 int32_t *v = state->V[ch];
262 int *offset = state->offset[ch];
263
264 for (i = 0; i < 16; i++) {
265 /* Shifting */
266 offset[i]--;
267 if (offset[i] < 0) {
268 offset[i] = 159;
269 memcpy(v + 160, v, 9 * sizeof(*v));
270 }
271
272 /* Distribute the new matrix value to the shifted position */
273 v[offset[i]] =
274 (int)( (unsigned)ff_synmatrix8[i][0] * frame->sb_sample[blk][ch][0] +
275 (unsigned)ff_synmatrix8[i][1] * frame->sb_sample[blk][ch][1] +
276 (unsigned)ff_synmatrix8[i][2] * frame->sb_sample[blk][ch][2] +
277 (unsigned)ff_synmatrix8[i][3] * frame->sb_sample[blk][ch][3] +
278 (unsigned)ff_synmatrix8[i][4] * frame->sb_sample[blk][ch][4] +
279 (unsigned)ff_synmatrix8[i][5] * frame->sb_sample[blk][ch][5] +
280 (unsigned)ff_synmatrix8[i][6] * frame->sb_sample[blk][ch][6] +
281 (unsigned)ff_synmatrix8[i][7] * frame->sb_sample[blk][ch][7] ) >> 15;
282 }
283
284 /* Compute the samples */
285 for (idx = 0, i = 0; i < 8; i++, idx += 5) {
286 k = (i + 8) & 0xf;
287
288 /* Store in output, Q0 */
289 AV_WN16A(&output_frame->data[ch][blk * 16 + i * 2], av_clip_int16(
290 (int)( (unsigned)v[offset[i] + 0] * ff_sbc_proto_8_80m0[idx + 0] +
291 (unsigned)v[offset[k] + 1] * ff_sbc_proto_8_80m1[idx + 0] +
292 (unsigned)v[offset[i] + 2] * ff_sbc_proto_8_80m0[idx + 1] +
293 (unsigned)v[offset[k] + 3] * ff_sbc_proto_8_80m1[idx + 1] +
294 (unsigned)v[offset[i] + 4] * ff_sbc_proto_8_80m0[idx + 2] +
295 (unsigned)v[offset[k] + 5] * ff_sbc_proto_8_80m1[idx + 2] +
296 (unsigned)v[offset[i] + 6] * ff_sbc_proto_8_80m0[idx + 3] +
297 (unsigned)v[offset[k] + 7] * ff_sbc_proto_8_80m1[idx + 3] +
298 (unsigned)v[offset[i] + 8] * ff_sbc_proto_8_80m0[idx + 4] +
299 (unsigned)v[offset[k] + 9] * ff_sbc_proto_8_80m1[idx + 4] ) >> 15));
300 }
301 }
302
303 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 switch (frame->subbands) {
309 case 4:
310 for (ch = 0; ch < frame->channels; ch++)
311 for (blk = 0; blk < frame->blocks; blk++)
312 sbc_synthesize_four(state, frame, ch, blk, output_frame);
313 break;
314
315 case 8:
316 for (ch = 0; ch < frame->channels; ch++)
317 for (blk = 0; blk < frame->blocks; blk++)
318 sbc_synthesize_eight(state, frame, ch, blk, output_frame);
319 break;
320 }
321 }
322
323 static int sbc_decode_init(AVCodecContext *avctx)
324 {
325 SBCDecContext *sbc = avctx->priv_data;
326 int i, ch;
327
328 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
329
330 sbc->frame.crc_ctx = av_crc_get_table(AV_CRC_8_EBU);
331
332 memset(sbc->dsp.V, 0, sizeof(sbc->dsp.V));
333 for (ch = 0; ch < 2; ch++)
334 for (i = 0; i < FF_ARRAY_ELEMS(sbc->dsp.offset[0]); i++)
335 sbc->dsp.offset[ch][i] = (10 * i + 10);
336 return 0;
337 }
338
339 static int sbc_decode_frame(AVCodecContext *avctx,
340 void *data, int *got_frame_ptr,
341 AVPacket *avpkt)
342 {
343 SBCDecContext *sbc = avctx->priv_data;
344 AVFrame *frame = data;
345 int ret, frame_length;
346
347 if (!sbc)
348 return AVERROR(EIO);
349
350 frame_length = sbc_unpack_frame(avpkt->data, &sbc->frame, avpkt->size);
351 if (frame_length <= 0)
352 return frame_length;
353
354 avctx->channels = sbc->frame.channels;
355
356 frame->nb_samples = sbc->frame.blocks * sbc->frame.subbands;
357 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
358 return ret;
359
360 sbc_synthesize_audio(&sbc->dsp, &sbc->frame, frame);
361
362 *got_frame_ptr = 1;
363
364 return frame_length;
365 }
366
367 const AVCodec ff_sbc_decoder = {
368 .name = "sbc",
369 .long_name = NULL_IF_CONFIG_SMALL("SBC (low-complexity subband codec)"),
370 .type = AVMEDIA_TYPE_AUDIO,
371 .id = AV_CODEC_ID_SBC,
372 .priv_data_size = sizeof(SBCDecContext),
373 .init = sbc_decode_init,
374 .decode = sbc_decode_frame,
375 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
376 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
377 .channel_layouts = (const uint64_t[]) { AV_CH_LAYOUT_MONO,
378 AV_CH_LAYOUT_STEREO, 0},
379 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
380 AV_SAMPLE_FMT_NONE },
381 .supported_samplerates = (const int[]) { 16000, 32000, 44100, 48000, 0 },
382 };
383