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