blob: ab8e37b0ed4d41a35843ea4d9ae15632c62d588b [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"
pbos22993e12015-10-19 02:39:06 -070015#include "webrtc/common_types.h"
kjellander6f8ce062015-11-16 13:52:24 -080016#include "webrtc/common_video/include/video_frame_buffer.h"
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000017#include "webrtc/common_video/rotation.h"
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000018#include "webrtc/typedefs.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000019
20namespace webrtc {
21
nisse66910702016-06-22 08:47:49 -070022// TODO(nisse): This class duplicates cricket::VideoFrame. There's
23// ongoing work to merge the classes. See
24// https://bugs.chromium.org/p/webrtc/issues/detail?id=5682.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070025class VideoFrame {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000026 public:
nisse66910702016-06-22 08:47:49 -070027 // TODO(nisse): Deprecated. Using the default constructor violates the
28 // reasonable assumption that video_frame_buffer() returns a valid buffer.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070029 VideoFrame();
nisse66910702016-06-22 08:47:49 -070030
31 // Preferred constructor.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070032 VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
33 uint32_t timestamp,
34 int64_t render_time_ms,
35 VideoRotation rotation);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000036
37 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
38 // on set dimensions - height and plane stride.
39 // If required size is bigger than the allocated one, new buffers of adequate
40 // size will be allocated.
nisse66910702016-06-22 08:47:49 -070041
42 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
43 // webrtc::VideoFrame merge. If you need to write into the frame, create a
44 // VideoFrameBuffer of the desired size, e.g, using I420Buffer::Create and
45 // write to that. And if you need to wrap it into a VideoFrame, pass it to the
46 // constructor.
Niels Möller739fcb92016-02-29 13:11:45 +010047 void CreateEmptyFrame(int width,
48 int height,
49 int stride_y,
50 int stride_u,
51 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000052
53 // CreateFrame: Sets the frame's members and buffers. If required size is
54 // bigger than allocated one, new buffers of adequate size will be allocated.
nisse66910702016-06-22 08:47:49 -070055
56 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
57 // webrtc::VideoFrame merge. Instead, create a VideoFrameBuffer and pass to
58 // the constructor. E.g, use I420Buffer::Copy(WrappedI420Buffer(...)).
Niels Möller739fcb92016-02-29 13:11:45 +010059 void CreateFrame(const uint8_t* buffer_y,
60 const uint8_t* buffer_u,
61 const uint8_t* buffer_v,
62 int width,
63 int height,
64 int stride_y,
65 int stride_u,
66 int stride_v,
67 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000068
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000069 // CreateFrame: Sets the frame's members and buffers. If required size is
70 // bigger than allocated one, new buffers of adequate size will be allocated.
71 // |buffer| must be a packed I420 buffer.
nisse66910702016-06-22 08:47:49 -070072
73 // TODO(nisse): Deprecated, see above method for advice.
Niels Möller739fcb92016-02-29 13:11:45 +010074 void CreateFrame(const uint8_t* buffer,
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000075 int width,
76 int height,
77 VideoRotation rotation);
78
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000079 // Deep copy frame: If required size is bigger than allocated one, new
80 // buffers of adequate size will be allocated.
nisse66910702016-06-22 08:47:49 -070081 // TODO(nisse): Should be deleted in the cricket::VideoFrame and
82 // webrtc::VideoFrame merge. Instead, use I420Buffer::Copy to make a copy of
83 // the pixel data, and use the constructor to wrap it into a VideoFrame.
Niels Möller739fcb92016-02-29 13:11:45 +010084 void CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000085
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000086 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
87 // reference to the video buffer also retained by |videoFrame|.
nisse66910702016-06-22 08:47:49 -070088 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
89 // webrtc::VideoFrame merge. Instead, pass video_frame_buffer() and timestamps
90 // to the constructor.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070091 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000092
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000093 // Get allocated size per plane.
nisse66910702016-06-22 08:47:49 -070094
95 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
96 // webrtc::VideoFrame merge. When used with memset, consider using
97 // libyuv::I420Rect instead.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000098 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000099
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000100 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000101 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000102
103 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000104 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000105
nisse66910702016-06-22 08:47:49 -0700106 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
107 // merge, we'll have methods timestamp_us and set_timestamp_us, all
108 // other frame timestamps will likely be deprecated.
109
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000110 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000111 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000112
113 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000114 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000115
nisse66910702016-06-22 08:47:49 -0700116 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000117 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000118 ntp_time_ms_ = ntp_time_ms;
119 }
120
nisse66910702016-06-22 08:47:49 -0700121 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000122 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000123
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000124 // Naming convention for Coordination of Video Orientation. Please see
125 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
126 //
127 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
128 //
129 // "not pending" = a frame that has a VideoRotation == 0.
130 //
131 // "apply rotation" = modify a frame from being "pending" to being "not
132 // pending" rotation (a no-op for "unrotated").
133 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000134 VideoRotation rotation() const { return rotation_; }
135 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000136 rotation_ = rotation;
137 }
138
nisse66910702016-06-22 08:47:49 -0700139 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000140 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000141 render_time_ms_ = render_time_ms;
142 }
143
nisse66910702016-06-22 08:47:49 -0700144 // Get render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000145 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000146
nisse66910702016-06-22 08:47:49 -0700147 // Return true if and only if video_frame_buffer() is null. Which is possible
148 // only if the object was default-constructed.
149 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
150 // webrtc::VideoFrame merge. The intention is that video_frame_buffer() never
151 // should return nullptr. To handle potentially uninitialized or non-existent
152 // frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be
153 // replaced by video_frame_buffer() == nullptr.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000154 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000155
nisse26acec42016-04-15 03:43:39 -0700156 // Return the underlying buffer. Never nullptr for a properly
157 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700158 // Creating a new reference breaks the HasOneRef and IsMutable
159 // logic. So return a const ref to our reference.
160 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
161 const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000162
skvlad3abb7642016-06-16 12:08:03 -0700163 // Return true if the frame is stored in a texture.
164 bool is_texture() {
165 return video_frame_buffer() &&
166 video_frame_buffer()->native_handle() != nullptr;
167 }
168
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000169 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000170 // An opaque reference counted handle that stores the pixel data.
171 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000172 uint32_t timestamp_;
173 int64_t ntp_time_ms_;
174 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000175 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000176};
177
asaperssonda535c42015-10-19 23:32:41 -0700178
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000179// TODO(pbos): Rename EncodedFrame and reformat this class' members.
180class EncodedImage {
181 public:
hbosd6648362016-01-21 05:43:11 -0800182 static const size_t kBufferPaddingBytesH264;
183
184 // Some decoders require encoded image buffers to be padded with a small
185 // number of additional bytes (due to over-reading byte readers).
186 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
187
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000188 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200189
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000190 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000191 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000192
asapersson4306fc72015-10-19 00:35:21 -0700193 struct AdaptReason {
194 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700195 : quality_resolution_downscales(-1),
196 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700197
198 int quality_resolution_downscales; // Number of times this frame is down
199 // scaled in resolution due to quality.
200 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700201 int bw_resolutions_disabled; // Number of resolutions that are not sent
202 // due to bandwidth for this frame.
203 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700204 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000205 uint32_t _encodedWidth = 0;
206 uint32_t _encodedHeight = 0;
207 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000208 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000209 int64_t ntp_time_ms_ = 0;
210 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200211 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000212 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000213 size_t _length;
214 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200215 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000216 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700217 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700218 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700219
220 // When an application indicates non-zero values here, it is taken as an
221 // indication that all future frames will be constrained with those limits
222 // until the application indicates a change again.
223 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000224};
225
226} // namespace webrtc
227#endif // WEBRTC_VIDEO_FRAME_H_