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