| 1 |
// Copyright 2018 Google Inc. |
| 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 |
//////////////////////////////////////////////////////////////////////////////// |
| 16 |
|
| 17 |
#include <cstddef> |
| 18 |
#include <cstdint> |
| 19 |
#include <cstdio> |
| 20 |
|
| 21 |
#include "./fuzz_utils.h" |
| 22 |
#include "src/dsp/cpu.h" |
| 23 |
#include "src/webp/decode.h" |
| 24 |
#include "src/webp/encode.h" |
| 25 |
|
| 26 |
namespace { |
| 27 |
|
| 28 |
const VP8CPUInfo default_VP8GetCPUInfo = fuzz_utils::VP8GetCPUInfo; |
| 29 |
|
| 30 |
void EncDecTest(bool use_argb, int source_image_index, WebPConfig config, |
| 31 |
int optimization_index, |
| 32 |
const fuzz_utils::CropOrScaleParams& crop_or_scale_params) { |
| 33 |
fuzz_utils::SetOptimization(default_VP8GetCPUInfo, optimization_index); |
| 34 |
|
| 35 |
// Init the source picture. |
| 36 |
WebPPicture pic = fuzz_utils::GetSourcePicture(source_image_index, use_argb); |
| 37 |
|
| 38 |
// Crop and scale. |
| 39 |
if (!fuzz_utils::CropOrScale(&pic, crop_or_scale_params)) { |
| 40 |
const WebPEncodingError error_code = pic.error_code; |
| 41 |
WebPPictureFree(&pic); |
| 42 |
if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY) return; |
| 43 |
fprintf(stderr, "ExtractAndCropOrScale failed. Error code: %d\n", |
| 44 |
error_code); |
| 45 |
abort(); |
| 46 |
} |
| 47 |
|
| 48 |
// Skip slow settings on big images, it's likely to timeout. |
| 49 |
if (pic.width * pic.height > 32 * 32) { |
| 50 |
if (config.lossless) { |
| 51 |
if (config.quality > 99.0f && config.method >= 5) { |
| 52 |
config.quality = 99.0f; |
| 53 |
config.method = 5; |
| 54 |
} |
| 55 |
} else { |
| 56 |
if (config.quality > 99.0f && config.method == 6) { |
| 57 |
config.quality = 99.0f; |
| 58 |
} |
| 59 |
} |
| 60 |
if (config.alpha_quality == 100 && config.method == 6) { |
| 61 |
config.alpha_quality = 99; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// Encode. |
| 66 |
WebPMemoryWriter memory_writer; |
| 67 |
WebPMemoryWriterInit(&memory_writer); |
| 68 |
pic.writer = WebPMemoryWrite; |
| 69 |
pic.custom_ptr = &memory_writer; |
| 70 |
if (!WebPEncode(&config, &pic)) { |
| 71 |
const WebPEncodingError error_code = pic.error_code; |
| 72 |
WebPMemoryWriterClear(&memory_writer); |
| 73 |
WebPPictureFree(&pic); |
| 74 |
if (error_code == VP8_ENC_ERROR_OUT_OF_MEMORY || |
| 75 |
error_code == VP8_ENC_ERROR_BAD_WRITE) { |
| 76 |
return; |
| 77 |
} |
| 78 |
fprintf(stderr, "WebPEncode failed. Error code: %d\n", error_code); |
| 79 |
abort(); |
| 80 |
} |
| 81 |
|
| 82 |
// Try decoding the result. |
| 83 |
const uint8_t* const out_data = memory_writer.mem; |
| 84 |
const size_t out_size = memory_writer.size; |
| 85 |
WebPDecoderConfig dec_config; |
| 86 |
if (!WebPInitDecoderConfig(&dec_config)) { |
| 87 |
fprintf(stderr, "WebPInitDecoderConfig failed.\n"); |
| 88 |
WebPMemoryWriterClear(&memory_writer); |
| 89 |
WebPPictureFree(&pic); |
| 90 |
abort(); |
| 91 |
} |
| 92 |
|
| 93 |
dec_config.output.colorspace = MODE_BGRA; |
| 94 |
const VP8StatusCode status = WebPDecode(out_data, out_size, &dec_config); |
| 95 |
if ((status != VP8_STATUS_OK && status != VP8_STATUS_OUT_OF_MEMORY && |
| 96 |
status != VP8_STATUS_USER_ABORT) || |
| 97 |
(status == VP8_STATUS_OK && (dec_config.output.width != pic.width || |
| 98 |
dec_config.output.height != pic.height))) { |
| 99 |
fprintf(stderr, "WebPDecode failed. status: %d.\n", status); |
| 100 |
WebPFreeDecBuffer(&dec_config.output); |
| 101 |
WebPMemoryWriterClear(&memory_writer); |
| 102 |
WebPPictureFree(&pic); |
| 103 |
abort(); |
| 104 |
} |
| 105 |
|
| 106 |
if (status == VP8_STATUS_OK) { |
| 107 |
const uint8_t* const rgba = dec_config.output.u.RGBA.rgba; |
| 108 |
const int w = dec_config.output.width; |
| 109 |
const int h = dec_config.output.height; |
| 110 |
|
| 111 |
// Compare the results if exact encoding. |
| 112 |
if (pic.use_argb && config.lossless && config.near_lossless == 100) { |
| 113 |
const uint32_t* src1 = (const uint32_t*)rgba; |
| 114 |
const uint32_t* src2 = pic.argb; |
| 115 |
for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) { |
| 116 |
for (int x = 0; x < w; ++x) { |
| 117 |
uint32_t v1 = src1[x], v2 = src2[x]; |
| 118 |
if (!config.exact) { |
| 119 |
if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) { |
| 120 |
// Only keep alpha for comparison of fully transparent area. |
| 121 |
v1 &= 0xff000000u; |
| 122 |
v2 &= 0xff000000u; |
| 123 |
} |
| 124 |
} |
| 125 |
if (v1 != v2) { |
| 126 |
fprintf(stderr, "Lossless compression failed pixel-exactness.\n"); |
| 127 |
WebPFreeDecBuffer(&dec_config.output); |
| 128 |
WebPMemoryWriterClear(&memory_writer); |
| 129 |
WebPPictureFree(&pic); |
| 130 |
abort(); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
WebPFreeDecBuffer(&dec_config.output); |
| 138 |
WebPMemoryWriterClear(&memory_writer); |
| 139 |
WebPPictureFree(&pic); |
| 140 |
} |
| 141 |
|
| 142 |
} // namespace |
| 143 |
|
| 144 |
FUZZ_TEST(EncDec, EncDecTest) |
| 145 |
.WithDomains(/*use_argb=*/fuzztest::Arbitrary<bool>(), |
| 146 |
/*source_image_index=*/ |
| 147 |
fuzztest::InRange<int>(0, fuzz_utils::kNumSourceImages - 1), |
| 148 |
fuzz_utils::ArbitraryWebPConfig(), |
| 149 |
/*optimization_index=*/ |
| 150 |
fuzztest::InRange<uint32_t>(0, |
| 151 |
fuzz_utils::kMaxOptimizationIndex), |
| 152 |
fuzz_utils::ArbitraryCropOrScaleParams()); |