Sergio Garcia Murillo | b63536f | 2022-03-25 09:04:09 +0100 | [diff] [blame] | 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/video/i420_buffer.h" |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/ref_counted_object.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 | |
| 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 | |
| 35 | int I422Rotate(const uint8_t* src_y, |
| 36 | int src_stride_y, |
| 37 | const uint8_t* src_u, |
| 38 | int src_stride_u, |
| 39 | const uint8_t* src_v, |
| 40 | int src_stride_v, |
| 41 | uint8_t* dst_y, |
| 42 | int dst_stride_y, |
| 43 | uint8_t* dst_u, |
| 44 | int dst_stride_u, |
| 45 | uint8_t* dst_v, |
| 46 | int dst_stride_v, |
| 47 | int width, |
| 48 | int height, |
| 49 | enum libyuv::RotationMode mode) { |
| 50 | int halfwidth = (width + 1) >> 1; |
| 51 | int halfheight = (height + 1) >> 1; |
| 52 | if (!src_y || !src_u || !src_v || width <= 0 || height == 0 || !dst_y || |
| 53 | !dst_u || !dst_v) { |
| 54 | return -1; |
| 55 | } |
| 56 | // Negative height means invert the image. |
| 57 | if (height < 0) { |
| 58 | height = -height; |
| 59 | src_y = src_y + (height - 1) * src_stride_y; |
| 60 | src_u = src_u + (height - 1) * src_stride_u; |
| 61 | src_v = src_v + (height - 1) * src_stride_v; |
| 62 | src_stride_y = -src_stride_y; |
| 63 | src_stride_u = -src_stride_u; |
| 64 | src_stride_v = -src_stride_v; |
| 65 | } |
| 66 | |
| 67 | switch (mode) { |
| 68 | case libyuv::kRotate0: |
| 69 | // copy frame |
| 70 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 71 | height); |
| 72 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, halfwidth, |
| 73 | height); |
| 74 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, halfwidth, |
| 75 | height); |
| 76 | return 0; |
| 77 | case libyuv::kRotate90: |
| 78 | // We need to rotate and rescale, we use plane Y as temporal storage. |
| 79 | libyuv::RotatePlane90(src_u, src_stride_u, dst_y, height, halfwidth, |
| 80 | height); |
| 81 | libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_u, halfheight, |
| 82 | halfheight, width, libyuv::kFilterBilinear); |
| 83 | libyuv::RotatePlane90(src_v, src_stride_v, dst_y, height, halfwidth, |
| 84 | height); |
| 85 | libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_v, halfheight, |
| 86 | halfheight, width, libyuv::kFilterLinear); |
| 87 | libyuv::RotatePlane90(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 88 | height); |
| 89 | return 0; |
| 90 | case libyuv::kRotate270: |
| 91 | // We need to rotate and rescale, we use plane Y as temporal storage. |
| 92 | libyuv::RotatePlane270(src_u, src_stride_u, dst_y, height, halfwidth, |
| 93 | height); |
| 94 | libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_u, halfheight, |
| 95 | halfheight, width, libyuv::kFilterBilinear); |
| 96 | libyuv::RotatePlane270(src_v, src_stride_v, dst_y, height, halfwidth, |
| 97 | height); |
| 98 | libyuv::ScalePlane(dst_y, height, height, halfwidth, dst_v, halfheight, |
| 99 | halfheight, width, libyuv::kFilterLinear); |
| 100 | libyuv::RotatePlane270(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 101 | height); |
| 102 | |
| 103 | return 0; |
| 104 | case libyuv::kRotate180: |
| 105 | libyuv::RotatePlane180(src_y, src_stride_y, dst_y, dst_stride_y, width, |
| 106 | height); |
| 107 | libyuv::RotatePlane180(src_u, src_stride_u, dst_u, dst_stride_u, |
| 108 | halfwidth, height); |
| 109 | libyuv::RotatePlane180(src_v, src_stride_v, dst_v, dst_stride_v, |
| 110 | halfwidth, height); |
| 111 | return 0; |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | int I422Scale(const uint8_t* src_y, |
| 119 | int src_stride_y, |
| 120 | const uint8_t* src_u, |
| 121 | int src_stride_u, |
| 122 | const uint8_t* src_v, |
| 123 | int src_stride_v, |
| 124 | int src_width, |
| 125 | int src_height, |
| 126 | uint8_t* dst_y, |
| 127 | int dst_stride_y, |
| 128 | uint8_t* dst_u, |
| 129 | int dst_stride_u, |
| 130 | uint8_t* dst_v, |
| 131 | int dst_stride_v, |
| 132 | int dst_width, |
| 133 | int dst_height, |
| 134 | enum libyuv::FilterMode filtering) { |
| 135 | if (!src_y || !src_u || !src_v || src_width <= 0 || src_height == 0 || |
| 136 | src_width > 32768 || src_height > 32768 || !dst_y || !dst_u || !dst_v || |
| 137 | dst_width <= 0 || dst_height <= 0) { |
| 138 | return -1; |
| 139 | } |
| 140 | int src_halfwidth = (src_width + 1) >> 1; |
| 141 | int dst_halfwidth = (dst_width + 1) >> 1; |
| 142 | |
| 143 | libyuv::ScalePlane(src_y, src_stride_y, src_width, src_height, dst_y, |
| 144 | dst_stride_y, dst_width, dst_height, filtering); |
| 145 | libyuv::ScalePlane(src_u, src_stride_u, src_halfwidth, src_height, dst_u, |
| 146 | dst_stride_u, dst_halfwidth, dst_height, filtering); |
| 147 | libyuv::ScalePlane(src_v, src_stride_v, src_halfwidth, src_height, dst_v, |
| 148 | dst_stride_v, dst_halfwidth, dst_height, filtering); |
| 149 | return 0; |
| 150 | } |
| 151 | } // namespace |
| 152 | |
| 153 | I422Buffer::I422Buffer(int width, int height) |
| 154 | : I422Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {} |
| 155 | |
| 156 | I422Buffer::I422Buffer(int width, |
| 157 | int height, |
| 158 | int stride_y, |
| 159 | int stride_u, |
| 160 | int stride_v) |
| 161 | : width_(width), |
| 162 | height_(height), |
| 163 | stride_y_(stride_y), |
| 164 | stride_u_(stride_u), |
| 165 | stride_v_(stride_v), |
| 166 | data_(static_cast<uint8_t*>( |
| 167 | AlignedMalloc(I422DataSize(height, stride_y, stride_u, stride_v), |
| 168 | kBufferAlignment))) { |
| 169 | RTC_DCHECK_GT(width, 0); |
| 170 | RTC_DCHECK_GT(height, 0); |
| 171 | RTC_DCHECK_GE(stride_y, width); |
| 172 | RTC_DCHECK_GE(stride_u, (width + 1) / 2); |
| 173 | RTC_DCHECK_GE(stride_v, (width + 1) / 2); |
| 174 | } |
| 175 | |
| 176 | I422Buffer::~I422Buffer() {} |
| 177 | |
| 178 | // static |
| 179 | rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width, int height) { |
| 180 | return rtc::make_ref_counted<I422Buffer>(width, height); |
| 181 | } |
| 182 | |
| 183 | // static |
| 184 | rtc::scoped_refptr<I422Buffer> I422Buffer::Create(int width, |
| 185 | int height, |
| 186 | int stride_y, |
| 187 | int stride_u, |
| 188 | int stride_v) { |
| 189 | return rtc::make_ref_counted<I422Buffer>(width, height, stride_y, stride_u, |
| 190 | stride_v); |
| 191 | } |
| 192 | |
| 193 | // static |
| 194 | rtc::scoped_refptr<I422Buffer> I422Buffer::Copy( |
| 195 | const I422BufferInterface& source) { |
| 196 | return Copy(source.width(), source.height(), source.DataY(), source.StrideY(), |
| 197 | source.DataU(), source.StrideU(), source.DataV(), |
| 198 | source.StrideV()); |
| 199 | } |
| 200 | |
| 201 | // static |
| 202 | rtc::scoped_refptr<I422Buffer> I422Buffer::Copy( |
| 203 | const I420BufferInterface& source) { |
| 204 | const int width = source.width(); |
| 205 | const int height = source.height(); |
| 206 | rtc::scoped_refptr<I422Buffer> buffer = Create(width, height); |
| 207 | RTC_CHECK_EQ( |
| 208 | 0, libyuv::I420ToI422( |
| 209 | source.DataY(), source.StrideY(), source.DataU(), source.StrideU(), |
| 210 | source.DataV(), source.StrideV(), buffer->MutableDataY(), |
| 211 | buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(), |
| 212 | buffer->MutableDataV(), buffer->StrideV(), width, height)); |
| 213 | return buffer; |
| 214 | } |
| 215 | |
| 216 | // static |
| 217 | rtc::scoped_refptr<I422Buffer> I422Buffer::Copy(int width, |
| 218 | int height, |
| 219 | const uint8_t* data_y, |
| 220 | int stride_y, |
| 221 | const uint8_t* data_u, |
| 222 | int stride_u, |
| 223 | const uint8_t* data_v, |
| 224 | int stride_v) { |
| 225 | // Note: May use different strides than the input data. |
| 226 | rtc::scoped_refptr<I422Buffer> buffer = Create(width, height); |
| 227 | RTC_CHECK_EQ(0, libyuv::I422Copy(data_y, stride_y, data_u, stride_u, data_v, |
| 228 | stride_v, buffer->MutableDataY(), |
| 229 | buffer->StrideY(), buffer->MutableDataU(), |
| 230 | buffer->StrideU(), buffer->MutableDataV(), |
| 231 | buffer->StrideV(), width, height)); |
| 232 | return buffer; |
| 233 | } |
| 234 | |
| 235 | // static |
| 236 | rtc::scoped_refptr<I422Buffer> I422Buffer::Rotate( |
| 237 | const I422BufferInterface& src, |
| 238 | VideoRotation rotation) { |
| 239 | RTC_CHECK(src.DataY()); |
| 240 | RTC_CHECK(src.DataU()); |
| 241 | RTC_CHECK(src.DataV()); |
| 242 | |
| 243 | int rotated_width = src.width(); |
| 244 | int rotated_height = src.height(); |
| 245 | if (rotation == webrtc::kVideoRotation_90 || |
| 246 | rotation == webrtc::kVideoRotation_270) { |
| 247 | std::swap(rotated_width, rotated_height); |
| 248 | } |
| 249 | |
| 250 | rtc::scoped_refptr<webrtc::I422Buffer> buffer = |
| 251 | I422Buffer::Create(rotated_width, rotated_height); |
| 252 | |
| 253 | RTC_CHECK_EQ( |
| 254 | 0, |
| 255 | I422Rotate(src.DataY(), src.StrideY(), src.DataU(), src.StrideU(), |
| 256 | src.DataV(), src.StrideV(), buffer->MutableDataY(), |
| 257 | buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(), |
| 258 | buffer->MutableDataV(), buffer->StrideV(), src.width(), |
| 259 | src.height(), static_cast<libyuv::RotationMode>(rotation))); |
| 260 | |
| 261 | return buffer; |
| 262 | } |
| 263 | |
| 264 | rtc::scoped_refptr<I420BufferInterface> I422Buffer::ToI420() { |
| 265 | rtc::scoped_refptr<I420Buffer> i420_buffer = |
| 266 | I420Buffer::Create(width(), height()); |
| 267 | libyuv::I422ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(), |
| 268 | i420_buffer->MutableDataY(), i420_buffer->StrideY(), |
| 269 | i420_buffer->MutableDataU(), i420_buffer->StrideU(), |
| 270 | i420_buffer->MutableDataV(), i420_buffer->StrideV(), |
| 271 | width(), height()); |
| 272 | return i420_buffer; |
| 273 | } |
| 274 | |
| 275 | void I422Buffer::InitializeData() { |
| 276 | memset(data_.get(), 0, |
| 277 | I422DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 278 | } |
| 279 | |
| 280 | int I422Buffer::width() const { |
| 281 | return width_; |
| 282 | } |
| 283 | |
| 284 | int I422Buffer::height() const { |
| 285 | return height_; |
| 286 | } |
| 287 | |
| 288 | const uint8_t* I422Buffer::DataY() const { |
| 289 | return data_.get(); |
| 290 | } |
| 291 | const uint8_t* I422Buffer::DataU() const { |
| 292 | return data_.get() + stride_y_ * height_; |
| 293 | } |
| 294 | const uint8_t* I422Buffer::DataV() const { |
| 295 | return data_.get() + stride_y_ * height_ + stride_u_ * height_; |
| 296 | } |
| 297 | |
| 298 | int I422Buffer::StrideY() const { |
| 299 | return stride_y_; |
| 300 | } |
| 301 | int I422Buffer::StrideU() const { |
| 302 | return stride_u_; |
| 303 | } |
| 304 | int I422Buffer::StrideV() const { |
| 305 | return stride_v_; |
| 306 | } |
| 307 | |
| 308 | uint8_t* I422Buffer::MutableDataY() { |
| 309 | return const_cast<uint8_t*>(DataY()); |
| 310 | } |
| 311 | uint8_t* I422Buffer::MutableDataU() { |
| 312 | return const_cast<uint8_t*>(DataU()); |
| 313 | } |
| 314 | uint8_t* I422Buffer::MutableDataV() { |
| 315 | return const_cast<uint8_t*>(DataV()); |
| 316 | } |
| 317 | |
| 318 | void I422Buffer::CropAndScaleFrom(const I422BufferInterface& src, |
| 319 | int offset_x, |
| 320 | int offset_y, |
| 321 | int crop_width, |
| 322 | int crop_height) { |
| 323 | RTC_CHECK_LE(crop_width, src.width()); |
| 324 | RTC_CHECK_LE(crop_height, src.height()); |
| 325 | RTC_CHECK_LE(crop_width + offset_x, src.width()); |
| 326 | RTC_CHECK_LE(crop_height + offset_y, src.height()); |
| 327 | RTC_CHECK_GE(offset_x, 0); |
| 328 | RTC_CHECK_GE(offset_y, 0); |
| 329 | |
| 330 | // Make sure offset is even so that u/v plane becomes aligned. |
| 331 | const int uv_offset_x = offset_x / 2; |
| 332 | const int uv_offset_y = offset_y; |
| 333 | offset_x = uv_offset_x * 2; |
| 334 | |
| 335 | const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x; |
| 336 | const uint8_t* u_plane = |
| 337 | src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x; |
| 338 | const uint8_t* v_plane = |
| 339 | src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x; |
| 340 | int res = I422Scale(y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane, |
| 341 | src.StrideV(), crop_width, crop_height, MutableDataY(), |
| 342 | StrideY(), MutableDataU(), StrideU(), MutableDataV(), |
| 343 | StrideV(), width(), height(), libyuv::kFilterBox); |
| 344 | |
| 345 | RTC_DCHECK_EQ(res, 0); |
| 346 | } |
| 347 | |
| 348 | } // namespace webrtc |