blob: cca685b923314c7d1fdfefc82a3037931b38459b [file] [log] [blame]
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +00001/*
2 * Copyright (c) 2015 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#include "webrtc/common_video/interface/video_frame_buffer.h"
12
13#include "webrtc/base/checks.h"
14
15// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
16static const int kBufferAlignment = 64;
17
18namespace webrtc {
19
20VideoFrameBuffer::~VideoFrameBuffer() {}
21
22I420Buffer::I420Buffer(int width, int height)
23 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
24}
25
26I420Buffer::I420Buffer(int width,
27 int height,
28 int stride_y,
29 int stride_u,
30 int stride_v)
31 : width_(width),
32 height_(height),
33 stride_y_(stride_y),
34 stride_u_(stride_u),
35 stride_v_(stride_v),
36 data_(static_cast<uint8_t*>(AlignedMalloc(
37 stride_y * height + (stride_u + stride_v) * ((height + 1) / 2),
38 kBufferAlignment))) {
39 DCHECK_GT(width, 0);
40 DCHECK_GT(height, 0);
41 DCHECK_GE(stride_y, width);
42 DCHECK_GE(stride_u, (width + 1) / 2);
43 DCHECK_GE(stride_v, (width + 1) / 2);
44}
45
46I420Buffer::~I420Buffer() {
47}
48
49int I420Buffer::width() const {
50 return width_;
51}
52
53int I420Buffer::height() const {
54 return height_;
55}
56
57const uint8_t* I420Buffer::data(PlaneType type) const {
58 switch (type) {
59 case kYPlane:
60 return data_.get();
61 case kUPlane:
62 return data_.get() + stride_y_ * height_;
63 case kVPlane:
64 return data_.get() + stride_y_ * height_ +
65 stride_u_ * ((height_ + 1) / 2);
66 default:
67 RTC_NOTREACHED();
68 return nullptr;
69 }
70}
71
72uint8_t* I420Buffer::data(PlaneType type) {
73 DCHECK(HasOneRef());
74 return const_cast<uint8_t*>(
75 static_cast<const VideoFrameBuffer*>(this)->data(type));
76}
77
78int I420Buffer::stride(PlaneType type) const {
79 switch (type) {
80 case kYPlane:
81 return stride_y_;
82 case kUPlane:
83 return stride_u_;
84 case kVPlane:
85 return stride_v_;
86 default:
87 RTC_NOTREACHED();
88 return 0;
89 }
90}
91
92rtc::scoped_refptr<NativeHandle> I420Buffer::native_handle() const {
93 return nullptr;
94}
95
96TextureBuffer::TextureBuffer(
97 const rtc::scoped_refptr<NativeHandle>& native_handle,
98 int width,
99 int height)
100 : native_handle_(native_handle), width_(width), height_(height) {
101 DCHECK(native_handle.get());
102 DCHECK_GT(width, 0);
103 DCHECK_GT(height, 0);
104}
105
106TextureBuffer::~TextureBuffer() {
107}
108
109int TextureBuffer::width() const {
110 return width_;
111}
112
113int TextureBuffer::height() const {
114 return height_;
115}
116
117const uint8_t* TextureBuffer::data(PlaneType type) const {
118 RTC_NOTREACHED(); // Should not be called.
119 return nullptr;
120}
121
122uint8_t* TextureBuffer::data(PlaneType type) {
123 RTC_NOTREACHED(); // Should not be called.
124 return nullptr;
125}
126
127int TextureBuffer::stride(PlaneType type) const {
128 RTC_NOTREACHED(); // Should not be called.
129 return 0;
130}
131
132rtc::scoped_refptr<NativeHandle> TextureBuffer::native_handle() const {
133 return native_handle_;
134}
135
Per33544192015-04-02 12:30:51 +0200136
137WrappedI420Buffer::WrappedI420Buffer(int desired_width,
138 int desired_height,
139 int width,
140 int height,
141 const uint8_t* y_plane,
142 int y_stride,
143 const uint8_t* u_plane,
144 int u_stride,
145 const uint8_t* v_plane,
146 int v_stride,
147 const rtc::Callback0<void>& no_longer_used)
148 : width_(desired_width),
149 height_(desired_height),
150 y_plane_(y_plane),
151 u_plane_(u_plane),
152 v_plane_(v_plane),
153 y_stride_(y_stride),
154 u_stride_(u_stride),
155 v_stride_(v_stride),
156 no_longer_used_cb_(no_longer_used) {
157 CHECK(width >= desired_width && height >= desired_height);
158
159 // Center crop to |desired_width| x |desired_height|.
160 // Make sure offset is even so that u/v plane becomes aligned.
161 const int offset_x = ((width - desired_width) / 2) & ~1;
162 const int offset_y = ((height - desired_height) / 2) & ~1;
163 y_plane_ += y_stride_ * offset_y + offset_x;
164 u_plane_ += u_stride_ * (offset_y / 2) + (offset_x / 2);
165 v_plane_ += v_stride_ * (offset_y / 2) + (offset_x / 2);
166}
167
168WrappedI420Buffer::~WrappedI420Buffer() {
169 no_longer_used_cb_();
170}
171
172
173int WrappedI420Buffer::width() const {
174 return width_;
175}
176
177int WrappedI420Buffer::height() const {
178 return height_;
179}
180
181const uint8_t* WrappedI420Buffer::data(PlaneType type) const {
182 switch (type) {
183 case kYPlane:
184 return y_plane_;
185 case kUPlane:
186 return u_plane_;
187 case kVPlane:
188 return v_plane_;
189 default:
190 RTC_NOTREACHED();
191 return nullptr;
192 }
193}
194
195uint8_t* WrappedI420Buffer::data(PlaneType type) {
196 RTC_NOTREACHED();
197 return nullptr;
198}
199
200int WrappedI420Buffer::stride(PlaneType type) const {
201 switch (type) {
202 case kYPlane:
203 return y_stride_;
204 case kUPlane:
205 return u_stride_;
206 case kVPlane:
207 return v_stride_;
208 default:
209 RTC_NOTREACHED();
210 return 0;
211 }
212}
213
214rtc::scoped_refptr<NativeHandle> WrappedI420Buffer::native_handle() const {
215 return nullptr;
216}
217
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000218} // namespace webrtc