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_buffer_entry {
41 } __attribute__((packed)) camera_metadata_buffer_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_buffer_entry_t #0 |
57 * |-----------------------------------------------|
59 * |-----------------------------------------------|
60 * | camera_metadata_buffer_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_buffer_entry_t *entries;
87 void *user; // User set pointer, not copied with buffer
91 /** Versioning information */
92 #define CURRENT_METADATA_VERSION 1
94 /** Flag definitions */
95 #define FLAG_SORTED 0x00000001
97 /** Tag information */
99 typedef struct tag_info {
100 const char *tag_name;
104 #include "camera_metadata_tag_info.c"
106 size_t camera_metadata_type_size[NUM_TYPES] = {
107 [TYPE_BYTE] = sizeof(uint8_t),
108 [TYPE_INT32] = sizeof(int32_t),
109 [TYPE_FLOAT] = sizeof(float),
110 [TYPE_INT64] = sizeof(int64_t),
111 [TYPE_DOUBLE] = sizeof(double),
112 [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
115 char *camera_metadata_type_names[NUM_TYPES] = {
116 [TYPE_BYTE] = "byte",
117 [TYPE_INT32] = "int32",
118 [TYPE_FLOAT] = "float",
119 [TYPE_INT64] = "int64",
120 [TYPE_DOUBLE] = "double",
121 [TYPE_RATIONAL] = "rational"
124 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
125 size_t data_capacity) {
126 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
128 void *buffer = malloc(memory_needed);
129 return place_camera_metadata(buffer, memory_needed,
134 camera_metadata_t *place_camera_metadata(void *dst,
136 size_t entry_capacity,
137 size_t data_capacity) {
138 if (dst == NULL) return NULL;
139 if (entry_capacity == 0) return NULL;
141 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
143 if (memory_needed > dst_size) return NULL;
145 camera_metadata_t *metadata = (camera_metadata_t*)dst;
146 metadata->version = CURRENT_METADATA_VERSION;
148 metadata->entry_count = 0;
149 metadata->entry_capacity = entry_capacity;
150 metadata->entries = (camera_metadata_buffer_entry_t*)(metadata + 1);
151 metadata->data_count = 0;
152 metadata->data_capacity = data_capacity;
153 metadata->size = memory_needed;
154 if (metadata->data_capacity != 0) {
156 (uint8_t*)(metadata->entries + metadata->entry_capacity);
158 metadata->data = NULL;
160 metadata->user = NULL;
164 void free_camera_metadata(camera_metadata_t *metadata) {
168 size_t calculate_camera_metadata_size(size_t entry_count,
170 size_t memory_needed = sizeof(camera_metadata_t);
171 memory_needed += sizeof(camera_metadata_buffer_entry_t[entry_count]);
172 memory_needed += sizeof(uint8_t[data_count]);
173 return memory_needed;
176 size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
177 if (metadata == NULL) return ERROR;
179 return metadata->size;
182 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
183 if (metadata == NULL) return ERROR;
185 ptrdiff_t reserved_size = metadata->size -
186 calculate_camera_metadata_size(metadata->entry_capacity,
187 metadata->data_capacity);
189 return calculate_camera_metadata_size(metadata->entry_count,
190 metadata->data_count) + reserved_size;
193 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
194 return metadata->entry_count;
197 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
198 return metadata->entry_capacity;
201 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
202 return metadata->data_count;
205 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
206 return metadata->data_capacity;
209 camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
210 const camera_metadata_t *src) {
211 size_t memory_needed = get_camera_metadata_compact_size(src);
213 if (dst == NULL) return NULL;
214 if (dst_size < memory_needed) return NULL;
216 // If copying a newer version of the structure, there may be additional
217 // header fields we don't know about but need to copy
218 ptrdiff_t reserved_size = src->size -
219 calculate_camera_metadata_size(src->entry_capacity,
222 camera_metadata_t *metadata = (camera_metadata_t*)dst;
223 metadata->version = CURRENT_METADATA_VERSION;
224 metadata->flags = src->flags;
225 metadata->entry_count = src->entry_count;
226 metadata->entry_capacity = src->entry_count;
227 metadata->entries = (camera_metadata_buffer_entry_t*)
228 ((uint8_t *)(metadata + 1) + reserved_size);
229 metadata->data_count = src->data_count;
230 metadata->data_capacity = src->data_count;
231 metadata->data = (uint8_t *)(metadata->entries + metadata->entry_capacity);
232 metadata->size = memory_needed;
234 if (reserved_size > 0) {
235 memcpy(metadata->reserved, src->reserved, reserved_size);
237 memcpy(metadata->entries, src->entries,
238 sizeof(camera_metadata_buffer_entry_t[metadata->entry_count]));
239 memcpy(metadata->data, src->data,
240 sizeof(uint8_t[metadata->data_count]));
241 metadata->user = NULL;
246 int append_camera_metadata(camera_metadata_t *dst,
247 const camera_metadata_t *src) {
248 if (dst == NULL || src == NULL ) return ERROR;
250 if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
251 if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
253 memcpy(dst->entries + dst->entry_count, src->entries,
254 sizeof(camera_metadata_buffer_entry_t[src->entry_count]));
255 memcpy(dst->data + dst->data_count, src->data,
256 sizeof(uint8_t[src->data_count]));
257 if (dst->data_count != 0) {
259 for (i = dst->entry_count;
260 i < dst->entry_count + src->entry_count;
262 camera_metadata_buffer_entry_t *entry = dst->entries + i;
263 if ( camera_metadata_type_size[entry->type] * entry->count > 4 ) {
264 entry->data.offset += dst->data_count;
268 if (dst->entry_count == 0) {
269 // Appending onto empty buffer, keep sorted state
270 dst->flags |= src->flags & FLAG_SORTED;
271 } else if (src->entry_count != 0) {
272 // Both src, dst are nonempty, cannot assume sort remains
273 dst->flags &= ~FLAG_SORTED;
275 // Src is empty, keep dst sorted state
277 dst->entry_count += src->entry_count;
278 dst->data_count += src->data_count;
283 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
285 if (type >= NUM_TYPES) return 0;
286 size_t data_bytes = data_count *
287 camera_metadata_type_size[type];
288 return data_bytes <= 4 ? 0 : data_bytes;
291 static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
297 if (dst == NULL) return ERROR;
298 if (dst->entry_count == dst->entry_capacity) return ERROR;
299 if (data == NULL) return ERROR;
302 calculate_camera_metadata_entry_data_size(type, data_count);
304 camera_metadata_buffer_entry_t *entry = dst->entries + dst->entry_count;
307 entry->count = data_count;
309 if (data_bytes == 0) {
310 memcpy(entry->data.value, data,
311 data_count * camera_metadata_type_size[type] );
313 entry->data.offset = dst->data_count;
314 memcpy(dst->data + entry->data.offset, data, data_bytes);
315 dst->data_count += data_bytes;
318 dst->flags &= ~FLAG_SORTED;
322 int add_camera_metadata_entry(camera_metadata_t *dst,
327 int type = get_camera_metadata_tag_type(tag);
329 ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
333 return add_camera_metadata_entry_raw(dst,
340 static int compare_entry_tags(const void *p1, const void *p2) {
341 uint32_t tag1 = ((camera_metadata_buffer_entry_t*)p1)->tag;
342 uint32_t tag2 = ((camera_metadata_buffer_entry_t*)p2)->tag;
343 return tag1 < tag2 ? -1 :
348 int sort_camera_metadata(camera_metadata_t *dst) {
349 if (dst == NULL) return ERROR;
350 if (dst->flags & FLAG_SORTED) return OK;
352 qsort(dst->entries, dst->entry_count,
353 sizeof(camera_metadata_buffer_entry_t),
355 dst->flags |= FLAG_SORTED;
360 int get_camera_metadata_entry(camera_metadata_t *src,
362 camera_metadata_entry_t *entry) {
363 if (src == NULL || entry == NULL) return ERROR;
364 if (index >= src->entry_count) return ERROR;
366 camera_metadata_buffer_entry_t *buffer_entry = src->entries + index;
368 entry->index = index;
369 entry->tag = buffer_entry->tag;
370 entry->type = buffer_entry->type;
371 entry->count = buffer_entry->count;
372 if (buffer_entry->count *
373 camera_metadata_type_size[buffer_entry->type] > 4) {
374 entry->data.u8 = src->data + buffer_entry->data.offset;
376 entry->data.u8 = buffer_entry->data.value;
381 int find_camera_metadata_entry(camera_metadata_t *src,
383 camera_metadata_entry_t *entry) {
384 if (src == NULL) return ERROR;
387 if (src->flags & FLAG_SORTED) {
388 // Sorted entries, do a binary search
389 camera_metadata_buffer_entry_t *search_entry = NULL;
390 camera_metadata_buffer_entry_t key;
392 search_entry = bsearch(&key,
395 sizeof(camera_metadata_buffer_entry_t),
397 if (search_entry == NULL) return NOT_FOUND;
398 index = search_entry - src->entries;
400 // Not sorted, linear search
401 for (index = 0; index < src->entry_count; index++) {
402 if (src->entries[index].tag == tag) {
406 if (index == src->entry_count) return NOT_FOUND;
409 return get_camera_metadata_entry(src, index,
413 int delete_camera_metadata_entry(camera_metadata_t *dst,
415 if (dst == NULL) return ERROR;
416 if (index >= dst->entry_count) return ERROR;
418 camera_metadata_buffer_entry_t *entry = dst->entries + index;
419 size_t data_bytes = calculate_camera_metadata_entry_data_size(entry->type,
422 if (data_bytes > 0) {
423 // Shift data buffer to overwrite deleted data
424 uint8_t *start = dst->data + entry->data.offset;
425 uint8_t *end = start + data_bytes;
426 size_t length = dst->data_count - entry->data.offset - data_bytes;
427 memmove(start, end, length);
429 // Update all entry indices to account for shift
430 camera_metadata_buffer_entry_t *e = dst->entries;
432 for (i = 0; i < dst->entry_count; i++) {
433 if (calculate_camera_metadata_entry_data_size(
434 e->type, e->count) > 0 &&
435 e->data.offset > entry->data.offset) {
436 e->data.offset -= data_bytes;
440 dst->data_count -= data_bytes;
443 memmove(entry, entry + 1,
444 sizeof(camera_metadata_buffer_entry_t) *
445 (dst->entry_count - index - 1) );
446 dst->entry_count -= 1;
451 int update_camera_metadata_entry(camera_metadata_t *dst,
455 camera_metadata_entry_t *updated_entry) {
456 if (dst == NULL) return ERROR;
457 if (index >= dst->entry_count) return ERROR;
459 camera_metadata_buffer_entry_t *entry = dst->entries + index;
462 calculate_camera_metadata_entry_data_size(entry->type,
465 calculate_camera_metadata_entry_data_size(entry->type,
467 if (data_bytes != entry_bytes) {
468 // May need to shift/add to data array
469 if (dst->data_capacity < dst->data_count + data_bytes - entry_bytes) {
473 if (entry_bytes != 0) {
475 uint8_t *start = dst->data + entry->data.offset;
476 uint8_t *end = start + entry_bytes;
477 size_t length = dst->data_count - entry->data.offset - entry_bytes;
478 memmove(start, end, length);
479 dst->data_count -= entry_bytes;
481 // Update all entry indices to account for shift
482 camera_metadata_buffer_entry_t *e = dst->entries;
484 for (i = 0; i < dst->entry_count; i++) {
485 if (calculate_camera_metadata_entry_data_size(
486 e->type, e->count) > 0 &&
487 e->data.offset > entry->data.offset) {
488 e->data.offset -= entry_bytes;
494 if (data_bytes != 0) {
496 entry->data.offset = dst->data_count;
498 memcpy(dst->data + entry->data.offset, data, data_bytes);
499 dst->data_count += data_bytes;
501 } else if (data_bytes != 0) {
502 // data size unchanged, reuse same data location
503 memcpy(dst->data + entry->data.offset, data, data_bytes);
506 if (data_bytes == 0) {
507 // Data fits into entry
508 memcpy(entry->data.value, data,
509 data_count * camera_metadata_type_size[entry->type]);
512 entry->count = data_count;
514 if (updated_entry != NULL) {
515 get_camera_metadata_entry(dst,
523 int set_camera_metadata_user_pointer(camera_metadata_t *dst, void* user) {
524 if (dst == NULL) return ERROR;
529 int get_camera_metadata_user_pointer(camera_metadata_t *dst, void** user) {
530 if (dst == NULL) return ERROR;
535 static const vendor_tag_query_ops_t *vendor_tag_ops = NULL;
537 const char *get_camera_metadata_section_name(uint32_t tag) {
538 uint32_t tag_section = tag >> 16;
539 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
540 return vendor_tag_ops->get_camera_vendor_section_name(
544 if (tag_section >= ANDROID_SECTION_COUNT) {
547 return camera_metadata_section_names[tag_section];
550 const char *get_camera_metadata_tag_name(uint32_t tag) {
551 uint32_t tag_section = tag >> 16;
552 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
553 return vendor_tag_ops->get_camera_vendor_tag_name(
557 if (tag_section >= ANDROID_SECTION_COUNT ||
558 tag >= camera_metadata_section_bounds[tag_section][1] ) {
561 uint32_t tag_index = tag & 0xFFFF;
562 return tag_info[tag_section][tag_index].tag_name;
565 int get_camera_metadata_tag_type(uint32_t tag) {
566 uint32_t tag_section = tag >> 16;
567 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
568 return vendor_tag_ops->get_camera_vendor_tag_type(
572 if (tag_section >= ANDROID_SECTION_COUNT ||
573 tag >= camera_metadata_section_bounds[tag_section][1] ) {
576 uint32_t tag_index = tag & 0xFFFF;
577 return tag_info[tag_section][tag_index].tag_type;
580 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops) {
581 vendor_tag_ops = query_ops;
585 static void print_data(int fd, const uint8_t *data_ptr, int type, int count);
587 void dump_camera_metadata(const camera_metadata_t *metadata,
590 if (metadata == NULL) {
591 ALOGE("%s: Metadata is null.", __FUNCTION__);
596 "Dumping camera metadata array. %d entries, "
597 "%d bytes of extra data.\n",
598 metadata->entry_count, metadata->data_count);
599 fdprintf(fd, " (%d entries and %d bytes data reserved)\n",
600 metadata->entry_capacity, metadata->data_capacity);
601 fdprintf(fd, " Version: %d, Flags: %08x\n",
602 metadata->version, metadata->flags);
603 for (i=0; i < metadata->entry_count; i++) {
604 camera_metadata_buffer_entry_t *entry = metadata->entries + i;
606 const char *tag_name, *tag_section;
607 tag_section = get_camera_metadata_section_name(entry->tag);
608 if (tag_section == NULL) {
609 tag_section = "unknownSection";
611 tag_name = get_camera_metadata_tag_name(entry->tag);
612 if (tag_name == NULL) {
613 tag_name = "unknownTag";
615 const char *type_name;
616 if (entry->type >= NUM_TYPES) {
617 type_name = "unknown";
619 type_name = camera_metadata_type_names[entry->type];
621 fdprintf(fd, "Tag: %s.%s (%05x): %s[%d]\n",
628 if (verbosity < 1) continue;
630 if (entry->type >= NUM_TYPES) continue;
632 size_t type_size = camera_metadata_type_size[entry->type];
634 if ( type_size * entry->count > 4 ) {
635 if (entry->data.offset >= metadata->data_count) {
636 ALOGE("%s: Malformed entry data offset: %d (max %d)",
639 metadata->data_count);
642 data_ptr = metadata->data + entry->data.offset;
644 data_ptr = entry->data.value;
646 int count = entry->count;
647 if (verbosity < 2 && count > 16) count = 16;
649 print_data(fd, data_ptr, entry->type, count);
653 static void print_data(int fd, const uint8_t *data_ptr, int type, int count) {
654 static int values_per_line[NUM_TYPES] = {
662 size_t type_size = camera_metadata_type_size[type];
664 int lines = count / values_per_line[type];
665 if (count % values_per_line[type] != 0) lines++;
669 for (j = 0; j < lines; j++) {
672 k < values_per_line[type] && count > 0;
673 k++, count--, index += type_size) {
677 fdprintf(fd, "%hhu ",
678 *(data_ptr + index));
682 *(int32_t*)(data_ptr + index));
685 fdprintf(fd, "%0.2f ",
686 *(float*)(data_ptr + index));
689 fdprintf(fd, "%lld ",
690 *(int64_t*)(data_ptr + index));
693 fdprintf(fd, "%0.2f ",
694 *(float*)(data_ptr + index));
696 case TYPE_RATIONAL: {
697 int32_t numerator = *(int32_t*)(data_ptr + index);
698 int32_t denominator = *(int32_t*)(data_ptr + index + 4);
699 fdprintf(fd, "(%d / %d) ",
700 numerator, denominator);
704 fdprintf(fd, "??? ");