blob: 0941052c7840ef81c9e00345f19b4e1049b44b32 [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
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080033V4L2Frame::V4L2Frame(
34 uint32_t w, uint32_t h, uint32_t fourcc,
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -080035 int bufIdx, int fd, uint32_t dataSize, uint64_t offset) :
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080036 mWidth(w), mHeight(h), mFourcc(fourcc),
Yuriy Romanenko9cdd6f92018-01-31 15:59:20 -080037 mBufferIndex(bufIdx), mFd(fd), mDataSize(dataSize), mOffset(offset) {}
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080038
39int 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 Romanenko9cdd6f92018-01-31 15:59:20 -080048 void* addr = mmap(NULL, mDataSize, PROT_READ, MAP_SHARED, mFd, mOffset);
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -080049 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
62int 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
75V4L2Frame::~V4L2Frame() {
76 unmap();
77}
78
79AllocatedFrame::AllocatedFrame(
80 uint32_t w, uint32_t h) :
81 mWidth(w), mHeight(h), mFourcc(V4L2_PIX_FMT_YUV420) {};
82
83AllocatedFrame::~AllocatedFrame() {}
84
85int 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
110int 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
117int 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 Yeh17982492018-02-05 17:41:01 -0800143bool 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 Yeh53f4cb12018-01-29 10:31:45 -0800149
Yin-Chia Yeh134093a2018-02-12 14:05:48 -0800150double SupportedV4L2Format::FrameRate::getDouble() const {
151 return durationDenominator / static_cast<double>(durationNumerator);
152}
153
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800154} // namespace implementation
155} // namespace V3_4
156} // namespace device
157
158
159namespace external {
160namespace common {
161
162namespace {
Yin-Chia Yeh1c30a5e2018-11-28 16:00:16 -0800163 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 Yeh17982492018-02-05 17:41:01 -0800168} // anonymous namespace
169
170const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml";
171
172ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) {
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800173 using namespace tinyxml2;
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800174 ExternalCameraConfig ret;
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800175
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 Yeh17982492018-02-05 17:41:01 -0800192 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 Yeh53f4cb12018-01-29 10:31:45 -0800215 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 Yeh134093a2018-02-12 14:05:48 -0800256 limit.fpsUpperBound = row->DoubleAttribute("fpsBound", /*Default*/1000.0);
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800257 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
chenhg87654442018-06-22 13:27:50 -0700272 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 Yeh1c30a5e2018-11-28 16:00:16 -0800281 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 Yeh17982492018-02-05 17:41:01 -0800288 ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d,"
Yin-Chia Yeh1c30a5e2018-11-28 16:00:16 -0800289 " num video buffers %d, num still buffers %d, orientation %d",
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800290 __FUNCTION__, ret.maxJpegBufSize,
Yin-Chia Yeh1c30a5e2018-11-28 16:00:16 -0800291 ret.numVideoBuffers, ret.numStillBuffers, ret.orientation);
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800292 for (const auto& limit : ret.fpsLimits) {
293 ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__,
294 limit.size.width, limit.size.height, limit.fpsUpperBound);
295 }
chenhg87654442018-06-22 13:27:50 -0700296 ALOGI("%s: minStreamSize: %dx%d" , __FUNCTION__,
297 ret.minStreamSize.width, ret.minStreamSize.height);
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800298 return ret;
299}
300
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800301ExternalCameraConfig::ExternalCameraConfig() :
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800302 maxJpegBufSize(kDefaultJpegBufSize),
303 numVideoBuffers(kDefaultNumVideoBuffer),
Yin-Chia Yeh1c30a5e2018-11-28 16:00:16 -0800304 numStillBuffers(kDefaultNumStillBuffer),
305 orientation(kDefaultOrientation) {
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800306 fpsLimits.push_back({/*Size*/{ 640, 480}, /*FPS upper bound*/30.0});
Yin-Chia Yehc15a1ca2018-03-02 14:16:52 -0800307 fpsLimits.push_back({/*Size*/{1280, 720}, /*FPS upper bound*/7.5});
308 fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/5.0});
chenhg87654442018-06-22 13:27:50 -0700309 minStreamSize = {0, 0};
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800310}
311
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800312
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800313} // namespace common
314} // namespace external
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800315} // namespace camera
316} // namespace hardware
317} // namespace android