1 /*
2  * Copyright (C) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_IMPL_H
17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_IMPL_H
18 
19 #include "HeifDecoder.h"
20 
21 #ifdef HEIF_HW_DECODE_ENABLE
22 #include "heif_parser.h"
23 #include "hevc_sw_decode_param.h"
24 #include "image_type.h"
25 #include "surface_buffer.h"
26 
27 #include "hardware/heif_hw_decoder.h"
28 
29 namespace OHOS::Media {
30     class ImageFwkExtManager;
31 }
32 
33 namespace OHOS {
34 namespace ImagePlugin {
35 class HeifDecoderImpl : public HeifDecoder {
36 public:
37     HeifDecoderImpl();
38 
39     ~HeifDecoderImpl() override;
40 
41     bool init(HeifStream *stream, HeifFrameInfo *frameInfo) override;
42 
43     bool getSequenceInfo(HeifFrameInfo *frameInfo, size_t *frameCount) override;
44 
45     bool setOutputColor(SkHeifColorFormat heifColor) override;
46 
47     bool decode(HeifFrameInfo *frameInfo) override;
48 
49     bool decodeSequence(int frameIndex, HeifFrameInfo *frameInfo) override;
50 
51     void setDstBuffer(uint8_t *dstBuffer, size_t rowStride, void *context) override;
52 
53     bool getScanline(uint8_t *dst) override;
54 
55     size_t skipScanlines(int count) override;
56 
57     bool getImageInfo(HeifFrameInfo *frameInfo) override;
58     bool decodeGainmap() override;
59     void setGainmapDstBuffer(uint8_t* dstBuffer, size_t rowStride) override;
60     bool getGainmapInfo(HeifFrameInfo* frameInfo) override;
61     bool getTmapInfo(HeifFrameInfo* frameInfo) override;
62     HeifImageHdrType getHdrType() override;
63     void getVividMetadata(std::vector<uint8_t>& uwaInfo, std::vector<uint8_t>& displayInfo,
64         std::vector<uint8_t>& lightInfo) override;
65     bool DodecodeGainmap(std::shared_ptr<HeifImage> &gainmapImage, GridInfo &gainmapGridInfo,
66                          uint8_t* gainmapDstMemory, size_t gainmapDstRowStride);
67     void getISOMetadata(std::vector<uint8_t>& isoMetadata) override;
68     void getErrMsg(std::string& errMsg) override;
69     bool CheckAuxiliaryMap(Media::AuxiliaryPictureType type);
70     bool setAuxiliaryMap(Media::AuxiliaryPictureType type);
71     bool getAuxiliaryMapInfo(HeifFrameInfo* frameInfo);
72     bool decodeAuxiliaryMap();
73     void setAuxiliaryDstBuffer(uint8_t* dstBuffer, size_t dstSize, size_t rowStride);
74     void getFragmentMetadata(Media::Rect& fragmentMetadata);
75 private:
76     bool Reinit(HeifFrameInfo *frameInfo);
77 
78     void InitFrameInfo(HeifFrameInfo *frameInfo, const std::shared_ptr<HeifImage> &image);
79 
80     void InitGridInfo(const std::shared_ptr<HeifImage> &image, GridInfo &gridInfo);
81 
82     void GetTileSize(const std::shared_ptr<HeifImage> &image, GridInfo &gridInfo);
83 
84     void GetRowColNum(GridInfo &gridInfo);
85 
86     GraphicPixelFormat GetInPixelFormat(const std::shared_ptr<HeifImage> &image);
87 
88     bool ProcessChunkHead(uint8_t *data, size_t len);
89 
90     void ReleaseHwDecoder(HeifHardwareDecoder *hwDecoder, bool isReuse);
91 
92     bool HwDecodeImage(HeifHardwareDecoder *hwDecoder,
93                        std::shared_ptr<HeifImage> &image, GridInfo &gridInfo,
94                        sptr<SurfaceBuffer> *outBuffer, bool isPrimary);
95 
96     void PreparePackedInput(HeifHardwareDecoder *hwDecoder, std::vector<std::shared_ptr<HeifImage>> tileImages,
97         std::vector<std::vector<uint8_t>> &packedInput, size_t gridCount);
98 
99     bool HwDecodeGrids(HeifHardwareDecoder *hwDecoder, std::shared_ptr<HeifImage> &image,
100                        GridInfo &gridInfo, sptr<SurfaceBuffer> &hwBuffer);
101 
102     bool HwDecodeIdenImage(HeifHardwareDecoder *hwDecoder,
103                            std::shared_ptr<HeifImage> &image, GridInfo &gridInfo,
104                            sptr<SurfaceBuffer> *outBuffer, bool isPrimary);
105 
106     bool HwDecodeSingleImage(HeifHardwareDecoder *hwDecoder, std::shared_ptr<HeifImage> &image,
107                              GridInfo &gridInfo, sptr<SurfaceBuffer> &hwBuffer);
108 
109     bool HwDecodeMimeImage(std::shared_ptr<HeifImage> &image);
110 
111     bool SwDecodeImage(std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param,
112                        GridInfo &gridInfo, bool isPrimary);
113     bool SwDecodeGainmap(std::shared_ptr<HeifImage> &gainMapImage,
114                          GridInfo &gainmapGridInfo, sptr<SurfaceBuffer> *outputBuf);
115 
116     bool SwDecodeGrids(Media::ImageFwkExtManager &extManager,
117                        std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param);
118 
119     bool SwDecodeIdenImage(std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param,
120                            GridInfo &gridInfo, bool isPrimary);
121 
122     bool SwDecodeSingleImage(Media::ImageFwkExtManager &extManager,
123                              std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param);
124 
125     bool HwApplyAlphaImage(std::shared_ptr<HeifImage> &masterImage, uint8_t *dstMemory, size_t dstRowStride);
126 
127     bool SwApplyAlphaImage(std::shared_ptr<HeifImage> &masterImage, uint8_t *dstMemory, size_t dstRowStride);
128 
129     bool ConvertHwBufferPixelFormat(sptr<SurfaceBuffer> &hwBuffer, GridInfo &gridInfo,
130                                     uint8_t *dstMemory, size_t dstRowStride);
131 
132     bool IsDirectYUVDecode();
133 
134     void SetColorSpaceInfo(HeifFrameInfo* info, const std::shared_ptr<HeifImage>& image);
135 
136     void SetHardwareDecodeErrMsg(const uint32_t width, const uint32_t height);
137 
138     std::shared_ptr<HeifParser> parser_;
139     std::shared_ptr<HeifImage> primaryImage_;
140     Media::PixelFormat outPixelFormat_;
141     HeifFrameInfo imageInfo_{};
142 
143     GridInfo gridInfo_ = {0, 0, false, 0, 0, 0, 0, 1};
144     uint8_t *srcMemory_ = nullptr;
145     uint8_t *dstMemory_;
146     size_t dstRowStride_;
147     SurfaceBuffer *dstHwBuffer_;
148 
149     std::shared_ptr<HeifImage> gainmapImage_ = nullptr;
150     HeifFrameInfo gainmapImageInfo_{};
151     uint8_t* gainmapDstMemory_;
152     size_t gainmapDstRowStride_;
153 
154     std::shared_ptr<HeifImage> auxiliaryImage_ = nullptr;
155     HeifFrameInfo auxiliaryImageInfo_{};
156     GridInfo auxiliaryGridInfo_ = {0, 0, false, 0, 0, 0, 0, 1};
157     uint8_t* auxiliaryDstMemory_;
158     size_t auxiliaryDstRowStride_;
159     size_t auxiliaryDstMemorySize_;
160 
161     HeifFrameInfo tmapInfo_{};
162     std::string errMsg_;
163 
164     GridInfo gainmapGridInfo_ = {0, 0, false, 0, 0, 0, 0, 1};
165 };
166 } // namespace ImagePlugin
167 } // namespace OHOS
168 #endif
169 
170 #ifdef __cplusplus
171 extern "C" {
172 #endif
173 
174 HeifDecoder* CreateHeifDecoderImpl(void);
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_IMPL_H
181