magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "api/video/video_frame_buffer.h" |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 12 | |
Ilya Nikolaevskiy | 38e9b06 | 2020-10-08 14:36:33 +0000 | [diff] [blame] | 13 | #include "api/video/i420_buffer.h" |
Sergio Garcia Murillo | b63536f | 2022-03-25 09:04:09 +0100 | [diff] [blame] | 14 | #include "api/video/i422_buffer.h" |
Stefan Mitic | ffdc680 | 2022-02-08 07:00:16 -0800 | [diff] [blame] | 15 | #include "api/video/i444_buffer.h" |
Henrik Boström | f412976 | 2021-03-22 10:22:54 +0100 | [diff] [blame] | 16 | #include "api/video/nv12_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/checks.h" |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Ilya Nikolaevskiy | 38e9b06 | 2020-10-08 14:36:33 +0000 | [diff] [blame] | 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 | |
Ilya Nikolaevskiy | a8507e3 | 2019-05-03 11:39:26 +0200 | [diff] [blame] | 35 | 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; |
Emircan Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | const I420ABufferInterface* VideoFrameBuffer::GetI420A() const { |
| 42 | RTC_CHECK(type() == Type::kI420A); |
| 43 | return static_cast<const I420ABufferInterface*>(this); |
| 44 | } |
| 45 | |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 46 | const I444BufferInterface* VideoFrameBuffer::GetI444() const { |
| 47 | RTC_CHECK(type() == Type::kI444); |
| 48 | return static_cast<const I444BufferInterface*>(this); |
| 49 | } |
| 50 | |
Sergio Garcia Murillo | b63536f | 2022-03-25 09:04:09 +0100 | [diff] [blame] | 51 | const I422BufferInterface* VideoFrameBuffer::GetI422() const { |
| 52 | RTC_CHECK(type() == Type::kI422); |
| 53 | return static_cast<const I422BufferInterface*>(this); |
| 54 | } |
| 55 | |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 56 | const I010BufferInterface* VideoFrameBuffer::GetI010() const { |
| 57 | RTC_CHECK(type() == Type::kI010); |
| 58 | return static_cast<const I010BufferInterface*>(this); |
| 59 | } |
| 60 | |
Sergio Garcia Murillo | 8545eba | 2022-06-17 11:48:14 +0200 | [diff] [blame] | 61 | const I210BufferInterface* VideoFrameBuffer::GetI210() const { |
| 62 | RTC_CHECK(type() == Type::kI210); |
| 63 | return static_cast<const I210BufferInterface*>(this); |
| 64 | } |
| 65 | |
Evan Shrubsole | 8499543 | 2020-09-09 16:14:19 +0200 | [diff] [blame] | 66 | const NV12BufferInterface* VideoFrameBuffer::GetNV12() const { |
| 67 | RTC_CHECK(type() == Type::kNV12); |
| 68 | return static_cast<const NV12BufferInterface*>(this); |
| 69 | } |
| 70 | |
Evan Shrubsole | b556b08 | 2020-10-08 14:56:45 +0200 | [diff] [blame] | 71 | rtc::scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::GetMappedFrameBuffer( |
| 72 | rtc::ArrayView<Type> types) { |
| 73 | RTC_CHECK(type() == Type::kNative); |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 77 | VideoFrameBuffer::Type I420BufferInterface::type() const { |
| 78 | return Type::kI420; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Evan Shrubsole | b556b08 | 2020-10-08 14:56:45 +0200 | [diff] [blame] | 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"; |
Sergio Garcia Murillo | b63536f | 2022-03-25 09:04:09 +0100 | [diff] [blame] | 91 | case VideoFrameBuffer::Type::kI422: |
| 92 | return "kI422"; |
Evan Shrubsole | b556b08 | 2020-10-08 14:56:45 +0200 | [diff] [blame] | 93 | case VideoFrameBuffer::Type::kI010: |
| 94 | return "kI010"; |
Sergio Garcia Murillo | 8545eba | 2022-06-17 11:48:14 +0200 | [diff] [blame] | 95 | case VideoFrameBuffer::Type::kI210: |
| 96 | return "kI210"; |
Evan Shrubsole | b556b08 | 2020-10-08 14:56:45 +0200 | [diff] [blame] | 97 | case VideoFrameBuffer::Type::kNV12: |
| 98 | return "kNV12"; |
| 99 | default: |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 100 | RTC_DCHECK_NOTREACHED(); |
Evan Shrubsole | b556b08 | 2020-10-08 14:56:45 +0200 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 104 | int I420BufferInterface::ChromaWidth() const { |
| 105 | return (width() + 1) / 2; |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 106 | } |
| 107 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 108 | int I420BufferInterface::ChromaHeight() const { |
| 109 | return (height() + 1) / 2; |
| 110 | } |
| 111 | |
| 112 | rtc::scoped_refptr<I420BufferInterface> I420BufferInterface::ToI420() { |
Niels Möller | 961f382 | 2022-01-12 10:06:55 +0100 | [diff] [blame] | 113 | return rtc::scoped_refptr<I420BufferInterface>(this); |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Ilya Nikolaevskiy | a8507e3 | 2019-05-03 11:39:26 +0200 | [diff] [blame] | 116 | const I420BufferInterface* I420BufferInterface::GetI420() const { |
| 117 | return this; |
| 118 | } |
| 119 | |
Emircan Uysaler | 574eaa4 | 2017-11-09 12:33:24 -0800 | [diff] [blame] | 120 | VideoFrameBuffer::Type I420ABufferInterface::type() const { |
| 121 | return Type::kI420A; |
| 122 | } |
| 123 | |
magjed | eaf4a1e | 2017-05-30 01:21:59 -0700 | [diff] [blame] | 124 | VideoFrameBuffer::Type I444BufferInterface::type() const { |
| 125 | return Type::kI444; |
| 126 | } |
| 127 | |
| 128 | int I444BufferInterface::ChromaWidth() const { |
| 129 | return width(); |
| 130 | } |
| 131 | |
| 132 | int I444BufferInterface::ChromaHeight() const { |
| 133 | return height(); |
| 134 | } |
| 135 | |
Stefan Mitic | ffdc680 | 2022-02-08 07:00:16 -0800 | [diff] [blame] | 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 | |
Sergio Garcia Murillo | b63536f | 2022-03-25 09:04:09 +0100 | [diff] [blame] | 149 | VideoFrameBuffer::Type I422BufferInterface::type() const { |
| 150 | return Type::kI422; |
| 151 | } |
| 152 | |
| 153 | int I422BufferInterface::ChromaWidth() const { |
| 154 | return (width() + 1) / 2; |
| 155 | } |
| 156 | |
| 157 | int I422BufferInterface::ChromaHeight() const { |
| 158 | return height(); |
| 159 | } |
| 160 | |
| 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 | |
Emircan Uysaler | 901e0ff | 2018-06-26 12:22:38 -0700 | [diff] [blame] | 174 | VideoFrameBuffer::Type I010BufferInterface::type() const { |
| 175 | return Type::kI010; |
| 176 | } |
| 177 | |
| 178 | int I010BufferInterface::ChromaWidth() const { |
| 179 | return (width() + 1) / 2; |
| 180 | } |
| 181 | |
| 182 | int I010BufferInterface::ChromaHeight() const { |
| 183 | return (height() + 1) / 2; |
| 184 | } |
| 185 | |
Sergio Garcia Murillo | 8545eba | 2022-06-17 11:48:14 +0200 | [diff] [blame] | 186 | VideoFrameBuffer::Type I210BufferInterface::type() const { |
| 187 | return Type::kI210; |
| 188 | } |
| 189 | |
| 190 | int I210BufferInterface::ChromaWidth() const { |
| 191 | return (width() + 1) / 2; |
| 192 | } |
| 193 | |
| 194 | int I210BufferInterface::ChromaHeight() const { |
| 195 | return height(); |
| 196 | } |
| 197 | |
Evan Shrubsole | 8499543 | 2020-09-09 16:14:19 +0200 | [diff] [blame] | 198 | VideoFrameBuffer::Type NV12BufferInterface::type() const { |
| 199 | return Type::kNV12; |
| 200 | } |
| 201 | |
| 202 | int NV12BufferInterface::ChromaWidth() const { |
| 203 | return (width() + 1) / 2; |
| 204 | } |
| 205 | |
| 206 | int NV12BufferInterface::ChromaHeight() const { |
| 207 | return (height() + 1) / 2; |
| 208 | } |
Henrik Boström | f412976 | 2021-03-22 10:22:54 +0100 | [diff] [blame] | 209 | |
| 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 | |
magjed | 712338e | 2017-05-11 05:11:57 -0700 | [diff] [blame] | 223 | } // namespace webrtc |