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