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