blob: 4dc3411e98ac2d218077d552d6ceb4cc52f28444 [file] [log] [blame]
pbos@webrtc.org1c655452014-09-17 09:02:251/*
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.org398a7842015-03-05 14:03:0814#include "webrtc/base/scoped_ref_ptr.h"
pbosb63d8ad2015-10-19 09:39:0615#include "webrtc/common_types.h"
kjellandercd662542015-11-16 21:52:2416#include "webrtc/common_video/include/video_frame_buffer.h"
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5417#include "webrtc/common_video/rotation.h"
magjed@webrtc.org20ef1432015-03-06 10:41:0018#include "webrtc/typedefs.h"
pbos@webrtc.org1c655452014-09-17 09:02:2519
20namespace webrtc {
21
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4022class VideoFrame {
pbos@webrtc.org1c655452014-09-17 09:02:2523 public:
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4024 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.org1c655452014-09-17 09:02:2529
30 // CreateEmptyFrame: Sets frame dimensions and allocates buffers based
31 // on set dimensions - height and plane stride.
32 // If required size is bigger than the allocated one, new buffers of adequate
33 // size will be allocated.
Niels Möllerc961c782016-02-29 12:11:4534 void CreateEmptyFrame(int width,
35 int height,
36 int stride_y,
37 int stride_u,
38 int stride_v);
pbos@webrtc.org1c655452014-09-17 09:02:2539
40 // CreateFrame: Sets the frame's members and buffers. If required size is
41 // bigger than allocated one, new buffers of adequate size will be allocated.
Niels Möllerc961c782016-02-29 12:11:4542 void CreateFrame(const uint8_t* buffer_y,
43 const uint8_t* buffer_u,
44 const uint8_t* buffer_v,
45 int width,
46 int height,
47 int stride_y,
48 int stride_u,
49 int stride_v,
50 VideoRotation rotation);
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5451
perkj@webrtc.org0db69ce2015-03-20 10:55:1552 // CreateFrame: Sets the frame's members and buffers. If required size is
53 // bigger than allocated one, new buffers of adequate size will be allocated.
54 // |buffer| must be a packed I420 buffer.
Niels Möllerc961c782016-02-29 12:11:4555 void CreateFrame(const uint8_t* buffer,
perkj@webrtc.org0db69ce2015-03-20 10:55:1556 int width,
57 int height,
58 VideoRotation rotation);
59
perkj@webrtc.orgbddb5882015-03-18 09:51:0560 // Deep copy frame: If required size is bigger than allocated one, new
61 // buffers of adequate size will be allocated.
Niels Möllerc961c782016-02-29 12:11:4562 void CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.org1c655452014-09-17 09:02:2563
perkj@webrtc.orgbddb5882015-03-18 09:51:0564 // Creates a shallow copy of |videoFrame|, i.e, the this object will retain a
65 // reference to the video buffer also retained by |videoFrame|.
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4066 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgbddb5882015-03-18 09:51:0567
pbos@webrtc.org1c655452014-09-17 09:02:2568 // Get allocated size per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0069 int allocated_size(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2570
pbos@webrtc.org1c655452014-09-17 09:02:2571 // Get frame width.
magjed@webrtc.org20ef1432015-03-06 10:41:0072 int width() const;
pbos@webrtc.org1c655452014-09-17 09:02:2573
74 // Get frame height.
magjed@webrtc.org20ef1432015-03-06 10:41:0075 int height() const;
pbos@webrtc.org1c655452014-09-17 09:02:2576
77 // Set frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:0078 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.org1c655452014-09-17 09:02:2579
80 // Get frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:0081 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.org1c655452014-09-17 09:02:2582
83 // Set capture ntp time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:0084 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:2585 ntp_time_ms_ = ntp_time_ms;
86 }
87
88 // Get capture ntp time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:0089 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:2590
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5491 // Naming convention for Coordination of Video Orientation. Please see
92 // http://www.etsi.org/deliver/etsi_ts/126100_126199/126114/12.07.00_60/ts_126114v120700p.pdf
93 //
94 // "pending rotation" or "pending" = a frame that has a VideoRotation > 0.
95 //
96 // "not pending" = a frame that has a VideoRotation == 0.
97 //
98 // "apply rotation" = modify a frame from being "pending" to being "not
99 // pending" rotation (a no-op for "unrotated").
100 //
magjed@webrtc.org20ef1432015-03-06 10:41:00101 VideoRotation rotation() const { return rotation_; }
102 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54103 rotation_ = rotation;
104 }
105
pbos@webrtc.org1c655452014-09-17 09:02:25106 // Set render time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00107 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25108 render_time_ms_ = render_time_ms;
109 }
110
111 // Get render time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00112 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25113
114 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org20ef1432015-03-06 10:41:00115 bool IsZeroSize() const;
pbos@webrtc.org1c655452014-09-17 09:02:25116
nissea186ef62016-04-15 10:43:39117 // Return the underlying buffer. Never nullptr for a properly
118 // initialized VideoFrame.
nissefe1461a2016-05-13 11:12:41119 // Creating a new reference breaks the HasOneRef and IsMutable
120 // logic. So return a const ref to our reference.
121 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& video_frame_buffer()
122 const;
pbos@webrtc.org1c655452014-09-17 09:02:25123
magjed@webrtc.org607f5b32015-03-23 12:27:55124 // Set the underlying buffer.
125 void set_video_frame_buffer(
126 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
127
Peter Boström020eb8a2015-06-05 09:08:03128 // Convert native-handle frame to memory-backed I420 frame. Should not be
129 // called on a non-native-handle frame.
130 VideoFrame ConvertNativeToI420Frame() const;
131
pbos@webrtc.org1c655452014-09-17 09:02:25132 private:
magjed@webrtc.org398a7842015-03-05 14:03:08133 // An opaque reference counted handle that stores the pixel data.
134 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.org1c655452014-09-17 09:02:25135 uint32_t timestamp_;
136 int64_t ntp_time_ms_;
137 int64_t render_time_ms_;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54138 VideoRotation rotation_;
pbos@webrtc.org1c655452014-09-17 09:02:25139};
140
asapersson39f77022015-10-20 06:32:41141
pbos@webrtc.org1c655452014-09-17 09:02:25142// TODO(pbos): Rename EncodedFrame and reformat this class' members.
143class EncodedImage {
144 public:
hbos6f757142016-01-21 13:43:11145 static const size_t kBufferPaddingBytesH264;
146
147 // Some decoders require encoded image buffers to be padded with a small
148 // number of additional bytes (due to over-reading byte readers).
149 static size_t GetBufferPaddingBytes(VideoCodecType codec_type);
150
pbos@webrtc.orgf0657cd2015-03-10 12:55:17151 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
Pereb1bef42016-04-19 13:01:23152
pkasting@chromium.org0ab923a2014-11-20 22:28:14153 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.orgf0657cd2015-03-10 12:55:17154 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.org1c655452014-09-17 09:02:25155
asapersson7a4c3c12015-10-19 07:35:21156 struct AdaptReason {
157 AdaptReason()
asapersson39f77022015-10-20 06:32:41158 : quality_resolution_downscales(-1),
159 bw_resolutions_disabled(-1) {}
asapersson7a4c3c12015-10-19 07:35:21160
161 int quality_resolution_downscales; // Number of times this frame is down
162 // scaled in resolution due to quality.
163 // Or -1 if information is not provided.
asapersson39f77022015-10-20 06:32:41164 int bw_resolutions_disabled; // Number of resolutions that are not sent
165 // due to bandwidth for this frame.
166 // Or -1 if information is not provided.
asapersson7a4c3c12015-10-19 07:35:21167 };
pbos@webrtc.orgf0657cd2015-03-10 12:55:17168 uint32_t _encodedWidth = 0;
169 uint32_t _encodedHeight = 0;
170 uint32_t _timeStamp = 0;
pbos@webrtc.org1c655452014-09-17 09:02:25171 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.orgf0657cd2015-03-10 12:55:17172 int64_t ntp_time_ms_ = 0;
173 int64_t capture_time_ms_ = 0;
Peter Boström9511e462015-10-23 13:58:18174 FrameType _frameType = kVideoFrameDelta;
pbos@webrtc.org1c655452014-09-17 09:02:25175 uint8_t* _buffer;
pkasting@chromium.org0ab923a2014-11-20 22:28:14176 size_t _length;
177 size_t _size;
Pereb1bef42016-04-19 13:01:23178 VideoRotation rotation_ = kVideoRotation_0;
pbos@webrtc.orgf0657cd2015-03-10 12:55:17179 bool _completeFrame = false;
asapersson7a4c3c12015-10-19 07:35:21180 AdaptReason adapt_reason_;
asaperssona0621102015-10-21 06:55:26181 int qp_ = -1; // Quantizer value.
pbos@webrtc.org1c655452014-09-17 09:02:25182};
183
184} // namespace webrtc
185#endif // WEBRTC_VIDEO_FRAME_H_