Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "libyuv.h"
8
#include "MacIOSurfaceHelpers.h"
9
#include "mozilla/gfx/MacIOSurface.h"
10
#include "YCbCrUtils.h"
11
12
namespace mozilla {
13
14
using namespace gfx;
15
16
namespace layers {
17
18
#define ALIGNED_32(x) ((x + 31) & ~31)
19
#define ALIGNEDPTR_32(x) \
20
reinterpret_cast<uint8_t*>((reinterpret_cast<uintptr_t>(x) + 31) & ~31)
21
22
static already_AddRefed<SourceSurface>
23
CreateSourceSurfaceFromLockedMacIOSurface(MacIOSurface* aSurface) {
24
size_t bytesPerRow = aSurface->GetBytesPerRow();
25
size_t ioWidth = aSurface->GetDevicePixelWidth();
26
size_t ioHeight = aSurface->GetDevicePixelHeight();
27
SurfaceFormat ioFormat = aSurface->GetFormat();
28
29
if ((ioFormat == SurfaceFormat::NV12 || ioFormat == SurfaceFormat::YUV422) &&
30
(ioWidth > PlanarYCbCrImage::MAX_DIMENSION ||
31
ioHeight > PlanarYCbCrImage::MAX_DIMENSION)) {
32
return nullptr;
33
}
34
35
SurfaceFormat format =
36
(ioFormat == SurfaceFormat::NV12 || ioFormat == SurfaceFormat::YUV422)
37
? SurfaceFormat::B8G8R8X8
38
: SurfaceFormat::B8G8R8A8;
39
40
RefPtr<DataSourceSurface> dataSurface = Factory::CreateDataSourceSurface(
41
IntSize::Truncate(ioWidth, ioHeight), format);
42
if (NS_WARN_IF(!dataSurface)) {
43
return nullptr;
44
}
45
46
DataSourceSurface::MappedSurface mappedSurface;
47
if (!dataSurface->Map(DataSourceSurface::WRITE, &mappedSurface)) {
48
return nullptr;
49
}
50
51
if (ioFormat == SurfaceFormat::NV12) {
52
/* Extract and separate the CbCr planes */
53
size_t cbCrStride = aSurface->GetBytesPerRow(1);
54
size_t cbCrWidth = aSurface->GetDevicePixelWidth(1);
55
size_t cbCrHeight = aSurface->GetDevicePixelHeight(1);
56
57
auto cbPlane = MakeUnique<uint8_t[]>(cbCrWidth * cbCrHeight);
58
auto crPlane = MakeUnique<uint8_t[]>(cbCrWidth * cbCrHeight);
59
60
uint8_t* src = (uint8_t*)aSurface->GetBaseAddressOfPlane(1);
61
uint8_t* cbDest = cbPlane.get();
62
uint8_t* crDest = crPlane.get();
63
64
for (size_t i = 0; i < cbCrHeight; i++) {
65
uint8_t* rowSrc = src + cbCrStride * i;
66
for (size_t j = 0; j < cbCrWidth; j++) {
67
*cbDest = *rowSrc;
68
cbDest++;
69
rowSrc++;
70
*crDest = *rowSrc;
71
crDest++;
72
rowSrc++;
73
}
74
}
75
76
/* Convert to RGB */
77
PlanarYCbCrData data;
78
data.mYChannel = (uint8_t*)aSurface->GetBaseAddressOfPlane(0);
79
data.mYStride = aSurface->GetBytesPerRow(0);
80
data.mYSize = IntSize::Truncate(ioWidth, ioHeight);
81
data.mCbChannel = cbPlane.get();
82
data.mCrChannel = crPlane.get();
83
data.mCbCrStride = cbCrWidth;
84
data.mCbCrSize = IntSize::Truncate(cbCrWidth, cbCrHeight);
85
data.mPicSize = data.mYSize;
86
data.mYUVColorSpace = aSurface->GetYUVColorSpace();
87
data.mColorRange = aSurface->IsFullRange() ? gfx::ColorRange::FULL
88
: gfx::ColorRange::LIMITED;
89
90
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8,
91
IntSize::Truncate(ioWidth, ioHeight), mappedSurface.mData,
92
mappedSurface.mStride);
93
} else if (ioFormat == SurfaceFormat::YUV422) {
94
if (ioWidth == ALIGNED_32(ioWidth)) {
95
// Optimization when width is aligned to 32.
96
IntSize size = IntSize::Truncate(ioWidth, ioHeight);
97
libyuv::ConvertToARGB((uint8_t*)aSurface->GetBaseAddress(),
98
0 /* not used */, mappedSurface.mData,
99
mappedSurface.mStride, 0, 0, size.width,
100
size.height, size.width, size.height,
101
libyuv::kRotate0, libyuv::FOURCC_UYVY);
102
} else {
103
/* Convert to YV16 */
104
size_t cbCrWidth = (ioWidth + 1) >> 1;
105
size_t cbCrHeight = ioHeight;
106
// Ensure our stride is a multiple of 32 to allow for memory aligned rows.
107
size_t cbCrStride = ALIGNED_32(cbCrWidth);
108
size_t strideDelta = cbCrStride - cbCrWidth;
109
MOZ_ASSERT(strideDelta <= 31);
110
111
auto yPlane = MakeUnique<uint8_t[]>(cbCrStride * 2 * ioHeight + 31);
112
auto cbPlane = MakeUnique<uint8_t[]>(cbCrStride * cbCrHeight + 31);
113
auto crPlane = MakeUnique<uint8_t[]>(cbCrStride * cbCrHeight + 31);
114
115
uint8_t* src = (uint8_t*)aSurface->GetBaseAddress();
116
uint8_t* yDest = ALIGNEDPTR_32(yPlane.get());
117
uint8_t* cbDest = ALIGNEDPTR_32(cbPlane.get());
118
uint8_t* crDest = ALIGNEDPTR_32(crPlane.get());
119
120
for (size_t i = 0; i < ioHeight; i++) {
121
uint8_t* rowSrc = src + bytesPerRow * i;
122
for (size_t j = 0; j < cbCrWidth; j++) {
123
*cbDest = *rowSrc;
124
cbDest++;
125
rowSrc++;
126
*yDest = *rowSrc;
127
yDest++;
128
rowSrc++;
129
*crDest = *rowSrc;
130
crDest++;
131
rowSrc++;
132
*yDest = *rowSrc;
133
yDest++;
134
rowSrc++;
135
}
136
if (strideDelta) {
137
cbDest += strideDelta;
138
crDest += strideDelta;
139
yDest += strideDelta << 1;
140
}
141
}
142
143
/* Convert to RGB */
144
PlanarYCbCrData data;
145
data.mYChannel = ALIGNEDPTR_32(yPlane.get());
146
data.mYStride = cbCrStride * 2;
147
data.mYSize = IntSize::Truncate(ioWidth, ioHeight);
148
data.mCbChannel = ALIGNEDPTR_32(cbPlane.get());
149
data.mCrChannel = ALIGNEDPTR_32(crPlane.get());
150
data.mCbCrStride = cbCrStride;
151
data.mCbCrSize = IntSize::Truncate(cbCrWidth, cbCrHeight);
152
data.mPicSize = data.mYSize;
153
data.mYUVColorSpace = aSurface->GetYUVColorSpace();
154
data.mColorRange = aSurface->IsFullRange() ? gfx::ColorRange::FULL
155
: gfx::ColorRange::LIMITED;
156
157
ConvertYCbCrToRGB(data, SurfaceFormat::B8G8R8X8,
158
IntSize::Truncate(ioWidth, ioHeight),
159
mappedSurface.mData, mappedSurface.mStride);
160
}
161
} else {
162
unsigned char* ioData = (unsigned char*)aSurface->GetBaseAddress();
163
164
for (size_t i = 0; i < ioHeight; ++i) {
165
memcpy(mappedSurface.mData + i * mappedSurface.mStride,
166
ioData + i * bytesPerRow, ioWidth * 4);
167
}
168
}
169
170
dataSurface->Unmap();
171
172
return dataSurface.forget();
173
}
174
175
already_AddRefed<SourceSurface> CreateSourceSurfaceFromMacIOSurface(
176
MacIOSurface* aSurface) {
177
aSurface->Lock();
178
RefPtr<SourceSurface> result =
179
CreateSourceSurfaceFromLockedMacIOSurface(aSurface);
180
aSurface->Unlock();
181
return result.forget();
182
}
183
184
} // namespace layers
185
} // namespace mozilla