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 | |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 14 | #include <assert.h> |
| 15 | |
| 16 | #include "webrtc/common_video/plane.h" |
| 17 | // TODO(pbos): Remove scoped_refptr include (and AddRef/Release if they're not |
| 18 | // used). |
| 19 | #include "webrtc/system_wrappers/interface/scoped_refptr.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 20 | #include "webrtc/typedefs.h" |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 21 | #include "webrtc/common_video/rotation.h" |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 25 | enum PlaneType { |
| 26 | kYPlane = 0, |
| 27 | kUPlane = 1, |
| 28 | kVPlane = 2, |
| 29 | kNumOfPlanes = 3 |
| 30 | }; |
| 31 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 32 | class I420VideoFrame { |
| 33 | public: |
| 34 | I420VideoFrame(); |
| 35 | virtual ~I420VideoFrame(); |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 36 | // Infrastructure for refCount implementation. |
| 37 | // Implements dummy functions for reference counting so that non reference |
| 38 | // counted instantiation can be done. These functions should not be called |
| 39 | // when creating the frame with new I420VideoFrame(). |
| 40 | // Note: do not pass a I420VideoFrame created with new I420VideoFrame() or |
| 41 | // equivalent to a scoped_refptr or memory leak will occur. |
| 42 | virtual int32_t AddRef() { |
| 43 | assert(false); |
| 44 | return -1; |
| 45 | } |
| 46 | virtual int32_t Release() { |
| 47 | assert(false); |
| 48 | return -1; |
| 49 | } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 50 | |
| 51 | // CreateEmptyFrame: Sets frame dimensions and allocates buffers based |
| 52 | // on set dimensions - height and plane stride. |
| 53 | // If required size is bigger than the allocated one, new buffers of adequate |
| 54 | // size will be allocated. |
| 55 | // Return value: 0 on success, -1 on error. |
| 56 | virtual int CreateEmptyFrame(int width, |
| 57 | int height, |
| 58 | int stride_y, |
| 59 | int stride_u, |
| 60 | int stride_v); |
| 61 | |
| 62 | // CreateFrame: Sets the frame's members and buffers. If required size is |
| 63 | // bigger than allocated one, new buffers of adequate size will be allocated. |
| 64 | // Return value: 0 on success, -1 on error. |
| 65 | virtual int CreateFrame(int size_y, |
| 66 | const uint8_t* buffer_y, |
| 67 | int size_u, |
| 68 | const uint8_t* buffer_u, |
| 69 | int size_v, |
| 70 | const uint8_t* buffer_v, |
| 71 | int width, |
| 72 | int height, |
| 73 | int stride_y, |
| 74 | int stride_u, |
| 75 | int stride_v); |
| 76 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 77 | // TODO(guoweis): remove the previous CreateFrame when chromium has this code. |
| 78 | virtual int CreateFrame(int size_y, |
| 79 | const uint8_t* buffer_y, |
| 80 | int size_u, |
| 81 | const uint8_t* buffer_u, |
| 82 | int size_v, |
| 83 | const uint8_t* buffer_v, |
| 84 | int width, |
| 85 | int height, |
| 86 | int stride_y, |
| 87 | int stride_u, |
| 88 | int stride_v, |
| 89 | VideoRotation rotation); |
| 90 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 91 | // Copy frame: If required size is bigger than allocated one, new buffers of |
| 92 | // adequate size will be allocated. |
| 93 | // Return value: 0 on success, -1 on error. |
| 94 | virtual int CopyFrame(const I420VideoFrame& videoFrame); |
| 95 | |
| 96 | // Make a copy of |this|. The caller owns the returned frame. |
| 97 | // Return value: a new frame on success, NULL on error. |
| 98 | virtual I420VideoFrame* CloneFrame() const; |
| 99 | |
| 100 | // Swap Frame. |
| 101 | virtual void SwapFrame(I420VideoFrame* videoFrame); |
| 102 | |
| 103 | // Get pointer to buffer per plane. |
| 104 | virtual uint8_t* buffer(PlaneType type); |
| 105 | // Overloading with const. |
| 106 | virtual const uint8_t* buffer(PlaneType type) const; |
| 107 | |
| 108 | // Get allocated size per plane. |
| 109 | virtual int allocated_size(PlaneType type) const; |
| 110 | |
| 111 | // Get allocated stride per plane. |
| 112 | virtual int stride(PlaneType type) const; |
| 113 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 114 | // Get frame width. |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 115 | virtual int width() const { return width_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 116 | |
| 117 | // Get frame height. |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 118 | virtual int height() const { return height_; } |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 119 | |
| 120 | // Set frame timestamp (90kHz). |
| 121 | virtual void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; } |
| 122 | |
| 123 | // Get frame timestamp (90kHz). |
| 124 | virtual uint32_t timestamp() const { return timestamp_; } |
| 125 | |
| 126 | // Set capture ntp time in miliseconds. |
| 127 | virtual void set_ntp_time_ms(int64_t ntp_time_ms) { |
| 128 | ntp_time_ms_ = ntp_time_ms; |
| 129 | } |
| 130 | |
| 131 | // Get capture ntp time in miliseconds. |
| 132 | virtual int64_t ntp_time_ms() const { return ntp_time_ms_; } |
| 133 | |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 134 | // Naming convention for Coordination of Video Orientation. Please see |
| 135 | // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf |
| 136 | // |
| 137 | // "pending rotation" or "pending" = a frame that has a VideoRotation > 0. |
| 138 | // |
| 139 | // "not pending" = a frame that has a VideoRotation == 0. |
| 140 | // |
| 141 | // "apply rotation" = modify a frame from being "pending" to being "not |
| 142 | // pending" rotation (a no-op for "unrotated"). |
| 143 | // |
| 144 | virtual VideoRotation rotation() const { return rotation_; } |
| 145 | virtual void set_rotation(VideoRotation rotation) { |
| 146 | rotation_ = rotation; |
| 147 | } |
| 148 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 149 | // Set render time in miliseconds. |
| 150 | virtual void set_render_time_ms(int64_t render_time_ms) { |
| 151 | render_time_ms_ = render_time_ms; |
| 152 | } |
| 153 | |
| 154 | // Get render time in miliseconds. |
| 155 | virtual int64_t render_time_ms() const { return render_time_ms_; } |
| 156 | |
| 157 | // Return true if underlying plane buffers are of zero size, false if not. |
| 158 | virtual bool IsZeroSize() const; |
| 159 | |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 160 | // Return the handle of the underlying video frame. This is used when the |
| 161 | // frame is backed by a texture. The object should be destroyed when it is no |
| 162 | // longer in use, so the underlying resource can be freed. |
| 163 | virtual void* native_handle() const; |
| 164 | |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 165 | protected: |
| 166 | // Verifies legality of parameters. |
| 167 | // Return value: 0 on success, -1 on error. |
| 168 | virtual int CheckDimensions(int width, |
| 169 | int height, |
| 170 | int stride_y, |
| 171 | int stride_u, |
| 172 | int stride_v); |
| 173 | // TODO(magjed): Move these to an internal frame buffer instead. |
| 174 | int width_; |
| 175 | int height_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 176 | |
| 177 | private: |
tommi@webrtc.org | 1f94407 | 2015-03-04 17:33:55 +0000 | [diff] [blame] | 178 | // Get the pointer to a specific plane. |
| 179 | const Plane* GetPlane(PlaneType type) const; |
| 180 | // Overloading with non-const. |
| 181 | Plane* GetPlane(PlaneType type); |
| 182 | |
| 183 | Plane y_plane_; |
| 184 | Plane u_plane_; |
| 185 | Plane v_plane_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 186 | uint32_t timestamp_; |
| 187 | int64_t ntp_time_ms_; |
| 188 | int64_t render_time_ms_; |
guoweis@webrtc.org | 1226e92 | 2015-02-11 18:37:54 +0000 | [diff] [blame] | 189 | VideoRotation rotation_; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | enum VideoFrameType { |
| 193 | kKeyFrame = 0, |
| 194 | kDeltaFrame = 1, |
| 195 | kGoldenFrame = 2, |
| 196 | kAltRefFrame = 3, |
| 197 | kSkipFrame = 4 |
| 198 | }; |
| 199 | |
| 200 | // TODO(pbos): Rename EncodedFrame and reformat this class' members. |
| 201 | class EncodedImage { |
| 202 | public: |
| 203 | EncodedImage() |
| 204 | : _encodedWidth(0), |
| 205 | _encodedHeight(0), |
| 206 | _timeStamp(0), |
| 207 | capture_time_ms_(0), |
| 208 | _frameType(kDeltaFrame), |
| 209 | _buffer(NULL), |
| 210 | _length(0), |
| 211 | _size(0), |
| 212 | _completeFrame(false) {} |
| 213 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 214 | EncodedImage(uint8_t* buffer, size_t length, size_t size) |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 215 | : _encodedWidth(0), |
| 216 | _encodedHeight(0), |
| 217 | _timeStamp(0), |
| 218 | ntp_time_ms_(0), |
| 219 | capture_time_ms_(0), |
| 220 | _frameType(kDeltaFrame), |
| 221 | _buffer(buffer), |
| 222 | _length(length), |
| 223 | _size(size), |
| 224 | _completeFrame(false) {} |
| 225 | |
| 226 | uint32_t _encodedWidth; |
| 227 | uint32_t _encodedHeight; |
| 228 | uint32_t _timeStamp; |
| 229 | // NTP time of the capture time in local timebase in milliseconds. |
| 230 | int64_t ntp_time_ms_; |
| 231 | int64_t capture_time_ms_; |
pbos@webrtc.org | 273a414 | 2014-12-01 15:23:21 +0000 | [diff] [blame] | 232 | // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType). |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 233 | VideoFrameType _frameType; |
| 234 | uint8_t* _buffer; |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 235 | size_t _length; |
| 236 | size_t _size; |
pbos@webrtc.org | ab990ae | 2014-09-17 09:02:25 +0000 | [diff] [blame] | 237 | bool _completeFrame; |
| 238 | }; |
| 239 | |
| 240 | } // namespace webrtc |
| 241 | #endif // WEBRTC_VIDEO_FRAME_H_ |
| 242 | |