blob: 78318f39d8648a44e11f892f02739502f59ab6d3 [file] [log] [blame]
Zhijun He046205c2015-01-08 17:40:00 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "Metadata"
Zhijun He046205c2015-01-08 17:40:00 -080019
Mark Salyzynd88dfe82017-04-11 08:56:09 -070020#include <log/log.h>
Zhijun He046205c2015-01-08 17:40:00 -080021
22#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
23#include <utils/Trace.h>
24
Mark Salyzynd88dfe82017-04-11 08:56:09 -070025#include <system/camera_metadata.h>
26
Zhijun He046205c2015-01-08 17:40:00 -080027#include "Metadata.h"
28
29namespace usb_camera_hal {
30
31Metadata::Metadata():
32 mData(NULL) {
33}
34
35Metadata::~Metadata() {
36 replace(NULL);
37}
38
39void Metadata::replace(camera_metadata_t *m) {
40 if (m == mData) {
41 return;
42 }
43 if (mData)
44 free_camera_metadata(mData);
45 mData = m;
46}
47
48int Metadata::init(const camera_metadata_t *metadata) {
49 camera_metadata_t* tmp;
50
51 if (!validate_camera_metadata_structure(metadata, NULL))
52 return -EINVAL;
53
54 tmp = clone_camera_metadata(metadata);
55 if (tmp == NULL)
56 return -EINVAL;
57
58 replace(tmp);
59 return 0;
60}
61
62int Metadata::addUInt8(uint32_t tag, int count, const uint8_t *data) {
63 if (!validate(tag, TYPE_BYTE, count)) return -EINVAL;
64 return add(tag, count, data);
65}
66
67int Metadata::add1UInt8(uint32_t tag, const uint8_t data) {
68 return addUInt8(tag, 1, &data);
69}
70
71int Metadata::addInt32(uint32_t tag, int count, const int32_t *data) {
72 if (!validate(tag, TYPE_INT32, count)) return -EINVAL;
73 return add(tag, count, data);
74}
75
76int Metadata::addFloat(uint32_t tag, int count, const float *data) {
77 if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL;
78 return add(tag, count, data);
79}
80
81int Metadata::addInt64(uint32_t tag, int count, const int64_t *data) {
82 if (!validate(tag, TYPE_INT64, count)) return -EINVAL;
83 return add(tag, count, data);
84}
85
86int Metadata::addDouble(uint32_t tag, int count, const double *data) {
87 if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL;
88 return add(tag, count, data);
89}
90
91int Metadata::addRational(uint32_t tag, int count,
92 const camera_metadata_rational_t *data) {
93 if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL;
94 return add(tag, count, data);
95}
96
97bool Metadata::validate(uint32_t tag, int tag_type, int count) {
98 if (get_camera_metadata_tag_type(tag) < 0) {
99 ALOGE("%s: Invalid metadata entry tag: %d", __func__, tag);
100 return false;
101 }
102 if (tag_type < 0 || tag_type >= NUM_TYPES) {
103 ALOGE("%s: Invalid metadata entry tag type: %d", __func__, tag_type);
104 return false;
105 }
106 if (tag_type != get_camera_metadata_tag_type(tag)) {
107 ALOGE("%s: Tag %d called with incorrect type: %s(%d)", __func__, tag,
108 camera_metadata_type_names[tag_type], tag_type);
109 return false;
110 }
111 if (count < 1) {
112 ALOGE("%s: Invalid metadata entry count: %d", __func__, count);
113 return false;
114 }
115 return true;
116}
117
118int Metadata::add(uint32_t tag, int count, const void *tag_data) {
119 // Opportunistically attempt to add if metadata has room for it
120 if (!add_camera_metadata_entry(mData, tag, tag_data, count)) {
121 return 0;
122 }
123
124 int res;
125 camera_metadata_t* tmp;
126 int tag_type = get_camera_metadata_tag_type(tag);
127 size_t size = calculate_camera_metadata_entry_data_size(tag_type, count);
128 size_t entry_capacity = get_camera_metadata_entry_count(mData) + 1;
129 size_t data_capacity = get_camera_metadata_data_count(mData) + size;
130
131 // Double new dimensions to minimize future reallocations
132 tmp = allocate_camera_metadata(entry_capacity * 2, data_capacity * 2);
133 if (tmp == NULL) {
134 ALOGE("%s: Failed to allocate new metadata with %zu entries, %zu data",
135 __func__, entry_capacity, data_capacity);
136 return -ENOMEM;
137 }
138 // Append the current metadata to the new (empty) metadata
139 res = append_camera_metadata(tmp, mData);
140 if (res) {
141 ALOGE("%s: Failed to append old metadata %p to new %p",
142 __func__, mData, tmp);
143 return res;
144 }
145 // Add the remaining new item
146 res = add_camera_metadata_entry(tmp, tag, tag_data, count);
147 if (res) {
148 ALOGE("%s: Failed to add new entry (%d, %p, %d) to metadata %p",
149 __func__, tag, tag_data, count, tmp);
150 return res;
151 }
152
153 replace(tmp);
154 return 0;
155}
156
157camera_metadata_t* Metadata::get() {
158 return mData;
159}
160
161} // namespace usb_camera_hal