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_ = lc3_setup_decoder(dt_us, sr_hz, sr_pcm_hz, hfp_lc3_decoder_mem_);
51 
52   if (hfp_lc3_decoder_ == nullptr) {
53     log::error("Wrong parameters provided");
54     return -EINVAL;
55   }
56 
57   return HFP_LC3_PKT_FRAME_LEN;
58 }
59 
cleanup()60 void HfpLc3Decoder::cleanup() {
61   if (hfp_lc3_decoder_mem_) {
62     osi_free_and_reset((void**)&hfp_lc3_decoder_mem_);
63     log::info("Released the decoder instance");
64   }
65 }
66 
transcode(uint8_t * i_buf,int i_len,uint8_t * o_buf,int o_len)67 int HfpLc3Decoder::transcode(uint8_t* i_buf, int i_len, uint8_t* o_buf, int o_len) {
68   if (o_buf == nullptr || o_len < HFP_LC3_PCM_BYTES + 1) {
69     log::error("Output buffer size is less than LC3 frame size");
70     return -EINVAL;
71   }
72 
73   // Check header to decide whether it's PLC.
74   uint8_t* in_frame = (i_buf[0] || i_buf[1]) ? i_buf + HFP_LC3_H2_HEADER_LEN : nullptr;
75 
76   // First byte is reserved to indicate PLC.
77   uint8_t* out_frame = o_buf + 1;
78 
79   /* Note this only fails when wrong parameters are supplied. */
80   int rc = lc3_decode(hfp_lc3_decoder_, in_frame, HFP_LC3_PKT_FRAME_LEN, MapLc3PcmFmt(param_.fmt()),
81                       out_frame, param_.stride());
82 
83   if (rc != 0 && rc != 1) {
84     log::warn("Wrong decode parameters");
85     std::fill(o_buf, o_buf + o_len, 0);
86   } else {
87     o_buf[0] = rc;
88   }
89   return HFP_LC3_PCM_BYTES + 1;
90 }
91 
92 }  // namespace mmc
93