scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkWebpCodec.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 9 | #include "SkTemplates.h" |
| 10 | |
| 11 | // A WebP decoder on top of (subset of) libwebp |
| 12 | // For more information on WebP image format, and libwebp library, see: |
| 13 | // https://code.google.com/speed/webp/ |
| 14 | // http://www.webmproject.org/code/#libwebp-webp-image-library |
| 15 | // https://chromium.googlesource.com/webm/libwebp |
| 16 | |
| 17 | // If moving libwebp out of skia source tree, path for webp headers must be |
| 18 | // updated accordingly. Here, we enforce using local copy in webp sub-directory. |
| 19 | #include "webp/decode.h" |
| 20 | #include "webp/encode.h" |
| 21 | |
| 22 | bool SkWebpCodec::IsWebp(SkStream* stream) { |
| 23 | // WEBP starts with the following: |
| 24 | // RIFFXXXXWEBPVP |
| 25 | // Where XXXX is unspecified. |
| 26 | const char LENGTH = 14; |
| 27 | char bytes[LENGTH]; |
| 28 | if (stream->read(&bytes, LENGTH) != LENGTH) { |
| 29 | return false; |
| 30 | } |
| 31 | return !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); |
| 32 | } |
| 33 | |
| 34 | static const size_t WEBP_VP8_HEADER_SIZE = 30; |
| 35 | |
| 36 | // Parse headers of RIFF container, and check for valid Webp (VP8) content. |
| 37 | // NOTE: This calls peek instead of read, since onGetPixels will need these |
| 38 | // bytes again. |
| 39 | static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { |
| 40 | unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
| 41 | if (!stream->peek(buffer, WEBP_VP8_HEADER_SIZE)) { |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | WebPBitstreamFeatures features; |
| 46 | VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &features); |
| 47 | if (VP8_STATUS_OK != status) { |
| 48 | return false; // Invalid WebP file. |
| 49 | } |
| 50 | |
| 51 | // sanity check for image size that's about to be decoded. |
| 52 | { |
| 53 | const int64_t size = sk_64_mul(features.width, features.height); |
| 54 | if (!sk_64_isS32(size)) { |
| 55 | return false; |
| 56 | } |
| 57 | // now check that if we are 4-bytes per pixel, we also don't overflow |
| 58 | if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | if (info) { |
| 64 | // FIXME: Is N32 the right type? |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 65 | // Is unpremul the right type? Clients of SkCodec may assume it's the |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 66 | // best type, when Skia currently cannot draw unpremul (and raster is faster |
| 67 | // with premul). |
| 68 | *info = SkImageInfo::Make(features.width, features.height, kN32_SkColorType, |
| 69 | SkToBool(features.has_alpha) ? kUnpremul_SkAlphaType |
| 70 | : kOpaque_SkAlphaType); |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
| 76 | SkAutoTDelete<SkStream> streamDeleter(stream); |
| 77 | SkImageInfo info; |
| 78 | if (webp_parse_header(stream, &info)) { |
| 79 | return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { |
scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame^] | 85 | if (dst.profileType() != src.profileType()) { |
| 86 | return false; |
| 87 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 88 | switch (dst.colorType()) { |
| 89 | // Both byte orders are supported. |
| 90 | case kBGRA_8888_SkColorType: |
| 91 | case kRGBA_8888_SkColorType: |
| 92 | break; |
scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame^] | 93 | case kRGB_565_SkColorType: |
| 94 | if (src.alphaType() == kOpaque_SkAlphaType |
| 95 | && dst.alphaType() == kOpaque_SkAlphaType) |
| 96 | { |
| 97 | return true; |
| 98 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 99 | default: |
| 100 | return false; |
| 101 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 102 | if (dst.alphaType() == src.alphaType()) { |
| 103 | return true; |
| 104 | } |
| 105 | return kPremul_SkAlphaType == dst.alphaType() && |
| 106 | kUnpremul_SkAlphaType == src.alphaType(); |
| 107 | } |
| 108 | |
| 109 | SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { |
| 110 | SkISize dim = this->getInfo().dimensions(); |
msarett | a0c414d | 2015-06-19 07:34:30 -0700 | [diff] [blame] | 111 | // SkCodec treats zero dimensional images as errors, so the minimum size |
| 112 | // that we will recommend is 1x1. |
| 113 | dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); |
| 114 | dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 115 | return dim; |
| 116 | } |
| 117 | |
| 118 | static WEBP_CSP_MODE webp_decode_mode(SkColorType ct, bool premultiply) { |
| 119 | switch (ct) { |
| 120 | case kBGRA_8888_SkColorType: |
| 121 | return premultiply ? MODE_bgrA : MODE_BGRA; |
| 122 | case kRGBA_8888_SkColorType: |
| 123 | return premultiply ? MODE_rgbA : MODE_RGBA; |
scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame^] | 124 | case kRGB_565_SkColorType: |
| 125 | return MODE_RGB_565; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 126 | default: |
| 127 | return MODE_LAST; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // The WebP decoding API allows us to incrementally pass chunks of bytes as we receive them to the |
| 132 | // decoder with WebPIAppend. In order to do so, we need to read chunks from the SkStream. This size |
| 133 | // is arbitrary. |
| 134 | static const size_t BUFFER_SIZE = 4096; |
| 135 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 136 | bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { |
| 137 | if (!desiredSubset) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); |
| 142 | if (!desiredSubset->intersect(bounds)) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we |
| 147 | // decode this exact subset. |
| 148 | // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. |
| 149 | desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; |
| 150 | desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; |
| 151 | return true; |
| 152 | } |
| 153 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 154 | SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 155 | const Options& options, SkPMColor*, int*) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 156 | switch (this->rewindIfNeeded()) { |
| 157 | case kCouldNotRewind_RewindState: |
| 158 | return kCouldNotRewind; |
| 159 | case kRewound_RewindState: |
| 160 | // Rewound to the beginning. Since creation only does a peek, the stream is at the |
| 161 | // correct position. |
| 162 | break; |
| 163 | case kNoRewindNecessary_RewindState: |
| 164 | // Already at the right spot for decoding. |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (!conversion_possible(dstInfo, this->getInfo())) { |
| 169 | return kInvalidConversion; |
| 170 | } |
| 171 | |
| 172 | WebPDecoderConfig config; |
| 173 | if (0 == WebPInitDecoderConfig(&config)) { |
| 174 | // ABI mismatch. |
| 175 | // FIXME: New enum for this? |
| 176 | return kInvalidInput; |
| 177 | } |
| 178 | |
| 179 | // Free any memory associated with the buffer. Must be called last, so we declare it first. |
| 180 | SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output)); |
| 181 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 182 | SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); |
| 183 | if (options.fSubset) { |
| 184 | // Caller is requesting a subset. |
| 185 | if (!bounds.contains(*options.fSubset)) { |
| 186 | // The subset is out of bounds. |
| 187 | return kInvalidParameters; |
| 188 | } |
| 189 | |
| 190 | bounds = *options.fSubset; |
| 191 | |
| 192 | // This is tricky. libwebp snaps the top and left to even values. We could let libwebp |
| 193 | // do the snap, and return a subset which is a different one than requested. The problem |
| 194 | // with that approach is that the caller may try to stitch subsets together, and if we |
| 195 | // returned different subsets than requested, there would be artifacts at the boundaries. |
| 196 | // Instead, we report that we cannot support odd values for top and left.. |
| 197 | if (!SkIsAlign2(bounds.fLeft) || !SkIsAlign2(bounds.fTop)) { |
| 198 | return kInvalidParameters; |
| 199 | } |
| 200 | |
| 201 | #ifdef SK_DEBUG |
| 202 | { |
| 203 | // Make a copy, since getValidSubset can change its input. |
| 204 | SkIRect subset(bounds); |
| 205 | // That said, getValidSubset should *not* change its input, in this case; otherwise |
| 206 | // getValidSubset does not match the actual subsets we can do. |
| 207 | SkASSERT(this->getValidSubset(&subset) && subset == bounds); |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | config.options.use_cropping = 1; |
| 212 | config.options.crop_left = bounds.fLeft; |
| 213 | config.options.crop_top = bounds.fTop; |
| 214 | config.options.crop_width = bounds.width(); |
| 215 | config.options.crop_height = bounds.height(); |
| 216 | } |
| 217 | |
| 218 | SkISize dstDimensions = dstInfo.dimensions(); |
| 219 | if (bounds.size() != dstDimensions) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 220 | // Caller is requesting scaling. |
| 221 | config.options.use_scaling = 1; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 222 | config.options.scaled_width = dstDimensions.width(); |
| 223 | config.options.scaled_height = dstDimensions.height(); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | config.output.colorspace = webp_decode_mode(dstInfo.colorType(), |
| 227 | dstInfo.alphaType() == kPremul_SkAlphaType); |
| 228 | config.output.u.RGBA.rgba = (uint8_t*) dst; |
| 229 | config.output.u.RGBA.stride = (int) rowBytes; |
| 230 | config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); |
| 231 | config.output.is_external_memory = 1; |
| 232 | |
| 233 | SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(NULL, 0, &config)); |
| 234 | if (!idec) { |
| 235 | return kInvalidInput; |
| 236 | } |
| 237 | |
| 238 | SkAutoMalloc storage(BUFFER_SIZE); |
| 239 | uint8_t* buffer = static_cast<uint8_t*>(storage.get()); |
| 240 | while (true) { |
| 241 | const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE); |
| 242 | if (0 == bytesRead) { |
| 243 | // FIXME: Maybe this is an incomplete image? How to decide? Based |
| 244 | // on the number of rows decoded? We can know the number of rows |
| 245 | // decoded using WebPIDecGetRGB. |
| 246 | return kInvalidInput; |
| 247 | } |
| 248 | |
| 249 | switch (WebPIAppend(idec, buffer, bytesRead)) { |
| 250 | case VP8_STATUS_OK: |
| 251 | return kSuccess; |
| 252 | case VP8_STATUS_SUSPENDED: |
| 253 | // Break out of the switch statement. Continue the loop. |
| 254 | break; |
| 255 | default: |
| 256 | return kInvalidInput; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) |
| 262 | : INHERITED(info, stream) {} |