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