capture.h

00001 /*
00002  *  V4L2 video capture example
00003  *
00004  *  This program can be used and distributed without restrictions.
00005  */
00006 
00007 #ifndef CAPTURE_H
00008 #define CAPTURE_H
00009 
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <assert.h>
00014 
00015 #include <getopt.h>             /* getopt_long() */
00016 
00017 #include <fcntl.h>              /* low-level i/o */
00018 #include <unistd.h>
00019 #include <errno.h>
00020 #include <sys/stat.h>
00021 #include <sys/types.h>
00022 #include <sys/time.h>
00023 #include <sys/mman.h>
00024 #include <sys/ioctl.h>
00025 
00026 #include <asm/types.h>          /* for videodev2.h */
00027 
00028 #include <linux/videodev.h>
00029 #include <linux/videodev2.h>
00030 
00031 #include <sys/resource.h>
00032 #include <sys/time.h>
00033 
00034 #define CLEAR(x) memset (&(x), 0, sizeof (x))
00035 
00036 typedef enum {
00037         IO_METHOD_READ,
00038         IO_METHOD_MMAP,
00039         IO_METHOD_USERPTR,
00040 } io_method;
00041 
00042 struct buffer {
00043         void *                  start;
00044         size_t                  length;
00045 };
00046 
00047 struct frame {
00048         unsigned char*       data;
00049         unsigned char*       lumData;
00050         int         width;
00051         int         height;
00052         int         pixType;
00053 };
00054 
00055 
00056 // Note, the canonical VideoFormat has diverged from this copy, so the
00057 // enum values will no longer be in correspondence
00058 enum IrobotVideoFormat
00059   {
00060     IROBOT_VIDFMT_GREY    = 0,  // format: [ grey(8) ]
00061     IROBOT_VIDFMT_RAW     = 1,
00062     IROBOT_VIDFMT_RGB555  = 2,  // format: [ (1) r(5) g(5) b(5) ]
00063     IROBOT_VIDFMT_RGB565  = 3,  // format: [ r(5) g(6) b(5) ]
00064     IROBOT_VIDFMT_RGB24   = 4,  // format: [ r(8) g(8) b(8) ]
00065     IROBOT_VIDFMT_RGB32   = 5,  // format: [ r(8) g(8) b(8) ]
00066     IROBOT_VIDFMT_YUYV    = 6,  // format: [ Y0(8) U0(8) Y1(8) V0(8) ]
00067     IROBOT_VIDFMT_UYVY    = 7,  // format: [ U0(8) Y0(8) V0(8) Y1(8) ]
00068     IROBOT_VIDFMT_YUV444  = 8,  // format: [ U0(8) Y0(8) V0(8) U1(8) Y1(8) V1(8) ]
00069     IROBOT_VIDFMT_YUV422  = 9,  // format: same as IROBOT_VIDFMT_UYVY
00070     IROBOT_VIDFMT_YUV411  = 10, // format: [ U(8) Y0(8) Y1(8) V(8) Y2(8) Y3(8) ]
00071     IROBOT_VIDFMT_YUV420  = 11, // does this exist?
00072     IROBOT_VIDFMT_YUV410  = 12, // does this exist?
00073     IROBOT_VIDFMT_YUV444P = 13,
00074     IROBOT_VIDFMT_YUV422P = 14,
00075     IROBOT_VIDFMT_YUV411P = 15,
00076     IROBOT_VIDFMT_YUV420P = 16,
00077     IROBOT_VIDFMT_YUV410P = 17,
00078     // KEEP THIS ITEM LAST:
00079     IROBOT_VIDFMT_AUTO    = 18  // auto selection of best mode
00080   };
00081 
00082 //! Mapping between our IrobotVideoFormat and V4L2 pixfmt:
00083   static __u32 v4l2format[IROBOT_VIDFMT_AUTO] = {
00084     V4L2_PIX_FMT_GREY,
00085     0xffffffff,  // RAW unsupported by V4L2?
00086     V4L2_PIX_FMT_RGB555,
00087     V4L2_PIX_FMT_RGB565,
00088     V4L2_PIX_FMT_BGR24,
00089     V4L2_PIX_FMT_BGR32,
00090     V4L2_PIX_FMT_YUYV,
00091     V4L2_PIX_FMT_UYVY,
00092     0xffffffff,  // YUV444 ???
00093     V4L2_PIX_FMT_UYVY,
00094     V4L2_PIX_FMT_Y41P,
00095     V4L2_PIX_FMT_YUV420,
00096     V4L2_PIX_FMT_YUV410,
00097     0xffffffff,  // YUV444P?
00098     V4L2_PIX_FMT_YUV422P,
00099     V4L2_PIX_FMT_YUV411P,
00100     V4L2_PIX_FMT_YUV420,
00101     V4L2_PIX_FMT_YUV410
00102   };
00103 
00104 extern int RGB_Y_tab[256];
00105 extern int B_U_tab[256];
00106 extern int G_U_tab[256];
00107 extern int G_V_tab[256];
00108 extern int R_V_tab[256];
00109 extern const int BITS_OUT;
00110 extern char*           dev_name;
00111 extern io_method        io;
00112 extern int              fd;
00113 extern buffer*         buffers;
00114 extern unsigned int     n_buffers;
00115 extern int width, height;
00116 extern frame*     currentFrame;
00117 
00118 void errno_exit(const char *s);
00119 int xioctl(int fd, int request, void *arg);
00120 void colorspace_init();
00121 void yuv422_to_rgb24_c(unsigned char* dst,
00122                        const int w, const int h,
00123                        const unsigned char* yuv422ptr,
00124                        const int byteswap);
00125 unsigned char clamp(int d);
00126 void yv12_to_rgb24_c(unsigned char* dst,
00127                      int dst_stride,
00128                      const unsigned char* y_src,
00129                      const unsigned char* u_src,
00130                      const unsigned char* v_src,
00131                      int y_stride,
00132                      int uv_stride,
00133                      int width,
00134                      int height);
00135 frame* process_image(const void *p, const int len, const bool color = true);
00136 int read_frame(void);
00137 frame* get_frame(bool color=true);
00138 void mainloop(void);
00139 void stop_capturing(void);
00140 void start_capturing(void);
00141 void uninit_device(void);
00142 void init_read(unsigned int buffer_size);
00143 void init_mmap(void);
00144 void init_userp(unsigned int buffer_size);
00145 void init_device(int pix_fmt);
00146 void close_device(void);
00147 void open_device(void);
00148 
00149 #endif
Generated on Sun May 8 08:05:39 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3