xref: /aosp_15_r20/external/webrtc/api/video/video_frame_buffer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "api/video/video_frame_buffer.h"
12 
13 #include "api/video/i420_buffer.h"
14 #include "api/video/i422_buffer.h"
15 #include "api/video/i444_buffer.h"
16 #include "api/video/nv12_buffer.h"
17 #include "rtc_base/checks.h"
18 
19 namespace webrtc {
20 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)21 rtc::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 
GetI420() const35 const I420BufferInterface* VideoFrameBuffer::GetI420() const {
36   // Overridden by subclasses that can return an I420 buffer without any
37   // conversion, in particular, I420BufferInterface.
38   return nullptr;
39 }
40 
GetI420A() const41 const I420ABufferInterface* VideoFrameBuffer::GetI420A() const {
42   RTC_CHECK(type() == Type::kI420A);
43   return static_cast<const I420ABufferInterface*>(this);
44 }
45 
GetI444() const46 const I444BufferInterface* VideoFrameBuffer::GetI444() const {
47   RTC_CHECK(type() == Type::kI444);
48   return static_cast<const I444BufferInterface*>(this);
49 }
50 
GetI422() const51 const I422BufferInterface* VideoFrameBuffer::GetI422() const {
52   RTC_CHECK(type() == Type::kI422);
53   return static_cast<const I422BufferInterface*>(this);
54 }
55 
GetI010() const56 const I010BufferInterface* VideoFrameBuffer::GetI010() const {
57   RTC_CHECK(type() == Type::kI010);
58   return static_cast<const I010BufferInterface*>(this);
59 }
60 
GetI210() const61 const I210BufferInterface* VideoFrameBuffer::GetI210() const {
62   RTC_CHECK(type() == Type::kI210);
63   return static_cast<const I210BufferInterface*>(this);
64 }
65 
GetNV12() const66 const NV12BufferInterface* VideoFrameBuffer::GetNV12() const {
67   RTC_CHECK(type() == Type::kNV12);
68   return static_cast<const NV12BufferInterface*>(this);
69 }
70 
GetMappedFrameBuffer(rtc::ArrayView<Type> types)71 rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::GetMappedFrameBuffer(
72     rtc::ArrayView<Type> types) {
73   RTC_CHECK(type() == Type::kNative);
74   return nullptr;
75 }
76 
type() const77 VideoFrameBuffer::Type I420BufferInterface::type() const {
78   return Type::kI420;
79 }
80 
VideoFrameBufferTypeToString(VideoFrameBuffer::Type type)81 const 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";
91     case VideoFrameBuffer::Type::kI422:
92       return "kI422";
93     case VideoFrameBuffer::Type::kI010:
94       return "kI010";
95     case VideoFrameBuffer::Type::kI210:
96       return "kI210";
97     case VideoFrameBuffer::Type::kNV12:
98       return "kNV12";
99     default:
100       RTC_DCHECK_NOTREACHED();
101   }
102 }
103 
ChromaWidth() const104 int I420BufferInterface::ChromaWidth() const {
105   return (width() + 1) / 2;
106 }
107 
ChromaHeight() const108 int I420BufferInterface::ChromaHeight() const {
109   return (height() + 1) / 2;
110 }
111 
ToI420()112 rtc::scoped_refptr<I420BufferInterface> I420BufferInterface::ToI420() {
113   return rtc::scoped_refptr<I420BufferInterface>(this);
114 }
115 
GetI420() const116 const I420BufferInterface* I420BufferInterface::GetI420() const {
117   return this;
118 }
119 
type() const120 VideoFrameBuffer::Type I420ABufferInterface::type() const {
121   return Type::kI420A;
122 }
123 
type() const124 VideoFrameBuffer::Type I444BufferInterface::type() const {
125   return Type::kI444;
126 }
127 
ChromaWidth() const128 int I444BufferInterface::ChromaWidth() const {
129   return width();
130 }
131 
ChromaHeight() const132 int I444BufferInterface::ChromaHeight() const {
133   return height();
134 }
135 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)136 rtc::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 
type() const149 VideoFrameBuffer::Type I422BufferInterface::type() const {
150   return Type::kI422;
151 }
152 
ChromaWidth() const153 int I422BufferInterface::ChromaWidth() const {
154   return (width() + 1) / 2;
155 }
156 
ChromaHeight() const157 int I422BufferInterface::ChromaHeight() const {
158   return height();
159 }
160 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)161 rtc::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 
type() const174 VideoFrameBuffer::Type I010BufferInterface::type() const {
175   return Type::kI010;
176 }
177 
ChromaWidth() const178 int I010BufferInterface::ChromaWidth() const {
179   return (width() + 1) / 2;
180 }
181 
ChromaHeight() const182 int I010BufferInterface::ChromaHeight() const {
183   return (height() + 1) / 2;
184 }
185 
type() const186 VideoFrameBuffer::Type I210BufferInterface::type() const {
187   return Type::kI210;
188 }
189 
ChromaWidth() const190 int I210BufferInterface::ChromaWidth() const {
191   return (width() + 1) / 2;
192 }
193 
ChromaHeight() const194 int I210BufferInterface::ChromaHeight() const {
195   return height();
196 }
197 
type() const198 VideoFrameBuffer::Type NV12BufferInterface::type() const {
199   return Type::kNV12;
200 }
201 
ChromaWidth() const202 int NV12BufferInterface::ChromaWidth() const {
203   return (width() + 1) / 2;
204 }
205 
ChromaHeight() const206 int NV12BufferInterface::ChromaHeight() const {
207   return (height() + 1) / 2;
208 }
209 
CropAndScale(int offset_x,int offset_y,int crop_width,int crop_height,int scaled_width,int scaled_height)210 rtc::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 
223 }  // namespace webrtc
224