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 | uint8_t* VideoFrameBuffer::MutableDataY() { |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 34 | RTC_NOTREACHED(); |
| 35 | return nullptr; |
| 36 | } |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 37 | uint8_t* VideoFrameBuffer::MutableDataU() { |
| 38 | RTC_NOTREACHED(); |
| 39 | return nullptr; |
| 40 | } |
| 41 | uint8_t* VideoFrameBuffer::MutableDataV() { |
| 42 | RTC_NOTREACHED(); |
| 43 | return nullptr; |
| 44 | } |
| 45 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 46 | VideoFrameBuffer::~VideoFrameBuffer() {} |
| 47 | |
| 48 | I420Buffer::I420Buffer(int width, int height) |
| 49 | : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) { |
| 50 | } |
| 51 | |
| 52 | I420Buffer::I420Buffer(int width, |
| 53 | int height, |
| 54 | int stride_y, |
| 55 | int stride_u, |
| 56 | int stride_v) |
| 57 | : width_(width), |
| 58 | height_(height), |
| 59 | stride_y_(stride_y), |
| 60 | stride_u_(stride_u), |
| 61 | stride_v_(stride_v), |
| 62 | data_(static_cast<uint8_t*>(AlignedMalloc( |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 63 | I420DataSize(height, stride_y, stride_u, stride_v), |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 64 | kBufferAlignment))) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 65 | RTC_DCHECK_GT(width, 0); |
| 66 | RTC_DCHECK_GT(height, 0); |
| 67 | RTC_DCHECK_GE(stride_y, width); |
| 68 | RTC_DCHECK_GE(stride_u, (width + 1) / 2); |
| 69 | RTC_DCHECK_GE(stride_v, (width + 1) / 2); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | I420Buffer::~I420Buffer() { |
| 73 | } |
| 74 | |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 75 | rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) { |
| 76 | return new rtc::RefCountedObject<I420Buffer>(width, height); |
| 77 | } |
| 78 | |
| 79 | rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, |
| 80 | int height, |
| 81 | int stride_y, |
| 82 | int stride_u, |
| 83 | int stride_v) { |
| 84 | return new rtc::RefCountedObject<I420Buffer>( |
| 85 | width, height, stride_y, stride_u, stride_v); |
| 86 | } |
| 87 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 88 | void I420Buffer::InitializeData() { |
| 89 | memset(data_.get(), 0, |
| 90 | I420DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 91 | } |
| 92 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 93 | int I420Buffer::width() const { |
| 94 | return width_; |
| 95 | } |
| 96 | |
| 97 | int I420Buffer::height() const { |
| 98 | return height_; |
| 99 | } |
| 100 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 101 | const uint8_t* I420Buffer::DataY() const { |
| 102 | return data_.get(); |
| 103 | } |
| 104 | const uint8_t* I420Buffer::DataU() const { |
| 105 | return data_.get() + stride_y_ * height_; |
| 106 | } |
| 107 | const uint8_t* I420Buffer::DataV() const { |
| 108 | return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 109 | } |
| 110 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 111 | uint8_t* I420Buffer::MutableDataY() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 112 | return const_cast<uint8_t*>(DataY()); |
| 113 | } |
| 114 | uint8_t* I420Buffer::MutableDataU() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 115 | return const_cast<uint8_t*>(DataU()); |
| 116 | } |
| 117 | uint8_t* I420Buffer::MutableDataV() { |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 118 | return const_cast<uint8_t*>(DataV()); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 119 | } |
| 120 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 121 | int I420Buffer::StrideY() const { |
| 122 | return stride_y_; |
| 123 | } |
| 124 | int I420Buffer::StrideU() const { |
| 125 | return stride_u_; |
| 126 | } |
| 127 | int I420Buffer::StrideV() const { |
| 128 | return stride_v_; |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 131 | void* I420Buffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 132 | return nullptr; |
| 133 | } |
| 134 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 135 | rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() { |
| 136 | RTC_NOTREACHED(); |
| 137 | return nullptr; |
| 138 | } |
| 139 | |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 140 | rtc::scoped_refptr<I420Buffer> I420Buffer::Copy( |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 141 | const rtc::scoped_refptr<VideoFrameBuffer>& source) { |
| 142 | int width = source->width(); |
| 143 | int height = source->height(); |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 144 | rtc::scoped_refptr<I420Buffer> target = I420Buffer::Create(width, height); |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 145 | RTC_CHECK(libyuv::I420Copy(source->DataY(), source->StrideY(), |
| 146 | source->DataU(), source->StrideU(), |
| 147 | source->DataV(), source->StrideV(), |
| 148 | target->MutableDataY(), target->StrideY(), |
| 149 | target->MutableDataU(), target->StrideU(), |
| 150 | target->MutableDataV(), target->StrideV(), |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 151 | width, height) == 0); |
| 152 | |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 153 | return target; |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame] | 154 | } |
| 155 | |
nisse | efec590 | 2016-06-09 00:31:39 -0700 | [diff] [blame] | 156 | void I420Buffer::SetToBlack() { |
| 157 | RTC_CHECK(libyuv::I420Rect(MutableDataY(), StrideY(), |
| 158 | MutableDataU(), StrideU(), |
| 159 | MutableDataV(), StrideV(), |
| 160 | 0, 0, width(), height(), |
| 161 | 0, 128, 128) == 0); |
| 162 | } |
| 163 | |
Niels Möller | 718a763 | 2016-06-13 13:06:01 +0200 | [diff] [blame] | 164 | void I420Buffer::CropAndScaleFrom( |
| 165 | const rtc::scoped_refptr<VideoFrameBuffer>& src, |
| 166 | int offset_x, |
| 167 | int offset_y, |
| 168 | int crop_width, |
| 169 | int crop_height) { |
| 170 | RTC_CHECK_LE(crop_width, src->width()); |
| 171 | RTC_CHECK_LE(crop_height, src->height()); |
| 172 | RTC_CHECK_LE(crop_width + offset_x, src->width()); |
| 173 | RTC_CHECK_LE(crop_height + offset_y, src->height()); |
| 174 | RTC_CHECK_GE(offset_x, 0); |
| 175 | RTC_CHECK_GE(offset_y, 0); |
| 176 | |
| 177 | // Make sure offset is even so that u/v plane becomes aligned. |
| 178 | const int uv_offset_x = offset_x / 2; |
| 179 | const int uv_offset_y = offset_y / 2; |
| 180 | offset_x = uv_offset_x * 2; |
| 181 | offset_y = uv_offset_y * 2; |
| 182 | |
| 183 | const uint8_t* y_plane = |
| 184 | src->DataY() + src->StrideY() * offset_y + offset_x; |
| 185 | const uint8_t* u_plane = |
| 186 | src->DataU() + src->StrideU() * uv_offset_y + uv_offset_x; |
| 187 | const uint8_t* v_plane = |
| 188 | src->DataV() + src->StrideV() * uv_offset_y + uv_offset_x; |
| 189 | int res = libyuv::I420Scale(y_plane, src->StrideY(), |
| 190 | u_plane, src->StrideU(), |
| 191 | v_plane, src->StrideV(), |
| 192 | crop_width, crop_height, |
| 193 | MutableDataY(), StrideY(), |
| 194 | MutableDataU(), StrideU(), |
| 195 | MutableDataV(), StrideV(), |
| 196 | width(), height(), libyuv::kFilterBox); |
| 197 | |
| 198 | RTC_DCHECK_EQ(res, 0); |
| 199 | } |
| 200 | |
| 201 | void I420Buffer::CropAndScaleFrom( |
| 202 | const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 203 | const int crop_width = |
| 204 | std::min(src->width(), width() * src->height() / height()); |
| 205 | const int crop_height = |
| 206 | std::min(src->height(), height() * src->width() / width()); |
| 207 | |
| 208 | CropAndScaleFrom( |
| 209 | src, |
| 210 | (src->width() - crop_width) / 2, (src->height() - crop_height) / 2, |
| 211 | crop_width, crop_height); |
| 212 | } |
| 213 | |
| 214 | void I420Buffer::ScaleFrom(const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 215 | CropAndScaleFrom(src, 0, 0, src->width(), src->height()); |
| 216 | } |
| 217 | |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 218 | rtc::scoped_refptr<I420Buffer> I420Buffer::CopyKeepStride( |
| 219 | const rtc::scoped_refptr<VideoFrameBuffer>& source) { |
| 220 | int width = source->width(); |
| 221 | int height = source->height(); |
| 222 | int stride_y = source->StrideY(); |
| 223 | int stride_u = source->StrideU(); |
| 224 | int stride_v = source->StrideV(); |
| 225 | rtc::scoped_refptr<I420Buffer> target = |
nisse | ac62bd4 | 2016-06-20 03:38:52 -0700 | [diff] [blame] | 226 | I420Buffer::Create(width, height, stride_y, stride_u, stride_v); |
Niels Möller | 6af2e86 | 2016-06-17 09:12:44 +0200 | [diff] [blame] | 227 | RTC_CHECK(libyuv::I420Copy(source->DataY(), stride_y, |
| 228 | source->DataU(), stride_u, |
| 229 | source->DataV(), stride_v, |
| 230 | target->MutableDataY(), stride_y, |
| 231 | target->MutableDataU(), stride_u, |
| 232 | target->MutableDataV(), stride_v, |
| 233 | width, height) == 0); |
| 234 | |
| 235 | return target; |
| 236 | } |
| 237 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 238 | NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 239 | int width, |
| 240 | int height) |
| 241 | : native_handle_(native_handle), width_(width), height_(height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 242 | RTC_DCHECK(native_handle != nullptr); |
| 243 | RTC_DCHECK_GT(width, 0); |
| 244 | RTC_DCHECK_GT(height, 0); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 247 | int NativeHandleBuffer::width() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 248 | return width_; |
| 249 | } |
| 250 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 251 | int NativeHandleBuffer::height() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 252 | return height_; |
| 253 | } |
| 254 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 255 | const uint8_t* NativeHandleBuffer::DataY() const { |
| 256 | RTC_NOTREACHED(); // Should not be called. |
| 257 | return nullptr; |
| 258 | } |
| 259 | const uint8_t* NativeHandleBuffer::DataU() const { |
| 260 | RTC_NOTREACHED(); // Should not be called. |
| 261 | return nullptr; |
| 262 | } |
| 263 | const uint8_t* NativeHandleBuffer::DataV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 264 | RTC_NOTREACHED(); // Should not be called. |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 268 | int NativeHandleBuffer::StrideY() const { |
| 269 | RTC_NOTREACHED(); // Should not be called. |
| 270 | return 0; |
| 271 | } |
| 272 | int NativeHandleBuffer::StrideU() const { |
| 273 | RTC_NOTREACHED(); // Should not be called. |
| 274 | return 0; |
| 275 | } |
| 276 | int NativeHandleBuffer::StrideV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 277 | RTC_NOTREACHED(); // Should not be called. |
| 278 | return 0; |
| 279 | } |
| 280 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 281 | void* NativeHandleBuffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 282 | return native_handle_; |
| 283 | } |
| 284 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 285 | WrappedI420Buffer::WrappedI420Buffer(int width, |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 286 | int height, |
| 287 | const uint8_t* y_plane, |
| 288 | int y_stride, |
| 289 | const uint8_t* u_plane, |
| 290 | int u_stride, |
| 291 | const uint8_t* v_plane, |
| 292 | int v_stride, |
| 293 | const rtc::Callback0<void>& no_longer_used) |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 294 | : width_(width), |
| 295 | height_(height), |
| 296 | y_plane_(y_plane), |
| 297 | u_plane_(u_plane), |
| 298 | v_plane_(v_plane), |
| 299 | y_stride_(y_stride), |
| 300 | u_stride_(u_stride), |
| 301 | v_stride_(v_stride), |
| 302 | no_longer_used_cb_(no_longer_used) { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | WrappedI420Buffer::~WrappedI420Buffer() { |
| 306 | no_longer_used_cb_(); |
| 307 | } |
| 308 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 309 | int WrappedI420Buffer::width() const { |
| 310 | return width_; |
| 311 | } |
| 312 | |
| 313 | int WrappedI420Buffer::height() const { |
| 314 | return height_; |
| 315 | } |
| 316 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 317 | const uint8_t* WrappedI420Buffer::DataY() const { |
| 318 | return y_plane_; |
| 319 | } |
| 320 | const uint8_t* WrappedI420Buffer::DataU() const { |
| 321 | return u_plane_; |
| 322 | } |
| 323 | const uint8_t* WrappedI420Buffer::DataV() const { |
| 324 | return v_plane_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 325 | } |
| 326 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 327 | int WrappedI420Buffer::StrideY() const { |
| 328 | return y_stride_; |
| 329 | } |
| 330 | int WrappedI420Buffer::StrideU() const { |
| 331 | return u_stride_; |
| 332 | } |
| 333 | int WrappedI420Buffer::StrideV() const { |
| 334 | return v_stride_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 337 | void* WrappedI420Buffer::native_handle() const { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 338 | return nullptr; |
| 339 | } |
| 340 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 341 | rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 342 | RTC_NOTREACHED(); |
| 343 | return nullptr; |
| 344 | } |
| 345 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 346 | } // namespace webrtc |