1 /*
2  * Copyright (c) 2022 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 #include "output/camera_output_capability.h"
16 #include "camera_log.h"
17 #include "camera_util.h"
18 
19 namespace OHOS {
20 namespace CameraStandard {
GetTargetRatio(ProfileSizeRatio sizeRatio,float unspecifiedValue)21 float GetTargetRatio(ProfileSizeRatio sizeRatio, float unspecifiedValue)
22 {
23     switch (sizeRatio) {
24         case RATIO_1_1:
25             return RATIO_VALUE_1_1;
26         case RATIO_4_3:
27             return RATIO_VALUE_4_3;
28         case RATIO_16_9:
29             return RATIO_VALUE_16_9;
30         case UNSPECIFIED:
31             return unspecifiedValue;
32         default:
33             return unspecifiedValue;
34     }
35     return unspecifiedValue;
36 }
37 
IsProfileSameRatio(Profile & srcProfile,ProfileSizeRatio sizeRatio,float unspecifiedValue)38 bool IsProfileSameRatio(Profile& srcProfile, ProfileSizeRatio sizeRatio, float unspecifiedValue)
39 {
40     CHECK_ERROR_RETURN_RET(srcProfile.size_.height == 0 || srcProfile.size_.width == 0, false);
41     float srcRatio = ((float)srcProfile.size_.width) / srcProfile.size_.height;
42     float targetRatio = GetTargetRatio(sizeRatio, unspecifiedValue);
43     CHECK_ERROR_RETURN_RET(targetRatio <= 0, false);
44     return abs(srcRatio - targetRatio) / targetRatio <= 0.05f; // 0.05f is 5% tolerance
45 }
46 
Profile(CameraFormat format,Size size)47 Profile::Profile(CameraFormat format, Size size) : format_(format), size_(size), specId_(0) {}
Profile(CameraFormat format,Size size,int32_t specId)48 Profile::Profile(CameraFormat format, Size size, int32_t specId) : format_(format), size_(size), specId_(specId) {}
Profile(CameraFormat format,Size size,Fps fps,std::vector<uint32_t> abilityId)49 Profile::Profile(CameraFormat format, Size size, Fps fps, std::vector<uint32_t> abilityId)
50     : format_(format), size_(size), fps_(fps), abilityId_(abilityId), specId_(0) {}
Profile(CameraFormat format,Size size,Fps fps,std::vector<uint32_t> abilityId,int32_t specId)51 Profile::Profile(CameraFormat format, Size size, Fps fps, std::vector<uint32_t> abilityId, int32_t specId)
52     : format_(format), size_(size), fps_(fps), abilityId_(abilityId), specId_(specId) {}
GetCameraFormat()53 CameraFormat Profile::GetCameraFormat()
54 {
55     return format_;
56 }
57 
GetAbilityId()58 std::vector<uint32_t> Profile::GetAbilityId()
59 {
60     return abilityId_;
61 }
62 
GetSize()63 Size Profile::GetSize()
64 {
65     return size_;
66 }
67 
GetFps()68 Fps Profile::GetFps()
69 {
70     return fps_;
71 }
72 
GetSpecId()73 int32_t Profile::GetSpecId()
74 {
75     return specId_;
76 }
77 
DumpProfile(std::string name) const78 void Profile::DumpProfile(std::string name) const
79 {
80     std::string abilityIdStr = Container2String(abilityId_.begin(), abilityId_.end());
81     MEDIA_DEBUG_LOG("%{public}s format : %{public}d, width: %{public}d, height: %{public}d, "
82                     "support ability: %{public}s, fixedFps: %{public}d, minFps: %{public}d, maxFps: %{public}d",
83                     name.c_str(), format_, size_.width, size_.height, abilityIdStr.c_str(),
84                     fps_.fixedFps, fps_.minFps, fps_.maxFps);
85 }
86 
DumpVideoProfile(std::string name) const87 void VideoProfile::DumpVideoProfile(std::string name) const
88 {
89     std::string frameratesStr = Container2String(framerates_.begin(), framerates_.end());
90     MEDIA_DEBUG_LOG("%{public}s format : %{public}d, width: %{public}d, height: %{public}d framerates: %{public}s",
91                     name.c_str(), format_, size_.width, size_.height, frameratesStr.c_str());
92 }
93 
VideoProfile(CameraFormat format,Size size,std::vector<int32_t> framerates)94 VideoProfile::VideoProfile(CameraFormat format, Size size, std::vector<int32_t> framerates) : Profile(format, size)
95 {
96     framerates_ = framerates;
97 }
98 
VideoProfile(CameraFormat format,Size size,std::vector<int32_t> framerates,int32_t specId)99 VideoProfile::VideoProfile(
100     CameraFormat format, Size size, std::vector<int32_t> framerates, int32_t specId) : Profile(format, size, specId)
101 {
102     framerates_ = framerates;
103 }
104 
IsContains(const VideoProfile & videoProfile)105 bool VideoProfile::IsContains(const VideoProfile& videoProfile)
106 {
107     bool isFormatSizeEqual = format_ == videoProfile.format_ && size_.width == videoProfile.size_.width &&
108                              size_.height == videoProfile.size_.height;
109     if (!isFormatSizeEqual) {
110         return false;
111     }
112     if (framerates_.empty()) {
113         return false;
114     }
115     if (videoProfile.framerates_.empty()) {
116         return true;
117     }
118     return *framerates_.begin() <= *videoProfile.framerates_.begin() &&
119            *(framerates_.end() - 1) >= *(videoProfile.framerates_.end() - 1);
120 }
121 
GetFrameRates()122 std::vector<int32_t> VideoProfile::GetFrameRates()
123 {
124     return framerates_;
125 }
126 
DepthProfile(CameraFormat format,DepthDataAccuracy dataAccuracy,Size size)127 DepthProfile::DepthProfile(CameraFormat format, DepthDataAccuracy dataAccuracy, Size size) : Profile(format, size)
128 {
129     dataAccuracy_ = dataAccuracy;
130 }
GetDataAccuracy()131 DepthDataAccuracy DepthProfile::GetDataAccuracy()
132 {
133     return dataAccuracy_;
134 }
135 
IsMatchPreviewProfiles(std::vector<Profile> & previewProfiles)136 bool CameraOutputCapability::IsMatchPreviewProfiles(std::vector<Profile>& previewProfiles)
137 {
138     CHECK_ERROR_RETURN_RET(previewProfiles.empty(), true);
139     CHECK_ERROR_RETURN_RET_LOG(previewProfiles_.empty(), false, "previewProfiles_ is empty, cant match");
140     for (auto& profile : previewProfiles) {
141         auto it = std::find(previewProfiles_.begin(), previewProfiles_.end(), profile);
142         if (it == previewProfiles_.end()) {
143             MEDIA_DEBUG_LOG("IsMatchPreviewProfiles previewProfile [format : %{public}d, width: %{public}d, "
144                 "height: %{public}d] cant match", profile.GetCameraFormat(), profile.GetSize().width,
145                 profile.GetSize().height);
146             return false;
147         }
148     }
149     MEDIA_DEBUG_LOG("IsMatchPreviewProfiles all previewProfiles can match");
150     return true;
151 }
152 
IsMatchPhotoProfiles(std::vector<Profile> & photoProfiles)153 bool CameraOutputCapability::IsMatchPhotoProfiles(std::vector<Profile>& photoProfiles)
154 {
155     CHECK_ERROR_RETURN_RET(photoProfiles.empty(), true);
156     CHECK_ERROR_RETURN_RET_LOG(photoProfiles_.empty(), false, "photoProfiles_ is empty, cant match");
157     for (auto& profile : photoProfiles) {
158         auto it = std::find(photoProfiles_.begin(), photoProfiles_.end(), profile);
159         if (it == photoProfiles_.end()) {
160             MEDIA_DEBUG_LOG("IsMatchPhotoProfiles photoProfile [format : %{public}d, width: %{public}d,"
161                 "height: %{public}d] cant match", profile.GetCameraFormat(), profile.GetSize().width,
162                 profile.GetSize().height);
163             return false;
164         }
165     }
166     MEDIA_DEBUG_LOG("IsMatchPhotoProfiles all photoProfiles can match");
167     return true;
168 }
169 
IsMatchVideoProfiles(std::vector<VideoProfile> & videoProfiles)170 bool CameraOutputCapability::IsMatchVideoProfiles(std::vector<VideoProfile>& videoProfiles)
171 {
172     CHECK_ERROR_RETURN_RET(videoProfiles.empty(), true);
173     CHECK_ERROR_RETURN_RET_LOG(videoProfiles_.empty(), false, "videoProfiles_ is empty, cant match");
174     for (auto& profile : videoProfiles) {
175         auto it = std::find_if(videoProfiles_.begin(), videoProfiles_.end(), [&profile](VideoProfile& profile_) {
176             return profile_.GetCameraFormat() == profile.GetCameraFormat() &&
177                    profile_.GetSize().width == profile.GetSize().width &&
178                    profile_.GetSize().height == profile.GetSize().height &&
179                    profile_.framerates_[0] <= profile.framerates_[0] &&
180                    profile_.framerates_[1] >= profile.framerates_[1];
181         });
182         if (it == videoProfiles_.end()) {
183             std::string frameratesStr = Container2String(profile.framerates_.begin(), profile.framerates_.end());
184             MEDIA_DEBUG_LOG("IsMatchVideoProfiles videoProfile [format : %{public}d, width: %{public}d,"
185                 "height: %{public}d framerates: %{public}s] cant match", profile.GetCameraFormat(),
186                 profile.GetSize().width, profile.GetSize().height, frameratesStr.c_str());
187             return false;
188         }
189     }
190     MEDIA_DEBUG_LOG("IsMatchVideoProfiles all videoProfiles can match");
191     return true;
192 }
193 
RemoveDuplicatesProfiles()194 void CameraOutputCapability::RemoveDuplicatesProfiles()
195 {
196     size_t previewSize = previewProfiles_.size();
197     size_t photoSize = photoProfiles_.size();
198     size_t videoSize = videoProfiles_.size();
199     RemoveDuplicatesProfile(previewProfiles_);
200     RemoveDuplicatesProfile(photoProfiles_);
201     RemoveDuplicatesProfile(videoProfiles_);
202     MEDIA_DEBUG_LOG("after remove duplicates preview size: %{public}zu -> %{public}zu, "
203                     "photo size: %{public}zu -> %{public}zu, video size:%{public}zu -> %{public}zu",
204                     previewSize, previewProfiles_.size(), photoSize, photoProfiles_.size(),
205                     videoSize, videoProfiles_.size());
206 }
207 
208 template <typename T>
RemoveDuplicatesProfile(std::vector<T> & profiles)209 void CameraOutputCapability::RemoveDuplicatesProfile(std::vector<T>& profiles)
210 {
211     std::vector<T> uniqueProfiles;
212     for (const auto& profile : profiles) {
213         if (std::find(uniqueProfiles.begin(), uniqueProfiles.end(), profile) == uniqueProfiles.end()) {
214             uniqueProfiles.push_back(profile);
215         }
216     }
217     profiles = uniqueProfiles;
218 }
219 
GetPhotoProfiles()220 std::vector<Profile> CameraOutputCapability::GetPhotoProfiles()
221 {
222     return photoProfiles_;
223 }
224 
SetPhotoProfiles(std::vector<Profile> photoProfiles)225 void CameraOutputCapability::SetPhotoProfiles(std::vector<Profile> photoProfiles)
226 {
227     photoProfiles_ = photoProfiles;
228 }
229 
GetPreviewProfiles()230 std::vector<Profile> CameraOutputCapability::GetPreviewProfiles()
231 {
232     return previewProfiles_;
233 }
234 
SetPreviewProfiles(std::vector<Profile> previewProfiles)235 void CameraOutputCapability::SetPreviewProfiles(std::vector<Profile> previewProfiles)
236 {
237     previewProfiles_ = previewProfiles;
238 }
239 
GetVideoProfiles()240 std::vector<VideoProfile> CameraOutputCapability::GetVideoProfiles()
241 {
242     return videoProfiles_;
243 }
244 
SetVideoProfiles(std::vector<VideoProfile> videoProfiles)245 void CameraOutputCapability::SetVideoProfiles(std::vector<VideoProfile> videoProfiles)
246 {
247     videoProfiles_ = videoProfiles;
248 }
249 
GetDepthProfiles()250 std::vector<DepthProfile> CameraOutputCapability::GetDepthProfiles()
251 {
252     return depthProfiles_;
253 }
254 
SetDepthProfiles(std::vector<DepthProfile> depthProfiles)255 void CameraOutputCapability::SetDepthProfiles(std::vector<DepthProfile> depthProfiles)
256 {
257     depthProfiles_ = depthProfiles;
258 }
259 
GetSupportedMetadataObjectType()260 std::vector<MetadataObjectType> CameraOutputCapability::GetSupportedMetadataObjectType()
261 {
262     return metadataObjTypes_;
263 }
264 
SetSupportedMetadataObjectType(std::vector<MetadataObjectType> metadataObjType)265 void CameraOutputCapability::SetSupportedMetadataObjectType(std::vector<MetadataObjectType> metadataObjType)
266 {
267     metadataObjTypes_ = metadataObjType;
268 }
269 } // namespace CameraStandard
270 } // namespace OHOS
271