Wednesday, February 06, 2008

YUV420P to YV12 format conversion

I converted the YUV420P to YV12 format using the following :


The YV12 format is essentially the same as YUV420p, but it has the U and V data reversed: the Y values are followed by the V values, with the U values last.Our output is YUV420P.

Our MPEG4 Decoder's output is YUV420P. So we need to convert it as
YV12. I used the following code to convert from YUV420P to YV12.

long ulNumberOfPixels = m_iWidth * m_iHeight;
long ulUVBufferSize = (m_iWidth * m_iHeight) /4;
unsigned char ch; for(long i = 0; i < ulUVBufferSize; i++)
{
ch = pbYUVData[ulNumberOfPixels + i] ;
pbYUVData[ulNumberOfPixels + i] = pbYUVData[ ulNumberOfPixels + ulUVBufferSize + i];
pbYUVData[ ulNumberOfPixels + ulUVBufferSize + i] = ch;
}

1 comment:

Payal said...

YV12 is a 4:2:0 YCrCb planar format comprised of a WxH Y plane followed by (W/2) x (H/2) Cr and Cb planes.

This format assumes

an even width
an even height
a horizontal stride multiple of 16 pixels
a vertical stride equal to the height
y_size = stride * height
c_stride = ALIGN(stride/2, 16)
c_size = c_stride * height/2
size = y_size + c_size * 2
cr_offset = y_size
cb_offset = y_size + c_size

Hence the buffer size of YV12 will be different from YUV420p. Also in case the width is not a multiple of 32, then the logic fails too.