fsl_video_common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Copyright 2017, 2020-2021, 2023 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_VIDEO_COMMON_H_
  9. #define _FSL_VIDEO_COMMON_H_
  10. #include "fsl_common.h"
  11. /*
  12. * Change log:
  13. *
  14. * 1.1.0
  15. * - Add stack function which supports LIFO item management.
  16. *
  17. * 1.0.5
  18. * - Fix IAR Pa082 warning.
  19. *
  20. * 1.0.4
  21. * - Add LUT8 definition.
  22. *
  23. * 1.0.3
  24. * - Add RAW8 definition.
  25. *
  26. * 1.0.2
  27. * - Fixed MISRA-C 2012 issues.
  28. *
  29. * 1.0.1
  30. * - Update the VIDEO_DelayMs for bare metal.
  31. *
  32. * 1.0.0
  33. * - Initial version
  34. */
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /*! @brief Pixel format FOURCC. */
  39. #define FSL_VIDEO_FOURCC(a, b, c, d) \
  40. ((uint32_t)(a) | ((uint32_t)(b) << 8U) | ((uint32_t)(c) << 16U) | ((uint32_t)(d) << 24U))
  41. /*! @brief Macro to define resolution. */
  42. #define FSL_VIDEO_RESOLUTION(width, height) ((uint32_t)(width) | ((uint32_t)(height) << 16U))
  43. #define FSL_VIDEO_EXTRACT_WIDTH(resolution) ((uint16_t)((resolution)&0xFFFFU))
  44. #define FSL_VIDEO_EXTRACT_HEIGHT(resolution) ((uint16_t)((resolution) >> 16U))
  45. /*! @brief Pixel format definition. */
  46. typedef enum _video_pixel_format
  47. {
  48. /* RAW */
  49. kVIDEO_PixelFormatRAW8 = FSL_VIDEO_FOURCC('G', 'R', 'B', 'G'), /*!< RAW8, GRBG. */
  50. /* LUT/palette */
  51. kVIDEO_PixelFormatLUT8 = FSL_VIDEO_FOURCC('L', 'U', 'T', '8'), /*!< 8-bit Indexed Color. */
  52. /* RGB */
  53. kVIDEO_PixelFormatXRGB8888 = FSL_VIDEO_FOURCC('X', 'R', '2', '4'), /*!< 32-bit XRGB8888. */
  54. kVIDEO_PixelFormatRGBX8888 = FSL_VIDEO_FOURCC('R', 'X', '2', '4'), /*!< 32-bit RGBX8888. */
  55. kVIDEO_PixelFormatXBGR8888 = FSL_VIDEO_FOURCC('X', 'B', '2', '4'), /*!< 32-bit XBGR8888. */
  56. kVIDEO_PixelFormatBGRX8888 = FSL_VIDEO_FOURCC('B', 'X', '2', '4'), /*!< 32-bit BGRX8888. */
  57. kVIDEO_PixelFormatRGB888 = FSL_VIDEO_FOURCC('R', 'G', '2', '4'), /*!< 24-bit RGB888. */
  58. kVIDEO_PixelFormatBGR888 = FSL_VIDEO_FOURCC('B', 'G', '2', '4'), /*!< 24-bit BGR888. */
  59. kVIDEO_PixelFormatRGB565 = FSL_VIDEO_FOURCC('R', 'G', '1', '6'), /*!< 16-bit RGB565. */
  60. kVIDEO_PixelFormatBGR565 = FSL_VIDEO_FOURCC('B', 'G', '1', '6'), /*!< 16-bit BGR565. */
  61. kVIDEO_PixelFormatXRGB1555 = FSL_VIDEO_FOURCC('X', 'R', '1', '5'), /*!< 16-bit XRGB1555. */
  62. kVIDEO_PixelFormatRGBX5551 = FSL_VIDEO_FOURCC('R', 'X', '1', '5'), /*!< 16-bit RGBX5551. */
  63. kVIDEO_PixelFormatXBGR1555 = FSL_VIDEO_FOURCC('X', 'B', '1', '5'), /*!< 16-bit XBGR1555. */
  64. kVIDEO_PixelFormatBGRX5551 = FSL_VIDEO_FOURCC('B', 'X', '1', '5'), /*!< 16-bit BGRX5551. */
  65. kVIDEO_PixelFormatXRGB4444 = FSL_VIDEO_FOURCC('X', 'R', '1', '2'), /*!< 16-bit XRGB4444. */
  66. kVIDEO_PixelFormatRGBX4444 = FSL_VIDEO_FOURCC('R', 'X', '1', '2'), /*!< 16-bit RGBX4444. */
  67. kVIDEO_PixelFormatXBGR4444 = FSL_VIDEO_FOURCC('X', 'B', '1', '2'), /*!< 16-bit XBGR4444. */
  68. kVIDEO_PixelFormatBGRX4444 = FSL_VIDEO_FOURCC('B', 'X', '1', '2'), /*!< 16-bit BGRX4444. */
  69. /* YUV. */
  70. kVIDEO_PixelFormatYUYV = FSL_VIDEO_FOURCC('Y', 'U', 'Y', 'V'), /*!< YUV422, Y-U-Y-V. */
  71. kVIDEO_PixelFormatYVYU = FSL_VIDEO_FOURCC('Y', 'V', 'Y', 'U'), /*!< YUV422, Y-V-Y-U. */
  72. kVIDEO_PixelFormatUYVY = FSL_VIDEO_FOURCC('U', 'Y', 'V', 'Y'), /*!< YUV422, U-Y-V-Y. */
  73. kVIDEO_PixelFormatVYUY = FSL_VIDEO_FOURCC('V', 'Y', 'U', 'Y'), /*!< YUV422, V-Y-U-Y. */
  74. kVIDEO_PixelFormatXYUV = FSL_VIDEO_FOURCC('X', 'Y', 'U', 'V'), /*!< YUV444, X-Y-U-V. */
  75. kVIDEO_PixelFormatXYVU = FSL_VIDEO_FOURCC('X', 'Y', 'V', 'U'), /*!< YUV444, X-Y-V-U. */
  76. } video_pixel_format_t;
  77. /*! @brief Resolution definition. */
  78. typedef enum _video_resolution
  79. {
  80. kVIDEO_ResolutionVGA = FSL_VIDEO_RESOLUTION(640, 480), /*!< VGA, 640 * 480 */
  81. kVIDEO_ResolutionQVGA = FSL_VIDEO_RESOLUTION(320, 240), /*!< QVGA, 320 * 240 */
  82. kVIDEO_ResolutionQQVGA = FSL_VIDEO_RESOLUTION(160, 120), /*!< QQVGA, 160 * 120 */
  83. kVIDEO_ResolutionCIF = FSL_VIDEO_RESOLUTION(352, 288), /*!< CIF, 352 * 288 */
  84. kVIDEO_ResolutionQCIF = FSL_VIDEO_RESOLUTION(176, 144), /*!< QCIF, 176 * 144 */
  85. kVIDEO_ResolutionQQCIF = FSL_VIDEO_RESOLUTION(88, 72), /*!< QQCIF, 88 * 72 */
  86. kVIDEO_Resolution720P = FSL_VIDEO_RESOLUTION(1280, 720), /*!< 720P, 1280 * 720 */
  87. kVIDEO_Resolution1080P = FSL_VIDEO_RESOLUTION(1920, 1080), /*!< 1080P, 1920 * 1280*/
  88. kVIDEO_ResolutionWXGA = FSL_VIDEO_RESOLUTION(1280, 800), /*!< WXGA, 1280 * 800 */
  89. } video_resolution_t;
  90. /*!
  91. * @brief Ring buffer structure.
  92. *
  93. * There is one empty room reserved in the ring buffer, used to distinguish
  94. * whether the ring buffer is full or empty. When rear equals front, it is empty;
  95. * when rear+1 equals front, it is full.
  96. */
  97. typedef struct
  98. {
  99. volatile uint32_t rear; /*!< Pointer to save the incoming item. */
  100. volatile uint32_t front; /*!< Pointer to read out the item. */
  101. void *volatile *buf; /*!< Memory to the ring buffer. */
  102. uint32_t size; /*!< Ring buffer total size. */
  103. } video_ringbuf_t;
  104. /*!
  105. * @brief Memory pool structure.
  106. */
  107. typedef struct
  108. {
  109. void *volatile pool; /*!< Pointer to the pool. */
  110. volatile uint32_t cnt; /*!< Count of memory blocks in the pool. */
  111. } video_mempool_t;
  112. /*!
  113. * @brief Stack structure.
  114. */
  115. typedef struct
  116. {
  117. void **buf; /*!< Pointer to the memory to store the items. */
  118. volatile uint32_t top; /*!< Current top stack top. */
  119. uint32_t maxCount; /*!< Maximal count of items can be stored in the stack. */
  120. } video_stack_t;
  121. /*******************************************************************************
  122. * API
  123. ******************************************************************************/
  124. #if defined(__cplusplus)
  125. extern "C" {
  126. #endif
  127. /*!
  128. * @name Common
  129. * @{
  130. */
  131. /*!
  132. * @brief Check the pixel format is YUV or not.
  133. *
  134. * @param format Pixel format.
  135. */
  136. bool VIDEO_IsYUV(video_pixel_format_t format);
  137. /*!
  138. * @brief Delay the specific time.
  139. *
  140. * @param ms How many milli-second to delay.
  141. */
  142. void VIDEO_DelayMs(uint32_t ms);
  143. /*!
  144. * @brief Get the pixel size in bits.
  145. *
  146. * @param pixelFormat The pixel format.
  147. * @return Bits per pixel.
  148. */
  149. uint8_t VIDEO_GetPixelSizeBits(video_pixel_format_t pixelFormat);
  150. /* @} */
  151. /*!
  152. * @name Ring buffer.
  153. * @{
  154. */
  155. /*!
  156. * @brief Initializes ring buffer.
  157. *
  158. * @param ringbuf Pointer to the ring buffer handle.
  159. * @param buf Memory to save the items.
  160. * @param size Size of the @p buf.
  161. * @return Returns @ref kStatus_Success if initialize success, otherwise returns
  162. * error code.
  163. */
  164. status_t VIDEO_RINGBUF_Init(video_ringbuf_t *ringbuf, void **buf, uint32_t size);
  165. /*!
  166. * @brief Get one item from the ring buffer.
  167. *
  168. * @param ringbuf Pointer to the ring buffer handle.
  169. * @param item Memory to save the item.
  170. * @return Returns @ref kStatus_Success if get success, otherwise returns
  171. * error code.
  172. */
  173. status_t VIDEO_RINGBUF_Get(video_ringbuf_t *ringbuf, void **item);
  174. /*!
  175. * @brief Put one item to the ring buffer.
  176. *
  177. * @param ringbuf Pointer to the ring buffer handle.
  178. * @param item The new item to save.
  179. * @return Returns @ref kStatus_Success if put success, otherwise returns
  180. * error code.
  181. */
  182. status_t VIDEO_RINGBUF_Put(video_ringbuf_t *ringbuf, void *item);
  183. /*!
  184. * @brief Get current count of items in the ring buffer.
  185. *
  186. * @param ringbuf Pointer to the ring buffer handle.
  187. * @return Returns the item count.
  188. */
  189. uint32_t VIDEO_RINGBUF_GetLength(video_ringbuf_t *ringbuf);
  190. /*!
  191. * @brief Check whether the ring buffer is empty.
  192. *
  193. * @param ringbuf Pointer to the ring buffer handle.
  194. * @return Returns true if the ring buffer is empty, otherwise returns false.
  195. */
  196. bool VIDEO_RINGBUF_IsEmpty(video_ringbuf_t *ringbuf);
  197. /*!
  198. * @brief Check whether the ring buffer is full.
  199. *
  200. * @param ringbuf Pointer to the ring buffer handle.
  201. * @return Returns true if the ring buffer is full, otherwise returns false.
  202. */
  203. bool VIDEO_RINGBUF_IsFull(video_ringbuf_t *ringbuf);
  204. /* @} */
  205. /*!
  206. * @name Memory Pool
  207. *
  208. * User can put memory block to the pool, or get memory block from the pool.
  209. * There is no count limitation to put memory block in to the pool. The memory
  210. * content in the pool might be modified.
  211. *
  212. * The memory block should be 4-byte aligned, and the dividable by 4-byte.
  213. *
  214. * @{
  215. */
  216. /*!
  217. * @brief Initializes memory pool.
  218. *
  219. * Initializes memory pool. Initial memory blocks in the memory pool is optional.
  220. * If initial blocks are used, user should specify the initial block size and count.
  221. *
  222. * @param mempool Pointer to the memory pool handle.
  223. * @param initMem Initial memory blocks to saved in the pool.
  224. * @param size Every memory block's size (bytes) in the @p initMem.
  225. * @param count Number of memory blocks @p initMem.
  226. * @return Returns @ref kStatus_Success if initialize success, otherwise returns
  227. * error code.
  228. */
  229. status_t VIDEO_MEMPOOL_Init(video_mempool_t *mempool, void *initMem, uint32_t size, uint32_t count);
  230. /*!
  231. * @brief Create an empty memory pool.
  232. *
  233. * @param mempool Pointer to the memory pool handle.
  234. */
  235. void VIDEO_MEMPOOL_InitEmpty(video_mempool_t *mempool);
  236. /*!
  237. * @brief Put memory block in the pool.
  238. *
  239. * @param mempool Pointer to the memory pool handle.
  240. * @param mem Pointer to the memory block.
  241. */
  242. void VIDEO_MEMPOOL_Put(video_mempool_t *mempool, void *mem);
  243. /*!
  244. * @brief Get memory block in the pool.
  245. *
  246. * @param mempool Pointer to the memory pool handle.
  247. * @return The memory block get from pool. If the pool is empty, returns NULL.
  248. */
  249. void *VIDEO_MEMPOOL_Get(video_mempool_t *mempool);
  250. /*!
  251. * @brief How many memory blocks in the pool.
  252. *
  253. * @param mempool Pointer to the memory pool handle.
  254. * @return The memory block count in the pool
  255. */
  256. uint32_t VIDEO_MEMPOOL_GetCount(video_mempool_t *mempool);
  257. /* @} */
  258. /*!
  259. * @name Stack which supports LIFO item management.
  260. * @{
  261. */
  262. /*!
  263. * @brief Initializes stack.
  264. *
  265. * @param stack Pointer to the stack handle.
  266. * @param buf Memory to save the items.
  267. * @param size Size of the @p buf.
  268. * @return Returns @ref kStatus_Success if initialize success, otherwise returns
  269. * error code.
  270. */
  271. status_t VIDEO_STACK_Init(video_stack_t *stack, void **buf, uint32_t size);
  272. /*!
  273. * @brief Pop one item from the stack.
  274. *
  275. * @param stack Pointer to the stack handle.
  276. * @param item Memory to save the item.
  277. * @return Returns @ref kStatus_Success if get success, returns
  278. * kStatus_Fail if the stack is empty.
  279. */
  280. status_t VIDEO_STACK_Pop(video_stack_t *stack, void **item);
  281. /*!
  282. * @brief Put one item to the stack.
  283. *
  284. * @param stack Pointer to the stack handle.
  285. * @param item The new item to save.
  286. * @return Returns @ref kStatus_Success if put success, returns
  287. * kStatus_Fail if the stack is full.
  288. */
  289. status_t VIDEO_STACK_Push(video_stack_t *stack, void *item);
  290. /*!
  291. * @brief Get current count of items in the stack.
  292. *
  293. * @param stack Pointer to the stack handle.
  294. * @return Returns the item count.
  295. */
  296. static inline uint32_t VIDEO_STACK_GetCount(video_stack_t *stack)
  297. {
  298. return stack->top;
  299. }
  300. /*!
  301. * @brief Get maxiumal count of items in the stack.
  302. *
  303. * @param stack Pointer to the stack handle.
  304. * @return Returns the maxiumal count of items in the stack.
  305. */
  306. static inline uint32_t VIDEO_STACK_GetMaxCount(video_stack_t *stack)
  307. {
  308. return stack->maxCount;
  309. }
  310. /* @} */
  311. #if defined(__cplusplus)
  312. }
  313. #endif
  314. #endif /* _FSL_VIDEO_COMMON_H_ */