Raw ↗
+ +
a18b27f fix(windows): resolve Win32 Winsock, signal, and CLI test compatibility
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_hdr_test.c - OpenAVX P216 16-bit, and HDR Metadata Test Suite
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <assert.h>
14 #include <time.h>
15 #include <dlfcn.h>
16 #include "avx_platform.h"
17  
18 #include "openavx.h"
19 #include "avx_hdr.h"
20 #include "avx_color.h"
21  
22 #define TEST_WIDTH 320
23 #define TEST_HEIGHT 240
24 #define TEST_PORT 7960
25  
26 typedef struct {
27 oavx_stream_t* p_send;
28 volatile bool running;
29 uint8_t* p216_data;
30 const char* hdr_metadata;
31 } sender_thread_arg_t;
32  
33 static void* sender_thread_func(void* arg) {
34 sender_thread_arg_t* s = (sender_thread_arg_t*)arg;
35  
36 oavx_video_frame_t frame = {
37 .width = TEST_WIDTH,
38 .height = TEST_HEIGHT,
39 .fourcc = OAVX_FOURCC_VIDEO_TYPE_P216,
40 .scan_type = OAVX_SCAN_PROGRESSIVE,
41 .planes = { s->p216_data, NULL, NULL, NULL },
42 .strides = { (size_t)TEST_WIDTH * 2, 0, 0, 0 },
43 .framerate = { 60000, 1000 },
44 .aspect_ratio = { (uint32_t)TEST_WIDTH, (uint32_t)TEST_HEIGHT },
45 .pts_ns = 55667788,
46 .dts_ns = 0,
47 .metadata = s->hdr_metadata,
48 .metadata_len = s->hdr_metadata ? strlen(s->hdr_metadata) : 0,
49 .user_ctx = NULL
50 };
51  
52 while (s->running) {
53 struct timespec ts = { 0, 15000000L }; /* ~60 fps */
54 nanosleep(&ts, NULL);
55 oavx_stream_send_video(s->p_send, &frame);
56 }
57 return NULL;
58 }
59  
60 int main(void) {
61 printf("\n======================================================================\n");
62 printf(" openavx HDR & 16-bit Video Verification Test Suite \n");
63 printf(" Features: P216 16-bit Video, Dynamic Load, and Rec.2100 HDR Metadata\n");
64 printf("======================================================================\n\n");
65  
66 /* --------------------------------------------------------------------- */
67 /* 1. Test Dynamic Loading: dlopen("build/libopenavx.so.1") */
68 /* --------------------------------------------------------------------- */
69 printf("[*] 1. Testing dynamic dlopen of libopenavx.so.1 and symbol resolution...\n");
70 void* handle = dlopen("build/libopenavx.so.1", RTLD_NOW);
71 if (!handle) handle = dlopen("./build/libopenavx.so.1", RTLD_NOW);
72 if (!handle) handle = dlopen("libopenavx.so.1", RTLD_NOW);
73 if (!handle) handle = dlopen("./libopenavx.so.1", RTLD_NOW);
74 if (!handle) handle = dlopen("../build/libopenavx.so.1", RTLD_NOW);
75 if (!handle) handle = dlopen("build/libopenavx.so", RTLD_NOW);
76 if (!handle) handle = dlopen("./build/libopenavx.so", RTLD_NOW);
77 if (!handle) handle = dlopen("libopenavx.so", RTLD_NOW);
78  
79 if (handle) {
80 typedef const char* (*version_fn)(void);
81 version_fn get_ver = (version_fn)dlsym(handle, "oavx_version");
82 assert(get_ver != NULL);
83 printf(" [+] Dynamic load succeeded: Version '%s'\n", get_ver());
84 dlclose(handle);
85 } else {
86 printf(" [*] Shared library not in search path yet (will verify post-build)\n");
87 }
88  
89 /* --------------------------------------------------------------------- */
90 /* 2. Test HDR Metadata XML Formatting and Parsing */
91 /* --------------------------------------------------------------------- */
92 printf("[*] 2. Testing HDR Metadata XML Formatting & Parsing...\n");
93  
94 /* Test A: HLG over BT.2020 */
95 avx_color_info_t hlg_info = {
96 .transfer = AVX_COLOR_TRANSFER_BT2100_HLG,
97 .matrix = AVX_COLOR_MATRIX_BT2020,
98 .primaries = AVX_COLOR_PRIMARIES_BT2020,
99 .max_cll = 0,
100 .max_fall = 0
101 };
102 char xml_buf[256];
103 bool fmt_ok = avx_hdr_format_color_info(&hlg_info, xml_buf, sizeof(xml_buf));
104 assert(fmt_ok);
105 printf(" [+] Formatted HLG XML: '%s'\n", xml_buf);
106 assert(strstr(xml_buf, "transfer=\"bt_2100_hlg\"") != NULL);
107 assert(strstr(xml_buf, "matrix=\"bt_2020\"") != NULL);
108 assert(strstr(xml_buf, "primaries=\"bt_2020\"") != NULL);
109  
110 avx_color_info_t parsed_hlg = {0};
111 bool parse_ok = avx_hdr_parse_color_info(xml_buf, &parsed_hlg);
112 assert(parse_ok);
113 assert(parsed_hlg.transfer == AVX_COLOR_TRANSFER_BT2100_HLG);
114 assert(parsed_hlg.matrix == AVX_COLOR_MATRIX_BT2020);
115 assert(parsed_hlg.primaries == AVX_COLOR_PRIMARIES_BT2020);
116 assert(avx_hdr_is_hdr(&parsed_hlg));
117  
118 /* Test B: PQ with mastering display metadata (MaxCLL 1000 nits, MaxFALL 400 nits) */
119 avx_color_info_t pq_info = {
120 .transfer = AVX_COLOR_TRANSFER_BT2100_PQ,
121 .matrix = AVX_COLOR_MATRIX_BT2020,
122 .primaries = AVX_COLOR_PRIMARIES_BT2020,
123 .max_cll = 1000,
124 .max_fall = 400
125 };
126 fmt_ok = avx_hdr_format_color_info(&pq_info, xml_buf, sizeof(xml_buf));
127 assert(fmt_ok);
128 printf(" [+] Formatted PQ XML: '%s'\n", xml_buf);
129 assert(strstr(xml_buf, "transfer=\"bt_2100_pq\"") != NULL);
130 assert(strstr(xml_buf, "max_cll=\"1000\"") != NULL);
131 assert(strstr(xml_buf, "max_fall=\"400\"") != NULL);
132  
133 avx_color_info_t parsed_pq = {0};
134 parse_ok = avx_hdr_parse_color_info(xml_buf, &parsed_pq);
135 assert(parse_ok);
136 assert(parsed_pq.transfer == AVX_COLOR_TRANSFER_BT2100_PQ);
137 assert(parsed_pq.max_cll == 1000);
138 assert(parsed_pq.max_fall == 400);
139 assert(avx_hdr_is_hdr(&parsed_pq));
140  
141 /* Test C: Frame helper methods */
142 oavx_video_frame_t test_vframe = {0};
143 char frame_xml[256];
144 bool set_ok = avx_hdr_frame_set_color_info(&test_vframe, &hlg_info, frame_xml, sizeof(frame_xml));
145 assert(set_ok);
146 assert(test_vframe.metadata == frame_xml);
147  
148 avx_color_info_t query_info = {0};
149 bool get_ok = avx_hdr_frame_get_color_info(&test_vframe, &query_info);
150 assert(get_ok);
151 assert(query_info.transfer == AVX_COLOR_TRANSFER_BT2100_HLG);
152 printf(" [+] Video frame HDR metadata setters & getters: PASSED\n");
153  
154 /* --------------------------------------------------------------------- */
155 /* 3. Test P216 16-bit Color Conversions */
156 /* --------------------------------------------------------------------- */
157 printf("[*] 3. Testing 16-bit P216 Semi-Planar Conversions...\n");
158  
159 size_t uyvy_size = (size_t)TEST_WIDTH * TEST_HEIGHT * 2;
160 size_t p216_size = (size_t)TEST_WIDTH * TEST_HEIGHT * 4;
161 size_t rgba_size = (size_t)TEST_WIDTH * TEST_HEIGHT * 4;
162  
163 uint8_t* orig_uyvy = (uint8_t*)malloc(uyvy_size);
164 uint8_t* p216_buf = (uint8_t*)malloc(p216_size);
165 uint8_t* back_uyvy = (uint8_t*)malloc(uyvy_size);
166 uint8_t* rgba_buf = (uint8_t*)malloc(rgba_size);
167 assert(orig_uyvy && p216_buf && back_uyvy && rgba_buf);
168  
169 /* Fill original UYVY gradient */
170 for (size_t i = 0; i < uyvy_size; ++i) {
171 orig_uyvy[i] = (uint8_t)(i & 0xFF);
172 }
173  
174 /* Convert UYVY -> P216 */
175 avx_color_uyvy_to_p216(orig_uyvy, p216_buf, TEST_WIDTH, TEST_HEIGHT);
176  
177 /* Verify P216 structure */
178 const uint16_t* py = (const uint16_t*)p216_buf;
179 const uint16_t* puv = (const uint16_t*)(p216_buf + (size_t)TEST_WIDTH * TEST_HEIGHT * 2);
180 /* In orig_uyvy: byte 0 = U, byte 1 = Y0, byte 2 = V, byte 3 = Y1 */
181 uint16_t expected_y0 = (uint16_t)((orig_uyvy[1] << 8) | orig_uyvy[1]);
182 uint16_t expected_u0 = (uint16_t)((orig_uyvy[0] << 8) | orig_uyvy[0]);
183 assert(py[0] == expected_y0);
184 assert(puv[0] == expected_u0);
185  
186 /* Convert P216 back to UYVY */
187 avx_color_p216_to_uyvy(p216_buf, back_uyvy, TEST_WIDTH, TEST_HEIGHT);
188  
189 /* Assert 100% roundtrip bitwise parity */
190 assert(memcmp(orig_uyvy, back_uyvy, uyvy_size) == 0);
191 printf(" [+] UYVY <-> P216 Roundtrip Bitwise Parity: 100%% EXACT\n");
192  
193 /* Convert P216 -> RGBA */
194 avx_color_p216_to_rgba(p216_buf, rgba_buf, TEST_WIDTH, TEST_HEIGHT);
195 /* Verify alpha channel is 255 */
196 for (size_t i = 0; i < (size_t)TEST_WIDTH * TEST_HEIGHT; ++i) {
197 assert(rgba_buf[4 * i + 3] == 255);
198 }
199 printf(" [+] P216 -> RGBA Conversion: PASSED\n");
200  
201 /* --------------------------------------------------------------------- */
202 /* 4. End-to-End P216 & HDR Network Loopback Streaming */
203 /* --------------------------------------------------------------------- */
204 printf("[*] 4. Testing End-to-End P216 & HDR Stream (Sender -> Receiver)...\n");
205  
206 oavx_context_t *ctx = oavx_context_create();
207 assert(ctx != NULL);
208  
209 oavx_stream_config_t send_create = {
210 .name = "OPENAVX_HDR_SOURCE",
211 .groups = "public",
212 .direction = OAVX_DIR_TRANSMIT,
213 .clock_sync = true
214 };
215 oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create);
216 assert(p_send != NULL);
217  
218 sender_thread_arg_t s_arg = {
219 .p_send = p_send,
220 .running = true,
221 .p216_data = p216_buf,
222 .hdr_metadata = xml_buf
223 };
224 avx_thread_t s_thread;
225 avx_thread_create(&s_thread, sender_thread_func, &s_arg);
226  
227 avx_sleep_ms(50);
228  
229 /* Mode A: Native P216 ingestion (compressed_v1 / raw) */
230 printf(" [*] Connecting Receiver in Native 16-bit P216 Mode...\n");
231 oavx_stream_config_t recv_create = {
232 .name = "P216_Client_Native",
233 .endpoint = "127.0.0.1:7960",
234 .direction = OAVX_DIR_RECEIVE,
235 .color_format = OAVX_COLOR_FORMAT_COMPRESSED_V1,
236 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
237 .clock_sync = false
238 };
239 oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create);
240 assert(p_recv != NULL);
241  
242 oavx_video_frame_t vf = {0};
243 oavx_audio_frame_t af = {0};
244 oavx_metadata_frame_t mf = {0};
245  
246 bool got_p216 = false;
247 int attempts = 0;
248 while (attempts++ < 40 && !got_p216) {
249 oavx_frame_type_e ftype = oavx_stream_receive(p_recv, &vf, &af, &mf, 200);
250 if (ftype == OAVX_FRAME_TYPE_VIDEO) {
251 got_p216 = true;
252 printf(" [+] Captured P216 Video Frame:\n");
253 printf(" - Resolution: %ux%u\n", vf.width, vf.height);
254 printf(" - FourCC: 0x%08X (P216: 0x%08X)\n", vf.fourcc, OAVX_FOURCC_VIDEO_TYPE_P216);
255 printf(" - Metadata: '%s'\n", vf.metadata ? vf.metadata : "(none)");
256  
257 assert(vf.width == TEST_WIDTH);
258 assert(vf.height == TEST_HEIGHT);
259 assert(vf.fourcc == OAVX_FOURCC_VIDEO_TYPE_P216);
260 assert(vf.metadata != NULL);
261  
262 avx_color_info_t rx_info = {0};
263 bool parsed_rx = avx_hdr_frame_get_color_info(&vf, &rx_info);
264 assert(parsed_rx);
265 assert(rx_info.transfer == AVX_COLOR_TRANSFER_BT2100_PQ);
266 assert(rx_info.max_cll == 1000);
267 printf(" [+] Frame-attached HDR metadata successfully recovered!\n");
268  
269 oavx_stream_release_video(p_recv, &vf);
270 }
271 }
272 assert(got_p216);
273 oavx_stream_close(p_recv);
274  
275 /* Mode B: Auto-downconversion of P216 to standard UYVY for legacy SDR displays */
276 printf(" [*] Connecting Receiver in Auto-Conversion Mode (P216 -> UYVY)...\n");
277 recv_create.color_format = OAVX_COLOR_FORMAT_UYVY_RGBA;
278 recv_create.name = "P216_Client_Converted";
279 p_recv = oavx_stream_open(ctx, &recv_create);
280 assert(p_recv != NULL);
281  
282 bool got_uyvy = false;
283 attempts = 0;
284 while (attempts++ < 40 && !got_uyvy) {
285 oavx_frame_type_e ftype = oavx_stream_receive(p_recv, &vf, &af, &mf, 200);
286 if (ftype == OAVX_FRAME_TYPE_VIDEO) {
287 got_uyvy = true;
288 printf(" [+] Captured Auto-Converted UYVY Frame:\n");
289 printf(" - FourCC: 0x%08X (UYVY: 0x%08X)\n", vf.fourcc, OAVX_FOURCC_VIDEO_TYPE_UYVY);
290 printf(" - Stride: %zu bytes\n", vf.strides[0]);
291 assert(vf.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY);
292 assert(vf.strides[0] == (size_t)TEST_WIDTH * 2);
293  
294 oavx_stream_release_video(p_recv, &vf);
295 }
296 }
297 assert(got_uyvy);
298  
299 /* Teardown */
300 printf("[*] 5. Tearing down test session...\n");
301 s_arg.running = false;
302 avx_thread_join(s_thread);
303 oavx_stream_close(p_recv);
304 oavx_stream_close(p_send);
305 oavx_context_destroy(ctx);
306  
307 free(orig_uyvy);
308 free(p216_buf);
309 free(back_uyvy);
310 free(rgba_buf);
311  
312 printf("\n[PASS] openavx_hdr_test\n\n");
313 return 0;
314 }