xref: /aosp_15_r20/external/webrtc/test/fuzzers/audio_decoder_pcm_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/g711/audio_decoder_pcm.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   const size_t num_channels = data[0] % 16 + 1;
23 
24   std::unique_ptr<AudioDecoder> dec;
25   if (data[1] % 2) {
26     dec = std::make_unique<AudioDecoderPcmU>(num_channels);
27   } else {
28     dec = std::make_unique<AudioDecoderPcmA>(num_channels);
29   }
30 
31   // Two first bytes of the data are used. Move forward.
32   data += 2;
33   size -= 2;
34 
35   // Allocate a maximum output size of 100 ms.
36   const size_t allocated_ouput_size_samples =
37       dec->SampleRateHz() * num_channels / 10;
38   std::unique_ptr<int16_t[]> output =
39       std::make_unique<int16_t[]>(allocated_ouput_size_samples);
40   FuzzAudioDecoder(DecoderFunctionType::kNormalDecode, data, size, dec.get(),
41                    dec->SampleRateHz(),
42                    allocated_ouput_size_samples * sizeof(int16_t),
43                    output.get());
44 }
45 }  // namespace webrtc
46