2012-09-13 20:52:26 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; 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/. */
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
#include "mozilla/layers/YCbCrImageDataSerializer.h"
|
2013-07-26 19:28:31 +00:00
|
|
|
#include "ImageTypes.h"
|
2013-07-30 09:59:51 +00:00
|
|
|
#include "yuv_convert.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
|
|
|
#include "gfx2DGlue.h"
|
2012-09-13 20:52:26 +00:00
|
|
|
|
|
|
|
#define MOZ_ALIGN_WORD(x) (((x) + 3) & ~3)
|
|
|
|
using namespace mozilla::ipc;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
// The Data is layed out as follows:
|
|
|
|
//
|
2013-05-23 07:17:10 +00:00
|
|
|
// +-----------------+ -++ --+ --+ <-- Beginning of the buffer
|
2012-09-13 20:52:26 +00:00
|
|
|
// | YCbCrBufferInfo | | | |
|
|
|
|
// +-----------------+ --+ | |
|
|
|
|
// | data | | | YCbCrBufferInfo->[mY/mCb/mCr]Offset
|
|
|
|
// +-----------------+ ------+ |
|
|
|
|
// | data | |
|
|
|
|
// +-----------------+ ----------+
|
|
|
|
// | data |
|
|
|
|
// +-----------------+
|
|
|
|
//
|
|
|
|
// There can be padding between the blocks above to keep word alignment.
|
|
|
|
|
|
|
|
// Structure written at the beginning og the data blob containing the image
|
|
|
|
// (as shown in the figure above). It contains the necessary informations to
|
|
|
|
// read the image in the blob.
|
|
|
|
struct YCbCrBufferInfo
|
|
|
|
{
|
|
|
|
uint32_t mYOffset;
|
|
|
|
uint32_t mCbOffset;
|
|
|
|
uint32_t mCrOffset;
|
|
|
|
uint32_t mYWidth;
|
|
|
|
uint32_t mYHeight;
|
|
|
|
uint32_t mCbCrWidth;
|
|
|
|
uint32_t mCbCrHeight;
|
2013-08-04 07:46:17 +00:00
|
|
|
StereoMode mStereoMode;
|
2012-09-13 20:52:26 +00:00
|
|
|
};
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
static YCbCrBufferInfo* GetYCbCrBufferInfo(uint8_t* aData)
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
return reinterpret_cast<YCbCrBufferInfo*>(aData);
|
2012-09-13 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
bool YCbCrImageDataDeserializerBase::IsValid()
|
|
|
|
{
|
|
|
|
if (mData == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-13 20:52:26 +00:00
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
uint8_t* YCbCrImageDataDeserializerBase::GetYData()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return reinterpret_cast<uint8_t*>(info) + info->mYOffset;
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
uint8_t* YCbCrImageDataDeserializerBase::GetCbData()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return reinterpret_cast<uint8_t*>(info) + info->mCbOffset;
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
uint8_t* YCbCrImageDataDeserializerBase::GetCrData()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return reinterpret_cast<uint8_t*>(info) + info->mCrOffset;
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
uint8_t* YCbCrImageDataDeserializerBase::GetData()
|
2013-04-05 17:24:58 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2013-04-05 17:24:58 +00:00
|
|
|
return (reinterpret_cast<uint8_t*>(info)) + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
uint32_t YCbCrImageDataDeserializerBase::GetYStride()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return info->mYWidth;
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
uint32_t YCbCrImageDataDeserializerBase::GetCbCrStride()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return info->mCbCrWidth;
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
gfxIntSize YCbCrImageDataDeserializerBase::GetYSize()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return gfxIntSize(info->mYWidth, info->mYHeight);
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
gfxIntSize YCbCrImageDataDeserializerBase::GetCbCrSize()
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
return gfxIntSize(info->mCbCrWidth, info->mCbCrHeight);
|
|
|
|
}
|
|
|
|
|
2013-08-04 07:46:17 +00:00
|
|
|
StereoMode YCbCrImageDataDeserializerBase::GetStereoMode()
|
|
|
|
{
|
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
|
|
|
return info->mStereoMode;
|
|
|
|
}
|
|
|
|
|
2012-09-13 20:52:26 +00:00
|
|
|
// Offset in bytes
|
|
|
|
static size_t ComputeOffset(uint32_t aHeight, uint32_t aStride)
|
|
|
|
{
|
|
|
|
return MOZ_ALIGN_WORD(aHeight * aStride);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minimum required shmem size in bytes
|
2013-05-23 07:17:10 +00:00
|
|
|
size_t
|
|
|
|
YCbCrImageDataSerializer::ComputeMinBufferSize(const gfx::IntSize& aYSize,
|
|
|
|
const gfx::IntSize& aCbCrSize)
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
|
|
|
uint32_t yStride = aYSize.width;
|
|
|
|
uint32_t CbCrStride = aCbCrSize.width;
|
|
|
|
|
|
|
|
return ComputeOffset(aYSize.height, yStride)
|
|
|
|
+ 2 * ComputeOffset(aCbCrSize.height, CbCrStride)
|
|
|
|
+ MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
size_t
|
|
|
|
YCbCrImageDataSerializer::ComputeMinBufferSize(const gfxIntSize& aYSize,
|
|
|
|
const gfxIntSize& aCbCrSize)
|
|
|
|
{
|
|
|
|
return ComputeMinBufferSize(gfx::IntSize(aYSize.width, aYSize.height),
|
|
|
|
gfx::IntSize(aCbCrSize.width, aCbCrSize.height));
|
|
|
|
}
|
2013-04-05 17:24:58 +00:00
|
|
|
// Offset in bytes
|
|
|
|
static size_t ComputeOffset(uint32_t aSize)
|
|
|
|
{
|
|
|
|
return MOZ_ALIGN_WORD(aSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minimum required shmem size in bytes
|
2013-05-23 07:17:10 +00:00
|
|
|
size_t
|
|
|
|
YCbCrImageDataSerializer::ComputeMinBufferSize(uint32_t aSize)
|
2013-04-05 17:24:58 +00:00
|
|
|
{
|
|
|
|
return ComputeOffset(aSize) + MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
void
|
|
|
|
YCbCrImageDataSerializer::InitializeBufferInfo(const gfx::IntSize& aYSize,
|
2013-08-04 07:46:17 +00:00
|
|
|
const gfx::IntSize& aCbCrSize,
|
|
|
|
StereoMode aStereoMode)
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
YCbCrBufferInfo* info = GetYCbCrBufferInfo(mData);
|
2012-09-13 20:52:26 +00:00
|
|
|
info->mYOffset = MOZ_ALIGN_WORD(sizeof(YCbCrBufferInfo));
|
|
|
|
info->mCbOffset = info->mYOffset
|
|
|
|
+ MOZ_ALIGN_WORD(aYSize.width * aYSize.height);
|
|
|
|
info->mCrOffset = info->mCbOffset
|
|
|
|
+ MOZ_ALIGN_WORD(aCbCrSize.width * aCbCrSize.height);
|
|
|
|
|
|
|
|
info->mYWidth = aYSize.width;
|
|
|
|
info->mYHeight = aYSize.height;
|
|
|
|
info->mCbCrWidth = aCbCrSize.width;
|
|
|
|
info->mCbCrHeight = aCbCrSize.height;
|
2013-08-04 07:46:17 +00:00
|
|
|
info->mStereoMode = aStereoMode;
|
2012-09-13 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
void
|
|
|
|
YCbCrImageDataSerializer::InitializeBufferInfo(const gfxIntSize& aYSize,
|
2013-08-04 07:46:17 +00:00
|
|
|
const gfxIntSize& aCbCrSize,
|
|
|
|
StereoMode aStereoMode)
|
2012-09-13 20:52:26 +00:00
|
|
|
{
|
2013-05-23 07:17:10 +00:00
|
|
|
InitializeBufferInfo(gfx::IntSize(aYSize.width, aYSize.height),
|
2013-08-04 07:46:17 +00:00
|
|
|
gfx::IntSize(aCbCrSize.width, aCbCrSize.height),
|
|
|
|
aStereoMode);
|
2012-09-13 20:52:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 10:36:33 +00:00
|
|
|
static void CopyLineWithSkip(const uint8_t* src, uint8_t* dst, uint32_t len, uint32_t skip) {
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
*dst = *src;
|
|
|
|
src += 1 + skip;
|
|
|
|
++dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-23 07:17:10 +00:00
|
|
|
bool
|
|
|
|
YCbCrImageDataSerializer::CopyData(const uint8_t* aYData,
|
|
|
|
const uint8_t* aCbData, const uint8_t* aCrData,
|
|
|
|
gfxIntSize aYSize, uint32_t aYStride,
|
|
|
|
gfxIntSize aCbCrSize, uint32_t aCbCrStride,
|
|
|
|
uint32_t aYSkip, uint32_t aCbCrSkip)
|
2012-10-29 15:08:06 +00:00
|
|
|
{
|
|
|
|
if (!IsValid() || GetYSize() != aYSize || GetCbCrSize() != aCbCrSize) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-04 10:36:33 +00:00
|
|
|
for (int i = 0; i < aYSize.height; ++i) {
|
|
|
|
if (aYSkip == 0) {
|
|
|
|
// fast path
|
|
|
|
memcpy(GetYData() + i * GetYStride(),
|
|
|
|
aYData + i * aYStride,
|
|
|
|
aYSize.width);
|
|
|
|
} else {
|
|
|
|
// slower path
|
|
|
|
CopyLineWithSkip(aYData + i * aYStride,
|
|
|
|
GetYData() + i * GetYStride(),
|
|
|
|
aYSize.width, aYSkip);
|
|
|
|
}
|
2012-10-29 15:08:06 +00:00
|
|
|
}
|
2012-12-04 10:36:33 +00:00
|
|
|
for (int i = 0; i < aCbCrSize.height; ++i) {
|
|
|
|
if (aCbCrSkip == 0) {
|
|
|
|
// fast path
|
|
|
|
memcpy(GetCbData() + i * GetCbCrStride(),
|
|
|
|
aCbData + i * aCbCrStride,
|
|
|
|
aCbCrSize.width);
|
|
|
|
memcpy(GetCrData() + i * GetCbCrStride(),
|
|
|
|
aCrData + i * aCbCrStride,
|
|
|
|
aCbCrSize.width);
|
|
|
|
} else {
|
|
|
|
// slower path
|
|
|
|
CopyLineWithSkip(aCbData + i * aCbCrStride,
|
|
|
|
GetCbData() + i * GetCbCrStride(),
|
|
|
|
aCbCrSize.width, aCbCrSkip);
|
|
|
|
CopyLineWithSkip(aCrData + i * aCbCrStride,
|
|
|
|
GetCrData() + i * GetCbCrStride(),
|
|
|
|
aCbCrSize.width, aCbCrSkip);
|
|
|
|
}
|
2012-10-29 15:08:06 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-30 09:59:51 +00:00
|
|
|
TemporaryRef<gfx::DataSourceSurface>
|
|
|
|
YCbCrImageDataDeserializer::ToDataSourceSurface()
|
|
|
|
{
|
|
|
|
RefPtr<gfx::DataSourceSurface> result =
|
|
|
|
gfx::Factory::CreateDataSourceSurface(ToIntSize(GetYSize()), gfx::FORMAT_R8G8B8X8);
|
|
|
|
|
|
|
|
gfx::ConvertYCbCrToRGB32(GetYData(), GetCbData(), GetCrData(),
|
|
|
|
result->GetData(),
|
|
|
|
0, 0, //pic x and y
|
|
|
|
GetYSize().width, GetYSize().height,
|
|
|
|
GetYStride(), GetCbCrStride(),
|
|
|
|
result->Stride(),
|
|
|
|
gfx::YV12);
|
|
|
|
result->MarkDirty();
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2012-10-29 15:08:06 +00:00
|
|
|
|
2012-09-13 20:52:26 +00:00
|
|
|
} // namespace
|
|
|
|
} // namespace
|