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 "modules/audio_processing/aec3/echo_canceller3.h"
12 #include "modules/audio_processing/audio_buffer.h"
13 #include "modules/audio_processing/include/audio_processing.h"
14 #include "test/fuzzers/fuzz_data_helper.h"
15
16 namespace webrtc {
17 namespace {
18 using SampleRate = ::webrtc::AudioProcessing::NativeRate;
19
PrepareAudioBuffer(int sample_rate_hz,test::FuzzDataHelper * fuzz_data,AudioBuffer * buffer)20 void PrepareAudioBuffer(int sample_rate_hz,
21 test::FuzzDataHelper* fuzz_data,
22 AudioBuffer* buffer) {
23 float* const* channels = buffer->channels_f();
24 for (size_t i = 0; i < buffer->num_channels(); ++i) {
25 for (size_t j = 0; j < buffer->num_frames(); ++j) {
26 channels[i][j] =
27 static_cast<float>(fuzz_data->ReadOrDefaultValue<int16_t>(0));
28 }
29 }
30 if (sample_rate_hz == 32000 || sample_rate_hz == 48000) {
31 buffer->SplitIntoFrequencyBands();
32 }
33 }
34
35 } // namespace
36
FuzzOneInput(const uint8_t * data,size_t size)37 void FuzzOneInput(const uint8_t* data, size_t size) {
38 if (size > 200000) {
39 return;
40 }
41
42 test::FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size));
43
44 constexpr int kSampleRates[] = {16000, 32000, 48000};
45 const int sample_rate_hz =
46 static_cast<size_t>(fuzz_data.SelectOneOf(kSampleRates));
47
48 constexpr int kMaxNumChannels = 9;
49 const size_t num_render_channels =
50 1 + fuzz_data.ReadOrDefaultValue<uint8_t>(0) % (kMaxNumChannels - 1);
51 const size_t num_capture_channels =
52 1 + fuzz_data.ReadOrDefaultValue<uint8_t>(0) % (kMaxNumChannels - 1);
53
54 EchoCanceller3 aec3(EchoCanceller3Config(), sample_rate_hz,
55 num_render_channels, num_capture_channels);
56
57 AudioBuffer capture_audio(sample_rate_hz, num_capture_channels,
58 sample_rate_hz, num_capture_channels,
59 sample_rate_hz, num_capture_channels);
60 AudioBuffer render_audio(sample_rate_hz, num_render_channels, sample_rate_hz,
61 num_render_channels, sample_rate_hz,
62 num_render_channels);
63
64 // Fuzz frames while there is still fuzzer data.
65 while (fuzz_data.BytesLeft() > 0) {
66 bool is_capture = fuzz_data.ReadOrDefaultValue(true);
67 bool level_changed = fuzz_data.ReadOrDefaultValue(true);
68 if (is_capture) {
69 PrepareAudioBuffer(sample_rate_hz, &fuzz_data, &capture_audio);
70 aec3.ProcessCapture(&capture_audio, level_changed);
71 } else {
72 PrepareAudioBuffer(sample_rate_hz, &fuzz_data, &render_audio);
73 aec3.AnalyzeRender(&render_audio);
74 }
75 }
76 }
77 } // namespace webrtc
78