Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 | |
Byoungchan Lee | 02334e0 | 2021-08-14 11:41:59 +0900 | [diff] [blame] | 13 | import androidx.annotation.Nullable; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 14 | import java.nio.ByteBuffer; |
| 15 | import org.webrtc.VideoFrame.I420Buffer; |
| 16 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 17 | /** Implementation of VideoFrame.I420Buffer backed by Java direct byte buffers. */ |
| 18 | public class JavaI420Buffer implements VideoFrame.I420Buffer { |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 19 | private final int width; |
| 20 | private final int height; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 21 | private final ByteBuffer dataY; |
| 22 | private final ByteBuffer dataU; |
| 23 | private final ByteBuffer dataV; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 24 | private final int strideY; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 25 | private final int strideU; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 26 | private final int strideV; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 27 | private final RefCountDelegate refCountDelegate; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 28 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 29 | private JavaI420Buffer(int width, int height, ByteBuffer dataY, int strideY, ByteBuffer dataU, |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 30 | int strideU, ByteBuffer dataV, int strideV, @Nullable Runnable releaseCallback) { |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 31 | this.width = width; |
| 32 | this.height = height; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 33 | this.dataY = dataY; |
| 34 | this.dataU = dataU; |
| 35 | this.dataV = dataV; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 36 | this.strideY = strideY; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 37 | this.strideU = strideU; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 38 | this.strideV = strideV; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 39 | this.refCountDelegate = new RefCountDelegate(releaseCallback); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 42 | private static void checkCapacity(ByteBuffer data, int width, int height, int stride) { |
| 43 | // The last row does not necessarily need padding. |
| 44 | final int minCapacity = stride * (height - 1) + width; |
| 45 | if (data.capacity() < minCapacity) { |
| 46 | throw new IllegalArgumentException( |
| 47 | "Buffer must be at least " + minCapacity + " bytes, but was " + data.capacity()); |
| 48 | } |
| 49 | } |
| 50 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 51 | /** Wraps existing ByteBuffers into JavaI420Buffer object without copying the contents. */ |
| 52 | public static JavaI420Buffer wrap(int width, int height, ByteBuffer dataY, int strideY, |
Sami Kalliomäki | 15f0a12 | 2018-08-03 14:06:59 +0200 | [diff] [blame] | 53 | ByteBuffer dataU, int strideU, ByteBuffer dataV, int strideV, |
| 54 | @Nullable Runnable releaseCallback) { |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 55 | if (dataY == null || dataU == null || dataV == null) { |
| 56 | throw new IllegalArgumentException("Data buffers cannot be null."); |
| 57 | } |
| 58 | if (!dataY.isDirect() || !dataU.isDirect() || !dataV.isDirect()) { |
| 59 | throw new IllegalArgumentException("Data buffers must be direct byte buffers."); |
| 60 | } |
| 61 | |
| 62 | // Slice the buffers to prevent external modifications to the position / limit of the buffer. |
| 63 | // Note that this doesn't protect the contents of the buffers from modifications. |
| 64 | dataY = dataY.slice(); |
| 65 | dataU = dataU.slice(); |
| 66 | dataV = dataV.slice(); |
| 67 | |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 68 | final int chromaWidth = (width + 1) / 2; |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 69 | final int chromaHeight = (height + 1) / 2; |
Magnus Jedvert | 6507054 | 2018-06-14 12:23:01 +0200 | [diff] [blame] | 70 | checkCapacity(dataY, width, height, strideY); |
| 71 | checkCapacity(dataU, chromaWidth, chromaHeight, strideU); |
| 72 | checkCapacity(dataV, chromaWidth, chromaHeight, strideV); |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 73 | |
| 74 | return new JavaI420Buffer( |
| 75 | width, height, dataY, strideY, dataU, strideU, dataV, strideV, releaseCallback); |
| 76 | } |
| 77 | |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 78 | /** Allocates an empty I420Buffer suitable for an image of the given dimensions. */ |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 79 | public static JavaI420Buffer allocate(int width, int height) { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 80 | int chromaHeight = (height + 1) / 2; |
| 81 | int strideUV = (width + 1) / 2; |
| 82 | int yPos = 0; |
| 83 | int uPos = yPos + width * height; |
| 84 | int vPos = uPos + strideUV * chromaHeight; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 85 | |
Sami Kalliomäki | 0611065 | 2018-02-20 11:19:58 +0100 | [diff] [blame] | 86 | ByteBuffer buffer = |
| 87 | JniCommon.nativeAllocateByteBuffer(width * height + 2 * strideUV * chromaHeight); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 88 | |
| 89 | buffer.position(yPos); |
| 90 | buffer.limit(uPos); |
| 91 | ByteBuffer dataY = buffer.slice(); |
| 92 | |
| 93 | buffer.position(uPos); |
| 94 | buffer.limit(vPos); |
| 95 | ByteBuffer dataU = buffer.slice(); |
| 96 | |
| 97 | buffer.position(vPos); |
| 98 | buffer.limit(vPos + strideUV * chromaHeight); |
| 99 | ByteBuffer dataV = buffer.slice(); |
| 100 | |
Sami Kalliomäki | 0611065 | 2018-02-20 11:19:58 +0100 | [diff] [blame] | 101 | return new JavaI420Buffer(width, height, dataY, width, dataU, strideUV, dataV, strideUV, |
| 102 | () -> { JniCommon.nativeFreeByteBuffer(buffer); }); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public int getWidth() { |
| 107 | return width; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public int getHeight() { |
| 112 | return height; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public ByteBuffer getDataY() { |
Bjorn Mellem | d629314 | 2017-09-29 11:33:52 -0700 | [diff] [blame] | 117 | // Return a slice to prevent relative reads from changing the position. |
| 118 | return dataY.slice(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public ByteBuffer getDataU() { |
Bjorn Mellem | d629314 | 2017-09-29 11:33:52 -0700 | [diff] [blame] | 123 | // Return a slice to prevent relative reads from changing the position. |
| 124 | return dataU.slice(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public ByteBuffer getDataV() { |
Bjorn Mellem | d629314 | 2017-09-29 11:33:52 -0700 | [diff] [blame] | 129 | // Return a slice to prevent relative reads from changing the position. |
| 130 | return dataV.slice(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int getStrideY() { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 135 | return strideY; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public int getStrideU() { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 140 | return strideU; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public int getStrideV() { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 145 | return strideV; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public I420Buffer toI420() { |
sakal | 5ca60cc | 2017-08-09 05:25:49 -0700 | [diff] [blame] | 150 | retain(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 151 | return this; |
| 152 | } |
| 153 | |
| 154 | @Override |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 155 | public void retain() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 156 | refCountDelegate.retain(); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 157 | } |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 158 | |
| 159 | @Override |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 160 | public void release() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 161 | refCountDelegate.release(); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 162 | } |
| 163 | |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public VideoFrame.Buffer cropAndScale( |
| 166 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
Sami Kalliomäki | ae1bcea | 2018-07-24 16:59:05 +0200 | [diff] [blame] | 167 | return cropAndScaleI420(this, cropX, cropY, cropWidth, cropHeight, scaleWidth, scaleHeight); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 168 | } |
Sami Kalliomäki | ae1bcea | 2018-07-24 16:59:05 +0200 | [diff] [blame] | 169 | |
| 170 | public static VideoFrame.Buffer cropAndScaleI420(final I420Buffer buffer, int cropX, int cropY, |
| 171 | int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
| 172 | if (cropWidth == scaleWidth && cropHeight == scaleHeight) { |
| 173 | // No scaling. |
| 174 | ByteBuffer dataY = buffer.getDataY(); |
| 175 | ByteBuffer dataU = buffer.getDataU(); |
| 176 | ByteBuffer dataV = buffer.getDataV(); |
| 177 | |
| 178 | dataY.position(cropX + cropY * buffer.getStrideY()); |
| 179 | dataU.position(cropX / 2 + cropY / 2 * buffer.getStrideU()); |
| 180 | dataV.position(cropX / 2 + cropY / 2 * buffer.getStrideV()); |
| 181 | |
| 182 | buffer.retain(); |
| 183 | return JavaI420Buffer.wrap(scaleWidth, scaleHeight, dataY.slice(), buffer.getStrideY(), |
| 184 | dataU.slice(), buffer.getStrideU(), dataV.slice(), buffer.getStrideV(), buffer::release); |
| 185 | } |
| 186 | |
| 187 | JavaI420Buffer newBuffer = JavaI420Buffer.allocate(scaleWidth, scaleHeight); |
| 188 | nativeCropAndScaleI420(buffer.getDataY(), buffer.getStrideY(), buffer.getDataU(), |
| 189 | buffer.getStrideU(), buffer.getDataV(), buffer.getStrideV(), cropX, cropY, cropWidth, |
| 190 | cropHeight, newBuffer.getDataY(), newBuffer.getStrideY(), newBuffer.getDataU(), |
| 191 | newBuffer.getStrideU(), newBuffer.getDataV(), newBuffer.getStrideV(), scaleWidth, |
| 192 | scaleHeight); |
| 193 | return newBuffer; |
| 194 | } |
| 195 | |
| 196 | private static native void nativeCropAndScaleI420(ByteBuffer srcY, int srcStrideY, |
| 197 | ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, int cropX, int cropY, |
| 198 | int cropWidth, int cropHeight, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 199 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int scaleWidth, int scaleHeight); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 200 | } |