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 #ifndef CAMERA_METADATA_INFO_H
17 #define CAMERA_METADATA_INFO_H
18 
19 #include <cstdio>
20 #include <cstdint>
21 #include <string>
22 #include "camera_metadata_operator.h"
23 
24 static constexpr uint32_t MAX_SUPPORTED_TAGS = 1000;
25 static constexpr uint32_t MAX_SUPPORTED_ITEMS = (12000 * 2 * 2);
26 static constexpr uint32_t MAX_ITEM_CAPACITY = (1000 * 10);
27 static constexpr uint32_t MAX_DATA_CAPACITY = (1000 * 10 * 10 * 10);
28 static constexpr uint32_t MIN_VEC_SIZE = 12;
29 
30 #define METADATA_PACKET_ALIGNMENT \
31     MaxAlignment(MaxAlignment(DATA_ALIGNMENT, METADATA_ALIGNMENT), ITEM_ALIGNMENT)
32 
33 namespace OHOS::Camera {
34 class CameraMetadata {
35 public:
36     CameraMetadata(size_t itemCapacity, size_t dataCapacity);
37     ~CameraMetadata();
38 
39     bool addEntry(uint32_t item, const void *data, size_t data_count);
40     bool updateEntry(uint32_t tag, const void *data, size_t data_count);
41     common_metadata_header_t *get();
42     const common_metadata_header_t *get() const;
43     bool isValid() const;
44 
45 private:
46     common_metadata_header_t *metadata_;
47 
48     bool resize_add_metadata(uint32_t item, const void *data, size_t data_count);
49     void replace_metadata(common_metadata_header_t *newMetadata);
50     static uint32_t AlignTo(uint32_t val, uint32_t alignment);
51     static uint32_t MaxAlignment(uint32_t dataAlignment, uint32_t metadataAlignment);
52     static size_t CalculateCameraMetadataMemoryRequired(uint32_t itemCount, uint32_t dataCount);
53     static int UpdateameraMetadataItemSize(camera_metadata_item_entry_t *item, uint32_t dataCount,
54         common_metadata_header_t *dst, const void *data);
55     static int AddCameraMetadataItemVerify(common_metadata_header_t *dst,
56         uint32_t item, const void *data, size_t dataCount, uint32_t *dataType);
57     static int moveMetadataMemery(common_metadata_header_t *dst,
58         camera_metadata_item_entry_t *itemToDelete, size_t dataBytes);
59     static int copyMetadataMemory(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
60         size_t dataPayloadSize, const void *data);
61 
62 public:
63     // Allocate a new camera metadata buffer and return the metadata header
64     static common_metadata_header_t *AllocateCameraMetadataBuffer(uint32_t item_capacity, uint32_t data_capacity);
65 
66     // Is camera metadata item exist
67     static bool IsCameraMetadataItemExist(const common_metadata_header_t *src, uint32_t item);
68 
69     // Find camera metadata item and fill the found item
70     static int FindCameraMetadataItem(const common_metadata_header_t *src, uint32_t item,
71         camera_metadata_item_t *metadataItem);
72 
73     // Find camera metadata item index if the item exists
74     static int FindCameraMetadataItemIndex(const common_metadata_header_t *src, uint32_t item, uint32_t *index,
75         bool isPrintLog = false);
76 
77     // Get camera metadata item name
78     static const char *GetCameraMetadataItemName(uint32_t item);
79 
80     // Update camera metadata item and fill the updated item
81     static int UpdateCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data,
82         uint32_t dataCount, camera_metadata_item_t *updatedItem);
83 
84     // Update camera metadata item by index and fill the updated item
85     static int UpdateCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index, const void *data,
86         uint32_t dataCount, camera_metadata_item_t *updated_item);
87 
88     // Add camera metadata item
89     static int AddCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data, size_t dataCount);
90 
91     // Delete camera metadata item
92     static int DeleteCameraMetadataItem(common_metadata_header_t *dst, uint32_t item);
93 
94     // Delete camera metadata item by index
95     static int DeleteCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index);
96 
97     // Free camera metadata buffer
98     static void FreeCameraMetadataBuffer(common_metadata_header_t *dst);
99 
100     static std::string MetadataItemDump(const common_metadata_header_t *metadataHeader, uint32_t item);
101 
102     static std::string FormatCameraMetadataToString(const common_metadata_header_t *metadataHeader);
103 
104     // Internal use
105     static camera_metadata_item_entry_t *GetMetadataItems(const common_metadata_header_t *metadataHeader);
106     static uint8_t *GetMetadataData(const common_metadata_header_t *metadataHeader);
107     static int GetCameraMetadataItem(const common_metadata_header_t *src, uint32_t index, camera_metadata_item_t *item);
108     static uint32_t GetCameraMetadataItemCount(const common_metadata_header_t *metadata_header);
109     static uint32_t GetCameraMetadataItemCapacity(const common_metadata_header_t *metadata_header);
110     static uint32_t GetCameraMetadataDataSize(const common_metadata_header_t *metadata_header);
111     static int32_t CopyCameraMetadataItems(common_metadata_header_t *newMetadata,
112         const common_metadata_header_t *oldMetadata);
113     static int32_t CalculateCameraMetadataItemDataSize(uint32_t type, size_t data_count);
114     static int32_t GetCameraMetadataItemType(uint32_t item, uint32_t *data_type);
115     static common_metadata_header_t *FillCameraMetadata(common_metadata_header_t *buffer, size_t memoryRequired,
116         uint32_t itemCapacity, uint32_t dataCapacity);
117     static int32_t GetMetadataSection(uint32_t itemSection, uint32_t *section);
118     static int MetadataExpandItemMem(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
119         size_t oldItemSize);
120     static int32_t GetAllVendorTags(std::vector<vendorTag_t>& tagVec);
121 };
122 } // namespace Camera
123 #endif /* CAMERA_METADATA_INFO_H */
124