Coverage Report

Created: 2026-08-13 07:15

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