Need Sample to convert YUV420 buffer to RGB using NPP (Nvidia Performance Primitives).

Hi all,

Need to convert YUV420 buffer to RGB using NPP.

  1. YUV420 buffer : “uint8_t *yuv_image”
  2. Convert above buffer to RGB 8 bit buffer i.e. “uint8_t *yuv_image”
  3. Image width x height : 1280 x 720

How can I use NPP “nppiYCbCr420ToRGB_8u_P3C3R(…)” to achieve this. Is this NPP function correct or need to use another variant ?

I want to use the following NPP colour conversion Function with parameters:

NppStatus nppiYCbCr420ToRGB_8u_P3C3R ( const Npp8u *const * pSrc,
int nSrcStep[3],
Npp8u * pDst,
int nDstStep,
NppiSize oSizeROI)

Not able to find any sample except BoxFiler in sample codes for how to use NPP calls.

Thanks in advance,
Shanmuka G

If you have access to the device pointers you could simple write your own conversion kernel.

Should be pretty straightforward per pixel operation, something like:

__device__ void convertYUV2RGB(float* y, float* u, float* v, float *r, float *g, float *b)
{

	*r = y + (1.370705 * (v-128));
    *g = y - (0.698001 * (v-128)) - (0.337633 * (u-128));
    *b = y + (1.732446 * (u-128));
    *r = clamp(*r, 0, 255);
    *g = clamp(*g, 0, 255);
    *b = clamp(*b, 0, 255);

}

template<int DIM_X>
__global__ void d_convertYUV2RGB_kernel(float* d_y, float* d_u, float* d_v, float* d_r, float* d_g, float* d_b, int rows, int cols, int pitch)
{

	int tx = threadIdx.x + blockIdx.x*DIM_X;
	int ty = blockIdx.y;

	if( tx < cols && ty < rows)
	{

		int idx = tx + ty*pitch;

		float y = d_y[idx];
		float u = d_u[idx];
		float v = d_v[idx];

		float r = -1.0f;
		float g = -1.0f;
		float b = -1.0f;

		convertYUV2RGB(&y, &u, &v, &r, &g, &b);

		d_r[idx] = r;
		d_g[idx] = g;
		d_b[idx] = b;

	}
}

void convertYUV2RGB_kernel(float* d_y, float* d_u, float* d_v, float* d_r, float* d_g, float* d_b, int rows, int cols, int pitch)
{

	const int DIM_X = 128;

	int X_BLOCKS = (cols + DIM_X - 1)/DIM_X;
	int Y_BLOCKS = rows;

	dim3 grid(X_BLOCKS, Y_BLOCKS);

	d_convertYUV2RGB_kernel<DIM_X><<< grid, DIM_X>>>(d_y, d_u, d_v, d_r,d_g,_d_b, rows, cols, pitch);

}

Thanks Jimmy for your reply, your code is helpful but, I want to use NPP for image processing rather than the costume kernel…

Anyway thanks for your efforts, If you have any Samples related to NPP please share with me.