Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* IPU utils */
- #include <stdio.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <unistd.h>
- #include <string.h>
- #include <signal.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #include <sys/mman.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <linux/mxcfb.h>
- #include <linux/ipu.h>
- // helper functions
- static unsigned int fmt_to_bpp(unsigned int pixelformat);
- // input parameters
- static const char *input_filename = "5mp_img.rgba";
- static const int input_w = 2592;
- static const int input_h = 1944;
- static const char *output_filename = "output_resized.rgb";
- static const int output_w = 1296; // half resolution
- static const int output_h = 972;
- int main()
- {
- struct ipu_task task;
- FILE * file_in = NULL;
- FILE * file_out = NULL;
- int fd_ipu = 0; // IPU file descriptor
- int isize = 0; // input size
- int osize = 0; // output size
- void *inbuf = NULL;
- void *outbuf = NULL;
- int ret = 0;
- int total_cnt = 0;
- struct timeval begin, end;
- int sec, usec, run_time = 0;
- memset(&task, 0, sizeof(task));
- task.priority = 0;
- task.task_id = 0;
- task.timeout = 1000;
- task.input.width = input_w;
- task.input.height = input_h;
- task.input.format = v4l2_fourcc('R', 'G', 'B', 'A');
- task.output.width = output_w;
- task.output.height = output_h;
- task.output.format = v4l2_fourcc('R', 'G', 'B', '3');
- task.output.rotate = 0;
- // open input file for reading
- file_in = fopen(input_filename, "rb");
- if (file_in == NULL){
- printf("there is no such file for reading %s\n", input_filename);
- ret = -1;
- goto err0;
- }
- // open IPU device
- fd_ipu = open("/dev/mxc_ipu", O_RDWR, 0);
- if (fd_ipu < 0) {
- printf("open ipu dev fail\n");
- ret = -1;
- goto err1;
- }
- // calculate input size from image dimensions and bits-per-pixel according to format
- isize = task.input.paddr =
- task.input.width * task.input.height
- * fmt_to_bpp(task.input.format)/8;
- // allocate contingous physical memory for input image
- // input.paddr contains the amount needed
- // this value will be replaced with physical address on success
- ret = ioctl(fd_ipu, IPU_ALLOC, &task.input.paddr);
- if (ret < 0) {
- printf("ioctl IPU_ALLOC fail: (errno = %d)\n", errno);
- goto err2;
- }
- // create memory map; obtain the allocated memory virtual address
- inbuf = mmap(0, isize, PROT_READ | PROT_WRITE,
- MAP_SHARED, fd_ipu, task.input.paddr);
- if (!inbuf) {
- printf("mmap fail\n");
- ret = -1;
- goto err3;
- }
- // allocate memory for output image
- osize = task.output.paddr =
- task.output.width * task.output.height
- * fmt_to_bpp(task.output.format)/8;
- ret = ioctl(fd_ipu, IPU_ALLOC, &task.output.paddr);
- if (ret < 0) {
- printf("ioctl IPU_ALLOC fail\n");
- goto err4;
- }
- // create memory map for output image
- outbuf = mmap(0, osize, PROT_READ | PROT_WRITE,
- MAP_SHARED, fd_ipu, task.output.paddr);
- if (!outbuf) {
- printf("mmap fail\n");
- ret = -1;
- goto err5;
- }
- // open output file for writing
- file_out = fopen(output_filename, "wb");
- if (file_out == NULL) {
- printf("can not open output file %s\n", output_filename);
- ret = -1;
- goto err6;
- }
- ret = ioctl(fd_ipu, IPU_CHECK_TASK, &task);
- if (ret != IPU_CHECK_OK) {
- if (ret > IPU_CHECK_ERR_MIN) {
- ret = 0;
- printf("ipu task check fail\n");
- goto err7;
- }
- }
- // read input image
- ret = fread(inbuf, 1, isize, file_in);
- if (ret < isize) {
- ret = 0;
- printf("Can not read enough data from input file\n");
- goto err7;
- }
- for ( ; total_cnt < 1; total_cnt++) {
- gettimeofday(&begin, NULL);
- ret = ioctl(fd_ipu, IPU_QUEUE_TASK, &task);
- if (ret < 0) {
- printf("ioct IPU_QUEUE_TASK fail\n");
- goto err7;
- }
- gettimeofday(&end, NULL);
- sec = end.tv_sec - begin.tv_sec;
- usec = end.tv_usec - begin.tv_usec;
- if (usec < 0) {
- sec--;
- usec = usec + 1000000;
- }
- run_time += (sec * 1000000) + usec;
- }
- // write output image
- ret = fwrite(outbuf, 1, osize, file_out);
- if (ret < osize) {
- ret = -1;
- printf("Can not write enough data into output file\n");
- goto err7;
- }
- printf("total frame count %d avg frame time %d us, fps %f\n",
- total_cnt, run_time/total_cnt, total_cnt/(run_time/1000000.0));
- // Error handling
- err7:
- if (file_out)
- fclose(file_out);
- err6:
- if (outbuf)
- munmap(outbuf, osize);
- err5:
- if (task.output.paddr)
- ioctl(fd_ipu, IPU_FREE, &task.output.paddr);
- err4:
- if (inbuf)
- munmap(inbuf, isize);
- err3:
- if (task.input.paddr)
- ioctl(fd_ipu, IPU_FREE, &task.input.paddr);
- err2:
- if (fd_ipu)
- close(fd_ipu);
- err1:
- if (file_in)
- fclose(file_in);
- err0:
- return ret;
- }
- static unsigned int fmt_to_bpp(unsigned int pixelformat)
- {
- unsigned int bpp;
- switch (pixelformat)
- {
- case IPU_PIX_FMT_RGB565:
- /*interleaved 422*/
- case IPU_PIX_FMT_YUYV:
- case IPU_PIX_FMT_UYVY:
- /*non-interleaved 422*/
- case IPU_PIX_FMT_YUV422P:
- case IPU_PIX_FMT_YVU422P:
- bpp = 16;
- break;
- case IPU_PIX_FMT_BGR24:
- case IPU_PIX_FMT_RGB24:
- case IPU_PIX_FMT_YUV444:
- case IPU_PIX_FMT_YUV444P:
- bpp = 24;
- break;
- case IPU_PIX_FMT_BGR32:
- case IPU_PIX_FMT_BGRA32:
- case IPU_PIX_FMT_RGB32:
- case IPU_PIX_FMT_RGBA32:
- case IPU_PIX_FMT_ABGR32:
- bpp = 32;
- break;
- /*non-interleaved 420*/
- case IPU_PIX_FMT_YUV420P:
- case IPU_PIX_FMT_YVU420P:
- case IPU_PIX_FMT_YUV420P2:
- case IPU_PIX_FMT_NV12:
- case IPU_PIX_FMT_TILED_NV12:
- bpp = 12;
- break;
- default:
- bpp = 8;
- break;
- }
- return bpp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement