Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/tools/video/video-buffer-converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public void onImage(BufferedImage image) {
## Error handling and edge cases
- All methods throw `NullPointerException` if src/dst is null; ensure proper checks.
- `ByteBuffer` destinations must be writable (not read-only) for `convertFromI420`.
- 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.
- 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.
- 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.
- 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.
- Ensure the correct FourCC is used for the actual memory layout you pass/expect.
- Beware of frame rotation metadata; conversions do not rotate pixels. Handle `VideoFrame.rotation` separately if your renderer requires upright images.

Expand Down
7 changes: 7 additions & 0 deletions webrtc-jni/src/main/cpp/include/api/VideoFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ namespace jni

namespace I420Buffer
{
// Checks a plane's dimensions, stride and direct-buffer capacity before
// native code reads or writes it. The last row needs only width bytes.
// Position and limit are ignored, as in I420Buffer's Java contract.
// Writable destinations must not be read-only. Throws on invalid input.
void CheckPlane(JNIEnv * env, const char * name, jobject data, jint stride,
jint width, jint height, bool writable = false);

// Wraps the pixels of the buffer in a NativeI420Buffer without
// copying them. With readOnly its planes are read-only byte buffers,
// for pixels that others share.
Expand Down
26 changes: 25 additions & 1 deletion webrtc-jni/src/main/cpp/src/JNI_VideoBufferConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "JavaRuntimeException.h"
#include "JavaUtils.h"

#include "api/video/i420_buffer.h"
#include "api/VideoFrame.h"

#include "libyuv/convert.h"
#include "libyuv/video_common.h"
Expand Down Expand Up @@ -159,6 +159,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I
return;
}

const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
jni::I420Buffer::CheckPlane(env, "Y", jSrcY, srcStrideY, width, height);
jni::I420Buffer::CheckPlane(env, "U", jSrcU, srcStrideU, chromaWidth, chromaHeight);
jni::I420Buffer::CheckPlane(env, "V", jSrcV, srcStrideV, chromaWidth, chromaHeight);

const uint8_t * srcY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcY));
const uint8_t * srcU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcU));
const uint8_t * srcV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcV));
Expand Down Expand Up @@ -191,6 +197,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_I
jobject jSrcV, jint srcStrideV, jobject dst, jint width, jint height, jint fourCC)
{
jni::CallFromJava(env, [&]() {
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
jni::I420Buffer::CheckPlane(env, "Y", jSrcY, srcStrideY, width, height);
jni::I420Buffer::CheckPlane(env, "U", jSrcU, srcStrideU, chromaWidth, chromaHeight);
jni::I420Buffer::CheckPlane(env, "V", jSrcV, srcStrideV, chromaWidth, chromaHeight);

const uint8_t * srcY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcY));
const uint8_t * srcU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcU));
const uint8_t * srcV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jSrcV));
Expand Down Expand Up @@ -232,6 +244,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_b
return;
}

const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
jni::I420Buffer::CheckPlane(env, "Y", jDstY, dstStrideY, width, height, true);
jni::I420Buffer::CheckPlane(env, "U", jDstU, dstStrideU, chromaWidth, chromaHeight, true);
jni::I420Buffer::CheckPlane(env, "V", jDstV, dstStrideV, chromaWidth, chromaHeight, true);

uint8_t * dstY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstY));
uint8_t * dstU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstU));
uint8_t * dstV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstV));
Expand Down Expand Up @@ -268,6 +286,12 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_video_VideoBufferConverter_d
jobject jDstU, jint dstStrideU, jobject jDstV, jint dstStrideV, jint fourCC)
{
jni::CallFromJava(env, [&]() {
const jint chromaWidth = static_cast<jint>((width + 1LL) / 2);
const jint chromaHeight = static_cast<jint>((height + 1LL) / 2);
jni::I420Buffer::CheckPlane(env, "Y", jDstY, dstStrideY, width, height, true);
jni::I420Buffer::CheckPlane(env, "U", jDstU, dstStrideU, chromaWidth, chromaHeight, true);
jni::I420Buffer::CheckPlane(env, "V", jDstV, dstStrideV, chromaWidth, chromaHeight, true);

uint8_t * dstY = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstY));
uint8_t * dstU = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstU));
uint8_t * dstV = static_cast<uint8_t *>(env->GetDirectBufferAddress(jDstV));
Expand Down
74 changes: 53 additions & 21 deletions webrtc-jni/src/main/cpp/src/api/VideoFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ namespace jni
{
namespace
{
class JavaByteBufferClass : public JavaClass
{
public:
explicit JavaByteBufferClass(JNIEnv * env)
{
cls = FindClass(env, "java/nio/ByteBuffer");
isReadOnly = GetMethod(env, cls, "isReadOnly", "()Z");
}

jclass cls;
jmethodID isReadOnly;
};

// A direct buffer over a plane, or throws: the allocation may fail, with or without an exception pending.
JavaLocalRef<jobject> NewPlane(JNIEnv * env, const char * name, const uint8_t * data, jlong size)
{
Expand All @@ -55,24 +68,6 @@ namespace jni
return view;
}

// Throws unless the plane holds every byte a copy reads: the last row ends at the width.
void CheckPlane(JNIEnv * env, const char * name, const JavaRef<jobject> & data, jint stride, jint width, jint height)
{
if (stride < width) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "Stride %d of the %s plane is less than its width %d", stride, name, width)));
}

// Neither factor exceeds an int, so the product cannot overflow.
const int64_t required = static_cast<int64_t>(stride) * (height - 1) + width;
const jlong capacity = env->GetDirectBufferCapacity(data);

if (capacity < required) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The %s plane holds %lld bytes, but a %dx%d plane with stride %d needs %lld",
name, static_cast<long long>(capacity), width, height, stride, static_cast<long long>(required))));
}
}
}

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

CheckPlane(env, "Y", dataY, strideY, width, height);
CheckPlane(env, "U", dataU, strideU, chromaWidth, chromaHeight);
CheckPlane(env, "V", dataV, strideV, chromaWidth, chromaHeight);
I420Buffer::CheckPlane(env, "Y", dataY, strideY, width, height);
I420Buffer::CheckPlane(env, "U", dataU, strideU, chromaWidth, chromaHeight);
I420Buffer::CheckPlane(env, "V", dataV, strideV, chromaWidth, chromaHeight);

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

namespace I420Buffer
{
void CheckPlane(JNIEnv * env, const char * name, jobject data, jint stride,
jint width, jint height, bool writable)
{
if (width <= 0 || height <= 0 || stride < width) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "Invalid %s plane dimensions %dx%d or stride %d", name, width, height, stride)));
}
if (data == nullptr) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The %s plane is null", name)));
}

const jlong capacity = env->GetDirectBufferCapacity(data);
if (capacity < 0 || env->GetDirectBufferAddress(data) == nullptr) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The %s plane must be a direct buffer", name)));
}

// Each factor is an int; the product and sum fit in 64 bits.
const int64_t required = static_cast<int64_t>(stride) * (height - 1) + width;
if (capacity < required) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The %s plane holds %lld bytes, but a %dx%d plane with stride %d needs %lld",
name, static_cast<long long>(capacity), width, height, stride, static_cast<long long>(required))));
}

if (writable) {
const auto javaClass = JavaClasses::get<JavaByteBufferClass>(env);
const jboolean readOnly = env->CallBooleanMethod(data, javaClass->isReadOnly);
ExceptionCheck(env);
if (readOnly) {
throw JavaWrappedException(JavaLocalRef<jthrowable>(env,
JavaRuntimeException(env, "The %s destination plane is read-only", name)));
}
}
}

JavaLocalRef<jobject> toJava(JNIEnv * env, const webrtc::scoped_refptr<webrtc::I420BufferInterface> & buffer,
bool readOnly)
{
Expand Down
24 changes: 18 additions & 6 deletions webrtc/src/main/java/dev/onvoid/webrtc/media/video/I420Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,42 @@

import java.nio.ByteBuffer;

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

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

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

/**
* Returns a direct ByteBuffer containing V-plane data. The buffer capacity is
* at least getStrideV() * ((getHeight() + 1) / 2) bytes. The position of the
* returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer
* large enough for every chroma row, without requiring trailing padding.
* The position of the returned buffer is ignored and must be 0. Callers may mutate the ByteBuffer
* (eg. through relative-read operations), so implementations must return a new
* ByteBuffer or slice for each call.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
* {@code 2 * ((width + 1) / 2)}. Conversions in both directions use this
* layout, and fail when the buffer is smaller than the frame.
*
* I420 planes must be non-null direct buffers, with positive dimensions,
* strides at least as large as their plane widths, and capacity for every
* pixel through the last row. The last row needs no trailing padding.
* Destination planes must also be writable. Invalid planes cause a
* {@link RuntimeException} before any destination pixels are written.
* Plane positions and limits are ignored, as specified by {@link I420Buffer}.
*
* @author Alex Andres
*/
public final class VideoBufferConverter {
Expand Down
Loading
Loading