最近在调试S5PV210上的camera驱动,因为对Android的samsung camera hal 不太了解,自己写了个测试程序方便调试。
因为camera驱动都是遵守V4L2标准,所以测试程序是通用的,在MX51平台也能工作。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <getopt.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/time.h> #include <sys/mman.h> #include <sys/ioctl.h> #include <asm/types.h> #include <linux/videodev2.h> #include <linux/fb.h> #define CLEAR(x) memset (&(x), 0, sizeof (x)) int PAL_WIDTH = 720; int PAL_HEIGHT = 576; enum { RGB565, RGB888, RGB32, }; struct buffer { void *start; size_t length; }; static char *dev_name = NULL; static int fd = -1; static unsigned int n_buffers = 0; static int g_width; static int g_height; static unsigned g_pixelformat; static struct fb_var_screeninfo vinfo; static struct fb_fix_screeninfo finfo; struct buffer *buffers = NULL; static int file_index = 1; static int g_count = 9; static void errno_exit (const char * s) { fprintf (stderr, "%s error %d, %s\n",s, errno, strerror (errno)); exit (EXIT_FAILURE); } static int xioctl (int fd,int request,void * arg) { int r; do r = ioctl (fd, request, arg); while (-1 == r && EINTR == errno); return r; } static void write_yuv(const void *p, int index) { int write_fd = 0; int ret; int length; char tmp[20]; sprintf(tmp, "/data/file/yuvfile%d", index); write_fd = open(tmp, O_RDWR|O_CREAT); if (write_fd == -1) { printf("%s: open yuvfile failed, errno(%d)\n", __func__, errno); } length = g_width * g_height * 2; ret = write(write_fd, p, length); if (ret != length) { printf("%s: write yuvfile failed, ret(%d), errno(%d)\n", __func__, ret, errno); } } static void process_image(const void *p) { if (file_index < g_count) { write_yuv(p, file_index); file_index = file_index + 1; } } static int read_frame(void) { int ret = 0; struct timeval timer; struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)) { switch (errno) { case EAGAIN: printf("VIDIOC_DQBUF return EAGAIN\n"); return 0; default: printf("VIDIOC_DQBUF failed, errno(%d):\n", errno, strerror(errno)); ret = -errno; return ret; } } assert (buf.index < n_buffers); printf("%s: buf.bytesused(%d), buf.index(%d)\n", __func__, buf.index); process_image(buffers[buf.index].start); if (-1 == xioctl(fd, VIDIOC_QBUF, &buf)) { printf("VIDIOC_BUF failed, errno(%d)\n", errno); ret = -errno; } gettimeofday(&timer, NULL); printf("(%d)th capture, tv_sec(%d), tv_usec\n", file_index, timer.tv_sec, timer.tv_usec); return ret; } static void run(void) { int ret; printf("%s: enter\n", __func__); while (1) { fd_set fds; struct timeval tv; FD_ZERO (&fds); FD_SET(fd, &fds); if (file_index == g_count) { break; } tv.tv_sec = 5; tv.tv_usec = 0; ret = select(fd + 1, &fds, NULL, NULL, &tv); if (-1 == ret) { if (EINTR == errno) { printf("errno == EINTR\n"); continue; } errno_exit("select"); } if (0 == ret) { printf("select timeout\n"); exit(EXIT_FAILURE); } if (read_frame()) continue; sleep(1); } printf("%s: end run\n", __func__); } static void stop_capturing (void) { enum v4l2_buf_type type; printf("%s: enter\n", __func__); type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type)) errno_exit ("VIDIOC_STREAMOFF"); } static void start_capturing(void) { unsigned int i; enum v4l2_buf_type type; printf("%s: enter\n", __func__); for (i = 0; i < n_buffers; ++i) { struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = i; if (-1 == xioctl (fd, VIDIOC_QBUF, &buf)) errno_exit ("VIDIOC_QBUF"); } type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (-1 == xioctl (fd, VIDIOC_STREAMON, &type)) errno_exit ("VIDIOC_STREAMON"); } static void uninit_device (void) { unsigned int i; for (i = 0; i < n_buffers; ++i) if (-1 == munmap(buffers[i].start, buffers[i].length)) errno_exit ("munmap"); free (buffers); } static void init_mmap (void) { struct v4l2_requestbuffers req; CLEAR (req); req.count = 4; req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; req.memory = V4L2_MEMORY_MMAP; if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) { if (EINVAL == errno) { fprintf (stderr, "%s does not support memory mapping\n", dev_name); exit (EXIT_FAILURE); } else { errno_exit ("VIDIOC_REQBUFS"); } } if (req.count < 2) { //if (req.count < 2) fprintf (stderr, "Insufficient buffer memory on %s\n",dev_name); exit (EXIT_FAILURE); } buffers = calloc(req.count, sizeof(*buffers)); if (!buffers) { fprintf (stderr, "Out of memory\n"); exit (EXIT_FAILURE); } printf("request buffer count(%d)\n", req.count); for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { struct v4l2_buffer buf; CLEAR (buf); buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; buf.memory = V4L2_MEMORY_MMAP; buf.index = n_buffers; if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf)) errno_exit ("VIDIOC_QUERYBUF"); buffers[n_buffers].length = buf.length; buffers[n_buffers].start = mmap(NULL, buf.length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, buf.m.offset); printf("buf.start(%p), buf.length(%d)\n", buffers[n_buffers].start, buf.length); if (MAP_FAILED == buffers[n_buffers].start) errno_exit ("mmap"); } } static void init_device (void) { struct v4l2_capability cap; struct v4l2_cropcap cropcap; struct v4l2_crop crop; struct v4l2_format fmt; if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap)) { printf("%s: VIDIOC_QUERYCAP\n", __func__, errno); exit (EXIT_FAILURE); } printf("support overlay cap.capabilities(0x%x)\n", cap.capabilities); if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { fprintf (stderr, "%s is no video capture device\n",dev_name); exit (EXIT_FAILURE); } if (!(cap.capabilities & V4L2_CAP_STREAMING)) { fprintf (stderr, "%s does not support streaming i/o\n",dev_name); exit (EXIT_FAILURE); } CLEAR (cropcap); cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) { crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; crop.c = cropcap.defrect; if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) { printf("VIDIOC_S_CROP failed(%d), %s\n", errno, strerror(errno)); } } CLEAR (fmt); fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; fmt.fmt.pix.width = PAL_WIDTH; fmt.fmt.pix.height = PAL_HEIGHT; fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; fmt.fmt.pix.field = V4L2_FIELD_NONE; if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt)) { errno_exit ("VIDIOC_S_FMT"); } printf("width(%d), height(%d), pixelformat(0x%x), field(%d)\n", fmt.fmt.pix.width, fmt.fmt.pix.height, fmt.fmt.pix.pixelformat, fmt.fmt.pix.field); g_width = fmt.fmt.pix.width; g_height = fmt.fmt.pix.height; init_mmap (); } static void close_device(void) { close(fd); } static void open_device(void) { struct stat st; fd = open(dev_name, O_RDWR| O_NONBLOCK, 0); if (-1 == fd) { fprintf (stderr, "Cannot open '%s': %d, %s\n", dev_name, errno, strerror (errno)); exit (EXIT_FAILURE); } } static void usage (FILE * fp,int argc,char ** argv) { fprintf (fp, "Usage: %s [options]\n\n" "Options:\n" "-d | --device name Video device name [/dev/video]\n" "-h | --help Print this message\n" "-t | --how long will display in seconds\n" "", argv[0]); } static const char short_options [] = "d:w:h:f:p:c:"; static const struct option long_options [] = { { "device", required_argument, NULL, 'd' }, { "help", no_argument, NULL, 'h' }, { "time", no_argument, NULL, 't' }, { 0, 0, 0, 0 } }; int select_input(int input) { int ret; ret = xioctl(fd, VIDIOC_S_INPUT, &input); if (ret) { printf("xioctl VIDIOC_S_INPUT failed errno(%d)\n", errno); } return ret; } int main (int argc,char ** argv) { dev_name = "/dev/video0"; for (;;) { int index; int c; int tmp; c = getopt_long(argc, argv, short_options, long_options, &index); if (-1 == c) break; switch (c) { case 0: break; case 'd': dev_name = optarg; break; case 'c': g_count = strtol(optarg, NULL, 10); break; case 'p': tmp = strtol(optarg, NULL, 10); if (tmp == 0) { g_pixelformat = V4L2_PIX_FMT_UYVY; } else if (tmp == 1) { g_pixelformat = V4L2_PIX_FMT_YUYV; } break; case 'w': PAL_WIDTH = strtol(optarg, NULL, 10); printf("PAL_WIDTH(%d)\n", PAL_WIDTH); break; case 'h': PAL_HEIGHT = strtol(optarg, NULL, 10); printf("PAL_HEIGHT(%d)\n", PAL_HEIGHT); break; default: usage (stderr, argc, argv); exit (EXIT_FAILURE); } } open_device(); select_input(0); init_device(); start_capturing (); run(); stop_capturing(); uninit_device(); close_device(); return 0; }
转载自:http://blog.csdn.net/kickxxx/article/details/7714880