}
 
 static const struct hantro_fmt *
-hantro_find_format(const struct hantro_fmt *formats, unsigned int num_fmts,
-                  u32 fourcc)
+hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc)
 {
-       unsigned int i;
+       const struct hantro_fmt *formats;
+       unsigned int i, num_fmts;
 
+       formats = hantro_get_formats(ctx, &num_fmts);
        for (i = 0; i < num_fmts; i++)
                if (formats[i].fourcc == fourcc)
                        return &formats[i];
+
        return NULL;
 }
 
 static const struct hantro_fmt *
-hantro_get_default_fmt(const struct hantro_fmt *formats, unsigned int num_fmts,
-                      bool bitstream)
+hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream)
 {
-       unsigned int i;
+       const struct hantro_fmt *formats;
+       unsigned int i, num_fmts;
 
+       formats = hantro_get_formats(ctx, &num_fmts);
        for (i = 0; i < num_fmts; i++) {
                if (bitstream == (formats[i].codec_mode !=
                                  HANTRO_MODE_NONE))
                                  struct v4l2_frmsizeenum *fsize)
 {
        struct hantro_ctx *ctx = fh_to_ctx(priv);
-       const struct hantro_fmt *formats, *fmt;
-       unsigned int num_fmts;
+       const struct hantro_fmt *fmt;
 
        if (fsize->index != 0) {
                vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
                return -EINVAL;
        }
 
-       formats = hantro_get_formats(ctx, &num_fmts);
-       fmt = hantro_find_format(formats, num_fmts, fsize->pixel_format);
+       fmt = hantro_find_format(ctx, fsize->pixel_format);
        if (!fmt) {
                vpu_debug(0, "unsupported bitstream format (%08x)\n",
                          fsize->pixel_format);
 {
        struct hantro_ctx *ctx = fh_to_ctx(priv);
        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       const struct hantro_fmt *formats, *fmt, *vpu_fmt;
-       unsigned int num_fmts;
+       const struct hantro_fmt *fmt, *vpu_fmt;
        bool coded;
 
        coded = capture == hantro_is_encoder_ctx(ctx);
                  (pix_mp->pixelformat >> 16) & 0x7f,
                  (pix_mp->pixelformat >> 24) & 0x7f);
 
-       formats = hantro_get_formats(ctx, &num_fmts);
-       fmt = hantro_find_format(formats, num_fmts, pix_mp->pixelformat);
+       fmt = hantro_find_format(ctx, pix_mp->pixelformat);
        if (!fmt) {
-               fmt = hantro_get_default_fmt(formats, num_fmts, coded);
+               fmt = hantro_get_default_fmt(ctx, coded);
                f->fmt.pix_mp.pixelformat = fmt->fourcc;
        }
 
 static void
 hantro_reset_encoded_fmt(struct hantro_ctx *ctx)
 {
-       const struct hantro_fmt *vpu_fmt, *formats;
+       const struct hantro_fmt *vpu_fmt;
        struct v4l2_pix_format_mplane *fmt;
-       unsigned int num_fmts;
 
-       formats = hantro_get_formats(ctx, &num_fmts);
-       vpu_fmt = hantro_get_default_fmt(formats, num_fmts, true);
+       vpu_fmt = hantro_get_default_fmt(ctx, true);
 
        if (hantro_is_encoder_ctx(ctx)) {
                ctx->vpu_dst_fmt = vpu_fmt;
 static void
 hantro_reset_raw_fmt(struct hantro_ctx *ctx)
 {
-       const struct hantro_fmt *raw_vpu_fmt, *formats;
+       const struct hantro_fmt *raw_vpu_fmt;
        struct v4l2_pix_format_mplane *raw_fmt, *encoded_fmt;
-       unsigned int num_fmts;
 
-       formats = hantro_get_formats(ctx, &num_fmts);
-       raw_vpu_fmt = hantro_get_default_fmt(formats, num_fmts, false);
+       raw_vpu_fmt = hantro_get_default_fmt(ctx, false);
 
        if (hantro_is_encoder_ctx(ctx)) {
                ctx->vpu_src_fmt = raw_vpu_fmt;
        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
        struct hantro_ctx *ctx = fh_to_ctx(priv);
        struct vb2_queue *vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
-       const struct hantro_fmt *formats;
-       unsigned int num_fmts;
        int ret;
 
        ret = vidioc_try_fmt_out_mplane(file, priv, f);
                        return -EBUSY;
        }
 
-       formats = hantro_get_formats(ctx, &num_fmts);
-       ctx->vpu_src_fmt = hantro_find_format(formats, num_fmts,
-                                             pix_mp->pixelformat);
+       ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
        ctx->src_fmt = *pix_mp;
 
        /*
 {
        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
        struct hantro_ctx *ctx = fh_to_ctx(priv);
-       const struct hantro_fmt *formats;
        struct vb2_queue *vq;
-       unsigned int num_fmts;
        int ret;
 
        /* Change not allowed if queue is busy. */
        if (ret)
                return ret;
 
-       formats = hantro_get_formats(ctx, &num_fmts);
-       ctx->vpu_dst_fmt = hantro_find_format(formats, num_fmts,
-                                             pix_mp->pixelformat);
+       ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat);
        ctx->dst_fmt = *pix_mp;
 
        /*