blob: 80f296c6ad473bb856f6e1d6c603c9e453c1dbde [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 Yeh17982492018-02-05 17:41:01 -0800150} // namespace implementation
151} // namespace V3_4
152} // namespace device
153
154
155namespace external {
156namespace common {
157
158namespace {
159 const int kDefaultJpegBufSize = 5 << 20; // 5MB
160 const int kDefaultNumVideoBuffer = 4;
161 const int kDefaultNumStillBuffer = 2;
162} // anonymous namespace
163
164const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml";
165
166ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) {
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800167 using namespace tinyxml2;
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800168 ExternalCameraConfig ret;
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800169
170 XMLDocument configXml;
171 XMLError err = configXml.LoadFile(cfgPath);
172 if (err != XML_SUCCESS) {
173 ALOGE("%s: Unable to load external camera config file '%s'. Error: %s",
174 __FUNCTION__, cfgPath, XMLDocument::ErrorIDToName(err));
175 return ret;
176 } else {
177 ALOGI("%s: load external camera config succeed!", __FUNCTION__);
178 }
179
180 XMLElement *extCam = configXml.FirstChildElement("ExternalCamera");
181 if (extCam == nullptr) {
182 ALOGI("%s: no external camera config specified", __FUNCTION__);
183 return ret;
184 }
185
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800186 XMLElement *providerCfg = extCam->FirstChildElement("Provider");
187 if (providerCfg == nullptr) {
188 ALOGI("%s: no external camera provider config specified", __FUNCTION__);
189 return ret;
190 }
191
192 XMLElement *ignore = providerCfg->FirstChildElement("ignore");
193 if (ignore == nullptr) {
194 ALOGI("%s: no internal ignored device specified", __FUNCTION__);
195 return ret;
196 }
197
198 XMLElement *id = ignore->FirstChildElement("id");
199 while (id != nullptr) {
200 const char* text = id->GetText();
201 if (text != nullptr) {
202 ret.mInternalDevices.insert(text);
203 ALOGI("%s: device %s will be ignored by external camera provider",
204 __FUNCTION__, text);
205 }
206 id = id->NextSiblingElement("id");
207 }
208
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800209 XMLElement *deviceCfg = extCam->FirstChildElement("Device");
210 if (deviceCfg == nullptr) {
211 ALOGI("%s: no external camera device config specified", __FUNCTION__);
212 return ret;
213 }
214
215 XMLElement *jpegBufSz = deviceCfg->FirstChildElement("MaxJpegBufferSize");
216 if (jpegBufSz == nullptr) {
217 ALOGI("%s: no max jpeg buffer size specified", __FUNCTION__);
218 } else {
219 ret.maxJpegBufSize = jpegBufSz->UnsignedAttribute("bytes", /*Default*/kDefaultJpegBufSize);
220 }
221
222 XMLElement *numVideoBuf = deviceCfg->FirstChildElement("NumVideoBuffers");
223 if (numVideoBuf == nullptr) {
224 ALOGI("%s: no num video buffers specified", __FUNCTION__);
225 } else {
226 ret.numVideoBuffers =
227 numVideoBuf->UnsignedAttribute("count", /*Default*/kDefaultNumVideoBuffer);
228 }
229
230 XMLElement *numStillBuf = deviceCfg->FirstChildElement("NumStillBuffers");
231 if (numStillBuf == nullptr) {
232 ALOGI("%s: no num still buffers specified", __FUNCTION__);
233 } else {
234 ret.numStillBuffers =
235 numStillBuf->UnsignedAttribute("count", /*Default*/kDefaultNumStillBuffer);
236 }
237
238 XMLElement *fpsList = deviceCfg->FirstChildElement("FpsList");
239 if (fpsList == nullptr) {
240 ALOGI("%s: no fps list specified", __FUNCTION__);
241 } else {
242 std::vector<FpsLimitation> limits;
243 XMLElement *row = fpsList->FirstChildElement("Limit");
244 while (row != nullptr) {
245 FpsLimitation prevLimit {{0, 0}, 1000.0};
246 FpsLimitation limit;
247 limit.size = {
248 row->UnsignedAttribute("width", /*Default*/0),
249 row->UnsignedAttribute("height", /*Default*/0)};
250 limit.fpsUpperBound = row->FloatAttribute("fpsBound", /*Default*/1000.0);
251 if (limit.size.width <= prevLimit.size.width ||
252 limit.size.height <= prevLimit.size.height ||
253 limit.fpsUpperBound >= prevLimit.fpsUpperBound) {
254 ALOGE("%s: FPS limit list must have increasing size and decreasing fps!"
255 " Prev %dx%d@%f, Current %dx%d@%f", __FUNCTION__,
256 prevLimit.size.width, prevLimit.size.height, prevLimit.fpsUpperBound,
257 limit.size.width, limit.size.height, limit.fpsUpperBound);
258 return ret;
259 }
260 limits.push_back(limit);
261 row = row->NextSiblingElement("Limit");
262 }
263 ret.fpsLimits = limits;
264 }
265
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800266 ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d,"
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800267 " num video buffers %d, num still buffers %d",
268 __FUNCTION__, ret.maxJpegBufSize,
269 ret.numVideoBuffers, ret.numStillBuffers);
270 for (const auto& limit : ret.fpsLimits) {
271 ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__,
272 limit.size.width, limit.size.height, limit.fpsUpperBound);
273 }
274 return ret;
275}
276
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800277ExternalCameraConfig::ExternalCameraConfig() :
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800278 maxJpegBufSize(kDefaultJpegBufSize),
279 numVideoBuffers(kDefaultNumVideoBuffer),
280 numStillBuffers(kDefaultNumStillBuffer) {
281 fpsLimits.push_back({/*Size*/{ 640, 480}, /*FPS upper bound*/30.0});
282 fpsLimits.push_back({/*Size*/{1280, 720}, /*FPS upper bound*/15.0});
283 fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/10.0});
284 fpsLimits.push_back({/*Size*/{4096, 3072}, /*FPS upper bound*/5.0});
285}
286
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800287
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800288} // namespace common
289} // namespace external
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800290} // namespace camera
291} // namespace hardware
292} // namespace android