From: "Jackson.lee" <jackson.lee@chipsnmedia.com>
To: mchehab@kernel.org, hverkuil-cisco@xs4all.nl,
nicolas.dufresne@collabora.com, bob.beckett@collabora.com
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
jackson.lee@chipsnmedia.com, lafley.kim@chipsnmedia.com,
b-brnich@ti.com, hverkuil@xs4all.nl, nas.chung@chipsnmedia.com,
stable@vger.kernel.org
Subject: [PATCH v1 7/9] media: chips-media: wave5: stamp decoded pictures from a decode-order queue
Date: Wed, 7 Oct 2026 10:59:44 +0900 [thread overview]
Message-ID: <20261007015946.53-8-jackson.lee@chipsnmedia.com> (raw)
In-Reply-To: <20261007015946.53-1-jackson.lee@chipsnmedia.com>
From: Jackson Lee <jackson.lee@chipsnmedia.com>
The VPU takes more than one picture command at a time, so one completion
can report a read pointer that has already passed several access units.
wave5_handle_src_buffer() then completes several source buffers in a
single call, and inst->timestamp keeps only the last of them, so every
earlier timestamp is overwritten before any picture is stamped with it.
Userspace carries its frame number in that timestamp. A number that
never comes back leaves its frame pending forever; GStreamer reports it
as "Too old frames, bug in decoder" once a hundred have accumulated.
Queue the timestamps in decode order and take one per decoded picture.
Fixes: 9707a6254a8a ("media: chips-media: wave5: Add the v4l2 layer")
Cc: stable@vger.kernel.org
Signed-off-by: Jackson Lee <jackson.lee@chipsnmedia.com>
Signed-off-by: Nas Chung <nas.chung@chipsnmedia.com>
---
.../chips-media/wave5/wave5-vpu-dec.c | 69 ++++++++++++++++++-
.../platform/chips-media/wave5/wave5-vpuapi.h | 10 +++
2 files changed, 78 insertions(+), 1 deletion(-)
diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
index 0c92b6c9a913..eae738df270e 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpu-dec.c
@@ -199,6 +199,59 @@ static bool wave5_last_src_buffer_consumed(struct v4l2_m2m_ctx *m2m_ctx)
return vpu_buf->consumed;
}
+/*
+ * The decode-order timestamp queue.
+ *
+ * GStreamer carries its frame number in tv_sec of the OUTPUT timestamp and
+ * reads it back off the CAPTURE buffer, so every source buffer's timestamp has
+ * to reach exactly one decoded picture.
+ *
+ * A scalar cannot do that. The VPU accepts more than one picture command at a
+ * time, so a single completion can report a read pointer that has already
+ * passed several access units; wave5_handle_src_buffer() then completes
+ * several source buffers in one call, and all but the last timestamp is
+ * overwritten before any picture is stamped with it. That number never comes
+ * back, and the frame it belongs to is left pending in userspace forever --
+ * observed as GStreamer's "Too old frames, bug in decoder" warning once a
+ * hundred of them have piled up.
+ *
+ * Queue them instead. Picture completions arrive in decode order, which is the
+ * order the source buffers were consumed in.
+ */
+static void wave5_ts_reset(struct vpu_instance *inst)
+{
+ inst->time_stamp.head = 0;
+ inst->time_stamp.tail = 0;
+}
+
+static void wave5_ts_push(struct vpu_instance *inst, u64 ts)
+{
+ int next = (inst->time_stamp.head + 1) % MAX_TIMESTAMP_CIR_BUF;
+
+ /*
+ * Full means pictures are not being reported for the data we feed, so
+ * the oldest entry has nothing left to claim it. Drop it rather than
+ * refuse the new one, which would shift every timestamp from here on.
+ */
+ if (next == inst->time_stamp.tail)
+ inst->time_stamp.tail = (inst->time_stamp.tail + 1) %
+ MAX_TIMESTAMP_CIR_BUF;
+
+ inst->time_stamp.buf[inst->time_stamp.head] = ts;
+ inst->time_stamp.head = next;
+}
+
+static bool wave5_ts_pop(struct vpu_instance *inst, u64 *ts)
+{
+ if (inst->time_stamp.head == inst->time_stamp.tail)
+ return false;
+
+ *ts = inst->time_stamp.buf[inst->time_stamp.tail];
+ inst->time_stamp.tail = (inst->time_stamp.tail + 1) %
+ MAX_TIMESTAMP_CIR_BUF;
+ return true;
+}
+
static void wave5_handle_src_buffer(struct vpu_instance *inst, dma_addr_t rd_ptr)
{
struct v4l2_m2m_ctx *m2m_ctx = inst->v4l2_fh.m2m_ctx;
@@ -232,6 +285,7 @@ static void wave5_handle_src_buffer(struct vpu_instance *inst, dma_addr_t rd_ptr
__func__, src_buf->vb2_buf.index);
src_buf = v4l2_m2m_src_buf_remove(m2m_ctx);
inst->timestamp = src_buf->vb2_buf.timestamp;
+ wave5_ts_push(inst, src_buf->vb2_buf.timestamp);
v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
consumed_bytes -= src_size;
@@ -422,8 +476,18 @@ static void wave5_vpu_dec_finish_decode(struct vpu_instance *inst)
struct vb2_buffer *vb = vb2_get_buffer(dst_vq,
dec_info.index_frame_decoded);
if (vb) {
+ u64 ts;
+
dec_buf = to_vb2_v4l2_buffer(vb);
- dec_buf->vb2_buf.timestamp = inst->timestamp;
+ /*
+ * Empty means this picture came out of data that was
+ * already accounted for. Fall back to the last timestamp
+ * seen rather than leave the buffer unstamped.
+ */
+ if (!wave5_ts_pop(inst, &ts))
+ ts = inst->timestamp;
+
+ dec_buf->vb2_buf.timestamp = ts;
} else {
dev_warn(inst->dev->dev, "%s: invalid decoded frame index %i",
__func__, dec_info.index_frame_decoded);
@@ -1529,6 +1593,9 @@ static int streamoff_output(struct vb2_queue *q)
*/
inst->remaining_consumed_bytes = 0;
+ /* The queued timestamps belong to those buffers as well. */
+ wave5_ts_reset(inst);
+
if (v4l2_m2m_has_stopped(m2m_ctx)) {
unsigned long flags;
diff --git a/drivers/media/platform/chips-media/wave5/wave5-vpuapi.h b/drivers/media/platform/chips-media/wave5/wave5-vpuapi.h
index abfe94fa18e7..f2c6efae4aa0 100644
--- a/drivers/media/platform/chips-media/wave5/wave5-vpuapi.h
+++ b/drivers/media/platform/chips-media/wave5/wave5-vpuapi.h
@@ -781,6 +781,15 @@ struct vpu_instance_ops {
void (*finish_process)(struct vpu_instance *inst);
};
+#define MAX_TIMESTAMP_CIR_BUF 30
+
+/* OUTPUT timestamps in decode order, one per consumed source buffer */
+struct timestamp_circ_buf {
+ u64 buf[MAX_TIMESTAMP_CIR_BUF];
+ int head;
+ int tail;
+};
+
struct vpu_instance {
struct list_head list;
struct v4l2_fh v4l2_fh;
@@ -817,6 +826,7 @@ struct vpu_instance {
struct list_head avail_dst_bufs;
struct v4l2_rect conf_win;
u64 timestamp;
+ struct timestamp_circ_buf time_stamp;
enum frame_buffer_format output_format;
bool cbcr_interleave;
bool nv21;
--
2.43.0
next prev parent reply other threads:[~2026-10-07 2:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-07 1:59 [PATCH v1 0/9] fix decoder corruption, stalls and seek issues Jackson.lee
2026-10-07 1:59 ` [PATCH v1 1/9] media: chips-media: wave5: Ensure Atomic Access to src_buf list Jackson.lee
2026-10-07 1:59 ` [PATCH v1 2/9] media: chips-media: wave5: drop the consumed-byte tally on OUTPUT streamoff Jackson.lee
2026-10-07 1:59 ` [PATCH v1 3/9] media: chips-media: wave5: wait before retrying a refused flush Jackson.lee
2026-10-07 1:59 ` [PATCH v1 4/9] media: chips-media: wave5: ack the interrupt after dispatching it Jackson.lee
2026-10-07 1:59 ` [PATCH v1 5/9] media: chips-media: wave5: finish a job only once Jackson.lee
2026-10-07 1:59 ` [PATCH v1 6/9] media: chips-media: wave5: decode only when the ring holds unclaimed bitstream Jackson.lee
2026-10-07 1:59 ` Jackson.lee [this message]
2026-10-07 1:59 ` [PATCH v1 8/9] media: chips-media: wave5: restore the display flags after a flush Jackson.lee
2026-10-07 1:59 ` [PATCH v1 9/9] media: chips-media: wave5: Stop FrameBuf Reset During Seek Jackson.lee
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261007015946.53-8-jackson.lee@chipsnmedia.com \
--to=jackson.lee@chipsnmedia.com \
--cc=b-brnich@ti.com \
--cc=bob.beckett@collabora.com \
--cc=hverkuil-cisco@xs4all.nl \
--cc=hverkuil@xs4all.nl \
--cc=lafley.kim@chipsnmedia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=nas.chung@chipsnmedia.com \
--cc=nicolas.dufresne@collabora.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®