blob: 8fbfb595a5169969ee5bd8688cdc01c34235818d [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
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.
nisse66910702016-06-22 08:47:49 -070051
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öller739fcb92016-02-29 13:11:45 +010057 void CreateEmptyFrame(int width,
58 int height,
59 int stride_y,
60 int stride_u,
61 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000062
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.
nisse66910702016-06-22 08:47:49 -070065
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öller739fcb92016-02-29 13:11:45 +010069 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.org1226e922015-02-11 18:37:54 +000078
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000079 // 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.
nisse66910702016-06-22 08:47:49 -070082
83 // TODO(nisse): Deprecated, see above method for advice.
Niels Möller739fcb92016-02-29 13:11:45 +010084 void CreateFrame(const uint8_t* buffer,
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000085 int width,
86 int height,
87 VideoRotation rotation);
88
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000089 // Deep copy frame: If required size is bigger than allocated one, new
90 // buffers of adequate size will be allocated.
nisse66910702016-06-22 08:47:49 -070091 // 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öller739fcb92016-02-29 13:11:45 +010094 void CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000095
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000096 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
97 // reference to the video buffer also retained by |videoFrame|.
nisse66910702016-06-22 08:47:49 -070098 // 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-Sanchez47650702015-05-29 17:21:40 -0700101 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +0000102
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000103 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000104 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000105
106 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000107 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000108
nisse74c10b52016-09-05 00:51:16 -0700109 // 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
nisse66910702016-06-22 08:47:49 -0700115 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
nisse74c10b52016-09-05 00:51:16 -0700116 // merge, timestamps other than timestamp_us will likely be
117 // deprecated.
nisse66910702016-06-22 08:47:49 -0700118
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000119 // Set frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -0700120 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000121
122 // Get frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -0700123 uint32_t timestamp() const { return timestamp_rtp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000124
nisse66910702016-06-22 08:47:49 -0700125 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000126 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000127 ntp_time_ms_ = ntp_time_ms;
128 }
129
nisse66910702016-06-22 08:47:49 -0700130 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000131 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000132
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000133 // 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.org45cdcce2015-03-06 10:41:00 +0000143 VideoRotation rotation() const { return rotation_; }
144 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000145 rotation_ = rotation;
146 }
147
nisse66910702016-06-22 08:47:49 -0700148 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000149 void set_render_time_ms(int64_t render_time_ms) {
nisse74c10b52016-09-05 00:51:16 -0700150 set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000151 }
152
nisse66910702016-06-22 08:47:49 -0700153 // Get render time in milliseconds.
nisse74c10b52016-09-05 00:51:16 -0700154 int64_t render_time_ms() const {
155 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
156 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000157
nisse66910702016-06-22 08:47:49 -0700158 // 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.org45cdcce2015-03-06 10:41:00 +0000165 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000166
nisse26acec42016-04-15 03:43:39 -0700167 // Return the underlying buffer. Never nullptr for a properly
168 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700169 // 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.orgab990ae2014-09-17 09:02:25 +0000173
skvlad3abb7642016-06-16 12:08:03 -0700174 // Return true if the frame is stored in a texture.
perkj26105b42016-09-29 22:39:10 -0700175 bool is_texture() const {
skvlad3abb7642016-06-16 12:08:03 -0700176 return video_frame_buffer() &&
177 video_frame_buffer()->native_handle() != nullptr;
178 }
179
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000180 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000181 // An opaque reference counted handle that stores the pixel data.
182 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisse74c10b52016-09-05 00:51:16 -0700183 uint32_t timestamp_rtp_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000184 int64_t ntp_time_ms_;
nisse74c10b52016-09-05 00:51:16 -0700185 int64_t timestamp_us_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000186 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000187};
188
asaperssonda535c42015-10-19 23:32:41 -0700189
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000190// TODO(pbos): Rename EncodedFrame and reformat this class' members.
191class EncodedImage {
192 public:
hbosd6648362016-01-21 05:43:11 -0800193 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.org4c8b9302015-03-10 12:55:17 +0000199 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200200
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000201 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000202 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000203
asapersson4306fc72015-10-19 00:35:21 -0700204 struct AdaptReason {
205 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700206 : quality_resolution_downscales(-1),
207 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700208
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.
asaperssonda535c42015-10-19 23:32:41 -0700212 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.
asapersson4306fc72015-10-19 00:35:21 -0700215 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000216 uint32_t _encodedWidth = 0;
217 uint32_t _encodedHeight = 0;
218 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000219 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000220 int64_t ntp_time_ms_ = 0;
221 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200222 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000223 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000224 size_t _length;
225 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200226 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000227 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700228 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700229 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700230
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.orgab990ae2014-09-17 09:02:25 +0000235};
236
237} // namespace webrtc
238#endif // WEBRTC_VIDEO_FRAME_H_