1 /* 2 * Copyright (C) 2015 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 //#define LOG_NDEBUG 0 18 #define LOG_TAG "Metadata" 19 20 #include <errno.h> 21 22 #include <log/log.h> 23 24 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) 25 #include <utils/Trace.h> 26 27 #include <system/camera_metadata.h> 28 29 #include "Metadata.h" 30 31 namespace usb_camera_hal { 32 33 Metadata::Metadata(): 34 mData(NULL) { 35 } 36 37 Metadata::~Metadata() { 38 replace(NULL); 39 } 40 41 void Metadata::replace(camera_metadata_t *m) { 42 if (m == mData) { 43 return; 44 } 45 if (mData) 46 free_camera_metadata(mData); 47 mData = m; 48 } 49 50 int Metadata::init(const camera_metadata_t *metadata) { 51 camera_metadata_t* tmp; 52 53 if (!validate_camera_metadata_structure(metadata, NULL)) 54 return -EINVAL; 55 56 tmp = clone_camera_metadata(metadata); 57 if (tmp == NULL) 58 return -EINVAL; 59 60 replace(tmp); 61 return 0; 62 } 63 64 int Metadata::addUInt8(uint32_t tag, int count, const uint8_t *data) { 65 if (!validate(tag, TYPE_BYTE, count)) return -EINVAL; 66 return add(tag, count, data); 67 } 68 69 int Metadata::add1UInt8(uint32_t tag, const uint8_t data) { 70 return addUInt8(tag, 1, &data); 71 } 72 73 int Metadata::addInt32(uint32_t tag, int count, const int32_t *data) { 74 if (!validate(tag, TYPE_INT32, count)) return -EINVAL; 75 return add(tag, count, data); 76 } 77 78 int Metadata::addFloat(uint32_t tag, int count, const float *data) { 79 if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL; 80 return add(tag, count, data); 81 } 82 83 int Metadata::addInt64(uint32_t tag, int count, const int64_t *data) { 84 if (!validate(tag, TYPE_INT64, count)) return -EINVAL; 85 return add(tag, count, data); 86 } 87 88 int Metadata::addDouble(uint32_t tag, int count, const double *data) { 89 if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL; 90 return add(tag, count, data); 91 } 92 93 int Metadata::addRational(uint32_t tag, int count, 94 const camera_metadata_rational_t *data) { 95 if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL; 96 return add(tag, count, data); 97 } 98 99 bool Metadata::validate(uint32_t tag, int tag_type, int count) { 100 if (get_camera_metadata_tag_type(tag) < 0) { 101 ALOGE("%s: Invalid metadata entry tag: %d", __func__, tag); 102 return false; 103 } 104 if (tag_type < 0 || tag_type >= NUM_TYPES) { 105 ALOGE("%s: Invalid metadata entry tag type: %d", __func__, tag_type); 106 return false; 107 } 108 if (tag_type != get_camera_metadata_tag_type(tag)) { 109 ALOGE("%s: Tag %d called with incorrect type: %s(%d)", __func__, tag, 110 camera_metadata_type_names[tag_type], tag_type); 111 return false; 112 } 113 if (count < 1) { 114 ALOGE("%s: Invalid metadata entry count: %d", __func__, count); 115 return false; 116 } 117 return true; 118 } 119 120 int Metadata::add(uint32_t tag, int count, const void *tag_data) { 121 // Opportunistically attempt to add if metadata has room for it 122 if (!add_camera_metadata_entry(mData, tag, tag_data, count)) { 123 return 0; 124 } 125 126 int res; 127 camera_metadata_t* tmp; 128 int tag_type = get_camera_metadata_tag_type(tag); 129 size_t size = calculate_camera_metadata_entry_data_size(tag_type, count); 130 size_t entry_capacity = get_camera_metadata_entry_count(mData) + 1; 131 size_t data_capacity = get_camera_metadata_data_count(mData) + size; 132 133 // Double new dimensions to minimize future reallocations 134 tmp = allocate_camera_metadata(entry_capacity * 2, data_capacity * 2); 135 if (tmp == NULL) { 136 ALOGE("%s: Failed to allocate new metadata with %zu entries, %zu data", 137 __func__, entry_capacity, data_capacity); 138 return -ENOMEM; 139 } 140 // Append the current metadata to the new (empty) metadata 141 res = append_camera_metadata(tmp, mData); 142 if (res) { 143 ALOGE("%s: Failed to append old metadata %p to new %p", 144 __func__, mData, tmp); 145 return res; 146 } 147 // Add the remaining new item 148 res = add_camera_metadata_entry(tmp, tag, tag_data, count); 149 if (res) { 150 ALOGE("%s: Failed to add new entry (%d, %p, %d) to metadata %p", 151 __func__, tag, tag_data, count, tmp); 152 return res; 153 } 154 155 replace(tmp); 156 return 0; 157 } 158 159 camera_metadata_t* Metadata::get() { 160 return mData; 161 } 162 163 } // namespace usb_camera_hal 164