* [PATCH v2 0/3] media: stateless MPEG-1 decoding, cedrus support
@ 2026-11-21 10:10 Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 1/3] media: uapi: mpeg2: add MPEG-1 flags Arun Iyengar via B4 Relay
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-21 10:10 UTC (permalink / raw)
To: Hans Verkuil, Nicolas Dufresne, Mauro Carvalho Chehab,
Benjamin Gaignard, Philipp Zabel, Maxime Ripard,
Paul Kocialkowski, Greg Kroah-Hartman, Chen-Yu Tsai,
Jernej Skrabec, Samuel Holland
Cc: Jonas Karlman, linux-media, linux-kernel, linux-rockchip,
linux-staging, linux-arm-kernel, linux-sunxi,
Christian Stefan Kövér-Draxl, Arun Iyengar
Builds on Jernej Skrabec's cedrus work; tested with an FFmpeg hwaccel
on top of Jonas Karlman's V4L2 request API series (FFmpeg PR 20847).
MPEG-1 decoded as MPEG-2 is wrong: the mismatch control differs, and
MPEG-1 has full pel motion vectors and D-pictures. Patch 1 adds
V4L2_MPEG2_SEQ_FLAG_MPEG1 and two full pel picture flags to the MPEG-2
controls and allows D-pictures; patch 2 makes hantro reject them
(compile tested only). Patch 3 implements them in cedrus.
Tested on H616 only, on media-committers next: MPEG-2, H.264 and H.265
decode bit-exact against FFmpeg software decoding. MPEG-1 streams with
B-pictures and full pel vectors differ from FFmpeg (-idct faani) by at
most 2 in I-, P- and B-pictures; FFmpeg's own IDCTs differ by up to 3
on the same streams. The full pel reference needs an FFmpeg fix for
skipped B macroblocks. MPEG-1 D-pictures are bit-exact against a
computed DC reference; stock v4l2-compliance 49 of 49.
Developed with Claude (Anthropic) as a coding assistant, which wrote
the code and the commit messages. I reviewed and tested them as
described above.
Changes in v2:
- Drop V4L2_PIX_FMT_MPEG1_SLICE: cedrus selects MPEG-1 from
V4L2_MPEG2_SEQ_FLAG_MPEG1 alone (Nicolas)
- hantro reject in its own patch
- Link to v1: https://lore.kernel.org/r/20261119-b4-cedrus-mpeg1-v1-0-aa5ba1192822@gmail.com
---
Arun Iyengar (3):
media: uapi: mpeg2: add MPEG-1 flags
media: verisilicon: reject MPEG-1 controls
media: cedrus: mpeg2: decode MPEG-1
.../media/v4l/ext-ctrls-codec-stateless.rst | 34 ++++++++++++++++++++--
drivers/media/platform/verisilicon/hantro_drv.c | 12 ++++++++
drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 +
drivers/staging/media/sunxi/cedrus/cedrus.h | 3 ++
drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c | 27 ++++++++++++++---
include/uapi/linux/v4l2-controls.h | 3 ++
6 files changed, 74 insertions(+), 6 deletions(-)
---
base-commit: ccf78740d63813c452c035b57d17a650492562fa
change-id: 20261114-b4-cedrus-mpeg1-8baae5a2eefe
Best regards,
--
Arun Iyengar <aiyengar1990@gmail.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/3] media: uapi: mpeg2: add MPEG-1 flags
2026-11-21 10:10 [PATCH v2 0/3] media: stateless MPEG-1 decoding, cedrus support Arun Iyengar via B4 Relay
@ 2026-11-21 10:10 ` Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 2/3] media: verisilicon: reject MPEG-1 controls Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 3/3] media: cedrus: mpeg2: decode MPEG-1 Arun Iyengar via B4 Relay
2 siblings, 0 replies; 4+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-21 10:10 UTC (permalink / raw)
To: Hans Verkuil, Nicolas Dufresne, Mauro Carvalho Chehab,
Benjamin Gaignard, Philipp Zabel, Maxime Ripard,
Paul Kocialkowski, Greg Kroah-Hartman, Chen-Yu Tsai,
Jernej Skrabec, Samuel Holland
Cc: Jonas Karlman, linux-media, linux-kernel, linux-rockchip,
linux-staging, linux-arm-kernel, linux-sunxi,
Christian Stefan Kövér-Draxl, Arun Iyengar
From: Arun Iyengar <aiyengar1990@gmail.com>
Stateless MPEG-2 decoders can decode MPEG-1, but the uAPI cannot say a
stream is MPEG-1, and decoding it as MPEG-2 is wrong: MPEG-1 has its own
inverse quantisation mismatch control, full pel motion vectors and
D-pictures.
Add V4L2_MPEG2_SEQ_FLAG_MPEG1, V4L2_MPEG2_PIC_FLAG_FULL_PEL_FORWARD and
_BACKWARD, and allow V4L2_MPEG2_PIC_CODING_TYPE_D for MPEG-1. Drivers
that cannot decode MPEG-1 or D-pictures reject them, also in
VIDIOC_TRY_EXT_CTRLS. Kernels without this change accept the flag and
decode the stream as MPEG-2.
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
.../media/v4l/ext-ctrls-codec-stateless.rst | 34 ++++++++++++++++++++--
drivers/media/v4l2-core/v4l2-ctrls-core.c | 1 +
include/uapi/linux/v4l2-controls.h | 3 ++
3 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/Documentation/userspace-api/media/v4l/ext-ctrls-codec-stateless.rst b/Documentation/userspace-api/media/v4l/ext-ctrls-codec-stateless.rst
index d7a3b8ef03a7..f51017b5d6f0 100644
--- a/Documentation/userspace-api/media/v4l/ext-ctrls-codec-stateless.rst
+++ b/Documentation/userspace-api/media/v4l/ext-ctrls-codec-stateless.rst
@@ -1317,6 +1317,22 @@ FWHT Flags
- 0x01
- Indication that all the frames for the sequence are progressive instead
of interlaced.
+ * - ``V4L2_MPEG2_SEQ_FLAG_MPEG1``
+ - 0x02
+ - The sequence is an ISO/IEC 11172-2 (MPEG-1) sequence: the inverse
+ quantisation uses the MPEG-1 mismatch control, motion vectors may be
+ full pel and pictures may be D-pictures. Drivers that cannot decode
+ MPEG-1 reject this flag, also in ``VIDIOC_TRY_EXT_CTRLS``. MPEG-1 has
+ no sequence or picture extensions: applications set the fields that
+ come from them to chroma_format 1, V4L2_MPEG2_SEQ_FLAG_PROGRESSIVE,
+ frame pictures with V4L2_MPEG2_PIC_FLAG_FRAME_PRED_DCT and
+ V4L2_MPEG2_PIC_FLAG_PROGRESSIVE, intra_dc_precision 0, the flags
+ V4L2_MPEG2_PIC_FLAG_Q_SCALE_TYPE, V4L2_MPEG2_PIC_FLAG_INTRA_VLC,
+ V4L2_MPEG2_PIC_FLAG_ALT_SCAN, V4L2_MPEG2_PIC_FLAG_CONCEALMENT_MV,
+ V4L2_MPEG2_PIC_FLAG_TOP_FIELD_FIRST and
+ V4L2_MPEG2_PIC_FLAG_REPEAT_FIRST cleared, both components of
+ ``f_code[0]`` set to forward_f_code and of ``f_code[1]`` to
+ backward_f_code.
.. raw:: latex
@@ -1366,8 +1382,12 @@ FWHT Flags
* - __u8
- ``picture_coding_type``
- Picture coding type for the frame covered by the current slice
- (V4L2_MPEG2_PIC_CODING_TYPE_I, V4L2_MPEG2_PIC_CODING_TYPE_P or
- V4L2_MPEG2_PIC_CODING_TYPE_B).
+ (V4L2_MPEG2_PIC_CODING_TYPE_I, V4L2_MPEG2_PIC_CODING_TYPE_P,
+ V4L2_MPEG2_PIC_CODING_TYPE_B, or V4L2_MPEG2_PIC_CODING_TYPE_D in MPEG-1
+ sequences). Decoding D-pictures is optional: drivers that cannot
+ decode them reject V4L2_MPEG2_PIC_CODING_TYPE_D, also in
+ ``VIDIOC_TRY_EXT_CTRLS``, which applications can use to find out
+ whether D-pictures are supported.
* - __u8
- ``picture_structure``
- Picture structure (1: interlaced top field, 2: interlaced bottom field,
@@ -1415,6 +1435,16 @@ FWHT Flags
* - ``V4L2_MPEG2_PIC_FLAG_PROGRESSIVE``
- 0x00000080
- Indicates whether the current frame is progressive.
+ * - ``V4L2_MPEG2_PIC_FLAG_FULL_PEL_FORWARD``
+ - 0x00000100
+ - full_pel_forward_vector of an MPEG-1 picture header: the forward
+ motion vectors are in full pel units. Only valid with
+ V4L2_MPEG2_SEQ_FLAG_MPEG1.
+ * - ``V4L2_MPEG2_PIC_FLAG_FULL_PEL_BACKWARD``
+ - 0x00000200
+ - full_pel_backward_vector of an MPEG-1 picture header: the backward
+ motion vectors are in full pel units. Only valid with
+ V4L2_MPEG2_SEQ_FLAG_MPEG1.
.. raw:: latex
diff --git a/drivers/media/v4l2-core/v4l2-ctrls-core.c b/drivers/media/v4l2-core/v4l2-ctrls-core.c
index 6e751a043a21..a12359626121 100644
--- a/drivers/media/v4l2-core/v4l2-ctrls-core.c
+++ b/drivers/media/v4l2-core/v4l2-ctrls-core.c
@@ -1049,6 +1049,7 @@ static int std_validate_compound(const struct v4l2_ctrl *ctrl, u32 idx,
case V4L2_MPEG2_PIC_CODING_TYPE_I:
case V4L2_MPEG2_PIC_CODING_TYPE_P:
case V4L2_MPEG2_PIC_CODING_TYPE_B:
+ case V4L2_MPEG2_PIC_CODING_TYPE_D:
break;
default:
return -EINVAL;
diff --git a/include/uapi/linux/v4l2-controls.h b/include/uapi/linux/v4l2-controls.h
index d17e41d51d2e..e7e6030ac00c 100644
--- a/include/uapi/linux/v4l2-controls.h
+++ b/include/uapi/linux/v4l2-controls.h
@@ -2007,6 +2007,7 @@ struct v4l2_ctrl_vp8_frame {
/* Stateless MPEG-2 controls */
#define V4L2_MPEG2_SEQ_FLAG_PROGRESSIVE 0x01
+#define V4L2_MPEG2_SEQ_FLAG_MPEG1 0x02
#define V4L2_CID_STATELESS_MPEG2_SEQUENCE (V4L2_CID_CODEC_STATELESS_BASE+220)
/**
@@ -2055,6 +2056,8 @@ struct v4l2_ctrl_mpeg2_sequence {
#define V4L2_MPEG2_PIC_FLAG_ALT_SCAN 0x0020
#define V4L2_MPEG2_PIC_FLAG_REPEAT_FIRST 0x0040
#define V4L2_MPEG2_PIC_FLAG_PROGRESSIVE 0x0080
+#define V4L2_MPEG2_PIC_FLAG_FULL_PEL_FORWARD 0x0100
+#define V4L2_MPEG2_PIC_FLAG_FULL_PEL_BACKWARD 0x0200
#define V4L2_CID_STATELESS_MPEG2_PICTURE (V4L2_CID_CODEC_STATELESS_BASE+221)
/**
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/3] media: verisilicon: reject MPEG-1 controls
2026-11-21 10:10 [PATCH v2 0/3] media: stateless MPEG-1 decoding, cedrus support Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 1/3] media: uapi: mpeg2: add MPEG-1 flags Arun Iyengar via B4 Relay
@ 2026-11-21 10:10 ` Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 3/3] media: cedrus: mpeg2: decode MPEG-1 Arun Iyengar via B4 Relay
2 siblings, 0 replies; 4+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-21 10:10 UTC (permalink / raw)
To: Hans Verkuil, Nicolas Dufresne, Mauro Carvalho Chehab,
Benjamin Gaignard, Philipp Zabel, Maxime Ripard,
Paul Kocialkowski, Greg Kroah-Hartman, Chen-Yu Tsai,
Jernej Skrabec, Samuel Holland
Cc: Jonas Karlman, linux-media, linux-kernel, linux-rockchip,
linux-staging, linux-arm-kernel, linux-sunxi,
Christian Stefan Kövér-Draxl, Arun Iyengar
From: Arun Iyengar <aiyengar1990@gmail.com>
The driver decodes everything in MPEG-2 mode. Reject
V4L2_MPEG2_SEQ_FLAG_MPEG1, the full pel flags and D-pictures, which it
would otherwise decode as MPEG-2. Compile tested only.
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
drivers/media/platform/verisilicon/hantro_drv.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/media/platform/verisilicon/hantro_drv.c b/drivers/media/platform/verisilicon/hantro_drv.c
index 26e5d5547466..c910a69e8e3c 100644
--- a/drivers/media/platform/verisilicon/hantro_drv.c
+++ b/drivers/media/platform/verisilicon/hantro_drv.c
@@ -264,6 +264,16 @@ static int hantro_try_ctrl(struct v4l2_ctrl *ctrl)
if (sps->bit_depth_luma_minus8 != 0 && sps->bit_depth_luma_minus8 != 2)
/* Only 8-bit and 10-bit are supported */
return -EINVAL;
+ } else if (ctrl->id == V4L2_CID_STATELESS_MPEG2_SEQUENCE) {
+ if (ctrl->p_new.p_mpeg2_sequence->flags & V4L2_MPEG2_SEQ_FLAG_MPEG1)
+ return -EINVAL;
+ } else if (ctrl->id == V4L2_CID_STATELESS_MPEG2_PICTURE) {
+ const struct v4l2_ctrl_mpeg2_picture *pic = ctrl->p_new.p_mpeg2_picture;
+
+ if (pic->picture_coding_type == V4L2_MPEG2_PIC_CODING_TYPE_D ||
+ pic->flags & (V4L2_MPEG2_PIC_FLAG_FULL_PEL_FORWARD |
+ V4L2_MPEG2_PIC_FLAG_FULL_PEL_BACKWARD))
+ return -EINVAL;
} else if (ctrl->id == V4L2_CID_STATELESS_VP9_FRAME) {
const struct v4l2_ctrl_vp9_frame *dec_params = ctrl->p_new.p_vp9_frame;
@@ -435,11 +445,13 @@ static const struct hantro_ctrl controls[] = {
.codec = HANTRO_MPEG2_DECODER,
.cfg = {
.id = V4L2_CID_STATELESS_MPEG2_SEQUENCE,
+ .ops = &hantro_ctrl_ops,
},
}, {
.codec = HANTRO_MPEG2_DECODER,
.cfg = {
.id = V4L2_CID_STATELESS_MPEG2_PICTURE,
+ .ops = &hantro_ctrl_ops,
},
}, {
.codec = HANTRO_MPEG2_DECODER,
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 3/3] media: cedrus: mpeg2: decode MPEG-1
2026-11-21 10:10 [PATCH v2 0/3] media: stateless MPEG-1 decoding, cedrus support Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 1/3] media: uapi: mpeg2: add MPEG-1 flags Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 2/3] media: verisilicon: reject MPEG-1 controls Arun Iyengar via B4 Relay
@ 2026-11-21 10:10 ` Arun Iyengar via B4 Relay
2 siblings, 0 replies; 4+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-21 10:10 UTC (permalink / raw)
To: Hans Verkuil, Nicolas Dufresne, Mauro Carvalho Chehab,
Benjamin Gaignard, Philipp Zabel, Maxime Ripard,
Paul Kocialkowski, Greg Kroah-Hartman, Chen-Yu Tsai,
Jernej Skrabec, Samuel Holland
Cc: Jonas Karlman, linux-media, linux-kernel, linux-rockchip,
linux-staging, linux-arm-kernel, linux-sunxi,
Christian Stefan Kövér-Draxl, Arun Iyengar
From: Arun Iyengar <aiyengar1990@gmail.com>
The engine has an MPEG-1 mode, selected with the MPEG-1 decoder type in
the trigger register, and takes the full pel flags and the picture
coding type (D-pictures included) in the picture header register.
Select the MPEG-1 mode when V4L2_MPEG2_SEQ_FLAG_MPEG1 is set (every
variant with the MPEG-2 decoder; tested on H616 only). A job fails if
an MPEG-1 sequence has field pictures, or an MPEG-2 one has D-pictures
or full pel flags.
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
drivers/staging/media/sunxi/cedrus/cedrus.h | 3 +++
drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c | 27 +++++++++++++++++++----
2 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/drivers/staging/media/sunxi/cedrus/cedrus.h b/drivers/staging/media/sunxi/cedrus/cedrus.h
index c4b1217c14b6..9c35078ef02f 100644
--- a/drivers/staging/media/sunxi/cedrus/cedrus.h
+++ b/drivers/staging/media/sunxi/cedrus/cedrus.h
@@ -144,6 +144,9 @@ struct cedrus_ctx {
void *entry_points_buf;
dma_addr_t entry_points_buf_addr;
} h265;
+ struct {
+ bool mpeg1;
+ } mpeg2;
struct {
unsigned int last_frame_p_type;
unsigned int last_filter_type;
diff --git a/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c b/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c
index 10e98f08aafc..557e3d029318 100644
--- a/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c
+++ b/drivers/staging/media/sunxi/cedrus/cedrus_mpeg2.c
@@ -48,6 +48,17 @@ static void cedrus_mpeg2_irq_disable(struct cedrus_ctx *ctx)
cedrus_write(dev, VE_DEC_MPEG_CTRL, reg);
}
+static bool cedrus_mpeg2_pic_valid(const struct v4l2_ctrl_mpeg2_sequence *seq,
+ const struct v4l2_ctrl_mpeg2_picture *pic)
+{
+ if (seq->flags & V4L2_MPEG2_SEQ_FLAG_MPEG1)
+ return pic->picture_structure == V4L2_MPEG2_PIC_FRAME;
+
+ return pic->picture_coding_type != V4L2_MPEG2_PIC_CODING_TYPE_D &&
+ !(pic->flags & (V4L2_MPEG2_PIC_FLAG_FULL_PEL_FORWARD |
+ V4L2_MPEG2_PIC_FLAG_FULL_PEL_BACKWARD));
+}
+
static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run)
{
const struct v4l2_ctrl_mpeg2_sequence *seq;
@@ -63,6 +74,11 @@ static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run)
seq = run->mpeg2.sequence;
pic = run->mpeg2.picture;
+ if (!cedrus_mpeg2_pic_valid(seq, pic))
+ return -EINVAL;
+
+ ctx->codec.mpeg2.mpeg1 = seq->flags & V4L2_MPEG2_SEQ_FLAG_MPEG1;
+
quantisation = run->mpeg2.quantisation;
/* Activate MPEG engine. */
@@ -101,8 +117,10 @@ static int cedrus_mpeg2_setup(struct cedrus_ctx *ctx, struct cedrus_run *run)
reg |= VE_DEC_MPEG_MP12HDR_Q_SCALE_TYPE(pic->flags & V4L2_MPEG2_PIC_FLAG_Q_SCALE_TYPE);
reg |= VE_DEC_MPEG_MP12HDR_INTRA_VLC_FORMAT(pic->flags & V4L2_MPEG2_PIC_FLAG_INTRA_VLC);
reg |= VE_DEC_MPEG_MP12HDR_ALTERNATE_SCAN(pic->flags & V4L2_MPEG2_PIC_FLAG_ALT_SCAN);
- reg |= VE_DEC_MPEG_MP12HDR_FULL_PEL_FORWARD_VECTOR(0);
- reg |= VE_DEC_MPEG_MP12HDR_FULL_PEL_BACKWARD_VECTOR(0);
+ reg |= VE_DEC_MPEG_MP12HDR_FULL_PEL_FORWARD_VECTOR(pic->flags &
+ V4L2_MPEG2_PIC_FLAG_FULL_PEL_FORWARD);
+ reg |= VE_DEC_MPEG_MP12HDR_FULL_PEL_BACKWARD_VECTOR(pic->flags &
+ V4L2_MPEG2_PIC_FLAG_FULL_PEL_BACKWARD);
cedrus_write(dev, VE_DEC_MPEG_MP12HDR, reg);
@@ -183,8 +201,9 @@ static void cedrus_mpeg2_trigger(struct cedrus_ctx *ctx)
u32 reg;
/* Trigger MPEG engine. */
- reg = VE_DEC_MPEG_TRIGGER_HW_MPEG_VLD | VE_DEC_MPEG_TRIGGER_MPEG2 |
- VE_DEC_MPEG_TRIGGER_MB_BOUNDARY;
+ reg = VE_DEC_MPEG_TRIGGER_HW_MPEG_VLD | VE_DEC_MPEG_TRIGGER_MB_BOUNDARY;
+ reg |= ctx->codec.mpeg2.mpeg1 ? VE_DEC_MPEG_TRIGGER_MPEG1 :
+ VE_DEC_MPEG_TRIGGER_MPEG2;
cedrus_write(dev, VE_DEC_MPEG_TRIGGER, reg);
}
--
2.55.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-21 10:11 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21 10:10 [PATCH v2 0/3] media: stateless MPEG-1 decoding, cedrus support Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 1/3] media: uapi: mpeg2: add MPEG-1 flags Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 2/3] media: verisilicon: reject MPEG-1 controls Arun Iyengar via B4 Relay
2026-11-21 10:10 ` [PATCH v2 3/3] media: cedrus: mpeg2: decode MPEG-1 Arun Iyengar via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).