blob: 212573aa4f4e8b33d816f2505a4f2b7e18be1524 [file] [log] [blame]
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -08001/*
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
26namespace android {
27namespace hardware {
28namespace camera {
29namespace device {
30namespace V3_4 {
31namespace implementation {
32
33namespace {
34 const int kDefaultJpegBufSize = 5 << 20; // 5MB
35 const int kDefaultNumVideoBuffer = 4;
36 const int kDefaultNumStillBuffer = 2;
37} // anonymous namespace
38
39const char* ExternalCameraDeviceConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml";
40
41V4L2Frame::V4L2Frame(
42 uint32_t w, uint32_t h, uint32_t fourcc,
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -080043 int bufIdx, int fd, uint32_t dataSize, uint64_t offset) :
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080044 mWidth(w), mHeight(h), mFourcc(fourcc),
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -080045 mBufferIndex(bufIdx), mFd(fd), mDataSize(dataSize), mOffset(offset) {}
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080046
47int V4L2Frame::map(uint8_t** data, size_t* dataSize) {
48 if (data == nullptr || dataSize == nullptr) {
49 ALOGI("%s: V4L2 buffer map bad argument: data %p, dataSize %p",
50 __FUNCTION__, data, dataSize);
51 return -EINVAL;
52 }
53
54 std::lock_guard<std::mutex> lk(mLock);
55 if (!mMapped) {
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -080056 void* addr = mmap(NULL, mDataSize, PROT_READ, MAP_SHARED, mFd, mOffset);
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080057 if (addr == MAP_FAILED) {
58 ALOGE("%s: V4L2 buffer map failed: %s", __FUNCTION__, strerror(errno));
59 return -EINVAL;
60 }
61 mData = static_cast<uint8_t*>(addr);
62 mMapped = true;
63 }
64 *data = mData;
65 *dataSize = mDataSize;
66 ALOGV("%s: V4L map FD %d, data %p size %zu", __FUNCTION__, mFd, mData, mDataSize);
67 return 0;
68}
69
70int V4L2Frame::unmap() {
71 std::lock_guard<std::mutex> lk(mLock);
72 if (mMapped) {
73 ALOGV("%s: V4L unmap data %p size %zu", __FUNCTION__, mData, mDataSize);
74 if (munmap(mData, mDataSize) != 0) {
75 ALOGE("%s: V4L2 buffer unmap failed: %s", __FUNCTION__, strerror(errno));
76 return -EINVAL;
77 }
78 mMapped = false;
79 }
80 return 0;
81}
82
83V4L2Frame::~V4L2Frame() {
84 unmap();
85}
86
87AllocatedFrame::AllocatedFrame(
88 uint32_t w, uint32_t h) :
89 mWidth(w), mHeight(h), mFourcc(V4L2_PIX_FMT_YUV420) {};
90
91AllocatedFrame::~AllocatedFrame() {}
92
93int AllocatedFrame::allocate(YCbCrLayout* out) {
94 std::lock_guard<std::mutex> lk(mLock);
95 if ((mWidth % 2) || (mHeight % 2)) {
96 ALOGE("%s: bad dimension %dx%d (not multiple of 2)", __FUNCTION__, mWidth, mHeight);
97 return -EINVAL;
98 }
99
100 uint32_t dataSize = mWidth * mHeight * 3 / 2; // YUV420
101 if (mData.size() != dataSize) {
102 mData.resize(dataSize);
103 }
104
105 if (out != nullptr) {
106 out->y = mData.data();
107 out->yStride = mWidth;
108 uint8_t* cbStart = mData.data() + mWidth * mHeight;
109 uint8_t* crStart = cbStart + mWidth * mHeight / 4;
110 out->cb = cbStart;
111 out->cr = crStart;
112 out->cStride = mWidth / 2;
113 out->chromaStep = 1;
114 }
115 return 0;
116}
117
118int AllocatedFrame::getLayout(YCbCrLayout* out) {
119 IMapper::Rect noCrop = {0, 0,
120 static_cast<int32_t>(mWidth),
121 static_cast<int32_t>(mHeight)};
122 return getCroppedLayout(noCrop, out);
123}
124
125int AllocatedFrame::getCroppedLayout(const IMapper::Rect& rect, YCbCrLayout* out) {
126 if (out == nullptr) {
127 ALOGE("%s: null out", __FUNCTION__);
128 return -1;
129 }
130
131 std::lock_guard<std::mutex> lk(mLock);
132 if ((rect.left + rect.width) > static_cast<int>(mWidth) ||
133 (rect.top + rect.height) > static_cast<int>(mHeight) ||
134 (rect.left % 2) || (rect.top % 2) || (rect.width % 2) || (rect.height % 2)) {
135 ALOGE("%s: bad rect left %d top %d w %d h %d", __FUNCTION__,
136 rect.left, rect.top, rect.width, rect.height);
137 return -1;
138 }
139
140 out->y = mData.data() + mWidth * rect.top + rect.left;
141 out->yStride = mWidth;
142 uint8_t* cbStart = mData.data() + mWidth * mHeight;
143 uint8_t* crStart = cbStart + mWidth * mHeight / 4;
144 out->cb = cbStart + mWidth * rect.top / 4 + rect.left / 2;
145 out->cr = crStart + mWidth * rect.top / 4 + rect.left / 2;
146 out->cStride = mWidth / 2;
147 out->chromaStep = 1;
148 return 0;
149}
150
151
152ExternalCameraDeviceConfig ExternalCameraDeviceConfig::loadFromCfg(const char* cfgPath) {
153 using namespace tinyxml2;
154 ExternalCameraDeviceConfig ret;
155
156 XMLDocument configXml;
157 XMLError err = configXml.LoadFile(cfgPath);
158 if (err != XML_SUCCESS) {
159 ALOGE("%s: Unable to load external camera config file '%s'. Error: %s",
160 __FUNCTION__, cfgPath, XMLDocument::ErrorIDToName(err));
161 return ret;
162 } else {
163 ALOGI("%s: load external camera config succeed!", __FUNCTION__);
164 }
165
166 XMLElement *extCam = configXml.FirstChildElement("ExternalCamera");
167 if (extCam == nullptr) {
168 ALOGI("%s: no external camera config specified", __FUNCTION__);
169 return ret;
170 }
171
172 XMLElement *deviceCfg = extCam->FirstChildElement("Device");
173 if (deviceCfg == nullptr) {
174 ALOGI("%s: no external camera device config specified", __FUNCTION__);
175 return ret;
176 }
177
178 XMLElement *jpegBufSz = deviceCfg->FirstChildElement("MaxJpegBufferSize");
179 if (jpegBufSz == nullptr) {
180 ALOGI("%s: no max jpeg buffer size specified", __FUNCTION__);
181 } else {
182 ret.maxJpegBufSize = jpegBufSz->UnsignedAttribute("bytes", /*Default*/kDefaultJpegBufSize);
183 }
184
185 XMLElement *numVideoBuf = deviceCfg->FirstChildElement("NumVideoBuffers");
186 if (numVideoBuf == nullptr) {
187 ALOGI("%s: no num video buffers specified", __FUNCTION__);
188 } else {
189 ret.numVideoBuffers =
190 numVideoBuf->UnsignedAttribute("count", /*Default*/kDefaultNumVideoBuffer);
191 }
192
193 XMLElement *numStillBuf = deviceCfg->FirstChildElement("NumStillBuffers");
194 if (numStillBuf == nullptr) {
195 ALOGI("%s: no num still buffers specified", __FUNCTION__);
196 } else {
197 ret.numStillBuffers =
198 numStillBuf->UnsignedAttribute("count", /*Default*/kDefaultNumStillBuffer);
199 }
200
201 XMLElement *fpsList = deviceCfg->FirstChildElement("FpsList");
202 if (fpsList == nullptr) {
203 ALOGI("%s: no fps list specified", __FUNCTION__);
204 } else {
205 std::vector<FpsLimitation> limits;
206 XMLElement *row = fpsList->FirstChildElement("Limit");
207 while (row != nullptr) {
208 FpsLimitation prevLimit {{0, 0}, 1000.0};
209 FpsLimitation limit;
210 limit.size = {
211 row->UnsignedAttribute("width", /*Default*/0),
212 row->UnsignedAttribute("height", /*Default*/0)};
213 limit.fpsUpperBound = row->FloatAttribute("fpsBound", /*Default*/1000.0);
214 if (limit.size.width <= prevLimit.size.width ||
215 limit.size.height <= prevLimit.size.height ||
216 limit.fpsUpperBound >= prevLimit.fpsUpperBound) {
217 ALOGE("%s: FPS limit list must have increasing size and decreasing fps!"
218 " Prev %dx%d@%f, Current %dx%d@%f", __FUNCTION__,
219 prevLimit.size.width, prevLimit.size.height, prevLimit.fpsUpperBound,
220 limit.size.width, limit.size.height, limit.fpsUpperBound);
221 return ret;
222 }
223 limits.push_back(limit);
224 row = row->NextSiblingElement("Limit");
225 }
226 ret.fpsLimits = limits;
227 }
228
229 ALOGI("%s: external camera cfd loaded: maxJpgBufSize %d,"
230 " num video buffers %d, num still buffers %d",
231 __FUNCTION__, ret.maxJpegBufSize,
232 ret.numVideoBuffers, ret.numStillBuffers);
233 for (const auto& limit : ret.fpsLimits) {
234 ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__,
235 limit.size.width, limit.size.height, limit.fpsUpperBound);
236 }
237 return ret;
238}
239
240ExternalCameraDeviceConfig::ExternalCameraDeviceConfig() :
241 maxJpegBufSize(kDefaultJpegBufSize),
242 numVideoBuffers(kDefaultNumVideoBuffer),
243 numStillBuffers(kDefaultNumStillBuffer) {
244 fpsLimits.push_back({/*Size*/{ 640, 480}, /*FPS upper bound*/30.0});
245 fpsLimits.push_back({/*Size*/{1280, 720}, /*FPS upper bound*/15.0});
246 fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/10.0});
247 fpsLimits.push_back({/*Size*/{4096, 3072}, /*FPS upper bound*/5.0});
248}
249
250bool isAspectRatioClose(float ar1, float ar2) {
251 const float kAspectRatioMatchThres = 0.025f; // This threshold is good enough to distinguish
252 // 4:3/16:9/20:9
253 // 1.33 / 1.78 / 2
254 return (std::abs(ar1 - ar2) < kAspectRatioMatchThres);
255}
256
257} // namespace implementation
258} // namespace V3_4
259} // namespace device
260} // namespace camera
261} // namespace hardware
262} // namespace android