Coverage Report

Created: 2023-09-25 08:12

/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
107M
#define MAX_PACKET (1500)
25
53.7M
#define SAMPLES (48000 * 10)
26
53.7M
#define MAX_FRAME_SAMP (5760)
27
28
static const int sampling_rates[] = {8000, 12000, 16000, 24000, 48000};
29
static const int channels[] = {1, 2};
30
static const int applications[] = {OPUS_APPLICATION_AUDIO,
31
                                   OPUS_APPLICATION_VOIP,
32
                                   OPUS_APPLICATION_RESTRICTED_LOWDELAY};
33
static const int frame_sizes_ms_x2[] = {5, 10, 20, 40, 80, 120, 160, 200, 240};
34
35
static opus_int16 inbuf[sizeof(opus_int16) * SAMPLES] = {0};
36
static unsigned char packet[MAX_PACKET + 257];
37
38
4.67k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
39
4.67k
  FuzzedDataProvider fdp(data, size);
40
41
4.67k
  int err = OPUS_OK;
42
4.67k
  int num_channels = fdp.PickValueInArray(channels);
43
4.67k
  int frame_size = fdp.PickValueInArray(frame_sizes_ms_x2);
44
4.67k
  int sampling_rate = fdp.PickValueInArray(sampling_rates);
45
4.67k
  int application = fdp.PickValueInArray(applications);
46
47
48
4.67k
  OpusEncoder *enc =
49
4.67k
      opus_encoder_create(sampling_rate, num_channels, application, &err);
50
4.67k
  if (err != OPUS_OK || enc == NULL) {
51
0
    opus_encoder_destroy(enc);
52
0
    return 0;
53
0
  }
54
55
4.67k
  opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
56
4.67k
  opus_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
57
4.67k
  opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
58
4.67k
  opus_encoder_ctl(
59
4.67k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
60
4.67k
  opus_encoder_ctl(enc,
61
4.67k
                   OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
62
4.67k
                       {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
63
4.67k
                        OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
64
4.67k
                        OPUS_BANDWIDTH_FULLBAND})));
65
4.67k
  opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
66
4.67k
  opus_encoder_ctl(
67
4.67k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
68
4.67k
  opus_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
69
4.67k
  opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
70
4.67k
  opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
71
4.67k
  opus_encoder_ctl(enc,
72
4.67k
                   OPUS_SET_SIGNAL(fdp.PickValueInArray(
73
4.67k
                       {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
74
4.67k
  opus_encoder_ctl(enc,
75
4.67k
                   OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
76
77
4.67k
  fdp.ConsumeData(inbuf, sizeof(inbuf));
78
79
4.67k
  size_t samp_count = 0;
80
53.7M
  do {
81
53.7M
    const int frame_size_samples = frame_size * sampling_rate / 2000;
82
83
53.7M
    const int len = opus_encode(enc, &inbuf[samp_count * num_channels],
84
53.7M
                                frame_size_samples, packet, MAX_PACKET);
85
53.7M
    if (len < 0 || len > MAX_PACKET) {
86
0
      break;
87
0
    }
88
53.7M
    samp_count += frame_size;
89
53.7M
  } while (samp_count < ((SAMPLES / 2) - MAX_FRAME_SAMP));
90
91
0
  opus_encoder_destroy(enc);
92
93
4.67k
  return 0;
94
4.67k
}