blob: d70a746daeb49334193c99d522cefd275615981c [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"
15#include "webrtc/common_video/interface/video_frame_buffer.h"
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5416#include "webrtc/common_video/rotation.h"
magjed@webrtc.org20ef1432015-03-06 10:41:0017#include "webrtc/typedefs.h"
pbos@webrtc.org1c655452014-09-17 09:02:2518
19namespace webrtc {
20
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4021class VideoFrame {
pbos@webrtc.org1c655452014-09-17 09:02:2522 public:
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4023 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.org1c655452014-09-17 09:02:2528
pbos@webrtc.orga7347d22015-03-18 14:11:3929 // TODO(pbos): Make all create/copy functions void, they should not be able to
30 // fail (which should be DCHECK/CHECKed instead).
31
pbos@webrtc.org1c655452014-09-17 09:02:2532 // 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.org20ef1432015-03-06 10:41:0037 int CreateEmptyFrame(int width,
38 int height,
39 int stride_y,
40 int stride_u,
41 int stride_v);
pbos@webrtc.org1c655452014-09-17 09:02:2542
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.org6ab64272015-03-16 13:26:0046 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org20ef1432015-03-06 10:41:0047 const uint8_t* buffer_u,
magjed@webrtc.org20ef1432015-03-06 10:41:0048 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.org1c655452014-09-17 09:02:2554
guoweis@webrtc.orgfd7343a2015-02-11 18:37:5455 // TODO(guoweis): remove the previous CreateFrame when chromium has this code.
hbos@webrtc.org6ab64272015-03-16 13:26:0056 int CreateFrame(const uint8_t* buffer_y,
magjed@webrtc.org20ef1432015-03-06 10:41:0057 const uint8_t* buffer_u,
magjed@webrtc.org20ef1432015-03-06 10:41:0058 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.orgfd7343a2015-02-11 18:37:5465
perkj@webrtc.org0db69ce2015-03-20 10:55:1566 // 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.orgbddb5882015-03-18 09:51:0575 // Deep copy frame: If required size is bigger than allocated one, new
76 // buffers of adequate size will be allocated.
pbos@webrtc.org1c655452014-09-17 09:02:2577 // Return value: 0 on success, -1 on error.
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4078 int CopyFrame(const VideoFrame& videoFrame);
pbos@webrtc.org1c655452014-09-17 09:02:2579
perkj@webrtc.orgbddb5882015-03-18 09:51:0580 // 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-Sanchez3ca60f32015-05-30 00:21:4082 void ShallowCopy(const VideoFrame& videoFrame);
perkj@webrtc.orgbddb5882015-03-18 09:51:0583
magjed@webrtc.org9f397f92015-03-11 10:06:5884 // Release frame buffer and reset time stamps.
85 void Reset();
86
pbos@webrtc.org1c655452014-09-17 09:02:2587 // Get pointer to buffer per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0088 uint8_t* buffer(PlaneType type);
pbos@webrtc.org1c655452014-09-17 09:02:2589 // Overloading with const.
magjed@webrtc.org20ef1432015-03-06 10:41:0090 const uint8_t* buffer(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2591
92 // Get allocated size per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0093 int allocated_size(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2594
95 // Get allocated stride per plane.
magjed@webrtc.org20ef1432015-03-06 10:41:0096 int stride(PlaneType type) const;
pbos@webrtc.org1c655452014-09-17 09:02:2597
pbos@webrtc.org1c655452014-09-17 09:02:2598 // Get frame width.
magjed@webrtc.org20ef1432015-03-06 10:41:0099 int width() const;
pbos@webrtc.org1c655452014-09-17 09:02:25100
101 // Get frame height.
magjed@webrtc.org20ef1432015-03-06 10:41:00102 int height() const;
pbos@webrtc.org1c655452014-09-17 09:02:25103
104 // Set frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:00105 void set_timestamp(uint32_t timestamp) { timestamp_ = timestamp; }
pbos@webrtc.org1c655452014-09-17 09:02:25106
107 // Get frame timestamp (90kHz).
magjed@webrtc.org20ef1432015-03-06 10:41:00108 uint32_t timestamp() const { return timestamp_; }
pbos@webrtc.org1c655452014-09-17 09:02:25109
110 // Set capture ntp time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00111 void set_ntp_time_ms(int64_t ntp_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25112 ntp_time_ms_ = ntp_time_ms;
113 }
114
115 // Get capture ntp time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00116 int64_t ntp_time_ms() const { return ntp_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25117
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54118 // 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.org20ef1432015-03-06 10:41:00128 VideoRotation rotation() const { return rotation_; }
129 void set_rotation(VideoRotation rotation) {
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54130 rotation_ = rotation;
131 }
132
pbos@webrtc.org1c655452014-09-17 09:02:25133 // Set render time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00134 void set_render_time_ms(int64_t render_time_ms) {
pbos@webrtc.org1c655452014-09-17 09:02:25135 render_time_ms_ = render_time_ms;
136 }
137
138 // Get render time in miliseconds.
magjed@webrtc.org20ef1432015-03-06 10:41:00139 int64_t render_time_ms() const { return render_time_ms_; }
pbos@webrtc.org1c655452014-09-17 09:02:25140
141 // Return true if underlying plane buffers are of zero size, false if not.
magjed@webrtc.org20ef1432015-03-06 10:41:00142 bool IsZeroSize() const;
pbos@webrtc.org1c655452014-09-17 09:02:25143
pbos@webrtc.org1c655452014-09-17 09:02:25144 // 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.org20ef1432015-03-06 10:41:00147 void* native_handle() const;
pbos@webrtc.org1c655452014-09-17 09:02:25148
magjed@webrtc.org398a7842015-03-05 14:03:08149 // Return the underlying buffer.
magjed@webrtc.org20ef1432015-03-06 10:41:00150 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer() const;
pbos@webrtc.org1c655452014-09-17 09:02:25151
magjed@webrtc.org607f5b32015-03-23 12:27:55152 // Set the underlying buffer.
153 void set_video_frame_buffer(
154 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer);
155
Peter Boström020eb8a2015-06-05 09:08:03156 // 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.org1c655452014-09-17 09:02:25160 private:
magjed@webrtc.org398a7842015-03-05 14:03:08161 // An opaque reference counted handle that stores the pixel data.
162 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer_;
pbos@webrtc.org1c655452014-09-17 09:02:25163 uint32_t timestamp_;
164 int64_t ntp_time_ms_;
165 int64_t render_time_ms_;
guoweis@webrtc.orgfd7343a2015-02-11 18:37:54166 VideoRotation rotation_;
pbos@webrtc.org1c655452014-09-17 09:02:25167};
168
169enum VideoFrameType {
170 kKeyFrame = 0,
171 kDeltaFrame = 1,
172 kGoldenFrame = 2,
173 kAltRefFrame = 3,
174 kSkipFrame = 4
175};
176
177// TODO(pbos): Rename EncodedFrame and reformat this class' members.
178class EncodedImage {
179 public:
pbos@webrtc.orgf0657cd2015-03-10 12:55:17180 EncodedImage() : EncodedImage(nullptr, 0, 0) {}
pkasting@chromium.org0ab923a2014-11-20 22:28:14181 EncodedImage(uint8_t* buffer, size_t length, size_t size)
pbos@webrtc.orgf0657cd2015-03-10 12:55:17182 : _buffer(buffer), _length(length), _size(size) {}
pbos@webrtc.org1c655452014-09-17 09:02:25183
pbos@webrtc.orgf0657cd2015-03-10 12:55:17184 uint32_t _encodedWidth = 0;
185 uint32_t _encodedHeight = 0;
186 uint32_t _timeStamp = 0;
pbos@webrtc.org1c655452014-09-17 09:02:25187 // NTP time of the capture time in local timebase in milliseconds.
pbos@webrtc.orgf0657cd2015-03-10 12:55:17188 int64_t ntp_time_ms_ = 0;
189 int64_t capture_time_ms_ = 0;
pbos@webrtc.orgee9497c2014-12-01 15:23:21190 // TODO(pbos): Use webrtc::FrameType directly (and remove VideoFrameType).
pbos@webrtc.orgf0657cd2015-03-10 12:55:17191 VideoFrameType _frameType = kDeltaFrame;
pbos@webrtc.org1c655452014-09-17 09:02:25192 uint8_t* _buffer;
pkasting@chromium.org0ab923a2014-11-20 22:28:14193 size_t _length;
194 size_t _size;
pbos@webrtc.orgf0657cd2015-03-10 12:55:17195 bool _completeFrame = false;
pbos@webrtc.org1c655452014-09-17 09:02:25196};
197
198} // namespace webrtc
199#endif // WEBRTC_VIDEO_FRAME_H_