blob: 96ba95d96a8a01231658e4dfde12161036959090 [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"
Niels Möller3f651d82018-12-19 15:06:17 +010017#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Henrik Lundinb82de302017-10-20 10:38:56 +020018#include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
19#include "modules/audio_coding/neteq/tools/audio_checksum.h"
20#include "modules/audio_coding/neteq/tools/encode_neteq_input.h"
21#include "modules/audio_coding/neteq/tools/neteq_test.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 Lundin5dcbbfd2017-12-07 09:21:36 +010024#include "test/fuzzers/fuzz_data_helper.h"
Henrik Lundinb82de302017-10-20 10:38:56 +020025
26namespace webrtc {
27namespace test {
28namespace {
Henrik Lundinb82de302017-10-20 10:38:56 +020029// Generate a mixture of sine wave and gaussian noise.
30class SineAndNoiseGenerator : public EncodeNetEqInput::Generator {
31 public:
32 // The noise generator is seeded with a value from the fuzzer data, but 0 is
33 // avoided (since it is not allowed by the Random class).
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010034 SineAndNoiseGenerator(int sample_rate_hz, FuzzDataHelper* fuzz_data)
Henrik Lundinb82de302017-10-20 10:38:56 +020035 : sample_rate_hz_(sample_rate_hz),
36 fuzz_data_(*fuzz_data),
37 noise_generator_(fuzz_data_.ReadOrDefaultValueNotZero<uint64_t>(1)) {}
38
39 // Generates num_samples of the sine-gaussian mixture.
40 rtc::ArrayView<const int16_t> Generate(size_t num_samples) override {
41 if (samples_.size() < num_samples) {
42 samples_.resize(num_samples);
43 }
44
45 rtc::ArrayView<int16_t> output(samples_.data(), num_samples);
46 // Randomize an amplitude between 0 and 32768; use 65000/2 if we are out of
47 // fuzzer data.
48 const float amplitude = fuzz_data_.ReadOrDefaultValue<uint16_t>(65000) / 2;
49 // Randomize a noise standard deviation between 0 and 1999.
50 const float noise_std = fuzz_data_.ReadOrDefaultValue<uint16_t>(0) % 2000;
51 for (auto& x : output) {
52 x = rtc::saturated_cast<int16_t>(amplitude * std::sin(phase_) +
53 noise_generator_.Gaussian(0, noise_std));
54 phase_ += 2 * kPi * kFreqHz / sample_rate_hz_;
55 }
56 return output;
57 }
58
59 private:
60 static constexpr int kFreqHz = 300; // The sinewave frequency.
61 const int sample_rate_hz_;
62 const double kPi = std::acos(-1);
63 std::vector<int16_t> samples_;
64 double phase_ = 0.0;
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010065 FuzzDataHelper& fuzz_data_;
Henrik Lundinb82de302017-10-20 10:38:56 +020066 Random noise_generator_;
67};
68
69class FuzzSignalInput : public NetEqInput {
70 public:
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010071 explicit FuzzSignalInput(FuzzDataHelper* fuzz_data,
Henrik Lundinb82de302017-10-20 10:38:56 +020072 int sample_rate,
73 uint8_t payload_type)
74 : fuzz_data_(*fuzz_data) {
75 AudioEncoderPcm16B::Config config;
76 config.payload_type = payload_type;
77 config.sample_rate_hz = sample_rate;
78 std::unique_ptr<AudioEncoder> encoder(new AudioEncoderPcm16B(config));
79 std::unique_ptr<EncodeNetEqInput::Generator> generator(
80 new SineAndNoiseGenerator(config.sample_rate_hz, fuzz_data));
81 input_.reset(new EncodeNetEqInput(std::move(generator), std::move(encoder),
82 std::numeric_limits<int64_t>::max()));
83 packet_ = input_->PopPacket();
84
85 // Select an output event period. This is how long time we wait between each
86 // call to NetEq::GetAudio. 10 ms is nominal, 9 and 11 ms will both lead to
87 // clock drift (in different directions).
88 constexpr int output_event_periods[] = {9, 10, 11};
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +010089 output_event_period_ms_ = fuzz_data_.SelectOneOf(output_event_periods);
Henrik Lundinb82de302017-10-20 10:38:56 +020090 }
91
Danil Chapovalov431abd92018-06-18 12:54:17 +020092 absl::optional<int64_t> NextPacketTime() const override {
Oskar Sundbomdf0822b2017-11-16 14:02:13 +010093 return packet_->time_ms;
Henrik Lundinb82de302017-10-20 10:38:56 +020094 }
95
Danil Chapovalov431abd92018-06-18 12:54:17 +020096 absl::optional<int64_t> NextOutputEventTime() const override {
Oskar Sundbomdf0822b2017-11-16 14:02:13 +010097 return next_output_event_ms_;
Henrik Lundinb82de302017-10-20 10:38:56 +020098 }
99
100 std::unique_ptr<PacketData> PopPacket() override {
101 RTC_DCHECK(packet_);
102 std::unique_ptr<PacketData> packet_to_return = std::move(packet_);
103 do {
104 packet_ = input_->PopPacket();
105 // If the next value from the fuzzer input is 0, the packet is discarded
106 // and the next one is pulled from the source.
107 } while (fuzz_data_.CanReadBytes(1) && fuzz_data_.Read<uint8_t>() == 0);
108 if (fuzz_data_.CanReadBytes(1)) {
109 // Generate jitter by setting an offset for the arrival time.
110 const int8_t arrival_time_offset_ms = fuzz_data_.Read<int8_t>();
111 // The arrival time can not be before the previous packets.
112 packet_->time_ms = std::max(packet_to_return->time_ms,
113 packet_->time_ms + arrival_time_offset_ms);
114 } else {
115 // Mark that we are at the end of the test. However, the current packet is
116 // still valid (but it may not have been fuzzed as expected).
117 ended_ = true;
118 }
119 return packet_to_return;
120 }
121
122 void AdvanceOutputEvent() override {
123 next_output_event_ms_ += output_event_period_ms_;
124 }
125
126 bool ended() const override { return ended_; }
127
Danil Chapovalov431abd92018-06-18 12:54:17 +0200128 absl::optional<RTPHeader> NextHeader() const override {
Henrik Lundinb82de302017-10-20 10:38:56 +0200129 RTC_DCHECK(packet_);
Oskar Sundbomdf0822b2017-11-16 14:02:13 +0100130 return packet_->header;
Henrik Lundinb82de302017-10-20 10:38:56 +0200131 }
132
133 private:
134 bool ended_ = false;
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +0100135 FuzzDataHelper& fuzz_data_;
Henrik Lundinb82de302017-10-20 10:38:56 +0200136 std::unique_ptr<EncodeNetEqInput> input_;
137 std::unique_ptr<PacketData> packet_;
138 int64_t next_output_event_ms_ = 0;
139 int64_t output_event_period_ms_ = 10;
140};
141} // namespace
142
143void FuzzOneInputTest(const uint8_t* data, size_t size) {
Sam Zackrisson26204702018-10-25 13:46:26 +0200144 if (size < 1 || size > 90000) {
Henrik Lundinb82de302017-10-20 10:38:56 +0200145 return;
Sam Zackrisson26204702018-10-25 13:46:26 +0200146 }
Henrik Lundin2a6d8642018-02-23 17:18:38 +0100147
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +0100148 FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size));
Henrik Lundinb82de302017-10-20 10:38:56 +0200149
150 // Allowed sample rates and payload types used in the test.
151 std::pair<int, uint8_t> rate_types[] = {
152 {8000, 93}, {16000, 94}, {32000, 95}, {48000, 96}};
Henrik Lundin5dcbbfd2017-12-07 09:21:36 +0100153 const auto rate_type = fuzz_data.SelectOneOf(rate_types);
Henrik Lundinb82de302017-10-20 10:38:56 +0200154 const int sample_rate = rate_type.first;
155 const uint8_t payload_type = rate_type.second;
156
157 // Set up the input signal generator.
158 std::unique_ptr<FuzzSignalInput> input(
159 new FuzzSignalInput(&fuzz_data, sample_rate, payload_type));
160
161 // Output sink for the test.
162 std::unique_ptr<AudioChecksum> output(new AudioChecksum);
163
164 // Configure NetEq and the NetEqTest object.
165 NetEqTest::Callbacks callbacks;
166 NetEq::Config config;
167 config.enable_post_decode_vad = true;
168 config.enable_fast_accelerate = true;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200169 auto codecs = NetEqTest::StandardDecoderMap();
170 // rate_types contains the payload types that will be used for encoding.
171 // Verify that they all are included in the standard decoder map, and that
172 // they point to the expected decoder types.
173 RTC_CHECK_EQ(codecs.count(rate_types[0].second), 1);
174 RTC_CHECK(codecs[rate_types[0].second].first == NetEqDecoder::kDecoderPCM16B);
175 RTC_CHECK_EQ(codecs.count(rate_types[1].second), 1);
176 RTC_CHECK(codecs[rate_types[1].second].first ==
177 NetEqDecoder::kDecoderPCM16Bwb);
178 RTC_CHECK_EQ(codecs.count(rate_types[2].second), 1);
179 RTC_CHECK(codecs[rate_types[2].second].first ==
180 NetEqDecoder::kDecoderPCM16Bswb32kHz);
181 RTC_CHECK_EQ(codecs.count(rate_types[3].second), 1);
182 RTC_CHECK(codecs[rate_types[3].second].first ==
183 NetEqDecoder::kDecoderPCM16Bswb48kHz);
184
Henrik Lundinb82de302017-10-20 10:38:56 +0200185 NetEqTest::ExtDecoderMap ext_codecs;
186
Niels Möller3f651d82018-12-19 15:06:17 +0100187 NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, ext_codecs,
188 nullptr, std::move(input), std::move(output), callbacks);
Henrik Lundinb82de302017-10-20 10:38:56 +0200189 test.Run();
190}
191
192} // namespace test
193
194void FuzzOneInput(const uint8_t* data, size_t size) {
195 test::FuzzOneInputTest(data, size);
196}
197
198} // namespace webrtc