blob: 2137877f1e7f2986144f2694ed02537993b3f297 [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.
34static bool webp_parse_header(SkStream* stream, SkImageInfo* info) {
35 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
scroggodb30be22015-12-08 18:54:13 -080036 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())
scroggo6f5e6192015-06-18 12:53:43 -070043 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?
scroggoeb602a52015-07-09 08:16:03 -070066 // Is unpremul the right type? Clients of SkCodec may assume it's the
scroggo6f5e6192015-06-18 12:53:43 -070067 // 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
76SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
77 SkAutoTDelete<SkStream> streamDeleter(stream);
78 SkImageInfo info;
79 if (webp_parse_header(stream, &info)) {
halcanary385fe4d2015-08-26 13:07:48 -070080 return new SkWebpCodec(info, streamDeleter.detach());
scroggo6f5e6192015-06-18 12:53:43 -070081 }
halcanary96fcdcc2015-08-27 07:41:13 -070082 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070083}
84
scroggocc2feb12015-08-14 08:32:46 -070085// This version is slightly different from SkCodecPriv's version of conversion_possible. It
86// supports both byte orders for 8888.
87static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
scroggo74992b52015-08-06 13:50:15 -070088 if (dst.profileType() != src.profileType()) {
89 return false;
90 }
scroggocc2feb12015-08-14 08:32:46 -070091
92 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
93 return false;
94 }
95
scroggo6f5e6192015-06-18 12:53:43 -070096 switch (dst.colorType()) {
97 // Both byte orders are supported.
98 case kBGRA_8888_SkColorType:
99 case kRGBA_8888_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700100 return true;
scroggo74992b52015-08-06 13:50:15 -0700101 case kRGB_565_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700102 return src.alphaType() == kOpaque_SkAlphaType;
scroggo6f5e6192015-06-18 12:53:43 -0700103 default:
104 return false;
105 }
scroggo6f5e6192015-06-18 12:53:43 -0700106}
107
108SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
109 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700110 // 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));
scroggo6f5e6192015-06-18 12:53:43 -0700114 return dim;
115}
116
scroggoe7fc14b2015-10-02 13:14:46 -0700117bool 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
scroggo6f5e6192015-06-18 12:53:43 -0700124static 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;
scroggo74992b52015-08-06 13:50:15 -0700130 case kRGB_565_SkColorType:
131 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700132 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.
140static const size_t BUFFER_SIZE = 4096;
141
scroggob636b452015-07-22 07:16:20 -0700142bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
143 if (!desiredSubset) {
144 return false;
145 }
146
msarettfdb47572015-10-13 12:50:14 -0700147 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
148 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700149 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
scroggoeb602a52015-07-09 08:16:03 -0700160SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
msarette6dd0042015-10-09 11:07:34 -0700161 const Options& options, SkPMColor*, int*,
162 int* rowsDecoded) {
scroggocc2feb12015-08-14 08:32:46 -0700163 if (!webp_conversion_possible(dstInfo, this->getInfo())) {
scroggo6f5e6192015-06-18 12:53:43 -0700164 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
scroggob636b452015-07-22 07:16:20 -0700177 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) {
scroggo6f5e6192015-06-18 12:53:43 -0700215 // Caller is requesting scaling.
216 config.options.use_scaling = 1;
scroggob636b452015-07-22 07:16:20 -0700217 config.options.scaled_width = dstDimensions.width();
218 config.options.scaled_height = dstDimensions.height();
scroggo6f5e6192015-06-18 12:53:43 -0700219 }
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
halcanary96fcdcc2015-08-27 07:41:13 -0700228 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700229 if (!idec) {
230 return kInvalidInput;
231 }
232
233 SkAutoMalloc storage(BUFFER_SIZE);
234 uint8_t* buffer = static_cast<uint8_t*>(storage.get());
235 while (true) {
236 const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE);
237 if (0 == bytesRead) {
msarette6dd0042015-10-09 11:07:34 -0700238 WebPIDecGetRGB(idec, rowsDecoded, NULL, NULL, NULL);
239 return kIncompleteInput;
scroggo6f5e6192015-06-18 12:53:43 -0700240 }
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
254SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream)
255 : INHERITED(info, stream) {}