linux/drivers/media/platform/mtk-vcodec/mtk_vcodec_dec.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2016 MediaTek Inc.
   4 * Author: PC Chen <pc.chen@mediatek.com>
   5 *         Tiffany Lin <tiffany.lin@mediatek.com>
   6 */
   7
   8#include <media/v4l2-event.h>
   9#include <media/v4l2-mem2mem.h>
  10#include <media/videobuf2-dma-contig.h>
  11
  12#include "mtk_vcodec_drv.h"
  13#include "mtk_vcodec_dec.h"
  14#include "mtk_vcodec_intr.h"
  15#include "mtk_vcodec_util.h"
  16#include "vdec_drv_if.h"
  17#include "mtk_vcodec_dec_pm.h"
  18
  19#define OUT_FMT_IDX     0
  20#define CAP_FMT_IDX     3
  21
  22#define MTK_VDEC_MIN_W  64U
  23#define MTK_VDEC_MIN_H  64U
  24#define DFT_CFG_WIDTH   MTK_VDEC_MIN_W
  25#define DFT_CFG_HEIGHT  MTK_VDEC_MIN_H
  26
  27static const struct mtk_video_fmt mtk_video_formats[] = {
  28        {
  29                .fourcc = V4L2_PIX_FMT_H264,
  30                .type = MTK_FMT_DEC,
  31                .num_planes = 1,
  32                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  33        },
  34        {
  35                .fourcc = V4L2_PIX_FMT_VP8,
  36                .type = MTK_FMT_DEC,
  37                .num_planes = 1,
  38                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  39        },
  40        {
  41                .fourcc = V4L2_PIX_FMT_VP9,
  42                .type = MTK_FMT_DEC,
  43                .num_planes = 1,
  44                .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
  45        },
  46        {
  47                .fourcc = V4L2_PIX_FMT_MT21C,
  48                .type = MTK_FMT_FRAME,
  49                .num_planes = 2,
  50        },
  51};
  52
  53static const struct mtk_codec_framesizes mtk_vdec_framesizes[] = {
  54        {
  55                .fourcc = V4L2_PIX_FMT_H264,
  56                .stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
  57                                MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
  58        },
  59        {
  60                .fourcc = V4L2_PIX_FMT_VP8,
  61                .stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
  62                                MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
  63        },
  64        {
  65                .fourcc = V4L2_PIX_FMT_VP9,
  66                .stepwise = {  MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
  67                                MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
  68        },
  69};
  70
  71#define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(mtk_vdec_framesizes)
  72#define NUM_FORMATS ARRAY_SIZE(mtk_video_formats)
  73
  74static const struct mtk_video_fmt *mtk_vdec_find_format(struct v4l2_format *f)
  75{
  76        const struct mtk_video_fmt *fmt;
  77        unsigned int k;
  78
  79        for (k = 0; k < NUM_FORMATS; k++) {
  80                fmt = &mtk_video_formats[k];
  81                if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
  82                        return fmt;
  83        }
  84
  85        return NULL;
  86}
  87
  88static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_ctx *ctx,
  89                                              enum v4l2_buf_type type)
  90{
  91        if (V4L2_TYPE_IS_OUTPUT(type))
  92                return &ctx->q_data[MTK_Q_DATA_SRC];
  93
  94        return &ctx->q_data[MTK_Q_DATA_DST];
  95}
  96
  97/*
  98 * This function tries to clean all display buffers, the buffers will return
  99 * in display order.
 100 * Note the buffers returned from codec driver may still be in driver's
 101 * reference list.
 102 */
 103static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_ctx *ctx)
 104{
 105        struct vdec_fb *disp_frame_buffer = NULL;
 106        struct mtk_video_dec_buf *dstbuf;
 107
 108        mtk_v4l2_debug(3, "[%d]", ctx->id);
 109        if (vdec_if_get_param(ctx,
 110                        GET_PARAM_DISP_FRAME_BUFFER,
 111                        &disp_frame_buffer)) {
 112                mtk_v4l2_err("[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
 113                        ctx->id);
 114                return NULL;
 115        }
 116
 117        if (disp_frame_buffer == NULL) {
 118                mtk_v4l2_debug(3, "No display frame buffer");
 119                return NULL;
 120        }
 121
 122        dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
 123                                frame_buffer);
 124        mutex_lock(&ctx->lock);
 125        if (dstbuf->used) {
 126                vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0,
 127                                        ctx->picinfo.fb_sz[0]);
 128                if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
 129                        vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1,
 130                                              ctx->picinfo.fb_sz[1]);
 131
 132                mtk_v4l2_debug(2,
 133                                "[%d]status=%x queue id=%d to done_list %d",
 134                                ctx->id, disp_frame_buffer->status,
 135                                dstbuf->vb.vb2_buf.index,
 136                                dstbuf->queued_in_vb2);
 137
 138                v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE);
 139                ctx->decoded_frame_cnt++;
 140        }
 141        mutex_unlock(&ctx->lock);
 142        return &dstbuf->vb.vb2_buf;
 143}
 144
 145/*
 146 * This function tries to clean all capture buffers that are not used as
 147 * reference buffers by codec driver any more
 148 * In this case, we need re-queue buffer to vb2 buffer if user space
 149 * already returns this buffer to v4l2 or this buffer is just the output of
 150 * previous sps/pps/resolution change decode, or do nothing if user
 151 * space still owns this buffer
 152 */
 153static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_ctx *ctx)
 154{
 155        struct mtk_video_dec_buf *dstbuf;
 156        struct vdec_fb *free_frame_buffer = NULL;
 157
 158        if (vdec_if_get_param(ctx,
 159                                GET_PARAM_FREE_FRAME_BUFFER,
 160                                &free_frame_buffer)) {
 161                mtk_v4l2_err("[%d] Error!! Cannot get param", ctx->id);
 162                return NULL;
 163        }
 164        if (free_frame_buffer == NULL) {
 165                mtk_v4l2_debug(3, " No free frame buffer");
 166                return NULL;
 167        }
 168
 169        mtk_v4l2_debug(3, "[%d] tmp_frame_addr = 0x%p",
 170                        ctx->id, free_frame_buffer);
 171
 172        dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
 173                                frame_buffer);
 174
 175        mutex_lock(&ctx->lock);
 176        if (dstbuf->used) {
 177                if ((dstbuf->queued_in_vb2) &&
 178                    (dstbuf->queued_in_v4l2) &&
 179                    (free_frame_buffer->status == FB_ST_FREE)) {
 180                        /*
 181                         * After decode sps/pps or non-display buffer, we don't
 182                         * need to return capture buffer to user space, but
 183                         * just re-queue this capture buffer to vb2 queue.
 184                         * This reduce overheads that dq/q unused capture
 185                         * buffer. In this case, queued_in_vb2 = true.
 186                         */
 187                        mtk_v4l2_debug(2,
 188                                "[%d]status=%x queue id=%d to rdy_queue %d",
 189                                ctx->id, free_frame_buffer->status,
 190                                dstbuf->vb.vb2_buf.index,
 191                                dstbuf->queued_in_vb2);
 192                        v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
 193                } else if ((dstbuf->queued_in_vb2 == false) &&
 194                           (dstbuf->queued_in_v4l2 == true)) {
 195                        /*
 196                         * If buffer in v4l2 driver but not in vb2 queue yet,
 197                         * and we get this buffer from free_list, it means
 198                         * that codec driver do not use this buffer as
 199                         * reference buffer anymore. We should q buffer to vb2
 200                         * queue, so later work thread could get this buffer
 201                         * for decode. In this case, queued_in_vb2 = false
 202                         * means this buffer is not from previous decode
 203                         * output.
 204                         */
 205                        mtk_v4l2_debug(2,
 206                                        "[%d]status=%x queue id=%d to rdy_queue",
 207                                        ctx->id, free_frame_buffer->status,
 208                                        dstbuf->vb.vb2_buf.index);
 209                        v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb);
 210                        dstbuf->queued_in_vb2 = true;
 211                } else {
 212                        /*
 213                         * Codec driver do not need to reference this capture
 214                         * buffer and this buffer is not in v4l2 driver.
 215                         * Then we don't need to do any thing, just add log when
 216                         * we need to debug buffer flow.
 217                         * When this buffer q from user space, it could
 218                         * directly q to vb2 buffer
 219                         */
 220                        mtk_v4l2_debug(3, "[%d]status=%x err queue id=%d %d %d",
 221                                        ctx->id, free_frame_buffer->status,
 222                                        dstbuf->vb.vb2_buf.index,
 223                                        dstbuf->queued_in_vb2,
 224                                        dstbuf->queued_in_v4l2);
 225                }
 226                dstbuf->used = false;
 227        }
 228        mutex_unlock(&ctx->lock);
 229        return &dstbuf->vb.vb2_buf;
 230}
 231
 232static void clean_display_buffer(struct mtk_vcodec_ctx *ctx)
 233{
 234        struct vb2_buffer *framptr;
 235
 236        do {
 237                framptr = get_display_buffer(ctx);
 238        } while (framptr);
 239}
 240
 241static void clean_free_buffer(struct mtk_vcodec_ctx *ctx)
 242{
 243        struct vb2_buffer *framptr;
 244
 245        do {
 246                framptr = get_free_buffer(ctx);
 247        } while (framptr);
 248}
 249
 250static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_ctx *ctx)
 251{
 252        static const struct v4l2_event ev_src_ch = {
 253                .type = V4L2_EVENT_SOURCE_CHANGE,
 254                .u.src_change.changes =
 255                V4L2_EVENT_SRC_CH_RESOLUTION,
 256        };
 257
 258        mtk_v4l2_debug(1, "[%d]", ctx->id);
 259        v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
 260}
 261
 262static void mtk_vdec_flush_decoder(struct mtk_vcodec_ctx *ctx)
 263{
 264        bool res_chg;
 265        int ret = 0;
 266
 267        ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
 268        if (ret)
 269                mtk_v4l2_err("DecodeFinal failed, ret=%d", ret);
 270
 271        clean_display_buffer(ctx);
 272        clean_free_buffer(ctx);
 273}
 274
 275static void mtk_vdec_update_fmt(struct mtk_vcodec_ctx *ctx,
 276                                unsigned int pixelformat)
 277{
 278        const struct mtk_video_fmt *fmt;
 279        struct mtk_q_data *dst_q_data;
 280        unsigned int k;
 281
 282        dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
 283        for (k = 0; k < NUM_FORMATS; k++) {
 284                fmt = &mtk_video_formats[k];
 285                if (fmt->fourcc == pixelformat) {
 286                        mtk_v4l2_debug(1, "Update cap fourcc(%d -> %d)",
 287                                dst_q_data->fmt->fourcc, pixelformat);
 288                        dst_q_data->fmt = fmt;
 289                        return;
 290                }
 291        }
 292
 293        mtk_v4l2_err("Cannot get fourcc(%d), using init value", pixelformat);
 294}
 295
 296static int mtk_vdec_pic_info_update(struct mtk_vcodec_ctx *ctx)
 297{
 298        unsigned int dpbsize = 0;
 299        int ret;
 300
 301        if (vdec_if_get_param(ctx,
 302                                GET_PARAM_PIC_INFO,
 303                                &ctx->last_decoded_picinfo)) {
 304                mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
 305                                ctx->id);
 306                return -EINVAL;
 307        }
 308
 309        if (ctx->last_decoded_picinfo.pic_w == 0 ||
 310                ctx->last_decoded_picinfo.pic_h == 0 ||
 311                ctx->last_decoded_picinfo.buf_w == 0 ||
 312                ctx->last_decoded_picinfo.buf_h == 0) {
 313                mtk_v4l2_err("Cannot get correct pic info");
 314                return -EINVAL;
 315        }
 316
 317        if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
 318                ctx->picinfo.cap_fourcc != 0)
 319                mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
 320
 321        if ((ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w) ||
 322            (ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h))
 323                return 0;
 324
 325        mtk_v4l2_debug(1,
 326                        "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
 327                        ctx->id, ctx->last_decoded_picinfo.pic_w,
 328                        ctx->last_decoded_picinfo.pic_h,
 329                        ctx->picinfo.pic_w, ctx->picinfo.pic_h,
 330                        ctx->last_decoded_picinfo.buf_w,
 331                        ctx->last_decoded_picinfo.buf_h);
 332
 333        ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
 334        if (dpbsize == 0)
 335                mtk_v4l2_err("Incorrect dpb size, ret=%d", ret);
 336
 337        ctx->dpb_size = dpbsize;
 338
 339        return ret;
 340}
 341
 342static void mtk_vdec_worker(struct work_struct *work)
 343{
 344        struct mtk_vcodec_ctx *ctx = container_of(work, struct mtk_vcodec_ctx,
 345                                decode_work);
 346        struct mtk_vcodec_dev *dev = ctx->dev;
 347        struct vb2_v4l2_buffer *src_buf, *dst_buf;
 348        struct mtk_vcodec_mem buf;
 349        struct vdec_fb *pfb;
 350        bool res_chg = false;
 351        int ret;
 352        struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
 353
 354        src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
 355        if (src_buf == NULL) {
 356                v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
 357                mtk_v4l2_debug(1, "[%d] src_buf empty!!", ctx->id);
 358                return;
 359        }
 360
 361        dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
 362        if (dst_buf == NULL) {
 363                v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
 364                mtk_v4l2_debug(1, "[%d] dst_buf empty!!", ctx->id);
 365                return;
 366        }
 367
 368        src_buf_info = container_of(src_buf, struct mtk_video_dec_buf, vb);
 369        dst_buf_info = container_of(dst_buf, struct mtk_video_dec_buf, vb);
 370
 371        pfb = &dst_buf_info->frame_buffer;
 372        pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
 373        pfb->base_y.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
 374        pfb->base_y.size = ctx->picinfo.fb_sz[0];
 375
 376        pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
 377        pfb->base_c.dma_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
 378        pfb->base_c.size = ctx->picinfo.fb_sz[1];
 379        pfb->status = 0;
 380        mtk_v4l2_debug(3, "===>[%d] vdec_if_decode() ===>", ctx->id);
 381
 382        mtk_v4l2_debug(3,
 383                        "id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
 384                        dst_buf->vb2_buf.index, pfb,
 385                        pfb->base_y.va, &pfb->base_y.dma_addr,
 386                        &pfb->base_c.dma_addr, pfb->base_y.size);
 387
 388        if (src_buf_info->lastframe) {
 389                mtk_v4l2_debug(1, "Got empty flush input buffer.");
 390                src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 391
 392                /* update dst buf status */
 393                dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
 394                mutex_lock(&ctx->lock);
 395                dst_buf_info->used = false;
 396                mutex_unlock(&ctx->lock);
 397
 398                vdec_if_decode(ctx, NULL, NULL, &res_chg);
 399                clean_display_buffer(ctx);
 400                vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 0, 0);
 401                if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
 402                        vb2_set_plane_payload(&dst_buf_info->vb.vb2_buf, 1, 0);
 403                dst_buf->flags |= V4L2_BUF_FLAG_LAST;
 404                v4l2_m2m_buf_done(&dst_buf_info->vb, VB2_BUF_STATE_DONE);
 405                clean_free_buffer(ctx);
 406                v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
 407                return;
 408        }
 409        buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
 410        buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
 411        buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
 412        if (!buf.va) {
 413                v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
 414                mtk_v4l2_err("[%d] id=%d src_addr is NULL!!",
 415                                ctx->id, src_buf->vb2_buf.index);
 416                return;
 417        }
 418        mtk_v4l2_debug(3, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
 419                        ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
 420        dst_buf_info->vb.vb2_buf.timestamp
 421                        = src_buf_info->vb.vb2_buf.timestamp;
 422        dst_buf_info->vb.timecode
 423                        = src_buf_info->vb.timecode;
 424        mutex_lock(&ctx->lock);
 425        dst_buf_info->used = true;
 426        mutex_unlock(&ctx->lock);
 427        src_buf_info->used = true;
 428
 429        ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
 430
 431        if (ret) {
 432                mtk_v4l2_err(
 433                        " <===[%d], src_buf[%d] sz=0x%zx pts=%llu dst_buf[%d] vdec_if_decode() ret=%d res_chg=%d===>",
 434                        ctx->id,
 435                        src_buf->vb2_buf.index,
 436                        buf.size,
 437                        src_buf_info->vb.vb2_buf.timestamp,
 438                        dst_buf->vb2_buf.index,
 439                        ret, res_chg);
 440                src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 441                if (ret == -EIO) {
 442                        mutex_lock(&ctx->lock);
 443                        src_buf_info->error = true;
 444                        mutex_unlock(&ctx->lock);
 445                }
 446                v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR);
 447        } else if (res_chg == false) {
 448                /*
 449                 * we only return src buffer with VB2_BUF_STATE_DONE
 450                 * when decode success without resolution change
 451                 */
 452                src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 453                v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE);
 454        }
 455
 456        dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
 457        clean_display_buffer(ctx);
 458        clean_free_buffer(ctx);
 459
 460        if (!ret && res_chg) {
 461                mtk_vdec_pic_info_update(ctx);
 462                /*
 463                 * On encountering a resolution change in the stream.
 464                 * The driver must first process and decode all
 465                 * remaining buffers from before the resolution change
 466                 * point, so call flush decode here
 467                 */
 468                mtk_vdec_flush_decoder(ctx);
 469                /*
 470                 * After all buffers containing decoded frames from
 471                 * before the resolution change point ready to be
 472                 * dequeued on the CAPTURE queue, the driver sends a
 473                 * V4L2_EVENT_SOURCE_CHANGE event for source change
 474                 * type V4L2_EVENT_SRC_CH_RESOLUTION
 475                 */
 476                mtk_vdec_queue_res_chg_event(ctx);
 477        }
 478        v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
 479}
 480
 481static int vidioc_try_decoder_cmd(struct file *file, void *priv,
 482                                struct v4l2_decoder_cmd *cmd)
 483{
 484        switch (cmd->cmd) {
 485        case V4L2_DEC_CMD_STOP:
 486        case V4L2_DEC_CMD_START:
 487                if (cmd->flags != 0) {
 488                        mtk_v4l2_err("cmd->flags=%u", cmd->flags);
 489                        return -EINVAL;
 490                }
 491                break;
 492        default:
 493                return -EINVAL;
 494        }
 495        return 0;
 496}
 497
 498
 499static int vidioc_decoder_cmd(struct file *file, void *priv,
 500                                struct v4l2_decoder_cmd *cmd)
 501{
 502        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 503        struct vb2_queue *src_vq, *dst_vq;
 504        int ret;
 505
 506        ret = vidioc_try_decoder_cmd(file, priv, cmd);
 507        if (ret)
 508                return ret;
 509
 510        mtk_v4l2_debug(1, "decoder cmd=%u", cmd->cmd);
 511        dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
 512                                V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 513        switch (cmd->cmd) {
 514        case V4L2_DEC_CMD_STOP:
 515                src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
 516                                V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
 517                if (!vb2_is_streaming(src_vq)) {
 518                        mtk_v4l2_debug(1, "Output stream is off. No need to flush.");
 519                        return 0;
 520                }
 521                if (!vb2_is_streaming(dst_vq)) {
 522                        mtk_v4l2_debug(1, "Capture stream is off. No need to flush.");
 523                        return 0;
 524                }
 525                v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf->vb);
 526                v4l2_m2m_try_schedule(ctx->m2m_ctx);
 527                break;
 528
 529        case V4L2_DEC_CMD_START:
 530                vb2_clear_last_buffer_dequeued(dst_vq);
 531                break;
 532
 533        default:
 534                return -EINVAL;
 535        }
 536
 537        return 0;
 538}
 539
 540void mtk_vdec_unlock(struct mtk_vcodec_ctx *ctx)
 541{
 542        mutex_unlock(&ctx->dev->dec_mutex);
 543}
 544
 545void mtk_vdec_lock(struct mtk_vcodec_ctx *ctx)
 546{
 547        mutex_lock(&ctx->dev->dec_mutex);
 548}
 549
 550void mtk_vcodec_dec_release(struct mtk_vcodec_ctx *ctx)
 551{
 552        vdec_if_deinit(ctx);
 553        ctx->state = MTK_STATE_FREE;
 554}
 555
 556void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_ctx *ctx)
 557{
 558        struct mtk_q_data *q_data;
 559
 560        ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
 561        ctx->fh.m2m_ctx = ctx->m2m_ctx;
 562        ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
 563        INIT_WORK(&ctx->decode_work, mtk_vdec_worker);
 564        ctx->colorspace = V4L2_COLORSPACE_REC709;
 565        ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
 566        ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
 567        ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
 568
 569        q_data = &ctx->q_data[MTK_Q_DATA_SRC];
 570        memset(q_data, 0, sizeof(struct mtk_q_data));
 571        q_data->visible_width = DFT_CFG_WIDTH;
 572        q_data->visible_height = DFT_CFG_HEIGHT;
 573        q_data->fmt = &mtk_video_formats[OUT_FMT_IDX];
 574        q_data->field = V4L2_FIELD_NONE;
 575
 576        q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
 577        q_data->bytesperline[0] = 0;
 578
 579        q_data = &ctx->q_data[MTK_Q_DATA_DST];
 580        memset(q_data, 0, sizeof(struct mtk_q_data));
 581        q_data->visible_width = DFT_CFG_WIDTH;
 582        q_data->visible_height = DFT_CFG_HEIGHT;
 583        q_data->coded_width = DFT_CFG_WIDTH;
 584        q_data->coded_height = DFT_CFG_HEIGHT;
 585        q_data->fmt = &mtk_video_formats[CAP_FMT_IDX];
 586        q_data->field = V4L2_FIELD_NONE;
 587
 588        v4l_bound_align_image(&q_data->coded_width,
 589                                MTK_VDEC_MIN_W,
 590                                MTK_VDEC_MAX_W, 4,
 591                                &q_data->coded_height,
 592                                MTK_VDEC_MIN_H,
 593                                MTK_VDEC_MAX_H, 5, 6);
 594
 595        q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
 596        q_data->bytesperline[0] = q_data->coded_width;
 597        q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
 598        q_data->bytesperline[1] = q_data->coded_width;
 599}
 600
 601static int vidioc_vdec_qbuf(struct file *file, void *priv,
 602                            struct v4l2_buffer *buf)
 603{
 604        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 605
 606        if (ctx->state == MTK_STATE_ABORT) {
 607                mtk_v4l2_err("[%d] Call on QBUF after unrecoverable error",
 608                                ctx->id);
 609                return -EIO;
 610        }
 611
 612        return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
 613}
 614
 615static int vidioc_vdec_dqbuf(struct file *file, void *priv,
 616                             struct v4l2_buffer *buf)
 617{
 618        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 619
 620        if (ctx->state == MTK_STATE_ABORT) {
 621                mtk_v4l2_err("[%d] Call on DQBUF after unrecoverable error",
 622                                ctx->id);
 623                return -EIO;
 624        }
 625
 626        return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
 627}
 628
 629static int vidioc_vdec_querycap(struct file *file, void *priv,
 630                                struct v4l2_capability *cap)
 631{
 632        strscpy(cap->driver, MTK_VCODEC_DEC_NAME, sizeof(cap->driver));
 633        strscpy(cap->bus_info, MTK_PLATFORM_STR, sizeof(cap->bus_info));
 634        strscpy(cap->card, MTK_PLATFORM_STR, sizeof(cap->card));
 635
 636        return 0;
 637}
 638
 639static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
 640                                     const struct v4l2_event_subscription *sub)
 641{
 642        switch (sub->type) {
 643        case V4L2_EVENT_EOS:
 644                return v4l2_event_subscribe(fh, sub, 2, NULL);
 645        case V4L2_EVENT_SOURCE_CHANGE:
 646                return v4l2_src_change_event_subscribe(fh, sub);
 647        default:
 648                return v4l2_ctrl_subscribe_event(fh, sub);
 649        }
 650}
 651
 652static int vidioc_try_fmt(struct v4l2_format *f,
 653                          const struct mtk_video_fmt *fmt)
 654{
 655        struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
 656        int i;
 657
 658        pix_fmt_mp->field = V4L2_FIELD_NONE;
 659
 660        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 661                pix_fmt_mp->num_planes = 1;
 662                pix_fmt_mp->plane_fmt[0].bytesperline = 0;
 663        } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 664                int tmp_w, tmp_h;
 665
 666                pix_fmt_mp->height = clamp(pix_fmt_mp->height,
 667                                        MTK_VDEC_MIN_H,
 668                                        MTK_VDEC_MAX_H);
 669                pix_fmt_mp->width = clamp(pix_fmt_mp->width,
 670                                        MTK_VDEC_MIN_W,
 671                                        MTK_VDEC_MAX_W);
 672
 673                /*
 674                 * Find next closer width align 64, heign align 64, size align
 675                 * 64 rectangle
 676                 * Note: This only get default value, the real HW needed value
 677                 *       only available when ctx in MTK_STATE_HEADER state
 678                 */
 679                tmp_w = pix_fmt_mp->width;
 680                tmp_h = pix_fmt_mp->height;
 681                v4l_bound_align_image(&pix_fmt_mp->width,
 682                                        MTK_VDEC_MIN_W,
 683                                        MTK_VDEC_MAX_W, 6,
 684                                        &pix_fmt_mp->height,
 685                                        MTK_VDEC_MIN_H,
 686                                        MTK_VDEC_MAX_H, 6, 9);
 687
 688                if (pix_fmt_mp->width < tmp_w &&
 689                        (pix_fmt_mp->width + 64) <= MTK_VDEC_MAX_W)
 690                        pix_fmt_mp->width += 64;
 691                if (pix_fmt_mp->height < tmp_h &&
 692                        (pix_fmt_mp->height + 64) <= MTK_VDEC_MAX_H)
 693                        pix_fmt_mp->height += 64;
 694
 695                mtk_v4l2_debug(0,
 696                        "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d",
 697                        tmp_w, tmp_h, pix_fmt_mp->width,
 698                        pix_fmt_mp->height,
 699                        pix_fmt_mp->width * pix_fmt_mp->height);
 700
 701                pix_fmt_mp->num_planes = fmt->num_planes;
 702                pix_fmt_mp->plane_fmt[0].sizeimage =
 703                                pix_fmt_mp->width * pix_fmt_mp->height;
 704                pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
 705
 706                if (pix_fmt_mp->num_planes == 2) {
 707                        pix_fmt_mp->plane_fmt[1].sizeimage =
 708                                (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
 709                        pix_fmt_mp->plane_fmt[1].bytesperline =
 710                                pix_fmt_mp->width;
 711                }
 712        }
 713
 714        for (i = 0; i < pix_fmt_mp->num_planes; i++)
 715                memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
 716                           sizeof(pix_fmt_mp->plane_fmt[0].reserved));
 717
 718        pix_fmt_mp->flags = 0;
 719        memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
 720        return 0;
 721}
 722
 723static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
 724                                struct v4l2_format *f)
 725{
 726        const struct mtk_video_fmt *fmt;
 727
 728        fmt = mtk_vdec_find_format(f);
 729        if (!fmt) {
 730                f->fmt.pix.pixelformat = mtk_video_formats[CAP_FMT_IDX].fourcc;
 731                fmt = mtk_vdec_find_format(f);
 732        }
 733
 734        return vidioc_try_fmt(f, fmt);
 735}
 736
 737static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
 738                                struct v4l2_format *f)
 739{
 740        struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
 741        const struct mtk_video_fmt *fmt;
 742
 743        fmt = mtk_vdec_find_format(f);
 744        if (!fmt) {
 745                f->fmt.pix.pixelformat = mtk_video_formats[OUT_FMT_IDX].fourcc;
 746                fmt = mtk_vdec_find_format(f);
 747        }
 748
 749        if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
 750                mtk_v4l2_err("sizeimage of output format must be given");
 751                return -EINVAL;
 752        }
 753
 754        return vidioc_try_fmt(f, fmt);
 755}
 756
 757static int vidioc_vdec_g_selection(struct file *file, void *priv,
 758                        struct v4l2_selection *s)
 759{
 760        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 761        struct mtk_q_data *q_data;
 762
 763        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 764                return -EINVAL;
 765
 766        q_data = &ctx->q_data[MTK_Q_DATA_DST];
 767
 768        switch (s->target) {
 769        case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 770                s->r.left = 0;
 771                s->r.top = 0;
 772                s->r.width = ctx->picinfo.pic_w;
 773                s->r.height = ctx->picinfo.pic_h;
 774                break;
 775        case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 776                s->r.left = 0;
 777                s->r.top = 0;
 778                s->r.width = ctx->picinfo.buf_w;
 779                s->r.height = ctx->picinfo.buf_h;
 780                break;
 781        case V4L2_SEL_TGT_COMPOSE:
 782                if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
 783                        /* set to default value if header info not ready yet*/
 784                        s->r.left = 0;
 785                        s->r.top = 0;
 786                        s->r.width = q_data->visible_width;
 787                        s->r.height = q_data->visible_height;
 788                }
 789                break;
 790        default:
 791                return -EINVAL;
 792        }
 793
 794        if (ctx->state < MTK_STATE_HEADER) {
 795                /* set to default value if header info not ready yet*/
 796                s->r.left = 0;
 797                s->r.top = 0;
 798                s->r.width = q_data->visible_width;
 799                s->r.height = q_data->visible_height;
 800                return 0;
 801        }
 802
 803        return 0;
 804}
 805
 806static int vidioc_vdec_s_selection(struct file *file, void *priv,
 807                                struct v4l2_selection *s)
 808{
 809        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 810
 811        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 812                return -EINVAL;
 813
 814        switch (s->target) {
 815        case V4L2_SEL_TGT_COMPOSE:
 816                s->r.left = 0;
 817                s->r.top = 0;
 818                s->r.width = ctx->picinfo.pic_w;
 819                s->r.height = ctx->picinfo.pic_h;
 820                break;
 821        default:
 822                return -EINVAL;
 823        }
 824
 825        return 0;
 826}
 827
 828static int vidioc_vdec_s_fmt(struct file *file, void *priv,
 829                             struct v4l2_format *f)
 830{
 831        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 832        struct v4l2_pix_format_mplane *pix_mp;
 833        struct mtk_q_data *q_data;
 834        int ret = 0;
 835        const struct mtk_video_fmt *fmt;
 836
 837        mtk_v4l2_debug(3, "[%d]", ctx->id);
 838
 839        q_data = mtk_vdec_get_q_data(ctx, f->type);
 840        if (!q_data)
 841                return -EINVAL;
 842
 843        pix_mp = &f->fmt.pix_mp;
 844        /*
 845         * Setting OUTPUT format after OUTPUT buffers are allocated is invalid
 846         * if using the stateful API.
 847         */
 848        if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
 849            vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
 850                mtk_v4l2_err("out_q_ctx buffers already requested");
 851                ret = -EBUSY;
 852        }
 853
 854        /*
 855         * Setting CAPTURE format after CAPTURE buffers are allocated is
 856         * invalid.
 857         */
 858        if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
 859            vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
 860                mtk_v4l2_err("cap_q_ctx buffers already requested");
 861                ret = -EBUSY;
 862        }
 863
 864        fmt = mtk_vdec_find_format(f);
 865        if (fmt == NULL) {
 866                if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 867                        f->fmt.pix.pixelformat =
 868                                mtk_video_formats[OUT_FMT_IDX].fourcc;
 869                        fmt = mtk_vdec_find_format(f);
 870                } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 871                        f->fmt.pix.pixelformat =
 872                                mtk_video_formats[CAP_FMT_IDX].fourcc;
 873                        fmt = mtk_vdec_find_format(f);
 874                }
 875        }
 876        if (fmt == NULL)
 877                return -EINVAL;
 878
 879        q_data->fmt = fmt;
 880        vidioc_try_fmt(f, q_data->fmt);
 881        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 882                q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
 883                q_data->coded_width = pix_mp->width;
 884                q_data->coded_height = pix_mp->height;
 885
 886                ctx->colorspace = pix_mp->colorspace;
 887                ctx->ycbcr_enc = pix_mp->ycbcr_enc;
 888                ctx->quantization = pix_mp->quantization;
 889                ctx->xfer_func = pix_mp->xfer_func;
 890
 891                if (ctx->state == MTK_STATE_FREE) {
 892                        ret = vdec_if_init(ctx, q_data->fmt->fourcc);
 893                        if (ret) {
 894                                mtk_v4l2_err("[%d]: vdec_if_init() fail ret=%d",
 895                                        ctx->id, ret);
 896                                return -EINVAL;
 897                        }
 898                        ctx->state = MTK_STATE_INIT;
 899                }
 900        }
 901
 902        return 0;
 903}
 904
 905static int vidioc_enum_framesizes(struct file *file, void *priv,
 906                                struct v4l2_frmsizeenum *fsize)
 907{
 908        int i = 0;
 909        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 910
 911        if (fsize->index != 0)
 912                return -EINVAL;
 913
 914        for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
 915                if (fsize->pixel_format != mtk_vdec_framesizes[i].fourcc)
 916                        continue;
 917
 918                fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
 919                fsize->stepwise = mtk_vdec_framesizes[i].stepwise;
 920                if (!(ctx->dev->dec_capability &
 921                                VCODEC_CAPABILITY_4K_DISABLED)) {
 922                        mtk_v4l2_debug(3, "4K is enabled");
 923                        fsize->stepwise.max_width =
 924                                        VCODEC_DEC_4K_CODED_WIDTH;
 925                        fsize->stepwise.max_height =
 926                                        VCODEC_DEC_4K_CODED_HEIGHT;
 927                }
 928                mtk_v4l2_debug(1, "%x, %d %d %d %d %d %d",
 929                                ctx->dev->dec_capability,
 930                                fsize->stepwise.min_width,
 931                                fsize->stepwise.max_width,
 932                                fsize->stepwise.step_width,
 933                                fsize->stepwise.min_height,
 934                                fsize->stepwise.max_height,
 935                                fsize->stepwise.step_height);
 936                return 0;
 937        }
 938
 939        return -EINVAL;
 940}
 941
 942static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
 943{
 944        const struct mtk_video_fmt *fmt;
 945        int i, j = 0;
 946
 947        for (i = 0; i < NUM_FORMATS; i++) {
 948                if (output_queue && (mtk_video_formats[i].type != MTK_FMT_DEC))
 949                        continue;
 950                if (!output_queue &&
 951                        (mtk_video_formats[i].type != MTK_FMT_FRAME))
 952                        continue;
 953
 954                if (j == f->index)
 955                        break;
 956                ++j;
 957        }
 958
 959        if (i == NUM_FORMATS)
 960                return -EINVAL;
 961
 962        fmt = &mtk_video_formats[i];
 963        f->pixelformat = fmt->fourcc;
 964        f->flags = fmt->flags;
 965
 966        return 0;
 967}
 968
 969static int vidioc_vdec_enum_fmt_vid_cap(struct file *file, void *priv,
 970                                        struct v4l2_fmtdesc *f)
 971{
 972        return vidioc_enum_fmt(f, false);
 973}
 974
 975static int vidioc_vdec_enum_fmt_vid_out(struct file *file, void *priv,
 976                                        struct v4l2_fmtdesc *f)
 977{
 978        return vidioc_enum_fmt(f, true);
 979}
 980
 981static int vidioc_vdec_g_fmt(struct file *file, void *priv,
 982                             struct v4l2_format *f)
 983{
 984        struct mtk_vcodec_ctx *ctx = fh_to_ctx(priv);
 985        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 986        struct vb2_queue *vq;
 987        struct mtk_q_data *q_data;
 988
 989        vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
 990        if (!vq) {
 991                mtk_v4l2_err("no vb2 queue for type=%d", f->type);
 992                return -EINVAL;
 993        }
 994
 995        q_data = mtk_vdec_get_q_data(ctx, f->type);
 996
 997        pix_mp->field = V4L2_FIELD_NONE;
 998        pix_mp->colorspace = ctx->colorspace;
 999        pix_mp->ycbcr_enc = ctx->ycbcr_enc;
1000        pix_mp->quantization = ctx->quantization;
1001        pix_mp->xfer_func = ctx->xfer_func;
1002
1003        if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
1004            (ctx->state >= MTK_STATE_HEADER)) {
1005                /* Until STREAMOFF is called on the CAPTURE queue
1006                 * (acknowledging the event), the driver operates as if
1007                 * the resolution hasn't changed yet.
1008                 * So we just return picinfo yet, and update picinfo in
1009                 * stop_streaming hook function
1010                 */
1011                q_data->sizeimage[0] = ctx->picinfo.fb_sz[0];
1012                q_data->sizeimage[1] = ctx->picinfo.fb_sz[1];
1013                q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
1014                q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
1015                q_data->coded_width = ctx->picinfo.buf_w;
1016                q_data->coded_height = ctx->picinfo.buf_h;
1017                ctx->last_decoded_picinfo.cap_fourcc = q_data->fmt->fourcc;
1018
1019                /*
1020                 * Width and height are set to the dimensions
1021                 * of the movie, the buffer is bigger and
1022                 * further processing stages should crop to this
1023                 * rectangle.
1024                 */
1025                pix_mp->width = q_data->coded_width;
1026                pix_mp->height = q_data->coded_height;
1027
1028                /*
1029                 * Set pixelformat to the format in which mt vcodec
1030                 * outputs the decoded frame
1031                 */
1032                pix_mp->num_planes = q_data->fmt->num_planes;
1033                pix_mp->pixelformat = q_data->fmt->fourcc;
1034                pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1035                pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1036                pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1037                pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1038
1039        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1040                /*
1041                 * This is run on OUTPUT
1042                 * The buffer contains compressed image
1043                 * so width and height have no meaning.
1044                 * Assign value here to pass v4l2-compliance test
1045                 */
1046                pix_mp->width = q_data->visible_width;
1047                pix_mp->height = q_data->visible_height;
1048                pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1049                pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1050                pix_mp->pixelformat = q_data->fmt->fourcc;
1051                pix_mp->num_planes = q_data->fmt->num_planes;
1052        } else {
1053                pix_mp->width = q_data->coded_width;
1054                pix_mp->height = q_data->coded_height;
1055                pix_mp->num_planes = q_data->fmt->num_planes;
1056                pix_mp->pixelformat = q_data->fmt->fourcc;
1057                pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1058                pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1059                pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1060                pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1061
1062                mtk_v4l2_debug(1, "[%d] type=%d state=%d Format information could not be read, not ready yet!",
1063                                ctx->id, f->type, ctx->state);
1064        }
1065
1066        return 0;
1067}
1068
1069static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1070                                unsigned int *nbuffers,
1071                                unsigned int *nplanes,
1072                                unsigned int sizes[],
1073                                struct device *alloc_devs[])
1074{
1075        struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1076        struct mtk_q_data *q_data;
1077        unsigned int i;
1078
1079        q_data = mtk_vdec_get_q_data(ctx, vq->type);
1080
1081        if (q_data == NULL) {
1082                mtk_v4l2_err("vq->type=%d err\n", vq->type);
1083                return -EINVAL;
1084        }
1085
1086        if (*nplanes) {
1087                for (i = 0; i < *nplanes; i++) {
1088                        if (sizes[i] < q_data->sizeimage[i])
1089                                return -EINVAL;
1090                }
1091        } else {
1092                if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1093                        *nplanes = 2;
1094                else
1095                        *nplanes = 1;
1096
1097                for (i = 0; i < *nplanes; i++)
1098                        sizes[i] = q_data->sizeimage[i];
1099        }
1100
1101        mtk_v4l2_debug(1,
1102                        "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
1103                        ctx->id, vq->type, *nplanes, *nbuffers,
1104                        sizes[0], sizes[1]);
1105
1106        return 0;
1107}
1108
1109static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
1110{
1111        struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1112        struct mtk_q_data *q_data;
1113        int i;
1114
1115        mtk_v4l2_debug(3, "[%d] (%d) id=%d",
1116                        ctx->id, vb->vb2_queue->type, vb->index);
1117
1118        q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
1119
1120        for (i = 0; i < q_data->fmt->num_planes; i++) {
1121                if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
1122                        mtk_v4l2_err("data will not fit into plane %d (%lu < %d)",
1123                                i, vb2_plane_size(vb, i),
1124                                q_data->sizeimage[i]);
1125                }
1126        }
1127
1128        return 0;
1129}
1130
1131static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
1132{
1133        struct vb2_v4l2_buffer *src_buf;
1134        struct mtk_vcodec_mem src_mem;
1135        bool res_chg = false;
1136        int ret = 0;
1137        unsigned int dpbsize = 1, i = 0;
1138        struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1139        struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
1140        struct mtk_video_dec_buf *buf = NULL;
1141        struct mtk_q_data *dst_q_data;
1142
1143        mtk_v4l2_debug(3, "[%d] (%d) id=%d, vb=%p",
1144                        ctx->id, vb->vb2_queue->type,
1145                        vb->index, vb);
1146        /*
1147         * check if this buffer is ready to be used after decode
1148         */
1149        if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1150                vb2_v4l2 = to_vb2_v4l2_buffer(vb);
1151                buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1152                mutex_lock(&ctx->lock);
1153                if (buf->used == false) {
1154                        v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
1155                        buf->queued_in_vb2 = true;
1156                        buf->queued_in_v4l2 = true;
1157                } else {
1158                        buf->queued_in_vb2 = false;
1159                        buf->queued_in_v4l2 = true;
1160                }
1161                mutex_unlock(&ctx->lock);
1162                return;
1163        }
1164
1165        v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
1166
1167        if (ctx->state != MTK_STATE_INIT) {
1168                mtk_v4l2_debug(3, "[%d] already init driver %d",
1169                                ctx->id, ctx->state);
1170                return;
1171        }
1172
1173        src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
1174        if (!src_buf) {
1175                mtk_v4l2_err("No src buffer");
1176                return;
1177        }
1178        buf = container_of(src_buf, struct mtk_video_dec_buf, vb);
1179        if (buf->lastframe) {
1180                /* This shouldn't happen. Just in case. */
1181                mtk_v4l2_err("Invalid flush buffer.");
1182                v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1183                return;
1184        }
1185
1186        src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
1187        src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
1188        src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
1189        mtk_v4l2_debug(2,
1190                        "[%d] buf id=%d va=%p dma=%pad size=%zx",
1191                        ctx->id, src_buf->vb2_buf.index,
1192                        src_mem.va, &src_mem.dma_addr,
1193                        src_mem.size);
1194
1195        ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
1196        if (ret || !res_chg) {
1197                /*
1198                 * fb == NULL means to parse SPS/PPS header or
1199                 * resolution info in src_mem. Decode can fail
1200                 * if there is no SPS header or picture info
1201                 * in bs
1202                 */
1203
1204                src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
1205                if (ret == -EIO) {
1206                        mtk_v4l2_err("[%d] Unrecoverable error in vdec_if_decode.",
1207                                        ctx->id);
1208                        ctx->state = MTK_STATE_ABORT;
1209                        v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
1210                } else {
1211                        v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
1212                }
1213                mtk_v4l2_debug(ret ? 0 : 1,
1214                               "[%d] vdec_if_decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
1215                               ctx->id, src_buf->vb2_buf.index,
1216                               src_mem.size, ret, res_chg);
1217                return;
1218        }
1219
1220        if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
1221                mtk_v4l2_err("[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
1222                                ctx->id);
1223                return;
1224        }
1225
1226        ctx->last_decoded_picinfo = ctx->picinfo;
1227        dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
1228        for (i = 0; i < dst_q_data->fmt->num_planes; i++) {
1229                dst_q_data->sizeimage[i] = ctx->picinfo.fb_sz[i];
1230                dst_q_data->bytesperline[i] = ctx->picinfo.buf_w;
1231        }
1232
1233        mtk_v4l2_debug(2, "[%d] vdec_if_init() OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
1234                        ctx->id,
1235                        ctx->picinfo.buf_w, ctx->picinfo.buf_h,
1236                        ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1237                        dst_q_data->sizeimage[0],
1238                        dst_q_data->sizeimage[1]);
1239
1240        ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
1241        if (dpbsize == 0)
1242                mtk_v4l2_err("[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
1243
1244        ctx->dpb_size = dpbsize;
1245        ctx->state = MTK_STATE_HEADER;
1246        mtk_v4l2_debug(1, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
1247
1248        mtk_vdec_queue_res_chg_event(ctx);
1249}
1250
1251static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
1252{
1253        struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1254        struct vb2_v4l2_buffer *vb2_v4l2;
1255        struct mtk_video_dec_buf *buf;
1256        bool buf_error;
1257
1258        vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
1259        buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, vb);
1260        mutex_lock(&ctx->lock);
1261        if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1262                buf->queued_in_v4l2 = false;
1263                buf->queued_in_vb2 = false;
1264        }
1265        buf_error = buf->error;
1266        mutex_unlock(&ctx->lock);
1267
1268        if (buf_error) {
1269                mtk_v4l2_err("Unrecoverable error on buffer.");
1270                ctx->state = MTK_STATE_ABORT;
1271        }
1272}
1273
1274static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
1275{
1276        struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
1277                                        struct vb2_v4l2_buffer, vb2_buf);
1278        struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
1279                                        struct mtk_video_dec_buf, vb);
1280
1281        if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1282                buf->used = false;
1283                buf->queued_in_v4l2 = false;
1284        } else {
1285                buf->lastframe = false;
1286        }
1287
1288        return 0;
1289}
1290
1291static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
1292{
1293        struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1294
1295        if (ctx->state == MTK_STATE_FLUSH)
1296                ctx->state = MTK_STATE_HEADER;
1297
1298        return 0;
1299}
1300
1301static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
1302{
1303        struct vb2_v4l2_buffer *src_buf = NULL, *dst_buf = NULL;
1304        struct mtk_vcodec_ctx *ctx = vb2_get_drv_priv(q);
1305
1306        mtk_v4l2_debug(3, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
1307                        ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
1308
1309        if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1310                while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
1311                        struct mtk_video_dec_buf *buf_info = container_of(
1312                                        src_buf, struct mtk_video_dec_buf, vb);
1313                        if (!buf_info->lastframe)
1314                                v4l2_m2m_buf_done(src_buf,
1315                                                VB2_BUF_STATE_ERROR);
1316                }
1317                return;
1318        }
1319
1320        if (ctx->state >= MTK_STATE_HEADER) {
1321
1322                /* Until STREAMOFF is called on the CAPTURE queue
1323                 * (acknowledging the event), the driver operates
1324                 * as if the resolution hasn't changed yet, i.e.
1325                 * VIDIOC_G_FMT< etc. return previous resolution.
1326                 * So we update picinfo here
1327                 */
1328                ctx->picinfo = ctx->last_decoded_picinfo;
1329
1330                mtk_v4l2_debug(2,
1331                                "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
1332                                ctx->id, ctx->last_decoded_picinfo.pic_w,
1333                                ctx->last_decoded_picinfo.pic_h,
1334                                ctx->picinfo.pic_w, ctx->picinfo.pic_h,
1335                                ctx->last_decoded_picinfo.buf_w,
1336                                ctx->last_decoded_picinfo.buf_h);
1337
1338                mtk_vdec_flush_decoder(ctx);
1339        }
1340        ctx->state = MTK_STATE_FLUSH;
1341
1342        while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
1343                vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
1344                if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
1345                        vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
1346                v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
1347        }
1348
1349}
1350
1351static void m2mops_vdec_device_run(void *priv)
1352{
1353        struct mtk_vcodec_ctx *ctx = priv;
1354        struct mtk_vcodec_dev *dev = ctx->dev;
1355
1356        queue_work(dev->decode_workqueue, &ctx->decode_work);
1357}
1358
1359static int m2mops_vdec_job_ready(void *m2m_priv)
1360{
1361        struct mtk_vcodec_ctx *ctx = m2m_priv;
1362
1363        mtk_v4l2_debug(3, "[%d]", ctx->id);
1364
1365        if (ctx->state == MTK_STATE_ABORT)
1366                return 0;
1367
1368        if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
1369            (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
1370                return 0;
1371
1372        if (ctx->state != MTK_STATE_HEADER)
1373                return 0;
1374
1375        return 1;
1376}
1377
1378static void m2mops_vdec_job_abort(void *priv)
1379{
1380        struct mtk_vcodec_ctx *ctx = priv;
1381
1382        ctx->state = MTK_STATE_ABORT;
1383}
1384
1385static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
1386{
1387        struct mtk_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
1388        int ret = 0;
1389
1390        switch (ctrl->id) {
1391        case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
1392                if (ctx->state >= MTK_STATE_HEADER) {
1393                        ctrl->val = ctx->dpb_size;
1394                } else {
1395                        mtk_v4l2_debug(0, "Seqinfo not ready");
1396                        ctrl->val = 0;
1397                }
1398                break;
1399        default:
1400                ret = -EINVAL;
1401        }
1402        return ret;
1403}
1404
1405static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
1406        .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
1407};
1408
1409int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_ctx *ctx)
1410{
1411        struct v4l2_ctrl *ctrl;
1412
1413        v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
1414
1415        ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
1416                                &mtk_vcodec_dec_ctrl_ops,
1417                                V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1418                                0, 32, 1, 1);
1419        ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1420        v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl,
1421                                &mtk_vcodec_dec_ctrl_ops,
1422                                V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
1423                                V4L2_MPEG_VIDEO_VP9_PROFILE_0,
1424                                0, V4L2_MPEG_VIDEO_VP9_PROFILE_0);
1425
1426        if (ctx->ctrl_hdl.error) {
1427                mtk_v4l2_err("Adding control failed %d",
1428                                ctx->ctrl_hdl.error);
1429                return ctx->ctrl_hdl.error;
1430        }
1431
1432        v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
1433        return 0;
1434}
1435
1436const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
1437        .device_run     = m2mops_vdec_device_run,
1438        .job_ready      = m2mops_vdec_job_ready,
1439        .job_abort      = m2mops_vdec_job_abort,
1440};
1441
1442static const struct vb2_ops mtk_vdec_vb2_ops = {
1443        .queue_setup    = vb2ops_vdec_queue_setup,
1444        .buf_prepare    = vb2ops_vdec_buf_prepare,
1445        .buf_queue      = vb2ops_vdec_buf_queue,
1446        .wait_prepare   = vb2_ops_wait_prepare,
1447        .wait_finish    = vb2_ops_wait_finish,
1448        .buf_init       = vb2ops_vdec_buf_init,
1449        .buf_finish     = vb2ops_vdec_buf_finish,
1450        .start_streaming        = vb2ops_vdec_start_streaming,
1451        .stop_streaming = vb2ops_vdec_stop_streaming,
1452};
1453
1454const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
1455        .vidioc_streamon        = v4l2_m2m_ioctl_streamon,
1456        .vidioc_streamoff       = v4l2_m2m_ioctl_streamoff,
1457        .vidioc_reqbufs         = v4l2_m2m_ioctl_reqbufs,
1458        .vidioc_querybuf        = v4l2_m2m_ioctl_querybuf,
1459        .vidioc_expbuf          = v4l2_m2m_ioctl_expbuf,
1460
1461        .vidioc_qbuf            = vidioc_vdec_qbuf,
1462        .vidioc_dqbuf           = vidioc_vdec_dqbuf,
1463
1464        .vidioc_try_fmt_vid_cap_mplane  = vidioc_try_fmt_vid_cap_mplane,
1465        .vidioc_try_fmt_vid_out_mplane  = vidioc_try_fmt_vid_out_mplane,
1466
1467        .vidioc_s_fmt_vid_cap_mplane    = vidioc_vdec_s_fmt,
1468        .vidioc_s_fmt_vid_out_mplane    = vidioc_vdec_s_fmt,
1469        .vidioc_g_fmt_vid_cap_mplane    = vidioc_vdec_g_fmt,
1470        .vidioc_g_fmt_vid_out_mplane    = vidioc_vdec_g_fmt,
1471
1472        .vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
1473
1474        .vidioc_enum_fmt_vid_cap        = vidioc_vdec_enum_fmt_vid_cap,
1475        .vidioc_enum_fmt_vid_out        = vidioc_vdec_enum_fmt_vid_out,
1476        .vidioc_enum_framesizes = vidioc_enum_framesizes,
1477
1478        .vidioc_querycap                = vidioc_vdec_querycap,
1479        .vidioc_subscribe_event         = vidioc_vdec_subscribe_evt,
1480        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
1481        .vidioc_g_selection             = vidioc_vdec_g_selection,
1482        .vidioc_s_selection             = vidioc_vdec_s_selection,
1483
1484        .vidioc_decoder_cmd = vidioc_decoder_cmd,
1485        .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
1486};
1487
1488int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
1489                           struct vb2_queue *dst_vq)
1490{
1491        struct mtk_vcodec_ctx *ctx = priv;
1492        int ret = 0;
1493
1494        mtk_v4l2_debug(3, "[%d]", ctx->id);
1495
1496        src_vq->type            = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1497        src_vq->io_modes        = VB2_DMABUF | VB2_MMAP;
1498        src_vq->drv_priv        = ctx;
1499        src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1500        src_vq->ops             = &mtk_vdec_vb2_ops;
1501        src_vq->mem_ops         = &vb2_dma_contig_memops;
1502        src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1503        src_vq->lock            = &ctx->dev->dev_mutex;
1504        src_vq->dev             = &ctx->dev->plat_dev->dev;
1505
1506        ret = vb2_queue_init(src_vq);
1507        if (ret) {
1508                mtk_v4l2_err("Failed to initialize videobuf2 queue(output)");
1509                return ret;
1510        }
1511        dst_vq->type            = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1512        dst_vq->io_modes        = VB2_DMABUF | VB2_MMAP;
1513        dst_vq->drv_priv        = ctx;
1514        dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1515        dst_vq->ops             = &mtk_vdec_vb2_ops;
1516        dst_vq->mem_ops         = &vb2_dma_contig_memops;
1517        dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1518        dst_vq->lock            = &ctx->dev->dev_mutex;
1519        dst_vq->dev             = &ctx->dev->plat_dev->dev;
1520
1521        ret = vb2_queue_init(dst_vq);
1522        if (ret) {
1523                vb2_queue_release(src_vq);
1524                mtk_v4l2_err("Failed to initialize videobuf2 queue(capture)");
1525        }
1526
1527        return ret;
1528}
1529