blob: 9b8eb1d3c687e3becbe63de1a56965dd6cb99a78 [file] [log] [blame]
Alex Rayb0be1032013-05-28 15:52:47 -07001/*
2 * Copyright (C) 2013 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 */
Mark Salyzynd88dfe82017-04-11 08:56:09 -070016//#define LOG_NDEBUG 0
17#define LOG_TAG "Metadata"
Alex Rayb0be1032013-05-28 15:52:47 -070018
Dan Albert81895aa2017-10-12 13:31:52 -070019#include <errno.h>
20
Alex Rayb0be1032013-05-28 15:52:47 -070021#include <system/camera_metadata.h>
22
Mark Salyzynd88dfe82017-04-11 08:56:09 -070023#include <log/log.h>
Alex Rayb0be1032013-05-28 15:52:47 -070024
25#define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL)
Alex Rayea803822013-10-14 15:56:43 -070026#include <utils/Trace.h>
Alex Rayb0be1032013-05-28 15:52:47 -070027
28#include "Metadata.h"
29
30namespace default_camera_hal {
31
Alex Ray62735082013-10-21 12:55:24 -070032Metadata::Metadata():
33 mData(NULL)
Alex Rayb0be1032013-05-28 15:52:47 -070034{
Alex Rayb0be1032013-05-28 15:52:47 -070035}
36
37Metadata::~Metadata()
38{
Alex Ray62735082013-10-21 12:55:24 -070039 replace(NULL);
Alex Rayb0be1032013-05-28 15:52:47 -070040}
41
Alex Ray62735082013-10-21 12:55:24 -070042void Metadata::replace(camera_metadata_t *m)
Alex Ray89a82662013-05-28 20:32:48 -070043{
Alex Ray62735082013-10-21 12:55:24 -070044 if (m == mData) {
45 ALOGE("%s: Replacing metadata with itself?!", __func__);
46 return;
Alex Ray89a82662013-05-28 20:32:48 -070047 }
Alex Ray62735082013-10-21 12:55:24 -070048 if (mData)
49 free_camera_metadata(mData);
50 mData = m;
Alex Ray89a82662013-05-28 20:32:48 -070051}
52
Alex Ray62735082013-10-21 12:55:24 -070053int Metadata::init(const camera_metadata_t *metadata)
54{
55 camera_metadata_t* tmp;
56
Jacopo Mondi2ecebbf2015-08-18 17:45:39 +020057 if (validate_camera_metadata_structure(metadata, NULL))
Alex Ray62735082013-10-21 12:55:24 -070058 return -EINVAL;
59
60 tmp = clone_camera_metadata(metadata);
61 if (tmp == NULL)
62 return -EINVAL;
63
64 replace(tmp);
65 return 0;
66}
67
68int Metadata::addUInt8(uint32_t tag, int count, const uint8_t *data)
Alex Rayb0be1032013-05-28 15:52:47 -070069{
70 if (!validate(tag, TYPE_BYTE, count)) return -EINVAL;
71 return add(tag, count, data);
72}
73
Alex Ray62735082013-10-21 12:55:24 -070074int Metadata::add1UInt8(uint32_t tag, const uint8_t data)
75{
76 return addUInt8(tag, 1, &data);
77}
78
79int Metadata::addInt32(uint32_t tag, int count, const int32_t *data)
Alex Rayb0be1032013-05-28 15:52:47 -070080{
81 if (!validate(tag, TYPE_INT32, count)) return -EINVAL;
82 return add(tag, count, data);
83}
84
Alex Ray62735082013-10-21 12:55:24 -070085int Metadata::addFloat(uint32_t tag, int count, const float *data)
Alex Rayb0be1032013-05-28 15:52:47 -070086{
87 if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL;
88 return add(tag, count, data);
89}
90
Alex Ray62735082013-10-21 12:55:24 -070091int Metadata::addInt64(uint32_t tag, int count, const int64_t *data)
Alex Rayb0be1032013-05-28 15:52:47 -070092{
93 if (!validate(tag, TYPE_INT64, count)) return -EINVAL;
94 return add(tag, count, data);
95}
96
Alex Ray62735082013-10-21 12:55:24 -070097int Metadata::addDouble(uint32_t tag, int count, const double *data)
Alex Rayb0be1032013-05-28 15:52:47 -070098{
99 if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL;
100 return add(tag, count, data);
101}
102
103int Metadata::addRational(uint32_t tag, int count,
Alex Ray62735082013-10-21 12:55:24 -0700104 const camera_metadata_rational_t *data)
Alex Rayb0be1032013-05-28 15:52:47 -0700105{
106 if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL;
107 return add(tag, count, data);
108}
109
110bool Metadata::validate(uint32_t tag, int tag_type, int count)
111{
112 if (get_camera_metadata_tag_type(tag) < 0) {
113 ALOGE("%s: Invalid metadata entry tag: %d", __func__, tag);
114 return false;
115 }
116 if (tag_type < 0 || tag_type >= NUM_TYPES) {
117 ALOGE("%s: Invalid metadata entry tag type: %d", __func__, tag_type);
118 return false;
119 }
120 if (tag_type != get_camera_metadata_tag_type(tag)) {
121 ALOGE("%s: Tag %d called with incorrect type: %s(%d)", __func__, tag,
122 camera_metadata_type_names[tag_type], tag_type);
123 return false;
124 }
125 if (count < 1) {
126 ALOGE("%s: Invalid metadata entry count: %d", __func__, count);
127 return false;
128 }
129 return true;
130}
131
Alex Ray62735082013-10-21 12:55:24 -0700132int Metadata::add(uint32_t tag, int count, const void *tag_data)
Alex Rayb0be1032013-05-28 15:52:47 -0700133{
Alex Ray62735082013-10-21 12:55:24 -0700134 int res;
Jacopo Mondi2ecebbf2015-08-18 17:45:39 +0200135 size_t entry_capacity = 0;
136 size_t data_capacity = 0;
Alex Ray62735082013-10-21 12:55:24 -0700137 camera_metadata_t* tmp;
Alex Rayb0be1032013-05-28 15:52:47 -0700138 int tag_type = get_camera_metadata_tag_type(tag);
Alex Ray62735082013-10-21 12:55:24 -0700139 size_t size = calculate_camera_metadata_entry_data_size(tag_type, count);
Alex Rayb0be1032013-05-28 15:52:47 -0700140
Jacopo Mondi2ecebbf2015-08-18 17:45:39 +0200141 if (NULL == mData) {
142 entry_capacity = 1;
143 data_capacity = size;
144 } else {
145 entry_capacity = get_camera_metadata_entry_count(mData) + 1;
146 data_capacity = get_camera_metadata_data_count(mData) + size;
147 }
148
149 // Opportunistically attempt to add if metadata exists and has room for it
150 if (mData && !add_camera_metadata_entry(mData, tag, tag_data, count))
Alex Ray62735082013-10-21 12:55:24 -0700151 return 0;
Alex Ray62735082013-10-21 12:55:24 -0700152 // Double new dimensions to minimize future reallocations
153 tmp = allocate_camera_metadata(entry_capacity * 2, data_capacity * 2);
154 if (tmp == NULL) {
Sasha Levitskiya82f4562014-04-21 17:20:17 -0700155 ALOGE("%s: Failed to allocate new metadata with %zu entries, %zu data",
Alex Ray62735082013-10-21 12:55:24 -0700156 __func__, entry_capacity, data_capacity);
Alex Rayb0be1032013-05-28 15:52:47 -0700157 return -ENOMEM;
Alex Ray62735082013-10-21 12:55:24 -0700158 }
Jacopo Mondi2ecebbf2015-08-18 17:45:39 +0200159 // Append the current metadata to the new (empty) metadata, if any
160 if (NULL != mData) {
161 res = append_camera_metadata(tmp, mData);
162 if (res) {
163 ALOGE("%s: Failed to append old metadata %p to new %p",
164 __func__, mData, tmp);
165 return res;
166 }
Alex Ray62735082013-10-21 12:55:24 -0700167 }
Jacopo Mondi2ecebbf2015-08-18 17:45:39 +0200168 // Add the remaining new item to tmp and replace mData
Sasha Levitskiya82f4562014-04-21 17:20:17 -0700169 res = add_camera_metadata_entry(tmp, tag, tag_data, count);
170 if (res) {
Alex Ray62735082013-10-21 12:55:24 -0700171 ALOGE("%s: Failed to add new entry (%d, %p, %d) to metadata %p",
172 __func__, tag, tag_data, count, tmp);
173 return res;
174 }
Alex Ray62735082013-10-21 12:55:24 -0700175 replace(tmp);
Jacopo Mondi2ecebbf2015-08-18 17:45:39 +0200176
Alex Rayb0be1032013-05-28 15:52:47 -0700177 return 0;
178}
179
Alex Ray62735082013-10-21 12:55:24 -0700180camera_metadata_t* Metadata::get()
Alex Rayb0be1032013-05-28 15:52:47 -0700181{
Alex Ray62735082013-10-21 12:55:24 -0700182 return mData;
Alex Rayb0be1032013-05-28 15:52:47 -0700183}
184
185} // namespace default_camera_hal