hpm_jpeg_drv.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_JPEG_DRV_H
  8. #define HPM_JPEG_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_jpeg_regs.h"
  11. /**
  12. * @brief Jpeg driver APIs
  13. * @defgroup Jpeg_interface JPEG driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. */
  17. /**
  18. * @brief Define events of the jpeg module
  19. */
  20. #define JPEG_EVENT_BUSY JPEG_STAT_BUSY_MASK
  21. #define JPEG_EVENT_OUT_DMA_FINISH JPEG_STAT_OUT_DMA_TRANSFER_DONE_MASK
  22. #define JPEG_EVENT_IN_DMA_FINISH JPEG_STAT_IN_DMA_TRANSFER_DONE_MASK
  23. #define JPEG_EVENT_ERROR (JPEG_STAT_RESTART_MARKER_ERROR_MASK | (0xF << 7))
  24. /**
  25. * @brief byte order in a word
  26. */
  27. #define JPEG_BYTE_ORDER_3210 (0U) /**< no order change, {A3, A2, A1, A0} */
  28. #define JPEG_BYTE_ORDER_2301 (1U) /**< order change, {A2, A3, A0, A1} */
  29. #define JPEG_BYTE_ORDER_1032 (2U) /**< order change, {A1, A0, A2, A3} */
  30. #define JPEG_BYTE_ORDER_0123 (3U) /**< order change, {A0, A1, A2, A3} */
  31. /**
  32. * @brief jpeg pixel conversion format
  33. */
  34. /**
  35. * @brief jpeg data format definition
  36. */
  37. #define JPEG_SUPPORTED_FORMAT_420 (0U) /**< hy=2, vy=2, hc=1, vc=1 */
  38. #define JPEG_SUPPORTED_FORMAT_422H (1U) /**< hy=2, vy=1, hc=1, vc=1 */
  39. #define JPEG_SUPPORTED_FORMAT_422V (2U) /**< hy=1, vy=2, hc=1, vc=1 */
  40. #define JPEG_SUPPORTED_FORMAT_444 (3U) /**< hy=1, vy=1, hc=1, vc=1 */
  41. #define JPEG_SUPPORTED_FORMAT_400 (4U) /**< hy=2, vy=2, hc=0, vc=0 */
  42. /**
  43. * @brief data format definition
  44. */
  45. typedef struct {
  46. uint8_t hy:2; /**< bit: 1-0 --> horizontal y component */
  47. uint8_t vy:2; /**< bit: 3-2 --> Vertical y component */
  48. uint8_t hc:2; /**< bit: bit: 5-4 --> horizontal c component */
  49. uint8_t vc:2; /**< bit: 7-6 --> Vertical c component */
  50. } jpeg_sampling_t;
  51. typedef struct {
  52. uint8_t pixel_width;
  53. uint8_t ipath;
  54. uint8_t opath;
  55. bool is_rgb;
  56. } jpeg_pixel_t;
  57. /**
  58. * @brief jpeg encoding and decoding configuration parameters
  59. * @arg bit: 31-27 --> name
  60. * @arg bit: 26-23 --> WIDTH IN BYTE
  61. * @arg bit: 22-3 --> ELEMENT COUNT
  62. * @arg bit: 2-0 --> TYPE
  63. */
  64. typedef enum jpeg_table {
  65. jpeg_table_qmem = 0x201002, /**< definition Decoder and Encoder Q. values */
  66. jpeg_table_huffenc = 0x201803, /**< definition Huffman Encoder table */
  67. jpeg_table_huffmin = 0x400104, /**< definition Huffman min values */
  68. jpeg_table_huffbase = 0x200405, /**< definition Huffman BASE mem values */
  69. jpeg_table_huffsymb = 0x101506, /**< definition Huffman SYMB mem values */
  70. } jpeg_table_t;
  71. typedef enum jpeg_pixel_format {
  72. jpeg_pixel_format_argb8888 = 0,
  73. jpeg_pixel_format_rgb565,
  74. jpeg_pixel_format_yuv422h1p,
  75. jpeg_pixel_format_yuv422h2p,
  76. jpeg_pixel_format_yuv420,
  77. jpeg_pixel_format_y8,
  78. } jpeg_pixel_format_t;
  79. /**
  80. * @brief jpeg encoding and decoding configuration parameters
  81. */
  82. typedef struct {
  83. uint8_t jpeg_format; /**< supported jpeg format */
  84. jpeg_pixel_format_t in_pixel_format;
  85. jpeg_pixel_format_t out_pixel_format;
  86. uint8_t in_byte_order; /**< byte order */
  87. uint8_t out_byte_order; /**< byte order */
  88. bool enable_ycbcr; /**< enable YCbCr or YUV */
  89. uint16_t width_in_pixel; /**< Image width register*/
  90. uint16_t height_in_pixel; /**< Image height register*/
  91. uint32_t in_buffer; /**< input buffer */
  92. uint32_t out_buffer; /**< output buffer */
  93. } jpeg_job_config_t;
  94. #ifdef __cplusplus
  95. extern "C" {
  96. #endif
  97. /**
  98. * @brief clear jpeg cfg Register
  99. *
  100. * @param [in] ptr JPEG base address, HPM_JPEG
  101. */
  102. static inline void jpeg_clear_cfg(JPEG_Type *ptr)
  103. {
  104. ptr->CFG = 0;
  105. }
  106. /**
  107. * @brief jpeg function disable
  108. *
  109. * @param [in] ptr JPEG base address, HPM_JPEG
  110. */
  111. static inline void jpeg_disable(JPEG_Type *ptr)
  112. {
  113. ptr->CFG &= ~JPEG_CFG_JPEG_EN_MASK;
  114. }
  115. /**
  116. * @brief jpeg function enable
  117. *
  118. * @param [in] ptr JPEG base address, HPM_JPEG
  119. */
  120. static inline void jpeg_enable(JPEG_Type *ptr)
  121. {
  122. ptr->CFG |= JPEG_CFG_JPEG_EN_MASK;
  123. }
  124. /**
  125. * @brief stop a encoder/decoder conversion
  126. *
  127. * @param [in] ptr JPEG base address, HPM_JPEG
  128. */
  129. static inline void jpeg_stop(JPEG_Type *ptr)
  130. {
  131. ptr->CFG &= ~JPEG_CFG_START_MASK;
  132. }
  133. /**
  134. * @brief start a new encoder/decoder conversion
  135. *
  136. * @param [in] ptr JPEG base address, HPM_JPEG
  137. */
  138. static inline void jpeg_start(JPEG_Type *ptr)
  139. {
  140. ptr->CFG |= JPEG_CFG_START_MASK;
  141. }
  142. /**
  143. * @brief obtain jpeg Status Register
  144. *
  145. * @param [in] ptr JPEG base address, HPM_JPEG
  146. * @retval jpeg register's status
  147. */
  148. static inline uint32_t jpeg_get_status(JPEG_Type *ptr)
  149. {
  150. return ptr->STAT;
  151. }
  152. /**
  153. * @brief clear jpeg Status Register
  154. *
  155. * @param [in] ptr JPEG base address, HPM_JPEG
  156. * @param [in] mask
  157. * @arg JPEG_EVENT_BUSY: the module is busy doing conversion and data transfer
  158. * @arg JPEG_EVENT_OUT_DMA_FINISH: OutDMA process done
  159. * @arg JPEG_EVENT_IN_DMA_FINISH: InDMA process done
  160. * @arg JPEG_EVENT_ERROR: the axi err
  161. *
  162. */
  163. static inline void jpeg_clear_status(JPEG_Type *ptr, uint32_t mask)
  164. {
  165. ptr->STAT |= mask;
  166. }
  167. /**
  168. * @brief Out DMA Bytes Counter
  169. *
  170. * @param [in] ptr JPEG base address, HPM_JPEG
  171. * @retval The out DMA counter
  172. */
  173. static inline uint32_t jpeg_get_encoded_length(JPEG_Type *ptr)
  174. {
  175. return JPEG_OUTDMACNT_VAL_GET(ptr->OUTDMACNT);
  176. }
  177. /**
  178. * @brief jpeg Software Reset
  179. *
  180. * @param [in] ptr JPEG base address, HPM_JPEG
  181. */
  182. static inline void jpeg_software_reset(JPEG_Type *ptr)
  183. {
  184. ptr->CFG |= JPEG_CFG_JPEG_SFTRST_MASK;
  185. ptr->CFG &= ~JPEG_CFG_JPEG_SFTRST_MASK;
  186. }
  187. /**
  188. * @brief stop a encoder/decoder conversion and Software Reset
  189. *
  190. * @param [in] ptr JPEG base address, HPM_JPEG
  191. */
  192. void jpeg_reset(JPEG_Type *ptr);
  193. /**
  194. * @brief jpeg enable interrupt
  195. *
  196. * @param [in] ptr JPEG base address, HPM_JPEG
  197. * @param [in] mask
  198. * @arg JPEG_EVENT_IN_DMA_FINISH: In DMA Done enable
  199. * @arg JPEG_EVENT_OUT_DMA_FINISH: interrupt enable for all interrupt sources of In DMA module
  200. * @arg JPEG_EVENT_ERROR: The jpg endec restart error interrupt enable
  201. *
  202. */
  203. void jpeg_enable_irq(JPEG_Type *ptr, uint32_t mask);
  204. /**
  205. * @brief jpeg disable interrupt
  206. *
  207. * @param [in] ptr JPEG base address, HPM_JPEG
  208. * @param [in] mask
  209. * @arg JPEG_EVENT_IN_DMA_FINISH: In DMA Done disable
  210. * @arg JPEG_EVENT_OUT_DMA_FINISH: interrupt disable for all interrupt sources of In DMA module
  211. * @arg JPEG_EVENT_ERROR: The jpg endec restart error interrupt disable
  212. *
  213. */
  214. void jpeg_disable_irq(JPEG_Type *ptr, uint32_t mask);
  215. /**
  216. * @brief stop a encoder/decoder conversion and Software Reset
  217. *
  218. * @param [in] ptr JPEG base address, HPM_JPEG
  219. */
  220. void jpeg_init(JPEG_Type *ptr);
  221. /**
  222. * @brief fill tables for jpeg controller
  223. *
  224. * @param [in] ptr JPEG base address, HPM_JPEG
  225. * @param [in] table
  226. * @arg jpeg_table_qmem: file describe for Decoder and Encoder Q. values
  227. * @arg jpeg_table_huffenc: file describe for Huffman Encoder table
  228. * @arg jpeg_table_huffmin: file describe for Huffman min values
  229. * @arg jpeg_table_huffbase: file describe for Huffman BASE mem values
  230. * @arg jpeg_table_huffsymb: file describe for Huffman SYMB mem values
  231. * @param [in] data
  232. * @arg huffenc: data for Huffman Encoder table
  233. * @arg huffmin: data for Huffman min values
  234. * @arg huffbase: data for Huffman BASE mem values
  235. * @arg huffsymb: data for Huffman SYMB mem values
  236. * @arg qetable: data for Encoder Q. values
  237. * @arg qdtable: data for Decoder Q. values
  238. * @param [in] count data length
  239. * @retval fill tables's status
  240. *
  241. */
  242. hpm_stat_t jpeg_fill_table(JPEG_Type *ptr, jpeg_table_t table, uint8_t *data, uint32_t count);
  243. /**
  244. * @brief it will start decoding, and the process status needs to be checked by
  245. * querying JPEG_EVENT
  246. *
  247. * @param [in] ptr JPEG base address, HPM_JPEG
  248. * @param [in] config config A pointer to the configuration struct of "jpeg_job_config_t"
  249. * @param [in] length Decoded data length
  250. * @retval jpeg decoding's status
  251. *
  252. */
  253. hpm_stat_t jpeg_start_decode(JPEG_Type *ptr, jpeg_job_config_t *config, uint32_t length);
  254. /**
  255. * @brief * it will start encoding, and the process status needs to be checked by
  256. * querying JPEG_EVENT
  257. *
  258. * @param [in] ptr JPEG base address, HPM_JPEG
  259. * @param [in] config config A pointer to the configuration struct of "jpeg_job_config_t"
  260. * @retval jpeg encoding's status
  261. *
  262. */
  263. hpm_stat_t jpeg_start_encode(JPEG_Type *ptr, jpeg_job_config_t *config);
  264. /**
  265. * @}
  266. *
  267. */
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* HPM_JPEG_DRV_H */