Stefan Mitic | ffdc680 | 2022-02-08 15:00:16 | [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/i444_buffer.h" |
| 11 | |
| 12 | #include <string.h> |
| 13 | |
| 14 | #include <algorithm> |
| 15 | #include <utility> |
| 16 | |
Niels Möller | 7c8c4db | 2022-06-13 08:36:38 | [diff] [blame] | 17 | #include "api/make_ref_counted.h" |
Stefan Mitic | ffdc680 | 2022-02-08 15:00:16 | [diff] [blame] | 18 | #include "api/video/i420_buffer.h" |
| 19 | #include "rtc_base/checks.h" |
Stefan Mitic | ffdc680 | 2022-02-08 15:00:16 | [diff] [blame] | 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 I444DataSize(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 | } // namespace |
| 36 | |
| 37 | I444Buffer::I444Buffer(int width, int height) |
| 38 | : I444Buffer(width, height, width, (width), (width)) {} |
| 39 | |
| 40 | I444Buffer::I444Buffer(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*>( |
| 51 | AlignedMalloc(I444DataSize(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)); |
| 57 | RTC_DCHECK_GE(stride_v, (width)); |
| 58 | } |
| 59 | |
| 60 | I444Buffer::~I444Buffer() {} |
| 61 | |
| 62 | // static |
| 63 | rtc::scoped_refptr<I444Buffer> I444Buffer::Create(int width, int height) { |
| 64 | return rtc::make_ref_counted<I444Buffer>(width, height); |
| 65 | } |
| 66 | |
| 67 | // static |
| 68 | rtc::scoped_refptr<I444Buffer> I444Buffer::Create(int width, |
| 69 | int height, |
| 70 | int stride_y, |
| 71 | int stride_u, |
| 72 | int stride_v) { |
| 73 | return rtc::make_ref_counted<I444Buffer>(width, height, stride_y, stride_u, |
| 74 | stride_v); |
| 75 | } |
| 76 | |
| 77 | // static |
| 78 | rtc::scoped_refptr<I444Buffer> I444Buffer::Copy( |
| 79 | const I444BufferInterface& source) { |
| 80 | return Copy(source.width(), source.height(), source.DataY(), source.StrideY(), |
| 81 | source.DataU(), source.StrideU(), source.DataV(), |
| 82 | source.StrideV()); |
| 83 | } |
| 84 | |
| 85 | // static |
| 86 | rtc::scoped_refptr<I444Buffer> I444Buffer::Copy(int width, |
| 87 | int height, |
| 88 | const uint8_t* data_y, |
| 89 | int stride_y, |
| 90 | const uint8_t* data_u, |
| 91 | int stride_u, |
| 92 | const uint8_t* data_v, |
| 93 | int stride_v) { |
| 94 | // Note: May use different strides than the input data. |
| 95 | rtc::scoped_refptr<I444Buffer> buffer = Create(width, height); |
| 96 | RTC_CHECK_EQ(0, libyuv::I444Copy(data_y, stride_y, data_u, stride_u, data_v, |
| 97 | stride_v, buffer->MutableDataY(), |
| 98 | buffer->StrideY(), buffer->MutableDataU(), |
| 99 | buffer->StrideU(), buffer->MutableDataV(), |
| 100 | buffer->StrideV(), width, height)); |
| 101 | return buffer; |
| 102 | } |
| 103 | |
| 104 | // static |
| 105 | rtc::scoped_refptr<I444Buffer> I444Buffer::Rotate( |
| 106 | const I444BufferInterface& src, |
| 107 | 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::I444Buffer> buffer = |
| 120 | I444Buffer::Create(rotated_width, rotated_height); |
| 121 | |
| 122 | RTC_CHECK_EQ(0, |
| 123 | libyuv::I444Rotate( |
| 124 | src.DataY(), src.StrideY(), src.DataU(), src.StrideU(), |
| 125 | src.DataV(), src.StrideV(), buffer->MutableDataY(), |
| 126 | buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(), |
| 127 | buffer->MutableDataV(), buffer->StrideV(), src.width(), |
| 128 | src.height(), static_cast<libyuv::RotationMode>(rotation))); |
| 129 | |
| 130 | return buffer; |
| 131 | } |
| 132 | |
| 133 | rtc::scoped_refptr<I420BufferInterface> I444Buffer::ToI420() { |
| 134 | rtc::scoped_refptr<I420Buffer> i420_buffer = |
| 135 | I420Buffer::Create(width(), height()); |
| 136 | libyuv::I444ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(), |
| 137 | i420_buffer->MutableDataY(), i420_buffer->StrideY(), |
| 138 | i420_buffer->MutableDataU(), i420_buffer->StrideU(), |
| 139 | i420_buffer->MutableDataV(), i420_buffer->StrideV(), |
| 140 | width(), height()); |
| 141 | return i420_buffer; |
| 142 | } |
| 143 | |
| 144 | void I444Buffer::InitializeData() { |
| 145 | memset(data_.get(), 0, |
| 146 | I444DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 147 | } |
| 148 | |
| 149 | int I444Buffer::width() const { |
| 150 | return width_; |
| 151 | } |
| 152 | |
| 153 | int I444Buffer::height() const { |
| 154 | return height_; |
| 155 | } |
| 156 | |
| 157 | const uint8_t* I444Buffer::DataY() const { |
| 158 | return data_.get(); |
| 159 | } |
| 160 | const uint8_t* I444Buffer::DataU() const { |
| 161 | return data_.get() + stride_y_ * height_; |
| 162 | } |
| 163 | const uint8_t* I444Buffer::DataV() const { |
| 164 | return data_.get() + stride_y_ * height_ + stride_u_ * ((height_)); |
| 165 | } |
| 166 | |
| 167 | int I444Buffer::StrideY() const { |
| 168 | return stride_y_; |
| 169 | } |
| 170 | int I444Buffer::StrideU() const { |
| 171 | return stride_u_; |
| 172 | } |
| 173 | int I444Buffer::StrideV() const { |
| 174 | return stride_v_; |
| 175 | } |
| 176 | |
| 177 | uint8_t* I444Buffer::MutableDataY() { |
| 178 | return const_cast<uint8_t*>(DataY()); |
| 179 | } |
| 180 | uint8_t* I444Buffer::MutableDataU() { |
| 181 | return const_cast<uint8_t*>(DataU()); |
| 182 | } |
| 183 | uint8_t* I444Buffer::MutableDataV() { |
| 184 | return const_cast<uint8_t*>(DataV()); |
| 185 | } |
| 186 | |
| 187 | void I444Buffer::CropAndScaleFrom(const I444BufferInterface& 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 | const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x; |
| 200 | const uint8_t* u_plane = src.DataU() + src.StrideU() * offset_y + offset_x; |
| 201 | const uint8_t* v_plane = src.DataV() + src.StrideV() * offset_y + offset_x; |
| 202 | int res = |
| 203 | libyuv::I444Scale(y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane, |
| 204 | src.StrideV(), crop_width, crop_height, MutableDataY(), |
| 205 | StrideY(), MutableDataU(), StrideU(), MutableDataV(), |
| 206 | StrideV(), width(), height(), libyuv::kFilterBox); |
| 207 | |
| 208 | RTC_DCHECK_EQ(res, 0); |
| 209 | } |
| 210 | |
| 211 | } // namespace webrtc |