From patchwork Sun Mar 5 21:36:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 9604765 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A9609602B4 for ; Sun, 5 Mar 2017 21:37:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9E44F27C0B for ; Sun, 5 Mar 2017 21:37:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92E0627E22; Sun, 5 Mar 2017 21:37:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1CC0827D8D for ; Sun, 5 Mar 2017 21:37:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752424AbdCEVhc (ORCPT ); Sun, 5 Mar 2017 16:37:32 -0500 Received: from galahad.ideasonboard.com ([185.26.127.97]:59119 "EHLO galahad.ideasonboard.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752388AbdCEVhc (ORCPT ); Sun, 5 Mar 2017 16:37:32 -0500 Received: from avalon.bb.dnainternet.fi (dfj612ybrt5fhg77mgycy-3.rev.dnainternet.fi [IPv6:2001:14ba:21f5:5b00:2e86:4862:ef6a:2804]) by galahad.ideasonboard.com (Postfix) with ESMTPSA id 14847201C1; Sun, 5 Mar 2017 22:34:37 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1488749677; bh=akz25mw5EKG1NcDPQkLFgUVXCAMsh5QA59lvXmFUj40=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sp1m5i92fmE/uB9W9P9d2KtidtJzLWjZ1OCrHmRQWeKbnrTSrDMfqTSYk4e+YuQhP eT59JgSxdG4UmYuQGZN6IcQwhKZv5GKcoyq8s4CPcGpY06um1ZHQrpbDded3K09/9Q YHT2nsRqPYmnTf6vAydCd487BT3VJYBNUhtW8Ows= From: Laurent Pinchart To: linux-media@vger.kernel.org Cc: linux-renesas-soc@vger.kernel.org, Mauro Carvalho Chehab , Hans Verkuil , Sakari Ailus Subject: [PATCH v2.2] v4l: Clearly document interactions between formats, controls and buffers Date: Sun, 5 Mar 2017 23:36:10 +0200 Message-Id: <20170305213610.3893-1-laurent.pinchart+renesas@ideasonboard.com> X-Mailer: git-send-email 2.10.2 In-Reply-To: <20170305143936.11257-1-laurent.pinchart+renesas@ideasonboard.com> References: <20170305143936.11257-1-laurent.pinchart+renesas@ideasonboard.com> Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP V4L2 exposes parameters that influence buffers sizes through the format ioctls (VIDIOC_G_FMT, VIDIOC_TRY_FMT, VIDIOC_S_FMT, and possibly VIDIOC_G_SELECTION and VIDIOC_S_SELECTION). Other parameters not part of the format structure may also influence buffer sizes or buffer layout in general. One existing such parameter is rotation, which is implemented by the V4L2_CID_ROTATE control and thus exposed through the V4L2 control ioctls. The interaction between those parameters and buffers is currently only partially specified by the V4L2 API. In particular interactions between controls and buffers isn't specified at all. The behaviour of the VIDIOC_S_FMT and VIDIOC_S_SELECTION ioctls when buffers are allocated is also not fully specified. This patch clearly defines and documents the interactions between formats, selections, controls and buffers. The preparatory discussions for the documentation change considered completely disallowing controls that change the buffer size or layout, in favour of extending the format API with a new ioctl that would bundle those controls with format information. The idea has been rejected, as this would essentially be a restricted version of the upcoming request API that wouldn't bring any additional value. Another option we have considered was to mandate the use of the request API to modify controls that influence buffer size or layout. This has also been rejected on the grounds that requiring the request API to change rotation even when streaming is stopped would significantly complicate implementation of drivers and usage of the V4L2 API for applications. Applications will however be required to use the upcoming request API to change at runtime formats or controls that influence the buffer size or layout, because of the need to synchronize buffers with the formats and controls. Otherwise there would be no way to interpret the content of a buffer correctly. Signed-off-by: Laurent Pinchart Acked-by: Sakari Ailus --- Changes since v2.1: - Fixed small issues in commit message - Simplified wording of one sentence in the documentation Changes since v2: - Document the interaction with ioctls that can affect formats (VIDIOC_S_SELECTION, VIDIOC_S_INPUT, VIDIOC_S_OUTPUT, VIDIOC_S_STD and VIDIOC_S_DV_TIMINGS) - Clarify the format/control change order --- Documentation/media/uapi/v4l/buffer.rst | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/Documentation/media/uapi/v4l/buffer.rst b/Documentation/media/uapi/v4l/buffer.rst index ac58966ccb9b..60d62a5824f8 100644 --- a/Documentation/media/uapi/v4l/buffer.rst +++ b/Documentation/media/uapi/v4l/buffer.rst @@ -34,6 +34,114 @@ flags are copied from the OUTPUT video buffer to the CAPTURE video buffer. +Interactions between formats, controls and buffers +================================================== + +V4L2 exposes parameters that influence the buffer size, or the way data is +laid out in the buffer. Those parameters are exposed through both formats and +controls. One example of such a control is the ``V4L2_CID_ROTATE`` control +that modifies the direction in which pixels are stored in the buffer, as well +as the buffer size when the selected format includes padding at the end of +lines. + +The set of information needed to interpret the content of a buffer (e.g. the +pixel format, the line stride, the tiling orientation or the rotation) is +collectively referred to in the rest of this section as the buffer layout. + +Modifying formats or controls that influence the buffer size or layout require +the stream to be stopped. Any attempt at such a modification while the stream +is active shall cause the ioctl setting the format or the control to return +the ``EBUSY`` error code. + +Controls that only influence the buffer layout can be modified at any time +when the stream is stopped. As they don't influence the buffer size, no +special handling is needed to synchronize those controls with buffer +allocation. + +Formats and controls that influence the buffer size interact with buffer +allocation. As buffer allocation is an expensive operation, drivers should +allow format or controls that influence the buffer size to be changed with +buffers allocated. A typical ioctl sequence to modify format and controls is + + #. VIDIOC_STREAMOFF + #. VIDIOC_S_EXT_CTRLS + #. VIDIOC_S_FMT + #. VIDIOC_QBUF + #. VIDIOC_STREAMON + +.. note:: + + The API doesn't mandate the above order for control (2.) and format (3.) + changes. Format and controls can be set in a different order, or even + interleaved, depending on the device and use case. For instance some + controls might behave differently for different pixel formats, in which + case the format might need to be set first. + +Queued buffers must be large enough for the new format or controls. + +Drivers shall return a ``ENOSPC`` error in response to format change +(:c:func:`VIDIOC_S_FMT`) or control changes (:c:func:`VIDIOC_S_CTRL` or +:c:func:`VIDIOC_S_EXT_CTRLS`) if buffers too small for the new format are +currently queued. As a simplification, drivers are allowed to return an error +from these ioctls if any buffer is currently queued, without checking the +queued buffers sizes. + +.. note:: + + The :c:func:`VIDIOC_S_SELECTION` ioctl can, depending on the hardware (for + instance if the device doesn't include a scaler), modify the format in + addition to the selection rectangle. Similarly, the + :c:func:`VIDIOC_S_INPUT`, :c:func:`VIDIOC_S_OUTPUT`, :c:func:`VIDIOC_S_STD` + and :c:func:`VIDIOC_S_DV_TIMINGS` ioctls can also modify the format and + selection rectangles. Driver shall return the same ``ENOSPC`` error from + all ioctls that would result in formats too large for queued buffers. + +Drivers shall also return a ``ENOSPC`` error from the :c:func:`VIDIOC_QBUF` +ioctl if the buffer being queued is too small for the current format or +controls. Together, these requirements ensure that queued buffers will always +be large enough for the configured format and controls. + +Userspace applications can query the buffer size required for a given format +and controls by first setting the desired control values and then trying the +desired format. The :c:func:`VIDIOC_TRY_FMT` ioctl will return the required +buffer size. + + #. VIDIOC_S_EXT_CTRLS(x) + #. VIDIOC_TRY_FMT() + #. VIDIOC_S_EXT_CTRLS(y) + #. VIDIOC_TRY_FMT() + +The :c:func:`VIDIOC_CREATE_BUFS` ioctl can then be used to allocate buffers +based on the queried sizes (for instance by allocating a set of buffers large +enough for all the desired formats and controls, or by allocating separate set +of appropriately sized buffers for each use case). + +To simplify their implementation, drivers may also require buffers to be +reallocated in order to change formats or controls that influence the buffer +size. In that case, to perform such changes, userspace applications shall +first stop the video stream with the :c:func:`VIDIOC_STREAMOFF` ioctl if it +is running and free all buffers with the :c:func:`VIDIOC_REQBUFS` ioctl if +they are allocated. The format or controls can then be modified, and buffers +shall then be reallocated and the stream restarted. A typical ioctl sequence +is + + #. VIDIOC_STREAMOFF + #. VIDIOC_REQBUFS(0) + #. VIDIOC_S_EXT_CTRLS + #. VIDIOC_S_FMT + #. VIDIOC_REQBUFS(n) + #. VIDIOC_QBUF + #. VIDIOC_STREAMON + +The second :c:func:`VIDIOC_REQBUFS` call will take the new format and control +value into account to compute the buffer size to allocate. Applications can +also retrieve the size by calling the :c:func:`VIDIOC_G_FMT` ioctl if needed. + +When reallocation is required, any attempt to modify format or controls that +influences the buffer size while buffers are allocated shall cause the format +or control set ioctl to return the ``EBUSY`` error code. + + .. c:type:: v4l2_buffer struct v4l2_buffer