From ac62bd4a3b9add714566e3c2b4e92621dda92792 Mon Sep 17 00:00:00 2001 From: nisse Date: Mon, 20 Jun 2016 03:38:52 -0700 Subject: [PATCH] Rewrite CreateBlackFrame in webrtcvideoengine. Don't use VideoFrameBuffer::MutableDataY and friends, instead, use I420Buffer::SetToBlack. Also introduce static method I420Buffer::Create, to create an object and return a scoped_refptr. TBR=marpan@webrtc.org # Trivial change to video_denoiser.cc BUG=webrtc:5921 Review-Url: https://codereview.webrtc.org/2078943002 Cr-Commit-Position: refs/heads/master@{#13212} --- .../common_video/i420_video_frame_unittest.cc | 16 +++++------ .../common_video/include/video_frame_buffer.h | 7 +++++ webrtc/common_video/video_frame.cc | 2 +- webrtc/common_video/video_frame_buffer.cc | 19 ++++++++++--- webrtc/media/engine/webrtcvideoengine2.cc | 27 +++++-------------- .../codecs/test/videoprocessor.cc | 4 +-- .../utility/quality_scaler_unittest.cc | 18 +++++-------- .../video_processing/video_denoiser.cc | 2 +- webrtc/test/fake_texture_frame.h | 2 +- 9 files changed, 48 insertions(+), 49 deletions(-) diff --git a/webrtc/common_video/i420_video_frame_unittest.cc b/webrtc/common_video/i420_video_frame_unittest.cc index c2037c1b63..406dbd3b9e 100644 --- a/webrtc/common_video/i420_video_frame_unittest.cc +++ b/webrtc/common_video/i420_video_frame_unittest.cc @@ -29,7 +29,7 @@ int ExpectedSize(int plane_stride, int image_height, PlaneType type) { rtc::scoped_refptr CreateGradient(int width, int height) { rtc::scoped_refptr buffer( - new rtc::RefCountedObject(width, height)); + I420Buffer::Create(width, height)); // Initialize with gradient, Y = 128(x/w + y/h), U = 256 x/w, V = 256 y/h for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { @@ -296,7 +296,7 @@ TEST(TestVideoFrame, TextureInitialValues) { TEST(TestI420FrameBuffer, Copy) { rtc::scoped_refptr buf1( - new rtc::RefCountedObject(20, 10)); + I420Buffer::Create(20, 10)); memset(buf1->MutableDataY(), 1, 200); memset(buf1->MutableDataU(), 2, 50); memset(buf1->MutableDataV(), 3, 50); @@ -309,7 +309,7 @@ TEST(TestI420FrameBuffer, Scale) { // Pure scaling, no cropping. rtc::scoped_refptr scaled_buffer( - new rtc::RefCountedObject(150, 75)); + I420Buffer::Create(150, 75)); scaled_buffer->ScaleFrom(buf); CheckCrop(scaled_buffer, 0.0, 0.0, 1.0, 1.0); @@ -320,7 +320,7 @@ TEST(TestI420FrameBuffer, CropXCenter) { // Pure center cropping, no scaling. rtc::scoped_refptr scaled_buffer( - new rtc::RefCountedObject(100, 100)); + I420Buffer::Create(100, 100)); scaled_buffer->CropAndScaleFrom(buf, 50, 0, 100, 100); CheckCrop(scaled_buffer, 0.25, 0.0, 0.5, 1.0); @@ -331,7 +331,7 @@ TEST(TestI420FrameBuffer, CropXNotCenter) { // Non-center cropping, no scaling. rtc::scoped_refptr scaled_buffer( - new rtc::RefCountedObject(100, 100)); + I420Buffer::Create(100, 100)); scaled_buffer->CropAndScaleFrom(buf, 25, 0, 100, 100); CheckCrop(scaled_buffer, 0.125, 0.0, 0.5, 1.0); @@ -342,7 +342,7 @@ TEST(TestI420FrameBuffer, CropYCenter) { // Pure center cropping, no scaling. rtc::scoped_refptr scaled_buffer( - new rtc::RefCountedObject(100, 100)); + I420Buffer::Create(100, 100)); scaled_buffer->CropAndScaleFrom(buf, 0, 50, 100, 100); CheckCrop(scaled_buffer, 0.0, 0.25, 1.0, 0.5); @@ -353,7 +353,7 @@ TEST(TestI420FrameBuffer, CropYNotCenter) { // Non-center cropping, no scaling. rtc::scoped_refptr scaled_buffer( - new rtc::RefCountedObject(100, 100)); + I420Buffer::Create(100, 100)); scaled_buffer->CropAndScaleFrom(buf, 0, 25, 100, 100); CheckCrop(scaled_buffer, 0.0, 0.125, 1.0, 0.5); @@ -364,7 +364,7 @@ TEST(TestI420FrameBuffer, CropAndScale16x9) { // Center crop to 640 x 360 (16/9 aspect), then scale down by 2. rtc::scoped_refptr scaled_buffer( - new rtc::RefCountedObject(320, 180)); + I420Buffer::Create(320, 180)); scaled_buffer->CropAndScaleFrom(buf); CheckCrop(scaled_buffer, 0.0, 0.125, 1.0, 0.75); diff --git a/webrtc/common_video/include/video_frame_buffer.h b/webrtc/common_video/include/video_frame_buffer.h index dfffb9a0d6..cf314fe932 100644 --- a/webrtc/common_video/include/video_frame_buffer.h +++ b/webrtc/common_video/include/video_frame_buffer.h @@ -88,6 +88,13 @@ class I420Buffer : public VideoFrameBuffer { I420Buffer(int width, int height); I420Buffer(int width, int height, int stride_y, int stride_u, int stride_v); + static rtc::scoped_refptr Create(int width, int height); + static rtc::scoped_refptr Create(int width, + int height, + int stride_y, + int stride_u, + int stride_v); + // Sets all three planes to all zeros. Used to work around for // quirks in memory checkers // (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and diff --git a/webrtc/common_video/video_frame.cc b/webrtc/common_video/video_frame.cc index 463e8ed442..b1721b4bc4 100644 --- a/webrtc/common_video/video_frame.cc +++ b/webrtc/common_video/video_frame.cc @@ -61,7 +61,7 @@ void VideoFrame::CreateEmptyFrame(int width, rotation_ = kVideoRotation_0; // Allocate a new buffer. - video_frame_buffer_ = new rtc::RefCountedObject( + video_frame_buffer_ = I420Buffer::Create( width, height, stride_y, stride_u, stride_v); } diff --git a/webrtc/common_video/video_frame_buffer.cc b/webrtc/common_video/video_frame_buffer.cc index 14e19dc0e3..2875c661c8 100644 --- a/webrtc/common_video/video_frame_buffer.cc +++ b/webrtc/common_video/video_frame_buffer.cc @@ -134,6 +134,19 @@ I420Buffer::I420Buffer(int width, I420Buffer::~I420Buffer() { } +rtc::scoped_refptr I420Buffer::Create(int width, int height) { + return new rtc::RefCountedObject(width, height); +} + +rtc::scoped_refptr I420Buffer::Create(int width, + int height, + int stride_y, + int stride_u, + int stride_v) { + return new rtc::RefCountedObject( + width, height, stride_y, stride_u, stride_v); +} + void I420Buffer::InitializeData() { memset(data_.get(), 0, I420DataSize(height_, stride_y_, stride_u_, stride_v_)); @@ -190,8 +203,7 @@ rtc::scoped_refptr I420Buffer::Copy( const rtc::scoped_refptr& source) { int width = source->width(); int height = source->height(); - rtc::scoped_refptr target = - new rtc::RefCountedObject(width, height); + rtc::scoped_refptr target = I420Buffer::Create(width, height); RTC_CHECK(libyuv::I420Copy(source->DataY(), source->StrideY(), source->DataU(), source->StrideU(), source->DataV(), source->StrideV(), @@ -273,8 +285,7 @@ rtc::scoped_refptr I420Buffer::CopyKeepStride( int stride_u = source->StrideU(); int stride_v = source->StrideV(); rtc::scoped_refptr target = - new rtc::RefCountedObject( - width, height, stride_y, stride_u, stride_v); + I420Buffer::Create(width, height, stride_y, stride_u, stride_v); RTC_CHECK(libyuv::I420Copy(source->DataY(), stride_y, source->DataU(), stride_u, source->DataV(), stride_v, diff --git a/webrtc/media/engine/webrtcvideoengine2.cc b/webrtc/media/engine/webrtcvideoengine2.cc index 432c025e30..3857db9587 100644 --- a/webrtc/media/engine/webrtcvideoengine2.cc +++ b/webrtc/media/engine/webrtcvideoengine2.cc @@ -1577,24 +1577,6 @@ WebRtcVideoChannel2::WebRtcVideoSendStream::~WebRtcVideoSendStream() { DestroyVideoEncoder(&allocated_encoder_); } -static webrtc::VideoFrame CreateBlackFrame(int width, - int height, - int64_t render_time_ms_, - webrtc::VideoRotation rotation) { - webrtc::VideoFrame frame; - frame.CreateEmptyFrame(width, height, width, (width + 1) / 2, - (width + 1) / 2); - memset(frame.video_frame_buffer()->MutableDataY(), 16, - frame.allocated_size(webrtc::kYPlane)); - memset(frame.video_frame_buffer()->MutableDataU(), 128, - frame.allocated_size(webrtc::kUPlane)); - memset(frame.video_frame_buffer()->MutableDataV(), 128, - frame.allocated_size(webrtc::kVPlane)); - frame.set_rotation(rotation); - frame.set_render_time_ms(render_time_ms_); - return frame; -} - void WebRtcVideoChannel2::WebRtcVideoSendStream::OnFrame( const VideoFrame& frame) { TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::OnFrame"); @@ -1691,8 +1673,13 @@ bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetVideoSend( // necessary to give this black frame a larger timestamp than the // previous one. last_frame_timestamp_ms_ += 1; - stream_->Input()->IncomingCapturedFrame(CreateBlackFrame( - last_frame_info_.width, last_frame_info_.height, + rtc::scoped_refptr black_buffer( + webrtc::I420Buffer::Create(last_frame_info_.width, + last_frame_info_.height)); + black_buffer->SetToBlack(); + + stream_->Input()->IncomingCapturedFrame(webrtc::VideoFrame( + black_buffer, 0 /* timestamp (90 kHz) */, last_frame_timestamp_ms_, last_frame_info_.rotation)); } source_ = source; diff --git a/webrtc/modules/video_coding/codecs/test/videoprocessor.cc b/webrtc/modules/video_coding/codecs/test/videoprocessor.cc index 1f7d738dc5..e969fb7fb9 100644 --- a/webrtc/modules/video_coding/codecs/test/videoprocessor.cc +++ b/webrtc/modules/video_coding/codecs/test/videoprocessor.cc @@ -335,8 +335,8 @@ void VideoProcessorImpl::FrameDecoded(const VideoFrame& image) { if (image.width() != config_.codec_settings->width || image.height() != config_.codec_settings->height) { rtc::scoped_refptr up_image( - new rtc::RefCountedObject(config_.codec_settings->width, - config_.codec_settings->height)); + I420Buffer::Create(config_.codec_settings->width, + config_.codec_settings->height)); // Should be the same aspect ratio, no cropping needed. up_image->ScaleFrom(image.video_frame_buffer()); diff --git a/webrtc/modules/video_coding/utility/quality_scaler_unittest.cc b/webrtc/modules/video_coding/utility/quality_scaler_unittest.cc index abe44ffa0d..27ee25cd94 100644 --- a/webrtc/modules/video_coding/utility/quality_scaler_unittest.cc +++ b/webrtc/modules/video_coding/utility/quality_scaler_unittest.cc @@ -42,8 +42,7 @@ class QualityScalerTest : public ::testing::Test { }; QualityScalerTest() { - input_frame_ = rtc::scoped_refptr( - new rtc::RefCountedObject(kWidth, kHeight)); + input_frame_ = I420Buffer::Create(kWidth, kHeight); qs_.Init(kLowQpThreshold, kHighQp, 0, 0, 0, kFramerate); qs_.OnEncodeFrame(input_frame_->width(), input_frame_->height()); } @@ -219,14 +218,12 @@ TEST_F(QualityScalerTest, ContinuouslyDownscalesOddResolutionsByHalfDimensionsAndBackUp) { const int kOddWidth = 517; const int kOddHeight = 1239; - input_frame_ = rtc::scoped_refptr( - new rtc::RefCountedObject(kOddWidth, kOddHeight)); + input_frame_ = I420Buffer::Create(kOddWidth, kOddHeight); ContinuouslyDownscalesByHalfDimensionsAndBackUp(); } void QualityScalerTest::DoesNotDownscaleFrameDimensions(int width, int height) { - input_frame_ = rtc::scoped_refptr( - new rtc::RefCountedObject(width, height)); + input_frame_ = I420Buffer::Create(width, height); for (int i = 0; i < kFramerate * kNumSeconds; ++i) { qs_.ReportDroppedFrame(); @@ -262,8 +259,7 @@ TEST_F(QualityScalerTest, DownscaleToVgaOnLowInitialBitrate) { static const int kWidth720p = 1280; static const int kHeight720p = 720; static const int kInitialBitrateKbps = 300; - input_frame_ = rtc::scoped_refptr( - new rtc::RefCountedObject(kWidth720p, kHeight720p)); + input_frame_ = I420Buffer::Create(kWidth720p, kHeight720p); qs_.Init(kLowQpThreshold, kDisabledBadQpThreshold, kInitialBitrateKbps, kWidth720p, kHeight720p, kFramerate); qs_.OnEncodeFrame(input_frame_->width(), input_frame_->height()); @@ -277,8 +273,7 @@ TEST_F(QualityScalerTest, DownscaleToQvgaOnLowerInitialBitrate) { static const int kWidth720p = 1280; static const int kHeight720p = 720; static const int kInitialBitrateKbps = 200; - input_frame_ = rtc::scoped_refptr( - new rtc::RefCountedObject(kWidth720p, kHeight720p)); + input_frame_ = I420Buffer::Create(kWidth720p, kHeight720p); qs_.Init(kLowQpThreshold, kDisabledBadQpThreshold, kInitialBitrateKbps, kWidth720p, kHeight720p, kFramerate); qs_.OnEncodeFrame(input_frame_->width(), input_frame_->height()); @@ -353,8 +348,7 @@ void QualityScalerTest::DownscaleEndsAt(int input_width, int end_height) { // Create a frame with 2x expected end width/height to verify that we can // scale down to expected end width/height. - input_frame_ = rtc::scoped_refptr( - new rtc::RefCountedObject(input_width, input_height)); + input_frame_ = I420Buffer::Create(input_width, input_height); int last_width = input_width; int last_height = input_height; diff --git a/webrtc/modules/video_processing/video_denoiser.cc b/webrtc/modules/video_processing/video_denoiser.cc index e51f628588..313a850b92 100644 --- a/webrtc/modules/video_processing/video_denoiser.cc +++ b/webrtc/modules/video_processing/video_denoiser.cc @@ -86,7 +86,7 @@ void VideoDenoiser::DenoiserReset( stride_v_ = frame->StrideV(); // Allocate an empty buffer for denoised_frame_prev. - *denoised_frame_prev = new rtc::RefCountedObject( + *denoised_frame_prev = I420Buffer::Create( width_, height_, stride_y_, stride_u_, stride_v_); // Allocate and initialize denoised_frame with key frame. *denoised_frame = I420Buffer::CopyKeepStride(frame); diff --git a/webrtc/test/fake_texture_frame.h b/webrtc/test/fake_texture_frame.h index 15b70d8f7b..23b2954460 100644 --- a/webrtc/test/fake_texture_frame.h +++ b/webrtc/test/fake_texture_frame.h @@ -39,7 +39,7 @@ class FakeNativeHandleBuffer : public NativeHandleBuffer { private: rtc::scoped_refptr NativeToI420Buffer() override { rtc::scoped_refptr buffer( - new rtc::RefCountedObject(width_, height_)); + I420Buffer::Create(width_, height_)); int half_height = (height_ + 1) / 2; int half_width = (width_ + 1) / 2; memset(buffer->MutableDataY(), 0, height_ * width_);