Android org.webrtc.VideoRenderer.I420Frame arrays to PreviewCallback.onPreviewFrame byte[]

前端 未结 2 1766
说谎
说谎 2021-01-13 22:52

I keep hoping some code will appear on the internet, but getting nowhere ;)

WebRTC incoming I420Frame object seems to have 3 arrays of yuvPlanes

A typical An

2条回答
  •  既然无缘
    2021-01-13 23:51

    OK, here you go:

    // Copy the bytes out of |src| and into |dst|, ignoring and overwriting
    // positon & limit in both buffers.
    //** copied from org/webrtc/VideoRenderer.java **//
    private static void copyPlane(ByteBuffer src, ByteBuffer dst) {
      src.position(0).limit(src.capacity());
      dst.put(src);
      dst.position(0).limit(dst.capacity());
    }
    
    public static android.graphics.YuvImage ConvertTo(org.webrtc.VideoRenderer.I420Frame src, int imageFormat) {
        switch (imageFormat) {
        default:
            return null;
    
        case android.graphics.ImageFormat.YV12: {
            byte[] bytes = new byte[src.yuvStrides[0]*src.height +
                                src.yuvStrides[1]*src.height/2 + 
                                src.yuvStrides[2]*src.height/2];
            ByteBuffer tmp = ByteBuffer.wrap(bytes, 0, src.yuvStrides[0]*src.height);
            copyPlane(src.yuvPlanes[0], tmp);
            tmp = ByteBuffer.wrap(bytes, src.yuvStrides[0]*src.height, src.yuvStrides[2]*src.height/2);
            copyPlane(src.yuvPlanes[2], tmp);
            tmp = ByteBuffer.wrap(bytes, src.yuvStrides[0]*src.height+src.yuvStrides[2]*src.height/2, src.yuvStrides[1]*src.height/2);
            copyPlane(src.yuvPlanes[1], tmp);
            int[] strides = src.yuvStrides.clone();
            return new YuvImage(bytes, imageFormat, src.width, src.height, strides);
        }
    
        case android.graphics.ImageFormat.NV21: {
            if (src.yuvStrides[0] != src.width)
                return convertLineByLine(src);
            if (src.yuvStrides[1] != src.width/2)
                return convertLineByLine(src);
            if (src.yuvStrides[2] != src.width/2)
                return convertLineByLine(src);
    
            byte[] bytes = new byte[src.yuvStrides[0]*src.height +
                                src.yuvStrides[1]*src.height/2 + 
                                src.yuvStrides[2]*src.height/2];
            ByteBuffer tmp = ByteBuffer.wrap(bytes, 0, src.width*src.height);
            copyPlane(src.yuvPlanes[0], tmp);
    
            byte[] tmparray = new byte[src.width/2*src.height/2];
            tmp = ByteBuffer.wrap(tmparray, 0, src.width/2*src.height/2);
    
            copyPlane(src.yuvPlanes[2], tmp);
            for (int row=0; row

    This converts I420Frame to Android YuvImage of android.graphics.ImageFormat.NV21, which you can compressToJpeg(). ImageFormat.YV12 support seems to be limited in SDK. Note that the Y and V must be shuffled.

    Most error checking is skipped for brevity.

提交回复
热议问题