at master 30 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_dec_drv.h" 13#include "mtk_vcodec_dec.h" 14#include "vdec_drv_if.h" 15#include "mtk_vcodec_dec_pm.h" 16 17#define DFT_CFG_WIDTH MTK_VDEC_MIN_W 18#define DFT_CFG_HEIGHT MTK_VDEC_MIN_H 19 20static const struct mtk_video_fmt * 21mtk_vdec_find_format(struct v4l2_format *f, 22 const struct mtk_vcodec_dec_pdata *dec_pdata) 23{ 24 const struct mtk_video_fmt *fmt; 25 unsigned int k; 26 27 for (k = 0; k < *dec_pdata->num_formats; k++) { 28 fmt = &dec_pdata->vdec_formats[k]; 29 if (fmt->fourcc == f->fmt.pix_mp.pixelformat) 30 return fmt; 31 } 32 33 return NULL; 34} 35 36static bool mtk_vdec_get_cap_fmt(struct mtk_vcodec_dec_ctx *ctx, int format_index) 37{ 38 const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 39 const struct mtk_video_fmt *fmt; 40 struct mtk_q_data *q_data; 41 int num_frame_count = 0, i; 42 bool ret = false; 43 44 fmt = &dec_pdata->vdec_formats[format_index]; 45 for (i = 0; i < *dec_pdata->num_formats; i++) { 46 if (dec_pdata->vdec_formats[i].type != MTK_FMT_FRAME) 47 continue; 48 49 num_frame_count++; 50 } 51 52 if (num_frame_count == 1 || (!ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MM21)) 53 return true; 54 55 q_data = &ctx->q_data[MTK_Q_DATA_SRC]; 56 switch (q_data->fmt->fourcc) { 57 case V4L2_PIX_FMT_H264_SLICE: 58 if (ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MT2110R) 59 ret = true; 60 break; 61 case V4L2_PIX_FMT_VP9_FRAME: 62 case V4L2_PIX_FMT_AV1_FRAME: 63 case V4L2_PIX_FMT_HEVC_SLICE: 64 if (ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MT2110T) 65 ret = true; 66 break; 67 default: 68 break; 69 } 70 71 return ret; 72} 73 74static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_dec_ctx *ctx, 75 enum v4l2_buf_type type) 76{ 77 if (V4L2_TYPE_IS_OUTPUT(type)) 78 return &ctx->q_data[MTK_Q_DATA_SRC]; 79 80 return &ctx->q_data[MTK_Q_DATA_DST]; 81} 82 83static int stateful_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) 84{ 85 return v4l2_m2m_ioctl_try_decoder_cmd(file, priv, cmd); 86} 87 88static int stateful_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) 89{ 90 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 91 struct vb2_queue *src_vq, *dst_vq; 92 int ret; 93 94 ret = stateful_try_decoder_cmd(file, priv, cmd); 95 if (ret) 96 return ret; 97 98 mtk_v4l2_vdec_dbg(1, ctx, "decoder cmd=%u", cmd->cmd); 99 dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, 100 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 101 switch (cmd->cmd) { 102 case V4L2_DEC_CMD_STOP: 103 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, 104 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 105 if (!vb2_is_streaming(src_vq)) { 106 mtk_v4l2_vdec_dbg(1, ctx, "Output stream is off. No need to flush."); 107 return 0; 108 } 109 if (!vb2_is_streaming(dst_vq)) { 110 mtk_v4l2_vdec_dbg(1, ctx, "Capture stream is off. No need to flush."); 111 return 0; 112 } 113 v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb); 114 v4l2_m2m_try_schedule(ctx->m2m_ctx); 115 break; 116 117 case V4L2_DEC_CMD_START: 118 vb2_clear_last_buffer_dequeued(dst_vq); 119 break; 120 121 default: 122 return -EINVAL; 123 } 124 125 return 0; 126} 127 128static int stateless_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) 129{ 130 return v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, cmd); 131} 132 133static int stateless_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) 134{ 135 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 136 int ret; 137 138 ret = v4l2_m2m_ioctl_stateless_try_decoder_cmd(file, priv, cmd); 139 if (ret) 140 return ret; 141 142 mtk_v4l2_vdec_dbg(3, ctx, "decoder cmd=%u", cmd->cmd); 143 switch (cmd->cmd) { 144 case V4L2_DEC_CMD_FLUSH: 145 /* 146 * If the flag of the output buffer is equals V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF, 147 * this command will prevent dequeueing the capture buffer containing the last 148 * decoded frame. Or do nothing 149 */ 150 break; 151 default: 152 mtk_v4l2_vdec_err(ctx, "invalid stateless decoder cmd=%u", cmd->cmd); 153 return -EINVAL; 154 } 155 156 return 0; 157} 158 159static int vidioc_try_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) 160{ 161 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 162 163 if (ctx->dev->vdec_pdata->uses_stateless_api) 164 return stateless_try_decoder_cmd(file, priv, cmd); 165 166 return stateful_try_decoder_cmd(file, priv, cmd); 167} 168 169static int vidioc_decoder_cmd(struct file *file, void *priv, struct v4l2_decoder_cmd *cmd) 170{ 171 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 172 173 if (ctx->dev->vdec_pdata->uses_stateless_api) 174 return stateless_decoder_cmd(file, priv, cmd); 175 176 return stateful_decoder_cmd(file, priv, cmd); 177} 178 179void mtk_vdec_unlock(struct mtk_vcodec_dec_ctx *ctx) 180{ 181 mutex_unlock(&ctx->dev->dec_mutex[ctx->hw_id]); 182} 183 184void mtk_vdec_lock(struct mtk_vcodec_dec_ctx *ctx) 185{ 186 mutex_lock(&ctx->dev->dec_mutex[ctx->hw_id]); 187} 188 189void mtk_vcodec_dec_release(struct mtk_vcodec_dec_ctx *ctx) 190{ 191 vdec_if_deinit(ctx); 192 ctx->state = MTK_STATE_FREE; 193} 194 195void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_dec_ctx *ctx) 196{ 197 struct mtk_q_data *q_data; 198 199 ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex; 200 ctx->fh.m2m_ctx = ctx->m2m_ctx; 201 ctx->fh.ctrl_handler = &ctx->ctrl_hdl; 202 INIT_WORK(&ctx->decode_work, ctx->dev->vdec_pdata->worker); 203 ctx->colorspace = V4L2_COLORSPACE_REC709; 204 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 205 ctx->quantization = V4L2_QUANTIZATION_DEFAULT; 206 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; 207 208 q_data = &ctx->q_data[MTK_Q_DATA_SRC]; 209 memset(q_data, 0, sizeof(struct mtk_q_data)); 210 q_data->visible_width = DFT_CFG_WIDTH; 211 q_data->visible_height = DFT_CFG_HEIGHT; 212 q_data->fmt = ctx->dev->vdec_pdata->default_out_fmt; 213 q_data->field = V4L2_FIELD_NONE; 214 215 q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT; 216 q_data->bytesperline[0] = 0; 217 218 q_data = &ctx->q_data[MTK_Q_DATA_DST]; 219 memset(q_data, 0, sizeof(struct mtk_q_data)); 220 q_data->visible_width = DFT_CFG_WIDTH; 221 q_data->visible_height = DFT_CFG_HEIGHT; 222 q_data->coded_width = DFT_CFG_WIDTH; 223 q_data->coded_height = DFT_CFG_HEIGHT; 224 q_data->fmt = ctx->dev->vdec_pdata->default_cap_fmt; 225 q_data->field = V4L2_FIELD_NONE; 226 227 q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height; 228 q_data->bytesperline[0] = q_data->coded_width; 229 q_data->sizeimage[1] = q_data->sizeimage[0] / 2; 230 q_data->bytesperline[1] = q_data->coded_width; 231} 232 233static int vidioc_vdec_qbuf(struct file *file, void *priv, 234 struct v4l2_buffer *buf) 235{ 236 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 237 238 if (ctx->state == MTK_STATE_ABORT) { 239 mtk_v4l2_vdec_err(ctx, "[%d] Call on QBUF after unrecoverable error", ctx->id); 240 return -EIO; 241 } 242 243 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); 244} 245 246static int vidioc_vdec_dqbuf(struct file *file, void *priv, 247 struct v4l2_buffer *buf) 248{ 249 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 250 251 if (ctx->state == MTK_STATE_ABORT) { 252 mtk_v4l2_vdec_err(ctx, "[%d] Call on DQBUF after unrecoverable error", ctx->id); 253 return -EIO; 254 } 255 256 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); 257} 258 259static int vidioc_vdec_querycap(struct file *file, void *priv, 260 struct v4l2_capability *cap) 261{ 262 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 263 struct device *dev = &ctx->dev->plat_dev->dev; 264 265 strscpy(cap->driver, dev->driver->name, sizeof(cap->driver)); 266 snprintf(cap->card, sizeof(cap->card), "MT%d video decoder", ctx->dev->chip_name); 267 268 return 0; 269} 270 271static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh, 272 const struct v4l2_event_subscription *sub) 273{ 274 struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(fh); 275 276 if (ctx->dev->vdec_pdata->uses_stateless_api) 277 return v4l2_ctrl_subscribe_event(fh, sub); 278 279 switch (sub->type) { 280 case V4L2_EVENT_EOS: 281 return v4l2_event_subscribe(fh, sub, 2, NULL); 282 case V4L2_EVENT_SOURCE_CHANGE: 283 return v4l2_src_change_event_subscribe(fh, sub); 284 default: 285 return v4l2_ctrl_subscribe_event(fh, sub); 286 } 287} 288 289static int vidioc_try_fmt(struct mtk_vcodec_dec_ctx *ctx, struct v4l2_format *f, 290 const struct mtk_video_fmt *fmt) 291{ 292 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; 293 const struct v4l2_frmsize_stepwise *frmsize; 294 295 pix_fmt_mp->field = V4L2_FIELD_NONE; 296 297 /* Always apply frame size constraints from the coded side */ 298 if (V4L2_TYPE_IS_OUTPUT(f->type)) 299 frmsize = &fmt->frmsize; 300 else 301 frmsize = &ctx->q_data[MTK_Q_DATA_SRC].fmt->frmsize; 302 303 pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VDEC_MIN_W, frmsize->max_width); 304 pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VDEC_MIN_H, frmsize->max_height); 305 306 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 307 pix_fmt_mp->num_planes = 1; 308 pix_fmt_mp->plane_fmt[0].bytesperline = 0; 309 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 310 int tmp_w, tmp_h; 311 312 /* 313 * Find next closer width align 64, height align 64, size align 314 * 64 rectangle 315 * Note: This only get default value, the real HW needed value 316 * only available when ctx in MTK_STATE_HEADER state 317 */ 318 tmp_w = pix_fmt_mp->width; 319 tmp_h = pix_fmt_mp->height; 320 v4l_bound_align_image(&pix_fmt_mp->width, MTK_VDEC_MIN_W, frmsize->max_width, 6, 321 &pix_fmt_mp->height, MTK_VDEC_MIN_H, frmsize->max_height, 6, 322 9); 323 324 if (pix_fmt_mp->width < tmp_w && 325 (pix_fmt_mp->width + 64) <= frmsize->max_width) 326 pix_fmt_mp->width += 64; 327 if (pix_fmt_mp->height < tmp_h && 328 (pix_fmt_mp->height + 64) <= frmsize->max_height) 329 pix_fmt_mp->height += 64; 330 331 mtk_v4l2_vdec_dbg(0, ctx, 332 "before resize wxh=%dx%d, after resize wxh=%dx%d, sizeimage=%d", 333 tmp_w, tmp_h, pix_fmt_mp->width, pix_fmt_mp->height, 334 pix_fmt_mp->width * pix_fmt_mp->height); 335 336 pix_fmt_mp->num_planes = fmt->num_planes; 337 pix_fmt_mp->plane_fmt[0].sizeimage = 338 pix_fmt_mp->width * pix_fmt_mp->height; 339 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width; 340 341 if (pix_fmt_mp->num_planes == 2) { 342 pix_fmt_mp->plane_fmt[1].sizeimage = 343 (pix_fmt_mp->width * pix_fmt_mp->height) / 2; 344 pix_fmt_mp->plane_fmt[1].bytesperline = 345 pix_fmt_mp->width; 346 } 347 } 348 349 pix_fmt_mp->flags = 0; 350 return 0; 351} 352 353static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv, 354 struct v4l2_format *f) 355{ 356 const struct mtk_video_fmt *fmt; 357 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 358 const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 359 360 fmt = mtk_vdec_find_format(f, dec_pdata); 361 if (!fmt) { 362 f->fmt.pix.pixelformat = 363 ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc; 364 fmt = mtk_vdec_find_format(f, dec_pdata); 365 } 366 367 return vidioc_try_fmt(ctx, f, fmt); 368} 369 370static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv, 371 struct v4l2_format *f) 372{ 373 struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; 374 const struct mtk_video_fmt *fmt; 375 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 376 const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 377 378 fmt = mtk_vdec_find_format(f, dec_pdata); 379 if (!fmt) { 380 f->fmt.pix.pixelformat = 381 ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc; 382 fmt = mtk_vdec_find_format(f, dec_pdata); 383 } 384 385 if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) { 386 mtk_v4l2_vdec_err(ctx, "sizeimage of output format must be given"); 387 return -EINVAL; 388 } 389 390 return vidioc_try_fmt(ctx, f, fmt); 391} 392 393static int vidioc_vdec_g_selection(struct file *file, void *priv, 394 struct v4l2_selection *s) 395{ 396 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 397 struct mtk_q_data *q_data; 398 399 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 400 return -EINVAL; 401 402 q_data = &ctx->q_data[MTK_Q_DATA_DST]; 403 404 switch (s->target) { 405 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 406 s->r.left = 0; 407 s->r.top = 0; 408 s->r.width = ctx->picinfo.pic_w; 409 s->r.height = ctx->picinfo.pic_h; 410 break; 411 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 412 s->r.left = 0; 413 s->r.top = 0; 414 s->r.width = ctx->picinfo.buf_w; 415 s->r.height = ctx->picinfo.buf_h; 416 break; 417 case V4L2_SEL_TGT_COMPOSE: 418 if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) { 419 /* set to default value if header info not ready yet*/ 420 s->r.left = 0; 421 s->r.top = 0; 422 s->r.width = q_data->visible_width; 423 s->r.height = q_data->visible_height; 424 } 425 break; 426 default: 427 return -EINVAL; 428 } 429 430 if (ctx->state < MTK_STATE_HEADER) { 431 /* set to default value if header info not ready yet*/ 432 s->r.left = 0; 433 s->r.top = 0; 434 s->r.width = q_data->visible_width; 435 s->r.height = q_data->visible_height; 436 return 0; 437 } 438 439 return 0; 440} 441 442static int vidioc_vdec_s_selection(struct file *file, void *priv, 443 struct v4l2_selection *s) 444{ 445 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 446 447 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 448 return -EINVAL; 449 450 switch (s->target) { 451 case V4L2_SEL_TGT_COMPOSE: 452 s->r.left = 0; 453 s->r.top = 0; 454 s->r.width = ctx->picinfo.pic_w; 455 s->r.height = ctx->picinfo.pic_h; 456 break; 457 default: 458 return -EINVAL; 459 } 460 461 return 0; 462} 463 464static int vidioc_vdec_s_fmt(struct file *file, void *priv, 465 struct v4l2_format *f) 466{ 467 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 468 struct v4l2_pix_format_mplane *pix_mp; 469 struct mtk_q_data *q_data; 470 int ret = 0; 471 const struct mtk_video_fmt *fmt; 472 const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 473 474 mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id); 475 476 q_data = mtk_vdec_get_q_data(ctx, f->type); 477 if (!q_data) 478 return -EINVAL; 479 480 pix_mp = &f->fmt.pix_mp; 481 /* 482 * Setting OUTPUT format after OUTPUT buffers are allocated is invalid 483 * if using the stateful API. 484 */ 485 if (!dec_pdata->uses_stateless_api && 486 f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE && 487 vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) { 488 mtk_v4l2_vdec_err(ctx, "out_q_ctx buffers already requested"); 489 ret = -EBUSY; 490 } 491 492 /* 493 * Setting CAPTURE format after CAPTURE buffers are allocated is 494 * invalid. 495 */ 496 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && 497 vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) { 498 mtk_v4l2_vdec_err(ctx, "cap_q_ctx buffers already requested"); 499 ret = -EBUSY; 500 } 501 502 fmt = mtk_vdec_find_format(f, dec_pdata); 503 if (fmt == NULL) { 504 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 505 f->fmt.pix.pixelformat = 506 dec_pdata->default_out_fmt->fourcc; 507 fmt = mtk_vdec_find_format(f, dec_pdata); 508 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 509 f->fmt.pix.pixelformat = 510 dec_pdata->default_cap_fmt->fourcc; 511 fmt = mtk_vdec_find_format(f, dec_pdata); 512 } 513 } 514 if (fmt == NULL) 515 return -EINVAL; 516 517 q_data->fmt = fmt; 518 vidioc_try_fmt(ctx, f, q_data->fmt); 519 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 520 q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage; 521 q_data->coded_width = pix_mp->width; 522 q_data->coded_height = pix_mp->height; 523 524 ctx->colorspace = pix_mp->colorspace; 525 ctx->ycbcr_enc = pix_mp->ycbcr_enc; 526 ctx->quantization = pix_mp->quantization; 527 ctx->xfer_func = pix_mp->xfer_func; 528 529 ctx->current_codec = fmt->fourcc; 530 if (ctx->state == MTK_STATE_FREE) { 531 ret = vdec_if_init(ctx, q_data->fmt->fourcc); 532 if (ret) { 533 mtk_v4l2_vdec_err(ctx, "[%d]: vdec_if_init() fail ret=%d", 534 ctx->id, ret); 535 return -EINVAL; 536 } 537 ctx->state = MTK_STATE_INIT; 538 } 539 } else { 540 ctx->capture_fourcc = fmt->fourcc; 541 } 542 543 /* 544 * If using the stateless API, S_FMT should have the effect of setting 545 * the CAPTURE queue resolution no matter which queue it was called on. 546 */ 547 if (dec_pdata->uses_stateless_api) { 548 ctx->picinfo.pic_w = pix_mp->width; 549 ctx->picinfo.pic_h = pix_mp->height; 550 551 /* 552 * If get pic info fail, need to use the default pic info params, or 553 * v4l2-compliance will fail 554 */ 555 ret = vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo); 556 if (ret) { 557 mtk_v4l2_vdec_err(ctx, "[%d]Error!! Get GET_PARAM_PICTURE_INFO Fail", 558 ctx->id); 559 } 560 561 ctx->last_decoded_picinfo = ctx->picinfo; 562 563 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1) { 564 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] = 565 ctx->picinfo.fb_sz[0] + 566 ctx->picinfo.fb_sz[1]; 567 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 568 ctx->picinfo.buf_w; 569 } else { 570 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] = 571 ctx->picinfo.fb_sz[0]; 572 ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] = 573 ctx->picinfo.buf_w; 574 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] = 575 ctx->picinfo.fb_sz[1]; 576 ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] = 577 ctx->picinfo.buf_w; 578 } 579 580 ctx->q_data[MTK_Q_DATA_DST].coded_width = ctx->picinfo.buf_w; 581 ctx->q_data[MTK_Q_DATA_DST].coded_height = ctx->picinfo.buf_h; 582 mtk_v4l2_vdec_dbg(2, ctx, 583 "[%d] init() plane:%d wxh=%dx%d pic wxh=%dx%d sz=0x%x_0x%x", 584 ctx->id, pix_mp->num_planes, 585 ctx->picinfo.buf_w, ctx->picinfo.buf_h, 586 ctx->picinfo.pic_w, ctx->picinfo.pic_h, 587 ctx->q_data[MTK_Q_DATA_DST].sizeimage[0], 588 ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]); 589 } 590 return 0; 591} 592 593static int vidioc_enum_framesizes(struct file *file, void *priv, 594 struct v4l2_frmsizeenum *fsize) 595{ 596 int i = 0; 597 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 598 const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 599 600 if (fsize->index != 0) 601 return -EINVAL; 602 603 for (i = 0; i < *dec_pdata->num_formats; i++) { 604 if (fsize->pixel_format != dec_pdata->vdec_formats[i].fourcc) 605 continue; 606 607 /* Only coded formats have frame sizes set */ 608 if (!dec_pdata->vdec_formats[i].frmsize.max_width) 609 return -ENOTTY; 610 611 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 612 fsize->stepwise = dec_pdata->vdec_formats[i].frmsize; 613 614 mtk_v4l2_vdec_dbg(1, ctx, "%x, %d %d %d %d %d %d", 615 ctx->dev->dec_capability, fsize->stepwise.min_width, 616 fsize->stepwise.max_width, fsize->stepwise.step_width, 617 fsize->stepwise.min_height, fsize->stepwise.max_height, 618 fsize->stepwise.step_height); 619 620 return 0; 621 } 622 623 return -EINVAL; 624} 625 626static int vidioc_enum_fmt(struct file *file, struct v4l2_fmtdesc *f, 627 bool output_queue) 628{ 629 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 630 const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata; 631 const struct mtk_video_fmt *fmt; 632 int i, j = 0; 633 634 for (i = 0; i < *dec_pdata->num_formats; i++) { 635 if (output_queue && 636 dec_pdata->vdec_formats[i].type != MTK_FMT_DEC) 637 continue; 638 if (!output_queue && 639 dec_pdata->vdec_formats[i].type != MTK_FMT_FRAME) 640 continue; 641 642 if (!output_queue && !mtk_vdec_get_cap_fmt(ctx, i)) 643 continue; 644 645 if (j == f->index) 646 break; 647 ++j; 648 } 649 650 if (i == *dec_pdata->num_formats) 651 return -EINVAL; 652 653 fmt = &dec_pdata->vdec_formats[i]; 654 f->pixelformat = fmt->fourcc; 655 f->flags = fmt->flags; 656 657 return 0; 658} 659 660static int vidioc_vdec_enum_fmt_vid_cap(struct file *file, void *priv, 661 struct v4l2_fmtdesc *f) 662{ 663 return vidioc_enum_fmt(file, f, false); 664} 665 666static int vidioc_vdec_enum_fmt_vid_out(struct file *file, void *priv, 667 struct v4l2_fmtdesc *f) 668{ 669 return vidioc_enum_fmt(file, f, true); 670} 671 672static int vidioc_vdec_g_fmt(struct file *file, void *priv, 673 struct v4l2_format *f) 674{ 675 struct mtk_vcodec_dec_ctx *ctx = file_to_dec_ctx(file); 676 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 677 struct mtk_q_data *q_data; 678 679 q_data = mtk_vdec_get_q_data(ctx, f->type); 680 681 pix_mp->field = V4L2_FIELD_NONE; 682 pix_mp->colorspace = ctx->colorspace; 683 pix_mp->ycbcr_enc = ctx->ycbcr_enc; 684 pix_mp->quantization = ctx->quantization; 685 pix_mp->xfer_func = ctx->xfer_func; 686 687 if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && 688 (ctx->state >= MTK_STATE_HEADER)) { 689 /* Until STREAMOFF is called on the CAPTURE queue 690 * (acknowledging the event), the driver operates as if 691 * the resolution hasn't changed yet. 692 * So we just return picinfo yet, and update picinfo in 693 * stop_streaming hook function 694 */ 695 q_data->sizeimage[0] = ctx->picinfo.fb_sz[0]; 696 q_data->sizeimage[1] = ctx->picinfo.fb_sz[1]; 697 q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w; 698 q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w; 699 q_data->coded_width = ctx->picinfo.buf_w; 700 q_data->coded_height = ctx->picinfo.buf_h; 701 ctx->last_decoded_picinfo.cap_fourcc = q_data->fmt->fourcc; 702 703 /* 704 * Width and height are set to the dimensions 705 * of the movie, the buffer is bigger and 706 * further processing stages should crop to this 707 * rectangle. 708 */ 709 pix_mp->width = q_data->coded_width; 710 pix_mp->height = q_data->coded_height; 711 712 /* 713 * Set pixelformat to the format in which mt vcodec 714 * outputs the decoded frame 715 */ 716 pix_mp->num_planes = q_data->fmt->num_planes; 717 pix_mp->pixelformat = q_data->fmt->fourcc; 718 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; 719 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; 720 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1]; 721 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1]; 722 723 } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 724 /* 725 * This is run on OUTPUT 726 * The buffer contains compressed image 727 * so width and height have no meaning. 728 * Assign value here to pass v4l2-compliance test 729 */ 730 pix_mp->width = q_data->visible_width; 731 pix_mp->height = q_data->visible_height; 732 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; 733 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; 734 pix_mp->pixelformat = q_data->fmt->fourcc; 735 pix_mp->num_planes = q_data->fmt->num_planes; 736 } else { 737 pix_mp->width = q_data->coded_width; 738 pix_mp->height = q_data->coded_height; 739 pix_mp->num_planes = q_data->fmt->num_planes; 740 pix_mp->pixelformat = q_data->fmt->fourcc; 741 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; 742 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; 743 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1]; 744 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1]; 745 746 mtk_v4l2_vdec_dbg(1, ctx, "[%d] type=%d state=%d Format information not ready!", 747 ctx->id, f->type, ctx->state); 748 } 749 750 return 0; 751} 752 753int vb2ops_vdec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, 754 unsigned int *nplanes, unsigned int sizes[], 755 struct device *alloc_devs[]) 756{ 757 struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vq); 758 struct mtk_q_data *q_data; 759 unsigned int i; 760 761 q_data = mtk_vdec_get_q_data(ctx, vq->type); 762 763 if (q_data == NULL) { 764 mtk_v4l2_vdec_err(ctx, "vq->type=%d err\n", vq->type); 765 return -EINVAL; 766 } 767 768 if (*nplanes) { 769 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 770 if (*nplanes != q_data->fmt->num_planes) 771 return -EINVAL; 772 } else { 773 if (*nplanes != 1) 774 return -EINVAL; 775 } 776 for (i = 0; i < *nplanes; i++) { 777 if (sizes[i] < q_data->sizeimage[i]) 778 return -EINVAL; 779 } 780 } else { 781 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 782 *nplanes = q_data->fmt->num_planes; 783 else 784 *nplanes = 1; 785 786 for (i = 0; i < *nplanes; i++) 787 sizes[i] = q_data->sizeimage[i]; 788 } 789 790 mtk_v4l2_vdec_dbg(1, ctx, 791 "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ", 792 ctx->id, vq->type, *nplanes, *nbuffers, sizes[0], sizes[1]); 793 794 return 0; 795} 796 797int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb) 798{ 799 struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 800 struct mtk_q_data *q_data; 801 int i; 802 803 mtk_v4l2_vdec_dbg(3, ctx, "[%d] (%d) id=%d", 804 ctx->id, vb->vb2_queue->type, vb->index); 805 806 q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type); 807 808 for (i = 0; i < q_data->fmt->num_planes; i++) { 809 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) { 810 mtk_v4l2_vdec_err(ctx, "data will not fit into plane %d (%lu < %d)", 811 i, vb2_plane_size(vb, i), q_data->sizeimage[i]); 812 return -EINVAL; 813 } 814 if (!V4L2_TYPE_IS_OUTPUT(vb->type)) 815 vb2_set_plane_payload(vb, i, q_data->sizeimage[i]); 816 } 817 818 return 0; 819} 820 821void vb2ops_vdec_buf_finish(struct vb2_buffer *vb) 822{ 823 struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 824 struct vb2_v4l2_buffer *vb2_v4l2; 825 struct mtk_video_dec_buf *buf; 826 bool buf_error; 827 828 vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf); 829 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb); 830 mutex_lock(&ctx->lock); 831 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 832 buf->queued_in_v4l2 = false; 833 buf->queued_in_vb2 = false; 834 } 835 buf_error = buf->error; 836 mutex_unlock(&ctx->lock); 837 838 if (buf_error) { 839 mtk_v4l2_vdec_err(ctx, "Unrecoverable error on buffer."); 840 ctx->state = MTK_STATE_ABORT; 841 } 842} 843 844int vb2ops_vdec_buf_init(struct vb2_buffer *vb) 845{ 846 struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb, 847 struct vb2_v4l2_buffer, vb2_buf); 848 struct mtk_video_dec_buf *buf = container_of(vb2_v4l2, 849 struct mtk_video_dec_buf, m2m_buf.vb); 850 851 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 852 buf->used = false; 853 buf->queued_in_v4l2 = false; 854 } 855 856 return 0; 857} 858 859int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count) 860{ 861 struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(q); 862 863 if (ctx->state == MTK_STATE_FLUSH) 864 ctx->state = MTK_STATE_HEADER; 865 866 return 0; 867} 868 869void vb2ops_vdec_stop_streaming(struct vb2_queue *q) 870{ 871 struct vb2_v4l2_buffer *src_buf = NULL, *dst_buf = NULL; 872 struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(q); 873 int ret; 874 875 mtk_v4l2_vdec_dbg(3, ctx, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d", 876 ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt); 877 878 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { 879 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) { 880 if (src_buf != &ctx->empty_flush_buf.vb) { 881 struct media_request *req = 882 src_buf->vb2_buf.req_obj.req; 883 v4l2_m2m_buf_done(src_buf, 884 VB2_BUF_STATE_ERROR); 885 if (req) 886 v4l2_ctrl_request_complete(req, &ctx->ctrl_hdl); 887 } 888 } 889 return; 890 } 891 892 if (ctx->state >= MTK_STATE_HEADER) { 893 894 /* Until STREAMOFF is called on the CAPTURE queue 895 * (acknowledging the event), the driver operates 896 * as if the resolution hasn't changed yet, i.e. 897 * VIDIOC_G_FMT< etc. return previous resolution. 898 * So we update picinfo here 899 */ 900 ctx->picinfo = ctx->last_decoded_picinfo; 901 902 mtk_v4l2_vdec_dbg(2, ctx, 903 "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", 904 ctx->id, ctx->last_decoded_picinfo.pic_w, 905 ctx->last_decoded_picinfo.pic_h, 906 ctx->picinfo.pic_w, ctx->picinfo.pic_h, 907 ctx->last_decoded_picinfo.buf_w, 908 ctx->last_decoded_picinfo.buf_h); 909 910 ret = ctx->dev->vdec_pdata->flush_decoder(ctx); 911 if (ret) 912 mtk_v4l2_vdec_err(ctx, "DecodeFinal failed, ret=%d", ret); 913 } 914 ctx->state = MTK_STATE_FLUSH; 915 916 while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) { 917 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0); 918 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2) 919 vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0); 920 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); 921 } 922 923} 924 925static void m2mops_vdec_device_run(void *priv) 926{ 927 struct mtk_vcodec_dec_ctx *ctx = priv; 928 struct mtk_vcodec_dec_dev *dev = ctx->dev; 929 930 queue_work(dev->decode_workqueue, &ctx->decode_work); 931} 932 933static int m2mops_vdec_job_ready(void *m2m_priv) 934{ 935 struct mtk_vcodec_dec_ctx *ctx = m2m_priv; 936 937 mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id); 938 939 if (ctx->state == MTK_STATE_ABORT) 940 return 0; 941 942 if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) || 943 (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h)) 944 return 0; 945 946 if (ctx->state != MTK_STATE_HEADER) 947 return 0; 948 949 return 1; 950} 951 952static void m2mops_vdec_job_abort(void *priv) 953{ 954 struct mtk_vcodec_dec_ctx *ctx = priv; 955 956 ctx->state = MTK_STATE_ABORT; 957} 958 959const struct v4l2_m2m_ops mtk_vdec_m2m_ops = { 960 .device_run = m2mops_vdec_device_run, 961 .job_ready = m2mops_vdec_job_ready, 962 .job_abort = m2mops_vdec_job_abort, 963}; 964 965const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = { 966 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 967 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 968 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 969 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 970 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 971 972 .vidioc_qbuf = vidioc_vdec_qbuf, 973 .vidioc_dqbuf = vidioc_vdec_dqbuf, 974 975 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane, 976 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane, 977 978 .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt, 979 .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt, 980 .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt, 981 .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt, 982 983 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 984 985 .vidioc_enum_fmt_vid_cap = vidioc_vdec_enum_fmt_vid_cap, 986 .vidioc_enum_fmt_vid_out = vidioc_vdec_enum_fmt_vid_out, 987 .vidioc_enum_framesizes = vidioc_enum_framesizes, 988 989 .vidioc_querycap = vidioc_vdec_querycap, 990 .vidioc_subscribe_event = vidioc_vdec_subscribe_evt, 991 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 992 .vidioc_g_selection = vidioc_vdec_g_selection, 993 .vidioc_s_selection = vidioc_vdec_s_selection, 994 995 .vidioc_decoder_cmd = vidioc_decoder_cmd, 996 .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd, 997}; 998 999int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq, 1000 struct vb2_queue *dst_vq) 1001{ 1002 struct mtk_vcodec_dec_ctx *ctx = priv; 1003 int ret = 0; 1004 1005 mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id); 1006 1007 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1008 src_vq->io_modes = VB2_DMABUF | VB2_MMAP; 1009 src_vq->drv_priv = ctx; 1010 src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf); 1011 src_vq->ops = ctx->dev->vdec_pdata->vdec_vb2_ops; 1012 src_vq->mem_ops = &vb2_dma_contig_memops; 1013 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1014 src_vq->lock = &ctx->dev->dev_mutex; 1015 src_vq->dev = &ctx->dev->plat_dev->dev; 1016 src_vq->allow_cache_hints = 1; 1017 1018 ret = vb2_queue_init(src_vq); 1019 if (ret) { 1020 mtk_v4l2_vdec_err(ctx, "Failed to initialize videobuf2 queue(output)"); 1021 return ret; 1022 } 1023 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1024 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; 1025 dst_vq->drv_priv = ctx; 1026 dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf); 1027 dst_vq->ops = ctx->dev->vdec_pdata->vdec_vb2_ops; 1028 dst_vq->mem_ops = &vb2_dma_contig_memops; 1029 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1030 dst_vq->lock = &ctx->dev->dev_mutex; 1031 dst_vq->dev = &ctx->dev->plat_dev->dev; 1032 dst_vq->allow_cache_hints = 1; 1033 1034 ret = vb2_queue_init(dst_vq); 1035 if (ret) 1036 mtk_v4l2_vdec_err(ctx, "Failed to initialize videobuf2 queue(capture)"); 1037 1038 return ret; 1039}