How to use NvBufferTransform to achieve YUYV data to GRAY8 conversion?

1.What is the difference between NvBufferPayload_SurfArray and NvBufferPayload_MemHandle?
2.What is the difference between NvBufferLayout_Pitch and NvBufferLayout_BlockLinear?

3.I want to convert YUYV format data to GREY format. How should I configure these two variables?

4.Implementing YUV to gray8 conversion using the following code, the resulting image has issues. Can you please help identify the error? Thank you.

void es_yuyv_2_grey(unsigned char *p_img_in, int width, int height, unsigned char *p_img_out)
{
	nv_buffer  buff_yuyv_buff = {0};
	nv_buffer * yuyv_buff = &buff_yuyv_buff;
	
    NvBufferCreateParams input_params = {0};
	
    input_params.payloadType 	= 	NvBufferPayload_SurfArray;
    input_params.width 			= 	width;
    input_params.height 		= 	height;
    input_params.layout 		= 	NvBufferLayout_Pitch;
	input_params.colorFormat 	= 	NvBufferColorFormat_YUYV;
	input_params.nvbuf_tag 		= 	NvBufferTag_VIDEO_CONVERT;
	
	if (-1 == NvBufferCreateEx(&yuyv_buff->dmabuff_fd, &input_params))
		ERROR_RETURN_NULL("Failed to create NvBuffer");
	if (-1 == NvBufferMemMap(yuyv_buff->dmabuff_fd, 0, NvBufferMem_Read_Write,
				(void**)&yuyv_buff->start))
	{		
		NvBufferDestroy(yuyv_buff->dmabuff_fd);
		ERROR_RETURN_NULL("Failed to map buffer");
	}
	
	nv_buffer  buff_grey_buff = {0};
	nv_buffer * grey_buff = &buff_grey_buff;		
        
	input_params.colorFormat = NvBufferColorFormat_GRAY8;
	
	if (-1 == NvBufferCreateEx(&grey_buff->dmabuff_fd, &input_params))
	{
		NvBufferMemUnMap(yuyv_buff->dmabuff_fd, 0, (void**)&yuyv_buff->start);
		NvBufferDestroy(yuyv_buff->dmabuff_fd);

		ERROR_RETURN_NULL("Failed to create NvBuffer");
	}
	if (-1 == NvBufferMemMap(grey_buff->dmabuff_fd, 0, NvBufferMem_Read_Write,
				(void**)&grey_buff->start))
	{
		NvBufferMemUnMap(yuyv_buff->dmabuff_fd, 0, (void**)&yuyv_buff->start);
		
		NvBufferDestroy(yuyv_buff->dmabuff_fd);
		NvBufferDestroy(grey_buff->dmabuff_fd);
		ERROR_RETURN_NULL("Failed to map buffer");
	}
	
	NvBufferTransformParams transParams;
	// Init the NvBufferTransformParams
	memset(&transParams, 0, sizeof(transParams));
	transParams.transform_flag = NVBUFFER_TRANSFORM_FILTER;
	transParams.transform_filter = NvBufferTransform_Filter_Smart;
	
	memcpy(yuyv_buff->start, p_img_in, width*height);
	
	NvBufferMemSyncForDevice(yuyv_buff->dmabuff_fd, 0, (void**)&yuyv_buff->start);
	
	// Convert the camera buffer from YUV422 to GRAY8
	if (-1 == NvBufferTransform(yuyv_buff->dmabuff_fd, grey_buff->dmabuff_fd,
				&transParams))
		printf("Failed to convert the buffer\n");

		
	memcpy(p_img_out, grey_buff->start, width*height);

	NvBufferMemUnMap(yuyv_buff->dmabuff_fd, 0, (void**)&yuyv_buff->start);
	NvBufferMemUnMap(grey_buff->dmabuff_fd, 0, (void**)&grey_buff->start);
	
	NvBufferDestroy(yuyv_buff->dmabuff_fd);
	NvBufferDestroy(grey_buff->dmabuff_fd);
	
}

Hi,
For format conversion, we have a sample code:

/usr/src/jetson_multimedia_api/samples/07_video_convert

Please take a look and give it a try.

For allocating buffers in specific format such as YUYV, GREY, RGBA, please always set to NvBufferPayload_SurfArray

Blockliner is private. Please always allocate to NvBufferLayout_Pitch

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.