/src/opus/tests/opus_multistream_encode_fuzzer.cc
Line | Count | Source |
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 <math.h> |
17 | | #include <stdint.h> |
18 | | #include <stdlib.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include "opus.h" |
22 | | #include "opus_defines.h" |
23 | | #include "opus_multistream.h" |
24 | | #include "opus_types.h" |
25 | | #include "../celt/os_support.h" |
26 | | |
27 | | #include "opus_ossfuzz_utils.h" |
28 | | |
29 | 76.2k | #define MAX_PACKET (1500) |
30 | | static unsigned char out[MAX_PACKET]; |
31 | | |
32 | 79.9k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
33 | 79.9k | FuzzedDataProvider fdp(data, size); |
34 | | |
35 | 79.9k | opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255); |
36 | 79.9k | const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48 |
37 | 79.9k | ARG_QEXT(96)}) * 1000; |
38 | 79.9k | int streams = fdp.ConsumeIntegralInRange(0, 255); |
39 | 79.9k | int coupled_streams = fdp.ConsumeIntegralInRange(0, 255); |
40 | 79.9k | int frame_size_ms_x2 = |
41 | 79.9k | fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240}); |
42 | 79.9k | int frame_size = frame_size_ms_x2 * frequency / 2000; |
43 | 79.9k | int application = |
44 | 79.9k | fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, |
45 | 79.9k | OPUS_APPLICATION_RESTRICTED_LOWDELAY}); |
46 | | |
47 | 79.9k | unsigned char *mapping = (unsigned char *)malloc(nb_channels); |
48 | 79.9k | if (!mapping) { |
49 | 0 | return 0; |
50 | 0 | } |
51 | 1.08M | for (unsigned char x = 0; x < nb_channels; ++x) { |
52 | 1.00M | mapping[x] = fdp.ConsumeIntegralInRange(0, 255); |
53 | 1.00M | } |
54 | | |
55 | 79.9k | int err = OPUS_OK; |
56 | 79.9k | OpusMSEncoder *enc = NULL; |
57 | 79.9k | if (fdp.ConsumeBool()) { |
58 | 57.5k | int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255}); |
59 | 57.5k | enc = opus_multistream_surround_encoder_create( |
60 | 57.5k | frequency, nb_channels, mapping_family, &streams, &coupled_streams, |
61 | 57.5k | mapping, application, &err); |
62 | 57.5k | } else { |
63 | 22.3k | enc = opus_multistream_encoder_create(frequency, nb_channels, streams, |
64 | 22.3k | coupled_streams, mapping, application, |
65 | 22.3k | &err); |
66 | 22.3k | } |
67 | 79.9k | free(mapping); |
68 | 79.9k | if (err != OPUS_OK || enc == NULL) { |
69 | 1.53k | opus_multistream_encoder_destroy(enc); |
70 | 1.53k | return 0; |
71 | 1.53k | } |
72 | | |
73 | 78.3k | opus_multistream_encoder_ctl( |
74 | 78.3k | enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10))); |
75 | 78.3k | opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool())); |
76 | 78.3k | opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool())); |
77 | 78.3k | opus_multistream_encoder_ctl( |
78 | 78.3k | enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2}))); |
79 | 78.3k | opus_multistream_encoder_ctl( |
80 | 78.3k | enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray( |
81 | 78.3k | {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND, |
82 | 78.3k | OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND, |
83 | 78.3k | OPUS_BANDWIDTH_FULLBAND}))); |
84 | 78.3k | opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool())); |
85 | 78.3k | opus_multistream_encoder_ctl( |
86 | 78.3k | enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100))); |
87 | 78.3k | opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool())); |
88 | 78.3k | opus_multistream_encoder_ctl( |
89 | 78.3k | enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24))); |
90 | 78.3k | opus_multistream_encoder_ctl( |
91 | 78.3k | enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool()))); |
92 | 78.3k | opus_multistream_encoder_ctl( |
93 | 78.3k | enc, OPUS_SET_SIGNAL(fdp.PickValueInArray( |
94 | 78.3k | {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC}))); |
95 | 78.3k | opus_multistream_encoder_ctl( |
96 | 78.3k | enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool())))); |
97 | | |
98 | 78.3k | const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels; |
99 | 78.3k | opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size); |
100 | 78.3k | if (pcm == NULL) { |
101 | 0 | opus_multistream_encoder_destroy(enc); |
102 | 0 | return 0; |
103 | 0 | } |
104 | 78.3k | memset(pcm, 0, pcm_size); |
105 | | |
106 | 78.3k | if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) { |
107 | 76.2k | const int len = |
108 | 76.2k | opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET); |
109 | 76.2k | (void)len; |
110 | 76.2k | } |
111 | | |
112 | 78.3k | opus_free(pcm); |
113 | 78.3k | opus_multistream_encoder_destroy(enc); |
114 | | |
115 | 78.3k | return 0; |
116 | 78.3k | } Line | Count | Source | 32 | 39.9k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 33 | 39.9k | FuzzedDataProvider fdp(data, size); | 34 | | | 35 | 39.9k | opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255); | 36 | 39.9k | const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48 | 37 | 39.9k | ARG_QEXT(96)}) * 1000; | 38 | 39.9k | int streams = fdp.ConsumeIntegralInRange(0, 255); | 39 | 39.9k | int coupled_streams = fdp.ConsumeIntegralInRange(0, 255); | 40 | 39.9k | int frame_size_ms_x2 = | 41 | 39.9k | fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240}); | 42 | 39.9k | int frame_size = frame_size_ms_x2 * frequency / 2000; | 43 | 39.9k | int application = | 44 | 39.9k | fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, | 45 | 39.9k | OPUS_APPLICATION_RESTRICTED_LOWDELAY}); | 46 | | | 47 | 39.9k | unsigned char *mapping = (unsigned char *)malloc(nb_channels); | 48 | 39.9k | if (!mapping) { | 49 | 0 | return 0; | 50 | 0 | } | 51 | 544k | for (unsigned char x = 0; x < nb_channels; ++x) { | 52 | 504k | mapping[x] = fdp.ConsumeIntegralInRange(0, 255); | 53 | 504k | } | 54 | | | 55 | 39.9k | int err = OPUS_OK; | 56 | 39.9k | OpusMSEncoder *enc = NULL; | 57 | 39.9k | if (fdp.ConsumeBool()) { | 58 | 28.7k | int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255}); | 59 | 28.7k | enc = opus_multistream_surround_encoder_create( | 60 | 28.7k | frequency, nb_channels, mapping_family, &streams, &coupled_streams, | 61 | 28.7k | mapping, application, &err); | 62 | 28.7k | } else { | 63 | 11.1k | enc = opus_multistream_encoder_create(frequency, nb_channels, streams, | 64 | 11.1k | coupled_streams, mapping, application, | 65 | 11.1k | &err); | 66 | 11.1k | } | 67 | 39.9k | free(mapping); | 68 | 39.9k | if (err != OPUS_OK || enc == NULL) { | 69 | 769 | opus_multistream_encoder_destroy(enc); | 70 | 769 | return 0; | 71 | 769 | } | 72 | | | 73 | 39.1k | opus_multistream_encoder_ctl( | 74 | 39.1k | enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10))); | 75 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool())); | 76 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool())); | 77 | 39.1k | opus_multistream_encoder_ctl( | 78 | 39.1k | enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2}))); | 79 | 39.1k | opus_multistream_encoder_ctl( | 80 | 39.1k | enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray( | 81 | 39.1k | {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND, | 82 | 39.1k | OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND, | 83 | 39.1k | OPUS_BANDWIDTH_FULLBAND}))); | 84 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool())); | 85 | 39.1k | opus_multistream_encoder_ctl( | 86 | 39.1k | enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100))); | 87 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool())); | 88 | 39.1k | opus_multistream_encoder_ctl( | 89 | 39.1k | enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24))); | 90 | 39.1k | opus_multistream_encoder_ctl( | 91 | 39.1k | enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool()))); | 92 | 39.1k | opus_multistream_encoder_ctl( | 93 | 39.1k | enc, OPUS_SET_SIGNAL(fdp.PickValueInArray( | 94 | 39.1k | {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC}))); | 95 | 39.1k | opus_multistream_encoder_ctl( | 96 | 39.1k | enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool())))); | 97 | | | 98 | 39.1k | const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels; | 99 | 39.1k | opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size); | 100 | 39.1k | if (pcm == NULL) { | 101 | 0 | opus_multistream_encoder_destroy(enc); | 102 | 0 | return 0; | 103 | 0 | } | 104 | 39.1k | memset(pcm, 0, pcm_size); | 105 | | | 106 | 39.1k | if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) { | 107 | 38.1k | const int len = | 108 | 38.1k | opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET); | 109 | 38.1k | (void)len; | 110 | 38.1k | } | 111 | | | 112 | 39.1k | opus_free(pcm); | 113 | 39.1k | opus_multistream_encoder_destroy(enc); | 114 | | | 115 | 39.1k | return 0; | 116 | 39.1k | } |
Line | Count | Source | 32 | 39.9k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | 33 | 39.9k | FuzzedDataProvider fdp(data, size); | 34 | | | 35 | 39.9k | opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255); | 36 | 39.9k | const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48 | 37 | 39.9k | ARG_QEXT(96)}) * 1000; | 38 | 39.9k | int streams = fdp.ConsumeIntegralInRange(0, 255); | 39 | 39.9k | int coupled_streams = fdp.ConsumeIntegralInRange(0, 255); | 40 | 39.9k | int frame_size_ms_x2 = | 41 | 39.9k | fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240}); | 42 | 39.9k | int frame_size = frame_size_ms_x2 * frequency / 2000; | 43 | 39.9k | int application = | 44 | 39.9k | fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP, | 45 | 39.9k | OPUS_APPLICATION_RESTRICTED_LOWDELAY}); | 46 | | | 47 | 39.9k | unsigned char *mapping = (unsigned char *)malloc(nb_channels); | 48 | 39.9k | if (!mapping) { | 49 | 0 | return 0; | 50 | 0 | } | 51 | 544k | for (unsigned char x = 0; x < nb_channels; ++x) { | 52 | 504k | mapping[x] = fdp.ConsumeIntegralInRange(0, 255); | 53 | 504k | } | 54 | | | 55 | 39.9k | int err = OPUS_OK; | 56 | 39.9k | OpusMSEncoder *enc = NULL; | 57 | 39.9k | if (fdp.ConsumeBool()) { | 58 | 28.7k | int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255}); | 59 | 28.7k | enc = opus_multistream_surround_encoder_create( | 60 | 28.7k | frequency, nb_channels, mapping_family, &streams, &coupled_streams, | 61 | 28.7k | mapping, application, &err); | 62 | 28.7k | } else { | 63 | 11.1k | enc = opus_multistream_encoder_create(frequency, nb_channels, streams, | 64 | 11.1k | coupled_streams, mapping, application, | 65 | 11.1k | &err); | 66 | 11.1k | } | 67 | 39.9k | free(mapping); | 68 | 39.9k | if (err != OPUS_OK || enc == NULL) { | 69 | 769 | opus_multistream_encoder_destroy(enc); | 70 | 769 | return 0; | 71 | 769 | } | 72 | | | 73 | 39.1k | opus_multistream_encoder_ctl( | 74 | 39.1k | enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10))); | 75 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool())); | 76 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool())); | 77 | 39.1k | opus_multistream_encoder_ctl( | 78 | 39.1k | enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2}))); | 79 | 39.1k | opus_multistream_encoder_ctl( | 80 | 39.1k | enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray( | 81 | 39.1k | {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND, | 82 | 39.1k | OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND, | 83 | 39.1k | OPUS_BANDWIDTH_FULLBAND}))); | 84 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool())); | 85 | 39.1k | opus_multistream_encoder_ctl( | 86 | 39.1k | enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100))); | 87 | 39.1k | opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool())); | 88 | 39.1k | opus_multistream_encoder_ctl( | 89 | 39.1k | enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24))); | 90 | 39.1k | opus_multistream_encoder_ctl( | 91 | 39.1k | enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool()))); | 92 | 39.1k | opus_multistream_encoder_ctl( | 93 | 39.1k | enc, OPUS_SET_SIGNAL(fdp.PickValueInArray( | 94 | 39.1k | {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC}))); | 95 | 39.1k | opus_multistream_encoder_ctl( | 96 | 39.1k | enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool())))); | 97 | | | 98 | 39.1k | const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels; | 99 | 39.1k | opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size); | 100 | 39.1k | if (pcm == NULL) { | 101 | 0 | opus_multistream_encoder_destroy(enc); | 102 | 0 | return 0; | 103 | 0 | } | 104 | 39.1k | memset(pcm, 0, pcm_size); | 105 | | | 106 | 39.1k | if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) { | 107 | 38.1k | const int len = | 108 | 38.1k | opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET); | 109 | 38.1k | (void)len; | 110 | 38.1k | } | 111 | | | 112 | 39.1k | opus_free(pcm); | 113 | 39.1k | opus_multistream_encoder_destroy(enc); | 114 | | | 115 | 39.1k | return 0; | 116 | 39.1k | } |
|