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" |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 18 | #include "libyuv/scale.h" |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 19 | |
| 20 | // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 21 | static const int kBufferAlignment = 64; |
| 22 | |
| 23 | namespace webrtc { |
| 24 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | int 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 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 33 | const 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 | |
| 47 | const uint8_t* VideoFrameBuffer::DataY() const { |
| 48 | return data(kYPlane); |
| 49 | } |
| 50 | const uint8_t* VideoFrameBuffer::DataU() const { |
| 51 | return data(kUPlane); |
| 52 | } |
| 53 | const uint8_t* VideoFrameBuffer::DataV() const { |
| 54 | return data(kVPlane); |
| 55 | } |
| 56 | |
| 57 | int 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 | |
| 71 | int VideoFrameBuffer::StrideY() const { |
| 72 | return stride(kYPlane); |
| 73 | } |
| 74 | int VideoFrameBuffer::StrideU() const { |
| 75 | return stride(kUPlane); |
| 76 | } |
| 77 | int VideoFrameBuffer::StrideV() const { |
| 78 | return stride(kVPlane); |
| 79 | } |
| 80 | |
| 81 | uint8_t* VideoFrameBuffer::MutableDataY() { |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 82 | RTC_NOTREACHED(); |
| 83 | return nullptr; |
| 84 | } |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 85 | uint8_t* VideoFrameBuffer::MutableDataU() { |
| 86 | RTC_NOTREACHED(); |
| 87 | return nullptr; |
| 88 | } |
| 89 | uint8_t* VideoFrameBuffer::MutableDataV() { |
| 90 | RTC_NOTREACHED(); |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | uint8_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 Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 107 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 108 | VideoFrameBuffer::~VideoFrameBuffer() {} |
| 109 | |
| 110 | I420Buffer::I420Buffer(int width, int height) |
| 111 | : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) { |
| 112 | } |
| 113 | |
| 114 | I420Buffer::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( |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 125 | I420DataSize(height, stride_y, stride_u, stride_v), |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 126 | kBufferAlignment))) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 127 | 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.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | I420Buffer::~I420Buffer() { |
| 135 | } |
| 136 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 137 | void I420Buffer::InitializeData() { |
| 138 | memset(data_.get(), 0, |
| 139 | I420DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 140 | } |
| 141 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 142 | int I420Buffer::width() const { |
| 143 | return width_; |
| 144 | } |
| 145 | |
| 146 | int I420Buffer::height() const { |
| 147 | return height_; |
| 148 | } |
| 149 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 150 | const uint8_t* I420Buffer::DataY() const { |
| 151 | return data_.get(); |
| 152 | } |
| 153 | const uint8_t* I420Buffer::DataU() const { |
| 154 | return data_.get() + stride_y_ * height_; |
| 155 | } |
| 156 | const uint8_t* I420Buffer::DataV() const { |
| 157 | return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 158 | } |
| 159 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 160 | uint8_t* I420Buffer::MutableDataY() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 161 | return const_cast<uint8_t*>(DataY()); |
| 162 | } |
| 163 | uint8_t* I420Buffer::MutableDataU() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 164 | return const_cast<uint8_t*>(DataU()); |
| 165 | } |
| 166 | uint8_t* I420Buffer::MutableDataV() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 167 | return const_cast<uint8_t*>(DataV()); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 168 | } |
| 169 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 170 | int I420Buffer::StrideY() const { |
| 171 | return stride_y_; |
| 172 | } |
| 173 | int I420Buffer::StrideU() const { |
| 174 | return stride_u_; |
| 175 | } |
| 176 | int I420Buffer::StrideV() const { |
| 177 | return stride_v_; |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 180 | void* I420Buffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 181 | return nullptr; |
| 182 | } |
| 183 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 184 | rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() { |
| 185 | RTC_NOTREACHED(); |
| 186 | return nullptr; |
| 187 | } |
| 188 | |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 189 | rtc::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); |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 195 | 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(), |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 201 | width, height) == 0); |
| 202 | |
| 203 | return copy; |
| 204 | } |
| 205 | |
nisse | efec590 | 2016-06-09 00:31:39 -0700 | [diff] [blame] | 206 | void 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öller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame^] | 214 | void 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 | |
| 251 | void 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 | |
| 264 | void I420Buffer::ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 265 | CropAndScaleFrom(src, 0, 0, src->width(), src->height()); |
| 266 | } |
| 267 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 268 | NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 269 | int width, |
| 270 | int height) |
| 271 | : native_handle_(native_handle), width_(width), height_(height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 272 | RTC_DCHECK(native_handle != nullptr); |
| 273 | RTC_DCHECK_GT(width, 0); |
| 274 | RTC_DCHECK_GT(height, 0); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 277 | int NativeHandleBuffer::width() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 278 | return width_; |
| 279 | } |
| 280 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 281 | int NativeHandleBuffer::height() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 282 | return height_; |
| 283 | } |
| 284 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 285 | const uint8_t* NativeHandleBuffer::DataY() const { |
| 286 | RTC_NOTREACHED(); // Should not be called. |
| 287 | return nullptr; |
| 288 | } |
| 289 | const uint8_t* NativeHandleBuffer::DataU() const { |
| 290 | RTC_NOTREACHED(); // Should not be called. |
| 291 | return nullptr; |
| 292 | } |
| 293 | const uint8_t* NativeHandleBuffer::DataV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 294 | RTC_NOTREACHED(); // Should not be called. |
| 295 | return nullptr; |
| 296 | } |
| 297 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 298 | int NativeHandleBuffer::StrideY() const { |
| 299 | RTC_NOTREACHED(); // Should not be called. |
| 300 | return 0; |
| 301 | } |
| 302 | int NativeHandleBuffer::StrideU() const { |
| 303 | RTC_NOTREACHED(); // Should not be called. |
| 304 | return 0; |
| 305 | } |
| 306 | int NativeHandleBuffer::StrideV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 307 | RTC_NOTREACHED(); // Should not be called. |
| 308 | return 0; |
| 309 | } |
| 310 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 311 | void* NativeHandleBuffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 312 | return native_handle_; |
| 313 | } |
| 314 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 315 | WrappedI420Buffer::WrappedI420Buffer(int width, |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 316 | 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 Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 324 | : 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) { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | WrappedI420Buffer::~WrappedI420Buffer() { |
| 336 | no_longer_used_cb_(); |
| 337 | } |
| 338 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 339 | int WrappedI420Buffer::width() const { |
| 340 | return width_; |
| 341 | } |
| 342 | |
| 343 | int WrappedI420Buffer::height() const { |
| 344 | return height_; |
| 345 | } |
| 346 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 347 | const uint8_t* WrappedI420Buffer::DataY() const { |
| 348 | return y_plane_; |
| 349 | } |
| 350 | const uint8_t* WrappedI420Buffer::DataU() const { |
| 351 | return u_plane_; |
| 352 | } |
| 353 | const uint8_t* WrappedI420Buffer::DataV() const { |
| 354 | return v_plane_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 355 | } |
| 356 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 357 | int WrappedI420Buffer::StrideY() const { |
| 358 | return y_stride_; |
| 359 | } |
| 360 | int WrappedI420Buffer::StrideU() const { |
| 361 | return u_stride_; |
| 362 | } |
| 363 | int WrappedI420Buffer::StrideV() const { |
| 364 | return v_stride_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 367 | void* WrappedI420Buffer::native_handle() const { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 368 | return nullptr; |
| 369 | } |
| 370 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 371 | rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 372 | RTC_NOTREACHED(); |
| 373 | return nullptr; |
| 374 | } |
| 375 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 376 | } // namespace webrtc |