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 #include "src/codec/SkHeifCodec.h"
8
9 #include "include/codec/SkCodec.h"
10 #include "include/codec/SkEncodedImageFormat.h"
11 #include "include/core/SkStream.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/SkColorData.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "src/base/SkEndian.h"
16 #include "src/codec/SkCodecPriv.h"
17
18 #define FOURCC(c1, c2, c3, c4) \
19 ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4))
20
IsSupported(const void * buffer,size_t bytesRead,SkEncodedImageFormat * format)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.
24 // Any valid ftyp box should have at least 8 bytes.
25 if (bytesRead < 8) {
26 return false;
27 }
28
29 const uint32_t* ptr = (const 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 int64_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 int64_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 bool isHeif = false;
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);
76 if (brand == FOURCC('m', 'i', 'f', '1') || brand == FOURCC('h', 'e', 'i', 'c')
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 }
91 }
92 }
93 if (isHeif) {
94 if (format != nullptr) {
95 *format = SkEncodedImageFormat::kHEIF;
96 }
97 return true;
98 }
99 return false;
100 }
101
get_orientation(const HeifFrameInfo & frameInfo)102 static SkEncodedOrigin get_orientation(const HeifFrameInfo& frameInfo) {
103 switch (frameInfo.mRotationAngle) {
104 case 0: return kTopLeft_SkEncodedOrigin;
105 case 90: return kRightTop_SkEncodedOrigin;
106 case 180: return kBottomRight_SkEncodedOrigin;
107 case 270: return kLeftBottom_SkEncodedOrigin;
108 }
109 return kDefault_SkEncodedOrigin;
110 }
111
112 struct SkHeifStreamWrapper : public HeifStream {
SkHeifStreamWrapperSkHeifStreamWrapper113 SkHeifStreamWrapper(SkStream* stream) : fStream(stream) {}
114
~SkHeifStreamWrapperSkHeifStreamWrapper115 ~SkHeifStreamWrapper() override {}
116
readSkHeifStreamWrapper117 size_t read(void* buffer, size_t size) override {
118 return fStream->read(buffer, size);
119 }
120
rewindSkHeifStreamWrapper121 bool rewind() override {
122 return fStream->rewind();
123 }
124
seekSkHeifStreamWrapper125 bool seek(size_t position) override {
126 return fStream->seek(position);
127 }
128
hasLengthSkHeifStreamWrapper129 bool hasLength() const override {
130 return fStream->hasLength();
131 }
132
getLengthSkHeifStreamWrapper133 size_t getLength() const override {
134 return fStream->getLength();
135 }
136
137 private:
138 std::unique_ptr<SkStream> fStream;
139 };
140
releaseProc(const void * ptr,void * context)141 static void releaseProc(const void* ptr, void* context) {
142 delete reinterpret_cast<std::vector<uint8_t>*>(context);
143 }
144
MakeFromStream(std::unique_ptr<SkStream> stream,SkCodec::SelectionPolicy selectionPolicy,Result * result)145 std::unique_ptr<SkCodec> SkHeifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
146 SkCodec::SelectionPolicy selectionPolicy, Result* result) {
147 SkASSERT(result);
148 if (!stream) {
149 *result = SkCodec::kInvalidInput;
150 return nullptr;
151 }
152 std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder());
153 if (heifDecoder == nullptr) {
154 *result = SkCodec::kInternalError;
155 return nullptr;
156 }
157
158 constexpr size_t bytesToRead = MinBufferedBytesNeeded();
159 char buffer[bytesToRead];
160 size_t bytesRead = stream->peek(buffer, bytesToRead);
161 if (0 == bytesRead) {
162 // It is possible the stream does not support peeking, but does support rewinding.
163 // Attempt to read() and pass the actual amount read to the decoder.
164 bytesRead = stream->read(buffer, bytesToRead);
165 if (!stream->rewind()) {
166 SkCodecPrintf("Encoded image data could not peek or rewind to determine format!\n");
167 *result = kCouldNotRewind;
168 return nullptr;
169 }
170 }
171
172 SkEncodedImageFormat format;
173 if (!SkHeifCodec::IsSupported(buffer, bytesRead, &format)) {
174 SkCodecPrintf("Failed to get format despite earlier detecting it");
175 *result = SkCodec::kInternalError;
176 return nullptr;
177 }
178
179 HeifFrameInfo heifInfo;
180 if (!heifDecoder->init(new SkHeifStreamWrapper(stream.release()), &heifInfo)) {
181 *result = SkCodec::kInvalidInput;
182 return nullptr;
183 }
184
185 size_t frameCount = 1;
186 if (selectionPolicy == SkCodec::SelectionPolicy::kPreferAnimation) {
187 HeifFrameInfo sequenceInfo;
188 if (heifDecoder->getSequenceInfo(&sequenceInfo, &frameCount) &&
189 frameCount > 1) {
190 heifInfo = std::move(sequenceInfo);
191 }
192 }
193
194 std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
195 if (heifInfo.mIccData.size() > 0) {
196 auto iccData = new std::vector<uint8_t>(std::move(heifInfo.mIccData));
197 auto icc = SkData::MakeWithProc(iccData->data(), iccData->size(), releaseProc, iccData);
198 profile = SkEncodedInfo::ICCProfile::Make(std::move(icc));
199 }
200 if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
201 // This will result in sRGB.
202 profile = nullptr;
203 }
204
205 uint8_t colorDepth = heifDecoder->getColorDepth();
206
207 SkEncodedInfo info = SkEncodedInfo::Make(heifInfo.mWidth, heifInfo.mHeight,
208 SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha,
209 /*bitsPerComponent*/ 8, std::move(profile), colorDepth);
210 SkEncodedOrigin orientation = get_orientation(heifInfo);
211
212 *result = SkCodec::kSuccess;
213 return std::unique_ptr<SkCodec>(new SkHeifCodec(
214 std::move(info), heifDecoder.release(), orientation, frameCount > 1, format));
215 }
216
SkHeifCodec(SkEncodedInfo && info,HeifDecoder * heifDecoder,SkEncodedOrigin origin,bool useAnimation,SkEncodedImageFormat format)217 SkHeifCodec::SkHeifCodec(
218 SkEncodedInfo&& info,
219 HeifDecoder* heifDecoder,
220 SkEncodedOrigin origin,
221 bool useAnimation,
222 SkEncodedImageFormat format)
223 : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin)
224 , fHeifDecoder(heifDecoder)
225 , fSwizzleSrcRow(nullptr)
226 , fColorXformSrcRow(nullptr)
227 , fUseAnimation(useAnimation)
228 , fFormat(format)
229 {}
230
conversionSupported(const SkImageInfo & dstInfo,bool srcIsOpaque,bool needsColorXform)231 bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque,
232 bool needsColorXform) {
233 SkASSERT(srcIsOpaque);
234
235 if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
236 return false;
237 }
238
239 if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
240 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
241 "- it is being decoded as non-opaque, which will draw slower\n");
242 }
243
244 uint8_t colorDepth = fHeifDecoder->getColorDepth();
245 switch (dstInfo.colorType()) {
246 case kRGBA_8888_SkColorType:
247 this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
248 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
249
250 case kBGRA_8888_SkColorType:
251 this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
252 return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888);
253
254 case kRGB_565_SkColorType:
255 this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
256 if (needsColorXform) {
257 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
258 } else {
259 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565);
260 }
261
262 case kRGBA_1010102_SkColorType:
263 this->setSrcXformFormat(skcms_PixelFormat_RGBA_1010102);
264 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_1010102);
265
266 case kRGBA_F16_SkColorType:
267 SkASSERT(needsColorXform);
268 if (srcIsOpaque && colorDepth == 10) {
269 this->setSrcXformFormat(skcms_PixelFormat_RGBA_1010102);
270 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_1010102);
271 } else {
272 this->setSrcXformFormat(skcms_PixelFormat_RGBA_8888);
273 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
274 }
275
276 default:
277 return false;
278 }
279 }
280
readRows(const SkImageInfo & dstInfo,void * dst,size_t rowBytes,int count,const Options & opts)281 int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
282 const Options& opts) {
283 // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
284 // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
285 // We can never swizzle "in place" because the swizzler may perform sampling and/or
286 // subsetting.
287 // When fColorXformSrcRow is non-null, it means that we need to color xform and that
288 // we cannot color xform "in place" (many times we can, but not when the dst is F16).
289 // In this case, we will color xform from fColorXformSrcRow into the dst.
290 uint8_t* decodeDst = (uint8_t*) dst;
291 uint32_t* swizzleDst = (uint32_t*) dst;
292 size_t decodeDstRowBytes = rowBytes;
293 size_t swizzleDstRowBytes = rowBytes;
294 int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
295 if (fSwizzleSrcRow && fColorXformSrcRow) {
296 decodeDst = fSwizzleSrcRow;
297 swizzleDst = fColorXformSrcRow;
298 decodeDstRowBytes = 0;
299 swizzleDstRowBytes = 0;
300 dstWidth = fSwizzler->swizzleWidth();
301 } else if (fColorXformSrcRow) {
302 decodeDst = (uint8_t*) fColorXformSrcRow;
303 swizzleDst = fColorXformSrcRow;
304 decodeDstRowBytes = 0;
305 swizzleDstRowBytes = 0;
306 } else if (fSwizzleSrcRow) {
307 decodeDst = fSwizzleSrcRow;
308 decodeDstRowBytes = 0;
309 dstWidth = fSwizzler->swizzleWidth();
310 }
311
312 for (int y = 0; y < count; y++) {
313 if (!fHeifDecoder->getScanline(decodeDst)) {
314 return y;
315 }
316
317 if (fSwizzler) {
318 fSwizzler->swizzle(swizzleDst, decodeDst);
319 }
320
321 if (this->colorXform()) {
322 this->applyColorXform(dst, swizzleDst, dstWidth);
323 dst = SkTAddOffset<void>(dst, rowBytes);
324 }
325
326 decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes);
327 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
328 }
329
330 return count;
331 }
332
onGetFrameCount()333 int SkHeifCodec::onGetFrameCount() {
334 if (!fUseAnimation) {
335 return 1;
336 }
337
338 if (fFrameHolder.size() == 0) {
339 size_t frameCount;
340 HeifFrameInfo frameInfo;
341 if (!fHeifDecoder->getSequenceInfo(&frameInfo, &frameCount)
342 || frameCount <= 1) {
343 fUseAnimation = false;
344 return 1;
345 }
346 fFrameHolder.reserve(frameCount);
347 for (size_t i = 0; i < frameCount; i++) {
348 Frame* frame = fFrameHolder.appendNewFrame();
349 frame->setXYWH(0, 0, frameInfo.mWidth, frameInfo.mHeight);
350 frame->setDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep);
351 // Currently we don't know the duration until the frame is actually
352 // decoded (onGetFrameInfo is also called before frame is decoded).
353 // For now, fill it base on the value reported for the sequence.
354 frame->setDuration(frameInfo.mDurationUs / 1000);
355 frame->setRequiredFrame(SkCodec::kNoFrame);
356 frame->setHasAlpha(false);
357 }
358 }
359
360 return fFrameHolder.size();
361 }
362
onGetFrame(int i) const363 const SkFrame* SkHeifCodec::FrameHolder::onGetFrame(int i) const {
364 return static_cast<const SkFrame*>(this->frame(i));
365 }
366
appendNewFrame()367 SkHeifCodec::Frame* SkHeifCodec::FrameHolder::appendNewFrame() {
368 const int i = this->size();
369 fFrames.emplace_back(i); // TODO: need to handle frame duration here
370 return &fFrames[i];
371 }
372
frame(int i) const373 const SkHeifCodec::Frame* SkHeifCodec::FrameHolder::frame(int i) const {
374 SkASSERT(i >= 0 && i < this->size());
375 return &fFrames[i];
376 }
377
editFrameAt(int i)378 SkHeifCodec::Frame* SkHeifCodec::FrameHolder::editFrameAt(int i) {
379 SkASSERT(i >= 0 && i < this->size());
380 return &fFrames[i];
381 }
382
onGetFrameInfo(int i,FrameInfo * frameInfo) const383 bool SkHeifCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
384 if (i >= fFrameHolder.size()) {
385 return false;
386 }
387
388 const Frame* frame = fFrameHolder.frame(i);
389 if (!frame) {
390 return false;
391 }
392
393 if (frameInfo) {
394 frame->fillIn(frameInfo, true);
395 }
396
397 return true;
398 }
399
onGetRepetitionCount()400 int SkHeifCodec::onGetRepetitionCount() {
401 return kRepetitionCountInfinite;
402 }
403
404 /*
405 * Performs the heif decode
406 */
onGetPixels(const SkImageInfo & dstInfo,void * dst,size_t dstRowBytes,const Options & options,int * rowsDecoded)407 SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo,
408 void* dst, size_t dstRowBytes,
409 const Options& options,
410 int* rowsDecoded) {
411 if (options.fSubset) {
412 // Not supporting subsets on this path for now.
413 // TODO: if the heif has tiles, we can support subset here, but
414 // need to retrieve tile config from metadata retriever first.
415 return kUnimplemented;
416 }
417
418 bool success;
419 if (fUseAnimation) {
420 success = fHeifDecoder->decodeSequence(options.fFrameIndex, &fFrameInfo);
421 fFrameHolder.editFrameAt(options.fFrameIndex)->setDuration(
422 fFrameInfo.mDurationUs / 1000);
423 } else {
424 success = fHeifDecoder->decode(&fFrameInfo);
425 }
426
427 if (!success) {
428 return kInvalidInput;
429 }
430
431 fSwizzler.reset(nullptr);
432 this->allocateStorage(dstInfo);
433
434 int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
435 if (rows < dstInfo.height()) {
436 *rowsDecoded = rows;
437 return kIncompleteInput;
438 }
439
440 return kSuccess;
441 }
442
allocateStorage(const SkImageInfo & dstInfo)443 void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) {
444 int dstWidth = dstInfo.width();
445
446 size_t swizzleBytes = 0;
447 if (fSwizzler) {
448 swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth;
449 dstWidth = fSwizzler->swizzleWidth();
450 SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
451 }
452
453 size_t xformBytes = 0;
454 if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() ||
455 kRGB_565_SkColorType == dstInfo.colorType())) {
456 xformBytes = dstWidth * sizeof(uint32_t);
457 }
458
459 size_t totalBytes = swizzleBytes + xformBytes;
460 fStorage.reset(totalBytes);
461 if (totalBytes > 0) {
462 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
463 fColorXformSrcRow = (xformBytes > 0) ?
464 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
465 }
466 }
467
initializeSwizzler(const SkImageInfo & dstInfo,const Options & options)468 void SkHeifCodec::initializeSwizzler(
469 const SkImageInfo& dstInfo, const Options& options) {
470 SkImageInfo swizzlerDstInfo = dstInfo;
471 switch (this->getSrcXformFormat()) {
472 case skcms_PixelFormat_RGBA_8888:
473 swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
474 break;
475 case skcms_PixelFormat_RGBA_1010102:
476 swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_1010102_SkColorType);
477 break;
478 default:
479 SkASSERT(false);
480 }
481
482 int srcBPP = 4;
483 if (dstInfo.colorType() == kRGB_565_SkColorType && !this->colorXform()) {
484 srcBPP = 2;
485 }
486
487 fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, options);
488 SkASSERT(fSwizzler);
489 }
490
getSampler(bool createIfNecessary)491 SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) {
492 if (!createIfNecessary || fSwizzler) {
493 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
494 return fSwizzler.get();
495 }
496
497 this->initializeSwizzler(this->dstInfo(), this->options());
498 this->allocateStorage(this->dstInfo());
499 return fSwizzler.get();
500 }
501
onRewind()502 bool SkHeifCodec::onRewind() {
503 fSwizzler.reset(nullptr);
504 fSwizzleSrcRow = nullptr;
505 fColorXformSrcRow = nullptr;
506 fStorage.reset();
507
508 return true;
509 }
510
onStartScanlineDecode(const SkImageInfo & dstInfo,const Options & options)511 SkCodec::Result SkHeifCodec::onStartScanlineDecode(
512 const SkImageInfo& dstInfo, const Options& options) {
513 // TODO: For now, just decode the whole thing even when there is a subset.
514 // If the heif image has tiles, we could potentially do this much faster,
515 // but the tile configuration needs to be retrieved from the metadata.
516 if (!fHeifDecoder->decode(&fFrameInfo)) {
517 return kInvalidInput;
518 }
519
520 if (options.fSubset) {
521 this->initializeSwizzler(dstInfo, options);
522 } else {
523 fSwizzler.reset(nullptr);
524 }
525
526 this->allocateStorage(dstInfo);
527
528 return kSuccess;
529 }
530
onGetScanlines(void * dst,int count,size_t dstRowBytes)531 int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
532 return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options());
533 }
534
onSkipScanlines(int count)535 bool SkHeifCodec::onSkipScanlines(int count) {
536 return count == (int) fHeifDecoder->skipScanlines(count);
537 }
538
539 namespace SkHeifDecoder {
IsHeif(const void * data,size_t len)540 bool IsHeif(const void* data, size_t len) {
541 return SkHeifCodec::IsSupported(data, len, nullptr);
542 }
543
Decode(std::unique_ptr<SkStream> stream,SkCodec::Result * outResult,SkCodecs::DecodeContext ctx)544 std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream,
545 SkCodec::Result* outResult,
546 SkCodecs::DecodeContext ctx) {
547 SkASSERT(ctx);
548 SkCodec::Result resultStorage;
549 if (!outResult) {
550 outResult = &resultStorage;
551 }
552 auto policy = static_cast<SkCodec::SelectionPolicy*>(ctx);
553 return SkHeifCodec::MakeFromStream(std::move(stream), *policy, outResult);
554 }
555
Decode(sk_sp<SkData> data,SkCodec::Result * outResult,SkCodecs::DecodeContext ctx)556 std::unique_ptr<SkCodec> Decode(sk_sp<SkData> data,
557 SkCodec::Result* outResult,
558 SkCodecs::DecodeContext ctx) {
559 if (!data) {
560 if (outResult) {
561 *outResult = SkCodec::kInvalidInput;
562 }
563 return nullptr;
564 }
565 return Decode(SkMemoryStream::Make(std::move(data)), outResult, ctx);
566 }
567 } // namespace SkHeifDecoder
568
569