blob: 474a1cdc4304fc7c69bc846a41775c660e4f49ea [file] [log] [blame]
Alex Loikoe5b94162019-04-08 15:19:411/*
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
15namespace webrtc {
16
17AudioDecoderMultiChannelOpusConfig MakeDecoderConfig(
18 int num_channels,
Alex Loiko556258a2019-04-09 09:22:0719 int num_streams,
Alex Loikoe5b94162019-04-08 15:19:4120 int coupled_streams,
21 std::vector<unsigned char> channel_mapping) {
22 AudioDecoderMultiChannelOpusConfig config;
23 config.num_channels = num_channels;
Alex Loiko556258a2019-04-09 09:22:0724 config.num_streams = num_streams;
Alex Loikoe5b94162019-04-08 15:19:4125 config.coupled_streams = coupled_streams;
26 config.channel_mapping = channel_mapping;
27 return config;
28}
29
30void FuzzOneInput(const uint8_t* data, size_t size) {
31 const std::vector<AudioDecoderMultiChannelOpusConfig> surround_configs = {
Alex Loiko556258a2019-04-09 09:22:0732 MakeDecoderConfig(1, 1, 0, {0}), // Mono
Alex Loikoe5b94162019-04-08 15:19:4133
Alex Loiko556258a2019-04-09 09:22:0734 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.
Alex Loikoe5b94162019-04-08 15:19:4141
Alex Loiko556258a2019-04-09 09:22:0742 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
Alex Loikoe5b94162019-04-08 15:19:4145 };
46
47 const auto config = surround_configs[data[0] % surround_configs.size()];
Alex Loiko556258a2019-04-09 09:22:0748 RTC_CHECK(config.IsOk());
Alex Loikoe5b94162019-04-08 15:19:4149 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