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