From bdcf38c89446b1b464a646414f6cd7573a190bd1 Mon Sep 17 00:00:00 2001 From: "magjed@webrtc.org" Date: Fri, 21 Nov 2014 10:53:00 +0000 Subject: [PATCH] cricket::VideoFrame: Refactor ConvertToRgbBuffer into base class There is also an implementation in Chromium that can be removed if/when this lands: content/renderer/media/webrtc/webrtc_video_capturer_adapter.cc R=fbarchard@google.com, pbos@webrtc.org, perkj@webrtc.org Review URL: https://webrtc-codereview.appspot.com/32059004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7728 4adac7df-926f-26a2-2b94-8c16560cd09d --- talk/media/base/videoframe.cc | 20 +++++++++++++++++ talk/media/base/videoframe.h | 2 +- talk/media/webrtc/webrtcvideoengine2.cc | 30 ------------------------- talk/media/webrtc/webrtcvideoframe.cc | 20 +---------------- 4 files changed, 22 insertions(+), 50 deletions(-) diff --git a/talk/media/base/videoframe.cc b/talk/media/base/videoframe.cc index 018d065e83..19ad934db6 100644 --- a/talk/media/base/videoframe.cc +++ b/talk/media/base/videoframe.cc @@ -115,6 +115,26 @@ void VideoFrame::CopyToFrame(VideoFrame* dst) const { dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch()); } +size_t VideoFrame::ConvertToRgbBuffer(uint32 to_fourcc, + uint8* buffer, + size_t size, + int stride_rgb) const { + const size_t needed = std::abs(stride_rgb) * GetHeight(); + if (size < needed) { + LOG(LS_WARNING) << "RGB buffer is not large enough"; + return needed; + } + + if (libyuv::ConvertFromI420(GetYPlane(), GetYPitch(), GetUPlane(), + GetUPitch(), GetVPlane(), GetVPitch(), buffer, + stride_rgb, static_cast(GetWidth()), + static_cast(GetHeight()), to_fourcc)) { + LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; + return 0; // 0 indicates error + } + return needed; +} + // TODO(fbarchard): Handle odd width/height with rounding. void VideoFrame::StretchToPlanes( uint8* dst_y, uint8* dst_u, uint8* dst_v, diff --git a/talk/media/base/videoframe.h b/talk/media/base/videoframe.h index d94e47081c..af4df08749 100644 --- a/talk/media/base/videoframe.h +++ b/talk/media/base/videoframe.h @@ -137,7 +137,7 @@ class VideoFrame { // not (like snprintf). Parameters size and stride_rgb are in units of bytes. // If there is insufficient space, nothing is written. virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, uint8 *buffer, - size_t size, int stride_rgb) const = 0; + size_t size, int stride_rgb) const; // Writes the frame into the given planes, stretched to the given width and // height. The parameter "interpolate" controls whether to interpolate or just diff --git a/talk/media/webrtc/webrtcvideoengine2.cc b/talk/media/webrtc/webrtcvideoengine2.cc index 338a0989b3..608b8073e5 100644 --- a/talk/media/webrtc/webrtcvideoengine2.cc +++ b/talk/media/webrtc/webrtcvideoengine2.cc @@ -665,36 +665,6 @@ class WebRtcVideoRenderFrame : public VideoFrame { return 0; } - // TODO(fbarchard): Refactor into base class and share with LMI - virtual size_t ConvertToRgbBuffer(uint32 to_fourcc, - uint8* buffer, - size_t size, - int stride_rgb) const OVERRIDE { - size_t width = GetWidth(); - size_t height = GetHeight(); - size_t needed = (stride_rgb >= 0 ? stride_rgb : -stride_rgb) * height; - if (size < needed) { - LOG(LS_WARNING) << "RGB buffer is not large enough"; - return needed; - } - - if (libyuv::ConvertFromI420(GetYPlane(), - GetYPitch(), - GetUPlane(), - GetUPitch(), - GetVPlane(), - GetVPitch(), - buffer, - stride_rgb, - static_cast(width), - static_cast(height), - to_fourcc)) { - LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; - return 0; // 0 indicates error - } - return needed; - } - protected: virtual VideoFrame* CreateEmptyFrame(int w, int h, diff --git a/talk/media/webrtc/webrtcvideoframe.cc b/talk/media/webrtc/webrtcvideoframe.cc index 9dbf5a5a85..9986cbe275 100644 --- a/talk/media/webrtc/webrtcvideoframe.cc +++ b/talk/media/webrtc/webrtcvideoframe.cc @@ -246,30 +246,12 @@ size_t WebRtcVideoFrame::CopyToBuffer(uint8* buffer, size_t size) const { return needed; } -// TODO(fbarchard): Refactor into base class and share with lmi size_t WebRtcVideoFrame::ConvertToRgbBuffer(uint32 to_fourcc, uint8* buffer, size_t size, int stride_rgb) const { if (!frame()->Buffer()) { return 0; } - size_t width = frame()->Width(); - size_t height = frame()->Height(); - size_t needed = (stride_rgb >= 0 ? stride_rgb : -stride_rgb) * height; - if (size < needed) { - LOG(LS_WARNING) << "RGB buffer is not large enough"; - return needed; - } - - if (libyuv::ConvertFromI420(GetYPlane(), GetYPitch(), GetUPlane(), - GetUPitch(), GetVPlane(), GetVPitch(), buffer, - stride_rgb, - static_cast(width), - static_cast(height), - to_fourcc)) { - LOG(LS_WARNING) << "RGB type not supported: " << to_fourcc; - return 0; // 0 indicates error - } - return needed; + return VideoFrame::ConvertToRgbBuffer(to_fourcc, buffer, size, stride_rgb); } void WebRtcVideoFrame::Attach(