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 if (vdec->codec_info.color_primaries == V4L2_COLORSPACE_DEFAULT) 812 vdec->codec_info.color_primaries = V4L2_COLORSPACE_REC709; 813 if (vdec->codec_info.transfer_chars == V4L2_XFER_FUNC_DEFAULT) 814 vdec->codec_info.transfer_chars = V4L2_XFER_FUNC_709; 815 if (vdec->codec_info.matrix_coeffs == V4L2_YCBCR_ENC_DEFAULT) 816 vdec->codec_info.matrix_coeffs = V4L2_YCBCR_ENC_709; 817 if (vdec->codec_info.full_range == V4L2_QUANTIZATION_DEFAULT) 818 vdec->codec_info.full_range = V4L2_QUANTIZATION_LIM_RANGE; 819 } 820 821 static void vdec_init_crop(struct vpu_inst *inst) 822 { 823 struct vdec_t *vdec = inst->priv; 824 825 inst->crop.left = vdec->codec_info.offset_x; 826 inst->crop.top = vdec->codec_info.offset_y; 827 inst->crop.width = vdec->codec_info.width; 828 inst->crop.height = vdec->codec_info.height; 829 } 830 831 static void vdec_init_mbi(struct vpu_inst *inst) 832 { 833 struct vdec_t *vdec = inst->priv; 834 835 vdec->mbi.size = vdec->codec_info.mbi_size; 836 vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer); 837 scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi"); 838 vdec->mbi.type = MEM_RES_MBI; 839 vdec->mbi.tag = vdec->seq_tag; 840 } 841 842 static void vdec_init_dcp(struct vpu_inst *inst) 843 { 844 struct vdec_t *vdec = inst->priv; 845 846 vdec->dcp.size = vdec->codec_info.dcp_size; 847 vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer); 848 scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp"); 849 vdec->dcp.type = MEM_RES_DCP; 850 vdec->dcp.tag = vdec->seq_tag; 851 } 852 853 static void vdec_request_one_fs(struct vdec_fs_info *fs) 854 { 855 fs->req_count++; 856 if (fs->req_count > fs->max_count) 857 fs->req_count = fs->max_count; 858 } 859 860 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs) 861 { 862 struct vpu_buffer *buffer; 863 864 if (!fs->size) 865 return -EINVAL; 866 867 if (fs->count >= fs->req_count) 868 return -EINVAL; 869 870 buffer = &fs->buffer[fs->count]; 871 if (buffer->virt && buffer->length >= fs->size) 872 return 0; 873 874 vpu_free_dma(buffer); 875 buffer->length = fs->size; 876 return vpu_alloc_dma(inst->core, buffer); 877 } 878 879 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs) 880 { 881 int ret; 882 883 while (fs->count < fs->req_count) { 884 ret = vdec_alloc_fs_buffer(inst, fs); 885 if (ret) 886 break; 887 fs->count++; 888 } 889 } 890 891 static void vdec_clear_fs(struct vdec_fs_info *fs) 892 { 893 u32 i; 894 895 if (!fs) 896 return; 897 898 for (i = 0; i < ARRAY_SIZE(fs->buffer); i++) 899 vpu_free_dma(&fs->buffer[i]); 900 memset(fs, 0, sizeof(*fs)); 901 } 902 903 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs) 904 { 905 struct vpu_fs_info info; 906 int ret; 907 908 if (fs->index >= fs->count) 909 return 0; 910 911 memset(&info, 0, sizeof(info)); 912 info.id = fs->index; 913 info.type = fs->type; 914 info.tag = fs->tag; 915 info.luma_addr = fs->buffer[fs->index].phys; 916 info.luma_size = fs->buffer[fs->index].length; 917 ret = vpu_session_alloc_fs(inst, &info); 918 if (ret) 919 return ret; 920 921 fs->index++; 922 return 0; 923 } 924 925 static int vdec_response_frame_abnormal(struct vpu_inst *inst) 926 { 927 struct vdec_t *vdec = inst->priv; 928 struct vpu_fs_info info; 929 930 if (!vdec->req_frame_count) 931 return 0; 932 933 memset(&info, 0, sizeof(info)); 934 info.type = MEM_RES_FRAME; 935 info.tag = vdec->seq_tag + 0xf0; 936 vpu_session_alloc_fs(inst, &info); 937 vdec->req_frame_count--; 938 939 return 0; 940 } 941 942 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf) 943 { 944 struct vdec_t *vdec = inst->priv; 945 struct vpu_vb2_buffer *vpu_buf; 946 struct vpu_fs_info info; 947 int ret; 948 949 if (inst->state != VPU_CODEC_STATE_ACTIVE) 950 return -EINVAL; 951 952 if (vdec->aborting) 953 return -EINVAL; 954 955 if (!vdec->req_frame_count) 956 return -EINVAL; 957 958 if (!vbuf) 959 return -EINVAL; 960 961 if (vdec->slots[vbuf->vb2_buf.index]) { 962 dev_err(inst->dev, "[%d] repeat alloc fs %d\n", 963 inst->id, vbuf->vb2_buf.index); 964 return -EINVAL; 965 } 966 967 dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n", 968 inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag); 969 vpu_buf = to_vpu_vb2_buffer(vbuf); 970 971 memset(&info, 0, sizeof(info)); 972 info.id = vbuf->vb2_buf.index; 973 info.type = MEM_RES_FRAME; 974 info.tag = vdec->seq_tag; 975 info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0); 976 info.luma_size = inst->cap_format.sizeimage[0]; 977 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1); 978 info.chromau_size = inst->cap_format.sizeimage[1]; 979 info.bytesperline = inst->cap_format.bytesperline[0]; 980 ret = vpu_session_alloc_fs(inst, &info); 981 if (ret) 982 return ret; 983 984 vpu_buf->tag = info.tag; 985 vpu_buf->luma = info.luma_addr; 986 vpu_buf->chroma_u = info.chromau_size; 987 vpu_buf->chroma_v = 0; 988 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE); 989 vdec->slots[info.id] = vpu_buf; 990 vdec->req_frame_count--; 991 992 return 0; 993 } 994 995 static void vdec_response_fs_request(struct vpu_inst *inst, bool force) 996 { 997 struct vdec_t *vdec = inst->priv; 998 int i; 999 int ret; 1000 1001 if (force) { 1002 for (i = vdec->req_frame_count; i > 0; i--) 1003 vdec_response_frame_abnormal(inst); 1004 return; 1005 } 1006 1007 for (i = vdec->req_frame_count; i > 0; i--) { 1008 ret = vpu_process_capture_buffer(inst); 1009 if (ret) 1010 break; 1011 if (vdec->eos_received) 1012 break; 1013 } 1014 1015 for (i = vdec->mbi.index; i < vdec->mbi.count; i++) { 1016 if (vdec_response_fs(inst, &vdec->mbi)) 1017 break; 1018 if (vdec->eos_received) 1019 break; 1020 } 1021 for (i = vdec->dcp.index; i < vdec->dcp.count; i++) { 1022 if (vdec_response_fs(inst, &vdec->dcp)) 1023 break; 1024 if (vdec->eos_received) 1025 break; 1026 } 1027 } 1028 1029 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag) 1030 { 1031 struct vpu_fs_info info; 1032 1033 memset(&info, 0, sizeof(info)); 1034 info.id = id; 1035 info.tag = tag; 1036 vpu_session_release_fs(inst, &info); 1037 } 1038 1039 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf) 1040 { 1041 if (!inst->fh.m2m_ctx) 1042 return; 1043 if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE) 1044 return; 1045 if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index)) 1046 return; 1047 v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf); 1048 } 1049 1050 static void vdec_clear_slots(struct vpu_inst *inst) 1051 { 1052 struct vdec_t *vdec = inst->priv; 1053 struct vpu_vb2_buffer *vpu_buf; 1054 struct vb2_v4l2_buffer *vbuf; 1055 int i; 1056 1057 for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) { 1058 if (!vdec->slots[i]) 1059 continue; 1060 1061 vpu_buf = vdec->slots[i]; 1062 vbuf = &vpu_buf->m2m_buf.vb; 1063 1064 vpu_trace(inst->dev, "clear slot %d\n", i); 1065 vdec_response_fs_release(inst, i, vpu_buf->tag); 1066 vdec_recycle_buffer(inst, vbuf); 1067 vdec->slots[i]->state = VPU_BUF_STATE_IDLE; 1068 vdec->slots[i] = NULL; 1069 } 1070 } 1071 1072 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr) 1073 { 1074 struct vdec_t *vdec = inst->priv; 1075 1076 vpu_inst_lock(inst); 1077 memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info)); 1078 1079 vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n", 1080 inst->id, 1081 vdec->codec_info.decoded_width, 1082 vdec->codec_info.decoded_height, 1083 vdec->codec_info.offset_x, 1084 vdec->codec_info.offset_y, 1085 vdec->codec_info.width, 1086 vdec->codec_info.height, 1087 hdr->num_ref_frms, 1088 hdr->num_dpb_frms); 1089 inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms; 1090 vdec->is_source_changed = vdec_check_source_change(inst); 1091 vdec_init_fmt(inst); 1092 vdec_init_crop(inst); 1093 vdec_init_mbi(inst); 1094 vdec_init_dcp(inst); 1095 if (!vdec->seq_hdr_found) { 1096 vdec->seq_tag = vdec->codec_info.tag; 1097 if (vdec->is_source_changed) { 1098 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0); 1099 vpu_notify_source_change(inst); 1100 vdec->is_source_changed = false; 1101 } 1102 } 1103 if (vdec->seq_tag != vdec->codec_info.tag) { 1104 vdec_response_fs_request(inst, true); 1105 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n", 1106 inst->id, vdec->seq_tag, vdec->codec_info.tag); 1107 } 1108 vdec->seq_hdr_found++; 1109 vdec->fixed_fmt = true; 1110 vpu_inst_unlock(inst); 1111 } 1112 1113 static void vdec_event_resolution_change(struct vpu_inst *inst) 1114 { 1115 struct vdec_t *vdec = inst->priv; 1116 1117 vpu_trace(inst->dev, "[%d]\n", inst->id); 1118 vpu_inst_lock(inst); 1119 vdec->seq_tag = vdec->codec_info.tag; 1120 vdec_clear_fs(&vdec->mbi); 1121 vdec_clear_fs(&vdec->dcp); 1122 vdec_clear_slots(inst); 1123 vdec_init_mbi(inst); 1124 vdec_init_dcp(inst); 1125 if (vdec->is_source_changed) { 1126 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0); 1127 vdec->source_change++; 1128 vdec_handle_resolution_change(inst); 1129 vdec->is_source_changed = false; 1130 } 1131 vpu_inst_unlock(inst); 1132 } 1133 1134 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs) 1135 { 1136 struct vdec_t *vdec = inst->priv; 1137 1138 if (!fs) 1139 return; 1140 1141 vpu_inst_lock(inst); 1142 1143 switch (fs->type) { 1144 case MEM_RES_FRAME: 1145 vdec->req_frame_count++; 1146 break; 1147 case MEM_RES_MBI: 1148 vdec_request_one_fs(&vdec->mbi); 1149 break; 1150 case MEM_RES_DCP: 1151 vdec_request_one_fs(&vdec->dcp); 1152 break; 1153 default: 1154 break; 1155 } 1156 1157 vdec_alloc_fs(inst, &vdec->mbi); 1158 vdec_alloc_fs(inst, &vdec->dcp); 1159 1160 vdec_response_fs_request(inst, false); 1161 1162 vpu_inst_unlock(inst); 1163 } 1164 1165 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs) 1166 { 1167 struct vdec_t *vdec = inst->priv; 1168 struct vpu_vb2_buffer *vpu_buf; 1169 struct vb2_v4l2_buffer *vbuf; 1170 1171 if (!fs || fs->id >= ARRAY_SIZE(vdec->slots)) 1172 return; 1173 if (fs->type != MEM_RES_FRAME) 1174 return; 1175 1176 if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) { 1177 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id); 1178 return; 1179 } 1180 1181 vpu_inst_lock(inst); 1182 vpu_buf = vdec->slots[fs->id]; 1183 vdec->slots[fs->id] = NULL; 1184 1185 if (!vpu_buf) { 1186 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id); 1187 goto exit; 1188 } 1189 1190 vbuf = &vpu_buf->m2m_buf.vb; 1191 if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) { 1192 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id); 1193 vdec->sequence++; 1194 } 1195 1196 vdec_response_fs_release(inst, fs->id, vpu_buf->tag); 1197 if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY) 1198 vdec_recycle_buffer(inst, vbuf); 1199 1200 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE); 1201 vpu_process_capture_buffer(inst); 1202 1203 exit: 1204 vpu_inst_unlock(inst); 1205 } 1206 1207 static void vdec_event_eos(struct vpu_inst *inst) 1208 { 1209 struct vdec_t *vdec = inst->priv; 1210 1211 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n", 1212 inst->id, 1213 vdec->params.frame_count, 1214 vdec->decoded_frame_count, 1215 vdec->display_frame_count, 1216 vdec->sequence); 1217 vpu_inst_lock(inst); 1218 vdec->eos_received++; 1219 vdec->fixed_fmt = false; 1220 inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP; 1221 vdec_set_last_buffer_dequeued(inst); 1222 vpu_inst_unlock(inst); 1223 } 1224 1225 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data) 1226 { 1227 switch (event) { 1228 case VPU_MSG_ID_SEQ_HDR_FOUND: 1229 vdec_event_seq_hdr(inst, data); 1230 break; 1231 case VPU_MSG_ID_RES_CHANGE: 1232 vdec_event_resolution_change(inst); 1233 break; 1234 case VPU_MSG_ID_FRAME_REQ: 1235 vdec_event_req_fs(inst, data); 1236 break; 1237 case VPU_MSG_ID_FRAME_RELEASE: 1238 vdec_evnet_rel_fs(inst, data); 1239 break; 1240 case VPU_MSG_ID_PIC_EOS: 1241 vdec_event_eos(inst); 1242 break; 1243 default: 1244 break; 1245 } 1246 } 1247 1248 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb) 1249 { 1250 struct vdec_t *vdec = inst->priv; 1251 struct vb2_v4l2_buffer *vbuf; 1252 struct vpu_rpc_buffer_desc desc; 1253 u32 free_space; 1254 int ret; 1255 1256 vbuf = to_vb2_v4l2_buffer(vb); 1257 dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n", 1258 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0)); 1259 1260 if (inst->state == VPU_CODEC_STATE_DEINIT) 1261 return -EINVAL; 1262 if (vdec->reset_codec) 1263 return -EINVAL; 1264 1265 if (inst->state == VPU_CODEC_STATE_STARTED) 1266 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0); 1267 1268 ret = vpu_iface_get_stream_buffer_desc(inst, &desc); 1269 if (ret) 1270 return ret; 1271 1272 free_space = vpu_helper_get_free_space(inst); 1273 if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000) 1274 return -ENOMEM; 1275 1276 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE); 1277 ret = vpu_iface_input_frame(inst, vb); 1278 if (ret < 0) 1279 return -ENOMEM; 1280 1281 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp); 1282 vdec->params.frame_count++; 1283 1284 if (vdec->drain) 1285 vdec_drain(inst); 1286 1287 return 0; 1288 } 1289 1290 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb) 1291 { 1292 struct vdec_t *vdec = inst->priv; 1293 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1294 int ret; 1295 1296 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) 1297 return -EINVAL; 1298 if (vdec->reset_codec) 1299 return -EINVAL; 1300 1301 ret = vdec_response_frame(inst, vbuf); 1302 if (ret) 1303 return ret; 1304 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf); 1305 return 0; 1306 } 1307 1308 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type) 1309 { 1310 struct vdec_t *vdec = inst->priv; 1311 1312 if (V4L2_TYPE_IS_OUTPUT(type)) 1313 return; 1314 1315 vdec_handle_resolution_change(inst); 1316 if (vdec->eos_received) 1317 vdec_set_last_buffer_dequeued(inst); 1318 } 1319 1320 static void vdec_abort(struct vpu_inst *inst) 1321 { 1322 struct vdec_t *vdec = inst->priv; 1323 struct vpu_rpc_buffer_desc desc; 1324 int ret; 1325 1326 vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state); 1327 1328 vdec->aborting = true; 1329 vpu_iface_add_scode(inst, SCODE_PADDING_ABORT); 1330 vdec->params.end_flag = 1; 1331 vpu_iface_set_decode_params(inst, &vdec->params, 1); 1332 1333 vpu_session_abort(inst); 1334 1335 ret = vpu_iface_get_stream_buffer_desc(inst, &desc); 1336 if (!ret) 1337 vpu_iface_update_stream_buffer(inst, desc.rptr, 1); 1338 1339 vpu_session_rst_buf(inst); 1340 vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n", 1341 inst->id, 1342 vdec->params.frame_count, 1343 vdec->decoded_frame_count, 1344 vdec->display_frame_count, 1345 vdec->sequence); 1346 vdec->params.end_flag = 0; 1347 vdec->drain = 0; 1348 vdec->params.frame_count = 0; 1349 vdec->decoded_frame_count = 0; 1350 vdec->display_frame_count = 0; 1351 vdec->sequence = 0; 1352 vdec->aborting = false; 1353 } 1354 1355 static void vdec_stop(struct vpu_inst *inst, bool free) 1356 { 1357 struct vdec_t *vdec = inst->priv; 1358 1359 vdec_clear_slots(inst); 1360 if (inst->state != VPU_CODEC_STATE_DEINIT) 1361 vpu_session_stop(inst); 1362 vdec_clear_fs(&vdec->mbi); 1363 vdec_clear_fs(&vdec->dcp); 1364 if (free) { 1365 vpu_free_dma(&vdec->udata); 1366 vpu_free_dma(&inst->stream_buffer); 1367 } 1368 vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1); 1369 vdec->reset_codec = false; 1370 } 1371 1372 static void vdec_release(struct vpu_inst *inst) 1373 { 1374 if (inst->id != VPU_INST_NULL_ID) 1375 vpu_trace(inst->dev, "[%d]\n", inst->id); 1376 vpu_inst_lock(inst); 1377 vdec_stop(inst, true); 1378 vpu_inst_unlock(inst); 1379 } 1380 1381 static void vdec_cleanup(struct vpu_inst *inst) 1382 { 1383 struct vdec_t *vdec; 1384 1385 if (!inst) 1386 return; 1387 1388 vdec = inst->priv; 1389 vfree(vdec); 1390 inst->priv = NULL; 1391 vfree(inst); 1392 } 1393 1394 static void vdec_init_params(struct vdec_t *vdec) 1395 { 1396 vdec->params.frame_count = 0; 1397 vdec->params.end_flag = 0; 1398 } 1399 1400 static int vdec_start(struct vpu_inst *inst) 1401 { 1402 struct vdec_t *vdec = inst->priv; 1403 int stream_buffer_size; 1404 int ret; 1405 1406 if (inst->state != VPU_CODEC_STATE_DEINIT) 1407 return 0; 1408 1409 vpu_trace(inst->dev, "[%d]\n", inst->id); 1410 if (!vdec->udata.virt) { 1411 vdec->udata.length = 0x1000; 1412 ret = vpu_alloc_dma(inst->core, &vdec->udata); 1413 if (ret) { 1414 dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id); 1415 goto error; 1416 } 1417 } 1418 1419 if (!inst->stream_buffer.virt) { 1420 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core); 1421 if (stream_buffer_size > 0) { 1422 inst->stream_buffer.length = stream_buffer_size; 1423 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer); 1424 if (ret) { 1425 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id); 1426 goto error; 1427 } 1428 inst->use_stream_buffer = true; 1429 } 1430 } 1431 1432 if (inst->use_stream_buffer) 1433 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer); 1434 vpu_iface_init_instance(inst); 1435 vdec->params.udata.base = vdec->udata.phys; 1436 vdec->params.udata.size = vdec->udata.length; 1437 ret = vpu_iface_set_decode_params(inst, &vdec->params, 0); 1438 if (ret) { 1439 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id); 1440 goto error; 1441 } 1442 1443 vdec_init_params(vdec); 1444 ret = vpu_session_start(inst); 1445 if (ret) { 1446 dev_err(inst->dev, "[%d] start fail\n", inst->id); 1447 goto error; 1448 } 1449 1450 vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0); 1451 1452 return 0; 1453 error: 1454 vpu_free_dma(&vdec->udata); 1455 vpu_free_dma(&inst->stream_buffer); 1456 return ret; 1457 } 1458 1459 static int vdec_start_session(struct vpu_inst *inst, u32 type) 1460 { 1461 struct vdec_t *vdec = inst->priv; 1462 int ret = 0; 1463 1464 if (V4L2_TYPE_IS_OUTPUT(type)) { 1465 if (vdec->reset_codec) 1466 vdec_stop(inst, false); 1467 if (inst->state == VPU_CODEC_STATE_DEINIT) { 1468 ret = vdec_start(inst); 1469 if (ret) 1470 return ret; 1471 } 1472 } 1473 1474 if (V4L2_TYPE_IS_OUTPUT(type)) { 1475 if (inst->state == VPU_CODEC_STATE_SEEK) 1476 vdec_update_state(inst, vdec->state, 1); 1477 vdec->eos_received = 0; 1478 vpu_process_output_buffer(inst); 1479 } else { 1480 vdec_cmd_start(inst); 1481 } 1482 if (inst->state == VPU_CODEC_STATE_ACTIVE) 1483 vdec_response_fs_request(inst, false); 1484 1485 return ret; 1486 } 1487 1488 static int vdec_stop_session(struct vpu_inst *inst, u32 type) 1489 { 1490 struct vdec_t *vdec = inst->priv; 1491 1492 if (inst->state == VPU_CODEC_STATE_DEINIT) 1493 return 0; 1494 1495 if (V4L2_TYPE_IS_OUTPUT(type)) { 1496 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0); 1497 vdec->drain = 0; 1498 } else { 1499 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) { 1500 vdec_abort(inst); 1501 vdec->eos_received = 0; 1502 } 1503 vdec_clear_slots(inst); 1504 } 1505 1506 return 0; 1507 } 1508 1509 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i) 1510 { 1511 struct vdec_t *vdec = inst->priv; 1512 int num = -1; 1513 1514 switch (i) { 1515 case 0: 1516 num = scnprintf(str, size, 1517 "req_frame_count = %d\ninterlaced = %d\n", 1518 vdec->req_frame_count, 1519 vdec->codec_info.progressive ? 0 : 1); 1520 break; 1521 case 1: 1522 num = scnprintf(str, size, 1523 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n", 1524 vdec->mbi.size, 1525 vdec->mbi.req_count, 1526 vdec->mbi.count, 1527 vdec->mbi.index); 1528 break; 1529 case 2: 1530 num = scnprintf(str, size, 1531 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n", 1532 vdec->dcp.size, 1533 vdec->dcp.req_count, 1534 vdec->dcp.count, 1535 vdec->dcp.index); 1536 break; 1537 case 3: 1538 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count); 1539 break; 1540 case 4: 1541 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count); 1542 break; 1543 case 5: 1544 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count); 1545 break; 1546 case 6: 1547 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence); 1548 break; 1549 case 7: 1550 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n", 1551 vdec->drain, vdec->eos_received, vdec->source_change); 1552 break; 1553 case 8: 1554 num = scnprintf(str, size, "fps = %d/%d\n", 1555 vdec->codec_info.frame_rate.numerator, 1556 vdec->codec_info.frame_rate.denominator); 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