xref: /aosp_15_r20/external/webrtc/api/video/i422_buffer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
17 #include "api/make_ref_counted.h"
18 #include "api/video/i420_buffer.h"
19 #include "rtc_base/checks.h"
20 #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.
25 static const int kBufferAlignment = 64;
26 
27 namespace webrtc {
28 
29 namespace {
30 
I422DataSize(int height,int stride_y,int stride_u,int stride_v)31 int I422DataSize(int height, int stride_y, int stride_u, int stride_v) {
32   return stride_y * height + stride_u * height + stride_v * height;
33 }
34 }  // namespace
35 
I422Buffer(int width,int height)36 I422Buffer::I422Buffer(int width, int height)
37     : I422Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {}
38 
I422Buffer(int width,int height,int stride_y,int stride_u,int stride_v)39 I422Buffer::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 
~I422Buffer()59 I422Buffer::~I422Buffer() {}
60 
61 // static
Create(int width,int height)62 rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width, int height) {
63   return rtc::make_ref_counted<I422Buffer>(width, height);
64 }
65 
66 // static
Create(int width,int height,int stride_y,int stride_u,int stride_v)67 rtc::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
Copy(const I422BufferInterface & source)77 rtc::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
Copy(const I420BufferInterface & source)85 rtc::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);
90   RTC_CHECK_EQ(
91       0, libyuv::I420ToI422(
92              source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
93              source.DataV(), source.StrideV(), buffer->MutableDataY(),
94              buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
95              buffer->MutableDataV(), buffer->StrideV(), width, height));
96   return buffer;
97 }
98 
99 // static
Copy(int width,int height,const uint8_t * data_y,int stride_y,const uint8_t * data_u,int stride_u,const uint8_t * data_v,int stride_v)100 rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(int width,
101                                                 int height,
102                                                 const uint8_t* data_y,
103                                                 int stride_y,
104                                                 const uint8_t* data_u,
105                                                 int stride_u,
106                                                 const uint8_t* data_v,
107                                                 int stride_v) {
108   // Note: May use different strides than the input data.
109   rtc::scoped_refptr<I422Buffer> buffer = Create(width, height);
110   RTC_CHECK_EQ(0, libyuv::I422Copy(data_y, stride_y, data_u, stride_u, data_v,
111                                    stride_v, buffer->MutableDataY(),
112                                    buffer->StrideY(), buffer->MutableDataU(),
113                                    buffer->StrideU(), buffer->MutableDataV(),
114                                    buffer->StrideV(), width, height));
115   return buffer;
116 }
117 
118 // static
Rotate(const I422BufferInterface & src,VideoRotation rotation)119 rtc::scoped_refptr<I422Buffer> I422Buffer::Rotate(
120     const I422BufferInterface& src,
121     VideoRotation rotation) {
122   RTC_CHECK(src.DataY());
123   RTC_CHECK(src.DataU());
124   RTC_CHECK(src.DataV());
125 
126   int rotated_width = src.width();
127   int rotated_height = src.height();
128   if (rotation == webrtc::kVideoRotation_90 ||
129       rotation == webrtc::kVideoRotation_270) {
130     std::swap(rotated_width, rotated_height);
131   }
132 
133   rtc::scoped_refptr<webrtc::I422Buffer> buffer =
134       I422Buffer::Create(rotated_width, rotated_height);
135 
136   RTC_CHECK_EQ(0,
137                libyuv::I422Rotate(
138                    src.DataY(), src.StrideY(), src.DataU(), src.StrideU(),
139                    src.DataV(), src.StrideV(), buffer->MutableDataY(),
140                    buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
141                    buffer->MutableDataV(), buffer->StrideV(), src.width(),
142                    src.height(), static_cast<libyuv::RotationMode>(rotation)));
143 
144   return buffer;
145 }
146 
ToI420()147 rtc::scoped_refptr<I420BufferInterface> I422Buffer::ToI420() {
148   rtc::scoped_refptr<I420Buffer> i420_buffer =
149       I420Buffer::Create(width(), height());
150   libyuv::I422ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
151                      i420_buffer->MutableDataY(), i420_buffer->StrideY(),
152                      i420_buffer->MutableDataU(), i420_buffer->StrideU(),
153                      i420_buffer->MutableDataV(), i420_buffer->StrideV(),
154                      width(), height());
155   return i420_buffer;
156 }
157 
InitializeData()158 void I422Buffer::InitializeData() {
159   memset(data_.get(), 0,
160          I422DataSize(height_, stride_y_, stride_u_, stride_v_));
161 }
162 
width() const163 int I422Buffer::width() const {
164   return width_;
165 }
166 
height() const167 int I422Buffer::height() const {
168   return height_;
169 }
170 
DataY() const171 const uint8_t* I422Buffer::DataY() const {
172   return data_.get();
173 }
DataU() const174 const uint8_t* I422Buffer::DataU() const {
175   return data_.get() + stride_y_ * height_;
176 }
DataV() const177 const uint8_t* I422Buffer::DataV() const {
178   return data_.get() + stride_y_ * height_ + stride_u_ * height_;
179 }
180 
StrideY() const181 int I422Buffer::StrideY() const {
182   return stride_y_;
183 }
StrideU() const184 int I422Buffer::StrideU() const {
185   return stride_u_;
186 }
StrideV() const187 int I422Buffer::StrideV() const {
188   return stride_v_;
189 }
190 
MutableDataY()191 uint8_t* I422Buffer::MutableDataY() {
192   return const_cast<uint8_t*>(DataY());
193 }
MutableDataU()194 uint8_t* I422Buffer::MutableDataU() {
195   return const_cast<uint8_t*>(DataU());
196 }
MutableDataV()197 uint8_t* I422Buffer::MutableDataV() {
198   return const_cast<uint8_t*>(DataV());
199 }
200 
CropAndScaleFrom(const I422BufferInterface & src,int offset_x,int offset_y,int crop_width,int crop_height)201 void I422Buffer::CropAndScaleFrom(const I422BufferInterface& src,
202                                   int offset_x,
203                                   int offset_y,
204                                   int crop_width,
205                                   int crop_height) {
206   RTC_CHECK_LE(crop_width, src.width());
207   RTC_CHECK_LE(crop_height, src.height());
208   RTC_CHECK_LE(crop_width + offset_x, src.width());
209   RTC_CHECK_LE(crop_height + offset_y, src.height());
210   RTC_CHECK_GE(offset_x, 0);
211   RTC_CHECK_GE(offset_y, 0);
212 
213   // Make sure offset is even so that u/v plane becomes aligned.
214   const int uv_offset_x = offset_x / 2;
215   const int uv_offset_y = offset_y;
216   offset_x = uv_offset_x * 2;
217 
218   const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
219   const uint8_t* u_plane =
220       src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
221   const uint8_t* v_plane =
222       src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
223 
224   int res =
225       libyuv::I422Scale(y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane,
226                         src.StrideV(), crop_width, crop_height, MutableDataY(),
227                         StrideY(), MutableDataU(), StrideU(), MutableDataV(),
228                         StrideV(), width(), height(), libyuv::kFilterBox);
229 
230   RTC_DCHECK_EQ(res, 0);
231 }
232 
233 }  // namespace webrtc
234