blob: afb8e837d11ca6df3dd2bc68980d5cd37e3ac772 [file] [log] [blame]
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +01001/*
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
11package org.webrtc;
12
13import java.nio.ByteBuffer;
14
15/** Wraps libyuv methods to Java. All passed byte buffers must be direct byte buffers. */
16public class YuvHelper {
Byoungchan Lee0b065522022-02-18 09:52:11 +090017 /**
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äkif6515cd2017-11-02 11:25:58 +010028 public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
Byoungchan Lee0b065522022-02-18 09:52:11 +090029 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äkif6515cd2017-11-02 11:25:58 +010033
Byoungchan Lee0b065522022-02-18 09:52:11 +090034 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äkif6515cd2017-11-02 11:25:58 +010043 throw new IllegalArgumentException("Expected destination buffer capacity to be at least "
Byoungchan Lee0b065522022-02-18 09:52:11 +090044 + dstEndV + " was " + dst.capacity());
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010045 }
46
Byoungchan Lee0b065522022-02-18 09:52:11 +090047 dst.limit(dstEndY);
48 dst.position(dstStartY);
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010049 final ByteBuffer dstY = dst.slice();
Byoungchan Lee0b065522022-02-18 09:52:11 +090050 dst.limit(dstEndU);
51 dst.position(dstStartU);
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010052 final ByteBuffer dstU = dst.slice();
Byoungchan Lee0b065522022-02-18 09:52:11 +090053 dst.limit(dstEndV);
54 dst.position(dstStartV);
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010055 final ByteBuffer dstV = dst.slice();
56
Byoungchan Lee0b065522022-02-18 09:52:11 +090057 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äkif6515cd2017-11-02 11:25:58 +010097 }
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 Lee0b065522022-02-18 09:52:11 +0900101 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äkif6515cd2017-11-02 11:25:58 +0100104 }
105
Sami Kalliomäki75db5522018-01-22 14:19:16 +0100106 /** 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äkiee98be72018-05-08 15:22:08 +0200137 /** 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äkib86a1772019-11-21 11:08:54 +0100143 /** 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 Lee0b065522022-02-18 09:52:11 +0900150 /**
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 Jedvert84d8ae52017-12-20 15:12:10 +0100155 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 Lee0b065522022-02-18 09:52:11 +0900158 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 Jedvert84d8ae52017-12-20 15:12:10 +0100162 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 Lee0b065522022-02-18 09:52:11 +0900169 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 Jedvert84d8ae52017-12-20 15:12:10 +0100173 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV,
174 dstStrideUV, width, height);
175 }
176
Sami Kalliomäki75db5522018-01-22 14:19:16 +0100177 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äkiee98be72018-05-08 15:22:08 +0200185 private static native void nativeCopyPlane(
186 ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100187 private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100188 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
189 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100190 private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100191 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
192 ByteBuffer dstUV, int dstStrideUV, int width, int height);
Sami Kalliomäki75db5522018-01-22 14:19:16 +0100193 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äkib86a1772019-11-21 11:08:54 +0100197 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äkif6515cd2017-11-02 11:25:58 +0100200}