1c5e268c6Sopenharmony_ci/*
2c5e268c6Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3c5e268c6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c5e268c6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c5e268c6Sopenharmony_ci * You may obtain a copy of the License at
6c5e268c6Sopenharmony_ci *
7c5e268c6Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c5e268c6Sopenharmony_ci *
9c5e268c6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c5e268c6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c5e268c6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c5e268c6Sopenharmony_ci * See the License for the specific language governing permissions and
13c5e268c6Sopenharmony_ci * limitations under the License.
14c5e268c6Sopenharmony_ci */
15c5e268c6Sopenharmony_ci
16c5e268c6Sopenharmony_ci#ifndef CAMERA_METADATA_OPERATOR_H
17c5e268c6Sopenharmony_ci#define CAMERA_METADATA_OPERATOR_H
18c5e268c6Sopenharmony_ci
19c5e268c6Sopenharmony_ci#include <cstdio>
20c5e268c6Sopenharmony_ci#include <cstdint>
21c5e268c6Sopenharmony_ci#include <string>
22c5e268c6Sopenharmony_ci
23c5e268c6Sopenharmony_ci#include "camera_device_ability_items.h"
24c5e268c6Sopenharmony_ci
25c5e268c6Sopenharmony_ci/** Versioning information */
26c5e268c6Sopenharmony_ci#define CURRENT_CAMERA_METADATA_VERSION 1
27c5e268c6Sopenharmony_ci
28c5e268c6Sopenharmony_ci#define ITEM_ALIGNMENT ((size_t) 4)
29c5e268c6Sopenharmony_ci
30c5e268c6Sopenharmony_ci#define DATA_ALIGNMENT ((size_t) 8)
31c5e268c6Sopenharmony_ci
32c5e268c6Sopenharmony_ci#define METADATA_ALIGNMENT ((size_t) 4)
33c5e268c6Sopenharmony_ci
34c5e268c6Sopenharmony_ci#define INDEX_COUNTER 2
35c5e268c6Sopenharmony_ci#define WRAP_LENGTH 4
36c5e268c6Sopenharmony_ci
37c5e268c6Sopenharmony_ci// data type
38c5e268c6Sopenharmony_cienum {
39c5e268c6Sopenharmony_ci    // uint8_t
40c5e268c6Sopenharmony_ci    META_TYPE_BYTE = 0,
41c5e268c6Sopenharmony_ci    // int32_t
42c5e268c6Sopenharmony_ci    META_TYPE_INT32 = 1,
43c5e268c6Sopenharmony_ci    // uint32_t
44c5e268c6Sopenharmony_ci    META_TYPE_UINT32 = 2,
45c5e268c6Sopenharmony_ci    // float
46c5e268c6Sopenharmony_ci    META_TYPE_FLOAT = 3,
47c5e268c6Sopenharmony_ci    // int64_t
48c5e268c6Sopenharmony_ci    META_TYPE_INT64 = 4,
49c5e268c6Sopenharmony_ci    // double
50c5e268c6Sopenharmony_ci    META_TYPE_DOUBLE = 5,
51c5e268c6Sopenharmony_ci    // A 64-bit fraction (camera_metadata_rational_t)
52c5e268c6Sopenharmony_ci    META_TYPE_RATIONAL = 6,
53c5e268c6Sopenharmony_ci    // Number of data type
54c5e268c6Sopenharmony_ci    META_NUM_TYPES
55c5e268c6Sopenharmony_ci};
56c5e268c6Sopenharmony_ci
57c5e268c6Sopenharmony_ciconst static char *OHOS_CAMERA_METADATA_TYPE[META_NUM_TYPES] = {
58c5e268c6Sopenharmony_ci    [META_TYPE_BYTE] = "byte",
59c5e268c6Sopenharmony_ci    [META_TYPE_INT32] = "int32",
60c5e268c6Sopenharmony_ci    [META_TYPE_UINT32] = "uint32",
61c5e268c6Sopenharmony_ci    [META_TYPE_FLOAT] = "float",
62c5e268c6Sopenharmony_ci    [META_TYPE_INT64] = "int64",
63c5e268c6Sopenharmony_ci    [META_TYPE_DOUBLE] = "double",
64c5e268c6Sopenharmony_ci    [META_TYPE_RATIONAL] = "rational"
65c5e268c6Sopenharmony_ci};
66c5e268c6Sopenharmony_ci
67c5e268c6Sopenharmony_citypedef struct camera_rational {
68c5e268c6Sopenharmony_ci    int32_t numerator;
69c5e268c6Sopenharmony_ci    int32_t denominator;
70c5e268c6Sopenharmony_ci} camera_rational_t;
71c5e268c6Sopenharmony_ci
72c5e268c6Sopenharmony_ci// common metadata header
73c5e268c6Sopenharmony_citypedef struct common_metadata_header {
74c5e268c6Sopenharmony_ci    uint32_t version;
75c5e268c6Sopenharmony_ci    uint32_t size;
76c5e268c6Sopenharmony_ci    uint32_t item_count;
77c5e268c6Sopenharmony_ci    uint32_t item_capacity;
78c5e268c6Sopenharmony_ci    uint32_t items_start; // Offset from common_metadata_header
79c5e268c6Sopenharmony_ci    uint32_t data_count;
80c5e268c6Sopenharmony_ci    uint32_t data_capacity;
81c5e268c6Sopenharmony_ci    uint32_t data_start; // Offset from common_metadata_header
82c5e268c6Sopenharmony_ci} common_metadata_header_t;
83c5e268c6Sopenharmony_ci
84c5e268c6Sopenharmony_citypedef struct camera_metadata_item_entry {
85c5e268c6Sopenharmony_ci    uint32_t item;
86c5e268c6Sopenharmony_ci    uint32_t data_type;
87c5e268c6Sopenharmony_ci    uint32_t count;
88c5e268c6Sopenharmony_ci    union {
89c5e268c6Sopenharmony_ci        uint32_t offset;
90c5e268c6Sopenharmony_ci        uint8_t  value[4];
91c5e268c6Sopenharmony_ci    } data;
92c5e268c6Sopenharmony_ci} camera_metadata_item_entry_t;
93c5e268c6Sopenharmony_ci
94c5e268c6Sopenharmony_citypedef struct camera_metadata_item {
95c5e268c6Sopenharmony_ci    uint32_t index;
96c5e268c6Sopenharmony_ci    uint32_t item;
97c5e268c6Sopenharmony_ci    uint32_t data_type;
98c5e268c6Sopenharmony_ci    uint32_t count;
99c5e268c6Sopenharmony_ci    union {
100c5e268c6Sopenharmony_ci        uint8_t  *u8;
101c5e268c6Sopenharmony_ci        int32_t  *i32;
102c5e268c6Sopenharmony_ci        uint32_t *ui32;
103c5e268c6Sopenharmony_ci        float    *f;
104c5e268c6Sopenharmony_ci        int64_t  *i64;
105c5e268c6Sopenharmony_ci        double   *d;
106c5e268c6Sopenharmony_ci        camera_rational_t *r;
107c5e268c6Sopenharmony_ci    } data;
108c5e268c6Sopenharmony_ci} camera_metadata_item_t;
109c5e268c6Sopenharmony_ci
110c5e268c6Sopenharmony_citypedef struct item_info {
111c5e268c6Sopenharmony_ci    const char *item_name;
112c5e268c6Sopenharmony_ci    uint8_t item_type;
113c5e268c6Sopenharmony_ci    int32_t dataCnt;
114c5e268c6Sopenharmony_ci} item_info_t;
115c5e268c6Sopenharmony_ci
116c5e268c6Sopenharmony_ci/* camera_metadata_section */
117c5e268c6Sopenharmony_citypedef enum camera_metadata_sec {
118c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_PROPERTIES,
119c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_SENSOR,
120c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_SENSOR_INFO,
121c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_STATISTICS,
122c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_CONTROL,
123c5e268c6Sopenharmony_ci    OHOS_SECTION_DEVICE_EXPOSURE,
124c5e268c6Sopenharmony_ci    OHOS_SECTION_DEVICE_FOCUS,
125c5e268c6Sopenharmony_ci    OHOS_SECTION_DEVICE_WHITE,
126c5e268c6Sopenharmony_ci    OHOS_SECTION_DEVICE_FLASH,
127c5e268c6Sopenharmony_ci    OHOS_SECTION_DEVICE_ZOOM,
128c5e268c6Sopenharmony_ci    OHOS_SECTION_STREAM_ABILITY,
129c5e268c6Sopenharmony_ci    OHOS_SECTION_STREAM_JPEG,
130c5e268c6Sopenharmony_ci    OHOS_SECTION_STREAM_VIDEO,
131c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_EFFECT,
132c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_SECURE,
133c5e268c6Sopenharmony_ci    OHOS_SECTION_CAMERA_XMAGE,
134c5e268c6Sopenharmony_ci    OHOS_SECTION_COUNT,
135c5e268c6Sopenharmony_ci} camera_metadata_sec_t;
136c5e268c6Sopenharmony_ci
137c5e268c6Sopenharmony_citypedef struct VendoTagInfo {
138c5e268c6Sopenharmony_ci    uint32_t tagId;
139c5e268c6Sopenharmony_ci    const char* tagName;
140c5e268c6Sopenharmony_ci    uint8_t tagType;
141c5e268c6Sopenharmony_ci} vendorTag_t;
142c5e268c6Sopenharmony_ci
143c5e268c6Sopenharmony_ci/* Return codes */
144c5e268c6Sopenharmony_ci#define CAM_META_FAILURE        (-1)
145c5e268c6Sopenharmony_ci#define CAM_META_SUCCESS         0
146c5e268c6Sopenharmony_ci#define CAM_META_INVALID_PARAM   2
147c5e268c6Sopenharmony_ci#define CAM_META_ITEM_NOT_FOUND  3
148c5e268c6Sopenharmony_ci#define CAM_META_ITEM_CAP_EXCEED 4
149c5e268c6Sopenharmony_ci#define CAM_META_DATA_CAP_EXCEED 5
150c5e268c6Sopenharmony_ci
151c5e268c6Sopenharmony_ci/* for shift opereration */
152c5e268c6Sopenharmony_ci#define BITWISE_SHIFT_16 16
153c5e268c6Sopenharmony_ci
154c5e268c6Sopenharmony_cinamespace OHOS::Camera {
155c5e268c6Sopenharmony_ci// Allocate a new camera metadata buffer and return the metadata header
156c5e268c6Sopenharmony_cicommon_metadata_header_t *AllocateCameraMetadataBuffer(uint32_t item_capacity, uint32_t data_capacity);
157c5e268c6Sopenharmony_ci
158c5e268c6Sopenharmony_ci// Is camera metadata item exist
159c5e268c6Sopenharmony_cibool IsCameraMetadataItemExist(const common_metadata_header_t *src, uint32_t item);
160c5e268c6Sopenharmony_ci
161c5e268c6Sopenharmony_ci// Find camera metadata item and fill the found item
162c5e268c6Sopenharmony_ciint FindCameraMetadataItem(const common_metadata_header_t *src, uint32_t item,
163c5e268c6Sopenharmony_ci    camera_metadata_item_t *metadataItem);
164c5e268c6Sopenharmony_ci
165c5e268c6Sopenharmony_ci// Find camera metadata item index if the item exists
166c5e268c6Sopenharmony_ciint FindCameraMetadataItemIndex(const common_metadata_header_t *src, uint32_t item, uint32_t *index);
167c5e268c6Sopenharmony_ci
168c5e268c6Sopenharmony_ci// Get camera metadata item name
169c5e268c6Sopenharmony_ciconst char *GetCameraMetadataItemName(uint32_t item);
170c5e268c6Sopenharmony_ci
171c5e268c6Sopenharmony_ci// Update camera metadata item and fill the updated item
172c5e268c6Sopenharmony_ciint UpdateCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data, uint32_t dataCount,
173c5e268c6Sopenharmony_ci    camera_metadata_item_t *updatedItem);
174c5e268c6Sopenharmony_ci
175c5e268c6Sopenharmony_ci// Update camera metadata item by index and fill the updated item
176c5e268c6Sopenharmony_ciint UpdateCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index, const void *data,
177c5e268c6Sopenharmony_ci    uint32_t dataCount, camera_metadata_item_t *updated_item);
178c5e268c6Sopenharmony_ci
179c5e268c6Sopenharmony_ci// Add camera metadata item
180c5e268c6Sopenharmony_ciint AddCameraMetadataItem(common_metadata_header_t *dst, uint32_t item, const void *data, size_t dataCount);
181c5e268c6Sopenharmony_ci
182c5e268c6Sopenharmony_ci// Delete camera metadata item
183c5e268c6Sopenharmony_ciint DeleteCameraMetadataItem(common_metadata_header_t *dst, uint32_t item);
184c5e268c6Sopenharmony_ci
185c5e268c6Sopenharmony_ci// Delete camera metadata item by index
186c5e268c6Sopenharmony_ciint DeleteCameraMetadataItemByIndex(common_metadata_header_t *dst, uint32_t index);
187c5e268c6Sopenharmony_ci
188c5e268c6Sopenharmony_ci// Free camera metadata buffer
189c5e268c6Sopenharmony_civoid FreeCameraMetadataBuffer(common_metadata_header_t *dst);
190c5e268c6Sopenharmony_ci
191c5e268c6Sopenharmony_cistd::string MetadataItemDump(const common_metadata_header_t *metadataHeader, uint32_t item);
192c5e268c6Sopenharmony_ci
193c5e268c6Sopenharmony_cistd::string FormatCameraMetadataToString(const common_metadata_header_t *metadataHeader);
194c5e268c6Sopenharmony_ci
195c5e268c6Sopenharmony_ci// Internal use
196c5e268c6Sopenharmony_cicamera_metadata_item_entry_t *GetMetadataItems(const common_metadata_header_t *metadataHeader);
197c5e268c6Sopenharmony_ciuint8_t *GetMetadataData(const common_metadata_header_t *metadataHeader);
198c5e268c6Sopenharmony_ciint GetCameraMetadataItem(const common_metadata_header_t *src, uint32_t index, camera_metadata_item_t *item);
199c5e268c6Sopenharmony_ciuint32_t GetCameraMetadataItemCount(const common_metadata_header_t *metadata_header);
200c5e268c6Sopenharmony_ciuint32_t GetCameraMetadataItemCapacity(const common_metadata_header_t *metadata_header);
201c5e268c6Sopenharmony_ciuint32_t GetCameraMetadataDataSize(const common_metadata_header_t *metadata_header);
202c5e268c6Sopenharmony_ciint32_t CopyCameraMetadataItems(common_metadata_header_t *newMetadata,
203c5e268c6Sopenharmony_ci    const common_metadata_header_t *oldMetadata);
204c5e268c6Sopenharmony_ciint32_t CalculateCameraMetadataItemDataSize(uint32_t type, size_t data_count);
205c5e268c6Sopenharmony_ciint32_t GetCameraMetadataItemType(uint32_t item, uint32_t *data_type);
206c5e268c6Sopenharmony_cicommon_metadata_header_t *FillCameraMetadata(common_metadata_header_t *buffer, size_t memoryRequired,
207c5e268c6Sopenharmony_ci    uint32_t itemCapacity, uint32_t dataCapacity);
208c5e268c6Sopenharmony_ciint32_t GetMetadataSection(uint32_t itemSection, uint32_t *section);
209c5e268c6Sopenharmony_ciint MetadataExpandItemMem(common_metadata_header_t *dst, camera_metadata_item_entry_t *item,
210c5e268c6Sopenharmony_ci    size_t oldItemSize);
211c5e268c6Sopenharmony_ciint32_t GetAllVendorTags(std::vector<vendorTag_t>& tagVec);
212c5e268c6Sopenharmony_ci} // Camera
213c5e268c6Sopenharmony_ci#endif // CAMERA_METADATA_OPERATOR_H
214