1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
/*
* Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "api/video/video_frame_buffer.h"
#include <string>
#include "api/array_view.h"
#include "api/scoped_refptr.h"
#include "api/video/i420_buffer.h"
#include "api/video/i422_buffer.h"
#include "api/video/i444_buffer.h"
#include "api/video/nv12_buffer.h"
#include "rtc_base/checks.h"
namespace webrtc {
scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::CropAndScale(
int offset_x,
int offset_y,
int crop_width,
int crop_height,
int scaled_width,
int scaled_height) {
scoped_refptr<I420Buffer> result =
I420Buffer::Create(scaled_width, scaled_height);
result->CropAndScaleFrom(*this->ToI420(), offset_x, offset_y, crop_width,
crop_height);
return result;
}
const I420BufferInterface* VideoFrameBuffer::GetI420() const {
// Overridden by subclasses that can return an I420 buffer without any
// conversion, in particular, I420BufferInterface.
return nullptr;
}
const I420ABufferInterface* VideoFrameBuffer::GetI420A() const {
RTC_CHECK(type() == Type::kI420A);
return static_cast<const I420ABufferInterface*>(this);
}
const I444BufferInterface* VideoFrameBuffer::GetI444() const {
RTC_CHECK(type() == Type::kI444);
return static_cast<const I444BufferInterface*>(this);
}
const I422BufferInterface* VideoFrameBuffer::GetI422() const {
RTC_CHECK(type() == Type::kI422);
return static_cast<const I422BufferInterface*>(this);
}
const I010BufferInterface* VideoFrameBuffer::GetI010() const {
RTC_CHECK(type() == Type::kI010);
return static_cast<const I010BufferInterface*>(this);
}
const I210BufferInterface* VideoFrameBuffer::GetI210() const {
RTC_CHECK(type() == Type::kI210);
return static_cast<const I210BufferInterface*>(this);
}
const I410BufferInterface* VideoFrameBuffer::GetI410() const {
RTC_CHECK(type() == Type::kI410);
return static_cast<const I410BufferInterface*>(this);
}
const NV12BufferInterface* VideoFrameBuffer::GetNV12() const {
RTC_CHECK(type() == Type::kNV12);
return static_cast<const NV12BufferInterface*>(this);
}
scoped_refptr<VideoFrameBuffer> VideoFrameBuffer::GetMappedFrameBuffer(
ArrayView<Type> /* types */) {
RTC_CHECK(type() == Type::kNative);
return nullptr;
}
std::string VideoFrameBuffer::storage_representation() const {
return "?";
}
VideoFrameBuffer::Type I420BufferInterface::type() const {
return Type::kI420;
}
const char* VideoFrameBufferTypeToString(VideoFrameBuffer::Type type) {
switch (type) {
case VideoFrameBuffer::Type::kNative:
return "kNative";
case VideoFrameBuffer::Type::kI420:
return "kI420";
case VideoFrameBuffer::Type::kI420A:
return "kI420A";
case VideoFrameBuffer::Type::kI444:
return "kI444";
case VideoFrameBuffer::Type::kI422:
return "kI422";
case VideoFrameBuffer::Type::kI010:
return "kI010";
case VideoFrameBuffer::Type::kI210:
return "kI210";
case VideoFrameBuffer::Type::kI410:
return "kI410";
case VideoFrameBuffer::Type::kNV12:
return "kNV12";
default:
RTC_DCHECK_NOTREACHED();
}
}
int I420BufferInterface::ChromaWidth() const {
return (width() + 1) / 2;
}
int I420BufferInterface::ChromaHeight() const {
return (height() + 1) / 2;
}
scoped_refptr<I420BufferInterface> I420BufferInterface::ToI420() {
return scoped_refptr<I420BufferInterface>(this);
}
const I420BufferInterface* I420BufferInterface::GetI420() const {
return this;
}
VideoFrameBuffer::Type I420ABufferInterface::type() const {
return Type::kI420A;
}
VideoFrameBuffer::Type I444BufferInterface::type() const {
return Type::kI444;
}
int I444BufferInterface::ChromaWidth() const {
return width();
}
int I444BufferInterface::ChromaHeight() const {
return height();
}
scoped_refptr<VideoFrameBuffer> I444BufferInterface::CropAndScale(
int offset_x,
int offset_y,
int crop_width,
int crop_height,
int scaled_width,
int scaled_height) {
scoped_refptr<I444Buffer> result =
I444Buffer::Create(scaled_width, scaled_height);
result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
return result;
}
VideoFrameBuffer::Type I422BufferInterface::type() const {
return Type::kI422;
}
int I422BufferInterface::ChromaWidth() const {
return (width() + 1) / 2;
}
int I422BufferInterface::ChromaHeight() const {
return height();
}
scoped_refptr<VideoFrameBuffer> I422BufferInterface::CropAndScale(
int offset_x,
int offset_y,
int crop_width,
int crop_height,
int scaled_width,
int scaled_height) {
scoped_refptr<I422Buffer> result =
I422Buffer::Create(scaled_width, scaled_height);
result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
return result;
}
VideoFrameBuffer::Type I010BufferInterface::type() const {
return Type::kI010;
}
int I010BufferInterface::ChromaWidth() const {
return (width() + 1) / 2;
}
int I010BufferInterface::ChromaHeight() const {
return (height() + 1) / 2;
}
VideoFrameBuffer::Type I210BufferInterface::type() const {
return Type::kI210;
}
int I210BufferInterface::ChromaWidth() const {
return (width() + 1) / 2;
}
int I210BufferInterface::ChromaHeight() const {
return height();
}
VideoFrameBuffer::Type I410BufferInterface::type() const {
return Type::kI410;
}
int I410BufferInterface::ChromaWidth() const {
return width();
}
int I410BufferInterface::ChromaHeight() const {
return height();
}
VideoFrameBuffer::Type NV12BufferInterface::type() const {
return Type::kNV12;
}
int NV12BufferInterface::ChromaWidth() const {
return (width() + 1) / 2;
}
int NV12BufferInterface::ChromaHeight() const {
return (height() + 1) / 2;
}
scoped_refptr<VideoFrameBuffer> NV12BufferInterface::CropAndScale(
int offset_x,
int offset_y,
int crop_width,
int crop_height,
int scaled_width,
int scaled_height) {
scoped_refptr<NV12Buffer> result =
NV12Buffer::Create(scaled_width, scaled_height);
result->CropAndScaleFrom(*this, offset_x, offset_y, crop_width, crop_height);
return result;
}
void CheckValidDimensions(int width,
int height,
int stride_y,
int stride_u,
int stride_v) {
RTC_CHECK_GT(width, 0);
RTC_CHECK_GT(height, 0);
RTC_CHECK_GE(stride_y, width);
RTC_CHECK_GT(stride_u, 0);
RTC_CHECK_GT(stride_v, 0);
}
} // namespace webrtc
|