• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/composer/v1_2/display_composer_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     void InitJpegDecoder() override;
49     uint32_t AllocOutputBuffer(DecodeContext &context, OHOS::HDI::Codec::Image::V2_1::CodecImageBuffer& outputBuffer);
50     void ReleaseOutputBuffer(DecodeContext &context, Media::AllocatorType allocatorType);
51     uint32_t HardWareDecode(DecodeContext &context);
52     uint32_t DoHardWareDecode(DecodeContext &context);
53     #endif
54     uint32_t GifDecode(uint32_t index, DecodeContext &context, const uint64_t rowStride);
55     uint32_t GetImageSize(uint32_t index, OHOS::Media::Size &size) override;
56     uint32_t GetTopLevelImageNum(uint32_t &num) override;
57     uint32_t PromoteIncrementalDecode(uint32_t index, ProgDecodeContext &context) override;
58     uint32_t SetDecodeOptions(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info) override;
59     void Reset() override;
60     void SetSource(InputDataStream &sourceStream) override;
61 
62     uint32_t GetImagePropertyInt(uint32_t index, const std::string &key, int32_t &value) override;
63     uint32_t GetImagePropertyString(uint32_t index, const std::string &key, std::string &value) override;
64     uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
65         const std::string &path) override;
66     uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
67         const int fd) override;
68     uint32_t ModifyImageProperty(uint32_t index, const std::string &key, const std::string &value,
69         uint8_t *data, uint32_t size) override;
70     uint32_t GetFilterArea(const int &privacyType, std::vector<std::pair<uint32_t, uint32_t>> &ranges) override;
71     Media::ImageHdrType CheckHdrType() override;
72     void ValidateAndCorrectMetaData(Media::HdrMetadata& metadata) override;
73     uint32_t GetGainMapOffset() override;
74     Media::HdrMetadata GetHdrMetadata(Media::ImageHdrType type) override;
75     bool DecodeHeifGainMap(DecodeContext &context) override;
76     bool GetHeifHdrColorSpace(ColorManager::ColorSpaceName &gainmap, ColorManager::ColorSpaceName &hdr) override;
77     uint32_t GetHeifParseErr() 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     void FillYuvInfo(DecodeContext &context, SkImageInfo &dstInfo);
86     OHOS::Media::Size GetHeifRegionGridSize() override;
87 private:
88     typedef struct FrameCacheInfo {
89         int width;
90         int height;
91         uint64_t rowStride;
92         uint64_t byteCount;
93     } FrameCacheInfo;
94 
95     bool CheckCodec();
96     bool CheckIndexValied(uint32_t index);
97     bool DecodeHeader();
98     bool IsSupportScaleOnDecode();
99     bool GetScaledSize(int &dWidth, int &dHeight, float &scale);
100     bool GetHardwareScaledSize(int &dWidth, int &dHeight, float &scale);
101     int GetSoftwareScaledSize(int dwidth, int dheight);
102     bool GetSampleSize(int dstWidth, int dstHeight);
103     bool IsSupportCropOnDecode();
104     bool IsSupportCropOnDecode(SkIRect &target);
105     bool IsSupportHardwareDecode();
106     bool IsDivisibleBySampleSize();
107     bool IsSupportSampleDecode(OHOS::Media::PixelFormat desiredFormat);
108     bool IsYuv420Format(OHOS::Media::PixelFormat format) const;
109     bool IsHeifToYuvDecode(const DecodeContext &context) const;
110     uint32_t DoHeifToYuvDecode(DecodeContext &context);
111     void UpdateDstInfoAndOutInfo(DecodeContext &context);
112     uint32_t DoHeifSharedMemDecode(DecodeContext &context);
113     bool IsHeifSharedMemDecode(DecodeContext &context);
114     bool ConvertInfoToAlphaType(SkAlphaType &alphaType, OHOS::Media::AlphaType &outputType);
115     bool ConvertInfoToColorType(SkColorType &format, OHOS::Media::PixelFormat &outputFormat);
116     bool GetPropertyCheck(uint32_t index, const std::string &key, uint32_t &res);
117     SkAlphaType ConvertToAlphaType(OHOS::Media::AlphaType desiredType, OHOS::Media::AlphaType &outputType);
118     uint32_t PreDecodeCheck(uint32_t index);
119     uint32_t PreDecodeCheckYuv(uint32_t index, OHOS::Media::PixelFormat desiredFormat);
120     uint32_t ReadJpegData(uint8_t* jpegBuffer, uint32_t jpegBufferSize);
121     JpegYuvFmt GetJpegYuvOutFmt(OHOS::Media::PixelFormat desiredFormat);
122     bool ResetCodec();
123     SkColorType ConvertToColorType(OHOS::Media::PixelFormat format, OHOS::Media::PixelFormat &outputFormat);
124     uint32_t SetContextPixelsBuffer(uint64_t byteCount, DecodeContext &context);
125     uint32_t GetMakerImagePropertyString(const std::string &key, std::string &value);
126     uint32_t CheckDecodeOptions(uint32_t index, const PixelDecodeOptions &opts);
127     static void ReportImageType(SkEncodedImageFormat skEncodeFormat);
128     bool CheckContext(const DecodeContext &context);
129     uint32_t DmaMemAlloc(DecodeContext &context, uint64_t count, SkImageInfo &dstInfo);
130     uint32_t JpegHwDmaMemAlloc(DecodeContext &context, uint64_t count, SkImageInfo &dstInfo);
131     uint32_t DmaAlloc(DecodeContext &context, uint64_t count, const OHOS::BufferRequestConfig &requestConfig);
132     uint32_t UpdateHeifYuvDataInfo(DecodeContext &context, SkImageInfo &heifInfo,
133         uint32_t gridTileWidth, uint32_t gridTileHeight);
134     uint32_t HeifYUVMemAlloc(DecodeContext &context, SkImageInfo &heifInfo);
135     uint32_t DoHeifDecode(DecodeContext &context);
136     uint32_t DoHeifToRgbDecode(DecodeContext &context);
137     void SetHeifSampleSize(const PixelDecodeOptions &opts, int &dstWidth, int &dstHeight,
138         SkColorType desireColor, SkAlphaType desireAlpha);
139     bool IsSupportHeifHardwareDecode(const PixelDecodeOptions &opts);
140     bool DoHeifSwDecode(DecodeContext &context);
141     void SetHeifDecodeError(DecodeContext &context);
142     void SetHeifParseError();
143     uint32_t ConvertFormatToYUV(DecodeContext &context, SkImageInfo &skInfo,
144         uint64_t byteCount, OHOS::Media::PixelFormat format);
145     bool IsHeifToSingleHdrDecode(const DecodeContext &context) const;
146     uint32_t AllocHeifSingleHdrBuffer(DecodeContext &context);
147     uint32_t DoHeifToSingleHdrDecode(OHOS::ImagePlugin::DecodeContext &context);
148     uint32_t HandleGifCache(uint8_t* src, uint8_t* dst, uint64_t rowStride, int dstHeight);
149     uint32_t GetFramePixels(SkImageInfo& info, uint8_t* buffer, uint64_t rowStride, SkCodec::Options options);
150     FrameCacheInfo InitFrameCacheInfo(const uint64_t rowStride, SkImageInfo info);
151     bool FrameCacheInfoIsEqual(FrameCacheInfo& src, FrameCacheInfo& dst);
152     uint32_t UpdateHardWareDecodeInfo(DecodeContext &context);
153     uint32_t ExtractHeifRegion(const PixelDecodeOptions &opts);
154     bool IsHeifValidCrop(OHOS::Media::Rect &crop, SkImageInfo &info, int32_t gridInfoCols, int32_t gridInfoRows);
155     void SetHeifDecodeRegion(DecodeContext &context, int32_t gridTileWidth, int32_t gridTileHeight);
156     void UpdateHeifRegionDstInfo(DecodeContext &context);
157     void UpdateHeifSKInfo(DecodeContext &context, uint64_t &rowStride);
158     bool IsHeifRegionDecode();
159     bool HeifGainMapRegionCrop(DecodeContext &gainmapRegionContext, int32_t rowStride, uint8_t* dstBuffer,
160         uint32_t gainmapWidth, uint32_t gainmapHeight);
161     bool IsRegionDecodeSupported(uint32_t index, const PixelDecodeOptions &opts, PlImageInfo &info);
162     SkCodec::Result DoRegionDecode(DecodeContext &context);
163     SkCodec::Result DoSampleDecode(DecodeContext &context);
164     bool IsRawFormat(std::string &name);
GetPluginType()165     std::string GetPluginType() override
166     {
167         return "ext";
168     }
169     OHOS::Media::Size GetHeifGridTileSize() override;
170 
171     ImagePlugin::InputDataStream *stream_ = nullptr;
172     uint32_t streamOff_ = 0;
173     std::unique_ptr<SkCodec> codec_;
174     SkImageInfo info_;
175     SkImageInfo dstInfo_;
176     SkCodec::Options dstOptions_;
177     SkIRect dstSubset_;
178     int32_t frameCount_ = 0;
179     EXIFInfo exifInfo_;
180     uint8_t *gifCache_ = nullptr;
181     int gifCacheIndex_ = 0;
182     FrameCacheInfo frameCacheInfo_ = {0, 0, 0, 0};
183     uint32_t heifParseErr_ = 0;
184     std::shared_ptr<Media::PixelMap> reusePixelmap_ = nullptr;
185     OHOS::Media::Rect desiredRegion_ = {0, 0, 0, 0};
186     typedef struct HeifGridRegionInfo {
187         int32_t colCount;
188         int32_t rowCount;
189         int32_t tileWidth;
190         int32_t tileHeight;
191         int32_t heightPadding;
192         int32_t widthPadding;
193         bool isGridType;
194     } HeifGridRegionInfo;
195     HeifGridRegionInfo heifGridRegionInfo_ = {0, 0, 0, 0, 0, 0, false};
196     int32_t gridTileWidth_ = 0;
197     int32_t gridTileHeight_ = 0;
198 #ifdef IMAGE_COLORSPACE_FLAG
199     std::shared_ptr<OHOS::ColorManager::ColorSpace> dstColorSpace_ = nullptr;
200     std::shared_ptr<OHOS::ColorManager::ColorSpace> srcColorSpace_ = nullptr;
201     OHOS::ColorManager::ColorSpace GetSrcColorSpace();
202     uint32_t ApplyDesiredColorSpace(DecodeContext &context);
203     OHOS::ColorManager::ColorSpaceName heifColorSpaceName_ = ColorManager::ColorSpaceName::NONE;
204     bool heifIsColorSpaceFromCicp_ = false;
205 #endif
206 
207 #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM)
208     // hardware
209     SkImageInfo hwDstInfo_;
210     OHOS::Media::Size orgImgSize_;
211     OHOS::Media::Size outputBufferSize_;
212     OHOS::HDI::Display::Composer::V1_2::PixelFormat outputColorFmt_ =
213         OHOS::HDI::Display::Composer::V1_2::PIXEL_FMT_RGBA_8888;
214     static constexpr uint32_t ALIGN_8 = 8;
215     static constexpr uint32_t ALIGN_16 = 16;
216 #endif
217     OHOS::Media::CropAndScaleStrategy cropAndScaleStrategy_ = OHOS::Media::CropAndScaleStrategy::DEFAULT;
218     OHOS::Media::Size RegiondesiredSize_;
219     bool SupportRegionFlag_;
220     //Yuv
221     OHOS::Media::Size desiredSizeYuv_;
222     int softSampleSize_ = 1;
223     uint32_t sampleSize_ = 1;
224 
225     // hdr
226     Media::ImageHdrType hdrType_ = Media::ImageHdrType::UNKNOWN;
227     uint32_t gainMapOffset_ = 0;
228 #ifdef JPEG_HW_DECODE_ENABLE
229     std::shared_ptr<JpegHardwareDecoder> hwDecoderPtr_ = nullptr;
230     bool initJpegErr_ = false;
231 #endif
232 };
233 } // namespace ImagePlugin
234 } // namespace OHOS
235 
236 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_EXT_DECODER_H
237