Coverage Report

Created: 2026-05-30 07:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libultrahdr/fuzzer/ultrahdr_legacy_fuzzer.cpp
Line
Count
Source
1
/*
2
 * Copyright 2023 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7
 * option. This file may not be copied, modified, or distributed
8
 * except according to those terms.
9
 */
10
11
#include <fuzzer/FuzzedDataProvider.h>
12
#include <algorithm>
13
#include <iostream>
14
#include <memory>
15
#include <random>
16
17
#include "ultrahdr/ultrahdrcommon.h"
18
#include "ultrahdr/gainmapmath.h"
19
#include "ultrahdr/jpegr.h"
20
21
using namespace ultrahdr;
22
23
// Color gamuts for image data, sync with ultrahdr.h
24
const int kCgMin = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
25
const int kCgMax = ULTRAHDR_COLORGAMUT_BT2100;
26
27
// Transfer functions for image data, sync with ultrahdr.h
28
const int kTfMin = ULTRAHDR_TF_UNSPECIFIED;
29
const int kTfMax = ULTRAHDR_TF_SRGB;
30
31
// Transfer functions for image data, sync with ultrahdr.h
32
const int kOfMin = ULTRAHDR_OUTPUT_UNSPECIFIED;
33
const int kOfMax = ULTRAHDR_OUTPUT_HDR_HLG;
34
35
// quality factor
36
const int kQfMin = -10;
37
const int kQfMax = 110;
38
39
class UltraHdrEncFuzzer {
40
 public:
41
0
  UltraHdrEncFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {};
42
  void process();
43
  template <typename T>
44
  void fillBuffer(T* data, int width, int height, int stride);
45
46
 private:
47
  FuzzedDataProvider mFdp;
48
};
49
50
template <typename T>
51
0
void UltraHdrEncFuzzer::fillBuffer(T* data, int width, int height, int stride) {
52
0
  if (!mFdp.remaining_bytes()) return;
53
54
0
  T* tmp = data;
55
0
  std::vector<T> buffer(width);
56
0
  for (int i = 0; i < buffer.size(); i++) {
57
0
    buffer[i] = mFdp.ConsumeIntegral<T>();
58
0
  }
59
0
  for (int j = 0; j < height; j++) {
60
0
    for (int i = 0; i < width; i += buffer.size()) {
61
0
      memcpy(tmp + i, buffer.data(), std::min((int)buffer.size(), (width - i)) * sizeof(*data));
62
0
      std::shuffle(buffer.begin(), buffer.end(),
63
0
                   std::default_random_engine(std::random_device{}()));
64
0
    }
65
0
    tmp += stride;
66
0
  }
67
0
}
Unexecuted instantiation: void UltraHdrEncFuzzer::fillBuffer<unsigned short>(unsigned short*, int, int, int)
Unexecuted instantiation: void UltraHdrEncFuzzer::fillBuffer<unsigned char>(unsigned char*, int, int, int)
68
69
0
void UltraHdrEncFuzzer::process() {
70
0
  if (mFdp.remaining_bytes()) {
71
0
    struct jpegr_uncompressed_struct p010Img{};
72
0
    struct jpegr_uncompressed_struct yuv420Img{};
73
0
    struct jpegr_uncompressed_struct grayImg{};
74
0
    struct jpegr_compressed_struct jpegImgR{};
75
0
    struct jpegr_compressed_struct jpegImg{};
76
0
    struct jpegr_compressed_struct jpegGainMap{};
77
78
    // which encode api to select
79
0
    int muxSwitch = mFdp.ConsumeIntegralInRange<int>(0, 4);
80
81
    // quality factor
82
0
    int quality = mFdp.ConsumeIntegralInRange<int>(kQfMin, kQfMax);
83
84
    // hdr_tf
85
0
    auto tf =
86
0
        static_cast<ultrahdr_transfer_function>(mFdp.ConsumeIntegralInRange<int>(kTfMin, kTfMax));
87
88
    // p010 Cg
89
0
    auto p010Cg =
90
0
        static_cast<ultrahdr_color_gamut>(mFdp.ConsumeIntegralInRange<int>(kCgMin, kCgMax));
91
92
    // 420 Cg
93
0
    auto yuv420Cg =
94
0
        static_cast<ultrahdr_color_gamut>(mFdp.ConsumeIntegralInRange<int>(kCgMin, kCgMax));
95
96
    // hdr_of
97
0
    auto of = static_cast<ultrahdr_output_format>(mFdp.ConsumeIntegralInRange<int>(kOfMin, kOfMax));
98
99
0
    int width = mFdp.ConsumeIntegralInRange<int>(kMinWidth, kMaxWidth);
100
0
    width = (width >> 1) << 1;
101
102
0
    int height = mFdp.ConsumeIntegralInRange<int>(kMinHeight, kMaxHeight);
103
0
    height = (height >> 1) << 1;
104
105
    // gain_map quality factor
106
0
    auto gainmap_quality = mFdp.ConsumeIntegral<int8_t>();
107
108
    // multi channel gainmap
109
0
    auto multi_channel_gainmap = mFdp.ConsumeIntegral<int8_t>();
110
111
    // gainmap scale factor
112
0
    auto gm_scale_factor = mFdp.ConsumeIntegralInRange<int16_t>(-32, 192);
113
114
    // encoding speed preset
115
0
    auto enc_preset = mFdp.ConsumeBool() ? UHDR_USAGE_REALTIME : UHDR_USAGE_BEST_QUALITY;
116
117
    // gainmap metadata
118
0
    auto minBoost = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
119
0
    auto maxBoost = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 64.0f);
120
0
    auto gamma = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 5);
121
0
    auto offsetSdr = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
122
0
    auto offsetHdr = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 1.0f);
123
0
    auto minCapacity = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 48.0f);
124
0
    auto maxCapacity = mFdp.ConsumeFloatingPointInRange<float>(-4.0f, 48.0f);
125
126
    // target display peak brightness
127
0
    auto targetDispPeakBrightness = mFdp.ConsumeFloatingPointInRange<float>(100.0f, 10500.0f);
128
129
    // raw buffer config
130
0
    bool hasP010Stride = mFdp.ConsumeBool();
131
0
    size_t yP010Stride = mFdp.ConsumeIntegralInRange<uint16_t>(width, width + 128);
132
0
    if (!hasP010Stride) yP010Stride = width;
133
0
    bool isP010UVContiguous = mFdp.ConsumeBool();
134
0
    bool hasP010UVStride = mFdp.ConsumeBool();
135
0
    size_t uvP010Stride = mFdp.ConsumeIntegralInRange<uint16_t>(width, width + 128);
136
0
    if (!hasP010UVStride) uvP010Stride = width;
137
138
0
    bool hasYuv420Stride = mFdp.ConsumeBool();
139
0
    size_t yYuv420Stride = mFdp.ConsumeIntegralInRange<uint16_t>(width, width + 128);
140
0
    if (!hasYuv420Stride) yYuv420Stride = width;
141
0
    bool isYuv420UVContiguous = mFdp.ConsumeBool();
142
0
    bool hasYuv420UVStride = mFdp.ConsumeBool();
143
0
    size_t uvYuv420Stride = mFdp.ConsumeIntegralInRange<uint16_t>(width / 2, width / 2 + 128);
144
0
    if (!hasYuv420UVStride) uvYuv420Stride = width / 2;
145
146
    // display boost
147
0
    float displayBoost = mFdp.ConsumeFloatingPointInRange<float>(1.0, FLT_MAX);
148
149
0
    std::unique_ptr<uint16_t[]> bufferYHdr = nullptr;
150
0
    std::unique_ptr<uint16_t[]> bufferUVHdr = nullptr;
151
0
    std::unique_ptr<uint8_t[]> bufferYSdr = nullptr;
152
0
    std::unique_ptr<uint8_t[]> bufferUVSdr = nullptr;
153
0
    std::unique_ptr<uint8_t[]> grayImgRaw = nullptr;
154
0
    if (muxSwitch != 4) {
155
      // init p010 image
156
0
      p010Img.width = width;
157
0
      p010Img.height = height;
158
0
      p010Img.colorGamut = p010Cg;
159
0
      p010Img.luma_stride = yP010Stride;
160
0
      if (isP010UVContiguous) {
161
0
        size_t p010Size = yP010Stride * height * 3 / 2;
162
0
        bufferYHdr = std::make_unique<uint16_t[]>(p010Size);
163
0
        p010Img.data = bufferYHdr.get();
164
0
        p010Img.chroma_data = nullptr;
165
0
        p010Img.chroma_stride = 0;
166
0
        fillBuffer<uint16_t>(bufferYHdr.get(), width, height, yP010Stride);
167
0
        fillBuffer<uint16_t>(bufferYHdr.get() + yP010Stride * height, width, height / 2,
168
0
                             yP010Stride);
169
0
      } else {
170
0
        size_t p010YSize = yP010Stride * height;
171
0
        bufferYHdr = std::make_unique<uint16_t[]>(p010YSize);
172
0
        p010Img.data = bufferYHdr.get();
173
0
        fillBuffer<uint16_t>(bufferYHdr.get(), width, height, yP010Stride);
174
0
        size_t p010UVSize = uvP010Stride * p010Img.height / 2;
175
0
        bufferUVHdr = std::make_unique<uint16_t[]>(p010UVSize);
176
0
        p010Img.chroma_data = bufferUVHdr.get();
177
0
        p010Img.chroma_stride = uvP010Stride;
178
0
        fillBuffer<uint16_t>(bufferUVHdr.get(), width, height / 2, uvP010Stride);
179
0
      }
180
0
    } else {
181
0
      size_t map_width = width / kMapDimensionScaleFactorDefault;
182
0
      size_t map_height = height / kMapDimensionScaleFactorDefault;
183
      // init 400 image
184
0
      grayImg.width = map_width;
185
0
      grayImg.height = map_height;
186
0
      grayImg.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
187
0
      const size_t graySize = map_width * map_height;
188
0
      grayImgRaw = std::make_unique<uint8_t[]>(graySize);
189
0
      grayImg.data = grayImgRaw.get();
190
0
      fillBuffer<uint8_t>(grayImgRaw.get(), map_width, map_height, map_width);
191
0
      grayImg.chroma_data = nullptr;
192
0
      grayImg.luma_stride = 0;
193
0
      grayImg.chroma_stride = 0;
194
0
    }
195
196
0
    if (muxSwitch > 0) {
197
      // init 420 image
198
0
      yuv420Img.width = width;
199
0
      yuv420Img.height = height;
200
0
      yuv420Img.colorGamut = yuv420Cg;
201
0
      yuv420Img.luma_stride = yYuv420Stride;
202
0
      if (isYuv420UVContiguous) {
203
0
        size_t yuv420Size = yYuv420Stride * height * 3 / 2;
204
0
        bufferYSdr = std::make_unique<uint8_t[]>(yuv420Size);
205
0
        yuv420Img.data = bufferYSdr.get();
206
0
        yuv420Img.chroma_data = nullptr;
207
0
        yuv420Img.chroma_stride = 0;
208
0
        fillBuffer<uint8_t>(bufferYSdr.get(), width, height, yYuv420Stride);
209
0
        fillBuffer<uint8_t>(bufferYSdr.get() + yYuv420Stride * height, width / 2, height / 2,
210
0
                            yYuv420Stride / 2);
211
0
        fillBuffer<uint8_t>(bufferYSdr.get() + yYuv420Stride * height * 5 / 4, width / 2,
212
0
                            height / 2, yYuv420Stride / 2);
213
0
      } else {
214
0
        size_t yuv420YSize = yYuv420Stride * height;
215
0
        bufferYSdr = std::make_unique<uint8_t[]>(yuv420YSize);
216
0
        yuv420Img.data = bufferYSdr.get();
217
0
        fillBuffer<uint8_t>(bufferYSdr.get(), width, height, yYuv420Stride);
218
0
        size_t yuv420UVSize = uvYuv420Stride * yuv420Img.height / 2 * 2;
219
0
        bufferUVSdr = std::make_unique<uint8_t[]>(yuv420UVSize);
220
0
        yuv420Img.chroma_data = bufferUVSdr.get();
221
0
        yuv420Img.chroma_stride = uvYuv420Stride;
222
0
        fillBuffer<uint8_t>(bufferUVSdr.get(), width / 2, height / 2, uvYuv420Stride);
223
0
        fillBuffer<uint8_t>(bufferUVSdr.get() + uvYuv420Stride * height / 2, width / 2, height / 2,
224
0
                            uvYuv420Stride);
225
0
      }
226
0
    }
227
228
    // dest
229
    // 2 * p010 size as input data is random, DCT compression might not behave as expected
230
0
    jpegImgR.maxLength = std::max(64 * 1024 /* min size 8kb */, width * height * 3 * 2);
231
0
    auto jpegImgRaw = std::make_unique<uint8_t[]>(jpegImgR.maxLength);
232
0
    jpegImgR.data = jpegImgRaw.get();
233
// #define DUMP_PARAM
234
#ifdef DUMP_PARAM
235
    std::cout << "Api Select " << muxSwitch << std::endl;
236
    std::cout << "image dimensions " << width << " x " << height << std::endl;
237
    std::cout << "p010 color gamut " << p010Img.colorGamut << std::endl;
238
    std::cout << "p010 luma stride " << p010Img.luma_stride << std::endl;
239
    std::cout << "p010 chroma stride " << p010Img.chroma_stride << std::endl;
240
    std::cout << "420 color gamut " << yuv420Img.colorGamut << std::endl;
241
    std::cout << "420 luma stride " << yuv420Img.luma_stride << std::endl;
242
    std::cout << "420 chroma stride " << yuv420Img.chroma_stride << std::endl;
243
    std::cout << "quality factor " << quality << std::endl;
244
#endif
245
0
    JpegR jpegHdr(nullptr, gm_scale_factor, gainmap_quality, multi_channel_gainmap, gamma,
246
0
                  enc_preset, minBoost, maxBoost, targetDispPeakBrightness);
247
0
    status_t status = JPEGR_UNKNOWN_ERROR;
248
0
    if (muxSwitch == 0) {  // api 0
249
0
      jpegImgR.length = 0;
250
0
      status = jpegHdr.encodeJPEGR(&p010Img, tf, &jpegImgR, quality, nullptr);
251
0
    } else if (muxSwitch == 1) {  // api 1
252
0
      jpegImgR.length = 0;
253
0
      status = jpegHdr.encodeJPEGR(&p010Img, &yuv420Img, tf, &jpegImgR, quality, nullptr);
254
0
    } else {
255
      // compressed img
256
0
      JpegEncoderHelper encoder;
257
0
      struct jpegr_uncompressed_struct yuv420ImgCopy = yuv420Img;
258
0
      if (yuv420ImgCopy.luma_stride == 0) yuv420ImgCopy.luma_stride = yuv420Img.width;
259
0
      if (!yuv420ImgCopy.chroma_data) {
260
0
        uint8_t* data = reinterpret_cast<uint8_t*>(yuv420Img.data);
261
0
        yuv420ImgCopy.chroma_data = data + yuv420Img.luma_stride * yuv420Img.height;
262
0
        yuv420ImgCopy.chroma_stride = yuv420Img.luma_stride >> 1;
263
0
      }
264
0
      const uint8_t* planes[3]{reinterpret_cast<uint8_t*>(yuv420ImgCopy.data),
265
0
                               reinterpret_cast<uint8_t*>(yuv420ImgCopy.chroma_data),
266
0
                               reinterpret_cast<uint8_t*>(yuv420ImgCopy.chroma_data) +
267
0
                                   yuv420ImgCopy.chroma_stride * yuv420ImgCopy.height / 2};
268
0
      const unsigned int strides[3]{yuv420ImgCopy.luma_stride, yuv420ImgCopy.chroma_stride,
269
0
                                    yuv420ImgCopy.chroma_stride};
270
0
      if (encoder
271
0
              .compressImage(planes, strides, yuv420ImgCopy.width, yuv420ImgCopy.height,
272
0
                             UHDR_IMG_FMT_12bppYCbCr420, quality, nullptr, 0)
273
0
              .error_code == UHDR_CODEC_OK) {
274
0
        jpegImg.length = encoder.getCompressedImageSize();
275
0
        jpegImg.maxLength = jpegImg.length;
276
0
        jpegImg.data = encoder.getCompressedImagePtr();
277
0
        jpegImg.colorGamut = yuv420Cg;
278
0
        if (muxSwitch == 2) {  // api 2
279
0
          jpegImgR.length = 0;
280
0
          status = jpegHdr.encodeJPEGR(&p010Img, &yuv420Img, &jpegImg, tf, &jpegImgR);
281
0
        } else if (muxSwitch == 3) {  // api 3
282
0
          jpegImgR.length = 0;
283
0
          status = jpegHdr.encodeJPEGR(&p010Img, &jpegImg, tf, &jpegImgR);
284
0
        } else if (muxSwitch == 4) {  // api 4
285
0
          jpegImgR.length = 0;
286
0
          JpegEncoderHelper gainMapEncoder;
287
0
          const uint8_t* planeGm[1]{reinterpret_cast<uint8_t*>(grayImg.data)};
288
0
          const unsigned int strideGm[1]{grayImg.width};
289
0
          if (gainMapEncoder
290
0
                  .compressImage(planeGm, strideGm, grayImg.width, grayImg.height,
291
0
                                 UHDR_IMG_FMT_8bppYCbCr400, quality, nullptr, 0)
292
0
                  .error_code == UHDR_CODEC_OK) {
293
0
            jpegGainMap.length = gainMapEncoder.getCompressedImageSize();
294
0
            jpegGainMap.maxLength = jpegImg.length;
295
0
            jpegGainMap.data = gainMapEncoder.getCompressedImagePtr();
296
0
            jpegGainMap.colorGamut = ULTRAHDR_COLORGAMUT_UNSPECIFIED;
297
0
            ultrahdr_metadata_struct metadata;
298
0
            metadata.version = kJpegrVersion;
299
0
            metadata.maxContentBoost = maxBoost;
300
0
            metadata.minContentBoost = minBoost;
301
0
            metadata.gamma = gamma;
302
0
            metadata.offsetSdr = offsetSdr;
303
0
            metadata.offsetHdr = offsetHdr;
304
0
            metadata.hdrCapacityMin = minCapacity;
305
0
            metadata.hdrCapacityMax = maxCapacity;
306
0
            status = jpegHdr.encodeJPEGR(&jpegImg, &jpegGainMap, &metadata, &jpegImgR);
307
0
          }
308
0
        }
309
0
      }
310
0
    }
311
0
    if (status == JPEGR_NO_ERROR) {
312
0
      jpegr_info_struct info{};
313
0
      status = jpegHdr.getJPEGRInfo(&jpegImgR, &info);
314
0
      if (status == JPEGR_NO_ERROR) {
315
0
        size_t outSize = info.width * info.height * ((of == ULTRAHDR_OUTPUT_HDR_LINEAR) ? 8 : 4);
316
0
        jpegr_uncompressed_struct decodedJpegR;
317
0
        auto decodedRaw = std::make_unique<uint8_t[]>(outSize);
318
0
        decodedJpegR.data = decodedRaw.get();
319
0
        ultrahdr_metadata_struct metadata;
320
0
        status = jpegHdr.decodeJPEGR(&jpegImgR, &decodedJpegR, displayBoost, nullptr, of, nullptr,
321
0
                                     &metadata);
322
0
        if (status != JPEGR_NO_ERROR) {
323
0
          ALOGE("encountered error during decoding %d", status);
324
0
        }
325
0
      } else {
326
0
        ALOGE("encountered error during get jpeg info %d", status);
327
0
      }
328
0
    } else {
329
0
      ALOGE("encountered error during encoding %d", status);
330
0
    }
331
0
  }
332
0
}
333
334
9.84k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
335
9.84k
  UltraHdrEncFuzzer fuzzHandle(data, size);
336
9.84k
  fuzzHandle.process();
337
9.84k
  return 0;
338
9.84k
}