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 | |
| 136 | SkEncodedInfo info = SkEncodedInfo::Make( |
| 137 | SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8); |
| 138 | |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 139 | SkEncodedOrigin orientation = get_orientation(frameInfo); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 140 | |
| 141 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 142 | if ((frameInfo.mIccSize > 0) && (frameInfo.mIccData != nullptr)) { |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 143 | colorSpace = SkColorSpace::MakeICC(frameInfo.mIccData.get(), |
| 144 | frameInfo.mIccSize); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 145 | } |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 146 | if (!colorSpace || colorSpace->type() != SkColorSpace::kRGB_Type) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 147 | colorSpace = SkColorSpace::MakeSRGB(); |
| 148 | } |
| 149 | |
| 150 | *result = kSuccess; |
| 151 | return std::unique_ptr<SkCodec>(new SkHeifCodec(frameInfo.mWidth, frameInfo.mHeight, |
| 152 | info, heifDecoder.release(), std::move(colorSpace), orientation)); |
| 153 | } |
| 154 | |
| 155 | SkHeifCodec::SkHeifCodec(int width, int height, const SkEncodedInfo& info, |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 156 | HeifDecoder* heifDecoder, sk_sp<SkColorSpace> colorSpace, SkEncodedOrigin origin) |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 157 | : INHERITED(width, height, info, SkColorSpaceXform::kRGBA_8888_ColorFormat, |
| 158 | nullptr, std::move(colorSpace), origin) |
| 159 | , fHeifDecoder(heifDecoder) |
| 160 | , fSwizzleSrcRow(nullptr) |
| 161 | , fColorXformSrcRow(nullptr) |
| 162 | {} |
| 163 | |
| 164 | /* |
| 165 | * Checks if the conversion between the input image and the requested output |
| 166 | * image has been implemented |
| 167 | * Sets the output color format |
| 168 | */ |
| 169 | bool SkHeifCodec::setOutputColorFormat(const SkImageInfo& dstInfo) { |
| 170 | if (kUnknown_SkAlphaType == dstInfo.alphaType()) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if (kOpaque_SkAlphaType != dstInfo.alphaType()) { |
| 175 | SkCodecPrintf("Warning: an opaque image should be decoded as opaque " |
| 176 | "- it is being decoded as non-opaque, which will draw slower\n"); |
| 177 | } |
| 178 | |
| 179 | switch (dstInfo.colorType()) { |
| 180 | case kRGBA_8888_SkColorType: |
| 181 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 182 | |
| 183 | case kBGRA_8888_SkColorType: |
| 184 | return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888); |
| 185 | |
| 186 | case kRGB_565_SkColorType: |
| 187 | if (this->colorXform()) { |
| 188 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 189 | } else { |
| 190 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565); |
| 191 | } |
| 192 | |
| 193 | case kRGBA_F16_SkColorType: |
| 194 | SkASSERT(this->colorXform()); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 195 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 196 | |
| 197 | default: |
| 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, |
| 203 | const Options& opts) { |
| 204 | // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case, |
| 205 | // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer. |
| 206 | // We can never swizzle "in place" because the swizzler may perform sampling and/or |
| 207 | // subsetting. |
| 208 | // When fColorXformSrcRow is non-null, it means that we need to color xform and that |
| 209 | // we cannot color xform "in place" (many times we can, but not when the dst is F16). |
| 210 | // In this case, we will color xform from fColorXformSrcRow into the dst. |
| 211 | uint8_t* decodeDst = (uint8_t*) dst; |
| 212 | uint32_t* swizzleDst = (uint32_t*) dst; |
| 213 | size_t decodeDstRowBytes = rowBytes; |
| 214 | size_t swizzleDstRowBytes = rowBytes; |
| 215 | int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width(); |
| 216 | if (fSwizzleSrcRow && fColorXformSrcRow) { |
| 217 | decodeDst = fSwizzleSrcRow; |
| 218 | swizzleDst = fColorXformSrcRow; |
| 219 | decodeDstRowBytes = 0; |
| 220 | swizzleDstRowBytes = 0; |
| 221 | dstWidth = fSwizzler->swizzleWidth(); |
| 222 | } else if (fColorXformSrcRow) { |
| 223 | decodeDst = (uint8_t*) fColorXformSrcRow; |
| 224 | swizzleDst = fColorXformSrcRow; |
| 225 | decodeDstRowBytes = 0; |
| 226 | swizzleDstRowBytes = 0; |
| 227 | } else if (fSwizzleSrcRow) { |
| 228 | decodeDst = fSwizzleSrcRow; |
| 229 | decodeDstRowBytes = 0; |
| 230 | dstWidth = fSwizzler->swizzleWidth(); |
| 231 | } |
| 232 | |
| 233 | for (int y = 0; y < count; y++) { |
| 234 | if (!fHeifDecoder->getScanline(decodeDst)) { |
| 235 | return y; |
| 236 | } |
| 237 | |
| 238 | if (fSwizzler) { |
| 239 | fSwizzler->swizzle(swizzleDst, decodeDst); |
| 240 | } |
| 241 | |
| 242 | if (this->colorXform()) { |
| 243 | this->applyColorXform(dst, swizzleDst, dstWidth, kOpaque_SkAlphaType); |
| 244 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 245 | } |
| 246 | |
| 247 | decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes); |
| 248 | swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes); |
| 249 | } |
| 250 | |
| 251 | return count; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Performs the heif decode |
| 256 | */ |
| 257 | SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 258 | void* dst, size_t dstRowBytes, |
| 259 | const Options& options, |
| 260 | int* rowsDecoded) { |
| 261 | if (options.fSubset) { |
| 262 | // Not supporting subsets on this path for now. |
| 263 | // TODO: if the heif has tiles, we can support subset here, but |
| 264 | // need to retrieve tile config from metadata retriever first. |
| 265 | return kUnimplemented; |
| 266 | } |
| 267 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 268 | // Check if we can decode to the requested destination and set the output color space |
| 269 | if (!this->setOutputColorFormat(dstInfo)) { |
| 270 | return kInvalidConversion; |
| 271 | } |
| 272 | |
| 273 | if (!fHeifDecoder->decode(&fFrameInfo)) { |
| 274 | return kInvalidInput; |
| 275 | } |
| 276 | |
Chong Zhang | 722b3a7 | 2017-08-23 11:17:36 -0700 | [diff] [blame] | 277 | fSwizzler.reset(nullptr); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 278 | this->allocateStorage(dstInfo); |
| 279 | |
| 280 | int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options); |
| 281 | if (rows < dstInfo.height()) { |
| 282 | *rowsDecoded = rows; |
| 283 | return kIncompleteInput; |
| 284 | } |
| 285 | |
| 286 | return kSuccess; |
| 287 | } |
| 288 | |
| 289 | void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) { |
| 290 | int dstWidth = dstInfo.width(); |
| 291 | |
| 292 | size_t swizzleBytes = 0; |
| 293 | if (fSwizzler) { |
| 294 | swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth; |
| 295 | dstWidth = fSwizzler->swizzleWidth(); |
| 296 | SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes)); |
| 297 | } |
| 298 | |
| 299 | size_t xformBytes = 0; |
| 300 | if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() || |
| 301 | kRGB_565_SkColorType == dstInfo.colorType())) { |
| 302 | xformBytes = dstWidth * sizeof(uint32_t); |
| 303 | } |
| 304 | |
| 305 | size_t totalBytes = swizzleBytes + xformBytes; |
| 306 | fStorage.reset(totalBytes); |
| 307 | if (totalBytes > 0) { |
| 308 | fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr; |
| 309 | fColorXformSrcRow = (xformBytes > 0) ? |
| 310 | SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void SkHeifCodec::initializeSwizzler( |
| 315 | const SkImageInfo& dstInfo, const Options& options) { |
| 316 | SkEncodedInfo swizzlerInfo = this->getEncodedInfo(); |
| 317 | |
| 318 | SkImageInfo swizzlerDstInfo = dstInfo; |
| 319 | if (this->colorXform()) { |
| 320 | // The color xform will be expecting RGBA 8888 input. |
| 321 | swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType); |
| 322 | } |
| 323 | |
| 324 | fSwizzler.reset(SkSwizzler::CreateSwizzler(swizzlerInfo, nullptr, |
| 325 | swizzlerDstInfo, options, nullptr, true)); |
| 326 | SkASSERT(fSwizzler); |
| 327 | } |
| 328 | |
| 329 | SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) { |
| 330 | if (!createIfNecessary || fSwizzler) { |
| 331 | SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow)); |
| 332 | return fSwizzler.get(); |
| 333 | } |
| 334 | |
| 335 | this->initializeSwizzler(this->dstInfo(), this->options()); |
| 336 | this->allocateStorage(this->dstInfo()); |
| 337 | return fSwizzler.get(); |
| 338 | } |
| 339 | |
| 340 | SkCodec::Result SkHeifCodec::onStartScanlineDecode( |
| 341 | const SkImageInfo& dstInfo, const Options& options) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 342 | // Check if we can decode to the requested destination and set the output color space |
| 343 | if (!this->setOutputColorFormat(dstInfo)) { |
| 344 | return kInvalidConversion; |
| 345 | } |
| 346 | |
| 347 | // TODO: For now, just decode the whole thing even when there is a subset. |
| 348 | // If the heif image has tiles, we could potentially do this much faster, |
| 349 | // but the tile configuration needs to be retrieved from the metadata. |
| 350 | if (!fHeifDecoder->decode(&fFrameInfo)) { |
| 351 | return kInvalidInput; |
| 352 | } |
| 353 | |
Chong Zhang | 722b3a7 | 2017-08-23 11:17:36 -0700 | [diff] [blame] | 354 | if (options.fSubset) { |
| 355 | this->initializeSwizzler(dstInfo, options); |
| 356 | } else { |
| 357 | fSwizzler.reset(nullptr); |
| 358 | } |
| 359 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 360 | this->allocateStorage(dstInfo); |
| 361 | |
| 362 | return kSuccess; |
| 363 | } |
| 364 | |
| 365 | int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
| 366 | return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options()); |
| 367 | } |
| 368 | |
| 369 | bool SkHeifCodec::onSkipScanlines(int count) { |
| 370 | return count == (int) fHeifDecoder->skipScanlines(count); |
| 371 | } |
| 372 | |
| 373 | #endif // SK_HAS_HEIF_LIBRARY |