blob: 62fa1837ed96d2bccdb949c2fcc7fbe439ba499d [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 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "api/video/i420_buffer.h"
nisseaf916892017-01-10 07:44:26 -080011
12#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
nisseaf916892017-01-10 07:44:26 -080014#include <algorithm>
15#include <utility>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/ref_counted_object.h"
Mirko Bonadei65432062017-12-11 09:32:13 +010019#include "third_party/libyuv/include/libyuv/convert.h"
20#include "third_party/libyuv/include/libyuv/planar_functions.h"
21#include "third_party/libyuv/include/libyuv/scale.h"
nisseaf916892017-01-10 07:44:26 -080022
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)
Yves Gerey665174f2018-06-19 15:03:05 +020037 : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {}
nisseaf916892017-01-10 07:44:26 -080038
39I420Buffer::I420Buffer(int width,
40 int height,
41 int stride_y,
42 int stride_u,
43 int stride_v)
44 : width_(width),
45 height_(height),
46 stride_y_(stride_y),
47 stride_u_(stride_u),
48 stride_v_(stride_v),
Yves Gerey665174f2018-06-19 15:03:05 +020049 data_(static_cast<uint8_t*>(
50 AlignedMalloc(I420DataSize(height, stride_y, stride_u, stride_v),
51 kBufferAlignment))) {
nisseaf916892017-01-10 07:44:26 -080052 RTC_DCHECK_GT(width, 0);
53 RTC_DCHECK_GT(height, 0);
54 RTC_DCHECK_GE(stride_y, width);
55 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
56 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
57}
58
Yves Gerey665174f2018-06-19 15:03:05 +020059I420Buffer::~I420Buffer() {}
nisseaf916892017-01-10 07:44:26 -080060
61// static
62rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) {
63 return new rtc::RefCountedObject<I420Buffer>(width, height);
64}
65
66// static
67rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width,
68 int height,
69 int stride_y,
70 int stride_u,
71 int stride_v) {
Yves Gerey665174f2018-06-19 15:03:05 +020072 return new rtc::RefCountedObject<I420Buffer>(width, height, stride_y,
73 stride_u, stride_v);
nisseaf916892017-01-10 07:44:26 -080074}
75
76// static
77rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
magjed3f075492017-06-01 10:02:26 -070078 const I420BufferInterface& source) {
Yves Gerey665174f2018-06-19 15:03:05 +020079 return Copy(source.width(), source.height(), source.DataY(), source.StrideY(),
80 source.DataU(), source.StrideU(), source.DataV(),
81 source.StrideV());
nisseaf916892017-01-10 07:44:26 -080082}
83
84// static
Yves Gerey665174f2018-06-19 15:03:05 +020085rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(int width,
86 int height,
87 const uint8_t* data_y,
88 int stride_y,
89 const uint8_t* data_u,
90 int stride_u,
91 const uint8_t* data_v,
92 int stride_v) {
nisseaf916892017-01-10 07:44:26 -080093 // Note: May use different strides than the input data.
94 rtc::scoped_refptr<I420Buffer> buffer = Create(width, height);
Yves Gerey665174f2018-06-19 15:03:05 +020095 RTC_CHECK_EQ(0, libyuv::I420Copy(data_y, stride_y, data_u, stride_u, data_v,
96 stride_v, buffer->MutableDataY(),
97 buffer->StrideY(), buffer->MutableDataU(),
98 buffer->StrideU(), buffer->MutableDataV(),
99 buffer->StrideV(), width, height));
nisseaf916892017-01-10 07:44:26 -0800100 return buffer;
101}
102
103// static
104rtc::scoped_refptr<I420Buffer> I420Buffer::Rotate(
magjed3f075492017-06-01 10:02:26 -0700105 const I420BufferInterface& src,
106 VideoRotation rotation) {
nisseaf916892017-01-10 07:44:26 -0800107 RTC_CHECK(src.DataY());
108 RTC_CHECK(src.DataU());
109 RTC_CHECK(src.DataV());
110
111 int rotated_width = src.width();
112 int rotated_height = src.height();
113 if (rotation == webrtc::kVideoRotation_90 ||
114 rotation == webrtc::kVideoRotation_270) {
115 std::swap(rotated_width, rotated_height);
116 }
117
118 rtc::scoped_refptr<webrtc::I420Buffer> buffer =
119 I420Buffer::Create(rotated_width, rotated_height);
120
Yves Gerey665174f2018-06-19 15:03:05 +0200121 RTC_CHECK_EQ(0,
122 libyuv::I420Rotate(
123 src.DataY(), src.StrideY(), src.DataU(), src.StrideU(),
124 src.DataV(), src.StrideV(), buffer->MutableDataY(),
125 buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
126 buffer->MutableDataV(), buffer->StrideV(), src.width(),
127 src.height(), static_cast<libyuv::RotationMode>(rotation)));
nisseaf916892017-01-10 07:44:26 -0800128
129 return buffer;
130}
131
nisseaf916892017-01-10 07:44:26 -0800132void I420Buffer::InitializeData() {
133 memset(data_.get(), 0,
134 I420DataSize(height_, stride_y_, stride_u_, stride_v_));
135}
136
137int I420Buffer::width() const {
138 return width_;
139}
140
141int I420Buffer::height() const {
142 return height_;
143}
144
145const uint8_t* I420Buffer::DataY() const {
146 return data_.get();
147}
148const uint8_t* I420Buffer::DataU() const {
149 return data_.get() + stride_y_ * height_;
150}
151const uint8_t* I420Buffer::DataV() const {
152 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
153}
154
155int I420Buffer::StrideY() const {
156 return stride_y_;
157}
158int I420Buffer::StrideU() const {
159 return stride_u_;
160}
161int I420Buffer::StrideV() const {
162 return stride_v_;
163}
164
nisseaf916892017-01-10 07:44:26 -0800165uint8_t* I420Buffer::MutableDataY() {
166 return const_cast<uint8_t*>(DataY());
167}
168uint8_t* I420Buffer::MutableDataU() {
169 return const_cast<uint8_t*>(DataU());
170}
171uint8_t* I420Buffer::MutableDataV() {
172 return const_cast<uint8_t*>(DataV());
173}
174
175// static
176void I420Buffer::SetBlack(I420Buffer* buffer) {
177 RTC_CHECK(libyuv::I420Rect(buffer->MutableDataY(), buffer->StrideY(),
178 buffer->MutableDataU(), buffer->StrideU(),
Yves Gerey665174f2018-06-19 15:03:05 +0200179 buffer->MutableDataV(), buffer->StrideV(), 0, 0,
180 buffer->width(), buffer->height(), 0, 128,
181 128) == 0);
nisseaf916892017-01-10 07:44:26 -0800182}
183
magjed3f075492017-06-01 10:02:26 -0700184void I420Buffer::CropAndScaleFrom(const I420BufferInterface& src,
185 int offset_x,
186 int offset_y,
187 int crop_width,
188 int crop_height) {
nisseaf916892017-01-10 07:44:26 -0800189 RTC_CHECK_LE(crop_width, src.width());
190 RTC_CHECK_LE(crop_height, src.height());
191 RTC_CHECK_LE(crop_width + offset_x, src.width());
192 RTC_CHECK_LE(crop_height + offset_y, src.height());
193 RTC_CHECK_GE(offset_x, 0);
194 RTC_CHECK_GE(offset_y, 0);
195
196 // Make sure offset is even so that u/v plane becomes aligned.
197 const int uv_offset_x = offset_x / 2;
198 const int uv_offset_y = offset_y / 2;
199 offset_x = uv_offset_x * 2;
200 offset_y = uv_offset_y * 2;
201
Yves Gerey665174f2018-06-19 15:03:05 +0200202 const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
nisseaf916892017-01-10 07:44:26 -0800203 const uint8_t* u_plane =
204 src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
205 const uint8_t* v_plane =
206 src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
Yves Gerey665174f2018-06-19 15:03:05 +0200207 int res =
208 libyuv::I420Scale(y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane,
209 src.StrideV(), crop_width, crop_height, MutableDataY(),
210 StrideY(), MutableDataU(), StrideU(), MutableDataV(),
211 StrideV(), width(), height(), libyuv::kFilterBox);
nisseaf916892017-01-10 07:44:26 -0800212
213 RTC_DCHECK_EQ(res, 0);
214}
215
magjed3f075492017-06-01 10:02:26 -0700216void I420Buffer::CropAndScaleFrom(const I420BufferInterface& src) {
nisseaf916892017-01-10 07:44:26 -0800217 const int crop_width =
218 std::min(src.width(), width() * src.height() / height());
219 const int crop_height =
220 std::min(src.height(), height() * src.width() / width());
221
Yves Gerey665174f2018-06-19 15:03:05 +0200222 CropAndScaleFrom(src, (src.width() - crop_width) / 2,
223 (src.height() - crop_height) / 2, crop_width, crop_height);
nisseaf916892017-01-10 07:44:26 -0800224}
225
magjed3f075492017-06-01 10:02:26 -0700226void I420Buffer::ScaleFrom(const I420BufferInterface& src) {
nisseaf916892017-01-10 07:44:26 -0800227 CropAndScaleFrom(src, 0, 0, src.width(), src.height());
228}
229
Ilya Nikolaevskiya9216602018-12-21 14:21:08 +0100230void I420Buffer::PasteFrom(const I420BufferInterface& picture,
231 int offset_col,
232 int offset_row) {
233 RTC_CHECK_LE(picture.width() + offset_col, width());
234 RTC_CHECK_LE(picture.height() + offset_row, height());
235 RTC_CHECK_GE(offset_col, 0);
236 RTC_CHECK_GE(offset_row, 0);
237
238 // Pasted picture has to be aligned so subsumpled UV plane isn't corrupted.
239 RTC_CHECK(offset_col % 2 == 0);
240 RTC_CHECK(offset_row % 2 == 0);
Ilya Nikolaevskiy12e5d392019-01-31 13:01:35 +0100241 RTC_CHECK(picture.width() % 2 == 0 ||
242 picture.width() + offset_col == width());
243 RTC_CHECK(picture.height() % 2 == 0 ||
244 picture.height() + offset_row == height());
Ilya Nikolaevskiya9216602018-12-21 14:21:08 +0100245
246 libyuv::CopyPlane(picture.DataY(), picture.StrideY(),
247 MutableDataY() + StrideY() * offset_row + offset_col,
248 StrideY(), picture.width(), picture.height());
249
250 libyuv::CopyPlane(
251 picture.DataU(), picture.StrideU(),
252 MutableDataU() + StrideU() * offset_row / 2 + offset_col / 2, StrideU(),
253 picture.width() / 2, picture.height() / 2);
254
255 libyuv::CopyPlane(
256 picture.DataV(), picture.StrideV(),
257 MutableDataV() + StrideV() * offset_row / 2 + offset_col / 2, StrideV(),
258 picture.width() / 2, picture.height() / 2);
259}
260
nisseaf916892017-01-10 07:44:26 -0800261} // namespace webrtc