[PATCH 1/8] media: mediatek: vcodec: Decode H.264 access units on MT8173
From: Ryan Brue
Date: Fri Nov 20 2026 - 14:56:17 EST
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@xxxxxxxxx>
---
.../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