blob: 2fc8b9ec68e61808a8ec60c785b03027004d11d0 [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
35 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
36 // on set dimensions - height and plane stride.
37 // If required size is bigger than the allocated one, new buffers of adequate
38 // size will be allocated.
39 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000040 int CreateEmptyFrame(int width,
41 int height,
42 int stride_y,
43 int stride_u,
44 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000045
46 // CreateFrame: Sets the frame's members and buffers. If required size is
47 // bigger than allocated one, new buffers of adequate size will be allocated.
48 // Return value: 0 on success, -1 on error.
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +000049 // TODO(magjed): Remove unnecessary buffer size arguments.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000050 int CreateFrame(int size_y,
51 const uint8_t* buffer_y,
52 int size_u,
53 const uint8_t* buffer_u,
54 int size_v,
55 const uint8_t* buffer_v,
56 int width,
57 int height,
58 int stride_y,
59 int stride_u,
60 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000061
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000062 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000063 int CreateFrame(int size_y,
64 const uint8_t* buffer_y,
65 int size_u,
66 const uint8_t* buffer_u,
67 int size_v,
68 const uint8_t* buffer_v,
69 int width,
70 int height,
71 int stride_y,
72 int stride_u,
73 int stride_v,
74 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000075
magjed@webrtc.orgd7452a02015-03-10 15:12:26 +000076 // Copy frame: If required size is bigger than allocated one, new buffers of
77 // adequate size will be allocated.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000078 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000079 int CopyFrame(const I420VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000080
81 // 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
88 // Get pointer to buffer per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000089 uint8_t* buffer(PlaneType type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000090 // Overloading with const.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000091 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000092
93 // Get allocated size per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000094 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000095
96 // Get allocated stride per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000097 int stride(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000099 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000100 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000101
102 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000103 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000104
105 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000106 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000107
108 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000109 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000110
111 // Set capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000112 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000113 ntp_time_ms_ = ntp_time_ms;
114 }
115
116 // Get capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000117 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000118
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000119 // Naming convention for Coordination of Video Orientation. Please see
120 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
121 //
122 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
123 //
124 // "not pending" = a frame that has a VideoRotation == 0.
125 //
126 // "apply rotation" = modify a frame from being "pending" to being "not
127 // pending" rotation (a no-op for "unrotated").
128 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000129 VideoRotation rotation() const { return rotation_; }
130 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000131 rotation_ = rotation;
132 }
133
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000134 // Set render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000135 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000136 render_time_ms_ = render_time_ms;
137 }
138
139 // Get render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000140 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000141
142 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000143 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000144
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000145 // Return the handle of the underlying video frame. This is used when the
146 // frame is backed by a texture. The object should be destroyed when it is no
147 // longer in use, so the underlying resource can be freed.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000148 void* native_handle() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000149
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000150 // Return the underlying buffer.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000151 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000152
153 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000154 // An opaque reference counted handle that stores the pixel data.
155 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000156 uint32_t timestamp_;
157 int64_t ntp_time_ms_;
158 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000159 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000160};
161
162enum VideoFrameType {
163 kKeyFrame = 0,
164 kDeltaFrame = 1,
165 kGoldenFrame = 2,
166 kAltRefFrame = 3,
167 kSkipFrame = 4
168};
169
170// TODO(pbos): Rename EncodedFrame and reformat this class' members.
171class EncodedImage {
172 public:
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000173 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000174 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000175 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000176
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000177 uint32_t _encodedWidth = 0;
178 uint32_t _encodedHeight = 0;
179 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000180 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000181 int64_t ntp_time_ms_ = 0;
182 int64_t capture_time_ms_ = 0;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000183 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000184 VideoFrameType _frameType = kDeltaFrame;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000185 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000186 size_t _length;
187 size_t _size;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000188 bool _completeFrame = false;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000189};
190
191} // namespace webrtc
192#endif // WEBRTC_VIDEO_FRAME_H_
193