blob: 5f7a572bdada643a3f9b37573d80b6e0e32a5dfc [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
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000018#include <assert.h>
19
mikhal@webrtc.orge239bf02012-12-19 00:07:57 +000020#include "webrtc/common_video/plane.h"
wu@webrtc.org9dba5252013-08-05 20:36:57 +000021#include "webrtc/system_wrappers/interface/scoped_refptr.h"
mikhal@webrtc.orge239bf02012-12-19 00:07:57 +000022#include "webrtc/typedefs.h"
23
24/*
25 * I420VideoFrame includes support for a reference counted impl.
26 */
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000027
28namespace webrtc {
29
30enum PlaneType {
31 kYPlane = 0,
32 kUPlane = 1,
mikhal@webrtc.org0e6f5972012-09-24 20:35:40 +000033 kVPlane = 2,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000034 kNumOfPlanes = 3
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000035};
36
37class I420VideoFrame {
38 public:
39 I420VideoFrame();
mikhal@webrtc.orge239bf02012-12-19 00:07:57 +000040 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;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000049
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.
wuchengli@chromium.org637c55f2014-05-28 07:00:51 +000054 // Return value: 0 on success, -1 on error.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000055 virtual int CreateEmptyFrame(int width, int height,
56 int stride_y, int stride_u, int stride_v);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000057
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.
wuchengli@chromium.org637c55f2014-05-28 07:00:51 +000060 // Return value: 0 on success, -1 on error.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000061 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);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000066
67 // Copy frame: If required size is bigger than allocated one, new buffers of
68 // adequate size will be allocated.
wuchengli@chromium.org637c55f2014-05-28 07:00:51 +000069 // Return value: 0 on success, -1 on error.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000070 virtual int CopyFrame(const I420VideoFrame& videoFrame);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000071
wuchengli@chromium.org637c55f2014-05-28 07:00:51 +000072 // 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
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000076 // Swap Frame.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000077 virtual void SwapFrame(I420VideoFrame* videoFrame);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000078
79 // Get pointer to buffer per plane.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000080 virtual uint8_t* buffer(PlaneType type);
mikhal@webrtc.org60ac6a62012-10-03 16:24:14 +000081 // Overloading with const.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000082 virtual const uint8_t* buffer(PlaneType type) const;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000083
84 // Get allocated size per plane.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000085 virtual int allocated_size(PlaneType type) const;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000086
87 // Get allocated stride per plane.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000088 virtual int stride(PlaneType type) const;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000089
90 // Set frame width.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000091 virtual int set_width(int width);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000092
93 // Set frame height.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000094 virtual int set_height(int height);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000095
96 // Get frame width.
wu@webrtc.org9dba5252013-08-05 20:36:57 +000097 virtual int width() const {return width_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +000098
99 // Get frame height.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000100 virtual int height() const {return height_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000101
102 // Set frame timestamp (90kHz).
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000103 virtual void set_timestamp(uint32_t timestamp) {timestamp_ = timestamp;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000104
105 // Get frame timestamp (90kHz).
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000106 virtual uint32_t timestamp() const {return timestamp_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000107
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000108 // 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
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000116 // Set render time in miliseconds.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000117 virtual void set_render_time_ms(int64_t render_time_ms) {render_time_ms_ =
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000118 render_time_ms;}
119
120 // Get render time in miliseconds.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000121 virtual int64_t render_time_ms() const {return render_time_ms_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000122
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000123 // Return true if underlying plane buffers are of zero size, false if not.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000124 virtual bool IsZeroSize() const;
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000125
126 // Reset underlying plane buffers sizes to 0. This function doesn't
127 // clear memory.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000128 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);
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000140
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000141 private:
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000142 // Get the pointer to a specific plane.
143 const Plane* GetPlane(PlaneType type) const;
mikhal@webrtc.org60ac6a62012-10-03 16:24:14 +0000144 // Overloading with non-const.
145 Plane* GetPlane(PlaneType type);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000146
147 Plane y_plane_;
148 Plane u_plane_;
149 Plane v_plane_;
150 int width_;
151 int height_;
152 uint32_t timestamp_;
wu@webrtc.org6c75c982014-04-15 17:46:33 +0000153 int64_t ntp_time_ms_;
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000154 int64_t render_time_ms_;
155}; // I420VideoFrame
156
157} // namespace webrtc
158
159#endif // COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H