webrtc_m130/webrtc/video_frame.h

228 lines
8.7 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_VIDEO_FRAME_H_
#define WEBRTC_VIDEO_FRAME_H_
Revert 8599 "Revert 8580 "Unify underlying frame buffer in I420VideoFrame and..."" It's possible to build Chrome on Windows with this patch now. BUG=1128 > This is unfortunately causing build problems in Chrome on Windows. >> Unify underlying frame buffer in I420VideoFrame and WebRtcVideoFrame >> >> Currently, I420VideoFrame uses three webrtc::Plane to store pixel data, and WebRtcVideoFrame uses WebRtcVideoFrame::FrameBuffer/webrtc::VideoFrame. The two subclasses WebRtcTextureVideoFrame and TextureVideoFrame use a NativeHandle to store pixel data, and there is also a class WebRtcVideoRenderFrame that wraps an I420VideoFrame. >> >> This CL replaces these classes with a new interface VideoFrameBuffer that provides the common functionality. This makes it possible to remove deep frame copies between cricket::VideoFrame and I420VideoFrame. >> >> Some additional minor changes are: >> * Disallow creation of 0x0 texture frames. >> * Remove the half-implemented ref count functions in I420VideoFrame. >> * Remove the Alias functionality in WebRtcVideoFrame >> >> The final goal is to eliminate all frame copies, but to limit the scope of this CL, some planned changes are postponed to follow-up CL:s (see planned changes in https://webrtc-codereview.appspot.com/38879004, or https://docs.google.com/document/d/1bxoJZNmlo-Z9GnQwIaWpEG6hDlL_W-bzka8Zb_K2NbA/preview). Specifically, this CL: >> * Keeps empty subclasses WebRtcTextureVideoFrame and TextureVideoFrame, and just delegates the construction to the superclass. >> * Keeps the deep copies from cricket::VideoFrame to I420VideoFrame. >> >> BUG=1128 >> R=mflodman@webrtc.org, pbos@webrtc.org, perkj@webrtc.org, tommi@webrtc.org >> >> Review URL: https://webrtc-codereview.appspot.com/42469004 R=pbos@webrtc.org TBR=mflodman, pbos, perkj, tommi Review URL: https://webrtc-codereview.appspot.com/45489004 Cr-Commit-Position: refs/heads/master@{#8616} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8616 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-05 14:03:08 +00:00
#include "webrtc/base/scoped_ref_ptr.h"
#include "webrtc/common_types.h"
#include "webrtc/common_video/include/video_frame_buffer.h"
#include "webrtc/common_video/rotation.h"
#include "webrtc/typedefs.h"
namespace webrtc {
// TODO(nisse): This class duplicates cricket::VideoFrame. There's
// ongoing work to merge the classes. See
// https://bugs.chromium.org/p/webrtc/issues/detail?id=5682.
class VideoFrame {
public:
// TODO(nisse): Deprecated. Using the default constructor violates the
// reasonable assumption that video_frame_buffer() returns a valid buffer.
VideoFrame();
// Preferred constructor.
VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
uint32_t timestamp,
int64_t render_time_ms,
VideoRotation rotation);
// CreateEmptyFrame: Sets frame dimensions and allocates buffers based
// on set dimensions - height and plane stride.
// If required size is bigger than the allocated one, new buffers of adequate
// size will be allocated.
// TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
// webrtc::VideoFrame merge. If you need to write into the frame, create a
// VideoFrameBuffer of the desired size, e.g, using I420Buffer::Create and
// write to that. And if you need to wrap it into a VideoFrame, pass it to the
// constructor.
void CreateEmptyFrame(int width,
int height,
int stride_y,
int stride_u,
int stride_v);
// CreateFrame: Sets the frame's members and buffers. If required size is
// bigger than allocated one, new buffers of adequate size will be allocated.
// TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
// webrtc::VideoFrame merge. Instead, create a VideoFrameBuffer and pass to
// the constructor. E.g, use I420Buffer::Copy(WrappedI420Buffer(...)).
void CreateFrame(const uint8_t* buffer_y,
const uint8_t* buffer_u,
const uint8_t* buffer_v,
int width,
int height,
int stride_y,
int stride_u,
int stride_v,
VideoRotation rotation);
// CreateFrame: Sets the frame's members and buffers. If required size is
// bigger than allocated one, new buffers of adequate size will be allocated.
// |buffer| must be a packed I420 buffer.
// TODO(nisse): Deprecated, see above method for advice.
void CreateFrame(const uint8_t* buffer,
int width,
int height,
VideoRotation rotation);
// Deep copy frame: If required size is bigger than allocated one, new
// buffers of adequate size will be allocated.
// TODO(nisse): Should be deleted in the cricket::VideoFrame and
// webrtc::VideoFrame merge. Instead, use I420Buffer::Copy to make a copy of
// the pixel data, and use the constructor to wrap it into a VideoFrame.
void CopyFrame(const VideoFrame& videoFrame);
// Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
// reference to the video buffer also retained by |videoFrame|.
// TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
// webrtc::VideoFrame merge. Instead, pass video_frame_buffer() and timestamps
// to the constructor.
void ShallowCopy(const VideoFrame& videoFrame);
// Get allocated size per plane.
// TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
// webrtc::VideoFrame merge. When used with memset, consider using
// libyuv::I420Rect instead.
int allocated_size(PlaneType type) const;
// Get frame width.
int width() const;
// Get frame height.
int height() const;
// TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
// merge, we'll have methods timestamp_us and set_timestamp_us, all
// other frame timestamps will likely be deprecated.
// Set frame timestamp (90kHz).
void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
// Get frame timestamp (90kHz).
uint32_t timestamp() const { return timestamp_; }
// Set capture ntp time in milliseconds.
void set_ntp_time_ms(int64_t ntp_time_ms) {
ntp_time_ms_ = ntp_time_ms;
}
// Get capture ntp time in milliseconds.
int64_t ntp_time_ms() const { return ntp_time_ms_; }
// Naming convention for Coordination of Video Orientation. Please see
// http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
//
// "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
//
// "not pending" = a frame that has a VideoRotation == 0.
//
// "apply rotation" = modify a frame from being "pending" to being "not
// pending" rotation (a no-op for "unrotated").
//
VideoRotation rotation() const { return rotation_; }
void set_rotation(VideoRotation rotation) {
rotation_ = rotation;
}
// Set render time in milliseconds.
void set_render_time_ms(int64_t render_time_ms) {
render_time_ms_ = render_time_ms;
}
// Get render time in milliseconds.
int64_t render_time_ms() const { return render_time_ms_; }
// Return true if and only if video_frame_buffer() is null. Which is possible
// only if the object was default-constructed.
// TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
// webrtc::VideoFrame merge. The intention is that video_frame_buffer() never
// should return nullptr. To handle potentially uninitialized or non-existent
// frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be
// replaced by video_frame_buffer() == nullptr.
bool IsZeroSize() const;
// Return the underlying buffer. Never nullptr for a properly
// initialized VideoFrame.
Reland of Delete webrtc::VideoFrame methods buffer and stride. (patchset #1 id:1 of https://codereview.webrtc.org/1983583002/ ) Reason for revert: Should work after cl https://codereview.webrtc.org/1985693002/ is landed, which initializes the frames used by FakeWebRtcVideoCaptureModule. So intend to reland after that, with no changes. Original issue's description: > Revert of Delete webrtc::VideoFrame methods buffer and stride. (patchset #2 id:290001 of https://codereview.webrtc.org/1963413004/ ) > > Reason for revert: > Speculative revert to see if failures on the DrMemory bot are related to this cl. See e.g. here: > https://build.chromium.org/p/client.webrtc/builders/Win%20DrMemory%20Full/builds/4243 > > UNINITIALIZED READ: reading 0x04980040-0x04980060 32 byte(s) within 0x04980040-0x04980060 > # 0 CopyRow_AVX > # 1 CopyPlane > # 2 I420Copy > # 3 webrtc::ExtractBuffer > # 4 cricket::WebRtcVideoCapturer::SignalFrameCapturedOnStartThread > # 5 cricket::WebRtcVideoCapturer::OnIncomingCapturedFrame > # 6 FakeWebRtcVideoCaptureModule::SendFrame > # 7 WebRtcVideoCapturerTest_TestCaptureVcm_Test::TestBody > # 8 testing::internal::HandleSehExceptionsInMethodIfSupported<> > > Original issue's description: > > Reland of Delete webrtc::VideoFrame methods buffer and stride. (patchset #1 id:1 of https://codereview.webrtc.org/1935443002/ ) > > > > Reason for revert: > > I plan to reland this change in a week or two, after downstream users are updated. > > > > Original issue's description: > > > Revert of Delete webrtc::VideoFrame methods buffer and stride. (patchset #14 id:250001 of https://codereview.webrtc.org/1900673002/ ) > > > > > > Reason for revert: > > > Breaks chrome FYI bots. > > > > > > Original issue's description: > > > > Delete webrtc::VideoFrame methods buffer and stride. > > > > > > > > To make the HasOneRef/IsMutable hack work, also had to change the > > > > video_frame_buffer method to return a const ref to a scoped_ref_ptr, > > > > to not imply an AddRef. > > > > > > > > BUG=webrtc:5682 > > > > > > TBR=perkj@webrtc.org,magjed@webrtc.org,pbos@webrtc.org,pthatcher@webrtc.org,stefan@webrtc.org > > > # Skipping CQ checks because original CL landed less than 1 days ago. > > > NOPRESUBMIT=true > > > NOTREECHECKS=true > > > NOTRY=true > > > BUG=webrtc:5682 > > > > > > Committed: https://crrev.com/5b3c443d301f2c2f18dac5b02652c08b91ea3828 > > > Cr-Commit-Position: refs/heads/master@{#12558} > > > > TBR=perkj@webrtc.org,magjed@webrtc.org,pbos@webrtc.org,pthatcher@webrtc.org,stefan@webrtc.org > > # Not skipping CQ checks because original CL landed more than 1 days ago. > > BUG=webrtc:5682 > > > > Committed: https://crrev.com/d0dc66e0ea30c8614001e425a4ae0aa7dd56c2a7 > > Cr-Commit-Position: refs/heads/master@{#12721} > > TBR=perkj@webrtc.org,magjed@webrtc.org,pbos@webrtc.org,pthatcher@webrtc.org,stefan@webrtc.org,nisse@webrtc.org > # Skipping CQ checks because original CL landed less than 1 days ago. > NOPRESUBMIT=true > NOTREECHECKS=true > NOTRY=true > BUG=webrtc:5682 > > Committed: https://crrev.com/d49c30cd2fe442f2b5b4ecec8d5cbaa430464725 > Cr-Commit-Position: refs/heads/master@{#12745} TBR=perkj@webrtc.org,magjed@webrtc.org,pbos@webrtc.org,pthatcher@webrtc.org,stefan@webrtc.org,tommi@webrtc.org # Not skipping CQ checks because original CL landed more than 1 days ago. BUG=webrtc:5682 Review-Url: https://codereview.webrtc.org/1979193003 Cr-Commit-Position: refs/heads/master@{#12773}
2016-05-17 04:05:47 -07:00
// Creating a new reference breaks the HasOneRef and IsMutable
// logic. So return a const ref to our reference.
const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
const;
Avoid unnecessary HW video encoder reconfiguration This change reduces the number of times the Android hardware video encoder is reconfigured when making an outgoing call. With this change, the encoder should only be initialized once as opposed to the ~3 times it happens currently. Before the fix, the following sequence of events caused the extra reconfigurations: 1. After the SetLocalDescription call, the WebRtcVideoSendStream is created. All frames from the camera are dropped until the corresponding VideoSendStream is created. 2. SetRemoteDescription() triggers the VideoSendStream creation. At this point, the encoder is configured for the first time, with the frame dimensions set to a low resolution default (176x144). 3. When the first video frame is received from the camera after the VideoSendStreamIsCreated, the encoder is reconfigured to the correct dimensions. If we are using the Android hardware encoder, the default configuration is set to encode from a memory buffer (use_surface=false). 4. When the frame is passed down to the encoder in androidmediaencoder_jni.cc EncodeOnCodecThread(), it may be stored in a texture instead of a memory buffer. In this case, yet another reconfiguration takes place to enable encoding from a texture. 5. Even if the resolution and texture flag were known at the start of the call, there would be a reconfiguration involved if the camera is rotated (such as when making a call from a phone in portrait orientation). The reason for that is that at construction time, WebRtcVideoEngine2 sets the VideoSinkWants structure parameter to request frames rotated by the source; the early frames will then arrive in portrait resolution. When the remote description is finally set, if the rotation RTP extension is supported by the remote receiver, the source is asked to provide non-rotated frames. The very next frame will then arrive in landscape resolution with a non-zero rotation value to be applied by the receiver. Since the encoder was configured with the last (portrait) frame size, it's going to need to be reconfigured again. The fix makes the following changes: 1. WebRtcVideoSendStream::OnFrame() now caches the last seen frame dimensions, and whether the frame was stored in a texture. 2. When the encoder is configured the first time (WebRtcVideoSendStream::SetCodec()) - the last seen frame dimensions are used instead of the default dimensions. 3. A flag that indicates if encoding is to be done from a texture has been added to the webrtc::VideoStream and webrtc::VideoCodec structs, and it's been wired up to be passed down all the way to the JNI code in androidmediaencoder_jni.cc. 4. MediaCodecVideoEncoder::InitEncode is now reading the is_surface flag from the VideoCodec structure instead of guessing the default as false. This way we end up with the correct encoder configuration the first time around. 5. WebRtcVideoSendStream now takes an optimistic guess and requests non- rotated frames when the supported RtpExtensions list is not available. This makes the "early" frames arrive non-rotated, and the cached dimensions will be correct for the common case when the rotation extension is supported. If the other side is an older endpoint which does not support rotation, the encoder will have to be reconfigured - but it's better to penalize the uncommon case rather than the common one. Review-Url: https://codereview.webrtc.org/2067103002 Cr-Commit-Position: refs/heads/master@{#13173}
2016-06-16 12:08:03 -07:00
// Return true if the frame is stored in a texture.
bool is_texture() {
return video_frame_buffer() &&
video_frame_buffer()->native_handle() != nullptr;
}
private:
Revert 8599 "Revert 8580 "Unify underlying frame buffer in I420VideoFrame and..."" It's possible to build Chrome on Windows with this patch now. BUG=1128 > This is unfortunately causing build problems in Chrome on Windows. >> Unify underlying frame buffer in I420VideoFrame and WebRtcVideoFrame >> >> Currently, I420VideoFrame uses three webrtc::Plane to store pixel data, and WebRtcVideoFrame uses WebRtcVideoFrame::FrameBuffer/webrtc::VideoFrame. The two subclasses WebRtcTextureVideoFrame and TextureVideoFrame use a NativeHandle to store pixel data, and there is also a class WebRtcVideoRenderFrame that wraps an I420VideoFrame. >> >> This CL replaces these classes with a new interface VideoFrameBuffer that provides the common functionality. This makes it possible to remove deep frame copies between cricket::VideoFrame and I420VideoFrame. >> >> Some additional minor changes are: >> * Disallow creation of 0x0 texture frames. >> * Remove the half-implemented ref count functions in I420VideoFrame. >> * Remove the Alias functionality in WebRtcVideoFrame >> >> The final goal is to eliminate all frame copies, but to limit the scope of this CL, some planned changes are postponed to follow-up CL:s (see planned changes in https://webrtc-codereview.appspot.com/38879004, or https://docs.google.com/document/d/1bxoJZNmlo-Z9GnQwIaWpEG6hDlL_W-bzka8Zb_K2NbA/preview). Specifically, this CL: >> * Keeps empty subclasses WebRtcTextureVideoFrame and TextureVideoFrame, and just delegates the construction to the superclass. >> * Keeps the deep copies from cricket::VideoFrame to I420VideoFrame. >> >> BUG=1128 >> R=mflodman@webrtc.org, pbos@webrtc.org, perkj@webrtc.org, tommi@webrtc.org >> >> Review URL: https://webrtc-codereview.appspot.com/42469004 R=pbos@webrtc.org TBR=mflodman, pbos, perkj, tommi Review URL: https://webrtc-codereview.appspot.com/45489004 Cr-Commit-Position: refs/heads/master@{#8616} git-svn-id: http://webrtc.googlecode.com/svn/trunk@8616 4adac7df-926f-26a2-2b94-8c16560cd09d
2015-03-05 14:03:08 +00:00
// An opaque reference counted handle that stores the pixel data.
rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
uint32_t timestamp_;
int64_t ntp_time_ms_;
int64_t render_time_ms_;
VideoRotation rotation_;
};
// TODO(pbos): Rename EncodedFrame and reformat this class' members.
class EncodedImage {
public:
static const size_t kBufferPaddingBytesH264;
// Some decoders require encoded image buffers to be padded with a small
// number of additional bytes (due to over-reading byte readers).
static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
EncodedImage() : EncodedImage(nullptr, 0, 0) {}
EncodedImage(uint8_t* buffer, size_t length, size_t size)
: _buffer(buffer), _length(length), _size(size) {}
struct AdaptReason {
AdaptReason()
: quality_resolution_downscales(-1),
bw_resolutions_disabled(-1) {}
int quality_resolution_downscales; // Number of times this frame is down
// scaled in resolution due to quality.
// Or -1 if information is not provided.
int bw_resolutions_disabled; // Number of resolutions that are not sent
// due to bandwidth for this frame.
// Or -1 if information is not provided.
};
uint32_t _encodedWidth = 0;
uint32_t _encodedHeight = 0;
uint32_t _timeStamp = 0;
// NTP time of the capture time in local timebase in milliseconds.
int64_t ntp_time_ms_ = 0;
int64_t capture_time_ms_ = 0;
FrameType _frameType = kVideoFrameDelta;
uint8_t* _buffer;
size_t _length;
size_t _size;
VideoRotation rotation_ = kVideoRotation_0;
bool _completeFrame = false;
AdaptReason adapt_reason_;
int qp_ = -1; // Quantizer value.
// When an application indicates non-zero values here, it is taken as an
// indication that all future frames will be constrained with those limits
// until the application indicates a change again.
PlayoutDelay playout_delay_ = {-1, -1};
};
} // namespace webrtc
#endif // WEBRTC_VIDEO_FRAME_H_