+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 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_alpha_color_test.c - 4:4:4:4 Alpha Transparency & RGB Color Formats Test |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <assert.h> |
| 14 | #include <unistd.h> |
| 15 | #include <math.h> |
| 16 | |
| 17 | #include "openavx.h" |
| 18 | #include "avx_color.h" |
| 19 | |
| 20 | static void test_color_math(void) { |
| 21 | printf("[*] 1. Testing Color Conversion Math & Roundtrips...\n"); |
| 22 | |
| 23 | /* 8 pixels (pairs for 4:2:2 chroma subsampling): Red x2, Green x2, Blue x2, White x2 */ |
| 24 | uint8_t rgba_in[32] = { |
| 25 | 255, 0, 0, 255, 255, 0, 0, 255, /* Red pair */ |
| 26 | 0, 255, 0, 255, 0, 255, 0, 255, /* Green pair */ |
| 27 | 0, 0, 255, 255, 0, 0, 255, 255, /* Blue pair */ |
| 28 | 255, 255, 255, 255, 255, 255, 255, 255 /* White pair */ |
| 29 | }; |
| 30 | uint8_t uyvy[16]; |
| 31 | uint8_t rgba_out[32]; |
| 32 | |
| 33 | avx_color_rgba_to_uyvy(rgba_in, uyvy, 8, 1); |
| 34 | avx_color_uyvy_to_rgba(uyvy, rgba_out, 8, 1); |
| 35 | |
| 36 | for (int i = 0; i < 8; ++i) { |
| 37 | int r_diff = abs((int)rgba_in[i * 4 + 0] - (int)rgba_out[i * 4 + 0]); |
| 38 | int g_diff = abs((int)rgba_in[i * 4 + 1] - (int)rgba_out[i * 4 + 1]); |
| 39 | int b_diff = abs((int)rgba_in[i * 4 + 2] - (int)rgba_out[i * 4 + 2]); |
| 40 | assert(rgba_out[i * 4 + 3] == 255); |
| 41 | /* Allow small integer rounding tolerance <= 2 */ |
| 42 | assert(r_diff <= 2); |
| 43 | assert(g_diff <= 2); |
| 44 | assert(b_diff <= 2); |
| 45 | } |
| 46 | printf(" -> RGBA <-> UYVY conversion roundtrip verified (tolerances <= 2).\n"); |
| 47 | |
| 48 | /* Test channel swap RGBA <-> BGRA */ |
| 49 | uint8_t bgra[32]; |
| 50 | uint8_t rgba_restored[32]; |
| 51 | avx_color_rgba_to_bgra(rgba_in, bgra, 8, 1); |
| 52 | |
| 53 | /* Red (255,0,0,255) in RGBA becomes (0,0,255,255) in BGRA */ |
| 54 | assert(bgra[0] == 0 && bgra[1] == 0 && bgra[2] == 255 && bgra[3] == 255); |
| 55 | /* Blue (0,0,255,255) in RGBA becomes (255,0,0,255) in BGRA */ |
| 56 | assert(bgra[16] == 255 && bgra[17] == 0 && bgra[18] == 0 && bgra[19] == 255); |
| 57 | |
| 58 | avx_color_rgba_to_bgra(bgra, rgba_restored, 8, 1); |
| 59 | assert(memcmp(rgba_in, rgba_restored, sizeof(rgba_in)) == 0); |
| 60 | printf(" -> RGBA <-> BGRA channel swap symmetry verified.\n"); |
| 61 | } |
| 62 | |
| 63 | static void test_alpha_compositing(void) { |
| 64 | printf("[*] 2. Testing Alpha Compositing & Transparency Engine...\n"); |
| 65 | |
| 66 | int w = 64, h = 64; |
| 67 | size_t bytes = (size_t)w * h * 4; |
| 68 | uint8_t *fg = (uint8_t*)malloc(bytes); |
| 69 | uint8_t *bg = (uint8_t*)malloc(bytes); |
| 70 | uint8_t *out = (uint8_t*)malloc(bytes); |
| 71 | assert(fg && bg && out); |
| 72 | |
| 73 | /* Background: 100% opaque Solid Blue (0, 0, 255, 255) */ |
| 74 | for (size_t i = 0; i < (size_t)w * h; ++i) { |
| 75 | bg[i * 4 + 0] = 0; |
| 76 | bg[i * 4 + 1] = 0; |
| 77 | bg[i * 4 + 2] = 255; |
| 78 | bg[i * 4 + 3] = 255; |
| 79 | } |
| 80 | |
| 81 | /* Foreground: |
| 82 | * Top half: 100% transparent (0, 255, 0, 0) |
| 83 | * Middle: 50% semi-transparent Red (255, 0, 0, 128) |
| 84 | * Bottom: 100% opaque Green (0, 255, 0, 255) |
| 85 | */ |
| 86 | for (int y = 0; y < h; ++y) { |
| 87 | for (int x = 0; x < w; ++x) { |
| 88 | size_t idx = ((size_t)y * w + x) * 4; |
| 89 | if (y < 20) { |
| 90 | fg[idx + 0] = 0; |
| 91 | fg[idx + 1] = 255; |
| 92 | fg[idx + 2] = 0; |
| 93 | fg[idx + 3] = 0; /* 0% alpha */ |
| 94 | } else if (y < 40) { |
| 95 | fg[idx + 0] = 255; |
| 96 | fg[idx + 1] = 0; |
| 97 | fg[idx + 2] = 0; |
| 98 | fg[idx + 3] = 128; /* 50% alpha */ |
| 99 | } else { |
| 100 | fg[idx + 0] = 0; |
| 101 | fg[idx + 1] = 255; |
| 102 | fg[idx + 2] = 0; |
| 103 | fg[idx + 3] = 255; /* 100% alpha */ |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | avx_color_alpha_blend_rgba(fg, bg, out, w, h); |
| 109 | |
| 110 | /* Verify top half (A=0): should be pure background (Blue) */ |
| 111 | assert(out[0] == 0 && out[1] == 0 && out[2] == 255 && out[3] == 255); |
| 112 | |
| 113 | /* Verify middle (A=128): blend of Red fg and Blue bg */ |
| 114 | size_t mid_idx = (30 * w + 30) * 4; |
| 115 | int r = out[mid_idx + 0]; |
| 116 | int g = out[mid_idx + 1]; |
| 117 | int b = out[mid_idx + 2]; |
| 118 | assert(abs(r - 128) <= 2); |
| 119 | assert(g == 0); |
| 120 | assert(abs(b - 127) <= 2); |
| 121 | assert(out[mid_idx + 3] == 255); |
| 122 | |
| 123 | /* Verify bottom half (A=255): pure foreground (Green) */ |
| 124 | size_t bot_idx = (50 * w + 30) * 4; |
| 125 | assert(out[bot_idx + 0] == 0 && out[bot_idx + 1] == 255 && out[bot_idx + 2] == 0 && out[bot_idx + 3] == 255); |
| 126 | |
| 127 | printf(" -> Alpha blending (0%% pass-through, 50%% blend, 100%% overwrite) verified.\n"); |
| 128 | |
| 129 | free(fg); |
| 130 | free(bg); |
| 131 | free(out); |
| 132 | } |
| 133 | |
| 134 | static void test_live_alpha_streaming(void) { |
| 135 | printf("[*] 3. Testing Live OpenAVX Stream with RGBA Alpha Channel...\n"); |
| 136 | |
| 137 | oavx_context_t *ctx = oavx_context_create(); |
| 138 | assert(ctx != NULL); |
| 139 | |
| 140 | /* 1. Create Sender transmitting RGBA frames */ |
| 141 | oavx_stream_config_t send_create = { |
| 142 | .name = "ALPHA_GRAPHICS_OVERLAY_1", |
| 143 | .groups = "public", |
| 144 | .direction = OAVX_DIR_TRANSMIT, |
| 145 | .clock_sync = true |
| 146 | }; |
| 147 | oavx_stream_t *p_send = oavx_stream_open(ctx, &send_create); |
| 148 | assert(p_send != NULL); |
| 149 | |
| 150 | int width = 320; |
| 151 | int height = 180; |
| 152 | size_t frame_bytes = (size_t)width * height * 4; |
| 153 | uint8_t *test_rgba = (uint8_t*)malloc(frame_bytes); |
| 154 | assert(test_rgba); |
| 155 | |
| 156 | /* Upper third: fully transparent (0, 0, 0, 0) |
| 157 | * Lower two thirds: semi-transparent red banner (200, 20, 20, 180) |
| 158 | */ |
| 159 | for (int y = 0; y < height; ++y) { |
| 160 | for (int x = 0; x < width; ++x) { |
| 161 | size_t idx = ((size_t)y * width + x) * 4; |
| 162 | if (y < 60) { |
| 163 | test_rgba[idx + 0] = 0; |
| 164 | test_rgba[idx + 1] = 0; |
| 165 | test_rgba[idx + 2] = 0; |
| 166 | test_rgba[idx + 3] = 0; |
| 167 | } else { |
| 168 | test_rgba[idx + 0] = 200; |
| 169 | test_rgba[idx + 1] = 20; |
| 170 | test_rgba[idx + 2] = 20; |
| 171 | test_rgba[idx + 3] = 180; |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | oavx_video_frame_t video_frame = { |
| 177 | .width = width, |
| 178 | .height = height, |
| 179 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_RGBA, |
| 180 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 181 | .planes = { test_rgba, NULL, NULL, NULL }, |
| 182 | .strides = { (size_t)width * 4, 0, 0, 0 }, |
| 183 | .framerate = { 60, 1 }, |
| 184 | .aspect_ratio = { 16, 9 }, |
| 185 | .pts_ns = 1000, |
| 186 | .dts_ns = 0, |
| 187 | .metadata = NULL, |
| 188 | .metadata_len = 0, |
| 189 | .user_ctx = NULL |
| 190 | }; |
| 191 | |
| 192 | /* 2. Connect Receiver requesting RGBX_RGBA */ |
| 193 | oavx_stream_config_t recv_create = { |
| 194 | .name = "Alpha_Receiver_1", |
| 195 | .endpoint = "127.0.0.1:7960", |
| 196 | .direction = OAVX_DIR_RECEIVE, |
| 197 | .color_format = OAVX_COLOR_FORMAT_RGBX_RGBA, |
| 198 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 199 | .clock_sync = false |
| 200 | }; |
| 201 | oavx_stream_t *p_recv = oavx_stream_open(ctx, &recv_create); |
| 202 | assert(p_recv != NULL); |
| 203 | |
| 204 | /* Send frame */ |
| 205 | oavx_stream_send_video(p_send, &video_frame); |
| 206 | |
| 207 | /* Capture frame */ |
| 208 | oavx_video_frame_t captured_video; |
| 209 | oavx_frame_type_t frame_type = oavx_stream_receive(p_recv, &captured_video, NULL, NULL, 1000); |
| 210 | assert(frame_type == OAVX_FRAME_TYPE_VIDEO); |
| 211 | assert(captured_video.fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA); |
| 212 | assert(captured_video.width == (uint32_t)width); |
| 213 | assert(captured_video.height == (uint32_t)height); |
| 214 | assert(captured_video.planes[0] != NULL); |
| 215 | |
| 216 | /* Check alpha values are preserved across network transmission */ |
| 217 | assert(captured_video.planes[0][3] == 0); /* Top pixel alpha = 0 */ |
| 218 | size_t banner_idx = ((size_t)100 * width + 50) * 4; |
| 219 | assert(captured_video.planes[0][banner_idx + 0] == 200); |
| 220 | assert(captured_video.planes[0][banner_idx + 1] == 20); |
| 221 | assert(captured_video.planes[0][banner_idx + 2] == 20); |
| 222 | assert(captured_video.planes[0][banner_idx + 3] == 180); /* Alpha preserved! */ |
| 223 | |
| 224 | oavx_stream_release_video(p_recv, &captured_video); |
| 225 | oavx_stream_close(p_recv); |
| 226 | |
| 227 | /* 3. Connect Receiver requesting BGRX_BGRA to test channel swapping negotiation */ |
| 228 | recv_create.color_format = OAVX_COLOR_FORMAT_BGRX_BGRA; |
| 229 | recv_create.name = "BGRA_Receiver_2"; |
| 230 | p_recv = oavx_stream_open(ctx, &recv_create); |
| 231 | assert(p_recv != NULL); |
| 232 | |
| 233 | oavx_stream_send_video(p_send, &video_frame); |
| 234 | frame_type = oavx_stream_receive(p_recv, &captured_video, NULL, NULL, 1000); |
| 235 | assert(frame_type == OAVX_FRAME_TYPE_VIDEO); |
| 236 | assert(captured_video.fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA); |
| 237 | assert(captured_video.strides[0] == (size_t)width * 4); |
| 238 | |
| 239 | /* Verify swapped Red and Blue in BGRA output */ |
| 240 | assert(captured_video.planes[0][banner_idx + 0] == 20); /* Blue (swapped) */ |
| 241 | assert(captured_video.planes[0][banner_idx + 1] == 20); /* Green */ |
| 242 | assert(captured_video.planes[0][banner_idx + 2] == 200); /* Red (swapped) */ |
| 243 | assert(captured_video.planes[0][banner_idx + 3] == 180); /* Alpha */ |
| 244 | |
| 245 | oavx_stream_release_video(p_recv, &captured_video); |
| 246 | oavx_stream_close(p_recv); |
| 247 | |
| 248 | /* 4. Connect Receiver requesting UYVY_RGBA (adaptive: keep RGBA when alpha present) */ |
| 249 | recv_create.color_format = OAVX_COLOR_FORMAT_UYVY_RGBA; |
| 250 | recv_create.name = "Adaptive_Receiver_3"; |
| 251 | p_recv = oavx_stream_open(ctx, &recv_create); |
| 252 | assert(p_recv != NULL); |
| 253 | |
| 254 | oavx_stream_send_video(p_send, &video_frame); |
| 255 | frame_type = oavx_stream_receive(p_recv, &captured_video, NULL, NULL, 1000); |
| 256 | assert(frame_type == OAVX_FRAME_TYPE_VIDEO); |
| 257 | /* Since source is RGBA (has alpha), UYVY_RGBA keeps it in RGBA */ |
| 258 | assert(captured_video.fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA); |
| 259 | assert(captured_video.planes[0][banner_idx + 3] == 180); |
| 260 | |
| 261 | oavx_stream_release_video(p_recv, &captured_video); |
| 262 | oavx_stream_close(p_recv); |
| 263 | |
| 264 | free(test_rgba); |
| 265 | oavx_stream_close(p_send); |
| 266 | oavx_context_destroy(ctx); |
| 267 | |
| 268 | printf(" -> Live streaming negotiation (RGBA, BGRA, Adaptive UYVY_RGBA) verified.\n"); |
| 269 | } |
| 270 | |
| 271 | static void test_nv12_i420_conversions(void) { |
| 272 | printf("[*] 4. Testing NV12 & I420 Semi-Planar Conversions & Multi-Plane Packing...\n"); |
| 273 | |
| 274 | int w = 8, h = 4; |
| 275 | size_t y_bytes = (size_t)w * h; |
| 276 | size_t uv_bytes = ((size_t)w * h) / 2; |
| 277 | size_t u_bytes = ((size_t)w * h) / 4; |
| 278 | size_t rgba_bytes = (size_t)w * h * 4; |
| 279 | |
| 280 | uint8_t nv12_y[32]; |
| 281 | uint8_t nv12_uv[16]; |
| 282 | uint8_t i420_u[8]; |
| 283 | uint8_t i420_v[8]; |
| 284 | uint8_t uyvy_out[64]; |
| 285 | uint8_t rgba_out[128]; |
| 286 | uint8_t nv12_y_rt[32]; |
| 287 | uint8_t nv12_uv_rt[16]; |
| 288 | uint8_t i420_u_rt[8]; |
| 289 | uint8_t i420_v_rt[8]; |
| 290 | |
| 291 | for (int i = 0; i < 32; ++i) nv12_y[i] = (uint8_t)(100 + i * 4); |
| 292 | for (int i = 0; i < 8; ++i) { |
| 293 | nv12_uv[2 * i + 0] = (uint8_t)(80 + i * 10); /* U */ |
| 294 | nv12_uv[2 * i + 1] = (uint8_t)(160 + i * 10); /* V */ |
| 295 | i420_u[i] = nv12_uv[2 * i + 0]; |
| 296 | i420_v[i] = nv12_uv[2 * i + 1]; |
| 297 | } |
| 298 | |
| 299 | /* 1. NV12 -> UYVY */ |
| 300 | avx_color_nv12_to_uyvy(nv12_y, w, nv12_uv, w, uyvy_out, w * 2, w, h); |
| 301 | /* Verify pixel 0 (row 0, col 0) */ |
| 302 | assert(uyvy_out[0] == nv12_uv[0]); /* U */ |
| 303 | assert(uyvy_out[1] == nv12_y[0]); /* Y0 */ |
| 304 | assert(uyvy_out[2] == nv12_uv[1]); /* V */ |
| 305 | assert(uyvy_out[3] == nv12_y[1]); /* Y1 */ |
| 306 | |
| 307 | /* 2. UYVY -> NV12 roundtrip */ |
| 308 | avx_color_uyvy_to_nv12(uyvy_out, w * 2, nv12_y_rt, w, nv12_uv_rt, w, w, h); |
| 309 | assert(memcmp(nv12_y, nv12_y_rt, y_bytes) == 0); |
| 310 | assert(memcmp(nv12_uv, nv12_uv_rt, uv_bytes) == 0); |
| 311 | printf(" -> NV12 <-> UYVY bit-exact conversion roundtrip verified.\n"); |
| 312 | |
| 313 | /* 3. I420 -> UYVY */ |
| 314 | memset(uyvy_out, 0, sizeof(uyvy_out)); |
| 315 | avx_color_i420_to_uyvy(nv12_y, w, i420_u, w / 2, i420_v, w / 2, uyvy_out, w * 2, w, h); |
| 316 | assert(uyvy_out[0] == i420_u[0]); |
| 317 | assert(uyvy_out[1] == nv12_y[0]); |
| 318 | assert(uyvy_out[2] == i420_v[0]); |
| 319 | assert(uyvy_out[3] == nv12_y[1]); |
| 320 | |
| 321 | /* 4. UYVY -> I420 roundtrip */ |
| 322 | avx_color_uyvy_to_i420(uyvy_out, w * 2, nv12_y_rt, w, i420_u_rt, w / 2, i420_v_rt, w / 2, w, h); |
| 323 | assert(memcmp(nv12_y, nv12_y_rt, y_bytes) == 0); |
| 324 | assert(memcmp(i420_u, i420_u_rt, u_bytes) == 0); |
| 325 | assert(memcmp(i420_v, i420_v_rt, u_bytes) == 0); |
| 326 | printf(" -> I420 <-> UYVY bit-exact conversion roundtrip verified.\n"); |
| 327 | |
| 328 | /* 5. NV12 -> RGBA & BGRA & I420 -> RGBA direct conversions */ |
| 329 | avx_color_nv12_to_rgba(nv12_y, w, nv12_uv, w, rgba_out, w * 4, w, h); |
| 330 | assert(rgba_out[3] == 255); /* Alpha channel opaque */ |
| 331 | |
| 332 | uint8_t bgra_out[256]; /* Extra stride capacity */ |
| 333 | memset(bgra_out, 0xA5, sizeof(bgra_out)); |
| 334 | size_t padded_bgra_stride = (size_t)w * 4 + 16; |
| 335 | avx_color_nv12_to_bgra(nv12_y, w, nv12_uv, w, bgra_out, padded_bgra_stride, w, h); |
| 336 | for (int py = 0; py < h; ++py) { |
| 337 | for (int px = 0; px < w; ++px) { |
| 338 | const uint8_t *rp = rgba_out + ((size_t)py * w + px) * 4; |
| 339 | const uint8_t *bp = bgra_out + (size_t)py * padded_bgra_stride + (size_t)px * 4; |
| 340 | assert(bp[0] == rp[2]); |
| 341 | assert(bp[1] == rp[1]); |
| 342 | assert(bp[2] == rp[0]); |
| 343 | assert(bp[3] == 255); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | uint8_t rgba_out_i420[128]; |
| 348 | avx_color_i420_to_rgba(nv12_y, w, i420_u, w / 2, i420_v, w / 2, rgba_out_i420, w * 4, w, h); |
| 349 | assert(memcmp(rgba_out, rgba_out_i420, rgba_bytes) == 0); |
| 350 | printf(" -> NV12 (RGBA + single-pass strided BGRA) & I420 direct color conversions verified.\n"); |
| 351 | |
| 352 | /* 6. YV12 (Y + V + U) conversions */ |
| 353 | uint8_t yv12_v_rt[8], yv12_u_rt[8]; |
| 354 | avx_color_uyvy_to_yv12(uyvy_out, w * 2, nv12_y_rt, w, yv12_v_rt, w / 2, yv12_u_rt, w / 2, w, h); |
| 355 | assert(memcmp(yv12_v_rt, i420_v, u_bytes) == 0); |
| 356 | assert(memcmp(yv12_u_rt, i420_u, u_bytes) == 0); |
| 357 | |
| 358 | uint8_t uyvy_yv12[64]; |
| 359 | avx_color_yv12_to_uyvy(nv12_y, w, i420_v, w / 2, i420_u, w / 2, uyvy_yv12, w * 2, w, h); |
| 360 | assert(memcmp(uyvy_out, uyvy_yv12, sizeof(uyvy_out)) == 0); |
| 361 | |
| 362 | uint8_t rgba_out_yv12[128]; |
| 363 | avx_color_yv12_to_rgba(nv12_y, w, i420_v, w / 2, i420_u, w / 2, rgba_out_yv12, w * 4, w, h); |
| 364 | assert(memcmp(rgba_out, rgba_out_yv12, rgba_bytes) == 0); |
| 365 | printf(" -> YV12 (swapped chroma) UYVY and RGBA conversions verified.\n"); |
| 366 | |
| 367 | /* 7. Direct RGBA -> NV12, RGBA -> I420, RGBA -> YV12 */ |
| 368 | uint8_t src_rgba[128]; |
| 369 | /* Initialize 2x2 pixel blocks (quads matching 4:2:0 chroma subsampling blocks) */ |
| 370 | for (int y2 = 0; y2 < h / 2; ++y2) { |
| 371 | for (int x2 = 0; x2 < w / 2; ++x2) { |
| 372 | int block = y2 * (w / 2) + x2; |
| 373 | uint8_t r = (uint8_t)(60 + block * 15); |
| 374 | uint8_t g = (uint8_t)(80 + block * 10); |
| 375 | uint8_t b = (uint8_t)(50 + block * 12); |
| 376 | for (int dy = 0; dy < 2; ++dy) { |
| 377 | for (int dx = 0; dx < 2; ++dx) { |
| 378 | int px = x2 * 2 + dx; |
| 379 | int py = y2 * 2 + dy; |
| 380 | int idx = py * w + px; |
| 381 | src_rgba[4 * idx + 0] = r; |
| 382 | src_rgba[4 * idx + 1] = g; |
| 383 | src_rgba[4 * idx + 2] = b; |
| 384 | src_rgba[4 * idx + 3] = 255; |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | uint8_t direct_nv12_y[32], direct_nv12_uv[16]; |
| 391 | avx_color_rgba_to_nv12(src_rgba, w * 4, direct_nv12_y, w, direct_nv12_uv, w, w, h); |
| 392 | |
| 393 | /* Verify roundtrip from NV12 back to RGBA */ |
| 394 | uint8_t rt_rgba[128]; |
| 395 | avx_color_nv12_to_rgba(direct_nv12_y, w, direct_nv12_uv, w, rt_rgba, w * 4, w, h); |
| 396 | for (int i = 0; i < 32; ++i) { |
| 397 | assert(abs((int)src_rgba[4 * i + 0] - (int)rt_rgba[4 * i + 0]) <= 2); |
| 398 | assert(abs((int)src_rgba[4 * i + 1] - (int)rt_rgba[4 * i + 1]) <= 2); |
| 399 | assert(abs((int)src_rgba[4 * i + 2] - (int)rt_rgba[4 * i + 2]) <= 2); |
| 400 | assert(rt_rgba[4 * i + 3] == 255); |
| 401 | } |
| 402 | |
| 403 | uint8_t direct_i420_y[32], direct_i420_u[8], direct_i420_v[8]; |
| 404 | avx_color_rgba_to_i420(src_rgba, w * 4, direct_i420_y, w, direct_i420_u, w / 2, direct_i420_v, w / 2, w, h); |
| 405 | assert(memcmp(direct_nv12_y, direct_i420_y, y_bytes) == 0); |
| 406 | for (int i = 0; i < 8; ++i) { |
| 407 | assert(direct_i420_u[i] == direct_nv12_uv[2 * i + 0]); |
| 408 | assert(direct_i420_v[i] == direct_nv12_uv[2 * i + 1]); |
| 409 | } |
| 410 | |
| 411 | uint8_t direct_yv12_y[32], direct_yv12_v[8], direct_yv12_u[8]; |
| 412 | avx_color_rgba_to_yv12(src_rgba, w * 4, direct_yv12_y, w, direct_yv12_v, w / 2, direct_yv12_u, w / 2, w, h); |
| 413 | assert(memcmp(direct_i420_y, direct_yv12_y, y_bytes) == 0); |
| 414 | assert(memcmp(direct_i420_u, direct_yv12_u, u_bytes) == 0); |
| 415 | assert(memcmp(direct_i420_v, direct_yv12_v, u_bytes) == 0); |
| 416 | printf(" -> Direct RGBA -> NV12, I420, and YV12 conversions verified (error <= 2).\n"); |
| 417 | } |
| 418 | |
| 419 | static void test_live_multiplane_separate_buffers(void) { |
| 420 | printf("[*] 5. Testing Multi-Plane Separate Buffer Streaming (UYVA & NV12 Padded Strides)...\n"); |
| 421 | |
| 422 | oavx_context_t *ctx = oavx_context_create(); |
| 423 | assert(ctx != NULL); |
| 424 | |
| 425 | oavx_stream_config_t tx_cfg = { |
| 426 | .name = "MULTIPLANAR_SENDER", |
| 427 | .groups = "public", |
| 428 | .direction = OAVX_DIR_TRANSMIT, |
| 429 | .clock_sync = false |
| 430 | }; |
| 431 | oavx_stream_t *tx = oavx_stream_open(ctx, &tx_cfg); |
| 432 | assert(tx != NULL); |
| 433 | |
| 434 | const oavx_endpoint_t *ep = oavx_stream_get_endpoint(tx); |
| 435 | assert(ep && ep->address); |
| 436 | |
| 437 | oavx_stream_config_t rx_cfg = { |
| 438 | .name = "MULTIPLANAR_RECEIVER", |
| 439 | .endpoint = ep->address, |
| 440 | .direction = OAVX_DIR_RECEIVE, |
| 441 | .color_format = OAVX_COLOR_FORMAT_FASTEST, |
| 442 | .bandwidth = OAVX_BANDWIDTH_HIGHEST, |
| 443 | .clock_sync = false |
| 444 | }; |
| 445 | oavx_stream_t *rx = oavx_stream_open(ctx, &rx_cfg); |
| 446 | assert(rx != NULL); |
| 447 | |
| 448 | usleep(50000); |
| 449 | |
| 450 | /* 1. Transmit UYVA frame with completely separate malloc allocations & padded stride */ |
| 451 | uint32_t w = 160; |
| 452 | uint32_t h = 90; |
| 453 | size_t padded_stride_yuv = (size_t)w * 2 + 64; /* Padded stride */ |
| 454 | size_t padded_stride_alpha = (size_t)w + 32; |
| 455 | |
| 456 | uint8_t *uyvy_plane = (uint8_t*)malloc(padded_stride_yuv * h); |
| 457 | uint8_t *alpha_plane = (uint8_t*)malloc(padded_stride_alpha * h); |
| 458 | assert(uyvy_plane && alpha_plane); |
| 459 | |
| 460 | /* Fill pattern */ |
| 461 | for (uint32_t y = 0; y < h; ++y) { |
| 462 | memset(uyvy_plane + y * padded_stride_yuv, (int)(y & 0xFF), (size_t)w * 2); |
| 463 | memset(alpha_plane + y * padded_stride_alpha, 210, (size_t)w); |
| 464 | } |
| 465 | |
| 466 | oavx_video_frame_t tx_uyva = { |
| 467 | .width = w, |
| 468 | .height = h, |
| 469 | .fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVA, |
| 470 | .scan_type = OAVX_SCAN_PROGRESSIVE, |
| 471 | .planes = { uyvy_plane, alpha_plane, NULL, NULL }, |
| 472 | .strides = { padded_stride_yuv, padded_stride_alpha, 0, 0 }, |
| 473 | .framerate = { 60, 1 }, |
| 474 | .aspect_ratio = { 16, 9 }, |
| 475 | .pts_ns = 5000000 |
| 476 | }; |
| 477 | |
| 478 | assert(oavx_stream_send_video(tx, &tx_uyva) == OAVX_SUCCESS); |
| 479 | |
| 480 | oavx_video_frame_t rx_v = {0}; |
| 481 | oavx_frame_type_t ft = oavx_stream_receive(rx, &rx_v, NULL, NULL, 500); |
| 482 | assert(ft == OAVX_FRAME_TYPE_VIDEO); |
| 483 | assert(rx_v.width == w && rx_v.height == h); |
| 484 | assert(rx_v.fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVA); |
| 485 | assert(rx_v.planes[0] != NULL); |
| 486 | assert(rx_v.planes[1] != NULL); |
| 487 | assert(rx_v.planes[1] == rx_v.planes[0] + (size_t)w * h * 2); |
| 488 | /* Verify reconstructed alpha */ |
| 489 | assert(rx_v.planes[1][0] == 210); |
| 490 | assert(rx_v.planes[1][w * h - 1] == 210); |
| 491 | oavx_stream_release_video(rx, &rx_v); |
| 492 | |
| 493 | free(uyvy_plane); |
| 494 | free(alpha_plane); |
| 495 | oavx_stream_close(rx); |
| 496 | oavx_stream_close(tx); |
| 497 | oavx_context_destroy(ctx); |
| 498 | printf(" -> Multi-plane separate buffer transmission with stride padding verified.\n"); |
| 499 | } |
| 500 | |
| 501 | int main(void) { |
| 502 | printf("\n=== OpenAVX Alpha & RGB Format Tests ===\n\n"); |
| 503 | |
| 504 | test_color_math(); |
| 505 | test_alpha_compositing(); |
| 506 | test_live_alpha_streaming(); |
| 507 | test_nv12_i420_conversions(); |
| 508 | test_live_multiplane_separate_buffers(); |
| 509 | |
| 510 | printf("\n[PASS] openavx_alpha_color_test\n\n"); |
| 511 | return 0; |
| 512 | } |