OSDN Git Service

Merge "camera: Clarify ZSL supports" into oc-dev
[android-x86/system-media.git] / camera / src / camera_metadata.c
1 /*
2  * Copyright (C) 2012 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_TAG "camera_metadata"
18
19 #include <system/camera_metadata.h>
20 #include <camera_metadata_hidden.h>
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <inttypes.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <log/log.h>
29
30 #define OK              0
31 #define ERROR           1
32 #define NOT_FOUND       (-ENOENT)
33 #define SN_EVENT_LOG_ID 0x534e4554
34
35 #define ALIGN_TO(val, alignment) \
36     (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
37
38 /**
39  * A single metadata entry, storing an array of values of a given type. If the
40  * array is no larger than 4 bytes in size, it is stored in the data.value[]
41  * array; otherwise, it can found in the parent's data array at index
42  * data.offset.
43  */
44 #define ENTRY_ALIGNMENT ((size_t) 4)
45 typedef struct camera_metadata_buffer_entry {
46     uint32_t tag;
47     uint32_t count;
48     union {
49         uint32_t offset;
50         uint8_t  value[4];
51     } data;
52     uint8_t  type;
53     uint8_t  reserved[3];
54 } camera_metadata_buffer_entry_t;
55
56 typedef uint32_t metadata_uptrdiff_t;
57 typedef uint32_t metadata_size_t;
58
59 /**
60  * A packet of metadata. This is a list of entries, each of which may point to
61  * its values stored at an offset in data.
62  *
63  * It is assumed by the utility functions that the memory layout of the packet
64  * is as follows:
65  *
66  *   |-----------------------------------------------|
67  *   | camera_metadata_t                             |
68  *   |                                               |
69  *   |-----------------------------------------------|
70  *   | reserved for future expansion                 |
71  *   |-----------------------------------------------|
72  *   | camera_metadata_buffer_entry_t #0             |
73  *   |-----------------------------------------------|
74  *   | ....                                          |
75  *   |-----------------------------------------------|
76  *   | camera_metadata_buffer_entry_t #entry_count-1 |
77  *   |-----------------------------------------------|
78  *   | free space for                                |
79  *   | (entry_capacity-entry_count) entries          |
80  *   |-----------------------------------------------|
81  *   | start of camera_metadata.data                 |
82  *   |                                               |
83  *   |-----------------------------------------------|
84  *   | free space for                                |
85  *   | (data_capacity-data_count) bytes              |
86  *   |-----------------------------------------------|
87  *
88  * With the total length of the whole packet being camera_metadata.size bytes.
89  *
90  * In short, the entries and data are contiguous in memory after the metadata
91  * header.
92  */
93 #define METADATA_ALIGNMENT ((size_t) 4)
94 struct camera_metadata {
95     metadata_size_t          size;
96     uint32_t                 version;
97     uint32_t                 flags;
98     metadata_size_t          entry_count;
99     metadata_size_t          entry_capacity;
100     metadata_uptrdiff_t      entries_start; // Offset from camera_metadata
101     metadata_size_t          data_count;
102     metadata_size_t          data_capacity;
103     metadata_uptrdiff_t      data_start; // Offset from camera_metadata
104     uint32_t                 padding;    // padding to 8 bytes boundary
105     metadata_vendor_id_t     vendor_id;
106 };
107
108 /**
109  * A datum of metadata. This corresponds to camera_metadata_entry_t::data
110  * with the difference that each element is not a pointer. We need to have a
111  * non-pointer type description in order to figure out the largest alignment
112  * requirement for data (DATA_ALIGNMENT).
113  */
114 #define DATA_ALIGNMENT ((size_t) 8)
115 typedef union camera_metadata_data {
116     uint8_t u8;
117     int32_t i32;
118     float   f;
119     int64_t i64;
120     double  d;
121     camera_metadata_rational_t r;
122 } camera_metadata_data_t;
123
124 _Static_assert(sizeof(metadata_size_t) == 4,
125          "Size of metadata_size_t must be 4");
126 _Static_assert(sizeof(metadata_uptrdiff_t) == 4,
127          "Size of metadata_uptrdiff_t must be 4");
128 _Static_assert(sizeof(metadata_vendor_id_t) == 8,
129          "Size of metadata_vendor_id_t must be 8");
130 _Static_assert(sizeof(camera_metadata_data_t) == 8,
131          "Size of camera_metadata_data_t must be 8");
132
133 _Static_assert(offsetof(camera_metadata_buffer_entry_t, tag) == 0,
134          "Offset of tag must be 0");
135 _Static_assert(offsetof(camera_metadata_buffer_entry_t, count) == 4,
136          "Offset of count must be 4");
137 _Static_assert(offsetof(camera_metadata_buffer_entry_t, data) == 8,
138          "Offset of data must be 8");
139 _Static_assert(offsetof(camera_metadata_buffer_entry_t, type) == 12,
140          "Offset of type must be 12");
141 _Static_assert(sizeof(camera_metadata_buffer_entry_t) == 16,
142          "Size of camera_metadata_buffer_entry_t must be 16");
143
144 _Static_assert(offsetof(camera_metadata_t, size) == 0,
145          "Offset of size must be 0");
146 _Static_assert(offsetof(camera_metadata_t, version) == 4,
147          "Offset of version must be 4");
148 _Static_assert(offsetof(camera_metadata_t, flags) == 8,
149          "Offset of flags must be 8");
150 _Static_assert(offsetof(camera_metadata_t, entry_count) == 12,
151          "Offset of entry_count must be 12");
152 _Static_assert(offsetof(camera_metadata_t, entry_capacity) == 16,
153          "Offset of entry_capacity must be 16");
154 _Static_assert(offsetof(camera_metadata_t, entries_start) == 20,
155          "Offset of entries_start must be 20");
156 _Static_assert(offsetof(camera_metadata_t, data_count) == 24,
157          "Offset of data_count must be 24");
158 _Static_assert(offsetof(camera_metadata_t, data_capacity) == 28,
159          "Offset of data_capacity must be 28");
160 _Static_assert(offsetof(camera_metadata_t, data_start) == 32,
161          "Offset of data_start must be 32");
162 _Static_assert(offsetof(camera_metadata_t, vendor_id) == 40,
163          "Offset of vendor_id must be 40");
164 _Static_assert(sizeof(camera_metadata_t) == 48,
165          "Size of camera_metadata_t must be 48");
166
167 /**
168  * The preferred alignment of a packet of camera metadata. In general,
169  * this is the lowest common multiple of the constituents of a metadata
170  * package, i.e, of DATA_ALIGNMENT and ENTRY_ALIGNMENT.
171  */
172 #define MAX_ALIGNMENT(A, B) (((A) > (B)) ? (A) : (B))
173 #define METADATA_PACKET_ALIGNMENT \
174     MAX_ALIGNMENT(MAX_ALIGNMENT(DATA_ALIGNMENT, METADATA_ALIGNMENT), ENTRY_ALIGNMENT)
175
176 /** Versioning information */
177 #define CURRENT_METADATA_VERSION 1
178
179 /** Flag definitions */
180 #define FLAG_SORTED 0x00000001
181
182 /** Tag information */
183
184 typedef struct tag_info {
185     const char *tag_name;
186     uint8_t     tag_type;
187 } tag_info_t;
188
189 #include "camera_metadata_tag_info.c"
190
191 const size_t camera_metadata_type_size[NUM_TYPES] = {
192     [TYPE_BYTE]     = sizeof(uint8_t),
193     [TYPE_INT32]    = sizeof(int32_t),
194     [TYPE_FLOAT]    = sizeof(float),
195     [TYPE_INT64]    = sizeof(int64_t),
196     [TYPE_DOUBLE]   = sizeof(double),
197     [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
198 };
199
200 const char *camera_metadata_type_names[NUM_TYPES] = {
201     [TYPE_BYTE]     = "byte",
202     [TYPE_INT32]    = "int32",
203     [TYPE_FLOAT]    = "float",
204     [TYPE_INT64]    = "int64",
205     [TYPE_DOUBLE]   = "double",
206     [TYPE_RATIONAL] = "rational"
207 };
208
209 static camera_metadata_buffer_entry_t *get_entries(
210         const camera_metadata_t *metadata) {
211     return (camera_metadata_buffer_entry_t*)
212             ((uint8_t*)metadata + metadata->entries_start);
213 }
214
215 static uint8_t *get_data(const camera_metadata_t *metadata) {
216     return (uint8_t*)metadata + metadata->data_start;
217 }
218
219 size_t get_camera_metadata_alignment() {
220     return METADATA_PACKET_ALIGNMENT;
221 }
222
223 camera_metadata_t *allocate_copy_camera_metadata_checked(
224         const camera_metadata_t *src,
225         size_t src_size) {
226
227     if (src == NULL) {
228         return NULL;
229     }
230
231     void *buffer = malloc(src_size);
232     memcpy(buffer, src, src_size);
233
234     camera_metadata_t *metadata = (camera_metadata_t*) buffer;
235     if (validate_camera_metadata_structure(metadata, &src_size) != OK) {
236         free(buffer);
237         return NULL;
238     }
239
240     return metadata;
241 }
242
243 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
244                                             size_t data_capacity) {
245
246     size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
247                                                           data_capacity);
248     void *buffer = malloc(memory_needed);
249     camera_metadata_t *metadata = place_camera_metadata(
250         buffer, memory_needed, entry_capacity, data_capacity);
251     if (!metadata) {
252         /* This should not happen when memory_needed is the same
253          * calculated in this function and in place_camera_metadata.
254          */
255         free(buffer);
256     }
257     return metadata;
258 }
259
260 camera_metadata_t *place_camera_metadata(void *dst,
261                                          size_t dst_size,
262                                          size_t entry_capacity,
263                                          size_t data_capacity) {
264     if (dst == NULL) return NULL;
265
266     size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
267                                                           data_capacity);
268     if (memory_needed > dst_size) return NULL;
269
270     camera_metadata_t *metadata = (camera_metadata_t*)dst;
271     metadata->version = CURRENT_METADATA_VERSION;
272     metadata->flags = 0;
273     metadata->entry_count = 0;
274     metadata->entry_capacity = entry_capacity;
275     metadata->entries_start =
276             ALIGN_TO(sizeof(camera_metadata_t), ENTRY_ALIGNMENT);
277     metadata->data_count = 0;
278     metadata->data_capacity = data_capacity;
279     metadata->size = memory_needed;
280     size_t data_unaligned = (uint8_t*)(get_entries(metadata) +
281             metadata->entry_capacity) - (uint8_t*)metadata;
282     metadata->data_start = ALIGN_TO(data_unaligned, DATA_ALIGNMENT);
283     metadata->vendor_id = CAMERA_METADATA_INVALID_VENDOR_ID;
284
285     assert(validate_camera_metadata_structure(metadata, NULL) == OK);
286     return metadata;
287 }
288 void free_camera_metadata(camera_metadata_t *metadata) {
289     free(metadata);
290 }
291
292 size_t calculate_camera_metadata_size(size_t entry_count,
293                                       size_t data_count) {
294     size_t memory_needed = sizeof(camera_metadata_t);
295     // Start entry list at aligned boundary
296     memory_needed = ALIGN_TO(memory_needed, ENTRY_ALIGNMENT);
297     memory_needed += sizeof(camera_metadata_buffer_entry_t[entry_count]);
298     // Start buffer list at aligned boundary
299     memory_needed = ALIGN_TO(memory_needed, DATA_ALIGNMENT);
300     memory_needed += sizeof(uint8_t[data_count]);
301     // Make sure camera metadata can be stacked in continuous memory
302     memory_needed = ALIGN_TO(memory_needed, METADATA_PACKET_ALIGNMENT);
303     return memory_needed;
304 }
305
306 size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
307     if (metadata == NULL) return ERROR;
308
309     return metadata->size;
310 }
311
312 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
313     if (metadata == NULL) return ERROR;
314
315     return calculate_camera_metadata_size(metadata->entry_count,
316                                           metadata->data_count);
317 }
318
319 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
320     return metadata->entry_count;
321 }
322
323 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
324     return metadata->entry_capacity;
325 }
326
327 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
328     return metadata->data_count;
329 }
330
331 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
332     return metadata->data_capacity;
333 }
334
335 camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
336         const camera_metadata_t *src) {
337     size_t memory_needed = get_camera_metadata_compact_size(src);
338
339     if (dst == NULL) return NULL;
340     if (dst_size < memory_needed) return NULL;
341
342     camera_metadata_t *metadata =
343         place_camera_metadata(dst, dst_size, src->entry_count, src->data_count);
344
345     metadata->flags = src->flags;
346     metadata->entry_count = src->entry_count;
347     metadata->data_count = src->data_count;
348     metadata->vendor_id = src->vendor_id;
349
350     memcpy(get_entries(metadata), get_entries(src),
351             sizeof(camera_metadata_buffer_entry_t[metadata->entry_count]));
352     memcpy(get_data(metadata), get_data(src),
353             sizeof(uint8_t[metadata->data_count]));
354
355     assert(validate_camera_metadata_structure(metadata, NULL) == OK);
356     return metadata;
357 }
358
359 // This method should be used when the camera metadata cannot be trusted. For example, when it's
360 // read from Parcel.
361 static int validate_and_calculate_camera_metadata_entry_data_size(size_t *data_size, uint8_t type,
362         size_t data_count) {
363     if (type >= NUM_TYPES) return ERROR;
364
365     // Check for overflow
366     if (data_count != 0 &&
367             camera_metadata_type_size[type] > (SIZE_MAX - DATA_ALIGNMENT + 1) / data_count) {
368         android_errorWriteLog(SN_EVENT_LOG_ID, "30741779");
369         return ERROR;
370     }
371
372     size_t data_bytes = data_count * camera_metadata_type_size[type];
373
374     if (data_size) {
375         *data_size = data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
376     }
377
378     return OK;
379 }
380
381 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
382         size_t data_count) {
383     if (type >= NUM_TYPES) return 0;
384
385     size_t data_bytes = data_count *
386             camera_metadata_type_size[type];
387
388     return data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
389 }
390
391 int validate_camera_metadata_structure(const camera_metadata_t *metadata,
392                                        const size_t *expected_size) {
393
394     if (metadata == NULL) {
395         ALOGE("%s: metadata is null!", __FUNCTION__);
396         return CAMERA_METADATA_VALIDATION_ERROR;
397     }
398
399     uintptr_t aligned_ptr = ALIGN_TO(metadata, METADATA_PACKET_ALIGNMENT);
400     const uintptr_t alignmentOffset = aligned_ptr - (uintptr_t) metadata;
401
402     // Check that the metadata pointer is well-aligned first.
403     {
404         static const struct {
405             const char *name;
406             size_t alignment;
407         } alignments[] = {
408             {
409                 .name = "camera_metadata",
410                 .alignment = METADATA_ALIGNMENT
411             },
412             {
413                 .name = "camera_metadata_buffer_entry",
414                 .alignment = ENTRY_ALIGNMENT
415             },
416             {
417                 .name = "camera_metadata_data",
418                 .alignment = DATA_ALIGNMENT
419             },
420         };
421
422         for (size_t i = 0; i < sizeof(alignments)/sizeof(alignments[0]); ++i) {
423             uintptr_t aligned_ptr = ALIGN_TO((uintptr_t) metadata + alignmentOffset,
424                     alignments[i].alignment);
425
426             if ((uintptr_t)metadata + alignmentOffset != aligned_ptr) {
427                 ALOGE("%s: Metadata pointer is not aligned (actual %p, "
428                       "expected %p, offset %" PRIuPTR ") to type %s",
429                       __FUNCTION__, metadata,
430                       (void*)aligned_ptr, alignmentOffset, alignments[i].name);
431                 return CAMERA_METADATA_VALIDATION_ERROR;
432             }
433         }
434     }
435
436     /**
437      * Check that the metadata contents are correct
438      */
439
440     if (expected_size != NULL && metadata->size > *expected_size) {
441         ALOGE("%s: Metadata size (%" PRIu32 ") should be <= expected size (%zu)",
442               __FUNCTION__, metadata->size, *expected_size);
443         return CAMERA_METADATA_VALIDATION_ERROR;
444     }
445
446     if (metadata->entry_count > metadata->entry_capacity) {
447         ALOGE("%s: Entry count (%" PRIu32 ") should be <= entry capacity "
448               "(%" PRIu32 ")",
449               __FUNCTION__, metadata->entry_count, metadata->entry_capacity);
450         return CAMERA_METADATA_VALIDATION_ERROR;
451     }
452
453     if (metadata->data_count > metadata->data_capacity) {
454         ALOGE("%s: Data count (%" PRIu32 ") should be <= data capacity "
455               "(%" PRIu32 ")",
456               __FUNCTION__, metadata->data_count, metadata->data_capacity);
457         android_errorWriteLog(SN_EVENT_LOG_ID, "30591838");
458         return CAMERA_METADATA_VALIDATION_ERROR;
459     }
460
461     const metadata_uptrdiff_t entries_end =
462         metadata->entries_start + metadata->entry_capacity;
463     if (entries_end < metadata->entries_start || // overflow check
464         entries_end > metadata->data_start) {
465
466         ALOGE("%s: Entry start + capacity (%" PRIu32 ") should be <= data start "
467               "(%" PRIu32 ")",
468                __FUNCTION__,
469               (metadata->entries_start + metadata->entry_capacity),
470               metadata->data_start);
471         return CAMERA_METADATA_VALIDATION_ERROR;
472     }
473
474     const metadata_uptrdiff_t data_end =
475         metadata->data_start + metadata->data_capacity;
476     if (data_end < metadata->data_start || // overflow check
477         data_end > metadata->size) {
478
479         ALOGE("%s: Data start + capacity (%" PRIu32 ") should be <= total size "
480               "(%" PRIu32 ")",
481                __FUNCTION__,
482               (metadata->data_start + metadata->data_capacity),
483               metadata->size);
484         return CAMERA_METADATA_VALIDATION_ERROR;
485     }
486
487     // Validate each entry
488     const metadata_size_t entry_count = metadata->entry_count;
489     camera_metadata_buffer_entry_t *entries = get_entries(metadata);
490
491     for (size_t i = 0; i < entry_count; ++i) {
492
493         if ((uintptr_t)&entries[i] + alignmentOffset !=
494                 ALIGN_TO((uintptr_t)&entries[i] + alignmentOffset, ENTRY_ALIGNMENT)) {
495             ALOGE("%s: Entry index %zu had bad alignment (address %p),"
496                   " expected alignment %zu",
497                   __FUNCTION__, i, &entries[i], ENTRY_ALIGNMENT);
498             return CAMERA_METADATA_VALIDATION_ERROR;
499         }
500
501         camera_metadata_buffer_entry_t entry = entries[i];
502
503         if (entry.type >= NUM_TYPES) {
504             ALOGE("%s: Entry index %zu had a bad type %d",
505                   __FUNCTION__, i, entry.type);
506             return CAMERA_METADATA_VALIDATION_ERROR;
507         }
508
509         // TODO: fix vendor_tag_ops across processes so we don't need to special
510         //       case vendor-specific tags
511         uint32_t tag_section = entry.tag >> 16;
512         int tag_type = get_local_camera_metadata_tag_type(entry.tag, metadata);
513         if (tag_type != (int)entry.type && tag_section < VENDOR_SECTION) {
514             ALOGE("%s: Entry index %zu had tag type %d, but the type was %d",
515                   __FUNCTION__, i, tag_type, entry.type);
516             return CAMERA_METADATA_VALIDATION_ERROR;
517         }
518
519         size_t data_size;
520         if (validate_and_calculate_camera_metadata_entry_data_size(&data_size, entry.type,
521                 entry.count) != OK) {
522             ALOGE("%s: Entry data size is invalid. type: %u count: %u", __FUNCTION__, entry.type,
523                     entry.count);
524             return CAMERA_METADATA_VALIDATION_ERROR;
525         }
526
527         if (data_size != 0) {
528             camera_metadata_data_t *data =
529                     (camera_metadata_data_t*) (get_data(metadata) +
530                                                entry.data.offset);
531
532             if ((uintptr_t)data + alignmentOffset !=
533                         ALIGN_TO((uintptr_t)data + alignmentOffset, DATA_ALIGNMENT)) {
534                 ALOGE("%s: Entry index %zu had bad data alignment (address %p),"
535                       " expected align %zu, (tag name %s, data size %zu)",
536                       __FUNCTION__, i, data, DATA_ALIGNMENT,
537                       get_local_camera_metadata_tag_name(entry.tag, metadata) ?
538                               : "unknown", data_size);
539                 return CAMERA_METADATA_VALIDATION_ERROR;
540             }
541
542             size_t data_entry_end = entry.data.offset + data_size;
543             if (data_entry_end < entry.data.offset || // overflow check
544                 data_entry_end > metadata->data_capacity) {
545
546                 ALOGE("%s: Entry index %zu data ends (%zu) beyond the capacity "
547                       "%" PRIu32, __FUNCTION__, i, data_entry_end,
548                       metadata->data_capacity);
549                 return CAMERA_METADATA_VALIDATION_ERROR;
550             }
551
552         } else if (entry.count == 0) {
553             if (entry.data.offset != 0) {
554                 ALOGE("%s: Entry index %zu had 0 items, but offset was non-0 "
555                      "(%" PRIu32 "), tag name: %s", __FUNCTION__, i, entry.data.offset,
556                         get_local_camera_metadata_tag_name(entry.tag, metadata) ? : "unknown");
557                 return CAMERA_METADATA_VALIDATION_ERROR;
558             }
559         } // else data stored inline, so we look at value which can be anything.
560     }
561
562     if (alignmentOffset == 0) {
563         return OK;
564     }
565     return CAMERA_METADATA_VALIDATION_SHIFTED;
566 }
567
568 int append_camera_metadata(camera_metadata_t *dst,
569         const camera_metadata_t *src) {
570     if (dst == NULL || src == NULL ) return ERROR;
571
572     // Check for overflow
573     if (src->entry_count + dst->entry_count < src->entry_count) return ERROR;
574     if (src->data_count + dst->data_count < src->data_count) return ERROR;
575     // Check for space
576     if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
577     if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
578
579     if ((dst->vendor_id != CAMERA_METADATA_INVALID_VENDOR_ID) &&
580             (src->vendor_id != CAMERA_METADATA_INVALID_VENDOR_ID)) {
581         if (dst->vendor_id != src->vendor_id) {
582             ALOGE("%s: Append for metadata from different vendors is"
583                     "not supported!", __func__);
584             return ERROR;
585         }
586     }
587
588     memcpy(get_entries(dst) + dst->entry_count, get_entries(src),
589             sizeof(camera_metadata_buffer_entry_t[src->entry_count]));
590     memcpy(get_data(dst) + dst->data_count, get_data(src),
591             sizeof(uint8_t[src->data_count]));
592     if (dst->data_count != 0) {
593         camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
594         for (size_t i = 0; i < src->entry_count; i++, entry++) {
595             if ( calculate_camera_metadata_entry_data_size(entry->type,
596                             entry->count) > 0 ) {
597                 entry->data.offset += dst->data_count;
598             }
599         }
600     }
601     if (dst->entry_count == 0) {
602         // Appending onto empty buffer, keep sorted state
603         dst->flags |= src->flags & FLAG_SORTED;
604     } else if (src->entry_count != 0) {
605         // Both src, dst are nonempty, cannot assume sort remains
606         dst->flags &= ~FLAG_SORTED;
607     } else {
608         // Src is empty, keep dst sorted state
609     }
610     dst->entry_count += src->entry_count;
611     dst->data_count += src->data_count;
612
613     if (dst->vendor_id == CAMERA_METADATA_INVALID_VENDOR_ID) {
614         dst->vendor_id = src->vendor_id;
615     }
616
617     assert(validate_camera_metadata_structure(dst, NULL) == OK);
618     return OK;
619 }
620
621 camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src) {
622     int res;
623     if (src == NULL) return NULL;
624     camera_metadata_t *clone = allocate_camera_metadata(
625         get_camera_metadata_entry_count(src),
626         get_camera_metadata_data_count(src));
627     if (clone != NULL) {
628         res = append_camera_metadata(clone, src);
629         if (res != OK) {
630             free_camera_metadata(clone);
631             clone = NULL;
632         }
633     }
634     assert(validate_camera_metadata_structure(clone, NULL) == OK);
635     return clone;
636 }
637
638 static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
639         uint32_t tag,
640         uint8_t  type,
641         const void *data,
642         size_t data_count) {
643
644     if (dst == NULL) return ERROR;
645     if (dst->entry_count == dst->entry_capacity) return ERROR;
646     if (data_count && data == NULL) return ERROR;
647
648     size_t data_bytes =
649             calculate_camera_metadata_entry_data_size(type, data_count);
650     if (data_bytes + dst->data_count > dst->data_capacity) return ERROR;
651
652     size_t data_payload_bytes =
653             data_count * camera_metadata_type_size[type];
654     camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
655     memset(entry, 0, sizeof(camera_metadata_buffer_entry_t));
656     entry->tag = tag;
657     entry->type = type;
658     entry->count = data_count;
659
660     if (data_bytes == 0) {
661         memcpy(entry->data.value, data,
662                 data_payload_bytes);
663     } else {
664         entry->data.offset = dst->data_count;
665         memcpy(get_data(dst) + entry->data.offset, data,
666                 data_payload_bytes);
667         dst->data_count += data_bytes;
668     }
669     dst->entry_count++;
670     dst->flags &= ~FLAG_SORTED;
671     assert(validate_camera_metadata_structure(dst, NULL) == OK);
672     return OK;
673 }
674
675 int add_camera_metadata_entry(camera_metadata_t *dst,
676         uint32_t tag,
677         const void *data,
678         size_t data_count) {
679
680     int type = get_local_camera_metadata_tag_type(tag, dst);
681     if (type == -1) {
682         ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
683         return ERROR;
684     }
685
686     return add_camera_metadata_entry_raw(dst,
687             tag,
688             type,
689             data,
690             data_count);
691 }
692
693 static int compare_entry_tags(const void *p1, const void *p2) {
694     uint32_t tag1 = ((camera_metadata_buffer_entry_t*)p1)->tag;
695     uint32_t tag2 = ((camera_metadata_buffer_entry_t*)p2)->tag;
696     return  tag1 < tag2 ? -1 :
697             tag1 == tag2 ? 0 :
698             1;
699 }
700
701 int sort_camera_metadata(camera_metadata_t *dst) {
702     if (dst == NULL) return ERROR;
703     if (dst->flags & FLAG_SORTED) return OK;
704
705     qsort(get_entries(dst), dst->entry_count,
706             sizeof(camera_metadata_buffer_entry_t),
707             compare_entry_tags);
708     dst->flags |= FLAG_SORTED;
709
710     assert(validate_camera_metadata_structure(dst, NULL) == OK);
711     return OK;
712 }
713
714 int get_camera_metadata_entry(camera_metadata_t *src,
715         size_t index,
716         camera_metadata_entry_t *entry) {
717     if (src == NULL || entry == NULL) return ERROR;
718     if (index >= src->entry_count) return ERROR;
719
720     camera_metadata_buffer_entry_t *buffer_entry = get_entries(src) + index;
721
722     entry->index = index;
723     entry->tag = buffer_entry->tag;
724     entry->type = buffer_entry->type;
725     entry->count = buffer_entry->count;
726     if (buffer_entry->count *
727             camera_metadata_type_size[buffer_entry->type] > 4) {
728         entry->data.u8 = get_data(src) + buffer_entry->data.offset;
729     } else {
730         entry->data.u8 = buffer_entry->data.value;
731     }
732     return OK;
733 }
734
735 int get_camera_metadata_ro_entry(const camera_metadata_t *src,
736         size_t index,
737         camera_metadata_ro_entry_t *entry) {
738     return get_camera_metadata_entry((camera_metadata_t*)src, index,
739             (camera_metadata_entry_t*)entry);
740 }
741
742 int find_camera_metadata_entry(camera_metadata_t *src,
743         uint32_t tag,
744         camera_metadata_entry_t *entry) {
745     if (src == NULL) return ERROR;
746
747     uint32_t index;
748     if (src->flags & FLAG_SORTED) {
749         // Sorted entries, do a binary search
750         camera_metadata_buffer_entry_t *search_entry = NULL;
751         camera_metadata_buffer_entry_t key;
752         key.tag = tag;
753         search_entry = bsearch(&key,
754                 get_entries(src),
755                 src->entry_count,
756                 sizeof(camera_metadata_buffer_entry_t),
757                 compare_entry_tags);
758         if (search_entry == NULL) return NOT_FOUND;
759         index = search_entry - get_entries(src);
760     } else {
761         // Not sorted, linear search
762         camera_metadata_buffer_entry_t *search_entry = get_entries(src);
763         for (index = 0; index < src->entry_count; index++, search_entry++) {
764             if (search_entry->tag == tag) {
765                 break;
766             }
767         }
768         if (index == src->entry_count) return NOT_FOUND;
769     }
770
771     return get_camera_metadata_entry(src, index,
772             entry);
773 }
774
775 int find_camera_metadata_ro_entry(const camera_metadata_t *src,
776         uint32_t tag,
777         camera_metadata_ro_entry_t *entry) {
778     return find_camera_metadata_entry((camera_metadata_t*)src, tag,
779             (camera_metadata_entry_t*)entry);
780 }
781
782
783 int delete_camera_metadata_entry(camera_metadata_t *dst,
784         size_t index) {
785     if (dst == NULL) return ERROR;
786     if (index >= dst->entry_count) return ERROR;
787
788     camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
789     size_t data_bytes = calculate_camera_metadata_entry_data_size(entry->type,
790             entry->count);
791
792     if (data_bytes > 0) {
793         // Shift data buffer to overwrite deleted data
794         uint8_t *start = get_data(dst) + entry->data.offset;
795         uint8_t *end = start + data_bytes;
796         size_t length = dst->data_count - entry->data.offset - data_bytes;
797         memmove(start, end, length);
798
799         // Update all entry indices to account for shift
800         camera_metadata_buffer_entry_t *e = get_entries(dst);
801         size_t i;
802         for (i = 0; i < dst->entry_count; i++) {
803             if (calculate_camera_metadata_entry_data_size(
804                     e->type, e->count) > 0 &&
805                     e->data.offset > entry->data.offset) {
806                 e->data.offset -= data_bytes;
807             }
808             ++e;
809         }
810         dst->data_count -= data_bytes;
811     }
812     // Shift entry array
813     memmove(entry, entry + 1,
814             sizeof(camera_metadata_buffer_entry_t) *
815             (dst->entry_count - index - 1) );
816     dst->entry_count -= 1;
817
818     assert(validate_camera_metadata_structure(dst, NULL) == OK);
819     return OK;
820 }
821
822 int update_camera_metadata_entry(camera_metadata_t *dst,
823         size_t index,
824         const void *data,
825         size_t data_count,
826         camera_metadata_entry_t *updated_entry) {
827     if (dst == NULL) return ERROR;
828     if (index >= dst->entry_count) return ERROR;
829
830     camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
831
832     size_t data_bytes =
833             calculate_camera_metadata_entry_data_size(entry->type,
834                     data_count);
835     size_t data_payload_bytes =
836             data_count * camera_metadata_type_size[entry->type];
837
838     size_t entry_bytes =
839             calculate_camera_metadata_entry_data_size(entry->type,
840                     entry->count);
841     if (data_bytes != entry_bytes) {
842         // May need to shift/add to data array
843         if (dst->data_capacity < dst->data_count + data_bytes - entry_bytes) {
844             // No room
845             return ERROR;
846         }
847         if (entry_bytes != 0) {
848             // Remove old data
849             uint8_t *start = get_data(dst) + entry->data.offset;
850             uint8_t *end = start + entry_bytes;
851             size_t length = dst->data_count - entry->data.offset - entry_bytes;
852             memmove(start, end, length);
853             dst->data_count -= entry_bytes;
854
855             // Update all entry indices to account for shift
856             camera_metadata_buffer_entry_t *e = get_entries(dst);
857             size_t i;
858             for (i = 0; i < dst->entry_count; i++) {
859                 if (calculate_camera_metadata_entry_data_size(
860                         e->type, e->count) > 0 &&
861                         e->data.offset > entry->data.offset) {
862                     e->data.offset -= entry_bytes;
863                 }
864                 ++e;
865             }
866         }
867
868         if (data_bytes != 0) {
869             // Append new data
870             entry->data.offset = dst->data_count;
871
872             memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
873             dst->data_count += data_bytes;
874         }
875     } else if (data_bytes != 0) {
876         // data size unchanged, reuse same data location
877         memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
878     }
879
880     if (data_bytes == 0) {
881         // Data fits into entry
882         memcpy(entry->data.value, data,
883                 data_payload_bytes);
884     }
885
886     entry->count = data_count;
887
888     if (updated_entry != NULL) {
889         get_camera_metadata_entry(dst,
890                 index,
891                 updated_entry);
892     }
893
894     assert(validate_camera_metadata_structure(dst, NULL) == OK);
895     return OK;
896 }
897
898 static const vendor_tag_ops_t *vendor_tag_ops = NULL;
899 static const struct vendor_tag_cache_ops *vendor_cache_ops = NULL;
900
901 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
902 const char *get_local_camera_metadata_section_name_vendor_id(uint32_t tag,
903         metadata_vendor_id_t id) {
904     uint32_t tag_section = tag >> 16;
905     if (tag_section >= VENDOR_SECTION && vendor_cache_ops != NULL &&
906                id != CAMERA_METADATA_INVALID_VENDOR_ID) {
907            return vendor_cache_ops->get_section_name(tag, id);
908     } else if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
909         return vendor_tag_ops->get_section_name(
910             vendor_tag_ops,
911             tag);
912     }
913     if (tag_section >= ANDROID_SECTION_COUNT) {
914         return NULL;
915     }
916     return camera_metadata_section_names[tag_section];
917 }
918
919 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
920 const char *get_local_camera_metadata_tag_name_vendor_id(uint32_t tag,
921         metadata_vendor_id_t id) {
922     uint32_t tag_section = tag >> 16;
923     if (tag_section >= VENDOR_SECTION && vendor_cache_ops != NULL &&
924                 id != CAMERA_METADATA_INVALID_VENDOR_ID) {
925             return vendor_cache_ops->get_tag_name(tag, id);
926     } else  if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
927         return vendor_tag_ops->get_tag_name(
928             vendor_tag_ops,
929             tag);
930     }
931     if (tag_section >= ANDROID_SECTION_COUNT ||
932         tag >= camera_metadata_section_bounds[tag_section][1] ) {
933         return NULL;
934     }
935     uint32_t tag_index = tag & 0xFFFF;
936     return tag_info[tag_section][tag_index].tag_name;
937 }
938
939 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
940 int get_local_camera_metadata_tag_type_vendor_id(uint32_t tag,
941         metadata_vendor_id_t id) {
942     uint32_t tag_section = tag >> 16;
943     if (tag_section >= VENDOR_SECTION && vendor_cache_ops != NULL &&
944                 id != CAMERA_METADATA_INVALID_VENDOR_ID) {
945             return vendor_cache_ops->get_tag_type(tag, id);
946     } else if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
947         return vendor_tag_ops->get_tag_type(
948             vendor_tag_ops,
949             tag);
950     }
951     if (tag_section >= ANDROID_SECTION_COUNT ||
952             tag >= camera_metadata_section_bounds[tag_section][1] ) {
953         return -1;
954     }
955     uint32_t tag_index = tag & 0xFFFF;
956     return tag_info[tag_section][tag_index].tag_type;
957 }
958
959 const char *get_camera_metadata_section_name(uint32_t tag) {
960     return get_local_camera_metadata_section_name(tag, NULL);
961 }
962
963 const char *get_camera_metadata_tag_name(uint32_t tag) {
964     return get_local_camera_metadata_tag_name(tag, NULL);
965 }
966
967 int get_camera_metadata_tag_type(uint32_t tag) {
968     return get_local_camera_metadata_tag_type(tag, NULL);
969 }
970
971 const char *get_local_camera_metadata_section_name(uint32_t tag,
972         const camera_metadata_t *meta) {
973     metadata_vendor_id_t id = (NULL == meta) ? CAMERA_METADATA_INVALID_VENDOR_ID :
974             meta->vendor_id;
975
976     return get_local_camera_metadata_section_name_vendor_id(tag, id);
977 }
978
979 const char *get_local_camera_metadata_tag_name(uint32_t tag,
980         const camera_metadata_t *meta) {
981     metadata_vendor_id_t id = (NULL == meta) ? CAMERA_METADATA_INVALID_VENDOR_ID :
982             meta->vendor_id;
983
984     return get_local_camera_metadata_tag_name_vendor_id(tag, id);
985 }
986
987 int get_local_camera_metadata_tag_type(uint32_t tag,
988         const camera_metadata_t *meta) {
989     metadata_vendor_id_t id = (NULL == meta) ? CAMERA_METADATA_INVALID_VENDOR_ID :
990             meta->vendor_id;
991
992     return get_local_camera_metadata_tag_type_vendor_id(tag, id);
993 }
994
995 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t* ops) {
996     // **DEPRECATED**
997     (void) ops;
998     ALOGE("%s: This function has been deprecated", __FUNCTION__);
999     return ERROR;
1000 }
1001
1002 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
1003 int set_camera_metadata_vendor_ops(const vendor_tag_ops_t* ops) {
1004     vendor_tag_ops = ops;
1005     return OK;
1006 }
1007
1008 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
1009 int set_camera_metadata_vendor_cache_ops(
1010         const struct vendor_tag_cache_ops *query_cache_ops) {
1011     vendor_cache_ops = query_cache_ops;
1012     return OK;
1013 }
1014
1015 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
1016 void set_camera_metadata_vendor_id(camera_metadata_t *meta,
1017         metadata_vendor_id_t id) {
1018     if (NULL != meta) {
1019         meta->vendor_id = id;
1020     }
1021 }
1022
1023 // Declared in system/media/private/camera/include/camera_metadata_hidden.h
1024 metadata_vendor_id_t get_camera_metadata_vendor_id(
1025         const camera_metadata_t *meta) {
1026     metadata_vendor_id_t ret = CAMERA_METADATA_INVALID_VENDOR_ID;
1027
1028     if (NULL != meta) {
1029         ret = meta->vendor_id;
1030     }
1031
1032     return ret;
1033 }
1034
1035 static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag, int type,
1036         int count,
1037         int indentation);
1038
1039 void dump_camera_metadata(const camera_metadata_t *metadata,
1040         int fd,
1041         int verbosity) {
1042     dump_indented_camera_metadata(metadata, fd, verbosity, 0);
1043 }
1044
1045 void dump_indented_camera_metadata(const camera_metadata_t *metadata,
1046         int fd,
1047         int verbosity,
1048         int indentation) {
1049     if (metadata == NULL) {
1050         dprintf(fd, "%*sDumping camera metadata array: Not allocated\n",
1051                 indentation, "");
1052         return;
1053     }
1054     unsigned int i;
1055     dprintf(fd,
1056             "%*sDumping camera metadata array: %" PRIu32 " / %" PRIu32 " entries, "
1057             "%" PRIu32 " / %" PRIu32 " bytes of extra data.\n", indentation, "",
1058             metadata->entry_count, metadata->entry_capacity,
1059             metadata->data_count, metadata->data_capacity);
1060     dprintf(fd, "%*sVersion: %d, Flags: %08x\n",
1061             indentation + 2, "",
1062             metadata->version, metadata->flags);
1063     camera_metadata_buffer_entry_t *entry = get_entries(metadata);
1064     for (i=0; i < metadata->entry_count; i++, entry++) {
1065
1066         const char *tag_name, *tag_section;
1067         tag_section = get_local_camera_metadata_section_name(entry->tag, metadata);
1068         if (tag_section == NULL) {
1069             tag_section = "unknownSection";
1070         }
1071         tag_name = get_local_camera_metadata_tag_name(entry->tag, metadata);
1072         if (tag_name == NULL) {
1073             tag_name = "unknownTag";
1074         }
1075         const char *type_name;
1076         if (entry->type >= NUM_TYPES) {
1077             type_name = "unknown";
1078         } else {
1079             type_name = camera_metadata_type_names[entry->type];
1080         }
1081         dprintf(fd, "%*s%s.%s (%05x): %s[%" PRIu32 "]\n",
1082              indentation + 2, "",
1083              tag_section,
1084              tag_name,
1085              entry->tag,
1086              type_name,
1087              entry->count);
1088
1089         if (verbosity < 1) continue;
1090
1091         if (entry->type >= NUM_TYPES) continue;
1092
1093         size_t type_size = camera_metadata_type_size[entry->type];
1094         uint8_t *data_ptr;
1095         if ( type_size * entry->count > 4 ) {
1096             if (entry->data.offset >= metadata->data_count) {
1097                 ALOGE("%s: Malformed entry data offset: %" PRIu32 " (max %" PRIu32 ")",
1098                         __FUNCTION__,
1099                         entry->data.offset,
1100                         metadata->data_count);
1101                 continue;
1102             }
1103             data_ptr = get_data(metadata) + entry->data.offset;
1104         } else {
1105             data_ptr = entry->data.value;
1106         }
1107         int count = entry->count;
1108         if (verbosity < 2 && count > 16) count = 16;
1109
1110         print_data(fd, data_ptr, entry->tag, entry->type, count, indentation);
1111     }
1112 }
1113
1114 static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag,
1115         int type, int count, int indentation) {
1116     static int values_per_line[NUM_TYPES] = {
1117         [TYPE_BYTE]     = 16,
1118         [TYPE_INT32]    = 4,
1119         [TYPE_FLOAT]    = 8,
1120         [TYPE_INT64]    = 2,
1121         [TYPE_DOUBLE]   = 4,
1122         [TYPE_RATIONAL] = 2,
1123     };
1124     size_t type_size = camera_metadata_type_size[type];
1125     char value_string_tmp[CAMERA_METADATA_ENUM_STRING_MAX_SIZE];
1126     uint32_t value;
1127
1128     int lines = count / values_per_line[type];
1129     if (count % values_per_line[type] != 0) lines++;
1130
1131     int index = 0;
1132     int j, k;
1133     for (j = 0; j < lines; j++) {
1134         dprintf(fd, "%*s[", indentation + 4, "");
1135         for (k = 0;
1136              k < values_per_line[type] && count > 0;
1137              k++, count--, index += type_size) {
1138
1139             switch (type) {
1140                 case TYPE_BYTE:
1141                     value = *(data_ptr + index);
1142                     if (camera_metadata_enum_snprint(tag,
1143                                                      value,
1144                                                      value_string_tmp,
1145                                                      sizeof(value_string_tmp))
1146                         == OK) {
1147                         dprintf(fd, "%s ", value_string_tmp);
1148                     } else {
1149                         dprintf(fd, "%hhu ",
1150                                 *(data_ptr + index));
1151                     }
1152                     break;
1153                 case TYPE_INT32:
1154                     value =
1155                             *(int32_t*)(data_ptr + index);
1156                     if (camera_metadata_enum_snprint(tag,
1157                                                      value,
1158                                                      value_string_tmp,
1159                                                      sizeof(value_string_tmp))
1160                         == OK) {
1161                         dprintf(fd, "%s ", value_string_tmp);
1162                     } else {
1163                         dprintf(fd, "%" PRId32 " ",
1164                                 *(int32_t*)(data_ptr + index));
1165                     }
1166                     break;
1167                 case TYPE_FLOAT:
1168                     dprintf(fd, "%0.8f ",
1169                             *(float*)(data_ptr + index));
1170                     break;
1171                 case TYPE_INT64:
1172                     dprintf(fd, "%" PRId64 " ",
1173                             *(int64_t*)(data_ptr + index));
1174                     break;
1175                 case TYPE_DOUBLE:
1176                     dprintf(fd, "%0.8f ",
1177                             *(double*)(data_ptr + index));
1178                     break;
1179                 case TYPE_RATIONAL: {
1180                     int32_t numerator = *(int32_t*)(data_ptr + index);
1181                     int32_t denominator = *(int32_t*)(data_ptr + index + 4);
1182                     dprintf(fd, "(%d / %d) ",
1183                             numerator, denominator);
1184                     break;
1185                 }
1186                 default:
1187                     dprintf(fd, "??? ");
1188             }
1189         }
1190         dprintf(fd, "]\n");
1191     }
1192 }