* [PATCH 1/8] media: mediatek: vcodec: Decode H.264 access units on MT8173
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-20 19:55 ` [PATCH 2/8] media: mediatek: vcodec: Pick an OUTPUT buffer size when given zero Ryan Brue
` (6 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
The stateful decoder interface hands the driver one H.264 access unit
per buffer (pixfmt-compressed.rst, V4L2_PIX_FMT_H264), but the MT8173
VPU firmware takes one NAL unit per call, and the driver passes it the
whole buffer with the type of the first unit only. An access unit that
starts with a delimiter, its parameter sets or an SEI therefore loses
its PPS, and every slice after it fails with a bitstream error. Pictures
of more than one slice never decoded at all: given one slice, the
hardware never raises its interrupt.
Walk the units of the buffer: the parameter sets one by one, then the
slices of the picture together in one call, through to the end of the
buffer. Skip the units the firmware does not decode (SEI, delimiters,
filler); handed the frame buffer with an SEI, it loses the picture
decoded into it later, so the frame buffer goes with the slices only,
and a frame buffer that no slice used goes back to the free list.
The decoder starts reading at the bitstream address rounded down to 16
bytes, and decodes from the first start code it finds there, so a unit
with another start code within those bytes in front of it, such as a
slice after a six-byte delimiter, is decoded as the wrong unit. Copy
such slices, and the parameter sets, which the firmware parses the same
way, to buffers of their own. The slice buffer grows to the largest
picture seen. A picture behind a delimiter, or behind parameter sets
short enough to share its 16 bytes, thus costs one copy of its slices.
Without a frame buffer only the header is being parsed, so stop at the
first slice, and stop when the firmware reports a resolution change,
since the core decodes the same buffer again after reconfiguring.
Buffers holding a single NAL unit decode the same as before.
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
.../mediatek/vcodec/decoder/vdec/vdec_h264_if.c | 168 ++++++++++++++++++++-
1 file changed, 161 insertions(+), 7 deletions(-)
diff --git a/drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_if.c b/drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_if.c
index a95327388f57..d2d7103c09c0 100644
--- a/drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_if.c
+++ b/drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_if.c
@@ -5,6 +5,7 @@
*/
#include <linux/module.h>
+#include <linux/sizes.h>
#include <linux/slab.h>
#include "../vdec_drv_if.h"
@@ -15,8 +16,11 @@
#define NAL_NON_IDR_SLICE 0x01
#define NAL_IDR_SLICE 0x05
+#define NAL_H264_SPS 0x07
#define NAL_H264_PPS 0x08
#define NAL_TYPE(value) ((value) & 0x1F)
+/* the decoder reads the bitstream from an address rounded down to this */
+#define H264_BS_READ_ALIGN 16
#define BUF_PREDICTION_SZ (32 * 1024)
@@ -118,6 +122,9 @@ struct vdec_h264_vsi {
* @num_nalu : how many nalus be decoded
* @ctx : point to mtk_vcodec_dec_ctx
* @pred_buf : HW working prediction buffer
+ * @param_buf : copy of a parameter set for the firmware
+ * @slice_buf : copy of the slices when another start code precedes theirs
+ * within the decoder's read alignment
* @mv_buf : HW working motion vector buffer
* @vpu : VPU instance
* @vsi : VPU shared information
@@ -126,6 +133,8 @@ struct vdec_h264_inst {
unsigned int num_nalu;
struct mtk_vcodec_dec_ctx *ctx;
struct mtk_vcodec_mem pred_buf;
+ struct mtk_vcodec_mem param_buf;
+ struct mtk_vcodec_mem slice_buf;
struct mtk_vcodec_mem mv_buf[H264_MAX_FB_NUM];
struct vdec_vpu_inst vpu;
struct vdec_h264_vsi *vsi;
@@ -309,6 +318,10 @@ static void vdec_h264_deinit(void *h_vdec)
vpu_dec_deinit(&inst->vpu);
free_prediction_buf(inst);
+ if (inst->param_buf.va)
+ mtk_vcodec_mem_free(inst->ctx, &inst->param_buf);
+ if (inst->slice_buf.va)
+ mtk_vcodec_mem_free(inst->ctx, &inst->slice_buf);
free_mv_buf(inst);
kfree(inst);
@@ -326,10 +339,10 @@ static int find_start_code(unsigned char *data, unsigned int data_sz)
return -1;
}
-static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
- struct vdec_fb *fb, bool *res_chg)
+static int vdec_h264_decode_nal(struct vdec_h264_inst *inst,
+ struct mtk_vcodec_mem *bs,
+ struct vdec_fb *fb, bool *res_chg)
{
- struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
struct vdec_vpu_inst *vpu = &inst->vpu;
int nal_start_idx = 0;
int err = 0;
@@ -345,10 +358,6 @@ static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
mtk_vdec_debug(inst->ctx, "+ [%d] FB y_dma=%llx c_dma=%llx va=%p",
++inst->num_nalu, y_fb_dma, c_fb_dma, fb);
- /* bs NULL means flush decoder */
- if (bs == NULL)
- return vpu_dec_reset(vpu);
-
buf = (unsigned char *)bs->va;
buf_sz = bs->size;
nal_start_idx = find_start_code(buf, buf_sz);
@@ -422,6 +431,151 @@ static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
return err;
}
+/* Offset of the next start code (00 00 01) at or after @from, or @size */
+static size_t next_start_code(const unsigned char *buf, size_t from,
+ size_t size)
+{
+ size_t i;
+
+ for (i = from; i + 3 <= size; i++)
+ if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
+ return i;
+
+ return size;
+}
+
+/*
+ * The decoder starts reading at the bitstream address rounded down to 16
+ * bytes and decodes from the first start code it finds there. A unit
+ * whose start code has another one in front of it within that window
+ * has to be copied to a buffer of its own; so do the parameter sets,
+ * which the firmware parses the same way.
+ */
+static bool vdec_h264_needs_copy(const struct mtk_vcodec_mem *bs, size_t start)
+{
+ size_t back = (bs->dma_addr + start) & (H264_BS_READ_ALIGN - 1);
+ size_t from = start > back ? start - back : 0;
+
+ return next_start_code(bs->va, from, start + 2) < start;
+}
+
+static int vdec_h264_copy_nal(struct vdec_h264_inst *inst,
+ struct mtk_vcodec_mem *dst,
+ struct mtk_vcodec_mem *nal)
+{
+ if (dst->size < nal->size + H264_BS_READ_ALIGN) {
+ if (dst->va)
+ mtk_vcodec_mem_free(inst->ctx, dst);
+ /* room past the end, as a bitstream buffer has */
+ dst->size = round_up(nal->size + H264_BS_READ_ALIGN, SZ_4K);
+ if (mtk_vcodec_mem_alloc(inst->ctx, dst)) {
+ dst->size = 0;
+ return -ENOMEM;
+ }
+ }
+
+ memcpy(dst->va, nal->va, nal->size);
+ nal->va = dst->va;
+ nal->dma_addr = dst->dma_addr;
+ return 0;
+}
+
+/*
+ * The firmware takes one NAL unit per call, while the stateful decoder
+ * interface hands the driver one access unit per buffer. Feed the units
+ * of the buffer to the firmware one by one: the parameter sets, then the
+ * slices of the picture together. Units it does not decode (SEI,
+ * delimiters, filler) are skipped: handed the frame buffer with an SEI,
+ * it loses the picture decoded into it later. The frame buffer goes with
+ * the slices only; a frame buffer that no slice used goes back to the
+ * free list.
+ * Without a frame buffer only the parameter sets are of interest, so
+ * stop at the first slice.
+ */
+static int vdec_h264_decode(void *h_vdec, struct mtk_vcodec_mem *bs,
+ struct vdec_fb *fb, bool *res_chg)
+{
+ struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
+ const unsigned char *buf;
+ size_t start, end;
+ int err;
+
+ /* bs NULL means flush decoder */
+ if (!bs)
+ return vpu_dec_reset(&inst->vpu);
+
+ *res_chg = false;
+ buf = bs->va;
+ start = next_start_code(buf, 0, bs->size);
+ if (start == bs->size)
+ return vdec_h264_decode_nal(inst, bs, fb, res_chg);
+
+ while (start < bs->size) {
+ struct mtk_vcodec_mem nal = *bs;
+ unsigned int nal_type;
+ bool is_slice;
+
+ nal_type = start + 3 < bs->size ? NAL_TYPE(buf[start + 3]) : 0;
+ is_slice = nal_type == NAL_NON_IDR_SLICE ||
+ nal_type == NAL_IDR_SLICE;
+ if (!fb && is_slice)
+ break;
+
+ if (is_slice) {
+ /*
+ * The slices of a picture go in one call: the hardware
+ * reads on through them, and interrupts once, at the
+ * picture's end.
+ */
+ end = bs->size;
+ } else {
+ end = next_start_code(buf, start + 3, bs->size);
+ /* A zero byte before a start code belongs to it */
+ if (end < bs->size && buf[end - 1] == 0)
+ end--;
+ }
+
+ if (!is_slice && nal_type != NAL_H264_SPS &&
+ nal_type != NAL_H264_PPS) {
+ start = next_start_code(buf, end, bs->size);
+ continue;
+ }
+
+ nal.va = bs->va + start;
+ nal.dma_addr = bs->dma_addr + start;
+ nal.size = end - start;
+
+ if (!is_slice)
+ err = vdec_h264_copy_nal(inst, &inst->param_buf, &nal);
+ else if (vdec_h264_needs_copy(bs, start))
+ err = vdec_h264_copy_nal(inst, &inst->slice_buf, &nal);
+ else
+ err = 0;
+ if (err) {
+ put_fb_to_free(inst, fb);
+ return err;
+ }
+
+ err = vdec_h264_decode_nal(inst, &nal, is_slice ? fb : NULL,
+ res_chg);
+ /* The slices run to the end of the buffer and keep the frame */
+ if (is_slice)
+ return err;
+ if (err) {
+ put_fb_to_free(inst, fb);
+ return err;
+ }
+ if (*res_chg)
+ break;
+
+ start = next_start_code(buf, end, bs->size);
+ }
+
+ put_fb_to_free(inst, fb);
+
+ return 0;
+}
+
static void vdec_h264_get_fb(struct vdec_h264_inst *inst,
struct h264_ring_fb_list *list,
bool disp_list, struct vdec_fb **out_fb)
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 2/8] media: mediatek: vcodec: Pick an OUTPUT buffer size when given zero
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
2026-11-20 19:55 ` [PATCH 1/8] media: mediatek: vcodec: Decode H.264 access units on MT8173 Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-20 19:55 ` [PATCH 3/8] media: mediatek: vcodec: Report the visible rectangle as default compose Ryan Brue
` (5 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
A client may leave the size of the bitstream (OUTPUT) buffers to the
decoder (dev-decoder.rst: sizeimage is the desired size, which the
decoder may adjust), and GStreamer probes with zero. VIDIOC_TRY_FMT
refused that with -EINVAL, while VIDIOC_S_FMT, which does not go through
that check, stored a zero buffer size. Choose 1 MiB, or three bytes per
four pixels of the given size, whichever is larger, in the format check
that both share, after the size is clamped.
Fixes: 590577a4e525 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
.../media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
index d76e891f784b..9339f53431aa 100644
--- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
+++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
@@ -5,6 +5,7 @@
* Tiffany Lin <tiffany.lin@mediatek.com>
*/
+#include <linux/sizes.h>
#include <media/v4l2-event.h>
#include <media/v4l2-mem2mem.h>
#include <media/videobuf2-dma-contig.h>
@@ -306,6 +307,11 @@ static int vidioc_try_fmt(struct mtk_vcodec_dec_ctx *ctx, struct v4l2_format *f,
if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
pix_fmt_mp->num_planes = 1;
pix_fmt_mp->plane_fmt[0].bytesperline = 0;
+ /* The client may leave the bitstream buffer size to us */
+ if (!pix_fmt_mp->plane_fmt[0].sizeimage)
+ pix_fmt_mp->plane_fmt[0].sizeimage =
+ max_t(u32, SZ_1M, pix_fmt_mp->width *
+ pix_fmt_mp->height * 3 / 4);
} else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
int tmp_w, tmp_h;
@@ -370,7 +376,6 @@ static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
struct v4l2_format *f)
{
- struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
const struct mtk_video_fmt *fmt;
struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file);
const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
@@ -382,11 +387,6 @@ static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
fmt = mtk_vdec_find_format(f, dec_pdata);
}
- if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
- mtk_v4l2_vdec_err(ctx, "sizeimage of output format must be given");
- return -EINVAL;
- }
-
return vidioc_try_fmt(ctx, f, fmt);
}
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 3/8] media: mediatek: vcodec: Report the visible rectangle as default compose
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
2026-11-20 19:55 ` [PATCH 1/8] media: mediatek: vcodec: Decode H.264 access units on MT8173 Ryan Brue
2026-11-20 19:55 ` [PATCH 2/8] media: mediatek: vcodec: Pick an OUTPUT buffer size when given zero Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-20 19:55 ` [PATCH 4/8] media: mtk-mdp: Round MT21C sources up to whole tiles Ryan Brue
` (4 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
The default compose rectangle of a decoder's CAPTURE queue is the
visible rectangle (dev-decoder.rst: equal to V4L2_SEL_TGT_CROP), but the
driver reported the picture size the firmware gives, which for H.264 is
a whole number of macroblocks: 1088 lines for a 1080-line stream, which
GStreamer then shows with eight lines of garbage. Report the crop
rectangle, as V4L2_SEL_TGT_COMPOSE already does for most codecs, and
return the same rectangle for V4L2_SEL_TGT_COMPOSE and from
VIDIOC_S_SELECTION, which only allows the default.
Fall back to the picture size when the codec leaves the crop rectangle
unset (the stateless VP8 and VP9 ones, for which V4L2_SEL_TGT_COMPOSE
returned the caller's rectangle) or reports it empty. Only MT8173 was
tested.
Fixes: 590577a4e525 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
.../mediatek/vcodec/decoder/mtk_vcodec_dec.c | 38 +++++++++++++---------
1 file changed, 23 insertions(+), 15 deletions(-)
diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
index 9339f53431aa..1223738ece4e 100644
--- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
+++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
@@ -390,6 +390,26 @@ static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
return vidioc_try_fmt(ctx, f, fmt);
}
+/*
+ * The visible rectangle (dev-decoder.rst), which for H.264 is the stream's
+ * cropping rectangle and not the picture size the firmware reports, which
+ * is a whole number of macroblocks. Not every codec fills it in.
+ */
+static void mtk_vdec_visible_rect(struct mtk_vcodec_dec_ctx *ctx,
+ struct v4l2_rect *r)
+{
+ struct v4l2_rect crop = {};
+
+ r->left = 0;
+ r->top = 0;
+ r->width = ctx->picinfo.pic_w;
+ r->height = ctx->picinfo.pic_h;
+
+ if (!vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &crop) &&
+ crop.width && crop.height)
+ *r = crop;
+}
+
static int vidioc_vdec_g_selection(struct file *file, void *priv,
struct v4l2_selection *s)
{
@@ -403,10 +423,7 @@ static int vidioc_vdec_g_selection(struct file *file, void *priv,
switch (s->target) {
case V4L2_SEL_TGT_COMPOSE_DEFAULT:
- s->r.left = 0;
- s->r.top = 0;
- s->r.width = ctx->picinfo.pic_w;
- s->r.height = ctx->picinfo.pic_h;
+ mtk_vdec_visible_rect(ctx, &s->r);
break;
case V4L2_SEL_TGT_COMPOSE_BOUNDS:
s->r.left = 0;
@@ -415,13 +432,7 @@ static int vidioc_vdec_g_selection(struct file *file, void *priv,
s->r.height = ctx->picinfo.buf_h;
break;
case V4L2_SEL_TGT_COMPOSE:
- if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
- /* set to default value if header info not ready yet*/
- s->r.left = 0;
- s->r.top = 0;
- s->r.width = q_data->visible_width;
- s->r.height = q_data->visible_height;
- }
+ mtk_vdec_visible_rect(ctx, &s->r);
break;
default:
return -EINVAL;
@@ -449,10 +460,7 @@ static int vidioc_vdec_s_selection(struct file *file, void *priv,
switch (s->target) {
case V4L2_SEL_TGT_COMPOSE:
- s->r.left = 0;
- s->r.top = 0;
- s->r.width = ctx->picinfo.pic_w;
- s->r.height = ctx->picinfo.pic_h;
+ mtk_vdec_visible_rect(ctx, &s->r);
break;
default:
return -EINVAL;
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 4/8] media: mtk-mdp: Round MT21C sources up to whole tiles
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
` (2 preceding siblings ...)
2026-11-20 19:55 ` [PATCH 3/8] media: mediatek: vcodec: Report the visible rectangle as default compose Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-24 16:38 ` Nicolas Dufresne
2026-11-20 19:55 ` [PATCH 5/8] media: mtk-mdp: Do not refuse a crop for the other side's size Ryan Brue
` (3 subsequent siblings)
7 siblings, 1 reply; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
MT21C, the MT8173 decoder's output, is laid out in tiles of 16x32
pixels, and the MDP firmware refuses a source of a size that is not a
whole number of them: converting a 1280x720 picture fails with -EINVAL
for every frame, while 1280x736, the decoder's own buffer size, works.
The alignment was 16x16, so 720 lines passed the format check.
Align MT21C to 16x32. mtk_mdp_bound_align_image() already rounds a size
up to the alignment, so a source covers its picture, and the visible
part is the crop rectangle.
Fixes: c8eb2d7e8202 ("[media] media: Add Mediatek MDP Driver")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
index d2813890cceb..34d02de4453d 100644
--- a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
+++ b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
@@ -40,7 +40,7 @@ struct mtk_mdp_pix_limit {
static struct mtk_mdp_pix_align mtk_mdp_size_align = {
.org_w = 16,
- .org_h = 16,
+ .org_h = 32,
.target_w = 2,
.target_h = 2,
};
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 4/8] media: mtk-mdp: Round MT21C sources up to whole tiles
2026-11-20 19:55 ` [PATCH 4/8] media: mtk-mdp: Round MT21C sources up to whole tiles Ryan Brue
@ 2026-11-24 16:38 ` Nicolas Dufresne
0 siblings, 0 replies; 10+ messages in thread
From: Nicolas Dufresne @ 2026-11-24 16:38 UTC (permalink / raw)
To: Ryan Brue, Tiffany Lin, Andrew-CT Chen, Yunfei Dong,
Mauro Carvalho Chehab, Matthias Brugger,
AngeloGioacchino Del Regno, Hans Verkuil, Minghsiu Tsai,
Houlong Wei, Enric Balletbo i Serra, Eizan Miyamoto,
Francois Buergisser
Cc: Youngjoon Lee, linux-media, linux-kernel, linux-arm-kernel,
linux-mediatek, Mauro Carvalho Chehab
[-- Attachment #1: Type: text/plain, Size: 1478 bytes --]
Le vendredi 20 novembre 2026 à 13:55 -0600, Ryan Brue a écrit :
> MT21C, the MT8173 decoder's output, is laid out in tiles of 16x32
> pixels, and the MDP firmware refuses a source of a size that is not a
> whole number of them: converting a 1280x720 picture fails with -EINVAL
> for every frame, while 1280x736, the decoder's own buffer size, works.
> The alignment was 16x16, so 720 lines passed the format check.
>
> Align MT21C to 16x32. mtk_mdp_bound_align_image() already rounds a size
> up to the alignment, so a source covers its picture, and the visible
> part is the crop rectangle.
>
> Fixes: c8eb2d7e8202 ("[media] media: Add Mediatek MDP Driver")
Is this commit in stable? If so you should CC stable.
> Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
Reviewed-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>
> ---
> drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
> index d2813890cceb..34d02de4453d 100644
> --- a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
> +++ b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
> @@ -40,7 +40,7 @@ struct mtk_mdp_pix_limit {
>
> static struct mtk_mdp_pix_align mtk_mdp_size_align = {
> .org_w = 16,
> - .org_h = 16,
> + .org_h = 32,
> .target_w = 2,
> .target_h = 2,
> };
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH 5/8] media: mtk-mdp: Do not refuse a crop for the other side's size
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
` (3 preceding siblings ...)
2026-11-20 19:55 ` [PATCH 4/8] media: mtk-mdp: Round MT21C sources up to whole tiles Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-20 19:55 ` [PATCH 6/8] media: mtk-mdp: Check buffer plane sizes against the format Ryan Brue
` (2 subsequent siblings)
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
Setting the source crop checks the scaling ratio against the destination
as it stands: the 32x32 default that open() sets, or whatever a client
set while probing. GStreamer sets the source format and crop first, and
a 1280x720 crop was refused with -EINVAL against that 32x32 destination.
The same holds for the compose rectangle and the source, and for the
rotation control.
Check the ratio once both queues are streaming, when both sizes are
settled: at VIDIOC_STREAMON, and for a crop, compose or rotation change
made while streaming. VIDIOC_STREAMON is the first point where both
sizes are known: before it a client may still set either side in any
order, and checking there leaves the queues untouched on refusal. Log a
refusal at debug level, since any client can trigger it.
Fixes: 0d1e342a17f5 ("media: mtk-mdp: Remove states for format checks")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c | 121 +++++++++++++---------
1 file changed, 73 insertions(+), 48 deletions(-)
diff --git a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
index 34d02de4453d..a40bcf61b7ca 100644
--- a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
+++ b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
@@ -727,10 +727,40 @@ static int mtk_mdp_m2m_reqbufs(struct file *file, void *fh,
return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
}
+static int mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant *var, int src_w,
+ int src_h, int dst_w, int dst_h, int rot)
+{
+ int tmp_w, tmp_h;
+
+ if (rot == 90 || rot == 270) {
+ tmp_w = dst_h;
+ tmp_h = dst_w;
+ } else {
+ tmp_w = dst_w;
+ tmp_h = dst_h;
+ }
+
+ if ((src_w / tmp_w) > var->h_scale_down_max ||
+ (src_h / tmp_h) > var->v_scale_down_max ||
+ (tmp_w / src_w) > var->h_scale_up_max ||
+ (tmp_h / src_h) > var->v_scale_up_max)
+ return -EINVAL;
+
+ return 0;
+}
+
+/* Both sizes are settled once both queues are streaming */
+static bool mtk_mdp_ctx_streaming(struct mtk_mdp_ctx *ctx)
+{
+ return vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->m2m_ctx)) &&
+ vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->m2m_ctx));
+}
+
static int mtk_mdp_m2m_streamon(struct file *file, void *fh,
enum v4l2_buf_type type)
{
struct mtk_mdp_ctx *ctx = file_to_ctx(file);
+ struct vb2_queue *other;
int ret;
if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_VPU_INIT)) {
@@ -744,6 +774,21 @@ static int mtk_mdp_m2m_streamon(struct file *file, void *fh,
mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_VPU_INIT);
}
+ /* the queue that starts second checks the ratio */
+ other = v4l2_m2m_get_vq(ctx->m2m_ctx, V4L2_TYPE_IS_OUTPUT(type) ?
+ V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
+ V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
+ if (vb2_is_streaming(other) &&
+ mtk_mdp_check_scaler_ratio(ctx->mdp_dev->variant,
+ ctx->s_frame.crop.width,
+ ctx->s_frame.crop.height,
+ ctx->d_frame.crop.width,
+ ctx->d_frame.crop.height,
+ ctx->ctrls.rotate->val)) {
+ dev_dbg(&ctx->mdp_dev->pdev->dev, "Out of scaler range\n");
+ return -EINVAL;
+ }
+
return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
}
@@ -810,35 +855,12 @@ static int mtk_mdp_m2m_g_selection(struct file *file, void *fh,
return -EINVAL;
}
-static int mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant *var, int src_w,
- int src_h, int dst_w, int dst_h, int rot)
-{
- int tmp_w, tmp_h;
-
- if (rot == 90 || rot == 270) {
- tmp_w = dst_h;
- tmp_h = dst_w;
- } else {
- tmp_w = dst_w;
- tmp_h = dst_h;
- }
-
- if ((src_w / tmp_w) > var->h_scale_down_max ||
- (src_h / tmp_h) > var->v_scale_down_max ||
- (tmp_w / src_w) > var->h_scale_up_max ||
- (tmp_h / src_h) > var->v_scale_up_max)
- return -EINVAL;
-
- return 0;
-}
-
static int mtk_mdp_m2m_s_selection(struct file *file, void *fh,
struct v4l2_selection *s)
{
struct mtk_mdp_ctx *ctx = file_to_ctx(file);
struct mtk_mdp_frame *frame;
struct v4l2_rect new_r;
- struct mtk_mdp_variant *variant = ctx->mdp_dev->variant;
int ret;
bool valid = false;
@@ -865,22 +887,27 @@ static int mtk_mdp_m2m_s_selection(struct file *file, void *fh,
else
frame = &ctx->d_frame;
- /* Check to see if scaling ratio is within supported range */
- if (V4L2_TYPE_IS_OUTPUT(s->type))
- ret = mtk_mdp_check_scaler_ratio(variant, new_r.width,
- new_r.height, ctx->d_frame.crop.width,
- ctx->d_frame.crop.height,
- ctx->ctrls.rotate->val);
- else
- ret = mtk_mdp_check_scaler_ratio(variant,
- ctx->s_frame.crop.width,
- ctx->s_frame.crop.height, new_r.width,
- new_r.height, ctx->ctrls.rotate->val);
-
- if (ret) {
- dev_info(&ctx->mdp_dev->pdev->dev,
- "Out of scaler range");
- return -EINVAL;
+ /*
+ * Until both queues are streaming the other side's size may still
+ * change, and VIDIOC_STREAMON checks the scaling ratio.
+ */
+ if (mtk_mdp_ctx_streaming(ctx)) {
+ struct v4l2_rect *src = &ctx->s_frame.crop;
+ struct v4l2_rect *dst = &ctx->d_frame.crop;
+
+ if (V4L2_TYPE_IS_OUTPUT(s->type))
+ src = &new_r;
+ else
+ dst = &new_r;
+
+ if (mtk_mdp_check_scaler_ratio(ctx->mdp_dev->variant,
+ src->width, src->height,
+ dst->width, dst->height,
+ ctx->ctrls.rotate->val)) {
+ dev_dbg(&ctx->mdp_dev->pdev->dev,
+ "Out of scaler range\n");
+ return -EINVAL;
+ }
}
s->r = new_r;
@@ -953,7 +980,6 @@ static int mtk_mdp_s_ctrl(struct v4l2_ctrl *ctrl)
struct mtk_mdp_ctx *ctx = ctrl_to_ctx(ctrl);
struct mtk_mdp_dev *mdp = ctx->mdp_dev;
struct mtk_mdp_variant *variant = mdp->variant;
- int ret = 0;
if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
return 0;
@@ -966,14 +992,13 @@ static int mtk_mdp_s_ctrl(struct v4l2_ctrl *ctrl)
ctx->vflip = ctrl->val;
break;
case V4L2_CID_ROTATE:
- ret = mtk_mdp_check_scaler_ratio(variant,
- ctx->s_frame.crop.width,
- ctx->s_frame.crop.height,
- ctx->d_frame.crop.width,
- ctx->d_frame.crop.height,
- ctx->ctrls.rotate->val);
-
- if (ret)
+ if (mtk_mdp_ctx_streaming(ctx) &&
+ mtk_mdp_check_scaler_ratio(variant,
+ ctx->s_frame.crop.width,
+ ctx->s_frame.crop.height,
+ ctx->d_frame.crop.width,
+ ctx->d_frame.crop.height,
+ ctx->ctrls.rotate->val))
return -EINVAL;
ctx->rotation = ctrl->val;
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 6/8] media: mtk-mdp: Check buffer plane sizes against the format
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
` (4 preceding siblings ...)
2026-11-20 19:55 ` [PATCH 5/8] media: mtk-mdp: Do not refuse a crop for the other side's size Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-20 19:55 ` [PATCH 7/8] media: mtk-mdp: Refuse a format change once buffers are allocated Ryan Brue
2026-11-20 19:55 ` [PATCH 8/8] media: mediatek: vcodec: " Ryan Brue
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
queue_setup() ignores the number of planes and their sizes that come with
VIDIOC_CREATE_BUFS and always answers with the current format, so a
client can allocate buffers too small for it, which v4l2-compliance
reports:
fail: v4l2-test-buffers.cpp(872): q.create_bufs(node, 1, &fmt) != EINVAL
Refuse a request with a different number of planes or a plane smaller
than the format needs, as vb2 expects of queue_setup(), and check each
buffer's planes in buf_prepare() as well.
Fixes: c8eb2d7e8202 ("[media] media: Add Mediatek MDP Driver")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
index a40bcf61b7ca..e0627127430b 100644
--- a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
+++ b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
@@ -546,6 +546,15 @@ static int mtk_mdp_m2m_queue_setup(struct vb2_queue *vq,
int i;
frame = mtk_mdp_ctx_get_frame(ctx, vq->type);
+ if (*num_planes) {
+ if (*num_planes != frame->fmt->num_planes)
+ return -EINVAL;
+ for (i = 0; i < frame->fmt->num_planes; i++)
+ if (sizes[i] < frame->payload[i])
+ return -EINVAL;
+ return 0;
+ }
+
*num_planes = frame->fmt->num_planes;
for (i = 0; i < frame->fmt->num_planes; i++)
sizes[i] = frame->payload[i];
@@ -563,6 +572,10 @@ static int mtk_mdp_m2m_buf_prepare(struct vb2_buffer *vb)
frame = mtk_mdp_ctx_get_frame(ctx, vb->vb2_queue->type);
+ for (i = 0; i < frame->fmt->num_planes; i++)
+ if (vb2_plane_size(vb, i) < frame->payload[i])
+ return -EINVAL;
+
if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
for (i = 0; i < frame->fmt->num_planes; i++)
vb2_set_plane_payload(vb, i, frame->payload[i]);
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 7/8] media: mtk-mdp: Refuse a format change once buffers are allocated
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
` (5 preceding siblings ...)
2026-11-20 19:55 ` [PATCH 6/8] media: mtk-mdp: Check buffer plane sizes against the format Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
2026-11-20 19:55 ` [PATCH 8/8] media: mediatek: vcodec: " Ryan Brue
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
VIDIOC_S_FMT stores the new format in the frame before it checks whether
the queue is streaming, so a refused change still takes effect: a client
switching a streaming CAPTURE queue from NV12M to YUV420M gets -EBUSY,
and the next frame is set up for three planes in buffers that have two.
And the check is for streaming only, so a format set after
VIDIOC_REQBUFS can need more than the buffers already allocated.
Check first, and refuse once the queue has buffers, as vb2_is_busy()
does for other drivers, logging it at debug level since any client can
trigger it.
Fixes: c8eb2d7e8202 ("[media] media: Add Mediatek MDP Driver")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
index e0627127430b..1be0f44fe1b2 100644
--- a/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
+++ b/drivers/media/platform/mediatek/mdp/mtk_mdp_m2m.c
@@ -698,6 +698,12 @@ static int mtk_mdp_m2m_s_fmt_mplane(struct file *file, void *fh,
mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
+ vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
+ if (vb2_is_busy(vq)) {
+ dev_dbg(&ctx->mdp_dev->pdev->dev, "queue %d busy\n", f->type);
+ return -EBUSY;
+ }
+
frame = mtk_mdp_ctx_get_frame(ctx, f->type);
fmt = mtk_mdp_try_fmt_mplane(ctx, f);
if (!fmt) {
@@ -706,12 +712,6 @@ static int mtk_mdp_m2m_s_fmt_mplane(struct file *file, void *fh,
}
frame->fmt = fmt;
- vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
- if (vb2_is_streaming(vq)) {
- dev_info(&ctx->mdp_dev->pdev->dev, "queue %d busy", f->type);
- return -EBUSY;
- }
-
pix_mp = &f->fmt.pix_mp;
for (i = 0; i < frame->fmt->num_planes; i++) {
frame->payload[i] = pix_mp->plane_fmt[i].sizeimage;
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread* [PATCH 8/8] media: mediatek: vcodec: Refuse a format change once buffers are allocated
2026-11-20 19:55 [PATCH 0/8] media: mediatek: Fix MT8173 decoding through GStreamer Ryan Brue
` (6 preceding siblings ...)
2026-11-20 19:55 ` [PATCH 7/8] media: mtk-mdp: Refuse a format change once buffers are allocated Ryan Brue
@ 2026-11-20 19:55 ` Ryan Brue
7 siblings, 0 replies; 10+ messages in thread
From: Ryan Brue @ 2026-11-20 19:55 UTC (permalink / raw)
To: Tiffany Lin, Andrew-CT Chen, Yunfei Dong, Mauro Carvalho Chehab,
Matthias Brugger, AngeloGioacchino Del Regno, Hans Verkuil,
Minghsiu Tsai, Houlong Wei, Enric Balletbo i Serra,
Eizan Miyamoto, Francois Buergisser
Cc: Nicolas Dufresne, Youngjoon Lee, linux-media, linux-kernel,
linux-arm-kernel, linux-mediatek, Mauro Carvalho Chehab,
Ryan Brue
VIDIOC_S_FMT on a queue whose buffers are allocated sets the result to
-EBUSY, then goes on to store the new format and returns 0, so the
client is told the change worked, and buffers allocated for the old
format are used with the new one.
Return -EBUSY there, as the checks intended, and log it at debug level,
since any client can trigger it. The CAPTURE check applies to the
stateless decoders too, which now also refuse S_FMT on a busy CAPTURE
queue, as vidioc-g-fmt.rst asks.
Fixes: 590577a4e525 ("[media] vcodec: mediatek: Add Mediatek V4L2 Video Decoder Driver")
Signed-off-by: Ryan Brue <ryanbrue.dev@gmail.com>
---
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
index 1223738ece4e..71db692ed848 100644
--- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
+++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
@@ -493,8 +493,8 @@ static int vidioc_vdec_s_fmt(struct file *file, void *priv,
if (!dec_pdata->uses_stateless_api &&
f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
- mtk_v4l2_vdec_err(ctx, "out_q_ctx buffers already requested");
- ret = -EBUSY;
+ mtk_v4l2_vdec_dbg(1, ctx, "out_q_ctx buffers already requested");
+ return -EBUSY;
}
/*
@@ -503,8 +503,8 @@ static int vidioc_vdec_s_fmt(struct file *file, void *priv,
*/
if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
- mtk_v4l2_vdec_err(ctx, "cap_q_ctx buffers already requested");
- ret = -EBUSY;
+ mtk_v4l2_vdec_dbg(1, ctx, "cap_q_ctx buffers already requested");
+ return -EBUSY;
}
fmt = mtk_vdec_find_format(f, dec_pdata);
--
2.56.0
^ permalink raw reply [flat|nested] 10+ messages in thread