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@3.4"
17 //#define LOG_NDEBUG 0
18 #include <log/log.h>
19
20 #include <cmath>
21 #include <cstring>
22 #include <sys/mman.h>
23 #include <linux/videodev2.h>
24
25 #define HAVE_JPEG // required for libyuv.h to export MJPEG decode APIs
26 #include <libyuv.h>
27
28 #include <jpeglib.h>
29
30 #include "ExternalCameraUtils.h"
31
32 namespace {
33
34 buffer_handle_t sEmptyBuffer = nullptr;
35
36 } // Anonymous namespace
37
38 namespace android {
39 namespace hardware {
40 namespace camera {
41 namespace device {
42 namespace V3_4 {
43 namespace implementation {
44
Frame(uint32_t width,uint32_t height,uint32_t fourcc)45 Frame::Frame(uint32_t width, uint32_t height, uint32_t fourcc) :
46 mWidth(width), mHeight(height), mFourcc(fourcc) {}
47
V4L2Frame(uint32_t w,uint32_t h,uint32_t fourcc,int bufIdx,int fd,uint32_t dataSize,uint64_t offset)48 V4L2Frame::V4L2Frame(
49 uint32_t w, uint32_t h, uint32_t fourcc,
50 int bufIdx, int fd, uint32_t dataSize, uint64_t offset) :
51 Frame(w, h, fourcc),
52 mBufferIndex(bufIdx), mFd(fd), mDataSize(dataSize), mOffset(offset) {}
53
map(uint8_t ** data,size_t * dataSize)54 int V4L2Frame::map(uint8_t** data, size_t* dataSize) {
55 if (data == nullptr || dataSize == nullptr) {
56 ALOGI("%s: V4L2 buffer map bad argument: data %p, dataSize %p",
57 __FUNCTION__, data, dataSize);
58 return -EINVAL;
59 }
60
61 std::lock_guard<std::mutex> lk(mLock);
62 if (!mMapped) {
63 void* addr = mmap(NULL, mDataSize, PROT_READ, MAP_SHARED, mFd, mOffset);
64 if (addr == MAP_FAILED) {
65 ALOGE("%s: V4L2 buffer map failed: %s", __FUNCTION__, strerror(errno));
66 return -EINVAL;
67 }
68 mData = static_cast<uint8_t*>(addr);
69 mMapped = true;
70 }
71 *data = mData;
72 *dataSize = mDataSize;
73 ALOGV("%s: V4L map FD %d, data %p size %zu", __FUNCTION__, mFd, mData, mDataSize);
74 return 0;
75 }
76
unmap()77 int V4L2Frame::unmap() {
78 std::lock_guard<std::mutex> lk(mLock);
79 if (mMapped) {
80 ALOGV("%s: V4L unmap data %p size %zu", __FUNCTION__, mData, mDataSize);
81 if (munmap(mData, mDataSize) != 0) {
82 ALOGE("%s: V4L2 buffer unmap failed: %s", __FUNCTION__, strerror(errno));
83 return -EINVAL;
84 }
85 mMapped = false;
86 }
87 return 0;
88 }
89
~V4L2Frame()90 V4L2Frame::~V4L2Frame() {
91 unmap();
92 }
93
getData(uint8_t ** outData,size_t * dataSize)94 int V4L2Frame::getData(uint8_t** outData, size_t* dataSize) {
95 return map(outData, dataSize);
96 }
97
AllocatedFrame(uint32_t w,uint32_t h)98 AllocatedFrame::AllocatedFrame(
99 uint32_t w, uint32_t h) :
100 Frame(w, h, V4L2_PIX_FMT_YUV420) {};
101
~AllocatedFrame()102 AllocatedFrame::~AllocatedFrame() {}
103
allocate(YCbCrLayout * out)104 int AllocatedFrame::allocate(YCbCrLayout* out) {
105 std::lock_guard<std::mutex> lk(mLock);
106 if ((mWidth % 2) || (mHeight % 2)) {
107 ALOGE("%s: bad dimension %dx%d (not multiple of 2)", __FUNCTION__, mWidth, mHeight);
108 return -EINVAL;
109 }
110
111 // This frame might be sent to jpeglib to be encoded. Since AllocatedFrame only contains YUV420,
112 // jpeglib expects height and width of Y component to be an integral multiple of 2*DCTSIZE,
113 // and heights and widths of Cb and Cr components to be an integral multiple of DCTSIZE. If the
114 // image size does not meet this requirement, libjpeg expects its input to be padded to meet the
115 // constraints. This padding is removed from the final encoded image so the content in the
116 // padding doesn't matter. What matters is that the memory is accessible to jpeglib at the time
117 // of encoding.
118 // For example, if the image size is 1500x844 and DCTSIZE is 8, jpeglib expects a YUV 420
119 // frame with components of following sizes:
120 // Y: 1504x848 because 1504 and 848 are the next smallest multiples of 2*8
121 // Cb/Cr: 752x424 which are the next smallest multiples of 8
122
123 // jpeglib takes an array of row pointers which makes vertical padding trivial when setting up
124 // the pointers. Padding horizontally is a bit more complicated. AllocatedFrame holds the data
125 // in a flattened buffer, which means memory accesses past a row will flow into the next logical
126 // row. For any row of a component, we can consider the first few bytes of the next row as
127 // padding for the current one. This is true for Y and Cb components and all but last row of the
128 // Cr component. Reading past the last row of Cr component will lead to undefined behavior as
129 // libjpeg attempts to read memory past the allocated buffer. To prevent undefined behavior,
130 // the buffer allocated here is padded such that libjpeg never accesses unallocated memory when
131 // reading the last row. Effectively, we only need to ensure that the last row of Cr component
132 // has width that is an integral multiple of DCTSIZE.
133
134 size_t dataSize = mWidth * mHeight * 3 / 2; // YUV420
135
136 size_t cbWidth = mWidth / 2;
137 size_t requiredCbWidth = DCTSIZE * ((cbWidth + DCTSIZE - 1) / DCTSIZE);
138 size_t padding = requiredCbWidth - cbWidth;
139 size_t finalSize = dataSize + padding;
140
141 if (mData.size() != finalSize) {
142 mData.resize(finalSize);
143 }
144
145 if (out != nullptr) {
146 out->y = mData.data();
147 out->yStride = mWidth;
148 uint8_t* cbStart = mData.data() + mWidth * mHeight;
149 uint8_t* crStart = cbStart + mWidth * mHeight / 4;
150 out->cb = cbStart;
151 out->cr = crStart;
152 out->cStride = mWidth / 2;
153 out->chromaStep = 1;
154 }
155 return 0;
156 }
157
getData(uint8_t ** outData,size_t * dataSize)158 int AllocatedFrame::getData(uint8_t** outData, size_t* dataSize) {
159 YCbCrLayout layout;
160 int ret = allocate(&layout);
161 if (ret != 0) {
162 return ret;
163 }
164 *outData = mData.data();
165 *dataSize = mData.size();
166 return 0;
167 }
168
getLayout(YCbCrLayout * out)169 int AllocatedFrame::getLayout(YCbCrLayout* out) {
170 IMapper::Rect noCrop = {0, 0,
171 static_cast<int32_t>(mWidth),
172 static_cast<int32_t>(mHeight)};
173 return getCroppedLayout(noCrop, out);
174 }
175
getCroppedLayout(const IMapper::Rect & rect,YCbCrLayout * out)176 int AllocatedFrame::getCroppedLayout(const IMapper::Rect& rect, YCbCrLayout* out) {
177 if (out == nullptr) {
178 ALOGE("%s: null out", __FUNCTION__);
179 return -1;
180 }
181
182 std::lock_guard<std::mutex> lk(mLock);
183 if ((rect.left + rect.width) > static_cast<int>(mWidth) ||
184 (rect.top + rect.height) > static_cast<int>(mHeight) ||
185 (rect.left % 2) || (rect.top % 2) || (rect.width % 2) || (rect.height % 2)) {
186 ALOGE("%s: bad rect left %d top %d w %d h %d", __FUNCTION__,
187 rect.left, rect.top, rect.width, rect.height);
188 return -1;
189 }
190
191 out->y = mData.data() + mWidth * rect.top + rect.left;
192 out->yStride = mWidth;
193 uint8_t* cbStart = mData.data() + mWidth * mHeight;
194 uint8_t* crStart = cbStart + mWidth * mHeight / 4;
195 out->cb = cbStart + mWidth * rect.top / 4 + rect.left / 2;
196 out->cr = crStart + mWidth * rect.top / 4 + rect.left / 2;
197 out->cStride = mWidth / 2;
198 out->chromaStep = 1;
199 return 0;
200 }
201
isAspectRatioClose(float ar1,float ar2)202 bool isAspectRatioClose(float ar1, float ar2) {
203 const float kAspectRatioMatchThres = 0.025f; // This threshold is good enough to distinguish
204 // 4:3/16:9/20:9
205 // 1.33 / 1.78 / 2
206 return (std::abs(ar1 - ar2) < kAspectRatioMatchThres);
207 }
208
getDouble() const209 double SupportedV4L2Format::FrameRate::getDouble() const {
210 return durationDenominator / static_cast<double>(durationNumerator);
211 }
212
importBufferImpl(std::map<int,CirculatingBuffers> & circulatingBuffers,HandleImporter & handleImporter,int32_t streamId,uint64_t bufId,buffer_handle_t buf,buffer_handle_t ** outBufPtr,bool allowEmptyBuf)213 ::android::hardware::camera::common::V1_0::Status importBufferImpl(
214 /*inout*/std::map<int, CirculatingBuffers>& circulatingBuffers,
215 /*inout*/HandleImporter& handleImporter,
216 int32_t streamId,
217 uint64_t bufId, buffer_handle_t buf,
218 /*out*/buffer_handle_t** outBufPtr,
219 bool allowEmptyBuf) {
220 using ::android::hardware::camera::common::V1_0::Status;
221 if (buf == nullptr && bufId == BUFFER_ID_NO_BUFFER) {
222 if (allowEmptyBuf) {
223 *outBufPtr = &sEmptyBuffer;
224 return Status::OK;
225 } else {
226 ALOGE("%s: bufferId %" PRIu64 " has null buffer handle!", __FUNCTION__, bufId);
227 return Status::ILLEGAL_ARGUMENT;
228 }
229 }
230
231 CirculatingBuffers& cbs = circulatingBuffers[streamId];
232 if (cbs.count(bufId) == 0) {
233 if (buf == nullptr) {
234 ALOGE("%s: bufferId %" PRIu64 " has null buffer handle!", __FUNCTION__, bufId);
235 return Status::ILLEGAL_ARGUMENT;
236 }
237 // Register a newly seen buffer
238 buffer_handle_t importedBuf = buf;
239 handleImporter.importBuffer(importedBuf);
240 if (importedBuf == nullptr) {
241 ALOGE("%s: output buffer for stream %d is invalid!", __FUNCTION__, streamId);
242 return Status::INTERNAL_ERROR;
243 } else {
244 cbs[bufId] = importedBuf;
245 }
246 }
247 *outBufPtr = &cbs[bufId];
248 return Status::OK;
249 }
250
getFourCcFromLayout(const YCbCrLayout & layout)251 uint32_t getFourCcFromLayout(const YCbCrLayout& layout) {
252 intptr_t cb = reinterpret_cast<intptr_t>(layout.cb);
253 intptr_t cr = reinterpret_cast<intptr_t>(layout.cr);
254 if (std::abs(cb - cr) == 1 && layout.chromaStep == 2) {
255 // Interleaved format
256 if (layout.cb > layout.cr) {
257 return V4L2_PIX_FMT_NV21;
258 } else {
259 return V4L2_PIX_FMT_NV12;
260 }
261 } else if (layout.chromaStep == 1) {
262 // Planar format
263 if (layout.cb > layout.cr) {
264 return V4L2_PIX_FMT_YVU420; // YV12
265 } else {
266 return V4L2_PIX_FMT_YUV420; // YU12
267 }
268 } else {
269 return FLEX_YUV_GENERIC;
270 }
271 }
272
getCropRect(CroppingType ct,const Size & inSize,const Size & outSize,IMapper::Rect * out)273 int getCropRect(
274 CroppingType ct, const Size& inSize, const Size& outSize, IMapper::Rect* out) {
275 if (out == nullptr) {
276 ALOGE("%s: out is null", __FUNCTION__);
277 return -1;
278 }
279
280 uint32_t inW = inSize.width;
281 uint32_t inH = inSize.height;
282 uint32_t outW = outSize.width;
283 uint32_t outH = outSize.height;
284
285 // Handle special case where aspect ratio is close to input but scaled
286 // dimension is slightly larger than input
287 float arIn = ASPECT_RATIO(inSize);
288 float arOut = ASPECT_RATIO(outSize);
289 if (isAspectRatioClose(arIn, arOut)) {
290 out->left = 0;
291 out->top = 0;
292 out->width = inW;
293 out->height = inH;
294 return 0;
295 }
296
297 if (ct == VERTICAL) {
298 uint64_t scaledOutH = static_cast<uint64_t>(outH) * inW / outW;
299 if (scaledOutH > inH) {
300 ALOGE("%s: Output size %dx%d cannot be vertically cropped from input size %dx%d",
301 __FUNCTION__, outW, outH, inW, inH);
302 return -1;
303 }
304 scaledOutH = scaledOutH & ~0x1; // make it multiple of 2
305
306 out->left = 0;
307 out->top = ((inH - scaledOutH) / 2) & ~0x1;
308 out->width = inW;
309 out->height = static_cast<int32_t>(scaledOutH);
310 ALOGV("%s: crop %dx%d to %dx%d: top %d, scaledH %d",
311 __FUNCTION__, inW, inH, outW, outH, out->top, static_cast<int32_t>(scaledOutH));
312 } else {
313 uint64_t scaledOutW = static_cast<uint64_t>(outW) * inH / outH;
314 if (scaledOutW > inW) {
315 ALOGE("%s: Output size %dx%d cannot be horizontally cropped from input size %dx%d",
316 __FUNCTION__, outW, outH, inW, inH);
317 return -1;
318 }
319 scaledOutW = scaledOutW & ~0x1; // make it multiple of 2
320
321 out->left = ((inW - scaledOutW) / 2) & ~0x1;
322 out->top = 0;
323 out->width = static_cast<int32_t>(scaledOutW);
324 out->height = inH;
325 ALOGV("%s: crop %dx%d to %dx%d: top %d, scaledW %d",
326 __FUNCTION__, inW, inH, outW, outH, out->top, static_cast<int32_t>(scaledOutW));
327 }
328
329 return 0;
330 }
331
formatConvert(const YCbCrLayout & in,const YCbCrLayout & out,Size sz,uint32_t format)332 int formatConvert(
333 const YCbCrLayout& in, const YCbCrLayout& out, Size sz, uint32_t format) {
334 int ret = 0;
335 switch (format) {
336 case V4L2_PIX_FMT_NV21:
337 ret = libyuv::I420ToNV21(
338 static_cast<uint8_t*>(in.y),
339 in.yStride,
340 static_cast<uint8_t*>(in.cb),
341 in.cStride,
342 static_cast<uint8_t*>(in.cr),
343 in.cStride,
344 static_cast<uint8_t*>(out.y),
345 out.yStride,
346 static_cast<uint8_t*>(out.cr),
347 out.cStride,
348 sz.width,
349 sz.height);
350 if (ret != 0) {
351 ALOGE("%s: convert to NV21 buffer failed! ret %d",
352 __FUNCTION__, ret);
353 return ret;
354 }
355 break;
356 case V4L2_PIX_FMT_NV12:
357 ret = libyuv::I420ToNV12(
358 static_cast<uint8_t*>(in.y),
359 in.yStride,
360 static_cast<uint8_t*>(in.cb),
361 in.cStride,
362 static_cast<uint8_t*>(in.cr),
363 in.cStride,
364 static_cast<uint8_t*>(out.y),
365 out.yStride,
366 static_cast<uint8_t*>(out.cb),
367 out.cStride,
368 sz.width,
369 sz.height);
370 if (ret != 0) {
371 ALOGE("%s: convert to NV12 buffer failed! ret %d",
372 __FUNCTION__, ret);
373 return ret;
374 }
375 break;
376 case V4L2_PIX_FMT_YVU420: // YV12
377 case V4L2_PIX_FMT_YUV420: // YU12
378 // TODO: maybe we can speed up here by somehow save this copy?
379 ret = libyuv::I420Copy(
380 static_cast<uint8_t*>(in.y),
381 in.yStride,
382 static_cast<uint8_t*>(in.cb),
383 in.cStride,
384 static_cast<uint8_t*>(in.cr),
385 in.cStride,
386 static_cast<uint8_t*>(out.y),
387 out.yStride,
388 static_cast<uint8_t*>(out.cb),
389 out.cStride,
390 static_cast<uint8_t*>(out.cr),
391 out.cStride,
392 sz.width,
393 sz.height);
394 if (ret != 0) {
395 ALOGE("%s: copy to YV12 or YU12 buffer failed! ret %d",
396 __FUNCTION__, ret);
397 return ret;
398 }
399 break;
400 case FLEX_YUV_GENERIC:
401 // TODO: b/72261744 write to arbitrary flexible YUV layout. Slow.
402 ALOGE("%s: unsupported flexible yuv layout"
403 " y %p cb %p cr %p y_str %d c_str %d c_step %d",
404 __FUNCTION__, out.y, out.cb, out.cr,
405 out.yStride, out.cStride, out.chromaStep);
406 return -1;
407 default:
408 ALOGE("%s: unknown YUV format 0x%x!", __FUNCTION__, format);
409 return -1;
410 }
411 return 0;
412 }
413
encodeJpegYU12(const Size & inSz,const YCbCrLayout & inLayout,int jpegQuality,const void * app1Buffer,size_t app1Size,void * out,const size_t maxOutSize,size_t & actualCodeSize)414 int encodeJpegYU12(
415 const Size & inSz, const YCbCrLayout& inLayout,
416 int jpegQuality, const void *app1Buffer, size_t app1Size,
417 void *out, const size_t maxOutSize, size_t &actualCodeSize)
418 {
419 /* libjpeg is a C library so we use C-style "inheritance" by
420 * putting libjpeg's jpeg_destination_mgr first in our custom
421 * struct. This allows us to cast jpeg_destination_mgr* to
422 * CustomJpegDestMgr* when we get it passed to us in a callback */
423 struct CustomJpegDestMgr {
424 struct jpeg_destination_mgr mgr;
425 JOCTET *mBuffer;
426 size_t mBufferSize;
427 size_t mEncodedSize;
428 bool mSuccess;
429 } dmgr;
430
431 jpeg_compress_struct cinfo = {};
432 jpeg_error_mgr jerr;
433
434 /* Initialize error handling with standard callbacks, but
435 * then override output_message (to print to ALOG) and
436 * error_exit to set a flag and print a message instead
437 * of killing the whole process */
438 cinfo.err = jpeg_std_error(&jerr);
439
440 cinfo.err->output_message = [](j_common_ptr cinfo) {
441 char buffer[JMSG_LENGTH_MAX];
442
443 /* Create the message */
444 (*cinfo->err->format_message)(cinfo, buffer);
445 ALOGE("libjpeg error: %s", buffer);
446 };
447 cinfo.err->error_exit = [](j_common_ptr cinfo) {
448 (*cinfo->err->output_message)(cinfo);
449 if(cinfo->client_data) {
450 auto & dmgr =
451 *reinterpret_cast<CustomJpegDestMgr*>(cinfo->client_data);
452 dmgr.mSuccess = false;
453 }
454 };
455 /* Now that we initialized some callbacks, let's create our compressor */
456 jpeg_create_compress(&cinfo);
457
458 /* Initialize our destination manager */
459 dmgr.mBuffer = static_cast<JOCTET*>(out);
460 dmgr.mBufferSize = maxOutSize;
461 dmgr.mEncodedSize = 0;
462 dmgr.mSuccess = true;
463 cinfo.client_data = static_cast<void*>(&dmgr);
464
465 /* These lambdas become C-style function pointers and as per C++11 spec
466 * may not capture anything */
467 dmgr.mgr.init_destination = [](j_compress_ptr cinfo) {
468 auto & dmgr = reinterpret_cast<CustomJpegDestMgr&>(*cinfo->dest);
469 dmgr.mgr.next_output_byte = dmgr.mBuffer;
470 dmgr.mgr.free_in_buffer = dmgr.mBufferSize;
471 ALOGV("%s:%d jpeg start: %p [%zu]",
472 __FUNCTION__, __LINE__, dmgr.mBuffer, dmgr.mBufferSize);
473 };
474
475 dmgr.mgr.empty_output_buffer = [](j_compress_ptr cinfo __unused) {
476 ALOGV("%s:%d Out of buffer", __FUNCTION__, __LINE__);
477 return 0;
478 };
479
480 dmgr.mgr.term_destination = [](j_compress_ptr cinfo) {
481 auto & dmgr = reinterpret_cast<CustomJpegDestMgr&>(*cinfo->dest);
482 dmgr.mEncodedSize = dmgr.mBufferSize - dmgr.mgr.free_in_buffer;
483 ALOGV("%s:%d Done with jpeg: %zu", __FUNCTION__, __LINE__, dmgr.mEncodedSize);
484 };
485 cinfo.dest = reinterpret_cast<struct jpeg_destination_mgr*>(&dmgr);
486
487 /* We are going to be using JPEG in raw data mode, so we are passing
488 * straight subsampled planar YCbCr and it will not touch our pixel
489 * data or do any scaling or anything */
490 cinfo.image_width = inSz.width;
491 cinfo.image_height = inSz.height;
492 cinfo.input_components = 3;
493 cinfo.in_color_space = JCS_YCbCr;
494
495 /* Initialize defaults and then override what we want */
496 jpeg_set_defaults(&cinfo);
497
498 jpeg_set_quality(&cinfo, jpegQuality, 1);
499 jpeg_set_colorspace(&cinfo, JCS_YCbCr);
500 cinfo.raw_data_in = 1;
501 cinfo.dct_method = JDCT_IFAST;
502
503 /* Configure sampling factors. The sampling factor is JPEG subsampling 420
504 * because the source format is YUV420. Note that libjpeg sampling factors
505 * are... a little weird. Sampling of Y=2,U=1,V=1 means there is 1 U and
506 * 1 V value for each 2 Y values */
507 cinfo.comp_info[0].h_samp_factor = 2;
508 cinfo.comp_info[0].v_samp_factor = 2;
509 cinfo.comp_info[1].h_samp_factor = 1;
510 cinfo.comp_info[1].v_samp_factor = 1;
511 cinfo.comp_info[2].h_samp_factor = 1;
512 cinfo.comp_info[2].v_samp_factor = 1;
513
514 /* Let's not hardcode YUV420 in 6 places... 5 was enough */
515 int maxVSampFactor = std::max( {
516 cinfo.comp_info[0].v_samp_factor,
517 cinfo.comp_info[1].v_samp_factor,
518 cinfo.comp_info[2].v_samp_factor
519 });
520 int cVSubSampling = cinfo.comp_info[0].v_samp_factor /
521 cinfo.comp_info[1].v_samp_factor;
522
523 /* Start the compressor */
524 jpeg_start_compress(&cinfo, TRUE);
525
526 /* Compute our macroblock height, so we can pad our input to be vertically
527 * macroblock aligned.
528 * TODO: Does it need to be horizontally MCU aligned too? */
529
530 size_t mcuV = DCTSIZE*maxVSampFactor;
531 size_t paddedHeight = mcuV * ((inSz.height + mcuV - 1) / mcuV);
532
533 /* libjpeg uses arrays of row pointers, which makes it really easy to pad
534 * data vertically (unfortunately doesn't help horizontally) */
535 std::vector<JSAMPROW> yLines (paddedHeight);
536 std::vector<JSAMPROW> cbLines(paddedHeight/cVSubSampling);
537 std::vector<JSAMPROW> crLines(paddedHeight/cVSubSampling);
538
539 uint8_t *py = static_cast<uint8_t*>(inLayout.y);
540 uint8_t *pcr = static_cast<uint8_t*>(inLayout.cr);
541 uint8_t *pcb = static_cast<uint8_t*>(inLayout.cb);
542
543 for(uint32_t i = 0; i < paddedHeight; i++)
544 {
545 /* Once we are in the padding territory we still point to the last line
546 * effectively replicating it several times ~ CLAMP_TO_EDGE */
547 int li = std::min(i, inSz.height - 1);
548 yLines[i] = static_cast<JSAMPROW>(py + li * inLayout.yStride);
549 if(i < paddedHeight / cVSubSampling)
550 {
551 li = std::min(i, (inSz.height - 1) / cVSubSampling);
552 crLines[i] = static_cast<JSAMPROW>(pcr + li * inLayout.cStride);
553 cbLines[i] = static_cast<JSAMPROW>(pcb + li * inLayout.cStride);
554 }
555 }
556
557 /* If APP1 data was passed in, use it */
558 if(app1Buffer && app1Size)
559 {
560 jpeg_write_marker(&cinfo, JPEG_APP0 + 1,
561 static_cast<const JOCTET*>(app1Buffer), app1Size);
562 }
563
564 /* While we still have padded height left to go, keep giving it one
565 * macroblock at a time. */
566 while (cinfo.next_scanline < cinfo.image_height) {
567 const uint32_t batchSize = DCTSIZE * maxVSampFactor;
568 const uint32_t nl = cinfo.next_scanline;
569 JSAMPARRAY planes[3]{ &yLines[nl],
570 &cbLines[nl/cVSubSampling],
571 &crLines[nl/cVSubSampling] };
572
573 uint32_t done = jpeg_write_raw_data(&cinfo, planes, batchSize);
574
575 if (done != batchSize) {
576 ALOGE("%s: compressed %u lines, expected %u (total %u/%u)",
577 __FUNCTION__, done, batchSize, cinfo.next_scanline,
578 cinfo.image_height);
579 return -1;
580 }
581 }
582
583 /* This will flush everything */
584 jpeg_finish_compress(&cinfo);
585
586 /* Grab the actual code size and set it */
587 actualCodeSize = dmgr.mEncodedSize;
588
589 return 0;
590 }
591
getMaxThumbnailResolution(const common::V1_0::helper::CameraMetadata & chars)592 Size getMaxThumbnailResolution(const common::V1_0::helper::CameraMetadata& chars) {
593 Size thumbSize { 0, 0 };
594 camera_metadata_ro_entry entry =
595 chars.find(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES);
596 for(uint32_t i = 0; i < entry.count; i += 2) {
597 Size sz { static_cast<uint32_t>(entry.data.i32[i]),
598 static_cast<uint32_t>(entry.data.i32[i+1]) };
599 if(sz.width * sz.height > thumbSize.width * thumbSize.height) {
600 thumbSize = sz;
601 }
602 }
603
604 if (thumbSize.width * thumbSize.height == 0) {
605 ALOGW("%s: non-zero thumbnail size not available", __FUNCTION__);
606 }
607
608 return thumbSize;
609 }
610
freeReleaseFences(hidl_vec<V3_2::CaptureResult> & results)611 void freeReleaseFences(hidl_vec<V3_2::CaptureResult>& results) {
612 for (auto& result : results) {
613 if (result.inputBuffer.releaseFence.getNativeHandle() != nullptr) {
614 native_handle_t* handle = const_cast<native_handle_t*>(
615 result.inputBuffer.releaseFence.getNativeHandle());
616 native_handle_close(handle);
617 native_handle_delete(handle);
618 }
619 for (auto& buf : result.outputBuffers) {
620 if (buf.releaseFence.getNativeHandle() != nullptr) {
621 native_handle_t* handle = const_cast<native_handle_t*>(
622 buf.releaseFence.getNativeHandle());
623 native_handle_close(handle);
624 native_handle_delete(handle);
625 }
626 }
627 }
628 return;
629 }
630
631 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
632 #define UPDATE(md, tag, data, size) \
633 do { \
634 if ((md).update((tag), (data), (size))) { \
635 ALOGE("Update " #tag " failed!"); \
636 return BAD_VALUE; \
637 } \
638 } while (0)
639
fillCaptureResultCommon(common::V1_0::helper::CameraMetadata & md,nsecs_t timestamp,camera_metadata_ro_entry & activeArraySize)640 status_t fillCaptureResultCommon(
641 common::V1_0::helper::CameraMetadata &md, nsecs_t timestamp,
642 camera_metadata_ro_entry& activeArraySize) {
643 if (activeArraySize.count < 4) {
644 ALOGE("%s: cannot find active array size!", __FUNCTION__);
645 return -EINVAL;
646 }
647 // android.control
648 // For USB camera, we don't know the AE state. Set the state to converged to
649 // indicate the frame should be good to use. Then apps don't have to wait the
650 // AE state.
651 const uint8_t aeState = ANDROID_CONTROL_AE_STATE_CONVERGED;
652 UPDATE(md, ANDROID_CONTROL_AE_STATE, &aeState, 1);
653
654 const uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
655 UPDATE(md, ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
656
657 // Set AWB state to converged to indicate the frame should be good to use.
658 const uint8_t awbState = ANDROID_CONTROL_AWB_STATE_CONVERGED;
659 UPDATE(md, ANDROID_CONTROL_AWB_STATE, &awbState, 1);
660
661 const uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF;
662 UPDATE(md, ANDROID_CONTROL_AWB_LOCK, &awbLock, 1);
663
664 const uint8_t flashState = ANDROID_FLASH_STATE_UNAVAILABLE;
665 UPDATE(md, ANDROID_FLASH_STATE, &flashState, 1);
666
667 // This means pipeline latency of X frame intervals. The maximum number is 4.
668 const uint8_t requestPipelineMaxDepth = 4;
669 UPDATE(md, ANDROID_REQUEST_PIPELINE_DEPTH, &requestPipelineMaxDepth, 1);
670
671 // android.scaler
672 const int32_t crop_region[] = {
673 activeArraySize.data.i32[0], activeArraySize.data.i32[1],
674 activeArraySize.data.i32[2], activeArraySize.data.i32[3],
675 };
676 UPDATE(md, ANDROID_SCALER_CROP_REGION, crop_region, ARRAY_SIZE(crop_region));
677
678 // android.sensor
679 UPDATE(md, ANDROID_SENSOR_TIMESTAMP, ×tamp, 1);
680
681 // android.statistics
682 const uint8_t lensShadingMapMode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
683 UPDATE(md, ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, &lensShadingMapMode, 1);
684
685 const uint8_t sceneFlicker = ANDROID_STATISTICS_SCENE_FLICKER_NONE;
686 UPDATE(md, ANDROID_STATISTICS_SCENE_FLICKER, &sceneFlicker, 1);
687
688 return OK;
689 }
690
691 #undef ARRAY_SIZE
692 #undef UPDATE
693
694 } // namespace implementation
695 } // namespace V3_4
696
697 namespace V3_6 {
698 namespace implementation {
699
AllocatedV4L2Frame(sp<V3_4::implementation::V4L2Frame> frameIn)700 AllocatedV4L2Frame::AllocatedV4L2Frame(sp<V3_4::implementation::V4L2Frame> frameIn) :
701 Frame(frameIn->mWidth, frameIn->mHeight, frameIn->mFourcc) {
702 uint8_t* dataIn;
703 size_t dataSize;
704 if (frameIn->getData(&dataIn, &dataSize) != 0) {
705 ALOGE("%s: map input V4L2 frame failed!", __FUNCTION__);
706 return;
707 }
708
709 mData.resize(dataSize);
710 std::memcpy(mData.data(), dataIn, dataSize);
711 }
712
getData(uint8_t ** outData,size_t * dataSize)713 int AllocatedV4L2Frame::getData(uint8_t** outData, size_t* dataSize) {
714 if (outData == nullptr || dataSize == nullptr) {
715 ALOGE("%s: outData(%p)/dataSize(%p) must not be null", __FUNCTION__, outData, dataSize);
716 return -1;
717 }
718
719 *outData = mData.data();
720 *dataSize = mData.size();
721 return 0;
722 }
723
~AllocatedV4L2Frame()724 AllocatedV4L2Frame::~AllocatedV4L2Frame() {}
725
726 } // namespace implementation
727 } // namespace V3_6
728 } // namespace device
729
730
731 namespace external {
732 namespace common {
733
734 namespace {
735 const int kDefaultCameraIdOffset = 100;
736 const int kDefaultJpegBufSize = 5 << 20; // 5MB
737 const int kDefaultNumVideoBuffer = 4;
738 const int kDefaultNumStillBuffer = 2;
739 const int kDefaultOrientation = 0; // suitable for natural landscape displays like tablet/TV
740 // For phone devices 270 is better
741 } // anonymous namespace
742
743 const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml";
744
loadFromCfg(const char * cfgPath)745 ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) {
746 using namespace tinyxml2;
747 ExternalCameraConfig ret;
748
749 XMLDocument configXml;
750 XMLError err = configXml.LoadFile(cfgPath);
751 if (err != XML_SUCCESS) {
752 ALOGE("%s: Unable to load external camera config file '%s'. Error: %s",
753 __FUNCTION__, cfgPath, XMLDocument::ErrorIDToName(err));
754 return ret;
755 } else {
756 ALOGI("%s: load external camera config succeed!", __FUNCTION__);
757 }
758
759 XMLElement *extCam = configXml.FirstChildElement("ExternalCamera");
760 if (extCam == nullptr) {
761 ALOGI("%s: no external camera config specified", __FUNCTION__);
762 return ret;
763 }
764
765 XMLElement *providerCfg = extCam->FirstChildElement("Provider");
766 if (providerCfg == nullptr) {
767 ALOGI("%s: no external camera provider config specified", __FUNCTION__);
768 return ret;
769 }
770
771 XMLElement *cameraIdOffset = providerCfg->FirstChildElement("CameraIdOffset");
772 if (cameraIdOffset != nullptr) {
773 ret.cameraIdOffset = std::atoi(cameraIdOffset->GetText());
774 }
775
776 XMLElement *ignore = providerCfg->FirstChildElement("ignore");
777 if (ignore == nullptr) {
778 ALOGI("%s: no internal ignored device specified", __FUNCTION__);
779 return ret;
780 }
781
782 XMLElement *id = ignore->FirstChildElement("id");
783 while (id != nullptr) {
784 const char* text = id->GetText();
785 if (text != nullptr) {
786 ret.mInternalDevices.insert(text);
787 ALOGI("%s: device %s will be ignored by external camera provider",
788 __FUNCTION__, text);
789 }
790 id = id->NextSiblingElement("id");
791 }
792
793 XMLElement *deviceCfg = extCam->FirstChildElement("Device");
794 if (deviceCfg == nullptr) {
795 ALOGI("%s: no external camera device config specified", __FUNCTION__);
796 return ret;
797 }
798
799 XMLElement *jpegBufSz = deviceCfg->FirstChildElement("MaxJpegBufferSize");
800 if (jpegBufSz == nullptr) {
801 ALOGI("%s: no max jpeg buffer size specified", __FUNCTION__);
802 } else {
803 ret.maxJpegBufSize = jpegBufSz->UnsignedAttribute("bytes", /*Default*/kDefaultJpegBufSize);
804 }
805
806 XMLElement *numVideoBuf = deviceCfg->FirstChildElement("NumVideoBuffers");
807 if (numVideoBuf == nullptr) {
808 ALOGI("%s: no num video buffers specified", __FUNCTION__);
809 } else {
810 ret.numVideoBuffers =
811 numVideoBuf->UnsignedAttribute("count", /*Default*/kDefaultNumVideoBuffer);
812 }
813
814 XMLElement *numStillBuf = deviceCfg->FirstChildElement("NumStillBuffers");
815 if (numStillBuf == nullptr) {
816 ALOGI("%s: no num still buffers specified", __FUNCTION__);
817 } else {
818 ret.numStillBuffers =
819 numStillBuf->UnsignedAttribute("count", /*Default*/kDefaultNumStillBuffer);
820 }
821
822 XMLElement *fpsList = deviceCfg->FirstChildElement("FpsList");
823 if (fpsList == nullptr) {
824 ALOGI("%s: no fps list specified", __FUNCTION__);
825 } else {
826 if (!updateFpsList(fpsList, ret.fpsLimits)) {
827 return ret;
828 }
829 }
830
831 XMLElement *depth = deviceCfg->FirstChildElement("Depth16Supported");
832 if (depth == nullptr) {
833 ret.depthEnabled = false;
834 ALOGI("%s: depth output is not enabled", __FUNCTION__);
835 } else {
836 ret.depthEnabled = depth->BoolAttribute("enabled", false);
837 }
838
839 if(ret.depthEnabled) {
840 XMLElement *depthFpsList = deviceCfg->FirstChildElement("DepthFpsList");
841 if (depthFpsList == nullptr) {
842 ALOGW("%s: no depth fps list specified", __FUNCTION__);
843 } else {
844 if(!updateFpsList(depthFpsList, ret.depthFpsLimits)) {
845 return ret;
846 }
847 }
848 }
849
850 XMLElement *minStreamSize = deviceCfg->FirstChildElement("MinimumStreamSize");
851 if (minStreamSize == nullptr) {
852 ALOGI("%s: no minimum stream size specified", __FUNCTION__);
853 } else {
854 ret.minStreamSize = {
855 minStreamSize->UnsignedAttribute("width", /*Default*/0),
856 minStreamSize->UnsignedAttribute("height", /*Default*/0)};
857 }
858
859 XMLElement *orientation = deviceCfg->FirstChildElement("Orientation");
860 if (orientation == nullptr) {
861 ALOGI("%s: no sensor orientation specified", __FUNCTION__);
862 } else {
863 ret.orientation = orientation->IntAttribute("degree", /*Default*/kDefaultOrientation);
864 }
865
866 ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d,"
867 " num video buffers %d, num still buffers %d, orientation %d",
868 __FUNCTION__, ret.maxJpegBufSize,
869 ret.numVideoBuffers, ret.numStillBuffers, ret.orientation);
870 for (const auto& limit : ret.fpsLimits) {
871 ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__,
872 limit.size.width, limit.size.height, limit.fpsUpperBound);
873 }
874 for (const auto& limit : ret.depthFpsLimits) {
875 ALOGI("%s: depthFpsLimitList: %dx%d@%f", __FUNCTION__, limit.size.width, limit.size.height,
876 limit.fpsUpperBound);
877 }
878 ALOGI("%s: minStreamSize: %dx%d" , __FUNCTION__,
879 ret.minStreamSize.width, ret.minStreamSize.height);
880 return ret;
881 }
882
updateFpsList(tinyxml2::XMLElement * fpsList,std::vector<FpsLimitation> & fpsLimits)883 bool ExternalCameraConfig::updateFpsList(tinyxml2::XMLElement* fpsList,
884 std::vector<FpsLimitation>& fpsLimits) {
885 using namespace tinyxml2;
886 std::vector<FpsLimitation> limits;
887 XMLElement* row = fpsList->FirstChildElement("Limit");
888 while (row != nullptr) {
889 FpsLimitation prevLimit{{0, 0}, 1000.0};
890 FpsLimitation limit;
891 limit.size = {row->UnsignedAttribute("width", /*Default*/ 0),
892 row->UnsignedAttribute("height", /*Default*/ 0)};
893 limit.fpsUpperBound = row->DoubleAttribute("fpsBound", /*Default*/ 1000.0);
894 if (limit.size.width <= prevLimit.size.width ||
895 limit.size.height <= prevLimit.size.height ||
896 limit.fpsUpperBound >= prevLimit.fpsUpperBound) {
897 ALOGE(
898 "%s: FPS limit list must have increasing size and decreasing fps!"
899 " Prev %dx%d@%f, Current %dx%d@%f",
900 __FUNCTION__, prevLimit.size.width, prevLimit.size.height, prevLimit.fpsUpperBound,
901 limit.size.width, limit.size.height, limit.fpsUpperBound);
902 return false;
903 }
904 limits.push_back(limit);
905 row = row->NextSiblingElement("Limit");
906 }
907 fpsLimits = limits;
908 return true;
909 }
910
ExternalCameraConfig()911 ExternalCameraConfig::ExternalCameraConfig() :
912 cameraIdOffset(kDefaultCameraIdOffset),
913 maxJpegBufSize(kDefaultJpegBufSize),
914 numVideoBuffers(kDefaultNumVideoBuffer),
915 numStillBuffers(kDefaultNumStillBuffer),
916 depthEnabled(false),
917 orientation(kDefaultOrientation) {
918 fpsLimits.push_back({/*Size*/{ 640, 480}, /*FPS upper bound*/30.0});
919 fpsLimits.push_back({/*Size*/{1280, 720}, /*FPS upper bound*/7.5});
920 fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/5.0});
921 minStreamSize = {0, 0};
922 }
923
924
925 } // namespace common
926 } // namespace external
927 } // namespace camera
928 } // namespace hardware
929 } // namespace android
930