frame_demo1.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "camera_demo1"
  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. static const char *img_name = "../resource/yuv420_1280x720_csky2016.yuv";
  27. static csi_img_format_t img_format = {
  28. .width = 1280,
  29. .height = 720,
  30. .pix_fmt = CSI_PIX_FMT_I420,
  31. };
  32. int main(int argc, char *argv[])
  33. {
  34. int ret = -1;
  35. int i;
  36. int img_fd;
  37. long img_size;
  38. int ion_fd;
  39. int dmabuf_fd;
  40. void *dmabuf_addr;
  41. csi_frame_s camera_frame;
  42. csi_camera_meta_s *camera_meta;
  43. // get image file size
  44. if (fdc_get_img_file_size(&img_size, img_name) != 0)
  45. goto LAB_ERR;
  46. // open ion device
  47. if (fdc_get_ion_device_fd(&ion_fd) != 0)
  48. goto LAB_ERR;
  49. // alloc dma-buf from ion device
  50. struct ion_allocation_data alloc_data;
  51. if (fdc_alloc_ion_buf(&alloc_data, ion_fd, img_size) != 0)
  52. goto LAB_ERR_ION_OPENED;
  53. dmabuf_fd = alloc_data.fd;
  54. // map dma-buf to user addr
  55. if (fdc_mmap_dmabuf_addr(&dmabuf_addr, img_size, dmabuf_fd) != 0)
  56. goto LAB_ERR_BUF_ALLOCED;
  57. // open image file
  58. img_fd = open(img_name, O_RDONLY);
  59. if (img_fd < 0) {
  60. LOG_E("Open image file '%s' failed, %s\n",
  61. img_name, strerror(errno));
  62. goto LAB_ERR_MMAPPED;
  63. }
  64. // store image into dma-buf
  65. if (fdc_store_img_to_dmabuf(dmabuf_addr, img_fd, img_size) != 0)
  66. goto LAB_ERR_IMG_OPENED;
  67. // create csi_frame
  68. if (fdc_create_csi_frame(&camera_frame, &img_format, &camera_meta, img_size, dmabuf_fd,
  69. dmabuf_addr) != 0)
  70. goto LAB_ERR_IMG_OPENED;
  71. // dump camera meta from csi frame
  72. fdc_dump_camera_meta((csi_camera_meta_s *)camera_frame.meta.data);
  73. // destory csi_frame
  74. fdc_destory_csi_frame(&camera_frame);
  75. ret = 0;
  76. LAB_ERR_IMG_OPENED:
  77. close(img_fd);
  78. LAB_ERR_MMAPPED:
  79. munmap(dmabuf_addr, img_size);
  80. LAB_ERR_BUF_ALLOCED:
  81. close(dmabuf_fd);
  82. LAB_ERR_ION_OPENED:
  83. close(ion_fd);
  84. LAB_ERR:
  85. return ret;
  86. }