Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
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 * fuzz/fuzz_speedhq.c - libFuzzer Target for SpeedHQ Bitstream Decoding
7 */
8  
9 #include <stdint.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include "avx_internal.h"
14 #include "avx_speedhq.h"
15  
16 int LLVMFuzzerInitialize(int *argc, char ***argv) {
17 (void)argc;
18 (void)argv;
19 avx_speedhq_init();
20 return 0;
21 }
22  
23 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
24 if (!data || size < 6) return 0;
25  
26 /* Extract fuzzed FourCC across all 8 supported SpeedHQ types */
27 static const uint32_t s_shq_fourccs[8] = {
28 OAVX_FOURCC_VIDEO_TYPE_SHQ0,
29 OAVX_FOURCC_VIDEO_TYPE_SHQ1,
30 OAVX_FOURCC_VIDEO_TYPE_SHQ2,
31 OAVX_FOURCC_VIDEO_TYPE_SHQ3,
32 OAVX_FOURCC_VIDEO_TYPE_SHQ4,
33 OAVX_FOURCC_VIDEO_TYPE_SHQ5,
34 OAVX_FOURCC_VIDEO_TYPE_SHQ7,
35 OAVX_FOURCC_VIDEO_TYPE_SHQ9
36 };
37 uint32_t fourcc = s_shq_fourccs[data[0] % 8];
38  
39 /* Extract fuzzed dimensions */
40 uint16_t raw_w = (uint16_t)data[1] | ((uint16_t)data[2] << 8);
41 uint16_t raw_h = (uint16_t)data[3] | ((uint16_t)data[4] << 8);
42  
43 /* Test both arbitrary and macroblock-aligned dimensions */
44 int w = (int)(raw_w % 1921);
45 int h = (int)(raw_h % 1081);
46  
47 if ((data[5] & 1) != 0) {
48 w = (w / 16) * 16;
49 }
50  
51 const uint8_t *payload = data + 6;
52 size_t payload_size = size - 6;
53  
54 if (w > 0 && h > 0 && w <= AVX_MAX_VIDEO_WIDTH && h <= AVX_MAX_VIDEO_HEIGHT) {
55 uint64_t out_u64 = (uint64_t)w * (uint64_t)h * 2ULL;
56 uint64_t alpha_u64 = (uint64_t)w * (uint64_t)h;
57  
58 if (out_u64 <= 64ULL * 1024ULL * 1024ULL) {
59 uint8_t *out_buf = (uint8_t *)malloc((size_t)out_u64);
60 uint8_t *alpha_buf = (uint8_t *)malloc((size_t)alpha_u64);
61  
62 if (out_buf) {
63 /* 1. Standalone pure C99 decoder */
64 avx_speedhq_decode_c99(w, h, payload, payload_size, out_buf);
65  
66 /* 2. SIMD-dispatched decoder */
67 avx_speedhq_decode(w, h, payload, payload_size, out_buf);
68  
69 /* 3. Extended decoders with alpha plane */
70 if (alpha_buf) {
71 bool has_alpha = false;
72 avx_speedhq_decode_ex(fourcc, w, h, payload, payload_size, out_buf, alpha_buf, &has_alpha);
73 avx_speedhq_decode_c99_ex(fourcc, w, h, payload, payload_size, out_buf, alpha_buf, &has_alpha);
74 }
75 free(out_buf);
76 }
77 if (alpha_buf) {
78 free(alpha_buf);
79 }
80 }
81 } else {
82 /* Edge cases: invalid or zero dimensions, NULL output buffers */
83 avx_speedhq_decode(w, h, payload, payload_size, NULL);
84 avx_speedhq_decode_c99(w, h, payload, payload_size, NULL);
85 }
86  
87 return 0;
88 }