blob: b01555e09288187723a5cf7570aea23be565dfee [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
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000073 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
74 // reference to the video buffer also retained by |videoFrame|.
nisse66910702016-06-22 08:47:49 -070075 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
76 // webrtc::VideoFrame merge. Instead, pass video_frame_buffer() and timestamps
77 // to the constructor.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070078 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000079
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000080 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000081 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000082
83 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000084 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000085
nisse74c10b52016-09-05 00:51:16 -070086 // System monotonic clock, same timebase as rtc::TimeMicros().
87 int64_t timestamp_us() const { return timestamp_us_; }
88 void set_timestamp_us(int64_t timestamp_us) {
89 timestamp_us_ = timestamp_us;
90 }
91
nisse66910702016-06-22 08:47:49 -070092 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
nisse74c10b52016-09-05 00:51:16 -070093 // merge, timestamps other than timestamp_us will likely be
94 // deprecated.
nisse66910702016-06-22 08:47:49 -070095
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000096 // Set frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070097 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098
99 // Get frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -0700100 uint32_t timestamp() const { return timestamp_rtp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000101
nisse66910702016-06-22 08:47:49 -0700102 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000103 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000104 ntp_time_ms_ = ntp_time_ms;
105 }
106
nisse66910702016-06-22 08:47:49 -0700107 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000108 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000109
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000110 // Naming convention for Coordination of Video Orientation. Please see
111 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
112 //
113 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
114 //
115 // "not pending" = a frame that has a VideoRotation == 0.
116 //
117 // "apply rotation" = modify a frame from being "pending" to being "not
118 // pending" rotation (a no-op for "unrotated").
119 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000120 VideoRotation rotation() const { return rotation_; }
121 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000122 rotation_ = rotation;
123 }
124
nisse66910702016-06-22 08:47:49 -0700125 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000126 void set_render_time_ms(int64_t render_time_ms) {
nisse74c10b52016-09-05 00:51:16 -0700127 set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000128 }
129
nisse66910702016-06-22 08:47:49 -0700130 // Get render time in milliseconds.
nisse74c10b52016-09-05 00:51:16 -0700131 int64_t render_time_ms() const {
132 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
133 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000134
nisse66910702016-06-22 08:47:49 -0700135 // Return true if and only if video_frame_buffer() is null. Which is possible
136 // only if the object was default-constructed.
137 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
138 // webrtc::VideoFrame merge. The intention is that video_frame_buffer() never
139 // should return nullptr. To handle potentially uninitialized or non-existent
140 // frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be
141 // replaced by video_frame_buffer() == nullptr.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000142 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000143
nisse26acec42016-04-15 03:43:39 -0700144 // Return the underlying buffer. Never nullptr for a properly
145 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700146 // Creating a new reference breaks the HasOneRef and IsMutable
147 // logic. So return a const ref to our reference.
148 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
149 const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000150
skvlad3abb7642016-06-16 12:08:03 -0700151 // Return true if the frame is stored in a texture.
perkjfa10b552016-10-02 23:45:26 -0700152 bool is_texture() const {
skvlad3abb7642016-06-16 12:08:03 -0700153 return video_frame_buffer() &&
154 video_frame_buffer()->native_handle() != nullptr;
155 }
156
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000157 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000158 // An opaque reference counted handle that stores the pixel data.
159 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisse74c10b52016-09-05 00:51:16 -0700160 uint32_t timestamp_rtp_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000161 int64_t ntp_time_ms_;
nisse74c10b52016-09-05 00:51:16 -0700162 int64_t timestamp_us_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000163 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000164};
165
asaperssonda535c42015-10-19 23:32:41 -0700166
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000167// TODO(pbos): Rename EncodedFrame and reformat this class' members.
168class EncodedImage {
169 public:
hbosd6648362016-01-21 05:43:11 -0800170 static const size_t kBufferPaddingBytesH264;
171
172 // Some decoders require encoded image buffers to be padded with a small
173 // number of additional bytes (due to over-reading byte readers).
174 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
175
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000176 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200177
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000178 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000179 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000180
asapersson4306fc72015-10-19 00:35:21 -0700181 struct AdaptReason {
182 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700183 : quality_resolution_downscales(-1),
184 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700185
186 int quality_resolution_downscales; // Number of times this frame is down
187 // scaled in resolution due to quality.
188 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700189 int bw_resolutions_disabled; // Number of resolutions that are not sent
190 // due to bandwidth for this frame.
191 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700192 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000193 uint32_t _encodedWidth = 0;
194 uint32_t _encodedHeight = 0;
195 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000196 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000197 int64_t ntp_time_ms_ = 0;
198 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200199 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000200 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000201 size_t _length;
202 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200203 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000204 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700205 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700206 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700207
208 // When an application indicates non-zero values here, it is taken as an
209 // indication that all future frames will be constrained with those limits
210 // until the application indicates a change again.
211 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000212};
213
214} // namespace webrtc
215#endif // WEBRTC_VIDEO_FRAME_H_