1 /*
2 * Copyright (c) 2018 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/i010_buffer.h"
11
12 #include <utility>
13
14 #include "api/make_ref_counted.h"
15 #include "api/video/i420_buffer.h"
16 #include "rtc_base/checks.h"
17 #include "third_party/libyuv/include/libyuv/convert.h"
18 #include "third_party/libyuv/include/libyuv/scale.h"
19
20 // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
21 static const int kBufferAlignment = 64;
22 static const int kBytesPerPixel = 2;
23
24 namespace webrtc {
25
26 namespace {
27
I010DataSize(int height,int stride_y,int stride_u,int stride_v)28 int I010DataSize(int height, int stride_y, int stride_u, int stride_v) {
29 return kBytesPerPixel *
30 (stride_y * height + (stride_u + stride_v) * ((height + 1) / 2));
31 }
32
33 } // namespace
34
I010Buffer(int width,int height,int stride_y,int stride_u,int stride_v)35 I010Buffer::I010Buffer(int width,
36 int height,
37 int stride_y,
38 int stride_u,
39 int stride_v)
40 : width_(width),
41 height_(height),
42 stride_y_(stride_y),
43 stride_u_(stride_u),
44 stride_v_(stride_v),
45 data_(static_cast<uint16_t*>(
46 AlignedMalloc(I010DataSize(height, stride_y, stride_u, stride_v),
47 kBufferAlignment))) {
48 RTC_DCHECK_GT(width, 0);
49 RTC_DCHECK_GT(height, 0);
50 RTC_DCHECK_GE(stride_y, width);
51 RTC_DCHECK_GE(stride_u, (width + 1) / 2);
52 RTC_DCHECK_GE(stride_v, (width + 1) / 2);
53 }
54
~I010Buffer()55 I010Buffer::~I010Buffer() {}
56
57 // static
Create(int width,int height)58 rtc::scoped_refptr<I010Buffer> I010Buffer::Create(int width, int height) {
59 return rtc::make_ref_counted<I010Buffer>(width, height, width,
60 (width + 1) / 2, (width + 1) / 2);
61 }
62
63 // static
Copy(const I010BufferInterface & source)64 rtc::scoped_refptr<I010Buffer> I010Buffer::Copy(
65 const I010BufferInterface& source) {
66 const int width = source.width();
67 const int height = source.height();
68 rtc::scoped_refptr<I010Buffer> buffer = Create(width, height);
69 RTC_CHECK_EQ(
70 0, libyuv::I010Copy(
71 source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
72 source.DataV(), source.StrideV(), buffer->MutableDataY(),
73 buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
74 buffer->MutableDataV(), buffer->StrideV(), width, height));
75 return buffer;
76 }
77
78 // static
Copy(const I420BufferInterface & source)79 rtc::scoped_refptr<I010Buffer> I010Buffer::Copy(
80 const I420BufferInterface& source) {
81 const int width = source.width();
82 const int height = source.height();
83 rtc::scoped_refptr<I010Buffer> buffer = Create(width, height);
84 RTC_CHECK_EQ(
85 0, libyuv::I420ToI010(
86 source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
87 source.DataV(), source.StrideV(), buffer->MutableDataY(),
88 buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
89 buffer->MutableDataV(), buffer->StrideV(), width, height));
90 return buffer;
91 }
92
93 // static
Rotate(const I010BufferInterface & src,VideoRotation rotation)94 rtc::scoped_refptr<I010Buffer> I010Buffer::Rotate(
95 const I010BufferInterface& src,
96 VideoRotation rotation) {
97 if (rotation == webrtc::kVideoRotation_0)
98 return Copy(src);
99
100 RTC_CHECK(src.DataY());
101 RTC_CHECK(src.DataU());
102 RTC_CHECK(src.DataV());
103 int rotated_width = src.width();
104 int rotated_height = src.height();
105 if (rotation == webrtc::kVideoRotation_90 ||
106 rotation == webrtc::kVideoRotation_270) {
107 std::swap(rotated_width, rotated_height);
108 }
109
110 rtc::scoped_refptr<webrtc::I010Buffer> buffer =
111 Create(rotated_width, rotated_height);
112 // TODO(emircan): Remove this when there is libyuv::I010Rotate().
113 for (int x = 0; x < src.width(); x++) {
114 for (int y = 0; y < src.height(); y++) {
115 int dest_x = x;
116 int dest_y = y;
117 switch (rotation) {
118 // This case is covered by the early return.
119 case webrtc::kVideoRotation_0:
120 RTC_DCHECK_NOTREACHED();
121 break;
122 case webrtc::kVideoRotation_90:
123 dest_x = src.height() - y - 1;
124 dest_y = x;
125 break;
126 case webrtc::kVideoRotation_180:
127 dest_x = src.width() - x - 1;
128 dest_y = src.height() - y - 1;
129 break;
130 case webrtc::kVideoRotation_270:
131 dest_x = y;
132 dest_y = src.width() - x - 1;
133 break;
134 }
135 buffer->MutableDataY()[dest_x + buffer->StrideY() * dest_y] =
136 src.DataY()[x + src.StrideY() * y];
137 dest_x /= 2;
138 dest_y /= 2;
139 int src_x = x / 2;
140 int src_y = y / 2;
141 buffer->MutableDataU()[dest_x + buffer->StrideU() * dest_y] =
142 src.DataU()[src_x + src.StrideU() * src_y];
143 buffer->MutableDataV()[dest_x + buffer->StrideV() * dest_y] =
144 src.DataV()[src_x + src.StrideV() * src_y];
145 }
146 }
147 return buffer;
148 }
149
ToI420()150 rtc::scoped_refptr<I420BufferInterface> I010Buffer::ToI420() {
151 rtc::scoped_refptr<I420Buffer> i420_buffer =
152 I420Buffer::Create(width(), height());
153 libyuv::I010ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
154 i420_buffer->MutableDataY(), i420_buffer->StrideY(),
155 i420_buffer->MutableDataU(), i420_buffer->StrideU(),
156 i420_buffer->MutableDataV(), i420_buffer->StrideV(),
157 width(), height());
158 return i420_buffer;
159 }
160
width() const161 int I010Buffer::width() const {
162 return width_;
163 }
164
height() const165 int I010Buffer::height() const {
166 return height_;
167 }
168
DataY() const169 const uint16_t* I010Buffer::DataY() const {
170 return data_.get();
171 }
DataU() const172 const uint16_t* I010Buffer::DataU() const {
173 return data_.get() + stride_y_ * height_;
174 }
DataV() const175 const uint16_t* I010Buffer::DataV() const {
176 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
177 }
178
StrideY() const179 int I010Buffer::StrideY() const {
180 return stride_y_;
181 }
StrideU() const182 int I010Buffer::StrideU() const {
183 return stride_u_;
184 }
StrideV() const185 int I010Buffer::StrideV() const {
186 return stride_v_;
187 }
188
MutableDataY()189 uint16_t* I010Buffer::MutableDataY() {
190 return const_cast<uint16_t*>(DataY());
191 }
MutableDataU()192 uint16_t* I010Buffer::MutableDataU() {
193 return const_cast<uint16_t*>(DataU());
194 }
MutableDataV()195 uint16_t* I010Buffer::MutableDataV() {
196 return const_cast<uint16_t*>(DataV());
197 }
198
CropAndScaleFrom(const I010BufferInterface & src,int offset_x,int offset_y,int crop_width,int crop_height)199 void I010Buffer::CropAndScaleFrom(const I010BufferInterface& src,
200 int offset_x,
201 int offset_y,
202 int crop_width,
203 int crop_height) {
204 RTC_CHECK_LE(crop_width, src.width());
205 RTC_CHECK_LE(crop_height, src.height());
206 RTC_CHECK_LE(crop_width + offset_x, src.width());
207 RTC_CHECK_LE(crop_height + offset_y, src.height());
208 RTC_CHECK_GE(offset_x, 0);
209 RTC_CHECK_GE(offset_y, 0);
210
211 // Make sure offset is even so that u/v plane becomes aligned.
212 const int uv_offset_x = offset_x / 2;
213 const int uv_offset_y = offset_y / 2;
214 offset_x = uv_offset_x * 2;
215 offset_y = uv_offset_y * 2;
216
217 const uint16_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
218 const uint16_t* u_plane =
219 src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
220 const uint16_t* v_plane =
221 src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
222 int res = libyuv::I420Scale_16(
223 y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane, src.StrideV(),
224 crop_width, crop_height, MutableDataY(), StrideY(), MutableDataU(),
225 StrideU(), MutableDataV(), StrideV(), width(), height(),
226 libyuv::kFilterBox);
227
228 RTC_DCHECK_EQ(res, 0);
229 }
230
ScaleFrom(const I010BufferInterface & src)231 void I010Buffer::ScaleFrom(const I010BufferInterface& src) {
232 CropAndScaleFrom(src, 0, 0, src.width(), src.height());
233 }
234
235 } // namespace webrtc
236