blob: da94fec4bcc946a3d09362782d45b6b08f82c54d [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. */
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010016@JNINamespace("webrtc::jni")
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010017public 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 Jedvert84d8ae52017-12-20 15:12:10 +010041 nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstU,
42 chromaWidth, dstV, chromaWidth, width, height);
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010043 }
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 Jedvert84d8ae52017-12-20 15:12:10 +010065 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstUV,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010066 chromaWidth * 2, width, height);
67 }
68
Sami Kalliomäki75db5522018-01-22 14:19:16 +010069 /** 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
Sami Kalliomäkiee98be72018-05-08 15:22:08 +0200100 /** Helper method for copying a single colour plane. */
101 public static void copyPlane(
102 ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height) {
103 nativeCopyPlane(src, srcStride, dst, dstStride, width, height);
104 }
105
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100106 public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
107 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU,
108 int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) {
109 nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU,
110 dstStrideU, dstV, dstStrideV, width, height);
111 }
112
113 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
114 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV,
115 int dstStrideUV, int width, int height) {
116 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV,
117 dstStrideUV, width, height);
118 }
119
Sami Kalliomäki75db5522018-01-22 14:19:16 +0100120 public static void I420Rotate(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
121 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU,
122 int dstStrideU, ByteBuffer dstV, int dstStrideV, int srcWidth, int srcHeight,
123 int rotationMode) {
124 nativeI420Rotate(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU,
125 dstStrideU, dstV, dstStrideV, srcWidth, srcHeight, rotationMode);
126 }
127
Sami Kalliomäkiee98be72018-05-08 15:22:08 +0200128 private static native void nativeCopyPlane(
129 ByteBuffer src, int srcStride, ByteBuffer dst, int dstStride, int width, int height);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100130 private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100131 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
132 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100133 private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100134 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
135 ByteBuffer dstUV, int dstStrideUV, int width, int height);
Sami Kalliomäki75db5522018-01-22 14:19:16 +0100136 private static native void nativeI420Rotate(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 srcWidth, int srcHeight,
139 int rotationMode);
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100140}