blob: ab8e37b0ed4d41a35843ea4d9ae15632c62d588b [file] [log] [blame]
pbos@webrtc.org1c655452014-09-17 09:02:251/*
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.org398a7842015-03-05 14:03:0814#include "webrtc/base/scoped_ref_ptr.h"
pbosb63d8ad2015-10-19 09:39:0615#include "webrtc/common_types.h"
kjellandercd662542015-11-16 21:52:2416#include "webrtc/common_video/include/video_frame_buffer.h"
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5417#include "webrtc/common_video/rotation.h"
magjed@webrtc.org20ef1432015-03-06 10:41:0018#include "webrtc/typedefs.h"
pbos@webrtc.org1c655452014-09-17 09:02:2519
20namespace webrtc {
21
nisse63df81b2016-06-22 15:47:4922// 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-Sanchez3ca60f32015-05-30 00:21:4025class VideoFrame {
pbos@webrtc.org1c655452014-09-17 09:02:2526 public:
nisse63df81b2016-06-22 15:47:4927 // TODO(nisse): Deprecated. Using the default constructor violates the
28 // reasonable assumption that video_frame_buffer() returns a valid buffer.
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4029 VideoFrame();
nisse63df81b2016-06-22 15:47:4930
31 // Preferred constructor.
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4032 VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
33 uint32_t timestamp,
34 int64_t render_time_ms,
35 VideoRotation rotation);
pbos@webrtc.org1c655452014-09-17 09:02:2536
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.
nisse63df81b2016-06-22 15:47:4941
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öllerc961c782016-02-29 12:11:4547 void CreateEmptyFrame(int width,
48 int height,
49 int stride_y,
50 int stride_u,
51 int stride_v);
pbos@webrtc.org1c655452014-09-17 09:02:2552
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.
nisse63df81b2016-06-22 15:47:4955
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öllerc961c782016-02-29 12:11:4559 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.orgfd7343a2015-02-11 18:37:5468
perkj@webrtc.org0db69ce2015-03-20 10:55:1569 // 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.
nisse63df81b2016-06-22 15:47:4972
73 // TODO(nisse): Deprecated, see above method for advice.
Niels Möllerc961c782016-02-29 12:11:4574 void CreateFrame(const uint8_t* buffer,
perkj@webrtc.org0db69ce2015-03-20 10:55:1575 int width,
76 int height,
77 VideoRotation rotation);
78
perkj@webrtc.orgbddb5882015-03-18 09:51:0579 // Deep copy frame: If required size is bigger than allocated one, new
80 // buffers of adequate size will be allocated.
nisse63df81b2016-06-22 15:47:4981 // 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öllerc961c782016-02-29 12:11:4584 void CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.org1c655452014-09-17 09:02:2585
perkj@webrtc.orgbddb5882015-03-18 09:51:0586 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
87 // reference to the video buffer also retained by |videoFrame|.
nisse63df81b2016-06-22 15:47:4988 // 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-Sanchez3ca60f32015-05-30 00:21:4091 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgbddb5882015-03-18 09:51:0592
pbos@webrtc.org1c655452014-09-17 09:02:2593 // Get allocated size per plane.
nisse63df81b2016-06-22 15:47:4994
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.org20ef1432015-03-06 10:41:0098 int allocated_size(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2599
pbos@webrtc.org1c655452014-09-17 09:02:25100 // Get frame width.
magjed@webrtc.org20ef1432015-03-06 10:41:00101 int width() const;
pbos@webrtc.org1c655452014-09-17 09:02:25102
103 // Get frame height.
magjed@webrtc.org20ef1432015-03-06 10:41:00104 int height() const;
pbos@webrtc.org1c655452014-09-17 09:02:25105
nisse63df81b2016-06-22 15:47:49106 // 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.org1c655452014-09-17 09:02:25110 // Set frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:00111 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.org1c655452014-09-17 09:02:25112
113 // Get frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:00114 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.org1c655452014-09-17 09:02:25115
nisse63df81b2016-06-22 15:47:49116 // Set capture ntp time in milliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00117 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25118 ntp_time_ms_ = ntp_time_ms;
119 }
120
nisse63df81b2016-06-22 15:47:49121 // Get capture ntp time in milliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00122 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25123
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54124 // 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.org20ef1432015-03-06 10:41:00134 VideoRotation rotation() const { return rotation_; }
135 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54136 rotation_ = rotation;
137 }
138
nisse63df81b2016-06-22 15:47:49139 // Set render time in milliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00140 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25141 render_time_ms_ = render_time_ms;
142 }
143
nisse63df81b2016-06-22 15:47:49144 // Get render time in milliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00145 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25146
nisse63df81b2016-06-22 15:47:49147 // 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.org20ef1432015-03-06 10:41:00154 bool IsZeroSize() const;
pbos@webrtc.org1c655452014-09-17 09:02:25155
nissea186ef62016-04-15 10:43:39156 // Return the underlying buffer. Never nullptr for a properly
157 // initialized VideoFrame.
nisse671d8052016-05-17 11:05:47158 // 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.org1c655452014-09-17 09:02:25162
skvlad48be0542016-06-16 19:08:03163 // 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.org1c655452014-09-17 09:02:25169 private:
magjed@webrtc.org398a7842015-03-05 14:03:08170 // An opaque reference counted handle that stores the pixel data.
171 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.org1c655452014-09-17 09:02:25172 uint32_t timestamp_;
173 int64_t ntp_time_ms_;
174 int64_t render_time_ms_;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54175 VideoRotation rotation_;
pbos@webrtc.org1c655452014-09-17 09:02:25176};
177
asapersson39f77022015-10-20 06:32:41178
pbos@webrtc.org1c655452014-09-17 09:02:25179// TODO(pbos): Rename EncodedFrame and reformat this class' members.
180class EncodedImage {
181 public:
hbos6f757142016-01-21 13:43:11182 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.orgf0657cd2015-03-10 12:55:17188 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Pereb1bef42016-04-19 13:01:23189
pkasting@chromium.org0ab923a2014-11-20 22:28:14190 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.orgf0657cd2015-03-10 12:55:17191 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.org1c655452014-09-17 09:02:25192
asapersson7a4c3c12015-10-19 07:35:21193 struct AdaptReason {
194 AdaptReason()
asapersson39f77022015-10-20 06:32:41195 : quality_resolution_downscales(-1),
196 bw_resolutions_disabled(-1) {}
asapersson7a4c3c12015-10-19 07:35:21197
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.
asapersson39f77022015-10-20 06:32:41201 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.
asapersson7a4c3c12015-10-19 07:35:21204 };
pbos@webrtc.orgf0657cd2015-03-10 12:55:17205 uint32_t _encodedWidth = 0;
206 uint32_t _encodedHeight = 0;
207 uint32_t _timeStamp = 0;
pbos@webrtc.org1c655452014-09-17 09:02:25208 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.orgf0657cd2015-03-10 12:55:17209 int64_t ntp_time_ms_ = 0;
210 int64_t capture_time_ms_ = 0;
Peter Boström9511e462015-10-23 13:58:18211 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.org1c655452014-09-17 09:02:25212 uint8_t* _buffer;
pkasting@chromium.org0ab923a2014-11-20 22:28:14213 size_t _length;
214 size_t _size;
Pereb1bef42016-04-19 13:01:23215 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.orgf0657cd2015-03-10 12:55:17216 bool _completeFrame = false;
asapersson7a4c3c12015-10-19 07:35:21217 AdaptReason adapt_reason_;
asaperssona0621102015-10-21 06:55:26218 int qp_ = -1; // Quantizer value.
isheriff00cc0452016-06-08 07:24:21219
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.org1c655452014-09-17 09:02:25224};
225
226} // namespace webrtc
227#endif // WEBRTC_VIDEO_FRAME_H_