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" |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 15 | #include "webrtc/common_video/interface/native_handle.h" |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 16 | #include "webrtc/common_video/interface/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 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 22 | class I420VideoFrame { |
| 23 | public: |
| 24 | I420VideoFrame(); |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 25 | I420VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer, |
| 26 | uint32_t timestamp, |
| 27 | int64_t render_time_ms, |
| 28 | VideoRotation rotation); |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 29 | I420VideoFrame(NativeHandle* handle, |
| 30 | int width, |
| 31 | int height, |
| 32 | uint32_t timestamp, |
| 33 | int64_t render_time_ms); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 34 | |
pbos@webrtc.org | 5a477a0 | 2015-03-18 14:11:39 +0000 | [diff] [blame] | 35 | // TODO(pbos): Make all create/copy functions void, they should not be able to |
| 36 | // fail (which should be DCHECK/CHECKed instead). |
| 37 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 38 | // CreateEmptyFrame: Sets frame dimensions and allocates buffers based |
| 39 | // on set dimensions - height and plane stride. |
| 40 | // If required size is bigger than the allocated one, new buffers of adequate |
| 41 | // size will be allocated. |
| 42 | // Return value: 0 on success, -1 on error. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 43 | int CreateEmptyFrame(int width, |
| 44 | int height, |
| 45 | int stride_y, |
| 46 | int stride_u, |
| 47 | int stride_v); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 48 | |
| 49 | // CreateFrame: Sets the frame's members and buffers. If required size is |
| 50 | // bigger than allocated one, new buffers of adequate size will be allocated. |
| 51 | // Return value: 0 on success, -1 on error. |
hbos@webrtc.org | 93d9d65 | 2015-03-16 13:26:00 +0000 | [diff] [blame] | 52 | int CreateFrame(const uint8_t* buffer_y, |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 53 | const uint8_t* buffer_u, |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 54 | const uint8_t* buffer_v, |
| 55 | int width, |
| 56 | int height, |
| 57 | int stride_y, |
| 58 | int stride_u, |
| 59 | int stride_v); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 60 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 61 | // TODO(guoweis): remove the previous CreateFrame when chromium has this code. |
hbos@webrtc.org | 93d9d65 | 2015-03-16 13:26:00 +0000 | [diff] [blame] | 62 | int CreateFrame(const uint8_t* buffer_y, |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 63 | const uint8_t* buffer_u, |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 64 | const uint8_t* buffer_v, |
| 65 | int width, |
| 66 | int height, |
| 67 | int stride_y, |
| 68 | int stride_u, |
| 69 | int stride_v, |
| 70 | VideoRotation rotation); |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 71 | |
perkj@webrtc.org | 9f9ea7e | 2015-03-20 10:55:15 +0000 | [diff] [blame] | 72 | // CreateFrame: Sets the frame's members and buffers. If required size is |
| 73 | // bigger than allocated one, new buffers of adequate size will be allocated. |
| 74 | // |buffer| must be a packed I420 buffer. |
| 75 | // Return value: 0 on success, -1 on error. |
| 76 | int CreateFrame(const uint8_t* buffer, |
| 77 | int width, |
| 78 | int height, |
| 79 | VideoRotation rotation); |
| 80 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 81 | // Deep copy frame: If required size is bigger than allocated one, new |
| 82 | // buffers of adequate size will be allocated. |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 83 | // Return value: 0 on success, -1 on error. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 84 | int CopyFrame(const I420VideoFrame& videoFrame); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 85 | |
perkj@webrtc.org | af612d5 | 2015-03-18 09:51:05 +0000 | [diff] [blame] | 86 | // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a |
| 87 | // reference to the video buffer also retained by |videoFrame|. |
| 88 | void ShallowCopy(const I420VideoFrame& videoFrame); |
| 89 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 90 | // Make a copy of |this|. The caller owns the returned frame. |
| 91 | // Return value: a new frame on success, NULL on error. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 92 | I420VideoFrame* CloneFrame() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 93 | |
| 94 | // Swap Frame. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 95 | void SwapFrame(I420VideoFrame* videoFrame); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 96 | |
magjed@webrtc.org | 0d9bb8e | 2015-03-11 10:06:58 +0000 | [diff] [blame] | 97 | // Release frame buffer and reset time stamps. |
| 98 | void Reset(); |
| 99 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 100 | // Get pointer to buffer per plane. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 101 | uint8_t* buffer(PlaneType type); |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 102 | // Overloading with const. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 103 | const uint8_t* buffer(PlaneType type) const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 104 | |
| 105 | // Get allocated size per plane. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 106 | int allocated_size(PlaneType type) const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 107 | |
| 108 | // Get allocated stride per plane. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 109 | int stride(PlaneType type) const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 110 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 111 | // Get frame width. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 112 | int width() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 113 | |
| 114 | // Get frame height. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 115 | int height() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 116 | |
| 117 | // Set frame timestamp (90kHz). |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 118 | void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 119 | |
| 120 | // Get frame timestamp (90kHz). |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 121 | uint32_t timestamp() const { return timestamp_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 122 | |
| 123 | // Set capture ntp time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 124 | void set_ntp_time_ms(int64_t ntp_time_ms) { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 125 | ntp_time_ms_ = ntp_time_ms; |
| 126 | } |
| 127 | |
| 128 | // Get capture ntp time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 129 | int64_t ntp_time_ms() const { return ntp_time_ms_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 130 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 131 | // Naming convention for Coordination of Video Orientation. Please see |
| 132 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf |
| 133 | // |
| 134 | // "pending rotation" or "pending" = a frame that has a VideoRotation > 0. |
| 135 | // |
| 136 | // "not pending" = a frame that has a VideoRotation == 0. |
| 137 | // |
| 138 | // "apply rotation" = modify a frame from being "pending" to being "not |
| 139 | // pending" rotation (a no-op for "unrotated"). |
| 140 | // |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 141 | VideoRotation rotation() const { return rotation_; } |
| 142 | void set_rotation(VideoRotation rotation) { |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 143 | rotation_ = rotation; |
| 144 | } |
| 145 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 146 | // Set render time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 147 | void set_render_time_ms(int64_t render_time_ms) { |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 148 | render_time_ms_ = render_time_ms; |
| 149 | } |
| 150 | |
| 151 | // Get render time in miliseconds. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 152 | int64_t render_time_ms() const { return render_time_ms_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 153 | |
| 154 | // 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] | 155 | bool IsZeroSize() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 156 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 157 | // Return the handle of the underlying video frame. This is used when the |
| 158 | // frame is backed by a texture. The object should be destroyed when it is no |
| 159 | // longer in use, so the underlying resource can be freed. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 160 | void* native_handle() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 161 | |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 162 | // Return the underlying buffer. |
magjed@webrtc.org | 45cdcce | 2015-03-06 10:41:00 +0000 | [diff] [blame] | 163 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 164 | |
| 165 | private: |
magjed@webrtc.org | 2386d6d | 2015-03-05 14:03:08 +0000 | [diff] [blame] | 166 | // An opaque reference counted handle that stores the pixel data. |
| 167 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 168 | uint32_t timestamp_; |
| 169 | int64_t ntp_time_ms_; |
| 170 | int64_t render_time_ms_; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 171 | VideoRotation rotation_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | enum VideoFrameType { |
| 175 | kKeyFrame = 0, |
| 176 | kDeltaFrame = 1, |
| 177 | kGoldenFrame = 2, |
| 178 | kAltRefFrame = 3, |
| 179 | kSkipFrame = 4 |
| 180 | }; |
| 181 | |
| 182 | // TODO(pbos): Rename EncodedFrame and reformat this class' members. |
| 183 | class EncodedImage { |
| 184 | public: |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 185 | EncodedImage() : EncodedImage(nullptr, 0, 0) {} |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 186 | EncodedImage(uint8_t* buffer, size_t length, size_t size) |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 187 | : _buffer(buffer), _length(length), _size(size) {} |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 188 | |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 189 | uint32_t _encodedWidth = 0; |
| 190 | uint32_t _encodedHeight = 0; |
| 191 | uint32_t _timeStamp = 0; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 192 | // NTP time of the capture time in local timebase in milliseconds. |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 193 | int64_t ntp_time_ms_ = 0; |
| 194 | int64_t capture_time_ms_ = 0; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 195 | // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType). |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 196 | VideoFrameType _frameType = kDeltaFrame; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 197 | uint8_t* _buffer; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 198 | size_t _length; |
| 199 | size_t _size; |
pbos@webrtc.org | 4c8b930 | 2015-03-10 12:55:17 +0000 | [diff] [blame] | 200 | bool _completeFrame = false; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | } // namespace webrtc |
| 204 | #endif // WEBRTC_VIDEO_FRAME_H_ |
| 205 | |