blob: 39ba8ee0e88915eeea1d37aa9dc33c1aa45f6015 [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"
pbos22993e12015-10-19 02:39:06 -070015#include "webrtc/common_types.h"
kjellander6f8ce062015-11-16 13:52:24 -080016#include "webrtc/common_video/include/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
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070022class VideoFrame {
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000023 public:
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070024 VideoFrame();
25 VideoFrame(const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
26 uint32_t timestamp,
27 int64_t render_time_ms,
28 VideoRotation rotation);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000029
kjellander54ebfca2016-02-26 07:38:54 -080030 // TODO(pbos): Make all create/copy functions void, they should not be able to
31 // fail (which should be RTC_DCHECK/CHECKed instead).
32
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000033 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
34 // on set dimensions - height and plane stride.
35 // If required size is bigger than the allocated one, new buffers of adequate
36 // size will be allocated.
kjellander54ebfca2016-02-26 07:38:54 -080037 // Return value: 0 on success, -1 on error.
38 int CreateEmptyFrame(int width,
39 int height,
40 int stride_y,
41 int stride_u,
42 int stride_v);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000043
44 // CreateFrame: Sets the frame's members and buffers. If required size is
45 // bigger than allocated one, new buffers of adequate size will be allocated.
kjellander54ebfca2016-02-26 07:38:54 -080046 // Return value: 0 on success, -1 on error.
47 int CreateFrame(const uint8_t* buffer_y,
48 const uint8_t* buffer_u,
49 const uint8_t* buffer_v,
50 int width,
51 int height,
52 int stride_y,
53 int stride_u,
54 int stride_v);
55
56 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
57 int CreateFrame(const uint8_t* buffer_y,
58 const uint8_t* buffer_u,
59 const uint8_t* buffer_v,
60 int width,
61 int height,
62 int stride_y,
63 int stride_u,
64 int stride_v,
65 VideoRotation rotation);
guoweis@webrtc.org1226e922015-02-11 18:37:54 +000066
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000067 // CreateFrame: Sets the frame's members and buffers. If required size is
68 // bigger than allocated one, new buffers of adequate size will be allocated.
69 // |buffer| must be a packed I420 buffer.
kjellander54ebfca2016-02-26 07:38:54 -080070 // Return value: 0 on success, -1 on error.
71 int CreateFrame(const uint8_t* buffer,
perkj@webrtc.org9f9ea7e2015-03-20 10:55:15 +000072 int width,
73 int height,
74 VideoRotation rotation);
75
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000076 // Deep copy frame: If required size is bigger than allocated one, new
77 // buffers of adequate size will be allocated.
kjellander54ebfca2016-02-26 07:38:54 -080078 // Return value: 0 on success, -1 on error.
79 int CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000080
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000081 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
82 // reference to the video buffer also retained by |videoFrame|.
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070083 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgaf612d52015-03-18 09:51:05 +000084
magjed@webrtc.org0d9bb8e2015-03-11 10:06:58 +000085 // Release frame buffer and reset time stamps.
86 void Reset();
87
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +000088 // 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
magjed@webrtc.orgd4e7d492015-03-23 12:27:55 +0000153 // Set the underlying buffer.
154 void set_video_frame_buffer(
155 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
156
Peter Boströmeb66e802015-06-05 11:08:03 +0200157 // Convert native-handle frame to memory-backed I420 frame. Should not be
158 // called on a non-native-handle frame.
159 VideoFrame ConvertNativeToI420Frame() const;
160
kjellander54ebfca2016-02-26 07:38:54 -0800161 bool EqualsFrame(const VideoFrame& frame) const;
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
asaperssonda535c42015-10-19 23:32:41 -0700172
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000173// TODO(pbos): Rename EncodedFrame and reformat this class' members.
174class EncodedImage {
175 public:
hbosd6648362016-01-21 05:43:11 -0800176 static const size_t kBufferPaddingBytesH264;
177
178 // Some decoders require encoded image buffers to be padded with a small
179 // number of additional bytes (due to over-reading byte readers).
180 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
181
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000182 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000183 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000184 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000185
asapersson4306fc72015-10-19 00:35:21 -0700186 struct AdaptReason {
187 AdaptReason()
asaperssonda535c42015-10-19 23:32:41 -0700188 : quality_resolution_downscales(-1),
189 bw_resolutions_disabled(-1) {}
asapersson4306fc72015-10-19 00:35:21 -0700190
191 int quality_resolution_downscales; // Number of times this frame is down
192 // scaled in resolution due to quality.
193 // Or -1 if information is not provided.
asaperssonda535c42015-10-19 23:32:41 -0700194 int bw_resolutions_disabled; // Number of resolutions that are not sent
195 // due to bandwidth for this frame.
196 // Or -1 if information is not provided.
asapersson4306fc72015-10-19 00:35:21 -0700197 };
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000198 uint32_t _encodedWidth = 0;
199 uint32_t _encodedHeight = 0;
200 uint32_t _timeStamp = 0;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000201 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000202 int64_t ntp_time_ms_ = 0;
203 int64_t capture_time_ms_ = 0;
Peter Boström49e196a2015-10-23 15:58:18 +0200204 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000205 uint8_t* _buffer;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000206 size_t _length;
207 size_t _size;
pbos@webrtc.org4c8b9302015-03-10 12:55:17 +0000208 bool _completeFrame = false;
asapersson4306fc72015-10-19 00:35:21 -0700209 AdaptReason adapt_reason_;
asapersson86b01602015-10-20 23:55:26 -0700210 int qp_ = -1; // Quantizer value.
pbos@webrtc.orgab990ae2014-09-17 09:02:25 +0000211};
212
213} // namespace webrtc
214#endif // WEBRTC_VIDEO_FRAME_H_