Create org.webrtc.JavaI420Buffer from YUV420 ByteBuffer

431 views Asked by At

Hi I am working on a live streaming solution where I need to edit the local video frame and send it to the connected peer, for this task I am editing https://webrtc.googlesource.com/src/+/master/sdk/android/src/java/org/webrtc/Camera2Session.java#201, the VideoFrame from here is processed to the editing engine, later on it's return a YUV420 ByteBuffer with following values:

  • frameWidth=1280
  • frameHeight=720
  • RowStrideY:1280
  • RowStrideU:640
  • RowStrideV:640
  • PlaneHeightY:720
  • PlaneHeightU:360
  • PlaneHeightV:360
  • PlaneOffsetY:0
  • PlaneOffsetU:921600
  • PlaneOffsetV:1152000
  • PixelStrideY=1
  • PixelStrideU=1
  • PixelStrideV=1

Now I want to recreate the VideoFrame object from this details, I tried various approaches to achieve this and almost very close to solution only having trouble to seprate the Y,U and V ByteBuffer from the ByteBuffer, due to which the output is little bit destorted.

enter image description here

Following code is been used by me for the conversions:

public static I420Buffer convert(ImageProcessResult result) {
    int frameWidth = result.getWidth();
    int frameHeight = result.getHeight();

    int rowStrideY = result.getRowStride(0);
    int rowStrideU = result.getRowStride(1);
    int rowStrideV = result.getRowStride(2);

    int offsetY = result.getPlaneOffset(0);
    int offsetU = result.getPlaneOffset(1);
    int offsetV = result.getPlaneOffset(2);

    ByteBuffer i420ByteBuffer = result.getBuffer();
    i420ByteBuffer.position(offsetY);
    final ByteBuffer dataY = i420ByteBuffer.slice();

    i420ByteBuffer.position(offsetU);
    final ByteBuffer dataU = i420ByteBuffer.slice();

    i420ByteBuffer.position(offsetV);
    final ByteBuffer dataV = i420ByteBuffer.slice();

    JavaI420Buffer frame = JavaI420Buffer.wrap(frameWidth, frameHeight, dataY, rowStrideY, dataU, rowStrideU, dataV, rowStrideV,
            () -> {
                JniCommon.nativeFreeByteBuffer(i420ByteBuffer);
            });
    return frame;
}

Any help to fix this issue will be appreciated, thanks in advance..!

0

There are 0 answers