blob: 626c01356cf45318e26a9076d396ffc202b0e603 [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"
15#include "webrtc/common_video/interface/video_frame_buffer.h"
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000016#include "webrtc/common_video/rotation.h"
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000017#include "webrtc/typedefs.h"
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000018
19namespace webrtc {
20
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070021class VideoFrame {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000022 public:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070023 VideoFrame();
24 VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
25 uint32_t timestamp,
26 int64_t render_time_ms,
27 VideoRotation rotation);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000028
pbos@webrtc.org5a477a02015-03-18 14:11:39 +000029 // TODO(pbos): Make all create/copy functions void, they should not be able to
henrikg91d6ede2015-09-17 00:24:34 -070030 // fail (which should be RTC_DCHECK/CHECKed instead).
pbos@webrtc.org5a477a02015-03-18 14:11:39 +000031
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000032 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
33 // on set dimensions - height and plane stride.
34 // If required size is bigger than the allocated one, new buffers of adequate
35 // size will be allocated.
36 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000037 int CreateEmptyFrame(int width,
38 int height,
39 int stride_y,
40 int stride_u,
41 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000042
43 // CreateFrame: Sets the frame's members and buffers. If required size is
44 // bigger than allocated one, new buffers of adequate size will be allocated.
45 // Return value: 0 on success, -1 on error.
hbos@webrtc.org93d9d652015-03-16 13:26:00 +000046 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000047 const uint8_t* buffer_u,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000048 const uint8_t* buffer_v,
49 int width,
50 int height,
51 int stride_y,
52 int stride_u,
53 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000054
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000055 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
hbos@webrtc.org93d9d652015-03-16 13:26:00 +000056 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000057 const uint8_t* buffer_u,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000058 const uint8_t* buffer_v,
59 int width,
60 int height,
61 int stride_y,
62 int stride_u,
63 int stride_v,
64 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000065
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000066 // CreateFrame: Sets the frame's members and buffers. If required size is
67 // bigger than allocated one, new buffers of adequate size will be allocated.
68 // |buffer| must be a packed I420 buffer.
69 // Return value: 0 on success, -1 on error.
70 int CreateFrame(const uint8_t* buffer,
71 int width,
72 int height,
73 VideoRotation rotation);
74
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000075 // Deep copy frame: If required size is bigger than allocated one, new
76 // buffers of adequate size will be allocated.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000077 // Return value: 0 on success, -1 on error.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070078 int CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000079
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000080 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
81 // reference to the video buffer also retained by |videoFrame|.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070082 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000083
magjed@webrtc.org0d9bb8e2015-03-11 10:06:58 +000084 // Release frame buffer and reset time stamps.
85 void Reset();
86
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000087 // Get pointer to buffer per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000088 uint8_t* buffer(PlaneType type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000089 // Overloading with const.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000090 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000091
92 // Get allocated size per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000093 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000094
95 // Get allocated stride per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000096 int stride(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000097
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000099 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000100
101 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000102 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000103
104 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000105 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000106
107 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000108 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000109
110 // Set capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000111 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000112 ntp_time_ms_ = ntp_time_ms;
113 }
114
115 // Get capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000116 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000117
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000118 // Naming convention for Coordination of Video Orientation. Please see
119 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
120 //
121 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
122 //
123 // "not pending" = a frame that has a VideoRotation == 0.
124 //
125 // "apply rotation" = modify a frame from being "pending" to being "not
126 // pending" rotation (a no-op for "unrotated").
127 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000128 VideoRotation rotation() const { return rotation_; }
129 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000130 rotation_ = rotation;
131 }
132
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000133 // Set render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000134 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000135 render_time_ms_ = render_time_ms;
136 }
137
138 // Get render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000139 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000140
141 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000142 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000143
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000144 // Return the handle of the underlying video frame. This is used when the
145 // frame is backed by a texture. The object should be destroyed when it is no
146 // longer in use, so the underlying resource can be freed.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000147 void* native_handle() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000148
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000149 // Return the underlying buffer.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000150 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000151
magjed@webrtc.orgd4e7d492015-03-23 12:27:55 +0000152 // Set the underlying buffer.
153 void set_video_frame_buffer(
154 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
155
Peter Boströmeb66e802015-06-05 11:08:03 +0200156 // Convert native-handle frame to memory-backed I420 frame. Should not be
157 // called on a non-native-handle frame.
158 VideoFrame ConvertNativeToI420Frame() const;
159
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000160 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000161 // An opaque reference counted handle that stores the pixel data.
162 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000163 uint32_t timestamp_;
164 int64_t ntp_time_ms_;
165 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000166 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000167};
168
169enum VideoFrameType {
170 kKeyFrame = 0,
171 kDeltaFrame = 1,
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000172};
173
174// TODO(pbos): Rename EncodedFrame and reformat this class' members.
175class EncodedImage {
176 public:
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000177 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000178 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000179 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000180
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000181 uint32_t _encodedWidth = 0;
182 uint32_t _encodedHeight = 0;
183 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000184 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000185 int64_t ntp_time_ms_ = 0;
186 int64_t capture_time_ms_ = 0;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000187 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000188 VideoFrameType _frameType = kDeltaFrame;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000189 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000190 size_t _length;
191 size_t _size;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000192 bool _completeFrame = false;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000193};
194
195} // namespace webrtc
196#endif // WEBRTC_VIDEO_FRAME_H_