blob: 2fb35401314a317de60923480251b6dc8e6d9ccb [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 -070033const uint8_t* VideoFrameBuffer::data(PlaneType type) const {
34 switch (type) {
35 case kYPlane:
36 return DataY();
37 case kUPlane:
38 return DataU();
39 case kVPlane:
40 return DataV();
41 default:
42 RTC_NOTREACHED();
43 return nullptr;
44 }
45}
46
47const uint8_t* VideoFrameBuffer::DataY() const {
48 return data(kYPlane);
49}
50const uint8_t* VideoFrameBuffer::DataU() const {
51 return data(kUPlane);
52}
53const uint8_t* VideoFrameBuffer::DataV() const {
54 return data(kVPlane);
55}
56
57int VideoFrameBuffer::stride(PlaneType type) const {
58 switch (type) {
59 case kYPlane:
60 return StrideY();
61 case kUPlane:
62 return StrideU();
63 case kVPlane:
64 return StrideV();
65 default:
66 RTC_NOTREACHED();
67 return 0;
68 }
69}
70
71int VideoFrameBuffer::StrideY() const {
72 return stride(kYPlane);
73}
74int VideoFrameBuffer::StrideU() const {
75 return stride(kUPlane);
76}
77int VideoFrameBuffer::StrideV() const {
78 return stride(kVPlane);
79}
80
81uint8_t* VideoFrameBuffer::MutableDataY() {
Magnus Jedvert3318f982015-08-26 16:06:21 +020082 RTC_NOTREACHED();
83 return nullptr;
84}
nisse06176e42016-04-18 05:34:40 -070085uint8_t* VideoFrameBuffer::MutableDataU() {
86 RTC_NOTREACHED();
87 return nullptr;
88}
89uint8_t* VideoFrameBuffer::MutableDataV() {
90 RTC_NOTREACHED();
91 return nullptr;
92}
93
94uint8_t* VideoFrameBuffer::MutableData(PlaneType type) {
95 switch (type) {
96 case kYPlane:
97 return MutableDataY();
98 case kUPlane:
99 return MutableDataU();
100 case kVPlane:
101 return MutableDataV();
102 default:
103 RTC_NOTREACHED();
104 return nullptr;
105 }
106}
Magnus Jedvert3318f982015-08-26 16:06:21 +0200107
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000108VideoFrameBuffer::~VideoFrameBuffer() {}
109
110I420Buffer::I420Buffer(int width, int height)
111 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
112}
113
114I420Buffer::I420Buffer(int width,
115 int height,
116 int stride_y,
117 int stride_u,
118 int stride_v)
119 : width_(width),
120 height_(height),
121 stride_y_(stride_y),
122 stride_u_(stride_u),
123 stride_v_(stride_v),
124 data_(static_cast<uint8_t*>(AlignedMalloc(
hbos900f9752016-02-05 08:08:34 -0800125 I420DataSize(height, stride_y, stride_u, stride_v),
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000126 kBufferAlignment))) {
henrikg91d6ede2015-09-17 00:24:34 -0700127 RTC_DCHECK_GT(width, 0);
128 RTC_DCHECK_GT(height, 0);
129 RTC_DCHECK_GE(stride_y, width);
130 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
131 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000132}
133
134I420Buffer::~I420Buffer() {
135}
136
hbos900f9752016-02-05 08:08:34 -0800137void I420Buffer::InitializeData() {
138 memset(data_.get(), 0,
139 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
140}
141
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000142int I420Buffer::width() const {
143 return width_;
144}
145
146int I420Buffer::height() const {
147 return height_;
148}
149
nisse06176e42016-04-18 05:34:40 -0700150const uint8_t* I420Buffer::DataY() const {
151 return data_.get();
152}
153const uint8_t* I420Buffer::DataU() const {
154 return data_.get() + stride_y_ * height_;
155}
156const uint8_t* I420Buffer::DataV() const {
157 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000158}
159
nisse06176e42016-04-18 05:34:40 -0700160uint8_t* I420Buffer::MutableDataY() {
nisse06176e42016-04-18 05:34:40 -0700161 return const_cast<uint8_t*>(DataY());
162}
163uint8_t* I420Buffer::MutableDataU() {
nisse06176e42016-04-18 05:34:40 -0700164 return const_cast<uint8_t*>(DataU());
165}
166uint8_t* I420Buffer::MutableDataV() {
nisse06176e42016-04-18 05:34:40 -0700167 return const_cast<uint8_t*>(DataV());
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000168}
169
nisse06176e42016-04-18 05:34:40 -0700170int I420Buffer::StrideY() const {
171 return stride_y_;
172}
173int I420Buffer::StrideU() const {
174 return stride_u_;
175}
176int I420Buffer::StrideV() const {
177 return stride_v_;
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000178}
179
Per9b3f56e2015-04-09 13:44:16 +0200180void* I420Buffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000181 return nullptr;
182}
183
Peter Boströmeb66e802015-06-05 11:08:03 +0200184rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() {
185 RTC_NOTREACHED();
186 return nullptr;
187}
188
nisse7cc9cc02016-03-29 23:44:19 -0700189rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
190 const rtc::scoped_refptr<VideoFrameBuffer>& buffer) {
191 int width = buffer->width();
192 int height = buffer->height();
193 rtc::scoped_refptr<I420Buffer> copy =
194 new rtc::RefCountedObject<I420Buffer>(width, height);
nisse06176e42016-04-18 05:34:40 -0700195 RTC_CHECK(libyuv::I420Copy(buffer->DataY(), buffer->StrideY(),
196 buffer->DataU(), buffer->StrideU(),
197 buffer->DataV(), buffer->StrideV(),
198 copy->MutableDataY(), copy->StrideY(),
199 copy->MutableDataU(), copy->StrideU(),
200 copy->MutableDataV(), copy->StrideV(),
nisse7cc9cc02016-03-29 23:44:19 -0700201 width, height) == 0);
202
203 return copy;
204}
205
nisseefec5902016-06-09 00:31:39 -0700206void I420Buffer::SetToBlack() {
207 RTC_CHECK(libyuv::I420Rect(MutableDataY(), StrideY(),
208 MutableDataU(), StrideU(),
209 MutableDataV(), StrideV(),
210 0, 0, width(), height(),
211 0, 128, 128) == 0);
212}
213
Niels Möller718a7632016-06-13 13:06:01 +0200214void I420Buffer::CropAndScaleFrom(
215 const rtc::scoped_refptr<VideoFrameBuffer>& src,
216 int offset_x,
217 int offset_y,
218 int crop_width,
219 int crop_height) {
220 RTC_CHECK_LE(crop_width, src->width());
221 RTC_CHECK_LE(crop_height, src->height());
222 RTC_CHECK_LE(crop_width + offset_x, src->width());
223 RTC_CHECK_LE(crop_height + offset_y, src->height());
224 RTC_CHECK_GE(offset_x, 0);
225 RTC_CHECK_GE(offset_y, 0);
226
227 // Make sure offset is even so that u/v plane becomes aligned.
228 const int uv_offset_x = offset_x / 2;
229 const int uv_offset_y = offset_y / 2;
230 offset_x = uv_offset_x * 2;
231 offset_y = uv_offset_y * 2;
232
233 const uint8_t* y_plane =
234 src->DataY() + src->StrideY() * offset_y + offset_x;
235 const uint8_t* u_plane =
236 src->DataU() + src->StrideU() * uv_offset_y + uv_offset_x;
237 const uint8_t* v_plane =
238 src->DataV() + src->StrideV() * uv_offset_y + uv_offset_x;
239 int res = libyuv::I420Scale(y_plane, src->StrideY(),
240 u_plane, src->StrideU(),
241 v_plane, src->StrideV(),
242 crop_width, crop_height,
243 MutableDataY(), StrideY(),
244 MutableDataU(), StrideU(),
245 MutableDataV(), StrideV(),
246 width(), height(), libyuv::kFilterBox);
247
248 RTC_DCHECK_EQ(res, 0);
249}
250
251void I420Buffer::CropAndScaleFrom(
252 const rtc::scoped_refptr<VideoFrameBuffer>& src) {
253 const int crop_width =
254 std::min(src->width(), width() * src->height() / height());
255 const int crop_height =
256 std::min(src->height(), height() * src->width() / width());
257
258 CropAndScaleFrom(
259 src,
260 (src->width() - crop_width) / 2, (src->height() - crop_height) / 2,
261 crop_width, crop_height);
262}
263
264void I420Buffer::ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) {
265 CropAndScaleFrom(src, 0, 0, src->width(), src->height());
266}
267
Peter Boströmeb66e802015-06-05 11:08:03 +0200268NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
269 int width,
270 int height)
271 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700272 RTC_DCHECK(native_handle != nullptr);
273 RTC_DCHECK_GT(width, 0);
274 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000275}
276
Peter Boströmeb66e802015-06-05 11:08:03 +0200277int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000278 return width_;
279}
280
Peter Boströmeb66e802015-06-05 11:08:03 +0200281int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000282 return height_;
283}
284
nisse06176e42016-04-18 05:34:40 -0700285const uint8_t* NativeHandleBuffer::DataY() const {
286 RTC_NOTREACHED(); // Should not be called.
287 return nullptr;
288}
289const uint8_t* NativeHandleBuffer::DataU() const {
290 RTC_NOTREACHED(); // Should not be called.
291 return nullptr;
292}
293const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000294 RTC_NOTREACHED(); // Should not be called.
295 return nullptr;
296}
297
nisse06176e42016-04-18 05:34:40 -0700298int NativeHandleBuffer::StrideY() const {
299 RTC_NOTREACHED(); // Should not be called.
300 return 0;
301}
302int NativeHandleBuffer::StrideU() const {
303 RTC_NOTREACHED(); // Should not be called.
304 return 0;
305}
306int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000307 RTC_NOTREACHED(); // Should not be called.
308 return 0;
309}
310
Peter Boströmeb66e802015-06-05 11:08:03 +0200311void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000312 return native_handle_;
313}
314
Magnus Jedvertc464f502015-08-25 23:22:08 +0200315WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200316 int height,
317 const uint8_t* y_plane,
318 int y_stride,
319 const uint8_t* u_plane,
320 int u_stride,
321 const uint8_t* v_plane,
322 int v_stride,
323 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200324 : width_(width),
325 height_(height),
326 y_plane_(y_plane),
327 u_plane_(u_plane),
328 v_plane_(v_plane),
329 y_stride_(y_stride),
330 u_stride_(u_stride),
331 v_stride_(v_stride),
332 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200333}
334
335WrappedI420Buffer::~WrappedI420Buffer() {
336 no_longer_used_cb_();
337}
338
Per33544192015-04-02 12:30:51 +0200339int WrappedI420Buffer::width() const {
340 return width_;
341}
342
343int WrappedI420Buffer::height() const {
344 return height_;
345}
346
nisse06176e42016-04-18 05:34:40 -0700347const uint8_t* WrappedI420Buffer::DataY() const {
348 return y_plane_;
349}
350const uint8_t* WrappedI420Buffer::DataU() const {
351 return u_plane_;
352}
353const uint8_t* WrappedI420Buffer::DataV() const {
354 return v_plane_;
Per33544192015-04-02 12:30:51 +0200355}
356
nisse06176e42016-04-18 05:34:40 -0700357int WrappedI420Buffer::StrideY() const {
358 return y_stride_;
359}
360int WrappedI420Buffer::StrideU() const {
361 return u_stride_;
362}
363int WrappedI420Buffer::StrideV() const {
364 return v_stride_;
Per33544192015-04-02 12:30:51 +0200365}
366
Per9b3f56e2015-04-09 13:44:16 +0200367void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200368 return nullptr;
369}
370
Peter Boströmeb66e802015-06-05 11:08:03 +0200371rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
372 RTC_NOTREACHED();
373 return nullptr;
374}
375
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000376} // namespace webrtc