blob: 0f2b9a68afd1a15691ffefccee2377d8902cddb9 [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
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070022class VideoFrame {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000023 public:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070024 VideoFrame();
25 VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
26 uint32_t timestamp,
27 int64_t render_time_ms,
28 VideoRotation rotation);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000029
30 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
31 // on set dimensions - height and plane stride.
32 // If required size is bigger than the allocated one, new buffers of adequate
33 // size will be allocated.
Niels Möller739fcb92016-02-29 13:11:45 +010034 void CreateEmptyFrame(int width,
35 int height,
36 int stride_y,
37 int stride_u,
38 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000039
40 // CreateFrame: Sets the frame's members and buffers. If required size is
41 // bigger than allocated one, new buffers of adequate size will be allocated.
Niels Möller739fcb92016-02-29 13:11:45 +010042 void CreateFrame(const uint8_t* buffer_y,
43 const uint8_t* buffer_u,
44 const uint8_t* buffer_v,
45 int width,
46 int height,
47 int stride_y,
48 int stride_u,
49 int stride_v,
50 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000051
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000052 // CreateFrame: Sets the frame's members and buffers. If required size is
53 // bigger than allocated one, new buffers of adequate size will be allocated.
54 // |buffer| must be a packed I420 buffer.
Niels Möller739fcb92016-02-29 13:11:45 +010055 void CreateFrame(const uint8_t* buffer,
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000056 int width,
57 int height,
58 VideoRotation rotation);
59
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000060 // Deep copy frame: If required size is bigger than allocated one, new
61 // buffers of adequate size will be allocated.
Niels Möller739fcb92016-02-29 13:11:45 +010062 void CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000063
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000064 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
65 // reference to the video buffer also retained by |videoFrame|.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070066 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000067
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000068 // Get allocated size per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000069 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000070
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000071 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000072 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000073
74 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000075 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000076
77 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000078 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000079
80 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000081 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000082
83 // Set capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000084 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000085 ntp_time_ms_ = ntp_time_ms;
86 }
87
88 // Get capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000089 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000090
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000091 // Naming convention for Coordination of Video Orientation. Please see
92 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
93 //
94 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
95 //
96 // "not pending" = a frame that has a VideoRotation == 0.
97 //
98 // "apply rotation" = modify a frame from being "pending" to being "not
99 // pending" rotation (a no-op for "unrotated").
100 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000101 VideoRotation rotation() const { return rotation_; }
102 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000103 rotation_ = rotation;
104 }
105
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000106 // Set render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000107 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000108 render_time_ms_ = render_time_ms;
109 }
110
111 // Get render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000112 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000113
114 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000115 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000116
nisse26acec42016-04-15 03:43:39 -0700117 // Return the underlying buffer. Never nullptr for a properly
118 // initialized VideoFrame.
nissec9c142f2016-05-17 04:05:47 -0700119 // Creating a new reference breaks the HasOneRef and IsMutable
120 // logic. So return a const ref to our reference.
121 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
122 const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000123
skvlad3abb7642016-06-16 12:08:03 -0700124 // Return true if the frame is stored in a texture.
125 bool is_texture() {
126 return video_frame_buffer() &&
127 video_frame_buffer()->native_handle() != nullptr;
128 }
129
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000130 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000131 // An opaque reference counted handle that stores the pixel data.
132 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000133 uint32_t timestamp_;
134 int64_t ntp_time_ms_;
135 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000136 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000137};
138
asaperssonda535c42015-10-19 23:32:41 -0700139
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000140// TODO(pbos): Rename EncodedFrame and reformat this class' members.
141class EncodedImage {
142 public:
hbosd6648362016-01-21 05:43:11 -0800143 static const size_t kBufferPaddingBytesH264;
144
145 // Some decoders require encoded image buffers to be padded with a small
146 // number of additional bytes (due to over-reading byte readers).
147 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
148
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000149 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Perba7dc722016-04-19 15:01:23 +0200150
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000151 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000152 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000153
asapersson4306fc72015-10-19 00:35:21 -0700154 struct AdaptReason {
155 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700156 : quality_resolution_downscales(-1),
157 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700158
159 int quality_resolution_downscales; // Number of times this frame is down
160 // scaled in resolution due to quality.
161 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700162 int bw_resolutions_disabled; // Number of resolutions that are not sent
163 // due to bandwidth for this frame.
164 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700165 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000166 uint32_t _encodedWidth = 0;
167 uint32_t _encodedHeight = 0;
168 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000169 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000170 int64_t ntp_time_ms_ = 0;
171 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200172 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000173 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000174 size_t _length;
175 size_t _size;
Perba7dc722016-04-19 15:01:23 +0200176 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000177 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700178 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700179 int qp_ = -1; // Quantizer value.
isheriff6b4b5f32016-06-08 00:24:21 -0700180
181 // When an application indicates non-zero values here, it is taken as an
182 // indication that all future frames will be constrained with those limits
183 // until the application indicates a change again.
184 PlayoutDelay playout_delay_ = {-1, -1};
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000185};
186
187} // namespace webrtc
188#endif // WEBRTC_VIDEO_FRAME_H_