1 /*
2 * Copyright (c) 2021-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 "camera_metadata_operator.h"
17 #include <securec.h>
18 #include <vector>
19 #include <map>
20 #include "camera_metadata_item_info.h"
21 #include "metadata_log.h"
22
23 namespace OHOS::Camera {
24 const int METADATA_HEADER_DATA_SIZE = 4;
25 const std::vector<int32_t> METADATATAGS = {
26 OHOS_ABILITY_CAMERA_POSITION,
27 OHOS_ABILITY_CAMERA_TYPE,
28 OHOS_ABILITY_CAMERA_CONNECTION_TYPE,
29 OHOS_ABILITY_MEMORY_TYPE,
30 OHOS_ABILITY_FPS_RANGES,
31 OHOS_ABILITY_FLASH_AVAILABLE,
32 OHOS_CAMERA_STREAM_ID,
33
34 OHOS_SENSOR_EXPOSURE_TIME,
35 OHOS_SENSOR_COLOR_CORRECTION_GAINS,
36 OHOS_SENSOR_ORIENTATION,
37 OHOS_ABILITY_MUTE_MODES,
38 OHOS_CONTROL_MUTE_MODE,
39
40 OHOS_SENSOR_INFO_ACTIVE_ARRAY_SIZE,
41 OHOS_SENSOR_INFO_SENSITIVITY_RANGE,
42 OHOS_SENSOR_INFO_MAX_FRAME_DURATION,
43 OHOS_SENSOR_INFO_PHYSICAL_SIZE,
44 OHOS_SENSOR_INFO_PIXEL_ARRAY_SIZE,
45 OHOS_SENSOR_INFO_TIMESTAMP,
46
47 OHOS_STATISTICS_FACE_DETECT_MODE,
48 OHOS_STATISTICS_FACE_DETECT_SWITCH,
49 OHOS_STATISTICS_FACE_DETECT_MAX_NUM,
50 OHOS_STATISTICS_HISTOGRAM_MODE,
51 OHOS_STATISTICS_FACE_IDS,
52 OHOS_STATISTICS_FACE_LANDMARKS,
53 OHOS_STATISTICS_FACE_RECTANGLES,
54 OHOS_STATISTICS_FACE_SCORES,
55
56 OHOS_CONTROL_AE_ANTIBANDING_MODE,
57 OHOS_CONTROL_AE_EXPOSURE_COMPENSATION,
58 OHOS_CONTROL_AE_LOCK,
59 OHOS_CONTROL_AE_MODE,
60 OHOS_CONTROL_AE_REGIONS,
61 OHOS_CONTROL_AE_TARGET_FPS_RANGE,
62 OHOS_CONTROL_FPS_RANGES,
63 OHOS_CONTROL_AF_MODE,
64 OHOS_CONTROL_AF_REGIONS,
65 OHOS_CONTROL_AF_TRIGGER,
66 OHOS_CONTROL_AF_TRIGGER_ID,
67 OHOS_CONTROL_AF_STATE,
68 OHOS_CONTROL_AWB_LOCK,
69 OHOS_CONTROL_AWB_MODE,
70 OHOS_CONTROL_AWB_REGIONS,
71 OHOS_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
72 OHOS_CONTROL_AE_AVAILABLE_MODES,
73 OHOS_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
74 OHOS_CONTROL_AE_COMPENSATION_RANGE,
75 OHOS_CONTROL_AE_COMPENSATION_STEP,
76 OHOS_CONTROL_AF_AVAILABLE_MODES,
77 OHOS_CONTROL_AWB_AVAILABLE_MODES,
78 OHOS_CONTROL_CAPTURE_MIRROR_SUPPORTED,
79 OHOS_CONTROL_CAPTURE_MIRROR,
80 OHOS_CONTROL_FOCUS_STATE,
81 OHOS_CONTROL_METER_POINT,
82 OHOS_CONTROL_METER_MODE,
83 OHOS_CONTROL_EXPOSURE_STATE,
84
85 // Camera device image acquisition related
86 OHOS_ABILITY_DEVICE_AVAILABLE_EXPOSUREMODES,
87 OHOS_CONTROL_EXPOSUREMODE,
88 OHOS_ABILITY_EXPOSURE_MODES,
89 OHOS_CONTROL_EXPOSURE_MODE,
90 OHOS_ABILITY_METER_MODES,
91
92 OHOS_ABILITY_DEVICE_AVAILABLE_FOCUSMODES,
93 OHOS_CONTROL_FOCUSMODE,
94 OHOS_ABILITY_FOCUS_MODES,
95 OHOS_CONTROL_FOCUS_MODE,
96 OHOS_ABILITY_FOCAL_LENGTH,
97
98 OHOS_ABILITY_DEVICE_AVAILABLE_FLASHMODES,
99 OHOS_CONTROL_FLASHMODE,
100 OHOS_ABILITY_FLASH_MODES,
101 OHOS_CONTROL_FLASH_MODE,
102 OHOS_CONTROL_FLASH_STATE,
103
104 OHOS_ABILITY_ZOOM_RATIO_RANGE,
105 OHOS_CONTROL_ZOOM_RATIO,
106 OHOS_CONTROL_ZOOM_CROP_REGION,
107 OHOS_ABILITY_ZOOM_CAP,
108 OHOS_ABILITY_SCENE_ZOOM_CAP,
109
110 // Camera flow control related
111 OHOS_ABILITY_STREAM_AVAILABLE_BASIC_CONFIGURATIONS,
112 OHOS_STREAM_AVAILABLE_FORMATS,
113 OHOS_ABILITY_STREAM_AVAILABLE_EXTEND_CONFIGURATIONS,
114
115 OHOS_JPEG_GPS_COORDINATES,
116 OHOS_JPEG_GPS_PROCESSING_METHOD,
117 OHOS_JPEG_GPS_TIMESTAMP,
118 OHOS_JPEG_ORIENTATION,
119 OHOS_JPEG_QUALITY,
120 OHOS_JPEG_THUMBNAIL_QUALITY,
121 OHOS_JPEG_THUMBNAIL_SIZE,
122 OHOS_JPEG_AVAILABLE_THUMBNAIL_SIZES,
123 OHOS_JPEG_MAX_SIZE,
124 OHOS_JPEG_SIZE,
125
126 OHOS_ABILITY_VIDEO_STABILIZATION_MODES,
127 OHOS_CONTROL_VIDEO_STABILIZATION_MODE,
128
129 // camera effects & algorithms
130 OHOS_ABILITY_SCENE_FILTER_TYPES,
131 OHOS_CONTROL_FILTER_TYPE,
132 OHOS_ABILITY_SCENE_PORTRAIT_EFFECT_TYPES,
133 OHOS_CONTROL_PORTRAIT_EFFECT_TYPE,
134 OHOS_ABILITY_SCENE_BEAUTY_TYPES,
135 OHOS_CONTROL_BEAUTY_TYPE,
136 OHOS_ABILITY_BEAUTY_AUTO_VALUES,
137 OHOS_CONTROL_BEAUTY_AUTO_VALUE,
138 OHOS_ABILITY_BEAUTY_FACE_SLENDER_VALUES,
139 OHOS_CONTROL_BEAUTY_FACE_SLENDER_VALUE,
140 OHOS_ABILITY_BEAUTY_SKIN_TONE_VALUES,
141 OHOS_CONTROL_BEAUTY_SKIN_TONE_VALUE,
142 OHOS_ABILITY_BEAUTY_SKIN_SMOOTH_VALUES,
143 OHOS_CONTROL_BEAUTY_SKIN_SMOOTH_VALUE,
144
145 // camera secure related
146 OHOS_CONTROL_SECURE_FACE_MODE,
147 OHOS_CONTROL_SECURE_FACE_INFO,
148 OHOS_CONTROL_SECURE_FACE_AUTH_RESULT,
149 OHOS_CONTROL_SECURE_FLASH_SEQ,
150 OHOS_CONTROL_SECURE_IR_LED_SWITCH,
151 OHOS_CONTROL_SECURE_IR_LOCKAE_SWITCH,
152 };
153
154 std::map<uint32_t, uint32_t> g_metadataSectionMap = {
155 {OHOS_CAMERA_PROPERTIES, OHOS_SECTION_CAMERA_PROPERTIES},
156 {OHOS_CAMERA_SENSOR, OHOS_SECTION_CAMERA_SENSOR},
157 {OHOS_CAMERA_SENSOR_INFO, OHOS_SECTION_CAMERA_SENSOR_INFO},
158 {OHOS_CAMERA_STATISTICS, OHOS_SECTION_CAMERA_STATISTICS},
159 {OHOS_DEVICE_CONTROL, OHOS_SECTION_CAMERA_CONTROL},
160 {OHOS_DEVICE_EXPOSURE, OHOS_SECTION_DEVICE_EXPOSURE},
161 {OHOS_DEVICE_FOCUS, OHOS_SECTION_DEVICE_FOCUS},
162 {OHOS_DEVICE_FLASH, OHOS_SECTION_DEVICE_FLASH},
163 {OHOS_DEVICE_ZOOM, OHOS_SECTION_DEVICE_ZOOM},
164 {OHOS_STREAM_ABILITY, OHOS_SECTION_STREAM_ABILITY},
165 {OHOS_STREAM_JPEG, OHOS_SECTION_STREAM_JPEG},
166 {OHOS_STREAM_VIDEO, OHOS_SECTION_STREAM_VIDEO},
167 {OHOS_CAMERA_EFFECT, OHOS_SECTION_CAMERA_EFFECT},
168 {OHOS_CAMERA_SECURE, OHOS_SECTION_CAMERA_SECURE}
169 };
AlignTo(uint32_t val,uint32_t alignment)170 uint32_t AlignTo(uint32_t val, uint32_t alignment)
171 {
172 return static_cast<uint32_t>((static_cast<uintptr_t>(val) + ((alignment) - 1)) & ~((alignment) - 1));
173 }
174
MaxAlignment(uint32_t dataAlignment,uint32_t metadataAlignment)175 uint32_t MaxAlignment(uint32_t dataAlignment, uint32_t metadataAlignment)
176 {
177 return ((dataAlignment > metadataAlignment) ? dataAlignment : metadataAlignment);
178 }
179
GetMetadataData(const common_metadata_header_t * metadataHeader)180 uint8_t *GetMetadataData(const common_metadata_header_t *metadataHeader)
181 {
182 if (!metadataHeader) {
183 METADATA_ERR_LOG("GetMetadataData metadataHeader is null");
184 return nullptr;
185 }
186 return (uint8_t *)metadataHeader + metadataHeader->data_start;
187 }
188
GetMetadataItems(const common_metadata_header_t * metadataHeader)189 camera_metadata_item_entry_t *GetMetadataItems(const common_metadata_header_t *metadataHeader)
190 {
191 return reinterpret_cast<camera_metadata_item_entry_t *>(
192 (reinterpret_cast<uint8_t *>(const_cast<common_metadata_header_t *>(metadataHeader)) +
193 metadataHeader->items_start));
194 }
195
FillCameraMetadata(common_metadata_header_t * buffer,size_t memoryRequired,uint32_t itemCapacity,uint32_t dataCapacity)196 common_metadata_header_t *FillCameraMetadata(common_metadata_header_t *buffer, size_t memoryRequired,
197 uint32_t itemCapacity, uint32_t dataCapacity)
198 {
199 METADATA_DEBUG_LOG("FillCameraMetadata start");
200 if (buffer == nullptr) {
201 METADATA_ERR_LOG("FillCameraMetadata buffer is null");
202 return nullptr;
203 }
204
205 common_metadata_header_t *metadataHeader = static_cast<common_metadata_header_t *>(buffer);
206 metadataHeader->version = CURRENT_CAMERA_METADATA_VERSION;
207 metadataHeader->size = memoryRequired;
208 metadataHeader->item_count = 0;
209 metadataHeader->item_capacity = itemCapacity;
210 metadataHeader->items_start = AlignTo(sizeof(common_metadata_header_t), ITEM_ALIGNMENT);
211 metadataHeader->data_count = 0;
212 metadataHeader->data_capacity = dataCapacity;
213 size_t dataUnaligned = reinterpret_cast<uint8_t *>(GetMetadataItems(metadataHeader) +
214 metadataHeader->item_capacity) - reinterpret_cast<uint8_t *>(metadataHeader);
215 metadataHeader->data_start = AlignTo(dataUnaligned, DATA_ALIGNMENT);
216
217 METADATA_DEBUG_LOG("FillCameraMetadata end");
218 return metadataHeader;
219 }
220
CalculateCameraMetadataMemoryRequired(uint32_t itemCount,uint32_t dataCount)221 size_t CalculateCameraMetadataMemoryRequired(uint32_t itemCount, uint32_t dataCount)
222 {
223 METADATA_DEBUG_LOG("CalculateCameraMetadataMemoryRequired start");
224 size_t memoryRequired = sizeof(common_metadata_header_t);
225 memoryRequired = AlignTo(memoryRequired, ITEM_ALIGNMENT);
226
227 memoryRequired += sizeof(camera_metadata_item_entry_t[itemCount]);
228 memoryRequired = AlignTo(memoryRequired, DATA_ALIGNMENT);
229
230 memoryRequired += sizeof(uint8_t[dataCount]);
231 memoryRequired = AlignTo(memoryRequired, METADATA_PACKET_ALIGNMENT);
232
233 METADATA_DEBUG_LOG("CalculateCameraMetadataMemoryRequired memoryRequired: %{public}zu", memoryRequired);
234 METADATA_DEBUG_LOG("CalculateCameraMetadataMemoryRequired end");
235 return memoryRequired;
236 }
237
AllocateCameraMetadataBuffer(uint32_t item_capacity,uint32_t data_capacity)238 common_metadata_header_t *AllocateCameraMetadataBuffer(uint32_t item_capacity, uint32_t data_capacity)
239 {
240 METADATA_DEBUG_LOG("AllocateCameraMetadataBuffer start");
241 METADATA_DEBUG_LOG("AllocateCameraMetadataBuffer item_capacity: %{public}u, data_capacity: %{public}u",
242 item_capacity, data_capacity);
243 size_t memoryRequired = CalculateCameraMetadataMemoryRequired(item_capacity, data_capacity);
244 void *buffer = calloc(1, memoryRequired);
245 if (buffer == nullptr) {
246 METADATA_ERR_LOG("AllocateCameraMetadataBuffer memory allocation failed");
247 return reinterpret_cast<common_metadata_header_t *>(buffer);
248 }
249
250 common_metadata_header_t *metadataHeader = FillCameraMetadata(reinterpret_cast<common_metadata_header_t *>(buffer),
251 memoryRequired,
252 item_capacity, data_capacity);
253 if (metadataHeader == nullptr) {
254 METADATA_ERR_LOG("AllocateCameraMetadataBuffer metadataHeader is null");
255 free(buffer);
256 }
257
258 METADATA_DEBUG_LOG("AllocateCameraMetadataBuffer end");
259 return metadataHeader;
260 }
261
GetMetadataSection(uint32_t itemSection,uint32_t * section)262 int32_t GetMetadataSection(uint32_t itemSection, uint32_t *section)
263 {
264 METADATA_DEBUG_LOG("GetMetadataSection start");
265 if (itemSection < OHOS_CAMERA_PROPERTIES ||
266 itemSection >= OHOS_ABILITY_SECTION_END) {
267 METADATA_ERR_LOG("GetMetadataSection itemSection is not valid");
268 return CAM_META_FAILURE;
269 }
270
271 int32_t ret = CAM_META_SUCCESS;
272 auto iter = g_metadataSectionMap.find(itemSection);
273 if (iter != g_metadataSectionMap.end()) {
274 *section = iter->second;
275 } else {
276 METADATA_ERR_LOG("GetMetadataSection item section is not defined");
277 ret = CAM_META_FAILURE;
278 }
279
280 METADATA_DEBUG_LOG("GetMetadataSection end");
281 return ret;
282 }
283
GetCameraMetadataItemType(uint32_t item,uint32_t * dataType)284 int32_t GetCameraMetadataItemType(uint32_t item, uint32_t *dataType)
285 {
286 METADATA_DEBUG_LOG("GetCameraMetadataItemType start");
287 if (dataType == nullptr) {
288 METADATA_ERR_LOG("GetCameraMetadataItemType dataType is null");
289 return CAM_META_INVALID_PARAM;
290 }
291 uint32_t section;
292 int32_t ret = GetMetadataSection(item >> BITWISE_SHIFT_16, §ion);
293 if (ret != CAM_META_SUCCESS) {
294 METADATA_ERR_LOG("GetCameraMetadataItemType section is not valid");
295 return ret;
296 }
297
298 if (item >= g_ohosCameraSectionBounds[section][1]) {
299 METADATA_ERR_LOG("GetCameraMetadataItemType item is not in section bound");
300 return CAM_META_FAILURE;
301 }
302
303 uint32_t itemIndex = item & 0xFFFF;
304 if (g_ohosItemInfo[section][itemIndex].item_type < META_TYPE_BYTE) {
305 METADATA_ERR_LOG("GetCameraMetadataItemType item is not initialized");
306 return CAM_META_FAILURE;
307 }
308
309 *dataType = g_ohosItemInfo[section][itemIndex].item_type;
310
311 METADATA_DEBUG_LOG("GetCameraMetadataItemType end");
312 return CAM_META_SUCCESS;
313 }
314
GetCameraMetadataItemName(uint32_t item)315 const char *GetCameraMetadataItemName(uint32_t item)
316 {
317 METADATA_DEBUG_LOG("GetCameraMetadataItemName start");
318 METADATA_DEBUG_LOG("GetCameraMetadataItemName item: %{public}u", item);
319 uint32_t section;
320 int32_t ret = GetMetadataSection(item >> BITWISE_SHIFT_16, §ion);
321 if (ret != CAM_META_SUCCESS) {
322 METADATA_ERR_LOG("GetCameraMetadataItemName section is not valid");
323 return nullptr;
324 }
325
326 if (item >= g_ohosCameraSectionBounds[section][1]) {
327 METADATA_ERR_LOG("GetCameraMetadataItemName item is not in section bound");
328 return nullptr;
329 }
330
331 uint32_t itemIndex = item & 0xFFFF;
332 METADATA_DEBUG_LOG("GetCameraMetadataItemName end");
333 return g_ohosItemInfo[section][itemIndex].item_name;
334 }
335
CalculateCameraMetadataItemDataSize(uint32_t type,size_t dataCount)336 size_t CalculateCameraMetadataItemDataSize(uint32_t type, size_t dataCount)
337 {
338 METADATA_DEBUG_LOG("CalculateCameraMetadataItemDataSize start");
339 if (type < META_TYPE_BYTE || type >= META_NUM_TYPES) {
340 METADATA_ERR_LOG("CalculateCameraMetadataItemDataSize invalid type");
341 return 0;
342 }
343
344 size_t dataBytes = dataCount * OHOS_CAMERA_METADATA_TYPE_SIZE[type];
345
346 METADATA_DEBUG_LOG("CalculateCameraMetadataItemDataSize end");
347 return (dataBytes <= METADATA_HEADER_DATA_SIZE) ? 0 : AlignTo(dataBytes, DATA_ALIGNMENT);
348 }
349
AddCameraMetadataItem(common_metadata_header_t * dst,uint32_t item,const void * data,size_t dataCount)350 int AddCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data, size_t dataCount)
351 {
352 METADATA_DEBUG_LOG("AddCameraMetadataItem start");
353 const char *name = GetCameraMetadataItemName(item);
354 if (name == nullptr) {
355 name = "<unknown>";
356 }
357 METADATA_DEBUG_LOG("AddCameraMetadataItem item id: %{public}u, name: %{public}s, "
358 "dataCount: %{public}zu", item, name, dataCount);
359
360 if (dst == nullptr) {
361 METADATA_ERR_LOG("AddCameraMetadataItem common_metadata_header_t is null");
362 return CAM_META_INVALID_PARAM;
363 }
364
365 if (!dataCount || data == nullptr) {
366 METADATA_ERR_LOG("AddCameraMetadataItem data is not valid. item: %{public}u, "
367 "dataCount: %{public}zu", item, dataCount);
368 return CAM_META_INVALID_PARAM;
369 }
370
371 if (dst->item_count == dst->item_capacity) {
372 METADATA_ERR_LOG("AddCameraMetadataItem item_capacity limit reached. "
373 "item_count: %{public}d, item_capacity: %{public}d",
374 dst->item_count, dst->item_capacity);
375 return CAM_META_ITEM_CAP_EXCEED;
376 }
377
378 uint32_t dataType;
379 int32_t ret = GetCameraMetadataItemType(item, &dataType);
380 if (ret != CAM_META_SUCCESS) {
381 METADATA_ERR_LOG("AddCameraMetadataItem invalid item type");
382 return CAM_META_INVALID_PARAM;
383 }
384
385 size_t dataBytes =
386 CalculateCameraMetadataItemDataSize(dataType, dataCount);
387 if (dataBytes + dst->data_count > dst->data_capacity) {
388 METADATA_ERR_LOG("AddCameraMetadataItem data_capacity limit reached");
389 return CAM_META_DATA_CAP_EXCEED;
390 }
391
392 size_t dataPayloadBytes =
393 dataCount * OHOS_CAMERA_METADATA_TYPE_SIZE[dataType];
394 camera_metadata_item_entry_t *metadataItem = GetMetadataItems(dst) + dst->item_count;
395 ret = memset_s(metadataItem, sizeof(camera_metadata_item_entry_t), 0, sizeof(camera_metadata_item_entry_t));
396 if (ret != EOK) {
397 METADATA_ERR_LOG("AddCameraMetadataItem: memset_s failed");
398 return CAM_META_FAILURE;
399 }
400 metadataItem->item = item;
401 metadataItem->data_type = dataType;
402 metadataItem->count = dataCount;
403
404 if (dataBytes == 0) {
405 ret = memcpy_s(metadataItem->data.value, METADATA_HEADER_DATA_SIZE, data, dataPayloadBytes);
406 if (ret != EOK) {
407 METADATA_ERR_LOG("AddCameraMetadataItem memory copy failed");
408 return CAM_META_FAILURE;
409 }
410 } else {
411 metadataItem->data.offset = dst->data_count;
412 ret = memcpy_s(GetMetadataData(dst) + metadataItem->data.offset, dst->data_capacity - dst->data_count, data,
413 dataPayloadBytes);
414 if (ret != EOK) {
415 METADATA_ERR_LOG("AddCameraMetadataItem memory copy failed");
416 return CAM_META_FAILURE;
417 }
418 dst->data_count += dataBytes;
419 }
420 dst->item_count++;
421
422 METADATA_DEBUG_LOG("AddCameraMetadataItem end");
423 return CAM_META_SUCCESS;
424 }
425
GetCameraMetadataItem(const common_metadata_header_t * src,uint32_t index,camera_metadata_item_t * item)426 int GetCameraMetadataItem(const common_metadata_header_t *src, uint32_t index, camera_metadata_item_t *item)
427 {
428 METADATA_DEBUG_LOG("GetCameraMetadataItem start");
429 if (src == nullptr || item == nullptr) {
430 METADATA_ERR_LOG("GetCameraMetadataItem src or item is null");
431 return CAM_META_INVALID_PARAM;
432 }
433
434 int32_t ret = memset_s(item, sizeof(camera_metadata_item_t), 0, sizeof(camera_metadata_item_t));
435 if (ret != EOK) {
436 METADATA_ERR_LOG("GetCameraMetadataItem: memset_s failed");
437 return CAM_META_FAILURE;
438 }
439 if (index >= src->item_count) {
440 METADATA_ERR_LOG("GetCameraMetadataItem index is greater than item count");
441 return CAM_META_INVALID_PARAM;
442 }
443
444 camera_metadata_item_entry_t *localItem = GetMetadataItems(src) + index;
445
446 item->index = index;
447 item->item = localItem->item;
448 item->data_type = localItem->data_type;
449 item->count = localItem->count;
450
451 size_t dataBytes = CalculateCameraMetadataItemDataSize(localItem->data_type, localItem->count);
452 if (dataBytes == 0) {
453 item->data.u8 = localItem->data.value;
454 } else {
455 item->data.u8 = GetMetadataData(src) + localItem->data.offset;
456 }
457
458 METADATA_DEBUG_LOG("GetCameraMetadataItem end");
459 return CAM_META_SUCCESS;
460 }
461
FindCameraMetadataItemIndex(const common_metadata_header_t * src,uint32_t item,uint32_t * idx)462 int FindCameraMetadataItemIndex(const common_metadata_header_t *src, uint32_t item, uint32_t *idx)
463 {
464 METADATA_DEBUG_LOG("FindCameraMetadataItemIndex start");
465 METADATA_DEBUG_LOG("FindCameraMetadataItemIndex item: %{public}u", item);
466 if (src == nullptr || idx == nullptr) {
467 METADATA_ERR_LOG("FindCameraMetadataItemIndex src or index is null");
468 return CAM_META_INVALID_PARAM;
469 }
470
471 camera_metadata_item_entry_t *searchItem = GetMetadataItems(src);
472 uint32_t index;
473 for (index = 0; index < src->item_count; index++, searchItem++) {
474 if (searchItem->item == item) {
475 break;
476 }
477 }
478
479 if (index == src->item_count) {
480 METADATA_ERR_LOG("FindCameraMetadataItemIndex item: %{public}u not found", item);
481 return CAM_META_ITEM_NOT_FOUND;
482 }
483
484 *idx = index;
485 METADATA_DEBUG_LOG("FindCameraMetadataItemIndex index: %{public}u", index);
486 METADATA_DEBUG_LOG("FindCameraMetadataItemIndex end");
487 return CAM_META_SUCCESS;
488 }
489
FindCameraMetadataItem(const common_metadata_header_t * src,uint32_t item,camera_metadata_item_t * metadataItem)490 int FindCameraMetadataItem(const common_metadata_header_t *src, uint32_t item, camera_metadata_item_t *metadataItem)
491 {
492 uint32_t index = 0;
493 const char *name = GetCameraMetadataItemName(item);
494 if (name == nullptr) {
495 name = "<unknown>";
496 }
497 METADATA_DEBUG_LOG("FindCameraMetadataItem item id: %{public}u, name: %{public}s", item, name);
498 int ret = FindCameraMetadataItemIndex(src, item, &index);
499 if (ret != CAM_META_SUCCESS) {
500 return ret;
501 }
502
503 return GetCameraMetadataItem(src, index, metadataItem);
504 }
505
SetOffset(camera_metadata_item_entry_t * metadataItems,camera_metadata_item_entry_t * item,size_t oldItemSize)506 void SetOffset(camera_metadata_item_entry_t *metadataItems, camera_metadata_item_entry_t *item, size_t oldItemSize)
507 {
508 if (CalculateCameraMetadataItemDataSize(metadataItems->data_type, metadataItems->count) > 0 &&
509 metadataItems->data.offset > item->data.offset) {
510 metadataItems->data.offset -= oldItemSize;
511 }
512 }
513
MetadataExpandItemMem(common_metadata_header_t * dst,camera_metadata_item_entry_t * item,size_t oldItemSize)514 int MetadataExpandItemMem(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
515 size_t oldItemSize)
516 {
517 if (item == nullptr || dst == nullptr) {
518 METADATA_ERR_LOG("MetadataExpandItemMem item is null or dst is null");
519 return CAM_META_INVALID_PARAM;
520 }
521 uint8_t *start = GetMetadataData(dst) + item->data.offset;
522 uint8_t *end = start + oldItemSize;
523 size_t length = dst->data_count - item->data.offset - oldItemSize;
524 if (length != 0) {
525 int32_t ret = memmove_s(start, length, end, length);
526 if (ret != EOK) {
527 METADATA_ERR_LOG("MetadataExpandItemMem memory move failed");
528 return CAM_META_FAILURE;
529 }
530 }
531 dst->data_count -= oldItemSize;
532
533 camera_metadata_item_entry_t *metadataItems = GetMetadataItems(dst);
534 for (uint32_t i = 0; i < dst->item_count; i++, ++metadataItems) {
535 SetOffset(metadataItems, item, oldItemSize);
536 }
537
538 return CAM_META_SUCCESS;
539 }
540
UpdateCameraMetadataItemByIndex(common_metadata_header_t * dst,uint32_t index,const void * data,uint32_t dataCount,camera_metadata_item_t * updatedItem)541 int UpdateCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index, const void *data, uint32_t dataCount,
542 camera_metadata_item_t *updatedItem)
543 {
544 METADATA_DEBUG_LOG("UpdateCameraMetadataItemByIndex start");
545 if ((dst == nullptr) || (index >= dst->item_count)) {
546 METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex dst is null or invalid index");
547 return CAM_META_INVALID_PARAM;
548 }
549
550 if (!dataCount || data == nullptr) {
551 METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex data is not valid. "
552 "dataCount: %{public}u", dataCount);
553 return CAM_META_INVALID_PARAM;
554 }
555
556 int32_t ret = CAM_META_SUCCESS;
557 camera_metadata_item_entry_t *item = GetMetadataItems(dst) + index;
558 size_t dataSize = CalculateCameraMetadataItemDataSize(item->data_type, dataCount);
559 size_t dataPayloadSize = dataCount * OHOS_CAMERA_METADATA_TYPE_SIZE[item->data_type];
560
561 size_t oldItemSize = CalculateCameraMetadataItemDataSize(item->data_type, item->count);
562 if (dataSize != oldItemSize) {
563 if (dst->data_capacity < (dst->data_count + dataSize - oldItemSize)) {
564 METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex data_capacity limit reached");
565 return CAM_META_DATA_CAP_EXCEED;
566 }
567
568 if (oldItemSize != 0) {
569 ret = MetadataExpandItemMem(dst, item, oldItemSize);
570 if (ret != CAM_META_SUCCESS) {
571 return ret;
572 }
573 }
574
575 if (dataSize != 0) {
576 item->data.offset = dst->data_count;
577 ret = memcpy_s(GetMetadataData(dst) + item->data.offset, dataPayloadSize, data, dataPayloadSize);
578 if (ret != EOK) {
579 METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex memory copy failed");
580 return CAM_META_FAILURE;
581 }
582 dst->data_count += dataSize;
583 }
584 } else if (dataSize != 0) {
585 ret = memcpy_s(GetMetadataData(dst) + item->data.offset, dataPayloadSize, data, dataPayloadSize);
586 if (ret != EOK) {
587 METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex memory copy failed");
588 return CAM_META_FAILURE;
589 }
590 }
591
592 if (dataSize == 0) {
593 ret = memcpy_s(item->data.value, dataPayloadSize, data, dataPayloadSize);
594 if (ret != EOK) {
595 METADATA_ERR_LOG("UpdateCameraMetadataItemByIndex memory copy failed");
596 return CAM_META_FAILURE;
597 }
598 }
599
600 item->count = dataCount;
601 if (updatedItem != nullptr) {
602 ret = GetCameraMetadataItem(dst, index, updatedItem);
603 if (ret != CAM_META_SUCCESS) {
604 return ret;
605 }
606 }
607
608 METADATA_DEBUG_LOG("UpdateCameraMetadataItemByIndex end");
609 return ret;
610 }
611
UpdateCameraMetadataItem(common_metadata_header_t * dst,uint32_t item,const void * data,uint32_t dataCount,camera_metadata_item_t * updatedItem)612 int UpdateCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data,
613 uint32_t dataCount, camera_metadata_item_t *updatedItem)
614 {
615 METADATA_DEBUG_LOG("UpdateCameraMetadataItem item id: %{public}u, dataCount: %{public}u", item, dataCount);
616 const char *name = GetCameraMetadataItemName(item);
617 if (name == nullptr) {
618 name = "<unknown>";
619 }
620 METADATA_DEBUG_LOG("UpdateCameraMetadataItem item id: %{public}u, name: %{public}s, "
621 "dataCount: %{public}u", item, name, dataCount);
622 if (!dataCount || data == nullptr) {
623 METADATA_ERR_LOG("UpdateCameraMetadataItem data is not valid. item: %{public}u, "
624 "dataCount: %{public}u", item, dataCount);
625 return CAM_META_INVALID_PARAM;
626 }
627
628 uint32_t index = 0;
629 int32_t ret = FindCameraMetadataItemIndex(dst, item, &index);
630 if (ret != CAM_META_SUCCESS) {
631 return ret;
632 }
633
634 return UpdateCameraMetadataItemByIndex(dst, index, data, dataCount, updatedItem);
635 }
636
DeleteCameraMetadataItemByIndex(common_metadata_header_t * dst,uint32_t index)637 int DeleteCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index)
638 {
639 METADATA_DEBUG_LOG("DeleteCameraMetadataItemByIndex start");
640 if (dst == nullptr) {
641 METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex dst is null");
642 return CAM_META_INVALID_PARAM;
643 }
644
645 if (index >= dst->item_count) {
646 METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex item not valid");
647 return CAM_META_INVALID_PARAM;
648 }
649
650 int32_t ret = CAM_META_SUCCESS;
651 camera_metadata_item_entry_t *itemToDelete = GetMetadataItems(dst) + index;
652 size_t dataBytes = CalculateCameraMetadataItemDataSize(itemToDelete->data_type, itemToDelete->count);
653 if (dataBytes > 0) {
654 uint8_t *start = GetMetadataData(dst) + itemToDelete->data.offset;
655 uint8_t *end = start + dataBytes;
656 size_t length = dst->data_count - itemToDelete->data.offset - dataBytes;
657 if (length != 0) {
658 ret = memmove_s(start, length, end, length);
659 if (ret != EOK) {
660 METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex memory move failed");
661 return CAM_META_FAILURE;
662 }
663 }
664 dst->data_count -= dataBytes;
665
666 camera_metadata_item_entry_t *metadataItems = GetMetadataItems(dst);
667 for (uint32_t i = 0; i < dst->item_count; i++, ++metadataItems) {
668 if (CalculateCameraMetadataItemDataSize(
669 metadataItems->data_type, metadataItems->count) > 0 &&
670 metadataItems->data.offset > itemToDelete->data.offset) {
671 metadataItems->data.offset -= dataBytes;
672 }
673 }
674 }
675
676 uint64_t length = sizeof(camera_metadata_item_entry_t) * (dst->item_count - index - 1);
677 if (length != 0) {
678 ret = memmove_s(itemToDelete, length, itemToDelete + 1, length);
679 if (ret != EOK) {
680 METADATA_ERR_LOG("DeleteCameraMetadataItemByIndex memory move failed");
681 return CAM_META_FAILURE;
682 }
683 }
684 dst->item_count -= 1;
685 METADATA_DEBUG_LOG("DeleteCameraMetadataItemByIndex end");
686 return ret;
687 }
688
DeleteCameraMetadataItem(common_metadata_header_t * dst,uint32_t item)689 int DeleteCameraMetadataItem(common_metadata_header_t *dst, uint32_t item)
690 {
691 METADATA_DEBUG_LOG("DeleteCameraMetadataItem item: %{public}u", item);
692 uint32_t index = 0;
693 int32_t ret = FindCameraMetadataItemIndex(dst, item, &index);
694 if (ret != CAM_META_SUCCESS) {
695 return ret;
696 }
697
698 return DeleteCameraMetadataItemByIndex(dst, index);
699 }
700
FreeCameraMetadataBuffer(common_metadata_header_t * dst)701 void FreeCameraMetadataBuffer(common_metadata_header_t *dst)
702 {
703 if (dst != nullptr) {
704 free(dst);
705 }
706 }
707
GetCameraMetadataItemCount(const common_metadata_header_t * metadataHeader)708 uint32_t GetCameraMetadataItemCount(const common_metadata_header_t *metadataHeader)
709 {
710 if (!metadataHeader) {
711 METADATA_ERR_LOG("GetCameraMetadataItemCount::metadataHeader is null");
712 return 0;
713 }
714 return metadataHeader->item_count;
715 }
716
GetCameraMetadataItemCapacity(const common_metadata_header_t * metadataHeader)717 uint32_t GetCameraMetadataItemCapacity(const common_metadata_header_t *metadataHeader)
718 {
719 if (!metadataHeader) {
720 METADATA_ERR_LOG("GetCameraMetadataItemCapacity::metadataHeader is null");
721 return 0;
722 }
723 return metadataHeader->item_capacity;
724 }
725
GetCameraMetadataDataSize(const common_metadata_header_t * metadataHeader)726 uint32_t GetCameraMetadataDataSize(const common_metadata_header_t *metadataHeader)
727 {
728 if (metadataHeader == nullptr) {
729 METADATA_ERR_LOG("GetCameraMetadataDataSize::metadataHeader is null");
730 return 0;
731 }
732 return metadataHeader->data_capacity;
733 }
734
CopyCameraMetadataItems(common_metadata_header_t * newMetadata,const common_metadata_header_t * oldMetadata)735 int32_t CopyCameraMetadataItems(common_metadata_header_t *newMetadata, const common_metadata_header_t *oldMetadata)
736 {
737 if (newMetadata == nullptr || oldMetadata == nullptr) {
738 return CAM_META_INVALID_PARAM;
739 }
740
741 int32_t ret;
742 if (oldMetadata->item_count != 0) {
743 ret = memcpy_s(GetMetadataItems(newMetadata), sizeof(camera_metadata_item_entry_t[oldMetadata->item_count]),
744 GetMetadataItems(oldMetadata), sizeof(camera_metadata_item_entry_t[oldMetadata->item_count]));
745 if (ret != EOK) {
746 METADATA_ERR_LOG("CopyCameraMetadataItems memory copy failed");
747 return CAM_META_FAILURE;
748 }
749 }
750
751 if (oldMetadata->data_count != 0) {
752 ret = memcpy_s(GetMetadataData(newMetadata), sizeof(uint8_t[oldMetadata->data_count]),
753 GetMetadataData(oldMetadata), sizeof(uint8_t[oldMetadata->data_count]));
754 if (ret != EOK) {
755 METADATA_ERR_LOG("CopyCameraMetadataItems memory copy failed");
756 return CAM_META_FAILURE;
757 }
758 }
759
760 newMetadata->item_count = oldMetadata->item_count;
761 newMetadata->data_count = oldMetadata->data_count;
762
763 return CAM_META_SUCCESS;
764 }
765
U8ItemToString(int32_t item,const camera_metadata_item_t entry)766 std::string U8ItemToString(int32_t item, const camera_metadata_item_t entry)
767 {
768 std::string st = {};
769 uint32_t count = entry.count;
770 std::string dataStr = std::to_string(*(entry.data.u8));
771 for (uint32_t i = 1; i < count; i++) {
772 if ((i % WRAP_LENGTH) == 0) {
773 dataStr += "]\n\t[" + std::to_string(*(entry.data.u8 + i));
774 } else {
775 dataStr += " " + std::to_string(*(entry.data.u8 + i));
776 }
777 }
778
779 const char *name = GetCameraMetadataItemName(item);
780 if (name == nullptr) {
781 METADATA_ERR_LOG("U8ItemToString: get u8 item name fail!");
782 return st;
783 }
784 std::string nameStr(name);
785
786 st = nameStr + " (" + std::to_string(entry.index) + "): " +
787 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
788
789 return st;
790 }
791
I32ItemToString(int32_t item,const camera_metadata_item_t entry)792 std::string I32ItemToString(int32_t item, const camera_metadata_item_t entry)
793 {
794 std::string st = {};
795 uint32_t count = entry.count;
796 std::string dataStr = std::to_string(*(entry.data.i32));
797 for (uint32_t i = 1; i < count; i++) {
798 if ((i % WRAP_LENGTH) == 0) {
799 dataStr += "]\n\t[" + std::to_string(*(entry.data.i32 + i));
800 } else {
801 dataStr += " " + std::to_string(*(entry.data.i32 + i));
802 }
803 }
804
805 const char *name = GetCameraMetadataItemName(item);
806 if (name == nullptr) {
807 METADATA_ERR_LOG("I32ItemToString: get i32 item name fail!");
808 return st;
809 }
810 std::string nameStr(name);
811
812 st = nameStr + " (" + std::to_string(entry.index) + "): " +
813 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
814
815 return st;
816 }
817
U32ItemToString(int32_t item,const camera_metadata_item_t entry)818 std::string U32ItemToString(int32_t item, const camera_metadata_item_t entry)
819 {
820 std::string st = {};
821 uint32_t count = entry.count;
822 std::string dataStr = std::to_string(*(entry.data.ui32));
823 for (uint32_t i = 1; i < count; i++) {
824 if ((i % WRAP_LENGTH) == 0) {
825 dataStr += "]\n\t[" + std::to_string(*(entry.data.ui32 + i));
826 } else {
827 dataStr += " " + std::to_string(*(entry.data.ui32 + i));
828 }
829 }
830
831 const char *name = GetCameraMetadataItemName(item);
832 if (name == nullptr) {
833 METADATA_ERR_LOG("U32ItemToString: get u32 item name fail!");
834 return st;
835 }
836 std::string nameStr(name);
837
838 st = nameStr + " (" + std::to_string(entry.index) + "): " +
839 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
840
841 return st;
842 }
843
I64ItemToString(int32_t item,const camera_metadata_item_t entry)844 std::string I64ItemToString(int32_t item, const camera_metadata_item_t entry)
845 {
846 std::string st = {};
847 uint32_t count = entry.count;
848 std::string dataStr = std::to_string(*(entry.data.i64));
849 for (uint32_t i = 1; i < count; i++) {
850 if ((i % WRAP_LENGTH) == 0) {
851 dataStr += "]\n\t[" + std::to_string(*(entry.data.i64 + i));
852 } else {
853 dataStr += " " + std::to_string(*(entry.data.i64 + i));
854 }
855 }
856
857 const char *name = GetCameraMetadataItemName(item);
858 if (name == nullptr) {
859 METADATA_ERR_LOG("I64ItemToString: get i64 item name fail!");
860 return st;
861 }
862 std::string nameStr(name);
863
864 st = nameStr + " (" + std::to_string(entry.index) + "): " +
865 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
866
867 return st;
868 }
869
FloatItemToString(int32_t item,const camera_metadata_item_t entry)870 std::string FloatItemToString(int32_t item, const camera_metadata_item_t entry)
871 {
872 std::string st = {};
873 uint32_t count = entry.count;
874 std::string dataStr = std::to_string(*(entry.data.f));
875 for (uint32_t i = 1; i < count; i++) {
876 if ((i % WRAP_LENGTH) == 0) {
877 dataStr += "]\n\t[" + std::to_string(*(entry.data.f + i));
878 } else {
879 dataStr += " " + std::to_string(*(entry.data.f + i));
880 }
881 }
882
883 const char *name = GetCameraMetadataItemName(item);
884 if (name == nullptr) {
885 METADATA_ERR_LOG("FloatItemToString: get float item name fail!");
886 return st;
887 }
888 std::string nameStr(name);
889
890 st = nameStr + " (" + std::to_string(entry.index) + "): " +
891 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
892
893 return st;
894 }
895
DoubleItemToString(int32_t item,const camera_metadata_item_t entry)896 std::string DoubleItemToString(int32_t item, const camera_metadata_item_t entry)
897 {
898 std::string st = {};
899 uint32_t count = entry.count;
900 std::string dataStr = std::to_string(*(entry.data.d));
901 for (uint32_t i = 1; i < count; i++) {
902 if ((i % WRAP_LENGTH) == 0) {
903 dataStr += "]\n\t[" + std::to_string(*(entry.data.d + i));
904 } else {
905 dataStr += " " + std::to_string(*(entry.data.d + i));
906 }
907 }
908
909 const char *name = GetCameraMetadataItemName(item);
910 if (name == nullptr) {
911 METADATA_ERR_LOG("DoubleItemToString: get double item name fail!");
912 return st;
913 }
914 std::string nameStr(name);
915
916 st = nameStr + " (" + std::to_string(entry.index) + "): " +
917 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
918
919 return st;
920 }
921
RationalItemToString(int32_t item,const camera_metadata_item_t entry)922 std::string RationalItemToString(int32_t item, const camera_metadata_item_t entry)
923 {
924 std::string st = {};
925 uint32_t count = entry.count;
926 std::string dataStr = std::to_string((*(entry.data.r)).numerator) + "/" +
927 std::to_string((*(entry.data.r)).denominator);
928 for (uint32_t i = 1; i < count; i++) {
929 if ((i % WRAP_LENGTH) == 0) {
930 dataStr += "]\n\t[" + std::to_string((*(entry.data.r + i)).numerator) + "/" +
931 std::to_string((*(entry.data.r + i)).denominator);
932 } else {
933 dataStr += " " + std::to_string((*(entry.data.r + i)).numerator) + "/" +
934 std::to_string((*(entry.data.r + i)).denominator);
935 }
936 }
937
938 const char *name = GetCameraMetadataItemName(item);
939 if (name == nullptr) {
940 METADATA_ERR_LOG("RationalItemToString: get rational item name fail!");
941 return st;
942 }
943 std::string nameStr(name);
944
945 st = nameStr + " (" + std::to_string(entry.index) + "): " +
946 OHOS_CAMERA_METADATA_TYPE[entry.data_type] + "[" + std::to_string(count) + "]" + "\n\t[" + dataStr + "]";
947
948 return st;
949 }
950
MetadataItemDump(const common_metadata_header_t * metadataHeader,uint32_t item)951 std::string MetadataItemDump(const common_metadata_header_t *metadataHeader, uint32_t item)
952 {
953 camera_metadata_item_t entry;
954 std::string st = {};
955
956 int ret = FindCameraMetadataItem(metadataHeader, item, &entry);
957 if (ret != 0) {
958 METADATA_ERR_LOG("get item error and item = %{public}d", item);
959 return st;
960 }
961
962 switch (entry.data_type) {
963 case META_TYPE_BYTE:
964 st = U8ItemToString(item, entry);
965 break;
966 case META_TYPE_INT32:
967 st = I32ItemToString(item, entry);
968 break;
969 case META_TYPE_UINT32:
970 st = U32ItemToString(item, entry);
971 break;
972 case META_TYPE_FLOAT:
973 st = FloatItemToString(item, entry);
974 break;
975 case META_TYPE_INT64:
976 st = I64ItemToString(item, entry);
977 break;
978 case META_TYPE_DOUBLE:
979 st = DoubleItemToString(item, entry);
980 break;
981 case META_TYPE_RATIONAL:
982 st = RationalItemToString(item, entry);
983 break;
984 default:
985 METADATA_ERR_LOG("invalid param and item = %{public}d", item);
986 break;
987 }
988
989 if (!st.empty()) {
990 st += "\n";
991 }
992 return st;
993 }
994
FormatCameraMetadataToString(const common_metadata_header_t * metadataHeader)995 std::string FormatCameraMetadataToString(const common_metadata_header_t *metadataHeader)
996 {
997 std::string metaStr;
998 if (metadataHeader == nullptr) {
999 METADATA_ERR_LOG("metadataHeader is nullptr");
1000 return metaStr;
1001 }
1002
1003 for (auto it = METADATATAGS.begin(); it != METADATATAGS.end(); it++) {
1004 metaStr += MetadataItemDump(metadataHeader, *it);
1005 }
1006
1007 return metaStr;
1008 }
1009 } // Camera
1010