blob: 7c05a97b0f20bb42f69f8d92dc84fd12e052e28b [file] [log] [blame]
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001/*
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 */
Elliott Hughes8e86c9c2014-05-22 01:18:02 -070016
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -070017#define _GNU_SOURCE // for fdprintf
Mark Salyzyn773e5c82014-03-13 13:24:19 -070018#include <inttypes.h>
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080019#include <system/camera_metadata.h>
Ruben Brunk9c13e192014-02-14 16:40:56 -080020#include <camera_metadata_hidden.h>
Igor Murashkind1cecc12013-04-30 17:21:54 -070021
22#define LOG_TAG "camera_metadata"
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080023#include <cutils/log.h>
Alex Ray151389f2013-11-12 18:09:31 -080024#include <assert.h>
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <errno.h>
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080028
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070029#define OK 0
30#define ERROR 1
31#define NOT_FOUND -ENOENT
Eino-Ville Talvala31540362012-11-07 16:36:50 -080032
Eino-Ville Talvala31540362012-11-07 16:36:50 -080033#define ALIGN_TO(val, alignment) \
Igor Murashkind1cecc12013-04-30 17:21:54 -070034 (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
35
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080036/**
37 * A single metadata entry, storing an array of values of a given type. If the
38 * array is no larger than 4 bytes in size, it is stored in the data.value[]
39 * array; otherwise, it can found in the parent's data array at index
40 * data.offset.
41 */
Narayan Kamath21532652014-05-30 15:25:18 +010042#define ENTRY_ALIGNMENT ((size_t) 4)
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -070043typedef struct camera_metadata_buffer_entry {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080044 uint32_t tag;
Narayan Kamath21532652014-05-30 15:25:18 +010045 uint32_t count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080046 union {
Narayan Kamath21532652014-05-30 15:25:18 +010047 uint32_t offset;
48 uint8_t value[4];
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080049 } data;
50 uint8_t type;
51 uint8_t reserved[3];
Eino-Ville Talvala31540362012-11-07 16:36:50 -080052} camera_metadata_buffer_entry_t;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080053
Narayan Kamath21532652014-05-30 15:25:18 +010054typedef uint32_t metadata_uptrdiff_t;
55typedef uint32_t metadata_size_t;
56
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080057/**
58 * A packet of metadata. This is a list of entries, each of which may point to
59 * its values stored at an offset in data.
60 *
61 * It is assumed by the utility functions that the memory layout of the packet
62 * is as follows:
63 *
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -070064 * |-----------------------------------------------|
65 * | camera_metadata_t |
66 * | |
67 * |-----------------------------------------------|
68 * | reserved for future expansion |
69 * |-----------------------------------------------|
70 * | camera_metadata_buffer_entry_t #0 |
71 * |-----------------------------------------------|
72 * | .... |
73 * |-----------------------------------------------|
74 * | camera_metadata_buffer_entry_t #entry_count-1 |
75 * |-----------------------------------------------|
76 * | free space for |
77 * | (entry_capacity-entry_count) entries |
78 * |-----------------------------------------------|
79 * | start of camera_metadata.data |
80 * | |
81 * |-----------------------------------------------|
82 * | free space for |
83 * | (data_capacity-data_count) bytes |
84 * |-----------------------------------------------|
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080085 *
86 * With the total length of the whole packet being camera_metadata.size bytes.
87 *
88 * In short, the entries and data are contiguous in memory after the metadata
89 * header.
90 */
Narayan Kamath21532652014-05-30 15:25:18 +010091#define METADATA_ALIGNMENT ((size_t) 4)
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080092struct camera_metadata {
Narayan Kamath21532652014-05-30 15:25:18 +010093 metadata_size_t size;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070094 uint32_t version;
95 uint32_t flags;
Narayan Kamath21532652014-05-30 15:25:18 +010096 metadata_size_t entry_count;
97 metadata_size_t entry_capacity;
98 metadata_uptrdiff_t entries_start; // Offset from camera_metadata
99 metadata_size_t data_count;
100 metadata_size_t data_capacity;
101 metadata_uptrdiff_t data_start; // Offset from camera_metadata
102 uint8_t reserved[];
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800103};
104
Igor Murashkin555aac82012-11-15 10:54:57 -0800105/**
106 * A datum of metadata. This corresponds to camera_metadata_entry_t::data
107 * with the difference that each element is not a pointer. We need to have a
108 * non-pointer type description in order to figure out the largest alignment
109 * requirement for data (DATA_ALIGNMENT).
110 */
Narayan Kamath21532652014-05-30 15:25:18 +0100111#define DATA_ALIGNMENT ((size_t) 8)
Igor Murashkin555aac82012-11-15 10:54:57 -0800112typedef union camera_metadata_data {
113 uint8_t u8;
114 int32_t i32;
115 float f;
116 int64_t i64;
117 double d;
118 camera_metadata_rational_t r;
119} camera_metadata_data_t;
120
Narayan Kamath21532652014-05-30 15:25:18 +0100121/**
122 * The preferred alignment of a packet of camera metadata. In general,
123 * this is the lowest common multiple of the constituents of a metadata
124 * package, i.e, of DATA_ALIGNMENT and ENTRY_ALIGNMENT.
125 */
126#define MAX_ALIGNMENT(A, B) (((A) > (B)) ? (A) : (B))
127#define METADATA_PACKET_ALIGNMENT \
128 MAX_ALIGNMENT(MAX_ALIGNMENT(DATA_ALIGNMENT, METADATA_ALIGNMENT), ENTRY_ALIGNMENT);
129
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700130/** Versioning information */
131#define CURRENT_METADATA_VERSION 1
132
133/** Flag definitions */
134#define FLAG_SORTED 0x00000001
135
136/** Tag information */
137
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800138typedef struct tag_info {
139 const char *tag_name;
140 uint8_t tag_type;
141} tag_info_t;
142
143#include "camera_metadata_tag_info.c"
144
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700145const size_t camera_metadata_type_size[NUM_TYPES] = {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800146 [TYPE_BYTE] = sizeof(uint8_t),
147 [TYPE_INT32] = sizeof(int32_t),
148 [TYPE_FLOAT] = sizeof(float),
149 [TYPE_INT64] = sizeof(int64_t),
150 [TYPE_DOUBLE] = sizeof(double),
151 [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
152};
153
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700154const char *camera_metadata_type_names[NUM_TYPES] = {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800155 [TYPE_BYTE] = "byte",
156 [TYPE_INT32] = "int32",
157 [TYPE_FLOAT] = "float",
158 [TYPE_INT64] = "int64",
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700159 [TYPE_DOUBLE] = "double",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800160 [TYPE_RATIONAL] = "rational"
161};
162
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800163static camera_metadata_buffer_entry_t *get_entries(
164 const camera_metadata_t *metadata) {
165 return (camera_metadata_buffer_entry_t*)
166 ((uint8_t*)metadata + metadata->entries_start);
167}
168
169static uint8_t *get_data(const camera_metadata_t *metadata) {
170 return (uint8_t*)metadata + metadata->data_start;
171}
172
Zhijun He1450f7f2013-12-05 07:44:56 -0800173size_t get_camera_metadata_alignment() {
Narayan Kamath21532652014-05-30 15:25:18 +0100174 return METADATA_PACKET_ALIGNMENT;
Zhijun He1450f7f2013-12-05 07:44:56 -0800175}
176
Igor Murashkind1cecc12013-04-30 17:21:54 -0700177camera_metadata_t *allocate_copy_camera_metadata_checked(
178 const camera_metadata_t *src,
179 size_t src_size) {
180
181 if (src == NULL) {
182 return NULL;
183 }
184
185 void *buffer = malloc(src_size);
186 memcpy(buffer, src, src_size);
187
188 camera_metadata_t *metadata = (camera_metadata_t*) buffer;
189 if (validate_camera_metadata_structure(metadata, &src_size) != OK) {
190 free(buffer);
191 return NULL;
192 }
193
194 return metadata;
195}
196
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800197camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
198 size_t data_capacity) {
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700199
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800200 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
201 data_capacity);
202 void *buffer = malloc(memory_needed);
203 return place_camera_metadata(buffer, memory_needed,
204 entry_capacity,
205 data_capacity);
206}
207
208camera_metadata_t *place_camera_metadata(void *dst,
209 size_t dst_size,
210 size_t entry_capacity,
211 size_t data_capacity) {
212 if (dst == NULL) return NULL;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800213
214 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
215 data_capacity);
216 if (memory_needed > dst_size) return NULL;
217
218 camera_metadata_t *metadata = (camera_metadata_t*)dst;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700219 metadata->version = CURRENT_METADATA_VERSION;
220 metadata->flags = 0;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800221 metadata->entry_count = 0;
222 metadata->entry_capacity = entry_capacity;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800223 metadata->entries_start =
224 ALIGN_TO(sizeof(camera_metadata_t), ENTRY_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800225 metadata->data_count = 0;
226 metadata->data_capacity = data_capacity;
227 metadata->size = memory_needed;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700228 size_t data_unaligned = (uint8_t*)(get_entries(metadata) +
229 metadata->entry_capacity) - (uint8_t*)metadata;
230 metadata->data_start = ALIGN_TO(data_unaligned, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800231
Alex Ray151389f2013-11-12 18:09:31 -0800232 assert(validate_camera_metadata_structure(metadata, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800233 return metadata;
234}
235void free_camera_metadata(camera_metadata_t *metadata) {
236 free(metadata);
237}
238
239size_t calculate_camera_metadata_size(size_t entry_count,
240 size_t data_count) {
241 size_t memory_needed = sizeof(camera_metadata_t);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800242 // Start entry list at aligned boundary
243 memory_needed = ALIGN_TO(memory_needed, ENTRY_ALIGNMENT);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700244 memory_needed += sizeof(camera_metadata_buffer_entry_t[entry_count]);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800245 // Start buffer list at aligned boundary
246 memory_needed = ALIGN_TO(memory_needed, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800247 memory_needed += sizeof(uint8_t[data_count]);
248 return memory_needed;
249}
250
251size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
252 if (metadata == NULL) return ERROR;
253
254 return metadata->size;
255}
256
257size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
258 if (metadata == NULL) return ERROR;
259
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800260 return calculate_camera_metadata_size(metadata->entry_count,
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800261 metadata->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800262}
263
264size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
265 return metadata->entry_count;
266}
267
268size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
269 return metadata->entry_capacity;
270}
271
272size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
273 return metadata->data_count;
274}
275
276size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
277 return metadata->data_capacity;
278}
279
280camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
281 const camera_metadata_t *src) {
282 size_t memory_needed = get_camera_metadata_compact_size(src);
283
284 if (dst == NULL) return NULL;
285 if (dst_size < memory_needed) return NULL;
286
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800287 camera_metadata_t *metadata =
288 place_camera_metadata(dst, dst_size, src->entry_count, src->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800289
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700290 metadata->flags = src->flags;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800291 metadata->entry_count = src->entry_count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800292 metadata->data_count = src->data_count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800293
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800294 memcpy(get_entries(metadata), get_entries(src),
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700295 sizeof(camera_metadata_buffer_entry_t[metadata->entry_count]));
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800296 memcpy(get_data(metadata), get_data(src),
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800297 sizeof(uint8_t[metadata->data_count]));
298
Alex Ray151389f2013-11-12 18:09:31 -0800299 assert(validate_camera_metadata_structure(metadata, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800300 return metadata;
301}
302
Igor Murashkind1cecc12013-04-30 17:21:54 -0700303int validate_camera_metadata_structure(const camera_metadata_t *metadata,
304 const size_t *expected_size) {
305
306 if (metadata == NULL) {
Zhijun He246a1ad2013-11-26 13:41:18 -0800307 ALOGE("%s: metadata is null!", __FUNCTION__);
Igor Murashkind1cecc12013-04-30 17:21:54 -0700308 return ERROR;
309 }
310
311 // Check that the metadata pointer is well-aligned first.
312 {
Narayan Kamath21532652014-05-30 15:25:18 +0100313 static const struct {
Igor Murashkind1cecc12013-04-30 17:21:54 -0700314 const char *name;
315 size_t alignment;
316 } alignments[] = {
317 {
318 .name = "camera_metadata",
Narayan Kamath21532652014-05-30 15:25:18 +0100319 .alignment = METADATA_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700320 },
321 {
322 .name = "camera_metadata_buffer_entry",
Narayan Kamath21532652014-05-30 15:25:18 +0100323 .alignment = ENTRY_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700324 },
325 {
326 .name = "camera_metadata_data",
Narayan Kamath21532652014-05-30 15:25:18 +0100327 .alignment = DATA_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700328 },
329 };
330
331 for (size_t i = 0; i < sizeof(alignments)/sizeof(alignments[0]); ++i) {
332 uintptr_t aligned_ptr = ALIGN_TO(metadata, alignments[i].alignment);
333
334 if ((uintptr_t)metadata != aligned_ptr) {
335 ALOGE("%s: Metadata pointer is not aligned (actual %p, "
336 "expected %p) to type %s",
337 __FUNCTION__, metadata,
338 (void*)aligned_ptr, alignments[i].name);
339 return ERROR;
340 }
341 }
342 }
343
344 /**
345 * Check that the metadata contents are correct
346 */
347
348 if (expected_size != NULL && metadata->size > *expected_size) {
Narayan Kamath21532652014-05-30 15:25:18 +0100349 ALOGE("%s: Metadata size (%" PRIu32 ") should be <= expected size (%zu)",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700350 __FUNCTION__, metadata->size, *expected_size);
351 return ERROR;
352 }
353
354 if (metadata->entry_count > metadata->entry_capacity) {
Narayan Kamath21532652014-05-30 15:25:18 +0100355 ALOGE("%s: Entry count (%" PRIu32 ") should be <= entry capacity "
356 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700357 __FUNCTION__, metadata->entry_count, metadata->entry_capacity);
358 return ERROR;
359 }
360
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -0700361 if (metadata->data_count > metadata->data_capacity) {
362 ALOGE("%s: Data count (%" PRIu32 ") should be <= data capacity "
363 "(%" PRIu32 ")",
364 __FUNCTION__, metadata->data_count, metadata->data_capacity);
365 android_errorWriteLog(SN_EVENT_LOG_ID, "30591838");
366 return ERROR;
367 }
Narayan Kamath21532652014-05-30 15:25:18 +0100368 const metadata_uptrdiff_t entries_end =
369 metadata->entries_start + metadata->entry_capacity;
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -0700370
371
Igor Murashkind1cecc12013-04-30 17:21:54 -0700372 if (entries_end < metadata->entries_start || // overflow check
373 entries_end > metadata->data_start) {
374
Narayan Kamath21532652014-05-30 15:25:18 +0100375 ALOGE("%s: Entry start + capacity (%" PRIu32 ") should be <= data start "
376 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700377 __FUNCTION__,
378 (metadata->entries_start + metadata->entry_capacity),
379 metadata->data_start);
380 return ERROR;
381 }
382
Narayan Kamath21532652014-05-30 15:25:18 +0100383 const metadata_uptrdiff_t data_end =
384 metadata->data_start + metadata->data_capacity;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700385 if (data_end < metadata->data_start || // overflow check
386 data_end > metadata->size) {
387
Narayan Kamath21532652014-05-30 15:25:18 +0100388 ALOGE("%s: Data start + capacity (%" PRIu32 ") should be <= total size "
389 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700390 __FUNCTION__,
391 (metadata->data_start + metadata->data_capacity),
392 metadata->size);
393 return ERROR;
394 }
395
396 // Validate each entry
Narayan Kamath21532652014-05-30 15:25:18 +0100397 const metadata_size_t entry_count = metadata->entry_count;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700398 camera_metadata_buffer_entry_t *entries = get_entries(metadata);
399
400 for (size_t i = 0; i < entry_count; ++i) {
401
402 if ((uintptr_t)&entries[i] != ALIGN_TO(&entries[i], ENTRY_ALIGNMENT)) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700403 ALOGE("%s: Entry index %zu had bad alignment (address %p),"
404 " expected alignment %zu",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700405 __FUNCTION__, i, &entries[i], ENTRY_ALIGNMENT);
406 return ERROR;
407 }
408
409 camera_metadata_buffer_entry_t entry = entries[i];
410
411 if (entry.type >= NUM_TYPES) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700412 ALOGE("%s: Entry index %zu had a bad type %d",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700413 __FUNCTION__, i, entry.type);
414 return ERROR;
415 }
416
417 // TODO: fix vendor_tag_ops across processes so we don't need to special
418 // case vendor-specific tags
419 uint32_t tag_section = entry.tag >> 16;
420 int tag_type = get_camera_metadata_tag_type(entry.tag);
421 if (tag_type != (int)entry.type && tag_section < VENDOR_SECTION) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700422 ALOGE("%s: Entry index %zu had tag type %d, but the type was %d",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700423 __FUNCTION__, i, tag_type, entry.type);
424 return ERROR;
425 }
426
427 size_t data_size =
428 calculate_camera_metadata_entry_data_size(entry.type,
429 entry.count);
430
431 if (data_size != 0) {
432 camera_metadata_data_t *data =
433 (camera_metadata_data_t*) (get_data(metadata) +
434 entry.data.offset);
435
436 if ((uintptr_t)data != ALIGN_TO(data, DATA_ALIGNMENT)) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700437 ALOGE("%s: Entry index %zu had bad data alignment (address %p),"
438 " expected align %zu, (tag name %s, data size %zu)",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700439 __FUNCTION__, i, data, DATA_ALIGNMENT,
440 get_camera_metadata_tag_name(entry.tag) ?: "unknown",
441 data_size);
442 return ERROR;
443 }
444
445 size_t data_entry_end = entry.data.offset + data_size;
446 if (data_entry_end < entry.data.offset || // overflow check
447 data_entry_end > metadata->data_capacity) {
448
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700449 ALOGE("%s: Entry index %zu data ends (%zu) beyond the capacity "
Narayan Kamath21532652014-05-30 15:25:18 +0100450 "%" PRIu32, __FUNCTION__, i, data_entry_end,
Igor Murashkind1cecc12013-04-30 17:21:54 -0700451 metadata->data_capacity);
452 return ERROR;
453 }
454
455 } else if (entry.count == 0) {
456 if (entry.data.offset != 0) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700457 ALOGE("%s: Entry index %zu had 0 items, but offset was non-0 "
Narayan Kamath21532652014-05-30 15:25:18 +0100458 "(%" PRIu32 "), tag name: %s", __FUNCTION__, i, entry.data.offset,
Zhijun He3064d312013-07-15 13:55:11 -0700459 get_camera_metadata_tag_name(entry.tag) ?: "unknown");
Igor Murashkind1cecc12013-04-30 17:21:54 -0700460 return ERROR;
461 }
462 } // else data stored inline, so we look at value which can be anything.
463 }
464
465 return OK;
466}
467
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800468int append_camera_metadata(camera_metadata_t *dst,
469 const camera_metadata_t *src) {
470 if (dst == NULL || src == NULL ) return ERROR;
471
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -0700472 // Check for overflow
473 if (src->entry_count + dst->entry_count < src->entry_count) return ERROR;
474 if (src->data_count + dst->data_count < src->data_count) return ERROR;
475 // Check for space
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800476 if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
477 if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
478
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800479 memcpy(get_entries(dst) + dst->entry_count, get_entries(src),
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700480 sizeof(camera_metadata_buffer_entry_t[src->entry_count]));
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800481 memcpy(get_data(dst) + dst->data_count, get_data(src),
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800482 sizeof(uint8_t[src->data_count]));
483 if (dst->data_count != 0) {
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800484 camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
485 for (size_t i = 0; i < src->entry_count; i++, entry++) {
486 if ( calculate_camera_metadata_entry_data_size(entry->type,
487 entry->count) > 0 ) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800488 entry->data.offset += dst->data_count;
489 }
490 }
491 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700492 if (dst->entry_count == 0) {
493 // Appending onto empty buffer, keep sorted state
494 dst->flags |= src->flags & FLAG_SORTED;
495 } else if (src->entry_count != 0) {
496 // Both src, dst are nonempty, cannot assume sort remains
497 dst->flags &= ~FLAG_SORTED;
498 } else {
499 // Src is empty, keep dst sorted state
500 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800501 dst->entry_count += src->entry_count;
502 dst->data_count += src->data_count;
503
Alex Ray151389f2013-11-12 18:09:31 -0800504 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800505 return OK;
506}
507
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700508camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src) {
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700509 int res;
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700510 if (src == NULL) return NULL;
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700511 camera_metadata_t *clone = allocate_camera_metadata(
512 get_camera_metadata_entry_count(src),
513 get_camera_metadata_data_count(src));
514 if (clone != NULL) {
515 res = append_camera_metadata(clone, src);
516 if (res != OK) {
517 free_camera_metadata(clone);
518 clone = NULL;
519 }
520 }
Alex Ray151389f2013-11-12 18:09:31 -0800521 assert(validate_camera_metadata_structure(clone, NULL) == OK);
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700522 return clone;
523}
524
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800525size_t calculate_camera_metadata_entry_data_size(uint8_t type,
526 size_t data_count) {
527 if (type >= NUM_TYPES) return 0;
528 size_t data_bytes = data_count *
529 camera_metadata_type_size[type];
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800530 return data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800531}
532
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700533static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800534 uint32_t tag,
535 uint8_t type,
536 const void *data,
537 size_t data_count) {
538
539 if (dst == NULL) return ERROR;
540 if (dst->entry_count == dst->entry_capacity) return ERROR;
541 if (data == NULL) return ERROR;
542
543 size_t data_bytes =
544 calculate_camera_metadata_entry_data_size(type, data_count);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800545 if (data_bytes + dst->data_count > dst->data_capacity) return ERROR;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800546
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800547 size_t data_payload_bytes =
548 data_count * camera_metadata_type_size[type];
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800549 camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
Zhijun He3064d312013-07-15 13:55:11 -0700550 memset(entry, 0, sizeof(camera_metadata_buffer_entry_t));
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800551 entry->tag = tag;
552 entry->type = type;
553 entry->count = data_count;
554
555 if (data_bytes == 0) {
556 memcpy(entry->data.value, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800557 data_payload_bytes);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800558 } else {
559 entry->data.offset = dst->data_count;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800560 memcpy(get_data(dst) + entry->data.offset, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800561 data_payload_bytes);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800562 dst->data_count += data_bytes;
563 }
564 dst->entry_count++;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700565 dst->flags &= ~FLAG_SORTED;
Alex Ray151389f2013-11-12 18:09:31 -0800566 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800567 return OK;
568}
569
570int add_camera_metadata_entry(camera_metadata_t *dst,
571 uint32_t tag,
572 const void *data,
573 size_t data_count) {
574
575 int type = get_camera_metadata_tag_type(tag);
576 if (type == -1) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700577 ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800578 return ERROR;
579 }
580
581 return add_camera_metadata_entry_raw(dst,
582 tag,
583 type,
584 data,
585 data_count);
586}
587
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700588static int compare_entry_tags(const void *p1, const void *p2) {
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700589 uint32_t tag1 = ((camera_metadata_buffer_entry_t*)p1)->tag;
590 uint32_t tag2 = ((camera_metadata_buffer_entry_t*)p2)->tag;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700591 return tag1 < tag2 ? -1 :
592 tag1 == tag2 ? 0 :
593 1;
594}
595
596int sort_camera_metadata(camera_metadata_t *dst) {
597 if (dst == NULL) return ERROR;
598 if (dst->flags & FLAG_SORTED) return OK;
599
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800600 qsort(get_entries(dst), dst->entry_count,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700601 sizeof(camera_metadata_buffer_entry_t),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700602 compare_entry_tags);
603 dst->flags |= FLAG_SORTED;
604
Alex Ray151389f2013-11-12 18:09:31 -0800605 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700606 return OK;
607}
608
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800609int get_camera_metadata_entry(camera_metadata_t *src,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700610 size_t index,
611 camera_metadata_entry_t *entry) {
612 if (src == NULL || entry == NULL) return ERROR;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800613 if (index >= src->entry_count) return ERROR;
614
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800615 camera_metadata_buffer_entry_t *buffer_entry = get_entries(src) + index;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800616
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700617 entry->index = index;
618 entry->tag = buffer_entry->tag;
619 entry->type = buffer_entry->type;
620 entry->count = buffer_entry->count;
621 if (buffer_entry->count *
622 camera_metadata_type_size[buffer_entry->type] > 4) {
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800623 entry->data.u8 = get_data(src) + buffer_entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700624 } else {
625 entry->data.u8 = buffer_entry->data.value;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700626 }
627 return OK;
628}
629
Alex Ray71fe0f82013-10-21 12:57:05 -0700630int get_camera_metadata_ro_entry(const camera_metadata_t *src,
631 size_t index,
632 camera_metadata_ro_entry_t *entry) {
633 return get_camera_metadata_entry((camera_metadata_t*)src, index,
634 (camera_metadata_entry_t*)entry);
635}
636
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700637int find_camera_metadata_entry(camera_metadata_t *src,
638 uint32_t tag,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700639 camera_metadata_entry_t *entry) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700640 if (src == NULL) return ERROR;
641
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700642 uint32_t index;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700643 if (src->flags & FLAG_SORTED) {
644 // Sorted entries, do a binary search
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700645 camera_metadata_buffer_entry_t *search_entry = NULL;
646 camera_metadata_buffer_entry_t key;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700647 key.tag = tag;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700648 search_entry = bsearch(&key,
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800649 get_entries(src),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700650 src->entry_count,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700651 sizeof(camera_metadata_buffer_entry_t),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700652 compare_entry_tags);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700653 if (search_entry == NULL) return NOT_FOUND;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800654 index = search_entry - get_entries(src);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800655 } else {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700656 // Not sorted, linear search
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800657 camera_metadata_buffer_entry_t *search_entry = get_entries(src);
658 for (index = 0; index < src->entry_count; index++, search_entry++) {
659 if (search_entry->tag == tag) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700660 break;
661 }
662 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700663 if (index == src->entry_count) return NOT_FOUND;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700664 }
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700665
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700666 return get_camera_metadata_entry(src, index,
667 entry);
668}
669
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700670int find_camera_metadata_ro_entry(const camera_metadata_t *src,
671 uint32_t tag,
672 camera_metadata_ro_entry_t *entry) {
673 return find_camera_metadata_entry((camera_metadata_t*)src, tag,
674 (camera_metadata_entry_t*)entry);
675}
676
677
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700678int delete_camera_metadata_entry(camera_metadata_t *dst,
679 size_t index) {
680 if (dst == NULL) return ERROR;
681 if (index >= dst->entry_count) return ERROR;
682
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800683 camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700684 size_t data_bytes = calculate_camera_metadata_entry_data_size(entry->type,
685 entry->count);
686
687 if (data_bytes > 0) {
688 // Shift data buffer to overwrite deleted data
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800689 uint8_t *start = get_data(dst) + entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700690 uint8_t *end = start + data_bytes;
691 size_t length = dst->data_count - entry->data.offset - data_bytes;
692 memmove(start, end, length);
693
694 // Update all entry indices to account for shift
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800695 camera_metadata_buffer_entry_t *e = get_entries(dst);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700696 size_t i;
697 for (i = 0; i < dst->entry_count; i++) {
698 if (calculate_camera_metadata_entry_data_size(
699 e->type, e->count) > 0 &&
700 e->data.offset > entry->data.offset) {
701 e->data.offset -= data_bytes;
702 }
703 ++e;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700704 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700705 dst->data_count -= data_bytes;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800706 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700707 // Shift entry array
708 memmove(entry, entry + 1,
709 sizeof(camera_metadata_buffer_entry_t) *
710 (dst->entry_count - index - 1) );
711 dst->entry_count -= 1;
712
Alex Ray151389f2013-11-12 18:09:31 -0800713 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700714 return OK;
715}
716
717int update_camera_metadata_entry(camera_metadata_t *dst,
718 size_t index,
719 const void *data,
720 size_t data_count,
721 camera_metadata_entry_t *updated_entry) {
722 if (dst == NULL) return ERROR;
723 if (index >= dst->entry_count) return ERROR;
724
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800725 camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700726
727 size_t data_bytes =
728 calculate_camera_metadata_entry_data_size(entry->type,
729 data_count);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800730 size_t data_payload_bytes =
731 data_count * camera_metadata_type_size[entry->type];
732
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700733 size_t entry_bytes =
734 calculate_camera_metadata_entry_data_size(entry->type,
735 entry->count);
736 if (data_bytes != entry_bytes) {
737 // May need to shift/add to data array
738 if (dst->data_capacity < dst->data_count + data_bytes - entry_bytes) {
739 // No room
740 return ERROR;
741 }
742 if (entry_bytes != 0) {
743 // Remove old data
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800744 uint8_t *start = get_data(dst) + entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700745 uint8_t *end = start + entry_bytes;
746 size_t length = dst->data_count - entry->data.offset - entry_bytes;
747 memmove(start, end, length);
748 dst->data_count -= entry_bytes;
749
750 // Update all entry indices to account for shift
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800751 camera_metadata_buffer_entry_t *e = get_entries(dst);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700752 size_t i;
753 for (i = 0; i < dst->entry_count; i++) {
754 if (calculate_camera_metadata_entry_data_size(
755 e->type, e->count) > 0 &&
756 e->data.offset > entry->data.offset) {
757 e->data.offset -= entry_bytes;
758 }
759 ++e;
760 }
761 }
762
763 if (data_bytes != 0) {
764 // Append new data
765 entry->data.offset = dst->data_count;
766
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800767 memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700768 dst->data_count += data_bytes;
769 }
770 } else if (data_bytes != 0) {
771 // data size unchanged, reuse same data location
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800772 memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700773 }
774
775 if (data_bytes == 0) {
776 // Data fits into entry
777 memcpy(entry->data.value, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800778 data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700779 }
780
781 entry->count = data_count;
782
783 if (updated_entry != NULL) {
784 get_camera_metadata_entry(dst,
785 index,
786 updated_entry);
787 }
788
Alex Ray151389f2013-11-12 18:09:31 -0800789 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800790 return OK;
791}
792
Ruben Brunk9c13e192014-02-14 16:40:56 -0800793static const vendor_tag_ops_t *vendor_tag_ops = NULL;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800794
795const char *get_camera_metadata_section_name(uint32_t tag) {
796 uint32_t tag_section = tag >> 16;
797 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800798 return vendor_tag_ops->get_section_name(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700799 vendor_tag_ops,
800 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800801 }
802 if (tag_section >= ANDROID_SECTION_COUNT) {
803 return NULL;
804 }
805 return camera_metadata_section_names[tag_section];
806}
807
808const char *get_camera_metadata_tag_name(uint32_t tag) {
809 uint32_t tag_section = tag >> 16;
810 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800811 return vendor_tag_ops->get_tag_name(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700812 vendor_tag_ops,
813 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800814 }
815 if (tag_section >= ANDROID_SECTION_COUNT ||
816 tag >= camera_metadata_section_bounds[tag_section][1] ) {
817 return NULL;
818 }
819 uint32_t tag_index = tag & 0xFFFF;
820 return tag_info[tag_section][tag_index].tag_name;
821}
822
823int get_camera_metadata_tag_type(uint32_t tag) {
824 uint32_t tag_section = tag >> 16;
825 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800826 return vendor_tag_ops->get_tag_type(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700827 vendor_tag_ops,
828 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800829 }
830 if (tag_section >= ANDROID_SECTION_COUNT ||
831 tag >= camera_metadata_section_bounds[tag_section][1] ) {
832 return -1;
833 }
834 uint32_t tag_index = tag & 0xFFFF;
835 return tag_info[tag_section][tag_index].tag_type;
836}
837
Ruben Brunk9c13e192014-02-14 16:40:56 -0800838int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t* ops) {
839 // **DEPRECATED**
840 ALOGE("%s: This function has been deprecated", __FUNCTION__);
841 return ERROR;
842}
843
844// Declared in system/media/private/camera/include/camera_metadata_hidden.h
845int set_camera_metadata_vendor_ops(const vendor_tag_ops_t* ops) {
846 vendor_tag_ops = ops;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800847 return OK;
848}
849
Igor Murashkin375cfd32012-12-03 13:55:33 -0800850static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag, int type,
851 int count,
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700852 int indentation);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800853
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700854void dump_camera_metadata(const camera_metadata_t *metadata,
855 int fd,
856 int verbosity) {
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700857 dump_indented_camera_metadata(metadata, fd, verbosity, 0);
858}
859
860void dump_indented_camera_metadata(const camera_metadata_t *metadata,
861 int fd,
862 int verbosity,
863 int indentation) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800864 if (metadata == NULL) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700865 dprintf(fd, "%*sDumping camera metadata array: Not allocated\n",
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700866 indentation, "");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800867 return;
868 }
869 unsigned int i;
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700870 dprintf(fd,
Narayan Kamath21532652014-05-30 15:25:18 +0100871 "%*sDumping camera metadata array: %" PRIu32 " / %" PRIu32 " entries, "
872 "%" PRIu32 " / %" PRIu32 " bytes of extra data.\n", indentation, "",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700873 metadata->entry_count, metadata->entry_capacity,
874 metadata->data_count, metadata->data_capacity);
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700875 dprintf(fd, "%*sVersion: %d, Flags: %08x\n",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700876 indentation + 2, "",
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700877 metadata->version, metadata->flags);
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800878 camera_metadata_buffer_entry_t *entry = get_entries(metadata);
879 for (i=0; i < metadata->entry_count; i++, entry++) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800880
881 const char *tag_name, *tag_section;
882 tag_section = get_camera_metadata_section_name(entry->tag);
883 if (tag_section == NULL) {
884 tag_section = "unknownSection";
885 }
886 tag_name = get_camera_metadata_tag_name(entry->tag);
887 if (tag_name == NULL) {
888 tag_name = "unknownTag";
889 }
890 const char *type_name;
891 if (entry->type >= NUM_TYPES) {
892 type_name = "unknown";
893 } else {
894 type_name = camera_metadata_type_names[entry->type];
895 }
Narayan Kamath21532652014-05-30 15:25:18 +0100896 dprintf(fd, "%*s%s.%s (%05x): %s[%" PRIu32 "]\n",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700897 indentation + 2, "",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800898 tag_section,
899 tag_name,
900 entry->tag,
901 type_name,
902 entry->count);
903
904 if (verbosity < 1) continue;
905
906 if (entry->type >= NUM_TYPES) continue;
907
908 size_t type_size = camera_metadata_type_size[entry->type];
909 uint8_t *data_ptr;
910 if ( type_size * entry->count > 4 ) {
911 if (entry->data.offset >= metadata->data_count) {
Narayan Kamath21532652014-05-30 15:25:18 +0100912 ALOGE("%s: Malformed entry data offset: %" PRIu32 " (max %" PRIu32 ")",
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700913 __FUNCTION__,
914 entry->data.offset,
915 metadata->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800916 continue;
917 }
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800918 data_ptr = get_data(metadata) + entry->data.offset;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800919 } else {
920 data_ptr = entry->data.value;
921 }
922 int count = entry->count;
923 if (verbosity < 2 && count > 16) count = 16;
924
Igor Murashkin375cfd32012-12-03 13:55:33 -0800925 print_data(fd, data_ptr, entry->tag, entry->type, count, indentation);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800926 }
927}
928
Igor Murashkin375cfd32012-12-03 13:55:33 -0800929static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag,
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700930 int type, int count, int indentation) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800931 static int values_per_line[NUM_TYPES] = {
932 [TYPE_BYTE] = 16,
933 [TYPE_INT32] = 4,
934 [TYPE_FLOAT] = 8,
935 [TYPE_INT64] = 2,
936 [TYPE_DOUBLE] = 4,
937 [TYPE_RATIONAL] = 2,
938 };
939 size_t type_size = camera_metadata_type_size[type];
Igor Murashkin375cfd32012-12-03 13:55:33 -0800940 char value_string_tmp[CAMERA_METADATA_ENUM_STRING_MAX_SIZE];
941 uint32_t value;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800942
943 int lines = count / values_per_line[type];
944 if (count % values_per_line[type] != 0) lines++;
945
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800946 int index = 0;
947 int j, k;
948 for (j = 0; j < lines; j++) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700949 dprintf(fd, "%*s[", indentation + 4, "");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800950 for (k = 0;
951 k < values_per_line[type] && count > 0;
952 k++, count--, index += type_size) {
953
954 switch (type) {
955 case TYPE_BYTE:
Igor Murashkin375cfd32012-12-03 13:55:33 -0800956 value = *(data_ptr + index);
957 if (camera_metadata_enum_snprint(tag,
958 value,
959 value_string_tmp,
960 sizeof(value_string_tmp))
961 == OK) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700962 dprintf(fd, "%s ", value_string_tmp);
Igor Murashkin375cfd32012-12-03 13:55:33 -0800963 } else {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700964 dprintf(fd, "%hhu ",
Igor Murashkin375cfd32012-12-03 13:55:33 -0800965 *(data_ptr + index));
966 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800967 break;
968 case TYPE_INT32:
Igor Murashkin375cfd32012-12-03 13:55:33 -0800969 value =
970 *(int32_t*)(data_ptr + index);
971 if (camera_metadata_enum_snprint(tag,
972 value,
973 value_string_tmp,
974 sizeof(value_string_tmp))
975 == OK) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700976 dprintf(fd, "%s ", value_string_tmp);
Igor Murashkin375cfd32012-12-03 13:55:33 -0800977 } else {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700978 dprintf(fd, "%" PRId32 " ",
Igor Murashkin375cfd32012-12-03 13:55:33 -0800979 *(int32_t*)(data_ptr + index));
980 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800981 break;
982 case TYPE_FLOAT:
Elliott Hughesd1c33572014-05-22 10:53:20 -0700983 dprintf(fd, "%0.8f ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800984 *(float*)(data_ptr + index));
985 break;
986 case TYPE_INT64:
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700987 dprintf(fd, "%" PRId64 " ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800988 *(int64_t*)(data_ptr + index));
989 break;
990 case TYPE_DOUBLE:
Elliott Hughesd1c33572014-05-22 10:53:20 -0700991 dprintf(fd, "%0.8f ",
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800992 *(double*)(data_ptr + index));
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800993 break;
994 case TYPE_RATIONAL: {
995 int32_t numerator = *(int32_t*)(data_ptr + index);
996 int32_t denominator = *(int32_t*)(data_ptr + index + 4);
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700997 dprintf(fd, "(%d / %d) ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800998 numerator, denominator);
999 break;
1000 }
1001 default:
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001002 dprintf(fd, "??? ");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001003 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001004 }
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001005 dprintf(fd, "]\n");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001006 }
1007}