Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * include/avx_audio_codec.h - Opus and IMA ADPCM audio codecs
7 */
8  
9 #ifndef AVX_AUDIO_CODEC_H
10 #define AVX_AUDIO_CODEC_H
11  
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include "openavx.h"
16  
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20  
21 /* Supported Audio Compression Codec Types */
22 typedef enum avx_audio_codec_type_e {
23 AVX_AUDIO_CODEC_NONE = 0,
24 AVX_AUDIO_CODEC_OPUS = 1,
25 AVX_AUDIO_CODEC_ADPCM = 2
26 } avx_audio_codec_type_e;
27  
28 /* Check whether the dynamic runtime libopus loader successfully bound symbols */
29 OPENAVX_API bool avx_audio_codec_has_opus(void);
30  
31 /* Opaque Audio Encoder & Decoder Handles */
32 typedef struct avx_audio_encoder_t avx_audio_encoder_t;
33 typedef struct avx_audio_decoder_t avx_audio_decoder_t;
34  
35 /**
36 * Creates an audio encoder instance.
37 * @param sample_rate Sample rate in Hz (e.g. 48000).
38 * @param channels Number of channels (1 to 32: Mono, Stereo, 5.1, 7.1, 16ch Broadcast, 32ch Pro-Audio).
39 * Opus is used for <= 8 channels; ADPCM is automatically used for > 8 channels.
40 * @param preferred_codec Desired codec (AVX_AUDIO_CODEC_OPUS or AVX_AUDIO_CODEC_ADPCM).
41 * If OPUS is requested but libopus is absent or channels > 8, auto-falls back to ADPCM.
42 * @param bitrate_kbps Target bitrate in kbps (0 for automatic: 128 stereo, 384 7.1).
43 */
44 OPENAVX_API avx_audio_encoder_t* avx_audio_encoder_create(
45 int sample_rate,
46 int channels,
47 avx_audio_codec_type_e preferred_codec,
48 int bitrate_kbps
49 );
50  
51 /**
52 * Destroys an audio encoder instance and releases its internal buffers.
53 */
54 OPENAVX_API void avx_audio_encoder_destroy(avx_audio_encoder_t* enc);
55  
56 /**
57 * Returns the active codec type currently being used by the encoder.
58 */
59 OPENAVX_API avx_audio_codec_type_e avx_audio_encoder_get_codec(const avx_audio_encoder_t* enc);
60  
61 /**
62 * Returns the FourCC of the active codec ('OPUS' or 'ADPC').
63 */
64 OPENAVX_API uint32_t avx_audio_encoder_get_fourcc(const avx_audio_encoder_t* enc);
65  
66 /**
67 * Feeds uncompressed planar float audio frames into the encoder.
68 * Audio is accumulated and chunked into optimal packet boundaries (20 ms for Opus).
69 */
70 OPENAVX_API bool avx_audio_encoder_feed(
71 avx_audio_encoder_t* enc,
72 const float* const* planes,
73 int frames,
74 int64_t pts_ns
75 );
76  
77 /**
78 * Pulls the next available compressed audio packet from the encoder.
79 * Returns true if a packet was retrieved, false if more input frames are needed.
80 */
81 OPENAVX_API bool avx_audio_encoder_pull(
82 avx_audio_encoder_t* enc,
83 uint8_t* out_pkt,
84 size_t max_pkt_size,
85 size_t* out_pkt_size,
86 int* out_samples,
87 int64_t* out_pts_ns,
88 uint32_t* out_codec_fourcc
89 );
90  
91 /**
92 * Flushes any remaining buffered samples in the encoder.
93 */
94 OPENAVX_API bool avx_audio_encoder_flush(
95 avx_audio_encoder_t* enc,
96 uint8_t* out_pkt,
97 size_t max_pkt_size,
98 size_t* out_pkt_size,
99 int* out_samples,
100 int64_t* out_pts_ns,
101 uint32_t* out_codec_fourcc
102 );
103  
104 /**
105 * Creates an audio decoder instance.
106 * @param sample_rate Target sample rate (e.g. 48000).
107 * @param channels Number of channels (1 to 32).
108 * @param codec_fourcc OAVX_FOURCC_AUDIO_TYPE_OPUS or OAVX_FOURCC_AUDIO_TYPE_ADPC.
109 */
110 OPENAVX_API avx_audio_decoder_t* avx_audio_decoder_create(
111 int sample_rate,
112 int channels,
113 uint32_t codec_fourcc
114 );
115  
116 /**
117 * Destroys an audio decoder instance and releases resources.
118 */
119 OPENAVX_API void avx_audio_decoder_destroy(avx_audio_decoder_t* dec);
120  
121 /**
122 * Decodes a compressed packet into planar float buffers.
123 * @return Number of decoded sample frames per channel, or negative on error.
124 */
125 OPENAVX_API int avx_audio_decoder_decode(
126 avx_audio_decoder_t* dec,
127 uint32_t codec_fourcc,
128 const uint8_t* pkt_data,
129 size_t pkt_size,
130 float* const* out_planes,
131 int max_samples
132 );
133  
134 /* ========================================================================= */
135 /* STANDALONE PURE C99 IMA ADPCM ENCODE / DECODE (ZERO DEPENDENCIES) */
136 /* ========================================================================= */
137  
138 /**
139 * Encodes planar float audio to 4-bit IMA ADPCM bitstream.
140 * Achieves 4:1 compression ratio over 16-bit PCM (8:1 over 32-bit float).
141 */
142 OPENAVX_API size_t avx_adpcm_encode_planar_float(
143 const float* const* in_planes,
144 int channels,
145 int samples,
146 uint8_t* out_buf,
147 size_t max_out_bytes
148 );
149  
150 /**
151 * Decodes 4-bit IMA ADPCM bitstream back into planar float audio.
152 */
153 OPENAVX_API int avx_adpcm_decode_to_planar_float(
154 const uint8_t* in_buf,
155 size_t in_bytes,
156 int channels,
157 float* const* out_planes,
158 int max_samples
159 );
160  
161 #ifdef __cplusplus
162 }
163 #endif
164  
165 #endif /* AVX_AUDIO_CODEC_H */