• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     CHECK_ERROR_RETURN_RET(!isFormatSizeEqual, false);
110     CHECK_ERROR_RETURN_RET(framerates_.empty(), false);
111     CHECK_ERROR_RETURN_RET(videoProfile.framerates_.empty(), true);
112     return *framerates_.begin() <= *videoProfile.framerates_.begin() &&
113            *(framerates_.end() - 1) >= *(videoProfile.framerates_.end() - 1);
114 }
115 
GetFrameRates()116 std::vector<int32_t> VideoProfile::GetFrameRates()
117 {
118     return framerates_;
119 }
120 
DepthProfile(CameraFormat format,DepthDataAccuracy dataAccuracy,Size size)121 DepthProfile::DepthProfile(CameraFormat format, DepthDataAccuracy dataAccuracy, Size size) : Profile(format, size)
122 {
123     dataAccuracy_ = dataAccuracy;
124 }
GetDataAccuracy()125 DepthDataAccuracy DepthProfile::GetDataAccuracy()
126 {
127     return dataAccuracy_;
128 }
129 
IsMatchPreviewProfiles(std::vector<Profile> & previewProfiles)130 bool CameraOutputCapability::IsMatchPreviewProfiles(std::vector<Profile>& previewProfiles)
131 {
132     CHECK_ERROR_RETURN_RET(previewProfiles.empty(), true);
133     CHECK_ERROR_RETURN_RET_LOG(previewProfiles_.empty(), false, "previewProfiles_ is empty, cant match");
134     for (auto& profile : previewProfiles) {
135         auto it = std::find(previewProfiles_.begin(), previewProfiles_.end(), profile);
136         if (it == previewProfiles_.end()) {
137             MEDIA_DEBUG_LOG("IsMatchPreviewProfiles previewProfile [format : %{public}d, width: %{public}d, "
138                 "height: %{public}d] cant match", profile.GetCameraFormat(), profile.GetSize().width,
139                 profile.GetSize().height);
140             return false;
141         }
142     }
143     MEDIA_DEBUG_LOG("IsMatchPreviewProfiles all previewProfiles can match");
144     return true;
145 }
146 
IsMatchPhotoProfiles(std::vector<Profile> & photoProfiles)147 bool CameraOutputCapability::IsMatchPhotoProfiles(std::vector<Profile>& photoProfiles)
148 {
149     CHECK_ERROR_RETURN_RET(photoProfiles.empty(), true);
150     CHECK_ERROR_RETURN_RET_LOG(photoProfiles_.empty(), false, "photoProfiles_ is empty, cant match");
151     for (auto& profile : photoProfiles) {
152         auto it = std::find(photoProfiles_.begin(), photoProfiles_.end(), profile);
153         if (it == photoProfiles_.end()) {
154             MEDIA_DEBUG_LOG("IsMatchPhotoProfiles photoProfile [format : %{public}d, width: %{public}d,"
155                 "height: %{public}d] cant match", profile.GetCameraFormat(), profile.GetSize().width,
156                 profile.GetSize().height);
157             return false;
158         }
159     }
160     MEDIA_DEBUG_LOG("IsMatchPhotoProfiles all photoProfiles can match");
161     return true;
162 }
163 
IsMatchVideoProfiles(std::vector<VideoProfile> & videoProfiles)164 bool CameraOutputCapability::IsMatchVideoProfiles(std::vector<VideoProfile>& videoProfiles)
165 {
166     CHECK_ERROR_RETURN_RET(videoProfiles.empty(), true);
167     CHECK_ERROR_RETURN_RET_LOG(videoProfiles_.empty(), false, "videoProfiles_ is empty, cant match");
168     for (auto& profile : videoProfiles) {
169         auto it = std::find_if(videoProfiles_.begin(), videoProfiles_.end(), [&profile](VideoProfile& profile_) {
170             return profile_.GetCameraFormat() == profile.GetCameraFormat() &&
171                    profile_.GetSize().width == profile.GetSize().width &&
172                    profile_.GetSize().height == profile.GetSize().height &&
173                    profile_.framerates_[0] <= profile.framerates_[0] &&
174                    profile_.framerates_[1] >= profile.framerates_[1];
175         });
176         if (it == videoProfiles_.end()) {
177             std::string frameratesStr = Container2String(profile.framerates_.begin(), profile.framerates_.end());
178             MEDIA_DEBUG_LOG("IsMatchVideoProfiles videoProfile [format : %{public}d, width: %{public}d,"
179                 "height: %{public}d framerates: %{public}s] cant match", profile.GetCameraFormat(),
180                 profile.GetSize().width, profile.GetSize().height, frameratesStr.c_str());
181             return false;
182         }
183     }
184     MEDIA_DEBUG_LOG("IsMatchVideoProfiles all videoProfiles can match");
185     return true;
186 }
187 
RemoveDuplicatesProfiles()188 void CameraOutputCapability::RemoveDuplicatesProfiles()
189 {
190     size_t previewSize = previewProfiles_.size();
191     size_t photoSize = photoProfiles_.size();
192     size_t videoSize = videoProfiles_.size();
193     RemoveDuplicatesProfile(previewProfiles_);
194     RemoveDuplicatesProfile(photoProfiles_);
195     RemoveDuplicatesProfile(videoProfiles_);
196     MEDIA_DEBUG_LOG("after remove duplicates preview size: %{public}zu -> %{public}zu, "
197                     "photo size: %{public}zu -> %{public}zu, video size:%{public}zu -> %{public}zu",
198                     previewSize, previewProfiles_.size(), photoSize, photoProfiles_.size(),
199                     videoSize, videoProfiles_.size());
200 }
201 
202 template <typename T>
RemoveDuplicatesProfile(std::vector<T> & profiles)203 void CameraOutputCapability::RemoveDuplicatesProfile(std::vector<T>& profiles)
204 {
205     std::vector<T> uniqueProfiles;
206     for (const auto& profile : profiles) {
207         CHECK_EXECUTE(std::find(uniqueProfiles.begin(), uniqueProfiles.end(), profile) == uniqueProfiles.end(),
208             uniqueProfiles.push_back(profile));
209     }
210     profiles = uniqueProfiles;
211 }
212 
GetPhotoProfiles()213 std::vector<Profile> CameraOutputCapability::GetPhotoProfiles()
214 {
215     return photoProfiles_;
216 }
217 
SetPhotoProfiles(std::vector<Profile> photoProfiles)218 void CameraOutputCapability::SetPhotoProfiles(std::vector<Profile> photoProfiles)
219 {
220     photoProfiles_ = photoProfiles;
221 }
222 
GetPreviewProfiles()223 std::vector<Profile> CameraOutputCapability::GetPreviewProfiles()
224 {
225     return previewProfiles_;
226 }
227 
SetPreviewProfiles(std::vector<Profile> previewProfiles)228 void CameraOutputCapability::SetPreviewProfiles(std::vector<Profile> previewProfiles)
229 {
230     previewProfiles_ = previewProfiles;
231 }
232 
GetVideoProfiles()233 std::vector<VideoProfile> CameraOutputCapability::GetVideoProfiles()
234 {
235     return videoProfiles_;
236 }
237 
SetVideoProfiles(std::vector<VideoProfile> videoProfiles)238 void CameraOutputCapability::SetVideoProfiles(std::vector<VideoProfile> videoProfiles)
239 {
240     videoProfiles_ = videoProfiles;
241 }
242 
GetDepthProfiles()243 std::vector<DepthProfile> CameraOutputCapability::GetDepthProfiles()
244 {
245     return depthProfiles_;
246 }
247 
SetDepthProfiles(std::vector<DepthProfile> depthProfiles)248 void CameraOutputCapability::SetDepthProfiles(std::vector<DepthProfile> depthProfiles)
249 {
250     depthProfiles_ = depthProfiles;
251 }
252 
GetSupportedMetadataObjectType()253 std::vector<MetadataObjectType> CameraOutputCapability::GetSupportedMetadataObjectType()
254 {
255     return metadataObjTypes_;
256 }
257 
SetSupportedMetadataObjectType(std::vector<MetadataObjectType> metadataObjType)258 void CameraOutputCapability::SetSupportedMetadataObjectType(std::vector<MetadataObjectType> metadataObjType)
259 {
260     metadataObjTypes_ = metadataObjType;
261 }
262 } // namespace CameraStandard
263 } // namespace OHOS
264