v4l2-ctl: add support to the CSC API
Subsystems (2)
v5 cover v4l2-ctl: add support to the CSC API
This patchset is the userspace support for the CSC API RFC v5:
https://patchwork.kernel.org/project/linux-media/list/?series=312673
Patches Summary:
================
patch 1 - allows userspace to set the colorspace, quantization, {ycbcr/hsv}_enc and xfer_func fields in the command 'v4l2-ctl --set-fmt-video ..' it also adds the new enumeration flags to the compliance tests.
patch 2 - adds the new enumeration flags V4L2_FMT_FLAG_CSC* to the function that prints the string description of the flags. The array fmtdesc_def is replaced by a macro FMTDESC_DEF(enc_type) since the flags V4L2_FMT_FLAG_CSC_YCBCR_ENC,V4L2_FMT_FLAG_CSC_HSV_ENC, have identical value and the string should change according to whether the pixelformat is HSV or not.
patches 4, 3 - are similar to patches 1, 2 but for subdevices. So patch 3 adds support for the CSC API for subdevices
and patch 4 adds the new enumeration flags V4L2_SUBDEV_MBUS_CODE_CSC* to the framework that prints the related string description
Testing:
=======
I added a python script to run all possible combinations of CSC settings on vivid:
https://gitlab.collabora.com/dafna/v4l-utils/-/blob/rfc-v5-jul-3-abend/contrib/test/vivid_test_csc.py
NOTE that the scripts might run for long time (1-2) hours since it iterates all possible combinations of csc conversions in vivid.
The commands to run the script:
git clone --single-branch --branch rfc-v5-jul-3-abend https://gitlab.collabora.com/dafna/v4l-utils.git
# <compile v4l-utils>
cd v4l-utils/contrib/test/
modprobe vivid
python3 vivid_test_csc.py
Also added test for rkisp1:
https://gitlab.collabora.com/dafna/v4l-utils/-/blob/rfc-v5-jul-3-abend/contrib/test/rkisp1-unit-tests.sh
To run the test:
git clone --single-branch --branch rfc-v5-jul-3-abend https://gitlab.collabora.com/dafna/v4l-utils.git
# <compile v4l-utils>
cd v4l-utils/contrib/test/
./rkisp1-unit-tests.sh
changes since v4:
- adding support to CSC of the colorspace and xfer_func fields in video devices and subdevices.
Dafna Hirschfeld (4):
v4l2-ctl: vidcap: Add support for the CSC API
v4l2: common: add the flags V4L2_FMT_FLAG_CSC* to the list that maps
flags to str
v4l2-ctl: subdev: Add support for the CSC API in the subdevices
v4l2-ctl: subdev: Add the flags to the list of supported mbus formats
utils/common/v4l2-info.cpp | 47 +++++++++++++++++---- utils/common/v4l2-info.h | 6 ++- utils/v4l2-compliance/v4l2-test-formats.cpp | 4 +- utils/v4l2-ctl/v4l2-ctl-subdev.cpp | 26 +++++++++--- utils/v4l2-ctl/v4l2-ctl-vidcap.cpp | 39 +++++++++++++++-- utils/v4l2-ctl/v4l2-ctl.cpp | 16 +++++-- 6 files changed, 114 insertions(+), 24 deletions(-)
Signature · 1 line
2.17.1
PATCH v5 1/4 v4l2-ctl: vidcap: Add support for the CSC API
With the CSC API, capture drivers can allow userspace to request to set the colorspace fields of the pixelformat. This patch adds supports for this feature. when calling 'v4l2-ctl --set-fmt-video' on capture devices userspace can ask to change the fields 'colorspace', 'ycbcr/hsv_enc', 'quantization' and 'xfer_func' of the format if the driver supports it.
--- utils/v4l2-compliance/v4l2-test-formats.cpp | 4 ++- utils/v4l2-ctl/v4l2-ctl-vidcap.cpp | 39 +++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-)
utils/v4l2-compliance/v4l2-test-formats.cpp +3 -1
@@ -264,7 +264,9 @@ static int testEnumFormatsType(struct node *node, unsigned type) return fail("drivers must never set the emulated flag\n"); if (fmtdesc.flags & ~(V4L2_FMT_FLAG_COMPRESSED | V4L2_FMT_FLAG_EMULATED | V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM | - V4L2_FMT_FLAG_DYN_RESOLUTION)) + V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_CSC_COLORSPACE | + V4L2_FMT_FLAG_CSC_YCBCR_ENC | V4L2_FMT_FLAG_CSC_HSV_ENC | + V4L2_FMT_FLAG_CSC_QUANTIZATION | V4L2_FMT_FLAG_CSC_XFER_FUNC)) return fail("unknown flag %08x returned\n", fmtdesc.flags); if (!(fmtdesc.flags & V4L2_FMT_FLAG_COMPRESSED)) fail_on_test(fmtdesc.flags & (V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM |
utils/v4l2-ctl/v4l2-ctl-vidcap.cpp +36 -3
@@ -21,6 +21,7 @@ static struct v4l2_frmsizeenum frmsize; /* list frame sizes */ static struct v4l2_frmivalenum frmival; /* list frame intervals */ static unsigned set_fmts; static __u32 width, height, pixfmt, field, flags; +static __u32 ycbcr, quantization, xfer_func, colorspace; static __u32 bytesperline[VIDEO_MAX_PLANES]; static __u32 sizeimage[VIDEO_MAX_PLANES];
@@ -101,7 +102,6 @@ static void print_video_fields(int fd) void vidcap_cmd(int ch, char *optarg) { - __u32 colorspace, xfer_func, ycbcr, quantization; char *value, *subs; bool be_pixfmt;
@@ -111,8 +111,7 @@ void vidcap_cmd(int ch, char *optarg) set_fmts = parse_fmt(optarg, width, height, pixfmt, field, colorspace, xfer_func, ycbcr, quantization, flags, bytesperline, sizeimage); - if (!set_fmts || - (set_fmts & (FmtColorspace | FmtYCbCr | FmtQuantization | FmtXferFunc))) { + if (!set_fmts) { vidcap_usage(); std::exit(EXIT_FAILURE); }
@@ -220,6 +219,23 @@ int vidcap_get_and_update_fmt(cv4l_fd &_fd, struct v4l2_format &vfmt) vfmt.fmt.pix_mp.plane_fmt[i].sizeimage = sizeimage[i]; } + + if (set_fmts & FmtColorspace) { + vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix_mp.colorspace = colorspace; + } + if (set_fmts & FmtYCbCr) { + vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix_mp.ycbcr_enc = ycbcr; + } + if (set_fmts & FmtQuantization) { + vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix_mp.quantization = quantization; + } + if (set_fmts & FmtXferFunc) { + vfmt.fmt.pix_mp.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix_mp.xfer_func = xfer_func; + } } else { if (set_fmts & FmtWidth) vfmt.fmt.pix.width = width;
@@ -249,6 +265,23 @@ int vidcap_get_and_update_fmt(cv4l_fd &_fd, struct v4l2_format &vfmt) } if (set_fmts & FmtSizeImage) vfmt.fmt.pix.sizeimage = sizeimage[0]; + if (set_fmts & FmtColorspace) { + vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix.colorspace = colorspace; + } + if (set_fmts & FmtYCbCr) { + vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix.ycbcr_enc = ycbcr; + } + if (set_fmts & FmtQuantization) { + vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix.quantization = quantization; + } + if (set_fmts & FmtXferFunc) { + vfmt.fmt.pix.flags |= V4L2_PIX_FMT_FLAG_SET_CSC; + vfmt.fmt.pix.xfer_func = xfer_func; + } + } if ((set_fmts & FmtPixelFormat) &&
Signature · 1 line
2.17.1
PATCH v5 2/4 v4l2: common: add the flags V4L2_FMT_FLAG_CSC* to the list that maps flags to str
In reply to Dafna HirschfeldReplace the array fmtdesc_def with a macro 'FMTDESC_DEF' The macro is used to to create two arrays 'fmtdesc_def_ycbcr', 'fmtdesc_def_hsv' that map the format flags to strings. The function 'fmtdesc2s' is changed to get a parameter 'is_hsv' that decides which array to use.
The new CSC flags V4L2_FMT_FLAG_CSC* are added to the arries.
--- utils/common/v4l2-info.cpp | 28 +++++++++++++++++++--------- utils/common/v4l2-info.h | 2 +- utils/v4l2-ctl/v4l2-ctl.cpp | 16 ++++++++++++---- 3 files changed, 32 insertions(+), 14 deletions(-)
utils/common/v4l2-info.cpp +19 -9
@@ -366,18 +366,28 @@ std::string service2s(unsigned service) return flags2s(service, service_def); } -static const flag_def fmtdesc_def[] = { - { V4L2_FMT_FLAG_COMPRESSED, "compressed" }, - { V4L2_FMT_FLAG_EMULATED, "emulated" }, - { V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM, "continuous-bytestream" }, - { V4L2_FMT_FLAG_DYN_RESOLUTION, "dyn-resolution" }, - { V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL, "enc-cap-frame-interval" }, - { 0, NULL } +#define FMTDESC_DEF(enc_type) \ +static const flag_def fmtdesc_ ## enc_type ## _def[] = { \ + { V4L2_FMT_FLAG_COMPRESSED, "compressed" }, \ + { V4L2_FMT_FLAG_EMULATED, "emulated" }, \ + { V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM, "continuous-bytestream" }, \ + { V4L2_FMT_FLAG_DYN_RESOLUTION, "dyn-resolution" }, \ + { V4L2_FMT_FLAG_ENC_CAP_FRAME_INTERVAL, "enc-cap-frame-interval" }, \ + { V4L2_FMT_FLAG_CSC_COLORSPACE, "csc-colorspace" }, \ + { V4L2_FMT_FLAG_CSC_YCBCR_ENC, "csc-"#enc_type }, \ + { V4L2_FMT_FLAG_CSC_QUANTIZATION, "csc-quantization" }, \ + { V4L2_FMT_FLAG_CSC_XFER_FUNC, "csc-xfer-func" }, \ + { 0, NULL } \ }; -std::string fmtdesc2s(unsigned flags) +FMTDESC_DEF(ycbcr) +FMTDESC_DEF(hsv) + +std::string fmtdesc2s(unsigned flags, bool is_hsv) { - return flags2s(flags, fmtdesc_def); + if (is_hsv) + return flags2s(flags, fmtdesc_hsv_def); + return flags2s(flags, fmtdesc_ycbcr_def); } static const flag_def selection_targets_def[] = {
utils/common/v4l2-info.h +1 -1
@@ -51,7 +51,7 @@ std::string pixflags2s(unsigned flags); std::string service2s(unsigned service);/* Return v4l2_fmtdesc flags description */ -std::string fmtdesc2s(unsigned flags); +std::string fmtdesc2s(unsigned flags, bool is_hsv);/* Return selection flags description */ std::string selflags2s(__u32 flags);
utils/v4l2-ctl/v4l2-ctl.cpp +12 -4
@@ -615,8 +615,12 @@ void print_video_formats(cv4l_fd &fd, __u32 type) do { printf("\t[%d]: '%s' (%s", fmt.index, fcc2s(fmt.pixelformat).c_str(), fmt.description); - if (fmt.flags) - printf(", %s", fmtdesc2s(fmt.flags).c_str()); + if (fmt.flags) { + bool is_hsv = fmt.pixelformat == V4L2_PIX_FMT_HSV24 || + fmt.pixelformat == V4L2_PIX_FMT_HSV32; + + printf(", %s", fmtdesc2s(fmt.flags, is_hsv).c_str()); + } printf(")\n"); } while (!fd.enum_fmt(fmt)); }
@@ -634,8 +638,12 @@ void print_video_formats_ext(cv4l_fd &fd, __u32 type) do { printf("\t[%d]: '%s' (%s", fmt.index, fcc2s(fmt.pixelformat).c_str(), fmt.description); - if (fmt.flags) - printf(", %s", fmtdesc2s(fmt.flags).c_str()); + if (fmt.flags) { + bool is_hsv = fmt.pixelformat == V4L2_PIX_FMT_HSV24 || + fmt.pixelformat == V4L2_PIX_FMT_HSV32; + + printf(", %s", fmtdesc2s(fmt.flags, is_hsv).c_str()); + } printf(")\n"); if (fd.enum_framesizes(frmsize, fmt.pixelformat)) continue;
Signature · 1 line
2.17.1
PATCH v5 3/4 v4l2-ctl: subdev: Add support for the CSC API in the subdevices
In reply to Dafna HirschfeldThe CSC API allows userspace to configure the colorspace, quantization, (ycbcr/hsv)_enc and xfer_func fields when setting the formats, so those fields are not read only in userspace. This patch adds support for this API in subevices.
--- utils/v4l2-ctl/v4l2-ctl-subdev.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
utils/v4l2-ctl/v4l2-ctl-subdev.cpp +12 -4
@@ -424,14 +424,22 @@ void subdev_set(cv4l_fd &_fd) fmt.format.code = ffmt.code; if (set_fmt & FmtField) fmt.format.field = ffmt.field; - if (set_fmt & FmtColorspace) + if (set_fmt & FmtColorspace) { fmt.format.colorspace = ffmt.colorspace; - if (set_fmt & FmtXferFunc) + fmt.format.flags |= V4L2_MBUS_FRAMEFMT_SET_CSC; + } + if (set_fmt & FmtXferFunc) { fmt.format.xfer_func = ffmt.xfer_func; - if (set_fmt & FmtYCbCr) + fmt.format.flags |= V4L2_MBUS_FRAMEFMT_SET_CSC; + } + if (set_fmt & FmtYCbCr) { fmt.format.ycbcr_enc = ffmt.ycbcr_enc; - if (set_fmt & FmtQuantization) + fmt.format.flags |= V4L2_MBUS_FRAMEFMT_SET_CSC; + } + if (set_fmt & FmtQuantization) { fmt.format.quantization = ffmt.quantization; + fmt.format.flags |= V4L2_MBUS_FRAMEFMT_SET_CSC; + } if (options[OptSetSubDevFormat]) printf("Note: --set-subdev-fmt is only for testing.\n"
Signature · 1 line
2.17.1
PATCH v5 4/4 v4l2-ctl: subdev: Add the flags to the list of supported mbus formats
In reply to Dafna HirschfeldAdd a new function mbus2s to that prints a descriptive string of the supported flags of the the v4l2_subdev_mbus_code_enum Use this function in the print_mbus_code function.
Also add a macro MBUS_DEF(enc_type) to create the two arries 'mbus_hsv_def' and mbus_ycbcr_def' that maps flags to string according to the enc_type (ycbcr/hsv)
--- utils/common/v4l2-info.cpp | 19 +++++++++++++++++++ utils/common/v4l2-info.h | 4 ++++ utils/v4l2-ctl/v4l2-ctl-subdev.cpp | 10 ++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-)
utils/common/v4l2-info.cpp +19 -0
@@ -390,6 +390,25 @@ std::string fmtdesc2s(unsigned flags, bool is_hsv) return flags2s(flags, fmtdesc_ycbcr_def); } +#define MBUS_DEF(enc_type) \ +static const flag_def mbus_ ## enc_type ## _def[] = { \ + { V4L2_SUBDEV_MBUS_CODE_CSC_COLORSPACE, "csc-colorspace" }, \ + { V4L2_SUBDEV_MBUS_CODE_CSC_YCBCR_ENC, "csc-"#enc_type }, \ + { V4L2_SUBDEV_MBUS_CODE_CSC_QUANTIZATION, "csc-quantization" }, \ + { V4L2_SUBDEV_MBUS_CODE_CSC_XFER_FUNC, "csc-xfer-func" }, \ + { 0, NULL } \ +}; + +MBUS_DEF(ycbcr) +MBUS_DEF(hsv) + +std::string mbus2s(unsigned flags, bool is_hsv) +{ + if (is_hsv) + return flags2s(flags, mbus_hsv_def); + return flags2s(flags, mbus_ycbcr_def); +} + static const flag_def selection_targets_def[] = { { V4L2_SEL_TGT_CROP_ACTIVE, "crop" }, { V4L2_SEL_TGT_CROP_DEFAULT, "crop_default" },
utils/common/v4l2-info.h +4 -0
@@ -8,6 +8,7 @@#include <string> #include <linux/videodev2.h> +#include <linux/v4l2-subdev.h> /* Print capability information */ void v4l2_info_capability(const v4l2_capability &cap);
@@ -50,6 +51,9 @@ std::string pixflags2s(unsigned flags);/* Return sliced vbi services description */ std::string service2s(unsigned service); +/* Return v4l2_subdev_mbus_code_enum flags description */ +std::string mbus2s(unsigned flags, bool is_hsv); +/* Return v4l2_fmtdesc flags description */ std::string fmtdesc2s(unsigned flags, bool is_hsv);
utils/v4l2-ctl/v4l2-ctl-subdev.cpp +8 -2
@@ -577,10 +577,10 @@ static void print_mbus_code(__u32 code) if (mbus_names[i].code == code) break; if (mbus_names[i].name) - printf("\t0x%04x: MEDIA_BUS_FMT_%s\n", + printf("\t0x%04x: MEDIA_BUS_FMT_%s", mbus_names[i].code, mbus_names[i].name); else - printf("\t0x%04x\n", code); + printf("\t0x%04x", code); } static void print_mbus_codes(int fd, __u32 pad)
@@ -597,6 +597,12 @@ static void print_mbus_codes(int fd, __u32 pad) if (ret) break; print_mbus_code(mbus_code.code); + if (mbus_code.flags) { + bool is_hsv = mbus_code.code == MEDIA_BUS_FMT_AHSV8888_1X32; + + printf(", %s", mbus2s(mbus_code.flags, is_hsv).c_str()); + } + printf("\n"); mbus_code.index++; } }
Signature · 1 line
2.17.1