blob: ea550aba881c5be0dfdd882235f3345744fbd231 [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);
Peter Boström308d1632015-06-02 15:04:23 +020028 VideoFrame(void* native_handle,
29 int width,
30 int height,
31 uint32_t timestamp,
32 int64_t render_time_ms,
33 VideoRotation rotation,
34 const rtc::Callback0<void>& no_longer_used);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000035
pbos@webrtc.org5a477a02015-03-18 14:11:39 +000036 // TODO(pbos): Make all create/copy functions void, they should not be able to
37 // fail (which should be DCHECK/CHECKed instead).
38
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000039 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
40 // on set dimensions - height and plane stride.
41 // If required size is bigger than the allocated one, new buffers of adequate
42 // size will be allocated.
43 // Return value: 0 on success, -1 on error.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000044 int CreateEmptyFrame(int width,
45 int height,
46 int stride_y,
47 int stride_u,
48 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000049
50 // CreateFrame: Sets the frame's members and buffers. If required size is
51 // bigger than allocated one, new buffers of adequate size will be allocated.
52 // Return value: 0 on success, -1 on error.
hbos@webrtc.org93d9d652015-03-16 13:26:00 +000053 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000054 const uint8_t* buffer_u,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000055 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.
hbos@webrtc.org93d9d652015-03-16 13:26:00 +000063 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000064 const uint8_t* buffer_u,
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000065 const uint8_t* buffer_v,
66 int width,
67 int height,
68 int stride_y,
69 int stride_u,
70 int stride_v,
71 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000072
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000073 // CreateFrame: Sets the frame's members and buffers. If required size is
74 // bigger than allocated one, new buffers of adequate size will be allocated.
75 // |buffer| must be a packed I420 buffer.
76 // Return value: 0 on success, -1 on error.
77 int CreateFrame(const uint8_t* buffer,
78 int width,
79 int height,
80 VideoRotation rotation);
81
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000082 // Deep copy frame: If required size is bigger than allocated one, new
83 // buffers of adequate size will be allocated.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000084 // Return value: 0 on success, -1 on error.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070085 int CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000086
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000087 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
88 // reference to the video buffer also retained by |videoFrame|.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070089 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000090
magjed@webrtc.org0d9bb8e2015-03-11 10:06:58 +000091 // Release frame buffer and reset time stamps.
92 void Reset();
93
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000094 // Get pointer to buffer per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000095 uint8_t* buffer(PlaneType type);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000096 // Overloading with const.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +000097 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000098
99 // Get allocated size per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000100 int allocated_size(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000101
102 // Get allocated stride per plane.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000103 int stride(PlaneType type) const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000104
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000105 // Get frame width.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000106 int width() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000107
108 // Get frame height.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000109 int height() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000110
111 // Set frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000112 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000113
114 // Get frame timestamp (90kHz).
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000115 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000116
117 // Set capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000118 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000119 ntp_time_ms_ = ntp_time_ms;
120 }
121
122 // Get capture ntp time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000123 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000124
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000125 // Naming convention for Coordination of Video Orientation. Please see
126 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
127 //
128 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
129 //
130 // "not pending" = a frame that has a VideoRotation == 0.
131 //
132 // "apply rotation" = modify a frame from being "pending" to being "not
133 // pending" rotation (a no-op for "unrotated").
134 //
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000135 VideoRotation rotation() const { return rotation_; }
136 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000137 rotation_ = rotation;
138 }
139
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000140 // Set render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000141 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000142 render_time_ms_ = render_time_ms;
143 }
144
145 // Get render time in miliseconds.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000146 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000147
148 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000149 bool IsZeroSize() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000150
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000151 // Return the handle of the underlying video frame. This is used when the
152 // frame is backed by a texture. The object should be destroyed when it is no
153 // longer in use, so the underlying resource can be freed.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000154 void* native_handle() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000155
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000156 // Return the underlying buffer.
magjed@webrtc.org45cdcce2015-03-06 10:41:00 +0000157 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000158
magjed@webrtc.orgd4e7d492015-03-23 12:27:55 +0000159 // Set the underlying buffer.
160 void set_video_frame_buffer(
161 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
162
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000163 private:
magjed@webrtc.org2386d6d2015-03-05 14:03:08 +0000164 // An opaque reference counted handle that stores the pixel data.
165 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000166 uint32_t timestamp_;
167 int64_t ntp_time_ms_;
168 int64_t render_time_ms_;
guoweis@webrtc.org1226e922015-02-11 18:37:54 +0000169 VideoRotation rotation_;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000170};
171
172enum VideoFrameType {
173 kKeyFrame = 0,
174 kDeltaFrame = 1,
175 kGoldenFrame = 2,
176 kAltRefFrame = 3,
177 kSkipFrame = 4
178};
179
180// TODO(pbos): Rename EncodedFrame and reformat this class' members.
181class EncodedImage {
182 public:
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000183 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000184 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000185 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000186
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000187 uint32_t _encodedWidth = 0;
188 uint32_t _encodedHeight = 0;
189 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000190 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000191 int64_t ntp_time_ms_ = 0;
192 int64_t capture_time_ms_ = 0;
pbos@webrtc.org273a4142014-12-01 15:23:21 +0000193 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000194 VideoFrameType _frameType = kDeltaFrame;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000195 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000196 size_t _length;
197 size_t _size;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000198 bool _completeFrame = false;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000199};
200
201} // namespace webrtc
202#endif // WEBRTC_VIDEO_FRAME_H_