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" |
| 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 Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 21 | #include "rtc_base/numerics/safe_conversions.h" |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 22 | #include "rtc_base/random.h" |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 23 | #include "test/fuzzers/fuzz_data_helper.h" |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | namespace test { |
| 27 | namespace { |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 28 | // Generate a mixture of sine wave and gaussian noise. |
| 29 | class 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 33 | SineAndNoiseGenerator(int sample_rate_hz, FuzzDataHelper* fuzz_data) |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 34 | : 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 64 | FuzzDataHelper& fuzz_data_; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 65 | Random noise_generator_; |
| 66 | }; |
| 67 | |
| 68 | class FuzzSignalInput : public NetEqInput { |
| 69 | public: |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 70 | explicit FuzzSignalInput(FuzzDataHelper* fuzz_data, |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 71 | 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 88 | output_event_period_ms_ = fuzz_data_.SelectOneOf(output_event_periods); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame^] | 91 | absl::optional<int64_t> NextPacketTime() const override { |
Oskar Sundbom | df0822b | 2017-11-16 14:02:13 +0100 | [diff] [blame] | 92 | return packet_->time_ms; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame^] | 95 | absl::optional<int64_t> NextOutputEventTime() const override { |
Oskar Sundbom | df0822b | 2017-11-16 14:02:13 +0100 | [diff] [blame] | 96 | return next_output_event_ms_; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 97 | } |
| 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 Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame^] | 127 | absl::optional<RTPHeader> NextHeader() const override { |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 128 | RTC_DCHECK(packet_); |
Oskar Sundbom | df0822b | 2017-11-16 14:02:13 +0100 | [diff] [blame] | 129 | return packet_->header; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private: |
| 133 | bool ended_ = false; |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 134 | FuzzDataHelper& fuzz_data_; |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 135 | 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 | |
| 142 | void FuzzOneInputTest(const uint8_t* data, size_t size) { |
| 143 | if (size < 1) |
| 144 | return; |
Henrik Lundin | 2a6d864 | 2018-02-23 17:18:38 +0100 | [diff] [blame] | 145 | // Limit the input size to 100000 bytes to avoid fuzzer timeout. |
| 146 | if (size > 100000) |
| 147 | return; |
| 148 | |
Henrik Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 149 | FuzzDataHelper fuzz_data(rtc::ArrayView<const uint8_t>(data, size)); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 150 | |
| 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 Lundin | 5dcbbfd | 2017-12-07 09:21:36 +0100 | [diff] [blame] | 154 | const auto rate_type = fuzz_data.SelectOneOf(rate_types); |
Henrik Lundin | b82de30 | 2017-10-20 10:38:56 +0200 | [diff] [blame] | 155 | 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 | |
| 204 | void FuzzOneInput(const uint8_t* data, size_t size) { |
| 205 | test::FuzzOneInputTest(data, size); |
| 206 | } |
| 207 | |
| 208 | } // namespace webrtc |