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, | |
sakal
2017/07/17 13:44:56
I would prefer to just have a constructor that is
mellem
2017/07/17 21:57:15
Changed the second constructor to an allocate() me
| |
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 = half(height); |
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 width and height. */ | |
51 I420BufferImpl(int width, int height) { | |
52 this(ByteBuffer.allocateDirect(size(width, height)), width, height, 0 /* yPo s */, | |
53 width /* strideY */, width * height /* uPos */, half(width) /* strideU * /, | |
54 width * height + half(width) * half(height) /* vPos */, half(width) /* s trideV */, | |
55 null /* releaseCallback */); | |
33 } | 56 } |
34 | 57 |
35 @Override | 58 @Override |
36 public int getWidth() { | 59 public int getWidth() { |
37 return width; | 60 return width; |
38 } | 61 } |
39 | 62 |
40 @Override | 63 @Override |
41 public int getHeight() { | 64 public int getHeight() { |
42 return height; | 65 return height; |
43 } | 66 } |
44 | 67 |
45 @Override | 68 @Override |
46 public ByteBuffer getDataY() { | 69 public ByteBuffer getDataY() { |
47 return y; | 70 ByteBuffer data = buffer.slice(); |
71 data.position(yPos); | |
72 data.limit(yPos + getStrideY() * height); | |
73 return data; | |
48 } | 74 } |
49 | 75 |
50 @Override | 76 @Override |
51 public ByteBuffer getDataU() { | 77 public ByteBuffer getDataU() { |
52 return u; | 78 ByteBuffer data = buffer.slice(); |
79 data.position(uPos); | |
80 data.limit(uPos + strideU * chromaHeight); | |
81 return data; | |
53 } | 82 } |
54 | 83 |
55 @Override | 84 @Override |
56 public ByteBuffer getDataV() { | 85 public ByteBuffer getDataV() { |
57 return v; | 86 ByteBuffer data = buffer.slice(); |
87 data.position(vPos); | |
88 data.limit(vPos + strideV * chromaHeight); | |
89 return data; | |
58 } | 90 } |
59 | 91 |
60 @Override | 92 @Override |
61 public int getStrideY() { | 93 public int getStrideY() { |
62 return width; | 94 return strideY; |
63 } | 95 } |
64 | 96 |
65 @Override | 97 @Override |
66 public int getStrideU() { | 98 public int getStrideU() { |
67 return strideUV; | 99 return strideU; |
68 } | 100 } |
69 | 101 |
70 @Override | 102 @Override |
71 public int getStrideV() { | 103 public int getStrideV() { |
72 return strideUV; | 104 return strideV; |
73 } | 105 } |
74 | 106 |
75 @Override | 107 @Override |
76 public I420Buffer toI420() { | 108 public I420Buffer toI420() { |
77 return this; | 109 return this; |
78 } | 110 } |
79 | 111 |
80 @Override | 112 @Override |
81 public void retain() {} | 113 public void retain() { |
114 ++refCount; | |
115 } | |
82 | 116 |
83 @Override | 117 @Override |
84 public void release() {} | 118 public void release() { |
119 if (--refCount == 0 && releaseCallback != null) { | |
120 releaseCallback.onRelease(); | |
121 } | |
122 } | |
123 | |
124 // Callback called when the frame is no longer referenced. | |
125 interface ReleaseCallback { | |
126 // Called when the frame is no longer referenced. | |
127 void onRelease(); | |
128 } | |
129 | |
130 private static int half(int num) { | |
131 return (num + 1) / 2; | |
132 } | |
133 | |
134 private static int size(int width, int height) { | |
135 return width * height + half(width) * half(height) * 2; | |
136 } | |
85 } | 137 } |
OLD | NEW |