blob: 34213e4656ce766f5cb6f2fdc24e566c02cf152e [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
Peter Boströmeb66e802015-06-05 11:08:03 +0200203NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
204 int width,
205 int height)
206 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700207 RTC_DCHECK(native_handle != nullptr);
208 RTC_DCHECK_GT(width, 0);
209 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000210}
211
Peter Boströmeb66e802015-06-05 11:08:03 +0200212int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000213 return width_;
214}
215
Peter Boströmeb66e802015-06-05 11:08:03 +0200216int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000217 return height_;
218}
219
nisse06176e42016-04-18 05:34:40 -0700220const uint8_t* NativeHandleBuffer::DataY() const {
221 RTC_NOTREACHED(); // Should not be called.
222 return nullptr;
223}
224const uint8_t* NativeHandleBuffer::DataU() const {
225 RTC_NOTREACHED(); // Should not be called.
226 return nullptr;
227}
228const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000229 RTC_NOTREACHED(); // Should not be called.
230 return nullptr;
231}
232
nisse06176e42016-04-18 05:34:40 -0700233int NativeHandleBuffer::StrideY() const {
234 RTC_NOTREACHED(); // Should not be called.
235 return 0;
236}
237int NativeHandleBuffer::StrideU() const {
238 RTC_NOTREACHED(); // Should not be called.
239 return 0;
240}
241int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000242 RTC_NOTREACHED(); // Should not be called.
243 return 0;
244}
245
Peter Boströmeb66e802015-06-05 11:08:03 +0200246void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000247 return native_handle_;
248}
249
Magnus Jedvertc464f502015-08-25 23:22:08 +0200250WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200251 int height,
252 const uint8_t* y_plane,
253 int y_stride,
254 const uint8_t* u_plane,
255 int u_stride,
256 const uint8_t* v_plane,
257 int v_stride,
258 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200259 : width_(width),
260 height_(height),
261 y_plane_(y_plane),
262 u_plane_(u_plane),
263 v_plane_(v_plane),
264 y_stride_(y_stride),
265 u_stride_(u_stride),
266 v_stride_(v_stride),
267 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200268}
269
270WrappedI420Buffer::~WrappedI420Buffer() {
271 no_longer_used_cb_();
272}
273
Per33544192015-04-02 12:30:51 +0200274int WrappedI420Buffer::width() const {
275 return width_;
276}
277
278int WrappedI420Buffer::height() const {
279 return height_;
280}
281
nisse06176e42016-04-18 05:34:40 -0700282const uint8_t* WrappedI420Buffer::DataY() const {
283 return y_plane_;
284}
285const uint8_t* WrappedI420Buffer::DataU() const {
286 return u_plane_;
287}
288const uint8_t* WrappedI420Buffer::DataV() const {
289 return v_plane_;
Per33544192015-04-02 12:30:51 +0200290}
291
nisse06176e42016-04-18 05:34:40 -0700292int WrappedI420Buffer::StrideY() const {
293 return y_stride_;
294}
295int WrappedI420Buffer::StrideU() const {
296 return u_stride_;
297}
298int WrappedI420Buffer::StrideV() const {
299 return v_stride_;
Per33544192015-04-02 12:30:51 +0200300}
301
Per9b3f56e2015-04-09 13:44:16 +0200302void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200303 return nullptr;
304}
305
Peter Boströmeb66e802015-06-05 11:08:03 +0200306rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
307 RTC_NOTREACHED();
308 return nullptr;
309}
310
Magnus Jedvertc464f502015-08-25 23:22:08 +0200311rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop(
312 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
313 int cropped_width,
314 int cropped_height) {
henrikg91d6ede2015-09-17 00:24:34 -0700315 RTC_CHECK(buffer->native_handle() == nullptr);
316 RTC_CHECK_LE(cropped_width, buffer->width());
317 RTC_CHECK_LE(cropped_height, buffer->height());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200318 if (buffer->width() == cropped_width && buffer->height() == cropped_height)
319 return buffer;
320
321 // Center crop to |cropped_width| x |cropped_height|.
322 // Make sure offset is even so that u/v plane becomes aligned.
323 const int uv_offset_x = (buffer->width() - cropped_width) / 4;
324 const int uv_offset_y = (buffer->height() - cropped_height) / 4;
325 const int offset_x = uv_offset_x * 2;
326 const int offset_y = uv_offset_y * 2;
327
nisse06176e42016-04-18 05:34:40 -0700328 const uint8_t* y_plane = buffer->DataY() +
329 buffer->StrideY() * offset_y + offset_x;
330 const uint8_t* u_plane = buffer->DataU() +
331 buffer->StrideU() * uv_offset_y + uv_offset_x;
332 const uint8_t* v_plane = buffer->DataV() +
333 buffer->StrideV() * uv_offset_y + uv_offset_x;
Magnus Jedvertc464f502015-08-25 23:22:08 +0200334 return new rtc::RefCountedObject<WrappedI420Buffer>(
335 cropped_width, cropped_height,
nisse06176e42016-04-18 05:34:40 -0700336 y_plane, buffer->StrideY(),
337 u_plane, buffer->StrideU(),
338 v_plane, buffer->StrideV(),
perkj14f41442015-11-30 22:15:45 -0800339 rtc::KeepRefUntilDone(buffer));
Magnus Jedvertc464f502015-08-25 23:22:08 +0200340}
341
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000342} // namespace webrtc