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 /** 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 width, int height)18 public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 19 ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) { 20 final int chromaHeight = (height + 1) / 2; 21 final int chromaWidth = (width + 1) / 2; 22 23 final int minSize = width * height + chromaWidth * chromaHeight * 2; 24 if (dst.capacity() < minSize) { 25 throw new IllegalArgumentException("Expected destination buffer capacity to be at least " 26 + minSize + " was " + dst.capacity()); 27 } 28 29 final int startY = 0; 30 final int startU = height * width; 31 final int startV = startU + chromaHeight * chromaWidth; 32 33 dst.position(startY); 34 final ByteBuffer dstY = dst.slice(); 35 dst.position(startU); 36 final ByteBuffer dstU = dst.slice(); 37 dst.position(startV); 38 final ByteBuffer dstV = dst.slice(); 39 40 nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstU, 41 chromaWidth, dstV, chromaWidth, width, height); 42 } 43 44 /** 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 width, int height)45 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 46 ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) { 47 final int chromaWidth = (width + 1) / 2; 48 final int chromaHeight = (height + 1) / 2; 49 50 final int minSize = width * height + chromaWidth * chromaHeight * 2; 51 if (dst.capacity() < minSize) { 52 throw new IllegalArgumentException("Expected destination buffer capacity to be at least " 53 + minSize + " was " + dst.capacity()); 54 } 55 56 final int startY = 0; 57 final int startUV = height * width; 58 59 dst.position(startY); 60 final ByteBuffer dstY = dst.slice(); 61 dst.position(startUV); 62 final ByteBuffer dstUV = dst.slice(); 63 64 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstUV, 65 chromaWidth * 2, width, height); 66 } 67 68 /** 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)69 public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 70 ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int srcWidth, int srcHeight, 71 int rotationMode) { 72 final int dstWidth = rotationMode % 180 == 0 ? srcWidth : srcHeight; 73 final int dstHeight = rotationMode % 180 == 0 ? srcHeight : srcWidth; 74 75 final int dstChromaHeight = (dstHeight + 1) / 2; 76 final int dstChromaWidth = (dstWidth + 1) / 2; 77 78 final int minSize = dstWidth * dstHeight + dstChromaWidth * dstChromaHeight * 2; 79 if (dst.capacity() < minSize) { 80 throw new IllegalArgumentException("Expected destination buffer capacity to be at least " 81 + minSize + " was " + dst.capacity()); 82 } 83 84 final int startY = 0; 85 final int startU = dstHeight * dstWidth; 86 final int startV = startU + dstChromaHeight * dstChromaWidth; 87 88 dst.position(startY); 89 final ByteBuffer dstY = dst.slice(); 90 dst.position(startU); 91 final ByteBuffer dstU = dst.slice(); 92 dst.position(startV); 93 final ByteBuffer dstV = dst.slice(); 94 95 nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstWidth, dstU, 96 dstChromaWidth, dstV, dstChromaWidth, srcWidth, srcHeight, rotationMode); 97 } 98 99 /** Helper method for copying a single colour plane. */ copyPlane( ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height)100 public static void copyPlane( 101 ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height) { 102 nativeCopyPlane(src, srcStride, dst, dstStride, width, height); 103 } 104 105 /** 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)106 public static void ABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, int dstStrideY, 107 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { 108 nativeABGRToI420( 109 src, srcStride, dstY, dstStrideY, dstU, dstStrideU, dstV, dstStrideV, width, height); 110 } 111 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)112 public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 113 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, 114 int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { 115 nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, 116 dstStrideU, dstV, dstStrideV, width, height); 117 } 118 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)119 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 120 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, 121 int dstStrideUV, int width, int height) { 122 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, 123 dstStrideUV, width, height); 124 } 125 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)126 public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, 127 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, 128 int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, 129 int rotationMode) { 130 nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, 131 dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode); 132 } 133 nativeCopyPlane( ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height)134 private static native void nativeCopyPlane( 135 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)136 private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, 137 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, 138 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)139 private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, 140 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, 141 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)142 private static native void nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, 143 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, 144 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, 145 int rotationMode); nativeABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height)146 private static native void nativeABGRToI420(ByteBuffer src, int srcStride, ByteBuffer dstY, 147 int dstStrideY, ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, 148 int height); 149 } 150