567 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*3 * Copyright (C) STMicroelectronics SA 20154 * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.5 */6 7#ifndef DELTA_H8#define DELTA_H9 10#include <linux/rpmsg.h>11#include <media/v4l2-device.h>12#include <media/v4l2-mem2mem.h>13 14#include "delta-cfg.h"15 16/*17 * enum delta_state - state of decoding instance18 *19 *@DELTA_STATE_WF_FORMAT:20 * Wait for compressed format to be set by V4L2 client in order21 * to know what is the relevant decoder to open.22 *23 *@DELTA_STATE_WF_STREAMINFO:24 * Wait for stream information to be available (bitstream25 * header parsing is done).26 *27 *@DELTA_STATE_READY:28 * Decoding instance is ready to decode compressed access unit.29 *30 *@DELTA_STATE_WF_EOS:31 * Decoding instance is waiting for EOS (End Of Stream) completion.32 *33 *@DELTA_STATE_EOS:34 * EOS (End Of Stream) is completed (signaled to user). Decoding instance35 * should then be closed.36 */37enum delta_state {38 DELTA_STATE_WF_FORMAT,39 DELTA_STATE_WF_STREAMINFO,40 DELTA_STATE_READY,41 DELTA_STATE_WF_EOS,42 DELTA_STATE_EOS43};44 45/*46 * struct delta_streaminfo - information about stream to decode47 *48 * @flags: validity of fields (crop, pixelaspect, other)49 * @width: width of video stream50 * @height: height ""51 * @streamformat: fourcc compressed format of video (MJPEG, MPEG2, ...)52 * @dpb: number of frames needed to decode a single frame53 * (h264 dpb, up to 16)54 * @crop: cropping window inside decoded frame (1920x1080@0,055 * inside 1920x1088 frame for ex.)56 * @pixelaspect: pixel aspect ratio of video (4/3, 5/4)57 * @field: interlaced or not58 * @profile: profile string59 * @level: level string60 * @other: other string information from codec61 * @colorspace: colorspace identifier62 * @xfer_func: transfer function identifier63 * @ycbcr_enc: Y'CbCr encoding identifier64 * @quantization: quantization identifier65 */66struct delta_streaminfo {67 u32 flags;68 u32 streamformat;69 u32 width;70 u32 height;71 u32 dpb;72 struct v4l2_rect crop;73 struct v4l2_fract pixelaspect;74 enum v4l2_field field;75 u8 profile[32];76 u8 level[32];77 u8 other[32];78 enum v4l2_colorspace colorspace;79 enum v4l2_xfer_func xfer_func;80 enum v4l2_ycbcr_encoding ycbcr_enc;81 enum v4l2_quantization quantization;82};83 84#define DELTA_STREAMINFO_FLAG_CROP 0x000185#define DELTA_STREAMINFO_FLAG_PIXELASPECT 0x000286#define DELTA_STREAMINFO_FLAG_OTHER 0x000487 88/*89 * struct delta_au - access unit structure.90 *91 * @vbuf: video buffer information for V4L292 * @list: V4L2 m2m list that the frame belongs to93 * @prepared: if set vaddr/paddr are resolved94 * @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 unit98 */99struct delta_au {100 struct vb2_v4l2_buffer vbuf; /* keep first */101 struct list_head list; /* keep second */102 103 bool prepared;104 u32 size;105 void *vaddr;106 dma_addr_t paddr;107 u32 flags;108 u64 dts;109};110 111/*112 * struct delta_frameinfo - information about decoded frame113 *114 * @flags: validity of fields (crop, pixelaspect)115 * @pixelformat: fourcc code for uncompressed video format116 * @width: width of frame117 * @height: height of frame118 * @aligned_width: width of frame (with encoder or decoder alignment119 * constraint)120 * @aligned_height: height of frame (with encoder or decoder alignment121 * constraint)122 * @size: maximum size in bytes required for data123 * @crop: cropping window inside frame (1920x1080@0,0124 * inside 1920x1088 frame for ex.)125 * @pixelaspect: pixel aspect ratio of video (4/3, 5/4)126 * @field: interlaced mode127 * @colorspace: colorspace identifier128 * @xfer_func: transfer function identifier129 * @ycbcr_enc: Y'CbCr encoding identifier130 * @quantization: quantization identifier131 */132struct delta_frameinfo {133 u32 flags;134 u32 pixelformat;135 u32 width;136 u32 height;137 u32 aligned_width;138 u32 aligned_height;139 u32 size;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;147};148 149#define DELTA_FRAMEINFO_FLAG_CROP 0x0001150#define DELTA_FRAMEINFO_FLAG_PIXELASPECT 0x0002151 152/*153 * struct delta_frame - frame structure.154 *155 * @vbuf: video buffer information for V4L2156 * @list: V4L2 m2m list that the frame belongs to157 * @info: frame information (width, height, format, alignment...)158 * @prepared: if set pix/vaddr/paddr are resolved159 * @index: frame index, aligned on V4L2 wow160 * @vaddr: virtual address (kernel can read/write)161 * @paddr: physical address (for hardware)162 * @state: frame state for frame lifecycle tracking163 * (DELTA_FRAME_FREE/DEC/OUT/REC/...)164 * @flags: frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)165 * @dts: decoding timestamp of this frame166 * @field: field order for interlaced frame167 */168struct delta_frame {169 struct vb2_v4l2_buffer vbuf; /* keep first */170 struct list_head list; /* keep second */171 172 struct delta_frameinfo info;173 bool prepared;174 u32 index;175 void *vaddr;176 dma_addr_t paddr;177 u32 state;178 u32 flags;179 u64 dts;180 enum v4l2_field field;181};182 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 */191 192/*193 * struct delta_dts - decoding timestamp.194 *195 * @list: list to chain timestamps196 * @val: timestamp in microseconds197 */198struct delta_dts {199 struct list_head list;200 u64 val;201};202 203struct delta_buf {204 u32 size;205 void *vaddr;206 dma_addr_t paddr;207 const char *name;208 unsigned long attrs;209};210 211struct delta_ipc_ctx {212 int cb_err;213 u32 copro_hdl;214 struct completion done;215 struct delta_buf ipc_buf_struct;216 struct delta_buf *ipc_buf;217};218 219struct delta_ipc_param {220 u32 size;221 void *data;222};223 224struct delta_ctx;225 226/*227 * struct delta_dec - decoder structure.228 *229 * @name: name of this decoder230 * @streamformat: input stream format that this decoder support231 * @pixelformat: pixel format of decoded frame that this decoder support232 * @max_width: (optional) maximum width that can decode this decoder233 * if not set, maximum width is DELTA_MAX_WIDTH234 * @max_height: (optional) maximum height that can decode this decoder235 * if not set, maximum height is DELTA_MAX_HEIGHT236 * @pm: (optional) if set, decoder will manage power on its own237 * @open: open this decoder238 * @close: close this decoder239 * @setup_frame: setup frame to be used by decoder, see below240 * @get_streaminfo: get stream related infos, see below241 * @get_frameinfo: get decoded frame related infos, see below242 * @set_frameinfo: (optional) set decoded frame related infos, see below243 * @setup_frame: setup frame to be used by decoder, see below244 * @decode: decode a single access unit, see below245 * @get_frame: get the next decoded frame available, see below246 * @recycle: recycle the given frame, see below247 * @flush: (optional) flush decoder, see below248 * @drain: (optional) drain decoder, see below249 */250struct delta_dec {251 const char *name;252 u32 streamformat;253 u32 pixelformat;254 u32 max_width;255 u32 max_height;256 bool pm;257 258 /*259 * decoder ops260 */261 int (*open)(struct delta_ctx *ctx);262 int (*close)(struct delta_ctx *ctx);263 264 /*265 * setup_frame() - setup frame to be used by decoder266 * @ctx: (in) instance267 * @frame: (in) frame to use268 * @frame.index (in) identifier of frame269 * @frame.vaddr (in) virtual address (kernel can read/write)270 * @frame.paddr (in) physical address (for hardware)271 *272 * Frame is to be allocated by caller, then given273 * to decoder through this call.274 * Several frames must be given to decoder (dpb),275 * each frame is identified using its index.276 */277 int (*setup_frame)(struct delta_ctx *ctx, struct delta_frame *frame);278 279 /*280 * get_streaminfo() - get stream related infos281 * @ctx: (in) instance282 * @streaminfo: (out) width, height, dpb,...283 *284 * Precondition: stream header must have been successfully285 * parsed to have this call successful & @streaminfo valid.286 * Header parsing must be done using decode(), giving287 * 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 bitstream access unit must be decoded till290 * get_streaminfo becomes successful.291 */292 int (*get_streaminfo)(struct delta_ctx *ctx,293 struct delta_streaminfo *streaminfo);294 295 /*296 * get_frameinfo() - get decoded frame related infos297 * @ctx: (in) instance298 * @frameinfo: (out) width, height, alignment, crop, ...299 *300 * Precondition: get_streaminfo() must be successful301 */302 int (*get_frameinfo)(struct delta_ctx *ctx,303 struct delta_frameinfo *frameinfo);304 305 /*306 * set_frameinfo() - set decoded frame related infos307 * @ctx: (in) instance308 * @frameinfo: (out) width, height, alignment, crop, ...309 *310 * Optional.311 * Typically used to negotiate with decoder the output312 * frame if decoder can do post-processing.313 */314 int (*set_frameinfo)(struct delta_ctx *ctx,315 struct delta_frameinfo *frameinfo);316 317 /*318 * decode() - decode a single access unit319 * @ctx: (in) instance320 * @au: (in/out) access unit321 * @au.size (in) size of au to decode322 * @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/325 * PFRAME/BFRAME)326 *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 could330 * have been decoded, which can be retrieved using331 * get_frame().332 */333 int (*decode)(struct delta_ctx *ctx, struct delta_au *au);334 335 /*336 * get_frame() - get the next decoded frame available337 * @ctx: (in) instance338 * @frame: (out) frame with decoded data:339 * @frame.index (out) identifier of frame340 * @frame.field (out) field order for interlaced frame341 * @frame.state (out) frame state for frame lifecycle tracking342 * @frame.flags (out) frame type (V4L2_BUF_FLAG_KEYFRAME/343 * PFRAME/BFRAME)344 *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 with348 * 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.351 */352 int (*get_frame)(struct delta_ctx *ctx, struct delta_frame **frame);353 354 /*355 * recycle() - recycle the given frame356 * @ctx: (in) instance357 * @frame: (in) frame to recycle:358 * @frame.index (in) identifier of frame359 *360 * recycle() is to be called by user when the decoded frame361 * is no more needed (composition/display done).362 * This frame will then be reused by decoder to proceed363 * 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 out366 * of available frames to proceed with decoding (starvation).367 * This case is guarded by wq_recycle wait queue which ensures that368 * decoder is called only if at least one frame is available.369 */370 int (*recycle)(struct delta_ctx *ctx, struct delta_frame *frame);371 372 /*373 * flush() - flush decoder374 * @ctx: (in) instance375 *376 * Optional.377 * Reset decoder context and discard all internal buffers.378 * This allows implementation of seek, which leads to discontinuity379 * of input bitstream that decoder must know to restart its internal380 * decoding logic.381 */382 int (*flush)(struct delta_ctx *ctx);383 384 /*385 * drain() - drain decoder386 * @ctx: (in) instance387 *388 * Optional.389 * Mark decoder pending frames (decoded but not yet output) as ready390 * 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() to392 * get all those pending frames.393 */394 int (*drain)(struct delta_ctx *ctx);395};396 397struct delta_dev;398 399/*400 * struct delta_ctx - instance structure.401 *402 * @flags: validity of fields (streaminfo)403 * @fh: V4L2 file handle404 * @dev: device context405 * @dec: selected decoder context for this instance406 * @ipc_ctx: context of IPC communication with firmware407 * @state: instance state408 * @frame_num: frame number409 * @au_num: access unit number410 * @max_au_size: max size of an access unit411 * @streaminfo: stream information (width, height, dpb, interlacing...)412 * @frameinfo: frame information (width, height, format, alignment...)413 * @nb_of_frames: number of frames available for decoding414 * @frames: array of decoding frames to keep track of frame415 * state and manage frame recycling416 * @decoded_frames: nb of decoded frames from opening417 * @output_frames: nb of output frames from opening418 * @dropped_frames: nb of frames dropped (ie access unit not parsed419 * 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 access425 * unit timestamps using this fifo.426 * @name: string naming this instance (debug purpose)427 * @run_work: decoding work428 * @lock: lock for decoding work serialization429 * @aborting: true if current job aborted430 * @priv: private decoder context for this instance, allocated431 * by decoder @open time.432 */433struct delta_ctx {434 u32 flags;435 struct v4l2_fh fh;436 struct delta_dev *dev;437 const struct delta_dec *dec;438 struct delta_ipc_ctx ipc_ctx;439 440 enum delta_state state;441 u32 frame_num;442 u32 au_num;443 size_t max_au_size;444 struct delta_streaminfo streaminfo;445 struct delta_frameinfo frameinfo;446 u32 nb_of_frames;447 struct delta_frame *frames[DELTA_MAX_FRAMES];448 u32 decoded_frames;449 u32 output_frames;450 u32 dropped_frames;451 u32 stream_errors;452 u32 decode_errors;453 u32 sys_errors;454 struct list_head dts;455 char name[100];456 struct work_struct run_work;457 struct mutex lock;458 bool aborting;459 void *priv;460};461 462#define DELTA_FLAG_STREAMINFO 0x0001463#define DELTA_FLAG_FRAMEINFO 0x0002464 465#define DELTA_MAX_FORMATS DELTA_MAX_DECODERS466 467/*468 * struct delta_dev - device struct, 1 per probe (so single one for469 * all platform life)470 *471 * @v4l2_dev: v4l2 device472 * @vdev: v4l2 video device473 * @pdev: platform device474 * @dev: device475 * @m2m_dev: memory-to-memory V4L2 device476 * @lock: device lock, for crit section & V4L2 ops serialization.477 * @clk_delta: delta main clock478 * @clk_st231: st231 coprocessor main clock479 * @clk_flash_promip: flash promip clock480 * @decoders: list of registered decoders481 * @nb_of_decoders: nb of registered decoders482 * @pixelformats: supported uncompressed video formats483 * @nb_of_pixelformats: number of supported umcompressed video formats484 * @streamformats: supported compressed video formats485 * @nb_of_streamformats:number of supported compressed video formats486 * @instance_id: rolling counter identifying an instance (debug purpose)487 * @work_queue: decoding job work queue488 * @rpmsg_driver: rpmsg IPC driver489 * @rpmsg_device: rpmsg IPC device490 */491struct delta_dev {492 struct v4l2_device v4l2_dev;493 struct video_device *vdev;494 struct platform_device *pdev;495 struct device *dev;496 struct v4l2_m2m_dev *m2m_dev;497 struct mutex lock;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];502 u32 nb_of_decoders;503 u32 pixelformats[DELTA_MAX_FORMATS];504 u32 nb_of_pixelformats;505 u32 streamformats[DELTA_MAX_FORMATS];506 u32 nb_of_streamformats;507 u8 instance_id;508 struct workqueue_struct *work_queue;509 struct rpmsg_driver rpmsg_driver;510 struct rpmsg_device *rpmsg_device;511};512 513static inline char *frame_type_str(u32 flags)514{515 if (flags & V4L2_BUF_FLAG_KEYFRAME)516 return "I";517 if (flags & V4L2_BUF_FLAG_PFRAME)518 return "P";519 if (flags & V4L2_BUF_FLAG_BFRAME)520 return "B";521 if (flags & V4L2_BUF_FLAG_LAST)522 return "EOS";523 return "?";524}525 526static inline char *frame_field_str(enum v4l2_field field)527{528 if (field == V4L2_FIELD_NONE)529 return "-";530 if (field == V4L2_FIELD_TOP)531 return "T";532 if (field == V4L2_FIELD_BOTTOM)533 return "B";534 if (field == V4L2_FIELD_INTERLACED)535 return "I";536 if (field == V4L2_FIELD_INTERLACED_TB)537 return "TB";538 if (field == V4L2_FIELD_INTERLACED_BT)539 return "BT";540 return "?";541}542 543static inline char *frame_state_str(u32 state, char *str, unsigned int len)544{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" : " ");552 return str;553}554 555int delta_get_frameinfo_default(struct delta_ctx *ctx,556 struct delta_frameinfo *frameinfo);557int delta_recycle_default(struct delta_ctx *pctx,558 struct delta_frame *frame);559 560int delta_get_free_frame(struct delta_ctx *ctx,561 struct delta_frame **pframe);562 563int delta_get_sync(struct delta_ctx *ctx);564void delta_put_autosuspend(struct delta_ctx *ctx);565 566#endif /* DELTA_H */567