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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "GMPVideoi420FrameImpl.h"
#include "mozilla/gmp/GMPTypes.h"
#include "mozilla/CheckedInt.h"
namespace mozilla::gmp {
GMPVideoi420FrameImpl::GMPVideoi420FrameImpl(GMPVideoHostImpl* aHost)
: mYPlane(aHost),
mUPlane(aHost),
mVPlane(aHost),
mWidth(0),
mHeight(0),
mTimestamp(0ll),
mDuration(0ll) {
MOZ_ASSERT(aHost);
}
GMPVideoi420FrameImpl::GMPVideoi420FrameImpl(
const GMPVideoi420FrameData& aFrameData, GMPVideoHostImpl* aHost)
: mYPlane(aFrameData.mYPlane(), aHost),
mUPlane(aFrameData.mUPlane(), aHost),
mVPlane(aFrameData.mVPlane(), aHost),
mWidth(aFrameData.mWidth()),
mHeight(aFrameData.mHeight()),
mTimestamp(aFrameData.mTimestamp()),
mUpdatedTimestamp(aFrameData.mUpdatedTimestamp()),
mDuration(aFrameData.mDuration()) {
MOZ_ASSERT(aHost);
}
GMPVideoi420FrameImpl::~GMPVideoi420FrameImpl() = default;
bool GMPVideoi420FrameImpl::InitFrameData(GMPVideoi420FrameData& aFrameData) {
mYPlane.InitPlaneData(aFrameData.mYPlane());
mUPlane.InitPlaneData(aFrameData.mUPlane());
mVPlane.InitPlaneData(aFrameData.mVPlane());
aFrameData.mWidth() = mWidth;
aFrameData.mHeight() = mHeight;
aFrameData.mTimestamp() = mTimestamp;
aFrameData.mUpdatedTimestamp() = mUpdatedTimestamp;
aFrameData.mDuration() = mDuration;
return true;
}
GMPVideoFrameFormat GMPVideoi420FrameImpl::GetFrameFormat() {
return kGMPI420VideoFrame;
}
void GMPVideoi420FrameImpl::Destroy() { delete this; }
/* static */
bool GMPVideoi420FrameImpl::CheckFrameData(
const GMPVideoi420FrameData& aFrameData) {
// We may be passed the "wrong" shmem (one smaller than the actual size).
// This implies a bug or serious error on the child size. Ignore this frame
// if so. Note: Size() greater than expected is also an error, but with no
// negative consequences
int32_t half_width = (aFrameData.mWidth() + 1) / 2;
if ((aFrameData.mYPlane().mStride() <= 0) ||
(aFrameData.mYPlane().mSize() <= 0) ||
(aFrameData.mUPlane().mStride() <= 0) ||
(aFrameData.mUPlane().mSize() <= 0) ||
(aFrameData.mVPlane().mStride() <= 0) ||
(aFrameData.mVPlane().mSize() <= 0) ||
(aFrameData.mYPlane().mSize() >
(int32_t)aFrameData.mYPlane().mBuffer().Size<uint8_t>()) ||
(aFrameData.mUPlane().mSize() >
(int32_t)aFrameData.mUPlane().mBuffer().Size<uint8_t>()) ||
(aFrameData.mVPlane().mSize() >
(int32_t)aFrameData.mVPlane().mBuffer().Size<uint8_t>()) ||
(aFrameData.mYPlane().mStride() < aFrameData.mWidth()) ||
(aFrameData.mUPlane().mStride() < half_width) ||
(aFrameData.mVPlane().mStride() < half_width) ||
(aFrameData.mYPlane().mSize() <
aFrameData.mYPlane().mStride() * aFrameData.mHeight()) ||
(aFrameData.mUPlane().mSize() <
aFrameData.mUPlane().mStride() * ((aFrameData.mHeight() + 1) / 2)) ||
(aFrameData.mVPlane().mSize() <
aFrameData.mVPlane().mStride() * ((aFrameData.mHeight() + 1) / 2))) {
return false;
}
return true;
}
bool GMPVideoi420FrameImpl::CheckDimensions(int32_t aWidth, int32_t aHeight,
int32_t aStride_y,
int32_t aStride_u,
int32_t aStride_v) {
int32_t half_width = (aWidth + 1) / 2;
if (aWidth < 1 || aHeight < 1 || aStride_y < aWidth ||
aStride_u < half_width || aStride_v < half_width ||
!(CheckedInt<int32_t>(aHeight) * aStride_y +
((CheckedInt<int32_t>(aHeight) + 1) / 2) *
(CheckedInt<int32_t>(aStride_u) + aStride_v))
.isValid()) {
return false;
}
return true;
}
const GMPPlaneImpl* GMPVideoi420FrameImpl::GetPlane(GMPPlaneType aType) const {
switch (aType) {
case kGMPYPlane:
return &mYPlane;
case kGMPUPlane:
return &mUPlane;
case kGMPVPlane:
return &mVPlane;
default:
MOZ_CRASH("Unknown plane type!");
}
return nullptr;
}
GMPPlaneImpl* GMPVideoi420FrameImpl::GetPlane(GMPPlaneType aType) {
switch (aType) {
case kGMPYPlane:
return &mYPlane;
case kGMPUPlane:
return &mUPlane;
case kGMPVPlane:
return &mVPlane;
default:
MOZ_CRASH("Unknown plane type!");
}
return nullptr;
}
GMPErr GMPVideoi420FrameImpl::CreateEmptyFrame(int32_t aWidth, int32_t aHeight,
int32_t aStride_y,
int32_t aStride_u,
int32_t aStride_v) {
if (!CheckDimensions(aWidth, aHeight, aStride_y, aStride_u, aStride_v)) {
return GMPGenericErr;
}
int32_t size_y = aStride_y * aHeight;
int32_t half_height = (aHeight + 1) / 2;
int32_t size_u = aStride_u * half_height;
int32_t size_v = aStride_v * half_height;
GMPErr err = mYPlane.CreateEmptyPlane(size_y, aStride_y, size_y);
if (err != GMPNoErr) {
return err;
}
err = mUPlane.CreateEmptyPlane(size_u, aStride_u, size_u);
if (err != GMPNoErr) {
return err;
}
err = mVPlane.CreateEmptyPlane(size_v, aStride_v, size_v);
if (err != GMPNoErr) {
return err;
}
mWidth = aWidth;
mHeight = aHeight;
mTimestamp = 0ll;
mUpdatedTimestamp.reset();
mDuration = 0ll;
return GMPNoErr;
}
GMPErr GMPVideoi420FrameImpl::CreateFrame(
int32_t aSize_y, const uint8_t* aBuffer_y, int32_t aSize_u,
const uint8_t* aBuffer_u, int32_t aSize_v, const uint8_t* aBuffer_v,
int32_t aWidth, int32_t aHeight, int32_t aStride_y, int32_t aStride_u,
int32_t aStride_v) {
MOZ_ASSERT(aBuffer_y);
MOZ_ASSERT(aBuffer_u);
MOZ_ASSERT(aBuffer_v);
if (aSize_y < 1 || aSize_u < 1 || aSize_v < 1) {
return GMPGenericErr;
}
if (!CheckDimensions(aWidth, aHeight, aStride_y, aStride_u, aStride_v)) {
return GMPGenericErr;
}
GMPErr err = mYPlane.Copy(aSize_y, aStride_y, aBuffer_y);
if (err != GMPNoErr) {
return err;
}
err = mUPlane.Copy(aSize_u, aStride_u, aBuffer_u);
if (err != GMPNoErr) {
return err;
}
err = mVPlane.Copy(aSize_v, aStride_v, aBuffer_v);
if (err != GMPNoErr) {
return err;
}
mWidth = aWidth;
mHeight = aHeight;
return GMPNoErr;
}
GMPErr GMPVideoi420FrameImpl::CopyFrame(const GMPVideoi420Frame& aFrame) {
auto& f = static_cast<const GMPVideoi420FrameImpl&>(aFrame);
GMPErr err = mYPlane.Copy(f.mYPlane);
if (err != GMPNoErr) {
return err;
}
err = mUPlane.Copy(f.mUPlane);
if (err != GMPNoErr) {
return err;
}
err = mVPlane.Copy(f.mVPlane);
if (err != GMPNoErr) {
return err;
}
mWidth = f.mWidth;
mHeight = f.mHeight;
mTimestamp = f.mTimestamp;
mUpdatedTimestamp = f.mUpdatedTimestamp;
mDuration = f.mDuration;
return GMPNoErr;
}
void GMPVideoi420FrameImpl::SwapFrame(GMPVideoi420Frame* aFrame) {
auto f = static_cast<GMPVideoi420FrameImpl*>(aFrame);
mYPlane.Swap(f->mYPlane);
mUPlane.Swap(f->mUPlane);
mVPlane.Swap(f->mVPlane);
std::swap(mWidth, f->mWidth);
std::swap(mHeight, f->mHeight);
std::swap(mTimestamp, f->mTimestamp);
std::swap(mUpdatedTimestamp, f->mUpdatedTimestamp);
std::swap(mDuration, f->mDuration);
}
uint8_t* GMPVideoi420FrameImpl::Buffer(GMPPlaneType aType) {
GMPPlane* p = GetPlane(aType);
if (p) {
return p->Buffer();
}
return nullptr;
}
const uint8_t* GMPVideoi420FrameImpl::Buffer(GMPPlaneType aType) const {
const GMPPlane* p = GetPlane(aType);
if (p) {
return p->Buffer();
}
return nullptr;
}
int32_t GMPVideoi420FrameImpl::AllocatedSize(GMPPlaneType aType) const {
const GMPPlane* p = GetPlane(aType);
if (p) {
return p->AllocatedSize();
}
return -1;
}
int32_t GMPVideoi420FrameImpl::Stride(GMPPlaneType aType) const {
const GMPPlane* p = GetPlane(aType);
if (p) {
return p->Stride();
}
return -1;
}
GMPErr GMPVideoi420FrameImpl::SetWidth(int32_t aWidth) {
if (!CheckDimensions(aWidth, mHeight, mYPlane.Stride(), mUPlane.Stride(),
mVPlane.Stride())) {
return GMPGenericErr;
}
mWidth = aWidth;
return GMPNoErr;
}
GMPErr GMPVideoi420FrameImpl::SetHeight(int32_t aHeight) {
if (!CheckDimensions(mWidth, aHeight, mYPlane.Stride(), mUPlane.Stride(),
mVPlane.Stride())) {
return GMPGenericErr;
}
mHeight = aHeight;
return GMPNoErr;
}
int32_t GMPVideoi420FrameImpl::Width() const { return mWidth; }
int32_t GMPVideoi420FrameImpl::Height() const { return mHeight; }
void GMPVideoi420FrameImpl::SetTimestamp(uint64_t aTimestamp) {
mTimestamp = aTimestamp;
}
uint64_t GMPVideoi420FrameImpl::Timestamp() const { return mTimestamp; }
void GMPVideoi420FrameImpl::SetUpdatedTimestamp(uint64_t aTimestamp) {
mUpdatedTimestamp = Some(aTimestamp);
}
uint64_t GMPVideoi420FrameImpl::UpdatedTimestamp() const {
return mUpdatedTimestamp ? *mUpdatedTimestamp : mTimestamp;
}
void GMPVideoi420FrameImpl::SetDuration(uint64_t aDuration) {
mDuration = aDuration;
}
uint64_t GMPVideoi420FrameImpl::Duration() const { return mDuration; }
bool GMPVideoi420FrameImpl::IsZeroSize() const {
return (mYPlane.IsZeroSize() && mUPlane.IsZeroSize() && mVPlane.IsZeroSize());
}
void GMPVideoi420FrameImpl::ResetSize() {
mYPlane.ResetSize();
mUPlane.ResetSize();
mVPlane.ResetSize();
}
} // namespace mozilla::gmp
|