[{"id":28970,"web_url":"https://patchwork.libcamera.org/comment/28970/","msgid":"<171046290174.1913896.11420169574014023828@ping.linuxembedded.co.uk>","date":"2024-03-15T00:35:01","subject":"Re: [PATCH v2 03/14] libcamera: v4l2_subdevice: Expose media bus\n\tformat info as internal API","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Laurent Pinchart (2024-03-15 00:16:02)\n> The V4L2SubdeviceFormatInfo structure, internal to the\n> v4l2_subdevice.cpp compilation unit, contains information about media\n> bus formats that will be useful in other parts of libcamera. To prepare\n> for this, expose the structure in the v4l2_subdevice.h header and turn\n> it into a class with a similar design as PixelFormatInfo.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>\n> ---\n> Changes since v1:\n> \n> - Improve MediaBusFormatInfo::code documentation\n\nLGTM\n\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n>  include/libcamera/internal/v4l2_subdevice.h | 13 +++\n>  src/libcamera/v4l2_subdevice.cpp            | 94 ++++++++++++++-------\n>  2 files changed, 76 insertions(+), 31 deletions(-)\n> \n> diff --git a/include/libcamera/internal/v4l2_subdevice.h b/include/libcamera/internal/v4l2_subdevice.h\n> index 17db311bcfb3..a4df9ddfd322 100644\n> --- a/include/libcamera/internal/v4l2_subdevice.h\n> +++ b/include/libcamera/internal/v4l2_subdevice.h\n> @@ -29,6 +29,19 @@ namespace libcamera {\n>  \n>  class MediaDevice;\n>  \n> +class MediaBusFormatInfo\n> +{\n> +public:\n> +       bool isValid() const { return code != 0; }\n> +\n> +       static const MediaBusFormatInfo &info(uint32_t code);\n> +\n> +       const char *name;\n> +       uint32_t code;\n> +       unsigned int bitsPerPixel;\n> +       PixelFormatInfo::ColourEncoding colourEncoding;\n> +};\n> +\n>  struct V4L2SubdeviceCapability final : v4l2_subdev_capability {\n>         bool isReadOnly() const\n>         {\n> diff --git a/src/libcamera/v4l2_subdevice.cpp b/src/libcamera/v4l2_subdevice.cpp\n> index 1286e7c385c4..4918e28e5f18 100644\n> --- a/src/libcamera/v4l2_subdevice.cpp\n> +++ b/src/libcamera/v4l2_subdevice.cpp\n> @@ -36,28 +36,40 @@ namespace libcamera {\n>  \n>  LOG_DECLARE_CATEGORY(V4L2)\n>  \n> +/**\n> + * \\class MediaBusFormatInfo\n> + * \\brief Information about media bus formats\n> + *\n> + * The MediaBusFormatInfo class groups together information describing a media\n> + * bus format. It facilitates handling of media bus formats by providing data\n> + * commonly used in pipeline handlers.\n> + *\n> + * \\var MediaBusFormatInfo::name\n> + * \\brief The format name as a human-readable string, used as the text\n> + * representation of the format\n> + *\n> + * \\var MediaBusFormatInfo::code\n> + * \\brief The media bus format code described by this instance (MEDIA_BUS_FMT_*)\n> + *\n> + * \\var MediaBusFormatInfo::bitsPerPixel\n> + * \\brief The average number of bits per pixel\n> + *\n> + * The number of bits per pixel averages the total number of bits for all\n> + * colour components over the whole image, excluding any padding bits or\n> + * padding pixels.\n> + *\n> + * For formats that transmit multiple or fractional pixels per sample, the\n> + * value will differ from the bus width.\n> + *\n> + * Formats that don't have a fixed number of bits per pixel, such as compressed\n> + * formats, report 0 in this field.\n> + *\n> + * \\var MediaBusFormatInfo::colourEncoding\n> + * \\brief The colour encoding type\n> + */\n> +\n>  namespace {\n>  \n> -/*\n> - * \\struct MediaBusFormatInfo\n> - * \\brief Information about media bus formats\n> - * \\param name Name of MBUS format\n> - * \\param code The media bus format code\n> - * \\param bitsPerPixel Bits per pixel\n> - * \\param colourEncoding Type of colour encoding\n> - */\n> -struct MediaBusFormatInfo {\n> -       const char *name;\n> -       uint32_t code;\n> -       unsigned int bitsPerPixel;\n> -       PixelFormatInfo::ColourEncoding colourEncoding;\n> -};\n> -\n> -/*\n> - * \\var mediaBusFormatInfo\n> - * \\brief A map that associates MediaBusFormatInfo struct to V4L2 media\n> - * bus codes\n> - */\n>  const std::map<uint32_t, MediaBusFormatInfo> mediaBusFormatInfo{\n>         /* This table is sorted to match the order in linux/media-bus-format.h */\n>         { MEDIA_BUS_FMT_RGB444_2X8_PADHI_BE, {\n> @@ -557,6 +569,33 @@ const std::map<uint32_t, MediaBusFormatInfo> mediaBusFormatInfo{\n>  \n>  } /* namespace */\n>  \n> +/**\n> + * \\fn bool MediaBusFormatInfo::isValid() const\n> + * \\brief Check if the media bus format info is valid\n> + * \\return True if the media bus format info is valid, false otherwise\n> + */\n> +\n> +/**\n> + * \\brief Retrieve information about a media bus format\n> + * \\param[in] code The media bus format code\n> + * \\return The MediaBusFormatInfo describing the \\a code if known, or an invalid\n> + * MediaBusFormatInfo otherwise\n> + */\n> +const MediaBusFormatInfo &MediaBusFormatInfo::info(uint32_t code)\n> +{\n> +       static const MediaBusFormatInfo invalid{};\n> +\n> +       const auto it = mediaBusFormatInfo.find(code);\n> +       if (it == mediaBusFormatInfo.end()) {\n> +               LOG(V4L2, Warning)\n> +                       << \"Unsupported media bus format \"\n> +                       << utils::hex(code, 4);\n> +               return invalid;\n> +       }\n> +\n> +       return it->second;\n> +}\n> +\n>  /**\n>   * \\struct V4L2SubdeviceCapability\n>   * \\brief struct v4l2_subdev_capability object wrapper and helpers\n> @@ -653,14 +692,7 @@ const std::string V4L2SubdeviceFormat::toString() const\n>   */\n>  uint8_t V4L2SubdeviceFormat::bitsPerPixel() const\n>  {\n> -       const auto it = mediaBusFormatInfo.find(mbus_code);\n> -       if (it == mediaBusFormatInfo.end()) {\n> -               LOG(V4L2, Error) << \"No information available for format '\"\n> -                                << *this << \"'\";\n> -               return 0;\n> -       }\n> -\n> -       return it->second.bitsPerPixel;\n> +       return MediaBusFormatInfo::info(mbus_code).bitsPerPixel;\n>  }\n>  \n>  /**\n> @@ -927,9 +959,9 @@ std::optional<ColorSpace> V4L2Subdevice::toColorSpace(const v4l2_mbus_framefmt &\n>                 return std::nullopt;\n>  \n>         PixelFormatInfo::ColourEncoding colourEncoding;\n> -       auto iter = mediaBusFormatInfo.find(format.code);\n> -       if (iter != mediaBusFormatInfo.end()) {\n> -               colourEncoding = iter->second.colourEncoding;\n> +       const MediaBusFormatInfo &info = MediaBusFormatInfo::info(format.code);\n> +       if (info.isValid()) {\n> +               colourEncoding = info.colourEncoding;\n>         } else {\n>                 LOG(V4L2, Warning)\n>                         << \"Unknown subdev format \"\n> -- \n> Regards,\n> \n> Laurent Pinchart\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 51E09BD808\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Mar 2024 00:35:06 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B929362C91;\n\tFri, 15 Mar 2024 01:35:05 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A035B62C87\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Mar 2024 01:35:04 +0100 (CET)","from pendragon.ideasonboard.com\n\t(aztw-30-b2-v4wan-166917-cust845.vm26.cable.virginm.net\n\t[82.37.23.78])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E8946667;\n\tFri, 15 Mar 2024 01:34:40 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"ijR6vvjz\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1710462881;\n\tbh=VOiAbt1P5fPFIyWhz9eSHjefNYCvm/2LbXxS1PhOVj4=;\n\th=In-Reply-To:References:Subject:From:To:Date:From;\n\tb=ijR6vvjzGeeNQYTdITUGW4GSldxVRN8b99cL3HDK8tpnL7Wuq1iwpKjfMPm3P9XvB\n\tWgTz7GwgcDIwyDDKnlfiTkLFks6n4c8nSm8mWLfYpYgirmdgCoOEXsSpXswzjCZbBf\n\t16PPg75FghTvtsSwINrZnXVRSedeS+RTa46trw8c=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20240315001613.2033-4-laurent.pinchart@ideasonboard.com>","References":"<20240315001613.2033-1-laurent.pinchart@ideasonboard.com>\n\t<20240315001613.2033-4-laurent.pinchart@ideasonboard.com>","Subject":"Re: [PATCH v2 03/14] libcamera: v4l2_subdevice: Expose media bus\n\tformat info as internal API","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 15 Mar 2024 00:35:01 +0000","Message-ID":"<171046290174.1913896.11420169574014023828@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]