1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) STMicroelectronics SA 2015
4 * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
10 #include <linux/rpmsg.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-mem2mem.h>
14 #include "delta-cfg.h"
17 * enum delta_state - state of decoding instance
19 *@DELTA_STATE_WF_FORMAT:
20 * Wait for compressed format to be set by V4L2 client in order
21 * to know what is the relevant decoder to open.
23 *@DELTA_STATE_WF_STREAMINFO:
24 * Wait for stream information to be available (bitstream
25 * header parsing is done).
28 * Decoding instance is ready to decode compressed access unit.
31 * Decoding instance is waiting for EOS (End Of Stream) completion.
34 * EOS (End Of Stream) is completed (signaled to user). Decoding instance
35 * should then be closed.
38 DELTA_STATE_WF_FORMAT
,
39 DELTA_STATE_WF_STREAMINFO
,
46 * struct delta_streaminfo - information about stream to decode
48 * @flags: validity of fields (crop, pixelaspect, other)
49 * @width: width of video stream
51 * @streamformat: fourcc compressed format of video (MJPEG, MPEG2, ...)
52 * @dpb: number of frames needed to decode a single frame
53 * (h264 dpb, up to 16)
54 * @crop: cropping window inside decoded frame (1920x1080@0,0
55 * inside 1920x1088 frame for ex.)
56 * @pixelaspect: pixel aspect ratio of video (4/3, 5/4)
57 * @field: interlaced or not
58 * @profile: profile string
59 * @level: level string
60 * @other: other string information from codec
61 * @colorspace: colorspace identifier
62 * @xfer_func: transfer function identifier
63 * @ycbcr_enc: Y'CbCr encoding identifier
64 * @quantization: quantization identifier
66 struct delta_streaminfo
{
72 struct v4l2_rect crop
;
73 struct v4l2_fract pixelaspect
;
74 enum v4l2_field field
;
78 enum v4l2_colorspace colorspace
;
79 enum v4l2_xfer_func xfer_func
;
80 enum v4l2_ycbcr_encoding ycbcr_enc
;
81 enum v4l2_quantization quantization
;
84 #define DELTA_STREAMINFO_FLAG_CROP 0x0001
85 #define DELTA_STREAMINFO_FLAG_PIXELASPECT 0x0002
86 #define DELTA_STREAMINFO_FLAG_OTHER 0x0004
89 * struct delta_au - access unit structure.
91 * @vbuf: video buffer information for V4L2
92 * @list: V4L2 m2m list that the frame belongs to
93 * @prepared: if set vaddr/paddr are resolved
94 * @vaddr: virtual address (kernel can read/write)
95 * @paddr: physical address (for hardware)
96 * @flags: access unit type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
97 * @dts: decoding timestamp of this access unit
100 struct vb2_v4l2_buffer vbuf
; /* keep first */
101 struct list_head list
; /* keep second */
112 * struct delta_frameinfo - information about decoded frame
114 * @flags: validity of fields (crop, pixelaspect)
115 * @pixelformat: fourcc code for uncompressed video format
116 * @width: width of frame
117 * @height: height of frame
118 * @aligned_width: width of frame (with encoder or decoder alignment
120 * @aligned_height: height of frame (with encoder or decoder alignment
122 * @size: maximum size in bytes required for data
123 * @crop: cropping window inside frame (1920x1080@0,0
124 * inside 1920x1088 frame for ex.)
125 * @pixelaspect: pixel aspect ratio of video (4/3, 5/4)
126 * @field: interlaced mode
127 * @colorspace: colorspace identifier
128 * @xfer_func: transfer function identifier
129 * @ycbcr_enc: Y'CbCr encoding identifier
130 * @quantization: quantization identifier
132 struct delta_frameinfo
{
140 struct v4l2_rect crop
;
141 struct v4l2_fract pixelaspect
;
142 enum v4l2_field field
;
143 enum v4l2_colorspace colorspace
;
144 enum v4l2_xfer_func xfer_func
;
145 enum v4l2_ycbcr_encoding ycbcr_enc
;
146 enum v4l2_quantization quantization
;
149 #define DELTA_FRAMEINFO_FLAG_CROP 0x0001
150 #define DELTA_FRAMEINFO_FLAG_PIXELASPECT 0x0002
153 * struct delta_frame - frame structure.
155 * @vbuf: video buffer information for V4L2
156 * @list: V4L2 m2m list that the frame belongs to
157 * @info: frame information (width, height, format, alignment...)
158 * @prepared: if set pix/vaddr/paddr are resolved
159 * @index: frame index, aligned on V4L2 wow
160 * @vaddr: virtual address (kernel can read/write)
161 * @paddr: physical address (for hardware)
162 * @state: frame state for frame lifecycle tracking
163 * (DELTA_FRAME_FREE/DEC/OUT/REC/...)
164 * @flags: frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
165 * @dts: decoding timestamp of this frame
166 * @field: field order for interlaced frame
169 struct vb2_v4l2_buffer vbuf
; /* keep first */
170 struct list_head list
; /* keep second */
172 struct delta_frameinfo info
;
180 enum v4l2_field field
;
183 /* frame state for frame lifecycle tracking */
184 #define DELTA_FRAME_FREE 0x00 /* is free and can be used for decoding */
185 #define DELTA_FRAME_REF 0x01 /* is a reference frame */
186 #define DELTA_FRAME_BSY 0x02 /* is owned by decoder and busy */
187 #define DELTA_FRAME_DEC 0x04 /* contains decoded content */
188 #define DELTA_FRAME_OUT 0x08 /* has been given to user */
189 #define DELTA_FRAME_RDY 0x10 /* is ready but still held by decoder */
190 #define DELTA_FRAME_M2M 0x20 /* is owned by mem2mem framework */
193 * struct delta_dts - decoding timestamp.
195 * @list: list to chain timestamps
196 * @val: timestamp in microseconds
199 struct list_head list
;
211 struct delta_ipc_ctx
{
214 struct completion done
;
215 struct delta_buf ipc_buf_struct
;
216 struct delta_buf
*ipc_buf
;
219 struct delta_ipc_param
{
227 * struct delta_dec - decoder structure.
229 * @name: name of this decoder
230 * @streamformat: input stream format that this decoder support
231 * @pixelformat: pixel format of decoded frame that this decoder support
232 * @max_width: (optional) maximum width that can decode this decoder
233 * if not set, maximum width is DELTA_MAX_WIDTH
234 * @max_height: (optional) maximum height that can decode this decoder
235 * if not set, maximum height is DELTA_MAX_HEIGHT
236 * @pm: (optional) if set, decoder will manage power on its own
237 * @open: open this decoder
238 * @close: close this decoder
239 * @setup_frame: setup frame to be used by decoder, see below
240 * @get_streaminfo: get stream related infos, see below
241 * @get_frameinfo: get decoded frame related infos, see below
242 * @set_frameinfo: (optional) set decoded frame related infos, see below
243 * @setup_frame: setup frame to be used by decoder, see below
244 * @decode: decode a single access unit, see below
245 * @get_frame: get the next decoded frame available, see below
246 * @recycle: recycle the given frame, see below
247 * @flush: (optional) flush decoder, see below
248 * @drain: (optional) drain decoder, see below
261 int (*open
)(struct delta_ctx
*ctx
);
262 int (*close
)(struct delta_ctx
*ctx
);
265 * setup_frame() - setup frame to be used by decoder
266 * @ctx: (in) instance
267 * @frame: (in) frame to use
268 * @frame.index (in) identifier of frame
269 * @frame.vaddr (in) virtual address (kernel can read/write)
270 * @frame.paddr (in) physical address (for hardware)
272 * Frame is to be allocated by caller, then given
273 * to decoder through this call.
274 * Several frames must be given to decoder (dpb),
275 * each frame is identified using its index.
277 int (*setup_frame
)(struct delta_ctx
*ctx
, struct delta_frame
*frame
);
280 * get_streaminfo() - get stream related infos
281 * @ctx: (in) instance
282 * @streaminfo: (out) width, height, dpb,...
284 * Precondition: stream header must have been successfully
285 * parsed to have this call successful & @streaminfo valid.
286 * Header parsing must be done using decode(), giving
287 * explicitly header access unit or first access unit of bitstream.
288 * If no valid header is found, get_streaminfo will return -ENODATA,
289 * in this case the next bistream access unit must be decoded till
290 * get_streaminfo becomes successful.
292 int (*get_streaminfo
)(struct delta_ctx
*ctx
,
293 struct delta_streaminfo
*streaminfo
);
296 * get_frameinfo() - get decoded frame related infos
297 * @ctx: (in) instance
298 * @frameinfo: (out) width, height, alignment, crop, ...
300 * Precondition: get_streaminfo() must be successful
302 int (*get_frameinfo
)(struct delta_ctx
*ctx
,
303 struct delta_frameinfo
*frameinfo
);
306 * set_frameinfo() - set decoded frame related infos
307 * @ctx: (in) instance
308 * @frameinfo: (out) width, height, alignment, crop, ...
311 * Typically used to negotiate with decoder the output
312 * frame if decoder can do post-processing.
314 int (*set_frameinfo
)(struct delta_ctx
*ctx
,
315 struct delta_frameinfo
*frameinfo
);
318 * decode() - decode a single access unit
319 * @ctx: (in) instance
320 * @au: (in/out) access unit
321 * @au.size (in) size of au to decode
322 * @au.vaddr (in) virtual address (kernel can read/write)
323 * @au.paddr (in) physical address (for hardware)
324 * @au.flags (out) au type (V4L2_BUF_FLAG_KEYFRAME/
327 * Decode the access unit given. Decode is synchronous;
328 * access unit memory is no more needed after this call.
329 * After this call, none, one or several frames could
330 * have been decoded, which can be retrieved using
333 int (*decode
)(struct delta_ctx
*ctx
, struct delta_au
*au
);
336 * get_frame() - get the next decoded frame available
337 * @ctx: (in) instance
338 * @frame: (out) frame with decoded data:
339 * @frame.index (out) identifier of frame
340 * @frame.field (out) field order for interlaced frame
341 * @frame.state (out) frame state for frame lifecycle tracking
342 * @frame.flags (out) frame type (V4L2_BUF_FLAG_KEYFRAME/
345 * Get the next available decoded frame.
346 * If no frame is available, -ENODATA is returned.
347 * If a frame is available, frame structure is filled with
348 * relevant data, frame.index identifying this exact frame.
349 * When this frame is no more needed by upper layers,
350 * recycle() must be called giving this frame identifier.
352 int (*get_frame
)(struct delta_ctx
*ctx
, struct delta_frame
**frame
);
355 * recycle() - recycle the given frame
356 * @ctx: (in) instance
357 * @frame: (in) frame to recycle:
358 * @frame.index (in) identifier of frame
360 * recycle() is to be called by user when the decoded frame
361 * is no more needed (composition/display done).
362 * This frame will then be reused by decoder to proceed
363 * with next frame decoding.
364 * If not enough frames have been provided through setup_frame(),
365 * or recycle() is not called fast enough, the decoder can run out
366 * of available frames to proceed with decoding (starvation).
367 * This case is guarded by wq_recycle wait queue which ensures that
368 * decoder is called only if at least one frame is available.
370 int (*recycle
)(struct delta_ctx
*ctx
, struct delta_frame
*frame
);
373 * flush() - flush decoder
374 * @ctx: (in) instance
377 * Reset decoder context and discard all internal buffers.
378 * This allows implementation of seek, which leads to discontinuity
379 * of input bitstream that decoder must know to restart its internal
382 int (*flush
)(struct delta_ctx
*ctx
);
385 * drain() - drain decoder
386 * @ctx: (in) instance
389 * Mark decoder pending frames (decoded but not yet output) as ready
390 * so that they can be output to client at EOS (End Of Stream).
391 * get_frame() is to be called in a loop right after drain() to
392 * get all those pending frames.
394 int (*drain
)(struct delta_ctx
*ctx
);
400 * struct delta_ctx - instance structure.
402 * @flags: validity of fields (streaminfo)
403 * @fh: V4L2 file handle
404 * @dev: device context
405 * @dec: selected decoder context for this instance
406 * @ipc_ctx: context of IPC communication with firmware
407 * @state: instance state
408 * @frame_num: frame number
409 * @au_num: access unit number
410 * @max_au_size: max size of an access unit
411 * @streaminfo: stream information (width, height, dpb, interlacing...)
412 * @frameinfo: frame information (width, height, format, alignment...)
413 * @nb_of_frames: number of frames available for decoding
414 * @frames: array of decoding frames to keep track of frame
415 * state and manage frame recycling
416 * @decoded_frames: nb of decoded frames from opening
417 * @output_frames: nb of output frames from opening
418 * @dropped_frames: nb of frames dropped (ie access unit not parsed
419 * or frame decoded but not output)
420 * @stream_errors: nb of stream errors (corrupted, not supported, ...)
421 * @decode_errors: nb of decode errors (firmware error)
422 * @sys_errors: nb of system errors (memory, ipc, ...)
423 * @dts: FIFO of decoding timestamp.
424 * output frames are timestamped with incoming access
425 * unit timestamps using this fifo.
426 * @name: string naming this instance (debug purpose)
427 * @run_work: decoding work
428 * @lock: lock for decoding work serialization
429 * @aborting: true if current job aborted
430 * @priv: private decoder context for this instance, allocated
431 * by decoder @open time.
436 struct delta_dev
*dev
;
437 const struct delta_dec
*dec
;
438 struct delta_ipc_ctx ipc_ctx
;
440 enum delta_state state
;
444 struct delta_streaminfo streaminfo
;
445 struct delta_frameinfo frameinfo
;
447 struct delta_frame
*frames
[DELTA_MAX_FRAMES
];
454 struct list_head dts
;
456 struct work_struct run_work
;
462 #define DELTA_FLAG_STREAMINFO 0x0001
463 #define DELTA_FLAG_FRAMEINFO 0x0002
465 #define DELTA_MAX_FORMATS DELTA_MAX_DECODERS
468 * struct delta_dev - device struct, 1 per probe (so single one for
471 * @v4l2_dev: v4l2 device
472 * @vdev: v4l2 video device
473 * @pdev: platform device
475 * @m2m_dev: memory-to-memory V4L2 device
476 * @lock: device lock, for crit section & V4L2 ops serialization.
477 * @clk_delta: delta main clock
478 * @clk_st231: st231 coprocessor main clock
479 * @clk_flash_promip: flash promip clock
480 * @decoders: list of registered decoders
481 * @nb_of_decoders: nb of registered decoders
482 * @pixelformats: supported uncompressed video formats
483 * @nb_of_pixelformats: number of supported umcompressed video formats
484 * @streamformats: supported compressed video formats
485 * @nb_of_streamformats:number of supported compressed video formats
486 * @instance_id: rolling counter identifying an instance (debug purpose)
487 * @work_queue: decoding job work queue
488 * @rpmsg_driver: rpmsg IPC driver
489 * @rpmsg_device: rpmsg IPC device
492 struct v4l2_device v4l2_dev
;
493 struct video_device
*vdev
;
494 struct platform_device
*pdev
;
496 struct v4l2_m2m_dev
*m2m_dev
;
498 struct clk
*clk_delta
;
499 struct clk
*clk_st231
;
500 struct clk
*clk_flash_promip
;
501 const struct delta_dec
*decoders
[DELTA_MAX_DECODERS
];
503 u32 pixelformats
[DELTA_MAX_FORMATS
];
504 u32 nb_of_pixelformats
;
505 u32 streamformats
[DELTA_MAX_FORMATS
];
506 u32 nb_of_streamformats
;
508 struct workqueue_struct
*work_queue
;
509 struct rpmsg_driver rpmsg_driver
;
510 struct rpmsg_device
*rpmsg_device
;
513 static inline char *frame_type_str(u32 flags
)
515 if (flags
& V4L2_BUF_FLAG_KEYFRAME
)
517 if (flags
& V4L2_BUF_FLAG_PFRAME
)
519 if (flags
& V4L2_BUF_FLAG_BFRAME
)
521 if (flags
& V4L2_BUF_FLAG_LAST
)
526 static inline char *frame_field_str(enum v4l2_field field
)
528 if (field
== V4L2_FIELD_NONE
)
530 if (field
== V4L2_FIELD_TOP
)
532 if (field
== V4L2_FIELD_BOTTOM
)
534 if (field
== V4L2_FIELD_INTERLACED
)
536 if (field
== V4L2_FIELD_INTERLACED_TB
)
538 if (field
== V4L2_FIELD_INTERLACED_BT
)
543 static inline char *frame_state_str(u32 state
, char *str
, unsigned int len
)
545 snprintf(str
, len
, "%s %s %s %s %s %s",
546 (state
& DELTA_FRAME_REF
) ? "ref" : " ",
547 (state
& DELTA_FRAME_BSY
) ? "bsy" : " ",
548 (state
& DELTA_FRAME_DEC
) ? "dec" : " ",
549 (state
& DELTA_FRAME_OUT
) ? "out" : " ",
550 (state
& DELTA_FRAME_M2M
) ? "m2m" : " ",
551 (state
& DELTA_FRAME_RDY
) ? "rdy" : " ");
555 int delta_get_frameinfo_default(struct delta_ctx
*ctx
,
556 struct delta_frameinfo
*frameinfo
);
557 int delta_recycle_default(struct delta_ctx
*pctx
,
558 struct delta_frame
*frame
);
560 int delta_get_free_frame(struct delta_ctx
*ctx
,
561 struct delta_frame
**pframe
);
563 int delta_get_sync(struct delta_ctx
*ctx
);
564 void delta_put_autosuspend(struct delta_ctx
*ctx
);