Derek’s hack to fix W7S flipped WebCam

I was going to use some of my vacation time to try to fix the vertical flip (upside down) bug in Chicony Electronics WebCam (04f2:b012) in W7S laptop, but decided to take a look on Google again if anyone has already found a fix for that… And I found it!!!

Derek Bruce wrote a small hack to uvcvideo that fixes this anoying bug at kernel module level. Is a very simple sollution, but works very well!!!

Just aply this patch to uvcvideo (content also avaliable at the end of this post). You can download latest uvcvideo from here, or checkout the latest svn version using the following command:

svn checkout svn://svn.berlios.de/linux-uvc/linux-uvc/trunk uvc-video

Important note: Default Makefile will put uvcvideo kernel module on the wrong path for Ubuntu, and you may need to change this line on Makefile:

INSTALL_MOD_DIR := kernel/ubuntu/media/usbvideo

to:

INSTALL_MOD_DIR := ubuntu/media/usbvideo

Download the patch file to the uvcvideo source folder, and aply it using the following command:

patch -i uvc-hack.patch

Compile and install the driver using make:

make
sudo make install

Just reload the kernel module:

modprobe -r uvcvideo
modprobe uvcvideo

And that’s all… your don’t need to flip the notebook anymore to talk with another people! 😉

Thanks a lot, Derek, for this nice patch!!!

The complete code from the pacth (Copy and save as uvc-hack.patch, for example):

diff -u trunk/uvc_queue.c ../uvc-dfu07/trunk/uvc_queue.c
--- trunk/uvc_queue.c	2008-05-06 00:48:16.000000000 -0500
+++ ../uvc-dfu07/trunk/uvc_queue.c	2008-05-24 01:01:34.000000000 -0500
@@ -287,10 +287,13 @@
  * available.
  */
 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
-		struct v4l2_buffer *v4l2_buf, int nonblocking)
+		struct v4l2_buffer *v4l2_buf, int nonblocking,
+		struct uvc_frame *frame)
 {
 	struct uvc_buffer *buf;
 	int ret = 0;
+	int i;
+	char swap[frame->wWidth << 1];

 	if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
 	    v4l2_buf->memory != V4L2_MEMORY_MMAP) {
@@ -333,6 +336,20 @@
 		goto done;
 	}

+	// Flip webcam image.
+	for (i = 0; i < (frame->wHeight >> 1); i++) {
+		memcpy(&swap, (char *) queue->mem + buf->buf.m.offset +
+			i * (frame->wWidth << 1), (frame->wWidth << 1));
+		memcpy((char *) queue->mem + buf->buf.m.offset +
+			i * (frame->wWidth << 1),
+			(char *) queue->mem + buf->buf.m.offset +
+			(frame->wHeight - 1 - i) *
+			(frame->wWidth << 1), (frame->wWidth << 1));
+		memcpy((char *) queue->mem + buf->buf.m.offset +
+			(frame->wHeight - 1 - i) *
+			(frame->wWidth << 1) , &swap, (frame->wWidth << 1));
+	}
+
 	list_del(&buf->stream);
 	__uvc_query_buffer(buf, v4l2_buf);

diff -u trunk/uvc_v4l2.c ../uvc-dfu07/trunk/uvc_v4l2.c
--- trunk/uvc_v4l2.c	2008-05-06 00:48:16.000000000 -0500
+++ ../uvc-dfu07/trunk/uvc_v4l2.c	2008-05-24 00:36:24.000000000 -0500
@@ -877,7 +877,8 @@
 			return -EBUSY;

 		return uvc_dequeue_buffer(&video->queue, arg,
-			file->f_flags & O_NONBLOCK);
+			file->f_flags & O_NONBLOCK,
+			video->streaming->cur_frame);

 	case VIDIOC_STREAMON:
 	{
diff -u trunk/uvcvideo.h ../uvc-dfu07/trunk/uvcvideo.h
--- trunk/uvcvideo.h	2008-05-06 00:48:16.000000000 -0500
+++ ../uvc-dfu07/trunk/uvcvideo.h	2008-05-24 00:36:24.000000000 -0500
@@ -711,7 +711,8 @@
 extern int uvc_queue_buffer(struct uvc_video_queue *queue,
 		struct v4l2_buffer *v4l2_buf);
 extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
-		struct v4l2_buffer *v4l2_buf, int nonblocking);
+		struct v4l2_buffer *v4l2_buf, int nonblocking,
+		struct uvc_frame *frame);
 extern int uvc_queue_enable(struct uvc_video_queue *queue, int enable);
 extern void uvc_queue_cancel(struct uvc_video_queue *queue);
 extern struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
Etiquetado , , , , , , , , , ,

2 pensamentos sobre “Derek’s hack to fix W7S flipped WebCam

  1. […] UPDATE (07/14/2008): Derek Bruce has developed a small hack that fixes the upside down webcam on W7S. I’ve described it better here. […]

  2. […] WebCam: (ATUALIZADO: 12/07/2008) – Um patch feito por Derek Bruce resolve o problema da imagem invertida na webcam! Confira aqui. […]

Deixe um comentário