Skip to contentSkip to conversation

HEVC/H.265 stateless support for V4L2 and Cedrus

PATCH Superseded Paul Kocialkowski 4 participants 12 messages Sep 27, 2019 – Oct 22, 2019

Subsystems (5)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v8 cover HEVC/H.265 stateless support for V4L2 and Cedrus

HEVC/H.265 stateless support for V4L2 and Cedrus

This is early support for HEVC/H.265 stateless decoding in V4L2, including both definitions and driver support for the Cedrus VPU driver, which concerns Allwinner devices.

A specific pixel format is introduced for the HEVC slice format and controls are provided to pass the bitstream metadata to the decoder. Some bitstream extensions are intentionally not supported at this point.

Since this is the first proposal for stateless HEVC/H.265 support in V4L2, reviews and comments about the controls definitions are particularly welcome.

On the Cedrus side, the H.265 implementation covers frame pictures with both uni-directional and bi-direction prediction modes (P/B slices). Field pictures (interleaved), scaling lists and 10-bit output are not supported at this point.

Changes since v7:
* Rebased on latest media tree;
* Fixed holes in structures for cacheline alignment;
* Added decode mode and start code controls
(only per-slice and no start code is supported at this point).

Changes since v6:
* Rebased on latest media tree from Hans;
* Reordered some fields to avoid holes and multi-padding;
* Updated the documentation.

Changes since v5:
* Rebased atop latest next media tree;
* Moved to flags instead of u8 fields;
* Added padding to ensure 64-bit alignment
(tested with GDB on 32 and 64-bit architectures);
* Reworked cedrus H.265 driver support a bit for flags;
* Split off codec-specific control validation and init;
* Added HEVC controls fields cleanup at std_validate to allow reliable control comparison with memcmp;
* Fixed various misc reported mistakes.

Changes since v4:
* Rebased atop latest H.254 series.

Changes since v3:
* Updated commit messages;
* Updated CID base to avoid conflicts;
* Used cpu_to_le32 for packed le32 data;
* Fixed misc minor issues in the drive code;
* Made it clear in the docs that the API will evolve;
* Made the pixfmt private and split commits about it.

Changes since v2:
* Moved headers to non-public API;
* Added H265 capability for A64 and H5;
* Moved docs to ext-ctrls-codec.rst;
* Mentionned sections of the spec in the docs;
* Added padding to control structures for 32-bit alignment;
* Made write function use void/size in bytes;
* Reduced the number of arguments to helpers when possible;
* Removed PHYS_OFFSET since we already set PFN_OFFSET;
* Added comments where suggested;
* Moved to timestamp for references instead of index;
* Fixed some style issues reported by checkpatch.

Changes since v1:
* Added a H.265 capability to whitelist relevant platforms;
* Switched over to tags instead of buffer indices in the DPB
* Declared variable in their reduced scope as suggested;
* Added the H.265/HEVC spec to the biblio;
* Used in-doc references to the spec and the required APIs;
* Removed debugging leftovers.

Cheers!

Paul Kocialkowski (3):
media: v4l: Add definitions for HEVC stateless decoding
media: pixfmt: Document the HEVC slice pixel format
media: cedrus: Add HEVC/H.265 decoding support

 Documentation/media/uapi/v4l/biblio.rst       |   9 +
 .../media/uapi/v4l/ext-ctrls-codec.rst        | 553 +++++++++++++++-
 .../media/uapi/v4l/pixfmt-compressed.rst      |  23 +
 .../media/uapi/v4l/vidioc-queryctrl.rst       |  18 +
 .../media/videodev2.h.rst.exceptions          |   3 +
 drivers/media/v4l2-core/v4l2-ctrls.c          | 108 ++-
 drivers/media/v4l2-core/v4l2-ioctl.c          |   1 +
 drivers/staging/media/sunxi/cedrus/Makefile   |   2 +-
 drivers/staging/media/sunxi/cedrus/cedrus.c   |  52 +-
 drivers/staging/media/sunxi/cedrus/cedrus.h   |  18 +
 .../staging/media/sunxi/cedrus/cedrus_dec.c   |   9 +
 .../staging/media/sunxi/cedrus/cedrus_h265.c  | 616 ++++++++++++++++++
 .../staging/media/sunxi/cedrus/cedrus_hw.c    |   4 +
 .../staging/media/sunxi/cedrus/cedrus_regs.h  | 271 ++++++++
 .../staging/media/sunxi/cedrus/cedrus_video.c |  10 +
 include/media/hevc-ctrls.h                    | 212 ++++++
 include/media/v4l2-ctrls.h                    |   7 +
 17 files changed, 1907 insertions(+), 9 deletions(-)
 create mode 100644 drivers/staging/media/sunxi/cedrus/cedrus_h265.c
 create mode 100644 include/media/hevc-ctrls.h
Signature · 1 line
2.23.0
#1 lore raw

PATCH v8 1/3 media: v4l: Add definitions for HEVC stateless decoding

This introduces the required definitions for HEVC decoding support with stateless VPUs. The controls associated to the HEVC slice format provide the required meta-data for decoding slices extracted from the bitstream.

They are not exported to the public V4L2 API since reworking this API will likely be needed for covering various use-cases and new hardware.

Multi-slice decoding is exposed as a valid decoding mode to match current H.264 support but it is not yet implemented.

The interface comes with the following limitations:
* No custom quantization matrices (scaling lists);
* Support for a single temporal layer only;
* No slice entry point offsets support;
* No conformance window support;
* No VUI parameters support;
* No support for SPS extensions: range, multilayer, 3d, scc, 4 bits;
* No support for PPS extensions: range, multilayer, 3d, scc, 4 bits.

---
 Documentation/media/uapi/v4l/biblio.rst       |   9 +
 .../media/uapi/v4l/ext-ctrls-codec.rst        | 553 +++++++++++++++++-
 .../media/uapi/v4l/vidioc-queryctrl.rst       |  18 +
 .../media/videodev2.h.rst.exceptions          |   3 +
 drivers/media/v4l2-core/v4l2-ctrls.c          | 108 +++-
 drivers/media/v4l2-core/v4l2-ioctl.c          |   1 +
 include/media/hevc-ctrls.h                    | 212 +++++++
 include/media/v4l2-ctrls.h                    |   7 +
 8 files changed, 907 insertions(+), 4 deletions(-)
 create mode 100644 include/media/hevc-ctrls.h
Documentation/media/uapi/v4l/biblio.rst +9 -0
index ad2ff258afa8..8095f57d3d75 100644
@@ -131,6 +131,15 @@ ITU-T Rec. H.264 Specification (04/2017 Edition)
 
 :author:    International Telecommunication Union (http://www.itu.ch)
 
+.. _hevc:
+
+ITU H.265/HEVC
+==============
+
+:title:     ITU-T Rec. H.265 | ISO/IEC 23008-2 "High Efficiency Video Coding"
+
+:author:    International Telecommunication Union (http://www.itu.ch), International Organisation for Standardisation (http://www.iso.ch)
+
 .. _jfif:
 
 JFIF
Documentation/media/uapi/v4l/ext-ctrls-codec.rst +550 -3

562 lines not on this page · raw message

Documentation/media/uapi/v4l/vidioc-queryctrl.rst +18 -0
index a3d56ffbf4cc..c0b59a5f0236 100644
@@ -473,6 +473,24 @@ See also the examples in :ref:`control`.
       - n/a
       - A struct :c:type:`v4l2_ctrl_h264_decode_params`, containing H264
 	decode parameters for stateless video decoders.
+    * - ``V4L2_CTRL_TYPE_HEVC_SPS``
+      - n/a
+      - n/a
+      - n/a
+      - A struct :c:type:`v4l2_ctrl_hevc_sps`, containing HEVC Sequence
+	Parameter Set for stateless video decoders.
+    * - ``V4L2_CTRL_TYPE_HEVC_PPS``
+      - n/a
+      - n/a
+      - n/a
+      - A struct :c:type:`v4l2_ctrl_hevc_pps`, containing HEVC Picture
+	Parameter Set for stateless video decoders.
+    * - ``V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS``
+      - n/a
+      - n/a
+      - n/a
+      - A struct :c:type:`v4l2_ctrl_hevc_slice_params`, containing HEVC
+	slice parameters for stateless video decoders.
 
 .. tabularcolumns:: |p{6.6cm}|p{2.2cm}|p{8.7cm}|
 
Documentation/media/videodev2.h.rst.exceptions +3 -0
index 35eb513d82a6..a939fe22027e 100644
@@ -141,6 +141,9 @@ replace symbol V4L2_CTRL_TYPE_H264_PPS :c:type:`v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_H264_SCALING_MATRIX :c:type:`v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_H264_SLICE_PARAMS :c:type:`v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_H264_DECODE_PARAMS :c:type:`v4l2_ctrl_type`
+replace symbol V4L2_CTRL_TYPE_HEVC_SPS :c:type:`v4l2_ctrl_type`
+replace symbol V4L2_CTRL_TYPE_HEVC_PPS :c:type:`v4l2_ctrl_type`
+replace symbol V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS :c:type:`v4l2_ctrl_type`
 
 # V4L2 capability defines
 replace define V4L2_CAP_VIDEO_CAPTURE device-capabilities
drivers/media/v4l2-core/v4l2-ctrls.c +107 -1

157 lines not on this page · raw message

drivers/media/v4l2-core/v4l2-ioctl.c +1 -0
index 8a302691447e..789ac3a5354a 100644
@@ -1338,6 +1338,7 @@ static void v4l_fill_fmtdesc(struct v4l2_fmtdesc *fmt)
 		case V4L2_PIX_FMT_VP8_FRAME:    descr = "VP8 Frame"; break;
 		case V4L2_PIX_FMT_VP9:		descr = "VP9"; break;
 		case V4L2_PIX_FMT_HEVC:		descr = "HEVC"; break; /* aka H.265 */
+		case V4L2_PIX_FMT_HEVC_SLICE:	descr = "HEVC Parsed Slice Data"; break;
 		case V4L2_PIX_FMT_FWHT:		descr = "FWHT"; break; /* used in vicodec */
 		case V4L2_PIX_FMT_FWHT_STATELESS:	descr = "FWHT Stateless"; break; /* used in vicodec */
 		case V4L2_PIX_FMT_CPIA1:	descr = "GSPCA CPiA YUV"; break;
include/media/hevc-ctrls.h new file +212 -0

212 lines not on this page · raw message

include/media/v4l2-ctrls.h +7 -0
index 570ff4b0205a..e212bee4a541 100644
@@ -21,6 +21,7 @@
 #include <media/fwht-ctrls.h>
 #include <media/h264-ctrls.h>
 #include <media/vp8-ctrls.h>
+#include <media/hevc-ctrls.h>
 
 /* forward references */
 struct file;
@@ -50,6 +51,9 @@ struct poll_table_struct;
  * @p_h264_slice_params:	Pointer to a struct v4l2_ctrl_h264_slice_params.
  * @p_h264_decode_params:	Pointer to a struct v4l2_ctrl_h264_decode_params.
  * @p_vp8_frame_header:		Pointer to a VP8 frame header structure.
+ * @p_hevc_sps:			Pointer to an HEVC sequence parameter set structure.
+ * @p_hevc_pps:			Pointer to an HEVC picture parameter set structure.
+ * @p_hevc_slice_params:	Pointer to an HEVC slice parameters structure.
  * @p:				Pointer to a compound value.
  */
 union v4l2_ctrl_ptr {
@@ -68,6 +72,9 @@ union v4l2_ctrl_ptr {
 	struct v4l2_ctrl_h264_slice_params *p_h264_slice_params;
 	struct v4l2_ctrl_h264_decode_params *p_h264_decode_params;
 	struct v4l2_ctrl_vp8_frame_header *p_vp8_frame_header;
+	struct v4l2_ctrl_hevc_sps *p_hevc_sps;
+	struct v4l2_ctrl_hevc_pps *p_hevc_pps;
+	struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
 	void *p;
 };
 
Signature · 1 line
2.23.0
#2 lore raw

PATCH v8 2/3 media: pixfmt: Document the HEVC slice pixel format

In reply to Paul Kocialkowski

Document the current state of the HEVC slice pixel format. The format will need to evolve in the future, which is why it is not part of the public API.

---
 .../media/uapi/v4l/pixfmt-compressed.rst      | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)
Documentation/media/uapi/v4l/pixfmt-compressed.rst +23 -0
index 292fdc116c77..7e9b2b939e59 100644
@@ -188,6 +188,29 @@ Compressed Formats
 	If :ref:`VIDIOC_ENUM_FMT` reports ``V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM``
 	then the decoder has no	requirements since it can parse all the
 	information from the raw bytestream.
+    * .. _V4L2-PIX-FMT-HEVC-SLICE:
+
+      - ``V4L2_PIX_FMT_HEVC_SLICE``
+      - 'S265'
+      - HEVC parsed slice data, as extracted from the HEVC bitstream.
+	This format is adapted for stateless video decoders that implement a
+	HEVC pipeline (using the :ref:`mem2mem` and :ref:`media-request-api`).
+	This pixelformat has two modifiers that must be set at least once
+	through the ``V4L2_CID_MPEG_VIDEO_HEVC_DECODE_MODE``
+        and ``V4L2_CID_MPEG_VIDEO_HEVC_START_CODE`` controls.
+	Metadata associated with the frame to decode is required to be passed
+	through the following controls :
+        * ``V4L2_CID_MPEG_VIDEO_HEVC_SPS``
+        * ``V4L2_CID_MPEG_VIDEO_HEVC_PPS``
+        * ``V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS``
+	See the :ref:`associated Codec Control IDs <v4l2-mpeg-hevc>`.
+	Buffers associated with this pixel format must contain the appropriate
+	number of macroblocks to decode a full corresponding frame.
+
+	.. note::
+
+	   This format is not yet part of the public kernel API and it
+	   is expected to change.
     * .. _V4L2-PIX-FMT-FWHT:
 
       - ``V4L2_PIX_FMT_FWHT``
Signature · 1 line
2.23.0
#3 lore raw

PATCH v8 3/3 media: cedrus: Add HEVC/H.265 decoding support

In reply to Paul Kocialkowski

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---
 drivers/staging/media/sunxi/cedrus/Makefile   |   2 +-
 drivers/staging/media/sunxi/cedrus/cedrus.c   |  52 +-
 drivers/staging/media/sunxi/cedrus/cedrus.h   |  18 +
 .../staging/media/sunxi/cedrus/cedrus_dec.c   |   9 +
 .../staging/media/sunxi/cedrus/cedrus_h265.c  | 616 ++++++++++++++++++
 .../staging/media/sunxi/cedrus/cedrus_hw.c    |   4 +
 .../staging/media/sunxi/cedrus/cedrus_regs.h  | 271 ++++++++
 .../staging/media/sunxi/cedrus/cedrus_video.c |  10 +
 8 files changed, 977 insertions(+), 5 deletions(-)
 create mode 100644 drivers/staging/media/sunxi/cedrus/cedrus_h265.c
drivers/staging/media/sunxi/cedrus/Makefile +1 -1
index c85ac6db0302..1bce49d3e7e2 100644
@@ -2,4 +2,4 @@
 obj-$(CONFIG_VIDEO_SUNXI_CEDRUS) += sunxi-cedrus.o
 
 sunxi-cedrus-y = cedrus.o cedrus_video.o cedrus_hw.o cedrus_dec.o \
-		 cedrus_mpeg2.o cedrus_h264.o
+		 cedrus_mpeg2.o cedrus_h264.o cedrus_h265.o
drivers/staging/media/sunxi/cedrus/cedrus.c +48 -4

82 lines not on this page · raw message

drivers/staging/media/sunxi/cedrus/cedrus.h +18 -0
index 2f017a651848..986e059e3202 100644
@@ -27,12 +27,14 @@
 #define CEDRUS_NAME			"cedrus"
 
 #define CEDRUS_CAPABILITY_UNTILED	BIT(0)
+#define CEDRUS_CAPABILITY_H265_DEC	BIT(1)
 
 #define CEDRUS_QUIRK_NO_DMA_OFFSET	BIT(0)
 
 enum cedrus_codec {
 	CEDRUS_CODEC_MPEG2,
 	CEDRUS_CODEC_H264,
+	CEDRUS_CODEC_H265,
 	CEDRUS_CODEC_LAST,
 };
 
@@ -67,6 +69,12 @@ struct cedrus_mpeg2_run {
 	const struct v4l2_ctrl_mpeg2_quantization	*quantization;
 };
 
+struct cedrus_h265_run {
+	const struct v4l2_ctrl_hevc_sps			*sps;
+	const struct v4l2_ctrl_hevc_pps			*pps;
+	const struct v4l2_ctrl_hevc_slice_params	*slice_params;
+};
+
 struct cedrus_run {
 	struct vb2_v4l2_buffer	*src;
 	struct vb2_v4l2_buffer	*dst;
@@ -74,6 +82,7 @@ struct cedrus_run {
 	union {
 		struct cedrus_h264_run	h264;
 		struct cedrus_mpeg2_run	mpeg2;
+		struct cedrus_h265_run	h265;
 	};
 };
 
@@ -110,6 +119,14 @@ struct cedrus_ctx {
 			void		*neighbor_info_buf;
 			dma_addr_t	neighbor_info_buf_dma;
 		} h264;
+		struct {
+			void		*mv_col_buf;
+			dma_addr_t	mv_col_buf_addr;
+			ssize_t		mv_col_buf_size;
+			ssize_t		mv_col_buf_unit_size;
+			void		*neighbor_info_buf;
+			dma_addr_t	neighbor_info_buf_addr;
+		} h265;
 	} codec;
 };
 
@@ -155,6 +172,7 @@ struct cedrus_dev {
 
 extern struct cedrus_dec_ops cedrus_dec_ops_mpeg2;
 extern struct cedrus_dec_ops cedrus_dec_ops_h264;
+extern struct cedrus_dec_ops cedrus_dec_ops_h265;
 
 static inline void cedrus_write(struct cedrus_dev *dev, u32 reg, u32 val)
 {
drivers/staging/media/sunxi/cedrus/cedrus_dec.c +9 -0
index 56ca4c9ad01c..4a2fc33a1d79 100644
@@ -59,6 +59,15 @@ void cedrus_device_run(void *priv)
 			V4L2_CID_MPEG_VIDEO_H264_SPS);
 		break;
 
+	case V4L2_PIX_FMT_HEVC_SLICE:
+		run.h265.sps = cedrus_find_control_data(ctx,
+			V4L2_CID_MPEG_VIDEO_HEVC_SPS);
+		run.h265.pps = cedrus_find_control_data(ctx,
+			V4L2_CID_MPEG_VIDEO_HEVC_PPS);
+		run.h265.slice_params = cedrus_find_control_data(ctx,
+			V4L2_CID_MPEG_VIDEO_HEVC_SLICE_PARAMS);
+		break;
+
 	default:
 		break;
 	}
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +616 -0

616 lines not on this page · raw message

drivers/staging/media/sunxi/cedrus/cedrus_hw.c +4 -0
index fc8579b90dab..2929f04d7312 100644
@@ -50,6 +50,10 @@ int cedrus_engine_enable(struct cedrus_dev *dev, enum cedrus_codec codec)
 		reg |= VE_MODE_DEC_H264;
 		break;
 
+	case CEDRUS_CODEC_H265:
+		reg |= VE_MODE_DEC_H265;
+		break;
+
 	default:
 		return -EINVAL;
 	}
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +271 -0

287 lines not on this page · raw message

drivers/staging/media/sunxi/cedrus/cedrus_video.c +10 -0
index eeee3efd247b..2c81f82efcbb 100644
@@ -41,6 +41,11 @@ static struct cedrus_format cedrus_formats[] = {
 		.pixelformat	= V4L2_PIX_FMT_H264_SLICE,
 		.directions	= CEDRUS_DECODE_SRC,
 	},
+	{
+		.pixelformat	= V4L2_PIX_FMT_HEVC_SLICE,
+		.directions	= CEDRUS_DECODE_SRC,
+		.capabilities	= CEDRUS_CAPABILITY_H265_DEC,
+	},
 	{
 		.pixelformat	= V4L2_PIX_FMT_SUNXI_TILED_NV12,
 		.directions	= CEDRUS_DECODE_DST,
@@ -105,6 +110,7 @@ static void cedrus_prepare_format(struct v4l2_pix_format *pix_fmt)
 	switch (pix_fmt->pixelformat) {
 	case V4L2_PIX_FMT_MPEG2_SLICE:
 	case V4L2_PIX_FMT_H264_SLICE:
+	case V4L2_PIX_FMT_HEVC_SLICE:
 		/* Zero bytes per line for encoded source. */
 		bytesperline = 0;
 
@@ -453,6 +459,10 @@ static int cedrus_start_streaming(struct vb2_queue *vq, unsigned int count)
 		ctx->current_codec = CEDRUS_CODEC_H264;
 		break;
 
+	case V4L2_PIX_FMT_HEVC_SLICE:
+		ctx->current_codec = CEDRUS_CODEC_H265;
+		break;
+
 	default:
 		return -EINVAL;
 	}
Signature · 1 line
2.23.0
#4 lore raw

Re: [PATCH v8 3/3] media: cedrus: Add HEVC/H.265 decoding support

Em Fri, 27 Sep 2019 16:34:11 +0200
Paul Kocialkowski <[email protected]> escreveu:

Quoted 8 lines

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---

...

drivers/staging/media/sunxi/cedrus/cedrus_h265.c line 251

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +2 -0
+		unsigned int ctb_size_luma =
+			1 << log2_max_luma_coding_block_size;

Shifts like this is a little scary. "1" constant is signed. So, if log2_max_luma_coding_block_size is 31, the above logic has undefined behavior. Different archs and C compilers may handle it on different ways.

drivers/staging/media/sunxi/cedrus/cedrus_regs.h line 483

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +2 -0
+#define VE_DEC_H265_LOW_ADDR_PRIMARY_CHROMA(a) \
+	(((a) << 24) & GENMASK(31, 24))

Same applies here and on other similar macros. You need to enforce
(a) to be unsigned, as otherwise the behavior is undefined.

Btw, this is a recurrent pattern on this file. I would define a macro, e. g. something like:

	#define MASK_BITS_AND_SHIFT(v, high, low) \
		((UL(v) << low) & GENMASK(high, low))

And use it for all similar patterns here.

The best would be to include such macro at linux/bits.h, although some upstream discussion is required.

So, for now, let's add it at this header file, but work upstream to have it merged there.

Thanks,
Mauro

#5 lore raw

Re: [PATCH v8 3/3] media: cedrus: Add HEVC/H.265 decoding support

Hi Mauro and thanks for the review,

Quoted 14 lines · Mauro Carvalho Chehab

Em Fri, 27 Sep 2019 16:34:11 +0200
Paul Kocialkowski <[email protected]> escreveu:

Quoted 8 lines

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---

...

Quoted 7 lines

drivers/staging/media/sunxi/cedrus/cedrus_h265.c line 251

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +2 -0
+		unsigned int ctb_size_luma =
+			1 << log2_max_luma_coding_block_size;

Shifts like this is a little scary. "1" constant is signed. So, if log2_max_luma_coding_block_size is 31, the above logic has undefined behavior. Different archs and C compilers may handle it on different ways.

I wasn't aware that it was the case, thanks for bringing this to light! I'll make it 1UL then.

Quoted 6 lines

drivers/staging/media/sunxi/cedrus/cedrus_regs.h line 483

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +2 -0
+#define VE_DEC_H265_LOW_ADDR_PRIMARY_CHROMA(a) \
+	(((a) << 24) & GENMASK(31, 24))

Same applies here and on other similar macros. You need to enforce
(a) to be unsigned, as otherwise the behavior is undefined.

Quoted 7 lines

Btw, this is a recurrent pattern on this file. I would define a macro, e. g. something like:

	#define MASK_BITS_AND_SHIFT(v, high, low) \
		((UL(v) << low) & GENMASK(high, low))

And use it for all similar patterns here.

Sounds good! I find that the reverse wording (SHIFT_AND_MASK_BITS) would be a bit more explicit since the shift happens prior to the mask.

Also we probably need to have parenthesis around "low", right?

Quoted 5 lines

The best would be to include such macro at linux/bits.h, although some upstream discussion is required.

So, for now, let's add it at this header file, but work upstream to have it merged there.

Understood, I'll include it in that header for now and send a separate patch for inclusion in linux/bits.h (apparently the preprocessor doesn't care about redefinitions so we can just remove the cedrus fashion once the common one is in).

What do you think?

Cheers,

Paul

#6 lore raw

Re: [PATCH v8 3/3] media: cedrus: Add HEVC/H.265 decoding support

Hi again,

Quoted 37 lines · Paul Kocialkowski

Hi Mauro and thanks for the review,

Quoted 14 lines · Mauro Carvalho Chehab

Em Fri, 27 Sep 2019 16:34:11 +0200
Paul Kocialkowski <[email protected]> escreveu:

Quoted 8 lines

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---

...

Quoted 7 lines

drivers/staging/media/sunxi/cedrus/cedrus_h265.c line 251

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +2 -0
+		unsigned int ctb_size_luma =
+			1 << log2_max_luma_coding_block_size;

Shifts like this is a little scary. "1" constant is signed. So, if log2_max_luma_coding_block_size is 31, the above logic has undefined behavior. Different archs and C compilers may handle it on different ways.

I wasn't aware that it was the case, thanks for bringing this to light! I'll make it 1UL then.

Quoted 9 lines

drivers/staging/media/sunxi/cedrus/cedrus_regs.h line 483

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +2 -0
+#define VE_DEC_H265_LOW_ADDR_PRIMARY_CHROMA(a) \
+	(((a) << 24) & GENMASK(31, 24))

Same applies here and on other similar macros. You need to enforce
(a) to be unsigned, as otherwise the behavior is undefined.

Btw, this is a recurrent pattern on this file. I would define a macro, e. g. something like:

Quoted 7 lines
Quoted 4 lines
	#define MASK_BITS_AND_SHIFT(v, high, low) \
		((UL(v) << low) & GENMASK(high, low))

And use it for all similar patterns here.

Sounds good! I find that the reverse wording (SHIFT_AND_MASK_BITS) would be a bit more explicit since the shift happens prior to the mask.

Apparently the UL(v) macro just appends UL to v in preprocessor, so it won't work with anything else than direct integers.

I'll replace it with a (unsigned long) cast, that seems to do the job.

Cheers,

Paul

Quoted 18 lines

Also we probably need to have parenthesis around "low", right?

Quoted 5 lines

The best would be to include such macro at linux/bits.h, although some upstream discussion is required.

So, for now, let's add it at this header file, but work upstream to have it merged there.

Understood, I'll include it in that header for now and send a separate patch for inclusion in linux/bits.h (apparently the preprocessor doesn't care about redefinitions so we can just remove the cedrus fashion once the common one is in).

What do you think?

Cheers,

Paul

Signature · 5 lines
Developer of free digital technology and hardware support.

Website: https://www.paulk.fr/
Coding blog: https://code.paulk.fr/
Git repositories: https://git.paulk.fr/ https://git.code.paulk.fr/
#7 lore raw

Re: [PATCH v8 3/3] media: cedrus: Add HEVC/H.265 decoding support

Quoted 45 lines · Paul Kocialkowski

Hi again,

Quoted 42 lines · Paul Kocialkowski

Hi Mauro and thanks for the review,

Quoted 14 lines · Mauro Carvalho Chehab

Em Fri, 27 Sep 2019 16:34:11 +0200
Paul Kocialkowski <[email protected]> escreveu:

Quoted 8 lines

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---

...

Quoted 7 lines

drivers/staging/media/sunxi/cedrus/cedrus_h265.c line 251

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +2 -0
+		unsigned int ctb_size_luma =
+			1 << log2_max_luma_coding_block_size;

Shifts like this is a little scary. "1" constant is signed. So, if log2_max_luma_coding_block_size is 31, the above logic has undefined behavior. Different archs and C compilers may handle it on different ways.

I wasn't aware that it was the case, thanks for bringing this to light! I'll make it 1UL then.

Quoted 13 lines

drivers/staging/media/sunxi/cedrus/cedrus_regs.h line 483

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +2 -0
+#define VE_DEC_H265_LOW_ADDR_PRIMARY_CHROMA(a) \
+	(((a) << 24) & GENMASK(31, 24))

Same applies here and on other similar macros. You need to enforce
(a) to be unsigned, as otherwise the behavior is undefined.

Btw, this is a recurrent pattern on this file. I would define a macro, e. g. something like:

	#define MASK_BITS_AND_SHIFT(v, high, low) \
		((UL(v) << low) & GENMASK(high, low))

And use it for all similar patterns here.

Quoted 7 lines
Quoted 2 lines

Sounds good! I find that the reverse wording (SHIFT_AND_MASK_BITS) would be a bit more explicit since the shift happens prior to the mask.

Apparently the UL(v) macro just appends UL to v in preprocessor, so it won't work with anything else than direct integers.

I'll replace it with a (unsigned long) cast, that seems to do the job.

Shouldn't that be a (u32) cast? Since this is used with 32 bit registers?

Regards,

Hans

Quoted 26 lines

Cheers,

Paul

Quoted 18 lines

Also we probably need to have parenthesis around "low", right?

Quoted 5 lines

The best would be to include such macro at linux/bits.h, although some upstream discussion is required.

So, for now, let's add it at this header file, but work upstream to have it merged there.

Understood, I'll include it in that header for now and send a separate patch for inclusion in linux/bits.h (apparently the preprocessor doesn't care about redefinitions so we can just remove the cedrus fashion once the common one is in).

What do you think?

Cheers,

Paul

#8 lore raw

Re: [PATCH v8 3/3] media: cedrus: Add HEVC/H.265 decoding support

Hi,

Quoted 49 lines · Hans Verkuil
Quoted 48 lines · Paul Kocialkowski

Hi again,

Quoted 44 lines · Paul Kocialkowski

Hi Mauro and thanks for the review,

Quoted 14 lines · Mauro Carvalho Chehab

Em Fri, 27 Sep 2019 16:34:11 +0200
Paul Kocialkowski <[email protected]> escreveu:

Quoted 8 lines

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---

...

Quoted 7 lines

drivers/staging/media/sunxi/cedrus/cedrus_h265.c line 251

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +2 -0
+		unsigned int ctb_size_luma =
+			1 << log2_max_luma_coding_block_size;

Shifts like this is a little scary. "1" constant is signed. So, if log2_max_luma_coding_block_size is 31, the above logic has undefined behavior. Different archs and C compilers may handle it on different ways.

I wasn't aware that it was the case, thanks for bringing this to light! I'll make it 1UL then.

Quoted 6 lines

drivers/staging/media/sunxi/cedrus/cedrus_regs.h line 483

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +2 -0
+#define VE_DEC_H265_LOW_ADDR_PRIMARY_CHROMA(a) \
+	(((a) << 24) & GENMASK(31, 24))

Same applies here and on other similar macros. You need to enforce
(a) to be unsigned, as otherwise the behavior is undefined.

Quoted 7 lines

Btw, this is a recurrent pattern on this file. I would define a macro, e. g. something like:

	#define MASK_BITS_AND_SHIFT(v, high, low) \
		((UL(v) << low) & GENMASK(high, low))

And use it for all similar patterns here.

Sounds good! I find that the reverse wording (SHIFT_AND_MASK_BITS) would be a bit more explicit since the shift happens prior to the mask.

Quoted 6 lines
Quoted 4 lines

Apparently the UL(v) macro just appends UL to v in preprocessor, so it won't work with anything else than direct integers.

I'll replace it with a (unsigned long) cast, that seems to do the job.

Shouldn't that be a (u32) cast? Since this is used with 32 bit registers?

This would work for cedrus, but I think that what Mauro had in mind was to migrate this macro to linux/bits.h, where everthing else (including GENMASK) is apparently defined in terms of unsigned long and not types with explicit numbers of bits. So I find it more consistent to go with unsigned long.

In our case, 64-bit platforms that use cedrus would calculate the macro on 64 bits and use it in 32-bit variables. Since we're never masking beyond the lower 32 bits, I don't see how things could go wrong and the situation looks fairly similar to the use of GENMASK in similar conditions.

Does that sound right to you or am I missing something here?

Cheers,

Paul

Quoted 31 lines

Regards,

Hans

Quoted 26 lines

Cheers,

Paul

Quoted 18 lines

Also we probably need to have parenthesis around "low", right?

Quoted 5 lines

The best would be to include such macro at linux/bits.h, although some upstream discussion is required.

So, for now, let's add it at this header file, but work upstream to have it merged there.

Understood, I'll include it in that header for now and send a separate patch for inclusion in linux/bits.h (apparently the preprocessor doesn't care about redefinitions so we can just remove the cedrus fashion once the common one is in).

What do you think?

Cheers,

Paul

Signature · 3 lines
Paul Kocialkowski, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com
#9 lore raw

Re: [PATCH v8 3/3] media: cedrus: Add HEVC/H.265 decoding support

Quoted 64 lines · Paul Kocialkowski

Hi,

Quoted 49 lines · Hans Verkuil
Quoted 48 lines · Paul Kocialkowski

Hi again,

Quoted 44 lines · Paul Kocialkowski

Hi Mauro and thanks for the review,

Quoted 14 lines · Mauro Carvalho Chehab

Em Fri, 27 Sep 2019 16:34:11 +0200
Paul Kocialkowski <[email protected]> escreveu:

Quoted 8 lines

This introduces support for HEVC/H.265 to the Cedrus VPU driver, with both uni-directional and bi-directional prediction modes supported.

Field-coded (interlaced) pictures, custom quantization matrices and 10-bit output are not supported at this point.

---

...

Quoted 7 lines

drivers/staging/media/sunxi/cedrus/cedrus_h265.c line 251

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_h265.c new file +2 -0
+		unsigned int ctb_size_luma =
+			1 << log2_max_luma_coding_block_size;

Shifts like this is a little scary. "1" constant is signed. So, if log2_max_luma_coding_block_size is 31, the above logic has undefined behavior. Different archs and C compilers may handle it on different ways.

I wasn't aware that it was the case, thanks for bringing this to light! I'll make it 1UL then.

Quoted 6 lines

drivers/staging/media/sunxi/cedrus/cedrus_regs.h line 483

Quoted 2 lines
drivers/staging/media/sunxi/cedrus/cedrus_regs.h +2 -0
+#define VE_DEC_H265_LOW_ADDR_PRIMARY_CHROMA(a) \
+	(((a) << 24) & GENMASK(31, 24))

Same applies here and on other similar macros. You need to enforce
(a) to be unsigned, as otherwise the behavior is undefined.

Quoted 7 lines

Btw, this is a recurrent pattern on this file. I would define a macro, e. g. something like:

	#define MASK_BITS_AND_SHIFT(v, high, low) \
		((UL(v) << low) & GENMASK(high, low))

And use it for all similar patterns here.

Sounds good! I find that the reverse wording (SHIFT_AND_MASK_BITS) would be a bit more explicit since the shift happens prior to the mask.

Quoted 6 lines
Quoted 4 lines

Apparently the UL(v) macro just appends UL to v in preprocessor, so it won't work with anything else than direct integers.

I'll replace it with a (unsigned long) cast, that seems to do the job.

Shouldn't that be a (u32) cast? Since this is used with 32 bit registers?

This would work for cedrus, but I think that what Mauro had in mind was to migrate this macro to linux/bits.h, where everthing else (including GENMASK) is apparently defined in terms of unsigned long and not types with explicit numbers of bits. So I find it more consistent to go with unsigned long.

Quoted 6 lines

In our case, 64-bit platforms that use cedrus would calculate the macro on 64 bits and use it in 32-bit variables. Since we're never masking beyond the lower 32 bits, I don't see how things could go wrong and the situation looks fairly similar to the use of GENMASK in similar conditions.

Does that sound right to you or am I missing something here?

Ah, OK. Fair enough.

Regards,

Hans

Quoted 37 lines

Cheers,

Paul

Quoted 31 lines

Regards,

Hans

Quoted 26 lines

Cheers,

Paul

Quoted 18 lines

Also we probably need to have parenthesis around "low", right?

Quoted 5 lines

The best would be to include such macro at linux/bits.h, although some upstream discussion is required.

So, for now, let's add it at this header file, but work upstream to have it merged there.

Understood, I'll include it in that header for now and send a separate patch for inclusion in linux/bits.h (apparently the preprocessor doesn't care about redefinitions so we can just remove the cedrus fashion once the common one is in).

What do you think?

Cheers,

Paul

Reviewed-by on the v8 series
#10 lore raw
In reply to Paul Kocialkowski

Dne petek, 27. september 2019 ob 16:34:08 CEST je Paul Kocialkowski napisal(a):

Quoted 10 lines

HEVC/H.265 stateless support for V4L2 and Cedrus

This is early support for HEVC/H.265 stateless decoding in V4L2, including both definitions and driver support for the Cedrus VPU driver, which concerns Allwinner devices.

A specific pixel format is introduced for the HEVC slice format and controls are provided to pass the bitstream metadata to the decoder. Some bitstream extensions are intentionally not supported at this point.

Quoted 8 lines

Since this is the first proposal for stateless HEVC/H.265 support in V4L2, reviews and comments about the controls definitions are particularly welcome.

On the Cedrus side, the H.265 implementation covers frame pictures with both uni-directional and bi-direction prediction modes (P/B slices). Field pictures (interleaved), scaling lists and 10-bit output are not supported at this point.

Whole series is:

Hopefully this can be merged soon.

Best regards,
Jernej

Quoted 82 lines

Changes since v7:
* Rebased on latest media tree;
* Fixed holes in structures for cacheline alignment;
* Added decode mode and start code controls
(only per-slice and no start code is supported at this point).

Changes since v6:
* Rebased on latest media tree from Hans;
* Reordered some fields to avoid holes and multi-padding;
* Updated the documentation.

Changes since v5:
* Rebased atop latest next media tree;
* Moved to flags instead of u8 fields;
* Added padding to ensure 64-bit alignment
(tested with GDB on 32 and 64-bit architectures);
* Reworked cedrus H.265 driver support a bit for flags;
* Split off codec-specific control validation and init;
* Added HEVC controls fields cleanup at std_validate to allow reliable control comparison with memcmp;
* Fixed various misc reported mistakes.

Changes since v4:
* Rebased atop latest H.254 series.

Changes since v3:
* Updated commit messages;
* Updated CID base to avoid conflicts;
* Used cpu_to_le32 for packed le32 data;
* Fixed misc minor issues in the drive code;
* Made it clear in the docs that the API will evolve;
* Made the pixfmt private and split commits about it.

Changes since v2:
* Moved headers to non-public API;
* Added H265 capability for A64 and H5;
* Moved docs to ext-ctrls-codec.rst;
* Mentionned sections of the spec in the docs;
* Added padding to control structures for 32-bit alignment;
* Made write function use void/size in bytes;
* Reduced the number of arguments to helpers when possible;
* Removed PHYS_OFFSET since we already set PFN_OFFSET;
* Added comments where suggested;
* Moved to timestamp for references instead of index;
* Fixed some style issues reported by checkpatch.

Changes since v1:
* Added a H.265 capability to whitelist relevant platforms;
* Switched over to tags instead of buffer indices in the DPB
* Declared variable in their reduced scope as suggested;
* Added the H.265/HEVC spec to the biblio;
* Used in-doc references to the spec and the required APIs;
* Removed debugging leftovers.

Cheers!

Paul Kocialkowski (3):
media: v4l: Add definitions for HEVC stateless decoding
media: pixfmt: Document the HEVC slice pixel format
media: cedrus: Add HEVC/H.265 decoding support

 Documentation/media/uapi/v4l/biblio.rst       |   9 +
 .../media/uapi/v4l/ext-ctrls-codec.rst        | 553 +++++++++++++++-
 .../media/uapi/v4l/pixfmt-compressed.rst      |  23 +
 .../media/uapi/v4l/vidioc-queryctrl.rst       |  18 +
 .../media/videodev2.h.rst.exceptions          |   3 +
 drivers/media/v4l2-core/v4l2-ctrls.c          | 108 ++-
 drivers/media/v4l2-core/v4l2-ioctl.c          |   1 +
 drivers/staging/media/sunxi/cedrus/Makefile   |   2 +-
 drivers/staging/media/sunxi/cedrus/cedrus.c   |  52 +-
 drivers/staging/media/sunxi/cedrus/cedrus.h   |  18 +
 .../staging/media/sunxi/cedrus/cedrus_dec.c   |   9 +
 .../staging/media/sunxi/cedrus/cedrus_h265.c  | 616 ++++++++++++++++++
 .../staging/media/sunxi/cedrus/cedrus_hw.c    |   4 +
 .../staging/media/sunxi/cedrus/cedrus_regs.h  | 271 ++++++++
 .../staging/media/sunxi/cedrus/cedrus_video.c |  10 +
 include/media/hevc-ctrls.h                    | 212 ++++++
 include/media/v4l2-ctrls.h                    |   7 +
 17 files changed, 1907 insertions(+), 9 deletions(-)
 create mode 100644 drivers/staging/media/sunxi/cedrus/cedrus_h265.c
 create mode 100644 include/media/hevc-ctrls.h
#11 lore raw

Hi,

Quoted 21 lines · Jernej Škrabec

Dne petek, 27. september 2019 ob 16:34:08 CEST je Paul Kocialkowski napisal(a):

Quoted 10 lines

HEVC/H.265 stateless support for V4L2 and Cedrus

This is early support for HEVC/H.265 stateless decoding in V4L2, including both definitions and driver support for the Cedrus VPU driver, which concerns Allwinner devices.

A specific pixel format is introduced for the HEVC slice format and controls are provided to pass the bitstream metadata to the decoder. Some bitstream extensions are intentionally not supported at this point.

Quoted 8 lines

Since this is the first proposal for stateless HEVC/H.265 support in V4L2, reviews and comments about the controls definitions are particularly welcome.

On the Cedrus side, the H.265 implementation covers frame pictures with both uni-directional and bi-direction prediction modes (P/B slices). Field pictures (interleaved), scaling lists and 10-bit output are not supported at this point.

Quoted 4 lines

Whole series is:

Hopefully this can be merged soon.

Thanks for the review!

I don't see any blockers left so hopefully it will happen soon :)

Cheers,

Paul

Quoted 86 lines
Quoted 82 lines

Changes since v7:
* Rebased on latest media tree;
* Fixed holes in structures for cacheline alignment;
* Added decode mode and start code controls
(only per-slice and no start code is supported at this point).

Changes since v6:
* Rebased on latest media tree from Hans;
* Reordered some fields to avoid holes and multi-padding;
* Updated the documentation.

Changes since v5:
* Rebased atop latest next media tree;
* Moved to flags instead of u8 fields;
* Added padding to ensure 64-bit alignment
(tested with GDB on 32 and 64-bit architectures);
* Reworked cedrus H.265 driver support a bit for flags;
* Split off codec-specific control validation and init;
* Added HEVC controls fields cleanup at std_validate to allow reliable control comparison with memcmp;
* Fixed various misc reported mistakes.

Changes since v4:
* Rebased atop latest H.254 series.

Changes since v3:
* Updated commit messages;
* Updated CID base to avoid conflicts;
* Used cpu_to_le32 for packed le32 data;
* Fixed misc minor issues in the drive code;
* Made it clear in the docs that the API will evolve;
* Made the pixfmt private and split commits about it.

Changes since v2:
* Moved headers to non-public API;
* Added H265 capability for A64 and H5;
* Moved docs to ext-ctrls-codec.rst;
* Mentionned sections of the spec in the docs;
* Added padding to control structures for 32-bit alignment;
* Made write function use void/size in bytes;
* Reduced the number of arguments to helpers when possible;
* Removed PHYS_OFFSET since we already set PFN_OFFSET;
* Added comments where suggested;
* Moved to timestamp for references instead of index;
* Fixed some style issues reported by checkpatch.

Changes since v1:
* Added a H.265 capability to whitelist relevant platforms;
* Switched over to tags instead of buffer indices in the DPB
* Declared variable in their reduced scope as suggested;
* Added the H.265/HEVC spec to the biblio;
* Used in-doc references to the spec and the required APIs;
* Removed debugging leftovers.

Cheers!

Paul Kocialkowski (3):
media: v4l: Add definitions for HEVC stateless decoding
media: pixfmt: Document the HEVC slice pixel format
media: cedrus: Add HEVC/H.265 decoding support

 Documentation/media/uapi/v4l/biblio.rst       |   9 +
 .../media/uapi/v4l/ext-ctrls-codec.rst        | 553 +++++++++++++++-
 .../media/uapi/v4l/pixfmt-compressed.rst      |  23 +
 .../media/uapi/v4l/vidioc-queryctrl.rst       |  18 +
 .../media/videodev2.h.rst.exceptions          |   3 +
 drivers/media/v4l2-core/v4l2-ctrls.c          | 108 ++-
 drivers/media/v4l2-core/v4l2-ioctl.c          |   1 +
 drivers/staging/media/sunxi/cedrus/Makefile   |   2 +-
 drivers/staging/media/sunxi/cedrus/cedrus.c   |  52 +-
 drivers/staging/media/sunxi/cedrus/cedrus.h   |  18 +
 .../staging/media/sunxi/cedrus/cedrus_dec.c   |   9 +
 .../staging/media/sunxi/cedrus/cedrus_h265.c  | 616 ++++++++++++++++++
 .../staging/media/sunxi/cedrus/cedrus_hw.c    |   4 +
 .../staging/media/sunxi/cedrus/cedrus_regs.h  | 271 ++++++++
 .../staging/media/sunxi/cedrus/cedrus_video.c |  10 +
 include/media/hevc-ctrls.h                    | 212 ++++++
 include/media/v4l2-ctrls.h                    |   7 +
 17 files changed, 1907 insertions(+), 9 deletions(-)
 create mode 100644 drivers/staging/media/sunxi/cedrus/cedrus_h265.c
 create mode 100644 include/media/hevc-ctrls.h
Signature · 3 lines
Paul Kocialkowski, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com