| 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; |
| 18 private final int width; | 19 private final int width; |
| 19 private final int height; | 20 private final int height; |
| 20 private final int strideUV; | 21 private final int chromaHeight; |
| 21 private final ByteBuffer y; | 22 private final int yPos; |
| 22 private final ByteBuffer u; | 23 private final int strideY; |
| 23 private final ByteBuffer v; | 24 private final int uPos; |
| 25 private final int strideU; |
| 26 private final int vPos; |
| 27 private final int strideV; |
| 28 private final ReleaseCallback releaseCallback; |
| 24 | 29 |
| 25 I420BufferImpl(int width, int height) { | 30 private int refCount; |
| 31 |
| 32 /** Allocates an I420Buffer backed by existing data. */ |
| 33 I420BufferImpl(ByteBuffer buffer, int width, int height, int yPos, int strideY
, int uPos, |
| 34 int strideU, int vPos, int strideV, ReleaseCallback releaseCallback) { |
| 35 this.buffer = buffer; |
| 26 this.width = width; | 36 this.width = width; |
| 27 this.height = height; | 37 this.height = height; |
| 28 this.strideUV = (width + 1) / 2; | 38 this.chromaHeight = (height + 1) / 2; |
| 29 int halfHeight = (height + 1) / 2; | 39 this.yPos = yPos; |
| 30 this.y = ByteBuffer.allocateDirect(width * height); | 40 this.strideY = strideY; |
| 31 this.u = ByteBuffer.allocateDirect(strideUV * halfHeight); | 41 this.uPos = uPos; |
| 32 this.v = ByteBuffer.allocateDirect(strideUV * halfHeight); | 42 this.strideU = strideU; |
| 43 this.vPos = vPos; |
| 44 this.strideV = strideV; |
| 45 this.releaseCallback = releaseCallback; |
| 46 |
| 47 this.refCount = 1; |
| 48 } |
| 49 |
| 50 /** Allocates an empty I420Buffer suitable for an image of the given dimension
s. */ |
| 51 static I420BufferImpl allocate(int width, int height) { |
| 52 int chromaHeight = (height + 1) / 2; |
| 53 int strideUV = (width + 1) / 2; |
| 54 int yPos = 0; |
| 55 int uPos = yPos + width * height; |
| 56 int vPos = uPos + strideUV * chromaHeight; |
| 57 ByteBuffer buffer = ByteBuffer.allocateDirect(width * height + 2 * strideUV
* chromaHeight); |
| 58 return new I420BufferImpl( |
| 59 buffer, width, height, yPos, width, uPos, strideUV, vPos, strideUV, null
); |
| 33 } | 60 } |
| 34 | 61 |
| 35 @Override | 62 @Override |
| 36 public int getWidth() { | 63 public int getWidth() { |
| 37 return width; | 64 return width; |
| 38 } | 65 } |
| 39 | 66 |
| 40 @Override | 67 @Override |
| 41 public int getHeight() { | 68 public int getHeight() { |
| 42 return height; | 69 return height; |
| 43 } | 70 } |
| 44 | 71 |
| 45 @Override | 72 @Override |
| 46 public ByteBuffer getDataY() { | 73 public ByteBuffer getDataY() { |
| 47 return y; | 74 ByteBuffer data = buffer.slice(); |
| 75 data.position(yPos); |
| 76 data.limit(yPos + getStrideY() * height); |
| 77 return data; |
| 48 } | 78 } |
| 49 | 79 |
| 50 @Override | 80 @Override |
| 51 public ByteBuffer getDataU() { | 81 public ByteBuffer getDataU() { |
| 52 return u; | 82 ByteBuffer data = buffer.slice(); |
| 83 data.position(uPos); |
| 84 data.limit(uPos + strideU * chromaHeight); |
| 85 return data; |
| 53 } | 86 } |
| 54 | 87 |
| 55 @Override | 88 @Override |
| 56 public ByteBuffer getDataV() { | 89 public ByteBuffer getDataV() { |
| 57 return v; | 90 ByteBuffer data = buffer.slice(); |
| 91 data.position(vPos); |
| 92 data.limit(vPos + strideV * chromaHeight); |
| 93 return data; |
| 58 } | 94 } |
| 59 | 95 |
| 60 @Override | 96 @Override |
| 61 public int getStrideY() { | 97 public int getStrideY() { |
| 62 return width; | 98 return strideY; |
| 63 } | 99 } |
| 64 | 100 |
| 65 @Override | 101 @Override |
| 66 public int getStrideU() { | 102 public int getStrideU() { |
| 67 return strideUV; | 103 return strideU; |
| 68 } | 104 } |
| 69 | 105 |
| 70 @Override | 106 @Override |
| 71 public int getStrideV() { | 107 public int getStrideV() { |
| 72 return strideUV; | 108 return strideV; |
| 73 } | 109 } |
| 74 | 110 |
| 75 @Override | 111 @Override |
| 76 public I420Buffer toI420() { | 112 public I420Buffer toI420() { |
| 77 return this; | 113 return this; |
| 78 } | 114 } |
| 79 | 115 |
| 80 @Override | 116 @Override |
| 81 public void retain() {} | 117 public void retain() { |
| 118 ++refCount; |
| 119 } |
| 82 | 120 |
| 83 @Override | 121 @Override |
| 84 public void release() {} | 122 public void release() { |
| 123 if (--refCount == 0 && releaseCallback != null) { |
| 124 releaseCallback.onRelease(); |
| 125 } |
| 126 } |
| 127 |
| 128 // Callback called when the frame is no longer referenced. |
| 129 interface ReleaseCallback { |
| 130 // Called when the frame is no longer referenced. |
| 131 void onRelease(); |
| 132 } |
| 85 } | 133 } |
| OLD | NEW |