libyuv/source/format_conversion.cc
fbarchard@google.com 780203897c rotate for x86 and bayer refactored - 3x faster.
BUG=1
TEST=tested with talk unittests.
Review URL: http://webrtc-codereview.appspot.com/250004

git-svn-id: http://libyuv.googlecode.com/svn/trunk@42 16f28f9a-4ce2-e073-06de-1de4eb20be90
2011-10-27 20:52:52 +00:00

443 lines
13 KiB
C++

/*
* Copyright (c) 2011 The LibYuv project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <assert.h>
#include "libyuv/cpu_id.h"
#include "video_common.h"
namespace libyuv {
// Most code in here is inspired by the material at
// http://www.siliconimaging.com/RGB%20Bayer.htm
// Forces compiler to inline, even against its better judgement. Use wisely.
#if defined(__GNUC__)
#define FORCE_INLINE __attribute__((always_inline))
#elif defined(WIN32)
#define FORCE_INLINE __forceinline
#else
#define FORCE_INLINE
#endif
// Note: to do this with Neon vld4.8 would load ARGB values into 4 registers
// and vst would select which 2 components to write. The low level would need
// to be ARGBToBG, ARGBToGB, ARGBToRG, ARGBToGR
#if defined(WIN32) && !defined(COVERAGE_ENABLED)
#define HAS_ARGBTOBAYERROW_SSSE3
__declspec(naked)
static void ARGBToBayerRow_SSSE3(const uint8* src_argb,
uint8* dst_bayer, uint32 selector, int pix) {
__asm {
mov eax, [esp + 4] // src_argb
mov edx, [esp + 8] // dst_bayer
movd xmm7, [esp + 12] // selector
mov ecx, [esp + 16] // pix
pshufd xmm7, xmm7, 0
wloop:
movdqa xmm0, [eax]
lea eax, [eax + 16]
pshufb xmm0, xmm7
movd [edx], xmm0
lea edx, [edx + 4]
sub ecx, 4
ja wloop
ret
}
}
#elif (defined(__x86_64__) || defined(__i386__)) && \
!defined(COVERAGE_ENABLED) && !defined(TARGET_IPHONE_SIMULATOR)
#define HAS_ARGBTOBAYERROW_SSSE3
static void ARGBToBayerRow_SSSE3(const uint8* src_argb, uint8* dst_bayer,
uint32 selector, int pix) {
asm volatile(
"movd %3,%%xmm7\n"
"pshufd $0x0,%%xmm7,%%xmm7\n"
"1:"
"movdqa (%0),%%xmm0\n"
"lea 0x10(%0),%0\n"
"pshufb %%xmm7,%%xmm0\n"
"movd %%xmm0,(%1)\n"
"lea 0x4(%1),%1\n"
"sub $0x4,%2\n"
"ja 1b\n"
: "+r"(src_argb), // %0
"+r"(dst_bayer), // %1
"+r"(pix) // %2
: "r"(selector) // %3
: "memory"
);
}
#endif
static void ARGBToBayerRow_C(const uint8* src_argb,
uint8* dst_bayer, uint32 selector, int pix) {
int index0 = selector & 0xff;
int index1 = (selector >> 8) & 0xff;
// Copy a row of Bayer.
for (int x = 0; x < (pix - 1); x += 2) {
dst_bayer[0] = src_argb[index0];
dst_bayer[1] = src_argb[index1];
src_argb += 8;
dst_bayer += 2;
}
if (pix & 1) {
dst_bayer[0] = src_argb[index0];
}
}
// generate a selector mask useful for pshufb
static uint32 GenerateSelector(int select0, int select1) {
return static_cast<uint32>(select0) |
static_cast<uint32>((select1 + 4) << 8) |
static_cast<uint32>((select0 + 8) << 16) |
static_cast<uint32>((select1 + 12) << 24);
}
// Converts 32 bit ARGB to any Bayer RGB format.
int ARGBToBayerRGB(const uint8* src_rgb, int src_stride_rgb,
uint8* dst_bayer, int dst_stride_bayer,
uint32 dst_fourcc_bayer,
int width, int height) {
if (height < 0) {
height = -height;
src_rgb = src_rgb + (height - 1) * src_stride_rgb;
src_stride_rgb = -src_stride_rgb;
}
void (*ARGBToBayerRow)(const uint8* src_argb,
uint8* dst_bayer, uint32 selector, int pix);
#if defined(HAS_ARGBTOBAYERROW_SSSE3)
if (libyuv::TestCpuFlag(libyuv::kCpuHasSSSE3) &&
(width % 4 == 0) &&
IS_ALIGNED(src_rgb, 16) && (src_stride_rgb % 16 == 0) &&
IS_ALIGNED(dst_bayer, 4) && (dst_stride_bayer % 4 == 0)) {
ARGBToBayerRow = ARGBToBayerRow_SSSE3;
} else
#endif
{
ARGBToBayerRow = ARGBToBayerRow_C;
}
int blue_index = 0;
int green_index = 1;
int red_index = 2;
// Now build a lookup table containing the indices for the four pixels in each
// 2x2 Bayer grid.
uint32 index_map[2];
switch (dst_fourcc_bayer) {
default:
assert(false);
case FOURCC_RGGB:
index_map[0] = GenerateSelector(red_index, green_index);
index_map[1] = GenerateSelector(green_index, blue_index);
break;
case FOURCC_BGGR:
index_map[0] = GenerateSelector(blue_index, green_index);
index_map[1] = GenerateSelector(green_index, red_index);
break;
case FOURCC_GRBG:
index_map[0] = GenerateSelector(green_index, red_index);
index_map[1] = GenerateSelector(blue_index, green_index);
break;
case FOURCC_GBRG:
index_map[0] = GenerateSelector(green_index, blue_index);
index_map[1] = GenerateSelector(red_index, green_index);
break;
}
// Now convert.
for (int y = 0; y < height; ++y) {
ARGBToBayerRow(src_rgb, dst_bayer, index_map[y & 1], width);
src_rgb += src_stride_rgb;
dst_bayer += dst_stride_bayer;
}
return 0;
}
#define AVG(a,b) (((a) + (b)) >> 1)
static void BayerRowBG(const uint8* src_bayer0, int src_stride_bayer,
uint8* dst_rgb, int pix) {
const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
uint8 g = src_bayer0[1];
uint8 r = src_bayer1[1];
for (int x = 0; x < (pix - 2); x += 2) {
dst_rgb[0] = src_bayer0[0];
dst_rgb[1] = AVG(g, src_bayer0[1]);
dst_rgb[2] = AVG(r, src_bayer1[1]);
dst_rgb[3] = 255U;
dst_rgb[4] = AVG(src_bayer0[0], src_bayer0[2]);
dst_rgb[5] = src_bayer0[1];
dst_rgb[6] = src_bayer1[1];
dst_rgb[7] = 255U;
g = src_bayer0[1];
r = src_bayer1[1];
src_bayer0 += 2;
src_bayer1 += 2;
dst_rgb += 8;
}
dst_rgb[0] = src_bayer0[0];
dst_rgb[1] = AVG(g, src_bayer0[1]);
dst_rgb[2] = AVG(r, src_bayer1[1]);
dst_rgb[3] = 255U;
dst_rgb[4] = src_bayer0[0];
dst_rgb[5] = src_bayer0[1];
dst_rgb[6] = src_bayer1[1];
dst_rgb[7] = 255U;
}
static void BayerRowRG(const uint8* src_bayer0, int src_stride_bayer,
uint8* dst_rgb, int pix) {
const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
uint8 g = src_bayer0[1];
uint8 b = src_bayer1[1];
for (int x = 0; x < (pix - 2); x += 2) {
dst_rgb[0] = AVG(b, src_bayer1[1]);
dst_rgb[1] = AVG(g, src_bayer0[1]);
dst_rgb[2] = src_bayer0[0];
dst_rgb[3] = 255U;
dst_rgb[4] = src_bayer1[1];
dst_rgb[5] = src_bayer0[1];
dst_rgb[6] = AVG(src_bayer0[0], src_bayer0[2]);
dst_rgb[7] = 255U;
g = src_bayer0[1];
b = src_bayer1[1];
src_bayer0 += 2;
src_bayer1 += 2;
dst_rgb += 8;
}
dst_rgb[0] = AVG(b, src_bayer1[1]);
dst_rgb[1] = AVG(g, src_bayer0[1]);
dst_rgb[2] = src_bayer0[0];
dst_rgb[3] = 255U;
dst_rgb[4] = src_bayer1[1];
dst_rgb[5] = src_bayer0[1];
dst_rgb[6] = src_bayer0[0];
dst_rgb[7] = 255U;
}
static void BayerRowGB(const uint8* src_bayer0, int src_stride_bayer,
uint8* dst_rgb, int pix) {
const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
uint8 b = src_bayer0[1];
for (int x = 0; x < (pix - 2); x += 2) {
dst_rgb[0] = AVG(b, src_bayer0[1]);
dst_rgb[1] = src_bayer0[0];
dst_rgb[2] = src_bayer1[0];
dst_rgb[3] = 255U;
dst_rgb[4] = src_bayer0[1];
dst_rgb[5] = AVG(src_bayer0[0], src_bayer0[2]);
dst_rgb[6] = AVG(src_bayer1[0], src_bayer1[2]);
dst_rgb[7] = 255U;
b = src_bayer0[1];
src_bayer0 += 2;
src_bayer1 += 2;
dst_rgb += 8;
}
dst_rgb[0] = AVG(b, src_bayer0[1]);
dst_rgb[1] = src_bayer0[0];
dst_rgb[2] = src_bayer1[0];
dst_rgb[3] = 255U;
dst_rgb[4] = src_bayer0[1];
dst_rgb[5] = src_bayer0[0];
dst_rgb[6] = src_bayer1[0];
dst_rgb[7] = 255U;
}
static void BayerRowGR(const uint8* src_bayer0, int src_stride_bayer,
uint8* dst_rgb, int pix) {
const uint8* src_bayer1 = src_bayer0 + src_stride_bayer;
uint8 r = src_bayer0[1];
for (int x = 0; x < (pix - 2); x += 2) {
dst_rgb[0] = src_bayer1[0];
dst_rgb[1] = src_bayer0[0];
dst_rgb[2] = AVG(r, src_bayer0[1]);
dst_rgb[3] = 255U;
dst_rgb[4] = AVG(src_bayer1[0], src_bayer1[2]);
dst_rgb[5] = AVG(src_bayer0[0], src_bayer0[2]);
dst_rgb[6] = src_bayer0[1];
dst_rgb[7] = 255U;
r = src_bayer0[1];
src_bayer0 += 2;
src_bayer1 += 2;
dst_rgb += 8;
}
dst_rgb[0] = src_bayer1[0];
dst_rgb[1] = src_bayer0[0];
dst_rgb[2] = AVG(r, src_bayer0[1]);
dst_rgb[3] = 255U;
dst_rgb[4] = src_bayer1[0];
dst_rgb[5] = src_bayer0[0];
dst_rgb[6] = src_bayer0[1];
dst_rgb[7] = 255U;
}
// Converts any Bayer RGB format to ARGB.
int BayerRGBToARGB(const uint8* src_bayer, int src_stride_bayer,
uint32 src_fourcc_bayer,
uint8* dst_rgb, int dst_stride_rgb,
int width, int height) {
if (height < 0) {
height = -height;
dst_rgb = dst_rgb + (height - 1) * dst_stride_rgb;
dst_stride_rgb = -dst_stride_rgb;
}
void (*BayerRow0)(const uint8* src_bayer, int src_stride_bayer,
uint8* dst_rgb, int pix);
void (*BayerRow1)(const uint8* src_bayer, int src_stride_bayer,
uint8* dst_rgb, int pix);
switch (src_fourcc_bayer) {
default:
assert(false);
case FOURCC_RGGB:
BayerRow0 = BayerRowRG;
BayerRow1 = BayerRowGB;
break;
case FOURCC_BGGR:
BayerRow0 = BayerRowBG;
BayerRow1 = BayerRowGR;
break;
case FOURCC_GRBG:
BayerRow0 = BayerRowGR;
BayerRow1 = BayerRowBG;
break;
case FOURCC_GBRG:
BayerRow0 = BayerRowGB;
BayerRow1 = BayerRowRG;
break;
}
for (int y = 0; y < (height - 1); y += 2) {
BayerRow0(src_bayer, src_stride_bayer, dst_rgb, width);
BayerRow1(src_bayer + src_stride_bayer, -src_stride_bayer,
dst_rgb + dst_stride_rgb, width);
src_bayer += src_stride_bayer * 2;
dst_rgb += dst_stride_rgb * 2;
}
if (height & 1) {
BayerRow0(src_bayer, -src_stride_bayer, dst_rgb, width);
}
return 0;
}
// Taken from http://en.wikipedia.org/wiki/YUV
static FORCE_INLINE int RGBToY(uint8 r, uint8 g, uint8 b) {
return (( 66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
}
static FORCE_INLINE int RGBToU(uint8 r, uint8 g, uint8 b) {
return ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
}
static FORCE_INLINE int RGBToV(uint8 r, uint8 g, uint8 b) {
return ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
}
static void ARGBtoYRow(const uint8* src_argb0,
uint8* dst_y, int width) {
for (int x = 0; x < width; ++x) {
dst_y[0] = RGBToY(src_argb0[2], src_argb0[1], src_argb0[0]);
src_argb0 += 4;
dst_y += 1;
}
}
static void ARGBtoUVRow(const uint8* src_argb0, int src_stride_argb,
uint8* dst_u,
uint8* dst_v,
int width) {
const uint8* src_argb1 = src_argb0 + src_stride_argb;
for (int x = 0; x < width - 1; x += 2) {
uint8 ab = (src_argb0[0] + src_argb0[4] + src_argb1[0] + src_argb1[4]) >> 2;
uint8 ag = (src_argb0[1] + src_argb0[5] + src_argb1[1] + src_argb1[5]) >> 2;
uint8 ar = (src_argb0[2] + src_argb0[6] + src_argb1[2] + src_argb1[6]) >> 2;
dst_u[0] = RGBToU(ar, ag, ab);
dst_v[0] = RGBToV(ar, ag, ab);
src_argb0 += 8;
src_argb1 += 8;
dst_u += 1;
dst_v += 1;
}
}
// Converts any Bayer RGB format to ARGB.
int BayerRGBToI420(const uint8* src_bayer, int src_stride_bayer,
uint32 src_fourcc_bayer,
uint8* dst_y, int dst_stride_y,
uint8* dst_u, int dst_stride_u,
uint8* dst_v, int dst_stride_v,
int width, int height) {
// Negative height means invert the image.
if (height < 0) {
height = -height;
int halfheight = (height + 1) >> 1;
dst_y = dst_y + (height - 1) * dst_stride_y;
dst_u = dst_u + (halfheight - 1) * dst_stride_u;
dst_v = dst_v + (halfheight - 1) * dst_stride_v;
dst_stride_y = -dst_stride_y;
dst_stride_u = -dst_stride_u;
dst_stride_v = -dst_stride_v;
}
void (*BayerRow0)(const uint8* src_bayer, int src_stride_bayer,
uint8* dst_rgb, int pix);
void (*BayerRow1)(const uint8* src_bayer, int src_stride_bayer,
uint8* dst_rgb, int pix);
switch (src_fourcc_bayer) {
default:
assert(false);
case FOURCC_RGGB:
BayerRow0 = BayerRowRG;
BayerRow1 = BayerRowGB;
break;
case FOURCC_BGGR:
BayerRow0 = BayerRowBG;
BayerRow1 = BayerRowGR;
break;
case FOURCC_GRBG:
BayerRow0 = BayerRowGR;
BayerRow1 = BayerRowBG;
break;
case FOURCC_GBRG:
BayerRow0 = BayerRowGB;
BayerRow1 = BayerRowRG;
break;
}
#define kMaxStride 2048 * 4
uint8 row[kMaxStride * 2];
for (int y = 0; y < (height - 1); y += 2) {
BayerRow0(src_bayer, src_stride_bayer, row, width);
BayerRow1(src_bayer + src_stride_bayer, -src_stride_bayer,
row + kMaxStride, width);
ARGBtoYRow(row, dst_y, width);
ARGBtoYRow(row + kMaxStride, dst_y + dst_stride_y, width);
ARGBtoUVRow(row, kMaxStride, dst_u, dst_v, width);
src_bayer += src_stride_bayer * 2;
dst_y += dst_stride_y * 2;
dst_u += dst_stride_u;
dst_v += dst_stride_v;
}
if (height & 1) {
BayerRow0(src_bayer, src_stride_bayer, row, width);
ARGBtoYRow(row, dst_y, width);
ARGBtoUVRow(row, 0, dst_u, dst_v, width);
}
return 0;
}
} // namespace libyuv