• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "metadata_common_utils.h"
17 #include "camera_metadata_operator.h"
18 #include "camera_util.h"
19 #include <cstdint>
20 #include <memory>
21 
22 #include "camera_log.h"
23 #include "capture_session.h"
24 
25 namespace OHOS {
26 namespace CameraStandard {
27 namespace {
FillSizeListFromStreamInfo(vector<Size> & sizeList,const StreamInfo & streamInfo,const camera_format_t targetFormat)28 void FillSizeListFromStreamInfo(
29     vector<Size>& sizeList, const StreamInfo& streamInfo, const camera_format_t targetFormat)
30 {
31     for (const auto &detail : streamInfo.detailInfos) {
32         camera_format_t hdi_format = static_cast<camera_format_t>(detail.format);
33         if (hdi_format != targetFormat) {
34             continue;
35         }
36         Size size { .width = detail.width, .height = detail.height };
37         sizeList.emplace_back(size);
38     }
39 }
40 
FillSizeListFromStreamInfo(vector<Size> & sizeList,const StreamRelatedInfo & streamInfo,const camera_format_t targetFormat)41 void FillSizeListFromStreamInfo(
42     vector<Size>& sizeList, const StreamRelatedInfo& streamInfo, const camera_format_t targetFormat)
43 {
44     for (const auto &detail : streamInfo.detailInfo) {
45         camera_format_t hdi_format = static_cast<camera_format_t>(detail.format);
46         if (hdi_format != targetFormat) {
47             continue;
48         }
49         Size size{.width = detail.width, .height = detail.height};
50         sizeList.emplace_back(size);
51     }
52 }
53 
GetSupportedPreviewSizeRangeFromProfileLevel(const int32_t modeName,camera_format_t targetFormat,const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)54 std::shared_ptr<vector<Size>> GetSupportedPreviewSizeRangeFromProfileLevel(
55     const int32_t modeName, camera_format_t targetFormat, const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)
56 {
57     CHECK_ERROR_RETURN_RET(metadata == nullptr, nullptr);
58     camera_metadata_item_t item;
59     int32_t retCode = Camera::FindCameraMetadataItem(metadata->get(), OHOS_ABILITY_AVAILABLE_PROFILE_LEVEL, &item);
60     CHECK_ERROR_RETURN_RET(retCode != CAM_META_SUCCESS || item.count == 0, nullptr);
61     std::shared_ptr<vector<Size>> sizeList = std::make_shared<vector<Size>>();
62     std::vector<SpecInfo> specInfos;
63     ProfileLevelInfo modeInfo = {};
64     CameraAbilityParseUtil::GetModeInfo(modeName, item, modeInfo);
65     specInfos.insert(specInfos.end(), modeInfo.specInfos.begin(), modeInfo.specInfos.end());
66     for (SpecInfo& specInfo : specInfos) {
67         for (StreamInfo& streamInfo : specInfo.streamInfos) {
68             CHECK_EXECUTE(streamInfo.streamType == 0,
69                 FillSizeListFromStreamInfo(*sizeList.get(), streamInfo, targetFormat));
70         }
71     }
72     MEDIA_INFO_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRangeFromProfileLevel listSize: %{public}d",
73         static_cast<int>(sizeList->size()));
74     return sizeList;
75 }
76 
GetSupportedPreviewSizeRangeFromExtendConfig(const int32_t modeName,camera_format_t targetFormat,const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)77 std::shared_ptr<vector<Size>> GetSupportedPreviewSizeRangeFromExtendConfig(
78     const int32_t modeName, camera_format_t targetFormat, const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)
79 {
80     auto item = MetadataCommonUtils::GetCapabilityEntry(metadata, OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS);
81     CHECK_ERROR_RETURN_RET(item == nullptr, nullptr);
82     std::shared_ptr<vector<Size>> sizeList = std::make_shared<vector<Size>>();
83 
84     ExtendInfo extendInfo = {};
85     std::shared_ptr<CameraStreamInfoParse> modeStreamParse = std::make_shared<CameraStreamInfoParse>();
86     modeStreamParse->getModeInfo(item->data.i32, item->count, extendInfo); // 解析tag中带的数据信息意义
87     for (uint32_t modeIndex = 0; modeIndex < extendInfo.modeCount; modeIndex++) {
88         auto modeInfo = std::move(extendInfo.modeInfo[modeIndex]);
89         MEDIA_DEBUG_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRangeFromExtendConfig check modeName: %{public}d",
90             modeInfo.modeName);
91         if (modeName != modeInfo.modeName) {
92             continue;
93         }
94         for (uint32_t streamIndex = 0; streamIndex < modeInfo.streamTypeCount; streamIndex++) {
95             auto streamInfo = std::move(modeInfo.streamInfo[streamIndex]);
96             int32_t streamType = streamInfo.streamType;
97             MEDIA_DEBUG_LOG(
98                 "MetadataCommonUtils::GetSupportedPreviewSizeRangeFromExtendConfig check streamType: %{public}d",
99                 streamType);
100             if (streamType != 0) { // Stremp type 0 is preview type.
101                 continue;
102             }
103             FillSizeListFromStreamInfo(*sizeList.get(), streamInfo, targetFormat);
104         }
105     }
106     MEDIA_INFO_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRangeFromExtendConfig listSize: %{public}d",
107         static_cast<int>(sizeList->size()));
108     return sizeList;
109 }
110 
GetSupportedPreviewSizeRangeFromBasicConfig(camera_format_t targetFormat,const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)111 std::shared_ptr<vector<Size>> GetSupportedPreviewSizeRangeFromBasicConfig(
112     camera_format_t targetFormat, const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)
113 {
114     auto item = MetadataCommonUtils::GetCapabilityEntry(metadata, OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS);
115     CHECK_ERROR_RETURN_RET(item == nullptr, nullptr);
116     std::shared_ptr<vector<Size>> sizeList = std::make_shared<vector<Size>>();
117 
118     const uint32_t widthOffset = 1;
119     const uint32_t heightOffset = 2;
120     const uint32_t unitStep = 3;
121 
122     for (uint32_t i = 0; i < item->count; i += unitStep) {
123         camera_format_t hdi_format = static_cast<camera_format_t>(item->data.i32[i]);
124         if (hdi_format != targetFormat) {
125             continue;
126         }
127         Size size;
128         size.width = static_cast<uint32_t>(item->data.i32[i + widthOffset]);
129         size.height = static_cast<uint32_t>(item->data.i32[i + heightOffset]);
130         sizeList->emplace_back(size);
131     }
132     MEDIA_INFO_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRangeFromBasicConfig listSize: %{public}d",
133         static_cast<int>(sizeList->size()));
134     return sizeList;
135 }
136 } // namespace
137 
GetCapabilityEntry(const std::shared_ptr<Camera::CameraMetadata> metadata,uint32_t metadataTag)138 std::shared_ptr<camera_metadata_item_t> MetadataCommonUtils::GetCapabilityEntry(
139     const std::shared_ptr<Camera::CameraMetadata> metadata, uint32_t metadataTag)
140 {
141     CHECK_ERROR_RETURN_RET(metadata == nullptr, nullptr);
142     std::shared_ptr<camera_metadata_item_t> item = std::make_shared<camera_metadata_item_t>();
143     int32_t retCode = Camera::FindCameraMetadataItem(metadata->get(), metadataTag, item.get());
144     CHECK_ERROR_RETURN_RET(retCode != CAM_META_SUCCESS || item->count == 0, nullptr);
145     return item;
146 }
147 
GetSupportedPreviewSizeRange(const int32_t modeName,camera_format_t targetFormat,const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)148 std::shared_ptr<vector<Size>> MetadataCommonUtils::GetSupportedPreviewSizeRange(
149     const int32_t modeName, camera_format_t targetFormat, const std::shared_ptr<OHOS::Camera::CameraMetadata> metadata)
150 {
151     MEDIA_DEBUG_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRange modeName: %{public}d, targetFormat:%{public}d",
152         modeName, targetFormat);
153     std::shared_ptr<vector<Size>> sizeList = std::make_shared<vector<Size>>();
154     auto levelList = GetSupportedPreviewSizeRangeFromProfileLevel(modeName, targetFormat, metadata);
155     if (levelList && levelList->empty() && (modeName == SceneMode::CAPTURE || modeName == SceneMode::VIDEO)) {
156         levelList = GetSupportedPreviewSizeRangeFromProfileLevel(SceneMode::NORMAL, targetFormat, metadata);
157     }
158     if (levelList && !levelList->empty()) {
159         for (auto& size : *levelList) {
160             MEDIA_DEBUG_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRange level info:%{public}dx%{public}d",
161                 size.width, size.height);
162         }
163         sizeList->insert(sizeList->end(), levelList->begin(), levelList->end());
164         return sizeList;
165     }
166 
167     auto extendList = GetSupportedPreviewSizeRangeFromExtendConfig(modeName, targetFormat, metadata);
168     if (extendList != nullptr && extendList->empty() &&
169         (modeName == SceneMode::CAPTURE || modeName == SceneMode::VIDEO)) {
170         extendList = GetSupportedPreviewSizeRangeFromExtendConfig(SceneMode::NORMAL, targetFormat, metadata);
171     }
172     if (extendList != nullptr && !extendList->empty()) {
173         for (auto& size : *extendList) {
174             MEDIA_DEBUG_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRange extend info:%{public}dx%{public}d",
175                 size.width, size.height);
176         }
177         sizeList->insert(sizeList->end(), extendList->begin(), extendList->end());
178         return sizeList;
179     }
180     auto basicList = GetSupportedPreviewSizeRangeFromBasicConfig(targetFormat, metadata);
181     if (basicList != nullptr && !basicList->empty()) {
182         for (auto& size : *basicList) {
183             MEDIA_DEBUG_LOG("MetadataCommonUtils::GetSupportedPreviewSizeRange basic info:%{public}dx%{public}d",
184                 size.width, size.height);
185         }
186         sizeList->insert(sizeList->end(), basicList->begin(), basicList->end());
187     }
188     return sizeList;
189 }
190 
CopyMetadata(const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata)191 std::shared_ptr<OHOS::Camera::CameraMetadata> MetadataCommonUtils::CopyMetadata(
192     const std::shared_ptr<OHOS::Camera::CameraMetadata> srcMetadata)
193 {
194     CHECK_ERROR_RETURN_RET_LOG(srcMetadata == nullptr, nullptr, "CopyMetadata fail,src is null");
195     auto oldMetadata = srcMetadata->get();
196     CHECK_ERROR_RETURN_RET(oldMetadata == nullptr, nullptr);
197     std::shared_ptr<OHOS::Camera::CameraMetadata> result =
198         std::make_shared<OHOS::Camera::CameraMetadata>(oldMetadata->item_capacity, oldMetadata->data_capacity);
199     auto newMetadata = result->get();
200     int32_t ret = OHOS::Camera::CopyCameraMetadataItems(newMetadata, oldMetadata);
201     CHECK_ERROR_PRINT_LOG(ret != CAM_META_SUCCESS, "CopyCameraMetadataItems failed ret:%{public}d", ret);
202     return result;
203 }
204 
ParsePhysicalApertureRangeByMode(const camera_metadata_item_t & item,const int32_t modeName)205 std::vector<float> ParsePhysicalApertureRangeByMode(const camera_metadata_item_t &item, const int32_t modeName)
206 {
207     const float factor = 20.0;
208     std::vector<float> allRange = {};
209     for (uint32_t i = 0; i < item.count; i++) {
210         allRange.push_back(item.data.f[i] * factor);
211     }
212     MEDIA_DEBUG_LOG("ParsePhysicalApertureRangeByMode allRange=%{public}s",
213                     Container2String(allRange.begin(), allRange.end()).c_str());
214     float npos = -1.0;
215     std::vector<std::vector<float>> modeRanges = {};
216     std::vector<float> modeRange = {};
217     for (uint32_t i = 0; i < item.count - 1; i++) {
218         if (item.data.f[i] == npos && item.data.f[i + 1] == npos) {
219             modeRange.emplace_back(npos);
220             MEDIA_DEBUG_LOG("ParsePhysicalApertureRangeByMode mode %{public}d, modeRange=%{public}s",
221                             modeName, Container2String(modeRange.begin(), modeRange.end()).c_str());
222             modeRanges.emplace_back(std::move(modeRange));
223             modeRange.clear();
224             i++;
225             continue;
226         }
227         modeRange.emplace_back(item.data.f[i]);
228     }
229     float currentMode = static_cast<float>(modeName);
230     auto it = std::find_if(modeRanges.begin(), modeRanges.end(),
231         [currentMode](auto value) -> bool {
232             return currentMode == value[0];
233         });
234     CHECK_ERROR_RETURN_RET_LOG(it == modeRanges.end(), {},
235         "ParsePhysicalApertureRangeByMode Failed meta not support mode:%{public}d", modeName);
236 
237     return *it;
238 }
239 
GetMetadataItem(const common_metadata_header_t * src,uint32_t tag)240 std::shared_ptr<camera_metadata_item_t> GetMetadataItem(const common_metadata_header_t* src, uint32_t tag)
241 {
242     CHECK_ERROR_RETURN_RET(src == nullptr, nullptr);
243     auto item = std::make_shared<camera_metadata_item_t>();
244     int32_t ret = OHOS::Camera::CameraMetadata::FindCameraMetadataItem(src, tag, item.get());
245     CHECK_ERROR_RETURN_RET(ret != CAM_META_SUCCESS, nullptr);
246     return item;
247 }
248 } // namespace CameraStandard
249 } // namespace OHOS
250