[PATCH v5 4/5] media: mediatek: vcodec: store current vb2 buffer to decode again

From: Yunfei Dong
Date: Sat Oct 12 2024 - 02:45:02 EST


All the src vb2 buffer are removed from ready list when STREAMOFF
capture queue, may remove a non exist vb2 buffer if lat is working
currently. The driver also need to use current vb2 buffer to decode
again to wait for enough resource when lat decode error.

Signed-off-by: Yunfei Dong <yunfei.dong@xxxxxxxxxxxx>
---
.../vcodec/decoder/mtk_vcodec_dec_drv.h | 2 ++
.../vcodec/decoder/mtk_vcodec_dec_stateless.c | 30 +++++++++++++++----
2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h
index 1fabe8c5b7a4..886fa385e2e6 100644
--- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h
+++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h
@@ -155,6 +155,7 @@ struct mtk_vcodec_dec_pdata {
* @last_decoded_picinfo: pic information get from latest decode
* @empty_flush_buf: a fake size-0 capture buffer that indicates flush. Used
* for stateful decoder.
+ * @cur_src_buffer: current vb2 buffer for the latest decode.
* @is_flushing: set to true if flushing is in progress.
*
* @current_codec: current set input codec, in V4L2 pixel format
@@ -201,6 +202,7 @@ struct mtk_vcodec_dec_ctx {
struct work_struct decode_work;
struct vdec_pic_info last_decoded_picinfo;
struct v4l2_m2m_buffer empty_flush_buf;
+ struct vb2_v4l2_buffer *cur_src_buffer;
bool is_flushing;

u32 current_codec;
diff --git a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c
index fe8ff379ebe8..d213b0f532a9 100644
--- a/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c
+++ b/drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c
@@ -320,7 +320,8 @@ static void mtk_vdec_worker(struct work_struct *work)
struct mtk_vcodec_dec_ctx *ctx =
container_of(work, struct mtk_vcodec_dec_ctx, decode_work);
struct mtk_vcodec_dec_dev *dev = ctx->dev;
- struct vb2_v4l2_buffer *vb2_v4l2_src;
+ struct vb2_v4l2_buffer *vb2_v4l2_src = ctx->cur_src_buffer;
+ struct vb2_v4l2_buffer *vb2_v4l2_dst;
struct vb2_buffer *vb2_src;
struct mtk_vcodec_mem *bs_src;
struct mtk_video_dec_buf *dec_buf_src;
@@ -329,7 +330,7 @@ static void mtk_vdec_worker(struct work_struct *work)
bool res_chg = false;
int ret;

- vb2_v4l2_src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
+ vb2_v4l2_src = vb2_v4l2_src ? vb2_v4l2_src : v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
if (!vb2_v4l2_src) {
v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
mtk_v4l2_vdec_dbg(1, ctx, "[%d] no available source buffer", ctx->id);
@@ -381,12 +382,29 @@ static void mtk_vdec_worker(struct work_struct *work)
ctx->current_codec == V4L2_PIX_FMT_VP8_FRAME) {
if (src_buf_req)
v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
- v4l2_m2m_buf_done_and_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx, state);
- } else {
- if (ret != -EAGAIN)
- v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
+ vb2_v4l2_dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
+ v4l2_m2m_buf_done(vb2_v4l2_dst, state);
+ v4l2_m2m_buf_done(vb2_v4l2_src, state);
+
v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
+ return;
}
+
+ /*
+ * If each codec return -EAGAIN to decode again, need to backup current source
+ * buffer, then the driver will get this buffer next time.
+ *
+ * If each codec decode error, must to set buffer done with error status for
+ * this buffer have been removed from ready list.
+ */
+ ctx->cur_src_buffer = (ret != -EAGAIN) ? NULL : vb2_v4l2_src;
+ if (ret && ret != -EAGAIN) {
+ if (src_buf_req)
+ v4l2_ctrl_request_complete(src_buf_req, &ctx->ctrl_hdl);
+ v4l2_m2m_buf_done(vb2_v4l2_src, state);
+ }
+
+ v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
}

static void vb2ops_vdec_stateless_buf_queue(struct vb2_buffer *vb)
--
2.46.0