xref: /aosp_15_r20/external/webrtc/api/video/video_frame_buffer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_frame_buffer.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include "api/video/i420_buffer.h"
14*d9f75844SAndroid Build Coastguard Worker #include "api/video/i422_buffer.h"
15*d9f75844SAndroid Build Coastguard Worker #include "api/video/i444_buffer.h"
16*d9f75844SAndroid Build Coastguard Worker #include "api/video/nv12_buffer.h"
17*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
20*d9f75844SAndroid Build Coastguard Worker 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)21*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::CropAndScale(
22*d9f75844SAndroid Build Coastguard Worker     int offset_x,
23*d9f75844SAndroid Build Coastguard Worker     int offset_y,
24*d9f75844SAndroid Build Coastguard Worker     int crop_width,
25*d9f75844SAndroid Build Coastguard Worker     int crop_height,
26*d9f75844SAndroid Build Coastguard Worker     int scaled_width,
27*d9f75844SAndroid Build Coastguard Worker     int scaled_height) {
28*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<I420Buffer> result =
29*d9f75844SAndroid Build Coastguard Worker       I420Buffer::Create(scaled_width, scaled_height);
30*d9f75844SAndroid Build Coastguard Worker   result->CropAndScaleFrom(*this->ToI420(), offset_x, offset_y, crop_width,
31*d9f75844SAndroid Build Coastguard Worker                            crop_height);
32*d9f75844SAndroid Build Coastguard Worker   return result;
33*d9f75844SAndroid Build Coastguard Worker }
34*d9f75844SAndroid Build Coastguard Worker 
GetI420() const35*d9f75844SAndroid Build Coastguard Worker const I420BufferInterface* VideoFrameBuffer::GetI420() const {
36*d9f75844SAndroid Build Coastguard Worker   // Overridden by subclasses that can return an I420 buffer without any
37*d9f75844SAndroid Build Coastguard Worker   // conversion, in particular, I420BufferInterface.
38*d9f75844SAndroid Build Coastguard Worker   return nullptr;
39*d9f75844SAndroid Build Coastguard Worker }
40*d9f75844SAndroid Build Coastguard Worker 
GetI420A() const41*d9f75844SAndroid Build Coastguard Worker const I420ABufferInterface* VideoFrameBuffer::GetI420A() const {
42*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kI420A);
43*d9f75844SAndroid Build Coastguard Worker   return static_cast<const I420ABufferInterface*>(this);
44*d9f75844SAndroid Build Coastguard Worker }
45*d9f75844SAndroid Build Coastguard Worker 
GetI444() const46*d9f75844SAndroid Build Coastguard Worker const I444BufferInterface* VideoFrameBuffer::GetI444() const {
47*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kI444);
48*d9f75844SAndroid Build Coastguard Worker   return static_cast<const I444BufferInterface*>(this);
49*d9f75844SAndroid Build Coastguard Worker }
50*d9f75844SAndroid Build Coastguard Worker 
GetI422() const51*d9f75844SAndroid Build Coastguard Worker const I422BufferInterface* VideoFrameBuffer::GetI422() const {
52*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kI422);
53*d9f75844SAndroid Build Coastguard Worker   return static_cast<const I422BufferInterface*>(this);
54*d9f75844SAndroid Build Coastguard Worker }
55*d9f75844SAndroid Build Coastguard Worker 
GetI010() const56*d9f75844SAndroid Build Coastguard Worker const I010BufferInterface* VideoFrameBuffer::GetI010() const {
57*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kI010);
58*d9f75844SAndroid Build Coastguard Worker   return static_cast<const I010BufferInterface*>(this);
59*d9f75844SAndroid Build Coastguard Worker }
60*d9f75844SAndroid Build Coastguard Worker 
GetI210() const61*d9f75844SAndroid Build Coastguard Worker const I210BufferInterface* VideoFrameBuffer::GetI210() const {
62*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kI210);
63*d9f75844SAndroid Build Coastguard Worker   return static_cast<const I210BufferInterface*>(this);
64*d9f75844SAndroid Build Coastguard Worker }
65*d9f75844SAndroid Build Coastguard Worker 
GetNV12() const66*d9f75844SAndroid Build Coastguard Worker const NV12BufferInterface* VideoFrameBuffer::GetNV12() const {
67*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kNV12);
68*d9f75844SAndroid Build Coastguard Worker   return static_cast<const NV12BufferInterface*>(this);
69*d9f75844SAndroid Build Coastguard Worker }
70*d9f75844SAndroid Build Coastguard Worker 
GetMappedFrameBuffer(rtc::ArrayView<Type> types)71*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::GetMappedFrameBuffer(
72*d9f75844SAndroid Build Coastguard Worker     rtc::ArrayView<Type> types) {
73*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(type() == Type::kNative);
74*d9f75844SAndroid Build Coastguard Worker   return nullptr;
75*d9f75844SAndroid Build Coastguard Worker }
76*d9f75844SAndroid Build Coastguard Worker 
type() const77*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type I420BufferInterface::type() const {
78*d9f75844SAndroid Build Coastguard Worker   return Type::kI420;
79*d9f75844SAndroid Build Coastguard Worker }
80*d9f75844SAndroid Build Coastguard Worker 
VideoFrameBufferTypeToString(VideoFrameBuffer::Type type)81*d9f75844SAndroid Build Coastguard Worker const char* VideoFrameBufferTypeToString(VideoFrameBuffer::Type type) {
82*d9f75844SAndroid Build Coastguard Worker   switch (type) {
83*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kNative:
84*d9f75844SAndroid Build Coastguard Worker       return "kNative";
85*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kI420:
86*d9f75844SAndroid Build Coastguard Worker       return "kI420";
87*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kI420A:
88*d9f75844SAndroid Build Coastguard Worker       return "kI420A";
89*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kI444:
90*d9f75844SAndroid Build Coastguard Worker       return "kI444";
91*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kI422:
92*d9f75844SAndroid Build Coastguard Worker       return "kI422";
93*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kI010:
94*d9f75844SAndroid Build Coastguard Worker       return "kI010";
95*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kI210:
96*d9f75844SAndroid Build Coastguard Worker       return "kI210";
97*d9f75844SAndroid Build Coastguard Worker     case VideoFrameBuffer::Type::kNV12:
98*d9f75844SAndroid Build Coastguard Worker       return "kNV12";
99*d9f75844SAndroid Build Coastguard Worker     default:
100*d9f75844SAndroid Build Coastguard Worker       RTC_DCHECK_NOTREACHED();
101*d9f75844SAndroid Build Coastguard Worker   }
102*d9f75844SAndroid Build Coastguard Worker }
103*d9f75844SAndroid Build Coastguard Worker 
ChromaWidth() const104*d9f75844SAndroid Build Coastguard Worker int I420BufferInterface::ChromaWidth() const {
105*d9f75844SAndroid Build Coastguard Worker   return (width() + 1) / 2;
106*d9f75844SAndroid Build Coastguard Worker }
107*d9f75844SAndroid Build Coastguard Worker 
ChromaHeight() const108*d9f75844SAndroid Build Coastguard Worker int I420BufferInterface::ChromaHeight() const {
109*d9f75844SAndroid Build Coastguard Worker   return (height() + 1) / 2;
110*d9f75844SAndroid Build Coastguard Worker }
111*d9f75844SAndroid Build Coastguard Worker 
ToI420()112*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<I420BufferInterface> I420BufferInterface::ToI420() {
113*d9f75844SAndroid Build Coastguard Worker   return rtc::scoped_refptr<I420BufferInterface>(this);
114*d9f75844SAndroid Build Coastguard Worker }
115*d9f75844SAndroid Build Coastguard Worker 
GetI420() const116*d9f75844SAndroid Build Coastguard Worker const I420BufferInterface* I420BufferInterface::GetI420() const {
117*d9f75844SAndroid Build Coastguard Worker   return this;
118*d9f75844SAndroid Build Coastguard Worker }
119*d9f75844SAndroid Build Coastguard Worker 
type() const120*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type I420ABufferInterface::type() const {
121*d9f75844SAndroid Build Coastguard Worker   return Type::kI420A;
122*d9f75844SAndroid Build Coastguard Worker }
123*d9f75844SAndroid Build Coastguard Worker 
type() const124*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type I444BufferInterface::type() const {
125*d9f75844SAndroid Build Coastguard Worker   return Type::kI444;
126*d9f75844SAndroid Build Coastguard Worker }
127*d9f75844SAndroid Build Coastguard Worker 
ChromaWidth() const128*d9f75844SAndroid Build Coastguard Worker int I444BufferInterface::ChromaWidth() const {
129*d9f75844SAndroid Build Coastguard Worker   return width();
130*d9f75844SAndroid Build Coastguard Worker }
131*d9f75844SAndroid Build Coastguard Worker 
ChromaHeight() const132*d9f75844SAndroid Build Coastguard Worker int I444BufferInterface::ChromaHeight() const {
133*d9f75844SAndroid Build Coastguard Worker   return height();
134*d9f75844SAndroid Build Coastguard Worker }
135*d9f75844SAndroid Build Coastguard Worker 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)136*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoFrameBuffer> I444BufferInterface::CropAndScale(
137*d9f75844SAndroid Build Coastguard Worker     int offset_x,
138*d9f75844SAndroid Build Coastguard Worker     int offset_y,
139*d9f75844SAndroid Build Coastguard Worker     int crop_width,
140*d9f75844SAndroid Build Coastguard Worker     int crop_height,
141*d9f75844SAndroid Build Coastguard Worker     int scaled_width,
142*d9f75844SAndroid Build Coastguard Worker     int scaled_height) {
143*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<I444Buffer> result =
144*d9f75844SAndroid Build Coastguard Worker       I444Buffer::Create(scaled_width, scaled_height);
145*d9f75844SAndroid Build Coastguard Worker   result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
146*d9f75844SAndroid Build Coastguard Worker   return result;
147*d9f75844SAndroid Build Coastguard Worker }
148*d9f75844SAndroid Build Coastguard Worker 
type() const149*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type I422BufferInterface::type() const {
150*d9f75844SAndroid Build Coastguard Worker   return Type::kI422;
151*d9f75844SAndroid Build Coastguard Worker }
152*d9f75844SAndroid Build Coastguard Worker 
ChromaWidth() const153*d9f75844SAndroid Build Coastguard Worker int I422BufferInterface::ChromaWidth() const {
154*d9f75844SAndroid Build Coastguard Worker   return (width() + 1) / 2;
155*d9f75844SAndroid Build Coastguard Worker }
156*d9f75844SAndroid Build Coastguard Worker 
ChromaHeight() const157*d9f75844SAndroid Build Coastguard Worker int I422BufferInterface::ChromaHeight() const {
158*d9f75844SAndroid Build Coastguard Worker   return height();
159*d9f75844SAndroid Build Coastguard Worker }
160*d9f75844SAndroid Build Coastguard Worker 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)161*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoFrameBuffer> I422BufferInterface::CropAndScale(
162*d9f75844SAndroid Build Coastguard Worker     int offset_x,
163*d9f75844SAndroid Build Coastguard Worker     int offset_y,
164*d9f75844SAndroid Build Coastguard Worker     int crop_width,
165*d9f75844SAndroid Build Coastguard Worker     int crop_height,
166*d9f75844SAndroid Build Coastguard Worker     int scaled_width,
167*d9f75844SAndroid Build Coastguard Worker     int scaled_height) {
168*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<I422Buffer> result =
169*d9f75844SAndroid Build Coastguard Worker       I422Buffer::Create(scaled_width, scaled_height);
170*d9f75844SAndroid Build Coastguard Worker   result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
171*d9f75844SAndroid Build Coastguard Worker   return result;
172*d9f75844SAndroid Build Coastguard Worker }
173*d9f75844SAndroid Build Coastguard Worker 
type() const174*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type I010BufferInterface::type() const {
175*d9f75844SAndroid Build Coastguard Worker   return Type::kI010;
176*d9f75844SAndroid Build Coastguard Worker }
177*d9f75844SAndroid Build Coastguard Worker 
ChromaWidth() const178*d9f75844SAndroid Build Coastguard Worker int I010BufferInterface::ChromaWidth() const {
179*d9f75844SAndroid Build Coastguard Worker   return (width() + 1) / 2;
180*d9f75844SAndroid Build Coastguard Worker }
181*d9f75844SAndroid Build Coastguard Worker 
ChromaHeight() const182*d9f75844SAndroid Build Coastguard Worker int I010BufferInterface::ChromaHeight() const {
183*d9f75844SAndroid Build Coastguard Worker   return (height() + 1) / 2;
184*d9f75844SAndroid Build Coastguard Worker }
185*d9f75844SAndroid Build Coastguard Worker 
type() const186*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type I210BufferInterface::type() const {
187*d9f75844SAndroid Build Coastguard Worker   return Type::kI210;
188*d9f75844SAndroid Build Coastguard Worker }
189*d9f75844SAndroid Build Coastguard Worker 
ChromaWidth() const190*d9f75844SAndroid Build Coastguard Worker int I210BufferInterface::ChromaWidth() const {
191*d9f75844SAndroid Build Coastguard Worker   return (width() + 1) / 2;
192*d9f75844SAndroid Build Coastguard Worker }
193*d9f75844SAndroid Build Coastguard Worker 
ChromaHeight() const194*d9f75844SAndroid Build Coastguard Worker int I210BufferInterface::ChromaHeight() const {
195*d9f75844SAndroid Build Coastguard Worker   return height();
196*d9f75844SAndroid Build Coastguard Worker }
197*d9f75844SAndroid Build Coastguard Worker 
type() const198*d9f75844SAndroid Build Coastguard Worker VideoFrameBuffer::Type NV12BufferInterface::type() const {
199*d9f75844SAndroid Build Coastguard Worker   return Type::kNV12;
200*d9f75844SAndroid Build Coastguard Worker }
201*d9f75844SAndroid Build Coastguard Worker 
ChromaWidth() const202*d9f75844SAndroid Build Coastguard Worker int NV12BufferInterface::ChromaWidth() const {
203*d9f75844SAndroid Build Coastguard Worker   return (width() + 1) / 2;
204*d9f75844SAndroid Build Coastguard Worker }
205*d9f75844SAndroid Build Coastguard Worker 
ChromaHeight() const206*d9f75844SAndroid Build Coastguard Worker int NV12BufferInterface::ChromaHeight() const {
207*d9f75844SAndroid Build Coastguard Worker   return (height() + 1) / 2;
208*d9f75844SAndroid Build Coastguard Worker }
209*d9f75844SAndroid Build Coastguard Worker 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)210*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<VideoFrameBuffer> NV12BufferInterface::CropAndScale(
211*d9f75844SAndroid Build Coastguard Worker     int offset_x,
212*d9f75844SAndroid Build Coastguard Worker     int offset_y,
213*d9f75844SAndroid Build Coastguard Worker     int crop_width,
214*d9f75844SAndroid Build Coastguard Worker     int crop_height,
215*d9f75844SAndroid Build Coastguard Worker     int scaled_width,
216*d9f75844SAndroid Build Coastguard Worker     int scaled_height) {
217*d9f75844SAndroid Build Coastguard Worker   rtc::scoped_refptr<NV12Buffer> result =
218*d9f75844SAndroid Build Coastguard Worker       NV12Buffer::Create(scaled_width, scaled_height);
219*d9f75844SAndroid Build Coastguard Worker   result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
220*d9f75844SAndroid Build Coastguard Worker   return result;
221*d9f75844SAndroid Build Coastguard Worker }
222*d9f75844SAndroid Build Coastguard Worker 
223*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
224