/src/opus/tests/opus_encode_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2021 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <fuzzer/FuzzedDataProvider.h> |
16 | | #include <stdint.h> |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include "opus.h" |
21 | | #include "opus_types.h" |
22 | | #include "opus_defines.h" |
23 | | |
24 | | #include "opus_ossfuzz_utils.h" |
25 | | |
26 | 154M | #define MAX_PACKET (1500) |
27 | | #if defined(ENABLE_QEXT) |
28 | | #define SAMPLES (96000 * 10) |
29 | | #else |
30 | 77.2M | #define SAMPLES (48000 * 10) |
31 | | #endif |
32 | 77.2M | #define MAX_FRAME_SAMP (5760) |
33 | | |
34 | | static const int sampling_rates[] = {8000, 12000, 16000, 24000, 48000 |
35 | | ARG_QEXT(96000)}; |
36 | | static const int channels[] = {1, 2}; |
37 | | static const int applications[] = {OPUS_APPLICATION_AUDIO, |
38 | | OPUS_APPLICATION_VOIP, |
39 | | OPUS_APPLICATION_RESTRICTED_LOWDELAY}; |
40 | | static const int frame_sizes_ms_x2[] = {5, 10, 20, 40, 80, 120, 160, 200, 240}; |
41 | | |
42 | | static opus_int16 inbuf[sizeof(opus_int16) * SAMPLES] = {0}; |
43 | | static unsigned char packet[MAX_PACKET + 257]; |
44 | | |
45 | 6.65k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
46 | 6.65k | FuzzedDataProvider fdp(data, size); |
47 | | |
48 | 6.65k | int err = OPUS_OK; |
49 | 6.65k | int num_channels = fdp.PickValueInArray(channels); |
50 | 6.65k | int frame_size = fdp.PickValueInArray(frame_sizes_ms_x2); |
51 | 6.65k | int sampling_rate = fdp.PickValueInArray(sampling_rates); |
52 | 6.65k | int application = fdp.PickValueInArray(applications); |
53 | | |
54 | | |
55 | 6.65k | OpusEncoder *enc = |
56 | 6.65k | opus_encoder_create(sampling_rate, num_channels, application, &err); |
57 | 6.65k | if (err != OPUS_OK || enc == NULL) { |
58 | 0 | opus_encoder_destroy(enc); |
59 | 0 | return 0; |
60 | 0 | } |
61 | | |
62 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10))); |
63 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool())); |
64 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool())); |
65 | 6.65k | opus_encoder_ctl( |
66 | 6.65k | enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2}))); |
67 | 6.65k | opus_encoder_ctl(enc, |
68 | 6.65k | OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray( |
69 | 6.65k | {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND, |
70 | 6.65k | OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND, |
71 | 6.65k | OPUS_BANDWIDTH_FULLBAND}))); |
72 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool())); |
73 | 6.65k | opus_encoder_ctl( |
74 | 6.65k | enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100))); |
75 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool())); |
76 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24))); |
77 | 6.65k | opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool()))); |
78 | 6.65k | opus_encoder_ctl(enc, |
79 | 6.65k | OPUS_SET_SIGNAL(fdp.PickValueInArray( |
80 | 6.65k | {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC}))); |
81 | 6.65k | opus_encoder_ctl(enc, |
82 | 6.65k | OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool())))); |
83 | | #ifdef ENABLE_QEXT |
84 | | opus_encoder_ctl(enc, OPUS_SET_QEXT(fdp.PickValueInArray({0, 1}))); |
85 | | #endif |
86 | | |
87 | 6.65k | fdp.ConsumeData(inbuf, sizeof(inbuf)); |
88 | | |
89 | 6.65k | size_t samp_count = 0; |
90 | 77.2M | do { |
91 | 77.2M | const int frame_size_samples = frame_size * sampling_rate / 2000; |
92 | | |
93 | 77.2M | const int len = opus_encode(enc, &inbuf[samp_count * num_channels], |
94 | 77.2M | frame_size_samples, packet, MAX_PACKET); |
95 | 77.2M | if (len < 0 || len > MAX_PACKET) { |
96 | 0 | break; |
97 | 0 | } |
98 | 77.2M | samp_count += frame_size; |
99 | 77.2M | } while (samp_count < ((SAMPLES / 2) - MAX_FRAME_SAMP)); |
100 | | |
101 | 0 | opus_encoder_destroy(enc); |
102 | | |
103 | 6.65k | return 0; |
104 | 6.65k | } |