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
media / base / video_frame_layout.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/base/video_frame_layout.h"
#include <numeric>
#include <sstream>
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/safe_conversions.h"
#include "media/base/video_frame.h"
namespace media {
namespace {
template <class T>
std::string VectorToString(const std::vector<T>& vec) {
std::ostringstream result;
std::string delim;
result << "[";
for (auto& v : vec) {
result << delim;
result << v;
if (delim.size() == 0)
delim = ", ";
}
result << "]";
return result.str();
}
std::vector<ColorPlaneLayout> CreatePlanes(VideoPixelFormat format,
const std::vector<size_t>& strides,
const gfx::Size& coded_size) {
std::vector<ColorPlaneLayout> planes(strides.size());
for (size_t i = 0; i < strides.size(); i++) {
size_t rows =
VideoFrame::PlaneSizeInSamples(format, i, coded_size).height();
planes[i].stride = strides[i];
planes[i].size = strides[i] * rows;
}
return planes;
}
} // namespace
// static
size_t VideoFrameLayout::NumPlanes(VideoPixelFormat format) {
switch (format) {
case PIXEL_FORMAT_UYVY:
case PIXEL_FORMAT_YUY2:
case PIXEL_FORMAT_ARGB:
case PIXEL_FORMAT_BGRA:
case PIXEL_FORMAT_XRGB:
case PIXEL_FORMAT_RGB24:
case PIXEL_FORMAT_MJPEG:
case PIXEL_FORMAT_Y16:
case PIXEL_FORMAT_ABGR:
case PIXEL_FORMAT_XBGR:
case PIXEL_FORMAT_XR30:
case PIXEL_FORMAT_XB30:
case PIXEL_FORMAT_RGBAF16:
return 1;
case PIXEL_FORMAT_NV12:
case PIXEL_FORMAT_NV16:
case PIXEL_FORMAT_NV21:
case PIXEL_FORMAT_NV24:
case PIXEL_FORMAT_P010LE:
case PIXEL_FORMAT_P210LE:
case PIXEL_FORMAT_P410LE:
return 2;
case PIXEL_FORMAT_I420:
case PIXEL_FORMAT_YV12:
case PIXEL_FORMAT_I422:
case PIXEL_FORMAT_I444:
case PIXEL_FORMAT_NV12A:
case PIXEL_FORMAT_YUV420P9:
case PIXEL_FORMAT_YUV422P9:
case PIXEL_FORMAT_YUV444P9:
case PIXEL_FORMAT_YUV420P10:
case PIXEL_FORMAT_YUV422P10:
case PIXEL_FORMAT_YUV444P10:
case PIXEL_FORMAT_YUV420P12:
case PIXEL_FORMAT_YUV422P12:
case PIXEL_FORMAT_YUV444P12:
return 3;
case PIXEL_FORMAT_I420A:
case PIXEL_FORMAT_I422A:
case PIXEL_FORMAT_I444A:
case PIXEL_FORMAT_YUV420AP10:
case PIXEL_FORMAT_YUV422AP10:
case PIXEL_FORMAT_YUV444AP10:
return 4;
case PIXEL_FORMAT_UNKNOWN:
// Note: PIXEL_FORMAT_UNKNOWN is used for end-of-stream frame.
// Set its NumPlanes() to zero to avoid NOTREACHED().
return 0;
}
NOTREACHED() << "Unsupported video frame format: " << format;
}
// static
std::optional<VideoFrameLayout> VideoFrameLayout::Create(
VideoPixelFormat format,
const gfx::Size& coded_size) {
return CreateWithStrides(format, coded_size,
std::vector<size_t>(NumPlanes(format), 0));
}
// static
std::optional<VideoFrameLayout> VideoFrameLayout::CreateWithStrides(
VideoPixelFormat format,
const gfx::Size& coded_size,
std::vector<size_t> strides,
size_t buffer_addr_align,
uint64_t modifier) {
return CreateWithPlanes(format, coded_size,
CreatePlanes(format, strides, coded_size),
buffer_addr_align, modifier);
}
// static
std::optional<VideoFrameLayout> VideoFrameLayout::CreateWithPlanes(
VideoPixelFormat format,
const gfx::Size& coded_size,
std::vector<ColorPlaneLayout> planes,
size_t buffer_addr_align,
uint64_t modifier) {
// NOTE: Even if format is UNKNOWN, it is valid if coded_sizes is not Empty().
// TODO(crbug.com/41421131): Return std::nullopt,
// if (format != PIXEL_FORMAT_UNKNOWN || !coded_sizes.IsEmpty())
// TODO(crbug.com/41421131): Return std::nullopt,
// if (planes.size() != NumPlanes(format))
return VideoFrameLayout(format, coded_size, std::move(planes),
false /*is_multi_planar */, buffer_addr_align,
modifier);
}
std::optional<VideoFrameLayout> VideoFrameLayout::CreateMultiPlanar(
VideoPixelFormat format,
const gfx::Size& coded_size,
std::vector<ColorPlaneLayout> planes,
size_t buffer_addr_align,
uint64_t modifier) {
// NOTE: Even if format is UNKNOWN, it is valid if coded_sizes is not Empty().
// TODO(crbug.com/41421131): Return std::nullopt,
// if (format != PIXEL_FORMAT_UNKNOWN || !coded_sizes.IsEmpty())
// TODO(crbug.com/41421131): Return std::nullopt,
// if (planes.size() != NumPlanes(format))
return VideoFrameLayout(format, coded_size, std::move(planes),
true /*is_multi_planar */, buffer_addr_align,
modifier);
}
VideoFrameLayout::VideoFrameLayout(VideoPixelFormat format,
const gfx::Size& coded_size,
std::vector<ColorPlaneLayout> planes,
bool is_multi_planar,
size_t buffer_addr_align,
uint64_t modifier)
: format_(format),
coded_size_(coded_size),
planes_(std::move(planes)),
is_multi_planar_(is_multi_planar),
buffer_addr_align_(buffer_addr_align),
modifier_(modifier) {
// Trigger NOTREACHED() if `format` is not valid.
NumPlanes(format);
}
VideoFrameLayout::~VideoFrameLayout() = default;
VideoFrameLayout::VideoFrameLayout(const VideoFrameLayout&) = default;
VideoFrameLayout::VideoFrameLayout(VideoFrameLayout&&) = default;
VideoFrameLayout& VideoFrameLayout::operator=(const VideoFrameLayout&) =
default;
bool VideoFrameLayout::operator==(const VideoFrameLayout& rhs) const {
return format_ == rhs.format_ && coded_size_ == rhs.coded_size_ &&
planes_ == rhs.planes_ && is_multi_planar_ == rhs.is_multi_planar_ &&
buffer_addr_align_ == rhs.buffer_addr_align_ &&
modifier_ == rhs.modifier_;
}
bool VideoFrameLayout::operator!=(const VideoFrameLayout& rhs) const {
return !(*this == rhs);
}
bool VideoFrameLayout::FitsInContiguousBufferOfSize(size_t data_size) const {
if (is_multi_planar_) {
return false;
}
for (const auto& plane : planes_) {
if (plane.offset > data_size || plane.size > data_size) {
return false;
}
// No individual plane should have a size + offset > data_size.
base::CheckedNumeric<size_t> plane_end = plane.size;
plane_end += plane.offset;
if (!plane_end.IsValid() || plane_end.ValueOrDie() > data_size) {
return false;
}
}
return true;
}
std::ostream& operator<<(std::ostream& ostream,
const VideoFrameLayout& layout) {
ostream << "VideoFrameLayout(format: " << layout.format()
<< ", coded_size: " << layout.coded_size().ToString()
<< ", planes (stride, offset, size): "
<< VectorToString(layout.planes())
<< ", is_multi_planar: " << layout.is_multi_planar()
<< ", buffer_addr_align: " << layout.buffer_addr_align()
<< ", modifier: 0x" << std::hex << layout.modifier() << ")";
return ostream;
}
} // namespace media