/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* formats.cpp - libcamera image formats
*/
#include "libcamera/internal/formats.h"
#include <algorithm>
#include <errno.h>
#include <libcamera/base/log.h>
#include <libcamera/base/utils.h>
#include <libcamera/formats.h>
/**
* \file internal/formats.h
* \brief Types and helper functions to handle libcamera image formats
*/
namespace libcamera {
LOG_DEFINE_CATEGORY(Formats)
/**
* \class PixelFormatInfo
* \brief Information about pixel formats
*
* The PixelFormatInfo class groups together information describing a pixel
* format. It facilitates handling of pixel formats by providing data commonly
* used in pipeline handlers.
*
* \var PixelFormatInfo::name
* \brief The format name as a human-readable string, used as the test
* representation of the PixelFormat
*
* \var PixelFormatInfo::format
* \brief The PixelFormat described by this instance
*
* \var PixelFormatInfo::v4l2Formats
* \brief The V4L2 pixel formats corresponding to the PixelFormat
*
* Multiple V4L2 formats may exist for one PixelFormat when the format uses
* multiple planes, as V4L2 defines separate 4CCs for contiguous and separate
* planes formats. The two entries in the array store the contiguous and
* non-contiguous V4L2 formats respectively. If the PixelFormat isn't a
* multiplanar format, or if no corresponding non-contiguous V4L2 format
* exists, the second entry is invalid.
*
* \var PixelFormatInfo::bitsPerPixel
* \brief The average number of bits per pixel
*
* The number per pixel averages the total number of bits for all colour
* components over the whole image, excluding any padding bits or padding
* pixels.
*
* For formats that store pixels with bit padding within words, only the
* effective bits are taken into account. For instance, 12-bit Bayer data
* stored in two bytes per pixel report 12, not 16, in this field.
*
* Formats that don't have a fixed number of bits per pixel, such as compressed
* formats, report 0 in this field.
*
* \var PixelFormatInfo::colourEncoding
* \brief The colour encoding type
*
* \var PixelFormatInfo::packed
* \brief Tell if multiple pixels are packed in the same bytes
*
* Packed formats are defined as storing data from multiple pixels in the same
* bytes. For instance, 12-bit Bayer data with two pixels stored in three bytes
* is packed, while the same data stored with 4 bits of padding in two bytes
* per pixel is not packed.
*
* \var PixelFormatInfo::pixelsPerGroup
* \brief The number of pixels in a pixel group
*
* A pixel group is defined as the minimum number of pixels (including padding)
* necessary in a row when the image has only one column of effective pixels.
* pixelsPerGroup refers to this value. PixelFormatInfo::Plane::bytesPerGroup,
* then, refers to the number of bytes that a pixel group consumes. This
* definition of a pixel group allows simple calculation of stride, as
* ceil(width / pixelsPerGroup) * bytesPerGroup. These values are determined
* only in terms of a row. The ceiling accounts for padding.
*
* A pixel group has a second constraint, such that the pixel group
* (bytesPerGroup and pixelsPerGroup) is the smallest repeatable unit.
* What this means is that, for example, in the IPU3 formats, if there is only
* one column of effective pixels, it looks like it could be fit in 5 bytes
* with 3 padding pixels (for a total of 4 pixels over 5 bytes). However, this
* unit is not repeatable, as at the 7th group in the same row, the pattern
* is broken. Therefore, the pixel group for IPU3 formats must be 25 pixels
* over 32 bytes.
*
* For example, for something simple like BGR888, it is self-explanatory:
* the pixel group size is 1, and the bytes necessary is 3, and there is
* only one plane with no (= 1) vertical subsampling. For YUYV, the
* CbCr pair is shared between two pixels, so even if you have only one
* pixel, you would still need a padded second Y sample, therefore the pixel
* group size is 2, and bytes necessary is 4. YUYV also has no vertical
* subsampling. NV12 has a pixel group size of 2 pixels, due to the CbCr plane.
* The bytes per group then, for both planes, is 2. The first plane has no
* vertical subsampling, but the second plane is subsampled by a factor of 2.
*
* The IPU3 raw Bayer formats are single-planar, and have a pixel group size of
* 25, consuming 32 bytes, due to the packing pattern being repeated in memory
* every 32 bytes. The IPU3 hardware, however, has an additional constraint on
* the DMA burst size, requiring lines to be multiple of 64 bytes. This isn't an
|