Index: webrtc/common_video/video_frame_buffer.cc |
diff --git a/webrtc/common_video/video_frame_buffer.cc b/webrtc/common_video/video_frame_buffer.cc |
index 34213e4656ce766f5cb6f2fdc24e566c02cf152e..2013596fcbf2dfcc933431c982118c730b0608c5 100644 |
--- a/webrtc/common_video/video_frame_buffer.cc |
+++ b/webrtc/common_video/video_frame_buffer.cc |
@@ -8,11 +8,14 @@ |
* be found in the AUTHORS file in the root of the source tree. |
*/ |
+#include <algorithm> |
+ |
#include "webrtc/common_video/include/video_frame_buffer.h" |
#include "webrtc/base/checks.h" |
#include "webrtc/base/keep_ref_until_done.h" |
#include "libyuv/convert.h" |
+#include "libyuv/scale.h" |
// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
static const int kBufferAlignment = 64; |
@@ -200,6 +203,57 @@ rtc::scoped_refptr<I420Buffer> I420Buffer::Copy( |
return copy; |
} |
+void I420Buffer::CropAndScale( |
+ const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
+ int offset_x, |
+ int offset_y, |
+ int crop_width, |
+ int crop_height) { |
+ RTC_CHECK_LE(crop_width, buffer->width()); |
+ RTC_CHECK_LE(crop_height, buffer->height()); |
+ RTC_CHECK_LE(crop_width + offset_x, buffer->width()); |
+ RTC_CHECK_LE(crop_height + offset_y, buffer->height()); |
+ RTC_CHECK_GE(offset_x, 0); |
+ RTC_CHECK_GE(offset_y, 0); |
+ |
+ // Make sure offset is even so that u/v plane becomes aligned. |
+ const int uv_offset_x = offset_x / 2; |
+ const int uv_offset_y = offset_y / 2; |
+ offset_x = uv_offset_x * 2; |
+ offset_y = uv_offset_y * 2; |
+ |
+ const uint8_t* y_plane = |
+ buffer->DataY() + buffer->StrideY() * offset_y + offset_x; |
+ const uint8_t* u_plane = |
+ buffer->DataU() + buffer->StrideU() * uv_offset_y + uv_offset_x; |
+ const uint8_t* v_plane = |
+ buffer->DataV() + buffer->StrideV() * uv_offset_y + uv_offset_x; |
+ int res = libyuv::I420Scale(y_plane, buffer->StrideY(), |
+ u_plane, buffer->StrideU(), |
+ v_plane, buffer->StrideV(), |
+ crop_width, crop_height, |
+ MutableDataY(), StrideY(), |
+ MutableDataU(), StrideU(), |
+ MutableDataV(), StrideV(), |
+ width(), height(), libyuv::kFilterBox); |
+ |
+ RTC_DCHECK(res == 0); |
magjed_webrtc
2016/05/30 11:58:26
Use RTC_DCHECK_EQ instead
nisse-webrtc
2016/05/30 12:58:22
Done.
|
+} |
+ |
+void I420Buffer::CenterCropAndScale( |
+ const rtc::scoped_refptr<I420Buffer>& dst, |
+ const rtc::scoped_refptr<VideoFrameBuffer>& src) { |
+ const int crop_width = |
+ std::min(src->width(), dst->width() * src->height() / dst->height()); |
+ const int crop_height = |
+ std::min(src->height(), dst->height() * src->width() / dst->width()); |
+ |
+ dst->CropAndScale( |
+ src, |
+ (src->width() - crop_width) / 2, (src->height() - crop_height) / 2, |
+ crop_width, crop_height); |
+} |
+ |
NativeHandleBuffer::NativeHandleBuffer(void* native_handle, |
int width, |
int height) |