1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * A virtual codec example device. 4 * 5 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 6 * 7 * This is a virtual codec device driver for testing the codec framework. 8 * It simulates a device that uses memory buffers for both source and 9 * destination and encodes or decodes the data. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/delay.h> 14 #include <linux/fs.h> 15 #include <linux/sched.h> 16 #include <linux/slab.h> 17 18 #include <linux/platform_device.h> 19 #include <media/v4l2-mem2mem.h> 20 #include <media/v4l2-device.h> 21 #include <media/v4l2-ioctl.h> 22 #include <media/v4l2-ctrls.h> 23 #include <media/v4l2-event.h> 24 #include <media/videobuf2-vmalloc.h> 25 26 #include "codec-v4l2-fwht.h" 27 28 MODULE_DESCRIPTION("Virtual codec device"); 29 MODULE_AUTHOR("Hans Verkuil <hverkuil@kernel.org>"); 30 MODULE_LICENSE("GPL v2"); 31 32 static bool multiplanar; 33 module_param(multiplanar, bool, 0444); 34 MODULE_PARM_DESC(multiplanar, 35 " use multi-planar API instead of single-planar API"); 36 37 static unsigned int debug; 38 module_param(debug, uint, 0644); 39 MODULE_PARM_DESC(debug, " activates debug info"); 40 41 #define VICODEC_NAME "vicodec" 42 #define MAX_WIDTH 4096U 43 #define MIN_WIDTH 640U 44 #define MAX_HEIGHT 2160U 45 #define MIN_HEIGHT 360U 46 /* Recommended number of buffers for the stateful codecs */ 47 #define VICODEC_REC_BUFS 2 48 49 #define dprintk(dev, fmt, arg...) \ 50 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg) 51 52 53 struct pixfmt_info { 54 u32 id; 55 unsigned int bytesperline_mult; 56 unsigned int sizeimage_mult; 57 unsigned int sizeimage_div; 58 unsigned int luma_step; 59 unsigned int chroma_step; 60 /* Chroma plane subsampling */ 61 unsigned int width_div; 62 unsigned int height_div; 63 }; 64 65 static const struct v4l2_fwht_pixfmt_info pixfmt_fwht = { 66 V4L2_PIX_FMT_FWHT, 0, 4, 1, 1, 1, 1, 1, 0, 1 67 }; 68 69 static const struct v4l2_fwht_pixfmt_info pixfmt_stateless_fwht = { 70 V4L2_PIX_FMT_FWHT_STATELESS, 0, 4, 1, 1, 1, 1, 1, 0, 1 71 }; 72 73 static void vicodec_dev_release(struct device *dev) 74 { 75 } 76 77 static struct platform_device vicodec_pdev = { 78 .name = VICODEC_NAME, 79 .dev.release = vicodec_dev_release, 80 }; 81 82 /* Per-queue, driver-specific private data */ 83 struct vicodec_q_data { 84 unsigned int coded_width; 85 unsigned int coded_height; 86 unsigned int visible_width; 87 unsigned int visible_height; 88 unsigned int sizeimage; 89 unsigned int vb2_sizeimage; 90 unsigned int sequence; 91 const struct v4l2_fwht_pixfmt_info *info; 92 }; 93 94 enum { 95 V4L2_M2M_SRC = 0, 96 V4L2_M2M_DST = 1, 97 }; 98 99 struct vicodec_dev_instance { 100 struct video_device vfd; 101 struct mutex mutex; 102 spinlock_t lock; 103 struct v4l2_m2m_dev *m2m_dev; 104 }; 105 106 struct vicodec_dev { 107 struct v4l2_device v4l2_dev; 108 struct vicodec_dev_instance stateful_enc; 109 struct vicodec_dev_instance stateful_dec; 110 struct vicodec_dev_instance stateless_dec; 111 #ifdef CONFIG_MEDIA_CONTROLLER 112 struct media_device mdev; 113 #endif 114 115 }; 116 117 struct vicodec_ctx { 118 struct v4l2_fh fh; 119 struct vicodec_dev *dev; 120 bool is_enc; 121 bool is_stateless; 122 spinlock_t *lock; 123 124 struct v4l2_ctrl_handler hdl; 125 126 /* Source and destination queue data */ 127 struct vicodec_q_data q_data[2]; 128 struct v4l2_fwht_state state; 129 130 u32 cur_buf_offset; 131 u32 comp_max_size; 132 u32 comp_size; 133 u32 header_size; 134 u32 comp_magic_cnt; 135 bool comp_has_frame; 136 bool comp_has_next_frame; 137 bool first_source_change_sent; 138 bool source_changed; 139 }; 140 141 static const struct v4l2_event vicodec_eos_event = { 142 .type = V4L2_EVENT_EOS 143 }; 144 145 static inline struct vicodec_ctx *file2ctx(struct file *file) 146 { 147 return container_of(file_to_v4l2_fh(file), struct vicodec_ctx, fh); 148 } 149 150 static struct vicodec_q_data *get_q_data(struct vicodec_ctx *ctx, 151 enum v4l2_buf_type type) 152 { 153 switch (type) { 154 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 155 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 156 return &ctx->q_data[V4L2_M2M_SRC]; 157 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 158 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 159 return &ctx->q_data[V4L2_M2M_DST]; 160 default: 161 break; 162 } 163 return NULL; 164 } 165 166 static void copy_cap_to_ref(const u8 *cap, const struct v4l2_fwht_pixfmt_info *info, 167 struct v4l2_fwht_state *state) 168 { 169 int plane_idx; 170 u8 *p_ref = state->ref_frame.buf; 171 unsigned int cap_stride = state->stride; 172 unsigned int ref_stride = state->ref_stride; 173 174 for (plane_idx = 0; plane_idx < info->planes_num; plane_idx++) { 175 int i; 176 unsigned int h_div = (plane_idx == 1 || plane_idx == 2) ? 177 info->height_div : 1; 178 const u8 *row_cap = cap; 179 u8 *row_ref = p_ref; 180 181 if (info->planes_num == 3 && plane_idx == 1) { 182 cap_stride /= 2; 183 ref_stride /= 2; 184 } 185 186 if (plane_idx == 1 && 187 (info->id == V4L2_PIX_FMT_NV24 || 188 info->id == V4L2_PIX_FMT_NV42)) { 189 cap_stride *= 2; 190 ref_stride *= 2; 191 } 192 193 for (i = 0; i < state->visible_height / h_div; i++) { 194 memcpy(row_ref, row_cap, ref_stride); 195 row_ref += ref_stride; 196 row_cap += cap_stride; 197 } 198 cap += cap_stride * (state->coded_height / h_div); 199 p_ref += ref_stride * (state->coded_height / h_div); 200 } 201 } 202 203 static bool validate_by_version(unsigned int flags, unsigned int version) 204 { 205 if (!version || version > V4L2_FWHT_VERSION) 206 return false; 207 208 if (version >= 2) { 209 unsigned int components_num = 1 + 210 ((flags & V4L2_FWHT_FL_COMPONENTS_NUM_MSK) >> 211 V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET); 212 unsigned int pixenc = flags & V4L2_FWHT_FL_PIXENC_MSK; 213 214 if (components_num == 0 || components_num > 4 || !pixenc) 215 return false; 216 } 217 return true; 218 } 219 220 static bool validate_stateless_params_flags(const struct v4l2_ctrl_fwht_params *params, 221 const struct v4l2_fwht_pixfmt_info *cur_info) 222 { 223 unsigned int width_div = 224 (params->flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2; 225 unsigned int height_div = 226 (params->flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2; 227 unsigned int components_num = 3; 228 unsigned int pixenc = 0; 229 230 if (params->version < 3) 231 return false; 232 233 components_num = 1 + ((params->flags & V4L2_FWHT_FL_COMPONENTS_NUM_MSK) >> 234 V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET); 235 pixenc = (params->flags & V4L2_FWHT_FL_PIXENC_MSK); 236 if (v4l2_fwht_validate_fmt(cur_info, width_div, height_div, 237 components_num, pixenc)) 238 return true; 239 return false; 240 } 241 242 243 static void update_state_from_header(struct vicodec_ctx *ctx) 244 { 245 const struct fwht_cframe_hdr *p_hdr = &ctx->state.header; 246 247 ctx->state.visible_width = ntohl(p_hdr->width); 248 ctx->state.visible_height = ntohl(p_hdr->height); 249 ctx->state.colorspace = ntohl(p_hdr->colorspace); 250 ctx->state.xfer_func = ntohl(p_hdr->xfer_func); 251 ctx->state.ycbcr_enc = ntohl(p_hdr->ycbcr_enc); 252 ctx->state.quantization = ntohl(p_hdr->quantization); 253 } 254 255 static int device_process(struct vicodec_ctx *ctx, 256 struct vb2_v4l2_buffer *src_vb, 257 struct vb2_v4l2_buffer *dst_vb) 258 { 259 struct vicodec_dev *dev = ctx->dev; 260 struct v4l2_fwht_state *state = &ctx->state; 261 u8 *p_src, *p_dst; 262 int ret = 0; 263 264 if (ctx->is_enc || ctx->is_stateless) 265 p_src = vb2_plane_vaddr(&src_vb->vb2_buf, 0); 266 else 267 p_src = state->compressed_frame; 268 269 if (ctx->is_stateless) { 270 struct media_request *src_req = src_vb->vb2_buf.req_obj.req; 271 272 ret = v4l2_ctrl_request_setup(src_req, &ctx->hdl); 273 if (ret) 274 return ret; 275 update_state_from_header(ctx); 276 277 ctx->state.header.size = 278 htonl(vb2_get_plane_payload(&src_vb->vb2_buf, 0)); 279 /* 280 * set the reference buffer from the reference timestamp 281 * only if this is a P-frame 282 */ 283 if (!(ntohl(ctx->state.header.flags) & V4L2_FWHT_FL_I_FRAME)) { 284 struct vb2_buffer *ref_vb2_buf; 285 struct vb2_queue *vq_cap = 286 v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 287 V4L2_BUF_TYPE_VIDEO_CAPTURE); 288 289 ref_vb2_buf = vb2_find_buffer(vq_cap, ctx->state.ref_frame_ts); 290 if (!ref_vb2_buf) 291 return -EINVAL; 292 if (ref_vb2_buf->state == VB2_BUF_STATE_ERROR) 293 ret = -EINVAL; 294 ctx->state.ref_frame.buf = 295 vb2_plane_vaddr(ref_vb2_buf, 0); 296 } else { 297 ctx->state.ref_frame.buf = NULL; 298 } 299 } 300 p_dst = vb2_plane_vaddr(&dst_vb->vb2_buf, 0); 301 if (!p_src || !p_dst) { 302 v4l2_err(&dev->v4l2_dev, 303 "Acquiring kernel pointers to buffers failed\n"); 304 return -EFAULT; 305 } 306 307 if (ctx->is_enc) { 308 struct vicodec_q_data *q_src; 309 int comp_sz_or_errcode; 310 311 q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 312 state->info = q_src->info; 313 comp_sz_or_errcode = v4l2_fwht_encode(state, p_src, p_dst); 314 if (comp_sz_or_errcode < 0) 315 return comp_sz_or_errcode; 316 vb2_set_plane_payload(&dst_vb->vb2_buf, 0, comp_sz_or_errcode); 317 } else { 318 struct vicodec_q_data *q_dst; 319 unsigned int comp_frame_size = ntohl(ctx->state.header.size); 320 321 q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 322 if (comp_frame_size > ctx->comp_max_size) 323 return -EINVAL; 324 state->info = q_dst->info; 325 ret = v4l2_fwht_decode(state, p_src, p_dst); 326 if (ret < 0) 327 return ret; 328 if (!ctx->is_stateless) 329 copy_cap_to_ref(p_dst, ctx->state.info, &ctx->state); 330 331 vb2_set_plane_payload(&dst_vb->vb2_buf, 0, q_dst->sizeimage); 332 if (ntohl(ctx->state.header.flags) & V4L2_FWHT_FL_I_FRAME) 333 dst_vb->flags |= V4L2_BUF_FLAG_KEYFRAME; 334 else 335 dst_vb->flags |= V4L2_BUF_FLAG_PFRAME; 336 } 337 return ret; 338 } 339 340 /* 341 * mem2mem callbacks 342 */ 343 static enum vb2_buffer_state get_next_header(struct vicodec_ctx *ctx, 344 u8 **pp, u32 sz) 345 { 346 static const u8 magic[] = { 347 0x4f, 0x4f, 0x4f, 0x4f, 0xff, 0xff, 0xff, 0xff 348 }; 349 u8 *p = *pp; 350 u32 state; 351 u8 *header = (u8 *)&ctx->state.header; 352 353 state = VB2_BUF_STATE_DONE; 354 355 if (!ctx->header_size) { 356 state = VB2_BUF_STATE_ERROR; 357 for (; p < *pp + sz; p++) { 358 u32 copy; 359 360 p = memchr(p, magic[ctx->comp_magic_cnt], 361 *pp + sz - p); 362 if (!p) { 363 ctx->comp_magic_cnt = 0; 364 p = *pp + sz; 365 break; 366 } 367 copy = sizeof(magic) - ctx->comp_magic_cnt; 368 if (*pp + sz - p < copy) 369 copy = *pp + sz - p; 370 371 memcpy(header + ctx->comp_magic_cnt, p, copy); 372 ctx->comp_magic_cnt += copy; 373 if (!memcmp(header, magic, ctx->comp_magic_cnt)) { 374 p += copy; 375 state = VB2_BUF_STATE_DONE; 376 break; 377 } 378 ctx->comp_magic_cnt = 0; 379 } 380 if (ctx->comp_magic_cnt < sizeof(magic)) { 381 *pp = p; 382 return state; 383 } 384 ctx->header_size = sizeof(magic); 385 } 386 387 if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) { 388 u32 copy = sizeof(struct fwht_cframe_hdr) - ctx->header_size; 389 390 if (*pp + sz - p < copy) 391 copy = *pp + sz - p; 392 393 memcpy(header + ctx->header_size, p, copy); 394 p += copy; 395 ctx->header_size += copy; 396 } 397 *pp = p; 398 return state; 399 } 400 401 /* device_run() - prepares and starts the device */ 402 static void device_run(void *priv) 403 { 404 struct vicodec_ctx *ctx = priv; 405 struct vicodec_dev *dev = ctx->dev; 406 struct vb2_v4l2_buffer *src_buf, *dst_buf; 407 struct vicodec_q_data *q_src, *q_dst; 408 u32 state; 409 struct media_request *src_req; 410 411 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 412 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 413 src_req = src_buf->vb2_buf.req_obj.req; 414 415 q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 416 q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 417 418 state = VB2_BUF_STATE_DONE; 419 if (device_process(ctx, src_buf, dst_buf)) 420 state = VB2_BUF_STATE_ERROR; 421 else 422 dst_buf->sequence = q_dst->sequence++; 423 dst_buf->flags &= ~V4L2_BUF_FLAG_LAST; 424 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf); 425 426 spin_lock(ctx->lock); 427 if (!ctx->comp_has_next_frame && 428 v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) { 429 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 430 v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event); 431 v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx); 432 } 433 if (ctx->is_enc || ctx->is_stateless) { 434 src_buf->sequence = q_src->sequence++; 435 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 436 v4l2_m2m_buf_done(src_buf, state); 437 } else if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == ctx->cur_buf_offset) { 438 src_buf->sequence = q_src->sequence++; 439 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 440 v4l2_m2m_buf_done(src_buf, state); 441 ctx->cur_buf_offset = 0; 442 ctx->comp_has_next_frame = false; 443 } 444 v4l2_m2m_buf_done(dst_buf, state); 445 446 ctx->comp_size = 0; 447 ctx->header_size = 0; 448 ctx->comp_magic_cnt = 0; 449 ctx->comp_has_frame = false; 450 spin_unlock(ctx->lock); 451 if (ctx->is_stateless && src_req) { 452 v4l2_ctrl_request_complete(src_req, &ctx->hdl); 453 media_request_manual_complete(src_req); 454 } 455 456 if (ctx->is_enc) 457 v4l2_m2m_job_finish(dev->stateful_enc.m2m_dev, ctx->fh.m2m_ctx); 458 else if (ctx->is_stateless) 459 v4l2_m2m_job_finish(dev->stateless_dec.m2m_dev, 460 ctx->fh.m2m_ctx); 461 else 462 v4l2_m2m_job_finish(dev->stateful_dec.m2m_dev, ctx->fh.m2m_ctx); 463 } 464 465 static void job_remove_src_buf(struct vicodec_ctx *ctx, u32 state) 466 { 467 struct vb2_v4l2_buffer *src_buf; 468 struct vicodec_q_data *q_src; 469 470 q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 471 spin_lock(ctx->lock); 472 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 473 src_buf->sequence = q_src->sequence++; 474 v4l2_m2m_buf_done(src_buf, state); 475 ctx->cur_buf_offset = 0; 476 spin_unlock(ctx->lock); 477 } 478 479 static const struct v4l2_fwht_pixfmt_info * 480 info_from_header(const struct fwht_cframe_hdr *p_hdr) 481 { 482 unsigned int flags = ntohl(p_hdr->flags); 483 unsigned int width_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2; 484 unsigned int height_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2; 485 unsigned int components_num = 3; 486 unsigned int pixenc = 0; 487 unsigned int version = ntohl(p_hdr->version); 488 489 if (version >= 2) { 490 components_num = 1 + ((flags & V4L2_FWHT_FL_COMPONENTS_NUM_MSK) >> 491 V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET); 492 pixenc = (flags & V4L2_FWHT_FL_PIXENC_MSK); 493 } 494 return v4l2_fwht_find_nth_fmt(width_div, height_div, 495 components_num, pixenc, 0); 496 } 497 498 static bool is_header_valid(const struct fwht_cframe_hdr *p_hdr) 499 { 500 const struct v4l2_fwht_pixfmt_info *info; 501 unsigned int w = ntohl(p_hdr->width); 502 unsigned int h = ntohl(p_hdr->height); 503 unsigned int version = ntohl(p_hdr->version); 504 unsigned int flags = ntohl(p_hdr->flags); 505 506 if (w < MIN_WIDTH || w > MAX_WIDTH || h < MIN_HEIGHT || h > MAX_HEIGHT) 507 return false; 508 509 if (!validate_by_version(flags, version)) 510 return false; 511 512 info = info_from_header(p_hdr); 513 if (!info) 514 return false; 515 return true; 516 } 517 518 static void update_capture_data_from_header(struct vicodec_ctx *ctx) 519 { 520 struct vicodec_q_data *q_dst = get_q_data(ctx, 521 V4L2_BUF_TYPE_VIDEO_CAPTURE); 522 const struct fwht_cframe_hdr *p_hdr = &ctx->state.header; 523 const struct v4l2_fwht_pixfmt_info *info = info_from_header(p_hdr); 524 unsigned int flags = ntohl(p_hdr->flags); 525 unsigned int hdr_width_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2; 526 unsigned int hdr_height_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2; 527 528 /* 529 * This function should not be used by a stateless codec since 530 * it changes values in q_data that are not request specific 531 */ 532 WARN_ON(ctx->is_stateless); 533 534 q_dst->info = info; 535 q_dst->visible_width = ntohl(p_hdr->width); 536 q_dst->visible_height = ntohl(p_hdr->height); 537 q_dst->coded_width = vic_round_dim(q_dst->visible_width, hdr_width_div); 538 q_dst->coded_height = vic_round_dim(q_dst->visible_height, 539 hdr_height_div); 540 541 q_dst->sizeimage = q_dst->coded_width * q_dst->coded_height * 542 q_dst->info->sizeimage_mult / q_dst->info->sizeimage_div; 543 ctx->state.colorspace = ntohl(p_hdr->colorspace); 544 545 ctx->state.xfer_func = ntohl(p_hdr->xfer_func); 546 ctx->state.ycbcr_enc = ntohl(p_hdr->ycbcr_enc); 547 ctx->state.quantization = ntohl(p_hdr->quantization); 548 } 549 550 static void set_last_buffer(struct vb2_v4l2_buffer *dst_buf, 551 const struct vb2_v4l2_buffer *src_buf, 552 struct vicodec_ctx *ctx) 553 { 554 struct vicodec_q_data *q_dst = get_q_data(ctx, 555 V4L2_BUF_TYPE_VIDEO_CAPTURE); 556 557 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0); 558 dst_buf->sequence = q_dst->sequence++; 559 560 v4l2_m2m_buf_copy_metadata(src_buf, dst_buf); 561 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 562 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); 563 } 564 565 static int job_ready(void *priv) 566 { 567 static const u8 magic[] = { 568 0x4f, 0x4f, 0x4f, 0x4f, 0xff, 0xff, 0xff, 0xff 569 }; 570 struct vicodec_ctx *ctx = priv; 571 struct vb2_v4l2_buffer *src_buf; 572 u8 *p_src; 573 u8 *p; 574 u32 sz; 575 u32 state; 576 struct vicodec_q_data *q_dst = get_q_data(ctx, 577 V4L2_BUF_TYPE_VIDEO_CAPTURE); 578 unsigned int flags; 579 unsigned int hdr_width_div; 580 unsigned int hdr_height_div; 581 unsigned int max_to_copy; 582 unsigned int comp_frame_size; 583 584 if (ctx->source_changed) 585 return 0; 586 if (ctx->is_stateless || ctx->is_enc || ctx->comp_has_frame) 587 return 1; 588 589 restart: 590 ctx->comp_has_next_frame = false; 591 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 592 if (!src_buf) 593 return 0; 594 p_src = vb2_plane_vaddr(&src_buf->vb2_buf, 0); 595 sz = vb2_get_plane_payload(&src_buf->vb2_buf, 0); 596 p = p_src + ctx->cur_buf_offset; 597 598 state = VB2_BUF_STATE_DONE; 599 600 if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) { 601 state = get_next_header(ctx, &p, p_src + sz - p); 602 if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) { 603 if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, 604 src_buf)) 605 return 1; 606 job_remove_src_buf(ctx, state); 607 goto restart; 608 } 609 } 610 611 comp_frame_size = ntohl(ctx->state.header.size); 612 613 /* 614 * The current scanned frame might be the first frame of a new 615 * resolution so its size might be larger than ctx->comp_max_size. 616 * In that case it is copied up to the current buffer capacity and 617 * the copy will continue after allocating new large enough buffer 618 * when restreaming 619 */ 620 max_to_copy = min(comp_frame_size, ctx->comp_max_size); 621 622 if (ctx->comp_size < max_to_copy) { 623 u32 copy = max_to_copy - ctx->comp_size; 624 625 if (copy > p_src + sz - p) 626 copy = p_src + sz - p; 627 628 memcpy(ctx->state.compressed_frame + ctx->comp_size, 629 p, copy); 630 p += copy; 631 ctx->comp_size += copy; 632 if (ctx->comp_size < max_to_copy) { 633 if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, 634 src_buf)) 635 return 1; 636 job_remove_src_buf(ctx, state); 637 goto restart; 638 } 639 } 640 ctx->cur_buf_offset = p - p_src; 641 if (ctx->comp_size == comp_frame_size) 642 ctx->comp_has_frame = true; 643 ctx->comp_has_next_frame = false; 644 if (ctx->comp_has_frame && sz - ctx->cur_buf_offset >= 645 sizeof(struct fwht_cframe_hdr)) { 646 struct fwht_cframe_hdr *p_hdr = (struct fwht_cframe_hdr *)p; 647 u32 frame_size = ntohl(p_hdr->size); 648 u32 remaining = sz - ctx->cur_buf_offset - sizeof(*p_hdr); 649 650 if (!memcmp(p, magic, sizeof(magic))) 651 ctx->comp_has_next_frame = remaining >= frame_size; 652 } 653 /* 654 * if the header is invalid the device_run will just drop the frame 655 * with an error 656 */ 657 if (!is_header_valid(&ctx->state.header) && ctx->comp_has_frame) 658 return 1; 659 flags = ntohl(ctx->state.header.flags); 660 hdr_width_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2; 661 hdr_height_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2; 662 663 if (ntohl(ctx->state.header.width) != q_dst->visible_width || 664 ntohl(ctx->state.header.height) != q_dst->visible_height || 665 !q_dst->info || 666 hdr_width_div != q_dst->info->width_div || 667 hdr_height_div != q_dst->info->height_div) { 668 static const struct v4l2_event rs_event = { 669 .type = V4L2_EVENT_SOURCE_CHANGE, 670 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, 671 }; 672 673 struct vb2_v4l2_buffer *dst_buf = 674 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 675 676 update_capture_data_from_header(ctx); 677 v4l2_event_queue_fh(&ctx->fh, &rs_event); 678 set_last_buffer(dst_buf, src_buf, ctx); 679 ctx->source_changed = true; 680 return 0; 681 } 682 return 1; 683 } 684 685 /* 686 * video ioctls 687 */ 688 689 static const struct v4l2_fwht_pixfmt_info *find_fmt(u32 fmt) 690 { 691 const struct v4l2_fwht_pixfmt_info *info = 692 v4l2_fwht_find_pixfmt(fmt); 693 694 if (!info) 695 info = v4l2_fwht_get_pixfmt(0); 696 return info; 697 } 698 699 static int vidioc_querycap(struct file *file, void *priv, 700 struct v4l2_capability *cap) 701 { 702 strscpy(cap->driver, VICODEC_NAME, sizeof(cap->driver)); 703 strscpy(cap->card, VICODEC_NAME, sizeof(cap->card)); 704 snprintf(cap->bus_info, sizeof(cap->bus_info), 705 "platform:%s", VICODEC_NAME); 706 return 0; 707 } 708 709 static int enum_fmt(struct v4l2_fmtdesc *f, struct vicodec_ctx *ctx, 710 bool is_out) 711 { 712 bool is_uncomp = (ctx->is_enc && is_out) || (!ctx->is_enc && !is_out); 713 714 if (V4L2_TYPE_IS_MULTIPLANAR(f->type) && !multiplanar) 715 return -EINVAL; 716 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type) && multiplanar) 717 return -EINVAL; 718 719 if (is_uncomp) { 720 const struct v4l2_fwht_pixfmt_info *info = 721 get_q_data(ctx, f->type)->info; 722 723 if (ctx->is_enc || 724 !vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) 725 info = v4l2_fwht_get_pixfmt(f->index); 726 else 727 info = v4l2_fwht_find_nth_fmt(info->width_div, 728 info->height_div, 729 info->components_num, 730 info->pixenc, 731 f->index); 732 if (!info) 733 return -EINVAL; 734 f->pixelformat = info->id; 735 } else { 736 if (f->index) 737 return -EINVAL; 738 f->pixelformat = ctx->is_stateless ? 739 V4L2_PIX_FMT_FWHT_STATELESS : V4L2_PIX_FMT_FWHT; 740 if (!ctx->is_enc && !ctx->is_stateless) 741 f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION | 742 V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM; 743 } 744 return 0; 745 } 746 747 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 748 struct v4l2_fmtdesc *f) 749 { 750 struct vicodec_ctx *ctx = file2ctx(file); 751 752 return enum_fmt(f, ctx, false); 753 } 754 755 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, 756 struct v4l2_fmtdesc *f) 757 { 758 struct vicodec_ctx *ctx = file2ctx(file); 759 760 return enum_fmt(f, ctx, true); 761 } 762 763 static int vidioc_g_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f) 764 { 765 struct vicodec_q_data *q_data; 766 struct v4l2_pix_format_mplane *pix_mp; 767 struct v4l2_pix_format *pix; 768 const struct v4l2_fwht_pixfmt_info *info; 769 770 q_data = get_q_data(ctx, f->type); 771 info = q_data->info; 772 773 switch (f->type) { 774 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 775 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 776 if (multiplanar) 777 return -EINVAL; 778 pix = &f->fmt.pix; 779 pix->width = q_data->coded_width; 780 pix->height = q_data->coded_height; 781 pix->field = V4L2_FIELD_NONE; 782 pix->pixelformat = info->id; 783 pix->bytesperline = q_data->coded_width * 784 info->bytesperline_mult; 785 pix->sizeimage = q_data->sizeimage; 786 pix->colorspace = ctx->state.colorspace; 787 pix->xfer_func = ctx->state.xfer_func; 788 pix->ycbcr_enc = ctx->state.ycbcr_enc; 789 pix->quantization = ctx->state.quantization; 790 break; 791 792 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 793 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 794 if (!multiplanar) 795 return -EINVAL; 796 pix_mp = &f->fmt.pix_mp; 797 pix_mp->width = q_data->coded_width; 798 pix_mp->height = q_data->coded_height; 799 pix_mp->field = V4L2_FIELD_NONE; 800 pix_mp->pixelformat = info->id; 801 pix_mp->num_planes = 1; 802 pix_mp->plane_fmt[0].bytesperline = 803 q_data->coded_width * info->bytesperline_mult; 804 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage; 805 pix_mp->colorspace = ctx->state.colorspace; 806 pix_mp->xfer_func = ctx->state.xfer_func; 807 pix_mp->ycbcr_enc = ctx->state.ycbcr_enc; 808 pix_mp->quantization = ctx->state.quantization; 809 break; 810 default: 811 return -EINVAL; 812 } 813 return 0; 814 } 815 816 static int vidioc_g_fmt_vid_out(struct file *file, void *priv, 817 struct v4l2_format *f) 818 { 819 return vidioc_g_fmt(file2ctx(file), f); 820 } 821 822 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 823 struct v4l2_format *f) 824 { 825 return vidioc_g_fmt(file2ctx(file), f); 826 } 827 828 static int vidioc_try_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f) 829 { 830 struct v4l2_pix_format_mplane *pix_mp; 831 struct v4l2_pix_format *pix; 832 struct v4l2_plane_pix_format *plane; 833 const struct v4l2_fwht_pixfmt_info *info = ctx->is_stateless ? 834 &pixfmt_stateless_fwht : &pixfmt_fwht; 835 836 switch (f->type) { 837 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 838 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 839 pix = &f->fmt.pix; 840 if (pix->pixelformat != V4L2_PIX_FMT_FWHT && 841 pix->pixelformat != V4L2_PIX_FMT_FWHT_STATELESS) 842 info = find_fmt(pix->pixelformat); 843 844 pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH); 845 pix->width = vic_round_dim(pix->width, info->width_div); 846 847 pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT); 848 pix->height = vic_round_dim(pix->height, info->height_div); 849 850 pix->field = V4L2_FIELD_NONE; 851 pix->bytesperline = 852 pix->width * info->bytesperline_mult; 853 pix->sizeimage = pix->width * pix->height * 854 info->sizeimage_mult / info->sizeimage_div; 855 if (pix->pixelformat == V4L2_PIX_FMT_FWHT) 856 pix->sizeimage += sizeof(struct fwht_cframe_hdr); 857 break; 858 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 859 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 860 pix_mp = &f->fmt.pix_mp; 861 plane = pix_mp->plane_fmt; 862 if (pix_mp->pixelformat != V4L2_PIX_FMT_FWHT && 863 pix_mp->pixelformat != V4L2_PIX_FMT_FWHT_STATELESS) 864 info = find_fmt(pix_mp->pixelformat); 865 pix_mp->num_planes = 1; 866 867 pix_mp->width = clamp(pix_mp->width, MIN_WIDTH, MAX_WIDTH); 868 pix_mp->width = vic_round_dim(pix_mp->width, info->width_div); 869 870 pix_mp->height = clamp(pix_mp->height, MIN_HEIGHT, MAX_HEIGHT); 871 pix_mp->height = vic_round_dim(pix_mp->height, 872 info->height_div); 873 874 pix_mp->field = V4L2_FIELD_NONE; 875 plane->bytesperline = 876 pix_mp->width * info->bytesperline_mult; 877 plane->sizeimage = pix_mp->width * pix_mp->height * 878 info->sizeimage_mult / info->sizeimage_div; 879 if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT) 880 plane->sizeimage += sizeof(struct fwht_cframe_hdr); 881 break; 882 default: 883 return -EINVAL; 884 } 885 886 return 0; 887 } 888 889 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 890 struct v4l2_format *f) 891 { 892 struct vicodec_ctx *ctx = file2ctx(file); 893 struct v4l2_pix_format_mplane *pix_mp; 894 struct v4l2_pix_format *pix; 895 896 switch (f->type) { 897 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 898 if (multiplanar) 899 return -EINVAL; 900 pix = &f->fmt.pix; 901 pix->pixelformat = ctx->is_enc ? V4L2_PIX_FMT_FWHT : 902 find_fmt(f->fmt.pix.pixelformat)->id; 903 pix->colorspace = ctx->state.colorspace; 904 pix->xfer_func = ctx->state.xfer_func; 905 pix->ycbcr_enc = ctx->state.ycbcr_enc; 906 pix->quantization = ctx->state.quantization; 907 break; 908 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 909 if (!multiplanar) 910 return -EINVAL; 911 pix_mp = &f->fmt.pix_mp; 912 pix_mp->pixelformat = ctx->is_enc ? V4L2_PIX_FMT_FWHT : 913 find_fmt(pix_mp->pixelformat)->id; 914 pix_mp->colorspace = ctx->state.colorspace; 915 pix_mp->xfer_func = ctx->state.xfer_func; 916 pix_mp->ycbcr_enc = ctx->state.ycbcr_enc; 917 pix_mp->quantization = ctx->state.quantization; 918 break; 919 default: 920 return -EINVAL; 921 } 922 923 return vidioc_try_fmt(ctx, f); 924 } 925 926 static int vidioc_try_fmt_vid_out(struct file *file, void *priv, 927 struct v4l2_format *f) 928 { 929 struct vicodec_ctx *ctx = file2ctx(file); 930 struct v4l2_pix_format_mplane *pix_mp; 931 struct v4l2_pix_format *pix; 932 933 switch (f->type) { 934 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 935 if (multiplanar) 936 return -EINVAL; 937 pix = &f->fmt.pix; 938 if (ctx->is_enc) 939 pix->pixelformat = find_fmt(pix->pixelformat)->id; 940 else if (ctx->is_stateless) 941 pix->pixelformat = V4L2_PIX_FMT_FWHT_STATELESS; 942 else 943 pix->pixelformat = V4L2_PIX_FMT_FWHT; 944 if (!pix->colorspace) 945 pix->colorspace = V4L2_COLORSPACE_REC709; 946 break; 947 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 948 if (!multiplanar) 949 return -EINVAL; 950 pix_mp = &f->fmt.pix_mp; 951 if (ctx->is_enc) 952 pix_mp->pixelformat = find_fmt(pix_mp->pixelformat)->id; 953 else if (ctx->is_stateless) 954 pix_mp->pixelformat = V4L2_PIX_FMT_FWHT_STATELESS; 955 else 956 pix_mp->pixelformat = V4L2_PIX_FMT_FWHT; 957 if (!pix_mp->colorspace) 958 pix_mp->colorspace = V4L2_COLORSPACE_REC709; 959 break; 960 default: 961 return -EINVAL; 962 } 963 964 return vidioc_try_fmt(ctx, f); 965 } 966 967 static int vidioc_s_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f) 968 { 969 struct vicodec_q_data *q_data; 970 struct vb2_queue *vq; 971 bool fmt_changed = true; 972 struct v4l2_pix_format_mplane *pix_mp; 973 struct v4l2_pix_format *pix; 974 975 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 976 977 q_data = get_q_data(ctx, f->type); 978 if (!q_data) 979 return -EINVAL; 980 981 switch (f->type) { 982 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 983 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 984 pix = &f->fmt.pix; 985 if (ctx->is_enc && V4L2_TYPE_IS_OUTPUT(f->type)) 986 fmt_changed = 987 !q_data->info || 988 q_data->info->id != pix->pixelformat || 989 q_data->coded_width != pix->width || 990 q_data->coded_height != pix->height; 991 992 if (vb2_is_busy(vq) && fmt_changed) 993 return -EBUSY; 994 995 if (pix->pixelformat == V4L2_PIX_FMT_FWHT) 996 q_data->info = &pixfmt_fwht; 997 else if (pix->pixelformat == V4L2_PIX_FMT_FWHT_STATELESS) 998 q_data->info = &pixfmt_stateless_fwht; 999 else 1000 q_data->info = find_fmt(pix->pixelformat); 1001 q_data->coded_width = pix->width; 1002 q_data->coded_height = pix->height; 1003 q_data->sizeimage = pix->sizeimage; 1004 break; 1005 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 1006 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 1007 pix_mp = &f->fmt.pix_mp; 1008 if (ctx->is_enc && V4L2_TYPE_IS_OUTPUT(f->type)) 1009 fmt_changed = 1010 !q_data->info || 1011 q_data->info->id != pix_mp->pixelformat || 1012 q_data->coded_width != pix_mp->width || 1013 q_data->coded_height != pix_mp->height; 1014 1015 if (vb2_is_busy(vq) && fmt_changed) 1016 return -EBUSY; 1017 1018 if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT) 1019 q_data->info = &pixfmt_fwht; 1020 else if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT_STATELESS) 1021 q_data->info = &pixfmt_stateless_fwht; 1022 else 1023 q_data->info = find_fmt(pix_mp->pixelformat); 1024 q_data->coded_width = pix_mp->width; 1025 q_data->coded_height = pix_mp->height; 1026 q_data->sizeimage = pix_mp->plane_fmt[0].sizeimage; 1027 break; 1028 default: 1029 return -EINVAL; 1030 } 1031 1032 dprintk(ctx->dev, 1033 "Setting format for type %d, coded wxh: %dx%d, fourcc: 0x%08x\n", 1034 f->type, q_data->coded_width, q_data->coded_height, 1035 q_data->info->id); 1036 1037 return 0; 1038 } 1039 1040 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 1041 struct v4l2_format *f) 1042 { 1043 int ret; 1044 1045 ret = vidioc_try_fmt_vid_cap(file, priv, f); 1046 if (ret) 1047 return ret; 1048 1049 return vidioc_s_fmt(file2ctx(file), f); 1050 } 1051 1052 static int vidioc_s_fmt_vid_out(struct file *file, void *priv, 1053 struct v4l2_format *f) 1054 { 1055 struct vicodec_ctx *ctx = file2ctx(file); 1056 struct vicodec_q_data *q_data; 1057 struct vicodec_q_data *q_data_cap; 1058 struct v4l2_pix_format *pix; 1059 struct v4l2_pix_format_mplane *pix_mp; 1060 u32 coded_w = 0, coded_h = 0; 1061 unsigned int size = 0; 1062 int ret; 1063 1064 q_data = get_q_data(ctx, f->type); 1065 q_data_cap = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1066 1067 ret = vidioc_try_fmt_vid_out(file, priv, f); 1068 if (ret) 1069 return ret; 1070 1071 if (ctx->is_enc) { 1072 struct vb2_queue *vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 1073 struct vb2_queue *vq_cap = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 1074 V4L2_BUF_TYPE_VIDEO_CAPTURE); 1075 const struct v4l2_fwht_pixfmt_info *info = ctx->is_stateless ? 1076 &pixfmt_stateless_fwht : &pixfmt_fwht; 1077 1078 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1079 coded_w = f->fmt.pix.width; 1080 coded_h = f->fmt.pix.height; 1081 } else { 1082 coded_w = f->fmt.pix_mp.width; 1083 coded_h = f->fmt.pix_mp.height; 1084 } 1085 if (vb2_is_busy(vq) && (coded_w != q_data->coded_width || 1086 coded_h != q_data->coded_height)) 1087 return -EBUSY; 1088 size = coded_w * coded_h * 1089 info->sizeimage_mult / info->sizeimage_div; 1090 if (!ctx->is_stateless) 1091 size += sizeof(struct fwht_cframe_hdr); 1092 1093 if (vb2_is_busy(vq_cap) && size > q_data_cap->sizeimage) 1094 return -EBUSY; 1095 } 1096 1097 ret = vidioc_s_fmt(file2ctx(file), f); 1098 if (!ret) { 1099 if (ctx->is_enc) { 1100 q_data->visible_width = coded_w; 1101 q_data->visible_height = coded_h; 1102 q_data_cap->coded_width = coded_w; 1103 q_data_cap->coded_height = coded_h; 1104 q_data_cap->sizeimage = size; 1105 } 1106 1107 switch (f->type) { 1108 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 1109 pix = &f->fmt.pix; 1110 ctx->state.colorspace = pix->colorspace; 1111 ctx->state.xfer_func = pix->xfer_func; 1112 ctx->state.ycbcr_enc = pix->ycbcr_enc; 1113 ctx->state.quantization = pix->quantization; 1114 break; 1115 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 1116 pix_mp = &f->fmt.pix_mp; 1117 ctx->state.colorspace = pix_mp->colorspace; 1118 ctx->state.xfer_func = pix_mp->xfer_func; 1119 ctx->state.ycbcr_enc = pix_mp->ycbcr_enc; 1120 ctx->state.quantization = pix_mp->quantization; 1121 break; 1122 default: 1123 break; 1124 } 1125 } 1126 return ret; 1127 } 1128 1129 static int vidioc_g_selection(struct file *file, void *priv, 1130 struct v4l2_selection *s) 1131 { 1132 struct vicodec_ctx *ctx = file2ctx(file); 1133 struct vicodec_q_data *q_data; 1134 1135 q_data = get_q_data(ctx, s->type); 1136 if (!q_data) 1137 return -EINVAL; 1138 /* 1139 * encoder supports only cropping on the OUTPUT buffer 1140 * decoder supports only composing on the CAPTURE buffer 1141 */ 1142 if (ctx->is_enc && s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1143 switch (s->target) { 1144 case V4L2_SEL_TGT_CROP: 1145 s->r.left = 0; 1146 s->r.top = 0; 1147 s->r.width = q_data->visible_width; 1148 s->r.height = q_data->visible_height; 1149 return 0; 1150 case V4L2_SEL_TGT_CROP_DEFAULT: 1151 case V4L2_SEL_TGT_CROP_BOUNDS: 1152 s->r.left = 0; 1153 s->r.top = 0; 1154 s->r.width = q_data->coded_width; 1155 s->r.height = q_data->coded_height; 1156 return 0; 1157 } 1158 } else if (!ctx->is_enc && s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 1159 switch (s->target) { 1160 case V4L2_SEL_TGT_COMPOSE: 1161 s->r.left = 0; 1162 s->r.top = 0; 1163 s->r.width = q_data->visible_width; 1164 s->r.height = q_data->visible_height; 1165 return 0; 1166 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 1167 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 1168 s->r.left = 0; 1169 s->r.top = 0; 1170 s->r.width = q_data->coded_width; 1171 s->r.height = q_data->coded_height; 1172 return 0; 1173 } 1174 } 1175 return -EINVAL; 1176 } 1177 1178 static int vidioc_s_selection(struct file *file, void *priv, 1179 struct v4l2_selection *s) 1180 { 1181 struct vicodec_ctx *ctx = file2ctx(file); 1182 struct vicodec_q_data *q_data; 1183 1184 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 1185 return -EINVAL; 1186 1187 q_data = get_q_data(ctx, s->type); 1188 if (!q_data) 1189 return -EINVAL; 1190 1191 if (!ctx->is_enc || s->target != V4L2_SEL_TGT_CROP) 1192 return -EINVAL; 1193 1194 s->r.left = 0; 1195 s->r.top = 0; 1196 q_data->visible_width = clamp(s->r.width, MIN_WIDTH, 1197 q_data->coded_width); 1198 s->r.width = q_data->visible_width; 1199 q_data->visible_height = clamp(s->r.height, MIN_HEIGHT, 1200 q_data->coded_height); 1201 s->r.height = q_data->visible_height; 1202 return 0; 1203 } 1204 1205 static int vicodec_encoder_cmd(struct file *file, void *priv, 1206 struct v4l2_encoder_cmd *ec) 1207 { 1208 struct vicodec_ctx *ctx = file2ctx(file); 1209 int ret; 1210 1211 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec); 1212 if (ret < 0) 1213 return ret; 1214 1215 if (!vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q)) 1216 return 0; 1217 1218 ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec); 1219 if (ret < 0) 1220 return ret; 1221 1222 if (ec->cmd == V4L2_ENC_CMD_STOP && 1223 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1224 v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event); 1225 1226 if (ec->cmd == V4L2_ENC_CMD_START && 1227 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1228 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q); 1229 1230 return 0; 1231 } 1232 1233 static int vicodec_decoder_cmd(struct file *file, void *priv, 1234 struct v4l2_decoder_cmd *dc) 1235 { 1236 struct vicodec_ctx *ctx = file2ctx(file); 1237 int ret; 1238 1239 /* 1240 * This ioctl should not be used with a stateless codec that doesn't 1241 * support holding buffers and the associated flush command. 1242 */ 1243 WARN_ON(ctx->is_stateless); 1244 1245 ret = v4l2_m2m_ioctl_try_decoder_cmd(file, priv, dc); 1246 if (ret < 0) 1247 return ret; 1248 1249 if (!vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q)) 1250 return 0; 1251 1252 ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, dc); 1253 if (ret < 0) 1254 return ret; 1255 1256 if (dc->cmd == V4L2_DEC_CMD_STOP && 1257 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1258 v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event); 1259 1260 if (dc->cmd == V4L2_DEC_CMD_START && 1261 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1262 vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q); 1263 1264 return 0; 1265 } 1266 1267 static int vicodec_enum_framesizes(struct file *file, void *priv, 1268 struct v4l2_frmsizeenum *fsize) 1269 { 1270 switch (fsize->pixel_format) { 1271 case V4L2_PIX_FMT_FWHT_STATELESS: 1272 break; 1273 case V4L2_PIX_FMT_FWHT: 1274 break; 1275 default: 1276 if (find_fmt(fsize->pixel_format)->id == fsize->pixel_format) 1277 break; 1278 return -EINVAL; 1279 } 1280 1281 if (fsize->index) 1282 return -EINVAL; 1283 1284 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 1285 1286 fsize->stepwise.min_width = MIN_WIDTH; 1287 fsize->stepwise.max_width = MAX_WIDTH; 1288 fsize->stepwise.step_width = 8; 1289 fsize->stepwise.min_height = MIN_HEIGHT; 1290 fsize->stepwise.max_height = MAX_HEIGHT; 1291 fsize->stepwise.step_height = 8; 1292 1293 return 0; 1294 } 1295 1296 static int vicodec_subscribe_event(struct v4l2_fh *fh, 1297 const struct v4l2_event_subscription *sub) 1298 { 1299 struct vicodec_ctx *ctx = container_of(fh, struct vicodec_ctx, fh); 1300 1301 switch (sub->type) { 1302 case V4L2_EVENT_SOURCE_CHANGE: 1303 if (ctx->is_enc) 1304 return -EINVAL; 1305 fallthrough; 1306 case V4L2_EVENT_EOS: 1307 if (ctx->is_stateless) 1308 return -EINVAL; 1309 return v4l2_event_subscribe(fh, sub, 0, NULL); 1310 default: 1311 return v4l2_ctrl_subscribe_event(fh, sub); 1312 } 1313 } 1314 1315 static const struct v4l2_ioctl_ops vicodec_ioctl_ops = { 1316 .vidioc_querycap = vidioc_querycap, 1317 1318 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 1319 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1320 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 1321 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 1322 1323 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_vid_cap, 1324 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap, 1325 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_vid_cap, 1326 1327 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, 1328 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, 1329 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, 1330 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, 1331 1332 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_vid_out, 1333 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out, 1334 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_vid_out, 1335 1336 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 1337 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 1338 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 1339 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 1340 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 1341 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 1342 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 1343 .vidioc_remove_bufs = v4l2_m2m_ioctl_remove_bufs, 1344 1345 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 1346 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 1347 1348 .vidioc_g_selection = vidioc_g_selection, 1349 .vidioc_s_selection = vidioc_s_selection, 1350 1351 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 1352 .vidioc_encoder_cmd = vicodec_encoder_cmd, 1353 .vidioc_try_decoder_cmd = v4l2_m2m_ioctl_try_decoder_cmd, 1354 .vidioc_decoder_cmd = vicodec_decoder_cmd, 1355 .vidioc_enum_framesizes = vicodec_enum_framesizes, 1356 1357 .vidioc_subscribe_event = vicodec_subscribe_event, 1358 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1359 }; 1360 1361 1362 /* 1363 * Queue operations 1364 */ 1365 1366 static int vicodec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, 1367 unsigned int *nplanes, unsigned int sizes[], 1368 struct device *alloc_devs[]) 1369 { 1370 struct vicodec_ctx *ctx = vb2_get_drv_priv(vq); 1371 struct vicodec_q_data *q_data = get_q_data(ctx, vq->type); 1372 unsigned int size = q_data->sizeimage; 1373 1374 if (*nplanes) 1375 return sizes[0] < size ? -EINVAL : 0; 1376 1377 *nplanes = 1; 1378 sizes[0] = size; 1379 q_data->vb2_sizeimage = size; 1380 return 0; 1381 } 1382 1383 static int vicodec_buf_out_validate(struct vb2_buffer *vb) 1384 { 1385 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1386 1387 vbuf->field = V4L2_FIELD_NONE; 1388 return 0; 1389 } 1390 1391 static int vicodec_buf_prepare(struct vb2_buffer *vb) 1392 { 1393 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1394 struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1395 struct vicodec_q_data *q_data; 1396 1397 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type); 1398 1399 q_data = get_q_data(ctx, vb->vb2_queue->type); 1400 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { 1401 if (vbuf->field == V4L2_FIELD_ANY) 1402 vbuf->field = V4L2_FIELD_NONE; 1403 if (vbuf->field != V4L2_FIELD_NONE) { 1404 dprintk(ctx->dev, "%s field isn't supported\n", 1405 __func__); 1406 return -EINVAL; 1407 } 1408 } 1409 1410 if (vb2_plane_size(vb, 0) < q_data->vb2_sizeimage) { 1411 dprintk(ctx->dev, 1412 "%s data will not fit into plane (%lu < %lu)\n", 1413 __func__, vb2_plane_size(vb, 0), 1414 (long)q_data->vb2_sizeimage); 1415 return -EINVAL; 1416 } 1417 1418 return 0; 1419 } 1420 1421 static void vicodec_buf_queue(struct vb2_buffer *vb) 1422 { 1423 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1424 struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1425 unsigned int sz = vb2_get_plane_payload(&vbuf->vb2_buf, 0); 1426 u8 *p_src = vb2_plane_vaddr(&vbuf->vb2_buf, 0); 1427 u8 *p = p_src; 1428 struct vb2_queue *vq_out = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 1429 V4L2_BUF_TYPE_VIDEO_OUTPUT); 1430 struct vb2_queue *vq_cap = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 1431 V4L2_BUF_TYPE_VIDEO_CAPTURE); 1432 bool header_valid = false; 1433 static const struct v4l2_event rs_event = { 1434 .type = V4L2_EVENT_SOURCE_CHANGE, 1435 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION, 1436 }; 1437 1438 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && 1439 vb2_is_streaming(vb->vb2_queue) && 1440 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) { 1441 unsigned int i; 1442 1443 for (i = 0; i < vb->num_planes; i++) 1444 vb2_set_plane_payload(vb, i, 0); 1445 1446 vbuf->field = V4L2_FIELD_NONE; 1447 vbuf->sequence = 1448 get_q_data(ctx, vb->vb2_queue->type)->sequence++; 1449 1450 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf); 1451 v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event); 1452 return; 1453 } 1454 1455 /* buf_queue handles only the first source change event */ 1456 if (ctx->first_source_change_sent) { 1457 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1458 return; 1459 } 1460 1461 /* 1462 * if both queues are streaming, the source change event is 1463 * handled in job_ready 1464 */ 1465 if (vb2_is_streaming(vq_cap) && vb2_is_streaming(vq_out)) { 1466 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1467 return; 1468 } 1469 1470 /* 1471 * source change event is relevant only for the stateful decoder 1472 * in the compressed stream 1473 */ 1474 if (ctx->is_stateless || ctx->is_enc || 1475 V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) { 1476 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1477 return; 1478 } 1479 1480 do { 1481 enum vb2_buffer_state state = 1482 get_next_header(ctx, &p, p_src + sz - p); 1483 1484 if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) { 1485 v4l2_m2m_buf_done(vbuf, state); 1486 return; 1487 } 1488 header_valid = is_header_valid(&ctx->state.header); 1489 /* 1490 * p points right after the end of the header in the 1491 * buffer. If the header is invalid we set p to point 1492 * to the next byte after the start of the header 1493 */ 1494 if (!header_valid) { 1495 p = p - sizeof(struct fwht_cframe_hdr) + 1; 1496 if (p < p_src) 1497 p = p_src; 1498 ctx->header_size = 0; 1499 ctx->comp_magic_cnt = 0; 1500 } 1501 1502 } while (!header_valid); 1503 1504 ctx->cur_buf_offset = p - p_src; 1505 update_capture_data_from_header(ctx); 1506 ctx->first_source_change_sent = true; 1507 v4l2_event_queue_fh(&ctx->fh, &rs_event); 1508 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1509 } 1510 1511 static void vicodec_return_bufs(struct vb2_queue *q, u32 state) 1512 { 1513 struct vicodec_ctx *ctx = vb2_get_drv_priv(q); 1514 struct vb2_v4l2_buffer *vbuf; 1515 1516 for (;;) { 1517 if (V4L2_TYPE_IS_OUTPUT(q->type)) 1518 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1519 else 1520 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1521 if (vbuf == NULL) 1522 return; 1523 if (ctx->is_stateless && V4L2_TYPE_IS_OUTPUT(q->type)) { 1524 struct media_request *req = vbuf->vb2_buf.req_obj.req; 1525 1526 v4l2_ctrl_request_complete(req, &ctx->hdl); 1527 media_request_manual_complete(req); 1528 } 1529 spin_lock(ctx->lock); 1530 v4l2_m2m_buf_done(vbuf, state); 1531 spin_unlock(ctx->lock); 1532 } 1533 } 1534 1535 static unsigned int total_frame_size(struct vicodec_q_data *q_data) 1536 { 1537 unsigned int size; 1538 unsigned int chroma_div; 1539 1540 if (!q_data->info) { 1541 WARN_ON(1); 1542 return 0; 1543 } 1544 size = q_data->coded_width * q_data->coded_height; 1545 chroma_div = q_data->info->width_div * q_data->info->height_div; 1546 1547 if (q_data->info->components_num == 4) 1548 return 2 * size + 2 * (size / chroma_div); 1549 else if (q_data->info->components_num == 3) 1550 return size + 2 * (size / chroma_div); 1551 return size; 1552 } 1553 1554 static int vicodec_start_streaming(struct vb2_queue *q, 1555 unsigned int count) 1556 { 1557 struct vicodec_ctx *ctx = vb2_get_drv_priv(q); 1558 struct vicodec_q_data *q_data = get_q_data(ctx, q->type); 1559 struct v4l2_fwht_state *state = &ctx->state; 1560 const struct v4l2_fwht_pixfmt_info *info = q_data->info; 1561 unsigned int size = q_data->coded_width * q_data->coded_height; 1562 unsigned int chroma_div; 1563 unsigned int total_planes_size; 1564 u8 *new_comp_frame = NULL; 1565 1566 chroma_div = info->width_div * info->height_div; 1567 q_data->sequence = 0; 1568 1569 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q); 1570 1571 state->gop_cnt = 0; 1572 1573 if ((V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) || 1574 (V4L2_TYPE_IS_CAPTURE(q->type) && ctx->is_enc)) 1575 return 0; 1576 1577 if (info->id == V4L2_PIX_FMT_FWHT || 1578 info->id == V4L2_PIX_FMT_FWHT_STATELESS) { 1579 vicodec_return_bufs(q, VB2_BUF_STATE_QUEUED); 1580 return -EINVAL; 1581 } 1582 total_planes_size = total_frame_size(q_data); 1583 ctx->comp_max_size = total_planes_size; 1584 1585 state->visible_width = q_data->visible_width; 1586 state->visible_height = q_data->visible_height; 1587 state->coded_width = q_data->coded_width; 1588 state->coded_height = q_data->coded_height; 1589 state->stride = q_data->coded_width * 1590 info->bytesperline_mult; 1591 1592 if (ctx->is_stateless) { 1593 state->ref_stride = state->stride; 1594 return 0; 1595 } 1596 state->ref_stride = q_data->coded_width * info->luma_alpha_step; 1597 1598 state->ref_frame.buf = kvmalloc(total_planes_size, GFP_KERNEL); 1599 state->ref_frame.luma = state->ref_frame.buf; 1600 new_comp_frame = kvmalloc(ctx->comp_max_size, GFP_KERNEL); 1601 1602 if (!state->ref_frame.luma || !new_comp_frame) { 1603 kvfree(state->ref_frame.luma); 1604 kvfree(new_comp_frame); 1605 vicodec_return_bufs(q, VB2_BUF_STATE_QUEUED); 1606 return -ENOMEM; 1607 } 1608 /* 1609 * if state->compressed_frame was already allocated then 1610 * it contain data of the first frame of the new resolution 1611 */ 1612 if (state->compressed_frame) { 1613 if (ctx->comp_size > ctx->comp_max_size) 1614 ctx->comp_size = ctx->comp_max_size; 1615 1616 memcpy(new_comp_frame, 1617 state->compressed_frame, ctx->comp_size); 1618 } 1619 1620 kvfree(state->compressed_frame); 1621 state->compressed_frame = new_comp_frame; 1622 1623 if (info->components_num < 3) { 1624 state->ref_frame.cb = NULL; 1625 state->ref_frame.cr = NULL; 1626 state->ref_frame.alpha = NULL; 1627 return 0; 1628 } 1629 1630 state->ref_frame.cb = state->ref_frame.luma + size; 1631 state->ref_frame.cr = state->ref_frame.cb + size / chroma_div; 1632 1633 if (info->components_num == 4) 1634 state->ref_frame.alpha = 1635 state->ref_frame.cr + size / chroma_div; 1636 else 1637 state->ref_frame.alpha = NULL; 1638 1639 return 0; 1640 } 1641 1642 static void vicodec_stop_streaming(struct vb2_queue *q) 1643 { 1644 struct vicodec_ctx *ctx = vb2_get_drv_priv(q); 1645 1646 vicodec_return_bufs(q, VB2_BUF_STATE_ERROR); 1647 1648 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); 1649 1650 if (V4L2_TYPE_IS_OUTPUT(q->type) && 1651 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1652 v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event); 1653 1654 if (!ctx->is_enc && V4L2_TYPE_IS_OUTPUT(q->type)) 1655 ctx->first_source_change_sent = false; 1656 1657 if ((!V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) || 1658 (V4L2_TYPE_IS_OUTPUT(q->type) && ctx->is_enc)) { 1659 if (!ctx->is_stateless) 1660 kvfree(ctx->state.ref_frame.buf); 1661 ctx->state.ref_frame.buf = NULL; 1662 ctx->state.ref_frame.luma = NULL; 1663 ctx->comp_max_size = 0; 1664 ctx->source_changed = false; 1665 } 1666 if (V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) { 1667 ctx->cur_buf_offset = 0; 1668 ctx->comp_size = 0; 1669 ctx->header_size = 0; 1670 ctx->comp_magic_cnt = 0; 1671 ctx->comp_has_frame = false; 1672 ctx->comp_has_next_frame = false; 1673 } 1674 } 1675 1676 static void vicodec_buf_request_complete(struct vb2_buffer *vb) 1677 { 1678 struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1679 1680 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); 1681 media_request_manual_complete(vb->req_obj.req); 1682 } 1683 1684 1685 static const struct vb2_ops vicodec_qops = { 1686 .queue_setup = vicodec_queue_setup, 1687 .buf_out_validate = vicodec_buf_out_validate, 1688 .buf_prepare = vicodec_buf_prepare, 1689 .buf_queue = vicodec_buf_queue, 1690 .buf_request_complete = vicodec_buf_request_complete, 1691 .start_streaming = vicodec_start_streaming, 1692 .stop_streaming = vicodec_stop_streaming, 1693 }; 1694 1695 static int queue_init(void *priv, struct vb2_queue *src_vq, 1696 struct vb2_queue *dst_vq) 1697 { 1698 struct vicodec_ctx *ctx = priv; 1699 int ret; 1700 1701 src_vq->type = (multiplanar ? 1702 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE : 1703 V4L2_BUF_TYPE_VIDEO_OUTPUT); 1704 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1705 src_vq->drv_priv = ctx; 1706 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1707 src_vq->ops = &vicodec_qops; 1708 src_vq->mem_ops = &vb2_vmalloc_memops; 1709 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1710 if (ctx->is_enc) { 1711 src_vq->lock = &ctx->dev->stateful_enc.mutex; 1712 src_vq->min_reqbufs_allocation = VICODEC_REC_BUFS; 1713 } else if (ctx->is_stateless) { 1714 src_vq->lock = &ctx->dev->stateless_dec.mutex; 1715 } else { 1716 src_vq->lock = &ctx->dev->stateful_dec.mutex; 1717 } 1718 src_vq->supports_requests = ctx->is_stateless; 1719 src_vq->requires_requests = ctx->is_stateless; 1720 ret = vb2_queue_init(src_vq); 1721 if (ret) 1722 return ret; 1723 1724 dst_vq->type = (multiplanar ? 1725 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : 1726 V4L2_BUF_TYPE_VIDEO_CAPTURE); 1727 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 1728 dst_vq->max_num_buffers = 64; 1729 dst_vq->drv_priv = ctx; 1730 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1731 dst_vq->ops = &vicodec_qops; 1732 dst_vq->mem_ops = &vb2_vmalloc_memops; 1733 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1734 dst_vq->lock = src_vq->lock; 1735 if (!ctx->is_stateless && !ctx->is_enc) 1736 dst_vq->min_reqbufs_allocation = VICODEC_REC_BUFS; 1737 1738 return vb2_queue_init(dst_vq); 1739 } 1740 1741 static int vicodec_try_ctrl(struct v4l2_ctrl *ctrl) 1742 { 1743 struct vicodec_ctx *ctx = container_of(ctrl->handler, 1744 struct vicodec_ctx, hdl); 1745 const struct v4l2_ctrl_fwht_params *params; 1746 struct vicodec_q_data *q_dst = get_q_data(ctx, 1747 V4L2_BUF_TYPE_VIDEO_CAPTURE); 1748 1749 switch (ctrl->id) { 1750 case V4L2_CID_STATELESS_FWHT_PARAMS: 1751 if (!q_dst->info) 1752 return -EINVAL; 1753 params = ctrl->p_new.p_fwht_params; 1754 if (params->width > q_dst->coded_width || 1755 params->width < MIN_WIDTH || 1756 params->height > q_dst->coded_height || 1757 params->height < MIN_HEIGHT) 1758 return -EINVAL; 1759 if (!validate_by_version(params->flags, params->version)) 1760 return -EINVAL; 1761 if (!validate_stateless_params_flags(params, q_dst->info)) 1762 return -EINVAL; 1763 return 0; 1764 default: 1765 return 0; 1766 } 1767 return 0; 1768 } 1769 1770 static void update_header_from_stateless_params(struct vicodec_ctx *ctx, 1771 const struct v4l2_ctrl_fwht_params *params) 1772 { 1773 struct fwht_cframe_hdr *p_hdr = &ctx->state.header; 1774 1775 p_hdr->magic1 = FWHT_MAGIC1; 1776 p_hdr->magic2 = FWHT_MAGIC2; 1777 p_hdr->version = htonl(params->version); 1778 p_hdr->width = htonl(params->width); 1779 p_hdr->height = htonl(params->height); 1780 p_hdr->flags = htonl(params->flags); 1781 p_hdr->colorspace = htonl(params->colorspace); 1782 p_hdr->xfer_func = htonl(params->xfer_func); 1783 p_hdr->ycbcr_enc = htonl(params->ycbcr_enc); 1784 p_hdr->quantization = htonl(params->quantization); 1785 } 1786 1787 static int vicodec_s_ctrl(struct v4l2_ctrl *ctrl) 1788 { 1789 struct vicodec_ctx *ctx = container_of(ctrl->handler, 1790 struct vicodec_ctx, hdl); 1791 const struct v4l2_ctrl_fwht_params *params; 1792 1793 switch (ctrl->id) { 1794 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: 1795 ctx->state.gop_size = ctrl->val; 1796 return 0; 1797 case V4L2_CID_FWHT_I_FRAME_QP: 1798 ctx->state.i_frame_qp = ctrl->val; 1799 return 0; 1800 case V4L2_CID_FWHT_P_FRAME_QP: 1801 ctx->state.p_frame_qp = ctrl->val; 1802 return 0; 1803 case V4L2_CID_STATELESS_FWHT_PARAMS: 1804 params = ctrl->p_new.p_fwht_params; 1805 update_header_from_stateless_params(ctx, params); 1806 ctx->state.ref_frame_ts = params->backward_ref_ts; 1807 return 0; 1808 } 1809 return -EINVAL; 1810 } 1811 1812 static const struct v4l2_ctrl_ops vicodec_ctrl_ops = { 1813 .s_ctrl = vicodec_s_ctrl, 1814 .try_ctrl = vicodec_try_ctrl, 1815 }; 1816 1817 static const struct v4l2_ctrl_config vicodec_ctrl_stateless_state = { 1818 .ops = &vicodec_ctrl_ops, 1819 .id = V4L2_CID_STATELESS_FWHT_PARAMS, 1820 .elem_size = sizeof(struct v4l2_ctrl_fwht_params), 1821 }; 1822 1823 /* 1824 * File operations 1825 */ 1826 static int vicodec_open(struct file *file) 1827 { 1828 const struct v4l2_fwht_pixfmt_info *info = v4l2_fwht_get_pixfmt(0); 1829 struct video_device *vfd = video_devdata(file); 1830 struct vicodec_dev *dev = video_drvdata(file); 1831 struct vicodec_ctx *ctx = NULL; 1832 struct v4l2_ctrl_handler *hdl; 1833 unsigned int raw_size; 1834 unsigned int comp_size; 1835 int rc = 0; 1836 1837 if (mutex_lock_interruptible(vfd->lock)) 1838 return -ERESTARTSYS; 1839 ctx = kzalloc_obj(*ctx); 1840 if (!ctx) { 1841 rc = -ENOMEM; 1842 goto open_unlock; 1843 } 1844 1845 if (vfd == &dev->stateful_enc.vfd) 1846 ctx->is_enc = true; 1847 else if (vfd == &dev->stateless_dec.vfd) 1848 ctx->is_stateless = true; 1849 1850 v4l2_fh_init(&ctx->fh, video_devdata(file)); 1851 ctx->dev = dev; 1852 hdl = &ctx->hdl; 1853 v4l2_ctrl_handler_init(hdl, 5); 1854 v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1855 1, 16, 1, 10); 1856 v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_FWHT_I_FRAME_QP, 1857 1, 31, 1, 20); 1858 v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_FWHT_P_FRAME_QP, 1859 1, 31, 1, 20); 1860 1861 if (ctx->is_stateless) 1862 v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_stateless_state, NULL); 1863 else 1864 v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, ctx->is_enc ? 1865 V4L2_CID_MIN_BUFFERS_FOR_OUTPUT : 1866 V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1867 VICODEC_REC_BUFS, VICODEC_REC_BUFS, 1, 1868 VICODEC_REC_BUFS); 1869 1870 if (hdl->error) { 1871 rc = hdl->error; 1872 v4l2_ctrl_handler_free(hdl); 1873 kfree(ctx); 1874 goto open_unlock; 1875 } 1876 ctx->fh.ctrl_handler = hdl; 1877 v4l2_ctrl_handler_setup(hdl); 1878 1879 if (ctx->is_enc) 1880 ctx->q_data[V4L2_M2M_SRC].info = info; 1881 else if (ctx->is_stateless) 1882 ctx->q_data[V4L2_M2M_SRC].info = &pixfmt_stateless_fwht; 1883 else 1884 ctx->q_data[V4L2_M2M_SRC].info = &pixfmt_fwht; 1885 ctx->q_data[V4L2_M2M_SRC].coded_width = 1280; 1886 ctx->q_data[V4L2_M2M_SRC].coded_height = 720; 1887 ctx->q_data[V4L2_M2M_SRC].visible_width = 1280; 1888 ctx->q_data[V4L2_M2M_SRC].visible_height = 720; 1889 raw_size = 1280 * 720 * info->sizeimage_mult / info->sizeimage_div; 1890 comp_size = 1280 * 720 * pixfmt_fwht.sizeimage_mult / 1891 pixfmt_fwht.sizeimage_div; 1892 if (ctx->is_enc) 1893 ctx->q_data[V4L2_M2M_SRC].sizeimage = raw_size; 1894 else if (ctx->is_stateless) 1895 ctx->q_data[V4L2_M2M_SRC].sizeimage = comp_size; 1896 else 1897 ctx->q_data[V4L2_M2M_SRC].sizeimage = 1898 comp_size + sizeof(struct fwht_cframe_hdr); 1899 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC]; 1900 if (ctx->is_enc) { 1901 ctx->q_data[V4L2_M2M_DST].info = &pixfmt_fwht; 1902 ctx->q_data[V4L2_M2M_DST].sizeimage = 1903 comp_size + sizeof(struct fwht_cframe_hdr); 1904 } else { 1905 ctx->q_data[V4L2_M2M_DST].info = info; 1906 ctx->q_data[V4L2_M2M_DST].sizeimage = raw_size; 1907 } 1908 1909 ctx->state.colorspace = V4L2_COLORSPACE_REC709; 1910 1911 if (ctx->is_enc) { 1912 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_enc.m2m_dev, 1913 ctx, &queue_init); 1914 ctx->lock = &dev->stateful_enc.lock; 1915 } else if (ctx->is_stateless) { 1916 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateless_dec.m2m_dev, 1917 ctx, &queue_init); 1918 ctx->lock = &dev->stateless_dec.lock; 1919 } else { 1920 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_dec.m2m_dev, 1921 ctx, &queue_init); 1922 ctx->lock = &dev->stateful_dec.lock; 1923 } 1924 1925 if (IS_ERR(ctx->fh.m2m_ctx)) { 1926 rc = PTR_ERR(ctx->fh.m2m_ctx); 1927 1928 v4l2_ctrl_handler_free(hdl); 1929 v4l2_fh_exit(&ctx->fh); 1930 kfree(ctx); 1931 goto open_unlock; 1932 } 1933 1934 v4l2_fh_add(&ctx->fh, file); 1935 1936 open_unlock: 1937 mutex_unlock(vfd->lock); 1938 return rc; 1939 } 1940 1941 static int vicodec_release(struct file *file) 1942 { 1943 struct video_device *vfd = video_devdata(file); 1944 struct vicodec_ctx *ctx = file2ctx(file); 1945 1946 mutex_lock(vfd->lock); 1947 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 1948 mutex_unlock(vfd->lock); 1949 v4l2_fh_del(&ctx->fh, file); 1950 v4l2_fh_exit(&ctx->fh); 1951 v4l2_ctrl_handler_free(&ctx->hdl); 1952 kvfree(ctx->state.compressed_frame); 1953 kfree(ctx); 1954 1955 return 0; 1956 } 1957 1958 static int vicodec_request_validate(struct media_request *req) 1959 { 1960 struct media_request_object *obj; 1961 struct v4l2_ctrl_handler *parent_hdl, *hdl; 1962 struct vicodec_ctx *ctx = NULL; 1963 struct v4l2_ctrl *ctrl; 1964 unsigned int count; 1965 1966 list_for_each_entry(obj, &req->objects, list) { 1967 struct vb2_buffer *vb; 1968 1969 if (vb2_request_object_is_buffer(obj)) { 1970 vb = container_of(obj, struct vb2_buffer, req_obj); 1971 ctx = vb2_get_drv_priv(vb->vb2_queue); 1972 1973 break; 1974 } 1975 } 1976 1977 if (!ctx) { 1978 pr_err("No buffer was provided with the request\n"); 1979 return -ENOENT; 1980 } 1981 1982 count = vb2_request_buffer_cnt(req); 1983 if (!count) { 1984 v4l2_info(&ctx->dev->v4l2_dev, 1985 "No buffer was provided with the request\n"); 1986 return -ENOENT; 1987 } else if (count > 1) { 1988 v4l2_info(&ctx->dev->v4l2_dev, 1989 "More than one buffer was provided with the request\n"); 1990 return -EINVAL; 1991 } 1992 1993 parent_hdl = &ctx->hdl; 1994 1995 hdl = v4l2_ctrl_request_hdl_find(req, parent_hdl); 1996 if (!hdl) { 1997 v4l2_info(&ctx->dev->v4l2_dev, "Missing codec control\n"); 1998 return -ENOENT; 1999 } 2000 ctrl = v4l2_ctrl_request_hdl_ctrl_find(hdl, 2001 vicodec_ctrl_stateless_state.id); 2002 v4l2_ctrl_request_hdl_put(hdl); 2003 if (!ctrl) { 2004 v4l2_info(&ctx->dev->v4l2_dev, 2005 "Missing required codec control\n"); 2006 return -ENOENT; 2007 } 2008 2009 return vb2_request_validate(req); 2010 } 2011 2012 static void vicodec_request_queue(struct media_request *req) 2013 { 2014 media_request_mark_manual_completion(req); 2015 v4l2_m2m_request_queue(req); 2016 } 2017 2018 static const struct v4l2_file_operations vicodec_fops = { 2019 .owner = THIS_MODULE, 2020 .open = vicodec_open, 2021 .release = vicodec_release, 2022 .poll = v4l2_m2m_fop_poll, 2023 .unlocked_ioctl = video_ioctl2, 2024 .mmap = v4l2_m2m_fop_mmap, 2025 }; 2026 2027 static const struct video_device vicodec_videodev = { 2028 .name = VICODEC_NAME, 2029 .vfl_dir = VFL_DIR_M2M, 2030 .fops = &vicodec_fops, 2031 .ioctl_ops = &vicodec_ioctl_ops, 2032 .minor = -1, 2033 .release = video_device_release_empty, 2034 }; 2035 2036 static const struct media_device_ops vicodec_m2m_media_ops = { 2037 .req_validate = vicodec_request_validate, 2038 .req_queue = vicodec_request_queue, 2039 }; 2040 2041 static const struct v4l2_m2m_ops m2m_ops = { 2042 .device_run = device_run, 2043 .job_ready = job_ready, 2044 }; 2045 2046 static int register_instance(struct vicodec_dev *dev, 2047 struct vicodec_dev_instance *dev_instance, 2048 const char *name, bool is_enc, bool is_stateless) 2049 { 2050 struct video_device *vfd; 2051 int ret; 2052 2053 spin_lock_init(&dev_instance->lock); 2054 mutex_init(&dev_instance->mutex); 2055 dev_instance->m2m_dev = v4l2_m2m_init(&m2m_ops); 2056 if (IS_ERR(dev_instance->m2m_dev)) { 2057 v4l2_err(&dev->v4l2_dev, "Failed to init vicodec enc device\n"); 2058 return PTR_ERR(dev_instance->m2m_dev); 2059 } 2060 2061 dev_instance->vfd = vicodec_videodev; 2062 vfd = &dev_instance->vfd; 2063 vfd->lock = &dev_instance->mutex; 2064 vfd->v4l2_dev = &dev->v4l2_dev; 2065 strscpy(vfd->name, name, sizeof(vfd->name)); 2066 vfd->device_caps = V4L2_CAP_STREAMING | 2067 (multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M); 2068 if (is_enc || is_stateless) { 2069 v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD); 2070 v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD); 2071 } 2072 if (!is_enc) { 2073 v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD); 2074 v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD); 2075 } 2076 video_set_drvdata(vfd, dev); 2077 2078 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0); 2079 if (ret) { 2080 v4l2_err(&dev->v4l2_dev, "Failed to register video device '%s'\n", name); 2081 v4l2_m2m_release(dev_instance->m2m_dev); 2082 return ret; 2083 } 2084 v4l2_info(&dev->v4l2_dev, "Device '%s' registered as /dev/video%d\n", 2085 name, vfd->num); 2086 return 0; 2087 } 2088 2089 static void vicodec_v4l2_dev_release(struct v4l2_device *v4l2_dev) 2090 { 2091 struct vicodec_dev *dev = container_of(v4l2_dev, struct vicodec_dev, v4l2_dev); 2092 2093 v4l2_device_unregister(&dev->v4l2_dev); 2094 v4l2_m2m_release(dev->stateful_enc.m2m_dev); 2095 v4l2_m2m_release(dev->stateful_dec.m2m_dev); 2096 v4l2_m2m_release(dev->stateless_dec.m2m_dev); 2097 #ifdef CONFIG_MEDIA_CONTROLLER 2098 media_device_cleanup(&dev->mdev); 2099 #endif 2100 kfree(dev); 2101 } 2102 2103 static int vicodec_probe(struct platform_device *pdev) 2104 { 2105 struct vicodec_dev *dev; 2106 int ret; 2107 2108 dev = kzalloc_obj(*dev); 2109 if (!dev) 2110 return -ENOMEM; 2111 2112 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 2113 if (ret) 2114 goto free_dev; 2115 2116 dev->v4l2_dev.release = vicodec_v4l2_dev_release; 2117 2118 #ifdef CONFIG_MEDIA_CONTROLLER 2119 dev->mdev.dev = &pdev->dev; 2120 strscpy(dev->mdev.model, "vicodec", sizeof(dev->mdev.model)); 2121 strscpy(dev->mdev.bus_info, "platform:vicodec", 2122 sizeof(dev->mdev.bus_info)); 2123 media_device_init(&dev->mdev); 2124 dev->mdev.ops = &vicodec_m2m_media_ops; 2125 dev->v4l2_dev.mdev = &dev->mdev; 2126 #endif 2127 2128 platform_set_drvdata(pdev, dev); 2129 2130 ret = register_instance(dev, &dev->stateful_enc, "stateful-encoder", 2131 true, false); 2132 if (ret) 2133 goto unreg_dev; 2134 2135 ret = register_instance(dev, &dev->stateful_dec, "stateful-decoder", 2136 false, false); 2137 if (ret) 2138 goto unreg_sf_enc; 2139 2140 ret = register_instance(dev, &dev->stateless_dec, "stateless-decoder", 2141 false, true); 2142 if (ret) 2143 goto unreg_sf_dec; 2144 2145 #ifdef CONFIG_MEDIA_CONTROLLER 2146 ret = v4l2_m2m_register_media_controller(dev->stateful_enc.m2m_dev, 2147 &dev->stateful_enc.vfd, 2148 MEDIA_ENT_F_PROC_VIDEO_ENCODER); 2149 if (ret) { 2150 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for enc\n"); 2151 goto unreg_m2m; 2152 } 2153 2154 ret = v4l2_m2m_register_media_controller(dev->stateful_dec.m2m_dev, 2155 &dev->stateful_dec.vfd, 2156 MEDIA_ENT_F_PROC_VIDEO_DECODER); 2157 if (ret) { 2158 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for dec\n"); 2159 goto unreg_m2m_sf_enc_mc; 2160 } 2161 2162 ret = v4l2_m2m_register_media_controller(dev->stateless_dec.m2m_dev, 2163 &dev->stateless_dec.vfd, 2164 MEDIA_ENT_F_PROC_VIDEO_DECODER); 2165 if (ret) { 2166 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for stateless dec\n"); 2167 goto unreg_m2m_sf_dec_mc; 2168 } 2169 2170 ret = media_device_register(&dev->mdev); 2171 if (ret) { 2172 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n"); 2173 goto unreg_m2m_sl_dec_mc; 2174 } 2175 #endif 2176 return 0; 2177 2178 #ifdef CONFIG_MEDIA_CONTROLLER 2179 unreg_m2m_sl_dec_mc: 2180 v4l2_m2m_unregister_media_controller(dev->stateless_dec.m2m_dev); 2181 unreg_m2m_sf_dec_mc: 2182 v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev); 2183 unreg_m2m_sf_enc_mc: 2184 v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev); 2185 unreg_m2m: 2186 video_unregister_device(&dev->stateless_dec.vfd); 2187 v4l2_m2m_release(dev->stateless_dec.m2m_dev); 2188 #endif 2189 unreg_sf_dec: 2190 video_unregister_device(&dev->stateful_dec.vfd); 2191 v4l2_m2m_release(dev->stateful_dec.m2m_dev); 2192 unreg_sf_enc: 2193 video_unregister_device(&dev->stateful_enc.vfd); 2194 v4l2_m2m_release(dev->stateful_enc.m2m_dev); 2195 unreg_dev: 2196 v4l2_device_unregister(&dev->v4l2_dev); 2197 free_dev: 2198 kfree(dev); 2199 2200 return ret; 2201 } 2202 2203 static void vicodec_remove(struct platform_device *pdev) 2204 { 2205 struct vicodec_dev *dev = platform_get_drvdata(pdev); 2206 2207 v4l2_info(&dev->v4l2_dev, "Removing " VICODEC_NAME); 2208 2209 #ifdef CONFIG_MEDIA_CONTROLLER 2210 media_device_unregister(&dev->mdev); 2211 v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev); 2212 v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev); 2213 v4l2_m2m_unregister_media_controller(dev->stateless_dec.m2m_dev); 2214 #endif 2215 2216 video_unregister_device(&dev->stateful_enc.vfd); 2217 video_unregister_device(&dev->stateful_dec.vfd); 2218 video_unregister_device(&dev->stateless_dec.vfd); 2219 v4l2_device_put(&dev->v4l2_dev); 2220 } 2221 2222 static struct platform_driver vicodec_pdrv = { 2223 .probe = vicodec_probe, 2224 .remove = vicodec_remove, 2225 .driver = { 2226 .name = VICODEC_NAME, 2227 }, 2228 }; 2229 2230 static void __exit vicodec_exit(void) 2231 { 2232 platform_driver_unregister(&vicodec_pdrv); 2233 platform_device_unregister(&vicodec_pdev); 2234 } 2235 2236 static int __init vicodec_init(void) 2237 { 2238 int ret; 2239 2240 ret = platform_device_register(&vicodec_pdev); 2241 if (ret) 2242 return ret; 2243 2244 ret = platform_driver_register(&vicodec_pdrv); 2245 if (ret) 2246 platform_device_unregister(&vicodec_pdev); 2247 2248 return ret; 2249 } 2250 2251 module_init(vicodec_init); 2252 module_exit(vicodec_exit); 2253