+ +
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_v4l2_sink_test.c - Linux V4L2 Virtual Camera Sink Verification |
| 7 | */ |
| 8 | |
| 9 | #define _GNU_SOURCE |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <assert.h> |
| 14 | |
| 15 | #include "avx_platform.h" |
| 16 | #include "openavx.h" |
| 17 | #include "avx_v4l2_sink.h" |
| 18 | #include "avx_color.h" |
| 19 | |
| 20 | #if defined(AVX_OS_LINUX) |
| 21 | #include <linux/videodev2.h> |
| 22 | #endif |
| 23 | |
| 24 | static void test_uyvy_to_yuyv_bitwise_parity(void) |
| 25 | { |
| 26 | printf("[*] Test 1: UYVY -> YUYV Bitwise Parity & SIMD Vector Alignment...\n"); |
| 27 | |
| 28 | const size_t width = 1920; |
| 29 | const size_t height = 1080; |
| 30 | const size_t pixels = width * height; |
| 31 | const size_t byte_count = pixels * 2; |
| 32 | |
| 33 | uint8_t *uyvy_src = (uint8_t*)malloc(byte_count); |
| 34 | uint8_t *yuyv_dst = (uint8_t*)malloc(byte_count); |
| 35 | assert(uyvy_src && yuyv_dst); |
| 36 | |
| 37 | /* Fill with deterministic U0 Y0 V0 Y1 sequence */ |
| 38 | for (size_t i = 0; i < byte_count; i += 4) { |
| 39 | uyvy_src[i + 0] = (uint8_t)(0x10 + (i & 0x7F)); /* U0 */ |
| 40 | uyvy_src[i + 1] = (uint8_t)(0x80 + (i & 0x7F)); /* Y0 */ |
| 41 | uyvy_src[i + 2] = (uint8_t)(0x20 + (i & 0x7F)); /* V0 */ |
| 42 | uyvy_src[i + 3] = (uint8_t)(0x90 + (i & 0x7F)); /* Y1 */ |
| 43 | } |
| 44 | |
| 45 | /* Bitwise conversion verification */ |
| 46 | const uint32_t *src32 = (const uint32_t*)uyvy_src; |
| 47 | uint32_t *dst32 = (uint32_t*)yuyv_dst; |
| 48 | size_t words = pixels / 2; |
| 49 | |
| 50 | for (size_t i = 0; i < words; ++i) { |
| 51 | uint32_t v = src32[i]; |
| 52 | dst32[i] = ((v & 0xFF00FF00) >> 8) | ((v & 0x00FF00FF) << 8); |
| 53 | } |
| 54 | |
| 55 | /* Verify byte-by-byte that Y0 U0 Y1 V0 was produced */ |
| 56 | for (size_t i = 0; i < byte_count; i += 4) { |
| 57 | assert(yuyv_dst[i + 0] == uyvy_src[i + 1]); /* Y0 */ |
| 58 | assert(yuyv_dst[i + 1] == uyvy_src[i + 0]); /* U0 */ |
| 59 | assert(yuyv_dst[i + 2] == uyvy_src[i + 3]); /* Y1 */ |
| 60 | assert(yuyv_dst[i + 3] == uyvy_src[i + 2]); /* V0 */ |
| 61 | } |
| 62 | |
| 63 | free(uyvy_src); |
| 64 | free(yuyv_dst); |
| 65 | printf(" [PASS] Test 1: 1080p UYVY to YUYV bitwise swap validated.\n"); |
| 66 | } |
| 67 | |
| 68 | static void test_rgba_to_yuyv_pipeline(void) |
| 69 | { |
| 70 | printf("[*] Test 2: Full Color Pipeline (RGBA -> UYVY -> YUYV)...\n"); |
| 71 | |
| 72 | const uint32_t width = 64; |
| 73 | const uint32_t height = 64; |
| 74 | const size_t pixels = width * height; |
| 75 | |
| 76 | uint8_t *rgba = (uint8_t*)malloc(pixels * 4); |
| 77 | uint8_t *uyvy = (uint8_t*)malloc(pixels * 2); |
| 78 | uint8_t *yuyv = (uint8_t*)malloc(pixels * 2); |
| 79 | assert(rgba && uyvy && yuyv); |
| 80 | |
| 81 | /* Generate solid Pure White (R=255, G=255, B=255, A=255) */ |
| 82 | for (size_t i = 0; i < pixels; ++i) { |
| 83 | rgba[i * 4 + 0] = 255; |
| 84 | rgba[i * 4 + 1] = 255; |
| 85 | rgba[i * 4 + 2] = 255; |
| 86 | rgba[i * 4 + 3] = 255; |
| 87 | } |
| 88 | |
| 89 | avx_color_rgba_to_uyvy(rgba, uyvy, width, height); |
| 90 | |
| 91 | /* Convert UYVY -> YUYV */ |
| 92 | const uint32_t *src32 = (const uint32_t*)uyvy; |
| 93 | uint32_t *dst32 = (uint32_t*)yuyv; |
| 94 | for (size_t i = 0; i < pixels / 2; ++i) { |
| 95 | uint32_t v = src32[i]; |
| 96 | dst32[i] = ((v & 0xFF00FF00) >> 8) | ((v & 0x00FF00FF) << 8); |
| 97 | } |
| 98 | |
| 99 | /* For white in BT.601 / BT.709 studio swing: |
| 100 | * Y ~= 235, U ~= 128, V ~= 128 */ |
| 101 | uint8_t y0 = yuyv[0]; |
| 102 | uint8_t u0 = yuyv[1]; |
| 103 | uint8_t y1 = yuyv[2]; |
| 104 | uint8_t v0 = yuyv[3]; |
| 105 | |
| 106 | assert(y0 >= 230 && y0 <= 240); |
| 107 | assert(y1 >= 230 && y1 <= 240); |
| 108 | assert(u0 >= 125 && u0 <= 131); |
| 109 | assert(v0 >= 125 && v0 <= 131); |
| 110 | |
| 111 | free(rgba); |
| 112 | free(uyvy); |
| 113 | free(yuyv); |
| 114 | printf(" [PASS] Test 2: RGBA to YUYV color accuracy verified (White Y=235, U=128, V=128).\n"); |
| 115 | } |
| 116 | |
| 117 | static void test_sink_configuration_and_resilience(void) |
| 118 | { |
| 119 | printf("[*] Test 3: V4L2 Sink Parameter Resilience & Safe Teardown...\n"); |
| 120 | |
| 121 | /* 3.1 Destroying NULL sink must not crash */ |
| 122 | avx_v4l2_sink_destroy(NULL); |
| 123 | |
| 124 | /* 3.2 Invalid config: non-existent device path */ |
| 125 | avx_v4l2_sink_config_t cfg_invalid = { |
| 126 | .device_path = "/dev/video_non_existent_9999", |
| 127 | .width = 1920, |
| 128 | .height = 1080 |
| 129 | }; |
| 130 | avx_v4l2_sink_t *sink_inv = avx_v4l2_sink_create(&cfg_invalid); |
| 131 | assert(sink_inv == NULL); |
| 132 | |
| 133 | /* 3.3 Invalid format calls on NULL sink */ |
| 134 | assert(avx_v4l2_sink_set_format(NULL, 1920, 1080, 0) == -1); |
| 135 | assert(avx_v4l2_sink_write_raw(NULL, "data", 4) == -1); |
| 136 | assert(avx_v4l2_sink_write_frame(NULL, NULL) == -1); |
| 137 | assert(strcmp(avx_v4l2_sink_get_device_path(NULL), "") == 0); |
| 138 | |
| 139 | printf(" [PASS] Test 3: Error handling and defensive checks verified.\n"); |
| 140 | } |
| 141 | |
| 142 | static void test_v4l2_loopback_probing(void) |
| 143 | { |
| 144 | printf("[*] Test 4: V4L2 Loopback Probing Subsystem...\n"); |
| 145 | |
| 146 | char loopback_path[128] = {0}; |
| 147 | bool found = avx_v4l2_sink_probe_loopback(loopback_path, sizeof(loopback_path)); |
| 148 | |
| 149 | if (found) { |
| 150 | printf(" [+] Discovered active V4L2 loopback device: %s\n", loopback_path); |
| 151 | assert(strlen(loopback_path) > 0); |
| 152 | |
| 153 | /* Attempt to open and write 1 synthetic frame to the discovered loopback */ |
| 154 | avx_v4l2_sink_config_t cfg = { |
| 155 | .device_path = loopback_path, |
| 156 | .width = 640, |
| 157 | .height = 480, |
| 158 | .pixelformat = 0 /* Default YUYV */ |
| 159 | }; |
| 160 | avx_v4l2_sink_t *sink = avx_v4l2_sink_create(&cfg); |
| 161 | if (sink) { |
| 162 | printf(" [+] Successfully initialized loopback sink on %s\n", loopback_path); |
| 163 | uint8_t frame_buf[640 * 480 * 2]; |
| 164 | memset(frame_buf, 0x80, sizeof(frame_buf)); |
| 165 | int wret = avx_v4l2_sink_write_raw(sink, frame_buf, sizeof(frame_buf)); |
| 166 | printf(" [+] Frame write return code: %d\n", wret); |
| 167 | avx_v4l2_sink_destroy(sink); |
| 168 | } else { |
| 169 | printf(" [-] Loopback device %s found but requires user video group permissions\n", loopback_path); |
| 170 | } |
| 171 | } else { |
| 172 | printf(" [i] No active v4l2loopback kernel module loaded (probe returned false as expected).\n"); |
| 173 | } |
| 174 | |
| 175 | printf(" [PASS] Test 4: Loopback probing executed safely.\n"); |
| 176 | } |
| 177 | |
| 178 | int main(void) |
| 179 | { |
| 180 | printf("=================================================================\n"); |
| 181 | printf("OpenAVX V4L2 Virtual Camera Sink Test Suite\n"); |
| 182 | printf("Target: Linux Loopback Video Subsystem (v4l2loopback)\n"); |
| 183 | printf("=================================================================\n"); |
| 184 | |
| 185 | test_uyvy_to_yuyv_bitwise_parity(); |
| 186 | test_rgba_to_yuyv_pipeline(); |
| 187 | test_sink_configuration_and_resilience(); |
| 188 | test_v4l2_loopback_probing(); |
| 189 | |
| 190 | printf("=================================================================\n"); |
| 191 | printf("All V4L2 Virtual Camera Sink Tests Passed Successfully (100%%)!\n"); |
| 192 | printf("=================================================================\n"); |
| 193 | return 0; |
| 194 | } |