Home | History | Annotate | Download | only in src
      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 #define _GNU_SOURCE // for fdprintf
     17 #include <system/camera_metadata.h>
     18 #include <cutils/log.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 #include <errno.h>
     22 
     23 #define OK         0
     24 #define ERROR      1
     25 #define NOT_FOUND -ENOENT
     26 /**
     27  * A single metadata entry, storing an array of values of a given type. If the
     28  * array is no larger than 4 bytes in size, it is stored in the data.value[]
     29  * array; otherwise, it can found in the parent's data array at index
     30  * data.offset.
     31  */
     32 typedef struct camera_metadata_entry {
     33     uint32_t tag;
     34     size_t   count;
     35     union {
     36         size_t  offset;
     37         uint8_t value[4];
     38     } data;
     39     uint8_t  type;
     40     uint8_t  reserved[3];
     41 } __attribute__((packed)) camera_metadata_entry_t;
     42 
     43 /**
     44  * A packet of metadata. This is a list of entries, each of which may point to
     45  * its values stored at an offset in data.
     46  *
     47  * It is assumed by the utility functions that the memory layout of the packet
     48  * is as follows:
     49  *
     50  *   |----------------------------------------|
     51  *   | camera_metadata_t                      |
     52  *   |                                        |
     53  *   |----------------------------------------|
     54  *   | reserved for future expansion          |
     55  *   |----------------------------------------|
     56  *   | camera_metadata_entry_t #0             |
     57  *   |----------------------------------------|
     58  *   | ....                                   |
     59  *   |----------------------------------------|
     60  *   | camera_metadata_entry_t #entry_count-1 |
     61  *   |----------------------------------------|
     62  *   | free space for                         |
     63  *   | (entry_capacity-entry_count) entries   |
     64  *   |----------------------------------------|
     65  *   | start of camera_metadata.data          |
     66  *   |                                        |
     67  *   |----------------------------------------|
     68  *   | free space for                         |
     69  *   | (data_capacity-data_count) bytes       |
     70  *   |----------------------------------------|
     71  *
     72  * With the total length of the whole packet being camera_metadata.size bytes.
     73  *
     74  * In short, the entries and data are contiguous in memory after the metadata
     75  * header.
     76  */
     77 struct camera_metadata {
     78     size_t                   size;
     79     uint32_t                 version;
     80     uint32_t                 flags;
     81     size_t                   entry_count;
     82     size_t                   entry_capacity;
     83     camera_metadata_entry_t *entries;
     84     size_t                   data_count;
     85     size_t                   data_capacity;
     86     uint8_t                 *data;
     87     uint8_t                  reserved[0];
     88 };
     89 
     90 /** Versioning information */
     91 #define CURRENT_METADATA_VERSION 1
     92 
     93 /** Flag definitions */
     94 #define FLAG_SORTED 0x00000001
     95 
     96 /** Tag information */
     97 
     98 typedef struct tag_info {
     99     const char *tag_name;
    100     uint8_t     tag_type;
    101 } tag_info_t;
    102 
    103 #include "camera_metadata_tag_info.c"
    104 
    105 size_t camera_metadata_type_size[NUM_TYPES] = {
    106     [TYPE_BYTE]     = sizeof(uint8_t),
    107     [TYPE_INT32]    = sizeof(int32_t),
    108     [TYPE_FLOAT]    = sizeof(float),
    109     [TYPE_INT64]    = sizeof(int64_t),
    110     [TYPE_DOUBLE]   = sizeof(double),
    111     [TYPE_RATIONAL] = sizeof(camera_metadata_rational_t)
    112 };
    113 
    114 char *camera_metadata_type_names[NUM_TYPES] = {
    115     [TYPE_BYTE]     = "byte",
    116     [TYPE_INT32]    = "int32",
    117     [TYPE_FLOAT]    = "float",
    118     [TYPE_INT64]    = "int64",
    119     [TYPE_RATIONAL] = "rational"
    120 };
    121 
    122 camera_metadata_t *allocate_camera_metadata(size_t entry_capacity,
    123                                             size_t data_capacity) {
    124     size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
    125                                                           data_capacity);
    126     void *buffer = malloc(memory_needed);
    127     return place_camera_metadata(buffer, memory_needed,
    128                                  entry_capacity,
    129                                  data_capacity);
    130 }
    131 
    132 camera_metadata_t *place_camera_metadata(void *dst,
    133                                          size_t dst_size,
    134                                          size_t entry_capacity,
    135                                          size_t data_capacity) {
    136     if (dst == NULL) return NULL;
    137     if (entry_capacity == 0) return NULL;
    138 
    139     size_t memory_needed = calculate_camera_metadata_size(entry_capacity,
    140                                                           data_capacity);
    141     if (memory_needed > dst_size) return NULL;
    142 
    143     camera_metadata_t *metadata = (camera_metadata_t*)dst;
    144     metadata->version = CURRENT_METADATA_VERSION;
    145     metadata->flags = 0;
    146     metadata->entry_count = 0;
    147     metadata->entry_capacity = entry_capacity;
    148     metadata->entries = (camera_metadata_entry_t*)(metadata + 1);
    149     metadata->data_count = 0;
    150     metadata->data_capacity = data_capacity;
    151     metadata->size = memory_needed;
    152     if (metadata->data_capacity != 0) {
    153         metadata->data =
    154                 (uint8_t*)(metadata->entries + metadata->entry_capacity);
    155     } else {
    156         metadata->data = NULL;
    157     }
    158 
    159     return metadata;
    160 }
    161 void free_camera_metadata(camera_metadata_t *metadata) {
    162     free(metadata);
    163 }
    164 
    165 size_t calculate_camera_metadata_size(size_t entry_count,
    166                                       size_t data_count) {
    167     size_t memory_needed = sizeof(camera_metadata_t);
    168     memory_needed += sizeof(camera_metadata_entry_t[entry_count]);
    169     memory_needed += sizeof(uint8_t[data_count]);
    170     return memory_needed;
    171 }
    172 
    173 size_t get_camera_metadata_size(const camera_metadata_t *metadata) {
    174     if (metadata == NULL) return ERROR;
    175 
    176     return metadata->size;
    177 }
    178 
    179 size_t get_camera_metadata_compact_size(const camera_metadata_t *metadata) {
    180     if (metadata == NULL) return ERROR;
    181 
    182     ptrdiff_t reserved_size = metadata->size -
    183             calculate_camera_metadata_size(metadata->entry_capacity,
    184                                            metadata->data_capacity);
    185 
    186     return calculate_camera_metadata_size(metadata->entry_count,
    187                                           metadata->data_count) + reserved_size;
    188 }
    189 
    190 size_t get_camera_metadata_entry_count(const camera_metadata_t *metadata) {
    191     return metadata->entry_count;
    192 }
    193 
    194 size_t get_camera_metadata_entry_capacity(const camera_metadata_t *metadata) {
    195     return metadata->entry_capacity;
    196 }
    197 
    198 size_t get_camera_metadata_data_count(const camera_metadata_t *metadata) {
    199     return metadata->data_count;
    200 }
    201 
    202 size_t get_camera_metadata_data_capacity(const camera_metadata_t *metadata) {
    203     return metadata->data_capacity;
    204 }
    205 
    206 camera_metadata_t* copy_camera_metadata(void *dst, size_t dst_size,
    207         const camera_metadata_t *src) {
    208     size_t memory_needed = get_camera_metadata_compact_size(src);
    209 
    210     if (dst == NULL) return NULL;
    211     if (dst_size < memory_needed) return NULL;
    212 
    213     // If copying a newer version of the structure, there may be additional
    214     // header fields we don't know about but need to copy
    215     ptrdiff_t reserved_size = src->size -
    216             calculate_camera_metadata_size(src->entry_capacity,
    217                                            src->data_capacity);
    218 
    219     camera_metadata_t *metadata = (camera_metadata_t*)dst;
    220     metadata->version = CURRENT_METADATA_VERSION;
    221     metadata->flags = src->flags;
    222     metadata->entry_count = src->entry_count;
    223     metadata->entry_capacity = src->entry_count;
    224     metadata->entries = (camera_metadata_entry_t*)
    225              ((uint8_t *)(metadata + 1) + reserved_size);
    226     metadata->data_count = src->data_count;
    227     metadata->data_capacity = src->data_count;
    228     metadata->data = (uint8_t *)(metadata->entries + metadata->entry_capacity);
    229     metadata->size = memory_needed;
    230 
    231     if (reserved_size > 0) {
    232         memcpy(metadata->reserved, src->reserved, reserved_size);
    233     }
    234     memcpy(metadata->entries, src->entries,
    235             sizeof(camera_metadata_entry_t[metadata->entry_count]));
    236     memcpy(metadata->data, src->data,
    237             sizeof(uint8_t[metadata->data_count]));
    238 
    239     return metadata;
    240 }
    241 
    242 int append_camera_metadata(camera_metadata_t *dst,
    243         const camera_metadata_t *src) {
    244     if (dst == NULL || src == NULL ) return ERROR;
    245 
    246     if (dst->entry_capacity < src->entry_count + dst->entry_count) return ERROR;
    247     if (dst->data_capacity < src->data_count + dst->data_count) return ERROR;
    248 
    249     memcpy(dst->entries + dst->entry_count, src->entries,
    250             sizeof(camera_metadata_entry_t[src->entry_count]));
    251     memcpy(dst->data + dst->data_count, src->data,
    252             sizeof(uint8_t[src->data_count]));
    253     if (dst->data_count != 0) {
    254         unsigned int i;
    255         for (i = dst->entry_count;
    256              i < dst->entry_count + src->entry_count;
    257              i++) {
    258             camera_metadata_entry_t *entry = dst->entries + i;
    259             if ( camera_metadata_type_size[entry->type] * entry->count > 4 ) {
    260                 entry->data.offset += dst->data_count;
    261             }
    262         }
    263     }
    264     dst->entry_count += src->entry_count;
    265     dst->data_count += src->data_count;
    266     dst->flags &= ~FLAG_SORTED;
    267 
    268     return OK;
    269 }
    270 
    271 size_t calculate_camera_metadata_entry_data_size(uint8_t type,
    272         size_t data_count) {
    273     if (type >= NUM_TYPES) return 0;
    274     size_t data_bytes = data_count *
    275             camera_metadata_type_size[type];
    276     return data_bytes <= 4 ? 0 : data_bytes;
    277 }
    278 
    279 static int add_camera_metadata_entry_raw(camera_metadata_t *dst,
    280         uint32_t tag,
    281         uint8_t  type,
    282         const void *data,
    283         size_t data_count) {
    284 
    285     if (dst == NULL) return ERROR;
    286     if (dst->entry_count == dst->entry_capacity) return ERROR;
    287     if (data == NULL) return ERROR;
    288 
    289     size_t data_bytes =
    290             calculate_camera_metadata_entry_data_size(type, data_count);
    291 
    292     camera_metadata_entry_t *entry = dst->entries + dst->entry_count;
    293     entry->tag = tag;
    294     entry->type = type;
    295     entry->count = data_count;
    296 
    297     if (data_bytes == 0) {
    298         memcpy(entry->data.value, data,
    299                 data_count * camera_metadata_type_size[type] );
    300     } else {
    301         entry->data.offset = dst->data_count;
    302         memcpy(dst->data + entry->data.offset, data, data_bytes);
    303         dst->data_count += data_bytes;
    304     }
    305     dst->entry_count++;
    306     dst->flags &= ~FLAG_SORTED;
    307     return OK;
    308 }
    309 
    310 int add_camera_metadata_entry(camera_metadata_t *dst,
    311         uint32_t tag,
    312         const void *data,
    313         size_t data_count) {
    314 
    315     int type = get_camera_metadata_tag_type(tag);
    316     if (type == -1) {
    317         ALOGE("%s: Unknown tag %04x.", __FUNCTION__, tag);
    318         return ERROR;
    319     }
    320 
    321     return add_camera_metadata_entry_raw(dst,
    322             tag,
    323             type,
    324             data,
    325             data_count);
    326 }
    327 
    328 static int compare_entry_tags(const void *p1, const void *p2) {
    329     uint32_t tag1 = ((camera_metadata_entry_t*)p1)->tag;
    330     uint32_t tag2 = ((camera_metadata_entry_t*)p2)->tag;
    331     return  tag1 < tag2 ? -1 :
    332             tag1 == tag2 ? 0 :
    333             1;
    334 }
    335 
    336 int sort_camera_metadata(camera_metadata_t *dst) {
    337     if (dst == NULL) return ERROR;
    338     if (dst->flags & FLAG_SORTED) return OK;
    339 
    340     qsort(dst->entries, dst->entry_count,
    341             sizeof(camera_metadata_entry_t),
    342             compare_entry_tags);
    343     dst->flags |= FLAG_SORTED;
    344 
    345     return OK;
    346 }
    347 
    348 int get_camera_metadata_entry(camera_metadata_t *src,
    349         uint32_t index,
    350         uint32_t *tag,
    351         uint8_t *type,
    352         void **data,
    353         size_t *data_count) {
    354     if (src == NULL ) return ERROR;
    355     if (index >= src->entry_count) return ERROR;
    356 
    357     camera_metadata_entry_t *entry = src->entries + index;
    358 
    359     if (tag != NULL) *tag = entry->tag;
    360     if (type != NULL) *type = entry->type;
    361     if (data_count != NULL) *data_count = entry->count;
    362     if (data != NULL) {
    363         if (entry->count * camera_metadata_type_size[entry->type] > 4) {
    364             *data = src->data + entry->data.offset;
    365         } else {
    366             *data = entry->data.value;
    367         }
    368     }
    369     return OK;
    370 }
    371 
    372 int find_camera_metadata_entry(camera_metadata_t *src,
    373         uint32_t tag,
    374         uint8_t *type,
    375         void **data,
    376         size_t *data_count) {
    377     if (src == NULL) return ERROR;
    378 
    379     camera_metadata_entry_t *entry = NULL;
    380     if (src->flags & FLAG_SORTED) {
    381         // Sorted entries, do a binary search
    382         camera_metadata_entry_t key;
    383         key.tag = tag;
    384         entry = bsearch(&key,
    385                 src->entries,
    386                 src->entry_count,
    387                 sizeof(camera_metadata_entry_t),
    388                 compare_entry_tags);
    389     } else {
    390         // Not sorted, linear search
    391         unsigned int i;
    392         for (i = 0; i < src->entry_count; i++) {
    393             if (src->entries[i].tag == tag) {
    394                 entry = src->entries + i;
    395                 break;
    396             }
    397         }
    398     }
    399     if (entry == NULL) return NOT_FOUND;
    400 
    401     if (type != NULL) *type = entry->type;
    402     if (data_count != NULL) *data_count = entry->count;
    403     if (data != NULL) {
    404         if (entry->count * camera_metadata_type_size[entry->type] > 4) {
    405             *data = src->data + entry->data.offset;
    406         } else {
    407             *data = entry->data.value;
    408         }
    409     }
    410     return OK;
    411 }
    412 
    413 static const vendor_tag_query_ops_t *vendor_tag_ops = NULL;
    414 
    415 const char *get_camera_metadata_section_name(uint32_t tag) {
    416     uint32_t tag_section = tag >> 16;
    417     if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
    418         return vendor_tag_ops->get_camera_vendor_section_name(
    419             vendor_tag_ops,
    420             tag);
    421     }
    422     if (tag_section >= ANDROID_SECTION_COUNT) {
    423         return NULL;
    424     }
    425     return camera_metadata_section_names[tag_section];
    426 }
    427 
    428 const char *get_camera_metadata_tag_name(uint32_t tag) {
    429     uint32_t tag_section = tag >> 16;
    430     if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
    431         return vendor_tag_ops->get_camera_vendor_tag_name(
    432             vendor_tag_ops,
    433             tag);
    434     }
    435     if (tag_section >= ANDROID_SECTION_COUNT ||
    436         tag >= camera_metadata_section_bounds[tag_section][1] ) {
    437         return NULL;
    438     }
    439     uint32_t tag_index = tag & 0xFFFF;
    440     return tag_info[tag_section][tag_index].tag_name;
    441 }
    442 
    443 int get_camera_metadata_tag_type(uint32_t tag) {
    444     uint32_t tag_section = tag >> 16;
    445     if (tag_section >= VENDOR_SECTION && vendor_tag_ops != NULL) {
    446         return vendor_tag_ops->get_camera_vendor_tag_type(
    447             vendor_tag_ops,
    448             tag);
    449     }
    450     if (tag_section >= ANDROID_SECTION_COUNT ||
    451             tag >= camera_metadata_section_bounds[tag_section][1] ) {
    452         return -1;
    453     }
    454     uint32_t tag_index = tag & 0xFFFF;
    455     return tag_info[tag_section][tag_index].tag_type;
    456 }
    457 
    458 int set_camera_metadata_vendor_tag_ops(const vendor_tag_query_ops_t *query_ops) {
    459     vendor_tag_ops = query_ops;
    460     return OK;
    461 }
    462 
    463 static void print_data(int fd, const uint8_t *data_ptr, int type, int count);
    464 
    465 void dump_camera_metadata(const camera_metadata_t *metadata,
    466         int fd,
    467         int verbosity) {
    468     if (metadata == NULL) {
    469         ALOGE("%s: Metadata is null.", __FUNCTION__);
    470         return;
    471     }
    472     unsigned int i;
    473     fdprintf(fd,
    474             "Dumping camera metadata array. %d entries, "
    475             "%d bytes of extra data.\n",
    476             metadata->entry_count, metadata->data_count);
    477     fdprintf(fd, "  (%d entries and %d bytes data reserved)\n",
    478             metadata->entry_capacity, metadata->data_capacity);
    479     fdprintf(fd, "  Version: %d, Flags: %08x\n",
    480             metadata->version, metadata->flags);
    481     for (i=0; i < metadata->entry_count; i++) {
    482         camera_metadata_entry_t *entry = metadata->entries + i;
    483 
    484         const char *tag_name, *tag_section;
    485         tag_section = get_camera_metadata_section_name(entry->tag);
    486         if (tag_section == NULL) {
    487             tag_section = "unknownSection";
    488         }
    489         tag_name = get_camera_metadata_tag_name(entry->tag);
    490         if (tag_name == NULL) {
    491             tag_name = "unknownTag";
    492         }
    493         const char *type_name;
    494         if (entry->type >= NUM_TYPES) {
    495             type_name = "unknown";
    496         } else {
    497             type_name = camera_metadata_type_names[entry->type];
    498         }
    499         fdprintf(fd, "Tag: %s.%s (%05x): %s[%d]\n",
    500              tag_section,
    501              tag_name,
    502              entry->tag,
    503              type_name,
    504              entry->count);
    505 
    506         if (verbosity < 1) continue;
    507 
    508         if (entry->type >= NUM_TYPES) continue;
    509 
    510         size_t type_size = camera_metadata_type_size[entry->type];
    511         uint8_t *data_ptr;
    512         if ( type_size * entry->count > 4 ) {
    513             if (entry->data.offset >= metadata->data_count) {
    514                 ALOGE("%s: Malformed entry data offset: %d (max %d)",
    515                         __FUNCTION__,
    516                         entry->data.offset,
    517                         metadata->data_count);
    518                 continue;
    519             }
    520             data_ptr = metadata->data + entry->data.offset;
    521         } else {
    522             data_ptr = entry->data.value;
    523         }
    524         int count = entry->count;
    525         if (verbosity < 2 && count > 16) count = 16;
    526 
    527         print_data(fd, data_ptr, entry->type, count);
    528     }
    529 }
    530 
    531 static void print_data(int fd, const uint8_t *data_ptr, int type, int count) {
    532     static int values_per_line[NUM_TYPES] = {
    533         [TYPE_BYTE]     = 16,
    534         [TYPE_INT32]    = 4,
    535         [TYPE_FLOAT]    = 8,
    536         [TYPE_INT64]    = 2,
    537         [TYPE_DOUBLE]   = 4,
    538         [TYPE_RATIONAL] = 2,
    539     };
    540     size_t type_size = camera_metadata_type_size[type];
    541 
    542     int lines = count / values_per_line[type];
    543     if (count % values_per_line[type] != 0) lines++;
    544 
    545     int index = 0;
    546     int j, k;
    547     for (j = 0; j < lines; j++) {
    548         fdprintf(fd, " [");
    549         for (k = 0;
    550              k < values_per_line[type] && count > 0;
    551              k++, count--, index += type_size) {
    552 
    553             switch (type) {
    554                 case TYPE_BYTE:
    555                     fdprintf(fd, "%hhu ",
    556                             *(data_ptr + index));
    557                     break;
    558                 case TYPE_INT32:
    559                     fdprintf(fd, "%d ",
    560                             *(int32_t*)(data_ptr + index));
    561                     break;
    562                 case TYPE_FLOAT:
    563                     fdprintf(fd, "%0.2f ",
    564                             *(float*)(data_ptr + index));
    565                     break;
    566                 case TYPE_INT64:
    567                     fdprintf(fd, "%lld ",
    568                             *(int64_t*)(data_ptr + index));
    569                     break;
    570                 case TYPE_DOUBLE:
    571                     fdprintf(fd, "%0.2f ",
    572                             *(float*)(data_ptr + index));
    573                     break;
    574                 case TYPE_RATIONAL: {
    575                     int32_t numerator = *(int32_t*)(data_ptr + index);
    576                     int32_t denominator = *(int32_t*)(data_ptr + index + 4);
    577                     fdprintf(fd, "(%d / %d) ",
    578                             numerator, denominator);
    579                     break;
    580                 }
    581                 default:
    582                     fdprintf(fd, "??? ");
    583             }
    584         }
    585         fdprintf(fd, "]\n");
    586     }
    587 }
    588