1 /*
2  * Copyright (C) 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 #define LOG_TAG "hfp_lc3_decoder"
18 
19 #include <bluetooth/log.h>
20 
21 #include <algorithm>
22 
23 #include "hfp_lc3_decoder.h"
24 #include "mmc/codec_client/codec_client.h"
25 #include "mmc/proto/mmc_config.pb.h"
26 
27 using namespace bluetooth;
28 
29 const int HFP_LC3_H2_HEADER_LEN = 2;
30 const int HFP_LC3_PKT_FRAME_LEN = 58;
31 const int HFP_LC3_PCM_BYTES = 480;
32 
33 static mmc::CodecClient* client = nullptr;
34 static const uint8_t plc_buf[HFP_LC3_H2_HEADER_LEN + HFP_LC3_PKT_FRAME_LEN] = {0};
35 
hfp_lc3_decoder_init()36 bool hfp_lc3_decoder_init() {
37   hfp_lc3_decoder_cleanup();
38   client = new mmc::CodecClient;
39 
40   const int dt_us = 7500;
41   const int sr_hz = 32000;
42   const int sr_pcm_hz = 32000;
43 
44   mmc::Lc3Param param;
45   param.set_dt_us(dt_us);
46   param.set_sr_hz(sr_hz);
47   param.set_sr_pcm_hz(sr_pcm_hz);
48   param.set_stride(1);
49   param.set_fmt(mmc::Lc3Param::kLc3PcmFormatS16);
50 
51   mmc::ConfigParam config;
52   *config.mutable_hfp_lc3_decoder_param() = param;
53 
54   int ret = client->init(config);
55   if (ret < 0) {
56     log::error("Init failed with error message, {}", strerror(-ret));
57     return false;
58   }
59 
60   return true;
61 }
62 
hfp_lc3_decoder_cleanup()63 void hfp_lc3_decoder_cleanup() {
64   if (client) {
65     client->cleanup();
66     delete client;
67     client = nullptr;
68   }
69 }
70 
hfp_lc3_decoder_decode_packet(const uint8_t * i_buf,int16_t * o_buf,size_t out_len)71 bool hfp_lc3_decoder_decode_packet(const uint8_t* i_buf, int16_t* o_buf, size_t out_len) {
72   if (o_buf == nullptr || out_len < HFP_LC3_PCM_BYTES) {
73     log::error("Output buffer size {} is less than LC3 frame size {}", out_len, HFP_LC3_PCM_BYTES);
74     return false;
75   }
76 
77   if (!client) {
78     log::error("CodecClient has not been initialized");
79     return false;
80   }
81 
82   // Pass zeros to MMC when i_buf is nullptr.
83   const uint8_t* frame = i_buf ? i_buf : plc_buf;
84 
85   // One extra byte in the beginning to indicate whether PLC was conducted.
86   uint8_t* o_packet = new uint8_t[out_len + 1];
87 
88   int rc = client->transcode((uint8_t*)frame, HFP_LC3_PKT_FRAME_LEN + HFP_LC3_H2_HEADER_LEN,
89                              o_packet, out_len + 1);
90 
91   if (rc < 0) {
92     log::warn("Decode failed with error message, {}", strerror(-rc));
93     return false;
94   }
95 
96   bool plc_conducted = o_packet[0];
97 
98   std::copy(o_packet + 1, o_packet + 1 + out_len, (uint8_t*)o_buf);
99 
100   delete[] o_packet;
101   return !plc_conducted;
102 }
103