nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "api/video/i420_buffer.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 11 | |
| 12 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame^] | 13 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | #include <utility> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/ref_counted_object.h" |
Mirko Bonadei | 6543206 | 2017-12-11 09:32:13 +0100 | [diff] [blame] | 19 | #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" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 22 | |
| 23 | // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD. |
| 24 | static const int kBufferAlignment = 64; |
| 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | int 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 | |
| 36 | I420Buffer::I420Buffer(int width, int height) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {} |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 38 | |
| 39 | I420Buffer::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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 49 | data_(static_cast<uint8_t*>( |
| 50 | AlignedMalloc(I420DataSize(height, stride_y, stride_u, stride_v), |
| 51 | kBufferAlignment))) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 59 | I420Buffer::~I420Buffer() {} |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 60 | |
| 61 | // static |
| 62 | rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) { |
| 63 | return new rtc::RefCountedObject<I420Buffer>(width, height); |
| 64 | } |
| 65 | |
| 66 | // static |
| 67 | rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, |
| 68 | int height, |
| 69 | int stride_y, |
| 70 | int stride_u, |
| 71 | int stride_v) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | return new rtc::RefCountedObject<I420Buffer>(width, height, stride_y, |
| 73 | stride_u, stride_v); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // static |
| 77 | rtc::scoped_refptr<I420Buffer> I420Buffer::Copy( |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 78 | const I420BufferInterface& source) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 79 | return Copy(source.width(), source.height(), source.DataY(), source.StrideY(), |
| 80 | source.DataU(), source.StrideU(), source.DataV(), |
| 81 | source.StrideV()); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // static |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 85 | rtc::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) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 93 | // Note: May use different strides than the input data. |
| 94 | rtc::scoped_refptr<I420Buffer> buffer = Create(width, height); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 95 | 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)); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 100 | return buffer; |
| 101 | } |
| 102 | |
| 103 | // static |
| 104 | rtc::scoped_refptr<I420Buffer> I420Buffer::Rotate( |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 105 | const I420BufferInterface& src, |
| 106 | VideoRotation rotation) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 107 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 121 | 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))); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 128 | |
| 129 | return buffer; |
| 130 | } |
| 131 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 132 | void I420Buffer::InitializeData() { |
| 133 | memset(data_.get(), 0, |
| 134 | I420DataSize(height_, stride_y_, stride_u_, stride_v_)); |
| 135 | } |
| 136 | |
| 137 | int I420Buffer::width() const { |
| 138 | return width_; |
| 139 | } |
| 140 | |
| 141 | int I420Buffer::height() const { |
| 142 | return height_; |
| 143 | } |
| 144 | |
| 145 | const uint8_t* I420Buffer::DataY() const { |
| 146 | return data_.get(); |
| 147 | } |
| 148 | const uint8_t* I420Buffer::DataU() const { |
| 149 | return data_.get() + stride_y_ * height_; |
| 150 | } |
| 151 | const uint8_t* I420Buffer::DataV() const { |
| 152 | return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2); |
| 153 | } |
| 154 | |
| 155 | int I420Buffer::StrideY() const { |
| 156 | return stride_y_; |
| 157 | } |
| 158 | int I420Buffer::StrideU() const { |
| 159 | return stride_u_; |
| 160 | } |
| 161 | int I420Buffer::StrideV() const { |
| 162 | return stride_v_; |
| 163 | } |
| 164 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 165 | uint8_t* I420Buffer::MutableDataY() { |
| 166 | return const_cast<uint8_t*>(DataY()); |
| 167 | } |
| 168 | uint8_t* I420Buffer::MutableDataU() { |
| 169 | return const_cast<uint8_t*>(DataU()); |
| 170 | } |
| 171 | uint8_t* I420Buffer::MutableDataV() { |
| 172 | return const_cast<uint8_t*>(DataV()); |
| 173 | } |
| 174 | |
| 175 | // static |
| 176 | void I420Buffer::SetBlack(I420Buffer* buffer) { |
| 177 | RTC_CHECK(libyuv::I420Rect(buffer->MutableDataY(), buffer->StrideY(), |
| 178 | buffer->MutableDataU(), buffer->StrideU(), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 179 | buffer->MutableDataV(), buffer->StrideV(), 0, 0, |
| 180 | buffer->width(), buffer->height(), 0, 128, |
| 181 | 128) == 0); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 182 | } |
| 183 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 184 | void I420Buffer::CropAndScaleFrom(const I420BufferInterface& src, |
| 185 | int offset_x, |
| 186 | int offset_y, |
| 187 | int crop_width, |
| 188 | int crop_height) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 189 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 202 | const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x; |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 203 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 207 | 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); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 212 | |
| 213 | RTC_DCHECK_EQ(res, 0); |
| 214 | } |
| 215 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 216 | void I420Buffer::CropAndScaleFrom(const I420BufferInterface& src) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 217 | 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 222 | CropAndScaleFrom(src, (src.width() - crop_width) / 2, |
| 223 | (src.height() - crop_height) / 2, crop_width, crop_height); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 224 | } |
| 225 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 226 | void I420Buffer::ScaleFrom(const I420BufferInterface& src) { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 227 | CropAndScaleFrom(src, 0, 0, src.width(), src.height()); |
| 228 | } |
| 229 | |
Ilya Nikolaevskiy | a921660 | 2018-12-21 14:21:08 +0100 | [diff] [blame] | 230 | void 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 Nikolaevskiy | 12e5d39 | 2019-01-31 13:01:35 +0100 | [diff] [blame] | 241 | 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 Nikolaevskiy | a921660 | 2018-12-21 14:21:08 +0100 | [diff] [blame] | 245 | |
| 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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 261 | } // namespace webrtc |