diff mbox

[v2,2/2] libv4lconvert: Support for RGB32 and BGR32 format

Message ID 1375483372-4354-3-git-send-email-ricardo.ribalda@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ricardo Ribalda Delgado Aug. 2, 2013, 10:42 p.m. UTC
This patch adds support for V4L2_PIX_FMT_BGR32 and V4L2_PIX_FMT_BGR32
formats.

Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
---
 lib/libv4lconvert/libv4lconvert-priv.h |    5 ++-
 lib/libv4lconvert/libv4lconvert.c      |   58 ++++++++++++++++++++++++++++----
 lib/libv4lconvert/rgbyuv.c             |   45 +++++++++++++++++++------
 3 files changed, 90 insertions(+), 18 deletions(-)

Comments

Gregor Jasny Aug. 3, 2013, 4:42 p.m. UTC | #1
On 8/3/13 12:42 AM, Ricardo Ribalda Delgado wrote:
> +	case V4L2_PIX_FMT_RGB32:
> +		switch (dest_pix_fmt) {
> +		case V4L2_PIX_FMT_RGB24:
> +			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 0);
> +			break;
> +		case V4L2_PIX_FMT_BGR24:
> +			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 1);
> +			break;
> +		case V4L2_PIX_FMT_YUV420:
> +			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 4);
> +			break;
> +		case V4L2_PIX_FMT_YVU420:
> +			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 4);
> +			break;
> +		}
> +		if (src_size < (width * height * 4)) {
> +			V4LCONVERT_ERR("short rgb32 data frame\n");
> +			errno = EPIPE;
> +			result = -1;
> +		}
> +		break;

I have not looked at the whole function but shouldn't this sanity check 
happen before the actual work? Also aren't you applying the condition 
here also for rgb24_to_xxx which should have only three bpp?

> +	case V4L2_PIX_FMT_BGR32:
> +		switch (dest_pix_fmt) {
> +		case V4L2_PIX_FMT_RGB24:
> +			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 1);
> +			break;
> +		case V4L2_PIX_FMT_BGR24:
> +			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 0);
> +			break;
> +		case V4L2_PIX_FMT_YUV420:
> +			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0, 4);
> +			break;
> +		case V4L2_PIX_FMT_YVU420:
> +			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1, 4);
> +			break;
> +		}
> +		if (src_size < (width * height * 4)) {
> +			V4LCONVERT_ERR("short bgr32 data frame\n");
> +			errno = EPIPE;
> +			result = -1;
> +		}
> +		break;

Same here. And also in the other patch.


--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Ricardo Ribalda Delgado Aug. 4, 2013, 8:05 a.m. UTC | #2
Hello Gregor

Thanks for your comments. I have replied inline.

On Sat, Aug 3, 2013 at 6:42 PM, Gregor Jasny <gjasny@googlemail.com> wrote:
> On 8/3/13 12:42 AM, Ricardo Ribalda Delgado wrote:
>>
>> +       case V4L2_PIX_FMT_RGB32:
>> +               switch (dest_pix_fmt) {
>> +               case V4L2_PIX_FMT_RGB24:
>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>> height, 0);
>> +                       break;
>> +               case V4L2_PIX_FMT_BGR24:
>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>> height, 1);
>> +                       break;
>> +               case V4L2_PIX_FMT_YUV420:
>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0,
>> 4);
>> +                       break;
>> +               case V4L2_PIX_FMT_YVU420:
>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1,
>> 4);
>> +                       break;
>> +               }
>> +               if (src_size < (width * height * 4)) {
>> +                       V4LCONVERT_ERR("short rgb32 data frame\n");
>> +                       errno = EPIPE;
>> +                       result = -1;
>> +               }
>> +               break;
>
>
> I have not looked at the whole function but shouldn't this sanity check
> happen before the actual work?

Yes, but it is how it is done in the whole library with all the
formats. Please grep for "short " on libv4lconvert.c

> Also aren't you applying the condition here
> also for rgb24_to_xxx which should have only three bpp?
>

I have modified the function rgb24_to_yuv420 to support other bytes per pixel.

>
>> +       case V4L2_PIX_FMT_BGR32:
>> +               switch (dest_pix_fmt) {
>> +               case V4L2_PIX_FMT_RGB24:
>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>> height, 1);
>> +                       break;
>> +               case V4L2_PIX_FMT_BGR24:
>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>> height, 0);
>> +                       break;
>> +               case V4L2_PIX_FMT_YUV420:
>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0,
>> 4);
>> +                       break;
>> +               case V4L2_PIX_FMT_YVU420:
>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1,
>> 4);
>> +                       break;
>> +               }
>> +               if (src_size < (width * height * 4)) {
>> +                       V4LCONVERT_ERR("short bgr32 data frame\n");
>> +                       errno = EPIPE;
>> +                       result = -1;
>> +               }
>> +               break;
>
>
> Same here. And also in the other patch.
>
>

Thanks again
Ricardo Ribalda Delgado Aug. 9, 2013, 4:03 p.m. UTC | #3
ping?

On Sun, Aug 4, 2013 at 10:05 AM, Ricardo Ribalda Delgado
<ricardo.ribalda@gmail.com> wrote:
> Hello Gregor
>
> Thanks for your comments. I have replied inline.
>
> On Sat, Aug 3, 2013 at 6:42 PM, Gregor Jasny <gjasny@googlemail.com> wrote:
>> On 8/3/13 12:42 AM, Ricardo Ribalda Delgado wrote:
>>>
>>> +       case V4L2_PIX_FMT_RGB32:
>>> +               switch (dest_pix_fmt) {
>>> +               case V4L2_PIX_FMT_RGB24:
>>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>>> height, 0);
>>> +                       break;
>>> +               case V4L2_PIX_FMT_BGR24:
>>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>>> height, 1);
>>> +                       break;
>>> +               case V4L2_PIX_FMT_YUV420:
>>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0,
>>> 4);
>>> +                       break;
>>> +               case V4L2_PIX_FMT_YVU420:
>>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1,
>>> 4);
>>> +                       break;
>>> +               }
>>> +               if (src_size < (width * height * 4)) {
>>> +                       V4LCONVERT_ERR("short rgb32 data frame\n");
>>> +                       errno = EPIPE;
>>> +                       result = -1;
>>> +               }
>>> +               break;
>>
>>
>> I have not looked at the whole function but shouldn't this sanity check
>> happen before the actual work?
>
> Yes, but it is how it is done in the whole library with all the
> formats. Please grep for "short " on libv4lconvert.c
>
>> Also aren't you applying the condition here
>> also for rgb24_to_xxx which should have only three bpp?
>>
>
> I have modified the function rgb24_to_yuv420 to support other bytes per pixel.
>
>>
>>> +       case V4L2_PIX_FMT_BGR32:
>>> +               switch (dest_pix_fmt) {
>>> +               case V4L2_PIX_FMT_RGB24:
>>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>>> height, 1);
>>> +                       break;
>>> +               case V4L2_PIX_FMT_BGR24:
>>> +                       v4lconvert_rgb32_to_rgb24(src, dest, width,
>>> height, 0);
>>> +                       break;
>>> +               case V4L2_PIX_FMT_YUV420:
>>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0,
>>> 4);
>>> +                       break;
>>> +               case V4L2_PIX_FMT_YVU420:
>>> +                       v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1,
>>> 4);
>>> +                       break;
>>> +               }
>>> +               if (src_size < (width * height * 4)) {
>>> +                       V4LCONVERT_ERR("short bgr32 data frame\n");
>>> +                       errno = EPIPE;
>>> +                       result = -1;
>>> +               }
>>> +               break;
>>
>>
>> Same here. And also in the other patch.
>>
>>
>
> Thanks again
>
> --
> Ricardo Ribalda
diff mbox

Patch

diff --git a/lib/libv4lconvert/libv4lconvert-priv.h b/lib/libv4lconvert/libv4lconvert-priv.h
index 6422fdd..ac1391e 100644
--- a/lib/libv4lconvert/libv4lconvert-priv.h
+++ b/lib/libv4lconvert/libv4lconvert-priv.h
@@ -108,7 +108,7 @@  unsigned char *v4lconvert_alloc_buffer(int needed,
 int v4lconvert_oom_error(struct v4lconvert_data *data);
 
 void v4lconvert_rgb24_to_yuv420(const unsigned char *src, unsigned char *dest,
-		const struct v4l2_format *src_fmt, int bgr, int yvu);
+		const struct v4l2_format *src_fmt, int bgr, int yvu, int bpp);
 
 void v4lconvert_yuv420_to_rgb24(const unsigned char *src, unsigned char *dst,
 		int width, int height, int yvu);
@@ -158,6 +158,9 @@  void v4lconvert_y16_to_rgb24(const unsigned char *src, unsigned char *dest,
 void v4lconvert_y16_to_yuv420(const unsigned char *src, unsigned char *dest,
 		const struct v4l2_format *src_fmt);
 
+void v4lconvert_rgb32_to_rgb24(const unsigned char *src, unsigned char *dest,
+		int width, int height, int bgr);
+
 int v4lconvert_y10b_to_rgb24(struct v4lconvert_data *data,
 	const unsigned char *src, unsigned char *dest, int width, int height);
 
diff --git a/lib/libv4lconvert/libv4lconvert.c b/lib/libv4lconvert/libv4lconvert.c
index bc5e34f..2aec99a 100644
--- a/lib/libv4lconvert/libv4lconvert.c
+++ b/lib/libv4lconvert/libv4lconvert.c
@@ -84,6 +84,8 @@  static const struct v4lconvert_pixfmt supported_src_pixfmts[] = {
 	SUPPORTED_DST_PIXFMTS,
 	/* packed rgb formats */
 	{ V4L2_PIX_FMT_RGB565,		16,	 4,	 6,	0 },
+	{ V4L2_PIX_FMT_BGR32,		32,	 4,	 6,	0 },
+	{ V4L2_PIX_FMT_RGB32,		32,	 4,	 6,	0 },
 	/* yuv 4:2:2 formats */
 	{ V4L2_PIX_FMT_YUYV,		16,	 5,	 4,	0 },
 	{ V4L2_PIX_FMT_YVYU,		16,	 5,	 4,	0 },
@@ -981,10 +983,10 @@  static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
 			v4lconvert_swap_rgb(d, dest, width, height);
 			break;
 		case V4L2_PIX_FMT_YUV420:
-			v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 0);
+			v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 0, 3);
 			break;
 		case V4L2_PIX_FMT_YVU420:
-			v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 1);
+			v4lconvert_rgb24_to_yuv420(d, dest, fmt, 0, 1, 3);
 			break;
 		}
 		break;
@@ -1079,10 +1081,10 @@  static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
 			v4lconvert_swap_rgb(src, dest, width, height);
 			break;
 		case V4L2_PIX_FMT_YUV420:
-			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0);
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 3);
 			break;
 		case V4L2_PIX_FMT_YVU420:
-			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1);
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 3);
 			break;
 		}
 		if (src_size < (width * height * 3)) {
@@ -1101,10 +1103,10 @@  static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
 			memcpy(dest, src, width * height * 3);
 			break;
 		case V4L2_PIX_FMT_YUV420:
-			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0);
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0, 3);
 			break;
 		case V4L2_PIX_FMT_YVU420:
-			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1);
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1, 3);
 			break;
 		}
 		if (src_size < (width * height * 3)) {
@@ -1114,6 +1116,50 @@  static int v4lconvert_convert_pixfmt(struct v4lconvert_data *data,
 		}
 		break;
 
+	case V4L2_PIX_FMT_RGB32:
+		switch (dest_pix_fmt) {
+		case V4L2_PIX_FMT_RGB24:
+			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 0);
+			break;
+		case V4L2_PIX_FMT_BGR24:
+			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 1);
+			break;
+		case V4L2_PIX_FMT_YUV420:
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 0, 4);
+			break;
+		case V4L2_PIX_FMT_YVU420:
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 0, 1, 4);
+			break;
+		}
+		if (src_size < (width * height * 4)) {
+			V4LCONVERT_ERR("short rgb32 data frame\n");
+			errno = EPIPE;
+			result = -1;
+		}
+		break;
+
+	case V4L2_PIX_FMT_BGR32:
+		switch (dest_pix_fmt) {
+		case V4L2_PIX_FMT_RGB24:
+			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 1);
+			break;
+		case V4L2_PIX_FMT_BGR24:
+			v4lconvert_rgb32_to_rgb24(src, dest, width, height, 0);
+			break;
+		case V4L2_PIX_FMT_YUV420:
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 0, 4);
+			break;
+		case V4L2_PIX_FMT_YVU420:
+			v4lconvert_rgb24_to_yuv420(src, dest, fmt, 1, 1, 4);
+			break;
+		}
+		if (src_size < (width * height * 4)) {
+			V4LCONVERT_ERR("short bgr32 data frame\n");
+			errno = EPIPE;
+			result = -1;
+		}
+		break;
+
 	case V4L2_PIX_FMT_YUV420:
 		switch (dest_pix_fmt) {
 		case V4L2_PIX_FMT_RGB24:
diff --git a/lib/libv4lconvert/rgbyuv.c b/lib/libv4lconvert/rgbyuv.c
index bef034f..d2f11bf 100644
--- a/lib/libv4lconvert/rgbyuv.c
+++ b/lib/libv4lconvert/rgbyuv.c
@@ -35,7 +35,7 @@ 
 	} while (0)
 
 void v4lconvert_rgb24_to_yuv420(const unsigned char *src, unsigned char *dest,
-		const struct v4l2_format *src_fmt, int bgr, int yvu)
+		const struct v4l2_format *src_fmt, int bgr, int yvu, int bpp)
 {
 	int x, y;
 	unsigned char *udest, *vdest;
@@ -47,9 +47,10 @@  void v4lconvert_rgb24_to_yuv420(const unsigned char *src, unsigned char *dest,
 				RGB2Y(src[2], src[1], src[0], *dest++);
 			else
 				RGB2Y(src[0], src[1], src[2], *dest++);
-			src += 3;
+			src += bpp;
 		}
-		src += src_fmt->fmt.pix.bytesperline - 3 * src_fmt->fmt.pix.width;
+
+		src += src_fmt->fmt.pix.bytesperline - bpp * src_fmt->fmt.pix.width;
 	}
 	src -= src_fmt->fmt.pix.height * src_fmt->fmt.pix.bytesperline;
 
@@ -66,19 +67,19 @@  void v4lconvert_rgb24_to_yuv420(const unsigned char *src, unsigned char *dest,
 		for (x = 0; x < src_fmt->fmt.pix.width / 2; x++) {
 			int avg_src[3];
 
-			avg_src[0] = (src[0] + src[3] + src[src_fmt->fmt.pix.bytesperline] +
-					src[src_fmt->fmt.pix.bytesperline + 3]) / 4;
-			avg_src[1] = (src[1] + src[4] + src[src_fmt->fmt.pix.bytesperline + 1] +
-					src[src_fmt->fmt.pix.bytesperline + 4]) / 4;
-			avg_src[2] = (src[2] + src[5] + src[src_fmt->fmt.pix.bytesperline + 2] +
-					src[src_fmt->fmt.pix.bytesperline + 5]) / 4;
+			avg_src[0] = (src[0] + src[bpp] + src[src_fmt->fmt.pix.bytesperline] +
+					src[src_fmt->fmt.pix.bytesperline + bpp]) / 4;
+			avg_src[1] = (src[1] + src[bpp + 1] + src[src_fmt->fmt.pix.bytesperline + 1] +
+					src[src_fmt->fmt.pix.bytesperline + bpp + 1]) / 4;
+			avg_src[2] = (src[2] + src[bpp + 2] + src[src_fmt->fmt.pix.bytesperline + 2] +
+					src[src_fmt->fmt.pix.bytesperline + bpp + 2]) / 4;
 			if (bgr)
 				RGB2UV(avg_src[2], avg_src[1], avg_src[0], *udest++, *vdest++);
 			else
 				RGB2UV(avg_src[0], avg_src[1], avg_src[2], *udest++, *vdest++);
-			src += 6;
+			src += 2 * bpp;
 		}
-		src += 2 * src_fmt->fmt.pix.bytesperline - 3 * src_fmt->fmt.pix.width;
+		src += 2 * src_fmt->fmt.pix.bytesperline - bpp * src_fmt->fmt.pix.width;
 	}
 }
 
@@ -725,3 +726,25 @@  int v4lconvert_y10b_to_yuv420(struct v4lconvert_data *data,
 
 	return 0;
 }
+
+void v4lconvert_rgb32_to_rgb24(const unsigned char *src, unsigned char *dest,
+		int width, int height,int bgr)
+{
+	int j;
+	while (--height >= 0) {
+		for (j = 0; j < width; j++) {
+			if (bgr){
+				*dest++ = src[2];
+				*dest++ = src[1];
+				*dest++ = src[0];
+				src+=4;
+			}
+			else{
+				*dest++ = *src++;
+				*dest++ = *src++;
+				*dest++ = *src++;
+				src+=1;
+			}
+		}
+	}
+}