blob: a13112105e0f7ec598f0985fd80996cc81a57695 [file] [log] [blame]
magjed55220212017-06-02 02:45:56 -07001/*
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
11package org.webrtc;
12
13import android.graphics.Matrix;
14import java.nio.ByteBuffer;
15
16/**
17 * Java version of webrtc::VideoFrame and webrtc::VideoFrameBuffer. A difference from the C++
18 * version is that no explicit tag is used, and clients are expected to use 'instanceof' to find the
19 * right subclass of the buffer. This allows clients to create custom VideoFrame.Buffer in
20 * arbitrary format in their custom VideoSources, and then cast it back to the correct subclass in
21 * their custom VideoSinks. All implementations must also implement the toI420() function,
22 * converting from the underlying representation if necessary. I420 is the most widely accepted
23 * format and serves as a fallback for video sinks that can only handle I420, e.g. the internal
24 * WebRTC software encoders.
25 */
26public class VideoFrame {
27 public interface Buffer {
28 /**
29 * Resolution of the buffer in pixels.
30 */
31 int getWidth();
32 int getHeight();
33
34 /**
35 * Returns a memory-backed frame in I420 format. If the pixel data is in another format, a
36 * conversion will take place. All implementations must provide a fallback to I420 for
37 * compatibility with e.g. the internal WebRTC software encoders.
38 */
39 I420Buffer toI420();
40
41 /**
42 * Reference counting is needed since a video buffer can be shared between multiple VideoSinks,
43 * and the buffer needs to be returned to the VideoSource as soon as all references are gone.
44 */
45 void retain();
46 void release();
sakal836f60c2017-07-28 07:12:23 -070047
48 /**
49 * Crops a region defined by |cropx|, |cropY|, |cropWidth| and |cropHeight|. Scales it to size
50 * |scaleWidth| x |scaleHeight|.
51 */
52 Buffer cropAndScale(
53 int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight);
magjed55220212017-06-02 02:45:56 -070054 }
55
56 /**
57 * Interface for I420 buffers.
58 */
59 public interface I420Buffer extends Buffer {
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020060 /**
61 * Returns a direct ByteBuffer containing Y-plane data. The buffer size is at least getStrideY()
Bjorn Mellemd6293142017-09-29 11:33:52 -070062 * * getHeight() bytes. Callers may mutate the ByteBuffer (eg. through relative-read
63 * operations), so implementations must return a new ByteBuffer or slice for each call.
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020064 */
magjed55220212017-06-02 02:45:56 -070065 ByteBuffer getDataY();
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020066 /**
67 * Returns a direct ByteBuffer containing U-plane data. The buffer size is at least getStrideU()
Bjorn Mellemd6293142017-09-29 11:33:52 -070068 * * ((getHeight() + 1) / 2) bytes. Callers may mutate the ByteBuffer (eg. through relative-read
69 * operations), so implementations must return a new ByteBuffer or slice for each call.
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020070 */
magjed55220212017-06-02 02:45:56 -070071 ByteBuffer getDataU();
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020072 /**
73 * Returns a direct ByteBuffer containing V-plane data. The buffer size is at least getStrideV()
Bjorn Mellemd6293142017-09-29 11:33:52 -070074 * * ((getHeight() + 1) / 2) bytes. Callers may mutate the ByteBuffer (eg. through relative-read
75 * operations), so implementations must return a new ByteBuffer or slice for each call.
Sami Kalliomäkibc7a1a92017-09-27 12:50:47 +020076 */
magjed55220212017-06-02 02:45:56 -070077 ByteBuffer getDataV();
78
79 int getStrideY();
80 int getStrideU();
81 int getStrideV();
82 }
83
84 /**
85 * Interface for buffers that are stored as a single texture, either in OES or RGB format.
86 */
87 public interface TextureBuffer extends Buffer {
88 enum Type { OES, RGB }
89
90 Type getType();
91 int getTextureId();
sakal836f60c2017-07-28 07:12:23 -070092
93 /**
94 * Retrieve the transform matrix associated with the frame. This transform matrix maps 2D
95 * homogeneous coordinates of the form (s, t, 1) with s and t in the inclusive range [0, 1] to
96 * the coordinate that should be used to sample that location from the buffer.
97 */
98 public Matrix getTransformMatrix();
magjed55220212017-06-02 02:45:56 -070099 }
100
101 private final Buffer buffer;
102 private final int rotation;
103 private final long timestampNs;
magjed55220212017-06-02 02:45:56 -0700104
sakal836f60c2017-07-28 07:12:23 -0700105 public VideoFrame(Buffer buffer, int rotation, long timestampNs) {
magjed55220212017-06-02 02:45:56 -0700106 if (buffer == null) {
107 throw new IllegalArgumentException("buffer not allowed to be null");
108 }
sakal6bdcefc2017-08-15 01:56:02 -0700109 if (rotation % 90 != 0) {
110 throw new IllegalArgumentException("rotation must be a multiple of 90");
111 }
magjed55220212017-06-02 02:45:56 -0700112 this.buffer = buffer;
113 this.rotation = rotation;
114 this.timestampNs = timestampNs;
magjed55220212017-06-02 02:45:56 -0700115 }
116
117 public Buffer getBuffer() {
118 return buffer;
119 }
120
121 /**
122 * Rotation of the frame in degrees.
123 */
124 public int getRotation() {
125 return rotation;
126 }
127
128 /**
129 * Timestamp of the frame in nano seconds.
130 */
131 public long getTimestampNs() {
132 return timestampNs;
133 }
134
sakal6bdcefc2017-08-15 01:56:02 -0700135 public int getRotatedWidth() {
136 if (rotation % 180 == 0) {
137 return buffer.getWidth();
138 }
139 return buffer.getHeight();
140 }
141
142 public int getRotatedHeight() {
143 if (rotation % 180 == 0) {
144 return buffer.getHeight();
145 }
146 return buffer.getWidth();
147 }
148
magjed55220212017-06-02 02:45:56 -0700149 /**
magjed55220212017-06-02 02:45:56 -0700150 * Reference counting of the underlying buffer.
151 */
152 public void retain() {
153 buffer.retain();
154 }
155
156 public void release() {
157 buffer.release();
158 }
sakal836f60c2017-07-28 07:12:23 -0700159
160 public static VideoFrame.Buffer cropAndScaleI420(final I420Buffer buffer, int cropX, int cropY,
161 int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) {
162 if (cropWidth == scaleWidth && cropHeight == scaleHeight) {
163 // No scaling.
164 ByteBuffer dataY = buffer.getDataY();
165 ByteBuffer dataU = buffer.getDataU();
166 ByteBuffer dataV = buffer.getDataV();
167
168 dataY.position(cropX + cropY * buffer.getStrideY());
169 dataU.position(cropX / 2 + cropY / 2 * buffer.getStrideU());
170 dataV.position(cropX / 2 + cropY / 2 * buffer.getStrideV());
171
172 buffer.retain();
173 return new I420BufferImpl(buffer.getWidth(), buffer.getHeight(), dataY.slice(),
174 buffer.getStrideY(), dataU.slice(), buffer.getStrideU(), dataV.slice(),
175 buffer.getStrideV(), new Runnable() {
176 @Override
177 public void run() {
178 buffer.release();
179 }
180 });
181 }
182
183 I420BufferImpl newBuffer = I420BufferImpl.allocate(scaleWidth, scaleHeight);
184 nativeCropAndScaleI420(buffer.getDataY(), buffer.getStrideY(), buffer.getDataU(),
185 buffer.getStrideU(), buffer.getDataV(), buffer.getStrideV(), cropX, cropY, cropWidth,
186 cropHeight, newBuffer.getDataY(), newBuffer.getStrideY(), newBuffer.getDataU(),
187 newBuffer.getStrideU(), newBuffer.getDataV(), newBuffer.getStrideV(), scaleWidth,
188 scaleHeight);
189 return newBuffer;
190 }
191
192 private static native void nativeCropAndScaleI420(ByteBuffer srcY, int srcStrideY,
193 ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, int cropX, int cropY,
194 int cropWidth, int cropHeight, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU,
195 int dstStrideU, ByteBuffer dstV, int dstStrideV, int scaleWidth, int scaleHeight);
magjed55220212017-06-02 02:45:56 -0700196}