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 Media::ImageHdrType CheckHdrType() override; 71 uint32_t GetGainMapOffset() override; 72 Media::HdrMetadata GetHdrMetadata(Media::ImageHdrType type) override; 73 bool DecodeHeifGainMap(DecodeContext &context) override; 74 bool GetHeifHdrColorSpace(ColorManager::ColorSpaceName &gainmap, ColorManager::ColorSpaceName &hdr) override; 75 uint32_t GetHeifParseErr() override; 76 bool DecodeHeifAuxiliaryMap(DecodeContext& context, Media::AuxiliaryPictureType type) override; 77 bool CheckAuxiliaryMap(Media::AuxiliaryPictureType type) override; 78 bool GetHeifFragmentMetadata(Media::Rect& metadata) override; 79 #ifdef IMAGE_COLORSPACE_FLAG 80 OHOS::ColorManager::ColorSpace GetPixelMapColorSpace() override; 81 bool IsSupportICCProfile() override; 82 #endif 83 static void FillYuvInfo(DecodeContext &context, SkImageInfo &dstInfo); 84 private: 85 typedef struct FrameCacheInfo { 86 int width; 87 int height; 88 uint64_t rowStride; 89 uint64_t byteCount; 90 } FrameCacheInfo; 91 92 bool CheckCodec(); 93 bool CheckIndexValied(uint32_t index); 94 bool DecodeHeader(); 95 bool IsSupportScaleOnDecode(); 96 bool GetScaledSize(int &dWidth, int &dHeight, float &scale); 97 bool GetHardwareScaledSize(int &dWidth, int &dHeight, float &scale); 98 int GetSoftwareScaledSize(int dwidth, int dheight); 99 bool IsSupportCropOnDecode(); 100 bool IsSupportCropOnDecode(SkIRect &target); 101 bool IsSupportHardwareDecode(); 102 bool IsYuv420Format(OHOS::Media::PixelFormat format) const; 103 bool IsHeifToYuvDecode(const DecodeContext &context) const; 104 uint32_t DoHeifToYuvDecode(DecodeContext &context); 105 uint32_t DoHeifSharedMemDecode(DecodeContext &context); 106 bool IsHeifSharedMemDecode(DecodeContext &context); 107 bool ConvertInfoToAlphaType(SkAlphaType &alphaType, OHOS::Media::AlphaType &outputType); 108 bool ConvertInfoToColorType(SkColorType &format, OHOS::Media::PixelFormat &outputFormat); 109 bool GetPropertyCheck(uint32_t index, const std::string &key, uint32_t &res); 110 SkAlphaType ConvertToAlphaType(OHOS::Media::AlphaType desiredType, OHOS::Media::AlphaType &outputType); 111 uint32_t PreDecodeCheck(uint32_t index); 112 uint32_t PreDecodeCheckYuv(uint32_t index, OHOS::Media::PixelFormat desiredFormat); 113 uint32_t ReadJpegData(uint8_t* jpegBuffer, uint32_t jpegBufferSize); 114 JpegYuvFmt GetJpegYuvOutFmt(OHOS::Media::PixelFormat desiredFormat); 115 bool ResetCodec(); 116 SkColorType ConvertToColorType(OHOS::Media::PixelFormat format, OHOS::Media::PixelFormat &outputFormat); 117 uint32_t SetContextPixelsBuffer(uint64_t byteCount, DecodeContext &context); 118 uint32_t GetMakerImagePropertyString(const std::string &key, std::string &value); 119 uint32_t CheckDecodeOptions(uint32_t index, const PixelDecodeOptions &opts); 120 static void ReportImageType(SkEncodedImageFormat skEncodeFormat); 121 bool CheckContext(const DecodeContext &context); 122 uint32_t DmaMemAlloc(DecodeContext &context, uint64_t count, SkImageInfo &dstInfo); 123 uint32_t JpegHwDmaMemAlloc(DecodeContext &context, uint64_t count, SkImageInfo &dstInfo); 124 uint32_t DmaAlloc(DecodeContext &context, uint64_t count, const OHOS::BufferRequestConfig &requestConfig); 125 uint32_t AllocateHeifYuvAuxiliaryBuffer(DecodeContext& context, uint32_t width, uint32_t height); 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 uint32_t UpdateHardWareDecodeInfo(DecodeContext &context); 138 bool IsRegionDecodeSupported(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info); 139 SkCodec::Result DoRegionDecode(DecodeContext &context); 140 141 ImagePlugin::InputDataStream *stream_ = nullptr; 142 uint32_t streamOff_ = 0; 143 std::unique_ptr<SkCodec> codec_; 144 SkImageInfo info_; 145 SkImageInfo dstInfo_; 146 SkCodec::Options dstOptions_; 147 SkIRect dstSubset_; 148 int32_t frameCount_ = 0; 149 EXIFInfo exifInfo_; 150 uint8_t *gifCache_ = nullptr; 151 int gifCacheIndex_ = 0; 152 FrameCacheInfo frameCacheInfo_ = {0, 0, 0, 0}; 153 uint32_t heifParseErr_ = 0; 154 std::shared_ptr<Media::PixelMap> reusePixelmap_ = nullptr; 155 #ifdef IMAGE_COLORSPACE_FLAG 156 std::shared_ptr<OHOS::ColorManager::ColorSpace> dstColorSpace_ = nullptr; 157 std::shared_ptr<OHOS::ColorManager::ColorSpace> srcColorSpace_ = nullptr; 158 OHOS::ColorManager::ColorSpace GetSrcColorSpace(); 159 uint32_t ApplyDesiredColorSpace(DecodeContext &context); 160 #endif 161 162 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) 163 // hardware 164 SkImageInfo hwDstInfo_; 165 OHOS::Media::Size orgImgSize_; 166 OHOS::Media::Size outputBufferSize_; 167 PixelFormat outputColorFmt_ = PIXEL_FMT_RGBA_8888; 168 uint32_t sampleSize_ = 1; 169 static constexpr uint32_t ALIGN_8 = 8; 170 static constexpr uint32_t ALIGN_16 = 16; 171 #endif 172 OHOS::Media::CropAndScaleStrategy cropAndScaleStrategy_ = OHOS::Media::CropAndScaleStrategy::DEFAULT; 173 OHOS::Media::Size RegiondesiredSize_; 174 bool SupportRegionFlag_; 175 //Yuv 176 OHOS::Media::Size desiredSizeYuv_; 177 178 // hdr 179 Media::ImageHdrType hdrType_ = Media::ImageHdrType::UNKNOWN; 180 uint32_t gainMapOffset_ = 0; 181 }; 182 } // namespace ImagePlugin 183 } // namespace OHOS 184 185 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H 186