media: hantro: Use post processor scaling capacities
authorBenjamin Gaignard <benjamin.gaignard@collabora.com>
Mon, 4 Apr 2022 16:06:40 +0000 (18:06 +0200)
committerMauro Carvalho Chehab <mchehab@kernel.org>
Fri, 13 May 2022 09:02:22 +0000 (11:02 +0200)
Hantro G2 post processor is able to down scale decoded frames
by a factor of 2, 4 or 8.
Add enum_framesizes() ops to postproc_ops structure to enumerate the
possible output sizes for a given input resolution.
For G2 post-processor use fsize->index (from 0 to 3) as power of 2
divisor. As described in v4l2 documentation return -EINVAL when scaling
down isn't possible.

fluster scores:
77/147 for HEVC
143/303 for VP9

Signed-off-by: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
drivers/staging/media/hantro/hantro.h
drivers/staging/media/hantro/hantro_g2_regs.h
drivers/staging/media/hantro/hantro_hw.h
drivers/staging/media/hantro/hantro_postproc.c
drivers/staging/media/hantro/hantro_v4l2.c

index 357f83b..26308bb 100644 (file)
@@ -475,5 +475,7 @@ void hantro_postproc_disable(struct hantro_ctx *ctx);
 void hantro_postproc_enable(struct hantro_ctx *ctx);
 void hantro_postproc_free(struct hantro_ctx *ctx);
 int hantro_postproc_alloc(struct hantro_ctx *ctx);
+int hanto_postproc_enum_framesizes(struct hantro_ctx *ctx,
+                                  struct v4l2_frmsizeenum *fsize);
 
 #endif /* HANTRO_H_ */
index b7c6f98..877d663 100644 (file)
 #define g2_buswidth            G2_DEC_REG(58, 8,  0x7)
 #define g2_max_burst           G2_DEC_REG(58, 0,  0xff)
 
+#define g2_down_scale_e                G2_DEC_REG(184, 7, 0x1)
+#define g2_down_scale_y                G2_DEC_REG(184, 2, 0x3)
+#define g2_down_scale_x                G2_DEC_REG(184, 0, 0x3)
+
 #define G2_REG_CONFIG                          G2_SWREG(58)
 #define G2_REG_CONFIG_DEC_CLK_GATE_E           BIT(16)
 #define G2_REG_CONFIG_DEC_CLK_GATE_IDLE_E      BIT(17)
 #define G2_TILE_FILTER_ADDR            (G2_SWREG(179))
 #define G2_TILE_SAO_ADDR               (G2_SWREG(181))
 #define G2_TILE_BSD_ADDR               (G2_SWREG(183))
+#define G2_DS_DST                      (G2_SWREG(186))
+#define G2_DS_DST_CHR                  (G2_SWREG(188))
 
 #define g2_strm_buffer_len     G2_DEC_REG(258, 0, 0xffffffff)
 #define g2_strm_start_offset   G2_DEC_REG(259, 0, 0xffffffff)
index c5dc771..0e5cafd 100644 (file)
@@ -245,12 +245,16 @@ struct hantro_postproc_ctx {
 /**
  * struct hantro_postproc_ops - post-processor operations
  *
- * @enable:    Enable the post-processor block. Optional.
- * @disable:   Disable the post-processor block. Optional.
+ * @enable:            Enable the post-processor block. Optional.
+ * @disable:           Disable the post-processor block. Optional.
+ * @enum_framesizes:   Enumerate possible scaled output formats.
+ *                     Returns zero if OK, a negative value in error cases.
+ *                     Optional.
  */
 struct hantro_postproc_ops {
        void (*enable)(struct hantro_ctx *ctx);
        void (*disable)(struct hantro_ctx *ctx);
+       int (*enum_framesizes)(struct hantro_ctx *ctx, struct v4l2_frmsizeenum *fsize);
 };
 
 /**
index 248abe5..ab168c1 100644 (file)
@@ -100,21 +100,58 @@ static void hantro_postproc_g1_enable(struct hantro_ctx *ctx)
        HANTRO_PP_REG_WRITE(vpu, display_width, ctx->dst_fmt.width);
 }
 
+static int down_scale_factor(struct hantro_ctx *ctx)
+{
+       if (ctx->src_fmt.width == ctx->dst_fmt.width)
+               return 0;
+
+       return DIV_ROUND_CLOSEST(ctx->src_fmt.width, ctx->dst_fmt.width);
+}
+
 static void hantro_postproc_g2_enable(struct hantro_ctx *ctx)
 {
        struct hantro_dev *vpu = ctx->dev;
        struct vb2_v4l2_buffer *dst_buf;
        size_t chroma_offset = ctx->dst_fmt.width * ctx->dst_fmt.height;
+       int down_scale = down_scale_factor(ctx);
        dma_addr_t dst_dma;
 
        dst_buf = hantro_get_dst_buf(ctx);
        dst_dma = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
 
-       hantro_write_addr(vpu, G2_RS_OUT_LUMA_ADDR, dst_dma);
-       hantro_write_addr(vpu, G2_RS_OUT_CHROMA_ADDR, dst_dma + chroma_offset);
+       if (down_scale) {
+               hantro_reg_write(vpu, &g2_down_scale_e, 1);
+               hantro_reg_write(vpu, &g2_down_scale_y, down_scale >> 2);
+               hantro_reg_write(vpu, &g2_down_scale_x, down_scale >> 2);
+               hantro_write_addr(vpu, G2_DS_DST, dst_dma);
+               hantro_write_addr(vpu, G2_DS_DST_CHR, dst_dma + (chroma_offset >> down_scale));
+       } else {
+               hantro_write_addr(vpu, G2_RS_OUT_LUMA_ADDR, dst_dma);
+               hantro_write_addr(vpu, G2_RS_OUT_CHROMA_ADDR, dst_dma + chroma_offset);
+       }
        hantro_reg_write(vpu, &g2_out_rs_e, 1);
 }
 
+static int hantro_postproc_g2_enum_framesizes(struct hantro_ctx *ctx,
+                                             struct v4l2_frmsizeenum *fsize)
+{
+       /**
+        * G2 scaler can scale down by 0, 2, 4 or 8
+        * use fsize->index has power of 2 diviser
+        **/
+       if (fsize->index > 3)
+               return -EINVAL;
+
+       if (!ctx->src_fmt.width || !ctx->src_fmt.height)
+               return -EINVAL;
+
+       fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
+       fsize->discrete.width = ctx->src_fmt.width >> fsize->index;
+       fsize->discrete.height = ctx->src_fmt.height >> fsize->index;
+
+       return 0;
+}
+
 void hantro_postproc_free(struct hantro_ctx *ctx)
 {
        struct hantro_dev *vpu = ctx->dev;
@@ -197,6 +234,17 @@ void hantro_postproc_enable(struct hantro_ctx *ctx)
                vpu->variant->postproc_ops->enable(ctx);
 }
 
+int hanto_postproc_enum_framesizes(struct hantro_ctx *ctx,
+                                  struct v4l2_frmsizeenum *fsize)
+{
+       struct hantro_dev *vpu = ctx->dev;
+
+       if (vpu->variant->postproc_ops && vpu->variant->postproc_ops->enum_framesizes)
+               return vpu->variant->postproc_ops->enum_framesizes(ctx, fsize);
+
+       return -EINVAL;
+}
+
 const struct hantro_postproc_ops hantro_g1_postproc_ops = {
        .enable = hantro_postproc_g1_enable,
        .disable = hantro_postproc_g1_disable,
@@ -205,4 +253,5 @@ const struct hantro_postproc_ops hantro_g1_postproc_ops = {
 const struct hantro_postproc_ops hantro_g2_postproc_ops = {
        .enable = hantro_postproc_g2_enable,
        .disable = hantro_postproc_g2_disable,
+       .enum_framesizes = hantro_postproc_g2_enum_framesizes,
 };
index 71a6279..ed45886 100644 (file)
@@ -116,12 +116,6 @@ static int vidioc_enum_framesizes(struct file *file, void *priv,
        struct hantro_ctx *ctx = fh_to_ctx(priv);
        const struct hantro_fmt *fmt;
 
-       if (fsize->index != 0) {
-               vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
-                         fsize->index);
-               return -EINVAL;
-       }
-
        fmt = hantro_find_format(ctx, fsize->pixel_format);
        if (!fmt) {
                vpu_debug(0, "unsupported bitstream format (%08x)\n",
@@ -129,9 +123,14 @@ static int vidioc_enum_framesizes(struct file *file, void *priv,
                return -EINVAL;
        }
 
-       /* This only makes sense for coded formats */
-       if (fmt->codec_mode == HANTRO_MODE_NONE)
+       /* For non-coded formats check if postprocessing scaling is possible */
+       if (fmt->codec_mode == HANTRO_MODE_NONE && hantro_needs_postproc(ctx, fmt)) {
+               return hanto_postproc_enum_framesizes(ctx, fsize);
+       } else if (fsize->index != 0) {
+               vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
+                         fsize->index);
                return -EINVAL;
+       }
 
        fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
        fsize->stepwise = fmt->frmsize;