1 /* 2 * Copyright (C) 2016 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 V4L2_CAMERA_HAL_METADATA_MAP_CONVERTER_H_ 18 #define V4L2_CAMERA_HAL_METADATA_MAP_CONVERTER_H_ 19 20 #include <errno.h> 21 22 #include <map> 23 #include <memory> 24 25 #include "../common.h" 26 #include "converter_interface.h" 27 28 namespace v4l2_camera_hal { 29 30 // A MapConverter fits values converted by a wrapped converter 31 // to a map entry corresponding to the key with the nearest value. 32 template <typename TMetadata, typename TV4L2, typename TMapKey> 33 class MapConverter : public ConverterInterface<TMetadata, TV4L2> { 34 public: 35 MapConverter( 36 std::shared_ptr<ConverterInterface<TMetadata, TMapKey>> wrapped_converter, 37 std::map<TMapKey, TV4L2> conversion_map); 38 39 virtual int MetadataToV4L2(TMetadata value, TV4L2* conversion) override; 40 virtual int V4L2ToMetadata(TV4L2 value, TMetadata* conversion) override; 41 42 private: 43 std::shared_ptr<ConverterInterface<TMetadata, TMapKey>> wrapped_converter_; 44 std::map<TMapKey, TV4L2> conversion_map_; 45 46 DISALLOW_COPY_AND_ASSIGN(MapConverter); 47 }; 48 49 // ----------------------------------------------------------------------------- 50 51 template <typename TMetadata, typename TV4L2, typename TMapKey> 52 MapConverter<TMetadata, TV4L2, TMapKey>::MapConverter( 53 std::shared_ptr<ConverterInterface<TMetadata, TMapKey>> wrapped_converter, 54 std::map<TMapKey, TV4L2> conversion_map) 55 : wrapped_converter_(std::move(wrapped_converter)), 56 conversion_map_(conversion_map) { 57 HAL_LOG_ENTER(); 58 } 59 60 template <typename TMetadata, typename TV4L2, typename TMapKey> 61 int MapConverter<TMetadata, TV4L2, TMapKey>::MetadataToV4L2(TMetadata value, 62 TV4L2* conversion) { 63 HAL_LOG_ENTER(); 64 65 if (conversion_map_.empty()) { 66 HAL_LOGE("Empty conversion map."); 67 return -EINVAL; 68 } 69 70 TMapKey raw_conversion = 0; 71 int res = wrapped_converter_->MetadataToV4L2(value, &raw_conversion); 72 if (res) { 73 HAL_LOGE("Failed to perform underlying conversion."); 74 return res; 75 } 76 77 // Find nearest key. 78 auto kv = conversion_map_.lower_bound(raw_conversion); 79 // lower_bound finds the first >= element. 80 if (kv == conversion_map_.begin()) { 81 // Searching for less than the smallest key, so that will be the nearest. 82 *conversion = kv->second; 83 } else if (kv == conversion_map_.end()) { 84 // Searching for greater than the largest key, so that will be the nearest. 85 --kv; 86 *conversion = kv->second; 87 } else { 88 // Since kv points to the first >= element, either that or the previous 89 // element will be nearest. 90 *conversion = kv->second; 91 TMapKey diff = kv->first - raw_conversion; 92 93 // Now compare to the previous. This element will be < raw conversion, 94 // so reverse the order of the subtraction. 95 --kv; 96 if (raw_conversion - kv->first < diff) { 97 *conversion = kv->second; 98 } 99 } 100 101 return 0; 102 } 103 104 template <typename TMetadata, typename TV4L2, typename TMapKey> 105 int MapConverter<TMetadata, TV4L2, TMapKey>::V4L2ToMetadata( 106 TV4L2 value, TMetadata* conversion) { 107 HAL_LOG_ENTER(); 108 109 // Unfortunately no bi-directional map lookup in C++. 110 // Breaking on second, not first found so that a warning 111 // can be given if there are multiple values. 112 size_t count = 0; 113 int res; 114 for (auto kv : conversion_map_) { 115 if (kv.second == value) { 116 ++count; 117 if (count == 1) { 118 // First match. 119 res = wrapped_converter_->V4L2ToMetadata(kv.first, conversion); 120 } else { 121 // second match. 122 break; 123 } 124 } 125 } 126 127 if (count == 0) { 128 HAL_LOGE("Couldn't find map conversion of V4L2 value %d.", value); 129 return -EINVAL; 130 } else if (count > 1) { 131 HAL_LOGW("Multiple map conversions found for V4L2 value %d, using first.", 132 value); 133 } 134 return res; 135 } 136 137 } // namespace v4l2_camera_hal 138 139 #endif // V4L2_CAMERA_HAL_METADATA_MAP_CONVERTER_H_ 140