camera左右镜像功能实现

版本 Android 12 USB camera
1、` …/3.4/default/ExternalCameraDevice.cpp | 28 +++±
…/default/ExternalCameraDeviceSession.cpp | 116 +++++++++++++++±-
…/ExternalCameraDeviceSession_3.4.h | 2 +
3 files changed, 133 insertions(+), 13 deletions(-)

diff --git a/hardware/interfaces/camera/device/3.4/default/ExternalCameraDevice.cpp b/hardware/interfaces/camera/device/3.4/default/ExternalCameraDevice.cpp
index f4dd6d4b8b…a55f3e8735 100755
— a/hardware/interfaces/camera/device/3.4/default/ExternalCameraDevice.cpp
+++ b/hardware/interfaces/camera/device/3.4/default/ExternalCameraDevice.cpp
@@ -24,6 +24,7 @@
#include <linux/videodev2.h>
#include <linux/v4l2-subdev.h>
#include <linux/videodev2.h>
+#include <cutils/properties.h>
#include “android-base/macros.h”
#include “CameraMetadata.h”
#include “…/…/3.2/default/include/convert.h”
@@ -43,6 +44,8 @@ namespace V3_4 {
namespace implementation {

namespace {
+

  • static bool mMirrorCameraEnable = false;
    // Only support MJPEG for now as it seems to be the one supports higher fps
    // Other formats to consider in the future:
    // * V4L2_PIX_FMT_YVU420 (== YV12)
    @@ -82,9 +85,18 @@ ExternalCameraDevice::ExternalCameraDevice(
    } else {
    ALOGE(“%s: device path match failed for %s”, FUNCTION, mDevicePath.c_str());
    }
  • char value[92];
  • property_get(“mirror.front.camera.enable”, value, “0”);
  • if (atoi(value)) {
  •    mMirrorCameraEnable = true;
    
  • }

}

-ExternalCameraDevice::~ExternalCameraDevice() {}
+ExternalCameraDevice::~ExternalCameraDevice() {

  • mMirrorCameraEnable = false;
    +}

bool ExternalCameraDevice::isInitFailed() {
Mutex::Autolock _l(mLock);
@@ -444,8 +456,18 @@ status_t ExternalCameraDevice::initDefaultCharsKeys(
UPDATE(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
&opticalStabilizationMode, 1);

  • const uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
  • UPDATE(ANDROID_LENS_FACING, &facing, 1);
  • ALOGD(“ExternalCameraDevice ----$$$$----!!”);

  • /*if(mMirrorCameraEnable){

  •   const char* dev = mCameraId.c_str();
    
  •   int index = atoi(dev);
    
  •   ALOGE("dev:%s index:%d", dev, index);
    
  •   const uint8_t facing = (index / 2) % 2 ? ANDROID_LENS_FACING_FRONT : ANDROID_LENS_FACING_BACK;
    
  •   ALOGD("ExternalCameraDevice ----$$$$----!! (index / 2) % 2 == %d",(index / 2) % 2);
    
  •   UPDATE(ANDROID_LENS_FACING, &facing, 1);		
    
  • }else{*/

  •   const uint8_t facing = ANDROID_LENS_FACING_EXTERNAL;
    
  •   UPDATE(ANDROID_LENS_FACING, &facing, 1);
    
  • //}

    // android.noiseReduction
    const uint8_t noiseReductionMode = ANDROID_NOISE_REDUCTION_MODE_OFF;
    diff --git a/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp b/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
    index cb13e2c131…3ed8052f7c 100755
    — a/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
    +++ b/hardware/interfaces/camera/device/3.4/default/ExternalCameraDeviceSession.cpp
    @@ -43,7 +43,7 @@
    #include <fcntl.h>
    #include <poll.h>
    #include <unistd.h>

+#include <cutils/properties.h>

#include “ExternalCameraDevice_3_4.h”
#include “subvideo.h”
@@ -102,6 +102,7 @@ constexpr int IOCTL_RETRY_SLEEP_US = 33000; // 33ms * MAX_RETRY = 0.5 seconds
// Constants for tryLock during dumpstate
static constexpr int kDumpLockRetries = 50;
static constexpr int kDumpLockSleep = 60000;
+static bool mMirrorCameraEnable = false;

bool tryLock(Mutex& mutex)
{
@@ -309,6 +310,13 @@ ExternalCameraDeviceSession::ExternalCameraDeviceSession(
}
ALOGD(“@%s,mCameraId:%s,mSubDevice:%d”,FUNCTION,mCameraId.c_str(),mSubDevice);
#endif
+

  •   char value[92];
    
  •   //ALOGD("ExternalCameraDeviceSession ----000----!!");
    
  •   property_get("mirror.front.camera.enable", value, "0");
    
  •   if (atoi(value)) {
    
  •   	mMirrorCameraEnable = true;
    
  •   }
       }
    

void ExternalCameraDeviceSession::createPreviewBuffer() {
@@ -484,6 +492,7 @@ ExternalCameraDeviceSession::~ExternalCameraDeviceSession() {
ALOGE(“ExternalCameraDeviceSession deleted before close!”);
close(/callerIsDtor/true);
}

  • mMirrorCameraEnable = false;
    }

@@ -2448,9 +2457,59 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
input.yStride = tempFrameWidth; //mYu12Frame->mWidth;
input.cb = (uint8_t*)(req->mVirAddr) + tempFrameWidth * tempFrameHeight;
input.cStride = tempFrameWidth; //mYu12Frame->mWidth;

  •    LOGD("format is BLOB or YV12, use software NV12ToI420");
    
  •    ALOGD("format is BLOB or YV12, use software NV12ToI420");
       ATRACE_BEGIN("NV12toI420");
    
  •    int res = libyuv::NV12ToI420(
    
  •   //ALOGD("ExternalCameraDeviceSession ----00----!!");
    
  •   if(mMirrorCameraEnable){
    
  •   	int res = libyuv::NV12ToI420(
    
  •            static_cast<uint8_t*>(input.y),
    
  •            input.yStride,
    
  •            static_cast<uint8_t*>(input.cb),
    
  •            input.cStride,
    
  •   		static_cast<uint8_t*>(mInternalYu12FrameLayout.y),
    
  •            mInternalYu12FrameLayout.yStride,
    
  •            static_cast<uint8_t*>(mInternalYu12FrameLayout.cb),
    
  •            mInternalYu12FrameLayout.cStride,
    
  •            static_cast<uint8_t*>(mInternalYu12FrameLayout.cr),
    
  •            mInternalYu12FrameLayout.cStride,
    
  •            mInternalYu12Frame->mWidth, mInternalYu12Frame->mHeight);
    
  •   		if (res == 0) {
    
  •   			camera_metadata_ro_entry entry = mCameraCharacteristics.find(ANDROID_SENSOR_ORIENTATION);
    
  •   			int orientation = entry.data.i32[0];
    
  •   			entry = mCameraCharacteristics.find(ANDROID_LENS_FACING);
    
  •   			int facing = entry.data.u8[0];
    
  •   			ALOGE("sensor orientation:%d facing:%d,ANDROID_LENS_FACING_FRONT:%d", orientation, facing, ANDROID_LENS_FACING_FRONT);
    
  •   			if (facing == ANDROID_LENS_FACING_FRONT) {
    
  •   				/*ALOGE("y:%d,yStride:%d,cb:%d,cr:%d,cStride:%d", mInternalYu12FrameLayout.y, 
    
  •   					mInternalYu12FrameLayout.yStride, mInternalYu12FrameLayout.cb, mInternalYu12FrameLayout.cr,
    
  •   					mInternalYu12FrameLayout.cStride);
    
  •   				ALOGE("mWidth:%d,mHeight:%d", mInternalYu12Frame->mWidth, mInternalYu12Frame->mHeight);*/
    
  •   				if (orientation % 180) {
    
  •   					ALOGE("I420Scale");
    
  •   					res = libyuv::I420Scale(static_cast<uint8_t*>(mInternalYu12FrameLayout.y), mInternalYu12FrameLayout.yStride,
    
  •   						static_cast<uint8_t*>(mInternalYu12FrameLayout.cb), mInternalYu12FrameLayout.cStride,
    
  •   						static_cast<uint8_t*>(mInternalYu12FrameLayout.cr), mInternalYu12FrameLayout.cStride,
    
  •   						mInternalYu12Frame->mWidth, mInternalYu12Frame->mHeight,
    
  •   						static_cast<uint8_t*>(mYu12FrameLayout.y), mYu12FrameLayout.yStride,
    
  •   						static_cast<uint8_t*>(mYu12FrameLayout.cb), mYu12FrameLayout.cStride,
    
  •   						static_cast<uint8_t*>(mYu12FrameLayout.cr), mYu12FrameLayout.cStride,
    
  •   						mYu12Frame->mWidth, mYu12Frame->mHeight
    
  •   						,libyuv::kFilterNone);
    
  •   					}
    
  •   				else {
    
  •   					ALOGE("I420Mirror");
    
  •   					res = libyuv::I420Mirror(static_cast<uint8_t*>(mInternalYu12FrameLayout.y), mInternalYu12FrameLayout.yStride,
    
  •   						static_cast<uint8_t*>(mInternalYu12FrameLayout.cb), mInternalYu12FrameLayout.cStride,
    
  •   						static_cast<uint8_t*>(mInternalYu12FrameLayout.cr), mInternalYu12FrameLayout.cStride,
    
  •   						static_cast<uint8_t*>(mYu12FrameLayout.y), mYu12FrameLayout.yStride,
    
  •   						static_cast<uint8_t*>(mYu12FrameLayout.cb), mYu12FrameLayout.cStride,
    
  •   						static_cast<uint8_t*>(mYu12FrameLayout.cr), mYu12FrameLayout.cStride,
    
  •   						mYu12Frame->mWidth, mYu12Frame->mHeight);
    
  •   					}
    
  •   			}
    
  •   		}
    
  •   }else{
    
  •   	int res = libyuv::NV12ToI420(
               static_cast<uint8_t*>(input.y),
               input.yStride,
               static_cast<uint8_t*>(input.cb),
    

@@ -2462,6 +2521,8 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
static_cast<uint8_t*>(mYu12TempLayout.cr),
mYu12TempLayout.cStride,
mTempYu12Frame->mWidth, mTempYu12Frame->mHeight);

  •   }
    
  •    ATRACE_END();
       IMapper::Rect inputCrop;
       inputCrop.left = mapleft;
    

@@ -2497,6 +2558,7 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
libyuv::FilterMode::kFilterNone);

     if (res != 0) {
  •   	//ALOGD("ExternalCameraDeviceSession ----11----!!");
           // For some webcam, the first few V4L2 frames might be malformed...
           ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res);
           lk.unlock();
    

@@ -2535,6 +2597,7 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
ATRACE_END();

    if (res != 0) {
  •   	//ALOGD("ExternalCameraDeviceSession ----22----!!");
           // For some webcam, the first few V4L2 frames might be malformed...
           ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res);
           lk.unlock();
    

@@ -2572,6 +2635,7 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
ATRACE_END();

    if (res != 0) {
  •       //ALOGD("ExternalCameraDeviceSession ----33----!!");
           // For some webcam, the first few V4L2 frames might be malformed...
           ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res);
           lk.unlock();
    

@@ -2610,6 +2674,7 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
ATRACE_END();

    if (res != 0) {
  •   	//ALOGD("ExternalCameraDeviceSession ----44----!!");
           // For some webcam, the first few V4L2 frames might be malformed...
           ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res);
           lk.unlock();
    

@@ -2647,6 +2712,7 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
ATRACE_END();

    if (res != 0) {
  •       //ALOGD("ExternalCameraDeviceSession ----55----!!");
           // For some webcam, the first few V4L2 frames might be malformed...
           ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, res);
           lk.unlock();
    

@@ -2676,6 +2742,7 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
mYu12Frame->mWidth, mYu12Frame->mHeight);
ATRACE_END();
if (ret != 0) {

  •   	//ALOGD("ExternalCameraDeviceSession ----66----!!");
           // For some webcam, the first few V4L2 frames might be malformed...
           ALOGE("%s: Convert V4L2 frame to YU12 failed! res %d", __FUNCTION__, ret);
           lk.unlock();
    

@@ -3076,13 +3143,32 @@ bool ExternalCameraDeviceSession::OutputThread::threadLoop() {
}
isJpegNeedCropScale = false;
} else {

  •                camera2::RgaCropScale::rga_scale_crop(
    
  •                    tempFrameWidth, tempFrameHeight, req->mShareFd,
    
  •                    HAL_PIXEL_FORMAT_YCrCb_NV12, handle_fd,
    
  •                    halBuf.width, halBuf.height, 100, false, true,
    
  •                    (halBuf.format == PixelFormat::YCRCB_420_SP), is16Align,
    
  •                    req->frameIn->mFourcc == V4L2_PIX_FMT_YUYV);
    
  •                ALOGV("%s: ANDROID_SCALER_CROP_REGION not set",__FUNCTION__);
    
  •   			//ALOGD("ExternalCameraDeviceSession ----%%%%0----!!");
    
  •   			if(mMirrorCameraEnable){
    
  •   				/*camera_metadata_ro_entry entry = mCameraCharacteristics.find(ANDROID_LENS_FACING);
    
  •   				int facing = entry.data.u8[0];
    
  •   				bool isFront = facing == ANDROID_LENS_FACING_EXTERNAL;
    
  •   				bool isVideo = !((halBuf.usage & GRALLOC_USAGE_HW_TEXTURE) == GRALLOC_USAGE_HW_TEXTURE);
    
  •   				bool mirror = (isFront) && (isVideo);
    
  •   				ALOGV("---zc usage:%" PRIx64, static_cast<uint64_t>(halBuf.usage));
    
  •   				ALOGV("---zc GRALLOC_USAGE_HW_TEXTURE:%x,VIDEO_ENCODER:%x", GRALLOC_USAGE_HW_TEXTURE, BufferUsage::VIDEO_ENCODER);
    
  •   				ALOGV("---zc isFront:%d,isVideo:%d,mirror:%d", isFront, isVideo, mirror);*/
    
  •   				camera2::RgaCropScale::rga_scale_crop(
    
  •   					tempFrameWidth, tempFrameHeight, req->mShareFd,
    
  •   					HAL_PIXEL_FORMAT_YCrCb_NV12, handle_fd,
    
  •   					halBuf.width, halBuf.height, 100, true, true,
    
  •   					(halBuf.format == PixelFormat::YCRCB_420_SP), is16Align,
    
  •   					req->frameIn->mFourcc == V4L2_PIX_FMT_YUYV);
    
  •   				ALOGV("%s: ANDROID_SCALER_CROP_REGION not set",__FUNCTION__);						
    
  •   			}else{
    
  •   				camera2::RgaCropScale::rga_scale_crop(
    
  •   					tempFrameWidth, tempFrameHeight, req->mShareFd,
    
  •   					HAL_PIXEL_FORMAT_YCrCb_NV12, handle_fd,
    
  •   					halBuf.width, halBuf.height, 100, false, true,
    
  •   					(halBuf.format == PixelFormat::YCRCB_420_SP), is16Align,
    
  •   					req->frameIn->mFourcc == V4L2_PIX_FMT_YUYV);
    
  •   				ALOGV("%s: ANDROID_SCALER_CROP_REGION not set",__FUNCTION__);						
    
  •   			}
               }
    

#ifdef DUMP_YUV
{
@@ -3170,6 +3256,16 @@ Status ExternalCameraDeviceSession::OutputThread::allocateIntermediateBuffers(
}
}

  • //ALOGD(“ExternalCameraDeviceSession ----77----!!”);
  • if(mMirrorCameraEnable){
  •   mInternalYu12Frame.clear();
    
  •   mInternalYu12Frame = new AllocatedFrame(v4lSize.width, v4lSize.height);
    
  •   int ret = mInternalYu12Frame->allocate(&mInternalYu12FrameLayout);
    
  •   if (ret != 0) {
    
  •   	ALOGE("%s: allocating YU12 frame failed!", __FUNCTION__);
    
  •   }	
    
  • }
  • // Allocating intermediate YU12 thumbnail frame
    if (mYu12ThumbFrame == nullptr ||
    mYu12ThumbFrame->mWidth != thumbSize.width ||
    diff --git a/hardware/interfaces/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraDeviceSession_3.4.h b/hardware/interfaces/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraDeviceSession_3.4.h
    index 0a621a4b60…93086bffec 100755
    — a/hardware/interfaces/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraDeviceSession_3.4.h
    +++ b/hardware/interfaces/camera/device/3.4/default/include/ext_device_v3_4_impl/ExternalCameraDeviceSession_3.4.h
    @@ -188,11 +188,13 @@ struct ExternalCameraDeviceSession : public virtual RefBase,
    sp mYu12Frame;
    sp mYu12ThumbFrame;
    sp mTempYu12Frame;
  •   sp<AllocatedFrame> mInternalYu12Frame;
       std::unordered_map<Size, sp<AllocatedFrame>, SizeHasher> mIntermediateBuffers;
       std::unordered_map<Size, sp<AllocatedFrame>, SizeHasher> mScaledYu12Frames;
       YCbCrLayout mYu12FrameLayout;
       YCbCrLayout mYu12ThumbFrameLayout;
       YCbCrLayout mYu12TempLayout;
    
  •   YCbCrLayout mInternalYu12FrameLayout;
       uint32_t mBlobBufferSize = 0; // 0 -> HAL derive buffer size, else: use given size
    
       std::string mExifMake;
    


2.25.1

`

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值