Question

i have a an image size of 640 x480 and i want to use yv12 as an output format from that. the image is being processed by some API and i want to get the output in YV12 format.

What i am trying at the moment is that

int width = 640;
int height = 480;

byte[] byteArray = new byte[Convert.ToInt32((width * height + (2 * ((width * height) / 4))) * 1.5) ];

captureDevice.GetPreviewBufferYCbCr(byteArray)");

            int YLength = Convert.ToInt32(width * height * 1.5);

            // UVlength multipled by 2 because it is for both u and v
            int UVLength = Convert.ToInt32(2 * (width / 2) * (weight / 2)) * 1.5);

            var inputYBuffer = byteArray.AsBuffer(0, YLength);
            var inputUVBuffer = byteArray.AsBuffer(YLength, UVLength);


            var inputBuffers = new IBuffer[] { inputYBuffer, inputUVBuffer };
            var inputScanlines = new uint[] { (uint)YLength, (uint)UVLength  };
            /////Creating input buffer that supports Nv12 format

                Bitmap inputBtm = new Bitmap(
                outputBufferSize,
                ColorMode.Yvu420Sp,
                inputScanlines, // 1.5 bytes per pixel in  Yuv420Sp mode
                inputBuffers);



            //As v length is same as u length in output buffer so v length can be used in both place.
            int vLength = UVLength / 2;
            var outputYBuffer = byteArray.AsBuffer(0, YLength);
            var outputVBuffer = byteArray.AsBuffer(YLength, vLength);
            var outputUBuffer = byteArray.AsBuffer(YLength + vLength, vLength);

            var outputBuffers = new IBuffer[] { outputYBuffer, outputVBuffer, outputUBuffer };
            //
            var outputScanlines = new uint[] { (uint)YLength, (uint)vLength, (uint)vLength };


            Bitmap outputBtm = new Bitmap(
                outputBufferSize,
                ColorMode.Yuv420P,
                outputScanlines, 
                outputBuffers);**strong text**

so what i want to ask that am i creating the outputbuffer properly according to the format YUV420P. There is an API in which i am passing the input buffer in the of NV12 and i am assuming that it will give me the output buffer in YV12 (YUV420P) format. So i have created the Y plane and it have width x height x 1.5 bytes. 1.5 because YV12 is a 12 bits for mat and similarly U plane is having width/2 x height/2 x 1.5 bytes and similarly the V plane. I don't care at the moment about the YUV420P and YVU420P as long as my format is correct and i just have to swap and U and V planes.

Was it helpful?

Solution

YUV 4:2:0 planar looks like this:

----------------------
|     Y      | Cb|Cr |
----------------------

where:

Y = width x height pixels (bytes)
Cb = Y / 4 pixels (bytes)
Cr = Y / 4 pixels (bytes)

Total num pixels (bytes) = width * height * 3 / 2

This is how pixels are placed in 4:2:0 sub-sampling:

420

As you can see, each chroma value is shared between 4 luma-pixels.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top