diff -Nru gambas2-2.10.0.orig/gb.v4l/src/CWebcam.c gambas2-2.10.0/gb.v4l/src/CWebcam.c --- gambas2-2.10.0.orig/gb.v4l/src/CWebcam.c 2008-12-29 14:16:29.000000000 +0100 +++ gambas2-2.10.0/gb.v4l/src/CWebcam.c 2009-02-20 03:26:27.000000000 +0100 @@ -90,6 +90,15 @@ /*********************************************************************************** + V4l2 interface + +************************************************************************************/ + +#include "video_device2.c" + + +/*********************************************************************************** + Camera setup ************************************************************************************/ @@ -426,7 +435,11 @@ char *buf; int w,h; - buf=(char*)vd_get_image(DEVICE); + if (DEVICE->v4l_version == 2) + buf=(char*)vd_get_image2(DEVICE); + else + buf=(char*)vd_get_image(DEVICE); + if (!buf) return -1; w=DEVICE->vmmap.width; h=DEVICE->vmmap.height; @@ -638,28 +651,39 @@ struct video_tuner vtuner; VIDEO_STREAM *str; - mydev=open (GB.FileName(STRING(Device),LENGTH(Device)),O_RDWR); + mydev=open (GB.FileName(STRING(Device),LENGTH(Device)),O_RDWR | O_NONBLOCK, 0); if (mydev==-1) { GB.Error("Unable to open device"); return; } + DEVICE=vd_setup(DEF_WIDTH,DEF_HEIGHT,DEF_DEPTH,mydev); - if (!vd_setup_capture_mode(DEVICE)) + if (!init_device2(DEVICE) ) { - close(mydev); - GB.Free(POINTER(&DEVICE)); - GB.Error("Unable to setup capture mode"); - return; + DEVICE->v4l_version=2; + vd_get_capabilities(DEVICE); + start_capturing (mydev); } + else + { + DEVICE->v4l_version=1; + if (!vd_setup_capture_mode(DEVICE)) + { + close(mydev); + GB.Free(POINTER(&DEVICE)); + GB.Error("Unable to setup capture mode"); + return; + } - vd_setup_video_source(DEVICE,IN_DEFAULT,NORM_DEFAULT); + vd_setup_video_source(DEVICE,IN_DEFAULT,NORM_DEFAULT); + if (vd_ioctl (DEVICE, VIDIOCGTUNER, &vtuner)) DEVICE->Freq2=1; + + } GB.Alloc(POINTER(&THIS->device),sizeof(char)*(LENGTH(Device)+1)); strcpy(THIS->device,STRING(Device)); - if (vd_ioctl (DEVICE, VIDIOCGTUNER, &vtuner)) DEVICE->Freq2=1; - THIS->stream.desc=&VideoStream; str=(VIDEO_STREAM*)POINTER(&THIS->stream); str->handle=(void*)THIS; @@ -673,6 +697,10 @@ if (DEVICE) { + if (DEVICE->v4l_version == 2) { + stop_capturing(DEVICE->dev); + uninit_device2(); + } vd_close(DEVICE); GB.Free(POINTER(&DEVICE)); } @@ -707,27 +735,40 @@ whiteness=DEVICE->videopict.whiteness; if (THIS->membuf) GB.Free(POINTER(&THIS->membuf)); + stop_capturing (DEVICE->dev); + uninit_device2(); + close(DEVICE->dev); vd_close(DEVICE); GB.Free(POINTER(&DEVICE)); - mydev=open(THIS->device,O_RDWR); + mydev=open(THIS->device,O_RDWR | O_NONBLOCK,0 ); if (mydev==-1) { GB.Error("Unable to open device"); return; } + DEVICE=vd_setup(w,h,DEF_DEPTH,mydev); - if (!vd_setup_capture_mode(DEVICE)) + if (!init_device2(DEVICE) ) { - close(mydev); - GB.Free(POINTER(&DEVICE)); - GB.Error("Unable to setup capture mode"); - return; + DEVICE->v4l_version=2; + vd_get_capabilities(DEVICE); + start_capturing (mydev); + } + else + { + DEVICE->v4l_version=1; + if (!vd_setup_capture_mode(DEVICE)) + { + close(mydev); + GB.Free(POINTER(&DEVICE)); + GB.Error("Unable to setup capture mode"); + return; + } + + vd_setup_video_source(DEVICE,channel,norm); } - - vd_setup_video_source(DEVICE,channel,norm); - DEVICE->videopict.hue=hue; DEVICE->videopict.contrast=contrast; DEVICE->videopict.brightness=brightness; @@ -795,7 +836,12 @@ char *buf; int w,h; - buf=(char*)vd_get_image(DEVICE); + if (DEVICE->v4l_version == 2) + { + buf=(char*)vd_get_image2(DEVICE); + } else { + buf=(char*)vd_get_image(DEVICE); + } if (!buf) { GB.Error("Unable to get image"); @@ -819,7 +865,11 @@ char *buf; int w,h; - buf=(char*)vd_get_image(DEVICE); + if (DEVICE->v4l_version == 2) + buf=(char*)vd_get_image2(DEVICE); + else + buf=(char*)vd_get_image(DEVICE); + if (!buf) { GB.Error("Unable to get image"); @@ -827,8 +877,8 @@ return; } - w=DEVICE->vmmap.width; - h=DEVICE->vmmap.height; + w=DEVICE->width; + h=DEVICE->height; vd_image_done(DEVICE); GB.Picture.Create(&ret,(void*)buf,w,h,GB_IMAGE_BGR); @@ -902,7 +952,12 @@ GB.Error("Unable to open file for writting"); return; } - buf=(char*)vd_get_image(DEVICE); + if (DEVICE->v4l_version == 2) { + buf=(char*)vd_get_image2(DEVICE); + } else { + buf=(char*)vd_get_image(DEVICE); + } + if (!buf) { fclose(fd); @@ -910,8 +965,8 @@ return; } - w=DEVICE->vmmap.width; - h=DEVICE->vmmap.height; + w=DEVICE->width; + h=DEVICE->height; switch(format) { diff -Nru gambas2-2.10.0.orig/gb.v4l/src/CWebcam.h gambas2-2.10.0/gb.v4l/src/CWebcam.h --- gambas2-2.10.0.orig/gb.v4l/src/CWebcam.h 2008-12-29 14:16:29.000000000 +0100 +++ gambas2-2.10.0/gb.v4l/src/CWebcam.h 2009-02-20 03:03:27.000000000 +0100 @@ -72,6 +73,7 @@ unsigned char *frame_buffer; // not the video memory, but one image int dev; // fd of the physical device int Freq2; + int v4l_version; } video_device_t; diff -Nru gambas2-2.10.0.orig/gb.v4l/src/video_device2.c gambas2-2.10.0/gb.v4l/src/video_device2.c --- gambas2-2.10.0.orig/gb.v4l/src/video_device2.c 1970-01-01 01:00:00.000000000 +0100 +++ gambas2-2.10.0/gb.v4l/src/video_device2.c 2009-02-20 03:40:19.000000000 +0100 @@ -0,0 +1,663 @@ +/* + * V4L2 video capture example + * + * This program can be used and distributed without restrictions. + */ + +#include +#include +#include +#include + +#include /* getopt_long() */ + +#include /* low-level i/o */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include /* for videodev2.h */ + +#include +#include + +#define MEMCLEAR(x) memset (&(x), 0, sizeof (x)) + +typedef enum { + IO_METHOD_READ, + IO_METHOD_MMAP, + IO_METHOD_USERPTR, +} io_method; + +struct buffer { + void * start; + size_t length; +}; + +static io_method io = IO_METHOD_MMAP; +struct buffer * buffers = NULL; +unsigned char * bufferRGB = NULL; +static unsigned int n_buffers = 0; + +#define CLIP(color) (unsigned char)(((color)>0xFF)?0xff:(((color)<0)?0:(color))) + +void v4lconvert_yuyv_to_bgr24(const unsigned char *src, unsigned char *dest, + int width, int height) +{ + int j; + + while (--height >= 0) { + for (j = 0; j < width; j += 2) { + int u = src[1]; + int v = src[3]; + int u1 = (((u - 128) << 7) + (u - 128)) >> 6; + int rg = (((u - 128) << 1) + (u - 128) + + ((v - 128) << 2) + ((v - 128) << 1)) >> 3; + int v1 = (((v - 128) << 1) + (v - 128)) >> 1; + + *dest++ = CLIP(src[0] + u1); + *dest++ = CLIP(src[0] - rg); + *dest++ = CLIP(src[0] + v1); + + *dest++ = CLIP(src[2] + u1); + *dest++ = CLIP(src[2] - rg); + *dest++ = CLIP(src[2] + v1); + src += 4; + } + } +} + + + +void v4lconvert_yuyv_to_rgb24(const unsigned char *src, unsigned char *dest, + int width, int height) +{ + int j; + + while (--height >= 0) { + for (j = 0; j < width; j += 2) { + int u = src[1]; + int v = src[3]; + int u1 = (((u - 128) << 7) + (u - 128)) >> 6; + int rg = (((u - 128) << 1) + (u - 128) + + ((v - 128) << 2) + ((v - 128) << 1)) >> 3; + int v1 = (((v - 128) << 1) + (v - 128)) >> 1; + + *dest++ = CLIP(src[0] + v1); + *dest++ = CLIP(src[0] - rg); + *dest++ = CLIP(src[0] + u1); + + *dest++ = CLIP(src[2] + v1); + *dest++ = CLIP(src[2] - rg); + *dest++ = CLIP(src[2] + u1); + src += 4; + } + } +} + +int convert_yuv_to_rgb_pixel(int y, int u, int v) +{ + unsigned int pixel32 = 0; + unsigned char *pixel = (unsigned char *)&pixel32; + int r, g, b; + + r = y + (1.370705 * (v-128)); + g = y - (0.698001 * (v-128)) - (0.337633 * (u-128)); + b = y + (1.732446 * (u-128)); + + if(r > 255) r = 255; + if(g > 255) g = 255; + if(b > 255) b = 255; + if(r < 0) r = 0; + if(g < 0) g = 0; + if(b < 0) b = 0; + + pixel[0] = r * 220 / 256; + pixel[1] = g * 220 / 256; + pixel[2] = b * 220 / 256; + + return pixel32; +} + +int convert_yuv_to_rgb_buffer(const unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height) +{ + unsigned int in, out = 0; + unsigned int pixel_16; + unsigned char pixel_24[3]; + unsigned int pixel32; + int y0, u, y1, v; + + for(in = 0; in < width * height * 2; in += 4) { + pixel_16 = + yuv[in + 3] << 24 | + yuv[in + 2] << 16 | + yuv[in + 1] << 8 | + yuv[in + 0]; + + y0 = (pixel_16 & 0x000000ff); + u = (pixel_16 & 0x0000ff00) >> 8; + y1 = (pixel_16 & 0x00ff0000) >> 16; + v = (pixel_16 & 0xff000000) >> 24; + + pixel32 = convert_yuv_to_rgb_pixel(y0, u, v); + pixel_24[0] = (pixel32 & 0x000000ff); + pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; + pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; + + rgb[out++] = pixel_24[0]; + rgb[out++] = pixel_24[1]; + rgb[out++] = pixel_24[2]; + + pixel32 = convert_yuv_to_rgb_pixel(y1, u, v); + pixel_24[0] = (pixel32 & 0x000000ff); + pixel_24[1] = (pixel32 & 0x0000ff00) >> 8; + pixel_24[2] = (pixel32 & 0x00ff0000) >> 16; + + rgb[out++] = pixel_24[0]; + rgb[out++] = pixel_24[1]; + rgb[out++] = pixel_24[2]; + } + + return 0; +} + +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 +process_image (int width, int height, const void *p) +{ + if(bufferRGB == NULL) + return; + + v4lconvert_yuyv_to_bgr24(p, bufferRGB, width, height); +} + +static int +read_frame (int width, int height, int fd) +{ + struct v4l2_buffer buf; + unsigned int i; + + switch (io) { + case IO_METHOD_READ: + if (-1 == read (fd, buffers[0].start, buffers[0].length)) { + switch (errno) { + case EAGAIN: + return 0; + + case EIO: + /* Could ignore EIO, see spec. */ + + /* fall through */ + + default: + errno_exit ("read"); + } + } + + process_image (width, height, buffers[0].start); + + break; + + case IO_METHOD_MMAP: + MEMCLEAR (buf); + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_MMAP; + + if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) { + switch (errno) { + case EAGAIN: + return 0; + + case EIO: + /* Could ignore EIO, see spec. */ + + /* fall through */ + + default: + errno_exit ("VIDIOC_DQBUF"); + } + } + + assert (buf.index < n_buffers); + + process_image (width, height, buffers[buf.index].start); + + if (-1 == xioctl (fd, VIDIOC_QBUF, &buf)) + errno_exit ("VIDIOC_QBUF"); + + break; + + case IO_METHOD_USERPTR: + MEMCLEAR (buf); + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_USERPTR; + + if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) { + switch (errno) { + case EAGAIN: + return 0; + + case EIO: + /* Could ignore EIO, see spec. */ + + /* fall through */ + + default: + errno_exit ("VIDIOC_DQBUF"); + } + } + + for (i = 0; i < n_buffers; ++i) + if (buf.m.userptr == (unsigned long) buffers[i].start + && buf.length == buffers[i].length) + break; + + assert (i < n_buffers); + + process_image (width, height, (void *) buf.m.userptr); + + if (-1 == xioctl (fd, VIDIOC_QBUF, &buf)) + errno_exit ("VIDIOC_QBUF"); + + break; + } + + return 1; +} + +unsigned char * +vd_get_image2 (video_device_t *vd) +{ + for (;;) { + fd_set fds; + struct timeval tv; + int r; + + FD_ZERO (&fds); + FD_SET (vd->dev, &fds); + + /* Timeout. */ + tv.tv_sec = 2; + tv.tv_usec = 0; + + r = select (vd->dev + 1, &fds, NULL, NULL, &tv); + + if (-1 == r) { + if (EINTR == errno) + continue; + + errno_exit ("select"); + } + + if (0 == r) { + fprintf (stderr, "select timeout\n"); + exit (EXIT_FAILURE); + } + if (read_frame (vd->width, vd->height, vd->dev)) + return bufferRGB; + + /* EAGAIN - continue select loop. */ + } + return NULL; +} + +static void +stop_capturing (int fd) +{ + enum v4l2_buf_type type; + + switch (io) { + case IO_METHOD_READ: + /* Nothing to do. */ + break; + + case IO_METHOD_MMAP: + case IO_METHOD_USERPTR: + type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type)) + errno_exit ("VIDIOC_STREAMOFF"); + + break; + } +} + +static void +start_capturing (int fd) +{ + unsigned int i; + enum v4l2_buf_type type; + + switch (io) { + case IO_METHOD_READ: + /* Nothing to do. */ + break; + + case IO_METHOD_MMAP: + for (i = 0; i < n_buffers; ++i) { + struct v4l2_buffer buf; + + MEMCLEAR (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"); + + break; + + case IO_METHOD_USERPTR: + for (i = 0; i < n_buffers; ++i) { + struct v4l2_buffer buf; + + MEMCLEAR (buf); + + buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + buf.memory = V4L2_MEMORY_USERPTR; + buf.index = i; + buf.m.userptr = (unsigned long) buffers[i].start; + buf.length = buffers[i].length; + + 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"); + + break; + } +} + +static void +uninit_device2 (void) +{ + unsigned int i; + + switch (io) { + case IO_METHOD_READ: + free (buffers[0].start); + break; + + case IO_METHOD_MMAP: + for (i = 0; i < n_buffers; ++i) + if (-1 == munmap (buffers[i].start, buffers[i].length)) + return; + break; + + case IO_METHOD_USERPTR: + for (i = 0; i < n_buffers; ++i) + free (buffers[i].start); + break; + } + + free (buffers); + free (bufferRGB); +} + +static void +init_read (unsigned int buffer_size) +{ + buffers = calloc (1, sizeof (*buffers)); + + if (!buffers) { + fprintf (stderr, "Out of memory\n"); + exit (EXIT_FAILURE); + } + + buffers[0].length = buffer_size; + buffers[0].start = malloc (buffer_size); + + if (!buffers[0].start) { + fprintf (stderr, "Out of memory\n"); + exit (EXIT_FAILURE); + } +} + +static void +init_mmap (int fd) +{ + struct v4l2_requestbuffers req; + + MEMCLEAR (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, "Device does not support " + "memory mapping\n"); + exit (EXIT_FAILURE); + } else { + errno_exit ("VIDIOC_REQBUFS"); + } + } + + if (req.count < 2) { + fprintf (stderr, "Insufficient buffer memory on device\n"); + exit (EXIT_FAILURE); + } + + buffers = calloc (req.count, sizeof (*buffers)); + + if (!buffers) { + fprintf (stderr, "Out of memory\n"); + exit (EXIT_FAILURE); + } + + for (n_buffers = 0; n_buffers < req.count; ++n_buffers) { + struct v4l2_buffer buf; + + MEMCLEAR (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 /* start anywhere */, + buf.length, + PROT_READ | PROT_WRITE /* required */, + MAP_SHARED /* recommended */, + fd, buf.m.offset); + + if (MAP_FAILED == buffers[n_buffers].start) + errno_exit ("mmap"); + } +} + +static void +init_userp (unsigned int buffer_size, int fd) +{ + struct v4l2_requestbuffers req; + unsigned int page_size; + + page_size = getpagesize (); + buffer_size = (buffer_size + page_size - 1) & ~(page_size - 1); + + MEMCLEAR (req); + + req.count = 4; + req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + req.memory = V4L2_MEMORY_USERPTR; + + if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) { + if (EINVAL == errno) { + fprintf (stderr, "Device does not support " + "user pointer i/o\n"); + exit (EXIT_FAILURE); + } else { + errno_exit ("VIDIOC_REQBUFS"); + } + } + + buffers = calloc (4, sizeof (*buffers)); + + if (!buffers) { + fprintf (stderr, "Out of memory\n"); + exit (EXIT_FAILURE); + } + + for (n_buffers = 0; n_buffers < 4; ++n_buffers) { + buffers[n_buffers].length = buffer_size; + buffers[n_buffers].start = memalign (/* boundary */ page_size, + buffer_size); + + if (!buffers[n_buffers].start) { + fprintf (stderr, "Out of memory\n"); + exit (EXIT_FAILURE); + } + } +} + +int +init_device2 (video_device_t *vd) +{ + struct v4l2_capability cap; + struct v4l2_cropcap cropcap; + struct v4l2_crop crop; + struct v4l2_format fmt; + unsigned int min; + + if (-1 == xioctl (vd->dev, VIDIOC_QUERYCAP, &cap)) { + if (EINVAL == errno) { + fprintf (stderr, "Device is no V4L2 device\n"); + return 1; + } else { + errno_exit ("VIDIOC_QUERYCAP"); + } + } + + if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { + fprintf (stderr, "Device is no video capture device\n"); + return 2; + } + + switch (io) { + case IO_METHOD_READ: + if (!(cap.capabilities & V4L2_CAP_READWRITE)) { + fprintf (stderr, "Device does not support read i/o\n"); + return 3; + } + + break; + + case IO_METHOD_MMAP: + case IO_METHOD_USERPTR: + if (!(cap.capabilities & V4L2_CAP_STREAMING)) { + fprintf (stderr, "Device does not support streaming i/o\n"); + return 4; + } + + break; + } + + + /* Select video input, video standard and tune here. */ + + + MEMCLEAR (cropcap); + + cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + + if (0 == xioctl (vd->dev, VIDIOC_CROPCAP, &cropcap)) { + crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + crop.c = cropcap.defrect; /* reset to default */ + + if (-1 == xioctl (vd->dev, VIDIOC_S_CROP, &crop)) { + switch (errno) { + case EINVAL: + /* Cropping not supported. */ + break; + default: + /* Errors ignored. */ + break; + } + } + } else { + /* Errors ignored. */ + } + + + MEMCLEAR (fmt); + + fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + fmt.fmt.pix.width = vd->width; + fmt.fmt.pix.height = vd->height; + fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; + fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; + + if (-1 == xioctl (vd->dev, VIDIOC_S_FMT, &fmt)) + return 1; + + bufferRGB = malloc(vd->width*vd->height*vd->depth); + /* Note VIDIOC_S_FMT may change width and height. */ + + /* Buggy driver paranoia. */ + min = fmt.fmt.pix.width * 2; + if (fmt.fmt.pix.bytesperline < min) + fmt.fmt.pix.bytesperline = min; + min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; + if (fmt.fmt.pix.sizeimage < min) + fmt.fmt.pix.sizeimage = min; + + switch (io) { + case IO_METHOD_READ: + init_read (fmt.fmt.pix.sizeimage); + break; + + case IO_METHOD_MMAP: + init_mmap (vd->dev); + break; + + case IO_METHOD_USERPTR: + init_userp (fmt.fmt.pix.sizeimage,vd->dev); + break; + } + + return 0; +}