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 "SkCodecPriv.h" 9 #include "SkWebpCodec.h" 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(const void* buf, size_t bytesRead) { 24 // WEBP starts with the following: 25 // RIFFXXXXWEBPVP 26 // Where XXXX is unspecified. 27 const char* bytes = static_cast<const char*>(buf); 28 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); 29 } 30 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. 34 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { 35 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; 36 SkASSERT(WEBP_VP8_HEADER_SIZE <= SkCodec::MinBufferedBytesNeeded()); 37 38 const size_t bytesPeeked = stream->peek(buffer, WEBP_VP8_HEADER_SIZE); 39 if (bytesPeeked != WEBP_VP8_HEADER_SIZE) { 40 // Use read + rewind as a backup 41 if (stream->read(buffer, WEBP_VP8_HEADER_SIZE) != WEBP_VP8_HEADER_SIZE 42 || !stream->rewind()) 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? 66 // Is unpremul the right type? Clients of SkCodec may assume it's the 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)) { 80 return new SkWebpCodec(info, streamDeleter.detach()); 81 } 82 return nullptr; 83 } 84 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) { 88 if (dst.profileType() != src.profileType()) { 89 return false; 90 } 91 92 if (!valid_alpha(dst.alphaType(), src.alphaType())) { 93 return false; 94 } 95 96 switch (dst.colorType()) { 97 // Both byte orders are supported. 98 case kBGRA_8888_SkColorType: 99 case kRGBA_8888_SkColorType: 100 return true; 101 case kRGB_565_SkColorType: 102 return src.alphaType() == kOpaque_SkAlphaType; 103 default: 104 return false; 105 } 106 } 107 108 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { 109 SkISize dim = this->getInfo().dimensions(); 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)); 114 return dim; 115 } 116 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 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; 130 case kRGB_565_SkColorType: 131 return MODE_RGB_565; 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 142 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { 143 if (!desiredSubset) { 144 return false; 145 } 146 147 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions()); 148 if (!dimensions.contains(*desiredSubset)) { 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 160 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, 161 const Options& options, SkPMColor*, int*, 162 int* rowsDecoded) { 163 if (!webp_conversion_possible(dstInfo, this->getInfo())) { 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 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) { 215 // Caller is requesting scaling. 216 config.options.use_scaling = 1; 217 config.options.scaled_width = dstDimensions.width(); 218 config.options.scaled_height = dstDimensions.height(); 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 228 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config)); 229 if (!idec) { 230 return kInvalidInput; 231 } 232 233 SkAutoTMalloc<uint8_t> storage(BUFFER_SIZE); 234 uint8_t* buffer = storage.get(); 235 while (true) { 236 const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE); 237 if (0 == bytesRead) { 238 WebPIDecGetRGB(idec, rowsDecoded, NULL, NULL, NULL); 239 return kIncompleteInput; 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) {} 256