• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OPERATOR_H
17 #define CAMERA_METADATA_OPERATOR_H
18 
19 #include <cstdio>
20 #include <cstdint>
21 #include <string>
22 
23 #include "camera_device_ability_items.h"
24 
25 /** Versioning information */
26 #define CURRENT_CAMERA_METADATA_VERSION 1
27 
28 #define ITEM_ALIGNMENT ((size_t) 4)
29 
30 #define DATA_ALIGNMENT ((size_t) 8)
31 
32 #define METADATA_ALIGNMENT ((size_t) 4)
33 
34 #define METADATA_PACKET_ALIGNMENT \
35     MaxAlignment(MaxAlignment(DATA_ALIGNMENT, METADATA_ALIGNMENT), ITEM_ALIGNMENT)
36 
37 #define INDEX_COUNTER 2
38 #define WRAP_LENGTH 4
39 
40 // data type
41 enum {
42     // uint8_t
43     META_TYPE_BYTE = 0,
44     // int32_t
45     META_TYPE_INT32 = 1,
46     // uint32_t
47     META_TYPE_UINT32 = 2,
48     // float
49     META_TYPE_FLOAT = 3,
50     // int64_t
51     META_TYPE_INT64 = 4,
52     // double
53     META_TYPE_DOUBLE = 5,
54     // A 64-bit fraction (camera_metadata_rational_t)
55     META_TYPE_RATIONAL = 6,
56     // Number of data type
57     META_NUM_TYPES
58 };
59 
60 const static char *OHOS_CAMERA_METADATA_TYPE[META_NUM_TYPES] = {
61     [META_TYPE_BYTE] = "byte",
62     [META_TYPE_INT32] = "int32",
63     [META_TYPE_UINT32] = "uint32",
64     [META_TYPE_FLOAT] = "float",
65     [META_TYPE_INT64] = "int64",
66     [META_TYPE_DOUBLE] = "double",
67     [META_TYPE_RATIONAL] = "rational"
68 };
69 
70 typedef struct camera_rational {
71     int32_t numerator;
72     int32_t denominator;
73 } camera_rational_t;
74 
75 // common metadata header
76 typedef struct common_metadata_header {
77     uint32_t version;
78     uint32_t size;
79     uint32_t item_count;
80     uint32_t item_capacity;
81     uint32_t items_start; // Offset from common_metadata_header
82     uint32_t data_count;
83     uint32_t data_capacity;
84     uint32_t data_start; // Offset from common_metadata_header
85 } common_metadata_header_t;
86 
87 typedef struct camera_metadata_item_entry {
88     uint32_t item;
89     uint32_t data_type;
90     uint32_t count;
91     union {
92         uint32_t offset;
93         uint8_t  value[4];
94     } data;
95 } camera_metadata_item_entry_t;
96 
97 typedef struct camera_metadata_item {
98     uint32_t index;
99     uint32_t item;
100     uint32_t data_type;
101     uint32_t count;
102     union {
103         uint8_t  *u8;
104         int32_t  *i32;
105         uint32_t *ui32;
106         float    *f;
107         int64_t  *i64;
108         double   *d;
109         camera_rational_t *r;
110     } data;
111 } camera_metadata_item_t;
112 
113 typedef struct item_info {
114     const char *item_name;
115     uint8_t item_type;
116     int32_t dataCnt;
117 } item_info_t;
118 
119 /* camera_metadata_section */
120 typedef enum camera_metadata_sec {
121     OHOS_SECTION_CAMERA_PROPERTIES,
122     OHOS_SECTION_CAMERA_SENSOR,
123     OHOS_SECTION_CAMERA_SENSOR_INFO,
124     OHOS_SECTION_CAMERA_STATISTICS,
125     OHOS_SECTION_CAMERA_CONTROL,
126     OHOS_SECTION_DEVICE_EXPOSURE,
127     OHOS_SECTION_DEVICE_FOCUS,
128     OHOS_SECTION_DEVICE_FLASH,
129     OHOS_SECTION_DEVICE_ZOOM,
130     OHOS_SECTION_STREAM_ABILITY,
131     OHOS_SECTION_STREAM_JPEG,
132     OHOS_SECTION_STREAM_VIDEO,
133     OHOS_SECTION_CAMERA_EFFECT,
134     OHOS_SECTION_CAMERA_SECURE,
135     OHOS_SECTION_COUNT,
136 } camera_metadata_sec_t;
137 
138 /* Return codes */
139 #define CAM_META_FAILURE        (-1)
140 #define CAM_META_SUCCESS         0
141 #define CAM_META_INVALID_PARAM   2
142 #define CAM_META_ITEM_NOT_FOUND  3
143 #define CAM_META_ITEM_CAP_EXCEED 4
144 #define CAM_META_DATA_CAP_EXCEED 5
145 
146 /* for shift opereration */
147 #define BITWISE_SHIFT_16 16
148 
149 namespace OHOS::Camera {
150 uint32_t AlignTo(uint32_t val, uint32_t alignment);
151 
152 uint32_t MaxAlignment(uint32_t dataAlignment, uint32_t metadataAlignment);
153 
154 // Allocate a new camera metadata buffer and return the metadata header
155 common_metadata_header_t *AllocateCameraMetadataBuffer(uint32_t item_capacity, uint32_t data_capacity);
156 
157 // Find camera metadata item and fill the found item
158 int FindCameraMetadataItem(const common_metadata_header_t *src, uint32_t item, camera_metadata_item_t *metadataItem);
159 
160 // Find camera metadata item index if the item exists
161 int FindCameraMetadataItemIndex(const common_metadata_header_t *src, uint32_t item, uint32_t *index);
162 
163 // Get camera metadata item name
164 const char *GetCameraMetadataItemName(uint32_t item);
165 
166 // Update camera metadata item and fill the updated item
167 int UpdateCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data, uint32_t dataCount,
168                              camera_metadata_item_t *updatedItem);
169 
170 // Update camera metadata item by index and fill the updated item
171 int UpdateCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index, const void *data,
172                                     uint32_t dataCount, camera_metadata_item_t *updated_item);
173 
174 // Add camera metadata item
175 int AddCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data, size_t dataCount);
176 
177 // Delete camera metadata item
178 int DeleteCameraMetadataItem(common_metadata_header_t *dst, uint32_t item);
179 
180 // Delete camera metadata item by index
181 int DeleteCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index);
182 
183 // Free camera metadata buffer
184 void FreeCameraMetadataBuffer(common_metadata_header_t *dst);
185 
186 std::string MetadataItemDump(const common_metadata_header_t *metadataHeader, uint32_t item);
187 
188 std::string FormatCameraMetadataToString(const common_metadata_header_t *metadataHeader);
189 
190 // Internal use
191 camera_metadata_item_entry_t *GetMetadataItems(const common_metadata_header_t *metadataHeader);
192 uint8_t *GetMetadataData(const common_metadata_header_t *metadataHeader);
193 int GetCameraMetadataItem(const common_metadata_header_t *src, uint32_t index, camera_metadata_item_t *item);
194 uint32_t GetCameraMetadataItemCount(const common_metadata_header_t *metadata_header);
195 uint32_t GetCameraMetadataItemCapacity(const common_metadata_header_t *metadata_header);
196 uint32_t GetCameraMetadataDataSize(const common_metadata_header_t *metadata_header);
197 int32_t CopyCameraMetadataItems(common_metadata_header_t *newMetadata, const common_metadata_header_t *oldMetadata);
198 size_t CalculateCameraMetadataItemDataSize(uint32_t type, size_t data_count);
199 int32_t GetCameraMetadataItemType(uint32_t item, uint32_t *data_type);
200 common_metadata_header_t *FillCameraMetadata(common_metadata_header_t *buffer, size_t memoryRequired,
201                                              uint32_t itemCapacity, uint32_t dataCapacity);
202 int32_t GetMetadataSection(uint32_t itemSection, uint32_t *section);
203 int MetadataExpandItemMem(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
204     size_t oldItemSize);
205 } // Camera
206 #endif // CAMERA_METADATA_OPERATOR_H
207