You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

3461 lines
116 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_jpeg.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 14-April-2017
  7. * @brief JPEG HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the JPEG encoder/decoder peripheral:
  10. * + Initialization and de-initialization functions
  11. * + JPEG processing functions encoding and decoding
  12. * + JPEG decoding Getting Info and encoding configuration setting
  13. * + JPEG enable/disable header parsing functions (for decoding)
  14. * + JPEG Input/Output Buffer configuration.
  15. * + JPEG callback functions
  16. * + JPEG Abort/Pause/Resume functions
  17. * + JPEG custom quantization tables setting functions
  18. * + IRQ handler management
  19. * + Peripheral State and Error functions
  20. *
  21. @verbatim
  22. ==============================================================================
  23. ##### How to use this driver #####
  24. ==============================================================================
  25. [..]
  26. (#) Initialize the JPEG peripheral using HAL_JPEG_Init : No initialization parameters are required.
  27. Only the call to HAL_JPEG_Init is necessary to initialize the JPEG peripheral.
  28. (#) If operation is JPEG encoding use function HAL_JPEG_ConfigEncoding to set
  29. the encoding parameters (mandatory before calling the encoding function).
  30. the application can change the encoding parameter "ImageQuality" from
  31. 1 to 100 to obtain a more or less quality (visual quality vs the original row image),
  32. and inversely more or less jpg file size.
  33. (#) Note that for decoding operation the JPEG peripheral output data are organized in
  34. YCbCr blocks called MCU (Minimum Coded Unit) as defioned in the JPEG specification
  35. ISO/IEC 10918-1 standard.
  36. It is up to the application to transform these YCbCr blocks to RGB data that can be display.
  37. Respectively, for Encoding operation the JPEG peripheral input should be organized
  38. in YCbCr MCU blocks. It is up to the application to perform the necessary RGB to YCbCr
  39. MCU blocks transformation before feeding the JPEG peripheral with data.
  40. (#) Use functions HAL_JPEG_Encode and HAL_JPEG_Decode to start respectively
  41. a JPEG encoding/decoding operation in polling method (blocking).
  42. (#) Use functions HAL_JPEG_Encode_IT and HAL_JPEG_Decode_IT to start respectively
  43. a JPEG encoding/decoding operation with Interrupt method (not blocking).
  44. (#) Use functions HAL_JPEG_Encode_DMA and HAL_JPEG_Decode_DMA to start respectively
  45. a JPEG encoding/decoding operation with DMA method (not blocking).
  46. (#) Callback HAL_JPEG_InfoReadyCallback is asserted if the current operation
  47. is a JPEG decoding to provide the application with JPEG image parameters.
  48. This callback is asserted when the JPEG peripheral successfully parse the
  49. JPEG header.
  50. (#) Callback HAL_JPEG_GetDataCallback is asserted for both encoding and decoding
  51. operations to inform the application that the input buffer has been
  52. consumed by the peripheral and to ask for a new data chunk if the operation
  53. (encoding/decoding) has not been complete yet.
  54. (++) This CallBack should be implemented in the application side. It should
  55. call the function HAL_JPEG_ConfigInputBuffer if new input data are available,
  56. or call HAL_JPEG_Pause with parameter XferSelection set to JPEG_PAUSE_RESUME_INPUT
  57. to inform the JPEG HAL driver that the ongoing operation shall pause waiting for the
  58. application to provide a new input data chunk.
  59. Once the application succeed getting new data and if the input has been paused,
  60. the application can call the function HAL_JPEG_ConfigInputBuffer to set the new
  61. input buffer and size, then resume the JPEG HAL input by calling new function HAL_JPEG_Resume.
  62. If the application has ended feeding the HAL JPEG with input data (no more input data), the application
  63. Should call the function HAL_JPEG_ConfigInputBuffer (within the callback HAL_JPEG_GetDataCallback)
  64. with the parameter InDataLength set to zero.
  65. (++) The mechanism of HAL_JPEG_ConfigInputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
  66. to the application to provide the input data (for encoding or decoding) by chunks.
  67. If the new input data chunk is not available (because data should be read from an input file
  68. for example) the application can pause the JPEG input (using function HAL_JPEG_Pause)
  69. Once the new input data chunk is available ( read from a file for example), the application
  70. can call the function HAL_JPEG_ConfigInputBuffer to provide the HAL with the new chunk
  71. then resume the JPEG HAL input by calling function HAL_JPEG_Resume.
  72. (++) The application can call functions HAL_JPEG_ConfigInputBuffer then HAL_JPEG_Resume.
  73. any time (outside the HAL_JPEG_GetDataCallback) Once the new input chunk data available.
  74. However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
  75. (if necessary) within the callback HAL_JPEG_GetDataCallback, i.e when the HAL JPEG has ended
  76. Transferring the previous chunk buffer to the JPEG peripheral.
  77. (#) Callback HAL_JPEG_DataReadyCallback is asserted when the HAL JPEG driver
  78. has filled the given output buffer with the given size.
  79. (++) This CallBack should be implemented in the application side. It should
  80. call the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
  81. with the new output buffer location and size to be used to store next data chunk.
  82. if the application is not ready to provide the output chunk location then it can
  83. call the function HAL_JPEG_Pause with parameter XferSelection set to "JPEG_PAUSE_RESUME_OUTPUT"
  84. to inform the JPEG HAL driver that it shall pause output data. Once the application
  85. is ready to receive the new data chunk (output buffer location free or available) it should call
  86. the function HAL_JPEG_ConfigOutputBuffer to provide the HAL JPEG driver
  87. with the new output chunk buffer location and size, then call "HAL_JPEG_Resume"
  88. to inform the HAL that it shall resume outputting data in the given output buffer.
  89. (++) The mechanism of HAL_JPEG_ConfigOutputBuffer/HAL_JPEG_Pause/HAL_JPEG_Resume allows
  90. the application to receive data from the JPEG peripheral by chunks. when a chunk
  91. is received, the application can pause the HAL JPEG output data to be able to process
  92. these received data (YCbCr to RGB conversion in case of decoding or data storage in case
  93. of encoding).
  94. (++) The application can call functions HAL_JPEG_ ConfigOutputBuffer then HAL_JPEG_Resume.
  95. any time (outside the HAL_JPEG_DataReadyCallback) Once the output data buffer is free to use.
  96. However, to keep data coherency, the function HAL_JPEG_Pause must be imperatively called
  97. (if necessary) within the callback HAL_JPEG_ DataReadyCallback, i.e when the HAL JPEG has ended
  98. Transferring the previous chunk buffer from the JPEG peripheral to the application.
  99. (#) Callback HAL_JPEG_EncodeCpltCallback is asserted when the HAL JPEG driver has
  100. ended the current JPEG encoding operation, and all output data has been transmitted
  101. to the application.
  102. (#) Callback HAL_JPEG_DecodeCpltCallback is asserted when the HAL JPEG driver has
  103. ended the current JPEG decoding operation. and all output data has been transmitted
  104. to the application.
  105. (#) Callback HAL_JPEG_ErrorCallback is asserted when an error occurred during
  106. the current operation. the application can call the function "HAL_JPEG_GetError"
  107. to retrieve the error codes.
  108. (#) By default the HAL JPEG driver uses the default quantization tables
  109. as provide in the JPEG specification (ISO/IEC 10918-1 standard) for encoding.
  110. User can change these default tables if necessary using the function HAL_JPEG_SetUserQuantTables
  111. Note that for decoding the quantization tables are automatically extracted from
  112. the JPEG header.
  113. (#) To control JPEG state you can use the following function: HAL_JPEG_GetState()
  114. *** JPEG HAL driver macros list ***
  115. =============================================
  116. [..]
  117. Below the list of most used macros in JPEG HAL driver.
  118. (+) __HAL_JPEG_RESET_HANDLE_STATE : Reset JPEG handle state.
  119. (+) __HAL_JPEG_ENABLE : Enable the JPEG peripheral.
  120. (+) __HAL_JPEG_DISABLE : Disable the JPEG peripheral.
  121. (+) __HAL_JPEG_GET_FLAG : Check the specified JPEG status flag.
  122. (+) __HAL_JPEG_CLEAR_FLAG : Clear the specified JPEG status flag.
  123. (+) __HAL_JPEG_ENABLE_IT : Enable the specified JPEG Interrupt.
  124. (+) __HAL_JPEG_DISABLE_IT : Disable the specified JPEG Interrupt.
  125. (+) __HAL_JPEG_GET_IT_SOURCE : returns the state of the specified JPEG Interrupt (Enabled or disabled).
  126. @endverbatim
  127. ******************************************************************************
  128. * @attention
  129. *
  130. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  131. *
  132. * Redistribution and use in source and binary forms, with or without modification,
  133. * are permitted provided that the following conditions are met:
  134. * 1. Redistributions of source code must retain the above copyright notice,
  135. * this list of conditions and the following disclaimer.
  136. * 2. Redistributions in binary form must reproduce the above copyright notice,
  137. * this list of conditions and the following disclaimer in the documentation
  138. * and/or other materials provided with the distribution.
  139. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  140. * may be used to endorse or promote products derived from this software
  141. * without specific prior written permission.
  142. *
  143. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  144. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  145. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  146. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  147. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  148. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  149. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  150. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  151. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  152. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  153. *
  154. ******************************************************************************
  155. */
  156. /* Includes ------------------------------------------------------------------*/
  157. #include "stm32f7xx_hal.h"
  158. /** @addtogroup STM32F7xx_HAL_Driver
  159. * @{
  160. */
  161. /** @defgroup JPEG JPEG
  162. * @brief JPEG HAL module driver.
  163. * @{
  164. */
  165. #ifdef HAL_JPEG_MODULE_ENABLED
  166. #if defined (STM32F767xx) || defined (STM32F769xx) || defined (STM32F777xx) || defined (STM32F779xx)
  167. /* Private define ------------------------------------------------------------*/
  168. /** @addtogroup JPEG_Private_Constants
  169. * @{
  170. */
  171. #define JPEG_TIMEOUT_VALUE ((uint32_t)1000U) /* 1s */
  172. #define JPEG_AC_HUFF_TABLE_SIZE ((uint32_t)162U) /* Huffman AC table size : 162 codes*/
  173. #define JPEG_DC_HUFF_TABLE_SIZE ((uint32_t)12U) /* Huffman AC table size : 12 codes*/
  174. #define JPEG_FIFO_SIZE ((uint32_t)16U) /* JPEG Input/Output HW FIFO size in words*/
  175. #define JPEG_INTERRUPT_MASK ((uint32_t)0x0000007EU) /* JPEG Interrupt Mask*/
  176. #define JPEG_DMA_MASK ((uint32_t)0x00001800U) /* JPEG DMA request Mask*/
  177. #define JPEG_DMA_IDMA ((uint32_t)JPEG_CR_IDMAEN) /* DMA request for the input FIFO */
  178. #define JPEG_DMA_ODMA ((uint32_t)JPEG_CR_ODMAEN) /* DMA request for the output FIFO */
  179. #define JPEG_CONTEXT_ENCODE ((uint32_t)0x00000001U) /* JPEG context : operation is encoding*/
  180. #define JPEG_CONTEXT_DECODE ((uint32_t)0x00000002U) /* JPEG context : operation is decoding*/
  181. #define JPEG_CONTEXT_OPERATION_MASK ((uint32_t)0x00000003U) /* JPEG context : operation Mask */
  182. #define JPEG_CONTEXT_POLLING ((uint32_t)0x00000004U) /* JPEG context : Transfer use Polling */
  183. #define JPEG_CONTEXT_IT ((uint32_t)0x00000008U) /* JPEG context : Transfer use Interrupt */
  184. #define JPEG_CONTEXT_DMA ((uint32_t)0x0000000CU) /* JPEG context : Transfer use DMA */
  185. #define JPEG_CONTEXT_METHOD_MASK ((uint32_t)0x0000000CU) /* JPEG context : Transfer Mask */
  186. #define JPEG_CONTEXT_CONF_ENCODING ((uint32_t)0x00000100U) /* JPEG context : encoding config done */
  187. #define JPEG_CONTEXT_PAUSE_INPUT ((uint32_t)0x00001000U) /* JPEG context : Pause Input */
  188. #define JPEG_CONTEXT_PAUSE_OUTPUT ((uint32_t)0x00002000U) /* JPEG context : Pause Output */
  189. #define JPEG_CONTEXT_CUSTOM_TABLES ((uint32_t)0x00004000U) /* JPEG context : Use custom quantization tables */
  190. #define JPEG_CONTEXT_ENDING_DMA ((uint32_t)0x00008000U) /* JPEG context : ending with DMA in progress */
  191. #define JPEG_PROCESS_ONGOING ((uint32_t)0x00000000U) /* Process is on going */
  192. #define JPEG_PROCESS_DONE ((uint32_t)0x00000001U) /* Process is done (ends) */
  193. /**
  194. * @}
  195. */
  196. /* Private typedef -----------------------------------------------------------*/
  197. /** @addtogroup JPEG_Private_Types
  198. * @{
  199. */
  200. /*
  201. JPEG Huffman Table Structure definition :
  202. This implementation of Huffman table structure is compliant with ISO/IEC 10918-1 standard , Annex C Huffman Table specification
  203. */
  204. typedef struct
  205. {
  206. /* These two fields directly represent the contents of a JPEG DHT marker */
  207. uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
  208. uint8_t HuffVal[162]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
  209. }JPEG_ACHuffTableTypeDef;
  210. typedef struct
  211. {
  212. /* These two fields directly represent the contents of a JPEG DHT marker */
  213. uint8_t Bits[16]; /*!< bits[k] = # of symbols with codes of length k bits, this parameter corresponds to BITS list in the Annex C */
  214. uint8_t HuffVal[12]; /*!< The symbols, in order of incremented code length, this parameter corresponds to HUFFVAL list in the Annex C */
  215. }JPEG_DCHuffTableTypeDef;
  216. typedef struct
  217. {
  218. uint8_t CodeLength[JPEG_AC_HUFF_TABLE_SIZE]; /*!< Code length */
  219. uint32_t HuffmanCode[JPEG_AC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
  220. }JPEG_AC_HuffCodeTableTypeDef;
  221. typedef struct
  222. {
  223. uint8_t CodeLength[JPEG_DC_HUFF_TABLE_SIZE]; /*!< Code length */
  224. uint32_t HuffmanCode[JPEG_DC_HUFF_TABLE_SIZE]; /*!< HuffmanCode */
  225. }JPEG_DC_HuffCodeTableTypeDef;
  226. /**
  227. * @}
  228. */
  229. /* Private macro -------------------------------------------------------------*/
  230. /** @addtogroup JPEG_Private_Macros
  231. * @{
  232. */
  233. #define JPEG_ENABLE_DMA(__HANDLE__,__DMA__) ((__HANDLE__)->Instance->CR |= ((__DMA__) & JPEG_DMA_MASK))
  234. /*note : To disable a DMA request we must use MODIFY_REG macro to avoid writing "1" to the FIFO flush bits
  235. located in the same DMA request enable register (CR register). */
  236. #define JPEG_DISABLE_DMA(__HANDLE__,__DMA__) MODIFY_REG((__HANDLE__)->Instance->CR, ((__DMA__) & JPEG_DMA_MASK), 0)
  237. /**
  238. * @}
  239. */
  240. /* Private variables ---------------------------------------------------------*/
  241. /** @addtogroup JPEG_Private_Variables
  242. * @{
  243. */
  244. static const JPEG_DCHuffTableTypeDef JPEG_DCLUM_HuffTable =
  245. {
  246. { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, /*Bits*/
  247. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
  248. };
  249. static const JPEG_DCHuffTableTypeDef JPEG_DCCHROM_HuffTable =
  250. {
  251. { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /*Bits*/
  252. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb } /*HUFFVAL */
  253. };
  254. static const JPEG_ACHuffTableTypeDef JPEG_ACLUM_HuffTable =
  255. {
  256. { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d }, /*Bits*/
  257. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, /*HUFFVAL */
  258. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  259. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  260. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  261. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  262. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  263. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  264. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  265. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  266. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  267. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  268. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  269. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  270. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  271. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  272. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  273. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  274. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  275. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  276. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  277. 0xf9, 0xfa }
  278. };
  279. static const JPEG_ACHuffTableTypeDef JPEG_ACCHROM_HuffTable =
  280. {
  281. { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 }, /*Bits*/
  282. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, /*HUFFVAL */
  283. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  284. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  285. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  286. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  287. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  288. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  289. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  290. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  291. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  292. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  293. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  294. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  295. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  296. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  297. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  298. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  299. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  300. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  301. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  302. 0xf9, 0xfa }
  303. };
  304. /*
  305. These are the sample quantization tables given in JPEG spec ISO/IEC 10918-1 standard , section K.1.
  306. */
  307. static const uint8_t JPEG_LUM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
  308. {
  309. 16, 11, 10, 16, 24, 40, 51, 61,
  310. 12, 12, 14, 19, 26, 58, 60, 55,
  311. 14, 13, 16, 24, 40, 57, 69, 56,
  312. 14, 17, 22, 29, 51, 87, 80, 62,
  313. 18, 22, 37, 56, 68, 109, 103, 77,
  314. 24, 35, 55, 64, 81, 104, 113, 92,
  315. 49, 64, 78, 87, 103, 121, 120, 101,
  316. 72, 92, 95, 98, 112, 100, 103, 99
  317. };
  318. static const uint8_t JPEG_CHROM_QuantTable[JPEG_QUANT_TABLE_SIZE] =
  319. {
  320. 17, 18, 24, 47, 99, 99, 99, 99,
  321. 18, 21, 26, 66, 99, 99, 99, 99,
  322. 24, 26, 56, 99, 99, 99, 99, 99,
  323. 47, 66, 99, 99, 99, 99, 99, 99,
  324. 99, 99, 99, 99, 99, 99, 99, 99,
  325. 99, 99, 99, 99, 99, 99, 99, 99,
  326. 99, 99, 99, 99, 99, 99, 99, 99,
  327. 99, 99, 99, 99, 99, 99, 99, 99
  328. };
  329. static const uint8_t JPEG_ZIGZAG_ORDER[JPEG_QUANT_TABLE_SIZE] =
  330. {
  331. 0, 1, 8, 16, 9, 2, 3, 10,
  332. 17, 24, 32, 25, 18, 11, 4, 5,
  333. 12, 19, 26, 33, 40, 48, 41, 34,
  334. 27, 20, 13, 6, 7, 14, 21, 28,
  335. 35, 42, 49, 56, 57, 50, 43, 36,
  336. 29, 22, 15, 23, 30, 37, 44, 51,
  337. 58, 59, 52, 45, 38, 31, 39, 46,
  338. 53, 60, 61, 54, 47, 55, 62, 63
  339. };
  340. /**
  341. * @}
  342. */
  343. /* Private function prototypes -----------------------------------------------*/
  344. /** @addtogroup JPEG_Private_Functions_Prototypes
  345. * @{
  346. */
  347. static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK);
  348. static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable);
  349. static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable);
  350. static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress);
  351. static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress);
  352. static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
  353. static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1);
  354. static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress);
  355. static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg);
  356. static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg);
  357. static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg);
  358. static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg);
  359. static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg);
  360. static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords);
  361. static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords);
  362. static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg);
  363. static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg);
  364. static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg);
  365. static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg);
  366. static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg);
  367. static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma);
  368. static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma);
  369. static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma);
  370. static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma) ;
  371. /**
  372. * @}
  373. */
  374. /** @defgroup JPEG_Exported_Functions JPEG Exported Functions
  375. * @{
  376. */
  377. /** @defgroup JPEG_Exported_Functions_Group1 Initialization and de-initialization functions
  378. * @brief Initialization and de-initialization functions.
  379. *
  380. @verbatim
  381. ==============================================================================
  382. ##### Initialization and de-initialization functions #####
  383. ==============================================================================
  384. [..] This section provides functions allowing to:
  385. (+) Initialize the JPEG peripheral and creates the associated handle
  386. (+) DeInitialize the JPEG peripheral
  387. @endverbatim
  388. * @{
  389. */
  390. /**
  391. * @brief Initializes the JPEG according to the specified
  392. * parameters in the JPEG_InitTypeDef and creates the associated handle.
  393. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  394. * the configuration information for JPEG module
  395. * @retval HAL status
  396. */
  397. HAL_StatusTypeDef HAL_JPEG_Init(JPEG_HandleTypeDef *hjpeg)
  398. {
  399. /*Note : these intermediate variables are used to avoid MISRA warning
  400. regarding rule 11.5 */
  401. uint32_t acLum_huffmanTableAddr = (uint32_t)(&JPEG_ACLUM_HuffTable);
  402. uint32_t dcLum_huffmanTableAddr = (uint32_t)(&JPEG_DCLUM_HuffTable);
  403. uint32_t acChrom_huffmanTableAddr = (uint32_t)(&JPEG_ACCHROM_HuffTable);
  404. uint32_t dcChrom_huffmanTableAddr = (uint32_t)(&JPEG_DCCHROM_HuffTable);
  405. /* Check the JPEG handle allocation */
  406. if(hjpeg == NULL)
  407. {
  408. return HAL_ERROR;
  409. }
  410. if(hjpeg->State == HAL_JPEG_STATE_RESET)
  411. {
  412. /* Allocate lock resource and initialize it */
  413. hjpeg->Lock = HAL_UNLOCKED;
  414. /* Init the low level hardware : GPIO, CLOCK */
  415. HAL_JPEG_MspInit(hjpeg);
  416. }
  417. /* Change the JPEG state */
  418. hjpeg->State = HAL_JPEG_STATE_BUSY;
  419. /* Start the JPEG Core*/
  420. __HAL_JPEG_ENABLE(hjpeg);
  421. /* Stop the JPEG encoding/decoding process*/
  422. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  423. /* Disable All Interrupts */
  424. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  425. /* Disable All DMA requests */
  426. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  427. /* Flush input and output FIFOs*/
  428. hjpeg->Instance->CR |= JPEG_CR_IFF;
  429. hjpeg->Instance->CR |= JPEG_CR_OFF;
  430. /* Clear all flags */
  431. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  432. hjpeg->QuantTable0 = (uint8_t *)JPEG_LUM_QuantTable;
  433. hjpeg->QuantTable1 = (uint8_t *)JPEG_CHROM_QuantTable;
  434. hjpeg->QuantTable2 = NULL;
  435. hjpeg->QuantTable3 = NULL;
  436. /* init the default Huffman tables*/
  437. if(JPEG_Set_HuffEnc_Mem(hjpeg, (JPEG_ACHuffTableTypeDef *)acLum_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcLum_huffmanTableAddr, (JPEG_ACHuffTableTypeDef *)acChrom_huffmanTableAddr, (JPEG_DCHuffTableTypeDef *)dcChrom_huffmanTableAddr) != HAL_OK)
  438. {
  439. hjpeg->ErrorCode = HAL_JPEG_ERROR_HUFF_TABLE;
  440. return HAL_ERROR;
  441. }
  442. /* Enable header processing*/
  443. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
  444. /* Reset JpegInCount and JpegOutCount */
  445. hjpeg->JpegInCount = 0;
  446. hjpeg->JpegOutCount = 0;
  447. /* Change the JPEG state */
  448. hjpeg->State = HAL_JPEG_STATE_READY;
  449. /* Reset the JPEG ErrorCode */
  450. hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
  451. /*Clear the context filelds*/
  452. hjpeg->Context = 0;
  453. /* Return function status */
  454. return HAL_OK;
  455. }
  456. /**
  457. * @brief DeInitializes the JPEG peripheral.
  458. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  459. * the configuration information for JPEG module
  460. * @retval HAL status
  461. */
  462. HAL_StatusTypeDef HAL_JPEG_DeInit(JPEG_HandleTypeDef *hjpeg)
  463. {
  464. /* Check the JPEG handle allocation */
  465. if(hjpeg == NULL)
  466. {
  467. return HAL_ERROR;
  468. }
  469. /* DeInit the low level hardware: CLOCK, NVIC.*/
  470. HAL_JPEG_MspDeInit(hjpeg);
  471. /* Change the JPEG state */
  472. hjpeg->State = HAL_JPEG_STATE_BUSY;
  473. /* Reset the JPEG ErrorCode */
  474. hjpeg->ErrorCode = HAL_JPEG_ERROR_NONE;
  475. /* Reset JpegInCount and JpegOutCount */
  476. hjpeg->JpegInCount = 0;
  477. hjpeg->JpegOutCount = 0;
  478. /* Change the JPEG state */
  479. hjpeg->State = HAL_JPEG_STATE_RESET;
  480. /*Clear the context fields*/
  481. hjpeg->Context = 0;
  482. /* Release Lock */
  483. __HAL_UNLOCK(hjpeg);
  484. /* Return function status */
  485. return HAL_OK;
  486. }
  487. /**
  488. * @brief Initializes the JPEG MSP.
  489. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  490. * the configuration information for JPEG module
  491. * @retval None
  492. */
  493. __weak void HAL_JPEG_MspInit(JPEG_HandleTypeDef *hjpeg)
  494. {
  495. /* Prevent unused argument(s) compilation warning */
  496. UNUSED(hjpeg);
  497. /* NOTE : This function Should not be modified, when the callback is needed,
  498. the HAL_JPEG_MspInit could be implemented in the user file
  499. */
  500. }
  501. /**
  502. * @brief DeInitializes JPEG MSP.
  503. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  504. * the configuration information for JPEG module
  505. * @retval None
  506. */
  507. __weak void HAL_JPEG_MspDeInit(JPEG_HandleTypeDef *hjpeg)
  508. {
  509. /* Prevent unused argument(s) compilation warning */
  510. UNUSED(hjpeg);
  511. /* NOTE : This function Should not be modified, when the callback is needed,
  512. the HAL_JPEG_MspDeInit could be implemented in the user file
  513. */
  514. }
  515. /**
  516. * @}
  517. */
  518. /** @defgroup JPEG_Exported_Functions_Group2 Configuration functions
  519. * @brief JPEG Configuration functions.
  520. *
  521. @verbatim
  522. ==============================================================================
  523. ##### Configuration functions #####
  524. ==============================================================================
  525. [..] This section provides functions allowing to:
  526. (+) HAL_JPEG_ConfigEncoding() : JPEG encoding configuration
  527. (+) HAL_JPEG_GetInfo() : Extract the image configuration from the JPEG header during the decoding
  528. (+) HAL_JPEG_EnableHeaderParsing() : Enable JPEG Header parsing for decoding
  529. (+) HAL_JPEG_DisableHeaderParsing() : Disable JPEG Header parsing for decoding
  530. (+) HAL_JPEG_SetUserQuantTables : Modify the default Quantization tables used for JPEG encoding.
  531. @endverbatim
  532. * @{
  533. */
  534. /**
  535. * @brief Set the JPEG encoding configuration.
  536. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  537. * the configuration information for JPEG module
  538. * @param pConf: pointer to a JPEG_ConfTypeDef structure that contains
  539. * the encoding configuration
  540. * @retval HAL status
  541. */
  542. HAL_StatusTypeDef HAL_JPEG_ConfigEncoding(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pConf)
  543. {
  544. uint32_t error = HAL_OK;
  545. uint32_t numberMCU, hfactor, vfactor,hMCU, vMCU;
  546. /* Check the JPEG handle allocation */
  547. if( (hjpeg == NULL) || (pConf == NULL) )
  548. {
  549. return HAL_ERROR;
  550. }
  551. else
  552. {
  553. /* Check the parameters */
  554. assert_param(IS_JPEG_COLORSPACE(pConf->ColorSpace));
  555. assert_param(IS_JPEG_CHROMASUBSAMPLING(pConf->ChromaSubsampling));
  556. assert_param(IS_JPEG_IMAGE_QUALITY(pConf->ImageQuality));
  557. /* Process Locked */
  558. __HAL_LOCK(hjpeg);
  559. if(hjpeg->State == HAL_JPEG_STATE_READY)
  560. {
  561. hjpeg->State = HAL_JPEG_STATE_BUSY;
  562. hjpeg->Conf.ColorSpace = pConf->ColorSpace;
  563. hjpeg->Conf.ChromaSubsampling = pConf->ChromaSubsampling;
  564. hjpeg->Conf.ImageHeight = pConf->ImageHeight;
  565. hjpeg->Conf.ImageWidth = pConf->ImageWidth;
  566. hjpeg->Conf.ImageQuality = pConf->ImageQuality;
  567. /* Reset the Color Space : by default only one quantization table is used*/
  568. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_COLORSPACE;
  569. /* Set Number of color components*/
  570. if(hjpeg->Conf.ColorSpace == JPEG_GRAYSCALE_COLORSPACE)
  571. {
  572. /*Gray Scale is only one component 8x8 blocks i.e 4:4:4*/
  573. hjpeg->Conf.ChromaSubsampling = JPEG_444_SUBSAMPLING;
  574. JPEG_SetColorGrayScale(hjpeg);
  575. /* Set quantization table 0*/
  576. error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
  577. }
  578. else if(hjpeg->Conf.ColorSpace == JPEG_YCBCR_COLORSPACE)
  579. {
  580. /*
  581. Set the Color Space for YCbCr : 2 quantization tables are used
  582. one for Luminance(Y) and one for both Chrominances (Cb & Cr)
  583. */
  584. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_0;
  585. JPEG_SetColorYCBCR(hjpeg);
  586. /* Set quantization table 0*/
  587. error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
  588. /*By default quantization table 0 for component 0 and quantization table 1 for both components 1 and 2*/
  589. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
  590. if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
  591. {
  592. /* use 3 quantization tables , one for each component*/
  593. hjpeg->Instance->CONFR1 &= (~JPEG_CONFR1_COLORSPACE);
  594. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE_1;
  595. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
  596. /*Use Quantization 1 table for component 1*/
  597. hjpeg->Instance->CONFR5 &= (~JPEG_CONFR5_QT);
  598. hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
  599. /*Use Quantization 2 table for component 2*/
  600. hjpeg->Instance->CONFR6 &= (~JPEG_CONFR6_QT);
  601. hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
  602. }
  603. }
  604. else if(hjpeg->Conf.ColorSpace == JPEG_CMYK_COLORSPACE)
  605. {
  606. JPEG_SetColorCMYK(hjpeg);
  607. /* Set quantization table 0*/
  608. error = JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable0, (uint32_t *)(hjpeg->Instance->QMEM0));
  609. /*By default quantization table 0 for All components*/
  610. if((hjpeg->Context & JPEG_CONTEXT_CUSTOM_TABLES) != 0) /*Use user customized quantization tables , 1 table per component*/
  611. {
  612. /* use 4 quantization tables , one for each component*/
  613. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_COLORSPACE;
  614. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable1, (uint32_t *)(hjpeg->Instance->QMEM1));
  615. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable2, (uint32_t *)(hjpeg->Instance->QMEM2));
  616. error |= JPEG_Set_Quantization_Mem(hjpeg, hjpeg->QuantTable3, (uint32_t *)(hjpeg->Instance->QMEM3));
  617. /*Use Quantization 1 table for component 1*/
  618. hjpeg->Instance->CONFR5 |= JPEG_CONFR5_QT_0;
  619. /*Use Quantization 2 table for component 2*/
  620. hjpeg->Instance->CONFR6 |= JPEG_CONFR6_QT_1;
  621. /*Use Quantization 3 table for component 3*/
  622. hjpeg->Instance->CONFR7 |= JPEG_CONFR7_QT;
  623. }
  624. }
  625. if(error != HAL_OK)
  626. {
  627. hjpeg->ErrorCode = HAL_JPEG_ERROR_QUANT_TABLE;
  628. /* Process Unlocked */
  629. __HAL_UNLOCK(hjpeg);
  630. /* Set the JPEG State to ready */
  631. hjpeg->State = HAL_JPEG_STATE_READY;
  632. return HAL_ERROR;
  633. }
  634. /* Set the image size*/
  635. MODIFY_REG(hjpeg->Instance->CONFR1, JPEG_CONFR1_YSIZE, ((hjpeg->Conf.ImageHeight & 0x0000FFFF) << 16)); /* set the number of lines*/
  636. MODIFY_REG(hjpeg->Instance->CONFR3, JPEG_CONFR3_XSIZE, ((hjpeg->Conf.ImageWidth & 0x0000FFFF) << 16)); /* set the number of pixels per line*/
  637. if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING) /* 4:2:0*/
  638. {
  639. hfactor = 16;
  640. vfactor = 16;
  641. }
  642. else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING) /* 4:2:2*/
  643. {
  644. hfactor = 16;
  645. vfactor = 8;
  646. }
  647. else /* Default is 8x8 MCU, 4:4:4*/
  648. {
  649. hfactor = 8;
  650. vfactor = 8;
  651. }
  652. hMCU = (hjpeg->Conf.ImageWidth / hfactor);
  653. if((hjpeg->Conf.ImageWidth % hfactor) != 0)
  654. {
  655. hMCU++; /*+1 for horizontal incomplete MCU */
  656. }
  657. vMCU = (hjpeg->Conf.ImageHeight / vfactor);
  658. if((hjpeg->Conf.ImageHeight % vfactor) != 0)
  659. {
  660. vMCU++; /*+1 for vertical incomplete MCU */
  661. }
  662. numberMCU = (hMCU * vMCU) - 1; /* Bit Field JPEG_CONFR2_NMCU shall be set to NB_MCU - 1*/
  663. /* Set the number of MCU*/
  664. hjpeg->Instance->CONFR2 = (numberMCU & JPEG_CONFR2_NMCU);
  665. hjpeg->Context |= JPEG_CONTEXT_CONF_ENCODING;
  666. /* Process Unlocked */
  667. __HAL_UNLOCK(hjpeg);
  668. /* Set the JPEG State to ready */
  669. hjpeg->State = HAL_JPEG_STATE_READY;
  670. /* Return function status */
  671. return HAL_OK;
  672. }
  673. else
  674. {
  675. /* Process Unlocked */
  676. __HAL_UNLOCK(hjpeg);
  677. /* Return function status */
  678. return HAL_BUSY;
  679. }
  680. }
  681. }
  682. /**
  683. * @brief Extract the image configuration from the JPEG header during the decoding
  684. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  685. * the configuration information for JPEG module
  686. * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
  687. * The JPEG decoded header informations
  688. * @retval HAL status
  689. */
  690. HAL_StatusTypeDef HAL_JPEG_GetInfo(JPEG_HandleTypeDef *hjpeg, JPEG_ConfTypeDef *pInfo)
  691. {
  692. uint32_t yblockNb, cBblockNb, cRblockNb;
  693. /* Check the JPEG handle allocation */
  694. if((hjpeg == NULL) || (pInfo == NULL))
  695. {
  696. return HAL_ERROR;
  697. }
  698. /*Read the conf parameters */
  699. if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF_1)
  700. {
  701. pInfo->ColorSpace = JPEG_YCBCR_COLORSPACE;
  702. }
  703. else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == 0)
  704. {
  705. pInfo->ColorSpace = JPEG_GRAYSCALE_COLORSPACE;
  706. }
  707. else if((hjpeg->Instance->CONFR1 & JPEG_CONFR1_NF) == JPEG_CONFR1_NF)
  708. {
  709. pInfo->ColorSpace = JPEG_CMYK_COLORSPACE;
  710. }
  711. pInfo->ImageHeight = (hjpeg->Instance->CONFR1 & 0xFFFF0000U) >> 16;
  712. pInfo->ImageWidth = (hjpeg->Instance->CONFR3 & 0xFFFF0000U) >> 16;
  713. if((pInfo->ColorSpace == JPEG_YCBCR_COLORSPACE) || (pInfo->ColorSpace == JPEG_CMYK_COLORSPACE))
  714. {
  715. yblockNb = (hjpeg->Instance->CONFR4 & JPEG_CONFR4_NB) >> 4;
  716. cBblockNb = (hjpeg->Instance->CONFR5 & JPEG_CONFR5_NB) >> 4;
  717. cRblockNb = (hjpeg->Instance->CONFR6 & JPEG_CONFR6_NB) >> 4;
  718. if((yblockNb == 1) && (cBblockNb == 0) && (cRblockNb == 0))
  719. {
  720. pInfo->ChromaSubsampling = JPEG_422_SUBSAMPLING; /*16x8 block*/
  721. }
  722. else if((yblockNb == 0) && (cBblockNb == 0) && (cRblockNb == 0))
  723. {
  724. pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
  725. }
  726. else if((yblockNb == 3) && (cBblockNb == 0) && (cRblockNb == 0))
  727. {
  728. pInfo->ChromaSubsampling = JPEG_420_SUBSAMPLING;
  729. }
  730. else /*Default is 4:4:4*/
  731. {
  732. pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
  733. }
  734. }
  735. else
  736. {
  737. pInfo->ChromaSubsampling = JPEG_444_SUBSAMPLING;
  738. }
  739. pInfo->ImageQuality = JPEG_GetQuality(hjpeg);
  740. /* Return function status */
  741. return HAL_OK;
  742. }
  743. /**
  744. * @brief Enable JPEG Header parsing for decoding
  745. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  746. * the configuration information for the JPEG.
  747. * @retval HAL status
  748. */
  749. HAL_StatusTypeDef HAL_JPEG_EnableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
  750. {
  751. /* Process locked */
  752. __HAL_LOCK(hjpeg);
  753. if(hjpeg->State == HAL_JPEG_STATE_READY)
  754. {
  755. /* Change the JPEG state */
  756. hjpeg->State = HAL_JPEG_STATE_BUSY;
  757. /* Enable header processing*/
  758. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_HDR;
  759. /* Process unlocked */
  760. __HAL_UNLOCK(hjpeg);
  761. /* Change the JPEG state */
  762. hjpeg->State = HAL_JPEG_STATE_READY;
  763. return HAL_OK;
  764. }
  765. else
  766. {
  767. /* Process unlocked */
  768. __HAL_UNLOCK(hjpeg);
  769. return HAL_BUSY;
  770. }
  771. }
  772. /**
  773. * @brief Disable JPEG Header parsing for decoding
  774. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  775. * the configuration information for the JPEG.
  776. * @retval HAL status
  777. */
  778. HAL_StatusTypeDef HAL_JPEG_DisableHeaderParsing(JPEG_HandleTypeDef *hjpeg)
  779. {
  780. /* Process locked */
  781. __HAL_LOCK(hjpeg);
  782. if(hjpeg->State == HAL_JPEG_STATE_READY)
  783. {
  784. /* Change the JPEG state */
  785. hjpeg->State = HAL_JPEG_STATE_BUSY;
  786. /* Disable header processing*/
  787. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_HDR;
  788. /* Process unlocked */
  789. __HAL_UNLOCK(hjpeg);
  790. /* Change the JPEG state */
  791. hjpeg->State = HAL_JPEG_STATE_READY;
  792. return HAL_OK;
  793. }
  794. else
  795. {
  796. /* Process unlocked */
  797. __HAL_UNLOCK(hjpeg);
  798. return HAL_BUSY;
  799. }
  800. }
  801. /**
  802. * @brief Modify the default Quantization tables used for JPEG encoding.
  803. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  804. * the configuration information for JPEG module
  805. * @param QTable0 : pointer to uint8_t , define the user quantification table for color component 1.
  806. * If NULL assume no need to update the table and no error return
  807. * @param QTable1 : pointer to uint8_t , define the user quantification table for color component 2.
  808. * If NULL assume no need to update the table and no error return.
  809. * @param QTable2 : pointer to uint8_t , define the user quantification table for color component 3,
  810. * If NULL assume no need to update the table and no error return.
  811. * @param QTable3 : pointer to uint8_t , define the user quantification table for color component 4.
  812. * If NULL assume no need to update the table and no error return.
  813. *
  814. * @retval HAL status
  815. */
  816. HAL_StatusTypeDef HAL_JPEG_SetUserQuantTables(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable0, uint8_t *QTable1, uint8_t *QTable2, uint8_t *QTable3)
  817. {
  818. /* Process Locked */
  819. __HAL_LOCK(hjpeg);
  820. if(hjpeg->State == HAL_JPEG_STATE_READY)
  821. {
  822. /* Change the DMA state */
  823. hjpeg->State = HAL_JPEG_STATE_BUSY;
  824. hjpeg->Context |= JPEG_CONTEXT_CUSTOM_TABLES;
  825. hjpeg->QuantTable0 = QTable0;
  826. hjpeg->QuantTable1 = QTable1;
  827. hjpeg->QuantTable2 = QTable2;
  828. hjpeg->QuantTable3 = QTable3;
  829. /* Process Unlocked */
  830. __HAL_UNLOCK(hjpeg);
  831. /* Change the DMA state */
  832. hjpeg->State = HAL_JPEG_STATE_READY;
  833. /* Return function status */
  834. return HAL_OK;
  835. }
  836. else
  837. {
  838. /* Process Unlocked */
  839. __HAL_UNLOCK(hjpeg);
  840. return HAL_BUSY;
  841. }
  842. }
  843. /**
  844. * @}
  845. */
  846. /** @defgroup JPEG_Exported_Functions_Group3 encoding/decoding processing functions
  847. * @brief processing functions.
  848. *
  849. @verbatim
  850. ==============================================================================
  851. ##### JPEG processing functions #####
  852. ==============================================================================
  853. [..] This section provides functions allowing to:
  854. (+) HAL_JPEG_Encode() : JPEG encoding with polling process
  855. (+) HAL_JPEG_Decode() : JPEG decoding with polling process
  856. (+) HAL_JPEG_Encode_IT() : JPEG encoding with interrupt process
  857. (+) HAL_JPEG_Decode_IT() : JPEG decoding with interrupt process
  858. (+) HAL_JPEG_Encode_DMA() : JPEG encoding with DMA process
  859. (+) HAL_JPEG_Decode_DMA() : JPEG decoding with DMA process
  860. (+) HAL_JPEG_Pause() : Pause the Input/Output processing
  861. (+) HAL_JPEG_Resume() : Resume the JPEG Input/Output processing
  862. (+) HAL_JPEG_ConfigInputBuffer() : Config Encoding/Decoding Input Buffer
  863. (+) HAL_JPEG_ConfigOutputBuffer() : Config Encoding/Decoding Output Buffer
  864. (+) HAL_JPEG_Abort() : Aborts the JPEG Encoding/Decoding
  865. @endverbatim
  866. * @{
  867. */
  868. /**
  869. * @brief Starts JPEG encoding with polling processing
  870. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  871. * the configuration information for JPEG module
  872. * @param pDataInMCU: Pointer to the Input buffer
  873. * @param InDataLength: size in bytes Input buffer
  874. * @param pDataOut: Pointer to the jpeg output data buffer
  875. * @param OutDataLength: size in bytes of the Output buffer
  876. * @param Timeout: Specify Timeout value
  877. * @retval HAL status
  878. */
  879. HAL_StatusTypeDef HAL_JPEG_Encode(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength, uint32_t Timeout)
  880. {
  881. uint32_t tickstart = 0;
  882. /* Check the parameters */
  883. assert_param((InDataLength >= 4));
  884. assert_param((OutDataLength >= 4));
  885. /* Check In/out buffer allocation and size */
  886. if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
  887. (InDataLength == 0) || (OutDataLength == 0))
  888. {
  889. return HAL_ERROR;
  890. }
  891. /* Process locked */
  892. __HAL_LOCK(hjpeg);
  893. if(hjpeg->State != HAL_JPEG_STATE_READY)
  894. {
  895. /* Process Unlocked */
  896. __HAL_UNLOCK(hjpeg);
  897. return HAL_BUSY;
  898. }
  899. if(hjpeg->State == HAL_JPEG_STATE_READY)
  900. {
  901. if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
  902. {
  903. /*Change JPEG state*/
  904. hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
  905. /*Set the Context to Encode with Polling*/
  906. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  907. hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_POLLING);
  908. /* Get tick */
  909. tickstart = HAL_GetTick();
  910. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  911. InDataLength = InDataLength - (InDataLength % 4);
  912. OutDataLength = OutDataLength - (OutDataLength % 4);
  913. /*Store In/out buffers pointers and size*/
  914. hjpeg->pJpegInBuffPtr = pDataInMCU;
  915. hjpeg->pJpegOutBuffPtr = pDataOut;
  916. hjpeg->InDataLength = InDataLength;
  917. hjpeg->OutDataLength = OutDataLength;
  918. /*Reset In/out data counter */
  919. hjpeg->JpegInCount = 0;
  920. hjpeg->JpegOutCount = 0;
  921. /*Init decoding process*/
  922. JPEG_Init_Process(hjpeg);
  923. /*JPEG data processing : In/Out FIFO transfer*/
  924. while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
  925. {
  926. if(Timeout != HAL_MAX_DELAY)
  927. {
  928. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  929. {
  930. /* Update error code */
  931. hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
  932. /* Process Unlocked */
  933. __HAL_UNLOCK(hjpeg);
  934. /*Change JPEG state*/
  935. hjpeg->State= HAL_JPEG_STATE_READY;
  936. return HAL_TIMEOUT;
  937. }
  938. }
  939. }
  940. /* Process Unlocked */
  941. __HAL_UNLOCK(hjpeg);
  942. /*Change JPEG state*/
  943. hjpeg->State= HAL_JPEG_STATE_READY;
  944. }else
  945. {
  946. /* Process Unlocked */
  947. __HAL_UNLOCK(hjpeg);
  948. return HAL_ERROR;
  949. }
  950. }
  951. /* Return function status */
  952. return HAL_OK;
  953. }
  954. /**
  955. * @brief Starts JPEG decoding with polling processing
  956. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  957. * the configuration information for JPEG module
  958. * @param pDataIn: Pointer to the input data buffer
  959. * @param InDataLength: size in bytes Input buffer
  960. * @param pDataOutMCU: Pointer to the Output data buffer
  961. * @param OutDataLength: size in bytes of the Output buffer
  962. * @param Timeout: Specify Timeout value
  963. * @retval HAL status
  964. */
  965. HAL_StatusTypeDef HAL_JPEG_Decode(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength, uint32_t Timeout)
  966. {
  967. uint32_t tickstart = 0;
  968. /* Check the parameters */
  969. assert_param((InDataLength >= 4));
  970. assert_param((OutDataLength >= 4));
  971. /* Check In/out buffer allocation and size */
  972. if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
  973. (InDataLength == 0) || (OutDataLength == 0))
  974. {
  975. return HAL_ERROR;
  976. }
  977. /* Process Locked */
  978. __HAL_LOCK(hjpeg);
  979. /* Get tick */
  980. tickstart = HAL_GetTick();
  981. if(hjpeg->State == HAL_JPEG_STATE_READY)
  982. {
  983. /*Change JPEG state*/
  984. hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
  985. /*Set the Context to Decode with Polling*/
  986. /*Set the Context to Encode with Polling*/
  987. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  988. hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_POLLING);
  989. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  990. InDataLength = InDataLength - (InDataLength % 4);
  991. OutDataLength = OutDataLength - (OutDataLength % 4);
  992. /*Store In/out buffers pointers and size*/
  993. hjpeg->pJpegInBuffPtr = pDataIn;
  994. hjpeg->pJpegOutBuffPtr = pDataOutMCU;
  995. hjpeg->InDataLength = InDataLength;
  996. hjpeg->OutDataLength = OutDataLength;
  997. /*Reset In/out data counter */
  998. hjpeg->JpegInCount = 0;
  999. hjpeg->JpegOutCount = 0;
  1000. /*Init decoding process*/
  1001. JPEG_Init_Process(hjpeg);
  1002. /*JPEG data processing : In/Out FIFO transfer*/
  1003. while((JPEG_Process(hjpeg) == JPEG_PROCESS_ONGOING))
  1004. {
  1005. if(Timeout != HAL_MAX_DELAY)
  1006. {
  1007. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1008. {
  1009. /* Update error code */
  1010. hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
  1011. /* Process Unlocked */
  1012. __HAL_UNLOCK(hjpeg);
  1013. /*Change JPEG state*/
  1014. hjpeg->State= HAL_JPEG_STATE_READY;
  1015. return HAL_TIMEOUT;
  1016. }
  1017. }
  1018. }
  1019. /* Process Unlocked */
  1020. __HAL_UNLOCK(hjpeg);
  1021. /*Change JPEG state*/
  1022. hjpeg->State= HAL_JPEG_STATE_READY;
  1023. }else
  1024. {
  1025. /* Process Unlocked */
  1026. __HAL_UNLOCK(hjpeg);
  1027. return HAL_BUSY;
  1028. }
  1029. /* Return function status */
  1030. return HAL_OK;
  1031. }
  1032. /**
  1033. * @brief Starts JPEG encoding with interrupt processing
  1034. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1035. * the configuration information for JPEG module
  1036. * @param pDataInMCU: Pointer to the Input buffer
  1037. * @param InDataLength: size in bytes Input buffer
  1038. * @param pDataOut: Pointer to the jpeg output data buffer
  1039. * @param OutDataLength: size in bytes of the Output buffer
  1040. * @retval HAL status
  1041. */
  1042. HAL_StatusTypeDef HAL_JPEG_Encode_IT(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
  1043. {
  1044. /* Check the parameters */
  1045. assert_param((InDataLength >= 4));
  1046. assert_param((OutDataLength >= 4));
  1047. /* Check In/out buffer allocation and size */
  1048. if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
  1049. (InDataLength == 0) || (OutDataLength == 0))
  1050. {
  1051. return HAL_ERROR;
  1052. }
  1053. /* Process Locked */
  1054. __HAL_LOCK(hjpeg);
  1055. if(hjpeg->State != HAL_JPEG_STATE_READY)
  1056. {
  1057. /* Process Unlocked */
  1058. __HAL_UNLOCK(hjpeg);
  1059. return HAL_BUSY;
  1060. }
  1061. else
  1062. {
  1063. if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
  1064. {
  1065. /*Change JPEG state*/
  1066. hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
  1067. /*Set the Context to Encode with IT*/
  1068. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1069. hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_IT);
  1070. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  1071. InDataLength = InDataLength - (InDataLength % 4);
  1072. OutDataLength = OutDataLength - (OutDataLength % 4);
  1073. /*Store In/out buffers pointers and size*/
  1074. hjpeg->pJpegInBuffPtr = pDataInMCU;
  1075. hjpeg->pJpegOutBuffPtr = pDataOut;
  1076. hjpeg->InDataLength = InDataLength;
  1077. hjpeg->OutDataLength = OutDataLength;
  1078. /*Reset In/out data counter */
  1079. hjpeg->JpegInCount = 0;
  1080. hjpeg->JpegOutCount = 0;
  1081. /*Init decoding process*/
  1082. JPEG_Init_Process(hjpeg);
  1083. }
  1084. else
  1085. {
  1086. /* Process Unlocked */
  1087. __HAL_UNLOCK(hjpeg);
  1088. return HAL_ERROR;
  1089. }
  1090. }
  1091. /* Return function status */
  1092. return HAL_OK;
  1093. }
  1094. /**
  1095. * @brief Starts JPEG decoding with interrupt processing
  1096. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1097. * the configuration information for JPEG module
  1098. * @param pDataIn: Pointer to the input data buffer
  1099. * @param InDataLength: size in bytes Input buffer
  1100. * @param pDataOutMCU: Pointer to the Output data buffer
  1101. * @param OutDataLength: size in bytes of the Output buffer
  1102. * @retval HAL status
  1103. */
  1104. HAL_StatusTypeDef HAL_JPEG_Decode_IT(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
  1105. {
  1106. /* Check the parameters */
  1107. assert_param((InDataLength >= 4));
  1108. assert_param((OutDataLength >= 4));
  1109. /* Check In/out buffer allocation and size */
  1110. if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
  1111. (InDataLength == 0) || (OutDataLength == 0))
  1112. {
  1113. return HAL_ERROR;
  1114. }
  1115. /* Process Locked */
  1116. __HAL_LOCK(hjpeg);
  1117. if(hjpeg->State == HAL_JPEG_STATE_READY)
  1118. {
  1119. /*Change JPEG state*/
  1120. hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
  1121. /*Set the Context to Decode with IT*/
  1122. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1123. hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_IT);
  1124. /*In/Out Data length must be multiple of 4 Bytes (1 word)*/
  1125. InDataLength = InDataLength - (InDataLength % 4);
  1126. OutDataLength = OutDataLength - (OutDataLength % 4);
  1127. /*Store In/out buffers pointers and size*/
  1128. hjpeg->pJpegInBuffPtr = pDataIn;
  1129. hjpeg->pJpegOutBuffPtr = pDataOutMCU;
  1130. hjpeg->InDataLength = InDataLength;
  1131. hjpeg->OutDataLength = OutDataLength;
  1132. /*Reset In/out data counter */
  1133. hjpeg->JpegInCount = 0;
  1134. hjpeg->JpegOutCount = 0;
  1135. /*Init decoding process*/
  1136. JPEG_Init_Process(hjpeg);
  1137. }
  1138. else
  1139. {
  1140. /* Process Unlocked */
  1141. __HAL_UNLOCK(hjpeg);
  1142. return HAL_BUSY;
  1143. }
  1144. /* Return function status */
  1145. return HAL_OK;
  1146. }
  1147. /**
  1148. * @brief Starts JPEG encoding with DMA processing
  1149. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1150. * the configuration information for JPEG module
  1151. * @param pDataInMCU: Pointer to the Input buffer
  1152. * @param InDataLength: size in bytes Input buffer
  1153. * @param pDataOut: Pointer to the jpeg output data buffer
  1154. * @param OutDataLength: size in bytes of the Output buffer
  1155. * @retval HAL status
  1156. */
  1157. HAL_StatusTypeDef HAL_JPEG_Encode_DMA(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataInMCU, uint32_t InDataLength, uint8_t *pDataOut, uint32_t OutDataLength)
  1158. {
  1159. /* Check the parameters */
  1160. assert_param((InDataLength >= 4));
  1161. assert_param((OutDataLength >= 4));
  1162. /* Check In/out buffer allocation and size */
  1163. if((hjpeg == NULL) || (pDataInMCU == NULL) || (pDataOut == NULL) || \
  1164. (InDataLength == 0) || (OutDataLength == 0))
  1165. {
  1166. return HAL_ERROR;
  1167. }
  1168. /* Process Locked */
  1169. __HAL_LOCK(hjpeg);
  1170. if(hjpeg->State != HAL_JPEG_STATE_READY)
  1171. {
  1172. /* Process Unlocked */
  1173. __HAL_UNLOCK(hjpeg);
  1174. return HAL_BUSY;
  1175. }
  1176. else
  1177. {
  1178. if((hjpeg->Context & JPEG_CONTEXT_CONF_ENCODING) == JPEG_CONTEXT_CONF_ENCODING )
  1179. {
  1180. /*Change JPEG state*/
  1181. hjpeg->State = HAL_JPEG_STATE_BUSY_ENCODING;
  1182. /*Set the Context to Encode with DMA*/
  1183. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1184. hjpeg->Context |= (JPEG_CONTEXT_ENCODE | JPEG_CONTEXT_DMA);
  1185. /*Store In/out buffers pointers and size*/
  1186. hjpeg->pJpegInBuffPtr = pDataInMCU;
  1187. hjpeg->pJpegOutBuffPtr = pDataOut;
  1188. hjpeg->InDataLength = InDataLength;
  1189. hjpeg->OutDataLength = OutDataLength;
  1190. /*Reset In/out data counter */
  1191. hjpeg->JpegInCount = 0;
  1192. hjpeg->JpegOutCount = 0;
  1193. /*Init decoding process*/
  1194. JPEG_Init_Process(hjpeg);
  1195. /* JPEG encoding process using DMA */
  1196. JPEG_DMA_StartProcess(hjpeg);
  1197. }
  1198. else
  1199. {
  1200. /* Process Unlocked */
  1201. __HAL_UNLOCK(hjpeg);
  1202. return HAL_ERROR;
  1203. }
  1204. }
  1205. /* Return function status */
  1206. return HAL_OK;
  1207. }
  1208. /**
  1209. * @brief Starts JPEG decoding with DMA processing
  1210. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1211. * the configuration information for JPEG module
  1212. * @param pDataIn: Pointer to the input data buffer
  1213. * @param InDataLength: size in bytes Input buffer
  1214. * @param pDataOutMCU: Pointer to the Output data buffer
  1215. * @param OutDataLength: size in bytes of the Output buffer
  1216. * @retval HAL status
  1217. */
  1218. HAL_StatusTypeDef HAL_JPEG_Decode_DMA(JPEG_HandleTypeDef *hjpeg ,uint8_t *pDataIn ,uint32_t InDataLength ,uint8_t *pDataOutMCU ,uint32_t OutDataLength)
  1219. {
  1220. /* Check the parameters */
  1221. assert_param((InDataLength >= 4));
  1222. assert_param((OutDataLength >= 4));
  1223. /* Check In/out buffer allocation and size */
  1224. if((hjpeg == NULL) || (pDataIn == NULL) || (pDataOutMCU == NULL) || \
  1225. (InDataLength == 0) || (OutDataLength == 0))
  1226. {
  1227. return HAL_ERROR;
  1228. }
  1229. /* Process Locked */
  1230. __HAL_LOCK(hjpeg);
  1231. if(hjpeg->State == HAL_JPEG_STATE_READY)
  1232. {
  1233. /*Change JPEG state*/
  1234. hjpeg->State = HAL_JPEG_STATE_BUSY_DECODING;
  1235. /*Set the Context to Decode with DMA*/
  1236. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK);
  1237. hjpeg->Context |= (JPEG_CONTEXT_DECODE | JPEG_CONTEXT_DMA);
  1238. /*Store In/out buffers pointers and size*/
  1239. hjpeg->pJpegInBuffPtr = pDataIn;
  1240. hjpeg->pJpegOutBuffPtr = pDataOutMCU;
  1241. hjpeg->InDataLength = InDataLength;
  1242. hjpeg->OutDataLength = OutDataLength;
  1243. /*Reset In/out data counter */
  1244. hjpeg->JpegInCount = 0;
  1245. hjpeg->JpegOutCount = 0;
  1246. /*Init decoding process*/
  1247. JPEG_Init_Process(hjpeg);
  1248. /* JPEG decoding process using DMA */
  1249. JPEG_DMA_StartProcess(hjpeg);
  1250. }
  1251. else
  1252. {
  1253. /* Process Unlocked */
  1254. __HAL_UNLOCK(hjpeg);
  1255. return HAL_BUSY;
  1256. }
  1257. /* Return function status */
  1258. return HAL_OK;
  1259. }
  1260. /**
  1261. * @brief Pause the JPEG Input/Output processing
  1262. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1263. * the configuration information for JPEG module
  1264. * @param XferSelection: This parameter can be one of the following values :
  1265. * JPEG_PAUSE_RESUME_INPUT : Pause Input processing
  1266. * JPEG_PAUSE_RESUME_OUTPUT: Pause Output processing
  1267. * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Pause Input and Output processing
  1268. * @retval HAL status
  1269. */
  1270. HAL_StatusTypeDef HAL_JPEG_Pause(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
  1271. {
  1272. uint32_t mask = 0;
  1273. assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
  1274. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1275. {
  1276. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1277. {
  1278. hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
  1279. mask |= JPEG_DMA_IDMA;
  1280. }
  1281. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1282. {
  1283. hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
  1284. mask |= JPEG_DMA_ODMA;
  1285. }
  1286. JPEG_DISABLE_DMA(hjpeg,mask);
  1287. }
  1288. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  1289. {
  1290. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1291. {
  1292. hjpeg->Context |= JPEG_CONTEXT_PAUSE_INPUT;
  1293. mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
  1294. }
  1295. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1296. {
  1297. hjpeg->Context |= JPEG_CONTEXT_PAUSE_OUTPUT;
  1298. mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
  1299. }
  1300. __HAL_JPEG_DISABLE_IT(hjpeg,mask);
  1301. }
  1302. /* Return function status */
  1303. return HAL_OK;
  1304. }
  1305. /**
  1306. * @brief Resume the JPEG Input/Output processing
  1307. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1308. * the configuration information for JPEG module
  1309. * @param XferSelection: This parameter can be one of the following values :
  1310. * JPEG_PAUSE_RESUME_INPUT : Resume Input processing
  1311. * JPEG_PAUSE_RESUME_OUTPUT: Resume Output processing
  1312. * JPEG_PAUSE_RESUME_INPUT_OUTPUT: Resume Input and Output processing
  1313. * @retval HAL status
  1314. */
  1315. HAL_StatusTypeDef HAL_JPEG_Resume(JPEG_HandleTypeDef *hjpeg, uint32_t XferSelection)
  1316. {
  1317. uint32_t mask = 0;
  1318. assert_param(IS_JPEG_PAUSE_RESUME_STATE(XferSelection));
  1319. if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0))
  1320. {
  1321. /* if nothing paused to resume return error*/
  1322. return HAL_ERROR;
  1323. }
  1324. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1325. {
  1326. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1327. {
  1328. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
  1329. mask |= JPEG_DMA_IDMA;
  1330. /*JPEG Input DMA transfer data number must be multiple of DMA buffer size
  1331. as the destination is a 32 bits register */
  1332. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  1333. if(hjpeg->InDataLength > 0)
  1334. {
  1335. /* Start DMA FIFO In transfer */
  1336. HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
  1337. }
  1338. }
  1339. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1340. {
  1341. if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
  1342. {
  1343. JPEG_DMA_PollResidualData(hjpeg);
  1344. }
  1345. else
  1346. {
  1347. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
  1348. mask |= JPEG_DMA_ODMA;
  1349. /* Start DMA FIFO Out transfer */
  1350. HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
  1351. }
  1352. }
  1353. JPEG_ENABLE_DMA(hjpeg,mask);
  1354. }
  1355. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  1356. {
  1357. if((XferSelection & JPEG_PAUSE_RESUME_INPUT) == JPEG_PAUSE_RESUME_INPUT)
  1358. {
  1359. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_INPUT);
  1360. mask |= (JPEG_IT_IFT | JPEG_IT_IFNF);
  1361. }
  1362. if((XferSelection & JPEG_PAUSE_RESUME_OUTPUT) == JPEG_PAUSE_RESUME_OUTPUT)
  1363. {
  1364. hjpeg->Context &= (~JPEG_CONTEXT_PAUSE_OUTPUT);
  1365. mask |= (JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC);
  1366. }
  1367. __HAL_JPEG_ENABLE_IT(hjpeg,mask);
  1368. }
  1369. /* Return function status */
  1370. return HAL_OK;
  1371. }
  1372. /**
  1373. * @brief Config Encoding/Decoding Input Buffer.
  1374. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1375. * the configuration information for JPEG module.
  1376. * @param pNewInputBuffer: Pointer to the new input data buffer
  1377. * @param InDataLength: Size in bytes of the new Input data buffer
  1378. * @retval HAL status
  1379. */
  1380. void HAL_JPEG_ConfigInputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewInputBuffer, uint32_t InDataLength)
  1381. {
  1382. hjpeg->pJpegInBuffPtr = pNewInputBuffer;
  1383. hjpeg->InDataLength = InDataLength;
  1384. }
  1385. /**
  1386. * @brief Config Encoding/Decoding Output Buffer.
  1387. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1388. * the configuration information for JPEG module.
  1389. * @param pNewOutputBuffer: Pointer to the new output data buffer
  1390. * @param OutDataLength: Size in bytes of the new Output data buffer
  1391. * @retval HAL status
  1392. */
  1393. void HAL_JPEG_ConfigOutputBuffer(JPEG_HandleTypeDef *hjpeg, uint8_t *pNewOutputBuffer, uint32_t OutDataLength)
  1394. {
  1395. hjpeg->pJpegOutBuffPtr = pNewOutputBuffer;
  1396. hjpeg->OutDataLength = OutDataLength;
  1397. }
  1398. /**
  1399. * @brief Aborts the JPEG Encoding/Decoding.
  1400. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1401. * the configuration information for JPEG module
  1402. * @retval HAL status
  1403. */
  1404. HAL_StatusTypeDef HAL_JPEG_Abort(JPEG_HandleTypeDef *hjpeg)
  1405. {
  1406. uint32_t tickstart, tmpContext;
  1407. tmpContext = hjpeg->Context;
  1408. /*Reset the Context operation and method*/
  1409. hjpeg->Context &= ~(JPEG_CONTEXT_OPERATION_MASK | JPEG_CONTEXT_METHOD_MASK | JPEG_CONTEXT_ENDING_DMA);
  1410. if((tmpContext & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1411. {
  1412. /* Stop the DMA In/out Xfer*/
  1413. HAL_DMA_Abort(hjpeg->hdmaout);
  1414. HAL_DMA_Abort(hjpeg->hdmain);
  1415. }
  1416. /* Stop the JPEG encoding/decoding process*/
  1417. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  1418. /* Get tick */
  1419. tickstart = HAL_GetTick();
  1420. /* Check if the JPEG Codec is effectively disabled */
  1421. while(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_COF) != RESET)
  1422. {
  1423. /* Check for the Timeout */
  1424. if((HAL_GetTick() - tickstart ) > JPEG_TIMEOUT_VALUE)
  1425. {
  1426. /* Update error code */
  1427. hjpeg->ErrorCode |= HAL_JPEG_ERROR_TIMEOUT;
  1428. /* Change the DMA state */
  1429. hjpeg->State = HAL_JPEG_STATE_TIMEOUT;
  1430. /* Process Unlocked */
  1431. __HAL_UNLOCK(hjpeg);
  1432. return HAL_TIMEOUT;
  1433. }
  1434. }
  1435. /* Disable All Interrupts */
  1436. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  1437. /* Disable All DMA requests */
  1438. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  1439. /* Flush input and output FIFOs*/
  1440. hjpeg->Instance->CR |= JPEG_CR_IFF;
  1441. hjpeg->Instance->CR |= JPEG_CR_OFF;
  1442. /* Clear all flags */
  1443. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  1444. /* Reset JpegInCount and JpegOutCount */
  1445. hjpeg->JpegInCount = 0;
  1446. hjpeg->JpegOutCount = 0;
  1447. /*Reset the Context Pause*/
  1448. hjpeg->Context &= ~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT);
  1449. /* Change the DMA state*/
  1450. hjpeg->State = HAL_JPEG_STATE_READY;
  1451. /* Process Unlocked */
  1452. __HAL_UNLOCK(hjpeg);
  1453. /* Return function status */
  1454. return HAL_OK;
  1455. }
  1456. /**
  1457. * @}
  1458. */
  1459. /** @defgroup JPEG_Exported_Functions_Group4 JPEG Decode/Encode callback functions
  1460. * @brief JPEG process callback functions.
  1461. *
  1462. @verbatim
  1463. ==============================================================================
  1464. ##### JPEG Decode and Encode callback functions #####
  1465. ==============================================================================
  1466. [..] This section provides callback functions:
  1467. (+) HAL_JPEG_InfoReadyCallback() : Decoding JPEG Info ready callback
  1468. (+) HAL_JPEG_EncodeCpltCallback() : Encoding complete callback.
  1469. (+) HAL_JPEG_DecodeCpltCallback() : Decoding complete callback.
  1470. (+) HAL_JPEG_ErrorCallback() : JPEG error callback.
  1471. (+) HAL_JPEG_GetDataCallback() : Get New Data chunk callback.
  1472. (+) HAL_JPEG_DataReadyCallback() : Decoded/Encoded Data ready callback.
  1473. @endverbatim
  1474. * @{
  1475. */
  1476. /**
  1477. * @brief Decoding JPEG Info ready callback.
  1478. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1479. * the configuration information for JPEG module
  1480. * @param pInfo: pointer to a JPEG_ConfTypeDef structure that contains
  1481. * The JPEG decoded header informations
  1482. * @retval None
  1483. */
  1484. __weak void HAL_JPEG_InfoReadyCallback(JPEG_HandleTypeDef *hjpeg,JPEG_ConfTypeDef *pInfo)
  1485. {
  1486. /* Prevent unused argument(s) compilation warning */
  1487. UNUSED(hjpeg);
  1488. UNUSED(pInfo);
  1489. /* NOTE : This function Should not be modified, when the callback is needed,
  1490. the HAL_JPEG_HeaderParsingCpltCallback could be implemented in the user file
  1491. */
  1492. }
  1493. /**
  1494. * @brief Encoding complete callback.
  1495. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1496. * the configuration information for JPEG module
  1497. * @retval None
  1498. */
  1499. __weak void HAL_JPEG_EncodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
  1500. {
  1501. /* Prevent unused argument(s) compilation warning */
  1502. UNUSED(hjpeg);
  1503. /* NOTE : This function Should not be modified, when the callback is needed,
  1504. the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
  1505. */
  1506. }
  1507. /**
  1508. * @brief Decoding complete callback.
  1509. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1510. * the configuration information for JPEG module
  1511. * @retval None
  1512. */
  1513. __weak void HAL_JPEG_DecodeCpltCallback(JPEG_HandleTypeDef *hjpeg)
  1514. {
  1515. /* Prevent unused argument(s) compilation warning */
  1516. UNUSED(hjpeg);
  1517. /* NOTE : This function Should not be modified, when the callback is needed,
  1518. the HAL_JPEG_EncodeCpltCallback could be implemented in the user file
  1519. */
  1520. }
  1521. /**
  1522. * @brief JPEG error callback.
  1523. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1524. * the configuration information for JPEG module
  1525. * @retval None
  1526. */
  1527. __weak void HAL_JPEG_ErrorCallback(JPEG_HandleTypeDef *hjpeg)
  1528. {
  1529. /* Prevent unused argument(s) compilation warning */
  1530. UNUSED(hjpeg);
  1531. /* NOTE : This function Should not be modified, when the callback is needed,
  1532. the HAL_JPEG_ErrorCallback could be implemented in the user file
  1533. */
  1534. }
  1535. /**
  1536. * @brief Get New Data chunk callback.
  1537. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1538. * the configuration information for JPEG module
  1539. * @param NbDecodedData: Number of consummed data in the previous chunk in bytes
  1540. * @retval None
  1541. */
  1542. __weak void HAL_JPEG_GetDataCallback(JPEG_HandleTypeDef *hjpeg, uint32_t NbDecodedData)
  1543. {
  1544. /* Prevent unused argument(s) compilation warning */
  1545. UNUSED(hjpeg);
  1546. UNUSED(NbDecodedData);
  1547. /* NOTE : This function Should not be modified, when the callback is needed,
  1548. the HAL_JPEG_GetDataCallback could be implemented in the user file
  1549. */
  1550. }
  1551. /**
  1552. * @brief Decoded/Encoded Data ready callback.
  1553. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1554. * the configuration information for JPEG module
  1555. * @param pDataOut: pointer to the output data buffer
  1556. * @param OutDataLength: number in bytes of data available in the specified output buffer
  1557. * @retval None
  1558. */
  1559. __weak void HAL_JPEG_DataReadyCallback (JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOut, uint32_t OutDataLength)
  1560. {
  1561. /* Prevent unused argument(s) compilation warning */
  1562. UNUSED(hjpeg);
  1563. UNUSED(pDataOut);
  1564. UNUSED(OutDataLength);
  1565. /* NOTE : This function Should not be modified, when the callback is needed,
  1566. the HAL_JPEG_DataReadyCallback could be implemented in the user file
  1567. */
  1568. }
  1569. /**
  1570. * @}
  1571. */
  1572. /** @defgroup JPEG_Exported_Functions_Group5 JPEG IRQ handler management
  1573. * @brief JPEG IRQ handler.
  1574. *
  1575. @verbatim
  1576. ==============================================================================
  1577. ##### JPEG IRQ handler management #####
  1578. ==============================================================================
  1579. [..] This section provides JPEG IRQ handler function.
  1580. (+) HAL_JPEG_IRQHandler() : handles JPEG interrupt request
  1581. @endverbatim
  1582. * @{
  1583. */
  1584. /**
  1585. * @brief This function handles JPEG interrupt request.
  1586. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1587. * the configuration information for JPEG module
  1588. * @retval None
  1589. */
  1590. void HAL_JPEG_IRQHandler(JPEG_HandleTypeDef *hjpeg)
  1591. {
  1592. switch(hjpeg->State)
  1593. {
  1594. case HAL_JPEG_STATE_BUSY_ENCODING:
  1595. case HAL_JPEG_STATE_BUSY_DECODING:
  1596. /* continue JPEG data encoding/Decoding*/
  1597. /* JPEG data processing : In/Out FIFO transfer*/
  1598. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  1599. {
  1600. JPEG_Process(hjpeg);
  1601. }
  1602. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  1603. {
  1604. JPEG_DMA_ContinueProcess(hjpeg);
  1605. }
  1606. break;
  1607. default:
  1608. break;
  1609. }
  1610. }
  1611. /**
  1612. * @}
  1613. */
  1614. /** @defgroup JPEG_Exported_Functions_Group6 Peripheral State functions
  1615. * @brief Peripheral State functions.
  1616. *
  1617. @verbatim
  1618. ==============================================================================
  1619. ##### Peripheral State and Error functions #####
  1620. ==============================================================================
  1621. [..] This section provides JPEG State and Errors function.
  1622. (+) HAL_JPEG_GetState() : permits to get in run-time the JPEG state.
  1623. (+) HAL_JPEG_GetError() : Returns the JPEG error code if any.
  1624. @endverbatim
  1625. * @{
  1626. */
  1627. /**
  1628. * @brief Returns the JPEG state.
  1629. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1630. * the configuration information for JPEG module
  1631. * @retval JPEG state
  1632. */
  1633. HAL_JPEG_STATETypeDef HAL_JPEG_GetState(JPEG_HandleTypeDef *hjpeg)
  1634. {
  1635. return hjpeg->State;
  1636. }
  1637. /**
  1638. * @brief Return the JPEG error code
  1639. * @param hjpeg : pointer to a JPEG_HandleTypeDef structure that contains
  1640. * the configuration information for the specified JPEG.
  1641. * @retval JPEG Error Code
  1642. */
  1643. uint32_t HAL_JPEG_GetError(JPEG_HandleTypeDef *hjpeg)
  1644. {
  1645. return hjpeg->ErrorCode;
  1646. }
  1647. /**
  1648. * @}
  1649. */
  1650. /**
  1651. * @}
  1652. */
  1653. /** @addtogroup JPEG_Private_Functions
  1654. * @{
  1655. */
  1656. /**
  1657. * @brief Generates Huffman sizes/Codes Table from Bits/vals Table
  1658. * @param Bits: pointer to bits table
  1659. * @param Huffsize: pointer to sizes table
  1660. * @param Huffcode: pointer to codes table
  1661. * @param LastK: pointer to last Coeff (table dimmension)
  1662. * @retval HAL status
  1663. */
  1664. static HAL_StatusTypeDef JPEG_Bits_To_SizeCodes(uint8_t *Bits, uint8_t *Huffsize, uint32_t *Huffcode, uint32_t *LastK)
  1665. {
  1666. uint32_t i, p, l, code, si;
  1667. /* Figure C.1: Generation of table of Huffman code sizes */
  1668. p = 0;
  1669. for (l = 0; l < 16; l++)
  1670. {
  1671. i = (uint32_t)Bits[l];
  1672. if ( (p + i) > 256)
  1673. { /* check for table overflow */
  1674. return HAL_ERROR;
  1675. }
  1676. while (i != 0)
  1677. {
  1678. Huffsize[p++] = (uint8_t) l+1;
  1679. i--;
  1680. }
  1681. }
  1682. Huffsize[p] = 0;
  1683. *LastK = p;
  1684. /* Figure C.2: Generation of table of Huffman codes */
  1685. code = 0;
  1686. si = Huffsize[0];
  1687. p = 0;
  1688. while (Huffsize[p] != 0)
  1689. {
  1690. while (((uint32_t) Huffsize[p]) == si)
  1691. {
  1692. Huffcode[p++] = code;
  1693. code++;
  1694. }
  1695. /* code must fit in "size" bits (si), no code is allowed to be all ones*/
  1696. if (((uint32_t) code) >= (((uint32_t) 1) << si))
  1697. {
  1698. return HAL_ERROR;
  1699. }
  1700. code <<= 1;
  1701. si++;
  1702. }
  1703. /* Return function status */
  1704. return HAL_OK;
  1705. }
  1706. /**
  1707. * @brief Transform a Bits/Vals AC Huffman table to sizes/Codes huffman Table
  1708. * that can programmed to the JPEG encoder registers
  1709. * @param AC_BitsValsTable: pointer to AC huffman bits/vals table
  1710. * @param AC_SizeCodesTable: pointer to AC huffman Sizes/Codes table
  1711. * @retval HAL status
  1712. */
  1713. static HAL_StatusTypeDef JPEG_ACHuff_BitsVals_To_SizeCodes(JPEG_ACHuffTableTypeDef *AC_BitsValsTable, JPEG_AC_HuffCodeTableTypeDef *AC_SizeCodesTable)
  1714. {
  1715. HAL_StatusTypeDef error;
  1716. uint8_t huffsize[257];
  1717. uint32_t huffcode[257];
  1718. uint32_t k;
  1719. uint32_t l,lsb, msb;
  1720. uint32_t lastK;
  1721. error = JPEG_Bits_To_SizeCodes(AC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
  1722. if(error != HAL_OK)
  1723. {
  1724. return error;
  1725. }
  1726. /* Figure C.3: Ordering procedure for encoding procedure code tables */
  1727. k=0;
  1728. while(k < lastK)
  1729. {
  1730. l = AC_BitsValsTable->HuffVal[k];
  1731. if(l == 0)
  1732. {
  1733. l = 160; /*l = 0x00 EOB code*/
  1734. }
  1735. else if(l == 0xF0)/* l = 0xF0 ZRL code*/
  1736. {
  1737. l = 161;
  1738. }
  1739. else
  1740. {
  1741. msb = (l & 0xF0) >> 4;
  1742. lsb = (l & 0x0F);
  1743. l = (msb * 10) + lsb - 1;
  1744. }
  1745. if(l >= JPEG_AC_HUFF_TABLE_SIZE)
  1746. {
  1747. return HAL_ERROR; /* Huffman Table overflow error*/
  1748. }
  1749. else
  1750. {
  1751. AC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
  1752. AC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
  1753. k++;
  1754. }
  1755. }
  1756. /* Return function status */
  1757. return HAL_OK;
  1758. }
  1759. /**
  1760. * @brief Transform a Bits/Vals DC Huffman table to sizes/Codes huffman Table
  1761. * that can programmed to the JPEG encoder registers
  1762. * @param DC_BitsValsTable: pointer to DC huffman bits/vals table
  1763. * @param DC_SizeCodesTable: pointer to DC huffman Sizes/Codes table
  1764. * @retval HAL status
  1765. */
  1766. static HAL_StatusTypeDef JPEG_DCHuff_BitsVals_To_SizeCodes(JPEG_DCHuffTableTypeDef *DC_BitsValsTable, JPEG_DC_HuffCodeTableTypeDef *DC_SizeCodesTable)
  1767. {
  1768. HAL_StatusTypeDef error;
  1769. uint32_t k;
  1770. uint32_t l;
  1771. uint32_t lastK;
  1772. uint8_t huffsize[257];
  1773. uint32_t huffcode[257];
  1774. error = JPEG_Bits_To_SizeCodes(DC_BitsValsTable->Bits, huffsize, huffcode, &lastK);
  1775. if(error != HAL_OK)
  1776. {
  1777. return error;
  1778. }
  1779. /* Figure C.3: ordering procedure for encoding procedure code tables */
  1780. k=0;
  1781. while(k < lastK)
  1782. {
  1783. l = DC_BitsValsTable->HuffVal[k];
  1784. if(l >= JPEG_DC_HUFF_TABLE_SIZE)
  1785. {
  1786. return HAL_ERROR; /* Huffman Table overflow error*/
  1787. }
  1788. else
  1789. {
  1790. DC_SizeCodesTable->HuffmanCode[l] = huffcode[k];
  1791. DC_SizeCodesTable->CodeLength[l] = huffsize[k] - 1;
  1792. k++;
  1793. }
  1794. }
  1795. /* Return function status */
  1796. return HAL_OK;
  1797. }
  1798. /**
  1799. * @brief Set the JPEG register with an DC huffman table at the given DC table address
  1800. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1801. * the configuration information for JPEG module
  1802. * @param HuffTableDC: pointer to DC huffman table
  1803. * @param DCTableAddress: Encoder DC huffman table address it could be HUFFENC_DC0 or HUFFENC_DC1.
  1804. * @retval HAL status
  1805. */
  1806. static HAL_StatusTypeDef JPEG_Set_HuffDC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_DCHuffTableTypeDef *HuffTableDC, uint32_t *DCTableAddress)
  1807. {
  1808. HAL_StatusTypeDef error = HAL_OK;
  1809. JPEG_DC_HuffCodeTableTypeDef dcSizeCodesTable;
  1810. uint32_t i, lsb, msb;
  1811. __IO uint32_t *address, *addressDef;
  1812. if(DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC0))
  1813. {
  1814. address = (hjpeg->Instance->HUFFENC_DC0 + (JPEG_DC_HUFF_TABLE_SIZE/2));
  1815. }
  1816. else if (DCTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_DC1))
  1817. {
  1818. address = (hjpeg->Instance->HUFFENC_DC1 + (JPEG_DC_HUFF_TABLE_SIZE/2));
  1819. }
  1820. else
  1821. {
  1822. return HAL_ERROR;
  1823. }
  1824. if(HuffTableDC != NULL)
  1825. {
  1826. error = JPEG_DCHuff_BitsVals_To_SizeCodes(HuffTableDC, &dcSizeCodesTable);
  1827. if(error != HAL_OK)
  1828. {
  1829. return error;
  1830. }
  1831. addressDef = address;
  1832. *addressDef = 0x0FFF0FFF;
  1833. addressDef++;
  1834. *addressDef = 0x0FFF0FFF;
  1835. i = JPEG_DC_HUFF_TABLE_SIZE;
  1836. while(i>0)
  1837. {
  1838. i--;
  1839. address --;
  1840. msb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
  1841. i--;
  1842. lsb = ((uint32_t)(((uint32_t)dcSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)dcSizeCodesTable.HuffmanCode[i] & 0xFF);
  1843. *address = lsb | (msb << 16);
  1844. }
  1845. }
  1846. /* Return function status */
  1847. return HAL_OK;
  1848. }
  1849. /**
  1850. * @brief Set the JPEG register with an AC huffman table at the given AC table address
  1851. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1852. * the configuration information for JPEG module
  1853. * @param HuffTableAC: pointer to AC huffman table
  1854. * @param ACTableAddress: Encoder AC huffman table address it could be HUFFENC_AC0 or HUFFENC_AC1.
  1855. * @retval HAL status
  1856. */
  1857. static HAL_StatusTypeDef JPEG_Set_HuffAC_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC, uint32_t *ACTableAddress)
  1858. {
  1859. HAL_StatusTypeDef error = HAL_OK;
  1860. JPEG_AC_HuffCodeTableTypeDef acSizeCodesTable;
  1861. uint32_t i, lsb, msb;
  1862. __IO uint32_t *address, *addressDef;
  1863. if(ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC0))
  1864. {
  1865. address = (hjpeg->Instance->HUFFENC_AC0 + (JPEG_AC_HUFF_TABLE_SIZE/2));
  1866. }
  1867. else if (ACTableAddress == (uint32_t *)(hjpeg->Instance->HUFFENC_AC1))
  1868. {
  1869. address = (hjpeg->Instance->HUFFENC_AC1 + (JPEG_AC_HUFF_TABLE_SIZE/2));
  1870. }
  1871. else
  1872. {
  1873. return HAL_ERROR;
  1874. }
  1875. if(HuffTableAC != NULL)
  1876. {
  1877. error = JPEG_ACHuff_BitsVals_To_SizeCodes(HuffTableAC, &acSizeCodesTable);
  1878. if(error != HAL_OK)
  1879. {
  1880. return error;
  1881. }
  1882. /* Default values settings: 162:167 FFFh , 168:175 FD0h_FD7h */
  1883. /* Locations 162:175 of each AC table contain information used internally by the core */
  1884. addressDef = address;
  1885. for(i=0; i<3; i++)
  1886. {
  1887. *addressDef = 0x0FFF0FFF;
  1888. addressDef++;
  1889. }
  1890. *addressDef = 0x0FD10FD0;
  1891. addressDef++;
  1892. *addressDef = 0x0FD30FD2;
  1893. addressDef++;
  1894. *addressDef = 0x0FD50FD4;
  1895. addressDef++;
  1896. *addressDef = 0x0FD70FD6;
  1897. /* end of Locations 162:175 */
  1898. i = JPEG_AC_HUFF_TABLE_SIZE;
  1899. while (i > 0)
  1900. {
  1901. i--;
  1902. address--;
  1903. msb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
  1904. i--;
  1905. lsb = ((uint32_t)(((uint32_t)acSizeCodesTable.CodeLength[i] & 0xF) << 8 )) | ((uint32_t)acSizeCodesTable.HuffmanCode[i] & 0xFF);
  1906. *address = lsb | (msb << 16);
  1907. }
  1908. }
  1909. /* Return function status */
  1910. return HAL_OK;
  1911. }
  1912. /**
  1913. * @brief Configure the JPEG encoder register huffman tables to used during
  1914. * the encdoing operation
  1915. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1916. * the configuration information for JPEG module
  1917. * @param HuffTableAC0: AC0 huffman table
  1918. * @param HuffTableDC0: DC0 huffman table
  1919. * @param HuffTableAC1: AC1 huffman table
  1920. * @param HuffTableDC1: DC1 huffman table
  1921. * @retval None
  1922. */
  1923. static HAL_StatusTypeDef JPEG_Set_HuffEnc_Mem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
  1924. {
  1925. HAL_StatusTypeDef error = HAL_OK;
  1926. JPEG_Set_Huff_DHTMem(hjpeg, HuffTableAC0, HuffTableDC0, HuffTableAC1, HuffTableDC1);
  1927. if(HuffTableAC0 != NULL)
  1928. {
  1929. error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC0, (uint32_t *)(hjpeg->Instance->HUFFENC_AC0));
  1930. if(error != HAL_OK)
  1931. {
  1932. return error;
  1933. }
  1934. }
  1935. if(HuffTableAC1 != NULL)
  1936. {
  1937. error = JPEG_Set_HuffAC_Mem(hjpeg, HuffTableAC1, (uint32_t *)(hjpeg->Instance->HUFFENC_AC1));
  1938. if(error != HAL_OK)
  1939. {
  1940. return error;
  1941. }
  1942. }
  1943. if(HuffTableDC0 != NULL)
  1944. {
  1945. error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC0, (uint32_t *)hjpeg->Instance->HUFFENC_DC0);
  1946. if(error != HAL_OK)
  1947. {
  1948. return error;
  1949. }
  1950. }
  1951. if(HuffTableDC1 != NULL)
  1952. {
  1953. error = JPEG_Set_HuffDC_Mem(hjpeg, HuffTableDC1, (uint32_t *)hjpeg->Instance->HUFFENC_DC1);
  1954. if(error != HAL_OK)
  1955. {
  1956. return error;
  1957. }
  1958. }
  1959. /* Return function status */
  1960. return HAL_OK;
  1961. }
  1962. /**
  1963. * @brief Configure the JPEG register huffman tables to be included in the JPEG
  1964. * file header (used for encoding only)
  1965. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  1966. * the configuration information for JPEG module
  1967. * @param HuffTableAC0: AC0 huffman table
  1968. * @param HuffTableDC0: DC0 huffman table
  1969. * @param HuffTableAC1: AC1 huffman table
  1970. * @param HuffTableDC1: DC1 huffman table
  1971. * @retval None
  1972. */
  1973. static void JPEG_Set_Huff_DHTMem(JPEG_HandleTypeDef *hjpeg, JPEG_ACHuffTableTypeDef *HuffTableAC0, JPEG_DCHuffTableTypeDef *HuffTableDC0 , JPEG_ACHuffTableTypeDef *HuffTableAC1, JPEG_DCHuffTableTypeDef *HuffTableDC1)
  1974. {
  1975. uint32_t value, index;
  1976. __IO uint32_t *address;
  1977. if(HuffTableDC0 != NULL)
  1978. {
  1979. /* DC0 Huffman Table : BITS*/
  1980. /* DC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address to DHTMEM + 3*/
  1981. address = (hjpeg->Instance->DHTMEM + 3);
  1982. index = 16;
  1983. while(index > 0)
  1984. {
  1985. *address = (((uint32_t)HuffTableDC0->Bits[index-1] & 0xFF) << 24)|
  1986. (((uint32_t)HuffTableDC0->Bits[index-2] & 0xFF) << 16)|
  1987. (((uint32_t)HuffTableDC0->Bits[index-3] & 0xFF) << 8) |
  1988. ((uint32_t)HuffTableDC0->Bits[index-4] & 0xFF);
  1989. address--;
  1990. index -=4;
  1991. }
  1992. /* DC0 Huffman Table : Val*/
  1993. /* DC0 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +4 to DHTMEM + 6 */
  1994. address = (hjpeg->Instance->DHTMEM + 6);
  1995. index = 12;
  1996. while(index > 0)
  1997. {
  1998. *address = (((uint32_t)HuffTableDC0->HuffVal[index-1] & 0xFF) << 24)|
  1999. (((uint32_t)HuffTableDC0->HuffVal[index-2] & 0xFF) << 16)|
  2000. (((uint32_t)HuffTableDC0->HuffVal[index-3] & 0xFF) << 8) |
  2001. ((uint32_t)HuffTableDC0->HuffVal[index-4] & 0xFF);
  2002. address--;
  2003. index -=4;
  2004. }
  2005. }
  2006. if(HuffTableAC0 != NULL)
  2007. {
  2008. /* AC0 Huffman Table : BITS*/
  2009. /* AC0 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 7 to DHTMEM + 10*/
  2010. address = (hjpeg->Instance->DHTMEM + 10);
  2011. index = 16;
  2012. while(index > 0)
  2013. {
  2014. *address = (((uint32_t)HuffTableAC0->Bits[index-1] & 0xFF) << 24)|
  2015. (((uint32_t)HuffTableAC0->Bits[index-2] & 0xFF) << 16)|
  2016. (((uint32_t)HuffTableAC0->Bits[index-3] & 0xFF) << 8) |
  2017. ((uint32_t)HuffTableAC0->Bits[index-4] & 0xFF);
  2018. address--;
  2019. index -=4;
  2020. }
  2021. /* AC0 Huffman Table : Val*/
  2022. /* AC0 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 11 to DHTMEM + 51 */
  2023. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 51) belong to AC0 VALS table */
  2024. address = (hjpeg->Instance->DHTMEM + 51);
  2025. value = *address & 0xFFFF0000U;
  2026. value = value | (((uint32_t)HuffTableAC0->HuffVal[161] & 0xFF) << 8) | ((uint32_t)HuffTableAC0->HuffVal[160] & 0xFF);
  2027. *address = value;
  2028. /*continue setting 160 AC0 huffman values */
  2029. address--; /* address = hjpeg->Instance->DHTMEM + 50*/
  2030. index = 160;
  2031. while(index > 0)
  2032. {
  2033. *address = (((uint32_t)HuffTableAC0->HuffVal[index-1] & 0xFF) << 24)|
  2034. (((uint32_t)HuffTableAC0->HuffVal[index-2] & 0xFF) << 16)|
  2035. (((uint32_t)HuffTableAC0->HuffVal[index-3] & 0xFF) << 8) |
  2036. ((uint32_t)HuffTableAC0->HuffVal[index-4] & 0xFF);
  2037. address--;
  2038. index -=4;
  2039. }
  2040. }
  2041. if(HuffTableDC1 != NULL)
  2042. {
  2043. /* DC1 Huffman Table : BITS*/
  2044. /* DC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM + 51 base address to DHTMEM + 55*/
  2045. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 51) belong to DC1 Bits table */
  2046. address = (hjpeg->Instance->DHTMEM + 51);
  2047. value = *address & 0x0000FFFFU;
  2048. value = value | (((uint32_t)HuffTableDC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->Bits[0] & 0xFF) << 16);
  2049. *address = value;
  2050. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 55) belong to DC1 Bits table */
  2051. address = (hjpeg->Instance->DHTMEM + 55);
  2052. value = *address & 0xFFFF0000U;
  2053. value = value | (((uint32_t)HuffTableDC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->Bits[14] & 0xFF);
  2054. *address = value;
  2055. /*continue setting 12 DC1 huffman Bits from DHTMEM + 54 down to DHTMEM + 52*/
  2056. address--;
  2057. index = 12;
  2058. while(index > 0)
  2059. {
  2060. *address = (((uint32_t)HuffTableDC1->Bits[index+1] & 0xFF) << 24)|
  2061. (((uint32_t)HuffTableDC1->Bits[index] & 0xFF) << 16)|
  2062. (((uint32_t)HuffTableDC1->Bits[index-1] & 0xFF) << 8) |
  2063. ((uint32_t)HuffTableDC1->Bits[index-2] & 0xFF);
  2064. address--;
  2065. index -=4;
  2066. }
  2067. /* DC1 Huffman Table : Val*/
  2068. /* DC1 VALS is a 12 Bytes table i.e 3x32bits words from DHTMEM base address +55 to DHTMEM + 58 */
  2069. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 55) belong to DC1 Val table */
  2070. address = (hjpeg->Instance->DHTMEM + 55);
  2071. value = *address & 0x0000FFFF;
  2072. value = value | (((uint32_t)HuffTableDC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableDC1->HuffVal[0] & 0xFF) << 16);
  2073. *address = value;
  2074. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 58) belong to DC1 Val table */
  2075. address = (hjpeg->Instance->DHTMEM + 58);
  2076. value = *address & 0xFFFF0000U;
  2077. value = value | (((uint32_t)HuffTableDC1->HuffVal[11] & 0xFF) << 8) | ((uint32_t)HuffTableDC1->HuffVal[10] & 0xFF);
  2078. *address = value;
  2079. /*continue setting 8 DC1 huffman val from DHTMEM + 57 down to DHTMEM + 56*/
  2080. address--;
  2081. index = 8;
  2082. while(index > 0)
  2083. {
  2084. *address = (((uint32_t)HuffTableDC1->HuffVal[index+1] & 0xFF) << 24)|
  2085. (((uint32_t)HuffTableDC1->HuffVal[index] & 0xFF) << 16)|
  2086. (((uint32_t)HuffTableDC1->HuffVal[index-1] & 0xFF) << 8) |
  2087. ((uint32_t)HuffTableDC1->HuffVal[index-2] & 0xFF);
  2088. address--;
  2089. index -=4;
  2090. }
  2091. }
  2092. if(HuffTableAC1 != NULL)
  2093. {
  2094. /* AC1 Huffman Table : BITS*/
  2095. /* AC1 BITS is a 16 Bytes table i.e 4x32bits words from DHTMEM base address + 58 to DHTMEM + 62*/
  2096. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 58) belong to AC1 Bits table */
  2097. address = (hjpeg->Instance->DHTMEM + 58);
  2098. value = *address & 0x0000FFFFU;
  2099. value = value | (((uint32_t)HuffTableAC1->Bits[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->Bits[0] & 0xFF) << 16);
  2100. *address = value;
  2101. /* only Byte 0 and Byte 1 of the last word (@ DHTMEM + 62) belong to Bits Val table */
  2102. address = (hjpeg->Instance->DHTMEM + 62);
  2103. value = *address & 0xFFFF0000U;
  2104. value = value | (((uint32_t)HuffTableAC1->Bits[15] & 0xFF) << 8) | ((uint32_t)HuffTableAC1->Bits[14] & 0xFF);
  2105. *address = value;
  2106. /*continue setting 12 AC1 huffman Bits from DHTMEM + 61 down to DHTMEM + 59*/
  2107. address--;
  2108. index = 12;
  2109. while(index > 0)
  2110. {
  2111. *address = (((uint32_t)HuffTableAC1->Bits[index+1] & 0xFF) << 24)|
  2112. (((uint32_t)HuffTableAC1->Bits[index] & 0xFF) << 16)|
  2113. (((uint32_t)HuffTableAC1->Bits[index-1] & 0xFF) << 8) |
  2114. ((uint32_t)HuffTableAC1->Bits[index-2] & 0xFF);
  2115. address--;
  2116. index -=4;
  2117. }
  2118. /* AC1 Huffman Table : Val*/
  2119. /* AC1 VALS is a 162 Bytes table i.e 41x32bits words from DHTMEM base address + 62 to DHTMEM + 102 */
  2120. /* only Byte 2 and Byte 3 of the first word (@ DHTMEM + 62) belong to AC1 VALS table */
  2121. address = (hjpeg->Instance->DHTMEM + 62);
  2122. value = *address & 0x0000FFFF;
  2123. value = value | (((uint32_t)HuffTableAC1->HuffVal[1] & 0xFF) << 24) | (((uint32_t)HuffTableAC1->HuffVal[0] & 0xFF) << 16);
  2124. *address = value;
  2125. /*continue setting 160 AC1 huffman values from DHTMEM + 63 to DHTMEM+102 */
  2126. address = (hjpeg->Instance->DHTMEM + 102);
  2127. index = 160;
  2128. while(index > 0)
  2129. {
  2130. *address = (((uint32_t)HuffTableAC1->HuffVal[index+1] & 0xFF) << 24)|
  2131. (((uint32_t)HuffTableAC1->HuffVal[index] & 0xFF) << 16)|
  2132. (((uint32_t)HuffTableAC1->HuffVal[index-1] & 0xFF) << 8) |
  2133. ((uint32_t)HuffTableAC1->HuffVal[index-2] & 0xFF);
  2134. address--;
  2135. index -=4;
  2136. }
  2137. }
  2138. }
  2139. /**
  2140. * @brief Configure the JPEG registers with a given quantization table
  2141. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2142. * the configuration information for JPEG module
  2143. * @param QTable: pointer to an array of 64 bytes giving the quantization table
  2144. * @param QTableAddress: destination quantization address in the JPEG peripheral
  2145. * it could be QMEM0, QMEM1, QMEM2 or QMEM3
  2146. * @retval None
  2147. */
  2148. static HAL_StatusTypeDef JPEG_Set_Quantization_Mem(JPEG_HandleTypeDef *hjpeg, uint8_t *QTable, uint32_t *QTableAddress)
  2149. {
  2150. uint32_t i, j, *tableAddress, quantRow, quantVal, ScaleFactor;
  2151. if((QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM0))) ||
  2152. (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM1))) ||
  2153. (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM2))) ||
  2154. (QTableAddress == ((uint32_t *)(hjpeg->Instance->QMEM3))))
  2155. {
  2156. tableAddress = QTableAddress;
  2157. }
  2158. else
  2159. {
  2160. return HAL_ERROR;
  2161. }
  2162. if ((hjpeg->Conf.ImageQuality >= 50) && (hjpeg->Conf.ImageQuality <= 100))
  2163. {
  2164. ScaleFactor = 200 - (hjpeg->Conf.ImageQuality * 2);
  2165. }
  2166. else if (hjpeg->Conf.ImageQuality > 0)
  2167. {
  2168. ScaleFactor = ((uint32_t) 5000) / ((uint32_t) hjpeg->Conf.ImageQuality);
  2169. }
  2170. else
  2171. {
  2172. return HAL_ERROR;
  2173. }
  2174. /*Quantization_table = (Standard_quanization_table * ScaleFactor + 50) / 100*/
  2175. i = 0;
  2176. while( i < JPEG_QUANT_TABLE_SIZE)
  2177. {
  2178. quantRow = 0;
  2179. for(j=0; j<4; j++)
  2180. {
  2181. /* Note that the quantization coefficients must be specified in the table in zigzag order */
  2182. quantVal = ((((uint32_t) QTable[JPEG_ZIGZAG_ORDER[i+j]]) * ScaleFactor) + 50) / 100;
  2183. if(quantVal == 0)
  2184. {
  2185. quantVal = 1;
  2186. }
  2187. else if (quantVal > 255)
  2188. {
  2189. quantVal = 255;
  2190. }
  2191. quantRow |= ((quantVal & 0xFF) << (8 * j));
  2192. }
  2193. i += 4;
  2194. *tableAddress = quantRow;
  2195. tableAddress ++;
  2196. }
  2197. /* Return function status */
  2198. return HAL_OK;
  2199. }
  2200. /**
  2201. * @brief Configure the JPEG registers for YCbCr color space
  2202. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2203. * the configuration information for JPEG module
  2204. * @retval None
  2205. */
  2206. static void JPEG_SetColorYCBCR(JPEG_HandleTypeDef *hjpeg)
  2207. {
  2208. uint32_t ySamplingH;
  2209. uint32_t ySamplingV;
  2210. uint32_t yblockNb;
  2211. /*Set Number of color components to 3*/
  2212. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_NF;
  2213. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_NF_1;
  2214. /* compute MCU block size and Y, Cb ,Cr sampling factors*/
  2215. if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
  2216. {
  2217. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2218. ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
  2219. yblockNb = 0x30; /* 4 blocks of 8x8*/
  2220. }
  2221. else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
  2222. {
  2223. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2224. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2225. yblockNb = 0x10; /* 2 blocks of 8x8*/
  2226. }
  2227. else /*JPEG_444_SUBSAMPLING and default*/
  2228. {
  2229. ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
  2230. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2231. yblockNb = 0; /* 1 block of 8x8*/
  2232. }
  2233. hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
  2234. hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF_1 | JPEG_CONFR1_NS_1);
  2235. /*Reset CONFR4 register*/
  2236. hjpeg->Instance->CONFR4 = 0;
  2237. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
  2238. hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
  2239. /*Reset CONFR5 register*/
  2240. hjpeg->Instance->CONFR5 = 0;
  2241. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
  2242. hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0 | JPEG_CONFR5_QT_0 | JPEG_CONFR5_HA | JPEG_CONFR5_HD);
  2243. /*Reset CONFR6 register*/
  2244. hjpeg->Instance->CONFR6 = 0;
  2245. /*Set Horizental and Vertical sampling factor and number of blocks for component 2*/
  2246. /* In YCBCR , by default, both chrominance components (component 1 and component 2) use the same Quantization table (table 1) */
  2247. /* In YCBCR , both chrominance components (component 1 and component 2) use the same Huffman tables (table 1) */
  2248. hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0 | JPEG_CONFR6_QT_0 | JPEG_CONFR6_HA | JPEG_CONFR6_HD);
  2249. }
  2250. /**
  2251. * @brief Configure the JPEG registers for GrayScale color space
  2252. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2253. * the configuration information for JPEG module
  2254. * @retval None
  2255. */
  2256. static void JPEG_SetColorGrayScale(JPEG_HandleTypeDef *hjpeg)
  2257. {
  2258. /*Set Number of color components to 1*/
  2259. hjpeg->Instance->CONFR1 &= ~(JPEG_CONFR1_NF | JPEG_CONFR1_NS);
  2260. /*in GrayScale use 1 single Quantization table (Table 0)*/
  2261. /*in GrayScale use only one couple of AC/DC huffman table (table 0)*/
  2262. /*Reset CONFR4 register*/
  2263. hjpeg->Instance->CONFR4 = 0;
  2264. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
  2265. hjpeg->Instance->CONFR4 |= JPEG_CONFR4_HSF_0 | JPEG_CONFR4_VSF_0 ;
  2266. }
  2267. /**
  2268. * @brief Configure the JPEG registers for CMYK color space
  2269. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2270. * the configuration information for JPEG module
  2271. * @retval None
  2272. */
  2273. static void JPEG_SetColorCMYK(JPEG_HandleTypeDef *hjpeg)
  2274. {
  2275. uint32_t ySamplingH;
  2276. uint32_t ySamplingV;
  2277. uint32_t yblockNb;
  2278. /*Set Number of color components to 4*/
  2279. hjpeg->Instance->CONFR1 |= (JPEG_CONFR1_NF | JPEG_CONFR1_NS);
  2280. /* compute MCU block size and Y, Cb ,Cr sampling factors*/
  2281. if(hjpeg->Conf.ChromaSubsampling == JPEG_420_SUBSAMPLING)
  2282. {
  2283. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2284. ySamplingV = JPEG_CONFR4_VSF_1; /* Vs = 2*/
  2285. yblockNb = 0x30; /* 4 blocks of 8x8*/
  2286. }
  2287. else if(hjpeg->Conf.ChromaSubsampling == JPEG_422_SUBSAMPLING)
  2288. {
  2289. ySamplingH = JPEG_CONFR4_HSF_1; /* Hs = 2*/
  2290. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2291. yblockNb = 0x10; /* 2 blocks of 8x8*/
  2292. }
  2293. else /*JPEG_444_SUBSAMPLING and default*/
  2294. {
  2295. ySamplingH = JPEG_CONFR4_HSF_0; /* Hs = 1*/
  2296. ySamplingV = JPEG_CONFR4_VSF_0; /* Vs = 1*/
  2297. yblockNb = 0; /* 1 block of 8x8*/
  2298. }
  2299. /*Reset CONFR4 register*/
  2300. hjpeg->Instance->CONFR4 = 0;
  2301. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 0*/
  2302. hjpeg->Instance->CONFR4 |= (ySamplingH | ySamplingV | (yblockNb & JPEG_CONFR4_NB) );
  2303. /*Reset CONFR5 register*/
  2304. hjpeg->Instance->CONFR5 = 0;
  2305. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 1*/
  2306. hjpeg->Instance->CONFR5 |= (JPEG_CONFR5_HSF_0 | JPEG_CONFR5_VSF_0);
  2307. /*Reset CONFR6 register*/
  2308. hjpeg->Instance->CONFR6 = 0;
  2309. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 2*/
  2310. hjpeg->Instance->CONFR6 |= (JPEG_CONFR6_HSF_0 | JPEG_CONFR6_VSF_0);
  2311. /*Reset CONFR7 register*/
  2312. hjpeg->Instance->CONFR7 = 0;
  2313. /*Set Horizental and Vertical sampling factor , number of blocks , Quantization table and Huffman AC/DC tables for component 3*/
  2314. hjpeg->Instance->CONFR7 |= (JPEG_CONFR7_HSF_0 | JPEG_CONFR7_VSF_0);
  2315. }
  2316. /**
  2317. * @brief Init the JPEG encoding/decoding process in case of Polling or Interrupt and DMA
  2318. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2319. * the configuration information for JPEG module
  2320. * @retval None
  2321. */
  2322. static void JPEG_Init_Process(JPEG_HandleTypeDef *hjpeg)
  2323. {
  2324. /*Reset pause*/
  2325. hjpeg->Context &= (~(JPEG_CONTEXT_PAUSE_INPUT | JPEG_CONTEXT_PAUSE_OUTPUT));
  2326. if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2327. {
  2328. /*Set JPEG Codec to Decoding mode */
  2329. hjpeg->Instance->CONFR1 |= JPEG_CONFR1_DE;
  2330. }
  2331. else if((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2332. {
  2333. /*Set JPEG Codec to Encoding mode */
  2334. hjpeg->Instance->CONFR1 &= ~JPEG_CONFR1_DE;
  2335. }
  2336. /*Stop JPEG processing */
  2337. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2338. /* Disable All Interrupts */
  2339. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2340. /* Disable All DMA requests */
  2341. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  2342. /* Flush input and output FIFOs*/
  2343. hjpeg->Instance->CR |= JPEG_CR_IFF;
  2344. hjpeg->Instance->CR |= JPEG_CR_OFF;
  2345. /* Clear all flags */
  2346. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  2347. /*Start Encoding/Decoding*/
  2348. hjpeg->Instance->CONFR0 |= JPEG_CONFR0_START;
  2349. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  2350. {
  2351. /*Enable IN/OUT, end of Conversation, and end of header parsing interruptions*/
  2352. __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_IFT | JPEG_IT_IFNF | JPEG_IT_OFT | JPEG_IT_OFNE | JPEG_IT_EOC |JPEG_IT_HPD);
  2353. }
  2354. else if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA)
  2355. {
  2356. /*Enable End Of Conversation, and End Of Header parsing interruptions*/
  2357. __HAL_JPEG_ENABLE_IT(hjpeg, JPEG_IT_EOC |JPEG_IT_HPD);
  2358. }
  2359. }
  2360. /**
  2361. * @brief JPEG encoding/decoding process in case of Polling or Interrupt
  2362. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2363. * the configuration information for JPEG module
  2364. * @retval JPEG_PROCESS_DONE if the process has ends else JPEG_PROCESS_ONGOING
  2365. */
  2366. static uint32_t JPEG_Process(JPEG_HandleTypeDef *hjpeg)
  2367. {
  2368. uint32_t tmpContext;
  2369. /*End of header processing flag rised*/
  2370. if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
  2371. {
  2372. /*Call Header parsing complet callback */
  2373. HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
  2374. /* Reset the ImageQuality */
  2375. hjpeg->Conf.ImageQuality = 0;
  2376. /* Note : the image quality is only available at the end of the decoding operation */
  2377. /* at the current stage the calculated image quality is not correct so reset it */
  2378. /*Call Info Ready callback */
  2379. HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
  2380. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
  2381. /* Clear header processing done flag */
  2382. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
  2383. }
  2384. /*Input FIFO status handling*/
  2385. if((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0)
  2386. {
  2387. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFTF) != RESET)
  2388. {
  2389. /*Input FIFO threshold flag rised*/
  2390. /*4 words (16 bytes) can be written in */
  2391. JPEG_ReadInputData(hjpeg,4);
  2392. }
  2393. else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_IFNFF) != RESET)
  2394. {
  2395. /*Input FIFO Not Full flag rised*/
  2396. /*32-bit value can be written in */
  2397. JPEG_ReadInputData(hjpeg,1);
  2398. }
  2399. }
  2400. /*Output FIFO flag handling*/
  2401. if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
  2402. {
  2403. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFTF) != RESET)
  2404. {
  2405. /*Output FIFO threshold flag rised*/
  2406. /*4 words (16 bytes) can be read out */
  2407. JPEG_StoreOutputData(hjpeg, 4);
  2408. }
  2409. else if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != RESET)
  2410. {
  2411. /*Output FIFO Not Empty flag rised*/
  2412. /*32-bit value can be read out */
  2413. JPEG_StoreOutputData(hjpeg, 1);
  2414. }
  2415. }
  2416. /*End of Conversion handling :i.e EOC flag is high and OFTF low and OFNEF low*/
  2417. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF | JPEG_FLAG_OFTF | JPEG_FLAG_OFNEF) == JPEG_FLAG_EOCF)
  2418. {
  2419. /*Stop Encoding/Decoding*/
  2420. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2421. if((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_IT)
  2422. {
  2423. /* Disable All Interrupts */
  2424. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2425. }
  2426. /* Clear all flags */
  2427. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  2428. /*Call End of conversion callback */
  2429. if(hjpeg->JpegOutCount > 0)
  2430. {
  2431. /*Output Buffer is not empty, call DecodedDataReadyCallback*/
  2432. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2433. hjpeg->JpegOutCount = 0;
  2434. }
  2435. /*Reset Context Operation*/
  2436. tmpContext = hjpeg->Context;
  2437. /*Clear all context fields execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
  2438. hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
  2439. /* Process Unlocked */
  2440. __HAL_UNLOCK(hjpeg);
  2441. /* Change the JPEG state */
  2442. hjpeg->State = HAL_JPEG_STATE_READY;
  2443. /*Call End of Encoding/Decoding callback */
  2444. if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2445. {
  2446. HAL_JPEG_DecodeCpltCallback(hjpeg);
  2447. }
  2448. else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2449. {
  2450. HAL_JPEG_EncodeCpltCallback(hjpeg);
  2451. }
  2452. return JPEG_PROCESS_DONE;
  2453. }
  2454. return JPEG_PROCESS_ONGOING;
  2455. }
  2456. /**
  2457. * @brief Store some output data from the JPEG peripheral to the output buffer.
  2458. * This function is used when the JPEG peripheral has new data to output
  2459. * in case of Polling or Interrupt process
  2460. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2461. * the configuration information for JPEG module
  2462. * @param nbOutputWords: Number of output words (of 32 bits) ready from the JPEG peripheral
  2463. * @retval None
  2464. */
  2465. static void JPEG_StoreOutputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbOutputWords)
  2466. {
  2467. uint32_t index, nBwords, nbBytes , dataword, *pOutData;
  2468. pOutData = (uint32_t *)(((uint32_t *)hjpeg->pJpegOutBuffPtr) + (hjpeg->JpegOutCount/4));
  2469. if(hjpeg->OutDataLength >= (hjpeg->JpegOutCount + (nbOutputWords*4)))
  2470. {
  2471. for(index = 0; index < nbOutputWords; index++)
  2472. {
  2473. /*Transfer 32 bits from the JPEG output FIFO*/
  2474. *pOutData = hjpeg->Instance->DOR;
  2475. pOutData++;
  2476. hjpeg->JpegOutCount += 4;
  2477. }
  2478. if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
  2479. {
  2480. /*Output Buffer is full, call DecodedDataReadyCallback*/
  2481. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2482. hjpeg->JpegOutCount = 0;
  2483. }
  2484. }
  2485. else if(hjpeg->OutDataLength > hjpeg->JpegOutCount)
  2486. {
  2487. nBwords = (hjpeg->OutDataLength - hjpeg->JpegOutCount)/4;
  2488. for(index = 0; index < nBwords; index++)
  2489. {
  2490. /*Transfer 32 bits from the JPEG output FIFO*/
  2491. *pOutData = hjpeg->Instance->DOR;
  2492. pOutData++;
  2493. hjpeg->JpegOutCount += 4;
  2494. }
  2495. if(hjpeg->OutDataLength == hjpeg->JpegOutCount)
  2496. {
  2497. /*Output Buffer is full, call DecodedDataReadyCallback*/
  2498. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2499. hjpeg->JpegOutCount = 0;
  2500. }
  2501. else
  2502. {
  2503. nbBytes = hjpeg->OutDataLength - hjpeg->JpegOutCount;
  2504. dataword = hjpeg->Instance->DOR;
  2505. for(index = 0; index < nbBytes; index++)
  2506. {
  2507. hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
  2508. hjpeg->JpegOutCount++;
  2509. }
  2510. /*Output Buffer is full, call DecodedDataReadyCallback*/
  2511. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2512. hjpeg->JpegOutCount = 0;
  2513. nbBytes = 4 - nbBytes;
  2514. for(index = nbBytes; index < 4; index++)
  2515. {
  2516. hjpeg->pJpegOutBuffPtr[hjpeg->JpegOutCount] = (dataword >> (8*index)) & 0xFF;
  2517. hjpeg->JpegOutCount++;
  2518. }
  2519. }
  2520. }
  2521. }
  2522. /**
  2523. * @brief Read some input Data from the input buffer.
  2524. * This function is used when the JPEG peripheral needs new data
  2525. * in case of Polling or Interrupt process
  2526. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2527. * the configuration information for JPEG module
  2528. * @param nbRequestWords: Number of input words (of 32 bits) that the JPE peripheral request
  2529. * @retval None
  2530. */
  2531. static void JPEG_ReadInputData(JPEG_HandleTypeDef *hjpeg, uint32_t nbRequestWords)
  2532. {
  2533. uint32_t nbBytes = 0, nBwords, index, Dataword;
  2534. if((hjpeg->InDataLength == 0) || (nbRequestWords == 0))
  2535. {
  2536. /* No more Input data : nothing to do*/
  2537. HAL_JPEG_Pause(hjpeg, JPEG_PAUSE_RESUME_INPUT);
  2538. }
  2539. else if(hjpeg->InDataLength > hjpeg->JpegInCount)
  2540. {
  2541. nbBytes = hjpeg->InDataLength - hjpeg->JpegInCount;
  2542. }
  2543. else if(hjpeg->InDataLength == hjpeg->JpegInCount)
  2544. {
  2545. /*Call HAL_JPEG_GetDataCallback to get new data */
  2546. HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
  2547. if(hjpeg->InDataLength > 4)
  2548. {
  2549. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  2550. }
  2551. hjpeg->JpegInCount = 0;
  2552. nbBytes = hjpeg->InDataLength;
  2553. }
  2554. if((nbBytes > 0) && ((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0))
  2555. {
  2556. nBwords = nbBytes / 4;
  2557. if(nBwords >= nbRequestWords)
  2558. {
  2559. for(index = 0; index < nbRequestWords; index++)
  2560. {
  2561. hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
  2562. hjpeg->JpegInCount += 4;
  2563. }
  2564. }
  2565. else /*nBwords < nbRequestWords*/
  2566. {
  2567. if(nBwords > 0)
  2568. {
  2569. for(index = 0; index < nBwords; index++)
  2570. {
  2571. hjpeg->Instance->DIR = *((uint32_t *)(((uint32_t *)hjpeg->pJpegInBuffPtr) + (hjpeg->JpegInCount/4)));
  2572. hjpeg->JpegInCount += 4;
  2573. }
  2574. }
  2575. else
  2576. {
  2577. /* end of file*/
  2578. Dataword = 0;
  2579. for(index=0; index< nbBytes; index++)
  2580. {
  2581. Dataword |= (uint32_t)hjpeg->pJpegInBuffPtr[hjpeg->JpegInCount] << (8 * index);
  2582. hjpeg->JpegInCount++;
  2583. }
  2584. hjpeg->Instance->DIR = Dataword;
  2585. }
  2586. }
  2587. }
  2588. }
  2589. /**
  2590. * @brief Start the JPEG DMA process (encoding/decoding)
  2591. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2592. * the configuration information for JPEG module
  2593. * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
  2594. */
  2595. static HAL_StatusTypeDef JPEG_DMA_StartProcess(JPEG_HandleTypeDef *hjpeg)
  2596. {
  2597. if((hjpeg->InDataLength < 4) || (hjpeg->OutDataLength < 4))
  2598. {
  2599. return HAL_ERROR;
  2600. }
  2601. /* Reset Ending DMA internal context flag*/
  2602. hjpeg->Context &= ~JPEG_CONTEXT_ENDING_DMA;
  2603. /* Disable DMA In/Out Request*/
  2604. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
  2605. /* Set the JPEG DMA In transfer complete callback */
  2606. hjpeg->hdmain->XferCpltCallback = JPEG_DMAInCpltCallback;
  2607. /* Set the DMA In error callback */
  2608. hjpeg->hdmain->XferErrorCallback = JPEG_DMAErrorCallback;
  2609. /* Set the JPEG DMA Out transfer complete callback */
  2610. hjpeg->hdmaout->XferCpltCallback = JPEG_DMAOutCpltCallback;
  2611. /* Set the DMA Out error callback */
  2612. hjpeg->hdmaout->XferErrorCallback = JPEG_DMAErrorCallback;
  2613. /* Set the DMA Out Abort callback */
  2614. hjpeg->hdmaout->XferAbortCallback = JPEG_DMAOutAbortCallback;
  2615. /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
  2616. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  2617. /*DMA transfer size must be a multiple of 4 bytes i.e mutliple of 32bits words*/
  2618. hjpeg->OutDataLength = hjpeg->OutDataLength - (hjpeg->OutDataLength % 4);
  2619. /* Start DMA FIFO In transfer */
  2620. HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
  2621. /* Start DMA FIFO Out transfer */
  2622. HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
  2623. /* Enable JPEG In/Out DMA requests*/
  2624. JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA | JPEG_DMA_ODMA);
  2625. return HAL_OK;
  2626. }
  2627. /**
  2628. * @brief Continue the current JPEG DMA process (encoding/decoding)
  2629. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2630. * the configuration information for JPEG module
  2631. * @retval JPEG_PROCESS_DONE if process ends else JPEG_PROCESS_ONGOING
  2632. */
  2633. static uint32_t JPEG_DMA_ContinueProcess(JPEG_HandleTypeDef *hjpeg)
  2634. {
  2635. /*End of header processing flag rises*/
  2636. if(((hjpeg->Context & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE) && (__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_HPDF) != RESET))
  2637. {
  2638. /*Call Header parsing complete callback */
  2639. HAL_JPEG_GetInfo(hjpeg, &hjpeg->Conf);
  2640. /* Reset the ImageQuality */
  2641. hjpeg->Conf.ImageQuality = 0;
  2642. /* Note : the image quality is only available at the end of the decoding operation */
  2643. /* at the current stage the calculated image quality is not correct so reset it */
  2644. /*Call Info Ready callback */
  2645. HAL_JPEG_InfoReadyCallback(hjpeg, &hjpeg->Conf);
  2646. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_IT_HPD);
  2647. /* Clear header processing done flag */
  2648. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_HPDF);
  2649. }
  2650. /*End of Conversion handling*/
  2651. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) != RESET)
  2652. {
  2653. /*Disabkle JPEG In/Out DMA Requests*/
  2654. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA | JPEG_DMA_IDMA);
  2655. hjpeg->Context |= JPEG_CONTEXT_ENDING_DMA;
  2656. /*Stop Encoding/Decoding*/
  2657. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2658. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2659. /* Clear all flags */
  2660. __HAL_JPEG_CLEAR_FLAG(hjpeg,JPEG_FLAG_ALL);
  2661. if(hjpeg->hdmain->State == HAL_DMA_STATE_BUSY)
  2662. {
  2663. /* Stop the DMA In Xfer*/
  2664. HAL_DMA_Abort_IT(hjpeg->hdmain);
  2665. }
  2666. if(hjpeg->hdmaout->State == HAL_DMA_STATE_BUSY)
  2667. {
  2668. /* Stop the DMA out Xfer*/
  2669. HAL_DMA_Abort_IT(hjpeg->hdmaout);
  2670. }
  2671. else
  2672. {
  2673. return JPEG_DMA_EndProcess(hjpeg);
  2674. }
  2675. }
  2676. return JPEG_PROCESS_ONGOING;
  2677. }
  2678. /**
  2679. * @brief Finalize the current JPEG DMA process (encoding/decoding)
  2680. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2681. * the configuration information for JPEG module
  2682. * @retval JPEG_PROCESS_DONE
  2683. */
  2684. static uint32_t JPEG_DMA_EndProcess(JPEG_HandleTypeDef *hjpeg)
  2685. {
  2686. uint32_t tmpContext;
  2687. hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hjpeg->hdmaout->Instance->NDTR & DMA_SxNDT) << 2);
  2688. /*if Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
  2689. if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
  2690. {
  2691. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2692. hjpeg->JpegOutCount = 0;
  2693. }
  2694. /*Check if remaining data in the output FIFO*/
  2695. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) == 0)
  2696. {
  2697. /*Stop Encoding/Decoding*/
  2698. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2699. tmpContext = hjpeg->Context;
  2700. /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
  2701. hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
  2702. /* Process Unlocked */
  2703. __HAL_UNLOCK(hjpeg);
  2704. /* Change the JPEG state */
  2705. hjpeg->State = HAL_JPEG_STATE_READY;
  2706. /*Call End of Encoding/Decoding callback */
  2707. if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2708. {
  2709. HAL_JPEG_DecodeCpltCallback(hjpeg);
  2710. }
  2711. else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2712. {
  2713. HAL_JPEG_EncodeCpltCallback(hjpeg);
  2714. }
  2715. }
  2716. else if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
  2717. {
  2718. JPEG_DMA_PollResidualData(hjpeg);
  2719. return JPEG_PROCESS_DONE;
  2720. }
  2721. return JPEG_PROCESS_ONGOING;
  2722. }
  2723. /**
  2724. * @brief Poll residual output data when DMA process (encoding/decoding)
  2725. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2726. * the configuration information for JPEG module
  2727. * @retval None.
  2728. */
  2729. static void JPEG_DMA_PollResidualData(JPEG_HandleTypeDef *hjpeg)
  2730. {
  2731. uint32_t tmpContext, count = JPEG_FIFO_SIZE, *pDataOut;
  2732. pDataOut = (uint32_t *)(hjpeg->pJpegOutBuffPtr + hjpeg->JpegOutCount);
  2733. while((__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_OFNEF) != 0) && (count > 0))
  2734. {
  2735. count--;
  2736. *pDataOut = hjpeg->Instance->DOR;
  2737. pDataOut++;
  2738. hjpeg->JpegOutCount += 4;
  2739. if(hjpeg->JpegOutCount == hjpeg->OutDataLength)
  2740. {
  2741. /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
  2742. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2743. hjpeg->JpegOutCount = 0;
  2744. }
  2745. }
  2746. /*Stop Encoding/Decoding*/
  2747. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2748. if(hjpeg->JpegOutCount > 0)
  2749. {
  2750. /*Output Buffer is not empty, call DecodedDataReadyCallback*/
  2751. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2752. hjpeg->JpegOutCount = 0;
  2753. }
  2754. tmpContext = hjpeg->Context;
  2755. /*Clear all context fileds execpt JPEG_CONTEXT_CONF_ENCODING and JPEG_CONTEXT_CUSTOM_TABLES*/
  2756. hjpeg->Context &= (JPEG_CONTEXT_CONF_ENCODING | JPEG_CONTEXT_CUSTOM_TABLES);
  2757. /* Process Unlocked */
  2758. __HAL_UNLOCK(hjpeg);
  2759. /* Change the JPEG state */
  2760. hjpeg->State = HAL_JPEG_STATE_READY;
  2761. /*Call End of Encoding/Decoding callback */
  2762. if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_DECODE)
  2763. {
  2764. HAL_JPEG_DecodeCpltCallback(hjpeg);
  2765. }
  2766. else if((tmpContext & JPEG_CONTEXT_OPERATION_MASK) == JPEG_CONTEXT_ENCODE)
  2767. {
  2768. HAL_JPEG_EncodeCpltCallback(hjpeg);
  2769. }
  2770. }
  2771. /**
  2772. * @brief DMA input transfer complete callback
  2773. * @param hdma: pointer to a DMA_HandleTypeDef structure.
  2774. * @retval None
  2775. */
  2776. static void JPEG_DMAInCpltCallback(DMA_HandleTypeDef *hdma)
  2777. {
  2778. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2779. /* Disable The JPEG IT so the DMA Input Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
  2780. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2781. if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
  2782. {
  2783. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_IDMA);
  2784. hjpeg->JpegInCount = hjpeg->InDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
  2785. /*Call HAL_JPEG_GetDataCallback to get new data */
  2786. HAL_JPEG_GetDataCallback(hjpeg, hjpeg->JpegInCount);
  2787. if(hjpeg->InDataLength >= 4)
  2788. {
  2789. /*JPEG Input DMA transfer data number must be multiple of 32 bits word
  2790. as the destination is a 32 bits (4 bytes) register */
  2791. hjpeg->InDataLength = hjpeg->InDataLength - (hjpeg->InDataLength % 4);
  2792. }
  2793. else if(hjpeg->InDataLength > 0)
  2794. {
  2795. /*Transfer last data word (i.e last 4 bytes)*/
  2796. hjpeg->InDataLength = 4;
  2797. }
  2798. if(((hjpeg->Context & JPEG_CONTEXT_PAUSE_INPUT) == 0) && (hjpeg->InDataLength > 0))
  2799. {
  2800. /* Start DMA FIFO In transfer */
  2801. HAL_DMA_Start_IT(hjpeg->hdmain, (uint32_t)hjpeg->pJpegInBuffPtr, (uint32_t)&hjpeg->Instance->DIR, hjpeg->InDataLength >> 2);
  2802. JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_IDMA);
  2803. }
  2804. /* JPEG Conversion still on going : Enable the JPEG IT */
  2805. __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
  2806. }
  2807. }
  2808. /**
  2809. * @brief DMA output transfer complete callback
  2810. * @param hdma: pointer to a DMA_HandleTypeDef structure.
  2811. * @retval None
  2812. */
  2813. static void JPEG_DMAOutCpltCallback(DMA_HandleTypeDef *hdma)
  2814. {
  2815. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2816. /* Disable The JPEG IT so the DMA Output Callback can not be interrupted by the JPEG EOC IT or JPEG HPD IT */
  2817. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2818. if(((hjpeg->Context & JPEG_CONTEXT_METHOD_MASK) == JPEG_CONTEXT_DMA) && ((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) == 0))
  2819. {
  2820. if(__HAL_JPEG_GET_FLAG(hjpeg, JPEG_FLAG_EOCF) == 0)
  2821. {
  2822. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_ODMA);
  2823. hjpeg->JpegOutCount = hjpeg->OutDataLength - ((hdma->Instance->NDTR & DMA_SxNDT) << 2);
  2824. /*Output Buffer is full, call HAL_JPEG_DataReadyCallback*/
  2825. HAL_JPEG_DataReadyCallback (hjpeg, hjpeg->pJpegOutBuffPtr, hjpeg->JpegOutCount);
  2826. if((hjpeg->Context & JPEG_CONTEXT_PAUSE_OUTPUT) == 0)
  2827. {
  2828. /* Start DMA FIFO Out transfer */
  2829. HAL_DMA_Start_IT(hjpeg->hdmaout, (uint32_t)&hjpeg->Instance->DOR, (uint32_t)hjpeg->pJpegOutBuffPtr, hjpeg->OutDataLength >> 2);
  2830. JPEG_ENABLE_DMA(hjpeg,JPEG_DMA_ODMA);
  2831. }
  2832. }
  2833. /* JPEG Conversion still on going : Enable the JPEG IT */
  2834. __HAL_JPEG_ENABLE_IT(hjpeg,JPEG_IT_EOC |JPEG_IT_HPD);
  2835. }
  2836. }
  2837. /**
  2838. * @brief DMA Transfer error callback
  2839. * @param hdma: pointer to a DMA_HandleTypeDef structure.
  2840. * @retval None
  2841. */
  2842. static void JPEG_DMAErrorCallback(DMA_HandleTypeDef *hdma)
  2843. {
  2844. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2845. /* if DMA error is FIFO error ignore it */
  2846. if(HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
  2847. {
  2848. /*Stop Encoding/Decoding*/
  2849. hjpeg->Instance->CONFR0 &= ~JPEG_CONFR0_START;
  2850. /* Disable All Interrupts */
  2851. __HAL_JPEG_DISABLE_IT(hjpeg,JPEG_INTERRUPT_MASK);
  2852. /* Disable All DMA requests */
  2853. JPEG_DISABLE_DMA(hjpeg,JPEG_DMA_MASK);
  2854. hjpeg->State= HAL_JPEG_STATE_READY;
  2855. hjpeg->ErrorCode |= HAL_JPEG_ERROR_DMA;
  2856. HAL_JPEG_ErrorCallback(hjpeg);
  2857. }
  2858. }
  2859. /**
  2860. * @brief DMA output Abort callback
  2861. * @param hdma: pointer to a DMA_HandleTypeDef structure.
  2862. * @retval None
  2863. */
  2864. static void JPEG_DMAOutAbortCallback(DMA_HandleTypeDef *hdma)
  2865. {
  2866. JPEG_HandleTypeDef* hjpeg = (JPEG_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  2867. if((hjpeg->Context & JPEG_CONTEXT_ENDING_DMA) != 0)
  2868. {
  2869. JPEG_DMA_EndProcess(hjpeg);
  2870. }
  2871. }
  2872. /**
  2873. * @brief Calculate the decoded image quality (from 1 to 100)
  2874. * @param hjpeg: pointer to a JPEG_HandleTypeDef structure that contains
  2875. * the configuration information for JPEG module
  2876. * @retval JPEG image quality from 1 to 100.
  2877. */
  2878. static uint32_t JPEG_GetQuality(JPEG_HandleTypeDef *hjpeg)
  2879. {
  2880. uint32_t quality = 0;
  2881. uint32_t quantRow, quantVal,scale, i, j;
  2882. uint32_t *tableAddress = (uint32_t *)hjpeg->Instance->QMEM0;
  2883. i = 0;
  2884. while( i < JPEG_QUANT_TABLE_SIZE)
  2885. {
  2886. quantRow = *tableAddress;
  2887. for(j=0; j<4; j++)
  2888. {
  2889. quantVal = (quantRow >> (8 * j)) & 0xFF;
  2890. if(quantVal == 1)
  2891. {
  2892. /* if Quantization value = 1 then quality is 100%*/
  2893. quality += 100;
  2894. }
  2895. else
  2896. {
  2897. /* Note that the quantization coefficients must be specified in the table in zigzag order */
  2898. scale = (quantVal*100)/((uint32_t) JPEG_LUM_QuantTable[JPEG_ZIGZAG_ORDER[i+j]]);
  2899. if(scale <= 100)
  2900. {
  2901. quality += (200 - scale)/2;
  2902. }
  2903. else
  2904. {
  2905. quality += 5000/scale;
  2906. }
  2907. }
  2908. }
  2909. i += 4;
  2910. tableAddress ++;
  2911. }
  2912. return (quality/((uint32_t)64));
  2913. }
  2914. /**
  2915. * @}
  2916. */
  2917. #endif /* STM32F767xx || STM32F769xx || STM32F777xx || STM32F779xx */
  2918. #endif /* HAL_JPEG_MODULE_ENABLED */
  2919. /**
  2920. * @}
  2921. */
  2922. /**
  2923. * @}
  2924. */
  2925. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/