drv_jpeg.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-12-15 Rbb666 the first version
  9. */
  10. #include <rtthread.h>
  11. #include "rthw.h"
  12. #include "drv_g2d.h"
  13. #include "drv_jpeg.h"
  14. #ifdef BSP_USING_JPEG
  15. static rt_sem_t _SemaphoreJPEG = RT_NULL;
  16. volatile static jpeg_status_t g_jpeg_status = JPEG_STATUS_NONE;
  17. static int32_t _WaitForCallbackTimed(uint32_t TimeOut)
  18. {
  19. return rt_sem_take(_SemaphoreJPEG, TimeOut) ? RT_ETIMEOUT : RT_EOK;
  20. }
  21. static void _decode_read(decode_drv_t *decode, int32_t x, int32_t y, void *pInBuffer, int32_t xSize, int32_t ySize)
  22. {
  23. decode->decode_read(x, y, pInBuffer, xSize, ySize);
  24. }
  25. int JPEG_Draw_frame(decode_drv_t *decode, void *pbuffer, int32_t x0, int32_t y0)
  26. {
  27. int32_t x;
  28. int32_t y;
  29. int32_t Error;
  30. int32_t xSize;
  31. int32_t ySize;
  32. int32_t OutBufferSize;
  33. uint32_t LinesDecoded;
  34. jpeg_color_space_t ColorSpace;
  35. void *pOutBuffer;
  36. void *_aInBuffer;
  37. Error = 0;
  38. xSize = 0;
  39. ySize = 0;
  40. x = x0;
  41. y = y0;
  42. g_jpeg_status = JPEG_STATUS_NONE;
  43. fsp_err_t err = FSP_SUCCESS;
  44. /* Initialize JPEG Codec Driver */
  45. err = R_JPEG_Open(&g_jpeg0_ctrl, &g_jpeg0_cfg);
  46. /* Handle error */
  47. if (FSP_SUCCESS != err)
  48. {
  49. /* JPEG Codec initialization failed */
  50. rt_kprintf("JPEG Codec driver initialization FAILED\n");
  51. }
  52. _aInBuffer = (uint8_t *)pbuffer;
  53. while (!(g_jpeg_status & JPEG_STATUS_ERROR))
  54. {
  55. /* Set in-buffer to get some information about the JPEG */
  56. if (R_JPEG_InputBufferSet(&g_jpeg0_ctrl, _aInBuffer,
  57. DCODE_BUFFER_SIZE) != FSP_SUCCESS)
  58. {
  59. Error = 2;
  60. break;
  61. }
  62. /* Wait for callback */
  63. if (_WaitForCallbackTimed(JPEG_TIMEOUT) == RT_ETIMEOUT)
  64. {
  65. Error = 3;
  66. break;
  67. }
  68. if (g_jpeg_status & JPEG_STATUS_IMAGE_SIZE_READY)
  69. {
  70. break;
  71. }
  72. /* Move pointer to next block of input data (if needed) */
  73. // _aInBuffer = (uint8_t *)((uint32_t) _aInBuffer + DCODE_BUFFER_SIZE);
  74. }
  75. if (!Error)
  76. {
  77. /* Get image dimensions */
  78. if (R_JPEG_DecodeImageSizeGet(&g_jpeg0_ctrl, (uint16_t *) &xSize,
  79. (uint16_t *) &ySize) != FSP_SUCCESS)
  80. {
  81. Error = 4;
  82. }
  83. }
  84. if (!Error)
  85. {
  86. /* Get color space and check dimensions accordingly */
  87. R_JPEG_DecodePixelFormatGet(&g_jpeg0_ctrl, &ColorSpace);
  88. if (g_jpeg_status & JPEG_STATUS_ERROR)
  89. {
  90. /* Image dimensions incompatible with JPEG Codec */
  91. Error = 5;
  92. }
  93. }
  94. if (!Error)
  95. {
  96. /* Set up out buffer */
  97. // xSize * 16 * 2
  98. OutBufferSize = xSize * 16 * 2;
  99. pOutBuffer = (void *) decode->jpeg_out_buf;
  100. /* Set stride value */
  101. if (R_JPEG_DecodeHorizontalStrideSet(&g_jpeg0_ctrl, (uint32_t) xSize) != FSP_SUCCESS)
  102. {
  103. Error = 6;
  104. }
  105. }
  106. if (!Error)
  107. {
  108. /* Start decoding process by setting out-buffer */
  109. if (R_JPEG_OutputBufferSet(&g_jpeg0_ctrl, pOutBuffer, (uint32_t) OutBufferSize) != FSP_SUCCESS)
  110. {
  111. Error = 7;
  112. }
  113. }
  114. if (!Error)
  115. {
  116. while (!(g_jpeg_status & JPEG_STATUS_ERROR))
  117. {
  118. if (_WaitForCallbackTimed(JPEG_TIMEOUT) == RT_ETIMEOUT)
  119. {
  120. Error = 8;
  121. break;
  122. }
  123. if ((g_jpeg_status & JPEG_STATUS_OUTPUT_PAUSE) || (g_jpeg_status & JPEG_STATUS_OPERATION_COMPLETE))
  124. {
  125. /* Draw the JPEG work buffer to the framebuffer */
  126. R_JPEG_DecodeLinesDecodedGet(&g_jpeg0_ctrl, &LinesDecoded);
  127. _decode_read(decode, x, y, (void *) pOutBuffer, xSize, (int32_t)LinesDecoded);
  128. y = y + (int32_t)LinesDecoded;
  129. if (!(g_jpeg_status & JPEG_STATUS_OPERATION_COMPLETE))
  130. {
  131. /* Set the output buffer to the next 16-line block */
  132. if (R_JPEG_OutputBufferSet(&g_jpeg0_ctrl, pOutBuffer,
  133. (uint32_t) OutBufferSize) != FSP_SUCCESS)
  134. {
  135. Error = 10;
  136. break;
  137. }
  138. }
  139. }
  140. if (g_jpeg_status & JPEG_STATUS_INPUT_PAUSE)
  141. {
  142. /* Get next block of input data */
  143. /* Set the new input buffer pointer */
  144. if (R_JPEG_InputBufferSet(&g_jpeg0_ctrl, (void *) _aInBuffer,
  145. DCODE_BUFFER_SIZE) != FSP_SUCCESS)
  146. {
  147. Error = 9;
  148. break;
  149. }
  150. }
  151. if (g_jpeg_status & JPEG_STATUS_OPERATION_COMPLETE)
  152. {
  153. break;
  154. }
  155. }
  156. }
  157. if ((g_jpeg_status & JPEG_STATUS_ERROR) && (!Error))
  158. {
  159. Error = 1;
  160. }
  161. R_JPEG_Close(&g_jpeg0_ctrl);
  162. return Error;
  163. }
  164. int rt_hw_jpeg_init(void)
  165. {
  166. _SemaphoreJPEG = rt_sem_create("jpeg_sem", 0, RT_IPC_FLAG_PRIO);
  167. if (_SemaphoreJPEG == RT_NULL)
  168. {
  169. rt_kprintf("create jpeg decode semphr failed.\n");
  170. return RT_ERROR;
  171. }
  172. return RT_EOK;
  173. }
  174. INIT_DEVICE_EXPORT(rt_hw_jpeg_init);
  175. /*******************************************************************************************************************//**
  176. * @brief Decode callback function.
  177. * @param[in] p_args
  178. * @retval None
  179. **********************************************************************************************************************/
  180. void decode_callback(jpeg_callback_args_t *p_args)
  181. {
  182. rt_interrupt_enter();
  183. g_jpeg_status = p_args->status;
  184. rt_sem_release(_SemaphoreJPEG);
  185. rt_interrupt_leave();
  186. }
  187. #endif