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