animation.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "FreeRTOS.h"
  2. #include "task.h"
  3. #include "semphr.h"
  4. #include "board.h"
  5. #include "chip.h"
  6. #include "sfud.h"
  7. #include "ota_update.h"
  8. #include "mfcapi.h"
  9. #include "jpegdecapi.h"
  10. #include "animation.h"
  11. static TaskHandle_t animation_task = NULL;
  12. static SemaphoreHandle_t animation_mutex;
  13. #if ANIMATION_POLICY == ANIMATION_USE_SMALL_MEM
  14. static BANIHEADER ani_header;
  15. static BANIHEADER *aniheader = &ani_header;
  16. #else
  17. static BANIHEADER *aniheader;
  18. #endif
  19. static unsigned int anioffset;
  20. static unsigned int anisize;
  21. static int ani_playing = 0;
  22. static int ani_replay = 0;
  23. static int ani_stop = 0;
  24. static int ani_frames = 0;
  25. static int ani_display_index = 0;
  26. //static int ani_take_vdisbuf = 0;
  27. static unsigned int ani_frame_addr;
  28. static uint32_t first_show_done = 0;
  29. static void animation_thread(void *param)
  30. {
  31. MFCHandle *mfc_handle = NULL;
  32. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  33. sfud_flash *sflash;
  34. #endif
  35. uint32_t size;
  36. uint32_t stick, etick, delaytick;
  37. uint32_t display_addr;
  38. uint32_t *tmp_addr = NULL;
  39. anioffset = get_upfile_offset(UPFILE_TYPE_ANIMATION, 0);
  40. anisize = get_upfile_size(UPFILE_TYPE_ANIMATION);
  41. if (anisize > 0) {
  42. void *anibuf = pvPortMalloc(anisize);
  43. if (anibuf) {
  44. #if DEVICE_TYPE_SELECT != EMMC_FLASH
  45. sflash = sfud_get_device(0);
  46. sfud_read(sflash, anioffset, anisize, anibuf);
  47. #else
  48. emmc_read(anioffset, anisize, anibuf);
  49. #endif
  50. aniheader = (BANIHEADER*)anibuf;
  51. if (aniheader->magic != MKTAG('B', 'A', 'N', 'I')) {
  52. printf("Read animation file error!\n");
  53. vPortFree(anibuf);
  54. vTaskDelete(NULL);
  55. return;
  56. }
  57. CP15_clean_dcache_for_dma((uint32_t)anibuf, (uint32_t)anibuf + anisize);
  58. } else {
  59. printf("Error! No enough memory for animation file.\n");
  60. vTaskDelete(NULL);
  61. return;
  62. }
  63. } else {
  64. printf("Warning! Not found animation in update file.\n");
  65. vTaskDelete(NULL);
  66. return;
  67. }
  68. ani_frame_addr = (uint32_t)aniheader + sizeof(BANIHEADER);
  69. for (;;) {
  70. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  71. xSemaphoreTake(animation_mutex, portMAX_DELAY);
  72. if (ani_replay) {
  73. ani_frame_addr = (uint32_t)aniheader + sizeof(BANIHEADER);
  74. ani_frames = 0;
  75. ani_replay = 0;
  76. }
  77. stick = xTaskGetTickCount();
  78. ani_playing = 1;
  79. size = *(uint32_t*)ani_frame_addr;
  80. if (xVideoDisplayBufTake(pdMS_TO_TICKS(10)) == pdTRUE) {
  81. if(!mfc_handle)
  82. mfc_handle = mfc_init(RAW_STRM_TYPE_JPEG);
  83. if(!mfc_handle) {
  84. printf("%s, mfc_init failed.\n", __func__);
  85. continue ;
  86. }
  87. display_addr = ulVideoDisplayBufGet();
  88. JpegHeaderInfo jpegInfo = {0};
  89. jpegInfo.handle = mfc_handle;
  90. jpegInfo.jpg_addr = ani_frame_addr + 8;
  91. jpegInfo.jpg_size = size;
  92. jpegInfo.dec_addry = display_addr;
  93. jpegInfo.dec_size = ulVideoDisplayBufGetSize();
  94. #if LCD_ROTATE_ANGLE != LCD_ROTATE_ANGLE_0
  95. if(!tmp_addr) {
  96. tmp_addr = (u32 *)pvPortMalloc(jpegInfo.dec_size);
  97. if(!tmp_addr) {
  98. printf("tmp_addr pvPortMalloc failed.\n");
  99. }
  100. }
  101. if(tmp_addr) {
  102. jpegInfo.dec_addry = VIRT_TO_PHY((u32)tmp_addr);
  103. }
  104. #endif
  105. if (mfc_jpegdec(&jpegInfo) != 0) {
  106. printf("animation decode %d frame fail.\n", ani_frames);
  107. vVideoDisplayBufFree(display_addr);
  108. } else {
  109. int pxp_format = PXP_S0_FORMAT_YUV2P420;
  110. int lcd_format = LCD_OSD_FORAMT_Y_UV420;
  111. /* avoid warnning */
  112. (void) pxp_format;
  113. (void) lcd_format;
  114. /* YUV format only support y_uv420/y_u_v420/y_uv422/y_u_v422 */
  115. if (jpegInfo.dec_format == JPEGDEC_YCbCr420_SEMIPLANAR) {
  116. pxp_format = PXP_S0_FORMAT_YUV2P420;
  117. lcd_format = LCD_OSD_FORAMT_Y_UV420;
  118. } else if (jpegInfo.dec_format == JPEGDEC_YCbCr422_SEMIPLANAR) {
  119. pxp_format = PXP_S0_FORMAT_YUV2P422;
  120. lcd_format = LCD_OSD_FORAMT_Y_UV422;
  121. } else {
  122. printf("%s Invalid jpegdec format.\n", __func__);
  123. goto next_frame;
  124. }
  125. if(!first_show_done) {
  126. #if LCD_ROTATE_ANGLE == LCD_ROTATE_ANGLE_0
  127. int align_width = (LCD_WIDTH + 0xF) & (~0xF);
  128. int align_height = (LCD_HEIGHT + 0xF) & (~0xF);
  129. if((align_width == jpegInfo.dec_width) && (align_height == jpegInfo.dec_height)) {
  130. ark_lcd_set_osd_size(LCD_VIDEO_LAYER, LCD_WIDTH, LCD_HEIGHT);
  131. } else {
  132. ark_lcd_set_osd_size(LCD_VIDEO_LAYER, jpegInfo.dec_width, jpegInfo.dec_height);
  133. }
  134. ark_lcd_set_osd_format(LCD_VIDEO_LAYER, lcd_format);
  135. #else
  136. ark_lcd_set_osd_size(LCD_VIDEO_LAYER, LCD_WIDTH, LCD_HEIGHT);
  137. ark_lcd_set_osd_format(LCD_VIDEO_LAYER, LCD_OSD_FORAMT_RGB565);
  138. #endif
  139. ark_lcd_osd_enable(LCD_VIDEO_LAYER, 1);
  140. ark_lcd_osd_enable(LCD_UI_LAYER, 0);
  141. first_show_done = 1;
  142. }
  143. #if LCD_ROTATE_ANGLE == LCD_ROTATE_ANGLE_0
  144. ark_lcd_set_osd_yaddr(LCD_VIDEO_LAYER, jpegInfo.dec_addry);
  145. ark_lcd_set_osd_uaddr(LCD_VIDEO_LAYER, jpegInfo.dec_addru);
  146. ark_lcd_set_osd_vaddr(LCD_VIDEO_LAYER, jpegInfo.dec_addrv);
  147. #else
  148. pxp_scaler_rotate(jpegInfo.dec_addry, jpegInfo.dec_addru, jpegInfo.dec_addrv,
  149. pxp_format, jpegInfo.dec_width, jpegInfo.dec_height,
  150. display_addr, 0, PXP_OUT_FORMAT_RGB565, LCD_WIDTH, LCD_HEIGHT, LCD_ROTATE_ANGLE);
  151. ark_lcd_set_osd_yaddr(LCD_VIDEO_LAYER, display_addr);
  152. #endif
  153. ark_lcd_set_osd_sync(LCD_VIDEO_LAYER);
  154. ark_lcd_set_osd_sync(LCD_UI_LAYER);
  155. ark_lcd_wait_for_vsync();
  156. vVideoDisplayBufRender(display_addr);
  157. // ani_take_vdisbuf = 1;
  158. }
  159. }
  160. next_frame:
  161. ani_frame_addr += size;
  162. ani_display_index = !ani_display_index;
  163. if (ani_stop || (++ani_frames >= aniheader->aniCount + aniheader->hasBootlogo ? 1 : 0)
  164. #if ANIMATION_POLICY == ANIMATION_USE_SMALL_MEM
  165. || (ani_frame_addr > anioffset + anisize))
  166. #else
  167. || (ani_frame_addr > (uint32_t)aniheader + anisize))
  168. #endif
  169. {
  170. printf("animation end.\n");
  171. ani_stop = 0;
  172. if (aniheader->aniCount > 0)
  173. vTaskDelay(aniheader->aniDelayHideTime);
  174. else if (aniheader->hasBootlogo)
  175. vTaskDelay(aniheader->bootlogoDisplayTime);
  176. // if (ani_take_vdisbuf) {
  177. ark_lcd_osd_enable(LCD_VIDEO_LAYER, 0);
  178. ark_lcd_osd_enable(LCD_UI_LAYER, 1);
  179. ark_lcd_set_osd_sync(LCD_UI_LAYER);
  180. ark_lcd_set_osd_sync(LCD_VIDEO_LAYER);
  181. // }
  182. vVideoDisplayBufFree(display_addr);
  183. vVideoDisplayBufGive();
  184. ani_playing = 0;
  185. if (ani_replay)
  186. xTaskNotifyGive(animation_task);
  187. xSemaphoreGive(animation_mutex);
  188. if(mfc_handle) {
  189. mfc_uninit(mfc_handle);
  190. mfc_handle = NULL;
  191. }
  192. if(tmp_addr) {
  193. vPortFree(tmp_addr);
  194. tmp_addr = NULL;
  195. }
  196. continue;
  197. }
  198. vVideoDisplayBufGive();
  199. xSemaphoreGive(animation_mutex);
  200. etick = xTaskGetTickCount();
  201. if (aniheader->hasBootlogo && ani_frames == 1)
  202. delaytick = pdMS_TO_TICKS(aniheader->bootlogoDisplayTime) > (etick - stick) ?
  203. pdMS_TO_TICKS(aniheader->bootlogoDisplayTime) - (etick - stick) : 0;
  204. else
  205. delaytick = pdMS_TO_TICKS(1000 / aniheader->aniFps) > (etick - stick) ?
  206. pdMS_TO_TICKS(1000 / aniheader->aniFps) - (etick - stick) : 0;
  207. if (delaytick)
  208. vTaskDelay(delaytick);
  209. xTaskNotifyGive(animation_task);
  210. }
  211. }
  212. int animation_init(void)
  213. {
  214. if (animation_task) {
  215. printf("animation is already inited.\n");
  216. return 0;
  217. }
  218. animation_mutex = xSemaphoreCreateMutex();
  219. /* Create a task to play animation */
  220. if (xTaskCreate(animation_thread, "animation", configMINIMAL_STACK_SIZE, NULL,
  221. configMAX_PRIORITIES - 2, &animation_task) != pdPASS) {
  222. printf("create animation task fail.\n");
  223. animation_task = NULL;
  224. return -1;
  225. }
  226. return 0;
  227. }
  228. void animation_start(void)
  229. {
  230. xSemaphoreTake(animation_mutex, portMAX_DELAY);
  231. first_show_done=0;
  232. if (ani_playing) {
  233. printf("animation is already playing.\n");
  234. xSemaphoreGive(animation_mutex);
  235. return;
  236. }
  237. #if ANIMATION_POLICY == ANIMATION_USE_SMALL_MEM
  238. ani_frame_addr = anioffset + sizeof(BANIHEADER);
  239. #else
  240. ani_frame_addr = (uint32_t)aniheader + sizeof(BANIHEADER);
  241. #endif
  242. ani_frames = 0;
  243. if (animation_task)
  244. xTaskNotifyGive(animation_task);
  245. xSemaphoreGive(animation_mutex);
  246. }
  247. /* replay animation even if animation is already playing */
  248. void animation_restart(void)
  249. {
  250. xSemaphoreTake(animation_mutex, portMAX_DELAY);
  251. first_show_done=0;
  252. ani_stop=0;
  253. if (ani_playing) {
  254. ani_replay = 1;
  255. xSemaphoreGive(animation_mutex);
  256. return;
  257. }
  258. #if ANIMATION_POLICY == ANIMATION_USE_SMALL_MEM
  259. ani_frame_addr = anioffset + sizeof(BANIHEADER);
  260. #else
  261. ani_frame_addr = (uint32_t)aniheader + sizeof(BANIHEADER);
  262. #endif
  263. ani_frames = 0;
  264. if (animation_task)
  265. xTaskNotifyGive(animation_task);
  266. xSemaphoreGive(animation_mutex);
  267. }
  268. void animation_stop(void)
  269. {
  270. xSemaphoreTake(animation_mutex, portMAX_DELAY);
  271. ani_stop = 1;
  272. xSemaphoreGive(animation_mutex);
  273. }
  274. int get_animation_status(void)
  275. {
  276. return ani_playing;
  277. }