xref: /aosp_15_r20/external/webrtc/test/fuzzers/audio_decoder_pcm16b_fuzzer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2022 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 <memory>
12 
13 #include "modules/audio_coding/codecs/pcm16b/audio_decoder_pcm16b.h"
14 #include "test/fuzzers/audio_decoder_fuzzer.h"
15 
16 namespace webrtc {
FuzzOneInput(const uint8_t * data,size_t size)17 void FuzzOneInput(const uint8_t* data, size_t size) {
18   if (size > 10000 || size < 2) {
19     return;
20   }
21 
22   int sample_rate_hz;
23   switch (data[0] % 4) {
24     case 0:
25       sample_rate_hz = 8000;
26       break;
27     case 1:
28       sample_rate_hz = 16000;
29       break;
30     case 2:
31       sample_rate_hz = 32000;
32       break;
33     case 3:
34       sample_rate_hz = 48000;
35       break;
36     default:
37       RTC_DCHECK_NOTREACHED();
38       return;
39   }
40   const size_t num_channels = data[1] % 16 + 1;
41 
42   // Two first bytes of the data are used. Move forward.
43   data += 2;
44   size -= 2;
45 
46   AudioDecoderPcm16B dec(sample_rate_hz, num_channels);
47   // Allocate a maximum output size of 100 ms.
48   const size_t allocated_ouput_size_samples =
49       sample_rate_hz * num_channels / 10;
50   std::unique_ptr<int16_t[]> output =
51       std::make_unique<int16_t[]>(allocated_ouput_size_samples);
52   FuzzAudioDecoder(
53       DecoderFunctionType::kNormalDecode, data, size, &dec, sample_rate_hz,
54       allocated_ouput_size_samples * sizeof(int16_t), output.get());
55 }
56 }  // namespace webrtc
57