Coverage Report

Created: 2025-07-18 07:17

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