16
16
17
17
package dev .onvoid .webrtc .media .video ;
18
18
19
- import dev .onvoid .webrtc .media .FourCC ;
20
-
21
19
import java .nio .ByteBuffer ;
22
20
21
+ import dev .onvoid .webrtc .media .FourCC ;
22
+
23
+ /**
24
+ * Utility methods to convert between I420 video frame buffers and other pixel
25
+ * formats represented by a FourCC. Delegates actual conversion work to native
26
+ * methods for performance.
27
+ *
28
+ * @author Alex Andres
29
+ */
23
30
public final class VideoBufferConverter {
24
31
32
+ /**
33
+ * Convert an arbitrary {@link VideoFrameBuffer} to the specified pixel format
34
+ * (given by {@code fourCC}) and write the result into a destination byte array.
35
+ * <p>
36
+ * The source buffer is first converted (if necessary) to an intermediate
37
+ * I420 layout via {@link VideoFrameBuffer#toI420()} and then transformed.
38
+ *
39
+ * @param src Source video frame buffer (will be converted to I420 if not already).
40
+ * @param dst Destination byte array expected to be large enough to hold the converted frame.
41
+ * @param fourCC Target FourCC format identifier.
42
+ *
43
+ * @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
44
+ * @throws Exception if the native conversion fails.
45
+ */
25
46
public static void convertFromI420 (VideoFrameBuffer src , byte [] dst , FourCC fourCC ) throws Exception {
26
47
if (src == null ) {
27
48
throw new NullPointerException ("Source buffer must not be null" );
@@ -40,7 +61,21 @@ public static void convertFromI420(VideoFrameBuffer src, byte[] dst, FourCC four
40
61
i420 .getWidth (), i420 .getHeight (),
41
62
fourCC .value ());
42
63
}
43
-
64
+
65
+ /**
66
+ * Convert an arbitrary {@link VideoFrameBuffer} to the specified pixel format
67
+ * and write the result into a {@link ByteBuffer}. If the destination buffer
68
+ * is a direct buffer, a native direct path is used; otherwise its backing
69
+ * array (or a temporary array) is employed.
70
+ *
71
+ * @param src Source video frame buffer.
72
+ * @param dst Writable destination {@link ByteBuffer}. Must not be read-only.
73
+ * @param fourCC Target FourCC format identifier.
74
+ *
75
+ * @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
76
+ * @throws IllegalArgumentException if {@code dst} is read-only.
77
+ * @throws Exception if the native conversion fails.
78
+ */
44
79
public static void convertFromI420 (VideoFrameBuffer src , ByteBuffer dst , FourCC fourCC ) throws Exception {
45
80
if (src == null ) {
46
81
throw new NullPointerException ("Source buffer must not be null" );
@@ -84,6 +119,17 @@ public static void convertFromI420(VideoFrameBuffer src, ByteBuffer dst, FourCC
84
119
}
85
120
}
86
121
122
+ /**
123
+ * Convert a source frame stored in a byte array (encoded in the format indicated
124
+ * by {@code fourCC}) to I420 and write the planes into the provided {@link I420Buffer}.
125
+ *
126
+ * @param src Source pixel data in the specified FourCC format.
127
+ * @param dst Destination I420 buffer (pre-allocated).
128
+ * @param fourCC FourCC describing the layout of {@code src}.
129
+ *
130
+ * @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
131
+ * @throws Exception if the native conversion fails.
132
+ */
87
133
public static void convertToI420 (byte [] src , I420Buffer dst , FourCC fourCC ) throws Exception {
88
134
if (src == null ) {
89
135
throw new NullPointerException ("Source buffer must not be null" );
@@ -101,6 +147,19 @@ public static void convertToI420(byte[] src, I420Buffer dst, FourCC fourCC) thro
101
147
fourCC .value ());
102
148
}
103
149
150
+ /**
151
+ * Convert a source frame stored in a {@link ByteBuffer} (encoded in the format
152
+ * specified by {@code fourCC}) to I420 and write the result into the provided
153
+ * {@link I420Buffer}. Uses a direct native path for direct buffers; otherwise
154
+ * reads from the backing or a temporary array.
155
+ *
156
+ * @param src Source pixel data buffer.
157
+ * @param dst Destination I420 buffer (pre-allocated).
158
+ * @param fourCC FourCC describing the layout of {@code src}.
159
+ *
160
+ * @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
161
+ * @throws Exception if the native conversion fails.
162
+ */
104
163
public static void convertToI420 (ByteBuffer src , I420Buffer dst , FourCC fourCC ) throws Exception {
105
164
if (src == null ) {
106
165
throw new NullPointerException ("Source buffer must not be null" );
0 commit comments