[PATCH 3/8] media: mediatek: vcodec: Report the visible rectangle as default compose

From: Ryan Brue

Date: Fri Nov 20 2026 - 14:55:53 EST


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@xxxxxxxxx>
---
.../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