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