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 <cstddef>
24 #include <cstdint>
25 
26 #include "a2dp_codec_api.h"
27 #include "embdrv/sbc/decoder/include/oi_codec_sbc.h"
28 #include "embdrv/sbc/decoder/include/oi_cpu_dep.h"
29 #include "embdrv/sbc/decoder/include/oi_status.h"
30 #include "stack/include/bt_hdr.h"
31 
32 using namespace bluetooth;
33 
34 namespace std {
35 template <>
36 struct formatter<OI_STATUS> : enum_formatter<OI_STATUS> {};
37 }  // namespace std
38 
39 typedef struct {
40   OI_CODEC_SBC_DECODER_CONTEXT decoder_context;
41   uint32_t context_data[CODEC_DATA_WORDS(2, SBC_CODEC_FAST_FILTER_BUFFERS)];
42   int16_t decode_buf[15 * SBC_MAX_SAMPLES_PER_FRAME * SBC_MAX_CHANNELS];
43   decoded_data_callback_t decode_callback;
44 } tA2DP_SBC_DECODER_CB;
45 
46 static tA2DP_SBC_DECODER_CB a2dp_sbc_decoder_cb;
47 
a2dp_sbc_decoder_init(decoded_data_callback_t decode_callback)48 bool a2dp_sbc_decoder_init(decoded_data_callback_t decode_callback) {
49   OI_STATUS status = OI_CODEC_SBC_DecoderReset(
50           &a2dp_sbc_decoder_cb.decoder_context, a2dp_sbc_decoder_cb.context_data,
51           sizeof(a2dp_sbc_decoder_cb.context_data), 2, 2, false);
52   if (!OI_SUCCESS(status)) {
53     log::error("OI_CODEC_SBC_DecoderReset failed with error code {}", status);
54     return false;
55   }
56 
57   a2dp_sbc_decoder_cb.decode_callback = decode_callback;
58   return true;
59 }
60 
a2dp_sbc_decoder_cleanup(void)61 void a2dp_sbc_decoder_cleanup(void) {
62   // Do nothing.
63 }
64 
a2dp_sbc_decoder_decode_packet(BT_HDR * p_buf)65 bool a2dp_sbc_decoder_decode_packet(BT_HDR* p_buf) {
66   uint8_t* data = p_buf->data + p_buf->offset;
67   size_t data_size = p_buf->len;
68 
69   if (data_size == 0) {
70     log::error("Empty packet");
71     return false;
72   }
73   size_t num_frames = data[0] & 0xf;
74   data += 1;
75   data_size -= 1;
76 
77   const OI_BYTE* oi_data = data;
78   uint32_t oi_size = data_size;
79   size_t out_avail = sizeof(a2dp_sbc_decoder_cb.decode_buf);
80   int16_t* out_ptr = a2dp_sbc_decoder_cb.decode_buf;
81 
82   for (size_t i = 0; i < num_frames; ++i) {
83     uint32_t out_size = out_avail;
84     OI_STATUS status = OI_CODEC_SBC_DecodeFrame(&a2dp_sbc_decoder_cb.decoder_context, &oi_data,
85                                                 &oi_size, out_ptr, &out_size);
86     if (!OI_SUCCESS(status)) {
87       log::error("Decoding failure: {}", status);
88       return false;
89     }
90     out_avail -= out_size;
91     out_ptr += out_size / sizeof(*out_ptr);
92   }
93 
94   size_t out_used = (out_ptr - a2dp_sbc_decoder_cb.decode_buf) * sizeof(*out_ptr);
95   a2dp_sbc_decoder_cb.decode_callback(reinterpret_cast<uint8_t*>(a2dp_sbc_decoder_cb.decode_buf),
96                                       out_used);
97   return true;
98 }
99