blob: ff120a6f4c2a1515434023713de598cae5dec3d1 [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
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000047 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
48 // reference to the video buffer also retained by |videoFrame|.
nisse66910702016-06-22 08:47:49 -070049 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
50 // webrtc::VideoFrame merge. Instead, pass video_frame_buffer() and timestamps
51 // to the constructor.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070052 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000053
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000054 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000055 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000056
57 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000058 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000059
nisse74c10b52016-09-05 00:51:16 -070060 // System monotonic clock, same timebase as rtc::TimeMicros().
61 int64_t timestamp_us() const { return timestamp_us_; }
62 void set_timestamp_us(int64_t timestamp_us) {
63 timestamp_us_ = timestamp_us;
64 }
65
nisse66910702016-06-22 08:47:49 -070066 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
nisse74c10b52016-09-05 00:51:16 -070067 // merge, timestamps other than timestamp_us will likely be
68 // deprecated.
nisse66910702016-06-22 08:47:49 -070069
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000070 // Set frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070071 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000072
73 // Get frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070074 uint32_t timestamp() const { return timestamp_rtp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000075
nisse09347852016-10-19 00:30:30 -070076 // For now, transport_frame_id and rtp timestamp are the same.
77 // TODO(nisse): Must be handled differently for QUIC.
78 uint32_t transport_frame_id() const { return timestamp(); }
79
nisse66910702016-06-22 08:47:49 -070080 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000081 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000082 ntp_time_ms_ = ntp_time_ms;
83 }
84
nisse66910702016-06-22 08:47:49 -070085 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000086 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000087
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000088 // Naming convention for Coordination of Video Orientation. Please see
89 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
90 //
91 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
92 //
93 // "not pending" = a frame that has a VideoRotation == 0.
94 //
95 // "apply rotation" = modify a frame from being "pending" to being "not
96 // pending" rotation (a no-op for "unrotated").
97 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000098 VideoRotation rotation() const { return rotation_; }
99 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000100 rotation_ = rotation;
101 }
102
nisse66910702016-06-22 08:47:49 -0700103 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000104 void set_render_time_ms(int64_t render_time_ms) {
nisse74c10b52016-09-05 00:51:16 -0700105 set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000106 }
107
nisse66910702016-06-22 08:47:49 -0700108 // Get render time in milliseconds.
nisse74c10b52016-09-05 00:51:16 -0700109 int64_t render_time_ms() const {
110 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
111 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000112
nisse66910702016-06-22 08:47:49 -0700113 // Return true if and only if video_frame_buffer() is null. Which is possible
114 // only if the object was default-constructed.
115 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
116 // webrtc::VideoFrame merge. The intention is that video_frame_buffer() never
117 // should return nullptr. To handle potentially uninitialized or non-existent
118 // frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be
119 // replaced by video_frame_buffer() == nullptr.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000120 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000121
nisse26acec42016-04-15 03:43:39 -0700122 // Return the underlying buffer. Never nullptr for a properly
123 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700124 // Creating a new reference breaks the HasOneRef and IsMutable
125 // logic. So return a const ref to our reference.
126 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
127 const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000128
skvlad3abb7642016-06-16 12:08:03 -0700129 // Return true if the frame is stored in a texture.
perkjfa10b552016-10-02 23:45:26 -0700130 bool is_texture() const {
skvlad3abb7642016-06-16 12:08:03 -0700131 return video_frame_buffer() &&
132 video_frame_buffer()->native_handle() != nullptr;
133 }
134
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000135 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000136 // An opaque reference counted handle that stores the pixel data.
137 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisse74c10b52016-09-05 00:51:16 -0700138 uint32_t timestamp_rtp_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000139 int64_t ntp_time_ms_;
nisse74c10b52016-09-05 00:51:16 -0700140 int64_t timestamp_us_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000141 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000142};
143
asaperssonda535c42015-10-19 23:32:41 -0700144
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000145// TODO(pbos): Rename EncodedFrame and reformat this class' members.
146class EncodedImage {
147 public:
hbosd6648362016-01-21 05:43:11 -0800148 static const size_t kBufferPaddingBytesH264;
149
150 // Some decoders require encoded image buffers to be padded with a small
151 // number of additional bytes (due to over-reading byte readers).
152 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
153
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000154 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200155
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000156 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000157 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000158
asapersson4306fc72015-10-19 00:35:21 -0700159 struct AdaptReason {
160 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700161 : quality_resolution_downscales(-1),
162 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700163
164 int quality_resolution_downscales; // Number of times this frame is down
165 // scaled in resolution due to quality.
166 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700167 int bw_resolutions_disabled; // Number of resolutions that are not sent
168 // due to bandwidth for this frame.
169 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700170 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000171 uint32_t _encodedWidth = 0;
172 uint32_t _encodedHeight = 0;
173 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000174 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000175 int64_t ntp_time_ms_ = 0;
176 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200177 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000178 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000179 size_t _length;
180 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200181 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000182 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700183 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700184 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700185
186 // When an application indicates non-zero values here, it is taken as an
187 // indication that all future frames will be constrained with those limits
188 // until the application indicates a change again.
189 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000190};
191
192} // namespace webrtc
193#endif // WEBRTC_VIDEO_FRAME_H_