blob: 27aea3efc595f5a3df62114880f9a4f2432f8afd [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
kjellander6f8ce062015-11-16 13:52:24 -080011#include "webrtc/common_video/include/video_frame_buffer.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000012
13#include "webrtc/base/checks.h"
perkj14f41442015-11-30 22:15:45 -080014#include "webrtc/base/keep_ref_until_done.h"
nisse7cc9cc02016-03-29 23:44:19 -070015#include "libyuv/convert.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000016
17// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
18static const int kBufferAlignment = 64;
19
20namespace webrtc {
21
hbos900f9752016-02-05 08:08:34 -080022namespace {
23
24int I420DataSize(int height, int stride_y, int stride_u, int stride_v) {
25 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2);
26}
27
28} // namespace
29
nisse06176e42016-04-18 05:34:40 -070030const uint8_t* VideoFrameBuffer::data(PlaneType type) const {
31 switch (type) {
32 case kYPlane:
33 return DataY();
34 case kUPlane:
35 return DataU();
36 case kVPlane:
37 return DataV();
38 default:
39 RTC_NOTREACHED();
40 return nullptr;
41 }
42}
43
44const uint8_t* VideoFrameBuffer::DataY() const {
45 return data(kYPlane);
46}
47const uint8_t* VideoFrameBuffer::DataU() const {
48 return data(kUPlane);
49}
50const uint8_t* VideoFrameBuffer::DataV() const {
51 return data(kVPlane);
52}
53
54int VideoFrameBuffer::stride(PlaneType type) const {
55 switch (type) {
56 case kYPlane:
57 return StrideY();
58 case kUPlane:
59 return StrideU();
60 case kVPlane:
61 return StrideV();
62 default:
63 RTC_NOTREACHED();
64 return 0;
65 }
66}
67
68int VideoFrameBuffer::StrideY() const {
69 return stride(kYPlane);
70}
71int VideoFrameBuffer::StrideU() const {
72 return stride(kUPlane);
73}
74int VideoFrameBuffer::StrideV() const {
75 return stride(kVPlane);
76}
77
78uint8_t* VideoFrameBuffer::MutableDataY() {
Magnus Jedvert3318f982015-08-26 16:06:21 +020079 RTC_NOTREACHED();
80 return nullptr;
81}
nisse06176e42016-04-18 05:34:40 -070082uint8_t* VideoFrameBuffer::MutableDataU() {
83 RTC_NOTREACHED();
84 return nullptr;
85}
86uint8_t* VideoFrameBuffer::MutableDataV() {
87 RTC_NOTREACHED();
88 return nullptr;
89}
90
91uint8_t* VideoFrameBuffer::MutableData(PlaneType type) {
92 switch (type) {
93 case kYPlane:
94 return MutableDataY();
95 case kUPlane:
96 return MutableDataU();
97 case kVPlane:
98 return MutableDataV();
99 default:
100 RTC_NOTREACHED();
101 return nullptr;
102 }
103}
Magnus Jedvert3318f982015-08-26 16:06:21 +0200104
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000105VideoFrameBuffer::~VideoFrameBuffer() {}
106
107I420Buffer::I420Buffer(int width, int height)
108 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
109}
110
111I420Buffer::I420Buffer(int width,
112 int height,
113 int stride_y,
114 int stride_u,
115 int stride_v)
116 : width_(width),
117 height_(height),
118 stride_y_(stride_y),
119 stride_u_(stride_u),
120 stride_v_(stride_v),
121 data_(static_cast<uint8_t*>(AlignedMalloc(
hbos900f9752016-02-05 08:08:34 -0800122 I420DataSize(height, stride_y, stride_u, stride_v),
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000123 kBufferAlignment))) {
henrikg91d6ede2015-09-17 00:24:34 -0700124 RTC_DCHECK_GT(width, 0);
125 RTC_DCHECK_GT(height, 0);
126 RTC_DCHECK_GE(stride_y, width);
127 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
128 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000129}
130
131I420Buffer::~I420Buffer() {
132}
133
hbos900f9752016-02-05 08:08:34 -0800134void I420Buffer::InitializeData() {
135 memset(data_.get(), 0,
136 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
137}
138
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000139int I420Buffer::width() const {
140 return width_;
141}
142
143int I420Buffer::height() const {
144 return height_;
145}
146
nisse06176e42016-04-18 05:34:40 -0700147const uint8_t* I420Buffer::DataY() const {
148 return data_.get();
149}
150const uint8_t* I420Buffer::DataU() const {
151 return data_.get() + stride_y_ * height_;
152}
153const uint8_t* I420Buffer::DataV() const {
154 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000155}
156
nisse06176e42016-04-18 05:34:40 -0700157uint8_t* I420Buffer::MutableDataY() {
nisse06176e42016-04-18 05:34:40 -0700158 return const_cast<uint8_t*>(DataY());
159}
160uint8_t* I420Buffer::MutableDataU() {
nisse06176e42016-04-18 05:34:40 -0700161 return const_cast<uint8_t*>(DataU());
162}
163uint8_t* I420Buffer::MutableDataV() {
nisse06176e42016-04-18 05:34:40 -0700164 return const_cast<uint8_t*>(DataV());
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000165}
166
nisse06176e42016-04-18 05:34:40 -0700167int I420Buffer::StrideY() const {
168 return stride_y_;
169}
170int I420Buffer::StrideU() const {
171 return stride_u_;
172}
173int I420Buffer::StrideV() const {
174 return stride_v_;
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000175}
176
Per9b3f56e2015-04-09 13:44:16 +0200177void* I420Buffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000178 return nullptr;
179}
180
Peter Boströmeb66e802015-06-05 11:08:03 +0200181rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() {
182 RTC_NOTREACHED();
183 return nullptr;
184}
185
nisse7cc9cc02016-03-29 23:44:19 -0700186rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
187 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
188 int width = buffer->width();
189 int height = buffer->height();
190 rtc::scoped_refptr<I420Buffer> copy =
191 new rtc::RefCountedObject<I420Buffer>(width, height);
nisse06176e42016-04-18 05:34:40 -0700192 RTC_CHECK(libyuv::I420Copy(buffer->DataY(), buffer->StrideY(),
193 buffer->DataU(), buffer->StrideU(),
194 buffer->DataV(), buffer->StrideV(),
195 copy->MutableDataY(), copy->StrideY(),
196 copy->MutableDataU(), copy->StrideU(),
197 copy->MutableDataV(), copy->StrideV(),
nisse7cc9cc02016-03-29 23:44:19 -0700198 width, height) == 0);
199
200 return copy;
201}
202
nisseefec5902016-06-09 00:31:39 -0700203void I420Buffer::SetToBlack() {
204 RTC_CHECK(libyuv::I420Rect(MutableDataY(), StrideY(),
205 MutableDataU(), StrideU(),
206 MutableDataV(), StrideV(),
207 0, 0, width(), height(),
208 0, 128, 128) == 0);
209}
210
Peter Boströmeb66e802015-06-05 11:08:03 +0200211NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
212 int width,
213 int height)
214 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700215 RTC_DCHECK(native_handle != nullptr);
216 RTC_DCHECK_GT(width, 0);
217 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000218}
219
Peter Boströmeb66e802015-06-05 11:08:03 +0200220int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000221 return width_;
222}
223
Peter Boströmeb66e802015-06-05 11:08:03 +0200224int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000225 return height_;
226}
227
nisse06176e42016-04-18 05:34:40 -0700228const uint8_t* NativeHandleBuffer::DataY() const {
229 RTC_NOTREACHED(); // Should not be called.
230 return nullptr;
231}
232const uint8_t* NativeHandleBuffer::DataU() const {
233 RTC_NOTREACHED(); // Should not be called.
234 return nullptr;
235}
236const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000237 RTC_NOTREACHED(); // Should not be called.
238 return nullptr;
239}
240
nisse06176e42016-04-18 05:34:40 -0700241int NativeHandleBuffer::StrideY() const {
242 RTC_NOTREACHED(); // Should not be called.
243 return 0;
244}
245int NativeHandleBuffer::StrideU() const {
246 RTC_NOTREACHED(); // Should not be called.
247 return 0;
248}
249int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000250 RTC_NOTREACHED(); // Should not be called.
251 return 0;
252}
253
Peter Boströmeb66e802015-06-05 11:08:03 +0200254void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000255 return native_handle_;
256}
257
Magnus Jedvertc464f502015-08-25 23:22:08 +0200258WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200259 int height,
260 const uint8_t* y_plane,
261 int y_stride,
262 const uint8_t* u_plane,
263 int u_stride,
264 const uint8_t* v_plane,
265 int v_stride,
266 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200267 : width_(width),
268 height_(height),
269 y_plane_(y_plane),
270 u_plane_(u_plane),
271 v_plane_(v_plane),
272 y_stride_(y_stride),
273 u_stride_(u_stride),
274 v_stride_(v_stride),
275 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200276}
277
278WrappedI420Buffer::~WrappedI420Buffer() {
279 no_longer_used_cb_();
280}
281
Per33544192015-04-02 12:30:51 +0200282int WrappedI420Buffer::width() const {
283 return width_;
284}
285
286int WrappedI420Buffer::height() const {
287 return height_;
288}
289
nisse06176e42016-04-18 05:34:40 -0700290const uint8_t* WrappedI420Buffer::DataY() const {
291 return y_plane_;
292}
293const uint8_t* WrappedI420Buffer::DataU() const {
294 return u_plane_;
295}
296const uint8_t* WrappedI420Buffer::DataV() const {
297 return v_plane_;
Per33544192015-04-02 12:30:51 +0200298}
299
nisse06176e42016-04-18 05:34:40 -0700300int WrappedI420Buffer::StrideY() const {
301 return y_stride_;
302}
303int WrappedI420Buffer::StrideU() const {
304 return u_stride_;
305}
306int WrappedI420Buffer::StrideV() const {
307 return v_stride_;
Per33544192015-04-02 12:30:51 +0200308}
309
Per9b3f56e2015-04-09 13:44:16 +0200310void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200311 return nullptr;
312}
313
Peter Boströmeb66e802015-06-05 11:08:03 +0200314rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
315 RTC_NOTREACHED();
316 return nullptr;
317}
318
Magnus Jedvertc464f502015-08-25 23:22:08 +0200319rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop(
320 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
321 int cropped_width,
322 int cropped_height) {
henrikg91d6ede2015-09-17 00:24:34 -0700323 RTC_CHECK(buffer->native_handle() == nullptr);
324 RTC_CHECK_LE(cropped_width, buffer->width());
325 RTC_CHECK_LE(cropped_height, buffer->height());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200326 if (buffer->width() == cropped_width && buffer->height() == cropped_height)
327 return buffer;
328
329 // Center crop to |cropped_width| x |cropped_height|.
330 // Make sure offset is even so that u/v plane becomes aligned.
331 const int uv_offset_x = (buffer->width() - cropped_width) / 4;
332 const int uv_offset_y = (buffer->height() - cropped_height) / 4;
333 const int offset_x = uv_offset_x * 2;
334 const int offset_y = uv_offset_y * 2;
335
nisse06176e42016-04-18 05:34:40 -0700336 const uint8_t* y_plane = buffer->DataY() +
337 buffer->StrideY() * offset_y + offset_x;
338 const uint8_t* u_plane = buffer->DataU() +
339 buffer->StrideU() * uv_offset_y + uv_offset_x;
340 const uint8_t* v_plane = buffer->DataV() +
341 buffer->StrideV() * uv_offset_y + uv_offset_x;
Magnus Jedvertc464f502015-08-25 23:22:08 +0200342 return new rtc::RefCountedObject<WrappedI420Buffer>(
343 cropped_width, cropped_height,
nisse06176e42016-04-18 05:34:40 -0700344 y_plane, buffer->StrideY(),
345 u_plane, buffer->StrideU(),
346 v_plane, buffer->StrideV(),
perkj14f41442015-11-30 22:15:45 -0800347 rtc::KeepRefUntilDone(buffer));
Magnus Jedvertc464f502015-08-25 23:22:08 +0200348}
349
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000350} // namespace webrtc