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
20namespace webrtc {
21
22bool 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
36int 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
42VideoFrame::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
48VideoFrame::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
59int 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
92int 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
104int 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
125int 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
139int 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
158void 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
166void 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
174uint8_t* VideoFrame::buffer(PlaneType type) {
175  return video_frame_buffer_ ? video_frame_buffer_->MutableData(type)
176                             : nullptr;
177}
178
179const uint8_t* VideoFrame::buffer(PlaneType type) const {
180  return video_frame_buffer_ ? video_frame_buffer_->data(type) : nullptr;
181}
182
183int 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
188int VideoFrame::stride(PlaneType type) const {
189  return video_frame_buffer_ ? video_frame_buffer_->stride(type) : 0;
190}
191
192int VideoFrame::width() const {
193  return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
194}
195
196int VideoFrame::height() const {
197  return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
198}
199
200bool VideoFrame::IsZeroSize() const {
201  return !video_frame_buffer_;
202}
203
204void* VideoFrame::native_handle() const {
205  return video_frame_buffer_ ? video_frame_buffer_->native_handle() : nullptr;
206}
207
208rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
209  return video_frame_buffer_;
210}
211
212void VideoFrame::set_video_frame_buffer(
213    const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer) {
214  video_frame_buffer_ = buffer;
215}
216
217VideoFrame 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
225bool 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