magjed | 5522021 | 2017-06-02 02:45:56 -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 | |
| 13 | import android.graphics.Matrix; |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 14 | import android.opengl.GLES11Ext; |
| 15 | import android.opengl.GLES20; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 16 | import java.nio.ByteBuffer; |
Sami Kalliomäki | e7592d8 | 2018-03-22 13:32:44 +0100 | [diff] [blame] | 17 | import javax.annotation.Nullable; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * Java version of webrtc::VideoFrame and webrtc::VideoFrameBuffer. A difference from the C++ |
| 21 | * version is that no explicit tag is used, and clients are expected to use 'instanceof' to find the |
| 22 | * right subclass of the buffer. This allows clients to create custom VideoFrame.Buffer in |
| 23 | * arbitrary format in their custom VideoSources, and then cast it back to the correct subclass in |
| 24 | * their custom VideoSinks. All implementations must also implement the toI420() function, |
| 25 | * converting from the underlying representation if necessary. I420 is the most widely accepted |
| 26 | * format and serves as a fallback for video sinks that can only handle I420, e.g. the internal |
| 27 | * WebRTC software encoders. |
| 28 | */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 29 | @JNINamespace("webrtc::jni") |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 30 | public class VideoFrame { |
| 31 | public interface Buffer { |
| 32 | /** |
| 33 | * Resolution of the buffer in pixels. |
| 34 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 35 | @CalledByNative("Buffer") int getWidth(); |
| 36 | @CalledByNative("Buffer") int getHeight(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Returns a memory-backed frame in I420 format. If the pixel data is in another format, a |
| 40 | * conversion will take place. All implementations must provide a fallback to I420 for |
| 41 | * compatibility with e.g. the internal WebRTC software encoders. |
| 42 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 43 | @CalledByNative("Buffer") I420Buffer toI420(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Reference counting is needed since a video buffer can be shared between multiple VideoSinks, |
| 47 | * and the buffer needs to be returned to the VideoSource as soon as all references are gone. |
| 48 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 49 | @CalledByNative("Buffer") void retain(); |
| 50 | @CalledByNative("Buffer") void release(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Crops a region defined by |cropx|, |cropY|, |cropWidth| and |cropHeight|. Scales it to size |
| 54 | * |scaleWidth| x |scaleHeight|. |
| 55 | */ |
Magnus Jedvert | 202be39 | 2017-11-18 16:09:17 +0100 | [diff] [blame] | 56 | @CalledByNative("Buffer") |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 57 | Buffer cropAndScale( |
| 58 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Interface for I420 buffers. |
| 63 | */ |
| 64 | public interface I420Buffer extends Buffer { |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 65 | /** |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 66 | * Returns a direct ByteBuffer containing Y-plane data. The buffer capacity is at least |
| 67 | * getStrideY() * getHeight() bytes. The position of the returned buffer is ignored and must |
| 68 | * be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 69 | * implementations must return a new ByteBuffer or slice for each call. |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 70 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 71 | @CalledByNative("I420Buffer") ByteBuffer getDataY(); |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 72 | /** |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 73 | * Returns a direct ByteBuffer containing U-plane data. The buffer capacity is at least |
| 74 | * getStrideU() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored |
| 75 | * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 76 | * implementations must return a new ByteBuffer or slice for each call. |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 77 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 78 | @CalledByNative("I420Buffer") ByteBuffer getDataU(); |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 79 | /** |
Sami Kalliomäki | e3044fe | 2017-10-02 09:41:55 +0200 | [diff] [blame] | 80 | * Returns a direct ByteBuffer containing V-plane data. The buffer capacity is at least |
| 81 | * getStrideV() * ((getHeight() + 1) / 2) bytes. The position of the returned buffer is ignored |
| 82 | * and must be 0. Callers may mutate the ByteBuffer (eg. through relative-read operations), so |
| 83 | * implementations must return a new ByteBuffer or slice for each call. |
Sami Kalliomäki | bc7a1a9 | 2017-09-27 12:50:47 +0200 | [diff] [blame] | 84 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 85 | @CalledByNative("I420Buffer") ByteBuffer getDataV(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 86 | |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 87 | @CalledByNative("I420Buffer") int getStrideY(); |
| 88 | @CalledByNative("I420Buffer") int getStrideU(); |
| 89 | @CalledByNative("I420Buffer") int getStrideV(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Interface for buffers that are stored as a single texture, either in OES or RGB format. |
| 94 | */ |
| 95 | public interface TextureBuffer extends Buffer { |
Sami Kalliomäki | cb98b11 | 2017-10-16 11:20:26 +0200 | [diff] [blame] | 96 | enum Type { |
| 97 | OES(GLES11Ext.GL_TEXTURE_EXTERNAL_OES), |
| 98 | RGB(GLES20.GL_TEXTURE_2D); |
| 99 | |
| 100 | private final int glTarget; |
| 101 | |
| 102 | private Type(final int glTarget) { |
| 103 | this.glTarget = glTarget; |
| 104 | } |
| 105 | |
| 106 | public int getGlTarget() { |
| 107 | return glTarget; |
| 108 | } |
| 109 | } |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 110 | |
| 111 | Type getType(); |
| 112 | int getTextureId(); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Retrieve the transform matrix associated with the frame. This transform matrix maps 2D |
| 116 | * homogeneous coordinates of the form (s, t, 1) with s and t in the inclusive range [0, 1] to |
| 117 | * the coordinate that should be used to sample that location from the buffer. |
| 118 | */ |
| 119 | public Matrix getTransformMatrix(); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | private final Buffer buffer; |
| 123 | private final int rotation; |
| 124 | private final long timestampNs; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 125 | |
Magnus Jedvert | 1f2a3e7 | 2017-11-23 16:56:44 +0100 | [diff] [blame] | 126 | @CalledByNative |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 127 | public VideoFrame(Buffer buffer, int rotation, long timestampNs) { |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 128 | if (buffer == null) { |
| 129 | throw new IllegalArgumentException("buffer not allowed to be null"); |
| 130 | } |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 131 | if (rotation % 90 != 0) { |
| 132 | throw new IllegalArgumentException("rotation must be a multiple of 90"); |
| 133 | } |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 134 | this.buffer = buffer; |
| 135 | this.rotation = rotation; |
| 136 | this.timestampNs = timestampNs; |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 139 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 140 | public Buffer getBuffer() { |
| 141 | return buffer; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Rotation of the frame in degrees. |
| 146 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 147 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 148 | public int getRotation() { |
| 149 | return rotation; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Timestamp of the frame in nano seconds. |
| 154 | */ |
Magnus Jedvert | c2ac3c6 | 2017-11-14 17:08:59 +0100 | [diff] [blame] | 155 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 156 | public long getTimestampNs() { |
| 157 | return timestampNs; |
| 158 | } |
| 159 | |
sakal | 6bdcefc | 2017-08-15 01:56:02 -0700 | [diff] [blame] | 160 | public int getRotatedWidth() { |
| 161 | if (rotation % 180 == 0) { |
| 162 | return buffer.getWidth(); |
| 163 | } |
| 164 | return buffer.getHeight(); |
| 165 | } |
| 166 | |
| 167 | public int getRotatedHeight() { |
| 168 | if (rotation % 180 == 0) { |
| 169 | return buffer.getHeight(); |
| 170 | } |
| 171 | return buffer.getWidth(); |
| 172 | } |
| 173 | |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 174 | /** |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 175 | * Reference counting of the underlying buffer. |
| 176 | */ |
| 177 | public void retain() { |
| 178 | buffer.retain(); |
| 179 | } |
| 180 | |
Sami Kalliomäki | 2bde850 | 2018-02-15 13:58:15 +0100 | [diff] [blame] | 181 | @CalledByNative |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 182 | public void release() { |
| 183 | buffer.release(); |
| 184 | } |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 185 | |
Sami Kalliomäki | 1641ca3 | 2018-04-04 15:59:31 +0200 | [diff] [blame^] | 186 | // TODO(sakal): This file should be strictly an interface. This method should be moved somewhere |
| 187 | // else. |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 188 | public static VideoFrame.Buffer cropAndScaleI420(final I420Buffer buffer, int cropX, int cropY, |
| 189 | int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
| 190 | if (cropWidth == scaleWidth && cropHeight == scaleHeight) { |
| 191 | // No scaling. |
| 192 | ByteBuffer dataY = buffer.getDataY(); |
| 193 | ByteBuffer dataU = buffer.getDataU(); |
| 194 | ByteBuffer dataV = buffer.getDataV(); |
| 195 | |
| 196 | dataY.position(cropX + cropY * buffer.getStrideY()); |
| 197 | dataU.position(cropX / 2 + cropY / 2 * buffer.getStrideU()); |
| 198 | dataV.position(cropX / 2 + cropY / 2 * buffer.getStrideV()); |
| 199 | |
| 200 | buffer.retain(); |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 201 | return JavaI420Buffer.wrap(buffer.getWidth(), buffer.getHeight(), dataY.slice(), |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 202 | buffer.getStrideY(), dataU.slice(), buffer.getStrideU(), dataV.slice(), |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 203 | buffer.getStrideV(), buffer::release); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 206 | JavaI420Buffer newBuffer = JavaI420Buffer.allocate(scaleWidth, scaleHeight); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 207 | nativeCropAndScaleI420(buffer.getDataY(), buffer.getStrideY(), buffer.getDataU(), |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 208 | buffer.getStrideU(), buffer.getDataV(), buffer.getStrideV(), cropX, cropY, cropWidth, |
| 209 | cropHeight, newBuffer.getDataY(), newBuffer.getStrideY(), newBuffer.getDataU(), |
| 210 | newBuffer.getStrideU(), newBuffer.getDataV(), newBuffer.getStrideV(), scaleWidth, |
| 211 | scaleHeight); |
| 212 | return newBuffer; |
| 213 | } |
| 214 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 215 | private static native void nativeCropAndScaleI420(ByteBuffer srcY, int srcStrideY, |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 216 | ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, int cropX, int cropY, |
| 217 | int cropWidth, int cropHeight, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 218 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int scaleWidth, int scaleHeight); |
magjed | 5522021 | 2017-06-02 02:45:56 -0700 | [diff] [blame] | 219 | } |