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 - High-Performance Audio/Video Network Library
6 * test/openavx_audio_codec_test.c - Unit & Verification Suite for Audio Compression (Opus & IMA ADPCM)
7 */
8  
9 #define _DEFAULT_SOURCE
10 #define _POSIX_C_SOURCE 200809L
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <string.h>
16 #include <math.h>
17 #include <unistd.h>
18 #include <assert.h>
19  
20 #include "openavx.h"
21 #include "avx_audio_codec.h"
22 #include "avx_audio.h"
23 #include "avx_platform.h"
24  
25 #ifndef M_PI
26 #define M_PI 3.14159265358979323846
27 #endif
28  
29 #define ASSERT_TRUE(cond) do { \
30 if (!(cond)) { \
31 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s' failed\n", __FILE__, __LINE__, #cond); \
32 exit(1); \
33 } \
34 } while(0)
35  
36 #define ASSERT_EQ(a, b) do { \
37 if ((a) != (b)) { \
38 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s == %s' failed (%ld != %ld)\n", \
39 __FILE__, __LINE__, #a, #b, (long)(a), (long)(b)); \
40 exit(1); \
41 } \
42 } while(0)
43  
44 #define ASSERT_NEAR(a, b, eps) do { \
45 float _diff = fabsf((float)(a) - (float)(b)); \
46 if (_diff > (eps)) { \
47 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s' =~ '%s' failed (diff=%f, eps=%f, a=%f, b=%f)\n", \
48 __FILE__, __LINE__, #a, #b, _diff, (float)(eps), (float)(a), (float)(b)); \
49 exit(1); \
50 } \
51 } while(0)
52  
53 /* Helper: Generate planar sine wave */
54 static void generate_sine_wave(float *plane, int samples, int sample_rate, float freq, float amplitude) {
55 for (int i = 0; i < samples; ++i) {
56 plane[i] = amplitude * sinf((float)(2.0 * M_PI * freq * (double)i / (double)sample_rate));
57 }
58 }
59  
60 /* ========================================================================= */
61 /* TEST 1: PURE C99 IMA ADPCM ENCODE / DECODE ROUNDTRIP */
62 /* ========================================================================= */
63  
64 static void test_adpcm_roundtrip(void) {
65 printf("[*] 1. Testing Pure C99 IMA ADPCM Multi-Channel Roundtrip...\n");
66  
67 const int channel_configs[] = { 1, 2, 4, 6, 8, 16, 32 };
68 const int num_configs = sizeof(channel_configs) / sizeof(channel_configs[0]);
69 const int samples = 960;
70 const int sample_rate = 48000;
71  
72 for (int c_idx = 0; c_idx < num_configs; ++c_idx) {
73 int channels = channel_configs[c_idx];
74 printf(" -> Testing ADPCM with %d channel(s)...\n", channels);
75  
76 float *in_planes[32];
77 float *out_planes[32];
78 for (int c = 0; c < channels; ++c) {
79 in_planes[c] = (float*)malloc(samples * sizeof(float));
80 out_planes[c] = (float*)malloc(samples * sizeof(float));
81 float freq = 220.0f * (float)(c + 1);
82 generate_sine_wave(in_planes[c], samples, sample_rate, freq, 0.8f);
83 memset(out_planes[c], 0, samples * sizeof(float));
84 }
85  
86 /* Encode planar float to ADPCM */
87 uint8_t adpcm_buf[65536];
88 size_t encoded_bytes = avx_adpcm_encode_planar_float(
89 (const float* const*)in_planes, channels, samples, adpcm_buf, sizeof(adpcm_buf));
90  
91 ASSERT_TRUE(encoded_bytes > 0);
92 /* Expected size: 8-byte global header + per-channel (4-byte header + (samples - 1)/2 nibbles) */
93 size_t expected_size = 8 + (size_t)channels * (4 + (samples > 1 ? (size_t)(samples - 1 + 1) / 2 : 0));
94 ASSERT_EQ(encoded_bytes, expected_size);
95  
96 /* Decode ADPCM back to planar float */
97 int decoded_samples = avx_adpcm_decode_to_planar_float(
98 adpcm_buf, encoded_bytes, channels, out_planes, samples);
99  
100 ASSERT_EQ(decoded_samples, samples);
101  
102 /* Verify accuracy: IMA ADPCM is 4-bit lossy, check max sample difference < 0.12 */
103 for (int c = 0; c < channels; ++c) {
104 float max_diff = 0.0f;
105 float energy_in = 0.0f;
106 float energy_out = 0.0f;
107 for (int s = 0; s < samples; ++s) {
108 float diff = fabsf(in_planes[c][s] - out_planes[c][s]);
109 if (diff > max_diff) max_diff = diff;
110 energy_in += in_planes[c][s] * in_planes[c][s];
111 energy_out += out_planes[c][s] * out_planes[c][s];
112 }
113 ASSERT_TRUE(max_diff < 0.12f);
114 ASSERT_TRUE(energy_out > 0.5f * energy_in);
115 free(in_planes[c]);
116 free(out_planes[c]);
117 }
118 printf(" [+] %d channel(s) passed successfully (compression ratio: 8:1 over float32)\n", channels);
119 }
120 }
121  
122 /* ========================================================================= */
123 /* TEST 2: OPUS MULTISTREAM DYNAMIC ENCODER / DECODER MATRIX */
124 /* ========================================================================= */
125  
126 static void test_opus_multistream(void) {
127 printf("[*] 2. Testing Opus Multi-Stream Dynamic Codec (or ADPCM Fallback)...\n");
128  
129 bool has_opus = avx_audio_codec_has_opus();
130 printf(" -> Host libopus runtime status: %s\n", has_opus ? "LOADED DYNAMICALLY (dlopen)" : "ABSENT (will test ADPCM fallback)");
131  
132 const int channel_configs[] = { 1, 2, 4, 6, 8 };
133 const int num_configs = sizeof(channel_configs) / sizeof(channel_configs[0]);
134 const int samples = 960;
135 const int sample_rate = 48000;
136  
137 for (int c_idx = 0; c_idx < num_configs; ++c_idx) {
138 int channels = channel_configs[c_idx];
139 printf(" -> Testing encoder/decoder with %d channel(s)...\n", channels);
140  
141 avx_audio_encoder_t *enc = avx_audio_encoder_create(
142 sample_rate, channels, AVX_AUDIO_CODEC_OPUS, 0);
143 ASSERT_TRUE(enc != NULL);
144  
145 if (has_opus) {
146 ASSERT_EQ(avx_audio_encoder_get_codec(enc), AVX_AUDIO_CODEC_OPUS);
147 ASSERT_EQ(avx_audio_encoder_get_fourcc(enc), OAVX_FOURCC_AUDIO_TYPE_OPUS);
148 } else {
149 ASSERT_EQ(avx_audio_encoder_get_codec(enc), AVX_AUDIO_CODEC_ADPCM);
150 ASSERT_EQ(avx_audio_encoder_get_fourcc(enc), OAVX_FOURCC_AUDIO_TYPE_ADPC);
151 }
152  
153 uint32_t active_fourcc = avx_audio_encoder_get_fourcc(enc);
154 avx_audio_decoder_t *dec = avx_audio_decoder_create(sample_rate, channels, active_fourcc);
155 ASSERT_TRUE(dec != NULL);
156  
157 float *in_planes[8];
158 float *out_planes[8];
159 for (int c = 0; c < channels; ++c) {
160 in_planes[c] = (float*)malloc(samples * sizeof(float));
161 out_planes[c] = (float*)malloc(samples * sizeof(float));
162 float freq = 300.0f + (float)c * 150.0f;
163 generate_sine_wave(in_planes[c], samples, sample_rate, freq, 0.7f);
164 memset(out_planes[c], 0, samples * sizeof(float));
165 }
166  
167 /* Feed 960 samples into encoder */
168 bool fed = avx_audio_encoder_feed(enc, (const float* const*)in_planes, samples, 1000000000LL);
169 ASSERT_TRUE(fed);
170  
171 uint8_t pkt[65536];
172 size_t pkt_size = 0;
173 int out_samples = 0;
174 int64_t out_pts = 0;
175 uint32_t out_fourcc = 0;
176  
177 bool pulled = avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
178 ASSERT_TRUE(pulled);
179 ASSERT_TRUE(pkt_size > 0);
180 ASSERT_EQ(out_samples, samples);
181 ASSERT_EQ(out_fourcc, active_fourcc);
182  
183 /* Decode packet back into planar buffers */
184 int decoded = avx_audio_decoder_decode(dec, out_fourcc, pkt, pkt_size, out_planes, samples);
185 ASSERT_EQ(decoded, samples);
186  
187 /* Verify output has energy and no nan/inf */
188 for (int c = 0; c < channels; ++c) {
189 float energy = 0.0f;
190 for (int s = 0; s < samples; ++s) {
191 ASSERT_TRUE(!isnan(out_planes[c][s]) && !isinf(out_planes[c][s]));
192 energy += out_planes[c][s] * out_planes[c][s];
193 }
194 ASSERT_TRUE(energy > 1.0f);
195 free(in_planes[c]);
196 free(out_planes[c]);
197 }
198  
199 avx_audio_decoder_destroy(dec);
200 avx_audio_encoder_destroy(enc);
201 printf(" [+] %d channel(s) encoder/decoder roundtrip verified\n", channels);
202 }
203 }
204  
205 /* ========================================================================= */
206 /* TEST 3: IRREGULAR CHUNK ACCUMULATOR & FLUSH ENGINE */
207 /* ========================================================================= */
208  
209 static void test_accumulator_and_flush(void) {
210 printf("[*] 3. Testing 20 ms Chunk Accumulator & Flush Engine (60 fps 800-sample feeds)...\n");
211  
212 const int channels = 2;
213 const int sample_rate = 48000;
214 const int feed_frames = 800; /* Typical 48 kHz audio per frame at 60 fps */
215  
216 avx_audio_encoder_t *enc = avx_audio_encoder_create(
217 sample_rate, channels, AVX_AUDIO_CODEC_OPUS, 128);
218 ASSERT_TRUE(enc != NULL);
219  
220 float *in_planes[2];
221 in_planes[0] = (float*)malloc(feed_frames * sizeof(float));
222 in_planes[1] = (float*)malloc(feed_frames * sizeof(float));
223 generate_sine_wave(in_planes[0], feed_frames, sample_rate, 440.0f, 0.8f);
224 generate_sine_wave(in_planes[1], feed_frames, sample_rate, 880.0f, 0.8f);
225  
226 uint8_t pkt[65536];
227 size_t pkt_size = 0;
228 int out_samples = 0;
229 int64_t out_pts = 0;
230 uint32_t out_fourcc = 0;
231  
232 bool is_opus = (avx_audio_encoder_get_codec(enc) == AVX_AUDIO_CODEC_OPUS);
233  
234 if (is_opus) {
235 /* Feed 1: 800 samples in accumulator (Opus needs 960). Pull should return false! */
236 bool fed1 = avx_audio_encoder_feed(enc, (const float* const*)in_planes, feed_frames, 1000LL);
237 ASSERT_TRUE(fed1);
238 bool pulled1 = avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
239 ASSERT_TRUE(!pulled1);
240 printf(" [+] Feed 1 (800 samples): Correctly buffered, pull deferred\n");
241  
242 /* Feed 2: 800 samples (total 1600 samples). Pull should return 960 samples! Remaining 640. */
243 bool fed2 = avx_audio_encoder_feed(enc, (const float* const*)in_planes, feed_frames, 2000LL);
244 ASSERT_TRUE(fed2);
245 bool pulled2 = avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
246 ASSERT_TRUE(pulled2);
247 ASSERT_EQ(out_samples, 960);
248 printf(" [+] Feed 2 (800 samples, total 1600): Pulled 960 samples successfully\n");
249  
250 /* Second pull without feeding should return false because only 640 remain (< 960) */
251 bool pulled2_sub = avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
252 ASSERT_TRUE(!pulled2_sub);
253  
254 /* Flush: Should encode the remaining samples (zero-padded to 960 for Opus RFC 6716 boundary) */
255 bool flushed = avx_audio_encoder_flush(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
256 ASSERT_TRUE(flushed);
257 ASSERT_EQ(out_samples, 960);
258 printf(" [+] Flush: Flushed remaining samples cleanly (out_samples=%d)\n", out_samples);
259 } else {
260 /* ADPCM fallback encodes arbitrary accumulated sample counts immediately without 960-sample framing */
261 bool fed1 = avx_audio_encoder_feed(enc, (const float* const*)in_planes, feed_frames, 1000LL);
262 ASSERT_TRUE(fed1);
263 bool pulled1 = avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
264 ASSERT_TRUE(pulled1);
265 ASSERT_EQ(out_samples, feed_frames);
266 ASSERT_EQ(out_pts, 1000LL);
267 ASSERT_TRUE(!avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc));
268 printf(" [+] ADPCM Feed 1 (800 samples): Pulled immediately cleanly\n");
269  
270 bool fed2 = avx_audio_encoder_feed(enc, (const float* const*)in_planes, 640, 2000LL);
271 ASSERT_TRUE(fed2);
272 bool flushed = avx_audio_encoder_flush(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc);
273 ASSERT_TRUE(flushed);
274 ASSERT_EQ(out_samples, 640);
275 ASSERT_EQ(out_pts, 2000LL);
276 printf(" [+] ADPCM Flush (640 samples): Flushed cleanly (out_samples=%d)\n", out_samples);
277 }
278  
279 /* Subsequent pull and flush should return false (accumulator empty) */
280 ASSERT_TRUE(!avx_audio_encoder_pull(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc));
281 ASSERT_TRUE(!avx_audio_encoder_flush(enc, pkt, sizeof(pkt), &pkt_size, &out_samples, &out_pts, &out_fourcc));
282  
283 free(in_planes[0]);
284 free(in_planes[1]);
285 avx_audio_encoder_destroy(enc);
286 printf(" [+] Accumulator and flush verified\n");
287 }
288  
289 /* ========================================================================= */
290 /* TEST 4: END-TO-END STREAM TRANSMIT & RECEIVE WITH AUDIO COMPRESSION */
291 /* ========================================================================= */
292  
293 static void test_stream_compressed_loopback(void) {
294 printf("[*] 4. Testing End-to-End Stream Pipeline with Compressed Audio ('AVXO')...\n");
295  
296 oavx_context_t *ctx = oavx_context_create();
297 ASSERT_TRUE(ctx != NULL);
298  
299 oavx_stream_config_t tx_cfg = {
300 .name = "OAVX_COMP_AUDIO_STREAM",
301 .direction = OAVX_DIR_TRANSMIT,
302 .clock_sync = false,
303 .groups = "Public"
304 };
305 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
306 ASSERT_TRUE(tx != NULL);
307  
308 /* Enable Manual Audio Compression with 192 kbps target */
309 oavx_status_t st = oavx_stream_set_audio_compression(tx, OAVX_AUDIO_COMPRESSION_MANUAL, 192);
310 ASSERT_EQ(st, OAVX_SUCCESS);
311  
312 oavx_stream_config_t rx_cfg = {
313 .name = "OAVX_COMP_AUDIO_STREAM",
314 .endpoint = "127.0.0.1:7960",
315 .direction = OAVX_DIR_RECEIVE,
316 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
317 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
318 .groups = "Public"
319 };
320 oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg);
321 ASSERT_TRUE(rx != NULL);
322  
323 /* Allow TCP connection handshake to complete */
324 usleep(150000);
325  
326 /* Transmit 5.1 Surround audio frames (6 channels, 48 kHz, 960 frames = 20 ms) */
327 const int channels = 6;
328 const int frames = 960;
329 const int sample_rate = 48000;
330  
331 float *tx_planes[6];
332 for (int c = 0; c < channels; ++c) {
333 tx_planes[c] = (float*)malloc(frames * sizeof(float));
334 generate_sine_wave(tx_planes[c], frames, sample_rate, 440.0f + (float)c * 100.0f, 0.7f);
335 }
336  
337 oavx_audio_frame_t a_send;
338 memset(&a_send, 0, sizeof(a_send));
339 a_send.sample_rate = sample_rate;
340 a_send.channels = channels;
341 a_send.frames = frames;
342 a_send.format = OAVX_AUDIO_FMT_FLT_PLANAR;
343 a_send.stride_bytes = frames * sizeof(float);
344 a_send.pts_ns = 1234567890LL;
345 for (int c = 0; c < channels; ++c) {
346 a_send.planes[c] = tx_planes[c];
347 }
348  
349 oavx_status_t send_st = oavx_stream_send_audio(tx, &a_send);
350 ASSERT_EQ(send_st, OAVX_SUCCESS);
351  
352 /* Receive on the other side */
353 oavx_video_frame_t v_recv;
354 oavx_audio_frame_t a_recv;
355 oavx_metadata_frame_t m_recv;
356 memset(&v_recv, 0, sizeof(v_recv));
357 memset(&a_recv, 0, sizeof(a_recv));
358 memset(&m_recv, 0, sizeof(m_recv));
359  
360 oavx_frame_type_t ft = oavx_stream_receive(rx, &v_recv, &a_recv, &m_recv, 1000);
361 ASSERT_EQ(ft, OAVX_FRAME_TYPE_AUDIO);
362  
363 /* Verify decoded audio frame properties */
364 ASSERT_EQ(a_recv.sample_rate, (uint32_t)sample_rate);
365 ASSERT_EQ(a_recv.channels, (uint16_t)channels);
366 ASSERT_EQ(a_recv.frames, (uint32_t)frames);
367 ASSERT_EQ(a_recv.format, OAVX_AUDIO_FMT_FLT_PLANAR);
368 ASSERT_EQ(a_recv.pts_ns, 1234567890LL);
369 ASSERT_TRUE(a_recv.planes[0] != NULL);
370 ASSERT_TRUE(a_recv.planes[5] != NULL);
371  
372 /* Check telemetry */
373 oavx_telemetry_t telem;
374 ASSERT_TRUE(oavx_stream_get_telemetry(tx, &telem));
375 ASSERT_TRUE(telem.audio_frames_total >= 1);
376 ASSERT_TRUE(telem.total_wire_bytes > 0);
377  
378 oavx_stream_release_audio(rx, &a_recv);
379  
380 for (int c = 0; c < channels; ++c) {
381 free(tx_planes[c]);
382 }
383  
384 oavx_stream_close(rx);
385 oavx_stream_close(tx);
386 oavx_context_destroy(ctx);
387 printf(" [+] End-to-end compressed audio transmission verified\n");
388 }
389  
390 /* ========================================================================= */
391 /* TEST 5: ADPCM CORRUPTED / OVERFLOW HEADER RESILIENCE (FUZZER REGRESSION) */
392 /* ========================================================================= */
393  
394 static void test_adpcm_fuzz_bounds(void) {
395 printf("[*] 5. Testing ADPCM corrupted / overflow header resilience...\n");
396  
397 /* Buffer with 'AD' header and byte 7 = 0xFF (tests signed int shift overflow) */
398 uint8_t corrupt_hdr[32];
399 memset(corrupt_hdr, 0, sizeof(corrupt_hdr));
400 corrupt_hdr[0] = 0x41; /* 'A' */
401 corrupt_hdr[1] = 0x44; /* 'D' */
402 corrupt_hdr[2] = 2; /* 2 channels */
403 corrupt_hdr[3] = 0;
404 corrupt_hdr[4] = 0x00;
405 corrupt_hdr[5] = 0x00;
406 corrupt_hdr[6] = 0x00;
407 corrupt_hdr[7] = 0xFF; /* 255 in MSB: previously caused UBSan signed shift overflow */
408  
409 float p0[100], p1[100];
410 float* out_planes[2] = { p0, p1 };
411  
412 int ret = avx_adpcm_decode_to_planar_float(corrupt_hdr, sizeof(corrupt_hdr), 2, out_planes, 100);
413 ASSERT_EQ(ret, -1);
414  
415 /* Zero samples */
416 corrupt_hdr[7] = 0x00;
417 ret = avx_adpcm_decode_to_planar_float(corrupt_hdr, sizeof(corrupt_hdr), 2, out_planes, 100);
418 ASSERT_EQ(ret, -1);
419  
420 /* Truncated buffer */
421 ret = avx_adpcm_decode_to_planar_float(corrupt_hdr, 4, 2, out_planes, 100);
422 ASSERT_EQ(ret, -1);
423  
424 /* Non-positive max_samples boundary checks */
425 ret = avx_adpcm_decode_to_planar_float(corrupt_hdr, sizeof(corrupt_hdr), 2, out_planes, 0);
426 ASSERT_EQ(ret, -1);
427 ret = avx_adpcm_decode_to_planar_float(corrupt_hdr, sizeof(corrupt_hdr), 2, out_planes, -1);
428 ASSERT_EQ(ret, -1);
429  
430 printf(" [+] ADPCM header boundary and shift overflow protection verified\n");
431 }
432  
433 /* ========================================================================= */
434 /* TEST 6: MULTI-CHANNEL LIVE TRANSPORT (16-CH SDI & 32-CH DAW) */
435 /* ========================================================================= */
436  
437 static void test_multichannel_transport_16ch_32ch(void) {
438 printf("[*] 6. Testing End-to-End Multi-Channel Broadcast Transport (16ch & 32ch)...\n");
439  
440 oavx_context_t *ctx = oavx_context_create();
441 ASSERT_TRUE(ctx != NULL);
442  
443 /* --------------------------------------------------------------------- */
444 /* PART A: 16-Channel Broadcast SDI Audio Stream over TCP */
445 /* --------------------------------------------------------------------- */
446 printf(" -> Part A: 16-Channel SDI Broadcast Stream over TCP...\n");
447 {
448 oavx_stream_config_t tx_cfg = {
449 .name = "OAVX_16CH_SDI_FEED",
450 .direction = OAVX_DIR_TRANSMIT,
451 .clock_sync = false,
452 .groups = "Public"
453 };
454 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
455 ASSERT_TRUE(tx != NULL);
456  
457 const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx);
458 ASSERT_TRUE(ep != NULL && ep->address != NULL);
459  
460 oavx_stream_config_t rx_cfg = {
461 .name = "OAVX_16CH_SDI_RECV",
462 .endpoint = ep->address,
463 .direction = OAVX_DIR_RECEIVE,
464 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
465 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
466 .groups = "Public"
467 };
468 oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg);
469 ASSERT_TRUE(rx != NULL);
470  
471 usleep(150000);
472  
473 const int channels = 16;
474 const int frames = 960;
475 const int sample_rate = 48000;
476  
477 float *tx_planes[16];
478 for (int c = 0; c < channels; ++c) {
479 tx_planes[c] = (float*)malloc(frames * sizeof(float));
480 generate_sine_wave(tx_planes[c], frames, sample_rate, 100.0f * (float)(c + 1), 0.75f);
481 }
482  
483 oavx_audio_frame_t a_send;
484 memset(&a_send, 0, sizeof(a_send));
485 a_send.sample_rate = sample_rate;
486 a_send.channels = channels;
487 a_send.frames = frames;
488 a_send.format = OAVX_AUDIO_FMT_FLT_PLANAR;
489 a_send.stride_bytes = frames * sizeof(float);
490 a_send.pts_ns = 2000000000LL;
491 a_send.channel_mask = avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_16CH);
492 for (int c = 0; c < channels; ++c) {
493 a_send.planes[c] = tx_planes[c];
494 }
495  
496 oavx_status_t send_st = oavx_stream_send_audio(tx, &a_send);
497 ASSERT_EQ(send_st, OAVX_SUCCESS);
498  
499 oavx_audio_frame_t a_recv;
500 memset(&a_recv, 0, sizeof(a_recv));
501 oavx_frame_type_t ft = oavx_stream_receive(rx, NULL, &a_recv, NULL, 1000);
502 ASSERT_EQ(ft, OAVX_FRAME_TYPE_AUDIO);
503  
504 ASSERT_EQ(a_recv.sample_rate, (uint32_t)sample_rate);
505 ASSERT_EQ(a_recv.channels, 16);
506 ASSERT_EQ(a_recv.frames, (uint32_t)frames);
507 ASSERT_EQ(a_recv.format, OAVX_AUDIO_FMT_FLT_PLANAR);
508 ASSERT_EQ(a_recv.channel_mask, avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_16CH));
509  
510 /* Verify bit-exact fidelity across all 16 channels */
511 for (int c = 0; c < channels; ++c) {
512 ASSERT_TRUE(a_recv.planes[c] != NULL);
513 const float *rx_plane = (const float*)a_recv.planes[c];
514 for (int s = 0; s < frames; ++s) {
515 ASSERT_NEAR(rx_plane[s], tx_planes[c][s], 1e-6f);
516 }
517 }
518 for (int c = channels; c < OAVX_MAX_AUDIO_CHANNELS; ++c) {
519 ASSERT_TRUE(a_recv.planes[c] == NULL);
520 }
521  
522 oavx_stream_release_audio(rx, &a_recv);
523 for (int c = 0; c < channels; ++c) free(tx_planes[c]);
524  
525 oavx_stream_close(rx);
526 oavx_stream_close(tx);
527 printf(" [+] 16-channel uncompressed SDI stream verified with bit-exact fidelity\n");
528 }
529  
530 /* --------------------------------------------------------------------- */
531 /* PART B: 32-Channel Pro-Audio / Dante DAW Stream over TCP */
532 /* --------------------------------------------------------------------- */
533 printf(" -> Part B: 32-Channel Pro-Audio Matrix Stream over TCP...\n");
534 {
535 oavx_stream_config_t tx_cfg = {
536 .name = "OAVX_32CH_DAW_FEED",
537 .direction = OAVX_DIR_TRANSMIT,
538 .clock_sync = false,
539 .groups = "Public"
540 };
541 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
542 ASSERT_TRUE(tx != NULL);
543  
544 const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx);
545 ASSERT_TRUE(ep != NULL && ep->address != NULL);
546  
547 oavx_stream_config_t rx_cfg = {
548 .name = "OAVX_32CH_DAW_RECV",
549 .endpoint = ep->address,
550 .direction = OAVX_DIR_RECEIVE,
551 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
552 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
553 .groups = "Public"
554 };
555 oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg);
556 ASSERT_TRUE(rx != NULL);
557  
558 usleep(150000);
559  
560 const int channels = 32;
561 const int frames = 960;
562 const int sample_rate = 48000;
563  
564 float *tx_planes[32];
565 for (int c = 0; c < channels; ++c) {
566 tx_planes[c] = (float*)malloc(frames * sizeof(float));
567 generate_sine_wave(tx_planes[c], frames, sample_rate, 50.0f * (float)(c + 1), 0.70f);
568 }
569  
570 oavx_audio_frame_t a_send;
571 memset(&a_send, 0, sizeof(a_send));
572 a_send.sample_rate = sample_rate;
573 a_send.channels = channels;
574 a_send.frames = frames;
575 a_send.format = OAVX_AUDIO_FMT_FLT_PLANAR;
576 a_send.stride_bytes = frames * sizeof(float);
577 a_send.pts_ns = 3000000000LL;
578 a_send.channel_mask = avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_32CH);
579 for (int c = 0; c < channels; ++c) {
580 a_send.planes[c] = tx_planes[c];
581 }
582  
583 oavx_status_t send_st = oavx_stream_send_audio(tx, &a_send);
584 ASSERT_EQ(send_st, OAVX_SUCCESS);
585  
586 oavx_audio_frame_t a_recv;
587 memset(&a_recv, 0, sizeof(a_recv));
588 oavx_frame_type_t ft = oavx_stream_receive(rx, NULL, &a_recv, NULL, 1000);
589 ASSERT_EQ(ft, OAVX_FRAME_TYPE_AUDIO);
590  
591 ASSERT_EQ(a_recv.sample_rate, (uint32_t)sample_rate);
592 ASSERT_EQ(a_recv.channels, 32);
593 ASSERT_EQ(a_recv.frames, (uint32_t)frames);
594 ASSERT_EQ(a_recv.format, OAVX_AUDIO_FMT_FLT_PLANAR);
595 ASSERT_EQ(a_recv.channel_mask, avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_32CH));
596  
597 /* Verify bit-exact fidelity across all 32 channels */
598 for (int c = 0; c < channels; ++c) {
599 ASSERT_TRUE(a_recv.planes[c] != NULL);
600 const float *rx_plane = (const float*)a_recv.planes[c];
601 for (int s = 0; s < frames; ++s) {
602 ASSERT_NEAR(rx_plane[s], tx_planes[c][s], 1e-6f);
603 }
604 }
605  
606 oavx_stream_release_audio(rx, &a_recv);
607 for (int c = 0; c < channels; ++c) free(tx_planes[c]);
608  
609 oavx_stream_close(rx);
610 oavx_stream_close(tx);
611 printf(" [+] 32-channel uncompressed Pro-Audio stream verified with bit-exact fidelity\n");
612 }
613  
614 /* --------------------------------------------------------------------- */
615 /* PART C: 16-Channel Broadcast SDI Audio Stream over RUDP */
616 /* --------------------------------------------------------------------- */
617 printf(" -> Part C: 16-Channel SDI Broadcast Stream over RUDP...\n");
618 {
619 oavx_stream_config_t tx_cfg = {
620 .name = "OAVX_16CH_RUDP_FEED",
621 .direction = OAVX_DIR_TRANSMIT,
622 .transport = OAVX_TRANSPORT_RUDP,
623 .clock_sync = false,
624 .groups = "Public"
625 };
626 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
627 ASSERT_TRUE(tx != NULL);
628  
629 const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx);
630 ASSERT_TRUE(ep != NULL && ep->address != NULL);
631  
632 oavx_stream_config_t rx_cfg = {
633 .name = "OAVX_16CH_RUDP_RECV",
634 .endpoint = ep->address,
635 .direction = OAVX_DIR_RECEIVE,
636 .transport = OAVX_TRANSPORT_RUDP,
637 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
638 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
639 .groups = "Public"
640 };
641 oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg);
642 ASSERT_TRUE(rx != NULL);
643  
644 usleep(150000);
645  
646 const int channels = 16;
647 const int frames = 480;
648 const int sample_rate = 48000;
649  
650 float *tx_planes[16];
651 for (int c = 0; c < channels; ++c) {
652 tx_planes[c] = (float*)malloc(frames * sizeof(float));
653 generate_sine_wave(tx_planes[c], frames, sample_rate, 120.0f * (float)(c + 1), 0.70f);
654 }
655  
656 oavx_audio_frame_t a_send;
657 memset(&a_send, 0, sizeof(a_send));
658 a_send.sample_rate = sample_rate;
659 a_send.channels = channels;
660 a_send.frames = frames;
661 a_send.format = OAVX_AUDIO_FMT_FLT_PLANAR;
662 a_send.stride_bytes = frames * sizeof(float);
663 a_send.pts_ns = 4000000000LL;
664 a_send.channel_mask = avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_16CH);
665 for (int c = 0; c < channels; ++c) {
666 a_send.planes[c] = tx_planes[c];
667 }
668  
669 oavx_status_t send_st = oavx_stream_send_audio(tx, &a_send);
670 ASSERT_EQ(send_st, OAVX_SUCCESS);
671  
672 oavx_audio_frame_t a_recv;
673 memset(&a_recv, 0, sizeof(a_recv));
674 oavx_frame_type_t ft = oavx_stream_receive(rx, NULL, &a_recv, NULL, 500);
675 ASSERT_EQ(ft, OAVX_FRAME_TYPE_AUDIO);
676  
677 ASSERT_EQ(a_recv.sample_rate, (uint32_t)sample_rate);
678 ASSERT_EQ(a_recv.channels, 16);
679 ASSERT_EQ(a_recv.frames, (uint32_t)frames);
680 ASSERT_EQ(a_recv.format, OAVX_AUDIO_FMT_FLT_PLANAR);
681 ASSERT_EQ(a_recv.channel_mask, avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_16CH));
682  
683 for (int c = 0; c < channels; ++c) {
684 ASSERT_TRUE(a_recv.planes[c] != NULL);
685 const float *rx_plane = (const float*)a_recv.planes[c];
686 for (int s = 0; s < frames; ++s) {
687 ASSERT_NEAR(rx_plane[s], tx_planes[c][s], 1e-6f);
688 }
689 }
690  
691 oavx_stream_release_audio(rx, &a_recv);
692 for (int c = 0; c < channels; ++c) free(tx_planes[c]);
693  
694 oavx_stream_close(rx);
695 oavx_stream_close(tx);
696 printf(" [+] 16-channel RUDP stream verified with bit-exact fidelity\n");
697 }
698  
699 /* --------------------------------------------------------------------- */
700 /* PART D: 32-Channel Pro-Audio Matrix Stream over RUDP */
701 /* --------------------------------------------------------------------- */
702 printf(" -> Part D: 32-Channel Pro-Audio Matrix Stream over RUDP...\n");
703 {
704 oavx_stream_config_t tx_cfg = {
705 .name = "OAVX_32CH_RUDP_FEED",
706 .direction = OAVX_DIR_TRANSMIT,
707 .transport = OAVX_TRANSPORT_RUDP,
708 .clock_sync = false,
709 .groups = "Public"
710 };
711 oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg);
712 ASSERT_TRUE(tx != NULL);
713  
714 const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx);
715 ASSERT_TRUE(ep != NULL && ep->address != NULL);
716  
717 oavx_stream_config_t rx_cfg = {
718 .name = "OAVX_32CH_RUDP_RECV",
719 .endpoint = ep->address,
720 .direction = OAVX_DIR_RECEIVE,
721 .transport = OAVX_TRANSPORT_RUDP,
722 .color_format = OAVX_COLOR_FORMAT_UYVY_RGBA,
723 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
724 .groups = "Public"
725 };
726 oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg);
727 ASSERT_TRUE(rx != NULL);
728  
729 usleep(150000);
730  
731 const int channels = 32;
732 const int frames = 480;
733 const int sample_rate = 48000;
734  
735 float *tx_planes[32];
736 for (int c = 0; c < channels; ++c) {
737 tx_planes[c] = (float*)malloc(frames * sizeof(float));
738 generate_sine_wave(tx_planes[c], frames, sample_rate, 60.0f * (float)(c + 1), 0.65f);
739 }
740  
741 oavx_audio_frame_t a_send;
742 memset(&a_send, 0, sizeof(a_send));
743 a_send.sample_rate = sample_rate;
744 a_send.channels = channels;
745 a_send.frames = frames;
746 a_send.format = OAVX_AUDIO_FMT_FLT_PLANAR;
747 a_send.stride_bytes = frames * sizeof(float);
748 a_send.pts_ns = 5000000000LL;
749 a_send.channel_mask = avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_32CH);
750 for (int c = 0; c < channels; ++c) {
751 a_send.planes[c] = tx_planes[c];
752 }
753  
754 oavx_status_t send_st = oavx_stream_send_audio(tx, &a_send);
755 ASSERT_EQ(send_st, OAVX_SUCCESS);
756  
757 oavx_audio_frame_t a_recv;
758 memset(&a_recv, 0, sizeof(a_recv));
759 oavx_frame_type_t ft = oavx_stream_receive(rx, NULL, &a_recv, NULL, 500);
760 ASSERT_EQ(ft, OAVX_FRAME_TYPE_AUDIO);
761  
762 ASSERT_EQ(a_recv.sample_rate, (uint32_t)sample_rate);
763 ASSERT_EQ(a_recv.channels, 32);
764 ASSERT_EQ(a_recv.frames, (uint32_t)frames);
765 ASSERT_EQ(a_recv.format, OAVX_AUDIO_FMT_FLT_PLANAR);
766 ASSERT_EQ(a_recv.channel_mask, avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_32CH));
767  
768 /* Verify bit-exact fidelity across all 32 channels */
769 for (int c = 0; c < channels; ++c) {
770 ASSERT_TRUE(a_recv.planes[c] != NULL);
771 const float *rx_plane = (const float*)a_recv.planes[c];
772 for (int s = 0; s < frames; ++s) {
773 ASSERT_NEAR(rx_plane[s], tx_planes[c][s], 1e-6f);
774 }
775 }
776  
777 oavx_stream_release_audio(rx, &a_recv);
778 for (int c = 0; c < channels; ++c) free(tx_planes[c]);
779  
780 oavx_stream_close(rx);
781 oavx_stream_close(tx);
782 printf(" [+] 32-channel RUDP stream verified with bit-exact fidelity\n");
783 }
784  
785 oavx_context_destroy(ctx);
786 printf(" [+] Multi-Channel Broadcast Transport (16ch & 32ch): PASSED\n");
787 }
788  
789 int main(void) {
790 printf("=== OpenAVX Audio Codec Tests ===\n\n");
791  
792 test_adpcm_roundtrip();
793 test_opus_multistream();
794 test_accumulator_and_flush();
795 test_stream_compressed_loopback();
796 test_adpcm_fuzz_bounds();
797 test_multichannel_transport_16ch_32ch();
798  
799 printf("\n[PASS] openavx_audio_codec_test\n");
800 return 0;
801 }