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#ifndef SkHeifCodec_DEFINED
9#define SkHeifCodec_DEFINED
10
11#include "include/codec/SkCodec.h"
12#include "include/codec/SkEncodedOrigin.h"
13#include "include/core/SkImageInfo.h"
14#include "include/core/SkStream.h"
15#include "src/codec/SkFrameHolder.h"
16#include "src/codec/SkSwizzler.h"
17
18#if __has_include("HeifDecoderAPI.h")
19 #include "HeifDecoderAPI.h"
20#else
21 #include "src/codec/SkStubHeifDecoderAPI.h"
22#endif
23
24class SkHeifCodec : public SkCodec {
25public:
26 static bool IsHeif(const void*, size_t);
27
28 /*
29 * Assumes IsHeif was called and returned true.
30 */
31 static std::unique_ptr<SkCodec> MakeFromStream(
32 std::unique_ptr<SkStream>, SkCodec::SelectionPolicy selectionPolicy, Result*);
33
34protected:
35
36 Result onGetPixels(
37 const SkImageInfo& dstInfo,
38 void* dst, size_t dstRowBytes,
39 const Options& options,
40 int* rowsDecoded) override;
41
42 SkEncodedImageFormat onGetEncodedFormat() const override {
43 return SkEncodedImageFormat::kHEIF;
44 }
45
46 int onGetFrameCount() override;
47 bool onGetFrameInfo(int, FrameInfo*) const override;
48 int onGetRepetitionCount() override;
49 const SkFrameHolder* getFrameHolder() const override {
50 return &fFrameHolder;
51 }
52
53 bool conversionSupported(const SkImageInfo&, bool, bool) override;
54
55 bool onRewind() override;
56
57private:
58 /*
59 * Creates an instance of the decoder
60 * Called only by NewFromStream
61 */
62 SkHeifCodec(SkEncodedInfo&&, HeifDecoder*, SkEncodedOrigin, bool animation);
63
64 void initializeSwizzler(const SkImageInfo& dstInfo, const Options& options);
65 void allocateStorage(const SkImageInfo& dstInfo);
66 int readRows(const SkImageInfo& dstInfo, void* dst,
67 size_t rowBytes, int count, const Options&);
68
69 /*
70 * Scanline decoding.
71 */
72 SkSampler* getSampler(bool createIfNecessary) override;
73 Result onStartScanlineDecode(const SkImageInfo& dstInfo,
74 const Options& options) override;
75 int onGetScanlines(void* dst, int count, size_t rowBytes) override;
76 bool onSkipScanlines(int count) override;
77
78 std::unique_ptr<HeifDecoder> fHeifDecoder;
79 HeifFrameInfo fFrameInfo;
80 SkAutoTMalloc<uint8_t> fStorage;
81 uint8_t* fSwizzleSrcRow;
82 uint32_t* fColorXformSrcRow;
83
84 std::unique_ptr<SkSwizzler> fSwizzler;
85 bool fUseAnimation;
86
87 class Frame : public SkFrame {
88 public:
89 Frame(int i) : INHERITED(i) {}
90
91 protected:
92 SkEncodedInfo::Alpha onReportedAlpha() const override {
93 return SkEncodedInfo::Alpha::kOpaque_Alpha;
94 }
95
96 private:
97 typedef SkFrame INHERITED;
98 };
99
100 class FrameHolder : public SkFrameHolder {
101 public:
102 ~FrameHolder() override {}
103 void setScreenSize(int w, int h) {
104 fScreenWidth = w;
105 fScreenHeight = h;
106 }
107 Frame* appendNewFrame();
108 const Frame* frame(int i) const;
109 Frame* editFrameAt(int i);
110 int size() const {
111 return static_cast<int>(fFrames.size());
112 }
113 void reserve(int size) {
114 fFrames.reserve(size);
115 }
116
117 protected:
118 const SkFrame* onGetFrame(int i) const override;
119
120 private:
121 std::vector<Frame> fFrames;
122 };
123
124 FrameHolder fFrameHolder;
125 typedef SkCodec INHERITED;
126};
127
128#endif // SkHeifCodec_DEFINED
129