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