Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 1 | /* |
| 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öller | 3f651d8 | 2018-12-19 15:06:17 +0100 | [diff] [blame] | 17 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 18 | #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 Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 22 | #include "rtc_base/numerics/safe_conversions.h" |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 23 | #include "rtc_base/random.h" |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 24 | #include "test/fuzzers/fuzz_data_helper.h" |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | namespace test { |
| 28 | namespace { |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 29 | // Generate a mixture of sine wave and gaussian noise. |
| 30 | class 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 34 | SineAndNoiseGenerator(int sample_rate_hz, FuzzDataHelper* fuzz_data) |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 35 | : 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 65 | FuzzDataHelper& fuzz_data_; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 66 | Random noise_generator_; |
| 67 | }; |
| 68 | |
| 69 | class FuzzSignalInput : public NetEqInput { |
| 70 | public: |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 71 | explicit FuzzSignalInput(FuzzDataHelper* fuzz_data, |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 72 | 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 89 | output_event_period_ms_ = fuzz_data_.SelectOneOf(output_event_periods); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 92 | absl::optional<int64_t> NextPacketTime() const override { |
Oskar Sundbom | df0822b | 2017-11-16 14:02:13 +0100 | [diff] [blame] | 93 | return packet_->time_ms; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 96 | absl::optional<int64_t> NextOutputEventTime() const override { |
Oskar Sundbom | df0822b | 2017-11-16 14:02:13 +0100 | [diff] [blame] | 97 | return next_output_event_ms_; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 98 | } |
| 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 Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 128 | absl::optional<RTPHeader> NextHeader() const override { |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 129 | RTC_DCHECK(packet_); |
Oskar Sundbom | df0822b | 2017-11-16 14:02:13 +0100 | [diff] [blame] | 130 | return packet_->header; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private: |
| 134 | bool ended_ = false; |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 135 | FuzzDataHelper& fuzz_data_; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 136 | 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 | |
| 143 | void FuzzOneInputTest(const uint8_t* data, size_t size) { |
Sam Zackrisson | 2620470 | 2018-10-25 13:46:26 +0200 | [diff] [blame] | 144 | if (size < 1 || size > 90000) { |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 145 | return; |
Sam Zackrisson | 2620470 | 2018-10-25 13:46:26 +0200 | [diff] [blame] | 146 | } |
Henrik Lundin | 2a6d864 | 2018-02-23 17:18:38 +0100 | [diff] [blame] | 147 | |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 148 | FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size)); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 149 | |
| 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 153 | const auto rate_type = fuzz_data.SelectOneOf(rate_types); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 154 | 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 Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 169 | 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 | |
Niels Möller | bd6dee8 | 2019-01-02 09:39:47 +0100 | [diff] [blame^] | 185 | NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, nullptr, |
| 186 | std::move(input), std::move(output), callbacks); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 187 | test.Run(); |
| 188 | } |
| 189 | |
| 190 | } // namespace test |
| 191 | |
| 192 | void FuzzOneInput(const uint8_t* data, size_t size) { |
| 193 | test::FuzzOneInputTest(data, size); |
| 194 | } |
| 195 | |
| 196 | } // namespace webrtc |