+ +
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 | * fuzz/fuzz_audio_codec.c - libFuzzer Target for Opus & IMA ADPCM Audio Codecs |
| 7 | */ |
| 8 | |
| 9 | #include <stdint.h> |
| 10 | #include <stddef.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include "openavx.h" |
| 14 | #include "avx_audio_codec.h" |
| 15 | |
| 16 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 17 | if (!data || size < 8) return 0; |
| 18 | |
| 19 | /* Extract channels and codec parameters from the first 4 bytes */ |
| 20 | int channel_options[] = { 1, 2, 4, 6, 8 }; |
| 21 | int channels = channel_options[data[0] % 5]; |
| 22 | int sample_rate = (data[1] % 2 == 0) ? 48000 : 44100; |
| 23 | uint32_t fourcc = (data[2] % 2 == 0) ? OAVX_FOURCC_AUDIO_TYPE_OPUS : OAVX_FOURCC_AUDIO_TYPE_ADPC; |
| 24 | |
| 25 | const uint8_t *payload = data + 4; |
| 26 | size_t payload_len = size - 4; |
| 27 | |
| 28 | /* 1. Fuzz Standalone IMA ADPCM Decoder */ |
| 29 | const int max_samples = 4096; |
| 30 | float *planes[8] = {NULL}; |
| 31 | for (int c = 0; c < 8; ++c) { |
| 32 | planes[c] = (float*)calloc(max_samples, sizeof(float)); |
| 33 | if (!planes[c]) { |
| 34 | for (int j = 0; j < c; ++j) free(planes[j]); |
| 35 | return 0; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | avx_adpcm_decode_to_planar_float(payload, payload_len, channels, planes, max_samples); |
| 40 | |
| 41 | /* 2. Fuzz IMA ADPCM Encoder with synthetic float data */ |
| 42 | int synth_samples = (int)(payload_len / (sizeof(float) * channels)); |
| 43 | if (synth_samples > 0 && synth_samples <= max_samples) { |
| 44 | for (int c = 0; c < channels; ++c) { |
| 45 | memcpy(planes[c], payload + (size_t)c * synth_samples * sizeof(float), synth_samples * sizeof(float)); |
| 46 | } |
| 47 | uint8_t out_adpcm[65536]; |
| 48 | avx_adpcm_encode_planar_float((const float* const*)planes, channels, synth_samples, out_adpcm, sizeof(out_adpcm)); |
| 49 | } |
| 50 | |
| 51 | /* 3. Fuzz Opus / IMA ADPCM Audio Decoder */ |
| 52 | avx_audio_decoder_t *dec = avx_audio_decoder_create(sample_rate, channels, fourcc); |
| 53 | if (dec) { |
| 54 | avx_audio_decoder_decode(dec, fourcc, payload, payload_len, planes, max_samples); |
| 55 | avx_audio_decoder_destroy(dec); |
| 56 | } |
| 57 | |
| 58 | /* 4. Fuzz Audio Encoder Feed, Pull, and Flush */ |
| 59 | avx_audio_encoder_t *enc = avx_audio_encoder_create(sample_rate, channels, AVX_AUDIO_CODEC_OPUS, 128); |
| 60 | if (enc) { |
| 61 | int feed_samples = (int)(data[3] % 240) + 1; |
| 62 | if (feed_samples <= max_samples) { |
| 63 | avx_audio_encoder_feed(enc, (const float* const*)planes, feed_samples, 0); |
| 64 | |
| 65 | uint8_t out_pkt[65536]; |
| 66 | size_t out_pkt_size = 0; |
| 67 | int out_samples = 0; |
| 68 | int64_t out_pts = 0; |
| 69 | uint32_t out_fourcc = 0; |
| 70 | |
| 71 | avx_audio_encoder_pull(enc, out_pkt, sizeof(out_pkt), &out_pkt_size, &out_samples, &out_pts, &out_fourcc); |
| 72 | avx_audio_encoder_flush(enc, out_pkt, sizeof(out_pkt), &out_pkt_size, &out_samples, &out_pts, &out_fourcc); |
| 73 | } |
| 74 | avx_audio_encoder_destroy(enc); |
| 75 | } |
| 76 | |
| 77 | for (int c = 0; c < 8; ++c) { |
| 78 | free(planes[c]); |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |