media: verisilicon: Conditionally ignore native formats
authorBenjamin Gaignard <benjamin.gaignard@collabora.com>
Wed, 3 May 2023 08:34:38 +0000 (09:34 +0100)
committerMauro Carvalho Chehab <mchehab@kernel.org>
Fri, 9 Jun 2023 15:20:21 +0000 (16:20 +0100)
AV1 film grain feature requires to use the postprocessor to produce
valid frames. In such case the driver shouldn't propose native pixels
format but only post-processed pixels format.
Additionally if when setting a control a value could change capture
queue pixels formats it is needed to call hantro_reset_raw_fmt().

Signed-off-by: Benjamin Gaignard <benjamin.gaignard@collabora.com>
Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>
drivers/media/platform/verisilicon/hantro.h
drivers/media/platform/verisilicon/hantro_drv.c
drivers/media/platform/verisilicon/hantro_postproc.c
drivers/media/platform/verisilicon/hantro_v4l2.c
drivers/media/platform/verisilicon/hantro_v4l2.h

index a98cb40a8d3be2b83ccad2c3bc2f1adc6bdcb317..6523ffb7488124f821c6f6c52c707f36fd9e09a2 100644 (file)
@@ -231,6 +231,8 @@ struct hantro_dev {
  * @ctrl_handler:      Control handler used to register controls.
  * @jpeg_quality:      User-specified JPEG compression quality.
  * @bit_depth:         Bit depth of current frame
+ * @need_postproc:     Set to true if the bitstream features require to
+ *                     use the post-processor.
  *
  * @codec_ops:         Set of operations related to codec mode.
  * @postproc:          Post-processing context.
@@ -261,6 +263,7 @@ struct hantro_ctx {
 
        const struct hantro_codec_ops *codec_ops;
        struct hantro_postproc_ctx postproc;
+       bool need_postproc;
 
        /* Specific for particular codec modes. */
        union {
index dd41a6de1a0c3fb40c2dd2df17fa2fd55d8d3100..c0a368bacf8803d9f2dac8b541c8634d48a1ab03 100644 (file)
@@ -319,7 +319,7 @@ static int hantro_vp9_s_ctrl(struct v4l2_ctrl *ctrl)
                if (ctx->bit_depth == bit_depth)
                        return 0;
 
-               return hantro_reset_raw_fmt(ctx, bit_depth);
+               return hantro_reset_raw_fmt(ctx, bit_depth, HANTRO_AUTO_POSTPROC);
        }
        default:
                return -EINVAL;
@@ -343,7 +343,7 @@ static int hantro_hevc_s_ctrl(struct v4l2_ctrl *ctrl)
                if (ctx->bit_depth == bit_depth)
                        return 0;
 
-               return hantro_reset_raw_fmt(ctx, bit_depth);
+               return hantro_reset_raw_fmt(ctx, bit_depth, HANTRO_AUTO_POSTPROC);
        }
        default:
                return -EINVAL;
@@ -363,11 +363,17 @@ static int hantro_av1_s_ctrl(struct v4l2_ctrl *ctrl)
        case V4L2_CID_STATELESS_AV1_SEQUENCE:
        {
                int bit_depth = ctrl->p_new.p_av1_sequence->bit_depth;
+               bool need_postproc = HANTRO_AUTO_POSTPROC;
 
-               if (ctx->bit_depth == bit_depth)
+               if (ctrl->p_new.p_av1_sequence->flags
+                   & V4L2_AV1_SEQUENCE_FLAG_FILM_GRAIN_PARAMS_PRESENT)
+                       need_postproc = HANTRO_FORCE_POSTPROC;
+
+               if (ctx->bit_depth == bit_depth &&
+                   ctx->need_postproc == need_postproc)
                        return 0;
 
-               return hantro_reset_raw_fmt(ctx, bit_depth);
+               return hantro_reset_raw_fmt(ctx, bit_depth, need_postproc);
        }
        default:
                return -EINVAL;
index bb16af50719d42be18e806a156726d73935b445d..c977d64105b183fa48850283ee576e3df61bed63 100644 (file)
@@ -57,6 +57,10 @@ bool hantro_needs_postproc(const struct hantro_ctx *ctx,
 {
        if (ctx->is_encoder)
                return false;
+
+       if (ctx->need_postproc)
+               return true;
+
        return fmt->postprocessed;
 }
 
@@ -197,7 +201,7 @@ int hantro_postproc_alloc(struct hantro_ctx *ctx)
        unsigned int i, buf_size;
 
        /* this should always pick native format */
-       fmt = hantro_get_default_fmt(ctx, false, ctx->bit_depth);
+       fmt = hantro_get_default_fmt(ctx, false, ctx->bit_depth, HANTRO_AUTO_POSTPROC);
        if (!fmt)
                return -EINVAL;
        v4l2_fill_pixfmt_mp(&pix_mp, fmt->fourcc, ctx->src_fmt.width,
index 3367aa956ba3098120b62ad1469e9d3670ea84d1..e871c078dd59eea7c77a96bcc2f4bdc151a5940c 100644 (file)
 #define  HANTRO_DEFAULT_BIT_DEPTH 8
 
 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
-                             struct v4l2_pix_format_mplane *pix_mp);
+                             struct v4l2_pix_format_mplane *pix_mp,
+                             bool need_postproc);
 static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
                              struct v4l2_pix_format_mplane *pix_mp);
 
 static const struct hantro_fmt *
-hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts)
+hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts, bool need_postproc)
 {
        const struct hantro_fmt *formats;
 
+       if (need_postproc) {
+               *num_fmts = 0;
+               return NULL;
+       }
+
        if (ctx->is_encoder) {
                formats = ctx->dev->variant->enc_fmts;
                *num_fmts = ctx->dev->variant->num_enc_fmts;
@@ -108,7 +114,7 @@ hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
        const struct hantro_fmt *formats;
        unsigned int i, num_fmts;
 
-       formats = hantro_get_formats(ctx, &num_fmts);
+       formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
        for (i = 0; i < num_fmts; i++)
                if (formats[i].fourcc == fourcc)
                        return &formats[i];
@@ -121,18 +127,28 @@ hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
 }
 
 const struct hantro_fmt *
-hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream, int bit_depth)
+hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream,
+                      int bit_depth, bool need_postproc)
 {
        const struct hantro_fmt *formats;
        unsigned int i, num_fmts;
 
-       formats = hantro_get_formats(ctx, &num_fmts);
+       formats = hantro_get_formats(ctx, &num_fmts, need_postproc);
+       for (i = 0; i < num_fmts; i++) {
+               if (bitstream == (formats[i].codec_mode !=
+                                 HANTRO_MODE_NONE) &&
+                   hantro_check_depth_match(&formats[i], bit_depth))
+                       return &formats[i];
+       }
+
+       formats = hantro_get_postproc_formats(ctx, &num_fmts);
        for (i = 0; i < num_fmts; i++) {
                if (bitstream == (formats[i].codec_mode !=
                                  HANTRO_MODE_NONE) &&
                    hantro_check_depth_match(&formats[i], bit_depth))
                        return &formats[i];
        }
+
        return NULL;
 }
 
@@ -199,7 +215,7 @@ static int vidioc_enum_fmt(struct file *file, void *priv,
         */
        skip_mode_none = capture == ctx->is_encoder;
 
-       formats = hantro_get_formats(ctx, &num_fmts);
+       formats = hantro_get_formats(ctx, &num_fmts, HANTRO_AUTO_POSTPROC);
        for (i = 0; i < num_fmts; i++) {
                bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE;
                fmt = &formats[i];
@@ -294,7 +310,7 @@ static int hantro_try_fmt(const struct hantro_ctx *ctx,
 
        fmt = hantro_find_format(ctx, pix_mp->pixelformat);
        if (!fmt) {
-               fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH);
+               fmt = hantro_get_default_fmt(ctx, coded, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
                pix_mp->pixelformat = fmt->fourcc;
        }
 
@@ -383,7 +399,7 @@ hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
        const struct hantro_fmt *vpu_fmt;
        struct v4l2_pix_format_mplane fmt;
 
-       vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH);
+       vpu_fmt = hantro_get_default_fmt(ctx, true, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
        if (!vpu_fmt)
                return;
 
@@ -393,17 +409,17 @@ hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
        if (ctx->is_encoder)
                hantro_set_fmt_cap(ctx, &fmt);
        else
-               hantro_set_fmt_out(ctx, &fmt);
+               hantro_set_fmt_out(ctx, &fmt, HANTRO_AUTO_POSTPROC);
 }
 
 int
-hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth)
+hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc)
 {
        const struct hantro_fmt *raw_vpu_fmt;
        struct v4l2_pix_format_mplane raw_fmt, *encoded_fmt;
        int ret;
 
-       raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth);
+       raw_vpu_fmt = hantro_get_default_fmt(ctx, false, bit_depth, need_postproc);
        if (!raw_vpu_fmt)
                return -EINVAL;
 
@@ -418,12 +434,14 @@ hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth)
        raw_fmt.width = encoded_fmt->width;
        raw_fmt.height = encoded_fmt->height;
        if (ctx->is_encoder)
-               ret = hantro_set_fmt_out(ctx, &raw_fmt);
+               ret = hantro_set_fmt_out(ctx, &raw_fmt, need_postproc);
        else
                ret = hantro_set_fmt_cap(ctx, &raw_fmt);
 
-       if (!ret)
+       if (!ret) {
                ctx->bit_depth = bit_depth;
+               ctx->need_postproc = need_postproc;
+       }
 
        return ret;
 }
@@ -431,7 +449,7 @@ hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth)
 void hantro_reset_fmts(struct hantro_ctx *ctx)
 {
        hantro_reset_encoded_fmt(ctx);
-       hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH);
+       hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
 }
 
 static void
@@ -478,7 +496,8 @@ hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc)
 }
 
 static int hantro_set_fmt_out(struct hantro_ctx *ctx,
-                             struct v4l2_pix_format_mplane *pix_mp)
+                             struct v4l2_pix_format_mplane *pix_mp,
+                             bool need_postproc)
 {
        struct vb2_queue *vq;
        int ret;
@@ -531,7 +550,9 @@ static int hantro_set_fmt_out(struct hantro_ctx *ctx,
         * changes to the raw format.
         */
        if (!ctx->is_encoder)
-               hantro_reset_raw_fmt(ctx, hantro_get_format_depth(pix_mp->pixelformat));
+               hantro_reset_raw_fmt(ctx,
+                                    hantro_get_format_depth(pix_mp->pixelformat),
+                                    need_postproc);
 
        /* Colorimetry information are always propagated. */
        ctx->dst_fmt.colorspace = pix_mp->colorspace;
@@ -594,7 +615,7 @@ static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
         * changes to the raw format.
         */
        if (ctx->is_encoder)
-               hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH);
+               hantro_reset_raw_fmt(ctx, HANTRO_DEFAULT_BIT_DEPTH, HANTRO_AUTO_POSTPROC);
 
        /* Colorimetry information are always propagated. */
        ctx->src_fmt.colorspace = pix_mp->colorspace;
@@ -614,7 +635,7 @@ static int hantro_set_fmt_cap(struct hantro_ctx *ctx,
 static int
 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
 {
-       return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp);
+       return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp, HANTRO_AUTO_POSTPROC);
 }
 
 static int
index 9ea2fef57dcd99ede930428339bd373d1aa27062..fca7e3a690e55f7a1232d4e97d695298bdf0de54 100644 (file)
 
 #include "hantro.h"
 
+#define HANTRO_FORCE_POSTPROC  true
+#define HANTRO_AUTO_POSTPROC   false
+
 extern const struct v4l2_ioctl_ops hantro_ioctl_ops;
 extern const struct vb2_ops hantro_queue_ops;
 
-int hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth);
+int hantro_reset_raw_fmt(struct hantro_ctx *ctx, int bit_depth, bool need_postproc);
 void hantro_reset_fmts(struct hantro_ctx *ctx);
 int hantro_get_format_depth(u32 fourcc);
 const struct hantro_fmt *
-hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream, int bit_depth);
+hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream,
+                      int bit_depth, bool need_postproc);
 
 #endif /* HANTRO_V4L2_H_ */