From patchwork Fri Aug 27 06:44:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hirokazu Honda X-Patchwork-Id: 13530 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 071A9BD87D for ; Fri, 27 Aug 2021 06:44:48 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4703D68932; Fri, 27 Aug 2021 08:44:47 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="ZmD0BJo+"; dkim-atps=neutral Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 96B7160288 for ; Fri, 27 Aug 2021 08:44:46 +0200 (CEST) Received: by mail-pf1-x42c.google.com with SMTP id s29so54823pfw.5 for ; Thu, 26 Aug 2021 23:44:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+DtX14Bn2/Cfefny+vTtcaYWQ1ERk0zFfOXH+PSH7gc=; b=ZmD0BJo+WbQAd5Mwx+rbc9QG2Hip/KugDnBNohEdB0dJIN6X3rzc7BGOqgnNO1PjTw pCehCvYfeGUwezy2uHO9h8JqM9DOiTyvSjHpoNl4Mkk+wj6r/3+R/VLHu9IUxcwc1zt2 TFHNBpwPHPl2gZihEeuUh/kPz8f1f2vgdD8Wk= 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:mime-version :content-transfer-encoding; bh=+DtX14Bn2/Cfefny+vTtcaYWQ1ERk0zFfOXH+PSH7gc=; b=A/fp/V4YXLT+mlZTg+W24mWJ4ybp2MqpRhKerfkBrWyLreWFu0LuBcFZ1h9Fce+fkM ujj7UObq8CdY1yVuAPJTwSSdLf3Ahb+XjWNe+Oq6cdNqeitdfPsMwf8vpsyWfymp/DBL 74AHDV8iRjiRv8XGi6c5U7pWokIclcRfFtm9HV4iI94tdn2YNVXjNhbKt9YwtWGHrKzq t7BxQOJ14+H79snUCLY6xtPLjfVOgyGXW/F5MVHagFAM8M9tDqLInYVMmjqrtuotuDdz U1SSJJ/4ea8rTD9fnjy1l0CDkEo3rI3xbL/EWOoYURq+1p+YBXsL19uE6eSOWaqGyoY5 fsCQ== X-Gm-Message-State: AOAM530pzBtocORI1Y/O2qvo5MGAhvJKa7qCAGxfNWo4ssTVaYRy6/f3 9+QCKxZQtKdzU/myjxww8TvJXQmK1jYQ4w== X-Google-Smtp-Source: ABdhPJxDrO5cCqqBq8qvsIkfJukSTIFElf1j+65YtsNKQKQonQSc4grkpKuBs+4mXjDVDCyXZsAVtw== X-Received: by 2002:a62:ab01:0:b0:3f2:89dc:aa8d with SMTP id p1-20020a62ab01000000b003f289dcaa8dmr2597772pff.20.1630046684530; Thu, 26 Aug 2021 23:44:44 -0700 (PDT) Received: from hiroh2.tok.corp.google.com ([2401:fa00:8f:203:e14c:e27b:81d2:aa99]) by smtp.gmail.com with ESMTPSA id u20sm5499885pgm.4.2021.08.26.23.44.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Aug 2021 23:44:44 -0700 (PDT) From: Hirokazu Honda To: libcamera-devel@lists.libcamera.org Date: Fri, 27 Aug 2021 15:44:39 +0900 Message-Id: <20210827064439.879308-1-hiroh@chromium.org> X-Mailer: git-send-email 2.33.0.259.gc128427fd7-goog MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 7/9] libcamera: v4l2_videodevice: Create color-format planes in createBuffer() 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: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" V4L2VideDevice::createBuffer() creates the same number of FrameBuffer::Planes as V4L2 format planes. Therefore, if the v4l2 format single is single-planar format, the created number of FrameBuffer::Planes is 1. It should rather create the same number of FrameBuffer::Planes as the color format planes. Signed-off-by: Hirokazu Honda Reviewed-by: Laurent Pinchart --- Let me send this patch only in v4. --- include/libcamera/internal/v4l2_videodevice.h | 1 + src/libcamera/v4l2_videodevice.cpp | 69 ++++++++++++++++--- 2 files changed, 61 insertions(+), 9 deletions(-) -- 2.33.0.259.gc128427fd7-goog diff --git a/include/libcamera/internal/v4l2_videodevice.h b/include/libcamera/internal/v4l2_videodevice.h index e767ec84..4a5d2cad 100644 --- a/include/libcamera/internal/v4l2_videodevice.h +++ b/include/libcamera/internal/v4l2_videodevice.h @@ -242,6 +242,7 @@ private: FrameBuffer *dequeueBuffer(); V4L2Capability caps_; + V4L2DeviceFormat format_; enum v4l2_buf_type bufferType_; enum v4l2_memory memoryType_; diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp index 2ff25af2..9d35618d 100644 --- a/src/libcamera/v4l2_videodevice.cpp +++ b/src/libcamera/v4l2_videodevice.cpp @@ -25,6 +25,7 @@ #include +#include "libcamera/internal/formats.h" #include "libcamera/internal/media_device.h" #include "libcamera/internal/media_object.h" @@ -579,6 +580,12 @@ int V4L2VideoDevice::open() << "Opened device " << caps_.bus_info() << ": " << caps_.driver() << ": " << caps_.card(); + ret = getFormat(&format_); + if (ret) { + LOG(V4L2, Error) << "Failed to get format"; + return ret; + } + return 0; } @@ -668,6 +675,12 @@ int V4L2VideoDevice::open(int handle, enum v4l2_buf_type type) << "Opened device " << caps_.bus_info() << ": " << caps_.driver() << ": " << caps_.card(); + ret = getFormat(&format_); + if (ret) { + LOG(V4L2, Error) << "Failed to get format"; + return ret; + } + return 0; } @@ -761,12 +774,20 @@ int V4L2VideoDevice::tryFormat(V4L2DeviceFormat *format) */ int V4L2VideoDevice::setFormat(V4L2DeviceFormat *format) { + int ret = 0; if (caps_.isMeta()) - return trySetFormatMeta(format, true); + ret = trySetFormatMeta(format, true); else if (caps_.isMultiplanar()) - return trySetFormatMultiplane(format, true); + ret = trySetFormatMultiplane(format, true); else - return trySetFormatSingleplane(format, true); + ret = trySetFormatSingleplane(format, true); + + /* Cache the set format on success. */ + if (ret) + return ret; + + format_ = *format; + return 0; } int V4L2VideoDevice::getFormatMeta(V4L2DeviceFormat *format) @@ -1152,8 +1173,13 @@ int V4L2VideoDevice::requestBuffers(unsigned int count, * successful return the driver's internal buffer management is initialized in * MMAP mode, and the video device is ready to accept queueBuffer() calls. * - * The number of planes and the plane sizes for the allocation are determined - * by the currently active format on the device as set by setFormat(). + * The number of planes and their offsets and sizes are determined by the + * currently active format on the device as set by setFormat(). They do not map + * to the V4L2 buffer planes, but to colour planes of the pixel format. For + * instance, if the active format is formats::NV12, the allocated FrameBuffer + * instances will have two planes, for the luma and chroma components, + * regardless of whether the device uses V4L2_PIX_FMT_NV12 or + * V4L2_PIX_FMT_NV12M. * * Buffers allocated with this function shall later be free with * releaseBuffers(). If buffers have already been allocated with @@ -1190,8 +1216,13 @@ int V4L2VideoDevice::allocateBuffers(unsigned int count, * usable with any V4L2 video device in DMABUF mode, or with other dmabuf * importers. * - * The number of planes and the plane sizes for the allocation are determined - * by the currently active format on the device as set by setFormat(). + * The number of planes and their offsets and sizes are determined by the + * currently active format on the device as set by setFormat(). They do not map + * to the V4L2 buffer planes, but to colour planes of the pixel format. For + * instance, if the active format is formats::NV12, the allocated FrameBuffer + * instances will have two planes, for the luma and chroma components, + * regardless of whether the device uses V4L2_PIX_FMT_NV12 or + * V4L2_PIX_FMT_NV12M. * * Multiple independent sets of buffers can be allocated with multiple calls to * this function. Device-specific limitations may apply regarding the minimum @@ -1289,12 +1320,32 @@ std::unique_ptr V4L2VideoDevice::createBuffer(unsigned int index) * \todo Set the right offset once V4L2 API provides a way. */ plane.offset = 0; - plane.length = multiPlanar ? - buf.m.planes[nplane].length : buf.length; + plane.length = multiPlanar ? buf.m.planes[nplane].length : buf.length; planes.push_back(std::move(plane)); } + const auto &info = PixelFormatInfo::info(format_.fourcc); + if (info.isValid() && info.numPlanes() != numPlanes) { + ASSERT(numPlanes == 1u); + const size_t numColorPlanes = info.numPlanes(); + planes.resize(numColorPlanes); + const FileDescriptor &fd = planes[0].fd; + size_t offset = 0; + for (size_t i = 0; i < numColorPlanes; ++i) { + planes[i].fd = fd; + planes[i].offset = offset; + + /* \todo Take the V4L2 stride into account */ + const unsigned int vertSubSample = + info.planes[i].verticalSubSampling; + planes[i].length = + info.stride(format_.size.width, i, 1u) * + ((format_.size.height + vertSubSample - 1) / vertSubSample); + offset += planes[i].length; + } + } + return std::make_unique(std::move(planes)); }