blob: c28d077bb329826750ebca4bb92ac2f005e2ac02 [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"
msarett9d15dab2016-08-24 07:36:06 -070021#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070022#include "webp/encode.h"
23
scroggodb30be22015-12-08 18:54:13 -080024bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070025 // WEBP starts with the following:
26 // RIFFXXXXWEBPVP
27 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080028 const char* bytes = static_cast<const char*>(buf);
29 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070030}
31
scroggo6f5e6192015-06-18 12:53:43 -070032// 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.
msarettac6c7502016-04-25 09:30:24 -070035// Returns an SkWebpCodec on success;
36SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
37 SkAutoTDelete<SkStream> streamDeleter(stream);
38
scroggo6f5e6192015-06-18 12:53:43 -070039 unsigned char buffer[WEBP_VP8_HEADER_SIZE];
scroggodb30be22015-12-08 18:54:13 -080040 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())
msarettac6c7502016-04-25 09:30:24 -070047 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070048 }
49
50 WebPBitstreamFeatures features;
51 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &features);
52 if (VP8_STATUS_OK != status) {
msarettac6c7502016-04-25 09:30:24 -070053 return nullptr; // Invalid WebP file.
scroggo6f5e6192015-06-18 12:53:43 -070054 }
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)) {
msarettac6c7502016-04-25 09:30:24 -070060 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070061 }
62 // now check that if we are 4-bytes per pixel, we also don't overflow
63 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
msarettac6c7502016-04-25 09:30:24 -070064 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070065 }
66 }
67
msarettac6c7502016-04-25 09:30:24 -070068 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;
msarettc30c4182016-04-20 11:53:35 -070086 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -070087 } else {
88 color = SkEncodedInfo::kYUV_Color;
89 alpha = SkEncodedInfo::kOpaque_Alpha;
90 }
91 break;
92 case 2:
93 // This is the lossless format (BGRA).
msarettac6c7502016-04-25 09:30:24 -070094 color = SkEncodedInfo::kBGRA_Color;
95 alpha = SkEncodedInfo::kUnpremul_Alpha;
96 break;
97 default:
98 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070099 }
scroggo6f5e6192015-06-18 12:53:43 -0700100
msarett9d15dab2016-08-24 07:36:06 -0700101 // 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
msarettac6c7502016-04-25 09:30:24 -0700122 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
msarett9d15dab2016-08-24 07:36:06 -0700123 return new SkWebpCodec(features.width, features.height, info, colorSpace,
124 streamDeleter.release());
scroggo6f5e6192015-06-18 12:53:43 -0700125}
126
scroggocc2feb12015-08-14 08:32:46 -0700127// This version is slightly different from SkCodecPriv's version of conversion_possible. It
128// supports both byte orders for 8888.
129static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
scroggocc2feb12015-08-14 08:32:46 -0700130 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
131 return false;
132 }
133
scroggo6f5e6192015-06-18 12:53:43 -0700134 switch (dst.colorType()) {
135 // Both byte orders are supported.
136 case kBGRA_8888_SkColorType:
137 case kRGBA_8888_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700138 return true;
scroggo74992b52015-08-06 13:50:15 -0700139 case kRGB_565_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700140 return src.alphaType() == kOpaque_SkAlphaType;
scroggo6f5e6192015-06-18 12:53:43 -0700141 default:
142 return false;
143 }
scroggo6f5e6192015-06-18 12:53:43 -0700144}
145
146SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
147 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700148 // 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));
scroggo6f5e6192015-06-18 12:53:43 -0700152 return dim;
153}
154
scroggoe7fc14b2015-10-02 13:14:46 -0700155bool 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
scroggo6f5e6192015-06-18 12:53:43 -0700162static 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;
scroggo74992b52015-08-06 13:50:15 -0700168 case kRGB_565_SkColorType:
169 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700170 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.
178static const size_t BUFFER_SIZE = 4096;
179
scroggob636b452015-07-22 07:16:20 -0700180bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
181 if (!desiredSubset) {
182 return false;
183 }
184
msarettfdb47572015-10-13 12:50:14 -0700185 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
186 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700187 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
scroggoeb602a52015-07-09 08:16:03 -0700198SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
msarette6dd0042015-10-09 11:07:34 -0700199 const Options& options, SkPMColor*, int*,
200 int* rowsDecoded) {
scroggocc2feb12015-08-14 08:32:46 -0700201 if (!webp_conversion_possible(dstInfo, this->getInfo())) {
scroggo6f5e6192015-06-18 12:53:43 -0700202 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
scroggob636b452015-07-22 07:16:20 -0700215 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) {
scroggo6f5e6192015-06-18 12:53:43 -0700253 // Caller is requesting scaling.
254 config.options.use_scaling = 1;
scroggob636b452015-07-22 07:16:20 -0700255 config.options.scaled_width = dstDimensions.width();
256 config.options.scaled_height = dstDimensions.height();
scroggo6f5e6192015-06-18 12:53:43 -0700257 }
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
halcanary96fcdcc2015-08-27 07:41:13 -0700266 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700267 if (!idec) {
268 return kInvalidInput;
269 }
270
scroggo565901d2015-12-10 10:44:13 -0800271 SkAutoTMalloc<uint8_t> storage(BUFFER_SIZE);
272 uint8_t* buffer = storage.get();
scroggo6f5e6192015-06-18 12:53:43 -0700273 while (true) {
274 const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE);
275 if (0 == bytesRead) {
msarette6dd0042015-10-09 11:07:34 -0700276 WebPIDecGetRGB(idec, rowsDecoded, NULL, NULL, NULL);
277 return kIncompleteInput;
scroggo6f5e6192015-06-18 12:53:43 -0700278 }
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
msarett9d15dab2016-08-24 07:36:06 -0700292SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
293 sk_sp<SkColorSpace> colorSpace, SkStream* stream)
294 : INHERITED(width, height, info, stream, colorSpace)
295{}