1 /* 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 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 */ I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight, int dstStrideY, int dstSliceHeightY, int dstStrideU, int dstSliceHeightU)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 40 // to calculate the exact end 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. */ I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight)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 /** 69 * Copy I420 Buffer to a contiguously allocated buffer. 70 * @param dstStrideY the stride of output buffers' Y plane. 71 * @param dstSliceHeightY the slice-height of output buffer's Y plane. 72 */ I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight, int dstStrideY, int dstSliceHeightY)73 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 74 ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight, int dstStrideY, 75 int dstSliceHeightY) { 76 final int chromaHeight = (dstHeight + 1) / 2; 77 final int chromaWidth = (dstWidth + 1) / 2; 78 79 final int dstStartY = 0; 80 final int dstEndY = dstStartY + dstStrideY * dstHeight; 81 final int dstStartUV = dstStartY + dstStrideY * dstSliceHeightY; 82 final int dstEndUV = dstStartUV + chromaWidth * chromaHeight * 2; 83 if (dst.capacity() < dstEndUV) { 84 throw new IllegalArgumentException("Expected destination buffer capacity to be at least " 85 + dstEndUV + " was " + dst.capacity()); 86 } 87 88 dst.limit(dstEndY); 89 dst.position(dstStartY); 90 final ByteBuffer dstY = dst.slice(); 91 dst.limit(dstEndUV); 92 dst.position(dstStartUV); 93 final ByteBuffer dstUV = dst.slice(); 94 95 I420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, 96 chromaWidth * 2, dstWidth, dstHeight); 97 } 98 99 /** Helper method for copying I420 to tightly packed NV12 destination buffer. */ I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight)100 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 101 ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int dstWidth, int dstHeight) { 102 I420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dst, dstWidth, dstHeight, 103 dstWidth, dstHeight); 104 } 105 106 /** Helper method for rotating I420 to tightly packed destination buffer. */ I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int srcWidth, int srcHeight, int rotationMode)107 public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 108 ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int srcWidth, int srcHeight, 109 int rotationMode) { 110 final int dstWidth = rotationMode % 180 == 0 ? srcWidth : srcHeight; 111 final int dstHeight = rotationMode % 180 == 0 ? srcHeight : srcWidth; 112 113 final int dstChromaHeight = (dstHeight + 1) / 2; 114 final int dstChromaWidth = (dstWidth + 1) / 2; 115 116 final int minSize = dstWidth * dstHeight + dstChromaWidth * dstChromaHeight * 2; 117 if (dst.capacity() < minSize) { 118 throw new IllegalArgumentException("Expected destination buffer capacity to be at least " 119 + minSize + " was " + dst.capacity()); 120 } 121 122 final int startY = 0; 123 final int startU = dstHeight * dstWidth; 124 final int startV = startU + dstChromaHeight * dstChromaWidth; 125 126 dst.position(startY); 127 final ByteBuffer dstY = dst.slice(); 128 dst.position(startU); 129 final ByteBuffer dstU = dst.slice(); 130 dst.position(startV); 131 final ByteBuffer dstV = dst.slice(); 132 133 nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstWidth, dstU, 134 dstChromaWidth, dstV, dstChromaWidth, srcWidth, srcHeight, rotationMode); 135 } 136 137 /** Helper method for copying a single colour plane. */ copyPlane( ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height)138 public static void copyPlane( 139 ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height) { 140 nativeCopyPlane(src, srcStride, dst, dstStride, width, height); 141 } 142 143 /** Converts ABGR little endian (rgba in memory) to I420. */ ABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height)144 public static void ABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, int dstStrideY, 145 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { 146 nativeABGRToI420( 147 src, srcStride, dstY, dstStrideY, dstU, dstStrideU, dstV, dstStrideV, width, height); 148 } 149 150 /** 151 * Copies I420 to the I420 dst buffer. 152 * <p> Unlike `libyuv::I420Copy`, this function checks if the height <= 0, so flipping is not 153 * supported. 154 */ I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height)155 public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 156 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, 157 int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { 158 if (srcY == null || srcU == null || srcV == null || dstY == null || dstU == null || dstV == null 159 || width <= 0 || height <= 0) { 160 throw new IllegalArgumentException("Invalid I420Copy input arguments"); 161 } 162 nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, 163 dstStrideU, dstV, dstStrideV, width, height); 164 } 165 I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, int dstStrideUV, int width, int height)166 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 167 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, 168 int dstStrideUV, int width, int height) { 169 if (srcY == null || srcU == null || srcV == null || dstY == null || dstUV == null || width <= 0 170 || height <= 0) { 171 throw new IllegalArgumentException("Invalid I420ToNV12 input arguments"); 172 } 173 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, 174 dstStrideUV, width, height); 175 } 176 I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, int rotationMode)177 public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 178 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, 179 int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, 180 int rotationMode) { 181 nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, 182 dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode); 183 } 184 nativeCopyPlane( ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height)185 private static native void nativeCopyPlane( 186 ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height); nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height)187 private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, 188 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, 189 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height); nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, int dstStrideUV, int width, int height)190 private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, 191 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, 192 ByteBuffer dstUV, int dstStrideUV, int width, int height); nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, int rotationMode)193 private static native void nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, 194 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, 195 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, 196 int rotationMode); nativeABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height)197 private static native void nativeABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, 198 int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, 199 int height); 200 } 201