blob: a3057e9be5df72b19928f2dc6f620ab081d5f93b [file] [log] [blame]
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_VIDEO_FRAME_H_
12#define WEBRTC_VIDEO_FRAME_H_
13
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000014#include "webrtc/base/scoped_ref_ptr.h"
nisse74c10b52016-09-05 00:51:16 -070015#include "webrtc/base/timeutils.h"
pbos22993e12015-10-19 02:39:06 -070016#include "webrtc/common_types.h"
kjellander6f8ce062015-11-16 13:52:24 -080017#include "webrtc/common_video/include/video_frame_buffer.h"
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000018#include "webrtc/common_video/rotation.h"
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000019#include "webrtc/typedefs.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000020
21namespace webrtc {
22
nisse66910702016-06-22 08:47:49 -070023// TODO(nisse): This class duplicates cricket::VideoFrame. There's
24// ongoing work to merge the classes. See
25// https://bugs.chromium.org/p/webrtc/issues/detail?id=5682.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070026class VideoFrame {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000027 public:
nisse66910702016-06-22 08:47:49 -070028 // TODO(nisse): Deprecated. Using the default constructor violates the
29 // reasonable assumption that video_frame_buffer() returns a valid buffer.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070030 VideoFrame();
nisse66910702016-06-22 08:47:49 -070031
nisse74c10b52016-09-05 00:51:16 -070032 // TODO(nisse): This constructor is consistent with
33 // cricket::WebRtcVideoFrame. After the class
34 // cricket::WebRtcVideoFrame and its baseclass cricket::VideoFrame
35 // are deleted, we should consider whether or not we want to stick
36 // to this style and deprecate the other constructors.
37 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
38 webrtc::VideoRotation rotation,
39 int64_t timestamp_us);
40
nisse66910702016-06-22 08:47:49 -070041 // Preferred constructor.
nisse74c10b52016-09-05 00:51:16 -070042 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070043 uint32_t timestamp,
44 int64_t render_time_ms,
45 VideoRotation rotation);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000046
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000047 // CreateFrame: Sets the frame's members and buffers. If required size is
48 // bigger than allocated one, new buffers of adequate size will be allocated.
nisse66910702016-06-22 08:47:49 -070049
50 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
51 // webrtc::VideoFrame merge. Instead, create a VideoFrameBuffer and pass to
52 // the constructor. E.g, use I420Buffer::Copy(WrappedI420Buffer(...)).
Niels Möller739fcb92016-02-29 13:11:45 +010053 void CreateFrame(const uint8_t* buffer_y,
54 const uint8_t* buffer_u,
55 const uint8_t* buffer_v,
56 int width,
57 int height,
58 int stride_y,
59 int stride_u,
60 int stride_v,
61 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000062
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000063 // CreateFrame: Sets the frame's members and buffers. If required size is
64 // bigger than allocated one, new buffers of adequate size will be allocated.
65 // |buffer| must be a packed I420 buffer.
nisse66910702016-06-22 08:47:49 -070066
67 // TODO(nisse): Deprecated, see above method for advice.
Niels Möller739fcb92016-02-29 13:11:45 +010068 void CreateFrame(const uint8_t* buffer,
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000069 int width,
70 int height,
71 VideoRotation rotation);
72
ivoc21a18ee2016-10-06 06:29:30 -070073 // Deep copy frame: If required size is bigger than allocated one, new
74 // buffers of adequate size will be allocated.
75 // TODO(nisse): Should be deleted in the cricket::VideoFrame and
76 // webrtc::VideoFrame merge. Instead, use I420Buffer::Copy to make a copy of
77 // the pixel data, and use the constructor to wrap it into a VideoFrame.
78 void CopyFrame(const VideoFrame& videoFrame);
79
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000080 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
81 // reference to the video buffer also retained by |videoFrame|.
nisse66910702016-06-22 08:47:49 -070082 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
83 // webrtc::VideoFrame merge. Instead, pass video_frame_buffer() and timestamps
84 // to the constructor.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070085 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000086
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000087 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000088 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000089
90 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000091 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000092
nisse74c10b52016-09-05 00:51:16 -070093 // System monotonic clock, same timebase as rtc::TimeMicros().
94 int64_t timestamp_us() const { return timestamp_us_; }
95 void set_timestamp_us(int64_t timestamp_us) {
96 timestamp_us_ = timestamp_us;
97 }
98
nisse66910702016-06-22 08:47:49 -070099 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
nisse74c10b52016-09-05 00:51:16 -0700100 // merge, timestamps other than timestamp_us will likely be
101 // deprecated.
nisse66910702016-06-22 08:47:49 -0700102
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000103 // Set frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -0700104 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000105
106 // Get frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -0700107 uint32_t timestamp() const { return timestamp_rtp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000108
nisse09347852016-10-19 00:30:30 -0700109 // For now, transport_frame_id and rtp timestamp are the same.
110 // TODO(nisse): Must be handled differently for QUIC.
111 uint32_t transport_frame_id() const { return timestamp(); }
112
nisse66910702016-06-22 08:47:49 -0700113 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000114 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000115 ntp_time_ms_ = ntp_time_ms;
116 }
117
nisse66910702016-06-22 08:47:49 -0700118 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000119 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000120
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000121 // Naming convention for Coordination of Video Orientation. Please see
122 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
123 //
124 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
125 //
126 // "not pending" = a frame that has a VideoRotation == 0.
127 //
128 // "apply rotation" = modify a frame from being "pending" to being "not
129 // pending" rotation (a no-op for "unrotated").
130 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000131 VideoRotation rotation() const { return rotation_; }
132 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000133 rotation_ = rotation;
134 }
135
nisse66910702016-06-22 08:47:49 -0700136 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000137 void set_render_time_ms(int64_t render_time_ms) {
nisse74c10b52016-09-05 00:51:16 -0700138 set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000139 }
140
nisse66910702016-06-22 08:47:49 -0700141 // Get render time in milliseconds.
nisse74c10b52016-09-05 00:51:16 -0700142 int64_t render_time_ms() const {
143 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
144 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000145
nisse66910702016-06-22 08:47:49 -0700146 // Return true if and only if video_frame_buffer() is null. Which is possible
147 // only if the object was default-constructed.
148 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
149 // webrtc::VideoFrame merge. The intention is that video_frame_buffer() never
150 // should return nullptr. To handle potentially uninitialized or non-existent
151 // frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be
152 // replaced by video_frame_buffer() == nullptr.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000153 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000154
nisse26acec42016-04-15 03:43:39 -0700155 // Return the underlying buffer. Never nullptr for a properly
156 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700157 // Creating a new reference breaks the HasOneRef and IsMutable
158 // logic. So return a const ref to our reference.
159 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
160 const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000161
skvlad3abb7642016-06-16 12:08:03 -0700162 // Return true if the frame is stored in a texture.
perkjfa10b552016-10-02 23:45:26 -0700163 bool is_texture() const {
skvlad3abb7642016-06-16 12:08:03 -0700164 return video_frame_buffer() &&
165 video_frame_buffer()->native_handle() != nullptr;
166 }
167
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000168 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000169 // An opaque reference counted handle that stores the pixel data.
170 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisse74c10b52016-09-05 00:51:16 -0700171 uint32_t timestamp_rtp_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000172 int64_t ntp_time_ms_;
nisse74c10b52016-09-05 00:51:16 -0700173 int64_t timestamp_us_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000174 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000175};
176
asaperssonda535c42015-10-19 23:32:41 -0700177
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000178// TODO(pbos): Rename EncodedFrame and reformat this class' members.
179class EncodedImage {
180 public:
hbosd6648362016-01-21 05:43:11 -0800181 static const size_t kBufferPaddingBytesH264;
182
183 // Some decoders require encoded image buffers to be padded with a small
184 // number of additional bytes (due to over-reading byte readers).
185 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
186
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000187 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200188
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000189 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000190 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000191
asapersson4306fc72015-10-19 00:35:21 -0700192 struct AdaptReason {
193 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700194 : quality_resolution_downscales(-1),
195 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700196
197 int quality_resolution_downscales; // Number of times this frame is down
198 // scaled in resolution due to quality.
199 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700200 int bw_resolutions_disabled; // Number of resolutions that are not sent
201 // due to bandwidth for this frame.
202 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700203 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000204 uint32_t _encodedWidth = 0;
205 uint32_t _encodedHeight = 0;
206 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000207 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000208 int64_t ntp_time_ms_ = 0;
209 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200210 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000211 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000212 size_t _length;
213 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200214 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000215 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700216 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700217 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700218
219 // When an application indicates non-zero values here, it is taken as an
220 // indication that all future frames will be constrained with those limits
221 // until the application indicates a change again.
222 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000223};
224
225} // namespace webrtc
226#endif // WEBRTC_VIDEO_FRAME_H_