| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | package org.webrtc; |
| 12 |
| 13 | import java.nio.ByteBuffer; |
| 14 |
| 15 | /** Wraps libyuv methods to Java. All passed byte buffers must be direct byte buffers. */ |
| 16 | public class YuvHelper { |
| 17 | /** |
| 18 | * Copy I420 Buffer to a contiguously allocated buffer. |
| 19 | * <p> In Android, MediaCodec can request a buffer of a specific layout with the stride and |
| 20 | * slice-height (or plane height), and this function is used in this case. |
| 21 | * <p> For more information, see |
| 22 | * https://cs.android.com/android/platform/superproject/+/64fea7e5726daebc40f46890100837c01091100d:frameworks/base/media/java/android/media/MediaFormat.java;l=568 |
| 23 | * @param dstStrideY the stride of output buffers' Y plane. |
| 24 | * @param dstSliceHeightY the slice-height of output buffer's Y plane. |
| 25 | * @param dstStrideU the stride of output buffers' U (and V) plane. |
| 26 | * @param dstSliceHeightU the slice-height of output buffer's U (and V) plane |
| 27 | */ |
| 28 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 29 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight, int dstStrideY, |
| 30 | int dstSliceHeightY, int dstStrideU, int dstSliceHeightU) { |
| 31 | final int chromaWidth = (dstWidth + 1) / 2; |
| 32 | final int chromaHeight = (dstHeight + 1) / 2; |
| 33 |
| 34 | final int dstStartY = 0; |
| 35 | final int dstEndY = dstStartY + dstStrideY * dstHeight; |
| 36 | final int dstStartU = dstStartY + dstStrideY * dstSliceHeightY; |
| 37 | final int dstEndU = dstStartU + dstStrideU * chromaHeight; |
| 38 | final int dstStartV = dstStartU + dstStrideU * dstSliceHeightU; |
| 39 | // The last line doesn't need any padding, so use chromaWidth to calculate the exact end |
| 40 | // position. |
| 41 | final int dstEndV = dstStartV + dstStrideU * (chromaHeight - 1) + chromaWidth; |
| 42 | if (dst.capacity() < dstEndV) { |
| 43 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 44 | + dstEndV + " was " + dst.capacity()); |
| 45 | } |
| 46 |
| 47 | dst.limit(dstEndY); |
| 48 | dst.position(dstStartY); |
| 49 | final ByteBuffer dstY = dst.slice(); |
| 50 | dst.limit(dstEndU); |
| 51 | dst.position(dstStartU); |
| 52 | final ByteBuffer dstU = dst.slice(); |
| 53 | dst.limit(dstEndV); |
| 54 | dst.position(dstStartV); |
| 55 | final ByteBuffer dstV = dst.slice(); |
| 56 |
| 57 | I420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 58 | dstStrideU, dstV, dstStrideU, dstWidth, dstHeight); |
| 59 | } |
| 60 |
| 61 | /** Helper method for copying I420 to tightly packed destination buffer. */ |
| 62 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 63 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight) { |
| 64 | I420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dst, dstWidth, dstHeight, |
| 65 | dstWidth, dstHeight, (dstWidth + 1) / 2, (dstHeight + 1) / 2); |
| 66 | } |
| 67 |
| 68 | /** Helper method for copying I420 to buffer with the given stride and slice height. */ |
| 69 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 70 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight, int dstStride, |
| 71 | int dstSliceHeight) { |
| 72 | I420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dst, dstWidth, dstHeight, |
| 73 | dstStride, dstSliceHeight, (dstStride + 1) / 2, (dstSliceHeight + 1) / 2); |
| 74 | } |
| 75 |
| 76 | /** |
| 77 | * Copy I420 Buffer to a contiguously allocated buffer. |
| 78 | * @param dstStrideY the stride of output buffers' Y plane. |
| 79 | * @param dstSliceHeightY the slice-height of output buffer's Y plane. |
| 80 | */ |
| 81 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 82 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight, int dstStrideY, |
| 83 | int dstSliceHeightY) { |
| 84 | final int chromaHeight = (dstHeight + 1) / 2; |
| 85 | final int chromaWidth = (dstWidth + 1) / 2; |
| 86 |
| 87 | final int dstStartY = 0; |
| 88 | final int dstEndY = dstStartY + dstStrideY * dstHeight; |
| 89 | final int dstStartUV = dstStartY + dstStrideY * dstSliceHeightY; |
| 90 | final int dstEndUV = dstStartUV + chromaWidth * chromaHeight * 2; |
| 91 | if (dst.capacity() < dstEndUV) { |
| 92 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 93 | + dstEndUV + " was " + dst.capacity()); |
| 94 | } |
| 95 |
| 96 | dst.limit(dstEndY); |
| 97 | dst.position(dstStartY); |
| 98 | final ByteBuffer dstY = dst.slice(); |
| 99 | dst.limit(dstEndUV); |
| 100 | dst.position(dstStartUV); |
| 101 | final ByteBuffer dstUV = dst.slice(); |
| 102 |
| 103 | I420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, |
| 104 | chromaWidth * 2, dstWidth, dstHeight); |
| 105 | } |
| 106 |
| 107 | /** Helper method for copying I420 to tightly packed NV12 destination buffer. */ |
| 108 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 109 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight) { |
| 110 | I420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dst, dstWidth, dstHeight, |
| 111 | dstWidth, dstHeight); |
| 112 | } |
| 113 |
| 114 | /** Helper method for rotating I420 to tightly packed destination buffer. */ |
| 115 | public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 116 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int srcWidth, int srcHeight, |
| 117 | int rotationMode) { |
| 118 | checkNotNull(srcY, "srcY"); |
| 119 | checkNotNull(srcU, "srcU"); |
| 120 | checkNotNull(srcV, "srcV"); |
| 121 | checkNotNull(dst, "dst"); |
| 122 | final int dstWidth = rotationMode % 180 == 0 ? srcWidth : srcHeight; |
| 123 | final int dstHeight = rotationMode % 180 == 0 ? srcHeight : srcWidth; |
| 124 |
| 125 | final int dstChromaHeight = (dstHeight + 1) / 2; |
| 126 | final int dstChromaWidth = (dstWidth + 1) / 2; |
| 127 |
| 128 | final int minSize = dstWidth * dstHeight + dstChromaWidth * dstChromaHeight * 2; |
| 129 | if (dst.capacity() < minSize) { |
| 130 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 131 | + minSize + " was " + dst.capacity()); |
| 132 | } |
| 133 |
| 134 | final int startY = 0; |
| 135 | final int startU = dstHeight * dstWidth; |
| 136 | final int startV = startU + dstChromaHeight * dstChromaWidth; |
| 137 |
| 138 | dst.position(startY); |
| 139 | final ByteBuffer dstY = dst.slice(); |
| 140 | dst.position(startU); |
| 141 | final ByteBuffer dstU = dst.slice(); |
| 142 | dst.position(startV); |
| 143 | final ByteBuffer dstV = dst.slice(); |
| 144 |
| 145 | nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstWidth, dstU, |
| 146 | dstChromaWidth, dstV, dstChromaWidth, srcWidth, srcHeight, rotationMode); |
| 147 | } |
| 148 |
| 149 | /** Helper method for copying a single colour plane. */ |
| 150 | public static void copyPlane( |
| 151 | ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height) { |
| 152 | nativeCopyPlane( |
| 153 | checkNotNull(src, "src"), srcStride, checkNotNull(dst, "dst"), dstStride, width, height); |
| 154 | } |
| 155 |
| 156 | /** Converts ABGR little endian (rgba in memory) to I420. */ |
| 157 | public static void ABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, int dstStrideY, |
| 158 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { |
| 159 | nativeABGRToI420(checkNotNull(src, "src"), srcStride, checkNotNull(dstY, "dstY"), dstStrideY, |
| 160 | checkNotNull(dstU, "dstU"), dstStrideU, checkNotNull(dstV, "dstV"), dstStrideV, width, |
| 161 | height); |
| 162 | } |
| 163 |
| 164 | /** |
| 165 | * Copies I420 to the I420 dst buffer. |
| 166 | * <p> Unlike `libyuv::I420Copy`, this function checks if the height <= 0, so flipping is not |
| 167 | * supported. |
| 168 | */ |
| 169 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 170 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 171 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { |
| 172 | checkNotNull(srcY, "srcY"); |
| 173 | checkNotNull(srcU, "srcU"); |
| 174 | checkNotNull(srcV, "srcV"); |
| 175 | checkNotNull(dstY, "dstY"); |
| 176 | checkNotNull(dstU, "dstU"); |
| 177 | checkNotNull(dstV, "dstV"); |
| 178 | if (width <= 0 || height <= 0) { |
| 179 | throw new IllegalArgumentException("I420Copy: width and height should not be negative"); |
| 180 | } |
| 181 | nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 182 | dstStrideU, dstV, dstStrideV, width, height); |
| 183 | } |
| 184 |
| 185 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 186 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, |
| 187 | int dstStrideUV, int width, int height) { |
| 188 | checkNotNull(srcY, "srcY"); |
| 189 | checkNotNull(srcU, "srcU"); |
| 190 | checkNotNull(srcV, "srcV"); |
| 191 | checkNotNull(dstY, "dstY"); |
| 192 | checkNotNull(dstUV, "dstUV"); |
| 193 | if (width <= 0 || height <= 0) { |
| 194 | throw new IllegalArgumentException("I420ToNV12: width and height should not be negative"); |
| 195 | } |
| 196 | nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, |
| 197 | dstStrideUV, width, height); |
| 198 | } |
| 199 |
| 200 | public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 201 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 202 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, |
| 203 | int rotationMode) { |
| 204 | checkNotNull(srcY, "srcY"); |
| 205 | checkNotNull(srcU, "srcU"); |
| 206 | checkNotNull(srcV, "srcV"); |
| 207 | checkNotNull(dstY, "dstY"); |
| 208 | checkNotNull(dstU, "dstU"); |
| 209 | checkNotNull(dstV, "dstV"); |
| 210 | nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 211 | dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode); |
| 212 | } |
| 213 |
| 214 | private static <T> T checkNotNull(T obj, String description) { |
| 215 | if (obj == null) { |
| 216 | throw new NullPointerException(description + " should not be null"); |
| 217 | } |
| 218 | return obj; |
| 219 | } |
| 220 |
| 221 | private static native void nativeCopyPlane( |
| 222 | ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height); |
| 223 | private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 224 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 225 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height); |
| 226 | private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 227 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 228 | ByteBuffer dstUV, int dstStrideUV, int width, int height); |
| 229 | private static native void nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 230 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 231 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, |
| 232 | int rotationMode); |
| 233 | private static native void nativeABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, |
| 234 | int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, |
| 235 | int height); |
| 236 | } |
| 237 |