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