blob: d2f94fe0b5abcd3488d1ec4c014fa71e5e1f0ae3 [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.org9f9ea7e2015-03-20 10:55:15 +000072 // CreateFrame: Sets the frame's members and buffers. If required size is
73 // bigger than allocated one, new buffers of adequate size will be allocated.
74 // |buffer| must be a packed I420 buffer.
75 // Return value: 0 on success, -1 on error.
76 int CreateFrame(const uint8_t* buffer,
77 int width,
78 int height,
79 VideoRotation rotation);
80
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000081 // Deep copy frame: If required size is bigger than allocated one, new
82 // buffers of adequate size will be allocated.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000083 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000084 int CopyFrame(const I420VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000085
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000086 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
87 // reference to the video buffer also retained by |videoFrame|.
88 void ShallowCopy(const I420VideoFrame& videoFrame);
89
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000090 // Make a copy of |this|. The caller owns the returned frame.
91 // Return value: a new frame on success, NULL on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000092 I420VideoFrame* CloneFrame() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000093
94 // Swap Frame.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000095 void SwapFrame(I420VideoFrame* videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000096
magjed@webrtc.org0d9bb8e2015-03-11 10:06:58 +000097 // Release frame buffer and reset time stamps.
98 void Reset();
99
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000100 // Get pointer to buffer per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000101 uint8_t* buffer(PlaneType type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000102 // Overloading with const.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000103 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000104
105 // Get allocated size per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000106 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000107
108 // Get allocated stride per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000109 int stride(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000110
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000111 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000112 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000113
114 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000115 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000116
117 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000118 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000119
120 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000121 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000122
123 // Set capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000124 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000125 ntp_time_ms_ = ntp_time_ms;
126 }
127
128 // Get capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000129 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000130
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000131 // Naming convention for Coordination of Video Orientation. Please see
132 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
133 //
134 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
135 //
136 // "not pending" = a frame that has a VideoRotation == 0.
137 //
138 // "apply rotation" = modify a frame from being "pending" to being "not
139 // pending" rotation (a no-op for "unrotated").
140 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000141 VideoRotation rotation() const { return rotation_; }
142 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000143 rotation_ = rotation;
144 }
145
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000146 // Set render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000147 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000148 render_time_ms_ = render_time_ms;
149 }
150
151 // Get render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000152 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000153
154 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000155 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000156
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000157 // Return the handle of the underlying video frame. This is used when the
158 // frame is backed by a texture. The object should be destroyed when it is no
159 // longer in use, so the underlying resource can be freed.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000160 void* native_handle() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000161
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000162 // Return the underlying buffer.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000163 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000164
magjed@webrtc.orgd4e7d492015-03-23 12:27:55 +0000165 // Set the underlying buffer.
166 void set_video_frame_buffer(
167 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
168
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000169 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000170 // An opaque reference counted handle that stores the pixel data.
171 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000172 uint32_t timestamp_;
173 int64_t ntp_time_ms_;
174 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000175 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000176};
177
178enum VideoFrameType {
179 kKeyFrame = 0,
180 kDeltaFrame = 1,
181 kGoldenFrame = 2,
182 kAltRefFrame = 3,
183 kSkipFrame = 4
184};
185
186// TODO(pbos): Rename EncodedFrame and reformat this class' members.
187class EncodedImage {
188 public:
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000189 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000190 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000191 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000192
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000193 uint32_t _encodedWidth = 0;
194 uint32_t _encodedHeight = 0;
195 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000196 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000197 int64_t ntp_time_ms_ = 0;
198 int64_t capture_time_ms_ = 0;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000199 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000200 VideoFrameType _frameType = kDeltaFrame;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000201 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000202 size_t _length;
203 size_t _size;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000204 bool _completeFrame = false;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000205};
206
207} // namespace webrtc
208#endif // WEBRTC_VIDEO_FRAME_H_