blob: 5fd62a6bbb08e30f41d229468adfaf5e0e3cd97b [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
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070023class VideoFrame {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000024 public:
nisse74c10b52016-09-05 00:51:16 -070025 // TODO(nisse): This constructor is consistent with
26 // cricket::WebRtcVideoFrame. After the class
27 // cricket::WebRtcVideoFrame and its baseclass cricket::VideoFrame
28 // are deleted, we should consider whether or not we want to stick
29 // to this style and deprecate the other constructors.
30 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
31 webrtc::VideoRotation rotation,
32 int64_t timestamp_us);
33
nisse66910702016-06-22 08:47:49 -070034 // Preferred constructor.
nisse74c10b52016-09-05 00:51:16 -070035 VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070036 uint32_t timestamp,
37 int64_t render_time_ms,
38 VideoRotation rotation);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000039
nisse67dca9f2016-10-31 08:05:50 -070040 // Support move and copy.
41 VideoFrame(const VideoFrame&) = default;
42 VideoFrame(VideoFrame&&) = default;
43 VideoFrame& operator=(const VideoFrame&) = default;
44 VideoFrame& operator=(VideoFrame&&) = default;
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000045
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000046 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000047 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000048
49 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000050 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000051
nisse74c10b52016-09-05 00:51:16 -070052 // System monotonic clock, same timebase as rtc::TimeMicros().
53 int64_t timestamp_us() const { return timestamp_us_; }
54 void set_timestamp_us(int64_t timestamp_us) {
55 timestamp_us_ = timestamp_us;
56 }
57
nisse66910702016-06-22 08:47:49 -070058 // TODO(nisse): After the cricket::VideoFrame and webrtc::VideoFrame
nisse74c10b52016-09-05 00:51:16 -070059 // merge, timestamps other than timestamp_us will likely be
60 // deprecated.
nisse66910702016-06-22 08:47:49 -070061
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000062 // Set frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070063 void set_timestamp(uint32_t timestamp) { timestamp_rtp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000064
65 // Get frame timestamp (90kHz).
nisse74c10b52016-09-05 00:51:16 -070066 uint32_t timestamp() const { return timestamp_rtp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000067
nisse09347852016-10-19 00:30:30 -070068 // For now, transport_frame_id and rtp timestamp are the same.
69 // TODO(nisse): Must be handled differently for QUIC.
70 uint32_t transport_frame_id() const { return timestamp(); }
71
nisse66910702016-06-22 08:47:49 -070072 // Set capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000073 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000074 ntp_time_ms_ = ntp_time_ms;
75 }
76
nisse66910702016-06-22 08:47:49 -070077 // Get capture ntp time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000078 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000079
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000080 // Naming convention for Coordination of Video Orientation. Please see
81 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
82 //
83 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
84 //
85 // "not pending" = a frame that has a VideoRotation == 0.
86 //
87 // "apply rotation" = modify a frame from being "pending" to being "not
88 // pending" rotation (a no-op for "unrotated").
89 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000090 VideoRotation rotation() const { return rotation_; }
91 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000092 rotation_ = rotation;
93 }
94
nisse66910702016-06-22 08:47:49 -070095 // Set render time in milliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000096 void set_render_time_ms(int64_t render_time_ms) {
nisse74c10b52016-09-05 00:51:16 -070097 set_timestamp_us(render_time_ms * rtc::kNumMicrosecsPerMillisec);;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098 }
99
nisse66910702016-06-22 08:47:49 -0700100 // Get render time in milliseconds.
nisse74c10b52016-09-05 00:51:16 -0700101 int64_t render_time_ms() const {
102 return timestamp_us() / rtc::kNumMicrosecsPerMillisec;
103 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000104
nisse26acec42016-04-15 03:43:39 -0700105 // Return the underlying buffer. Never nullptr for a properly
106 // initialized VideoFrame.
nissee3fe4a72016-11-10 08:44:38 -0800107 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000108
skvlad3abb7642016-06-16 12:08:03 -0700109 // Return true if the frame is stored in a texture.
perkjfa10b552016-10-02 23:45:26 -0700110 bool is_texture() const {
skvlad3abb7642016-06-16 12:08:03 -0700111 return video_frame_buffer() &&
112 video_frame_buffer()->native_handle() != nullptr;
113 }
114
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000115 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000116 // An opaque reference counted handle that stores the pixel data.
117 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
nisse74c10b52016-09-05 00:51:16 -0700118 uint32_t timestamp_rtp_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000119 int64_t ntp_time_ms_;
nisse74c10b52016-09-05 00:51:16 -0700120 int64_t timestamp_us_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000121 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000122};
123
asaperssonda535c42015-10-19 23:32:41 -0700124
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000125// TODO(pbos): Rename EncodedFrame and reformat this class' members.
126class EncodedImage {
127 public:
hbosd6648362016-01-21 05:43:11 -0800128 static const size_t kBufferPaddingBytesH264;
129
130 // Some decoders require encoded image buffers to be padded with a small
131 // number of additional bytes (due to over-reading byte readers).
132 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
133
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000134 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200135
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000136 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000137 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000138
asapersson4306fc72015-10-19 00:35:21 -0700139 struct AdaptReason {
kthelgasonfe04bd42016-12-19 06:17:30 -0800140 AdaptReason()
141 : quality_resolution_downscales(-1),
142 bw_resolutions_disabled(-1) {}
143
144 int quality_resolution_downscales; // Number of times this frame is down
145 // scaled in resolution due to quality.
146 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700147 int bw_resolutions_disabled; // Number of resolutions that are not sent
148 // due to bandwidth for this frame.
149 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700150 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000151 uint32_t _encodedWidth = 0;
152 uint32_t _encodedHeight = 0;
153 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000154 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000155 int64_t ntp_time_ms_ = 0;
156 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200157 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000158 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000159 size_t _length;
160 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200161 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000162 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700163 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700164 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700165
166 // When an application indicates non-zero values here, it is taken as an
167 // indication that all future frames will be constrained with those limits
168 // until the application indicates a change again.
169 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000170};
171
172} // namespace webrtc
173#endif // WEBRTC_VIDEO_FRAME_H_