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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* 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 "mozilla/EndianUtils.h"
#include "gfx2DGlue.h"
#include "mozilla/gfx/Swizzle.h"
#include "YCbCrUtils.h"
#include "yuv_convert.h"
#include "ycbcr_to_rgb565.h"
namespace mozilla {
namespace gfx {
// clang-format off
void
GetYCbCrToRGBDestFormatAndSize(const layers::PlanarYCbCrData& aData,
SurfaceFormat& aSuggestedFormat,
IntSize& aSuggestedSize)
{
YUVType yuvtype =
TypeFromSize(aData.mYSize.width,
aData.mYSize.height,
aData.mCbCrSize.width,
aData.mCbCrSize.height);
// 'prescale' is true if the scaling is to be done as part of the
// YCbCr to RGB conversion rather than on the RGB data when rendered.
bool prescale = aSuggestedSize.width > 0 && aSuggestedSize.height > 0 &&
aSuggestedSize != aData.mPicSize;
if (aSuggestedFormat == SurfaceFormat::R5G6B5_UINT16) {
#if defined(HAVE_YCBCR_TO_RGB565)
if (prescale &&
!IsScaleYCbCrToRGB565Fast(aData.mPicX,
aData.mPicY,
aData.mPicSize.width,
aData.mPicSize.height,
aSuggestedSize.width,
aSuggestedSize.height,
yuvtype,
FILTER_BILINEAR) &&
IsConvertYCbCrToRGB565Fast(aData.mPicX,
aData.mPicY,
aData.mPicSize.width,
aData.mPicSize.height,
yuvtype)) {
prescale = false;
}
#else
// yuv2rgb16 function not available
aSuggestedFormat = SurfaceFormat::B8G8R8X8;
#endif
}
else if (aSuggestedFormat != SurfaceFormat::B8G8R8X8) {
// No other formats are currently supported.
aSuggestedFormat = SurfaceFormat::B8G8R8X8;
}
if (aSuggestedFormat == SurfaceFormat::B8G8R8X8) {
/* ScaleYCbCrToRGB32 does not support a picture offset, nor 4:4:4 data.
See bugs 639415 and 640073. */
if (aData.mPicX != 0 || aData.mPicY != 0 || yuvtype == YV24)
prescale = false;
}
if (!prescale) {
aSuggestedSize = aData.mPicSize;
}
}
static inline void
ConvertYCbCr16to8Line(uint8_t* aDst,
int aStride,
const uint16_t* aSrc,
int aStride16,
int aWidth,
int aHeight,
int aBitDepth)
{
uint16_t mask = (1 << aBitDepth) - 1;
for (int i = 0; i < aHeight; i++) {
for (int j = 0; j < aWidth; j++) {
uint16_t val = (aSrc[j] & mask) >> (aBitDepth - 8);
aDst[j] = val;
}
aDst += aStride;
aSrc += aStride16;
}
}
void
ConvertYCbCrToRGBInternal(const layers::PlanarYCbCrData& aData,
const SurfaceFormat& aDestFormat,
const IntSize& aDestSize,
unsigned char* aDestBuffer,
int32_t aStride)
{
// ConvertYCbCrToRGB et al. assume the chroma planes are rounded up if the
// luma plane is odd sized. Monochrome images have 0-sized CbCr planes
MOZ_ASSERT(aData.mCbCrSize.width == aData.mYSize.width ||
aData.mCbCrSize.width == (aData.mYSize.width + 1) >> 1 ||
aData.mCbCrSize.width == 0);
MOZ_ASSERT(aData.mCbCrSize.height == aData.mYSize.height ||
aData.mCbCrSize.height == (aData.mYSize.height + 1) >> 1 ||
aData.mCbCrSize.height == 0);
// Used if converting to 8 bits YUV.
UniquePtr<uint8_t[]> yChannel;
UniquePtr<uint8_t[]> cbChannel;
UniquePtr<uint8_t[]> crChannel;
layers::PlanarYCbCrData dstData;
const layers::PlanarYCbCrData& srcData =
aData.mColorDepth == ColorDepth::COLOR_8 ? aData : dstData;
if (aData.mColorDepth != ColorDepth::COLOR_8) {
// Convert to 8 bits data first.
dstData.mPicSize = aData.mPicSize;
dstData.mPicX = aData.mPicX;
dstData.mPicY = aData.mPicY;
dstData.mYSize = aData.mYSize;
// We align the destination stride to 32 bytes, so that libyuv can use
// SSE optimised code.
dstData.mYStride = (aData.mYSize.width + 31) & ~31;
dstData.mCbCrSize = aData.mCbCrSize;
dstData.mCbCrStride = (aData.mCbCrSize.width + 31) & ~31;
dstData.mYUVColorSpace = aData.mYUVColorSpace;
dstData.mColorDepth = ColorDepth::COLOR_8;
size_t ySize = GetAlignedStride<1>(dstData.mYStride, aData.mYSize.height);
size_t cbcrSize =
GetAlignedStride<1>(dstData.mCbCrStride, aData.mCbCrSize.height);
if (ySize == 0) {
MOZ_DIAGNOSTIC_ASSERT(cbcrSize == 0, "CbCr without Y makes no sense");
return;
}
yChannel = MakeUnique<uint8_t[]>(ySize);
dstData.mYChannel = yChannel.get();
int bitDepth = BitDepthForColorDepth(aData.mColorDepth);
ConvertYCbCr16to8Line(dstData.mYChannel,
dstData.mYStride,
reinterpret_cast<uint16_t*>(aData.mYChannel),
aData.mYStride / 2,
aData.mYSize.width,
aData.mYSize.height,
bitDepth);
if (cbcrSize) {
cbChannel = MakeUnique<uint8_t[]>(cbcrSize);
crChannel = MakeUnique<uint8_t[]>(cbcrSize);
dstData.mCbChannel = cbChannel.get();
dstData.mCrChannel = crChannel.get();
ConvertYCbCr16to8Line(dstData.mCbChannel,
dstData.mCbCrStride,
reinterpret_cast<uint16_t*>(aData.mCbChannel),
aData.mCbCrStride / 2,
aData.mCbCrSize.width,
aData.mCbCrSize.height,
bitDepth);
ConvertYCbCr16to8Line(dstData.mCrChannel,
dstData.mCbCrStride,
reinterpret_cast<uint16_t*>(aData.mCrChannel),
aData.mCbCrStride / 2,
aData.mCbCrSize.width,
aData.mCbCrSize.height,
bitDepth);
}
}
YUVType yuvtype =
TypeFromSize(srcData.mYSize.width,
srcData.mYSize.height,
srcData.mCbCrSize.width,
srcData.mCbCrSize.height);
// Convert from YCbCr to RGB now, scaling the image if needed.
if (aDestSize != srcData.mPicSize) {
#if defined(HAVE_YCBCR_TO_RGB565)
if (aDestFormat == SurfaceFormat::R5G6B5_UINT16) {
ScaleYCbCrToRGB565(srcData.mYChannel,
srcData.mCbChannel,
srcData.mCrChannel,
aDestBuffer,
srcData.mPicX,
srcData.mPicY,
srcData.mPicSize.width,
srcData.mPicSize.height,
aDestSize.width,
aDestSize.height,
srcData.mYStride,
srcData.mCbCrStride,
aStride,
yuvtype,
FILTER_BILINEAR);
} else
#endif
ScaleYCbCrToRGB32(srcData.mYChannel, //
srcData.mCbChannel,
srcData.mCrChannel,
aDestBuffer,
srcData.mPicSize.width,
srcData.mPicSize.height,
aDestSize.width,
aDestSize.height,
srcData.mYStride,
srcData.mCbCrStride,
aStride,
yuvtype,
srcData.mYUVColorSpace,
FILTER_BILINEAR);
} else { // no prescale
#if defined(HAVE_YCBCR_TO_RGB565)
if (aDestFormat == SurfaceFormat::R5G6B5_UINT16) {
ConvertYCbCrToRGB565(srcData.mYChannel,
srcData.mCbChannel,
srcData.mCrChannel,
aDestBuffer,
srcData.mPicX,
srcData.mPicY,
srcData.mPicSize.width,
srcData.mPicSize.height,
srcData.mYStride,
srcData.mCbCrStride,
aStride,
yuvtype);
} else // aDestFormat != SurfaceFormat::R5G6B5_UINT16
#endif
ConvertYCbCrToRGB32(srcData.mYChannel, //
srcData.mCbChannel,
srcData.mCrChannel,
aDestBuffer,
srcData.mPicX,
srcData.mPicY,
srcData.mPicSize.width,
srcData.mPicSize.height,
srcData.mYStride,
srcData.mCbCrStride,
aStride,
yuvtype,
srcData.mYUVColorSpace);
}
}
void ConvertYCbCrToRGB(const layers::PlanarYCbCrData& aData,
const SurfaceFormat& aDestFormat,
const IntSize& aDestSize, unsigned char* aDestBuffer,
int32_t aStride) {
ConvertYCbCrToRGBInternal(aData, aDestFormat, aDestSize, aDestBuffer,
aStride);
#if MOZ_BIG_ENDIAN()
// libyuv makes endian-correct result, which needs to be swapped to BGRX
if (aDestFormat != SurfaceFormat::R5G6B5_UINT16)
gfx::SwizzleData(aDestBuffer, aStride, gfx::SurfaceFormat::X8R8G8B8,
aDestBuffer, aStride, gfx::SurfaceFormat::B8G8R8X8,
aData.mPicSize);
#endif
}
void FillAlphaToRGBA(const uint8_t* aAlpha, const int32_t aAlphaStride,
uint8_t* aBuffer, const int32_t aWidth,
const int32_t aHeight, const gfx::SurfaceFormat& aFormat) {
MOZ_ASSERT(aAlphaStride >= aWidth);
MOZ_ASSERT(aFormat ==
SurfaceFormat::B8G8R8A8); // required for SurfaceFormatBit::OS_A
const int bpp = BytesPerPixel(aFormat);
const size_t rgbaStride = aWidth * bpp;
const uint8_t* src = aAlpha;
for (int32_t h = 0; h < aHeight; ++h) {
size_t offset = static_cast<size_t>(SurfaceFormatBit::OS_A) / 8;
for (int32_t w = 0; w < aWidth; ++w) {
aBuffer[offset] = src[w];
offset += bpp;
}
src += aAlphaStride;
aBuffer += rgbaStride;
}
}
void ConvertYCbCrAToARGB(const layers::PlanarYCbCrAData& aData,
const SurfaceFormat& aDestFormat,
const IntSize& aDestSize, unsigned char* aDestBuffer,
int32_t aStride) {
// libyuv makes endian-correct result, so the format needs to be B8G8R8A8.
MOZ_ASSERT(aDestFormat == SurfaceFormat::B8G8R8A8);
MOZ_ASSERT(aData.mAlphaSize == aData.mYSize);
YUVType yuvtype = TypeFromSize(aData.mYSize.width, aData.mYSize.height,
aData.mCbCrSize.width, aData.mCbCrSize.height);
if (yuvtype == YV12) {
// Currently this function only has support for I420 type
ConvertI420AlphaToARGB(aData.mYChannel, aData.mCbChannel, aData.mCrChannel,
aData.mAlphaChannel, aData.mYStride,
aData.mCbCrStride, aDestBuffer, aStride,
aData.mPicSize.width, aData.mPicSize.height);
return;
}
// This function converts non-8-bpc images to 8-bpc. (Bug 1682322)
ConvertYCbCrToRGBInternal(aData, aDestFormat, aDestSize, aDestBuffer,
aStride);
// Note alpha stride is equal to Y stride.
FillAlphaToRGBA(aData.mAlphaChannel, aData.mYStride, aDestBuffer,
aData.mPicSize.width, aData.mPicSize.height, aDestFormat);
// Do preattenuate as what ConvertI420AlphaToARGB does
ARGBAttenuate(aDestBuffer, aStride, aDestBuffer, aStride,
aData.mPicSize.width, aData.mPicSize.height);
#if MOZ_BIG_ENDIAN()
// libyuv makes endian-correct result, which needs to be swapped to BGRA
gfx::SwizzleData(aDestBuffer, aStride, gfx::SurfaceFormat::A8R8G8B8,
aDestBuffer, aStride, gfx::SurfaceFormat::B8G8R8A8,
aData.mPicSize);
#endif
}
void
ConvertI420AlphaToARGB(const uint8_t* aSrcY,
const uint8_t* aSrcU,
const uint8_t* aSrcV,
const uint8_t* aSrcA,
int aSrcStrideYA, int aSrcStrideUV,
uint8_t* aDstARGB, int aDstStrideARGB,
int aWidth, int aHeight) {
ConvertI420AlphaToARGB32(aSrcY,
aSrcU,
aSrcV,
aSrcA,
aDstARGB,
aWidth,
aHeight,
aSrcStrideYA,
aSrcStrideUV,
aDstStrideARGB);
#if MOZ_BIG_ENDIAN()
// libyuv makes endian-correct result, which needs to be swapped to BGRA
gfx::SwizzleData(aDstARGB, aDstStrideARGB, gfx::SurfaceFormat::A8R8G8B8,
aDstARGB, aDstStrideARGB, gfx::SurfaceFormat::B8G8R8A8,
IntSize(aWidth, aHeight));
#endif
}
} // namespace gfx
} // namespace mozilla
|