• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
10 #include "api/video/i420_buffer.h"
11 
12 #include <string.h>
13 
14 #include <algorithm>
15 #include <utility>
16 
17 #include "rtc_base/checks.h"
18 #include "rtc_base/ref_counted_object.h"
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"
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 
I420DataSize(int height,int stride_y,int stride_u,int stride_v)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 
I420Buffer(int width,int height)36 I420Buffer::I420Buffer(int width, int height)
37     : I420Buffer(width, height, width, (width + 1) / 2, (width + 1) / 2) {}
38 
I420Buffer(int width,int height,int stride_y,int stride_u,int stride_v)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),
49       data_(static_cast<uint8_t*>(
50           AlignedMalloc(I420DataSize(height, stride_y, stride_u, stride_v),
51                         kBufferAlignment))) {
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 
~I420Buffer()59 I420Buffer::~I420Buffer() {}
60 
61 // static
Create(int width,int height)62 rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width, int height) {
63   return new rtc::RefCountedObject<I420Buffer>(width, height);
64 }
65 
66 // static
Create(int width,int height,int stride_y,int stride_u,int stride_v)67 rtc::scoped_refptr<I420Buffer> I420Buffer::Create(int width,
68                                                   int height,
69                                                   int stride_y,
70                                                   int stride_u,
71                                                   int stride_v) {
72   return new rtc::RefCountedObject<I420Buffer>(width, height, stride_y,
73                                                stride_u, stride_v);
74 }
75 
76 // static
Copy(const I420BufferInterface & source)77 rtc::scoped_refptr<I420Buffer> I420Buffer::Copy(
78     const I420BufferInterface& source) {
79   return Copy(source.width(), source.height(), source.DataY(), source.StrideY(),
80               source.DataU(), source.StrideU(), source.DataV(),
81               source.StrideV());
82 }
83 
84 // static
Copy(int width,int height,const uint8_t * data_y,int stride_y,const uint8_t * data_u,int stride_u,const uint8_t * data_v,int stride_v)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) {
93   // Note: May use different strides than the input data.
94   rtc::scoped_refptr<I420Buffer> buffer = Create(width, height);
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));
100   return buffer;
101 }
102 
103 // static
Rotate(const I420BufferInterface & src,VideoRotation rotation)104 rtc::scoped_refptr<I420Buffer> I420Buffer::Rotate(
105     const I420BufferInterface& src,
106     VideoRotation rotation) {
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 
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)));
128 
129   return buffer;
130 }
131 
InitializeData()132 void I420Buffer::InitializeData() {
133   memset(data_.get(), 0,
134          I420DataSize(height_, stride_y_, stride_u_, stride_v_));
135 }
136 
width() const137 int I420Buffer::width() const {
138   return width_;
139 }
140 
height() const141 int I420Buffer::height() const {
142   return height_;
143 }
144 
DataY() const145 const uint8_t* I420Buffer::DataY() const {
146   return data_.get();
147 }
DataU() const148 const uint8_t* I420Buffer::DataU() const {
149   return data_.get() + stride_y_ * height_;
150 }
DataV() const151 const uint8_t* I420Buffer::DataV() const {
152   return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
153 }
154 
StrideY() const155 int I420Buffer::StrideY() const {
156   return stride_y_;
157 }
StrideU() const158 int I420Buffer::StrideU() const {
159   return stride_u_;
160 }
StrideV() const161 int I420Buffer::StrideV() const {
162   return stride_v_;
163 }
164 
MutableDataY()165 uint8_t* I420Buffer::MutableDataY() {
166   return const_cast<uint8_t*>(DataY());
167 }
MutableDataU()168 uint8_t* I420Buffer::MutableDataU() {
169   return const_cast<uint8_t*>(DataU());
170 }
MutableDataV()171 uint8_t* I420Buffer::MutableDataV() {
172   return const_cast<uint8_t*>(DataV());
173 }
174 
175 // static
SetBlack(I420Buffer * buffer)176 void I420Buffer::SetBlack(I420Buffer* buffer) {
177   RTC_CHECK(libyuv::I420Rect(buffer->MutableDataY(), buffer->StrideY(),
178                              buffer->MutableDataU(), buffer->StrideU(),
179                              buffer->MutableDataV(), buffer->StrideV(), 0, 0,
180                              buffer->width(), buffer->height(), 0, 128,
181                              128) == 0);
182 }
183 
CropAndScaleFrom(const I420BufferInterface & src,int offset_x,int offset_y,int crop_width,int crop_height)184 void I420Buffer::CropAndScaleFrom(const I420BufferInterface& src,
185                                   int offset_x,
186                                   int offset_y,
187                                   int crop_width,
188                                   int crop_height) {
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 
202   const uint8_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
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;
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);
212 
213   RTC_DCHECK_EQ(res, 0);
214 }
215 
CropAndScaleFrom(const I420BufferInterface & src)216 void I420Buffer::CropAndScaleFrom(const I420BufferInterface& src) {
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 
222   CropAndScaleFrom(src, (src.width() - crop_width) / 2,
223                    (src.height() - crop_height) / 2, crop_width, crop_height);
224 }
225 
ScaleFrom(const I420BufferInterface & src)226 void I420Buffer::ScaleFrom(const I420BufferInterface& src) {
227   CropAndScaleFrom(src, 0, 0, src.width(), src.height());
228 }
229 
PasteFrom(const I420BufferInterface & picture,int offset_col,int offset_row)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);
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());
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 
261 }  // namespace webrtc
262