camera_metadata.h revision f5fb8a5516876c5a48cc44b4f1c99504e0b9f245
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SYSTEM_CORE_INCLUDE_ANDROID_CAMERA_METADATA_H
18#define SYSTEM_CORE_INCLUDE_ANDROID_CAMERA_METADATA_H
19
20#include <string.h>
21#include <stdint.h>
22#include <cutils/compiler.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28/**
29 * Tag hierarchy and enum definitions for camera_metadata_entry
30 * =============================================================================
31 */
32
33/**
34 * Main enum definitions are in a separate file to make it easy to
35 * maintain
36 */
37#include "camera_metadata_tags.h"
38
39/**
40 * Enum range for each top-level category
41 */
42ANDROID_API
43extern unsigned int camera_metadata_section_bounds[ANDROID_SECTION_COUNT][2];
44ANDROID_API
45extern const char *camera_metadata_section_names[ANDROID_SECTION_COUNT];
46
47/**
48 * Type definitions for camera_metadata_entry
49 * =============================================================================
50 */
51enum {
52    // Unsigned 8-bit integer (uint8_t)
53    TYPE_BYTE = 0,
54    // Signed 32-bit integer (int32_t)
55    TYPE_INT32 = 1,
56    // 32-bit float (float)
57    TYPE_FLOAT = 2,
58    // Signed 64-bit integer (int64_t)
59    TYPE_INT64 = 3,
60    // 64-bit float (double)
61    TYPE_DOUBLE = 4,
62    // A 64-bit fraction (camera_metadata_rational_t)
63    TYPE_RATIONAL = 5,
64    // Number of type fields
65    NUM_TYPES
66};
67
68typedef struct camera_metadata_rational {
69    int32_t numerator;
70    int32_t denominator;
71} camera_metadata_rational_t;
72
73/**
74 * A reference to a metadata entry in a buffer.
75 *
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.
79 */
80typedef struct camera_metadata_entry {
81    size_t   index;
82    uint32_t tag;
83    uint8_t  type;
84    size_t   count;
85    union {
86        uint8_t *u8;
87        int32_t *i32;
88        float   *f;
89        int64_t *i64;
90        double  *d;
91        camera_metadata_rational_t *r;
92    } data;
93} camera_metadata_entry_t;
94
95/**
96 * Size in bytes of each entry type
97 */
98ANDROID_API
99extern size_t camera_metadata_type_size[NUM_TYPES];
100
101/**
102 * Main definitions for the metadata entry and array structures
103 * =============================================================================
104 */
105
106/**
107 * A packet of metadata. This is a list of metadata entries, each of which has
108 * an integer tag to identify its meaning, 'type' and 'count' field, and the
109 * data, which contains a 'count' number of entries of type 'type'. The packet
110 * has a fixed capacity for entries and for extra data.  A new entry uses up one
111 * entry slot, and possibly some amount of data capacity; the function
112 * calculate_camera_metadata_entry_data_size() provides the amount of data
113 * capacity that would be used up by an entry.
114 *
115 * Entries are not sorted by default, and are not forced to be unique - multiple
116 * entries with the same tag are allowed. The packet will not dynamically resize
117 * when full.
118 *
119 * The packet is contiguous in memory, with size in bytes given by
120 * get_camera_metadata_size(). Therefore, it can be copied safely with memcpy()
121 * to a buffer of sufficient size. The copy_camera_metadata() function is
122 * intended for eliminating unused capacity in the destination packet.
123 */
124struct camera_metadata;
125typedef struct camera_metadata camera_metadata_t;
126
127/**
128 * Functions for manipulating camera metadata
129 * =============================================================================
130 */
131
132/**
133 * Allocate a new camera_metadata structure, with some initial space for entries
134 * and extra data. The entry_capacity is measured in entry counts, and
135 * data_capacity in bytes. The resulting structure is all contiguous in memory,
136 * and can be freed with free_camera_metadata().
137 */
138ANDROID_API
139camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
140        size_t data_capacity);
141
142/**
143 * Place a camera metadata structure into an existing buffer. Returns NULL if
144 * the buffer is too small for the requested number of reserved entries and
145 * bytes of data. The entry_capacity is measured in entry counts, and
146 * data_capacity in bytes. If the buffer is larger than the required space,
147 * unused space will be left at the end. If successful, returns a pointer to the
148 * metadata header placed at the start of the buffer. It is the caller's
149 * responsibility to free the original buffer; do not call
150 * free_camera_metadata() with the returned pointer.
151 */
152ANDROID_API
153camera_metadata_t *place_camera_metadata(void *dst, size_t dst_size,
154        size_t entry_capacity,
155        size_t data_capacity);
156
157/**
158 * Free a camera_metadata structure. Should only be used with structures
159 * allocated with allocate_camera_metadata().
160 */
161ANDROID_API
162void free_camera_metadata(camera_metadata_t *metadata);
163
164/**
165 * Calculate the buffer size needed for a metadata structure of entry_count
166 * metadata entries, needing a total of data_count bytes of extra data storage.
167 */
168ANDROID_API
169size_t calculate_camera_metadata_size(size_t entry_count,
170        size_t data_count);
171
172/**
173 * Get current size of entire metadata structure in bytes, including reserved
174 * but unused space.
175 */
176ANDROID_API
177size_t get_camera_metadata_size(const camera_metadata_t *metadata);
178
179/**
180 * Get size of entire metadata buffer in bytes, not including reserved but
181 * unused space. This is the amount of space needed by copy_camera_metadata for
182 * its dst buffer.
183 */
184ANDROID_API
185size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata);
186
187/**
188 * Get the current number of entries in the metadata packet.
189 */
190ANDROID_API
191size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata);
192
193/**
194 * Get the maximum number of entries that could fit in the metadata packet.
195 */
196ANDROID_API
197size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata);
198
199/**
200 * Get the current count of bytes used for value storage in the metadata packet.
201 */
202ANDROID_API
203size_t get_camera_metadata_data_count(const camera_metadata_t *metadata);
204
205/**
206 * Get the maximum count of bytes that could be used for value storage in the
207 * metadata packet.
208 */
209ANDROID_API
210size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata);
211
212/**
213 * Copy a metadata structure to a memory buffer, compacting it along the
214 * way. That is, in the copied structure, entry_count == entry_capacity, and
215 * data_count == data_capacity.
216 *
217 * If dst_size > get_camera_metadata_compact_size(), the unused bytes are at the
218 * end of the buffer. If dst_size < get_camera_metadata_compact_size(), returns
219 * NULL. Otherwise returns a pointer to the metadata structure header placed at
220 * the start of dst.
221 *
222 * Since the buffer was not allocated by allocate_camera_metadata, the caller is
223 * responsible for freeing the underlying buffer when needed; do not call
224 * free_camera_metadata.
225 */
226ANDROID_API
227camera_metadata_t *copy_camera_metadata(void *dst, size_t dst_size,
228        const camera_metadata_t *src);
229
230/**
231 * Append camera metadata in src to an existing metadata structure in dst.  This
232 * does not resize the destination structure, so if it is too small, a non-zero
233 * value is returned. On success, 0 is returned. Appending onto a sorted
234 * structure results in a non-sorted combined structure.
235 */
236ANDROID_API
237int append_camera_metadata(camera_metadata_t *dst, const camera_metadata_t *src);
238
239/**
240 * Calculate the number of bytes of extra data a given metadata entry will take
241 * up. That is, if entry of 'type' with a payload of 'data_count' values is
242 * added, how much will the value returned by get_camera_metadata_data_count()
243 * be increased? This value may be zero, if no extra data storage is needed.
244 */
245ANDROID_API
246size_t calculate_camera_metadata_entry_data_size(uint8_t type,
247        size_t data_count);
248
249/**
250 * Add a metadata entry to a metadata structure. Returns 0 if the addition
251 * succeeded. Returns a non-zero value if there is insufficient reserved space
252 * left to add the entry, or if the tag is unknown.  data_count is the number of
253 * entries in the data array of the tag's type, not a count of
254 * bytes. Vendor-defined tags can not be added using this method, unless
255 * set_vendor_tag_query_ops() has been called first. Entries are always added to
256 * the end of the structure (highest index), so after addition, a
257 * previously-sorted array will be marked as unsorted.
258 */
259ANDROID_API
260int add_camera_metadata_entry(camera_metadata_t *dst,
261        uint32_t tag,
262        const void *data,
263        size_t data_count);
264
265/**
266 * Sort the metadata buffer for fast searching. If already marked as sorted,
267 * does nothing. Adding or appending entries to the buffer will place the buffer
268 * back into an unsorted state.
269 */
270ANDROID_API
271int sort_camera_metadata(camera_metadata_t *dst);
272
273/**
274 * Get metadata entry at position index in the metadata buffer.
275 *
276 * src and index are inputs; the passed-in entry is updated with the details of
277 * the entry. The data pointer points to the real data in the buffer, and can be
278 * updated as long as the data count does not change.
279 */
280ANDROID_API
281int get_camera_metadata_entry(camera_metadata_t *src,
282        size_t index,
283        camera_metadata_entry_t *entry);
284
285/**
286 * Find an entry with given tag value. If not found, returns -ENOENT. Otherwise,
287 * returns entry contents like get_camera_metadata_entry.
288 *
289 * If multiple entries with the same tag exist, does not have any guarantees on
290 * which is returned. To speed up searching for tags, sort the metadata
291 * structure first by calling sort_camera_metadata().
292 */
293ANDROID_API
294int find_camera_metadata_entry(camera_metadata_t *src,
295        uint32_t tag,
296        camera_metadata_entry_t *entry);
297
298/**
299 * Delete an entry at given index. This is an expensive operation, since it
300 * requires repacking entries and possibly entry data. This also invalidates any
301 * existing camera_metadata_entry.data pointers to this buffer. Sorting is
302 * maintained.
303 */
304ANDROID_API
305int delete_camera_metadata_entry(camera_metadata_t *dst,
306        size_t index);
307
308/**
309 * Updates a metadata entry with new data. If the data size is changing, may
310 * need to adjust the data array, making this an O(N) operation. If the data
311 * size is the same or still fits in the entry space, this is O(1). Maintains
312 * sorting, but invalidates camera_metadata_entry instances that point to the
313 * updated entry. If a non-NULL value is passed in to entry, the entry structure
314 * is updated to match the new buffer state.  Returns a non-zero value if there
315 * is no room for the new data in the buffer.
316 */
317ANDROID_API
318int update_camera_metadata_entry(camera_metadata_t *dst,
319        size_t index,
320        const void *data,
321        size_t data_count,
322        camera_metadata_entry_t *updated_entry);
323
324/**
325 * Retrieve human-readable name of section the tag is in. Returns NULL if
326 * no such tag is defined. Returns NULL for tags in the vendor section, unless
327 * set_vendor_tag_query_ops() has been used.
328 */
329ANDROID_API
330const char *get_camera_metadata_section_name(uint32_t tag);
331
332/**
333 * Retrieve human-readable name of tag (not including section). Returns NULL if
334 * no such tag is defined. Returns NULL for tags in the vendor section, unless
335 * set_vendor_tag_query_ops() has been used.
336 */
337ANDROID_API
338const char *get_camera_metadata_tag_name(uint32_t tag);
339
340/**
341 * Retrieve the type of a tag. Returns -1 if no such tag is defined. Returns -1
342 * for tags in the vendor section, unless set_vendor_tag_query_ops() has been
343 * used.
344 */
345ANDROID_API
346int get_camera_metadata_tag_type(uint32_t tag);
347
348/**
349 * Set up vendor-specific tag query methods. These are needed to properly add
350 * entries with vendor-specified tags and to use the
351 * get_camera_metadata_section_name, _tag_name, and _tag_type methods with
352 * vendor tags. Returns 0 on success.
353 */
354typedef struct vendor_tag_query_ops vendor_tag_query_ops_t;
355struct vendor_tag_query_ops {
356    /**
357     * Get vendor section name for a vendor-specified entry tag. Only called for
358     * tags >= 0x80000000. The section name must start with the name of the
359     * vendor in the Java package style. For example, CameraZoom inc must prefix
360     * their sections with "com.camerazoom." Must return NULL if the tag is
361     * outside the bounds of vendor-defined sections.
362     */
363    const char *(*get_camera_vendor_section_name)(
364        const vendor_tag_query_ops_t *v,
365        uint32_t tag);
366    /**
367     * Get tag name for a vendor-specified entry tag. Only called for tags >=
368     * 0x80000000. Must return NULL if the tag is outside the bounds of
369     * vendor-defined sections.
370     */
371    const char *(*get_camera_vendor_tag_name)(
372        const vendor_tag_query_ops_t *v,
373        uint32_t tag);
374    /**
375     * Get tag type for a vendor-specified entry tag. Only called for tags >=
376     * 0x80000000. Must return -1 if the tag is outside the bounds of
377     * vendor-defined sections.
378     */
379    int (*get_camera_vendor_tag_type)(
380        const vendor_tag_query_ops_t *v,
381        uint32_t tag);
382};
383
384ANDROID_API
385int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops);
386
387/**
388 * Print fields in the metadata to the log.
389 * verbosity = 0: Only tag entry information
390 * verbosity = 1: Tag entry information plus at most 16 data values
391 * verbosity = 2: All information
392 */
393ANDROID_API
394void dump_camera_metadata(const camera_metadata_t *metadata,
395        int fd,
396        int verbosity);
397
398#ifdef __cplusplus
399}
400#endif
401
402#endif
403