xref: /aosp_15_r20/external/webrtc/test/fuzzers/aec3_fuzzer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "absl/types/optional.h"
12 #include "modules/audio_processing/aec3/echo_canceller3.h"
13 #include "modules/audio_processing/audio_buffer.h"
14 #include "modules/audio_processing/include/audio_processing.h"
15 #include "test/fuzzers/fuzz_data_helper.h"
16 
17 namespace webrtc {
18 namespace {
19 using SampleRate = ::webrtc::AudioProcessing::NativeRate;
20 
PrepareAudioBuffer(int sample_rate_hz,test::FuzzDataHelper * fuzz_data,AudioBuffer * buffer)21 void PrepareAudioBuffer(int sample_rate_hz,
22                         test::FuzzDataHelper* fuzz_data,
23                         AudioBuffer* buffer) {
24   float* const* channels = buffer->channels_f();
25   for (size_t i = 0; i < buffer->num_channels(); ++i) {
26     for (size_t j = 0; j < buffer->num_frames(); ++j) {
27       channels[i][j] =
28           static_cast<float>(fuzz_data->ReadOrDefaultValue<int16_t>(0));
29     }
30   }
31   if (sample_rate_hz == 32000 || sample_rate_hz == 48000) {
32     buffer->SplitIntoFrequencyBands();
33   }
34 }
35 
36 }  // namespace
37 
FuzzOneInput(const uint8_t * data,size_t size)38 void FuzzOneInput(const uint8_t* data, size_t size) {
39   if (size > 200000) {
40     return;
41   }
42 
43   test::FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size));
44 
45   constexpr int kSampleRates[] = {16000, 32000, 48000};
46   const int sample_rate_hz =
47       static_cast<size_t>(fuzz_data.SelectOneOf(kSampleRates));
48 
49   constexpr int kMaxNumChannels = 9;
50   const size_t num_render_channels =
51       1 + fuzz_data.ReadOrDefaultValue<uint8_t>(0) % (kMaxNumChannels - 1);
52   const size_t num_capture_channels =
53       1 + fuzz_data.ReadOrDefaultValue<uint8_t>(0) % (kMaxNumChannels - 1);
54 
55   EchoCanceller3 aec3(EchoCanceller3Config(),
56                       /*multichannel_config=*/absl::nullopt, sample_rate_hz,
57                       num_render_channels, num_capture_channels);
58 
59   AudioBuffer capture_audio(sample_rate_hz, num_capture_channels,
60                             sample_rate_hz, num_capture_channels,
61                             sample_rate_hz, num_capture_channels);
62   AudioBuffer render_audio(sample_rate_hz, num_render_channels, sample_rate_hz,
63                            num_render_channels, sample_rate_hz,
64                            num_render_channels);
65 
66   // Fuzz frames while there is still fuzzer data.
67   while (fuzz_data.BytesLeft() > 0) {
68     bool is_capture = fuzz_data.ReadOrDefaultValue(true);
69     bool level_changed = fuzz_data.ReadOrDefaultValue(true);
70     if (is_capture) {
71       PrepareAudioBuffer(sample_rate_hz, &fuzz_data, &capture_audio);
72       aec3.ProcessCapture(&capture_audio, level_changed);
73     } else {
74       PrepareAudioBuffer(sample_rate_hz, &fuzz_data, &render_audio);
75       aec3.AnalyzeRender(&render_audio);
76     }
77   }
78 }
79 }  // namespace webrtc
80