1 /*
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
20 namespace webrtc {
21
EqualPlane(const uint8_t * data1,const uint8_t * data2,int stride,int width,int height)22 bool EqualPlane(const uint8_t* data1,
23 const uint8_t* data2,
24 int stride,
25 int width,
26 int height) {
27 for (int y = 0; y < height; ++y) {
28 if (memcmp(data1, data2, width) != 0)
29 return false;
30 data1 += stride;
31 data2 += stride;
32 }
33 return true;
34 }
35
ExpectedSize(int plane_stride,int image_height,PlaneType type)36 int ExpectedSize(int plane_stride, int image_height, PlaneType type) {
37 if (type == kYPlane)
38 return plane_stride * image_height;
39 return plane_stride * ((image_height + 1) / 2);
40 }
41
VideoFrame()42 VideoFrame::VideoFrame() {
43 // Intentionally using Reset instead of initializer list so that any missed
44 // fields in Reset will be caught by memory checkers.
45 Reset();
46 }
47
VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer> & buffer,uint32_t timestamp,int64_t render_time_ms,VideoRotation rotation)48 VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
49 uint32_t timestamp,
50 int64_t render_time_ms,
51 VideoRotation rotation)
52 : video_frame_buffer_(buffer),
53 timestamp_(timestamp),
54 ntp_time_ms_(0),
55 render_time_ms_(render_time_ms),
56 rotation_(rotation) {
57 }
58
CreateEmptyFrame(int width,int height,int stride_y,int stride_u,int stride_v)59 int VideoFrame::CreateEmptyFrame(int width,
60 int height,
61 int stride_y,
62 int stride_u,
63 int stride_v) {
64 const int half_width = (width + 1) / 2;
65 RTC_DCHECK_GT(width, 0);
66 RTC_DCHECK_GT(height, 0);
67 RTC_DCHECK_GE(stride_y, width);
68 RTC_DCHECK_GE(stride_u, half_width);
69 RTC_DCHECK_GE(stride_v, half_width);
70
71 // Creating empty frame - reset all values.
72 timestamp_ = 0;
73 ntp_time_ms_ = 0;
74 render_time_ms_ = 0;
75 rotation_ = kVideoRotation_0;
76
77 // Check if it's safe to reuse allocation.
78 if (video_frame_buffer_ && video_frame_buffer_->HasOneRef() &&
79 !video_frame_buffer_->native_handle() &&
80 width == video_frame_buffer_->width() &&
81 height == video_frame_buffer_->height() && stride_y == stride(kYPlane) &&
82 stride_u == stride(kUPlane) && stride_v == stride(kVPlane)) {
83 return 0;
84 }
85
86 // Need to allocate new buffer.
87 video_frame_buffer_ = new rtc::RefCountedObject<I420Buffer>(
88 width, height, stride_y, stride_u, stride_v);
89 return 0;
90 }
91
CreateFrame(const uint8_t * buffer_y,const uint8_t * buffer_u,const uint8_t * buffer_v,int width,int height,int stride_y,int stride_u,int stride_v)92 int VideoFrame::CreateFrame(const uint8_t* buffer_y,
93 const uint8_t* buffer_u,
94 const uint8_t* buffer_v,
95 int width,
96 int height,
97 int stride_y,
98 int stride_u,
99 int stride_v) {
100 return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y,
101 stride_u, stride_v, kVideoRotation_0);
102 }
103
CreateFrame(const uint8_t * buffer_y,const uint8_t * buffer_u,const uint8_t * buffer_v,int width,int height,int stride_y,int stride_u,int stride_v,VideoRotation rotation)104 int VideoFrame::CreateFrame(const uint8_t* buffer_y,
105 const uint8_t* buffer_u,
106 const uint8_t* buffer_v,
107 int width,
108 int height,
109 int stride_y,
110 int stride_u,
111 int stride_v,
112 VideoRotation rotation) {
113 const int half_height = (height + 1) / 2;
114 const int expected_size_y = height * stride_y;
115 const int expected_size_u = half_height * stride_u;
116 const int expected_size_v = half_height * stride_v;
117 CreateEmptyFrame(width, height, stride_y, stride_u, stride_v);
118 memcpy(buffer(kYPlane), buffer_y, expected_size_y);
119 memcpy(buffer(kUPlane), buffer_u, expected_size_u);
120 memcpy(buffer(kVPlane), buffer_v, expected_size_v);
121 rotation_ = rotation;
122 return 0;
123 }
124
CreateFrame(const uint8_t * buffer,int width,int height,VideoRotation rotation)125 int VideoFrame::CreateFrame(const uint8_t* buffer,
126 int width,
127 int height,
128 VideoRotation rotation) {
129 const int stride_y = width;
130 const int stride_uv = (width + 1) / 2;
131
132 const uint8_t* buffer_y = buffer;
133 const uint8_t* buffer_u = buffer_y + stride_y * height;
134 const uint8_t* buffer_v = buffer_u + stride_uv * ((height + 1) / 2);
135 return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y,
136 stride_uv, stride_uv, rotation);
137 }
138
CopyFrame(const VideoFrame & videoFrame)139 int VideoFrame::CopyFrame(const VideoFrame& videoFrame) {
140 if (videoFrame.IsZeroSize()) {
141 video_frame_buffer_ = nullptr;
142 } else if (videoFrame.native_handle()) {
143 video_frame_buffer_ = videoFrame.video_frame_buffer();
144 } else {
145 CreateFrame(videoFrame.buffer(kYPlane), videoFrame.buffer(kUPlane),
146 videoFrame.buffer(kVPlane), videoFrame.width(),
147 videoFrame.height(), videoFrame.stride(kYPlane),
148 videoFrame.stride(kUPlane), videoFrame.stride(kVPlane));
149 }
150
151 timestamp_ = videoFrame.timestamp_;
152 ntp_time_ms_ = videoFrame.ntp_time_ms_;
153 render_time_ms_ = videoFrame.render_time_ms_;
154 rotation_ = videoFrame.rotation_;
155 return 0;
156 }
157
ShallowCopy(const VideoFrame & videoFrame)158 void VideoFrame::ShallowCopy(const VideoFrame& videoFrame) {
159 video_frame_buffer_ = videoFrame.video_frame_buffer();
160 timestamp_ = videoFrame.timestamp_;
161 ntp_time_ms_ = videoFrame.ntp_time_ms_;
162 render_time_ms_ = videoFrame.render_time_ms_;
163 rotation_ = videoFrame.rotation_;
164 }
165
Reset()166 void VideoFrame::Reset() {
167 video_frame_buffer_ = nullptr;
168 timestamp_ = 0;
169 ntp_time_ms_ = 0;
170 render_time_ms_ = 0;
171 rotation_ = kVideoRotation_0;
172 }
173
buffer(PlaneType type)174 uint8_t* VideoFrame::buffer(PlaneType type) {
175 return video_frame_buffer_ ? video_frame_buffer_->MutableData(type)
176 : nullptr;
177 }
178
buffer(PlaneType type) const179 const uint8_t* VideoFrame::buffer(PlaneType type) const {
180 return video_frame_buffer_ ? video_frame_buffer_->data(type) : nullptr;
181 }
182
allocated_size(PlaneType type) const183 int VideoFrame::allocated_size(PlaneType type) const {
184 const int plane_height = (type == kYPlane) ? height() : (height() + 1) / 2;
185 return plane_height * stride(type);
186 }
187
stride(PlaneType type) const188 int VideoFrame::stride(PlaneType type) const {
189 return video_frame_buffer_ ? video_frame_buffer_->stride(type) : 0;
190 }
191
width() const192 int VideoFrame::width() const {
193 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
194 }
195
height() const196 int VideoFrame::height() const {
197 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
198 }
199
IsZeroSize() const200 bool VideoFrame::IsZeroSize() const {
201 return !video_frame_buffer_;
202 }
203
native_handle() const204 void* VideoFrame::native_handle() const {
205 return video_frame_buffer_ ? video_frame_buffer_->native_handle() : nullptr;
206 }
207
video_frame_buffer() const208 rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
209 return video_frame_buffer_;
210 }
211
set_video_frame_buffer(const rtc::scoped_refptr<webrtc::VideoFrameBuffer> & buffer)212 void VideoFrame::set_video_frame_buffer(
213 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer) {
214 video_frame_buffer_ = buffer;
215 }
216
ConvertNativeToI420Frame() const217 VideoFrame VideoFrame::ConvertNativeToI420Frame() const {
218 RTC_DCHECK(native_handle());
219 VideoFrame frame;
220 frame.ShallowCopy(*this);
221 frame.set_video_frame_buffer(video_frame_buffer_->NativeToI420Buffer());
222 return frame;
223 }
224
EqualsFrame(const VideoFrame & frame) const225 bool VideoFrame::EqualsFrame(const VideoFrame& frame) const {
226 if (width() != frame.width() || height() != frame.height() ||
227 stride(kYPlane) != frame.stride(kYPlane) ||
228 stride(kUPlane) != frame.stride(kUPlane) ||
229 stride(kVPlane) != frame.stride(kVPlane) ||
230 timestamp() != frame.timestamp() ||
231 ntp_time_ms() != frame.ntp_time_ms() ||
232 render_time_ms() != frame.render_time_ms()) {
233 return false;
234 }
235 const int half_width = (width() + 1) / 2;
236 const int half_height = (height() + 1) / 2;
237 return EqualPlane(buffer(kYPlane), frame.buffer(kYPlane),
238 stride(kYPlane), width(), height()) &&
239 EqualPlane(buffer(kUPlane), frame.buffer(kUPlane),
240 stride(kUPlane), half_width, half_height) &&
241 EqualPlane(buffer(kVPlane), frame.buffer(kVPlane),
242 stride(kVPlane), half_width, half_height);
243 }
244
245 } // namespace webrtc
246