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