summaryrefslogtreecommitdiff
path: root/modules/camera/3_4/metadata/enum_converter.cpp
blob: 14da006d92ef64772d5bdeed9500f521ffd6f936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "EnumConverter"

#include "enum_converter.h"

#include <errno.h>

#include "../common.h"

namespace v4l2_camera_hal {

EnumConverter::EnumConverter(
    const std::multimap<int32_t, uint8_t>& v4l2_to_metadata)
    : v4l2_to_metadata_(v4l2_to_metadata) {
  HAL_LOG_ENTER();
}

int EnumConverter::MetadataToV4L2(uint8_t value, int32_t* conversion) {
  // Unfortunately no bi-directional map lookup in C++.
  // Breaking on second, not first found so that a warning
  // can be given if there are multiple values.
  size_t count = 0;
  for (auto kv : v4l2_to_metadata_) {
    if (kv.second == value) {
      ++count;
      if (count == 1) {
        // First match.
        *conversion = kv.first;
      } else {
        // second match.
        break;
      }
    }
  }

  if (count == 0) {
    HAL_LOGV("Couldn't find V4L2 conversion of metadata value %d.", value);
    return -EINVAL;
  } else if (count > 1) {
    HAL_LOGV(
        "Multiple V4L2 conversions found for metadata value %d, using first.",
        value);
  }
  return 0;
}

int EnumConverter::V4L2ToMetadata(int32_t value, uint8_t* conversion) {
  auto element_range = v4l2_to_metadata_.equal_range(value);
  if (element_range.first == element_range.second) {
    HAL_LOGV("Couldn't find metadata conversion of V4L2 value %d.", value);
    return -EINVAL;
  }

  auto element = element_range.first;
  *conversion = element->second;

  if (++element != element_range.second) {
    HAL_LOGV(
        "Multiple metadata conversions found for V4L2 value %d, using first.",
        value);
  }
  return 0;
}

}  // namespace v4l2_camera_hal