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