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/delay.h> 13 #include <linux/videodev2.h> 14 #include <linux/ktime.h> 15 #include <linux/rational.h> 16 #include <linux/vmalloc.h> 17 #include <media/v4l2-device.h> 18 #include <media/v4l2-event.h> 19 #include <media/v4l2-mem2mem.h> 20 #include <media/v4l2-ioctl.h> 21 #include <media/videobuf2-v4l2.h> 22 #include <media/videobuf2-dma-contig.h> 23 #include <media/videobuf2-vmalloc.h> 24 #include "vpu.h" 25 #include "vpu_defs.h" 26 #include "vpu_core.h" 27 #include "vpu_helpers.h" 28 #include "vpu_v4l2.h" 29 #include "vpu_cmds.h" 30 #include "vpu_rpc.h" 31 32 #define VENC_OUTPUT_ENABLE BIT(0) 33 #define VENC_CAPTURE_ENABLE BIT(1) 34 #define VENC_ENABLE_MASK (VENC_OUTPUT_ENABLE | VENC_CAPTURE_ENABLE) 35 #define VENC_MAX_BUF_CNT 8 36 37 struct venc_t { 38 struct vpu_encode_params params; 39 u32 request_key_frame; 40 u32 input_ready; 41 u32 cpb_size; 42 bool bitrate_change; 43 44 struct vpu_buffer enc[VENC_MAX_BUF_CNT]; 45 struct vpu_buffer ref[VENC_MAX_BUF_CNT]; 46 struct vpu_buffer act[VENC_MAX_BUF_CNT]; 47 struct list_head frames; 48 u32 frame_count; 49 u32 encode_count; 50 u32 ready_count; 51 u32 enable; 52 u32 stopped; 53 54 u32 skipped_count; 55 u32 skipped_bytes; 56 57 wait_queue_head_t wq; 58 }; 59 60 struct venc_frame_t { 61 struct list_head list; 62 struct vpu_enc_pic_info info; 63 u32 bytesused; 64 s64 timestamp; 65 }; 66 67 static const struct vpu_format venc_formats[] = { 68 { 69 .pixfmt = V4L2_PIX_FMT_NV12M, 70 .num_planes = 2, 71 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 72 }, 73 { 74 .pixfmt = V4L2_PIX_FMT_H264, 75 .num_planes = 1, 76 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 77 }, 78 {0, 0, 0, 0}, 79 }; 80 81 static int venc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) 82 { 83 strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver)); 84 strscpy(cap->card, "amphion vpu encoder", sizeof(cap->card)); 85 strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info)); 86 87 return 0; 88 } 89 90 static int venc_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f) 91 { 92 struct vpu_inst *inst = to_inst(file); 93 const struct vpu_format *fmt; 94 95 memset(f->reserved, 0, sizeof(f->reserved)); 96 fmt = vpu_helper_enum_format(inst, f->type, f->index); 97 if (!fmt) 98 return -EINVAL; 99 100 f->pixelformat = fmt->pixfmt; 101 f->flags = fmt->flags; 102 103 return 0; 104 } 105 106 static int venc_enum_framesizes(struct file *file, void *fh, struct v4l2_frmsizeenum *fsize) 107 { 108 struct vpu_inst *inst = to_inst(file); 109 const struct vpu_core_resources *res; 110 111 if (!fsize || fsize->index) 112 return -EINVAL; 113 114 if (!vpu_helper_find_format(inst, 0, fsize->pixel_format)) 115 return -EINVAL; 116 117 res = vpu_get_resource(inst); 118 if (!res) 119 return -EINVAL; 120 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 121 fsize->stepwise.max_width = res->max_width; 122 fsize->stepwise.max_height = res->max_height; 123 fsize->stepwise.min_width = res->min_width; 124 fsize->stepwise.min_height = res->min_height; 125 fsize->stepwise.step_width = res->step_width; 126 fsize->stepwise.step_height = res->step_height; 127 128 return 0; 129 } 130 131 static int venc_enum_frameintervals(struct file *file, void *fh, struct v4l2_frmivalenum *fival) 132 { 133 struct vpu_inst *inst = to_inst(file); 134 const struct vpu_core_resources *res; 135 136 if (!fival || fival->index) 137 return -EINVAL; 138 139 if (!vpu_helper_find_format(inst, 0, fival->pixel_format)) 140 return -EINVAL; 141 142 if (!fival->width || !fival->height) 143 return -EINVAL; 144 145 res = vpu_get_resource(inst); 146 if (!res) 147 return -EINVAL; 148 if (fival->width < res->min_width || fival->width > res->max_width || 149 fival->height < res->min_height || fival->height > res->max_height) 150 return -EINVAL; 151 152 fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS; 153 fival->stepwise.min.numerator = 1; 154 fival->stepwise.min.denominator = USHRT_MAX; 155 fival->stepwise.max.numerator = USHRT_MAX; 156 fival->stepwise.max.denominator = 1; 157 fival->stepwise.step.numerator = 1; 158 fival->stepwise.step.denominator = 1; 159 160 return 0; 161 } 162 163 static int venc_g_fmt(struct file *file, void *fh, struct v4l2_format *f) 164 { 165 struct vpu_inst *inst = to_inst(file); 166 struct venc_t *venc = inst->priv; 167 struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp; 168 struct vpu_format *cur_fmt; 169 int i; 170 171 cur_fmt = vpu_get_format(inst, f->type); 172 173 pixmp->pixelformat = cur_fmt->pixfmt; 174 pixmp->num_planes = cur_fmt->num_planes; 175 pixmp->width = cur_fmt->width; 176 pixmp->height = cur_fmt->height; 177 pixmp->field = cur_fmt->field; 178 pixmp->flags = cur_fmt->flags; 179 for (i = 0; i < pixmp->num_planes; i++) { 180 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i]; 181 pixmp->plane_fmt[i].sizeimage = cur_fmt->sizeimage[i]; 182 } 183 184 f->fmt.pix_mp.colorspace = venc->params.color.primaries; 185 f->fmt.pix_mp.xfer_func = venc->params.color.transfer; 186 f->fmt.pix_mp.ycbcr_enc = venc->params.color.matrix; 187 f->fmt.pix_mp.quantization = venc->params.color.full_range; 188 189 return 0; 190 } 191 192 static int venc_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 193 { 194 struct vpu_inst *inst = to_inst(file); 195 196 vpu_try_fmt_common(inst, f); 197 198 return 0; 199 } 200 201 static int venc_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 202 { 203 struct vpu_inst *inst = to_inst(file); 204 const struct vpu_format *fmt; 205 struct vpu_format *cur_fmt; 206 struct vb2_queue *q; 207 struct venc_t *venc = inst->priv; 208 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 209 int i; 210 211 q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type); 212 if (!q) 213 return -EINVAL; 214 if (vb2_is_busy(q)) 215 return -EBUSY; 216 217 fmt = vpu_try_fmt_common(inst, f); 218 if (!fmt) 219 return -EINVAL; 220 221 cur_fmt = vpu_get_format(inst, f->type); 222 223 cur_fmt->pixfmt = fmt->pixfmt; 224 cur_fmt->num_planes = fmt->num_planes; 225 cur_fmt->flags = fmt->flags; 226 cur_fmt->width = pix_mp->width; 227 cur_fmt->height = pix_mp->height; 228 for (i = 0; i < fmt->num_planes; i++) { 229 cur_fmt->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage; 230 cur_fmt->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline; 231 } 232 233 if (pix_mp->field != V4L2_FIELD_ANY) 234 cur_fmt->field = pix_mp->field; 235 236 if (V4L2_TYPE_IS_OUTPUT(f->type)) { 237 venc->params.input_format = cur_fmt->pixfmt; 238 venc->params.src_stride = cur_fmt->bytesperline[0]; 239 venc->params.src_width = cur_fmt->width; 240 venc->params.src_height = cur_fmt->height; 241 venc->params.crop.left = 0; 242 venc->params.crop.top = 0; 243 venc->params.crop.width = cur_fmt->width; 244 venc->params.crop.height = cur_fmt->height; 245 } else { 246 venc->params.codec_format = cur_fmt->pixfmt; 247 venc->params.out_width = cur_fmt->width; 248 venc->params.out_height = cur_fmt->height; 249 } 250 251 if (V4L2_TYPE_IS_OUTPUT(f->type)) { 252 if (!vpu_color_check_primaries(pix_mp->colorspace)) { 253 venc->params.color.primaries = pix_mp->colorspace; 254 vpu_color_get_default(venc->params.color.primaries, 255 &venc->params.color.transfer, 256 &venc->params.color.matrix, 257 &venc->params.color.full_range); 258 } 259 if (!vpu_color_check_transfers(pix_mp->xfer_func)) 260 venc->params.color.transfer = pix_mp->xfer_func; 261 if (!vpu_color_check_matrix(pix_mp->ycbcr_enc)) 262 venc->params.color.matrix = pix_mp->ycbcr_enc; 263 if (!vpu_color_check_full_range(pix_mp->quantization)) 264 venc->params.color.full_range = pix_mp->quantization; 265 } 266 267 pix_mp->colorspace = venc->params.color.primaries; 268 pix_mp->xfer_func = venc->params.color.transfer; 269 pix_mp->ycbcr_enc = venc->params.color.matrix; 270 pix_mp->quantization = venc->params.color.full_range; 271 272 return 0; 273 } 274 275 static int venc_g_parm(struct file *file, void *fh, struct v4l2_streamparm *parm) 276 { 277 struct vpu_inst *inst = to_inst(file); 278 struct venc_t *venc = inst->priv; 279 struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe; 280 281 if (!parm) 282 return -EINVAL; 283 284 if (!vpu_helper_check_type(inst, parm->type)) 285 return -EINVAL; 286 287 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 288 parm->parm.capture.readbuffers = 0; 289 timeperframe->numerator = venc->params.frame_rate.numerator; 290 timeperframe->denominator = venc->params.frame_rate.denominator; 291 292 return 0; 293 } 294 295 static int venc_s_parm(struct file *file, void *fh, struct v4l2_streamparm *parm) 296 { 297 struct vpu_inst *inst = to_inst(file); 298 struct venc_t *venc = inst->priv; 299 struct v4l2_fract *timeperframe = &parm->parm.capture.timeperframe; 300 unsigned long n, d; 301 302 if (!parm) 303 return -EINVAL; 304 305 if (!vpu_helper_check_type(inst, parm->type)) 306 return -EINVAL; 307 308 if (!timeperframe->numerator) 309 timeperframe->numerator = venc->params.frame_rate.numerator; 310 if (!timeperframe->denominator) 311 timeperframe->denominator = venc->params.frame_rate.denominator; 312 313 venc->params.frame_rate.numerator = timeperframe->numerator; 314 venc->params.frame_rate.denominator = timeperframe->denominator; 315 316 rational_best_approximation(venc->params.frame_rate.numerator, 317 venc->params.frame_rate.denominator, 318 venc->params.frame_rate.numerator, 319 venc->params.frame_rate.denominator, 320 &n, &d); 321 venc->params.frame_rate.numerator = n; 322 venc->params.frame_rate.denominator = d; 323 324 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 325 memset(parm->parm.capture.reserved, 0, sizeof(parm->parm.capture.reserved)); 326 327 return 0; 328 } 329 330 static int venc_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 331 { 332 struct vpu_inst *inst = to_inst(file); 333 struct venc_t *venc = inst->priv; 334 335 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 336 return -EINVAL; 337 338 switch (s->target) { 339 case V4L2_SEL_TGT_CROP_DEFAULT: 340 case V4L2_SEL_TGT_CROP_BOUNDS: 341 s->r.left = 0; 342 s->r.top = 0; 343 s->r.width = inst->out_format.width; 344 s->r.height = inst->out_format.height; 345 break; 346 case V4L2_SEL_TGT_CROP: 347 s->r = venc->params.crop; 348 break; 349 default: 350 return -EINVAL; 351 } 352 353 return 0; 354 } 355 356 static int venc_valid_crop(struct venc_t *venc, const struct vpu_core_resources *res) 357 { 358 struct v4l2_rect *rect = NULL; 359 u32 min_width; 360 u32 min_height; 361 u32 src_width; 362 u32 src_height; 363 364 rect = &venc->params.crop; 365 min_width = res->min_width; 366 min_height = res->min_height; 367 src_width = venc->params.src_width; 368 src_height = venc->params.src_height; 369 370 if (rect->width == 0 || rect->height == 0) 371 return -EINVAL; 372 if (rect->left > src_width - min_width || rect->top > src_height - min_height) 373 return -EINVAL; 374 375 rect->width = min(rect->width, src_width - rect->left); 376 rect->width = max_t(u32, rect->width, min_width); 377 378 rect->height = min(rect->height, src_height - rect->top); 379 rect->height = max_t(u32, rect->height, min_height); 380 381 return 0; 382 } 383 384 static int venc_s_selection(struct file *file, void *fh, struct v4l2_selection *s) 385 { 386 struct vpu_inst *inst = to_inst(file); 387 const struct vpu_core_resources *res; 388 struct venc_t *venc = inst->priv; 389 390 res = vpu_get_resource(inst); 391 if (!res) 392 return -EINVAL; 393 394 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT && s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 395 return -EINVAL; 396 if (s->target != V4L2_SEL_TGT_CROP) 397 return -EINVAL; 398 399 venc->params.crop.left = ALIGN(s->r.left, res->step_width); 400 venc->params.crop.top = ALIGN(s->r.top, res->step_height); 401 venc->params.crop.width = ALIGN(s->r.width, res->step_width); 402 venc->params.crop.height = ALIGN(s->r.height, res->step_height); 403 if (venc_valid_crop(venc, res)) { 404 venc->params.crop.left = 0; 405 venc->params.crop.top = 0; 406 venc->params.crop.width = venc->params.src_width; 407 venc->params.crop.height = venc->params.src_height; 408 } 409 410 inst->crop = venc->params.crop; 411 412 return 0; 413 } 414 415 static int venc_drain(struct vpu_inst *inst) 416 { 417 struct venc_t *venc = inst->priv; 418 int ret; 419 420 if (!inst->fh.m2m_ctx) 421 return 0; 422 423 if (inst->state != VPU_CODEC_STATE_DRAIN) 424 return 0; 425 426 if (v4l2_m2m_num_src_bufs_ready(inst->fh.m2m_ctx)) 427 return 0; 428 429 if (!venc->input_ready) 430 return 0; 431 432 venc->input_ready = false; 433 vpu_trace(inst->dev, "[%d]\n", inst->id); 434 ret = vpu_session_stop(inst); 435 if (ret) 436 return ret; 437 inst->state = VPU_CODEC_STATE_STOP; 438 wake_up_all(&venc->wq); 439 440 return 0; 441 } 442 443 static int venc_request_eos(struct vpu_inst *inst) 444 { 445 inst->state = VPU_CODEC_STATE_DRAIN; 446 venc_drain(inst); 447 448 return 0; 449 } 450 451 static int venc_encoder_cmd(struct file *file, void *fh, struct v4l2_encoder_cmd *cmd) 452 { 453 struct vpu_inst *inst = to_inst(file); 454 int ret; 455 456 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, fh, cmd); 457 if (ret) 458 return ret; 459 460 vpu_inst_lock(inst); 461 if (cmd->cmd == V4L2_ENC_CMD_STOP) { 462 if (inst->state == VPU_CODEC_STATE_DEINIT) 463 vpu_set_last_buffer_dequeued(inst); 464 else 465 venc_request_eos(inst); 466 } 467 vpu_inst_unlock(inst); 468 469 return 0; 470 } 471 472 static int venc_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub) 473 { 474 switch (sub->type) { 475 case V4L2_EVENT_EOS: 476 return v4l2_event_subscribe(fh, sub, 0, NULL); 477 case V4L2_EVENT_CTRL: 478 return v4l2_ctrl_subscribe_event(fh, sub); 479 default: 480 return -EINVAL; 481 } 482 } 483 484 static const struct v4l2_ioctl_ops venc_ioctl_ops = { 485 .vidioc_querycap = venc_querycap, 486 .vidioc_enum_fmt_vid_cap = venc_enum_fmt, 487 .vidioc_enum_fmt_vid_out = venc_enum_fmt, 488 .vidioc_enum_framesizes = venc_enum_framesizes, 489 .vidioc_enum_frameintervals = venc_enum_frameintervals, 490 .vidioc_g_fmt_vid_cap_mplane = venc_g_fmt, 491 .vidioc_g_fmt_vid_out_mplane = venc_g_fmt, 492 .vidioc_try_fmt_vid_cap_mplane = venc_try_fmt, 493 .vidioc_try_fmt_vid_out_mplane = venc_try_fmt, 494 .vidioc_s_fmt_vid_cap_mplane = venc_s_fmt, 495 .vidioc_s_fmt_vid_out_mplane = venc_s_fmt, 496 .vidioc_g_parm = venc_g_parm, 497 .vidioc_s_parm = venc_s_parm, 498 .vidioc_g_selection = venc_g_selection, 499 .vidioc_s_selection = venc_s_selection, 500 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 501 .vidioc_encoder_cmd = venc_encoder_cmd, 502 .vidioc_subscribe_event = venc_subscribe_event, 503 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 504 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 505 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 506 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 507 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 508 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 509 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 510 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 511 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 512 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 513 }; 514 515 static int venc_op_s_ctrl(struct v4l2_ctrl *ctrl) 516 { 517 struct vpu_inst *inst = ctrl_to_inst(ctrl); 518 struct venc_t *venc = inst->priv; 519 int ret = 0; 520 521 vpu_inst_lock(inst); 522 switch (ctrl->id) { 523 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: 524 venc->params.profile = ctrl->val; 525 break; 526 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: 527 venc->params.level = ctrl->val; 528 break; 529 case V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE: 530 venc->params.rc_enable = ctrl->val; 531 break; 532 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 533 venc->params.rc_mode = ctrl->val; 534 break; 535 case V4L2_CID_MPEG_VIDEO_BITRATE: 536 if (ctrl->val != venc->params.bitrate) 537 venc->bitrate_change = true; 538 venc->params.bitrate = ctrl->val; 539 break; 540 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: 541 venc->params.bitrate_max = ctrl->val; 542 break; 543 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: 544 venc->params.gop_length = ctrl->val; 545 break; 546 case V4L2_CID_MPEG_VIDEO_B_FRAMES: 547 venc->params.bframes = ctrl->val; 548 break; 549 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: 550 venc->params.i_frame_qp = ctrl->val; 551 break; 552 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: 553 venc->params.p_frame_qp = ctrl->val; 554 break; 555 case V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP: 556 venc->params.b_frame_qp = ctrl->val; 557 break; 558 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME: 559 venc->request_key_frame = 1; 560 break; 561 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: 562 venc->cpb_size = ctrl->val * 1024; 563 break; 564 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: 565 venc->params.sar.enable = ctrl->val; 566 break; 567 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: 568 venc->params.sar.idc = ctrl->val; 569 break; 570 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH: 571 venc->params.sar.width = ctrl->val; 572 break; 573 case V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT: 574 venc->params.sar.height = ctrl->val; 575 break; 576 case V4L2_CID_MPEG_VIDEO_HEADER_MODE: 577 break; 578 default: 579 ret = -EINVAL; 580 break; 581 } 582 vpu_inst_unlock(inst); 583 584 return ret; 585 } 586 587 static const struct v4l2_ctrl_ops venc_ctrl_ops = { 588 .s_ctrl = venc_op_s_ctrl, 589 .g_volatile_ctrl = vpu_helper_g_volatile_ctrl, 590 }; 591 592 static int venc_ctrl_init(struct vpu_inst *inst) 593 { 594 struct v4l2_ctrl *ctrl; 595 int ret; 596 597 ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20); 598 if (ret) 599 return ret; 600 601 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, 602 V4L2_CID_MPEG_VIDEO_H264_PROFILE, 603 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 604 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) | 605 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) | 606 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)), 607 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); 608 609 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, 610 V4L2_CID_MPEG_VIDEO_H264_LEVEL, 611 V4L2_MPEG_VIDEO_H264_LEVEL_5_1, 612 0x0, 613 V4L2_MPEG_VIDEO_H264_LEVEL_4_0); 614 615 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 616 V4L2_CID_MPEG_VIDEO_FRAME_RC_ENABLE, 0, 1, 1, 1); 617 618 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, 619 V4L2_CID_MPEG_VIDEO_BITRATE_MODE, 620 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 621 ~((1 << V4L2_MPEG_VIDEO_BITRATE_MODE_VBR) | 622 (1 << V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)), 623 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); 624 625 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 626 V4L2_CID_MPEG_VIDEO_BITRATE, 627 BITRATE_MIN, 628 BITRATE_MAX, 629 BITRATE_STEP, 630 BITRATE_DEFAULT); 631 632 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 633 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, 634 BITRATE_MIN, BITRATE_MAX, 635 BITRATE_STEP, 636 BITRATE_DEFAULT_PEAK); 637 638 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 639 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, (1 << 16) - 1, 1, 30); 640 641 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 642 V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 4, 1, 0); 643 644 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 645 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 1, 51, 1, 26); 646 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 647 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 1, 51, 1, 28); 648 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 649 V4L2_CID_MPEG_VIDEO_H264_B_FRAME_QP, 1, 51, 1, 30); 650 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 651 V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME, 0, 0, 0, 0); 652 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 653 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2); 654 if (ctrl) 655 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 656 ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 657 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2); 658 if (ctrl) 659 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; 660 661 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 662 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 64, 10240, 1, 1024); 663 664 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 665 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 0, 1, 1, 1); 666 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, 667 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC, 668 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_EXTENDED, 669 0x0, 670 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1); 671 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 672 V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_WIDTH, 673 0, USHRT_MAX, 1, 1); 674 v4l2_ctrl_new_std(&inst->ctrl_handler, &venc_ctrl_ops, 675 V4L2_CID_MPEG_VIDEO_H264_VUI_EXT_SAR_HEIGHT, 676 0, USHRT_MAX, 1, 1); 677 v4l2_ctrl_new_std_menu(&inst->ctrl_handler, &venc_ctrl_ops, 678 V4L2_CID_MPEG_VIDEO_HEADER_MODE, 679 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, 680 ~(1 << V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME), 681 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME); 682 683 ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler); 684 if (ret) { 685 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret); 686 v4l2_ctrl_handler_free(&inst->ctrl_handler); 687 return ret; 688 } 689 690 return 0; 691 } 692 693 static bool venc_check_ready(struct vpu_inst *inst, unsigned int type) 694 { 695 struct venc_t *venc = inst->priv; 696 697 if (V4L2_TYPE_IS_OUTPUT(type)) { 698 if (vpu_helper_get_free_space(inst) < venc->cpb_size) 699 return false; 700 return venc->input_ready; 701 } 702 703 if (list_empty(&venc->frames)) 704 return false; 705 return true; 706 } 707 708 static u32 venc_get_enable_mask(u32 type) 709 { 710 if (V4L2_TYPE_IS_OUTPUT(type)) 711 return VENC_OUTPUT_ENABLE; 712 else 713 return VENC_CAPTURE_ENABLE; 714 } 715 716 static void venc_set_enable(struct venc_t *venc, u32 type, int enable) 717 { 718 u32 mask = venc_get_enable_mask(type); 719 720 if (enable) 721 venc->enable |= mask; 722 else 723 venc->enable &= ~mask; 724 } 725 726 static u32 venc_get_enable(struct venc_t *venc, u32 type) 727 { 728 return venc->enable & venc_get_enable_mask(type); 729 } 730 731 static void venc_input_done(struct vpu_inst *inst) 732 { 733 struct venc_t *venc = inst->priv; 734 735 vpu_inst_lock(inst); 736 venc->input_ready = true; 737 vpu_process_output_buffer(inst); 738 if (inst->state == VPU_CODEC_STATE_DRAIN) 739 venc_drain(inst); 740 vpu_inst_unlock(inst); 741 } 742 743 /* 744 * It's hardware limitation, that there may be several bytes 745 * redundant data at the beginning of frame. 746 * For android platform, the redundant data may cause cts test fail 747 * So driver will strip them 748 */ 749 static int venc_precheck_encoded_frame(struct vpu_inst *inst, struct venc_frame_t *frame) 750 { 751 struct venc_t *venc; 752 int skipped; 753 754 if (!frame || !frame->bytesused) 755 return -EINVAL; 756 757 venc = inst->priv; 758 skipped = vpu_helper_find_startcode(&inst->stream_buffer, 759 inst->cap_format.pixfmt, 760 frame->info.wptr - inst->stream_buffer.phys, 761 frame->bytesused); 762 if (skipped > 0) { 763 frame->bytesused -= skipped; 764 frame->info.wptr = vpu_helper_step_walk(&inst->stream_buffer, 765 frame->info.wptr, skipped); 766 venc->skipped_bytes += skipped; 767 venc->skipped_count++; 768 } 769 770 return 0; 771 } 772 773 static int venc_get_one_encoded_frame(struct vpu_inst *inst, 774 struct venc_frame_t *frame, 775 struct vb2_v4l2_buffer *vbuf) 776 { 777 struct venc_t *venc = inst->priv; 778 779 if (!vbuf) 780 return -EAGAIN; 781 782 if (!venc_get_enable(inst->priv, vbuf->vb2_buf.type)) { 783 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 784 return 0; 785 } 786 if (frame->bytesused > vbuf->vb2_buf.planes[0].length) { 787 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 788 return -ENOMEM; 789 } 790 791 venc_precheck_encoded_frame(inst, frame); 792 793 if (frame->bytesused) { 794 u32 rptr = frame->info.wptr; 795 void *dst = vb2_plane_vaddr(&vbuf->vb2_buf, 0); 796 797 vpu_helper_copy_from_stream_buffer(&inst->stream_buffer, 798 &rptr, frame->bytesused, dst); 799 vpu_iface_update_stream_buffer(inst, rptr, 0); 800 } 801 vb2_set_plane_payload(&vbuf->vb2_buf, 0, frame->bytesused); 802 vbuf->sequence = frame->info.frame_id; 803 vbuf->vb2_buf.timestamp = frame->info.timestamp; 804 vbuf->field = inst->cap_format.field; 805 vbuf->flags |= frame->info.pic_type; 806 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE); 807 dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, frame->info.timestamp); 808 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 809 venc->ready_count++; 810 811 if (vbuf->flags & V4L2_BUF_FLAG_KEYFRAME) 812 dev_dbg(inst->dev, "[%d][%d]key frame\n", inst->id, frame->info.frame_id); 813 814 return 0; 815 } 816 817 static int venc_get_encoded_frames(struct vpu_inst *inst) 818 { 819 struct venc_t *venc; 820 struct venc_frame_t *frame; 821 struct venc_frame_t *tmp; 822 823 if (!inst->fh.m2m_ctx) 824 return 0; 825 venc = inst->priv; 826 list_for_each_entry_safe(frame, tmp, &venc->frames, list) { 827 if (venc_get_one_encoded_frame(inst, frame, 828 v4l2_m2m_dst_buf_remove(inst->fh.m2m_ctx))) 829 break; 830 list_del_init(&frame->list); 831 vfree(frame); 832 } 833 834 return 0; 835 } 836 837 static int venc_frame_encoded(struct vpu_inst *inst, void *arg) 838 { 839 struct vpu_enc_pic_info *info = arg; 840 struct venc_frame_t *frame; 841 struct venc_t *venc; 842 int ret = 0; 843 844 if (!info) 845 return -EINVAL; 846 venc = inst->priv; 847 frame = vzalloc(sizeof(*frame)); 848 if (!frame) 849 return -ENOMEM; 850 851 memcpy(&frame->info, info, sizeof(frame->info)); 852 frame->bytesused = info->frame_size; 853 854 vpu_inst_lock(inst); 855 list_add_tail(&frame->list, &venc->frames); 856 venc->encode_count++; 857 venc_get_encoded_frames(inst); 858 vpu_inst_unlock(inst); 859 860 return ret; 861 } 862 863 static void venc_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame) 864 { 865 struct vb2_v4l2_buffer *vbuf; 866 867 if (!inst->fh.m2m_ctx) 868 return; 869 870 vpu_inst_lock(inst); 871 if (!venc_get_enable(inst->priv, frame->type)) 872 goto exit; 873 vbuf = vpu_find_buf_by_sequence(inst, frame->type, frame->sequence); 874 if (!vbuf) { 875 dev_err(inst->dev, "[%d] can't find buf: type %d, sequence %d\n", 876 inst->id, frame->type, frame->sequence); 877 goto exit; 878 } 879 880 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE); 881 if (V4L2_TYPE_IS_OUTPUT(frame->type)) 882 v4l2_m2m_src_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf); 883 else 884 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf); 885 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE); 886 exit: 887 vpu_inst_unlock(inst); 888 } 889 890 static void venc_set_last_buffer_dequeued(struct vpu_inst *inst) 891 { 892 struct venc_t *venc = inst->priv; 893 894 if (venc->stopped && list_empty(&venc->frames)) 895 vpu_set_last_buffer_dequeued(inst); 896 } 897 898 static void venc_stop_done(struct vpu_inst *inst) 899 { 900 struct venc_t *venc = inst->priv; 901 902 vpu_inst_lock(inst); 903 venc->stopped = true; 904 venc_set_last_buffer_dequeued(inst); 905 vpu_inst_unlock(inst); 906 907 wake_up_all(&venc->wq); 908 } 909 910 static void venc_event_notify(struct vpu_inst *inst, u32 event, void *data) 911 { 912 } 913 914 static void venc_release(struct vpu_inst *inst) 915 { 916 } 917 918 static void venc_cleanup(struct vpu_inst *inst) 919 { 920 struct venc_t *venc; 921 922 if (!inst) 923 return; 924 925 venc = inst->priv; 926 if (venc) 927 vfree(venc); 928 inst->priv = NULL; 929 vfree(inst); 930 } 931 932 static int venc_start_session(struct vpu_inst *inst, u32 type) 933 { 934 struct venc_t *venc = inst->priv; 935 int stream_buffer_size; 936 int ret; 937 938 venc_set_enable(venc, type, 1); 939 if ((venc->enable & VENC_ENABLE_MASK) != VENC_ENABLE_MASK) 940 return 0; 941 942 vpu_iface_init_instance(inst); 943 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core); 944 if (stream_buffer_size > 0) { 945 inst->stream_buffer.length = max_t(u32, stream_buffer_size, venc->cpb_size * 3); 946 ret = vpu_alloc_dma(inst->core, &inst->stream_buffer); 947 if (ret) 948 goto error; 949 950 inst->use_stream_buffer = true; 951 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer); 952 } 953 954 ret = vpu_iface_set_encode_params(inst, &venc->params, 0); 955 if (ret) 956 goto error; 957 ret = vpu_session_configure_codec(inst); 958 if (ret) 959 goto error; 960 961 inst->state = VPU_CODEC_STATE_CONFIGURED; 962 /*vpu_iface_config_memory_resource*/ 963 964 /*config enc expert mode parameter*/ 965 ret = vpu_iface_set_encode_params(inst, &venc->params, 1); 966 if (ret) 967 goto error; 968 969 ret = vpu_session_start(inst); 970 if (ret) 971 goto error; 972 inst->state = VPU_CODEC_STATE_STARTED; 973 974 venc->bitrate_change = false; 975 venc->input_ready = true; 976 venc->frame_count = 0; 977 venc->encode_count = 0; 978 venc->ready_count = 0; 979 venc->stopped = false; 980 vpu_process_output_buffer(inst); 981 if (venc->frame_count == 0) 982 dev_err(inst->dev, "[%d] there is no input when starting\n", inst->id); 983 984 return 0; 985 error: 986 venc_set_enable(venc, type, 0); 987 inst->state = VPU_CODEC_STATE_DEINIT; 988 989 vpu_free_dma(&inst->stream_buffer); 990 return ret; 991 } 992 993 static void venc_cleanup_mem_resource(struct vpu_inst *inst) 994 { 995 struct venc_t *venc; 996 u32 i; 997 998 venc = inst->priv; 999 1000 for (i = 0; i < ARRAY_SIZE(venc->enc); i++) 1001 vpu_free_dma(&venc->enc[i]); 1002 for (i = 0; i < ARRAY_SIZE(venc->ref); i++) 1003 vpu_free_dma(&venc->ref[i]); 1004 } 1005 1006 static void venc_request_mem_resource(struct vpu_inst *inst, 1007 u32 enc_frame_size, 1008 u32 enc_frame_num, 1009 u32 ref_frame_size, 1010 u32 ref_frame_num, 1011 u32 act_frame_size, 1012 u32 act_frame_num) 1013 { 1014 struct venc_t *venc; 1015 u32 i; 1016 int ret; 1017 1018 venc = inst->priv; 1019 if (enc_frame_num > ARRAY_SIZE(venc->enc)) { 1020 dev_err(inst->dev, "[%d] enc num(%d) is out of range\n", inst->id, enc_frame_num); 1021 return; 1022 } 1023 if (ref_frame_num > ARRAY_SIZE(venc->ref)) { 1024 dev_err(inst->dev, "[%d] ref num(%d) is out of range\n", inst->id, ref_frame_num); 1025 return; 1026 } 1027 if (act_frame_num > ARRAY_SIZE(venc->act)) { 1028 dev_err(inst->dev, "[%d] act num(%d) is out of range\n", inst->id, act_frame_num); 1029 return; 1030 } 1031 1032 for (i = 0; i < enc_frame_num; i++) { 1033 venc->enc[i].length = enc_frame_size; 1034 ret = vpu_alloc_dma(inst->core, &venc->enc[i]); 1035 if (ret) { 1036 venc_cleanup_mem_resource(inst); 1037 return; 1038 } 1039 } 1040 for (i = 0; i < ref_frame_num; i++) { 1041 venc->ref[i].length = ref_frame_size; 1042 ret = vpu_alloc_dma(inst->core, &venc->ref[i]); 1043 if (ret) { 1044 venc_cleanup_mem_resource(inst); 1045 return; 1046 } 1047 } 1048 if (act_frame_num != 1 || act_frame_size > inst->act.length) { 1049 venc_cleanup_mem_resource(inst); 1050 return; 1051 } 1052 venc->act[0].length = act_frame_size; 1053 venc->act[0].phys = inst->act.phys; 1054 venc->act[0].virt = inst->act.virt; 1055 1056 for (i = 0; i < enc_frame_num; i++) 1057 vpu_iface_config_memory_resource(inst, MEM_RES_ENC, i, &venc->enc[i]); 1058 for (i = 0; i < ref_frame_num; i++) 1059 vpu_iface_config_memory_resource(inst, MEM_RES_REF, i, &venc->ref[i]); 1060 for (i = 0; i < act_frame_num; i++) 1061 vpu_iface_config_memory_resource(inst, MEM_RES_ACT, i, &venc->act[i]); 1062 } 1063 1064 static void venc_cleanup_frames(struct venc_t *venc) 1065 { 1066 struct venc_frame_t *frame; 1067 struct venc_frame_t *tmp; 1068 1069 list_for_each_entry_safe(frame, tmp, &venc->frames, list) { 1070 list_del_init(&frame->list); 1071 vfree(frame); 1072 } 1073 } 1074 1075 static int venc_stop_session(struct vpu_inst *inst, u32 type) 1076 { 1077 struct venc_t *venc = inst->priv; 1078 1079 venc_set_enable(venc, type, 0); 1080 if (venc->enable & VENC_ENABLE_MASK) 1081 return 0; 1082 1083 if (inst->state == VPU_CODEC_STATE_DEINIT) 1084 return 0; 1085 1086 if (inst->state != VPU_CODEC_STATE_STOP) 1087 venc_request_eos(inst); 1088 1089 call_void_vop(inst, wait_prepare); 1090 if (!wait_event_timeout(venc->wq, venc->stopped, VPU_TIMEOUT)) { 1091 set_bit(inst->id, &inst->core->hang_mask); 1092 vpu_session_debug(inst); 1093 } 1094 call_void_vop(inst, wait_finish); 1095 1096 inst->state = VPU_CODEC_STATE_DEINIT; 1097 venc_cleanup_frames(inst->priv); 1098 vpu_free_dma(&inst->stream_buffer); 1099 venc_cleanup_mem_resource(inst); 1100 1101 return 0; 1102 } 1103 1104 static int venc_process_output(struct vpu_inst *inst, struct vb2_buffer *vb) 1105 { 1106 struct venc_t *venc = inst->priv; 1107 struct vb2_v4l2_buffer *vbuf; 1108 u32 flags; 1109 1110 if (inst->state == VPU_CODEC_STATE_DEINIT) 1111 return -EINVAL; 1112 1113 vbuf = to_vb2_v4l2_buffer(vb); 1114 if (inst->state == VPU_CODEC_STATE_STARTED) 1115 inst->state = VPU_CODEC_STATE_ACTIVE; 1116 1117 flags = vbuf->flags; 1118 if (venc->request_key_frame) { 1119 vbuf->flags |= V4L2_BUF_FLAG_KEYFRAME; 1120 venc->request_key_frame = 0; 1121 } 1122 if (venc->bitrate_change) { 1123 vpu_session_update_parameters(inst, &venc->params); 1124 venc->bitrate_change = false; 1125 } 1126 dev_dbg(inst->dev, "[%d][INPUT TS]%32lld\n", inst->id, vb->timestamp); 1127 vpu_iface_input_frame(inst, vb); 1128 vbuf->flags = flags; 1129 venc->input_ready = false; 1130 venc->frame_count++; 1131 vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE); 1132 1133 return 0; 1134 } 1135 1136 static int venc_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb) 1137 { 1138 struct venc_t *venc; 1139 struct venc_frame_t *frame = NULL; 1140 struct vb2_v4l2_buffer *vbuf; 1141 int ret; 1142 1143 venc = inst->priv; 1144 if (list_empty(&venc->frames)) 1145 return -EINVAL; 1146 1147 frame = list_first_entry(&venc->frames, struct venc_frame_t, list); 1148 vbuf = to_vb2_v4l2_buffer(vb); 1149 v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf); 1150 ret = venc_get_one_encoded_frame(inst, frame, vbuf); 1151 if (ret) 1152 return ret; 1153 1154 list_del_init(&frame->list); 1155 vfree(frame); 1156 return 0; 1157 } 1158 1159 static void venc_on_queue_empty(struct vpu_inst *inst, u32 type) 1160 { 1161 struct venc_t *venc = inst->priv; 1162 1163 if (V4L2_TYPE_IS_OUTPUT(type)) 1164 return; 1165 1166 if (venc->stopped) 1167 venc_set_last_buffer_dequeued(inst); 1168 } 1169 1170 static int venc_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i) 1171 { 1172 struct venc_t *venc = inst->priv; 1173 int num = -1; 1174 1175 switch (i) { 1176 case 0: 1177 num = scnprintf(str, size, "profile = %d\n", venc->params.profile); 1178 break; 1179 case 1: 1180 num = scnprintf(str, size, "level = %d\n", venc->params.level); 1181 break; 1182 case 2: 1183 num = scnprintf(str, size, "fps = %d/%d\n", 1184 venc->params.frame_rate.numerator, 1185 venc->params.frame_rate.denominator); 1186 break; 1187 case 3: 1188 num = scnprintf(str, size, "%d x %d -> %d x %d\n", 1189 venc->params.src_width, 1190 venc->params.src_height, 1191 venc->params.out_width, 1192 venc->params.out_height); 1193 break; 1194 case 4: 1195 num = scnprintf(str, size, "(%d, %d) %d x %d\n", 1196 venc->params.crop.left, 1197 venc->params.crop.top, 1198 venc->params.crop.width, 1199 venc->params.crop.height); 1200 break; 1201 case 5: 1202 num = scnprintf(str, size, 1203 "enable = 0x%x, input = %d, encode = %d, ready = %d, stopped = %d\n", 1204 venc->enable, 1205 venc->frame_count, venc->encode_count, 1206 venc->ready_count, 1207 venc->stopped); 1208 break; 1209 case 6: 1210 num = scnprintf(str, size, "gop = %d\n", venc->params.gop_length); 1211 break; 1212 case 7: 1213 num = scnprintf(str, size, "bframes = %d\n", venc->params.bframes); 1214 break; 1215 case 8: 1216 num = scnprintf(str, size, "rc: %s, mode = %d, bitrate = %d(%d), qp = %d\n", 1217 venc->params.rc_enable ? "enable" : "disable", 1218 venc->params.rc_mode, 1219 venc->params.bitrate, 1220 venc->params.bitrate_max, 1221 venc->params.i_frame_qp); 1222 break; 1223 case 9: 1224 num = scnprintf(str, size, "sar: enable = %d, idc = %d, %d x %d\n", 1225 venc->params.sar.enable, 1226 venc->params.sar.idc, 1227 venc->params.sar.width, 1228 venc->params.sar.height); 1229 1230 break; 1231 case 10: 1232 num = scnprintf(str, size, 1233 "colorspace: primaries = %d, transfer = %d, matrix = %d, full_range = %d\n", 1234 venc->params.color.primaries, 1235 venc->params.color.transfer, 1236 venc->params.color.matrix, 1237 venc->params.color.full_range); 1238 break; 1239 case 11: 1240 num = scnprintf(str, size, "skipped: count = %d, bytes = %d\n", 1241 venc->skipped_count, venc->skipped_bytes); 1242 break; 1243 default: 1244 break; 1245 } 1246 1247 return num; 1248 } 1249 1250 static struct vpu_inst_ops venc_inst_ops = { 1251 .ctrl_init = venc_ctrl_init, 1252 .check_ready = venc_check_ready, 1253 .input_done = venc_input_done, 1254 .get_one_frame = venc_frame_encoded, 1255 .buf_done = venc_buf_done, 1256 .stop_done = venc_stop_done, 1257 .event_notify = venc_event_notify, 1258 .release = venc_release, 1259 .cleanup = venc_cleanup, 1260 .start = venc_start_session, 1261 .mem_request = venc_request_mem_resource, 1262 .stop = venc_stop_session, 1263 .process_output = venc_process_output, 1264 .process_capture = venc_process_capture, 1265 .on_queue_empty = venc_on_queue_empty, 1266 .get_debug_info = venc_get_debug_info, 1267 .wait_prepare = vpu_inst_unlock, 1268 .wait_finish = vpu_inst_lock, 1269 }; 1270 1271 static void venc_init(struct file *file) 1272 { 1273 struct vpu_inst *inst = to_inst(file); 1274 struct venc_t *venc; 1275 struct v4l2_format f; 1276 struct v4l2_streamparm parm; 1277 1278 venc = inst->priv; 1279 venc->params.qp_min = 1; 1280 venc->params.qp_max = 51; 1281 venc->params.qp_min_i = 1; 1282 venc->params.qp_max_i = 51; 1283 venc->params.bitrate_min = BITRATE_MIN; 1284 1285 memset(&f, 0, sizeof(f)); 1286 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1287 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M; 1288 f.fmt.pix_mp.width = 1280; 1289 f.fmt.pix_mp.height = 720; 1290 f.fmt.pix_mp.field = V4L2_FIELD_NONE; 1291 f.fmt.pix_mp.colorspace = V4L2_COLORSPACE_REC709; 1292 venc_s_fmt(file, &inst->fh, &f); 1293 1294 memset(&f, 0, sizeof(f)); 1295 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1296 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264; 1297 f.fmt.pix_mp.width = 1280; 1298 f.fmt.pix_mp.height = 720; 1299 f.fmt.pix_mp.field = V4L2_FIELD_NONE; 1300 venc_s_fmt(file, &inst->fh, &f); 1301 1302 memset(&parm, 0, sizeof(parm)); 1303 parm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1304 parm.parm.capture.timeperframe.numerator = 1; 1305 parm.parm.capture.timeperframe.denominator = 30; 1306 venc_s_parm(file, &inst->fh, &parm); 1307 } 1308 1309 static int venc_open(struct file *file) 1310 { 1311 struct vpu_inst *inst; 1312 struct venc_t *venc; 1313 int ret; 1314 1315 inst = vzalloc(sizeof(*inst)); 1316 if (!inst) 1317 return -ENOMEM; 1318 1319 venc = vzalloc(sizeof(*venc)); 1320 if (!venc) { 1321 vfree(inst); 1322 return -ENOMEM; 1323 } 1324 1325 inst->ops = &venc_inst_ops; 1326 inst->formats = venc_formats; 1327 inst->type = VPU_CORE_TYPE_ENC; 1328 inst->priv = venc; 1329 INIT_LIST_HEAD(&venc->frames); 1330 init_waitqueue_head(&venc->wq); 1331 1332 ret = vpu_v4l2_open(file, inst); 1333 if (ret) 1334 return ret; 1335 1336 venc_init(file); 1337 1338 return 0; 1339 } 1340 1341 static const struct v4l2_file_operations venc_fops = { 1342 .owner = THIS_MODULE, 1343 .open = venc_open, 1344 .release = vpu_v4l2_close, 1345 .unlocked_ioctl = video_ioctl2, 1346 .poll = v4l2_m2m_fop_poll, 1347 .mmap = v4l2_m2m_fop_mmap, 1348 }; 1349 1350 const struct v4l2_ioctl_ops *venc_get_ioctl_ops(void) 1351 { 1352 return &venc_ioctl_ops; 1353 } 1354 1355 const struct v4l2_file_operations *venc_get_fops(void) 1356 { 1357 return &venc_fops; 1358 } 1359