blob: fddc1b57fde312361b072830a3be2f3501219f77 [file] [log] [blame]
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +01001/*
2 * Copyright (c) 2021 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 "api/video/i422_buffer.h"
11
12#include <string.h>
13
14#include <algorithm>
15#include <utility>
16
Niels Möller7c8c4db2022-06-13 10:36:38 +020017#include "api/make_ref_counted.h"
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010018#include "api/video/i420_buffer.h"
19#include "rtc_base/checks.h"
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010020#include "third_party/libyuv/include/libyuv/convert.h"
21#include "third_party/libyuv/include/libyuv/planar_functions.h"
22#include "third_party/libyuv/include/libyuv/scale.h"
23
24// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
25static const int kBufferAlignment = 64;
26
27namespace webrtc {
28
29namespace {
30
31int I422DataSize(int height, int stride_y, int stride_u, int stride_v) {
32 return stride_y * height + stride_u * height + stride_v * height;
33}
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010034} // namespace
35
36I422Buffer::I422Buffer(int width, int height)
37 : I422Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {}
38
39I422Buffer::I422Buffer(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),
49 data_(static_cast<uint8_t*>(
50 AlignedMalloc(I422DataSize(height, stride_y, stride_u, stride_v),
51 kBufferAlignment))) {
52 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
59I422Buffer::~I422Buffer() {}
60
61// static
62rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width, int height) {
63 return rtc::make_ref_counted<I422Buffer>(width, height);
64}
65
66// static
67rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width,
68 int height,
69 int stride_y,
70 int stride_u,
71 int stride_v) {
72 return rtc::make_ref_counted<I422Buffer>(width, height, stride_y, stride_u,
73 stride_v);
74}
75
76// static
77rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(
78 const I422BufferInterface& source) {
79 return Copy(source.width(), source.height(), source.DataY(), source.StrideY(),
80 source.DataU(), source.StrideU(), source.DataV(),
81 source.StrideV());
82}
83
84// static
85rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(
86 const I420BufferInterface& source) {
87 const int width = source.width();
88 const int height = source.height();
89 rtc::scoped_refptr<I422Buffer> buffer = Create(width, height);
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +010090 int res = libyuv::I420ToI422(
91 source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
92 source.DataV(), source.StrideV(), buffer->MutableDataY(),
93 buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
94 buffer->MutableDataV(), buffer->StrideV(), width, height);
95 RTC_DCHECK_EQ(res, 0);
96
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010097 return buffer;
98}
99
100// static
101rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(int width,
102 int height,
103 const uint8_t* data_y,
104 int stride_y,
105 const uint8_t* data_u,
106 int stride_u,
107 const uint8_t* data_v,
108 int stride_v) {
109 // Note: May use different strides than the input data.
110 rtc::scoped_refptr<I422Buffer> buffer = Create(width, height);
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +0100111 int res = libyuv::I422Copy(data_y, stride_y, data_u, stride_u, data_v,
112 stride_v, buffer->MutableDataY(),
113 buffer->StrideY(), buffer->MutableDataU(),
114 buffer->StrideU(), buffer->MutableDataV(),
115 buffer->StrideV(), width, height);
116 RTC_DCHECK_EQ(res, 0);
117
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +0100118 return buffer;
119}
120
121// static
122rtc::scoped_refptr<I422Buffer> I422Buffer::Rotate(
123 const I422BufferInterface& src,
124 VideoRotation rotation) {
125 RTC_CHECK(src.DataY());
126 RTC_CHECK(src.DataU());
127 RTC_CHECK(src.DataV());
128
129 int rotated_width = src.width();
130 int rotated_height = src.height();
131 if (rotation == webrtc::kVideoRotation_90 ||
132 rotation == webrtc::kVideoRotation_270) {
133 std::swap(rotated_width, rotated_height);
134 }
135
136 rtc::scoped_refptr<webrtc::I422Buffer> buffer =
137 I422Buffer::Create(rotated_width, rotated_height);
138
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +0100139 int res = libyuv::I422Rotate(
140 src.DataY(), src.StrideY(), src.DataU(), src.StrideU(), src.DataV(),
141 src.StrideV(), buffer->MutableDataY(), buffer->StrideY(),
142 buffer->MutableDataU(), buffer->StrideU(), buffer->MutableDataV(),
143 buffer->StrideV(), src.width(), src.height(),
144 static_cast<libyuv::RotationMode>(rotation));
145 RTC_DCHECK_EQ(res, 0);
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +0100146
147 return buffer;
148}
149
150rtc::scoped_refptr<I420BufferInterface> I422Buffer::ToI420() {
151 rtc::scoped_refptr<I420Buffer> i420_buffer =
152 I420Buffer::Create(width(), height());
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +0100153 int res = libyuv::I422ToI420(
154 DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
155 i420_buffer->MutableDataY(), i420_buffer->StrideY(),
156 i420_buffer->MutableDataU(), i420_buffer->StrideU(),
157 i420_buffer->MutableDataV(), i420_buffer->StrideV(), width(), height());
158 RTC_DCHECK_EQ(res, 0);
159
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +0100160 return i420_buffer;
161}
162
163void I422Buffer::InitializeData() {
164 memset(data_.get(), 0,
165 I422DataSize(height_, stride_y_, stride_u_, stride_v_));
166}
167
168int I422Buffer::width() const {
169 return width_;
170}
171
172int I422Buffer::height() const {
173 return height_;
174}
175
176const uint8_t* I422Buffer::DataY() const {
177 return data_.get();
178}
179const uint8_t* I422Buffer::DataU() const {
180 return data_.get() + stride_y_ * height_;
181}
182const uint8_t* I422Buffer::DataV() const {
183 return data_.get() + stride_y_ * height_ + stride_u_ * height_;
184}
185
186int I422Buffer::StrideY() const {
187 return stride_y_;
188}
189int I422Buffer::StrideU() const {
190 return stride_u_;
191}
192int I422Buffer::StrideV() const {
193 return stride_v_;
194}
195
196uint8_t* I422Buffer::MutableDataY() {
197 return const_cast<uint8_t*>(DataY());
198}
199uint8_t* I422Buffer::MutableDataU() {
200 return const_cast<uint8_t*>(DataU());
201}
202uint8_t* I422Buffer::MutableDataV() {
203 return const_cast<uint8_t*>(DataV());
204}
205
206void I422Buffer::CropAndScaleFrom(const I422BufferInterface& src,
207 int offset_x,
208 int offset_y,
209 int crop_width,
210 int crop_height) {
211 RTC_CHECK_LE(crop_width, src.width());
212 RTC_CHECK_LE(crop_height, src.height());
213 RTC_CHECK_LE(crop_width + offset_x, src.width());
214 RTC_CHECK_LE(crop_height + offset_y, src.height());
215 RTC_CHECK_GE(offset_x, 0);
216 RTC_CHECK_GE(offset_y, 0);
217
218 // Make sure offset is even so that u/v plane becomes aligned.
219 const int uv_offset_x = offset_x / 2;
220 const int uv_offset_y = offset_y;
221 offset_x = uv_offset_x * 2;
222
223 const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
224 const uint8_t* u_plane =
225 src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
226 const uint8_t* v_plane =
227 src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
Sergio Garcia Murillo8545eba2022-06-17 11:48:14 +0200228
Sergio Garcia Murillo00112742022-04-04 12:41:24 +0200229 int res =
Sergio Garcia Murillo8545eba2022-06-17 11:48:14 +0200230 libyuv::I422Scale(y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane,
231 src.StrideV(), crop_width, crop_height, MutableDataY(),
232 StrideY(), MutableDataU(), StrideU(), MutableDataV(),
233 StrideV(), width(), height(), libyuv::kFilterBox);
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +0100234 RTC_DCHECK_EQ(res, 0);
235}
236
237} // namespace webrtc