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