Android live video streaming (seven) NV21 conversion NV12

The previous chapter has successfully encoded YUV NV21 into H264 video stream, but because MediaCodec needs to input NV12, it needs to convert NV21 to NV12

Let's first look at the difference between NV21 and NV12.

Both NV21 and NV12 belong to YUV420, so the arrangement of Y is the same, the only difference is the arrangement of UV.

NV21 and NV12

We can see here that it is actually the position of the UV, which is lost, that is, we only need to exchange the data of U and V to achieve the effect we want:


/**
 * 把 NV21 的数据 转换成 NV12
 */
fun nv21ConvertNv12(data: ByteArray, width: Int, height: Int)
{
    var tmp: Byte
    for (index in width * height until data.size)
    {
        if ((index + 1) % 2 == 0)
        {
            tmp = data[index - 1]
            data[index - 1] = data[index]
            data[index] = tmp
        }
    }
}

Note that the data required here is still the width and height of the video, which is why the previous chapters have always emphasized that the width and height information is very important

We perform a conversion before encoding with MediaCodec, then encode and save it as an h264 file, and play it with the VLC player. This time, the effect is normally seen:

normal display

The video stream is solved, now the audio stream is left, wait for me to study... I'll copy the code...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325828209&siteId=291194637