linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] media: imx-pxp: Add rotation support
@ 2021-10-07 12:57 Fabio Estevam
  2021-10-08 12:38 ` Philipp Zabel
  0 siblings, 1 reply; 2+ messages in thread
From: Fabio Estevam @ 2021-10-07 12:57 UTC (permalink / raw)
  To: p.zabel; +Cc: hverkuil-cisco, linux-media, Fabio Estevam

PXP allows clockwise rotation of 0°, 90°, 180° and 270°.

Add support for it.

Tested on a imx6ull-evk.

For example, to rotate 90° the following Gstreamer pipeline can
be used:

gst-launch-1.0 videotestsrc ! video/x-raw,width=640,height=480 ! \
v4l2convert extra-controls=cid,rotate=90  ! \
video/x-raw,width=120,height=160 ! fbdevsink

Signed-off-by: Fabio Estevam <festevam@denx.de>
---
Changes since v2:
- Move the swap prior to the first usage of dst_height.

 drivers/media/platform/imx-pxp.c | 38 ++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/drivers/media/platform/imx-pxp.c b/drivers/media/platform/imx-pxp.c
index 723b096fedd1..acfc77f9d58f 100644
--- a/drivers/media/platform/imx-pxp.c
+++ b/drivers/media/platform/imx-pxp.c
@@ -168,6 +168,13 @@ enum {
 	V4L2_M2M_DST = 1,
 };
 
+enum pxp_rotation_modes {
+	PXP_ROTATE_0,
+	PXP_ROTATE_90,
+	PXP_ROTATE_180,
+	PXP_ROTATE_270,
+};
+
 static struct pxp_fmt *find_format(struct v4l2_format *f)
 {
 	struct pxp_fmt *fmt;
@@ -211,6 +218,7 @@ struct pxp_ctx {
 	/* Processing mode */
 	int			mode;
 	u8			alpha_component;
+	u8			rotation;
 
 	enum v4l2_colorspace	colorspace;
 	enum v4l2_xfer_func	xfer_func;
@@ -767,14 +775,19 @@ static int pxp_start(struct pxp_ctx *ctx, struct vb2_v4l2_buffer *in_vb,
 		 V4L2_BUF_FLAG_BFRAME |
 		 V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
 
-	/* Rotation disabled, 8x8 block size */
+	/* 8x8 block size */
 	ctrl = BF_PXP_CTRL_VFLIP0(!!(ctx->mode & MEM2MEM_VFLIP)) |
-	       BF_PXP_CTRL_HFLIP0(!!(ctx->mode & MEM2MEM_HFLIP));
+	       BF_PXP_CTRL_HFLIP0(!!(ctx->mode & MEM2MEM_HFLIP)) |
+	       BF_PXP_CTRL_ROTATE0(ctx->rotation);
 	/* Always write alpha value as V4L2_CID_ALPHA_COMPONENT */
 	out_ctrl = BF_PXP_OUT_CTRL_ALPHA(ctx->alpha_component) |
 		   BF_PXP_OUT_CTRL_ALPHA_OUTPUT(1) |
 		   pxp_v4l2_pix_fmt_to_out_format(dst_fourcc);
 	out_buf = p_out;
+
+	if (ctx->rotation == PXP_ROTATE_90 || ctx->rotation == PXP_ROTATE_270)
+		swap(dst_width, dst_height);
+
 	switch (dst_fourcc) {
 	case V4L2_PIX_FMT_NV12:
 	case V4L2_PIX_FMT_NV21:
@@ -1297,6 +1310,22 @@ static int pxp_s_fmt_vid_out(struct file *file, void *priv,
 	return 0;
 }
 
+static u8 pxp_degrees_to_rot_mode(u32 degrees)
+{
+	switch (degrees) {
+	case 0:
+		return PXP_ROTATE_0;
+	case 90:
+		return PXP_ROTATE_90;
+	case 180:
+		return PXP_ROTATE_180;
+	case 270:
+		return PXP_ROTATE_270;
+	default:
+		return PXP_ROTATE_0;
+	}
+}
+
 static int pxp_s_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct pxp_ctx *ctx =
@@ -1317,6 +1346,10 @@ static int pxp_s_ctrl(struct v4l2_ctrl *ctrl)
 			ctx->mode &= ~MEM2MEM_VFLIP;
 		break;
 
+	case V4L2_CID_ROTATE:
+		ctx->rotation = pxp_degrees_to_rot_mode(ctrl->val);
+		break;
+
 	case V4L2_CID_ALPHA_COMPONENT:
 		ctx->alpha_component = ctrl->val;
 		break;
@@ -1524,6 +1557,7 @@ static int pxp_open(struct file *file)
 	v4l2_ctrl_handler_init(hdl, 4);
 	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
 	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
+	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
 	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
 			  0, 255, 1, 255);
 	if (hdl->error) {
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] media: imx-pxp: Add rotation support
  2021-10-07 12:57 [PATCH v3] media: imx-pxp: Add rotation support Fabio Estevam
@ 2021-10-08 12:38 ` Philipp Zabel
  0 siblings, 0 replies; 2+ messages in thread
From: Philipp Zabel @ 2021-10-08 12:38 UTC (permalink / raw)
  To: Fabio Estevam; +Cc: hverkuil-cisco, linux-media

Hi Fabio,

On Thu, 2021-10-07 at 09:57 -0300, Fabio Estevam wrote:
> PXP allows clockwise rotation of 0°, 90°, 180° and 270°.
> 
> Add support for it.
> 
> Tested on a imx6ull-evk.
> 
> For example, to rotate 90° the following Gstreamer pipeline can
> be used:
> 
> gst-launch-1.0 videotestsrc ! video/x-raw,width=640,height=480 ! \
> v4l2convert extra-controls=cid,rotate=90  ! \
> video/x-raw,width=120,height=160 ! fbdevsink
> 
> Signed-off-by: Fabio Estevam <festevam@denx.de>
> ---
> Changes since v2:
> - Move the swap prior to the first usage of dst_height.
> 
>  drivers/media/platform/imx-pxp.c | 38 ++++++++++++++++++++++++++++++--
>  1 file changed, 36 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/media/platform/imx-pxp.c b/drivers/media/platform/imx-pxp.c
> index 723b096fedd1..acfc77f9d58f 100644
> --- a/drivers/media/platform/imx-pxp.c
> +++ b/drivers/media/platform/imx-pxp.c
> @@ -168,6 +168,13 @@ enum {
>  	V4L2_M2M_DST = 1,
>  };
>  
> +enum pxp_rotation_modes {
> +	PXP_ROTATE_0,
> +	PXP_ROTATE_90,
> +	PXP_ROTATE_180,
> +	PXP_ROTATE_270,

These values are already defined in the imx-pxp.h header:

#define BV_PXP_CTRL_ROTATE0__ROT_0   0x0
#define BV_PXP_CTRL_ROTATE0__ROT_90  0x1
#define BV_PXP_CTRL_ROTATE0__ROT_180 0x2
#define BV_PXP_CTRL_ROTATE0__ROT_270 0x3

I'd use those instead.

> +};
> +
>  static struct pxp_fmt *find_format(struct v4l2_format *f)
>  {
>  	struct pxp_fmt *fmt;
> @@ -211,6 +218,7 @@ struct pxp_ctx {
>  	/* Processing mode */
>  	int			mode;
>  	u8			alpha_component;
> +	u8			rotation;
>  
>  	enum v4l2_colorspace	colorspace;
>  	enum v4l2_xfer_func	xfer_func;
> @@ -767,14 +775,19 @@ static int pxp_start(struct pxp_ctx *ctx, struct vb2_v4l2_buffer *in_vb,
>  		 V4L2_BUF_FLAG_BFRAME |
>  		 V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
>  
> -	/* Rotation disabled, 8x8 block size */
> +	/* 8x8 block size */
>  	ctrl = BF_PXP_CTRL_VFLIP0(!!(ctx->mode & MEM2MEM_VFLIP)) |
> -	       BF_PXP_CTRL_HFLIP0(!!(ctx->mode & MEM2MEM_HFLIP));
> +	       BF_PXP_CTRL_HFLIP0(!!(ctx->mode & MEM2MEM_HFLIP)) |
> +	       BF_PXP_CTRL_ROTATE0(ctx->rotation);
>  	/* Always write alpha value as V4L2_CID_ALPHA_COMPONENT */
>  	out_ctrl = BF_PXP_OUT_CTRL_ALPHA(ctx->alpha_component) |
>  		   BF_PXP_OUT_CTRL_ALPHA_OUTPUT(1) |
>  		   pxp_v4l2_pix_fmt_to_out_format(dst_fourcc);
>  	out_buf = p_out;
> +
> +	if (ctx->rotation == PXP_ROTATE_90 || ctx->rotation == PXP_ROTATE_270)
> +		swap(dst_width, dst_height);
> +
>  	switch (dst_fourcc) {
>  	case V4L2_PIX_FMT_NV12:
>  	case V4L2_PIX_FMT_NV21:
> @@ -1297,6 +1310,22 @@ static int pxp_s_fmt_vid_out(struct file *file, void *priv,
>  	return 0;
>  }
>  
> +static u8 pxp_degrees_to_rot_mode(u32 degrees)
> +{
> +	switch (degrees) {
> +	case 0:
> +		return PXP_ROTATE_0;
> +	case 90:
> +		return PXP_ROTATE_90;
> +	case 180:
> +		return PXP_ROTATE_180;
> +	case 270:
> +		return PXP_ROTATE_270;
> +	default:
> +		return PXP_ROTATE_0;

You can combine the 0° and default cases.

> +	}
> +}
> +
>  static int pxp_s_ctrl(struct v4l2_ctrl *ctrl)
>  {
>  	struct pxp_ctx *ctx =
> @@ -1317,6 +1346,10 @@ static int pxp_s_ctrl(struct v4l2_ctrl *ctrl)
>  			ctx->mode &= ~MEM2MEM_VFLIP;
>  		break;
>  
> +	case V4L2_CID_ROTATE:
> +		ctx->rotation = pxp_degrees_to_rot_mode(ctrl->val);
> +		break;
> +
>  	case V4L2_CID_ALPHA_COMPONENT:
>  		ctx->alpha_component = ctrl->val;
>  		break;
> @@ -1524,6 +1557,7 @@ static int pxp_open(struct file *file)
>  	v4l2_ctrl_handler_init(hdl, 4);
>  	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
>  	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
> +	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
>  	v4l2_ctrl_new_std(hdl, &pxp_ctrl_ops, V4L2_CID_ALPHA_COMPONENT,
>  			  0, 255, 1, 255);
>  	if (hdl->error) {

regards
Philipp

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-08 12:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-07 12:57 [PATCH v3] media: imx-pxp: Add rotation support Fabio Estevam
2021-10-08 12:38 ` Philipp Zabel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).