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 | |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 30 | uint8_t* VideoFrameBuffer::MutableData(PlaneType type) { |
| 31 | RTC_NOTREACHED(); |
| 32 | return nullptr; |
| 33 | } |
| 34 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 35 | VideoFrameBuffer::~VideoFrameBuffer() {} |
| 36 | |
| 37 | I420Buffer::I420Buffer(int width, int height) |
| 38 | : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) { |
| 39 | } |
| 40 | |
| 41 | I420Buffer::I420Buffer(int width, |
| 42 | int height, |
| 43 | int stride_y, |
| 44 | int stride_u, |
| 45 | int stride_v) |
| 46 | : width_(width), |
| 47 | height_(height), |
| 48 | stride_y_(stride_y), |
| 49 | stride_u_(stride_u), |
| 50 | stride_v_(stride_v), |
| 51 | data_(static_cast<uint8_t*>(AlignedMalloc( |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 52 | I420DataSize(height, stride_y, stride_u, stride_v), |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 53 | kBufferAlignment))) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 54 | RTC_DCHECK_GT(width, 0); |
| 55 | RTC_DCHECK_GT(height, 0); |
| 56 | RTC_DCHECK_GE(stride_y, width); |
| 57 | RTC_DCHECK_GE(stride_u, (width + 1) / 2); |
| 58 | RTC_DCHECK_GE(stride_v, (width + 1) / 2); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | I420Buffer::~I420Buffer() { |
| 62 | } |
| 63 | |
hbos | 900f975 | 2016-02-05 08:08:34 -0800 | [diff] [blame] | 64 | void I420Buffer::InitializeData() { |
| 65 | memset(data_.get(), 0, |
| 66 | I420DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 67 | } |
| 68 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 69 | int I420Buffer::width() const { |
| 70 | return width_; |
| 71 | } |
| 72 | |
| 73 | int I420Buffer::height() const { |
| 74 | return height_; |
| 75 | } |
| 76 | |
| 77 | const uint8_t* I420Buffer::data(PlaneType type) const { |
| 78 | switch (type) { |
| 79 | case kYPlane: |
| 80 | return data_.get(); |
| 81 | case kUPlane: |
| 82 | return data_.get() + stride_y_ * height_; |
| 83 | case kVPlane: |
| 84 | return data_.get() + stride_y_ * height_ + |
| 85 | stride_u_ * ((height_ + 1) / 2); |
| 86 | default: |
| 87 | RTC_NOTREACHED(); |
| 88 | return nullptr; |
| 89 | } |
| 90 | } |
| 91 | |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 92 | uint8_t* I420Buffer::MutableData(PlaneType type) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 93 | RTC_DCHECK(HasOneRef()); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 94 | return const_cast<uint8_t*>( |
| 95 | static_cast<const VideoFrameBuffer*>(this)->data(type)); |
| 96 | } |
| 97 | |
| 98 | int I420Buffer::stride(PlaneType type) const { |
| 99 | switch (type) { |
| 100 | case kYPlane: |
| 101 | return stride_y_; |
| 102 | case kUPlane: |
| 103 | return stride_u_; |
| 104 | case kVPlane: |
| 105 | return stride_v_; |
| 106 | default: |
| 107 | RTC_NOTREACHED(); |
| 108 | return 0; |
| 109 | } |
| 110 | } |
| 111 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 112 | void* I420Buffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 113 | return nullptr; |
| 114 | } |
| 115 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 116 | rtc::scoped_refptr<VideoFrameBuffer> I420Buffer::NativeToI420Buffer() { |
| 117 | RTC_NOTREACHED(); |
| 118 | return nullptr; |
| 119 | } |
| 120 | |
nisse | 7cc9cc0 | 2016-03-29 23:44:19 -0700 | [diff] [blame^] | 121 | rtc::scoped_refptr<I420Buffer> I420Buffer::Copy( |
| 122 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer) { |
| 123 | int width = buffer->width(); |
| 124 | int height = buffer->height(); |
| 125 | rtc::scoped_refptr<I420Buffer> copy = |
| 126 | new rtc::RefCountedObject<I420Buffer>(width, height); |
| 127 | RTC_CHECK(libyuv::I420Copy(buffer->data(kYPlane), buffer->stride(kYPlane), |
| 128 | buffer->data(kUPlane), buffer->stride(kUPlane), |
| 129 | buffer->data(kVPlane), buffer->stride(kVPlane), |
| 130 | copy->MutableData(kYPlane), copy->stride(kYPlane), |
| 131 | copy->MutableData(kUPlane), copy->stride(kUPlane), |
| 132 | copy->MutableData(kVPlane), copy->stride(kVPlane), |
| 133 | width, height) == 0); |
| 134 | |
| 135 | return copy; |
| 136 | } |
| 137 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 138 | NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 139 | int width, |
| 140 | int height) |
| 141 | : native_handle_(native_handle), width_(width), height_(height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 142 | RTC_DCHECK(native_handle != nullptr); |
| 143 | RTC_DCHECK_GT(width, 0); |
| 144 | RTC_DCHECK_GT(height, 0); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 147 | int NativeHandleBuffer::width() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 148 | return width_; |
| 149 | } |
| 150 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 151 | int NativeHandleBuffer::height() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 152 | return height_; |
| 153 | } |
| 154 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 155 | const uint8_t* NativeHandleBuffer::data(PlaneType type) const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 156 | RTC_NOTREACHED(); // Should not be called. |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 160 | int NativeHandleBuffer::stride(PlaneType type) const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 161 | RTC_NOTREACHED(); // Should not be called. |
| 162 | return 0; |
| 163 | } |
| 164 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 165 | void* NativeHandleBuffer::native_handle() const { |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 166 | return native_handle_; |
| 167 | } |
| 168 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 169 | WrappedI420Buffer::WrappedI420Buffer(int width, |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 170 | int height, |
| 171 | const uint8_t* y_plane, |
| 172 | int y_stride, |
| 173 | const uint8_t* u_plane, |
| 174 | int u_stride, |
| 175 | const uint8_t* v_plane, |
| 176 | int v_stride, |
| 177 | const rtc::Callback0<void>& no_longer_used) |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 178 | : width_(width), |
| 179 | height_(height), |
| 180 | y_plane_(y_plane), |
| 181 | u_plane_(u_plane), |
| 182 | v_plane_(v_plane), |
| 183 | y_stride_(y_stride), |
| 184 | u_stride_(u_stride), |
| 185 | v_stride_(v_stride), |
| 186 | no_longer_used_cb_(no_longer_used) { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | WrappedI420Buffer::~WrappedI420Buffer() { |
| 190 | no_longer_used_cb_(); |
| 191 | } |
| 192 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 193 | int WrappedI420Buffer::width() const { |
| 194 | return width_; |
| 195 | } |
| 196 | |
| 197 | int WrappedI420Buffer::height() const { |
| 198 | return height_; |
| 199 | } |
| 200 | |
| 201 | const uint8_t* WrappedI420Buffer::data(PlaneType type) const { |
| 202 | switch (type) { |
| 203 | case kYPlane: |
| 204 | return y_plane_; |
| 205 | case kUPlane: |
| 206 | return u_plane_; |
| 207 | case kVPlane: |
| 208 | return v_plane_; |
| 209 | default: |
| 210 | RTC_NOTREACHED(); |
| 211 | return nullptr; |
| 212 | } |
| 213 | } |
| 214 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 215 | int WrappedI420Buffer::stride(PlaneType type) const { |
| 216 | switch (type) { |
| 217 | case kYPlane: |
| 218 | return y_stride_; |
| 219 | case kUPlane: |
| 220 | return u_stride_; |
| 221 | case kVPlane: |
| 222 | return v_stride_; |
| 223 | default: |
| 224 | RTC_NOTREACHED(); |
| 225 | return 0; |
| 226 | } |
| 227 | } |
| 228 | |
Per | 9b3f56e | 2015-04-09 13:44:16 +0200 | [diff] [blame] | 229 | void* WrappedI420Buffer::native_handle() const { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 230 | return nullptr; |
| 231 | } |
| 232 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 233 | rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 234 | RTC_NOTREACHED(); |
| 235 | return nullptr; |
| 236 | } |
| 237 | |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 238 | rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop( |
| 239 | const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 240 | int cropped_width, |
| 241 | int cropped_height) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 242 | RTC_CHECK(buffer->native_handle() == nullptr); |
| 243 | RTC_CHECK_LE(cropped_width, buffer->width()); |
| 244 | RTC_CHECK_LE(cropped_height, buffer->height()); |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 245 | if (buffer->width() == cropped_width && buffer->height() == cropped_height) |
| 246 | return buffer; |
| 247 | |
| 248 | // Center crop to |cropped_width| x |cropped_height|. |
| 249 | // Make sure offset is even so that u/v plane becomes aligned. |
| 250 | const int uv_offset_x = (buffer->width() - cropped_width) / 4; |
| 251 | const int uv_offset_y = (buffer->height() - cropped_height) / 4; |
| 252 | const int offset_x = uv_offset_x * 2; |
| 253 | const int offset_y = uv_offset_y * 2; |
| 254 | |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 255 | const uint8_t* y_plane = buffer->data(kYPlane) + |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 256 | buffer->stride(kYPlane) * offset_y + offset_x; |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 257 | const uint8_t* u_plane = buffer->data(kUPlane) + |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 258 | buffer->stride(kUPlane) * uv_offset_y + uv_offset_x; |
Magnus Jedvert | 3318f98 | 2015-08-26 16:06:21 +0200 | [diff] [blame] | 259 | const uint8_t* v_plane = buffer->data(kVPlane) + |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 260 | buffer->stride(kVPlane) * uv_offset_y + uv_offset_x; |
| 261 | return new rtc::RefCountedObject<WrappedI420Buffer>( |
| 262 | cropped_width, cropped_height, |
| 263 | y_plane, buffer->stride(kYPlane), |
| 264 | u_plane, buffer->stride(kUPlane), |
| 265 | v_plane, buffer->stride(kVPlane), |
perkj | 14f4144 | 2015-11-30 22:15:45 -0800 | [diff] [blame] | 266 | rtc::KeepRefUntilDone(buffer)); |
Magnus Jedvert | c464f50 | 2015-08-25 23:22:08 +0200 | [diff] [blame] | 267 | } |
| 268 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 269 | } // namespace webrtc |