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 { |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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 | */ |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 28 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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; |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 33 | |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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) { |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 43 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 44 | + dstEndV + " was " + dst.capacity()); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 47 | dst.limit(dstEndY); |
| 48 | dst.position(dstStartY); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 49 | final ByteBuffer dstY = dst.slice(); |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 50 | dst.limit(dstEndU); |
| 51 | dst.position(dstStartU); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 52 | final ByteBuffer dstU = dst.slice(); |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 53 | dst.limit(dstEndV); |
| 54 | dst.position(dstStartV); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 55 | final ByteBuffer dstV = dst.slice(); |
| 56 | |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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 | /** |
| 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 | */ |
| 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); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** Helper method for copying I420 to tightly packed NV12 destination buffer. */ |
| 100 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 106 | /** Helper method for rotating I420 to tightly packed destination buffer. */ |
| 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 | |
Sami Kalliomäki | ee98be7 | 2018-05-08 15:22:08 +0200 | [diff] [blame] | 137 | /** Helper method for copying a single colour plane. */ |
| 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 | |
Sami Kalliomäki | b86a177 | 2019-11-21 11:08:54 +0100 | [diff] [blame] | 143 | /** Converts ABGR little endian (rgba in memory) to I420. */ |
| 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 | |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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 | */ |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 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) { |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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 | } |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 162 | nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU, |
| 163 | dstStrideU, dstV, dstStrideV, width, height); |
| 164 | } |
| 165 | |
| 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) { |
Byoungchan Lee | 0b06552 | 2022-02-18 09:52:11 +0900 | [diff] [blame^] | 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 | } |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 173 | nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV, |
| 174 | dstStrideUV, width, height); |
| 175 | } |
| 176 | |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 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 | |
Sami Kalliomäki | ee98be7 | 2018-05-08 15:22:08 +0200 | [diff] [blame] | 185 | private static native void nativeCopyPlane( |
| 186 | ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 187 | private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 188 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 189 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height); |
Magnus Jedvert | 84d8ae5 | 2017-12-20 15:12:10 +0100 | [diff] [blame] | 190 | private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 191 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 192 | ByteBuffer dstUV, int dstStrideUV, int width, int height); |
Sami Kalliomäki | 75db552 | 2018-01-22 14:19:16 +0100 | [diff] [blame] | 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); |
Sami Kalliomäki | b86a177 | 2019-11-21 11:08:54 +0100 | [diff] [blame] | 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); |
Sami Kalliomäki | f6515cd | 2017-11-02 11:25:58 +0100 | [diff] [blame] | 200 | } |