1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Hantro VPU codec driver 4 * 5 * Copyright (C) 2018 Collabora, Ltd. 6 * Copyright (C) 2018 Rockchip Electronics Co., Ltd. 7 * Alpha Lin <Alpha.Lin@rock-chips.com> 8 * Jeffy Chen <jeffy.chen@rock-chips.com> 9 * 10 * Copyright 2018 Google LLC. 11 * Tomasz Figa <tfiga@chromium.org> 12 * 13 * Based on s5p-mfc driver by Samsung Electronics Co., Ltd. 14 * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd. 15 */ 16 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/module.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/videodev2.h> 22 #include <linux/workqueue.h> 23 #include <media/v4l2-ctrls.h> 24 #include <media/v4l2-event.h> 25 #include <media/v4l2-mem2mem.h> 26 27 #include "hantro.h" 28 #include "hantro_hw.h" 29 #include "hantro_v4l2.h" 30 31 static int hantro_set_fmt_out(struct hantro_ctx *ctx, 32 struct v4l2_pix_format_mplane *pix_mp); 33 static int hantro_set_fmt_cap(struct hantro_ctx *ctx, 34 struct v4l2_pix_format_mplane *pix_mp); 35 36 static const struct hantro_fmt * 37 hantro_get_formats(const struct hantro_ctx *ctx, unsigned int *num_fmts) 38 { 39 const struct hantro_fmt *formats; 40 41 if (ctx->is_encoder) { 42 formats = ctx->dev->variant->enc_fmts; 43 *num_fmts = ctx->dev->variant->num_enc_fmts; 44 } else { 45 formats = ctx->dev->variant->dec_fmts; 46 *num_fmts = ctx->dev->variant->num_dec_fmts; 47 } 48 49 return formats; 50 } 51 52 static const struct hantro_fmt * 53 hantro_get_postproc_formats(const struct hantro_ctx *ctx, 54 unsigned int *num_fmts) 55 { 56 struct hantro_dev *vpu = ctx->dev; 57 58 if (ctx->is_encoder || !vpu->variant->postproc_fmts) { 59 *num_fmts = 0; 60 return NULL; 61 } 62 63 *num_fmts = ctx->dev->variant->num_postproc_fmts; 64 return ctx->dev->variant->postproc_fmts; 65 } 66 67 int hantro_get_format_depth(u32 fourcc) 68 { 69 switch (fourcc) { 70 case V4L2_PIX_FMT_P010: 71 case V4L2_PIX_FMT_P010_4L4: 72 return 10; 73 default: 74 return 8; 75 } 76 } 77 78 static bool 79 hantro_check_depth_match(const struct hantro_ctx *ctx, 80 const struct hantro_fmt *fmt) 81 { 82 int fmt_depth, ctx_depth = 8; 83 84 if (!fmt->match_depth && !fmt->postprocessed) 85 return true; 86 87 /* 0 means default depth, which is 8 */ 88 if (ctx->bit_depth) 89 ctx_depth = ctx->bit_depth; 90 91 fmt_depth = hantro_get_format_depth(fmt->fourcc); 92 93 /* 94 * Allow only downconversion for postproc formats for now. 95 * It may be possible to relax that on some HW. 96 */ 97 if (!fmt->match_depth) 98 return fmt_depth <= ctx_depth; 99 100 return fmt_depth == ctx_depth; 101 } 102 103 static const struct hantro_fmt * 104 hantro_find_format(const struct hantro_ctx *ctx, u32 fourcc) 105 { 106 const struct hantro_fmt *formats; 107 unsigned int i, num_fmts; 108 109 formats = hantro_get_formats(ctx, &num_fmts); 110 for (i = 0; i < num_fmts; i++) 111 if (formats[i].fourcc == fourcc) 112 return &formats[i]; 113 114 formats = hantro_get_postproc_formats(ctx, &num_fmts); 115 for (i = 0; i < num_fmts; i++) 116 if (formats[i].fourcc == fourcc) 117 return &formats[i]; 118 return NULL; 119 } 120 121 const struct hantro_fmt * 122 hantro_get_default_fmt(const struct hantro_ctx *ctx, bool bitstream) 123 { 124 const struct hantro_fmt *formats; 125 unsigned int i, num_fmts; 126 127 formats = hantro_get_formats(ctx, &num_fmts); 128 for (i = 0; i < num_fmts; i++) { 129 if (bitstream == (formats[i].codec_mode != 130 HANTRO_MODE_NONE) && 131 hantro_check_depth_match(ctx, &formats[i])) 132 return &formats[i]; 133 } 134 return NULL; 135 } 136 137 static int vidioc_querycap(struct file *file, void *priv, 138 struct v4l2_capability *cap) 139 { 140 struct hantro_dev *vpu = video_drvdata(file); 141 struct video_device *vdev = video_devdata(file); 142 143 strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver)); 144 strscpy(cap->card, vdev->name, sizeof(cap->card)); 145 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s", 146 vpu->dev->driver->name); 147 return 0; 148 } 149 150 static int vidioc_enum_framesizes(struct file *file, void *priv, 151 struct v4l2_frmsizeenum *fsize) 152 { 153 struct hantro_ctx *ctx = fh_to_ctx(priv); 154 const struct hantro_fmt *fmt; 155 156 fmt = hantro_find_format(ctx, fsize->pixel_format); 157 if (!fmt) { 158 vpu_debug(0, "unsupported bitstream format (%08x)\n", 159 fsize->pixel_format); 160 return -EINVAL; 161 } 162 163 /* For non-coded formats check if postprocessing scaling is possible */ 164 if (fmt->codec_mode == HANTRO_MODE_NONE) { 165 if (hantro_needs_postproc(ctx, fmt)) 166 return hanto_postproc_enum_framesizes(ctx, fsize); 167 else 168 return -ENOTTY; 169 } else if (fsize->index != 0) { 170 vpu_debug(0, "invalid frame size index (expected 0, got %d)\n", 171 fsize->index); 172 return -EINVAL; 173 } 174 175 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 176 fsize->stepwise = fmt->frmsize; 177 178 return 0; 179 } 180 181 static int vidioc_enum_fmt(struct file *file, void *priv, 182 struct v4l2_fmtdesc *f, bool capture) 183 184 { 185 struct hantro_ctx *ctx = fh_to_ctx(priv); 186 const struct hantro_fmt *fmt, *formats; 187 unsigned int num_fmts, i, j = 0; 188 bool skip_mode_none; 189 190 /* 191 * When dealing with an encoder: 192 * - on the capture side we want to filter out all MODE_NONE formats. 193 * - on the output side we want to filter out all formats that are 194 * not MODE_NONE. 195 * When dealing with a decoder: 196 * - on the capture side we want to filter out all formats that are 197 * not MODE_NONE. 198 * - on the output side we want to filter out all MODE_NONE formats. 199 */ 200 skip_mode_none = capture == ctx->is_encoder; 201 202 formats = hantro_get_formats(ctx, &num_fmts); 203 for (i = 0; i < num_fmts; i++) { 204 bool mode_none = formats[i].codec_mode == HANTRO_MODE_NONE; 205 fmt = &formats[i]; 206 207 if (skip_mode_none == mode_none) 208 continue; 209 if (!hantro_check_depth_match(ctx, fmt)) 210 continue; 211 if (j == f->index) { 212 f->pixelformat = fmt->fourcc; 213 return 0; 214 } 215 ++j; 216 } 217 218 /* 219 * Enumerate post-processed formats. As per the specification, 220 * we enumerated these formats after natively decoded formats 221 * as a hint for applications on what's the preferred fomat. 222 */ 223 if (!capture) 224 return -EINVAL; 225 formats = hantro_get_postproc_formats(ctx, &num_fmts); 226 for (i = 0; i < num_fmts; i++) { 227 fmt = &formats[i]; 228 229 if (!hantro_check_depth_match(ctx, fmt)) 230 continue; 231 if (j == f->index) { 232 f->pixelformat = fmt->fourcc; 233 return 0; 234 } 235 ++j; 236 } 237 238 return -EINVAL; 239 } 240 241 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 242 struct v4l2_fmtdesc *f) 243 { 244 return vidioc_enum_fmt(file, priv, f, true); 245 } 246 247 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, 248 struct v4l2_fmtdesc *f) 249 { 250 return vidioc_enum_fmt(file, priv, f, false); 251 } 252 253 static int vidioc_g_fmt_out_mplane(struct file *file, void *priv, 254 struct v4l2_format *f) 255 { 256 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 257 struct hantro_ctx *ctx = fh_to_ctx(priv); 258 259 vpu_debug(4, "f->type = %d\n", f->type); 260 261 *pix_mp = ctx->src_fmt; 262 263 return 0; 264 } 265 266 static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv, 267 struct v4l2_format *f) 268 { 269 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 270 struct hantro_ctx *ctx = fh_to_ctx(priv); 271 272 vpu_debug(4, "f->type = %d\n", f->type); 273 274 *pix_mp = ctx->dst_fmt; 275 276 return 0; 277 } 278 279 static int hantro_try_fmt(const struct hantro_ctx *ctx, 280 struct v4l2_pix_format_mplane *pix_mp, 281 enum v4l2_buf_type type) 282 { 283 const struct hantro_fmt *fmt, *vpu_fmt; 284 bool capture = V4L2_TYPE_IS_CAPTURE(type); 285 bool coded; 286 287 coded = capture == ctx->is_encoder; 288 289 vpu_debug(4, "trying format %c%c%c%c\n", 290 (pix_mp->pixelformat & 0x7f), 291 (pix_mp->pixelformat >> 8) & 0x7f, 292 (pix_mp->pixelformat >> 16) & 0x7f, 293 (pix_mp->pixelformat >> 24) & 0x7f); 294 295 fmt = hantro_find_format(ctx, pix_mp->pixelformat); 296 if (!fmt) { 297 fmt = hantro_get_default_fmt(ctx, coded); 298 pix_mp->pixelformat = fmt->fourcc; 299 } 300 301 if (coded) { 302 pix_mp->num_planes = 1; 303 vpu_fmt = fmt; 304 } else if (ctx->is_encoder) { 305 vpu_fmt = ctx->vpu_dst_fmt; 306 } else { 307 vpu_fmt = fmt; 308 /* 309 * Width/height on the CAPTURE end of a decoder are ignored and 310 * replaced by the OUTPUT ones. 311 */ 312 pix_mp->width = ctx->src_fmt.width; 313 pix_mp->height = ctx->src_fmt.height; 314 } 315 316 pix_mp->field = V4L2_FIELD_NONE; 317 318 v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height, 319 &vpu_fmt->frmsize); 320 321 if (!coded) { 322 /* Fill remaining fields */ 323 v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width, 324 pix_mp->height); 325 if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_H264_SLICE && 326 !hantro_needs_postproc(ctx, fmt)) 327 pix_mp->plane_fmt[0].sizeimage += 328 hantro_h264_mv_size(pix_mp->width, 329 pix_mp->height); 330 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_VP9_FRAME && 331 !hantro_needs_postproc(ctx, fmt)) 332 pix_mp->plane_fmt[0].sizeimage += 333 hantro_vp9_mv_size(pix_mp->width, 334 pix_mp->height); 335 else if (ctx->vpu_src_fmt->fourcc == V4L2_PIX_FMT_HEVC_SLICE && 336 !hantro_needs_postproc(ctx, fmt)) 337 pix_mp->plane_fmt[0].sizeimage += 338 hantro_hevc_mv_size(pix_mp->width, 339 pix_mp->height); 340 } else if (!pix_mp->plane_fmt[0].sizeimage) { 341 /* 342 * For coded formats the application can specify 343 * sizeimage. If the application passes a zero sizeimage, 344 * let's default to the maximum frame size. 345 */ 346 pix_mp->plane_fmt[0].sizeimage = fmt->header_size + 347 pix_mp->width * pix_mp->height * fmt->max_depth; 348 } 349 350 return 0; 351 } 352 353 static int vidioc_try_fmt_cap_mplane(struct file *file, void *priv, 354 struct v4l2_format *f) 355 { 356 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type); 357 } 358 359 static int vidioc_try_fmt_out_mplane(struct file *file, void *priv, 360 struct v4l2_format *f) 361 { 362 return hantro_try_fmt(fh_to_ctx(priv), &f->fmt.pix_mp, f->type); 363 } 364 365 static void 366 hantro_reset_fmt(struct v4l2_pix_format_mplane *fmt, 367 const struct hantro_fmt *vpu_fmt) 368 { 369 memset(fmt, 0, sizeof(*fmt)); 370 371 fmt->pixelformat = vpu_fmt->fourcc; 372 fmt->field = V4L2_FIELD_NONE; 373 fmt->colorspace = V4L2_COLORSPACE_JPEG; 374 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 375 fmt->quantization = V4L2_QUANTIZATION_DEFAULT; 376 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT; 377 } 378 379 static void 380 hantro_reset_encoded_fmt(struct hantro_ctx *ctx) 381 { 382 const struct hantro_fmt *vpu_fmt; 383 struct v4l2_pix_format_mplane *fmt; 384 385 vpu_fmt = hantro_get_default_fmt(ctx, true); 386 387 if (ctx->is_encoder) { 388 ctx->vpu_dst_fmt = vpu_fmt; 389 fmt = &ctx->dst_fmt; 390 } else { 391 ctx->vpu_src_fmt = vpu_fmt; 392 fmt = &ctx->src_fmt; 393 } 394 395 hantro_reset_fmt(fmt, vpu_fmt); 396 fmt->width = vpu_fmt->frmsize.min_width; 397 fmt->height = vpu_fmt->frmsize.min_height; 398 if (ctx->is_encoder) 399 hantro_set_fmt_cap(ctx, fmt); 400 else 401 hantro_set_fmt_out(ctx, fmt); 402 } 403 404 static void 405 hantro_reset_raw_fmt(struct hantro_ctx *ctx) 406 { 407 const struct hantro_fmt *raw_vpu_fmt; 408 struct v4l2_pix_format_mplane *raw_fmt, *encoded_fmt; 409 410 raw_vpu_fmt = hantro_get_default_fmt(ctx, false); 411 412 if (ctx->is_encoder) { 413 ctx->vpu_src_fmt = raw_vpu_fmt; 414 raw_fmt = &ctx->src_fmt; 415 encoded_fmt = &ctx->dst_fmt; 416 } else { 417 ctx->vpu_dst_fmt = raw_vpu_fmt; 418 raw_fmt = &ctx->dst_fmt; 419 encoded_fmt = &ctx->src_fmt; 420 } 421 422 hantro_reset_fmt(raw_fmt, raw_vpu_fmt); 423 raw_fmt->width = encoded_fmt->width; 424 raw_fmt->height = encoded_fmt->height; 425 if (ctx->is_encoder) 426 hantro_set_fmt_out(ctx, raw_fmt); 427 else 428 hantro_set_fmt_cap(ctx, raw_fmt); 429 } 430 431 void hantro_reset_fmts(struct hantro_ctx *ctx) 432 { 433 hantro_reset_encoded_fmt(ctx); 434 hantro_reset_raw_fmt(ctx); 435 } 436 437 static void 438 hantro_update_requires_request(struct hantro_ctx *ctx, u32 fourcc) 439 { 440 switch (fourcc) { 441 case V4L2_PIX_FMT_JPEG: 442 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = false; 443 break; 444 case V4L2_PIX_FMT_MPEG2_SLICE: 445 case V4L2_PIX_FMT_VP8_FRAME: 446 case V4L2_PIX_FMT_H264_SLICE: 447 case V4L2_PIX_FMT_HEVC_SLICE: 448 case V4L2_PIX_FMT_VP9_FRAME: 449 ctx->fh.m2m_ctx->out_q_ctx.q.requires_requests = true; 450 break; 451 default: 452 break; 453 } 454 } 455 456 static void 457 hantro_update_requires_hold_capture_buf(struct hantro_ctx *ctx, u32 fourcc) 458 { 459 struct vb2_queue *vq; 460 461 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 462 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 463 464 switch (fourcc) { 465 case V4L2_PIX_FMT_JPEG: 466 case V4L2_PIX_FMT_MPEG2_SLICE: 467 case V4L2_PIX_FMT_VP8_FRAME: 468 case V4L2_PIX_FMT_HEVC_SLICE: 469 case V4L2_PIX_FMT_VP9_FRAME: 470 vq->subsystem_flags &= ~(VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF); 471 break; 472 case V4L2_PIX_FMT_H264_SLICE: 473 vq->subsystem_flags |= VB2_V4L2_FL_SUPPORTS_M2M_HOLD_CAPTURE_BUF; 474 break; 475 default: 476 break; 477 } 478 } 479 480 static int hantro_set_fmt_out(struct hantro_ctx *ctx, 481 struct v4l2_pix_format_mplane *pix_mp) 482 { 483 struct vb2_queue *vq; 484 int ret; 485 486 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 487 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 488 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 489 if (ret) 490 return ret; 491 492 if (!ctx->is_encoder) { 493 struct vb2_queue *peer_vq; 494 495 /* 496 * In order to support dynamic resolution change, 497 * the decoder admits a resolution change, as long 498 * as the pixelformat remains. Can't be done if streaming. 499 */ 500 if (vb2_is_streaming(vq) || (vb2_is_busy(vq) && 501 pix_mp->pixelformat != ctx->src_fmt.pixelformat)) 502 return -EBUSY; 503 /* 504 * Since format change on the OUTPUT queue will reset 505 * the CAPTURE queue, we can't allow doing so 506 * when the CAPTURE queue has buffers allocated. 507 */ 508 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 509 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 510 if (vb2_is_busy(peer_vq)) 511 return -EBUSY; 512 } else { 513 /* 514 * The encoder doesn't admit a format change if 515 * there are OUTPUT buffers allocated. 516 */ 517 if (vb2_is_busy(vq)) 518 return -EBUSY; 519 } 520 521 ctx->vpu_src_fmt = hantro_find_format(ctx, pix_mp->pixelformat); 522 ctx->src_fmt = *pix_mp; 523 524 /* 525 * Current raw format might have become invalid with newly 526 * selected codec, so reset it to default just to be safe and 527 * keep internal driver state sane. User is mandated to set 528 * the raw format again after we return, so we don't need 529 * anything smarter. 530 * Note that hantro_reset_raw_fmt() also propagates size 531 * changes to the raw format. 532 */ 533 if (!ctx->is_encoder) 534 hantro_reset_raw_fmt(ctx); 535 536 /* Colorimetry information are always propagated. */ 537 ctx->dst_fmt.colorspace = pix_mp->colorspace; 538 ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc; 539 ctx->dst_fmt.xfer_func = pix_mp->xfer_func; 540 ctx->dst_fmt.quantization = pix_mp->quantization; 541 542 hantro_update_requires_request(ctx, pix_mp->pixelformat); 543 hantro_update_requires_hold_capture_buf(ctx, pix_mp->pixelformat); 544 545 vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode); 546 vpu_debug(0, "fmt - w: %d, h: %d\n", 547 pix_mp->width, pix_mp->height); 548 return 0; 549 } 550 551 static int hantro_set_fmt_cap(struct hantro_ctx *ctx, 552 struct v4l2_pix_format_mplane *pix_mp) 553 { 554 struct vb2_queue *vq; 555 int ret; 556 557 /* Change not allowed if queue is busy. */ 558 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 559 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 560 if (vb2_is_busy(vq)) 561 return -EBUSY; 562 563 if (ctx->is_encoder) { 564 struct vb2_queue *peer_vq; 565 566 /* 567 * Since format change on the CAPTURE queue will reset 568 * the OUTPUT queue, we can't allow doing so 569 * when the OUTPUT queue has buffers allocated. 570 */ 571 peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 572 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 573 if (vb2_is_busy(peer_vq) && 574 (pix_mp->pixelformat != ctx->dst_fmt.pixelformat || 575 pix_mp->height != ctx->dst_fmt.height || 576 pix_mp->width != ctx->dst_fmt.width)) 577 return -EBUSY; 578 } 579 580 ret = hantro_try_fmt(ctx, pix_mp, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 581 if (ret) 582 return ret; 583 584 ctx->vpu_dst_fmt = hantro_find_format(ctx, pix_mp->pixelformat); 585 ctx->dst_fmt = *pix_mp; 586 587 /* 588 * Current raw format might have become invalid with newly 589 * selected codec, so reset it to default just to be safe and 590 * keep internal driver state sane. User is mandated to set 591 * the raw format again after we return, so we don't need 592 * anything smarter. 593 * Note that hantro_reset_raw_fmt() also propagates size 594 * changes to the raw format. 595 */ 596 if (ctx->is_encoder) 597 hantro_reset_raw_fmt(ctx); 598 599 /* Colorimetry information are always propagated. */ 600 ctx->src_fmt.colorspace = pix_mp->colorspace; 601 ctx->src_fmt.ycbcr_enc = pix_mp->ycbcr_enc; 602 ctx->src_fmt.xfer_func = pix_mp->xfer_func; 603 ctx->src_fmt.quantization = pix_mp->quantization; 604 605 vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode); 606 vpu_debug(0, "fmt - w: %d, h: %d\n", 607 pix_mp->width, pix_mp->height); 608 609 hantro_update_requires_request(ctx, pix_mp->pixelformat); 610 611 return 0; 612 } 613 614 static int 615 vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f) 616 { 617 return hantro_set_fmt_out(fh_to_ctx(priv), &f->fmt.pix_mp); 618 } 619 620 static int 621 vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f) 622 { 623 return hantro_set_fmt_cap(fh_to_ctx(priv), &f->fmt.pix_mp); 624 } 625 626 static int vidioc_g_selection(struct file *file, void *priv, 627 struct v4l2_selection *sel) 628 { 629 struct hantro_ctx *ctx = fh_to_ctx(priv); 630 631 /* Crop only supported on source. */ 632 if (!ctx->is_encoder || 633 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 634 return -EINVAL; 635 636 switch (sel->target) { 637 case V4L2_SEL_TGT_CROP_DEFAULT: 638 case V4L2_SEL_TGT_CROP_BOUNDS: 639 sel->r.top = 0; 640 sel->r.left = 0; 641 sel->r.width = ctx->src_fmt.width; 642 sel->r.height = ctx->src_fmt.height; 643 break; 644 case V4L2_SEL_TGT_CROP: 645 sel->r.top = 0; 646 sel->r.left = 0; 647 sel->r.width = ctx->dst_fmt.width; 648 sel->r.height = ctx->dst_fmt.height; 649 break; 650 default: 651 return -EINVAL; 652 } 653 654 return 0; 655 } 656 657 static int vidioc_s_selection(struct file *file, void *priv, 658 struct v4l2_selection *sel) 659 { 660 struct hantro_ctx *ctx = fh_to_ctx(priv); 661 struct v4l2_rect *rect = &sel->r; 662 struct vb2_queue *vq; 663 664 /* Crop only supported on source. */ 665 if (!ctx->is_encoder || 666 sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 667 return -EINVAL; 668 669 /* Change not allowed if the queue is streaming. */ 670 vq = v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx); 671 if (vb2_is_streaming(vq)) 672 return -EBUSY; 673 674 if (sel->target != V4L2_SEL_TGT_CROP) 675 return -EINVAL; 676 677 /* 678 * We do not support offsets, and we can crop only inside 679 * right-most or bottom-most macroblocks. 680 */ 681 if (rect->left != 0 || rect->top != 0 || 682 round_up(rect->width, MB_DIM) != ctx->src_fmt.width || 683 round_up(rect->height, MB_DIM) != ctx->src_fmt.height) { 684 /* Default to full frame for incorrect settings. */ 685 rect->left = 0; 686 rect->top = 0; 687 rect->width = ctx->src_fmt.width; 688 rect->height = ctx->src_fmt.height; 689 } else { 690 /* We support widths aligned to 4 pixels and arbitrary heights. */ 691 rect->width = round_up(rect->width, 4); 692 } 693 694 ctx->dst_fmt.width = rect->width; 695 ctx->dst_fmt.height = rect->height; 696 697 return 0; 698 } 699 700 static const struct v4l2_event hantro_eos_event = { 701 .type = V4L2_EVENT_EOS 702 }; 703 704 static int vidioc_encoder_cmd(struct file *file, void *priv, 705 struct v4l2_encoder_cmd *ec) 706 { 707 struct hantro_ctx *ctx = fh_to_ctx(priv); 708 int ret; 709 710 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec); 711 if (ret < 0) 712 return ret; 713 714 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) || 715 !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx))) 716 return 0; 717 718 ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec); 719 if (ret < 0) 720 return ret; 721 722 if (ec->cmd == V4L2_ENC_CMD_STOP && 723 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 724 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event); 725 726 if (ec->cmd == V4L2_ENC_CMD_START) 727 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q); 728 729 return 0; 730 } 731 732 const struct v4l2_ioctl_ops hantro_ioctl_ops = { 733 .vidioc_querycap = vidioc_querycap, 734 .vidioc_enum_framesizes = vidioc_enum_framesizes, 735 736 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane, 737 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane, 738 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane, 739 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane, 740 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane, 741 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane, 742 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, 743 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 744 745 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 746 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 747 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 748 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 749 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 750 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 751 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 752 753 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 754 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 755 756 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 757 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 758 759 .vidioc_g_selection = vidioc_g_selection, 760 .vidioc_s_selection = vidioc_s_selection, 761 762 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 763 .vidioc_encoder_cmd = vidioc_encoder_cmd, 764 }; 765 766 static int 767 hantro_queue_setup(struct vb2_queue *vq, unsigned int *num_buffers, 768 unsigned int *num_planes, unsigned int sizes[], 769 struct device *alloc_devs[]) 770 { 771 struct hantro_ctx *ctx = vb2_get_drv_priv(vq); 772 struct v4l2_pix_format_mplane *pixfmt; 773 int i; 774 775 switch (vq->type) { 776 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 777 pixfmt = &ctx->dst_fmt; 778 break; 779 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 780 pixfmt = &ctx->src_fmt; 781 break; 782 default: 783 vpu_err("invalid queue type: %d\n", vq->type); 784 return -EINVAL; 785 } 786 787 if (*num_planes) { 788 if (*num_planes != pixfmt->num_planes) 789 return -EINVAL; 790 for (i = 0; i < pixfmt->num_planes; ++i) 791 if (sizes[i] < pixfmt->plane_fmt[i].sizeimage) 792 return -EINVAL; 793 return 0; 794 } 795 796 *num_planes = pixfmt->num_planes; 797 for (i = 0; i < pixfmt->num_planes; ++i) 798 sizes[i] = pixfmt->plane_fmt[i].sizeimage; 799 return 0; 800 } 801 802 static int 803 hantro_buf_plane_check(struct vb2_buffer *vb, 804 struct v4l2_pix_format_mplane *pixfmt) 805 { 806 unsigned int sz; 807 int i; 808 809 for (i = 0; i < pixfmt->num_planes; ++i) { 810 sz = pixfmt->plane_fmt[i].sizeimage; 811 vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n", 812 i, vb2_plane_size(vb, i), sz); 813 if (vb2_plane_size(vb, i) < sz) { 814 vpu_err("plane %d is too small for output\n", i); 815 return -EINVAL; 816 } 817 } 818 return 0; 819 } 820 821 static int hantro_buf_prepare(struct vb2_buffer *vb) 822 { 823 struct vb2_queue *vq = vb->vb2_queue; 824 struct hantro_ctx *ctx = vb2_get_drv_priv(vq); 825 struct v4l2_pix_format_mplane *pix_fmt; 826 int ret; 827 828 if (V4L2_TYPE_IS_OUTPUT(vq->type)) 829 pix_fmt = &ctx->src_fmt; 830 else 831 pix_fmt = &ctx->dst_fmt; 832 ret = hantro_buf_plane_check(vb, pix_fmt); 833 if (ret) 834 return ret; 835 /* 836 * Buffer's bytesused must be written by driver for CAPTURE buffers. 837 * (for OUTPUT buffers, if userspace passes 0 bytesused, v4l2-core sets 838 * it to buffer length). 839 */ 840 if (V4L2_TYPE_IS_CAPTURE(vq->type)) { 841 if (ctx->is_encoder) 842 vb2_set_plane_payload(vb, 0, 0); 843 else 844 vb2_set_plane_payload(vb, 0, pix_fmt->plane_fmt[0].sizeimage); 845 } 846 847 return 0; 848 } 849 850 static void hantro_buf_queue(struct vb2_buffer *vb) 851 { 852 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 853 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 854 855 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && 856 vb2_is_streaming(vb->vb2_queue) && 857 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) { 858 unsigned int i; 859 860 for (i = 0; i < vb->num_planes; i++) 861 vb2_set_plane_payload(vb, i, 0); 862 863 vbuf->field = V4L2_FIELD_NONE; 864 vbuf->sequence = ctx->sequence_cap++; 865 866 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf); 867 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event); 868 return; 869 } 870 871 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 872 } 873 874 static bool hantro_vq_is_coded(struct vb2_queue *q) 875 { 876 struct hantro_ctx *ctx = vb2_get_drv_priv(q); 877 878 return ctx->is_encoder != V4L2_TYPE_IS_OUTPUT(q->type); 879 } 880 881 static int hantro_start_streaming(struct vb2_queue *q, unsigned int count) 882 { 883 struct hantro_ctx *ctx = vb2_get_drv_priv(q); 884 int ret = 0; 885 886 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q); 887 888 if (V4L2_TYPE_IS_OUTPUT(q->type)) 889 ctx->sequence_out = 0; 890 else 891 ctx->sequence_cap = 0; 892 893 if (hantro_vq_is_coded(q)) { 894 enum hantro_codec_mode codec_mode; 895 896 if (V4L2_TYPE_IS_OUTPUT(q->type)) 897 codec_mode = ctx->vpu_src_fmt->codec_mode; 898 else 899 codec_mode = ctx->vpu_dst_fmt->codec_mode; 900 901 vpu_debug(4, "Codec mode = %d\n", codec_mode); 902 ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode]; 903 if (ctx->codec_ops->init) { 904 ret = ctx->codec_ops->init(ctx); 905 if (ret) 906 return ret; 907 } 908 909 if (hantro_needs_postproc(ctx, ctx->vpu_dst_fmt)) { 910 ret = hantro_postproc_alloc(ctx); 911 if (ret) 912 goto err_codec_exit; 913 } 914 } 915 return ret; 916 917 err_codec_exit: 918 if (ctx->codec_ops->exit) 919 ctx->codec_ops->exit(ctx); 920 return ret; 921 } 922 923 static void 924 hantro_return_bufs(struct vb2_queue *q, 925 struct vb2_v4l2_buffer *(*buf_remove)(struct v4l2_m2m_ctx *)) 926 { 927 struct hantro_ctx *ctx = vb2_get_drv_priv(q); 928 929 for (;;) { 930 struct vb2_v4l2_buffer *vbuf; 931 932 vbuf = buf_remove(ctx->fh.m2m_ctx); 933 if (!vbuf) 934 break; 935 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, 936 &ctx->ctrl_handler); 937 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 938 } 939 } 940 941 static void hantro_stop_streaming(struct vb2_queue *q) 942 { 943 struct hantro_ctx *ctx = vb2_get_drv_priv(q); 944 945 if (hantro_vq_is_coded(q)) { 946 hantro_postproc_free(ctx); 947 if (ctx->codec_ops && ctx->codec_ops->exit) 948 ctx->codec_ops->exit(ctx); 949 } 950 951 /* 952 * The mem2mem framework calls v4l2_m2m_cancel_job before 953 * .stop_streaming, so there isn't any job running and 954 * it is safe to return all the buffers. 955 */ 956 if (V4L2_TYPE_IS_OUTPUT(q->type)) 957 hantro_return_bufs(q, v4l2_m2m_src_buf_remove); 958 else 959 hantro_return_bufs(q, v4l2_m2m_dst_buf_remove); 960 961 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); 962 963 if (V4L2_TYPE_IS_OUTPUT(q->type) && 964 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 965 v4l2_event_queue_fh(&ctx->fh, &hantro_eos_event); 966 } 967 968 static void hantro_buf_request_complete(struct vb2_buffer *vb) 969 { 970 struct hantro_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 971 972 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler); 973 } 974 975 static int hantro_buf_out_validate(struct vb2_buffer *vb) 976 { 977 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 978 979 vbuf->field = V4L2_FIELD_NONE; 980 return 0; 981 } 982 983 const struct vb2_ops hantro_queue_ops = { 984 .queue_setup = hantro_queue_setup, 985 .buf_prepare = hantro_buf_prepare, 986 .buf_queue = hantro_buf_queue, 987 .buf_out_validate = hantro_buf_out_validate, 988 .buf_request_complete = hantro_buf_request_complete, 989 .start_streaming = hantro_start_streaming, 990 .stop_streaming = hantro_stop_streaming, 991 .wait_prepare = vb2_ops_wait_prepare, 992 .wait_finish = vb2_ops_wait_finish, 993 }; 994