pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 14 | #include "webrtc/base/scoped_ref_ptr.h" |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 15 | #include "webrtc/common_types.h" |
kjellander | 6f8ce06 | 2015-11-16 13:52:24 -0800 | [diff] [blame] | 16 | #include "webrtc/common_video/include/video_frame_buffer.h" |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 17 | #include "webrtc/common_video/rotation.h" |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 18 | #include "webrtc/typedefs.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 22 | class VideoFrame { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 23 | public: |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 24 | 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.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 29 | |
| 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öller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 34 | void CreateEmptyFrame(int width, |
| 35 | int height, |
| 36 | int stride_y, |
| 37 | int stride_u, |
| 38 | int stride_v); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 39 | |
| 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öller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 42 | 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.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 51 | |
perkj@webrtc.org | 9f9ea7e | 2015-03-20 10:55:15 +0000 | [diff] [blame] | 52 | // 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öller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 55 | void CreateFrame(const uint8_t* buffer, |
perkj@webrtc.org | 9f9ea7e | 2015-03-20 10:55:15 +0000 | [diff] [blame] | 56 | int width, |
| 57 | int height, |
| 58 | VideoRotation rotation); |
| 59 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 60 | // Deep copy frame: If required size is bigger than allocated one, new |
| 61 | // buffers of adequate size will be allocated. |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 62 | void CopyFrame(const VideoFrame& videoFrame); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 63 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 64 | // 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-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 66 | void ShallowCopy(const VideoFrame& videoFrame); |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 67 | |
magjed@webrtc.org | 0d9bb8e | 2015-03-11 10:06:58 +0000 | [diff] [blame] | 68 | // Release frame buffer and reset time stamps. |
| 69 | void Reset(); |
| 70 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 71 | // Get pointer to buffer per plane. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 72 | uint8_t* buffer(PlaneType type); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 73 | // Overloading with const. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 74 | const uint8_t* buffer(PlaneType type) const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 75 | |
| 76 | // Get allocated size per plane. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 77 | int allocated_size(PlaneType type) const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 78 | |
| 79 | // Get allocated stride per plane. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 80 | int stride(PlaneType type) const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 81 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 82 | // Get frame width. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 83 | int width() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 84 | |
| 85 | // Get frame height. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 86 | int height() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 87 | |
| 88 | // Set frame timestamp (90kHz). |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 89 | void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 90 | |
| 91 | // Get frame timestamp (90kHz). |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 92 | uint32_t timestamp() const { return timestamp_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 93 | |
| 94 | // Set capture ntp time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 95 | void set_ntp_time_ms(int64_t ntp_time_ms) { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 96 | ntp_time_ms_ = ntp_time_ms; |
| 97 | } |
| 98 | |
| 99 | // Get capture ntp time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 100 | int64_t ntp_time_ms() const { return ntp_time_ms_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 101 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 102 | // Naming convention for Coordination of Video Orientation. Please see |
| 103 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf |
| 104 | // |
| 105 | // "pending rotation" or "pending" = a frame that has a VideoRotation > 0. |
| 106 | // |
| 107 | // "not pending" = a frame that has a VideoRotation == 0. |
| 108 | // |
| 109 | // "apply rotation" = modify a frame from being "pending" to being "not |
| 110 | // pending" rotation (a no-op for "unrotated"). |
| 111 | // |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 112 | VideoRotation rotation() const { return rotation_; } |
| 113 | void set_rotation(VideoRotation rotation) { |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 114 | rotation_ = rotation; |
| 115 | } |
| 116 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 117 | // Set render time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 118 | void set_render_time_ms(int64_t render_time_ms) { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 119 | render_time_ms_ = render_time_ms; |
| 120 | } |
| 121 | |
| 122 | // Get render time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 123 | int64_t render_time_ms() const { return render_time_ms_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 124 | |
| 125 | // Return true if underlying plane buffers are of zero size, false if not. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 126 | bool IsZeroSize() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 127 | |
nisse | 26acec4 | 2016-04-15 03:43:39 -0700 | [diff] [blame^] | 128 | // Return the underlying buffer. Never nullptr for a properly |
| 129 | // initialized VideoFrame. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 130 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 131 | |
magjed@webrtc.org | d4e7d49 | 2015-03-23 12:27:55 +0000 | [diff] [blame] | 132 | // Set the underlying buffer. |
| 133 | void set_video_frame_buffer( |
| 134 | const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer); |
| 135 | |
Peter Boström | eb66e80 | 2015-06-05 11:08:03 +0200 | [diff] [blame] | 136 | // Convert native-handle frame to memory-backed I420 frame. Should not be |
| 137 | // called on a non-native-handle frame. |
| 138 | VideoFrame ConvertNativeToI420Frame() const; |
| 139 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 140 | private: |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 141 | // An opaque reference counted handle that stores the pixel data. |
| 142 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 143 | uint32_t timestamp_; |
| 144 | int64_t ntp_time_ms_; |
| 145 | int64_t render_time_ms_; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 146 | VideoRotation rotation_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 149 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 150 | // TODO(pbos): Rename EncodedFrame and reformat this class' members. |
| 151 | class EncodedImage { |
| 152 | public: |
hbos | d664836 | 2016-01-21 05:43:11 -0800 | [diff] [blame] | 153 | static const size_t kBufferPaddingBytesH264; |
| 154 | |
| 155 | // Some decoders require encoded image buffers to be padded with a small |
| 156 | // number of additional bytes (due to over-reading byte readers). |
| 157 | static size_t GetBufferPaddingBytes(VideoCodecType codec_type); |
| 158 | |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 159 | EncodedImage() : EncodedImage(nullptr, 0, 0) {} |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 160 | EncodedImage(uint8_t* buffer, size_t length, size_t size) |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 161 | : _buffer(buffer), _length(length), _size(size) {} |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 162 | |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 163 | struct AdaptReason { |
| 164 | AdaptReason() |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 165 | : quality_resolution_downscales(-1), |
| 166 | bw_resolutions_disabled(-1) {} |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 167 | |
| 168 | int quality_resolution_downscales; // Number of times this frame is down |
| 169 | // scaled in resolution due to quality. |
| 170 | // Or -1 if information is not provided. |
asapersson | da535c4 | 2015-10-19 23:32:41 -0700 | [diff] [blame] | 171 | int bw_resolutions_disabled; // Number of resolutions that are not sent |
| 172 | // due to bandwidth for this frame. |
| 173 | // Or -1 if information is not provided. |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 174 | }; |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 175 | uint32_t _encodedWidth = 0; |
| 176 | uint32_t _encodedHeight = 0; |
| 177 | uint32_t _timeStamp = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 178 | // NTP time of the capture time in local timebase in milliseconds. |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 179 | int64_t ntp_time_ms_ = 0; |
| 180 | int64_t capture_time_ms_ = 0; |
Peter Boström | 49e196a | 2015-10-23 15:58:18 +0200 | [diff] [blame] | 181 | FrameType _frameType = kVideoFrameDelta; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 182 | uint8_t* _buffer; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 183 | size_t _length; |
| 184 | size_t _size; |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 185 | bool _completeFrame = false; |
asapersson | 4306fc7 | 2015-10-19 00:35:21 -0700 | [diff] [blame] | 186 | AdaptReason adapt_reason_; |
asapersson | 86b0160 | 2015-10-20 23:55:26 -0700 | [diff] [blame] | 187 | int qp_ = -1; // Quantizer value. |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace webrtc |
| 191 | #endif // WEBRTC_VIDEO_FRAME_H_ |