blob: 2f7a2f0c35d6e4ae4090966e63e44e42f2f30ce7 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/codec/SkWebpCodec.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/codec/SkCodecAnimation.h"
11#include "include/core/SkBitmap.h"
12#include "include/core/SkCanvas.h"
13#include "include/private/SkTemplates.h"
14#include "include/private/SkTo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/codec/SkCodecPriv.h"
Leon Scroggins IIIba458302019-09-24 12:40:11 -040016#include "src/codec/SkParseEncodedOrigin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/codec/SkSampler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/core/SkRasterPipeline.h"
19#include "src/core/SkStreamPriv.h"
scroggo6f5e6192015-06-18 12:53:43 -070020
21// A WebP decoder on top of (subset of) libwebp
22// For more information on WebP image format, and libwebp library, see:
23// https://code.google.com/speed/webp/
24// http://www.webmproject.org/code/#libwebp-webp-image-library
25// https://chromium.googlesource.com/webm/libwebp
26
27// If moving libwebp out of skia source tree, path for webp headers must be
28// updated accordingly. Here, we enforce using local copy in webp sub-directory.
29#include "webp/decode.h"
msarett9d15dab2016-08-24 07:36:06 -070030#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070031#include "webp/encode.h"
32
scroggodb30be22015-12-08 18:54:13 -080033bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070034 // WEBP starts with the following:
35 // RIFFXXXXWEBPVP
36 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080037 const char* bytes = static_cast<const char*>(buf);
38 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070039}
40
scroggo6f5e6192015-06-18 12:53:43 -070041// Parse headers of RIFF container, and check for valid Webp (VP8) content.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040042// Returns an SkWebpCodec on success
Mike Reedede7bac2017-07-23 15:30:02 -040043std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
44 Result* result) {
msarettff2a6c82016-09-07 11:23:28 -070045 // Webp demux needs a contiguous data buffer.
46 sk_sp<SkData> data = nullptr;
47 if (stream->getMemoryBase()) {
48 // It is safe to make without copy because we'll hold onto the stream.
49 data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
50 } else {
Mike Reedede7bac2017-07-23 15:30:02 -040051 data = SkCopyStreamToData(stream.get());
scroggodb30be22015-12-08 18:54:13 -080052
msarettff2a6c82016-09-07 11:23:28 -070053 // 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 -040054 stream.reset(nullptr);
msarettff2a6c82016-09-07 11:23:28 -070055 }
56
57 // It's a little strange that the |demux| will outlive |webpData|, though it needs the
58 // pointer in |webpData| to remain valid. This works because the pointer remains valid
59 // until the SkData is freed.
60 WebPData webpData = { data->bytes(), data->size() };
Leon Scroggins III588fb042017-07-14 16:32:31 -040061 WebPDemuxState state;
62 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state));
63 switch (state) {
64 case WEBP_DEMUX_PARSE_ERROR:
65 *result = kInvalidInput;
66 return nullptr;
67 case WEBP_DEMUX_PARSING_HEADER:
68 *result = kIncompleteInput;
69 return nullptr;
70 case WEBP_DEMUX_PARSED_HEADER:
71 case WEBP_DEMUX_DONE:
72 SkASSERT(demux);
73 break;
scroggo6f5e6192015-06-18 12:53:43 -070074 }
75
Matt Sarett5c496172017-02-07 17:01:16 -050076 const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
77 const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
78
Leon Scroggins III982fff22020-07-31 14:09:06 -040079 // Validate the image size that's about to be decoded.
Matt Sarett5c496172017-02-07 17:01:16 -050080 {
81 const int64_t size = sk_64_mul(width, height);
Matt Sarett5c496172017-02-07 17:01:16 -050082 // now check that if we are 4-bytes per pixel, we also don't overflow
Herb Derbyc402b772017-09-20 11:56:00 -040083 if (!SkTFitsIn<int32_t>(size) || SkTo<int32_t>(size) > (0x7FFFFFFF >> 2)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040084 *result = kInvalidInput;
Matt Sarett5c496172017-02-07 17:01:16 -050085 return nullptr;
86 }
87 }
88
Leon Scroggins III36f7e322018-08-27 11:55:46 -040089 std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050090 {
91 WebPChunkIterator chunkIterator;
92 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
93 if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -040094 // FIXME: I think this could be MakeWithoutCopy
95 auto chunk = SkData::MakeWithCopy(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
96 profile = SkEncodedInfo::ICCProfile::Make(std::move(chunk));
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050097 }
Leon Scroggins III5dd47e42018-09-27 15:26:48 -040098 if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
99 profile = nullptr;
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500100 }
scroggo6f5e6192015-06-18 12:53:43 -0700101 }
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500102
103 SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
104 {
105 WebPChunkIterator chunkIterator;
106 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
107 if (WebPDemuxGetChunk(demux, "EXIF", 1, &chunkIterator)) {
Leon Scroggins IIIba458302019-09-24 12:40:11 -0400108 SkParseEncodedOrigin(chunkIterator.chunk.bytes, chunkIterator.chunk.size, &origin);
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500109 }
msarettff2a6c82016-09-07 11:23:28 -0700110 }
111
Matt Sarett5c496172017-02-07 17:01:16 -0500112 // Get the first frame and its "features" to determine the color and alpha types.
msarettff2a6c82016-09-07 11:23:28 -0700113 WebPIterator frame;
114 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
115 if (!WebPDemuxGetFrame(demux, 1, &frame)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400116 *result = kIncompleteInput;
msarettff2a6c82016-09-07 11:23:28 -0700117 return nullptr;
118 }
119
msarettff2a6c82016-09-07 11:23:28 -0700120 WebPBitstreamFeatures features;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400121 switch (WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features)) {
122 case VP8_STATUS_OK:
123 break;
124 case VP8_STATUS_SUSPENDED:
125 case VP8_STATUS_NOT_ENOUGH_DATA:
126 *result = kIncompleteInput;
127 return nullptr;
128 default:
129 *result = kInvalidInput;
130 return nullptr;
msarettff2a6c82016-09-07 11:23:28 -0700131 }
132
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400133 const bool hasAlpha = SkToBool(frame.has_alpha)
134 || frame.width != width || frame.height != height;
msarettac6c7502016-04-25 09:30:24 -0700135 SkEncodedInfo::Color color;
136 SkEncodedInfo::Alpha alpha;
137 switch (features.format) {
138 case 0:
Matt Sarett5c496172017-02-07 17:01:16 -0500139 // This indicates a "mixed" format. We could see this for
140 // animated webps (multiple fragments).
msarettac6c7502016-04-25 09:30:24 -0700141 // We could also guess kYUV here, but I think it makes more
142 // sense to guess kBGRA which is likely closer to the final
143 // output. Otherwise, we might end up converting
144 // BGRA->YUVA->BGRA.
John Stiles30212b72020-06-11 17:55:07 -0400145 [[fallthrough]];
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400146 case 2:
147 // This is the lossless format (BGRA).
148 if (hasAlpha) {
149 color = SkEncodedInfo::kBGRA_Color;
150 alpha = SkEncodedInfo::kUnpremul_Alpha;
151 } else {
152 color = SkEncodedInfo::kBGRX_Color;
153 alpha = SkEncodedInfo::kOpaque_Alpha;
154 }
msarettac6c7502016-04-25 09:30:24 -0700155 break;
156 case 1:
157 // This is the lossy format (YUV).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400158 if (hasAlpha) {
msarettac6c7502016-04-25 09:30:24 -0700159 color = SkEncodedInfo::kYUVA_Color;
msarettc30c4182016-04-20 11:53:35 -0700160 alpha = SkEncodedInfo::kUnpremul_Alpha;
msarettac6c7502016-04-25 09:30:24 -0700161 } else {
162 color = SkEncodedInfo::kYUV_Color;
163 alpha = SkEncodedInfo::kOpaque_Alpha;
164 }
165 break;
msarettac6c7502016-04-25 09:30:24 -0700166 default:
Leon Scroggins III588fb042017-07-14 16:32:31 -0400167 *result = kInvalidInput;
msarettac6c7502016-04-25 09:30:24 -0700168 return nullptr;
scroggo6f5e6192015-06-18 12:53:43 -0700169 }
scroggo6f5e6192015-06-18 12:53:43 -0700170
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500171
Leon Scroggins III588fb042017-07-14 16:32:31 -0400172 *result = kSuccess;
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400173 SkEncodedInfo info = SkEncodedInfo::Make(width, height, color, alpha, 8, std::move(profile));
174 return std::unique_ptr<SkCodec>(new SkWebpCodec(std::move(info), std::move(stream),
175 demux.release(), std::move(data), origin));
scroggo6f5e6192015-06-18 12:53:43 -0700176}
177
Leon Scroggins III03588412017-11-17 08:07:32 -0500178static WEBP_CSP_MODE webp_decode_mode(SkColorType dstCT, bool premultiply) {
179 switch (dstCT) {
scroggo6f5e6192015-06-18 12:53:43 -0700180 case kBGRA_8888_SkColorType:
181 return premultiply ? MODE_bgrA : MODE_BGRA;
182 case kRGBA_8888_SkColorType:
183 return premultiply ? MODE_rgbA : MODE_RGBA;
scroggo74992b52015-08-06 13:50:15 -0700184 case kRGB_565_SkColorType:
185 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700186 default:
187 return MODE_LAST;
188 }
189}
190
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400191SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
192 const int i = this->size();
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500193 fFrames.emplace_back(i, hasAlpha ? SkEncodedInfo::kUnpremul_Alpha
194 : SkEncodedInfo::kOpaque_Alpha);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400195 return &fFrames[i];
196}
197
scroggob636b452015-07-22 07:16:20 -0700198bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
199 if (!desiredSubset) {
200 return false;
201 }
202
Leon Scroggins III712476e2018-10-03 15:47:00 -0400203 if (!this->bounds().contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700204 return false;
205 }
206
207 // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
208 // decode this exact subset.
209 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
210 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
211 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
212 return true;
213}
214
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400215int SkWebpCodec::onGetRepetitionCount() {
216 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
217 if (!(flags & ANIMATION_FLAG)) {
218 return 0;
219 }
220
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500221 int loopCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
222 if (0 == loopCount) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400223 return kRepetitionCountInfinite;
224 }
225
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500226 loopCount--;
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500227 return loopCount;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400228}
229
230int SkWebpCodec::onGetFrameCount() {
231 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
232 if (!(flags & ANIMATION_FLAG)) {
233 return 1;
234 }
235
236 const uint32_t oldFrameCount = fFrameHolder.size();
237 if (fFailed) {
238 return oldFrameCount;
239 }
240
241 const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
242 if (oldFrameCount == frameCount) {
243 // We have already parsed this.
244 return frameCount;
245 }
246
247 fFrameHolder.reserve(frameCount);
248
249 for (uint32_t i = oldFrameCount; i < frameCount; i++) {
250 WebPIterator iter;
251 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
252
253 if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
254 fFailed = true;
255 break;
256 }
257
258 // libwebp only reports complete frames of an animated image.
259 SkASSERT(iter.complete);
260
261 Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha);
262 frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height);
263 frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ?
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400264 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
265 SkCodecAnimation::DisposalMethod::kKeep);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400266 frame->setDuration(iter.duration);
267 if (WEBP_MUX_BLEND != iter.blend_method) {
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500268 frame->setBlend(SkCodecAnimation::Blend::kSrc);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400269 }
270 fFrameHolder.setAlphaAndRequiredFrame(frame);
271 }
272
273 return fFrameHolder.size();
274
275}
276
277const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
278 return static_cast<const SkFrame*>(this->frame(i));
279}
280
281const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
282 SkASSERT(i >= 0 && i < this->size());
283 return &fFrames[i];
284}
285
286bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
287 if (i >= fFrameHolder.size()) {
288 return false;
289 }
290
291 const Frame* frame = fFrameHolder.frame(i);
292 if (!frame) {
293 return false;
294 }
295
296 if (frameInfo) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400297 // libwebp only reports fully received frames for an
298 // animated image.
Leon Scroggins III469d67e2020-11-11 12:45:40 -0500299 frame->fillIn(frameInfo, true);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400300 }
301
302 return true;
303}
304
305static bool is_8888(SkColorType colorType) {
306 switch (colorType) {
307 case kRGBA_8888_SkColorType:
308 case kBGRA_8888_SkColorType:
309 return true;
310 default:
311 return false;
312 }
313}
314
Leon Scroggins III03588412017-11-17 08:07:32 -0500315// Requires that the src input be unpremultiplied (or opaque).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400316static void blend_line(SkColorType dstCT, void* dst,
Mike Klein45c16fa2017-07-18 18:15:13 -0400317 SkColorType srcCT, const void* src,
Leon Scroggins III03588412017-11-17 08:07:32 -0500318 SkAlphaType dstAt,
319 bool srcHasAlpha,
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400320 int width) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400321 SkRasterPipeline_MemoryCtx dst_ctx = { (void*)dst, 0 },
322 src_ctx = { (void*)src, 0 };
Mike Klein45c16fa2017-07-18 18:15:13 -0400323
Mike Kleinb24704d2017-05-24 07:53:00 -0400324 SkRasterPipeline_<256> p;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400325
Mike Klein1a3eb522018-10-18 10:11:00 -0400326 p.append_load_dst(dstCT, &dst_ctx);
Leon Scroggins III03588412017-11-17 08:07:32 -0500327 if (kUnpremul_SkAlphaType == dstAt) {
Mike Klein1a3eb522018-10-18 10:11:00 -0400328 p.append(SkRasterPipeline::premul_dst);
Leon Scroggins III03588412017-11-17 08:07:32 -0500329 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400330
Mike Klein1a3eb522018-10-18 10:11:00 -0400331 p.append_load(srcCT, &src_ctx);
Leon Scroggins III03588412017-11-17 08:07:32 -0500332 if (srcHasAlpha) {
333 p.append(SkRasterPipeline::premul);
334 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400335
336 p.append(SkRasterPipeline::srcover);
337
Leon Scroggins III03588412017-11-17 08:07:32 -0500338 if (kUnpremul_SkAlphaType == dstAt) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400339 p.append(SkRasterPipeline::unpremul);
340 }
Mike Klein1a3eb522018-10-18 10:11:00 -0400341 p.append_store(dstCT, &dst_ctx);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400342
Mike Klein45c16fa2017-07-18 18:15:13 -0400343 p.run(0,0, width,1);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400344}
345
scroggoeb602a52015-07-09 08:16:03 -0700346SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000347 const Options& options, int* rowsDecodedPtr) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400348 const int index = options.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400349 SkASSERT(0 == index || index < fFrameHolder.size());
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500350 SkASSERT(0 == index || !options.fSubset);
scroggo6f5e6192015-06-18 12:53:43 -0700351
352 WebPDecoderConfig config;
353 if (0 == WebPInitDecoderConfig(&config)) {
354 // ABI mismatch.
355 // FIXME: New enum for this?
356 return kInvalidInput;
357 }
358
359 // Free any memory associated with the buffer. Must be called last, so we declare it first.
360 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
361
Matt Sarett5c496172017-02-07 17:01:16 -0500362 WebPIterator frame;
363 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400364 // If this succeeded in onGetFrameCount(), it should succeed again here.
365 SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
Matt Sarett604971e2017-02-06 09:51:48 -0500366
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400367 const bool independent = index == 0 ? true :
Nigel Tao66bc5242018-08-22 10:56:03 +1000368 (fFrameHolder.frame(index)->getRequiredFrame() == kNoFrame);
Matt Sarett5c496172017-02-07 17:01:16 -0500369 // Get the frameRect. libwebp will have already signaled an error if this is not fully
370 // contained by the canvas.
371 auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
Leon Scroggins III712476e2018-10-03 15:47:00 -0400372 SkASSERT(this->bounds().contains(frameRect));
373 const bool frameIsSubset = frameRect != this->bounds();
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400374 if (independent && frameIsSubset) {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400375 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
Matt Sarett604971e2017-02-06 09:51:48 -0500376 }
377
Matt Sarett5c496172017-02-07 17:01:16 -0500378 int dstX = frameRect.x();
379 int dstY = frameRect.y();
380 int subsetWidth = frameRect.width();
381 int subsetHeight = frameRect.height();
382 if (options.fSubset) {
383 SkIRect subset = *options.fSubset;
Leon Scroggins III712476e2018-10-03 15:47:00 -0400384 SkASSERT(this->bounds().contains(subset));
Matt Sarett5c496172017-02-07 17:01:16 -0500385 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
386 SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
387
Mike Reed3012cba2019-08-26 10:31:13 -0400388 if (!SkIRect::Intersects(subset, frameRect)) {
Matt Sarett5c496172017-02-07 17:01:16 -0500389 return kSuccess;
390 }
391
Brian Osman788b9162020-02-07 10:36:46 -0500392 int minXOffset = std::min(dstX, subset.x());
393 int minYOffset = std::min(dstY, subset.y());
Matt Sarett5c496172017-02-07 17:01:16 -0500394 dstX -= minXOffset;
395 dstY -= minYOffset;
396 frameRect.offset(-minXOffset, -minYOffset);
397 subset.offset(-minXOffset, -minYOffset);
398
399 // Just like we require that the requested subset x and y offset are even, libwebp
400 // guarantees that the frame x and y offset are even (it's actually impossible to specify
401 // an odd frame offset). So we can still guarantee that the adjusted offsets are even.
402 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
403
404 SkIRect intersection;
405 SkAssertResult(intersection.intersect(frameRect, subset));
406 subsetWidth = intersection.width();
407 subsetHeight = intersection.height();
408
409 config.options.use_cropping = 1;
410 config.options.crop_left = subset.x();
411 config.options.crop_top = subset.y();
412 config.options.crop_width = subsetWidth;
413 config.options.crop_height = subsetHeight;
414 }
415
416 // Ignore the frame size and offset when determining if scaling is necessary.
417 int scaledWidth = subsetWidth;
418 int scaledHeight = subsetHeight;
Leon Scroggins III712476e2018-10-03 15:47:00 -0400419 SkISize srcSize = options.fSubset ? options.fSubset->size() : this->dimensions();
Matt Sarett5c496172017-02-07 17:01:16 -0500420 if (srcSize != dstInfo.dimensions()) {
scroggo6f5e6192015-06-18 12:53:43 -0700421 config.options.use_scaling = 1;
Matt Sarett5c496172017-02-07 17:01:16 -0500422
423 if (frameIsSubset) {
424 float scaleX = ((float) dstInfo.width()) / srcSize.width();
425 float scaleY = ((float) dstInfo.height()) / srcSize.height();
426
427 // We need to be conservative here and floor rather than round.
428 // Otherwise, we may find ourselves decoding off the end of memory.
429 dstX = scaleX * dstX;
430 scaledWidth = scaleX * scaledWidth;
431 dstY = scaleY * dstY;
432 scaledHeight = scaleY * scaledHeight;
433 if (0 == scaledWidth || 0 == scaledHeight) {
434 return kSuccess;
435 }
436 } else {
437 scaledWidth = dstInfo.width();
438 scaledHeight = dstInfo.height();
439 }
440
441 config.options.scaled_width = scaledWidth;
442 config.options.scaled_height = scaledHeight;
scroggo6f5e6192015-06-18 12:53:43 -0700443 }
444
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400445 const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400446 && frame.has_alpha;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400447
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400448 auto webpInfo = dstInfo;
449 if (!frame.has_alpha) {
450 webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
Leon Scroggins23cbb992020-12-09 13:33:17 -0500451 } else if (this->colorXform() || blendWithPrevFrame) {
452 // the colorXform and blend_line expect unpremul.
453 webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400454 }
455 if (this->colorXform()) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400456 // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a
457 // color transform, we should decode to whatever is easiest for libwebp, and then let the
458 // color transform swizzle if necessary.
459 // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost). Lossless webp is
460 // 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 -0400461 webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400462 }
463
Leon Scroggins23cbb992020-12-09 13:33:17 -0500464 SkBitmap webpDst;
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400465 if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400466 // We will decode the entire image and then perform the color transform. libwebp
467 // does not provide a row-by-row API. This is a shame particularly when we do not want
468 // 8888, since we will need to create another image sized buffer.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400469 webpDst.allocPixels(webpInfo);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400470 } else {
471 // libwebp can decode directly into the output memory.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400472 webpDst.installPixels(webpInfo, dst, rowBytes);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400473 }
474
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400475 config.output.colorspace = webp_decode_mode(webpInfo.colorType(),
Leon Scroggins23cbb992020-12-09 13:33:17 -0500476 webpInfo.alphaType() == kPremul_SkAlphaType);
scroggo6f5e6192015-06-18 12:53:43 -0700477 config.output.is_external_memory = 1;
478
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400479 config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
480 config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
Mike Reedf0ffb892017-10-03 14:47:21 -0400481 config.output.u.RGBA.size = webpDst.computeByteSize();
msarettff2a6c82016-09-07 11:23:28 -0700482
halcanary96fcdcc2015-08-27 07:41:13 -0700483 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700484 if (!idec) {
485 return kInvalidInput;
486 }
487
Leon Scroggins IIIe5677462017-09-27 16:31:08 -0400488 int rowsDecoded = 0;
msarette99883f2016-09-08 06:05:35 -0700489 SkCodec::Result result;
msarettff2a6c82016-09-07 11:23:28 -0700490 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
491 case VP8_STATUS_OK:
Matt Sarett5c496172017-02-07 17:01:16 -0500492 rowsDecoded = scaledHeight;
msarette99883f2016-09-08 06:05:35 -0700493 result = kSuccess;
494 break;
msarettff2a6c82016-09-07 11:23:28 -0700495 case VP8_STATUS_SUSPENDED:
Leon Scroggins IIIe5677462017-09-27 16:31:08 -0400496 if (!WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr)
497 || rowsDecoded <= 0) {
498 return kInvalidInput;
499 }
Matt Sarett5c496172017-02-07 17:01:16 -0500500 *rowsDecodedPtr = rowsDecoded + dstY;
msarette99883f2016-09-08 06:05:35 -0700501 result = kIncompleteInput;
502 break;
msarettff2a6c82016-09-07 11:23:28 -0700503 default:
504 return kInvalidInput;
scroggo6f5e6192015-06-18 12:53:43 -0700505 }
msarette99883f2016-09-08 06:05:35 -0700506
Mike Reed7fcfb622018-02-09 13:26:46 -0500507 const size_t dstBpp = dstInfo.bytesPerPixel();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400508 dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
509 const size_t srcRowBytes = config.output.u.RGBA.stride;
510
511 const auto dstCT = dstInfo.colorType();
Matt Sarett313c4632016-10-20 12:35:23 -0400512 if (this->colorXform()) {
Matt Sarett5c496172017-02-07 17:01:16 -0500513 uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400514 SkBitmap tmp;
515 void* xformDst;
516
517 if (blendWithPrevFrame) {
518 // Xform into temporary bitmap big enough for one row.
519 tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
520 xformDst = tmp.getPixels();
521 } else {
522 xformDst = dst;
523 }
Leon Scroggins III03588412017-11-17 08:07:32 -0500524
Robert Phillipsb3050b92017-02-06 13:12:18 +0000525 for (int y = 0; y < rowsDecoded; y++) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400526 this->applyColorXform(xformDst, xformSrc, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400527 if (blendWithPrevFrame) {
Mike Klein76f57062018-06-08 14:00:19 -0400528 blend_line(dstCT, dst, dstCT, xformDst,
Leon Scroggins III03588412017-11-17 08:07:32 -0500529 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400530 dst = SkTAddOffset<void>(dst, rowBytes);
531 } else {
532 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
533 }
Matt Sarett5c496172017-02-07 17:01:16 -0500534 xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
msarette99883f2016-09-08 06:05:35 -0700535 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400536 } else if (blendWithPrevFrame) {
537 const uint8_t* src = config.output.u.RGBA.rgba;
538
539 for (int y = 0; y < rowsDecoded; y++) {
Mike Klein76f57062018-06-08 14:00:19 -0400540 blend_line(dstCT, dst, webpDst.colorType(), src,
Leon Scroggins III03588412017-11-17 08:07:32 -0500541 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400542 src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
543 dst = SkTAddOffset<void>(dst, rowBytes);
544 }
msarette99883f2016-09-08 06:05:35 -0700545 }
546
547 return result;
scroggo6f5e6192015-06-18 12:53:43 -0700548}
549
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400550SkWebpCodec::SkWebpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500551 WebPDemuxer* demux, sk_sp<SkData> data, SkEncodedOrigin origin)
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400552 : INHERITED(std::move(info), skcms_PixelFormat_BGRA_8888, std::move(stream),
553 origin)
msarettff2a6c82016-09-07 11:23:28 -0700554 , fDemux(demux)
555 , fData(std::move(data))
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400556 , fFailed(false)
557{
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400558 const auto& eInfo = this->getEncodedInfo();
559 fFrameHolder.setScreenSize(eInfo.width(), eInfo.height());
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400560}