[6/8] rkvdec: Use vb2_find_buffer

Message ID 20220706182657.210650-7-ezequiel@vanguardiasur.com.ar (mailing list archive)
State Changes Requested
Delegated to: Sakari Ailus
Headers
Series videobuf2: Replace vb2_find_timestamp() with vb2_find_buffer() |

Commit Message

Ezequiel Garcia July 6, 2022, 6:26 p.m. UTC
Use the newly introduced vb2_find_buffer API to get a vb2_buffer
given a buffer timestamp.

Signed-off-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
---
 drivers/staging/media/rkvdec/rkvdec-h264.c | 41 ++++++++--------------
 drivers/staging/media/rkvdec/rkvdec-vp9.c  | 10 +++---
 2 files changed, 19 insertions(+), 32 deletions(-)
  

Comments

Tomasz Figa July 8, 2022, 4:40 a.m. UTC | #1
Hi Ezequiel,

On Thu, Jul 7, 2022 at 3:27 AM Ezequiel Garcia
<ezequiel@vanguardiasur.com.ar> wrote:
>
> Use the newly introduced vb2_find_buffer API to get a vb2_buffer
> given a buffer timestamp.
>
> Signed-off-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
> ---
>  drivers/staging/media/rkvdec/rkvdec-h264.c | 41 ++++++++--------------
>  drivers/staging/media/rkvdec/rkvdec-vp9.c  | 10 +++---
>  2 files changed, 19 insertions(+), 32 deletions(-)
>
> diff --git a/drivers/staging/media/rkvdec/rkvdec-h264.c b/drivers/staging/media/rkvdec/rkvdec-h264.c
> index 2992fb87cf72..4af5a831bde0 100644
> --- a/drivers/staging/media/rkvdec/rkvdec-h264.c
> +++ b/drivers/staging/media/rkvdec/rkvdec-h264.c
> @@ -109,7 +109,7 @@ struct rkvdec_h264_run {
>         const struct v4l2_ctrl_h264_sps *sps;
>         const struct v4l2_ctrl_h264_pps *pps;
>         const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
> -       int ref_buf_idx[V4L2_H264_NUM_DPB_ENTRIES];
> +       struct vb2_buffer *ref_buf[V4L2_H264_NUM_DPB_ENTRIES];

How do we guarantee that those pointers remain valid through the
lifetime of this structure?

Best regards,
Tomasz

>  };
>
>  struct rkvdec_h264_ctx {
> @@ -742,17 +742,16 @@ static void lookup_ref_buf_idx(struct rkvdec_ctx *ctx,
>                 struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
>                 const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
>                 struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> -               int buf_idx = -1;
> +               struct vb2_buffer *buf = NULL;
>
>                 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) {
> -                       buf_idx = vb2_find_timestamp(cap_q,
> -                                                    dpb[i].reference_ts, 0);
> -                       if (buf_idx < 0)
> +                       buf = vb2_find_buffer(cap_q, dpb[i].reference_ts);
> +                       if (!buf)
>                                 pr_debug("No buffer for reference_ts %llu",
>                                          dpb[i].reference_ts);
>                 }
>
> -               run->ref_buf_idx[i] = buf_idx;
> +               run->ref_buf[i] = buf;
>         }
>  }
>
> @@ -805,7 +804,7 @@ static void assemble_hw_rps(struct rkvdec_ctx *ctx,
>                         if (WARN_ON(ref->index >= ARRAY_SIZE(dec_params->dpb)))
>                                 continue;
>
> -                       dpb_valid = run->ref_buf_idx[ref->index] >= 0;
> +                       dpb_valid = run->ref_buf[ref->index] != NULL;
>                         bottom = ref->fields == V4L2_H264_BOTTOM_FIELD_REF;
>
>                         set_ps_field(hw_rps, DPB_INFO(i, j),
> @@ -881,24 +880,6 @@ static const u32 poc_reg_tbl_bottom_field[16] = {
>         RKVDEC_REG_H264_POC_REFER2(1)
>  };
>
> -static struct vb2_buffer *
> -get_ref_buf(struct rkvdec_ctx *ctx, struct rkvdec_h264_run *run,
> -           unsigned int dpb_idx)
> -{
> -       struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> -       struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> -       int buf_idx = run->ref_buf_idx[dpb_idx];
> -
> -       /*
> -        * If a DPB entry is unused or invalid, address of current destination
> -        * buffer is returned.
> -        */
> -       if (buf_idx < 0)
> -               return &run->base.bufs.dst->vb2_buf;
> -
> -       return vb2_get_buffer(cap_q, buf_idx);
> -}
> -
>  static void config_registers(struct rkvdec_ctx *ctx,
>                              struct rkvdec_h264_run *run)
>  {
> @@ -971,8 +952,14 @@ static void config_registers(struct rkvdec_ctx *ctx,
>
>         /* config ref pic address & poc */
>         for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
> -               struct vb2_buffer *vb_buf = get_ref_buf(ctx, run, i);
> -
> +               struct vb2_buffer *vb_buf = run->ref_buf[i];
> +
> +               /*
> +                * If a DPB entry is unused or invalid, address of current destination
> +                * buffer is returned.
> +                */
> +               if (!vb_buf)
> +                       vb_buf = &dst_buf->vb2_buf;
>                 refer_addr = vb2_dma_contig_plane_dma_addr(vb_buf, 0);
>
>                 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
> diff --git a/drivers/staging/media/rkvdec/rkvdec-vp9.c b/drivers/staging/media/rkvdec/rkvdec-vp9.c
> index c2f42e76be10..d8c1c0db15c7 100644
> --- a/drivers/staging/media/rkvdec/rkvdec-vp9.c
> +++ b/drivers/staging/media/rkvdec/rkvdec-vp9.c
> @@ -383,17 +383,17 @@ get_ref_buf(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp)
>  {
>         struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
>         struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> -       int buf_idx;
> +       struct vb2_buffer *buf;
>
>         /*
>          * If a ref is unused or invalid, address of current destination
>          * buffer is returned.
>          */
> -       buf_idx = vb2_find_timestamp(cap_q, timestamp, 0);
> -       if (buf_idx < 0)
> -               return vb2_to_rkvdec_decoded_buf(&dst->vb2_buf);
> +       buf = vb2_find_buffer(cap_q, timestamp);
> +       if (!buf)
> +               buf = &dst->vb2_buf;
>
> -       return vb2_to_rkvdec_decoded_buf(vb2_get_buffer(cap_q, buf_idx));
> +       return vb2_to_rkvdec_decoded_buf(buf);
>  }
>
>  static dma_addr_t get_mv_base_addr(struct rkvdec_decoded_buffer *buf)
> --
> 2.34.3
>
  
Ezequiel Garcia July 8, 2022, 11:21 a.m. UTC | #2
Hi Tomasz,

On Fri, Jul 08, 2022 at 01:40:53PM +0900, Tomasz Figa wrote:
> Hi Ezequiel,
> 
> On Thu, Jul 7, 2022 at 3:27 AM Ezequiel Garcia
> <ezequiel@vanguardiasur.com.ar> wrote:
> >
> > Use the newly introduced vb2_find_buffer API to get a vb2_buffer
> > given a buffer timestamp.
> >
> > Signed-off-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
> > ---
> >  drivers/staging/media/rkvdec/rkvdec-h264.c | 41 ++++++++--------------
> >  drivers/staging/media/rkvdec/rkvdec-vp9.c  | 10 +++---
> >  2 files changed, 19 insertions(+), 32 deletions(-)
> >
> > diff --git a/drivers/staging/media/rkvdec/rkvdec-h264.c b/drivers/staging/media/rkvdec/rkvdec-h264.c
> > index 2992fb87cf72..4af5a831bde0 100644
> > --- a/drivers/staging/media/rkvdec/rkvdec-h264.c
> > +++ b/drivers/staging/media/rkvdec/rkvdec-h264.c
> > @@ -109,7 +109,7 @@ struct rkvdec_h264_run {
> >         const struct v4l2_ctrl_h264_sps *sps;
> >         const struct v4l2_ctrl_h264_pps *pps;
> >         const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
> > -       int ref_buf_idx[V4L2_H264_NUM_DPB_ENTRIES];
> > +       struct vb2_buffer *ref_buf[V4L2_H264_NUM_DPB_ENTRIES];
> 
> How do we guarantee that those pointers remain valid through the
> lifetime of this structure?
> 

The rkvdec_h264_run is populated in .device_run, and used to program
the hardware for each decode job.

So these videobuf2 buffer won't outlive a given decode job. The vb2
queue can't be released (so buffers can't be released) while
a job is runnning (i.e. the driver is in a "streaming" state).

We should be good, right?

Thanks for the review,
Ezequiel

> Best regards,
> Tomasz
> 
> >  };
> >
> >  struct rkvdec_h264_ctx {
> > @@ -742,17 +742,16 @@ static void lookup_ref_buf_idx(struct rkvdec_ctx *ctx,
> >                 struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> >                 const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
> >                 struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> > -               int buf_idx = -1;
> > +               struct vb2_buffer *buf = NULL;
> >
> >                 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) {
> > -                       buf_idx = vb2_find_timestamp(cap_q,
> > -                                                    dpb[i].reference_ts, 0);
> > -                       if (buf_idx < 0)
> > +                       buf = vb2_find_buffer(cap_q, dpb[i].reference_ts);
> > +                       if (!buf)
> >                                 pr_debug("No buffer for reference_ts %llu",
> >                                          dpb[i].reference_ts);
> >                 }
> >
> > -               run->ref_buf_idx[i] = buf_idx;
> > +               run->ref_buf[i] = buf;
> >         }
> >  }
> >
> > @@ -805,7 +804,7 @@ static void assemble_hw_rps(struct rkvdec_ctx *ctx,
> >                         if (WARN_ON(ref->index >= ARRAY_SIZE(dec_params->dpb)))
> >                                 continue;
> >
> > -                       dpb_valid = run->ref_buf_idx[ref->index] >= 0;
> > +                       dpb_valid = run->ref_buf[ref->index] != NULL;
> >                         bottom = ref->fields == V4L2_H264_BOTTOM_FIELD_REF;
> >
> >                         set_ps_field(hw_rps, DPB_INFO(i, j),
> > @@ -881,24 +880,6 @@ static const u32 poc_reg_tbl_bottom_field[16] = {
> >         RKVDEC_REG_H264_POC_REFER2(1)
> >  };
> >
> > -static struct vb2_buffer *
> > -get_ref_buf(struct rkvdec_ctx *ctx, struct rkvdec_h264_run *run,
> > -           unsigned int dpb_idx)
> > -{
> > -       struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> > -       struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> > -       int buf_idx = run->ref_buf_idx[dpb_idx];
> > -
> > -       /*
> > -        * If a DPB entry is unused or invalid, address of current destination
> > -        * buffer is returned.
> > -        */
> > -       if (buf_idx < 0)
> > -               return &run->base.bufs.dst->vb2_buf;
> > -
> > -       return vb2_get_buffer(cap_q, buf_idx);
> > -}
> > -
> >  static void config_registers(struct rkvdec_ctx *ctx,
> >                              struct rkvdec_h264_run *run)
> >  {
> > @@ -971,8 +952,14 @@ static void config_registers(struct rkvdec_ctx *ctx,
> >
> >         /* config ref pic address & poc */
> >         for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
> > -               struct vb2_buffer *vb_buf = get_ref_buf(ctx, run, i);
> > -
> > +               struct vb2_buffer *vb_buf = run->ref_buf[i];
> > +
> > +               /*
> > +                * If a DPB entry is unused or invalid, address of current destination
> > +                * buffer is returned.
> > +                */
> > +               if (!vb_buf)
> > +                       vb_buf = &dst_buf->vb2_buf;
> >                 refer_addr = vb2_dma_contig_plane_dma_addr(vb_buf, 0);
> >
> >                 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
> > diff --git a/drivers/staging/media/rkvdec/rkvdec-vp9.c b/drivers/staging/media/rkvdec/rkvdec-vp9.c
> > index c2f42e76be10..d8c1c0db15c7 100644
> > --- a/drivers/staging/media/rkvdec/rkvdec-vp9.c
> > +++ b/drivers/staging/media/rkvdec/rkvdec-vp9.c
> > @@ -383,17 +383,17 @@ get_ref_buf(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp)
> >  {
> >         struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> >         struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> > -       int buf_idx;
> > +       struct vb2_buffer *buf;
> >
> >         /*
> >          * If a ref is unused or invalid, address of current destination
> >          * buffer is returned.
> >          */
> > -       buf_idx = vb2_find_timestamp(cap_q, timestamp, 0);
> > -       if (buf_idx < 0)
> > -               return vb2_to_rkvdec_decoded_buf(&dst->vb2_buf);
> > +       buf = vb2_find_buffer(cap_q, timestamp);
> > +       if (!buf)
> > +               buf = &dst->vb2_buf;
> >
> > -       return vb2_to_rkvdec_decoded_buf(vb2_get_buffer(cap_q, buf_idx));
> > +       return vb2_to_rkvdec_decoded_buf(buf);
> >  }
> >
> >  static dma_addr_t get_mv_base_addr(struct rkvdec_decoded_buffer *buf)
> > --
> > 2.34.3
> >
  
Tomasz Figa July 8, 2022, 11:45 a.m. UTC | #3
On Fri, Jul 8, 2022 at 8:21 PM Ezequiel Garcia
<ezequiel@vanguardiasur.com.ar> wrote:
>
> Hi Tomasz,
>
> On Fri, Jul 08, 2022 at 01:40:53PM +0900, Tomasz Figa wrote:
> > Hi Ezequiel,
> >
> > On Thu, Jul 7, 2022 at 3:27 AM Ezequiel Garcia
> > <ezequiel@vanguardiasur.com.ar> wrote:
> > >
> > > Use the newly introduced vb2_find_buffer API to get a vb2_buffer
> > > given a buffer timestamp.
> > >
> > > Signed-off-by: Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>
> > > ---
> > >  drivers/staging/media/rkvdec/rkvdec-h264.c | 41 ++++++++--------------
> > >  drivers/staging/media/rkvdec/rkvdec-vp9.c  | 10 +++---
> > >  2 files changed, 19 insertions(+), 32 deletions(-)
> > >
> > > diff --git a/drivers/staging/media/rkvdec/rkvdec-h264.c b/drivers/staging/media/rkvdec/rkvdec-h264.c
> > > index 2992fb87cf72..4af5a831bde0 100644
> > > --- a/drivers/staging/media/rkvdec/rkvdec-h264.c
> > > +++ b/drivers/staging/media/rkvdec/rkvdec-h264.c
> > > @@ -109,7 +109,7 @@ struct rkvdec_h264_run {
> > >         const struct v4l2_ctrl_h264_sps *sps;
> > >         const struct v4l2_ctrl_h264_pps *pps;
> > >         const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
> > > -       int ref_buf_idx[V4L2_H264_NUM_DPB_ENTRIES];
> > > +       struct vb2_buffer *ref_buf[V4L2_H264_NUM_DPB_ENTRIES];
> >
> > How do we guarantee that those pointers remain valid through the
> > lifetime of this structure?
> >
>
> The rkvdec_h264_run is populated in .device_run, and used to program
> the hardware for each decode job.
>
> So these videobuf2 buffer won't outlive a given decode job. The vb2
> queue can't be released (so buffers can't be released) while
> a job is runnning (i.e. the driver is in a "streaming" state).
>
> We should be good, right?

Makes sense, thanks!

I think the only other comment was about the new helper being
initially inline and then turned back into a regular function, so if
we don't get any other comments in a few more days, feel free to
ignore that one.

Best regards,
Tomasz

>
> Thanks for the review,
> Ezequiel
>
> > Best regards,
> > Tomasz
> >
> > >  };
> > >
> > >  struct rkvdec_h264_ctx {
> > > @@ -742,17 +742,16 @@ static void lookup_ref_buf_idx(struct rkvdec_ctx *ctx,
> > >                 struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> > >                 const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
> > >                 struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> > > -               int buf_idx = -1;
> > > +               struct vb2_buffer *buf = NULL;
> > >
> > >                 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) {
> > > -                       buf_idx = vb2_find_timestamp(cap_q,
> > > -                                                    dpb[i].reference_ts, 0);
> > > -                       if (buf_idx < 0)
> > > +                       buf = vb2_find_buffer(cap_q, dpb[i].reference_ts);
> > > +                       if (!buf)
> > >                                 pr_debug("No buffer for reference_ts %llu",
> > >                                          dpb[i].reference_ts);
> > >                 }
> > >
> > > -               run->ref_buf_idx[i] = buf_idx;
> > > +               run->ref_buf[i] = buf;
> > >         }
> > >  }
> > >
> > > @@ -805,7 +804,7 @@ static void assemble_hw_rps(struct rkvdec_ctx *ctx,
> > >                         if (WARN_ON(ref->index >= ARRAY_SIZE(dec_params->dpb)))
> > >                                 continue;
> > >
> > > -                       dpb_valid = run->ref_buf_idx[ref->index] >= 0;
> > > +                       dpb_valid = run->ref_buf[ref->index] != NULL;
> > >                         bottom = ref->fields == V4L2_H264_BOTTOM_FIELD_REF;
> > >
> > >                         set_ps_field(hw_rps, DPB_INFO(i, j),
> > > @@ -881,24 +880,6 @@ static const u32 poc_reg_tbl_bottom_field[16] = {
> > >         RKVDEC_REG_H264_POC_REFER2(1)
> > >  };
> > >
> > > -static struct vb2_buffer *
> > > -get_ref_buf(struct rkvdec_ctx *ctx, struct rkvdec_h264_run *run,
> > > -           unsigned int dpb_idx)
> > > -{
> > > -       struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> > > -       struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> > > -       int buf_idx = run->ref_buf_idx[dpb_idx];
> > > -
> > > -       /*
> > > -        * If a DPB entry is unused or invalid, address of current destination
> > > -        * buffer is returned.
> > > -        */
> > > -       if (buf_idx < 0)
> > > -               return &run->base.bufs.dst->vb2_buf;
> > > -
> > > -       return vb2_get_buffer(cap_q, buf_idx);
> > > -}
> > > -
> > >  static void config_registers(struct rkvdec_ctx *ctx,
> > >                              struct rkvdec_h264_run *run)
> > >  {
> > > @@ -971,8 +952,14 @@ static void config_registers(struct rkvdec_ctx *ctx,
> > >
> > >         /* config ref pic address & poc */
> > >         for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
> > > -               struct vb2_buffer *vb_buf = get_ref_buf(ctx, run, i);
> > > -
> > > +               struct vb2_buffer *vb_buf = run->ref_buf[i];
> > > +
> > > +               /*
> > > +                * If a DPB entry is unused or invalid, address of current destination
> > > +                * buffer is returned.
> > > +                */
> > > +               if (!vb_buf)
> > > +                       vb_buf = &dst_buf->vb2_buf;
> > >                 refer_addr = vb2_dma_contig_plane_dma_addr(vb_buf, 0);
> > >
> > >                 if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
> > > diff --git a/drivers/staging/media/rkvdec/rkvdec-vp9.c b/drivers/staging/media/rkvdec/rkvdec-vp9.c
> > > index c2f42e76be10..d8c1c0db15c7 100644
> > > --- a/drivers/staging/media/rkvdec/rkvdec-vp9.c
> > > +++ b/drivers/staging/media/rkvdec/rkvdec-vp9.c
> > > @@ -383,17 +383,17 @@ get_ref_buf(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp)
> > >  {
> > >         struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
> > >         struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
> > > -       int buf_idx;
> > > +       struct vb2_buffer *buf;
> > >
> > >         /*
> > >          * If a ref is unused or invalid, address of current destination
> > >          * buffer is returned.
> > >          */
> > > -       buf_idx = vb2_find_timestamp(cap_q, timestamp, 0);
> > > -       if (buf_idx < 0)
> > > -               return vb2_to_rkvdec_decoded_buf(&dst->vb2_buf);
> > > +       buf = vb2_find_buffer(cap_q, timestamp);
> > > +       if (!buf)
> > > +               buf = &dst->vb2_buf;
> > >
> > > -       return vb2_to_rkvdec_decoded_buf(vb2_get_buffer(cap_q, buf_idx));
> > > +       return vb2_to_rkvdec_decoded_buf(buf);
> > >  }
> > >
> > >  static dma_addr_t get_mv_base_addr(struct rkvdec_decoded_buffer *buf)
> > > --
> > > 2.34.3
> > >
  

Patch

diff --git a/drivers/staging/media/rkvdec/rkvdec-h264.c b/drivers/staging/media/rkvdec/rkvdec-h264.c
index 2992fb87cf72..4af5a831bde0 100644
--- a/drivers/staging/media/rkvdec/rkvdec-h264.c
+++ b/drivers/staging/media/rkvdec/rkvdec-h264.c
@@ -109,7 +109,7 @@  struct rkvdec_h264_run {
 	const struct v4l2_ctrl_h264_sps *sps;
 	const struct v4l2_ctrl_h264_pps *pps;
 	const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
-	int ref_buf_idx[V4L2_H264_NUM_DPB_ENTRIES];
+	struct vb2_buffer *ref_buf[V4L2_H264_NUM_DPB_ENTRIES];
 };
 
 struct rkvdec_h264_ctx {
@@ -742,17 +742,16 @@  static void lookup_ref_buf_idx(struct rkvdec_ctx *ctx,
 		struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
 		const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
 		struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
-		int buf_idx = -1;
+		struct vb2_buffer *buf = NULL;
 
 		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) {
-			buf_idx = vb2_find_timestamp(cap_q,
-						     dpb[i].reference_ts, 0);
-			if (buf_idx < 0)
+			buf = vb2_find_buffer(cap_q, dpb[i].reference_ts);
+			if (!buf)
 				pr_debug("No buffer for reference_ts %llu",
 					 dpb[i].reference_ts);
 		}
 
-		run->ref_buf_idx[i] = buf_idx;
+		run->ref_buf[i] = buf;
 	}
 }
 
@@ -805,7 +804,7 @@  static void assemble_hw_rps(struct rkvdec_ctx *ctx,
 			if (WARN_ON(ref->index >= ARRAY_SIZE(dec_params->dpb)))
 				continue;
 
-			dpb_valid = run->ref_buf_idx[ref->index] >= 0;
+			dpb_valid = run->ref_buf[ref->index] != NULL;
 			bottom = ref->fields == V4L2_H264_BOTTOM_FIELD_REF;
 
 			set_ps_field(hw_rps, DPB_INFO(i, j),
@@ -881,24 +880,6 @@  static const u32 poc_reg_tbl_bottom_field[16] = {
 	RKVDEC_REG_H264_POC_REFER2(1)
 };
 
-static struct vb2_buffer *
-get_ref_buf(struct rkvdec_ctx *ctx, struct rkvdec_h264_run *run,
-	    unsigned int dpb_idx)
-{
-	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
-	struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
-	int buf_idx = run->ref_buf_idx[dpb_idx];
-
-	/*
-	 * If a DPB entry is unused or invalid, address of current destination
-	 * buffer is returned.
-	 */
-	if (buf_idx < 0)
-		return &run->base.bufs.dst->vb2_buf;
-
-	return vb2_get_buffer(cap_q, buf_idx);
-}
-
 static void config_registers(struct rkvdec_ctx *ctx,
 			     struct rkvdec_h264_run *run)
 {
@@ -971,8 +952,14 @@  static void config_registers(struct rkvdec_ctx *ctx,
 
 	/* config ref pic address & poc */
 	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
-		struct vb2_buffer *vb_buf = get_ref_buf(ctx, run, i);
-
+		struct vb2_buffer *vb_buf = run->ref_buf[i];
+
+		/*
+		 * If a DPB entry is unused or invalid, address of current destination
+		 * buffer is returned.
+		 */
+		if (!vb_buf)
+			vb_buf = &dst_buf->vb2_buf;
 		refer_addr = vb2_dma_contig_plane_dma_addr(vb_buf, 0);
 
 		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
diff --git a/drivers/staging/media/rkvdec/rkvdec-vp9.c b/drivers/staging/media/rkvdec/rkvdec-vp9.c
index c2f42e76be10..d8c1c0db15c7 100644
--- a/drivers/staging/media/rkvdec/rkvdec-vp9.c
+++ b/drivers/staging/media/rkvdec/rkvdec-vp9.c
@@ -383,17 +383,17 @@  get_ref_buf(struct rkvdec_ctx *ctx, struct vb2_v4l2_buffer *dst, u64 timestamp)
 {
 	struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
 	struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
-	int buf_idx;
+	struct vb2_buffer *buf;
 
 	/*
 	 * If a ref is unused or invalid, address of current destination
 	 * buffer is returned.
 	 */
-	buf_idx = vb2_find_timestamp(cap_q, timestamp, 0);
-	if (buf_idx < 0)
-		return vb2_to_rkvdec_decoded_buf(&dst->vb2_buf);
+	buf = vb2_find_buffer(cap_q, timestamp);
+	if (!buf)
+		buf = &dst->vb2_buf;
 
-	return vb2_to_rkvdec_decoded_buf(vb2_get_buffer(cap_q, buf_idx));
+	return vb2_to_rkvdec_decoded_buf(buf);
 }
 
 static dma_addr_t get_mv_base_addr(struct rkvdec_decoded_buffer *buf)