Skip to content

Commit 163a017

Browse files
authored
fix: convert only the window between position and limit of a ByteBuffer (#415)
VideoBufferConverter passes direct buffers as slices and heap buffers with offset and length, so slices and buffers with a position no longer read or write outside their window. The position stays as it is.
1 parent aa338da commit 163a017

5 files changed

Lines changed: 276 additions & 40 deletions

File tree

‎docs/tools/video/video-buffer-converter.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public void onImage(BufferedImage image) {
135135
## Error handling and edge cases
136136
- All methods throw `NullPointerException` if src/dst is null; ensure proper checks.
137137
- `ByteBuffer` destinations must be writable (not read-only) for `convertFromI420`.
138+
- A `ByteBuffer` is read or written between its position and its limit only, also when it is a slice or a view of a larger buffer. The window must hold the whole frame; the position and limit stay as they are.
138139
- Ensure the correct FourCC is used for the actual memory layout you pass/expect.
139140
- Beware of frame rotation metadata; conversions do not rotate pixels. Handle `VideoFrame.rotation` separately if your renderer requires upright images.
140141

‎webrtc-jni/src/main/cpp/include/JNI_VideoBufferConverter.h‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webrtc-jni/src/main/cpp/src/JNI_VideoBufferConverter.cpp‎

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,35 @@ size_t CalcBufferSize(int width, int height, int fourCC) {
6363
return bufferSize;
6464
}
6565

66+
// Throws and returns false unless [offset, offset + length) lies within the array.
67+
static bool CheckArrayWindow(JNIEnv * env, jbyteArray array, jint offset, jint length)
68+
{
69+
jsize arrayLength = env->GetArrayLength(array);
70+
71+
if (offset < 0 || length < 0 || offset > arrayLength - length) {
72+
env->Throw(jni::JavaRuntimeException(env, "Invalid array window [offset %d, length %d, array %d]",
73+
offset, length, arrayLength));
74+
return false;
75+
}
76+
77+
return true;
78+
}
79+
6680
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I420toByteArray
6781
(JNIEnv * env, jclass cls, jobject jSrcY, jint srcStrideY, jobject jSrcU, jint srcStrideU,
68-
jobject jSrcV, jint srcStrideV, jbyteArray dst, jint width, jint height, jint fourCC)
82+
jobject jSrcV, jint srcStrideV, jbyteArray dst, jint dstOffset, jint dstLength,
83+
jint width, jint height, jint fourCC)
6984
{
7085
try {
71-
jsize arrayLength = env->GetArrayLength(dst);
86+
if (!CheckArrayWindow(env, dst, dstOffset, dstLength)) {
87+
return;
88+
}
89+
7290
size_t requiredSize = CalcBufferSize(width, height, fourCC);
7391

74-
if (arrayLength < requiredSize) {
92+
if (static_cast<size_t>(dstLength) < requiredSize) {
7593
env->Throw(jni::JavaRuntimeException(env, "Insufficient buffer size [has %d, need %zd]",
76-
arrayLength, requiredSize));
94+
dstLength, requiredSize));
7795
return;
7896
}
7997

@@ -89,7 +107,7 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I
89107
if (!arrayPtr) {
90108
return;
91109
}
92-
uint8_t * dstPtr = reinterpret_cast<uint8_t *>(arrayPtr.get());
110+
uint8_t * dstPtr = reinterpret_cast<uint8_t *>(arrayPtr.get()) + dstOffset;
93111

94112
const int conversionResult = libyuv::ConvertFromI420(srcY, srcStrideY, srcU, srcStrideU,
95113
srcV, srcStrideV, dstPtr, 0, width, height, static_cast<uint32_t>(fourCC));
@@ -138,16 +156,20 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I
138156
}
139157

140158
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_byteArrayToI420
141-
(JNIEnv * env, jclass cls, jbyteArray src, jint width, jint height, jobject jDstY, jint dstStrideY,
142-
jobject jDstU, jint dstStrideU, jobject jDstV, jint dstStrideV, jint fourCC)
159+
(JNIEnv * env, jclass cls, jbyteArray src, jint srcOffset, jint srcLength, jint width, jint height,
160+
jobject jDstY, jint dstStrideY, jobject jDstU, jint dstStrideU, jobject jDstV, jint dstStrideV,
161+
jint fourCC)
143162
{
144163
try {
145-
jsize arrayLength = env->GetArrayLength(src);
164+
if (!CheckArrayWindow(env, src, srcOffset, srcLength)) {
165+
return;
166+
}
167+
146168
size_t requiredSize = CalcBufferSize(width, height, fourCC);
147169

148-
if (arrayLength < requiredSize) {
170+
if (static_cast<size_t>(srcLength) < requiredSize) {
149171
env->Throw(jni::JavaRuntimeException(env, "Insufficient buffer size [has %d, need %zd]",
150-
arrayLength, requiredSize));
172+
srcLength, requiredSize));
151173
return;
152174
}
153175

@@ -162,10 +184,10 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_b
162184
if (!arrayPtr) {
163185
return;
164186
}
165-
const uint8_t * srcPtr = reinterpret_cast<uint8_t *>(arrayPtr.get());
187+
const uint8_t * srcPtr = reinterpret_cast<uint8_t *>(arrayPtr.get()) + srcOffset;
166188

167189
const int conversionResult = libyuv::ConvertToI420(
168-
srcPtr, arrayLength,
190+
srcPtr, srcLength,
169191
dstY, dstStrideY,
170192
dstU, dstStrideU,
171193
dstV, dstStrideV,

‎webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoBufferConverter.java‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void convertFromI420(VideoFrameBuffer src, byte[] dst, FourCC four
5858
i420.getDataY(), i420.getStrideY(),
5959
i420.getDataU(), i420.getStrideU(),
6060
i420.getDataV(), i420.getStrideV(),
61-
dst,
61+
dst, 0, dst.length,
6262
i420.getWidth(), i420.getHeight(),
6363
fourCC.value());
6464
}
@@ -67,10 +67,15 @@ public static void convertFromI420(VideoFrameBuffer src, byte[] dst, FourCC four
6767
* Convert an arbitrary {@link VideoFrameBuffer} to the specified pixel format
6868
* and write the result into a {@link ByteBuffer}. If the destination buffer
6969
* is a direct buffer, a native direct path is used; otherwise its backing
70-
* array (or a temporary array) is employed.
70+
* array is employed.
71+
* <p>
72+
* The result is written to the bytes between the buffer's position and its
73+
* limit, starting at the position. The position and limit stay as they are,
74+
* and no byte outside of them is touched.
7175
*
7276
* @param src Source video frame buffer.
73-
* @param dst Writable destination {@link ByteBuffer}. Must not be read-only.
77+
* @param dst Writable destination {@link ByteBuffer}. Must not be read-only,
78+
* and must have enough bytes remaining for the converted frame.
7479
* @param fourCC Target FourCC format identifier.
7580
*
7681
* @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
@@ -92,30 +97,23 @@ public static void convertFromI420(VideoFrameBuffer src, ByteBuffer dst, FourCC
9297
I420Buffer i420 = src.toI420();
9398

9499
if (dst.isDirect()) {
100+
// The slice starts at the position and ends at the limit, which
101+
// the native code sees as the buffer's address and capacity.
95102
I420toDirectBuffer(
96103
i420.getDataY(), i420.getStrideY(),
97104
i420.getDataU(), i420.getStrideU(),
98105
i420.getDataV(), i420.getStrideV(),
99-
dst,
106+
dst.slice(),
100107
i420.getWidth(), i420.getHeight(),
101108
fourCC.value());
102109
}
103110
else {
104-
byte[] arrayBuffer;
105-
106-
if (dst.hasArray()) {
107-
arrayBuffer = dst.array();
108-
}
109-
else {
110-
arrayBuffer = new byte[dst.remaining()];
111-
dst.get(arrayBuffer);
112-
}
113-
111+
// A writable heap buffer always has an accessible array.
114112
I420toByteArray(
115113
i420.getDataY(), i420.getStrideY(),
116114
i420.getDataU(), i420.getStrideU(),
117115
i420.getDataV(), i420.getStrideV(),
118-
arrayBuffer,
116+
dst.array(), dst.arrayOffset() + dst.position(), dst.remaining(),
119117
i420.getWidth(), i420.getHeight(),
120118
fourCC.value());
121119
}
@@ -142,7 +140,7 @@ public static void convertToI420(byte[] src, I420Buffer dst, FourCC fourCC)
142140
}
143141

144142
byteArrayToI420(
145-
src,
143+
src, 0, src.length,
146144
dst.getWidth(), dst.getHeight(),
147145
dst.getDataY(), dst.getStrideY(),
148146
dst.getDataU(), dst.getStrideU(),
@@ -155,6 +153,10 @@ public static void convertToI420(byte[] src, I420Buffer dst, FourCC fourCC)
155153
* specified by {@code fourCC}) to I420 and write the result into the provided
156154
* {@link I420Buffer}. Uses a direct native path for direct buffers; otherwise
157155
* reads from the backing or a temporary array.
156+
* <p>
157+
* The source frame is read from the bytes between the buffer's position and
158+
* its limit, starting at the position. The position and limit stay as they
159+
* are.
158160
*
159161
* @param src Source pixel data buffer.
160162
* @param dst Destination I420 buffer (pre-allocated).
@@ -173,27 +175,35 @@ public static void convertToI420(ByteBuffer src, I420Buffer dst, FourCC fourCC)
173175
}
174176

175177
if (src.isDirect()) {
178+
// The slice starts at the position and ends at the limit, which
179+
// the native code sees as the buffer's address and capacity.
176180
directBufferToI420(
177-
src,
181+
src.slice(),
178182
dst.getWidth(), dst.getHeight(),
179183
dst.getDataY(), dst.getStrideY(),
180184
dst.getDataU(), dst.getStrideU(),
181185
dst.getDataV(), dst.getStrideV(),
182186
fourCC.value());
183187
}
184188
else {
185-
byte[] arrayBuffer;
189+
byte[] array;
190+
int offset;
191+
int length = src.remaining();
186192

187193
if (src.hasArray()) {
188-
arrayBuffer = src.array();
194+
array = src.array();
195+
offset = src.arrayOffset() + src.position();
189196
}
190197
else {
191-
arrayBuffer = new byte[src.remaining()];
192-
src.get(arrayBuffer);
198+
// Read-only, so the array is not accessible. Copied through a
199+
// duplicate, which leaves the position of the buffer as it is.
200+
array = new byte[length];
201+
offset = 0;
202+
src.duplicate().get(array);
193203
}
194204

195205
byteArrayToI420(
196-
arrayBuffer,
206+
array, offset, length,
197207
dst.getWidth(), dst.getHeight(),
198208
dst.getDataY(), dst.getStrideY(),
199209
dst.getDataU(), dst.getStrideU(),
@@ -206,7 +216,7 @@ private native static void I420toByteArray(
206216
ByteBuffer srcY, int srcStrideY,
207217
ByteBuffer srcU, int srcStrideU,
208218
ByteBuffer srcV, int srcStrideV,
209-
byte[] dst,
219+
byte[] dst, int dstOffset, int dstLength,
210220
int width, int height,
211221
int fourCC) throws Exception;
212222

@@ -219,7 +229,7 @@ private native static void I420toDirectBuffer(
219229
int fourCC) throws Exception;
220230

221231
private native static void byteArrayToI420(
222-
byte[] src,
232+
byte[] src, int srcOffset, int srcLength,
223233
int width, int height,
224234
ByteBuffer dstY, int dstStrideY,
225235
ByteBuffer dstU, int dstStrideU,

0 commit comments

Comments
 (0)