blob: 7cdbd53f9d41cc8ff71d99f09ead95005375650e [file] [log] [blame]
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07001/*
2 * Copyright (c) 2012 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#include "webrtc/video_frame.h"
12
13#include <string.h>
14
15#include <algorithm> // swap
16
17#include "webrtc/base/bind.h"
18#include "webrtc/base/checks.h"
19
20namespace webrtc {
21
22VideoFrame::VideoFrame() {
23 // Intentionally using Reset instead of initializer list so that any missed
24 // fields in Reset will be caught by memory checkers.
25 Reset();
26}
27
28VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
29 uint32_t timestamp,
30 int64_t render_time_ms,
31 VideoRotation rotation)
32 : video_frame_buffer_(buffer),
33 timestamp_(timestamp),
34 ntp_time_ms_(0),
35 render_time_ms_(render_time_ms),
36 rotation_(rotation) {
37}
38
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070039int VideoFrame::CreateEmptyFrame(int width,
40 int height,
41 int stride_y,
42 int stride_u,
43 int stride_v) {
44 const int half_width = (width + 1) / 2;
henrikg91d6ede2015-09-17 00:24:34 -070045 RTC_DCHECK_GT(width, 0);
46 RTC_DCHECK_GT(height, 0);
47 RTC_DCHECK_GE(stride_y, width);
48 RTC_DCHECK_GE(stride_u, half_width);
49 RTC_DCHECK_GE(stride_v, half_width);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070050
51 // Creating empty frame - reset all values.
52 timestamp_ = 0;
53 ntp_time_ms_ = 0;
54 render_time_ms_ = 0;
55 rotation_ = kVideoRotation_0;
56
57 // Check if it's safe to reuse allocation.
58 if (video_frame_buffer_ && video_frame_buffer_->HasOneRef() &&
59 !video_frame_buffer_->native_handle() &&
60 width == video_frame_buffer_->width() &&
61 height == video_frame_buffer_->height() && stride_y == stride(kYPlane) &&
62 stride_u == stride(kUPlane) && stride_v == stride(kVPlane)) {
63 return 0;
64 }
65
66 // Need to allocate new buffer.
67 video_frame_buffer_ = new rtc::RefCountedObject<I420Buffer>(
68 width, height, stride_y, stride_u, stride_v);
69 return 0;
70}
71
72int VideoFrame::CreateFrame(const uint8_t* buffer_y,
73 const uint8_t* buffer_u,
74 const uint8_t* buffer_v,
75 int width,
76 int height,
77 int stride_y,
78 int stride_u,
79 int stride_v) {
80 return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y,
81 stride_u, stride_v, kVideoRotation_0);
82}
83
84int VideoFrame::CreateFrame(const uint8_t* buffer_y,
85 const uint8_t* buffer_u,
86 const uint8_t* buffer_v,
87 int width,
88 int height,
89 int stride_y,
90 int stride_u,
91 int stride_v,
92 VideoRotation rotation) {
93 const int half_height = (height + 1) / 2;
94 const int expected_size_y = height * stride_y;
95 const int expected_size_u = half_height * stride_u;
96 const int expected_size_v = half_height * stride_v;
97 CreateEmptyFrame(width, height, stride_y, stride_u, stride_v);
98 memcpy(buffer(kYPlane), buffer_y, expected_size_y);
99 memcpy(buffer(kUPlane), buffer_u, expected_size_u);
100 memcpy(buffer(kVPlane), buffer_v, expected_size_v);
101 rotation_ = rotation;
102 return 0;
103}
104
105int VideoFrame::CreateFrame(const uint8_t* buffer,
106 int width,
107 int height,
108 VideoRotation rotation) {
109 const int stride_y = width;
110 const int stride_uv = (width + 1) / 2;
111
112 const uint8_t* buffer_y = buffer;
113 const uint8_t* buffer_u = buffer_y + stride_y * height;
114 const uint8_t* buffer_v = buffer_u + stride_uv * ((height + 1) / 2);
115 return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y,
116 stride_uv, stride_uv, rotation);
117}
118
119int VideoFrame::CopyFrame(const VideoFrame& videoFrame) {
120 if (videoFrame.IsZeroSize()) {
121 video_frame_buffer_ = nullptr;
122 } else if (videoFrame.native_handle()) {
123 video_frame_buffer_ = videoFrame.video_frame_buffer();
124 } else {
125 CreateFrame(videoFrame.buffer(kYPlane), videoFrame.buffer(kUPlane),
126 videoFrame.buffer(kVPlane), videoFrame.width(),
127 videoFrame.height(), videoFrame.stride(kYPlane),
128 videoFrame.stride(kUPlane), videoFrame.stride(kVPlane));
129 }
130
131 timestamp_ = videoFrame.timestamp_;
132 ntp_time_ms_ = videoFrame.ntp_time_ms_;
133 render_time_ms_ = videoFrame.render_time_ms_;
134 rotation_ = videoFrame.rotation_;
135 return 0;
136}
137
138void VideoFrame::ShallowCopy(const VideoFrame& videoFrame) {
139 video_frame_buffer_ = videoFrame.video_frame_buffer();
140 timestamp_ = videoFrame.timestamp_;
141 ntp_time_ms_ = videoFrame.ntp_time_ms_;
142 render_time_ms_ = videoFrame.render_time_ms_;
143 rotation_ = videoFrame.rotation_;
144}
145
146void VideoFrame::Reset() {
147 video_frame_buffer_ = nullptr;
148 timestamp_ = 0;
149 ntp_time_ms_ = 0;
150 render_time_ms_ = 0;
151 rotation_ = kVideoRotation_0;
152}
153
154uint8_t* VideoFrame::buffer(PlaneType type) {
Magnus Jedvert3318f982015-08-26 16:06:21 +0200155 return video_frame_buffer_ ? video_frame_buffer_->MutableData(type)
156 : nullptr;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700157}
158
159const uint8_t* VideoFrame::buffer(PlaneType type) const {
Magnus Jedvert3318f982015-08-26 16:06:21 +0200160 return video_frame_buffer_ ? video_frame_buffer_->data(type) : nullptr;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700161}
162
163int VideoFrame::allocated_size(PlaneType type) const {
164 const int plane_height = (type == kYPlane) ? height() : (height() + 1) / 2;
165 return plane_height * stride(type);
166}
167
168int VideoFrame::stride(PlaneType type) const {
169 return video_frame_buffer_ ? video_frame_buffer_->stride(type) : 0;
170}
171
172int VideoFrame::width() const {
173 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
174}
175
176int VideoFrame::height() const {
177 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
178}
179
180bool VideoFrame::IsZeroSize() const {
181 return !video_frame_buffer_;
182}
183
184void* VideoFrame::native_handle() const {
185 return video_frame_buffer_ ? video_frame_buffer_->native_handle() : nullptr;
186}
187
188rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
189 return video_frame_buffer_;
190}
191
192void VideoFrame::set_video_frame_buffer(
193 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer) {
194 video_frame_buffer_ = buffer;
195}
196
Peter Boströmeb66e802015-06-05 11:08:03 +0200197VideoFrame VideoFrame::ConvertNativeToI420Frame() const {
henrikg91d6ede2015-09-17 00:24:34 -0700198 RTC_DCHECK(native_handle());
Peter Boströmeb66e802015-06-05 11:08:03 +0200199 VideoFrame frame;
200 frame.ShallowCopy(*this);
201 frame.set_video_frame_buffer(video_frame_buffer_->NativeToI420Buffer());
202 return frame;
203}
204
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700205} // namespace webrtc