media: replace strncpy() by strscpy()
[linux-block.git] / drivers / media / platform / vicodec / vicodec-core.c
index d7636fe9e1749641077fe350632df0a2a1780cfe..bd01a9206aa69c38d09e30e52100b996f3797e01 100644 (file)
@@ -64,6 +64,10 @@ static const struct v4l2_fwht_pixfmt_info pixfmt_fwht = {
        V4L2_PIX_FMT_FWHT, 0, 3, 1, 1, 1, 1, 1, 0, 1
 };
 
+static const struct v4l2_fwht_pixfmt_info pixfmt_stateless_fwht = {
+       V4L2_PIX_FMT_FWHT_STATELESS, 0, 3, 1, 1, 1, 1, 1, 0, 1
+};
+
 static void vicodec_dev_release(struct device *dev)
 {
 }
@@ -89,27 +93,29 @@ enum {
        V4L2_M2M_DST = 1,
 };
 
+struct vicodec_dev_instance {
+       struct video_device     vfd;
+       struct mutex            mutex;
+       spinlock_t              lock;
+       struct v4l2_m2m_dev     *m2m_dev;
+};
+
 struct vicodec_dev {
        struct v4l2_device      v4l2_dev;
-       struct video_device     enc_vfd;
-       struct video_device     dec_vfd;
+       struct vicodec_dev_instance stateful_enc;
+       struct vicodec_dev_instance stateful_dec;
+       struct vicodec_dev_instance stateless_dec;
 #ifdef CONFIG_MEDIA_CONTROLLER
        struct media_device     mdev;
 #endif
 
-       struct mutex            enc_mutex;
-       struct mutex            dec_mutex;
-       spinlock_t              enc_lock;
-       spinlock_t              dec_lock;
-
-       struct v4l2_m2m_dev     *enc_dev;
-       struct v4l2_m2m_dev     *dec_dev;
 };
 
 struct vicodec_ctx {
        struct v4l2_fh          fh;
        struct vicodec_dev      *dev;
        bool                    is_enc;
+       bool                    is_stateless;
        spinlock_t              *lock;
 
        struct v4l2_ctrl_handler hdl;
@@ -148,27 +154,149 @@ static struct vicodec_q_data *get_q_data(struct vicodec_ctx *ctx,
        case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
                return &ctx->q_data[V4L2_M2M_DST];
        default:
-               WARN_ON(1);
                break;
        }
        return NULL;
 }
 
+static void copy_cap_to_ref(const u8 *cap, const struct v4l2_fwht_pixfmt_info *info,
+               struct v4l2_fwht_state *state)
+{
+       int plane_idx;
+       u8 *p_ref = state->ref_frame.buf;
+       unsigned int cap_stride = state->stride;
+       unsigned int ref_stride = state->ref_stride;
+
+       for (plane_idx = 0; plane_idx < info->planes_num; plane_idx++) {
+               int i;
+               unsigned int h_div = (plane_idx == 1 || plane_idx == 2) ?
+                       info->height_div : 1;
+               const u8 *row_cap = cap;
+               u8 *row_ref = p_ref;
+
+               if (info->planes_num == 3 && plane_idx == 1) {
+                       cap_stride /= 2;
+                       ref_stride /= 2;
+               }
+
+               if (plane_idx == 1 &&
+                   (info->id == V4L2_PIX_FMT_NV24 ||
+                    info->id == V4L2_PIX_FMT_NV42)) {
+                       cap_stride *= 2;
+                       ref_stride *= 2;
+               }
+
+               for (i = 0; i < state->visible_height / h_div; i++) {
+                       memcpy(row_ref, row_cap, ref_stride);
+                       row_ref += ref_stride;
+                       row_cap += cap_stride;
+               }
+               cap += cap_stride * (state->coded_height / h_div);
+               p_ref += ref_stride * (state->coded_height / h_div);
+       }
+}
+
+static bool validate_by_version(unsigned int flags, unsigned int version)
+{
+       if (!version || version > FWHT_VERSION)
+               return false;
+
+       if (version >= 2) {
+               unsigned int components_num = 1 +
+                       ((flags & FWHT_FL_COMPONENTS_NUM_MSK) >>
+                        FWHT_FL_COMPONENTS_NUM_OFFSET);
+               unsigned int pixenc = flags & FWHT_FL_PIXENC_MSK;
+
+               if (components_num == 0 || components_num > 4 || !pixenc)
+                       return false;
+       }
+       return true;
+}
+
+static bool validate_stateless_params_flags(const struct v4l2_ctrl_fwht_params *params,
+                                           const struct v4l2_fwht_pixfmt_info *cur_info)
+{
+       unsigned int width_div =
+               (params->flags & FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
+       unsigned int height_div =
+               (params->flags & FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
+       unsigned int components_num = 3;
+       unsigned int pixenc = 0;
+
+       if (params->version < 3)
+               return false;
+
+       components_num = 1 + ((params->flags & FWHT_FL_COMPONENTS_NUM_MSK) >>
+                             FWHT_FL_COMPONENTS_NUM_OFFSET);
+       pixenc = (params->flags & FWHT_FL_PIXENC_MSK);
+       if (v4l2_fwht_validate_fmt(cur_info, width_div, height_div,
+                                   components_num, pixenc))
+               return true;
+       return false;
+}
+
+
+static void update_state_from_header(struct vicodec_ctx *ctx)
+{
+       const struct fwht_cframe_hdr *p_hdr = &ctx->state.header;
+
+       ctx->state.visible_width = ntohl(p_hdr->width);
+       ctx->state.visible_height = ntohl(p_hdr->height);
+       ctx->state.colorspace = ntohl(p_hdr->colorspace);
+       ctx->state.xfer_func = ntohl(p_hdr->xfer_func);
+       ctx->state.ycbcr_enc = ntohl(p_hdr->ycbcr_enc);
+       ctx->state.quantization = ntohl(p_hdr->quantization);
+}
+
 static int device_process(struct vicodec_ctx *ctx,
                          struct vb2_v4l2_buffer *src_vb,
                          struct vb2_v4l2_buffer *dst_vb)
 {
        struct vicodec_dev *dev = ctx->dev;
-       struct vicodec_q_data *q_dst;
        struct v4l2_fwht_state *state = &ctx->state;
        u8 *p_src, *p_dst;
-       int ret;
+       int ret = 0;
 
-       q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
-       if (ctx->is_enc)
+       if (ctx->is_enc || ctx->is_stateless)
                p_src = vb2_plane_vaddr(&src_vb->vb2_buf, 0);
        else
                p_src = state->compressed_frame;
+
+       if (ctx->is_stateless) {
+               struct media_request *src_req = src_vb->vb2_buf.req_obj.req;
+
+               ret = v4l2_ctrl_request_setup(src_req, &ctx->hdl);
+               if (ret)
+                       return ret;
+               update_state_from_header(ctx);
+
+               ctx->state.header.size =
+                       htonl(vb2_get_plane_payload(&src_vb->vb2_buf, 0));
+               /*
+                * set the reference buffer from the reference timestamp
+                * only if this is a P-frame
+                */
+               if (!(ntohl(ctx->state.header.flags) & FWHT_FL_I_FRAME)) {
+                       struct vb2_buffer *ref_vb2_buf;
+                       int ref_buf_idx;
+                       struct vb2_queue *vq_cap =
+                               v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
+                                               V4L2_BUF_TYPE_VIDEO_CAPTURE);
+
+                       ref_buf_idx = vb2_find_timestamp(vq_cap,
+                                                        ctx->state.ref_frame_ts, 0);
+                       if (ref_buf_idx < 0)
+                               return -EINVAL;
+
+                       ref_vb2_buf = vq_cap->bufs[ref_buf_idx];
+                       if (ref_vb2_buf->state == VB2_BUF_STATE_ERROR)
+                               ret = -EINVAL;
+                       ctx->state.ref_frame.buf =
+                               vb2_plane_vaddr(ref_vb2_buf, 0);
+               } else {
+                       ctx->state.ref_frame.buf = NULL;
+               }
+       }
        p_dst = vb2_plane_vaddr(&dst_vb->vb2_buf, 0);
        if (!p_src || !p_dst) {
                v4l2_err(&dev->v4l2_dev,
@@ -178,30 +306,31 @@ static int device_process(struct vicodec_ctx *ctx,
 
        if (ctx->is_enc) {
                struct vicodec_q_data *q_src;
+               int comp_sz_or_errcode;
 
                q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
                state->info = q_src->info;
-               ret = v4l2_fwht_encode(state, p_src, p_dst);
-               if (ret < 0)
-                       return ret;
-               vb2_set_plane_payload(&dst_vb->vb2_buf, 0, ret);
+               comp_sz_or_errcode = v4l2_fwht_encode(state, p_src, p_dst);
+               if (comp_sz_or_errcode < 0)
+                       return comp_sz_or_errcode;
+               vb2_set_plane_payload(&dst_vb->vb2_buf, 0, comp_sz_or_errcode);
        } else {
+               struct vicodec_q_data *q_dst;
                unsigned int comp_frame_size = ntohl(ctx->state.header.size);
 
+               q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
                if (comp_frame_size > ctx->comp_max_size)
                        return -EINVAL;
                state->info = q_dst->info;
                ret = v4l2_fwht_decode(state, p_src, p_dst);
                if (ret < 0)
                        return ret;
+               if (!ctx->is_stateless)
+                       copy_cap_to_ref(p_dst, ctx->state.info, &ctx->state);
+
                vb2_set_plane_payload(&dst_vb->vb2_buf, 0, q_dst->sizeimage);
        }
-
-       dst_vb->sequence = q_dst->sequence++;
-       dst_vb->flags &= ~V4L2_BUF_FLAG_LAST;
-       v4l2_m2m_buf_copy_metadata(src_vb, dst_vb, !ctx->is_enc);
-
-       return 0;
+       return ret;
 }
 
 /*
@@ -274,16 +403,26 @@ static void device_run(void *priv)
        struct vicodec_ctx *ctx = priv;
        struct vicodec_dev *dev = ctx->dev;
        struct vb2_v4l2_buffer *src_buf, *dst_buf;
-       struct vicodec_q_data *q_src;
+       struct vicodec_q_data *q_src, *q_dst;
        u32 state;
+       struct media_request *src_req;
+
 
        src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
        dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
+       src_req = src_buf->vb2_buf.req_obj.req;
+
        q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
+       q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
 
        state = VB2_BUF_STATE_DONE;
        if (device_process(ctx, src_buf, dst_buf))
                state = VB2_BUF_STATE_ERROR;
+       else
+               dst_buf->sequence = q_dst->sequence++;
+       dst_buf->flags &= ~V4L2_BUF_FLAG_LAST;
+       v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, !ctx->is_enc);
+
        ctx->last_dst_buf = dst_buf;
 
        spin_lock(ctx->lock);
@@ -291,7 +430,7 @@ static void device_run(void *priv)
                dst_buf->flags |= V4L2_BUF_FLAG_LAST;
                v4l2_event_queue_fh(&ctx->fh, &eos_event);
        }
-       if (ctx->is_enc) {
+       if (ctx->is_enc || ctx->is_stateless) {
                src_buf->sequence = q_src->sequence++;
                src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
                v4l2_m2m_buf_done(src_buf, state);
@@ -303,6 +442,9 @@ static void device_run(void *priv)
                ctx->comp_has_next_frame = false;
        }
        v4l2_m2m_buf_done(dst_buf, state);
+       if (ctx->is_stateless && src_req)
+               v4l2_ctrl_request_complete(src_req, &ctx->hdl);
+
        ctx->comp_size = 0;
        ctx->header_size = 0;
        ctx->comp_magic_cnt = 0;
@@ -310,9 +452,12 @@ static void device_run(void *priv)
        spin_unlock(ctx->lock);
 
        if (ctx->is_enc)
-               v4l2_m2m_job_finish(dev->enc_dev, ctx->fh.m2m_ctx);
+               v4l2_m2m_job_finish(dev->stateful_enc.m2m_dev, ctx->fh.m2m_ctx);
+       else if (ctx->is_stateless)
+               v4l2_m2m_job_finish(dev->stateless_dec.m2m_dev,
+                                   ctx->fh.m2m_ctx);
        else
-               v4l2_m2m_job_finish(dev->dec_dev, ctx->fh.m2m_ctx);
+               v4l2_m2m_job_finish(dev->stateful_dec.m2m_dev, ctx->fh.m2m_ctx);
 }
 
 static void job_remove_src_buf(struct vicodec_ctx *ctx, u32 state)
@@ -344,7 +489,7 @@ info_from_header(const struct fwht_cframe_hdr *p_hdr)
                                FWHT_FL_COMPONENTS_NUM_OFFSET);
                pixenc = (flags & FWHT_FL_PIXENC_MSK);
        }
-       return v4l2_fwht_default_fmt(width_div, height_div,
+       return v4l2_fwht_find_nth_fmt(width_div, height_div,
                                     components_num, pixenc, 0);
 }
 
@@ -356,21 +501,11 @@ static bool is_header_valid(const struct fwht_cframe_hdr *p_hdr)
        unsigned int version = ntohl(p_hdr->version);
        unsigned int flags = ntohl(p_hdr->flags);
 
-       if (!version || version > FWHT_VERSION)
-               return false;
-
        if (w < MIN_WIDTH || w > MAX_WIDTH || h < MIN_HEIGHT || h > MAX_HEIGHT)
                return false;
 
-       if (version >= 2) {
-               unsigned int components_num = 1 +
-                       ((flags & FWHT_FL_COMPONENTS_NUM_MSK) >>
-                       FWHT_FL_COMPONENTS_NUM_OFFSET);
-               unsigned int pixenc = flags & FWHT_FL_PIXENC_MSK;
-
-               if (components_num == 0 || components_num > 4 || !pixenc)
-                       return false;
-       }
+       if (!validate_by_version(flags, version))
+               return false;
 
        info = info_from_header(p_hdr);
        if (!info)
@@ -388,6 +523,12 @@ static void update_capture_data_from_header(struct vicodec_ctx *ctx)
        unsigned int hdr_width_div = (flags & FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
        unsigned int hdr_height_div = (flags & FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
 
+       /*
+        * This function should not be used by a stateless codec since
+        * it changes values in q_data that are not request specific
+        */
+       WARN_ON(ctx->is_stateless);
+
        q_dst->info = info;
        q_dst->visible_width = ntohl(p_hdr->width);
        q_dst->visible_height = ntohl(p_hdr->height);
@@ -440,7 +581,7 @@ static int job_ready(void *priv)
 
        if (ctx->source_changed)
                return 0;
-       if (ctx->is_enc || ctx->comp_has_frame)
+       if (ctx->is_stateless || ctx->is_enc || ctx->comp_has_frame)
                return 1;
 
 restart:
@@ -551,8 +692,8 @@ static const struct v4l2_fwht_pixfmt_info *find_fmt(u32 fmt)
 static int vidioc_querycap(struct file *file, void *priv,
                           struct v4l2_capability *cap)
 {
-       strncpy(cap->driver, VICODEC_NAME, sizeof(cap->driver) - 1);
-       strncpy(cap->card, VICODEC_NAME, sizeof(cap->card) - 1);
+       strscpy(cap->driver, VICODEC_NAME, sizeof(cap->driver));
+       strscpy(cap->card, VICODEC_NAME, sizeof(cap->card));
        snprintf(cap->bus_info, sizeof(cap->bus_info),
                        "platform:%s", VICODEC_NAME);
        return 0;
@@ -575,7 +716,7 @@ static int enum_fmt(struct v4l2_fmtdesc *f, struct vicodec_ctx *ctx,
                if (!info || ctx->is_enc)
                        info = v4l2_fwht_get_pixfmt(f->index);
                else
-                       info = v4l2_fwht_default_fmt(info->width_div,
+                       info = v4l2_fwht_find_nth_fmt(info->width_div,
                                                     info->height_div,
                                                     info->components_num,
                                                     info->pixenc,
@@ -586,7 +727,8 @@ static int enum_fmt(struct v4l2_fmtdesc *f, struct vicodec_ctx *ctx,
        } else {
                if (f->index)
                        return -EINVAL;
-               f->pixelformat = V4L2_PIX_FMT_FWHT;
+               f->pixelformat = ctx->is_stateless ?
+                       V4L2_PIX_FMT_FWHT_STATELESS : V4L2_PIX_FMT_FWHT;
        }
        return 0;
 }
@@ -688,13 +830,15 @@ static int vidioc_try_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
        struct v4l2_pix_format_mplane *pix_mp;
        struct v4l2_pix_format *pix;
        struct v4l2_plane_pix_format *plane;
-       const struct v4l2_fwht_pixfmt_info *info = &pixfmt_fwht;
+       const struct v4l2_fwht_pixfmt_info *info = ctx->is_stateless ?
+               &pixfmt_stateless_fwht : &pixfmt_fwht;
 
        switch (f->type) {
        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
                pix = &f->fmt.pix;
-               if (pix->pixelformat != V4L2_PIX_FMT_FWHT)
+               if (pix->pixelformat != V4L2_PIX_FMT_FWHT &&
+                   pix->pixelformat != V4L2_PIX_FMT_FWHT_STATELESS)
                        info = find_fmt(pix->pixelformat);
 
                pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH);
@@ -715,7 +859,8 @@ static int vidioc_try_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
        case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
                pix_mp = &f->fmt.pix_mp;
                plane = pix_mp->plane_fmt;
-               if (pix_mp->pixelformat != V4L2_PIX_FMT_FWHT)
+               if (pix_mp->pixelformat != V4L2_PIX_FMT_FWHT &&
+                   pix_mp->pixelformat != V4L2_PIX_FMT_FWHT_STATELESS)
                        info = find_fmt(pix_mp->pixelformat);
                pix_mp->num_planes = 1;
 
@@ -792,8 +937,12 @@ static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
                if (multiplanar)
                        return -EINVAL;
                pix = &f->fmt.pix;
-               pix->pixelformat = !ctx->is_enc ? V4L2_PIX_FMT_FWHT :
-                                  find_fmt(pix->pixelformat)->id;
+               if (ctx->is_enc)
+                       pix->pixelformat = find_fmt(pix->pixelformat)->id;
+               else if (ctx->is_stateless)
+                       pix->pixelformat = V4L2_PIX_FMT_FWHT_STATELESS;
+               else
+                       pix->pixelformat = V4L2_PIX_FMT_FWHT;
                if (!pix->colorspace)
                        pix->colorspace = V4L2_COLORSPACE_REC709;
                break;
@@ -801,8 +950,12 @@ static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
                if (!multiplanar)
                        return -EINVAL;
                pix_mp = &f->fmt.pix_mp;
-               pix_mp->pixelformat = !ctx->is_enc ? V4L2_PIX_FMT_FWHT :
-                                     find_fmt(pix_mp->pixelformat)->id;
+               if (ctx->is_enc)
+                       pix_mp->pixelformat = find_fmt(pix_mp->pixelformat)->id;
+               else if (ctx->is_stateless)
+                       pix_mp->pixelformat = V4L2_PIX_FMT_FWHT_STATELESS;
+               else
+                       pix_mp->pixelformat = V4L2_PIX_FMT_FWHT;
                if (!pix_mp->colorspace)
                        pix_mp->colorspace = V4L2_COLORSPACE_REC709;
                break;
@@ -845,6 +998,8 @@ static int vidioc_s_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
 
                if (pix->pixelformat == V4L2_PIX_FMT_FWHT)
                        q_data->info = &pixfmt_fwht;
+               else if (pix->pixelformat == V4L2_PIX_FMT_FWHT_STATELESS)
+                       q_data->info = &pixfmt_stateless_fwht;
                else
                        q_data->info = find_fmt(pix->pixelformat);
                q_data->coded_width = pix->width;
@@ -866,6 +1021,8 @@ static int vidioc_s_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
 
                if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT)
                        q_data->info = &pixfmt_fwht;
+               else if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT_STATELESS)
+                       q_data->info = &pixfmt_stateless_fwht;
                else
                        q_data->info = find_fmt(pix_mp->pixelformat);
                q_data->coded_width = pix_mp->width;
@@ -945,16 +1102,6 @@ static int vidioc_g_selection(struct file *file, void *priv,
 {
        struct vicodec_ctx *ctx = file2ctx(file);
        struct vicodec_q_data *q_data;
-       enum v4l2_buf_type valid_cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-       enum v4l2_buf_type valid_out_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
-
-       if (multiplanar) {
-               valid_cap_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-               valid_out_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-       }
-
-       if (s->type != valid_cap_type && s->type != valid_out_type)
-               return -EINVAL;
 
        q_data = get_q_data(ctx, s->type);
        if (!q_data)
@@ -963,18 +1110,14 @@ static int vidioc_g_selection(struct file *file, void *priv,
         * encoder supports only cropping on the OUTPUT buffer
         * decoder supports only composing on the CAPTURE buffer
         */
-       if ((ctx->is_enc && s->type == valid_out_type) ||
-           (!ctx->is_enc && s->type == valid_cap_type)) {
+       if (ctx->is_enc && s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
                switch (s->target) {
-               case V4L2_SEL_TGT_COMPOSE:
                case V4L2_SEL_TGT_CROP:
                        s->r.left = 0;
                        s->r.top = 0;
                        s->r.width = q_data->visible_width;
                        s->r.height = q_data->visible_height;
                        return 0;
-               case V4L2_SEL_TGT_COMPOSE_DEFAULT:
-               case V4L2_SEL_TGT_COMPOSE_BOUNDS:
                case V4L2_SEL_TGT_CROP_DEFAULT:
                case V4L2_SEL_TGT_CROP_BOUNDS:
                        s->r.left = 0;
@@ -983,6 +1126,22 @@ static int vidioc_g_selection(struct file *file, void *priv,
                        s->r.height = q_data->coded_height;
                        return 0;
                }
+       } else if (!ctx->is_enc && s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
+               switch (s->target) {
+               case V4L2_SEL_TGT_COMPOSE:
+                       s->r.left = 0;
+                       s->r.top = 0;
+                       s->r.width = q_data->visible_width;
+                       s->r.height = q_data->visible_height;
+                       return 0;
+               case V4L2_SEL_TGT_COMPOSE_DEFAULT:
+               case V4L2_SEL_TGT_COMPOSE_BOUNDS:
+                       s->r.left = 0;
+                       s->r.top = 0;
+                       s->r.width = q_data->coded_width;
+                       s->r.height = q_data->coded_height;
+                       return 0;
+               }
        }
        return -EINVAL;
 }
@@ -992,12 +1151,8 @@ static int vidioc_s_selection(struct file *file, void *priv,
 {
        struct vicodec_ctx *ctx = file2ctx(file);
        struct vicodec_q_data *q_data;
-       enum v4l2_buf_type out_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
-
-       if (multiplanar)
-               out_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
 
-       if (s->type != out_type)
+       if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
                return -EINVAL;
 
        q_data = get_q_data(ctx, s->type);
@@ -1092,6 +1247,8 @@ static int vicodec_enum_framesizes(struct file *file, void *fh,
                                   struct v4l2_frmsizeenum *fsize)
 {
        switch (fsize->pixel_format) {
+       case V4L2_PIX_FMT_FWHT_STATELESS:
+               break;
        case V4L2_PIX_FMT_FWHT:
                break;
        default:
@@ -1200,6 +1357,14 @@ static int vicodec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
        return 0;
 }
 
+static int vicodec_buf_out_validate(struct vb2_buffer *vb)
+{
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+
+       vbuf->field = V4L2_FIELD_NONE;
+       return 0;
+}
+
 static int vicodec_buf_prepare(struct vb2_buffer *vb)
 {
        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
@@ -1263,10 +1428,11 @@ static void vicodec_buf_queue(struct vb2_buffer *vb)
        }
 
        /*
-        * source change event is relevant only for the decoder
+        * source change event is relevant only for the stateful decoder
         * in the compressed stream
         */
-       if (ctx->is_enc || !V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
+       if (ctx->is_stateless || ctx->is_enc ||
+           !V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
                v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
                return;
        }
@@ -1314,12 +1480,33 @@ static void vicodec_return_bufs(struct vb2_queue *q, u32 state)
                        vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
                if (vbuf == NULL)
                        return;
+               v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
+                                          &ctx->hdl);
                spin_lock(ctx->lock);
                v4l2_m2m_buf_done(vbuf, state);
                spin_unlock(ctx->lock);
        }
 }
 
+static unsigned int total_frame_size(struct vicodec_q_data *q_data)
+{
+       unsigned int size;
+       unsigned int chroma_div;
+
+       if (!q_data->info) {
+               WARN_ON(1);
+               return 0;
+       }
+       size = q_data->coded_width * q_data->coded_height;
+       chroma_div = q_data->info->width_div * q_data->info->height_div;
+
+       if (q_data->info->components_num == 4)
+               return 2 * size + 2 * (size / chroma_div);
+       else if (q_data->info->components_num == 3)
+               return size + 2 * (size / chroma_div);
+       return size;
+}
+
 static int vicodec_start_streaming(struct vb2_queue *q,
                                   unsigned int count)
 {
@@ -1330,7 +1517,7 @@ static int vicodec_start_streaming(struct vb2_queue *q,
        unsigned int size = q_data->coded_width * q_data->coded_height;
        unsigned int chroma_div;
        unsigned int total_planes_size;
-       u8 *new_comp_frame;
+       u8 *new_comp_frame = NULL;
 
        if (!info)
                return -EINVAL;
@@ -1338,24 +1525,24 @@ static int vicodec_start_streaming(struct vb2_queue *q,
        chroma_div = info->width_div * info->height_div;
        q_data->sequence = 0;
 
-       ctx->last_src_buf = NULL;
-       ctx->last_dst_buf = NULL;
+       if (V4L2_TYPE_IS_OUTPUT(q->type))
+               ctx->last_src_buf = NULL;
+       else
+               ctx->last_dst_buf = NULL;
+
        state->gop_cnt = 0;
 
        if ((V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) ||
            (!V4L2_TYPE_IS_OUTPUT(q->type) && ctx->is_enc))
                return 0;
 
-       if (info->id == V4L2_PIX_FMT_FWHT) {
+       if (info->id == V4L2_PIX_FMT_FWHT ||
+           info->id == V4L2_PIX_FMT_FWHT_STATELESS) {
                vicodec_return_bufs(q, VB2_BUF_STATE_QUEUED);
                return -EINVAL;
        }
-       if (info->components_num == 4)
-               total_planes_size = 2 * size + 2 * (size / chroma_div);
-       else if (info->components_num == 3)
-               total_planes_size = size + 2 * (size / chroma_div);
-       else
-               total_planes_size = size;
+       total_planes_size = total_frame_size(q_data);
+       ctx->comp_max_size = total_planes_size;
 
        state->visible_width = q_data->visible_width;
        state->visible_height = q_data->visible_height;
@@ -1364,8 +1551,14 @@ static int vicodec_start_streaming(struct vb2_queue *q,
        state->stride = q_data->coded_width *
                                info->bytesperline_mult;
 
-       state->ref_frame.luma = kvmalloc(total_planes_size, GFP_KERNEL);
-       ctx->comp_max_size = total_planes_size;
+       if (ctx->is_stateless) {
+               state->ref_stride = state->stride;
+               return 0;
+       }
+       state->ref_stride = q_data->coded_width * info->luma_alpha_step;
+
+       state->ref_frame.buf = kvmalloc(total_planes_size, GFP_KERNEL);
+       state->ref_frame.luma = state->ref_frame.buf;
        new_comp_frame = kvmalloc(ctx->comp_max_size, GFP_KERNEL);
 
        if (!state->ref_frame.luma || !new_comp_frame) {
@@ -1413,7 +1606,10 @@ static void vicodec_stop_streaming(struct vb2_queue *q)
 
        if ((!V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) ||
            (V4L2_TYPE_IS_OUTPUT(q->type) && ctx->is_enc)) {
-               kvfree(ctx->state.ref_frame.luma);
+               if (!ctx->is_stateless)
+                       kvfree(ctx->state.ref_frame.buf);
+               ctx->state.ref_frame.buf = NULL;
+               ctx->state.ref_frame.luma = NULL;
                ctx->comp_max_size = 0;
                ctx->source_changed = false;
        }
@@ -1427,14 +1623,24 @@ static void vicodec_stop_streaming(struct vb2_queue *q)
        }
 }
 
+static void vicodec_buf_request_complete(struct vb2_buffer *vb)
+{
+       struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+
+       v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
+}
+
+
 static const struct vb2_ops vicodec_qops = {
-       .queue_setup     = vicodec_queue_setup,
-       .buf_prepare     = vicodec_buf_prepare,
-       .buf_queue       = vicodec_buf_queue,
-       .start_streaming = vicodec_start_streaming,
-       .stop_streaming  = vicodec_stop_streaming,
-       .wait_prepare    = vb2_ops_wait_prepare,
-       .wait_finish     = vb2_ops_wait_finish,
+       .queue_setup            = vicodec_queue_setup,
+       .buf_out_validate       = vicodec_buf_out_validate,
+       .buf_prepare            = vicodec_buf_prepare,
+       .buf_queue              = vicodec_buf_queue,
+       .buf_request_complete   = vicodec_buf_request_complete,
+       .start_streaming        = vicodec_start_streaming,
+       .stop_streaming         = vicodec_stop_streaming,
+       .wait_prepare           = vb2_ops_wait_prepare,
+       .wait_finish            = vb2_ops_wait_finish,
 };
 
 static int queue_init(void *priv, struct vb2_queue *src_vq,
@@ -1452,9 +1658,14 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
        src_vq->ops = &vicodec_qops;
        src_vq->mem_ops = &vb2_vmalloc_memops;
        src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
-       src_vq->lock = ctx->is_enc ? &ctx->dev->enc_mutex :
-               &ctx->dev->dec_mutex;
-
+       if (ctx->is_enc)
+               src_vq->lock = &ctx->dev->stateful_enc.mutex;
+       else if (ctx->is_stateless)
+               src_vq->lock = &ctx->dev->stateless_dec.mutex;
+       else
+               src_vq->lock = &ctx->dev->stateful_dec.mutex;
+       src_vq->supports_requests = ctx->is_stateless;
+       src_vq->requires_requests = ctx->is_stateless;
        ret = vb2_queue_init(src_vq);
        if (ret)
                return ret;
@@ -1473,53 +1684,86 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
        return vb2_queue_init(dst_vq);
 }
 
-#define VICODEC_CID_CUSTOM_BASE                (V4L2_CID_MPEG_BASE | 0xf000)
-#define VICODEC_CID_I_FRAME_QP         (VICODEC_CID_CUSTOM_BASE + 0)
-#define VICODEC_CID_P_FRAME_QP         (VICODEC_CID_CUSTOM_BASE + 1)
+static int vicodec_try_ctrl(struct v4l2_ctrl *ctrl)
+{
+       struct vicodec_ctx *ctx = container_of(ctrl->handler,
+                       struct vicodec_ctx, hdl);
+       const struct v4l2_ctrl_fwht_params *params;
+       struct vicodec_q_data *q_dst = get_q_data(ctx,
+                       V4L2_BUF_TYPE_VIDEO_CAPTURE);
+
+       switch (ctrl->id) {
+       case V4L2_CID_MPEG_VIDEO_FWHT_PARAMS:
+               if (!q_dst->info)
+                       return -EINVAL;
+               params = ctrl->p_new.p_fwht_params;
+               if (params->width > q_dst->coded_width ||
+                   params->width < MIN_WIDTH ||
+                   params->height > q_dst->coded_height ||
+                   params->height < MIN_HEIGHT)
+                       return -EINVAL;
+               if (!validate_by_version(params->flags, params->version))
+                       return -EINVAL;
+               if (!validate_stateless_params_flags(params, q_dst->info))
+                       return -EINVAL;
+               return 0;
+       default:
+               return 0;
+       }
+       return 0;
+}
+
+static void update_header_from_stateless_params(struct vicodec_ctx *ctx,
+                                               const struct v4l2_ctrl_fwht_params *params)
+{
+       struct fwht_cframe_hdr *p_hdr = &ctx->state.header;
+
+       p_hdr->magic1 = FWHT_MAGIC1;
+       p_hdr->magic2 = FWHT_MAGIC2;
+       p_hdr->version = htonl(params->version);
+       p_hdr->width = htonl(params->width);
+       p_hdr->height = htonl(params->height);
+       p_hdr->flags = htonl(params->flags);
+       p_hdr->colorspace = htonl(params->colorspace);
+       p_hdr->xfer_func = htonl(params->xfer_func);
+       p_hdr->ycbcr_enc = htonl(params->ycbcr_enc);
+       p_hdr->quantization = htonl(params->quantization);
+}
 
 static int vicodec_s_ctrl(struct v4l2_ctrl *ctrl)
 {
        struct vicodec_ctx *ctx = container_of(ctrl->handler,
                                               struct vicodec_ctx, hdl);
+       const struct v4l2_ctrl_fwht_params *params;
 
        switch (ctrl->id) {
        case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
                ctx->state.gop_size = ctrl->val;
                return 0;
-       case VICODEC_CID_I_FRAME_QP:
+       case V4L2_CID_FWHT_I_FRAME_QP:
                ctx->state.i_frame_qp = ctrl->val;
                return 0;
-       case VICODEC_CID_P_FRAME_QP:
+       case V4L2_CID_FWHT_P_FRAME_QP:
                ctx->state.p_frame_qp = ctrl->val;
                return 0;
+       case V4L2_CID_MPEG_VIDEO_FWHT_PARAMS:
+               params = ctrl->p_new.p_fwht_params;
+               update_header_from_stateless_params(ctx, params);
+               ctx->state.ref_frame_ts = params->backward_ref_ts;
+               return 0;
        }
        return -EINVAL;
 }
 
 static const struct v4l2_ctrl_ops vicodec_ctrl_ops = {
        .s_ctrl = vicodec_s_ctrl,
+       .try_ctrl = vicodec_try_ctrl,
 };
 
-static const struct v4l2_ctrl_config vicodec_ctrl_i_frame = {
-       .ops = &vicodec_ctrl_ops,
-       .id = VICODEC_CID_I_FRAME_QP,
-       .name = "FWHT I-Frame QP Value",
-       .type = V4L2_CTRL_TYPE_INTEGER,
-       .min = 1,
-       .max = 31,
-       .def = 20,
-       .step = 1,
-};
-
-static const struct v4l2_ctrl_config vicodec_ctrl_p_frame = {
-       .ops = &vicodec_ctrl_ops,
-       .id = VICODEC_CID_P_FRAME_QP,
-       .name = "FWHT P-Frame QP Value",
-       .type = V4L2_CTRL_TYPE_INTEGER,
-       .min = 1,
-       .max = 31,
-       .def = 20,
-       .step = 1,
+static const struct v4l2_ctrl_config vicodec_ctrl_stateless_state = {
+       .ops            = &vicodec_ctrl_ops,
+       .id             = V4L2_CID_MPEG_VIDEO_FWHT_PARAMS,
+       .elem_size      = sizeof(struct v4l2_ctrl_fwht_params),
 };
 
 /*
@@ -1542,8 +1786,10 @@ static int vicodec_open(struct file *file)
                goto open_unlock;
        }
 
-       if (vfd == &dev->enc_vfd)
+       if (vfd == &dev->stateful_enc.vfd)
                ctx->is_enc = true;
+       else if (vfd == &dev->stateless_dec.vfd)
+               ctx->is_stateless = true;
 
        v4l2_fh_init(&ctx->fh, video_devdata(file));
        file->private_data = &ctx->fh;
@@ -1552,8 +1798,12 @@ static int vicodec_open(struct file *file)
        v4l2_ctrl_handler_init(hdl, 4);
        v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
                          1, 16, 1, 10);
-       v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_i_frame, NULL);
-       v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_p_frame, NULL);
+       v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_FWHT_I_FRAME_QP,
+                         1, 31, 1, 20);
+       v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_FWHT_P_FRAME_QP,
+                         1, 31, 1, 20);
+       if (ctx->is_stateless)
+               v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_stateless_state, NULL);
        if (hdl->error) {
                rc = hdl->error;
                v4l2_ctrl_handler_free(hdl);
@@ -1563,15 +1813,19 @@ static int vicodec_open(struct file *file)
        ctx->fh.ctrl_handler = hdl;
        v4l2_ctrl_handler_setup(hdl);
 
-       ctx->q_data[V4L2_M2M_SRC].info =
-               ctx->is_enc ? v4l2_fwht_get_pixfmt(0) : &pixfmt_fwht;
+       if (ctx->is_enc)
+               ctx->q_data[V4L2_M2M_SRC].info = v4l2_fwht_get_pixfmt(0);
+       else if (ctx->is_stateless)
+               ctx->q_data[V4L2_M2M_SRC].info = &pixfmt_stateless_fwht;
+       else
+               ctx->q_data[V4L2_M2M_SRC].info = &pixfmt_fwht;
        ctx->q_data[V4L2_M2M_SRC].coded_width = 1280;
        ctx->q_data[V4L2_M2M_SRC].coded_height = 720;
        ctx->q_data[V4L2_M2M_SRC].visible_width = 1280;
        ctx->q_data[V4L2_M2M_SRC].visible_height = 720;
        size = 1280 * 720 * ctx->q_data[V4L2_M2M_SRC].info->sizeimage_mult /
                ctx->q_data[V4L2_M2M_SRC].info->sizeimage_div;
-       if (ctx->is_enc)
+       if (ctx->is_enc || ctx->is_stateless)
                ctx->q_data[V4L2_M2M_SRC].sizeimage = size;
        else
                ctx->q_data[V4L2_M2M_SRC].sizeimage =
@@ -1590,13 +1844,17 @@ static int vicodec_open(struct file *file)
        ctx->state.colorspace = V4L2_COLORSPACE_REC709;
 
        if (ctx->is_enc) {
-               ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->enc_dev, ctx,
-                                                   &queue_init);
-               ctx->lock = &dev->enc_lock;
+               ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_enc.m2m_dev,
+                                                   ctx, &queue_init);
+               ctx->lock = &dev->stateful_enc.lock;
+       } else if (ctx->is_stateless) {
+               ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateless_dec.m2m_dev,
+                                                   ctx, &queue_init);
+               ctx->lock = &dev->stateless_dec.lock;
        } else {
-               ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->dec_dev, ctx,
-                                                   &queue_init);
-               ctx->lock = &dev->dec_lock;
+               ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_dec.m2m_dev,
+                                                   ctx, &queue_init);
+               ctx->lock = &dev->stateful_dec.lock;
        }
 
        if (IS_ERR(ctx->fh.m2m_ctx)) {
@@ -1620,17 +1878,71 @@ static int vicodec_release(struct file *file)
        struct video_device *vfd = video_devdata(file);
        struct vicodec_ctx *ctx = file2ctx(file);
 
-       v4l2_fh_del(&ctx->fh);
-       v4l2_fh_exit(&ctx->fh);
-       v4l2_ctrl_handler_free(&ctx->hdl);
        mutex_lock(vfd->lock);
        v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
        mutex_unlock(vfd->lock);
+       v4l2_fh_del(&ctx->fh);
+       v4l2_fh_exit(&ctx->fh);
+       v4l2_ctrl_handler_free(&ctx->hdl);
+       kvfree(ctx->state.compressed_frame);
        kfree(ctx);
 
        return 0;
 }
 
+static int vicodec_request_validate(struct media_request *req)
+{
+       struct media_request_object *obj;
+       struct v4l2_ctrl_handler *parent_hdl, *hdl;
+       struct vicodec_ctx *ctx = NULL;
+       struct v4l2_ctrl *ctrl;
+       unsigned int count;
+
+       list_for_each_entry(obj, &req->objects, list) {
+               struct vb2_buffer *vb;
+
+               if (vb2_request_object_is_buffer(obj)) {
+                       vb = container_of(obj, struct vb2_buffer, req_obj);
+                       ctx = vb2_get_drv_priv(vb->vb2_queue);
+
+                       break;
+               }
+       }
+
+       if (!ctx) {
+               pr_err("No buffer was provided with the request\n");
+               return -ENOENT;
+       }
+
+       count = vb2_request_buffer_cnt(req);
+       if (!count) {
+               v4l2_info(&ctx->dev->v4l2_dev,
+                         "No buffer was provided with the request\n");
+               return -ENOENT;
+       } else if (count > 1) {
+               v4l2_info(&ctx->dev->v4l2_dev,
+                         "More than one buffer was provided with the request\n");
+               return -EINVAL;
+       }
+
+       parent_hdl = &ctx->hdl;
+
+       hdl = v4l2_ctrl_request_hdl_find(req, parent_hdl);
+       if (!hdl) {
+               v4l2_info(&ctx->dev->v4l2_dev, "Missing codec control\n");
+               return -ENOENT;
+       }
+       ctrl = v4l2_ctrl_request_hdl_ctrl_find(hdl,
+                                              vicodec_ctrl_stateless_state.id);
+       if (!ctrl) {
+               v4l2_info(&ctx->dev->v4l2_dev,
+                         "Missing required codec control\n");
+               return -ENOENT;
+       }
+
+       return vb2_request_validate(req);
+}
+
 static const struct v4l2_file_operations vicodec_fops = {
        .owner          = THIS_MODULE,
        .open           = vicodec_open,
@@ -1649,24 +1961,67 @@ static const struct video_device vicodec_videodev = {
        .release        = video_device_release_empty,
 };
 
+static const struct media_device_ops vicodec_m2m_media_ops = {
+       .req_validate   = vicodec_request_validate,
+       .req_queue      = v4l2_m2m_request_queue,
+};
+
 static const struct v4l2_m2m_ops m2m_ops = {
        .device_run     = device_run,
        .job_ready      = job_ready,
 };
 
+static int register_instance(struct vicodec_dev *dev,
+                            struct vicodec_dev_instance *dev_instance,
+                            const char *name, bool is_enc)
+{
+       struct video_device *vfd;
+       int ret;
+
+       spin_lock_init(&dev_instance->lock);
+       mutex_init(&dev_instance->mutex);
+       dev_instance->m2m_dev = v4l2_m2m_init(&m2m_ops);
+       if (IS_ERR(dev_instance->m2m_dev)) {
+               v4l2_err(&dev->v4l2_dev, "Failed to init vicodec enc device\n");
+               return PTR_ERR(dev_instance->m2m_dev);
+       }
+
+       dev_instance->vfd = vicodec_videodev;
+       vfd = &dev_instance->vfd;
+       vfd->lock = &dev_instance->mutex;
+       vfd->v4l2_dev = &dev->v4l2_dev;
+       strscpy(vfd->name, name, sizeof(vfd->name));
+       vfd->device_caps = V4L2_CAP_STREAMING |
+               (multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
+       if (is_enc) {
+               v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
+               v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
+       } else {
+               v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
+               v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
+       }
+       video_set_drvdata(vfd, dev);
+
+       ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
+       if (ret) {
+               v4l2_err(&dev->v4l2_dev, "Failed to register video device '%s'\n", name);
+               v4l2_m2m_release(dev_instance->m2m_dev);
+               return ret;
+       }
+       v4l2_info(&dev->v4l2_dev, "Device '%s' registered as /dev/video%d\n",
+                 name, vfd->num);
+       return 0;
+}
+
 static int vicodec_probe(struct platform_device *pdev)
 {
        struct vicodec_dev *dev;
-       struct video_device *vfd;
        int ret;
 
        dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
        if (!dev)
                return -ENOMEM;
 
-       spin_lock_init(&dev->enc_lock);
-       spin_lock_init(&dev->dec_lock);
-
        ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
        if (ret)
                return ret;
@@ -1677,103 +2032,74 @@ static int vicodec_probe(struct platform_device *pdev)
        strscpy(dev->mdev.bus_info, "platform:vicodec",
                sizeof(dev->mdev.bus_info));
        media_device_init(&dev->mdev);
+       dev->mdev.ops = &vicodec_m2m_media_ops;
        dev->v4l2_dev.mdev = &dev->mdev;
 #endif
 
-       mutex_init(&dev->enc_mutex);
-       mutex_init(&dev->dec_mutex);
-
        platform_set_drvdata(pdev, dev);
 
-       dev->enc_dev = v4l2_m2m_init(&m2m_ops);
-       if (IS_ERR(dev->enc_dev)) {
-               v4l2_err(&dev->v4l2_dev, "Failed to init vicodec device\n");
-               ret = PTR_ERR(dev->enc_dev);
+       if (register_instance(dev, &dev->stateful_enc,
+                             "stateful-encoder", true))
                goto unreg_dev;
-       }
 
-       dev->dec_dev = v4l2_m2m_init(&m2m_ops);
-       if (IS_ERR(dev->dec_dev)) {
-               v4l2_err(&dev->v4l2_dev, "Failed to init vicodec device\n");
-               ret = PTR_ERR(dev->dec_dev);
-               goto err_enc_m2m;
-       }
+       if (register_instance(dev, &dev->stateful_dec,
+                             "stateful-decoder", false))
+               goto unreg_sf_enc;
 
-       dev->enc_vfd = vicodec_videodev;
-       vfd = &dev->enc_vfd;
-       vfd->lock = &dev->enc_mutex;
-       vfd->v4l2_dev = &dev->v4l2_dev;
-       strscpy(vfd->name, "vicodec-enc", sizeof(vfd->name));
-       vfd->device_caps = V4L2_CAP_STREAMING |
-               (multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
-       v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
-       v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
-       video_set_drvdata(vfd, dev);
-
-       ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
-       if (ret) {
-               v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
-               goto err_dec_m2m;
-       }
-       v4l2_info(&dev->v4l2_dev,
-                       "Device registered as /dev/video%d\n", vfd->num);
+       if (register_instance(dev, &dev->stateless_dec,
+                             "stateless-decoder", false))
+               goto unreg_sf_dec;
 
-       dev->dec_vfd = vicodec_videodev;
-       vfd = &dev->dec_vfd;
-       vfd->lock = &dev->dec_mutex;
-       vfd->v4l2_dev = &dev->v4l2_dev;
-       vfd->device_caps = V4L2_CAP_STREAMING |
-               (multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
-       strscpy(vfd->name, "vicodec-dec", sizeof(vfd->name));
-       v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
-       v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
-       video_set_drvdata(vfd, dev);
-
-       ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
+#ifdef CONFIG_MEDIA_CONTROLLER
+       ret = v4l2_m2m_register_media_controller(dev->stateful_enc.m2m_dev,
+                                                &dev->stateful_enc.vfd,
+                                                MEDIA_ENT_F_PROC_VIDEO_ENCODER);
        if (ret) {
-               v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
-               goto unreg_enc;
+               v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for enc\n");
+               goto unreg_m2m;
        }
-       v4l2_info(&dev->v4l2_dev,
-                       "Device registered as /dev/video%d\n", vfd->num);
 
-#ifdef CONFIG_MEDIA_CONTROLLER
-       ret = v4l2_m2m_register_media_controller(dev->enc_dev,
-                       &dev->enc_vfd, MEDIA_ENT_F_PROC_VIDEO_ENCODER);
+       ret = v4l2_m2m_register_media_controller(dev->stateful_dec.m2m_dev,
+                                                &dev->stateful_dec.vfd,
+                                                MEDIA_ENT_F_PROC_VIDEO_DECODER);
        if (ret) {
-               v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
-               goto unreg_m2m;
+               v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for dec\n");
+               goto unreg_m2m_sf_enc_mc;
        }
 
-       ret = v4l2_m2m_register_media_controller(dev->dec_dev,
-                       &dev->dec_vfd, MEDIA_ENT_F_PROC_VIDEO_DECODER);
+       ret = v4l2_m2m_register_media_controller(dev->stateless_dec.m2m_dev,
+                                                &dev->stateless_dec.vfd,
+                                                MEDIA_ENT_F_PROC_VIDEO_DECODER);
        if (ret) {
-               v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
-               goto unreg_m2m_enc_mc;
+               v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for stateless dec\n");
+               goto unreg_m2m_sf_dec_mc;
        }
 
        ret = media_device_register(&dev->mdev);
        if (ret) {
                v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
-               goto unreg_m2m_dec_mc;
+               goto unreg_m2m_sl_dec_mc;
        }
 #endif
        return 0;
 
 #ifdef CONFIG_MEDIA_CONTROLLER
-unreg_m2m_dec_mc:
-       v4l2_m2m_unregister_media_controller(dev->dec_dev);
-unreg_m2m_enc_mc:
-       v4l2_m2m_unregister_media_controller(dev->enc_dev);
+unreg_m2m_sl_dec_mc:
+       v4l2_m2m_unregister_media_controller(dev->stateless_dec.m2m_dev);
+unreg_m2m_sf_dec_mc:
+       v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev);
+unreg_m2m_sf_enc_mc:
+       v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev);
 unreg_m2m:
-       video_unregister_device(&dev->dec_vfd);
+       video_unregister_device(&dev->stateless_dec.vfd);
+       v4l2_m2m_release(dev->stateless_dec.m2m_dev);
 #endif
-unreg_enc:
-       video_unregister_device(&dev->enc_vfd);
-err_dec_m2m:
-       v4l2_m2m_release(dev->dec_dev);
-err_enc_m2m:
-       v4l2_m2m_release(dev->enc_dev);
+unreg_sf_dec:
+       video_unregister_device(&dev->stateful_dec.vfd);
+       v4l2_m2m_release(dev->stateful_dec.m2m_dev);
+unreg_sf_enc:
+       video_unregister_device(&dev->stateful_enc.vfd);
+       v4l2_m2m_release(dev->stateful_enc.m2m_dev);
 unreg_dev:
        v4l2_device_unregister(&dev->v4l2_dev);
 
@@ -1788,15 +2114,17 @@ static int vicodec_remove(struct platform_device *pdev)
 
 #ifdef CONFIG_MEDIA_CONTROLLER
        media_device_unregister(&dev->mdev);
-       v4l2_m2m_unregister_media_controller(dev->enc_dev);
-       v4l2_m2m_unregister_media_controller(dev->dec_dev);
+       v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev);
+       v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev);
+       v4l2_m2m_unregister_media_controller(dev->stateless_dec.m2m_dev);
        media_device_cleanup(&dev->mdev);
 #endif
 
-       v4l2_m2m_release(dev->enc_dev);
-       v4l2_m2m_release(dev->dec_dev);
-       video_unregister_device(&dev->enc_vfd);
-       video_unregister_device(&dev->dec_vfd);
+       v4l2_m2m_release(dev->stateful_enc.m2m_dev);
+       v4l2_m2m_release(dev->stateful_dec.m2m_dev);
+       video_unregister_device(&dev->stateful_enc.vfd);
+       video_unregister_device(&dev->stateful_dec.vfd);
+       video_unregister_device(&dev->stateless_dec.vfd);
        v4l2_device_unregister(&dev->v4l2_dev);
 
        return 0;