1 /* 2 * Copyright (C) 2023 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_EXT_DECODER_H 17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H 18 19 #include <cstdint> 20 #include <string> 21 22 #include "abs_image_decoder.h" 23 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 24 #include "display_type.h" 25 #include "hardware/jpeg_hw_decoder.h" 26 #endif 27 #include "ext_stream.h" 28 #include "exif_info.h" 29 #include "include/codec/SkCodec.h" 30 #include "nocopyable.h" 31 #include "plugin_class_base.h" 32 #include "jpeg_yuv_decoder/jpeg_decoder_yuv.h" 33 34 namespace OHOS { 35 struct BufferRequestConfig; 36 } 37 38 namespace OHOS { 39 namespace ImagePlugin { 40 class ExtDecoder : public AbsImageDecoder, public OHOS::MultimediaPlugin::PluginClassBase, NoCopyable { 41 public: 42 ExtDecoder(); 43 ~ExtDecoder() override; 44 bool HasProperty(std::string key) override; 45 uint32_t Decode(uint32_t index, DecodeContext &context) override; 46 uint32_t DecodeToYuv420(uint32_t index, DecodeContext &context); 47 #ifdef JPEG_HW_DECODE_ENABLE 48 uint32_t AllocOutputBuffer(DecodeContext &context, OHOS::HDI::Codec::Image::V2_0::CodecImageBuffer& outputBuffer); 49 void ReleaseOutputBuffer(DecodeContext &context, Media::AllocatorType allocatorType); 50 uint32_t HardWareDecode(DecodeContext &context); 51 uint32_t DoHardWareDecode(DecodeContext &context); 52 #endif 53 uint32_t GifDecode(uint32_t index, DecodeContext &context, const uint64_t rowStride); 54 uint32_t GetImageSize(uint32_t index, OHOS::Media::Size &size) override; 55 uint32_t GetTopLevelImageNum(uint32_t &num) override; 56 uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override; 57 uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override; 58 void Reset() override; 59 void SetSource(InputDataStream &sourceStream) override; 60 61 uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) override; 62 uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) override; 63 uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value, 64 const std::string &path) override; 65 uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value, 66 const int fd) override; 67 uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value, 68 uint8_t *data, uint32_t size) override; 69 uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges) override; 70 71 uint32_t GetHeifParseErr() override; 72 73 Media::ImageHdrType CheckHdrType() override; 74 uint32_t GetGainMapOffset() override; 75 Media::HdrMetadata GetHdrMetadata(Media::ImageHdrType type) override; 76 bool DecodeHeifGainMap(DecodeContext &context) override; 77 bool GetHeifHdrColorSpace(ColorManager::ColorSpaceName &gainmap, ColorManager::ColorSpaceName &hdr) override; 78 bool DecodeHeifAuxiliaryMap(DecodeContext& context, Media::AuxiliaryPictureType type) override; 79 bool CheckAuxiliaryMap(Media::AuxiliaryPictureType type) override; 80 bool GetHeifFragmentMetadata(Media::Rect& metadata) override; 81 #ifdef IMAGE_COLORSPACE_FLAG 82 OHOS::ColorManager::ColorSpace GetPixelMapColorSpace() override; 83 bool IsSupportICCProfile() override; 84 #endif 85 static void FillYuvInfo(DecodeContext &context, SkImageInfo &dstInfo); 86 private: 87 typedef struct FrameCacheInfo { 88 int width; 89 int height; 90 uint64_t rowStride; 91 uint64_t byteCount; 92 } FrameCacheInfo; 93 94 bool CheckCodec(); 95 bool CheckIndexValied(uint32_t index); 96 bool DecodeHeader(); 97 bool IsSupportScaleOnDecode(); 98 bool GetScaledSize(int &dWidth, int &dHeight, float &scale); 99 bool GetHardwareScaledSize(int &dWidth, int &dHeight, float &scale); 100 bool IsSupportCropOnDecode(); 101 bool IsSupportCropOnDecode(SkIRect &target); 102 bool IsSupportHardwareDecode(); 103 bool IsYuv420Format(OHOS::Media::PixelFormat format) const; 104 bool IsHeifToYuvDecode(const DecodeContext &context) const; 105 uint32_t DoHeifToYuvDecode(DecodeContext &context); 106 uint32_t DoHeifSharedMemDecode(DecodeContext &context); 107 bool IsHeifSharedMemDecode(DecodeContext &context); 108 bool ConvertInfoToAlphaType(SkAlphaType &alphaType, OHOS::Media::AlphaType &outputType); 109 bool ConvertInfoToColorType(SkColorType &format, OHOS::Media::PixelFormat &outputFormat); 110 bool GetPropertyCheck(uint32_t index, const std::string &key, uint32_t &res); 111 SkAlphaType ConvertToAlphaType(OHOS::Media::AlphaType desiredType, OHOS::Media::AlphaType &outputType); 112 uint32_t PreDecodeCheck(uint32_t index); 113 uint32_t PreDecodeCheckYuv(uint32_t index, OHOS::Media::PixelFormat desiredFormat); 114 uint32_t ReadJpegData(uint8_t* jpegBuffer, uint32_t jpegBufferSize); 115 JpegYuvFmt GetJpegYuvOutFmt(OHOS::Media::PixelFormat desiredFormat); 116 bool ResetCodec(); 117 SkColorType ConvertToColorType(OHOS::Media::PixelFormat format, OHOS::Media::PixelFormat &outputFormat); 118 uint32_t SetContextPixelsBuffer(uint64_t byteCount, DecodeContext &context); 119 uint32_t GetMakerImagePropertyString(const std::string &key, std::string &value); 120 uint32_t CheckDecodeOptions(uint32_t index, const PixelDecodeOptions &opts); 121 static void ReportImageType(SkEncodedImageFormat skEncodeFormat); 122 bool CheckContext(const DecodeContext &context); 123 uint32_t DmaMemAlloc(DecodeContext &context, uint64_t count, SkImageInfo &dstInfo); 124 uint32_t JpegHwDmaMemAlloc(DecodeContext &context, uint64_t count, SkImageInfo &dstInfo); 125 uint32_t DmaAlloc(DecodeContext &context, uint64_t count, const OHOS::BufferRequestConfig &requestConfig); 126 uint32_t HeifYUVMemAlloc(DecodeContext &context); 127 void SetHeifDecodeError(DecodeContext &context); 128 void SetHeifParseError(); 129 uint32_t ConvertFormatToYUV(DecodeContext &context, SkImageInfo &skInfo, 130 uint64_t byteCount, OHOS::Media::PixelFormat format); 131 bool IsHeifToSingleHdrDecode(const DecodeContext &context) const; 132 uint32_t DoHeifToSingleHdrDecode(OHOS::ImagePlugin::DecodeContext &context); 133 uint32_t HandleGifCache(uint8_t* src, uint8_t* dst, uint64_t rowStride, int dstHeight); 134 uint32_t GetFramePixels(SkImageInfo& info, uint8_t* buffer, uint64_t rowStride, SkCodec::Options options); 135 FrameCacheInfo InitFrameCacheInfo(const uint64_t rowStride, SkImageInfo info); 136 bool FrameCacheInfoIsEqual(FrameCacheInfo& src, FrameCacheInfo& dst); 137 void UpdateHardWareDecodeInfo(DecodeContext &context); 138 139 ImagePlugin::InputDataStream *stream_ = nullptr; 140 uint32_t streamOff_ = 0; 141 std::unique_ptr<SkCodec> codec_; 142 SkImageInfo info_; 143 SkImageInfo dstInfo_; 144 SkCodec::Options dstOptions_; 145 SkIRect dstSubset_; 146 int32_t frameCount_ = 0; 147 EXIFInfo exifInfo_; 148 uint8_t *gifCache_ = nullptr; 149 int gifCacheIndex_ = 0; 150 FrameCacheInfo frameCacheInfo_ = {0, 0, 0, 0}; 151 uint32_t heifParseErr_ = 0; 152 std::shared_ptr<Media::PixelMap> reusePixelmap_ = nullptr; 153 #ifdef IMAGE_COLORSPACE_FLAG 154 std::shared_ptr<OHOS::ColorManager::ColorSpace> dstColorSpace_ = nullptr; 155 std::shared_ptr<OHOS::ColorManager::ColorSpace> srcColorSpace_ = nullptr; 156 OHOS::ColorManager::ColorSpace GetSrcColorSpace(); 157 uint32_t ApplyDesiredColorSpace(DecodeContext &context); 158 #endif 159 160 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 161 // hardware 162 SkImageInfo hwDstInfo_; 163 OHOS::Media::Size orgImgSize_; 164 OHOS::Media::Size outputBufferSize_; 165 PixelFormat outputColorFmt_ = PIXEL_FMT_RGBA_8888; 166 uint32_t sampleSize_ = 1; 167 static constexpr uint32_t ALIGN_8 = 8; 168 static constexpr uint32_t ALIGN_16 = 16; 169 #endif 170 171 //Yuv 172 OHOS::Media::Size desiredSizeYuv_; 173 174 // hdr 175 Media::ImageHdrType hdrType_ = Media::ImageHdrType::UNKNOWN; 176 uint32_t gainMapOffset_ = 0; 177 }; 178 } // namespace ImagePlugin 179 } // namespace OHOS 180 181 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H 182