| 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 |
| 11 | #include "api/video/nv12_buffer.h" |
| 12 |
| 13 | #include "api/make_ref_counted.h" |
| 14 | #include "api/video/i420_buffer.h" |
| 15 | #include "rtc_base/checks.h" |
| 16 | #include "third_party/libyuv/include/libyuv/convert.h" |
| 17 | #include "third_party/libyuv/include/libyuv/scale.h" |
| 18 |
| 19 | namespace webrtc { |
| 20 |
| 21 | namespace { |
| 22 |
| 23 | static const int kBufferAlignment = 64; |
| 24 |
| 25 | int NV12DataSize(int height, int stride_y, int stride_uv) { |
| 26 | return stride_y * height + stride_uv * ((height + 1) / 2); |
| 27 | } |
| 28 |
| 29 | } // namespace |
| 30 |
| 31 | NV12Buffer::NV12Buffer(int width, int height) |
| 32 | : NV12Buffer(width, height, width, width + width % 2) {} |
| 33 |
| 34 | NV12Buffer::NV12Buffer(int width, int height, int stride_y, int stride_uv) |
| 35 | : width_(width), |
| 36 | height_(height), |
| 37 | stride_y_(stride_y), |
| 38 | stride_uv_(stride_uv), |
| 39 | data_(static_cast<uint8_t*>( |
| 40 | AlignedMalloc(NV12DataSize(height_, stride_y_, stride_uv), |
| 41 | kBufferAlignment))) { |
| 42 | RTC_DCHECK_GT(width, 0); |
| 43 | RTC_DCHECK_GT(height, 0); |
| 44 | RTC_DCHECK_GE(stride_y, width); |
| 45 | RTC_DCHECK_GE(stride_uv, (width + width % 2)); |
| 46 | } |
| 47 |
| 48 | NV12Buffer::~NV12Buffer() = default; |
| 49 |
| 50 | // static |
| 51 | rtc::scoped_refptr<NV12Buffer> NV12Buffer::Create(int width, int height) { |
| 52 | return rtc::make_ref_counted<NV12Buffer>(width, height); |
| 53 | } |
| 54 |
| 55 | // static |
| 56 | rtc::scoped_refptr<NV12Buffer> NV12Buffer::Create(int width, |
| 57 | int height, |
| 58 | int stride_y, |
| 59 | int stride_uv) { |
| 60 | return rtc::make_ref_counted<NV12Buffer>(width, height, stride_y, stride_uv); |
| 61 | } |
| 62 |
| 63 | // static |
| 64 | rtc::scoped_refptr<NV12Buffer> NV12Buffer::Copy( |
| 65 | const I420BufferInterface& i420_buffer) { |
| 66 | rtc::scoped_refptr<NV12Buffer> buffer = |
| 67 | NV12Buffer::Create(i420_buffer.width(), i420_buffer.height()); |
| 68 | libyuv::I420ToNV12( |
| 69 | i420_buffer.DataY(), i420_buffer.StrideY(), i420_buffer.DataU(), |
| 70 | i420_buffer.StrideU(), i420_buffer.DataV(), i420_buffer.StrideV(), |
| 71 | buffer->MutableDataY(), buffer->StrideY(), buffer->MutableDataUV(), |
| 72 | buffer->StrideUV(), buffer->width(), buffer->height()); |
| 73 | return buffer; |
| 74 | } |
| 75 |
| 76 | rtc::scoped_refptr<I420BufferInterface> NV12Buffer::ToI420() { |
| 77 | rtc::scoped_refptr<I420Buffer> i420_buffer = |
| 78 | I420Buffer::Create(width(), height()); |
| 79 | libyuv::NV12ToI420(DataY(), StrideY(), DataUV(), StrideUV(), |
| 80 | i420_buffer->MutableDataY(), i420_buffer->StrideY(), |
| 81 | i420_buffer->MutableDataU(), i420_buffer->StrideU(), |
| 82 | i420_buffer->MutableDataV(), i420_buffer->StrideV(), |
| 83 | width(), height()); |
| 84 | return i420_buffer; |
| 85 | } |
| 86 |
| 87 | int NV12Buffer::width() const { |
| 88 | return width_; |
| 89 | } |
| 90 | int NV12Buffer::height() const { |
| 91 | return height_; |
| 92 | } |
| 93 |
| 94 | int NV12Buffer::StrideY() const { |
| 95 | return stride_y_; |
| 96 | } |
| 97 | int NV12Buffer::StrideUV() const { |
| 98 | return stride_uv_; |
| 99 | } |
| 100 |
| 101 | const uint8_t* NV12Buffer::DataY() const { |
| 102 | return data_.get(); |
| 103 | } |
| 104 |
| 105 | const uint8_t* NV12Buffer::DataUV() const { |
| 106 | return data_.get() + UVOffset(); |
| 107 | } |
| 108 |
| 109 | uint8_t* NV12Buffer::MutableDataY() { |
| 110 | return data_.get(); |
| 111 | } |
| 112 |
| 113 | uint8_t* NV12Buffer::MutableDataUV() { |
| 114 | return data_.get() + UVOffset(); |
| 115 | } |
| 116 |
| 117 | size_t NV12Buffer::UVOffset() const { |
| 118 | return stride_y_ * height_; |
| 119 | } |
| 120 |
| 121 | void NV12Buffer::InitializeData() { |
| 122 | memset(data_.get(), 0, NV12DataSize(height_, stride_y_, stride_uv_)); |
| 123 | } |
| 124 |
| 125 | void NV12Buffer::CropAndScaleFrom(const NV12BufferInterface& src, |
| 126 | int offset_x, |
| 127 | int offset_y, |
| 128 | int crop_width, |
| 129 | int crop_height) { |
| 130 | RTC_CHECK_LE(crop_width, src.width()); |
| 131 | RTC_CHECK_LE(crop_height, src.height()); |
| 132 | RTC_CHECK_LE(crop_width + offset_x, src.width()); |
| 133 | RTC_CHECK_LE(crop_height + offset_y, src.height()); |
| 134 | RTC_CHECK_GE(offset_x, 0); |
| 135 | RTC_CHECK_GE(offset_y, 0); |
| 136 |
| 137 | // Make sure offset is even so that u/v plane becomes aligned. |
| 138 | const int uv_offset_x = offset_x / 2; |
| 139 | const int uv_offset_y = offset_y / 2; |
| 140 | offset_x = uv_offset_x * 2; |
| 141 | offset_y = uv_offset_y * 2; |
| 142 |
| 143 | const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x; |
| 144 | const uint8_t* uv_plane = |
| 145 | src.DataUV() + src.StrideUV() * uv_offset_y + uv_offset_x * 2; |
| 146 |
| 147 | int res = libyuv::NV12Scale(y_plane, src.StrideY(), uv_plane, src.StrideUV(), |
| 148 | crop_width, crop_height, MutableDataY(), |
| 149 | StrideY(), MutableDataUV(), StrideUV(), width(), |
| 150 | height(), libyuv::kFilterBox); |
| 151 |
| 152 | RTC_DCHECK_EQ(res, 0); |
| 153 | } |
| 154 |
| 155 | } // namespace webrtc |
| 156 |