blob: eb914eba2afe5717b6f7fb21ad99dab1ab3233c1 [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
Mark Salyzyn773e5c82014-03-13 13:24:19 -070017#include <inttypes.h>
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080018#include <system/camera_metadata.h>
Ruben Brunk9c13e192014-02-14 16:40:56 -080019#include <camera_metadata_hidden.h>
Igor Murashkind1cecc12013-04-30 17:21:54 -070020
21#define LOG_TAG "camera_metadata"
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080022#include <cutils/log.h>
Alex Ray151389f2013-11-12 18:09:31 -080023#include <assert.h>
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070024#include <stdio.h>
25#include <stdlib.h>
26#include <errno.h>
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080027
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070028#define OK 0
29#define ERROR 1
30#define NOT_FOUND -ENOENT
Eino-Ville Talvala31540362012-11-07 16:36:50 -080031
Eino-Ville Talvala31540362012-11-07 16:36:50 -080032#define ALIGN_TO(val, alignment) \
Igor Murashkind1cecc12013-04-30 17:21:54 -070033 (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
34
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080035/**
36 * A single metadata entry, storing an array of values of a given type. If the
37 * array is no larger than 4 bytes in size, it is stored in the data.value[]
38 * array; otherwise, it can found in the parent's data array at index
39 * data.offset.
40 */
Narayan Kamath21532652014-05-30 15:25:18 +010041#define ENTRY_ALIGNMENT ((size_t) 4)
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -070042typedef struct camera_metadata_buffer_entry {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080043 uint32_t tag;
Narayan Kamath21532652014-05-30 15:25:18 +010044 uint32_t count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080045 union {
Narayan Kamath21532652014-05-30 15:25:18 +010046 uint32_t offset;
47 uint8_t value[4];
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080048 } data;
49 uint8_t type;
50 uint8_t reserved[3];
Eino-Ville Talvala31540362012-11-07 16:36:50 -080051} camera_metadata_buffer_entry_t;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080052
Narayan Kamath21532652014-05-30 15:25:18 +010053typedef uint32_t metadata_uptrdiff_t;
54typedef uint32_t metadata_size_t;
55
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080056/**
57 * A packet of metadata. This is a list of entries, each of which may point to
58 * its values stored at an offset in data.
59 *
60 * It is assumed by the utility functions that the memory layout of the packet
61 * is as follows:
62 *
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -070063 * |-----------------------------------------------|
64 * | camera_metadata_t |
65 * | |
66 * |-----------------------------------------------|
67 * | reserved for future expansion |
68 * |-----------------------------------------------|
69 * | camera_metadata_buffer_entry_t #0 |
70 * |-----------------------------------------------|
71 * | .... |
72 * |-----------------------------------------------|
73 * | camera_metadata_buffer_entry_t #entry_count-1 |
74 * |-----------------------------------------------|
75 * | free space for |
76 * | (entry_capacity-entry_count) entries |
77 * |-----------------------------------------------|
78 * | start of camera_metadata.data |
79 * | |
80 * |-----------------------------------------------|
81 * | free space for |
82 * | (data_capacity-data_count) bytes |
83 * |-----------------------------------------------|
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080084 *
85 * With the total length of the whole packet being camera_metadata.size bytes.
86 *
87 * In short, the entries and data are contiguous in memory after the metadata
88 * header.
89 */
Narayan Kamath21532652014-05-30 15:25:18 +010090#define METADATA_ALIGNMENT ((size_t) 4)
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080091struct camera_metadata {
Narayan Kamath21532652014-05-30 15:25:18 +010092 metadata_size_t size;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070093 uint32_t version;
94 uint32_t flags;
Narayan Kamath21532652014-05-30 15:25:18 +010095 metadata_size_t entry_count;
96 metadata_size_t entry_capacity;
97 metadata_uptrdiff_t entries_start; // Offset from camera_metadata
98 metadata_size_t data_count;
99 metadata_size_t data_capacity;
100 metadata_uptrdiff_t data_start; // Offset from camera_metadata
101 uint8_t reserved[];
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800102};
103
Igor Murashkin555aac82012-11-15 10:54:57 -0800104/**
105 * A datum of metadata. This corresponds to camera_metadata_entry_t::data
106 * with the difference that each element is not a pointer. We need to have a
107 * non-pointer type description in order to figure out the largest alignment
108 * requirement for data (DATA_ALIGNMENT).
109 */
Narayan Kamath21532652014-05-30 15:25:18 +0100110#define DATA_ALIGNMENT ((size_t) 8)
Igor Murashkin555aac82012-11-15 10:54:57 -0800111typedef union camera_metadata_data {
112 uint8_t u8;
113 int32_t i32;
114 float f;
115 int64_t i64;
116 double d;
117 camera_metadata_rational_t r;
118} camera_metadata_data_t;
119
Narayan Kamath21532652014-05-30 15:25:18 +0100120/**
121 * The preferred alignment of a packet of camera metadata. In general,
122 * this is the lowest common multiple of the constituents of a metadata
123 * package, i.e, of DATA_ALIGNMENT and ENTRY_ALIGNMENT.
124 */
125#define MAX_ALIGNMENT(A, B) (((A) > (B)) ? (A) : (B))
126#define METADATA_PACKET_ALIGNMENT \
127 MAX_ALIGNMENT(MAX_ALIGNMENT(DATA_ALIGNMENT, METADATA_ALIGNMENT), ENTRY_ALIGNMENT);
128
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700129/** Versioning information */
130#define CURRENT_METADATA_VERSION 1
131
132/** Flag definitions */
133#define FLAG_SORTED 0x00000001
134
135/** Tag information */
136
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800137typedef struct tag_info {
138 const char *tag_name;
139 uint8_t tag_type;
140} tag_info_t;
141
142#include "camera_metadata_tag_info.c"
143
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700144const size_t camera_metadata_type_size[NUM_TYPES] = {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800145 [TYPE_BYTE] = sizeof(uint8_t),
146 [TYPE_INT32] = sizeof(int32_t),
147 [TYPE_FLOAT] = sizeof(float),
148 [TYPE_INT64] = sizeof(int64_t),
149 [TYPE_DOUBLE] = sizeof(double),
150 [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
151};
152
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700153const char *camera_metadata_type_names[NUM_TYPES] = {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800154 [TYPE_BYTE] = "byte",
155 [TYPE_INT32] = "int32",
156 [TYPE_FLOAT] = "float",
157 [TYPE_INT64] = "int64",
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700158 [TYPE_DOUBLE] = "double",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800159 [TYPE_RATIONAL] = "rational"
160};
161
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800162static camera_metadata_buffer_entry_t *get_entries(
163 const camera_metadata_t *metadata) {
164 return (camera_metadata_buffer_entry_t*)
165 ((uint8_t*)metadata + metadata->entries_start);
166}
167
168static uint8_t *get_data(const camera_metadata_t *metadata) {
169 return (uint8_t*)metadata + metadata->data_start;
170}
171
Zhijun He1450f7f2013-12-05 07:44:56 -0800172size_t get_camera_metadata_alignment() {
Narayan Kamath21532652014-05-30 15:25:18 +0100173 return METADATA_PACKET_ALIGNMENT;
Zhijun He1450f7f2013-12-05 07:44:56 -0800174}
175
Igor Murashkind1cecc12013-04-30 17:21:54 -0700176camera_metadata_t *allocate_copy_camera_metadata_checked(
177 const camera_metadata_t *src,
178 size_t src_size) {
179
180 if (src == NULL) {
181 return NULL;
182 }
183
184 void *buffer = malloc(src_size);
185 memcpy(buffer, src, src_size);
186
187 camera_metadata_t *metadata = (camera_metadata_t*) buffer;
188 if (validate_camera_metadata_structure(metadata, &src_size) != OK) {
189 free(buffer);
190 return NULL;
191 }
192
193 return metadata;
194}
195
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800196camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
197 size_t data_capacity) {
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700198
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800199 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
200 data_capacity);
201 void *buffer = malloc(memory_needed);
202 return place_camera_metadata(buffer, memory_needed,
203 entry_capacity,
204 data_capacity);
205}
206
207camera_metadata_t *place_camera_metadata(void *dst,
208 size_t dst_size,
209 size_t entry_capacity,
210 size_t data_capacity) {
211 if (dst == NULL) return NULL;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800212
213 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
214 data_capacity);
215 if (memory_needed > dst_size) return NULL;
216
217 camera_metadata_t *metadata = (camera_metadata_t*)dst;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700218 metadata->version = CURRENT_METADATA_VERSION;
219 metadata->flags = 0;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800220 metadata->entry_count = 0;
221 metadata->entry_capacity = entry_capacity;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800222 metadata->entries_start =
223 ALIGN_TO(sizeof(camera_metadata_t), ENTRY_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800224 metadata->data_count = 0;
225 metadata->data_capacity = data_capacity;
226 metadata->size = memory_needed;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700227 size_t data_unaligned = (uint8_t*)(get_entries(metadata) +
228 metadata->entry_capacity) - (uint8_t*)metadata;
229 metadata->data_start = ALIGN_TO(data_unaligned, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800230
Alex Ray151389f2013-11-12 18:09:31 -0800231 assert(validate_camera_metadata_structure(metadata, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800232 return metadata;
233}
234void free_camera_metadata(camera_metadata_t *metadata) {
235 free(metadata);
236}
237
238size_t calculate_camera_metadata_size(size_t entry_count,
239 size_t data_count) {
240 size_t memory_needed = sizeof(camera_metadata_t);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800241 // Start entry list at aligned boundary
242 memory_needed = ALIGN_TO(memory_needed, ENTRY_ALIGNMENT);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700243 memory_needed += sizeof(camera_metadata_buffer_entry_t[entry_count]);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800244 // Start buffer list at aligned boundary
245 memory_needed = ALIGN_TO(memory_needed, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800246 memory_needed += sizeof(uint8_t[data_count]);
247 return memory_needed;
248}
249
250size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
251 if (metadata == NULL) return ERROR;
252
253 return metadata->size;
254}
255
256size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
257 if (metadata == NULL) return ERROR;
258
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800259 return calculate_camera_metadata_size(metadata->entry_count,
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800260 metadata->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800261}
262
263size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
264 return metadata->entry_count;
265}
266
267size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
268 return metadata->entry_capacity;
269}
270
271size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
272 return metadata->data_count;
273}
274
275size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
276 return metadata->data_capacity;
277}
278
279camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
280 const camera_metadata_t *src) {
281 size_t memory_needed = get_camera_metadata_compact_size(src);
282
283 if (dst == NULL) return NULL;
284 if (dst_size < memory_needed) return NULL;
285
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800286 camera_metadata_t *metadata =
287 place_camera_metadata(dst, dst_size, src->entry_count, src->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800288
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700289 metadata->flags = src->flags;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800290 metadata->entry_count = src->entry_count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800291 metadata->data_count = src->data_count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800292
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800293 memcpy(get_entries(metadata), get_entries(src),
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700294 sizeof(camera_metadata_buffer_entry_t[metadata->entry_count]));
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800295 memcpy(get_data(metadata), get_data(src),
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800296 sizeof(uint8_t[metadata->data_count]));
297
Alex Ray151389f2013-11-12 18:09:31 -0800298 assert(validate_camera_metadata_structure(metadata, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800299 return metadata;
300}
301
Igor Murashkind1cecc12013-04-30 17:21:54 -0700302int validate_camera_metadata_structure(const camera_metadata_t *metadata,
303 const size_t *expected_size) {
304
305 if (metadata == NULL) {
Zhijun He246a1ad2013-11-26 13:41:18 -0800306 ALOGE("%s: metadata is null!", __FUNCTION__);
Igor Murashkind1cecc12013-04-30 17:21:54 -0700307 return ERROR;
308 }
309
310 // Check that the metadata pointer is well-aligned first.
311 {
Narayan Kamath21532652014-05-30 15:25:18 +0100312 static const struct {
Igor Murashkind1cecc12013-04-30 17:21:54 -0700313 const char *name;
314 size_t alignment;
315 } alignments[] = {
316 {
317 .name = "camera_metadata",
Narayan Kamath21532652014-05-30 15:25:18 +0100318 .alignment = METADATA_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700319 },
320 {
321 .name = "camera_metadata_buffer_entry",
Narayan Kamath21532652014-05-30 15:25:18 +0100322 .alignment = ENTRY_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700323 },
324 {
325 .name = "camera_metadata_data",
Narayan Kamath21532652014-05-30 15:25:18 +0100326 .alignment = DATA_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700327 },
328 };
329
330 for (size_t i = 0; i < sizeof(alignments)/sizeof(alignments[0]); ++i) {
331 uintptr_t aligned_ptr = ALIGN_TO(metadata, alignments[i].alignment);
332
333 if ((uintptr_t)metadata != aligned_ptr) {
334 ALOGE("%s: Metadata pointer is not aligned (actual %p, "
335 "expected %p) to type %s",
336 __FUNCTION__, metadata,
337 (void*)aligned_ptr, alignments[i].name);
338 return ERROR;
339 }
340 }
341 }
342
343 /**
344 * Check that the metadata contents are correct
345 */
346
347 if (expected_size != NULL && metadata->size > *expected_size) {
Narayan Kamath21532652014-05-30 15:25:18 +0100348 ALOGE("%s: Metadata size (%" PRIu32 ") should be <= expected size (%zu)",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700349 __FUNCTION__, metadata->size, *expected_size);
350 return ERROR;
351 }
352
353 if (metadata->entry_count > metadata->entry_capacity) {
Narayan Kamath21532652014-05-30 15:25:18 +0100354 ALOGE("%s: Entry count (%" PRIu32 ") should be <= entry capacity "
355 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700356 __FUNCTION__, metadata->entry_count, metadata->entry_capacity);
357 return ERROR;
358 }
359
Narayan Kamath21532652014-05-30 15:25:18 +0100360 const metadata_uptrdiff_t entries_end =
361 metadata->entries_start + metadata->entry_capacity;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700362 if (entries_end < metadata->entries_start || // overflow check
363 entries_end > metadata->data_start) {
364
Narayan Kamath21532652014-05-30 15:25:18 +0100365 ALOGE("%s: Entry start + capacity (%" PRIu32 ") should be <= data start "
366 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700367 __FUNCTION__,
368 (metadata->entries_start + metadata->entry_capacity),
369 metadata->data_start);
370 return ERROR;
371 }
372
Narayan Kamath21532652014-05-30 15:25:18 +0100373 const metadata_uptrdiff_t data_end =
374 metadata->data_start + metadata->data_capacity;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700375 if (data_end < metadata->data_start || // overflow check
376 data_end > metadata->size) {
377
Narayan Kamath21532652014-05-30 15:25:18 +0100378 ALOGE("%s: Data start + capacity (%" PRIu32 ") should be <= total size "
379 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700380 __FUNCTION__,
381 (metadata->data_start + metadata->data_capacity),
382 metadata->size);
383 return ERROR;
384 }
385
386 // Validate each entry
Narayan Kamath21532652014-05-30 15:25:18 +0100387 const metadata_size_t entry_count = metadata->entry_count;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700388 camera_metadata_buffer_entry_t *entries = get_entries(metadata);
389
390 for (size_t i = 0; i < entry_count; ++i) {
391
392 if ((uintptr_t)&entries[i] != ALIGN_TO(&entries[i], ENTRY_ALIGNMENT)) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700393 ALOGE("%s: Entry index %zu had bad alignment (address %p),"
394 " expected alignment %zu",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700395 __FUNCTION__, i, &entries[i], ENTRY_ALIGNMENT);
396 return ERROR;
397 }
398
399 camera_metadata_buffer_entry_t entry = entries[i];
400
401 if (entry.type >= NUM_TYPES) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700402 ALOGE("%s: Entry index %zu had a bad type %d",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700403 __FUNCTION__, i, entry.type);
404 return ERROR;
405 }
406
407 // TODO: fix vendor_tag_ops across processes so we don't need to special
408 // case vendor-specific tags
409 uint32_t tag_section = entry.tag >> 16;
410 int tag_type = get_camera_metadata_tag_type(entry.tag);
411 if (tag_type != (int)entry.type && tag_section < VENDOR_SECTION) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700412 ALOGE("%s: Entry index %zu had tag type %d, but the type was %d",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700413 __FUNCTION__, i, tag_type, entry.type);
414 return ERROR;
415 }
416
417 size_t data_size =
418 calculate_camera_metadata_entry_data_size(entry.type,
419 entry.count);
420
421 if (data_size != 0) {
422 camera_metadata_data_t *data =
423 (camera_metadata_data_t*) (get_data(metadata) +
424 entry.data.offset);
425
426 if ((uintptr_t)data != ALIGN_TO(data, DATA_ALIGNMENT)) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700427 ALOGE("%s: Entry index %zu had bad data alignment (address %p),"
428 " expected align %zu, (tag name %s, data size %zu)",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700429 __FUNCTION__, i, data, DATA_ALIGNMENT,
430 get_camera_metadata_tag_name(entry.tag) ?: "unknown",
431 data_size);
432 return ERROR;
433 }
434
435 size_t data_entry_end = entry.data.offset + data_size;
436 if (data_entry_end < entry.data.offset || // overflow check
437 data_entry_end > metadata->data_capacity) {
438
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700439 ALOGE("%s: Entry index %zu data ends (%zu) beyond the capacity "
Narayan Kamath21532652014-05-30 15:25:18 +0100440 "%" PRIu32, __FUNCTION__, i, data_entry_end,
Igor Murashkind1cecc12013-04-30 17:21:54 -0700441 metadata->data_capacity);
442 return ERROR;
443 }
444
445 } else if (entry.count == 0) {
446 if (entry.data.offset != 0) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700447 ALOGE("%s: Entry index %zu had 0 items, but offset was non-0 "
Narayan Kamath21532652014-05-30 15:25:18 +0100448 "(%" PRIu32 "), tag name: %s", __FUNCTION__, i, entry.data.offset,
Zhijun He3064d312013-07-15 13:55:11 -0700449 get_camera_metadata_tag_name(entry.tag) ?: "unknown");
Igor Murashkind1cecc12013-04-30 17:21:54 -0700450 return ERROR;
451 }
452 } // else data stored inline, so we look at value which can be anything.
453 }
454
455 return OK;
456}
457
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800458int append_camera_metadata(camera_metadata_t *dst,
459 const camera_metadata_t *src) {
460 if (dst == NULL || src == NULL ) return ERROR;
461
462 if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
463 if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
464
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800465 memcpy(get_entries(dst) + dst->entry_count, get_entries(src),
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700466 sizeof(camera_metadata_buffer_entry_t[src->entry_count]));
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800467 memcpy(get_data(dst) + dst->data_count, get_data(src),
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800468 sizeof(uint8_t[src->data_count]));
469 if (dst->data_count != 0) {
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800470 camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
471 for (size_t i = 0; i < src->entry_count; i++, entry++) {
472 if ( calculate_camera_metadata_entry_data_size(entry->type,
473 entry->count) > 0 ) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800474 entry->data.offset += dst->data_count;
475 }
476 }
477 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700478 if (dst->entry_count == 0) {
479 // Appending onto empty buffer, keep sorted state
480 dst->flags |= src->flags & FLAG_SORTED;
481 } else if (src->entry_count != 0) {
482 // Both src, dst are nonempty, cannot assume sort remains
483 dst->flags &= ~FLAG_SORTED;
484 } else {
485 // Src is empty, keep dst sorted state
486 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800487 dst->entry_count += src->entry_count;
488 dst->data_count += src->data_count;
489
Alex Ray151389f2013-11-12 18:09:31 -0800490 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800491 return OK;
492}
493
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700494camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src) {
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700495 int res;
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700496 if (src == NULL) return NULL;
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700497 camera_metadata_t *clone = allocate_camera_metadata(
498 get_camera_metadata_entry_count(src),
499 get_camera_metadata_data_count(src));
500 if (clone != NULL) {
501 res = append_camera_metadata(clone, src);
502 if (res != OK) {
503 free_camera_metadata(clone);
504 clone = NULL;
505 }
506 }
Alex Ray151389f2013-11-12 18:09:31 -0800507 assert(validate_camera_metadata_structure(clone, NULL) == OK);
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700508 return clone;
509}
510
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800511size_t calculate_camera_metadata_entry_data_size(uint8_t type,
512 size_t data_count) {
513 if (type >= NUM_TYPES) return 0;
514 size_t data_bytes = data_count *
515 camera_metadata_type_size[type];
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800516 return data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800517}
518
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700519static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800520 uint32_t tag,
521 uint8_t type,
522 const void *data,
523 size_t data_count) {
524
525 if (dst == NULL) return ERROR;
526 if (dst->entry_count == dst->entry_capacity) return ERROR;
527 if (data == NULL) return ERROR;
528
529 size_t data_bytes =
530 calculate_camera_metadata_entry_data_size(type, data_count);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800531 if (data_bytes + dst->data_count > dst->data_capacity) return ERROR;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800532
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800533 size_t data_payload_bytes =
534 data_count * camera_metadata_type_size[type];
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800535 camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
Zhijun He3064d312013-07-15 13:55:11 -0700536 memset(entry, 0, sizeof(camera_metadata_buffer_entry_t));
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800537 entry->tag = tag;
538 entry->type = type;
539 entry->count = data_count;
540
541 if (data_bytes == 0) {
542 memcpy(entry->data.value, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800543 data_payload_bytes);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800544 } else {
545 entry->data.offset = dst->data_count;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800546 memcpy(get_data(dst) + entry->data.offset, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800547 data_payload_bytes);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800548 dst->data_count += data_bytes;
549 }
550 dst->entry_count++;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700551 dst->flags &= ~FLAG_SORTED;
Alex Ray151389f2013-11-12 18:09:31 -0800552 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800553 return OK;
554}
555
556int add_camera_metadata_entry(camera_metadata_t *dst,
557 uint32_t tag,
558 const void *data,
559 size_t data_count) {
560
561 int type = get_camera_metadata_tag_type(tag);
562 if (type == -1) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700563 ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800564 return ERROR;
565 }
566
567 return add_camera_metadata_entry_raw(dst,
568 tag,
569 type,
570 data,
571 data_count);
572}
573
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700574static int compare_entry_tags(const void *p1, const void *p2) {
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700575 uint32_t tag1 = ((camera_metadata_buffer_entry_t*)p1)->tag;
576 uint32_t tag2 = ((camera_metadata_buffer_entry_t*)p2)->tag;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700577 return tag1 < tag2 ? -1 :
578 tag1 == tag2 ? 0 :
579 1;
580}
581
582int sort_camera_metadata(camera_metadata_t *dst) {
583 if (dst == NULL) return ERROR;
584 if (dst->flags & FLAG_SORTED) return OK;
585
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800586 qsort(get_entries(dst), dst->entry_count,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700587 sizeof(camera_metadata_buffer_entry_t),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700588 compare_entry_tags);
589 dst->flags |= FLAG_SORTED;
590
Alex Ray151389f2013-11-12 18:09:31 -0800591 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700592 return OK;
593}
594
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800595int get_camera_metadata_entry(camera_metadata_t *src,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700596 size_t index,
597 camera_metadata_entry_t *entry) {
598 if (src == NULL || entry == NULL) return ERROR;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800599 if (index >= src->entry_count) return ERROR;
600
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800601 camera_metadata_buffer_entry_t *buffer_entry = get_entries(src) + index;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800602
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700603 entry->index = index;
604 entry->tag = buffer_entry->tag;
605 entry->type = buffer_entry->type;
606 entry->count = buffer_entry->count;
607 if (buffer_entry->count *
608 camera_metadata_type_size[buffer_entry->type] > 4) {
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800609 entry->data.u8 = get_data(src) + buffer_entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700610 } else {
611 entry->data.u8 = buffer_entry->data.value;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700612 }
613 return OK;
614}
615
Alex Ray71fe0f82013-10-21 12:57:05 -0700616int get_camera_metadata_ro_entry(const camera_metadata_t *src,
617 size_t index,
618 camera_metadata_ro_entry_t *entry) {
619 return get_camera_metadata_entry((camera_metadata_t*)src, index,
620 (camera_metadata_entry_t*)entry);
621}
622
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700623int find_camera_metadata_entry(camera_metadata_t *src,
624 uint32_t tag,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700625 camera_metadata_entry_t *entry) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700626 if (src == NULL) return ERROR;
627
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700628 uint32_t index;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700629 if (src->flags & FLAG_SORTED) {
630 // Sorted entries, do a binary search
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700631 camera_metadata_buffer_entry_t *search_entry = NULL;
632 camera_metadata_buffer_entry_t key;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700633 key.tag = tag;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700634 search_entry = bsearch(&key,
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800635 get_entries(src),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700636 src->entry_count,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700637 sizeof(camera_metadata_buffer_entry_t),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700638 compare_entry_tags);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700639 if (search_entry == NULL) return NOT_FOUND;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800640 index = search_entry - get_entries(src);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800641 } else {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700642 // Not sorted, linear search
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800643 camera_metadata_buffer_entry_t *search_entry = get_entries(src);
644 for (index = 0; index < src->entry_count; index++, search_entry++) {
645 if (search_entry->tag == tag) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700646 break;
647 }
648 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700649 if (index == src->entry_count) return NOT_FOUND;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700650 }
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700651
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700652 return get_camera_metadata_entry(src, index,
653 entry);
654}
655
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700656int find_camera_metadata_ro_entry(const camera_metadata_t *src,
657 uint32_t tag,
658 camera_metadata_ro_entry_t *entry) {
659 return find_camera_metadata_entry((camera_metadata_t*)src, tag,
660 (camera_metadata_entry_t*)entry);
661}
662
663
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700664int delete_camera_metadata_entry(camera_metadata_t *dst,
665 size_t index) {
666 if (dst == NULL) return ERROR;
667 if (index >= dst->entry_count) return ERROR;
668
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800669 camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700670 size_t data_bytes = calculate_camera_metadata_entry_data_size(entry->type,
671 entry->count);
672
673 if (data_bytes > 0) {
674 // Shift data buffer to overwrite deleted data
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800675 uint8_t *start = get_data(dst) + entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700676 uint8_t *end = start + data_bytes;
677 size_t length = dst->data_count - entry->data.offset - data_bytes;
678 memmove(start, end, length);
679
680 // Update all entry indices to account for shift
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800681 camera_metadata_buffer_entry_t *e = get_entries(dst);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700682 size_t i;
683 for (i = 0; i < dst->entry_count; i++) {
684 if (calculate_camera_metadata_entry_data_size(
685 e->type, e->count) > 0 &&
686 e->data.offset > entry->data.offset) {
687 e->data.offset -= data_bytes;
688 }
689 ++e;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700690 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700691 dst->data_count -= data_bytes;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800692 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700693 // Shift entry array
694 memmove(entry, entry + 1,
695 sizeof(camera_metadata_buffer_entry_t) *
696 (dst->entry_count - index - 1) );
697 dst->entry_count -= 1;
698
Alex Ray151389f2013-11-12 18:09:31 -0800699 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700700 return OK;
701}
702
703int update_camera_metadata_entry(camera_metadata_t *dst,
704 size_t index,
705 const void *data,
706 size_t data_count,
707 camera_metadata_entry_t *updated_entry) {
708 if (dst == NULL) return ERROR;
709 if (index >= dst->entry_count) return ERROR;
710
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800711 camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700712
713 size_t data_bytes =
714 calculate_camera_metadata_entry_data_size(entry->type,
715 data_count);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800716 size_t data_payload_bytes =
717 data_count * camera_metadata_type_size[entry->type];
718
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700719 size_t entry_bytes =
720 calculate_camera_metadata_entry_data_size(entry->type,
721 entry->count);
722 if (data_bytes != entry_bytes) {
723 // May need to shift/add to data array
724 if (dst->data_capacity < dst->data_count + data_bytes - entry_bytes) {
725 // No room
726 return ERROR;
727 }
728 if (entry_bytes != 0) {
729 // Remove old data
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800730 uint8_t *start = get_data(dst) + entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700731 uint8_t *end = start + entry_bytes;
732 size_t length = dst->data_count - entry->data.offset - entry_bytes;
733 memmove(start, end, length);
734 dst->data_count -= entry_bytes;
735
736 // Update all entry indices to account for shift
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800737 camera_metadata_buffer_entry_t *e = get_entries(dst);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700738 size_t i;
739 for (i = 0; i < dst->entry_count; i++) {
740 if (calculate_camera_metadata_entry_data_size(
741 e->type, e->count) > 0 &&
742 e->data.offset > entry->data.offset) {
743 e->data.offset -= entry_bytes;
744 }
745 ++e;
746 }
747 }
748
749 if (data_bytes != 0) {
750 // Append new data
751 entry->data.offset = dst->data_count;
752
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800753 memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700754 dst->data_count += data_bytes;
755 }
756 } else if (data_bytes != 0) {
757 // data size unchanged, reuse same data location
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800758 memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700759 }
760
761 if (data_bytes == 0) {
762 // Data fits into entry
763 memcpy(entry->data.value, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800764 data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700765 }
766
767 entry->count = data_count;
768
769 if (updated_entry != NULL) {
770 get_camera_metadata_entry(dst,
771 index,
772 updated_entry);
773 }
774
Alex Ray151389f2013-11-12 18:09:31 -0800775 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800776 return OK;
777}
778
Ruben Brunk9c13e192014-02-14 16:40:56 -0800779static const vendor_tag_ops_t *vendor_tag_ops = NULL;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800780
781const char *get_camera_metadata_section_name(uint32_t tag) {
782 uint32_t tag_section = tag >> 16;
783 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800784 return vendor_tag_ops->get_section_name(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700785 vendor_tag_ops,
786 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800787 }
788 if (tag_section >= ANDROID_SECTION_COUNT) {
789 return NULL;
790 }
791 return camera_metadata_section_names[tag_section];
792}
793
794const char *get_camera_metadata_tag_name(uint32_t tag) {
795 uint32_t tag_section = tag >> 16;
796 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800797 return vendor_tag_ops->get_tag_name(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700798 vendor_tag_ops,
799 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800800 }
801 if (tag_section >= ANDROID_SECTION_COUNT ||
802 tag >= camera_metadata_section_bounds[tag_section][1] ) {
803 return NULL;
804 }
805 uint32_t tag_index = tag & 0xFFFF;
806 return tag_info[tag_section][tag_index].tag_name;
807}
808
809int get_camera_metadata_tag_type(uint32_t tag) {
810 uint32_t tag_section = tag >> 16;
811 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800812 return vendor_tag_ops->get_tag_type(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700813 vendor_tag_ops,
814 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800815 }
816 if (tag_section >= ANDROID_SECTION_COUNT ||
817 tag >= camera_metadata_section_bounds[tag_section][1] ) {
818 return -1;
819 }
820 uint32_t tag_index = tag & 0xFFFF;
821 return tag_info[tag_section][tag_index].tag_type;
822}
823
Ruben Brunk9c13e192014-02-14 16:40:56 -0800824int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t* ops) {
825 // **DEPRECATED**
826 ALOGE("%s: This function has been deprecated", __FUNCTION__);
827 return ERROR;
828}
829
830// Declared in system/media/private/camera/include/camera_metadata_hidden.h
831int set_camera_metadata_vendor_ops(const vendor_tag_ops_t* ops) {
832 vendor_tag_ops = ops;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800833 return OK;
834}
835
Igor Murashkin375cfd32012-12-03 13:55:33 -0800836static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag, int type,
837 int count,
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700838 int indentation);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800839
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700840void dump_camera_metadata(const camera_metadata_t *metadata,
841 int fd,
842 int verbosity) {
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700843 dump_indented_camera_metadata(metadata, fd, verbosity, 0);
844}
845
846void dump_indented_camera_metadata(const camera_metadata_t *metadata,
847 int fd,
848 int verbosity,
849 int indentation) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800850 if (metadata == NULL) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700851 dprintf(fd, "%*sDumping camera metadata array: Not allocated\n",
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700852 indentation, "");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800853 return;
854 }
855 unsigned int i;
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700856 dprintf(fd,
Narayan Kamath21532652014-05-30 15:25:18 +0100857 "%*sDumping camera metadata array: %" PRIu32 " / %" PRIu32 " entries, "
858 "%" PRIu32 " / %" PRIu32 " bytes of extra data.\n", indentation, "",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700859 metadata->entry_count, metadata->entry_capacity,
860 metadata->data_count, metadata->data_capacity);
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700861 dprintf(fd, "%*sVersion: %d, Flags: %08x\n",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700862 indentation + 2, "",
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700863 metadata->version, metadata->flags);
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800864 camera_metadata_buffer_entry_t *entry = get_entries(metadata);
865 for (i=0; i < metadata->entry_count; i++, entry++) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800866
867 const char *tag_name, *tag_section;
868 tag_section = get_camera_metadata_section_name(entry->tag);
869 if (tag_section == NULL) {
870 tag_section = "unknownSection";
871 }
872 tag_name = get_camera_metadata_tag_name(entry->tag);
873 if (tag_name == NULL) {
874 tag_name = "unknownTag";
875 }
876 const char *type_name;
877 if (entry->type >= NUM_TYPES) {
878 type_name = "unknown";
879 } else {
880 type_name = camera_metadata_type_names[entry->type];
881 }
Narayan Kamath21532652014-05-30 15:25:18 +0100882 dprintf(fd, "%*s%s.%s (%05x): %s[%" PRIu32 "]\n",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700883 indentation + 2, "",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800884 tag_section,
885 tag_name,
886 entry->tag,
887 type_name,
888 entry->count);
889
890 if (verbosity < 1) continue;
891
892 if (entry->type >= NUM_TYPES) continue;
893
894 size_t type_size = camera_metadata_type_size[entry->type];
895 uint8_t *data_ptr;
896 if ( type_size * entry->count > 4 ) {
897 if (entry->data.offset >= metadata->data_count) {
Narayan Kamath21532652014-05-30 15:25:18 +0100898 ALOGE("%s: Malformed entry data offset: %" PRIu32 " (max %" PRIu32 ")",
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700899 __FUNCTION__,
900 entry->data.offset,
901 metadata->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800902 continue;
903 }
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800904 data_ptr = get_data(metadata) + entry->data.offset;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800905 } else {
906 data_ptr = entry->data.value;
907 }
908 int count = entry->count;
909 if (verbosity < 2 && count > 16) count = 16;
910
Igor Murashkin375cfd32012-12-03 13:55:33 -0800911 print_data(fd, data_ptr, entry->tag, entry->type, count, indentation);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800912 }
913}
914
Igor Murashkin375cfd32012-12-03 13:55:33 -0800915static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag,
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700916 int type, int count, int indentation) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800917 static int values_per_line[NUM_TYPES] = {
918 [TYPE_BYTE] = 16,
919 [TYPE_INT32] = 4,
920 [TYPE_FLOAT] = 8,
921 [TYPE_INT64] = 2,
922 [TYPE_DOUBLE] = 4,
923 [TYPE_RATIONAL] = 2,
924 };
925 size_t type_size = camera_metadata_type_size[type];
Igor Murashkin375cfd32012-12-03 13:55:33 -0800926 char value_string_tmp[CAMERA_METADATA_ENUM_STRING_MAX_SIZE];
927 uint32_t value;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800928
929 int lines = count / values_per_line[type];
930 if (count % values_per_line[type] != 0) lines++;
931
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800932 int index = 0;
933 int j, k;
934 for (j = 0; j < lines; j++) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700935 dprintf(fd, "%*s[", indentation + 4, "");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800936 for (k = 0;
937 k < values_per_line[type] && count > 0;
938 k++, count--, index += type_size) {
939
940 switch (type) {
941 case TYPE_BYTE:
Igor Murashkin375cfd32012-12-03 13:55:33 -0800942 value = *(data_ptr + index);
943 if (camera_metadata_enum_snprint(tag,
944 value,
945 value_string_tmp,
946 sizeof(value_string_tmp))
947 == OK) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700948 dprintf(fd, "%s ", value_string_tmp);
Igor Murashkin375cfd32012-12-03 13:55:33 -0800949 } else {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700950 dprintf(fd, "%hhu ",
Igor Murashkin375cfd32012-12-03 13:55:33 -0800951 *(data_ptr + index));
952 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800953 break;
954 case TYPE_INT32:
Igor Murashkin375cfd32012-12-03 13:55:33 -0800955 value =
956 *(int32_t*)(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, "%" PRId32 " ",
Igor Murashkin375cfd32012-12-03 13:55:33 -0800965 *(int32_t*)(data_ptr + index));
966 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800967 break;
968 case TYPE_FLOAT:
Elliott Hughesd1c33572014-05-22 10:53:20 -0700969 dprintf(fd, "%0.8f ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800970 *(float*)(data_ptr + index));
971 break;
972 case TYPE_INT64:
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700973 dprintf(fd, "%" PRId64 " ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800974 *(int64_t*)(data_ptr + index));
975 break;
976 case TYPE_DOUBLE:
Elliott Hughesd1c33572014-05-22 10:53:20 -0700977 dprintf(fd, "%0.8f ",
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800978 *(double*)(data_ptr + index));
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800979 break;
980 case TYPE_RATIONAL: {
981 int32_t numerator = *(int32_t*)(data_ptr + index);
982 int32_t denominator = *(int32_t*)(data_ptr + index + 4);
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700983 dprintf(fd, "(%d / %d) ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800984 numerator, denominator);
985 break;
986 }
987 default:
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700988 dprintf(fd, "??? ");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800989 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800990 }
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700991 dprintf(fd, "]\n");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800992 }
993}