From patchwork Mon Aug 3 16:18:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9141 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 91CF4BD86F for ; Mon, 3 Aug 2020 16:18:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 2B9E960905; Mon, 3 Aug 2020 18:18:24 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="bxT/5q0q"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 28A8460745 for ; Mon, 3 Aug 2020 18:18:22 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 9230F560; Mon, 3 Aug 2020 18:18:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471501; bh=yS7LBsdtVmlDAoa0b6Q9/+LoNNQ8kRM7DnSnfHT4HrM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bxT/5q0qht8EOSx/OoGRbRwGvuXfPdpuWZbUwr8gjnwb6ihucDs5luIR+smZEPtxz uKdmbXnkf7IWiwwmu2bfzi4P1rfzCoSR+6YrQxw/0kveYBvBeJWO25OAA4Xkxfe0dl Rh9IfwBhjWMt5vrYYz2YGqciCrrdrxiheiqKfWY4= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:05 +0100 Message-Id: <20200803161816.107113-2-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 01/12] libcamera: FrameBuffer: Correct Buffer references 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" Update the remaining (capitalised) Buffer references to the new FrameBuffer object that they refer to. Lower-case 'buffer' terms are kept, as a generic reference to a buffer rather than the specific type 'FrameBuffer'. Signed-off-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/libcamera/buffer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 1a1d4bac7aed..8278f8a92af4 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -174,11 +174,11 @@ FrameBuffer::FrameBuffer(const std::vector &planes, unsigned int cookie) * The intended callers of this method are buffer completion handlers that * need to associate a buffer to the request it belongs to. * - * A Buffer is associated to a request by Request::addBuffer() and the + * A FrameBuffer is associated to a request by Request::addBuffer() and the * association is valid until the buffer completes. The returned request * pointer is valid only during that interval. * - * \return The Request the Buffer belongs to, or nullptr if the buffer is + * \return The Request the FrameBuffer belongs to, or nullptr if the buffer is * not associated with a request */ @@ -224,7 +224,7 @@ FrameBuffer::FrameBuffer(const std::vector &planes, unsigned int cookie) /** * \brief Copy the contents from another buffer - * \param[in] src Buffer to copy + * \param[in] src FrameBuffer to copy * * Copy the buffer contents and metadata from \a src to this buffer. The * destination FrameBuffer shall have the same number of planes as the source From patchwork Mon Aug 3 16:18:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9142 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 54473BD86F for ; Mon, 3 Aug 2020 16:18:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 7B953619F4; Mon, 3 Aug 2020 18:18:24 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="WOikV1ul"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8BB07605A9 for ; Mon, 3 Aug 2020 18:18:22 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id EC8F69CE; Mon, 3 Aug 2020 18:18:21 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471502; bh=J6UIdvHGBPmQweKl+qZmHGZG1DflYGit3djkhFdbAgU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WOikV1ulzJUPvHUuqchB0CeB/RIQyPMAd9R4mlJEr/aqn1/4R+A/O8EW1aK5zO1kt bllqL7zthhAfLgicXvUdIRxSB6U9nbcjkp5sLglKcVcghixHfSg7tFMk/CI+G81kWl U/Pst3nzV58Nwx80hHoY4NQwcl56buxbFvLP7iEQ= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:06 +0100 Message-Id: <20200803161816.107113-3-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 02/12] libcamera: formats: add planeCount helper 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" Determine the number of planes used by a format by counting the number of PxielFormatPlaneInfo entries with a valid entry. Signed-off-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- v2: - Rename to numPlanes - Simplify fucntion doc. --- include/libcamera/internal/formats.h | 2 ++ src/libcamera/formats.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h index b8e474916378..51a8a6b8b0ae 100644 --- a/include/libcamera/internal/formats.h +++ b/include/libcamera/internal/formats.h @@ -46,6 +46,8 @@ public: unsigned int frameSize(const Size &size, const std::array &strides) const; + unsigned int numPlanes() const; + /* \todo Add support for non-contiguous memory planes */ const char *name; PixelFormat format; diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp index a51e96810aed..ebaae9be3d75 100644 --- a/src/libcamera/formats.cpp +++ b/src/libcamera/formats.cpp @@ -821,4 +821,22 @@ PixelFormatInfo::frameSize(const Size &size, return sum; } +/** + * \brief Retrieve the number of planes represented by the format + * \return The number of planes used by the format + */ +unsigned int PixelFormatInfo::numPlanes() const +{ + unsigned int count = 0; + + for (const PixelFormatPlaneInfo &p : planes) { + if (p.bytesPerGroup == 0) + break; + + count++; + } + + return count; +} + } /* namespace libcamera */ From patchwork Mon Aug 3 16:18:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9143 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 A654DBD87B for ; Mon, 3 Aug 2020 16:18:25 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id C621561A12; Mon, 3 Aug 2020 18:18:24 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Aw8K6VsO"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id DDD9D60905 for ; Mon, 3 Aug 2020 18:18:22 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 6AA0FA62; Mon, 3 Aug 2020 18:18:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471502; bh=ma1nnWQzZHdN8csH7HZAqALRKraHiWDg3KbQNBH+Il0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Aw8K6VsOTxeIJNVAK9vU+c68dnNj3a7mkHo1WmROd+L2eLGOVjtMAbJUULwNZhlyE M28Mv1kjnhccMe6iyoavUVGJEYrwD7sFQGj2y9pXW4VW3a/WbLLzhm+5v/RyOsMVnW D2VWEHNbNg9WUcLGk/XfigkQzLNv7FIc/f0oWtv0= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:07 +0100 Message-Id: <20200803161816.107113-4-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 03/12] libcamera: buffer: Create a MappedBuffer 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" Provide a MappedFrameBuffer helper class which will map all of the Planes within a FrameBuffer and provide CPU addressable pointers for those planes. The MappedFrameBuffer implements the interface of the MappedBuffer allowing other buffer types to be constructed of the same form, with a common interface and cleanup. This allows MappedBuffer instances to be created from Camera3Buffer types. Mappings are removed upon destruction. Signed-off-by: Kieran Bingham --- include/libcamera/internal/buffer.h | 46 ++++++++++ src/libcamera/buffer.cpp | 134 ++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 include/libcamera/internal/buffer.h diff --git a/include/libcamera/internal/buffer.h b/include/libcamera/internal/buffer.h new file mode 100644 index 000000000000..c04c294fecda --- /dev/null +++ b/include/libcamera/internal/buffer.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * buffer.h - Internal buffer handling + */ +#ifndef __LIBCAMERA_INTERNAL_BUFFER_H__ +#define __LIBCAMERA_INTERNAL_BUFFER_H__ + +#include + +#include +#include + +namespace libcamera { + +using MappedPlane = Span; + +class MappedBuffer +{ +public: + MappedBuffer(); + ~MappedBuffer(); + + MappedBuffer(MappedBuffer &&rhs); + MappedBuffer &operator=(MappedBuffer &&rhs); + + bool isValid() const { return valid_; } + int error() const { return error_; } + const std::vector &maps() const { return maps_; } + +protected: + int error_; + bool valid_; + std::vector maps_; +}; + +class MappedFrameBuffer : public MappedBuffer +{ +public: + MappedFrameBuffer(const FrameBuffer *buffer, int flags); +}; + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_INTERNAL_BUFFER_H__ */ diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 8278f8a92af4..2f667db42922 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -6,6 +6,7 @@ */ #include +#include "libcamera/internal/buffer.h" #include #include @@ -290,4 +291,137 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) return 0; } +/** + * \class MappedBuffer + * \brief Provide + * + * The MappedBuffer interface provides access to a set of MappedPlanes which + * are available for access by a CPU. + * + * The MappedBuffer interface does not implement any valid constructor but + * defines the move operators and destructors for the derived implementations, + * which are able to construct according to their derived types and given + * flags. + */ + +/** + * \brief Construct an empty MappedBuffer + * + * A default constructor is required to allow subclassing the MappedBuffer + * class. Construct an initialised, but invalid MappedBuffer. + */ +MappedBuffer::MappedBuffer() + : error_(0), valid_(false) +{ +} + +/** + * \brief Construct a MappedBuffer by taking the \a rhs mappings + * \param[in] rhs The other MappedBuffer + * + * Moving a MappedBuffer moves the mappings contained in the \a rhs to the new + * MappedBuffer and invalidates the \a rhs. No mappings are unmapped or + * destroyed in this process. + */ +MappedBuffer::MappedBuffer(MappedBuffer &&rhs) +{ + *this = std::move(rhs); +} + +/** + * \brief Move assingment operator, move a MappedBuffer by taking the \a rhs mappings + * \param[in] rhs The other MappedBuffer + * + * Moving a MappedBuffer moves the mappings contained in the \a rhs to the new + * MappedBuffer and invalidates the \a rhs. No mappings are unmapped or + * destroyed in this process. + */ +MappedBuffer &MappedBuffer::operator=(MappedBuffer &&rhs) +{ + error_ = rhs.error_; + valid_ = rhs.valid_; + maps_ = std::move(rhs.maps_); + rhs.valid_ = false; + rhs.error_ = 0; + + return *this; +} + +MappedBuffer::~MappedBuffer() +{ + for (MappedPlane map : maps_) + munmap(map.data(), map.size()); +} + +/** + * \fn MappedBuffer::isValid() + * \brief Check if the MappedBuffer instance is valid + * \return True if the MappedBuffer has valid mappings, false otherwise + */ + +/** + * \fn MappedBuffer::error() + * \brief Retrieve the map error status + * + * This function retrieves the error status from the MappedBuffer. + * The error status is a negative number as defined by errno.h. If + * no error occurred, this function returns 0. + */ + +/** + * \var MappedBuffer::valid_ + * \brief Stores the status of the mapping + * + * MappedBuffer implementations shall set this to represent if the mapping + * was successfully completed without errors. + */ + +/** + * \var MappedBuffer::error_ + * \brief Stores the error value if present + * + * MappedBuffer implementations shall set this to a negative value as defined + * by errno.h if an error occured during the mapping process + */ + +/** + * \class MappedFrameBuffer + * \brief Maps a FrameBuffer using the MappedBuffer interface + * + * The MappedFrameBuffer interface maps a FrameBuffer instance + * + * The MappedBuffer interface does not implement any constructor but defines + * the move operators and destructors for the derived implementations, which + * are able to construct according to their derived types and given flags. + */ + +/** + * \brief Map all planes of a FrameBuffer + * \param[in] buffer FrameBuffer to be mapped + * \param[in] flags Protection flags to apply to map + * + * Construct an object to map a frame buffer for CPU access. + * The flags are passed directly to mmap and should be either PROT_READ, + * PROT_WRITE, or a bitwise-or combination of both. + */ +MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags) +{ + maps_.reserve(buffer->planes().size()); + + for (const FrameBuffer::Plane &plane : buffer->planes()) { + void *address = mmap(nullptr, plane.length, flags, + MAP_SHARED, plane.fd.fd(), 0); + + if (address == MAP_FAILED) { + error_ = -errno; + LOG(Buffer, Error) << "Failed to mmap plane"; + break; + } + + maps_.emplace_back(static_cast(address), plane.length); + } + + valid_ = buffer->planes().size() == maps_.size(); +} + } /* namespace libcamera */ From patchwork Mon Aug 3 16:18:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9144 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 73C0BBD86F for ; Mon, 3 Aug 2020 16:18:27 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3B38F60396; Mon, 3 Aug 2020 18:18:27 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="W6VsN1L+"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 389A66199D for ; Mon, 3 Aug 2020 18:18:23 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id B8FD3B27; Mon, 3 Aug 2020 18:18:22 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471502; bh=ogy899GEHQ2j1b5E66u6H0gWtBw2hIdVmT+pTTs87Pw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W6VsN1L+AQz8JTKK1fH/lraVX0V77WQx/YyE6HMrI1uIt4LUAKpueT8+RBVLaAQ7o 8bgYL9WiGCiMwdEyGr7IAzdqCwq0lXEH4zSbpMeN+vKiYMxyhC8oCFpAb94v5nhqBG Rb+dIxZsrwjPTRs6JqWxh41EF67fant7U7LnUKFs= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:08 +0100 Message-Id: <20200803161816.107113-5-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 04/12] libcamera: buffer: Convert copyFrom to use MappedFrameBuffer 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" Utilise the new MappedFrameBuffer helper to handle all mapping and unmapping of the copyFrom helper function. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/libcamera/buffer.cpp | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp index 2f667db42922..fc15fc1c8ec4 100644 --- a/src/libcamera/buffer.cpp +++ b/src/libcamera/buffer.cpp @@ -258,32 +258,23 @@ int FrameBuffer::copyFrom(const FrameBuffer *src) } } - for (unsigned int i = 0; i < planes_.size(); i++) { - void *dstmem = mmap(nullptr, planes_[i].length, PROT_WRITE, - MAP_SHARED, planes_[i].fd.fd(), 0); + MappedFrameBuffer source(src, PROT_READ); + MappedFrameBuffer destination(this, PROT_WRITE); - if (dstmem == MAP_FAILED) { - LOG(Buffer, Error) - << "Failed to map destination plane " << i; - metadata_.status = FrameMetadata::FrameError; - return -EINVAL; - } - - void *srcmem = mmap(nullptr, src->planes_[i].length, PROT_READ, - MAP_SHARED, src->planes_[i].fd.fd(), 0); - - if (srcmem == MAP_FAILED) { - munmap(dstmem, planes_[i].length); - LOG(Buffer, Error) - << "Failed to map source plane " << i; - metadata_.status = FrameMetadata::FrameError; - return -EINVAL; - } + if (!source.isValid()) { + LOG(Buffer, Error) << "Failed to map source planes"; + return -EINVAL; + } - memcpy(dstmem, srcmem, src->planes_[i].length); + if (!destination.isValid()) { + LOG(Buffer, Error) << "Failed to map destination planes"; + return -EINVAL; + } - munmap(srcmem, src->planes_[i].length); - munmap(dstmem, planes_[i].length); + for (unsigned int i = 0; i < planes_.size(); i++) { + memcpy(destination.maps()[i].data(), + source.maps()[i].data(), + source.maps()[i].size()); } metadata_ = src->metadata_; From patchwork Mon Aug 3 16:18:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9145 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 2F59DBD86F for ; Mon, 3 Aug 2020 16:18:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D901F61813; Mon, 3 Aug 2020 18:18:27 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="cL8Ifsov"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A640B605A9 for ; Mon, 3 Aug 2020 18:18:23 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 1E04DB8A; Mon, 3 Aug 2020 18:18:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471503; bh=8Nc71NPFkUqbkfLc0see6ufULLU2YVZLzAw+W/8t56Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cL8Ifsov/Btl2Mvpdfpvs5Q1xCJ26wvSUtsb7vzywO06Kq9d4m8iImbfbSPaS7C4N HwtP7/R5hlXqnSDqQo31PqGHLWCabh+ZSaaVaCIqalbrwf/tNmMCTiLh162TYutqCA jgQjv0stOFU66gtIKhQH0ESR5pVeJTadltBNXHNI= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:09 +0100 Message-Id: <20200803161816.107113-6-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 05/12] android: camera_device: Fix trivial spelling 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" Fix the spelling of 'successfully' reported in an error message. Reviewed-by: Laurent Pinchart Signed-off-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/android/camera_device.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index b49d6681e5a5..cf50821eec71 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1124,7 +1124,7 @@ void CameraDevice::requestComplete(Request *request) std::unique_ptr resultMetadata; if (request->status() != Request::RequestComplete) { - LOG(HAL, Error) << "Request not succesfully completed: " + LOG(HAL, Error) << "Request not successfully completed: " << request->status(); status = CAMERA3_BUFFER_STATUS_ERROR; } From patchwork Mon Aug 3 16:18:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9146 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 AB547BD86F for ; Mon, 3 Aug 2020 16:18:28 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 733BD61B35; Mon, 3 Aug 2020 18:18:28 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="RTeL4JJM"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2BC65619A6 for ; Mon, 3 Aug 2020 18:18:25 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 8AFD8D98; Mon, 3 Aug 2020 18:18:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471503; bh=wkI9536wnTTr4ozf9rNXc3a5+hWQMqW9dN1micTDvuE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RTeL4JJMoU94teCcvWsovAGvH839+7mf6gcawEaO4LAKcwhvRGeVLSjwN+M1XHoyq uIrFEXvyNbcYZGzQ67JnQnQG/5Lp23cl/T//e8qhx3Y3Q5QdYvPTh0qxMYyzbT+CIe vk2bekYHs0mZTHGXVmhdvzfXkmSYHR6so9QC7HJ8= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:10 +0100 Message-Id: <20200803161816.107113-7-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 06/12] android: camera_device: Report an error in notifyError() 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" If an error is generated, try to be verbose about it in the libcamera logs. Signed-off-by: Kieran Bingham Reviewed-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- v2: - Add todo to note possible debug info expansion - Fix typo - Print .toString() formatted pixel format v3: - Remove colon from todo: Note that the calling point of this function always gives stream zero anyway. src/android/camera_device.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index cf50821eec71..388945b1075c 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1198,6 +1198,13 @@ void CameraDevice::notifyError(uint32_t frameNumber, camera3_stream_t *stream) { camera3_notify_msg_t notify = {}; + /* + * \todo Report and identify the stream number or configuration to + * clarify the stream that failed. + */ + LOG(HAL, Error) << "Error occurred on frame " << frameNumber << " (" + << toPixelFormat(stream->format).toString() << ")"; + notify.type = CAMERA3_MSG_ERROR; notify.message.error.error_stream = stream; notify.message.error.frame_number = frameNumber; From patchwork Mon Aug 3 16:18:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9147 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 15C68BD87B for ; Mon, 3 Aug 2020 16:18:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D588661ACD; Mon, 3 Aug 2020 18:18:28 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="EDRfkF/K"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9154D619CA for ; Mon, 3 Aug 2020 18:18:25 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 18842DA0; Mon, 3 Aug 2020 18:18:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471505; bh=AbPYBkydbCRoiGrcWQRan60YLgJZyyMShJ6atIn/YHs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EDRfkF/KFV0u3lqo1/LtQ8Qwqc+bSFNm8Bnzma3yBU0w904Bsy2qUoYoBa+2HaHm7 5IytFakeSgWuPu8bpFUEWp+gZ6NPrpbljXG19AGfFvNywNMAxlv3nB/yvlDs7BMmAE tq3X31sxWFEA2pGWLqQf15WYxzTCR6LqRicsvNco= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:11 +0100 Message-Id: <20200803161816.107113-8-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 07/12] android: camera_device: Only construct required planes 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" The camera3buffer describes the number of filedescriptors given. Don't try to construct more planes than that. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/android/camera_device.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 388945b1075c..78b0246e40f0 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1022,9 +1022,20 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer) { std::vector planes; - for (unsigned int i = 0; i < 3; i++) { + for (int i = 0; i < camera3buffer->numFds; i++) { + /* Skip unused planes. */ + if (camera3buffer->data[i] == -1) + break; + FrameBuffer::Plane plane; plane.fd = FileDescriptor(camera3buffer->data[i]); + if (!plane.fd.isValid()) { + LOG(HAL, Error) << "Failed to obtain FileDescriptor (" + << camera3buffer->data[i] << ") " + << " on plane " << i; + break; + } + /* * Setting length to zero here is OK as the length is only used * to map the memory of the plane. Libcamera do not need to poke From patchwork Mon Aug 3 16:18:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9148 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 79730BD86F for ; Mon, 3 Aug 2020 16:18:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3810361ACF; Mon, 3 Aug 2020 18:18:29 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="odc+9DN3"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EE863619CD for ; Mon, 3 Aug 2020 18:18:25 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 73308DA7; Mon, 3 Aug 2020 18:18:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471505; bh=sGlt204RW56KeyLzNgiOWi8anTYrFWhaP7mkErKGcMU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=odc+9DN3AXwFJ1Lmrn++LVQzJ/eXfJR4JheMqdPDLX9INjsDd3X1Teb/spcplJqCV WVcj+K/axBEVlqgMjWo5tlmgZr2IhOr2+dXDU3E0w7XXucwNbH3YM25E81WcuRQHyi fW8p+7VSAOwK4tjmcULyUhBxb6U0LWez8zU7uKeQ= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:12 +0100 Message-Id: <20200803161816.107113-9-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 08/12] android: camera_device: Query plane length 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" Use lseek to query the length of planes where possible rather than leaving the plane.length as zero, which prevents mapping buffers for software processing. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- v2: - no longer support an invalid plane length. --- src/android/camera_device.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 78b0246e40f0..9f3f3315aed4 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1036,12 +1036,13 @@ FrameBuffer *CameraDevice::createFrameBuffer(const buffer_handle_t camera3buffer break; } - /* - * Setting length to zero here is OK as the length is only used - * to map the memory of the plane. Libcamera do not need to poke - * at the memory content queued by the HAL. - */ - plane.length = 0; + off_t length = lseek(plane.fd.fd(), 0, SEEK_END); + if (length == -1) { + LOG(HAL, Error) << "Failed to query plane length"; + break; + } + + plane.length = length; planes.push_back(std::move(plane)); } From patchwork Mon Aug 3 16:18:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9149 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 D930EBD87B for ; Mon, 3 Aug 2020 16:18:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9FE8061ADE; Mon, 3 Aug 2020 18:18:29 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="ohCO7UPF"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 38A4460396 for ; Mon, 3 Aug 2020 18:18:26 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id CA863FCC; Mon, 3 Aug 2020 18:18:25 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471506; bh=X9wiBEZ2oKUPYHLOBPD207/2aKgscdykfwGfxeMnlR8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ohCO7UPFEadyBZsweWDKxd0oqMc/Eh111JOmbXYo66SK9kk5T2r092Sv6M5ddSK9R c51zofbIOSYAp6XUDydArbHx7ppCWVwOhVtyj6opRgZzDfB0PMLvOWHbK6QA4sj2b+ XuVHYy96Uwhw+jnvgHkBr812+32UQZVlCdy0PmHI= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:13 +0100 Message-Id: <20200803161816.107113-10-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 09/12] android: camera_device: Report configuration changes from validate() 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" When we call validate on a configuration, if there are any adjustments on the configuration, we fail without showing why. Display the stream configuration after the validate stage to aid debugging stream startup failures. Signed-off-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/android/camera_device.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index 9f3f3315aed4..ae52a5ca8b86 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -987,7 +987,11 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) case CameraConfiguration::Valid: break; case CameraConfiguration::Adjusted: - LOG(HAL, Info) << "Camera configuration adjusted"; + LOG(HAL, Info) << "Camera configuration adjusted:"; + + for (const StreamConfiguration &cfg : *config_) + LOG(HAL, Info) << " : " << cfg.toString(); + config_.reset(); return -EINVAL; case CameraConfiguration::Invalid: From patchwork Mon Aug 3 16:18:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9150 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 39148BD86F for ; Mon, 3 Aug 2020 16:18:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 03450619F4; Mon, 3 Aug 2020 18:18:30 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="rg/Cx51Q"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 94035619AC for ; Mon, 3 Aug 2020 18:18:26 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 22DDD10C6; Mon, 3 Aug 2020 18:18:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471506; bh=6JrzFw6nTgUeS+IVcb2v4uvk0DtjTPuS0ERzxli3sRg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rg/Cx51Qke0+wyzwAgYS++Ani/w5nrH0rH5/WY8iZyqjl53ueg7zm/zz69S1uS/LX 30FegSfmIja35SLn5ZO6GjUTUtUd+7JFxJWigAPhWwqHEIOiaVv6D6OKXIYmuDBX/Z wKieiOOEs6i2z2CHLiR+fETthcd4ak5O6VxlYip4= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:14 +0100 Message-Id: <20200803161816.107113-11-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 10/12] android: camera_device: Generate ResultMetadata earlier 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" Generate the ResultMetadata before performing JPEG compression so that JPEG specific metadata can be added to the metadata when it has been processed. Signed-off-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- src/android/camera_device.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index ae52a5ca8b86..e23ab055d012 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -1138,6 +1138,8 @@ void CameraDevice::requestComplete(Request *request) const std::map &buffers = request->buffers(); camera3_buffer_status status = CAMERA3_BUFFER_STATUS_OK; std::unique_ptr resultMetadata; + Camera3RequestDescriptor *descriptor = + reinterpret_cast(request->cookie()); if (request->status() != Request::RequestComplete) { LOG(HAL, Error) << "Request not successfully completed: " @@ -1145,9 +1147,18 @@ void CameraDevice::requestComplete(Request *request) status = CAMERA3_BUFFER_STATUS_ERROR; } + /* + * \todo The timestamp used for the metadata is currently always taken + * from the first buffer (which may be the first stream) in the Request. + * It might be appropriate to return a 'correct' (as determined by + * pipeline handlers) timestamp in the Request itself. + */ + FrameBuffer *buffer = buffers.begin()->second; + resultMetadata = getResultMetadata(descriptor->frameNumber, + buffer->metadata().timestamp); + /* Prepare to call back the Android camera stack. */ - Camera3RequestDescriptor *descriptor = - reinterpret_cast(request->cookie()); + camera3_capture_result_t captureResult = {}; captureResult.frame_number = descriptor->frameNumber; @@ -1160,21 +1171,13 @@ void CameraDevice::requestComplete(Request *request) captureResult.output_buffers = const_cast(descriptor->buffers); - /* - * \todo The timestamp used for the metadata is currently always taken - * from the first buffer (which may be the first stream) in the Request. - * It might be appropriate to return a 'correct' (as determined by - * pipeline handlers) timestamp in the Request itself. - */ - FrameBuffer *buffer = buffers.begin()->second; if (status == CAMERA3_BUFFER_STATUS_OK) { notifyShutter(descriptor->frameNumber, buffer->metadata().timestamp); captureResult.partial_result = 1; - resultMetadata = getResultMetadata(descriptor->frameNumber, - buffer->metadata().timestamp); + captureResult.result = resultMetadata->get(); } From patchwork Mon Aug 3 16:18:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9151 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 8BFD8BD87B for ; Mon, 3 Aug 2020 16:18:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 5577861B4F; Mon, 3 Aug 2020 18:18:30 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="qPNZNa2J"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id EF6D3619AC for ; Mon, 3 Aug 2020 18:18:26 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 741D91163; Mon, 3 Aug 2020 18:18:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471506; bh=zHxJF4V6+ZNNHrzvBw2p5OhSJwvCn61xPS0ahCUMv3g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qPNZNa2J9/m2UED2Faa+ijYvhe2SZiS5m3EzORM5FIzf96YYKrMWES3XWFtL+nqk+ zGI24mTY5zjfjYt1NnWRTn/Z6+7rkUl/iKoAUkKpDeDv7Ue/rvsnVGi6Z6RNXu7wCV Jy7vELQCZnqr1+RccgCTj+TGouo8KztMyy/4Puck= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:15 +0100 Message-Id: <20200803161816.107113-12-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 11/12] android: Introduce JPEG compression 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" Provide a compressor interface and implement a JPEG compressor using libjpeg. Signed-off-by: Kieran Bingham Reviewed-by: Jacopo Mondi --- v2: - Convert to use the libcamera format information rather than duplicating it ourselves. - Not easy to get the horizontal subsampling - not easy to determine if we have an nvSwap... Possible Todos: - Convert to RAW api if possible. - Add custom JPEG error handler src/android/jpeg/compressor.h | 28 ++++ src/android/jpeg/compressor_jpeg.cpp | 215 +++++++++++++++++++++++++++ src/android/jpeg/compressor_jpeg.h | 46 ++++++ src/android/meson.build | 1 + src/libcamera/meson.build | 2 + 5 files changed, 292 insertions(+) create mode 100644 src/android/jpeg/compressor.h create mode 100644 src/android/jpeg/compressor_jpeg.cpp create mode 100644 src/android/jpeg/compressor_jpeg.h diff --git a/src/android/jpeg/compressor.h b/src/android/jpeg/compressor.h new file mode 100644 index 000000000000..18d8f65eba02 --- /dev/null +++ b/src/android/jpeg/compressor.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * compressor.h - Image compression interface + */ +#ifndef __ANDROID_JPEG_COMPRESSOR_H__ +#define __ANDROID_JPEG_COMPRESSOR_H__ + +#include +#include + +struct CompressedImage { + unsigned char *data; + unsigned long length; +}; + +class Compressor +{ +public: + virtual ~Compressor(){}; + + virtual int configure(const libcamera::StreamConfiguration &cfg) = 0; + virtual int compress(const libcamera::FrameBuffer *source, CompressedImage *image) = 0; + virtual void free(CompressedImage *image) = 0; +}; + +#endif /* __ANDROID_JPEG_COMPRESSOR_H__ */ diff --git a/src/android/jpeg/compressor_jpeg.cpp b/src/android/jpeg/compressor_jpeg.cpp new file mode 100644 index 000000000000..83149417878d --- /dev/null +++ b/src/android/jpeg/compressor_jpeg.cpp @@ -0,0 +1,215 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * compressor_jpeg.cpp - JPEG compression using libjpeg native API + */ + +#include "compressor_jpeg.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "libcamera/internal/formats.h" +#include "libcamera/internal/log.h" + +using namespace libcamera; + +LOG_DEFINE_CATEGORY(JPEG) + +namespace { + +struct JPEGPixelFormatInfo { + J_COLOR_SPACE colorSpace; + const PixelFormatInfo &pixelFormatInfo; + bool nvSwap; +}; + +std::map pixelInfo{ + { formats::R8, { JCS_GRAYSCALE, PixelFormatInfo::info(formats::R8), false } }, + + { formats::RGB888, { JCS_EXT_BGR, PixelFormatInfo::info(formats::RGB888), false } }, + { formats::BGR888, { JCS_EXT_RGB, PixelFormatInfo::info(formats::BGR888), false } }, + + { formats::NV12, { JCS_YCbCr, PixelFormatInfo::info(formats::NV12), false } }, + { formats::NV21, { JCS_YCbCr, PixelFormatInfo::info(formats::NV21), true } }, + { formats::NV16, { JCS_YCbCr, PixelFormatInfo::info(formats::NV16), false } }, + { formats::NV61, { JCS_YCbCr, PixelFormatInfo::info(formats::NV61), true } }, + { formats::NV24, { JCS_YCbCr, PixelFormatInfo::info(formats::NV24), false } }, + { formats::NV42, { JCS_YCbCr, PixelFormatInfo::info(formats::NV42), true } }, +}; + +const struct JPEGPixelFormatInfo &findPixelInfo(const PixelFormat &format) +{ + static const struct JPEGPixelFormatInfo invalidPixelFormat { + JCS_UNKNOWN, PixelFormatInfo(), false + }; + + const auto iter = pixelInfo.find(format); + if (iter == pixelInfo.end()) { + LOG(JPEG, Error) << "Unsupported pixel format for JPEG compressor: " + << format.toString(); + return invalidPixelFormat; + } + + return iter->second; +} + +} /* namespace */ + +CompressorJPEG::CompressorJPEG() + : quality_(95) +{ + /* \todo: Expand error handling coverage. */ + compress_.err = jpeg_std_error(&jerr_); + + jpeg_create_compress(&compress_); +} + +CompressorJPEG::~CompressorJPEG() +{ + jpeg_destroy_compress(&compress_); +} + +int CompressorJPEG::configure(const StreamConfiguration &cfg) +{ + const struct JPEGPixelFormatInfo info = findPixelInfo(cfg.pixelFormat); + if (info.colorSpace == JCS_UNKNOWN) + return -ENOTSUP; + + compress_.image_width = cfg.size.width; + compress_.image_height = cfg.size.height; + compress_.in_color_space = info.colorSpace; + + compress_.input_components = info.colorSpace == JCS_GRAYSCALE ? 1 : 3; + + jpeg_set_defaults(&compress_); + jpeg_set_quality(&compress_, quality_, TRUE); + + pixelFormatInfo = &info.pixelFormatInfo; + + nv_ = pixelFormatInfo->numPlanes() == 2; + nvSwap_ = info.nvSwap; + + return 0; +} + +void CompressorJPEG::compressRGB(const libcamera::MappedBuffer *frame) +{ + unsigned char *src = static_cast(frame->maps()[0].data()); + unsigned int stride = pixelFormatInfo->stride(compress_.image_width, 0); + + JSAMPROW row_pointer[1]; + + while (compress_.next_scanline < compress_.image_height) { + row_pointer[0] = &src[compress_.next_scanline * stride]; + jpeg_write_scanlines(&compress_, row_pointer, 1); + } +} + +/* + * Compress the incoming buffer from a supported NV format. + * This naively unpacks the semi-planar NV12 to a YUV888 format for libjpeg. + * Utilisation of the RAW api will be implemented on top as a performance + * improvement. + */ +void CompressorJPEG::compressNV(const libcamera::MappedBuffer *frame) +{ + std::vector tmprowbuf(compress_.image_width * 3); + + /* + * \todo Use the raw api, and only unpack the cb/cr samples to new line buffers. + * If possible, see if we can set appropriate pixel strides too to save even that copy. + * + * Possible hints at: + * https://sourceforge.net/p/libjpeg/mailman/message/30815123/ + */ + unsigned int y_stride = pixelFormatInfo->stride(compress_.image_width, 0); + unsigned int c_stride = pixelFormatInfo->stride(compress_.image_width, 1); + + unsigned int horzSubSample = 2 * compress_.image_width / c_stride; + unsigned int vertSubSample = pixelFormatInfo->planes[1].verticalSubSampling; + + unsigned int c_inc = horzSubSample == 1 ? 2 : 0; + unsigned int cb_pos = nvSwap_ ? 1 : 0; + unsigned int cr_pos = nvSwap_ ? 0 : 1; + + const unsigned char *src = static_cast(frame->maps()[0].data()); + const unsigned char *src_c = src + y_stride * compress_.image_height; + + JSAMPROW row_pointer[1]; + row_pointer[0] = &tmprowbuf[0]; + + for (unsigned int y = 0; y < compress_.image_height; y++) { + unsigned char *dst = &tmprowbuf[0]; + + const unsigned char *src_y = src + y * compress_.image_width; + const unsigned char *src_cb = src_c + (y / vertSubSample) * c_stride + cb_pos; + const unsigned char *src_cr = src_c + (y / vertSubSample) * c_stride + cr_pos; + + for (unsigned int x = 0; x < compress_.image_width; x += 2) { + dst[0] = *src_y; + dst[1] = *src_cb; + dst[2] = *src_cr; + src_y++; + src_cb += c_inc; + src_cr += c_inc; + dst += 3; + + dst[0] = *src_y; + dst[1] = *src_cb; + dst[2] = *src_cr; + src_y++; + src_cb += 2; + src_cr += 2; + dst += 3; + } + + jpeg_write_scanlines(&compress_, row_pointer, 1); + } +} + +int CompressorJPEG::compress(const FrameBuffer *source, CompressedImage *jpeg) +{ + MappedFrameBuffer frame(source, PROT_READ); + if (!frame.isValid()) { + LOG(JPEG, Error) << "Failed to map FrameBuffer : " + << strerror(frame.error()); + return -frame.error(); + } + + jpeg_mem_dest(&compress_, &jpeg->data, &jpeg->length); + + jpeg_start_compress(&compress_, TRUE); + + LOG(JPEG, Debug) << "JPEG Compress Starting:" + << " Width: " << compress_.image_width + << " height: " << compress_.image_height; + + if (nv_) + compressNV(&frame); + else + compressRGB(&frame); + + LOG(JPEG, Debug) << "JPEG Compress Completed"; + + jpeg_finish_compress(&compress_); + + return 0; +} + +void CompressorJPEG::free(CompressedImage *jpeg) +{ + ::free(jpeg->data); + jpeg->data = nullptr; +} diff --git a/src/android/jpeg/compressor_jpeg.h b/src/android/jpeg/compressor_jpeg.h new file mode 100644 index 000000000000..3c0c3ff68449 --- /dev/null +++ b/src/android/jpeg/compressor_jpeg.h @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2020, Google Inc. + * + * compressor_jpeg.h - JPEG compression using libjpeg + */ +#ifndef __ANDROID_JPEG_COMPRESSOR_JPEG_H__ +#define __ANDROID_JPEG_COMPRESSOR_JPEG_H__ + +#include "compressor.h" + +#include +#include + +#include "libcamera/internal/buffer.h" +#include "libcamera/internal/formats.h" + +#include + +class CompressorJPEG : public Compressor +{ +public: + CompressorJPEG(); + ~CompressorJPEG(); + + int configure(const libcamera::StreamConfiguration &cfg); + int compress(const libcamera::FrameBuffer *source, CompressedImage *jpeg); + void free(CompressedImage *jpeg); + +private: + void compressRGB(const libcamera::MappedBuffer *frame); + void compressYUV(const libcamera::MappedBuffer *frame); + void compressNV(const libcamera::MappedBuffer *frame); + + struct jpeg_compress_struct compress_; + struct jpeg_error_mgr jerr_; + + unsigned int quality_; + + const libcamera::PixelFormatInfo *pixelFormatInfo; + + bool nv_; + bool nvSwap_; +}; + +#endif /* __ANDROID_JPEG_COMPRESSOR_JPEG_H__ */ diff --git a/src/android/meson.build b/src/android/meson.build index 822cad621f01..51dcd99ee62f 100644 --- a/src/android/meson.build +++ b/src/android/meson.build @@ -6,6 +6,7 @@ android_hal_sources = files([ 'camera_device.cpp', 'camera_metadata.cpp', 'camera_ops.cpp', + 'jpeg/compressor_jpeg.cpp', ]) android_camera_metadata_sources = files([ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index 3aad4386ffc2..d78e2c1f6eb8 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -124,6 +124,8 @@ if get_option('android') libcamera_sources += android_hal_sources includes += android_includes libcamera_link_with += android_camera_metadata + + libcamera_deps += dependency('libjpeg') endif # We add '/' to the build_rpath as a 'safe' path to act as a boolean flag. From patchwork Mon Aug 3 16:18:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 9152 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 E61B7BD86F for ; Mon, 3 Aug 2020 16:18:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id B00D161A12; Mon, 3 Aug 2020 18:18:30 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="UHERhVzu"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 4BA6B61B35 for ; Mon, 3 Aug 2020 18:18:27 +0200 (CEST) Received: from Q.local (cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id DC345112C; Mon, 3 Aug 2020 18:18:26 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1596471507; bh=ewZFYXoI8DWP3N+YUCUoCaloJSkAZAnh3QdATsykt4Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UHERhVzuq8Oj7mo5X1KEDonhwMY0y8aA1bZ8EW31J1eyBZnb7kVoqbdklj98B7Mqd OhQclHYeAngLGXTYe3o1ptfaan9gbWYmj1fKYNgk6x5lHgj99+RNAjXI+I6XfWxcP2 nVavpMqPuWDUi3GmWxvWnbdyqn1BUuTku8E1uOQs= From: Kieran Bingham To: libcamera devel Date: Mon, 3 Aug 2020 17:18:16 +0100 Message-Id: <20200803161816.107113-13-kieran.bingham@ideasonboard.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> References: <20200803161816.107113-1-kieran.bingham@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 12/12] android: camera_device: Support MJPEG stream construction 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" MJPEG streams must be created referencing a libcamera stream. This stream may already be provided by the request configuration, in which case the existing stream is utilised. If no compatible stream is available to encode, a new stream is requested from the libcamera configuration. Signed-off-by: Kieran Bingham --- This is probably the main patch holding me up at the moment, as there are just so many bits I'm not happy with. - Clean up the "Fill in the JPEG blob header." - Added streams for different MPEG requirements don't have their own buffers (only just realised that today, so I think we should just pull that out for a first integration) - JPEG compression should run in a thread. - I really dislike the current mapping between camera3_stream to libcamera stream with the index - The JPEG compressor is not currently destructed. I was going to handle that by moving the CameraStream to a class which would then destroy the compressor in it's destructor - but that hasn't gone down that path, maybe it still should. - Metadata additions are horrible to keep in sync (especially through rebases) - More stuff ;-) Anyway, here's the current WIP - and it works ... so hey that's a bonus ;-) src/android/camera_device.cpp | 202 +++++++++++++++++++++++++++++++++- src/android/camera_device.h | 8 ++ 2 files changed, 204 insertions(+), 6 deletions(-) diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp index e23ab055d012..42b08cfc5fed 100644 --- a/src/android/camera_device.cpp +++ b/src/android/camera_device.cpp @@ -8,6 +8,7 @@ #include "camera_device.h" #include "camera_ops.h" +#include #include #include @@ -21,6 +22,8 @@ #include "camera_metadata.h" #include "system/graphics.h" +#include "jpeg/compressor_jpeg.h" + using namespace libcamera; namespace { @@ -80,6 +83,40 @@ const std::map camera3FormatsMap = { LOG_DECLARE_CATEGORY(HAL); +class MappedCamera3Buffer : public MappedBuffer +{ +public: + MappedCamera3Buffer(const buffer_handle_t camera3buffer, int flags) + { + maps_.reserve(camera3buffer->numFds); + error_ = 0; + + for (int i = 0; i < camera3buffer->numFds; i++) { + if (camera3buffer->data[i] == -1) + continue; + + off_t length = lseek(camera3buffer->data[i], 0, SEEK_END); + if (length < 0) { + error_ = errno; + LOG(HAL, Error) << "Failed to query plane length"; + break; + } + + void *address = mmap(nullptr, length, flags, MAP_SHARED, + camera3buffer->data[i], 0); + if (address == MAP_FAILED) { + error_ = errno; + LOG(HAL, Error) << "Failed to mmap plane"; + break; + } + + maps_.emplace_back(static_cast(address), static_cast(length)); + } + + valid_ = error_ == 0; + } +}; + /* * \struct Camera3RequestDescriptor * @@ -368,9 +405,9 @@ std::tuple CameraDevice::calculateStaticMetadataSize() { /* * \todo Keep this in sync with the actual number of entries. - * Currently: 50 entries, 647 bytes of static metadata + * Currently: 51 entries, 651 bytes of static metadata */ - uint32_t numEntries = 50; + uint32_t numEntries = 51; uint32_t byteSize = 651; /* @@ -527,6 +564,9 @@ const camera_metadata_t *CameraDevice::getStaticMetadata() availableThumbnailSizes.data(), availableThumbnailSizes.size()); + int32_t jpegMaxSize = 13 << 20; /* 13631488 from USB HAL */ + staticMetadata_->addEntry(ANDROID_JPEG_MAX_SIZE, &jpegMaxSize, 1); + /* Sensor static metadata. */ int32_t pixelArraySize[] = { 2592, 1944, @@ -729,6 +769,7 @@ const camera_metadata_t *CameraDevice::getStaticMetadata() ANDROID_CONTROL_AWB_LOCK_AVAILABLE, ANDROID_CONTROL_AVAILABLE_MODES, ANDROID_JPEG_AVAILABLE_THUMBNAIL_SIZES, + ANDROID_JPEG_MAX_SIZE, ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, @@ -795,6 +836,9 @@ const camera_metadata_t *CameraDevice::getStaticMetadata() ANDROID_SENSOR_EXPOSURE_TIME, ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, ANDROID_STATISTICS_SCENE_FLICKER, + ANDROID_JPEG_SIZE, + ANDROID_JPEG_QUALITY, + ANDROID_JPEG_ORIENTATION, }; staticMetadata_->addEntry(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, availableResultKeys.data(), @@ -956,6 +1000,7 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) */ unsigned int streamIndex = 0; + /* First handle all non-MJPEG streams */ for (unsigned int i = 0; i < stream_list->num_streams; ++i) { camera3_stream_t *stream = stream_list->streams[i]; @@ -971,6 +1016,18 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) if (!format.isValid()) return -EINVAL; + stream->priv = static_cast(&streams_[i]); + streams_[i].format = format; + streams_[i].size = Size(stream->width, stream->height); + + /* Defer handling of MJPEG streams until all others are known. */ + if (format == formats::MJPEG) { + LOG(HAL, Info) << "Handling MJPEG stream"; + + streams_[i].index = -1; + continue; + } + StreamConfiguration streamConfiguration; streamConfiguration.size.width = stream->width; @@ -980,7 +1037,61 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) config_->addConfiguration(streamConfiguration); streams_[i].index = streamIndex++; - stream->priv = static_cast(&streams_[i]); + } + + /* Now handle MJPEG streams, adding a new stream if required. */ + for (unsigned int i = 0; i < stream_list->num_streams; ++i) { + camera3_stream_t *stream = stream_list->streams[i]; + CameraStream *cameraStream = &streams_[i]; + + /* Only process MJPEG streams */ + if (cameraStream->format != formats::MJPEG) + continue; + + bool match = false; + + /* Search for a compatible stream */ + for (unsigned int j = 0; j < config_->size(); j++) { + StreamConfiguration &cfg = config_->at(j); + + /* + * The PixelFormat must also be compatible with the + * encoder. + */ + if (cfg.size == cameraStream->size) { + LOG(HAL, Info) + << "Stream " << i + << " using libcamera stream " + << j; + + match = true; + cameraStream->index = j; + } + } + + /* + * Without a compatible match for JPEG encoding we must + * introduce a new stream to satisfy the request requirements. + */ + if (!match) { + StreamConfiguration streamConfiguration; + + /* + * \todo: The pixelFormat should be a 'best-fit' choice + * and may require a validation cycle. This is not yet + * handled, and should be considered as part of any + * stream configuration reworks. + */ + streamConfiguration.size.width = stream->width; + streamConfiguration.size.height = stream->height; + streamConfiguration.pixelFormat = formats::NV12; + + LOG(HAL, Info) << "Adding " << streamConfiguration.toString() + << " for MJPEG support"; + + config_->addConfiguration(streamConfiguration); + streams_[i].index = streamIndex++; + } } switch (config_->validate()) { @@ -1007,6 +1118,18 @@ int CameraDevice::configureStreams(camera3_stream_configuration_t *stream_list) /* Use the bufferCount confirmed by the validation process. */ stream->max_buffers = cfg.bufferCount; + + /* + * Construct a software compressor for MJPEG streams from the + * chosen libcamera source stream. + */ + if (cameraStream->format == formats::MJPEG) { + cameraStream->jpeg = new CompressorJPEG(); + cameraStream->jpeg->configure(cfg); + } else { + /* Either construct this correctly, or use a better interface */ + cameraStream->jpeg = nullptr; + } } /* @@ -1101,6 +1224,10 @@ int CameraDevice::processCaptureRequest(camera3_capture_request_t *camera3Reques descriptor->buffers[i].stream = camera3Buffers[i].stream; descriptor->buffers[i].buffer = camera3Buffers[i].buffer; + /* Software streams are handled after hardware streams complete. */ + if (cameraStream->format == formats::MJPEG) + continue; + /* * Create a libcamera buffer using the dmabuf descriptors of * the camera3Buffer for each stream. The FrameBuffer is @@ -1157,9 +1284,72 @@ void CameraDevice::requestComplete(Request *request) resultMetadata = getResultMetadata(descriptor->frameNumber, buffer->metadata().timestamp); - /* Prepare to call back the Android camera stack. */ + /* Handle any JPEG compression */ + for (unsigned int i = 0; i < descriptor->numBuffers; ++i) { + CameraStream *cameraStream = + static_cast(descriptor->buffers[i].stream->priv); + Compressor *compressor = cameraStream->jpeg; + /* Only handle streams with compression enabled. */ + if (!compressor) + continue; + StreamConfiguration *streamConfiguration = &config_->at(cameraStream->index); + Stream *stream = streamConfiguration->stream(); + FrameBuffer *buffer = request->findBuffer(stream); + if (!buffer) { + LOG(HAL, Error) << "Failed to find a source stream buffer"; + continue; + } + + MappedCamera3Buffer mapped(*descriptor->buffers[i].buffer, PROT_READ | PROT_WRITE); + if (!mapped.isValid()) { + LOG(HAL, Error) << "Failed to mmap android blob buffer"; + continue; + } + + CompressedImage output; + output.data = static_cast(mapped.maps()[0].data()); + output.length = mapped.maps()[0].size(); + + int ret = compressor->compress(buffer, &output); + if (ret) { + LOG(HAL, Error) << "Failed to compress stream image"; + status = CAMERA3_BUFFER_STATUS_ERROR; + } + + /* Fill in the JPEG blob header. */ + { + /* The mapped size of the buffer is being returned as substantially larger + * than the requested JPEG_MAX_SIZE (re-defined here). + * Utilise this static size as a workaround to ensure the correct offset + * of the blob is determined. + * + * \todo Investigate and fix the root cause of this. + */ + int32_t max_size = { 13 << 20 }; + uint8_t *resultPtr = static_cast(mapped.maps()[0].data()) + + max_size - sizeof(struct camera3_jpeg_blob); + auto *blob = reinterpret_cast(resultPtr); + + blob->jpeg_blob_id = CAMERA3_JPEG_BLOB_ID; + blob->jpeg_size = output.length; + } + + /* Update the JPEG result Metadata. */ + resultMetadata->addEntry(ANDROID_JPEG_SIZE, + &output.length, 1); + + const uint32_t jpeg_quality = 95; + resultMetadata->addEntry(ANDROID_JPEG_QUALITY, + &jpeg_quality, 1); + + const uint32_t jpeg_orientation = 0; + resultMetadata->addEntry(ANDROID_JPEG_ORIENTATION, + &jpeg_orientation, 1); + } + + /* Prepare to call back the Android camera stack. */ camera3_capture_result_t captureResult = {}; captureResult.frame_number = descriptor->frameNumber; captureResult.num_output_buffers = descriptor->numBuffers; @@ -1240,10 +1430,10 @@ std::unique_ptr CameraDevice::getResultMetadata(int frame_number { /* * \todo Keep this in sync with the actual number of entries. - * Currently: 12 entries, 36 bytes + * Currently: 17 entries, 58 bytes */ std::unique_ptr resultMetadata = - std::make_unique(15, 50); + std::make_unique(17, 58); if (!resultMetadata->isValid()) { LOG(HAL, Error) << "Failed to allocate static metadata"; return nullptr; diff --git a/src/android/camera_device.h b/src/android/camera_device.h index 00472c219388..c9ece426a00e 100644 --- a/src/android/camera_device.h +++ b/src/android/camera_device.h @@ -23,6 +23,8 @@ #include "libcamera/internal/log.h" #include "libcamera/internal/message.h" +#include "jpeg/compressor_jpeg.h" + class CameraMetadata; struct CameraStream { @@ -32,6 +34,12 @@ struct CameraStream { * one or more streams to the Android framework. */ unsigned int index; + + libcamera::PixelFormat format; + libcamera::Size size; + + /* Make sure this gets destructed correctly */ + CompressorJPEG *jpeg; }; class CameraDevice : protected libcamera::Loggable