blob: 4579484debca49dd920d7c2ded63c19fe3aa7359 [file] [log] [blame]
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +00001/*
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.orge239bf02012-12-19 00:07:57 +000018#include "webrtc/common_video/plane.h"
wu@webrtc.org9dba5252013-08-05 20:36:57 +000019#include "webrtc/system_wrappers/interface/scoped_refptr.h"
mikhal@webrtc.orge239bf02012-12-19 00:07:57 +000020#include "webrtc/typedefs.h"
21
22/*
23 * I420VideoFrame includes support for a reference counted impl.
24 */
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000025
26namespace webrtc {
27
28enum PlaneType {
29 kYPlane = 0,
30 kUPlane = 1,
mikhal@webrtc.org0e6f5972012-09-24 20:35:40 +000031 kVPlane = 2,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000032 kNumOfPlanes = 3
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000033};
34
35class I420VideoFrame {
36 public:
37 I420VideoFrame();
mikhal@webrtc.orge239bf02012-12-19 00:07:57 +000038 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.org043ed9e2012-09-18 16:14:26 +000047
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.org9dba5252013-08-05 20:36:57 +000053 virtual int CreateEmptyFrame(int width, int height,
54 int stride_y, int stride_u, int stride_v);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000055
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.org9dba5252013-08-05 20:36:57 +000059 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.org043ed9e2012-09-18 16:14:26 +000064
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.org9dba5252013-08-05 20:36:57 +000068 virtual int CopyFrame(const I420VideoFrame& videoFrame);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000069
70 // Swap Frame.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000071 virtual void SwapFrame(I420VideoFrame* videoFrame);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000072
73 // Get pointer to buffer per plane.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000074 virtual uint8_t* buffer(PlaneType type);
mikhal@webrtc.org60ac6a62012-10-03 16:24:14 +000075 // Overloading with const.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000076 virtual const uint8_t* buffer(PlaneType type) const;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000077
78 // Get allocated size per plane.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000079 virtual int allocated_size(PlaneType type) const;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000080
81 // Get allocated stride per plane.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000082 virtual int stride(PlaneType type) const;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000083
84 // Set frame width.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000085 virtual int set_width(int width);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000086
87 // Set frame height.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000088 virtual int set_height(int height);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000089
90 // Get frame width.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000091 virtual int width() const {return width_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000092
93 // Get frame height.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000094 virtual int height() const {return height_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000095
96 // Set frame timestamp (90kHz).
wu@webrtc.org9dba5252013-08-05 20:36:57 +000097 virtual void set_timestamp(uint32_t timestamp) {timestamp_ = timestamp;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000098
99 // Get frame timestamp (90kHz).
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000100 virtual uint32_t timestamp() const {return timestamp_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000101
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000102 // 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.org043ed9e2012-09-18 16:14:26 +0000110 // Set render time in miliseconds.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000111 virtual void set_render_time_ms(int64_t render_time_ms) {render_time_ms_ =
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000112 render_time_ms;}
113
114 // Get render time in miliseconds.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000115 virtual int64_t render_time_ms() const {return render_time_ms_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000116
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000117 // Return true if underlying plane buffers are of zero size, false if not.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000118 virtual bool IsZeroSize() const;
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000119
120 // Reset underlying plane buffers sizes to 0. This function doesn't
121 // clear memory.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000122 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.orgdfc6b572012-10-15 16:12:09 +0000134
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000135 private:
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000136 // Get the pointer to a specific plane.
137 const Plane* GetPlane(PlaneType type) const;
mikhal@webrtc.org60ac6a62012-10-03 16:24:14 +0000138 // Overloading with non-const.
139 Plane* GetPlane(PlaneType type);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000140
141 Plane y_plane_;
142 Plane u_plane_;
143 Plane v_plane_;
144 int width_;
145 int height_;
146 uint32_t timestamp_;
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000147 int64_t ntp_time_ms_;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000148 int64_t render_time_ms_;
149}; // I420VideoFrame
150
151} // namespace webrtc
152
153#endif // COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H