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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 9 | |
| 10 | #ifdef SK_HAS_HEIF_LIBRARY |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/codec/SkCodec.h" |
| 12 | #include "include/core/SkStream.h" |
| 13 | #include "include/private/SkColorData.h" |
| 14 | #include "src/codec/SkCodecPriv.h" |
| 15 | #include "src/codec/SkHeifCodec.h" |
| 16 | #include "src/core/SkEndian.h" |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 17 | |
| 18 | #define FOURCC(c1, c2, c3, c4) \ |
| 19 | ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4)) |
| 20 | |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 21 | bool SkHeifCodec::IsSupported(const void* buffer, size_t bytesRead, |
| 22 | SkEncodedImageFormat* format) { |
| 23 | // Parse the ftyp box up to bytesRead to determine if this is HEIF or AVIF. |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 24 | // Any valid ftyp box should have at least 8 bytes. |
| 25 | if (bytesRead < 8) { |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 26 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | uint32_t* ptr = (uint32_t*)buffer; |
| 30 | uint64_t chunkSize = SkEndian_SwapBE32(ptr[0]); |
| 31 | uint32_t chunkType = SkEndian_SwapBE32(ptr[1]); |
| 32 | |
| 33 | if (chunkType != FOURCC('f', 't', 'y', 'p')) { |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 34 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Mike Klein | 329d504 | 2017-10-20 13:48:55 -0400 | [diff] [blame] | 37 | int64_t offset = 8; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 38 | if (chunkSize == 1) { |
| 39 | // This indicates that the next 8 bytes represent the chunk size, |
| 40 | // and chunk data comes after that. |
| 41 | if (bytesRead < 16) { |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 42 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 43 | } |
| 44 | auto* chunkSizePtr = SkTAddOffset<const uint64_t>(buffer, offset); |
| 45 | chunkSize = SkEndian_SwapBE64(*chunkSizePtr); |
| 46 | if (chunkSize < 16) { |
| 47 | // The smallest valid chunk is 16 bytes long in this case. |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 48 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 49 | } |
| 50 | offset += 8; |
| 51 | } else if (chunkSize < 8) { |
| 52 | // The smallest valid chunk is 8 bytes long. |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 53 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | if (chunkSize > bytesRead) { |
| 57 | chunkSize = bytesRead; |
| 58 | } |
Mike Klein | 329d504 | 2017-10-20 13:48:55 -0400 | [diff] [blame] | 59 | int64_t chunkDataSize = chunkSize - offset; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 60 | // It should at least have major brand (4-byte) and minor version (4-bytes). |
| 61 | // The rest of the chunk (if any) is a list of (4-byte) compatible brands. |
| 62 | if (chunkDataSize < 8) { |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 63 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4; |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 67 | bool isHeif = false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 68 | for (size_t i = 0; i < numCompatibleBrands + 2; ++i) { |
| 69 | if (i == 1) { |
| 70 | // Skip this index, it refers to the minorVersion, |
| 71 | // not a brand. |
| 72 | continue; |
| 73 | } |
| 74 | auto* brandPtr = SkTAddOffset<const uint32_t>(buffer, offset + 4 * i); |
| 75 | uint32_t brand = SkEndian_SwapBE32(*brandPtr); |
Chong Zhang | 3c23593 | 2017-10-03 13:16:06 -0700 | [diff] [blame] | 76 | if (brand == FOURCC('m', 'i', 'f', '1') || brand == FOURCC('h', 'e', 'i', 'c') |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 77 | || brand == FOURCC('m', 's', 'f', '1') || brand == FOURCC('h', 'e', 'v', 'c') |
| 78 | || brand == FOURCC('a', 'v', 'i', 'f') || brand == FOURCC('a', 'v', 'i', 's')) { |
| 79 | // AVIF files could have "mif1" as the major brand. So we cannot |
| 80 | // distinguish whether the image is AVIF or HEIC just based on the |
| 81 | // "mif1" brand. So wait until we see a specific avif brand to |
| 82 | // determine whether it is AVIF or HEIC. |
| 83 | isHeif = true; |
| 84 | if (brand == FOURCC('a', 'v', 'i', 'f') |
| 85 | || brand == FOURCC('a', 'v', 'i', 's')) { |
| 86 | if (format != nullptr) { |
| 87 | *format = SkEncodedImageFormat::kAVIF; |
| 88 | } |
| 89 | return true; |
| 90 | } |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 91 | } |
| 92 | } |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 93 | if (isHeif) { |
| 94 | if (format != nullptr) { |
| 95 | *format = SkEncodedImageFormat::kHEIF; |
| 96 | } |
| 97 | return true; |
| 98 | } |
Brian Salomon | 8b5d44b | 2020-11-03 18:42:41 +0000 | [diff] [blame] | 99 | return false; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 102 | static SkEncodedOrigin get_orientation(const HeifFrameInfo& frameInfo) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 103 | switch (frameInfo.mRotationAngle) { |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 104 | case 0: return kTopLeft_SkEncodedOrigin; |
| 105 | case 90: return kRightTop_SkEncodedOrigin; |
| 106 | case 180: return kBottomRight_SkEncodedOrigin; |
| 107 | case 270: return kLeftBottom_SkEncodedOrigin; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 108 | } |
Mike Klein | 32a0a63 | 2017-10-19 08:33:12 -0400 | [diff] [blame] | 109 | return kDefault_SkEncodedOrigin; |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | struct SkHeifStreamWrapper : public HeifStream { |
| 113 | SkHeifStreamWrapper(SkStream* stream) : fStream(stream) {} |
| 114 | |
| 115 | ~SkHeifStreamWrapper() override {} |
| 116 | |
| 117 | size_t read(void* buffer, size_t size) override { |
| 118 | return fStream->read(buffer, size); |
| 119 | } |
| 120 | |
| 121 | bool rewind() override { |
| 122 | return fStream->rewind(); |
| 123 | } |
| 124 | |
| 125 | bool seek(size_t position) override { |
| 126 | return fStream->seek(position); |
| 127 | } |
| 128 | |
| 129 | bool hasLength() const override { |
| 130 | return fStream->hasLength(); |
| 131 | } |
| 132 | |
| 133 | size_t getLength() const override { |
| 134 | return fStream->getLength(); |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | std::unique_ptr<SkStream> fStream; |
| 139 | }; |
| 140 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 141 | static void releaseProc(const void* ptr, void* context) { |
| 142 | delete reinterpret_cast<std::vector<uint8_t>*>(context); |
| 143 | } |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 144 | |
| 145 | std::unique_ptr<SkCodec> SkHeifCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 146 | SkCodec::SelectionPolicy selectionPolicy, SkEncodedImageFormat format, Result* result) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 147 | std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder()); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 148 | if (heifDecoder == nullptr) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 149 | *result = kInternalError; |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 153 | HeifFrameInfo heifInfo; |
| 154 | if (!heifDecoder->init(new SkHeifStreamWrapper(stream.release()), &heifInfo)) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 155 | *result = kInvalidInput; |
| 156 | return nullptr; |
| 157 | } |
| 158 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 159 | size_t frameCount = 1; |
| 160 | if (selectionPolicy == SkCodec::SelectionPolicy::kPreferAnimation) { |
| 161 | HeifFrameInfo sequenceInfo; |
| 162 | if (heifDecoder->getSequenceInfo(&sequenceInfo, &frameCount) && |
| 163 | frameCount > 1) { |
| 164 | heifInfo = std::move(sequenceInfo); |
| 165 | } |
| 166 | } |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 167 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 168 | std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr; |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 169 | if (heifInfo.mIccData.size() > 0) { |
| 170 | auto iccData = new std::vector<uint8_t>(std::move(heifInfo.mIccData)); |
| 171 | auto icc = SkData::MakeWithProc(iccData->data(), iccData->size(), releaseProc, iccData); |
| 172 | profile = SkEncodedInfo::ICCProfile::Make(std::move(icc)); |
| 173 | } |
Leon Scroggins III | 5dd47e4 | 2018-09-27 15:26:48 -0400 | [diff] [blame] | 174 | if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) { |
| 175 | // This will result in sRGB. |
| 176 | profile = nullptr; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 177 | } |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 178 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 179 | SkEncodedInfo info = SkEncodedInfo::Make(heifInfo.mWidth, heifInfo.mHeight, |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 180 | SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8, std::move(profile)); |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 181 | SkEncodedOrigin orientation = get_orientation(heifInfo); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 182 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 183 | *result = kSuccess; |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 184 | return std::unique_ptr<SkCodec>(new SkHeifCodec( |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 185 | std::move(info), heifDecoder.release(), orientation, frameCount > 1, format)); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 186 | } |
| 187 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 188 | SkHeifCodec::SkHeifCodec( |
| 189 | SkEncodedInfo&& info, |
| 190 | HeifDecoder* heifDecoder, |
Leon Scroggins III | 2b39cbe | 2019-08-22 11:35:18 -0400 | [diff] [blame] | 191 | SkEncodedOrigin origin, |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 192 | bool useAnimation, |
| 193 | SkEncodedImageFormat format) |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 194 | : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin) |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 195 | , fHeifDecoder(heifDecoder) |
| 196 | , fSwizzleSrcRow(nullptr) |
| 197 | , fColorXformSrcRow(nullptr) |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 198 | , fUseAnimation(useAnimation) |
Vignesh Venkatasubramanian | eb7f960 | 2020-11-04 14:13:39 -0800 | [diff] [blame] | 199 | , fFormat(format) |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 200 | {} |
| 201 | |
Leon Scroggins III | 712476e | 2018-10-03 15:47:00 -0400 | [diff] [blame] | 202 | bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque, |
| 203 | bool needsColorXform) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 204 | SkASSERT(srcIsOpaque); |
| 205 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 206 | if (kUnknown_SkAlphaType == dstInfo.alphaType()) { |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | if (kOpaque_SkAlphaType != dstInfo.alphaType()) { |
| 211 | SkCodecPrintf("Warning: an opaque image should be decoded as opaque " |
| 212 | "- it is being decoded as non-opaque, which will draw slower\n"); |
| 213 | } |
| 214 | |
| 215 | switch (dstInfo.colorType()) { |
| 216 | case kRGBA_8888_SkColorType: |
| 217 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 218 | |
| 219 | case kBGRA_8888_SkColorType: |
| 220 | return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888); |
| 221 | |
| 222 | case kRGB_565_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 223 | if (needsColorXform) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 224 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 225 | } else { |
| 226 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565); |
| 227 | } |
| 228 | |
| 229 | case kRGBA_F16_SkColorType: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 230 | SkASSERT(needsColorXform); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 231 | return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888); |
| 232 | |
| 233 | default: |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, |
| 239 | const Options& opts) { |
| 240 | // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case, |
| 241 | // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer. |
| 242 | // We can never swizzle "in place" because the swizzler may perform sampling and/or |
| 243 | // subsetting. |
| 244 | // When fColorXformSrcRow is non-null, it means that we need to color xform and that |
| 245 | // we cannot color xform "in place" (many times we can, but not when the dst is F16). |
| 246 | // In this case, we will color xform from fColorXformSrcRow into the dst. |
| 247 | uint8_t* decodeDst = (uint8_t*) dst; |
| 248 | uint32_t* swizzleDst = (uint32_t*) dst; |
| 249 | size_t decodeDstRowBytes = rowBytes; |
| 250 | size_t swizzleDstRowBytes = rowBytes; |
| 251 | int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width(); |
| 252 | if (fSwizzleSrcRow && fColorXformSrcRow) { |
| 253 | decodeDst = fSwizzleSrcRow; |
| 254 | swizzleDst = fColorXformSrcRow; |
| 255 | decodeDstRowBytes = 0; |
| 256 | swizzleDstRowBytes = 0; |
| 257 | dstWidth = fSwizzler->swizzleWidth(); |
| 258 | } else if (fColorXformSrcRow) { |
| 259 | decodeDst = (uint8_t*) fColorXformSrcRow; |
| 260 | swizzleDst = fColorXformSrcRow; |
| 261 | decodeDstRowBytes = 0; |
| 262 | swizzleDstRowBytes = 0; |
| 263 | } else if (fSwizzleSrcRow) { |
| 264 | decodeDst = fSwizzleSrcRow; |
| 265 | decodeDstRowBytes = 0; |
| 266 | dstWidth = fSwizzler->swizzleWidth(); |
| 267 | } |
| 268 | |
| 269 | for (int y = 0; y < count; y++) { |
| 270 | if (!fHeifDecoder->getScanline(decodeDst)) { |
| 271 | return y; |
| 272 | } |
| 273 | |
| 274 | if (fSwizzler) { |
| 275 | fSwizzler->swizzle(swizzleDst, decodeDst); |
| 276 | } |
| 277 | |
| 278 | if (this->colorXform()) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 279 | this->applyColorXform(dst, swizzleDst, dstWidth); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 280 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 281 | } |
| 282 | |
| 283 | decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes); |
| 284 | swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes); |
| 285 | } |
| 286 | |
| 287 | return count; |
| 288 | } |
| 289 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 290 | int SkHeifCodec::onGetFrameCount() { |
| 291 | if (!fUseAnimation) { |
| 292 | return 1; |
| 293 | } |
| 294 | |
| 295 | if (fFrameHolder.size() == 0) { |
| 296 | size_t frameCount; |
| 297 | HeifFrameInfo frameInfo; |
| 298 | if (!fHeifDecoder->getSequenceInfo(&frameInfo, &frameCount) |
| 299 | || frameCount <= 1) { |
| 300 | fUseAnimation = false; |
| 301 | return 1; |
| 302 | } |
| 303 | fFrameHolder.reserve(frameCount); |
| 304 | for (size_t i = 0; i < frameCount; i++) { |
| 305 | Frame* frame = fFrameHolder.appendNewFrame(); |
| 306 | frame->setXYWH(0, 0, frameInfo.mWidth, frameInfo.mHeight); |
| 307 | frame->setDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep); |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 308 | // Currently we don't know the duration until the frame is actually |
| 309 | // decoded (onGetFrameInfo is also called before frame is decoded). |
| 310 | // For now, fill it base on the value reported for the sequence. |
| 311 | frame->setDuration(frameInfo.mDurationUs / 1000); |
| 312 | frame->setRequiredFrame(SkCodec::kNoFrame); |
| 313 | frame->setHasAlpha(false); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return fFrameHolder.size(); |
| 318 | } |
| 319 | |
| 320 | const SkFrame* SkHeifCodec::FrameHolder::onGetFrame(int i) const { |
| 321 | return static_cast<const SkFrame*>(this->frame(i)); |
| 322 | } |
| 323 | |
| 324 | SkHeifCodec::Frame* SkHeifCodec::FrameHolder::appendNewFrame() { |
| 325 | const int i = this->size(); |
| 326 | fFrames.emplace_back(i); // TODO: need to handle frame duration here |
| 327 | return &fFrames[i]; |
| 328 | } |
| 329 | |
| 330 | const SkHeifCodec::Frame* SkHeifCodec::FrameHolder::frame(int i) const { |
| 331 | SkASSERT(i >= 0 && i < this->size()); |
| 332 | return &fFrames[i]; |
| 333 | } |
| 334 | |
Chong Zhang | ea68eac | 2019-08-26 11:28:38 -0700 | [diff] [blame] | 335 | SkHeifCodec::Frame* SkHeifCodec::FrameHolder::editFrameAt(int i) { |
| 336 | SkASSERT(i >= 0 && i < this->size()); |
| 337 | return &fFrames[i]; |
| 338 | } |
| 339 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 340 | bool SkHeifCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const { |
| 341 | if (i >= fFrameHolder.size()) { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | const Frame* frame = fFrameHolder.frame(i); |
| 346 | if (!frame) { |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | if (frameInfo) { |
Leon Scroggins III | 469d67e | 2020-11-11 12:45:40 -0500 | [diff] [blame] | 351 | frame->fillIn(frameInfo, true); |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | int SkHeifCodec::onGetRepetitionCount() { |
| 358 | return kRepetitionCountInfinite; |
| 359 | } |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 360 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 361 | /* |
| 362 | * Performs the heif decode |
| 363 | */ |
| 364 | SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo, |
| 365 | void* dst, size_t dstRowBytes, |
| 366 | const Options& options, |
| 367 | int* rowsDecoded) { |
| 368 | if (options.fSubset) { |
| 369 | // Not supporting subsets on this path for now. |
| 370 | // TODO: if the heif has tiles, we can support subset here, but |
| 371 | // need to retrieve tile config from metadata retriever first. |
| 372 | return kUnimplemented; |
| 373 | } |
| 374 | |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 375 | bool success; |
| 376 | if (fUseAnimation) { |
| 377 | success = fHeifDecoder->decodeSequence(options.fFrameIndex, &fFrameInfo); |
Chong Zhang | ea68eac | 2019-08-26 11:28:38 -0700 | [diff] [blame] | 378 | fFrameHolder.editFrameAt(options.fFrameIndex)->setDuration( |
| 379 | fFrameInfo.mDurationUs / 1000); |
Leon Scroggins III | 6154ac4 | 2019-08-14 11:29:29 -0400 | [diff] [blame] | 380 | } else { |
| 381 | success = fHeifDecoder->decode(&fFrameInfo); |
| 382 | } |
| 383 | |
| 384 | if (!success) { |
| 385 | return kInvalidInput; |
| 386 | } |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 387 | |
Chong Zhang | 722b3a7 | 2017-08-23 11:17:36 -0700 | [diff] [blame] | 388 | fSwizzler.reset(nullptr); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 389 | this->allocateStorage(dstInfo); |
| 390 | |
| 391 | int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options); |
| 392 | if (rows < dstInfo.height()) { |
| 393 | *rowsDecoded = rows; |
| 394 | return kIncompleteInput; |
| 395 | } |
| 396 | |
| 397 | return kSuccess; |
| 398 | } |
| 399 | |
| 400 | void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) { |
| 401 | int dstWidth = dstInfo.width(); |
| 402 | |
| 403 | size_t swizzleBytes = 0; |
| 404 | if (fSwizzler) { |
| 405 | swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth; |
| 406 | dstWidth = fSwizzler->swizzleWidth(); |
| 407 | SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes)); |
| 408 | } |
| 409 | |
| 410 | size_t xformBytes = 0; |
| 411 | if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() || |
| 412 | kRGB_565_SkColorType == dstInfo.colorType())) { |
| 413 | xformBytes = dstWidth * sizeof(uint32_t); |
| 414 | } |
| 415 | |
| 416 | size_t totalBytes = swizzleBytes + xformBytes; |
| 417 | fStorage.reset(totalBytes); |
| 418 | if (totalBytes > 0) { |
| 419 | fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr; |
| 420 | fColorXformSrcRow = (xformBytes > 0) ? |
| 421 | SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void SkHeifCodec::initializeSwizzler( |
| 426 | const SkImageInfo& dstInfo, const Options& options) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 427 | SkImageInfo swizzlerDstInfo = dstInfo; |
| 428 | if (this->colorXform()) { |
| 429 | // The color xform will be expecting RGBA 8888 input. |
| 430 | swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType); |
| 431 | } |
| 432 | |
Leon Scroggins III | 65f4aea | 2018-10-24 12:17:22 -0400 | [diff] [blame] | 433 | int srcBPP = 4; |
| 434 | if (dstInfo.colorType() == kRGB_565_SkColorType && !this->colorXform()) { |
| 435 | srcBPP = 2; |
| 436 | } |
| 437 | |
| 438 | fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, options); |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 439 | SkASSERT(fSwizzler); |
| 440 | } |
| 441 | |
| 442 | SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) { |
| 443 | if (!createIfNecessary || fSwizzler) { |
| 444 | SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow)); |
| 445 | return fSwizzler.get(); |
| 446 | } |
| 447 | |
| 448 | this->initializeSwizzler(this->dstInfo(), this->options()); |
| 449 | this->allocateStorage(this->dstInfo()); |
| 450 | return fSwizzler.get(); |
| 451 | } |
| 452 | |
Leon Scroggins III | bcc7532 | 2019-04-15 10:34:53 -0400 | [diff] [blame] | 453 | bool SkHeifCodec::onRewind() { |
| 454 | fSwizzler.reset(nullptr); |
| 455 | fSwizzleSrcRow = nullptr; |
| 456 | fColorXformSrcRow = nullptr; |
| 457 | fStorage.reset(); |
| 458 | |
| 459 | return true; |
| 460 | } |
| 461 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 462 | SkCodec::Result SkHeifCodec::onStartScanlineDecode( |
| 463 | const SkImageInfo& dstInfo, const Options& options) { |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 464 | // TODO: For now, just decode the whole thing even when there is a subset. |
| 465 | // If the heif image has tiles, we could potentially do this much faster, |
| 466 | // but the tile configuration needs to be retrieved from the metadata. |
| 467 | if (!fHeifDecoder->decode(&fFrameInfo)) { |
| 468 | return kInvalidInput; |
| 469 | } |
| 470 | |
Chong Zhang | 722b3a7 | 2017-08-23 11:17:36 -0700 | [diff] [blame] | 471 | if (options.fSubset) { |
| 472 | this->initializeSwizzler(dstInfo, options); |
| 473 | } else { |
| 474 | fSwizzler.reset(nullptr); |
| 475 | } |
| 476 | |
Leon Scroggins III | 04be2b5 | 2017-08-17 15:13:20 -0400 | [diff] [blame] | 477 | this->allocateStorage(dstInfo); |
| 478 | |
| 479 | return kSuccess; |
| 480 | } |
| 481 | |
| 482 | int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) { |
| 483 | return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options()); |
| 484 | } |
| 485 | |
| 486 | bool SkHeifCodec::onSkipScanlines(int count) { |
| 487 | return count == (int) fHeifDecoder->skipScanlines(count); |
| 488 | } |
| 489 | |
| 490 | #endif // SK_HAS_HEIF_LIBRARY |