blob: ae770c6039a52572be08fbfd4b8ba3ba93b77287 [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
2 * Copyright (c) 2015 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#include "webrtc/api/video/i420_buffer.h"
11
12#include <string.h>
13
14#include <algorithm>
15#include <utility>
16
17#include "webrtc/base/checks.h"
18#include "webrtc/base/keep_ref_until_done.h"
19#include "libyuv/convert.h"
20#include "libyuv/planar_functions.h"
21#include "libyuv/scale.h"
22
23// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
24static const int kBufferAlignment = 64;
25
26namespace webrtc {
27
28namespace {
29
30int I420DataSize(int height, int stride_y, int stride_u, int stride_v) {
31 return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2);
32}
33
34} // namespace
35
36I420Buffer::I420Buffer(int width, int height)
37 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {
38}
39
40I420Buffer::I420Buffer(int width,
41 int height,
42 int stride_y,
43 int stride_u,
44 int stride_v)
45 : width_(width),
46 height_(height),
47 stride_y_(stride_y),
48 stride_u_(stride_u),
49 stride_v_(stride_v),
50 data_(static_cast<uint8_t*>(AlignedMalloc(
51 I420DataSize(height, stride_y, stride_u, stride_v),
52 kBufferAlignment))) {
53 RTC_DCHECK_GT(width, 0);
54 RTC_DCHECK_GT(height, 0);
55 RTC_DCHECK_GE(stride_y, width);
56 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
57 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
58}
59
60I420Buffer::~I420Buffer() {
61}
62
63// static
64rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) {
65 return new rtc::RefCountedObject<I420Buffer>(width, height);
66}
67
68// static
69rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width,
70 int height,
71 int stride_y,
72 int stride_u,
73 int stride_v) {
74 return new rtc::RefCountedObject<I420Buffer>(
75 width, height, stride_y, stride_u, stride_v);
76}
77
78// static
79rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
80 const VideoFrameBuffer& source) {
81 return Copy(source.width(), source.height(),
82 source.DataY(), source.StrideY(),
83 source.DataU(), source.StrideU(),
84 source.DataV(), source.StrideV());
85}
86
87// static
88rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
89 int width, int height,
90 const uint8_t* data_y, int stride_y,
91 const uint8_t* data_u, int stride_u,
92 const uint8_t* data_v, int stride_v) {
93 // Note: May use different strides than the input data.
94 rtc::scoped_refptr<I420Buffer> buffer = Create(width, height);
95 RTC_CHECK_EQ(0, libyuv::I420Copy(data_y, stride_y,
96 data_u, stride_u,
97 data_v, stride_v,
98 buffer->MutableDataY(), buffer->StrideY(),
99 buffer->MutableDataU(), buffer->StrideU(),
100 buffer->MutableDataV(), buffer->StrideV(),
101 width, height));
102 return buffer;
103}
104
105// static
106rtc::scoped_refptr<I420Buffer> I420Buffer::Rotate(
107 const VideoFrameBuffer& src, VideoRotation rotation) {
108 RTC_CHECK(src.DataY());
109 RTC_CHECK(src.DataU());
110 RTC_CHECK(src.DataV());
111
112 int rotated_width = src.width();
113 int rotated_height = src.height();
114 if (rotation == webrtc::kVideoRotation_90 ||
115 rotation == webrtc::kVideoRotation_270) {
116 std::swap(rotated_width, rotated_height);
117 }
118
119 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
120 I420Buffer::Create(rotated_width, rotated_height);
121
122 RTC_CHECK_EQ(0, libyuv::I420Rotate(
123 src.DataY(), src.StrideY(),
124 src.DataU(), src.StrideU(),
125 src.DataV(), src.StrideV(),
126 buffer->MutableDataY(), buffer->StrideY(), buffer->MutableDataU(),
127 buffer->StrideU(), buffer->MutableDataV(), buffer->StrideV(),
128 src.width(), src.height(),
129 static_cast<libyuv::RotationMode>(rotation)));
130
131 return buffer;
132}
133
nisseaf916892017-01-10 07:44:26 -0800134void I420Buffer::InitializeData() {
135 memset(data_.get(), 0,
136 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
137}
138
139int I420Buffer::width() const {
140 return width_;
141}
142
143int I420Buffer::height() const {
144 return height_;
145}
146
147const uint8_t* I420Buffer::DataY() const {
148 return data_.get();
149}
150const uint8_t* I420Buffer::DataU() const {
151 return data_.get() + stride_y_ * height_;
152}
153const uint8_t* I420Buffer::DataV() const {
154 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
155}
156
157int I420Buffer::StrideY() const {
158 return stride_y_;
159}
160int I420Buffer::StrideU() const {
161 return stride_u_;
162}
163int I420Buffer::StrideV() const {
164 return stride_v_;
165}
166
nisseaf916892017-01-10 07:44:26 -0800167uint8_t* I420Buffer::MutableDataY() {
168 return const_cast<uint8_t*>(DataY());
169}
170uint8_t* I420Buffer::MutableDataU() {
171 return const_cast<uint8_t*>(DataU());
172}
173uint8_t* I420Buffer::MutableDataV() {
174 return const_cast<uint8_t*>(DataV());
175}
176
177// static
178void I420Buffer::SetBlack(I420Buffer* buffer) {
179 RTC_CHECK(libyuv::I420Rect(buffer->MutableDataY(), buffer->StrideY(),
180 buffer->MutableDataU(), buffer->StrideU(),
181 buffer->MutableDataV(), buffer->StrideV(),
182 0, 0, buffer->width(), buffer->height(),
183 0, 128, 128) == 0);
184}
185
186void I420Buffer::CropAndScaleFrom(
187 const VideoFrameBuffer& src,
188 int offset_x,
189 int offset_y,
190 int crop_width,
191 int crop_height) {
192 RTC_CHECK_LE(crop_width, src.width());
193 RTC_CHECK_LE(crop_height, src.height());
194 RTC_CHECK_LE(crop_width + offset_x, src.width());
195 RTC_CHECK_LE(crop_height + offset_y, src.height());
196 RTC_CHECK_GE(offset_x, 0);
197 RTC_CHECK_GE(offset_y, 0);
198
199 // Make sure offset is even so that u/v plane becomes aligned.
200 const int uv_offset_x = offset_x / 2;
201 const int uv_offset_y = offset_y / 2;
202 offset_x = uv_offset_x * 2;
203 offset_y = uv_offset_y * 2;
204
205 const uint8_t* y_plane =
206 src.DataY() + src.StrideY() * offset_y + offset_x;
207 const uint8_t* u_plane =
208 src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
209 const uint8_t* v_plane =
210 src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
211 int res = libyuv::I420Scale(y_plane, src.StrideY(),
212 u_plane, src.StrideU(),
213 v_plane, src.StrideV(),
214 crop_width, crop_height,
215 MutableDataY(), StrideY(),
216 MutableDataU(), StrideU(),
217 MutableDataV(), StrideV(),
218 width(), height(), libyuv::kFilterBox);
219
220 RTC_DCHECK_EQ(res, 0);
221}
222
223void I420Buffer::CropAndScaleFrom(
224 const VideoFrameBuffer& src) {
225 const int crop_width =
226 std::min(src.width(), width() * src.height() / height());
227 const int crop_height =
228 std::min(src.height(), height() * src.width() / width());
229
230 CropAndScaleFrom(
231 src,
232 (src.width() - crop_width) / 2, (src.height() - crop_height) / 2,
233 crop_width, crop_height);
234}
235
236void I420Buffer::ScaleFrom(const VideoFrameBuffer& src) {
237 CropAndScaleFrom(src, 0, 0, src.width(), src.height());
238}
239
240} // namespace webrtc