| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import androidx.annotation.Nullable; |
| 14 | import java.nio.ByteBuffer; |
| 15 |
| 16 | public class NV12Buffer implements VideoFrame.Buffer { |
| 17 | private final int width; |
| 18 | private final int height; |
| 19 | private final int stride; |
| 20 | private final int sliceHeight; |
| 21 | private final ByteBuffer buffer; |
| 22 | private final RefCountDelegate refCountDelegate; |
| 23 |
| 24 | public NV12Buffer(int width, int height, int stride, int sliceHeight, ByteBuffer buffer, |
| 25 | @Nullable Runnable releaseCallback) { |
| 26 | this.width = width; |
| 27 | this.height = height; |
| 28 | this.stride = stride; |
| 29 | this.sliceHeight = sliceHeight; |
| 30 | this.buffer = buffer; |
| 31 | this.refCountDelegate = new RefCountDelegate(releaseCallback); |
| 32 | } |
| 33 |
| 34 | @Override |
| 35 | public int getWidth() { |
| 36 | return width; |
| 37 | } |
| 38 |
| 39 | @Override |
| 40 | public int getHeight() { |
| 41 | return height; |
| 42 | } |
| 43 |
| 44 | @Override |
| 45 | public VideoFrame.I420Buffer toI420() { |
| 46 | return (VideoFrame.I420Buffer) cropAndScale(0, 0, width, height, width, height); |
| 47 | } |
| 48 |
| 49 | @Override |
| 50 | public void retain() { |
| 51 | refCountDelegate.retain(); |
| 52 | } |
| 53 |
| 54 | @Override |
| 55 | public void release() { |
| 56 | refCountDelegate.release(); |
| 57 | } |
| 58 |
| 59 | @Override |
| 60 | public VideoFrame.Buffer cropAndScale( |
| 61 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
| 62 | JavaI420Buffer newBuffer = JavaI420Buffer.allocate(scaleWidth, scaleHeight); |
| 63 | nativeCropAndScale(cropX, cropY, cropWidth, cropHeight, scaleWidth, scaleHeight, buffer, width, |
| 64 | height, stride, sliceHeight, newBuffer.getDataY(), newBuffer.getStrideY(), |
| 65 | newBuffer.getDataU(), newBuffer.getStrideU(), newBuffer.getDataV(), newBuffer.getStrideV()); |
| 66 | return newBuffer; |
| 67 | } |
| 68 |
| 69 | private static native void nativeCropAndScale(int cropX, int cropY, int cropWidth, int cropHeight, |
| 70 | int scaleWidth, int scaleHeight, ByteBuffer src, int srcWidth, int srcHeight, int srcStride, |
| 71 | int srcSliceHeight, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, |
| 72 | ByteBuffer dstV, int dstStrideV); |
| 73 | } |
| 74 |