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 (at) 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 33 V4L2Frame::V4L2Frame( 34 uint32_t w, uint32_t h, uint32_t fourcc, 35 int bufIdx, int fd, uint32_t dataSize, uint64_t offset) : 36 mWidth(w), mHeight(h), mFourcc(fourcc), 37 mBufferIndex(bufIdx), mFd(fd), mDataSize(dataSize), mOffset(offset) {} 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) { 48 void* addr = mmap(NULL, mDataSize, PROT_READ, MAP_SHARED, mFd, mOffset); 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 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 } 149 150 double SupportedV4L2Format::FrameRate::getDouble() const { 151 return durationDenominator / static_cast<double>(durationNumerator); 152 } 153 154 } // namespace implementation 155 } // namespace V3_4 156 } // namespace device 157 158 159 namespace external { 160 namespace common { 161 162 namespace { 163 const int kDefaultJpegBufSize = 5 << 20; // 5MB 164 const int kDefaultNumVideoBuffer = 4; 165 const int kDefaultNumStillBuffer = 2; 166 } // anonymous namespace 167 168 const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml"; 169 170 ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) { 171 using namespace tinyxml2; 172 ExternalCameraConfig ret; 173 174 XMLDocument configXml; 175 XMLError err = configXml.LoadFile(cfgPath); 176 if (err != XML_SUCCESS) { 177 ALOGE("%s: Unable to load external camera config file '%s'. Error: %s", 178 __FUNCTION__, cfgPath, XMLDocument::ErrorIDToName(err)); 179 return ret; 180 } else { 181 ALOGI("%s: load external camera config succeed!", __FUNCTION__); 182 } 183 184 XMLElement *extCam = configXml.FirstChildElement("ExternalCamera"); 185 if (extCam == nullptr) { 186 ALOGI("%s: no external camera config specified", __FUNCTION__); 187 return ret; 188 } 189 190 XMLElement *providerCfg = extCam->FirstChildElement("Provider"); 191 if (providerCfg == nullptr) { 192 ALOGI("%s: no external camera provider config specified", __FUNCTION__); 193 return ret; 194 } 195 196 XMLElement *ignore = providerCfg->FirstChildElement("ignore"); 197 if (ignore == nullptr) { 198 ALOGI("%s: no internal ignored device specified", __FUNCTION__); 199 return ret; 200 } 201 202 XMLElement *id = ignore->FirstChildElement("id"); 203 while (id != nullptr) { 204 const char* text = id->GetText(); 205 if (text != nullptr) { 206 ret.mInternalDevices.insert(text); 207 ALOGI("%s: device %s will be ignored by external camera provider", 208 __FUNCTION__, text); 209 } 210 id = id->NextSiblingElement("id"); 211 } 212 213 XMLElement *deviceCfg = extCam->FirstChildElement("Device"); 214 if (deviceCfg == nullptr) { 215 ALOGI("%s: no external camera device config specified", __FUNCTION__); 216 return ret; 217 } 218 219 XMLElement *jpegBufSz = deviceCfg->FirstChildElement("MaxJpegBufferSize"); 220 if (jpegBufSz == nullptr) { 221 ALOGI("%s: no max jpeg buffer size specified", __FUNCTION__); 222 } else { 223 ret.maxJpegBufSize = jpegBufSz->UnsignedAttribute("bytes", /*Default*/kDefaultJpegBufSize); 224 } 225 226 XMLElement *numVideoBuf = deviceCfg->FirstChildElement("NumVideoBuffers"); 227 if (numVideoBuf == nullptr) { 228 ALOGI("%s: no num video buffers specified", __FUNCTION__); 229 } else { 230 ret.numVideoBuffers = 231 numVideoBuf->UnsignedAttribute("count", /*Default*/kDefaultNumVideoBuffer); 232 } 233 234 XMLElement *numStillBuf = deviceCfg->FirstChildElement("NumStillBuffers"); 235 if (numStillBuf == nullptr) { 236 ALOGI("%s: no num still buffers specified", __FUNCTION__); 237 } else { 238 ret.numStillBuffers = 239 numStillBuf->UnsignedAttribute("count", /*Default*/kDefaultNumStillBuffer); 240 } 241 242 XMLElement *fpsList = deviceCfg->FirstChildElement("FpsList"); 243 if (fpsList == nullptr) { 244 ALOGI("%s: no fps list specified", __FUNCTION__); 245 } else { 246 std::vector<FpsLimitation> limits; 247 XMLElement *row = fpsList->FirstChildElement("Limit"); 248 while (row != nullptr) { 249 FpsLimitation prevLimit {{0, 0}, 1000.0}; 250 FpsLimitation limit; 251 limit.size = { 252 row->UnsignedAttribute("width", /*Default*/0), 253 row->UnsignedAttribute("height", /*Default*/0)}; 254 limit.fpsUpperBound = row->DoubleAttribute("fpsBound", /*Default*/1000.0); 255 if (limit.size.width <= prevLimit.size.width || 256 limit.size.height <= prevLimit.size.height || 257 limit.fpsUpperBound >= prevLimit.fpsUpperBound) { 258 ALOGE("%s: FPS limit list must have increasing size and decreasing fps!" 259 " Prev %dx%d@%f, Current %dx%d@%f", __FUNCTION__, 260 prevLimit.size.width, prevLimit.size.height, prevLimit.fpsUpperBound, 261 limit.size.width, limit.size.height, limit.fpsUpperBound); 262 return ret; 263 } 264 limits.push_back(limit); 265 row = row->NextSiblingElement("Limit"); 266 } 267 ret.fpsLimits = limits; 268 } 269 270 ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d," 271 " num video buffers %d, num still buffers %d", 272 __FUNCTION__, ret.maxJpegBufSize, 273 ret.numVideoBuffers, ret.numStillBuffers); 274 for (const auto& limit : ret.fpsLimits) { 275 ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__, 276 limit.size.width, limit.size.height, limit.fpsUpperBound); 277 } 278 return ret; 279 } 280 281 ExternalCameraConfig::ExternalCameraConfig() : 282 maxJpegBufSize(kDefaultJpegBufSize), 283 numVideoBuffers(kDefaultNumVideoBuffer), 284 numStillBuffers(kDefaultNumStillBuffer) { 285 fpsLimits.push_back({/*Size*/{ 640, 480}, /*FPS upper bound*/30.0}); 286 fpsLimits.push_back({/*Size*/{1280, 720}, /*FPS upper bound*/7.5}); 287 fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/5.0}); 288 } 289 290 291 } // namespace common 292 } // namespace external 293 } // namespace camera 294 } // namespace hardware 295 } // namespace android 296