blob: 611964d70e282ed538d8e6d13a389b7a4ae44fbb [file] [log] [blame]
Henrik Lundinb82de302017-10-20 10:38:56 +02001/*
2 * Copyright (c) 2017 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 <cmath>
12#include <limits>
13#include <memory>
14#include <vector>
15
16#include "api/array_view.h"
17#include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
18#include "modules/audio_coding/neteq/tools/audio_checksum.h"
19#include "modules/audio_coding/neteq/tools/encode_neteq_input.h"
20#include "modules/audio_coding/neteq/tools/neteq_test.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010021#include "rtc_base/numerics/safe_conversions.h"
Henrik Lundinb82de302017-10-20 10:38:56 +020022#include "rtc_base/random.h"
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010023#include "test/fuzzers/fuzz_data_helper.h"
Henrik Lundinb82de302017-10-20 10:38:56 +020024
25namespace webrtc {
26namespace test {
27namespace {
Henrik Lundinb82de302017-10-20 10:38:56 +020028// Generate a mixture of sine wave and gaussian noise.
29class SineAndNoiseGenerator : public EncodeNetEqInput::Generator {
30 public:
31 // The noise generator is seeded with a value from the fuzzer data, but 0 is
32 // avoided (since it is not allowed by the Random class).
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010033 SineAndNoiseGenerator(int sample_rate_hz, FuzzDataHelper* fuzz_data)
Henrik Lundinb82de302017-10-20 10:38:56 +020034 : sample_rate_hz_(sample_rate_hz),
35 fuzz_data_(*fuzz_data),
36 noise_generator_(fuzz_data_.ReadOrDefaultValueNotZero<uint64_t>(1)) {}
37
38 // Generates num_samples of the sine-gaussian mixture.
39 rtc::ArrayView<const int16_t> Generate(size_t num_samples) override {
40 if (samples_.size() < num_samples) {
41 samples_.resize(num_samples);
42 }
43
44 rtc::ArrayView<int16_t> output(samples_.data(), num_samples);
45 // Randomize an amplitude between 0 and 32768; use 65000/2 if we are out of
46 // fuzzer data.
47 const float amplitude = fuzz_data_.ReadOrDefaultValue<uint16_t>(65000) / 2;
48 // Randomize a noise standard deviation between 0 and 1999.
49 const float noise_std = fuzz_data_.ReadOrDefaultValue<uint16_t>(0) % 2000;
50 for (auto& x : output) {
51 x = rtc::saturated_cast<int16_t>(amplitude * std::sin(phase_) +
52 noise_generator_.Gaussian(0, noise_std));
53 phase_ += 2 * kPi * kFreqHz / sample_rate_hz_;
54 }
55 return output;
56 }
57
58 private:
59 static constexpr int kFreqHz = 300; // The sinewave frequency.
60 const int sample_rate_hz_;
61 const double kPi = std::acos(-1);
62 std::vector<int16_t> samples_;
63 double phase_ = 0.0;
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010064 FuzzDataHelper& fuzz_data_;
Henrik Lundinb82de302017-10-20 10:38:56 +020065 Random noise_generator_;
66};
67
68class FuzzSignalInput : public NetEqInput {
69 public:
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010070 explicit FuzzSignalInput(FuzzDataHelper* fuzz_data,
Henrik Lundinb82de302017-10-20 10:38:56 +020071 int sample_rate,
72 uint8_t payload_type)
73 : fuzz_data_(*fuzz_data) {
74 AudioEncoderPcm16B::Config config;
75 config.payload_type = payload_type;
76 config.sample_rate_hz = sample_rate;
77 std::unique_ptr<AudioEncoder> encoder(new AudioEncoderPcm16B(config));
78 std::unique_ptr<EncodeNetEqInput::Generator> generator(
79 new SineAndNoiseGenerator(config.sample_rate_hz, fuzz_data));
80 input_.reset(new EncodeNetEqInput(std::move(generator), std::move(encoder),
81 std::numeric_limits<int64_t>::max()));
82 packet_ = input_->PopPacket();
83
84 // Select an output event period. This is how long time we wait between each
85 // call to NetEq::GetAudio. 10 ms is nominal, 9 and 11 ms will both lead to
86 // clock drift (in different directions).
87 constexpr int output_event_periods[] = {9, 10, 11};
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010088 output_event_period_ms_ = fuzz_data_.SelectOneOf(output_event_periods);
Henrik Lundinb82de302017-10-20 10:38:56 +020089 }
90
Danil Chapovalov431abd92018-06-18 12:54:17 +020091 absl::optional<int64_t> NextPacketTime() const override {
Oskar Sundbomdf0822b2017-11-16 14:02:13 +010092 return packet_->time_ms;
Henrik Lundinb82de302017-10-20 10:38:56 +020093 }
94
Danil Chapovalov431abd92018-06-18 12:54:17 +020095 absl::optional<int64_t> NextOutputEventTime() const override {
Oskar Sundbomdf0822b2017-11-16 14:02:13 +010096 return next_output_event_ms_;
Henrik Lundinb82de302017-10-20 10:38:56 +020097 }
98
99 std::unique_ptr<PacketData> PopPacket() override {
100 RTC_DCHECK(packet_);
101 std::unique_ptr<PacketData> packet_to_return = std::move(packet_);
102 do {
103 packet_ = input_->PopPacket();
104 // If the next value from the fuzzer input is 0, the packet is discarded
105 // and the next one is pulled from the source.
106 } while (fuzz_data_.CanReadBytes(1) && fuzz_data_.Read<uint8_t>() == 0);
107 if (fuzz_data_.CanReadBytes(1)) {
108 // Generate jitter by setting an offset for the arrival time.
109 const int8_t arrival_time_offset_ms = fuzz_data_.Read<int8_t>();
110 // The arrival time can not be before the previous packets.
111 packet_->time_ms = std::max(packet_to_return->time_ms,
112 packet_->time_ms + arrival_time_offset_ms);
113 } else {
114 // Mark that we are at the end of the test. However, the current packet is
115 // still valid (but it may not have been fuzzed as expected).
116 ended_ = true;
117 }
118 return packet_to_return;
119 }
120
121 void AdvanceOutputEvent() override {
122 next_output_event_ms_ += output_event_period_ms_;
123 }
124
125 bool ended() const override { return ended_; }
126
Danil Chapovalov431abd92018-06-18 12:54:17 +0200127 absl::optional<RTPHeader> NextHeader() const override {
Henrik Lundinb82de302017-10-20 10:38:56 +0200128 RTC_DCHECK(packet_);
Oskar Sundbomdf0822b2017-11-16 14:02:13 +0100129 return packet_->header;
Henrik Lundinb82de302017-10-20 10:38:56 +0200130 }
131
132 private:
133 bool ended_ = false;
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +0100134 FuzzDataHelper& fuzz_data_;
Henrik Lundinb82de302017-10-20 10:38:56 +0200135 std::unique_ptr<EncodeNetEqInput> input_;
136 std::unique_ptr<PacketData> packet_;
137 int64_t next_output_event_ms_ = 0;
138 int64_t output_event_period_ms_ = 10;
139};
140} // namespace
141
142void FuzzOneInputTest(const uint8_t* data, size_t size) {
143 if (size < 1)
144 return;
Henrik Lundin2a6d8642018-02-23 17:18:38 +0100145 // Limit the input size to 100000 bytes to avoid fuzzer timeout.
146 if (size > 100000)
147 return;
148
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +0100149 FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size));
Henrik Lundinb82de302017-10-20 10:38:56 +0200150
151 // Allowed sample rates and payload types used in the test.
152 std::pair<int, uint8_t> rate_types[] = {
153 {8000, 93}, {16000, 94}, {32000, 95}, {48000, 96}};
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +0100154 const auto rate_type = fuzz_data.SelectOneOf(rate_types);
Henrik Lundinb82de302017-10-20 10:38:56 +0200155 const int sample_rate = rate_type.first;
156 const uint8_t payload_type = rate_type.second;
157
158 // Set up the input signal generator.
159 std::unique_ptr<FuzzSignalInput> input(
160 new FuzzSignalInput(&fuzz_data, sample_rate, payload_type));
161
162 // Output sink for the test.
163 std::unique_ptr<AudioChecksum> output(new AudioChecksum);
164
165 // Configure NetEq and the NetEqTest object.
166 NetEqTest::Callbacks callbacks;
167 NetEq::Config config;
168 config.enable_post_decode_vad = true;
169 config.enable_fast_accelerate = true;
170 NetEqTest::DecoderMap codecs;
171 codecs[0] = std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu");
172 codecs[8] = std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma");
173 codecs[103] = std::make_pair(NetEqDecoder::kDecoderISAC, "isac");
174 codecs[104] = std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb");
175 codecs[111] = std::make_pair(NetEqDecoder::kDecoderOpus, "opus");
176 codecs[9] = std::make_pair(NetEqDecoder::kDecoderG722, "g722");
177 codecs[106] = std::make_pair(NetEqDecoder::kDecoderAVT, "avt");
178 codecs[114] = std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16");
179 codecs[115] = std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32");
180 codecs[116] = std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48");
181 codecs[117] = std::make_pair(NetEqDecoder::kDecoderRED, "red");
182 codecs[13] = std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb");
183 codecs[98] = std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb");
184 codecs[99] = std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32");
185 codecs[100] = std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48");
186 // One of these payload types will be used for encoding.
187 codecs[rate_types[0].second] =
188 std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb");
189 codecs[rate_types[1].second] =
190 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb");
191 codecs[rate_types[2].second] =
192 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32");
193 codecs[rate_types[3].second] =
194 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48");
195 NetEqTest::ExtDecoderMap ext_codecs;
196
197 NetEqTest test(config, codecs, ext_codecs, std::move(input),
198 std::move(output), callbacks);
199 test.Run();
200}
201
202} // namespace test
203
204void FuzzOneInput(const uint8_t* data, size_t size) {
205 test::FuzzOneInputTest(data, size);
206}
207
208} // namespace webrtc