blob: 05692788972e4cd07c5b2fb43a1c8a802690f3b5 [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
nisse7341ab82016-11-02 03:39:58 -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
nisse67dca9f2016-10-31 08:05:50 -070047 // Support move and copy.
48 VideoFrame(const VideoFrame&) = default;
49 VideoFrame(VideoFrame&&) = default;
50 VideoFrame& operator=(const VideoFrame&) = default;
51 VideoFrame& operator=(VideoFrame&&) = default;
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000052
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000053 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000054 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000055
56 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000057 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000058
nisse74c10b52016-09-05 00:51:16 -070059 // System monotonic clock, same timebase as rtc::TimeMicros().
60 int64_t timestamp_us() const { return timestamp_us_; }
61 void set_timestamp_us(int64_t timestamp_us) {
62 timestamp_us_ = timestamp_us;
63 }
64
nisse66910702016-06-22 08:47:49 -070065 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
nisse74c10b52016-09-05 00:51:16 -070066 // merge, timestamps other than timestamp_us will likely be
67 // deprecated.
nisse66910702016-06-22 08:47:49 -070068
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000069 // Set frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070070 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000071
72 // Get frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070073 uint32_t timestamp() const { return timestamp_rtp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000074
nisse09347852016-10-19 00:30:30 -070075 // For now, transport_frame_id and rtp timestamp are the same.
76 // TODO(nisse): Must be handled differently for QUIC.
77 uint32_t transport_frame_id() const { return timestamp(); }
78
nisse66910702016-06-22 08:47:49 -070079 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000080 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000081 ntp_time_ms_ = ntp_time_ms;
82 }
83
nisse66910702016-06-22 08:47:49 -070084 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000085 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000086
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000087 // Naming convention for Coordination of Video Orientation. Please see
88 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
89 //
90 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
91 //
92 // "not pending" = a frame that has a VideoRotation == 0.
93 //
94 // "apply rotation" = modify a frame from being "pending" to being "not
95 // pending" rotation (a no-op for "unrotated").
96 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000097 VideoRotation rotation() const { return rotation_; }
98 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000099 rotation_ = rotation;
100 }
101
nisse66910702016-06-22 08:47:49 -0700102 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000103 void set_render_time_ms(int64_t render_time_ms) {
nisse74c10b52016-09-05 00:51:16 -0700104 set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000105 }
106
nisse66910702016-06-22 08:47:49 -0700107 // Get render time in milliseconds.
nisse74c10b52016-09-05 00:51:16 -0700108 int64_t render_time_ms() const {
109 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
110 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000111
nisse66910702016-06-22 08:47:49 -0700112 // Return true if and only if video_frame_buffer() is null. Which is possible
113 // only if the object was default-constructed.
114 // TODO(nisse): Deprecated. Should be deleted in the cricket::VideoFrame and
115 // webrtc::VideoFrame merge. The intention is that video_frame_buffer() never
116 // should return nullptr. To handle potentially uninitialized or non-existent
117 // frames, consider using rtc::Optional. Otherwise, IsZeroSize() can be
118 // replaced by video_frame_buffer() == nullptr.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000119 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000120
nisse26acec42016-04-15 03:43:39 -0700121 // Return the underlying buffer. Never nullptr for a properly
122 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700123 // Creating a new reference breaks the HasOneRef and IsMutable
124 // logic. So return a const ref to our reference.
125 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
126 const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000127
skvlad3abb7642016-06-16 12:08:03 -0700128 // Return true if the frame is stored in a texture.
perkjfa10b552016-10-02 23:45:26 -0700129 bool is_texture() const {
skvlad3abb7642016-06-16 12:08:03 -0700130 return video_frame_buffer() &&
131 video_frame_buffer()->native_handle() != nullptr;
132 }
133
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000134 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000135 // An opaque reference counted handle that stores the pixel data.
136 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisse74c10b52016-09-05 00:51:16 -0700137 uint32_t timestamp_rtp_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000138 int64_t ntp_time_ms_;
nisse74c10b52016-09-05 00:51:16 -0700139 int64_t timestamp_us_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000140 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000141};
142
asaperssonda535c42015-10-19 23:32:41 -0700143
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000144// TODO(pbos): Rename EncodedFrame and reformat this class' members.
145class EncodedImage {
146 public:
hbosd6648362016-01-21 05:43:11 -0800147 static const size_t kBufferPaddingBytesH264;
148
149 // Some decoders require encoded image buffers to be padded with a small
150 // number of additional bytes (due to over-reading byte readers).
151 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
152
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000153 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200154
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000155 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000156 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000157
asapersson4306fc72015-10-19 00:35:21 -0700158 struct AdaptReason {
159 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700160 : quality_resolution_downscales(-1),
161 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700162
163 int quality_resolution_downscales; // Number of times this frame is down
164 // scaled in resolution due to quality.
165 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700166 int bw_resolutions_disabled; // Number of resolutions that are not sent
167 // due to bandwidth for this frame.
168 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700169 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000170 uint32_t _encodedWidth = 0;
171 uint32_t _encodedHeight = 0;
172 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000173 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000174 int64_t ntp_time_ms_ = 0;
175 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200176 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000177 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000178 size_t _length;
179 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200180 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000181 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700182 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700183 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700184
185 // When an application indicates non-zero values here, it is taken as an
186 // indication that all future frames will be constrained with those limits
187 // until the application indicates a change again.
188 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000189};
190
191} // namespace webrtc
192#endif // WEBRTC_VIDEO_FRAME_H_