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 18 #include <assert.h> 19 20 #include "webrtc/common_video/plane.h" 21 #include "webrtc/system_wrappers/interface/scoped_refptr.h" 22 #include "webrtc/typedefs.h" 23 24 /* 25 * I420VideoFrame includes support for a reference counted impl. 26 */ 27 28 namespace webrtc { 29 30 enum PlaneType { 31 kYPlane = 0, 32 kUPlane = 1, 33 kVPlane = 2, 34 kNumOfPlanes = 3 35 }; 36 37 class I420VideoFrame { 38 public: 39 I420VideoFrame(); 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;} 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. 54 // Return value: 0 on success, -1 on error. 55 virtual int CreateEmptyFrame(int width, int height, 56 int stride_y, int stride_u, int stride_v); 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. 60 // Return value: 0 on success, -1 on error. 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); 66 67 // Copy frame: If required size is bigger than allocated one, new buffers of 68 // adequate size will be allocated. 69 // Return value: 0 on success, -1 on error. 70 virtual int CopyFrame(const I420VideoFrame& videoFrame); 71 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 76 // Swap Frame. 77 virtual void SwapFrame(I420VideoFrame* videoFrame); 78 79 // Get pointer to buffer per plane. 80 virtual uint8_t* buffer(PlaneType type); 81 // Overloading with const. 82 virtual const uint8_t* buffer(PlaneType type) const; 83 84 // Get allocated size per plane. 85 virtual int allocated_size(PlaneType type) const; 86 87 // Get allocated stride per plane. 88 virtual int stride(PlaneType type) const; 89 90 // Set frame width. 91 virtual int set_width(int width); 92 93 // Set frame height. 94 virtual int set_height(int height); 95 96 // Get frame width. 97 virtual int width() const {return width_;} 98 99 // Get frame height. 100 virtual int height() const {return height_;} 101 102 // Set frame timestamp (90kHz). 103 virtual void set_timestamp(uint32_t timestamp) {timestamp_ = timestamp;} 104 105 // Get frame timestamp (90kHz). 106 virtual uint32_t timestamp() const {return timestamp_;} 107 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 116 // Set render time in miliseconds. 117 virtual void set_render_time_ms(int64_t render_time_ms) {render_time_ms_ = 118 render_time_ms;} 119 120 // Get render time in miliseconds. 121 virtual int64_t render_time_ms() const {return render_time_ms_;} 122 123 // Return true if underlying plane buffers are of zero size, false if not. 124 virtual bool IsZeroSize() const; 125 126 // Reset underlying plane buffers sizes to 0. This function doesn't 127 // clear memory. 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); 140 141 private: 142 // Get the pointer to a specific plane. 143 const Plane* GetPlane(PlaneType type) const; 144 // Overloading with non-const. 145 Plane* GetPlane(PlaneType type); 146 147 Plane y_plane_; 148 Plane u_plane_; 149 Plane v_plane_; 150 int width_; 151 int height_; 152 uint32_t timestamp_; 153 int64_t ntp_time_ms_; 154 int64_t render_time_ms_; 155 }; // I420VideoFrame 156 157 } // namespace webrtc 158 159 #endif // COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H 160