blob: eb0c6c4094a54f7e5e97772089423df63521212f [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
Chien-Yu Chen61af47d2016-08-12 15:40:16 -070029#define OK 0
30#define ERROR 1
31#define NOT_FOUND -ENOENT
32#define SN_EVENT_LOG_ID 0x534e4554
Eino-Ville Talvala31540362012-11-07 16:36:50 -080033
Eino-Ville Talvala31540362012-11-07 16:36:50 -080034#define ALIGN_TO(val, alignment) \
Igor Murashkind1cecc12013-04-30 17:21:54 -070035 (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
36
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080037/**
38 * A single metadata entry, storing an array of values of a given type. If the
39 * array is no larger than 4 bytes in size, it is stored in the data.value[]
40 * array; otherwise, it can found in the parent's data array at index
41 * data.offset.
42 */
Narayan Kamath21532652014-05-30 15:25:18 +010043#define ENTRY_ALIGNMENT ((size_t) 4)
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -070044typedef struct camera_metadata_buffer_entry {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080045 uint32_t tag;
Narayan Kamath21532652014-05-30 15:25:18 +010046 uint32_t count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080047 union {
Narayan Kamath21532652014-05-30 15:25:18 +010048 uint32_t offset;
49 uint8_t value[4];
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080050 } data;
51 uint8_t type;
52 uint8_t reserved[3];
Eino-Ville Talvala31540362012-11-07 16:36:50 -080053} camera_metadata_buffer_entry_t;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080054
Narayan Kamath21532652014-05-30 15:25:18 +010055typedef uint32_t metadata_uptrdiff_t;
56typedef uint32_t metadata_size_t;
57
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080058/**
59 * A packet of metadata. This is a list of entries, each of which may point to
60 * its values stored at an offset in data.
61 *
62 * It is assumed by the utility functions that the memory layout of the packet
63 * is as follows:
64 *
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -070065 * |-----------------------------------------------|
66 * | camera_metadata_t |
67 * | |
68 * |-----------------------------------------------|
69 * | reserved for future expansion |
70 * |-----------------------------------------------|
71 * | camera_metadata_buffer_entry_t #0 |
72 * |-----------------------------------------------|
73 * | .... |
74 * |-----------------------------------------------|
75 * | camera_metadata_buffer_entry_t #entry_count-1 |
76 * |-----------------------------------------------|
77 * | free space for |
78 * | (entry_capacity-entry_count) entries |
79 * |-----------------------------------------------|
80 * | start of camera_metadata.data |
81 * | |
82 * |-----------------------------------------------|
83 * | free space for |
84 * | (data_capacity-data_count) bytes |
85 * |-----------------------------------------------|
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080086 *
87 * With the total length of the whole packet being camera_metadata.size bytes.
88 *
89 * In short, the entries and data are contiguous in memory after the metadata
90 * header.
91 */
Narayan Kamath21532652014-05-30 15:25:18 +010092#define METADATA_ALIGNMENT ((size_t) 4)
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -080093struct camera_metadata {
Narayan Kamath21532652014-05-30 15:25:18 +010094 metadata_size_t size;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -070095 uint32_t version;
96 uint32_t flags;
Narayan Kamath21532652014-05-30 15:25:18 +010097 metadata_size_t entry_count;
98 metadata_size_t entry_capacity;
99 metadata_uptrdiff_t entries_start; // Offset from camera_metadata
100 metadata_size_t data_count;
101 metadata_size_t data_capacity;
102 metadata_uptrdiff_t data_start; // Offset from camera_metadata
103 uint8_t reserved[];
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800104};
105
Igor Murashkin555aac82012-11-15 10:54:57 -0800106/**
107 * A datum of metadata. This corresponds to camera_metadata_entry_t::data
108 * with the difference that each element is not a pointer. We need to have a
109 * non-pointer type description in order to figure out the largest alignment
110 * requirement for data (DATA_ALIGNMENT).
111 */
Narayan Kamath21532652014-05-30 15:25:18 +0100112#define DATA_ALIGNMENT ((size_t) 8)
Igor Murashkin555aac82012-11-15 10:54:57 -0800113typedef union camera_metadata_data {
114 uint8_t u8;
115 int32_t i32;
116 float f;
117 int64_t i64;
118 double d;
119 camera_metadata_rational_t r;
120} camera_metadata_data_t;
121
Narayan Kamath21532652014-05-30 15:25:18 +0100122/**
123 * The preferred alignment of a packet of camera metadata. In general,
124 * this is the lowest common multiple of the constituents of a metadata
125 * package, i.e, of DATA_ALIGNMENT and ENTRY_ALIGNMENT.
126 */
127#define MAX_ALIGNMENT(A, B) (((A) > (B)) ? (A) : (B))
128#define METADATA_PACKET_ALIGNMENT \
129 MAX_ALIGNMENT(MAX_ALIGNMENT(DATA_ALIGNMENT, METADATA_ALIGNMENT), ENTRY_ALIGNMENT);
130
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700131/** Versioning information */
132#define CURRENT_METADATA_VERSION 1
133
134/** Flag definitions */
135#define FLAG_SORTED 0x00000001
136
137/** Tag information */
138
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800139typedef struct tag_info {
140 const char *tag_name;
141 uint8_t tag_type;
142} tag_info_t;
143
144#include "camera_metadata_tag_info.c"
145
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700146const size_t camera_metadata_type_size[NUM_TYPES] = {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800147 [TYPE_BYTE] = sizeof(uint8_t),
148 [TYPE_INT32] = sizeof(int32_t),
149 [TYPE_FLOAT] = sizeof(float),
150 [TYPE_INT64] = sizeof(int64_t),
151 [TYPE_DOUBLE] = sizeof(double),
152 [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
153};
154
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700155const char *camera_metadata_type_names[NUM_TYPES] = {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800156 [TYPE_BYTE] = "byte",
157 [TYPE_INT32] = "int32",
158 [TYPE_FLOAT] = "float",
159 [TYPE_INT64] = "int64",
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700160 [TYPE_DOUBLE] = "double",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800161 [TYPE_RATIONAL] = "rational"
162};
163
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800164static camera_metadata_buffer_entry_t *get_entries(
165 const camera_metadata_t *metadata) {
166 return (camera_metadata_buffer_entry_t*)
167 ((uint8_t*)metadata + metadata->entries_start);
168}
169
170static uint8_t *get_data(const camera_metadata_t *metadata) {
171 return (uint8_t*)metadata + metadata->data_start;
172}
173
Zhijun He1450f7f2013-12-05 07:44:56 -0800174size_t get_camera_metadata_alignment() {
Narayan Kamath21532652014-05-30 15:25:18 +0100175 return METADATA_PACKET_ALIGNMENT;
Zhijun He1450f7f2013-12-05 07:44:56 -0800176}
177
Igor Murashkind1cecc12013-04-30 17:21:54 -0700178camera_metadata_t *allocate_copy_camera_metadata_checked(
179 const camera_metadata_t *src,
180 size_t src_size) {
181
182 if (src == NULL) {
183 return NULL;
184 }
185
Emilian Peev7dcbfce2017-11-06 10:41:19 +0000186 if (src_size < sizeof(camera_metadata_t)) {
187 ALOGE("%s: Source size too small!", __FUNCTION__);
188 android_errorWriteLog(0x534e4554, "67782345");
189 return NULL;
190 }
191
Igor Murashkind1cecc12013-04-30 17:21:54 -0700192 void *buffer = malloc(src_size);
193 memcpy(buffer, src, src_size);
194
195 camera_metadata_t *metadata = (camera_metadata_t*) buffer;
196 if (validate_camera_metadata_structure(metadata, &src_size) != OK) {
197 free(buffer);
198 return NULL;
199 }
200
201 return metadata;
202}
203
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800204camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
205 size_t data_capacity) {
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700206
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800207 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
208 data_capacity);
209 void *buffer = malloc(memory_needed);
210 return place_camera_metadata(buffer, memory_needed,
211 entry_capacity,
212 data_capacity);
213}
214
215camera_metadata_t *place_camera_metadata(void *dst,
216 size_t dst_size,
217 size_t entry_capacity,
218 size_t data_capacity) {
219 if (dst == NULL) return NULL;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800220
221 size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
222 data_capacity);
223 if (memory_needed > dst_size) return NULL;
224
225 camera_metadata_t *metadata = (camera_metadata_t*)dst;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700226 metadata->version = CURRENT_METADATA_VERSION;
227 metadata->flags = 0;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800228 metadata->entry_count = 0;
229 metadata->entry_capacity = entry_capacity;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800230 metadata->entries_start =
231 ALIGN_TO(sizeof(camera_metadata_t), ENTRY_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800232 metadata->data_count = 0;
233 metadata->data_capacity = data_capacity;
234 metadata->size = memory_needed;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700235 size_t data_unaligned = (uint8_t*)(get_entries(metadata) +
236 metadata->entry_capacity) - (uint8_t*)metadata;
237 metadata->data_start = ALIGN_TO(data_unaligned, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800238
Alex Ray151389f2013-11-12 18:09:31 -0800239 assert(validate_camera_metadata_structure(metadata, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800240 return metadata;
241}
242void free_camera_metadata(camera_metadata_t *metadata) {
243 free(metadata);
244}
245
246size_t calculate_camera_metadata_size(size_t entry_count,
247 size_t data_count) {
248 size_t memory_needed = sizeof(camera_metadata_t);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800249 // Start entry list at aligned boundary
250 memory_needed = ALIGN_TO(memory_needed, ENTRY_ALIGNMENT);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700251 memory_needed += sizeof(camera_metadata_buffer_entry_t[entry_count]);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800252 // Start buffer list at aligned boundary
253 memory_needed = ALIGN_TO(memory_needed, DATA_ALIGNMENT);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800254 memory_needed += sizeof(uint8_t[data_count]);
255 return memory_needed;
256}
257
258size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
259 if (metadata == NULL) return ERROR;
260
261 return metadata->size;
262}
263
264size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
265 if (metadata == NULL) return ERROR;
266
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800267 return calculate_camera_metadata_size(metadata->entry_count,
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800268 metadata->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800269}
270
271size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
272 return metadata->entry_count;
273}
274
275size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
276 return metadata->entry_capacity;
277}
278
279size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
280 return metadata->data_count;
281}
282
283size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
284 return metadata->data_capacity;
285}
286
287camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
288 const camera_metadata_t *src) {
289 size_t memory_needed = get_camera_metadata_compact_size(src);
290
291 if (dst == NULL) return NULL;
292 if (dst_size < memory_needed) return NULL;
293
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800294 camera_metadata_t *metadata =
295 place_camera_metadata(dst, dst_size, src->entry_count, src->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800296
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700297 metadata->flags = src->flags;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800298 metadata->entry_count = src->entry_count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800299 metadata->data_count = src->data_count;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800300
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800301 memcpy(get_entries(metadata), get_entries(src),
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700302 sizeof(camera_metadata_buffer_entry_t[metadata->entry_count]));
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800303 memcpy(get_data(metadata), get_data(src),
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800304 sizeof(uint8_t[metadata->data_count]));
305
Alex Ray151389f2013-11-12 18:09:31 -0800306 assert(validate_camera_metadata_structure(metadata, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800307 return metadata;
308}
309
Chien-Yu Chen61af47d2016-08-12 15:40:16 -0700310// This method should be used when the camera metadata cannot be trusted. For example, when it's
311// read from Parcel.
312static int validate_and_calculate_camera_metadata_entry_data_size(size_t *data_size, uint8_t type,
313 size_t data_count) {
314 if (type >= NUM_TYPES) return ERROR;
315
316 // Check for overflow
317 if (data_count != 0 &&
318 camera_metadata_type_size[type] > (SIZE_MAX - DATA_ALIGNMENT + 1) / data_count) {
319 android_errorWriteLog(SN_EVENT_LOG_ID, "30741779");
320 return ERROR;
321 }
322
323 size_t data_bytes = data_count * camera_metadata_type_size[type];
324
325 if (data_size) {
326 *data_size = data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
327 }
328
329 return OK;
330}
331
332size_t calculate_camera_metadata_entry_data_size(uint8_t type,
333 size_t data_count) {
334 if (type >= NUM_TYPES) return 0;
335
336 size_t data_bytes = data_count *
337 camera_metadata_type_size[type];
338
339 return data_bytes <= 4 ? 0 : ALIGN_TO(data_bytes, DATA_ALIGNMENT);
340}
341
Igor Murashkind1cecc12013-04-30 17:21:54 -0700342int validate_camera_metadata_structure(const camera_metadata_t *metadata,
343 const size_t *expected_size) {
344
345 if (metadata == NULL) {
Zhijun He246a1ad2013-11-26 13:41:18 -0800346 ALOGE("%s: metadata is null!", __FUNCTION__);
Igor Murashkind1cecc12013-04-30 17:21:54 -0700347 return ERROR;
348 }
349
350 // Check that the metadata pointer is well-aligned first.
351 {
Narayan Kamath21532652014-05-30 15:25:18 +0100352 static const struct {
Igor Murashkind1cecc12013-04-30 17:21:54 -0700353 const char *name;
354 size_t alignment;
355 } alignments[] = {
356 {
357 .name = "camera_metadata",
Narayan Kamath21532652014-05-30 15:25:18 +0100358 .alignment = METADATA_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700359 },
360 {
361 .name = "camera_metadata_buffer_entry",
Narayan Kamath21532652014-05-30 15:25:18 +0100362 .alignment = ENTRY_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700363 },
364 {
365 .name = "camera_metadata_data",
Narayan Kamath21532652014-05-30 15:25:18 +0100366 .alignment = DATA_ALIGNMENT
Igor Murashkind1cecc12013-04-30 17:21:54 -0700367 },
368 };
369
370 for (size_t i = 0; i < sizeof(alignments)/sizeof(alignments[0]); ++i) {
371 uintptr_t aligned_ptr = ALIGN_TO(metadata, alignments[i].alignment);
372
373 if ((uintptr_t)metadata != aligned_ptr) {
374 ALOGE("%s: Metadata pointer is not aligned (actual %p, "
375 "expected %p) to type %s",
376 __FUNCTION__, metadata,
377 (void*)aligned_ptr, alignments[i].name);
378 return ERROR;
379 }
380 }
381 }
382
383 /**
384 * Check that the metadata contents are correct
385 */
386
387 if (expected_size != NULL && metadata->size > *expected_size) {
Narayan Kamath21532652014-05-30 15:25:18 +0100388 ALOGE("%s: Metadata size (%" PRIu32 ") should be <= expected size (%zu)",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700389 __FUNCTION__, metadata->size, *expected_size);
390 return ERROR;
391 }
392
393 if (metadata->entry_count > metadata->entry_capacity) {
Narayan Kamath21532652014-05-30 15:25:18 +0100394 ALOGE("%s: Entry count (%" PRIu32 ") should be <= entry capacity "
395 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700396 __FUNCTION__, metadata->entry_count, metadata->entry_capacity);
397 return ERROR;
398 }
399
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -0700400 if (metadata->data_count > metadata->data_capacity) {
401 ALOGE("%s: Data count (%" PRIu32 ") should be <= data capacity "
402 "(%" PRIu32 ")",
403 __FUNCTION__, metadata->data_count, metadata->data_capacity);
404 android_errorWriteLog(SN_EVENT_LOG_ID, "30591838");
405 return ERROR;
406 }
Narayan Kamath21532652014-05-30 15:25:18 +0100407 const metadata_uptrdiff_t entries_end =
408 metadata->entries_start + metadata->entry_capacity;
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -0700409
410
Igor Murashkind1cecc12013-04-30 17:21:54 -0700411 if (entries_end < metadata->entries_start || // overflow check
412 entries_end > metadata->data_start) {
413
Narayan Kamath21532652014-05-30 15:25:18 +0100414 ALOGE("%s: Entry start + capacity (%" PRIu32 ") should be <= data start "
415 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700416 __FUNCTION__,
417 (metadata->entries_start + metadata->entry_capacity),
418 metadata->data_start);
419 return ERROR;
420 }
421
Narayan Kamath21532652014-05-30 15:25:18 +0100422 const metadata_uptrdiff_t data_end =
423 metadata->data_start + metadata->data_capacity;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700424 if (data_end < metadata->data_start || // overflow check
425 data_end > metadata->size) {
426
Narayan Kamath21532652014-05-30 15:25:18 +0100427 ALOGE("%s: Data start + capacity (%" PRIu32 ") should be <= total size "
428 "(%" PRIu32 ")",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700429 __FUNCTION__,
430 (metadata->data_start + metadata->data_capacity),
431 metadata->size);
432 return ERROR;
433 }
434
435 // Validate each entry
Narayan Kamath21532652014-05-30 15:25:18 +0100436 const metadata_size_t entry_count = metadata->entry_count;
Igor Murashkind1cecc12013-04-30 17:21:54 -0700437 camera_metadata_buffer_entry_t *entries = get_entries(metadata);
438
439 for (size_t i = 0; i < entry_count; ++i) {
440
441 if ((uintptr_t)&entries[i] != ALIGN_TO(&entries[i], ENTRY_ALIGNMENT)) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700442 ALOGE("%s: Entry index %zu had bad alignment (address %p),"
443 " expected alignment %zu",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700444 __FUNCTION__, i, &entries[i], ENTRY_ALIGNMENT);
445 return ERROR;
446 }
447
448 camera_metadata_buffer_entry_t entry = entries[i];
449
450 if (entry.type >= NUM_TYPES) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700451 ALOGE("%s: Entry index %zu had a bad type %d",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700452 __FUNCTION__, i, entry.type);
453 return ERROR;
454 }
455
456 // TODO: fix vendor_tag_ops across processes so we don't need to special
457 // case vendor-specific tags
458 uint32_t tag_section = entry.tag >> 16;
459 int tag_type = get_camera_metadata_tag_type(entry.tag);
460 if (tag_type != (int)entry.type && tag_section < VENDOR_SECTION) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700461 ALOGE("%s: Entry index %zu had tag type %d, but the type was %d",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700462 __FUNCTION__, i, tag_type, entry.type);
463 return ERROR;
464 }
465
Chien-Yu Chen61af47d2016-08-12 15:40:16 -0700466 size_t data_size;
467 if (validate_and_calculate_camera_metadata_entry_data_size(&data_size, entry.type,
468 entry.count) != OK) {
469 ALOGE("%s: Entry data size is invalid. type: %u count: %u", __FUNCTION__, entry.type,
470 entry.count);
471 return ERROR;
472 }
Igor Murashkind1cecc12013-04-30 17:21:54 -0700473
474 if (data_size != 0) {
475 camera_metadata_data_t *data =
476 (camera_metadata_data_t*) (get_data(metadata) +
477 entry.data.offset);
478
479 if ((uintptr_t)data != ALIGN_TO(data, DATA_ALIGNMENT)) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700480 ALOGE("%s: Entry index %zu had bad data alignment (address %p),"
481 " expected align %zu, (tag name %s, data size %zu)",
Igor Murashkind1cecc12013-04-30 17:21:54 -0700482 __FUNCTION__, i, data, DATA_ALIGNMENT,
483 get_camera_metadata_tag_name(entry.tag) ?: "unknown",
484 data_size);
485 return ERROR;
486 }
487
488 size_t data_entry_end = entry.data.offset + data_size;
489 if (data_entry_end < entry.data.offset || // overflow check
490 data_entry_end > metadata->data_capacity) {
491
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700492 ALOGE("%s: Entry index %zu data ends (%zu) beyond the capacity "
Narayan Kamath21532652014-05-30 15:25:18 +0100493 "%" PRIu32, __FUNCTION__, i, data_entry_end,
Igor Murashkind1cecc12013-04-30 17:21:54 -0700494 metadata->data_capacity);
495 return ERROR;
496 }
497
498 } else if (entry.count == 0) {
499 if (entry.data.offset != 0) {
Mark Salyzyn773e5c82014-03-13 13:24:19 -0700500 ALOGE("%s: Entry index %zu had 0 items, but offset was non-0 "
Narayan Kamath21532652014-05-30 15:25:18 +0100501 "(%" PRIu32 "), tag name: %s", __FUNCTION__, i, entry.data.offset,
Zhijun He3064d312013-07-15 13:55:11 -0700502 get_camera_metadata_tag_name(entry.tag) ?: "unknown");
Igor Murashkind1cecc12013-04-30 17:21:54 -0700503 return ERROR;
504 }
505 } // else data stored inline, so we look at value which can be anything.
506 }
507
508 return OK;
509}
510
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800511int append_camera_metadata(camera_metadata_t *dst,
512 const camera_metadata_t *src) {
513 if (dst == NULL || src == NULL ) return ERROR;
514
Eino-Ville Talvaladd2dd092016-08-16 15:48:05 -0700515 // Check for overflow
516 if (src->entry_count + dst->entry_count < src->entry_count) return ERROR;
517 if (src->data_count + dst->data_count < src->data_count) return ERROR;
518 // Check for space
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800519 if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
520 if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
521
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800522 memcpy(get_entries(dst) + dst->entry_count, get_entries(src),
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700523 sizeof(camera_metadata_buffer_entry_t[src->entry_count]));
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800524 memcpy(get_data(dst) + dst->data_count, get_data(src),
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800525 sizeof(uint8_t[src->data_count]));
526 if (dst->data_count != 0) {
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800527 camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
528 for (size_t i = 0; i < src->entry_count; i++, entry++) {
529 if ( calculate_camera_metadata_entry_data_size(entry->type,
530 entry->count) > 0 ) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800531 entry->data.offset += dst->data_count;
532 }
533 }
534 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700535 if (dst->entry_count == 0) {
536 // Appending onto empty buffer, keep sorted state
537 dst->flags |= src->flags & FLAG_SORTED;
538 } else if (src->entry_count != 0) {
539 // Both src, dst are nonempty, cannot assume sort remains
540 dst->flags &= ~FLAG_SORTED;
541 } else {
542 // Src is empty, keep dst sorted state
543 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800544 dst->entry_count += src->entry_count;
545 dst->data_count += src->data_count;
546
Alex Ray151389f2013-11-12 18:09:31 -0800547 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800548 return OK;
549}
550
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700551camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src) {
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700552 int res;
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700553 if (src == NULL) return NULL;
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700554 camera_metadata_t *clone = allocate_camera_metadata(
555 get_camera_metadata_entry_count(src),
556 get_camera_metadata_data_count(src));
557 if (clone != NULL) {
558 res = append_camera_metadata(clone, src);
559 if (res != OK) {
560 free_camera_metadata(clone);
561 clone = NULL;
562 }
563 }
Alex Ray151389f2013-11-12 18:09:31 -0800564 assert(validate_camera_metadata_structure(clone, NULL) == OK);
Eino-Ville Talvalafd258822012-06-06 17:22:42 -0700565 return clone;
566}
567
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700568static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800569 uint32_t tag,
570 uint8_t type,
571 const void *data,
572 size_t data_count) {
573
574 if (dst == NULL) return ERROR;
575 if (dst->entry_count == dst->entry_capacity) return ERROR;
576 if (data == NULL) return ERROR;
577
578 size_t data_bytes =
579 calculate_camera_metadata_entry_data_size(type, data_count);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800580 if (data_bytes + dst->data_count > dst->data_capacity) return ERROR;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800581
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800582 size_t data_payload_bytes =
583 data_count * camera_metadata_type_size[type];
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800584 camera_metadata_buffer_entry_t *entry = get_entries(dst) + dst->entry_count;
Zhijun He3064d312013-07-15 13:55:11 -0700585 memset(entry, 0, sizeof(camera_metadata_buffer_entry_t));
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800586 entry->tag = tag;
587 entry->type = type;
588 entry->count = data_count;
589
590 if (data_bytes == 0) {
591 memcpy(entry->data.value, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800592 data_payload_bytes);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800593 } else {
594 entry->data.offset = dst->data_count;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800595 memcpy(get_data(dst) + entry->data.offset, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800596 data_payload_bytes);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800597 dst->data_count += data_bytes;
598 }
599 dst->entry_count++;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700600 dst->flags &= ~FLAG_SORTED;
Alex Ray151389f2013-11-12 18:09:31 -0800601 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800602 return OK;
603}
604
605int add_camera_metadata_entry(camera_metadata_t *dst,
606 uint32_t tag,
607 const void *data,
608 size_t data_count) {
609
610 int type = get_camera_metadata_tag_type(tag);
611 if (type == -1) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700612 ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800613 return ERROR;
614 }
615
616 return add_camera_metadata_entry_raw(dst,
617 tag,
618 type,
619 data,
620 data_count);
621}
622
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700623static int compare_entry_tags(const void *p1, const void *p2) {
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700624 uint32_t tag1 = ((camera_metadata_buffer_entry_t*)p1)->tag;
625 uint32_t tag2 = ((camera_metadata_buffer_entry_t*)p2)->tag;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700626 return tag1 < tag2 ? -1 :
627 tag1 == tag2 ? 0 :
628 1;
629}
630
631int sort_camera_metadata(camera_metadata_t *dst) {
632 if (dst == NULL) return ERROR;
633 if (dst->flags & FLAG_SORTED) return OK;
634
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800635 qsort(get_entries(dst), dst->entry_count,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700636 sizeof(camera_metadata_buffer_entry_t),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700637 compare_entry_tags);
638 dst->flags |= FLAG_SORTED;
639
Alex Ray151389f2013-11-12 18:09:31 -0800640 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700641 return OK;
642}
643
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800644int get_camera_metadata_entry(camera_metadata_t *src,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700645 size_t index,
646 camera_metadata_entry_t *entry) {
647 if (src == NULL || entry == NULL) return ERROR;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800648 if (index >= src->entry_count) return ERROR;
649
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800650 camera_metadata_buffer_entry_t *buffer_entry = get_entries(src) + index;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800651
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700652 entry->index = index;
653 entry->tag = buffer_entry->tag;
654 entry->type = buffer_entry->type;
655 entry->count = buffer_entry->count;
656 if (buffer_entry->count *
657 camera_metadata_type_size[buffer_entry->type] > 4) {
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800658 entry->data.u8 = get_data(src) + buffer_entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700659 } else {
660 entry->data.u8 = buffer_entry->data.value;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700661 }
662 return OK;
663}
664
Alex Ray71fe0f82013-10-21 12:57:05 -0700665int get_camera_metadata_ro_entry(const camera_metadata_t *src,
666 size_t index,
667 camera_metadata_ro_entry_t *entry) {
668 return get_camera_metadata_entry((camera_metadata_t*)src, index,
669 (camera_metadata_entry_t*)entry);
670}
671
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700672int find_camera_metadata_entry(camera_metadata_t *src,
673 uint32_t tag,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700674 camera_metadata_entry_t *entry) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700675 if (src == NULL) return ERROR;
676
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700677 uint32_t index;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700678 if (src->flags & FLAG_SORTED) {
679 // Sorted entries, do a binary search
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700680 camera_metadata_buffer_entry_t *search_entry = NULL;
681 camera_metadata_buffer_entry_t key;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700682 key.tag = tag;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700683 search_entry = bsearch(&key,
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800684 get_entries(src),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700685 src->entry_count,
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700686 sizeof(camera_metadata_buffer_entry_t),
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700687 compare_entry_tags);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700688 if (search_entry == NULL) return NOT_FOUND;
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800689 index = search_entry - get_entries(src);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800690 } else {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700691 // Not sorted, linear search
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800692 camera_metadata_buffer_entry_t *search_entry = get_entries(src);
693 for (index = 0; index < src->entry_count; index++, search_entry++) {
694 if (search_entry->tag == tag) {
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700695 break;
696 }
697 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700698 if (index == src->entry_count) return NOT_FOUND;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700699 }
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700700
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700701 return get_camera_metadata_entry(src, index,
702 entry);
703}
704
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700705int find_camera_metadata_ro_entry(const camera_metadata_t *src,
706 uint32_t tag,
707 camera_metadata_ro_entry_t *entry) {
708 return find_camera_metadata_entry((camera_metadata_t*)src, tag,
709 (camera_metadata_entry_t*)entry);
710}
711
712
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700713int delete_camera_metadata_entry(camera_metadata_t *dst,
714 size_t index) {
715 if (dst == NULL) return ERROR;
716 if (index >= dst->entry_count) return ERROR;
717
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800718 camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700719 size_t data_bytes = calculate_camera_metadata_entry_data_size(entry->type,
720 entry->count);
721
722 if (data_bytes > 0) {
723 // Shift data buffer to overwrite deleted data
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800724 uint8_t *start = get_data(dst) + entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700725 uint8_t *end = start + data_bytes;
726 size_t length = dst->data_count - entry->data.offset - data_bytes;
727 memmove(start, end, length);
728
729 // Update all entry indices to account for shift
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800730 camera_metadata_buffer_entry_t *e = get_entries(dst);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700731 size_t i;
732 for (i = 0; i < dst->entry_count; i++) {
733 if (calculate_camera_metadata_entry_data_size(
734 e->type, e->count) > 0 &&
735 e->data.offset > entry->data.offset) {
736 e->data.offset -= data_bytes;
737 }
738 ++e;
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700739 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700740 dst->data_count -= data_bytes;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800741 }
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700742 // Shift entry array
743 memmove(entry, entry + 1,
744 sizeof(camera_metadata_buffer_entry_t) *
745 (dst->entry_count - index - 1) );
746 dst->entry_count -= 1;
747
Alex Ray151389f2013-11-12 18:09:31 -0800748 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700749 return OK;
750}
751
752int update_camera_metadata_entry(camera_metadata_t *dst,
753 size_t index,
754 const void *data,
755 size_t data_count,
756 camera_metadata_entry_t *updated_entry) {
757 if (dst == NULL) return ERROR;
758 if (index >= dst->entry_count) return ERROR;
759
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800760 camera_metadata_buffer_entry_t *entry = get_entries(dst) + index;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700761
762 size_t data_bytes =
763 calculate_camera_metadata_entry_data_size(entry->type,
764 data_count);
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800765 size_t data_payload_bytes =
766 data_count * camera_metadata_type_size[entry->type];
767
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700768 size_t entry_bytes =
769 calculate_camera_metadata_entry_data_size(entry->type,
770 entry->count);
771 if (data_bytes != entry_bytes) {
772 // May need to shift/add to data array
773 if (dst->data_capacity < dst->data_count + data_bytes - entry_bytes) {
774 // No room
775 return ERROR;
776 }
777 if (entry_bytes != 0) {
778 // Remove old data
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800779 uint8_t *start = get_data(dst) + entry->data.offset;
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700780 uint8_t *end = start + entry_bytes;
781 size_t length = dst->data_count - entry->data.offset - entry_bytes;
782 memmove(start, end, length);
783 dst->data_count -= entry_bytes;
784
785 // Update all entry indices to account for shift
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800786 camera_metadata_buffer_entry_t *e = get_entries(dst);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700787 size_t i;
788 for (i = 0; i < dst->entry_count; i++) {
789 if (calculate_camera_metadata_entry_data_size(
790 e->type, e->count) > 0 &&
791 e->data.offset > entry->data.offset) {
792 e->data.offset -= entry_bytes;
793 }
794 ++e;
795 }
796 }
797
798 if (data_bytes != 0) {
799 // Append new data
800 entry->data.offset = dst->data_count;
801
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800802 memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700803 dst->data_count += data_bytes;
804 }
805 } else if (data_bytes != 0) {
806 // data size unchanged, reuse same data location
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800807 memcpy(get_data(dst) + entry->data.offset, data, data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700808 }
809
810 if (data_bytes == 0) {
811 // Data fits into entry
812 memcpy(entry->data.value, data,
Eino-Ville Talvala31540362012-11-07 16:36:50 -0800813 data_payload_bytes);
Eino-Ville Talvalaf5fb8a52012-05-10 14:22:28 -0700814 }
815
816 entry->count = data_count;
817
818 if (updated_entry != NULL) {
819 get_camera_metadata_entry(dst,
820 index,
821 updated_entry);
822 }
823
Alex Ray151389f2013-11-12 18:09:31 -0800824 assert(validate_camera_metadata_structure(dst, NULL) == OK);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800825 return OK;
826}
827
Ruben Brunk9c13e192014-02-14 16:40:56 -0800828static const vendor_tag_ops_t *vendor_tag_ops = NULL;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800829
830const char *get_camera_metadata_section_name(uint32_t tag) {
831 uint32_t tag_section = tag >> 16;
832 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800833 return vendor_tag_ops->get_section_name(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700834 vendor_tag_ops,
835 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800836 }
837 if (tag_section >= ANDROID_SECTION_COUNT) {
838 return NULL;
839 }
840 return camera_metadata_section_names[tag_section];
841}
842
843const char *get_camera_metadata_tag_name(uint32_t tag) {
844 uint32_t tag_section = tag >> 16;
845 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800846 return vendor_tag_ops->get_tag_name(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700847 vendor_tag_ops,
848 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800849 }
850 if (tag_section >= ANDROID_SECTION_COUNT ||
851 tag >= camera_metadata_section_bounds[tag_section][1] ) {
852 return NULL;
853 }
854 uint32_t tag_index = tag & 0xFFFF;
855 return tag_info[tag_section][tag_index].tag_name;
856}
857
858int get_camera_metadata_tag_type(uint32_t tag) {
859 uint32_t tag_section = tag >> 16;
860 if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
Ruben Brunk9c13e192014-02-14 16:40:56 -0800861 return vendor_tag_ops->get_tag_type(
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700862 vendor_tag_ops,
863 tag);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800864 }
865 if (tag_section >= ANDROID_SECTION_COUNT ||
866 tag >= camera_metadata_section_bounds[tag_section][1] ) {
867 return -1;
868 }
869 uint32_t tag_index = tag & 0xFFFF;
870 return tag_info[tag_section][tag_index].tag_type;
871}
872
Ruben Brunk9c13e192014-02-14 16:40:56 -0800873int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t* ops) {
874 // **DEPRECATED**
875 ALOGE("%s: This function has been deprecated", __FUNCTION__);
876 return ERROR;
877}
878
879// Declared in system/media/private/camera/include/camera_metadata_hidden.h
880int set_camera_metadata_vendor_ops(const vendor_tag_ops_t* ops) {
881 vendor_tag_ops = ops;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800882 return OK;
883}
884
Igor Murashkin375cfd32012-12-03 13:55:33 -0800885static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag, int type,
886 int count,
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700887 int indentation);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800888
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700889void dump_camera_metadata(const camera_metadata_t *metadata,
890 int fd,
891 int verbosity) {
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700892 dump_indented_camera_metadata(metadata, fd, verbosity, 0);
893}
894
895void dump_indented_camera_metadata(const camera_metadata_t *metadata,
896 int fd,
897 int verbosity,
898 int indentation) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800899 if (metadata == NULL) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700900 dprintf(fd, "%*sDumping camera metadata array: Not allocated\n",
Eino-Ville Talvala2f4aca62012-08-24 11:42:06 -0700901 indentation, "");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800902 return;
903 }
904 unsigned int i;
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700905 dprintf(fd,
Narayan Kamath21532652014-05-30 15:25:18 +0100906 "%*sDumping camera metadata array: %" PRIu32 " / %" PRIu32 " entries, "
907 "%" PRIu32 " / %" PRIu32 " bytes of extra data.\n", indentation, "",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700908 metadata->entry_count, metadata->entry_capacity,
909 metadata->data_count, metadata->data_capacity);
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700910 dprintf(fd, "%*sVersion: %d, Flags: %08x\n",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700911 indentation + 2, "",
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700912 metadata->version, metadata->flags);
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800913 camera_metadata_buffer_entry_t *entry = get_entries(metadata);
914 for (i=0; i < metadata->entry_count; i++, entry++) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800915
916 const char *tag_name, *tag_section;
917 tag_section = get_camera_metadata_section_name(entry->tag);
918 if (tag_section == NULL) {
919 tag_section = "unknownSection";
920 }
921 tag_name = get_camera_metadata_tag_name(entry->tag);
922 if (tag_name == NULL) {
923 tag_name = "unknownTag";
924 }
925 const char *type_name;
926 if (entry->type >= NUM_TYPES) {
927 type_name = "unknown";
928 } else {
929 type_name = camera_metadata_type_names[entry->type];
930 }
Narayan Kamath21532652014-05-30 15:25:18 +0100931 dprintf(fd, "%*s%s.%s (%05x): %s[%" PRIu32 "]\n",
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700932 indentation + 2, "",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800933 tag_section,
934 tag_name,
935 entry->tag,
936 type_name,
937 entry->count);
938
939 if (verbosity < 1) continue;
940
941 if (entry->type >= NUM_TYPES) continue;
942
943 size_t type_size = camera_metadata_type_size[entry->type];
944 uint8_t *data_ptr;
945 if ( type_size * entry->count > 4 ) {
946 if (entry->data.offset >= metadata->data_count) {
Narayan Kamath21532652014-05-30 15:25:18 +0100947 ALOGE("%s: Malformed entry data offset: %" PRIu32 " (max %" PRIu32 ")",
Eino-Ville Talvala94c19012012-04-04 15:30:06 -0700948 __FUNCTION__,
949 entry->data.offset,
950 metadata->data_count);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800951 continue;
952 }
Eino-Ville Talvalab10d56a2012-11-13 11:45:12 -0800953 data_ptr = get_data(metadata) + entry->data.offset;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800954 } else {
955 data_ptr = entry->data.value;
956 }
957 int count = entry->count;
958 if (verbosity < 2 && count > 16) count = 16;
959
Igor Murashkin375cfd32012-12-03 13:55:33 -0800960 print_data(fd, data_ptr, entry->tag, entry->type, count, indentation);
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800961 }
962}
963
Igor Murashkin375cfd32012-12-03 13:55:33 -0800964static void print_data(int fd, const uint8_t *data_ptr, uint32_t tag,
Eino-Ville Talvala1d27e5b2012-07-30 10:46:22 -0700965 int type, int count, int indentation) {
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800966 static int values_per_line[NUM_TYPES] = {
967 [TYPE_BYTE] = 16,
968 [TYPE_INT32] = 4,
969 [TYPE_FLOAT] = 8,
970 [TYPE_INT64] = 2,
971 [TYPE_DOUBLE] = 4,
972 [TYPE_RATIONAL] = 2,
973 };
974 size_t type_size = camera_metadata_type_size[type];
Igor Murashkin375cfd32012-12-03 13:55:33 -0800975 char value_string_tmp[CAMERA_METADATA_ENUM_STRING_MAX_SIZE];
976 uint32_t value;
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800977
978 int lines = count / values_per_line[type];
979 if (count % values_per_line[type] != 0) lines++;
980
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800981 int index = 0;
982 int j, k;
983 for (j = 0; j < lines; j++) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700984 dprintf(fd, "%*s[", indentation + 4, "");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -0800985 for (k = 0;
986 k < values_per_line[type] && count > 0;
987 k++, count--, index += type_size) {
988
989 switch (type) {
990 case TYPE_BYTE:
Igor Murashkin375cfd32012-12-03 13:55:33 -0800991 value = *(data_ptr + index);
992 if (camera_metadata_enum_snprint(tag,
993 value,
994 value_string_tmp,
995 sizeof(value_string_tmp))
996 == OK) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700997 dprintf(fd, "%s ", value_string_tmp);
Igor Murashkin375cfd32012-12-03 13:55:33 -0800998 } else {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -0700999 dprintf(fd, "%hhu ",
Igor Murashkin375cfd32012-12-03 13:55:33 -08001000 *(data_ptr + index));
1001 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001002 break;
1003 case TYPE_INT32:
Igor Murashkin375cfd32012-12-03 13:55:33 -08001004 value =
1005 *(int32_t*)(data_ptr + index);
1006 if (camera_metadata_enum_snprint(tag,
1007 value,
1008 value_string_tmp,
1009 sizeof(value_string_tmp))
1010 == OK) {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001011 dprintf(fd, "%s ", value_string_tmp);
Igor Murashkin375cfd32012-12-03 13:55:33 -08001012 } else {
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001013 dprintf(fd, "%" PRId32 " ",
Igor Murashkin375cfd32012-12-03 13:55:33 -08001014 *(int32_t*)(data_ptr + index));
1015 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001016 break;
1017 case TYPE_FLOAT:
Elliott Hughesd1c33572014-05-22 10:53:20 -07001018 dprintf(fd, "%0.8f ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001019 *(float*)(data_ptr + index));
1020 break;
1021 case TYPE_INT64:
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001022 dprintf(fd, "%" PRId64 " ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001023 *(int64_t*)(data_ptr + index));
1024 break;
1025 case TYPE_DOUBLE:
Elliott Hughesd1c33572014-05-22 10:53:20 -07001026 dprintf(fd, "%0.8f ",
Eino-Ville Talvala31540362012-11-07 16:36:50 -08001027 *(double*)(data_ptr + index));
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001028 break;
1029 case TYPE_RATIONAL: {
1030 int32_t numerator = *(int32_t*)(data_ptr + index);
1031 int32_t denominator = *(int32_t*)(data_ptr + index + 4);
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001032 dprintf(fd, "(%d / %d) ",
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001033 numerator, denominator);
1034 break;
1035 }
1036 default:
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001037 dprintf(fd, "??? ");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001038 }
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001039 }
Elliott Hughes8e86c9c2014-05-22 01:18:02 -07001040 dprintf(fd, "]\n");
Eino-Ville Talvalad8afb4d2012-02-10 14:27:08 -08001041 }
1042}