delta.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2015
  3. * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #ifndef DELTA_H
  7. #define DELTA_H
  8. #include <linux/rpmsg.h>
  9. #include <media/v4l2-device.h>
  10. #include <media/v4l2-mem2mem.h>
  11. #include "delta-cfg.h"
  12. /*
  13. * enum delta_state - state of decoding instance
  14. *
  15. *@DELTA_STATE_WF_FORMAT:
  16. * Wait for compressed format to be set by V4L2 client in order
  17. * to know what is the relevant decoder to open.
  18. *
  19. *@DELTA_STATE_WF_STREAMINFO:
  20. * Wait for stream information to be available (bitstream
  21. * header parsing is done).
  22. *
  23. *@DELTA_STATE_READY:
  24. * Decoding instance is ready to decode compressed access unit.
  25. *
  26. *@DELTA_STATE_WF_EOS:
  27. * Decoding instance is waiting for EOS (End Of Stream) completion.
  28. *
  29. *@DELTA_STATE_EOS:
  30. * EOS (End Of Stream) is completed (signaled to user). Decoding instance
  31. * should then be closed.
  32. */
  33. enum delta_state {
  34. DELTA_STATE_WF_FORMAT,
  35. DELTA_STATE_WF_STREAMINFO,
  36. DELTA_STATE_READY,
  37. DELTA_STATE_WF_EOS,
  38. DELTA_STATE_EOS
  39. };
  40. /*
  41. * struct delta_streaminfo - information about stream to decode
  42. *
  43. * @flags: validity of fields (crop, pixelaspect, other)
  44. * @width: width of video stream
  45. * @height: height ""
  46. * @streamformat: fourcc compressed format of video (MJPEG, MPEG2, ...)
  47. * @dpb: number of frames needed to decode a single frame
  48. * (h264 dpb, up to 16)
  49. * @crop: cropping window inside decoded frame (1920x1080@0,0
  50. * inside 1920x1088 frame for ex.)
  51. * @pixelaspect: pixel aspect ratio of video (4/3, 5/4)
  52. * @field: interlaced or not
  53. * @profile: profile string
  54. * @level: level string
  55. * @other: other string information from codec
  56. * @colorspace: colorspace identifier
  57. * @xfer_func: transfer function identifier
  58. * @ycbcr_enc: Y'CbCr encoding identifier
  59. * @quantization: quantization identifier
  60. */
  61. struct delta_streaminfo {
  62. u32 flags;
  63. u32 streamformat;
  64. u32 width;
  65. u32 height;
  66. u32 dpb;
  67. struct v4l2_rect crop;
  68. struct v4l2_fract pixelaspect;
  69. enum v4l2_field field;
  70. u8 profile[32];
  71. u8 level[32];
  72. u8 other[32];
  73. enum v4l2_colorspace colorspace;
  74. enum v4l2_xfer_func xfer_func;
  75. enum v4l2_ycbcr_encoding ycbcr_enc;
  76. enum v4l2_quantization quantization;
  77. };
  78. #define DELTA_STREAMINFO_FLAG_CROP 0x0001
  79. #define DELTA_STREAMINFO_FLAG_PIXELASPECT 0x0002
  80. #define DELTA_STREAMINFO_FLAG_OTHER 0x0004
  81. /*
  82. * struct delta_au - access unit structure.
  83. *
  84. * @vbuf: video buffer information for V4L2
  85. * @list: V4L2 m2m list that the frame belongs to
  86. * @prepared: if set vaddr/paddr are resolved
  87. * @vaddr: virtual address (kernel can read/write)
  88. * @paddr: physical address (for hardware)
  89. * @flags: access unit type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
  90. * @dts: decoding timestamp of this access unit
  91. */
  92. struct delta_au {
  93. struct vb2_v4l2_buffer vbuf; /* keep first */
  94. struct list_head list; /* keep second */
  95. bool prepared;
  96. u32 size;
  97. void *vaddr;
  98. dma_addr_t paddr;
  99. u32 flags;
  100. u64 dts;
  101. };
  102. /*
  103. * struct delta_frameinfo - information about decoded frame
  104. *
  105. * @flags: validity of fields (crop, pixelaspect)
  106. * @pixelformat: fourcc code for uncompressed video format
  107. * @width: width of frame
  108. * @height: height of frame
  109. * @aligned_width: width of frame (with encoder or decoder alignment
  110. * constraint)
  111. * @aligned_height: height of frame (with encoder or decoder alignment
  112. * constraint)
  113. * @size: maximum size in bytes required for data
  114. * @crop: cropping window inside frame (1920x1080@0,0
  115. * inside 1920x1088 frame for ex.)
  116. * @pixelaspect: pixel aspect ratio of video (4/3, 5/4)
  117. * @field: interlaced mode
  118. * @colorspace: colorspace identifier
  119. * @xfer_func: transfer function identifier
  120. * @ycbcr_enc: Y'CbCr encoding identifier
  121. * @quantization: quantization identifier
  122. */
  123. struct delta_frameinfo {
  124. u32 flags;
  125. u32 pixelformat;
  126. u32 width;
  127. u32 height;
  128. u32 aligned_width;
  129. u32 aligned_height;
  130. u32 size;
  131. struct v4l2_rect crop;
  132. struct v4l2_fract pixelaspect;
  133. enum v4l2_field field;
  134. enum v4l2_colorspace colorspace;
  135. enum v4l2_xfer_func xfer_func;
  136. enum v4l2_ycbcr_encoding ycbcr_enc;
  137. enum v4l2_quantization quantization;
  138. };
  139. #define DELTA_FRAMEINFO_FLAG_CROP 0x0001
  140. #define DELTA_FRAMEINFO_FLAG_PIXELASPECT 0x0002
  141. /*
  142. * struct delta_frame - frame structure.
  143. *
  144. * @vbuf: video buffer information for V4L2
  145. * @list: V4L2 m2m list that the frame belongs to
  146. * @info: frame information (width, height, format, alignment...)
  147. * @prepared: if set pix/vaddr/paddr are resolved
  148. * @index: frame index, aligned on V4L2 wow
  149. * @vaddr: virtual address (kernel can read/write)
  150. * @paddr: physical address (for hardware)
  151. * @state: frame state for frame lifecycle tracking
  152. * (DELTA_FRAME_FREE/DEC/OUT/REC/...)
  153. * @flags: frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
  154. * @dts: decoding timestamp of this frame
  155. * @field: field order for interlaced frame
  156. */
  157. struct delta_frame {
  158. struct vb2_v4l2_buffer vbuf; /* keep first */
  159. struct list_head list; /* keep second */
  160. struct delta_frameinfo info;
  161. bool prepared;
  162. u32 index;
  163. void *vaddr;
  164. dma_addr_t paddr;
  165. u32 state;
  166. u32 flags;
  167. u64 dts;
  168. enum v4l2_field field;
  169. };
  170. /* frame state for frame lifecycle tracking */
  171. #define DELTA_FRAME_FREE 0x00 /* is free and can be used for decoding */
  172. #define DELTA_FRAME_REF 0x01 /* is a reference frame */
  173. #define DELTA_FRAME_BSY 0x02 /* is owned by decoder and busy */
  174. #define DELTA_FRAME_DEC 0x04 /* contains decoded content */
  175. #define DELTA_FRAME_OUT 0x08 /* has been given to user */
  176. #define DELTA_FRAME_RDY 0x10 /* is ready but still held by decoder */
  177. #define DELTA_FRAME_M2M 0x20 /* is owned by mem2mem framework */
  178. /*
  179. * struct delta_dts - decoding timestamp.
  180. *
  181. * @list: list to chain timestamps
  182. * @val: timestamp in microseconds
  183. */
  184. struct delta_dts {
  185. struct list_head list;
  186. u64 val;
  187. };
  188. struct delta_buf {
  189. u32 size;
  190. void *vaddr;
  191. dma_addr_t paddr;
  192. const char *name;
  193. unsigned long attrs;
  194. };
  195. struct delta_ipc_ctx {
  196. int cb_err;
  197. u32 copro_hdl;
  198. struct completion done;
  199. struct delta_buf ipc_buf_struct;
  200. struct delta_buf *ipc_buf;
  201. };
  202. struct delta_ipc_param {
  203. u32 size;
  204. void *data;
  205. };
  206. struct delta_ctx;
  207. /*
  208. * struct delta_dec - decoder structure.
  209. *
  210. * @name: name of this decoder
  211. * @streamformat: input stream format that this decoder support
  212. * @pixelformat: pixel format of decoded frame that this decoder support
  213. * @max_width: (optional) maximum width that can decode this decoder
  214. * if not set, maximum width is DELTA_MAX_WIDTH
  215. * @max_height: (optional) maximum height that can decode this decoder
  216. * if not set, maximum height is DELTA_MAX_HEIGHT
  217. * @pm: (optional) if set, decoder will manage power on its own
  218. * @open: open this decoder
  219. * @close: close this decoder
  220. * @setup_frame: setup frame to be used by decoder, see below
  221. * @get_streaminfo: get stream related infos, see below
  222. * @get_frameinfo: get decoded frame related infos, see below
  223. * @set_frameinfo: (optional) set decoded frame related infos, see below
  224. * @setup_frame: setup frame to be used by decoder, see below
  225. * @decode: decode a single access unit, see below
  226. * @get_frame: get the next decoded frame available, see below
  227. * @recycle: recycle the given frame, see below
  228. * @flush: (optional) flush decoder, see below
  229. * @drain: (optional) drain decoder, see below
  230. */
  231. struct delta_dec {
  232. const char *name;
  233. u32 streamformat;
  234. u32 pixelformat;
  235. u32 max_width;
  236. u32 max_height;
  237. bool pm;
  238. /*
  239. * decoder ops
  240. */
  241. int (*open)(struct delta_ctx *ctx);
  242. int (*close)(struct delta_ctx *ctx);
  243. /*
  244. * setup_frame() - setup frame to be used by decoder
  245. * @ctx: (in) instance
  246. * @frame: (in) frame to use
  247. * @frame.index (in) identifier of frame
  248. * @frame.vaddr (in) virtual address (kernel can read/write)
  249. * @frame.paddr (in) physical address (for hardware)
  250. *
  251. * Frame is to be allocated by caller, then given
  252. * to decoder through this call.
  253. * Several frames must be given to decoder (dpb),
  254. * each frame is identified using its index.
  255. */
  256. int (*setup_frame)(struct delta_ctx *ctx, struct delta_frame *frame);
  257. /*
  258. * get_streaminfo() - get stream related infos
  259. * @ctx: (in) instance
  260. * @streaminfo: (out) width, height, dpb,...
  261. *
  262. * Precondition: stream header must have been successfully
  263. * parsed to have this call successful & @streaminfo valid.
  264. * Header parsing must be done using decode(), giving
  265. * explicitly header access unit or first access unit of bitstream.
  266. * If no valid header is found, get_streaminfo will return -ENODATA,
  267. * in this case the next bistream access unit must be decoded till
  268. * get_streaminfo becomes successful.
  269. */
  270. int (*get_streaminfo)(struct delta_ctx *ctx,
  271. struct delta_streaminfo *streaminfo);
  272. /*
  273. * get_frameinfo() - get decoded frame related infos
  274. * @ctx: (in) instance
  275. * @frameinfo: (out) width, height, alignment, crop, ...
  276. *
  277. * Precondition: get_streaminfo() must be successful
  278. */
  279. int (*get_frameinfo)(struct delta_ctx *ctx,
  280. struct delta_frameinfo *frameinfo);
  281. /*
  282. * set_frameinfo() - set decoded frame related infos
  283. * @ctx: (in) instance
  284. * @frameinfo: (out) width, height, alignment, crop, ...
  285. *
  286. * Optional.
  287. * Typically used to negotiate with decoder the output
  288. * frame if decoder can do post-processing.
  289. */
  290. int (*set_frameinfo)(struct delta_ctx *ctx,
  291. struct delta_frameinfo *frameinfo);
  292. /*
  293. * decode() - decode a single access unit
  294. * @ctx: (in) instance
  295. * @au: (in/out) access unit
  296. * @au.size (in) size of au to decode
  297. * @au.vaddr (in) virtual address (kernel can read/write)
  298. * @au.paddr (in) physical address (for hardware)
  299. * @au.flags (out) au type (V4L2_BUF_FLAG_KEYFRAME/
  300. * PFRAME/BFRAME)
  301. *
  302. * Decode the access unit given. Decode is synchronous;
  303. * access unit memory is no more needed after this call.
  304. * After this call, none, one or several frames could
  305. * have been decoded, which can be retrieved using
  306. * get_frame().
  307. */
  308. int (*decode)(struct delta_ctx *ctx, struct delta_au *au);
  309. /*
  310. * get_frame() - get the next decoded frame available
  311. * @ctx: (in) instance
  312. * @frame: (out) frame with decoded data:
  313. * @frame.index (out) identifier of frame
  314. * @frame.field (out) field order for interlaced frame
  315. * @frame.state (out) frame state for frame lifecycle tracking
  316. * @frame.flags (out) frame type (V4L2_BUF_FLAG_KEYFRAME/
  317. * PFRAME/BFRAME)
  318. *
  319. * Get the next available decoded frame.
  320. * If no frame is available, -ENODATA is returned.
  321. * If a frame is available, frame structure is filled with
  322. * relevant data, frame.index identifying this exact frame.
  323. * When this frame is no more needed by upper layers,
  324. * recycle() must be called giving this frame identifier.
  325. */
  326. int (*get_frame)(struct delta_ctx *ctx, struct delta_frame **frame);
  327. /*
  328. * recycle() - recycle the given frame
  329. * @ctx: (in) instance
  330. * @frame: (in) frame to recycle:
  331. * @frame.index (in) identifier of frame
  332. *
  333. * recycle() is to be called by user when the decoded frame
  334. * is no more needed (composition/display done).
  335. * This frame will then be reused by decoder to proceed
  336. * with next frame decoding.
  337. * If not enough frames have been provided through setup_frame(),
  338. * or recycle() is not called fast enough, the decoder can run out
  339. * of available frames to proceed with decoding (starvation).
  340. * This case is guarded by wq_recycle wait queue which ensures that
  341. * decoder is called only if at least one frame is available.
  342. */
  343. int (*recycle)(struct delta_ctx *ctx, struct delta_frame *frame);
  344. /*
  345. * flush() - flush decoder
  346. * @ctx: (in) instance
  347. *
  348. * Optional.
  349. * Reset decoder context and discard all internal buffers.
  350. * This allows implementation of seek, which leads to discontinuity
  351. * of input bitstream that decoder must know to restart its internal
  352. * decoding logic.
  353. */
  354. int (*flush)(struct delta_ctx *ctx);
  355. /*
  356. * drain() - drain decoder
  357. * @ctx: (in) instance
  358. *
  359. * Optional.
  360. * Mark decoder pending frames (decoded but not yet output) as ready
  361. * so that they can be output to client at EOS (End Of Stream).
  362. * get_frame() is to be called in a loop right after drain() to
  363. * get all those pending frames.
  364. */
  365. int (*drain)(struct delta_ctx *ctx);
  366. };
  367. struct delta_dev;
  368. /*
  369. * struct delta_ctx - instance structure.
  370. *
  371. * @flags: validity of fields (streaminfo)
  372. * @fh: V4L2 file handle
  373. * @dev: device context
  374. * @dec: selected decoder context for this instance
  375. * @ipc_ctx: context of IPC communication with firmware
  376. * @state: instance state
  377. * @frame_num: frame number
  378. * @au_num: access unit number
  379. * @max_au_size: max size of an access unit
  380. * @streaminfo: stream information (width, height, dpb, interlacing...)
  381. * @frameinfo: frame information (width, height, format, alignment...)
  382. * @nb_of_frames: number of frames available for decoding
  383. * @frames: array of decoding frames to keep track of frame
  384. * state and manage frame recycling
  385. * @decoded_frames: nb of decoded frames from opening
  386. * @output_frames: nb of output frames from opening
  387. * @dropped_frames: nb of frames dropped (ie access unit not parsed
  388. * or frame decoded but not output)
  389. * @stream_errors: nb of stream errors (corrupted, not supported, ...)
  390. * @decode_errors: nb of decode errors (firmware error)
  391. * @sys_errors: nb of system errors (memory, ipc, ...)
  392. * @dts: FIFO of decoding timestamp.
  393. * output frames are timestamped with incoming access
  394. * unit timestamps using this fifo.
  395. * @name: string naming this instance (debug purpose)
  396. * @run_work: decoding work
  397. * @lock: lock for decoding work serialization
  398. * @aborting: true if current job aborted
  399. * @priv: private decoder context for this instance, allocated
  400. * by decoder @open time.
  401. */
  402. struct delta_ctx {
  403. u32 flags;
  404. struct v4l2_fh fh;
  405. struct delta_dev *dev;
  406. const struct delta_dec *dec;
  407. struct delta_ipc_ctx ipc_ctx;
  408. enum delta_state state;
  409. u32 frame_num;
  410. u32 au_num;
  411. size_t max_au_size;
  412. struct delta_streaminfo streaminfo;
  413. struct delta_frameinfo frameinfo;
  414. u32 nb_of_frames;
  415. struct delta_frame *frames[DELTA_MAX_FRAMES];
  416. u32 decoded_frames;
  417. u32 output_frames;
  418. u32 dropped_frames;
  419. u32 stream_errors;
  420. u32 decode_errors;
  421. u32 sys_errors;
  422. struct list_head dts;
  423. char name[100];
  424. struct work_struct run_work;
  425. struct mutex lock;
  426. bool aborting;
  427. void *priv;
  428. };
  429. #define DELTA_FLAG_STREAMINFO 0x0001
  430. #define DELTA_FLAG_FRAMEINFO 0x0002
  431. #define DELTA_MAX_FORMATS DELTA_MAX_DECODERS
  432. /*
  433. * struct delta_dev - device struct, 1 per probe (so single one for
  434. * all platform life)
  435. *
  436. * @v4l2_dev: v4l2 device
  437. * @vdev: v4l2 video device
  438. * @pdev: platform device
  439. * @dev: device
  440. * @m2m_dev: memory-to-memory V4L2 device
  441. * @lock: device lock, for crit section & V4L2 ops serialization.
  442. * @clk_delta: delta main clock
  443. * @clk_st231: st231 coprocessor main clock
  444. * @clk_flash_promip: flash promip clock
  445. * @decoders: list of registered decoders
  446. * @nb_of_decoders: nb of registered decoders
  447. * @pixelformats: supported uncompressed video formats
  448. * @nb_of_pixelformats: number of supported umcompressed video formats
  449. * @streamformats: supported compressed video formats
  450. * @nb_of_streamformats:number of supported compressed video formats
  451. * @instance_id: rolling counter identifying an instance (debug purpose)
  452. * @work_queue: decoding job work queue
  453. * @rpmsg_driver: rpmsg IPC driver
  454. * @rpmsg_device: rpmsg IPC device
  455. */
  456. struct delta_dev {
  457. struct v4l2_device v4l2_dev;
  458. struct video_device *vdev;
  459. struct platform_device *pdev;
  460. struct device *dev;
  461. struct v4l2_m2m_dev *m2m_dev;
  462. struct mutex lock;
  463. struct clk *clk_delta;
  464. struct clk *clk_st231;
  465. struct clk *clk_flash_promip;
  466. const struct delta_dec *decoders[DELTA_MAX_DECODERS];
  467. u32 nb_of_decoders;
  468. u32 pixelformats[DELTA_MAX_FORMATS];
  469. u32 nb_of_pixelformats;
  470. u32 streamformats[DELTA_MAX_FORMATS];
  471. u32 nb_of_streamformats;
  472. u8 instance_id;
  473. struct workqueue_struct *work_queue;
  474. struct rpmsg_driver rpmsg_driver;
  475. struct rpmsg_device *rpmsg_device;
  476. };
  477. static inline char *frame_type_str(u32 flags)
  478. {
  479. if (flags & V4L2_BUF_FLAG_KEYFRAME)
  480. return "I";
  481. if (flags & V4L2_BUF_FLAG_PFRAME)
  482. return "P";
  483. if (flags & V4L2_BUF_FLAG_BFRAME)
  484. return "B";
  485. if (flags & V4L2_BUF_FLAG_LAST)
  486. return "EOS";
  487. return "?";
  488. }
  489. static inline char *frame_field_str(enum v4l2_field field)
  490. {
  491. if (field == V4L2_FIELD_NONE)
  492. return "-";
  493. if (field == V4L2_FIELD_TOP)
  494. return "T";
  495. if (field == V4L2_FIELD_BOTTOM)
  496. return "B";
  497. if (field == V4L2_FIELD_INTERLACED)
  498. return "I";
  499. if (field == V4L2_FIELD_INTERLACED_TB)
  500. return "TB";
  501. if (field == V4L2_FIELD_INTERLACED_BT)
  502. return "BT";
  503. return "?";
  504. }
  505. static inline char *frame_state_str(u32 state, char *str, unsigned int len)
  506. {
  507. snprintf(str, len, "%s %s %s %s %s %s",
  508. (state & DELTA_FRAME_REF) ? "ref" : " ",
  509. (state & DELTA_FRAME_BSY) ? "bsy" : " ",
  510. (state & DELTA_FRAME_DEC) ? "dec" : " ",
  511. (state & DELTA_FRAME_OUT) ? "out" : " ",
  512. (state & DELTA_FRAME_M2M) ? "m2m" : " ",
  513. (state & DELTA_FRAME_RDY) ? "rdy" : " ");
  514. return str;
  515. }
  516. int delta_get_frameinfo_default(struct delta_ctx *ctx,
  517. struct delta_frameinfo *frameinfo);
  518. int delta_recycle_default(struct delta_ctx *pctx,
  519. struct delta_frame *frame);
  520. int delta_get_free_frame(struct delta_ctx *ctx,
  521. struct delta_frame **pframe);
  522. int delta_get_sync(struct delta_ctx *ctx);
  523. void delta_put_autosuspend(struct delta_ctx *ctx);
  524. #endif /* DELTA_H */