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 Library
6 * test/openavx_framesync_util_test.c
7 * Comprehensive test suite for Frame Synchronizer & Interleaved Audio / Video Utilities
8 */
9  
10 #include "openavx.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 #include <assert.h>
16 #include <unistd.h>
17 #include <time.h>
18  
19 #define PI 3.14159265358979323846
20  
21 static void test_interleaved_audio_conversions(void) {
22 printf("[*] Testing Audio Interleaved Conversions (16s, 32f, 32s)...\n");
23  
24 const int sample_rate = 48000;
25 const int channels = 2;
26 const int samples = 480;
27  
28 /* 1. Allocate Planar Float Source */
29 float* planar_src = (float*)malloc((size_t)channels * samples * sizeof(float));
30 assert(planar_src != NULL);
31  
32 for (int s = 0; s < samples; ++s) {
33 planar_src[0 * samples + s] = sinf(2.0f * (float)PI * 440.0f * (float)s / (float)sample_rate); /* Left: 440Hz */
34 planar_src[1 * samples + s] = sinf(2.0f * (float)PI * 880.0f * (float)s / (float)sample_rate); /* Right: 880Hz */
35 }
36  
37 oavx_audio_frame_t src_frame = {
38 .sample_rate = sample_rate,
39 .channels = channels,
40 .frames = samples,
41 .format = OAVX_AUDIO_FMT_FLT_PLANAR,
42 .planes = { planar_src, planar_src + samples, NULL, NULL, NULL, NULL, NULL, NULL },
43 .stride_bytes = samples * sizeof(float),
44 .pts_ns = 1234567,
45 .metadata = NULL,
46 .metadata_len = 0,
47 .user_ctx = NULL
48 };
49  
50 /* 2. Planar -> Interleaved 16s -> Planar Roundtrip */
51 int16_t* buf_16s = (int16_t*)malloc((size_t)channels * samples * sizeof(int16_t));
52 assert(buf_16s != NULL);
53  
54 oavx_audio_frame_interleaved_16s_t frame_16s = {
55 .reference_level = 20,
56 .data = buf_16s
57 };
58  
59 oavx_util_audio_to_interleaved_16s(&src_frame, &frame_16s);
60 assert(frame_16s.sample_rate == (uint32_t)sample_rate);
61 assert(frame_16s.channels == (uint16_t)channels);
62 assert(frame_16s.frames == (uint32_t)samples);
63 assert(frame_16s.pts_ns == 1234567);
64  
65 /* Verify interleaving pattern: buf_16s[0] = L0, buf_16s[1] = R0, buf_16s[2] = L1, buf_16s[3] = R1 */
66 assert(abs(buf_16s[0] - (int)roundf(planar_src[0] * 32767.0f)) <= 1);
67 assert(abs(buf_16s[1] - (int)roundf(planar_src[samples] * 32767.0f)) <= 1);
68  
69 /* Convert back from 16s to Planar */
70 float* planar_dst_16 = (float*)malloc((size_t)channels * samples * sizeof(float));
71 assert(planar_dst_16 != NULL);
72 oavx_audio_frame_t dst_16 = {
73 .planes = { planar_dst_16, planar_dst_16 + samples, NULL, NULL, NULL, NULL, NULL, NULL },
74 .stride_bytes = samples * sizeof(float)
75 };
76 oavx_util_audio_from_interleaved_16s(&frame_16s, &dst_16);
77  
78 for (int c = 0; c < channels; ++c) {
79 for (int s = 0; s < samples; ++s) {
80 float diff = fabsf(planar_src[c * samples + s] - planar_dst_16[c * samples + s]);
81 /* 16-bit quantization maximum error is 1.0 / 32767 ~ 0.000031 */
82 assert(diff < 0.0001f);
83 }
84 }
85 printf(" [+] Interleaved 16s roundtrip verified (16-bit precision intact)\n");
86  
87 /* 3. Planar -> Interleaved 32f -> Planar Roundtrip */
88 float* buf_32f = (float*)malloc((size_t)channels * samples * sizeof(float));
89 assert(buf_32f != NULL);
90 oavx_audio_frame_interleaved_32f_t frame_32f = {
91 .data = buf_32f
92 };
93 oavx_util_audio_to_interleaved_32f(&src_frame, &frame_32f);
94 assert(frame_32f.sample_rate == (uint32_t)sample_rate);
95 assert(frame_32f.channels == (uint16_t)channels);
96 assert(frame_32f.frames == (uint32_t)samples);
97  
98 float* planar_dst_32f = (float*)malloc((size_t)channels * samples * sizeof(float));
99 assert(planar_dst_32f != NULL);
100 oavx_audio_frame_t dst_32f = {
101 .planes = { planar_dst_32f, planar_dst_32f + samples, NULL, NULL, NULL, NULL, NULL, NULL },
102 .stride_bytes = samples * sizeof(float)
103 };
104 oavx_util_audio_from_interleaved_32f(&frame_32f, &dst_32f);
105  
106 for (int c = 0; c < channels; ++c) {
107 for (int s = 0; s < samples; ++s) {
108 assert(planar_src[c * samples + s] == planar_dst_32f[c * samples + s]);
109 }
110 }
111 printf(" [+] Interleaved 32f roundtrip verified (bitwise exact)\n");
112  
113 /* 4. Planar -> Interleaved 32s -> Planar Roundtrip */
114 int32_t* buf_32s = (int32_t*)malloc((size_t)channels * samples * sizeof(int32_t));
115 assert(buf_32s != NULL);
116 oavx_audio_frame_interleaved_32s_t frame_32s = {
117 .data = buf_32s
118 };
119 oavx_util_audio_to_interleaved_32s(&src_frame, &frame_32s);
120 assert(frame_32s.sample_rate == (uint32_t)sample_rate);
121 assert(frame_32s.channels == (uint16_t)channels);
122 assert(frame_32s.frames == (uint32_t)samples);
123  
124 float* planar_dst_32s = (float*)malloc((size_t)channels * samples * sizeof(float));
125 assert(planar_dst_32s != NULL);
126 oavx_audio_frame_t dst_32s = {
127 .planes = { planar_dst_32s, planar_dst_32s + samples, NULL, NULL, NULL, NULL, NULL, NULL },
128 .stride_bytes = samples * sizeof(float)
129 };
130 oavx_util_audio_from_interleaved_32s(&frame_32s, &dst_32s);
131  
132 for (int c = 0; c < channels; ++c) {
133 for (int s = 0; s < samples; ++s) {
134 float diff = fabsf(planar_src[c * samples + s] - planar_dst_32s[c * samples + s]);
135 assert(diff < 0.000001f);
136 }
137 }
138 printf(" [+] Interleaved 32s roundtrip verified (32-bit precision intact)\n");
139  
140 free(planar_src);
141 free(buf_16s);
142 free(planar_dst_16);
143 free(buf_32f);
144 free(planar_dst_32f);
145 free(buf_32s);
146 free(planar_dst_32s);
147 }
148  
149 static void test_v210_p216_conversions(void) {
150 printf("[*] Testing SMPTE 292M 10-bit SDI V210 <-> P216 Video Conversions...\n");
151  
152 const int width = 1920;
153 const int height = 1080;
154  
155 /* Allocate source P216 frame */
156 size_t p216_bytes = (size_t)width * height * 2 * 2; /* Y plane (width*height*2) + UV plane (width*height*2) */
157 uint16_t* p216_orig = (uint16_t*)malloc(p216_bytes);
158 assert(p216_orig != NULL);
159  
160 uint16_t* y_orig = p216_orig;
161 uint16_t* uv_orig = p216_orig + (size_t)width * height;
162  
163 /* Fill with deterministic 10-bit test pattern (64 to 940 for Y, 64 to 960 for UV) */
164 for (int y = 0; y < height; ++y) {
165 for (int x = 0; x < width; ++x) {
166 uint16_t y_val = (uint16_t)((64 + ((x * 876) / width)) & 0x3FF);
167 uint16_t u_val = (uint16_t)((64 + ((y * 896) / height)) & 0x3FF);
168 uint16_t v_val = (uint16_t)((512 + ((x - y) & 0xFF)) & 0x3FF);
169  
170 y_orig[y * width + x] = (uint16_t)(y_val << 6); /* Left-justified 16-bit */
171 if ((x % 2) == 0) {
172 uv_orig[y * width + x + 0] = (uint16_t)(u_val << 6);
173 uv_orig[y * width + x + 1] = (uint16_t)(v_val << 6);
174 }
175 }
176 }
177  
178 oavx_video_frame_t src_p216 = {
179 .width = width,
180 .height = height,
181 .fourcc = OAVX_FOURCC_VIDEO_TYPE_P216,
182 .scan_type = OAVX_SCAN_PROGRESSIVE,
183 .planes = { (uint8_t*)p216_orig, (uint8_t*)(p216_orig + width * height), NULL, NULL },
184 .strides = { (size_t)width * 2, (size_t)width * 2, 0, 0 },
185 .framerate = { 60000, 1000 },
186 .aspect_ratio = { 16, 9 },
187 .pts_ns = 10002000,
188 .dts_ns = 0,
189 .metadata = NULL,
190 .metadata_len = 0,
191 .user_ctx = NULL
192 };
193  
194 /* Allocate V210 destination frame */
195 size_t v210_stride = (size_t)(width / 6) * 16;
196 size_t v210_bytes = v210_stride * height;
197 uint8_t* v210_buf = (uint8_t*)malloc(v210_bytes);
198 assert(v210_buf != NULL);
199  
200 oavx_video_frame_t dst_v210 = {
201 .planes = { v210_buf, NULL, NULL, NULL }
202 };
203  
204 /* Convert P216 -> V210 */
205 oavx_util_P216_to_V210(&src_p216, &dst_v210);
206 assert(dst_v210.width == (uint32_t)width);
207 assert(dst_v210.height == (uint32_t)height);
208 assert(dst_v210.fourcc == (oavx_fourcc_video_type_e)0x30313276); /* "v210" */
209 assert(dst_v210.strides[0] == v210_stride);
210  
211 /* Allocate unpacked P216 frame */
212 uint16_t* p216_unpacked = (uint16_t*)malloc(p216_bytes);
213 assert(p216_unpacked != NULL);
214 memset(p216_unpacked, 0, p216_bytes);
215  
216 oavx_video_frame_t dst_p216 = {
217 .planes = { (uint8_t*)p216_unpacked, NULL, NULL, NULL }
218 };
219  
220 /* Convert V210 -> P216 */
221 oavx_util_V210_to_P216(&dst_v210, &dst_p216);
222 assert(dst_p216.width == (uint32_t)width);
223 assert(dst_p216.height == (uint32_t)height);
224 assert(dst_p216.fourcc == OAVX_FOURCC_VIDEO_TYPE_P216);
225  
226 /* Verify 100% bitwise parity on all 10-bit samples */
227 int mismatches = 0;
228 for (size_t i = 0; i < p216_bytes / 2; ++i) {
229 if (p216_orig[i] != p216_unpacked[i]) {
230 mismatches++;
231 }
232 }
233 assert(mismatches == 0);
234 printf(" [+] SMPTE 292M V210 <-> P216 10-bit roundtrip 100%% bitwise exact (%zu samples verified)\n", p216_bytes / 2);
235  
236 free(p216_orig);
237 free(v210_buf);
238 free(p216_unpacked);
239 }
240  
241 static void test_framesync_and_interleaved_send(void) {
242 printf("[*] Testing FrameSync Engine & Interleaved Audio Transmission...\n");
243  
244 oavx_context_t *ctx = oavx_context_create();
245 assert(ctx != NULL);
246  
247 /* Create Sender */
248 oavx_stream_config_t send_create = {
249 .name = "FrameSync_Test_Stream",
250 .groups = NULL,
251 .direction = OAVX_DIR_TRANSMIT,
252 .clock_sync = false
253 };
254 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create);
255 assert(p_send != NULL);
256  
257 /* Create Receiver connected to loopback sender */
258 oavx_stream_config_t recv_create = {
259 .name = "FrameSync_Test_Recv",
260 .endpoint = "127.0.0.1:7960",
261 .direction = OAVX_DIR_RECEIVE,
262 .color_format = OAVX_COLOR_FORMAT_BGRX_BGRA,
263 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
264 .clock_sync = false
265 };
266 oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create);
267 assert(p_recv != NULL);
268  
269 /* Create FrameSynchronizer */
270 oavx_framesync_t *p_fs = oavx_framesync_create(p_recv);
271 assert(p_fs != NULL);
272  
273 /* Wait for receiver to connect to sender */
274 for (int wait = 0; wait < 30; ++wait) {
275 if (oavx_stream_get_connection_count(p_send, 50) > 0) break;
276 struct timespec ts = {0, 20000000L};
277 nanosleep(&ts, NULL);
278 }
279  
280 /* Send Video Frame & Interleaved Audio */
281 const int w = 320;
282 const int h = 240;
283 size_t video_size = (size_t)w * h * 2;
284 uint8_t* vdata = (uint8_t*)malloc(video_size);
285 assert(vdata != NULL);
286 memset(vdata, 0x80, video_size); /* 50% gray UYVY */
287  
288 oavx_video_frame_t vframe = {
289 .width = w,
290 .height = h,
291 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY,
292 .scan_type = OAVX_SCAN_PROGRESSIVE,
293 .planes = { vdata, NULL, NULL, NULL },
294 .strides = { (size_t)w * 2, 0, 0, 0 },
295 .framerate = { 30000, 1000 },
296 .aspect_ratio = { 4, 3 },
297 .pts_ns = 500000,
298 .dts_ns = 0,
299 .metadata = "<openavx_color_info color_format=\"rec709\"/>",
300 .metadata_len = strlen("<openavx_color_info color_format=\"rec709\"/>"),
301 .user_ctx = NULL
302 };
303  
304 const int audio_rate = 48000;
305 const int audio_ch = 2;
306 const int audio_samples = 1920; /* 40ms of 48kHz audio */
307 int16_t* pcm_16s = (int16_t*)malloc((size_t)audio_ch * audio_samples * sizeof(int16_t));
308 assert(pcm_16s != NULL);
309 for (int s = 0; s < audio_samples; ++s) {
310 pcm_16s[s * 2 + 0] = (int16_t)(sinf(2.0f * (float)PI * 1000.0f * (float)s / 48000.0f) * 16384.0f);
311 pcm_16s[s * 2 + 1] = (int16_t)(sinf(2.0f * (float)PI * 500.0f * (float)s / 48000.0f) * 16384.0f);
312 }
313  
314 oavx_audio_frame_interleaved_16s_t send_audio_16s = {
315 .sample_rate = audio_rate,
316 .channels = audio_ch,
317 .frames = audio_samples,
318 .pts_ns = 500000,
319 .reference_level = 20,
320 .data = pcm_16s
321 };
322  
323 /* Allocate planar audio destination to send */
324 float* planar_send_buf = (float*)malloc((size_t)audio_ch * audio_samples * sizeof(float));
325 assert(planar_send_buf != NULL);
326 oavx_audio_frame_t planar_send_frame = {
327 .planes = { planar_send_buf, planar_send_buf + audio_samples, NULL, NULL, NULL, NULL, NULL, NULL },
328 .stride_bytes = audio_samples * sizeof(float)
329 };
330 oavx_util_audio_from_interleaved_16s(&send_audio_16s, &planar_send_frame);
331  
332 /* Transmit several frames to seed the pipeline */
333 for (int i = 0; i < 5; ++i) {
334 oavx_stream_send_video(p_send, &vframe);
335 oavx_stream_send_audio(p_send, &planar_send_frame);
336 struct timespec ts = {0, 20000000L}; /* 20ms */
337 nanosleep(&ts, NULL);
338 }
339  
340 /* Allow network packets to arrive */
341 struct timespec ts = {0, 150000000L}; /* 150ms */
342 nanosleep(&ts, NULL);
343  
344 /* Capture Synchronized Video */
345 oavx_video_frame_t fs_video;
346 memset(&fs_video, 0, sizeof(fs_video));
347 oavx_framesync_capture_video(p_fs, &fs_video, OAVX_SCAN_PROGRESSIVE);
348 assert(fs_video.planes[0] != NULL);
349 assert(fs_video.width == (uint32_t)w);
350 assert(fs_video.height == (uint32_t)h);
351 printf(" [+] FrameSync captured synchronized video frame: %ux%u (stride=%zu)\n",
352 fs_video.width, fs_video.height, fs_video.strides[0]);
353 oavx_framesync_free_video(p_fs, &fs_video);
354  
355 /* Capture Synchronized Audio with Dynamic Resampling (48kHz source -> 44.1kHz host clock) */
356 int initial_queue = oavx_framesync_audio_queue_depth(p_fs);
357 printf(" [+] Initial FrameSync audio queue depth: %d samples\n", initial_queue);
358  
359 oavx_audio_frame_t fs_audio;
360 memset(&fs_audio, 0, sizeof(fs_audio));
361 const int host_audio_rate = 44100;
362 const int host_samples_needed = 441; /* Exactly 10ms of 44.1kHz audio */
363  
364 oavx_framesync_capture_audio(p_fs, &fs_audio, host_audio_rate, 2, host_samples_needed);
365 assert(fs_audio.planes[0] != NULL);
366 assert(fs_audio.sample_rate == (uint32_t)host_audio_rate);
367 assert(fs_audio.channels == 2);
368 assert(fs_audio.frames == (uint32_t)host_samples_needed);
369 printf(" [+] FrameSync locked audio: resampled 48kHz -> %uHz (%u samples delivered)\n",
370 fs_audio.sample_rate, fs_audio.frames);
371 oavx_framesync_free_audio(p_fs, &fs_audio);
372  
373 int final_queue = oavx_framesync_audio_queue_depth(p_fs);
374 printf(" [+] Post-pull FrameSync audio queue depth: %d samples\n", final_queue);
375  
376 /* Teardown */
377 oavx_framesync_destroy(p_fs);
378 oavx_stream_close(p_recv);
379 oavx_stream_close(p_send);
380 free(vdata);
381 free(pcm_16s);
382 free(planar_send_buf);
383 oavx_context_destroy(ctx);
384 }
385  
386 static void test_framesync_multiplane_video(void) {
387 printf("[*] Testing FrameSync Multi-Plane Video Anchoring (UYVA & NV12)...\n");
388  
389 oavx_context_t* ctx = oavx_context_create();
390 assert(ctx != NULL);
391  
392 oavx_stream_config_t send_create = {
393 .name = "FrameSync_MultiPlane_Stream",
394 .groups = NULL,
395 .direction = OAVX_DIR_TRANSMIT,
396 .clock_sync = false
397 };
398 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create);
399 assert(p_send != NULL);
400  
401 oavx_stream_config_t recv_create = {
402 .name = "FrameSync_MultiPlane_Recv",
403 .endpoint = "127.0.0.1:7960",
404 .direction = OAVX_DIR_RECEIVE,
405 .color_format = OAVX_COLOR_FORMAT_FASTEST,
406 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
407 .clock_sync = false
408 };
409 oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create);
410 assert(p_recv != NULL);
411  
412 oavx_framesync_t *p_fs = oavx_framesync_create(p_recv);
413 assert(p_fs != NULL);
414  
415 for (int wait = 0; wait < 30; ++wait) {
416 if (oavx_stream_get_connection_count(p_send, 50) > 0) break;
417 struct timespec ts = {0, 20000000L};
418 nanosleep(&ts, NULL);
419 }
420  
421 const int w = 160;
422 const int h = 90;
423 uint8_t* uyva_yuv = (uint8_t*)malloc((size_t)w * h * 2);
424 uint8_t* uyva_alpha = (uint8_t*)malloc((size_t)w * h);
425 assert(uyva_yuv && uyva_alpha);
426 memset(uyva_yuv, 0x80, (size_t)w * h * 2);
427 memset(uyva_alpha, 0xEE, (size_t)w * h);
428  
429 oavx_video_frame_t uyva_frame = {
430 .width = (uint32_t)w,
431 .height = (uint32_t)h,
432 .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVA,
433 .scan_type = OAVX_SCAN_PROGRESSIVE,
434 .planes = { uyva_yuv, uyva_alpha, NULL, NULL },
435 .strides = { (size_t)w * 2, (size_t)w, 0, 0 },
436 .framerate = { 30000, 1000 },
437 .aspect_ratio = { 16, 9 },
438 .pts_ns = 555555,
439 .dts_ns = 0,
440 .metadata = NULL,
441 .metadata_len = 0,
442 .user_ctx = NULL
443 };
444  
445 for (int i = 0; i < 5; ++i) {
446 oavx_stream_send_video(p_send, &uyva_frame);
447 struct timespec ts = {0, 20000000L};
448 nanosleep(&ts, NULL);
449 }
450  
451 struct timespec ts = {0, 150000000L};
452 nanosleep(&ts, NULL);
453  
454 oavx_video_frame_t fs_video;
455 memset(&fs_video, 0, sizeof(fs_video));
456 oavx_framesync_capture_video(p_fs, &fs_video, OAVX_SCAN_PROGRESSIVE);
457 assert(fs_video.planes[0] != NULL);
458 assert(fs_video.planes[1] != NULL);
459 assert(fs_video.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVA);
460 assert(fs_video.strides[0] == (size_t)w * 2);
461 assert(fs_video.strides[1] == (size_t)w);
462 /* Verify planes[1] is inside the delivery buffer at offset w * h * 2 */
463 assert(fs_video.planes[1] == (uint8_t*)fs_video.planes[0] + (size_t)w * h * 2);
464 assert(((uint8_t*)fs_video.planes[1])[0] == 0xEE);
465 assert(((uint8_t*)fs_video.planes[0])[0] == 0x80);
466 printf(" [+] FrameSync UYVA plane 1 correctly anchored and preserved alpha byte 0xEE\n");
467  
468 oavx_framesync_free_video(p_fs, &fs_video);
469 assert(fs_video.planes[0] == NULL);
470 assert(fs_video.planes[1] == NULL);
471  
472 oavx_framesync_destroy(p_fs);
473 oavx_stream_close(p_recv);
474 oavx_stream_close(p_send);
475 free(uyva_yuv);
476 free(uyva_alpha);
477 oavx_context_destroy(ctx);
478 printf(" [+] FrameSync multi-plane video test passed\n");
479 }
480  
481 int main(void) {
482 printf("=== OpenAVX FrameSync & Format Utility Tests ===\n");
483  
484 test_interleaved_audio_conversions();
485 test_v210_p216_conversions();
486 test_framesync_and_interleaved_send();
487 test_framesync_multiplane_video();
488  
489 printf("\n[PASS] openavx_framesync_util_test\n");
490 return 0;
491 }