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