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     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 
getData(uint8_t ** outData,size_t * dataSize)129 int 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 
getLayout(YCbCrLayout * out)140 int 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 
getCroppedLayout(const IMapper::Rect & rect,YCbCrLayout * out)147 int 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 
isAspectRatioClose(float ar1,float ar2)173 bool 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 }
179 
getDouble() const180 double SupportedV4L2Format::FrameRate::getDouble() const {
181     return durationDenominator / static_cast<double>(durationNumerator);
182 }
183 
importBufferImpl(std::map<int,CirculatingBuffers> & circulatingBuffers,HandleImporter & handleImporter,int32_t streamId,uint64_t bufId,buffer_handle_t buf,buffer_handle_t ** outBufPtr,bool allowEmptyBuf)184 ::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 
getFourCcFromLayout(const YCbCrLayout & layout)222 uint32_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 
getCropRect(CroppingType ct,const Size & inSize,const Size & outSize,IMapper::Rect * out)244 int 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 
formatConvert(const YCbCrLayout & in,const YCbCrLayout & out,Size sz,uint32_t format)303 int 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 
encodeJpegYU12(const Size & inSz,const YCbCrLayout & inLayout,int jpegQuality,const void * app1Buffer,size_t app1Size,void * out,const size_t maxOutSize,size_t & actualCodeSize)385 int 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             li = std::min(i, (inSz.height - 1) / cVSubSampling);
523             crLines[i] = static_cast<JSAMPROW>(pcr + li * inLayout.cStride);
524             cbLines[i] = static_cast<JSAMPROW>(pcb + li * inLayout.cStride);
525         }
526     }
527 
528     /* If APP1 data was passed in, use it */
529     if(app1Buffer && app1Size)
530     {
531         jpeg_write_marker(&cinfo, JPEG_APP0 + 1,
532              static_cast<const JOCTET*>(app1Buffer), app1Size);
533     }
534 
535     /* While we still have padded height left to go, keep giving it one
536      * macroblock at a time. */
537     while (cinfo.next_scanline < cinfo.image_height) {
538         const uint32_t batchSize = DCTSIZE * maxVSampFactor;
539         const uint32_t nl = cinfo.next_scanline;
540         JSAMPARRAY planes[3]{ &yLines[nl],
541                               &cbLines[nl/cVSubSampling],
542                               &crLines[nl/cVSubSampling] };
543 
544         uint32_t done = jpeg_write_raw_data(&cinfo, planes, batchSize);
545 
546         if (done != batchSize) {
547             ALOGE("%s: compressed %u lines, expected %u (total %u/%u)",
548               __FUNCTION__, done, batchSize, cinfo.next_scanline,
549               cinfo.image_height);
550             return -1;
551         }
552     }
553 
554     /* This will flush everything */
555     jpeg_finish_compress(&cinfo);
556 
557     /* Grab the actual code size and set it */
558     actualCodeSize = dmgr.mEncodedSize;
559 
560     return 0;
561 }
562 
getMaxThumbnailResolution(const common::V1_0::helper::CameraMetadata & chars)563 Size getMaxThumbnailResolution(const common::V1_0::helper::CameraMetadata& chars) {
564     Size thumbSize { 0, 0 };
565     camera_metadata_ro_entry entry =
566         chars.find(ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES);
567     for(uint32_t i = 0; i < entry.count; i += 2) {
568         Size sz { static_cast<uint32_t>(entry.data.i32[i]),
569                   static_cast<uint32_t>(entry.data.i32[i+1]) };
570         if(sz.width * sz.height > thumbSize.width * thumbSize.height) {
571             thumbSize = sz;
572         }
573     }
574 
575     if (thumbSize.width * thumbSize.height == 0) {
576         ALOGW("%s: non-zero thumbnail size not available", __FUNCTION__);
577     }
578 
579     return thumbSize;
580 }
581 
freeReleaseFences(hidl_vec<V3_2::CaptureResult> & results)582 void freeReleaseFences(hidl_vec<V3_2::CaptureResult>& results) {
583     for (auto& result : results) {
584         if (result.inputBuffer.releaseFence.getNativeHandle() != nullptr) {
585             native_handle_t* handle = const_cast<native_handle_t*>(
586                     result.inputBuffer.releaseFence.getNativeHandle());
587             native_handle_close(handle);
588             native_handle_delete(handle);
589         }
590         for (auto& buf : result.outputBuffers) {
591             if (buf.releaseFence.getNativeHandle() != nullptr) {
592                 native_handle_t* handle = const_cast<native_handle_t*>(
593                         buf.releaseFence.getNativeHandle());
594                 native_handle_close(handle);
595                 native_handle_delete(handle);
596             }
597         }
598     }
599     return;
600 }
601 
602 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
603 #define UPDATE(md, tag, data, size)               \
604 do {                                              \
605     if ((md).update((tag), (data), (size))) {     \
606         ALOGE("Update " #tag " failed!");         \
607         return BAD_VALUE;                         \
608     }                                             \
609 } while (0)
610 
fillCaptureResultCommon(common::V1_0::helper::CameraMetadata & md,nsecs_t timestamp,camera_metadata_ro_entry & activeArraySize)611 status_t fillCaptureResultCommon(
612         common::V1_0::helper::CameraMetadata &md, nsecs_t timestamp,
613         camera_metadata_ro_entry& activeArraySize) {
614     if (activeArraySize.count < 4) {
615         ALOGE("%s: cannot find active array size!", __FUNCTION__);
616         return -EINVAL;
617     }
618     // android.control
619     // For USB camera, we don't know the AE state. Set the state to converged to
620     // indicate the frame should be good to use. Then apps don't have to wait the
621     // AE state.
622     const uint8_t aeState = ANDROID_CONTROL_AE_STATE_CONVERGED;
623     UPDATE(md, ANDROID_CONTROL_AE_STATE, &aeState, 1);
624 
625     const uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
626     UPDATE(md, ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
627 
628     // Set AWB state to converged to indicate the frame should be good to use.
629     const uint8_t awbState = ANDROID_CONTROL_AWB_STATE_CONVERGED;
630     UPDATE(md, ANDROID_CONTROL_AWB_STATE, &awbState, 1);
631 
632     const uint8_t awbLock = ANDROID_CONTROL_AWB_LOCK_OFF;
633     UPDATE(md, ANDROID_CONTROL_AWB_LOCK, &awbLock, 1);
634 
635     const uint8_t flashState = ANDROID_FLASH_STATE_UNAVAILABLE;
636     UPDATE(md, ANDROID_FLASH_STATE, &flashState, 1);
637 
638     // This means pipeline latency of X frame intervals. The maximum number is 4.
639     const uint8_t requestPipelineMaxDepth = 4;
640     UPDATE(md, ANDROID_REQUEST_PIPELINE_DEPTH, &requestPipelineMaxDepth, 1);
641 
642     // android.scaler
643     const int32_t crop_region[] = {
644           activeArraySize.data.i32[0], activeArraySize.data.i32[1],
645           activeArraySize.data.i32[2], activeArraySize.data.i32[3],
646     };
647     UPDATE(md, ANDROID_SCALER_CROP_REGION, crop_region, ARRAY_SIZE(crop_region));
648 
649     // android.sensor
650     UPDATE(md, ANDROID_SENSOR_TIMESTAMP, &timestamp, 1);
651 
652     // android.statistics
653     const uint8_t lensShadingMapMode = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF;
654     UPDATE(md, ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, &lensShadingMapMode, 1);
655 
656     const uint8_t sceneFlicker = ANDROID_STATISTICS_SCENE_FLICKER_NONE;
657     UPDATE(md, ANDROID_STATISTICS_SCENE_FLICKER, &sceneFlicker, 1);
658 
659     return OK;
660 }
661 
662 #undef ARRAY_SIZE
663 #undef UPDATE
664 
665 }  // namespace implementation
666 }  // namespace V3_4
667 
668 namespace V3_6 {
669 namespace implementation {
670 
AllocatedV4L2Frame(sp<V3_4::implementation::V4L2Frame> frameIn)671 AllocatedV4L2Frame::AllocatedV4L2Frame(sp<V3_4::implementation::V4L2Frame> frameIn) :
672         Frame(frameIn->mWidth, frameIn->mHeight, frameIn->mFourcc) {
673     uint8_t* dataIn;
674     size_t dataSize;
675     if (frameIn->getData(&dataIn, &dataSize) != 0) {
676         ALOGE("%s: map input V4L2 frame failed!", __FUNCTION__);
677         return;
678     }
679 
680     mData.resize(dataSize);
681     std::memcpy(mData.data(), dataIn, dataSize);
682 }
683 
getData(uint8_t ** outData,size_t * dataSize)684 int AllocatedV4L2Frame::getData(uint8_t** outData, size_t* dataSize) {
685     if (outData == nullptr || dataSize == nullptr) {
686         ALOGE("%s: outData(%p)/dataSize(%p) must not be null", __FUNCTION__, outData, dataSize);
687         return -1;
688     }
689 
690     *outData = mData.data();
691     *dataSize = mData.size();
692     return 0;
693 }
694 
~AllocatedV4L2Frame()695 AllocatedV4L2Frame::~AllocatedV4L2Frame() {}
696 
697 }  // namespace implementation
698 }  // namespace V3_6
699 }  // namespace device
700 
701 
702 namespace external {
703 namespace common {
704 
705 namespace {
706     const int kDefaultCameraIdOffset = 100;
707     const int kDefaultJpegBufSize = 5 << 20; // 5MB
708     const int kDefaultNumVideoBuffer = 4;
709     const int kDefaultNumStillBuffer = 2;
710     const int kDefaultOrientation = 0; // suitable for natural landscape displays like tablet/TV
711                                        // For phone devices 270 is better
712 } // anonymous namespace
713 
714 const char* ExternalCameraConfig::kDefaultCfgPath = "/vendor/etc/external_camera_config.xml";
715 
loadFromCfg(const char * cfgPath)716 ExternalCameraConfig ExternalCameraConfig::loadFromCfg(const char* cfgPath) {
717     using namespace tinyxml2;
718     ExternalCameraConfig ret;
719 
720     XMLDocument configXml;
721     XMLError err = configXml.LoadFile(cfgPath);
722     if (err != XML_SUCCESS) {
723         ALOGE("%s: Unable to load external camera config file '%s'. Error: %s",
724                 __FUNCTION__, cfgPath, XMLDocument::ErrorIDToName(err));
725         return ret;
726     } else {
727         ALOGI("%s: load external camera config succeed!", __FUNCTION__);
728     }
729 
730     XMLElement *extCam = configXml.FirstChildElement("ExternalCamera");
731     if (extCam == nullptr) {
732         ALOGI("%s: no external camera config specified", __FUNCTION__);
733         return ret;
734     }
735 
736     XMLElement *providerCfg = extCam->FirstChildElement("Provider");
737     if (providerCfg == nullptr) {
738         ALOGI("%s: no external camera provider config specified", __FUNCTION__);
739         return ret;
740     }
741 
742     XMLElement *cameraIdOffset = providerCfg->FirstChildElement("CameraIdOffset");
743     if (cameraIdOffset != nullptr) {
744         ret.cameraIdOffset = std::atoi(cameraIdOffset->GetText());
745     }
746 
747     XMLElement *ignore = providerCfg->FirstChildElement("ignore");
748     if (ignore == nullptr) {
749         ALOGI("%s: no internal ignored device specified", __FUNCTION__);
750         return ret;
751     }
752 
753     XMLElement *id = ignore->FirstChildElement("id");
754     while (id != nullptr) {
755         const char* text = id->GetText();
756         if (text != nullptr) {
757             ret.mInternalDevices.insert(text);
758             ALOGI("%s: device %s will be ignored by external camera provider",
759                     __FUNCTION__, text);
760         }
761         id = id->NextSiblingElement("id");
762     }
763 
764     XMLElement *deviceCfg = extCam->FirstChildElement("Device");
765     if (deviceCfg == nullptr) {
766         ALOGI("%s: no external camera device config specified", __FUNCTION__);
767         return ret;
768     }
769 
770     XMLElement *jpegBufSz = deviceCfg->FirstChildElement("MaxJpegBufferSize");
771     if (jpegBufSz == nullptr) {
772         ALOGI("%s: no max jpeg buffer size specified", __FUNCTION__);
773     } else {
774         ret.maxJpegBufSize = jpegBufSz->UnsignedAttribute("bytes", /*Default*/kDefaultJpegBufSize);
775     }
776 
777     XMLElement *numVideoBuf = deviceCfg->FirstChildElement("NumVideoBuffers");
778     if (numVideoBuf == nullptr) {
779         ALOGI("%s: no num video buffers specified", __FUNCTION__);
780     } else {
781         ret.numVideoBuffers =
782                 numVideoBuf->UnsignedAttribute("count", /*Default*/kDefaultNumVideoBuffer);
783     }
784 
785     XMLElement *numStillBuf = deviceCfg->FirstChildElement("NumStillBuffers");
786     if (numStillBuf == nullptr) {
787         ALOGI("%s: no num still buffers specified", __FUNCTION__);
788     } else {
789         ret.numStillBuffers =
790                 numStillBuf->UnsignedAttribute("count", /*Default*/kDefaultNumStillBuffer);
791     }
792 
793     XMLElement *fpsList = deviceCfg->FirstChildElement("FpsList");
794     if (fpsList == nullptr) {
795         ALOGI("%s: no fps list specified", __FUNCTION__);
796     } else {
797         if (!updateFpsList(fpsList, ret.fpsLimits)) {
798             return ret;
799         }
800     }
801 
802     XMLElement *depth = deviceCfg->FirstChildElement("Depth16Supported");
803     if (depth == nullptr) {
804         ret.depthEnabled = false;
805         ALOGI("%s: depth output is not enabled", __FUNCTION__);
806     } else {
807         ret.depthEnabled = depth->BoolAttribute("enabled", false);
808     }
809 
810     if(ret.depthEnabled) {
811         XMLElement *depthFpsList = deviceCfg->FirstChildElement("DepthFpsList");
812         if (depthFpsList == nullptr) {
813             ALOGW("%s: no depth fps list specified", __FUNCTION__);
814         } else {
815             if(!updateFpsList(depthFpsList, ret.depthFpsLimits)) {
816                 return ret;
817             }
818         }
819     }
820 
821     XMLElement *minStreamSize = deviceCfg->FirstChildElement("MinimumStreamSize");
822     if (minStreamSize == nullptr) {
823        ALOGI("%s: no minimum stream size specified", __FUNCTION__);
824     } else {
825         ret.minStreamSize = {
826                 minStreamSize->UnsignedAttribute("width", /*Default*/0),
827                 minStreamSize->UnsignedAttribute("height", /*Default*/0)};
828     }
829 
830     XMLElement *orientation = deviceCfg->FirstChildElement("Orientation");
831     if (orientation == nullptr) {
832         ALOGI("%s: no sensor orientation specified", __FUNCTION__);
833     } else {
834         ret.orientation = orientation->IntAttribute("degree", /*Default*/kDefaultOrientation);
835     }
836 
837     ALOGI("%s: external camera cfg loaded: maxJpgBufSize %d,"
838             " num video buffers %d, num still buffers %d, orientation %d",
839             __FUNCTION__, ret.maxJpegBufSize,
840             ret.numVideoBuffers, ret.numStillBuffers, ret.orientation);
841     for (const auto& limit : ret.fpsLimits) {
842         ALOGI("%s: fpsLimitList: %dx%d@%f", __FUNCTION__,
843                 limit.size.width, limit.size.height, limit.fpsUpperBound);
844     }
845     for (const auto& limit : ret.depthFpsLimits) {
846         ALOGI("%s: depthFpsLimitList: %dx%d@%f", __FUNCTION__, limit.size.width, limit.size.height,
847               limit.fpsUpperBound);
848     }
849     ALOGI("%s: minStreamSize: %dx%d" , __FUNCTION__,
850          ret.minStreamSize.width, ret.minStreamSize.height);
851     return ret;
852 }
853 
updateFpsList(tinyxml2::XMLElement * fpsList,std::vector<FpsLimitation> & fpsLimits)854 bool ExternalCameraConfig::updateFpsList(tinyxml2::XMLElement* fpsList,
855         std::vector<FpsLimitation>& fpsLimits) {
856     using namespace tinyxml2;
857     std::vector<FpsLimitation> limits;
858     XMLElement* row = fpsList->FirstChildElement("Limit");
859     while (row != nullptr) {
860         FpsLimitation prevLimit{{0, 0}, 1000.0};
861         FpsLimitation limit;
862         limit.size = {row->UnsignedAttribute("width", /*Default*/ 0),
863                       row->UnsignedAttribute("height", /*Default*/ 0)};
864         limit.fpsUpperBound = row->DoubleAttribute("fpsBound", /*Default*/ 1000.0);
865         if (limit.size.width <= prevLimit.size.width ||
866             limit.size.height <= prevLimit.size.height ||
867             limit.fpsUpperBound >= prevLimit.fpsUpperBound) {
868             ALOGE(
869                 "%s: FPS limit list must have increasing size and decreasing fps!"
870                 " Prev %dx%d@%f, Current %dx%d@%f",
871                 __FUNCTION__, prevLimit.size.width, prevLimit.size.height, prevLimit.fpsUpperBound,
872                 limit.size.width, limit.size.height, limit.fpsUpperBound);
873             return false;
874         }
875         limits.push_back(limit);
876         row = row->NextSiblingElement("Limit");
877     }
878     fpsLimits = limits;
879     return true;
880 }
881 
ExternalCameraConfig()882 ExternalCameraConfig::ExternalCameraConfig() :
883         cameraIdOffset(kDefaultCameraIdOffset),
884         maxJpegBufSize(kDefaultJpegBufSize),
885         numVideoBuffers(kDefaultNumVideoBuffer),
886         numStillBuffers(kDefaultNumStillBuffer),
887         depthEnabled(false),
888         orientation(kDefaultOrientation) {
889     fpsLimits.push_back({/*Size*/{ 640,  480}, /*FPS upper bound*/30.0});
890     fpsLimits.push_back({/*Size*/{1280,  720}, /*FPS upper bound*/7.5});
891     fpsLimits.push_back({/*Size*/{1920, 1080}, /*FPS upper bound*/5.0});
892     minStreamSize = {0, 0};
893 }
894 
895 
896 }  // namespace common
897 }  // namespace external
898 }  // namespace camera
899 }  // namespace hardware
900 }  // namespace android
901