2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "src/codec/SkWebpCodec.h"
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/SkCodecPriv.h"
16 #include "src/codec/SkParseEncodedOrigin.h"
17 #include "src/codec/SkSampler.h"
18 #include "src/core/SkRasterPipeline.h"
19 #include "src/core/SkStreamPriv.h"
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
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"
30 #include "webp/demux.h"
31 #include "webp/encode.h"
33 bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
34 // WEBP starts with the following:
36 // Where XXXX is unspecified.
37 const char* bytes = static_cast<const char*>(buf);
38 return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
41 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
42 // Returns an SkWebpCodec on success
43 std::unique_ptr<SkCodec> SkWebpCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
45 // 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());
51 data = SkCopyStreamToData(stream.get());
53 // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
54 stream.reset(nullptr);
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() };
62 SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, &state));
64 case WEBP_DEMUX_PARSE_ERROR:
65 *result = kInvalidInput;
67 case WEBP_DEMUX_PARSING_HEADER:
68 *result = kIncompleteInput;
70 case WEBP_DEMUX_PARSED_HEADER:
76 const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
77 const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
79 // Validate the image size that's about to be decoded.
81 const int64_t size = sk_64_mul(width, height);
82 // now check that if we are 4-bytes per pixel, we also don't overflow
83 if (!SkTFitsIn<int32_t>(size) || SkTo<int32_t>(size) > (0x7FFFFFFF >> 2)) {
84 *result = kInvalidInput;
89 std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
91 WebPChunkIterator chunkIterator;
92 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
93 if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
94 // 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));
98 if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
103 SkEncodedOrigin origin = kDefault_SkEncodedOrigin;
105 WebPChunkIterator chunkIterator;
106 SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
107 if (WebPDemuxGetChunk(demux, "EXIF", 1, &chunkIterator)) {
108 SkParseEncodedOrigin(chunkIterator.chunk.bytes, chunkIterator.chunk.size, &origin);
112 // Get the first frame and its "features" to determine the color and alpha types.
114 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
115 if (!WebPDemuxGetFrame(demux, 1, &frame)) {
116 *result = kIncompleteInput;
120 WebPBitstreamFeatures features;
121 switch (WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features)) {
124 case VP8_STATUS_SUSPENDED:
125 case VP8_STATUS_NOT_ENOUGH_DATA:
126 *result = kIncompleteInput;
129 *result = kInvalidInput;
133 const bool hasAlpha = SkToBool(frame.has_alpha)
134 || frame.width != width || frame.height != height;
135 SkEncodedInfo::Color color;
136 SkEncodedInfo::Alpha alpha;
137 switch (features.format) {
139 // This indicates a "mixed" format. We could see this for
140 // animated webps (multiple fragments).
141 // 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
147 // This is the lossless format (BGRA).
149 color = SkEncodedInfo::kBGRA_Color;
150 alpha = SkEncodedInfo::kUnpremul_Alpha;
152 color = SkEncodedInfo::kBGRX_Color;
153 alpha = SkEncodedInfo::kOpaque_Alpha;
157 // This is the lossy format (YUV).
159 color = SkEncodedInfo::kYUVA_Color;
160 alpha = SkEncodedInfo::kUnpremul_Alpha;
162 color = SkEncodedInfo::kYUV_Color;
163 alpha = SkEncodedInfo::kOpaque_Alpha;
167 *result = kInvalidInput;
173 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));
178 static WEBP_CSP_MODE webp_decode_mode(SkColorType dstCT, bool premultiply) {
180 case kBGRA_8888_SkColorType:
181 return premultiply ? MODE_bgrA : MODE_BGRA;
182 case kRGBA_8888_SkColorType:
183 return premultiply ? MODE_rgbA : MODE_RGBA;
184 case kRGB_565_SkColorType:
191 SkWebpCodec::Frame* SkWebpCodec::FrameHolder::appendNewFrame(bool hasAlpha) {
192 const int i = this->size();
193 fFrames.emplace_back(i, hasAlpha ? SkEncodedInfo::kUnpremul_Alpha
194 : SkEncodedInfo::kOpaque_Alpha);
198 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
199 if (!desiredSubset) {
203 if (!this->bounds().contains(*desiredSubset)) {
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;
215 int SkWebpCodec::onGetRepetitionCount() {
216 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
217 if (!(flags & ANIMATION_FLAG)) {
221 int loopCount = WebPDemuxGetI(fDemux.get(), WEBP_FF_LOOP_COUNT);
222 if (0 == loopCount) {
223 return kRepetitionCountInfinite;
230 int SkWebpCodec::onGetFrameCount() {
231 auto flags = WebPDemuxGetI(fDemux.get(), WEBP_FF_FORMAT_FLAGS);
232 if (!(flags & ANIMATION_FLAG)) {
236 const uint32_t oldFrameCount = fFrameHolder.size();
238 return oldFrameCount;
241 const uint32_t frameCount = WebPDemuxGetI(fDemux, WEBP_FF_FRAME_COUNT);
242 if (oldFrameCount == frameCount) {
243 // We have already parsed this.
247 fFrameHolder.reserve(frameCount);
249 for (uint32_t i = oldFrameCount; i < frameCount; i++) {
251 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoIter(&iter);
253 if (!WebPDemuxGetFrame(fDemux.get(), i + 1, &iter)) {
258 // libwebp only reports complete frames of an animated image.
259 SkASSERT(iter.complete);
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 ?
264 SkCodecAnimation::DisposalMethod::kRestoreBGColor :
265 SkCodecAnimation::DisposalMethod::kKeep);
266 frame->setDuration(iter.duration);
267 if (WEBP_MUX_BLEND != iter.blend_method) {
268 frame->setBlend(SkCodecAnimation::Blend::kSrc);
270 fFrameHolder.setAlphaAndRequiredFrame(frame);
273 return fFrameHolder.size();
277 const SkFrame* SkWebpCodec::FrameHolder::onGetFrame(int i) const {
278 return static_cast<const SkFrame*>(this->frame(i));
281 const SkWebpCodec::Frame* SkWebpCodec::FrameHolder::frame(int i) const {
282 SkASSERT(i >= 0 && i < this->size());
286 bool SkWebpCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
287 if (i >= fFrameHolder.size()) {
291 const Frame* frame = fFrameHolder.frame(i);
297 // libwebp only reports fully received frames for an
299 frame->fillIn(frameInfo, true);
305 static bool is_8888(SkColorType colorType) {
307 case kRGBA_8888_SkColorType:
308 case kBGRA_8888_SkColorType:
315 // Requires that the src input be unpremultiplied (or opaque).
316 static void blend_line(SkColorType dstCT, void* dst,
317 SkColorType srcCT, const void* src,
321 SkRasterPipeline_MemoryCtx dst_ctx = { (void*)dst, 0 },
322 src_ctx = { (void*)src, 0 };
324 SkRasterPipeline_<256> p;
326 p.append_load_dst(dstCT, &dst_ctx);
327 if (kUnpremul_SkAlphaType == dstAt) {
328 p.append(SkRasterPipeline::premul_dst);
331 p.append_load(srcCT, &src_ctx);
333 p.append(SkRasterPipeline::premul);
336 p.append(SkRasterPipeline::srcover);
338 if (kUnpremul_SkAlphaType == dstAt) {
339 p.append(SkRasterPipeline::unpremul);
341 p.append_store(dstCT, &dst_ctx);
346 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
347 const Options& options, int* rowsDecodedPtr) {
348 const int index = options.fFrameIndex;
349 SkASSERT(0 == index || index < fFrameHolder.size());
350 SkASSERT(0 == index || !options.fSubset);
352 WebPDecoderConfig config;
353 if (0 == WebPInitDecoderConfig(&config)) {
355 // FIXME: New enum for this?
356 return kInvalidInput;
359 // Free any memory associated with the buffer. Must be called last, so we declare it first.
360 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
363 SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
364 // If this succeeded in onGetFrameCount(), it should succeed again here.
365 SkAssertResult(WebPDemuxGetFrame(fDemux, index + 1, &frame));
367 const bool independent = index == 0 ? true :
368 (fFrameHolder.frame(index)->getRequiredFrame() == kNoFrame);
369 // 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);
372 SkASSERT(this->bounds().contains(frameRect));
373 const bool frameIsSubset = frameRect != this->bounds();
374 if (independent && frameIsSubset) {
375 SkSampler::Fill(dstInfo, dst, rowBytes, options.fZeroInitialized);
378 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;
384 SkASSERT(this->bounds().contains(subset));
385 SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
386 SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
388 if (!SkIRect::Intersects(subset, frameRect)) {
392 int minXOffset = std::min(dstX, subset.x());
393 int minYOffset = std::min(dstY, subset.y());
396 frameRect.offset(-minXOffset, -minYOffset);
397 subset.offset(-minXOffset, -minYOffset);
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));
404 SkIRect intersection;
405 SkAssertResult(intersection.intersect(frameRect, subset));
406 subsetWidth = intersection.width();
407 subsetHeight = intersection.height();
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;
416 // Ignore the frame size and offset when determining if scaling is necessary.
417 int scaledWidth = subsetWidth;
418 int scaledHeight = subsetHeight;
419 SkISize srcSize = options.fSubset ? options.fSubset->size() : this->dimensions();
420 if (srcSize != dstInfo.dimensions()) {
421 config.options.use_scaling = 1;
424 float scaleX = ((float) dstInfo.width()) / srcSize.width();
425 float scaleY = ((float) dstInfo.height()) / srcSize.height();
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) {
437 scaledWidth = dstInfo.width();
438 scaledHeight = dstInfo.height();
441 config.options.scaled_width = scaledWidth;
442 config.options.scaled_height = scaledHeight;
445 const bool blendWithPrevFrame = !independent && frame.blend_method == WEBP_MUX_BLEND
448 auto webpInfo = dstInfo;
449 if (!frame.has_alpha) {
450 webpInfo = webpInfo.makeAlphaType(kOpaque_SkAlphaType);
451 } else if (this->colorXform() || blendWithPrevFrame) {
452 // the colorXform and blend_line expect unpremul.
453 webpInfo = webpInfo.makeAlphaType(kUnpremul_SkAlphaType);
455 if (this->colorXform()) {
456 // 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.
461 webpInfo = webpInfo.makeColorType(kBGRA_8888_SkColorType);
465 if ((this->colorXform() && !is_8888(dstInfo.colorType())) || blendWithPrevFrame) {
466 // 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.
469 webpDst.allocPixels(webpInfo);
471 // libwebp can decode directly into the output memory.
472 webpDst.installPixels(webpInfo, dst, rowBytes);
475 config.output.colorspace = webp_decode_mode(webpInfo.colorType(),
476 webpInfo.alphaType() == kPremul_SkAlphaType);
477 config.output.is_external_memory = 1;
479 config.output.u.RGBA.rgba = reinterpret_cast<uint8_t*>(webpDst.getAddr(dstX, dstY));
480 config.output.u.RGBA.stride = static_cast<int>(webpDst.rowBytes());
481 config.output.u.RGBA.size = webpDst.computeByteSize();
483 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
485 return kInvalidInput;
489 SkCodec::Result result;
490 switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
492 rowsDecoded = scaledHeight;
495 case VP8_STATUS_SUSPENDED:
496 if (!WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr)
497 || rowsDecoded <= 0) {
498 return kInvalidInput;
500 *rowsDecodedPtr = rowsDecoded + dstY;
501 result = kIncompleteInput;
504 return kInvalidInput;
507 const size_t dstBpp = dstInfo.bytesPerPixel();
508 dst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
509 const size_t srcRowBytes = config.output.u.RGBA.stride;
511 const auto dstCT = dstInfo.colorType();
512 if (this->colorXform()) {
513 uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
517 if (blendWithPrevFrame) {
518 // Xform into temporary bitmap big enough for one row.
519 tmp.allocPixels(dstInfo.makeWH(scaledWidth, 1));
520 xformDst = tmp.getPixels();
525 for (int y = 0; y < rowsDecoded; y++) {
526 this->applyColorXform(xformDst, xformSrc, scaledWidth);
527 if (blendWithPrevFrame) {
528 blend_line(dstCT, dst, dstCT, xformDst,
529 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
530 dst = SkTAddOffset<void>(dst, rowBytes);
532 xformDst = SkTAddOffset<void>(xformDst, rowBytes);
534 xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
536 } else if (blendWithPrevFrame) {
537 const uint8_t* src = config.output.u.RGBA.rgba;
539 for (int y = 0; y < rowsDecoded; y++) {
540 blend_line(dstCT, dst, webpDst.colorType(), src,
541 dstInfo.alphaType(), frame.has_alpha, scaledWidth);
542 src = SkTAddOffset<const uint8_t>(src, srcRowBytes);
543 dst = SkTAddOffset<void>(dst, rowBytes);
550 SkWebpCodec::SkWebpCodec(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream,
551 WebPDemuxer* demux, sk_sp<SkData> data, SkEncodedOrigin origin)
552 : INHERITED(std::move(info), skcms_PixelFormat_BGRA_8888, std::move(stream),
555 , fData(std::move(data))
558 const auto& eInfo = this->getEncodedInfo();
559 fFrameHolder.setScreenSize(eInfo.width(), eInfo.height());