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.
16 #define _GNU_SOURCE // for fdprintf
17 #include <system/camera_metadata.h>
18 #include <cutils/log.h>
25 #define NOT_FOUND -ENOENT
27 * A single metadata entry, storing an array of values of a given type. If the
28 * array is no larger than 4 bytes in size, it is stored in the data.value[]
29 * array; otherwise, it can found in the parent's data array at index
32 typedef struct camera_metadata_entry {
41 } __attribute__((packed)) camera_metadata_entry_t;
44 * A packet of metadata. This is a list of entries, each of which may point to
45 * its values stored at an offset in data.
47 * It is assumed by the utility functions that the memory layout of the packet
50 * |----------------------------------------|
51 * | camera_metadata_t |
53 * |----------------------------------------|
54 * | reserved for future expansion |
55 * |----------------------------------------|
56 * | camera_metadata_entry_t #0 |
57 * |----------------------------------------|
59 * |----------------------------------------|
60 * | camera_metadata_entry_t #entry_count-1 |
61 * |----------------------------------------|
63 * | (entry_capacity-entry_count) entries |
64 * |----------------------------------------|
65 * | start of camera_metadata.data |
67 * |----------------------------------------|
69 * | (data_capacity-data_count) bytes |
70 * |----------------------------------------|
72 * With the total length of the whole packet being camera_metadata.size bytes.
74 * In short, the entries and data are contiguous in memory after the metadata
77 struct camera_metadata {
82 size_t entry_capacity;
83 camera_metadata_entry_t *entries;
90 /** Versioning information */
91 #define CURRENT_METADATA_VERSION 1
93 /** Flag definitions */
94 #define FLAG_SORTED 0x00000001
96 /** Tag information */
98 typedef struct tag_info {
103 #include "camera_metadata_tag_info.c"
105 size_t camera_metadata_type_size[NUM_TYPES] = {
106 [TYPE_BYTE] = sizeof(uint8_t),
107 [TYPE_INT32] = sizeof(int32_t),
108 [TYPE_FLOAT] = sizeof(float),
109 [TYPE_INT64] = sizeof(int64_t),
110 [TYPE_DOUBLE] = sizeof(double),
111 [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
114 char *camera_metadata_type_names[NUM_TYPES] = {
115 [TYPE_BYTE] = "byte",
116 [TYPE_INT32] = "int32",
117 [TYPE_FLOAT] = "float",
118 [TYPE_INT64] = "int64",
119 [TYPE_RATIONAL] = "rational"
122 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
123 size_t data_capacity) {
124 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
126 void *buffer = malloc(memory_needed);
127 return place_camera_metadata(buffer, memory_needed,
132 camera_metadata_t *place_camera_metadata(void *dst,
134 size_t entry_capacity,
135 size_t data_capacity) {
136 if (dst == NULL) return NULL;
137 if (entry_capacity == 0) return NULL;
139 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
141 if (memory_needed > dst_size) return NULL;
143 camera_metadata_t *metadata = (camera_metadata_t*)dst;
144 metadata->version = CURRENT_METADATA_VERSION;
146 metadata->entry_count = 0;
147 metadata->entry_capacity = entry_capacity;
148 metadata->entries = (camera_metadata_entry_t*)(metadata + 1);
149 metadata->data_count = 0;
150 metadata->data_capacity = data_capacity;
151 metadata->size = memory_needed;
152 if (metadata->data_capacity != 0) {
154 (uint8_t*)(metadata->entries + metadata->entry_capacity);
156 metadata->data = NULL;
161 void free_camera_metadata(camera_metadata_t *metadata) {
165 size_t calculate_camera_metadata_size(size_t entry_count,
167 size_t memory_needed = sizeof(camera_metadata_t);
168 memory_needed += sizeof(camera_metadata_entry_t[entry_count]);
169 memory_needed += sizeof(uint8_t[data_count]);
170 return memory_needed;
173 size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
174 if (metadata == NULL) return ERROR;
176 return metadata->size;
179 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
180 if (metadata == NULL) return ERROR;
182 ptrdiff_t reserved_size = metadata->size -
183 calculate_camera_metadata_size(metadata->entry_capacity,
184 metadata->data_capacity);
186 return calculate_camera_metadata_size(metadata->entry_count,
187 metadata->data_count) + reserved_size;
190 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
191 return metadata->entry_count;
194 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
195 return metadata->entry_capacity;
198 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
199 return metadata->data_count;
202 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
203 return metadata->data_capacity;
206 camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
207 const camera_metadata_t *src) {
208 size_t memory_needed = get_camera_metadata_compact_size(src);
210 if (dst == NULL) return NULL;
211 if (dst_size < memory_needed) return NULL;
213 // If copying a newer version of the structure, there may be additional
214 // header fields we don't know about but need to copy
215 ptrdiff_t reserved_size = src->size -
216 calculate_camera_metadata_size(src->entry_capacity,
219 camera_metadata_t *metadata = (camera_metadata_t*)dst;
220 metadata->version = CURRENT_METADATA_VERSION;
221 metadata->flags = src->flags;
222 metadata->entry_count = src->entry_count;
223 metadata->entry_capacity = src->entry_count;
224 metadata->entries = (camera_metadata_entry_t*)
225 ((uint8_t *)(metadata + 1) + reserved_size);
226 metadata->data_count = src->data_count;
227 metadata->data_capacity = src->data_count;
228 metadata->data = (uint8_t *)(metadata->entries + metadata->entry_capacity);
229 metadata->size = memory_needed;
231 if (reserved_size > 0) {
232 memcpy(metadata->reserved, src->reserved, reserved_size);
234 memcpy(metadata->entries, src->entries,
235 sizeof(camera_metadata_entry_t[metadata->entry_count]));
236 memcpy(metadata->data, src->data,
237 sizeof(uint8_t[metadata->data_count]));
242 int append_camera_metadata(camera_metadata_t *dst,
243 const camera_metadata_t *src) {
244 if (dst == NULL || src == NULL ) return ERROR;
246 if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
247 if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
249 memcpy(dst->entries + dst->entry_count, src->entries,
250 sizeof(camera_metadata_entry_t[src->entry_count]));
251 memcpy(dst->data + dst->data_count, src->data,
252 sizeof(uint8_t[src->data_count]));
253 if (dst->data_count != 0) {
255 for (i = dst->entry_count;
256 i < dst->entry_count + src->entry_count;
258 camera_metadata_entry_t *entry = dst->entries + i;
259 if ( camera_metadata_type_size[entry->type] * entry->count > 4 ) {
260 entry->data.offset += dst->data_count;
264 dst->entry_count += src->entry_count;
265 dst->data_count += src->data_count;
266 dst->flags &= ~FLAG_SORTED;
271 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
273 if (type >= NUM_TYPES) return 0;
274 size_t data_bytes = data_count *
275 camera_metadata_type_size[type];
276 return data_bytes <= 4 ? 0 : data_bytes;
279 static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
285 if (dst == NULL) return ERROR;
286 if (dst->entry_count == dst->entry_capacity) return ERROR;
287 if (data == NULL) return ERROR;
290 calculate_camera_metadata_entry_data_size(type, data_count);
292 camera_metadata_entry_t *entry = dst->entries + dst->entry_count;
295 entry->count = data_count;
297 if (data_bytes == 0) {
298 memcpy(entry->data.value, data,
299 data_count * camera_metadata_type_size[type] );
301 entry->data.offset = dst->data_count;
302 memcpy(dst->data + entry->data.offset, data, data_bytes);
303 dst->data_count += data_bytes;
306 dst->flags &= ~FLAG_SORTED;
310 int add_camera_metadata_entry(camera_metadata_t *dst,
315 int type = get_camera_metadata_tag_type(tag);
317 ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
321 return add_camera_metadata_entry_raw(dst,
328 static int compare_entry_tags(const void *p1, const void *p2) {
329 uint32_t tag1 = ((camera_metadata_entry_t*)p1)->tag;
330 uint32_t tag2 = ((camera_metadata_entry_t*)p2)->tag;
331 return tag1 < tag2 ? -1 :
336 int sort_camera_metadata(camera_metadata_t *dst) {
337 if (dst == NULL) return ERROR;
338 if (dst->flags & FLAG_SORTED) return OK;
340 qsort(dst->entries, dst->entry_count,
341 sizeof(camera_metadata_entry_t),
343 dst->flags |= FLAG_SORTED;
348 int get_camera_metadata_entry(camera_metadata_t *src,
353 size_t *data_count) {
354 if (src == NULL ) return ERROR;
355 if (index >= src->entry_count) return ERROR;
357 camera_metadata_entry_t *entry = src->entries + index;
359 if (tag != NULL) *tag = entry->tag;
360 if (type != NULL) *type = entry->type;
361 if (data_count != NULL) *data_count = entry->count;
363 if (entry->count * camera_metadata_type_size[entry->type] > 4) {
364 *data = src->data + entry->data.offset;
366 *data = entry->data.value;
372 int find_camera_metadata_entry(camera_metadata_t *src,
376 size_t *data_count) {
377 if (src == NULL) return ERROR;
379 camera_metadata_entry_t *entry = NULL;
380 if (src->flags & FLAG_SORTED) {
381 // Sorted entries, do a binary search
382 camera_metadata_entry_t key;
384 entry = bsearch(&key,
387 sizeof(camera_metadata_entry_t),
390 // Not sorted, linear search
392 for (i = 0; i < src->entry_count; i++) {
393 if (src->entries[i].tag == tag) {
394 entry = src->entries + i;
399 if (entry == NULL) return NOT_FOUND;
401 if (type != NULL) *type = entry->type;
402 if (data_count != NULL) *data_count = entry->count;
404 if (entry->count * camera_metadata_type_size[entry->type] > 4) {
405 *data = src->data + entry->data.offset;
407 *data = entry->data.value;
413 static const vendor_tag_query_ops_t *vendor_tag_ops = NULL;
415 const char *get_camera_metadata_section_name(uint32_t tag) {
416 uint32_t tag_section = tag >> 16;
417 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
418 return vendor_tag_ops->get_camera_vendor_section_name(
422 if (tag_section >= ANDROID_SECTION_COUNT) {
425 return camera_metadata_section_names[tag_section];
428 const char *get_camera_metadata_tag_name(uint32_t tag) {
429 uint32_t tag_section = tag >> 16;
430 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
431 return vendor_tag_ops->get_camera_vendor_tag_name(
435 if (tag_section >= ANDROID_SECTION_COUNT ||
436 tag >= camera_metadata_section_bounds[tag_section][1] ) {
439 uint32_t tag_index = tag & 0xFFFF;
440 return tag_info[tag_section][tag_index].tag_name;
443 int get_camera_metadata_tag_type(uint32_t tag) {
444 uint32_t tag_section = tag >> 16;
445 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
446 return vendor_tag_ops->get_camera_vendor_tag_type(
450 if (tag_section >= ANDROID_SECTION_COUNT ||
451 tag >= camera_metadata_section_bounds[tag_section][1] ) {
454 uint32_t tag_index = tag & 0xFFFF;
455 return tag_info[tag_section][tag_index].tag_type;
458 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops) {
459 vendor_tag_ops = query_ops;
463 static void print_data(int fd, const uint8_t *data_ptr, int type, int count);
465 void dump_camera_metadata(const camera_metadata_t *metadata,
468 if (metadata == NULL) {
469 ALOGE("%s: Metadata is null.", __FUNCTION__);
474 "Dumping camera metadata array. %d entries, "
475 "%d bytes of extra data.\n",
476 metadata->entry_count, metadata->data_count);
477 fdprintf(fd, " (%d entries and %d bytes data reserved)\n",
478 metadata->entry_capacity, metadata->data_capacity);
479 fdprintf(fd, " Version: %d, Flags: %08x\n",
480 metadata->version, metadata->flags);
481 for (i=0; i < metadata->entry_count; i++) {
482 camera_metadata_entry_t *entry = metadata->entries + i;
484 const char *tag_name, *tag_section;
485 tag_section = get_camera_metadata_section_name(entry->tag);
486 if (tag_section == NULL) {
487 tag_section = "unknownSection";
489 tag_name = get_camera_metadata_tag_name(entry->tag);
490 if (tag_name == NULL) {
491 tag_name = "unknownTag";
493 const char *type_name;
494 if (entry->type >= NUM_TYPES) {
495 type_name = "unknown";
497 type_name = camera_metadata_type_names[entry->type];
499 fdprintf(fd, "Tag: %s.%s (%05x): %s[%d]\n",
506 if (verbosity < 1) continue;
508 if (entry->type >= NUM_TYPES) continue;
510 size_t type_size = camera_metadata_type_size[entry->type];
512 if ( type_size * entry->count > 4 ) {
513 if (entry->data.offset >= metadata->data_count) {
514 ALOGE("%s: Malformed entry data offset: %d (max %d)",
517 metadata->data_count);
520 data_ptr = metadata->data + entry->data.offset;
522 data_ptr = entry->data.value;
524 int count = entry->count;
525 if (verbosity < 2 && count > 16) count = 16;
527 print_data(fd, data_ptr, entry->type, count);
531 static void print_data(int fd, const uint8_t *data_ptr, int type, int count) {
532 static int values_per_line[NUM_TYPES] = {
540 size_t type_size = camera_metadata_type_size[type];
542 int lines = count / values_per_line[type];
543 if (count % values_per_line[type] != 0) lines++;
547 for (j = 0; j < lines; j++) {
550 k < values_per_line[type] && count > 0;
551 k++, count--, index += type_size) {
555 fdprintf(fd, "%hhu ",
556 *(data_ptr + index));
560 *(int32_t*)(data_ptr + index));
563 fdprintf(fd, "%0.2f ",
564 *(float*)(data_ptr + index));
567 fdprintf(fd, "%lld ",
568 *(int64_t*)(data_ptr + index));
571 fdprintf(fd, "%0.2f ",
572 *(float*)(data_ptr + index));
574 case TYPE_RATIONAL: {
575 int32_t numerator = *(int32_t*)(data_ptr + index);
576 int32_t denominator = *(int32_t*)(data_ptr + index + 4);
577 fdprintf(fd, "(%d / %d) ",
578 numerator, denominator);
582 fdprintf(fd, "??? ");