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