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 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 23 | bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 24 | // WEBP starts with the following: |
| 25 | // RIFFXXXXWEBPVP |
| 26 | // Where XXXX is unspecified. |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 27 | const char* bytes = static_cast<const char*>(buf); |
| 28 | return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 29 | } |
| 30 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 31 | // Parse headers of RIFF container, and check for valid Webp (VP8) content. |
| 32 | // NOTE: This calls peek instead of read, since onGetPixels will need these |
| 33 | // bytes again. |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 34 | // Returns an SkWebpCodec on success; |
| 35 | SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { |
| 36 | SkAutoTDelete<SkStream> streamDeleter(stream); |
| 37 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 38 | unsigned char buffer[WEBP_VP8_HEADER_SIZE]; |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 39 | SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded()); |
| 40 | |
| 41 | const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE); |
| 42 | if (bytesPeeked != WEBP_VP8_HEADER_SIZE) { |
| 43 | // Use read + rewind as a backup |
| 44 | if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE |
| 45 | || !stream->rewind()) |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 46 | return nullptr; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | WebPBitstreamFeatures features; |
| 50 | VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &features); |
| 51 | if (VP8_STATUS_OK != status) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 52 | return nullptr; // Invalid WebP file. |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // sanity check for image size that's about to be decoded. |
| 56 | { |
| 57 | const int64_t size = sk_64_mul(features.width, features.height); |
| 58 | if (!sk_64_isS32(size)) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 59 | return nullptr; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 60 | } |
| 61 | // now check that if we are 4-bytes per pixel, we also don't overflow |
| 62 | if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 63 | return nullptr; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 67 | SkEncodedInfo::Color color; |
| 68 | SkEncodedInfo::Alpha alpha; |
| 69 | switch (features.format) { |
| 70 | case 0: |
| 71 | // This indicates a "mixed" format. We would see this for |
| 72 | // animated webps or for webps encoded in multiple fragments. |
| 73 | // I believe that this is a rare case. |
| 74 | // We could also guess kYUV here, but I think it makes more |
| 75 | // sense to guess kBGRA which is likely closer to the final |
| 76 | // output. Otherwise, we might end up converting |
| 77 | // BGRA->YUVA->BGRA. |
| 78 | color = SkEncodedInfo::kBGRA_Color; |
| 79 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 80 | break; |
| 81 | case 1: |
| 82 | // This is the lossy format (YUV). |
| 83 | if (SkToBool(features.has_alpha)) { |
| 84 | color = SkEncodedInfo::kYUVA_Color; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 85 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 86 | } else { |
| 87 | color = SkEncodedInfo::kYUV_Color; |
| 88 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 89 | } |
| 90 | break; |
| 91 | case 2: |
| 92 | // This is the lossless format (BGRA). |
| 93 | // FIXME: Should we check the has_alpha flag here? It looks |
| 94 | // like the image is encoded with an alpha channel |
| 95 | // regardless of whether or not the alpha flag is set. |
| 96 | color = SkEncodedInfo::kBGRA_Color; |
| 97 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 98 | break; |
| 99 | default: |
| 100 | return nullptr; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 101 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 102 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame^] | 103 | SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8); |
| 104 | return new SkWebpCodec(features.width, features.height, info, streamDeleter.release()); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 105 | } |
| 106 | |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 107 | // This version is slightly different from SkCodecPriv's version of conversion_possible. It |
| 108 | // supports both byte orders for 8888. |
| 109 | static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { |
msarett | 6e077e1 | 2016-04-06 15:45:41 -0700 | [diff] [blame] | 110 | // FIXME: skbug.com/4895 |
| 111 | // Currently, we ignore the SkColorProfileType on the SkImageInfo. We |
| 112 | // will treat the encoded data as linear regardless of what the client |
| 113 | // requests. |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 114 | |
| 115 | if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
| 116 | return false; |
| 117 | } |
| 118 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 119 | switch (dst.colorType()) { |
| 120 | // Both byte orders are supported. |
| 121 | case kBGRA_8888_SkColorType: |
| 122 | case kRGBA_8888_SkColorType: |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 123 | return true; |
scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame] | 124 | case kRGB_565_SkColorType: |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 125 | return src.alphaType() == kOpaque_SkAlphaType; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 126 | default: |
| 127 | return false; |
| 128 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { |
| 132 | SkISize dim = this->getInfo().dimensions(); |
msarett | a0c414d | 2015-06-19 07:34:30 -0700 | [diff] [blame] | 133 | // SkCodec treats zero dimensional images as errors, so the minimum size |
| 134 | // that we will recommend is 1x1. |
| 135 | dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth)); |
| 136 | dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight)); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 137 | return dim; |
| 138 | } |
| 139 | |
scroggo | e7fc14b | 2015-10-02 13:14:46 -0700 | [diff] [blame] | 140 | bool SkWebpCodec::onDimensionsSupported(const SkISize& dim) { |
| 141 | const SkImageInfo& info = this->getInfo(); |
| 142 | return dim.width() >= 1 && dim.width() <= info.width() |
| 143 | && dim.height() >= 1 && dim.height() <= info.height(); |
| 144 | } |
| 145 | |
| 146 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 147 | static WEBP_CSP_MODE webp_decode_mode(SkColorType ct, bool premultiply) { |
| 148 | switch (ct) { |
| 149 | case kBGRA_8888_SkColorType: |
| 150 | return premultiply ? MODE_bgrA : MODE_BGRA; |
| 151 | case kRGBA_8888_SkColorType: |
| 152 | return premultiply ? MODE_rgbA : MODE_RGBA; |
scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame] | 153 | case kRGB_565_SkColorType: |
| 154 | return MODE_RGB_565; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 155 | default: |
| 156 | return MODE_LAST; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | // The WebP decoding API allows us to incrementally pass chunks of bytes as we receive them to the |
| 161 | // decoder with WebPIAppend. In order to do so, we need to read chunks from the SkStream. This size |
| 162 | // is arbitrary. |
| 163 | static const size_t BUFFER_SIZE = 4096; |
| 164 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 165 | bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { |
| 166 | if (!desiredSubset) { |
| 167 | return false; |
| 168 | } |
| 169 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 170 | SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions()); |
| 171 | if (!dimensions.contains(*desiredSubset)) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we |
| 176 | // decode this exact subset. |
| 177 | // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. |
| 178 | desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; |
| 179 | desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; |
| 180 | return true; |
| 181 | } |
| 182 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 183 | SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 184 | const Options& options, SkPMColor*, int*, |
| 185 | int* rowsDecoded) { |
scroggo | cc2feb1 | 2015-08-14 08:32:46 -0700 | [diff] [blame] | 186 | if (!webp_conversion_possible(dstInfo, this->getInfo())) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 187 | return kInvalidConversion; |
| 188 | } |
| 189 | |
| 190 | WebPDecoderConfig config; |
| 191 | if (0 == WebPInitDecoderConfig(&config)) { |
| 192 | // ABI mismatch. |
| 193 | // FIXME: New enum for this? |
| 194 | return kInvalidInput; |
| 195 | } |
| 196 | |
| 197 | // Free any memory associated with the buffer. Must be called last, so we declare it first. |
| 198 | SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output)); |
| 199 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 200 | SkIRect bounds = SkIRect::MakeSize(this->getInfo().dimensions()); |
| 201 | if (options.fSubset) { |
| 202 | // Caller is requesting a subset. |
| 203 | if (!bounds.contains(*options.fSubset)) { |
| 204 | // The subset is out of bounds. |
| 205 | return kInvalidParameters; |
| 206 | } |
| 207 | |
| 208 | bounds = *options.fSubset; |
| 209 | |
| 210 | // This is tricky. libwebp snaps the top and left to even values. We could let libwebp |
| 211 | // do the snap, and return a subset which is a different one than requested. The problem |
| 212 | // with that approach is that the caller may try to stitch subsets together, and if we |
| 213 | // returned different subsets than requested, there would be artifacts at the boundaries. |
| 214 | // Instead, we report that we cannot support odd values for top and left.. |
| 215 | if (!SkIsAlign2(bounds.fLeft) || !SkIsAlign2(bounds.fTop)) { |
| 216 | return kInvalidParameters; |
| 217 | } |
| 218 | |
| 219 | #ifdef SK_DEBUG |
| 220 | { |
| 221 | // Make a copy, since getValidSubset can change its input. |
| 222 | SkIRect subset(bounds); |
| 223 | // That said, getValidSubset should *not* change its input, in this case; otherwise |
| 224 | // getValidSubset does not match the actual subsets we can do. |
| 225 | SkASSERT(this->getValidSubset(&subset) && subset == bounds); |
| 226 | } |
| 227 | #endif |
| 228 | |
| 229 | config.options.use_cropping = 1; |
| 230 | config.options.crop_left = bounds.fLeft; |
| 231 | config.options.crop_top = bounds.fTop; |
| 232 | config.options.crop_width = bounds.width(); |
| 233 | config.options.crop_height = bounds.height(); |
| 234 | } |
| 235 | |
| 236 | SkISize dstDimensions = dstInfo.dimensions(); |
| 237 | if (bounds.size() != dstDimensions) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 238 | // Caller is requesting scaling. |
| 239 | config.options.use_scaling = 1; |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 240 | config.options.scaled_width = dstDimensions.width(); |
| 241 | config.options.scaled_height = dstDimensions.height(); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | config.output.colorspace = webp_decode_mode(dstInfo.colorType(), |
| 245 | dstInfo.alphaType() == kPremul_SkAlphaType); |
| 246 | config.output.u.RGBA.rgba = (uint8_t*) dst; |
| 247 | config.output.u.RGBA.stride = (int) rowBytes; |
| 248 | config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); |
| 249 | config.output.is_external_memory = 1; |
| 250 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 251 | SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config)); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 252 | if (!idec) { |
| 253 | return kInvalidInput; |
| 254 | } |
| 255 | |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 256 | SkAutoTMalloc<uint8_t> storage(BUFFER_SIZE); |
| 257 | uint8_t* buffer = storage.get(); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 258 | while (true) { |
| 259 | const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE); |
| 260 | if (0 == bytesRead) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 261 | WebPIDecGetRGB(idec, rowsDecoded, NULL, NULL, NULL); |
| 262 | return kIncompleteInput; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | switch (WebPIAppend(idec, buffer, bytesRead)) { |
| 266 | case VP8_STATUS_OK: |
| 267 | return kSuccess; |
| 268 | case VP8_STATUS_SUSPENDED: |
| 269 | // Break out of the switch statement. Continue the loop. |
| 270 | break; |
| 271 | default: |
| 272 | return kInvalidInput; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 277 | SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream) |
msarett | 6e077e1 | 2016-04-06 15:45:41 -0700 | [diff] [blame] | 278 | // The spec says an unmarked image is sRGB, so we return that space here. |
| 279 | // TODO: Add support for parsing ICC profiles from webps. |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 280 | : INHERITED(width, height, info, stream, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)) {} |