blob: 86de823dc964be85c5c7e24fb835ff8de62828b6 [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
hbosbab934b2016-01-27 01:36:03 -080022// FFmpeg's decoder, used by H264DecoderImpl, requires up to 8 bytes padding due
23// to optimized bitstream readers. See avcodec_decode_video2.
24const size_t EncodedImage::kBufferPaddingBytesH264 = 8;
hbosd6648362016-01-21 05:43:11 -080025
jackychen67e94fb2016-01-11 21:34:07 -080026bool EqualPlane(const uint8_t* data1,
27 const uint8_t* data2,
28 int stride,
29 int width,
30 int height) {
31 for (int y = 0; y < height; ++y) {
32 if (memcmp(data1, data2, width) != 0)
33 return false;
34 data1 += stride;
35 data2 += stride;
36 }
37 return true;
38}
39
40int ExpectedSize(int plane_stride, int image_height, PlaneType type) {
jackychena276e732016-01-13 05:36:31 -080041 if (type == kYPlane)
42 return plane_stride * image_height;
43 return plane_stride * ((image_height + 1) / 2);
jackychen67e94fb2016-01-11 21:34:07 -080044}
45
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070046VideoFrame::VideoFrame() {
47 // Intentionally using Reset instead of initializer list so that any missed
48 // fields in Reset will be caught by memory checkers.
49 Reset();
50}
51
52VideoFrame::VideoFrame(const rtc::scoped_refptr<VideoFrameBuffer>& buffer,
53 uint32_t timestamp,
54 int64_t render_time_ms,
55 VideoRotation rotation)
56 : video_frame_buffer_(buffer),
57 timestamp_(timestamp),
58 ntp_time_ms_(0),
59 render_time_ms_(render_time_ms),
60 rotation_(rotation) {
61}
62
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070063int VideoFrame::CreateEmptyFrame(int width,
64 int height,
65 int stride_y,
66 int stride_u,
67 int stride_v) {
68 const int half_width = (width + 1) / 2;
henrikg91d6ede2015-09-17 00:24:34 -070069 RTC_DCHECK_GT(width, 0);
70 RTC_DCHECK_GT(height, 0);
71 RTC_DCHECK_GE(stride_y, width);
72 RTC_DCHECK_GE(stride_u, half_width);
73 RTC_DCHECK_GE(stride_v, half_width);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070074
75 // Creating empty frame - reset all values.
76 timestamp_ = 0;
77 ntp_time_ms_ = 0;
78 render_time_ms_ = 0;
79 rotation_ = kVideoRotation_0;
80
81 // Check if it's safe to reuse allocation.
82 if (video_frame_buffer_ && video_frame_buffer_->HasOneRef() &&
83 !video_frame_buffer_->native_handle() &&
84 width == video_frame_buffer_->width() &&
85 height == video_frame_buffer_->height() && stride_y == stride(kYPlane) &&
86 stride_u == stride(kUPlane) && stride_v == stride(kVPlane)) {
87 return 0;
88 }
89
90 // Need to allocate new buffer.
91 video_frame_buffer_ = new rtc::RefCountedObject<I420Buffer>(
92 width, height, stride_y, stride_u, stride_v);
93 return 0;
94}
95
96int VideoFrame::CreateFrame(const uint8_t* buffer_y,
97 const uint8_t* buffer_u,
98 const uint8_t* buffer_v,
99 int width,
100 int height,
101 int stride_y,
102 int stride_u,
103 int stride_v) {
104 return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y,
105 stride_u, stride_v, kVideoRotation_0);
106}
107
108int VideoFrame::CreateFrame(const uint8_t* buffer_y,
109 const uint8_t* buffer_u,
110 const uint8_t* buffer_v,
111 int width,
112 int height,
113 int stride_y,
114 int stride_u,
115 int stride_v,
116 VideoRotation rotation) {
117 const int half_height = (height + 1) / 2;
118 const int expected_size_y = height * stride_y;
119 const int expected_size_u = half_height * stride_u;
120 const int expected_size_v = half_height * stride_v;
121 CreateEmptyFrame(width, height, stride_y, stride_u, stride_v);
122 memcpy(buffer(kYPlane), buffer_y, expected_size_y);
123 memcpy(buffer(kUPlane), buffer_u, expected_size_u);
124 memcpy(buffer(kVPlane), buffer_v, expected_size_v);
125 rotation_ = rotation;
126 return 0;
127}
128
129int VideoFrame::CreateFrame(const uint8_t* buffer,
130 int width,
131 int height,
132 VideoRotation rotation) {
133 const int stride_y = width;
134 const int stride_uv = (width + 1) / 2;
135
136 const uint8_t* buffer_y = buffer;
137 const uint8_t* buffer_u = buffer_y + stride_y * height;
138 const uint8_t* buffer_v = buffer_u + stride_uv * ((height + 1) / 2);
139 return CreateFrame(buffer_y, buffer_u, buffer_v, width, height, stride_y,
140 stride_uv, stride_uv, rotation);
141}
142
143int VideoFrame::CopyFrame(const VideoFrame& videoFrame) {
144 if (videoFrame.IsZeroSize()) {
145 video_frame_buffer_ = nullptr;
146 } else if (videoFrame.native_handle()) {
147 video_frame_buffer_ = videoFrame.video_frame_buffer();
148 } else {
149 CreateFrame(videoFrame.buffer(kYPlane), videoFrame.buffer(kUPlane),
150 videoFrame.buffer(kVPlane), videoFrame.width(),
151 videoFrame.height(), videoFrame.stride(kYPlane),
152 videoFrame.stride(kUPlane), videoFrame.stride(kVPlane));
153 }
154
155 timestamp_ = videoFrame.timestamp_;
156 ntp_time_ms_ = videoFrame.ntp_time_ms_;
157 render_time_ms_ = videoFrame.render_time_ms_;
158 rotation_ = videoFrame.rotation_;
159 return 0;
160}
161
162void VideoFrame::ShallowCopy(const VideoFrame& videoFrame) {
163 video_frame_buffer_ = videoFrame.video_frame_buffer();
164 timestamp_ = videoFrame.timestamp_;
165 ntp_time_ms_ = videoFrame.ntp_time_ms_;
166 render_time_ms_ = videoFrame.render_time_ms_;
167 rotation_ = videoFrame.rotation_;
168}
169
170void VideoFrame::Reset() {
171 video_frame_buffer_ = nullptr;
172 timestamp_ = 0;
173 ntp_time_ms_ = 0;
174 render_time_ms_ = 0;
175 rotation_ = kVideoRotation_0;
176}
177
178uint8_t* VideoFrame::buffer(PlaneType type) {
Magnus Jedvert3318f982015-08-26 16:06:21 +0200179 return video_frame_buffer_ ? video_frame_buffer_->MutableData(type)
180 : nullptr;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700181}
182
183const uint8_t* VideoFrame::buffer(PlaneType type) const {
Magnus Jedvert3318f982015-08-26 16:06:21 +0200184 return video_frame_buffer_ ? video_frame_buffer_->data(type) : nullptr;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700185}
186
187int VideoFrame::allocated_size(PlaneType type) const {
188 const int plane_height = (type == kYPlane) ? height() : (height() + 1) / 2;
189 return plane_height * stride(type);
190}
191
192int VideoFrame::stride(PlaneType type) const {
193 return video_frame_buffer_ ? video_frame_buffer_->stride(type) : 0;
194}
195
196int VideoFrame::width() const {
197 return video_frame_buffer_ ? video_frame_buffer_->width() : 0;
198}
199
200int VideoFrame::height() const {
201 return video_frame_buffer_ ? video_frame_buffer_->height() : 0;
202}
203
204bool VideoFrame::IsZeroSize() const {
205 return !video_frame_buffer_;
206}
207
208void* VideoFrame::native_handle() const {
209 return video_frame_buffer_ ? video_frame_buffer_->native_handle() : nullptr;
210}
211
212rtc::scoped_refptr<VideoFrameBuffer> VideoFrame::video_frame_buffer() const {
213 return video_frame_buffer_;
214}
215
216void VideoFrame::set_video_frame_buffer(
217 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer) {
218 video_frame_buffer_ = buffer;
219}
220
Peter Boströmeb66e802015-06-05 11:08:03 +0200221VideoFrame VideoFrame::ConvertNativeToI420Frame() const {
henrikg91d6ede2015-09-17 00:24:34 -0700222 RTC_DCHECK(native_handle());
Peter Boströmeb66e802015-06-05 11:08:03 +0200223 VideoFrame frame;
224 frame.ShallowCopy(*this);
225 frame.set_video_frame_buffer(video_frame_buffer_->NativeToI420Buffer());
226 return frame;
227}
228
jackychen67e94fb2016-01-11 21:34:07 -0800229bool VideoFrame::EqualsFrame(const VideoFrame& frame) const {
jackychena276e732016-01-13 05:36:31 -0800230 if (width() != frame.width() || height() != frame.height() ||
231 stride(kYPlane) != frame.stride(kYPlane) ||
232 stride(kUPlane) != frame.stride(kUPlane) ||
233 stride(kVPlane) != frame.stride(kVPlane) ||
234 timestamp() != frame.timestamp() ||
235 ntp_time_ms() != frame.ntp_time_ms() ||
236 render_time_ms() != frame.render_time_ms()) {
jackychen67e94fb2016-01-11 21:34:07 -0800237 return false;
238 }
jackychena276e732016-01-13 05:36:31 -0800239 const int half_width = (width() + 1) / 2;
240 const int half_height = (height() + 1) / 2;
241 return EqualPlane(buffer(kYPlane), frame.buffer(kYPlane),
242 stride(kYPlane), width(), height()) &&
243 EqualPlane(buffer(kUPlane), frame.buffer(kUPlane),
244 stride(kUPlane), half_width, half_height) &&
245 EqualPlane(buffer(kVPlane), frame.buffer(kVPlane),
246 stride(kVPlane), half_width, half_height);
jackychen67e94fb2016-01-11 21:34:07 -0800247}
248
hbosd6648362016-01-21 05:43:11 -0800249size_t EncodedImage::GetBufferPaddingBytes(VideoCodecType codec_type) {
250 switch (codec_type) {
251 case kVideoCodecVP8:
252 case kVideoCodecVP9:
253 return 0;
254 case kVideoCodecH264:
255 return kBufferPaddingBytesH264;
256 case kVideoCodecI420:
257 case kVideoCodecRED:
258 case kVideoCodecULPFEC:
259 case kVideoCodecGeneric:
260 case kVideoCodecUnknown:
261 return 0;
262 }
263 RTC_NOTREACHED();
264 return 0;
265}
266
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700267} // namespace webrtc