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_audio_resampler.c - libFuzzer Target for Audio Resampling & Downmixing
7 */
8  
9 #include <stdint.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "avx_audio.h"
14  
15 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
16 if (!data || size < 12) return 0;
17  
18 /* Extract parameters from header */
19 int in_rate = ((int)(data[0] | (data[1] << 8)) % 200000) + 1;
20 int out_rate = ((int)(data[2] | (data[3] << 8)) % 200000) + 1;
21 int channels = (data[4] % 8) + 1;
22 avx_audio_resample_mode_e mode = (avx_audio_resample_mode_e)(data[5] % 3);
23 avx_audio_normalize_e norm_mode = (avx_audio_normalize_e)(data[6] % 3);
24  
25 const uint8_t *sample_bytes = data + 12;
26 size_t sample_len = size - 12;
27 int total_floats = (int)(sample_len / sizeof(float));
28 int frames = total_floats / channels;
29 if (frames > 1024) frames = 1024;
30 if (frames < 1) frames = 1;
31  
32 /* Prepare planar input arrays */
33 float *in_data[8] = {NULL};
34 float *out_data[8] = {NULL};
35 int max_out = frames * 4 + 256;
36  
37 for (int c = 0; c < 8; ++c) {
38 in_data[c] = (float *)calloc((size_t)frames + 16, sizeof(float));
39 out_data[c] = (float *)calloc((size_t)max_out + 16, sizeof(float));
40 if (!in_data[c] || !out_data[c]) {
41 for (int j = 0; j <= c; ++j) {
42 free(in_data[j]);
43 free(out_data[j]);
44 }
45 return 0;
46 }
47 }
48  
49 /* Fill in_data with fuzzer bytes if available */
50 if (total_floats > 0) {
51 for (int c = 0; c < channels; ++c) {
52 for (int f = 0; f < frames; ++f) {
53 size_t src_idx = (size_t)f * (size_t)channels + (size_t)c;
54 if (src_idx < (size_t)total_floats) {
55 memcpy(&in_data[c][f], sample_bytes + src_idx * sizeof(float), sizeof(float));
56 }
57 }
58 }
59 }
60  
61 /* 1. Test stateful resampler */
62 avx_resampler_t *r = avx_resampler_create(in_rate, out_rate, channels, mode);
63 if (r) {
64 (void)avx_resampler_process(r, (const float * const *)in_data, frames, out_data, max_out);
65 (void)avx_resampler_flush(r, out_data, max_out);
66 avx_resampler_reset(r);
67 avx_resampler_destroy(r);
68 }
69  
70 /* 2. Test one-shot simple resample */
71 (void)avx_audio_resample_simple(in_data[0], frames, in_rate, out_data[0], max_out, out_rate, mode);
72  
73 /* 3. Test downmixing routines */
74 if (frames > 0) {
75 avx_audio_downmix_stereo_to_mono(in_data[0], in_data[1], out_data[0], frames, norm_mode);
76 avx_audio_downmix_quad_to_stereo((const float * const *)in_data, out_data[0], out_data[1], frames, norm_mode);
77 avx_audio_downmix_5_1_to_stereo((const float * const *)in_data, out_data[0], out_data[1], frames, norm_mode);
78 avx_audio_downmix_7_1_to_stereo((const float * const *)in_data, out_data[0], out_data[1], frames, norm_mode);
79 }
80  
81 for (int c = 0; c < 8; ++c) {
82 free(in_data[c]);
83 free(out_data[c]);
84 }
85  
86 return 0;
87 }