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. */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 16 | @JNINamespace("webrtc::jni") |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 17 | public class YuvHelper { |
| 18 | /** Helper method for copying I420 to tightly packed destination buffer. */ |
| 19 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 20 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) { |
| 21 | final int chromaHeight = (height + 1) / 2; |
| 22 | final int chromaWidth = (width + 1) / 2; |
| 23 | |
| 24 | final int minSize = width * height + chromaWidth * chromaHeight * 2; |
| 25 | if (dst.capacity() < minSize) { |
| 26 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 27 | + minSize + " was " + dst.capacity()); |
| 28 | } |
| 29 | |
| 30 | final int startY = 0; |
| 31 | final int startU = height * width; |
| 32 | final int startV = startU + chromaHeight * chromaWidth; |
| 33 | |
| 34 | dst.position(startY); |
| 35 | final ByteBuffer dstY = dst.slice(); |
| 36 | dst.position(startU); |
| 37 | final ByteBuffer dstU = dst.slice(); |
| 38 | dst.position(startV); |
| 39 | final ByteBuffer dstV = dst.slice(); |
| 40 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 41 | nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstU, |
| 42 | chromaWidth, dstV, chromaWidth, width, height); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** Helper method for copying I420 to tightly packed NV12 destination buffer. */ |
| 46 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 47 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) { |
| 48 | final int chromaWidth = (width + 1) / 2; |
| 49 | final int chromaHeight = (height + 1) / 2; |
| 50 | |
| 51 | final int minSize = width * height + chromaWidth * chromaHeight * 2; |
| 52 | if (dst.capacity() < minSize) { |
| 53 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 54 | + minSize + " was " + dst.capacity()); |
| 55 | } |
| 56 | |
| 57 | final int startY = 0; |
| 58 | final int startUV = height * width; |
| 59 | |
| 60 | dst.position(startY); |
| 61 | final ByteBuffer dstY = dst.slice(); |
| 62 | dst.position(startUV); |
| 63 | final ByteBuffer dstUV = dst.slice(); |
| 64 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 65 | nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstUV, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 66 | chromaWidth * 2, width, height); |
| 67 | } |
| 68 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame^] | 69 | /** Helper method for rotating I420 to tightly packed destination buffer. */ |
| 70 | public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 71 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int srcWidth, int srcHeight, |
| 72 | int rotationMode) { |
| 73 | final int dstWidth = rotationMode % 180 == 0 ? srcWidth : srcHeight; |
| 74 | final int dstHeight = rotationMode % 180 == 0 ? srcHeight : srcWidth; |
| 75 | |
| 76 | final int dstChromaHeight = (dstHeight + 1) / 2; |
| 77 | final int dstChromaWidth = (dstWidth + 1) / 2; |
| 78 | |
| 79 | final int minSize = dstWidth * dstHeight + dstChromaWidth * dstChromaHeight * 2; |
| 80 | if (dst.capacity() < minSize) { |
| 81 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 82 | + minSize + " was " + dst.capacity()); |
| 83 | } |
| 84 | |
| 85 | final int startY = 0; |
| 86 | final int startU = dstHeight * dstWidth; |
| 87 | final int startV = startU + dstChromaHeight * dstChromaWidth; |
| 88 | |
| 89 | dst.position(startY); |
| 90 | final ByteBuffer dstY = dst.slice(); |
| 91 | dst.position(startU); |
| 92 | final ByteBuffer dstU = dst.slice(); |
| 93 | dst.position(startV); |
| 94 | final ByteBuffer dstV = dst.slice(); |
| 95 | |
| 96 | nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstWidth, dstU, |
| 97 | dstChromaWidth, dstV, dstChromaWidth, srcWidth, srcHeight, rotationMode); |
| 98 | } |
| 99 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 100 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 101 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 102 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) { |
| 103 | nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 104 | dstStrideU, dstV, dstStrideV, width, height); |
| 105 | } |
| 106 | |
| 107 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 108 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV, |
| 109 | int dstStrideUV, int width, int height) { |
| 110 | nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, |
| 111 | dstStrideUV, width, height); |
| 112 | } |
| 113 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame^] | 114 | public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 115 | ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU, |
| 116 | int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, |
| 117 | int rotationMode) { |
| 118 | nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 119 | dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode); |
| 120 | } |
| 121 | |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 122 | private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 123 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 124 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 125 | private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 126 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 127 | ByteBuffer dstUV, int dstStrideUV, int width, int height); |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame^] | 128 | private static native void nativeI420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 129 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 130 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight, |
| 131 | int rotationMode); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 132 | } |