1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright 2020-2021 NXP 4 */ 5 6 #include <linux/init.h> 7 #include <linux/interconnect.h> 8 #include <linux/ioctl.h> 9 #include <linux/list.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/vmalloc.h> 13 #include <linux/videodev2.h> 14 #include <media/v4l2-device.h> 15 #include <media/v4l2-event.h> 16 #include <media/v4l2-mem2mem.h> 17 #include <media/v4l2-ioctl.h> 18 #include <media/videobuf2-v4l2.h> 19 #include <media/videobuf2-dma-contig.h> 20 #include <media/videobuf2-vmalloc.h> 21 #include "vpu.h" 22 #include "vpu_defs.h" 23 #include "vpu_core.h" 24 #include "vpu_helpers.h" 25 #include "vpu_v4l2.h" 26 #include "vpu_cmds.h" 27 #include "vpu_rpc.h" 28 29 #define VDEC_MIN_BUFFER_CAP 8 30 #define VDEC_MIN_BUFFER_OUT 8 31 32 struct vdec_fs_info { 33 char name[8]; 34 u32 type; 35 u32 max_count; 36 u32 req_count; 37 u32 count; 38 u32 index; 39 u32 size; 40 struct vpu_buffer buffer[32]; 41 u32 tag; 42 }; 43 44 struct vdec_t { 45 u32 seq_hdr_found; 46 struct vpu_buffer udata; 47 struct vpu_decode_params params; 48 struct vpu_dec_codec_info codec_info; 49 enum vpu_codec_state state; 50 51 struct vpu_vb2_buffer *slots[VB2_MAX_FRAME]; 52 u32 req_frame_count; 53 struct vdec_fs_info mbi; 54 struct vdec_fs_info dcp; 55 u32 seq_tag; 56 57 bool reset_codec; 58 bool fixed_fmt; 59 u32 decoded_frame_count; 60 u32 display_frame_count; 61 u32 sequence; 62 u32 eos_received; 63 bool is_source_changed; 64 u32 source_change; 65 u32 drain; 66 bool aborting; 67 }; 68 69 static const struct vpu_format vdec_formats[] = { 70 { 71 .pixfmt = V4L2_PIX_FMT_NV12M_8L128, 72 .num_planes = 2, 73 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 74 }, 75 { 76 .pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128, 77 .num_planes = 2, 78 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 79 }, 80 { 81 .pixfmt = V4L2_PIX_FMT_H264, 82 .num_planes = 1, 83 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 84 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 85 }, 86 { 87 .pixfmt = V4L2_PIX_FMT_H264_MVC, 88 .num_planes = 1, 89 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 90 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 91 }, 92 { 93 .pixfmt = V4L2_PIX_FMT_HEVC, 94 .num_planes = 1, 95 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 96 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 97 }, 98 { 99 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G, 100 .num_planes = 1, 101 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 102 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 103 }, 104 { 105 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L, 106 .num_planes = 1, 107 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 108 }, 109 { 110 .pixfmt = V4L2_PIX_FMT_MPEG2, 111 .num_planes = 1, 112 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 113 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 114 }, 115 { 116 .pixfmt = V4L2_PIX_FMT_MPEG4, 117 .num_planes = 1, 118 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 119 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 120 }, 121 { 122 .pixfmt = V4L2_PIX_FMT_XVID, 123 .num_planes = 1, 124 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 125 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 126 }, 127 { 128 .pixfmt = V4L2_PIX_FMT_VP8, 129 .num_planes = 1, 130 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 131 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 132 }, 133 { 134 .pixfmt = V4L2_PIX_FMT_H263, 135 .num_planes = 1, 136 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 137 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION 138 }, 139 {0, 0, 0, 0}, 140 }; 141 142 static const struct v4l2_ctrl_ops vdec_ctrl_ops = { 143 .g_volatile_ctrl = vpu_helper_g_volatile_ctrl, 144 }; 145 146 static int vdec_ctrl_init(struct vpu_inst *inst) 147 { 148 struct v4l2_ctrl *ctrl; 149 int ret; 150 151 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20); 152 if (ret) 153 return ret; 154 155 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops, 156 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2); 157 if (ctrl) 158 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 159 160 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops, 161 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2); 162 if (ctrl) 163 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 164 165 if (inst->ctrl_handler.error) { 166 ret = inst->ctrl_handler.error; 167 v4l2_ctrl_handler_free(&inst->ctrl_handler); 168 return ret; 169 } 170 171 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler); 172 if (ret) { 173 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret); 174 v4l2_ctrl_handler_free(&inst->ctrl_handler); 175 return ret; 176 } 177 178 return 0; 179 } 180 181 static void vdec_handle_resolution_change(struct vpu_inst *inst) 182 { 183 struct vdec_t *vdec = inst->priv; 184 struct vb2_queue *q; 185 186 if (!inst->fh.m2m_ctx) 187 return; 188 189 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 190 return; 191 if (!vdec->source_change) 192 return; 193 194 q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx); 195 if (!list_empty(&q->done_list)) 196 return; 197 198 vdec->source_change--; 199 vpu_notify_source_change(inst); 200 } 201 202 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force) 203 { 204 struct vdec_t *vdec = inst->priv; 205 enum vpu_codec_state pre_state = inst->state; 206 207 if (state == VPU_CODEC_STATE_SEEK) { 208 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 209 vdec->state = inst->state; 210 else 211 vdec->state = VPU_CODEC_STATE_ACTIVE; 212 } 213 if (inst->state != VPU_CODEC_STATE_SEEK || force) 214 inst->state = state; 215 else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 216 vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE; 217 218 if (inst->state != pre_state) 219 vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state); 220 221 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 222 vdec_handle_resolution_change(inst); 223 224 return 0; 225 } 226 227 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst) 228 { 229 struct vdec_t *vdec = inst->priv; 230 231 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 232 return; 233 234 if (vdec->eos_received) { 235 if (!vpu_set_last_buffer_dequeued(inst)) { 236 vdec->eos_received--; 237 vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0); 238 } 239 } 240 } 241 242 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 243 { 244 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver)); 245 strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card)); 246 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info)); 247 248 return 0; 249 } 250 251 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 252 { 253 struct vpu_inst *inst = to_inst(file); 254 struct vdec_t *vdec = inst->priv; 255 const struct vpu_format *fmt; 256 int ret = -EINVAL; 257 258 vpu_inst_lock(inst); 259 if (!V4L2_TYPE_IS_OUTPUT(f->type) && vdec->fixed_fmt) { 260 if (f->index == 0) { 261 f->pixelformat = inst->cap_format.pixfmt; 262 f->flags = inst->cap_format.flags; 263 ret = 0; 264 } 265 } else { 266 fmt = vpu_helper_enum_format(inst, f->type, f->index); 267 memset(f->reserved, 0, sizeof(f->reserved)); 268 if (!fmt) 269 goto exit; 270 271 f->pixelformat = fmt->pixfmt; 272 f->flags = fmt->flags; 273 ret = 0; 274 } 275 276 exit: 277 vpu_inst_unlock(inst); 278 return ret; 279 } 280 281 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 282 { 283 struct vpu_inst *inst = to_inst(file); 284 struct vdec_t *vdec = inst->priv; 285 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 286 struct vpu_format *cur_fmt; 287 int i; 288 289 cur_fmt = vpu_get_format(inst, f->type); 290 291 pixmp->pixelformat = cur_fmt->pixfmt; 292 pixmp->num_planes = cur_fmt->num_planes; 293 pixmp->width = cur_fmt->width; 294 pixmp->height = cur_fmt->height; 295 pixmp->field = cur_fmt->field; 296 pixmp->flags = cur_fmt->flags; 297 for (i = 0; i < pixmp->num_planes; i++) { 298 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i]; 299 pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i]; 300 } 301 302 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries; 303 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars; 304 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs; 305 f->fmt.pix_mp.quantization = vdec->codec_info.full_range; 306 307 return 0; 308 } 309 310 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 311 { 312 struct vpu_inst *inst = to_inst(file); 313 struct vdec_t *vdec = inst->priv; 314 315 vpu_try_fmt_common(inst, f); 316 317 vpu_inst_lock(inst); 318 if (vdec->fixed_fmt) { 319 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries; 320 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars; 321 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs; 322 f->fmt.pix_mp.quantization = vdec->codec_info.full_range; 323 } else { 324 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT; 325 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT; 326 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 327 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT; 328 } 329 vpu_inst_unlock(inst); 330 331 return 0; 332 } 333 334 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f) 335 { 336 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 337 const struct vpu_format *fmt; 338 struct vpu_format *cur_fmt; 339 struct vb2_queue *q; 340 struct vdec_t *vdec = inst->priv; 341 int i; 342 343 if (!inst->fh.m2m_ctx) 344 return -EINVAL; 345 346 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type); 347 if (!q) 348 return -EINVAL; 349 if (vb2_is_busy(q)) 350 return -EBUSY; 351 352 fmt = vpu_try_fmt_common(inst, f); 353 if (!fmt) 354 return -EINVAL; 355 356 cur_fmt = vpu_get_format(inst, f->type); 357 if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) { 358 if (cur_fmt->pixfmt != fmt->pixfmt) { 359 vdec->reset_codec = true; 360 vdec->fixed_fmt = false; 361 } 362 } 363 cur_fmt->pixfmt = fmt->pixfmt; 364 if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) { 365 cur_fmt->num_planes = fmt->num_planes; 366 cur_fmt->flags = fmt->flags; 367 cur_fmt->width = pixmp->width; 368 cur_fmt->height = pixmp->height; 369 for (i = 0; i < fmt->num_planes; i++) { 370 cur_fmt->sizeimage[i] = pixmp->plane_fmt[i].sizeimage; 371 cur_fmt->bytesperline[i] = pixmp->plane_fmt[i].bytesperline; 372 } 373 if (pixmp->field != V4L2_FIELD_ANY) 374 cur_fmt->field = pixmp->field; 375 } else { 376 pixmp->num_planes = cur_fmt->num_planes; 377 pixmp->width = cur_fmt->width; 378 pixmp->height = cur_fmt->height; 379 for (i = 0; i < pixmp->num_planes; i++) { 380 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i]; 381 pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i]; 382 } 383 pixmp->field = cur_fmt->field; 384 } 385 386 if (!vdec->fixed_fmt) { 387 if (V4L2_TYPE_IS_OUTPUT(f->type)) { 388 vdec->params.codec_format = cur_fmt->pixfmt; 389 vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace; 390 vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func; 391 vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc; 392 vdec->codec_info.full_range = f->fmt.pix_mp.quantization; 393 } else { 394 vdec->params.output_format = cur_fmt->pixfmt; 395 inst->crop.left = 0; 396 inst->crop.top = 0; 397 inst->crop.width = cur_fmt->width; 398 inst->crop.height = cur_fmt->height; 399 } 400 } 401 402 vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id, 403 f->fmt.pix_mp.pixelformat, 404 f->fmt.pix_mp.pixelformat >> 8, 405 f->fmt.pix_mp.pixelformat >> 16, 406 f->fmt.pix_mp.pixelformat >> 24, 407 f->fmt.pix_mp.width, 408 f->fmt.pix_mp.height); 409 410 return 0; 411 } 412 413 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 414 { 415 struct vpu_inst *inst = to_inst(file); 416 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 417 struct vdec_t *vdec = inst->priv; 418 int ret = 0; 419 420 vpu_inst_lock(inst); 421 ret = vdec_s_fmt_common(inst, f); 422 if (ret) 423 goto exit; 424 425 if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) { 426 struct v4l2_format fc; 427 428 memset(&fc, 0, sizeof(fc)); 429 fc.type = inst->cap_format.type; 430 fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt; 431 fc.fmt.pix_mp.width = pixmp->width; 432 fc.fmt.pix_mp.height = pixmp->height; 433 vdec_s_fmt_common(inst, &fc); 434 } 435 436 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries; 437 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars; 438 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs; 439 f->fmt.pix_mp.quantization = vdec->codec_info.full_range; 440 441 exit: 442 vpu_inst_unlock(inst); 443 return ret; 444 } 445 446 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 447 { 448 struct vpu_inst *inst = to_inst(file); 449 450 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 451 return -EINVAL; 452 453 switch (s->target) { 454 case V4L2_SEL_TGT_COMPOSE: 455 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 456 case V4L2_SEL_TGT_COMPOSE_PADDED: 457 s->r = inst->crop; 458 break; 459 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 460 s->r.left = 0; 461 s->r.top = 0; 462 s->r.width = inst->cap_format.width; 463 s->r.height = inst->cap_format.height; 464 break; 465 default: 466 return -EINVAL; 467 } 468 469 return 0; 470 } 471 472 static int vdec_drain(struct vpu_inst *inst) 473 { 474 struct vdec_t *vdec = inst->priv; 475 476 if (!inst->fh.m2m_ctx) 477 return 0; 478 479 if (!vdec->drain) 480 return 0; 481 482 if (!vpu_is_source_empty(inst)) 483 return 0; 484 485 if (!vdec->params.frame_count) { 486 vpu_set_last_buffer_dequeued(inst); 487 return 0; 488 } 489 490 vpu_iface_add_scode(inst, SCODE_PADDING_EOS); 491 vdec->params.end_flag = 1; 492 vpu_iface_set_decode_params(inst, &vdec->params, 1); 493 vdec->drain = 0; 494 vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count); 495 496 return 0; 497 } 498 499 static int vdec_cmd_start(struct vpu_inst *inst) 500 { 501 struct vdec_t *vdec = inst->priv; 502 503 switch (inst->state) { 504 case VPU_CODEC_STATE_STARTED: 505 case VPU_CODEC_STATE_DRAIN: 506 case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE: 507 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0); 508 break; 509 default: 510 break; 511 } 512 vpu_process_capture_buffer(inst); 513 if (vdec->eos_received) 514 vdec_set_last_buffer_dequeued(inst); 515 return 0; 516 } 517 518 static int vdec_cmd_stop(struct vpu_inst *inst) 519 { 520 struct vdec_t *vdec = inst->priv; 521 522 vpu_trace(inst->dev, "[%d]\n", inst->id); 523 524 if (inst->state == VPU_CODEC_STATE_DEINIT) { 525 vpu_set_last_buffer_dequeued(inst); 526 } else { 527 vdec->drain = 1; 528 vdec_drain(inst); 529 } 530 531 return 0; 532 } 533 534 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd) 535 { 536 struct vpu_inst *inst = to_inst(file); 537 int ret; 538 539 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd); 540 if (ret) 541 return ret; 542 543 vpu_inst_lock(inst); 544 switch (cmd->cmd) { 545 case V4L2_DEC_CMD_START: 546 vdec_cmd_start(inst); 547 break; 548 case V4L2_DEC_CMD_STOP: 549 vdec_cmd_stop(inst); 550 break; 551 default: 552 break; 553 } 554 vpu_inst_unlock(inst); 555 556 return 0; 557 } 558 559 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub) 560 { 561 switch (sub->type) { 562 case V4L2_EVENT_EOS: 563 return v4l2_event_subscribe(fh, sub, 0, NULL); 564 case V4L2_EVENT_SOURCE_CHANGE: 565 return v4l2_src_change_event_subscribe(fh, sub); 566 case V4L2_EVENT_CTRL: 567 return v4l2_ctrl_subscribe_event(fh, sub); 568 default: 569 return -EINVAL; 570 } 571 572 return 0; 573 } 574 575 static const struct v4l2_ioctl_ops vdec_ioctl_ops = { 576 .vidioc_querycap = vdec_querycap, 577 .vidioc_enum_fmt_vid_cap = vdec_enum_fmt, 578 .vidioc_enum_fmt_vid_out = vdec_enum_fmt, 579 .vidioc_g_fmt_vid_cap_mplane = vdec_g_fmt, 580 .vidioc_g_fmt_vid_out_mplane = vdec_g_fmt, 581 .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt, 582 .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt, 583 .vidioc_s_fmt_vid_cap_mplane = vdec_s_fmt, 584 .vidioc_s_fmt_vid_out_mplane = vdec_s_fmt, 585 .vidioc_g_selection = vdec_g_selection, 586 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, 587 .vidioc_decoder_cmd = vdec_decoder_cmd, 588 .vidioc_subscribe_event = vdec_subscribe_event, 589 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 590 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 591 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 592 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 593 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 594 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 595 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 596 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 597 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 598 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 599 }; 600 601 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type) 602 { 603 struct vdec_t *vdec = inst->priv; 604 605 if (V4L2_TYPE_IS_OUTPUT(type)) 606 return true; 607 608 if (vdec->req_frame_count) 609 return true; 610 611 return false; 612 } 613 614 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count) 615 { 616 if (count > 1) 617 vpu_skip_frame(inst, count - 1); 618 619 return vpu_next_src_buf(inst); 620 } 621 622 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg) 623 { 624 struct vdec_t *vdec = inst->priv; 625 struct vpu_dec_pic_info *info = arg; 626 struct vpu_vb2_buffer *vpu_buf; 627 struct vb2_v4l2_buffer *vbuf; 628 struct vb2_v4l2_buffer *src_buf; 629 int ret = 0; 630 631 if (!info || info->id >= ARRAY_SIZE(vdec->slots)) 632 return -EINVAL; 633 634 vpu_inst_lock(inst); 635 vpu_buf = vdec->slots[info->id]; 636 if (!vpu_buf) { 637 dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id); 638 ret = -EINVAL; 639 goto exit; 640 } 641 vbuf = &vpu_buf->m2m_buf.vb; 642 src_buf = vdec_get_src_buffer(inst, info->consumed_count); 643 if (src_buf) { 644 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true); 645 if (info->consumed_count) { 646 v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx); 647 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE); 648 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); 649 } else { 650 vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED); 651 } 652 } 653 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) 654 dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id); 655 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED); 656 vdec->decoded_frame_count++; 657 exit: 658 vpu_inst_unlock(inst); 659 660 return ret; 661 } 662 663 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma) 664 { 665 struct vdec_t *vdec = inst->priv; 666 int i; 667 668 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) { 669 if (!vdec->slots[i]) 670 continue; 671 if (luma == vdec->slots[i]->luma) 672 return vdec->slots[i]; 673 } 674 675 return NULL; 676 } 677 678 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame) 679 { 680 struct vdec_t *vdec = inst->priv; 681 struct vpu_vb2_buffer *vpu_buf; 682 struct vb2_v4l2_buffer *vbuf; 683 u32 sequence; 684 685 if (!frame) 686 return; 687 688 vpu_inst_lock(inst); 689 sequence = vdec->sequence++; 690 vpu_buf = vdec_find_buffer(inst, frame->luma); 691 vpu_inst_unlock(inst); 692 if (!vpu_buf) { 693 dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n", 694 inst->id, frame->id, frame->luma); 695 return; 696 } 697 if (frame->skipped) { 698 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id); 699 return; 700 } 701 702 vbuf = &vpu_buf->m2m_buf.vb; 703 if (vbuf->vb2_buf.index != frame->id) 704 dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n", 705 inst->id, vbuf->vb2_buf.index, frame->id); 706 707 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED) 708 dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id); 709 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY); 710 vb2_set_plane_payload(&vbuf->vb2_buf, 0, inst->cap_format.sizeimage[0]); 711 vb2_set_plane_payload(&vbuf->vb2_buf, 1, inst->cap_format.sizeimage[1]); 712 vbuf->field = inst->cap_format.field; 713 vbuf->sequence = sequence; 714 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp); 715 716 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 717 vpu_inst_lock(inst); 718 vdec->display_frame_count++; 719 vpu_inst_unlock(inst); 720 dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n", 721 inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence); 722 } 723 724 static void vdec_stop_done(struct vpu_inst *inst) 725 { 726 struct vdec_t *vdec = inst->priv; 727 728 vpu_inst_lock(inst); 729 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0); 730 vdec->seq_hdr_found = 0; 731 vdec->req_frame_count = 0; 732 vdec->reset_codec = false; 733 vdec->fixed_fmt = false; 734 vdec->params.end_flag = 0; 735 vdec->drain = 0; 736 vdec->params.frame_count = 0; 737 vdec->decoded_frame_count = 0; 738 vdec->display_frame_count = 0; 739 vdec->sequence = 0; 740 vdec->eos_received = 0; 741 vdec->is_source_changed = false; 742 vdec->source_change = 0; 743 inst->total_input_count = 0; 744 vpu_inst_unlock(inst); 745 } 746 747 static bool vdec_check_source_change(struct vpu_inst *inst) 748 { 749 struct vdec_t *vdec = inst->priv; 750 const struct vpu_format *fmt; 751 int i; 752 753 if (!inst->fh.m2m_ctx) 754 return false; 755 756 if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx))) 757 return true; 758 fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt); 759 if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt) 760 return true; 761 if (inst->cap_format.width != vdec->codec_info.decoded_width) 762 return true; 763 if (inst->cap_format.height != vdec->codec_info.decoded_height) 764 return true; 765 if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap) 766 return true; 767 if (inst->crop.left != vdec->codec_info.offset_x) 768 return true; 769 if (inst->crop.top != vdec->codec_info.offset_y) 770 return true; 771 if (inst->crop.width != vdec->codec_info.width) 772 return true; 773 if (inst->crop.height != vdec->codec_info.height) 774 return true; 775 if (fmt && inst->cap_format.num_planes != fmt->num_planes) 776 return true; 777 for (i = 0; i < inst->cap_format.num_planes; i++) { 778 if (inst->cap_format.bytesperline[i] != vdec->codec_info.bytesperline[i]) 779 return true; 780 if (inst->cap_format.sizeimage[i] != vdec->codec_info.sizeimage[i]) 781 return true; 782 } 783 784 return false; 785 } 786 787 static void vdec_init_fmt(struct vpu_inst *inst) 788 { 789 struct vdec_t *vdec = inst->priv; 790 const struct vpu_format *fmt; 791 int i; 792 793 fmt = vpu_helper_find_format(inst, inst->cap_format.type, vdec->codec_info.pixfmt); 794 inst->out_format.width = vdec->codec_info.width; 795 inst->out_format.height = vdec->codec_info.height; 796 inst->cap_format.width = vdec->codec_info.decoded_width; 797 inst->cap_format.height = vdec->codec_info.decoded_height; 798 inst->cap_format.pixfmt = vdec->codec_info.pixfmt; 799 if (fmt) { 800 inst->cap_format.num_planes = fmt->num_planes; 801 inst->cap_format.flags = fmt->flags; 802 } 803 for (i = 0; i < inst->cap_format.num_planes; i++) { 804 inst->cap_format.bytesperline[i] = vdec->codec_info.bytesperline[i]; 805 inst->cap_format.sizeimage[i] = vdec->codec_info.sizeimage[i]; 806 } 807 if (vdec->codec_info.progressive) 808 inst->cap_format.field = V4L2_FIELD_NONE; 809 else 810 inst->cap_format.field = V4L2_FIELD_SEQ_TB; 811 } 812 813 static void vdec_init_crop(struct vpu_inst *inst) 814 { 815 struct vdec_t *vdec = inst->priv; 816 817 inst->crop.left = vdec->codec_info.offset_x; 818 inst->crop.top = vdec->codec_info.offset_y; 819 inst->crop.width = vdec->codec_info.width; 820 inst->crop.height = vdec->codec_info.height; 821 } 822 823 static void vdec_init_mbi(struct vpu_inst *inst) 824 { 825 struct vdec_t *vdec = inst->priv; 826 827 vdec->mbi.size = vdec->codec_info.mbi_size; 828 vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer); 829 scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi"); 830 vdec->mbi.type = MEM_RES_MBI; 831 vdec->mbi.tag = vdec->seq_tag; 832 } 833 834 static void vdec_init_dcp(struct vpu_inst *inst) 835 { 836 struct vdec_t *vdec = inst->priv; 837 838 vdec->dcp.size = vdec->codec_info.dcp_size; 839 vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer); 840 scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp"); 841 vdec->dcp.type = MEM_RES_DCP; 842 vdec->dcp.tag = vdec->seq_tag; 843 } 844 845 static void vdec_request_one_fs(struct vdec_fs_info *fs) 846 { 847 fs->req_count++; 848 if (fs->req_count > fs->max_count) 849 fs->req_count = fs->max_count; 850 } 851 852 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs) 853 { 854 struct vpu_buffer *buffer; 855 856 if (!fs->size) 857 return -EINVAL; 858 859 if (fs->count >= fs->req_count) 860 return -EINVAL; 861 862 buffer = &fs->buffer[fs->count]; 863 if (buffer->virt && buffer->length >= fs->size) 864 return 0; 865 866 vpu_free_dma(buffer); 867 buffer->length = fs->size; 868 return vpu_alloc_dma(inst->core, buffer); 869 } 870 871 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs) 872 { 873 int ret; 874 875 while (fs->count < fs->req_count) { 876 ret = vdec_alloc_fs_buffer(inst, fs); 877 if (ret) 878 break; 879 fs->count++; 880 } 881 } 882 883 static void vdec_clear_fs(struct vdec_fs_info *fs) 884 { 885 u32 i; 886 887 if (!fs) 888 return; 889 890 for (i = 0; i < ARRAY_SIZE(fs->buffer); i++) 891 vpu_free_dma(&fs->buffer[i]); 892 memset(fs, 0, sizeof(*fs)); 893 } 894 895 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs) 896 { 897 struct vpu_fs_info info; 898 int ret; 899 900 if (fs->index >= fs->count) 901 return 0; 902 903 memset(&info, 0, sizeof(info)); 904 info.id = fs->index; 905 info.type = fs->type; 906 info.tag = fs->tag; 907 info.luma_addr = fs->buffer[fs->index].phys; 908 info.luma_size = fs->buffer[fs->index].length; 909 ret = vpu_session_alloc_fs(inst, &info); 910 if (ret) 911 return ret; 912 913 fs->index++; 914 return 0; 915 } 916 917 static int vdec_response_frame_abnormal(struct vpu_inst *inst) 918 { 919 struct vdec_t *vdec = inst->priv; 920 struct vpu_fs_info info; 921 922 if (!vdec->req_frame_count) 923 return 0; 924 925 memset(&info, 0, sizeof(info)); 926 info.type = MEM_RES_FRAME; 927 info.tag = vdec->seq_tag + 0xf0; 928 vpu_session_alloc_fs(inst, &info); 929 vdec->req_frame_count--; 930 931 return 0; 932 } 933 934 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf) 935 { 936 struct vdec_t *vdec = inst->priv; 937 struct vpu_vb2_buffer *vpu_buf; 938 struct vpu_fs_info info; 939 int ret; 940 941 if (inst->state != VPU_CODEC_STATE_ACTIVE) 942 return -EINVAL; 943 944 if (vdec->aborting) 945 return -EINVAL; 946 947 if (!vdec->req_frame_count) 948 return -EINVAL; 949 950 if (!vbuf) 951 return -EINVAL; 952 953 if (vdec->slots[vbuf->vb2_buf.index]) { 954 dev_err(inst->dev, "[%d] repeat alloc fs %d\n", 955 inst->id, vbuf->vb2_buf.index); 956 return -EINVAL; 957 } 958 959 dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n", 960 inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag); 961 vpu_buf = to_vpu_vb2_buffer(vbuf); 962 963 memset(&info, 0, sizeof(info)); 964 info.id = vbuf->vb2_buf.index; 965 info.type = MEM_RES_FRAME; 966 info.tag = vdec->seq_tag; 967 info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0); 968 info.luma_size = inst->cap_format.sizeimage[0]; 969 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1); 970 info.chromau_size = inst->cap_format.sizeimage[1]; 971 info.bytesperline = inst->cap_format.bytesperline[0]; 972 ret = vpu_session_alloc_fs(inst, &info); 973 if (ret) 974 return ret; 975 976 vpu_buf->tag = info.tag; 977 vpu_buf->luma = info.luma_addr; 978 vpu_buf->chroma_u = info.chromau_size; 979 vpu_buf->chroma_v = 0; 980 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE); 981 vdec->slots[info.id] = vpu_buf; 982 vdec->req_frame_count--; 983 984 return 0; 985 } 986 987 static void vdec_response_fs_request(struct vpu_inst *inst, bool force) 988 { 989 struct vdec_t *vdec = inst->priv; 990 int i; 991 int ret; 992 993 if (force) { 994 for (i = vdec->req_frame_count; i > 0; i--) 995 vdec_response_frame_abnormal(inst); 996 return; 997 } 998 999 for (i = vdec->req_frame_count; i > 0; i--) { 1000 ret = vpu_process_capture_buffer(inst); 1001 if (ret) 1002 break; 1003 if (vdec->eos_received) 1004 break; 1005 } 1006 1007 for (i = vdec->mbi.index; i < vdec->mbi.count; i++) { 1008 if (vdec_response_fs(inst, &vdec->mbi)) 1009 break; 1010 if (vdec->eos_received) 1011 break; 1012 } 1013 for (i = vdec->dcp.index; i < vdec->dcp.count; i++) { 1014 if (vdec_response_fs(inst, &vdec->dcp)) 1015 break; 1016 if (vdec->eos_received) 1017 break; 1018 } 1019 } 1020 1021 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag) 1022 { 1023 struct vpu_fs_info info; 1024 1025 memset(&info, 0, sizeof(info)); 1026 info.id = id; 1027 info.tag = tag; 1028 vpu_session_release_fs(inst, &info); 1029 } 1030 1031 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf) 1032 { 1033 if (!inst->fh.m2m_ctx) 1034 return; 1035 if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE) 1036 return; 1037 if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index)) 1038 return; 1039 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf); 1040 } 1041 1042 static void vdec_clear_slots(struct vpu_inst *inst) 1043 { 1044 struct vdec_t *vdec = inst->priv; 1045 struct vpu_vb2_buffer *vpu_buf; 1046 struct vb2_v4l2_buffer *vbuf; 1047 int i; 1048 1049 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) { 1050 if (!vdec->slots[i]) 1051 continue; 1052 1053 vpu_buf = vdec->slots[i]; 1054 vbuf = &vpu_buf->m2m_buf.vb; 1055 1056 vpu_trace(inst->dev, "clear slot %d\n", i); 1057 vdec_response_fs_release(inst, i, vpu_buf->tag); 1058 vdec_recycle_buffer(inst, vbuf); 1059 vdec->slots[i]->state = VPU_BUF_STATE_IDLE; 1060 vdec->slots[i] = NULL; 1061 } 1062 } 1063 1064 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr) 1065 { 1066 struct vdec_t *vdec = inst->priv; 1067 1068 vpu_inst_lock(inst); 1069 memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info)); 1070 1071 vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n", 1072 inst->id, 1073 vdec->codec_info.decoded_width, 1074 vdec->codec_info.decoded_height, 1075 vdec->codec_info.offset_x, 1076 vdec->codec_info.offset_y, 1077 vdec->codec_info.width, 1078 vdec->codec_info.height, 1079 hdr->num_ref_frms, 1080 hdr->num_dpb_frms); 1081 inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms; 1082 vdec->is_source_changed = vdec_check_source_change(inst); 1083 vdec_init_fmt(inst); 1084 vdec_init_crop(inst); 1085 vdec_init_mbi(inst); 1086 vdec_init_dcp(inst); 1087 if (!vdec->seq_hdr_found) { 1088 vdec->seq_tag = vdec->codec_info.tag; 1089 if (vdec->is_source_changed) { 1090 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0); 1091 vpu_notify_source_change(inst); 1092 vdec->is_source_changed = false; 1093 } 1094 } 1095 if (vdec->seq_tag != vdec->codec_info.tag) { 1096 vdec_response_fs_request(inst, true); 1097 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n", 1098 inst->id, vdec->seq_tag, vdec->codec_info.tag); 1099 } 1100 vdec->seq_hdr_found++; 1101 vdec->fixed_fmt = true; 1102 vpu_inst_unlock(inst); 1103 } 1104 1105 static void vdec_event_resolution_change(struct vpu_inst *inst) 1106 { 1107 struct vdec_t *vdec = inst->priv; 1108 1109 vpu_trace(inst->dev, "[%d]\n", inst->id); 1110 vpu_inst_lock(inst); 1111 vdec->seq_tag = vdec->codec_info.tag; 1112 vdec_clear_fs(&vdec->mbi); 1113 vdec_clear_fs(&vdec->dcp); 1114 vdec_clear_slots(inst); 1115 vdec_init_mbi(inst); 1116 vdec_init_dcp(inst); 1117 if (vdec->is_source_changed) { 1118 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0); 1119 vdec->source_change++; 1120 vdec_handle_resolution_change(inst); 1121 vdec->is_source_changed = false; 1122 } 1123 vpu_inst_unlock(inst); 1124 } 1125 1126 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs) 1127 { 1128 struct vdec_t *vdec = inst->priv; 1129 1130 if (!fs) 1131 return; 1132 1133 vpu_inst_lock(inst); 1134 1135 switch (fs->type) { 1136 case MEM_RES_FRAME: 1137 vdec->req_frame_count++; 1138 break; 1139 case MEM_RES_MBI: 1140 vdec_request_one_fs(&vdec->mbi); 1141 break; 1142 case MEM_RES_DCP: 1143 vdec_request_one_fs(&vdec->dcp); 1144 break; 1145 default: 1146 break; 1147 } 1148 1149 vdec_alloc_fs(inst, &vdec->mbi); 1150 vdec_alloc_fs(inst, &vdec->dcp); 1151 1152 vdec_response_fs_request(inst, false); 1153 1154 vpu_inst_unlock(inst); 1155 } 1156 1157 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs) 1158 { 1159 struct vdec_t *vdec = inst->priv; 1160 struct vpu_vb2_buffer *vpu_buf; 1161 struct vb2_v4l2_buffer *vbuf; 1162 1163 if (!fs || fs->id >= ARRAY_SIZE(vdec->slots)) 1164 return; 1165 if (fs->type != MEM_RES_FRAME) 1166 return; 1167 1168 if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) { 1169 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id); 1170 return; 1171 } 1172 1173 vpu_inst_lock(inst); 1174 vpu_buf = vdec->slots[fs->id]; 1175 vdec->slots[fs->id] = NULL; 1176 1177 if (!vpu_buf) { 1178 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id); 1179 goto exit; 1180 } 1181 1182 vbuf = &vpu_buf->m2m_buf.vb; 1183 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) { 1184 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id); 1185 vdec->sequence++; 1186 } 1187 1188 vdec_response_fs_release(inst, fs->id, vpu_buf->tag); 1189 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY) 1190 vdec_recycle_buffer(inst, vbuf); 1191 1192 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE); 1193 vpu_process_capture_buffer(inst); 1194 1195 exit: 1196 vpu_inst_unlock(inst); 1197 } 1198 1199 static void vdec_event_eos(struct vpu_inst *inst) 1200 { 1201 struct vdec_t *vdec = inst->priv; 1202 1203 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n", 1204 inst->id, 1205 vdec->params.frame_count, 1206 vdec->decoded_frame_count, 1207 vdec->display_frame_count, 1208 vdec->sequence); 1209 vpu_inst_lock(inst); 1210 vdec->eos_received++; 1211 vdec->fixed_fmt = false; 1212 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP; 1213 vdec_set_last_buffer_dequeued(inst); 1214 vpu_inst_unlock(inst); 1215 } 1216 1217 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data) 1218 { 1219 switch (event) { 1220 case VPU_MSG_ID_SEQ_HDR_FOUND: 1221 vdec_event_seq_hdr(inst, data); 1222 break; 1223 case VPU_MSG_ID_RES_CHANGE: 1224 vdec_event_resolution_change(inst); 1225 break; 1226 case VPU_MSG_ID_FRAME_REQ: 1227 vdec_event_req_fs(inst, data); 1228 break; 1229 case VPU_MSG_ID_FRAME_RELEASE: 1230 vdec_evnet_rel_fs(inst, data); 1231 break; 1232 case VPU_MSG_ID_PIC_EOS: 1233 vdec_event_eos(inst); 1234 break; 1235 default: 1236 break; 1237 } 1238 } 1239 1240 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb) 1241 { 1242 struct vdec_t *vdec = inst->priv; 1243 struct vb2_v4l2_buffer *vbuf; 1244 struct vpu_rpc_buffer_desc desc; 1245 u32 free_space; 1246 int ret; 1247 1248 vbuf = to_vb2_v4l2_buffer(vb); 1249 dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n", 1250 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0)); 1251 1252 if (inst->state == VPU_CODEC_STATE_DEINIT) 1253 return -EINVAL; 1254 if (vdec->reset_codec) 1255 return -EINVAL; 1256 1257 if (inst->state == VPU_CODEC_STATE_STARTED) 1258 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0); 1259 1260 ret = vpu_iface_get_stream_buffer_desc(inst, &desc); 1261 if (ret) 1262 return ret; 1263 1264 free_space = vpu_helper_get_free_space(inst); 1265 if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000) 1266 return -ENOMEM; 1267 1268 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE); 1269 ret = vpu_iface_input_frame(inst, vb); 1270 if (ret < 0) 1271 return -ENOMEM; 1272 1273 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp); 1274 vdec->params.frame_count++; 1275 1276 if (vdec->drain) 1277 vdec_drain(inst); 1278 1279 return 0; 1280 } 1281 1282 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb) 1283 { 1284 struct vdec_t *vdec = inst->priv; 1285 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1286 int ret; 1287 1288 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 1289 return -EINVAL; 1290 if (vdec->reset_codec) 1291 return -EINVAL; 1292 1293 ret = vdec_response_frame(inst, vbuf); 1294 if (ret) 1295 return ret; 1296 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf); 1297 return 0; 1298 } 1299 1300 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type) 1301 { 1302 struct vdec_t *vdec = inst->priv; 1303 1304 if (V4L2_TYPE_IS_OUTPUT(type)) 1305 return; 1306 1307 vdec_handle_resolution_change(inst); 1308 if (vdec->eos_received) 1309 vdec_set_last_buffer_dequeued(inst); 1310 } 1311 1312 static void vdec_abort(struct vpu_inst *inst) 1313 { 1314 struct vdec_t *vdec = inst->priv; 1315 struct vpu_rpc_buffer_desc desc; 1316 int ret; 1317 1318 vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state); 1319 1320 vdec->aborting = true; 1321 vpu_iface_add_scode(inst, SCODE_PADDING_ABORT); 1322 vdec->params.end_flag = 1; 1323 vpu_iface_set_decode_params(inst, &vdec->params, 1); 1324 1325 vpu_session_abort(inst); 1326 1327 ret = vpu_iface_get_stream_buffer_desc(inst, &desc); 1328 if (!ret) 1329 vpu_iface_update_stream_buffer(inst, desc.rptr, 1); 1330 1331 vpu_session_rst_buf(inst); 1332 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n", 1333 inst->id, 1334 vdec->params.frame_count, 1335 vdec->decoded_frame_count, 1336 vdec->display_frame_count, 1337 vdec->sequence); 1338 vdec->params.end_flag = 0; 1339 vdec->drain = 0; 1340 vdec->params.frame_count = 0; 1341 vdec->decoded_frame_count = 0; 1342 vdec->display_frame_count = 0; 1343 vdec->sequence = 0; 1344 vdec->aborting = false; 1345 } 1346 1347 static void vdec_stop(struct vpu_inst *inst, bool free) 1348 { 1349 struct vdec_t *vdec = inst->priv; 1350 1351 vdec_clear_slots(inst); 1352 if (inst->state != VPU_CODEC_STATE_DEINIT) 1353 vpu_session_stop(inst); 1354 vdec_clear_fs(&vdec->mbi); 1355 vdec_clear_fs(&vdec->dcp); 1356 if (free) { 1357 vpu_free_dma(&vdec->udata); 1358 vpu_free_dma(&inst->stream_buffer); 1359 } 1360 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1); 1361 vdec->reset_codec = false; 1362 } 1363 1364 static void vdec_release(struct vpu_inst *inst) 1365 { 1366 if (inst->id != VPU_INST_NULL_ID) 1367 vpu_trace(inst->dev, "[%d]\n", inst->id); 1368 vpu_inst_lock(inst); 1369 vdec_stop(inst, true); 1370 vpu_inst_unlock(inst); 1371 } 1372 1373 static void vdec_cleanup(struct vpu_inst *inst) 1374 { 1375 struct vdec_t *vdec; 1376 1377 if (!inst) 1378 return; 1379 1380 vdec = inst->priv; 1381 vfree(vdec); 1382 inst->priv = NULL; 1383 vfree(inst); 1384 } 1385 1386 static void vdec_init_params(struct vdec_t *vdec) 1387 { 1388 vdec->params.frame_count = 0; 1389 vdec->params.end_flag = 0; 1390 } 1391 1392 static int vdec_start(struct vpu_inst *inst) 1393 { 1394 struct vdec_t *vdec = inst->priv; 1395 int stream_buffer_size; 1396 int ret; 1397 1398 if (inst->state != VPU_CODEC_STATE_DEINIT) 1399 return 0; 1400 1401 vpu_trace(inst->dev, "[%d]\n", inst->id); 1402 if (!vdec->udata.virt) { 1403 vdec->udata.length = 0x1000; 1404 ret = vpu_alloc_dma(inst->core, &vdec->udata); 1405 if (ret) { 1406 dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id); 1407 goto error; 1408 } 1409 } 1410 1411 if (!inst->stream_buffer.virt) { 1412 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core); 1413 if (stream_buffer_size > 0) { 1414 inst->stream_buffer.length = stream_buffer_size; 1415 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer); 1416 if (ret) { 1417 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id); 1418 goto error; 1419 } 1420 inst->use_stream_buffer = true; 1421 } 1422 } 1423 1424 if (inst->use_stream_buffer) 1425 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer); 1426 vpu_iface_init_instance(inst); 1427 vdec->params.udata.base = vdec->udata.phys; 1428 vdec->params.udata.size = vdec->udata.length; 1429 ret = vpu_iface_set_decode_params(inst, &vdec->params, 0); 1430 if (ret) { 1431 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id); 1432 goto error; 1433 } 1434 1435 vdec_init_params(vdec); 1436 ret = vpu_session_start(inst); 1437 if (ret) { 1438 dev_err(inst->dev, "[%d] start fail\n", inst->id); 1439 goto error; 1440 } 1441 1442 vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0); 1443 1444 return 0; 1445 error: 1446 vpu_free_dma(&vdec->udata); 1447 vpu_free_dma(&inst->stream_buffer); 1448 return ret; 1449 } 1450 1451 static int vdec_start_session(struct vpu_inst *inst, u32 type) 1452 { 1453 struct vdec_t *vdec = inst->priv; 1454 int ret = 0; 1455 1456 if (V4L2_TYPE_IS_OUTPUT(type)) { 1457 if (vdec->reset_codec) 1458 vdec_stop(inst, false); 1459 if (inst->state == VPU_CODEC_STATE_DEINIT) { 1460 ret = vdec_start(inst); 1461 if (ret) 1462 return ret; 1463 } 1464 } 1465 1466 if (V4L2_TYPE_IS_OUTPUT(type)) { 1467 if (inst->state == VPU_CODEC_STATE_SEEK) 1468 vdec_update_state(inst, vdec->state, 1); 1469 vdec->eos_received = 0; 1470 vpu_process_output_buffer(inst); 1471 } else { 1472 vdec_cmd_start(inst); 1473 } 1474 if (inst->state == VPU_CODEC_STATE_ACTIVE) 1475 vdec_response_fs_request(inst, false); 1476 1477 return ret; 1478 } 1479 1480 static int vdec_stop_session(struct vpu_inst *inst, u32 type) 1481 { 1482 struct vdec_t *vdec = inst->priv; 1483 1484 if (inst->state == VPU_CODEC_STATE_DEINIT) 1485 return 0; 1486 1487 if (V4L2_TYPE_IS_OUTPUT(type)) { 1488 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0); 1489 vdec->drain = 0; 1490 } else { 1491 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) { 1492 vdec_abort(inst); 1493 vdec->eos_received = 0; 1494 } 1495 vdec_clear_slots(inst); 1496 } 1497 1498 return 0; 1499 } 1500 1501 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i) 1502 { 1503 struct vdec_t *vdec = inst->priv; 1504 int num = -1; 1505 1506 switch (i) { 1507 case 0: 1508 num = scnprintf(str, size, 1509 "req_frame_count = %d\ninterlaced = %d\n", 1510 vdec->req_frame_count, 1511 vdec->codec_info.progressive ? 0 : 1); 1512 break; 1513 case 1: 1514 num = scnprintf(str, size, 1515 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n", 1516 vdec->mbi.size, 1517 vdec->mbi.req_count, 1518 vdec->mbi.count, 1519 vdec->mbi.index); 1520 break; 1521 case 2: 1522 num = scnprintf(str, size, 1523 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n", 1524 vdec->dcp.size, 1525 vdec->dcp.req_count, 1526 vdec->dcp.count, 1527 vdec->dcp.index); 1528 break; 1529 case 3: 1530 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count); 1531 break; 1532 case 4: 1533 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count); 1534 break; 1535 case 5: 1536 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count); 1537 break; 1538 case 6: 1539 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence); 1540 break; 1541 case 7: 1542 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n", 1543 vdec->drain, vdec->eos_received, vdec->source_change); 1544 break; 1545 case 8: 1546 num = scnprintf(str, size, "fps = %d/%d\n", 1547 vdec->codec_info.frame_rate.numerator, 1548 vdec->codec_info.frame_rate.denominator); 1549 break; 1550 case 9: 1551 num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n", 1552 vdec->codec_info.color_primaries, 1553 vdec->codec_info.transfer_chars, 1554 vdec->codec_info.matrix_coeffs, 1555 vdec->codec_info.full_range, 1556 vdec->codec_info.vui_present); 1557 break; 1558 default: 1559 break; 1560 } 1561 1562 return num; 1563 } 1564 1565 static struct vpu_inst_ops vdec_inst_ops = { 1566 .ctrl_init = vdec_ctrl_init, 1567 .check_ready = vdec_check_ready, 1568 .buf_done = vdec_buf_done, 1569 .get_one_frame = vdec_frame_decoded, 1570 .stop_done = vdec_stop_done, 1571 .event_notify = vdec_event_notify, 1572 .release = vdec_release, 1573 .cleanup = vdec_cleanup, 1574 .start = vdec_start_session, 1575 .stop = vdec_stop_session, 1576 .process_output = vdec_process_output, 1577 .process_capture = vdec_process_capture, 1578 .on_queue_empty = vdec_on_queue_empty, 1579 .get_debug_info = vdec_get_debug_info, 1580 .wait_prepare = vpu_inst_unlock, 1581 .wait_finish = vpu_inst_lock, 1582 }; 1583 1584 static void vdec_init(struct file *file) 1585 { 1586 struct vpu_inst *inst = to_inst(file); 1587 struct v4l2_format f; 1588 1589 memset(&f, 0, sizeof(f)); 1590 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1591 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264; 1592 f.fmt.pix_mp.width = 1280; 1593 f.fmt.pix_mp.height = 720; 1594 f.fmt.pix_mp.field = V4L2_FIELD_NONE; 1595 vdec_s_fmt(file, &inst->fh, &f); 1596 1597 memset(&f, 0, sizeof(f)); 1598 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1599 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128; 1600 f.fmt.pix_mp.width = 1280; 1601 f.fmt.pix_mp.height = 720; 1602 f.fmt.pix_mp.field = V4L2_FIELD_NONE; 1603 vdec_s_fmt(file, &inst->fh, &f); 1604 } 1605 1606 static int vdec_open(struct file *file) 1607 { 1608 struct vpu_inst *inst; 1609 struct vdec_t *vdec; 1610 int ret; 1611 1612 inst = vzalloc(sizeof(*inst)); 1613 if (!inst) 1614 return -ENOMEM; 1615 1616 vdec = vzalloc(sizeof(*vdec)); 1617 if (!vdec) { 1618 vfree(inst); 1619 return -ENOMEM; 1620 } 1621 1622 inst->ops = &vdec_inst_ops; 1623 inst->formats = vdec_formats; 1624 inst->type = VPU_CORE_TYPE_DEC; 1625 inst->priv = vdec; 1626 1627 ret = vpu_v4l2_open(file, inst); 1628 if (ret) 1629 return ret; 1630 1631 vdec->fixed_fmt = false; 1632 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP; 1633 inst->min_buffer_out = VDEC_MIN_BUFFER_OUT; 1634 vdec_init(file); 1635 1636 return 0; 1637 } 1638 1639 static const struct v4l2_file_operations vdec_fops = { 1640 .owner = THIS_MODULE, 1641 .open = vdec_open, 1642 .release = vpu_v4l2_close, 1643 .unlocked_ioctl = video_ioctl2, 1644 .poll = v4l2_m2m_fop_poll, 1645 .mmap = v4l2_m2m_fop_mmap, 1646 }; 1647 1648 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void) 1649 { 1650 return &vdec_ioctl_ops; 1651 } 1652 1653 const struct v4l2_file_operations *vdec_get_fops(void) 1654 { 1655 return &vdec_fops; 1656 } 1657