frame_demo_common.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <sys/stat.h>
  16. #include <sys/ioctl.h>
  17. #include <sys/mman.h>
  18. #include <sys/time.h>
  19. #define LOG_LEVEL 2
  20. //#define LOG_PREFIX "demo_common"
  21. #include <syslog.h>
  22. #include <ion.h>
  23. #include <csi_frame.h>
  24. #include <csi_camera_frame.h>
  25. #include "frame_demo_common.h"
  26. #define PAGE_SIZE 4096
  27. //#define IMG_YUV420_FILENAME "../resource/yuv420_1280x720_csky2016.yuv"
  28. #define IMG_RESOLUTION_WIDTH 1280
  29. #define IMG_RESOLUTION_HEIGHT 720
  30. /* fdc = frame demo common */
  31. int fdc_get_img_file_size(long *img_size, const char *filename)
  32. {
  33. struct stat img_stat;
  34. if (stat(filename, &img_stat) != 0) {
  35. LOG_E("stat file '%s' failed, %s\n", filename, strerror(errno));
  36. return -1;
  37. }
  38. *img_size = img_stat.st_size;
  39. LOG_O("Get image '%s' size=%ld\n", filename, *img_size);
  40. return 0;
  41. }
  42. int fdc_get_ion_device_fd(int *ion_fd)
  43. {
  44. *ion_fd = open(ION_DEVICE_NAME, O_RDWR);
  45. if (*ion_fd < 0) {
  46. LOG_E("Open ion device:'%s' failed, %s\n", ION_DEVICE_NAME, strerror(errno));
  47. return -1;
  48. }
  49. LOG_O("Open ion device OK, ion_fd=%d\n", *ion_fd);
  50. return 0;
  51. }
  52. int fdc_alloc_ion_buf(struct ion_allocation_data *alloc_data,
  53. int ion_fd, unsigned long img_size)
  54. {
  55. alloc_data->len = img_size;
  56. if (ioctl(ion_fd, ION_IOC_ALLOC, alloc_data) ||
  57. alloc_data->fd < 0) {
  58. LOG_E("ion ioctl failed, %s\n", strerror(errno));
  59. return -1;
  60. }
  61. int dmabuf_fd = alloc_data->fd;
  62. LOG_O("Alloc %ld from ion device OK, dmabuf_fd=%d\n", img_size, dmabuf_fd);
  63. return 0;
  64. }
  65. int fdc_mmap_dmabuf_addr(void **addr, unsigned long length, int dmabuf_fd)
  66. {
  67. LOG_D("Enter, length=%zd, dmabuf_fd=%d\n", length, dmabuf_fd);
  68. *addr = mmap(NULL, length, PROT_READ | PROT_WRITE,
  69. MAP_SHARED, dmabuf_fd, 0);
  70. if (*addr == NULL || *addr == MAP_FAILED) {
  71. LOG_E("Map dmabuf failed\n");
  72. return -1;
  73. }
  74. LOG_O("mmap dmabuf to user addr: %p OK\n", *addr);
  75. return 0;
  76. }
  77. int fdc_store_img_to_dmabuf(void *dmabuf_addr, int img_fd, long img_size)
  78. {
  79. long totle_len=0, read_len = 0;
  80. char *img_ptr = (char*)dmabuf_addr;
  81. while ((read_len = read(img_fd, (img_ptr + totle_len), PAGE_SIZE)) > 0) {
  82. totle_len += read_len;
  83. }
  84. if (read_len < 0) {
  85. LOG_E("Read from image file failed, %s\n", strerror(errno));
  86. return -1;
  87. }
  88. if (totle_len != img_size) {
  89. LOG_E("Read total len(%ld) != stat size(%ld)\n", totle_len, img_size);
  90. return -1;
  91. }
  92. LOG_O("Store image into dma-buf OK\n");
  93. return 0;
  94. }
  95. int fdc_create_csi_frame(csi_frame_s *camera_frame,
  96. csi_img_format_t *img_format, csi_camera_meta_s **camera_meta,
  97. long img_size, int dmabuf_fd, void *dmabuf_addr)
  98. {
  99. // fill csi_frame's img info
  100. camera_frame->img.type = CSI_IMG_TYPE_DMA_BUF;
  101. camera_frame->img.size = img_size;
  102. camera_frame->img.width = img_format->width;
  103. camera_frame->img.height = img_format->height;
  104. camera_frame->img.pix_format = img_format->pix_fmt;
  105. camera_frame->img.num_planes = 1; // Now use only 1 plane data
  106. camera_frame->img.fds[0] = dmabuf_fd;
  107. camera_frame->img.strides[0] = img_format->width;
  108. camera_frame->img.offsets[0] = 0;
  109. camera_frame->img.fds[1] = -1;
  110. camera_frame->img.strides[1] = 0;
  111. camera_frame->img.offsets[1] = 0;
  112. camera_frame->img.fds[2] = -1;
  113. camera_frame->img.strides[2] = 0;
  114. camera_frame->img.offsets[2] = 0;
  115. camera_frame->img.modifier = 0;
  116. // prepare csi_camera_frame meta info
  117. int meta_unit_count = 5;
  118. size_t meta_data_size;
  119. csi_camera_frame_alloc_meta(camera_meta, meta_unit_count, &meta_data_size);
  120. (*camera_meta)->units[0].id = CSI_CAMERA_META_ID_CAMERA_NAME;
  121. (*camera_meta)->units[0].type = CSI_META_VALUE_TYPE_STR;
  122. strncpy((*camera_meta)->units[0].str_value, "/dev/video0",
  123. sizeof((*camera_meta)->units[0].str_value));
  124. (*camera_meta)->units[1].id = CSI_CAMERA_META_ID_CHANNEL_ID;
  125. (*camera_meta)->units[1].type = CSI_META_VALUE_TYPE_UINT;
  126. (*camera_meta)->units[1].uint_value = 1;
  127. (*camera_meta)->units[2].id = CSI_CAMERA_META_ID_FRAME_ID;
  128. (*camera_meta)->units[2].type = CSI_META_VALUE_TYPE_INT;
  129. (*camera_meta)->units[2].uint_value = 2;
  130. (*camera_meta)->units[3].id = CSI_CAMERA_META_ID_TIMESTAMP;
  131. (*camera_meta)->units[3].type = CSI_META_VALUE_TYPE_TIMEVAL;
  132. gettimeofday(&(*camera_meta)->units[3].time_value, NULL);
  133. (*camera_meta)->units[4].id = CSI_CAMERA_META_ID_HDR;
  134. (*camera_meta)->units[4].type = CSI_META_VALUE_TYPE_TIMEVAL;
  135. (*camera_meta)->units[4].bool_value = false;
  136. camera_frame->meta.type = CSI_META_TYPE_CAMERA;
  137. camera_frame->meta.size = meta_data_size;
  138. camera_frame->meta.data = *camera_meta;
  139. LOG_D("*camera_meta=camera_frame->meta.data=%p\n", camera_frame->meta.data);
  140. return 0;
  141. }
  142. void fdc_dump_camera_meta(csi_camera_meta_s *camera_meta)
  143. {
  144. int ret;
  145. csi_camrea_meta_unit_s meta_unit;
  146. ret = csi_camera_frame_get_meta_unit(&meta_unit, camera_meta, CSI_CAMERA_META_ID_TIMESTAMP);
  147. if (ret == 0) {
  148. struct timeval tv = meta_unit.time_value;
  149. struct tm *t = localtime(&tv.tv_sec);
  150. if (t != NULL) {
  151. LOG_O("Get timestamp from csi frame:%d-%d-%d %d:%d:%d.%ld\n",
  152. 1900+t->tm_year, 1+t->tm_mon, t->tm_mday,
  153. t->tm_hour, t->tm_min, t->tm_sec, tv.tv_usec);
  154. return;
  155. } else {
  156. LOG_E("timestamp is invalid\n");
  157. return;
  158. }
  159. }
  160. return;
  161. }
  162. void fdc_destory_csi_frame(csi_frame_s *camera_frame)
  163. {
  164. if (camera_frame->meta.data != NULL)
  165. csi_camera_frame_free_meta((csi_camera_meta_s*)camera_frame->meta.data);
  166. }
  167. void fdc_dump_msghdr(struct msghdr *msg)
  168. {
  169. int i;
  170. if (msg == NULL) {
  171. LOG_E("mgs = NULL\n");
  172. return;
  173. }
  174. LOG_D("struct msghdr *msg(%p) = {\n", msg);
  175. LOG_D("\t .msg_iov=%p\n", msg->msg_iov);
  176. LOG_D("\t .msg_iovlen=%zd\n", msg->msg_iovlen);
  177. for(i = 0; i < msg->msg_iovlen; i++) {
  178. struct iovec *iovc = &(msg->msg_iov[i]);
  179. LOG_D("\t .msg_iov[%d]={.iov_base=%p, .iov_len=%zd}\n",
  180. i, msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len);
  181. }
  182. LOG_D("\t .msg_control=%p\n", msg->msg_control);
  183. LOG_D("\t .msg_control=%zd\n", msg->msg_controllen);
  184. LOG_D("\t .name=%p\n", msg->msg_name);
  185. LOG_D("\t .msg_namelen=%d\n", msg->msg_namelen);
  186. LOG_D("\t .msg_flags=%d\n", msg->msg_flags);
  187. LOG_D("}\n");
  188. }