1 /*
2  * Copyright (C) 2022 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 #include "ConfigManagerUtil.h"
18 
19 #include <android-base/logging.h>
20 #include <linux/videodev2.h>
21 
22 #include <sstream>
23 #include <string>
24 
25 #include <system/graphics-base-v1.0.h>
26 
27 using ::aidl::android::hardware::automotive::evs::CameraParam;
28 using ::aidl::android::hardware::graphics::common::PixelFormat;
29 
convertToEvsCameraParam(const std::string & id,CameraParam & camParam)30 bool ConfigManagerUtil::convertToEvsCameraParam(const std::string& id, CameraParam& camParam) {
31     std::string trimmed = ConfigManagerUtil::trimString(id);
32     bool success = true;
33 
34     if (!trimmed.compare("BRIGHTNESS")) {
35         camParam = CameraParam::BRIGHTNESS;
36     } else if (!trimmed.compare("CONTRAST")) {
37         camParam = CameraParam::CONTRAST;
38     } else if (!trimmed.compare("AUTOGAIN")) {
39         camParam = CameraParam::AUTOGAIN;
40     } else if (!trimmed.compare("GAIN")) {
41         camParam = CameraParam::GAIN;
42     } else if (!trimmed.compare("AUTO_WHITE_BALANCE")) {
43         camParam = CameraParam::AUTO_WHITE_BALANCE;
44     } else if (!trimmed.compare("WHITE_BALANCE_TEMPERATURE")) {
45         camParam = CameraParam::WHITE_BALANCE_TEMPERATURE;
46     } else if (!trimmed.compare("SHARPNESS")) {
47         camParam = CameraParam::SHARPNESS;
48     } else if (!trimmed.compare("AUTO_EXPOSURE")) {
49         camParam = CameraParam::AUTO_EXPOSURE;
50     } else if (!trimmed.compare("ABSOLUTE_EXPOSURE")) {
51         camParam = CameraParam::ABSOLUTE_EXPOSURE;
52     } else if (!trimmed.compare("ABSOLUTE_FOCUS")) {
53         camParam = CameraParam::ABSOLUTE_FOCUS;
54     } else if (!trimmed.compare("AUTO_FOCUS")) {
55         camParam = CameraParam::AUTO_FOCUS;
56     } else if (!trimmed.compare("ABSOLUTE_ZOOM")) {
57         camParam = CameraParam::ABSOLUTE_ZOOM;
58     } else {
59         success = false;
60     }
61 
62     return success;
63 }
64 
convertToPixelFormat(const std::string & in,PixelFormat & out)65 bool ConfigManagerUtil::convertToPixelFormat(const std::string& in, PixelFormat& out) {
66     std::string trimmed = ConfigManagerUtil::trimString(in);
67     bool success = true;
68 
69     if (!trimmed.compare("RGBA_8888")) {
70         out = PixelFormat::RGBA_8888;
71     } else if (!trimmed.compare("YCRCB_420_SP")) {
72         out = PixelFormat::YCRCB_420_SP;
73     } else if (!trimmed.compare("YCBCR_422_I")) {
74         out = PixelFormat::YCBCR_422_I;
75     } else {
76         out = PixelFormat::UNSPECIFIED;
77         success = false;
78     }
79 
80     return success;
81 }
82 
convertToMetadataTag(const char * name,camera_metadata_tag & aTag)83 bool ConfigManagerUtil::convertToMetadataTag(const char* name, camera_metadata_tag& aTag) {
84     if (!std::strcmp(name, "LENS_DISTORTION")) {
85         aTag = ANDROID_LENS_DISTORTION;
86     } else if (!std::strcmp(name, "LENS_INTRINSIC_CALIBRATION")) {
87         aTag = ANDROID_LENS_INTRINSIC_CALIBRATION;
88     } else if (!std::strcmp(name, "LENS_POSE_ROTATION")) {
89         aTag = ANDROID_LENS_POSE_ROTATION;
90     } else if (!std::strcmp(name, "LENS_POSE_TRANSLATION")) {
91         aTag = ANDROID_LENS_POSE_TRANSLATION;
92     } else if (!std::strcmp(name, "REQUEST_AVAILABLE_CAPABILITIES")) {
93         aTag = ANDROID_REQUEST_AVAILABLE_CAPABILITIES;
94     } else if (!std::strcmp(name, "LOGICAL_MULTI_CAMERA_PHYSICAL_IDS")) {
95         aTag = ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS;
96     } else {
97         return false;
98     }
99 
100     return true;
101 }
102 
convertToCameraCapability(const char * name,camera_metadata_enum_android_request_available_capabilities_t & cap)103 bool ConfigManagerUtil::convertToCameraCapability(
104         const char* name, camera_metadata_enum_android_request_available_capabilities_t& cap) {
105     if (!std::strcmp(name, "DEPTH_OUTPUT")) {
106         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT;
107     } else if (!std::strcmp(name, "LOGICAL_MULTI_CAMERA")) {
108         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA;
109     } else if (!std::strcmp(name, "MONOCHROME")) {
110         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME;
111     } else if (!std::strcmp(name, "SECURE_IMAGE_DATA")) {
112         cap = ANDROID_REQUEST_AVAILABLE_CAPABILITIES_SECURE_IMAGE_DATA;
113     } else {
114         return false;
115     }
116 
117     return true;
118 }
119 
convertFloatArray(const char * sz,const char * vals,size_t & count,const char delimiter)120 float* ConfigManagerUtil::convertFloatArray(const char* sz, const char* vals, size_t& count,
121                                             const char delimiter) {
122     std::string size_string(sz);
123     std::string value_string(vals);
124 
125     count = stoi(size_string);
126     float* result = new float[count];
127     std::stringstream values(value_string);
128 
129     int32_t idx = 0;
130     std::string token;
131     while (getline(values, token, delimiter)) {
132         result[idx++] = stof(token);
133     }
134 
135     return result;
136 }
137 
trimString(const std::string & src,const std::string & ws)138 std::string ConfigManagerUtil::trimString(const std::string& src, const std::string& ws) {
139     const auto s = src.find_first_not_of(ws);
140     if (s == std::string::npos) {
141         return "";
142     }
143 
144     const auto e = src.find_last_not_of(ws);
145     const auto r = e - s + 1;
146 
147     return src.substr(s, r);
148 }
149