From patchwork Mon Feb 3 16:58:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 2767 Return-Path: Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 90C9D60446 for ; Mon, 3 Feb 2020 18:01:52 +0100 (CET) Received: by mail-wr1-x42e.google.com with SMTP id z7so19116671wrl.13 for ; Mon, 03 Feb 2020 09:01:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id; bh=ZI2JDNB1QiPO75CAKN+FCqsulC4Cld4Cl0VPe1af0NM=; b=fZ5cEPYItcsX2B4ghYOLeNjSt+78Zg2LUEcqsZtpBJB6n9jDezhyM2RFGfZDZYRx6e kpXjwiNXU29SzDCBhL/j/fV0hbMYhA4ga9sAlZ/4JQkiZibU7JmJRMb3RFYL+b64dVi9 V189m3FSt0G4PFOVcv3kL4/g7Nuo75ysF7ApncsCB//FZ06UQd+q8oAvNwyVWD8n1sJ3 eP+RlsKN4CauJZy/SEGTjdwTMS+7NpnAUXQ8PtBruyQsHjYnT1sFIW1hsY53XizwvbzT 1hbSR2+Gte2ZW6w94mP1o0YmEXxJYwZbsrGS1pU2qJBMjp3GI3EMy0Fo4disdXrIkBEK RgMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ZI2JDNB1QiPO75CAKN+FCqsulC4Cld4Cl0VPe1af0NM=; b=rs/7S+DdJ2hOmPtlifhdwbzOaGgIk7FgHicl7bxV6oAHWZ9TCOXu9Nrh1r0juc3gl9 ri73y/5BBcat0hTUI3MUcptAMbQOxBysNNhChmU6kKDXpgQ0jzCwUE2HwUIBlVJsRp37 Nml7ZM6L34j5W9xJa4Vh7m80INYEYv2UpcluZ2361+HVDur2RhzIdXBEc5TcbdOKiuF+ WRlXM7BAGrBWhJG+rkTAqxuxHHN0SkMLIC6QpBpTYgUxHMHQinvNWKKNB9BPMziLI01A m9VU05RLsnHPXgLwwxej+zRxouMgIT57Bms72yk6LkjMrudRwS9kPYcPDmIBNabeSAPR taaA== X-Gm-Message-State: APjAAAWXpG6m+9ei1guex+IL4lEVpKOeYZZfkdN+b1TLBd2NkljXxFg6 HAxmUDUhXPgGf1CBtiM2M1OqbOPJM2SbSw== X-Google-Smtp-Source: APXvYqxnoNinN5/+aK2kEVRv/YciHCrOjPgHB24FO1cNG04TrcEcJoHAE/rUkPCfVA0GDuvSAw9SVw== X-Received: by 2002:adf:edc4:: with SMTP id v4mr16314933wro.336.1580749311906; Mon, 03 Feb 2020 09:01:51 -0800 (PST) Received: from naushir-VirtualBox.pitowers.org (cust51-dsl50.idnet.net. [212.69.50.51]) by smtp.gmail.com with ESMTPSA id s15sm25714387wrp.4.2020.02.03.09.01.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Feb 2020 09:01:51 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Mon, 3 Feb 2020 16:58:51 +0000 Message-Id: <20200203165851.11033-1-naush@raspberrypi.com> X-Mailer: git-send-email 2.17.1 Subject: [libcamera-devel] [PATCH v2] libcamera: v4l2_videodevice: Add crop/selection control. X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 Feb 2020 17:01:52 -0000 Add control for cropping/selection on a V4L2 video device through the VIDIOC_S_SELECTION ioctl. This is similar to the existing cropping control available on V4L2 sub-devices. Signed-off-by: Naushir Patuck Reviewed-by: Jacopo Mondi --- src/libcamera/include/v4l2_videodevice.h | 5 +++ src/libcamera/v4l2_videodevice.cpp | 48 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/libcamera/include/v4l2_videodevice.h b/src/libcamera/include/v4l2_videodevice.h index e4d35ab..d8edfde 100644 --- a/src/libcamera/include/v4l2_videodevice.h +++ b/src/libcamera/include/v4l2_videodevice.h @@ -182,6 +182,9 @@ public: int setFormat(V4L2DeviceFormat *format); ImageFormats formats(); + int setCrop(Rectangle *rect); + int setCompose(Rectangle *rect); + int exportBuffers(unsigned int count, std::vector> *buffers); int importBuffers(unsigned int count); @@ -213,6 +216,8 @@ private: int getFormatSingleplane(V4L2DeviceFormat *format); int setFormatSingleplane(V4L2DeviceFormat *format); + int setSelection(unsigned int target, Rectangle *rect); + std::vector enumPixelformats(); std::vector enumSizes(unsigned int pixelFormat); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 8226773..01d9694 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -1499,6 +1499,54 @@ uint32_t V4L2VideoDevice::toV4L2Fourcc(PixelFormat pixelFormat, bool multiplanar return 0; } +/** + * \brief Set a crop rectangle on the V4L2 video device node + * \param[inout] rect The rectangle describing the crop target area + * \return 0 on success or a negative error code otherwise + */ +int V4L2VideoDevice::setCrop(Rectangle *rect) +{ + return setSelection(V4L2_SEL_TGT_CROP, rect); +} + +/** + * \brief Set a compose rectangle on the V4L2 video device node + * \param[inout] rect The rectangle describing the compose target area + * \return 0 on success or a negative error code otherwise + */ +int V4L2VideoDevice::setCompose(Rectangle *rect) +{ + return setSelection(V4L2_SEL_TGT_COMPOSE, rect); +} + +int V4L2VideoDevice::setSelection(unsigned int target, Rectangle *rect) +{ + struct v4l2_selection sel = {}; + + sel.type = bufferType_; + sel.target = target; + sel.flags = 0; + + sel.r.left = rect->x; + sel.r.top = rect->y; + sel.r.width = rect->w; + sel.r.height = rect->h; + + int ret = ioctl(VIDIOC_S_SELECTION, &sel); + if (ret < 0) { + LOG(V4L2, Error) << "Unable to set rectangle " << target + << ": " << strerror(-ret); + return ret; + } + + rect->x = sel.r.left; + rect->y = sel.r.top; + rect->w = sel.r.width; + rect->h = sel.r.height; + + return 0; +} + /** * \class V4L2M2MDevice * \brief Memory-to-Memory video device