Skip to contentSkip to conversation

video: Extended vivi pixel format support

PATCH New Magnus Damm 1 participant 6 messages Oct 14, 2008 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

cover video: Extended vivi pixel format support

Extended vivi pixel format support

[PATCH 01/05] video: Precalculate vivi yuv values [PATCH 02/05] video: Teach vivi about multiple pixel formats [PATCH 03/05] video: Add uyvy pixel format support to vivi [PATCH 04/05] video: Add support for rgb565 pixel formats to vivi [PATCH 05/05] video: Add support for rgb555 pixel formats to vivi

These patches improve the RGB->YUV color space conversion code in vivi.c and also add support for the following pixel formats:

V4L2_PIX_FMT_UYVY
V4L2_PIX_FMT_RGB565 /* gggbbbbb rrrrrggg */
V4L2_PIX_FMT_RGB565X /* rrrrrggg gggbbbbb */
V4L2_PIX_FMT_RGB555 /* gggbbbbb arrrrrgg */
V4L2_PIX_FMT_RGB555X /* arrrrrgg gggbbbbb */

With these patches the vivi driver can be used as a reference for testing the byte order of 16-bit pixel formats. This allows testing of user space software without actual capture hardware. It is also useful for people who are developing new drivers and have doubts which 16-bit RGB interpretation is the valid one (RGB vs BGR).

The color space conversion code is updated to use precalculated YUV values instead of doing RGB->YUV calculation for every two pixels.

---

 drivers/media/video/vivi.c |  314 +++++++++++++++++++++++++++++++-------------
 1 file changed, 225 insertions(+), 89 deletions(-)

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#1 lore raw

PATCH 1/5 video: Precalculate vivi yuv values

From: Magnus Damm <[email protected]>

This patch improves the color space conversion code in vivi.c to directly draw with precalculated YUV values as palette instead of drawing with YUV that is calculated from RGB for every two pixels. This way we eliminate the need for 9 multiplications every two pixels.

A side effect of this patch is that the time counter is changed from green text on black background to white text on black background.

---

 drivers/media/video/vivi.c |  117 +++++++++++++++++++++-----------------------
 1 file changed, 58 insertions(+), 59 deletions(-)
drivers/media/video/vivi.c +58 -59
@@ -190,6 +190,7 @@ struct vivi_fh {
 	struct videobuf_queue      vb_vidq;
 
 	enum v4l2_buf_type         type;
+	unsigned char              bars[8][3];
 };
 
 /* ------------------------------------------------------------------
@@ -234,13 +235,41 @@ static u8 bars[8][3] = {
 #define TSTAMP_MAX_Y TSTAMP_MIN_Y+15
 #define TSTAMP_MIN_X 64
 
-static void gen_line(char *basep, int inipos, int wmax,
+static void gen_twopix(struct vivi_fh *fh, unsigned char *buf, int colorpos)
+{
+	unsigned char r_y, g_u, b_v;
+	unsigned char *p;
+	int color;
+
+	r_y = fh->bars[colorpos][0]; /* R or precalculated Y */
+	g_u = fh->bars[colorpos][1]; /* G or precalculated U */
+	b_v = fh->bars[colorpos][2]; /* B or precalculated V */
+
+	for (color = 0; color < 4; color++) {
+		p = buf + color;
+
+		switch (color) {
+		case 0:
+		case 2:
+			*p = r_y;
+			break;
+		case 1:
+			*p = g_u;
+			break;
+		case 3:
+			*p = b_v;
+			break;
+		}
+	}
+}
+
+static void gen_line(struct vivi_fh *fh, char *basep, int inipos, int wmax,
 		int hmax, int line, int count, char *timestr)
 {
-	int  w, i, j, y;
+	int  w, i, j;
 	int pos = inipos;
-	char *p, *s;
-	u8   chr, r, g, b, color;
+	char *s;
+	u8 chr;
 
 	/* We will just duplicate the second pixel at the packet */
 	wmax /= 2;
@@ -248,27 +277,9 @@ static void gen_line(char *basep, int in
 	/* Generate a standard color bar pattern */
 	for (w = 0; w < wmax; w++) {
 		int colorpos = ((w + count) * 8/(wmax + 1)) % 8;
-		r = bars[colorpos][0];
-		g = bars[colorpos][1];
-		b = bars[colorpos][2];
-
-		for (color = 0; color < 4; color++) {
-			p = basep + pos;
-
-			switch (color) {
-			case 0:
-			case 2:
-				*p = TO_Y(r, g, b);	/* Luma */
-				break;
-			case 1:
-				*p = TO_U(r, g, b);	/* Cb */
-				break;
-			case 3:
-				*p = TO_V(r, g, b);	/* Cr */
-				break;
-			}
-			pos++;
-		}
+
+		gen_twopix(fh, basep + pos, colorpos);
+		pos += 4; /* only 16 bpp supported for now */
 	}
 
 	/* Checks if it is possible to show timestamp */
@@ -283,38 +294,12 @@ static void gen_line(char *basep, int in
 		for (s = timestr; *s; s++) {
 			chr = rom8x16_bits[(*s-0x30)*16+line-TSTAMP_MIN_Y];
 			for (i = 0; i < 7; i++) {
-				if (chr & 1 << (7 - i)) {
-					/* Font color*/
-					r = 0;
-					g = 198;
-					b = 0;
-				} else {
-					/* Background color */
-					r = bars[BLACK][0];
-					g = bars[BLACK][1];
-					b = bars[BLACK][2];
-				}
-
 				pos = inipos + j * 2;
-				for (color = 0; color < 4; color++) {
-					p = basep + pos;
-
-					y = TO_Y(r, g, b);
-
-					switch (color) {
-					case 0:
-					case 2:
-						*p = TO_Y(r, g, b); /* Luma */
-						break;
-					case 1:
-						*p = TO_U(r, g, b); /* Cb */
-						break;
-					case 3:
-						*p = TO_V(r, g, b); /* Cr */
-						break;
-					}
-					pos++;
-				}
+				/* Draw white font on black background */
+				if (chr & 1 << (7 - i))
+					gen_twopix(fh, basep + pos, WHITE);
+				else
+					gen_twopix(fh, basep + pos, BLACK);
 				j++;
 			}
 		}
@@ -324,8 +309,9 @@ end:
 	return;
 }
 
-static void vivi_fillbuff(struct vivi_dev *dev, struct vivi_buffer *buf)
+static void vivi_fillbuff(struct vivi_fh *fh, struct vivi_buffer *buf)
 {
+	struct vivi_dev *dev = fh->dev;
 	int h , pos = 0;
 	int hmax  = buf->vb.height;
 	int wmax  = buf->vb.width;
@@ -341,7 +327,7 @@ static void vivi_fillbuff(struct vivi_de
 		return;
 
 	for (h = 0; h < hmax; h++) {
-		gen_line(tmpbuf, 0, wmax, hmax, h, dev->mv_count,
+		gen_line(fh, tmpbuf, 0, wmax, hmax, h, dev->mv_count,
 			 dev->timestr);
 		memcpy(vbuf + pos, tmpbuf, wmax * 2);
 		pos += wmax*2;
@@ -410,7 +396,7 @@ static void vivi_thread_tick(struct vivi
 	do_gettimeofday(&buf->vb.ts);
 
 	/* Fill buffer */
-	vivi_fillbuff(dev, buf);
+	vivi_fillbuff(fh, buf);
 	dprintk(dev, 1, "filled buffer %p\n", buf);
 
 	wake_up(&buf->vb.done);
@@ -714,6 +700,8 @@ static int vidioc_s_fmt_vid_cap(struct f
 {
 	struct vivi_fh  *fh = priv;
 	struct videobuf_queue *q = &fh->vb_vidq;
+	unsigned char r, g, b;
+	int k;
 
 	int ret = vidioc_try_fmt_vid_cap(file, fh, f);
 	if (ret < 0)
@@ -733,6 +721,17 @@ static int vidioc_s_fmt_vid_cap(struct f
 	fh->vb_vidq.field = f->fmt.pix.field;
 	fh->type          = f->type;
 
+	/* precalculate color bar values to speed up rendering */
+	for (k = 0; k < 8; k++) {
+		r = bars[k][0];
+		g = bars[k][1];
+		b = bars[k][2];
+
+		fh->bars[k][0] = TO_Y(r, g, b);	/* Luma */
+		fh->bars[k][1] = TO_U(r, g, b);	/* Cb */
+		fh->bars[k][2] = TO_V(r, g, b);	/* Cr */
+	}
+
 	ret = 0;
 out:
 	mutex_unlock(&q->vb_lock);

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#2 lore raw

PATCH 2/5 video: Teach vivi about multiple pixel formats

In reply to Magnus Damm

From: Magnus Damm <[email protected]>

This patch contains the ground work to add support for multiple pixel formats to vivi.c

---

 drivers/media/video/vivi.c |   97 ++++++++++++++++++++++++++++++--------------
 1 file changed, 67 insertions(+), 30 deletions(-)
drivers/media/video/vivi.c +67 -30
@@ -128,12 +128,31 @@ struct vivi_fmt {
 	int   depth;
 };
 
-static struct vivi_fmt format = {
-	.name     = "4:2:2, packed, YUYV",
-	.fourcc   = V4L2_PIX_FMT_YUYV,
-	.depth    = 16,
+static struct vivi_fmt formats[] = {
+	{
+		.name     = "4:2:2, packed, YUYV",
+		.fourcc   = V4L2_PIX_FMT_YUYV,
+		.depth    = 16,
+	},
 };
 
+static struct vivi_fmt *get_format(struct v4l2_format *f)
+{
+	struct vivi_fmt *fmt;
+	unsigned int k;
+
+	for (k = 0; k < ARRAY_SIZE(formats); k++) {
+		fmt = &formats[k];
+		if (fmt->fourcc == f->fmt.pix.pixelformat)
+			break;
+	}
+
+	if (k == ARRAY_SIZE(formats))
+		return NULL;
+
+	return &formats[k];
+}
+
 struct sg_to_addr {
 	int pos;
 	struct scatterlist *sg;
@@ -248,16 +267,20 @@ static void gen_twopix(struct vivi_fh *f
 	for (color = 0; color < 4; color++) {
 		p = buf + color;
 
-		switch (color) {
-		case 0:
-		case 2:
-			*p = r_y;
-			break;
-		case 1:
-			*p = g_u;
-			break;
-		case 3:
-			*p = b_v;
+		switch (fh->fmt->fourcc) {
+		case V4L2_PIX_FMT_YUYV:
+			switch (color) {
+			case 0:
+			case 2:
+				*p = r_y;
+				break;
+			case 1:
+				*p = g_u;
+				break;
+			case 3:
+				*p = b_v;
+				break;
+			}
 			break;
 		}
 	}
@@ -622,11 +645,15 @@ static int vidioc_querycap(struct file *
 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
 					struct v4l2_fmtdesc *f)
 {
-	if (f->index > 0)
+	struct vivi_fmt *fmt;
+
+	if (f->index >= ARRAY_SIZE(formats))
 		return -EINVAL;
 
-	strlcpy(f->description, format.name, sizeof(f->description));
-	f->pixelformat = format.fourcc;
+	fmt = &formats[f->index];
+
+	strlcpy(f->description, fmt->name, sizeof(f->description));
+	f->pixelformat = fmt->fourcc;
 	return 0;
 }
 
@@ -656,13 +683,12 @@ static int vidioc_try_fmt_vid_cap(struct
 	enum v4l2_field field;
 	unsigned int maxw, maxh;
 
-	if (format.fourcc != f->fmt.pix.pixelformat) {
-		dprintk(dev, 1, "Fourcc format (0x%08x) invalid. "
-			"Driver accepts only 0x%08x\n",
-			f->fmt.pix.pixelformat, format.fourcc);
+	fmt = get_format(f);
+	if (!fmt) {
+		dprintk(dev, 1, "Fourcc format (0x%08x) invalid.\n",
+			f->fmt.pix.pixelformat);
 		return -EINVAL;
 	}
-	fmt = &format;
 
 	field = f->fmt.pix.field;
 
@@ -701,7 +727,7 @@ static int vidioc_s_fmt_vid_cap(struct f
 	struct vivi_fh  *fh = priv;
 	struct videobuf_queue *q = &fh->vb_vidq;
 	unsigned char r, g, b;
-	int k;
+	int k, is_yuv;
 
 	int ret = vidioc_try_fmt_vid_cap(file, fh, f);
 	if (ret < 0)
@@ -715,7 +741,7 @@ static int vidioc_s_fmt_vid_cap(struct f
 		goto out;
 	}
 
-	fh->fmt           = &format;
+	fh->fmt           = get_format(f);
 	fh->width         = f->fmt.pix.width;
 	fh->height        = f->fmt.pix.height;
 	fh->vb_vidq.field = f->fmt.pix.field;
@@ -726,10 +752,23 @@ static int vidioc_s_fmt_vid_cap(struct f
 		r = bars[k][0];
 		g = bars[k][1];
 		b = bars[k][2];
+		is_yuv = 0;
+
+		switch (fh->fmt->fourcc) {
+		case V4L2_PIX_FMT_YUYV:
+			is_yuv = 1;
+			break;
+		}
 
-		fh->bars[k][0] = TO_Y(r, g, b);	/* Luma */
-		fh->bars[k][1] = TO_U(r, g, b);	/* Cb */
-		fh->bars[k][2] = TO_V(r, g, b);	/* Cr */
+		if (is_yuv) {
+			fh->bars[k][0] = TO_Y(r, g, b);	/* Luma */
+			fh->bars[k][1] = TO_U(r, g, b);	/* Cb */
+			fh->bars[k][2] = TO_V(r, g, b);	/* Cr */
+		} else {
+			fh->bars[k][0] = r;
+			fh->bars[k][1] = g;
+			fh->bars[k][2] = b;
+		}
 	}
 
 	ret = 0;
@@ -885,8 +924,6 @@ static int vidioc_s_ctrl(struct file *fi
 	File operations for the device
    ------------------------------------------------------------------*/
 
-#define line_buf_size(norm) (norm_maxw(norm)*(format.depth+7)/8)
-
 static int vivi_open(struct inode *inode, struct file *file)
 {
 	int minor = iminor(inode);
@@ -931,7 +968,7 @@ unlock:
 	fh->dev      = dev;
 
 	fh->type     = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-	fh->fmt      = &format;
+	fh->fmt      = &formats[0];
 	fh->width    = 640;
 	fh->height   = 480;
 

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#3 lore raw

PATCH 3/5 video: Add uyvy pixel format support to vivi

In reply to Magnus Damm

From: Magnus Damm <[email protected]>

This patch simply adds UYVY pixel format support to the vivi driver.

---

 drivers/media/video/vivi.c |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/media/video/vivi.c +20 -0
@@ -134,6 +134,11 @@ static struct vivi_fmt formats[] = {
 		.fourcc   = V4L2_PIX_FMT_YUYV,
 		.depth    = 16,
 	},
+	{
+		.name     = "4:2:2, packed, UYVY",
+		.fourcc   = V4L2_PIX_FMT_UYVY,
+		.depth    = 16,
+	},
 };
 
 static struct vivi_fmt *get_format(struct v4l2_format *f)
@@ -282,6 +287,20 @@ static void gen_twopix(struct vivi_fh *f
 				break;
 			}
 			break;
+		case V4L2_PIX_FMT_UYVY:
+			switch (color) {
+			case 1:
+			case 3:
+				*p = r_y;
+				break;
+			case 0:
+				*p = g_u;
+				break;
+			case 2:
+				*p = b_v;
+				break;
+			}
+			break;
 		}
 	}
 }
@@ -756,6 +775,7 @@ static int vidioc_s_fmt_vid_cap(struct f
 
 		switch (fh->fmt->fourcc) {
 		case V4L2_PIX_FMT_YUYV:
+		case V4L2_PIX_FMT_UYVY:
 			is_yuv = 1;
 			break;
 		}

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#4 lore raw

PATCH 4/5 video: Add support for rgb565 pixel formats to vivi

In reply to Magnus Damm

From: Magnus Damm <[email protected]>

This patch adds RGB565 pixel format support to the vivi driver. Both little endian and big endian versions are added. The driver follows the RGB pixel format described in Table 2-2 of the V4L2 API spec, _not_ the older BGR interpretation described in Table 2-1.

---

 drivers/media/video/vivi.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
drivers/media/video/vivi.c +40 -0
@@ -139,6 +139,16 @@ static struct vivi_fmt formats[] = {
 		.fourcc   = V4L2_PIX_FMT_UYVY,
 		.depth    = 16,
 	},
+	{
+		.name     = "RGB565 (LE)",
+		.fourcc   = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
+		.depth    = 16,
+	},
+	{
+		.name     = "RGB565 (BE)",
+		.fourcc   = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
+		.depth    = 16,
+	},
 };
 
 static struct vivi_fmt *get_format(struct v4l2_format *f)
@@ -301,6 +311,30 @@ static void gen_twopix(struct vivi_fh *f
 				break;
 			}
 			break;
+		case V4L2_PIX_FMT_RGB565:
+			switch (color) {
+			case 0:
+			case 2:
+				*p = (g_u << 5) | b_v;
+				break;
+			case 1:
+			case 3:
+				*p = (r_y << 3) | (g_u >> 3);
+				break;
+			}
+			break;
+		case V4L2_PIX_FMT_RGB565X:
+			switch (color) {
+			case 0:
+			case 2:
+				*p = (r_y << 3) | (g_u >> 3);
+				break;
+			case 1:
+			case 3:
+				*p = (g_u << 5) | b_v;
+				break;
+			}
+			break;
 		}
 	}
 }
@@ -778,6 +812,12 @@ static int vidioc_s_fmt_vid_cap(struct f
 		case V4L2_PIX_FMT_UYVY:
 			is_yuv = 1;
 			break;
+		case V4L2_PIX_FMT_RGB565:
+		case V4L2_PIX_FMT_RGB565X:
+			r >>= 3;
+			g >>= 2;
+			b >>= 3;
+			break;
 		}
 
 		if (is_yuv) {

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

#5 lore raw

PATCH 5/5 video: Add support for rgb555 pixel formats to vivi

In reply to Magnus Damm

From: Magnus Damm <[email protected]>

This patch adds RGB555 pixel format support to the vivi driver. Both little endian and big endian versions are added. The driver follows the RGB pixel format described in Table 2-2 of the V4L2 API spec, _not_ the older BGR interpretation described in Table 2-1.

---

 drivers/media/video/vivi.c |   40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
drivers/media/video/vivi.c +40 -0
@@ -149,6 +149,16 @@ static struct vivi_fmt formats[] = {
 		.fourcc   = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
 		.depth    = 16,
 	},
+	{
+		.name     = "RGB555 (LE)",
+		.fourcc   = V4L2_PIX_FMT_RGB555, /* gggbbbbb arrrrrgg */
+		.depth    = 16,
+	},
+	{
+		.name     = "RGB555 (BE)",
+		.fourcc   = V4L2_PIX_FMT_RGB555X, /* arrrrrgg gggbbbbb */
+		.depth    = 16,
+	},
 };
 
 static struct vivi_fmt *get_format(struct v4l2_format *f)
@@ -335,6 +345,30 @@ static void gen_twopix(struct vivi_fh *f
 				break;
 			}
 			break;
+		case V4L2_PIX_FMT_RGB555:
+			switch (color) {
+			case 0:
+			case 2:
+				*p = (g_u << 5) | b_v;
+				break;
+			case 1:
+			case 3:
+				*p = (r_y << 2) | (g_u >> 3);
+				break;
+			}
+			break;
+		case V4L2_PIX_FMT_RGB555X:
+			switch (color) {
+			case 0:
+			case 2:
+				*p = (r_y << 2) | (g_u >> 3);
+				break;
+			case 1:
+			case 3:
+				*p = (g_u << 5) | b_v;
+				break;
+			}
+			break;
 		}
 	}
 }
@@ -818,6 +852,12 @@ static int vidioc_s_fmt_vid_cap(struct f
 			g >>= 2;
 			b >>= 3;
 			break;
+		case V4L2_PIX_FMT_RGB555:
+		case V4L2_PIX_FMT_RGB555X:
+			r >>= 3;
+			g >>= 3;
+			b >>= 3;
+			break;
 		}
 
 		if (is_yuv) {

--
video4linux-list mailing list
Unsubscribe mailto:[email protected]?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list