blob: 2dd7c6134e15e6e8109bbb2bfe82b3421dc2342d [file] [log] [blame]
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +00001/*
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
14#include <assert.h>
15
16#include "webrtc/common_video/plane.h"
17// TODO(pbos): Remove scoped_refptr include (and AddRef/Release if they're not
18// used).
19#include "webrtc/system_wrappers/interface/scoped_refptr.h"
20#include "webrtc/typedefs.h"
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000021#include "webrtc/common_video/rotation.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000022
23namespace webrtc {
24
25enum PlaneType {
26 kYPlane = 0,
27 kUPlane = 1,
28 kVPlane = 2,
29 kNumOfPlanes = 3
30};
31
32class I420VideoFrame {
33 public:
34 I420VideoFrame();
35 virtual ~I420VideoFrame();
36 // Infrastructure for refCount implementation.
37 // Implements dummy functions for reference counting so that non reference
38 // counted instantiation can be done. These functions should not be called
39 // when creating the frame with new I420VideoFrame().
40 // Note: do not pass a I420VideoFrame created with new I420VideoFrame() or
41 // equivalent to a scoped_refptr or memory leak will occur.
42 virtual int32_t AddRef() {
43 assert(false);
44 return -1;
45 }
46 virtual int32_t Release() {
47 assert(false);
48 return -1;
49 }
50
51 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
52 // on set dimensions - height and plane stride.
53 // If required size is bigger than the allocated one, new buffers of adequate
54 // size will be allocated.
55 // Return value: 0 on success, -1 on error.
56 virtual int CreateEmptyFrame(int width,
57 int height,
58 int stride_y,
59 int stride_u,
60 int stride_v);
61
62 // CreateFrame: Sets the frame's members and buffers. If required size is
63 // bigger than allocated one, new buffers of adequate size will be allocated.
64 // Return value: 0 on success, -1 on error.
65 virtual int CreateFrame(int size_y,
66 const uint8_t* buffer_y,
67 int size_u,
68 const uint8_t* buffer_u,
69 int size_v,
70 const uint8_t* buffer_v,
71 int width,
72 int height,
73 int stride_y,
74 int stride_u,
75 int stride_v);
76
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000077 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
78 virtual int CreateFrame(int size_y,
79 const uint8_t* buffer_y,
80 int size_u,
81 const uint8_t* buffer_u,
82 int size_v,
83 const uint8_t* buffer_v,
84 int width,
85 int height,
86 int stride_y,
87 int stride_u,
88 int stride_v,
89 VideoRotation rotation);
90
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000091 // Copy frame: If required size is bigger than allocated one, new buffers of
92 // adequate size will be allocated.
93 // Return value: 0 on success, -1 on error.
94 virtual int CopyFrame(const I420VideoFrame& videoFrame);
95
96 // Make a copy of |this|. The caller owns the returned frame.
97 // Return value: a new frame on success, NULL on error.
98 virtual I420VideoFrame* CloneFrame() const;
99
100 // Swap Frame.
101 virtual void SwapFrame(I420VideoFrame* videoFrame);
102
103 // Get pointer to buffer per plane.
104 virtual uint8_t* buffer(PlaneType type);
105 // Overloading with const.
106 virtual const uint8_t* buffer(PlaneType type) const;
107
108 // Get allocated size per plane.
109 virtual int allocated_size(PlaneType type) const;
110
111 // Get allocated stride per plane.
112 virtual int stride(PlaneType type) const;
113
magjed@webrtc.org7400e0b2015-02-27 15:18:26 +0000114 // Set frame width.
115 virtual int set_width(int width);
116
117 // Set frame height.
118 virtual int set_height(int height);
119
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000120 // Get frame width.
121 virtual int width() const { return width_; }
122
123 // Get frame height.
124 virtual int height() const { return height_; }
125
126 // Set frame timestamp (90kHz).
127 virtual void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
128
129 // Get frame timestamp (90kHz).
130 virtual uint32_t timestamp() const { return timestamp_; }
131
132 // Set capture ntp time in miliseconds.
133 virtual void set_ntp_time_ms(int64_t ntp_time_ms) {
134 ntp_time_ms_ = ntp_time_ms;
135 }
136
137 // Get capture ntp time in miliseconds.
138 virtual int64_t ntp_time_ms() const { return ntp_time_ms_; }
139
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000140 // Naming convention for Coordination of Video Orientation. Please see
141 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
142 //
143 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
144 //
145 // "not pending" = a frame that has a VideoRotation == 0.
146 //
147 // "apply rotation" = modify a frame from being "pending" to being "not
148 // pending" rotation (a no-op for "unrotated").
149 //
150 virtual VideoRotation rotation() const { return rotation_; }
151 virtual void set_rotation(VideoRotation rotation) {
152 rotation_ = rotation;
153 }
154
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000155 // Set render time in miliseconds.
156 virtual void set_render_time_ms(int64_t render_time_ms) {
157 render_time_ms_ = render_time_ms;
158 }
159
160 // Get render time in miliseconds.
161 virtual int64_t render_time_ms() const { return render_time_ms_; }
162
163 // Return true if underlying plane buffers are of zero size, false if not.
164 virtual bool IsZeroSize() const;
165
magjed@webrtc.org7400e0b2015-02-27 15:18:26 +0000166 // Reset underlying plane buffers sizes to 0. This function doesn't
167 // clear memory.
168 virtual void ResetSize();
169
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000170 // Return the handle of the underlying video frame. This is used when the
171 // frame is backed by a texture. The object should be destroyed when it is no
172 // longer in use, so the underlying resource can be freed.
173 virtual void* native_handle() const;
174
175 protected:
176 // Verifies legality of parameters.
177 // Return value: 0 on success, -1 on error.
178 virtual int CheckDimensions(int width,
179 int height,
180 int stride_y,
181 int stride_u,
182 int stride_v);
183
184 private:
185 // Get the pointer to a specific plane.
186 const Plane* GetPlane(PlaneType type) const;
187 // Overloading with non-const.
188 Plane* GetPlane(PlaneType type);
189
190 Plane y_plane_;
191 Plane u_plane_;
192 Plane v_plane_;
magjed@webrtc.org7400e0b2015-02-27 15:18:26 +0000193 int width_;
194 int height_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000195 uint32_t timestamp_;
196 int64_t ntp_time_ms_;
197 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000198 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000199};
200
201enum VideoFrameType {
202 kKeyFrame = 0,
203 kDeltaFrame = 1,
204 kGoldenFrame = 2,
205 kAltRefFrame = 3,
206 kSkipFrame = 4
207};
208
209// TODO(pbos): Rename EncodedFrame and reformat this class' members.
210class EncodedImage {
211 public:
212 EncodedImage()
213 : _encodedWidth(0),
214 _encodedHeight(0),
215 _timeStamp(0),
216 capture_time_ms_(0),
217 _frameType(kDeltaFrame),
218 _buffer(NULL),
219 _length(0),
220 _size(0),
221 _completeFrame(false) {}
222
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000223 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000224 : _encodedWidth(0),
225 _encodedHeight(0),
226 _timeStamp(0),
227 ntp_time_ms_(0),
228 capture_time_ms_(0),
229 _frameType(kDeltaFrame),
230 _buffer(buffer),
231 _length(length),
232 _size(size),
233 _completeFrame(false) {}
234
235 uint32_t _encodedWidth;
236 uint32_t _encodedHeight;
237 uint32_t _timeStamp;
238 // NTP time of the capture time in local timebase in milliseconds.
239 int64_t ntp_time_ms_;
240 int64_t capture_time_ms_;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000241 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000242 VideoFrameType _frameType;
243 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000244 size_t _length;
245 size_t _size;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000246 bool _completeFrame;
247};
248
249} // namespace webrtc
250#endif // WEBRTC_VIDEO_FRAME_H_
251