From patchwork Tue Feb 4 11:24:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Naushir Patuck X-Patchwork-Id: 2768 Return-Path: Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A905E60445 for ; Tue, 4 Feb 2020 12:26:47 +0100 (CET) Received: by mail-wm1-x341.google.com with SMTP id a9so3152870wmj.3 for ; Tue, 04 Feb 2020 03:26:47 -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=YI/5KwC4FYG4B34Fr9E1MoGFP89Pz0rzIYpQIGKf9GU=; b=JBWnrICciFMJvEJmZGaUYO/UPXC7M14uB+cfUQGdUpp3KK3z3yAbVecT1kX2i3mxu5 eau6TJwuftKNCKmPiTNXOK9sNNujNyurEU1auOg5aRcR8lOZAYGY/xC+/n1643BvmFbB NXddTQ49YamFDnCYHQC9E4aA+0qgwIFHRIvsc13O2klE7p9yVa+/Xu53z4ccWzkc3LTY OZv51axsj64VuoUwNzLmoC6CgtLwQLYtv08CWWbgnGrHwqKgTKOoWTcUclQ0nKnaRB+k sB2q+uRFktHZry/oHvgNs/ifUpVaF/Jiljbq+zgmJD/aAHfG7C0R1azHD7IpuKOLoVcd SGXg== 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=YI/5KwC4FYG4B34Fr9E1MoGFP89Pz0rzIYpQIGKf9GU=; b=N9FWX3QNv9++OdwDTCv+XZS50zy+q6VZ80Oi7iYHxg50BAk4cUSE/HyY5tzEXEUu2/ Tnz9IA43k1FISv70HpsKfjaIM38B8H8iwm9st6SGgi9Mt7YnLnGI8ZIs3VypUAeMtTSh obdHmPiKngY1+66dbmhJlXjp2/AfvqOSjY8ffWHKWiDgTMPyQ+tw1VNZg2KV0kgXaiFk Ya24Z2uadciyufCjS2J8B4/ricIQpXXI/9+uv4yWxOkFzWgywgCdH5dxLvKBkg3FWCTM EQxcqiunkMoHaOTPlyrVhVle1F2iPoklfFfQkwxMSar32qAKyl/o4SJIJEL8zmRU5WNG e2pA== X-Gm-Message-State: APjAAAXVADB8js30B9YgLrwXp5jzO7XPe1vWz8z6OBep/Czw+/lHbQSh pmoRG7G1d+Svyb+43J+Rlkb/C1XAK7+xGw== X-Google-Smtp-Source: APXvYqzFgxdOq22Yvlk+VL2kfhAtf7Fw9gQV1TBAbre1/wdiwwMTGfFeSzcKepvFadt1wgAts2TvNA== X-Received: by 2002:a05:600c:299:: with SMTP id 25mr5443902wmk.68.1580815607004; Tue, 04 Feb 2020 03:26:47 -0800 (PST) Received: from naushir-VirtualBox.pitowers.org (cust51-dsl50.idnet.net. [212.69.50.51]) by smtp.gmail.com with ESMTPSA id p7sm3280187wmp.31.2020.02.04.03.26.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2020 03:26:46 -0800 (PST) From: Naushir Patuck To: libcamera-devel@lists.libcamera.org Date: Tue, 4 Feb 2020 11:24:44 +0000 Message-Id: <20200204112444.8395-1-naush@raspberrypi.com> X-Mailer: git-send-email 2.17.1 Subject: [libcamera-devel] [PATCH v3] 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: Tue, 04 Feb 2020 11:26:47 -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 Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- 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..fcf0726 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); @@ -216,6 +219,8 @@ private: std::vector enumPixelformats(); std::vector enumSizes(unsigned int pixelFormat); + int setSelection(unsigned int target, Rectangle *rect); + int requestBuffers(unsigned int count); std::unique_ptr createBuffer(const struct v4l2_buffer &buf); FileDescriptor exportDmabufFd(unsigned int index, unsigned int plane); diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 8226773..f3c66ed 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -940,6 +940,54 @@ std::vector V4L2VideoDevice::enumSizes(unsigned int pixelFormat) return sizes; } +/** + * \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; +} + int V4L2VideoDevice::requestBuffers(unsigned int count) { struct v4l2_requestbuffers rb = {};