Coverage Report

Created: 2026-01-17 07:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
75.7k
#define MAX_PACKET (1500)
30
static unsigned char out[MAX_PACKET];
31
32
79.4k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
79.4k
  FuzzedDataProvider fdp(data, size);
34
35
79.4k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
79.4k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
79.4k
                                                     ARG_QEXT(96)}) * 1000;
38
79.4k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
79.4k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
79.4k
  int frame_size_ms_x2 =
41
79.4k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
79.4k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
79.4k
  int application =
44
79.4k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
79.4k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
79.4k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
48
79.4k
  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.4k
  int err = OPUS_OK;
56
79.4k
  OpusMSEncoder *enc = NULL;
57
79.4k
  if (fdp.ConsumeBool()) {
58
56.8k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
59
56.8k
    enc = opus_multistream_surround_encoder_create(
60
56.8k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
61
56.8k
        mapping, application, &err);
62
56.8k
  } else {
63
22.5k
    enc = opus_multistream_encoder_create(frequency, nb_channels, streams,
64
22.5k
                                          coupled_streams, mapping, application,
65
22.5k
                                          &err);
66
22.5k
  }
67
79.4k
  free(mapping);
68
79.4k
  if (err != OPUS_OK || enc == NULL) {
69
1.54k
    opus_multistream_encoder_destroy(enc);
70
1.54k
    return 0;
71
1.54k
  }
72
73
77.9k
  opus_multistream_encoder_ctl(
74
77.9k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
75
77.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
76
77.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
77
77.9k
  opus_multistream_encoder_ctl(
78
77.9k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
79
77.9k
  opus_multistream_encoder_ctl(
80
77.9k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
81
77.9k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
82
77.9k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
83
77.9k
                OPUS_BANDWIDTH_FULLBAND})));
84
77.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
85
77.9k
  opus_multistream_encoder_ctl(
86
77.9k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
87
77.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
88
77.9k
  opus_multistream_encoder_ctl(
89
77.9k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
90
77.9k
  opus_multistream_encoder_ctl(
91
77.9k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
92
77.9k
  opus_multistream_encoder_ctl(
93
77.9k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
94
77.9k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
95
77.9k
  opus_multistream_encoder_ctl(
96
77.9k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
97
98
77.9k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
99
77.9k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
100
77.9k
  if (pcm == NULL) {
101
0
    opus_multistream_encoder_destroy(enc);
102
0
    return 0;
103
0
  }
104
77.9k
  memset(pcm, 0, pcm_size);
105
106
77.9k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
107
75.7k
    const int len =
108
75.7k
        opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET);
109
75.7k
    (void)len;
110
75.7k
  }
111
112
77.9k
  opus_free(pcm);
113
77.9k
  opus_multistream_encoder_destroy(enc);
114
115
77.9k
  return 0;
116
77.9k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
39.7k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
39.7k
  FuzzedDataProvider fdp(data, size);
34
35
39.7k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
39.7k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
39.7k
                                                     ARG_QEXT(96)}) * 1000;
38
39.7k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
39.7k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
39.7k
  int frame_size_ms_x2 =
41
39.7k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
39.7k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
39.7k
  int application =
44
39.7k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
39.7k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
39.7k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
48
39.7k
  if (!mapping) {
49
0
    return 0;
50
0
  }
51
542k
  for (unsigned char x = 0; x < nb_channels; ++x) {
52
502k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
53
502k
  }
54
55
39.7k
  int err = OPUS_OK;
56
39.7k
  OpusMSEncoder *enc = NULL;
57
39.7k
  if (fdp.ConsumeBool()) {
58
28.4k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
59
28.4k
    enc = opus_multistream_surround_encoder_create(
60
28.4k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
61
28.4k
        mapping, application, &err);
62
28.4k
  } else {
63
11.2k
    enc = opus_multistream_encoder_create(frequency, nb_channels, streams,
64
11.2k
                                          coupled_streams, mapping, application,
65
11.2k
                                          &err);
66
11.2k
  }
67
39.7k
  free(mapping);
68
39.7k
  if (err != OPUS_OK || enc == NULL) {
69
774
    opus_multistream_encoder_destroy(enc);
70
774
    return 0;
71
774
  }
72
73
38.9k
  opus_multistream_encoder_ctl(
74
38.9k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
75
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
76
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
77
38.9k
  opus_multistream_encoder_ctl(
78
38.9k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
79
38.9k
  opus_multistream_encoder_ctl(
80
38.9k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
81
38.9k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
82
38.9k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
83
38.9k
                OPUS_BANDWIDTH_FULLBAND})));
84
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
85
38.9k
  opus_multistream_encoder_ctl(
86
38.9k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
87
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
88
38.9k
  opus_multistream_encoder_ctl(
89
38.9k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
90
38.9k
  opus_multistream_encoder_ctl(
91
38.9k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
92
38.9k
  opus_multistream_encoder_ctl(
93
38.9k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
94
38.9k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
95
38.9k
  opus_multistream_encoder_ctl(
96
38.9k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
97
98
38.9k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
99
38.9k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
100
38.9k
  if (pcm == NULL) {
101
0
    opus_multistream_encoder_destroy(enc);
102
0
    return 0;
103
0
  }
104
38.9k
  memset(pcm, 0, pcm_size);
105
106
38.9k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
107
37.8k
    const int len =
108
37.8k
        opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET);
109
37.8k
    (void)len;
110
37.8k
  }
111
112
38.9k
  opus_free(pcm);
113
38.9k
  opus_multistream_encoder_destroy(enc);
114
115
38.9k
  return 0;
116
38.9k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
39.7k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
39.7k
  FuzzedDataProvider fdp(data, size);
34
35
39.7k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
39.7k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
39.7k
                                                     ARG_QEXT(96)}) * 1000;
38
39.7k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
39.7k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
39.7k
  int frame_size_ms_x2 =
41
39.7k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
39.7k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
39.7k
  int application =
44
39.7k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
39.7k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
39.7k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
48
39.7k
  if (!mapping) {
49
0
    return 0;
50
0
  }
51
542k
  for (unsigned char x = 0; x < nb_channels; ++x) {
52
502k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
53
502k
  }
54
55
39.7k
  int err = OPUS_OK;
56
39.7k
  OpusMSEncoder *enc = NULL;
57
39.7k
  if (fdp.ConsumeBool()) {
58
28.4k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
59
28.4k
    enc = opus_multistream_surround_encoder_create(
60
28.4k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
61
28.4k
        mapping, application, &err);
62
28.4k
  } else {
63
11.2k
    enc = opus_multistream_encoder_create(frequency, nb_channels, streams,
64
11.2k
                                          coupled_streams, mapping, application,
65
11.2k
                                          &err);
66
11.2k
  }
67
39.7k
  free(mapping);
68
39.7k
  if (err != OPUS_OK || enc == NULL) {
69
774
    opus_multistream_encoder_destroy(enc);
70
774
    return 0;
71
774
  }
72
73
38.9k
  opus_multistream_encoder_ctl(
74
38.9k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
75
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
76
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
77
38.9k
  opus_multistream_encoder_ctl(
78
38.9k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
79
38.9k
  opus_multistream_encoder_ctl(
80
38.9k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
81
38.9k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
82
38.9k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
83
38.9k
                OPUS_BANDWIDTH_FULLBAND})));
84
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
85
38.9k
  opus_multistream_encoder_ctl(
86
38.9k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
87
38.9k
  opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
88
38.9k
  opus_multistream_encoder_ctl(
89
38.9k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
90
38.9k
  opus_multistream_encoder_ctl(
91
38.9k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
92
38.9k
  opus_multistream_encoder_ctl(
93
38.9k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
94
38.9k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
95
38.9k
  opus_multistream_encoder_ctl(
96
38.9k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
97
98
38.9k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
99
38.9k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
100
38.9k
  if (pcm == NULL) {
101
0
    opus_multistream_encoder_destroy(enc);
102
0
    return 0;
103
0
  }
104
38.9k
  memset(pcm, 0, pcm_size);
105
106
38.9k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
107
37.8k
    const int len =
108
37.8k
        opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET);
109
37.8k
    (void)len;
110
37.8k
  }
111
112
38.9k
  opus_free(pcm);
113
38.9k
  opus_multistream_encoder_destroy(enc);
114
115
38.9k
  return 0;
116
38.9k
}