1 /*
2  * Copyright 2023 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 #include "mmc/codec_server/hfp_lc3_mmc_decoder.h"
18 
19 #include <bluetooth/log.h>
20 #include <lc3.h>
21 
22 #include "mmc/codec_server/lc3_utils.h"
23 #include "mmc/proto/mmc_config.pb.h"
24 #include "osi/include/allocator.h"
25 
26 namespace mmc {
27 
28 using namespace bluetooth;
29 
HfpLc3Decoder()30 HfpLc3Decoder::HfpLc3Decoder() : hfp_lc3_decoder_mem_(nullptr) {}
31 
~HfpLc3Decoder()32 HfpLc3Decoder::~HfpLc3Decoder() { cleanup(); }
33 
init(ConfigParam config)34 int HfpLc3Decoder::init(ConfigParam config) {
35   cleanup();
36 
37   if (!config.has_hfp_lc3_decoder_param()) {
38     log::error("HFP LC3 decoder params are not set");
39     return -EINVAL;
40   }
41 
42   param_ = config.hfp_lc3_decoder_param();
43   int dt_us = param_.dt_us();
44   int sr_hz = param_.sr_hz();
45   int sr_pcm_hz = param_.sr_pcm_hz();
46   const unsigned dec_size = lc3_decoder_size(dt_us, sr_pcm_hz);
47 
48   hfp_lc3_decoder_mem_ = osi_malloc(dec_size);
49 
50   hfp_lc3_decoder_ =
51       lc3_setup_decoder(dt_us, sr_hz, sr_pcm_hz, hfp_lc3_decoder_mem_);
52 
53   if (hfp_lc3_decoder_ == nullptr) {
54     log::error("Wrong parameters provided");
55     return -EINVAL;
56   }
57 
58   return HFP_LC3_PKT_FRAME_LEN;
59 }
60 
cleanup()61 void HfpLc3Decoder::cleanup() {
62   if (hfp_lc3_decoder_mem_) {
63     osi_free_and_reset((void**)&hfp_lc3_decoder_mem_);
64     log::info("Released the decoder instance");
65   }
66 }
67 
transcode(uint8_t * i_buf,int i_len,uint8_t * o_buf,int o_len)68 int HfpLc3Decoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf,
69                              int o_len) {
70   if (o_buf == nullptr || o_len < HFP_LC3_PCM_BYTES + 1) {
71     log::error("Output buffer size is less than LC3 frame size");
72     return -EINVAL;
73   }
74 
75   // Check header to decide whether it's PLC.
76   uint8_t* in_frame =
77       (i_buf[0] || i_buf[1]) ? i_buf + HFP_LC3_H2_HEADER_LEN : nullptr;
78 
79   // First byte is reserved to indicate PLC.
80   uint8_t* out_frame = o_buf + 1;
81 
82   /* Note this only fails when wrong parameters are supplied. */
83   int rc = lc3_decode(hfp_lc3_decoder_, in_frame, HFP_LC3_PKT_FRAME_LEN,
84                       MapLc3PcmFmt(param_.fmt()), out_frame, param_.stride());
85 
86   if (rc != 0 && rc != 1) {
87     log::warn("Wrong decode parameters");
88     std::fill(o_buf, o_buf + o_len, 0);
89   } else
90     o_buf[0] = rc;
91   return HFP_LC3_PCM_BYTES + 1;
92 }
93 
94 }  // namespace mmc
95