blob: d78e854eb90009035110bcb3034f4bc60fdf05e9 [file] [log] [blame]
Emircan Uysaler901e0ff2018-06-26 12:22:38 -07001/*
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
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070012#include <utility>
13
Niels Möller7c8c4db2022-06-13 10:36:38 +020014#include "api/make_ref_counted.h"
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070015#include "api/video/i420_buffer.h"
16#include "rtc_base/checks.h"
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070017#include "third_party/libyuv/include/libyuv/convert.h"
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070018#include "third_party/libyuv/include/libyuv/scale.h"
19
20// Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
21static const int kBufferAlignment = 64;
22static const int kBytesPerPixel = 2;
23
24namespace webrtc {
25
26namespace {
27
28int 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
35I010Buffer::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
55I010Buffer::~I010Buffer() {}
56
57// static
58rtc::scoped_refptr<I010Buffer> I010Buffer::Create(int width, int height) {
Tomas Gunnarssonc1d58912021-04-22 19:21:43 +020059 return rtc::make_ref_counted<I010Buffer>(width, height, width,
60 (width + 1) / 2, (width + 1) / 2);
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070061}
62
63// static
64rtc::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);
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +010069 int res = libyuv::I010Copy(
70 source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
71 source.DataV(), source.StrideV(), buffer->MutableDataY(),
72 buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
73 buffer->MutableDataV(), buffer->StrideV(), width, height);
74 RTC_DCHECK_EQ(res, 0);
75
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070076 return buffer;
77}
78
79// static
80rtc::scoped_refptr<I010Buffer> I010Buffer::Copy(
81 const I420BufferInterface& source) {
82 const int width = source.width();
83 const int height = source.height();
84 rtc::scoped_refptr<I010Buffer> buffer = Create(width, height);
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +010085 int res = 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 RTC_DCHECK_EQ(res, 0);
91
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070092 return buffer;
93}
94
95// static
96rtc::scoped_refptr<I010Buffer> I010Buffer::Rotate(
97 const I010BufferInterface& src,
98 VideoRotation rotation) {
99 if (rotation == webrtc::kVideoRotation_0)
100 return Copy(src);
101
102 RTC_CHECK(src.DataY());
103 RTC_CHECK(src.DataU());
104 RTC_CHECK(src.DataV());
105 int rotated_width = src.width();
106 int rotated_height = src.height();
107 if (rotation == webrtc::kVideoRotation_90 ||
108 rotation == webrtc::kVideoRotation_270) {
109 std::swap(rotated_width, rotated_height);
110 }
111
112 rtc::scoped_refptr<webrtc::I010Buffer> buffer =
113 Create(rotated_width, rotated_height);
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +0100114
115 int res = libyuv::I010Rotate(
116 src.DataY(), src.StrideY(), src.DataU(), src.StrideU(), src.DataV(),
117 src.StrideV(), buffer->MutableDataY(), buffer->StrideY(),
118 buffer->MutableDataU(), buffer->StrideU(), buffer->MutableDataV(),
119 buffer->StrideV(), src.width(), src.height(),
120 static_cast<libyuv::RotationMode>(rotation));
121 RTC_DCHECK_EQ(res, 0);
122
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700123 return buffer;
124}
125
126rtc::scoped_refptr<I420BufferInterface> I010Buffer::ToI420() {
127 rtc::scoped_refptr<I420Buffer> i420_buffer =
128 I420Buffer::Create(width(), height());
Sergio Garcia Murillobfc26c62023-01-09 16:08:20 +0100129 int res = libyuv::I010ToI420(
130 DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
131 i420_buffer->MutableDataY(), i420_buffer->StrideY(),
132 i420_buffer->MutableDataU(), i420_buffer->StrideU(),
133 i420_buffer->MutableDataV(), i420_buffer->StrideV(), width(), height());
134 RTC_DCHECK_EQ(res, 0);
135
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700136 return i420_buffer;
137}
138
139int I010Buffer::width() const {
140 return width_;
141}
142
143int I010Buffer::height() const {
144 return height_;
145}
146
147const uint16_t* I010Buffer::DataY() const {
148 return data_.get();
149}
150const uint16_t* I010Buffer::DataU() const {
151 return data_.get() + stride_y_ * height_;
152}
153const uint16_t* I010Buffer::DataV() const {
154 return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
155}
156
157int I010Buffer::StrideY() const {
158 return stride_y_;
159}
160int I010Buffer::StrideU() const {
161 return stride_u_;
162}
163int I010Buffer::StrideV() const {
164 return stride_v_;
165}
166
167uint16_t* I010Buffer::MutableDataY() {
168 return const_cast<uint16_t*>(DataY());
169}
170uint16_t* I010Buffer::MutableDataU() {
171 return const_cast<uint16_t*>(DataU());
172}
173uint16_t* I010Buffer::MutableDataV() {
174 return const_cast<uint16_t*>(DataV());
175}
176
177void I010Buffer::CropAndScaleFrom(const I010BufferInterface& src,
178 int offset_x,
179 int offset_y,
180 int crop_width,
181 int crop_height) {
182 RTC_CHECK_LE(crop_width, src.width());
183 RTC_CHECK_LE(crop_height, src.height());
184 RTC_CHECK_LE(crop_width + offset_x, src.width());
185 RTC_CHECK_LE(crop_height + offset_y, src.height());
186 RTC_CHECK_GE(offset_x, 0);
187 RTC_CHECK_GE(offset_y, 0);
188
189 // Make sure offset is even so that u/v plane becomes aligned.
190 const int uv_offset_x = offset_x / 2;
191 const int uv_offset_y = offset_y / 2;
192 offset_x = uv_offset_x * 2;
193 offset_y = uv_offset_y * 2;
194
195 const uint16_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
196 const uint16_t* u_plane =
197 src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
198 const uint16_t* v_plane =
199 src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
200 int res = libyuv::I420Scale_16(
201 y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane, src.StrideV(),
202 crop_width, crop_height, MutableDataY(), StrideY(), MutableDataU(),
203 StrideU(), MutableDataV(), StrideV(), width(), height(),
204 libyuv::kFilterBox);
205
206 RTC_DCHECK_EQ(res, 0);
207}
208
209void I010Buffer::ScaleFrom(const I010BufferInterface& src) {
210 CropAndScaleFrom(src, 0, 0, src.width(), src.height());
211}
212
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700213} // namespace webrtc