blob: 3f673cd3a9fce45aa840a78fe13e7f3de6f44e79 [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
tommi@webrtc.org1f944072015-03-04 17:33:55 +000014#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"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000020#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
tommi@webrtc.org1f944072015-03-04 17:33:55 +000025enum PlaneType {
26 kYPlane = 0,
27 kUPlane = 1,
28 kVPlane = 2,
29 kNumOfPlanes = 3
30};
31
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000032class I420VideoFrame {
33 public:
34 I420VideoFrame();
35 virtual ~I420VideoFrame();
tommi@webrtc.org1f944072015-03-04 17:33:55 +000036 // 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 }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000050
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
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000114 // Get frame width.
tommi@webrtc.org1f944072015-03-04 17:33:55 +0000115 virtual int width() const { return width_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000116
117 // Get frame height.
tommi@webrtc.org1f944072015-03-04 17:33:55 +0000118 virtual int height() const { return height_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000119
120 // Set frame timestamp (90kHz).
121 virtual void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
122
123 // Get frame timestamp (90kHz).
124 virtual uint32_t timestamp() const { return timestamp_; }
125
126 // Set capture ntp time in miliseconds.
127 virtual void set_ntp_time_ms(int64_t ntp_time_ms) {
128 ntp_time_ms_ = ntp_time_ms;
129 }
130
131 // Get capture ntp time in miliseconds.
132 virtual int64_t ntp_time_ms() const { return ntp_time_ms_; }
133
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000134 // Naming convention for Coordination of Video Orientation. Please see
135 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
136 //
137 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
138 //
139 // "not pending" = a frame that has a VideoRotation == 0.
140 //
141 // "apply rotation" = modify a frame from being "pending" to being "not
142 // pending" rotation (a no-op for "unrotated").
143 //
144 virtual VideoRotation rotation() const { return rotation_; }
145 virtual void set_rotation(VideoRotation rotation) {
146 rotation_ = rotation;
147 }
148
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000149 // Set render time in miliseconds.
150 virtual void set_render_time_ms(int64_t render_time_ms) {
151 render_time_ms_ = render_time_ms;
152 }
153
154 // Get render time in miliseconds.
155 virtual int64_t render_time_ms() const { return render_time_ms_; }
156
157 // Return true if underlying plane buffers are of zero size, false if not.
158 virtual bool IsZeroSize() const;
159
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000160 // Return the handle of the underlying video frame. This is used when the
161 // frame is backed by a texture. The object should be destroyed when it is no
162 // longer in use, so the underlying resource can be freed.
163 virtual void* native_handle() const;
164
tommi@webrtc.org1f944072015-03-04 17:33:55 +0000165 protected:
166 // Verifies legality of parameters.
167 // Return value: 0 on success, -1 on error.
168 virtual int CheckDimensions(int width,
169 int height,
170 int stride_y,
171 int stride_u,
172 int stride_v);
173 // TODO(magjed): Move these to an internal frame buffer instead.
174 int width_;
175 int height_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000176
177 private:
tommi@webrtc.org1f944072015-03-04 17:33:55 +0000178 // Get the pointer to a specific plane.
179 const Plane* GetPlane(PlaneType type) const;
180 // Overloading with non-const.
181 Plane* GetPlane(PlaneType type);
182
183 Plane y_plane_;
184 Plane u_plane_;
185 Plane v_plane_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000186 uint32_t timestamp_;
187 int64_t ntp_time_ms_;
188 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000189 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000190};
191
192enum VideoFrameType {
193 kKeyFrame = 0,
194 kDeltaFrame = 1,
195 kGoldenFrame = 2,
196 kAltRefFrame = 3,
197 kSkipFrame = 4
198};
199
200// TODO(pbos): Rename EncodedFrame and reformat this class' members.
201class EncodedImage {
202 public:
203 EncodedImage()
204 : _encodedWidth(0),
205 _encodedHeight(0),
206 _timeStamp(0),
207 capture_time_ms_(0),
208 _frameType(kDeltaFrame),
209 _buffer(NULL),
210 _length(0),
211 _size(0),
212 _completeFrame(false) {}
213
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000214 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000215 : _encodedWidth(0),
216 _encodedHeight(0),
217 _timeStamp(0),
218 ntp_time_ms_(0),
219 capture_time_ms_(0),
220 _frameType(kDeltaFrame),
221 _buffer(buffer),
222 _length(length),
223 _size(size),
224 _completeFrame(false) {}
225
226 uint32_t _encodedWidth;
227 uint32_t _encodedHeight;
228 uint32_t _timeStamp;
229 // NTP time of the capture time in local timebase in milliseconds.
230 int64_t ntp_time_ms_;
231 int64_t capture_time_ms_;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000232 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000233 VideoFrameType _frameType;
234 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000235 size_t _length;
236 size_t _size;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000237 bool _completeFrame;
238};
239
240} // namespace webrtc
241#endif // WEBRTC_VIDEO_FRAME_H_
242