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