blob: 67e2c57779d15729940161d71ec0ce5d13a64e89 [file] [log] [blame]
Leon Scroggins III04be2b52017-08-17 15:13:20 -04001/*
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#ifndef SkHeifCodec_DEFINED
9#define SkHeifCodec_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/codec/SkCodec.h"
12#include "include/codec/SkEncodedOrigin.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkStream.h"
Leon Scroggins III6154ac42019-08-14 11:29:29 -040015#include "src/codec/SkFrameHolder.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/codec/SkSwizzler.h"
Mike Klein36f182f2017-10-20 12:33:53 -040017
Mike Klein36f182f2017-10-20 12:33:53 -040018#if __has_include("HeifDecoderAPI.h")
19 #include "HeifDecoderAPI.h"
20#else
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021 #include "src/codec/SkStubHeifDecoderAPI.h"
Mike Klein36f182f2017-10-20 12:33:53 -040022#endif
Leon Scroggins III04be2b52017-08-17 15:13:20 -040023
24class SkHeifCodec : public SkCodec {
25public:
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080026 /*
27 * Returns true if one of kHEIF or kAVIF images were detected. If |format|
28 * is not nullptr, it will contain the detected format. Returns false
29 * otherwise.
30 */
31 static bool IsSupported(const void*, size_t, SkEncodedImageFormat* format);
Leon Scroggins III04be2b52017-08-17 15:13:20 -040032
33 /*
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080034 * Assumes IsSupported was called and it returned a non-nullopt value.
Leon Scroggins III04be2b52017-08-17 15:13:20 -040035 */
Leon Scroggins III6154ac42019-08-14 11:29:29 -040036 static std::unique_ptr<SkCodec> MakeFromStream(
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080037 std::unique_ptr<SkStream>, SkCodec::SelectionPolicy selectionPolicy,
38 SkEncodedImageFormat, Result*);
Leon Scroggins III04be2b52017-08-17 15:13:20 -040039
40protected:
41
42 Result onGetPixels(
43 const SkImageInfo& dstInfo,
44 void* dst, size_t dstRowBytes,
45 const Options& options,
46 int* rowsDecoded) override;
47
48 SkEncodedImageFormat onGetEncodedFormat() const override {
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080049 return fFormat;
Leon Scroggins III04be2b52017-08-17 15:13:20 -040050 }
51
Leon Scroggins III6154ac42019-08-14 11:29:29 -040052 int onGetFrameCount() override;
53 bool onGetFrameInfo(int, FrameInfo*) const override;
54 int onGetRepetitionCount() override;
55 const SkFrameHolder* getFrameHolder() const override {
56 return &fFrameHolder;
57 }
Leon Scroggins III6154ac42019-08-14 11:29:29 -040058
Leon Scroggins III712476e2018-10-03 15:47:00 -040059 bool conversionSupported(const SkImageInfo&, bool, bool) override;
Leon Scroggins III04be2b52017-08-17 15:13:20 -040060
Leon Scroggins IIIbcc75322019-04-15 10:34:53 -040061 bool onRewind() override;
62
Leon Scroggins III04be2b52017-08-17 15:13:20 -040063private:
64 /*
65 * Creates an instance of the decoder
66 * Called only by NewFromStream
67 */
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080068 SkHeifCodec(SkEncodedInfo&&, HeifDecoder*, SkEncodedOrigin, bool animation,
69 SkEncodedImageFormat);
Leon Scroggins III04be2b52017-08-17 15:13:20 -040070
71 void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
72 void allocateStorage(const SkImageInfo& dstInfo);
73 int readRows(const SkImageInfo& dstInfo, void* dst,
74 size_t rowBytes, int count, const Options&);
75
76 /*
77 * Scanline decoding.
78 */
79 SkSampler* getSampler(bool createIfNecessary) override;
80 Result onStartScanlineDecode(const SkImageInfo& dstInfo,
81 const Options& options) override;
82 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
83 bool onSkipScanlines(int count) override;
84
85 std::unique_ptr<HeifDecoder> fHeifDecoder;
86 HeifFrameInfo fFrameInfo;
87 SkAutoTMalloc<uint8_t> fStorage;
88 uint8_t* fSwizzleSrcRow;
89 uint32_t* fColorXformSrcRow;
90
91 std::unique_ptr<SkSwizzler> fSwizzler;
Leon Scroggins III6154ac42019-08-14 11:29:29 -040092 bool fUseAnimation;
Vignesh Venkatasubramanianeb7f9602020-11-04 14:13:39 -080093 const SkEncodedImageFormat fFormat;
Leon Scroggins III04be2b52017-08-17 15:13:20 -040094
Leon Scroggins III6154ac42019-08-14 11:29:29 -040095 class Frame : public SkFrame {
96 public:
97 Frame(int i) : INHERITED(i) {}
98
99 protected:
100 SkEncodedInfo::Alpha onReportedAlpha() const override {
101 return SkEncodedInfo::Alpha::kOpaque_Alpha;
102 }
103
104 private:
John Stiles7571f9e2020-09-02 22:42:33 -0400105 using INHERITED = SkFrame;
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400106 };
107
108 class FrameHolder : public SkFrameHolder {
109 public:
110 ~FrameHolder() override {}
111 void setScreenSize(int w, int h) {
112 fScreenWidth = w;
113 fScreenHeight = h;
114 }
115 Frame* appendNewFrame();
116 const Frame* frame(int i) const;
Chong Zhangea68eac2019-08-26 11:28:38 -0700117 Frame* editFrameAt(int i);
Leon Scroggins III6154ac42019-08-14 11:29:29 -0400118 int size() const {
119 return static_cast<int>(fFrames.size());
120 }
121 void reserve(int size) {
122 fFrames.reserve(size);
123 }
124
125 protected:
126 const SkFrame* onGetFrame(int i) const override;
127
128 private:
129 std::vector<Frame> fFrames;
130 };
131
132 FrameHolder fFrameHolder;
John Stiles7571f9e2020-09-02 22:42:33 -0400133 using INHERITED = SkCodec;
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400134};
135
Leon Scroggins III04be2b52017-08-17 15:13:20 -0400136#endif // SkHeifCodec_DEFINED