blob: 45f2ec3039c7861e923d562defb7ba2317c1b0c7 [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
102 // Set render time in miliseconds.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000103 virtual void set_render_time_ms(int64_t render_time_ms) {render_time_ms_ =
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000104 render_time_ms;}
105
106 // Get render time in miliseconds.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000107 virtual int64_t render_time_ms() const {return render_time_ms_;}
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000108
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000109 // Return true if underlying plane buffers are of zero size, false if not.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000110 virtual bool IsZeroSize() const;
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000111
112 // Reset underlying plane buffers sizes to 0. This function doesn't
113 // clear memory.
wu@webrtc.org9dba5252013-08-05 20:36:57 +0000114 virtual void ResetSize();
115
116 // Return the handle of the underlying video frame. This is used when the
117 // frame is backed by a texture. The object should be destroyed when it is no
118 // longer in use, so the underlying resource can be freed.
119 virtual void* native_handle() const;
120
121 protected:
122 // Verifies legality of parameters.
123 // Return value: 0 on success, -1 on error.
124 virtual int CheckDimensions(int width, int height,
125 int stride_y, int stride_u, int stride_v);
mikhal@webrtc.orgdfc6b572012-10-15 16:12:09 +0000126
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000127 private:
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000128 // Get the pointer to a specific plane.
129 const Plane* GetPlane(PlaneType type) const;
mikhal@webrtc.org60ac6a62012-10-03 16:24:14 +0000130 // Overloading with non-const.
131 Plane* GetPlane(PlaneType type);
mikhal@webrtc.org043ed9e2012-09-18 16:14:26 +0000132
133 Plane y_plane_;
134 Plane u_plane_;
135 Plane v_plane_;
136 int width_;
137 int height_;
138 uint32_t timestamp_;
139 int64_t render_time_ms_;
140}; // I420VideoFrame
141
142} // namespace webrtc
143
144#endif // COMMON_VIDEO_INTERFACE_I420_VIDEO_FRAME_H