Coverage Report

Created: 2026-01-10 07:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/tests/opus_projection_encoder_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_projection.h"
24
#include "opus_types.h"
25
#include "../celt/os_support.h"
26
27
#include "opus_ossfuzz_utils.h"
28
29
61.2k
#define MAX_PACKET (1500)
30
static unsigned char out[MAX_PACKET];
31
32
63.0k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
63.0k
  FuzzedDataProvider fdp(data, size);
34
35
63.0k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
63.0k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
63.0k
                                                     ARG_QEXT(96)}) * 1000;
38
63.0k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
63.0k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
63.0k
  int frame_size_ms_x2 =
41
63.0k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
63.0k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
63.0k
  int application =
44
63.0k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
63.0k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
48
63.0k
  int err = OPUS_OK;
49
63.0k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
50
63.0k
  OpusProjectionEncoder *enc = opus_projection_ambisonics_encoder_create(
51
63.0k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
52
63.0k
        application, &err);
53
54
63.0k
  if (err != OPUS_OK || enc == NULL) {
55
356
    opus_projection_encoder_destroy(enc);
56
356
    return 0;
57
356
  }
58
59
62.6k
  opus_projection_encoder_ctl(
60
62.6k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
61
62.6k
  opus_projection_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
62
62.6k
  opus_projection_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
63
62.6k
  opus_projection_encoder_ctl(
64
62.6k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
65
62.6k
  opus_projection_encoder_ctl(
66
62.6k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
67
62.6k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
68
62.6k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
69
62.6k
                OPUS_BANDWIDTH_FULLBAND})));
70
62.6k
  opus_projection_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
71
62.6k
  opus_projection_encoder_ctl(
72
62.6k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
73
62.6k
  opus_projection_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
74
62.6k
  opus_projection_encoder_ctl(
75
62.6k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
76
62.6k
  opus_projection_encoder_ctl(
77
62.6k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
78
62.6k
  opus_projection_encoder_ctl(
79
62.6k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
80
62.6k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
81
62.6k
  opus_projection_encoder_ctl(
82
62.6k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
83
84
62.6k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
85
62.6k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
86
62.6k
  if (pcm == NULL) {
87
0
    opus_projection_encoder_destroy(enc);
88
0
    return 0;
89
0
  }
90
62.6k
  memset(pcm, 0, pcm_size);
91
92
62.6k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
93
61.2k
    const int len =
94
61.2k
        opus_projection_encode(enc, pcm, frame_size, out, MAX_PACKET);
95
61.2k
    (void)len;
96
61.2k
  }
97
98
62.6k
  opus_free(pcm);
99
62.6k
  opus_projection_encoder_destroy(enc);
100
101
62.6k
  return 0;
102
62.6k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
31.5k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
31.5k
  FuzzedDataProvider fdp(data, size);
34
35
31.5k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
31.5k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
31.5k
                                                     ARG_QEXT(96)}) * 1000;
38
31.5k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
31.5k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
31.5k
  int frame_size_ms_x2 =
41
31.5k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
31.5k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
31.5k
  int application =
44
31.5k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
31.5k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
48
31.5k
  int err = OPUS_OK;
49
31.5k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
50
31.5k
  OpusProjectionEncoder *enc = opus_projection_ambisonics_encoder_create(
51
31.5k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
52
31.5k
        application, &err);
53
54
31.5k
  if (err != OPUS_OK || enc == NULL) {
55
178
    opus_projection_encoder_destroy(enc);
56
178
    return 0;
57
178
  }
58
59
31.3k
  opus_projection_encoder_ctl(
60
31.3k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
61
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
62
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
63
31.3k
  opus_projection_encoder_ctl(
64
31.3k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
65
31.3k
  opus_projection_encoder_ctl(
66
31.3k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
67
31.3k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
68
31.3k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
69
31.3k
                OPUS_BANDWIDTH_FULLBAND})));
70
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
71
31.3k
  opus_projection_encoder_ctl(
72
31.3k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
73
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
74
31.3k
  opus_projection_encoder_ctl(
75
31.3k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
76
31.3k
  opus_projection_encoder_ctl(
77
31.3k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
78
31.3k
  opus_projection_encoder_ctl(
79
31.3k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
80
31.3k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
81
31.3k
  opus_projection_encoder_ctl(
82
31.3k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
83
84
31.3k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
85
31.3k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
86
31.3k
  if (pcm == NULL) {
87
0
    opus_projection_encoder_destroy(enc);
88
0
    return 0;
89
0
  }
90
31.3k
  memset(pcm, 0, pcm_size);
91
92
31.3k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
93
30.6k
    const int len =
94
30.6k
        opus_projection_encode(enc, pcm, frame_size, out, MAX_PACKET);
95
30.6k
    (void)len;
96
30.6k
  }
97
98
31.3k
  opus_free(pcm);
99
31.3k
  opus_projection_encoder_destroy(enc);
100
101
31.3k
  return 0;
102
31.3k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
31.5k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
31.5k
  FuzzedDataProvider fdp(data, size);
34
35
31.5k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
31.5k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
31.5k
                                                     ARG_QEXT(96)}) * 1000;
38
31.5k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
31.5k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
31.5k
  int frame_size_ms_x2 =
41
31.5k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
31.5k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
31.5k
  int application =
44
31.5k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
31.5k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
48
31.5k
  int err = OPUS_OK;
49
31.5k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
50
31.5k
  OpusProjectionEncoder *enc = opus_projection_ambisonics_encoder_create(
51
31.5k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
52
31.5k
        application, &err);
53
54
31.5k
  if (err != OPUS_OK || enc == NULL) {
55
178
    opus_projection_encoder_destroy(enc);
56
178
    return 0;
57
178
  }
58
59
31.3k
  opus_projection_encoder_ctl(
60
31.3k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
61
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
62
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
63
31.3k
  opus_projection_encoder_ctl(
64
31.3k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
65
31.3k
  opus_projection_encoder_ctl(
66
31.3k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
67
31.3k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
68
31.3k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
69
31.3k
                OPUS_BANDWIDTH_FULLBAND})));
70
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
71
31.3k
  opus_projection_encoder_ctl(
72
31.3k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
73
31.3k
  opus_projection_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
74
31.3k
  opus_projection_encoder_ctl(
75
31.3k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
76
31.3k
  opus_projection_encoder_ctl(
77
31.3k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
78
31.3k
  opus_projection_encoder_ctl(
79
31.3k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
80
31.3k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
81
31.3k
  opus_projection_encoder_ctl(
82
31.3k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
83
84
31.3k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
85
31.3k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
86
31.3k
  if (pcm == NULL) {
87
0
    opus_projection_encoder_destroy(enc);
88
0
    return 0;
89
0
  }
90
31.3k
  memset(pcm, 0, pcm_size);
91
92
31.3k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
93
30.6k
    const int len =
94
30.6k
        opus_projection_encode(enc, pcm, frame_size, out, MAX_PACKET);
95
30.6k
    (void)len;
96
30.6k
  }
97
98
31.3k
  opus_free(pcm);
99
31.3k
  opus_projection_encoder_destroy(enc);
100
101
31.3k
  return 0;
102
31.3k
}