pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 14 | #include "webrtc/base/scoped_ref_ptr.h" |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 15 | #include "webrtc/base/timeutils.h" |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 16 | #include "webrtc/common_types.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 17 | #include "webrtc/common_video/include/video_frame_buffer.h" |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 18 | #include "webrtc/common_video/rotation.h" |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 23 | // 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-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 26 | class VideoFrame { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 27 | public: |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 28 | // TODO(nisse): Deprecated. Using the default constructor violates the |
| 29 | // reasonable assumption that video_frame_buffer() returns a valid buffer. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 30 | VideoFrame(); |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 31 | |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 32 | // 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 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 41 | // Preferred constructor. |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 42 | VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer, |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 43 | uint32_t timestamp, |
| 44 | int64_t render_time_ms, |
| 45 | VideoRotation rotation); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 46 | |
| 47 | // CreateEmptyFrame: Sets frame dimensions and allocates buffers based |
| 48 | // on set dimensions - height and plane stride. |
| 49 | // If required size is bigger than the allocated one, new buffers of adequate |
| 50 | // size will be allocated. |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 51 | |
| 52 | // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and |
| 53 | // webrtc::VideoFrame merge. If you need to write into the frame, create a |
| 54 | // VideoFrameBuffer of the desired size, e.g, using I420Buffer::Create and |
| 55 | // write to that. And if you need to wrap it into a VideoFrame, pass it to the |
| 56 | // constructor. |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 57 | void CreateEmptyFrame(int width, |
| 58 | int height, |
| 59 | int stride_y, |
| 60 | int stride_u, |
| 61 | int stride_v); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 62 | |
| 63 | // 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. |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 65 | |
| 66 | // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and |
| 67 | // webrtc::VideoFrame merge. Instead, create a VideoFrameBuffer and pass to |
| 68 | // the constructor. E.g, use I420Buffer::Copy(WrappedI420Buffer(...)). |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 69 | void CreateFrame(const uint8_t* buffer_y, |
| 70 | const uint8_t* buffer_u, |
| 71 | const uint8_t* buffer_v, |
| 72 | int width, |
| 73 | int height, |
| 74 | int stride_y, |
| 75 | int stride_u, |
| 76 | int stride_v, |
| 77 | VideoRotation rotation); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 78 | |
perkj@webrtc.org | 9f9ea7e | 2015-03-20 10:55:15 +0000 | [diff] [blame] | 79 | // CreateFrame: Sets the frame's members and buffers. If required size is |
| 80 | // bigger than allocated one, new buffers of adequate size will be allocated. |
| 81 | // |buffer| must be a packed I420 buffer. |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 82 | |
| 83 | // TODO(nisse): Deprecated, see above method for advice. |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 84 | void CreateFrame(const uint8_t* buffer, |
perkj@webrtc.org | 9f9ea7e | 2015-03-20 10:55:15 +0000 | [diff] [blame] | 85 | int width, |
| 86 | int height, |
| 87 | VideoRotation rotation); |
| 88 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 89 | // Deep copy frame: If required size is bigger than allocated one, new |
| 90 | // buffers of adequate size will be allocated. |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 91 | // TODO(nisse): Should be deleted in the cricket::VideoFrame and |
| 92 | // webrtc::VideoFrame merge. Instead, use I420Buffer::Copy to make a copy of |
| 93 | // the pixel data, and use the constructor to wrap it into a VideoFrame. |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 94 | void CopyFrame(const VideoFrame& videoFrame); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 95 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 96 | // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a |
| 97 | // reference to the video buffer also retained by |videoFrame|. |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 98 | // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and |
| 99 | // webrtc::VideoFrame merge. Instead, pass video_frame_buffer() and timestamps |
| 100 | // to the constructor. |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 101 | void ShallowCopy(const VideoFrame& videoFrame); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 102 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 103 | // Get frame width. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 104 | int width() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 105 | |
| 106 | // Get frame height. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 107 | int height() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 108 | |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 109 | // System monotonic clock, same timebase as rtc::TimeMicros(). |
| 110 | int64_t timestamp_us() const { return timestamp_us_; } |
| 111 | void set_timestamp_us(int64_t timestamp_us) { |
| 112 | timestamp_us_ = timestamp_us; |
| 113 | } |
| 114 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 115 | // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 116 | // merge, timestamps other than timestamp_us will likely be |
| 117 | // deprecated. |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 118 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 119 | // Set frame timestamp (90kHz). |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 120 | void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 121 | |
| 122 | // Get frame timestamp (90kHz). |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 123 | uint32_t timestamp() const { return timestamp_rtp_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 124 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 125 | // Set capture ntp time in milliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 126 | void set_ntp_time_ms(int64_t ntp_time_ms) { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 127 | ntp_time_ms_ = ntp_time_ms; |
| 128 | } |
| 129 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 130 | // Get capture ntp time in milliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 131 | int64_t ntp_time_ms() const { return ntp_time_ms_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 132 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 133 | // Naming convention for Coordination of Video Orientation. Please see |
| 134 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf |
| 135 | // |
| 136 | // "pending rotation" or "pending" = a frame that has a VideoRotation > 0. |
| 137 | // |
| 138 | // "not pending" = a frame that has a VideoRotation == 0. |
| 139 | // |
| 140 | // "apply rotation" = modify a frame from being "pending" to being "not |
| 141 | // pending" rotation (a no-op for "unrotated"). |
| 142 | // |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 143 | VideoRotation rotation() const { return rotation_; } |
| 144 | void set_rotation(VideoRotation rotation) { |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 145 | rotation_ = rotation; |
| 146 | } |
| 147 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 148 | // Set render time in milliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 149 | void set_render_time_ms(int64_t render_time_ms) { |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 150 | set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 153 | // Get render time in milliseconds. |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 154 | int64_t render_time_ms() const { |
| 155 | return timestamp_us() / rtc::kNumMicrosecsPerMillisec; |
| 156 | } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 157 | |
nisse | 6691070 | 2016-06-22 08:47:49 -0700 | [diff] [blame] | 158 | // Return true if and only if video_frame_buffer() is null. Which is possible |
| 159 | // only if the object was default-constructed. |
| 160 | // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and |
| 161 | // webrtc::VideoFrame merge. The intention is that video_frame_buffer() never |
| 162 | // should return nullptr. To handle potentially uninitialized or non-existent |
| 163 | // frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be |
| 164 | // replaced by video_frame_buffer() == nullptr. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 165 | bool IsZeroSize() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 166 | |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame] | 167 | // Return the underlying buffer. Never nullptr for a properly |
| 168 | // initialized VideoFrame. |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 169 | // Creating a new reference breaks the HasOneRef and IsMutable |
| 170 | // logic. So return a const ref to our reference. |
| 171 | const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer() |
| 172 | const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 173 | |
skvlad | 3abb764 | 2016-06-16 12:08:03 -0700 | [diff] [blame] | 174 | // Return true if the frame is stored in a texture. |
perkj | 26105b4 | 2016-09-29 22:39:10 -0700 | [diff] [blame] | 175 | bool is_texture() const { |
skvlad | 3abb764 | 2016-06-16 12:08:03 -0700 | [diff] [blame] | 176 | return video_frame_buffer() && |
| 177 | video_frame_buffer()->native_handle() != nullptr; |
| 178 | } |
| 179 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 180 | private: |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 181 | // An opaque reference counted handle that stores the pixel data. |
| 182 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_; |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 183 | uint32_t timestamp_rtp_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 184 | int64_t ntp_time_ms_; |
nisse | 74c10b5 | 2016-09-05 00:51:16 -0700 | [diff] [blame] | 185 | int64_t timestamp_us_; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 186 | VideoRotation rotation_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 189 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 190 | // TODO(pbos): Rename EncodedFrame and reformat this class' members. |
| 191 | class EncodedImage { |
| 192 | public: |
hbos | d664836 | 2016-01-21 05:43:11 -0800 | [diff] [blame] | 193 | static const size_t kBufferPaddingBytesH264; |
| 194 | |
| 195 | // Some decoders require encoded image buffers to be padded with a small |
| 196 | // number of additional bytes (due to over-reading byte readers). |
| 197 | static size_t GetBufferPaddingBytes(VideoCodecType codec_type); |
| 198 | |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 199 | EncodedImage() : EncodedImage(nullptr, 0, 0) {} |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 200 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 201 | EncodedImage(uint8_t* buffer, size_t length, size_t size) |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 202 | : _buffer(buffer), _length(length), _size(size) {} |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 203 | |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 204 | struct AdaptReason { |
| 205 | AdaptReason() |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 206 | : quality_resolution_downscales(-1), |
| 207 | bw_resolutions_disabled(-1) {} |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 208 | |
| 209 | int quality_resolution_downscales; // Number of times this frame is down |
| 210 | // scaled in resolution due to quality. |
| 211 | // Or -1 if information is not provided. |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 212 | int bw_resolutions_disabled; // Number of resolutions that are not sent |
| 213 | // due to bandwidth for this frame. |
| 214 | // Or -1 if information is not provided. |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 215 | }; |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 216 | uint32_t _encodedWidth = 0; |
| 217 | uint32_t _encodedHeight = 0; |
| 218 | uint32_t _timeStamp = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 219 | // NTP time of the capture time in local timebase in milliseconds. |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 220 | int64_t ntp_time_ms_ = 0; |
| 221 | int64_t capture_time_ms_ = 0; |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 222 | FrameType _frameType = kVideoFrameDelta; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 223 | uint8_t* _buffer; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 224 | size_t _length; |
| 225 | size_t _size; |
Per | ba7dc72 | 2016-04-19 15:01:23 +0200 | [diff] [blame] | 226 | VideoRotation rotation_ = kVideoRotation_0; |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 227 | bool _completeFrame = false; |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 228 | AdaptReason adapt_reason_; |
asapersson | 86b0160 | 2015-10-20 23:55:26 -0700 | [diff] [blame] | 229 | int qp_ = -1; // Quantizer value. |
isheriff | 6b4b5f3 | 2016-06-08 00:24:21 -0700 | [diff] [blame] | 230 | |
| 231 | // When an application indicates non-zero values here, it is taken as an |
| 232 | // indication that all future frames will be constrained with those limits |
| 233 | // until the application indicates a change again. |
| 234 | PlayoutDelay playout_delay_ = {-1, -1}; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | } // namespace webrtc |
| 238 | #endif // WEBRTC_VIDEO_FRAME_H_ |