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 #include "image_source_ohos.h"
17
18 #include "media_errors.h"
19
20 namespace OHOS::Ace {
21 namespace {
InitDecodeOptions(Media::DecodeOptions & options,const std::pair<int32_t,int32_t> & size,AIImageQuality imageQuality,bool isHdrDecoderNeed,PixelFormat photoDecodeFormat)22 void InitDecodeOptions(Media::DecodeOptions& options, const std::pair<int32_t, int32_t>& size,
23 AIImageQuality imageQuality, bool isHdrDecoderNeed, PixelFormat photoDecodeFormat)
24 {
25 options.preferDma = true;
26 // only hdr image need to decoder in hdr mode
27 if (isHdrDecoderNeed) {
28 options.desiredDynamicRange = Media::DecodeDynamicRange::AUTO;
29 }
30 if (photoDecodeFormat == PixelFormat::NV21) {
31 options.photoDesiredPixelFormat = Media::PixelFormat::NV21;
32 } else if (photoDecodeFormat == PixelFormat::RGBA_8888) {
33 options.photoDesiredPixelFormat = Media::PixelFormat::RGBA_8888;
34 } else if (photoDecodeFormat == PixelFormat::RGBA_1010102) {
35 options.photoDesiredPixelFormat = Media::PixelFormat::RGBA_1010102;
36 } else if (photoDecodeFormat == PixelFormat::YCBCR_P010) {
37 options.photoDesiredPixelFormat = Media::PixelFormat::YCBCR_P010;
38 } else if (photoDecodeFormat == PixelFormat::YCRCB_P010) {
39 options.photoDesiredPixelFormat = Media::PixelFormat::YCRCB_P010;
40 }
41 // Pass imageQuality to imageFramework
42 options.resolutionQuality = static_cast<Media::ResolutionQuality>(imageQuality);
43 if (size.first > 0 && size.second > 0) {
44 options.desiredSize = { size.first, size.second };
45 }
46 }
47 } // namespace
48
Create(int32_t fd)49 RefPtr<ImageSource> ImageSource::Create(int32_t fd)
50 {
51 uint32_t errorCode;
52 Media::SourceOptions options;
53 auto src = Media::ImageSource::CreateImageSource(fd, options, errorCode);
54 if (errorCode != Media::SUCCESS) {
55 TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
56 return nullptr;
57 }
58 return MakeRefPtr<ImageSourceOhos>(std::move(src));
59 }
60
Create(const uint8_t * data,uint32_t size)61 RefPtr<ImageSource> ImageSource::Create(const uint8_t* data, uint32_t size)
62 {
63 uint32_t errorCode;
64 Media::SourceOptions options;
65 auto src = Media::ImageSource::CreateImageSource(data, size, options, errorCode);
66 if (errorCode != Media::SUCCESS) {
67 TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
68 return nullptr;
69 }
70 return MakeRefPtr<ImageSourceOhos>(std::move(src));
71 }
72
Create(const std::string & filePath)73 RefPtr<ImageSource> ImageSource::Create(const std::string& filePath)
74 {
75 Media::SourceOptions opts;
76 uint32_t errorCode = 0;
77 auto src = Media::ImageSource::CreateImageSource(filePath, opts, errorCode);
78 if (errorCode != Media::SUCCESS) {
79 TAG_LOGW(AceLogTag::ACE_IMAGE, "create image source failed, errorCode = %{public}u", errorCode);
80 return nullptr;
81 }
82 return MakeRefPtr<ImageSourceOhos>(std::move(src));
83 }
84
IsAstc(const uint8_t * data,size_t size)85 bool ImageSource::IsAstc(const uint8_t* data, size_t size)
86 {
87 return Media::ImageSource::IsASTC(data, size);
88 }
89
GetASTCInfo(const uint8_t * data,size_t size)90 ImageSource::Size ImageSource::GetASTCInfo(const uint8_t* data, size_t size)
91 {
92 Media::ASTCInfo astcInfo;
93 Media::ImageSource::GetASTCInfo(data, size, astcInfo);
94 return { astcInfo.size.width, astcInfo.size.height };
95 }
96
GetProperty(const std::string & key)97 std::string ImageSourceOhos::GetProperty(const std::string& key)
98 {
99 std::string value;
100 uint32_t res = imageSource_->GetImagePropertyString(0, key, value);
101 if (res != Media::SUCCESS) {
102 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get ImageSource property %{public}s failed, errorCode = %{public}u",
103 key.c_str(), res);
104 }
105 return value;
106 }
107
CreatePixelMap(const Size & size,AIImageQuality imageQuality,bool isHdrDecoderNeed,PixelFormat photoDecodeFormat)108 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(
109 const Size& size, AIImageQuality imageQuality, bool isHdrDecoderNeed, PixelFormat photoDecodeFormat)
110 {
111 return CreatePixelMap(0, size, imageQuality, isHdrDecoderNeed, photoDecodeFormat);
112 }
113
CreatePixelMap(uint32_t index,const Size & size,AIImageQuality imageQuality,bool isHdrDecoderNeed,PixelFormat photoDecodeFormat)114 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap(
115 uint32_t index, const Size& size, AIImageQuality imageQuality, bool isHdrDecoderNeed, PixelFormat photoDecodeFormat)
116 {
117 Media::DecodeOptions options;
118 InitDecodeOptions(options, size, imageQuality, isHdrDecoderNeed, photoDecodeFormat);
119 uint32_t errorCode;
120 auto pixmap = imageSource_->CreatePixelMapEx(index, options, errorCode);
121 if (errorCode != Media::SUCCESS) {
122 TAG_LOGW(AceLogTag::ACE_IMAGE,
123 "create PixelMap from ImageSource failed, index = %{public}u, errorCode = %{public}u", index, errorCode);
124 return nullptr;
125 }
126 return PixelMap::Create(std::move(pixmap));
127 }
128
CreatePixelMap()129 RefPtr<PixelMap> ImageSourceOhos::CreatePixelMap()
130 {
131 uint32_t errorCode;
132 Media::DecodeOptions decodeOpts;
133 auto pixelMap = imageSource_->CreatePixelMap(decodeOpts, errorCode);
134 if (errorCode != Media::SUCCESS) {
135 TAG_LOGW(AceLogTag::ACE_IMAGE,
136 "create PixelMap from ImageSource failed, errorCode = %{public}u", errorCode);
137 return nullptr;
138 }
139 return PixelMap::Create(std::move(pixelMap));
140 }
141
GetImageSize()142 ImageSource::Size ImageSourceOhos::GetImageSize()
143 {
144 Media::ImageInfo info;
145 auto errorCode = imageSource_->GetImageInfo(info);
146 if (errorCode != Media::SUCCESS) {
147 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get ImageSource info failed, errorCode = %{public}u", errorCode);
148 return { 0, 0 };
149 }
150 return { info.size.width, info.size.height };
151 }
152
GetFrameCount()153 uint32_t ImageSourceOhos::GetFrameCount()
154 {
155 uint32_t errorCode;
156 auto frameCount = imageSource_->GetFrameCount(errorCode);
157 if (errorCode != Media::SUCCESS) {
158 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get image frame count failed, errorCode = %{public}u", errorCode);
159 return 0;
160 }
161 return frameCount;
162 }
163
GetEncodedFormat()164 std::string ImageSourceOhos::GetEncodedFormat()
165 {
166 uint32_t errorCode;
167 auto sourceInfo = imageSource_->GetSourceInfo(errorCode);
168 if (errorCode != Media::SUCCESS) {
169 TAG_LOGW(AceLogTag::ACE_IMAGE, "Get image source info failed, errorCode = %{public}u", errorCode);
170 return "";
171 }
172 return sourceInfo.encodedFormat;
173 }
174 } // namespace OHOS::Ace
175