blob: c12b1df5edf955d547aad34892d441544528f485 [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"
msarettff2a6c82016-09-07 11:23:28 -070010#include "SkStreamPriv.h"
scroggo6f5e6192015-06-18 12:53:43 -070011#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"
msarett9d15dab2016-08-24 07:36:06 -070022#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070023#include "webp/encode.h"
24
scroggodb30be22015-12-08 18:54:13 -080025bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070026 // WEBP starts with the following:
27 // RIFFXXXXWEBPVP
28 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080029 const char* bytes = static_cast<const char*>(buf);
30 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070031}
32
scroggo6f5e6192015-06-18 12:53:43 -070033// 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.
msarettac6c7502016-04-25 09:30:24 -070036// Returns an SkWebpCodec on success;
37SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
38 SkAutoTDelete<SkStream> streamDeleter(stream);
39
msarettff2a6c82016-09-07 11:23:28 -070040 // 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);
scroggodb30be22015-12-08 18:54:13 -080047
msarettff2a6c82016-09-07 11:23:28 -070048 // 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) {
msarettac6c7502016-04-25 09:30:24 -070058 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070059 }
60
msarettff2a6c82016-09-07 11:23:28 -070061 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);
scroggo6f5e6192015-06-18 12:53:43 -070066 }
67
msarettff2a6c82016-09-07 11:23:28 -070068 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.
scroggo6f5e6192015-06-18 12:53:43 -070087 {
msarettff2a6c82016-09-07 11:23:28 -070088 const int64_t size = sk_64_mul(frame.width, frame.height);
scroggo6f5e6192015-06-18 12:53:43 -070089 if (!sk_64_isS32(size)) {
msarettac6c7502016-04-25 09:30:24 -070090 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070091 }
92 // now check that if we are 4-bytes per pixel, we also don't overflow
93 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
msarettac6c7502016-04-25 09:30:24 -070094 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -070095 }
96 }
97
msarettff2a6c82016-09-07 11:23:28 -070098 // 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
msarettac6c7502016-04-25 09:30:24 -0700108 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;
msarettc30c4182016-04-20 11:53:35 -0700126 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -0700127 } else {
128 color = SkEncodedInfo::kYUV_Color;
129 alpha = SkEncodedInfo::kOpaque_Alpha;
130 }
131 break;
132 case 2:
133 // This is the lossless format (BGRA).
msarettac6c7502016-04-25 09:30:24 -0700134 color = SkEncodedInfo::kBGRA_Color;
135 alpha = SkEncodedInfo::kUnpremul_Alpha;
136 break;
137 default:
138 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -0700139 }
scroggo6f5e6192015-06-18 12:53:43 -0700140
msarettac6c7502016-04-25 09:30:24 -0700141 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
msarettff2a6c82016-09-07 11:23:28 -0700142 return new SkWebpCodec(features.width, features.height, info, std::move(colorSpace),
143 streamDeleter.release(), demux.release(), std::move(data));
scroggo6f5e6192015-06-18 12:53:43 -0700144}
145
scroggocc2feb12015-08-14 08:32:46 -0700146// This version is slightly different from SkCodecPriv's version of conversion_possible. It
147// supports both byte orders for 8888.
148static bool webp_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) {
scroggocc2feb12015-08-14 08:32:46 -0700149 if (!valid_alpha(dst.alphaType(), src.alphaType())) {
150 return false;
151 }
152
scroggo6f5e6192015-06-18 12:53:43 -0700153 switch (dst.colorType()) {
154 // Both byte orders are supported.
155 case kBGRA_8888_SkColorType:
156 case kRGBA_8888_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700157 return true;
scroggo74992b52015-08-06 13:50:15 -0700158 case kRGB_565_SkColorType:
scroggocc2feb12015-08-14 08:32:46 -0700159 return src.alphaType() == kOpaque_SkAlphaType;
scroggo6f5e6192015-06-18 12:53:43 -0700160 default:
161 return false;
162 }
scroggo6f5e6192015-06-18 12:53:43 -0700163}
164
165SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
166 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700167 // 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));
scroggo6f5e6192015-06-18 12:53:43 -0700171 return dim;
172}
173
scroggoe7fc14b2015-10-02 13:14:46 -0700174bool 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
scroggo6f5e6192015-06-18 12:53:43 -0700180static 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;
scroggo74992b52015-08-06 13:50:15 -0700186 case kRGB_565_SkColorType:
187 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700188 default:
189 return MODE_LAST;
190 }
191}
192
scroggob636b452015-07-22 07:16:20 -0700193bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
194 if (!desiredSubset) {
195 return false;
196 }
197
msarettfdb47572015-10-13 12:50:14 -0700198 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
199 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700200 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
scroggoeb602a52015-07-09 08:16:03 -0700211SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
msarette6dd0042015-10-09 11:07:34 -0700212 const Options& options, SkPMColor*, int*,
213 int* rowsDecoded) {
scroggocc2feb12015-08-14 08:32:46 -0700214 if (!webp_conversion_possible(dstInfo, this->getInfo())) {
scroggo6f5e6192015-06-18 12:53:43 -0700215 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
scroggob636b452015-07-22 07:16:20 -0700228 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) {
scroggo6f5e6192015-06-18 12:53:43 -0700266 // Caller is requesting scaling.
267 config.options.use_scaling = 1;
scroggob636b452015-07-22 07:16:20 -0700268 config.options.scaled_width = dstDimensions.width();
269 config.options.scaled_height = dstDimensions.height();
scroggo6f5e6192015-06-18 12:53:43 -0700270 }
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
msarettff2a6c82016-09-07 11:23:28 -0700279 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
halcanary96fcdcc2015-08-27 07:41:13 -0700284 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700285 if (!idec) {
286 return kInvalidInput;
287 }
288
msarettff2a6c82016-09-07 11:23:28 -0700289 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);
msarette6dd0042015-10-09 11:07:34 -0700294 return kIncompleteInput;
msarettff2a6c82016-09-07 11:23:28 -0700295 default:
296 return kInvalidInput;
scroggo6f5e6192015-06-18 12:53:43 -0700297 }
298}
299
msarett9d15dab2016-08-24 07:36:06 -0700300SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
msarettff2a6c82016-09-07 11:23:28 -0700301 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))
msarett9d15dab2016-08-24 07:36:06 -0700306{}