aboutsummaryrefslogtreecommitdiff
path: root/projects/libwebp/fuzz_webp_enc_dec.cc
blob: c1830e5d1c66e2587a2cfe565a3879fe7ee03917 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include "webp/encode.h"
#include "webp/decode.h"
#include "img_alpha.h"
#include "img_grid.h"
#include "img_peak.h"
#include "dsp/dsp.h"

namespace {

const VP8CPUInfo LibGetCPUInfo = VP8GetCPUInfo;

int GetCPUInfoNoSSE41(CPUFeature feature) {
  if (feature == kSSE4_1 || feature == kAVX) return 0;
  return LibGetCPUInfo(feature);
}

int GetCPUInfoNoAVX(CPUFeature feature) {
  if (feature == kAVX) return 0;
  return LibGetCPUInfo(feature);
}

int GetCPUInfoForceSlowSSSE3(CPUFeature feature) {
  if (feature == kSlowSSSE3 && LibGetCPUInfo(kSSE3)) {
    return 1;  // we have SSE3 -> force SlowSSSE3
  }
  return LibGetCPUInfo(feature);
}

int GetCPUInfoOnlyC(CPUFeature feature) {
  return false;
}

const VP8CPUInfo kVP8CPUInfos[5] = {
    GetCPUInfoOnlyC, GetCPUInfoForceSlowSSSE3,
    GetCPUInfoNoSSE41, GetCPUInfoNoAVX, LibGetCPUInfo
};

static uint32_t Extract(uint32_t max, const uint8_t data[], size_t size,
                        uint32_t* const bit_pos) {
  uint32_t v = 0;
  int range = 1;
  while (*bit_pos < 8 * size && range <= max) {
    const uint8_t mask = 1u << (*bit_pos & 7);
    v = (v << 1) | !!(data[*bit_pos >> 3] & mask);
    range <<= 1;
    ++*bit_pos;
  }
  return v % (max + 1);
}

static int max(int a, int b) { return ((a < b) ? b : a); }

}  //  namespace

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* const data, size_t size) {
  // Extract a configuration from the packed bits.
  WebPConfig config;
  if (!WebPConfigInit(&config)) {
    fprintf(stderr, "WebPConfigInit failed.\n");
    abort();
  }
  uint32_t bit_pos = 0;
  config.lossless = Extract(1, data, size, &bit_pos);
  config.quality = Extract(100, data, size, &bit_pos);
  config.method = Extract(6, data, size, &bit_pos);
  config.image_hint =
      (WebPImageHint)Extract(WEBP_HINT_LAST - 1, data, size, &bit_pos);
  config.segments = 1 + Extract(3, data, size, &bit_pos);
  config.sns_strength = Extract(100, data, size, &bit_pos);
  config.filter_strength = Extract(100, data, size, &bit_pos);
  config.filter_sharpness = Extract(7, data, size, &bit_pos);
  config.filter_type = Extract(1, data, size, &bit_pos);
  config.autofilter = Extract(1, data, size, &bit_pos);
  config.alpha_compression = Extract(1, data, size, &bit_pos);
  config.alpha_filtering = Extract(2, data, size, &bit_pos);
  config.alpha_quality = Extract(100, data, size, &bit_pos);
  config.pass = 1 + Extract(9, data, size, &bit_pos);
  config.show_compressed = 1;
  config.preprocessing = Extract(2, data, size, &bit_pos);
  config.partitions = Extract(3, data, size, &bit_pos);
  config.partition_limit = 10 * Extract(10, data, size, &bit_pos);
  config.emulate_jpeg_size = Extract(1, data, size, &bit_pos);
  config.thread_level = Extract(1, data, size, &bit_pos);
  config.low_memory = Extract(1, data, size, &bit_pos);
  config.near_lossless = 20 * Extract(5, data, size, &bit_pos);
  config.exact = Extract(1, data, size, &bit_pos);
  config.use_delta_palette = Extract(1, data, size, &bit_pos);
  config.use_sharp_yuv = Extract(1, data, size, &bit_pos);
  if (!WebPValidateConfig(&config)) {
    fprintf(stderr, "WebPValidateConfig failed.\n");
    abort();
  }

  // Init the source picture.
  WebPPicture pic;
  if (!WebPPictureInit(&pic)) {
    fprintf(stderr, "WebPPictureInit failed.\n");
    abort();
  }
  pic.use_argb = Extract(1, data, size, &bit_pos);

  VP8GetCPUInfo = kVP8CPUInfos[Extract(4, data, size, &bit_pos)];

  // Pick a source picture.
  const uint8_t* kImagesData[] = {
      kImgAlphaData,
      kImgGridData,
      kImgPeakData
  };
  const int kImagesWidth[] = {
      kImgAlphaWidth,
      kImgGridWidth,
      kImgPeakWidth
  };
  const int kImagesHeight[] = {
      kImgAlphaHeight,
      kImgGridHeight,
      kImgPeakHeight
  };
  const size_t kNbImages = sizeof(kImagesData) / sizeof(kImagesData[0]);
  const size_t image_index = Extract(kNbImages - 1, data, size, &bit_pos);
  const uint8_t* const image_data = kImagesData[image_index];
  pic.width = kImagesWidth[image_index];
  pic.height = kImagesHeight[image_index];
  pic.argb_stride = pic.width * 4 * sizeof(uint8_t);

  // Read the bytes.
  if (!WebPPictureImportRGBA(&pic, image_data, pic.argb_stride)) {
    fprintf(stderr, "Can't read input image: %zu\n", image_index);
    WebPPictureFree(&pic);
    abort();
  }

  // Crop and scale.
  const bool alter_input = Extract(1, data, size, &bit_pos) != 0;
  const bool crop_or_scale = Extract(1, data, size, &bit_pos) != 0;
  const int width_ratio = 1 + Extract(7, data, size, &bit_pos);
  const int height_ratio = 1 + Extract(7, data, size, &bit_pos);
  if (alter_input) {
    if (crop_or_scale) {
      const uint32_t left_ratio = 1 + Extract(7, data, size, &bit_pos);
      const uint32_t top_ratio = 1 + Extract(7, data, size, &bit_pos);
      const int cropped_width = max(1, pic.width / width_ratio);
      const int cropped_height = max(1, pic.height / height_ratio);
      const int cropped_left = (pic.width - cropped_width) / left_ratio;
      const int cropped_top = (pic.height - cropped_height) / top_ratio;
      if (!WebPPictureCrop(&pic, cropped_left, cropped_top, cropped_width,
                           cropped_height)) {
        fprintf(stderr, "WebPPictureCrop failed. Parameters: %d,%d,%d,%d\n",
                cropped_left, cropped_top, cropped_width, cropped_height);
        WebPPictureFree(&pic);
        abort();
      }
    } else {
      const int scaled_width = 1 + pic.width * width_ratio / 4;
      const int scaled_height = 1 + pic.height * height_ratio / 4;
      if (!WebPPictureRescale(&pic, scaled_width, scaled_height)) {
        fprintf(stderr, "WebPPictureRescale failed. Parameters: %d,%d\n",
                scaled_width, scaled_height);
        WebPPictureFree(&pic);
        abort();
      }
    }
  }

  // Skip slow settings on big images, it's likely to timeout.
  if (pic.width * pic.height > 16 * 16) {
    if (config.lossless) {
      if (config.quality >= 99.0f && config.method >= 5) {
        config.quality = 99.0f;
        config.method = 5;
      }
    } else {
      if (config.quality >= 99.0f && config.method == 6) {
        config.quality = 99.0f;
      }
    }
    if (config.alpha_quality == 100 && config.method == 6) {
      config.alpha_quality = 99;
    }
  }

  // Encode.
  WebPMemoryWriter memory_writer;
  WebPMemoryWriterInit(&memory_writer);
  pic.writer = WebPMemoryWrite;
  pic.custom_ptr = &memory_writer;
  if (!WebPEncode(&config, &pic)) {
    fprintf(stderr, "WebPEncode failed. Error code: %d\nFile: %zu\n",
            pic.error_code, image_index);
    WebPMemoryWriterClear(&memory_writer);
    WebPPictureFree(&pic);
    abort();
  }

  // Try decoding the result.
  int w, h;
  const uint8_t* const out_data = memory_writer.mem;
  const size_t out_size = memory_writer.size;
  uint8_t* const rgba = WebPDecodeBGRA(out_data, out_size, &w, &h);
  if (rgba == nullptr || w != pic.width || h != pic.height) {
    fprintf(stderr, "WebPDecodeBGRA failed.\nFile: %zu\n", image_index);
    WebPFree(rgba);
    WebPMemoryWriterClear(&memory_writer);
    WebPPictureFree(&pic);
    abort();
  }

  // Compare the results if exact encoding.
  if (pic.use_argb && config.lossless && config.near_lossless == 100) {
    const uint32_t* src1 = (const uint32_t*)rgba;
    const uint32_t* src2 = pic.argb;
    for (int y = 0; y < h; ++y, src1 += w, src2 += pic.argb_stride) {
      for (int x = 0; x < w; ++x) {
        uint32_t v1 = src1[x], v2 = src2[x];
        if (!config.exact) {
          if ((v1 & 0xff000000u) == 0 || (v2 & 0xff000000u) == 0) {
            // Only keep alpha for comparison of fully transparent area.
            v1 &= 0xff000000u;
            v2 &= 0xff000000u;
          }
        }
        if (v1 != v2) {
          fprintf(stderr,
                  "Lossless compression failed pixel-exactness.\nFile: %zu\n",
                  image_index);
          WebPFree(rgba);
          WebPMemoryWriterClear(&memory_writer);
          WebPPictureFree(&pic);
          abort();
        }
      }
    }
  }

  WebPFree(rgba);
  WebPMemoryWriterClear(&memory_writer);
  WebPPictureFree(&pic);
  return 0;
}