Skip to contentSkip to conversation

media: imx: Add Rec.709 and limited range encoding support

PATCH In review Steve Longerbeam 5 participants 14 messages May 22, 2019 – Oct 23, 2019

Subsystems (8)
lore
Share & tools
lore

Steve Longerbeam

author maintainer
#0 lore raw

v8 cover media: imx: Add Rec.709 and limited range encoding support

This patchset adds support for Rec.709 encoding and inverse encoding, and encoding between both full-range and limited-range quantization for both YUV and RGB, to ipu-ic. The imx-media driver can now support both BT.601 and Rec.709 encoding, and limited and full-range quantization.

History:
v8:
- removed the Fixes: and cc: stable in patch "Fully describe colorspace conversions". This patch is too difficult to backport to stable trees. No functional changes in this version.

v7:
- removed the run-time transformation of coefficients from full-range to limited-range, and instead hard-code all the coefficient tables.
- moved the coefficient tables into a new module ipu-ic-csc.c.
- introduced ipu_ic_calc_csc() which pre-determines coefficients, which are then passed to ipu_ic_task_init(_rsc)() as a filled 'struct ipu_ic_csc'.
- added support for limited-range RGB.

v6:
- tweak some of the coefficients slightly, they were not getting rounded correctly.
- move the introduction of calc_csc_coeffs() to an earlier patch for easier patch readability.

v5:
- the hard-coded encode coefficients now convert only between full-range quantization. A new function is added to transform the coefficients to limited-range at input or output.
- add a bug fix patch for saturation bit in TPMEM register.
- add a patch to fully describe input and output colorspace to the IC task init functions.
- add imx_media_try_colorimetry(), called at all sink/source pad try_fmt.

v4:
- fix a compile error in init_csc(), reported by Tim Harvey.

v3:
- fix some inconsistent From: and Signed-off-by:'s. No functional changes.

v2:
- rename ic_csc_rgb2rgb matrix to ic_csc_identity.
- only return "Unsupported YCbCr encoding" error if inf != outf, since if inf == outf, the identity matrix can be used. Reported by Tim Harvey.
- move ic_route check above default colorimetry checks, and fill default colorspace for ic_route, otherwise it's not possible to set BT.709 encoding for ic routes.

Steve Longerbeam (5):
gpu: ipu-v3: ipu-ic: Fix saturation bit offset in TPMEM
gpu: ipu-v3: ipu-ic: Fully describe colorspace conversions
gpu: ipu-v3: ipu-ic-csc: Add support for limited range encoding
gpu: ipu-v3: ipu-ic-csc: Add support for Rec.709 encoding
media: imx: Try colorimetry at both sink and source pads

 drivers/gpu/ipu-v3/Makefile                 |   4 +-
 drivers/gpu/ipu-v3/ipu-ic-csc.c             | 410 ++++++++++++++++++++
 drivers/gpu/ipu-v3/ipu-ic.c                 | 138 +++----
 drivers/gpu/ipu-v3/ipu-image-convert.c      |  28 +-
 drivers/staging/media/imx/imx-ic-prp.c      |   6 +-
 drivers/staging/media/imx/imx-ic-prpencvf.c |  42 +-
 drivers/staging/media/imx/imx-media-csi.c   |  19 +-
 drivers/staging/media/imx/imx-media-utils.c |  73 ++--
 drivers/staging/media/imx/imx-media-vdic.c  |   5 +-
 drivers/staging/media/imx/imx-media.h       |   5 +-
 drivers/staging/media/imx/imx7-media-csi.c  |   8 +-
 include/video/imx-ipu-v3.h                  |  56 ++-
 12 files changed, 614 insertions(+), 180 deletions(-)
 create mode 100644 drivers/gpu/ipu-v3/ipu-ic-csc.c
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v8 1/5 gpu: ipu-v3: ipu-ic: Fix saturation bit offset in TPMEM

The saturation bit was being set at bit 9 in the second 32-bit word of the TPMEM CSC. This isn't correct, the saturation bit is bit 42, which is bit 10 of the second word.

Fixes 1aa8ea0d2bd5d ("gpu: ipu-v3: Add Image Converter unit")
---
 drivers/gpu/ipu-v3/ipu-ic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/ipu-v3/ipu-ic.c +1 -1
index 594c3cbc8291..18816ccf600e 100644
@@ -257,7 +257,7 @@ static int init_csc(struct ipu_ic *ic,
 	writel(param, base++);
 
 	param = ((a[0] & 0x1fe0) >> 5) | (params->scale << 8) |
-		(params->sat << 9);
+		(params->sat << 10);
 	writel(param, base++);
 
 	param = ((a[1] & 0x1f) << 27) | ((c[0][1] & 0x1ff) << 18) |
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v8 2/5 gpu: ipu-v3: ipu-ic: Fully describe colorspace conversions

In reply to Steve Longerbeam

Only providing the input and output RGB/YUV space to the IC task init functions is not sufficient. To fully characterize a colorspace conversion, the Y'CbCr encoding standard, and quantization also need to be specified.

Define a 'struct ipu_ic_colorspace' that includes all the above.

This allows to actually enforce the fact that the IC:

- can only encode to/from YUV and RGB full range. A follow-up patch will remove this restriction.
- can only encode using BT.601 standard. A follow-up patch will add Rec.709 encoding support.

The determination of the CSC coefficients based on the input/output 'struct ipu_ic_colorspace' are moved to a new exported function ipu_ic_calc_csc(), and 'struct ic_csc_params' is exported as 'struct ipu_ic_csc_params'. ipu_ic_calc_csc() fills a 'struct ipu_ic_csc' with the input/output 'struct ipu_ic_colorspace' and the calculated 'struct ic_csc_params' from those input/output colorspaces.

The functions ipu_ic_task_init(_rsc)() now take a filled 'struct ipu_ic_csc'.

The existing CSC coefficient tables and ipu_ic_calc_csc() are moved to a new module ipu-ic-csc.c. This is in preparation for adding more coefficient tables for limited range quantization and more encoding standards.

The existing ycbcr2rgb and inverse rgb2ycbcr tables defined the BT.601 Y'CbCr encoding coefficients. The rgb2ycbcr table specifically described the BT.601 encoding from full range RGB to full range YUV. Table comments have been added in ipu-ic-csc.c to make this more clear.

The ycbcr2rgb inverse table described encoding YUV limited range to RGB full range. To be consistent with the rgb2ycbcr table, this table is converted to YUV full range to RGB full range, and the comments are expanded in ipu-ic-csc.c.

The ic_csc_rgb2rgb table was just an identity matrix, so it is renamed 'identity' in ipu-ic-csc.c.

---
Changes in v8:
- remove Fixes: and cc: stable. This patch is too difficult to backport to stable trees.
Changes in v7:
- squashed with "gpu: ipu-v3: ipu-ic: Fix BT.601 coefficients".
- moved the coefficient tables and calc_csc_coeffs() to a new module ipu-ic-csc.c, and added exported ipu_ic_calc_csc() to it.
- drop v4l2_colorspace (chromaticities) from 'struct ipu_ic_colorspace'. It's implied that xfer_func (gamma function) must be the same for input and output, so make that implicit for chromaticities too.
- drop passing priv to calc_csc_coeffs(), was only used to print error messages.
- removed the inverse_encode boolean in calc_csc_coeffs().
- express negative coefficients as true signed int's, for better readability.
- tweak inverse coeff in comments, no change to rounded table values.

---
 drivers/gpu/ipu-v3/Makefile                 |   4 +-
 drivers/gpu/ipu-v3/ipu-ic-csc.c             | 129 ++++++++++++++++++
 drivers/gpu/ipu-v3/ipu-ic.c                 | 138 +++++++-------------
 drivers/gpu/ipu-v3/ipu-image-convert.c      |  28 ++--
 drivers/staging/media/imx/imx-ic-prpencvf.c |  34 ++++-
 include/video/imx-ipu-v3.h                  |  56 +++++++-
 6 files changed, 271 insertions(+), 118 deletions(-)
 create mode 100644 drivers/gpu/ipu-v3/ipu-ic-csc.c
drivers/gpu/ipu-v3/Makefile +2 -2
index 7cc8b47e488b..5fe5ef20701a 100644
@@ -2,8 +2,8 @@
 obj-$(CONFIG_IMX_IPUV3_CORE) += imx-ipu-v3.o
 
 imx-ipu-v3-objs := ipu-common.o ipu-cpmem.o ipu-csi.o ipu-dc.o ipu-di.o \
-		ipu-dp.o ipu-dmfc.o ipu-ic.o ipu-image-convert.o \
-		ipu-smfc.o ipu-vdi.o
+		ipu-dp.o ipu-dmfc.o ipu-ic.o ipu-ic-csc.o \
+		ipu-image-convert.o ipu-smfc.o ipu-vdi.o
 
 ifdef CONFIG_DRM
 	imx-ipu-v3-objs += ipu-pre.o ipu-prg.o
drivers/gpu/ipu-v3/ipu-ic-csc.c new file +129 -0

129 lines not on this page · raw message

drivers/gpu/ipu-v3/ipu-ic.c +45 -93

224 lines not on this page · raw message

drivers/gpu/ipu-v3/ipu-image-convert.c +17 -11
index 13103ab86050..3520eac0eacc 100644
@@ -155,6 +155,7 @@ struct ipu_image_convert_ctx {
 	/* Source/destination image data and rotation mode */
 	struct ipu_image_convert_image in;
 	struct ipu_image_convert_image out;
+	struct ipu_ic_csc csc;
 	enum ipu_rotate_mode rot_mode;
 	u32 downsize_coeff_h;
 	u32 downsize_coeff_v;
@@ -1317,7 +1318,6 @@ static int convert_start(struct ipu_image_convert_run *run, unsigned int tile)
 	struct ipu_image_convert_priv *priv = chan->priv;
 	struct ipu_image_convert_image *s_image = &ctx->in;
 	struct ipu_image_convert_image *d_image = &ctx->out;
-	enum ipu_color_space src_cs, dest_cs;
 	unsigned int dst_tile = ctx->out_tile_map[tile];
 	unsigned int dest_width, dest_height;
 	unsigned int col, row;
@@ -1327,9 +1327,6 @@ static int convert_start(struct ipu_image_convert_run *run, unsigned int tile)
 	dev_dbg(priv->ipu->dev, "%s: task %u: starting ctx %p run %p tile %u -> %u\n",
 		__func__, chan->ic_task, ctx, run, tile, dst_tile);
 
-	src_cs = ipu_pixelformat_to_colorspace(s_image->fmt->fourcc);
-	dest_cs = ipu_pixelformat_to_colorspace(d_image->fmt->fourcc);
-
 	if (ipu_rot_mode_is_irt(ctx->rot_mode)) {
 		/* swap width/height for resizer */
 		dest_width = d_image->tile[dst_tile].height;
@@ -1352,13 +1349,12 @@ static int convert_start(struct ipu_image_convert_run *run, unsigned int tile)
 		s_image->tile[tile].height, dest_width, dest_height, rsc);
 
 	/* setup the IC resizer and CSC */
-	ret = ipu_ic_task_init_rsc(chan->ic,
-			       s_image->tile[tile].width,
-			       s_image->tile[tile].height,
-			       dest_width,
-			       dest_height,
-			       src_cs, dest_cs,
-			       rsc);
+	ret = ipu_ic_task_init_rsc(chan->ic, &ctx->csc,
+				   s_image->tile[tile].width,
+				   s_image->tile[tile].height,
+				   dest_width,
+				   dest_height,
+				   rsc);
 	if (ret) {
 		dev_err(priv->ipu->dev, "ipu_ic_task_init failed, %d\n", ret);
 		return ret;
@@ -2058,6 +2054,16 @@ ipu_image_convert_prepare(struct ipu_soc *ipu, enum ipu_ic_task ic_task,
 
 	calc_tile_resize_coefficients(ctx);
 
+	ret = ipu_ic_calc_csc(&ctx->csc,
+			s_image->base.pix.ycbcr_enc,
+			s_image->base.pix.quantization,
+			ipu_pixelformat_to_colorspace(s_image->fmt->fourcc),
+			d_image->base.pix.ycbcr_enc,
+			d_image->base.pix.quantization,
+			ipu_pixelformat_to_colorspace(d_image->fmt->fourcc));
+	if (ret)
+		goto out_free;
+
 	dump_format(ctx, s_image);
 	dump_format(ctx, d_image);
 
drivers/staging/media/imx/imx-ic-prpencvf.c +28 -6
index 64037b0a8387..e8b36a181ccc 100644
@@ -456,6 +456,7 @@ static int prp_setup_rotation(struct prp_priv *priv)
 	const struct imx_media_pixfmt *outcc, *incc;
 	struct v4l2_mbus_framefmt *infmt;
 	struct v4l2_pix_format *outfmt;
+	struct ipu_ic_csc csc;
 	dma_addr_t phys[2];
 	int ret;
 
@@ -464,6 +465,17 @@ static int prp_setup_rotation(struct prp_priv *priv)
 	incc = priv->cc[PRPENCVF_SINK_PAD];
 	outcc = vdev->cc;
 
+	ret = ipu_ic_calc_csc(&csc,
+			      infmt->ycbcr_enc, infmt->quantization,
+			      incc->cs,
+			      outfmt->ycbcr_enc, outfmt->quantization,
+			      outcc->cs);
+	if (ret) {
+		v4l2_err(&ic_priv->sd, "ipu_ic_calc_csc failed, %d\n",
+			 ret);
+		return ret;
+	}
+
 	ret = imx_media_alloc_dma_buf(priv->md, &priv->rot_buf[0],
 				      outfmt->sizeimage);
 	if (ret) {
@@ -477,10 +489,9 @@ static int prp_setup_rotation(struct prp_priv *priv)
 		goto free_rot0;
 	}
 
-	ret = ipu_ic_task_init(priv->ic,
+	ret = ipu_ic_task_init(priv->ic, &csc,
 			       infmt->width, infmt->height,
-			       outfmt->height, outfmt->width,
-			       incc->cs, outcc->cs);
+			       outfmt->height, outfmt->width);
 	if (ret) {
 		v4l2_err(&ic_priv->sd, "ipu_ic_task_init failed, %d\n", ret);
 		goto free_rot1;
@@ -572,6 +583,7 @@ static int prp_setup_norotation(struct prp_priv *priv)
 	const struct imx_media_pixfmt *outcc, *incc;
 	struct v4l2_mbus_framefmt *infmt;
 	struct v4l2_pix_format *outfmt;
+	struct ipu_ic_csc csc;
 	dma_addr_t phys[2];
 	int ret;
 
@@ -580,10 +592,20 @@ static int prp_setup_norotation(struct prp_priv *priv)
 	incc = priv->cc[PRPENCVF_SINK_PAD];
 	outcc = vdev->cc;
 
-	ret = ipu_ic_task_init(priv->ic,
+	ret = ipu_ic_calc_csc(&csc,
+			      infmt->ycbcr_enc, infmt->quantization,
+			      incc->cs,
+			      outfmt->ycbcr_enc, outfmt->quantization,
+			      outcc->cs);
+	if (ret) {
+		v4l2_err(&ic_priv->sd, "ipu_ic_calc_csc failed, %d\n",
+			 ret);
+		return ret;
+	}
+
+	ret = ipu_ic_task_init(priv->ic, &csc,
 			       infmt->width, infmt->height,
-			       outfmt->width, outfmt->height,
-			       incc->cs, outcc->cs);
+			       outfmt->width, outfmt->height);
 	if (ret) {
 		v4l2_err(&ic_priv->sd, "ipu_ic_task_init failed, %d\n", ret);
 		return ret;
include/video/imx-ipu-v3.h +50 -6

70 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH v8 3/5 gpu: ipu-v3: ipu-ic-csc: Add support for limited range encoding

In reply to Steve Longerbeam

Add support for encodings to or from limited range quantization.

---
Changes in v7:
- hard-code the coefficients instead of deriving the limited range coefficients from the full2full coefficients on the fly with fixed-point math.
- add support for RGB limited-range.

---
 drivers/gpu/ipu-v3/ipu-ic-csc.c | 180 +++++++++++++++++++++++++++++---
 1 file changed, 166 insertions(+), 14 deletions(-)
drivers/gpu/ipu-v3/ipu-ic-csc.c +166 -14
index 5fb469cd64fe..8e9150b1d668 100644
@@ -10,6 +10,10 @@
 #include <linux/sizes.h>
 #include "ipu-prv.h"
 
+#define QUANT_MAP(q)					\
+	((q) == V4L2_QUANTIZATION_FULL_RANGE ||		\
+	 (q) == V4L2_QUANTIZATION_DEFAULT ? 0 : 1)
+
 /* identity matrix */
 static const struct ipu_ic_csc_params identity = {
 	.coeff = {
@@ -21,12 +25,87 @@ static const struct ipu_ic_csc_params identity = {
 	.scale = 2,
 };
 
+/*
+ * RGB full-range to RGB limited-range
+ *
+ * R_lim = 0.8588 * R_full + 16
+ * G_lim = 0.8588 * G_full + 16
+ * B_lim = 0.8588 * B_full + 16
+ */
+static const struct ipu_ic_csc_params rgbf2rgbl = {
+	.coeff = {
+		{  220,    0,    0, },
+		{    0,  220,    0, },
+		{    0,    0,  220, },
+	},
+	.offset = { 64, 64, 64, },
+	.scale = 1,
+};
+
+/*
+ * RGB limited-range to RGB full-range
+ *
+ * R_full = 1.1644 * (R_lim - 16)
+ * G_full = 1.1644 * (G_lim - 16)
+ * B_full = 1.1644 * (B_lim - 16)
+ */
+static const struct ipu_ic_csc_params rgbl2rgbf = {
+	.coeff = {
+		{  149,    0,    0, },
+		{    0,  149,    0, },
+		{    0,    0,  149, },
+	},
+	.offset = { -37, -37, -37, },
+	.scale = 2,
+};
+
+/*
+ * YUV full-range to YUV limited-range
+ *
+ * Y_lim  = 0.8588 * Y_full + 16
+ * Cb_lim = 0.8784 * (Cb_full - 128) + 128
+ * Cr_lim = 0.8784 * (Cr_full - 128) + 128
+ */
+static const struct ipu_ic_csc_params yuvf2yuvl = {
+	.coeff = {
+		{  220,    0,    0, },
+		{    0,  225,    0, },
+		{    0,    0,  225, },
+	},
+	.offset = { 64, 62, 62, },
+	.scale = 1,
+	.sat = true,
+};
+
+/*
+ * YUV limited-range to YUV full-range
+ *
+ * Y_full  = 1.1644 * (Y_lim - 16)
+ * Cb_full = 1.1384 * (Cb_lim - 128) + 128
+ * Cr_full = 1.1384 * (Cr_lim - 128) + 128
+ */
+static const struct ipu_ic_csc_params yuvl2yuvf = {
+	.coeff = {
+		{  149,    0,    0, },
+		{    0,  146,    0, },
+		{    0,    0,  146, },
+	},
+	.offset = { -37, -35, -35, },
+	.scale = 2,
+};
+
 static const struct ipu_ic_csc_params *rgb2rgb[] = {
 	&identity,
+	&rgbf2rgbl,
+	&rgbl2rgbf,
+	&identity,
 };
 
 static const struct ipu_ic_csc_params *yuv2yuv[] = {
 	&identity,
+	&yuvf2yuvl,
+	&yuvl2yuvf,
+	&identity,
 };
 
 /*
@@ -46,6 +125,41 @@ static const struct ipu_ic_csc_params rgbf2yuvf_601 = {
 	.scale = 1,
 };
 
+/* BT.601 RGB full-range to YUV limited-range */
+static const struct ipu_ic_csc_params rgbf2yuvl_601 = {
+	.coeff = {
+		{   66,  129,   25, },
+		{  -38,  -74,  112, },
+		{  112,  -94,  -18, },
+	},
+	.offset = { 64, 512, 512, },
+	.scale = 1,
+	.sat = true,
+};
+
+/* BT.601 RGB limited-range to YUV full-range */
+static const struct ipu_ic_csc_params rgbl2yuvf_601 = {
+	.coeff = {
+		{   89,  175,   34, },
+		{  -50,  -99,  149, },
+		{  149, -125,  -24, },
+	},
+	.offset = { -75, 512, 512, },
+	.scale = 1,
+};
+
+/* BT.601 RGB limited-range to YUV limited-range */
+static const struct ipu_ic_csc_params rgbl2yuvl_601 = {
+	.coeff = {
+		{   77,  150,   29, },
+		{  -44,  -87,  131, },
+		{  131, -110,  -21, },
+	},
+	.offset = { 0, 512, 512, },
+	.scale = 1,
+	.sat = true,
+};
+
 /*
  * BT.601 YUV full-range to RGB full-range
  *
@@ -69,39 +183,77 @@ static const struct ipu_ic_csc_params yuvf2rgbf_601 = {
 	.scale = 2,
 };
 
+/* BT.601 YUV full-range to RGB limited-range */
+static const struct ipu_ic_csc_params yuvf2rgbl_601 = {
+	.coeff = {
+		{  110,    0,  154, },
+		{  110,  -38,  -78, },
+		{  110,  195,    0, },
+	},
+	.offset = { -276, 265, -358, },
+	.scale = 2,
+};
+
+/* BT.601 YUV limited-range to RGB full-range */
+static const struct ipu_ic_csc_params yuvl2rgbf_601 = {
+	.coeff = {
+		{   75,    0,  102, },
+		{   75,  -25,  -52, },
+		{   75,  129,    0, },
+	},
+	.offset = { -223, 136, -277, },
+	.scale = 3,
+};
+
+/* BT.601 YUV limited-range to RGB limited-range */
+static const struct ipu_ic_csc_params yuvl2rgbl_601 = {
+	.coeff = {
+		{  128,    0,  175, },
+		{  128,  -43,  -89, },
+		{  128,  222,    0, },
+	},
+	.offset = { -351, 265, -443, },
+	.scale = 2,
+};
+
 static const struct ipu_ic_csc_params *rgb2yuv_601[] = {
 	&rgbf2yuvf_601,
+	&rgbf2yuvl_601,
+	&rgbl2yuvf_601,
+	&rgbl2yuvl_601,
 };
 
 static const struct ipu_ic_csc_params *yuv2rgb_601[] = {
 	&yuvf2rgbf_601,
+	&yuvf2rgbl_601,
+	&yuvl2rgbf_601,
+	&yuvl2rgbl_601,
 };
 
 static int calc_csc_coeffs(struct ipu_ic_csc *csc)
 {
+	const struct ipu_ic_csc_params **params_tbl;
+	int tbl_idx;
+
 	if (csc->out_cs.enc != V4L2_YCBCR_ENC_601)
 		return -ENOTSUPP;
 
-	if ((csc->in_cs.cs == IPUV3_COLORSPACE_YUV &&
-	     csc->in_cs.quant != V4L2_QUANTIZATION_FULL_RANGE) ||
-	    (csc->out_cs.cs == IPUV3_COLORSPACE_YUV &&
-	     csc->out_cs.quant != V4L2_QUANTIZATION_FULL_RANGE))
-		return -ENOTSUPP;
-
-	if ((csc->in_cs.cs == IPUV3_COLORSPACE_RGB &&
-	     csc->in_cs.quant != V4L2_QUANTIZATION_FULL_RANGE) ||
-	    (csc->out_cs.cs == IPUV3_COLORSPACE_RGB &&
-	     csc->out_cs.quant != V4L2_QUANTIZATION_FULL_RANGE))
-		return -ENOTSUPP;
+	tbl_idx = (QUANT_MAP(csc->in_cs.quant) << 1) |
+		QUANT_MAP(csc->out_cs.quant);
 
 	if (csc->in_cs.cs == csc->out_cs.cs) {
 		csc->params = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
-			*yuv2yuv[0] : *rgb2rgb[0];
+			*yuv2yuv[tbl_idx] : *rgb2rgb[tbl_idx];
+
 		return 0;
 	}
 
-	csc->params = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
-		*yuv2rgb_601[0] : *rgb2yuv_601[0];
+	/* YUV <-> RGB encoding is required */
+
+	params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
+		yuv2rgb_601 : rgb2yuv_601;
+
+	csc->params = *params_tbl[tbl_idx];
 
 	return 0;
 }
Signature · 6 lines
2.17.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#4 lore raw

PATCH v8 4/5 gpu: ipu-v3: ipu-ic-csc: Add support for Rec.709 encoding

In reply to Steve Longerbeam

Add support for Rec.709 encoding and inverse encoding.

---
Changes in v7:
- moved CSC tables to new module ipu-ic-csc.c.
- express negative coefficients as true signed int's, for better readability.
Changes in v5:
- moved API changes to a previous patch.
- moved CSC coeff calc to new function calc_csc_coeffs(). Changes in v4:
- fix compile error.
Chnges in v3:
- none.
Changes in v2:
- only return "Unsupported YCbCr encoding" error if inf != outf, since if inf == outf, the identity matrix can be used. Reported by Tim Harvey.

---
 drivers/gpu/ipu-v3/ipu-ic-csc.c | 139 ++++++++++++++++++++++++++++++--
 1 file changed, 134 insertions(+), 5 deletions(-)
drivers/gpu/ipu-v3/ipu-ic-csc.c +134 -5
index 8e9150b1d668..09e94aa12c40 100644
@@ -230,14 +230,133 @@ static const struct ipu_ic_csc_params *yuv2rgb_601[] = {
 	&yuvl2rgbl_601,
 };
 
+/*
+ * REC.709 encoding from RGB full range to YUV full range:
+ *
+ * Y =  .2126 * R + .7152 * G + .0722 * B
+ * U = -.1146 * R - .3854 * G + .5000 * B + 128
+ * V =  .5000 * R - .4542 * G - .0458 * B + 128
+ */
+static const struct ipu_ic_csc_params rgbf2yuvf_709 = {
+	.coeff = {
+		{  54,  183,  19 },
+		{ -29,  -99, 128 },
+		{ 128, -116, -12 },
+	},
+	.offset = { 0, 512, 512 },
+	.scale = 1,
+};
+
+/* Rec.709 RGB full-range to YUV limited-range */
+static const struct ipu_ic_csc_params rgbf2yuvl_709 = {
+	.coeff = {
+		{   47,  157,   16, },
+		{  -26,  -87,  112, },
+		{  112, -102,  -10, },
+	},
+	.offset = { 64, 512, 512, },
+	.scale = 1,
+	.sat = true,
+};
+
+/* Rec.709 RGB limited-range to YUV full-range */
+static const struct ipu_ic_csc_params rgbl2yuvf_709 = {
+	.coeff = {
+		{   63,  213,   22, },
+		{  -34, -115,  149, },
+		{  149, -135,  -14, },
+	},
+	.offset = { -75, 512, 512, },
+	.scale = 1,
+};
+
+/* Rec.709 RGB limited-range to YUV limited-range */
+static const struct ipu_ic_csc_params rgbl2yuvl_709 = {
+	.coeff = {
+		{   54,  183,   18, },
+		{  -30, -101,  131, },
+		{  131, -119,  -12, },
+	},
+	.offset = { 0, 512, 512, },
+	.scale = 1,
+	.sat = true,
+};
+
+/*
+ * Inverse REC.709 encoding from YUV full range to RGB full range:
+ *
+ * R = 1. * Y +      0 * (Cb - 128) + 1.5748 * (Cr - 128)
+ * G = 1. * Y -  .1873 * (Cb - 128) -  .4681 * (Cr - 128)
+ * B = 1. * Y + 1.8556 * (Cb - 128) +      0 * (Cr - 128)
+ *
+ * equivalently (factoring out the offsets):
+ *
+ * R = 1. * Y  +      0 * Cb + 1.5748 * Cr - 201.574
+ * G = 1. * Y  -  .1873 * Cb -  .4681 * Cr +  83.891
+ * B = 1. * Y  + 1.8556 * Cb +      0 * Cr - 237.517
+ */
+static const struct ipu_ic_csc_params yuvf2rgbf_709 = {
+	.coeff = {
+		{  128,   0, 202 },
+		{  128, -24, -60 },
+		{  128, 238,   0 },
+	},
+	.offset = { -403, 168, -475 },
+	.scale = 2,
+};
+
+/* Rec.709 YUV full-range to RGB limited-range */
+static const struct ipu_ic_csc_params yuvf2rgbl_709 = {
+	.coeff = {
+		{  110,    0,  173, },
+		{  110,  -21,  -51, },
+		{  110,  204,    0, },
+	},
+	.offset = { -314, 176, -376, },
+	.scale = 2,
+};
+
+/* Rec.709 YUV limited-range to RGB full-range */
+static const struct ipu_ic_csc_params yuvl2rgbf_709 = {
+	.coeff = {
+		{   75,    0,  115, },
+		{   75,  -14,  -34, },
+		{   75,  135,    0, },
+	},
+	.offset = { -248, 77, -289, },
+	.scale = 3,
+};
+
+/* Rec.709 YUV limited-range to RGB limited-range */
+static const struct ipu_ic_csc_params yuvl2rgbl_709 = {
+	.coeff = {
+		{  128,    0,  197, },
+		{  128,  -23,  -59, },
+		{  128,  232,    0, },
+	},
+	.offset = { -394, 164, -464, },
+	.scale = 2,
+};
+
+static const struct ipu_ic_csc_params *rgb2yuv_709[] = {
+	&rgbf2yuvf_709,
+	&rgbf2yuvl_709,
+	&rgbl2yuvf_709,
+	&rgbl2yuvl_709,
+};
+
+static const struct ipu_ic_csc_params *yuv2rgb_709[] = {
+	&yuvf2rgbf_709,
+	&yuvf2rgbl_709,
+	&yuvl2rgbf_709,
+	&yuvl2rgbl_709,
+};
+
 static int calc_csc_coeffs(struct ipu_ic_csc *csc)
 {
 	const struct ipu_ic_csc_params **params_tbl;
 	int tbl_idx;
 
-	if (csc->out_cs.enc != V4L2_YCBCR_ENC_601)
-		return -ENOTSUPP;
-
 	tbl_idx = (QUANT_MAP(csc->in_cs.quant) << 1) |
 		QUANT_MAP(csc->out_cs.quant);
 
@@ -250,8 +369,18 @@ static int calc_csc_coeffs(struct ipu_ic_csc *csc)
 
 	/* YUV <-> RGB encoding is required */
 
-	params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
-		yuv2rgb_601 : rgb2yuv_601;
+	switch (csc->out_cs.enc) {
+	case V4L2_YCBCR_ENC_601:
+		params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
+			yuv2rgb_601 : rgb2yuv_601;
+		break;
+	case V4L2_YCBCR_ENC_709:
+		params_tbl = (csc->in_cs.cs == IPUV3_COLORSPACE_YUV) ?
+			yuv2rgb_709 : rgb2yuv_709;
+		break;
+	default:
+		return -ENOTSUPP;
+	}
 
 	csc->params = *params_tbl[tbl_idx];
 
Signature · 1 line
2.17.1
author maintainer
#5 lore raw

PATCH v8 5/5 media: imx: Try colorimetry at both sink and source pads

In reply to Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
 drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
 drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
 drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
 drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
 drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
 drivers/staging/media/imx/imx-media.h       |  5 +-
 drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
 7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 			sdformat->format.code = cc->codes[0];
 		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		break;
 	case PRP_SRC_PAD_PRPENC:
 	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 		break;
 	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
 	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
 	*fmt = sdformat->format;
 out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 	} else {
 		v4l_bound_align_image(&sdformat->format.width,
 				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
 				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
 				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
 	struct v4l2_mbus_framefmt *infmt =
 		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		return;
+	}
 
 	switch (infmt->field) {
 	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
 		break;
 	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
 		/* Reset crop and compose rectangles */
 		crop->left = 0;
 		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
 }
 
 static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +36 -37

101 lines not on this page · raw message

drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
 				      &sdformat->format.height,
 				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
 		/* input must be interlaced! Choose SEQ_TB if not */
 		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
 			sdformat->format.field = V4L2_FIELD_SEQ_TB;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
 			    const struct imx_media_pixfmt **cc);
 int imx_media_init_cfg(struct v4l2_subdev *sd,
 		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
 				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -5
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
 		sdformat->format.colorspace = in_fmt->colorspace;
 		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
 		break;
 	case IMX7_CSI_PAD_SINK:
 		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 							 false);
 			sdformat->format.code = (*cc)->codes[0];
 		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
 		break;
 	default:
 		return -EINVAL;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
Acked-by on v8 5/5
#6 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

Quoted 10 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

It's probably easiest to apply this whole series to the gpu subsystem. You have my Acked-by for this media patch.

Regards,

Hans

Quoted 293 lines

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
 drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
 drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
 drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
 drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
 drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
 drivers/staging/media/imx/imx-media.h       |  5 +-
 drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
 7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 			sdformat->format.code = cc->codes[0];
 		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		break;
 	case PRP_SRC_PAD_PRPENC:
 	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 		break;
 	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
 	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
 	*fmt = sdformat->format;
 out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 	} else {
 		v4l_bound_align_image(&sdformat->format.width,
 				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
 				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
 				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
 	struct v4l2_mbus_framefmt *infmt =
 		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		return;
+	}
 
 	switch (infmt->field) {
 	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
 		break;
 	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
 		/* Reset crop and compose rectangles */
 		crop->left = 0;
 		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
 }
 
 static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +36 -37
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
 EXPORT_SYMBOL_GPL(imx_media_init_cfg);
 
 /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
  *
  * tryfmt->code must be set on entry.
  *
  * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * Y`CbCr encoding must be fixed. The IC supports only BT.601 Y`CbCr
+ * or Rec.709 Y`CbCr encoding.
  */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route)
 {
 	const struct imx_media_pixfmt *cc;
 	bool is_rgb = false;
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
 	cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
 	if (!cc)
 		cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-	if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+	if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
 		is_rgb = true;
 
-	/* fill field if necessary */
-	if (tryfmt->field == V4L2_FIELD_ANY)
-		tryfmt->field = fmt->field;
+	switch (tryfmt->colorspace) {
+	case V4L2_COLORSPACE_SMPTE170M:
+	case V4L2_COLORSPACE_REC709:
+	case V4L2_COLORSPACE_JPEG:
+	case V4L2_COLORSPACE_SRGB:
+	case V4L2_COLORSPACE_BT2020:
+	case V4L2_COLORSPACE_OPRGB:
+	case V4L2_COLORSPACE_DCI_P3:
+	case V4L2_COLORSPACE_RAW:
+		break;
+	default:
+		tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+		break;
+	}
+
+	if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+		tryfmt->xfer_func =
+			V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
 
-	/* fill colorimetry if necessary */
-	if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) {
-		tryfmt->colorspace = fmt->colorspace;
-		tryfmt->xfer_func = fmt->xfer_func;
-		tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-		tryfmt->quantization = fmt->quantization;
+	if (ic_route) {
+		if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 &&
+		    tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+			tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
 	} else {
-		if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) {
-			tryfmt->xfer_func =
-				V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-		}
 		if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
 			tryfmt->ycbcr_enc =
 				V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
 		}
-		if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) {
-			tryfmt->quantization =
-				V4L2_MAP_QUANTIZATION_DEFAULT(
-					is_rgb, tryfmt->colorspace,
-					tryfmt->ycbcr_enc);
-		}
 	}
 
-	if (ic_route) {
-		tryfmt->quantization = is_rgb ?
-			V4L2_QUANTIZATION_FULL_RANGE :
-			V4L2_QUANTIZATION_LIM_RANGE;
-		tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-	}
+	if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
+		tryfmt->quantization =
+			V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+						      tryfmt->colorspace,
+						      tryfmt->ycbcr_enc);
 }
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);
 
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
 				      &sdformat->format.height,
 				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
 		/* input must be interlaced! Choose SEQ_TB if not */
 		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
 			sdformat->format.field = V4L2_FIELD_SEQ_TB;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
 			    const struct imx_media_pixfmt **cc);
 int imx_media_init_cfg(struct v4l2_subdev *sd,
 		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
 				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -5
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
 		sdformat->format.colorspace = in_fmt->colorspace;
 		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
 		break;
 	case IMX7_CSI_PAD_SINK:
 		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 							 false);
 			sdformat->format.code = (*cc)->codes[0];
 		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
 		break;
 	default:
 		return -EINVAL;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
 	return 0;
 }
 
maintainer
#7 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

In reply to Steve Longerbeam

Hi Steve,

Quoted 10 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

I've applied them on the imx-drm/next branch with Hans' Acked-by on 5/5.

regards
Philipp

#8 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

In reply to Steve Longerbeam

Hi Steve,

Quoted 161 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
 drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
 drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
 drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
 drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
 drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
 drivers/staging/media/imx/imx-media.h       |  5 +-
 drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
 7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 			sdformat->format.code = cc->codes[0];
 		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		break;
 	case PRP_SRC_PAD_PRPENC:
 	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 		break;
 	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
 	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
 	*fmt = sdformat->format;
 out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 	} else {
 		v4l_bound_align_image(&sdformat->format.width,
 				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
 				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
 				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
 	struct v4l2_mbus_framefmt *infmt =
 		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		return;
+	}
 
 	switch (infmt->field) {
 	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
 		break;
 	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
 		/* Reset crop and compose rectangles */
 		crop->left = 0;
 		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
 }
 
 static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +7 -10
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
 EXPORT_SYMBOL_GPL(imx_media_init_cfg);
 
 /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
  *
  * tryfmt->code must be set on entry.
  *
  * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * Y`CbCr encoding must be fixed. The IC supports only BT.601 Y`CbCr
+ * or Rec.709 Y`CbCr encoding.
  */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route)
 {
 	const struct imx_media_pixfmt *cc;
 	bool is_rgb = false;

drivers/staging/media/imx/imx-media-utils.c void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt, · line 541

Quoted 10 lines
drivers/staging/media/imx/imx-media-utils.c +1 -4
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
 	cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
 	if (!cc)
 		cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-	if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+	if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
 		is_rgb = true;
 
-	/* fill field if necessary */
-	if (tryfmt->field == V4L2_FIELD_ANY)
-		tryfmt->field = fmt->field;

Have you tested this patch on i.MX7 ? It breaks pipeline validation as the field field isn't handled anymore in imx7_csi_try_fmt(), due to the removal if this code.

I have a written a fix, I'll prepare a patch.

Quoted 134 lines
drivers/staging/media/imx/imx-media-utils.c +28 -23
+	switch (tryfmt->colorspace) {
+	case V4L2_COLORSPACE_SMPTE170M:
+	case V4L2_COLORSPACE_REC709:
+	case V4L2_COLORSPACE_JPEG:
+	case V4L2_COLORSPACE_SRGB:
+	case V4L2_COLORSPACE_BT2020:
+	case V4L2_COLORSPACE_OPRGB:
+	case V4L2_COLORSPACE_DCI_P3:
+	case V4L2_COLORSPACE_RAW:
+		break;
+	default:
+		tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+		break;
+	}
+
+	if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+		tryfmt->xfer_func =
+			V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
 
-	/* fill colorimetry if necessary */
-	if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) {
-		tryfmt->colorspace = fmt->colorspace;
-		tryfmt->xfer_func = fmt->xfer_func;
-		tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-		tryfmt->quantization = fmt->quantization;
+	if (ic_route) {
+		if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 &&
+		    tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+			tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
 	} else {
-		if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) {
-			tryfmt->xfer_func =
-				V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-		}
 		if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
 			tryfmt->ycbcr_enc =
 				V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
 		}
-		if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) {
-			tryfmt->quantization =
-				V4L2_MAP_QUANTIZATION_DEFAULT(
-					is_rgb, tryfmt->colorspace,
-					tryfmt->ycbcr_enc);
-		}
 	}
 
-	if (ic_route) {
-		tryfmt->quantization = is_rgb ?
-			V4L2_QUANTIZATION_FULL_RANGE :
-			V4L2_QUANTIZATION_LIM_RANGE;
-		tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-	}
+	if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
+		tryfmt->quantization =
+			V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+						      tryfmt->colorspace,
+						      tryfmt->ycbcr_enc);
 }
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);
 
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
 				      &sdformat->format.height,
 				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
 		/* input must be interlaced! Choose SEQ_TB if not */
 		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
 			sdformat->format.field = V4L2_FIELD_SEQ_TB;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
 			    const struct imx_media_pixfmt **cc);
 int imx_media_init_cfg(struct v4l2_subdev *sd,
 		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
 				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -5
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
 		sdformat->format.colorspace = in_fmt->colorspace;
 		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
 		break;
 	case IMX7_CSI_PAD_SINK:
 		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 							 false);
 			sdformat->format.code = (*cc)->codes[0];
 		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
 		break;
 	default:
 		return -EINVAL;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
 	return 0;
 }
 
Signature · 1 line
2.17.1
Signature · 3 lines
Regards,

Laurent Pinchart
author maintainer
#9 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

Hi Laurent,

Quoted 171 lines · Laurent Pinchart

Hi Steve,

Quoted 168 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
  drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
  drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
  drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
  drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
  drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
  drivers/staging/media/imx/imx-media.h       |  5 +-
  drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
  7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
  			sdformat->format.code = cc->codes[0];
  		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  		break;
  	case PRP_SRC_PAD_PRPENC:
  	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
  		break;
  	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
  	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
  	*fmt = sdformat->format;
  out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
  		/* propagate colorimetry from sink */
  		sdformat->format.colorspace = infmt->colorspace;
  		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
  	} else {
  		v4l_bound_align_image(&sdformat->format.width,
  				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
  				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
  				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
  }
 
  static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
  	struct v4l2_mbus_framefmt *infmt =
  		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  		return;
+	}
 
  	switch (infmt->field) {
  	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
  		/* propagate colorimetry from sink */
  		sdformat->format.colorspace = infmt->colorspace;
  		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
  		break;
  	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
  		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
  		/* Reset crop and compose rectangles */
  		crop->left = 0;
  		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
  }
 
  static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +8 -11
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
  EXPORT_SYMBOL_GPL(imx_media_init_cfg);
 
  /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
   *
   * tryfmt->code must be set on entry.
   *
   * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * Y`CbCr encoding must be fixed. The IC supports only BT.601 Y`CbCr
+ * or Rec.709 Y`CbCr encoding.
   */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route)
  {
  	const struct imx_media_pixfmt *cc;
  	bool is_rgb = false;
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
  	cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
  	if (!cc)
  		cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-	if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+	if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
  		is_rgb = true;
 
Quoted 4 lines

drivers/staging/media/imx/imx-media-utils.c void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt, · line 541

Quoted 3 lines
drivers/staging/media/imx/imx-media-utils.c +0 -3
-	/* fill field if necessary */
-	if (tryfmt->field == V4L2_FIELD_ANY)
-		tryfmt->field = fmt->field;

Have you tested this patch on i.MX7 ?

Nope.

Quoted 3 lines

It breaks pipeline validation as
the field field isn't handled anymore in imx7_csi_try_fmt(), due to the removal if this code.

Yep, patches crossed paths.

Quoted 2 lines

I have a written a fix, I'll prepare a patch.

Please do, thanks.

Steve

Quoted 131 lines
Quoted 131 lines
drivers/staging/media/imx/imx-media-utils.c +28 -23
+	switch (tryfmt->colorspace) {
+	case V4L2_COLORSPACE_SMPTE170M:
+	case V4L2_COLORSPACE_REC709:
+	case V4L2_COLORSPACE_JPEG:
+	case V4L2_COLORSPACE_SRGB:
+	case V4L2_COLORSPACE_BT2020:
+	case V4L2_COLORSPACE_OPRGB:
+	case V4L2_COLORSPACE_DCI_P3:
+	case V4L2_COLORSPACE_RAW:
+		break;
+	default:
+		tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+		break;
+	}
+
+	if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+		tryfmt->xfer_func =
+			V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
 
-	/* fill colorimetry if necessary */
-	if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) {
-		tryfmt->colorspace = fmt->colorspace;
-		tryfmt->xfer_func = fmt->xfer_func;
-		tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-		tryfmt->quantization = fmt->quantization;
+	if (ic_route) {
+		if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 &&
+		    tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+			tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
  	} else {
-		if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) {
-			tryfmt->xfer_func =
-				V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-		}
  		if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
  			tryfmt->ycbcr_enc =
  				V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
  		}
-		if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) {
-			tryfmt->quantization =
-				V4L2_MAP_QUANTIZATION_DEFAULT(
-					is_rgb, tryfmt->colorspace,
-					tryfmt->ycbcr_enc);
-		}
  	}
 
-	if (ic_route) {
-		tryfmt->quantization = is_rgb ?
-			V4L2_QUANTIZATION_FULL_RANGE :
-			V4L2_QUANTIZATION_LIM_RANGE;
-		tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-	}
+	if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
+		tryfmt->quantization =
+			V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+						      tryfmt->colorspace,
+						      tryfmt->ycbcr_enc);
  }
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);
 
  int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
  				  struct v4l2_rect *compose,
drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
  				      &sdformat->format.height,
  				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
  		/* input must be interlaced! Choose SEQ_TB if not */
  		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
  			sdformat->format.field = V4L2_FIELD_SEQ_TB;
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
  }
 
  static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
  			    const struct imx_media_pixfmt **cc);
  int imx_media_init_cfg(struct v4l2_subdev *sd,
  		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
  int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
  				  struct v4l2_rect *compose,
  				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -5
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
  		sdformat->format.colorspace = in_fmt->colorspace;
  		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
  		break;
  	case IMX7_CSI_PAD_SINK:
  		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
  							 false);
  			sdformat->format.code = (*cc)->codes[0];
  		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
  		break;
  	default:
  		return -EINVAL;
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
  	return 0;
  }
 
Quoted 3 lines
Quoted 3 lines
Signature · 1 line
2.17.1

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author maintainer
#10 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

Quoted 171 lines · Steve Longerbeam

Hi Laurent,

Quoted 159 lines · Laurent Pinchart

Hi Steve,

Quoted 156 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.
---
  drivers/staging/media/imx/imx-ic-prp.c      |  6 +- drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +-- drivers/staging/media/imx/imx-media-csi.c   | 19 +++--- drivers/staging/media/imx/imx-media-utils.c | 73
++++++++++-----------
  drivers/staging/media/imx/imx-media-vdic.c  |  5 +- drivers/staging/media/imx/imx-media.h       |  5 +- drivers/staging/media/imx/imx7-media-csi.c  |  8 +--

  7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +2 -1
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
               sdformat->format.code = cc->codes[0];
           }
   - imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-                           true);
+        if (sdformat->format.field == V4L2_FIELD_ANY)
+            sdformat->format.field = V4L2_FIELD_NONE;
           break;

      case PRP_SRC_PAD_PRPENC:
      case PRP_SRC_PAD_PRPVF:

drivers/staging/media/imx/imx-ic-prp.c +1 -0
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
           break;
       }
   +    imx_media_try_colorimetry(&sdformat->format, true);
+
       fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
       *fmt = sdformat->format;

  out:

drivers/staging/media/imx/imx-ic-prpencvf.c +4 -3
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
           /* propagate colorimetry from sink */
           sdformat->format.colorspace = infmt->colorspace;
           sdformat->format.xfer_func = infmt->xfer_func;
-        sdformat->format.quantization = infmt->quantization;
-        sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
       } else {
 v4l_bound_align_image(&sdformat->format.width,
                         MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
                         MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
                         S_ALIGN);
   - imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-                           true);
+        if (sdformat->format.field == V4L2_FIELD_ANY)
+            sdformat->format.field = V4L2_FIELD_NONE;
       }
+
+    imx_media_try_colorimetry(&sdformat->format, true);
   }
     static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +0 -0
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,

      struct v4l2_mbus_framefmt *infmt = __csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which); -    /* no restrictions on sink pad field type */
-    if (sdformat->pad == CSI_SINK_PAD)
+    /*
+     * no restrictions on sink pad field type except must +     * be initialized.
+     */
+    if (sdformat->pad == CSI_SINK_PAD) {
+        if (sdformat->format.field == V4L2_FIELD_ANY) +            sdformat->format.field = V4L2_FIELD_NONE; return;
+    }
        switch (infmt->field) {
      case V4L2_FIELD_SEQ_TB:

drivers/staging/media/imx/imx-media-csi.c +0 -2
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
           /* propagate colorimetry from sink */
           sdformat->format.colorspace = infmt->colorspace;
           sdformat->format.xfer_func = infmt->xfer_func;
-        sdformat->format.quantization = infmt->quantization;
-        sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
             break;

      case CSI_SINK_PAD:

drivers/staging/media/imx/imx-media-csi.c +0 -0
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
             csi_try_field(priv, cfg, sdformat);

  -        imx_media_fill_default_mbus_fields(
-            &sdformat->format, infmt,
-            priv->active_output_pad == CSI_SRC_PAD_DIRECT); -
          /* Reset crop and compose rectangles */ crop->left = 0;
          crop->top = 0;

drivers/staging/media/imx/imx-media-csi.c +3 -0
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
             break;
       }
+
+    imx_media_try_colorimetry(&sdformat->format,
+            priv->active_output_pad == CSI_SRC_PAD_DIRECT);
   }
     static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +3 -6
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
   EXPORT_SYMBOL_GPL(imx_media_init_cfg);
     /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
    *
    * tryfmt->code must be set on entry.
    *
    * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and 

output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * YCbCr encoding must be fixed. The IC supports only BT.601 YCbCr
+ * or Rec.709 Y`CbCr encoding.
   */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-                    struct v4l2_mbus_framefmt *fmt,
-                    bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt, +                   bool ic_route)
  {
      const struct imx_media_pixfmt *cc;
      bool is_rgb = false;

drivers/staging/media/imx/imx-media-utils.c +0 -0
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct 
 v4l2_mbus_framefmt *tryfmt,
       cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
       if (!cc)
Quoted 8 lines
Quoted 7 lines
drivers/staging/media/imx/imx-media-utils.c +1 -1
           cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-    if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+    if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
           is_rgb = true;

  -    /* fill field if necessary */
-    if (tryfmt->field == V4L2_FIELD_ANY)
-        tryfmt->field = fmt->field;

Have you tested this patch on i.MX7 ?

Quoted 7 lines

Nope.

Quoted 3 lines

  It breaks pipeline validation as
the field field isn't handled anymore in imx7_csi_try_fmt(), due to the removal if this code.

Yep, patches crossed paths.

Hmm, actually after closer review it looks like this was missed in this patch. So in the IMX7_CSI_PAD_SINK case in imx7_csi_try_fmt(), need to add:

if (sdformat->format.field == V4L2_FIELD_ANY)
        sdformat->format.field = V4L2_FIELD_NONE;

Steve

Quoted 134 lines
Quoted 133 lines
Quoted 133 lines

+    switch (tryfmt->colorspace) {
+    case V4L2_COLORSPACE_SMPTE170M:
+    case V4L2_COLORSPACE_REC709:
+    case V4L2_COLORSPACE_JPEG:
+    case V4L2_COLORSPACE_SRGB:
+    case V4L2_COLORSPACE_BT2020:
+    case V4L2_COLORSPACE_OPRGB:
+    case V4L2_COLORSPACE_DCI_P3:
+    case V4L2_COLORSPACE_RAW:
+        break;
+    default:
+        tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+        break;
+    }
+
+    if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+        tryfmt->xfer_func =
+ V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
  -    /* fill colorimetry if necessary */
-    if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) { -        tryfmt->colorspace = fmt->colorspace;
-        tryfmt->xfer_func = fmt->xfer_func;
-        tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-        tryfmt->quantization = fmt->quantization;
+    if (ic_route) {
+        if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 && +            tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+            tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
      } else {
-        if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) { -            tryfmt->xfer_func =
- V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-        }
          if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) { tryfmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
          }
-        if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) { -            tryfmt->quantization =
-                V4L2_MAP_QUANTIZATION_DEFAULT(
-                    is_rgb, tryfmt->colorspace,
-                    tryfmt->ycbcr_enc);
-        }
      }
  -    if (ic_route) {
-        tryfmt->quantization = is_rgb ?
-            V4L2_QUANTIZATION_FULL_RANGE :
-            V4L2_QUANTIZATION_LIM_RANGE;
-        tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-    }
+    if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) +        tryfmt->quantization =
+            V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+                              tryfmt->colorspace,
+                              tryfmt->ycbcr_enc);
  }

drivers/staging/media/imx/imx-media-utils.c +1 -1
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);

    int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix, struct v4l2_rect *compose,

drivers/staging/media/imx/imx-media-vdic.c +2 -2
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
                         &sdformat->format.height,
                         MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
   - imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-                           true);
-
           /* input must be interlaced! Choose SEQ_TB if not */
           if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
               sdformat->format.field = V4L2_FIELD_SEQ_TB;
           break;
       }
+
+    imx_media_try_colorimetry(&sdformat->format, true);
   }
     static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct 
 v4l2_mbus_framefmt *mbus,
                   const struct imx_media_pixfmt **cc);
   int imx_media_init_cfg(struct v4l2_subdev *sd,
                  struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt 
 *tryfmt,
-                    struct v4l2_mbus_framefmt *fmt,
-                    bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+                   bool ic_route);
   int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,

                    struct v4l2_rect *compose, const struct v4l2_mbus_framefmt *mbus,

drivers/staging/media/imx/imx7-media-csi.c +0 -2
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
             sdformat->format.colorspace = in_fmt->colorspace;
           sdformat->format.xfer_func = in_fmt->xfer_func;
-        sdformat->format.quantization = in_fmt->quantization;
-        sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
           break;

      case IMX7_CSI_PAD_SINK:
          *cc = imx_media_find_mbus_format(sdformat->format.code,

drivers/staging/media/imx/imx7-media-csi.c +0 -3
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi 
 *csi,
                                false);
               sdformat->format.code = (*cc)->codes[0];
           }
-
- imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-                           false);
           break;

      default:
          return -EINVAL;
          break;
      }

drivers/staging/media/imx/imx7-media-csi.c +3 -0
+
+    imx_media_try_colorimetry(&sdformat->format, false);
+

      return 0;
  }
  --
2.17.1

#11 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

In reply to Laurent Pinchart

Hi Laurent,

Quoted 179 lines · Laurent Pinchart

Hi Steve,

Quoted 161 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
 drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
 drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
 drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
 drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
 drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
 drivers/staging/media/imx/imx-media.h       |  5 +-
 drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
 7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 			sdformat->format.code = cc->codes[0];
 		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		break;
 	case PRP_SRC_PAD_PRPENC:
 	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
 		break;
 	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
 	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
 	*fmt = sdformat->format;
 out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 	} else {
 		v4l_bound_align_image(&sdformat->format.width,
 				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
 				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
 				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
 	struct v4l2_mbus_framefmt *infmt =
 		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
 		return;
+	}
 
 	switch (infmt->field) {
 	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 		/* propagate colorimetry from sink */
 		sdformat->format.colorspace = infmt->colorspace;
 		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
 		break;
 	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
 		/* Reset crop and compose rectangles */
 		crop->left = 0;
 		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
 }
 
 static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +7 -10
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
 EXPORT_SYMBOL_GPL(imx_media_init_cfg);
 
 /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
  *
  * tryfmt->code must be set on entry.
  *
  * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * Y`CbCr encoding must be fixed. The IC supports only BT.601 Y`CbCr
+ * or Rec.709 Y`CbCr encoding.
  */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route)
 {
 	const struct imx_media_pixfmt *cc;
 	bool is_rgb = false;

drivers/staging/media/imx/imx-media-utils.c void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt, · line 541

Quoted 10 lines
drivers/staging/media/imx/imx-media-utils.c +1 -4
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
 	cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
 	if (!cc)
 		cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-	if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+	if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
 		is_rgb = true;
 
-	/* fill field if necessary */
-	if (tryfmt->field == V4L2_FIELD_ANY)
-		tryfmt->field = fmt->field;

Have you tested this patch on i.MX7 ? It breaks pipeline validation as the field field isn't handled anymore in imx7_csi_try_fmt(), due to the removal if this code.

Quoted 2 lines

I have a written a fix, I'll prepare a patch.

Somehow I missed completely this series. I do not recall it at all. Thanks in advance for your patch Laurent.

---
Cheers,
Rui

Quoted 135 lines
Quoted 135 lines
drivers/staging/media/imx/imx-media-utils.c +28 -23
+	switch (tryfmt->colorspace) {
+	case V4L2_COLORSPACE_SMPTE170M:
+	case V4L2_COLORSPACE_REC709:
+	case V4L2_COLORSPACE_JPEG:
+	case V4L2_COLORSPACE_SRGB:
+	case V4L2_COLORSPACE_BT2020:
+	case V4L2_COLORSPACE_OPRGB:
+	case V4L2_COLORSPACE_DCI_P3:
+	case V4L2_COLORSPACE_RAW:
+		break;
+	default:
+		tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+		break;
+	}
+
+	if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+		tryfmt->xfer_func =
+			V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
 
-	/* fill colorimetry if necessary */
-	if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) {
-		tryfmt->colorspace = fmt->colorspace;
-		tryfmt->xfer_func = fmt->xfer_func;
-		tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-		tryfmt->quantization = fmt->quantization;
+	if (ic_route) {
+		if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 &&
+		    tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+			tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
 	} else {
-		if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) {
-			tryfmt->xfer_func =
-				V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-		}
 		if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
 			tryfmt->ycbcr_enc =
 				V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
 		}
-		if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) {
-			tryfmt->quantization =
-				V4L2_MAP_QUANTIZATION_DEFAULT(
-					is_rgb, tryfmt->colorspace,
-					tryfmt->ycbcr_enc);
-		}
 	}
 
-	if (ic_route) {
-		tryfmt->quantization = is_rgb ?
-			V4L2_QUANTIZATION_FULL_RANGE :
-			V4L2_QUANTIZATION_LIM_RANGE;
-		tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-	}
+	if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
+		tryfmt->quantization =
+			V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+						      tryfmt->colorspace,
+						      tryfmt->ycbcr_enc);
 }
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);
 
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
 				      &sdformat->format.height,
 				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
 		/* input must be interlaced! Choose SEQ_TB if not */
 		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
 			sdformat->format.field = V4L2_FIELD_SEQ_TB;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
 }
 
 static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
 			    const struct imx_media_pixfmt **cc);
 int imx_media_init_cfg(struct v4l2_subdev *sd,
 		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
 int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
 				  struct v4l2_rect *compose,
 				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -6
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
 		sdformat->format.colorspace = in_fmt->colorspace;
 		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
 		break;
 	case IMX7_CSI_PAD_SINK:
 		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 							 false);
 			sdformat->format.code = (*cc)->codes[0];
 		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
 		break;
 	default:
 		return -EINVAL;
 		break;
 	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
 	return 0;
 }
 
--

2.17.1

author maintainer
#12 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

Hi Laurent, Rui,

Besides field type ANY, the imx7 CSI should probably be dealing with other field type conversions as well. I may be mistaken, but like the imx6, the imx7 does not have the ability to detect whether a captured field is a top field or a bottom field, so it can't support ALTERNATE mode. It should probably capture two consecutive fields and in that case and report seq-tb or seq-bt at its output. Also the imx6 supports interlacing field lines, if that is the case for imx7 it should also support converting ALTERNATE to INTERLACED at its output.

Steve

Quoted 323 lines · Rui Miguel Silva

Hi Laurent,

Quoted 171 lines · Laurent Pinchart

Hi Steve,

Quoted 168 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
  drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
  drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
  drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
  drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
  drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
  drivers/staging/media/imx/imx-media.h       |  5 +-
  drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
  7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
  			sdformat->format.code = cc->codes[0];
  		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  		break;
  	case PRP_SRC_PAD_PRPENC:
  	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
  		break;
  	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
  	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
  	*fmt = sdformat->format;
  out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
  		/* propagate colorimetry from sink */
  		sdformat->format.colorspace = infmt->colorspace;
  		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
  	} else {
  		v4l_bound_align_image(&sdformat->format.width,
  				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
  				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
  				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
  }
 
  static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
  	struct v4l2_mbus_framefmt *infmt =
  		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  		return;
+	}
 
  	switch (infmt->field) {
  	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
  		/* propagate colorimetry from sink */
  		sdformat->format.colorspace = infmt->colorspace;
  		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
  		break;
  	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
  		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
  		/* Reset crop and compose rectangles */
  		crop->left = 0;
  		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
  }
 
  static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +8 -11
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
  EXPORT_SYMBOL_GPL(imx_media_init_cfg);
 
  /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
   *
   * tryfmt->code must be set on entry.
   *
   * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * Y`CbCr encoding must be fixed. The IC supports only BT.601 Y`CbCr
+ * or Rec.709 Y`CbCr encoding.
   */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route)
  {
  	const struct imx_media_pixfmt *cc;
  	bool is_rgb = false;
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
  	cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
  	if (!cc)
  		cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-	if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+	if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
  		is_rgb = true;
 
Quoted 9 lines

drivers/staging/media/imx/imx-media-utils.c void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt, · line 541

Quoted 3 lines
drivers/staging/media/imx/imx-media-utils.c +0 -3
-	/* fill field if necessary */
-	if (tryfmt->field == V4L2_FIELD_ANY)
-		tryfmt->field = fmt->field;

Have you tested this patch on i.MX7 ? It breaks pipeline validation as the field field isn't handled anymore in imx7_csi_try_fmt(), due to the removal if this code.

I have a written a fix, I'll prepare a patch.

Somehow I missed completely this series. I do not recall it at all. Thanks in advance for your patch Laurent.

---
Cheers,
Rui

Quoted 134 lines
Quoted 134 lines
drivers/staging/media/imx/imx-media-utils.c +28 -23
+	switch (tryfmt->colorspace) {
+	case V4L2_COLORSPACE_SMPTE170M:
+	case V4L2_COLORSPACE_REC709:
+	case V4L2_COLORSPACE_JPEG:
+	case V4L2_COLORSPACE_SRGB:
+	case V4L2_COLORSPACE_BT2020:
+	case V4L2_COLORSPACE_OPRGB:
+	case V4L2_COLORSPACE_DCI_P3:
+	case V4L2_COLORSPACE_RAW:
+		break;
+	default:
+		tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+		break;
+	}
+
+	if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+		tryfmt->xfer_func =
+			V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
 
-	/* fill colorimetry if necessary */
-	if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) {
-		tryfmt->colorspace = fmt->colorspace;
-		tryfmt->xfer_func = fmt->xfer_func;
-		tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-		tryfmt->quantization = fmt->quantization;
+	if (ic_route) {
+		if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 &&
+		    tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+			tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
  	} else {
-		if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) {
-			tryfmt->xfer_func =
-				V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-		}
  		if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
  			tryfmt->ycbcr_enc =
  				V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
  		}
-		if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) {
-			tryfmt->quantization =
-				V4L2_MAP_QUANTIZATION_DEFAULT(
-					is_rgb, tryfmt->colorspace,
-					tryfmt->ycbcr_enc);
-		}
  	}
 
-	if (ic_route) {
-		tryfmt->quantization = is_rgb ?
-			V4L2_QUANTIZATION_FULL_RANGE :
-			V4L2_QUANTIZATION_LIM_RANGE;
-		tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-	}
+	if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
+		tryfmt->quantization =
+			V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+						      tryfmt->colorspace,
+						      tryfmt->ycbcr_enc);
  }
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);
 
  int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
  				  struct v4l2_rect *compose,
drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
  				      &sdformat->format.height,
  				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
  		/* input must be interlaced! Choose SEQ_TB if not */
  		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
  			sdformat->format.field = V4L2_FIELD_SEQ_TB;
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
  }
 
  static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
  			    const struct imx_media_pixfmt **cc);
  int imx_media_init_cfg(struct v4l2_subdev *sd,
  		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
  int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
  				  struct v4l2_rect *compose,
  				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -6
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
  		sdformat->format.colorspace = in_fmt->colorspace;
  		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
  		break;
  	case IMX7_CSI_PAD_SINK:
  		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
  							 false);
  			sdformat->format.code = (*cc)->codes[0];
  		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
  		break;
  	default:
  		return -EINVAL;
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
  	return 0;
  }
 
--

2.17.1

#13 lore raw

Re: [PATCH v8 5/5] media: imx: Try colorimetry at both sink and source pads

Hi Steve,

Quoted 10 lines · Steve Longerbeam

Hi Laurent, Rui,

Besides field type ANY, the imx7 CSI should probably be dealing with other field type conversions as well. I may be mistaken, but like the imx6, the imx7 does not have the ability to detect whether a captured field is a top field or a bottom field, so it can't support ALTERNATE mode. It should probably capture two consecutive fields and in that case and report seq-tb or seq-bt at its output. Also the imx6 supports interlacing field lines, if that is the case for imx7 it should also support converting ALTERNATE to INTERLACED at its output.

Yeah, that makes sense to me, I already saw yours csi_try_field that does something in this lines.

I will try to handle that in imx7 also.

Thanks for your inputs here.

Cheers,
Rui

Quoted 328 lines

Steve

Quoted 323 lines · Rui Miguel Silva

Hi Laurent,

Quoted 171 lines · Laurent Pinchart

Hi Steve,

Quoted 168 lines · Steve Longerbeam

Retask imx_media_fill_default_mbus_fields() to try colorimetry parameters, renaming it to to imx_media_try_colorimetry(), and call it at both sink and source pad try_fmt's. The unrelated check for uninitialized field value is moved out to appropriate places in each subdev try_fmt.

The IC now supports Rec.709 and BT.601 Y'CbCr encoding, and both limited and full range quantization for both YUV and RGB space, so allow those for pipelines that route through the IC.

---
Changes in v7:
- squashed with "media: imx: Allow Rec.709 encoding for IC routes".
- remove the RGB full-range quantization restriction for IC routes.

---
  drivers/staging/media/imx/imx-ic-prp.c      |  6 +-
  drivers/staging/media/imx/imx-ic-prpencvf.c |  8 +--
  drivers/staging/media/imx/imx-media-csi.c   | 19 +++---
  drivers/staging/media/imx/imx-media-utils.c | 73 ++++++++++-----------
  drivers/staging/media/imx/imx-media-vdic.c  |  5 +-
  drivers/staging/media/imx/imx-media.h       |  5 +-
  drivers/staging/media/imx/imx7-media-csi.c  |  8 +--
  7 files changed, 62 insertions(+), 62 deletions(-)
drivers/staging/media/imx/imx-ic-prp.c +4 -2
index 10ffe00f1a54..f87fe0203720 100644
@@ -193,8 +193,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
  			sdformat->format.code = cc->codes[0];
  		}
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  		break;
  	case PRP_SRC_PAD_PRPENC:
  	case PRP_SRC_PAD_PRPVF:
@@ -203,6 +203,8 @@ static int prp_set_fmt(struct v4l2_subdev *sd,
  		break;
  	}
 
+	imx_media_try_colorimetry(&sdformat->format, true);
+
  	fmt = __prp_get_fmt(priv, cfg, sdformat->pad, sdformat->which);
  	*fmt = sdformat->format;
  out:
drivers/staging/media/imx/imx-ic-prpencvf.c +4 -4
index e8b36a181ccc..f2fe3c11c70e 100644
@@ -907,8 +907,6 @@ static void prp_try_fmt(struct prp_priv *priv,
  		/* propagate colorimetry from sink */
  		sdformat->format.colorspace = infmt->colorspace;
  		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
  	} else {
  		v4l_bound_align_image(&sdformat->format.width,
  				      MIN_W_SINK, MAX_W_SINK, W_ALIGN_SINK,
@@ -916,9 +914,11 @@ static void prp_try_fmt(struct prp_priv *priv,
  				      MIN_H_SINK, MAX_H_SINK, H_ALIGN_SINK,
  				      S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
  }
 
  static int prp_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-csi.c +11 -8
index 1d248aca40a9..dce4addadff4 100644
@@ -1375,9 +1375,15 @@ static void csi_try_field(struct csi_priv *priv,
  	struct v4l2_mbus_framefmt *infmt =
  		__csi_get_fmt(priv, cfg, CSI_SINK_PAD, sdformat->which);
 
-	/* no restrictions on sink pad field type */
-	if (sdformat->pad == CSI_SINK_PAD)
+	/*
+	 * no restrictions on sink pad field type except must
+	 * be initialized.
+	 */
+	if (sdformat->pad == CSI_SINK_PAD) {
+		if (sdformat->format.field == V4L2_FIELD_ANY)
+			sdformat->format.field = V4L2_FIELD_NONE;
  		return;
+	}
 
  	switch (infmt->field) {
  	case V4L2_FIELD_SEQ_TB:
@@ -1455,8 +1461,6 @@ static void csi_try_fmt(struct csi_priv *priv,
  		/* propagate colorimetry from sink */
  		sdformat->format.colorspace = infmt->colorspace;
  		sdformat->format.xfer_func = infmt->xfer_func;
-		sdformat->format.quantization = infmt->quantization;
-		sdformat->format.ycbcr_enc = infmt->ycbcr_enc;
 
  		break;
  	case CSI_SINK_PAD:
@@ -1476,10 +1480,6 @@ static void csi_try_fmt(struct csi_priv *priv,
 
  		csi_try_field(priv, cfg, sdformat);
 
-		imx_media_fill_default_mbus_fields(
-			&sdformat->format, infmt,
-			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
-
  		/* Reset crop and compose rectangles */
  		crop->left = 0;
  		crop->top = 0;
@@ -1495,6 +1495,9 @@ static void csi_try_fmt(struct csi_priv *priv,
 
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format,
+			priv->active_output_pad == CSI_SRC_PAD_DIRECT);
  }
 
  static int csi_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media-utils.c +8 -11
index b41842dba5ec..05b63395084e 100644
@@ -511,21 +511,18 @@ int imx_media_init_cfg(struct v4l2_subdev *sd,
  EXPORT_SYMBOL_GPL(imx_media_init_cfg);
 
  /*
- * Check whether the field and colorimetry parameters in tryfmt are
- * uninitialized, and if so fill them with the values from fmt,
- * or if tryfmt->colorspace has been initialized, all the default
- * colorimetry params can be derived from tryfmt->colorspace.
+ * Default the colorspace in tryfmt to SRGB if set to an unsupported
+ * colorspace or not initialized. Then set the remaining colorimetry
+ * parameters based on the colorspace if they are uninitialized.
   *
   * tryfmt->code must be set on entry.
   *
   * If this format is destined to be routed through the Image Converter,
- * quantization and Y`CbCr encoding must be fixed. The IC expects and
- * produces fixed quantization and Y`CbCr encoding at its input and output
- * (full range for RGB, limited range for YUV, and V4L2_YCBCR_ENC_601).
+ * Y`CbCr encoding must be fixed. The IC supports only BT.601 Y`CbCr
+ * or Rec.709 Y`CbCr encoding.
   */
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route)
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route)
  {
  	const struct imx_media_pixfmt *cc;
  	bool is_rgb = false;
@@ -533,44 +530,46 @@ void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
  	cc = imx_media_find_mbus_format(tryfmt->code, CS_SEL_ANY, true);
  	if (!cc)
  		cc = imx_media_find_ipu_format(tryfmt->code, CS_SEL_ANY);
-	if (cc && cc->cs != IPUV3_COLORSPACE_YUV)
+	if (cc && cc->cs == IPUV3_COLORSPACE_RGB)
  		is_rgb = true;
 
Quoted 9 lines

drivers/staging/media/imx/imx-media-utils.c void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt, · line 541

Quoted 3 lines
drivers/staging/media/imx/imx-media-utils.c +0 -3
-	/* fill field if necessary */
-	if (tryfmt->field == V4L2_FIELD_ANY)
-		tryfmt->field = fmt->field;

Have you tested this patch on i.MX7 ? It breaks pipeline validation as the field field isn't handled anymore in imx7_csi_try_fmt(), due to the removal if this code.

I have a written a fix, I'll prepare a patch.

Somehow I missed completely this series. I do not recall it at all. Thanks in advance for your patch Laurent.

---
Cheers,
Rui

Quoted 134 lines
Quoted 134 lines
drivers/staging/media/imx/imx-media-utils.c +28 -23
+	switch (tryfmt->colorspace) {
+	case V4L2_COLORSPACE_SMPTE170M:
+	case V4L2_COLORSPACE_REC709:
+	case V4L2_COLORSPACE_JPEG:
+	case V4L2_COLORSPACE_SRGB:
+	case V4L2_COLORSPACE_BT2020:
+	case V4L2_COLORSPACE_OPRGB:
+	case V4L2_COLORSPACE_DCI_P3:
+	case V4L2_COLORSPACE_RAW:
+		break;
+	default:
+		tryfmt->colorspace = V4L2_COLORSPACE_SRGB;
+		break;
+	}
+
+	if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT)
+		tryfmt->xfer_func =
+			V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
 
-	/* fill colorimetry if necessary */
-	if (tryfmt->colorspace == V4L2_COLORSPACE_DEFAULT) {
-		tryfmt->colorspace = fmt->colorspace;
-		tryfmt->xfer_func = fmt->xfer_func;
-		tryfmt->ycbcr_enc = fmt->ycbcr_enc;
-		tryfmt->quantization = fmt->quantization;
+	if (ic_route) {
+		if (tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_601 &&
+		    tryfmt->ycbcr_enc != V4L2_YCBCR_ENC_709)
+			tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
  	} else {
-		if (tryfmt->xfer_func == V4L2_XFER_FUNC_DEFAULT) {
-			tryfmt->xfer_func =
-				V4L2_MAP_XFER_FUNC_DEFAULT(tryfmt->colorspace);
-		}
  		if (tryfmt->ycbcr_enc == V4L2_YCBCR_ENC_DEFAULT) {
  			tryfmt->ycbcr_enc =
  				V4L2_MAP_YCBCR_ENC_DEFAULT(tryfmt->colorspace);
  		}
-		if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT) {
-			tryfmt->quantization =
-				V4L2_MAP_QUANTIZATION_DEFAULT(
-					is_rgb, tryfmt->colorspace,
-					tryfmt->ycbcr_enc);
-		}
  	}
 
-	if (ic_route) {
-		tryfmt->quantization = is_rgb ?
-			V4L2_QUANTIZATION_FULL_RANGE :
-			V4L2_QUANTIZATION_LIM_RANGE;
-		tryfmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
-	}
+	if (tryfmt->quantization == V4L2_QUANTIZATION_DEFAULT)
+		tryfmt->quantization =
+			V4L2_MAP_QUANTIZATION_DEFAULT(is_rgb,
+						      tryfmt->colorspace,
+						      tryfmt->ycbcr_enc);
  }
-EXPORT_SYMBOL_GPL(imx_media_fill_default_mbus_fields);
+EXPORT_SYMBOL_GPL(imx_media_try_colorimetry);
 
  int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
  				  struct v4l2_rect *compose,
drivers/staging/media/imx/imx-media-vdic.c +2 -3
index 4487374c9435..fbafd7fb7aeb 100644
@@ -617,14 +617,13 @@ static void vdic_try_fmt(struct vdic_priv *priv,
  				      &sdformat->format.height,
  				      MIN_H, MAX_H_VDIC, H_ALIGN, S_ALIGN);
 
-		imx_media_fill_default_mbus_fields(&sdformat->format, infmt,
-						   true);
-
  		/* input must be interlaced! Choose SEQ_TB if not */
  		if (!V4L2_FIELD_HAS_BOTH(sdformat->format.field))
  			sdformat->format.field = V4L2_FIELD_SEQ_TB;
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, true);
  }
 
  static int vdic_set_fmt(struct v4l2_subdev *sd,
drivers/staging/media/imx/imx-media.h +2 -3
index 6587aa49e005..23024c9bc887 100644
@@ -172,9 +172,8 @@ int imx_media_init_mbus_fmt(struct v4l2_mbus_framefmt *mbus,
  			    const struct imx_media_pixfmt **cc);
  int imx_media_init_cfg(struct v4l2_subdev *sd,
  		       struct v4l2_subdev_pad_config *cfg);
-void imx_media_fill_default_mbus_fields(struct v4l2_mbus_framefmt *tryfmt,
-					struct v4l2_mbus_framefmt *fmt,
-					bool ic_route);
+void imx_media_try_colorimetry(struct v4l2_mbus_framefmt *tryfmt,
+			       bool ic_route);
  int imx_media_mbus_fmt_to_pix_fmt(struct v4l2_pix_format *pix,
  				  struct v4l2_rect *compose,
  				  const struct v4l2_mbus_framefmt *mbus,
drivers/staging/media/imx/imx7-media-csi.c +3 -6
index a708a0340eb1..6e2f4c3eb24f 100644
@@ -1003,8 +1003,6 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
 
  		sdformat->format.colorspace = in_fmt->colorspace;
  		sdformat->format.xfer_func = in_fmt->xfer_func;
-		sdformat->format.quantization = in_fmt->quantization;
-		sdformat->format.ycbcr_enc = in_fmt->ycbcr_enc;
  		break;
  	case IMX7_CSI_PAD_SINK:
  		*cc = imx_media_find_mbus_format(sdformat->format.code,
@@ -1015,14 +1013,14 @@ static int imx7_csi_try_fmt(struct imx7_csi *csi,
  							 false);
  			sdformat->format.code = (*cc)->codes[0];
  		}
-
-		imx_media_fill_default_mbus_fields(&sdformat->format, in_fmt,
-						   false);
  		break;
  	default:
  		return -EINVAL;
  		break;
  	}
+
+	imx_media_try_colorimetry(&sdformat->format, false);
+
  	return 0;
  }
 
--

2.17.1