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"
13 #include "SkColorPriv.h"
14 #include "SkColorSpace_Base.h"
15 #include "SkEndian.h"
16 #include "SkStream.h"
17 #include "SkHeifCodec.h"
18
19 #define FOURCC(c1, c2, c3, c4) \
20 ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4))
21
IsHeif(const void * buffer,size_t bytesRead)22 bool SkHeifCodec::IsHeif(const void* buffer, size_t bytesRead) {
23 // Parse the ftyp box up to bytesRead to determine if this is HEIF.
24 // Any valid ftyp box should have at least 8 bytes.
25 if (bytesRead < 8) {
26 return false;
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')) {
34 return false;
35 }
36
37 off64_t offset = 8;
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) {
42 return false;
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.
48 return false;
49 }
50 offset += 8;
51 } else if (chunkSize < 8) {
52 // The smallest valid chunk is 8 bytes long.
53 return false;
54 }
55
56 if (chunkSize > bytesRead) {
57 chunkSize = bytesRead;
58 }
59 off64_t chunkDataSize = chunkSize - offset;
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) {
63 return false;
64 }
65
66 uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4;
67 for (size_t i = 0; i < numCompatibleBrands + 2; ++i) {
68 if (i == 1) {
69 // Skip this index, it refers to the minorVersion,
70 // not a brand.
71 continue;
72 }
73 auto* brandPtr = SkTAddOffset<const uint32_t>(buffer, offset + 4 * i);
74 uint32_t brand = SkEndian_SwapBE32(*brandPtr);
75 if (brand == FOURCC('m', 'i', 'f', '1') || brand == FOURCC('h', 'e', 'i', 'c')) {
76 return true;
77 }
78 }
79 return false;
80 }
81
get_orientation(const HeifFrameInfo & frameInfo)82 static SkCodec::Origin get_orientation(const HeifFrameInfo& frameInfo) {
83 switch (frameInfo.mRotationAngle) {
84 case 0: return SkCodec::kTopLeft_Origin;
85 case 90: return SkCodec::kRightTop_Origin;
86 case 180: return SkCodec::kBottomRight_Origin;
87 case 270: return SkCodec::kLeftBottom_Origin;
88 }
89 return SkCodec::kDefault_Origin;
90 }
91
92 struct SkHeifStreamWrapper : public HeifStream {
SkHeifStreamWrapperSkHeifStreamWrapper93 SkHeifStreamWrapper(SkStream* stream) : fStream(stream) {}
94
~SkHeifStreamWrapperSkHeifStreamWrapper95 ~SkHeifStreamWrapper() override {}
96
readSkHeifStreamWrapper97 size_t read(void* buffer, size_t size) override {
98 return fStream->read(buffer, size);
99 }
100
rewindSkHeifStreamWrapper101 bool rewind() override {
102 return fStream->rewind();
103 }
104
seekSkHeifStreamWrapper105 bool seek(size_t position) override {
106 return fStream->seek(position);
107 }
108
hasLengthSkHeifStreamWrapper109 bool hasLength() const override {
110 return fStream->hasLength();
111 }
112
getLengthSkHeifStreamWrapper113 size_t getLength() const override {
114 return fStream->getLength();
115 }
116
117 private:
118 std::unique_ptr<SkStream> fStream;
119 };
120
NewFromStream(SkStream * stream,Result * result)121 SkCodec* SkHeifCodec::NewFromStream(SkStream* stream, Result* result) {
122 std::unique_ptr<SkStream> streamDeleter(stream);
123
124 std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder());
125 if (heifDecoder.get() == nullptr) {
126 *result = kInternalError;
127 return nullptr;
128 }
129
130 HeifFrameInfo frameInfo;
131 if (!heifDecoder->init(new SkHeifStreamWrapper(streamDeleter.release()),
132 &frameInfo)) {
133 *result = kInvalidInput;
134 return nullptr;
135 }
136
137 SkEncodedInfo info = SkEncodedInfo::Make(
138 SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8);
139
140 Origin orientation = get_orientation(frameInfo);
141
142 sk_sp<SkColorSpace> colorSpace = nullptr;
143 if ((frameInfo.mIccSize > 0) && (frameInfo.mIccData != nullptr)) {
144 SkColorSpace_Base::ICCTypeFlag iccType = SkColorSpace_Base::kRGB_ICCTypeFlag;
145 colorSpace = SkColorSpace_Base::MakeICC(
146 frameInfo.mIccData.get(), frameInfo.mIccSize, iccType);
147 }
148 if (!colorSpace) {
149 colorSpace = SkColorSpace::MakeSRGB();
150 }
151
152 *result = kSuccess;
153 return new SkHeifCodec(frameInfo.mWidth, frameInfo.mHeight,
154 info, heifDecoder.release(), std::move(colorSpace), orientation);
155 }
156
SkHeifCodec(int width,int height,const SkEncodedInfo & info,HeifDecoder * heifDecoder,sk_sp<SkColorSpace> colorSpace,Origin origin)157 SkHeifCodec::SkHeifCodec(int width, int height, const SkEncodedInfo& info,
158 HeifDecoder* heifDecoder, sk_sp<SkColorSpace> colorSpace, Origin origin)
159 : INHERITED(width, height, info, SkColorSpaceXform::kRGBA_8888_ColorFormat,
160 nullptr, std::move(colorSpace), origin)
161 , fHeifDecoder(heifDecoder)
162 , fSwizzleSrcRow(nullptr)
163 , fColorXformSrcRow(nullptr)
164 {}
165
166 /*
167 * Checks if the conversion between the input image and the requested output
168 * image has been implemented
169 * Sets the output color format
170 */
setOutputColorFormat(const SkImageInfo & dstInfo)171 bool SkHeifCodec::setOutputColorFormat(const SkImageInfo& dstInfo) {
172 if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
173 return false;
174 }
175
176 if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
177 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
178 "- it is being decoded as non-opaque, which will draw slower\n");
179 }
180
181 switch (dstInfo.colorType()) {
182 case kRGBA_8888_SkColorType:
183 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
184
185 case kBGRA_8888_SkColorType:
186 return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888);
187
188 case kRGB_565_SkColorType:
189 if (this->colorXform()) {
190 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
191 } else {
192 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565);
193 }
194
195 case kRGBA_F16_SkColorType:
196 SkASSERT(this->colorXform());
197
198 if (!dstInfo.colorSpace()->gammaIsLinear()) {
199 return false;
200 }
201 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
202
203 default:
204 return false;
205 }
206 }
207
readRows(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,int count,const Options & opts)208 int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
209 const Options& opts) {
210 // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
211 // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
212 // We can never swizzle "in place" because the swizzler may perform sampling and/or
213 // subsetting.
214 // When fColorXformSrcRow is non-null, it means that we need to color xform and that
215 // we cannot color xform "in place" (many times we can, but not when the dst is F16).
216 // In this case, we will color xform from fColorXformSrcRow into the dst.
217 uint8_t* decodeDst = (uint8_t*) dst;
218 uint32_t* swizzleDst = (uint32_t*) dst;
219 size_t decodeDstRowBytes = rowBytes;
220 size_t swizzleDstRowBytes = rowBytes;
221 int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
222 if (fSwizzleSrcRow && fColorXformSrcRow) {
223 decodeDst = fSwizzleSrcRow;
224 swizzleDst = fColorXformSrcRow;
225 decodeDstRowBytes = 0;
226 swizzleDstRowBytes = 0;
227 dstWidth = fSwizzler->swizzleWidth();
228 } else if (fColorXformSrcRow) {
229 decodeDst = (uint8_t*) fColorXformSrcRow;
230 swizzleDst = fColorXformSrcRow;
231 decodeDstRowBytes = 0;
232 swizzleDstRowBytes = 0;
233 } else if (fSwizzleSrcRow) {
234 decodeDst = fSwizzleSrcRow;
235 decodeDstRowBytes = 0;
236 dstWidth = fSwizzler->swizzleWidth();
237 }
238
239 for (int y = 0; y < count; y++) {
240 if (!fHeifDecoder->getScanline(decodeDst)) {
241 return y;
242 }
243
244 if (fSwizzler) {
245 fSwizzler->swizzle(swizzleDst, decodeDst);
246 }
247
248 if (this->colorXform()) {
249 this->applyColorXform(dst, swizzleDst, dstWidth, kOpaque_SkAlphaType);
250 dst = SkTAddOffset<void>(dst, rowBytes);
251 }
252
253 decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes);
254 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
255 }
256
257 return count;
258 }
259
260 /*
261 * Performs the heif decode
262 */
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t dstRowBytes,const Options & options,int * rowsDecoded)263 SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo,
264 void* dst, size_t dstRowBytes,
265 const Options& options,
266 int* rowsDecoded) {
267 if (options.fSubset) {
268 // Not supporting subsets on this path for now.
269 // TODO: if the heif has tiles, we can support subset here, but
270 // need to retrieve tile config from metadata retriever first.
271 return kUnimplemented;
272 }
273
274 if (!this->initializeColorXform(dstInfo, options.fPremulBehavior)) {
275 return kInvalidConversion;
276 }
277
278 // Check if we can decode to the requested destination and set the output color space
279 if (!this->setOutputColorFormat(dstInfo)) {
280 return kInvalidConversion;
281 }
282
283 if (!fHeifDecoder->decode(&fFrameInfo)) {
284 return kInvalidInput;
285 }
286
287 fSwizzler.reset(nullptr);
288 this->allocateStorage(dstInfo);
289
290 int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
291 if (rows < dstInfo.height()) {
292 *rowsDecoded = rows;
293 return kIncompleteInput;
294 }
295
296 return kSuccess;
297 }
298
allocateStorage(const SkImageInfo & dstInfo)299 void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) {
300 int dstWidth = dstInfo.width();
301
302 size_t swizzleBytes = 0;
303 if (fSwizzler) {
304 swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth;
305 dstWidth = fSwizzler->swizzleWidth();
306 SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
307 }
308
309 size_t xformBytes = 0;
310 if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() ||
311 kRGB_565_SkColorType == dstInfo.colorType())) {
312 xformBytes = dstWidth * sizeof(uint32_t);
313 }
314
315 size_t totalBytes = swizzleBytes + xformBytes;
316 fStorage.reset(totalBytes);
317 if (totalBytes > 0) {
318 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
319 fColorXformSrcRow = (xformBytes > 0) ?
320 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
321 }
322 }
323
initializeSwizzler(const SkImageInfo & dstInfo,const Options & options)324 void SkHeifCodec::initializeSwizzler(
325 const SkImageInfo& dstInfo, const Options& options) {
326 SkImageInfo swizzlerDstInfo = dstInfo;
327 if (this->colorXform()) {
328 // The color xform will be expecting RGBA 8888 input.
329 swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
330 }
331
332 int srcBPP = 4;
333 if (dstInfo.colorType() == kRGB_565_SkColorType && !this->colorXform()) {
334 srcBPP = 2;
335 }
336
337 fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, options);
338 SkASSERT(fSwizzler);
339 }
340
getSampler(bool createIfNecessary)341 SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) {
342 if (!createIfNecessary || fSwizzler) {
343 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
344 return fSwizzler.get();
345 }
346
347 this->initializeSwizzler(this->dstInfo(), this->options());
348 this->allocateStorage(this->dstInfo());
349 return fSwizzler.get();
350 }
351
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)352 SkCodec::Result SkHeifCodec::onStartScanlineDecode(
353 const SkImageInfo& dstInfo, const Options& options) {
354 if (!this->initializeColorXform(dstInfo, options.fPremulBehavior)) {
355 return kInvalidConversion;
356 }
357
358 // Check if we can decode to the requested destination and set the output color space
359 if (!this->setOutputColorFormat(dstInfo)) {
360 return kInvalidConversion;
361 }
362
363 // TODO: For now, just decode the whole thing even when there is a subset.
364 // If the heif image has tiles, we could potentially do this much faster,
365 // but the tile configuration needs to be retrieved from the metadata.
366 if (!fHeifDecoder->decode(&fFrameInfo)) {
367 return kInvalidInput;
368 }
369
370 if (options.fSubset) {
371 this->initializeSwizzler(dstInfo, options);
372 } else {
373 fSwizzler.reset(nullptr);
374 }
375
376 this->allocateStorage(dstInfo);
377
378 return kSuccess;
379 }
380
onGetScanlines(void * dst,int count,size_t dstRowBytes)381 int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
382 return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options());
383 }
384
onSkipScanlines(int count)385 bool SkHeifCodec::onSkipScanlines(int count) {
386 return count == (int) fHeifDecoder->skipScanlines(count);
387 }
388
389 #endif // SK_HAS_HEIF_LIBRARY
390