Home | History | Annotate | Download | only in codec
      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 
      8 #include "SkCodecPriv.h"
      9 #include "SkColorSpaceXform.h"
     10 #include "SkSampler.h"
     11 #include "SkStreamPriv.h"
     12 #include "SkTemplates.h"
     13 #include "SkWebpCodec.h"
     14 
     15 // A WebP decoder on top of (subset of) libwebp
     16 // For more information on WebP image format, and libwebp library, see:
     17 //   https://code.google.com/speed/webp/
     18 //   http://www.webmproject.org/code/#libwebp-webp-image-library
     19 //   https://chromium.googlesource.com/webm/libwebp
     20 
     21 // If moving libwebp out of skia source tree, path for webp headers must be
     22 // updated accordingly. Here, we enforce using local copy in webp sub-directory.
     23 #include "webp/decode.h"
     24 #include "webp/demux.h"
     25 #include "webp/encode.h"
     26 
     27 bool SkWebpCodec::IsWebp(const void* buf, size_t bytesRead) {
     28     // WEBP starts with the following:
     29     // RIFFXXXXWEBPVP
     30     // Where XXXX is unspecified.
     31     const char* bytes = static_cast<const char*>(buf);
     32     return bytesRead >= 14 && !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6);
     33 }
     34 
     35 // Parse headers of RIFF container, and check for valid Webp (VP8) content.
     36 // NOTE: This calls peek instead of read, since onGetPixels will need these
     37 // bytes again.
     38 // Returns an SkWebpCodec on success;
     39 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) {
     40     std::unique_ptr<SkStream> streamDeleter(stream);
     41 
     42     // Webp demux needs a contiguous data buffer.
     43     sk_sp<SkData> data = nullptr;
     44     if (stream->getMemoryBase()) {
     45         // It is safe to make without copy because we'll hold onto the stream.
     46         data = SkData::MakeWithoutCopy(stream->getMemoryBase(), stream->getLength());
     47     } else {
     48         data = SkCopyStreamToData(stream);
     49 
     50         // If we are forced to copy the stream to a data, we can go ahead and delete the stream.
     51         streamDeleter.reset(nullptr);
     52     }
     53 
     54     // It's a little strange that the |demux| will outlive |webpData|, though it needs the
     55     // pointer in |webpData| to remain valid.  This works because the pointer remains valid
     56     // until the SkData is freed.
     57     WebPData webpData = { data->bytes(), data->size() };
     58     SkAutoTCallVProc<WebPDemuxer, WebPDemuxDelete> demux(WebPDemuxPartial(&webpData, nullptr));
     59     if (nullptr == demux) {
     60         return nullptr;
     61     }
     62 
     63     const int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
     64     const int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
     65 
     66     // Sanity check for image size that's about to be decoded.
     67     {
     68         const int64_t size = sk_64_mul(width, height);
     69         if (!sk_64_isS32(size)) {
     70             return nullptr;
     71         }
     72         // now check that if we are 4-bytes per pixel, we also don't overflow
     73         if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) {
     74             return nullptr;
     75         }
     76     }
     77 
     78     WebPChunkIterator chunkIterator;
     79     SkAutoTCallVProc<WebPChunkIterator, WebPDemuxReleaseChunkIterator> autoCI(&chunkIterator);
     80     sk_sp<SkColorSpace> colorSpace = nullptr;
     81     bool unsupportedICC = false;
     82     if (WebPDemuxGetChunk(demux, "ICCP", 1, &chunkIterator)) {
     83         colorSpace = SkColorSpace::MakeICC(chunkIterator.chunk.bytes, chunkIterator.chunk.size);
     84         if (!colorSpace) {
     85             unsupportedICC = true;
     86         }
     87     }
     88     if (!colorSpace) {
     89         colorSpace = SkColorSpace::MakeSRGB();
     90     }
     91 
     92     // Get the first frame and its "features" to determine the color and alpha types.
     93     // Since we do not yet support animated webp, this is the only frame that we will
     94     // decode.
     95     WebPIterator frame;
     96     SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
     97     if (!WebPDemuxGetFrame(demux, 1, &frame)) {
     98         return nullptr;
     99     }
    100 
    101     WebPBitstreamFeatures features;
    102     VP8StatusCode status = WebPGetFeatures(frame.fragment.bytes, frame.fragment.size, &features);
    103     if (VP8_STATUS_OK != status) {
    104         return nullptr;
    105     }
    106 
    107     SkEncodedInfo::Color color;
    108     SkEncodedInfo::Alpha alpha;
    109     switch (features.format) {
    110         case 0:
    111             // This indicates a "mixed" format.  We could see this for
    112             // animated webps (multiple fragments).
    113             // I believe that this is a rare case.
    114             // We could also guess kYUV here, but I think it makes more
    115             // sense to guess kBGRA which is likely closer to the final
    116             // output.  Otherwise, we might end up converting
    117             // BGRA->YUVA->BGRA.
    118             color = SkEncodedInfo::kBGRA_Color;
    119             alpha = SkEncodedInfo::kUnpremul_Alpha;
    120             break;
    121         case 1:
    122             // This is the lossy format (YUV).
    123             if (SkToBool(features.has_alpha) || frame.width != width || frame.height != height) {
    124                 color = SkEncodedInfo::kYUVA_Color;
    125                 alpha = SkEncodedInfo::kUnpremul_Alpha;
    126             } else {
    127                 color = SkEncodedInfo::kYUV_Color;
    128                 alpha = SkEncodedInfo::kOpaque_Alpha;
    129             }
    130             break;
    131         case 2:
    132             // This is the lossless format (BGRA).
    133             color = SkEncodedInfo::kBGRA_Color;
    134             alpha = SkEncodedInfo::kUnpremul_Alpha;
    135             break;
    136         default:
    137             return nullptr;
    138     }
    139 
    140     SkEncodedInfo info = SkEncodedInfo::Make(color, alpha, 8);
    141     SkWebpCodec* codecOut = new SkWebpCodec(width, height, info, std::move(colorSpace),
    142                                             streamDeleter.release(), demux.release(),
    143                                             std::move(data));
    144     codecOut->setUnsupportedICC(unsupportedICC);
    145     return codecOut;
    146 }
    147 
    148 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const {
    149     SkISize dim = this->getInfo().dimensions();
    150     // SkCodec treats zero dimensional images as errors, so the minimum size
    151     // that we will recommend is 1x1.
    152     dim.fWidth = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fWidth));
    153     dim.fHeight = SkTMax(1, SkScalarRoundToInt(desiredScale * dim.fHeight));
    154     return dim;
    155 }
    156 
    157 bool SkWebpCodec::onDimensionsSupported(const SkISize& dim) {
    158     const SkImageInfo& info = this->getInfo();
    159     return dim.width() >= 1 && dim.width() <= info.width()
    160             && dim.height() >= 1 && dim.height() <= info.height();
    161 }
    162 
    163 static WEBP_CSP_MODE webp_decode_mode(SkColorType ct, bool premultiply) {
    164     switch (ct) {
    165         case kBGRA_8888_SkColorType:
    166             return premultiply ? MODE_bgrA : MODE_BGRA;
    167         case kRGBA_8888_SkColorType:
    168             return premultiply ? MODE_rgbA : MODE_RGBA;
    169         case kRGB_565_SkColorType:
    170             return MODE_RGB_565;
    171         default:
    172             return MODE_LAST;
    173     }
    174 }
    175 
    176 bool SkWebpCodec::onGetValidSubset(SkIRect* desiredSubset) const {
    177     if (!desiredSubset) {
    178         return false;
    179     }
    180 
    181     SkIRect dimensions  = SkIRect::MakeSize(this->getInfo().dimensions());
    182     if (!dimensions.contains(*desiredSubset)) {
    183         return false;
    184     }
    185 
    186     // As stated below, libwebp snaps to even left and top. Make sure top and left are even, so we
    187     // decode this exact subset.
    188     // Leave right and bottom unmodified, so we suggest a slightly larger subset than requested.
    189     desiredSubset->fLeft = (desiredSubset->fLeft >> 1) << 1;
    190     desiredSubset->fTop = (desiredSubset->fTop >> 1) << 1;
    191     return true;
    192 }
    193 
    194 SkCodec::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, size_t rowBytes,
    195                                          const Options& options, SkPMColor*, int*,
    196                                          int* rowsDecodedPtr) {
    197     if (!conversion_possible(dstInfo, this->getInfo()) ||
    198         !this->initializeColorXform(dstInfo, options.fPremulBehavior))
    199     {
    200         return kInvalidConversion;
    201     }
    202 
    203     WebPDecoderConfig config;
    204     if (0 == WebPInitDecoderConfig(&config)) {
    205         // ABI mismatch.
    206         // FIXME: New enum for this?
    207         return kInvalidInput;
    208     }
    209 
    210     // Free any memory associated with the buffer. Must be called last, so we declare it first.
    211     SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output));
    212 
    213     WebPIterator frame;
    214     SkAutoTCallVProc<WebPIterator, WebPDemuxReleaseIterator> autoFrame(&frame);
    215     // If this succeeded in NewFromStream(), it should succeed again here.
    216     SkAssertResult(WebPDemuxGetFrame(fDemux, 1, &frame));
    217 
    218     // Get the frameRect.  libwebp will have already signaled an error if this is not fully
    219     // contained by the canvas.
    220     auto frameRect = SkIRect::MakeXYWH(frame.x_offset, frame.y_offset, frame.width, frame.height);
    221     SkASSERT(this->getInfo().bounds().contains(frameRect));
    222     bool frameIsSubset = frameRect.size() != this->getInfo().dimensions();
    223     if (frameIsSubset) {
    224         SkSampler::Fill(dstInfo, dst, rowBytes, 0, options.fZeroInitialized);
    225     }
    226 
    227     int dstX = frameRect.x();
    228     int dstY = frameRect.y();
    229     int subsetWidth = frameRect.width();
    230     int subsetHeight = frameRect.height();
    231     if (options.fSubset) {
    232         SkIRect subset = *options.fSubset;
    233         SkASSERT(this->getInfo().bounds().contains(subset));
    234         SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
    235         SkASSERT(this->getValidSubset(&subset) && subset == *options.fSubset);
    236 
    237         if (!SkIRect::IntersectsNoEmptyCheck(subset, frameRect)) {
    238             return kSuccess;
    239         }
    240 
    241         int minXOffset = SkTMin(dstX, subset.x());
    242         int minYOffset = SkTMin(dstY, subset.y());
    243         dstX -= minXOffset;
    244         dstY -= minYOffset;
    245         frameRect.offset(-minXOffset, -minYOffset);
    246         subset.offset(-minXOffset, -minYOffset);
    247 
    248         // Just like we require that the requested subset x and y offset are even, libwebp
    249         // guarantees that the frame x and y offset are even (it's actually impossible to specify
    250         // an odd frame offset).  So we can still guarantee that the adjusted offsets are even.
    251         SkASSERT(SkIsAlign2(subset.fLeft) && SkIsAlign2(subset.fTop));
    252 
    253         SkIRect intersection;
    254         SkAssertResult(intersection.intersect(frameRect, subset));
    255         subsetWidth = intersection.width();
    256         subsetHeight = intersection.height();
    257 
    258         config.options.use_cropping = 1;
    259         config.options.crop_left = subset.x();
    260         config.options.crop_top = subset.y();
    261         config.options.crop_width = subsetWidth;
    262         config.options.crop_height = subsetHeight;
    263     }
    264 
    265     // Ignore the frame size and offset when determining if scaling is necessary.
    266     int scaledWidth = subsetWidth;
    267     int scaledHeight = subsetHeight;
    268     SkISize srcSize = options.fSubset ? options.fSubset->size() : this->getInfo().dimensions();
    269     if (srcSize != dstInfo.dimensions()) {
    270         config.options.use_scaling = 1;
    271 
    272         if (frameIsSubset) {
    273             float scaleX = ((float) dstInfo.width()) / srcSize.width();
    274             float scaleY = ((float) dstInfo.height()) / srcSize.height();
    275 
    276             // We need to be conservative here and floor rather than round.
    277             // Otherwise, we may find ourselves decoding off the end of memory.
    278             dstX = scaleX * dstX;
    279             scaledWidth = scaleX * scaledWidth;
    280             dstY = scaleY * dstY;
    281             scaledHeight = scaleY * scaledHeight;
    282             if (0 == scaledWidth || 0 == scaledHeight) {
    283                 return kSuccess;
    284             }
    285         } else {
    286             scaledWidth = dstInfo.width();
    287             scaledHeight = dstInfo.height();
    288         }
    289 
    290         config.options.scaled_width = scaledWidth;
    291         config.options.scaled_height = scaledHeight;
    292     }
    293 
    294     // Swizzling between RGBA and BGRA is zero cost in a color transform.  So when we have a
    295     // color transform, we should decode to whatever is easiest for libwebp, and then let the
    296     // color transform swizzle if necessary.
    297     // Lossy webp is encoded as YUV (so RGBA and BGRA are the same cost).  Lossless webp is
    298     // encoded as BGRA. This means decoding to BGRA is either faster or the same cost as RGBA.
    299     config.output.colorspace = this->colorXform() ? MODE_BGRA :
    300             webp_decode_mode(dstInfo.colorType(), dstInfo.alphaType() == kPremul_SkAlphaType);
    301     config.output.is_external_memory = 1;
    302 
    303     // We will decode the entire image and then perform the color transform.  libwebp
    304     // does not provide a row-by-row API.  This is a shame particularly when we do not want
    305     // 8888, since we will need to create another image sized buffer.
    306     SkAutoTMalloc<uint32_t> pixels;
    307     bool needsCopy = this->colorXform() && kRGBA_8888_SkColorType != dstInfo.colorType() &&
    308                                            kBGRA_8888_SkColorType != dstInfo.colorType();
    309     void* webpDst = needsCopy ? pixels.reset(dstInfo.width() * dstInfo.height()) : dst;
    310     size_t webpRowBytes = needsCopy ? dstInfo.width() * sizeof(uint32_t) : rowBytes;
    311     size_t totalBytes = needsCopy ? webpRowBytes * dstInfo.height()
    312                                   : dstInfo.getSafeSize(webpRowBytes);
    313     size_t dstBpp = SkColorTypeBytesPerPixel(dstInfo.colorType());
    314     size_t webpBpp = needsCopy ? sizeof(uint32_t) : dstBpp;
    315 
    316     size_t offset = dstX * webpBpp + dstY * webpRowBytes;
    317     config.output.u.RGBA.rgba = SkTAddOffset<uint8_t>(webpDst, offset);
    318     config.output.u.RGBA.stride = (int) webpRowBytes;
    319     config.output.u.RGBA.size = totalBytes - offset;
    320 
    321     SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(nullptr, 0, &config));
    322     if (!idec) {
    323         return kInvalidInput;
    324     }
    325 
    326     int rowsDecoded;
    327     SkCodec::Result result;
    328     switch (WebPIUpdate(idec, frame.fragment.bytes, frame.fragment.size)) {
    329         case VP8_STATUS_OK:
    330             rowsDecoded = scaledHeight;
    331             result = kSuccess;
    332             break;
    333         case VP8_STATUS_SUSPENDED:
    334             WebPIDecGetRGB(idec, &rowsDecoded, nullptr, nullptr, nullptr);
    335             *rowsDecodedPtr = rowsDecoded + dstY;
    336             result = kIncompleteInput;
    337             break;
    338         default:
    339             return kInvalidInput;
    340     }
    341 
    342     if (this->colorXform()) {
    343         SkColorSpaceXform::ColorFormat dstColorFormat = select_xform_format(dstInfo.colorType());
    344         SkAlphaType xformAlphaType = select_xform_alpha(dstInfo.alphaType(),
    345                                                         this->getInfo().alphaType());
    346 
    347         uint32_t* xformSrc = (uint32_t*) config.output.u.RGBA.rgba;
    348         void* xformDst = SkTAddOffset<void>(dst, dstBpp * dstX + rowBytes * dstY);
    349         size_t srcRowBytes = config.output.u.RGBA.stride;
    350         for (int y = 0; y < rowsDecoded; y++) {
    351             SkAssertResult(this->colorXform()->apply(dstColorFormat, xformDst,
    352                     SkColorSpaceXform::kBGRA_8888_ColorFormat, xformSrc, scaledWidth,
    353                     xformAlphaType));
    354             xformDst = SkTAddOffset<void>(xformDst, rowBytes);
    355             xformSrc = SkTAddOffset<uint32_t>(xformSrc, srcRowBytes);
    356         }
    357     }
    358 
    359     return result;
    360 }
    361 
    362 SkWebpCodec::SkWebpCodec(int width, int height, const SkEncodedInfo& info,
    363                          sk_sp<SkColorSpace> colorSpace, SkStream* stream, WebPDemuxer* demux,
    364                          sk_sp<SkData> data)
    365     : INHERITED(width, height, info, stream, std::move(colorSpace))
    366     , fDemux(demux)
    367     , fData(std::move(data))
    368 {}
    369