SkHeifCodec.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "include/core/SkTypes.h"
  8. #ifdef SK_HAS_HEIF_LIBRARY
  9. #include "include/codec/SkCodec.h"
  10. #include "include/core/SkStream.h"
  11. #include "include/private/SkColorData.h"
  12. #include "src/codec/SkCodecPriv.h"
  13. #include "src/codec/SkHeifCodec.h"
  14. #include "src/core/SkEndian.h"
  15. #define FOURCC(c1, c2, c3, c4) \
  16. ((c1) << 24 | (c2) << 16 | (c3) << 8 | (c4))
  17. bool SkHeifCodec::IsHeif(const void* buffer, size_t bytesRead) {
  18. // Parse the ftyp box up to bytesRead to determine if this is HEIF.
  19. // Any valid ftyp box should have at least 8 bytes.
  20. if (bytesRead < 8) {
  21. return false;
  22. }
  23. uint32_t* ptr = (uint32_t*)buffer;
  24. uint64_t chunkSize = SkEndian_SwapBE32(ptr[0]);
  25. uint32_t chunkType = SkEndian_SwapBE32(ptr[1]);
  26. if (chunkType != FOURCC('f', 't', 'y', 'p')) {
  27. return false;
  28. }
  29. int64_t offset = 8;
  30. if (chunkSize == 1) {
  31. // This indicates that the next 8 bytes represent the chunk size,
  32. // and chunk data comes after that.
  33. if (bytesRead < 16) {
  34. return false;
  35. }
  36. auto* chunkSizePtr = SkTAddOffset<const uint64_t>(buffer, offset);
  37. chunkSize = SkEndian_SwapBE64(*chunkSizePtr);
  38. if (chunkSize < 16) {
  39. // The smallest valid chunk is 16 bytes long in this case.
  40. return false;
  41. }
  42. offset += 8;
  43. } else if (chunkSize < 8) {
  44. // The smallest valid chunk is 8 bytes long.
  45. return false;
  46. }
  47. if (chunkSize > bytesRead) {
  48. chunkSize = bytesRead;
  49. }
  50. int64_t chunkDataSize = chunkSize - offset;
  51. // It should at least have major brand (4-byte) and minor version (4-bytes).
  52. // The rest of the chunk (if any) is a list of (4-byte) compatible brands.
  53. if (chunkDataSize < 8) {
  54. return false;
  55. }
  56. uint32_t numCompatibleBrands = (chunkDataSize - 8) / 4;
  57. for (size_t i = 0; i < numCompatibleBrands + 2; ++i) {
  58. if (i == 1) {
  59. // Skip this index, it refers to the minorVersion,
  60. // not a brand.
  61. continue;
  62. }
  63. auto* brandPtr = SkTAddOffset<const uint32_t>(buffer, offset + 4 * i);
  64. uint32_t brand = SkEndian_SwapBE32(*brandPtr);
  65. if (brand == FOURCC('m', 'i', 'f', '1') || brand == FOURCC('h', 'e', 'i', 'c')
  66. || brand == FOURCC('m', 's', 'f', '1') || brand == FOURCC('h', 'e', 'v', 'c')) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. static SkEncodedOrigin get_orientation(const HeifFrameInfo& frameInfo) {
  73. switch (frameInfo.mRotationAngle) {
  74. case 0: return kTopLeft_SkEncodedOrigin;
  75. case 90: return kRightTop_SkEncodedOrigin;
  76. case 180: return kBottomRight_SkEncodedOrigin;
  77. case 270: return kLeftBottom_SkEncodedOrigin;
  78. }
  79. return kDefault_SkEncodedOrigin;
  80. }
  81. struct SkHeifStreamWrapper : public HeifStream {
  82. SkHeifStreamWrapper(SkStream* stream) : fStream(stream) {}
  83. ~SkHeifStreamWrapper() override {}
  84. size_t read(void* buffer, size_t size) override {
  85. return fStream->read(buffer, size);
  86. }
  87. bool rewind() override {
  88. return fStream->rewind();
  89. }
  90. bool seek(size_t position) override {
  91. return fStream->seek(position);
  92. }
  93. bool hasLength() const override {
  94. return fStream->hasLength();
  95. }
  96. size_t getLength() const override {
  97. return fStream->getLength();
  98. }
  99. private:
  100. std::unique_ptr<SkStream> fStream;
  101. };
  102. std::unique_ptr<SkCodec> SkHeifCodec::MakeFromStream(
  103. std::unique_ptr<SkStream> stream, Result* result) {
  104. std::unique_ptr<HeifDecoder> heifDecoder(createHeifDecoder());
  105. if (heifDecoder.get() == nullptr) {
  106. *result = kInternalError;
  107. return nullptr;
  108. }
  109. HeifFrameInfo frameInfo;
  110. if (!heifDecoder->init(new SkHeifStreamWrapper(stream.release()),
  111. &frameInfo)) {
  112. *result = kInvalidInput;
  113. return nullptr;
  114. }
  115. std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
  116. if ((frameInfo.mIccSize > 0) && (frameInfo.mIccData != nullptr)) {
  117. // FIXME: Would it be possible to use MakeWithoutCopy?
  118. auto icc = SkData::MakeWithCopy(frameInfo.mIccData.get(), frameInfo.mIccSize);
  119. profile = SkEncodedInfo::ICCProfile::Make(std::move(icc));
  120. }
  121. if (profile && profile->profile()->data_color_space != skcms_Signature_RGB) {
  122. // This will result in sRGB.
  123. profile = nullptr;
  124. }
  125. SkEncodedInfo info = SkEncodedInfo::Make(frameInfo.mWidth, frameInfo.mHeight,
  126. SkEncodedInfo::kYUV_Color, SkEncodedInfo::kOpaque_Alpha, 8, std::move(profile));
  127. SkEncodedOrigin orientation = get_orientation(frameInfo);
  128. *result = kSuccess;
  129. return std::unique_ptr<SkCodec>(new SkHeifCodec(std::move(info), heifDecoder.release(),
  130. orientation));
  131. }
  132. SkHeifCodec::SkHeifCodec(SkEncodedInfo&& info, HeifDecoder* heifDecoder, SkEncodedOrigin origin)
  133. : INHERITED(std::move(info), skcms_PixelFormat_RGBA_8888, nullptr, origin)
  134. , fHeifDecoder(heifDecoder)
  135. , fSwizzleSrcRow(nullptr)
  136. , fColorXformSrcRow(nullptr)
  137. {}
  138. bool SkHeifCodec::conversionSupported(const SkImageInfo& dstInfo, bool srcIsOpaque,
  139. bool needsColorXform) {
  140. SkASSERT(srcIsOpaque);
  141. if (kUnknown_SkAlphaType == dstInfo.alphaType()) {
  142. return false;
  143. }
  144. if (kOpaque_SkAlphaType != dstInfo.alphaType()) {
  145. SkCodecPrintf("Warning: an opaque image should be decoded as opaque "
  146. "- it is being decoded as non-opaque, which will draw slower\n");
  147. }
  148. switch (dstInfo.colorType()) {
  149. case kRGBA_8888_SkColorType:
  150. return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
  151. case kBGRA_8888_SkColorType:
  152. return fHeifDecoder->setOutputColor(kHeifColorFormat_BGRA_8888);
  153. case kRGB_565_SkColorType:
  154. if (needsColorXform) {
  155. return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
  156. } else {
  157. return fHeifDecoder->setOutputColor(kHeifColorFormat_RGB565);
  158. }
  159. case kRGBA_F16_SkColorType:
  160. SkASSERT(needsColorXform);
  161. return fHeifDecoder->setOutputColor(kHeifColorFormat_RGBA_8888);
  162. default:
  163. return false;
  164. }
  165. }
  166. int SkHeifCodec::readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count,
  167. const Options& opts) {
  168. // When fSwizzleSrcRow is non-null, it means that we need to swizzle. In this case,
  169. // we will always decode into fSwizzlerSrcRow before swizzling into the next buffer.
  170. // We can never swizzle "in place" because the swizzler may perform sampling and/or
  171. // subsetting.
  172. // When fColorXformSrcRow is non-null, it means that we need to color xform and that
  173. // we cannot color xform "in place" (many times we can, but not when the dst is F16).
  174. // In this case, we will color xform from fColorXformSrcRow into the dst.
  175. uint8_t* decodeDst = (uint8_t*) dst;
  176. uint32_t* swizzleDst = (uint32_t*) dst;
  177. size_t decodeDstRowBytes = rowBytes;
  178. size_t swizzleDstRowBytes = rowBytes;
  179. int dstWidth = opts.fSubset ? opts.fSubset->width() : dstInfo.width();
  180. if (fSwizzleSrcRow && fColorXformSrcRow) {
  181. decodeDst = fSwizzleSrcRow;
  182. swizzleDst = fColorXformSrcRow;
  183. decodeDstRowBytes = 0;
  184. swizzleDstRowBytes = 0;
  185. dstWidth = fSwizzler->swizzleWidth();
  186. } else if (fColorXformSrcRow) {
  187. decodeDst = (uint8_t*) fColorXformSrcRow;
  188. swizzleDst = fColorXformSrcRow;
  189. decodeDstRowBytes = 0;
  190. swizzleDstRowBytes = 0;
  191. } else if (fSwizzleSrcRow) {
  192. decodeDst = fSwizzleSrcRow;
  193. decodeDstRowBytes = 0;
  194. dstWidth = fSwizzler->swizzleWidth();
  195. }
  196. for (int y = 0; y < count; y++) {
  197. if (!fHeifDecoder->getScanline(decodeDst)) {
  198. return y;
  199. }
  200. if (fSwizzler) {
  201. fSwizzler->swizzle(swizzleDst, decodeDst);
  202. }
  203. if (this->colorXform()) {
  204. this->applyColorXform(dst, swizzleDst, dstWidth);
  205. dst = SkTAddOffset<void>(dst, rowBytes);
  206. }
  207. decodeDst = SkTAddOffset<uint8_t>(decodeDst, decodeDstRowBytes);
  208. swizzleDst = SkTAddOffset<uint32_t>(swizzleDst, swizzleDstRowBytes);
  209. }
  210. return count;
  211. }
  212. /*
  213. * Performs the heif decode
  214. */
  215. SkCodec::Result SkHeifCodec::onGetPixels(const SkImageInfo& dstInfo,
  216. void* dst, size_t dstRowBytes,
  217. const Options& options,
  218. int* rowsDecoded) {
  219. if (options.fSubset) {
  220. // Not supporting subsets on this path for now.
  221. // TODO: if the heif has tiles, we can support subset here, but
  222. // need to retrieve tile config from metadata retriever first.
  223. return kUnimplemented;
  224. }
  225. if (!fHeifDecoder->decode(&fFrameInfo)) {
  226. return kInvalidInput;
  227. }
  228. fSwizzler.reset(nullptr);
  229. this->allocateStorage(dstInfo);
  230. int rows = this->readRows(dstInfo, dst, dstRowBytes, dstInfo.height(), options);
  231. if (rows < dstInfo.height()) {
  232. *rowsDecoded = rows;
  233. return kIncompleteInput;
  234. }
  235. return kSuccess;
  236. }
  237. void SkHeifCodec::allocateStorage(const SkImageInfo& dstInfo) {
  238. int dstWidth = dstInfo.width();
  239. size_t swizzleBytes = 0;
  240. if (fSwizzler) {
  241. swizzleBytes = fFrameInfo.mBytesPerPixel * fFrameInfo.mWidth;
  242. dstWidth = fSwizzler->swizzleWidth();
  243. SkASSERT(!this->colorXform() || SkIsAlign4(swizzleBytes));
  244. }
  245. size_t xformBytes = 0;
  246. if (this->colorXform() && (kRGBA_F16_SkColorType == dstInfo.colorType() ||
  247. kRGB_565_SkColorType == dstInfo.colorType())) {
  248. xformBytes = dstWidth * sizeof(uint32_t);
  249. }
  250. size_t totalBytes = swizzleBytes + xformBytes;
  251. fStorage.reset(totalBytes);
  252. if (totalBytes > 0) {
  253. fSwizzleSrcRow = (swizzleBytes > 0) ? fStorage.get() : nullptr;
  254. fColorXformSrcRow = (xformBytes > 0) ?
  255. SkTAddOffset<uint32_t>(fStorage.get(), swizzleBytes) : nullptr;
  256. }
  257. }
  258. void SkHeifCodec::initializeSwizzler(
  259. const SkImageInfo& dstInfo, const Options& options) {
  260. SkImageInfo swizzlerDstInfo = dstInfo;
  261. if (this->colorXform()) {
  262. // The color xform will be expecting RGBA 8888 input.
  263. swizzlerDstInfo = swizzlerDstInfo.makeColorType(kRGBA_8888_SkColorType);
  264. }
  265. int srcBPP = 4;
  266. if (dstInfo.colorType() == kRGB_565_SkColorType && !this->colorXform()) {
  267. srcBPP = 2;
  268. }
  269. fSwizzler = SkSwizzler::MakeSimple(srcBPP, swizzlerDstInfo, options);
  270. SkASSERT(fSwizzler);
  271. }
  272. SkSampler* SkHeifCodec::getSampler(bool createIfNecessary) {
  273. if (!createIfNecessary || fSwizzler) {
  274. SkASSERT(!fSwizzler || (fSwizzleSrcRow && fStorage.get() == fSwizzleSrcRow));
  275. return fSwizzler.get();
  276. }
  277. this->initializeSwizzler(this->dstInfo(), this->options());
  278. this->allocateStorage(this->dstInfo());
  279. return fSwizzler.get();
  280. }
  281. bool SkHeifCodec::onRewind() {
  282. fSwizzler.reset(nullptr);
  283. fSwizzleSrcRow = nullptr;
  284. fColorXformSrcRow = nullptr;
  285. fStorage.reset();
  286. return true;
  287. }
  288. SkCodec::Result SkHeifCodec::onStartScanlineDecode(
  289. const SkImageInfo& dstInfo, const Options& options) {
  290. // TODO: For now, just decode the whole thing even when there is a subset.
  291. // If the heif image has tiles, we could potentially do this much faster,
  292. // but the tile configuration needs to be retrieved from the metadata.
  293. if (!fHeifDecoder->decode(&fFrameInfo)) {
  294. return kInvalidInput;
  295. }
  296. if (options.fSubset) {
  297. this->initializeSwizzler(dstInfo, options);
  298. } else {
  299. fSwizzler.reset(nullptr);
  300. }
  301. this->allocateStorage(dstInfo);
  302. return kSuccess;
  303. }
  304. int SkHeifCodec::onGetScanlines(void* dst, int count, size_t dstRowBytes) {
  305. return this->readRows(this->dstInfo(), dst, dstRowBytes, count, this->options());
  306. }
  307. bool SkHeifCodec::onSkipScanlines(int count) {
  308. return count == (int) fHeifDecoder->skipScanlines(count);
  309. }
  310. #endif // SK_HAS_HEIF_LIBRARY