xref: /btstack/3rd-party/lc3-google/include/lc3.h (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
19a19cd78SMatthias Ringwald /******************************************************************************
29a19cd78SMatthias Ringwald  *
34930cef6SMatthias 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 :
23*6897da5cSDirk Helbig  *
24*6897da5cSDirk Helbig  *   - Low Complexity Communication Codec (LC3)
259a19cd78SMatthias Ringwald  *     Bluetooth Specification v1.0
269a19cd78SMatthias Ringwald  *
27*6897da5cSDirk Helbig  *   - ETSI TS 103 634 v1.4.1
28*6897da5cSDirk Helbig  *     Digital Enhanced Cordless Telecommunications (DECT)
29*6897da5cSDirk Helbig  *     Low Complexity Communication Codec plus (LC3plus)
309a19cd78SMatthias Ringwald  *
31*6897da5cSDirk Helbig  * LC3 and LC3 Plus are audio codecs designed for low-latency audio transport.
329a19cd78SMatthias Ringwald  *
339a19cd78SMatthias Ringwald  * - Unlike most other codecs, the LC3 codec is focused on audio streaming
349a19cd78SMatthias Ringwald  *   in constrained (on packet sizes and interval) tranport layer.
359a19cd78SMatthias Ringwald  *   In this way, the LC3 does not handle :
369a19cd78SMatthias Ringwald  *   VBR (Variable Bitrate), based on input signal complexity
379a19cd78SMatthias Ringwald  *   ABR (Adaptative Bitrate). It does not rely on any bit reservoir,
389a19cd78SMatthias Ringwald  *       a frame will be strictly encoded in the bytes budget given by
399a19cd78SMatthias Ringwald  *       the user (or transport layer).
409a19cd78SMatthias Ringwald  *
419a19cd78SMatthias Ringwald  *   However, the bitrate (bytes budget for encoding a frame) can be
429a19cd78SMatthias Ringwald  *   freely changed at any time. But will not rely on signal complexity,
439a19cd78SMatthias Ringwald  *   it can follow a temporary bandwidth increase or reduction.
449a19cd78SMatthias Ringwald  *
459a19cd78SMatthias Ringwald  * - Unlike classic codecs, the LC3 codecs does not run on fixed amount
464930cef6SMatthias Ringwald  *   of samples as input. It operates only on fixed frame duration, for
474930cef6SMatthias Ringwald  *   any supported sample rates (8 to 48 KHz). Two frames duration are
489a19cd78SMatthias Ringwald  *   available 7.5ms and 10ms.
499a19cd78SMatthias Ringwald  *
509a19cd78SMatthias Ringwald  *
51*6897da5cSDirk Helbig  * --- LC3 Plus features ---
52*6897da5cSDirk Helbig  *
53*6897da5cSDirk Helbig  * In addition to LC3, following features of LC3 Plus are proposed:
54*6897da5cSDirk Helbig  * - Frame duration of 2.5 and 5ms.
55*6897da5cSDirk Helbig  * - High-Resolution mode, 48 KHz, and 96 kHz sampling rates.
56*6897da5cSDirk Helbig  *
57*6897da5cSDirk Helbig  * The distinction between LC3 and LC3 plus is made according to :
58*6897da5cSDirk Helbig  *
59*6897da5cSDirk Helbig  *      Frame Duration  | 2.5ms |  5ms  | 7.5ms | 10 ms |
60*6897da5cSDirk Helbig  *     ---------------- | ----- | ----- | ----- | ----- |
61*6897da5cSDirk Helbig  *      LC3             |       |       |   X   |   X   |
62*6897da5cSDirk Helbig  *      LC3 Plus        |   X   |   X   |       |   X   |
63*6897da5cSDirk Helbig  *
64*6897da5cSDirk Helbig  * The 10 ms frame duration is available in LC3 and LC3 plus standard.
65*6897da5cSDirk Helbig  * In this mode, the produced bitstream can be referenced either
66*6897da5cSDirk Helbig  * as LC3 or LC3 plus.
67*6897da5cSDirk Helbig  *
68*6897da5cSDirk Helbig  * The LC3 Plus high-resolution mode should be preferred at high bitrates
69*6897da5cSDirk Helbig  * and larger audio bandwidth. In this mode, the audio bandwidth is always
70*6897da5cSDirk Helbig  * up to the Nyquist frequency, compared to LC3 at 48 KHz, which limits
71*6897da5cSDirk Helbig  * the bandwidth to 20 KHz.
72*6897da5cSDirk Helbig  *
73*6897da5cSDirk Helbig  *
74*6897da5cSDirk Helbig  * --- Bit rate ---
75*6897da5cSDirk Helbig  *
76*6897da5cSDirk Helbig  * The proposed implementation accepts any frame sizes between 20 and 400 Bytes
77*6897da5cSDirk Helbig  * for non-high-resolution mode. Mind that the LC3 Plus standard defines
78*6897da5cSDirk Helbig  * smaller sizes for frame durations shorter than 10 ms and/or sampling rates
79*6897da5cSDirk Helbig  * less than 48 kHz.
80*6897da5cSDirk Helbig  *
81*6897da5cSDirk Helbig  * In High-Resolution mode, the frame sizes (and bitrates) are restricted
82*6897da5cSDirk Helbig  * as follows:
83*6897da5cSDirk Helbig  *
84*6897da5cSDirk Helbig  *      HR Configuration  |  Frame sizes  | Bitrate (kbps) |
85*6897da5cSDirk Helbig  *     ------------------ | ------------- | -------------- |
86*6897da5cSDirk Helbig  *        10 ms - 48 KHz  |   156 to 625  |   124.8 - 500  |
87*6897da5cSDirk Helbig  *        10 ms - 96 KHz  |   187 to 625  |   149.6 - 500  |
88*6897da5cSDirk Helbig  *     ------------------ | ------------- | -------------- |
89*6897da5cSDirk Helbig  *         5 ms - 48 KHz  |    93 to 375  |   148.8 - 600  |
90*6897da5cSDirk Helbig  *         5 ms - 96 KHz  |   109 to 375  |   174.4 - 600  |
91*6897da5cSDirk Helbig  *     ------------------ | ------------- | -------------- |
92*6897da5cSDirk Helbig  *       2.5 ms - 48 KHz  |    54 to 210  |   172.8 - 672  |
93*6897da5cSDirk Helbig  *       2.5 ms - 96 KHz  |    62 to 210  |   198.4 - 672  |
94*6897da5cSDirk Helbig  *
95*6897da5cSDirk Helbig  *
969a19cd78SMatthias Ringwald  * --- About 44.1 KHz sample rate ---
979a19cd78SMatthias Ringwald  *
98*6897da5cSDirk Helbig  * The Bluetooth specification and the ETSI TS 103 634 standard references
99*6897da5cSDirk Helbig  * the 44.1 KHz sample rate, although there is no support in the core algorithm
100*6897da5cSDirk Helbig  * of the codec.
101*6897da5cSDirk Helbig  * We can summarize the 44.1 KHz support by "You can put any sample rate around
102*6897da5cSDirk Helbig  * the defined base sample rates." Please mind the following items :
1039a19cd78SMatthias Ringwald  *
104*6897da5cSDirk Helbig  *   1. The frame size will not be 2.5ms, 5ms, 7.5 ms or 10 ms, but is scaled
1059a19cd78SMatthias Ringwald  *      by 'supported sample rate' / 'input sample rate'
1069a19cd78SMatthias Ringwald  *
1079a19cd78SMatthias Ringwald  *   2. The bandwidth will be hard limited (to 20 KHz) if you select 48 KHz.
1089a19cd78SMatthias Ringwald  *      The encoded bandwidth will also be affected by the above inverse
1099a19cd78SMatthias Ringwald  *      factor of 20 KHz.
1109a19cd78SMatthias Ringwald  *
1119a19cd78SMatthias Ringwald  * Applied to 44.1 KHz, we get :
1129a19cd78SMatthias Ringwald  *
1139a19cd78SMatthias Ringwald  *   1. About  8.16 ms frame duration, instead of 7.5 ms
1149a19cd78SMatthias Ringwald  *      About 10.88 ms frame duration, instead of  10 ms
1159a19cd78SMatthias Ringwald  *
1169a19cd78SMatthias Ringwald  *   2. The bandwidth becomes limited to 18.375 KHz
1179a19cd78SMatthias Ringwald  *
1189a19cd78SMatthias Ringwald  *
1199a19cd78SMatthias Ringwald  * --- How to encode / decode ---
1209a19cd78SMatthias Ringwald  *
1214930cef6SMatthias Ringwald  * An encoder / decoder context needs to be setup. This context keeps states
1229a19cd78SMatthias Ringwald  * on the current stream to proceed, and samples that overlapped across
1239a19cd78SMatthias Ringwald  * frames.
1249a19cd78SMatthias Ringwald  *
1259a19cd78SMatthias Ringwald  * You have two ways to setup the encoder / decoder :
1269a19cd78SMatthias Ringwald  *
1279a19cd78SMatthias Ringwald  * - Using static memory allocation (this module does not rely on
1289a19cd78SMatthias Ringwald  *   any dynamic memory allocation). The types `lc3_xxcoder_mem_16k_t`,
1299a19cd78SMatthias Ringwald  *   and `lc3_xxcoder_mem_48k_t` have size of the memory needed for
1309a19cd78SMatthias Ringwald  *   encoding up to 16 KHz or 48 KHz.
1319a19cd78SMatthias Ringwald  *
1329a19cd78SMatthias Ringwald  * - Using dynamic memory allocation. The `lc3_xxcoder_size()` procedure
1339a19cd78SMatthias Ringwald  *   returns the needed memory size, for a given configuration. The memory
1349a19cd78SMatthias Ringwald  *   space must be aligned to a pointer size. As an example, you can setup
1359a19cd78SMatthias Ringwald  *   encoder like this :
1369a19cd78SMatthias Ringwald  *
1379a19cd78SMatthias Ringwald  *   | enc = lc3_setup_encoder(frame_us, sample rate,
1389a19cd78SMatthias Ringwald  *   |      malloc(lc3_encoder_size(frame_us, sample rate)));
1399a19cd78SMatthias Ringwald  *   | ...
1409a19cd78SMatthias Ringwald  *   | free(enc);
1419a19cd78SMatthias Ringwald  *
1429a19cd78SMatthias Ringwald  *   Note :
1439a19cd78SMatthias Ringwald  *   - A NULL memory adress as input, will return a NULL encoder context.
1449a19cd78SMatthias Ringwald  *   - The returned encoder handle is set at the address of the allocated
1459a19cd78SMatthias Ringwald  *     memory space, you can directly free the handle.
1469a19cd78SMatthias Ringwald  *
1479a19cd78SMatthias Ringwald  * Next, call the `lc3_encode()` encoding procedure, for each frames.
1489a19cd78SMatthias Ringwald  * To handle multichannel streams (Stereo or more), you can proceed with
1494930cef6SMatthias Ringwald  * interleaved channels PCM stream like this :
1509a19cd78SMatthias Ringwald  *
1519a19cd78SMatthias Ringwald  *   | for(int ich = 0; ich < nch: ich++)
1529a19cd78SMatthias Ringwald  *   |     lc3_encode(encoder[ich], pcm + ich, nch, ...);
1539a19cd78SMatthias Ringwald  *
1549a19cd78SMatthias Ringwald  *   with `nch` as the number of channels in the PCM stream
1554930cef6SMatthias Ringwald  *
1564930cef6SMatthias Ringwald  * ---
1574930cef6SMatthias Ringwald  *
1584930cef6SMatthias Ringwald  * Antoine SOULIER, Tempow / Google LLC
1594930cef6SMatthias Ringwald  *
1609a19cd78SMatthias Ringwald  */
1619a19cd78SMatthias Ringwald 
1629a19cd78SMatthias Ringwald #ifndef __LC3_H
1639a19cd78SMatthias Ringwald #define __LC3_H
1649a19cd78SMatthias Ringwald 
1659a19cd78SMatthias Ringwald #ifdef __cplusplus
1669a19cd78SMatthias Ringwald extern "C" {
1679a19cd78SMatthias Ringwald #endif
1689a19cd78SMatthias Ringwald 
1699a19cd78SMatthias Ringwald #include <stdint.h>
1709a19cd78SMatthias Ringwald #include <stdbool.h>
1719a19cd78SMatthias Ringwald 
1729a19cd78SMatthias Ringwald #include "lc3_private.h"
1739a19cd78SMatthias Ringwald 
1749a19cd78SMatthias Ringwald 
1759a19cd78SMatthias Ringwald /**
1769a19cd78SMatthias Ringwald  * Limitations
177*6897da5cSDirk Helbig  * - On the bitrate, in bps
1789a19cd78SMatthias Ringwald  * - On the size of the frames in bytes
179*6897da5cSDirk Helbig  * - On the number of samples by frames
1809a19cd78SMatthias Ringwald  */
1819a19cd78SMatthias Ringwald 
1829a19cd78SMatthias Ringwald #define LC3_MIN_BITRATE         16000
1839a19cd78SMatthias Ringwald #define LC3_MAX_BITRATE        320000
184*6897da5cSDirk Helbig #define LC3_HR_MAX_BITRATE     672000
1859a19cd78SMatthias Ringwald 
1869a19cd78SMatthias Ringwald #define LC3_MIN_FRAME_BYTES        20
1879a19cd78SMatthias Ringwald #define LC3_MAX_FRAME_BYTES       400
188*6897da5cSDirk Helbig #define LC3_HR_MAX_FRAME_BYTES    625
189*6897da5cSDirk Helbig 
190*6897da5cSDirk Helbig #define LC3_MIN_FRAME_SAMPLES     LC3_NS( 2500,  8000)
191*6897da5cSDirk Helbig #define LC3_MAX_FRAME_SAMPLES     LC3_NS(10000, 48000)
192*6897da5cSDirk Helbig #define LC3_HR_MAX_FRAME_SAMPLES  LC3_NS(10000, 96000)
1939a19cd78SMatthias Ringwald 
1949a19cd78SMatthias Ringwald 
1959a19cd78SMatthias Ringwald /**
1969a19cd78SMatthias Ringwald  * Parameters check
1979a19cd78SMatthias Ringwald  *   LC3_CHECK_DT_US(us)  True when frame duration in us is suitable
1989a19cd78SMatthias Ringwald  *   LC3_CHECK_SR_HZ(sr)  True when sample rate in Hz is suitable
199*6897da5cSDirk Helbig  *
200*6897da5cSDirk Helbig  *   LC3_HR_CHECK_SR_HZ(hrmode, sr)
201*6897da5cSDirk Helbig  *     True when sample rate in Hz is suitable, according to the
202*6897da5cSDirk Helbig  *     selection of the high-resolution mode `hrmode`.
2039a19cd78SMatthias Ringwald  */
2049a19cd78SMatthias Ringwald 
2059a19cd78SMatthias Ringwald #define LC3_CHECK_DT_US(us) \
206*6897da5cSDirk Helbig     ( ((us) == 2500) || ((us) ==  5000) || \
207*6897da5cSDirk Helbig       ((us) == 7500) || ((us) == 10000)    )
2089a19cd78SMatthias Ringwald 
2099a19cd78SMatthias Ringwald #define LC3_CHECK_SR_HZ(sr) \
2109a19cd78SMatthias Ringwald     ( ((sr) ==  8000) || ((sr) == 16000) || ((sr) == 24000) || \
2119a19cd78SMatthias Ringwald       ((sr) == 32000) || ((sr) == 48000)                       )
2129a19cd78SMatthias Ringwald 
213*6897da5cSDirk Helbig #define LC3_HR_CHECK_SR_HZ(hrmode, sr) \
214*6897da5cSDirk Helbig     ( (hrmode) ? ((sr) == 48000) || ((sr) == 96000) : LC3_CHECK_SR_HZ(sr) )
215*6897da5cSDirk Helbig 
2169a19cd78SMatthias Ringwald 
2179a19cd78SMatthias Ringwald /**
2189a19cd78SMatthias Ringwald  * PCM Sample Format
2199a19cd78SMatthias Ringwald  *   S16      Signed 16 bits, in 16 bits words (int16_t)
2209a19cd78SMatthias Ringwald  *   S24      Signed 24 bits, using low three bytes of 32 bits words (int32_t).
2214c4eb519SMatthias Ringwald  *            The high byte sign extends (bits 31..24 set to b23).
2224c4eb519SMatthias Ringwald  *   S24_3LE  Signed 24 bits packed in 3 bytes little endian
2234c4eb519SMatthias Ringwald  *   FLOAT    Floating point 32 bits (float type), in range -1 to 1
2249a19cd78SMatthias Ringwald  */
2259a19cd78SMatthias Ringwald 
2269a19cd78SMatthias Ringwald enum lc3_pcm_format {
2279a19cd78SMatthias Ringwald     LC3_PCM_FORMAT_S16,
2289a19cd78SMatthias Ringwald     LC3_PCM_FORMAT_S24,
2294c4eb519SMatthias Ringwald     LC3_PCM_FORMAT_S24_3LE,
2304c4eb519SMatthias Ringwald     LC3_PCM_FORMAT_FLOAT,
2319a19cd78SMatthias Ringwald };
2329a19cd78SMatthias Ringwald 
2339a19cd78SMatthias Ringwald 
2349a19cd78SMatthias Ringwald /**
2359a19cd78SMatthias Ringwald  * Handle
2369a19cd78SMatthias Ringwald  */
2379a19cd78SMatthias Ringwald 
2389a19cd78SMatthias Ringwald typedef struct lc3_encoder *lc3_encoder_t;
2399a19cd78SMatthias Ringwald typedef struct lc3_decoder *lc3_decoder_t;
2409a19cd78SMatthias Ringwald 
2419a19cd78SMatthias Ringwald 
2429a19cd78SMatthias Ringwald /**
243*6897da5cSDirk Helbig  * Static memory of encoder/decoder contexts
2449a19cd78SMatthias Ringwald  *
2459a19cd78SMatthias Ringwald  * Propose types suitable for static memory allocation, supporting
2469a19cd78SMatthias Ringwald  * any frame duration, and maximum sample rates 16k and 48k respectively
2479a19cd78SMatthias Ringwald  * You can customize your type using the `LC3_ENCODER_MEM_T` or
2489a19cd78SMatthias Ringwald  * `LC3_DECODER_MEM_T` macro.
2499a19cd78SMatthias Ringwald  */
2509a19cd78SMatthias Ringwald 
2519a19cd78SMatthias Ringwald typedef LC3_ENCODER_MEM_T(10000, 16000) lc3_encoder_mem_16k_t;
2529a19cd78SMatthias Ringwald typedef LC3_ENCODER_MEM_T(10000, 48000) lc3_encoder_mem_48k_t;
2539a19cd78SMatthias Ringwald 
2549a19cd78SMatthias Ringwald typedef LC3_DECODER_MEM_T(10000, 16000) lc3_decoder_mem_16k_t;
2559a19cd78SMatthias Ringwald typedef LC3_DECODER_MEM_T(10000, 48000) lc3_decoder_mem_48k_t;
2569a19cd78SMatthias Ringwald 
2579a19cd78SMatthias Ringwald 
2589a19cd78SMatthias Ringwald /**
2599a19cd78SMatthias Ringwald  * Return the number of PCM samples in a frame
260*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
261*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
262*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
2639a19cd78SMatthias Ringwald  * return          Number of PCM samples, -1 on bad parameters
2649a19cd78SMatthias Ringwald  */
265*6897da5cSDirk Helbig LC3_EXPORT int lc3_hr_frame_samples(bool hrmode, int dt_us, int sr_hz);
266*6897da5cSDirk Helbig 
267*6897da5cSDirk Helbig LC3_EXPORT int lc3_frame_samples(int dt_us, int sr_hz);
2689a19cd78SMatthias Ringwald 
2699a19cd78SMatthias Ringwald /**
2709a19cd78SMatthias Ringwald  * Return the size of frames, from bitrate
271*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
272*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
273*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
274*6897da5cSDirk Helbig  * bitrate         Target bitrate in bit per second, 0 or `INT_MAX` returns
275*6897da5cSDirk Helbig  *                 respectively the minimum and maximum allowed size.
2769a19cd78SMatthias Ringwald  * return          The floor size in bytes of the frames, -1 on bad parameters
2779a19cd78SMatthias Ringwald  */
278*6897da5cSDirk Helbig LC3_EXPORT int lc3_hr_frame_bytes(
279*6897da5cSDirk Helbig     bool hrmode, int dt_us, int sr_hz, int bitrate);
280*6897da5cSDirk Helbig 
281*6897da5cSDirk Helbig LC3_EXPORT int lc3_frame_bytes(int dt_us, int bitrate);
282*6897da5cSDirk Helbig 
283*6897da5cSDirk Helbig /**
284*6897da5cSDirk Helbig  * Return the size of frame blocks, from bitrate
285*6897da5cSDirk Helbig  * A frame block contains the frame data from all channels.
286*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
287*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
288*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
289*6897da5cSDirk Helbig  * nchannels       The number of channels (or frames) in the block (<= 8)
290*6897da5cSDirk Helbig  * bitrate         Target bitrate in bit per second, 0 or `INT_MAX` returns
291*6897da5cSDirk Helbig  *                 respectively the minimum and maximum allowed size.
292*6897da5cSDirk Helbig  * return          The floor size in bytes of the frames, -1 on bad parameters
293*6897da5cSDirk Helbig  */
294*6897da5cSDirk Helbig LC3_EXPORT int lc3_hr_frame_block_bytes(
295*6897da5cSDirk Helbig     bool hrmode, int dt_us, int sr_hz, int nchannels, int bitrate);
296*6897da5cSDirk Helbig 
297*6897da5cSDirk Helbig LC3_EXPORT int lc3_frame_block_bytes(int dt_us, int nframes, int bitrate);
2989a19cd78SMatthias Ringwald 
2999a19cd78SMatthias Ringwald /**
3009a19cd78SMatthias Ringwald  * Resolve the bitrate, from the size of frames
301*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
302*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
303*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
304*6897da5cSDirk Helbig  * nbytes          Size in bytes of the frames or frame blocks
305*6897da5cSDirk Helbig  * return          The ceiled bitrate in bps, -1 on bad parameters
3069a19cd78SMatthias Ringwald  */
307*6897da5cSDirk Helbig LC3_EXPORT int lc3_hr_resolve_bitrate(
308*6897da5cSDirk Helbig     bool hrmode, int dt_us, int sr_hz, int nbytes);
309*6897da5cSDirk Helbig 
310*6897da5cSDirk Helbig LC3_EXPORT int lc3_resolve_bitrate(int dt_us, int nbytes);
3119a19cd78SMatthias Ringwald 
3129a19cd78SMatthias Ringwald /**
3139a19cd78SMatthias Ringwald  * Return algorithmic delay, as a number of samples
314*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
315*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
316*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
3179a19cd78SMatthias Ringwald  * return          Number of algorithmic delay samples, -1 on bad parameters
3189a19cd78SMatthias Ringwald  */
319*6897da5cSDirk Helbig LC3_EXPORT int lc3_hr_delay_samples(bool hrmode, int dt_us, int sr_hz);
320*6897da5cSDirk Helbig 
321*6897da5cSDirk Helbig LC3_EXPORT int lc3_delay_samples(int dt_us, int sr_hz);
3229a19cd78SMatthias Ringwald 
3239a19cd78SMatthias Ringwald /**
3249a19cd78SMatthias Ringwald  * Return size needed for an encoder
325*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
326*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
327*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
3289a19cd78SMatthias Ringwald  * return          Size of then encoder in bytes, 0 on bad parameters
3299a19cd78SMatthias Ringwald  *
3309a19cd78SMatthias Ringwald  * The `sr_hz` parameter is the sample rate of the PCM input stream,
331*6897da5cSDirk Helbig  * and will match `sr_pcm_hz` of `lc3_hr_setup_encoder()`.
3329a19cd78SMatthias Ringwald  */
333*6897da5cSDirk Helbig LC3_EXPORT unsigned lc3_hr_encoder_size(bool hrmode, int dt_us, int sr_hz);
334*6897da5cSDirk Helbig 
335*6897da5cSDirk Helbig LC3_EXPORT unsigned lc3_encoder_size(int dt_us, int sr_hz);
3369a19cd78SMatthias Ringwald 
3379a19cd78SMatthias Ringwald /**
3389a19cd78SMatthias Ringwald  * Setup encoder
339*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
340*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
341*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
3429a19cd78SMatthias Ringwald  * sr_pcm_hz       Input sample rate, downsampling option of input, or 0
3439a19cd78SMatthias Ringwald  * mem             Encoder memory space, aligned to pointer type
3449a19cd78SMatthias Ringwald  * return          Encoder as an handle, NULL on bad parameters
3459a19cd78SMatthias Ringwald  *
3469a19cd78SMatthias Ringwald  * The `sr_pcm_hz` parameter is a downsampling option of PCM input,
3479a19cd78SMatthias Ringwald  * the value `0` fallback to the sample rate of the encoded stream `sr_hz`.
3489a19cd78SMatthias Ringwald  * When used, `sr_pcm_hz` is intended to be higher or equal to the encoder
3499a19cd78SMatthias Ringwald  * sample rate `sr_hz`. The size of the context needed, given by
350*6897da5cSDirk Helbig  * `lc3_hr_encoder_size()` will be set accordingly to `sr_pcm_hz`.
3519a19cd78SMatthias Ringwald  */
352*6897da5cSDirk Helbig LC3_EXPORT lc3_encoder_t lc3_hr_setup_encoder(
353*6897da5cSDirk Helbig     bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
354*6897da5cSDirk Helbig 
355*6897da5cSDirk Helbig LC3_EXPORT lc3_encoder_t lc3_setup_encoder(
3569a19cd78SMatthias Ringwald     int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
3579a19cd78SMatthias Ringwald 
3589a19cd78SMatthias Ringwald /**
3599a19cd78SMatthias Ringwald  * Encode a frame
3609a19cd78SMatthias Ringwald  * encoder         Handle of the encoder
3619a19cd78SMatthias Ringwald  * fmt             PCM input format
3629a19cd78SMatthias Ringwald  * pcm, stride     Input PCM samples, and count between two consecutives
363*6897da5cSDirk Helbig  * nbytes          Target size, in bytes, of the frame
3649a19cd78SMatthias Ringwald  * out             Output buffer of `nbytes` size
3659a19cd78SMatthias Ringwald  * return          0: On success  -1: Wrong parameters
3669a19cd78SMatthias Ringwald  */
367*6897da5cSDirk Helbig LC3_EXPORT int lc3_encode(
368*6897da5cSDirk Helbig     lc3_encoder_t encoder, enum lc3_pcm_format fmt,
3699a19cd78SMatthias Ringwald     const void *pcm, int stride, int nbytes, void *out);
3709a19cd78SMatthias Ringwald 
3719a19cd78SMatthias Ringwald /**
3729a19cd78SMatthias Ringwald  * Return size needed for an decoder
373*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
374*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
375*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
3769a19cd78SMatthias Ringwald  * return          Size of then decoder in bytes, 0 on bad parameters
3779a19cd78SMatthias Ringwald  *
3789a19cd78SMatthias Ringwald  * The `sr_hz` parameter is the sample rate of the PCM output stream,
379*6897da5cSDirk Helbig  * and will match `sr_pcm_hz` of `lc3_hr_setup_decoder()`.
3809a19cd78SMatthias Ringwald  */
381*6897da5cSDirk Helbig LC3_EXPORT unsigned lc3_hr_decoder_size(bool hrmode, int dt_us, int sr_hz);
382*6897da5cSDirk Helbig 
383*6897da5cSDirk Helbig LC3_EXPORT unsigned lc3_decoder_size(int dt_us, int sr_hz);
3849a19cd78SMatthias Ringwald 
3859a19cd78SMatthias Ringwald /**
3869a19cd78SMatthias Ringwald  * Setup decoder
387*6897da5cSDirk Helbig  * hrmode          Enable High-Resolution mode (48000 and 96000 sample rates)
388*6897da5cSDirk Helbig  * dt_us           Frame duration in us, 2500, 5000, 7500 or 10000
389*6897da5cSDirk Helbig  * sr_hz           Sample rate in Hz, 8000, 16000, 24000, 32000, 48000 or 96000
3909a19cd78SMatthias Ringwald  * sr_pcm_hz       Output sample rate, upsampling option of output (or 0)
3919a19cd78SMatthias Ringwald  * mem             Decoder memory space, aligned to pointer type
3929a19cd78SMatthias Ringwald  * return          Decoder as an handle, NULL on bad parameters
3939a19cd78SMatthias Ringwald  *
3949a19cd78SMatthias Ringwald  * The `sr_pcm_hz` parameter is an upsampling option of PCM output,
3959a19cd78SMatthias Ringwald  * the value `0` fallback to the sample rate of the decoded stream `sr_hz`.
3969a19cd78SMatthias Ringwald  * When used, `sr_pcm_hz` is intended to be higher or equal to the decoder
3979a19cd78SMatthias Ringwald  * sample rate `sr_hz`. The size of the context needed, given by
398*6897da5cSDirk Helbig  * `lc3_hr_decoder_size()` will be set accordingly to `sr_pcm_hz`.
3999a19cd78SMatthias Ringwald  */
400*6897da5cSDirk Helbig LC3_EXPORT lc3_decoder_t lc3_hr_setup_decoder(
401*6897da5cSDirk Helbig     bool hrmode, int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
402*6897da5cSDirk Helbig 
403*6897da5cSDirk Helbig LC3_EXPORT lc3_decoder_t lc3_setup_decoder(
4049a19cd78SMatthias Ringwald     int dt_us, int sr_hz, int sr_pcm_hz, void *mem);
4059a19cd78SMatthias Ringwald 
4069a19cd78SMatthias Ringwald /**
4079a19cd78SMatthias Ringwald  * Decode a frame
4089a19cd78SMatthias Ringwald  * decoder         Handle of the decoder
4099a19cd78SMatthias Ringwald  * in, nbytes      Input bitstream, and size in bytes, NULL performs PLC
4109a19cd78SMatthias Ringwald  * fmt             PCM output format
4119a19cd78SMatthias Ringwald  * pcm, stride     Output PCM samples, and count between two consecutives
4129a19cd78SMatthias Ringwald  * return          0: On success  1: PLC operated  -1: Wrong parameters
4139a19cd78SMatthias Ringwald  */
414*6897da5cSDirk Helbig LC3_EXPORT int lc3_decode(
415*6897da5cSDirk Helbig     lc3_decoder_t decoder, const void *in, int nbytes,
4169a19cd78SMatthias Ringwald     enum lc3_pcm_format fmt, void *pcm, int stride);
4179a19cd78SMatthias Ringwald 
4189a19cd78SMatthias Ringwald 
4199a19cd78SMatthias Ringwald #ifdef __cplusplus
4209a19cd78SMatthias Ringwald }
4219a19cd78SMatthias Ringwald #endif
4229a19cd78SMatthias Ringwald 
4239a19cd78SMatthias Ringwald #endif /* __LC3_H */
424