18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) STMicroelectronics SA 2015
48c2ecf20Sopenharmony_ci * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#ifndef DELTA_H
88c2ecf20Sopenharmony_ci#define DELTA_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/rpmsg.h>
118c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
128c2ecf20Sopenharmony_ci#include <media/v4l2-mem2mem.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include "delta-cfg.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*
178c2ecf20Sopenharmony_ci * enum delta_state - state of decoding instance
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci *@DELTA_STATE_WF_FORMAT:
208c2ecf20Sopenharmony_ci *	Wait for compressed format to be set by V4L2 client in order
218c2ecf20Sopenharmony_ci *	to know what is the relevant decoder to open.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci *@DELTA_STATE_WF_STREAMINFO:
248c2ecf20Sopenharmony_ci *	Wait for stream information to be available (bitstream
258c2ecf20Sopenharmony_ci *	header parsing is done).
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci *@DELTA_STATE_READY:
288c2ecf20Sopenharmony_ci *	Decoding instance is ready to decode compressed access unit.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci *@DELTA_STATE_WF_EOS:
318c2ecf20Sopenharmony_ci *	Decoding instance is waiting for EOS (End Of Stream) completion.
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci *@DELTA_STATE_EOS:
348c2ecf20Sopenharmony_ci *	EOS (End Of Stream) is completed (signaled to user). Decoding instance
358c2ecf20Sopenharmony_ci *	should then be closed.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_cienum delta_state {
388c2ecf20Sopenharmony_ci	DELTA_STATE_WF_FORMAT,
398c2ecf20Sopenharmony_ci	DELTA_STATE_WF_STREAMINFO,
408c2ecf20Sopenharmony_ci	DELTA_STATE_READY,
418c2ecf20Sopenharmony_ci	DELTA_STATE_WF_EOS,
428c2ecf20Sopenharmony_ci	DELTA_STATE_EOS
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/*
468c2ecf20Sopenharmony_ci * struct delta_streaminfo - information about stream to decode
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci * @flags:		validity of fields (crop, pixelaspect, other)
498c2ecf20Sopenharmony_ci * @width:		width of video stream
508c2ecf20Sopenharmony_ci * @height:		height ""
518c2ecf20Sopenharmony_ci * @streamformat:	fourcc compressed format of video (MJPEG, MPEG2, ...)
528c2ecf20Sopenharmony_ci * @dpb:		number of frames needed to decode a single frame
538c2ecf20Sopenharmony_ci *			(h264 dpb, up to 16)
548c2ecf20Sopenharmony_ci * @crop:		cropping window inside decoded frame (1920x1080@0,0
558c2ecf20Sopenharmony_ci *			inside 1920x1088 frame for ex.)
568c2ecf20Sopenharmony_ci * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
578c2ecf20Sopenharmony_ci * @field:		interlaced or not
588c2ecf20Sopenharmony_ci * @profile:		profile string
598c2ecf20Sopenharmony_ci * @level:		level string
608c2ecf20Sopenharmony_ci * @other:		other string information from codec
618c2ecf20Sopenharmony_ci * @colorspace:		colorspace identifier
628c2ecf20Sopenharmony_ci * @xfer_func:		transfer function identifier
638c2ecf20Sopenharmony_ci * @ycbcr_enc:		Y'CbCr encoding identifier
648c2ecf20Sopenharmony_ci * @quantization:	quantization identifier
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_cistruct delta_streaminfo {
678c2ecf20Sopenharmony_ci	u32 flags;
688c2ecf20Sopenharmony_ci	u32 streamformat;
698c2ecf20Sopenharmony_ci	u32 width;
708c2ecf20Sopenharmony_ci	u32 height;
718c2ecf20Sopenharmony_ci	u32 dpb;
728c2ecf20Sopenharmony_ci	struct v4l2_rect crop;
738c2ecf20Sopenharmony_ci	struct v4l2_fract pixelaspect;
748c2ecf20Sopenharmony_ci	enum v4l2_field field;
758c2ecf20Sopenharmony_ci	u8 profile[32];
768c2ecf20Sopenharmony_ci	u8 level[32];
778c2ecf20Sopenharmony_ci	u8 other[32];
788c2ecf20Sopenharmony_ci	enum v4l2_colorspace colorspace;
798c2ecf20Sopenharmony_ci	enum v4l2_xfer_func xfer_func;
808c2ecf20Sopenharmony_ci	enum v4l2_ycbcr_encoding ycbcr_enc;
818c2ecf20Sopenharmony_ci	enum v4l2_quantization quantization;
828c2ecf20Sopenharmony_ci};
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define DELTA_STREAMINFO_FLAG_CROP		0x0001
858c2ecf20Sopenharmony_ci#define DELTA_STREAMINFO_FLAG_PIXELASPECT	0x0002
868c2ecf20Sopenharmony_ci#define DELTA_STREAMINFO_FLAG_OTHER		0x0004
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci/*
898c2ecf20Sopenharmony_ci * struct delta_au - access unit structure.
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci * @vbuf:	video buffer information for V4L2
928c2ecf20Sopenharmony_ci * @list:	V4L2 m2m list that the frame belongs to
938c2ecf20Sopenharmony_ci * @prepared:	if set vaddr/paddr are resolved
948c2ecf20Sopenharmony_ci * @vaddr:	virtual address (kernel can read/write)
958c2ecf20Sopenharmony_ci * @paddr:	physical address (for hardware)
968c2ecf20Sopenharmony_ci * @flags:	access unit type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
978c2ecf20Sopenharmony_ci * @dts:	decoding timestamp of this access unit
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_cistruct delta_au {
1008c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer vbuf;	/* keep first */
1018c2ecf20Sopenharmony_ci	struct list_head list;	/* keep second */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	bool prepared;
1048c2ecf20Sopenharmony_ci	u32 size;
1058c2ecf20Sopenharmony_ci	void *vaddr;
1068c2ecf20Sopenharmony_ci	dma_addr_t paddr;
1078c2ecf20Sopenharmony_ci	u32 flags;
1088c2ecf20Sopenharmony_ci	u64 dts;
1098c2ecf20Sopenharmony_ci};
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci * struct delta_frameinfo - information about decoded frame
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * @flags:		validity of fields (crop, pixelaspect)
1158c2ecf20Sopenharmony_ci * @pixelformat:	fourcc code for uncompressed video format
1168c2ecf20Sopenharmony_ci * @width:		width of frame
1178c2ecf20Sopenharmony_ci * @height:		height of frame
1188c2ecf20Sopenharmony_ci * @aligned_width:	width of frame (with encoder or decoder alignment
1198c2ecf20Sopenharmony_ci *			constraint)
1208c2ecf20Sopenharmony_ci * @aligned_height:	height of frame (with encoder or decoder alignment
1218c2ecf20Sopenharmony_ci *			constraint)
1228c2ecf20Sopenharmony_ci * @size:		maximum size in bytes required for data
1238c2ecf20Sopenharmony_ci * @crop:		cropping window inside frame (1920x1080@0,0
1248c2ecf20Sopenharmony_ci *			inside 1920x1088 frame for ex.)
1258c2ecf20Sopenharmony_ci * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
1268c2ecf20Sopenharmony_ci * @field:		interlaced mode
1278c2ecf20Sopenharmony_ci * @colorspace:		colorspace identifier
1288c2ecf20Sopenharmony_ci * @xfer_func:		transfer function identifier
1298c2ecf20Sopenharmony_ci * @ycbcr_enc:		Y'CbCr encoding identifier
1308c2ecf20Sopenharmony_ci * @quantization:	quantization identifier
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_cistruct delta_frameinfo {
1338c2ecf20Sopenharmony_ci	u32 flags;
1348c2ecf20Sopenharmony_ci	u32 pixelformat;
1358c2ecf20Sopenharmony_ci	u32 width;
1368c2ecf20Sopenharmony_ci	u32 height;
1378c2ecf20Sopenharmony_ci	u32 aligned_width;
1388c2ecf20Sopenharmony_ci	u32 aligned_height;
1398c2ecf20Sopenharmony_ci	u32 size;
1408c2ecf20Sopenharmony_ci	struct v4l2_rect crop;
1418c2ecf20Sopenharmony_ci	struct v4l2_fract pixelaspect;
1428c2ecf20Sopenharmony_ci	enum v4l2_field field;
1438c2ecf20Sopenharmony_ci	enum v4l2_colorspace colorspace;
1448c2ecf20Sopenharmony_ci	enum v4l2_xfer_func xfer_func;
1458c2ecf20Sopenharmony_ci	enum v4l2_ycbcr_encoding ycbcr_enc;
1468c2ecf20Sopenharmony_ci	enum v4l2_quantization quantization;
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define DELTA_FRAMEINFO_FLAG_CROP		0x0001
1508c2ecf20Sopenharmony_ci#define DELTA_FRAMEINFO_FLAG_PIXELASPECT	0x0002
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/*
1538c2ecf20Sopenharmony_ci * struct delta_frame - frame structure.
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * @vbuf:	video buffer information for V4L2
1568c2ecf20Sopenharmony_ci * @list:	V4L2 m2m list that the frame belongs to
1578c2ecf20Sopenharmony_ci * @info:	frame information (width, height, format, alignment...)
1588c2ecf20Sopenharmony_ci * @prepared:	if set pix/vaddr/paddr are resolved
1598c2ecf20Sopenharmony_ci * @index:	frame index, aligned on V4L2 wow
1608c2ecf20Sopenharmony_ci * @vaddr:	virtual address (kernel can read/write)
1618c2ecf20Sopenharmony_ci * @paddr:	physical address (for hardware)
1628c2ecf20Sopenharmony_ci * @state:	frame state for frame lifecycle tracking
1638c2ecf20Sopenharmony_ci *		(DELTA_FRAME_FREE/DEC/OUT/REC/...)
1648c2ecf20Sopenharmony_ci * @flags:	frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
1658c2ecf20Sopenharmony_ci * @dts:	decoding timestamp of this frame
1668c2ecf20Sopenharmony_ci * @field:	field order for interlaced frame
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_cistruct delta_frame {
1698c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer vbuf;	/* keep first */
1708c2ecf20Sopenharmony_ci	struct list_head list;	/* keep second */
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	struct delta_frameinfo info;
1738c2ecf20Sopenharmony_ci	bool prepared;
1748c2ecf20Sopenharmony_ci	u32 index;
1758c2ecf20Sopenharmony_ci	void *vaddr;
1768c2ecf20Sopenharmony_ci	dma_addr_t paddr;
1778c2ecf20Sopenharmony_ci	u32 state;
1788c2ecf20Sopenharmony_ci	u32 flags;
1798c2ecf20Sopenharmony_ci	u64 dts;
1808c2ecf20Sopenharmony_ci	enum v4l2_field field;
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* frame state for frame lifecycle tracking */
1848c2ecf20Sopenharmony_ci#define DELTA_FRAME_FREE	0x00 /* is free and can be used for decoding */
1858c2ecf20Sopenharmony_ci#define DELTA_FRAME_REF		0x01 /* is a reference frame */
1868c2ecf20Sopenharmony_ci#define DELTA_FRAME_BSY		0x02 /* is owned by decoder and busy */
1878c2ecf20Sopenharmony_ci#define DELTA_FRAME_DEC		0x04 /* contains decoded content */
1888c2ecf20Sopenharmony_ci#define DELTA_FRAME_OUT		0x08 /* has been given to user */
1898c2ecf20Sopenharmony_ci#define DELTA_FRAME_RDY		0x10 /* is ready but still held by decoder */
1908c2ecf20Sopenharmony_ci#define DELTA_FRAME_M2M		0x20 /* is owned by mem2mem framework */
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/*
1938c2ecf20Sopenharmony_ci * struct delta_dts - decoding timestamp.
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * @list:	list to chain timestamps
1968c2ecf20Sopenharmony_ci * @val:	timestamp in microseconds
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_cistruct delta_dts {
1998c2ecf20Sopenharmony_ci	struct list_head list;
2008c2ecf20Sopenharmony_ci	u64 val;
2018c2ecf20Sopenharmony_ci};
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistruct delta_buf {
2048c2ecf20Sopenharmony_ci	u32 size;
2058c2ecf20Sopenharmony_ci	void *vaddr;
2068c2ecf20Sopenharmony_ci	dma_addr_t paddr;
2078c2ecf20Sopenharmony_ci	const char *name;
2088c2ecf20Sopenharmony_ci	unsigned long attrs;
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistruct delta_ipc_ctx {
2128c2ecf20Sopenharmony_ci	int cb_err;
2138c2ecf20Sopenharmony_ci	u32 copro_hdl;
2148c2ecf20Sopenharmony_ci	struct completion done;
2158c2ecf20Sopenharmony_ci	struct delta_buf ipc_buf_struct;
2168c2ecf20Sopenharmony_ci	struct delta_buf *ipc_buf;
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistruct delta_ipc_param {
2208c2ecf20Sopenharmony_ci	u32 size;
2218c2ecf20Sopenharmony_ci	void *data;
2228c2ecf20Sopenharmony_ci};
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistruct delta_ctx;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci * struct delta_dec - decoder structure.
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci * @name:		name of this decoder
2308c2ecf20Sopenharmony_ci * @streamformat:	input stream format that this decoder support
2318c2ecf20Sopenharmony_ci * @pixelformat:	pixel format of decoded frame that this decoder support
2328c2ecf20Sopenharmony_ci * @max_width:		(optional) maximum width that can decode this decoder
2338c2ecf20Sopenharmony_ci *			if not set, maximum width is DELTA_MAX_WIDTH
2348c2ecf20Sopenharmony_ci * @max_height:		(optional) maximum height that can decode this decoder
2358c2ecf20Sopenharmony_ci *			if not set, maximum height is DELTA_MAX_HEIGHT
2368c2ecf20Sopenharmony_ci * @pm:			(optional) if set, decoder will manage power on its own
2378c2ecf20Sopenharmony_ci * @open:		open this decoder
2388c2ecf20Sopenharmony_ci * @close:		close this decoder
2398c2ecf20Sopenharmony_ci * @setup_frame:	setup frame to be used by decoder, see below
2408c2ecf20Sopenharmony_ci * @get_streaminfo:	get stream related infos, see below
2418c2ecf20Sopenharmony_ci * @get_frameinfo:	get decoded frame related infos, see below
2428c2ecf20Sopenharmony_ci * @set_frameinfo:	(optional) set decoded frame related infos, see below
2438c2ecf20Sopenharmony_ci * @setup_frame:	setup frame to be used by decoder, see below
2448c2ecf20Sopenharmony_ci * @decode:		decode a single access unit, see below
2458c2ecf20Sopenharmony_ci * @get_frame:		get the next decoded frame available, see below
2468c2ecf20Sopenharmony_ci * @recycle:		recycle the given frame, see below
2478c2ecf20Sopenharmony_ci * @flush:		(optional) flush decoder, see below
2488c2ecf20Sopenharmony_ci * @drain:		(optional) drain decoder, see below
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_cistruct delta_dec {
2518c2ecf20Sopenharmony_ci	const char *name;
2528c2ecf20Sopenharmony_ci	u32 streamformat;
2538c2ecf20Sopenharmony_ci	u32 pixelformat;
2548c2ecf20Sopenharmony_ci	u32 max_width;
2558c2ecf20Sopenharmony_ci	u32 max_height;
2568c2ecf20Sopenharmony_ci	bool pm;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/*
2598c2ecf20Sopenharmony_ci	 * decoder ops
2608c2ecf20Sopenharmony_ci	 */
2618c2ecf20Sopenharmony_ci	int (*open)(struct delta_ctx *ctx);
2628c2ecf20Sopenharmony_ci	int (*close)(struct delta_ctx *ctx);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	/*
2658c2ecf20Sopenharmony_ci	 * setup_frame() - setup frame to be used by decoder
2668c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
2678c2ecf20Sopenharmony_ci	 * @frame:	(in) frame to use
2688c2ecf20Sopenharmony_ci	 *  @frame.index	(in) identifier of frame
2698c2ecf20Sopenharmony_ci	 *  @frame.vaddr	(in) virtual address (kernel can read/write)
2708c2ecf20Sopenharmony_ci	 *  @frame.paddr	(in) physical address (for hardware)
2718c2ecf20Sopenharmony_ci	 *
2728c2ecf20Sopenharmony_ci	 * Frame is to be allocated by caller, then given
2738c2ecf20Sopenharmony_ci	 * to decoder through this call.
2748c2ecf20Sopenharmony_ci	 * Several frames must be given to decoder (dpb),
2758c2ecf20Sopenharmony_ci	 * each frame is identified using its index.
2768c2ecf20Sopenharmony_ci	 */
2778c2ecf20Sopenharmony_ci	int (*setup_frame)(struct delta_ctx *ctx, struct delta_frame *frame);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	/*
2808c2ecf20Sopenharmony_ci	 * get_streaminfo() - get stream related infos
2818c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
2828c2ecf20Sopenharmony_ci	 * @streaminfo:	(out) width, height, dpb,...
2838c2ecf20Sopenharmony_ci	 *
2848c2ecf20Sopenharmony_ci	 * Precondition: stream header must have been successfully
2858c2ecf20Sopenharmony_ci	 * parsed to have this call successful & @streaminfo valid.
2868c2ecf20Sopenharmony_ci	 * Header parsing must be done using decode(), giving
2878c2ecf20Sopenharmony_ci	 * explicitly header access unit or first access unit of bitstream.
2888c2ecf20Sopenharmony_ci	 * If no valid header is found, get_streaminfo will return -ENODATA,
2898c2ecf20Sopenharmony_ci	 * in this case the next bitstream access unit must be decoded till
2908c2ecf20Sopenharmony_ci	 * get_streaminfo becomes successful.
2918c2ecf20Sopenharmony_ci	 */
2928c2ecf20Sopenharmony_ci	int (*get_streaminfo)(struct delta_ctx *ctx,
2938c2ecf20Sopenharmony_ci			      struct delta_streaminfo *streaminfo);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	/*
2968c2ecf20Sopenharmony_ci	 * get_frameinfo() - get decoded frame related infos
2978c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
2988c2ecf20Sopenharmony_ci	 * @frameinfo:	(out) width, height, alignment, crop, ...
2998c2ecf20Sopenharmony_ci	 *
3008c2ecf20Sopenharmony_ci	 * Precondition: get_streaminfo() must be successful
3018c2ecf20Sopenharmony_ci	 */
3028c2ecf20Sopenharmony_ci	int (*get_frameinfo)(struct delta_ctx *ctx,
3038c2ecf20Sopenharmony_ci			     struct delta_frameinfo *frameinfo);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/*
3068c2ecf20Sopenharmony_ci	 * set_frameinfo() - set decoded frame related infos
3078c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
3088c2ecf20Sopenharmony_ci	 * @frameinfo:	(out) width, height, alignment, crop, ...
3098c2ecf20Sopenharmony_ci	 *
3108c2ecf20Sopenharmony_ci	 * Optional.
3118c2ecf20Sopenharmony_ci	 * Typically used to negotiate with decoder the output
3128c2ecf20Sopenharmony_ci	 * frame if decoder can do post-processing.
3138c2ecf20Sopenharmony_ci	 */
3148c2ecf20Sopenharmony_ci	int (*set_frameinfo)(struct delta_ctx *ctx,
3158c2ecf20Sopenharmony_ci			     struct delta_frameinfo *frameinfo);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/*
3188c2ecf20Sopenharmony_ci	 * decode() - decode a single access unit
3198c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
3208c2ecf20Sopenharmony_ci	 * @au:		(in/out) access unit
3218c2ecf20Sopenharmony_ci	 *  @au.size	(in) size of au to decode
3228c2ecf20Sopenharmony_ci	 *  @au.vaddr	(in) virtual address (kernel can read/write)
3238c2ecf20Sopenharmony_ci	 *  @au.paddr	(in) physical address (for hardware)
3248c2ecf20Sopenharmony_ci	 *  @au.flags	(out) au type (V4L2_BUF_FLAG_KEYFRAME/
3258c2ecf20Sopenharmony_ci	 *			PFRAME/BFRAME)
3268c2ecf20Sopenharmony_ci	 *
3278c2ecf20Sopenharmony_ci	 * Decode the access unit given. Decode is synchronous;
3288c2ecf20Sopenharmony_ci	 * access unit memory is no more needed after this call.
3298c2ecf20Sopenharmony_ci	 * After this call, none, one or several frames could
3308c2ecf20Sopenharmony_ci	 * have been decoded, which can be retrieved using
3318c2ecf20Sopenharmony_ci	 * get_frame().
3328c2ecf20Sopenharmony_ci	 */
3338c2ecf20Sopenharmony_ci	int (*decode)(struct delta_ctx *ctx, struct delta_au *au);
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	/*
3368c2ecf20Sopenharmony_ci	 * get_frame() - get the next decoded frame available
3378c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
3388c2ecf20Sopenharmony_ci	 * @frame:	(out) frame with decoded data:
3398c2ecf20Sopenharmony_ci	 *  @frame.index	(out) identifier of frame
3408c2ecf20Sopenharmony_ci	 *  @frame.field	(out) field order for interlaced frame
3418c2ecf20Sopenharmony_ci	 *  @frame.state	(out) frame state for frame lifecycle tracking
3428c2ecf20Sopenharmony_ci	 *  @frame.flags	(out) frame type (V4L2_BUF_FLAG_KEYFRAME/
3438c2ecf20Sopenharmony_ci	 *			PFRAME/BFRAME)
3448c2ecf20Sopenharmony_ci	 *
3458c2ecf20Sopenharmony_ci	 * Get the next available decoded frame.
3468c2ecf20Sopenharmony_ci	 * If no frame is available, -ENODATA is returned.
3478c2ecf20Sopenharmony_ci	 * If a frame is available, frame structure is filled with
3488c2ecf20Sopenharmony_ci	 * relevant data, frame.index identifying this exact frame.
3498c2ecf20Sopenharmony_ci	 * When this frame is no more needed by upper layers,
3508c2ecf20Sopenharmony_ci	 * recycle() must be called giving this frame identifier.
3518c2ecf20Sopenharmony_ci	 */
3528c2ecf20Sopenharmony_ci	int (*get_frame)(struct delta_ctx *ctx, struct delta_frame **frame);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	/*
3558c2ecf20Sopenharmony_ci	 * recycle() - recycle the given frame
3568c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
3578c2ecf20Sopenharmony_ci	 * @frame:	(in) frame to recycle:
3588c2ecf20Sopenharmony_ci	 *  @frame.index	(in) identifier of frame
3598c2ecf20Sopenharmony_ci	 *
3608c2ecf20Sopenharmony_ci	 * recycle() is to be called by user when the decoded frame
3618c2ecf20Sopenharmony_ci	 * is no more needed (composition/display done).
3628c2ecf20Sopenharmony_ci	 * This frame will then be reused by decoder to proceed
3638c2ecf20Sopenharmony_ci	 * with next frame decoding.
3648c2ecf20Sopenharmony_ci	 * If not enough frames have been provided through setup_frame(),
3658c2ecf20Sopenharmony_ci	 * or recycle() is not called fast enough, the decoder can run out
3668c2ecf20Sopenharmony_ci	 * of available frames to proceed with decoding (starvation).
3678c2ecf20Sopenharmony_ci	 * This case is guarded by wq_recycle wait queue which ensures that
3688c2ecf20Sopenharmony_ci	 * decoder is called only if at least one frame is available.
3698c2ecf20Sopenharmony_ci	 */
3708c2ecf20Sopenharmony_ci	int (*recycle)(struct delta_ctx *ctx, struct delta_frame *frame);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/*
3738c2ecf20Sopenharmony_ci	 * flush() - flush decoder
3748c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
3758c2ecf20Sopenharmony_ci	 *
3768c2ecf20Sopenharmony_ci	 * Optional.
3778c2ecf20Sopenharmony_ci	 * Reset decoder context and discard all internal buffers.
3788c2ecf20Sopenharmony_ci	 * This allows implementation of seek, which leads to discontinuity
3798c2ecf20Sopenharmony_ci	 * of input bitstream that decoder must know to restart its internal
3808c2ecf20Sopenharmony_ci	 * decoding logic.
3818c2ecf20Sopenharmony_ci	 */
3828c2ecf20Sopenharmony_ci	int (*flush)(struct delta_ctx *ctx);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/*
3858c2ecf20Sopenharmony_ci	 * drain() - drain decoder
3868c2ecf20Sopenharmony_ci	 * @ctx:	(in) instance
3878c2ecf20Sopenharmony_ci	 *
3888c2ecf20Sopenharmony_ci	 * Optional.
3898c2ecf20Sopenharmony_ci	 * Mark decoder pending frames (decoded but not yet output) as ready
3908c2ecf20Sopenharmony_ci	 * so that they can be output to client at EOS (End Of Stream).
3918c2ecf20Sopenharmony_ci	 * get_frame() is to be called in a loop right after drain() to
3928c2ecf20Sopenharmony_ci	 * get all those pending frames.
3938c2ecf20Sopenharmony_ci	 */
3948c2ecf20Sopenharmony_ci	int (*drain)(struct delta_ctx *ctx);
3958c2ecf20Sopenharmony_ci};
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistruct delta_dev;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/*
4008c2ecf20Sopenharmony_ci * struct delta_ctx - instance structure.
4018c2ecf20Sopenharmony_ci *
4028c2ecf20Sopenharmony_ci * @flags:		validity of fields (streaminfo)
4038c2ecf20Sopenharmony_ci * @fh:			V4L2 file handle
4048c2ecf20Sopenharmony_ci * @dev:		device context
4058c2ecf20Sopenharmony_ci * @dec:		selected decoder context for this instance
4068c2ecf20Sopenharmony_ci * @ipc_ctx:		context of IPC communication with firmware
4078c2ecf20Sopenharmony_ci * @state:		instance state
4088c2ecf20Sopenharmony_ci * @frame_num:		frame number
4098c2ecf20Sopenharmony_ci * @au_num:		access unit number
4108c2ecf20Sopenharmony_ci * @max_au_size:	max size of an access unit
4118c2ecf20Sopenharmony_ci * @streaminfo:		stream information (width, height, dpb, interlacing...)
4128c2ecf20Sopenharmony_ci * @frameinfo:		frame information (width, height, format, alignment...)
4138c2ecf20Sopenharmony_ci * @nb_of_frames:	number of frames available for decoding
4148c2ecf20Sopenharmony_ci * @frames:		array of decoding frames to keep track of frame
4158c2ecf20Sopenharmony_ci *			state and manage frame recycling
4168c2ecf20Sopenharmony_ci * @decoded_frames:	nb of decoded frames from opening
4178c2ecf20Sopenharmony_ci * @output_frames:	nb of output frames from opening
4188c2ecf20Sopenharmony_ci * @dropped_frames:	nb of frames dropped (ie access unit not parsed
4198c2ecf20Sopenharmony_ci *			or frame decoded but not output)
4208c2ecf20Sopenharmony_ci * @stream_errors:	nb of stream errors (corrupted, not supported, ...)
4218c2ecf20Sopenharmony_ci * @decode_errors:	nb of decode errors (firmware error)
4228c2ecf20Sopenharmony_ci * @sys_errors:		nb of system errors (memory, ipc, ...)
4238c2ecf20Sopenharmony_ci * @dts:		FIFO of decoding timestamp.
4248c2ecf20Sopenharmony_ci *			output frames are timestamped with incoming access
4258c2ecf20Sopenharmony_ci *			unit timestamps using this fifo.
4268c2ecf20Sopenharmony_ci * @name:		string naming this instance (debug purpose)
4278c2ecf20Sopenharmony_ci * @run_work:		decoding work
4288c2ecf20Sopenharmony_ci * @lock:		lock for decoding work serialization
4298c2ecf20Sopenharmony_ci * @aborting:		true if current job aborted
4308c2ecf20Sopenharmony_ci * @priv:		private decoder context for this instance, allocated
4318c2ecf20Sopenharmony_ci *			by decoder @open time.
4328c2ecf20Sopenharmony_ci */
4338c2ecf20Sopenharmony_cistruct delta_ctx {
4348c2ecf20Sopenharmony_ci	u32 flags;
4358c2ecf20Sopenharmony_ci	struct v4l2_fh fh;
4368c2ecf20Sopenharmony_ci	struct delta_dev *dev;
4378c2ecf20Sopenharmony_ci	const struct delta_dec *dec;
4388c2ecf20Sopenharmony_ci	struct delta_ipc_ctx ipc_ctx;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	enum delta_state state;
4418c2ecf20Sopenharmony_ci	u32 frame_num;
4428c2ecf20Sopenharmony_ci	u32 au_num;
4438c2ecf20Sopenharmony_ci	size_t max_au_size;
4448c2ecf20Sopenharmony_ci	struct delta_streaminfo streaminfo;
4458c2ecf20Sopenharmony_ci	struct delta_frameinfo frameinfo;
4468c2ecf20Sopenharmony_ci	u32 nb_of_frames;
4478c2ecf20Sopenharmony_ci	struct delta_frame *frames[DELTA_MAX_FRAMES];
4488c2ecf20Sopenharmony_ci	u32 decoded_frames;
4498c2ecf20Sopenharmony_ci	u32 output_frames;
4508c2ecf20Sopenharmony_ci	u32 dropped_frames;
4518c2ecf20Sopenharmony_ci	u32 stream_errors;
4528c2ecf20Sopenharmony_ci	u32 decode_errors;
4538c2ecf20Sopenharmony_ci	u32 sys_errors;
4548c2ecf20Sopenharmony_ci	struct list_head dts;
4558c2ecf20Sopenharmony_ci	char name[100];
4568c2ecf20Sopenharmony_ci	struct work_struct run_work;
4578c2ecf20Sopenharmony_ci	struct mutex lock;
4588c2ecf20Sopenharmony_ci	bool aborting;
4598c2ecf20Sopenharmony_ci	void *priv;
4608c2ecf20Sopenharmony_ci};
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci#define DELTA_FLAG_STREAMINFO 0x0001
4638c2ecf20Sopenharmony_ci#define DELTA_FLAG_FRAMEINFO 0x0002
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci#define DELTA_MAX_FORMATS  DELTA_MAX_DECODERS
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci/*
4688c2ecf20Sopenharmony_ci * struct delta_dev - device struct, 1 per probe (so single one for
4698c2ecf20Sopenharmony_ci * all platform life)
4708c2ecf20Sopenharmony_ci *
4718c2ecf20Sopenharmony_ci * @v4l2_dev:		v4l2 device
4728c2ecf20Sopenharmony_ci * @vdev:		v4l2 video device
4738c2ecf20Sopenharmony_ci * @pdev:		platform device
4748c2ecf20Sopenharmony_ci * @dev:		device
4758c2ecf20Sopenharmony_ci * @m2m_dev:		memory-to-memory V4L2 device
4768c2ecf20Sopenharmony_ci * @lock:		device lock, for crit section & V4L2 ops serialization.
4778c2ecf20Sopenharmony_ci * @clk_delta:		delta main clock
4788c2ecf20Sopenharmony_ci * @clk_st231:		st231 coprocessor main clock
4798c2ecf20Sopenharmony_ci * @clk_flash_promip:	flash promip clock
4808c2ecf20Sopenharmony_ci * @decoders:		list of registered decoders
4818c2ecf20Sopenharmony_ci * @nb_of_decoders:	nb of registered decoders
4828c2ecf20Sopenharmony_ci * @pixelformats:	supported uncompressed video formats
4838c2ecf20Sopenharmony_ci * @nb_of_pixelformats:	number of supported umcompressed video formats
4848c2ecf20Sopenharmony_ci * @streamformats:	supported compressed video formats
4858c2ecf20Sopenharmony_ci * @nb_of_streamformats:number of supported compressed video formats
4868c2ecf20Sopenharmony_ci * @instance_id:	rolling counter identifying an instance (debug purpose)
4878c2ecf20Sopenharmony_ci * @work_queue:		decoding job work queue
4888c2ecf20Sopenharmony_ci * @rpmsg_driver:	rpmsg IPC driver
4898c2ecf20Sopenharmony_ci * @rpmsg_device:	rpmsg IPC device
4908c2ecf20Sopenharmony_ci */
4918c2ecf20Sopenharmony_cistruct delta_dev {
4928c2ecf20Sopenharmony_ci	struct v4l2_device v4l2_dev;
4938c2ecf20Sopenharmony_ci	struct video_device *vdev;
4948c2ecf20Sopenharmony_ci	struct platform_device *pdev;
4958c2ecf20Sopenharmony_ci	struct device *dev;
4968c2ecf20Sopenharmony_ci	struct v4l2_m2m_dev *m2m_dev;
4978c2ecf20Sopenharmony_ci	struct mutex lock;
4988c2ecf20Sopenharmony_ci	struct clk *clk_delta;
4998c2ecf20Sopenharmony_ci	struct clk *clk_st231;
5008c2ecf20Sopenharmony_ci	struct clk *clk_flash_promip;
5018c2ecf20Sopenharmony_ci	const struct delta_dec *decoders[DELTA_MAX_DECODERS];
5028c2ecf20Sopenharmony_ci	u32 nb_of_decoders;
5038c2ecf20Sopenharmony_ci	u32 pixelformats[DELTA_MAX_FORMATS];
5048c2ecf20Sopenharmony_ci	u32 nb_of_pixelformats;
5058c2ecf20Sopenharmony_ci	u32 streamformats[DELTA_MAX_FORMATS];
5068c2ecf20Sopenharmony_ci	u32 nb_of_streamformats;
5078c2ecf20Sopenharmony_ci	u8 instance_id;
5088c2ecf20Sopenharmony_ci	struct workqueue_struct *work_queue;
5098c2ecf20Sopenharmony_ci	struct rpmsg_driver rpmsg_driver;
5108c2ecf20Sopenharmony_ci	struct rpmsg_device *rpmsg_device;
5118c2ecf20Sopenharmony_ci};
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistatic inline char *frame_type_str(u32 flags)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	if (flags & V4L2_BUF_FLAG_KEYFRAME)
5168c2ecf20Sopenharmony_ci		return "I";
5178c2ecf20Sopenharmony_ci	if (flags & V4L2_BUF_FLAG_PFRAME)
5188c2ecf20Sopenharmony_ci		return "P";
5198c2ecf20Sopenharmony_ci	if (flags & V4L2_BUF_FLAG_BFRAME)
5208c2ecf20Sopenharmony_ci		return "B";
5218c2ecf20Sopenharmony_ci	if (flags & V4L2_BUF_FLAG_LAST)
5228c2ecf20Sopenharmony_ci		return "EOS";
5238c2ecf20Sopenharmony_ci	return "?";
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic inline char *frame_field_str(enum v4l2_field field)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	if (field == V4L2_FIELD_NONE)
5298c2ecf20Sopenharmony_ci		return "-";
5308c2ecf20Sopenharmony_ci	if (field == V4L2_FIELD_TOP)
5318c2ecf20Sopenharmony_ci		return "T";
5328c2ecf20Sopenharmony_ci	if (field == V4L2_FIELD_BOTTOM)
5338c2ecf20Sopenharmony_ci		return "B";
5348c2ecf20Sopenharmony_ci	if (field == V4L2_FIELD_INTERLACED)
5358c2ecf20Sopenharmony_ci		return "I";
5368c2ecf20Sopenharmony_ci	if (field == V4L2_FIELD_INTERLACED_TB)
5378c2ecf20Sopenharmony_ci		return "TB";
5388c2ecf20Sopenharmony_ci	if (field == V4L2_FIELD_INTERLACED_BT)
5398c2ecf20Sopenharmony_ci		return "BT";
5408c2ecf20Sopenharmony_ci	return "?";
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic inline char *frame_state_str(u32 state, char *str, unsigned int len)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	snprintf(str, len, "%s %s %s %s %s %s",
5468c2ecf20Sopenharmony_ci		 (state & DELTA_FRAME_REF)  ? "ref" : "   ",
5478c2ecf20Sopenharmony_ci		 (state & DELTA_FRAME_BSY)  ? "bsy" : "   ",
5488c2ecf20Sopenharmony_ci		 (state & DELTA_FRAME_DEC)  ? "dec" : "   ",
5498c2ecf20Sopenharmony_ci		 (state & DELTA_FRAME_OUT)  ? "out" : "   ",
5508c2ecf20Sopenharmony_ci		 (state & DELTA_FRAME_M2M)  ? "m2m" : "   ",
5518c2ecf20Sopenharmony_ci		 (state & DELTA_FRAME_RDY)  ? "rdy" : "   ");
5528c2ecf20Sopenharmony_ci	return str;
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ciint delta_get_frameinfo_default(struct delta_ctx *ctx,
5568c2ecf20Sopenharmony_ci				struct delta_frameinfo *frameinfo);
5578c2ecf20Sopenharmony_ciint delta_recycle_default(struct delta_ctx *pctx,
5588c2ecf20Sopenharmony_ci			  struct delta_frame *frame);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ciint delta_get_free_frame(struct delta_ctx *ctx,
5618c2ecf20Sopenharmony_ci			 struct delta_frame **pframe);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ciint delta_get_sync(struct delta_ctx *ctx);
5648c2ecf20Sopenharmony_civoid delta_put_autosuspend(struct delta_ctx *ctx);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci#endif /* DELTA_H */
567