• 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 #include "camera_metadata_info.h"
17 #include <securec.h>
18 #include "metadata_log.h"
19 
20 namespace OHOS::Camera {
CameraMetadata(size_t itemCapacity,size_t dataCapacity)21 CameraMetadata::CameraMetadata(size_t itemCapacity, size_t dataCapacity)
22 {
23     metadata_ = AllocateCameraMetadataBuffer(itemCapacity, AlignTo(dataCapacity, DATA_ALIGNMENT));
24 }
25 
~CameraMetadata()26 CameraMetadata::~CameraMetadata()
27 {
28     if (metadata_) {
29         FreeCameraMetadataBuffer(metadata_);
30         metadata_ = nullptr;
31     }
32 }
33 
addEntry(uint32_t item,const void * data,size_t data_count)34 bool CameraMetadata::addEntry(uint32_t item, const void *data, size_t data_count)
35 {
36     if (metadata_ == nullptr) {
37         METADATA_ERR_LOG("metadata_ is null");
38         return false;
39     }
40 
41     auto result = AddCameraMetadataItem(metadata_, item, data, data_count);
42     if (!result) {
43         return true;
44     }
45 
46     if (result != CAM_META_ITEM_CAP_EXCEED && result != CAM_META_DATA_CAP_EXCEED) {
47         const char *name = GetCameraMetadataItemName(item);
48 
49         if (name) {
50             METADATA_ERR_LOG("Failed to add tag. tagname = %{public}s", name);
51         } else {
52             METADATA_ERR_LOG("Failed to add unknown tag");
53         }
54         return false;
55     }
56 
57     return resize_add_metadata(item, data, data_count);
58 }
59 
resize_add_metadata(uint32_t item,const void * data,size_t data_count)60 bool CameraMetadata::resize_add_metadata(uint32_t item, const void *data, size_t data_count)
61 {
62     uint32_t data_type;
63 
64     auto itemCapacity = GetCameraMetadataItemCapacity(metadata_);
65     auto data_capacity = GetCameraMetadataDataSize(metadata_);
66 
67     int32_t ret = GetCameraMetadataItemType(item, &data_type);
68     if (ret != CAM_META_SUCCESS) {
69         METADATA_ERR_LOG("GetCameraMetadataItemType invalid item type");
70         return false;
71     }
72     size_t size = CalculateCameraMetadataItemDataSize(data_type, data_count);
73 
74     common_metadata_header_t *newMetadata = AllocateCameraMetadataBuffer((itemCapacity + 1) * INDEX_COUNTER,
75         AlignTo((data_capacity + size) * INDEX_COUNTER, DATA_ALIGNMENT));
76 
77     if (newMetadata == nullptr) {
78         METADATA_ERR_LOG("Failed to resize the metadata buffer");
79         return false;
80     }
81 
82     auto result = CopyCameraMetadataItems(newMetadata, metadata_);
83     if (result != CAM_META_SUCCESS) {
84         METADATA_ERR_LOG("Failed to copy the old metadata to new metadata");
85         FreeCameraMetadataBuffer(newMetadata);
86         return false;
87     }
88 
89     result = AddCameraMetadataItem(newMetadata, item, data, data_count);
90     if (result != CAM_META_SUCCESS) {
91         METADATA_ERR_LOG("Failed to add new entry");
92         FreeCameraMetadataBuffer(newMetadata);
93         return false;
94     }
95     replace_metadata(newMetadata);
96 
97     return true;
98 }
99 
replace_metadata(common_metadata_header_t * newMetadata)100 void CameraMetadata::replace_metadata(common_metadata_header_t *newMetadata)
101 {
102     if (metadata_ == newMetadata) {
103         return;
104     }
105 
106     FreeCameraMetadataBuffer(metadata_);
107     metadata_ = newMetadata;
108 }
109 
updateEntry(uint32_t tag,const void * data,size_t dataCount)110 bool CameraMetadata::updateEntry(uint32_t tag, const void *data, size_t dataCount)
111 {
112     if (metadata_ == nullptr) {
113         METADATA_ERR_LOG("metadata_ is null");
114         return false;
115     }
116 
117     const char *name = GetCameraMetadataItemName(tag);
118     (void)name;
119     camera_metadata_item_t item;
120     int ret = FindCameraMetadataItem(metadata_, tag, &item);
121     if (ret) {
122         METADATA_ERR_LOG("Failed to update tag tagname = %{public}s : not present", (name ? name : "<unknown>"));
123         return false;
124     }
125     METADATA_INFO_LOG("updateEntry item id: %{public}d, name: %{public}s, "
126                       "dataCount: %{public}zu", tag, name ? name : "<unknown>", dataCount);
127     ret = UpdateCameraMetadataItemByIndex(metadata_, item.index, data, dataCount, nullptr);
128     if (ret) {
129         const char *name_ = GetCameraMetadataItemName(tag);
130         (void)name_;
131         METADATA_ERR_LOG("Failed to update tag tagname = %{public}s", (name_ ? name_ : "<unknown>"));
132         return false;
133     }
134 
135     return true;
136 }
137 
get()138 common_metadata_header_t *CameraMetadata::get()
139 {
140     return metadata_;
141 }
142 
get() const143 const common_metadata_header_t *CameraMetadata::get() const
144 {
145     return metadata_;
146 }
147 
isValid() const148 bool CameraMetadata::isValid() const
149 {
150     return metadata_ != nullptr;
151 }
152 } // Camera
153