scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/codec/SkWebpCodec.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #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 III | ba45830 | 2019-09-24 12:40:11 -0400 | [diff] [blame] | 17 | #include "src/codec/SkParseEncodedOrigin.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/codec/SkSampler.h" |
| 19 | #include "src/core/SkMakeUnique.h" |
| 20 | #include "src/core/SkRasterPipeline.h" |
| 21 | #include "src/core/SkStreamPriv.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 22 | |
| 23 | // A WebP decoder on top of (subset of) libwebp |
| 24 | // For more information on WebP image format, and libwebp library, see: |
| 25 | // https://code.google.com/speed/webp/ |
| 26 | // http://www.webmproject.org/code/#libwebp-webp-image-library |
| 27 | // https://chromium.googlesource.com/webm/libwebp |
| 28 | |
| 29 | // If moving libwebp out of skia source tree, path for webp headers must be |
| 30 | // updated accordingly. Here, we enforce using local copy in webp sub-directory. |
| 31 | #include "webp/decode.h" |
msarett | 9d15dab | 2016-08-24 07:36:06 -0700 | [diff] [blame] | 32 | #include "webp/demux.h" |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 33 | #include "webp/encode.h" |
| 34 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 35 | bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 36 | // WEBP starts with the following: |
| 37 | // RIFFXXXXWEBPVP |
| 38 | // Where XXXX is unspecified. |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 39 | const char* bytes = static_cast<const char*>(buf); |
| 40 | return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 41 | } |
| 42 | |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 43 | // Parse headers of RIFF container, and check for valid Webp (VP8) content. |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 44 | // Returns an SkWebpCodec on success |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 45 | std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 46 | Result* result) { |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 47 | // Webp demux needs a contiguous data buffer. |
| 48 | sk_sp<SkData> data = nullptr; |
| 49 | if (stream->getMemoryBase()) { |
| 50 | // It is safe to make without copy because we'll hold onto the stream. |
| 51 | data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength()); |
| 52 | } else { |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 53 | data = SkCopyStreamToData(stream.get()); |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 54 | |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 55 | // If we are forced to copy the stream to a data, we can go ahead and delete the stream. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 56 | stream.reset(nullptr); |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // It's a little strange that the |demux| will outlive |webpData|, though it needs the |
| 60 | // pointer in |webpData| to remain valid. This works because the pointer remains valid |
| 61 | // until the SkData is freed. |
| 62 | WebPData webpData = { data->bytes(), data->size() }; |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 63 | WebPDemuxState state; |
| 64 | SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state)); |
| 65 | switch (state) { |
| 66 | case WEBP_DEMUX_PARSE_ERROR: |
| 67 | *result = kInvalidInput; |
| 68 | return nullptr; |
| 69 | case WEBP_DEMUX_PARSING_HEADER: |
| 70 | *result = kIncompleteInput; |
| 71 | return nullptr; |
| 72 | case WEBP_DEMUX_PARSED_HEADER: |
| 73 | case WEBP_DEMUX_DONE: |
| 74 | SkASSERT(demux); |
| 75 | break; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 78 | const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH); |
| 79 | const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT); |
| 80 | |
| 81 | // Sanity check for image size that's about to be decoded. |
| 82 | { |
| 83 | const int64_t size = sk_64_mul(width, height); |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 84 | // now check that if we are 4-bytes per pixel, we also don't overflow |
Herb Derby | c402b77 | 2017-09-20 11:56:00 -0400 | [diff] [blame] | 85 | if (!SkTFitsIn<int32_t>(size) || SkTo<int32_t>(size) > (0x7FFFFFFF >> 2)) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 86 | *result = kInvalidInput; |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 87 | return nullptr; |
| 88 | } |
| 89 | } |
| 90 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 91 | std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr; |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 92 | { |
| 93 | WebPChunkIterator chunkIterator; |
| 94 | SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator); |
| 95 | if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 96 | // FIXME: I think this could be MakeWithoutCopy |
| 97 | auto chunk = SkData::MakeWithCopy(chunkIterator.chunk.bytes, chunkIterator.chunk.size); |
| 98 | profile = SkEncodedInfo::ICCProfile::Make(std::move(chunk)); |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 99 | } |
Leon Scroggins III | 5dd47e4 | 2018-09-27 15:26:48 -0400 | [diff] [blame] | 100 | if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) { |
| 101 | profile = nullptr; |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 102 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 103 | } |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 104 | |
| 105 | SkEncodedOrigin origin = kDefault_SkEncodedOrigin; |
| 106 | { |
| 107 | WebPChunkIterator chunkIterator; |
| 108 | SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator); |
| 109 | if (WebPDemuxGetChunk(demux, "EXIF", 1, &chunkIterator)) { |
Leon Scroggins III | ba45830 | 2019-09-24 12:40:11 -0400 | [diff] [blame] | 110 | SkParseEncodedOrigin(chunkIterator.chunk.bytes, chunkIterator.chunk.size, &origin); |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 111 | } |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 114 | // Get the first frame and its "features" to determine the color and alpha types. |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 115 | WebPIterator frame; |
| 116 | SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); |
| 117 | if (!WebPDemuxGetFrame(demux, 1, &frame)) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 118 | *result = kIncompleteInput; |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 119 | return nullptr; |
| 120 | } |
| 121 | |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 122 | WebPBitstreamFeatures features; |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 123 | switch (WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features)) { |
| 124 | case VP8_STATUS_OK: |
| 125 | break; |
| 126 | case VP8_STATUS_SUSPENDED: |
| 127 | case VP8_STATUS_NOT_ENOUGH_DATA: |
| 128 | *result = kIncompleteInput; |
| 129 | return nullptr; |
| 130 | default: |
| 131 | *result = kInvalidInput; |
| 132 | return nullptr; |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 135 | const bool hasAlpha = SkToBool(frame.has_alpha) |
| 136 | || frame.width != width || frame.height != height; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 137 | SkEncodedInfo::Color color; |
| 138 | SkEncodedInfo::Alpha alpha; |
| 139 | switch (features.format) { |
| 140 | case 0: |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 141 | // This indicates a "mixed" format. We could see this for |
| 142 | // animated webps (multiple fragments). |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 143 | // We could also guess kYUV here, but I think it makes more |
| 144 | // sense to guess kBGRA which is likely closer to the final |
| 145 | // output. Otherwise, we might end up converting |
| 146 | // BGRA->YUVA->BGRA. |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 147 | // Fallthrough: |
| 148 | case 2: |
| 149 | // This is the lossless format (BGRA). |
| 150 | if (hasAlpha) { |
| 151 | color = SkEncodedInfo::kBGRA_Color; |
| 152 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 153 | } else { |
| 154 | color = SkEncodedInfo::kBGRX_Color; |
| 155 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 156 | } |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 157 | break; |
| 158 | case 1: |
| 159 | // This is the lossy format (YUV). |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 160 | if (hasAlpha) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 161 | color = SkEncodedInfo::kYUVA_Color; |
msarett | c30c418 | 2016-04-20 11:53:35 -0700 | [diff] [blame] | 162 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 163 | } else { |
| 164 | color = SkEncodedInfo::kYUV_Color; |
| 165 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 166 | } |
| 167 | break; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 168 | default: |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 169 | *result = kInvalidInput; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 170 | return nullptr; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 171 | } |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 172 | |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 173 | |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 174 | *result = kSuccess; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 175 | SkEncodedInfo info = SkEncodedInfo::Make(width, height, color, alpha, 8, std::move(profile)); |
| 176 | return std::unique_ptr<SkCodec>(new SkWebpCodec(std::move(info), std::move(stream), |
| 177 | demux.release(), std::move(data), origin)); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 180 | static WEBP_CSP_MODE webp_decode_mode(SkColorType dstCT, bool premultiply) { |
| 181 | switch (dstCT) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 182 | case kBGRA_8888_SkColorType: |
| 183 | return premultiply ? MODE_bgrA : MODE_BGRA; |
| 184 | case kRGBA_8888_SkColorType: |
| 185 | return premultiply ? MODE_rgbA : MODE_RGBA; |
scroggo | 74992b5 | 2015-08-06 13:50:15 -0700 | [diff] [blame] | 186 | case kRGB_565_SkColorType: |
| 187 | return MODE_RGB_565; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 188 | default: |
| 189 | return MODE_LAST; |
| 190 | } |
| 191 | } |
| 192 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 193 | SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) { |
| 194 | const int i = this->size(); |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 195 | fFrames.emplace_back(i, hasAlpha ? SkEncodedInfo::kUnpremul_Alpha |
| 196 | : SkEncodedInfo::kOpaque_Alpha); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 197 | return &fFrames[i]; |
| 198 | } |
| 199 | |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 200 | bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const { |
| 201 | if (!desiredSubset) { |
| 202 | return false; |
| 203 | } |
| 204 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 205 | if (!this->bounds().contains(*desiredSubset)) { |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we |
| 210 | // decode this exact subset. |
| 211 | // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested. |
| 212 | desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1; |
| 213 | desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1; |
| 214 | return true; |
| 215 | } |
| 216 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 217 | int SkWebpCodec::onGetRepetitionCount() { |
| 218 | auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS); |
| 219 | if (!(flags & ANIMATION_FLAG)) { |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | const int repCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT); |
| 224 | if (0 == repCount) { |
| 225 | return kRepetitionCountInfinite; |
| 226 | } |
| 227 | |
| 228 | return repCount; |
| 229 | } |
| 230 | |
| 231 | int SkWebpCodec::onGetFrameCount() { |
| 232 | auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS); |
| 233 | if (!(flags & ANIMATION_FLAG)) { |
| 234 | return 1; |
| 235 | } |
| 236 | |
| 237 | const uint32_t oldFrameCount = fFrameHolder.size(); |
| 238 | if (fFailed) { |
| 239 | return oldFrameCount; |
| 240 | } |
| 241 | |
| 242 | const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT); |
| 243 | if (oldFrameCount == frameCount) { |
| 244 | // We have already parsed this. |
| 245 | return frameCount; |
| 246 | } |
| 247 | |
| 248 | fFrameHolder.reserve(frameCount); |
| 249 | |
| 250 | for (uint32_t i = oldFrameCount; i < frameCount; i++) { |
| 251 | WebPIterator iter; |
| 252 | SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter); |
| 253 | |
| 254 | if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) { |
| 255 | fFailed = true; |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | // libwebp only reports complete frames of an animated image. |
| 260 | SkASSERT(iter.complete); |
| 261 | |
| 262 | Frame* frame = fFrameHolder.appendNewFrame(iter.has_alpha); |
| 263 | frame->setXYWH(iter.x_offset, iter.y_offset, iter.width, iter.height); |
| 264 | frame->setDisposalMethod(iter.dispose_method == WEBP_MUX_DISPOSE_BACKGROUND ? |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 265 | SkCodecAnimation::DisposalMethod::kRestoreBGColor : |
| 266 | SkCodecAnimation::DisposalMethod::kKeep); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 267 | frame->setDuration(iter.duration); |
| 268 | if (WEBP_MUX_BLEND != iter.blend_method) { |
| 269 | frame->setBlend(SkCodecAnimation::Blend::kBG); |
| 270 | } |
| 271 | fFrameHolder.setAlphaAndRequiredFrame(frame); |
| 272 | } |
| 273 | |
| 274 | return fFrameHolder.size(); |
| 275 | |
| 276 | } |
| 277 | |
| 278 | const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const { |
| 279 | return static_cast<const SkFrame*>(this->frame(i)); |
| 280 | } |
| 281 | |
| 282 | const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const { |
| 283 | SkASSERT(i >= 0 && i < this->size()); |
| 284 | return &fFrames[i]; |
| 285 | } |
| 286 | |
| 287 | bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const { |
| 288 | if (i >= fFrameHolder.size()) { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | const Frame* frame = fFrameHolder.frame(i); |
| 293 | if (!frame) { |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | if (frameInfo) { |
| 298 | frameInfo->fRequiredFrame = frame->getRequiredFrame(); |
| 299 | frameInfo->fDuration = frame->getDuration(); |
| 300 | // libwebp only reports fully received frames for an |
| 301 | // animated image. |
| 302 | frameInfo->fFullyReceived = true; |
Leon Scroggins III | c8037dc | 2017-12-05 13:55:24 -0500 | [diff] [blame] | 303 | frameInfo->fAlphaType = frame->hasAlpha() ? kUnpremul_SkAlphaType |
| 304 | : kOpaque_SkAlphaType; |
Leon Scroggins III | 33deb7e | 2017-06-07 12:31:51 -0400 | [diff] [blame] | 305 | frameInfo->fDisposalMethod = frame->getDisposalMethod(); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
| 311 | static bool is_8888(SkColorType colorType) { |
| 312 | switch (colorType) { |
| 313 | case kRGBA_8888_SkColorType: |
| 314 | case kBGRA_8888_SkColorType: |
| 315 | return true; |
| 316 | default: |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 321 | // Requires that the src input be unpremultiplied (or opaque). |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 322 | static void blend_line(SkColorType dstCT, void* dst, |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 323 | SkColorType srcCT, const void* src, |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 324 | SkAlphaType dstAt, |
| 325 | bool srcHasAlpha, |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 326 | int width) { |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 327 | SkRasterPipeline_MemoryCtx dst_ctx = { (void*)dst, 0 }, |
| 328 | src_ctx = { (void*)src, 0 }; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 329 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 330 | SkRasterPipeline_<256> p; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 331 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 332 | p.append_load_dst(dstCT, &dst_ctx); |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 333 | if (kUnpremul_SkAlphaType == dstAt) { |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 334 | p.append(SkRasterPipeline::premul_dst); |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 335 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 336 | |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 337 | p.append_load(srcCT, &src_ctx); |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 338 | if (srcHasAlpha) { |
| 339 | p.append(SkRasterPipeline::premul); |
| 340 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 341 | |
| 342 | p.append(SkRasterPipeline::srcover); |
| 343 | |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 344 | if (kUnpremul_SkAlphaType == dstAt) { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 345 | p.append(SkRasterPipeline::unpremul); |
| 346 | } |
Mike Klein | 1a3eb52 | 2018-10-18 10:11:00 -0400 | [diff] [blame] | 347 | p.append_store(dstCT, &dst_ctx); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 348 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 349 | p.run(0,0, width,1); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 350 | } |
| 351 | |
scroggo | eb602a5 | 2015-07-09 08:16:03 -0700 | [diff] [blame] | 352 | SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 353 | const Options& options, int* rowsDecodedPtr) { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 354 | const int index = options.fFrameIndex; |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 355 | SkASSERT(0 == index || index < fFrameHolder.size()); |
Leon Scroggins III | 42ee284 | 2018-01-14 14:46:51 -0500 | [diff] [blame] | 356 | SkASSERT(0 == index || !options.fSubset); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 357 | |
| 358 | WebPDecoderConfig config; |
| 359 | if (0 == WebPInitDecoderConfig(&config)) { |
| 360 | // ABI mismatch. |
| 361 | // FIXME: New enum for this? |
| 362 | return kInvalidInput; |
| 363 | } |
| 364 | |
| 365 | // Free any memory associated with the buffer. Must be called last, so we declare it first. |
| 366 | SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output)); |
| 367 | |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 368 | WebPIterator frame; |
| 369 | SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 370 | // If this succeeded in onGetFrameCount(), it should succeed again here. |
| 371 | SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame)); |
Matt Sarett | 604971e | 2017-02-06 09:51:48 -0500 | [diff] [blame] | 372 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 373 | const bool independent = index == 0 ? true : |
Nigel Tao | 66bc524 | 2018-08-22 10:56:03 +1000 | [diff] [blame] | 374 | (fFrameHolder.frame(index)->getRequiredFrame() == kNoFrame); |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 375 | // Get the frameRect. libwebp will have already signaled an error if this is not fully |
| 376 | // contained by the canvas. |
| 377 | auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height); |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 378 | SkASSERT(this->bounds().contains(frameRect)); |
| 379 | const bool frameIsSubset = frameRect != this->bounds(); |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 380 | if (independent && frameIsSubset) { |
Leon Scroggins III | e643a9e | 2018-08-03 16:15:04 -0400 | [diff] [blame] | 381 | SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized); |
Matt Sarett | 604971e | 2017-02-06 09:51:48 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 384 | int dstX = frameRect.x(); |
| 385 | int dstY = frameRect.y(); |
| 386 | int subsetWidth = frameRect.width(); |
| 387 | int subsetHeight = frameRect.height(); |
| 388 | if (options.fSubset) { |
| 389 | SkIRect subset = *options.fSubset; |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 390 | SkASSERT(this->bounds().contains(subset)); |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 391 | SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop)); |
| 392 | SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset); |
| 393 | |
Mike Reed | 3012cba | 2019-08-26 10:31:13 -0400 | [diff] [blame] | 394 | if (!SkIRect::Intersects(subset, frameRect)) { |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 395 | return kSuccess; |
| 396 | } |
| 397 | |
| 398 | int minXOffset = SkTMin(dstX, subset.x()); |
| 399 | int minYOffset = SkTMin(dstY, subset.y()); |
| 400 | dstX -= minXOffset; |
| 401 | dstY -= minYOffset; |
| 402 | frameRect.offset(-minXOffset, -minYOffset); |
| 403 | subset.offset(-minXOffset, -minYOffset); |
| 404 | |
| 405 | // Just like we require that the requested subset x and y offset are even, libwebp |
| 406 | // guarantees that the frame x and y offset are even (it's actually impossible to specify |
| 407 | // an odd frame offset). So we can still guarantee that the adjusted offsets are even. |
| 408 | SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop)); |
| 409 | |
| 410 | SkIRect intersection; |
| 411 | SkAssertResult(intersection.intersect(frameRect, subset)); |
| 412 | subsetWidth = intersection.width(); |
| 413 | subsetHeight = intersection.height(); |
| 414 | |
| 415 | config.options.use_cropping = 1; |
| 416 | config.options.crop_left = subset.x(); |
| 417 | config.options.crop_top = subset.y(); |
| 418 | config.options.crop_width = subsetWidth; |
| 419 | config.options.crop_height = subsetHeight; |
| 420 | } |
| 421 | |
| 422 | // Ignore the frame size and offset when determining if scaling is necessary. |
| 423 | int scaledWidth = subsetWidth; |
| 424 | int scaledHeight = subsetHeight; |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 425 | SkISize srcSize = options.fSubset ? options.fSubset->size() : this->dimensions(); |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 426 | if (srcSize != dstInfo.dimensions()) { |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 427 | config.options.use_scaling = 1; |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 428 | |
| 429 | if (frameIsSubset) { |
| 430 | float scaleX = ((float) dstInfo.width()) / srcSize.width(); |
| 431 | float scaleY = ((float) dstInfo.height()) / srcSize.height(); |
| 432 | |
| 433 | // We need to be conservative here and floor rather than round. |
| 434 | // Otherwise, we may find ourselves decoding off the end of memory. |
| 435 | dstX = scaleX * dstX; |
| 436 | scaledWidth = scaleX * scaledWidth; |
| 437 | dstY = scaleY * dstY; |
| 438 | scaledHeight = scaleY * scaledHeight; |
| 439 | if (0 == scaledWidth || 0 == scaledHeight) { |
| 440 | return kSuccess; |
| 441 | } |
| 442 | } else { |
| 443 | scaledWidth = dstInfo.width(); |
| 444 | scaledHeight = dstInfo.height(); |
| 445 | } |
| 446 | |
| 447 | config.options.scaled_width = scaledWidth; |
| 448 | config.options.scaled_height = scaledHeight; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Leon Scroggins III | 1f6af6b | 2017-06-12 16:41:09 -0400 | [diff] [blame] | 451 | const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 452 | && frame.has_alpha; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 453 | |
| 454 | SkBitmap webpDst; |
Leon Scroggins III | ee92f13 | 2017-05-23 15:28:46 -0400 | [diff] [blame] | 455 | auto webpInfo = dstInfo; |
| 456 | if (!frame.has_alpha) { |
| 457 | webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType); |
| 458 | } |
| 459 | if (this->colorXform()) { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 460 | // Swizzling between RGBA and BGRA is zero cost in a color transform. So when we have a |
| 461 | // color transform, we should decode to whatever is easiest for libwebp, and then let the |
| 462 | // color transform swizzle if necessary. |
| 463 | // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost). Lossless webp is |
| 464 | // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA. |
Leon Scroggins III | ee92f13 | 2017-05-23 15:28:46 -0400 | [diff] [blame] | 465 | webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 466 | |
Leon Scroggins III | ee92f13 | 2017-05-23 15:28:46 -0400 | [diff] [blame] | 467 | if (webpInfo.alphaType() == kPremul_SkAlphaType) { |
| 468 | webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) { |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 473 | // We will decode the entire image and then perform the color transform. libwebp |
| 474 | // does not provide a row-by-row API. This is a shame particularly when we do not want |
| 475 | // 8888, since we will need to create another image sized buffer. |
Leon Scroggins III | ee92f13 | 2017-05-23 15:28:46 -0400 | [diff] [blame] | 476 | webpDst.allocPixels(webpInfo); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 477 | } else { |
| 478 | // libwebp can decode directly into the output memory. |
Leon Scroggins III | ee92f13 | 2017-05-23 15:28:46 -0400 | [diff] [blame] | 479 | webpDst.installPixels(webpInfo, dst, rowBytes); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 480 | } |
| 481 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 482 | config.output.colorspace = webp_decode_mode(webpInfo.colorType(), |
| 483 | frame.has_alpha && dstInfo.alphaType() == kPremul_SkAlphaType && !this->colorXform()); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 484 | config.output.is_external_memory = 1; |
| 485 | |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 486 | config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY)); |
| 487 | config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes()); |
Mike Reed | f0ffb89 | 2017-10-03 14:47:21 -0400 | [diff] [blame] | 488 | config.output.u.RGBA.size = webpDst.computeByteSize(); |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 489 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 490 | SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config)); |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 491 | if (!idec) { |
| 492 | return kInvalidInput; |
| 493 | } |
| 494 | |
Leon Scroggins III | e567746 | 2017-09-27 16:31:08 -0400 | [diff] [blame] | 495 | int rowsDecoded = 0; |
msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 496 | SkCodec::Result result; |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 497 | switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) { |
| 498 | case VP8_STATUS_OK: |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 499 | rowsDecoded = scaledHeight; |
msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 500 | result = kSuccess; |
| 501 | break; |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 502 | case VP8_STATUS_SUSPENDED: |
Leon Scroggins III | e567746 | 2017-09-27 16:31:08 -0400 | [diff] [blame] | 503 | if (!WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr) |
| 504 | || rowsDecoded <= 0) { |
| 505 | return kInvalidInput; |
| 506 | } |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 507 | *rowsDecodedPtr = rowsDecoded + dstY; |
msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 508 | result = kIncompleteInput; |
| 509 | break; |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 510 | default: |
| 511 | return kInvalidInput; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 512 | } |
msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 513 | |
Mike Reed | 7fcfb62 | 2018-02-09 13:26:46 -0500 | [diff] [blame] | 514 | const size_t dstBpp = dstInfo.bytesPerPixel(); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 515 | dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY); |
| 516 | const size_t srcRowBytes = config.output.u.RGBA.stride; |
| 517 | |
| 518 | const auto dstCT = dstInfo.colorType(); |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 519 | if (this->colorXform()) { |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 520 | uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba; |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 521 | SkBitmap tmp; |
| 522 | void* xformDst; |
| 523 | |
| 524 | if (blendWithPrevFrame) { |
| 525 | // Xform into temporary bitmap big enough for one row. |
| 526 | tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1)); |
| 527 | xformDst = tmp.getPixels(); |
| 528 | } else { |
| 529 | xformDst = dst; |
| 530 | } |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 531 | |
Robert Phillips | b3050b9 | 2017-02-06 13:12:18 +0000 | [diff] [blame] | 532 | for (int y = 0; y < rowsDecoded; y++) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 533 | this->applyColorXform(xformDst, xformSrc, scaledWidth); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 534 | if (blendWithPrevFrame) { |
Mike Klein | 76f5706 | 2018-06-08 14:00:19 -0400 | [diff] [blame] | 535 | blend_line(dstCT, dst, dstCT, xformDst, |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 536 | dstInfo.alphaType(), frame.has_alpha, scaledWidth); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 537 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 538 | } else { |
| 539 | xformDst = SkTAddOffset<void>(xformDst, rowBytes); |
| 540 | } |
Matt Sarett | 5c49617 | 2017-02-07 17:01:16 -0500 | [diff] [blame] | 541 | xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes); |
msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 542 | } |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 543 | } else if (blendWithPrevFrame) { |
| 544 | const uint8_t* src = config.output.u.RGBA.rgba; |
| 545 | |
| 546 | for (int y = 0; y < rowsDecoded; y++) { |
Mike Klein | 76f5706 | 2018-06-08 14:00:19 -0400 | [diff] [blame] | 547 | blend_line(dstCT, dst, webpDst.colorType(), src, |
Leon Scroggins III | 0358841 | 2017-11-17 08:07:32 -0500 | [diff] [blame] | 548 | dstInfo.alphaType(), frame.has_alpha, scaledWidth); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 549 | src = SkTAddOffset<const uint8_t>(src, srcRowBytes); |
| 550 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 551 | } |
msarett | e99883f | 2016-09-08 06:05:35 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | return result; |
scroggo | 6f5e619 | 2015-06-18 12:53:43 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 557 | SkWebpCodec::SkWebpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream, |
Leon Scroggins III | da3e9ad | 2018-01-26 15:48:26 -0500 | [diff] [blame] | 558 | WebPDemuxer* demux, sk_sp<SkData> data, SkEncodedOrigin origin) |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 559 | : INHERITED(std::move(info), skcms_PixelFormat_BGRA_8888, std::move(stream), |
| 560 | origin) |
msarett | ff2a6c8 | 2016-09-07 11:23:28 -0700 | [diff] [blame] | 561 | , fDemux(demux) |
| 562 | , fData(std::move(data)) |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 563 | , fFailed(false) |
| 564 | { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 565 | const auto& eInfo = this->getEncodedInfo(); |
| 566 | fFrameHolder.setScreenSize(eInfo.width(), eInfo.height()); |
Leon Scroggins III | 557fbbe | 2017-05-23 09:37:21 -0400 | [diff] [blame] | 567 | } |