2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef SYSTEM_MEDIA_INCLUDE_ANDROID_CAMERA_METADATA_H
18 #define SYSTEM_MEDIA_INCLUDE_ANDROID_CAMERA_METADATA_H
22 #include <cutils/compiler.h>
29 * Tag hierarchy and enum definitions for camera_metadata_entry
30 * =============================================================================
34 * Main enum definitions are in a separate file to make it easy to
37 #include "camera_metadata_tags.h"
40 * Enum range for each top-level category
43 extern unsigned int camera_metadata_section_bounds[ANDROID_SECTION_COUNT][2];
45 extern const char *camera_metadata_section_names[ANDROID_SECTION_COUNT];
48 * Type definitions for camera_metadata_entry
49 * =============================================================================
52 // Unsigned 8-bit integer (uint8_t)
54 // Signed 32-bit integer (int32_t)
56 // 32-bit float (float)
58 // Signed 64-bit integer (int64_t)
60 // 64-bit float (double)
62 // A 64-bit fraction (camera_metadata_rational_t)
64 // Number of type fields
68 typedef struct camera_metadata_rational {
71 } camera_metadata_rational_t;
74 * A reference to a metadata entry in a buffer.
76 * The data union pointers point to the real data in the buffer, and can be
77 * modified in-place if the count does not need to change. The count is the
78 * number of entries in data of the entry's type, not a count of bytes.
80 typedef struct camera_metadata_entry {
91 camera_metadata_rational_t *r;
93 } camera_metadata_entry_t;
96 * A read-only reference to a metadata entry in a buffer. Identical to
97 * camera_metadata_entry in layout
99 typedef struct camera_metadata_ro_entry {
110 const camera_metadata_rational_t *r;
112 } camera_metadata_ro_entry_t;
115 * Size in bytes of each entry type
118 extern const size_t camera_metadata_type_size[NUM_TYPES];
121 * Human-readable name of each entry type
124 extern const char* camera_metadata_type_names[NUM_TYPES];
127 * Main definitions for the metadata entry and array structures
128 * =============================================================================
132 * A packet of metadata. This is a list of metadata entries, each of which has
133 * an integer tag to identify its meaning, 'type' and 'count' field, and the
134 * data, which contains a 'count' number of entries of type 'type'. The packet
135 * has a fixed capacity for entries and for extra data. A new entry uses up one
136 * entry slot, and possibly some amount of data capacity; the function
137 * calculate_camera_metadata_entry_data_size() provides the amount of data
138 * capacity that would be used up by an entry.
140 * Entries are not sorted by default, and are not forced to be unique - multiple
141 * entries with the same tag are allowed. The packet will not dynamically resize
144 * The packet is contiguous in memory, with size in bytes given by
145 * get_camera_metadata_size(). Therefore, it can be copied safely with memcpy()
146 * to a buffer of sufficient size. The copy_camera_metadata() function is
147 * intended for eliminating unused capacity in the destination packet.
149 struct camera_metadata;
150 typedef struct camera_metadata camera_metadata_t;
153 * Functions for manipulating camera metadata
154 * =============================================================================
156 * NOTE: Unless otherwise specified, functions that return type "int"
157 * return 0 on success, and non-0 value on error.
161 * Allocate a new camera_metadata structure, with some initial space for entries
162 * and extra data. The entry_capacity is measured in entry counts, and
163 * data_capacity in bytes. The resulting structure is all contiguous in memory,
164 * and can be freed with free_camera_metadata().
167 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
168 size_t data_capacity);
171 * Get the required alignment of a packet of camera metadata, which is the
172 * maximal alignment of the embedded camera_metadata, camera_metadata_buffer_entry,
173 * and camera_metadata_data.
176 size_t get_camera_metadata_alignment();
179 * Allocate a new camera_metadata structure of size src_size. Copy the data,
180 * ignoring alignment, and then attempt validation. If validation
181 * fails, free the memory and return NULL. Otherwise return the pointer.
183 * The resulting pointer can be freed with free_camera_metadata().
186 camera_metadata_t *allocate_copy_camera_metadata_checked(
187 const camera_metadata_t *src,
191 * Place a camera metadata structure into an existing buffer. Returns NULL if
192 * the buffer is too small for the requested number of reserved entries and
193 * bytes of data. The entry_capacity is measured in entry counts, and
194 * data_capacity in bytes. If the buffer is larger than the required space,
195 * unused space will be left at the end. If successful, returns a pointer to the
196 * metadata header placed at the start of the buffer. It is the caller's
197 * responsibility to free the original buffer; do not call
198 * free_camera_metadata() with the returned pointer.
201 camera_metadata_t *place_camera_metadata(void *dst, size_t dst_size,
202 size_t entry_capacity,
203 size_t data_capacity);
206 * Free a camera_metadata structure. Should only be used with structures
207 * allocated with allocate_camera_metadata().
210 void free_camera_metadata(camera_metadata_t *metadata);
213 * Calculate the buffer size needed for a metadata structure of entry_count
214 * metadata entries, needing a total of data_count bytes of extra data storage.
217 size_t calculate_camera_metadata_size(size_t entry_count,
221 * Get current size of entire metadata structure in bytes, including reserved
225 size_t get_camera_metadata_size(const camera_metadata_t *metadata);
228 * Get size of entire metadata buffer in bytes, not including reserved but
229 * unused space. This is the amount of space needed by copy_camera_metadata for
233 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata);
236 * Get the current number of entries in the metadata packet.
238 * metadata packet must be valid, which can be checked before the call with
239 * validate_camera_metadata_structure().
242 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata);
245 * Get the maximum number of entries that could fit in the metadata packet.
248 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata);
251 * Get the current count of bytes used for value storage in the metadata packet.
254 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata);
257 * Get the maximum count of bytes that could be used for value storage in the
261 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata);
264 * Copy a metadata structure to a memory buffer, compacting it along the
265 * way. That is, in the copied structure, entry_count == entry_capacity, and
266 * data_count == data_capacity.
268 * If dst_size > get_camera_metadata_compact_size(), the unused bytes are at the
269 * end of the buffer. If dst_size < get_camera_metadata_compact_size(), returns
270 * NULL. Otherwise returns a pointer to the metadata structure header placed at
273 * Since the buffer was not allocated by allocate_camera_metadata, the caller is
274 * responsible for freeing the underlying buffer when needed; do not call
275 * free_camera_metadata.
278 camera_metadata_t *copy_camera_metadata(void *dst, size_t dst_size,
279 const camera_metadata_t *src);
282 // Non-zero return values for validate_camera_metadata_structure
284 CAMERA_METADATA_VALIDATION_ERROR = 1,
285 CAMERA_METADATA_VALIDATION_SHIFTED = 2,
289 * Validate that a metadata is structurally sane. That is, its internal
290 * state is such that we won't get buffer overflows or run into other
291 * 'impossible' issues when calling the other API functions.
293 * This is useful in particular after copying the binary metadata blob
294 * from an untrusted source, since passing this check means the data is at least
297 * The expected_size argument is optional.
299 * Returns 0: on success
300 * CAMERA_METADATA_VALIDATION_ERROR: on error
301 * CAMERA_METADATA_VALIDATION_SHIFTED: when the data is not properly aligned, but can be
302 * used as input of clone_camera_metadata and the returned metadata will be valid.
306 int validate_camera_metadata_structure(const camera_metadata_t *metadata,
307 const size_t *expected_size);
310 * Append camera metadata in src to an existing metadata structure in dst. This
311 * does not resize the destination structure, so if it is too small, a non-zero
312 * value is returned. On success, 0 is returned. Appending onto a sorted
313 * structure results in a non-sorted combined structure.
316 int append_camera_metadata(camera_metadata_t *dst, const camera_metadata_t *src);
319 * Clone an existing metadata buffer, compacting along the way. This is
320 * equivalent to allocating a new buffer of the minimum needed size, then
321 * appending the buffer to be cloned into the new buffer. The resulting buffer
322 * can be freed with free_camera_metadata(). Returns NULL if cloning failed.
325 camera_metadata_t *clone_camera_metadata(const camera_metadata_t *src);
328 * Calculate the number of bytes of extra data a given metadata entry will take
329 * up. That is, if entry of 'type' with a payload of 'data_count' values is
330 * added, how much will the value returned by get_camera_metadata_data_count()
331 * be increased? This value may be zero, if no extra data storage is needed.
334 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
338 * Add a metadata entry to a metadata structure. Returns 0 if the addition
339 * succeeded. Returns a non-zero value if there is insufficient reserved space
340 * left to add the entry, or if the tag is unknown. data_count is the number of
341 * entries in the data array of the tag's type, not a count of
342 * bytes. Vendor-defined tags can not be added using this method, unless
343 * set_vendor_tag_query_ops() has been called first. Entries are always added to
344 * the end of the structure (highest index), so after addition, a
345 * previously-sorted array will be marked as unsorted.
347 * Returns 0 on success. A non-0 value is returned on error.
350 int add_camera_metadata_entry(camera_metadata_t *dst,
356 * Sort the metadata buffer for fast searching. If already marked as sorted,
357 * does nothing. Adding or appending entries to the buffer will place the buffer
358 * back into an unsorted state.
360 * Returns 0 on success. A non-0 value is returned on error.
363 int sort_camera_metadata(camera_metadata_t *dst);
366 * Get metadata entry at position index in the metadata buffer.
367 * Index must be less than entry count, which is returned by
368 * get_camera_metadata_entry_count().
370 * src and index are inputs; the passed-in entry is updated with the details of
371 * the entry. The data pointer points to the real data in the buffer, and can be
372 * updated as long as the data count does not change.
374 * Returns 0 on success. A non-0 value is returned on error.
377 int get_camera_metadata_entry(camera_metadata_t *src,
379 camera_metadata_entry_t *entry);
382 * Get metadata entry at position index, but disallow editing the data.
385 int get_camera_metadata_ro_entry(const camera_metadata_t *src,
387 camera_metadata_ro_entry_t *entry);
390 * Find an entry with given tag value. If not found, returns -ENOENT. Otherwise,
391 * returns entry contents like get_camera_metadata_entry.
393 * If multiple entries with the same tag exist, does not have any guarantees on
394 * which is returned. To speed up searching for tags, sort the metadata
395 * structure first by calling sort_camera_metadata().
398 int find_camera_metadata_entry(camera_metadata_t *src,
400 camera_metadata_entry_t *entry);
403 * Find an entry with given tag value, but disallow editing the data
406 int find_camera_metadata_ro_entry(const camera_metadata_t *src,
408 camera_metadata_ro_entry_t *entry);
411 * Delete an entry at given index. This is an expensive operation, since it
412 * requires repacking entries and possibly entry data. This also invalidates any
413 * existing camera_metadata_entry.data pointers to this buffer. Sorting is
417 int delete_camera_metadata_entry(camera_metadata_t *dst,
421 * Updates a metadata entry with new data. If the data size is changing, may
422 * need to adjust the data array, making this an O(N) operation. If the data
423 * size is the same or still fits in the entry space, this is O(1). Maintains
424 * sorting, but invalidates camera_metadata_entry instances that point to the
425 * updated entry. If a non-NULL value is passed in to entry, the entry structure
426 * is updated to match the new buffer state. Returns a non-zero value if there
427 * is no room for the new data in the buffer.
430 int update_camera_metadata_entry(camera_metadata_t *dst,
434 camera_metadata_entry_t *updated_entry);
437 * Retrieve human-readable name of section the tag is in. Returns NULL if
438 * no such tag is defined. Returns NULL for tags in the vendor section, unless
439 * set_vendor_tag_query_ops() has been used.
442 const char *get_camera_metadata_section_name(uint32_t tag);
445 * Retrieve human-readable name of tag (not including section). Returns NULL if
446 * no such tag is defined. Returns NULL for tags in the vendor section, unless
447 * set_vendor_tag_query_ops() has been used.
450 const char *get_camera_metadata_tag_name(uint32_t tag);
453 * Retrieve the type of a tag. Returns -1 if no such tag is defined. Returns -1
454 * for tags in the vendor section, unless set_vendor_tag_query_ops() has been
458 int get_camera_metadata_tag_type(uint32_t tag);
461 * Retrieve human-readable name of section the tag is in. Returns NULL if
462 * no such tag is defined.
465 const char *get_local_camera_metadata_section_name(uint32_t tag,
466 const camera_metadata_t *meta);
469 * Retrieve human-readable name of tag (not including section). Returns NULL if
470 * no such tag is defined.
473 const char *get_local_camera_metadata_tag_name(uint32_t tag,
474 const camera_metadata_t *meta);
477 * Retrieve the type of a tag. Returns -1 if no such tag is defined.
480 int get_local_camera_metadata_tag_type(uint32_t tag,
481 const camera_metadata_t *meta);
484 * Set up vendor-specific tag query methods. These are needed to properly add
485 * entries with vendor-specified tags and to use the
486 * get_camera_metadata_section_name, _tag_name, and _tag_type methods with
487 * vendor tags. Returns 0 on success.
489 * **DEPRECATED** - Please use vendor_tag_ops defined in camera_vendor_tags.h
492 typedef struct vendor_tag_query_ops vendor_tag_query_ops_t;
493 struct vendor_tag_query_ops {
495 * Get vendor section name for a vendor-specified entry tag. Only called for
496 * tags >= 0x80000000. The section name must start with the name of the
497 * vendor in the Java package style. For example, CameraZoom inc must prefix
498 * their sections with "com.camerazoom." Must return NULL if the tag is
499 * outside the bounds of vendor-defined sections.
501 const char *(*get_camera_vendor_section_name)(
502 const vendor_tag_query_ops_t *v,
505 * Get tag name for a vendor-specified entry tag. Only called for tags >=
506 * 0x80000000. Must return NULL if the tag is outside the bounds of
507 * vendor-defined sections.
509 const char *(*get_camera_vendor_tag_name)(
510 const vendor_tag_query_ops_t *v,
513 * Get tag type for a vendor-specified entry tag. Only called for tags >=
514 * 0x80000000. Must return -1 if the tag is outside the bounds of
515 * vendor-defined sections.
517 int (*get_camera_vendor_tag_type)(
518 const vendor_tag_query_ops_t *v,
521 * Get the number of vendor tags supported on this platform. Used to
522 * calculate the size of buffer needed for holding the array of all tags
523 * returned by get_camera_vendor_tags().
525 int (*get_camera_vendor_tag_count)(
526 const vendor_tag_query_ops_t *v);
528 * Fill an array with all the supported vendor tags on this platform.
529 * get_camera_vendor_tag_count() returns the number of tags supported, and
530 * tag_array should be allocated with enough space to hold all of the tags.
532 void (*get_camera_vendor_tags)(
533 const vendor_tag_query_ops_t *v,
534 uint32_t *tag_array);
538 * **DEPRECATED** - This should only be used by the camera framework. Camera
539 * metadata will transition to using vendor_tag_ops defined in
540 * camera_vendor_tags.h instead.
543 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops);
546 * Print fields in the metadata to the log.
547 * verbosity = 0: Only tag entry information
548 * verbosity = 1: Tag entry information plus at most 16 data values
549 * verbosity = 2: All information
552 void dump_camera_metadata(const camera_metadata_t *metadata,
557 * Print fields in the metadata to the log; adds indentation parameter, which
558 * specifies the number of spaces to insert before each line of the dump
561 void dump_indented_camera_metadata(const camera_metadata_t *metadata,
567 * Prints the specified tag value as a string. Only works for enum tags.
568 * Returns 0 on success, -1 on failure.
571 int camera_metadata_enum_snprint(uint32_t tag,