10c078e31SDaniel Almeida /* SPDX-License-Identifier: GPL-2.0 */
20c078e31SDaniel Almeida /*
30c078e31SDaniel Almeida * A virtual stateless device for stateless uAPI development purposes.
40c078e31SDaniel Almeida *
50c078e31SDaniel Almeida * This tool's objective is to help the development and testing of userspace
60c078e31SDaniel Almeida * applications that use the V4L2 stateless API to decode media.
70c078e31SDaniel Almeida *
80c078e31SDaniel Almeida * A userspace implementation can use visl to run a decoding loop even when no
90c078e31SDaniel Almeida * hardware is available or when the kernel uAPI for the codec has not been
100c078e31SDaniel Almeida * upstreamed yet. This can reveal bugs at an early stage.
110c078e31SDaniel Almeida *
120c078e31SDaniel Almeida * This driver can also trace the contents of the V4L2 controls submitted to it.
130c078e31SDaniel Almeida * It can also dump the contents of the vb2 buffers through a debugfs
140c078e31SDaniel Almeida * interface. This is in many ways similar to the tracing infrastructure
150c078e31SDaniel Almeida * available for other popular encode/decode APIs out there and can help develop
160c078e31SDaniel Almeida * a userspace application by using another (working) one as a reference.
170c078e31SDaniel Almeida *
180c078e31SDaniel Almeida * Note that no actual decoding of video frames is performed by visl. The V4L2
190c078e31SDaniel Almeida * test pattern generator is used to write various debug information to the
200c078e31SDaniel Almeida * capture buffers instead.
210c078e31SDaniel Almeida *
220c078e31SDaniel Almeida * Copyright (C) 2022 Collabora, Ltd.
230c078e31SDaniel Almeida *
240c078e31SDaniel Almeida * Based on the vim2m driver, that is:
250c078e31SDaniel Almeida *
260c078e31SDaniel Almeida * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
270c078e31SDaniel Almeida * Pawel Osciak, <pawel@osciak.com>
280c078e31SDaniel Almeida * Marek Szyprowski, <m.szyprowski@samsung.com>
290c078e31SDaniel Almeida *
300c078e31SDaniel Almeida * Based on the vicodec driver, that is:
310c078e31SDaniel Almeida *
320c078e31SDaniel Almeida * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
330c078e31SDaniel Almeida *
340c078e31SDaniel Almeida * Based on the Cedrus VPU driver, that is:
350c078e31SDaniel Almeida *
360c078e31SDaniel Almeida * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
370c078e31SDaniel Almeida * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
380c078e31SDaniel Almeida * Copyright (C) 2018 Bootlin
390c078e31SDaniel Almeida */
400c078e31SDaniel Almeida
410c078e31SDaniel Almeida #ifndef _VISL_H_
420c078e31SDaniel Almeida #define _VISL_H_
430c078e31SDaniel Almeida
440c078e31SDaniel Almeida #include <linux/debugfs.h>
450c078e31SDaniel Almeida #include <linux/list.h>
460c078e31SDaniel Almeida
470c078e31SDaniel Almeida #include <media/v4l2-ctrls.h>
480c078e31SDaniel Almeida #include <media/v4l2-device.h>
490c078e31SDaniel Almeida #include <media/tpg/v4l2-tpg.h>
500c078e31SDaniel Almeida
510c078e31SDaniel Almeida #define VISL_NAME "visl"
520c078e31SDaniel Almeida #define VISL_M2M_NQUEUES 2
530c078e31SDaniel Almeida
540c078e31SDaniel Almeida #define TPG_STR_BUF_SZ 2048
550c078e31SDaniel Almeida
560c078e31SDaniel Almeida extern unsigned int visl_transtime_ms;
570c078e31SDaniel Almeida
580c078e31SDaniel Almeida struct visl_ctrls {
590c078e31SDaniel Almeida const struct visl_ctrl_desc *ctrls;
600c078e31SDaniel Almeida unsigned int num_ctrls;
610c078e31SDaniel Almeida };
620c078e31SDaniel Almeida
630c078e31SDaniel Almeida struct visl_coded_format_desc {
640c078e31SDaniel Almeida u32 pixelformat;
650c078e31SDaniel Almeida struct v4l2_frmsize_stepwise frmsize;
660c078e31SDaniel Almeida const struct visl_ctrls *ctrls;
670c078e31SDaniel Almeida unsigned int num_decoded_fmts;
680c078e31SDaniel Almeida const u32 *decoded_fmts;
690c078e31SDaniel Almeida };
700c078e31SDaniel Almeida
710c078e31SDaniel Almeida extern const struct visl_coded_format_desc visl_coded_fmts[];
720c078e31SDaniel Almeida extern const size_t num_coded_fmts;
730c078e31SDaniel Almeida
740c078e31SDaniel Almeida enum {
750c078e31SDaniel Almeida V4L2_M2M_SRC = 0,
760c078e31SDaniel Almeida V4L2_M2M_DST = 1,
770c078e31SDaniel Almeida };
780c078e31SDaniel Almeida
790c078e31SDaniel Almeida extern unsigned int visl_debug;
800c078e31SDaniel Almeida #define dprintk(dev, fmt, arg...) \
810c078e31SDaniel Almeida v4l2_dbg(1, visl_debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
820c078e31SDaniel Almeida
830c078e31SDaniel Almeida extern int visl_dprintk_frame_start;
840c078e31SDaniel Almeida extern unsigned int visl_dprintk_nframes;
850c078e31SDaniel Almeida extern bool keep_bitstream_buffers;
860c078e31SDaniel Almeida extern int bitstream_trace_frame_start;
870c078e31SDaniel Almeida extern unsigned int bitstream_trace_nframes;
88*8901f20bSDetlev Casanova extern bool tpg_verbose;
890c078e31SDaniel Almeida
900c078e31SDaniel Almeida #define frame_dprintk(dev, current, fmt, arg...) \
910c078e31SDaniel Almeida do { \
920c078e31SDaniel Almeida if (visl_dprintk_frame_start > -1 && \
930c078e31SDaniel Almeida (current) >= visl_dprintk_frame_start && \
940c078e31SDaniel Almeida (current) < visl_dprintk_frame_start + visl_dprintk_nframes) \
950c078e31SDaniel Almeida dprintk(dev, fmt, ## arg); \
960c078e31SDaniel Almeida } while (0) \
970c078e31SDaniel Almeida
980c078e31SDaniel Almeida struct visl_q_data {
990c078e31SDaniel Almeida unsigned int sequence;
1000c078e31SDaniel Almeida };
1010c078e31SDaniel Almeida
1020c078e31SDaniel Almeida struct visl_dev {
1030c078e31SDaniel Almeida struct v4l2_device v4l2_dev;
1040c078e31SDaniel Almeida struct video_device vfd;
1050c078e31SDaniel Almeida #ifdef CONFIG_MEDIA_CONTROLLER
1060c078e31SDaniel Almeida struct media_device mdev;
1070c078e31SDaniel Almeida #endif
1080c078e31SDaniel Almeida
1090c078e31SDaniel Almeida struct mutex dev_mutex;
1100c078e31SDaniel Almeida
1110c078e31SDaniel Almeida struct v4l2_m2m_dev *m2m_dev;
1120c078e31SDaniel Almeida
1130c078e31SDaniel Almeida #ifdef CONFIG_VISL_DEBUGFS
1140c078e31SDaniel Almeida struct dentry *debugfs_root;
1150c078e31SDaniel Almeida struct dentry *bitstream_debugfs;
1160c078e31SDaniel Almeida struct list_head bitstream_blobs;
1170c078e31SDaniel Almeida
1180c078e31SDaniel Almeida /* Protects the "blob" list */
1190c078e31SDaniel Almeida struct mutex bitstream_lock;
1200c078e31SDaniel Almeida #endif
1210c078e31SDaniel Almeida };
1220c078e31SDaniel Almeida
1230c078e31SDaniel Almeida enum visl_codec {
1240c078e31SDaniel Almeida VISL_CODEC_NONE,
1250c078e31SDaniel Almeida VISL_CODEC_FWHT,
1260c078e31SDaniel Almeida VISL_CODEC_MPEG2,
1270c078e31SDaniel Almeida VISL_CODEC_VP8,
1280c078e31SDaniel Almeida VISL_CODEC_VP9,
1290c078e31SDaniel Almeida VISL_CODEC_H264,
1300c078e31SDaniel Almeida VISL_CODEC_HEVC,
13198b3cd0bSDetlev Casanova VISL_CODEC_AV1,
1320c078e31SDaniel Almeida };
1330c078e31SDaniel Almeida
1340c078e31SDaniel Almeida struct visl_blob {
1350c078e31SDaniel Almeida struct list_head list;
1360c078e31SDaniel Almeida struct dentry *dentry;
1370c078e31SDaniel Almeida struct debugfs_blob_wrapper blob;
1380c078e31SDaniel Almeida };
1390c078e31SDaniel Almeida
1400c078e31SDaniel Almeida struct visl_ctx {
1410c078e31SDaniel Almeida struct v4l2_fh fh;
1420c078e31SDaniel Almeida struct visl_dev *dev;
1430c078e31SDaniel Almeida struct v4l2_ctrl_handler hdl;
1440c078e31SDaniel Almeida
1450c078e31SDaniel Almeida struct mutex vb_mutex;
1460c078e31SDaniel Almeida
1470c078e31SDaniel Almeida struct visl_q_data q_data[VISL_M2M_NQUEUES];
1480c078e31SDaniel Almeida enum visl_codec current_codec;
1490c078e31SDaniel Almeida
1500c078e31SDaniel Almeida const struct visl_coded_format_desc *coded_format_desc;
1510c078e31SDaniel Almeida
1520c078e31SDaniel Almeida struct v4l2_format coded_fmt;
1530c078e31SDaniel Almeida struct v4l2_format decoded_fmt;
1540c078e31SDaniel Almeida
1550c078e31SDaniel Almeida struct tpg_data tpg;
1560c078e31SDaniel Almeida u64 capture_streamon_jiffies;
1570c078e31SDaniel Almeida char *tpg_str_buf;
1580c078e31SDaniel Almeida };
1590c078e31SDaniel Almeida
1600c078e31SDaniel Almeida struct visl_ctrl_desc {
1610c078e31SDaniel Almeida struct v4l2_ctrl_config cfg;
1620c078e31SDaniel Almeida };
1630c078e31SDaniel Almeida
visl_file_to_ctx(struct file * file)1640c078e31SDaniel Almeida static inline struct visl_ctx *visl_file_to_ctx(struct file *file)
1650c078e31SDaniel Almeida {
1660c078e31SDaniel Almeida return container_of(file->private_data, struct visl_ctx, fh);
1670c078e31SDaniel Almeida }
1680c078e31SDaniel Almeida
visl_v4l2fh_to_ctx(struct v4l2_fh * v4l2_fh)1690c078e31SDaniel Almeida static inline struct visl_ctx *visl_v4l2fh_to_ctx(struct v4l2_fh *v4l2_fh)
1700c078e31SDaniel Almeida {
1710c078e31SDaniel Almeida return container_of(v4l2_fh, struct visl_ctx, fh);
1720c078e31SDaniel Almeida }
1730c078e31SDaniel Almeida
1740c078e31SDaniel Almeida void *visl_find_control_data(struct visl_ctx *ctx, u32 id);
1750c078e31SDaniel Almeida struct v4l2_ctrl *visl_find_control(struct visl_ctx *ctx, u32 id);
1760c078e31SDaniel Almeida u32 visl_control_num_elems(struct visl_ctx *ctx, u32 id);
1770c078e31SDaniel Almeida
1780c078e31SDaniel Almeida #endif /* _VISL_H_ */
179