1 /*
2  *  Copyright (c) 2019 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 "api/audio_codecs/opus/audio_decoder_multi_channel_opus.h"
12 #include "api/audio_codecs/opus/audio_decoder_multi_channel_opus_config.h"
13 #include "test/fuzzers/audio_decoder_fuzzer.h"
14 
15 namespace webrtc {
16 
MakeDecoderConfig(int num_channels,int num_streams,int coupled_streams,std::vector<unsigned char> channel_mapping)17 AudioDecoderMultiChannelOpusConfig MakeDecoderConfig(
18     int num_channels,
19     int num_streams,
20     int coupled_streams,
21     std::vector<unsigned char> channel_mapping) {
22   AudioDecoderMultiChannelOpusConfig config;
23   config.num_channels = num_channels;
24   config.num_streams = num_streams;
25   config.coupled_streams = coupled_streams;
26   config.channel_mapping = channel_mapping;
27   return config;
28 }
29 
FuzzOneInput(const uint8_t * data,size_t size)30 void FuzzOneInput(const uint8_t* data, size_t size) {
31   const std::vector<AudioDecoderMultiChannelOpusConfig> surround_configs = {
32       MakeDecoderConfig(1, 1, 0, {0}),  // Mono
33 
34       MakeDecoderConfig(2, 2, 0, {0, 0}),  // Copy the first (of
35                                            // 2) decoded streams
36                                            // into both output
37                                            // channel 0 and output
38                                            // channel 1. Ignore
39                                            // the 2nd decoded
40                                            // stream.
41 
42       MakeDecoderConfig(4, 2, 2, {0, 1, 2, 3}),             // Quad.
43       MakeDecoderConfig(6, 4, 2, {0, 4, 1, 2, 3, 5}),       // 5.1
44       MakeDecoderConfig(8, 5, 3, {0, 6, 1, 2, 3, 4, 5, 7})  // 7.1
45   };
46 
47   const auto config = surround_configs[data[0] % surround_configs.size()];
48   RTC_CHECK(config.IsOk());
49   std::unique_ptr<AudioDecoder> dec =
50       AudioDecoderMultiChannelOpus::MakeAudioDecoder(config);
51   RTC_CHECK(dec);
52   const int kSampleRateHz = 48000;
53   const size_t kAllocatedOuputSizeSamples =
54       4 * kSampleRateHz / 10;  // 4x100 ms, 4 times the size of the output array
55                                // for the stereo Opus codec. It should be enough
56                                // for 8 channels.
57   int16_t output[kAllocatedOuputSizeSamples];
58   FuzzAudioDecoder(DecoderFunctionType::kNormalDecode, data, size, dec.get(),
59                    kSampleRateHz, sizeof(output), output);
60 }
61 }  // namespace webrtc
62