Skip to content

Commit

Permalink
fuzzers: add editor effects api to fuzz tests
Browse files Browse the repository at this point in the history
crop, resize, mirror and rotate are added to encoder and decoder fuzzer
tests.

Test: ./ultrahdr_enc_fuzzer
Test: ./ultrahdr_dec_fuzzer
  • Loading branch information
ram-mohan committed Sep 25, 2024
1 parent 5e7ceda commit 130439e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 40 deletions.
54 changes: 44 additions & 10 deletions fuzzer/ultrahdr_dec_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,31 @@ class UltraHdrDecFuzzer {
};

void UltraHdrDecFuzzer::process() {
// hdr_of
auto tf = static_cast<uhdr_color_transfer>(mFdp.ConsumeIntegralInRange<int8_t>(kTfMin, kTfMax));
auto boost = mFdp.ConsumeFloatingPointInRange<float>(0.0f, 100.0f);
auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();
auto output_ct =
static_cast<uhdr_color_transfer>(mFdp.ConsumeIntegralInRange<int8_t>(kTfMin, kTfMax));
auto displayBoost = mFdp.ConsumeFloatingPointInRange<float>(-10.0f, 100.0f);
auto enableGpu = mFdp.ConsumeBool();

// editing effects
auto applyMirror = mFdp.ConsumeBool();
uhdr_mirror_direction_t direction =
mFdp.ConsumeBool() ? UHDR_MIRROR_VERTICAL : UHDR_MIRROR_HORIZONTAL;

auto applyRotate = mFdp.ConsumeBool();
int degrees = degrees = mFdp.PickValueInArray({-90, 0, 90, 180, 270});

auto applyCrop = mFdp.ConsumeBool();
int left = mFdp.ConsumeIntegral<int16_t>();
int right = mFdp.ConsumeIntegral<int16_t>();
int top = mFdp.ConsumeIntegral<int16_t>();
int bottom = mFdp.ConsumeIntegral<int16_t>();

auto applyResize = mFdp.ConsumeBool();
int resizeWidth = mFdp.ConsumeIntegral<int16_t>();
int resizeHeight = mFdp.ConsumeIntegral<int16_t>();

auto buffer = mFdp.ConsumeRemainingBytes<uint8_t>();

uhdr_compressed_image_t jpegImgR{
buffer.data(), (unsigned int)buffer.size(), (unsigned int)buffer.size(),
UHDR_CG_UNSPECIFIED, UHDR_CT_UNSPECIFIED, UHDR_CR_UNSPECIFIED};
Expand All @@ -52,18 +72,25 @@ void UltraHdrDecFuzzer::process() {
} \
} \
}

(void)is_uhdr_image(buffer.data(), buffer.size());

uhdr_codec_private_t* dec_handle = uhdr_create_decoder();
if (dec_handle) {
ON_ERR(uhdr_dec_set_image(dec_handle, &jpegImgR))
ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, tf))
if (tf == UHDR_CT_LINEAR)
ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, output_ct))
if (output_ct == UHDR_CT_LINEAR)
ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_64bppRGBAHalfFloat))
else if (tf == UHDR_CT_SRGB)
else if (output_ct == UHDR_CT_SRGB)
ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA8888))
else
ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA1010102))
ON_ERR(uhdr_dec_set_out_max_display_boost(dec_handle, boost))
ON_ERR(uhdr_dec_set_out_max_display_boost(dec_handle, displayBoost))
ON_ERR(uhdr_enable_gpu_acceleration(dec_handle, enableGpu))
if (applyMirror) ON_ERR(uhdr_add_effect_mirror(dec_handle, direction))
if (applyRotate) ON_ERR(uhdr_add_effect_rotate(dec_handle, degrees))
if (applyCrop) ON_ERR(uhdr_add_effect_crop(dec_handle, left, right, top, bottom))
if (applyResize) ON_ERR(uhdr_add_effect_resize(dec_handle, resizeWidth, resizeHeight))
uhdr_dec_probe(dec_handle);
auto width = uhdr_dec_get_image_width(dec_handle);
auto height = uhdr_dec_get_image_height(dec_handle);
Expand All @@ -74,9 +101,16 @@ void UltraHdrDecFuzzer::process() {
// ALOGV("image height %d ", (int)height);
// ALOGV("gainmap width %d ", (int)gainmap_width);
// ALOGV("gainmap height %d ", (int)gainmap_height);
// ALOGV("output color transfer %d ", (int)tf);
// ALOGV("max display boost %f ", (float)boost);
// ALOGV("output color transfer %d ", (int)output_ct);
// ALOGV("max display displayBoost %f ", (float)displayBoost);
// ALOGV("enable gpu %d ", (int)enableGpu);
// if (applyMirror) ALOGV("added mirror effect, direction %d", (int)direction);
// if (applyRotate) ALOGV("added rotate effect, degrees %d", (int)degrees);
// if (applyCrop)
// ALOGV("added crop effect, crop-left %d, crop-right %d, crop-top %d, crop-bottom %d", left,
// right, top, bottom);
// if (applyResize)
// ALOGV("added resize effect, resize wd %d, resize ht %d", resizeWidth, resizeHeight);

uhdr_dec_get_exif(dec_handle);
uhdr_dec_get_icc(dec_handle);
Expand Down
84 changes: 54 additions & 30 deletions fuzzer/ultrahdr_enc_fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ constexpr int kCrMax = UHDR_CR_FULL_RANGE;
constexpr int kTfMin = UHDR_CT_UNSPECIFIED;
constexpr int kTfMax = UHDR_CT_SRGB;

// quality factor
constexpr int kQfMin = -10;
constexpr int kQfMax = 110;

class UltraHdrEncFuzzer {
public:
UltraHdrEncFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {};
Expand Down Expand Up @@ -86,11 +82,9 @@ void UltraHdrEncFuzzer::process() {
mFdp.ConsumeBool() ? UHDR_IMG_FMT_24bppYCbCrP010 : UHDR_IMG_FMT_32bppRGBA1010102;

// sdr_img_fmt
uhdr_img_fmt_t sdr_img_fmt;
if (muxSwitch > 1)
sdr_img_fmt = UHDR_IMG_FMT_12bppYCbCr420;
else
sdr_img_fmt = mFdp.ConsumeBool() ? UHDR_IMG_FMT_12bppYCbCr420 : UHDR_IMG_FMT_32bppRGBA8888;
uhdr_img_fmt_t sdr_img_fmt =
mFdp.ConsumeBool() ? UHDR_IMG_FMT_12bppYCbCr420 : UHDR_IMG_FMT_32bppRGBA8888;
if (muxSwitch > 1) sdr_img_fmt = UHDR_IMG_FMT_12bppYCbCr420;

// width
int width = mFdp.ConsumeIntegralInRange<uint16_t>(kMinWidth, kMaxWidth);
Expand All @@ -104,8 +98,8 @@ void UltraHdrEncFuzzer::process() {
height = (height >> 1) << 1;
}

// hdr_tf
auto tf =
// hdr Ct
auto hdr_ct =
static_cast<uhdr_color_transfer_t>(mFdp.ConsumeIntegralInRange<int8_t>(kTfMin, kTfMax));

// hdr Cg
Expand All @@ -121,16 +115,16 @@ void UltraHdrEncFuzzer::process() {
static_cast<uhdr_color_range_t>(mFdp.ConsumeIntegralInRange<int8_t>(kCrMin, kCrMax));

// base quality factor
int base_quality = mFdp.ConsumeIntegralInRange<int8_t>(kQfMin, kQfMax);
auto base_quality = mFdp.ConsumeIntegral<int8_t>();

// gain_map quality factor
int gainmap_quality = mFdp.ConsumeIntegralInRange<int8_t>(kQfMin, kQfMax);
auto gainmap_quality = mFdp.ConsumeIntegral<int8_t>();

// multi channel gainmap
auto multi_channel_gainmap = mFdp.ConsumeBool();
auto multi_channel_gainmap = mFdp.ConsumeIntegral<int8_t>();

// gainmap scale factor
auto gm_scale_factor = mFdp.ConsumeIntegralInRange<uint8_t>(0, 150);
auto gm_scale_factor = mFdp.ConsumeIntegralInRange<int16_t>(-32, 192);

// encoding speed preset
auto enc_preset = mFdp.ConsumeBool() ? UHDR_USAGE_REALTIME : UHDR_USAGE_BEST_QUALITY;
Expand All @@ -139,16 +133,34 @@ void UltraHdrEncFuzzer::process() {
auto gamma = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 5);

// content boost
auto minBoost = mFdp.ConsumeFloatingPointInRange<float>(-1.0f, 100);
auto maxBoost = mFdp.ConsumeFloatingPointInRange<float>(minBoost - 1.0f, 100);
auto minBoost = mFdp.ConsumeFloatingPointInRange<float>(-16.0f, 64.0f);
auto maxBoost = mFdp.ConsumeFloatingPointInRange<float>(-16.0f, 64.0f);

// editing effects
auto applyMirror = mFdp.ConsumeBool();
uhdr_mirror_direction_t direction =
mFdp.ConsumeBool() ? UHDR_MIRROR_VERTICAL : UHDR_MIRROR_HORIZONTAL;

auto applyRotate = mFdp.ConsumeBool();
int degrees = degrees = mFdp.PickValueInArray({-90, 0, 90, 180, 270});

auto applyCrop = mFdp.ConsumeBool();
int left = mFdp.ConsumeIntegral<int16_t>();
int right = mFdp.ConsumeIntegral<int16_t>();
int top = mFdp.ConsumeIntegral<int16_t>();
int bottom = mFdp.ConsumeIntegral<int16_t>();

auto applyResize = mFdp.ConsumeBool();
int resizeWidth = mFdp.ConsumeIntegral<int16_t>();
int resizeHeight = mFdp.ConsumeIntegral<int16_t>();

// ALOGV("encoding configuration options : ");
// ALOGV("encoding api %d ", (int)muxSwitch);
// ALOGV("image width %d ", (int)width);
// ALOGV("image height %d ", (int)height);
// ALOGV("hdr intent color gamut %d ", (int)hdr_cg);
// ALOGV("sdr intent color gamut %d ", (int)sdr_cg);
// ALOGV("hdr intent color transfer %d ", (int)tf);
// ALOGV("hdr intent color transfer %d ", (int)hdr_ct);
// ALOGV("hdr intent color range %d ", (int)hdr_cr);
// ALOGV("hdr intent image format %d ", (int)hdr_img_fmt);
// ALOGV("sdr intent color format %d ", (int)sdr_img_fmt);
Expand All @@ -159,6 +171,13 @@ void UltraHdrEncFuzzer::process() {
// ALOGV("encoding preset %d ", (int)enc_preset);
// ALOGV("gainmap gamma %f ", (float)gamma);
// ALOGV("min max content boost %f %f ", (float)minBoost, (float)maxBoost);
// if (applyMirror) ALOGV("added mirror effect, direction %d", (int)direction);
// if (applyRotate) ALOGV("added rotate effect, degrees %d", (int)degrees);
// if (applyCrop)
// ALOGV("added crop effect, crop-left %d, crop-right %d, crop-top %d, crop-bottom %d", left,
// right, top, bottom);
// if (applyResize)
// ALOGV("added resize effect, resize wd %d, resize ht %d", resizeWidth, resizeHeight);

std::unique_ptr<uint32_t[]> bufferHdr = nullptr;
std::unique_ptr<uint16_t[]> bufferYHdr = nullptr;
Expand Down Expand Up @@ -189,7 +208,7 @@ void UltraHdrEncFuzzer::process() {
hdrImg.h = height;
hdrImg.cg = hdr_cg;
hdrImg.fmt = hdr_img_fmt;
hdrImg.ct = tf;
hdrImg.ct = hdr_ct;
hdrImg.range = hdr_cr;
hdrImg.stride[UHDR_PLANE_Y] = yStride;
if (hdr_img_fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
Expand Down Expand Up @@ -311,14 +330,19 @@ void UltraHdrEncFuzzer::process() {
ON_ERR(uhdr_enc_set_quality(enc_handle, base_quality, UHDR_BASE_IMG))
ON_ERR(uhdr_enc_set_quality(enc_handle, gainmap_quality, UHDR_GAIN_MAP_IMG))
char greeting[] = "Exif says hello world";
uhdr_mem_block_t exif{greeting, sizeof greeting, sizeof greeting};
uhdr_mem_block_t exif{greeting, mFdp.ConsumeIntegralInRange<uint8_t>(0, sizeof greeting * 2),
sizeof greeting};
ON_ERR(uhdr_enc_set_exif_data(enc_handle, &exif))
ON_ERR(uhdr_enc_set_using_multi_channel_gainmap(enc_handle, multi_channel_gainmap))
ON_ERR(uhdr_enc_set_gainmap_scale_factor(enc_handle, gm_scale_factor))
ON_ERR(uhdr_enc_set_gainmap_gamma(enc_handle, gamma))
ON_ERR(uhdr_enc_set_min_max_content_boost(enc_handle, minBoost, maxBoost))
ON_ERR(uhdr_enc_set_preset(enc_handle, enc_preset))
ON_ERR(uhdr_enable_gpu_acceleration(enc_handle, 1))
if (applyMirror) ON_ERR(uhdr_add_effect_mirror(enc_handle, direction))
if (applyRotate) ON_ERR(uhdr_add_effect_rotate(enc_handle, degrees))
if (applyCrop) ON_ERR(uhdr_add_effect_crop(enc_handle, left, right, top, bottom))
if (applyResize) ON_ERR(uhdr_add_effect_resize(enc_handle, resizeWidth, resizeHeight))

uhdr_error_info_t status = {UHDR_CODEC_OK, 0, ""};
if (muxSwitch == 0 || muxSwitch == 1) { // api 0 or api 1
Expand All @@ -341,13 +365,13 @@ void UltraHdrEncFuzzer::process() {
UHDR_CODEC_OK) {
struct uhdr_compressed_image jpegGainMap = gainMapEncoder.getCompressedImage();
uhdr_gainmap_metadata metadata;
metadata.max_content_boost = maxBoost;
metadata.min_content_boost = minBoost;
metadata.gamma = gamma;
metadata.offset_sdr = 0.0f;
metadata.offset_hdr = 0.0f;
metadata.hdr_capacity_min = 1.0f;
metadata.hdr_capacity_max = metadata.max_content_boost;
metadata.max_content_boost = mFdp.ConsumeFloatingPoint<float>();
metadata.min_content_boost = mFdp.ConsumeFloatingPoint<float>();
metadata.gamma = mFdp.ConsumeFloatingPoint<float>();
metadata.offset_sdr = mFdp.ConsumeFloatingPoint<float>();
metadata.offset_hdr = mFdp.ConsumeFloatingPoint<float>();
metadata.hdr_capacity_min = mFdp.ConsumeFloatingPoint<float>();
metadata.hdr_capacity_max = mFdp.ConsumeFloatingPoint<float>();
ON_ERR(uhdr_enc_set_compressed_image(enc_handle, &jpegImg, UHDR_BASE_IMG))
ON_ERR(uhdr_enc_set_gainmap_image(enc_handle, &jpegGainMap, &metadata))
status = uhdr_encode(enc_handle);
Expand All @@ -361,10 +385,10 @@ void UltraHdrEncFuzzer::process() {
uhdr_codec_private_t* dec_handle = uhdr_create_decoder();
if (dec_handle) {
ON_ERR(uhdr_dec_set_image(dec_handle, output))
ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, tf))
if (tf == UHDR_CT_LINEAR)
ON_ERR(uhdr_dec_set_out_color_transfer(dec_handle, hdr_ct))
if (hdr_ct == UHDR_CT_LINEAR)
ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_64bppRGBAHalfFloat))
else if (tf == UHDR_CT_SRGB)
else if (hdr_ct == UHDR_CT_SRGB)
ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA8888))
else
ON_ERR(uhdr_dec_set_out_img_format(dec_handle, UHDR_IMG_FMT_32bppRGBA1010102))
Expand Down

0 comments on commit 130439e

Please sign in to comment.