YUV to RG color conversion with G2D problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

YUV to RG color conversion with G2D problem

2,057 Views
ivan_garcia
Contributor I

Hi!
We are trying to convert YUV frames captured using libuvc (usb cameras) to RGB, but we are not able to get the right color.

The format that libuvc gives the frames in is YUV422 acording with the headers:

enum uvc_frame_format {
....
UVC_FRAME_FORMAT_COMPRESSED,
/** YUYV/YUV2/YUV422: YUV encoding with one luminance value per pixel and
* one UV (chrominance) pair for every two pixels.
*/

With dimensions 1920x1024, stride 3840 (1920*2)

And the code we are using to convert the color is:

cv::Mat ConvertFrame(uvc_frame_t *frame, int inputFormat=G2D_YUYV){
void *G2D_Handler=NULL;
if (g2d_open(&G2D_Handler)!=0){
puts("G2D_Start: Error opening g2d device");
};

int size = frame->data_bytes;
struct g2d_buf *srcM = g2d_alloc (size,1);
struct g2d_buf *dstM = g2d_alloc (frame->width * frame->height * 3,1);
memcpy(srcM->buf_vaddr, frame->data, size);

struct g2d_surface src,dst;
src.planes[0] = srcM->buf_paddr;
src.planes[1] = 0; //srcM->buf_paddr;
src.planes[2] = 0; //srcM->buf_paddr;
src.left = 0;
src.top = 0;
src.right = frame->width;
src.bottom = frame->height;
src.stride = frame->step;
src.width = frame->width;
src.height = frame->height;
src.rot = G2D_ROTATION_0;
src.format = inputFormat;


cv::Mat out(frame->height, frame->width, CV_8UC3, dstM->buf_vaddr );
dst.planes[0] = dstM->buf_paddr;
dst.left = 0;
dst.top = 0;
dst.right = out.cols;
dst.bottom = out.rows;
dst.stride = out.step1();
dst.width = out.cols;
dst.height = out.rows;
dst.rot = G2D_ROTATION_0;
dst.format = G2D_RGB888 ;

int r = g2d_blit(G2D_Handler, &src, &dst);
if (r!=0) puts("convertFrame :: ERROR g2d_blit");
r=g2d_finish(G2D_Handler);
if (r!=0) puts("convertFrame :: ERROR g2d_finish");

char name[128];
snprintf(name,128,"g2dOutput_%d.png", inputFormat );
cv::imwrite(name,out);

g2d_free (srcM);
return out;
}

 

The better results we could get are with inputFormat = G2D_YUYV, but as can be seen in the attached image, this is far to be correct.

 

Can someone give us an idea on how to solve this problem?

Thanks in advance!

0 Kudos
Reply
6 Replies

1,611 Views
ivan_garcia
Contributor I

Hi everybody,

Yes, I was able to fix the problem:
It was only a problem of the sizes used.

void *G2D::convert(void *imageBuffer, int width, int height){
		int size = width * height *2;

		struct g2d_surface src,dst;
		src.planes[0] = srcM->buf_paddr;
		src.planes[1] = 0;
		src.planes[2] = 0;
		src.left = 0;
		src.top = 0;
		src.right = width;
		src.bottom = height/2;
		src.stride = width *2;
		src.width = width;
		src.height = height/2;
		src.rot = G2D_ROTATION_0;
		src.format = G2D_YUYV;


		dst.planes[0] = dstM->buf_paddr;
		dst.left = 0;
		dst.top = 0;
		dst.right = width;
		dst.bottom = height;
		dst.stride = width ;
		dst.width = width;
		dst.height = height;
		dst.rot = G2D_ROTATION_0;
		dst.format = G2D_RGB888 ;


		memcpy(srcM->buf_vaddr, imageBuffer, size);

		int r = g2d_blit(G2D_Handler, &src, &dst);
		if (r!=0) puts("convertFrame :: ERROR g2d_blit");
		r=g2d_finish(G2D_Handler);
		if (r!=0) puts("convertFrame :: ERROR g2d_finish");

		return dstM->buf_vaddr;
	}

Hope it is useful for you!

0 Kudos
Reply

1,607 Views
malik_cisse
Senior Contributor I

Thank you for the swift feedback Ivan,

I implemented your solution but my image is still broken (see attachment). I also attached the code, maybe you find something obvious.

What CPU are you using.

Not sure why you have 
src.bottom = height/2;
src.height = height/2;
Thx

0 Kudos
Reply

1,617 Views
malik_cisse
Senior Contributor I

Hi Ivan,

Could you solve this problem?
I am facing same issue.
Thx

0 Kudos
Reply

1,619 Views
malik_cisse
Senior Contributor I

Hello @Bio_TICFSL ,

I have exact same problem:

On NXP imx8mp G2D GPU (GC520L) YUYV to RGBX works fine:
G2D_YUYV to G2D_RGBX8888 works fine

However YUYV to RGB (24bit RGB888) does not work. I have same broken pictures as above.
G2D_YUYV to G2D_RGB888

Any hints how I can solve this problem?
Thank you

0 Kudos
Reply

2,035 Views
Bio_TICFSL
NXP TechSupport
NXP TechSupport

Hello ivan_garcia

Currently, libg2d does not support YUYV convert to BGR. I will discuss with R&D team to enable.

 

Regards

0 Kudos
Reply

1,914 Views
quercuspau
Contributor II

Hello @Bio_TICFSL ,

any news related to this topic?

BR

0 Kudos
Reply