bool hantro_is_encoder_ctx(const struct hantro_ctx *ctx);
 
 void *hantro_get_ctrl(struct hantro_ctx *ctx, u32 id);
-dma_addr_t hantro_get_ref(struct vb2_queue *q, u64 ts);
+dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts);
 
 static inline struct vb2_v4l2_buffer *
 hantro_get_src_buf(struct hantro_ctx *ctx)
 
        return ctrl ? ctrl->p_cur.p : NULL;
 }
 
-dma_addr_t hantro_get_ref(struct vb2_queue *q, u64 ts)
+dma_addr_t hantro_get_ref(struct hantro_ctx *ctx, u64 ts)
 {
+       struct vb2_queue *q = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx);
        struct vb2_buffer *buf;
        int index;
 
 
 
        /* Set up addresses of DPB buffers. */
        for (i = 0; i < HANTRO_H264_DPB_SIZE; i++) {
-               struct vb2_buffer *buf =  hantro_h264_get_ref_buf(ctx, i);
+               dma_addr_t dma_addr = hantro_h264_get_ref_buf(ctx, i);
 
-               vdpu_write_relaxed(vpu, vb2_dma_contig_plane_dma_addr(buf, 0),
-                                  G1_REG_ADDR_REF(i));
+               vdpu_write_relaxed(vpu, dma_addr, G1_REG_ADDR_REF(i));
        }
 }
 
 
 {
        dma_addr_t forward_addr = 0, backward_addr = 0;
        dma_addr_t current_addr, addr;
-       struct vb2_queue *vq;
-
-       vq = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx);
 
        switch (picture->picture_coding_type) {
        case V4L2_MPEG2_PICTURE_CODING_TYPE_B:
-               backward_addr = hantro_get_ref(vq,
+               backward_addr = hantro_get_ref(ctx,
                                               slice_params->backward_ref_ts);
                /* fall-through */
        case V4L2_MPEG2_PICTURE_CODING_TYPE_P:
-               forward_addr = hantro_get_ref(vq,
+               forward_addr = hantro_get_ref(ctx,
                                              slice_params->forward_ref_ts);
        }
 
 
 static void cfg_ref(struct hantro_ctx *ctx,
                    const struct v4l2_ctrl_vp8_frame_header *hdr)
 {
-       struct vb2_queue *cap_q = &ctx->fh.m2m_ctx->cap_q_ctx.q;
        struct hantro_dev *vpu = ctx->dev;
        struct vb2_v4l2_buffer *vb2_dst;
        dma_addr_t ref;
 
        vb2_dst = hantro_get_dst_buf(ctx);
 
-       ref = hantro_get_ref(cap_q, hdr->last_frame_ts);
+       ref = hantro_get_ref(ctx, hdr->last_frame_ts);
        if (!ref)
                ref = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);
        vdpu_write_relaxed(vpu, ref, G1_REG_ADDR_REF(0));
 
-       ref = hantro_get_ref(cap_q, hdr->golden_frame_ts);
+       ref = hantro_get_ref(ctx, hdr->golden_frame_ts);
        WARN_ON(!ref && hdr->golden_frame_ts);
        if (!ref)
                ref = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);
                ref |= G1_REG_ADDR_REF_TOPC_E;
        vdpu_write_relaxed(vpu, ref, G1_REG_ADDR_REF(4));
 
-       ref = hantro_get_ref(cap_q, hdr->alt_frame_ts);
+       ref = hantro_get_ref(ctx, hdr->alt_frame_ts);
        WARN_ON(!ref && hdr->alt_frame_ts);
        if (!ref)
                ref = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);
 
        }
 }
 
-struct vb2_buffer *hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
-                                          unsigned int dpb_idx)
+dma_addr_t hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
+                                  unsigned int dpb_idx)
 {
-       struct vb2_queue *cap_q = &ctx->fh.m2m_ctx->cap_q_ctx.q;
        struct v4l2_h264_dpb_entry *dpb = ctx->h264_dec.dpb;
-       struct vb2_buffer *buf;
-       int buf_idx = -1;
+       dma_addr_t dma_addr = 0;
 
        if (dpb[dpb_idx].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
-               buf_idx = vb2_find_timestamp(cap_q,
-                                            dpb[dpb_idx].reference_ts, 0);
+               dma_addr = hantro_get_ref(ctx, dpb[dpb_idx].reference_ts);
 
-       if (buf_idx >= 0) {
-               buf = vb2_get_buffer(cap_q, buf_idx);
-       } else {
+       if (!dma_addr) {
                struct vb2_v4l2_buffer *dst_buf;
+               struct vb2_buffer *buf;
 
                /*
                 * If a DPB entry is unused or invalid, address of current
                 */
                dst_buf = hantro_get_dst_buf(ctx);
                buf = &dst_buf->vb2_buf;
+               dma_addr = vb2_dma_contig_plane_dma_addr(buf, 0);
        }
 
-       return buf;
+       return dma_addr;
 }
 
 int hantro_h264_dec_prepare_run(struct hantro_ctx *ctx)
 
 int hantro_jpeg_enc_init(struct hantro_ctx *ctx);
 void hantro_jpeg_enc_exit(struct hantro_ctx *ctx);
 
-struct vb2_buffer *hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
-                                          unsigned int dpb_idx);
+dma_addr_t hantro_h264_get_ref_buf(struct hantro_ctx *ctx,
+                                  unsigned int dpb_idx);
 int hantro_h264_dec_prepare_run(struct hantro_ctx *ctx);
 void hantro_g1_h264_dec_run(struct hantro_ctx *ctx);
 int hantro_h264_dec_init(struct hantro_ctx *ctx);
 
 {
        dma_addr_t forward_addr = 0, backward_addr = 0;
        dma_addr_t current_addr, addr;
-       struct vb2_queue *vq;
-
-       vq = v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx);
 
        switch (picture->picture_coding_type) {
        case V4L2_MPEG2_PICTURE_CODING_TYPE_B:
-               backward_addr = hantro_get_ref(vq,
+               backward_addr = hantro_get_ref(ctx,
                                               slice_params->backward_ref_ts);
                /* fall-through */
        case V4L2_MPEG2_PICTURE_CODING_TYPE_P:
-               forward_addr = hantro_get_ref(vq,
+               forward_addr = hantro_get_ref(ctx,
                                              slice_params->forward_ref_ts);
        }
 
 
 {
        struct hantro_dev *vpu = ctx->dev;
        struct vb2_v4l2_buffer *vb2_dst;
-       struct vb2_queue *cap_q;
        dma_addr_t ref;
 
-       cap_q = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
        vb2_dst = hantro_get_dst_buf(ctx);
 
-       ref = hantro_get_ref(cap_q, hdr->last_frame_ts);
+       ref = hantro_get_ref(ctx, hdr->last_frame_ts);
        if (!ref)
                ref = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);
        vdpu_write_relaxed(vpu, ref, VDPU_REG_VP8_ADDR_REF0);
 
-       ref = hantro_get_ref(cap_q, hdr->golden_frame_ts);
+       ref = hantro_get_ref(ctx, hdr->golden_frame_ts);
        WARN_ON(!ref && hdr->golden_frame_ts);
        if (!ref)
                ref = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);
                ref |= VDPU_REG_VP8_GREF_SIGN_BIAS;
        vdpu_write_relaxed(vpu, ref, VDPU_REG_VP8_ADDR_REF2_5(2));
 
-       ref = hantro_get_ref(cap_q, hdr->alt_frame_ts);
+       ref = hantro_get_ref(ctx, hdr->alt_frame_ts);
        WARN_ON(!ref && hdr->alt_frame_ts);
        if (!ref)
                ref = vb2_dma_contig_plane_dma_addr(&vb2_dst->vb2_buf, 0);