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 { |
| 17 | /** Helper method for copying I420 to tightly packed destination buffer. */ |
| 18 | public static void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 19 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) { |
| 20 | final int chromaHeight = (height + 1) / 2; |
| 21 | final int chromaWidth = (width + 1) / 2; |
| 22 | |
| 23 | final int minSize = width * height + chromaWidth * chromaHeight * 2; |
| 24 | if (dst.capacity() < minSize) { |
| 25 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 26 | + minSize + " was " + dst.capacity()); |
| 27 | } |
| 28 | |
| 29 | final int startY = 0; |
| 30 | final int startU = height * width; |
| 31 | final int startV = startU + chromaHeight * chromaWidth; |
| 32 | |
| 33 | dst.position(startY); |
| 34 | final ByteBuffer dstY = dst.slice(); |
| 35 | dst.position(startU); |
| 36 | final ByteBuffer dstU = dst.slice(); |
| 37 | dst.position(startV); |
| 38 | final ByteBuffer dstV = dst.slice(); |
| 39 | |
| 40 | I420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstU, chromaWidth, |
| 41 | dstV, chromaWidth, width, height); |
| 42 | } |
| 43 | |
| 44 | /** Helper method for copying I420 to tightly packed NV12 destination buffer. */ |
| 45 | public static void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, int srcStrideU, |
| 46 | ByteBuffer srcV, int srcStrideV, ByteBuffer dst, int width, int height) { |
| 47 | final int chromaWidth = (width + 1) / 2; |
| 48 | final int chromaHeight = (height + 1) / 2; |
| 49 | |
| 50 | final int minSize = width * height + chromaWidth * chromaHeight * 2; |
| 51 | if (dst.capacity() < minSize) { |
| 52 | throw new IllegalArgumentException("Expected destination buffer capacity to be at least " |
| 53 | + minSize + " was " + dst.capacity()); |
| 54 | } |
| 55 | |
| 56 | final int startY = 0; |
| 57 | final int startUV = height * width; |
| 58 | |
| 59 | dst.position(startY); |
| 60 | final ByteBuffer dstY = dst.slice(); |
| 61 | dst.position(startUV); |
| 62 | final ByteBuffer dstUV = dst.slice(); |
| 63 | |
| 64 | I420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, width, dstUV, |
| 65 | chromaWidth * 2, width, height); |
| 66 | } |
| 67 | |
| 68 | public static native void I420Copy(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 69 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 70 | ByteBuffer dstU, int dstStrideU, ByteBuffer dstV, int dstStrideV, int width, int height); |
| 71 | public static native void I420ToNV12(ByteBuffer srcY, int srcStrideY, ByteBuffer srcU, |
| 72 | int srcStrideU, ByteBuffer srcV, int srcStrideV, ByteBuffer dstY, int dstStrideY, |
| 73 | ByteBuffer dstUV, int dstStrideUV, int width, int height); |
| 74 | } |