Skip to contentSkip to conversation

H.264 stateless encoder RFC 0/6

RFC In review Andrzej Pietrasiewicz 4 participants 14 messages Nov 16, 2023 – Nov 18, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover H.264 stateless encoder RFC 0/6

Dear All,

This series adds uAPI for stateless H.264 encoding and an accompanying driver using it.

It has been tested on an stm32mp25 and there exists a gstreamer user:

https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5676

example pipeline:

gst-launch-1.0 videotestsrc num-buffers=30 ! video/x-raw, format=YUY2 ! v4l2slh264enc ! filesink location=test.h264

Rebased onto v6.6 with:

- some patches from ST to actually run the hardware
- my previous VP8 statless encoding series
- VP8 support for H1 from Hugues Fruchet

In particular, this series depends on the latter, which can be found here:

https://patchwork.linuxtv.org/project/linux-media/list/?series=11358

Here's a branch which contains everything needed to actually run:

https://gitlab.collabora.com/linux/for-upstream/-/tree/h264-enc-rfc-6.6

I kindly ask for comments.

Regards,

Andrzej Pietrasiewicz (6):
media: verisilicon Correct a typo in
H1_REG_ENC_CTRL2_DEBLOCKING_FILTER_MODE
media: verisilicon: Correct a typo in H1_REG_MAD_CTRL_MAD_THRESHOLD
media: verisilicon: Improve constant's name
media: verisilicon: Update H1 register definitions
media: uapi: Add H.264 stateless encoding uAPI
media: verisilicon: Add H.264 stateless encoder

 drivers/media/platform/verisilicon/Makefile   |   1 +
 drivers/media/platform/verisilicon/hantro.h   |   3 +
 .../media/platform/verisilicon/hantro_drv.c   |  10 +
 .../platform/verisilicon/hantro_h1_h264_enc.c | 493 +++++++++++
 .../platform/verisilicon/hantro_h1_regs.h     |  20 +-
 .../platform/verisilicon/hantro_h1_vp8_enc.c  |   2 +-
 .../media/platform/verisilicon/hantro_h264.c  | 777 ++++++++++++++++++
 .../media/platform/verisilicon/hantro_hw.h    |  23 +
 .../platform/verisilicon/stm32mp25_venc_hw.c  |  22 +-
 drivers/media/v4l2-core/v4l2-ctrls-core.c     |  54 ++
 drivers/media/v4l2-core/v4l2-ctrls-defs.c     |   9 +
 include/uapi/linux/v4l2-controls.h            |  85 ++
 include/uapi/linux/videodev2.h                |   2 +
 13 files changed, 1496 insertions(+), 5 deletions(-)
 create mode 100644 drivers/media/platform/verisilicon/hantro_h1_h264_enc.c
Signature · 1 line
2.25.1
#1 lore raw

RFC 1/6 media: verisilicon Correct a typo in H1_REG_ENC_CTRL2_DEBLOCKING_FILTER_MODE

It's a FILTER and not FILETER.

---
 drivers/media/platform/verisilicon/hantro_h1_regs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/verisilicon/hantro_h1_regs.h +1 -1
index 5062ee1c87a2..57e89bb975ae 100644
@@ -65,7 +65,7 @@
 #define    H1_REG_ENC_CTRL1_INTRA_PRED_MODE(x)		((x) << 16)
 #define    H1_REG_ENC_CTRL1_FRAME_NUM(x)		((x))
 #define H1_REG_ENC_CTRL2				0x048
-#define    H1_REG_ENC_CTRL2_DEBLOCKING_FILETER_MODE(x)	((x) << 30)
+#define    H1_REG_ENC_CTRL2_DEBLOCKING_FILTER_MODE(x)	((x) << 30)
 #define    H1_REG_ENC_CTRL2_H264_SLICE_SIZE(x)		((x) << 23)
 #define    H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV	BIT(22)
 #define    H1_REG_ENC_CTRL2_TRANS8X8_MODE_EN		BIT(21)
Signature · 1 line
2.25.1
#3 lore raw

RFC 2/6 media: verisilicon: Correct a typo in H1_REG_MAD_CTRL_MAD_THRESHOLD

In reply to Andrzej Pietrasiewicz

It's a THRESHOLD and not a THREDHOLD.

---
 drivers/media/platform/verisilicon/hantro_h1_regs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/verisilicon/hantro_h1_regs.h +1 -1
index 57e89bb975ae..7752d1291c0e 100644
@@ -92,7 +92,7 @@
 #define H1_REG_STR_BUF_LIMIT				0x060
 #define H1_REG_MAD_CTRL					0x064
 #define    H1_REG_MAD_CTRL_QP_ADJUST(x)			((x) << 28)
-#define    H1_REG_MAD_CTRL_MAD_THREDHOLD(x)		((x) << 22)
+#define    H1_REG_MAD_CTRL_MAD_THRESHOLD(x)		((x) << 22)
 #define    H1_REG_MAD_CTRL_QP_SUM_DIV2(x)		((x))
 #define H1_REG_ADDR_VP8_PROB_CNT			0x068
 #define H1_REG_QP_VAL					0x06c
Signature · 1 line
2.25.1
#5 lore raw

RFC 3/6 media: verisilicon: Improve constant's name

In reply to Andrzej Pietrasiewicz

For VP8 BIT(18) of this register is for enabling the boolean encoder.

---
 drivers/media/platform/verisilicon/hantro_h1_regs.h    | 2 +-
 drivers/media/platform/verisilicon/hantro_h1_vp8_enc.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_h1_regs.h +1 -1
index 7752d1291c0e..c1c66c934a24 100644
@@ -70,7 +70,7 @@
 #define    H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV	BIT(22)
 #define    H1_REG_ENC_CTRL2_TRANS8X8_MODE_EN		BIT(21)
 #define    H1_REG_ENC_CTRL2_CABAC_INIT_IDC(x)		((x) << 19)
-#define    H1_REG_ENC_CTRL2_ENTROPY_CODING_MODE		BIT(18)
+#define    H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE		BIT(18)
 #define    H1_REG_ENC_CTRL2_H264_INTER4X4_MODE		BIT(17)
 #define    H1_REG_ENC_CTRL2_H264_STREAM_MODE		BIT(16)
 #define    H1_REG_ENC_CTRL2_INTRA16X16_MODE(x)		((x))
drivers/media/platform/verisilicon/hantro_h1_vp8_enc.c +1 -1
index 05aa0dd9c09c..08c5079fbfd0 100644
@@ -1226,7 +1226,7 @@ static void hantro_h1_vp8_enc_set_params(struct hantro_dev *vpu, struct hantro_c
 	reg = 0;
 	if (mb_width * mb_height > MAX_MB_COUNT_TO_DISABLE_QUARTER_PIXEL_MV)
 		reg = H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV;
-	reg |= H1_REG_ENC_CTRL2_ENTROPY_CODING_MODE;
+	reg |= H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE;
 
 	inter_favor = 128 - ctx->vp8_enc.prob_intra;
 	if (inter_favor >= 0)
Signature · 1 line
2.25.1
#6 lore raw

Re: [RFC 3/6] media: verisilicon: Improve constant's name

Quoted 2 lines · Andrzej Pietrasiewicz

For VP8 BIT(18) of this register is for enabling the boolean encoder.

Yes, but for H.264 it selects the entropy coding mode, 0 for CAVLC and 1 for CABAC. You even add it back in the last patch. I'd do it here, so you disambiguate the definition within one patch.

ChenYu

Quoted 36 lines
---
 drivers/media/platform/verisilicon/hantro_h1_regs.h    | 2 +-
 drivers/media/platform/verisilicon/hantro_h1_vp8_enc.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_h1_regs.h +1 -1
index 7752d1291c0e..c1c66c934a24 100644
@@ -70,7 +70,7 @@
 #define    H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV      BIT(22)
 #define    H1_REG_ENC_CTRL2_TRANS8X8_MODE_EN           BIT(21)
 #define    H1_REG_ENC_CTRL2_CABAC_INIT_IDC(x)          ((x) << 19)
-#define    H1_REG_ENC_CTRL2_ENTROPY_CODING_MODE                BIT(18)
+#define    H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE         BIT(18)
 #define    H1_REG_ENC_CTRL2_H264_INTER4X4_MODE         BIT(17)
 #define    H1_REG_ENC_CTRL2_H264_STREAM_MODE           BIT(16)
 #define    H1_REG_ENC_CTRL2_INTRA16X16_MODE(x)         ((x))
drivers/media/platform/verisilicon/hantro_h1_vp8_enc.c +1 -2
index 05aa0dd9c09c..08c5079fbfd0 100644
@@ -1226,7 +1226,7 @@ static void hantro_h1_vp8_enc_set_params(struct hantro_dev *vpu, struct hantro_c
        reg = 0;
        if (mb_width * mb_height > MAX_MB_COUNT_TO_DISABLE_QUARTER_PIXEL_MV)
                reg = H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV;
-       reg |= H1_REG_ENC_CTRL2_ENTROPY_CODING_MODE;
+       reg |= H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE;
 
        inter_favor = 128 - ctx->vp8_enc.prob_intra;
        if (inter_favor >= 0)
--

2.25.1

#7 lore raw

Re: [RFC 3/6] media: verisilicon: Improve constant's name

Hi,

Thanks for looking at it.

W dniu 17.11.2023 o 07:11, Chen-Yu Tsai pisze:

Quoted 9 lines
Quoted 2 lines · Andrzej Pietrasiewicz

For VP8 BIT(18) of this register is for enabling the boolean encoder.

Yes, but for H.264 it selects the entropy coding mode, 0 for CAVLC and 1 for CABAC. You even add it back in the last patch. I'd do it here, so you disambiguate the definition within one patch.

The rationale behind doing what I did is this:

At this moment the H1 H.264 encoder is non-existent in the kernel, so why would we keep H.264-related definitions? I re-introduce it when the encoder appears.

That said, other H.264-specific constants do exist at this moment anyway. So I'm fine with either approach.

Andrzej

Quoted 42 lines

ChenYu

Quoted 33 lines
---
  drivers/media/platform/verisilicon/hantro_h1_regs.h    | 2 +-
  drivers/media/platform/verisilicon/hantro_h1_vp8_enc.c | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/verisilicon/hantro_h1_regs.h +1 -1
index 7752d1291c0e..c1c66c934a24 100644
@@ -70,7 +70,7 @@
  #define    H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV      BIT(22)
  #define    H1_REG_ENC_CTRL2_TRANS8X8_MODE_EN           BIT(21)
  #define    H1_REG_ENC_CTRL2_CABAC_INIT_IDC(x)          ((x) << 19)
-#define    H1_REG_ENC_CTRL2_ENTROPY_CODING_MODE                BIT(18)
+#define    H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE         BIT(18)
  #define    H1_REG_ENC_CTRL2_H264_INTER4X4_MODE         BIT(17)
  #define    H1_REG_ENC_CTRL2_H264_STREAM_MODE           BIT(16)
  #define    H1_REG_ENC_CTRL2_INTRA16X16_MODE(x)         ((x))
drivers/media/platform/verisilicon/hantro_h1_vp8_enc.c +1 -2
index 05aa0dd9c09c..08c5079fbfd0 100644
@@ -1226,7 +1226,7 @@ static void hantro_h1_vp8_enc_set_params(struct hantro_dev *vpu, struct hantro_c
         reg = 0;
         if (mb_width * mb_height > MAX_MB_COUNT_TO_DISABLE_QUARTER_PIXEL_MV)
                 reg = H1_REG_ENC_CTRL2_DISABLE_QUARTER_PIXMV;
-       reg |= H1_REG_ENC_CTRL2_ENTROPY_CODING_MODE;
+       reg |= H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE;
 
         inter_favor = 128 - ctx->vp8_enc.prob_intra;
         if (inter_favor >= 0)
--
Quoted 3 lines

2.25.1

_______________________________________________
Kernel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

#8 lore raw

RFC 4/6 media: verisilicon: Update H1 register definitions

In reply to Andrzej Pietrasiewicz

Add definition of register at offset 0x00c.

---
 drivers/media/platform/verisilicon/hantro_h1_regs.h | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/media/platform/verisilicon/hantro_h1_regs.h +9 -0
index c1c66c934a24..efb46da23eab 100644
@@ -23,6 +23,15 @@
 #define     H1_REG_AXI_CTRL_INPUT_SWAP32		BIT(2)
 #define     H1_REG_AXI_CTRL_OUTPUT_SWAP8		BIT(1)
 #define     H1_REG_AXI_CTRL_INPUT_SWAP8			BIT(0)
+#define H1_REG_DEVICE_CTRL				0x00c
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP8	BIT(27)
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP16	BIT(26)
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP32	BIT(25)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP8		BIT(24)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP16		BIT(23)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP32		BIT(22)
+#define     H1_REG_DEVICE_CTRL_INPUT_READ_1MB		BIT(21)
+#define     H1_REG_DEVICE_CTRL_AXI_DUAL_CHANNEL		BIT(20)
 #define H1_REG_ADDR_OUTPUT_STREAM			0x014
 #define H1_REG_ADDR_OUTPUT_CTRL				0x018
 #define H1_REG_ADDR_REF_LUMA				0x01c
Signature · 1 line
2.25.1
#9 lore raw

Re: [RFC 4/6] media: verisilicon: Update H1 register definitions

Quoted 15 lines · Andrzej Pietrasiewicz

Add definition of register at offset 0x00c.

---
 drivers/media/platform/verisilicon/hantro_h1_regs.h | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/media/platform/verisilicon/hantro_h1_regs.h +0 -0
index c1c66c934a24..efb46da23eab 100644
@@ -23,6 +23,15 @@
 #define     H1_REG_AXI_CTRL_INPUT_SWAP32               BIT(2)
 #define     H1_REG_AXI_CTRL_OUTPUT_SWAP8               BIT(1)

drivers/media/platform/verisilicon/hantro_h1_regs.h line 34

Quoted 10 lines
drivers/media/platform/verisilicon/hantro_h1_regs.h +9 -0
 #define     H1_REG_AXI_CTRL_INPUT_SWAP8                        BIT(0)
+#define H1_REG_DEVICE_CTRL                             0x00c
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP8      BIT(27)
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP16     BIT(26)
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP32     BIT(25)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP8         BIT(24)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP16                BIT(23)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP32                BIT(22)
+#define     H1_REG_DEVICE_CTRL_INPUT_READ_1MB          BIT(21)
+#define     H1_REG_DEVICE_CTRL_AXI_DUAL_CHANNEL                BIT(20)

According to the i.MX8M Mini reference manual, this bit is a "disable" control, i.e. setting this bit disables dual channel AXI. I think the macro should explicitly state this in the naming, so something like H1_REG_DEVICE_CTRL_DISABLE_AXI_DUAL_CH(ANNEL).

Other bits matches the reference manual.

Quoted 7 lines
drivers/media/platform/verisilicon/hantro_h1_regs.h +0 -1
 #define H1_REG_ADDR_OUTPUT_STREAM                      0x014
 #define H1_REG_ADDR_OUTPUT_CTRL                                0x018
 #define H1_REG_ADDR_REF_LUMA                           0x01c
--

2.25.1

#10 lore raw

Re: [RFC 4/6] media: verisilicon: Update H1 register definitions

Hi,

W dniu 17.11.2023 o 07:56, Chen-Yu Tsai pisze:

Quoted 24 lines
Quoted 22 lines · Andrzej Pietrasiewicz

Add definition of register at offset 0x00c.

---
  drivers/media/platform/verisilicon/hantro_h1_regs.h | 9 +++++++++
  1 file changed, 9 insertions(+)
drivers/media/platform/verisilicon/hantro_h1_regs.h +6 -0
index c1c66c934a24..efb46da23eab 100644
@@ -23,6 +23,15 @@
  #define     H1_REG_AXI_CTRL_INPUT_SWAP32               BIT(2)
  #define     H1_REG_AXI_CTRL_OUTPUT_SWAP8               BIT(1)
  #define     H1_REG_AXI_CTRL_INPUT_SWAP8                        BIT(0)
+#define H1_REG_DEVICE_CTRL                             0x00c
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP8      BIT(27)
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP16     BIT(26)
+#define     H1_REG_DEVICE_CTRL_SCALE_OUTPUT_SWAP32     BIT(25)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP8         BIT(24)
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP16                BIT(23)
Quoted 8 lines

drivers/media/platform/verisilicon/hantro_h1_regs.h line 34

Quoted 3 lines
drivers/media/platform/verisilicon/hantro_h1_regs.h +3 -0
+#define     H1_REG_DEVICE_CTRL_MV_OUTPUT_SWAP32                BIT(22)
+#define     H1_REG_DEVICE_CTRL_INPUT_READ_1MB          BIT(21)
+#define     H1_REG_DEVICE_CTRL_AXI_DUAL_CHANNEL                BIT(20)

According to the i.MX8M Mini reference manual, this bit is a "disable" control, i.e. setting this bit disables dual channel AXI. I think the macro should explicitly state this in the naming, so something like H1_REG_DEVICE_CTRL_DISABLE_AXI_DUAL_CH(ANNEL).

Good point, thanks.

Andrzej

Quoted 13 lines

Other bits matches the reference manual.

Quoted 7 lines
drivers/media/platform/verisilicon/hantro_h1_regs.h +0 -1
  #define H1_REG_ADDR_OUTPUT_STREAM                      0x014
  #define H1_REG_ADDR_OUTPUT_CTRL                                0x018
  #define H1_REG_ADDR_REF_LUMA                           0x01c
--

2.25.1

_______________________________________________
Kernel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

#11 lore raw

RFC 5/6 media: uapi: Add H.264 stateless encoding uAPI

In reply to Andrzej Pietrasiewicz

Ported from:

https://github.com/bootlin/linux/tree/hantro/h264-encoding-v5.11

---
 drivers/media/v4l2-core/v4l2-ctrls-core.c | 54 ++++++++++++++
 drivers/media/v4l2-core/v4l2-ctrls-defs.c |  9 +++
 include/uapi/linux/v4l2-controls.h        | 85 +++++++++++++++++++++++
 include/uapi/linux/videodev2.h            |  2 +
 4 files changed, 150 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +54 -0
index c7799ceb3d6d..04b7e64312be 100644
@@ -790,6 +790,48 @@ static int validate_av1_sequence(struct v4l2_ctrl_av1_sequence *s)
 	return 0;
 }
 
+static int
+validate_h264_encode_params(struct v4l2_ctrl_h264_encode_params *encode_params)
+{
+	/* Make sure we're not passed invalid flags. */
+	if (encode_params->flags & ~(V4L2_H264_ENCODE_FLAG_ENTROPY_CABAC |
+		V4L2_H264_ENCODE_FLAG_TRANSFORM_8X8_MODE |
+		V4L2_H264_ENCODE_FLAG_CONSTRAINED_INTRA_PRED |
+		V4L2_H264_ENCODE_FLAG_MARK_LONGTERM))
+		return -EINVAL;
+
+	if (encode_params->slice_type != V4L2_H264_SLICE_TYPE_I &&
+	    encode_params->slice_type != V4L2_H264_SLICE_TYPE_P &&
+	    encode_params->slice_type != V4L2_H264_SLICE_TYPE_B &&
+	    encode_params->slice_type != V4L2_H264_SLICE_TYPE_SI &&
+		encode_params->slice_type != V4L2_H264_SLICE_TYPE_SP)
+		return -EINVAL;
+
+	if (encode_params->cabac_init_idc > 2)
+		return -EINVAL;
+
+	if (encode_params->nalu_type != V4L2_H264_NAL_CODED_SLICE_NON_IDR_PIC &&
+	    encode_params->nalu_type != V4L2_H264_NAL_CODED_SLICE_IDR_PIC)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int
+validate_h264_encode_rc(struct v4l2_ctrl_h264_encode_rc *encode_rc)
+{
+	if (encode_rc->qp > 51)
+		return -EINVAL;
+
+	if (encode_rc->qp_min > 51 || encode_rc->qp_max > 51)
+		return -EINVAL;
+
+	if (encode_rc->qp_min > encode_rc->qp_max)
+		return -EINVAL;
+
+	return 0;
+}
+
 /*
  * Compound controls validation requires setting unused fields/flags to zero
  * in order to properly detect unchanged controls with v4l2_ctrl_type_op_equal's
@@ -1179,6 +1221,12 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 			return -EINVAL;
 		break;
 
+	case V4L2_CTRL_TYPE_H264_ENCODE_PARAMS:
+		return validate_h264_encode_params(p);
+
+	case V4L2_CTRL_TYPE_H264_ENCODE_RC:
+		return validate_h264_encode_rc(p);
+
 	default:
 		return -EINVAL;
 	}
@@ -1881,6 +1929,12 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
 	case V4L2_CTRL_TYPE_VP8_ENCODE_PARAMS:
 		elem_size = sizeof(struct v4l2_ctrl_vp8_encode_params);
 		break;
+	case V4L2_CTRL_TYPE_H264_ENCODE_PARAMS:
+		elem_size = sizeof(struct v4l2_ctrl_h264_encode_params);
+		break;
+	case V4L2_CTRL_TYPE_H264_ENCODE_RC:
+		elem_size = sizeof(struct v4l2_ctrl_h264_encode_rc);
+		break;
 	default:
 		if (type < V4L2_CTRL_COMPOUND_TYPES)
 			elem_size = sizeof(s32);
drivers/media/v4l2-core/v4l2-ctrls-defs.c +9 -0
index bd26f1d89bd5..920ef552770c 100644
@@ -1239,6 +1239,9 @@ const char *v4l2_ctrl_get_name(u32 id)
 	case V4L2_CID_STATELESS_VP8_ENCODE_PARAMS:		return "VP8 Encode Parameters";
 	case V4L2_CID_STATELESS_VP8_ENCODE_QP:			return "VP8 Encode QP";
 
+	case V4L2_CID_STATELESS_H264_ENCODE_PARAMS:             return "H264 Encode Parameters";
+	case V4L2_CID_STATELESS_H264_ENCODE_RC:                 return "H264 Encode Rate-Control";
+
 	/* Colorimetry controls */
 	/* Keep the order of the 'case's the same as in v4l2-controls.h! */
 	case V4L2_CID_COLORIMETRY_CLASS:	return "Colorimetry Controls";
@@ -1607,6 +1610,12 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
 	case V4L2_CID_COLORIMETRY_HDR10_MASTERING_DISPLAY:
 		*type = V4L2_CTRL_TYPE_HDR10_MASTERING_DISPLAY;
 		break;
+	case V4L2_CID_STATELESS_H264_ENCODE_PARAMS:
+		*type = V4L2_CTRL_TYPE_H264_ENCODE_PARAMS;
+		break;
+	case V4L2_CID_STATELESS_H264_ENCODE_RC:
+		*type = V4L2_CTRL_TYPE_H264_ENCODE_RC;
+		break;
 	default:
 		*type = V4L2_CTRL_TYPE_INTEGER;
 		break;
include/uapi/linux/v4l2-controls.h +85 -0

91 lines not on this page · raw message

include/uapi/linux/videodev2.h +2 -0
index b3cbdc60b82c..082018abdd18 100644
@@ -1893,6 +1893,8 @@ enum v4l2_ctrl_type {
 	V4L2_CTRL_TYPE_H264_SLICE_PARAMS    = 0x0203,
 	V4L2_CTRL_TYPE_H264_DECODE_PARAMS   = 0x0204,
 	V4L2_CTRL_TYPE_H264_PRED_WEIGHTS    = 0x0205,
+	V4L2_CTRL_TYPE_H264_ENCODE_PARAMS   = 0x0210,
+	V4L2_CTRL_TYPE_H264_ENCODE_RC       = 0x0211,
 
 	V4L2_CTRL_TYPE_FWHT_PARAMS	    = 0x0220,
 
Signature · 1 line
2.25.1
#12 lore raw

RFC 6/6 media: verisilicon: Add H.264 stateless encoder

In reply to Andrzej Pietrasiewicz

Add H.264 stateless encoder for the Hantro H1 variant.
It only supports constrained baseline, only I and P frames and allows using either last frame as reference, or a long time reference frame (which must have been marked as such in advance).

---
 drivers/media/platform/verisilicon/Makefile   |   1 +
 drivers/media/platform/verisilicon/hantro.h   |   3 +
 .../media/platform/verisilicon/hantro_drv.c   |  10 +
 .../platform/verisilicon/hantro_h1_h264_enc.c | 493 +++++++++++
 .../platform/verisilicon/hantro_h1_regs.h     |   5 +
 .../media/platform/verisilicon/hantro_h264.c  | 777 ++++++++++++++++++
 .../media/platform/verisilicon/hantro_hw.h    |  23 +
 .../platform/verisilicon/stm32mp25_venc_hw.c  |  22 +-
 8 files changed, 1333 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/platform/verisilicon/hantro_h1_h264_enc.c
drivers/media/platform/verisilicon/Makefile +1 -0
index faa19644fecb..f9cea26e2cab 100644
@@ -9,6 +9,7 @@ hantro-vpu-y += \
 		hantro_boolenc.o \
 		hantro_h1_jpeg_enc.o \
 		hantro_h1_vp8_enc.o \
+		hantro_h1_h264_enc.o \
 		hantro_g1.o \
 		hantro_g1_h264_dec.o \
 		hantro_g1_mpeg2_dec.o \
drivers/media/platform/verisilicon/hantro.h +3 -0
index a248734013db..42c69deed10e 100644
@@ -33,6 +33,7 @@ struct hantro_postproc_ops;
 
 #define HANTRO_JPEG_ENCODER	BIT(0)
 #define HANTRO_VP8_ENCODER	BIT(1)
+#define HANTRO_H264_ENCODER	BIT(2)
 #define HANTRO_ENCODERS		0x0000ffff
 #define HANTRO_MPEG2_DECODER	BIT(16)
 #define HANTRO_VP8_DECODER	BIT(17)
@@ -126,6 +127,7 @@ enum hantro_codec_mode {
 	HANTRO_MODE_VP9_DEC,
 	HANTRO_MODE_AV1_DEC,
 	HANTRO_MODE_VP8_ENC,
+	HANTRO_MODE_H264_ENC,
 };
 
 /*
@@ -277,6 +279,7 @@ struct hantro_ctx {
 		struct hantro_vp9_dec_hw_ctx vp9_dec;
 		struct hantro_av1_dec_hw_ctx av1_dec;
 		struct hantro_vp8_enc_hw_ctx vp8_enc;
+		struct hantro_h264_enc_hw_ctx h264_enc;
 	};
 };
 
drivers/media/platform/verisilicon/hantro_drv.c +10 -0
index 0c8a4fcf2bf4..da52f6e28949 100644
@@ -603,6 +603,16 @@ static const struct hantro_ctrl controls[] = {
 		.cfg = {
 			.id = V4L2_CID_STATELESS_AV1_FILM_GRAIN,
 		},
+	}, {
+		.codec = HANTRO_H264_ENCODER,
+		.cfg = {
+			.id = V4L2_CID_STATELESS_H264_ENCODE_PARAMS,
+		},
+	}, {
+		.codec = HANTRO_H264_ENCODER,
+		.cfg = {
+			.id = V4L2_CID_STATELESS_H264_ENCODE_RC,
+		},
 	},
 };
 
drivers/media/platform/verisilicon/hantro_h1_h264_enc.c new file +493 -0

493 lines not on this page · raw message

drivers/media/platform/verisilicon/hantro_h1_regs.h +5 -0
index efb46da23eab..4be852630cf7 100644
@@ -80,6 +80,7 @@
 #define    H1_REG_ENC_CTRL2_TRANS8X8_MODE_EN		BIT(21)
 #define    H1_REG_ENC_CTRL2_CABAC_INIT_IDC(x)		((x) << 19)
 #define    H1_REG_ENC_CTRL2_VP8_BOOLENC_ENABLE		BIT(18)
+#define    H1_REG_ENC_CTRL2_ENTROPY_CABAC		BIT(18)
 #define    H1_REG_ENC_CTRL2_H264_INTER4X4_MODE		BIT(17)
 #define    H1_REG_ENC_CTRL2_H264_STREAM_MODE		BIT(16)
 #define    H1_REG_ENC_CTRL2_INTRA16X16_MODE(x)		((x))
@@ -148,6 +149,8 @@
 #define     H1_REG_VP8_SEG0_DQUT_DC_Y2(x)		(((x) & 0x1ff) << 17)
 #define     H1_REG_VP8_SEG0_DQUT_AC_Y1(x)		(((x) & 0x1ff) << 8)
 #define     H1_REG_VP8_SEG0_DQUT_DC_Y1(x)		(((x) & 0xff) << 0)
+#define H1_REG_H264_REFERENCE_IDX			0x084
+#define     H1_REG_H264_MV_REF_IDX(x)			(((x) & 0x03) << 26)
 #define H1_REG_VP8_SEG0_QUANT_DQUT_1			0x088
 #define     H1_REG_VP8_SEGMENT_MAP_UPDATE		BIT(30)
 #define     H1_REG_VP8_SEGMENT_EN			BIT(29)
@@ -156,6 +159,8 @@
 #define     H1_REG_VP8_SEG0_DQUT_AC_CHR(x)		(((x) & 0x1ff) << 17)
 #define     H1_REG_VP8_SEG0_DQUT_DC_CHR(x)		(((x) & 0xff) << 9)
 #define     H1_REG_VP8_SEG0_DQUT_AC_Y2(x)		(((x) & 0x1ff) << 0)
+#define H1_REG_H264_REFERENCE_CTRL			0x088
+#define     H1_REG_H264_MARK_LONGTRERM			BIT(26)
 #define H1_REG_VP8_BOOL_ENC				0x08c
 #define H1_REG_CHKPT_DELTA_QP				0x090
 #define     H1_REG_CHKPT_DELTA_QP_CHK0(x)		(((x) & 0x0f) << 0)
drivers/media/platform/verisilicon/hantro_h264.c +777 -0

786 lines not on this page · raw message

drivers/media/platform/verisilicon/hantro_hw.h +23 -0
index e2dc03b81b0d..444a339642d3 100644
@@ -417,6 +417,24 @@ struct hantro_vp8_enc_hw_ctx {
 	u8 copy_buffer_to_alternate;
 };
 
+#define HANTRO_H264_NUM_INTERNAL_FRAMES 4
+#define HANTRO_H264_CABAC_IDC_NUM	3
+#define HANTRO_H264_CABAC_CV_NUM	460
+
+struct hantro_h264_enc_hw_ctx {
+	struct hantro_aux_buf luma_internal[HANTRO_H264_NUM_INTERNAL_FRAMES];
+	struct hantro_aux_buf chroma_internal[HANTRO_H264_NUM_INTERNAL_FRAMES];
+	u64 reference_ts[HANTRO_H264_NUM_INTERNAL_FRAMES];
+	int last_idx;
+	int last_prev_idx;
+	int reconstr_idx;
+	int ltr_idx;
+	struct hantro_aux_buf nal_table;
+	struct hantro_aux_buf cabac_ctx[HANTRO_H264_CABAC_IDC_NUM];
+	struct hantro_aux_buf mv_buf;
+	struct hantro_aux_buf segment_map;
+};
+
 /**
  * struct hantro_codec_ops - codec mode specific operations
  *
@@ -635,4 +653,9 @@ void hantro_vp8_enc_exit(struct hantro_ctx *ctx);
 int hantro_h1_vp8_enc_run(struct hantro_ctx *ctx);
 void hantro_h1_vp8_enc_done(struct hantro_ctx *ctx);
 
+int hantro_h1_h264_enc_run(struct hantro_ctx *ctx);
+int hantro_h264_enc_init(struct hantro_ctx *ctx);
+void hantro_h1_h264_enc_done(struct hantro_ctx *ctx);
+void hantro_h264_enc_exit(struct hantro_ctx *ctx);
+
 #endif /* HANTRO_HW_H_ */
drivers/media/platform/verisilicon/stm32mp25_venc_hw.c +21 -1
index 6e351502c98d..95260c6193c5 100644
@@ -113,6 +113,19 @@ static const struct hantro_fmt stm32mp25_venc_fmts[] = {
 			.step_height = MB_DIM,
 		},
 	},
+	{
+		.fourcc = V4L2_PIX_FMT_H264_SLICE,
+		.codec_mode = HANTRO_MODE_H264_ENC,
+		.max_depth = 2,
+		.frmsize = {
+			.min_width = 96,
+			.max_width = 4080,
+			.step_width = MB_DIM,
+			.min_height = 96,
+			.max_height = 4080,
+			.step_height = MB_DIM,
+		},
+	},
 };
 
 static irqreturn_t stm32mp25_venc_irq(int irq, void *dev_id)
@@ -161,6 +174,13 @@ static const struct hantro_codec_ops stm32mp25_venc_codec_ops[] = {
 		.done = hantro_h1_vp8_enc_done,
 		.exit = hantro_vp8_enc_exit,
 	},
+	[HANTRO_MODE_H264_ENC] = {
+		.run = hantro_h1_h264_enc_run,
+		.reset = stm32mp25_venc_reset,
+		.init = hantro_h264_enc_init,
+		.done = hantro_h1_h264_enc_done,
+		.exit = hantro_h264_enc_exit,
+	},
 };
 
 /*
@@ -178,7 +198,7 @@ static const char * const stm32mp25_venc_clk_names[] = {
 const struct hantro_variant stm32mp25_venc_variant = {
 	.enc_fmts = stm32mp25_venc_fmts,
 	.num_enc_fmts = ARRAY_SIZE(stm32mp25_venc_fmts),
-	.codec = HANTRO_JPEG_ENCODER | HANTRO_VP8_ENCODER,
+	.codec = HANTRO_JPEG_ENCODER | HANTRO_VP8_ENCODER | HANTRO_H264_ENCODER,
 	.codec_ops = stm32mp25_venc_codec_ops,
 	.irqs = stm32mp25_venc_irqs,
 	.num_irqs = ARRAY_SIZE(stm32mp25_venc_irqs),
Signature · 1 line
2.25.1
#13 lore raw
In reply to Andrzej Pietrasiewicz
Quoted 25 lines · Andrzej Pietrasiewicz

Dear All,

This series adds uAPI for stateless H.264 encoding and an accompanying driver using it.

It has been tested on an stm32mp25 and there exists a gstreamer user:

https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/5676

example pipeline:

gst-launch-1.0 videotestsrc num-buffers=30 ! video/x-raw, format=YUY2 ! v4l2slh264enc ! filesink location=test.h264

Rebased onto v6.6 with:

- some patches from ST to actually run the hardware
- my previous VP8 statless encoding series
- VP8 support for H1 from Hugues Fruchet

In particular, this series depends on the latter, which can be found here:

Quoted 7 lines

https://patchwork.linuxtv.org/project/linux-media/list/?series=11358

Here's a branch which contains everything needed to actually run:

https://gitlab.collabora.com/linux/for-upstream/-/tree/h264-enc-rfc-6.6

I kindly ask for comments.

I attempted to port this to an i.MX8M Mini without much success. I get the driver to enumerate, and the gst-inspect of the v4l2codecs detects the device for the encoder, but I get t a bunch of errors.

Pipeline is PREROLLING ...

[  141.946608] f->type = 10
[  141.951595] f->type = 10
[  141.954331] f->type = 10
[  141.957027] f->type = 9
[  141.959512] trying format S264
[  141.962583] trying format NV12
[  141.965639] OUTPUT codec mode: -1
[  141.968962] fmt - w: 640, h: 480
[  141.972202] CAPTURE codec mode: 8
[  141.975529] fmt - w: 640, h: 480
[  141.978778] f->type = 10
[  141.981316] trying format YUYV
[  141.984384] OUTPUT codec mode: -1
[  141.987707] fmt - w: 640, h: 480
[  141.998500] Codec mode = 8
[  142.018876] plane 0 size: 4194304, sizeimage: 4194304
[  142.023986] plane 0 size: 614400, sizeimage: 614400

Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...[ 142.035697] plane 0 size: 4194304,
sizeimage: 4194304

Redistribute latency...
New cl[ 142.042146] plane 0 size: 614400, sizeimage: 614400
ock: GstSystemClock
ERROR: from element /GstPipeline:pipeline0/v4l2slh264enc:v4l2slh264enc0: Driver did not ack the request.
Additional debug info:
../subprojects/gst-plugins-bad/sys/v4l2codecs/gstv4l2codech264enc.c(1002):
gst_v4l2_codec_h264_enc_encode_frame ():
/GstPipeline:pipeline0/v4l2slh264enc:v4l2slh264enc0
Execution ended after 0:00:01.019990114
Setting pipeline to NULL ...
ERROR: from element /GstPipeline:pipeline0/GstVideoTestSrc:videotestsrc0: Internal data stream error.
Additional debug info:
../subprojects/gstreamer/libs/gst/base/gstbasesrc.c(3153):
gst_base_src_loop ():
/GstPipeline:pipeline0/GstVideoTestSrc:videotestsrc0:
streaming stopped, reason error (-5)
ERROR: from element /GstPipeline:pipeline0/GstFileSink:filesink0:
Error while writing to file "test.h264".
Additional debug info:
../subprojects/gstreamer/plugins/elements/gstfilesink.c(716):
gst_file_sink_event (): /GstPipeline:pipeline0/GstFileSink:filesink0

[  144.058389] hantro_watchdog:127: frame processing timed out!

Freeing pipeline ...
[h264-enc-st-dev] root@localhost:~/gstreamer#

If I can get this working, I can do more testing. I don't know how similar the H1 is on the STM32 vs the i.MX8M Mini. I don't have a datasheet for the STM32, but the registers that I checked seemed like they matched, but I don't know enough about how this all works, so I'm a bit at a loss. I can post my own RFC if people are open to reviewing it too. I'll go through and review what I can.

adam

Quoted 32 lines

Regards,

Andrzej Pietrasiewicz (6):
media: verisilicon Correct a typo in
H1_REG_ENC_CTRL2_DEBLOCKING_FILTER_MODE
media: verisilicon: Correct a typo in H1_REG_MAD_CTRL_MAD_THRESHOLD
media: verisilicon: Improve constant's name
media: verisilicon: Update H1 register definitions
media: uapi: Add H.264 stateless encoding uAPI
media: verisilicon: Add H.264 stateless encoder

 drivers/media/platform/verisilicon/Makefile   |   1 +
 drivers/media/platform/verisilicon/hantro.h   |   3 +
 .../media/platform/verisilicon/hantro_drv.c   |  10 +
 .../platform/verisilicon/hantro_h1_h264_enc.c | 493 +++++++++++
 .../platform/verisilicon/hantro_h1_regs.h     |  20 +-
 .../platform/verisilicon/hantro_h1_vp8_enc.c  |   2 +-
 .../media/platform/verisilicon/hantro_h264.c  | 777 ++++++++++++++++++
 .../media/platform/verisilicon/hantro_hw.h    |  23 +
 .../platform/verisilicon/stm32mp25_venc_hw.c  |  22 +-
 drivers/media/v4l2-core/v4l2-ctrls-core.c     |  54 ++
 drivers/media/v4l2-core/v4l2-ctrls-defs.c     |   9 +
 include/uapi/linux/v4l2-controls.h            |  85 ++
 include/uapi/linux/videodev2.h                |   2 +
 13 files changed, 1496 insertions(+), 5 deletions(-)
 create mode 100644 drivers/media/platform/verisilicon/hantro_h1_h264_enc.c

--
2.25.1