blob: 14e19dc0e33f4bd90b07345ec387dd9b29495f40 [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(
Niels Möller6af2e862016-06-17 09:12:44 +0200190 const rtc::scoped_refptr<VideoFrameBuffer>& source) {
191 int width = source->width();
192 int height = source->height();
193 rtc::scoped_refptr<I420Buffer> target =
nisse7cc9cc02016-03-29 23:44:19 -0700194 new rtc::RefCountedObject<I420Buffer>(width, height);
Niels Möller6af2e862016-06-17 09:12:44 +0200195 RTC_CHECK(libyuv::I420Copy(source->DataY(), source->StrideY(),
196 source->DataU(), source->StrideU(),
197 source->DataV(), source->StrideV(),
198 target->MutableDataY(), target->StrideY(),
199 target->MutableDataU(), target->StrideU(),
200 target->MutableDataV(), target->StrideV(),
nisse7cc9cc02016-03-29 23:44:19 -0700201 width, height) == 0);
202
Niels Möller6af2e862016-06-17 09:12:44 +0200203 return target;
nisse7cc9cc02016-03-29 23:44:19 -0700204}
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
Niels Möller6af2e862016-06-17 09:12:44 +0200268rtc::scoped_refptr<I420Buffer> I420Buffer::CopyKeepStride(
269 const rtc::scoped_refptr<VideoFrameBuffer>& source) {
270 int width = source->width();
271 int height = source->height();
272 int stride_y = source->StrideY();
273 int stride_u = source->StrideU();
274 int stride_v = source->StrideV();
275 rtc::scoped_refptr<I420Buffer> target =
276 new rtc::RefCountedObject<I420Buffer>(
277 width, height, stride_y, stride_u, stride_v);
278 RTC_CHECK(libyuv::I420Copy(source->DataY(), stride_y,
279 source->DataU(), stride_u,
280 source->DataV(), stride_v,
281 target->MutableDataY(), stride_y,
282 target->MutableDataU(), stride_u,
283 target->MutableDataV(), stride_v,
284 width, height) == 0);
285
286 return target;
287}
288
Peter Boströmeb66e802015-06-05 11:08:03 +0200289NativeHandleBuffer::NativeHandleBuffer(void* native_handle,
290 int width,
291 int height)
292 : native_handle_(native_handle), width_(width), height_(height) {
henrikg91d6ede2015-09-17 00:24:34 -0700293 RTC_DCHECK(native_handle != nullptr);
294 RTC_DCHECK_GT(width, 0);
295 RTC_DCHECK_GT(height, 0);
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000296}
297
Peter Boströmeb66e802015-06-05 11:08:03 +0200298int NativeHandleBuffer::width() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000299 return width_;
300}
301
Peter Boströmeb66e802015-06-05 11:08:03 +0200302int NativeHandleBuffer::height() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000303 return height_;
304}
305
nisse06176e42016-04-18 05:34:40 -0700306const uint8_t* NativeHandleBuffer::DataY() const {
307 RTC_NOTREACHED(); // Should not be called.
308 return nullptr;
309}
310const uint8_t* NativeHandleBuffer::DataU() const {
311 RTC_NOTREACHED(); // Should not be called.
312 return nullptr;
313}
314const uint8_t* NativeHandleBuffer::DataV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000315 RTC_NOTREACHED(); // Should not be called.
316 return nullptr;
317}
318
nisse06176e42016-04-18 05:34:40 -0700319int NativeHandleBuffer::StrideY() const {
320 RTC_NOTREACHED(); // Should not be called.
321 return 0;
322}
323int NativeHandleBuffer::StrideU() const {
324 RTC_NOTREACHED(); // Should not be called.
325 return 0;
326}
327int NativeHandleBuffer::StrideV() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000328 RTC_NOTREACHED(); // Should not be called.
329 return 0;
330}
331
Peter Boströmeb66e802015-06-05 11:08:03 +0200332void* NativeHandleBuffer::native_handle() const {
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000333 return native_handle_;
334}
335
Magnus Jedvertc464f502015-08-25 23:22:08 +0200336WrappedI420Buffer::WrappedI420Buffer(int width,
Per33544192015-04-02 12:30:51 +0200337 int height,
338 const uint8_t* y_plane,
339 int y_stride,
340 const uint8_t* u_plane,
341 int u_stride,
342 const uint8_t* v_plane,
343 int v_stride,
344 const rtc::Callback0<void>& no_longer_used)
Magnus Jedvertc464f502015-08-25 23:22:08 +0200345 : width_(width),
346 height_(height),
347 y_plane_(y_plane),
348 u_plane_(u_plane),
349 v_plane_(v_plane),
350 y_stride_(y_stride),
351 u_stride_(u_stride),
352 v_stride_(v_stride),
353 no_longer_used_cb_(no_longer_used) {
Per33544192015-04-02 12:30:51 +0200354}
355
356WrappedI420Buffer::~WrappedI420Buffer() {
357 no_longer_used_cb_();
358}
359
Per33544192015-04-02 12:30:51 +0200360int WrappedI420Buffer::width() const {
361 return width_;
362}
363
364int WrappedI420Buffer::height() const {
365 return height_;
366}
367
nisse06176e42016-04-18 05:34:40 -0700368const uint8_t* WrappedI420Buffer::DataY() const {
369 return y_plane_;
370}
371const uint8_t* WrappedI420Buffer::DataU() const {
372 return u_plane_;
373}
374const uint8_t* WrappedI420Buffer::DataV() const {
375 return v_plane_;
Per33544192015-04-02 12:30:51 +0200376}
377
nisse06176e42016-04-18 05:34:40 -0700378int WrappedI420Buffer::StrideY() const {
379 return y_stride_;
380}
381int WrappedI420Buffer::StrideU() const {
382 return u_stride_;
383}
384int WrappedI420Buffer::StrideV() const {
385 return v_stride_;
Per33544192015-04-02 12:30:51 +0200386}
387
Per9b3f56e2015-04-09 13:44:16 +0200388void* WrappedI420Buffer::native_handle() const {
Per33544192015-04-02 12:30:51 +0200389 return nullptr;
390}
391
Peter Boströmeb66e802015-06-05 11:08:03 +0200392rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() {
393 RTC_NOTREACHED();
394 return nullptr;
395}
396
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000397} // namespace webrtc