Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: v4l2: add support for colorspace conversion API (CSC) for video capture

For video capture it is the driver that reports the colorspace,
transfer function, Y'CbCr/HSV encoding and quantization range
used by the video, and there is no way to request something
different, even though many HDTV receivers have some sort of
colorspace conversion capabilities.

For output video this feature already exists since the application
specifies this information for the video format it will send out, and
the transmitter will enable any available CSC if a format conversion has
to be performed in order to match the capabilities of the sink.

For video capture we propose adding new v4l2_pix_format flag:
V4L2_PIX_FMT_FLAG_SET_CSC. The flag is set by the application,
the driver will interpret the colorspace, xfer_func, ycbcr_enc/hsv_enc
and quantization fields as the requested colorspace information and will
attempt to do the conversion it supports.

Drivers set the flags
V4L2_FMT_FLAG_CSC_COLORSPACE,
V4L2_FMT_FLAG_CSC_XFER_FUNC,
V4L2_FMT_FLAG_CSC_YCBCR_ENC/V4L2_FMT_FLAG_CSC_HSV_ENC,
V4L2_FMT_FLAG_CSC_QUANTIZATION,
in the flags field of the struct v4l2_fmtdesc during enumeration to
indicate that they support colorspace conversion for the respective field.

Drivers do not have to actually look at the flags. If the flags are not
set, then the fields 'colorspace', 'xfer_func', 'ycbcr_enc/hsv_enc',
and 'quantization' are set to the default values by the core, i.e. just
pass on the received format without conversion.

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@collabora.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

authored by

Dafna Hirschfeld and committed by
Mauro Carvalho Chehab
b38c73ca 1698a7f1

+109 -17
+4 -12
Documentation/userspace-api/media/v4l/pixfmt-v4l2-mplane.rst
··· 98 98 * - __u8 99 99 - ``ycbcr_enc`` 100 100 - Y'CbCr encoding, from enum :c:type:`v4l2_ycbcr_encoding`. 101 - This information supplements the ``colorspace`` and must be set by 102 - the driver for capture streams and by the application for output 103 - streams, see :ref:`colorspaces`. 101 + See struct :c:type:`v4l2_pix_format`. 104 102 * - __u8 105 103 - ``hsv_enc`` 106 104 - HSV encoding, from enum :c:type:`v4l2_hsv_encoding`. 107 - This information supplements the ``colorspace`` and must be set by 108 - the driver for capture streams and by the application for output 109 - streams, see :ref:`colorspaces`. 105 + See struct :c:type:`v4l2_pix_format`. 110 106 * - } 111 107 - 112 108 * - __u8 113 109 - ``quantization`` 114 110 - Quantization range, from enum :c:type:`v4l2_quantization`. 115 - This information supplements the ``colorspace`` and must be set by 116 - the driver for capture streams and by the application for output 117 - streams, see :ref:`colorspaces`. 111 + See struct :c:type:`v4l2_pix_format`. 118 112 * - __u8 119 113 - ``xfer_func`` 120 114 - Transfer function, from enum :c:type:`v4l2_xfer_func`. 121 - This information supplements the ``colorspace`` and must be set by 122 - the driver for capture streams and by the application for output 123 - streams, see :ref:`colorspaces`. 115 + See struct :c:type:`v4l2_pix_format`. 124 116 * - __u8 125 117 - ``reserved[7]`` 126 118 - Reserved for future extensions. Should be zeroed by drivers and
+59 -5
Documentation/userspace-api/media/v4l/pixfmt-v4l2.rst
··· 109 109 - Image colorspace, from enum :c:type:`v4l2_colorspace`. 110 110 This information supplements the ``pixelformat`` and must be set 111 111 by the driver for capture streams and by the application for 112 - output streams, see :ref:`colorspaces`. 112 + output streams, see :ref:`colorspaces`. If the application sets the 113 + flag ``V4L2_PIX_FMT_FLAG_SET_CSC`` then the application can set 114 + this field for a capture stream to request a specific colorspace 115 + for the captured image data. If the driver cannot handle requested 116 + conversion, it will return another supported colorspace. 117 + The driver indicates that colorspace conversion is supported by setting 118 + the flag V4L2_FMT_FLAG_CSC_COLORSPACE in the corresponding struct 119 + :c:type:`v4l2_fmtdesc` during enumeration. See :ref:`fmtdesc-flags`. 113 120 * - __u32 114 121 - ``priv`` 115 122 - This field indicates whether the remaining fields of the ··· 153 146 - Y'CbCr encoding, from enum :c:type:`v4l2_ycbcr_encoding`. 154 147 This information supplements the ``colorspace`` and must be set by 155 148 the driver for capture streams and by the application for output 156 - streams, see :ref:`colorspaces`. 149 + streams, see :ref:`colorspaces`. If the application sets the 150 + flag ``V4L2_PIX_FMT_FLAG_SET_CSC`` then the application can set 151 + this field for a capture stream to request a specific Y'CbCr encoding 152 + for the captured image data. If the driver cannot handle requested 153 + conversion, it will return another supported encoding. 154 + This field is ignored for HSV pixelformats. The driver indicates that 155 + ycbcr_enc conversion is supported by setting the flag 156 + V4L2_FMT_FLAG_CSC_YCBCR_ENC in the corresponding struct 157 + :c:type:`v4l2_fmtdesc` during enumeration. See :ref:`fmtdesc-flags`. 157 158 * - __u32 158 159 - ``hsv_enc`` 159 160 - HSV encoding, from enum :c:type:`v4l2_hsv_encoding`. 160 161 This information supplements the ``colorspace`` and must be set by 161 162 the driver for capture streams and by the application for output 162 - streams, see :ref:`colorspaces`. 163 + streams, see :ref:`colorspaces`. If the application sets the flag 164 + ``V4L2_PIX_FMT_FLAG_SET_CSC`` then the application can set this 165 + field for a capture stream to request a specific HSV encoding for the 166 + captured image data. If the driver cannot handle requested 167 + conversion, it will return another supported encoding. 168 + This field is ignored for non-HSV pixelformats. The driver indicates 169 + that hsv_enc conversion is supported by setting the flag 170 + V4L2_FMT_FLAG_CSC_HSV_ENC in the corresponding struct 171 + :c:type:`v4l2_fmtdesc` during enumeration. See :ref:`fmtdesc-flags`. 163 172 * - } 164 173 - 165 174 * - __u32 ··· 183 160 - Quantization range, from enum :c:type:`v4l2_quantization`. 184 161 This information supplements the ``colorspace`` and must be set by 185 162 the driver for capture streams and by the application for output 186 - streams, see :ref:`colorspaces`. 163 + streams, see :ref:`colorspaces`. If the application sets the flag 164 + ``V4L2_PIX_FMT_FLAG_SET_CSC`` then the application can set 165 + this field for a capture stream to request a specific quantization 166 + range for the captured image data. If the driver cannot handle requested 167 + conversion, it will return another supported quantization. 168 + The driver indicates that quantization conversion is supported by setting 169 + the flag V4L2_FMT_FLAG_CSC_QUANTIZATION in the corresponding struct 170 + :c:type:`v4l2_fmtdesc` during enumeration. See :ref:`fmtdesc-flags`. 187 171 * - __u32 188 172 - ``xfer_func`` 189 173 - Transfer function, from enum :c:type:`v4l2_xfer_func`. 190 174 This information supplements the ``colorspace`` and must be set by 191 175 the driver for capture streams and by the application for output 192 - streams, see :ref:`colorspaces`. 176 + streams, see :ref:`colorspaces`. If the application sets the flag 177 + ``V4L2_PIX_FMT_FLAG_SET_CSC`` then the application can set 178 + this field for a capture stream to request a specific transfer function 179 + for the captured image data. If the driver cannot handle requested 180 + conversion, it will return another supported transfer function. 181 + The driver indicates that xfer_func conversion is supported by setting 182 + the flag V4L2_FMT_FLAG_CSC_XFER_FUNC in the corresponding struct 183 + :c:type:`v4l2_fmtdesc` during enumeration. See :ref:`fmtdesc-flags`. 193 184 194 185 .. tabularcolumns:: |p{6.6cm}|p{2.2cm}|p{8.7cm}| 195 186 ··· 221 184 by RGBA values (128, 192, 255, 128), the same pixel described with 222 185 premultiplied colors would be described by RGBA values (64, 96, 223 186 128, 128) 187 + * .. _`v4l2-pix-fmt-flag-set-csc`: 188 + 189 + - ``V4L2_PIX_FMT_FLAG_SET_CSC`` 190 + - 0x00000002 191 + - Set by the application. It is only used for capture and is 192 + ignored for output streams. If set, then request the device to do 193 + colorspace conversion from the received colorspace to the requested 194 + colorspace values. If the colorimetry field (``colorspace``, ``xfer_func``, 195 + ``ycbcr_enc``, ``hsv_enc`` or ``quantization``) is set to ``*_DEFAULT``, 196 + then that colorimetry setting will remain unchanged from what was received. 197 + So in order to change the quantization, only the ``quantization`` field shall 198 + be set to non default value (``V4L2_QUANTIZATION_FULL_RANGE`` or 199 + ``V4L2_QUANTIZATION_LIM_RANGE``) and all other colorimetry fields shall 200 + be set to ``*_DEFAULT``. 201 + 202 + To check which conversions are supported by the hardware for the current 203 + pixel format, see :ref:`fmtdesc-flags`.
+35
Documentation/userspace-api/media/v4l/vidioc-enum-fmt.rst
··· 191 191 This flag can only be used in combination with the 192 192 ``V4L2_FMT_FLAG_COMPRESSED`` flag, since this applies to 193 193 compressed formats only. This flag is valid for stateful encoders only. 194 + * - ``V4L2_FMT_FLAG_CSC_COLORSPACE`` 195 + - 0x0020 196 + - The driver allows the application to try to change the default 197 + colorspace. This flag is relevant only for capture devices. 198 + The application can ask to configure the colorspace of the capture device 199 + when calling the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl with 200 + :ref:`V4L2_PIX_FMT_FLAG_SET_CSC <v4l2-pix-fmt-flag-set-csc>` set. 201 + * - ``V4L2_FMT_FLAG_CSC_XFER_FUNC`` 202 + - 0x0040 203 + - The driver allows the application to try to change the default 204 + transfer function. This flag is relevant only for capture devices. 205 + The application can ask to configure the transfer function of the capture 206 + device when calling the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl with 207 + :ref:`V4L2_PIX_FMT_FLAG_SET_CSC <v4l2-pix-fmt-flag-set-csc>` set. 208 + * - ``V4L2_FMT_FLAG_CSC_YCBCR_ENC`` 209 + - 0x0080 210 + - The driver allows the application to try to change the default 211 + Y'CbCr encoding. This flag is relevant only for capture devices. 212 + The application can ask to configure the Y'CbCr encoding of the capture device 213 + when calling the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl with 214 + :ref:`V4L2_PIX_FMT_FLAG_SET_CSC <v4l2-pix-fmt-flag-set-csc>` set. 215 + * - ``V4L2_FMT_FLAG_CSC_HSV_ENC`` 216 + - 0x0080 217 + - The driver allows the application to try to change the default 218 + HSV encoding. This flag is relevant only for capture devices. 219 + The application can ask to configure the HSV encoding of the capture device 220 + when calling the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl with 221 + :ref:`V4L2_PIX_FMT_FLAG_SET_CSC <v4l2-pix-fmt-flag-set-csc>` set. 222 + * - ``V4L2_FMT_FLAG_CSC_QUANTIZATION`` 223 + - 0x0100 224 + - The driver allows the application to try to change the default 225 + quantization. This flag is relevant only for capture devices. 226 + The application can ask to configure the quantization of the capture 227 + device when calling the :ref:`VIDIOC_S_FMT <VIDIOC_G_FMT>` ioctl with 228 + :ref:`V4L2_PIX_FMT_FLAG_SET_CSC <v4l2-pix-fmt-flag-set-csc>` set. 194 229 195 230 196 231 Return Value
+5
Documentation/userspace-api/media/videodev2.h.rst.exceptions
··· 188 188 replace define V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM fmtdesc-flags 189 189 replace define V4L2_FMT_FLAG_DYN_RESOLUTION fmtdesc-flags 190 190 replace define V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL fmtdesc-flags 191 + replace define V4L2_FMT_FLAG_CSC_COLORSPACE fmtdesc-flags 192 + replace define V4L2_FMT_FLAG_CSC_XFER_FUNC fmtdesc-flags 193 + replace define V4L2_FMT_FLAG_CSC_YCBCR_ENC fmtdesc-flags 194 + replace define V4L2_FMT_FLAG_CSC_HSV_ENC fmtdesc-flags 195 + replace define V4L2_FMT_FLAG_CSC_QUANTIZATION fmtdesc-flags 191 196 192 197 # V4L2 timecode types 193 198 replace define V4L2_TC_TYPE_24FPS timecode-type
+6
include/uapi/linux/videodev2.h
··· 777 777 778 778 /* Flags */ 779 779 #define V4L2_PIX_FMT_FLAG_PREMUL_ALPHA 0x00000001 780 + #define V4L2_PIX_FMT_FLAG_SET_CSC 0x00000002 780 781 781 782 /* 782 783 * F O R M A T E N U M E R A T I O N ··· 797 796 #define V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM 0x0004 798 797 #define V4L2_FMT_FLAG_DYN_RESOLUTION 0x0008 799 798 #define V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL 0x0010 799 + #define V4L2_FMT_FLAG_CSC_COLORSPACE 0x0020 800 + #define V4L2_FMT_FLAG_CSC_XFER_FUNC 0x0040 801 + #define V4L2_FMT_FLAG_CSC_YCBCR_ENC 0x0080 802 + #define V4L2_FMT_FLAG_CSC_HSV_ENC V4L2_FMT_FLAG_CSC_YCBCR_ENC 803 + #define V4L2_FMT_FLAG_CSC_QUANTIZATION 0x0100 800 804 801 805 /* Frame Size and frame rate enumeration */ 802 806 /*