xref: /btstack/src/classic/hfp_codec.c (revision 79cc780fc053279da9e16c24776e510173bab4e5)
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 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
54 #include "btstack_sbc.h"
55 #define FRAME_SIZE_MSBC 57
56 static void hfp_codec_encode_msbc(hfp_codec_t * hfp_codec, int16_t * pcm_samples);
57 #endif
58 
59 #include "hfp_codec.h"
60 
61 void hfp_codec_init(hfp_codec_t * hfp_codec, uint8_t codec_id){
62     memset(hfp_codec, 0, sizeof(hfp_codec_t));
63     hfp_h2_framing_init(&hfp_codec->h2_framing);
64     switch (codec_id){
65 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
66         case HFP_CODEC_MSBC:
67             hfp_codec->samples_per_frame = 120;
68             hfp_codec->encode = &hfp_codec_encode_msbc;
69             btstack_sbc_encoder_init(&hfp_codec->msbc_state, SBC_MODE_mSBC, 16, 8, SBC_ALLOCATION_METHOD_LOUDNESS, 16000, 26, SBC_CHANNEL_MODE_MONO);
70             break;
71 #endif
72         default:
73             btstack_assert(false);
74             break;
75     }
76 }
77 
78 bool hfp_codec_can_encode_audio_frame_now(const hfp_codec_t * hfp_codec){
79     return hfp_codec->read_pos == hfp_codec->write_pos;
80 }
81 
82 uint16_t hfp_codec_num_audio_samples_per_frame(const hfp_codec_t * hfp_codec){
83     return hfp_codec->samples_per_frame;
84 }
85 
86 #ifdef ENABLE_HFP_WIDE_BAND_SPEECH
87 static void hfp_codec_encode_msbc(hfp_codec_t * hfp_codec, int16_t * pcm_samples){
88     // Synchronization Header H2
89     hfp_h2_framing_add_header(&hfp_codec->h2_framing, hfp_codec->sco_packet);
90     hfp_codec->write_pos += 2;
91 
92     // Encode SBC Frame
93     btstack_sbc_encoder_process_data(pcm_samples);
94     (void)memcpy(&hfp_codec->sco_packet[hfp_codec->write_pos],
95                  btstack_sbc_encoder_sbc_buffer(), FRAME_SIZE_MSBC);
96     hfp_codec->write_pos += FRAME_SIZE_MSBC;
97 
98     // Final padding to use 60 bytes
99     hfp_codec->sco_packet[hfp_codec->write_pos++] = 0;
100 }
101 #endif
102 
103 void hfp_codec_encode_audio_frame(hfp_codec_t * hfp_codec, int16_t * pcm_samples){
104     btstack_assert(hfp_codec_can_encode_audio_frame_now(hfp_codec));
105 
106     // reset packet buffer
107     hfp_codec->read_pos = 0;
108     hfp_codec->write_pos = 0;
109 
110     // encode
111     hfp_codec->encode(hfp_codec, pcm_samples);
112 }
113 
114 uint16_t hfp_codec_num_bytes_available(const hfp_codec_t * hfp_codec){
115     return hfp_codec->write_pos - hfp_codec->read_pos;
116 }
117 
118 void hfp_codec_read_from_stream(hfp_codec_t * hfp_codec, uint8_t * buf, int size){
119     uint16_t num_bytes_available = hfp_codec_num_bytes_available(hfp_codec);
120     btstack_assert(num_bytes_available >= size);
121 
122     uint16_t num_bytes_to_copy = btstack_min(num_bytes_available, size);
123 
124     memcpy(buf, &hfp_codec->sco_packet[hfp_codec->read_pos], num_bytes_to_copy);
125     hfp_codec->read_pos += num_bytes_to_copy;
126 }
127 
128 void hfp_codec_deinit(hfp_codec_t * hfp_codec){
129     UNUSED(hfp_codec);
130 }
131