blob: 23643c4bfd579501cf3ed14f42e1088e4be71d65 [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
Niels Möller718a7632016-06-13 13:06:01 +020011#include <algorithm>
12
kjellander6f8ce062015-11-16 13:52:24 -080013#include "webrtc/common_video/include/video_frame_buffer.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000014
15#include "webrtc/base/checks.h"
perkj14f41442015-11-30 22:15:45 -080016#include "webrtc/base/keep_ref_until_done.h"
nisse7cc9cc02016-03-29 23:44:19 -070017#include "libyuv/convert.h"
Niels Möller718a7632016-06-13 13:06:01 +020018#include "libyuv/scale.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000019
20// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
21static const int kBufferAlignment = 64;
22
23namespace webrtc {
24
hbos900f9752016-02-05 08:08:34 -080025namespace {
26
27int I420DataSize(int height, int stride_y, int stride_u, int stride_v) {
28 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2);
29}
30
31} // namespace
32
nisse06176e42016-04-18 05:34:40 -070033uint8_t* VideoFrameBuffer::MutableDataY() {
Magnus Jedvert3318f982015-08-26 16:06:21 +020034 RTC_NOTREACHED();
35 return nullptr;
36}
nisse06176e42016-04-18 05:34:40 -070037uint8_t* VideoFrameBuffer::MutableDataU() {
38 RTC_NOTREACHED();
39 return nullptr;
40}
41uint8_t* VideoFrameBuffer::MutableDataV() {
42 RTC_NOTREACHED();
43 return nullptr;
44}
45
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000046VideoFrameBuffer::~VideoFrameBuffer() {}
47
48I420Buffer::I420Buffer(int width, int height)
49 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
50}
51
52I420Buffer::I420Buffer(int width,
53 int height,
54 int stride_y,
55 int stride_u,
56 int stride_v)
57 : width_(width),
58 height_(height),
59 stride_y_(stride_y),
60 stride_u_(stride_u),
61 stride_v_(stride_v),
62 data_(static_cast<uint8_t*>(AlignedMalloc(
hbos900f9752016-02-05 08:08:34 -080063 I420DataSize(height, stride_y, stride_u, stride_v),
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000064 kBufferAlignment))) {
henrikg91d6ede2015-09-17 00:24:34 -070065 RTC_DCHECK_GT(width, 0);
66 RTC_DCHECK_GT(height, 0);
67 RTC_DCHECK_GE(stride_y, width);
68 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
69 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000070}
71
72I420Buffer::~I420Buffer() {
73}
74
nisseac62bd42016-06-20 03:38:52 -070075rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) {
76 return new rtc::RefCountedObject<I420Buffer>(width, height);
77}
78
79rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width,
80 int height,
81 int stride_y,
82 int stride_u,
83 int stride_v) {
84 return new rtc::RefCountedObject<I420Buffer>(
85 width, height, stride_y, stride_u, stride_v);
86}
87
hbos900f9752016-02-05 08:08:34 -080088void I420Buffer::InitializeData() {
89 memset(data_.get(), 0,
90 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
91}
92
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000093int I420Buffer::width() const {
94 return width_;
95}
96
97int I420Buffer::height() const {
98 return height_;
99}
100
nisse06176e42016-04-18 05:34:40 -0700101const uint8_t* I420Buffer::DataY() const {
102 return data_.get();
103}
104const uint8_t* I420Buffer::DataU() const {
105 return data_.get() + stride_y_ * height_;
106}
107const uint8_t* I420Buffer::DataV() const {
108 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000109}
110
nisse06176e42016-04-18 05:34:40 -0700111uint8_t* I420Buffer::MutableDataY() {
nisse06176e42016-04-18 05:34:40 -0700112 return const_cast<uint8_t*>(DataY());
113}
114uint8_t* I420Buffer::MutableDataU() {
nisse06176e42016-04-18 05:34:40 -0700115 return const_cast<uint8_t*>(DataU());
116}
117uint8_t* I420Buffer::MutableDataV() {
nisse06176e42016-04-18 05:34:40 -0700118 return const_cast<uint8_t*>(DataV());
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000119}
120
nisse06176e42016-04-18 05:34:40 -0700121int I420Buffer::StrideY() const {
122 return stride_y_;
123}
124int I420Buffer::StrideU() const {
125 return stride_u_;
126}
127int I420Buffer::StrideV() const {
128 return stride_v_;
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000129}
130
Per9b3f56e2015-04-09 13:44:16 +0200131void* I420Buffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000132 return nullptr;
133}
134
Peter Boströmeb66e802015-06-05 11:08:03 +0200135rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() {
136 RTC_NOTREACHED();
137 return nullptr;
138}
139
nisse7cc9cc02016-03-29 23:44:19 -0700140rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
Niels Möller6af2e862016-06-17 09:12:44 +0200141 const rtc::scoped_refptr<VideoFrameBuffer>& source) {
142 int width = source->width();
143 int height = source->height();
nisseac62bd42016-06-20 03:38:52 -0700144 rtc::scoped_refptr<I420Buffer> target = I420Buffer::Create(width, height);
Niels Möller6af2e862016-06-17 09:12:44 +0200145 RTC_CHECK(libyuv::I420Copy(source->DataY(), source->StrideY(),
146 source->DataU(), source->StrideU(),
147 source->DataV(), source->StrideV(),
148 target->MutableDataY(), target->StrideY(),
149 target->MutableDataU(), target->StrideU(),
150 target->MutableDataV(), target->StrideV(),
nisse7cc9cc02016-03-29 23:44:19 -0700151 width, height) == 0);
152
Niels Möller6af2e862016-06-17 09:12:44 +0200153 return target;
nisse7cc9cc02016-03-29 23:44:19 -0700154}
155
nisseefec5902016-06-09 00:31:39 -0700156void I420Buffer::SetToBlack() {
157 RTC_CHECK(libyuv::I420Rect(MutableDataY(), StrideY(),
158 MutableDataU(), StrideU(),
159 MutableDataV(), StrideV(),
160 0, 0, width(), height(),
161 0, 128, 128) == 0);
162}
163
Niels Möller718a7632016-06-13 13:06:01 +0200164void I420Buffer::CropAndScaleFrom(
165 const rtc::scoped_refptr<VideoFrameBuffer>& src,
166 int offset_x,
167 int offset_y,
168 int crop_width,
169 int crop_height) {
170 RTC_CHECK_LE(crop_width, src->width());
171 RTC_CHECK_LE(crop_height, src->height());
172 RTC_CHECK_LE(crop_width + offset_x, src->width());
173 RTC_CHECK_LE(crop_height + offset_y, src->height());
174 RTC_CHECK_GE(offset_x, 0);
175 RTC_CHECK_GE(offset_y, 0);
176
177 // Make sure offset is even so that u/v plane becomes aligned.
178 const int uv_offset_x = offset_x / 2;
179 const int uv_offset_y = offset_y / 2;
180 offset_x = uv_offset_x * 2;
181 offset_y = uv_offset_y * 2;
182
183 const uint8_t* y_plane =
184 src->DataY() + src->StrideY() * offset_y + offset_x;
185 const uint8_t* u_plane =
186 src->DataU() + src->StrideU() * uv_offset_y + uv_offset_x;
187 const uint8_t* v_plane =
188 src->DataV() + src->StrideV() * uv_offset_y + uv_offset_x;
189 int res = libyuv::I420Scale(y_plane, src->StrideY(),
190 u_plane, src->StrideU(),
191 v_plane, src->StrideV(),
192 crop_width, crop_height,
193 MutableDataY(), StrideY(),
194 MutableDataU(), StrideU(),
195 MutableDataV(), StrideV(),
196 width(), height(), libyuv::kFilterBox);
197
198 RTC_DCHECK_EQ(res, 0);
199}
200
201void I420Buffer::CropAndScaleFrom(
202 const rtc::scoped_refptr<VideoFrameBuffer>& src) {
203 const int crop_width =
204 std::min(src->width(), width() * src->height() / height());
205 const int crop_height =
206 std::min(src->height(), height() * src->width() / width());
207
208 CropAndScaleFrom(
209 src,
210 (src->width() - crop_width) / 2, (src->height() - crop_height) / 2,
211 crop_width, crop_height);
212}
213
214void I420Buffer::ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) {
215 CropAndScaleFrom(src, 0, 0, src->width(), src->height());
216}
217
Niels Möller6af2e862016-06-17 09:12:44 +0200218rtc::scoped_refptr<I420Buffer> I420Buffer::CopyKeepStride(
219 const rtc::scoped_refptr<VideoFrameBuffer>& source) {
220 int width = source->width();
221 int height = source->height();
222 int stride_y = source->StrideY();
223 int stride_u = source->StrideU();
224 int stride_v = source->StrideV();
225 rtc::scoped_refptr<I420Buffer> target =
nisseac62bd42016-06-20 03:38:52 -0700226 I420Buffer::Create(width, height, stride_y, stride_u, stride_v);
Niels Möller6af2e862016-06-17 09:12:44 +0200227 RTC_CHECK(libyuv::I420Copy(source->DataY(), stride_y,
228 source->DataU(), stride_u,
229 source->DataV(), stride_v,
230 target->MutableDataY(), stride_y,
231 target->MutableDataU(), stride_u,
232 target->MutableDataV(), stride_v,
233 width, height) == 0);
234
235 return target;
236}
237
Peter Boströmeb66e802015-06-05 11:08:03 +0200238NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
239 int width,
240 int height)
241 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700242 RTC_DCHECK(native_handle != nullptr);
243 RTC_DCHECK_GT(width, 0);
244 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000245}
246
Peter Boströmeb66e802015-06-05 11:08:03 +0200247int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000248 return width_;
249}
250
Peter Boströmeb66e802015-06-05 11:08:03 +0200251int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000252 return height_;
253}
254
nisse06176e42016-04-18 05:34:40 -0700255const uint8_t* NativeHandleBuffer::DataY() const {
256 RTC_NOTREACHED(); // Should not be called.
257 return nullptr;
258}
259const uint8_t* NativeHandleBuffer::DataU() const {
260 RTC_NOTREACHED(); // Should not be called.
261 return nullptr;
262}
263const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000264 RTC_NOTREACHED(); // Should not be called.
265 return nullptr;
266}
267
nisse06176e42016-04-18 05:34:40 -0700268int NativeHandleBuffer::StrideY() const {
269 RTC_NOTREACHED(); // Should not be called.
270 return 0;
271}
272int NativeHandleBuffer::StrideU() const {
273 RTC_NOTREACHED(); // Should not be called.
274 return 0;
275}
276int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000277 RTC_NOTREACHED(); // Should not be called.
278 return 0;
279}
280
Peter Boströmeb66e802015-06-05 11:08:03 +0200281void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000282 return native_handle_;
283}
284
Magnus Jedvertc464f502015-08-25 23:22:08 +0200285WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200286 int height,
287 const uint8_t* y_plane,
288 int y_stride,
289 const uint8_t* u_plane,
290 int u_stride,
291 const uint8_t* v_plane,
292 int v_stride,
293 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200294 : width_(width),
295 height_(height),
296 y_plane_(y_plane),
297 u_plane_(u_plane),
298 v_plane_(v_plane),
299 y_stride_(y_stride),
300 u_stride_(u_stride),
301 v_stride_(v_stride),
302 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200303}
304
305WrappedI420Buffer::~WrappedI420Buffer() {
306 no_longer_used_cb_();
307}
308
Per33544192015-04-02 12:30:51 +0200309int WrappedI420Buffer::width() const {
310 return width_;
311}
312
313int WrappedI420Buffer::height() const {
314 return height_;
315}
316
nisse06176e42016-04-18 05:34:40 -0700317const uint8_t* WrappedI420Buffer::DataY() const {
318 return y_plane_;
319}
320const uint8_t* WrappedI420Buffer::DataU() const {
321 return u_plane_;
322}
323const uint8_t* WrappedI420Buffer::DataV() const {
324 return v_plane_;
Per33544192015-04-02 12:30:51 +0200325}
326
nisse06176e42016-04-18 05:34:40 -0700327int WrappedI420Buffer::StrideY() const {
328 return y_stride_;
329}
330int WrappedI420Buffer::StrideU() const {
331 return u_stride_;
332}
333int WrappedI420Buffer::StrideV() const {
334 return v_stride_;
Per33544192015-04-02 12:30:51 +0200335}
336
Per9b3f56e2015-04-09 13:44:16 +0200337void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200338 return nullptr;
339}
340
Peter Boströmeb66e802015-06-05 11:08:03 +0200341rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
342 RTC_NOTREACHED();
343 return nullptr;
344}
345
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000346} // namespace webrtc