1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bluetooth-a2dp"
18
19 #include "a2dp_sbc_decoder.h"
20
21 #include <bluetooth/log.h>
22
23 #include "embdrv/sbc/decoder/include/oi_codec_sbc.h"
24 #include "embdrv/sbc/decoder/include/oi_status.h"
25 #include "stack/include/bt_hdr.h"
26
27 using namespace bluetooth;
28
29 namespace fmt {
30 template <>
31 struct formatter<OI_STATUS> : enum_formatter<OI_STATUS> {};
32 } // namespace fmt
33
34 typedef struct {
35 OI_CODEC_SBC_DECODER_CONTEXT decoder_context;
36 uint32_t context_data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
37 int16_t decode_buf[15 * SBC_MAX_SAMPLES_PER_FRAME * SBC_MAX_CHANNELS];
38 decoded_data_callback_t decode_callback;
39 } tA2DP_SBC_DECODER_CB;
40
41 static tA2DP_SBC_DECODER_CB a2dp_sbc_decoder_cb;
42
A2DP_LoadDecoderSbc(void)43 bool A2DP_LoadDecoderSbc(void) {
44 // Nothing to do - the library is statically linked
45 return true;
46 }
47
A2DP_UnloadDecoderSbc(void)48 void A2DP_UnloadDecoderSbc(void) { a2dp_sbc_decoder_cleanup(); }
49
a2dp_sbc_decoder_init(decoded_data_callback_t decode_callback)50 bool a2dp_sbc_decoder_init(decoded_data_callback_t decode_callback) {
51 OI_STATUS status = OI_CODEC_SBC_DecoderReset(
52 &a2dp_sbc_decoder_cb.decoder_context, a2dp_sbc_decoder_cb.context_data,
53 sizeof(a2dp_sbc_decoder_cb.context_data), 2, 2, false);
54 if (!OI_SUCCESS(status)) {
55 log::error("OI_CODEC_SBC_DecoderReset failed with error code {}", status);
56 return false;
57 }
58
59 a2dp_sbc_decoder_cb.decode_callback = decode_callback;
60 return true;
61 }
62
a2dp_sbc_decoder_cleanup(void)63 void a2dp_sbc_decoder_cleanup(void) {
64 // Do nothing.
65 }
66
a2dp_sbc_decoder_decode_packet(BT_HDR * p_buf)67 bool a2dp_sbc_decoder_decode_packet(BT_HDR* p_buf) {
68 uint8_t* data = p_buf->data + p_buf->offset;
69 size_t data_size = p_buf->len;
70
71 if (data_size == 0) {
72 log::error("Empty packet");
73 return false;
74 }
75 size_t num_frames = data[0] & 0xf;
76 data += 1;
77 data_size -= 1;
78
79 const OI_BYTE* oi_data = data;
80 uint32_t oi_size = data_size;
81 size_t out_avail = sizeof(a2dp_sbc_decoder_cb.decode_buf);
82 int16_t* out_ptr = a2dp_sbc_decoder_cb.decode_buf;
83
84 for (size_t i = 0; i < num_frames; ++i) {
85 uint32_t out_size = out_avail;
86 OI_STATUS status =
87 OI_CODEC_SBC_DecodeFrame(&a2dp_sbc_decoder_cb.decoder_context, &oi_data,
88 &oi_size, out_ptr, &out_size);
89 if (!OI_SUCCESS(status)) {
90 log::error("Decoding failure: {}", status);
91 return false;
92 }
93 out_avail -= out_size;
94 out_ptr += out_size / sizeof(*out_ptr);
95 }
96
97 size_t out_used =
98 (out_ptr - a2dp_sbc_decoder_cb.decode_buf) * sizeof(*out_ptr);
99 a2dp_sbc_decoder_cb.decode_callback(
100 reinterpret_cast<uint8_t*>(a2dp_sbc_decoder_cb.decode_buf), out_used);
101 return true;
102 }
103