1*9a19cd78SMatthias Ringwald /****************************************************************************** 2*9a19cd78SMatthias Ringwald * 3*9a19cd78SMatthias Ringwald * Copyright 2021 Google, Inc. 4*9a19cd78SMatthias Ringwald * 5*9a19cd78SMatthias Ringwald * Licensed under the Apache License, Version 2.0 (the "License"); 6*9a19cd78SMatthias Ringwald * you may not use this file except in compliance with the License. 7*9a19cd78SMatthias Ringwald * You may obtain a copy of the License at: 8*9a19cd78SMatthias Ringwald * 9*9a19cd78SMatthias Ringwald * http://www.apache.org/licenses/LICENSE-2.0 10*9a19cd78SMatthias Ringwald * 11*9a19cd78SMatthias Ringwald * Unless required by applicable law or agreed to in writing, software 12*9a19cd78SMatthias Ringwald * distributed under the License is distributed on an "AS IS" BASIS, 13*9a19cd78SMatthias Ringwald * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*9a19cd78SMatthias Ringwald * See the License for the specific language governing permissions and 15*9a19cd78SMatthias Ringwald * limitations under the License. 16*9a19cd78SMatthias Ringwald * 17*9a19cd78SMatthias Ringwald ******************************************************************************/ 18*9a19cd78SMatthias Ringwald 19*9a19cd78SMatthias Ringwald /** 20*9a19cd78SMatthias Ringwald * Low Complexity Communication Codec (LC3) 21*9a19cd78SMatthias Ringwald * 22*9a19cd78SMatthias Ringwald * This implementation conforms to : 23*9a19cd78SMatthias Ringwald * Low Complexity Communication Codec (LC3) 24*9a19cd78SMatthias Ringwald * Bluetooth Specification v1.0 25*9a19cd78SMatthias Ringwald * 26*9a19cd78SMatthias Ringwald * 27*9a19cd78SMatthias Ringwald * The LC3 is an efficient low latency audio codec. 28*9a19cd78SMatthias Ringwald * 29*9a19cd78SMatthias Ringwald * - Unlike most other codecs, the LC3 codec is focused on audio streaming 30*9a19cd78SMatthias Ringwald * in constrained (on packet sizes and interval) tranport layer. 31*9a19cd78SMatthias Ringwald * In this way, the LC3 does not handle : 32*9a19cd78SMatthias Ringwald * VBR (Variable Bitrate), based on input signal complexity 33*9a19cd78SMatthias Ringwald * ABR (Adaptative Bitrate). It does not rely on any bit reservoir, 34*9a19cd78SMatthias Ringwald * a frame will be strictly encoded in the bytes budget given by 35*9a19cd78SMatthias Ringwald * the user (or transport layer). 36*9a19cd78SMatthias Ringwald * 37*9a19cd78SMatthias Ringwald * However, the bitrate (bytes budget for encoding a frame) can be 38*9a19cd78SMatthias Ringwald * freely changed at any time. But will not rely on signal complexity, 39*9a19cd78SMatthias Ringwald * it can follow a temporary bandwidth increase or reduction. 40*9a19cd78SMatthias Ringwald * 41*9a19cd78SMatthias Ringwald * - Unlike classic codecs, the LC3 codecs does not run on fixed amount 42*9a19cd78SMatthias Ringwald * of samples as input. It operate only on fixed frame duration, for 43*9a19cd78SMatthias Ringwald * any supported samplerates (8 to 48 KHz). Two frame duration are 44*9a19cd78SMatthias Ringwald * available 7.5ms and 10ms. 45*9a19cd78SMatthias Ringwald * 46*9a19cd78SMatthias Ringwald * 47*9a19cd78SMatthias Ringwald * --- About 44.1 KHz samplerate --- 48*9a19cd78SMatthias Ringwald * 49*9a19cd78SMatthias Ringwald * The Bluetooth specification oddly add the 44.1 KHz samplerate. Although 50*9a19cd78SMatthias Ringwald * there is NO SUPPORT in the core algorithm of the codec of 44.1 KHz. 51*9a19cd78SMatthias Ringwald * We can summarize the 44.1 KHz support by "you can put any samplerate 52*9a19cd78SMatthias Ringwald * around the base defined samplerates". But be concerned by : 53*9a19cd78SMatthias Ringwald * 54*9a19cd78SMatthias Ringwald * 1. The frame size will not be 7.5 ms or 10 ms, but is scaled 55*9a19cd78SMatthias Ringwald * by 'supported samplerate' / 'input samplerate' 56*9a19cd78SMatthias Ringwald * 57*9a19cd78SMatthias Ringwald * 2. The bandwidth will be hard limited (to 20 KHz) if you select 48 KHz. 58*9a19cd78SMatthias Ringwald * The encoded bandwidth will also be affected by the above inverse 59*9a19cd78SMatthias Ringwald * factor of 20 KHz. 60*9a19cd78SMatthias Ringwald * 61*9a19cd78SMatthias Ringwald * Applied to 44.1 KHz, we get : 62*9a19cd78SMatthias Ringwald * 63*9a19cd78SMatthias Ringwald * 1. About 8.16 ms frame duration, instead of 7.5 ms 64*9a19cd78SMatthias Ringwald * About 10.88 ms frame duration, instead of 10 ms 65*9a19cd78SMatthias Ringwald * 66*9a19cd78SMatthias Ringwald * 2. The bandwidth becomes limited to 18.375 KHz 67*9a19cd78SMatthias Ringwald * 68*9a19cd78SMatthias Ringwald * 69*9a19cd78SMatthias Ringwald * --- How to encode / decode --- 70*9a19cd78SMatthias Ringwald * 71*9a19cd78SMatthias Ringwald * An encoder / decoder context need to be setup. This context keep states 72*9a19cd78SMatthias Ringwald * on the current stream to proceed, and samples that overlapped across 73*9a19cd78SMatthias Ringwald * frames. 74*9a19cd78SMatthias Ringwald * 75*9a19cd78SMatthias Ringwald * You have two ways to setup the encoder / decoder : 76*9a19cd78SMatthias Ringwald * 77*9a19cd78SMatthias Ringwald * - Using static memory allocation (this module does not rely on 78*9a19cd78SMatthias Ringwald * any dynamic memory allocation). The types `lc3_xxcoder_mem_16k_t`, 79*9a19cd78SMatthias Ringwald * and `lc3_xxcoder_mem_48k_t` have size of the memory needed for 80*9a19cd78SMatthias Ringwald * encoding up to 16 KHz or 48 KHz. 81*9a19cd78SMatthias Ringwald * 82*9a19cd78SMatthias Ringwald * - Using dynamic memory allocation. The `lc3_xxcoder_size()` procedure 83*9a19cd78SMatthias Ringwald * returns the needed memory size, for a given configuration. The memory 84*9a19cd78SMatthias Ringwald * space must be aligned to a pointer size. As an example, you can setup 85*9a19cd78SMatthias Ringwald * encoder like this : 86*9a19cd78SMatthias Ringwald * 87*9a19cd78SMatthias Ringwald * | enc = lc3_setup_encoder(frame_us, samplerate, 88*9a19cd78SMatthias Ringwald * | malloc(lc3_encoder_size(frame_us, samplerate))); 89*9a19cd78SMatthias Ringwald * | ... 90*9a19cd78SMatthias Ringwald * | free(enc); 91*9a19cd78SMatthias Ringwald * 92*9a19cd78SMatthias Ringwald * Note : 93*9a19cd78SMatthias Ringwald * - A NULL memory adress as input, will return a NULL encoder context. 94*9a19cd78SMatthias Ringwald * - The returned encoder handle is set at the address of the allocated 95*9a19cd78SMatthias Ringwald * memory space, you can directly free the handle. 96*9a19cd78SMatthias Ringwald * 97*9a19cd78SMatthias Ringwald * Next, call the `lc3_encode()` encoding procedure, for each frames. 98*9a19cd78SMatthias Ringwald * To handle multichannel streams (Stereo or more), you can proceed with 99*9a19cd78SMatthias Ringwald * inerleaved channels PCM stream like this : 100*9a19cd78SMatthias Ringwald * 101*9a19cd78SMatthias Ringwald * | for(int ich = 0; ich < nch: ich++) 102*9a19cd78SMatthias Ringwald * | lc3_encode(encoder[ich], pcm + ich, nch, ...); 103*9a19cd78SMatthias Ringwald * 104*9a19cd78SMatthias Ringwald * with `nch` as the number of channels in the PCM stream 105*9a19cd78SMatthias Ringwald */ 106*9a19cd78SMatthias Ringwald 107*9a19cd78SMatthias Ringwald #ifndef __LC3_H 108*9a19cd78SMatthias Ringwald #define __LC3_H 109*9a19cd78SMatthias Ringwald 110*9a19cd78SMatthias Ringwald #ifdef __cplusplus 111*9a19cd78SMatthias Ringwald extern "C" { 112*9a19cd78SMatthias Ringwald #endif 113*9a19cd78SMatthias Ringwald 114*9a19cd78SMatthias Ringwald #include <stdint.h> 115*9a19cd78SMatthias Ringwald #include <stdbool.h> 116*9a19cd78SMatthias Ringwald 117*9a19cd78SMatthias Ringwald #include "lc3_private.h" 118*9a19cd78SMatthias Ringwald 119*9a19cd78SMatthias Ringwald 120*9a19cd78SMatthias Ringwald /** 121*9a19cd78SMatthias Ringwald * Limitations 122*9a19cd78SMatthias Ringwald * - On the bitrate, in bps, of a stream 123*9a19cd78SMatthias Ringwald * - On the size of the frames in bytes 124*9a19cd78SMatthias Ringwald */ 125*9a19cd78SMatthias Ringwald 126*9a19cd78SMatthias Ringwald #define LC3_MIN_BITRATE 16000 127*9a19cd78SMatthias Ringwald #define LC3_MAX_BITRATE 320000 128*9a19cd78SMatthias Ringwald 129*9a19cd78SMatthias Ringwald #define LC3_MIN_FRAME_BYTES 20 130*9a19cd78SMatthias Ringwald #define LC3_MAX_FRAME_BYTES 400 131*9a19cd78SMatthias Ringwald 132*9a19cd78SMatthias Ringwald 133*9a19cd78SMatthias Ringwald /** 134*9a19cd78SMatthias Ringwald * Parameters check 135*9a19cd78SMatthias Ringwald * LC3_CHECK_DT_US(us) True when frame duration in us is suitable 136*9a19cd78SMatthias Ringwald * LC3_CHECK_SR_HZ(sr) True when samplerate in Hz is suitable 137*9a19cd78SMatthias Ringwald */ 138*9a19cd78SMatthias Ringwald 139*9a19cd78SMatthias Ringwald #define LC3_CHECK_DT_US(us) \ 140*9a19cd78SMatthias Ringwald ( ((us) == 7500) || ((us) == 10000) ) 141*9a19cd78SMatthias Ringwald 142*9a19cd78SMatthias Ringwald #define LC3_CHECK_SR_HZ(sr) \ 143*9a19cd78SMatthias Ringwald ( ((sr) == 8000) || ((sr) == 16000) || ((sr) == 24000) || \ 144*9a19cd78SMatthias Ringwald ((sr) == 32000) || ((sr) == 48000) ) 145*9a19cd78SMatthias Ringwald 146*9a19cd78SMatthias Ringwald 147*9a19cd78SMatthias Ringwald /** 148*9a19cd78SMatthias Ringwald * PCM Sample Format 149*9a19cd78SMatthias Ringwald * S16 Signed 16 bits, in 16 bits words (int16_t) 150*9a19cd78SMatthias Ringwald * S24 Signed 24 bits, using low three bytes of 32 bits words (int32_t). 151*9a19cd78SMatthias Ringwald * The high byte sign extends the sample value (bits 31..24 set to b23). 152*9a19cd78SMatthias Ringwald */ 153*9a19cd78SMatthias Ringwald 154*9a19cd78SMatthias Ringwald enum lc3_pcm_format { 155*9a19cd78SMatthias Ringwald LC3_PCM_FORMAT_S16, 156*9a19cd78SMatthias Ringwald LC3_PCM_FORMAT_S24, 157*9a19cd78SMatthias Ringwald }; 158*9a19cd78SMatthias Ringwald 159*9a19cd78SMatthias Ringwald 160*9a19cd78SMatthias Ringwald /** 161*9a19cd78SMatthias Ringwald * Handle 162*9a19cd78SMatthias Ringwald */ 163*9a19cd78SMatthias Ringwald 164*9a19cd78SMatthias Ringwald typedef struct lc3_encoder *lc3_encoder_t; 165*9a19cd78SMatthias Ringwald typedef struct lc3_decoder *lc3_decoder_t; 166*9a19cd78SMatthias Ringwald 167*9a19cd78SMatthias Ringwald 168*9a19cd78SMatthias Ringwald /** 169*9a19cd78SMatthias Ringwald * Static memory of encoder context 170*9a19cd78SMatthias Ringwald * 171*9a19cd78SMatthias Ringwald * Propose types suitable for static memory allocation, supporting 172*9a19cd78SMatthias Ringwald * any frame duration, and maximum samplerates 16k and 48k respectively 173*9a19cd78SMatthias Ringwald * You can customize your type using the `LC3_ENCODER_MEM_T` or 174*9a19cd78SMatthias Ringwald * `LC3_DECODER_MEM_T` macro. 175*9a19cd78SMatthias Ringwald */ 176*9a19cd78SMatthias Ringwald 177*9a19cd78SMatthias Ringwald typedef LC3_ENCODER_MEM_T(10000, 16000) lc3_encoder_mem_16k_t; 178*9a19cd78SMatthias Ringwald typedef LC3_ENCODER_MEM_T(10000, 48000) lc3_encoder_mem_48k_t; 179*9a19cd78SMatthias Ringwald 180*9a19cd78SMatthias Ringwald typedef LC3_DECODER_MEM_T(10000, 16000) lc3_decoder_mem_16k_t; 181*9a19cd78SMatthias Ringwald typedef LC3_DECODER_MEM_T(10000, 48000) lc3_decoder_mem_48k_t; 182*9a19cd78SMatthias Ringwald 183*9a19cd78SMatthias Ringwald 184*9a19cd78SMatthias Ringwald /** 185*9a19cd78SMatthias Ringwald * Return the number of PCM samples in a frame 186*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 187*9a19cd78SMatthias Ringwald * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 188*9a19cd78SMatthias Ringwald * return Number of PCM samples, -1 on bad parameters 189*9a19cd78SMatthias Ringwald */ 190*9a19cd78SMatthias Ringwald int lc3_frame_samples(int dt_us, int sr_hz); 191*9a19cd78SMatthias Ringwald 192*9a19cd78SMatthias Ringwald /** 193*9a19cd78SMatthias Ringwald * Return the size of frames, from bitrate 194*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 195*9a19cd78SMatthias Ringwald * bitrate Target bitrate in bit per seconds 196*9a19cd78SMatthias Ringwald * return The floor size in bytes of the frames, -1 on bad parameters 197*9a19cd78SMatthias Ringwald */ 198*9a19cd78SMatthias Ringwald int lc3_frame_bytes(int dt_us, int bitrate); 199*9a19cd78SMatthias Ringwald 200*9a19cd78SMatthias Ringwald /** 201*9a19cd78SMatthias Ringwald * Resolve the bitrate, from the size of frames 202*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 203*9a19cd78SMatthias Ringwald * nbytes Size in bytes of the frames 204*9a19cd78SMatthias Ringwald * return The according bitrate in bps, -1 on bad parameters 205*9a19cd78SMatthias Ringwald */ 206*9a19cd78SMatthias Ringwald int lc3_resolve_bitrate(int dt_us, int nbytes); 207*9a19cd78SMatthias Ringwald 208*9a19cd78SMatthias Ringwald /** 209*9a19cd78SMatthias Ringwald * Return algorithmic delay, as a number of samples 210*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 211*9a19cd78SMatthias Ringwald * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 212*9a19cd78SMatthias Ringwald * return Number of algorithmic delay samples, -1 on bad parameters 213*9a19cd78SMatthias Ringwald */ 214*9a19cd78SMatthias Ringwald int lc3_delay_samples(int dt_us, int sr_hz); 215*9a19cd78SMatthias Ringwald 216*9a19cd78SMatthias Ringwald /** 217*9a19cd78SMatthias Ringwald * Return size needed for an encoder 218*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 219*9a19cd78SMatthias Ringwald * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 220*9a19cd78SMatthias Ringwald * return Size of then encoder in bytes, 0 on bad parameters 221*9a19cd78SMatthias Ringwald * 222*9a19cd78SMatthias Ringwald * The `sr_hz` parameter is the samplerate of the PCM input stream, 223*9a19cd78SMatthias Ringwald * and will match `sr_pcm_hz` of `lc3_setup_encoder()`. 224*9a19cd78SMatthias Ringwald */ 225*9a19cd78SMatthias Ringwald unsigned lc3_encoder_size(int dt_us, int sr_hz); 226*9a19cd78SMatthias Ringwald 227*9a19cd78SMatthias Ringwald /** 228*9a19cd78SMatthias Ringwald * Setup encoder 229*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 230*9a19cd78SMatthias Ringwald * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 231*9a19cd78SMatthias Ringwald * sr_pcm_hz Input samplerate, downsampling option of input, or 0 232*9a19cd78SMatthias Ringwald * mem Encoder memory space, aligned to pointer type 233*9a19cd78SMatthias Ringwald * return Encoder as an handle, NULL on bad parameters 234*9a19cd78SMatthias Ringwald * 235*9a19cd78SMatthias Ringwald * The `sr_pcm_hz` parameter is a downsampling option of PCM input, 236*9a19cd78SMatthias Ringwald * the value `0` fallback to the samplerate of the encoded stream `sr_hz`. 237*9a19cd78SMatthias Ringwald * When used, `sr_pcm_hz` is intended to be higher or equal to the encoder 238*9a19cd78SMatthias Ringwald * samplerate `sr_hz`. The size of the context needed, given by 239*9a19cd78SMatthias Ringwald * `lc3_encoder_size()` will be set accordingly to `sr_pcm_hz`. 240*9a19cd78SMatthias Ringwald */ 241*9a19cd78SMatthias Ringwald lc3_encoder_t lc3_setup_encoder( 242*9a19cd78SMatthias Ringwald int dt_us, int sr_hz, int sr_pcm_hz, void *mem); 243*9a19cd78SMatthias Ringwald 244*9a19cd78SMatthias Ringwald /** 245*9a19cd78SMatthias Ringwald * Encode a frame 246*9a19cd78SMatthias Ringwald * encoder Handle of the encoder 247*9a19cd78SMatthias Ringwald * fmt PCM input format 248*9a19cd78SMatthias Ringwald * pcm, stride Input PCM samples, and count between two consecutives 249*9a19cd78SMatthias Ringwald * nbytes Target size, in bytes, of the frame (20 to 400) 250*9a19cd78SMatthias Ringwald * out Output buffer of `nbytes` size 251*9a19cd78SMatthias Ringwald * return 0: On success -1: Wrong parameters 252*9a19cd78SMatthias Ringwald */ 253*9a19cd78SMatthias Ringwald int lc3_encode(lc3_encoder_t encoder, enum lc3_pcm_format fmt, 254*9a19cd78SMatthias Ringwald const void *pcm, int stride, int nbytes, void *out); 255*9a19cd78SMatthias Ringwald 256*9a19cd78SMatthias Ringwald /** 257*9a19cd78SMatthias Ringwald * Return size needed for an decoder 258*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 259*9a19cd78SMatthias Ringwald * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 260*9a19cd78SMatthias Ringwald * return Size of then decoder in bytes, 0 on bad parameters 261*9a19cd78SMatthias Ringwald * 262*9a19cd78SMatthias Ringwald * The `sr_hz` parameter is the samplerate of the PCM output stream, 263*9a19cd78SMatthias Ringwald * and will match `sr_pcm_hz` of `lc3_setup_decoder()`. 264*9a19cd78SMatthias Ringwald */ 265*9a19cd78SMatthias Ringwald unsigned lc3_decoder_size(int dt_us, int sr_hz); 266*9a19cd78SMatthias Ringwald 267*9a19cd78SMatthias Ringwald /** 268*9a19cd78SMatthias Ringwald * Setup decoder 269*9a19cd78SMatthias Ringwald * dt_us Frame duration in us, 7500 or 10000 270*9a19cd78SMatthias Ringwald * sr_hz Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000 271*9a19cd78SMatthias Ringwald * sr_pcm_hz Output samplerate, upsampling option of output (or 0) 272*9a19cd78SMatthias Ringwald * mem Decoder memory space, aligned to pointer type 273*9a19cd78SMatthias Ringwald * return Decoder as an handle, NULL on bad parameters 274*9a19cd78SMatthias Ringwald * 275*9a19cd78SMatthias Ringwald * The `sr_pcm_hz` parameter is an upsampling option of PCM output, 276*9a19cd78SMatthias Ringwald * the value `0` fallback to the samplerate of the decoded stream `sr_hz`. 277*9a19cd78SMatthias Ringwald * When used, `sr_pcm_hz` is intended to be higher or equal to the decoder 278*9a19cd78SMatthias Ringwald * samplerate `sr_hz`. The size of the context needed, given by 279*9a19cd78SMatthias Ringwald * `lc3_decoder_size()` will be set accordingly to `sr_pcm_hz`. 280*9a19cd78SMatthias Ringwald */ 281*9a19cd78SMatthias Ringwald lc3_decoder_t lc3_setup_decoder( 282*9a19cd78SMatthias Ringwald int dt_us, int sr_hz, int sr_pcm_hz, void *mem); 283*9a19cd78SMatthias Ringwald 284*9a19cd78SMatthias Ringwald /** 285*9a19cd78SMatthias Ringwald * Decode a frame 286*9a19cd78SMatthias Ringwald * decoder Handle of the decoder 287*9a19cd78SMatthias Ringwald * in, nbytes Input bitstream, and size in bytes, NULL performs PLC 288*9a19cd78SMatthias Ringwald * fmt PCM output format 289*9a19cd78SMatthias Ringwald * pcm, stride Output PCM samples, and count between two consecutives 290*9a19cd78SMatthias Ringwald * return 0: On success 1: PLC operated -1: Wrong parameters 291*9a19cd78SMatthias Ringwald */ 292*9a19cd78SMatthias Ringwald int lc3_decode(lc3_decoder_t decoder, const void *in, int nbytes, 293*9a19cd78SMatthias Ringwald enum lc3_pcm_format fmt, void *pcm, int stride); 294*9a19cd78SMatthias Ringwald 295*9a19cd78SMatthias Ringwald 296*9a19cd78SMatthias Ringwald #ifdef __cplusplus 297*9a19cd78SMatthias Ringwald } 298*9a19cd78SMatthias Ringwald #endif 299*9a19cd78SMatthias Ringwald 300*9a19cd78SMatthias Ringwald #endif /* __LC3_H */ 301