xref: /btstack/src/classic/hfp_codec.c (revision cf7250aa3d6090314187d303f46421dd69a5eef4)
1 /*
2  * Copyright (C) 2023 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "hfp_codec.c"
39 
40 // *****************************************************************************
41 //
42 // HFP Codec
43 //
44 // *****************************************************************************
45 
46 #include "btstack_config.h"
47 
48 #include <string.h>
49 
50 #include "hfp_codec.h"
51 #include "btstack_debug.h"
52 
53 // enable to send test data
54 // #define HFP_CODEC_TEST
55 
56 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
57 #include "btstack_sbc.h"
58 #define FRAME_SIZE_MSBC 57
59 static void hfp_codec_encode_msbc(hfp_codec_t * hfp_codec, int16_t * pcm_samples);
60 #endif
61 
62 #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
63 #define LC3_SWB_OCTETS_PER_FRAME   58
64 static void hfp_codec_encode_lc3swb(hfp_codec_t * hfp_codec, int16_t * pcm_samples);
65 #endif
66 
67 // HFP H2 Framing - might get moved into a hfp_h2.c
68 
69 // const
70 static const uint8_t hfp_h2_header_h2_byte_0         = 1;
71 static const uint8_t hfp_h2_header_h2_byte_1_table[] = {0x08, 0x38, 0xc8, 0xf8 };
72 
73 void hfp_h2_framing_init(hfp_h2_framing_t * hfp_h2_framing){
74     hfp_h2_framing->sequence_number = 0;
75 }
76 
77 /**
78  * @brief Add next H2 Header
79  * @param hfp_h2_framing
80  * @param buffer [2]
81  */
82 void hfp_h2_framing_add_header(hfp_h2_framing_t * hfp_h2_framing, uint8_t * buffer){
83     // Synchronization Header H2
84     buffer[0] = hfp_h2_header_h2_byte_0;
85     buffer[1] = hfp_h2_header_h2_byte_1_table[hfp_h2_framing->sequence_number];
86     hfp_h2_framing->sequence_number = (hfp_h2_framing->sequence_number + 1) & 3;
87 }
88 
89 
90 void hfp_codec_init(hfp_codec_t * hfp_codec, uint8_t codec_id){
91     memset(hfp_codec, 0, sizeof(hfp_codec_t));
92     hfp_h2_framing_init(&hfp_codec->h2_framing);
93     switch (codec_id){
94 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
95         case HFP_CODEC_MSBC:
96             hfp_codec->samples_per_frame = 120;
97             hfp_codec->encode = &hfp_codec_encode_msbc;
98             btstack_sbc_encoder_init(&hfp_codec->msbc_state, SBC_MODE_mSBC, 16, 8, SBC_ALLOCATION_METHOD_LOUDNESS, 16000, 26, SBC_CHANNEL_MODE_MONO);
99             break;
100 #endif
101 #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
102         case HFP_CODEC_LC3_SWB:
103             hfp_codec->samples_per_frame = 240;
104             hfp_codec->encode = &hfp_codec_encode_lc3swb;
105             // init lc3 encoder
106             hfp_codec->lc3_encoder = btstack_lc3_encoder_google_init_instance(&hfp_codec->lc3_encoder_context);
107             hfp_codec->lc3_encoder->configure(&hfp_codec->lc3_encoder_context, 32000, BTSTACK_LC3_FRAME_DURATION_7500US, LC3_SWB_OCTETS_PER_FRAME);
108             break;
109 #endif
110         default:
111             btstack_assert(false);
112             break;
113     }
114 }
115 
116 bool hfp_codec_can_encode_audio_frame_now(const hfp_codec_t * hfp_codec){
117     return hfp_codec->write_pos <= SCO_FRAME_SIZE;
118 }
119 
120 uint16_t hfp_codec_num_audio_samples_per_frame(const hfp_codec_t * hfp_codec){
121     return hfp_codec->samples_per_frame;
122 }
123 
124 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
125 static void hfp_codec_encode_msbc(hfp_codec_t * hfp_codec, int16_t * pcm_samples){
126     // Encode SBC Frame
127     btstack_sbc_encoder_process_data(pcm_samples);
128     (void)memcpy(&hfp_codec->sco_packet[hfp_codec->write_pos], btstack_sbc_encoder_sbc_buffer(), FRAME_SIZE_MSBC);
129     hfp_codec->write_pos += FRAME_SIZE_MSBC;
130     // Final padding to use SCO_FRAME_SIZE bytes
131     hfp_codec->sco_packet[hfp_codec->write_pos++] = 0;
132 }
133 #endif
134 
135 #ifdef ENABLE_HFP_SUPER_WIDE_BAND_SPEECH
136 static void hfp_codec_encode_lc3swb(hfp_codec_t * hfp_codec, int16_t * pcm_samples){
137     // Encode LC3 Frame
138     hfp_codec->lc3_encoder->encode_signed_16(&hfp_codec->lc3_encoder_context, pcm_samples, 1, &hfp_codec->sco_packet[hfp_codec->write_pos]);
139     hfp_codec->write_pos += LC3_SWB_OCTETS_PER_FRAME;
140 }
141 #endif
142 
143 void hfp_codec_encode_audio_frame(hfp_codec_t * hfp_codec, int16_t * pcm_samples){
144     btstack_assert(hfp_codec_can_encode_audio_frame_now(hfp_codec));
145     // Synchronization Header H2
146     hfp_h2_framing_add_header(&hfp_codec->h2_framing, &hfp_codec->sco_packet[hfp_codec->write_pos]);
147     hfp_codec->write_pos += 2;
148     // encode
149 #ifdef HFP_CODEC_TEST
150     // packet counter
151     static uint8_t counter = 0;
152     hfp_codec->sco_packet[hfp_codec->write_pos++] = counter++;
153     // test data
154     uint8_t i;
155     for (i=3;i<SCO_FRAME_SIZE;i++){
156         hfp_codec->sco_packet[hfp_codec->write_pos++] = i;
157     }
158 #else
159     hfp_codec->encode(hfp_codec, pcm_samples);
160 #endif
161     log_info("Encode frame, read %u, write %u", hfp_codec->read_pos, hfp_codec->write_pos);
162 }
163 
164 uint16_t hfp_codec_num_bytes_available(const hfp_codec_t * hfp_codec){
165     return hfp_codec->write_pos - hfp_codec->read_pos;
166 }
167 
168 void hfp_codec_read_from_stream(hfp_codec_t * hfp_codec, uint8_t * buf, uint16_t size){
169     uint16_t num_bytes_available = hfp_codec_num_bytes_available(hfp_codec);
170     btstack_assert(num_bytes_available >= size);
171 
172     uint16_t num_bytes_to_copy = btstack_min(num_bytes_available, size);
173 
174     memcpy(buf, &hfp_codec->sco_packet[hfp_codec->read_pos], num_bytes_to_copy);
175     hfp_codec->read_pos += num_bytes_to_copy;
176 
177     // reset buffer
178     if (hfp_codec->read_pos == hfp_codec->write_pos){
179         hfp_codec->read_pos = 0;
180         hfp_codec->write_pos = 0;
181     }
182 
183     log_info("Read %u from stream, read %u, write %u", size, hfp_codec->read_pos, hfp_codec->write_pos);
184 }
185 
186 void hfp_codec_deinit(hfp_codec_t * hfp_codec){
187     UNUSED(hfp_codec);
188 }
189