• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void getISOMetadata(std::vector<uint8_t>& isoMetadata) override;
66     void getErrMsg(std::string& errMsg) override;
67     bool CheckAuxiliaryMap(Media::AuxiliaryPictureType type);
68     bool setAuxiliaryMap(Media::AuxiliaryPictureType type);
69     bool getAuxiliaryMapInfo(HeifFrameInfo* frameInfo);
70     bool decodeAuxiliaryMap();
71     void setAuxiliaryDstBuffer(uint8_t* dstBuffer, size_t dstSize, size_t rowStride);
72     void getFragmentMetadata(Media::Rect& fragmentMetadata);
73 private:
74     bool Reinit(HeifFrameInfo *frameInfo);
75 
76     void InitFrameInfo(HeifFrameInfo *frameInfo, const std::shared_ptr<HeifImage> &image);
77 
78     void InitGridInfo(const std::shared_ptr<HeifImage> &image, GridInfo &gridInfo);
79 
80     void GetTileSize(const std::shared_ptr<HeifImage> &image, GridInfo &gridInfo);
81 
82     void GetRowColNum(GridInfo &gridInfo);
83 
84     GraphicPixelFormat GetInPixelFormat(const std::shared_ptr<HeifImage> &image);
85 
86     bool ProcessChunkHead(uint8_t *data, size_t len);
87 
88     void ReleaseHwDecoder(HeifHardwareDecoder *hwDecoder, bool isReuse);
89 
90     bool HwDecodeImage(HeifHardwareDecoder *hwDecoder,
91                        std::shared_ptr<HeifImage> &image, GridInfo &gridInfo,
92                        sptr<SurfaceBuffer> *outBuffer, bool isPrimary);
93 
94     void PreparePackedInput(HeifHardwareDecoder *hwDecoder, std::vector<std::shared_ptr<HeifImage>> tileImages,
95         std::vector<std::vector<uint8_t>> &packedInput, size_t gridCount);
96 
97     bool HwDecodeGrids(HeifHardwareDecoder *hwDecoder, std::shared_ptr<HeifImage> &image,
98                        GridInfo &gridInfo, sptr<SurfaceBuffer> &hwBuffer);
99 
100     bool HwDecodeIdenImage(HeifHardwareDecoder *hwDecoder,
101                            std::shared_ptr<HeifImage> &image, GridInfo &gridInfo,
102                            sptr<SurfaceBuffer> *outBuffer, bool isPrimary);
103 
104     bool HwDecodeSingleImage(HeifHardwareDecoder *hwDecoder, std::shared_ptr<HeifImage> &image,
105                              GridInfo &gridInfo, sptr<SurfaceBuffer> &hwBuffer);
106 
107     bool HwDecodeMimeImage(std::shared_ptr<HeifImage> &image);
108 
109     bool SwDecodeImage(std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param,
110                        GridInfo &gridInfo, bool isPrimary);
111 
112     bool SwDecodeGrids(Media::ImageFwkExtManager &extManager,
113                        std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param);
114 
115     bool SwDecodeIdenImage(std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param,
116                            GridInfo &gridInfo, bool isPrimary);
117 
118     bool SwDecodeSingleImage(Media::ImageFwkExtManager &extManager,
119                              std::shared_ptr<HeifImage> &image, HevcSoftDecodeParam &param);
120 
121     bool HwApplyAlphaImage(std::shared_ptr<HeifImage> &masterImage, uint8_t *dstMemory, size_t dstRowStride);
122 
123     bool SwApplyAlphaImage(std::shared_ptr<HeifImage> &masterImage, uint8_t *dstMemory, size_t dstRowStride);
124 
125     bool ConvertHwBufferPixelFormat(sptr<SurfaceBuffer> &hwBuffer, GridInfo &gridInfo,
126                                     uint8_t *dstMemory, size_t dstRowStride);
127 
128     bool IsDirectYUVDecode();
129 
130     void SetColorSpaceInfo(HeifFrameInfo* info, const std::shared_ptr<HeifImage>& image);
131 
132     void SetHardwareDecodeErrMsg(const uint32_t width, const uint32_t height);
133 
134     std::shared_ptr<HeifParser> parser_;
135     std::shared_ptr<HeifImage> primaryImage_;
136     Media::PixelFormat outPixelFormat_;
137     HeifFrameInfo imageInfo_{};
138 
139     GridInfo gridInfo_ = {0, 0, false, 0, 0, 0, 0, 1};
140     uint8_t *srcMemory_ = nullptr;
141     uint8_t *dstMemory_;
142     size_t dstRowStride_;
143     SurfaceBuffer *dstHwBuffer_;
144 
145     std::shared_ptr<HeifImage> gainmapImage_ = nullptr;
146     HeifFrameInfo gainmapImageInfo_{};
147     uint8_t* gainmapDstMemory_;
148     size_t gainmapDstRowStride_;
149 
150     std::shared_ptr<HeifImage> auxiliaryImage_ = nullptr;
151     HeifFrameInfo auxiliaryImageInfo_{};
152     GridInfo auxiliaryGridInfo_ = {0, 0, false, 0, 0, 0, 0, 1};
153     uint8_t* auxiliaryDstMemory_;
154     size_t auxiliaryDstRowStride_;
155     size_t auxiliaryDstMemorySize_;
156 
157     HeifFrameInfo tmapInfo_{};
158     std::string errMsg_;
159 
160     GridInfo gainmapGridInfo_ = {0, 0, false, 0, 0, 0, 0, 1};
161 };
162 } // namespace ImagePlugin
163 } // namespace OHOS
164 #endif
165 
166 #ifdef __cplusplus
167 extern "C" {
168 #endif
169 
170 HeifDecoder* CreateHeifDecoderImpl(void);
171 
172 #ifdef __cplusplus
173 }
174 #endif
175 
176 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_DECODER_IMPL_H
177