2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <system/camera_metadata.h>
18 #include <cutils/log.h>
24 * A single metadata entry, storing an array of values of a given type. If the
25 * array is no larger than 4 bytes in size, it is stored in the data.value[]
26 * array; otherwise, it can found in the parent's data array at index
29 typedef struct camera_metadata_entry {
38 } __attribute__((packed)) camera_metadata_entry_t;
41 * A packet of metadata. This is a list of entries, each of which may point to
42 * its values stored at an offset in data.
44 * It is assumed by the utility functions that the memory layout of the packet
47 * |----------------------------------------|
48 * | camera_metadata_t |
50 * |----------------------------------------|
51 * | reserved for future expansion |
52 * |----------------------------------------|
53 * | camera_metadata_entry_t #0 |
54 * |----------------------------------------|
56 * |----------------------------------------|
57 * | camera_metadata_entry_t #entry_count-1 |
58 * |----------------------------------------|
60 * | (entry_capacity-entry_count) entries |
61 * |----------------------------------------|
62 * | start of camera_metadata.data |
64 * |----------------------------------------|
66 * | (data_capacity-data_count) bytes |
67 * |----------------------------------------|
69 * With the total length of the whole packet being camera_metadata.size bytes.
71 * In short, the entries and data are contiguous in memory after the metadata
74 struct camera_metadata {
77 size_t entry_capacity;
78 camera_metadata_entry_t *entries;
85 typedef struct tag_info {
90 #include "camera_metadata_tag_info.c"
92 size_t camera_metadata_type_size[NUM_TYPES] = {
93 [TYPE_BYTE] = sizeof(uint8_t),
94 [TYPE_INT32] = sizeof(int32_t),
95 [TYPE_FLOAT] = sizeof(float),
96 [TYPE_INT64] = sizeof(int64_t),
97 [TYPE_DOUBLE] = sizeof(double),
98 [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
101 char *camera_metadata_type_names[NUM_TYPES] = {
102 [TYPE_BYTE] = "byte",
103 [TYPE_INT32] = "int32",
104 [TYPE_FLOAT] = "float",
105 [TYPE_INT64] = "int64",
106 [TYPE_RATIONAL] = "rational"
109 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
110 size_t data_capacity) {
111 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
113 void *buffer = malloc(memory_needed);
114 return place_camera_metadata(buffer, memory_needed,
119 camera_metadata_t *place_camera_metadata(void *dst,
121 size_t entry_capacity,
122 size_t data_capacity) {
123 if (dst == NULL) return NULL;
124 if (entry_capacity == 0) return NULL;
126 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
128 if (memory_needed > dst_size) return NULL;
130 camera_metadata_t *metadata = (camera_metadata_t*)dst;
131 metadata->entry_count = 0;
132 metadata->entry_capacity = entry_capacity;
133 metadata->entries = (camera_metadata_entry_t*)(metadata + 1);
134 metadata->data_count = 0;
135 metadata->data_capacity = data_capacity;
136 metadata->size = memory_needed;
137 if (metadata->data_capacity != 0) {
139 (uint8_t*)(metadata->entries + metadata->entry_capacity);
141 metadata->data = NULL;
146 void free_camera_metadata(camera_metadata_t *metadata) {
150 size_t calculate_camera_metadata_size(size_t entry_count,
152 size_t memory_needed = sizeof(camera_metadata_t);
153 memory_needed += sizeof(camera_metadata_entry_t[entry_count]);
154 memory_needed += sizeof(uint8_t[data_count]);
155 return memory_needed;
158 size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
159 if (metadata == NULL) return ERROR;
161 return metadata->size;
164 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
165 if (metadata == NULL) return ERROR;
167 ptrdiff_t reserved_size = metadata->size -
168 calculate_camera_metadata_size(metadata->entry_capacity,
169 metadata->data_capacity);
171 return calculate_camera_metadata_size(metadata->entry_count,
172 metadata->data_count) + reserved_size;
175 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
176 return metadata->entry_count;
179 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
180 return metadata->entry_capacity;
183 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
184 return metadata->data_count;
187 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
188 return metadata->data_capacity;
191 camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
192 const camera_metadata_t *src) {
193 size_t memory_needed = get_camera_metadata_compact_size(src);
195 if (dst == NULL) return NULL;
196 if (dst_size < memory_needed) return NULL;
198 // If copying a newer version of the structure, there may be additional
199 // header fields we don't know about but need to copy
200 ptrdiff_t reserved_size = src->size -
201 calculate_camera_metadata_size(src->entry_capacity,
204 camera_metadata_t *metadata = (camera_metadata_t*)dst;
205 metadata->entry_count = src->entry_count;
206 metadata->entry_capacity = src->entry_count;
207 metadata->entries = (camera_metadata_entry_t*)
208 ((uint8_t *)(metadata + 1) + reserved_size);
209 metadata->data_count = src->data_count;
210 metadata->data_capacity = src->data_count;
211 metadata->data = (uint8_t *)(metadata->entries + metadata->entry_capacity);
212 metadata->size = memory_needed;
214 if (reserved_size > 0) {
215 memcpy(metadata->reserved, src->reserved, reserved_size);
217 memcpy(metadata->entries, src->entries,
218 sizeof(camera_metadata_entry_t[metadata->entry_count]));
219 memcpy(metadata->data, src->data,
220 sizeof(uint8_t[metadata->data_count]));
225 int append_camera_metadata(camera_metadata_t *dst,
226 const camera_metadata_t *src) {
227 if (dst == NULL || src == NULL ) return ERROR;
229 if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
230 if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
232 memcpy(dst->entries + dst->entry_count, src->entries,
233 sizeof(camera_metadata_entry_t[src->entry_count]));
234 memcpy(dst->data + dst->data_count, src->data,
235 sizeof(uint8_t[src->data_count]));
236 if (dst->data_count != 0) {
238 for (i = dst->entry_count;
239 i < dst->entry_count + src->entry_count;
241 camera_metadata_entry_t *entry = dst->entries + i;
242 if ( camera_metadata_type_size[entry->type] * entry->count > 4 ) {
243 entry->data.offset += dst->data_count;
247 dst->entry_count += src->entry_count;
248 dst->data_count += src->data_count;
253 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
255 if (type >= NUM_TYPES) return 0;
256 size_t data_bytes = data_count *
257 camera_metadata_type_size[type];
258 return data_bytes <= 4 ? 0 : data_bytes;
261 int add_camera_metadata_entry_raw(camera_metadata_t *dst,
267 if (dst == NULL) return ERROR;
268 if (dst->entry_count == dst->entry_capacity) return ERROR;
269 if (data == NULL) return ERROR;
272 calculate_camera_metadata_entry_data_size(type, data_count);
274 camera_metadata_entry_t *entry = dst->entries + dst->entry_count;
277 entry->count = data_count;
279 if (data_bytes == 0) {
280 memcpy(entry->data.value, data,
281 data_count * camera_metadata_type_size[type] );
283 entry->data.offset = dst->data_count;
284 memcpy(dst->data + entry->data.offset, data, data_bytes);
285 dst->data_count += data_bytes;
291 int add_camera_metadata_entry(camera_metadata_t *dst,
296 int type = get_camera_metadata_tag_type(tag);
298 ALOGE("Unknown tag %04x (can't find type)", tag);
302 return add_camera_metadata_entry_raw(dst,
309 int get_camera_metadata_entry(camera_metadata_t *src,
314 size_t *data_count) {
315 if (src == NULL ) return ERROR;
316 if (tag == NULL) return ERROR;
317 if (type == NULL ) return ERROR;
318 if (data == NULL) return ERROR;
319 if (data_count == NULL) return ERROR;
321 if (index >= src->entry_count) return ERROR;
323 camera_metadata_entry_t *entry = src->entries + index;
327 *data_count = entry->count;
328 if (entry->count * camera_metadata_type_size[entry->type] > 4) {
329 *data = src->data + entry->data.offset;
331 *data = entry->data.value;
336 static const vendor_tag_query_ops_t *vendor_tag_ops = NULL;
338 const char *get_camera_metadata_section_name(uint32_t tag) {
339 uint32_t tag_section = tag >> 16;
340 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
341 return vendor_tag_ops->get_camera_vendor_section_name(tag);
343 if (tag_section >= ANDROID_SECTION_COUNT) {
346 return camera_metadata_section_names[tag_section];
349 const char *get_camera_metadata_tag_name(uint32_t tag) {
350 uint32_t tag_section = tag >> 16;
351 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
352 return vendor_tag_ops->get_camera_vendor_tag_name(tag);
354 if (tag_section >= ANDROID_SECTION_COUNT ||
355 tag >= camera_metadata_section_bounds[tag_section][1] ) {
358 uint32_t tag_index = tag & 0xFFFF;
359 return tag_info[tag_section][tag_index].tag_name;
362 int get_camera_metadata_tag_type(uint32_t tag) {
363 uint32_t tag_section = tag >> 16;
364 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
365 return vendor_tag_ops->get_camera_vendor_tag_type(tag);
367 if (tag_section >= ANDROID_SECTION_COUNT ||
368 tag >= camera_metadata_section_bounds[tag_section][1] ) {
371 uint32_t tag_index = tag & 0xFFFF;
372 return tag_info[tag_section][tag_index].tag_type;
375 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops) {
376 vendor_tag_ops = query_ops;
380 void print_data(const uint8_t *data_ptr, int type, int count);
382 void dump_camera_metadata(const camera_metadata_t *metadata, int verbosity) {
383 if (metadata == NULL) {
384 ALOGE("Metadata is null.");
388 ALOGD("Dumping camera metadata array. %d entries, %d bytes of extra data.",
389 metadata->entry_count, metadata->data_count);
390 ALOGD(" (%d entries and %d bytes data reserved)",
391 metadata->entry_capacity, metadata->data_capacity);
392 for (i=0; i < metadata->entry_count; i++) {
393 camera_metadata_entry_t *entry = metadata->entries + i;
395 const char *tag_name, *tag_section;
396 tag_section = get_camera_metadata_section_name(entry->tag);
397 if (tag_section == NULL) {
398 tag_section = "unknownSection";
400 tag_name = get_camera_metadata_tag_name(entry->tag);
401 if (tag_name == NULL) {
402 tag_name = "unknownTag";
404 const char *type_name;
405 if (entry->type >= NUM_TYPES) {
406 type_name = "unknown";
408 type_name = camera_metadata_type_names[entry->type];
410 ALOGD("Tag: %s.%s (%05x): %s[%d]",
417 if (verbosity < 1) continue;
419 if (entry->type >= NUM_TYPES) continue;
421 size_t type_size = camera_metadata_type_size[entry->type];
423 if ( type_size * entry->count > 4 ) {
424 if (entry->data.offset >= metadata->data_count) {
425 ALOGE("Malformed entry data offset: %d (max %d)",
427 metadata->data_count);
430 data_ptr = metadata->data + entry->data.offset;
432 data_ptr = entry->data.value;
434 int count = entry->count;
435 if (verbosity < 2 && count > 16) count = 16;
437 print_data(data_ptr, entry->type, count);
441 void print_data(const uint8_t *data_ptr, int type, int count) {
442 static int values_per_line[NUM_TYPES] = {
450 size_t type_size = camera_metadata_type_size[type];
452 int lines = count / values_per_line[type];
453 if (count % values_per_line[type] != 0) lines++;
455 char tmp1[80], tmp2[80];
459 for (j = 0; j < lines; j++) {
462 k < values_per_line[type] && count > 0;
463 k++, count--, index += type_size) {
467 snprintf(tmp2, sizeof(tmp2), "%hhu ",
468 *(data_ptr + index));
471 snprintf(tmp2, sizeof(tmp2), "%d ",
472 *(int32_t*)(data_ptr + index));
475 snprintf(tmp2, sizeof(tmp2), "%0.2f ",
476 *(float*)(data_ptr + index));
479 snprintf(tmp2, sizeof(tmp2), "%lld ",
480 *(int64_t*)(data_ptr + index));
483 snprintf(tmp2, sizeof(tmp2), "%0.2f ",
484 *(float*)(data_ptr + index));
486 case TYPE_RATIONAL: {
487 int32_t numerator = *(int32_t*)(data_ptr + index);
488 int32_t denominator = *(int32_t*)(data_ptr + index + 4);
489 snprintf(tmp2, sizeof(tmp2), "(%d / %d) ",
490 numerator, denominator);
494 snprintf(tmp2, sizeof(tmp2), "??? ");
496 strncat(tmp1, tmp2, sizeof(tmp1));
498 ALOGD(" [ %s]", tmp1);