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