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