Convert YV12 to NV21 (YUV YCrCb 4:2:0)

匆匆过客 提交于 2019-12-08 08:42:13

问题


How can you convert:

YV12 (FOOURCC code: 0x32315659)
to
NV21 (FOURCC code: 0x3132564E)
(YCrCb 4:2:0 Planar)

These are both common formats for Android video handling, but there is no example online converting directly between the two. You can go through RGB but I assume that will be too inefficient.

Ideally in C# or Java, but can convert code from whatever else...

The input is a byte[], and the width and height are known.

I have been trying to follow the Wikipedia Article but cannot get it to function cleanly.

For the bounty: a function taking the byte[] and outputting a byte[] in the other format.


回答1:


This is my take on it. This is still untested but here it goes:

YV12 8 bit Y plane followed by 8 bit 2x2 subsampled V and U planes. So a single frame will have a full size Y plane followed 1/4th size V and U planes.

NV21 8-bit Y plane followed by an interleaved V/U plane with 2x2 subsampling. So a single frame will have a full size Y plane followed V and U in a 8 bit by bit blocks.

So here goes the code

public static byte[] YV12toNV21(final byte[] input,
                                final byte[] output, final int width, final int height) {

    final int size = width * height;
    final int quarter = size / 4;
    final int vPosition = size; // This is where V starts
    final int uPosition = size + quarter; // This is where U starts

    System.arraycopy(input, 0, output, 0, size); // Y is same

    for (int i = 0; i < quarter; i++) {
        output[size + i*2 ] = input[vPosition + i]; // For NV21, V first
        output[size + i*2 + 1] = input[uPosition + i]; // For Nv21, U second
    }
    return output;
}


来源:https://stackoverflow.com/questions/42000543/convert-yv12-to-nv21-yuv-ycrcb-420

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!