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
6 * test/openavx_speedhq_alpha_test.c - SpeedHQ Alpha Transparency & Interoperability Test
7 */
8  
9 #define _GNU_SOURCE
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <stdbool.h>
15 #include <assert.h>
16 #include <time.h>
17 #include <unistd.h>
18  
19 #include "openavx.h"
20 #include "avx_platform.h"
21 #include "avx_speedhq.h"
22 #include "avx_color.h"
23  
24 static double get_time_ms(void)
25 {
26 struct timespec ts;
27 clock_gettime(CLOCK_MONOTONIC, &ts);
28 return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1000000.0;
29 }
30  
31 static void generate_test_graphics_uyva(int width, int height, uint8_t *uyvy, uint8_t *alpha)
32 {
33 for (int y = 0; y < height; ++y) {
34 for (int x = 0; x < width; ++x) {
35 /* Background gradient video (UYVY) */
36 uyvy[(y * width + x) * 2 + 0] = (uint8_t)((x * 255) / width);
37 uyvy[(y * width + x) * 2 + 1] = (uint8_t)((y * 255) / height);
38  
39 /* Overlay graphics with transparency (Lower-third banner and transparent background) */
40 if (y < height / 2) {
41 /* Top half: fully transparent overlay (alpha = 0) */
42 alpha[y * width + x] = 0;
43 } else if (y < (height * 3) / 4) {
44 /* Lower-third semi-transparent backdrop (alpha = 180) */
45 alpha[y * width + x] = 180;
46 } else {
47 /* Solid opaque lower badge (alpha up to 255) with horizontal ramp */
48 alpha[y * width + x] = (uint8_t)(128 + (x * 127) / (width - 1));
49 }
50 }
51 }
52 }
53  
54 int main(void)
55 {
56 printf("\n=== OpenAVX SpeedHQ Alpha Channel (SHQ3/UYVA) Verification ===\n");
57  
58 const int width = 1920;
59 const int height = 1080;
60 const size_t num_pixels = (size_t)width * height;
61 const size_t uyvy_size = num_pixels * 2;
62 const size_t alpha_size = num_pixels;
63  
64 uint8_t *src_uyvy = (uint8_t*)malloc(uyvy_size);
65 uint8_t *src_alpha = (uint8_t*)malloc(alpha_size);
66 assert(src_uyvy && src_alpha);
67  
68 generate_test_graphics_uyva(width, height, src_uyvy, src_alpha);
69  
70 /* 1. Pure C99 SpeedHQ-3 Standalone Codec Test */
71 printf("[*] 1. Testing C99 SpeedHQ-3 Standalone Alpha Compression (1080p)...\n");
72 size_t max_shq_size = uyvy_size + alpha_size;
73 uint8_t *shq_buf = (uint8_t*)malloc(max_shq_size);
74 assert(shq_buf);
75  
76 size_t c99_shq_size = 0;
77 double t0 = get_time_ms();
78 int ret = avx_speedhq_encode_c99_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ3,
79 width, height,
80 src_uyvy, src_alpha,
81 shq_buf, max_shq_size, &c99_shq_size);
82 double t1 = get_time_ms();
83 assert(ret == 0);
84 assert(c99_shq_size > 64);
85 printf(" [+] C99 Encoded 1080p SHQ3 frame: %zu bytes (ratio: %.1f:1) in %.2f ms\n",
86 c99_shq_size, (double)(uyvy_size + alpha_size) / (double)c99_shq_size, t1 - t0);
87  
88 /* Decompress with C99 */
89 uint8_t *dst_uyvy = (uint8_t*)malloc(uyvy_size);
90 uint8_t *dst_alpha = (uint8_t*)malloc(alpha_size);
91 assert(dst_uyvy && dst_alpha);
92 bool has_alpha = false;
93  
94 double t2 = get_time_ms();
95 ret = avx_speedhq_decode_c99_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ3,
96 width, height,
97 shq_buf, c99_shq_size,
98 dst_uyvy, dst_alpha, &has_alpha);
99 double t3 = get_time_ms();
100 assert(ret == 0);
101 assert(has_alpha);
102 printf(" [+] C99 Decoded 1080p SHQ3 frame in %.2f ms (has_alpha: true)\n", t3 - t2);
103  
104 /* Verify lossless alpha restoration */
105 size_t alpha_errors = 0;
106 for (size_t i = 0; i < num_pixels; ++i) {
107 if (src_alpha[i] != dst_alpha[i]) {
108 alpha_errors++;
109 }
110 }
111 printf(" [+] C99 alpha check: %zu mismatches / %zu pixels\n",
112 alpha_errors, num_pixels);
113 assert(alpha_errors == 0);
114  
115 /* 2. Standard SpeedHQ-3 Encode/Decode Path */
116 printf("[*] 2. Testing avx_speedhq_encode_ex/decode_ex (SHQ3, 1080p)...\n");
117 size_t ex_shq_size = 0;
118 ret = avx_speedhq_encode_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ3,
119 width, height,
120 src_uyvy, src_alpha,
121 shq_buf, max_shq_size, &ex_shq_size);
122 assert(ret == 0 && ex_shq_size > 0);
123  
124 memset(dst_alpha, 0, alpha_size);
125 has_alpha = false;
126 ret = avx_speedhq_decode_ex(OAVX_FOURCC_VIDEO_TYPE_SHQ3,
127 width, height,
128 shq_buf, ex_shq_size,
129 dst_uyvy, dst_alpha, &has_alpha);
130 assert(ret == 0);
131 assert(has_alpha);
132  
133 alpha_errors = 0;
134 for (size_t i = 0; i < num_pixels; ++i) {
135 if (src_alpha[i] != dst_alpha[i]) alpha_errors++;
136 }
137 printf(" [+] SHQ3 alpha check: %zu mismatches\n", alpha_errors);
138 assert(alpha_errors == 0);
139  
140 /* 3. End-to-End OpenAVX Network Loopback with Transparency */
141 printf("[*] 3. Testing End-to-End OpenAVX Stream Loopback with Alpha Channel...\n");
142 oavx_context_t *ctx = oavx_context_create();
143 assert(ctx != NULL);
144  
145 oavx_stream_config_t tx_cfg = {
146 .name = "OpenAVX SpeedHQ-3 Alpha Test",
147 .direction = OAVX_DIR_TRANSMIT,
148 .clock_sync = false
149 };
150 oavx_stream_t *p_send = oavx_stream_open(ctx, &tx_cfg);
151 assert(p_send != NULL);
152  
153 oavx_stream_config_t rx_cfg = {
154 .name = "SpeedHQ Alpha Receiver",
155 .endpoint = "127.0.0.1:7960",
156 .direction = OAVX_DIR_RECEIVE,
157 .color_format = OAVX_COLOR_FORMAT_RGBX_RGBA,
158 .bandwidth = OAVX_BANDWIDTH_HIGHEST,
159 .clock_sync = false
160 };
161 oavx_stream_t *p_recv = oavx_stream_open(ctx, &rx_cfg);
162 assert(p_recv != NULL);
163  
164 /* Connect receiver */
165 oavx_endpoint_t src = {
166 .name = "SpeedHQ-3 Alpha Source",
167 .address = "127.0.0.1:7960"
168 };
169 oavx_stream_connect(p_recv, &src);
170  
171 /* Build packed UYVA payload for transmission */
172 uint8_t *uyva_payload = (uint8_t*)malloc(uyvy_size + alpha_size);
173 assert(uyva_payload);
174 memcpy(uyva_payload, src_uyvy, uyvy_size);
175 memcpy(uyva_payload + uyvy_size, src_alpha, alpha_size);
176  
177 oavx_video_frame_t tx_frame = {
178 .width = width,
179 .height = height,
180 .fourcc = OAVX_FOURCC_VIDEO_TYPE_SHQ3,
181 .scan_type = OAVX_SCAN_PROGRESSIVE,
182 .planes = { uyva_payload, NULL, NULL, NULL },
183 .strides = { (size_t)width * 2, 0, 0, 0 },
184 .framerate = { 60000, 1000 },
185 .aspect_ratio = { 16, 9 },
186 .pts_ns = 123456789LL,
187 .dts_ns = 0,
188 .metadata = NULL,
189 .metadata_len = 0,
190 .user_ctx = NULL
191 };
192  
193 /* Send compressed SpeedHQ-3 frame and wait for capture */
194 oavx_video_frame_t rx_frame;
195 memset(&rx_frame, 0, sizeof(rx_frame));
196 oavx_frame_type_e frame_type = OAVX_FRAME_TYPE_NONE;
197 for (int attempt = 0; attempt < 50; attempt++) {
198 oavx_stream_send_video(p_send, &tx_frame);
199 frame_type = oavx_stream_receive(p_recv, &rx_frame, NULL, NULL, 100);
200 if (frame_type == OAVX_FRAME_TYPE_VIDEO) break;
201 avx_sleep_ms(20);
202 }
203 assert(frame_type == OAVX_FRAME_TYPE_VIDEO);
204 assert(rx_frame.planes[0] != NULL);
205 assert(rx_frame.width == (uint32_t)width && rx_frame.height == (uint32_t)height);
206 printf(" [+] Received video frame: %ux%u, FourCC: 0x%08X (RGBA expected: 0x%08X)\n",
207 rx_frame.width, rx_frame.height, rx_frame.fourcc, OAVX_FOURCC_VIDEO_TYPE_RGBA);
208 assert(rx_frame.fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA);
209  
210 /* Sample transparency from top (transparent) and bottom (opaque) */
211 uint8_t rx_top_alpha = rx_frame.planes[0][3]; /* (0,0) pixel alpha */
212 uint8_t rx_bot_alpha = rx_frame.planes[0][(num_pixels - 1) * 4 + 3]; /* last pixel alpha */
213 printf(" [+] Preserved Alpha: Top-Left=%u (expected %u), Bottom-Right=%u (expected %u)\n",
214 rx_top_alpha, src_alpha[0], rx_bot_alpha, src_alpha[num_pixels - 1]);
215 assert(rx_top_alpha == src_alpha[0]);
216 assert(rx_bot_alpha == src_alpha[num_pixels - 1]);
217  
218 oavx_stream_release_video(p_recv, &rx_frame);
219 oavx_stream_close(p_recv);
220 oavx_stream_close(p_send);
221 oavx_context_destroy(ctx);
222  
223 free(src_uyvy);
224 free(src_alpha);
225 free(dst_uyvy);
226 free(dst_alpha);
227 free(shq_buf);
228 free(uyva_payload);
229  
230 printf("\n[PASS] openavx_speedhq_alpha_test\n\n");
231 return 0;
232 }