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 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 203 | NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 204 | int width, |
| 205 | int height) |
| 206 | : native_handle_(native_handle), width_(width), height_(height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 207 | RTC_DCHECK(native_handle != nullptr); |
| 208 | RTC_DCHECK_GT(width, 0); |
| 209 | RTC_DCHECK_GT(height, 0); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 212 | int NativeHandleBuffer::width() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 213 | return width_; |
| 214 | } |
| 215 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 216 | int NativeHandleBuffer::height() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 217 | return height_; |
| 218 | } |
| 219 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 220 | const uint8_t* NativeHandleBuffer::DataY() const { |
| 221 | RTC_NOTREACHED(); // Should not be called. |
| 222 | return nullptr; |
| 223 | } |
| 224 | const uint8_t* NativeHandleBuffer::DataU() const { |
| 225 | RTC_NOTREACHED(); // Should not be called. |
| 226 | return nullptr; |
| 227 | } |
| 228 | const uint8_t* NativeHandleBuffer::DataV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 229 | RTC_NOTREACHED(); // Should not be called. |
| 230 | return nullptr; |
| 231 | } |
| 232 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 233 | int NativeHandleBuffer::StrideY() const { |
| 234 | RTC_NOTREACHED(); // Should not be called. |
| 235 | return 0; |
| 236 | } |
| 237 | int NativeHandleBuffer::StrideU() const { |
| 238 | RTC_NOTREACHED(); // Should not be called. |
| 239 | return 0; |
| 240 | } |
| 241 | int NativeHandleBuffer::StrideV() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 242 | RTC_NOTREACHED(); // Should not be called. |
| 243 | return 0; |
| 244 | } |
| 245 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 246 | void* NativeHandleBuffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 247 | return native_handle_; |
| 248 | } |
| 249 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 250 | WrappedI420Buffer::WrappedI420Buffer(int width, |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 251 | int height, |
| 252 | const uint8_t* y_plane, |
| 253 | int y_stride, |
| 254 | const uint8_t* u_plane, |
| 255 | int u_stride, |
| 256 | const uint8_t* v_plane, |
| 257 | int v_stride, |
| 258 | const rtc::Callback0<void>& no_longer_used) |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 259 | : width_(width), |
| 260 | height_(height), |
| 261 | y_plane_(y_plane), |
| 262 | u_plane_(u_plane), |
| 263 | v_plane_(v_plane), |
| 264 | y_stride_(y_stride), |
| 265 | u_stride_(u_stride), |
| 266 | v_stride_(v_stride), |
| 267 | no_longer_used_cb_(no_longer_used) { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | WrappedI420Buffer::~WrappedI420Buffer() { |
| 271 | no_longer_used_cb_(); |
| 272 | } |
| 273 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 274 | int WrappedI420Buffer::width() const { |
| 275 | return width_; |
| 276 | } |
| 277 | |
| 278 | int WrappedI420Buffer::height() const { |
| 279 | return height_; |
| 280 | } |
| 281 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 282 | const uint8_t* WrappedI420Buffer::DataY() const { |
| 283 | return y_plane_; |
| 284 | } |
| 285 | const uint8_t* WrappedI420Buffer::DataU() const { |
| 286 | return u_plane_; |
| 287 | } |
| 288 | const uint8_t* WrappedI420Buffer::DataV() const { |
| 289 | return v_plane_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 290 | } |
| 291 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 292 | int WrappedI420Buffer::StrideY() const { |
| 293 | return y_stride_; |
| 294 | } |
| 295 | int WrappedI420Buffer::StrideU() const { |
| 296 | return u_stride_; |
| 297 | } |
| 298 | int WrappedI420Buffer::StrideV() const { |
| 299 | return v_stride_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 302 | void* WrappedI420Buffer::native_handle() const { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 303 | return nullptr; |
| 304 | } |
| 305 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 306 | rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 307 | RTC_NOTREACHED(); |
| 308 | return nullptr; |
| 309 | } |
| 310 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 311 | rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop( |
| 312 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 313 | int cropped_width, |
| 314 | int cropped_height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 315 | RTC_CHECK(buffer->native_handle() == nullptr); |
| 316 | RTC_CHECK_LE(cropped_width, buffer->width()); |
| 317 | RTC_CHECK_LE(cropped_height, buffer->height()); |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 318 | if (buffer->width() == cropped_width && buffer->height() == cropped_height) |
| 319 | return buffer; |
| 320 | |
| 321 | // Center crop to |cropped_width| x |cropped_height|. |
| 322 | // Make sure offset is even so that u/v plane becomes aligned. |
| 323 | const int uv_offset_x = (buffer->width() - cropped_width) / 4; |
| 324 | const int uv_offset_y = (buffer->height() - cropped_height) / 4; |
| 325 | const int offset_x = uv_offset_x * 2; |
| 326 | const int offset_y = uv_offset_y * 2; |
| 327 | |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 328 | const uint8_t* y_plane = buffer->DataY() + |
| 329 | buffer->StrideY() * offset_y + offset_x; |
| 330 | const uint8_t* u_plane = buffer->DataU() + |
| 331 | buffer->StrideU() * uv_offset_y + uv_offset_x; |
| 332 | const uint8_t* v_plane = buffer->DataV() + |
| 333 | buffer->StrideV() * uv_offset_y + uv_offset_x; |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 334 | return new rtc::RefCountedObject<WrappedI420Buffer>( |
| 335 | cropped_width, cropped_height, |
nisse | 06176e4 | 2016-04-18 05:34:40 -0700 | [diff] [blame] | 336 | y_plane, buffer->StrideY(), |
| 337 | u_plane, buffer->StrideU(), |
| 338 | v_plane, buffer->StrideV(), |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 339 | rtc::KeepRefUntilDone(buffer)); |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 340 | } |
| 341 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 342 | } // namespace webrtc |