blob: f0b79113aebbe010976ad63573fb86baabfada8d [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"
21#include "modules/rtp_rtcp/source/byte_io.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010022#include "rtc_base/numerics/safe_conversions.h"
Henrik Lundinb82de302017-10-20 10:38:56 +020023#include "rtc_base/random.h"
Henrik Lundinb82de302017-10-20 10:38:56 +020024
25namespace webrtc {
26namespace test {
27namespace {
28// Helper class to take care of the fuzzer input, read from it, and keep track
29// of when the end of the data has been reached.
30class FuzzData {
31 public:
32 explicit FuzzData(rtc::ArrayView<const uint8_t> data) : data_(data) {}
33
34 // Returns true if n bytes can be read.
35 bool CanReadBytes(size_t n) const { return data_ix_ + n <= data_.size(); }
36
37 // Reads and returns data of type T.
38 template <typename T>
39 T Read() {
40 RTC_CHECK(CanReadBytes(sizeof(T)));
41 T x = ByteReader<T>::ReadLittleEndian(&data_[data_ix_]);
42 data_ix_ += sizeof(T);
43 return x;
44 }
45
46 // Reads and returns data of type T. Returns default_value if not enough
47 // fuzzer input remains to read a T.
48 template <typename T>
49 T ReadOrDefaultValue(T default_value) {
50 if (!CanReadBytes(sizeof(T))) {
51 return default_value;
52 }
53 return Read<T>();
54 }
55
56 // Like ReadOrDefaultValue, but replaces the value 0 with default_value.
57 template <typename T>
58 T ReadOrDefaultValueNotZero(T default_value) {
59 static_assert(std::is_integral<T>::value, "");
60 T x = ReadOrDefaultValue(default_value);
61 return x == 0 ? default_value : x;
62 }
63
64 // Returns one of the elements from the provided input array. The selection
65 // is based on the fuzzer input data. If not enough fuzzer data is available,
66 // the method will return the first element in the input array. The reason for
67 // not flaggin this as an error is that the method is called from the
68 // FuzzSignalInput constructor, and in constructors we typically do not handle
69 // errors. The code will work anyway, and the fuzzer will likely see that
70 // providing more data will actually make this method return something else.
71 template <typename T>
72 T SelectOneOf(rtc::ArrayView<const T> select_from) {
73 RTC_CHECK_LE(select_from.size(), std::numeric_limits<uint8_t>::max());
74 // Read an index between 0 and select_from.size() - 1 from the fuzzer data.
75 uint8_t index = ReadOrDefaultValue<uint8_t>(0) % select_from.size();
76 return select_from[index];
77 }
78
79 private:
80 rtc::ArrayView<const uint8_t> data_;
81 size_t data_ix_ = 0;
82};
83
84// Generate a mixture of sine wave and gaussian noise.
85class SineAndNoiseGenerator : public EncodeNetEqInput::Generator {
86 public:
87 // The noise generator is seeded with a value from the fuzzer data, but 0 is
88 // avoided (since it is not allowed by the Random class).
89 SineAndNoiseGenerator(int sample_rate_hz, FuzzData* fuzz_data)
90 : sample_rate_hz_(sample_rate_hz),
91 fuzz_data_(*fuzz_data),
92 noise_generator_(fuzz_data_.ReadOrDefaultValueNotZero<uint64_t>(1)) {}
93
94 // Generates num_samples of the sine-gaussian mixture.
95 rtc::ArrayView<const int16_t> Generate(size_t num_samples) override {
96 if (samples_.size() < num_samples) {
97 samples_.resize(num_samples);
98 }
99
100 rtc::ArrayView<int16_t> output(samples_.data(), num_samples);
101 // Randomize an amplitude between 0 and 32768; use 65000/2 if we are out of
102 // fuzzer data.
103 const float amplitude = fuzz_data_.ReadOrDefaultValue<uint16_t>(65000) / 2;
104 // Randomize a noise standard deviation between 0 and 1999.
105 const float noise_std = fuzz_data_.ReadOrDefaultValue<uint16_t>(0) % 2000;
106 for (auto& x : output) {
107 x = rtc::saturated_cast<int16_t>(amplitude * std::sin(phase_) +
108 noise_generator_.Gaussian(0, noise_std));
109 phase_ += 2 * kPi * kFreqHz / sample_rate_hz_;
110 }
111 return output;
112 }
113
114 private:
115 static constexpr int kFreqHz = 300; // The sinewave frequency.
116 const int sample_rate_hz_;
117 const double kPi = std::acos(-1);
118 std::vector<int16_t> samples_;
119 double phase_ = 0.0;
120 FuzzData& fuzz_data_;
121 Random noise_generator_;
122};
123
124class FuzzSignalInput : public NetEqInput {
125 public:
126 explicit FuzzSignalInput(FuzzData* fuzz_data,
127 int sample_rate,
128 uint8_t payload_type)
129 : fuzz_data_(*fuzz_data) {
130 AudioEncoderPcm16B::Config config;
131 config.payload_type = payload_type;
132 config.sample_rate_hz = sample_rate;
133 std::unique_ptr<AudioEncoder> encoder(new AudioEncoderPcm16B(config));
134 std::unique_ptr<EncodeNetEqInput::Generator> generator(
135 new SineAndNoiseGenerator(config.sample_rate_hz, fuzz_data));
136 input_.reset(new EncodeNetEqInput(std::move(generator), std::move(encoder),
137 std::numeric_limits<int64_t>::max()));
138 packet_ = input_->PopPacket();
139
140 // Select an output event period. This is how long time we wait between each
141 // call to NetEq::GetAudio. 10 ms is nominal, 9 and 11 ms will both lead to
142 // clock drift (in different directions).
143 constexpr int output_event_periods[] = {9, 10, 11};
144 output_event_period_ms_ =
145 fuzz_data_.SelectOneOf(rtc::ArrayView<const int>(output_event_periods));
146 }
147
148 rtc::Optional<int64_t> NextPacketTime() const override {
Oskar Sundbomdf0822b2017-11-16 14:02:13 +0100149 return packet_->time_ms;
Henrik Lundinb82de302017-10-20 10:38:56 +0200150 }
151
152 rtc::Optional<int64_t> NextOutputEventTime() const override {
Oskar Sundbomdf0822b2017-11-16 14:02:13 +0100153 return next_output_event_ms_;
Henrik Lundinb82de302017-10-20 10:38:56 +0200154 }
155
156 std::unique_ptr<PacketData> PopPacket() override {
157 RTC_DCHECK(packet_);
158 std::unique_ptr<PacketData> packet_to_return = std::move(packet_);
159 do {
160 packet_ = input_->PopPacket();
161 // If the next value from the fuzzer input is 0, the packet is discarded
162 // and the next one is pulled from the source.
163 } while (fuzz_data_.CanReadBytes(1) && fuzz_data_.Read<uint8_t>() == 0);
164 if (fuzz_data_.CanReadBytes(1)) {
165 // Generate jitter by setting an offset for the arrival time.
166 const int8_t arrival_time_offset_ms = fuzz_data_.Read<int8_t>();
167 // The arrival time can not be before the previous packets.
168 packet_->time_ms = std::max(packet_to_return->time_ms,
169 packet_->time_ms + arrival_time_offset_ms);
170 } else {
171 // Mark that we are at the end of the test. However, the current packet is
172 // still valid (but it may not have been fuzzed as expected).
173 ended_ = true;
174 }
175 return packet_to_return;
176 }
177
178 void AdvanceOutputEvent() override {
179 next_output_event_ms_ += output_event_period_ms_;
180 }
181
182 bool ended() const override { return ended_; }
183
184 rtc::Optional<RTPHeader> NextHeader() const override {
185 RTC_DCHECK(packet_);
Oskar Sundbomdf0822b2017-11-16 14:02:13 +0100186 return packet_->header;
Henrik Lundinb82de302017-10-20 10:38:56 +0200187 }
188
189 private:
190 bool ended_ = false;
191 FuzzData& fuzz_data_;
192 std::unique_ptr<EncodeNetEqInput> input_;
193 std::unique_ptr<PacketData> packet_;
194 int64_t next_output_event_ms_ = 0;
195 int64_t output_event_period_ms_ = 10;
196};
197} // namespace
198
199void FuzzOneInputTest(const uint8_t* data, size_t size) {
200 if (size < 1)
201 return;
202 FuzzData fuzz_data(rtc::ArrayView<const uint8_t>(data, size));
203
204 // Allowed sample rates and payload types used in the test.
205 std::pair<int, uint8_t> rate_types[] = {
206 {8000, 93}, {16000, 94}, {32000, 95}, {48000, 96}};
207 const auto rate_type = fuzz_data.SelectOneOf(
208 rtc::ArrayView<const std::pair<int, uint8_t>>(rate_types));
209 const int sample_rate = rate_type.first;
210 const uint8_t payload_type = rate_type.second;
211
212 // Set up the input signal generator.
213 std::unique_ptr<FuzzSignalInput> input(
214 new FuzzSignalInput(&fuzz_data, sample_rate, payload_type));
215
216 // Output sink for the test.
217 std::unique_ptr<AudioChecksum> output(new AudioChecksum);
218
219 // Configure NetEq and the NetEqTest object.
220 NetEqTest::Callbacks callbacks;
221 NetEq::Config config;
222 config.enable_post_decode_vad = true;
223 config.enable_fast_accelerate = true;
224 NetEqTest::DecoderMap codecs;
225 codecs[0] = std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu");
226 codecs[8] = std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma");
227 codecs[103] = std::make_pair(NetEqDecoder::kDecoderISAC, "isac");
228 codecs[104] = std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb");
229 codecs[111] = std::make_pair(NetEqDecoder::kDecoderOpus, "opus");
230 codecs[9] = std::make_pair(NetEqDecoder::kDecoderG722, "g722");
231 codecs[106] = std::make_pair(NetEqDecoder::kDecoderAVT, "avt");
232 codecs[114] = std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16");
233 codecs[115] = std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32");
234 codecs[116] = std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48");
235 codecs[117] = std::make_pair(NetEqDecoder::kDecoderRED, "red");
236 codecs[13] = std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb");
237 codecs[98] = std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb");
238 codecs[99] = std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32");
239 codecs[100] = std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48");
240 // One of these payload types will be used for encoding.
241 codecs[rate_types[0].second] =
242 std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb");
243 codecs[rate_types[1].second] =
244 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb");
245 codecs[rate_types[2].second] =
246 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32");
247 codecs[rate_types[3].second] =
248 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48");
249 NetEqTest::ExtDecoderMap ext_codecs;
250
251 NetEqTest test(config, codecs, ext_codecs, std::move(input),
252 std::move(output), callbacks);
253 test.Run();
254}
255
256} // namespace test
257
258void FuzzOneInput(const uint8_t* data, size_t size) {
259 test::FuzzOneInputTest(data, size);
260}
261
262} // namespace webrtc