Skip to contentSkip to conversation

media: Add a V4L2 HEVC stateless encode uAPI and helpers

RFC PATCH In review Arun Iyengar 1 participant 12 messages Nov 10 – Nov 13 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

cover media: Add a V4L2 HEVC stateless encode uAPI and helpers

Hi everyone,

This RFC proposes a V4L2 uAPI for stateless HEVC (H.265) encoders, built on the kernel-driven design of Paul Kocialkowski's H.264 stateless encode series, "[PATCH 00/14] media: Add V4L2 H.264 stateless encode and VC8000E support" (v1, 2026-05-22):
https://lore.kernel.org/all/[email protected]/
(https://patchwork.kernel.org/project/linux-media/list/?series=1099281)

It applies on media next 6628e7d4c6b2 (v7.4-rc1) with, in this order, patches 1-7 of that series and my "[PATCH RFC 00/11] media: h264:
Stateless encode core follow-ups" applied first (the prerequisite lines below). Only patches 7-9 here need them: they use the codec-independent constant bitrate helper (v4l2-enc-cbr) of the follow-ups; patches 1-6 and 10 only share Kconfig/Makefile context. As far as I can tell, this is the first HEVC stateless encode proposal. It is meant to help the H.264 discussion as much as to add HEVC: HEVC makes one answer to reference handling concrete. I expect to adapt it to the rework of the H.264 core helpers that Paul announced for v2.

What it reuses from the H.264 design:
- V4L2_PIX_FMT_HEVC_SLICE on the CAPTURE queue, with one access unit per buffer;
- the existing V4L2_CID_STATELESS_HEVC_SPS/PPS controls and structures for the coding tools. The parts a decoder never needed (profile, tier and level, conformance window, VUI, VPS) are derived in the kernel from the format, S_PARM, S_SELECTION and the stateful HEVC profile/level/tier controls;
- the kernel writes the VPS, SPS, PPS, AUD and, unless the hardware does it, the slice segment header;
- the QP comes from the stateful QP/rate control controls: direct QP, constant quality and constant bitrate in the core;
- the driver API: init/step/complete/exit, state_constrain and reconstruction buffer callbacks.

What is new:
- V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS: the NAL unit type, TemporalId, slice type and POC of the picture, the slice header syntax elements that the SPS/PPS enable, and its reference picture set, given as a list of OUTPUT buffer timestamps, each marked used-by-current-picture and/or long-term. The kernel keeps the reconstructed pictures, computes the POC deltas, codes the RPS, builds the default reference lists and drops every picture that is not listed, exactly as a decoder does (8.3.2). Userspace keeps full control of the GOP structure, long-term references and temporal layers without any MMCO-like syntax. Hardware with one reference read port uses the single entry marked as used.
- V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK, read-only: the values the kernel settled on (QP, resolved reference lists) and hardware statistics, in a codec-agnostic struct v4l2_stateless_encode_stats (QP sum/min/max, luma SSE, block counts) that H.264 could share. Applications never set it, so v4l2_ctrl_request_complete() stores the value the driver set at the end of the run in the request, and it can be read per request today, without read-only requests.
- A kernel HEVC NAL unit writer (v4l2-hevc-enc-rbsp) with byte-wise emulation prevention, and an encode core (v4l2-hevc-enc) for the RPS/DPB, accommodation, headers and feedback.
- KUnit tests for both: byte-exact VPS/SPS/PPS/AUD/slice headers for three configurations (vectors from an independent reference implementation, see the commit message), and GOP sequences through the core.
- Patches 7-9: the constant bitrate mode of the core on v4l2-enc-cbr, QP limits for I pictures and a re-encode hook for drivers whose hardware writes the slice header.

The uAPI was checked against two unrelated encoders: the Rockchip VEPU580 (RK3588; its driver is posted separately as an RFC) and the VeriSilicon VC8000E (its HEVC registers are in Paul's series). Both write slice headers from registers and take CPU-written parameter sets, and both map one-to-one onto the resolved slice header structure.

Questions where I would particularly like feedback:
1. Explicit references for HEVC versus the sliding window of the H.264 v1 series: is the asymmetry fine, or should H.264 go explicit too?
2. Should the H.264 and HEVC bit writers become one shared helper, as the rate control now is?
3. Is a read-only feedback control the right pattern, and should the stats be their own codec-agnostic control?
4. Capability discovery: reject-on-use plus written-back accommodation, or a dedicated caps control?
5. HEVC_SLICE reuse, or HEVC_SL alongside the H264_SL rename?

Not covered yet: multi-slice, tiles/WPP with kernel-written headers (a driver can still signal uniform tile columns, as the VEPU580 driver does above 4096 pixels), RPS candidates in the SPS, custom scaling lists, weighted prediction, SEI/HRD, 4:2:2/4:4:4 and 10-bit beyond what the SPS can express (Main 10 is signalled but untested).

Testing: the touched files build with W=1 on arm64. The KUnit tests pass on UML (23 tests: 19 for the RBSP writer, 4 for the core). "make SPHINXDIRS=userspace-api/media htmldocs" gives no new warnings. On hardware, the uAPI and helpers ran on an Orange Pi 5 (RK3588S) through the VEPU580 driver, on a v7.3-based kernel whose HEVC encode files are byte-identical to this series:
- constant bitrate, IDR period 120: 1080p60 (300 pictures of two clips) at 2, 4, 8 and 16 Mbit/s and 2160p30 (120 pictures of two clips) at 20 and 40 Mbit/s, within 3.4% of the target, the largest one second window at most 1.35 times the bitrate; PSNR at equal bitrate against the driver's earlier private rate control: +0.27 to +1.0 dB on the animation clips, -0.05 to -0.13 dB on the natural ones (+0.27 dB on average); 4104x2160, 5120x2880 and 8192x4320 in tile columns; every stream decodes in FFmpeg;
- with earlier revisions of these patches: 1080p and 4K at fixed QP and constant quality with GOP 1/10/30, long-term references, TMVP on/off and SAO off, plus two 4K streams in parallel, decode in FFmpeg without errors at 38-59 dB PSNR, and the feedback control matched the expected NAL type, POC, QP and reference list on every picture.

This series was developed with Claude (Anthropic) as a coding assistant:
it wrote the code, the documentation and the commit messages, and I reviewed and tested every change as described above.

---
Arun Iyengar (10):
media: hevc: Add H.265 syntax definitions
media: uapi: Add HEVC stateless encode support
media: hevc: Add stateless encode RBSP writer
media: hevc: Add KUnit tests for the stateless encode RBSP writer
media: hevc: Add stateless encode core
media: hevc: Add KUnit tests for the stateless encode core
media: v4l2-hevc-enc: Add a constant bitrate mode
media: v4l2-hevc-enc: Add QP limits for the I pictures
media: v4l2-hevc-enc: Re-encode oversized pictures
media: docs: Document the HEVC stateless encode uAPI

 .../media/v4l/ext-ctrls-codec-stateless.rst        |  411 ++++++
 .../userspace-api/media/v4l/pixfmt-compressed.rst  |    3 +
 .../media/v4l/videodev2.h.rst.exceptions           |    2 +
 .../userspace-api/media/v4l/vidioc-g-ext-ctrls.rst |    8 +
 .../userspace-api/media/v4l/vidioc-queryctrl.rst   |   12 +
 drivers/media/v4l2-core/.kunitconfig               |    6 +
 drivers/media/v4l2-core/Kconfig                    |   37 +
 drivers/media/v4l2-core/Makefile                   |    4 +
 drivers/media/v4l2-core/v4l2-ctrls-core.c          |  194 +++
 drivers/media/v4l2-core/v4l2-ctrls-defs.c          |    9 +
 drivers/media/v4l2-core/v4l2-hevc-enc-kunit.c      |  554 +++++++
 drivers/media/v4l2-core/v4l2-hevc-enc-rbsp-kunit.c |  696 +++++++++
 drivers/media/v4l2-core/v4l2-hevc-enc-rbsp.c       |  977 +++++++++++++
 drivers/media/v4l2-core/v4l2-hevc-enc.c            | 1510 ++++++++++++++++++++
 include/media/v4l2-ctrls.h                         |    4 +
 include/media/v4l2-hevc-enc-rbsp.h                 |  279 ++++
 include/media/v4l2-hevc-enc.h                      |  300 ++++
 include/media/v4l2-hevc.h                          |  132 +-
 include/uapi/linux/v4l2-controls.h                 |  188 +++
 include/uapi/linux/videodev2.h                     |    2 +
 20 files changed, 5327 insertions(+), 1 deletion(-)

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261110-hevc-encode-uapi-a26990b358b3
prerequisite-patch-id: fd11f19266754d1bd40bf61dd5d1da74a0dc13c2
prerequisite-patch-id: 7c7edc7344998d520efaf7b430c90fd527c704c7
prerequisite-patch-id: 4689de770eb1bcdc153d6beda2e392d0ce6f344d
prerequisite-patch-id: 0afdcc24892eac773036d05d8f504e5ccb35eb62
prerequisite-patch-id: ecc849b3328d4396380f0cfc331536c16d2e0d28
prerequisite-patch-id: c62d0fd16706c4328624948805eb2f8d7b223ae0
prerequisite-patch-id: e955d15603fd070034960a1362e4867c2f124d69
prerequisite-patch-id: 76eb64d7920ddc1f268afa6f13df14cf9d1931c8
prerequisite-patch-id: 654c303a6e9e4a3108e9ae9b7c9aa78f95b82c0c
prerequisite-patch-id: 96f470c1f8b2ca83ffc496f41fd173e925180501
prerequisite-patch-id: fa3430fcef00637bb84f9401bb9045dd46194298
prerequisite-patch-id: da666def8ed2b5296da8dee1be387b2818ba3cc3
prerequisite-patch-id: 7e532c86fbd0f7bd6705ac4f1544b7d7d27cbc26
prerequisite-patch-id: 09e1836b24606834b9ac4aa3c851d39f564cfef6
prerequisite-patch-id: fd8fcc747458e52344df760a17b61748361bfbb1
prerequisite-patch-id: 883eefffa7ea70d39d357467780cb4f2b69c8d36
prerequisite-patch-id: aa018c975f9b1f0555164d46446dea01d6496823
prerequisite-patch-id: d8d6323a5177f6b1ce661a86ee5813d55b4a3570

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw

RFC PATCH 1/10 media: hevc: Add H.265 syntax definitions

Add the ITU-T Rec. H.265 constants that a stateless HEVC encoder needs to produce a bitstream: NAL unit type codes (Table 7-1), profile IDCs (Annex A), access unit delimiter picture types (Table 7-2) and the VUI colour description code points (Tables E.3 to E.5, shared with ITU-T H.273).

Also add small helpers that classify NAL unit types (IRAP, IDR, BLA, leading and sub-layer non-reference pictures), since the reference picture set and picture order count rules depend on them.

These are kernel-internal definitions: the uAPI carries nal_unit_type as the raw H.265 value, like the HEVC decoding controls already do.

---
 include/media/v4l2-hevc.h | 132 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 131 insertions(+), 1 deletion(-)
include/media/v4l2-hevc.h +131 -1
index 973c96be16be..cc649b212a6d 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 /*
- * Helper functions for HEVC stateless codecs.
+ * Helper functions and ITU-T Rec. H.265 syntax definitions for HEVC stateless
+ * codecs.
  */
 
 #ifndef _MEDIA_V4L2_HEVC_H
@@ -38,4 +39,133 @@ v4l2_hevc_pps_num_tile_rows(const struct v4l2_ctrl_hevc_pps *pps)
 		     ARRAY_SIZE(pps->row_height_minus1));
 }
 
+/* ITU-T Rec. H.265 Table 7-1: NAL unit type codes */
+#define V4L2_HEVC_NAL_TRAIL_N		0
+#define V4L2_HEVC_NAL_TRAIL_R		1
+#define V4L2_HEVC_NAL_TSA_N		2
+#define V4L2_HEVC_NAL_TSA_R		3
+#define V4L2_HEVC_NAL_STSA_N		4
+#define V4L2_HEVC_NAL_STSA_R		5
+#define V4L2_HEVC_NAL_RADL_N		6
+#define V4L2_HEVC_NAL_RADL_R		7
+#define V4L2_HEVC_NAL_RASL_N		8
+#define V4L2_HEVC_NAL_RASL_R		9
+#define V4L2_HEVC_NAL_RSV_VCL_N14	14
+#define V4L2_HEVC_NAL_BLA_W_LP		16
+#define V4L2_HEVC_NAL_BLA_W_RADL	17
+#define V4L2_HEVC_NAL_BLA_N_LP		18
+#define V4L2_HEVC_NAL_IDR_W_RADL	19
+#define V4L2_HEVC_NAL_IDR_N_LP		20
+#define V4L2_HEVC_NAL_CRA_NUT		21
+#define V4L2_HEVC_NAL_RSV_IRAP_VCL23	23
+#define V4L2_HEVC_NAL_VPS		32
+#define V4L2_HEVC_NAL_SPS		33
+#define V4L2_HEVC_NAL_PPS		34
+#define V4L2_HEVC_NAL_AUD		35
+#define V4L2_HEVC_NAL_EOS		36
+#define V4L2_HEVC_NAL_EOB		37
+#define V4L2_HEVC_NAL_FD		38
+#define V4L2_HEVC_NAL_PREFIX_SEI	39
+#define V4L2_HEVC_NAL_SUFFIX_SEI	40
+
+/* ITU-T Rec. H.265 Annex A: general_profile_idc */
+#define V4L2_HEVC_PROFILE_IDC_MAIN		1
+#define V4L2_HEVC_PROFILE_IDC_MAIN_10		2
+#define V4L2_HEVC_PROFILE_IDC_MAIN_STILL_PICTURE	3
+#define V4L2_HEVC_PROFILE_IDC_RANGE_EXTENSIONS	4
+
+/* ITU-T Rec. H.265 Table 7-2: pic_type in access unit delimiters */
+#define V4L2_HEVC_AUD_PIC_TYPE_I	0
+#define V4L2_HEVC_AUD_PIC_TYPE_PI	1
+#define V4L2_HEVC_AUD_PIC_TYPE_BPI	2
+
+/* ITU-T Rec. H.265 Table E.1: aspect_ratio_idc */
+#define V4L2_HEVC_VUI_ASPECT_RATIO_IDC_EXTENDED	255
+
+/* ITU-T Rec. H.265 Table E.2: video_format */
+#define V4L2_HEVC_VUI_VIDEO_FORMAT_UNSPECIFIED	5
+
+/* ITU-T Rec. H.265 Tables E.3 to E.5 (ITU-T H.273 code points) */
+#define V4L2_HEVC_VUI_COLOUR_BT709		1
+#define V4L2_HEVC_VUI_COLOUR_UNSPECIFIED	2
+#define V4L2_HEVC_VUI_COLOUR_BT470_SYSTEM_M	4
+#define V4L2_HEVC_VUI_COLOUR_BT470_SYSTEM_BG	5
+#define V4L2_HEVC_VUI_COLOUR_SMPTE170M		6
+#define V4L2_HEVC_VUI_COLOUR_SMPTE240M		7
+#define V4L2_HEVC_VUI_COLOUR_BT2020		9
+#define V4L2_HEVC_VUI_COLOUR_SMPTE431		11
+
+#define V4L2_HEVC_VUI_TRANSFER_BT709		1
+#define V4L2_HEVC_VUI_TRANSFER_UNSPECIFIED	2
+#define V4L2_HEVC_VUI_TRANSFER_SMPTE170M	6
+#define V4L2_HEVC_VUI_TRANSFER_SMPTE240M	7
+#define V4L2_HEVC_VUI_TRANSFER_LINEAR		8
+#define V4L2_HEVC_VUI_TRANSFER_SRGB		13
+#define V4L2_HEVC_VUI_TRANSFER_SMPTE2084	16
+
+#define V4L2_HEVC_VUI_MATRIX_BT709		1
+#define V4L2_HEVC_VUI_MATRIX_UNSPECIFIED	2
+#define V4L2_HEVC_VUI_MATRIX_BT470_SYSTEM_BG	5
+#define V4L2_HEVC_VUI_MATRIX_SMPTE170M		6
+#define V4L2_HEVC_VUI_MATRIX_SMPTE240M		7
+#define V4L2_HEVC_VUI_MATRIX_BT2020_NCL		9
+#define V4L2_HEVC_VUI_MATRIX_BT2020_CL		10
+
+/* Maximum number of temporal sub-layers (sps_max_sub_layers_minus1 + 1) */
+#define V4L2_HEVC_MAX_SUB_LAYERS	7
+
+/* Maximum number of pictures in a short-term RPS (7.4.8) */
+#define V4L2_HEVC_MAX_ST_RPS_PICS	16
+
+/* Maximum number of long-term pictures signalled in a slice header */
+#define V4L2_HEVC_MAX_LT_PICS		16
+
+static inline bool v4l2_hevc_nal_is_irap(unsigned int nal_unit_type)
+{
+	return nal_unit_type >= V4L2_HEVC_NAL_BLA_W_LP &&
+	       nal_unit_type <= V4L2_HEVC_NAL_RSV_IRAP_VCL23;
+}
+
+static inline bool v4l2_hevc_nal_is_idr(unsigned int nal_unit_type)
+{
+	return nal_unit_type == V4L2_HEVC_NAL_IDR_W_RADL ||
+	       nal_unit_type == V4L2_HEVC_NAL_IDR_N_LP;
+}
+
+static inline bool v4l2_hevc_nal_is_bla(unsigned int nal_unit_type)
+{
+	return nal_unit_type >= V4L2_HEVC_NAL_BLA_W_LP &&
+	       nal_unit_type <= V4L2_HEVC_NAL_BLA_N_LP;
+}
+
+/*
+ * Sub-layer non-reference picture (3.132): even nal_unit_type values in the
+ * VCL range below RSV_VCL_N14.
+ */
+static inline bool v4l2_hevc_nal_is_sub_layer_non_ref(unsigned int nal_unit_type)
+{
+	return nal_unit_type <= V4L2_HEVC_NAL_RSV_VCL_N14 &&
+	       !(nal_unit_type & 1);
+}
+
+static inline bool v4l2_hevc_nal_is_leading(unsigned int nal_unit_type)
+{
+	return nal_unit_type >= V4L2_HEVC_NAL_RADL_N &&
+	       nal_unit_type <= V4L2_HEVC_NAL_RASL_R;
+}
+
+static inline char v4l2_hevc_slice_type_char(unsigned char slice_type)
+{
+	switch (slice_type) {
+	case V4L2_HEVC_SLICE_TYPE_B:
+		return 'B';
+	case V4L2_HEVC_SLICE_TYPE_P:
+		return 'P';
+	case V4L2_HEVC_SLICE_TYPE_I:
+		return 'I';
+	default:
+		return 'X';
+	}
+}
+
 #endif /* _MEDIA_V4L2_HEVC_H */
Signature · 1 line
2.55.0
#2 lore raw

RFC PATCH 2/10 media: uapi: Add HEVC stateless encode support

In reply to Arun Iyengar

Introduce the per-picture controls of a stateless HEVC encoder, modelled on the H.264 stateless encode uAPI: the sequence and picture parameter sets keep using the existing V4L2_CID_STATELESS_HEVC_SPS and _PPS controls (and struct v4l2_ctrl_hevc_sps/pps), and a new V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS control carries what the slice segment headers of one picture need.

Unlike H.264, HEVC signals reference picture marking explicitly in every slice header through the reference picture set (RPS). The encode params therefore carry the RPS as a list of reference pictures identified by the timestamp of the OUTPUT buffer they were encoded from, each marked as used by the current picture or kept for later pictures, and short or long term. The kernel knows the picture order count of each reconstructed picture it keeps, so it turns that list into st_ref_pic_set() and the long-term part of the slice header, builds the reference picture lists, and drops every picture that is not listed, exactly like a decoder does (8.3.2). Hardware with a single reference read port selects its reference simply by having one entry marked as used.

The flags that carry slice header syntax elements use the same bit values as their V4L2_HEVC_SLICE_PARAMS_FLAG_* counterparts.

Add a read-only V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK control that describes how the picture was actually encoded: the values the kernel settled on (slice QP, the resolved reference picture lists) and statistics measured by the hardware. Since applications never set it, v4l2_ctrl_request_complete() copies the value the driver set at the end of the encode run into the request, so the feedback can be read back per request without read-only request support. The statistics are kept in a codec-agnostic struct v4l2_stateless_encode_stats, so that the H.264 encoder can report the same data.

Validate the new compound controls: reject NAL unit types that are not VCL picture types, TemporalId values that H.265 forbids for the given type, IRAP pictures with P/B slices or used references, P/B pictures without a used reference and duplicate references; zero the fields that are not coded for the given slice type.

---
 drivers/media/v4l2-core/v4l2-ctrls-core.c | 194 ++++++++++++++++++++++++++++++
 drivers/media/v4l2-core/v4l2-ctrls-defs.c |   9 ++
 include/media/v4l2-ctrls.h                |   4 +
 include/uapi/linux/v4l2-controls.h        | 188 +++++++++++++++++++++++++++++
 include/uapi/linux/videodev2.h            |   2 +
 5 files changed, 397 insertions(+)
drivers/media/v4l2-core/v4l2-ctrls-core.c +194 -0
index cb2b4b72914f..bdeabeb4c751 100644
@@ -11,6 +11,7 @@
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-event.h>
 #include <media/v4l2-fwnode.h>
+#include <media/v4l2-hevc.h>
 
 #include "v4l2-ctrls-priv.h"
 
@@ -19,6 +20,21 @@ static const union v4l2_ctrl_ptr ptr_null;
 #define V4L2_HEVC_MAX_SHORT_TERM_REF_PIC_SETS	64
 #define V4L2_HEVC_MAX_LONG_TERM_REF_PICS_SPS	32
 
+#define V4L2_HEVC_ENCODE_FLAGS_ALL \
+	(V4L2_HEVC_ENCODE_FLAG_SLICE_SAO_LUMA | \
+	 V4L2_HEVC_ENCODE_FLAG_SLICE_SAO_CHROMA | \
+	 V4L2_HEVC_ENCODE_FLAG_SLICE_TEMPORAL_MVP_ENABLED | \
+	 V4L2_HEVC_ENCODE_FLAG_MVD_L1_ZERO | \
+	 V4L2_HEVC_ENCODE_FLAG_CABAC_INIT | \
+	 V4L2_HEVC_ENCODE_FLAG_COLLOCATED_FROM_L0 | \
+	 V4L2_HEVC_ENCODE_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED | \
+	 V4L2_HEVC_ENCODE_FLAG_SLICE_LOOP_FILTER_ACROSS_SLICES_ENABLED | \
+	 V4L2_HEVC_ENCODE_FLAG_REFERENCE | \
+	 V4L2_HEVC_ENCODE_FLAG_NO_OUTPUT_OF_PRIOR_PICS | \
+	 V4L2_HEVC_ENCODE_FLAG_NO_PIC_OUTPUT | \
+	 V4L2_HEVC_ENCODE_FLAG_NUM_REF_IDX_ACTIVE_OVERRIDE | \
+	 V4L2_HEVC_ENCODE_FLAG_DEBLOCKING_FILTER_OVERRIDE)
+
 static void fill_event(struct v4l2_event *ev, struct v4l2_ctrl *ctrl,
 		       u32 changes)
 {
@@ -116,6 +132,8 @@ static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 	struct v4l2_ctrl_h264_scaling_matrix *p_h264_scaling_matrix;
 	struct v4l2_ctrl_av1_sequence *p_av1_sequence;
 	struct v4l2_ctrl_hevc_sps *p_hevc_sps;
+	struct v4l2_ctrl_hevc_encode_params *p_hevc_enc_params;
+	struct v4l2_ctrl_hevc_encode_feedback *p_hevc_enc_feedback;
 	void *p = ptr.p + idx * ctrl->elem_size;
 
 	if (ctrl->p_def.p_const)
@@ -195,6 +213,20 @@ static void std_init_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 		/* 4:2:0 */
 		p_hevc_sps->chroma_format_idc = 1;
 		break;
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS:
+		p_hevc_enc_params = p;
+		/* A valid default: an IDR picture. */
+		p_hevc_enc_params->nal_unit_type = V4L2_HEVC_NAL_IDR_W_RADL;
+		p_hevc_enc_params->nuh_temporal_id_plus1 = 1;
+		p_hevc_enc_params->slice_type = V4L2_HEVC_SLICE_TYPE_I;
+		p_hevc_enc_params->flags = V4L2_HEVC_ENCODE_FLAG_REFERENCE;
+		break;
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK:
+		p_hevc_enc_feedback = p;
+		p_hevc_enc_feedback->nal_unit_type = V4L2_HEVC_NAL_IDR_W_RADL;
+		p_hevc_enc_feedback->nuh_temporal_id_plus1 = 1;
+		p_hevc_enc_feedback->slice_type = V4L2_HEVC_SLICE_TYPE_I;
+		break;
 	}
 }
 
@@ -449,6 +481,12 @@ void v4l2_ctrl_type_op_log(const struct v4l2_ctrl *ctrl)
 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
 		pr_cont("HEVC_DECODE_PARAMS");
 		break;
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS:
+		pr_cont("HEVC_ENCODE_PARAMS");
+		break;
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK:
+		pr_cont("HEVC_ENCODE_FEEDBACK");
+		break;
 	case V4L2_CTRL_TYPE_AV1_SEQUENCE:
 		pr_cont("AV1_SEQUENCE");
 		break;
@@ -988,6 +1026,140 @@ static int validate_av1_sequence(struct v4l2_ctrl_av1_sequence *s)
  * in order to properly detect unchanged controls with v4l2_ctrl_type_op_equal's
  * memcmp.
  */
+static int
+validate_hevc_encode_params(struct v4l2_ctrl_hevc_encode_params *p)
+{
+	u8 nal_unit_type = p->nal_unit_type;
+	bool used = false;
+	unsigned int i, j;
+
+	if (p->flags & ~V4L2_HEVC_ENCODE_FLAGS_ALL)
+		return -EINVAL;
+
+	/* Only the VCL NAL unit types of Table 7-1 that can be produced. */
+	if (nal_unit_type > V4L2_HEVC_NAL_RASL_R &&
+	    (nal_unit_type < V4L2_HEVC_NAL_BLA_W_LP ||
+	     nal_unit_type > V4L2_HEVC_NAL_CRA_NUT))
+		return -EINVAL;
+
+	if (p->slice_type > V4L2_HEVC_SLICE_TYPE_I)
+		return -EINVAL;
+
+	if (!p->nuh_temporal_id_plus1 ||
+	    p->nuh_temporal_id_plus1 > V4L2_HEVC_MAX_SUB_LAYERS)
+		return -EINVAL;
+
+	/* IRAP pictures only contain I slices and have TemporalId 0. */
+	if (v4l2_hevc_nal_is_irap(nal_unit_type) &&
+	    (p->slice_type != V4L2_HEVC_SLICE_TYPE_I ||
+	     p->nuh_temporal_id_plus1 != 1))
+		return -EINVAL;
+
+	/* TSA and STSA pictures cannot have TemporalId 0 (7.4.2.2). */
+	if (nal_unit_type >= V4L2_HEVC_NAL_TSA_N &&
+	    nal_unit_type <= V4L2_HEVC_NAL_STSA_R &&
+	    p->nuh_temporal_id_plus1 == 1)
+		return -EINVAL;
+
+	if (p->num_refs > V4L2_HEVC_DPB_ENTRIES_NUM_MAX)
+		return -EINVAL;
+
+	if (p->five_minus_max_num_merge_cand > 4)
+		return -EINVAL;
+
+	if (p->slice_cb_qp_offset < -12 || p->slice_cb_qp_offset > 12 ||
+	    p->slice_cr_qp_offset < -12 || p->slice_cr_qp_offset > 12)
+		return -EINVAL;
+
+	if (p->num_ref_idx_l0_active_minus1 >= V4L2_HEVC_DPB_ENTRIES_NUM_MAX - 1 ||
+	    p->num_ref_idx_l1_active_minus1 >= V4L2_HEVC_DPB_ENTRIES_NUM_MAX - 1 ||
+	    p->collocated_ref_idx >= V4L2_HEVC_DPB_ENTRIES_NUM_MAX - 1)
+		return -EINVAL;
+
+	if (p->flags & V4L2_HEVC_ENCODE_FLAG_DEBLOCKING_FILTER_OVERRIDE &&
+	    !(p->flags & V4L2_HEVC_ENCODE_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED)) {
+		if (p->slice_beta_offset_div2 < -6 ||
+		    p->slice_beta_offset_div2 > 6 ||
+		    p->slice_tc_offset_div2 < -6 ||
+		    p->slice_tc_offset_div2 > 6)
+			return -EINVAL;
+	} else {
+		p->slice_beta_offset_div2 = 0;
+		p->slice_tc_offset_div2 = 0;
+	}
+
+	if (!(p->flags & V4L2_HEVC_ENCODE_FLAG_DEBLOCKING_FILTER_OVERRIDE))
+		p->flags &= ~V4L2_HEVC_ENCODE_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED;
+
+	/* An IDR picture empties the DPB: it has no reference picture set. */
+	if (v4l2_hevc_nal_is_idr(nal_unit_type)) {
+		p->pic_order_cnt_val = 0;
+		p->num_refs = 0;
+	}
+
+	for (i = 0; i < V4L2_HEVC_DPB_ENTRIES_NUM_MAX; i++) {
+		struct v4l2_hevc_encode_ref *ref = &p->refs[i];
+
+		if (i >= p->num_refs) {
+			memset(ref, 0, sizeof(*ref));
+			continue;
+		}
+
+		if (ref->flags & ~(V4L2_HEVC_ENCODE_REF_FLAG_USED_BY_CURR_PIC |
+				   V4L2_HEVC_ENCODE_REF_FLAG_LONG_TERM))
+			return -EINVAL;
+
+		zero_reserved(*ref);
+
+		for (j = 0; j < i; j++)
+			if (p->refs[j].timestamp == ref->timestamp)
+				return -EINVAL;
+
+		if (ref->flags & V4L2_HEVC_ENCODE_REF_FLAG_USED_BY_CURR_PIC)
+			used = true;
+	}
+
+	/* NumPicTotalCurr is 0 for BLA and CRA pictures (7.4.7.2). */
+	if (v4l2_hevc_nal_is_irap(nal_unit_type) && used)
+		return -EINVAL;
+
+	/* P and B slices need at least one reference (7.4.7.2). */
+	if (p->slice_type != V4L2_HEVC_SLICE_TYPE_I && !used)
+		return -EINVAL;
+
+	if (p->slice_type == V4L2_HEVC_SLICE_TYPE_I) {
+		p->num_ref_idx_l0_active_minus1 = 0;
+		p->num_ref_idx_l1_active_minus1 = 0;
+		p->collocated_ref_idx = 0;
+		p->five_minus_max_num_merge_cand = 0;
+		p->flags &= ~(V4L2_HEVC_ENCODE_FLAG_NUM_REF_IDX_ACTIVE_OVERRIDE |
+			      V4L2_HEVC_ENCODE_FLAG_SLICE_TEMPORAL_MVP_ENABLED |
+			      V4L2_HEVC_ENCODE_FLAG_COLLOCATED_FROM_L0 |
+			      V4L2_HEVC_ENCODE_FLAG_CABAC_INIT);
+	}
+
+	if (p->slice_type != V4L2_HEVC_SLICE_TYPE_B) {
+		p->num_ref_idx_l1_active_minus1 = 0;
+		p->flags &= ~V4L2_HEVC_ENCODE_FLAG_MVD_L1_ZERO;
+
+		/* collocated_from_l0_flag is inferred to be 1 (7.4.7.1). */
+		if (p->slice_type == V4L2_HEVC_SLICE_TYPE_P)
+			p->flags |= V4L2_HEVC_ENCODE_FLAG_COLLOCATED_FROM_L0;
+	}
+
+	if (!(p->flags & V4L2_HEVC_ENCODE_FLAG_NUM_REF_IDX_ACTIVE_OVERRIDE)) {
+		p->num_ref_idx_l0_active_minus1 = 0;
+		p->num_ref_idx_l1_active_minus1 = 0;
+	}
+
+	if (!(p->flags & V4L2_HEVC_ENCODE_FLAG_SLICE_TEMPORAL_MVP_ENABLED))
+		p->collocated_ref_idx = 0;
+
+	zero_reserved(*p);
+
+	return 0;
+}
+
 static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 				 union v4l2_ctrl_ptr ptr)
 {
@@ -1008,6 +1180,7 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 	struct v4l2_ctrl_hevc_slice_params *p_hevc_slice_params;
 	struct v4l2_ctrl_hdr10_mastering_display *p_hdr10_mastering;
 	struct v4l2_ctrl_hevc_decode_params *p_hevc_decode_params;
+	struct v4l2_ctrl_hevc_encode_feedback *p_hevc_enc_feedback;
 	struct v4l2_area *area;
 	struct v4l2_rect *rect;
 	void *p = ptr.p + idx * ctrl->elem_size;
@@ -1402,6 +1575,21 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
 			return -EINVAL;
 		break;
 
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS:
+		return validate_hevc_encode_params(p);
+
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK:
+		p_hevc_enc_feedback = p;
+
+		memset(&p_hevc_enc_feedback->reserved0, 0,
+		       sizeof(p_hevc_enc_feedback->reserved0));
+		memset(&p_hevc_enc_feedback->reserved1, 0,
+		       sizeof(p_hevc_enc_feedback->reserved1));
+		memset(&p_hevc_enc_feedback->stats.reserved0, 0,
+		       sizeof(p_hevc_enc_feedback->stats.reserved0));
+		p_hevc_enc_feedback->stats.reserved1 = 0;
+		break;
+
 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
 		break;
 
@@ -2185,6 +2373,12 @@ static struct v4l2_ctrl *v4l2_ctrl_new(struct v4l2_ctrl_handler *hdl,
 	case V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS:
 		elem_size = sizeof(struct v4l2_ctrl_hevc_decode_params);
 		break;
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS:
+		elem_size = sizeof(struct v4l2_ctrl_hevc_encode_params);
+		break;
+	case V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK:
+		elem_size = sizeof(struct v4l2_ctrl_hevc_encode_feedback);
+		break;
 	case V4L2_CTRL_TYPE_HDR10_CLL_INFO:
 		elem_size = sizeof(struct v4l2_ctrl_hdr10_cll_info);
 		break;
drivers/media/v4l2-core/v4l2-ctrls-defs.c +9 -0
index a0ac858968b6..123b56a32ed5 100644
@@ -1239,6 +1239,8 @@ const char *v4l2_ctrl_get_name(u32 id)
 	case V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS:	return "HEVC Entry Point Offsets";
 	case V4L2_CID_STATELESS_HEVC_EXT_SPS_ST_RPS:		return "HEVC Short Term Ref Sets";
 	case V4L2_CID_STATELESS_HEVC_EXT_SPS_LT_RPS:		return "HEVC Long Term Ref Sets";
+	case V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS:		return "HEVC Encode Parameters";
+	case V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK:		return "HEVC Encode Feedback";
 	case V4L2_CID_STATELESS_AV1_SEQUENCE:			return "AV1 Sequence Parameters";
 	case V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY:		return "AV1 Tile Group Entry";
 	case V4L2_CID_STATELESS_AV1_FRAME:			return "AV1 Frame Parameters";
@@ -1597,6 +1599,13 @@ void v4l2_ctrl_fill(u32 id, const char **name, enum v4l2_ctrl_type *type,
 		*type = V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS;
 		*flags |= V4L2_CTRL_FLAG_DYNAMIC_ARRAY;
 		break;
+	case V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS:
+		*type = V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS;
+		break;
+	case V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK:
+		*type = V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK;
+		*flags |= V4L2_CTRL_FLAG_READ_ONLY;
+		break;
 	case V4L2_CID_STATELESS_VP9_COMPRESSED_HDR:
 		*type = V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR;
 		break;
include/media/v4l2-ctrls.h +4 -0
index 63f1aa373735..042d30b061da 100644
@@ -50,6 +50,8 @@ struct video_device;
  * @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_hevc_encode_params:	Pointer to an HEVC encode parameters structure.
+ * @p_hevc_encode_feedback:	Pointer to an HEVC encode feedback structure.
  * @p_hdr10_cll:		Pointer to an HDR10 Content Light Level structure.
  * @p_hdr10_mastering:		Pointer to an HDR10 Mastering Display structure.
  * @p_area:			Pointer to an area.
@@ -83,6 +85,8 @@ union v4l2_ctrl_ptr {
 	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;
+	struct v4l2_ctrl_hevc_encode_params *p_hevc_encode_params;
+	struct v4l2_ctrl_hevc_encode_feedback *p_hevc_encode_feedback;
 	struct v4l2_ctrl_vp9_compressed_hdr *p_vp9_compressed_hdr_probs;
 	struct v4l2_ctrl_vp9_frame *p_vp9_frame;
 	struct v4l2_ctrl_hdr10_cll_info *p_hdr10_cll;
include/uapi/linux/v4l2-controls.h +188 -0

200 lines not on this page · raw message

include/uapi/linux/videodev2.h +2 -0
index e19628c520e6..a5673254e315 100644
@@ -1993,6 +1993,8 @@ enum v4l2_ctrl_type {
 	V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS	= 0x0274,
 	V4L2_CTRL_TYPE_HEVC_EXT_SPS_ST_RPS	= 0x0275,
 	V4L2_CTRL_TYPE_HEVC_EXT_SPS_LT_RPS	= 0x0276,
+	V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS	= 0x0277,
+	V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK	= 0x0278,
 
 	V4L2_CTRL_TYPE_AV1_SEQUENCE	    = 0x280,
 	V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY = 0x281,
Signature · 1 line
2.55.0
#3 lore raw

RFC PATCH 3/10 media: hevc: Add stateless encode RBSP writer

In reply to Arun Iyengar

Add a writer for the HEVC NAL units that a stateless encoder needs the kernel to produce: video, sequence and picture parameter sets, access unit delimiters and slice segment headers, with optional Annex B start codes. It is the HEVC counterpart of the H.264 stateless encode RBSP helper.

The SPS and PPS are written from the existing struct v4l2_ctrl_hevc_sps and struct v4l2_ctrl_hevc_pps. What those structures do not carry because a decoder does not need it (profile, tier and level, the conformance window, VUI, temporal nesting) comes from a kernel-internal struct v4l2_hevc_sps_video, like struct v4l2_h264_sps_video does for H.264. The VPS is derived from both. Slice segment headers are written from a struct v4l2_hevc_slice_header holding the resolved syntax element values, so that drivers for hardware that writes its own slice headers can program their registers from the same structure.

The scope is what an encoder chooses to use: reference picture sets are always coded in the slice header, the SPS carries no RPS and no long-term reference pictures, scaling lists are the default ones, reference picture lists are not modified and there are no weighted prediction tables, HRD parameters, SEI or extensions. Writing those returns -EINVAL rather than an incorrect bitstream.

Emulation prevention is applied byte by byte as the payload is written, and errors (a full buffer, values that cannot be coded) are sticky so that the syntax writers read like the syntax tables of the specification. The H.264 writer keeps its own bit writer for now; both could share one later.

---
 drivers/media/v4l2-core/Kconfig              |   4 +
 drivers/media/v4l2-core/Makefile             |   1 +
 drivers/media/v4l2-core/v4l2-hevc-enc-rbsp.c | 977 +++++++++++++++++++++++++++
 include/media/v4l2-hevc-enc-rbsp.h           | 279 ++++++++
 4 files changed, 1261 insertions(+)
drivers/media/v4l2-core/Kconfig +4 -0
index b8b849a44abd..9f9eda5d6eac 100644
@@ -53,6 +53,10 @@ config V4L2_H264_ENC
 	tristate
 	select V4L2_ENC_CBR
 
+# Used by drivers that need v4l2-hevc-enc-rbsp.ko
+config V4L2_HEVC_ENC_RBSP
+	tristate
+
 # Used by drivers that need v4l2-vp9.ko
 config V4L2_VP9
 	tristate
drivers/media/v4l2-core/Makefile +1 -0
index 3aa0855d7be7..3dda145d94d1 100644
@@ -32,6 +32,7 @@ obj-$(CONFIG_V4L2_FWNODE) += v4l2-fwnode.o
 obj-$(CONFIG_V4L2_H264) += v4l2-h264.o
 obj-$(CONFIG_V4L2_H264_ENC) += v4l2-h264-enc.o v4l2-h264-enc-rbsp.o \
 			       v4l2-h264-enc-rc.o
+obj-$(CONFIG_V4L2_HEVC_ENC_RBSP) += v4l2-hevc-enc-rbsp.o
 obj-$(CONFIG_V4L2_ISP) += v4l2-isp.o
 obj-$(CONFIG_V4L2_JPEG_HELPER) += v4l2-jpeg.o
 obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o
drivers/media/v4l2-core/v4l2-hevc-enc-rbsp.c new file +977 -0

977 lines not on this page · raw message

include/media/v4l2-hevc-enc-rbsp.h new file +279 -0

279 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

RFC PATCH 4/10 media: hevc: Add KUnit tests for the stateless encode RBSP writer

In reply to Arun Iyengar

Check the HEVC RBSP writer byte for byte: Exp-Golomb coding, emulation prevention (including zero runs that do not start on byte boundaries and the NAL unit header, which is not subject to it), error handling, and the VPS, SPS, PPS, access unit delimiter and slice segment headers of three configurations that between them use every syntax element the writer can produce:

- 720p Main level 3.1 with SAO, AMP, TMVP and a VUI with colour description and timing;
- 640x360 coded as 640x368 with a conformance window, PCM, long-term references and a PPS with dependent slices, output flag, extra slice header bits, CABAC init, chroma QP offsets, 2x2 uniform tiles, deblocking override, list modification and slice header extension;
- 2160p60 Main 10 high tier level 5.1 with three temporal sub-layers, default scaling lists, a full VUI, VPS timing, non-uniform tiles with wavefront parallel processing and deblocking disabled.

No HEVC encoder was available to produce the expected bitstreams, so they come from an independent implementation that builds bit lists from the H.265 syntax tables and applies emulation prevention as a separate pass, and that parses them back to check every syntax element. The VPS of the first configuration also matches the VPS commonly found in x265 output for such a stream (from memory: it could not be regenerated here).

Add a .kunitconfig so the tests can be run with:

  ./tools/testing/kunit/kunit.py run --kunitconfig=drivers/media/v4l2-core
---
 drivers/media/v4l2-core/.kunitconfig               |   5 +
 drivers/media/v4l2-core/Kconfig                    |  13 +
 drivers/media/v4l2-core/Makefile                   |   1 +
 drivers/media/v4l2-core/v4l2-hevc-enc-rbsp-kunit.c | 696 +++++++++++++++++++++
 4 files changed, 715 insertions(+)
drivers/media/v4l2-core/.kunitconfig new file +5 -0
new file mode 100644
index 000000000000..4ef422b5e0a8
@@ -0,0 +1,5 @@
+CONFIG_KUNIT=y
+CONFIG_MEDIA_SUPPORT=y
+CONFIG_MEDIA_SUPPORT_FILTER=n
+CONFIG_VIDEO_DEV=y
+CONFIG_V4L2_HEVC_ENC_RBSP_KUNIT_TEST=y
drivers/media/v4l2-core/Kconfig +13 -0
index 9f9eda5d6eac..bc6bf06aa681 100644
@@ -57,6 +57,19 @@ config V4L2_H264_ENC
 config V4L2_HEVC_ENC_RBSP
 	tristate
 
+config V4L2_HEVC_ENC_RBSP_KUNIT_TEST
+	tristate "KUnit tests for the V4L2 HEVC encode RBSP writer" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	select V4L2_HEVC_ENC_RBSP
+	default KUNIT_ALL_TESTS
+	help
+	  Build the KUnit tests of the HEVC NAL unit writer used by stateless
+	  HEVC encoders. They check the VPS, SPS, PPS, access unit delimiter
+	  and slice segment headers it writes, byte for byte, against known
+	  bitstreams.
+
+	  If unsure, say N.
+
 # Used by drivers that need v4l2-vp9.ko
 config V4L2_VP9
 	tristate
drivers/media/v4l2-core/Makefile +1 -0
index 3dda145d94d1..6bf89b90165f 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_V4L2_H264) += v4l2-h264.o
 obj-$(CONFIG_V4L2_H264_ENC) += v4l2-h264-enc.o v4l2-h264-enc-rbsp.o \
 			       v4l2-h264-enc-rc.o
 obj-$(CONFIG_V4L2_HEVC_ENC_RBSP) += v4l2-hevc-enc-rbsp.o
+obj-$(CONFIG_V4L2_HEVC_ENC_RBSP_KUNIT_TEST) += v4l2-hevc-enc-rbsp-kunit.o
 obj-$(CONFIG_V4L2_ISP) += v4l2-isp.o
 obj-$(CONFIG_V4L2_JPEG_HELPER) += v4l2-jpeg.o
 obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o
drivers/media/v4l2-core/v4l2-hevc-enc-rbsp-kunit.c new file +696 -0

696 lines not on this page · raw message

Signature · 1 line
2.55.0
#5 lore raw

RFC PATCH 5/10 media: hevc: Add stateless encode core

In reply to Arun Iyengar

Add the kernel side of the stateless HEVC encode uAPI, with the same driver interface as the H.264 stateless encode core: drivers call v4l2_hevc_enc_init() and v4l2_hevc_enc_exit() when streaming starts and stops, v4l2_hevc_enc_step() before starting the hardware and v4l2_hevc_enc_complete() when it is done, and provide callbacks to allocate reconstruction buffers and to constrain the parameters to what the hardware supports.

For each picture, the core:
- reads the SPS, PPS and encode params controls and accommodates them:
the coded size and conformance window come from the CAPTURE format and an optional crop rectangle, the profile, tier and level from the stateful HEVC controls (the lowest fitting level when there is no level control), the VUI from the colorimetry and frame interval, and features the hardware or the RBSP writer cannot do are turned off. The SPS and PPS actually used are written back to their controls. A changed SPS is only activated by the next IRAP picture;
- resolves the reference picture set: each reference timestamp is looked up among the reconstructed pictures it keeps, the short-term entries are sorted by POC distance and coded with their deltas, the long-term ones with their POC LSB and MSB cycle, and reference picture lists 0 and 1 are built as in 8.3.4, limited to the number of entries the hardware supports. Drivers can also limit the number of negative, positive and long-term pictures, for hardware that writes the RPS from registers. It checks what a decoder would otherwise get wrong:
references that are not in the DPB, long-term pictures used as short-term ones, POC values whose MSB could not be derived, and pictures that need references without having one;
- selects the QP: from the I/P/B QP controls without frame rate control, from the constant quality control in CQ mode, or from driver rate control callbacks for CBR and VBR;
- writes the AUD, VPS, SPS, PPS and slice segment header that the hardware does not write itself at the start of the CAPTURE buffer.

On completion, it drops the pictures that were not in the RPS, stores the reconstruction of a reference picture in a free slot, sets the buffer flags, patches nuh_temporal_id_plus1 into the slice NAL units of hardware that cannot write it, and sets the feedback control so that it gets stored in the request. Nothing changes in the reference state before completion, so a failed or dropped encode run can simply be retried.

---
 drivers/media/v4l2-core/Kconfig         |    5 +
 drivers/media/v4l2-core/Makefile        |    1 +
 drivers/media/v4l2-core/v4l2-hevc-enc.c | 1418 +++++++++++++++++++++++++++++++
 include/media/v4l2-hevc-enc.h           |  284 +++++++
 4 files changed, 1708 insertions(+)
drivers/media/v4l2-core/Kconfig +5 -0
index bc6bf06aa681..4c39bd943e81 100644
@@ -57,6 +57,11 @@ config V4L2_H264_ENC
 config V4L2_HEVC_ENC_RBSP
 	tristate
 
+# Used by drivers that need v4l2-hevc-enc.ko
+config V4L2_HEVC_ENC
+	tristate
+	select V4L2_HEVC_ENC_RBSP
+
 config V4L2_HEVC_ENC_RBSP_KUNIT_TEST
 	tristate "KUnit tests for the V4L2 HEVC encode RBSP writer" if !KUNIT_ALL_TESTS
 	depends on KUNIT
drivers/media/v4l2-core/Makefile +1 -0
index 6bf89b90165f..153b8a6e74b8 100644
@@ -32,6 +32,7 @@ obj-$(CONFIG_V4L2_FWNODE) += v4l2-fwnode.o
 obj-$(CONFIG_V4L2_H264) += v4l2-h264.o
 obj-$(CONFIG_V4L2_H264_ENC) += v4l2-h264-enc.o v4l2-h264-enc-rbsp.o \
 			       v4l2-h264-enc-rc.o
+obj-$(CONFIG_V4L2_HEVC_ENC) += v4l2-hevc-enc.o
 obj-$(CONFIG_V4L2_HEVC_ENC_RBSP) += v4l2-hevc-enc-rbsp.o
 obj-$(CONFIG_V4L2_HEVC_ENC_RBSP_KUNIT_TEST) += v4l2-hevc-enc-rbsp-kunit.o
 obj-$(CONFIG_V4L2_ISP) += v4l2-isp.o
drivers/media/v4l2-core/v4l2-hevc-enc.c new file +1418 -0

1,418 lines not on this page · raw message

include/media/v4l2-hevc-enc.h new file +284 -0

284 lines not on this page · raw message

Signature · 1 line
2.55.0
#6 lore raw

RFC PATCH 6/10 media: hevc: Add KUnit tests for the stateless encode core

In reply to Arun Iyengar

Run short sequences of pictures through the HEVC stateless encode core, with a real control handler and a fake CAPTURE buffer, and check:

- an IDR picture followed by P pictures that use, keep without using, drop and turn long-term their references: the coded RPS, reference picture list 0, collocated picture, DPB contents, QP, buffer flags and feedback;
- that invalid references (dropped pictures, long-term pictures used as short-term, POC values a decoder could not derive, B slices on P-only hardware) fail without changing the DPB;
- CRA and IDR pictures keeping and flushing references;
- hardware that writes its own slice headers without TemporalId: nothing is written for pictures without parameter sets, and nuh_temporal_id_plus1 is patched after the encode run;
- intra-only hardware, for which the SPS is accommodated and no reference slots are allocated;
- the control validation of the encode parameters, including its default value.

---
 drivers/media/v4l2-core/.kunitconfig          |   1 +
 drivers/media/v4l2-core/Kconfig               |  14 +
 drivers/media/v4l2-core/Makefile              |   1 +
 drivers/media/v4l2-core/v4l2-hevc-enc-kunit.c | 554 ++++++++++++++++++++++++++
 4 files changed, 570 insertions(+)
drivers/media/v4l2-core/.kunitconfig +1 -0
index 4ef422b5e0a8..65aa43070053 100644
@@ -3,3 +3,4 @@ CONFIG_MEDIA_SUPPORT=y
 CONFIG_MEDIA_SUPPORT_FILTER=n
 CONFIG_VIDEO_DEV=y
 CONFIG_V4L2_HEVC_ENC_RBSP_KUNIT_TEST=y
+CONFIG_V4L2_HEVC_ENC_KUNIT_TEST=y
drivers/media/v4l2-core/Kconfig +14 -0
index 4c39bd943e81..eb245deb4622 100644
@@ -75,6 +75,20 @@ config V4L2_HEVC_ENC_RBSP_KUNIT_TEST
 
 	  If unsure, say N.
 
+config V4L2_HEVC_ENC_KUNIT_TEST
+	tristate "KUnit tests for the V4L2 HEVC encode core" if !KUNIT_ALL_TESTS
+	depends on KUNIT && VIDEO_DEV
+	select V4L2_HEVC_ENC
+	select VIDEOBUF2_CORE
+	default KUNIT_ALL_TESTS
+	help
+	  Build the KUnit tests of the stateless HEVC encode core. They run
+	  short sequences of pictures through it and check the reference
+	  picture sets, reference picture lists, decoded picture buffer and
+	  feedback that result, and the validation of the encode parameters.
+
+	  If unsure, say N.
+
 # Used by drivers that need v4l2-vp9.ko
 config V4L2_VP9
 	tristate
drivers/media/v4l2-core/Makefile +1 -0
index 153b8a6e74b8..d57740d71fe1 100644
@@ -35,6 +35,7 @@ obj-$(CONFIG_V4L2_H264_ENC) += v4l2-h264-enc.o v4l2-h264-enc-rbsp.o \
 obj-$(CONFIG_V4L2_HEVC_ENC) += v4l2-hevc-enc.o
 obj-$(CONFIG_V4L2_HEVC_ENC_RBSP) += v4l2-hevc-enc-rbsp.o
 obj-$(CONFIG_V4L2_HEVC_ENC_RBSP_KUNIT_TEST) += v4l2-hevc-enc-rbsp-kunit.o
+obj-$(CONFIG_V4L2_HEVC_ENC_KUNIT_TEST) += v4l2-hevc-enc-kunit.o
 obj-$(CONFIG_V4L2_ISP) += v4l2-isp.o
 obj-$(CONFIG_V4L2_JPEG_HELPER) += v4l2-jpeg.o
 obj-$(CONFIG_V4L2_MEM2MEM_DEV) += v4l2-mem2mem.o
drivers/media/v4l2-core/v4l2-hevc-enc-kunit.c new file +554 -0

554 lines not on this page · raw message

Signature · 1 line
2.55.0
#7 lore raw

RFC PATCH 7/10 media: v4l2-hevc-enc: Add a constant bitrate mode

In reply to Arun Iyengar

The HEVC encode core leaves the constant bitrate mode to the drivers, so each driver needs its own frame rate control.

Provide the mode in the core with the codec independent rate control of the H.264 encode core (v4l2-enc-cbr): one QP state for the stream, I pictures 4 QP below the P pictures (the default of the Rockchip MPP library for HEVC), picture size targets from a GOP plan with the I/P size ratio and IDR period measured on the stream, and the deviation from that plan spread over the next pictures. The size target of the current picture is available to drivers whose hardware also controls the rate within a picture.

A driver rate control with an estimate callback still replaces the mode.

---
 drivers/media/v4l2-core/Kconfig         |  1 +
 drivers/media/v4l2-core/v4l2-hevc-enc.c | 35 +++++++++++++++++++++++++++++++++
 include/media/v4l2-hevc-enc.h           | 11 +++++++++--
 3 files changed, 45 insertions(+), 2 deletions(-)
drivers/media/v4l2-core/Kconfig +1 -0
index eb245deb4622..0de0bad58791 100644
@@ -60,6 +60,7 @@ config V4L2_HEVC_ENC_RBSP
 # Used by drivers that need v4l2-hevc-enc.ko
 config V4L2_HEVC_ENC
 	tristate
+	select V4L2_ENC_CBR
 	select V4L2_HEVC_ENC_RBSP
 
 config V4L2_HEVC_ENC_RBSP_KUNIT_TEST
drivers/media/v4l2-core/v4l2-hevc-enc.c +35 -0
index e2aa7d2da96c..a167c4af6288 100644
@@ -18,6 +18,7 @@
 #include <linux/sort.h>
 #include <linux/v4l2-controls.h>
 #include <media/v4l2-ctrls.h>
+#include <media/v4l2-enc-cbr.h>
 #include <media/v4l2-hevc.h>
 #include <media/v4l2-hevc-enc.h>
 #include <media/v4l2-hevc-enc-rbsp.h>
@@ -26,6 +27,12 @@
 #define RBSP_UPDATE_AUD			BIT(0)
 #define RBSP_UPDATE_PARAMETER_SETS	BIT(1)
 
+/*
+ * Constant bitrate mode: I pictures 4 QP below the P pictures, the default
+ * of the Rockchip MPP library for HEVC.
+ */
+#define RC_CBR_QP_DELTA_INTRA		4
+
 /* Table A.8: MaxLumaPs and MaxLumaSr (main tier is enough to pick a level) */
 static const struct {
 	u8 level_idc;
@@ -150,6 +157,7 @@ int v4l2_hevc_enc_init(struct v4l2_hevc_enc *enc)
 	memset(&enc->state_next, 0, sizeof(enc->state_next));
 	memset(&enc->ref, 0, sizeof(enc->ref));
 	enc->state_serial = 0;
+	v4l2_enc_cbr_reset(&enc->cbr, RC_CBR_QP_DELTA_INTRA);
 
 	if (!enc->max_l0_refs)
 		enc->max_l0_refs = 1;
@@ -1052,6 +1060,18 @@ static int ref_step(struct v4l2_hevc_enc *enc)
 	return 0;
 }
 
+/*
+ * The core provides the constant bitrate mode unless the driver has its own
+ * rate control.
+ */
+static bool rc_core_cbr(const struct v4l2_hevc_enc *enc,
+			const struct v4l2_hevc_enc_state *state)
+{
+	return state->frame_rc_enable &&
+	       state->bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
+	       !(enc->rc_ops && enc->rc_ops->estimate);
+}
+
 static int rc_step(struct v4l2_hevc_enc *enc)
 {
 	struct v4l2_hevc_enc_state *state = &enc->state_active;
@@ -1074,6 +1094,17 @@ static int rc_step(struct v4l2_hevc_enc *enc)
 		if (range)
 			qp -= (state->quality - state->quality_min) *
 			      (state->qp_max - state->qp_min) / range;
+	} else if (rc_core_cbr(enc, state)) {
+		ret = v4l2_enc_cbr_begin(&enc->cbr, state->bitrate,
+					 &state->timeperframe,
+					 state->sps.pic_width_in_luma_samples *
+					 state->sps.pic_height_in_luma_samples,
+					 slice_type == V4L2_HEVC_SLICE_TYPE_I,
+					 state->qp_min, state->qp_max);
+		if (ret)
+			return ret;
+
+		qp = v4l2_enc_cbr_qp(&enc->cbr);
 	} else {
 		if (!enc->rc_ops || !enc->rc_ops->estimate)
 			return -EOPNOTSUPP;
@@ -1382,6 +1413,10 @@ int v4l2_hevc_enc_complete(struct v4l2_hevc_enc *enc,
 	    !v4l2_hevc_nal_is_sub_layer_non_ref(nal_unit_type))
 		enc->ref.prev_tid0_poc = encode->pic_order_cnt_val;
 
+	if (rc_core_cbr(enc, state))
+		v4l2_enc_cbr_end(&enc->cbr, (u64)bytesused * 8,
+				 &state->timeperframe, state->qp_min,
+				 state->qp_max);
 	if (enc->rc_ops && enc->rc_ops->complete)
 		enc->rc_ops->complete(enc, state, bytesused, stats);
 
include/media/v4l2-hevc-enc.h +9 -2
index 4797102e34b3..3db131370c56 100644
@@ -9,6 +9,7 @@
 #include <linux/types.h>
 #include <linux/v4l2-controls.h>
 #include <linux/videodev2.h>
+#include <media/v4l2-enc-cbr.h>
 #include <media/v4l2-hevc-enc-rbsp.h>
 
 struct v4l2_ctrl_handler;
@@ -186,8 +187,10 @@ struct v4l2_hevc_enc_ops {
 /**
  * struct v4l2_hevc_enc_rc_ops - driver rate control
  *
- * The core selects the QP itself for the direct QP and constant quality
- * modes. These callbacks provide the constant and variable bitrate modes.
+ * The core selects the QP itself for the direct QP, constant quality and
+ * constant bitrate modes. These callbacks replace the constant bitrate
+ * mode of the core (if @estimate is set) and provide the variable bitrate
+ * mode.
  *
  * @estimate: return the QP of the next picture in @qp
  * @complete: account for a completed picture
@@ -231,6 +234,9 @@ struct v4l2_hevc_enc_rc_ops {
  * @rbsp: NAL unit writer
  * @slice: slice segment header of the current picture
  * @qp: SliceQpY of the current picture
+ * @cbr: constant bitrate rate control of the core. In that mode,
+ *	 @cbr.target_bits is the size target of the current picture in bits,
+ *	 for hardware with a rate control within the picture.
  * @header_size: number of bytes the core wrote at the start of the
  *		 CAPTURE buffer. The hardware writes from that offset.
  * @parameter_sets_pending: parameter sets were written for a picture that
@@ -261,6 +267,7 @@ struct v4l2_hevc_enc {
 	struct v4l2_hevc_enc_rbsp rbsp;
 	struct v4l2_hevc_slice_header slice;
 	int qp;
+	struct v4l2_enc_cbr cbr;
 	unsigned int header_size;
 	bool parameter_sets_pending;
 
Signature · 1 line
2.55.0
#8 lore raw

RFC PATCH 8/10 media: v4l2-hevc-enc: Add QP limits for the I pictures

In reply to Arun Iyengar

The frame rate control of the core limits all pictures to the range of V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP and V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP. The I pictures, coded several QP steps below the P pictures, then reach very low QP values at high bitrates, where they cost many bits for little quality.

Take the limits of the I pictures from V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP and V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP when the driver has them, with a frame rate control. Without these controls, nothing changes.

---
 drivers/media/v4l2-core/v4l2-hevc-enc.c | 24 +++++++++++++++++++++---
 include/media/v4l2-hevc-enc.h           |  7 +++++++
 2 files changed, 28 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-hevc-enc.c +21 -3
index a167c4af6288..2e0ba832b868 100644
@@ -645,6 +645,12 @@ static void prepare_rc(struct v4l2_hevc_enc *enc,
 	state->qp_min = enc_ctrl_val(enc, V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP,
 				     -qp_bd_offset);
 	state->qp_max = enc_ctrl_val(enc, V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP, 51);
+	state->qp_min_i = enc_ctrl_val(enc,
+				       V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP,
+				       state->qp_min);
+	state->qp_max_i = enc_ctrl_val(enc,
+				       V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP,
+				       state->qp_max);
 	state->qp_i = enc_ctrl_val(enc, V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP, 30);
 	state->qp_p = enc_ctrl_val(enc, V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP,
 				   state->qp_i);
@@ -1072,10 +1078,23 @@ static bool rc_core_cbr(const struct v4l2_hevc_enc *enc,
 	       !(enc->rc_ops && enc->rc_ops->estimate);
 }
 
+/* QP limits of the picture: the I picture ones with a frame rate control */
+static int rc_clamp_qp(const struct v4l2_hevc_enc_state *state, int qp)
+{
+	int qp_bd_offset = 6 * state->sps.bit_depth_luma_minus8;
+
+	if (state->frame_rc_enable &&
+	    state->encode.slice_type == V4L2_HEVC_SLICE_TYPE_I)
+		qp = clamp(qp, state->qp_min_i, state->qp_max_i);
+	else
+		qp = clamp(qp, state->qp_min, state->qp_max);
+
+	return clamp(qp, -qp_bd_offset, 51);
+}
+
 static int rc_step(struct v4l2_hevc_enc *enc)
 {
 	struct v4l2_hevc_enc_state *state = &enc->state_active;
-	int qp_bd_offset = 6 * state->sps.bit_depth_luma_minus8;
 	u8 slice_type = state->encode.slice_type;
 	unsigned int range;
 	int qp, ret;
@@ -1114,8 +1133,7 @@ static int rc_step(struct v4l2_hevc_enc *enc)
 			return ret;
 	}
 
-	qp = clamp(qp, state->qp_min, state->qp_max);
-	enc->qp = clamp(qp, -qp_bd_offset, 51);
+	enc->qp = rc_clamp_qp(state, qp);
 
 	return 0;
 }
include/media/v4l2-hevc-enc.h +7 -0
index 3db131370c56..f1d3da3269f1 100644
@@ -128,6 +128,11 @@ struct v4l2_hevc_enc_ref {
  * @frame_rc_enable: V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE
  * @qp_min: V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP
  * @qp_max: V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP
+ * @qp_min_i: V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MIN_QP, @qp_min if the driver
+ *	      has no such control. Limits the I pictures with a frame rate
+ *	      control instead of @qp_min.
+ * @qp_max_i: V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_MAX_QP, @qp_max if the driver
+ *	      has no such control
  * @qp_i: V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP
  * @qp_p: V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP
  * @qp_b: V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP
@@ -153,6 +158,8 @@ struct v4l2_hevc_enc_state {
 	bool frame_rc_enable;
 	int qp_min;
 	int qp_max;
+	int qp_min_i;
+	int qp_max_i;
 	int qp_i;
 	int qp_p;
 	int qp_b;
Signature · 1 line
2.55.0
#9 lore raw

RFC PATCH 9/10 media: v4l2-hevc-enc: Re-encode oversized pictures

In reply to Arun Iyengar

A scene change in a P picture can make it many times larger than its size target, and the peak bitrate around it can then exceed what a decoder buffer of the stream takes. v4l2-enc-cbr can ask for a second pass of such a picture at a higher QP (v4l2_enc_cbr_reencode()).

Provide v4l2_hevc_enc_reencode() in the HEVC encode core for drivers whose hardware writes the slice header: called after the hardware finished a picture and before v4l2_hevc_enc_complete(), it returns true with the new QP in enc->qp when the constant bitrate mode wants the picture encoded again. Hardware that takes the slice header from the core cannot use it:
that header holds the QP of the first pass.

---
 drivers/media/v4l2-core/v4l2-hevc-enc.c | 39 +++++++++++++++++++++++++++++++++
 include/media/v4l2-hevc-enc.h           |  2 ++
 2 files changed, 41 insertions(+)
drivers/media/v4l2-core/v4l2-hevc-enc.c +39 -0
index 2e0ba832b868..d49cee336558 100644
@@ -1369,6 +1369,45 @@ static int ref_complete(struct v4l2_hevc_enc *enc, u64 timestamp)
 	return 0;
 }
 
+/**
+ * v4l2_hevc_enc_reencode() - decide on encoding the picture a second time
+ * @enc: the core context
+ * @bytesused: size of the encoded picture in bytes, with the headers the core
+ *	       wrote
+ *
+ * In the constant bitrate mode of the core, a picture far above its size
+ * target can be encoded again with a higher QP (see v4l2_enc_cbr_reencode()).
+ * Drivers that can run the hardware again for the same picture call it after
+ * the hardware finished it and before v4l2_hevc_enc_complete(). If it returns
+ * true, @enc->qp holds the new QP and the driver encodes the picture again,
+ * with the same size target and the same headers, writing the slice data
+ * from the same offset. Only for hardware that writes the slice header
+ * (V4L2_HEVC_ENC_FLAG_HW_SLICE_HEADER): the slice header of the core holds the
+ * QP of the first pass.
+ *
+ * Return: true if the picture is to be encoded again.
+ */
+bool v4l2_hevc_enc_reencode(struct v4l2_hevc_enc *enc, unsigned int bytesused)
+{
+	struct v4l2_hevc_enc_state *state = &enc->state_active;
+	bool intra = state->encode.slice_type == V4L2_HEVC_SLICE_TYPE_I;
+	int qp_max = intra ? state->qp_max_i : state->qp_max;
+
+	if (!(enc->flags & V4L2_HEVC_ENC_FLAG_HW_SLICE_HEADER) ||
+	    !rc_core_cbr(enc, state))
+		return false;
+
+	if (!v4l2_enc_cbr_reencode(&enc->cbr, (u64)bytesused * 8,
+				   &state->timeperframe, enc->qp,
+				   min(qp_max, 51)))
+		return false;
+
+	enc->qp = rc_clamp_qp(state, v4l2_enc_cbr_qp(&enc->cbr));
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(v4l2_hevc_enc_reencode);
+
 /**
  * v4l2_hevc_enc_complete() - complete the encode run of a picture
  * @enc: the core context
include/media/v4l2-hevc-enc.h +2 -0
index f1d3da3269f1..5be449e5ec03 100644
@@ -285,6 +285,8 @@ int v4l2_hevc_enc_init(struct v4l2_hevc_enc *enc);
 void v4l2_hevc_enc_exit(struct v4l2_hevc_enc *enc);
 int v4l2_hevc_enc_step(struct v4l2_hevc_enc *enc,
 		       struct vb2_v4l2_buffer *buffer);
+bool v4l2_hevc_enc_reencode(struct v4l2_hevc_enc *enc,
+			    unsigned int bytesused);
 int v4l2_hevc_enc_complete(struct v4l2_hevc_enc *enc,
 			   struct vb2_v4l2_buffer *buffer,
 			   const struct v4l2_stateless_encode_stats *stats);
Signature · 1 line
2.55.0
#10 lore raw

RFC PATCH 10/10 media: docs: Document the HEVC stateless encode uAPI

In reply to Arun Iyengar

Describe how a stateless HEVC encoder is used: the CAPTURE format and what each buffer receives, which parts of the parameter sets the kernel derives from the format, frame interval and stateful codec controls, how the QP is selected, how reference pictures are identified and kept, and how per-picture feedback is read back from completed requests.

Document the V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS and V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK controls with their structures and flags, and the codec-agnostic struct v4l2_stateless_encode_stats, and add the new control types to the VIDIOC_QUERYCTRL and VIDIOC_G_EXT_CTRLS references.

---
 .../media/v4l/ext-ctrls-codec-stateless.rst        | 411 +++++++++++++++++++++
 .../userspace-api/media/v4l/pixfmt-compressed.rst  |   3 +
 .../media/v4l/videodev2.h.rst.exceptions           |   2 +
 .../userspace-api/media/v4l/vidioc-g-ext-ctrls.rst |   8 +
 .../userspace-api/media/v4l/vidioc-queryctrl.rst   |  12 +
 5 files changed, 436 insertions(+)
Documentation/userspace-api/media/v4l/ext-ctrls-codec-stateless.rst +411 -0
index d7a3b8ef03a7..177940a6f987 100644
@@ -3079,6 +3079,417 @@ This structure contains all loop filter related parameters. See sections
       - Specifies if the short-term RPS is predicted from another short term RPS. See details in
         section 7.4.8 "Short-term reference picture set semantics" of the specification.
 
+.. _v4l2-codec-stateless-hevc-encode:
+
+.. rubric:: HEVC stateless encoding
+
+A stateless HEVC encoder exposes ``V4L2_PIX_FMT_HEVC_SLICE`` on its CAPTURE
+queue. Each CAPTURE buffer receives one complete access unit, with the start
+codes selected by ``V4L2_CID_STATELESS_HEVC_START_CODE``. Applications set
+the sequence and picture parameters with the ``V4L2_CID_STATELESS_HEVC_SPS``
+and ``V4L2_CID_STATELESS_HEVC_PPS`` controls, and the parameters of each
+picture with ``V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS``, in the request of the
+OUTPUT buffer that holds the picture to encode.
+
+The kernel writes the video, sequence and picture parameter sets, and the
+slice segment headers, unless the hardware writes them itself. It derives the
+parts of the parameter sets that the SPS and PPS controls do not carry from
+the rest of the configuration:
+
+- the coded size from the CAPTURE format, aligned to the minimum coding block
+  size, and the conformance window from the difference with the visible size;
+- the profile, tier and level from ``V4L2_CID_MPEG_VIDEO_HEVC_PROFILE``,
+  ``V4L2_CID_MPEG_VIDEO_HEVC_TIER`` and ``V4L2_CID_MPEG_VIDEO_HEVC_LEVEL``
+  when the encoder exposes them (otherwise the lowest level that fits the
+  coded size and frame rate);
+- the VUI timing information from the frame interval set with
+  :ref:`VIDIOC_S_PARM <VIDIOC_G_PARM>`, and the colour description from the
+  colorimetry of the format.
+
+The values of the SPS and PPS controls are adjusted to what the encoder
+supports, and can be read back after the encode run. A changed SPS only takes
+effect with the next IRAP picture.
+
+The quantisation parameter comes from the stateful codec controls: from
+``V4L2_CID_MPEG_VIDEO_HEVC_I_FRAME_QP``, ``V4L2_CID_MPEG_VIDEO_HEVC_P_FRAME_QP``
+and ``V4L2_CID_MPEG_VIDEO_HEVC_B_FRAME_QP`` when
+``V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE`` is 0, otherwise from the rate control
+mode selected by ``V4L2_CID_MPEG_VIDEO_BITRATE_MODE``, within
+``V4L2_CID_MPEG_VIDEO_HEVC_MIN_QP`` and ``V4L2_CID_MPEG_VIDEO_HEVC_MAX_QP``.
+These controls can be part of the request too.
+
+Reference pictures are identified by the timestamp of the OUTPUT buffer they
+were encoded from. The encoder keeps the reconstructed pictures internally:
+a picture is kept when it is encoded with
+``V4L2_HEVC_ENCODE_FLAG_REFERENCE`` and as long as each following picture
+lists it in its reference picture set. When an encode run fails, the set of
+kept pictures does not change.
+
+How each picture was encoded is reported by the read-only
+``V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK`` control. Applications do not set
+it in requests, so its value at the end of the encode run is stored in the
+request, and can be read with :ref:`VIDIOC_G_EXT_CTRLS <VIDIOC_G_EXT_CTRLS>`
+once the request completed.
+
+``V4L2_CID_STATELESS_HEVC_ENCODE_PARAMS (struct)``
+    Specifies the parameters of the picture to encode: its NAL unit header,
+    the slice segment header syntax elements that are not derived from other
+    controls, and its reference picture set. These parameters are defined
+    according to :ref:`hevc`, sections 7.3.1.2 "NAL unit header syntax" and
+    7.3.6 "Slice segment header syntax". When the picture is encoded as
+    several slices, all of them use these parameters.
+
+.. c:type:: v4l2_ctrl_hevc_encode_params
+
+.. cssclass:: longtable
+
+.. flat-table:: struct v4l2_ctrl_hevc_encode_params
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - __u8
+      - ``nal_unit_type``
+      - NAL unit type of the slice segments of the picture, from Table 7-1 of
+        the specification: one of the VCL types 0 (TRAIL_N) to 9 (RASL_R)
+        and 16 (BLA_W_LP) to 21 (CRA_NUT). The first picture is an IRAP
+        picture (16 to 21).
+    * - __u8
+      - ``nuh_temporal_id_plus1``
+      - TemporalId of the picture plus 1, from 1 to
+        ``sps_max_sub_layers_minus1`` + 1. It is 1 for IRAP pictures and
+        greater than 1 for TSA and STSA pictures.
+    * - __u8
+      - ``slice_type``
+      - ``V4L2_HEVC_SLICE_TYPE_I``, ``V4L2_HEVC_SLICE_TYPE_P`` or
+        ``V4L2_HEVC_SLICE_TYPE_B``. IRAP pictures are I pictures. An encoder
+        that cannot encode P pictures encodes them as I pictures; one that
+        cannot encode B pictures rejects them.
+    * - __u8
+      - ``pic_parameter_set_id``
+      - Identifies the PPS in use. The encoder supports a single PPS and
+        sets the value of the PPS control.
+    * - __s32
+      - ``pic_order_cnt_val``
+      - PicOrderCntVal of the picture, as described in section 8.3.1
+        "Decoding process for picture order count". It is 0 for IDR
+        pictures, and from 0 to MaxPicOrderCntLsb - 1 for BLA pictures and
+        for a CRA picture that starts the bitstream. Otherwise it differs by
+        less than MaxPicOrderCntLsb / 2 from the picture order count of the
+        previous picture with TemporalId 0 that is not a RASL, RADL or
+        sub-layer non-reference picture, so that a decoder can derive it.
+    * - __u8
+      - ``num_ref_idx_l0_active_minus1``
+      - Number of entries of reference picture list 0, minus 1, used with
+        ``V4L2_HEVC_ENCODE_FLAG_NUM_REF_IDX_ACTIVE_OVERRIDE``. Otherwise the
+        PPS default applies. The encoder limits it to what it supports.
+    * - __u8
+      - ``num_ref_idx_l1_active_minus1``
+      - Same for reference picture list 1.
+    * - __u8
+      - ``collocated_ref_idx``
+      - Reference index of the collocated picture used for temporal motion
+        vector prediction, in list 0 when
+        ``V4L2_HEVC_ENCODE_FLAG_COLLOCATED_FROM_L0`` is set, in list 1
+        otherwise.
+    * - __u8
+      - ``five_minus_max_num_merge_cand``
+      - Maximum number of merging motion vector prediction candidates
+        subtracted from 5.
+    * - __s8
+      - ``slice_cb_qp_offset``
+      - Difference added to ``pps_cb_qp_offset``, used when the PPS has
+        ``V4L2_HEVC_PPS_FLAG_PPS_SLICE_CHROMA_QP_OFFSETS_PRESENT``.
+    * - __s8
+      - ``slice_cr_qp_offset``
+      - Difference added to ``pps_cr_qp_offset``, used likewise.
+    * - __s8
+      - ``slice_beta_offset_div2``
+      - Deblocking parameter offset for beta divided by 2, used with
+        ``V4L2_HEVC_ENCODE_FLAG_DEBLOCKING_FILTER_OVERRIDE``.
+    * - __s8
+      - ``slice_tc_offset_div2``
+      - Deblocking parameter offset for tC divided by 2, used likewise.
+    * - __u8
+      - ``num_refs``
+      - Number of valid entries in ``refs``, up to
+        ``sps_max_dec_pic_buffering_minus1``.
+    * - __u8
+      - ``reserved[7]``
+      - Applications and drivers must set this to zero.
+    * - struct :c:type:`v4l2_hevc_encode_ref`
+      - ``refs[V4L2_HEVC_DPB_ENTRIES_NUM_MAX]``
+      - The reference picture set of the picture: every previously encoded
+        picture that the current picture or a following picture uses for
+        prediction, in any order. The encoder computes the picture order
+        count differences, codes the RPS and builds the reference picture
+        lists without modification, as described in section 8.3.4. Pictures
+        that are not listed are removed from the decoded picture buffer.
+        IDR pictures have no reference picture set and BLA pictures discard
+        it. An encoder that uses a single reference picture uses the one
+        entry that is used by the current picture.
+    * - __u64
+      - ``flags``
+      - See :ref:`HEVC Encode Parameters Flags <hevc_encode_params_flags>`
+
+.. c:type:: v4l2_hevc_encode_ref
+
+.. flat-table:: struct v4l2_hevc_encode_ref
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - __u64
+      - ``timestamp``
+      - Timestamp of the OUTPUT buffer from which the reference picture was
+        encoded. Use the :c:func:`v4l2_timeval_to_ns()` function to convert
+        the struct :c:type:`timeval` in struct :c:type:`v4l2_buffer` to a
+        __u64.
+    * - __u8
+      - ``flags``
+      - See :ref:`HEVC Encode Reference Flags <hevc_encode_ref_flags>`
+    * - __u8
+      - ``reserved[7]``
+      - Applications and drivers must set this to zero.
+
+.. _hevc_encode_ref_flags:
+
+``HEVC Encode Reference Flags``
+
+.. cssclass:: longtable
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - ``V4L2_HEVC_ENCODE_REF_FLAG_USED_BY_CURR_PIC``
+      - 0x01
+      - The current picture uses the reference picture for inter prediction
+        (it is in PocStCurrBefore, PocStCurrAfter or PocLtCurr). Otherwise it
+        is only kept for following pictures. P and B pictures have at least
+        one used reference, IRAP pictures have none.
+    * - ``V4L2_HEVC_ENCODE_REF_FLAG_LONG_TERM``
+      - 0x02
+      - The reference picture is a long-term reference picture. This
+        requires ``V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT``. Once a
+        picture is a long-term reference picture, it stays one.
+
+.. _hevc_encode_params_flags:
+
+``HEVC Encode Parameters Flags``
+
+.. cssclass:: longtable
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - ``V4L2_HEVC_ENCODE_FLAG_SLICE_SAO_LUMA``
+      - 0x00000001
+      - slice_sao_luma_flag. Ignored unless the SPS enables SAO.
+    * - ``V4L2_HEVC_ENCODE_FLAG_SLICE_SAO_CHROMA``
+      - 0x00000002
+      - slice_sao_chroma_flag. Ignored unless the SPS enables SAO.
+    * - ``V4L2_HEVC_ENCODE_FLAG_SLICE_TEMPORAL_MVP_ENABLED``
+      - 0x00000004
+      - slice_temporal_mvp_enabled_flag. Ignored unless the SPS enables
+        temporal motion vector prediction.
+    * - ``V4L2_HEVC_ENCODE_FLAG_MVD_L1_ZERO``
+      - 0x00000008
+      - mvd_l1_zero_flag, for B pictures.
+    * - ``V4L2_HEVC_ENCODE_FLAG_CABAC_INIT``
+      - 0x00000010
+      - cabac_init_flag, used when the PPS has
+        ``V4L2_HEVC_PPS_FLAG_CABAC_INIT_PRESENT``.
+    * - ``V4L2_HEVC_ENCODE_FLAG_COLLOCATED_FROM_L0``
+      - 0x00000020
+      - collocated_from_l0_flag, for B pictures. Always set for P pictures.
+    * - ``V4L2_HEVC_ENCODE_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED``
+      - 0x00000080
+      - slice_deblocking_filter_disabled_flag, used with
+        ``V4L2_HEVC_ENCODE_FLAG_DEBLOCKING_FILTER_OVERRIDE``.
+    * - ``V4L2_HEVC_ENCODE_FLAG_SLICE_LOOP_FILTER_ACROSS_SLICES_ENABLED``
+      - 0x00000100
+      - slice_loop_filter_across_slices_enabled_flag.
+    * - ``V4L2_HEVC_ENCODE_FLAG_REFERENCE``
+      - 0x00010000
+      - Keep the reconstructed picture so that following pictures can
+        reference it. Without this flag, the encoder may not store the
+        reconstruction at all.
+    * - ``V4L2_HEVC_ENCODE_FLAG_NO_OUTPUT_OF_PRIOR_PICS``
+      - 0x00020000
+      - no_output_of_prior_pics_flag, for IRAP pictures.
+    * - ``V4L2_HEVC_ENCODE_FLAG_NO_PIC_OUTPUT``
+      - 0x00040000
+      - pic_output_flag is 0, when the PPS has
+        ``V4L2_HEVC_PPS_FLAG_OUTPUT_FLAG_PRESENT``.
+    * - ``V4L2_HEVC_ENCODE_FLAG_NUM_REF_IDX_ACTIVE_OVERRIDE``
+      - 0x00080000
+      - Use ``num_ref_idx_l0_active_minus1`` and
+        ``num_ref_idx_l1_active_minus1`` instead of the PPS defaults.
+    * - ``V4L2_HEVC_ENCODE_FLAG_DEBLOCKING_FILTER_OVERRIDE``
+      - 0x00100000
+      - deblocking_filter_override_flag, used when the PPS has
+        ``V4L2_HEVC_PPS_FLAG_DEBLOCKING_FILTER_OVERRIDE_ENABLED``.
+
+The flags that carry a slice header syntax element have the same values as
+the ``V4L2_HEVC_SLICE_PARAMS_FLAG_*`` flags of the same element.
+
+``V4L2_CID_STATELESS_HEVC_ENCODE_FEEDBACK (struct)``
+    Read-only. Describes how a picture was encoded: the values the encoder
+    used where it adjusted or derived them, and statistics measured while
+    encoding. The encoder sets it when the encode run completes, so that it
+    is stored in the request.
+
+.. c:type:: v4l2_ctrl_hevc_encode_feedback
+
+.. cssclass:: longtable
+
+.. flat-table:: struct v4l2_ctrl_hevc_encode_feedback
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - __u64
+      - ``timestamp``
+      - Timestamp of the OUTPUT buffer that was encoded.
+    * - __s32
+      - ``pic_order_cnt_val``
+      - Picture order count of the picture.
+    * - __u8
+      - ``nal_unit_type``
+      - NAL unit type of the slice segments.
+    * - __u8
+      - ``nuh_temporal_id_plus1``
+      - TemporalId of the picture plus 1.
+    * - __u8
+      - ``slice_type``
+      - Slice type that was used.
+    * - __s8
+      - ``slice_qp``
+      - Luma quantisation parameter of the first slice (SliceQpY).
+    * - __u8
+      - ``num_ref_idx_l0_active``
+      - Number of entries in reference picture list 0.
+    * - __u8
+      - ``num_ref_idx_l1_active``
+      - Number of entries in reference picture list 1.
+    * - __u8
+      - ``reserved0[2]``
+      - Applications and drivers must set this to zero.
+    * - __u8
+      - ``ref_idx_l0[V4L2_HEVC_DPB_ENTRIES_NUM_MAX]``
+      - For each entry of reference picture list 0, its index in the
+        ``refs`` array of the encode parameters.
+    * - __u8
+      - ``ref_idx_l1[V4L2_HEVC_DPB_ENTRIES_NUM_MAX]``
+      - Same for reference picture list 1.
+    * - __u8
+      - ``reserved1[4]``
+      - Applications and drivers must set this to zero.
+    * - struct :c:type:`v4l2_stateless_encode_stats`
+      - ``stats``
+      - Statistics measured while encoding the picture.
+    * - __u64
+      - ``flags``
+      - See :ref:`HEVC Encode Feedback Flags <hevc_encode_feedback_flags>`
+
+.. _hevc_encode_feedback_flags:
+
+``HEVC Encode Feedback Flags``
+
+.. cssclass:: longtable
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - ``V4L2_HEVC_ENCODE_FEEDBACK_FLAG_REFERENCE``
+      - 0x00000001
+      - The reconstructed picture was kept for reference.
+
+.. c:type:: v4l2_stateless_encode_stats
+
+.. cssclass:: longtable
+
+.. flat-table:: struct v4l2_stateless_encode_stats
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - __u32
+      - ``flags``
+      - Which statistics are valid, see
+        :ref:`Stateless Encode Statistics Flags <stateless_encode_stats_flags>`.
+        The fields of the statistics that are not valid are zero.
+    * - __u32
+      - ``header_size``
+      - Number of bytes written by the kernel at the start of the CAPTURE
+        buffer (parameter sets, access unit delimiter, slice header) before
+        the data produced by the hardware. Always valid.
+    * - __u32
+      - ``qp_sum``
+      - Sum of the luma quantisation parameters of the blocks counted in
+        ``qp_count``.
+    * - __u32
+      - ``qp_count``
+      - Number of blocks summed in ``qp_sum``: ``qp_sum`` / ``qp_count`` is
+        the average QP of the picture.
+    * - __s8
+      - ``qp_min``
+      - Smallest luma quantisation parameter used in the picture.
+    * - __s8
+      - ``qp_max``
+      - Largest luma quantisation parameter used in the picture.
+    * - __u8
+      - ``reserved0[6]``
+      - Applications and drivers must set this to zero.
+    * - __u64
+      - ``sse_luma``
+      - Sum of squared errors between the source and reconstructed luma
+        samples, from which the PSNR of the picture can be computed.
+    * - __u32
+      - ``num_intra_blocks``
+      - Number of 8x8 luma blocks coded with intra prediction.
+    * - __u32
+      - ``num_inter_blocks``
+      - Number of 8x8 luma blocks coded with inter prediction, skipped
+        blocks included.
+    * - __u32
+      - ``num_skip_blocks``
+      - Number of 8x8 luma blocks coded in skip mode, counted in
+        ``num_inter_blocks`` too.
+    * - __u32
+      - ``reserved1``
+      - Applications and drivers must set this to zero.
+
+.. _stateless_encode_stats_flags:
+
+``Stateless Encode Statistics Flags``
+
+.. cssclass:: longtable
+
+.. flat-table::
+    :header-rows:  0
+    :stub-columns: 0
+    :widths:       1 1 2
+
+    * - ``V4L2_STATELESS_ENCODE_STATS_FLAG_QP``
+      - 0x00000001
+      - ``qp_sum``, ``qp_count``, ``qp_min`` and ``qp_max`` are valid.
+    * - ``V4L2_STATELESS_ENCODE_STATS_FLAG_SSE_LUMA``
+      - 0x00000002
+      - ``sse_luma`` is valid.
+    * - ``V4L2_STATELESS_ENCODE_STATS_FLAG_BLOCKS``
+      - 0x00000004
+      - ``num_intra_blocks`` and ``num_inter_blocks`` are valid.
+    * - ``V4L2_STATELESS_ENCODE_STATS_FLAG_SKIP_BLOCKS``
+      - 0x00000008
+      - ``num_skip_blocks`` is valid.
+
 .. _v4l2-codec-stateless-av1:
 
 ``V4L2_CID_STATELESS_AV1_SEQUENCE (struct)``
Documentation/userspace-api/media/v4l/pixfmt-compressed.rst +3 -0
index 235f955d3cd5..7483444ec823 100644
@@ -221,6 +221,9 @@ Compressed Formats
 	See the :ref:`associated Codec Control IDs <v4l2-codec-stateless-hevc>`.
 	Buffers associated with this pixel format must contain the appropriate
 	number of macroblocks to decode a full corresponding frame.
+	Stateless video encoders use this pixel format on their CAPTURE queue:
+	each buffer then receives one complete access unit, as described in
+	:ref:`HEVC stateless encoding <v4l2-codec-stateless-hevc-encode>`.
     * .. _V4L2-PIX-FMT-FWHT:
 
       - ``V4L2_PIX_FMT_FWHT``
Documentation/userspace-api/media/v4l/videodev2.h.rst.exceptions +2 -0
index 6182b4e2d2ee..6c8ac563413f 100644
@@ -168,6 +168,8 @@ replace symbol V4L2_CTRL_TYPE_HEVC_PPS :c:type:`V4L.v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS :c:type:`V4L.v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_HEVC_SCALING_MATRIX :c:type:`V4L.v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS :c:type:`V4L.v4l2_ctrl_type`
+replace symbol V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS :c:type:`V4L.v4l2_ctrl_type`
+replace symbol V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK :c:type:`V4L.v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_AV1_SEQUENCE :c:type:`V4L.v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_AV1_TILE_GROUP_ENTRY :c:type:`V4L.v4l2_ctrl_type`
 replace symbol V4L2_CTRL_TYPE_AV1_FRAME :c:type:`V4L.v4l2_ctrl_type`
Documentation/userspace-api/media/v4l/vidioc-g-ext-ctrls.rst +8 -0
index d78328152b75..0cc67e210359 100644
@@ -286,6 +286,14 @@ still cause this situation.
       - ``p_hevc_decode_params``
       - A pointer to a struct :c:type:`v4l2_ctrl_hevc_decode_params`. Valid if this
         control is of type ``V4L2_CTRL_TYPE_HEVC_DECODE_PARAMS``.
+    * - struct :c:type:`v4l2_ctrl_hevc_encode_params` *
+      - ``p_hevc_encode_params``
+      - A pointer to a struct :c:type:`v4l2_ctrl_hevc_encode_params`. Valid if this
+        control is of type ``V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS``.
+    * - struct :c:type:`v4l2_ctrl_hevc_encode_feedback` *
+      - ``p_hevc_encode_feedback``
+      - A pointer to a struct :c:type:`v4l2_ctrl_hevc_encode_feedback`. Valid if this
+        control is of type ``V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK``.
     * - struct :c:type:`v4l2_ctrl_av1_sequence` *
       - ``p_av1_sequence``
       - A pointer to a struct :c:type:`v4l2_ctrl_av1_sequence`. Valid if this control is
Documentation/userspace-api/media/v4l/vidioc-queryctrl.rst +12 -0
index 82c8b52e771c..cd9ef6b559a4 100644
@@ -543,6 +543,18 @@ See also the examples in :ref:`control`.
       - n/a
       - A struct :c:type:`v4l2_ctrl_hevc_ext_sps_st_rps`, containing HEVC
 	extended Short-Term RPS for stateless video decoders.
+    * - ``V4L2_CTRL_TYPE_HEVC_ENCODE_PARAMS``
+      - n/a
+      - n/a
+      - n/a
+      - A struct :c:type:`v4l2_ctrl_hevc_encode_params`, containing HEVC
+	encoding parameters for stateless video encoders.
+    * - ``V4L2_CTRL_TYPE_HEVC_ENCODE_FEEDBACK``
+      - n/a
+      - n/a
+      - n/a
+      - A struct :c:type:`v4l2_ctrl_hevc_encode_feedback`, containing HEVC
+	encoding feedback from stateless video encoders.
     * - ``V4L2_CTRL_TYPE_VP9_COMPRESSED_HDR``
       - n/a
       - n/a
Signature · 1 line
2.55.0
#11 lore raw
In reply to Arun Iyengar

Please don't review this series: it depends on my H.264 follow-ups RFC, which I have dropped, and it should have been a summary, as Paul asked there. The two ideas in it:

- References: each picture lists its reference pictures as OUTPUT buffer timestamps, marked used and/or long-term. The kernel codes the RPS and builds the reference lists from that.
- A read-only feedback control returns the QP, the resolved lists and hardware statistics with each request.

It ran on one encoder only (Rockchip VEPU580).

Arun