blob: 105584c8e7e50e921b46fb48a7d5b03bb1de6d57 [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
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100100 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äki75db5522018-01-22 14:19:16 +0100114 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 Jedvert84d8ae52017-12-20 15:12:10 +0100122 private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100123 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
124 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +0100125 private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +0100126 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
127 ByteBuffer dstUV, int dstStrideUV, int width, int height);
Sami Kalliomäki75db5522018-01-22 14:19:16 +0100128 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äkif6515cd2017-11-02 11:25:58 +0100132}