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