Advertisement
Guest User

i.MX IPU issue

a guest
Dec 4th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.33 KB | None | 0 0
  1. /* IPU utils */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #include <errno.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/mman.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <linux/mxcfb.h>
  16.  
  17. #include <linux/ipu.h>
  18.  
  19.  
  20. // helper functions
  21. static unsigned int fmt_to_bpp(unsigned int pixelformat);
  22.  
  23.  
  24. // input parameters
  25. static const char *input_filename = "5mp_img.rgba";
  26. static const int input_w        = 2592;
  27. static const int input_h        = 1944;
  28.  
  29. static const char *output_filename = "output_resized.rgb";
  30. static const int output_w       = 1296;     // half resolution
  31. static const int output_h       = 972;
  32.  
  33. int main()
  34. {
  35.   struct ipu_task task;
  36.  
  37.   FILE * file_in = NULL;
  38.   FILE * file_out = NULL;
  39.  
  40.   int fd_ipu = 0;   // IPU file descriptor
  41.   int isize = 0;    // input size
  42.   int osize = 0;    // output size
  43.  
  44.   void *inbuf = NULL;
  45.   void *outbuf = NULL;
  46.  
  47.   int ret = 0;
  48.   int total_cnt = 0;
  49.  
  50.   struct timeval begin, end;
  51.   int sec, usec, run_time = 0;
  52.  
  53.  
  54.  
  55.   memset(&task, 0, sizeof(task));
  56.  
  57.   task.priority = 0;
  58.   task.task_id  = 0;
  59.   task.timeout  = 1000;
  60.  
  61.   task.input.width    = input_w;
  62.   task.input.height   = input_h;
  63.   task.input.format   = v4l2_fourcc('R', 'G', 'B', 'A');
  64.  
  65.   task.output.width   = output_w;
  66.   task.output.height  = output_h;
  67.   task.output.format  = v4l2_fourcc('R', 'G', 'B', '3');
  68.   task.output.rotate  = 0;
  69.  
  70.  // open input file for reading
  71.   file_in = fopen(input_filename, "rb");
  72.   if (file_in == NULL){
  73.     printf("there is no such file for reading %s\n", input_filename);
  74.     ret = -1;
  75.     goto err0;
  76.   }
  77.  
  78.   // open IPU device
  79.   fd_ipu = open("/dev/mxc_ipu", O_RDWR, 0);
  80.   if (fd_ipu < 0) {
  81.     printf("open ipu dev fail\n");
  82.     ret = -1;
  83.     goto err1;
  84.   }
  85.  
  86.   // calculate input size from image dimensions and bits-per-pixel according to format
  87.   isize = task.input.paddr =
  88.     task.input.width * task.input.height
  89.     * fmt_to_bpp(task.input.format)/8;
  90.  
  91.  
  92.   // allocate contingous physical memory for input image
  93.   // input.paddr contains the amount needed
  94.   // this value will be replaced with physical address on success
  95.   ret = ioctl(fd_ipu, IPU_ALLOC, &task.input.paddr);
  96.   if (ret < 0) {
  97.     printf("ioctl IPU_ALLOC fail: (errno = %d)\n", errno);
  98.     goto err2;
  99.   }
  100.  
  101.   // create memory map; obtain the allocated memory virtual address
  102.   inbuf = mmap(0, isize, PROT_READ | PROT_WRITE,
  103.     MAP_SHARED, fd_ipu, task.input.paddr);
  104.   if (!inbuf) {
  105.     printf("mmap fail\n");
  106.     ret = -1;
  107.     goto err3;
  108.   }
  109.  
  110.  
  111.   // allocate memory for output image
  112.   osize = task.output.paddr =
  113.     task.output.width * task.output.height
  114.     * fmt_to_bpp(task.output.format)/8;
  115.   ret = ioctl(fd_ipu, IPU_ALLOC, &task.output.paddr);
  116.   if (ret < 0) {
  117.     printf("ioctl IPU_ALLOC fail\n");
  118.     goto err4;
  119.   }
  120.  
  121.   // create memory map for output image
  122.   outbuf = mmap(0, osize, PROT_READ | PROT_WRITE,
  123.       MAP_SHARED, fd_ipu, task.output.paddr);
  124.   if (!outbuf) {
  125.     printf("mmap fail\n");
  126.     ret = -1;
  127.     goto err5;
  128.   }
  129.  
  130.   // open output file for writing
  131.   file_out = fopen(output_filename, "wb");
  132.   if (file_out == NULL) {
  133.     printf("can not open output file %s\n", output_filename);
  134.     ret = -1;
  135.     goto err6;
  136.   }
  137.  
  138.   ret = ioctl(fd_ipu, IPU_CHECK_TASK, &task);
  139.   if (ret != IPU_CHECK_OK) {
  140.     if (ret > IPU_CHECK_ERR_MIN) {
  141.       ret = 0;
  142.       printf("ipu task check fail\n");
  143.       goto err7;
  144.     }
  145.   }
  146.  
  147.  
  148.   // read input image
  149.   ret = fread(inbuf, 1, isize, file_in);
  150.   if (ret < isize) {
  151.     ret = 0;
  152.     printf("Can not read enough data from input file\n");
  153.     goto err7;
  154.   }
  155.  
  156.  
  157.  
  158.   for ( ; total_cnt < 1; total_cnt++) {
  159.  
  160.     gettimeofday(&begin, NULL);
  161.  
  162.  
  163.  
  164.  
  165.     ret = ioctl(fd_ipu, IPU_QUEUE_TASK, &task);
  166.     if (ret < 0) {
  167.       printf("ioct IPU_QUEUE_TASK fail\n");
  168.       goto err7;
  169.     }
  170.  
  171.  
  172.     gettimeofday(&end, NULL);
  173.     sec = end.tv_sec - begin.tv_sec;
  174.     usec = end.tv_usec - begin.tv_usec;
  175.     if (usec < 0) {
  176.       sec--;
  177.       usec = usec + 1000000;
  178.     }
  179.     run_time += (sec * 1000000) + usec;
  180.  
  181.   }
  182.  
  183.   // write output image
  184.   ret = fwrite(outbuf, 1, osize, file_out);
  185.   if (ret < osize) {
  186.     ret = -1;
  187.     printf("Can not write enough data into output file\n");
  188.     goto err7;
  189.   }
  190.  
  191.   printf("total frame count %d avg frame time %d us, fps %f\n",
  192.       total_cnt, run_time/total_cnt, total_cnt/(run_time/1000000.0));
  193.  
  194. // Error handling
  195.   err7:
  196.     if (file_out)
  197.       fclose(file_out);
  198.   err6:
  199.     if (outbuf)
  200.       munmap(outbuf, osize);
  201.   err5:
  202.     if (task.output.paddr)
  203.       ioctl(fd_ipu, IPU_FREE, &task.output.paddr);
  204.   err4:
  205.     if (inbuf)
  206.       munmap(inbuf, isize);
  207.   err3:
  208.     if (task.input.paddr)
  209.       ioctl(fd_ipu, IPU_FREE, &task.input.paddr);
  210.   err2:
  211.     if (fd_ipu)
  212.       close(fd_ipu);
  213.   err1:
  214.     if (file_in)
  215.       fclose(file_in);
  216.   err0:
  217.     return ret;
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225. static unsigned int fmt_to_bpp(unsigned int pixelformat)
  226. {
  227.         unsigned int bpp;
  228.  
  229.         switch (pixelformat)
  230.         {
  231.                 case IPU_PIX_FMT_RGB565:
  232.                 /*interleaved 422*/
  233.                 case IPU_PIX_FMT_YUYV:
  234.                 case IPU_PIX_FMT_UYVY:
  235.                 /*non-interleaved 422*/
  236.                 case IPU_PIX_FMT_YUV422P:
  237.                 case IPU_PIX_FMT_YVU422P:
  238.                         bpp = 16;
  239.                         break;
  240.                 case IPU_PIX_FMT_BGR24:
  241.                 case IPU_PIX_FMT_RGB24:
  242.                 case IPU_PIX_FMT_YUV444:
  243.                 case IPU_PIX_FMT_YUV444P:
  244.                         bpp = 24;
  245.                         break;
  246.                 case IPU_PIX_FMT_BGR32:
  247.                 case IPU_PIX_FMT_BGRA32:
  248.                 case IPU_PIX_FMT_RGB32:
  249.                 case IPU_PIX_FMT_RGBA32:
  250.                 case IPU_PIX_FMT_ABGR32:
  251.                         bpp = 32;
  252.                         break;
  253.                 /*non-interleaved 420*/
  254.                 case IPU_PIX_FMT_YUV420P:
  255.                 case IPU_PIX_FMT_YVU420P:
  256.                 case IPU_PIX_FMT_YUV420P2:
  257.                 case IPU_PIX_FMT_NV12:
  258.     case IPU_PIX_FMT_TILED_NV12:
  259.                         bpp = 12;
  260.                         break;
  261.                 default:
  262.                         bpp = 8;
  263.                         break;
  264.         }
  265.         return bpp;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement