blob: d28a4dd46a60ffcd50fca31556873d7a5d639f7e [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 {
163 const int kDefaultJpegBufSize = 5 << 20; // 5MB
164 const int kDefaultNumVideoBuffer = 4;
165 const int kDefaultNumStillBuffer = 2;
166} // anonymous namespace
167
168const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml";
169
170ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) {
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800171 using namespace tinyxml2;
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800172 ExternalCameraConfig ret;
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800173
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
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800190 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
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800213 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)};
Yin-Chia Yeh134093a2018-02-12 14:05:48 -0800254 limit.fpsUpperBound = row->DoubleAttribute("fpsBound", /*Default*/1000.0);
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800255 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
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800270 ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d,"
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800271 " 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
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800281ExternalCameraConfig::ExternalCameraConfig() :
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800282 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*/15.0});
287 fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/10.0});
288 fpsLimits.push_back({/*Size*/{4096, 3072}, /*FPS upper bound*/5.0});
289}
290
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800291
Yin-Chia Yeh17982492018-02-05 17:41:01 -0800292} // namespace common
293} // namespace external
Yin-Chia Yeh53f4cb12018-01-29 10:31:45 -0800294} // namespace camera
295} // namespace hardware
296} // namespace android