1 /*
2 * Copyright (C) 2022 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 "hfp_msbc_decoder"
18
19 #include "hfp_msbc_decoder.h"
20
21 #include <bluetooth/log.h>
22
23 #include <cstring>
24
25 #include "embdrv/sbc/decoder/include/oi_codec_sbc.h"
26 #include "embdrv/sbc/decoder/include/oi_status.h"
27 #include "os/log.h"
28
29 #define HFP_MSBC_PKT_LEN 60
30 #define HFP_MSBC_PCM_BYTES 240
31
32 using namespace bluetooth;
33
34 namespace fmt {
35 template <>
36 struct formatter<OI_STATUS> : enum_formatter<OI_STATUS> {};
37 } // namespace fmt
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 } tHFP_MSBC_DECODER;
43
44 static tHFP_MSBC_DECODER hfp_msbc_decoder;
45
hfp_msbc_decoder_init()46 bool hfp_msbc_decoder_init() {
47 OI_STATUS status = OI_CODEC_SBC_DecoderReset(
48 &hfp_msbc_decoder.decoder_context, hfp_msbc_decoder.context_data,
49 sizeof(hfp_msbc_decoder.context_data), 1, 1, false);
50 if (!OI_SUCCESS(status)) {
51 log::error("OI_CODEC_SBC_DecoderReset failed with error code {}", status);
52 return false;
53 }
54
55 status = OI_CODEC_SBC_DecoderConfigureMSbc(&hfp_msbc_decoder.decoder_context);
56 if (!OI_SUCCESS(status)) {
57 log::error("OI_CODEC_SBC_DecoderConfigureMSbc failed with error code {}",
58 status);
59 return false;
60 }
61
62 return true;
63 }
64
hfp_msbc_decoder_cleanup(void)65 void hfp_msbc_decoder_cleanup(void) {
66 memset(&hfp_msbc_decoder, 0, sizeof(hfp_msbc_decoder));
67 }
68
69 // Get the HFP MSBC encoded maximum frame size
hfp_msbc_decoder_decode_packet(const uint8_t * i_buf,int16_t * o_buf,size_t out_len)70 bool hfp_msbc_decoder_decode_packet(const uint8_t* i_buf, int16_t* o_buf,
71 size_t out_len) {
72 if (out_len < HFP_MSBC_PCM_BYTES) {
73 log::error(
74 "Output buffer's size {} is less than one complete mSBC frame {}",
75 (unsigned long)out_len, HFP_MSBC_PCM_BYTES);
76 return false;
77 }
78
79 const OI_BYTE* oi_data;
80 uint32_t oi_size, out_avail;
81
82 oi_data = i_buf;
83 oi_size = HFP_MSBC_PKT_LEN;
84 out_avail = out_len;
85
86 OI_STATUS status = OI_CODEC_SBC_DecodeFrame(
87 &hfp_msbc_decoder.decoder_context, &oi_data, &oi_size, o_buf, &out_avail);
88 if (!OI_SUCCESS(status) || out_avail != HFP_MSBC_PCM_BYTES || oi_size != 0) {
89 log::error("Decoding failure: {}, {}, {}", status, (unsigned long)out_avail,
90 (unsigned long)oi_size);
91 return false;
92 }
93
94 return true;
95 }
96