blob: e26f46d07477e9a2fa1eb8e5a94a6391f1c87482 [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
Hal Canaryc640d0d2018-06-13 09:59:02 -04008#include "SkWebpCodec.h"
9
10#include "../jumper/SkJumper.h"
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040011#include "SkBitmap.h"
12#include "SkCanvas.h"
Leon Scroggins III33deb7e2017-06-07 12:31:51 -040013#include "SkCodecAnimation.h"
14#include "SkCodecAnimationPriv.h"
scroggocc2feb12015-08-14 08:32:46 -070015#include "SkCodecPriv.h"
msarette99883f2016-09-08 06:05:35 -070016#include "SkColorSpaceXform.h"
Mike Reedede7bac2017-07-23 15:30:02 -040017#include "SkMakeUnique.h"
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040018#include "SkRasterPipeline.h"
Matt Sarett5c496172017-02-07 17:01:16 -050019#include "SkSampler.h"
msarettff2a6c82016-09-07 11:23:28 -070020#include "SkStreamPriv.h"
scroggo6f5e6192015-06-18 12:53:43 -070021#include "SkTemplates.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040022#include "SkTo.h"
scroggo6f5e6192015-06-18 12:53:43 -070023
24// A WebP decoder on top of (subset of) libwebp
25// For more information on WebP image format, and libwebp library, see:
26// https://code.google.com/speed/webp/
27// http://www.webmproject.org/code/#libwebp-webp-image-library
28// https://chromium.googlesource.com/webm/libwebp
29
30// If moving libwebp out of skia source tree, path for webp headers must be
31// updated accordingly. Here, we enforce using local copy in webp sub-directory.
32#include "webp/decode.h"
msarett9d15dab2016-08-24 07:36:06 -070033#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070034#include "webp/encode.h"
35
scroggodb30be22015-12-08 18:54:13 -080036bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070037 // WEBP starts with the following:
38 // RIFFXXXXWEBPVP
39 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080040 const char* bytes = static_cast<const char*>(buf);
41 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070042}
43
scroggo6f5e6192015-06-18 12:53:43 -070044// Parse headers of RIFF container, and check for valid Webp (VP8) content.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040045// Returns an SkWebpCodec on success
Mike Reedede7bac2017-07-23 15:30:02 -040046std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
47 Result* result) {
msarettff2a6c82016-09-07 11:23:28 -070048 // Webp demux needs a contiguous data buffer.
49 sk_sp<SkData> data = nullptr;
50 if (stream->getMemoryBase()) {
51 // It is safe to make without copy because we'll hold onto the stream.
52 data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
53 } else {
Mike Reedede7bac2017-07-23 15:30:02 -040054 data = SkCopyStreamToData(stream.get());
scroggodb30be22015-12-08 18:54:13 -080055
msarettff2a6c82016-09-07 11:23:28 -070056 // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
Mike Reedede7bac2017-07-23 15:30:02 -040057 stream.reset(nullptr);
msarettff2a6c82016-09-07 11:23:28 -070058 }
59
60 // It's a little strange that the |demux| will outlive |webpData|, though it needs the
61 // pointer in |webpData| to remain valid. This works because the pointer remains valid
62 // until the SkData is freed.
63 WebPData webpData = { data->bytes(), data->size() };
Leon Scroggins III588fb042017-07-14 16:32:31 -040064 WebPDemuxState state;
65 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state));
66 switch (state) {
67 case WEBP_DEMUX_PARSE_ERROR:
68 *result = kInvalidInput;
69 return nullptr;
70 case WEBP_DEMUX_PARSING_HEADER:
71 *result = kIncompleteInput;
72 return nullptr;
73 case WEBP_DEMUX_PARSED_HEADER:
74 case WEBP_DEMUX_DONE:
75 SkASSERT(demux);
76 break;
scroggo6f5e6192015-06-18 12:53:43 -070077 }
78
Matt Sarett5c496172017-02-07 17:01:16 -050079 const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
80 const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
81
82 // Sanity check for image size that's about to be decoded.
83 {
84 const int64_t size = sk_64_mul(width, height);
Matt Sarett5c496172017-02-07 17:01:16 -050085 // now check that if we are 4-bytes per pixel, we also don't overflow
Herb Derbyc402b772017-09-20 11:56:00 -040086 if (!SkTFitsIn<int32_t>(size) || SkTo<int32_t>(size) > (0x7FFFFFFF >> 2)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040087 *result = kInvalidInput;
Matt Sarett5c496172017-02-07 17:01:16 -050088 return nullptr;
89 }
90 }
91
msarettff2a6c82016-09-07 11:23:28 -070092 sk_sp<SkColorSpace> colorSpace = nullptr;
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050093 {
94 WebPChunkIterator chunkIterator;
95 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
96 if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
97 colorSpace = SkColorSpace::MakeICC(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
98 }
99 if (!colorSpace || colorSpace->type() != SkColorSpace::kRGB_Type) {
100 colorSpace = SkColorSpace::MakeSRGB();
101 }
scroggo6f5e6192015-06-18 12:53:43 -0700102 }
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500103
104 SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
105 {
106 WebPChunkIterator chunkIterator;
107 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
108 if (WebPDemuxGetChunk(demux, "EXIF", 1, &chunkIterator)) {
109 is_orientation_marker(chunkIterator.chunk.bytes, chunkIterator.chunk.size, &origin);
110 }
msarettff2a6c82016-09-07 11:23:28 -0700111 }
112
Matt Sarett5c496172017-02-07 17:01:16 -0500113 // Get the first frame and its "features" to determine the color and alpha types.
msarettff2a6c82016-09-07 11:23:28 -0700114 WebPIterator frame;
115 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
116 if (!WebPDemuxGetFrame(demux, 1, &frame)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400117 *result = kIncompleteInput;
msarettff2a6c82016-09-07 11:23:28 -0700118 return nullptr;
119 }
120
msarettff2a6c82016-09-07 11:23:28 -0700121 WebPBitstreamFeatures features;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400122 switch (WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features)) {
123 case VP8_STATUS_OK:
124 break;
125 case VP8_STATUS_SUSPENDED:
126 case VP8_STATUS_NOT_ENOUGH_DATA:
127 *result = kIncompleteInput;
128 return nullptr;
129 default:
130 *result = kInvalidInput;
131 return nullptr;
msarettff2a6c82016-09-07 11:23:28 -0700132 }
133
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400134 const bool hasAlpha = SkToBool(frame.has_alpha)
135 || frame.width != width || frame.height != height;
msarettac6c7502016-04-25 09:30:24 -0700136 SkEncodedInfo::Color color;
137 SkEncodedInfo::Alpha alpha;
138 switch (features.format) {
139 case 0:
Matt Sarett5c496172017-02-07 17:01:16 -0500140 // This indicates a "mixed" format. We could see this for
141 // animated webps (multiple fragments).
msarettac6c7502016-04-25 09:30:24 -0700142 // We could also guess kYUV here, but I think it makes more
143 // sense to guess kBGRA which is likely closer to the final
144 // output. Otherwise, we might end up converting
145 // BGRA->YUVA->BGRA.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400146 // Fallthrough:
147 case 2:
148 // This is the lossless format (BGRA).
149 if (hasAlpha) {
150 color = SkEncodedInfo::kBGRA_Color;
151 alpha = SkEncodedInfo::kUnpremul_Alpha;
152 } else {
153 color = SkEncodedInfo::kBGRX_Color;
154 alpha = SkEncodedInfo::kOpaque_Alpha;
155 }
msarettac6c7502016-04-25 09:30:24 -0700156 break;
157 case 1:
158 // This is the lossy format (YUV).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400159 if (hasAlpha) {
msarettac6c7502016-04-25 09:30:24 -0700160 color = SkEncodedInfo::kYUVA_Color;
msarettc30c4182016-04-20 11:53:35 -0700161 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -0700162 } else {
163 color = SkEncodedInfo::kYUV_Color;
164 alpha = SkEncodedInfo::kOpaque_Alpha;
165 }
166 break;
msarettac6c7502016-04-25 09:30:24 -0700167 default:
Leon Scroggins III588fb042017-07-14 16:32:31 -0400168 *result = kInvalidInput;
msarettac6c7502016-04-25 09:30:24 -0700169 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -0700170 }
scroggo6f5e6192015-06-18 12:53:43 -0700171
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500172
Leon Scroggins III588fb042017-07-14 16:32:31 -0400173 *result = kSuccess;
msarettac6c7502016-04-25 09:30:24 -0700174 SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
Mike Reedede7bac2017-07-23 15:30:02 -0400175 return std::unique_ptr<SkCodec>(new SkWebpCodec(width, height, info, std::move(colorSpace),
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500176 std::move(stream), demux.release(), std::move(data),
177 origin));
scroggo6f5e6192015-06-18 12:53:43 -0700178}
179
scroggo6f5e6192015-06-18 12:53:43 -0700180SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
181 SkISize dim = this->getInfo().dimensions();
msaretta0c414d2015-06-19 07:34:30 -0700182 // SkCodec treats zero dimensional images as errors, so the minimum size
183 // that we will recommend is 1x1.
184 dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
185 dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
scroggo6f5e6192015-06-18 12:53:43 -0700186 return dim;
187}
188
scroggoe7fc14b2015-10-02 13:14:46 -0700189bool SkWebpCodec::onDimensionsSupported(const SkISize& dim) {
190 const SkImageInfo& info = this->getInfo();
191 return dim.width() >= 1 && dim.width() <= info.width()
192 && dim.height() >= 1 && dim.height() <= info.height();
193}
194
Leon Scroggins III03588412017-11-17 08:07:32 -0500195static WEBP_CSP_MODE webp_decode_mode(SkColorType dstCT, bool premultiply) {
196 switch (dstCT) {
scroggo6f5e6192015-06-18 12:53:43 -0700197 case kBGRA_8888_SkColorType:
198 return premultiply ? MODE_bgrA : MODE_BGRA;
199 case kRGBA_8888_SkColorType:
200 return premultiply ? MODE_rgbA : MODE_RGBA;
scroggo74992b52015-08-06 13:50:15 -0700201 case kRGB_565_SkColorType:
202 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700203 default:
204 return MODE_LAST;
205 }
206}
207
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400208SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
209 const int i = this->size();
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500210 fFrames.emplace_back(i, hasAlpha ? SkEncodedInfo::kUnpremul_Alpha
211 : SkEncodedInfo::kOpaque_Alpha);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400212 return &fFrames[i];
213}
214
scroggob636b452015-07-22 07:16:20 -0700215bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
216 if (!desiredSubset) {
217 return false;
218 }
219
msarettfdb47572015-10-13 12:50:14 -0700220 SkIRect dimensions = SkIRect::MakeSize(this->getInfo().dimensions());
221 if (!dimensions.contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700222 return false;
223 }
224
225 // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
226 // decode this exact subset.
227 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
228 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
229 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
230 return true;
231}
232
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400233int SkWebpCodec::onGetRepetitionCount() {
234 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
235 if (!(flags & ANIMATION_FLAG)) {
236 return 0;
237 }
238
239 const int repCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
240 if (0 == repCount) {
241 return kRepetitionCountInfinite;
242 }
243
244 return repCount;
245}
246
247int SkWebpCodec::onGetFrameCount() {
248 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
249 if (!(flags & ANIMATION_FLAG)) {
250 return 1;
251 }
252
253 const uint32_t oldFrameCount = fFrameHolder.size();
254 if (fFailed) {
255 return oldFrameCount;
256 }
257
258 const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
259 if (oldFrameCount == frameCount) {
260 // We have already parsed this.
261 return frameCount;
262 }
263
264 fFrameHolder.reserve(frameCount);
265
266 for (uint32_t i = oldFrameCount; i < frameCount; i++) {
267 WebPIterator iter;
268 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
269
270 if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
271 fFailed = true;
272 break;
273 }
274
275 // libwebp only reports complete frames of an animated image.
276 SkASSERT(iter.complete);
277
278 Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha);
279 frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height);
280 frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ?
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400281 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
282 SkCodecAnimation::DisposalMethod::kKeep);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400283 frame->setDuration(iter.duration);
284 if (WEBP_MUX_BLEND != iter.blend_method) {
285 frame->setBlend(SkCodecAnimation::Blend::kBG);
286 }
287 fFrameHolder.setAlphaAndRequiredFrame(frame);
288 }
289
290 return fFrameHolder.size();
291
292}
293
294const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
295 return static_cast<const SkFrame*>(this->frame(i));
296}
297
298const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
299 SkASSERT(i >= 0 && i < this->size());
300 return &fFrames[i];
301}
302
303bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
304 if (i >= fFrameHolder.size()) {
305 return false;
306 }
307
308 const Frame* frame = fFrameHolder.frame(i);
309 if (!frame) {
310 return false;
311 }
312
313 if (frameInfo) {
314 frameInfo->fRequiredFrame = frame->getRequiredFrame();
315 frameInfo->fDuration = frame->getDuration();
316 // libwebp only reports fully received frames for an
317 // animated image.
318 frameInfo->fFullyReceived = true;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500319 frameInfo->fAlphaType = frame->hasAlpha() ? kUnpremul_SkAlphaType
320 : kOpaque_SkAlphaType;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400321 frameInfo->fDisposalMethod = frame->getDisposalMethod();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400322 }
323
324 return true;
325}
326
327static bool is_8888(SkColorType colorType) {
328 switch (colorType) {
329 case kRGBA_8888_SkColorType:
330 case kBGRA_8888_SkColorType:
331 return true;
332 default:
333 return false;
334 }
335}
336
337static void pick_memory_stages(SkColorType ct, SkRasterPipeline::StockStage* load,
338 SkRasterPipeline::StockStage* store) {
339 switch(ct) {
340 case kUnknown_SkColorType:
341 case kAlpha_8_SkColorType:
342 case kARGB_4444_SkColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400343 case kGray_8_SkColorType:
Brian Salomone41e1762018-01-25 14:07:47 -0500344 case kRGB_888x_SkColorType:
Brian Salomone41e1762018-01-25 14:07:47 -0500345 case kRGB_101010x_SkColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400346 SkASSERT(false);
347 break;
348 case kRGB_565_SkColorType:
349 if (load) *load = SkRasterPipeline::load_565;
350 if (store) *store = SkRasterPipeline::store_565;
351 break;
352 case kRGBA_8888_SkColorType:
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400353 if (load) *load = SkRasterPipeline::load_8888;
354 if (store) *store = SkRasterPipeline::store_8888;
355 break;
Mike Kleinc2d20762017-06-27 19:53:21 -0400356 case kBGRA_8888_SkColorType:
357 if (load) *load = SkRasterPipeline::load_bgra;
358 if (store) *store = SkRasterPipeline::store_bgra;
359 break;
Mike Kleinac568a92018-01-25 09:09:32 -0500360 case kRGBA_1010102_SkColorType:
361 if (load) *load = SkRasterPipeline::load_1010102;
362 if (store) *store = SkRasterPipeline::store_1010102;
363 break;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400364 case kRGBA_F16_SkColorType:
365 if (load) *load = SkRasterPipeline::load_f16;
366 if (store) *store = SkRasterPipeline::store_f16;
367 break;
368 }
369}
370
Leon Scroggins III03588412017-11-17 08:07:32 -0500371// Requires that the src input be unpremultiplied (or opaque).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400372static void blend_line(SkColorType dstCT, void* dst,
Mike Klein45c16fa2017-07-18 18:15:13 -0400373 SkColorType srcCT, const void* src,
Leon Scroggins III03588412017-11-17 08:07:32 -0500374 SkAlphaType dstAt,
375 bool srcHasAlpha,
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400376 int width) {
Mike Klein45c16fa2017-07-18 18:15:13 -0400377 SkJumper_MemoryCtx dst_ctx = { (void*)dst, 0 },
378 src_ctx = { (void*)src, 0 };
379
Mike Kleinb24704d2017-05-24 07:53:00 -0400380 SkRasterPipeline_<256> p;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400381 SkRasterPipeline::StockStage load_dst, store_dst;
382 pick_memory_stages(dstCT, &load_dst, &store_dst);
383
384 // Load the final dst.
Mike Klein45c16fa2017-07-18 18:15:13 -0400385 p.append(load_dst, &dst_ctx);
Leon Scroggins III03588412017-11-17 08:07:32 -0500386 if (kUnpremul_SkAlphaType == dstAt) {
387 p.append(SkRasterPipeline::premul);
388 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400389 p.append(SkRasterPipeline::move_src_dst);
390
391 // Load the src.
392 SkRasterPipeline::StockStage load_src;
393 pick_memory_stages(srcCT, &load_src, nullptr);
Mike Klein45c16fa2017-07-18 18:15:13 -0400394 p.append(load_src, &src_ctx);
Leon Scroggins III03588412017-11-17 08:07:32 -0500395 if (srcHasAlpha) {
396 p.append(SkRasterPipeline::premul);
397 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400398
399 p.append(SkRasterPipeline::srcover);
400
401 // Convert back to dst.
Leon Scroggins III03588412017-11-17 08:07:32 -0500402 if (kUnpremul_SkAlphaType == dstAt) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400403 p.append(SkRasterPipeline::unpremul);
404 }
Mike Klein45c16fa2017-07-18 18:15:13 -0400405 p.append(store_dst, &dst_ctx);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400406
Mike Klein45c16fa2017-07-18 18:15:13 -0400407 p.run(0,0, width,1);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400408}
409
scroggoeb602a52015-07-09 08:16:03 -0700410SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000411 const Options& options, int* rowsDecodedPtr) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400412 const int index = options.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400413 SkASSERT(0 == index || index < fFrameHolder.size());
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400414
415 const auto& srcInfo = this->getInfo();
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500416 SkASSERT(0 == index || !options.fSubset);
scroggo6f5e6192015-06-18 12:53:43 -0700417
418 WebPDecoderConfig config;
419 if (0 == WebPInitDecoderConfig(&config)) {
420 // ABI mismatch.
421 // FIXME: New enum for this?
422 return kInvalidInput;
423 }
424
425 // Free any memory associated with the buffer. Must be called last, so we declare it first.
426 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
427
Matt Sarett5c496172017-02-07 17:01:16 -0500428 WebPIterator frame;
429 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400430 // If this succeeded in onGetFrameCount(), it should succeed again here.
431 SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
Matt Sarett604971e2017-02-06 09:51:48 -0500432
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400433 const bool independent = index == 0 ? true :
434 (fFrameHolder.frame(index)->getRequiredFrame() == kNone);
Matt Sarett5c496172017-02-07 17:01:16 -0500435 // Get the frameRect. libwebp will have already signaled an error if this is not fully
436 // contained by the canvas.
437 auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400438 SkASSERT(srcInfo.bounds().contains(frameRect));
439 const bool frameIsSubset = frameRect != srcInfo.bounds();
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400440 if (independent && frameIsSubset) {
441 SkSampler::Fill(dstInfo, dst, rowBytes, 0, options.fZeroInitialized);
Matt Sarett604971e2017-02-06 09:51:48 -0500442 }
443
Matt Sarett5c496172017-02-07 17:01:16 -0500444 int dstX = frameRect.x();
445 int dstY = frameRect.y();
446 int subsetWidth = frameRect.width();
447 int subsetHeight = frameRect.height();
448 if (options.fSubset) {
449 SkIRect subset = *options.fSubset;
450 SkASSERT(this->getInfo().bounds().contains(subset));
451 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
452 SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
453
454 if (!SkIRect::IntersectsNoEmptyCheck(subset, frameRect)) {
455 return kSuccess;
456 }
457
458 int minXOffset = SkTMin(dstX, subset.x());
459 int minYOffset = SkTMin(dstY, subset.y());
460 dstX -= minXOffset;
461 dstY -= minYOffset;
462 frameRect.offset(-minXOffset, -minYOffset);
463 subset.offset(-minXOffset, -minYOffset);
464
465 // Just like we require that the requested subset x and y offset are even, libwebp
466 // guarantees that the frame x and y offset are even (it's actually impossible to specify
467 // an odd frame offset). So we can still guarantee that the adjusted offsets are even.
468 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
469
470 SkIRect intersection;
471 SkAssertResult(intersection.intersect(frameRect, subset));
472 subsetWidth = intersection.width();
473 subsetHeight = intersection.height();
474
475 config.options.use_cropping = 1;
476 config.options.crop_left = subset.x();
477 config.options.crop_top = subset.y();
478 config.options.crop_width = subsetWidth;
479 config.options.crop_height = subsetHeight;
480 }
481
482 // Ignore the frame size and offset when determining if scaling is necessary.
483 int scaledWidth = subsetWidth;
484 int scaledHeight = subsetHeight;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400485 SkISize srcSize = options.fSubset ? options.fSubset->size() : srcInfo.dimensions();
Matt Sarett5c496172017-02-07 17:01:16 -0500486 if (srcSize != dstInfo.dimensions()) {
scroggo6f5e6192015-06-18 12:53:43 -0700487 config.options.use_scaling = 1;
Matt Sarett5c496172017-02-07 17:01:16 -0500488
489 if (frameIsSubset) {
490 float scaleX = ((float) dstInfo.width()) / srcSize.width();
491 float scaleY = ((float) dstInfo.height()) / srcSize.height();
492
493 // We need to be conservative here and floor rather than round.
494 // Otherwise, we may find ourselves decoding off the end of memory.
495 dstX = scaleX * dstX;
496 scaledWidth = scaleX * scaledWidth;
497 dstY = scaleY * dstY;
498 scaledHeight = scaleY * scaledHeight;
499 if (0 == scaledWidth || 0 == scaledHeight) {
500 return kSuccess;
501 }
502 } else {
503 scaledWidth = dstInfo.width();
504 scaledHeight = dstInfo.height();
505 }
506
507 config.options.scaled_width = scaledWidth;
508 config.options.scaled_height = scaledHeight;
scroggo6f5e6192015-06-18 12:53:43 -0700509 }
510
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400511 const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400512 && frame.has_alpha;
513 if (blendWithPrevFrame && options.fPremulBehavior == SkTransferFunctionBehavior::kRespect) {
514 // Blending is done with SkRasterPipeline, which requires a color space that is valid for
515 // rendering.
516 const auto* cs = dstInfo.colorSpace();
517 if (!cs || (!cs->gammaCloseToSRGB() && !cs->gammaIsLinear())) {
518 return kInvalidConversion;
519 }
520 }
521
522 SkBitmap webpDst;
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400523 auto webpInfo = dstInfo;
524 if (!frame.has_alpha) {
525 webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
526 }
527 if (this->colorXform()) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400528 // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a
529 // color transform, we should decode to whatever is easiest for libwebp, and then let the
530 // color transform swizzle if necessary.
531 // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost). Lossless webp is
532 // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400533 webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400534
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400535 if (webpInfo.alphaType() == kPremul_SkAlphaType) {
536 webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
537 }
538 }
539
540 if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400541 // We will decode the entire image and then perform the color transform. libwebp
542 // does not provide a row-by-row API. This is a shame particularly when we do not want
543 // 8888, since we will need to create another image sized buffer.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400544 webpDst.allocPixels(webpInfo);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400545 } else {
546 // libwebp can decode directly into the output memory.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400547 webpDst.installPixels(webpInfo, dst, rowBytes);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400548 }
549
Leon Scroggins III03588412017-11-17 08:07:32 -0500550 // Choose the step when we will perform premultiplication.
551 enum {
552 kNone,
553 kBlendLine,
554 kColorXform,
555 kLibwebp,
556 };
557 auto choose_premul_step = [&]() {
558 if (!frame.has_alpha) {
559 // None necessary.
560 return kNone;
561 }
562 if (blendWithPrevFrame) {
563 // Premultiply in blend_line, in a linear space.
564 return kBlendLine;
565 }
566 if (dstInfo.alphaType() != kPremul_SkAlphaType) {
567 // No blending is necessary, so we only need to premultiply if the
568 // client requested it.
569 return kNone;
570 }
571 if (this->colorXform()) {
572 // Premultiply in the colorXform, in a linear space.
573 return kColorXform;
574 }
575 return kLibwebp;
576 };
577 const auto premulStep = choose_premul_step();
578 config.output.colorspace = webp_decode_mode(webpInfo.colorType(), premulStep == kLibwebp);
scroggo6f5e6192015-06-18 12:53:43 -0700579 config.output.is_external_memory = 1;
580
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400581 config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
582 config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
Mike Reedf0ffb892017-10-03 14:47:21 -0400583 config.output.u.RGBA.size = webpDst.computeByteSize();
msarettff2a6c82016-09-07 11:23:28 -0700584
halcanary96fcdcc2015-08-27 07:41:13 -0700585 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700586 if (!idec) {
587 return kInvalidInput;
588 }
589
Leon Scroggins IIIe5677462017-09-27 16:31:08 -0400590 int rowsDecoded = 0;
msarette99883f2016-09-08 06:05:35 -0700591 SkCodec::Result result;
msarettff2a6c82016-09-07 11:23:28 -0700592 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
593 case VP8_STATUS_OK:
Matt Sarett5c496172017-02-07 17:01:16 -0500594 rowsDecoded = scaledHeight;
msarette99883f2016-09-08 06:05:35 -0700595 result = kSuccess;
596 break;
msarettff2a6c82016-09-07 11:23:28 -0700597 case VP8_STATUS_SUSPENDED:
Leon Scroggins IIIe5677462017-09-27 16:31:08 -0400598 if (!WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr)
599 || rowsDecoded <= 0) {
600 return kInvalidInput;
601 }
Matt Sarett5c496172017-02-07 17:01:16 -0500602 *rowsDecodedPtr = rowsDecoded + dstY;
msarette99883f2016-09-08 06:05:35 -0700603 result = kIncompleteInput;
604 break;
msarettff2a6c82016-09-07 11:23:28 -0700605 default:
606 return kInvalidInput;
scroggo6f5e6192015-06-18 12:53:43 -0700607 }
msarette99883f2016-09-08 06:05:35 -0700608
Mike Reed7fcfb622018-02-09 13:26:46 -0500609 const size_t dstBpp = dstInfo.bytesPerPixel();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400610 dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
611 const size_t srcRowBytes = config.output.u.RGBA.stride;
612
613 const auto dstCT = dstInfo.colorType();
Matt Sarett313c4632016-10-20 12:35:23 -0400614 if (this->colorXform()) {
Matt Sarett5c496172017-02-07 17:01:16 -0500615 uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400616 SkBitmap tmp;
617 void* xformDst;
618
619 if (blendWithPrevFrame) {
620 // Xform into temporary bitmap big enough for one row.
621 tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
622 xformDst = tmp.getPixels();
623 } else {
624 xformDst = dst;
625 }
Leon Scroggins III03588412017-11-17 08:07:32 -0500626
627 const auto xformAlphaType = (premulStep == kColorXform) ? kPremul_SkAlphaType :
628 ( frame.has_alpha) ? kUnpremul_SkAlphaType :
629 kOpaque_SkAlphaType ;
Robert Phillipsb3050b92017-02-06 13:12:18 +0000630 for (int y = 0; y < rowsDecoded; y++) {
Leon Scroggins2009c202017-11-15 13:49:19 +0000631 this->applyColorXform(xformDst, xformSrc, scaledWidth, xformAlphaType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400632 if (blendWithPrevFrame) {
Mike Klein76f57062018-06-08 14:00:19 -0400633 blend_line(dstCT, dst, dstCT, xformDst,
Leon Scroggins III03588412017-11-17 08:07:32 -0500634 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400635 dst = SkTAddOffset<void>(dst, rowBytes);
636 } else {
637 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
638 }
Matt Sarett5c496172017-02-07 17:01:16 -0500639 xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
msarette99883f2016-09-08 06:05:35 -0700640 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400641 } else if (blendWithPrevFrame) {
642 const uint8_t* src = config.output.u.RGBA.rgba;
643
644 for (int y = 0; y < rowsDecoded; y++) {
Mike Klein76f57062018-06-08 14:00:19 -0400645 blend_line(dstCT, dst, webpDst.colorType(), src,
Leon Scroggins III03588412017-11-17 08:07:32 -0500646 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400647 src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
648 dst = SkTAddOffset<void>(dst, rowBytes);
649 }
msarette99883f2016-09-08 06:05:35 -0700650 }
651
652 return result;
scroggo6f5e6192015-06-18 12:53:43 -0700653}
654
msarett9d15dab2016-08-24 07:36:06 -0700655SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
Mike Reedede7bac2017-07-23 15:30:02 -0400656 sk_sp<SkColorSpace> colorSpace, std::unique_ptr<SkStream> stream,
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500657 WebPDemuxer* demux, sk_sp<SkData> data, SkEncodedOrigin origin)
Mike Reedede7bac2017-07-23 15:30:02 -0400658 : INHERITED(width, height, info, SkColorSpaceXform::kBGRA_8888_ColorFormat, std::move(stream),
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500659 std::move(colorSpace), origin)
msarettff2a6c82016-09-07 11:23:28 -0700660 , fDemux(demux)
661 , fData(std::move(data))
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400662 , fFailed(false)
663{
664 fFrameHolder.setScreenSize(width, height);
665}