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