Raw ↗
+ +
5797689 fix(windows): resolve MSVC test linkage, missing unistd, and export declarations
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_test.c - SpeedHQ Intra-DCT verification and benchmark
7 *
8 * External FFmpeg interoperability is exercised through a local static test
9 * helper that dlopen()s libavcodec at runtime. libopenavx.so exports no
10 * SpeedHQ avcodec wrappers; this file is the only consumer of that path.
11 */
12  
13 #define _GNU_SOURCE
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <assert.h>
18 #include <time.h>
19 #include <math.h>
20 #ifndef _WIN32
21 #include <dlfcn.h>
22 #endif
23 #include <stdint.h>
24 #include <stdbool.h>
25  
26 #include "avx_platform.h"
27 #include "avx_speedhq.h"
28  
29 /* =========================================================================
30 * Local test-only helper: probe libavcodec for SpeedHQ encoder/decoder.
31 * None of this logic is exported by libopenavx.so.
32 * ========================================================================= */
33  
34 /* Minimal struct stubs matching libavcodec binary layout */
35 typedef struct {
36 const void *av_class;
37 int log_level_offset;
38 int codec_type;
39 const void *codec;
40 int codec_id;
41 unsigned int codec_tag;
42 void *priv_data;
43 void *internal;
44 void *opaque;
45 int64_t bit_rate;
46 int flags;
47 int flags2;
48 uint8_t *extradata;
49 int extradata_size;
50 int time_base_num;
51 int time_base_den;
52 int pkt_timebase_num;
53 int pkt_timebase_den;
54 int framerate_num;
55 int framerate_den;
56 int delay;
57 int width;
58 int height;
59 int coded_width;
60 int coded_height;
61 int sample_aspect_ratio_num;
62 int sample_aspect_ratio_den;
63 int pix_fmt;
64 } TestCodecCtxStub;
65  
66 typedef struct {
67 uint8_t *data[8];
68 int linesize[8];
69 uint8_t **extended_data;
70 int width;
71 int height;
72 int nb_samples;
73 int format;
74 } TestFrameStub;
75  
76 typedef struct {
77 void *buf;
78 int64_t pts;
79 int64_t dts;
80 uint8_t *data;
81 int size;
82 } TestPktStub;
83  
84 typedef void *(*fn_find_enc)(const char *);
85 typedef void *(*fn_find_dec)(const char *);
86 typedef void *(*fn_alloc_ctx)(const void *);
87 typedef void (*fn_free_ctx)(void **);
88 typedef int (*fn_open2)(void *, const void *, void **);
89 typedef void *(*fn_pkt_alloc)(void);
90 typedef void (*fn_pkt_free)(void **);
91 typedef void *(*fn_frm_alloc)(void);
92 typedef void (*fn_frm_free)(void **);
93 typedef int (*fn_frm_getbuf)(void *, int);
94 typedef int (*fn_send_frame)(void *, const void *);
95 typedef int (*fn_recv_pkt)(void *, void *);
96 typedef int (*fn_send_pkt)(void *, const void *);
97 typedef int (*fn_recv_frame)(void *, void *);
98 typedef int (*fn_opt_set)(void *, const char *, const char *, int);
99 typedef int (*fn_opt_set_int)(void *, const char *, int64_t, int);
100  
101 static struct {
102 void *h_codec;
103 void *h_util;
104 bool ready;
105 const void *shq_encoder;
106 const void *shq_decoder;
107 fn_find_enc find_encoder;
108 fn_find_dec find_decoder;
109 fn_alloc_ctx alloc_ctx;
110 fn_free_ctx free_ctx;
111 fn_open2 open2;
112 fn_pkt_alloc pkt_alloc;
113 fn_pkt_free pkt_free;
114 fn_frm_alloc frm_alloc;
115 fn_frm_free frm_free;
116 fn_frm_getbuf frm_getbuf;
117 fn_send_frame send_frame;
118 fn_recv_pkt recv_pkt;
119 fn_send_pkt send_pkt;
120 fn_recv_frame recv_frame;
121 fn_opt_set opt_set;
122 fn_opt_set_int opt_set_int;
123 } s_ffshq = {0};
124  
125 static bool test_ffmpeg_shq_available(void) {
126 #ifdef _WIN32
127 return false;
128 #else
129 if (s_ffshq.ready) return true;
130  
131 static const char *s_codec_libs[] = {"libavcodec.so.62",
132 "libavcodec.so.61",
133 "libavcodec.so.60",
134 "libavcodec.so.59",
135 "libavcodec.so.58",
136 "libavcodec.so",
137 NULL};
138 static const char *s_util_libs[] = {"libavutil.so.60",
139 "libavutil.so.59",
140 "libavutil.so.58",
141 "libavutil.so.57",
142 "libavutil.so.56",
143 "libavutil.so",
144 NULL};
145  
146 for (int i = 0; s_util_libs[i]; ++i) {
147 s_ffshq.h_util = dlopen(s_util_libs[i], RTLD_LAZY | RTLD_GLOBAL);
148 if (s_ffshq.h_util) break;
149 }
150 for (int i = 0; s_codec_libs[i]; ++i) {
151 s_ffshq.h_codec = dlopen(s_codec_libs[i], RTLD_LAZY | RTLD_GLOBAL);
152 if (s_ffshq.h_codec) break;
153 }
154 if (!s_ffshq.h_codec) return false;
155  
156 void *util = s_ffshq.h_util ? s_ffshq.h_util : s_ffshq.h_codec;
157  
158 #define SYM(h, t, name) ((t)dlsym((h), (name)))
159 s_ffshq.find_encoder = SYM(s_ffshq.h_codec, fn_find_enc, "avcodec_find_encoder_by_name");
160 s_ffshq.find_decoder = SYM(s_ffshq.h_codec, fn_find_dec, "avcodec_find_decoder_by_name");
161 s_ffshq.alloc_ctx = SYM(s_ffshq.h_codec, fn_alloc_ctx, "avcodec_alloc_context3");
162 s_ffshq.free_ctx = SYM(s_ffshq.h_codec, fn_free_ctx, "avcodec_free_context");
163 s_ffshq.open2 = SYM(s_ffshq.h_codec, fn_open2, "avcodec_open2");
164 s_ffshq.pkt_alloc = SYM(s_ffshq.h_codec, fn_pkt_alloc, "av_packet_alloc");
165 s_ffshq.pkt_free = SYM(s_ffshq.h_codec, fn_pkt_free, "av_packet_free");
166 s_ffshq.send_frame = SYM(s_ffshq.h_codec, fn_send_frame, "avcodec_send_frame");
167 s_ffshq.recv_pkt = SYM(s_ffshq.h_codec, fn_recv_pkt, "avcodec_receive_packet");
168 s_ffshq.send_pkt = SYM(s_ffshq.h_codec, fn_send_pkt, "avcodec_send_packet");
169 s_ffshq.recv_frame = SYM(s_ffshq.h_codec, fn_recv_frame, "avcodec_receive_frame");
170 s_ffshq.frm_alloc = SYM(util, fn_frm_alloc, "av_frame_alloc");
171 s_ffshq.frm_free = SYM(util, fn_frm_free, "av_frame_free");
172 s_ffshq.frm_getbuf = SYM(util, fn_frm_getbuf, "av_frame_get_buffer");
173 s_ffshq.opt_set = SYM(util, fn_opt_set, "av_opt_set");
174 s_ffshq.opt_set_int = SYM(util, fn_opt_set_int, "av_opt_set_int");
175 #undef SYM
176  
177 if (!s_ffshq.find_encoder || !s_ffshq.find_decoder || !s_ffshq.alloc_ctx || !s_ffshq.free_ctx || !s_ffshq.open2 ||
178 !s_ffshq.pkt_alloc || !s_ffshq.pkt_free || !s_ffshq.send_frame || !s_ffshq.recv_pkt || !s_ffshq.send_pkt ||
179 !s_ffshq.recv_frame || !s_ffshq.frm_alloc || !s_ffshq.frm_free || !s_ffshq.frm_getbuf)
180 return false;
181  
182 s_ffshq.shq_encoder = s_ffshq.find_encoder("speedhq");
183 s_ffshq.shq_decoder = s_ffshq.find_decoder("speedhq");
184  
185 if (!s_ffshq.shq_encoder || !s_ffshq.shq_decoder) return false;
186  
187 s_ffshq.ready = true;
188 return true;
189 #endif
190 }
191  
192 /*
193 * Encode packed UYVY to SpeedHQ-2 (SHQ2) via libavcodec.
194 * Returns 0 on success, -1 on failure.
195 */
196 static int test_ffmpeg_encode_shq(int width, int height, const uint8_t *uyvy, uint8_t *out, size_t out_max,
197 size_t *out_size) {
198 if (!s_ffshq.ready) return -1;
199  
200 void *ctx = s_ffshq.alloc_ctx(s_ffshq.shq_encoder);
201 if (!ctx) return -1;
202  
203 char size_str[32];
204 snprintf(size_str, sizeof(size_str), "%dx%d", width, height);
205 if (s_ffshq.opt_set) {
206 s_ffshq.opt_set(ctx, "video_size", size_str, 0);
207 s_ffshq.opt_set(ctx, "time_base", "1/60", 0);
208 s_ffshq.opt_set(ctx, "framerate", "60/1", 0);
209 }
210 if (s_ffshq.opt_set_int) {
211 s_ffshq.opt_set_int(ctx, "pixel_format", 4, 0);
212 s_ffshq.opt_set_int(ctx, "codec_tag", 0x32514853, 0);
213 }
214  
215 if (s_ffshq.open2(ctx, s_ffshq.shq_encoder, NULL) < 0) {
216 s_ffshq.free_ctx(&ctx);
217 return -1;
218 }
219  
220 void *frame = s_ffshq.frm_alloc();
221 if (!frame) {
222 s_ffshq.free_ctx(&ctx);
223 return -1;
224 }
225  
226 TestFrameStub *f = (TestFrameStub *)frame;
227 f->width = width;
228 f->height = height;
229 f->format = 4; /* YUV422P */
230 if (s_ffshq.frm_getbuf(frame, 32) < 0) {
231 s_ffshq.frm_free(&frame);
232 s_ffshq.free_ctx(&ctx);
233 return -1;
234 }
235  
236 /* Convert packed UYVY → planar YUV422P */
237 for (int y = 0; y < height; ++y) {
238 uint8_t *y_line = f->data[0] + (size_t)y * f->linesize[0];
239 uint8_t *u_line = f->data[1] + (size_t)y * f->linesize[1];
240 uint8_t *v_line = f->data[2] + (size_t)y * f->linesize[2];
241 const uint8_t *src = uyvy + (size_t)y * ((size_t)width * 2);
242 for (int x = 0; x < width / 2; ++x) {
243 u_line[x] = src[4 * x + 0];
244 y_line[2 * x + 0] = src[4 * x + 1];
245 v_line[x] = src[4 * x + 2];
246 y_line[2 * x + 1] = src[4 * x + 3];
247 }
248 }
249  
250 void *pkt = s_ffshq.pkt_alloc();
251 int ret = -1;
252 if (pkt) {
253 if (s_ffshq.send_frame(ctx, frame) >= 0 && s_ffshq.recv_pkt(ctx, pkt) >= 0) {
254 TestPktStub *p = (TestPktStub *)pkt;
255 if ((size_t)p->size <= out_max) {
256 memcpy(out, p->data, (size_t)p->size);
257 *out_size = (size_t)p->size;
258 ret = 0;
259 }
260 }
261 s_ffshq.pkt_free(&pkt);
262 }
263  
264 s_ffshq.frm_free(&frame);
265 s_ffshq.free_ctx(&ctx);
266 return ret;
267 }
268  
269 /*
270 * Decode a SpeedHQ-2 bitstream to packed UYVY via libavcodec.
271 * Returns 0 on success, -1 on failure.
272 */
273 static int test_ffmpeg_decode_shq(int width, int height, const uint8_t *shq, size_t shq_size, uint8_t *uyvy_out) {
274 if (!s_ffshq.ready) return -1;
275  
276 void *ctx = s_ffshq.alloc_ctx(s_ffshq.shq_decoder);
277 if (!ctx) return -1;
278  
279 char size_str[32];
280 snprintf(size_str, sizeof(size_str), "%dx%d", width, height);
281 if (s_ffshq.opt_set) {
282 s_ffshq.opt_set(ctx, "video_size", size_str, 0);
283 }
284 if (s_ffshq.opt_set_int) {
285 s_ffshq.opt_set_int(ctx, "codec_tag", 0x32514853, 0);
286 }
287  
288 if (s_ffshq.open2(ctx, s_ffshq.shq_decoder, NULL) < 0) {
289 s_ffshq.free_ctx(&ctx);
290 return -1;
291 }
292  
293 void *pkt = s_ffshq.pkt_alloc();
294 if (!pkt) {
295 s_ffshq.free_ctx(&ctx);
296 return -1;
297 }
298  
299 TestPktStub *p = (TestPktStub *)pkt;
300 p->data = (uint8_t *)shq;
301 p->size = (int)shq_size;
302  
303 int ret = -1;
304 if (s_ffshq.send_pkt(ctx, pkt) >= 0) {
305 void *frame = s_ffshq.frm_alloc();
306 if (frame && s_ffshq.recv_frame(ctx, frame) >= 0) {
307 TestFrameStub *f = (TestFrameStub *)frame;
308 int fw = f->width ? f->width : width;
309 int fh = f->height ? f->height : height;
310 /* SpeedHQ decodes to YUV422P (format 4) */
311 for (int y = 0; y < fh; ++y) {
312 uint8_t *dst = uyvy_out + (size_t)y * ((size_t)fw * 2);
313 const uint8_t *yl = f->data[0] + (size_t)y * f->linesize[0];
314 const uint8_t *ul = f->data[1] + (size_t)y * f->linesize[1];
315 const uint8_t *vl = f->data[2] + (size_t)y * f->linesize[2];
316 for (int x = 0; x < fw / 2; ++x) {
317 dst[4 * x + 0] = ul[x];
318 dst[4 * x + 1] = yl[2 * x + 0];
319 dst[4 * x + 2] = vl[x];
320 dst[4 * x + 3] = yl[2 * x + 1];
321 }
322 }
323 ret = 0;
324 }
325 if (frame) s_ffshq.frm_free(&frame);
326 }
327  
328 p->data = NULL;
329 p->size = 0;
330 s_ffshq.pkt_free(&pkt);
331 s_ffshq.free_ctx(&ctx);
332 return ret;
333 }
334  
335 static void test_ffmpeg_destroy(void) {
336 #ifndef _WIN32
337 if (s_ffshq.h_codec) {
338 dlclose(s_ffshq.h_codec);
339 s_ffshq.h_codec = NULL;
340 }
341 if (s_ffshq.h_util) {
342 dlclose(s_ffshq.h_util);
343 s_ffshq.h_util = NULL;
344 }
345 #endif
346 s_ffshq.ready = false;
347 }
348  
349 static double get_time_ms(void) {
350 return (double)avx_time_ns() / 1000000.0;
351 }
352  
353 static void generate_test_uyvy(int width, int height, uint8_t *buf) {
354 for (int y = 0; y < height; ++y) {
355 uint8_t *line = buf + (size_t)y * ((size_t)width * 2);
356 for (int x = 0; x < width / 2; ++x) {
357 uint8_t u = (uint8_t)((x * 255) / (width / 2));
358 uint8_t y0 = (uint8_t)((y * 255) / height);
359 uint8_t v = (uint8_t)(128);
360 uint8_t y1 = (uint8_t)(((y + 1) * 255) / (height + 1));
361  
362 line[4 * x + 0] = u;
363 line[4 * x + 1] = y0;
364 line[4 * x + 2] = v;
365 line[4 * x + 3] = y1;
366 }
367 }
368 }
369  
370 static void generate_checkerboard_uyvy(int width, int height, uint8_t *buf) {
371 for (int y = 0; y < height; ++y) {
372 uint8_t *line = buf + (size_t)y * ((size_t)width * 2);
373 for (int x = 0; x < width / 2; ++x) {
374 int check0 = (((x * 2) / 8) + (y / 8)) & 1;
375 int check1 = ((((x * 2) + 1) / 8) + (y / 8)) & 1;
376 line[4 * x + 0] = 128;
377 line[4 * x + 1] = check0 ? 235 : 16;
378 line[4 * x + 2] = 128;
379 line[4 * x + 3] = check1 ? 235 : 16;
380 }
381 }
382 }
383  
384 static void compute_uyvy_psnr(int width, int height, const uint8_t *ref, const uint8_t *test, double *out_y_psnr,
385 double *out_uv_psnr, int *out_max_diff) {
386 double y_mse = 0.0;
387 double uv_mse = 0.0;
388 int max_d = 0;
389 for (int y = 0; y < height; ++y) {
390 for (int x = 0; x < width / 2; ++x) {
391 size_t idx = ((size_t)y * (width / 2) + x) * 4;
392 int du = abs((int)ref[idx + 0] - (int)test[idx + 0]);
393 int dy0 = abs((int)ref[idx + 1] - (int)test[idx + 1]);
394 int dv = abs((int)ref[idx + 2] - (int)test[idx + 2]);
395 int dy1 = abs((int)ref[idx + 3] - (int)test[idx + 3]);
396  
397 if (du > max_d) max_d = du;
398 if (dy0 > max_d) max_d = dy0;
399 if (dv > max_d) max_d = dv;
400 if (dy1 > max_d) max_d = dy1;
401  
402 y_mse += (double)(dy0 * dy0 + dy1 * dy1);
403 uv_mse += (double)(du * du + dv * dv);
404 }
405 }
406 y_mse /= (double)(width * height);
407 uv_mse /= (double)(width * height);
408 *out_y_psnr = (y_mse > 0.0) ? (10.0 * log10(255.0 * 255.0 / y_mse)) : 99.0;
409 *out_uv_psnr = (uv_mse > 0.0) ? (10.0 * log10(255.0 * 255.0 / uv_mse)) : 99.0;
410 *out_max_diff = max_d;
411 }
412  
413 typedef struct {
414 int width;
415 int height;
416 const uint8_t *src_uyvy;
417 const uint8_t *ref_shq;
418 size_t ref_size;
419 } ShqMtStressArg;
420  
421 static void *shq_mt_stress_worker(void *arg) {
422 ShqMtStressArg *a = (ShqMtStressArg *)arg;
423 size_t raw_size = (size_t)a->width * (size_t)a->height * 2;
424 uint8_t *enc = (uint8_t *)malloc(raw_size);
425 uint8_t *dec = (uint8_t *)malloc(raw_size);
426 assert(enc && dec);
427  
428 for (int iter = 0; iter < 6; ++iter) {
429 size_t esize = 0;
430 int r = avx_speedhq_encode(a->width, a->height, a->src_uyvy, enc, raw_size, &esize);
431 assert(r == 0 && esize == a->ref_size);
432 assert(memcmp(enc, a->ref_shq, esize) == 0);
433  
434 memset(dec, 0, raw_size);
435 r = avx_speedhq_decode(a->width, a->height, enc, esize, dec);
436 assert(r == 0);
437 if (iter == 0) {
438 double yp = 0, uvp = 0;
439 int md = 0;
440 compute_uyvy_psnr(a->width, a->height, a->src_uyvy, dec, &yp, &uvp, &md);
441 assert(yp >= 35.0 && uvp >= 35.0 && md <= 10);
442 }
443 }
444  
445 free(enc);
446 free(dec);
447 return NULL;
448 }
449  
450 int main(void) {
451 printf("\n=== openavx SpeedHQ-2 Intra-DCT Codec Verification ===\n");
452  
453 const int width = 1920;
454 const int height = 1080;
455 const size_t raw_size = (size_t)width * height * 2; /* 4.14 MB for 1080p UYVY */
456  
457 uint8_t *src_uyvy = malloc(raw_size);
458 uint8_t *dst_uyvy = malloc(raw_size);
459 size_t max_shq_size = raw_size;
460 uint8_t *shq_buf = malloc(max_shq_size);
461  
462 assert(src_uyvy && dst_uyvy && shq_buf);
463 generate_test_uyvy(width, height, src_uyvy);
464  
465 /* 1. Initialize Codec */
466 printf("[*] 1. Initializing Native Standalone SpeedHQ subsystem...\n");
467 bool init_ok = avx_speedhq_init();
468 assert(init_ok);
469  
470 bool has_simd = avx_speedhq_has_acceleration();
471 printf(" -> Native Hardware SIMD Acceleration: %s\n",
472 has_simd ? "ENABLED (AVX2/NEON active)" : "DISABLED (Pure C99 mode)");
473  
474 /* 2. Standalone Native SpeedHQ Encode & Decode Test */
475 printf("[*] 2. Testing Standalone Native SpeedHQ Encoder & Decoder (1080p)...\n");
476 size_t native_size = 0;
477 double t0 = get_time_ms();
478 int ret = avx_speedhq_encode(width, height, src_uyvy, shq_buf, max_shq_size, &native_size);
479 double t1 = get_time_ms();
480  
481 assert(ret == 0);
482 assert(native_size > 64);
483 printf(" [+] Native Encoded 1080p frame: %zu bytes (compression ratio: %.1f:1) in %.2f ms\n", native_size,
484 (double)raw_size / native_size, t1 - t0);
485  
486 /* Verify header */
487 assert(shq_buf[0] == 96); /* Quality byte */
488 assert(shq_buf[1] == 4 && shq_buf[2] == 0 && shq_buf[3] == 0); /* Progressive offset 4 */
489  
490 memset(dst_uyvy, 0, raw_size);
491 double t2 = get_time_ms();
492 ret = avx_speedhq_decode(width, height, shq_buf, native_size, dst_uyvy);
493 double t3 = get_time_ms();
494  
495 assert(ret == 0);
496 printf(" [+] Native Decoded 1080p frame in %.2f ms\n", t3 - t2);
497  
498 /* Verify Reconstruction Quality (PSNR / MSE) */
499 double y_psnr = 0, uv_psnr = 0;
500 int max_diff = 0;
501 compute_uyvy_psnr(width, height, src_uyvy, dst_uyvy, &y_psnr, &uv_psnr, &max_diff);
502 printf(" [+] Native Quality: Y-PSNR=%.2f dB, UV-PSNR=%.2f dB (max diff=%d)\n", y_psnr, uv_psnr, max_diff);
503 assert(y_psnr >= 35.0);
504 assert(uv_psnr >= 35.0);
505 assert(max_diff <= 10);
506  
507 /* 3. Probe External Reference Codec (libavcodec SpeedHQ) for Interoperability Verification */
508 bool has_ffmpeg = test_ffmpeg_shq_available();
509 printf("[*] 3. Probing External Reference Codec (libavcodec SpeedHQ)...\n");
510 printf(" -> External FFmpeg SpeedHQ: %s\n",
511 has_ffmpeg ? "AVAILABLE (running interoperability checks)" : "NOT AVAILABLE (skipping reference tests)");
512  
513 if (has_ffmpeg) {
514 size_t ffmpeg_size = 0;
515 double ta0 = get_time_ms();
516 ret = test_ffmpeg_encode_shq(width, height, src_uyvy, shq_buf, max_shq_size, &ffmpeg_size);
517 double ta1 = get_time_ms();
518 assert(ret == 0);
519 printf(" [+] External FFmpeg Encoded 1080p frame: %zu bytes in %.2f ms\n", ffmpeg_size, ta1 - ta0);
520  
521 /* 4. Cross-Compatibility: Decode FFmpeg bitstream with Native OpenAVX decoder */
522 printf("[*] 4. Testing Cross-Compatibility (FFmpeg Bitstream -> Native OpenAVX Decoder)...\n");
523 memset(dst_uyvy, 0, raw_size);
524 ret = avx_speedhq_decode(width, height, shq_buf, ffmpeg_size, dst_uyvy);
525 assert(ret == 0);
526 compute_uyvy_psnr(width, height, src_uyvy, dst_uyvy, &y_psnr, &uv_psnr, &max_diff);
527 printf(" [+] OpenAVX parsed FFmpeg bitstream: Y-PSNR=%.2f dB, UV-PSNR=%.2f dB (max diff=%d)\n", y_psnr,
528 uv_psnr, max_diff);
529 assert(y_psnr >= 35.0);
530 assert(uv_psnr >= 35.0);
531 assert(max_diff <= 10);
532  
533 /* 5. Reverse Cross-Compatibility: Decode Native OpenAVX bitstream with FFmpeg decoder */
534 printf("[*] 5. Testing Reverse Cross-Compatibility (Native OpenAVX Bitstream -> FFmpeg Decoder)...\n");
535 memset(dst_uyvy, 0, raw_size);
536 ret = test_ffmpeg_decode_shq(width, height, shq_buf, native_size, dst_uyvy);
537 assert(ret == 0);
538 compute_uyvy_psnr(width, height, src_uyvy, dst_uyvy, &y_psnr, &uv_psnr, &max_diff);
539 printf(" [+] FFmpeg parsed OpenAVX bitstream: Y-PSNR=%.2f dB, UV-PSNR=%.2f dB (max diff=%d)\n", y_psnr,
540 uv_psnr, max_diff);
541 assert(y_psnr >= 35.0);
542 assert(uv_psnr >= 35.0);
543 assert(max_diff <= 10);
544 }
545  
546 /* 6. High-Frequency Checkerboard Test (verifies AC escape codes and DC row reset) */
547 printf("[*] 6. Testing High-Frequency Checkerboard Pattern (Escape & DC Reset Verification)...\n");
548 generate_checkerboard_uyvy(width, height, src_uyvy);
549 size_t check_size = 0;
550 ret = avx_speedhq_encode(width, height, src_uyvy, shq_buf, max_shq_size, &check_size);
551 assert(ret == 0);
552  
553 memset(dst_uyvy, 0, raw_size);
554 ret = avx_speedhq_decode(width, height, shq_buf, check_size, dst_uyvy);
555 assert(ret == 0);
556 compute_uyvy_psnr(width, height, src_uyvy, dst_uyvy, &y_psnr, &uv_psnr, &max_diff);
557 printf(" [+] Checkerboard Native OpenAVX: Y-PSNR=%.2f dB, UV-PSNR=%.2f dB (max diff=%d)\n", y_psnr, uv_psnr,
558 max_diff);
559 assert(y_psnr >= 30.0);
560 assert(uv_psnr >= 35.0);
561  
562 if (has_ffmpeg) {
563 memset(dst_uyvy, 0, raw_size);
564 ret = test_ffmpeg_decode_shq(width, height, shq_buf, check_size, dst_uyvy);
565 assert(ret == 0);
566 compute_uyvy_psnr(width, height, src_uyvy, dst_uyvy, &y_psnr, &uv_psnr, &max_diff);
567 printf(
568 " [+] Checkerboard OpenAVX Bitstream -> FFmpeg Decoder: Y-PSNR=%.2f dB, UV-PSNR=%.2f dB (max diff=%d)\n",
569 y_psnr, uv_psnr, max_diff);
570 assert(y_psnr >= 30.0);
571 assert(uv_psnr >= 35.0);
572 }
573  
574 /* 7. Single-thread vs Multi-slice Byte-Exact Determinism, Concurrent Stress & Re-Init */
575 printf("[*] 7. Testing 4-Slice Determinism, Concurrent Stress & Re-Init...\n");
576 generate_test_uyvy(width, height, src_uyvy);
577 size_t st_size = 0;
578 uint8_t *st_shq = malloc(max_shq_size);
579 uint8_t *st_dec = malloc(raw_size);
580 assert(st_shq && st_dec);
581  
582 setenv("OPENAVX_SPEEDHQ_THREADS", "1", 1);
583 avx_speedhq_destroy();
584 assert(avx_speedhq_encode(width, height, src_uyvy, st_shq, max_shq_size, &st_size) == 0);
585 assert(avx_speedhq_decode(width, height, st_shq, st_size, st_dec) == 0);
586  
587 setenv("OPENAVX_SPEEDHQ_THREADS", "4", 1);
588 avx_speedhq_destroy();
589 size_t mt_size = 0;
590 assert(avx_speedhq_encode(width, height, src_uyvy, shq_buf, max_shq_size, &mt_size) == 0);
591 assert(mt_size == st_size);
592 assert(memcmp(shq_buf, st_shq, mt_size) == 0);
593  
594 memset(dst_uyvy, 0, raw_size);
595 assert(avx_speedhq_decode(width, height, shq_buf, mt_size, dst_uyvy) == 0);
596 assert(memcmp(dst_uyvy, st_dec, raw_size) == 0);
597 unsetenv("OPENAVX_SPEEDHQ_THREADS");
598 printf(" [+] 1T vs 4T bitstreams & decoded UYVY are 100%% byte-identical (%zu bytes)\n", mt_size);
599  
600 /* Concurrent multi-thread stress (> scratch slot count to cover pool + fallback) */
601 ShqMtStressArg mt_arg = {width, height, src_uyvy, st_shq, st_size};
602 avx_thread_t workers[6];
603 for (int w = 0; w < 6; ++w) {
604 assert(avx_thread_create(&workers[w], shq_mt_stress_worker, &mt_arg) == 0);
605 }
606 for (int w = 0; w < 6; ++w) {
607 avx_thread_join(workers[w]);
608 }
609 printf(" [+] 6 concurrent threads x 6 frames (1080p encode+decode) passed cleanly!\n");
610  
611 avx_speedhq_destroy();
612 /* Verify codec works again seamlessly if re-used after destroy() */
613 size_t reinit_size = 0;
614 assert(avx_speedhq_encode(width, height, src_uyvy, shq_buf, max_shq_size, &reinit_size) == 0);
615 assert(reinit_size == st_size && memcmp(shq_buf, st_shq, reinit_size) == 0);
616 free(st_shq);
617 free(st_dec);
618  
619 avx_speedhq_destroy();
620 if (has_ffmpeg) {
621 test_ffmpeg_destroy();
622 }
623 free(src_uyvy);
624 free(dst_uyvy);
625 free(shq_buf);
626  
627 printf("\n[PASS] openavx_speedhq_test\n\n");
628 return 0;
629 }