1 /*
2 * Copyright (c) 2020 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
11 #include "api/video/nv12_buffer.h"
12
13 #include "api/video/i420_buffer.h"
14 #include "rtc_base/checks.h"
15 #include "rtc_base/ref_counted_object.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
NV12DataSize(int height,int stride_y,int stride_uv)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
NV12Buffer(int width,int height)31 NV12Buffer::NV12Buffer(int width, int height)
32 : NV12Buffer(width, height, width, width + width % 2) {}
33
NV12Buffer(int width,int height,int stride_y,int stride_uv)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
Create(int width,int height)51 rtc::scoped_refptr<NV12Buffer> NV12Buffer::Create(int width, int height) {
52 return new rtc::RefCountedObject<NV12Buffer>(width, height);
53 }
54
55 // static
Create(int width,int height,int stride_y,int stride_uv)56 rtc::scoped_refptr<NV12Buffer> NV12Buffer::Create(int width,
57 int height,
58 int stride_y,
59 int stride_uv) {
60 return new rtc::RefCountedObject<NV12Buffer>(width, height, stride_y,
61 stride_uv);
62 }
63
64 // static
Copy(const I420BufferInterface & i420_buffer)65 rtc::scoped_refptr<NV12Buffer> NV12Buffer::Copy(
66 const I420BufferInterface& i420_buffer) {
67 rtc::scoped_refptr<NV12Buffer> buffer =
68 NV12Buffer::Create(i420_buffer.width(), i420_buffer.height());
69 libyuv::I420ToNV12(
70 i420_buffer.DataY(), i420_buffer.StrideY(), i420_buffer.DataU(),
71 i420_buffer.StrideU(), i420_buffer.DataV(), i420_buffer.StrideV(),
72 buffer->MutableDataY(), buffer->StrideY(), buffer->MutableDataUV(),
73 buffer->StrideUV(), buffer->width(), buffer->height());
74 return buffer;
75 }
76
ToI420()77 rtc::scoped_refptr<I420BufferInterface> NV12Buffer::ToI420() {
78 rtc::scoped_refptr<I420Buffer> i420_buffer =
79 I420Buffer::Create(width(), height());
80 libyuv::NV12ToI420(DataY(), StrideY(), DataUV(), StrideUV(),
81 i420_buffer->MutableDataY(), i420_buffer->StrideY(),
82 i420_buffer->MutableDataU(), i420_buffer->StrideU(),
83 i420_buffer->MutableDataV(), i420_buffer->StrideV(),
84 width(), height());
85 return i420_buffer;
86 }
87
width() const88 int NV12Buffer::width() const {
89 return width_;
90 }
height() const91 int NV12Buffer::height() const {
92 return height_;
93 }
94
StrideY() const95 int NV12Buffer::StrideY() const {
96 return stride_y_;
97 }
StrideUV() const98 int NV12Buffer::StrideUV() const {
99 return stride_uv_;
100 }
101
DataY() const102 const uint8_t* NV12Buffer::DataY() const {
103 return data_.get();
104 }
105
DataUV() const106 const uint8_t* NV12Buffer::DataUV() const {
107 return data_.get() + UVOffset();
108 }
109
MutableDataY()110 uint8_t* NV12Buffer::MutableDataY() {
111 return data_.get();
112 }
113
MutableDataUV()114 uint8_t* NV12Buffer::MutableDataUV() {
115 return data_.get() + UVOffset();
116 }
117
UVOffset() const118 size_t NV12Buffer::UVOffset() const {
119 return stride_y_ * height_;
120 }
121
InitializeData()122 void NV12Buffer::InitializeData() {
123 memset(data_.get(), 0, NV12DataSize(height_, stride_y_, stride_uv_));
124 }
125
CropAndScaleFrom(const NV12BufferInterface & src,int offset_x,int offset_y,int crop_width,int crop_height)126 void NV12Buffer::CropAndScaleFrom(const NV12BufferInterface& src,
127 int offset_x,
128 int offset_y,
129 int crop_width,
130 int crop_height) {
131 RTC_CHECK_LE(crop_width, src.width());
132 RTC_CHECK_LE(crop_height, src.height());
133 RTC_CHECK_LE(crop_width + offset_x, src.width());
134 RTC_CHECK_LE(crop_height + offset_y, src.height());
135 RTC_CHECK_GE(offset_x, 0);
136 RTC_CHECK_GE(offset_y, 0);
137
138 // Make sure offset is even so that u/v plane becomes aligned.
139 const int uv_offset_x = offset_x / 2;
140 const int uv_offset_y = offset_y / 2;
141 offset_x = uv_offset_x * 2;
142 offset_y = uv_offset_y * 2;
143
144 const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
145 const uint8_t* uv_plane =
146 src.DataUV() + src.StrideUV() * uv_offset_y + uv_offset_x * 2;
147
148 // kFilterBox is unsupported in libyuv, so using kFilterBilinear instead.
149 int res = libyuv::NV12Scale(y_plane, src.StrideY(), uv_plane, src.StrideUV(),
150 crop_width, crop_height, MutableDataY(),
151 StrideY(), MutableDataUV(), StrideUV(), width(),
152 height(), libyuv::kFilterBilinear);
153
154 RTC_DCHECK_EQ(res, 0);
155 }
156
157 } // namespace webrtc
158