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 "include/core/SkTypes.h"
9
10#ifdef SK_HAS_HEIF_LIBRARY
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"
17
18#define FOURCC(c1, c2, c3, c4) \
19 ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4))
20
21bool 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
36 int64_t offset = 8;
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 }
58 int64_t chunkDataSize = chunkSize - offset;
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);
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')) {
76 return true;
77 }
78 }
79 return false;
80}
81
82static SkEncodedOrigin get_orientation(const HeifFrameInfo& frameInfo) {
83 switch (frameInfo.mRotationAngle) {
84 case 0: return kTopLeft_SkEncodedOrigin;
85 case 90: return kRightTop_SkEncodedOrigin;
86 case 180: return kBottomRight_SkEncodedOrigin;
87 case 270: return kLeftBottom_SkEncodedOrigin;
88 }
89 return kDefault_SkEncodedOrigin;
90}
91
92struct 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
117private:
118 std::unique_ptr<SkStream> fStream;
119};
120
121static void releaseProc(const void* ptr, void* context) {
122 delete reinterpret_cast<std::vector<uint8_t>*>(context);
123}
124
125std::unique_ptr<SkCodec> SkHeifCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
126 SkCodec::SelectionPolicy selectionPolicy, Result* result) {
127 std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder());
128 if (heifDecoder.get() == nullptr) {
129 *result = kInternalError;
130 return nullptr;
131 }
132
133 HeifFrameInfo heifInfo;
134 if (!heifDecoder->init(new SkHeifStreamWrapper(stream.release()), &heifInfo)) {
135 *result = kInvalidInput;
136 return nullptr;
137 }
138
139 size_t frameCount = 1;
140 if (selectionPolicy == SkCodec::SelectionPolicy::kPreferAnimation) {
141 HeifFrameInfo sequenceInfo;
142 if (heifDecoder->getSequenceInfo(&sequenceInfo, &frameCount) &&
143 frameCount > 1) {
144 heifInfo = std::move(sequenceInfo);
145 }
146 }
147
148 std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
149 if (heifInfo.mIccData.size() > 0) {
150 auto iccData = new std::vector<uint8_t>(std::move(heifInfo.mIccData));
151 auto icc = SkData::MakeWithProc(iccData->data(), iccData->size(), releaseProc, iccData);
152 profile = SkEncodedInfo::ICCProfile::Make(std::move(icc));
153 }
154 if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
155 // This will result in sRGB.
156 profile = nullptr;
157 }
158
159 SkEncodedInfo info = SkEncodedInfo::Make(heifInfo.mWidth, heifInfo.mHeight,
160 SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8, std::move(profile));
161 SkEncodedOrigin orientation = get_orientation(heifInfo);
162
163 *result = kSuccess;
164 return std::unique_ptr<SkCodec>(new SkHeifCodec(
165 std::move(info), heifDecoder.release(), orientation, frameCount > 1));
166}
167
168SkHeifCodec::SkHeifCodec(
169 SkEncodedInfo&& info,
170 HeifDecoder* heifDecoder,
171 SkEncodedOrigin origin,
172 bool useAnimation)
173 : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin)
174 , fHeifDecoder(heifDecoder)
175 , fSwizzleSrcRow(nullptr)
176 , fColorXformSrcRow(nullptr)
177 , fUseAnimation(useAnimation)
178{}
179
180bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque,
181 bool needsColorXform) {
182 SkASSERT(srcIsOpaque);
183
184 if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
185 return false;
186 }
187
188 if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
189 SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
190 "- it is being decoded as non-opaque, which will draw slower\n");
191 }
192
193 switch (dstInfo.colorType()) {
194 case kRGBA_8888_SkColorType:
195 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
196
197 case kBGRA_8888_SkColorType:
198 return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888);
199
200 case kRGB_565_SkColorType:
201 if (needsColorXform) {
202 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
203 } else {
204 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565);
205 }
206
207 case kRGBA_F16_SkColorType:
208 SkASSERT(needsColorXform);
209 return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
210
211 default:
212 return false;
213 }
214}
215
216int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
217 const Options& opts) {
218 // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
219 // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
220 // We can never swizzle "in place" because the swizzler may perform sampling and/or
221 // subsetting.
222 // When fColorXformSrcRow is non-null, it means that we need to color xform and that
223 // we cannot color xform "in place" (many times we can, but not when the dst is F16).
224 // In this case, we will color xform from fColorXformSrcRow into the dst.
225 uint8_t* decodeDst = (uint8_t*) dst;
226 uint32_t* swizzleDst = (uint32_t*) dst;
227 size_t decodeDstRowBytes = rowBytes;
228 size_t swizzleDstRowBytes = rowBytes;
229 int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
230 if (fSwizzleSrcRow && fColorXformSrcRow) {
231 decodeDst = fSwizzleSrcRow;
232 swizzleDst = fColorXformSrcRow;
233 decodeDstRowBytes = 0;
234 swizzleDstRowBytes = 0;
235 dstWidth = fSwizzler->swizzleWidth();
236 } else if (fColorXformSrcRow) {
237 decodeDst = (uint8_t*) fColorXformSrcRow;
238 swizzleDst = fColorXformSrcRow;
239 decodeDstRowBytes = 0;
240 swizzleDstRowBytes = 0;
241 } else if (fSwizzleSrcRow) {
242 decodeDst = fSwizzleSrcRow;
243 decodeDstRowBytes = 0;
244 dstWidth = fSwizzler->swizzleWidth();
245 }
246
247 for (int y = 0; y < count; y++) {
248 if (!fHeifDecoder->getScanline(decodeDst)) {
249 return y;
250 }
251
252 if (fSwizzler) {
253 fSwizzler->swizzle(swizzleDst, decodeDst);
254 }
255
256 if (this->colorXform()) {
257 this->applyColorXform(dst, swizzleDst, dstWidth);
258 dst = SkTAddOffset<void>(dst, rowBytes);
259 }
260
261 decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes);
262 swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
263 }
264
265 return count;
266}
267
268int SkHeifCodec::onGetFrameCount() {
269 if (!fUseAnimation) {
270 return 1;
271 }
272
273 if (fFrameHolder.size() == 0) {
274 size_t frameCount;
275 HeifFrameInfo frameInfo;
276 if (!fHeifDecoder->getSequenceInfo(&frameInfo, &frameCount)
277 || frameCount <= 1) {
278 fUseAnimation = false;
279 return 1;
280 }
281 fFrameHolder.reserve(frameCount);
282 for (size_t i = 0; i < frameCount; i++) {
283 Frame* frame = fFrameHolder.appendNewFrame();
284 frame->setXYWH(0, 0, frameInfo.mWidth, frameInfo.mHeight);
285 frame->setDisposalMethod(SkCodecAnimation::DisposalMethod::kKeep);
286 // Currently we don't know the duration until the frame is actually
287 // decoded (onGetFrameInfo is also called before frame is decoded).
288 // For now, fill it base on the value reported for the sequence.
289 frame->setDuration(frameInfo.mDurationUs / 1000);
290 frame->setRequiredFrame(SkCodec::kNoFrame);
291 frame->setHasAlpha(false);
292 }
293 }
294
295 return fFrameHolder.size();
296}
297
298const SkFrame* SkHeifCodec::FrameHolder::onGetFrame(int i) const {
299 return static_cast<const SkFrame*>(this->frame(i));
300}
301
302SkHeifCodec::Frame* SkHeifCodec::FrameHolder::appendNewFrame() {
303 const int i = this->size();
304 fFrames.emplace_back(i); // TODO: need to handle frame duration here
305 return &fFrames[i];
306}
307
308const SkHeifCodec::Frame* SkHeifCodec::FrameHolder::frame(int i) const {
309 SkASSERT(i >= 0 && i < this->size());
310 return &fFrames[i];
311}
312
313SkHeifCodec::Frame* SkHeifCodec::FrameHolder::editFrameAt(int i) {
314 SkASSERT(i >= 0 && i < this->size());
315 return &fFrames[i];
316}
317
318bool SkHeifCodec::onGetFrameInfo(int i, FrameInfo* frameInfo) const {
319 if (i >= fFrameHolder.size()) {
320 return false;
321 }
322
323 const Frame* frame = fFrameHolder.frame(i);
324 if (!frame) {
325 return false;
326 }
327
328 if (frameInfo) {
329 frameInfo->fRequiredFrame = SkCodec::kNoFrame;
330 frameInfo->fDuration = frame->getDuration();
331 frameInfo->fFullyReceived = true;
332 frameInfo->fAlphaType = kOpaque_SkAlphaType;
333 frameInfo->fDisposalMethod = SkCodecAnimation::DisposalMethod::kKeep;
334 }
335
336 return true;
337}
338
339int SkHeifCodec::onGetRepetitionCount() {
340 return kRepetitionCountInfinite;
341}
342
343/*
344 * Performs the heif decode
345 */
346SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo,
347 void* dst, size_t dstRowBytes,
348 const Options& options,
349 int* rowsDecoded) {
350 if (options.fSubset) {
351 // Not supporting subsets on this path for now.
352 // TODO: if the heif has tiles, we can support subset here, but
353 // need to retrieve tile config from metadata retriever first.
354 return kUnimplemented;
355 }
356
357 bool success;
358 if (fUseAnimation) {
359 success = fHeifDecoder->decodeSequence(options.fFrameIndex, &fFrameInfo);
360 fFrameHolder.editFrameAt(options.fFrameIndex)->setDuration(
361 fFrameInfo.mDurationUs / 1000);
362 } else {
363 success = fHeifDecoder->decode(&fFrameInfo);
364 }
365
366 if (!success) {
367 return kInvalidInput;
368 }
369
370 fSwizzler.reset(nullptr);
371 this->allocateStorage(dstInfo);
372
373 int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
374 if (rows < dstInfo.height()) {
375 *rowsDecoded = rows;
376 return kIncompleteInput;
377 }
378
379 return kSuccess;
380}
381
382void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) {
383 int dstWidth = dstInfo.width();
384
385 size_t swizzleBytes = 0;
386 if (fSwizzler) {
387 swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth;
388 dstWidth = fSwizzler->swizzleWidth();
389 SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
390 }
391
392 size_t xformBytes = 0;
393 if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() ||
394 kRGB_565_SkColorType == dstInfo.colorType())) {
395 xformBytes = dstWidth * sizeof(uint32_t);
396 }
397
398 size_t totalBytes = swizzleBytes + xformBytes;
399 fStorage.reset(totalBytes);
400 if (totalBytes > 0) {
401 fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
402 fColorXformSrcRow = (xformBytes > 0) ?
403 SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
404 }
405}
406
407void SkHeifCodec::initializeSwizzler(
408 const SkImageInfo& dstInfo, const Options& options) {
409 SkImageInfo swizzlerDstInfo = dstInfo;
410 if (this->colorXform()) {
411 // The color xform will be expecting RGBA 8888 input.
412 swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
413 }
414
415 int srcBPP = 4;
416 if (dstInfo.colorType() == kRGB_565_SkColorType && !this->colorXform()) {
417 srcBPP = 2;
418 }
419
420 fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, options);
421 SkASSERT(fSwizzler);
422}
423
424SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) {
425 if (!createIfNecessary || fSwizzler) {
426 SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
427 return fSwizzler.get();
428 }
429
430 this->initializeSwizzler(this->dstInfo(), this->options());
431 this->allocateStorage(this->dstInfo());
432 return fSwizzler.get();
433}
434
435bool SkHeifCodec::onRewind() {
436 fSwizzler.reset(nullptr);
437 fSwizzleSrcRow = nullptr;
438 fColorXformSrcRow = nullptr;
439 fStorage.reset();
440
441 return true;
442}
443
444SkCodec::Result SkHeifCodec::onStartScanlineDecode(
445 const SkImageInfo& dstInfo, const Options& options) {
446 // TODO: For now, just decode the whole thing even when there is a subset.
447 // If the heif image has tiles, we could potentially do this much faster,
448 // but the tile configuration needs to be retrieved from the metadata.
449 if (!fHeifDecoder->decode(&fFrameInfo)) {
450 return kInvalidInput;
451 }
452
453 if (options.fSubset) {
454 this->initializeSwizzler(dstInfo, options);
455 } else {
456 fSwizzler.reset(nullptr);
457 }
458
459 this->allocateStorage(dstInfo);
460
461 return kSuccess;
462}
463
464int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
465 return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options());
466}
467
468bool SkHeifCodec::onSkipScanlines(int count) {
469 return count == (int) fHeifDecoder->skipScanlines(count);
470}
471
472#endif // SK_HAS_HEIF_LIBRARY
473