From bfe45c29c59c486a07f2465e4e4452e6f4540991 Mon Sep 17 00:00:00 2001 From: Anders Carlsson Date: Mon, 19 Jun 2017 16:46:31 +0200 Subject: [PATCH] Use uint8 pointer instead of std::vector in NV12Scale. To prepare for landing 536773. Bug: webrtc:7785 Change-Id: I841218dca3fb9d83f362f7f2b9076f3f189e7c15 Reviewed-on: https://chromium-review.googlesource.com/539577 Commit-Queue: Anders Carlsson Reviewed-by: Magnus Jedvert Cr-Commit-Position: refs/heads/master@{#18662} --- .../common_video/libyuv/include/webrtc_libyuv.h | 6 +++++- webrtc/common_video/libyuv/libyuv_unittest.cc | 15 ++++++++++++--- webrtc/common_video/libyuv/webrtc_libyuv.cc | 9 ++------- .../Classes/Video/corevideo_frame_buffer.cc | 15 ++++++++++++++- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/webrtc/common_video/libyuv/include/webrtc_libyuv.h b/webrtc/common_video/libyuv/include/webrtc_libyuv.h index b0c6ab6f4a..13e58d2ae4 100644 --- a/webrtc/common_video/libyuv/include/webrtc_libyuv.h +++ b/webrtc/common_video/libyuv/include/webrtc_libyuv.h @@ -117,7 +117,11 @@ double I420SSIM(const I420BufferInterface& ref_buffer, const I420BufferInterface& test_buffer); // Helper function for scaling NV12 to NV12. -void NV12Scale(std::vector* tmp_buffer, +// If the |src_width| and |src_height| matches the |dst_width| and |dst_height|, +// then |tmp_buffer| is not used. In other cases, the minimum size of +// |tmp_buffer| should be: +// (src_width/2) * (src_height/2) * 2 + (dst_width/2) * (dst_height/2) * 2 +void NV12Scale(uint8_t* tmp_buffer, const uint8_t* src_y, int src_stride_y, const uint8_t* src_uv, int src_stride_uv, int src_width, int src_height, diff --git a/webrtc/common_video/libyuv/libyuv_unittest.cc b/webrtc/common_video/libyuv/libyuv_unittest.cc index ea90ec4e90..60e45baf81 100644 --- a/webrtc/common_video/libyuv/libyuv_unittest.cc +++ b/webrtc/common_video/libyuv/libyuv_unittest.cc @@ -277,8 +277,9 @@ TEST_F(TestLibYuv, NV12Scale2x2to2x2) { std::vector dst_y(4); std::vector dst_uv(2); - std::vector tmp_buffer; - NV12Scale(&tmp_buffer, + uint8_t* tmp_buffer = nullptr; + + NV12Scale(tmp_buffer, src_y.data(), 2, src_uv.data(), 2, 2, 2, @@ -301,7 +302,15 @@ TEST_F(TestLibYuv, NV12Scale4x4to2x2) { std::vector dst_uv(2); std::vector tmp_buffer; - NV12Scale(&tmp_buffer, + const int src_chroma_width = (4 + 1) / 2; + const int src_chroma_height = (4 + 1) / 2; + const int dst_chroma_width = (2 + 1) / 2; + const int dst_chroma_height = (2 + 1) / 2; + tmp_buffer.resize(src_chroma_width * src_chroma_height * 2 + + dst_chroma_width * dst_chroma_height * 2); + tmp_buffer.shrink_to_fit(); + + NV12Scale(tmp_buffer.data(), src_y, 4, src_uv, 4, 4, 4, diff --git a/webrtc/common_video/libyuv/webrtc_libyuv.cc b/webrtc/common_video/libyuv/webrtc_libyuv.cc index 2f60e0931f..0b3957a9cf 100644 --- a/webrtc/common_video/libyuv/webrtc_libyuv.cc +++ b/webrtc/common_video/libyuv/webrtc_libyuv.cc @@ -307,7 +307,7 @@ double I420SSIM(const VideoFrame* ref_frame, const VideoFrame* test_frame) { *test_frame->video_frame_buffer()->ToI420()); } -void NV12Scale(std::vector* tmp_buffer, +void NV12Scale(uint8_t* tmp_buffer, const uint8_t* src_y, int src_stride_y, const uint8_t* src_uv, int src_stride_uv, int src_width, int src_height, @@ -319,8 +319,6 @@ void NV12Scale(std::vector* tmp_buffer, if (src_width == dst_width && src_height == dst_height) { // No scaling. - tmp_buffer->clear(); - tmp_buffer->shrink_to_fit(); libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, src_width, src_height); libyuv::CopyPlane(src_uv, src_stride_uv, dst_uv, dst_stride_uv, @@ -332,11 +330,8 @@ void NV12Scale(std::vector* tmp_buffer, // Allocate temporary memory for spitting UV planes and scaling them. const int dst_chroma_width = (dst_width + 1) / 2; const int dst_chroma_height = (dst_height + 1) / 2; - tmp_buffer->resize(src_chroma_width * src_chroma_height * 2 + - dst_chroma_width * dst_chroma_height * 2); - tmp_buffer->shrink_to_fit(); - uint8_t* const src_u = tmp_buffer->data(); + uint8_t* const src_u = tmp_buffer; uint8_t* const src_v = src_u + src_chroma_width * src_chroma_height; uint8_t* const dst_u = src_v + src_chroma_width * src_chroma_height; uint8_t* const dst_v = dst_u + dst_chroma_width * dst_chroma_height; diff --git a/webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.cc b/webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.cc index 660b2aab9e..b0a17ffe32 100644 --- a/webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.cc +++ b/webrtc/sdk/objc/Framework/Classes/Video/corevideo_frame_buffer.cc @@ -148,7 +148,20 @@ bool CoreVideoFrameBuffer::CropAndScaleTo( src_y += src_y_stride * crop_y_ + crop_x_; src_uv += src_uv_stride * (crop_y_ / 2) + crop_x_; - NV12Scale(tmp_buffer, + if (crop_width_ == dst_width && crop_height_ == dst_height) { + tmp_buffer->clear(); + tmp_buffer->shrink_to_fit(); + } else { + const int src_chroma_width = (crop_width_ + 1) / 2; + const int src_chroma_height = (crop_height_ + 1) / 2; + const int dst_chroma_width = (dst_width + 1) / 2; + const int dst_chroma_height = (dst_height + 1) / 2; + tmp_buffer->resize(src_chroma_width * src_chroma_height * 2 + + dst_chroma_width * dst_chroma_height * 2); + tmp_buffer->shrink_to_fit(); + } + + NV12Scale(tmp_buffer->data(), src_y, src_y_stride, src_uv, src_uv_stride, crop_width_, crop_height_,