| 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 android.graphics.Matrix; |
| 14 | import android.os.Handler; |
| 15 |
| 16 | import org.telegram.messenger.FileLog; |
| 17 |
| 18 | import java.nio.ByteBuffer; |
| 19 |
| 20 | import androidx.annotation.Nullable; |
| 21 |
| 22 | /** |
| 23 | * Android texture buffer that glues together the necessary information together with a generic |
| 24 | * release callback. ToI420() is implemented by providing a Handler and a YuvConverter. |
| 25 | */ |
| 26 | public class TextureBufferImpl implements VideoFrame.TextureBuffer { |
| 27 | interface RefCountMonitor { |
| 28 | void onRetain(TextureBufferImpl textureBuffer); |
| 29 | void onRelease(TextureBufferImpl textureBuffer); |
| 30 | void onDestroy(TextureBufferImpl textureBuffer); |
| 31 | } |
| 32 |
| 33 | // This is the full resolution the texture has in memory after applying the transformation matrix |
| 34 | // that might include cropping. This resolution is useful to know when sampling the texture to |
| 35 | // avoid downscaling artifacts. |
| 36 | private final int unscaledWidth; |
| 37 | private final int unscaledHeight; |
| 38 | // This is the resolution that has been applied after cropAndScale(). |
| 39 | private final int width; |
| 40 | private final int height; |
| 41 | private final Type type; |
| 42 | private final int id; |
| 43 | private final Matrix transformMatrix; |
| 44 | private final Handler toI420Handler; |
| 45 | private final YuvConverter yuvConverter; |
| 46 | private final RefCountDelegate refCountDelegate; |
| 47 | private final RefCountMonitor refCountMonitor; |
| 48 |
| 49 | public TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix, |
| 50 | Handler toI420Handler, YuvConverter yuvConverter, @Nullable Runnable releaseCallback) { |
| 51 | this(width, height, width, height, type, id, transformMatrix, toI420Handler, yuvConverter, |
| 52 | new RefCountMonitor() { |
| 53 | @Override |
| 54 | public void onRetain(TextureBufferImpl textureBuffer) {} |
| 55 |
| 56 | @Override |
| 57 | public void onRelease(TextureBufferImpl textureBuffer) {} |
| 58 |
| 59 | @Override |
| 60 | public void onDestroy(TextureBufferImpl textureBuffer) { |
| 61 | if (releaseCallback != null) { |
| 62 | releaseCallback.run(); |
| 63 | } |
| 64 | } |
| 65 | }); |
| 66 | } |
| 67 |
| 68 | TextureBufferImpl(int width, int height, Type type, int id, Matrix transformMatrix, |
| 69 | Handler toI420Handler, YuvConverter yuvConverter, RefCountMonitor refCountMonitor) { |
| 70 | this(width, height, width, height, type, id, transformMatrix, toI420Handler, yuvConverter, |
| 71 | refCountMonitor); |
| 72 | } |
| 73 |
| 74 | private TextureBufferImpl(int unscaledWidth, int unscaledHeight, int width, int height, Type type, |
| 75 | int id, Matrix transformMatrix, Handler toI420Handler, YuvConverter yuvConverter, |
| 76 | RefCountMonitor refCountMonitor) { |
| 77 | this.unscaledWidth = unscaledWidth; |
| 78 | this.unscaledHeight = unscaledHeight; |
| 79 | this.width = width; |
| 80 | this.height = height; |
| 81 | this.type = type; |
| 82 | this.id = id; |
| 83 | this.transformMatrix = transformMatrix; |
| 84 | this.toI420Handler = toI420Handler; |
| 85 | this.yuvConverter = yuvConverter; |
| 86 | this.refCountDelegate = new RefCountDelegate(() -> refCountMonitor.onDestroy(this)); |
| 87 | this.refCountMonitor = refCountMonitor; |
| 88 | } |
| 89 |
| 90 | @Override |
| 91 | public VideoFrame.TextureBuffer.Type getType() { |
| 92 | return type; |
| 93 | } |
| 94 |
| 95 | @Override |
| 96 | public int getTextureId() { |
| 97 | return id; |
| 98 | } |
| 99 |
| 100 | @Override |
| 101 | public Matrix getTransformMatrix() { |
| 102 | return transformMatrix; |
| 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 VideoFrame.I420Buffer toI420() { |
| 117 | try { |
| 118 | return ThreadUtils.invokeAtFrontUninterruptibly( |
| 119 | toI420Handler, () -> yuvConverter.convert(this)); |
| 120 | } catch (Throwable e) { |
| 121 | FileLog.e(e); |
| 122 | //don't crash if something fails |
| 123 | final int frameWidth = getWidth(); |
| 124 | final int frameHeight = getHeight(); |
| 125 | final int stride = ((frameWidth + 7) / 8) * 8; |
| 126 | final int uvHeight = (frameHeight + 1) / 2; |
| 127 |
| 128 | final int totalHeight = frameHeight + uvHeight; |
| 129 | final ByteBuffer i420ByteBuffer = JniCommon.nativeAllocateByteBuffer(stride * totalHeight); |
| 130 |
| 131 | while (i420ByteBuffer.hasRemaining()) { |
| 132 | i420ByteBuffer.put((byte) 0); |
| 133 | } |
| 134 |
| 135 | final int viewportWidth = stride / 4; |
| 136 |
| 137 | final int yPos = 0; |
| 138 | final int uPos = yPos + stride * frameHeight; |
| 139 | final int vPos = uPos + stride / 2; |
| 140 |
| 141 | i420ByteBuffer.position(yPos); |
| 142 | i420ByteBuffer.limit(yPos + stride * frameHeight); |
| 143 | final ByteBuffer dataY = i420ByteBuffer.slice(); |
| 144 |
| 145 | i420ByteBuffer.position(uPos); |
| 146 | // The last row does not have padding. |
| 147 | final int uvSize = stride * (uvHeight - 1) + stride / 2; |
| 148 | i420ByteBuffer.limit(uPos + uvSize); |
| 149 | final ByteBuffer dataU = i420ByteBuffer.slice(); |
| 150 |
| 151 | i420ByteBuffer.position(vPos); |
| 152 | i420ByteBuffer.limit(vPos + uvSize); |
| 153 | final ByteBuffer dataV = i420ByteBuffer.slice(); |
| 154 |
| 155 |
| 156 | return JavaI420Buffer.wrap(frameWidth, frameHeight, dataY, stride, dataU, stride, dataV, stride, |
| 157 | () -> JniCommon.nativeFreeByteBuffer(i420ByteBuffer)); |
| 158 | } |
| 159 | } |
| 160 |
| 161 | @Override |
| 162 | public void retain() { |
| 163 | refCountMonitor.onRetain(this); |
| 164 | refCountDelegate.retain(); |
| 165 | } |
| 166 |
| 167 | @Override |
| 168 | public void release() { |
| 169 | refCountMonitor.onRelease(this); |
| 170 | refCountDelegate.release(); |
| 171 | } |
| 172 |
| 173 | @Override |
| 174 | public VideoFrame.Buffer cropAndScale( |
| 175 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
| 176 | final Matrix cropAndScaleMatrix = new Matrix(); |
| 177 | // In WebRTC, Y=0 is the top row, while in OpenGL Y=0 is the bottom row. This means that the Y |
| 178 | // direction is effectively reversed. |
| 179 | final int cropYFromBottom = height - (cropY + cropHeight); |
| 180 | cropAndScaleMatrix.preTranslate(cropX / (float) width, cropYFromBottom / (float) height); |
| 181 | cropAndScaleMatrix.preScale(cropWidth / (float) width, cropHeight / (float) height); |
| 182 |
| 183 | return applyTransformMatrix(cropAndScaleMatrix, |
| 184 | (int) Math.round(unscaledWidth * cropWidth / (float) width), |
| 185 | (int) Math.round(unscaledHeight * cropHeight / (float) height), scaleWidth, scaleHeight); |
| 186 | } |
| 187 |
| 188 | /** |
| 189 | * Returns the width of the texture in memory. This should only be used for downscaling, and you |
| 190 | * should still respect the width from getWidth(). |
| 191 | */ |
| 192 | public int getUnscaledWidth() { |
| 193 | return unscaledWidth; |
| 194 | } |
| 195 |
| 196 | /** |
| 197 | * Returns the height of the texture in memory. This should only be used for downscaling, and you |
| 198 | * should still respect the height from getHeight(). |
| 199 | */ |
| 200 | public int getUnscaledHeight() { |
| 201 | return unscaledHeight; |
| 202 | } |
| 203 |
| 204 | public Handler getToI420Handler() { |
| 205 | return toI420Handler; |
| 206 | } |
| 207 |
| 208 | public YuvConverter getYuvConverter() { |
| 209 | return yuvConverter; |
| 210 | } |
| 211 |
| 212 | /** |
| 213 | * Create a new TextureBufferImpl with an applied transform matrix and a new size. The |
| 214 | * existing buffer is unchanged. The given transform matrix is applied first when texture |
| 215 | * coordinates are still in the unmodified [0, 1] range. |
| 216 | */ |
| 217 | public TextureBufferImpl applyTransformMatrix( |
| 218 | Matrix transformMatrix, int newWidth, int newHeight) { |
| 219 | return applyTransformMatrix(transformMatrix, /* unscaledWidth= */ newWidth, |
| 220 | /* unscaledHeight= */ newHeight, /* scaledWidth= */ newWidth, |
| 221 | /* scaledHeight= */ newHeight); |
| 222 | } |
| 223 |
| 224 | private TextureBufferImpl applyTransformMatrix(Matrix transformMatrix, int unscaledWidth, |
| 225 | int unscaledHeight, int scaledWidth, int scaledHeight) { |
| 226 | final Matrix newMatrix = new Matrix(this.transformMatrix); |
| 227 | newMatrix.preConcat(transformMatrix); |
| 228 | retain(); |
| 229 | return new TextureBufferImpl(unscaledWidth, unscaledHeight, scaledWidth, scaledHeight, type, id, |
| 230 | newMatrix, toI420Handler, yuvConverter, new RefCountMonitor() { |
| 231 | @Override |
| 232 | public void onRetain(TextureBufferImpl textureBuffer) { |
| 233 | refCountMonitor.onRetain(TextureBufferImpl.this); |
| 234 | } |
| 235 |
| 236 | @Override |
| 237 | public void onRelease(TextureBufferImpl textureBuffer) { |
| 238 | refCountMonitor.onRelease(TextureBufferImpl.this); |
| 239 | } |
| 240 |
| 241 | @Override |
| 242 | public void onDestroy(TextureBufferImpl textureBuffer) { |
| 243 | release(); |
| 244 | } |
| 245 | }); |
| 246 | } |
| 247 | } |
| 248 |