blob: 398e30b606fe8bc24fe6d89bbdaf4176507ff2d6 [file] [log] [blame]
magjed712338e2017-05-11 05:11:57 -07001/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "api/video/video_frame_buffer.h"
magjed712338e2017-05-11 05:11:57 -070012
Ilya Nikolaevskiy38e9b062020-10-08 14:36:33 +000013#include "api/video/i420_buffer.h"
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010014#include "api/video/i422_buffer.h"
Stefan Miticffdc6802022-02-08 07:00:16 -080015#include "api/video/i444_buffer.h"
Henrik Boströmf4129762021-03-22 10:22:54 +010016#include "api/video/nv12_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
magjed712338e2017-05-11 05:11:57 -070018
19namespace webrtc {
20
Ilya Nikolaevskiy38e9b062020-10-08 14:36:33 +000021rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::CropAndScale(
22 int offset_x,
23 int offset_y,
24 int crop_width,
25 int crop_height,
26 int scaled_width,
27 int scaled_height) {
28 rtc::scoped_refptr<I420Buffer> result =
29 I420Buffer::Create(scaled_width, scaled_height);
30 result->CropAndScaleFrom(*this->ToI420(), offset_x, offset_y, crop_width,
31 crop_height);
32 return result;
33}
34
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +020035const I420BufferInterface* VideoFrameBuffer::GetI420() const {
36 // Overridden by subclasses that can return an I420 buffer without any
37 // conversion, in particular, I420BufferInterface.
38 return nullptr;
Emircan Uysaler574eaa42017-11-09 12:33:24 -080039}
40
41const I420ABufferInterface* VideoFrameBuffer::GetI420A() const {
42 RTC_CHECK(type() == Type::kI420A);
43 return static_cast<const I420ABufferInterface*>(this);
44}
45
magjed3f075492017-06-01 10:02:26 -070046const I444BufferInterface* VideoFrameBuffer::GetI444() const {
47 RTC_CHECK(type() == Type::kI444);
48 return static_cast<const I444BufferInterface*>(this);
49}
50
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010051const I422BufferInterface* VideoFrameBuffer::GetI422() const {
52 RTC_CHECK(type() == Type::kI422);
53 return static_cast<const I422BufferInterface*>(this);
54}
55
Emircan Uysaler901e0ff2018-06-26 12:22:38 -070056const I010BufferInterface* VideoFrameBuffer::GetI010() const {
57 RTC_CHECK(type() == Type::kI010);
58 return static_cast<const I010BufferInterface*>(this);
59}
60
Sergio Garcia Murillo8545eba2022-06-17 11:48:14 +020061const I210BufferInterface* VideoFrameBuffer::GetI210() const {
62 RTC_CHECK(type() == Type::kI210);
63 return static_cast<const I210BufferInterface*>(this);
64}
65
Evan Shrubsole84995432020-09-09 16:14:19 +020066const NV12BufferInterface* VideoFrameBuffer::GetNV12() const {
67 RTC_CHECK(type() == Type::kNV12);
68 return static_cast<const NV12BufferInterface*>(this);
69}
70
Evan Shrubsoleb556b082020-10-08 14:56:45 +020071rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::GetMappedFrameBuffer(
72 rtc::ArrayView<Type> types) {
73 RTC_CHECK(type() == Type::kNative);
74 return nullptr;
75}
76
magjedeaf4a1e2017-05-30 01:21:59 -070077VideoFrameBuffer::Type I420BufferInterface::type() const {
78 return Type::kI420;
magjed712338e2017-05-11 05:11:57 -070079}
80
Evan Shrubsoleb556b082020-10-08 14:56:45 +020081const char* VideoFrameBufferTypeToString(VideoFrameBuffer::Type type) {
82 switch (type) {
83 case VideoFrameBuffer::Type::kNative:
84 return "kNative";
85 case VideoFrameBuffer::Type::kI420:
86 return "kI420";
87 case VideoFrameBuffer::Type::kI420A:
88 return "kI420A";
89 case VideoFrameBuffer::Type::kI444:
90 return "kI444";
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +010091 case VideoFrameBuffer::Type::kI422:
92 return "kI422";
Evan Shrubsoleb556b082020-10-08 14:56:45 +020093 case VideoFrameBuffer::Type::kI010:
94 return "kI010";
Sergio Garcia Murillo8545eba2022-06-17 11:48:14 +020095 case VideoFrameBuffer::Type::kI210:
96 return "kI210";
Evan Shrubsoleb556b082020-10-08 14:56:45 +020097 case VideoFrameBuffer::Type::kNV12:
98 return "kNV12";
99 default:
Artem Titovd3251962021-11-15 16:57:07 +0100100 RTC_DCHECK_NOTREACHED();
Evan Shrubsoleb556b082020-10-08 14:56:45 +0200101 }
102}
103
magjedeaf4a1e2017-05-30 01:21:59 -0700104int I420BufferInterface::ChromaWidth() const {
105 return (width() + 1) / 2;
magjed712338e2017-05-11 05:11:57 -0700106}
107
magjedeaf4a1e2017-05-30 01:21:59 -0700108int I420BufferInterface::ChromaHeight() const {
109 return (height() + 1) / 2;
110}
111
112rtc::scoped_refptr<I420BufferInterface> I420BufferInterface::ToI420() {
Niels Möller961f3822022-01-12 10:06:55 +0100113 return rtc::scoped_refptr<I420BufferInterface>(this);
magjedeaf4a1e2017-05-30 01:21:59 -0700114}
115
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +0200116const I420BufferInterface* I420BufferInterface::GetI420() const {
117 return this;
118}
119
Emircan Uysaler574eaa42017-11-09 12:33:24 -0800120VideoFrameBuffer::Type I420ABufferInterface::type() const {
121 return Type::kI420A;
122}
123
magjedeaf4a1e2017-05-30 01:21:59 -0700124VideoFrameBuffer::Type I444BufferInterface::type() const {
125 return Type::kI444;
126}
127
128int I444BufferInterface::ChromaWidth() const {
129 return width();
130}
131
132int I444BufferInterface::ChromaHeight() const {
133 return height();
134}
135
Stefan Miticffdc6802022-02-08 07:00:16 -0800136rtc::scoped_refptr<VideoFrameBuffer> I444BufferInterface::CropAndScale(
137 int offset_x,
138 int offset_y,
139 int crop_width,
140 int crop_height,
141 int scaled_width,
142 int scaled_height) {
143 rtc::scoped_refptr<I444Buffer> result =
144 I444Buffer::Create(scaled_width, scaled_height);
145 result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
146 return result;
147}
148
Sergio Garcia Murillob63536f2022-03-25 09:04:09 +0100149VideoFrameBuffer::Type I422BufferInterface::type() const {
150 return Type::kI422;
151}
152
153int I422BufferInterface::ChromaWidth() const {
154 return (width() + 1) / 2;
155}
156
157int I422BufferInterface::ChromaHeight() const {
158 return height();
159}
160
161rtc::scoped_refptr<VideoFrameBuffer> I422BufferInterface::CropAndScale(
162 int offset_x,
163 int offset_y,
164 int crop_width,
165 int crop_height,
166 int scaled_width,
167 int scaled_height) {
168 rtc::scoped_refptr<I422Buffer> result =
169 I422Buffer::Create(scaled_width, scaled_height);
170 result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
171 return result;
172}
173
Emircan Uysaler901e0ff2018-06-26 12:22:38 -0700174VideoFrameBuffer::Type I010BufferInterface::type() const {
175 return Type::kI010;
176}
177
178int I010BufferInterface::ChromaWidth() const {
179 return (width() + 1) / 2;
180}
181
182int I010BufferInterface::ChromaHeight() const {
183 return (height() + 1) / 2;
184}
185
Sergio Garcia Murillo8545eba2022-06-17 11:48:14 +0200186VideoFrameBuffer::Type I210BufferInterface::type() const {
187 return Type::kI210;
188}
189
190int I210BufferInterface::ChromaWidth() const {
191 return (width() + 1) / 2;
192}
193
194int I210BufferInterface::ChromaHeight() const {
195 return height();
196}
197
Evan Shrubsole84995432020-09-09 16:14:19 +0200198VideoFrameBuffer::Type NV12BufferInterface::type() const {
199 return Type::kNV12;
200}
201
202int NV12BufferInterface::ChromaWidth() const {
203 return (width() + 1) / 2;
204}
205
206int NV12BufferInterface::ChromaHeight() const {
207 return (height() + 1) / 2;
208}
Henrik Boströmf4129762021-03-22 10:22:54 +0100209
210rtc::scoped_refptr<VideoFrameBuffer> NV12BufferInterface::CropAndScale(
211 int offset_x,
212 int offset_y,
213 int crop_width,
214 int crop_height,
215 int scaled_width,
216 int scaled_height) {
217 rtc::scoped_refptr<NV12Buffer> result =
218 NV12Buffer::Create(scaled_width, scaled_height);
219 result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
220 return result;
221}
222
magjed712338e2017-05-11 05:11:57 -0700223} // namespace webrtc