• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <fuzzer/FuzzedDataProvider.h>
17 
18 #include "include/lc3.h"
19 
TestEncoder(FuzzedDataProvider & fdp)20 void TestEncoder(FuzzedDataProvider& fdp) {
21   enum lc3_pcm_format pcm_format =
22       fdp.PickValueInArray({LC3_PCM_FORMAT_S16, LC3_PCM_FORMAT_S24});
23   int dt_us = fdp.PickValueInArray({10000, 7500});
24   int sr_hz =
25       fdp.PickValueInArray({8000, 16000, 24000, 32000, /*44100,*/ 48000});
26   unsigned enc_size = lc3_encoder_size(dt_us, sr_hz);
27   uint16_t output_byte_count = fdp.ConsumeIntegralInRange(20, 400);
28   uint16_t num_frames = lc3_frame_samples(dt_us, sr_hz);
29   uint8_t bytes_per_frame = (pcm_format == LC3_PCM_FORMAT_S16 ? 2 : 4);
30 
31   if (fdp.remaining_bytes() < num_frames * bytes_per_frame) {
32     return;
33   }
34 
35   std::vector<uint32_t> input_frames(
36       num_frames / (pcm_format == LC3_PCM_FORMAT_S16 ? 2 : 1));
37   fdp.ConsumeData(input_frames.data(), num_frames * bytes_per_frame);
38 
39   void* lc3_encoder_mem = nullptr;
40   lc3_encoder_mem = malloc(enc_size);
41   lc3_encoder_t lc3_encoder =
42       lc3_setup_encoder(dt_us, sr_hz, 0, lc3_encoder_mem);
43 
44   std::vector<uint8_t> output(output_byte_count);
45   lc3_encode(lc3_encoder, pcm_format, (const int16_t*)input_frames.data(), 1,
46              output.size(), output.data());
47 
48   free(lc3_encoder_mem);
49   lc3_encoder_mem = nullptr;
50   return;
51 }
52 
TestDecoder(FuzzedDataProvider & fdp)53 void TestDecoder(FuzzedDataProvider& fdp) {
54   enum lc3_pcm_format pcm_format =
55       fdp.PickValueInArray({LC3_PCM_FORMAT_S16, LC3_PCM_FORMAT_S24});
56   int dt_us = fdp.PickValueInArray({10000, 7500});
57   int sr_hz =
58       fdp.PickValueInArray({8000, 16000, 24000, 32000, /*44100,*/ 48000});
59   unsigned dec_size = lc3_decoder_size(dt_us, sr_hz);
60   uint16_t input_byte_count = fdp.ConsumeIntegralInRange(20, 400);
61   uint16_t num_frames = lc3_frame_samples(dt_us, sr_hz);
62 
63   if (fdp.remaining_bytes() < input_byte_count) {
64     return;
65   }
66 
67   std::vector<uint8_t> input(input_byte_count);
68   fdp.ConsumeData(input.data(), input.size());
69 
70   void* lc3_decoder_mem = nullptr;
71   lc3_decoder_mem = malloc(dec_size);
72   lc3_decoder_t lc3_decoder =
73       lc3_setup_decoder(dt_us, sr_hz, 0, lc3_decoder_mem);
74 
75   std::vector<uint32_t> output(num_frames /
76                                (pcm_format == LC3_PCM_FORMAT_S16 ? 2 : 1));
77   lc3_decode(lc3_decoder, input.data(), input.size(), pcm_format,
78              (int16_t*)output.data(), 1);
79 
80   /* Empty input performs PLC (packet loss concealment) */
81   lc3_decode(lc3_decoder, nullptr, 0, pcm_format, (int16_t*)output.data(), 1);
82 
83   free(lc3_decoder_mem);
84   lc3_decoder_mem = nullptr;
85   return;
86 }
87 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
89   FuzzedDataProvider fdp(data, size);
90   TestEncoder(fdp);
91   TestDecoder(fdp);
92   return 0;
93 }