blob: 67699a49eb3f3c1c6d76371371ea34c7d6f55848 [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
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000014#include "webrtc/base/scoped_ref_ptr.h"
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000015#include "webrtc/common_video/interface/native_handle.h"
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000016#include "webrtc/common_video/interface/video_frame_buffer.h"
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000017#include "webrtc/common_video/rotation.h"
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000018#include "webrtc/typedefs.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000019
20namespace webrtc {
21
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000022class I420VideoFrame {
23 public:
24 I420VideoFrame();
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000025 I420VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
26 uint32_t timestamp,
27 int64_t render_time_ms,
28 VideoRotation rotation);
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000029 I420VideoFrame(NativeHandle* handle,
30 int width,
31 int height,
32 uint32_t timestamp,
33 int64_t render_time_ms);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000034
pbos@webrtc.org5a477a02015-03-18 14:11:39 +000035 // TODO(pbos): Make all create/copy functions void, they should not be able to
36 // fail (which should be DCHECK/CHECKed instead).
37
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000038 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
39 // on set dimensions - height and plane stride.
40 // If required size is bigger than the allocated one, new buffers of adequate
41 // size will be allocated.
42 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000043 int CreateEmptyFrame(int width,
44 int height,
45 int stride_y,
46 int stride_u,
47 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000048
49 // CreateFrame: Sets the frame's members and buffers. If required size is
50 // bigger than allocated one, new buffers of adequate size will be allocated.
51 // Return value: 0 on success, -1 on error.
hbos@webrtc.org93d9d652015-03-16 13:26:00 +000052 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000053 const uint8_t* buffer_u,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000054 const uint8_t* buffer_v,
55 int width,
56 int height,
57 int stride_y,
58 int stride_u,
59 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000060
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000061 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
hbos@webrtc.org93d9d652015-03-16 13:26:00 +000062 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000063 const uint8_t* buffer_u,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000064 const uint8_t* buffer_v,
65 int width,
66 int height,
67 int stride_y,
68 int stride_u,
69 int stride_v,
70 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000071
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000072 // Deep copy frame: If required size is bigger than allocated one, new
73 // buffers of adequate size will be allocated.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000074 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000075 int CopyFrame(const I420VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000076
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000077 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
78 // reference to the video buffer also retained by |videoFrame|.
79 void ShallowCopy(const I420VideoFrame& videoFrame);
80
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000081 // Make a copy of |this|. The caller owns the returned frame.
82 // Return value: a new frame on success, NULL on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000083 I420VideoFrame* CloneFrame() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000084
85 // Swap Frame.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000086 void SwapFrame(I420VideoFrame* videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000087
magjed@webrtc.org0d9bb8e2015-03-11 10:06:58 +000088 // Release frame buffer and reset time stamps.
89 void Reset();
90
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000091 // Get pointer to buffer per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000092 uint8_t* buffer(PlaneType type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000093 // Overloading with const.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000094 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000095
96 // Get allocated size per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000097 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098
99 // Get allocated stride per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000100 int stride(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000101
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000102 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000103 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000104
105 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000106 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000107
108 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000109 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000110
111 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000112 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000113
114 // Set capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000115 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000116 ntp_time_ms_ = ntp_time_ms;
117 }
118
119 // Get capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000120 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000121
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000122 // Naming convention for Coordination of Video Orientation. Please see
123 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
124 //
125 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
126 //
127 // "not pending" = a frame that has a VideoRotation == 0.
128 //
129 // "apply rotation" = modify a frame from being "pending" to being "not
130 // pending" rotation (a no-op for "unrotated").
131 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000132 VideoRotation rotation() const { return rotation_; }
133 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000134 rotation_ = rotation;
135 }
136
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000137 // Set render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000138 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000139 render_time_ms_ = render_time_ms;
140 }
141
142 // Get render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000143 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000144
145 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000146 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000147
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000148 // Return the handle of the underlying video frame. This is used when the
149 // frame is backed by a texture. The object should be destroyed when it is no
150 // longer in use, so the underlying resource can be freed.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000151 void* native_handle() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000152
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000153 // Return the underlying buffer.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000154 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000155
156 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000157 // An opaque reference counted handle that stores the pixel data.
158 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000159 uint32_t timestamp_;
160 int64_t ntp_time_ms_;
161 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000162 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000163};
164
165enum VideoFrameType {
166 kKeyFrame = 0,
167 kDeltaFrame = 1,
168 kGoldenFrame = 2,
169 kAltRefFrame = 3,
170 kSkipFrame = 4
171};
172
173// TODO(pbos): Rename EncodedFrame and reformat this class' members.
174class EncodedImage {
175 public:
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000176 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000177 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000178 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000179
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000180 uint32_t _encodedWidth = 0;
181 uint32_t _encodedHeight = 0;
182 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000183 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000184 int64_t ntp_time_ms_ = 0;
185 int64_t capture_time_ms_ = 0;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000186 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000187 VideoFrameType _frameType = kDeltaFrame;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000188 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000189 size_t _length;
190 size_t _size;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000191 bool _completeFrame = false;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000192};
193
194} // namespace webrtc
195#endif // WEBRTC_VIDEO_FRAME_H_
196