Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 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. */ |
| 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 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 40 | nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstU, |
| 41 | chromaWidth, dstV, chromaWidth, width, height); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** Helper method for copying I420 to tightly packed NV12 destination buffer. */ |
| 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 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 64 | nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstUV, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 65 | chromaWidth * 2, width, height); |
| 66 | } |
| 67 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 68 | /** Helper method for rotating I420 to tightly packed destination buffer. */ |
| 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 | |
Sami Kalliomäki | ee98be7 | 2018-05-08 15:22:08 +0200 | [diff] [blame] | 99 | /** Helper method for copying a single colour plane. */ |
| 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 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 105 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 106 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 107 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { |
| 108 | nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 109 | dstStrideU, dstV, dstStrideV, width, height); |
| 110 | } |
| 111 | |
| 112 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 113 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, |
| 114 | int dstStrideUV, int width, int height) { |
| 115 | nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, |
| 116 | dstStrideUV, width, height); |
| 117 | } |
| 118 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 119 | public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 120 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 121 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, |
| 122 | int rotationMode) { |
| 123 | nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 124 | dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode); |
| 125 | } |
| 126 | |
Sami Kalliomäki | ee98be7 | 2018-05-08 15:22:08 +0200 | [diff] [blame] | 127 | private static native void nativeCopyPlane( |
| 128 | ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 129 | private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 130 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 131 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 132 | private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 133 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 134 | ByteBuffer dstUV, int dstStrideUV, int width, int height); |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 135 | private static native void nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 136 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 137 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, |
| 138 | int rotationMode); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 139 | } |