将YV12转换为NV21(YUV YCrCb 4:2:0) [英] Convert YV12 to NV21 (YUV YCrCb 4:2:0)

查看:159
本文介绍了将YV12转换为NV21(YUV YCrCb 4:2:0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换:

YV12 (FOOURCC代码:0x32315659)

NV21 (FOURCC代码:0x3132564E)
(YCrCb 4:2:0平面)

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

这两种都是Android视频处理的常用格式,但是没有直接在两者之间进行在线转换的示例.您可以使用RGB,但我认为这样做效率太低.

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.

理想情况是使用 C# Java ,但是可以转换其他代码...

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

输入的是 byte [] ,并且宽度和高度是已知的.

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

我一直在尝试遵循 Wikipedia文章,但无法使其正常运行.

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

为了获得赏金,一个函数采用byte []并以另一种格式输出byte [].

推荐答案

这是我的看法.这仍然未经测试,但是可以解决:

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

YV12 8位Y平面,然后是8位2x2二次采样的V和U平面.因此,单个帧将具有完整尺寸的Y平面,紧随其后的是1/4尺寸的V和U平面.

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位Y平面,然后是具有2x2子采样的交错V/U平面.因此,单个帧将具有完整大小的Y平面,后跟V和U的像素为8位.

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.

代码在这里

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;
}

这篇关于将YV12转换为NV21(YUV YCrCb 4:2:0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆