xref: /btstack/3rd-party/lc3-google/include/lc3.h (revision 4930cef6e21e6da2d7571b9259c7f0fb8bed3d01)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
3*4930cef6SMatthias Ringwald  *  Copyright 2022 Google LLC
49a19cd78SMatthias Ringwald  *
59a19cd78SMatthias Ringwald  *  Licensed under the Apache License, Version 2.0 (the "License");
69a19cd78SMatthias Ringwald  *  you may not use this file except in compliance with the License.
79a19cd78SMatthias Ringwald  *  You may obtain a copy of the License at:
89a19cd78SMatthias Ringwald  *
99a19cd78SMatthias Ringwald  *  http://www.apache.org/licenses/LICENSE-2.0
109a19cd78SMatthias Ringwald  *
119a19cd78SMatthias Ringwald  *  Unless required by applicable law or agreed to in writing, software
129a19cd78SMatthias Ringwald  *  distributed under the License is distributed on an "AS IS" BASIS,
139a19cd78SMatthias Ringwald  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a19cd78SMatthias Ringwald  *  See the License for the specific language governing permissions and
159a19cd78SMatthias Ringwald  *  limitations under the License.
169a19cd78SMatthias Ringwald  *
179a19cd78SMatthias Ringwald  ******************************************************************************/
189a19cd78SMatthias Ringwald 
199a19cd78SMatthias Ringwald /**
209a19cd78SMatthias Ringwald  * Low Complexity Communication Codec (LC3)
219a19cd78SMatthias Ringwald  *
229a19cd78SMatthias Ringwald  * This implementation conforms to :
239a19cd78SMatthias Ringwald  *   Low Complexity Communication Codec (LC3)
249a19cd78SMatthias Ringwald  *   Bluetooth Specification v1.0
259a19cd78SMatthias Ringwald  *
269a19cd78SMatthias Ringwald  *
279a19cd78SMatthias Ringwald  * The LC3 is an efficient low latency audio codec.
289a19cd78SMatthias Ringwald  *
299a19cd78SMatthias Ringwald  * - Unlike most other codecs, the LC3 codec is focused on audio streaming
309a19cd78SMatthias Ringwald  *   in constrained (on packet sizes and interval) tranport layer.
319a19cd78SMatthias Ringwald  *   In this way, the LC3 does not handle :
329a19cd78SMatthias Ringwald  *   VBR (Variable Bitrate), based on input signal complexity
339a19cd78SMatthias Ringwald  *   ABR (Adaptative Bitrate). It does not rely on any bit reservoir,
349a19cd78SMatthias Ringwald  *       a frame will be strictly encoded in the bytes budget given by
359a19cd78SMatthias Ringwald  *       the user (or transport layer).
369a19cd78SMatthias Ringwald  *
379a19cd78SMatthias Ringwald  *   However, the bitrate (bytes budget for encoding a frame) can be
389a19cd78SMatthias Ringwald  *   freely changed at any time. But will not rely on signal complexity,
399a19cd78SMatthias Ringwald  *   it can follow a temporary bandwidth increase or reduction.
409a19cd78SMatthias Ringwald  *
419a19cd78SMatthias Ringwald  * - Unlike classic codecs, the LC3 codecs does not run on fixed amount
42*4930cef6SMatthias Ringwald  *   of samples as input. It operates only on fixed frame duration, for
43*4930cef6SMatthias Ringwald  *   any supported samplerates (8 to 48 KHz). Two frames duration are
449a19cd78SMatthias Ringwald  *   available 7.5ms and 10ms.
459a19cd78SMatthias Ringwald  *
469a19cd78SMatthias Ringwald  *
479a19cd78SMatthias Ringwald  * --- About 44.1 KHz samplerate ---
489a19cd78SMatthias Ringwald  *
49*4930cef6SMatthias Ringwald  * The Bluetooth specification reference the 44.1 KHz samplerate, although
50*4930cef6SMatthias Ringwald  * there is no support in the core algorithm of the codec of 44.1 KHz.
519a19cd78SMatthias Ringwald  * We can summarize the 44.1 KHz support by "you can put any samplerate
52*4930cef6SMatthias Ringwald  * around the defined base samplerates". Please mind the following items :
539a19cd78SMatthias Ringwald  *
549a19cd78SMatthias Ringwald  *   1. The frame size will not be 7.5 ms or 10 ms, but is scaled
559a19cd78SMatthias Ringwald  *      by 'supported samplerate' / 'input samplerate'
569a19cd78SMatthias Ringwald  *
579a19cd78SMatthias Ringwald  *   2. The bandwidth will be hard limited (to 20 KHz) if you select 48 KHz.
589a19cd78SMatthias Ringwald  *      The encoded bandwidth will also be affected by the above inverse
599a19cd78SMatthias Ringwald  *      factor of 20 KHz.
609a19cd78SMatthias Ringwald  *
619a19cd78SMatthias Ringwald  * Applied to 44.1 KHz, we get :
629a19cd78SMatthias Ringwald  *
639a19cd78SMatthias Ringwald  *   1. About  8.16 ms frame duration, instead of 7.5 ms
649a19cd78SMatthias Ringwald  *      About 10.88 ms frame duration, instead of  10 ms
659a19cd78SMatthias Ringwald  *
669a19cd78SMatthias Ringwald  *   2. The bandwidth becomes limited to 18.375 KHz
679a19cd78SMatthias Ringwald  *
689a19cd78SMatthias Ringwald  *
699a19cd78SMatthias Ringwald  * --- How to encode / decode ---
709a19cd78SMatthias Ringwald  *
71*4930cef6SMatthias Ringwald  * An encoder / decoder context needs to be setup. This context keeps states
729a19cd78SMatthias Ringwald  * on the current stream to proceed, and samples that overlapped across
739a19cd78SMatthias Ringwald  * frames.
749a19cd78SMatthias Ringwald  *
759a19cd78SMatthias Ringwald  * You have two ways to setup the encoder / decoder :
769a19cd78SMatthias Ringwald  *
779a19cd78SMatthias Ringwald  * - Using static memory allocation (this module does not rely on
789a19cd78SMatthias Ringwald  *   any dynamic memory allocation). The types `lc3_xxcoder_mem_16k_t`,
799a19cd78SMatthias Ringwald  *   and `lc3_xxcoder_mem_48k_t` have size of the memory needed for
809a19cd78SMatthias Ringwald  *   encoding up to 16 KHz or 48 KHz.
819a19cd78SMatthias Ringwald  *
829a19cd78SMatthias Ringwald  * - Using dynamic memory allocation. The `lc3_xxcoder_size()` procedure
839a19cd78SMatthias Ringwald  *   returns the needed memory size, for a given configuration. The memory
849a19cd78SMatthias Ringwald  *   space must be aligned to a pointer size. As an example, you can setup
859a19cd78SMatthias Ringwald  *   encoder like this :
869a19cd78SMatthias Ringwald  *
879a19cd78SMatthias Ringwald  *   | enc = lc3_setup_encoder(frame_us, samplerate,
889a19cd78SMatthias Ringwald  *   |      malloc(lc3_encoder_size(frame_us, samplerate)));
899a19cd78SMatthias Ringwald  *   | ...
909a19cd78SMatthias Ringwald  *   | free(enc);
919a19cd78SMatthias Ringwald  *
929a19cd78SMatthias Ringwald  *   Note :
939a19cd78SMatthias Ringwald  *   - A NULL memory adress as input, will return a NULL encoder context.
949a19cd78SMatthias Ringwald  *   - The returned encoder handle is set at the address of the allocated
959a19cd78SMatthias Ringwald  *     memory space, you can directly free the handle.
969a19cd78SMatthias Ringwald  *
979a19cd78SMatthias Ringwald  * Next, call the `lc3_encode()` encoding procedure, for each frames.
989a19cd78SMatthias Ringwald  * To handle multichannel streams (Stereo or more), you can proceed with
99*4930cef6SMatthias Ringwald  * interleaved channels PCM stream like this :
1009a19cd78SMatthias Ringwald  *
1019a19cd78SMatthias Ringwald  *   | for(int ich = 0; ich < nch: ich++)
1029a19cd78SMatthias Ringwald  *   |     lc3_encode(encoder[ich], pcm + ich, nch, ...);
1039a19cd78SMatthias Ringwald  *
1049a19cd78SMatthias Ringwald  *   with `nch` as the number of channels in the PCM stream
105*4930cef6SMatthias Ringwald  *
106*4930cef6SMatthias Ringwald  * ---
107*4930cef6SMatthias Ringwald  *
108*4930cef6SMatthias Ringwald  * Antoine SOULIER, Tempow / Google LLC
109*4930cef6SMatthias Ringwald  *
1109a19cd78SMatthias Ringwald  */
1119a19cd78SMatthias Ringwald 
1129a19cd78SMatthias Ringwald #ifndef __LC3_H
1139a19cd78SMatthias Ringwald #define __LC3_H
1149a19cd78SMatthias Ringwald 
1159a19cd78SMatthias Ringwald #ifdef __cplusplus
1169a19cd78SMatthias Ringwald extern "C" {
1179a19cd78SMatthias Ringwald #endif
1189a19cd78SMatthias Ringwald 
1199a19cd78SMatthias Ringwald #include <stdint.h>
1209a19cd78SMatthias Ringwald #include <stdbool.h>
1219a19cd78SMatthias Ringwald 
1229a19cd78SMatthias Ringwald #include "lc3_private.h"
1239a19cd78SMatthias Ringwald 
1249a19cd78SMatthias Ringwald 
1259a19cd78SMatthias Ringwald /**
1269a19cd78SMatthias Ringwald  * Limitations
1279a19cd78SMatthias Ringwald  * - On the bitrate, in bps, of a stream
1289a19cd78SMatthias Ringwald  * - On the size of the frames in bytes
1299a19cd78SMatthias Ringwald  */
1309a19cd78SMatthias Ringwald 
1319a19cd78SMatthias Ringwald #define LC3_MIN_BITRATE    16000
1329a19cd78SMatthias Ringwald #define LC3_MAX_BITRATE   320000
1339a19cd78SMatthias Ringwald 
1349a19cd78SMatthias Ringwald #define LC3_MIN_FRAME_BYTES   20
1359a19cd78SMatthias Ringwald #define LC3_MAX_FRAME_BYTES  400
1369a19cd78SMatthias Ringwald 
1379a19cd78SMatthias Ringwald 
1389a19cd78SMatthias Ringwald /**
1399a19cd78SMatthias Ringwald  * Parameters check
1409a19cd78SMatthias Ringwald  *   LC3_CHECK_DT_US(us)  True when frame duration in us is suitable
1419a19cd78SMatthias Ringwald  *   LC3_CHECK_SR_HZ(sr)  True when samplerate in Hz is suitable
1429a19cd78SMatthias Ringwald  */
1439a19cd78SMatthias Ringwald 
1449a19cd78SMatthias Ringwald #define LC3_CHECK_DT_US(us) \
1459a19cd78SMatthias Ringwald     ( ((us) == 7500) || ((us) == 10000) )
1469a19cd78SMatthias Ringwald 
1479a19cd78SMatthias Ringwald #define LC3_CHECK_SR_HZ(sr) \
1489a19cd78SMatthias Ringwald     ( ((sr) ==  8000) || ((sr) == 16000) || ((sr) == 24000) || \
1499a19cd78SMatthias Ringwald       ((sr) == 32000) || ((sr) == 48000)                       )
1509a19cd78SMatthias Ringwald 
1519a19cd78SMatthias Ringwald 
1529a19cd78SMatthias Ringwald /**
1539a19cd78SMatthias Ringwald  * PCM Sample Format
1549a19cd78SMatthias Ringwald  *   S16  Signed 16 bits, in 16 bits words (int16_t)
1559a19cd78SMatthias Ringwald  *   S24  Signed 24 bits, using low three bytes of 32 bits words (int32_t).
1569a19cd78SMatthias Ringwald  *        The high byte sign extends the sample value (bits 31..24 set to b23).
1579a19cd78SMatthias Ringwald  */
1589a19cd78SMatthias Ringwald 
1599a19cd78SMatthias Ringwald enum lc3_pcm_format {
1609a19cd78SMatthias Ringwald     LC3_PCM_FORMAT_S16,
1619a19cd78SMatthias Ringwald     LC3_PCM_FORMAT_S24,
1629a19cd78SMatthias Ringwald };
1639a19cd78SMatthias Ringwald 
1649a19cd78SMatthias Ringwald 
1659a19cd78SMatthias Ringwald /**
1669a19cd78SMatthias Ringwald  * Handle
1679a19cd78SMatthias Ringwald  */
1689a19cd78SMatthias Ringwald 
1699a19cd78SMatthias Ringwald typedef struct lc3_encoder *lc3_encoder_t;
1709a19cd78SMatthias Ringwald typedef struct lc3_decoder *lc3_decoder_t;
1719a19cd78SMatthias Ringwald 
1729a19cd78SMatthias Ringwald 
1739a19cd78SMatthias Ringwald /**
1749a19cd78SMatthias Ringwald  * Static memory of encoder context
1759a19cd78SMatthias Ringwald  *
1769a19cd78SMatthias Ringwald  * Propose types suitable for static memory allocation, supporting
1779a19cd78SMatthias Ringwald  * any frame duration, and maximum samplerates 16k and 48k respectively
1789a19cd78SMatthias Ringwald  * You can customize your type using the `LC3_ENCODER_MEM_T` or
1799a19cd78SMatthias Ringwald  * `LC3_DECODER_MEM_T` macro.
1809a19cd78SMatthias Ringwald  */
1819a19cd78SMatthias Ringwald 
1829a19cd78SMatthias Ringwald typedef LC3_ENCODER_MEM_T(10000, 16000) lc3_encoder_mem_16k_t;
1839a19cd78SMatthias Ringwald typedef LC3_ENCODER_MEM_T(10000, 48000) lc3_encoder_mem_48k_t;
1849a19cd78SMatthias Ringwald 
1859a19cd78SMatthias Ringwald typedef LC3_DECODER_MEM_T(10000, 16000) lc3_decoder_mem_16k_t;
1869a19cd78SMatthias Ringwald typedef LC3_DECODER_MEM_T(10000, 48000) lc3_decoder_mem_48k_t;
1879a19cd78SMatthias Ringwald 
1889a19cd78SMatthias Ringwald 
1899a19cd78SMatthias Ringwald /**
1909a19cd78SMatthias Ringwald  * Return the number of PCM samples in a frame
1919a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
1929a19cd78SMatthias Ringwald  * sr_hz           Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000
1939a19cd78SMatthias Ringwald  * return          Number of PCM samples, -1 on bad parameters
1949a19cd78SMatthias Ringwald  */
1959a19cd78SMatthias Ringwald int lc3_frame_samples(int dt_us, int sr_hz);
1969a19cd78SMatthias Ringwald 
1979a19cd78SMatthias Ringwald /**
1989a19cd78SMatthias Ringwald  * Return the size of frames, from bitrate
1999a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
200*4930cef6SMatthias Ringwald  * bitrate         Target bitrate in bit per second
2019a19cd78SMatthias Ringwald  * return          The floor size in bytes of the frames, -1 on bad parameters
2029a19cd78SMatthias Ringwald  */
2039a19cd78SMatthias Ringwald int lc3_frame_bytes(int dt_us, int bitrate);
2049a19cd78SMatthias Ringwald 
2059a19cd78SMatthias Ringwald /**
2069a19cd78SMatthias Ringwald  * Resolve the bitrate, from the size of frames
2079a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
2089a19cd78SMatthias Ringwald  * nbytes          Size in bytes of the frames
2099a19cd78SMatthias Ringwald  * return          The according bitrate in bps, -1 on bad parameters
2109a19cd78SMatthias Ringwald  */
2119a19cd78SMatthias Ringwald int lc3_resolve_bitrate(int dt_us, int nbytes);
2129a19cd78SMatthias Ringwald 
2139a19cd78SMatthias Ringwald /**
2149a19cd78SMatthias Ringwald  * Return algorithmic delay, as a number of samples
2159a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
2169a19cd78SMatthias Ringwald  * sr_hz           Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000
2179a19cd78SMatthias Ringwald  * return          Number of algorithmic delay samples, -1 on bad parameters
2189a19cd78SMatthias Ringwald  */
2199a19cd78SMatthias Ringwald int lc3_delay_samples(int dt_us, int sr_hz);
2209a19cd78SMatthias Ringwald 
2219a19cd78SMatthias Ringwald /**
2229a19cd78SMatthias Ringwald  * Return size needed for an encoder
2239a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
2249a19cd78SMatthias Ringwald  * sr_hz           Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000
2259a19cd78SMatthias Ringwald  * return          Size of then encoder in bytes, 0 on bad parameters
2269a19cd78SMatthias Ringwald  *
2279a19cd78SMatthias Ringwald  * The `sr_hz` parameter is the samplerate of the PCM input stream,
2289a19cd78SMatthias Ringwald  * and will match `sr_pcm_hz` of `lc3_setup_encoder()`.
2299a19cd78SMatthias Ringwald  */
2309a19cd78SMatthias Ringwald unsigned lc3_encoder_size(int dt_us, int sr_hz);
2319a19cd78SMatthias Ringwald 
2329a19cd78SMatthias Ringwald /**
2339a19cd78SMatthias Ringwald  * Setup encoder
2349a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
2359a19cd78SMatthias Ringwald  * sr_hz           Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000
2369a19cd78SMatthias Ringwald  * sr_pcm_hz       Input samplerate, downsampling option of input, or 0
2379a19cd78SMatthias Ringwald  * mem             Encoder memory space, aligned to pointer type
2389a19cd78SMatthias Ringwald  * return          Encoder as an handle, NULL on bad parameters
2399a19cd78SMatthias Ringwald  *
2409a19cd78SMatthias Ringwald  * The `sr_pcm_hz` parameter is a downsampling option of PCM input,
2419a19cd78SMatthias Ringwald  * the value `0` fallback to the samplerate of the encoded stream `sr_hz`.
2429a19cd78SMatthias Ringwald  * When used, `sr_pcm_hz` is intended to be higher or equal to the encoder
2439a19cd78SMatthias Ringwald  * samplerate `sr_hz`. The size of the context needed, given by
2449a19cd78SMatthias Ringwald  * `lc3_encoder_size()` will be set accordingly to `sr_pcm_hz`.
2459a19cd78SMatthias Ringwald  */
2469a19cd78SMatthias Ringwald lc3_encoder_t lc3_setup_encoder(
2479a19cd78SMatthias Ringwald     int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
2489a19cd78SMatthias Ringwald 
2499a19cd78SMatthias Ringwald /**
2509a19cd78SMatthias Ringwald  * Encode a frame
2519a19cd78SMatthias Ringwald  * encoder         Handle of the encoder
2529a19cd78SMatthias Ringwald  * fmt             PCM input format
2539a19cd78SMatthias Ringwald  * pcm, stride     Input PCM samples, and count between two consecutives
2549a19cd78SMatthias Ringwald  * nbytes          Target size, in bytes, of the frame (20 to 400)
2559a19cd78SMatthias Ringwald  * out             Output buffer of `nbytes` size
2569a19cd78SMatthias Ringwald  * return          0: On success  -1: Wrong parameters
2579a19cd78SMatthias Ringwald  */
2589a19cd78SMatthias Ringwald int lc3_encode(lc3_encoder_t encoder, enum lc3_pcm_format fmt,
2599a19cd78SMatthias Ringwald     const void *pcm, int stride, int nbytes, void *out);
2609a19cd78SMatthias Ringwald 
2619a19cd78SMatthias Ringwald /**
2629a19cd78SMatthias Ringwald  * Return size needed for an decoder
2639a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
2649a19cd78SMatthias Ringwald  * sr_hz           Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000
2659a19cd78SMatthias Ringwald  * return          Size of then decoder in bytes, 0 on bad parameters
2669a19cd78SMatthias Ringwald  *
2679a19cd78SMatthias Ringwald  * The `sr_hz` parameter is the samplerate of the PCM output stream,
2689a19cd78SMatthias Ringwald  * and will match `sr_pcm_hz` of `lc3_setup_decoder()`.
2699a19cd78SMatthias Ringwald  */
2709a19cd78SMatthias Ringwald unsigned lc3_decoder_size(int dt_us, int sr_hz);
2719a19cd78SMatthias Ringwald 
2729a19cd78SMatthias Ringwald /**
2739a19cd78SMatthias Ringwald  * Setup decoder
2749a19cd78SMatthias Ringwald  * dt_us           Frame duration in us, 7500 or 10000
2759a19cd78SMatthias Ringwald  * sr_hz           Samplerate in Hz, 8000, 16000, 24000, 32000 or 48000
2769a19cd78SMatthias Ringwald  * sr_pcm_hz       Output samplerate, upsampling option of output (or 0)
2779a19cd78SMatthias Ringwald  * mem             Decoder memory space, aligned to pointer type
2789a19cd78SMatthias Ringwald  * return          Decoder as an handle, NULL on bad parameters
2799a19cd78SMatthias Ringwald  *
2809a19cd78SMatthias Ringwald  * The `sr_pcm_hz` parameter is an upsampling option of PCM output,
2819a19cd78SMatthias Ringwald  * the value `0` fallback to the samplerate of the decoded stream `sr_hz`.
2829a19cd78SMatthias Ringwald  * When used, `sr_pcm_hz` is intended to be higher or equal to the decoder
2839a19cd78SMatthias Ringwald  * samplerate `sr_hz`. The size of the context needed, given by
2849a19cd78SMatthias Ringwald  * `lc3_decoder_size()` will be set accordingly to `sr_pcm_hz`.
2859a19cd78SMatthias Ringwald  */
2869a19cd78SMatthias Ringwald lc3_decoder_t lc3_setup_decoder(
2879a19cd78SMatthias Ringwald     int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
2889a19cd78SMatthias Ringwald 
2899a19cd78SMatthias Ringwald /**
2909a19cd78SMatthias Ringwald  * Decode a frame
2919a19cd78SMatthias Ringwald  * decoder         Handle of the decoder
2929a19cd78SMatthias Ringwald  * in, nbytes      Input bitstream, and size in bytes, NULL performs PLC
2939a19cd78SMatthias Ringwald  * fmt             PCM output format
2949a19cd78SMatthias Ringwald  * pcm, stride     Output PCM samples, and count between two consecutives
2959a19cd78SMatthias Ringwald  * return          0: On success  1: PLC operated  -1: Wrong parameters
2969a19cd78SMatthias Ringwald  */
2979a19cd78SMatthias Ringwald int lc3_decode(lc3_decoder_t decoder, const void *in, int nbytes,
2989a19cd78SMatthias Ringwald     enum lc3_pcm_format fmt, void *pcm, int stride);
2999a19cd78SMatthias Ringwald 
3009a19cd78SMatthias Ringwald 
3019a19cd78SMatthias Ringwald #ifdef __cplusplus
3029a19cd78SMatthias Ringwald }
3039a19cd78SMatthias Ringwald #endif
3049a19cd78SMatthias Ringwald 
3059a19cd78SMatthias Ringwald #endif /* __LC3_H */
306