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