|
| 1 | +# Video Buffer Converter |
| 2 | + |
| 3 | +The VideoBufferConverter provides fast pixel format conversions between WebRTC's internal I420 video frame buffers and other formats identified by a FourCC. Conversions are delegated to optimized native routines. |
| 4 | + |
| 5 | +API: `dev.onvoid.webrtc.media.video.VideoBufferConverter` |
| 6 | + |
| 7 | +## When to use it |
| 8 | +- Rendering frames in UI toolkits that expect interleaved RGB(A) byte layouts. |
| 9 | +- Preparing frames for encoders/decoders that require specific pixel formats. |
| 10 | +- Importing external pixel data (e.g., RGBA, NV12) into the WebRTC pipeline as I420. |
| 11 | + |
| 12 | +See also: [Video Capture](../video_capture.md), [Custom Video Source](../custom_video_source.md). |
| 13 | + |
| 14 | +## Supported operations |
| 15 | + |
| 16 | +1) From I420 to other pixel formats |
| 17 | +- `convertFromI420(VideoFrameBuffer src, byte[] dst, FourCC fourCC)` |
| 18 | +- `convertFromI420(VideoFrameBuffer src, ByteBuffer dst, FourCC fourCC)` |
| 19 | + |
| 20 | +2) From other pixel formats to I420 |
| 21 | +- `convertToI420(byte[] src, I420Buffer dst, FourCC fourCC)` |
| 22 | +- `convertToI420(ByteBuffer src, I420Buffer dst, FourCC fourCC)` |
| 23 | + |
| 24 | +Notes: |
| 25 | +- The VideoFrameBuffer is internally converted to I420 if necessary using `VideoFrameBuffer#toI420()` before transformation. |
| 26 | +- When using ByteBuffer destinations/sources, direct buffers use a zero-copy native path for best performance; otherwise, the method will use the backing array or a temporary array. |
| 27 | + |
| 28 | +## FourCC formats |
| 29 | + |
| 30 | +The target/source pixel layout is selected with `dev.onvoid.webrtc.media.FourCC`. Common values include: |
| 31 | +- `FourCC.RGBA` – 4 bytes per pixel, RGBA order (commonly used with BufferedImage TYPE_4BYTE_ABGR interop, see example below) |
| 32 | +- `FourCC.ARGB`, `FourCC.ABGR`, `FourCC.BGRA` – other 32-bit packed variants |
| 33 | +- `FourCC.NV12`, `FourCC.NV21` – 4:2:0 semi-planar YUV formats |
| 34 | + |
| 35 | +Consult the FourCC enum in your version for the complete list. |
| 36 | + |
| 37 | +## Buffer sizing |
| 38 | + |
| 39 | +You must allocate destination buffers large enough for the chosen format: |
| 40 | +- For 32-bit RGBA-like formats: `width * height * 4` bytes |
| 41 | +- For NV12/NV21: `width * height * 3 / 2` bytes |
| 42 | +- For other layouts, compute according to their specification |
| 43 | + |
| 44 | +Attempting to convert into undersized buffers will result in an error. |
| 45 | + |
| 46 | +## Example: Convert VideoFrame to BufferedImage |
| 47 | + |
| 48 | +This example demonstrates converting a WebRTC VideoFrame to a Java BufferedImage using RGBA output. |
| 49 | + |
| 50 | +```java |
| 51 | +import dev.onvoid.webrtc.media.FourCC; |
| 52 | +import dev.onvoid.webrtc.media.video.VideoBufferConverter; |
| 53 | +import dev.onvoid.webrtc.media.video.VideoFrame; |
| 54 | +import dev.onvoid.webrtc.media.video.VideoFrameBuffer; |
| 55 | + |
| 56 | +import java.awt.image.BufferedImage; |
| 57 | +import java.awt.image.DataBufferByte; |
| 58 | + |
| 59 | +public void onVideoFrame(VideoFrame frame) { |
| 60 | + try { |
| 61 | + // Get frame dimensions |
| 62 | + VideoFrameBuffer frameBuffer = frame.buffer; |
| 63 | + int frameWidth = frameBuffer.getWidth(); |
| 64 | + int frameHeight = frameBuffer.getHeight(); |
| 65 | + |
| 66 | + // Create a BufferedImage with ABGR format (compatible with RGBA conversion) |
| 67 | + BufferedImage image = new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_4BYTE_ABGR); |
| 68 | + |
| 69 | + // Get the underlying byte array from the BufferedImage |
| 70 | + byte[] imageBuffer = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); |
| 71 | + |
| 72 | + // Convert the frame buffer from I420 format to RGBA format |
| 73 | + VideoBufferConverter.convertFromI420(frameBuffer, imageBuffer, FourCC.RGBA); |
| 74 | + |
| 75 | + // Now you can use the BufferedImage for display or further processing |
| 76 | + // e.g., display in Swing/JavaFX |
| 77 | + } |
| 78 | + catch (Exception e) { |
| 79 | + // Handle conversion errors |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + finally { |
| 83 | + // Always release the frame when done |
| 84 | + frame.release(); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +How it works: |
| 90 | +1. Create a BufferedImage sized to the frame. |
| 91 | +2. Access its backing byte[] via DataBufferByte. |
| 92 | +3. Convert the VideoFrameBuffer from I420 to RGBA into the image buffer. |
| 93 | + |
| 94 | +Tip: If you have a direct NIO ByteBuffer (e.g., for native interop), use the ByteBuffer overload to keep a direct native path. |
| 95 | + |
| 96 | +## Example: Import RGBA data into I420 |
| 97 | + |
| 98 | +```java |
| 99 | +import dev.onvoid.webrtc.media.FourCC; |
| 100 | +import dev.onvoid.webrtc.media.video.VideoBufferConverter; |
| 101 | +import dev.onvoid.webrtc.media.video.VideoFrame; |
| 102 | +import dev.onvoid.webrtc.media.video.VideoFrameBuffer; |
| 103 | + |
| 104 | +import java.awt.image.BufferedImage; |
| 105 | +import java.awt.image.DataBufferByte; |
| 106 | + |
| 107 | +public void onImage(BufferedImage image) { |
| 108 | + // Get image dimensions |
| 109 | + int imageWidth = image.getWidth(); |
| 110 | + int imageHeight = image.getHeight(); |
| 111 | + |
| 112 | + // Create a I420Buffer |
| 113 | + NativeI420Buffer i420 = NativeI420Buffer.allocate(imageWidth, imageHeight); |
| 114 | + |
| 115 | + // Get the underlying byte array from the BufferedImage |
| 116 | + byte[] imageBuffer = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); |
| 117 | + |
| 118 | + try { |
| 119 | + // In this example, we assume the BufferedImage is in 4 byte ABGR format |
| 120 | + VideoBufferConverter.convertToI420(imageBuffer, i420, FourCC.RGBA); |
| 121 | + |
| 122 | + // Now you can use the I420Buffer, e.g., wrap in a VideoFrame |
| 123 | + } |
| 124 | + catch (Exception e) { |
| 125 | + // Handle conversion errors |
| 126 | + e.printStackTrace(); |
| 127 | + } |
| 128 | + finally { |
| 129 | + // Always release the buffer when done |
| 130 | + i420.release(); |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## Error handling and edge cases |
| 136 | +- All methods throw NullPointerException if src/dst is null; ensure proper checks. |
| 137 | +- ByteBuffer destinations must be writable (not read-only) for `convertFromI420`. |
| 138 | +- Ensure the correct FourCC is used for the actual memory layout you pass/expect. |
| 139 | +- Beware of frame rotation metadata; conversions do not rotate pixels. Handle `VideoFrame.rotation` separately if your renderer requires upright images. |
| 140 | + |
| 141 | +## Related |
| 142 | +- [Video Capture](../video_capture.md) |
| 143 | +- [Custom Video Source](../custom_video_source.md) |
| 144 | +- [Screen Capturer](../screen_capturer.md) |
| 145 | +- [Window Capturer](../window_capturer.md) |
0 commit comments