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