blob: 2d6877bc3358c39307c17f232e1e9052c66b1a46 [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
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010069 public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
70 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstU,
71 int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height) {
72 nativeI420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstU,
73 dstStrideU, dstV, dstStrideV, width, height);
74 }
75
76 public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU,
77 ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, ByteBuffer dstUV,
78 int dstStrideUV, int width, int height) {
79 nativeI420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY, dstUV,
80 dstStrideUV, width, height);
81 }
82
83 private static native void nativeI420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010084 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
85 ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height);
Magnus Jedvert84d8ae52017-12-20 15:12:10 +010086 private static native void nativeI420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU,
Sami Kalliomäkif6515cd2017-11-02 11:25:58 +010087 int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY,
88 ByteBuffer dstUV, int dstStrideUV, int width, int height);
89}