magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 1 | /* |
| 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öller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 11 | #include <algorithm> |
| 12 | |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 13 | #include "webrtc/common_video/include/video_frame_buffer.h" |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 14 | |
| 15 | #include "webrtc/base/checks.h" |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 16 | #include "webrtc/base/keep_ref_until_done.h" |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 17 | #include "libyuv/convert.h" |
fbarchard | d1f83cf | 2016-07-08 17:33:20 -0700 | [diff] [blame] | 18 | #include "libyuv/planar_functions.h" |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 19 | #include "libyuv/scale.h" |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 20 | |
| 21 | // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 22 | static const int kBufferAlignment = 64; |
| 23 | |
| 24 | namespace webrtc { |
| 25 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 26 | namespace { |
| 27 | |
| 28 | int 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.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 34 | VideoFrameBuffer::~VideoFrameBuffer() {} |
| 35 | |
| 36 | I420Buffer::I420Buffer(int width, int height) |
| 37 | : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) { |
| 38 | } |
| 39 | |
| 40 | I420Buffer::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( |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 51 | I420DataSize(height, stride_y, stride_u, stride_v), |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 52 | kBufferAlignment))) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 53 | 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.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | I420Buffer::~I420Buffer() { |
| 61 | } |
| 62 | |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 63 | rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) { |
| 64 | return new rtc::RefCountedObject<I420Buffer>(width, height); |
| 65 | } |
| 66 | |
| 67 | rtc::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 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 76 | void I420Buffer::InitializeData() { |
| 77 | memset(data_.get(), 0, |
| 78 | I420DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 79 | } |
| 80 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 81 | int I420Buffer::width() const { |
| 82 | return width_; |
| 83 | } |
| 84 | |
| 85 | int I420Buffer::height() const { |
| 86 | return height_; |
| 87 | } |
| 88 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 89 | const uint8_t* I420Buffer::DataY() const { |
| 90 | return data_.get(); |
| 91 | } |
| 92 | const uint8_t* I420Buffer::DataU() const { |
| 93 | return data_.get() + stride_y_ * height_; |
| 94 | } |
| 95 | const uint8_t* I420Buffer::DataV() const { |
| 96 | return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 97 | } |
| 98 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 99 | uint8_t* I420Buffer::MutableDataY() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 100 | return const_cast<uint8_t*>(DataY()); |
| 101 | } |
| 102 | uint8_t* I420Buffer::MutableDataU() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 103 | return const_cast<uint8_t*>(DataU()); |
| 104 | } |
| 105 | uint8_t* I420Buffer::MutableDataV() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 106 | return const_cast<uint8_t*>(DataV()); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 107 | } |
| 108 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 109 | int I420Buffer::StrideY() const { |
| 110 | return stride_y_; |
| 111 | } |
| 112 | int I420Buffer::StrideU() const { |
| 113 | return stride_u_; |
| 114 | } |
| 115 | int I420Buffer::StrideV() const { |
| 116 | return stride_v_; |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 119 | void* I420Buffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 120 | return nullptr; |
| 121 | } |
| 122 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 123 | rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() { |
| 124 | RTC_NOTREACHED(); |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 128 | rtc::scoped_refptr<I420Buffer> I420Buffer::Copy( |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 129 | const rtc::scoped_refptr<VideoFrameBuffer>& source) { |
| 130 | int width = source->width(); |
| 131 | int height = source->height(); |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 132 | rtc::scoped_refptr<I420Buffer> target = I420Buffer::Create(width, height); |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 133 | 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(), |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 139 | width, height) == 0); |
| 140 | |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 141 | return target; |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 142 | } |
| 143 | |
nisse | efec590 | 2016-06-09 00:31:39 -0700 | [diff] [blame] | 144 | void 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öller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 152 | void 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 | |
| 189 | void 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 | |
| 202 | void I420Buffer::ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 203 | CropAndScaleFrom(src, 0, 0, src->width(), src->height()); |
| 204 | } |
| 205 | |
nisse | d507472 | 2016-08-30 08:45:44 -0700 | [diff] [blame] | 206 | // static |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 207 | rtc::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 = |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 215 | I420Buffer::Create(width, height, stride_y, stride_u, stride_v); |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 216 | 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 | |
nisse | d507472 | 2016-08-30 08:45:44 -0700 | [diff] [blame] | 227 | // static |
| 228 | rtc::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öm | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 262 | NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 263 | int width, |
| 264 | int height) |
| 265 | : native_handle_(native_handle), width_(width), height_(height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 266 | RTC_DCHECK(native_handle != nullptr); |
| 267 | RTC_DCHECK_GT(width, 0); |
| 268 | RTC_DCHECK_GT(height, 0); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 271 | int NativeHandleBuffer::width() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 272 | return width_; |
| 273 | } |
| 274 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 275 | int NativeHandleBuffer::height() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 276 | return height_; |
| 277 | } |
| 278 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 279 | const uint8_t* NativeHandleBuffer::DataY() const { |
| 280 | RTC_NOTREACHED(); // Should not be called. |
| 281 | return nullptr; |
| 282 | } |
| 283 | const uint8_t* NativeHandleBuffer::DataU() const { |
| 284 | RTC_NOTREACHED(); // Should not be called. |
| 285 | return nullptr; |
| 286 | } |
| 287 | const uint8_t* NativeHandleBuffer::DataV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 288 | RTC_NOTREACHED(); // Should not be called. |
| 289 | return nullptr; |
| 290 | } |
| 291 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 292 | int NativeHandleBuffer::StrideY() const { |
| 293 | RTC_NOTREACHED(); // Should not be called. |
| 294 | return 0; |
| 295 | } |
| 296 | int NativeHandleBuffer::StrideU() const { |
| 297 | RTC_NOTREACHED(); // Should not be called. |
| 298 | return 0; |
| 299 | } |
| 300 | int NativeHandleBuffer::StrideV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 301 | RTC_NOTREACHED(); // Should not be called. |
| 302 | return 0; |
| 303 | } |
| 304 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 305 | void* NativeHandleBuffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 306 | return native_handle_; |
| 307 | } |
| 308 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 309 | WrappedI420Buffer::WrappedI420Buffer(int width, |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 310 | 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 Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 318 | : 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) { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | WrappedI420Buffer::~WrappedI420Buffer() { |
| 330 | no_longer_used_cb_(); |
| 331 | } |
| 332 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 333 | int WrappedI420Buffer::width() const { |
| 334 | return width_; |
| 335 | } |
| 336 | |
| 337 | int WrappedI420Buffer::height() const { |
| 338 | return height_; |
| 339 | } |
| 340 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 341 | const uint8_t* WrappedI420Buffer::DataY() const { |
| 342 | return y_plane_; |
| 343 | } |
| 344 | const uint8_t* WrappedI420Buffer::DataU() const { |
| 345 | return u_plane_; |
| 346 | } |
| 347 | const uint8_t* WrappedI420Buffer::DataV() const { |
| 348 | return v_plane_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 349 | } |
| 350 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 351 | int WrappedI420Buffer::StrideY() const { |
| 352 | return y_stride_; |
| 353 | } |
| 354 | int WrappedI420Buffer::StrideU() const { |
| 355 | return u_stride_; |
| 356 | } |
| 357 | int WrappedI420Buffer::StrideV() const { |
| 358 | return v_stride_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 361 | void* WrappedI420Buffer::native_handle() const { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 362 | return nullptr; |
| 363 | } |
| 364 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 365 | rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 366 | RTC_NOTREACHED(); |
| 367 | return nullptr; |
| 368 | } |
| 369 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 370 | } // namespace webrtc |