mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H |
| 12 | #define COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H |
| 13 | |
| 14 | // I420VideoFrame class |
| 15 | // |
| 16 | // Storing and handling of YUV (I420) video frames. |
| 17 | |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | |
mikhal@webrtc.org | e239bf0 | 2012-12-19 00:07:57 +0000 | [diff] [blame] | 20 | #include "webrtc/common_video/plane.h" |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 21 | #include "webrtc/system_wrappers/interface/scoped_refptr.h" |
mikhal@webrtc.org | e239bf0 | 2012-12-19 00:07:57 +0000 | [diff] [blame] | 22 | #include "webrtc/typedefs.h" |
| 23 | |
| 24 | /* |
| 25 | * I420VideoFrame includes support for a reference counted impl. |
| 26 | */ |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | enum PlaneType { |
| 31 | kYPlane = 0, |
| 32 | kUPlane = 1, |
mikhal@webrtc.org | 0e6f597 | 2012-09-24 20:35:40 +0000 | [diff] [blame] | 33 | kVPlane = 2, |
mikhal@webrtc.org | 9fedff7 | 2012-10-24 18:33:04 +0000 | [diff] [blame] | 34 | kNumOfPlanes = 3 |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | class I420VideoFrame { |
| 38 | public: |
| 39 | I420VideoFrame(); |
mikhal@webrtc.org | e239bf0 | 2012-12-19 00:07:57 +0000 | [diff] [blame] | 40 | virtual ~I420VideoFrame(); |
| 41 | // Infrastructure for refCount implementation. |
| 42 | // Implements dummy functions for reference counting so that non reference |
| 43 | // counted instantiation can be done. These functions should not be called |
| 44 | // when creating the frame with new I420VideoFrame(). |
| 45 | // Note: do not pass a I420VideoFrame created with new I420VideoFrame() or |
| 46 | // equivalent to a scoped_refptr or memory leak will occur. |
| 47 | virtual int32_t AddRef() {assert(false); return -1;} |
| 48 | virtual int32_t Release() {assert(false); return -1;} |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 49 | |
| 50 | // CreateEmptyFrame: Sets frame dimensions and allocates buffers based |
| 51 | // on set dimensions - height and plane stride. |
| 52 | // If required size is bigger than the allocated one, new buffers of adequate |
| 53 | // size will be allocated. |
wuchengli@chromium.org | 637c55f | 2014-05-28 07:00:51 +0000 | [diff] [blame^] | 54 | // Return value: 0 on success, -1 on error. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 55 | virtual int CreateEmptyFrame(int width, int height, |
| 56 | int stride_y, int stride_u, int stride_v); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 57 | |
| 58 | // CreateFrame: Sets the frame's members and buffers. If required size is |
| 59 | // bigger than allocated one, new buffers of adequate size will be allocated. |
wuchengli@chromium.org | 637c55f | 2014-05-28 07:00:51 +0000 | [diff] [blame^] | 60 | // Return value: 0 on success, -1 on error. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 61 | virtual int CreateFrame(int size_y, const uint8_t* buffer_y, |
| 62 | int size_u, const uint8_t* buffer_u, |
| 63 | int size_v, const uint8_t* buffer_v, |
| 64 | int width, int height, |
| 65 | int stride_y, int stride_u, int stride_v); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 66 | |
| 67 | // Copy frame: If required size is bigger than allocated one, new buffers of |
| 68 | // adequate size will be allocated. |
wuchengli@chromium.org | 637c55f | 2014-05-28 07:00:51 +0000 | [diff] [blame^] | 69 | // Return value: 0 on success, -1 on error. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 70 | virtual int CopyFrame(const I420VideoFrame& videoFrame); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 71 | |
wuchengli@chromium.org | 637c55f | 2014-05-28 07:00:51 +0000 | [diff] [blame^] | 72 | // Make a copy of |this|. The caller owns the returned frame. |
| 73 | // Return value: a new frame on success, NULL on error. |
| 74 | virtual I420VideoFrame* CloneFrame() const; |
| 75 | |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 76 | // Swap Frame. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 77 | virtual void SwapFrame(I420VideoFrame* videoFrame); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 78 | |
| 79 | // Get pointer to buffer per plane. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 80 | virtual uint8_t* buffer(PlaneType type); |
mikhal@webrtc.org | 60ac6a6 | 2012-10-03 16:24:14 +0000 | [diff] [blame] | 81 | // Overloading with const. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 82 | virtual const uint8_t* buffer(PlaneType type) const; |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 83 | |
| 84 | // Get allocated size per plane. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 85 | virtual int allocated_size(PlaneType type) const; |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 86 | |
| 87 | // Get allocated stride per plane. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 88 | virtual int stride(PlaneType type) const; |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 89 | |
| 90 | // Set frame width. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 91 | virtual int set_width(int width); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 92 | |
| 93 | // Set frame height. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 94 | virtual int set_height(int height); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 95 | |
| 96 | // Get frame width. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 97 | virtual int width() const {return width_;} |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 98 | |
| 99 | // Get frame height. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 100 | virtual int height() const {return height_;} |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 101 | |
| 102 | // Set frame timestamp (90kHz). |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 103 | virtual void set_timestamp(uint32_t timestamp) {timestamp_ = timestamp;} |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 104 | |
| 105 | // Get frame timestamp (90kHz). |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 106 | virtual uint32_t timestamp() const {return timestamp_;} |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 107 | |
wu@webrtc.org | 6c75c98 | 2014-04-15 17:46:33 +0000 | [diff] [blame] | 108 | // Set capture ntp time in miliseconds. |
| 109 | virtual void set_ntp_time_ms(int64_t ntp_time_ms) { |
| 110 | ntp_time_ms_ = ntp_time_ms; |
| 111 | } |
| 112 | |
| 113 | // Get capture ntp time in miliseconds. |
| 114 | virtual int64_t ntp_time_ms() const {return ntp_time_ms_;} |
| 115 | |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 116 | // Set render time in miliseconds. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 117 | virtual void set_render_time_ms(int64_t render_time_ms) {render_time_ms_ = |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 118 | render_time_ms;} |
| 119 | |
| 120 | // Get render time in miliseconds. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 121 | virtual int64_t render_time_ms() const {return render_time_ms_;} |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 122 | |
mikhal@webrtc.org | dfc6b57 | 2012-10-15 16:12:09 +0000 | [diff] [blame] | 123 | // Return true if underlying plane buffers are of zero size, false if not. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 124 | virtual bool IsZeroSize() const; |
mikhal@webrtc.org | dfc6b57 | 2012-10-15 16:12:09 +0000 | [diff] [blame] | 125 | |
| 126 | // Reset underlying plane buffers sizes to 0. This function doesn't |
| 127 | // clear memory. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 128 | virtual void ResetSize(); |
| 129 | |
| 130 | // Return the handle of the underlying video frame. This is used when the |
| 131 | // frame is backed by a texture. The object should be destroyed when it is no |
| 132 | // longer in use, so the underlying resource can be freed. |
| 133 | virtual void* native_handle() const; |
| 134 | |
| 135 | protected: |
| 136 | // Verifies legality of parameters. |
| 137 | // Return value: 0 on success, -1 on error. |
| 138 | virtual int CheckDimensions(int width, int height, |
| 139 | int stride_y, int stride_u, int stride_v); |
mikhal@webrtc.org | dfc6b57 | 2012-10-15 16:12:09 +0000 | [diff] [blame] | 140 | |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 141 | private: |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 142 | // Get the pointer to a specific plane. |
| 143 | const Plane* GetPlane(PlaneType type) const; |
mikhal@webrtc.org | 60ac6a6 | 2012-10-03 16:24:14 +0000 | [diff] [blame] | 144 | // Overloading with non-const. |
| 145 | Plane* GetPlane(PlaneType type); |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 146 | |
| 147 | Plane y_plane_; |
| 148 | Plane u_plane_; |
| 149 | Plane v_plane_; |
| 150 | int width_; |
| 151 | int height_; |
| 152 | uint32_t timestamp_; |
wu@webrtc.org | 6c75c98 | 2014-04-15 17:46:33 +0000 | [diff] [blame] | 153 | int64_t ntp_time_ms_; |
mikhal@webrtc.org | 043ed9e | 2012-09-18 16:14:26 +0000 | [diff] [blame] | 154 | int64_t render_time_ms_; |
| 155 | }; // I420VideoFrame |
| 156 | |
| 157 | } // namespace webrtc |
| 158 | |
| 159 | #endif // COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H |