blob: 8a889b58330b5d8a305eaf73a75e9e22a96ebade [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"
15#include "src/codec/SkCodecAnimationPriv.h"
16#include "src/codec/SkCodecPriv.h"
Leon Scroggins IIIba458302019-09-24 12:40:11 -040017#include "src/codec/SkParseEncodedOrigin.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/codec/SkSampler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/core/SkRasterPipeline.h"
20#include "src/core/SkStreamPriv.h"
scroggo6f5e6192015-06-18 12:53:43 -070021
22// A WebP decoder on top of (subset of) libwebp
23// For more information on WebP image format, and libwebp library, see:
24// https://code.google.com/speed/webp/
25// http://www.webmproject.org/code/#libwebp-webp-image-library
26// https://chromium.googlesource.com/webm/libwebp
27
28// If moving libwebp out of skia source tree, path for webp headers must be
29// updated accordingly. Here, we enforce using local copy in webp sub-directory.
30#include "webp/decode.h"
msarett9d15dab2016-08-24 07:36:06 -070031#include "webp/demux.h"
scroggo6f5e6192015-06-18 12:53:43 -070032#include "webp/encode.h"
33
scroggodb30be22015-12-08 18:54:13 -080034bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
scroggo6f5e6192015-06-18 12:53:43 -070035 // WEBP starts with the following:
36 // RIFFXXXXWEBPVP
37 // Where XXXX is unspecified.
scroggodb30be22015-12-08 18:54:13 -080038 const char* bytes = static_cast<const char*>(buf);
39 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
scroggo6f5e6192015-06-18 12:53:43 -070040}
41
scroggo6f5e6192015-06-18 12:53:43 -070042// Parse headers of RIFF container, and check for valid Webp (VP8) content.
Leon Scroggins III557fbbe2017-05-23 09:37:21 -040043// Returns an SkWebpCodec on success
Mike Reedede7bac2017-07-23 15:30:02 -040044std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
45 Result* result) {
msarettff2a6c82016-09-07 11:23:28 -070046 // Webp demux needs a contiguous data buffer.
47 sk_sp<SkData> data = nullptr;
48 if (stream->getMemoryBase()) {
49 // It is safe to make without copy because we'll hold onto the stream.
50 data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
51 } else {
Mike Reedede7bac2017-07-23 15:30:02 -040052 data = SkCopyStreamToData(stream.get());
scroggodb30be22015-12-08 18:54:13 -080053
msarettff2a6c82016-09-07 11:23:28 -070054 // 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 -040055 stream.reset(nullptr);
msarettff2a6c82016-09-07 11:23:28 -070056 }
57
58 // It's a little strange that the |demux| will outlive |webpData|, though it needs the
59 // pointer in |webpData| to remain valid. This works because the pointer remains valid
60 // until the SkData is freed.
61 WebPData webpData = { data->bytes(), data->size() };
Leon Scroggins III588fb042017-07-14 16:32:31 -040062 WebPDemuxState state;
63 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state));
64 switch (state) {
65 case WEBP_DEMUX_PARSE_ERROR:
66 *result = kInvalidInput;
67 return nullptr;
68 case WEBP_DEMUX_PARSING_HEADER:
69 *result = kIncompleteInput;
70 return nullptr;
71 case WEBP_DEMUX_PARSED_HEADER:
72 case WEBP_DEMUX_DONE:
73 SkASSERT(demux);
74 break;
scroggo6f5e6192015-06-18 12:53:43 -070075 }
76
Matt Sarett5c496172017-02-07 17:01:16 -050077 const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
78 const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
79
80 // Sanity check for image size that's about to be decoded.
81 {
82 const int64_t size = sk_64_mul(width, height);
Matt Sarett5c496172017-02-07 17:01:16 -050083 // now check that if we are 4-bytes per pixel, we also don't overflow
Herb Derbyc402b772017-09-20 11:56:00 -040084 if (!SkTFitsIn<int32_t>(size) || SkTo<int32_t>(size) > (0x7FFFFFFF >> 2)) {
Leon Scroggins III588fb042017-07-14 16:32:31 -040085 *result = kInvalidInput;
Matt Sarett5c496172017-02-07 17:01:16 -050086 return nullptr;
87 }
88 }
89
Leon Scroggins III36f7e322018-08-27 11:55:46 -040090 std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050091 {
92 WebPChunkIterator chunkIterator;
93 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
94 if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -040095 // FIXME: I think this could be MakeWithoutCopy
96 auto chunk = SkData::MakeWithCopy(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
97 profile = SkEncodedInfo::ICCProfile::Make(std::move(chunk));
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -050098 }
Leon Scroggins III5dd47e42018-09-27 15:26:48 -040099 if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
100 profile = nullptr;
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500101 }
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)) {
Leon Scroggins IIIba458302019-09-24 12:40:11 -0400109 SkParseEncodedOrigin(chunkIterator.chunk.bytes, chunkIterator.chunk.size, &origin);
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500110 }
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;
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400174 SkEncodedInfo info = SkEncodedInfo::Make(width, height, color, alpha, 8, std::move(profile));
175 return std::unique_ptr<SkCodec>(new SkWebpCodec(std::move(info), std::move(stream),
176 demux.release(), std::move(data), origin));
scroggo6f5e6192015-06-18 12:53:43 -0700177}
178
Leon Scroggins III03588412017-11-17 08:07:32 -0500179static WEBP_CSP_MODE webp_decode_mode(SkColorType dstCT, bool premultiply) {
180 switch (dstCT) {
scroggo6f5e6192015-06-18 12:53:43 -0700181 case kBGRA_8888_SkColorType:
182 return premultiply ? MODE_bgrA : MODE_BGRA;
183 case kRGBA_8888_SkColorType:
184 return premultiply ? MODE_rgbA : MODE_RGBA;
scroggo74992b52015-08-06 13:50:15 -0700185 case kRGB_565_SkColorType:
186 return MODE_RGB_565;
scroggo6f5e6192015-06-18 12:53:43 -0700187 default:
188 return MODE_LAST;
189 }
190}
191
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400192SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
193 const int i = this->size();
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500194 fFrames.emplace_back(i, hasAlpha ? SkEncodedInfo::kUnpremul_Alpha
195 : SkEncodedInfo::kOpaque_Alpha);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400196 return &fFrames[i];
197}
198
scroggob636b452015-07-22 07:16:20 -0700199bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
200 if (!desiredSubset) {
201 return false;
202 }
203
Leon Scroggins III712476e2018-10-03 15:47:00 -0400204 if (!this->bounds().contains(*desiredSubset)) {
scroggob636b452015-07-22 07:16:20 -0700205 return false;
206 }
207
208 // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
209 // decode this exact subset.
210 // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
211 desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
212 desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
213 return true;
214}
215
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400216int SkWebpCodec::onGetRepetitionCount() {
217 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
218 if (!(flags & ANIMATION_FLAG)) {
219 return 0;
220 }
221
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500222 int loopCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
223 if (0 == loopCount) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400224 return kRepetitionCountInfinite;
225 }
226
Leon Scroggins IIIae834f572019-12-10 14:24:18 -0500227#ifndef SK_LEGACY_WEBP_LOOP_COUNT
228 loopCount--;
229#endif
230 return loopCount;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400231}
232
233int SkWebpCodec::onGetFrameCount() {
234 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
235 if (!(flags & ANIMATION_FLAG)) {
236 return 1;
237 }
238
239 const uint32_t oldFrameCount = fFrameHolder.size();
240 if (fFailed) {
241 return oldFrameCount;
242 }
243
244 const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
245 if (oldFrameCount == frameCount) {
246 // We have already parsed this.
247 return frameCount;
248 }
249
250 fFrameHolder.reserve(frameCount);
251
252 for (uint32_t i = oldFrameCount; i < frameCount; i++) {
253 WebPIterator iter;
254 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
255
256 if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
257 fFailed = true;
258 break;
259 }
260
261 // libwebp only reports complete frames of an animated image.
262 SkASSERT(iter.complete);
263
264 Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha);
265 frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height);
266 frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ?
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400267 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
268 SkCodecAnimation::DisposalMethod::kKeep);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400269 frame->setDuration(iter.duration);
270 if (WEBP_MUX_BLEND != iter.blend_method) {
271 frame->setBlend(SkCodecAnimation::Blend::kBG);
272 }
273 fFrameHolder.setAlphaAndRequiredFrame(frame);
274 }
275
276 return fFrameHolder.size();
277
278}
279
280const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
281 return static_cast<const SkFrame*>(this->frame(i));
282}
283
284const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
285 SkASSERT(i >= 0 && i < this->size());
286 return &fFrames[i];
287}
288
289bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
290 if (i >= fFrameHolder.size()) {
291 return false;
292 }
293
294 const Frame* frame = fFrameHolder.frame(i);
295 if (!frame) {
296 return false;
297 }
298
299 if (frameInfo) {
300 frameInfo->fRequiredFrame = frame->getRequiredFrame();
301 frameInfo->fDuration = frame->getDuration();
302 // libwebp only reports fully received frames for an
303 // animated image.
304 frameInfo->fFullyReceived = true;
Leon Scroggins IIIc8037dc2017-12-05 13:55:24 -0500305 frameInfo->fAlphaType = frame->hasAlpha() ? kUnpremul_SkAlphaType
306 : kOpaque_SkAlphaType;
Leon Scroggins III33deb7e2017-06-07 12:31:51 -0400307 frameInfo->fDisposalMethod = frame->getDisposalMethod();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400308 }
309
310 return true;
311}
312
313static bool is_8888(SkColorType colorType) {
314 switch (colorType) {
315 case kRGBA_8888_SkColorType:
316 case kBGRA_8888_SkColorType:
317 return true;
318 default:
319 return false;
320 }
321}
322
Leon Scroggins III03588412017-11-17 08:07:32 -0500323// Requires that the src input be unpremultiplied (or opaque).
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400324static void blend_line(SkColorType dstCT, void* dst,
Mike Klein45c16fa2017-07-18 18:15:13 -0400325 SkColorType srcCT, const void* src,
Leon Scroggins III03588412017-11-17 08:07:32 -0500326 SkAlphaType dstAt,
327 bool srcHasAlpha,
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400328 int width) {
Mike Kleinb11ab572018-10-24 06:42:14 -0400329 SkRasterPipeline_MemoryCtx dst_ctx = { (void*)dst, 0 },
330 src_ctx = { (void*)src, 0 };
Mike Klein45c16fa2017-07-18 18:15:13 -0400331
Mike Kleinb24704d2017-05-24 07:53:00 -0400332 SkRasterPipeline_<256> p;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400333
Mike Klein1a3eb522018-10-18 10:11:00 -0400334 p.append_load_dst(dstCT, &dst_ctx);
Leon Scroggins III03588412017-11-17 08:07:32 -0500335 if (kUnpremul_SkAlphaType == dstAt) {
Mike Klein1a3eb522018-10-18 10:11:00 -0400336 p.append(SkRasterPipeline::premul_dst);
Leon Scroggins III03588412017-11-17 08:07:32 -0500337 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400338
Mike Klein1a3eb522018-10-18 10:11:00 -0400339 p.append_load(srcCT, &src_ctx);
Leon Scroggins III03588412017-11-17 08:07:32 -0500340 if (srcHasAlpha) {
341 p.append(SkRasterPipeline::premul);
342 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400343
344 p.append(SkRasterPipeline::srcover);
345
Leon Scroggins III03588412017-11-17 08:07:32 -0500346 if (kUnpremul_SkAlphaType == dstAt) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400347 p.append(SkRasterPipeline::unpremul);
348 }
Mike Klein1a3eb522018-10-18 10:11:00 -0400349 p.append_store(dstCT, &dst_ctx);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400350
Mike Klein45c16fa2017-07-18 18:15:13 -0400351 p.run(0,0, width,1);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400352}
353
scroggoeb602a52015-07-09 08:16:03 -0700354SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
Leon Scroggins571b30f2017-07-11 17:35:31 +0000355 const Options& options, int* rowsDecodedPtr) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400356 const int index = options.fFrameIndex;
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400357 SkASSERT(0 == index || index < fFrameHolder.size());
Leon Scroggins III42ee2842018-01-14 14:46:51 -0500358 SkASSERT(0 == index || !options.fSubset);
scroggo6f5e6192015-06-18 12:53:43 -0700359
360 WebPDecoderConfig config;
361 if (0 == WebPInitDecoderConfig(&config)) {
362 // ABI mismatch.
363 // FIXME: New enum for this?
364 return kInvalidInput;
365 }
366
367 // Free any memory associated with the buffer. Must be called last, so we declare it first.
368 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
369
Matt Sarett5c496172017-02-07 17:01:16 -0500370 WebPIterator frame;
371 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400372 // If this succeeded in onGetFrameCount(), it should succeed again here.
373 SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
Matt Sarett604971e2017-02-06 09:51:48 -0500374
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400375 const bool independent = index == 0 ? true :
Nigel Tao66bc5242018-08-22 10:56:03 +1000376 (fFrameHolder.frame(index)->getRequiredFrame() == kNoFrame);
Matt Sarett5c496172017-02-07 17:01:16 -0500377 // Get the frameRect. libwebp will have already signaled an error if this is not fully
378 // contained by the canvas.
379 auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
Leon Scroggins III712476e2018-10-03 15:47:00 -0400380 SkASSERT(this->bounds().contains(frameRect));
381 const bool frameIsSubset = frameRect != this->bounds();
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400382 if (independent && frameIsSubset) {
Leon Scroggins IIIe643a9e2018-08-03 16:15:04 -0400383 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
Matt Sarett604971e2017-02-06 09:51:48 -0500384 }
385
Matt Sarett5c496172017-02-07 17:01:16 -0500386 int dstX = frameRect.x();
387 int dstY = frameRect.y();
388 int subsetWidth = frameRect.width();
389 int subsetHeight = frameRect.height();
390 if (options.fSubset) {
391 SkIRect subset = *options.fSubset;
Leon Scroggins III712476e2018-10-03 15:47:00 -0400392 SkASSERT(this->bounds().contains(subset));
Matt Sarett5c496172017-02-07 17:01:16 -0500393 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
394 SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
395
Mike Reed3012cba2019-08-26 10:31:13 -0400396 if (!SkIRect::Intersects(subset, frameRect)) {
Matt Sarett5c496172017-02-07 17:01:16 -0500397 return kSuccess;
398 }
399
400 int minXOffset = SkTMin(dstX, subset.x());
401 int minYOffset = SkTMin(dstY, subset.y());
402 dstX -= minXOffset;
403 dstY -= minYOffset;
404 frameRect.offset(-minXOffset, -minYOffset);
405 subset.offset(-minXOffset, -minYOffset);
406
407 // Just like we require that the requested subset x and y offset are even, libwebp
408 // guarantees that the frame x and y offset are even (it's actually impossible to specify
409 // an odd frame offset). So we can still guarantee that the adjusted offsets are even.
410 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
411
412 SkIRect intersection;
413 SkAssertResult(intersection.intersect(frameRect, subset));
414 subsetWidth = intersection.width();
415 subsetHeight = intersection.height();
416
417 config.options.use_cropping = 1;
418 config.options.crop_left = subset.x();
419 config.options.crop_top = subset.y();
420 config.options.crop_width = subsetWidth;
421 config.options.crop_height = subsetHeight;
422 }
423
424 // Ignore the frame size and offset when determining if scaling is necessary.
425 int scaledWidth = subsetWidth;
426 int scaledHeight = subsetHeight;
Leon Scroggins III712476e2018-10-03 15:47:00 -0400427 SkISize srcSize = options.fSubset ? options.fSubset->size() : this->dimensions();
Matt Sarett5c496172017-02-07 17:01:16 -0500428 if (srcSize != dstInfo.dimensions()) {
scroggo6f5e6192015-06-18 12:53:43 -0700429 config.options.use_scaling = 1;
Matt Sarett5c496172017-02-07 17:01:16 -0500430
431 if (frameIsSubset) {
432 float scaleX = ((float) dstInfo.width()) / srcSize.width();
433 float scaleY = ((float) dstInfo.height()) / srcSize.height();
434
435 // We need to be conservative here and floor rather than round.
436 // Otherwise, we may find ourselves decoding off the end of memory.
437 dstX = scaleX * dstX;
438 scaledWidth = scaleX * scaledWidth;
439 dstY = scaleY * dstY;
440 scaledHeight = scaleY * scaledHeight;
441 if (0 == scaledWidth || 0 == scaledHeight) {
442 return kSuccess;
443 }
444 } else {
445 scaledWidth = dstInfo.width();
446 scaledHeight = dstInfo.height();
447 }
448
449 config.options.scaled_width = scaledWidth;
450 config.options.scaled_height = scaledHeight;
scroggo6f5e6192015-06-18 12:53:43 -0700451 }
452
Leon Scroggins III1f6af6b2017-06-12 16:41:09 -0400453 const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400454 && frame.has_alpha;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400455
456 SkBitmap webpDst;
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400457 auto webpInfo = dstInfo;
458 if (!frame.has_alpha) {
459 webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
460 }
461 if (this->colorXform()) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400462 // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a
463 // color transform, we should decode to whatever is easiest for libwebp, and then let the
464 // color transform swizzle if necessary.
465 // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost). Lossless webp is
466 // 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 -0400467 webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400468
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400469 if (webpInfo.alphaType() == kPremul_SkAlphaType) {
470 webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
471 }
472 }
473
474 if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400475 // We will decode the entire image and then perform the color transform. libwebp
476 // does not provide a row-by-row API. This is a shame particularly when we do not want
477 // 8888, since we will need to create another image sized buffer.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400478 webpDst.allocPixels(webpInfo);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400479 } else {
480 // libwebp can decode directly into the output memory.
Leon Scroggins IIIee92f132017-05-23 15:28:46 -0400481 webpDst.installPixels(webpInfo, dst, rowBytes);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400482 }
483
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400484 config.output.colorspace = webp_decode_mode(webpInfo.colorType(),
485 frame.has_alpha && dstInfo.alphaType() == kPremul_SkAlphaType && !this->colorXform());
scroggo6f5e6192015-06-18 12:53:43 -0700486 config.output.is_external_memory = 1;
487
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400488 config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
489 config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
Mike Reedf0ffb892017-10-03 14:47:21 -0400490 config.output.u.RGBA.size = webpDst.computeByteSize();
msarettff2a6c82016-09-07 11:23:28 -0700491
halcanary96fcdcc2015-08-27 07:41:13 -0700492 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
scroggo6f5e6192015-06-18 12:53:43 -0700493 if (!idec) {
494 return kInvalidInput;
495 }
496
Leon Scroggins IIIe5677462017-09-27 16:31:08 -0400497 int rowsDecoded = 0;
msarette99883f2016-09-08 06:05:35 -0700498 SkCodec::Result result;
msarettff2a6c82016-09-07 11:23:28 -0700499 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
500 case VP8_STATUS_OK:
Matt Sarett5c496172017-02-07 17:01:16 -0500501 rowsDecoded = scaledHeight;
msarette99883f2016-09-08 06:05:35 -0700502 result = kSuccess;
503 break;
msarettff2a6c82016-09-07 11:23:28 -0700504 case VP8_STATUS_SUSPENDED:
Leon Scroggins IIIe5677462017-09-27 16:31:08 -0400505 if (!WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr)
506 || rowsDecoded <= 0) {
507 return kInvalidInput;
508 }
Matt Sarett5c496172017-02-07 17:01:16 -0500509 *rowsDecodedPtr = rowsDecoded + dstY;
msarette99883f2016-09-08 06:05:35 -0700510 result = kIncompleteInput;
511 break;
msarettff2a6c82016-09-07 11:23:28 -0700512 default:
513 return kInvalidInput;
scroggo6f5e6192015-06-18 12:53:43 -0700514 }
msarette99883f2016-09-08 06:05:35 -0700515
Mike Reed7fcfb622018-02-09 13:26:46 -0500516 const size_t dstBpp = dstInfo.bytesPerPixel();
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400517 dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
518 const size_t srcRowBytes = config.output.u.RGBA.stride;
519
520 const auto dstCT = dstInfo.colorType();
Matt Sarett313c4632016-10-20 12:35:23 -0400521 if (this->colorXform()) {
Matt Sarett5c496172017-02-07 17:01:16 -0500522 uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400523 SkBitmap tmp;
524 void* xformDst;
525
526 if (blendWithPrevFrame) {
527 // Xform into temporary bitmap big enough for one row.
528 tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
529 xformDst = tmp.getPixels();
530 } else {
531 xformDst = dst;
532 }
Leon Scroggins III03588412017-11-17 08:07:32 -0500533
Robert Phillipsb3050b92017-02-06 13:12:18 +0000534 for (int y = 0; y < rowsDecoded; y++) {
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400535 this->applyColorXform(xformDst, xformSrc, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400536 if (blendWithPrevFrame) {
Mike Klein76f57062018-06-08 14:00:19 -0400537 blend_line(dstCT, dst, dstCT, xformDst,
Leon Scroggins III03588412017-11-17 08:07:32 -0500538 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400539 dst = SkTAddOffset<void>(dst, rowBytes);
540 } else {
541 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
542 }
Matt Sarett5c496172017-02-07 17:01:16 -0500543 xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
msarette99883f2016-09-08 06:05:35 -0700544 }
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400545 } else if (blendWithPrevFrame) {
546 const uint8_t* src = config.output.u.RGBA.rgba;
547
548 for (int y = 0; y < rowsDecoded; y++) {
Mike Klein76f57062018-06-08 14:00:19 -0400549 blend_line(dstCT, dst, webpDst.colorType(), src,
Leon Scroggins III03588412017-11-17 08:07:32 -0500550 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400551 src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
552 dst = SkTAddOffset<void>(dst, rowBytes);
553 }
msarette99883f2016-09-08 06:05:35 -0700554 }
555
556 return result;
scroggo6f5e6192015-06-18 12:53:43 -0700557}
558
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400559SkWebpCodec::SkWebpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
Leon Scroggins IIIda3e9ad2018-01-26 15:48:26 -0500560 WebPDemuxer* demux, sk_sp<SkData> data, SkEncodedOrigin origin)
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400561 : INHERITED(std::move(info), skcms_PixelFormat_BGRA_8888, std::move(stream),
562 origin)
msarettff2a6c82016-09-07 11:23:28 -0700563 , fDemux(demux)
564 , fData(std::move(data))
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400565 , fFailed(false)
566{
Leon Scroggins III36f7e322018-08-27 11:55:46 -0400567 const auto& eInfo = this->getEncodedInfo();
568 fFrameHolder.setScreenSize(eInfo.width(), eInfo.height());
Leon Scroggins III557fbbe2017-05-23 09:37:21 -0400569}