| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 package org.webrtc; | 11 package org.webrtc; |
| 12 | 12 |
| 13 import java.nio.ByteBuffer; | 13 import java.nio.ByteBuffer; |
| 14 import org.webrtc.VideoFrame.I420Buffer; | 14 import org.webrtc.VideoFrame.I420Buffer; |
| 15 | 15 |
| 16 /** Implementation of an I420 VideoFrame buffer. */ | 16 /** Implementation of an I420 VideoFrame buffer. */ |
| 17 class I420BufferImpl implements VideoFrame.I420Buffer { | 17 class I420BufferImpl implements VideoFrame.I420Buffer { |
| 18 private final ByteBuffer buffer; | |
| 19 private final int width; | 18 private final int width; |
| 20 private final int height; | 19 private final int height; |
| 21 private final int chromaHeight; | 20 private final ByteBuffer dataY; |
| 22 private final int yPos; | 21 private final ByteBuffer dataU; |
| 22 private final ByteBuffer dataV; |
| 23 private final int strideY; | 23 private final int strideY; |
| 24 private final int uPos; | |
| 25 private final int strideU; | 24 private final int strideU; |
| 26 private final int vPos; | |
| 27 private final int strideV; | 25 private final int strideV; |
| 28 private final ReleaseCallback releaseCallback; | 26 private final Runnable releaseCallback; |
| 29 | 27 |
| 30 private int refCount; | 28 private int refCount; |
| 31 | 29 |
| 32 /** Allocates an I420Buffer backed by existing data. */ | 30 /** Constructs an I420Buffer backed by existing data. */ |
| 33 I420BufferImpl(ByteBuffer buffer, int width, int height, int yPos, int strideY
, int uPos, | 31 I420BufferImpl(int width, int height, ByteBuffer dataY, int strideY, ByteBuffe
r dataU, |
| 34 int strideU, int vPos, int strideV, ReleaseCallback releaseCallback) { | 32 int strideU, ByteBuffer dataV, int strideV, Runnable releaseCallback) { |
| 35 this.buffer = buffer; | |
| 36 this.width = width; | 33 this.width = width; |
| 37 this.height = height; | 34 this.height = height; |
| 38 this.chromaHeight = (height + 1) / 2; | 35 this.dataY = dataY; |
| 39 this.yPos = yPos; | 36 this.dataU = dataU; |
| 37 this.dataV = dataV; |
| 40 this.strideY = strideY; | 38 this.strideY = strideY; |
| 41 this.uPos = uPos; | |
| 42 this.strideU = strideU; | 39 this.strideU = strideU; |
| 43 this.vPos = vPos; | |
| 44 this.strideV = strideV; | 40 this.strideV = strideV; |
| 45 this.releaseCallback = releaseCallback; | 41 this.releaseCallback = releaseCallback; |
| 46 | 42 |
| 47 this.refCount = 1; | 43 this.refCount = 1; |
| 48 } | 44 } |
| 49 | 45 |
| 50 /** Allocates an empty I420Buffer suitable for an image of the given dimension
s. */ | 46 /** Allocates an empty I420Buffer suitable for an image of the given dimension
s. */ |
| 51 static I420BufferImpl allocate(int width, int height) { | 47 static I420BufferImpl allocate(int width, int height) { |
| 52 int chromaHeight = (height + 1) / 2; | 48 int chromaHeight = (height + 1) / 2; |
| 53 int strideUV = (width + 1) / 2; | 49 int strideUV = (width + 1) / 2; |
| 54 int yPos = 0; | 50 int yPos = 0; |
| 55 int uPos = yPos + width * height; | 51 int uPos = yPos + width * height; |
| 56 int vPos = uPos + strideUV * chromaHeight; | 52 int vPos = uPos + strideUV * chromaHeight; |
| 53 |
| 57 ByteBuffer buffer = ByteBuffer.allocateDirect(width * height + 2 * strideUV
* chromaHeight); | 54 ByteBuffer buffer = ByteBuffer.allocateDirect(width * height + 2 * strideUV
* chromaHeight); |
| 58 return new I420BufferImpl( | 55 |
| 59 buffer, width, height, yPos, width, uPos, strideUV, vPos, strideUV, null
); | 56 buffer.position(yPos); |
| 57 buffer.limit(uPos); |
| 58 ByteBuffer dataY = buffer.slice(); |
| 59 |
| 60 buffer.position(uPos); |
| 61 buffer.limit(vPos); |
| 62 ByteBuffer dataU = buffer.slice(); |
| 63 |
| 64 buffer.position(vPos); |
| 65 buffer.limit(vPos + strideUV * chromaHeight); |
| 66 ByteBuffer dataV = buffer.slice(); |
| 67 |
| 68 return new I420BufferImpl(width, height, dataY, width, dataU, strideUV, data
V, strideUV, null); |
| 60 } | 69 } |
| 61 | 70 |
| 62 @Override | 71 @Override |
| 63 public int getWidth() { | 72 public int getWidth() { |
| 64 return width; | 73 return width; |
| 65 } | 74 } |
| 66 | 75 |
| 67 @Override | 76 @Override |
| 68 public int getHeight() { | 77 public int getHeight() { |
| 69 return height; | 78 return height; |
| 70 } | 79 } |
| 71 | 80 |
| 72 @Override | 81 @Override |
| 73 public ByteBuffer getDataY() { | 82 public ByteBuffer getDataY() { |
| 74 ByteBuffer data = buffer.slice(); | 83 return dataY; |
| 75 data.position(yPos); | |
| 76 data.limit(yPos + getStrideY() * height); | |
| 77 return data; | |
| 78 } | 84 } |
| 79 | 85 |
| 80 @Override | 86 @Override |
| 81 public ByteBuffer getDataU() { | 87 public ByteBuffer getDataU() { |
| 82 ByteBuffer data = buffer.slice(); | 88 return dataU; |
| 83 data.position(uPos); | |
| 84 data.limit(uPos + strideU * chromaHeight); | |
| 85 return data; | |
| 86 } | 89 } |
| 87 | 90 |
| 88 @Override | 91 @Override |
| 89 public ByteBuffer getDataV() { | 92 public ByteBuffer getDataV() { |
| 90 ByteBuffer data = buffer.slice(); | 93 return dataV; |
| 91 data.position(vPos); | |
| 92 data.limit(vPos + strideV * chromaHeight); | |
| 93 return data; | |
| 94 } | 94 } |
| 95 | 95 |
| 96 @Override | 96 @Override |
| 97 public int getStrideY() { | 97 public int getStrideY() { |
| 98 return strideY; | 98 return strideY; |
| 99 } | 99 } |
| 100 | 100 |
| 101 @Override | 101 @Override |
| 102 public int getStrideU() { | 102 public int getStrideU() { |
| 103 return strideU; | 103 return strideU; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 } | 114 } |
| 115 | 115 |
| 116 @Override | 116 @Override |
| 117 public void retain() { | 117 public void retain() { |
| 118 ++refCount; | 118 ++refCount; |
| 119 } | 119 } |
| 120 | 120 |
| 121 @Override | 121 @Override |
| 122 public void release() { | 122 public void release() { |
| 123 if (--refCount == 0 && releaseCallback != null) { | 123 if (--refCount == 0 && releaseCallback != null) { |
| 124 releaseCallback.onRelease(); | 124 releaseCallback.run(); |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 | 127 |
| 128 // Callback called when the frame is no longer referenced. | 128 @Override |
| 129 interface ReleaseCallback { | 129 public VideoFrame.Buffer cropAndScale( |
| 130 // Called when the frame is no longer referenced. | 130 int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int s
caleHeight) { |
| 131 void onRelease(); | 131 return VideoFrame.cropAndScaleI420( |
| 132 this, cropX, cropY, cropWidth, cropHeight, scaleWidth, scaleHeight); |
| 132 } | 133 } |
| 133 } | 134 } |
| OLD | NEW |