xref: /linux/drivers/media/test-drivers/visl/visl-core.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A virtual stateless decoder device for stateless uAPI development purposes.
4  *
5  * This tool's objective is to help the development and testing of userspace
6  * applications that use the V4L2 stateless API to decode media.
7  *
8  * A userspace implementation can use visl to run a decoding loop even when no
9  * hardware is available or when the kernel uAPI for the codec has not been
10  * upstreamed yet. This can reveal bugs at an early stage.
11  *
12  * This driver can also trace the contents of the V4L2 controls submitted to it.
13  * It can also dump the contents of the vb2 buffers through a debugfs
14  * interface. This is in many ways similar to the tracing infrastructure
15  * available for other popular encode/decode APIs out there and can help develop
16  * a userspace application by using another (working) one as a reference.
17  *
18  * Note that no actual decoding of video frames is performed by visl. The V4L2
19  * test pattern generator is used to write various debug information to the
20  * capture buffers instead.
21  *
22  * Copyright (C) 2022 Collabora, Ltd.
23  *
24  * Based on the vim2m driver, that is:
25  *
26  * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
27  * Pawel Osciak, <pawel@osciak.com>
28  * Marek Szyprowski, <m.szyprowski@samsung.com>
29  *
30  * Based on the vicodec driver, that is:
31  *
32  * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
33  *
34  * Based on the Cedrus VPU driver, that is:
35  *
36  * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
37  * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
38  * Copyright (C) 2018 Bootlin
39  */
40 
41 #include <linux/debugfs.h>
42 #include <linux/module.h>
43 #include <linux/platform_device.h>
44 #include <media/v4l2-ctrls.h>
45 #include <media/v4l2-device.h>
46 #include <media/v4l2-ioctl.h>
47 #include <media/v4l2-mem2mem.h>
48 
49 #include "visl.h"
50 #include "visl-dec.h"
51 #include "visl-debugfs.h"
52 #include "visl-video.h"
53 
54 unsigned int visl_debug;
55 module_param(visl_debug, uint, 0644);
56 MODULE_PARM_DESC(visl_debug, " activates debug info");
57 
58 unsigned int visl_transtime_ms;
59 module_param(visl_transtime_ms, uint, 0644);
60 MODULE_PARM_DESC(visl_transtime_ms, " simulated process time in milliseconds.");
61 
62 /*
63  * dprintk can be slow through serial. This lets one limit the tracing to a
64  * particular number of frames
65  */
66 int visl_dprintk_frame_start = -1;
67 module_param(visl_dprintk_frame_start, int, 0);
68 MODULE_PARM_DESC(visl_dprintk_frame_start,
69 		 " a frame number to start tracing with dprintk");
70 
71 unsigned int visl_dprintk_nframes;
72 module_param(visl_dprintk_nframes, uint, 0);
73 MODULE_PARM_DESC(visl_dprintk_nframes,
74 		 " the number of frames to trace with dprintk");
75 
76 bool keep_bitstream_buffers;
77 module_param(keep_bitstream_buffers, bool, false);
78 MODULE_PARM_DESC(keep_bitstream_buffers,
79 		 " keep bitstream buffers in debugfs after streaming is stopped");
80 
81 int bitstream_trace_frame_start = -1;
82 module_param(bitstream_trace_frame_start, int, 0);
83 MODULE_PARM_DESC(bitstream_trace_frame_start,
84 		 " a frame number to start dumping the bitstream through debugfs");
85 
86 unsigned int bitstream_trace_nframes;
87 module_param(bitstream_trace_nframes, uint, 0);
88 MODULE_PARM_DESC(bitstream_trace_nframes,
89 		 " the number of frames to dump the bitstream through debugfs");
90 
91 static const struct visl_ctrl_desc visl_fwht_ctrl_descs[] = {
92 	{
93 		.cfg.id = V4L2_CID_STATELESS_FWHT_PARAMS,
94 	},
95 };
96 
97 const struct visl_ctrls visl_fwht_ctrls = {
98 	.ctrls = visl_fwht_ctrl_descs,
99 	.num_ctrls = ARRAY_SIZE(visl_fwht_ctrl_descs)
100 };
101 
102 static const struct visl_ctrl_desc visl_mpeg2_ctrl_descs[] = {
103 	{
104 		.cfg.id = V4L2_CID_STATELESS_MPEG2_SEQUENCE,
105 	},
106 	{
107 		.cfg.id = V4L2_CID_STATELESS_MPEG2_PICTURE,
108 	},
109 	{
110 		.cfg.id = V4L2_CID_STATELESS_MPEG2_QUANTISATION,
111 	},
112 };
113 
114 const struct visl_ctrls visl_mpeg2_ctrls = {
115 	.ctrls = visl_mpeg2_ctrl_descs,
116 	.num_ctrls = ARRAY_SIZE(visl_mpeg2_ctrl_descs),
117 };
118 
119 static const struct visl_ctrl_desc visl_vp8_ctrl_descs[] = {
120 	{
121 		.cfg.id = V4L2_CID_STATELESS_VP8_FRAME,
122 	},
123 };
124 
125 const struct visl_ctrls visl_vp8_ctrls = {
126 	.ctrls = visl_vp8_ctrl_descs,
127 	.num_ctrls = ARRAY_SIZE(visl_vp8_ctrl_descs),
128 };
129 
130 static const struct visl_ctrl_desc visl_vp9_ctrl_descs[] = {
131 	{
132 		.cfg.id = V4L2_CID_STATELESS_VP9_FRAME,
133 	},
134 	{
135 		.cfg.id = V4L2_CID_STATELESS_VP9_COMPRESSED_HDR,
136 	},
137 };
138 
139 const struct visl_ctrls visl_vp9_ctrls = {
140 	.ctrls = visl_vp9_ctrl_descs,
141 	.num_ctrls = ARRAY_SIZE(visl_vp9_ctrl_descs),
142 };
143 
144 static const struct visl_ctrl_desc visl_h264_ctrl_descs[] = {
145 	{
146 		.cfg.id = V4L2_CID_STATELESS_H264_DECODE_PARAMS,
147 	},
148 	{
149 		.cfg.id = V4L2_CID_STATELESS_H264_SPS,
150 	},
151 	{
152 		.cfg.id = V4L2_CID_STATELESS_H264_PPS,
153 	},
154 	{
155 		.cfg.id = V4L2_CID_STATELESS_H264_SCALING_MATRIX,
156 	},
157 	{
158 		.cfg.id = V4L2_CID_STATELESS_H264_DECODE_MODE,
159 	},
160 	{
161 		.cfg.id = V4L2_CID_STATELESS_H264_START_CODE,
162 	},
163 	{
164 		.cfg.id = V4L2_CID_STATELESS_H264_SLICE_PARAMS,
165 	},
166 	{
167 		.cfg.id = V4L2_CID_STATELESS_H264_PRED_WEIGHTS,
168 	},
169 };
170 
171 const struct visl_ctrls visl_h264_ctrls = {
172 	.ctrls = visl_h264_ctrl_descs,
173 	.num_ctrls = ARRAY_SIZE(visl_h264_ctrl_descs),
174 };
175 
176 static const struct visl_ctrl_desc visl_hevc_ctrl_descs[] = {
177 	{
178 		.cfg.id = V4L2_CID_STATELESS_HEVC_SPS,
179 	},
180 	{
181 		.cfg.id = V4L2_CID_STATELESS_HEVC_PPS,
182 	},
183 	{
184 		.cfg.id = V4L2_CID_STATELESS_HEVC_SLICE_PARAMS,
185 		/* The absolute maximum for level > 6 */
186 		.cfg.dims = { 600 },
187 	},
188 	{
189 		.cfg.id = V4L2_CID_STATELESS_HEVC_SCALING_MATRIX,
190 	},
191 	{
192 		.cfg.id = V4L2_CID_STATELESS_HEVC_DECODE_PARAMS,
193 	},
194 	{
195 		.cfg.id = V4L2_CID_STATELESS_HEVC_DECODE_MODE,
196 	},
197 	{
198 		.cfg.id = V4L2_CID_STATELESS_HEVC_START_CODE,
199 	},
200 	{
201 		.cfg.id = V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS,
202 		.cfg.dims = { 256 },
203 		.cfg.max = 0xffffffff,
204 		.cfg.step = 1,
205 	},
206 
207 };
208 
209 const struct visl_ctrls visl_hevc_ctrls = {
210 	.ctrls = visl_hevc_ctrl_descs,
211 	.num_ctrls = ARRAY_SIZE(visl_hevc_ctrl_descs),
212 };
213 
214 static const struct visl_ctrl_desc visl_av1_ctrl_descs[] = {
215 	{
216 		.cfg.id = V4L2_CID_STATELESS_AV1_FRAME,
217 	},
218 	{
219 		.cfg.id = V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY,
220 		.cfg.dims = { V4L2_AV1_MAX_TILE_COUNT },
221 	},
222 	{
223 		.cfg.id = V4L2_CID_STATELESS_AV1_SEQUENCE,
224 	},
225 	{
226 		.cfg.id = V4L2_CID_STATELESS_AV1_FILM_GRAIN,
227 	},
228 };
229 
230 const struct visl_ctrls visl_av1_ctrls = {
231 	.ctrls = visl_av1_ctrl_descs,
232 	.num_ctrls = ARRAY_SIZE(visl_av1_ctrl_descs),
233 };
234 
235 struct v4l2_ctrl *visl_find_control(struct visl_ctx *ctx, u32 id)
236 {
237 	struct v4l2_ctrl_handler *hdl = &ctx->hdl;
238 
239 	return v4l2_ctrl_find(hdl, id);
240 }
241 
242 void *visl_find_control_data(struct visl_ctx *ctx, u32 id)
243 {
244 	struct v4l2_ctrl *ctrl;
245 
246 	ctrl = visl_find_control(ctx, id);
247 	if (ctrl)
248 		return ctrl->p_cur.p;
249 
250 	return NULL;
251 }
252 
253 u32 visl_control_num_elems(struct visl_ctx *ctx, u32 id)
254 {
255 	struct v4l2_ctrl *ctrl;
256 
257 	ctrl = visl_find_control(ctx, id);
258 	if (ctrl)
259 		return ctrl->elems;
260 
261 	return 0;
262 }
263 
264 static void visl_device_release(struct video_device *vdev)
265 {
266 	struct visl_dev *dev = container_of(vdev, struct visl_dev, vfd);
267 
268 	v4l2_device_unregister(&dev->v4l2_dev);
269 	v4l2_m2m_release(dev->m2m_dev);
270 	media_device_cleanup(&dev->mdev);
271 	visl_debugfs_deinit(dev);
272 	kfree(dev);
273 }
274 
275 #define VISL_CONTROLS_COUNT	ARRAY_SIZE(visl_controls)
276 
277 static int visl_init_ctrls(struct visl_ctx *ctx)
278 {
279 	struct visl_dev *dev = ctx->dev;
280 	struct v4l2_ctrl_handler *hdl = &ctx->hdl;
281 	unsigned int ctrl_cnt = 0;
282 	unsigned int i;
283 	unsigned int j;
284 	const struct visl_ctrls *ctrls;
285 
286 	for (i = 0; i < num_coded_fmts; i++)
287 		ctrl_cnt += visl_coded_fmts[i].ctrls->num_ctrls;
288 
289 	v4l2_ctrl_handler_init(hdl, ctrl_cnt);
290 
291 	for (i = 0; i < num_coded_fmts; i++) {
292 		ctrls = visl_coded_fmts[i].ctrls;
293 		for (j = 0; j < ctrls->num_ctrls; j++)
294 			v4l2_ctrl_new_custom(hdl, &ctrls->ctrls[j].cfg, NULL);
295 	}
296 
297 	if (hdl->error) {
298 		v4l2_err(&dev->v4l2_dev,
299 			 "Failed to initialize control handler\n");
300 		v4l2_ctrl_handler_free(hdl);
301 		return hdl->error;
302 	}
303 
304 	ctx->fh.ctrl_handler = hdl;
305 	v4l2_ctrl_handler_setup(hdl);
306 
307 	return 0;
308 }
309 
310 static int visl_open(struct file *file)
311 {
312 	struct visl_dev *dev = video_drvdata(file);
313 	struct visl_ctx *ctx = NULL;
314 	int rc = 0;
315 
316 	if (mutex_lock_interruptible(&dev->dev_mutex))
317 		return -ERESTARTSYS;
318 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
319 	if (!ctx) {
320 		rc = -ENOMEM;
321 		goto unlock;
322 	}
323 
324 	ctx->tpg_str_buf = kzalloc(TPG_STR_BUF_SZ, GFP_KERNEL);
325 
326 	v4l2_fh_init(&ctx->fh, video_devdata(file));
327 	file->private_data = &ctx->fh;
328 	ctx->dev = dev;
329 
330 	rc = visl_init_ctrls(ctx);
331 	if (rc)
332 		goto free_ctx;
333 
334 	ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &visl_queue_init);
335 
336 	mutex_init(&ctx->vb_mutex);
337 
338 	if (IS_ERR(ctx->fh.m2m_ctx)) {
339 		rc = PTR_ERR(ctx->fh.m2m_ctx);
340 		goto free_hdl;
341 	}
342 
343 	rc = visl_set_default_format(ctx);
344 	if (rc)
345 		goto free_m2m_ctx;
346 
347 	v4l2_fh_add(&ctx->fh);
348 
349 	dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
350 		ctx, ctx->fh.m2m_ctx);
351 
352 	mutex_unlock(&dev->dev_mutex);
353 	return rc;
354 
355 free_m2m_ctx:
356 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
357 free_hdl:
358 	v4l2_ctrl_handler_free(&ctx->hdl);
359 	v4l2_fh_exit(&ctx->fh);
360 free_ctx:
361 	kfree(ctx->tpg_str_buf);
362 	kfree(ctx);
363 unlock:
364 	mutex_unlock(&dev->dev_mutex);
365 	return rc;
366 }
367 
368 static int visl_release(struct file *file)
369 {
370 	struct visl_dev *dev = video_drvdata(file);
371 	struct visl_ctx *ctx = visl_file_to_ctx(file);
372 
373 	dprintk(dev, "Releasing instance %p\n", ctx);
374 
375 	tpg_free(&ctx->tpg);
376 	v4l2_fh_del(&ctx->fh);
377 	v4l2_fh_exit(&ctx->fh);
378 	v4l2_ctrl_handler_free(&ctx->hdl);
379 	mutex_lock(&dev->dev_mutex);
380 	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
381 	mutex_unlock(&dev->dev_mutex);
382 
383 	kfree(ctx->tpg_str_buf);
384 	kfree(ctx);
385 
386 	return 0;
387 }
388 
389 static const struct v4l2_file_operations visl_fops = {
390 	.owner		= THIS_MODULE,
391 	.open		= visl_open,
392 	.release	= visl_release,
393 	.poll		= v4l2_m2m_fop_poll,
394 	.unlocked_ioctl	= video_ioctl2,
395 	.mmap		= v4l2_m2m_fop_mmap,
396 };
397 
398 static const struct video_device visl_videodev = {
399 	.name		= VISL_NAME,
400 	.vfl_dir	= VFL_DIR_M2M,
401 	.fops		= &visl_fops,
402 	.ioctl_ops	= &visl_ioctl_ops,
403 	.minor		= -1,
404 	.release	= visl_device_release,
405 	.device_caps	= V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING,
406 };
407 
408 static const struct v4l2_m2m_ops visl_m2m_ops = {
409 	.device_run	= visl_device_run,
410 };
411 
412 static const struct media_device_ops visl_m2m_media_ops = {
413 	.req_validate	= visl_request_validate,
414 	.req_queue	= v4l2_m2m_request_queue,
415 };
416 
417 static int visl_probe(struct platform_device *pdev)
418 {
419 	struct visl_dev *dev;
420 	struct video_device *vfd;
421 	int ret;
422 	int rc;
423 
424 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
425 	if (!dev)
426 		return -ENOMEM;
427 
428 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
429 	if (ret)
430 		goto error_visl_dev;
431 
432 	mutex_init(&dev->dev_mutex);
433 
434 	dev->vfd = visl_videodev;
435 	vfd = &dev->vfd;
436 	vfd->lock = &dev->dev_mutex;
437 	vfd->v4l2_dev = &dev->v4l2_dev;
438 
439 	video_set_drvdata(vfd, dev);
440 
441 	platform_set_drvdata(pdev, dev);
442 
443 	dev->m2m_dev = v4l2_m2m_init(&visl_m2m_ops);
444 	if (IS_ERR(dev->m2m_dev)) {
445 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
446 		ret = PTR_ERR(dev->m2m_dev);
447 		dev->m2m_dev = NULL;
448 		goto error_dev;
449 	}
450 
451 	dev->mdev.dev = &pdev->dev;
452 	strscpy(dev->mdev.model, "visl", sizeof(dev->mdev.model));
453 	strscpy(dev->mdev.bus_info, "platform:visl",
454 		sizeof(dev->mdev.bus_info));
455 	media_device_init(&dev->mdev);
456 	dev->mdev.ops = &visl_m2m_media_ops;
457 	dev->v4l2_dev.mdev = &dev->mdev;
458 
459 	ret = video_register_device(vfd, VFL_TYPE_VIDEO, -1);
460 	if (ret) {
461 		v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
462 		goto error_m2m;
463 	}
464 
465 	v4l2_info(&dev->v4l2_dev,
466 		  "Device registered as /dev/video%d\n", vfd->num);
467 
468 	ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
469 						 MEDIA_ENT_F_PROC_VIDEO_DECODER);
470 	if (ret) {
471 		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
472 		goto error_v4l2;
473 	}
474 
475 	ret = media_device_register(&dev->mdev);
476 	if (ret) {
477 		v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
478 		goto error_m2m_mc;
479 	}
480 
481 	rc = visl_debugfs_init(dev);
482 	if (rc)
483 		dprintk(dev, "visl_debugfs_init failed: %d\n"
484 			"Continuing without debugfs support\n", rc);
485 
486 	return 0;
487 
488 error_m2m_mc:
489 	v4l2_m2m_unregister_media_controller(dev->m2m_dev);
490 error_v4l2:
491 	video_unregister_device(&dev->vfd);
492 	/* visl_device_release called by video_unregister_device to release various objects */
493 	return ret;
494 error_m2m:
495 	v4l2_m2m_release(dev->m2m_dev);
496 error_dev:
497 	v4l2_device_unregister(&dev->v4l2_dev);
498 error_visl_dev:
499 	kfree(dev);
500 
501 	return ret;
502 }
503 
504 static void visl_remove(struct platform_device *pdev)
505 {
506 	struct visl_dev *dev = platform_get_drvdata(pdev);
507 
508 	v4l2_info(&dev->v4l2_dev, "Removing " VISL_NAME);
509 
510 #ifdef CONFIG_MEDIA_CONTROLLER
511 	if (media_devnode_is_registered(dev->mdev.devnode)) {
512 		media_device_unregister(&dev->mdev);
513 		v4l2_m2m_unregister_media_controller(dev->m2m_dev);
514 	}
515 #endif
516 	video_unregister_device(&dev->vfd);
517 }
518 
519 static struct platform_driver visl_pdrv = {
520 	.probe		= visl_probe,
521 	.remove_new	= visl_remove,
522 	.driver		= {
523 		.name	= VISL_NAME,
524 	},
525 };
526 
527 static void visl_dev_release(struct device *dev) {}
528 
529 static struct platform_device visl_pdev = {
530 	.name		= VISL_NAME,
531 	.dev.release	= visl_dev_release,
532 };
533 
534 static void __exit visl_exit(void)
535 {
536 	platform_driver_unregister(&visl_pdrv);
537 	platform_device_unregister(&visl_pdev);
538 }
539 
540 static int __init visl_init(void)
541 {
542 	int ret;
543 
544 	ret = platform_device_register(&visl_pdev);
545 	if (ret)
546 		return ret;
547 
548 	ret = platform_driver_register(&visl_pdrv);
549 	if (ret)
550 		platform_device_unregister(&visl_pdev);
551 
552 	return ret;
553 }
554 
555 MODULE_DESCRIPTION("Virtual stateless decoder device");
556 MODULE_AUTHOR("Daniel Almeida <daniel.almeida@collabora.com>");
557 MODULE_LICENSE("GPL");
558 
559 module_init(visl_init);
560 module_exit(visl_exit);
561