blob: 821bfc25514034007363144bb70981759f687510 [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"
magjed@webrtc.org398a7842015-03-05 14:03:0816#include "webrtc/common_video/interface/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
pbos@webrtc.orga7347d22015-03-18 14:11:3930 // TODO(pbos): Make all create/copy functions void, they should not be able to
henrikg5c075c82015-09-17 07:24:3431 // fail (which should be RTC_DCHECK/CHECKed instead).
pbos@webrtc.orga7347d22015-03-18 14:11:3932
pbos@webrtc.org1c655452014-09-17 09:02:2533 // 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.
37 // Return value: 0 on success, -1 on error.
magjed@webrtc.org20ef1432015-03-06 10:41:0038 int CreateEmptyFrame(int width,
39 int height,
40 int stride_y,
41 int stride_u,
42 int stride_v);
pbos@webrtc.org1c655452014-09-17 09:02:2543
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.
46 // Return value: 0 on success, -1 on error.
hbos@webrtc.org6ab64272015-03-16 13:26:0047 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org20ef1432015-03-06 10:41:0048 const uint8_t* buffer_u,
magjed@webrtc.org20ef1432015-03-06 10:41:0049 const uint8_t* buffer_v,
50 int width,
51 int height,
52 int stride_y,
53 int stride_u,
54 int stride_v);
pbos@webrtc.org1c655452014-09-17 09:02:2555
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5456 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
hbos@webrtc.org6ab64272015-03-16 13:26:0057 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org20ef1432015-03-06 10:41:0058 const uint8_t* buffer_u,
magjed@webrtc.org20ef1432015-03-06 10:41:0059 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.orgfd7343a2015-02-11 18:37:5466
perkj@webrtc.org0db69ce2015-03-20 10:55:1567 // 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.
70 // Return value: 0 on success, -1 on error.
71 int CreateFrame(const uint8_t* buffer,
72 int width,
73 int height,
74 VideoRotation rotation);
75
perkj@webrtc.orgbddb5882015-03-18 09:51:0576 // Deep copy frame: If required size is bigger than allocated one, new
77 // buffers of adequate size will be allocated.
pbos@webrtc.org1c655452014-09-17 09:02:2578 // Return value: 0 on success, -1 on error.
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4079 int CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.org1c655452014-09-17 09:02:2580
perkj@webrtc.orgbddb5882015-03-18 09:51:0581 // 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-Sanchez3ca60f32015-05-30 00:21:4083 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgbddb5882015-03-18 09:51:0584
magjed@webrtc.org9f397f92015-03-11 10:06:5885 // Release frame buffer and reset time stamps.
86 void Reset();
87
pbos@webrtc.org1c655452014-09-17 09:02:2588 // Get pointer to buffer per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0089 uint8_t* buffer(PlaneType type);
pbos@webrtc.org1c655452014-09-17 09:02:2590 // Overloading with const.
magjed@webrtc.org20ef1432015-03-06 10:41:0091 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2592
93 // Get allocated size per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0094 int allocated_size(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2595
96 // Get allocated stride per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0097 int stride(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2598
pbos@webrtc.org1c655452014-09-17 09:02:2599 // Get frame width.
magjed@webrtc.org20ef1432015-03-06 10:41:00100 int width() const;
pbos@webrtc.org1c655452014-09-17 09:02:25101
102 // Get frame height.
magjed@webrtc.org20ef1432015-03-06 10:41:00103 int height() const;
pbos@webrtc.org1c655452014-09-17 09:02:25104
105 // Set frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:00106 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.org1c655452014-09-17 09:02:25107
108 // Get frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:00109 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.org1c655452014-09-17 09:02:25110
111 // Set capture ntp time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00112 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25113 ntp_time_ms_ = ntp_time_ms;
114 }
115
116 // Get capture ntp time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00117 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25118
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54119 // 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.org20ef1432015-03-06 10:41:00129 VideoRotation rotation() const { return rotation_; }
130 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54131 rotation_ = rotation;
132 }
133
pbos@webrtc.org1c655452014-09-17 09:02:25134 // Set render time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00135 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25136 render_time_ms_ = render_time_ms;
137 }
138
139 // Get render time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00140 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25141
142 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org20ef1432015-03-06 10:41:00143 bool IsZeroSize() const;
pbos@webrtc.org1c655452014-09-17 09:02:25144
pbos@webrtc.org1c655452014-09-17 09:02:25145 // 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.org20ef1432015-03-06 10:41:00148 void* native_handle() const;
pbos@webrtc.org1c655452014-09-17 09:02:25149
magjed@webrtc.org398a7842015-03-05 14:03:08150 // Return the underlying buffer.
magjed@webrtc.org20ef1432015-03-06 10:41:00151 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.org1c655452014-09-17 09:02:25152
magjed@webrtc.org607f5b32015-03-23 12:27:55153 // Set the underlying buffer.
154 void set_video_frame_buffer(
155 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
156
Peter Boström020eb8a2015-06-05 09:08:03157 // 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
pbos@webrtc.org1c655452014-09-17 09:02:25161 private:
magjed@webrtc.org398a7842015-03-05 14:03:08162 // An opaque reference counted handle that stores the pixel data.
163 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.org1c655452014-09-17 09:02:25164 uint32_t timestamp_;
165 int64_t ntp_time_ms_;
166 int64_t render_time_ms_;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54167 VideoRotation rotation_;
pbos@webrtc.org1c655452014-09-17 09:02:25168};
169
asapersson39f77022015-10-20 06:32:41170
pbos@webrtc.org1c655452014-09-17 09:02:25171// TODO(pbos): Rename EncodedFrame and reformat this class' members.
172class EncodedImage {
173 public:
pbos@webrtc.orgf0657cd2015-03-10 12:55:17174 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org0ab923a2014-11-20 22:28:14175 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.orgf0657cd2015-03-10 12:55:17176 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.org1c655452014-09-17 09:02:25177
asapersson7a4c3c12015-10-19 07:35:21178 struct AdaptReason {
179 AdaptReason()
asapersson39f77022015-10-20 06:32:41180 : quality_resolution_downscales(-1),
181 bw_resolutions_disabled(-1) {}
asapersson7a4c3c12015-10-19 07:35:21182
183 int quality_resolution_downscales; // Number of times this frame is down
184 // scaled in resolution due to quality.
185 // Or -1 if information is not provided.
asapersson39f77022015-10-20 06:32:41186 int bw_resolutions_disabled; // Number of resolutions that are not sent
187 // due to bandwidth for this frame.
188 // Or -1 if information is not provided.
asapersson7a4c3c12015-10-19 07:35:21189 };
pbos@webrtc.orgf0657cd2015-03-10 12:55:17190 uint32_t _encodedWidth = 0;
191 uint32_t _encodedHeight = 0;
192 uint32_t _timeStamp = 0;
pbos@webrtc.org1c655452014-09-17 09:02:25193 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.orgf0657cd2015-03-10 12:55:17194 int64_t ntp_time_ms_ = 0;
195 int64_t capture_time_ms_ = 0;
pbosb63d8ad2015-10-19 09:39:06196 FrameType _frameType = kDeltaFrame;
pbos@webrtc.org1c655452014-09-17 09:02:25197 uint8_t* _buffer;
pkasting@chromium.org0ab923a2014-11-20 22:28:14198 size_t _length;
199 size_t _size;
pbos@webrtc.orgf0657cd2015-03-10 12:55:17200 bool _completeFrame = false;
asapersson7a4c3c12015-10-19 07:35:21201 AdaptReason adapt_reason_;
pbos@webrtc.org1c655452014-09-17 09:02:25202};
203
204} // namespace webrtc
205#endif // WEBRTC_VIDEO_FRAME_H_