Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 "SkTypes.h" |
| 9 | |
| 10 | #ifdef SK_HAS_HEIF_LIBRARY |
| 11 | #include "SkCodec.h" |
| 12 | #include "SkCodecPriv.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 13 | #include "SkColorData.h" |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 14 | #include "SkEndian.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkHeifCodec.h" |
| 17 | |
| 18 | #define FOURCC(c1, c2, c3, c4) \ |
| 19 | ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4)) |
| 20 | |
| 21 | bool SkHeifCodec::IsHeif(const void* buffer, size_t bytesRead) { |
| 22 | // Parse the ftyp box up to bytesRead to determine if this is HEIF. |
| 23 | // Any valid ftyp box should have at least 8 bytes. |
| 24 | if (bytesRead < 8) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | uint32_t* ptr = (uint32_t*)buffer; |
| 29 | uint64_t chunkSize = SkEndian_SwapBE32(ptr[0]); |
| 30 | uint32_t chunkType = SkEndian_SwapBE32(ptr[1]); |
| 31 | |
| 32 | if (chunkType != FOURCC('f', 't', 'y', 'p')) { |
| 33 | return false; |
| 34 | } |
| 35 | |
Mike Klein | 329d504 | 2017-10-20 13:48:55 -0400 | [diff] [blame] | 36 | int64_t offset = 8; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 37 | if (chunkSize == 1) { |
| 38 | // This indicates that the next 8 bytes represent the chunk size, |
| 39 | // and chunk data comes after that. |
| 40 | if (bytesRead < 16) { |
| 41 | return false; |
| 42 | } |
| 43 | auto* chunkSizePtr = SkTAddOffset<const uint64_t>(buffer, offset); |
| 44 | chunkSize = SkEndian_SwapBE64(*chunkSizePtr); |
| 45 | if (chunkSize < 16) { |
| 46 | // The smallest valid chunk is 16 bytes long in this case. |
| 47 | return false; |
| 48 | } |
| 49 | offset += 8; |
| 50 | } else if (chunkSize < 8) { |
| 51 | // The smallest valid chunk is 8 bytes long. |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | if (chunkSize > bytesRead) { |
| 56 | chunkSize = bytesRead; |
| 57 | } |
Mike Klein | 329d504 | 2017-10-20 13:48:55 -0400 | [diff] [blame] | 58 | int64_t chunkDataSize = chunkSize - offset; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 59 | // It should at least have major brand (4-byte) and minor version (4-bytes). |
| 60 | // The rest of the chunk (if any) is a list of (4-byte) compatible brands. |
| 61 | if (chunkDataSize < 8) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4; |
| 66 | for (size_t i = 0; i < numCompatibleBrands + 2; ++i) { |
| 67 | if (i == 1) { |
| 68 | // Skip this index, it refers to the minorVersion, |
| 69 | // not a brand. |
| 70 | continue; |
| 71 | } |
| 72 | auto* brandPtr = SkTAddOffset<const uint32_t>(buffer, offset + 4 * i); |
| 73 | uint32_t brand = SkEndian_SwapBE32(*brandPtr); |
Chong Zhang | 3c23593 | 2017-10-03 13:16:06 -0700 | [diff] [blame] | 74 | if (brand == FOURCC('m', 'i', 'f', '1') || brand == FOURCC('h', 'e', 'i', 'c') |
| 75 | || brand == FOURCC('m', 's', 'f', '1') || brand == FOURCC('h', 'e', 'v', 'c')) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 82 | static SkEncodedOrigin get_orientation(const HeifFrameInfo& frameInfo) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 83 | switch (frameInfo.mRotationAngle) { |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 84 | case 0: return kTopLeft_SkEncodedOrigin; |
| 85 | case 90: return kRightTop_SkEncodedOrigin; |
| 86 | case 180: return kBottomRight_SkEncodedOrigin; |
| 87 | case 270: return kLeftBottom_SkEncodedOrigin; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 88 | } |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 89 | return kDefault_SkEncodedOrigin; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | struct SkHeifStreamWrapper : public HeifStream { |
| 93 | SkHeifStreamWrapper(SkStream* stream) : fStream(stream) {} |
| 94 | |
| 95 | ~SkHeifStreamWrapper() override {} |
| 96 | |
| 97 | size_t read(void* buffer, size_t size) override { |
| 98 | return fStream->read(buffer, size); |
| 99 | } |
| 100 | |
| 101 | bool rewind() override { |
| 102 | return fStream->rewind(); |
| 103 | } |
| 104 | |
| 105 | bool seek(size_t position) override { |
| 106 | return fStream->seek(position); |
| 107 | } |
| 108 | |
| 109 | bool hasLength() const override { |
| 110 | return fStream->hasLength(); |
| 111 | } |
| 112 | |
| 113 | size_t getLength() const override { |
| 114 | return fStream->getLength(); |
| 115 | } |
| 116 | |
| 117 | private: |
| 118 | std::unique_ptr<SkStream> fStream; |
| 119 | }; |
| 120 | |
| 121 | std::unique_ptr<SkCodec> SkHeifCodec::MakeFromStream( |
| 122 | std::unique_ptr<SkStream> stream, Result* result) { |
| 123 | std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder()); |
| 124 | if (heifDecoder.get() == nullptr) { |
| 125 | *result = kInternalError; |
| 126 | return nullptr; |
| 127 | } |
| 128 | |
| 129 | HeifFrameInfo frameInfo; |
| 130 | if (!heifDecoder->init(new SkHeifStreamWrapper(stream.release()), |
| 131 | &frameInfo)) { |
| 132 | *result = kInvalidInput; |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 136 | std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr; |
| 137 | if ((frameInfo.mIccSize > 0) && (frameInfo.mIccData != nullptr)) { |
| 138 | // FIXME: Would it be possible to use MakeWithoutCopy? |
| 139 | auto icc = SkData::MakeWithCopy(frameInfo.mIccData.get(), frameInfo.mIccSize); |
| 140 | profile = SkEncodedInfo::ICCProfile::Make(std::move(icc)); |
| 141 | } |
| 142 | if (!profile || profile->profile()->data_color_space != skcms_Signature_RGB) { |
| 143 | profile = SkEncodedInfo::ICCProfile::MakeSRGB(); |
| 144 | } |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 145 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 146 | SkEncodedInfo info = SkEncodedInfo::Make(frameInfo.mWidth, frameInfo.mHeight, |
| 147 | SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8, std::move(profile)); |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 148 | SkEncodedOrigin orientation = get_orientation(frameInfo); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 149 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 150 | *result = kSuccess; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 151 | return std::unique_ptr<SkCodec>(new SkHeifCodec(std::move(info), heifDecoder.release(), |
| 152 | orientation)); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 155 | SkHeifCodec::SkHeifCodec(SkEncodedInfo&& info, HeifDecoder* heifDecoder, SkEncodedOrigin origin) |
| 156 | : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin) |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 157 | , fHeifDecoder(heifDecoder) |
| 158 | , fSwizzleSrcRow(nullptr) |
| 159 | , fColorXformSrcRow(nullptr) |
| 160 | {} |
| 161 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 162 | |
| 163 | bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, SkColorType /*srcColorType*/, |
| 164 | bool srcIsOpaque, bool needsColorXform) { |
| 165 | SkASSERT(srcIsOpaque); |
| 166 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 167 | if (kUnknown_SkAlphaType == dstInfo.alphaType()) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | if (kOpaque_SkAlphaType != dstInfo.alphaType()) { |
| 172 | SkCodecPrintf("Warning: an opaque image should be decoded as opaque " |
| 173 | "- it is being decoded as non-opaque, which will draw slower\n"); |
| 174 | } |
| 175 | |
| 176 | switch (dstInfo.colorType()) { |
| 177 | case kRGBA_8888_SkColorType: |
| 178 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 179 | |
| 180 | case kBGRA_8888_SkColorType: |
| 181 | return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888); |
| 182 | |
| 183 | case kRGB_565_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 184 | if (needsColorXform) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 185 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 186 | } else { |
| 187 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565); |
| 188 | } |
| 189 | |
| 190 | case kRGBA_F16_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 191 | SkASSERT(needsColorXform); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 192 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 193 | |
| 194 | default: |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, |
| 200 | const Options& opts) { |
| 201 | // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case, |
| 202 | // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer. |
| 203 | // We can never swizzle "in place" because the swizzler may perform sampling and/or |
| 204 | // subsetting. |
| 205 | // When fColorXformSrcRow is non-null, it means that we need to color xform and that |
| 206 | // we cannot color xform "in place" (many times we can, but not when the dst is F16). |
| 207 | // In this case, we will color xform from fColorXformSrcRow into the dst. |
| 208 | uint8_t* decodeDst = (uint8_t*) dst; |
| 209 | uint32_t* swizzleDst = (uint32_t*) dst; |
| 210 | size_t decodeDstRowBytes = rowBytes; |
| 211 | size_t swizzleDstRowBytes = rowBytes; |
| 212 | int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width(); |
| 213 | if (fSwizzleSrcRow && fColorXformSrcRow) { |
| 214 | decodeDst = fSwizzleSrcRow; |
| 215 | swizzleDst = fColorXformSrcRow; |
| 216 | decodeDstRowBytes = 0; |
| 217 | swizzleDstRowBytes = 0; |
| 218 | dstWidth = fSwizzler->swizzleWidth(); |
| 219 | } else if (fColorXformSrcRow) { |
| 220 | decodeDst = (uint8_t*) fColorXformSrcRow; |
| 221 | swizzleDst = fColorXformSrcRow; |
| 222 | decodeDstRowBytes = 0; |
| 223 | swizzleDstRowBytes = 0; |
| 224 | } else if (fSwizzleSrcRow) { |
| 225 | decodeDst = fSwizzleSrcRow; |
| 226 | decodeDstRowBytes = 0; |
| 227 | dstWidth = fSwizzler->swizzleWidth(); |
| 228 | } |
| 229 | |
| 230 | for (int y = 0; y < count; y++) { |
| 231 | if (!fHeifDecoder->getScanline(decodeDst)) { |
| 232 | return y; |
| 233 | } |
| 234 | |
| 235 | if (fSwizzler) { |
| 236 | fSwizzler->swizzle(swizzleDst, decodeDst); |
| 237 | } |
| 238 | |
| 239 | if (this->colorXform()) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 240 | this->applyColorXform(dst, swizzleDst, dstWidth); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 241 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 242 | } |
| 243 | |
| 244 | decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes); |
| 245 | swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes); |
| 246 | } |
| 247 | |
| 248 | return count; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Performs the heif decode |
| 253 | */ |
| 254 | SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 255 | void* dst, size_t dstRowBytes, |
| 256 | const Options& options, |
| 257 | int* rowsDecoded) { |
| 258 | if (options.fSubset) { |
| 259 | // Not supporting subsets on this path for now. |
| 260 | // TODO: if the heif has tiles, we can support subset here, but |
| 261 | // need to retrieve tile config from metadata retriever first. |
| 262 | return kUnimplemented; |
| 263 | } |
| 264 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 265 | if (!fHeifDecoder->decode(&fFrameInfo)) { |
| 266 | return kInvalidInput; |
| 267 | } |
| 268 | |
Chong Zhang | 722b3a7 | 2017-08-23 11:17:36 -0700 | [diff] [blame] | 269 | fSwizzler.reset(nullptr); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 270 | this->allocateStorage(dstInfo); |
| 271 | |
| 272 | int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options); |
| 273 | if (rows < dstInfo.height()) { |
| 274 | *rowsDecoded = rows; |
| 275 | return kIncompleteInput; |
| 276 | } |
| 277 | |
| 278 | return kSuccess; |
| 279 | } |
| 280 | |
| 281 | void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) { |
| 282 | int dstWidth = dstInfo.width(); |
| 283 | |
| 284 | size_t swizzleBytes = 0; |
| 285 | if (fSwizzler) { |
| 286 | swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth; |
| 287 | dstWidth = fSwizzler->swizzleWidth(); |
| 288 | SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes)); |
| 289 | } |
| 290 | |
| 291 | size_t xformBytes = 0; |
| 292 | if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() || |
| 293 | kRGB_565_SkColorType == dstInfo.colorType())) { |
| 294 | xformBytes = dstWidth * sizeof(uint32_t); |
| 295 | } |
| 296 | |
| 297 | size_t totalBytes = swizzleBytes + xformBytes; |
| 298 | fStorage.reset(totalBytes); |
| 299 | if (totalBytes > 0) { |
| 300 | fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr; |
| 301 | fColorXformSrcRow = (xformBytes > 0) ? |
| 302 | SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | void SkHeifCodec::initializeSwizzler( |
| 307 | const SkImageInfo& dstInfo, const Options& options) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 308 | SkImageInfo swizzlerDstInfo = dstInfo; |
| 309 | if (this->colorXform()) { |
| 310 | // The color xform will be expecting RGBA 8888 input. |
| 311 | swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType); |
| 312 | } |
| 313 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 314 | fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), nullptr, |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 315 | swizzlerDstInfo, options, nullptr, true)); |
| 316 | SkASSERT(fSwizzler); |
| 317 | } |
| 318 | |
| 319 | SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) { |
| 320 | if (!createIfNecessary || fSwizzler) { |
| 321 | SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow)); |
| 322 | return fSwizzler.get(); |
| 323 | } |
| 324 | |
| 325 | this->initializeSwizzler(this->dstInfo(), this->options()); |
| 326 | this->allocateStorage(this->dstInfo()); |
| 327 | return fSwizzler.get(); |
| 328 | } |
| 329 | |
| 330 | SkCodec::Result SkHeifCodec::onStartScanlineDecode( |
| 331 | const SkImageInfo& dstInfo, const Options& options) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 332 | // TODO: For now, just decode the whole thing even when there is a subset. |
| 333 | // If the heif image has tiles, we could potentially do this much faster, |
| 334 | // but the tile configuration needs to be retrieved from the metadata. |
| 335 | if (!fHeifDecoder->decode(&fFrameInfo)) { |
| 336 | return kInvalidInput; |
| 337 | } |
| 338 | |
Chong Zhang | 722b3a7 | 2017-08-23 11:17:36 -0700 | [diff] [blame] | 339 | if (options.fSubset) { |
| 340 | this->initializeSwizzler(dstInfo, options); |
| 341 | } else { |
| 342 | fSwizzler.reset(nullptr); |
| 343 | } |
| 344 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 345 | this->allocateStorage(dstInfo); |
| 346 | |
| 347 | return kSuccess; |
| 348 | } |
| 349 | |
| 350 | int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
| 351 | return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options()); |
| 352 | } |
| 353 | |
| 354 | bool SkHeifCodec::onSkipScanlines(int count) { |
| 355 | return count == (int) fHeifDecoder->skipScanlines(count); |
| 356 | } |
| 357 | |
| 358 | #endif // SK_HAS_HEIF_LIBRARY |