blob: 6f49e8aef9d0f1f387ff15cce119aca0699567ac [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
Magnus Jedvert3318f982015-08-26 16:06:21 +020030uint8_t* VideoFrameBuffer::MutableData(PlaneType type) {
31 RTC_NOTREACHED();
32 return nullptr;
33}
34
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000035VideoFrameBuffer::~VideoFrameBuffer() {}
36
37I420Buffer::I420Buffer(int width, int height)
38 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
39}
40
41I420Buffer::I420Buffer(int width,
42 int height,
43 int stride_y,
44 int stride_u,
45 int stride_v)
46 : width_(width),
47 height_(height),
48 stride_y_(stride_y),
49 stride_u_(stride_u),
50 stride_v_(stride_v),
51 data_(static_cast<uint8_t*>(AlignedMalloc(
hbos900f9752016-02-05 08:08:34 -080052 I420DataSize(height, stride_y, stride_u, stride_v),
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000053 kBufferAlignment))) {
henrikg91d6ede2015-09-17 00:24:34 -070054 RTC_DCHECK_GT(width, 0);
55 RTC_DCHECK_GT(height, 0);
56 RTC_DCHECK_GE(stride_y, width);
57 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
58 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000059}
60
61I420Buffer::~I420Buffer() {
62}
63
hbos900f9752016-02-05 08:08:34 -080064void I420Buffer::InitializeData() {
65 memset(data_.get(), 0,
66 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
67}
68
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000069int I420Buffer::width() const {
70 return width_;
71}
72
73int I420Buffer::height() const {
74 return height_;
75}
76
77const uint8_t* I420Buffer::data(PlaneType type) const {
78 switch (type) {
79 case kYPlane:
80 return data_.get();
81 case kUPlane:
82 return data_.get() + stride_y_ * height_;
83 case kVPlane:
84 return data_.get() + stride_y_ * height_ +
85 stride_u_ * ((height_ + 1) / 2);
86 default:
87 RTC_NOTREACHED();
88 return nullptr;
89 }
90}
91
Magnus Jedvert3318f982015-08-26 16:06:21 +020092uint8_t* I420Buffer::MutableData(PlaneType type) {
henrikg91d6ede2015-09-17 00:24:34 -070093 RTC_DCHECK(HasOneRef());
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000094 return const_cast<uint8_t*>(
95 static_cast<const VideoFrameBuffer*>(this)->data(type));
96}
97
98int I420Buffer::stride(PlaneType type) const {
99 switch (type) {
100 case kYPlane:
101 return stride_y_;
102 case kUPlane:
103 return stride_u_;
104 case kVPlane:
105 return stride_v_;
106 default:
107 RTC_NOTREACHED();
108 return 0;
109 }
110}
111
Per9b3f56e2015-04-09 13:44:16 +0200112void* I420Buffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000113 return nullptr;
114}
115
Peter Boströmeb66e802015-06-05 11:08:03 +0200116rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() {
117 RTC_NOTREACHED();
118 return nullptr;
119}
120
nisse7cc9cc02016-03-29 23:44:19 -0700121rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
122 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
123 int width = buffer->width();
124 int height = buffer->height();
125 rtc::scoped_refptr<I420Buffer> copy =
126 new rtc::RefCountedObject<I420Buffer>(width, height);
127 RTC_CHECK(libyuv::I420Copy(buffer->data(kYPlane), buffer->stride(kYPlane),
128 buffer->data(kUPlane), buffer->stride(kUPlane),
129 buffer->data(kVPlane), buffer->stride(kVPlane),
130 copy->MutableData(kYPlane), copy->stride(kYPlane),
131 copy->MutableData(kUPlane), copy->stride(kUPlane),
132 copy->MutableData(kVPlane), copy->stride(kVPlane),
133 width, height) == 0);
134
135 return copy;
136}
137
Peter Boströmeb66e802015-06-05 11:08:03 +0200138NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
139 int width,
140 int height)
141 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700142 RTC_DCHECK(native_handle != nullptr);
143 RTC_DCHECK_GT(width, 0);
144 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000145}
146
Peter Boströmeb66e802015-06-05 11:08:03 +0200147int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000148 return width_;
149}
150
Peter Boströmeb66e802015-06-05 11:08:03 +0200151int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000152 return height_;
153}
154
Peter Boströmeb66e802015-06-05 11:08:03 +0200155const uint8_t* NativeHandleBuffer::data(PlaneType type) const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000156 RTC_NOTREACHED(); // Should not be called.
157 return nullptr;
158}
159
Peter Boströmeb66e802015-06-05 11:08:03 +0200160int NativeHandleBuffer::stride(PlaneType type) const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000161 RTC_NOTREACHED(); // Should not be called.
162 return 0;
163}
164
Peter Boströmeb66e802015-06-05 11:08:03 +0200165void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000166 return native_handle_;
167}
168
Magnus Jedvertc464f502015-08-25 23:22:08 +0200169WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200170 int height,
171 const uint8_t* y_plane,
172 int y_stride,
173 const uint8_t* u_plane,
174 int u_stride,
175 const uint8_t* v_plane,
176 int v_stride,
177 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200178 : width_(width),
179 height_(height),
180 y_plane_(y_plane),
181 u_plane_(u_plane),
182 v_plane_(v_plane),
183 y_stride_(y_stride),
184 u_stride_(u_stride),
185 v_stride_(v_stride),
186 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200187}
188
189WrappedI420Buffer::~WrappedI420Buffer() {
190 no_longer_used_cb_();
191}
192
Per33544192015-04-02 12:30:51 +0200193int WrappedI420Buffer::width() const {
194 return width_;
195}
196
197int WrappedI420Buffer::height() const {
198 return height_;
199}
200
201const uint8_t* WrappedI420Buffer::data(PlaneType type) const {
202 switch (type) {
203 case kYPlane:
204 return y_plane_;
205 case kUPlane:
206 return u_plane_;
207 case kVPlane:
208 return v_plane_;
209 default:
210 RTC_NOTREACHED();
211 return nullptr;
212 }
213}
214
Per33544192015-04-02 12:30:51 +0200215int WrappedI420Buffer::stride(PlaneType type) const {
216 switch (type) {
217 case kYPlane:
218 return y_stride_;
219 case kUPlane:
220 return u_stride_;
221 case kVPlane:
222 return v_stride_;
223 default:
224 RTC_NOTREACHED();
225 return 0;
226 }
227}
228
Per9b3f56e2015-04-09 13:44:16 +0200229void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200230 return nullptr;
231}
232
Peter Boströmeb66e802015-06-05 11:08:03 +0200233rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
234 RTC_NOTREACHED();
235 return nullptr;
236}
237
Magnus Jedvertc464f502015-08-25 23:22:08 +0200238rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop(
239 const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
240 int cropped_width,
241 int cropped_height) {
henrikg91d6ede2015-09-17 00:24:34 -0700242 RTC_CHECK(buffer->native_handle() == nullptr);
243 RTC_CHECK_LE(cropped_width, buffer->width());
244 RTC_CHECK_LE(cropped_height, buffer->height());
Magnus Jedvertc464f502015-08-25 23:22:08 +0200245 if (buffer->width() == cropped_width && buffer->height() == cropped_height)
246 return buffer;
247
248 // Center crop to |cropped_width| x |cropped_height|.
249 // Make sure offset is even so that u/v plane becomes aligned.
250 const int uv_offset_x = (buffer->width() - cropped_width) / 4;
251 const int uv_offset_y = (buffer->height() - cropped_height) / 4;
252 const int offset_x = uv_offset_x * 2;
253 const int offset_y = uv_offset_y * 2;
254
Magnus Jedvert3318f982015-08-26 16:06:21 +0200255 const uint8_t* y_plane = buffer->data(kYPlane) +
Magnus Jedvertc464f502015-08-25 23:22:08 +0200256 buffer->stride(kYPlane) * offset_y + offset_x;
Magnus Jedvert3318f982015-08-26 16:06:21 +0200257 const uint8_t* u_plane = buffer->data(kUPlane) +
Magnus Jedvertc464f502015-08-25 23:22:08 +0200258 buffer->stride(kUPlane) * uv_offset_y + uv_offset_x;
Magnus Jedvert3318f982015-08-26 16:06:21 +0200259 const uint8_t* v_plane = buffer->data(kVPlane) +
Magnus Jedvertc464f502015-08-25 23:22:08 +0200260 buffer->stride(kVPlane) * uv_offset_y + uv_offset_x;
261 return new rtc::RefCountedObject<WrappedI420Buffer>(
262 cropped_width, cropped_height,
263 y_plane, buffer->stride(kYPlane),
264 u_plane, buffer->stride(kUPlane),
265 v_plane, buffer->stride(kVPlane),
perkj14f41442015-11-30 22:15:45 -0800266 rtc::KeepRefUntilDone(buffer));
Magnus Jedvertc464f502015-08-25 23:22:08 +0200267}
268
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000269} // namespace webrtc