blob: cefde2d20d147513c0549ed963c24d4bbd7a3d13 [file] [log] [blame]
scroggo6f5e6192015-06-18 12:53:43 -07001/*
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
scroggocc2feb12015-08-14 08:32:46 -07008#include "SkCodecPriv.h"
scroggo6f5e6192015-06-18 12:53:43 -07009#include "SkWebpCodec.h"
scroggo6f5e6192015-06-18 12:53:43 -070010#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
scroggodb30be22015-12-08 18:54:13 -080023bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070024 // WEBP starts with the following:
25 // RIFFXXXXWEBPVP
26 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080027 const char* bytes = static_cast<const char*>(buf);
28 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070029}
30
scroggo6f5e6192015-06-18 12:53:43 -070031// 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.
msarettac6c7502016-04-25 09:30:24 -070034// Returns an SkWebpCodec on success;
35SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
36 SkAutoTDelete<SkStream> streamDeleter(stream);
37
scroggo6f5e6192015-06-18 12:53:43 -070038 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
scroggodb30be22015-12-08 18:54:13 -080039 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())
msarettac6c7502016-04-25 09:30:24 -070046 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070047 }
48
49 WebPBitstreamFeatures features;
50 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &features);
51 if (VP8_STATUS_OK != status) {
msarettac6c7502016-04-25 09:30:24 -070052 return nullptr; // Invalid WebP file.
scroggo6f5e6192015-06-18 12:53:43 -070053 }
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)) {
msarettac6c7502016-04-25 09:30:24 -070059 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070060 }
61 // now check that if we are 4-bytes per pixel, we also don't overflow
62 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
msarettac6c7502016-04-25 09:30:24 -070063 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070064 }
65 }
66
msarettac6c7502016-04-25 09:30:24 -070067 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;
msarettc30c4182016-04-20 11:53:35 -070085 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -070086 } 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;
scroggo6f5e6192015-06-18 12:53:43 -0700101 }
scroggo6f5e6192015-06-18 12:53:43 -0700102
msarettac6c7502016-04-25 09:30:24 -0700103 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
104 return new SkWebpCodec(features.width, features.height, info, streamDeleter.release());
scroggo6f5e6192015-06-18 12:53:43 -0700105}
106
scroggocc2feb12015-08-14 08:32:46 -0700107// This version is slightly different from SkCodecPriv's version of conversion_possible. It
108// supports both byte orders for 8888.
109static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
msarett6e077e12016-04-06 15:45:41 -0700110 // 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.
scroggocc2feb12015-08-14 08:32:46 -0700114
115 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
116 return false;
117 }
118
scroggo6f5e6192015-06-18 12:53:43 -0700119 switch (dst.colorType()) {
120 // Both byte orders are supported.
121 case kBGRA_8888_SkColorType:
122 case kRGBA_8888_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700123 return true;
scroggo74992b52015-08-06 13:50:15 -0700124 case kRGB_565_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700125 return src.alphaType() == kOpaque_SkAlphaType;
scroggo6f5e6192015-06-18 12:53:43 -0700126 default:
127 return false;
128 }
scroggo6f5e6192015-06-18 12:53:43 -0700129}
130
131SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
132 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700133 // 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));
scroggo6f5e6192015-06-18 12:53:43 -0700137 return dim;
138}
139
scroggoe7fc14b2015-10-02 13:14:46 -0700140bool 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
scroggo6f5e6192015-06-18 12:53:43 -0700147static 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;
scroggo74992b52015-08-06 13:50:15 -0700153 case kRGB_565_SkColorType:
154 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700155 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.
163static const size_t BUFFER_SIZE = 4096;
164
scroggob636b452015-07-22 07:16:20 -0700165bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
166 if (!desiredSubset) {
167 return false;
168 }
169
msarettfdb47572015-10-13 12:50:14 -0700170 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
171 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700172 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
scroggoeb602a52015-07-09 08:16:03 -0700183SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
msarette6dd0042015-10-09 11:07:34 -0700184 const Options& options, SkPMColor*, int*,
185 int* rowsDecoded) {
scroggocc2feb12015-08-14 08:32:46 -0700186 if (!webp_conversion_possible(dstInfo, this->getInfo())) {
scroggo6f5e6192015-06-18 12:53:43 -0700187 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
scroggob636b452015-07-22 07:16:20 -0700200 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) {
scroggo6f5e6192015-06-18 12:53:43 -0700238 // Caller is requesting scaling.
239 config.options.use_scaling = 1;
scroggob636b452015-07-22 07:16:20 -0700240 config.options.scaled_width = dstDimensions.width();
241 config.options.scaled_height = dstDimensions.height();
scroggo6f5e6192015-06-18 12:53:43 -0700242 }
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
halcanary96fcdcc2015-08-27 07:41:13 -0700251 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700252 if (!idec) {
253 return kInvalidInput;
254 }
255
scroggo565901d2015-12-10 10:44:13 -0800256 SkAutoTMalloc<uint8_t> storage(BUFFER_SIZE);
257 uint8_t* buffer = storage.get();
scroggo6f5e6192015-06-18 12:53:43 -0700258 while (true) {
259 const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE);
260 if (0 == bytesRead) {
msarette6dd0042015-10-09 11:07:34 -0700261 WebPIDecGetRGB(idec, rowsDecoded, NULL, NULL, NULL);
262 return kIncompleteInput;
scroggo6f5e6192015-06-18 12:53:43 -0700263 }
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
msarettc30c4182016-04-20 11:53:35 -0700277SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info, SkStream* stream)
msarett6e077e12016-04-06 15:45:41 -0700278 // The spec says an unmarked image is sRGB, so we return that space here.
279 // TODO: Add support for parsing ICC profiles from webps.
msarettc30c4182016-04-20 11:53:35 -0700280 : INHERITED(width, height, info, stream, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named)) {}