Skip to content

Commit 0ea8d21

Browse files
committed
fix: validate I420 planes before native conversion
1 parent 97d22d6 commit 0ea8d21

7 files changed

Lines changed: 342 additions & 29 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public void onImage(BufferedImage image) {
137137
## Error handling and edge cases
138138
- All methods throw `NullPointerException` if src/dst is null; ensure proper checks.
139139
- `ByteBuffer` destinations must be writable (not read-only) for `convertFromI420`.
140-
- 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.
140+
- A packed `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.
141+
- I420 frames need positive dimensions. Every Y/U/V plane must be a non-null direct buffer with stride at least its plane width and capacity of at least `stride * (rows - 1L) + columns` bytes. Chroma dimensions round up to half the frame dimensions. The last row needs no trailing padding.
142+
- I420 plane positions and limits are ignored; return a slice with the appropriate capacity to restrict a plane's memory. Read-only planes are accepted as sources, but all three destination planes must be writable. Invalid planes cause a `RuntimeException` before conversion writes any pixels.
141143
- Ensure the correct FourCC is used for the actual memory layout you pass/expect.
142144
- Beware of frame rotation metadata; conversions do not rotate pixels. Handle `VideoFrame.rotation` separately if your renderer requires upright images.
143145

‎webrtc-jni/src/main/cpp/include/api/VideoFrame.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ namespace jni
4848

4949
namespace I420Buffer
5050
{
51+
// Checks a plane's dimensions, stride and direct-buffer capacity before
52+
// native code reads or writes it. The last row needs only width bytes.
53+
// Position and limit are ignored, as in I420Buffer's Java contract.
54+
// Writable destinations must not be read-only. Throws on invalid input.
55+
void CheckPlane(JNIEnv * env, const char * name, jobject data, jint stride,
56+
jint width, jint height, bool writable = false);
57+
5158
// Wraps the pixels of the buffer in a NativeI420Buffer without
5259
// copying them. With readOnly its planes are read-only byte buffers,
5360
// for pixels that others share.

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "JavaRuntimeException.h"
2020
#include "JavaUtils.h"
2121

22-
#include "api/video/i420_buffer.h"
22+
#include "api/VideoFrame.h"
2323

2424
#include "libyuv/convert.h"
2525
#include "libyuv/video_common.h"
@@ -159,6 +159,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I
159159
return;
160160
}
161161

162+
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
163+
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
164+
jni::I420Buffer::CheckPlane(env, "Y", jSrcY, srcStrideY, width, height);
165+
jni::I420Buffer::CheckPlane(env, "U", jSrcU, srcStrideU, chromaWidth, chromaHeight);
166+
jni::I420Buffer::CheckPlane(env, "V", jSrcV, srcStrideV, chromaWidth, chromaHeight);
167+
162168
const uint8_t * srcY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcY));
163169
const uint8_t * srcU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcU));
164170
const uint8_t * srcV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcV));
@@ -191,6 +197,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I
191197
jobject jSrcV, jint srcStrideV, jobject dst, jint width, jint height, jint fourCC)
192198
{
193199
jni::CallFromJava(env, [&]() {
200+
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
201+
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
202+
jni::I420Buffer::CheckPlane(env, "Y", jSrcY, srcStrideY, width, height);
203+
jni::I420Buffer::CheckPlane(env, "U", jSrcU, srcStrideU, chromaWidth, chromaHeight);
204+
jni::I420Buffer::CheckPlane(env, "V", jSrcV, srcStrideV, chromaWidth, chromaHeight);
205+
194206
const uint8_t * srcY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcY));
195207
const uint8_t * srcU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcU));
196208
const uint8_t * srcV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcV));
@@ -232,6 +244,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_b
232244
return;
233245
}
234246

247+
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
248+
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
249+
jni::I420Buffer::CheckPlane(env, "Y", jDstY, dstStrideY, width, height, true);
250+
jni::I420Buffer::CheckPlane(env, "U", jDstU, dstStrideU, chromaWidth, chromaHeight, true);
251+
jni::I420Buffer::CheckPlane(env, "V", jDstV, dstStrideV, chromaWidth, chromaHeight, true);
252+
235253
uint8_t * dstY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstY));
236254
uint8_t * dstU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstU));
237255
uint8_t * dstV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstV));
@@ -268,6 +286,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_d
268286
jobject jDstU, jint dstStrideU, jobject jDstV, jint dstStrideV, jint fourCC)
269287
{
270288
jni::CallFromJava(env, [&]() {
289+
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
290+
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
291+
jni::I420Buffer::CheckPlane(env, "Y", jDstY, dstStrideY, width, height, true);
292+
jni::I420Buffer::CheckPlane(env, "U", jDstU, dstStrideU, chromaWidth, chromaHeight, true);
293+
jni::I420Buffer::CheckPlane(env, "V", jDstV, dstStrideV, chromaWidth, chromaHeight, true);
294+
271295
uint8_t * dstY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstY));
272296
uint8_t * dstU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstU));
273297
uint8_t * dstV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstV));

‎webrtc-jni/src/main/cpp/src/api/VideoFrame.cpp‎

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ namespace jni
3131
{
3232
namespace
3333
{
34+
class JavaByteBufferClass : public JavaClass
35+
{
36+
public:
37+
explicit JavaByteBufferClass(JNIEnv * env)
38+
{
39+
cls = FindClass(env, "java/nio/ByteBuffer");
40+
isReadOnly = GetMethod(env, cls, "isReadOnly", "()Z");
41+
}
42+
43+
jclass cls;
44+
jmethodID isReadOnly;
45+
};
46+
3447
// A direct buffer over a plane, or throws: the allocation may fail, with or without an exception pending.
3548
JavaLocalRef<jobject> NewPlane(JNIEnv * env, const char * name, const uint8_t * data, jlong size)
3649
{
@@ -55,24 +68,6 @@ namespace jni
5568
return view;
5669
}
5770

58-
// Throws unless the plane holds every byte a copy reads: the last row ends at the width.
59-
void CheckPlane(JNIEnv * env, const char * name, const JavaRef<jobject> & data, jint stride, jint width, jint height)
60-
{
61-
if (stride < width) {
62-
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
63-
JavaRuntimeException(env, "Stride %d of the %s plane is less than its width %d", stride, name, width)));
64-
}
65-
66-
// Neither factor exceeds an int, so the product cannot overflow.
67-
const int64_t required = static_cast<int64_t>(stride) * (height - 1) + width;
68-
const jlong capacity = env->GetDirectBufferCapacity(data);
69-
70-
if (capacity < required) {
71-
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
72-
JavaRuntimeException(env, "The %s plane holds %lld bytes, but a %dx%d plane with stride %d needs %lld",
73-
name, static_cast<long long>(capacity), width, height, stride, static_cast<long long>(required))));
74-
}
75-
}
7671
}
7772

7873
namespace VideoFrame
@@ -203,16 +198,53 @@ namespace jni
203198
JavaRuntimeException(env, "Buffer size %dx%d is too large", width, height)));
204199
}
205200

206-
CheckPlane(env, "Y", dataY, strideY, width, height);
207-
CheckPlane(env, "U", dataU, strideU, chromaWidth, chromaHeight);
208-
CheckPlane(env, "V", dataV, strideV, chromaWidth, chromaHeight);
201+
I420Buffer::CheckPlane(env, "Y", dataY, strideY, width, height);
202+
I420Buffer::CheckPlane(env, "U", dataU, strideU, chromaWidth, chromaHeight);
203+
I420Buffer::CheckPlane(env, "V", dataV, strideV, chromaWidth, chromaHeight);
209204

210205
return webrtc::I420Buffer::Copy(width, height, y, strideY, u, strideU, v, strideV);
211206
}
212207
}
213208

214209
namespace I420Buffer
215210
{
211+
void CheckPlane(JNIEnv * env, const char * name, jobject data, jint stride,
212+
jint width, jint height, bool writable)
213+
{
214+
if (width <= 0 || height <= 0 || stride < width) {
215+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
216+
JavaRuntimeException(env, "Invalid %s plane dimensions %dx%d or stride %d", name, width, height, stride)));
217+
}
218+
if (data == nullptr) {
219+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
220+
JavaRuntimeException(env, "The %s plane is null", name)));
221+
}
222+
223+
const jlong capacity = env->GetDirectBufferCapacity(data);
224+
if (capacity < 0 || env->GetDirectBufferAddress(data) == nullptr) {
225+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
226+
JavaRuntimeException(env, "The %s plane must be a direct buffer", name)));
227+
}
228+
229+
// Each factor is an int; the product and sum fit in 64 bits.
230+
const int64_t required = static_cast<int64_t>(stride) * (height - 1) + width;
231+
if (capacity < required) {
232+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
233+
JavaRuntimeException(env, "The %s plane holds %lld bytes, but a %dx%d plane with stride %d needs %lld",
234+
name, static_cast<long long>(capacity), width, height, stride, static_cast<long long>(required))));
235+
}
236+
237+
if (writable) {
238+
const auto javaClass = JavaClasses::get<JavaByteBufferClass>(env);
239+
const jboolean readOnly = env->CallBooleanMethod(data, javaClass->isReadOnly);
240+
ExceptionCheck(env);
241+
if (readOnly) {
242+
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
243+
JavaRuntimeException(env, "The %s destination plane is read-only", name)));
244+
}
245+
}
246+
}
247+
216248
JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::scoped_refptr<webrtc::I420BufferInterface> & buffer,
217249
bool readOnly)
218250
{

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,42 @@
1818

1919
import java.nio.ByteBuffer;
2020

21+
/**
22+
* Planar I420 pixels with positive dimensions and direct Y, U and V buffers.
23+
* Each stride must be at least its plane's width. A plane must hold
24+
* {@code stride * (rows - 1L) + columns} bytes: padding after the last row is
25+
* not required. Chroma columns and rows are the frame dimensions rounded up
26+
* to half size. Plane position and limit are ignored; use a slice to expose
27+
* only the memory belonging to the plane.
28+
* <p>
29+
* Read-only planes can be used as sources. Operations that write pixels,
30+
* such as {@link VideoBufferConverter#convertToI420(byte[], I420Buffer,
31+
* dev.onvoid.webrtc.media.FourCC)}, require writable destination planes.
32+
*/
2133
public interface I420Buffer extends VideoFrameBuffer {
2234

2335
/**
2436
* Returns a direct ByteBuffer containing Y-plane data. The buffer capacity is
25-
* at least getStrideY() * getHeight() bytes. The position of the returned
26-
* buffer is ignored and must be 0. Callers may mutate the ByteBuffer (eg.
37+
* at least {@code getStrideY() * (getHeight() - 1L) + getWidth()} bytes.
38+
* The position of the returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer (eg.
2739
* through relative-read operations), so implementations must return a new
2840
* ByteBuffer or slice for each call.
2941
*/
3042
ByteBuffer getDataY();
3143

3244
/**
3345
* Returns a direct ByteBuffer containing U-plane data. The buffer capacity is
34-
* at least getStrideU() * ((getHeight() + 1) / 2) bytes. The position of the
35-
* returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer
46+
* large enough for every chroma row, without requiring trailing padding.
47+
* The position of the returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer
3648
* (eg. through relative-read operations), so implementations must return a new
3749
* ByteBuffer or slice for each call.
3850
*/
3951
ByteBuffer getDataU();
4052

4153
/**
4254
* Returns a direct ByteBuffer containing V-plane data. The buffer capacity is
43-
* at least getStrideV() * ((getHeight() + 1) / 2) bytes. The position of the
44-
* returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer
55+
* large enough for every chroma row, without requiring trailing padding.
56+
* The position of the returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer
4557
* (eg. through relative-read operations), so implementations must return a new
4658
* ByteBuffer or slice for each call.
4759
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
* {@code 2 * ((width + 1) / 2)}. Conversions in both directions use this
3535
* layout, and fail when the buffer is smaller than the frame.
3636
*
37+
* I420 planes must be non-null direct buffers, with positive dimensions,
38+
* strides at least as large as their plane widths, and capacity for every
39+
* pixel through the last row. The last row needs no trailing padding.
40+
* Destination planes must also be writable. Invalid planes cause a
41+
* {@link RuntimeException} before any destination pixels are written.
42+
* Plane positions and limits are ignored, as specified by {@link I420Buffer}.
43+
*
3744
* @author Alex Andres
3845
*/
3946
public final class VideoBufferConverter {

0 commit comments

Comments
 (0)