Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [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; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 14 | import javax.annotation.Nullable; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 15 | import org.webrtc.VideoFrame.I420Buffer; |
| 16 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 17 | /** Implementation of VideoFrame.I420Buffer backed by Java direct byte buffers. */ |
| 18 | public class JavaI420Buffer implements VideoFrame.I420Buffer { |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 19 | private final int width; |
| 20 | private final int height; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 21 | private final ByteBuffer dataY; |
| 22 | private final ByteBuffer dataU; |
| 23 | private final ByteBuffer dataV; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 24 | private final int strideY; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 25 | private final int strideU; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 26 | private final int strideV; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 27 | private final RefCountDelegate refCountDelegate; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 28 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 29 | private JavaI420Buffer(int width, int height, ByteBuffer dataY, int strideY, ByteBuffer dataU, |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 30 | int strideU, ByteBuffer dataV, int strideV, @Nullable Runnable releaseCallback) { |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 31 | this.width = width; |
| 32 | this.height = height; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 33 | this.dataY = dataY; |
| 34 | this.dataU = dataU; |
| 35 | this.dataV = dataV; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 36 | this.strideY = strideY; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 37 | this.strideU = strideU; |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 38 | this.strideV = strideV; |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 39 | this.refCountDelegate = new RefCountDelegate(releaseCallback); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 42 | /** Wraps existing ByteBuffers into JavaI420Buffer object without copying the contents. */ |
| 43 | public static JavaI420Buffer wrap(int width, int height, ByteBuffer dataY, int strideY, |
| 44 | ByteBuffer dataU, int strideU, ByteBuffer dataV, int strideV, Runnable releaseCallback) { |
| 45 | if (dataY == null || dataU == null || dataV == null) { |
| 46 | throw new IllegalArgumentException("Data buffers cannot be null."); |
| 47 | } |
| 48 | if (!dataY.isDirect() || !dataU.isDirect() || !dataV.isDirect()) { |
| 49 | throw new IllegalArgumentException("Data buffers must be direct byte buffers."); |
| 50 | } |
| 51 | |
| 52 | // Slice the buffers to prevent external modifications to the position / limit of the buffer. |
| 53 | // Note that this doesn't protect the contents of the buffers from modifications. |
| 54 | dataY = dataY.slice(); |
| 55 | dataU = dataU.slice(); |
| 56 | dataV = dataV.slice(); |
| 57 | |
| 58 | final int chromaHeight = (height + 1) / 2; |
| 59 | final int minCapacityY = strideY * height; |
| 60 | final int minCapacityU = strideU * chromaHeight; |
| 61 | final int minCapacityV = strideV * chromaHeight; |
| 62 | if (dataY.capacity() < minCapacityY) { |
| 63 | throw new IllegalArgumentException("Y-buffer must be at least " + minCapacityY + " bytes."); |
| 64 | } |
| 65 | if (dataU.capacity() < minCapacityU) { |
| 66 | throw new IllegalArgumentException("U-buffer must be at least " + minCapacityU + " bytes."); |
| 67 | } |
| 68 | if (dataV.capacity() < minCapacityV) { |
| 69 | throw new IllegalArgumentException("V-buffer must be at least " + minCapacityV + " bytes."); |
| 70 | } |
| 71 | |
| 72 | return new JavaI420Buffer( |
| 73 | width, height, dataY, strideY, dataU, strideU, dataV, strideV, releaseCallback); |
| 74 | } |
| 75 | |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 76 | /** Allocates an empty I420Buffer suitable for an image of the given dimensions. */ |
Sami Kalliomäki | 48b3c02 | 2017-10-04 17:01:00 +0200 | [diff] [blame] | 77 | public static JavaI420Buffer allocate(int width, int height) { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 78 | int chromaHeight = (height + 1) / 2; |
| 79 | int strideUV = (width + 1) / 2; |
| 80 | int yPos = 0; |
| 81 | int uPos = yPos + width * height; |
| 82 | int vPos = uPos + strideUV * chromaHeight; |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 83 | |
Sami Kalliomäki | 0611065 | 2018-02-20 11:19:58 +0100 | [diff] [blame] | 84 | ByteBuffer buffer = |
| 85 | JniCommon.nativeAllocateByteBuffer(width * height + 2 * strideUV * chromaHeight); |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 86 | |
| 87 | buffer.position(yPos); |
| 88 | buffer.limit(uPos); |
| 89 | ByteBuffer dataY = buffer.slice(); |
| 90 | |
| 91 | buffer.position(uPos); |
| 92 | buffer.limit(vPos); |
| 93 | ByteBuffer dataU = buffer.slice(); |
| 94 | |
| 95 | buffer.position(vPos); |
| 96 | buffer.limit(vPos + strideUV * chromaHeight); |
| 97 | ByteBuffer dataV = buffer.slice(); |
| 98 | |
Sami Kalliomäki | 0611065 | 2018-02-20 11:19:58 +0100 | [diff] [blame] | 99 | return new JavaI420Buffer(width, height, dataY, width, dataU, strideUV, dataV, strideUV, |
| 100 | () -> { JniCommon.nativeFreeByteBuffer(buffer); }); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public int getWidth() { |
| 105 | return width; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int getHeight() { |
| 110 | return height; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public ByteBuffer getDataY() { |
Bjorn Mellem | d629314 | 2017-09-29 11:33:52 -0700 | [diff] [blame] | 115 | // Return a slice to prevent relative reads from changing the position. |
| 116 | return dataY.slice(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public ByteBuffer getDataU() { |
Bjorn Mellem | d629314 | 2017-09-29 11:33:52 -0700 | [diff] [blame] | 121 | // Return a slice to prevent relative reads from changing the position. |
| 122 | return dataU.slice(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public ByteBuffer getDataV() { |
Bjorn Mellem | d629314 | 2017-09-29 11:33:52 -0700 | [diff] [blame] | 127 | // Return a slice to prevent relative reads from changing the position. |
| 128 | return dataV.slice(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public int getStrideY() { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 133 | return strideY; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public int getStrideU() { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 138 | return strideU; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public int getStrideV() { |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 143 | return strideV; |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public I420Buffer toI420() { |
sakal | 5ca60cc | 2017-08-09 05:25:49 -0700 | [diff] [blame] | 148 | retain(); |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 149 | return this; |
| 150 | } |
| 151 | |
| 152 | @Override |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 153 | public void retain() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 154 | refCountDelegate.retain(); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 155 | } |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 156 | |
| 157 | @Override |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 158 | public void release() { |
Sami Kalliomäki | 61db3fd | 2018-04-09 17:51:19 +0200 | [diff] [blame] | 159 | refCountDelegate.release(); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 160 | } |
| 161 | |
sakal | 836f60c | 2017-07-28 07:12:23 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public VideoFrame.Buffer cropAndScale( |
| 164 | int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) { |
| 165 | return VideoFrame.cropAndScaleI420( |
| 166 | this, cropX, cropY, cropWidth, cropHeight, scaleWidth, scaleHeight); |
Bjorn Mellem | 8fb2361 | 2017-07-18 11:33:39 -0700 | [diff] [blame] | 167 | } |
Bjorn Mellem | 9fbbdc2 | 2017-06-16 09:23:50 -0700 | [diff] [blame] | 168 | } |