| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <algorithm> |
| 12 |
| 11 #include "webrtc/common_video/include/video_frame_buffer.h" | 13 #include "webrtc/common_video/include/video_frame_buffer.h" |
| 12 | 14 |
| 13 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
| 14 #include "webrtc/base/keep_ref_until_done.h" | 16 #include "webrtc/base/keep_ref_until_done.h" |
| 15 #include "libyuv/convert.h" | 17 #include "libyuv/convert.h" |
| 18 #include "libyuv/scale.h" |
| 16 | 19 |
| 17 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. | 20 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 18 static const int kBufferAlignment = 64; | 21 static const int kBufferAlignment = 64; |
| 19 | 22 |
| 20 namespace webrtc { | 23 namespace webrtc { |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 int I420DataSize(int height, int stride_y, int stride_u, int stride_v) { | 27 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); | 28 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2); |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 buffer->DataU(), buffer->StrideU(), | 196 buffer->DataU(), buffer->StrideU(), |
| 194 buffer->DataV(), buffer->StrideV(), | 197 buffer->DataV(), buffer->StrideV(), |
| 195 copy->MutableDataY(), copy->StrideY(), | 198 copy->MutableDataY(), copy->StrideY(), |
| 196 copy->MutableDataU(), copy->StrideU(), | 199 copy->MutableDataU(), copy->StrideU(), |
| 197 copy->MutableDataV(), copy->StrideV(), | 200 copy->MutableDataV(), copy->StrideV(), |
| 198 width, height) == 0); | 201 width, height) == 0); |
| 199 | 202 |
| 200 return copy; | 203 return copy; |
| 201 } | 204 } |
| 202 | 205 |
| 206 void I420Buffer::CropAndScale( |
| 207 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
| 208 int offset_x, |
| 209 int offset_y, |
| 210 int crop_width, |
| 211 int crop_height) { |
| 212 RTC_CHECK_LE(crop_width, buffer->width()); |
| 213 RTC_CHECK_LE(crop_height, buffer->height()); |
| 214 RTC_CHECK_LE(crop_width + offset_x, buffer->width()); |
| 215 RTC_CHECK_LE(crop_height + offset_y, buffer->height()); |
| 216 RTC_CHECK_GE(offset_x, 0); |
| 217 RTC_CHECK_GE(offset_y, 0); |
| 218 |
| 219 // Make sure offset is even so that u/v plane becomes aligned. |
| 220 const int uv_offset_x = offset_x / 2; |
| 221 const int uv_offset_y = offset_y / 2; |
| 222 offset_x = uv_offset_x * 2; |
| 223 offset_y = uv_offset_y * 2; |
| 224 |
| 225 const uint8_t* y_plane = |
| 226 buffer->DataY() + buffer->StrideY() * offset_y + offset_x; |
| 227 const uint8_t* u_plane = |
| 228 buffer->DataU() + buffer->StrideU() * uv_offset_y + uv_offset_x; |
| 229 const uint8_t* v_plane = |
| 230 buffer->DataV() + buffer->StrideV() * uv_offset_y + uv_offset_x; |
| 231 int res = libyuv::I420Scale(y_plane, buffer->StrideY(), |
| 232 u_plane, buffer->StrideU(), |
| 233 v_plane, buffer->StrideV(), |
| 234 crop_width, crop_height, |
| 235 MutableDataY(), StrideY(), |
| 236 MutableDataU(), StrideU(), |
| 237 MutableDataV(), StrideV(), |
| 238 width(), height(), libyuv::kFilterBox); |
| 239 |
| 240 RTC_DCHECK_EQ(res, 0); |
| 241 } |
| 242 |
| 243 void I420Buffer::CenterCropAndScale( |
| 244 const rtc::scoped_refptr<I420Buffer>& dst, |
| 245 const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
| 246 const int crop_width = |
| 247 std::min(src->width(), dst->width() * src->height() / dst->height()); |
| 248 const int crop_height = |
| 249 std::min(src->height(), dst->height() * src->width() / dst->width()); |
| 250 |
| 251 dst->CropAndScale( |
| 252 src, |
| 253 (src->width() - crop_width) / 2, (src->height() - crop_height) / 2, |
| 254 crop_width, crop_height); |
| 255 } |
| 256 |
| 203 NativeHandleBuffer::NativeHandleBuffer(void* native_handle, | 257 NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
| 204 int width, | 258 int width, |
| 205 int height) | 259 int height) |
| 206 : native_handle_(native_handle), width_(width), height_(height) { | 260 : native_handle_(native_handle), width_(width), height_(height) { |
| 207 RTC_DCHECK(native_handle != nullptr); | 261 RTC_DCHECK(native_handle != nullptr); |
| 208 RTC_DCHECK_GT(width, 0); | 262 RTC_DCHECK_GT(width, 0); |
| 209 RTC_DCHECK_GT(height, 0); | 263 RTC_DCHECK_GT(height, 0); |
| 210 } | 264 } |
| 211 | 265 |
| 212 int NativeHandleBuffer::width() const { | 266 int NativeHandleBuffer::width() const { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 | 355 |
| 302 void* WrappedI420Buffer::native_handle() const { | 356 void* WrappedI420Buffer::native_handle() const { |
| 303 return nullptr; | 357 return nullptr; |
| 304 } | 358 } |
| 305 | 359 |
| 306 rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { | 360 rtc::scoped_refptr<VideoFrameBuffer> WrappedI420Buffer::NativeToI420Buffer() { |
| 307 RTC_NOTREACHED(); | 361 RTC_NOTREACHED(); |
| 308 return nullptr; | 362 return nullptr; |
| 309 } | 363 } |
| 310 | 364 |
| 311 rtc::scoped_refptr<VideoFrameBuffer> ShallowCenterCrop( | |
| 312 const rtc::scoped_refptr<VideoFrameBuffer>& buffer, | |
| 313 int cropped_width, | |
| 314 int cropped_height) { | |
| 315 RTC_CHECK(buffer->native_handle() == nullptr); | |
| 316 RTC_CHECK_LE(cropped_width, buffer->width()); | |
| 317 RTC_CHECK_LE(cropped_height, buffer->height()); | |
| 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 | |
| 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; | |
| 334 return new rtc::RefCountedObject<WrappedI420Buffer>( | |
| 335 cropped_width, cropped_height, | |
| 336 y_plane, buffer->StrideY(), | |
| 337 u_plane, buffer->StrideU(), | |
| 338 v_plane, buffer->StrideV(), | |
| 339 rtc::KeepRefUntilDone(buffer)); | |
| 340 } | |
| 341 | |
| 342 } // namespace webrtc | 365 } // namespace webrtc |
| OLD | NEW |