From 90ad47170414e19ed6c6dcc3f3c9a68fbc7ad175 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.org>
Date: Wed, 20 Mar 2019 10:06:51 +0000
Subject: [PATCH] staging: bcm2835-codec: Refactor default resolution
 code

The default resolution code was different for each role
as compressed formats need to pass bytesperline as 0 and
set up customised buffer sizes.
This is common setup, therefore amend get_sizeimage and
get_bytesperline to do the correct thing whether compressed
or uncompressed.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
---
 .../bcm2835-codec/bcm2835-v4l2-codec.c        | 103 +++++++-----------
 1 file changed, 40 insertions(+), 63 deletions(-)

--- a/drivers/staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c
+++ b/drivers/staging/vc04_services/bcm2835-codec/bcm2835-v4l2-codec.c
@@ -578,10 +578,17 @@ static void job_abort(void *priv)
 	ctx->aborting = 1;
 }
 
-static inline unsigned int get_sizeimage(int bpl, int height,
+static inline unsigned int get_sizeimage(int bpl, int width, int height,
 					 struct bcm2835_codec_fmt *fmt)
 {
-	return (bpl * height * fmt->size_multiplier_x2) >> 1;
+	if (fmt->flags & V4L2_FMT_FLAG_COMPRESSED) {
+		if (width * height > 1280 * 720)
+			return DEF_COMP_BUF_SIZE_GREATER_720P;
+		else
+			return DEF_COMP_BUF_SIZE_720P_OR_LESS;
+	} else {
+		return (bpl * height * fmt->size_multiplier_x2) >> 1;
+	}
 }
 
 static inline unsigned int get_bytesperline(int width,
@@ -1032,22 +1039,13 @@ static int vidioc_try_fmt(struct v4l2_fo
 		 * some of the pixels are active.
 		 */
 		f->fmt.pix.height = ALIGN(f->fmt.pix.height, 16);
-
-		f->fmt.pix.bytesperline = get_bytesperline(f->fmt.pix.width,
-							   fmt);
-		f->fmt.pix.sizeimage = get_sizeimage(f->fmt.pix.bytesperline,
-						     f->fmt.pix.height,
-						     fmt);
-	} else {
-		u32 min_size = f->fmt.pix.width > 1280 ||
-			       f->fmt.pix.height > 720 ?
-			       DEF_COMP_BUF_SIZE_GREATER_720P :
-			       DEF_COMP_BUF_SIZE_720P_OR_LESS;
-
-		f->fmt.pix.bytesperline = 0;
-		if (f->fmt.pix.sizeimage < min_size)
-			f->fmt.pix.sizeimage = min_size;
 	}
+	f->fmt.pix.bytesperline = get_bytesperline(f->fmt.pix.width,
+						   fmt);
+	f->fmt.pix.sizeimage = get_sizeimage(f->fmt.pix.bytesperline,
+					     f->fmt.pix.width,
+					     f->fmt.pix.height,
+					     fmt);
 
 	f->fmt.pix.field = V4L2_FIELD_NONE;
 
@@ -1159,6 +1157,7 @@ static int vidioc_s_fmt(struct bcm2835_c
 		q_data_dst->bytesperline =
 			get_bytesperline(f->fmt.pix.width, q_data_dst->fmt);
 		q_data_dst->sizeimage = get_sizeimage(q_data_dst->bytesperline,
+						      q_data_dst->crop_width,
 						      q_data_dst->height,
 						      q_data_dst->fmt);
 		update_capture_port = true;
@@ -2218,52 +2217,30 @@ static int bcm2835_codec_open(struct fil
 
 	ctx->q_data[V4L2_M2M_SRC].fmt = get_default_format(dev, false);
 	ctx->q_data[V4L2_M2M_DST].fmt = get_default_format(dev, true);
-	switch (dev->role) {
-	case DECODE:
-		/*
-		 * Input width and height are irrelevant as they will be defined
-		 * by the bitstream not the format. Required by V4L2 though.
-		 */
-		ctx->q_data[V4L2_M2M_SRC].crop_width = DEFAULT_WIDTH;
-		ctx->q_data[V4L2_M2M_SRC].crop_height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_SRC].height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_SRC].bytesperline = 0;
-		ctx->q_data[V4L2_M2M_SRC].sizeimage =
-						DEF_COMP_BUF_SIZE_720P_OR_LESS;
-
-		ctx->q_data[V4L2_M2M_DST].crop_width = DEFAULT_WIDTH;
-		ctx->q_data[V4L2_M2M_DST].crop_height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_DST].height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_DST].bytesperline =
-				get_bytesperline(DEFAULT_WIDTH,
-						 ctx->q_data[V4L2_M2M_DST].fmt);
-		ctx->q_data[V4L2_M2M_DST].sizeimage =
-			get_sizeimage(ctx->q_data[V4L2_M2M_DST].bytesperline,
-				      ctx->q_data[V4L2_M2M_DST].height,
-				      ctx->q_data[V4L2_M2M_DST].fmt);
-		break;
-	case ENCODE:
-		ctx->q_data[V4L2_M2M_SRC].crop_width = DEFAULT_WIDTH;
-		ctx->q_data[V4L2_M2M_SRC].crop_height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_SRC].height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_SRC].bytesperline =
-				get_bytesperline(DEFAULT_WIDTH,
-						 ctx->q_data[V4L2_M2M_SRC].fmt);
-		ctx->q_data[V4L2_M2M_SRC].sizeimage =
-			get_sizeimage(ctx->q_data[V4L2_M2M_SRC].bytesperline,
-				      ctx->q_data[V4L2_M2M_SRC].height,
-				      ctx->q_data[V4L2_M2M_SRC].fmt);
-
-		ctx->q_data[V4L2_M2M_DST].crop_width = DEFAULT_WIDTH;
-		ctx->q_data[V4L2_M2M_DST].crop_height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_DST].bytesperline = 0;
-		ctx->q_data[V4L2_M2M_DST].height = DEFAULT_HEIGHT;
-		ctx->q_data[V4L2_M2M_DST].sizeimage =
-						DEF_COMP_BUF_SIZE_720P_OR_LESS;
-		break;
-	case ISP:
-		break;
-	}
+
+	ctx->q_data[V4L2_M2M_SRC].crop_width = DEFAULT_WIDTH;
+	ctx->q_data[V4L2_M2M_SRC].crop_height = DEFAULT_HEIGHT;
+	ctx->q_data[V4L2_M2M_SRC].height = DEFAULT_HEIGHT;
+	ctx->q_data[V4L2_M2M_SRC].bytesperline =
+			get_bytesperline(DEFAULT_WIDTH,
+					 ctx->q_data[V4L2_M2M_SRC].fmt);
+	ctx->q_data[V4L2_M2M_SRC].sizeimage =
+		get_sizeimage(ctx->q_data[V4L2_M2M_SRC].bytesperline,
+			      ctx->q_data[V4L2_M2M_SRC].crop_width,
+			      ctx->q_data[V4L2_M2M_SRC].height,
+			      ctx->q_data[V4L2_M2M_SRC].fmt);
+
+	ctx->q_data[V4L2_M2M_DST].crop_width = DEFAULT_WIDTH;
+	ctx->q_data[V4L2_M2M_DST].crop_height = DEFAULT_HEIGHT;
+	ctx->q_data[V4L2_M2M_DST].height = DEFAULT_HEIGHT;
+	ctx->q_data[V4L2_M2M_DST].bytesperline =
+			get_bytesperline(DEFAULT_WIDTH,
+					 ctx->q_data[V4L2_M2M_DST].fmt);
+	ctx->q_data[V4L2_M2M_DST].sizeimage =
+		get_sizeimage(ctx->q_data[V4L2_M2M_DST].bytesperline,
+			      ctx->q_data[V4L2_M2M_DST].crop_width,
+			      ctx->q_data[V4L2_M2M_DST].height,
+			      ctx->q_data[V4L2_M2M_DST].fmt);
 
 	ctx->colorspace = V4L2_COLORSPACE_REC709;
 	ctx->bitrate = 10 * 1000 * 1000;
