blob: dbb2743db1265890f4c7a3a854b6a415d059ee98 [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"
fbarchardd1f83cf2016-07-08 17:33:20 -070018#include "libyuv/planar_functions.h"
Niels Möller718a7632016-06-13 13:06:01 +020019#include "libyuv/scale.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000020
21// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
22static const int kBufferAlignment = 64;
23
24namespace webrtc {
25
hbos900f9752016-02-05 08:08:34 -080026namespace {
27
28int I420DataSize(int height, int stride_y, int stride_u, int stride_v) {
29 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2);
30}
31
32} // namespace
33
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000034VideoFrameBuffer::~VideoFrameBuffer() {}
35
36I420Buffer::I420Buffer(int width, int height)
37 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
38}
39
40I420Buffer::I420Buffer(int width,
41 int height,
42 int stride_y,
43 int stride_u,
44 int stride_v)
45 : width_(width),
46 height_(height),
47 stride_y_(stride_y),
48 stride_u_(stride_u),
49 stride_v_(stride_v),
50 data_(static_cast<uint8_t*>(AlignedMalloc(
hbos900f9752016-02-05 08:08:34 -080051 I420DataSize(height, stride_y, stride_u, stride_v),
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000052 kBufferAlignment))) {
henrikg91d6ede2015-09-17 00:24:34 -070053 RTC_DCHECK_GT(width, 0);
54 RTC_DCHECK_GT(height, 0);
55 RTC_DCHECK_GE(stride_y, width);
56 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
57 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000058}
59
60I420Buffer::~I420Buffer() {
61}
62
nisseac62bd42016-06-20 03:38:52 -070063rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) {
64 return new rtc::RefCountedObject<I420Buffer>(width, height);
65}
66
67rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width,
68 int height,
69 int stride_y,
70 int stride_u,
71 int stride_v) {
72 return new rtc::RefCountedObject<I420Buffer>(
73 width, height, stride_y, stride_u, stride_v);
74}
75
hbos900f9752016-02-05 08:08:34 -080076void I420Buffer::InitializeData() {
77 memset(data_.get(), 0,
78 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
79}
80
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000081int I420Buffer::width() const {
82 return width_;
83}
84
85int I420Buffer::height() const {
86 return height_;
87}
88
nisse06176e42016-04-18 05:34:40 -070089const uint8_t* I420Buffer::DataY() const {
90 return data_.get();
91}
92const uint8_t* I420Buffer::DataU() const {
93 return data_.get() + stride_y_ * height_;
94}
95const uint8_t* I420Buffer::DataV() const {
96 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000097}
98
nisse06176e42016-04-18 05:34:40 -070099uint8_t* I420Buffer::MutableDataY() {
nisse06176e42016-04-18 05:34:40 -0700100 return const_cast<uint8_t*>(DataY());
101}
102uint8_t* I420Buffer::MutableDataU() {
nisse06176e42016-04-18 05:34:40 -0700103 return const_cast<uint8_t*>(DataU());
104}
105uint8_t* I420Buffer::MutableDataV() {
nisse06176e42016-04-18 05:34:40 -0700106 return const_cast<uint8_t*>(DataV());
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000107}
108
nisse06176e42016-04-18 05:34:40 -0700109int I420Buffer::StrideY() const {
110 return stride_y_;
111}
112int I420Buffer::StrideU() const {
113 return stride_u_;
114}
115int I420Buffer::StrideV() const {
116 return stride_v_;
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000117}
118
Per9b3f56e2015-04-09 13:44:16 +0200119void* I420Buffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000120 return nullptr;
121}
122
Peter Boströmeb66e802015-06-05 11:08:03 +0200123rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() {
124 RTC_NOTREACHED();
125 return nullptr;
126}
127
nisse7cc9cc02016-03-29 23:44:19 -0700128rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
Niels Möller6af2e862016-06-17 09:12:44 +0200129 const rtc::scoped_refptr<VideoFrameBuffer>& source) {
130 int width = source->width();
131 int height = source->height();
nisseac62bd42016-06-20 03:38:52 -0700132 rtc::scoped_refptr<I420Buffer> target = I420Buffer::Create(width, height);
Niels Möller6af2e862016-06-17 09:12:44 +0200133 RTC_CHECK(libyuv::I420Copy(source->DataY(), source->StrideY(),
134 source->DataU(), source->StrideU(),
135 source->DataV(), source->StrideV(),
136 target->MutableDataY(), target->StrideY(),
137 target->MutableDataU(), target->StrideU(),
138 target->MutableDataV(), target->StrideV(),
nisse7cc9cc02016-03-29 23:44:19 -0700139 width, height) == 0);
140
Niels Möller6af2e862016-06-17 09:12:44 +0200141 return target;
nisse7cc9cc02016-03-29 23:44:19 -0700142}
143
nisseefec5902016-06-09 00:31:39 -0700144void I420Buffer::SetToBlack() {
145 RTC_CHECK(libyuv::I420Rect(MutableDataY(), StrideY(),
146 MutableDataU(), StrideU(),
147 MutableDataV(), StrideV(),
148 0, 0, width(), height(),
149 0, 128, 128) == 0);
150}
151
Niels Möller718a7632016-06-13 13:06:01 +0200152void I420Buffer::CropAndScaleFrom(
153 const rtc::scoped_refptr<VideoFrameBuffer>& src,
154 int offset_x,
155 int offset_y,
156 int crop_width,
157 int crop_height) {
158 RTC_CHECK_LE(crop_width, src->width());
159 RTC_CHECK_LE(crop_height, src->height());
160 RTC_CHECK_LE(crop_width + offset_x, src->width());
161 RTC_CHECK_LE(crop_height + offset_y, src->height());
162 RTC_CHECK_GE(offset_x, 0);
163 RTC_CHECK_GE(offset_y, 0);
164
165 // Make sure offset is even so that u/v plane becomes aligned.
166 const int uv_offset_x = offset_x / 2;
167 const int uv_offset_y = offset_y / 2;
168 offset_x = uv_offset_x * 2;
169 offset_y = uv_offset_y * 2;
170
171 const uint8_t* y_plane =
172 src->DataY() + src->StrideY() * offset_y + offset_x;
173 const uint8_t* u_plane =
174 src->DataU() + src->StrideU() * uv_offset_y + uv_offset_x;
175 const uint8_t* v_plane =
176 src->DataV() + src->StrideV() * uv_offset_y + uv_offset_x;
177 int res = libyuv::I420Scale(y_plane, src->StrideY(),
178 u_plane, src->StrideU(),
179 v_plane, src->StrideV(),
180 crop_width, crop_height,
181 MutableDataY(), StrideY(),
182 MutableDataU(), StrideU(),
183 MutableDataV(), StrideV(),
184 width(), height(), libyuv::kFilterBox);
185
186 RTC_DCHECK_EQ(res, 0);
187}
188
189void I420Buffer::CropAndScaleFrom(
190 const rtc::scoped_refptr<VideoFrameBuffer>& src) {
191 const int crop_width =
192 std::min(src->width(), width() * src->height() / height());
193 const int crop_height =
194 std::min(src->height(), height() * src->width() / width());
195
196 CropAndScaleFrom(
197 src,
198 (src->width() - crop_width) / 2, (src->height() - crop_height) / 2,
199 crop_width, crop_height);
200}
201
202void I420Buffer::ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) {
203 CropAndScaleFrom(src, 0, 0, src->width(), src->height());
204}
205
nissed5074722016-08-30 08:45:44 -0700206// static
Niels Möller6af2e862016-06-17 09:12:44 +0200207rtc::scoped_refptr<I420Buffer> I420Buffer::CopyKeepStride(
208 const rtc::scoped_refptr<VideoFrameBuffer>& source) {
209 int width = source->width();
210 int height = source->height();
211 int stride_y = source->StrideY();
212 int stride_u = source->StrideU();
213 int stride_v = source->StrideV();
214 rtc::scoped_refptr<I420Buffer> target =
nisseac62bd42016-06-20 03:38:52 -0700215 I420Buffer::Create(width, height, stride_y, stride_u, stride_v);
Niels Möller6af2e862016-06-17 09:12:44 +0200216 RTC_CHECK(libyuv::I420Copy(source->DataY(), stride_y,
217 source->DataU(), stride_u,
218 source->DataV(), stride_v,
219 target->MutableDataY(), stride_y,
220 target->MutableDataU(), stride_u,
221 target->MutableDataV(), stride_v,
222 width, height) == 0);
223
224 return target;
225}
226
nissed5074722016-08-30 08:45:44 -0700227// static
228rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::Rotate(
229 const rtc::scoped_refptr<VideoFrameBuffer>& src,
230 VideoRotation rotation) {
231 RTC_DCHECK(src->DataY());
232 RTC_DCHECK(src->DataU());
233 RTC_DCHECK(src->DataV());
234
235 if (rotation == webrtc::kVideoRotation_0) {
236 return src;
237 }
238
239 int rotated_width = src->width();
240 int rotated_height = src->height();
241 if (rotation == webrtc::kVideoRotation_90 ||
242 rotation == webrtc::kVideoRotation_270) {
243 std::swap(rotated_width, rotated_height);
244 }
245
246 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
247 I420Buffer::Create(rotated_width, rotated_height);
248
249 int res = libyuv::I420Rotate(
250 src->DataY(), src->StrideY(),
251 src->DataU(), src->StrideU(),
252 src->DataV(), src->StrideV(),
253 buffer->MutableDataY(), buffer->StrideY(), buffer->MutableDataU(),
254 buffer->StrideU(), buffer->MutableDataV(), buffer->StrideV(),
255 src->width(), src->height(),
256 static_cast<libyuv::RotationMode>(rotation));
257 RTC_DCHECK_EQ(res, 0);
258
259 return buffer;
260}
261
Peter Boströmeb66e802015-06-05 11:08:03 +0200262NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
263 int width,
264 int height)
265 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700266 RTC_DCHECK(native_handle != nullptr);
267 RTC_DCHECK_GT(width, 0);
268 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000269}
270
Peter Boströmeb66e802015-06-05 11:08:03 +0200271int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000272 return width_;
273}
274
Peter Boströmeb66e802015-06-05 11:08:03 +0200275int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000276 return height_;
277}
278
nisse06176e42016-04-18 05:34:40 -0700279const uint8_t* NativeHandleBuffer::DataY() const {
280 RTC_NOTREACHED(); // Should not be called.
281 return nullptr;
282}
283const uint8_t* NativeHandleBuffer::DataU() const {
284 RTC_NOTREACHED(); // Should not be called.
285 return nullptr;
286}
287const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000288 RTC_NOTREACHED(); // Should not be called.
289 return nullptr;
290}
291
nisse06176e42016-04-18 05:34:40 -0700292int NativeHandleBuffer::StrideY() const {
293 RTC_NOTREACHED(); // Should not be called.
294 return 0;
295}
296int NativeHandleBuffer::StrideU() const {
297 RTC_NOTREACHED(); // Should not be called.
298 return 0;
299}
300int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000301 RTC_NOTREACHED(); // Should not be called.
302 return 0;
303}
304
Peter Boströmeb66e802015-06-05 11:08:03 +0200305void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000306 return native_handle_;
307}
308
Magnus Jedvertc464f502015-08-25 23:22:08 +0200309WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200310 int height,
311 const uint8_t* y_plane,
312 int y_stride,
313 const uint8_t* u_plane,
314 int u_stride,
315 const uint8_t* v_plane,
316 int v_stride,
317 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200318 : width_(width),
319 height_(height),
320 y_plane_(y_plane),
321 u_plane_(u_plane),
322 v_plane_(v_plane),
323 y_stride_(y_stride),
324 u_stride_(u_stride),
325 v_stride_(v_stride),
326 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200327}
328
329WrappedI420Buffer::~WrappedI420Buffer() {
330 no_longer_used_cb_();
331}
332
Per33544192015-04-02 12:30:51 +0200333int WrappedI420Buffer::width() const {
334 return width_;
335}
336
337int WrappedI420Buffer::height() const {
338 return height_;
339}
340
nisse06176e42016-04-18 05:34:40 -0700341const uint8_t* WrappedI420Buffer::DataY() const {
342 return y_plane_;
343}
344const uint8_t* WrappedI420Buffer::DataU() const {
345 return u_plane_;
346}
347const uint8_t* WrappedI420Buffer::DataV() const {
348 return v_plane_;
Per33544192015-04-02 12:30:51 +0200349}
350
nisse06176e42016-04-18 05:34:40 -0700351int WrappedI420Buffer::StrideY() const {
352 return y_stride_;
353}
354int WrappedI420Buffer::StrideU() const {
355 return u_stride_;
356}
357int WrappedI420Buffer::StrideV() const {
358 return v_stride_;
Per33544192015-04-02 12:30:51 +0200359}
360
Per9b3f56e2015-04-09 13:44:16 +0200361void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200362 return nullptr;
363}
364
Peter Boströmeb66e802015-06-05 11:08:03 +0200365rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
366 RTC_NOTREACHED();
367 return nullptr;
368}
369
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000370} // namespace webrtc