Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 LOG_TAG "ExtCamUtils@3.4" |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #include <log/log.h> |
| 19 | |
| 20 | #include <cmath> |
| 21 | #include <sys/mman.h> |
| 22 | #include <linux/videodev2.h> |
| 23 | #include "ExternalCameraUtils.h" |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace camera { |
| 28 | namespace device { |
| 29 | namespace V3_4 { |
| 30 | namespace implementation { |
| 31 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 32 | V4L2Frame::V4L2Frame( |
| 33 | uint32_t w, uint32_t h, uint32_t fourcc, |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 34 | int bufIdx, int fd, uint32_t dataSize, uint64_t offset) : |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 35 | mWidth(w), mHeight(h), mFourcc(fourcc), |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 36 | mBufferIndex(bufIdx), mFd(fd), mDataSize(dataSize), mOffset(offset) {} |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 37 | |
| 38 | int V4L2Frame::map(uint8_t** data, size_t* dataSize) { |
| 39 | if (data == nullptr || dataSize == nullptr) { |
| 40 | ALOGI("%s: V4L2 buffer map bad argument: data %p, dataSize %p", |
| 41 | __FUNCTION__, data, dataSize); |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
| 45 | std::lock_guard<std::mutex> lk(mLock); |
| 46 | if (!mMapped) { |
Yuriy Romanenko | 9cdd6f9 | 2018-01-31 15:59:20 -0800 | [diff] [blame] | 47 | void* addr = mmap(NULL, mDataSize, PROT_READ, MAP_SHARED, mFd, mOffset); |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 48 | if (addr == MAP_FAILED) { |
| 49 | ALOGE("%s: V4L2 buffer map failed: %s", __FUNCTION__, strerror(errno)); |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | mData = static_cast<uint8_t*>(addr); |
| 53 | mMapped = true; |
| 54 | } |
| 55 | *data = mData; |
| 56 | *dataSize = mDataSize; |
| 57 | ALOGV("%s: V4L map FD %d, data %p size %zu", __FUNCTION__, mFd, mData, mDataSize); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int V4L2Frame::unmap() { |
| 62 | std::lock_guard<std::mutex> lk(mLock); |
| 63 | if (mMapped) { |
| 64 | ALOGV("%s: V4L unmap data %p size %zu", __FUNCTION__, mData, mDataSize); |
| 65 | if (munmap(mData, mDataSize) != 0) { |
| 66 | ALOGE("%s: V4L2 buffer unmap failed: %s", __FUNCTION__, strerror(errno)); |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | mMapped = false; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | V4L2Frame::~V4L2Frame() { |
| 75 | unmap(); |
| 76 | } |
| 77 | |
| 78 | AllocatedFrame::AllocatedFrame( |
| 79 | uint32_t w, uint32_t h) : |
| 80 | mWidth(w), mHeight(h), mFourcc(V4L2_PIX_FMT_YUV420) {}; |
| 81 | |
| 82 | AllocatedFrame::~AllocatedFrame() {} |
| 83 | |
| 84 | int AllocatedFrame::allocate(YCbCrLayout* out) { |
| 85 | std::lock_guard<std::mutex> lk(mLock); |
| 86 | if ((mWidth % 2) || (mHeight % 2)) { |
| 87 | ALOGE("%s: bad dimension %dx%d (not multiple of 2)", __FUNCTION__, mWidth, mHeight); |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | uint32_t dataSize = mWidth * mHeight * 3 / 2; // YUV420 |
| 92 | if (mData.size() != dataSize) { |
| 93 | mData.resize(dataSize); |
| 94 | } |
| 95 | |
| 96 | if (out != nullptr) { |
| 97 | out->y = mData.data(); |
| 98 | out->yStride = mWidth; |
| 99 | uint8_t* cbStart = mData.data() + mWidth * mHeight; |
| 100 | uint8_t* crStart = cbStart + mWidth * mHeight / 4; |
| 101 | out->cb = cbStart; |
| 102 | out->cr = crStart; |
| 103 | out->cStride = mWidth / 2; |
| 104 | out->chromaStep = 1; |
| 105 | } |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | int AllocatedFrame::getLayout(YCbCrLayout* out) { |
| 110 | IMapper::Rect noCrop = {0, 0, |
| 111 | static_cast<int32_t>(mWidth), |
| 112 | static_cast<int32_t>(mHeight)}; |
| 113 | return getCroppedLayout(noCrop, out); |
| 114 | } |
| 115 | |
| 116 | int AllocatedFrame::getCroppedLayout(const IMapper::Rect& rect, YCbCrLayout* out) { |
| 117 | if (out == nullptr) { |
| 118 | ALOGE("%s: null out", __FUNCTION__); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | std::lock_guard<std::mutex> lk(mLock); |
| 123 | if ((rect.left + rect.width) > static_cast<int>(mWidth) || |
| 124 | (rect.top + rect.height) > static_cast<int>(mHeight) || |
| 125 | (rect.left % 2) || (rect.top % 2) || (rect.width % 2) || (rect.height % 2)) { |
| 126 | ALOGE("%s: bad rect left %d top %d w %d h %d", __FUNCTION__, |
| 127 | rect.left, rect.top, rect.width, rect.height); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | out->y = mData.data() + mWidth * rect.top + rect.left; |
| 132 | out->yStride = mWidth; |
| 133 | uint8_t* cbStart = mData.data() + mWidth * mHeight; |
| 134 | uint8_t* crStart = cbStart + mWidth * mHeight / 4; |
| 135 | out->cb = cbStart + mWidth * rect.top / 4 + rect.left / 2; |
| 136 | out->cr = crStart + mWidth * rect.top / 4 + rect.left / 2; |
| 137 | out->cStride = mWidth / 2; |
| 138 | out->chromaStep = 1; |
| 139 | return 0; |
| 140 | } |
| 141 | |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 142 | bool isAspectRatioClose(float ar1, float ar2) { |
| 143 | const float kAspectRatioMatchThres = 0.025f; // This threshold is good enough to distinguish |
| 144 | // 4:3/16:9/20:9 |
| 145 | // 1.33 / 1.78 / 2 |
| 146 | return (std::abs(ar1 - ar2) < kAspectRatioMatchThres); |
| 147 | } |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 148 | |
Yin-Chia Yeh | 134093a | 2018-02-12 14:05:48 -0800 | [diff] [blame] | 149 | double SupportedV4L2Format::FrameRate::getDouble() const { |
| 150 | return durationDenominator / static_cast<double>(durationNumerator); |
| 151 | } |
| 152 | |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 153 | } // namespace implementation |
| 154 | } // namespace V3_4 |
| 155 | } // namespace device |
| 156 | |
| 157 | |
| 158 | namespace external { |
| 159 | namespace common { |
| 160 | |
| 161 | namespace { |
Yin-Chia Yeh | 1c30a5e | 2018-11-28 16:00:16 -0800 | [diff] [blame] | 162 | const int kDefaultJpegBufSize = 5 << 20; // 5MB |
| 163 | const int kDefaultNumVideoBuffer = 4; |
| 164 | const int kDefaultNumStillBuffer = 2; |
| 165 | const int kDefaultOrientation = 0; // suitable for natural landscape displays like tablet/TV |
| 166 | // For phone devices 270 is better |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 167 | } // anonymous namespace |
| 168 | |
| 169 | const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml"; |
| 170 | |
| 171 | ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) { |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 172 | using namespace tinyxml2; |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 173 | ExternalCameraConfig ret; |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 174 | |
| 175 | XMLDocument configXml; |
| 176 | XMLError err = configXml.LoadFile(cfgPath); |
| 177 | if (err != XML_SUCCESS) { |
| 178 | ALOGE("%s: Unable to load external camera config file '%s'. Error: %s", |
| 179 | __FUNCTION__, cfgPath, XMLDocument::ErrorIDToName(err)); |
| 180 | return ret; |
| 181 | } else { |
| 182 | ALOGI("%s: load external camera config succeed!", __FUNCTION__); |
| 183 | } |
| 184 | |
| 185 | XMLElement *extCam = configXml.FirstChildElement("ExternalCamera"); |
| 186 | if (extCam == nullptr) { |
| 187 | ALOGI("%s: no external camera config specified", __FUNCTION__); |
| 188 | return ret; |
| 189 | } |
| 190 | |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 191 | XMLElement *providerCfg = extCam->FirstChildElement("Provider"); |
| 192 | if (providerCfg == nullptr) { |
| 193 | ALOGI("%s: no external camera provider config specified", __FUNCTION__); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | XMLElement *ignore = providerCfg->FirstChildElement("ignore"); |
| 198 | if (ignore == nullptr) { |
| 199 | ALOGI("%s: no internal ignored device specified", __FUNCTION__); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | XMLElement *id = ignore->FirstChildElement("id"); |
| 204 | while (id != nullptr) { |
| 205 | const char* text = id->GetText(); |
| 206 | if (text != nullptr) { |
| 207 | ret.mInternalDevices.insert(text); |
| 208 | ALOGI("%s: device %s will be ignored by external camera provider", |
| 209 | __FUNCTION__, text); |
| 210 | } |
| 211 | id = id->NextSiblingElement("id"); |
| 212 | } |
| 213 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 214 | XMLElement *deviceCfg = extCam->FirstChildElement("Device"); |
| 215 | if (deviceCfg == nullptr) { |
| 216 | ALOGI("%s: no external camera device config specified", __FUNCTION__); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | XMLElement *jpegBufSz = deviceCfg->FirstChildElement("MaxJpegBufferSize"); |
| 221 | if (jpegBufSz == nullptr) { |
| 222 | ALOGI("%s: no max jpeg buffer size specified", __FUNCTION__); |
| 223 | } else { |
| 224 | ret.maxJpegBufSize = jpegBufSz->UnsignedAttribute("bytes", /*Default*/kDefaultJpegBufSize); |
| 225 | } |
| 226 | |
| 227 | XMLElement *numVideoBuf = deviceCfg->FirstChildElement("NumVideoBuffers"); |
| 228 | if (numVideoBuf == nullptr) { |
| 229 | ALOGI("%s: no num video buffers specified", __FUNCTION__); |
| 230 | } else { |
| 231 | ret.numVideoBuffers = |
| 232 | numVideoBuf->UnsignedAttribute("count", /*Default*/kDefaultNumVideoBuffer); |
| 233 | } |
| 234 | |
| 235 | XMLElement *numStillBuf = deviceCfg->FirstChildElement("NumStillBuffers"); |
| 236 | if (numStillBuf == nullptr) { |
| 237 | ALOGI("%s: no num still buffers specified", __FUNCTION__); |
| 238 | } else { |
| 239 | ret.numStillBuffers = |
| 240 | numStillBuf->UnsignedAttribute("count", /*Default*/kDefaultNumStillBuffer); |
| 241 | } |
| 242 | |
| 243 | XMLElement *fpsList = deviceCfg->FirstChildElement("FpsList"); |
| 244 | if (fpsList == nullptr) { |
| 245 | ALOGI("%s: no fps list specified", __FUNCTION__); |
| 246 | } else { |
Emil Jahshan | eed0040 | 2018-12-11 15:15:17 +0200 | [diff] [blame] | 247 | if (!updateFpsList(fpsList, ret.fpsLimits)) { |
| 248 | return ret; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | XMLElement *depth = deviceCfg->FirstChildElement("Depth16Supported"); |
| 253 | if (depth == nullptr) { |
| 254 | ret.depthEnabled = false; |
| 255 | ALOGI("%s: depth output is not enabled", __FUNCTION__); |
| 256 | } else { |
| 257 | ret.depthEnabled = depth->BoolAttribute("enabled", false); |
| 258 | } |
| 259 | |
| 260 | if(ret.depthEnabled) { |
| 261 | XMLElement *depthFpsList = deviceCfg->FirstChildElement("DepthFpsList"); |
| 262 | if (depthFpsList == nullptr) { |
| 263 | ALOGW("%s: no depth fps list specified", __FUNCTION__); |
| 264 | } else { |
| 265 | if(!updateFpsList(depthFpsList, ret.depthFpsLimits)) { |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 266 | return ret; |
| 267 | } |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 268 | } |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 269 | } |
| 270 | |
chenhg | 8765444 | 2018-06-22 13:27:50 -0700 | [diff] [blame] | 271 | XMLElement *minStreamSize = deviceCfg->FirstChildElement("MinimumStreamSize"); |
| 272 | if (minStreamSize == nullptr) { |
| 273 | ALOGI("%s: no minimum stream size specified", __FUNCTION__); |
| 274 | } else { |
| 275 | ret.minStreamSize = { |
| 276 | minStreamSize->UnsignedAttribute("width", /*Default*/0), |
| 277 | minStreamSize->UnsignedAttribute("height", /*Default*/0)}; |
| 278 | } |
| 279 | |
Yin-Chia Yeh | 1c30a5e | 2018-11-28 16:00:16 -0800 | [diff] [blame] | 280 | XMLElement *orientation = deviceCfg->FirstChildElement("Orientation"); |
| 281 | if (orientation == nullptr) { |
| 282 | ALOGI("%s: no sensor orientation specified", __FUNCTION__); |
| 283 | } else { |
| 284 | ret.orientation = orientation->IntAttribute("degree", /*Default*/kDefaultOrientation); |
| 285 | } |
| 286 | |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 287 | ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d," |
Yin-Chia Yeh | 1c30a5e | 2018-11-28 16:00:16 -0800 | [diff] [blame] | 288 | " num video buffers %d, num still buffers %d, orientation %d", |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 289 | __FUNCTION__, ret.maxJpegBufSize, |
Yin-Chia Yeh | 1c30a5e | 2018-11-28 16:00:16 -0800 | [diff] [blame] | 290 | ret.numVideoBuffers, ret.numStillBuffers, ret.orientation); |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 291 | for (const auto& limit : ret.fpsLimits) { |
| 292 | ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__, |
| 293 | limit.size.width, limit.size.height, limit.fpsUpperBound); |
| 294 | } |
Emil Jahshan | eed0040 | 2018-12-11 15:15:17 +0200 | [diff] [blame] | 295 | for (const auto& limit : ret.depthFpsLimits) { |
| 296 | ALOGI("%s: depthFpsLimitList: %dx%d@%f", __FUNCTION__, limit.size.width, limit.size.height, |
| 297 | limit.fpsUpperBound); |
| 298 | } |
chenhg | 8765444 | 2018-06-22 13:27:50 -0700 | [diff] [blame] | 299 | ALOGI("%s: minStreamSize: %dx%d" , __FUNCTION__, |
| 300 | ret.minStreamSize.width, ret.minStreamSize.height); |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 301 | return ret; |
| 302 | } |
| 303 | |
Emil Jahshan | eed0040 | 2018-12-11 15:15:17 +0200 | [diff] [blame] | 304 | bool ExternalCameraConfig::updateFpsList(tinyxml2::XMLElement* fpsList, |
| 305 | std::vector<FpsLimitation>& fpsLimits) { |
| 306 | using namespace tinyxml2; |
| 307 | std::vector<FpsLimitation> limits; |
| 308 | XMLElement* row = fpsList->FirstChildElement("Limit"); |
| 309 | while (row != nullptr) { |
| 310 | FpsLimitation prevLimit{{0, 0}, 1000.0}; |
| 311 | FpsLimitation limit; |
| 312 | limit.size = {row->UnsignedAttribute("width", /*Default*/ 0), |
| 313 | row->UnsignedAttribute("height", /*Default*/ 0)}; |
| 314 | limit.fpsUpperBound = row->DoubleAttribute("fpsBound", /*Default*/ 1000.0); |
| 315 | if (limit.size.width <= prevLimit.size.width || |
| 316 | limit.size.height <= prevLimit.size.height || |
| 317 | limit.fpsUpperBound >= prevLimit.fpsUpperBound) { |
| 318 | ALOGE( |
| 319 | "%s: FPS limit list must have increasing size and decreasing fps!" |
| 320 | " Prev %dx%d@%f, Current %dx%d@%f", |
| 321 | __FUNCTION__, prevLimit.size.width, prevLimit.size.height, prevLimit.fpsUpperBound, |
| 322 | limit.size.width, limit.size.height, limit.fpsUpperBound); |
| 323 | return false; |
| 324 | } |
| 325 | limits.push_back(limit); |
| 326 | row = row->NextSiblingElement("Limit"); |
| 327 | } |
| 328 | fpsLimits = limits; |
| 329 | return true; |
| 330 | } |
| 331 | |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 332 | ExternalCameraConfig::ExternalCameraConfig() : |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 333 | maxJpegBufSize(kDefaultJpegBufSize), |
| 334 | numVideoBuffers(kDefaultNumVideoBuffer), |
Yin-Chia Yeh | 1c30a5e | 2018-11-28 16:00:16 -0800 | [diff] [blame] | 335 | numStillBuffers(kDefaultNumStillBuffer), |
Emil Jahshan | eed0040 | 2018-12-11 15:15:17 +0200 | [diff] [blame] | 336 | depthEnabled(false), |
Yin-Chia Yeh | 1c30a5e | 2018-11-28 16:00:16 -0800 | [diff] [blame] | 337 | orientation(kDefaultOrientation) { |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 338 | fpsLimits.push_back({/*Size*/{ 640, 480}, /*FPS upper bound*/30.0}); |
Yin-Chia Yeh | c15a1ca | 2018-03-02 14:16:52 -0800 | [diff] [blame] | 339 | fpsLimits.push_back({/*Size*/{1280, 720}, /*FPS upper bound*/7.5}); |
| 340 | fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/5.0}); |
chenhg | 8765444 | 2018-06-22 13:27:50 -0700 | [diff] [blame] | 341 | minStreamSize = {0, 0}; |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 344 | |
Yin-Chia Yeh | 1798249 | 2018-02-05 17:41:01 -0800 | [diff] [blame] | 345 | } // namespace common |
| 346 | } // namespace external |
Yin-Chia Yeh | 53f4cb1 | 2018-01-29 10:31:45 -0800 | [diff] [blame] | 347 | } // namespace camera |
| 348 | } // namespace hardware |
| 349 | } // namespace android |